This commit is contained in:
treeform 2022-03-22 14:09:00 -07:00
parent 2f88f6119a
commit b1f9b943f9
3 changed files with 21 additions and 21 deletions

View file

@ -8,13 +8,13 @@ import bumpy, chroma, pixie/common, pixie/fonts, pixie/images, pixie/masks,
type type
BaseLineAlignment* = enum BaselineAlignment* = enum
TopBaseAlign TopBaseline
HangingBaseAlign HangingBaseline
MiddleBaseAlign MiddleBaseline
AlphabeticBaseAlign AlphabeticBaseline
IdeographicBaseAlign IdeographicBaseline
BottomBaseAlign BottomBaseline
Context* = ref object Context* = ref object
image*: Image image*: Image
@ -27,7 +27,7 @@ type
font*: string ## File path to a .ttf or .otf file. font*: string ## File path to a .ttf or .otf file.
fontSize*: float32 fontSize*: float32
textAlign*: HorizontalAlignment textAlign*: HorizontalAlignment
textBaseline*: BaseLineAlignment textBaseline*: BaselineAlignment
lineDash: seq[float32] lineDash: seq[float32]
path: Path path: Path
mat: Mat3 mat: Mat3
@ -68,7 +68,7 @@ proc newContext*(image: Image): Context {.raises: [].} =
result.strokeStyle = newPaint(SolidPaint) result.strokeStyle = newPaint(SolidPaint)
result.strokeStyle.color = color(0, 0, 0, 1) result.strokeStyle.color = color(0, 0, 0, 1)
result.fontSize = 12 result.fontSize = 12
result.textBaseline = AlphabeticBaseAlign result.textBaseline = AlphabeticBaseline
proc newContext*(width, height: int): Context {.inline, raises: [PixieError].} = proc newContext*(width, height: int): Context {.inline, raises: [PixieError].} =
## Create a new Context that will draw to a new image of width and height. ## Create a new Context that will draw to a new image of width and height.
@ -198,19 +198,19 @@ proc fillText(ctx: Context, image: Image, text: string, at: Vec2) =
var at = at var at = at
case ctx.textBaseline: case ctx.textBaseline:
of TopBaseAlign: of TopBaseline:
discard discard
of HangingBaseAlign: of HangingBaseline:
# TODO: make accurate (Used by Tibetan and other Indic scripts.) # TODO: make accurate (Used by Tibetan and other Indic scripts.)
discard discard
of MiddleBaseAlign: of MiddleBaseline:
at.y -= round((font.typeface.ascent - font.typeface.descent) / 2 * font.scale) at.y -= round((font.typeface.ascent - font.typeface.descent) / 2 * font.scale)
of AlphabeticBaseAlign: of AlphabeticBaseline:
at.y -= round(font.typeface.ascent * font.scale) at.y -= round(font.typeface.ascent * font.scale)
of IdeographicBaseAlign: of IdeographicBaseline:
# TODO: make accurate (Used by Chinese, Japanese, and Korean scripts.) # TODO: make accurate (Used by Chinese, Japanese, and Korean scripts.)
at.y -= round((font.typeface.ascent - font.typeface.descent) * font.scale) at.y -= round((font.typeface.ascent - font.typeface.descent) * font.scale)
of BottomBaseAlign: of BottomBaseline:
at.y -= round((font.typeface.ascent - font.typeface.descent) * font.scale) at.y -= round((font.typeface.ascent - font.typeface.descent) * font.scale)
font.paint = ctx.fillStyle font.paint = ctx.fillStyle

Binary file not shown.

Before

Width:  |  Height:  |  Size: 71 KiB

After

Width:  |  Height:  |  Size: 68 KiB

View file

@ -653,12 +653,12 @@ block:
image.fill(rgba(255, 255, 255, 255)) image.fill(rgba(255, 255, 255, 255))
const baselines = @[ const baselines = @[
TopBaseAlign, TopBaseline,
HangingBaseAlign, HangingBaseline,
MiddleBaseAlign, MiddleBaseline,
AlphabeticBaseAlign, AlphabeticBaseline,
IdeographicBaseAlign, IdeographicBaseline,
BottomBaseAlign, BottomBaseline,
] ]
ctx.font = "tests/fonts/Roboto-Regular_1.ttf" ctx.font = "tests/fonts/Roboto-Regular_1.ttf"