Skip to content

Commit f535ae0

Browse files
committed
Resolve fonts in line parts
1 parent 1c7bfe7 commit f535ae0

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

lib/mudbrick.ex

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -197,22 +197,13 @@ defmodule Mudbrick do
197197
def text(context, write_or_writes, opts \\ [])
198198

199199
def text({doc, _contents_obj} = context, writes, opts) when is_list(writes) do
200-
opts =
201-
Keyword.update(opts, :font, nil, fn user_identifier ->
202-
case Map.fetch(Document.root_page_tree(doc).value.fonts, user_identifier) do
203-
{:ok, font} ->
204-
font.value
205-
206-
:error ->
207-
raise Font.Unregistered, "Unregistered font: #{user_identifier}"
208-
end
209-
end)
200+
opts = fetch_font(doc, opts)
210201

211202
text_block =
212203
writes
213204
|> Enum.reduce(Mudbrick.TextBlock.new(opts), fn
214205
{text, opts}, acc ->
215-
Mudbrick.TextBlock.write(acc, text, opts)
206+
Mudbrick.TextBlock.write(acc, text, fetch_font(doc, opts))
216207

217208
text, acc ->
218209
Mudbrick.TextBlock.write(acc, text, [])
@@ -228,6 +219,18 @@ defmodule Mudbrick do
228219
text(context, [write], opts)
229220
end
230221

222+
defp fetch_font(doc, opts) do
223+
Keyword.update(opts, :font, nil, fn user_identifier ->
224+
case Map.fetch(Document.root_page_tree(doc).value.fonts, user_identifier) do
225+
{:ok, font} ->
226+
font.value
227+
228+
:error ->
229+
raise Font.Unregistered, "Unregistered font: #{user_identifier}"
230+
end
231+
end)
232+
end
233+
231234
@doc """
232235
Produce `iodata` from the current document.
233236
"""

test/mudbrick_test.exs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ defmodule MudbrickTest do
8080
}
8181
)
8282
|> page(size: Page.size(:letter))
83-
|> text("hello, world!",
83+
|> text([{"hello, ", font: :courier}, "world!"],
8484
position: {300, 400},
8585
font: :helvetica,
8686
font_size: 100
@@ -168,15 +168,18 @@ defmodule MudbrickTest do
168168
>>
169169
endobj
170170
7 0 obj
171-
<</Length 124
171+
<</Length 152
172172
>>
173173
stream
174174
BT
175175
/F2 100 Tf
176176
120.0 TL
177177
300 400 Td
178178
0 0 0 rg
179-
(hello, world!) Tj
179+
/F1 100 Tf
180+
(hello, ) Tj
181+
/F2 100 Tf
182+
(world!) Tj
180183
ET
181184
BT
182185
/F1 10 Tf
@@ -210,14 +213,14 @@ defmodule MudbrickTest do
210213
0000001491 00000 n
211214
0000001559 00000 n
212215
0000001653 00000 n
213-
0000001827 00000 n
214-
0000001923 00000 n
216+
0000001855 00000 n
217+
0000001951 00000 n
215218
trailer
216219
<</Root 5 0 R
217220
/Size 10
218221
>>
219222
startxref
220-
1970
223+
1998
221224
%%EOF\
222225
"""
223226
end

0 commit comments

Comments
 (0)