simple api attempt 1

This commit is contained in:
Ryan Oldenburg 2021-04-26 02:37:40 -05:00
parent 9d2dc5c06f
commit 082f16096f
3 changed files with 68 additions and 54 deletions

View file

@ -317,3 +317,46 @@ proc strokePolygon*(
var path: Path
path.polygon(pos, size, sides)
mask.strokePath(path, strokeWidth)
proc fillText*(
image: Image,
font: Font,
text: string,
color: SomeColor,
transform: Vec2 | Mat3 = vec2(0, 0)
) =
for path in font.typeset(text):
image.fillPath(path, color, transform)
proc fillText*(
mask: Mask,
font: Font,
text: string,
color: SomeColor,
transform: Vec2 | Mat3 = vec2(0, 0)
) =
for path in font.typeset(text):
mask.fillPath(path, transform)
proc strokeText*(
image: Image,
font: Font,
text: string,
pos = vec2(0, 0),
color: SomeColor,
transform: Vec2 | Mat3 = vec2(0, 0),
strokeWidth = 1.0
) =
for path in font.typeset(text):
image.strokePath(path, color, transform, strokeWidth)
proc strokeText*(
mask: Mask,
font: Font,
text: string,
color: SomeColor,
transform: Vec2 | Mat3 = vec2(0, 0),
strokeWidth = 1.0
) =
for path in font.typeset(text):
mask.strokePath(path, transform, strokeWidth)

View file

@ -1,4 +1,4 @@
import common, random, pixie, strformat, unicode
import common, pixie, random, strformat, unicode
randomize()

View file

@ -11,14 +11,9 @@ block:
let font = readFont("tests/fonts/Roboto-Regular.ttf")
font.size = 72
let
image = newImage(200, 100)
layout = font.typeset("asdf")
let image = newImage(200, 100)
image.fill(rgba(255, 255, 255, 255))
for path in layout:
image.fillPath(path, rgba(0, 0, 0, 255))
image.fillText(font, "asdf", rgba(0, 0, 0, 255))
doDiff(image, "basic1")
@ -26,14 +21,9 @@ block:
let font = readFont("tests/fonts/Roboto-Regular.ttf")
font.size = 72
let
image = newImage(200, 100)
layout = font.typeset("A cow")
let image = newImage(200, 100)
image.fill(rgba(255, 255, 255, 255))
for path in layout:
image.fillPath(path, rgba(0, 0, 0, 255))
image.fillText(font, "A cow", rgba(0, 0, 0, 255))
doDiff(image, "basic2")
@ -41,14 +31,9 @@ block:
let font = readFont("tests/fonts/Roboto-Regular.ttf")
font.size = 24
let
image = newImage(200, 100)
layout = font.typeset("A bit of text HERE")
let image = newImage(200, 100)
image.fill(rgba(255, 255, 255, 255))
for path in layout:
image.fillPath(path, rgba(0, 0, 0, 255))
image.fillText(font, "A bit of text HERE", rgba(0, 0, 0, 255))
doDiff(image, "basic3")
@ -57,14 +42,9 @@ block:
font.size = 24
font.lineHeight = 100
let
image = newImage(200, 100)
layout = font.typeset("Line height")
let image = newImage(200, 100)
image.fill(rgba(255, 255, 255, 255))
for path in layout:
image.fillPath(path, rgba(0, 0, 0, 255))
image.fillText(font, "Line height", rgba(0, 0, 0, 255))
doDiff(image, "basic4")
@ -72,14 +52,9 @@ block:
let font = readFont("tests/fonts/Ubuntu-Regular.ttf")
font.size = 24
let
image = newImage(200, 100)
layout = font.typeset("Another font")
let image = newImage(200, 100)
image.fill(rgba(255, 255, 255, 255))
for path in layout:
image.fillPath(path, rgba(0, 0, 0, 255))
image.fillText(font, "Another font", rgba(0, 0, 0, 255))
doDiff(image, "basic5")
@ -87,14 +62,9 @@ block:
let font = readFont("tests/fonts/Aclonica-Regular.ttf")
font.size = 24
let
image = newImage(200, 100)
layout = font.typeset("Different font")
let image = newImage(200, 100)
image.fill(rgba(255, 255, 255, 255))
for path in layout:
image.fillPath(path, rgba(0, 0, 0, 255))
image.fillText(font, "Different font", rgba(0, 0, 0, 255))
doDiff(image, "basic6")
@ -102,18 +72,19 @@ block:
let font = readFont("tests/fonts/Roboto-Regular.ttf")
font.size = 24
let
image = newImage(200, 100)
layout1 = font.typeset("First line")
layout2 = font.typeset("Second line")
let image = newImage(200, 100)
image.fill(rgba(255, 255, 255, 255))
for path in layout1:
image.fillPath(path, rgba(0, 0, 0, 255))
for path in layout2:
image.fillPath(path, rgba(0, 0, 0, 255), vec2(0, font.defaultLineHeight))
image.fillText(
font,
"First line",
rgba(0, 0, 0, 255)
)
image.fillText(
font,
"Second line",
rgba(0, 0, 0, 255),
vec2(0, font.defaultLineHeight)
)
doDiff(image, "basic7")