Skip to content

Commit 4ab2a16

Browse files
committed
Refactor
1 parent fadfa36 commit 4ab2a16

File tree

1 file changed

+18
-32
lines changed

1 file changed

+18
-32
lines changed

lib/mudbrick.ex

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ defmodule Mudbrick do
6666
ContentStream.add(context, ContentStream.Td, tx: x, ty: y)
6767
end
6868

69-
defp latest_font_operation(content_stream) do
69+
defp latest_font_operation!(content_stream) do
7070
Enum.find(
7171
content_stream.value.operations,
7272
&match?(%ContentStream.Tf{}, &1)
73-
)
73+
) || raise Font.NotSet, "No font chosen"
7474
end
7575

7676
def text({_doc, content_stream} = context, text, colour: {r, g, b}) do
@@ -79,39 +79,25 @@ defmodule Mudbrick do
7979
|> ContentStream.add(ContentStream.Rg, r: r, g: g, b: b)
8080
|> text(text)
8181
|> ContentStream.add(ContentStream.QPop, [])
82-
|> ContentStream.add(latest_font_operation(content_stream))
82+
|> ContentStream.add(latest_font_operation!(content_stream))
8383
end
8484

8585
def text({_doc, content_stream} = context, text) do
86-
case latest_font_operation(content_stream) do
87-
%ContentStream.Tf{size: font_size, font: font} ->
88-
[first_part | parts] = String.split(text, "\n")
89-
90-
context
91-
|> ContentStream.add(
92-
ContentStream.TL,
93-
leading: font_size * 1.2
94-
)
95-
|> ContentStream.add(
96-
ContentStream.Tj,
97-
font: font,
98-
text: first_part
99-
)
100-
|> then(fn context ->
101-
for part <- parts, reduce: context do
102-
acc ->
103-
ContentStream.add(
104-
acc,
105-
ContentStream.Apostrophe,
106-
font: font,
107-
text: part
108-
)
109-
end
110-
end)
111-
112-
nil ->
113-
raise Font.NotSet, "No font chosen"
114-
end
86+
import ContentStream
87+
88+
tf = latest_font_operation!(content_stream)
89+
90+
[first_part | parts] = String.split(text, "\n")
91+
92+
context
93+
|> add(ContentStream.TL, leading: tf.size * 1.2)
94+
|> add(ContentStream.Tj, font: tf.font, text: first_part)
95+
|> then(fn context ->
96+
for part <- parts, reduce: context do
97+
acc ->
98+
add(acc, ContentStream.Apostrophe, font: tf.font, text: part)
99+
end
100+
end)
115101
end
116102

117103
def render({doc, _page}) do

0 commit comments

Comments
 (0)