Skip to content

Commit

Permalink
Merge pull request #85 from guzba/master
Browse files Browse the repository at this point in the history
0.7.14 fix for pixie 5
  • Loading branch information
treeform authored Aug 21, 2022
2 parents 0ef0e0b + 6ccf6b2 commit a2a5165
Show file tree
Hide file tree
Showing 116 changed files with 263 additions and 227 deletions.
264 changes: 152 additions & 112 deletions src/typography/font.nim

Large diffs are not rendered by default.

166 changes: 83 additions & 83 deletions src/typography/rasterizer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -162,89 +162,89 @@ proc getGlyphImage*(
var color = ColorRgba(r: 255, g: 255, b: 255, a: uint8(a * 255.0))
result[x, h-y-1] = color

proc getGlyphOutlineImage*(
font: Font,
unicode: string,
lines=true,
points=true,
winding=true
): Image =
## Get an outline of the glyph with controls points. Useful for debugging.
var glyph = font.typeface.glyphs[unicode]

const
green = ColorRgba(r: 0, g: 255, b: 0, a: 255)
red = ColorRgba(r: 255, g: 0, b: 0, a: 255)
blue = ColorRgba(r: 0, g: 0, b: 255, a: 255)

glyph.makeReady(font)

var fontHeight = font.typeface.ascent - font.typeface.descent
var scale = font.size / (fontHeight)
var tx = int floor(glyph.bboxMin.x * scale)
var ty = int floor(glyph.bboxMin.y * scale)
var w = int(ceil(glyph.bboxMax.x * scale)) - tx + 1
var h = int(ceil(glyph.bboxMax.y * scale)) - ty + 1

result = newImage(w, h)
let origin = vec2(float32 tx, float32 ty)

proc adjust(v: Vec2): Vec2 = (v) * scale - origin

proc flip(v: Vec2): Vec2 =
result.x = v.x
result.y = float32(h) - v.y

let ctx = newContext(result)

# Draw the outline.
for segments in glyph.segments:
if lines:
# Draw lines.
ctx.strokeStyle = red
for s in segments:
ctx.strokeSegment(segment(flip(adjust(s.at)), flip(adjust(s.to))))
if points:
# Draw points.
ctx.strokeStyle = blue
for ruleNum, c in cast[PathShim](glyph.path).commands:

for i in 0..<c.numbers.len div 2:
var at: Vec2
at.x = c.numbers[i*2+0]
at.y = c.numbers[i*2+1]
ctx.strokeSegment(segment(
flip(adjust(at)) + vec2(1, 1),
flip(adjust(at)) + vec2(-1, -1))
)
ctx.strokeSegment(segment(
flip(adjust(at)) + vec2(-1, 1),
flip(adjust(at)) + vec2(1, -1))
)
if winding:
# Draw winding order
for s in segments:
let
at = flip(adjust(s.at))
to = flip(adjust(s.to))
length = (at - to).length
mid = (at + to) / 2
angle = angle(at - to)
dir = dir(angle) * 3
dir2 = dir(angle + float32(PI/2)) * 3
winding = s.at.y > s.to.y
var color = if winding: blue else: green

if length > 0:
# Triangle.
ctx.strokeStyle = color
let
head = mid + dir
left = mid - dir + dir2
right = mid - dir - dir2
ctx.strokeSegment(segment(head, left))
ctx.strokeSegment(segment(left, right))
ctx.strokeSegment(segment(right, head))
# proc getGlyphOutlineImage*(
# font: Font,
# unicode: string,
# lines=true,
# points=true,
# winding=true
# ): Image =
# ## Get an outline of the glyph with controls points. Useful for debugging.
# var glyph = font.typeface.glyphs[unicode]

# const
# green = ColorRgba(r: 0, g: 255, b: 0, a: 255)
# red = ColorRgba(r: 255, g: 0, b: 0, a: 255)
# blue = ColorRgba(r: 0, g: 0, b: 255, a: 255)

# glyph.makeReady(font)

# var fontHeight = font.typeface.ascent - font.typeface.descent
# var scale = font.size / (fontHeight)
# var tx = int floor(glyph.bboxMin.x * scale)
# var ty = int floor(glyph.bboxMin.y * scale)
# var w = int(ceil(glyph.bboxMax.x * scale)) - tx + 1
# var h = int(ceil(glyph.bboxMax.y * scale)) - ty + 1

# result = newImage(w, h)
# let origin = vec2(float32 tx, float32 ty)

# proc adjust(v: Vec2): Vec2 = (v) * scale - origin

# proc flip(v: Vec2): Vec2 =
# result.x = v.x
# result.y = float32(h) - v.y

# let ctx = newContext(result)

# # Draw the outline.
# for segments in glyph.segments:
# if lines:
# # Draw lines.
# ctx.strokeStyle = red
# for s in segments:
# ctx.strokeSegment(segment(flip(adjust(s.at)), flip(adjust(s.to))))
# if points:
# # Draw points.
# ctx.strokeStyle = blue
# for ruleNum, c in cast[PathShim](glyph.path).commands:

# for i in 0..<c.numbers.len div 2:
# var at: Vec2
# at.x = c.numbers[i*2+0]
# at.y = c.numbers[i*2+1]
# ctx.strokeSegment(segment(
# flip(adjust(at)) + vec2(1, 1),
# flip(adjust(at)) + vec2(-1, -1))
# )
# ctx.strokeSegment(segment(
# flip(adjust(at)) + vec2(-1, 1),
# flip(adjust(at)) + vec2(1, -1))
# )
# if winding:
# # Draw winding order
# for s in segments:
# let
# at = flip(adjust(s.at))
# to = flip(adjust(s.to))
# length = (at - to).length
# mid = (at + to) / 2
# angle = angle(at - to)
# dir = dir(angle) * 3
# dir2 = dir(angle + float32(PI/2)) * 3
# winding = s.at.y > s.to.y
# var color = if winding: blue else: green

# if length > 0:
# # Triangle.
# ctx.strokeStyle = color
# let
# head = mid + dir
# left = mid - dir + dir2
# right = mid - dir - dir2
# ctx.strokeSegment(segment(head, left))
# ctx.strokeSegment(segment(left, right))
# ctx.strokeSegment(segment(right, head))

proc getGlyphImage*(
font: Font,
Expand Down
Binary file modified tests/rendered/alignment.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/rendered/basicSvg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/rendered/basicTtf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/rendered/basicTtf2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/rendered/ch.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/rendered/hFill.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/rendered/layout.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/rendered/layoutNoText.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/rendered/picking.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/rendered/ru.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/rendered/sample_svg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/rendered/sample_ttf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/rendered/scaledup.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/rendered/selection.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/rendered/sizes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/rendered/subpixelglyphs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/rendered/subpixelpos.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/rendered/tabs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/rendered/test_otf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/rendered/test_svg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/rendered/test_ttf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/rendered/wordwrap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/rendered/wordwrapch.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 10 additions & 10 deletions tests/test.nim
Original file line number Diff line number Diff line change
Expand Up @@ -202,20 +202,20 @@ block:

image.writeFile("rendered/subpixelglyphs.png")

block:
var font = readFontTtf("fonts/Moon Bold.otf")
font.size = 300
font.lineHeight = 300
# block:
# var font = readFontTtf("fonts/Moon Bold.otf")
# font.size = 300
# font.lineHeight = 300

var image = font.getGlyphOutlineImage("Q")
# var image = font.getGlyphOutlineImage("Q")

echo font.typeface.glyphs["Q"].pathStr
# echo font.typeface.glyphs["Q"].pathStr

image.writeFile("rendered/qOutLine.png")
# image.writeFile("rendered/qOutLine.png")

image = font.getGlyphImage("Q")
image.alphaToBlackAndWhite()
image.writeFile("rendered/qFill.png")
# image = font.getGlyphImage("Q")
# image.alphaToBlackAndWhite()
# image.writeFile("rendered/qFill.png")

block:
var image = newImage(200, 100)
Expand Down
32 changes: 16 additions & 16 deletions tests/test_char.nim
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,22 @@ block:

#print glyph

var image = font.getGlyphOutlineImage(
glyph.code,
lines=true,
points=true,
winding=true
)

image.writeFile("rendered/test_char.png")

var glyphOffset: Vec2
image = font.getGlyphImage(glyph, glyphOffset, quality=4)
image.alphaToBlackAndWhite()
image.writeFile("rendered/test_charFill.png")

if image.width == 1396:
quit()
# var image = font.getGlyphOutlineImage(
# glyph.code,
# lines=true,
# points=true,
# winding=true
# )

# image.writeFile("rendered/test_char.png")

# var glyphOffset: Vec2
# image = font.getGlyphImage(glyph, glyphOffset, quality=4)
# image.alphaToBlackAndWhite()
# image.writeFile("rendered/test_charFill.png")

# if image.width == 1396:
# quit()

let (outp, _) = execCmdEx("git diff tests/rendered/*.png")
if len(outp) != 0:
Expand Down
Binary file modified tests/textbox/backspace_delete.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/textbox/copy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/textbox/cut.png
Binary file modified tests/textbox/jump_down_0.png
Binary file modified tests/textbox/jump_down_1.png
Binary file modified tests/textbox/jump_down_2.png
Binary file modified tests/textbox/jump_down_3.png
Binary file modified tests/textbox/jump_up_0.png
Binary file modified tests/textbox/jump_up_1.png
Binary file modified tests/textbox/jump_up_2.png
Binary file modified tests/textbox/jump_up_3.png
Binary file modified tests/textbox/left_right.png
Binary file modified tests/textbox/left_right_shift_0.png
Binary file modified tests/textbox/left_right_shift_1.png
Binary file modified tests/textbox/left_right_shift_10.png
Binary file modified tests/textbox/left_right_shift_11.png
Binary file modified tests/textbox/left_right_shift_12.png
Binary file modified tests/textbox/left_right_shift_13.png
Binary file modified tests/textbox/left_right_shift_14.png
Binary file modified tests/textbox/left_right_shift_15.png
Binary file modified tests/textbox/left_right_shift_16.png
Binary file modified tests/textbox/left_right_shift_17.png
Binary file modified tests/textbox/left_right_shift_18.png
Binary file modified tests/textbox/left_right_shift_19.png
Binary file modified tests/textbox/left_right_shift_2.png
Binary file modified tests/textbox/left_right_shift_20.png
Binary file modified tests/textbox/left_right_shift_21.png
Binary file modified tests/textbox/left_right_shift_22.png
Binary file modified tests/textbox/left_right_shift_23.png
Binary file modified tests/textbox/left_right_shift_3.png
Binary file modified tests/textbox/left_right_shift_4.png
Binary file modified tests/textbox/left_right_shift_5.png
Binary file modified tests/textbox/left_right_shift_6.png
Binary file modified tests/textbox/left_right_shift_7.png
Binary file modified tests/textbox/left_right_shift_8.png
Binary file modified tests/textbox/left_right_shift_9.png
Binary file modified tests/textbox/paste_0.png
Binary file modified tests/textbox/paste_1.png
Binary file modified tests/textbox/picking_0.png
Binary file modified tests/textbox/picking_1.png
Binary file modified tests/textbox/picking_10.png
Binary file modified tests/textbox/picking_2.png
Binary file modified tests/textbox/picking_3.png
Binary file modified tests/textbox/picking_4.png
Binary file modified tests/textbox/picking_5.png
Binary file modified tests/textbox/picking_6.png
Binary file modified tests/textbox/picking_7.png
Binary file modified tests/textbox/picking_8.png
Binary file modified tests/textbox/picking_9.png
Binary file modified tests/textbox/plain.png
Binary file modified tests/textbox/scroll_0.png
Binary file modified tests/textbox/scroll_1.png
Binary file modified tests/textbox/scroll_10.png
Binary file modified tests/textbox/scroll_11.png
Binary file modified tests/textbox/scroll_12.png
Binary file modified tests/textbox/scroll_13.png
Binary file modified tests/textbox/scroll_14.png
Binary file modified tests/textbox/scroll_15.png
Binary file modified tests/textbox/scroll_16.png
Binary file modified tests/textbox/scroll_17.png
Binary file modified tests/textbox/scroll_18.png
Binary file modified tests/textbox/scroll_19.png
Binary file modified tests/textbox/scroll_2.png
Binary file modified tests/textbox/scroll_20.png
Binary file modified tests/textbox/scroll_21.png
Binary file modified tests/textbox/scroll_22.png
Binary file modified tests/textbox/scroll_3.png
Binary file modified tests/textbox/scroll_4.png
Binary file modified tests/textbox/scroll_5.png
Binary file modified tests/textbox/scroll_6.png
Binary file modified tests/textbox/scroll_7.png
Binary file modified tests/textbox/scroll_8.png
Binary file modified tests/textbox/scroll_9.png
Binary file modified tests/textbox/select_all.png
Binary file modified tests/textbox/select_peragraph.png
Binary file modified tests/textbox/select_word.png
Binary file modified tests/textbox/typing.png
Binary file modified tests/textbox/up_down_0.png
Binary file modified tests/textbox/up_down_1.png
Binary file modified tests/textbox/up_down_2.png
Binary file modified tests/textbox/up_down_3.png
Binary file modified tests/textbox/up_down_4.png
Binary file modified tests/textbox/up_down_5.png
Binary file modified tests/textbox/up_down_6.png
Binary file modified tests/textbox/up_down_7.png
Binary file modified tests/textbox/up_down_8.png
Binary file modified tests/textbox/up_down_9.png
Binary file modified tests/textbox/word_backspace_delete.png
8 changes: 2 additions & 6 deletions typography.nimble
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
version = "0.7.13"
version = "0.7.14"
author = "Andre von Houck"
description = "Fonts, Typesetting and Rasterization for Nim."
license = "MIT"
srcDir = "src"

requires "nim >= 1.4.0"
requires "pixie >= 3.1.1"
requires "vmath >= 1.0.0"
requires "chroma >= 0.2.3"
requires "pixie >= 5.0.1"
requires "print >= 0.1.0"
requires "bumpy >= 1.0.3"
requires "flatty >= 0.1.3"

task docs, "Generate API documents":
exec "nimble doc --index:on --project --out:docs --hints:off src/typography.nim"

0 comments on commit a2a5165

Please sign in to comment.