Skip to content

Commit

Permalink
Resolve fonts in line parts
Browse files Browse the repository at this point in the history
  • Loading branch information
camelpunch committed Oct 27, 2024
1 parent 1c7bfe7 commit f535ae0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
25 changes: 14 additions & 11 deletions lib/mudbrick.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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, [])
Expand All @@ -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.
"""
Expand Down
15 changes: 9 additions & 6 deletions test/mudbrick_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -168,15 +168,18 @@ defmodule MudbrickTest do
>>
endobj
7 0 obj
<</Length 124
<</Length 152
>>
stream
BT
/F2 100 Tf
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
Expand Down Expand Up @@ -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
<</Root 5 0 R
/Size 10
>>
startxref
1970
1998
%%EOF\
"""
end
Expand Down

0 comments on commit f535ae0

Please sign in to comment.