diff --git a/README.md b/README.md index ce3a080..3b555b6 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,50 @@ z | ✅ | close path | ## Examples +### Text +[examples/text.nim](examples/text.nim) +```nim +var font = readFont("tests/fonts/Roboto-Regular_1.ttf") +font.size = 20 + +let text = "Typesetting is the arrangement and composition of text in graphic design and publishing in both digital and traditional medias." + +image.fillText(font.typeset(text, bounds = vec2(180, 180)), vec2(10, 10)) +``` +![example output](examples/text.png) + +### Text spans +[examples/text_spans.nim](examples/text_spans.nim) +```nim +let font = readFont("tests/fonts/Ubuntu-Regular_1.ttf") + +var style1 = font +style1.size = 12 +style1.paint.color = rgba(200, 200, 200, 255) + +var style2 = font +style2.size = 36 +style2.paint.color = rgba(0, 0, 0, 255) + +var style3 = font +style3.size = 13 +style3.paint.color = rgba(0, 127, 244, 255) + +var style4 = font +style4.size = 14 +style4.paint.color = rgba(80, 80, 80, 255) + +let spans = @[ + newSpan("verb [with object] ", style1), + newSpan("strallow\n", style2), + newSpan("\nstral·low\n", style3), + newSpan("\n1. free (something) from restrictive restrictions \"the regulations are intended to strallow changes in public policy\" ", style4) +] + +image.fillText(typeset(spans, bounds = vec2(180, 180)), vec2(10, 10)) +``` +![example output](examples/text_spans.png) + ### Square [examples/square.nim](examples/square.nim) ```nim diff --git a/examples/text.nim b/examples/text.nim new file mode 100644 index 0000000..0048b79 --- /dev/null +++ b/examples/text.nim @@ -0,0 +1,12 @@ +import pixie + +let image = newImage(200, 200) +image.fill(rgba(255, 255, 255, 255)) + +var font = readFont("tests/fonts/Roboto-Regular_1.ttf") +font.size = 20 + +let text = "Typesetting is the arrangement and composition of text in graphic design and publishing in both digital and traditional medias." + +image.fillText(font.typeset(text, bounds = vec2(180, 180)), vec2(10, 10)) +image.writeFile("examples/text.png") diff --git a/examples/text.png b/examples/text.png new file mode 100644 index 0000000..7483842 Binary files /dev/null and b/examples/text.png differ diff --git a/examples/text_spans.nim b/examples/text_spans.nim new file mode 100644 index 0000000..eae062f --- /dev/null +++ b/examples/text_spans.nim @@ -0,0 +1,32 @@ +import pixie + +let image = newImage(200, 200) +image.fill(rgba(255, 255, 255, 255)) + +let font = readFont("tests/fonts/Ubuntu-Regular_1.ttf") + +var style1 = font +style1.size = 12 +style1.paint.color = rgba(200, 200, 200, 255) + +var style2 = font +style2.size = 36 +style2.paint.color = rgba(0, 0, 0, 255) + +var style3 = font +style3.size = 13 +style3.paint.color = rgba(0, 127, 244, 255) + +var style4 = font +style4.size = 14 +style4.paint.color = rgba(80, 80, 80, 255) + +let spans = @[ + newSpan("verb [with object] ", style1), + newSpan("strallow\n", style2), + newSpan("\nstral·low\n", style3), + newSpan("\n1. free (something) from restrictive restrictions \"the regulations are intended to strallow changes in public policy\" ", style4) +] + +image.fillText(typeset(spans, bounds = vec2(180, 180)), vec2(10, 10)) +image.writeFile("examples/text_spans.png") diff --git a/examples/text_spans.png b/examples/text_spans.png new file mode 100644 index 0000000..41bf361 Binary files /dev/null and b/examples/text_spans.png differ diff --git a/tools/gen_readme.nim b/tools/gen_readme.nim index a8a5c64..57360cd 100644 --- a/tools/gen_readme.nim +++ b/tools/gen_readme.nim @@ -11,6 +11,8 @@ proc cutBetween(str, a, b: string): string = var md: seq[string] var exampleFiles = [ + "examples/text.nim", + "examples/text_spans.nim", "examples/square.nim", "examples/line.nim", "examples/rounded_rectangle.nim",