Skip to content

Commit ed8cb8c

Browse files
committed
Start higher-order function alignment
1 parent 82d57c9 commit ed8cb8c

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

lib/mudbrick/content_stream.ex

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -128,22 +128,22 @@ defmodule Mudbrick.ContentStream do
128128

129129
text ->
130130
context
131-
|> align(text, old_alignment, new_alignment)
132-
|> add(Tj, font: tf.font, text: text)
133-
|> negate_right_alignment()
131+
|> align(text, old_alignment, new_alignment, fn ->
132+
%Tj{font: tf.font, text: text}
133+
end)
134134
end
135135
|> then(fn context ->
136136
for part <- parts, reduce: context do
137137
acc ->
138138
acc
139-
|> align(part, new_alignment)
140-
|> add(Apostrophe, font: tf.font, text: part)
141-
|> negate_right_alignment()
139+
|> align(part, new_alignment, fn ->
140+
%Apostrophe{font: tf.font, text: part}
141+
end)
142142
end
143143
end)
144144
end
145145

146-
defp align({_doc, content_stream} = context, text, old, new) do
146+
defp align({_doc, content_stream} = context, text, old, new, f) do
147147
case {old, new} do
148148
{_, :left} ->
149149
context
@@ -172,28 +172,32 @@ defmodule Mudbrick.ContentStream do
172172
|> add(Td, tx: -width, ty: 0, purpose: :align_right)
173173
|> put(current_alignment: :right)
174174
end
175+
|> add(f.())
176+
|> negate_right_alignment()
175177
end
176178

177-
defp align({_doc, content_stream} = context, text, new) do
178-
case new do
179-
:left ->
180-
context
181-
|> put(current_alignment: :left)
179+
defp align(context, _text, :left, f) do
180+
context
181+
|> put(current_alignment: :left)
182+
|> add(f.())
183+
|> negate_right_alignment()
184+
end
182185

183-
:right ->
184-
tf = Tf.latest!(content_stream)
186+
defp align({_doc, content_stream} = context, text, :right, f) do
187+
tf = Tf.latest!(content_stream)
185188

186-
case Font.width(tf.font, tf.size, text) do
187-
0 ->
188-
context
189-
|> put(current_alignment: :right)
189+
case Font.width(tf.font, tf.size, text) do
190+
0 ->
191+
context
192+
|> put(current_alignment: :right)
190193

191-
width ->
192-
context
193-
|> add(Td, tx: -width, ty: 0, purpose: :align_right)
194-
|> put(current_alignment: :right)
195-
end
194+
width ->
195+
context
196+
|> add(Td, tx: -width, ty: 0, purpose: :align_right)
197+
|> put(current_alignment: :right)
196198
end
199+
|> add(f.())
200+
|> negate_right_alignment()
197201
end
198202

199203
defp negate_right_alignment({_doc, cs} = context) do

0 commit comments

Comments
 (0)