From f535ae02a3b4b62a84f8e74710e5d2f61ba9010e Mon Sep 17 00:00:00 2001 From: Andrew Bruce Date: Sun, 27 Oct 2024 23:08:03 +0000 Subject: [PATCH] Resolve fonts in line parts --- lib/mudbrick.ex | 25 ++++++++++++++----------- test/mudbrick_test.exs | 15 +++++++++------ 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/lib/mudbrick.ex b/lib/mudbrick.ex index f70afcb..3bb8cb6 100644 --- a/lib/mudbrick.ex +++ b/lib/mudbrick.ex @@ -197,22 +197,13 @@ defmodule Mudbrick do def text(context, write_or_writes, opts \\ []) def text({doc, _contents_obj} = context, writes, opts) when is_list(writes) do - opts = - Keyword.update(opts, :font, nil, fn user_identifier -> - case Map.fetch(Document.root_page_tree(doc).value.fonts, user_identifier) do - {:ok, font} -> - font.value - - :error -> - raise Font.Unregistered, "Unregistered font: #{user_identifier}" - end - end) + opts = fetch_font(doc, opts) text_block = writes |> Enum.reduce(Mudbrick.TextBlock.new(opts), fn {text, opts}, acc -> - Mudbrick.TextBlock.write(acc, text, opts) + Mudbrick.TextBlock.write(acc, text, fetch_font(doc, opts)) text, acc -> Mudbrick.TextBlock.write(acc, text, []) @@ -228,6 +219,18 @@ defmodule Mudbrick do text(context, [write], opts) end + defp fetch_font(doc, opts) do + Keyword.update(opts, :font, nil, fn user_identifier -> + case Map.fetch(Document.root_page_tree(doc).value.fonts, user_identifier) do + {:ok, font} -> + font.value + + :error -> + raise Font.Unregistered, "Unregistered font: #{user_identifier}" + end + end) + end + @doc """ Produce `iodata` from the current document. """ diff --git a/test/mudbrick_test.exs b/test/mudbrick_test.exs index 2a8b2e6..2bd820d 100644 --- a/test/mudbrick_test.exs +++ b/test/mudbrick_test.exs @@ -80,7 +80,7 @@ defmodule MudbrickTest do } ) |> page(size: Page.size(:letter)) - |> text("hello, world!", + |> text([{"hello, ", font: :courier}, "world!"], position: {300, 400}, font: :helvetica, font_size: 100 @@ -168,7 +168,7 @@ defmodule MudbrickTest do >> endobj 7 0 obj - <> stream BT @@ -176,7 +176,10 @@ defmodule MudbrickTest do 120.0 TL 300 400 Td 0 0 0 rg - (hello, world!) Tj + /F1 100 Tf + (hello, ) Tj + /F2 100 Tf + (world!) Tj ET BT /F1 10 Tf @@ -210,14 +213,14 @@ defmodule MudbrickTest do 0000001491 00000 n 0000001559 00000 n 0000001653 00000 n - 0000001827 00000 n - 0000001923 00000 n + 0000001855 00000 n + 0000001951 00000 n trailer <> startxref - 1970 + 1998 %%EOF\ """ end