Skip to content

Commit 5c63bfa

Browse files
committed
Centre-alignment
1 parent f745d52 commit 5c63bfa

File tree

3 files changed

+56
-18
lines changed

3 files changed

+56
-18
lines changed

lib/mudbrick.ex

+32-17
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ defmodule Mudbrick do
215215
- `:position` - Coordinates from bottom-left of page in points. Default: `{0, 0}`.
216216
- `:font_size` - Size in points. Default: `12`.
217217
- `:leading` - Leading in points. Default is 120% of `:font_size`.
218-
- `:align` - Either `:left` or `:right`. Default: `:left`. Note that the rightmost point of right-aligned text is the horizontal offset provided to `:position`.
218+
- `:align` - `:left`, `:right` or `:centre`. Default: `:left`.
219+
Note that the rightmost point of right-aligned text is the horizontal offset provided to `:position`.
220+
The same position defines the centre point of centre-aligned text.
219221
220222
## Individual write options
221223
@@ -253,43 +255,56 @@ defmodule Mudbrick do
253255
...> |> page()
254256
...> |> text(["I am ", {"bold", font: :bold}], font: :regular, position: {200, 200})
255257
256-
Underlined text.
258+
[Underlined text](examples/underlined_text.pdf?#navpanes=0).
257259
258260
iex> import Mudbrick
259261
...> new(fonts: %{bodoni: Mudbrick.TestHelper.bodoni_regular()})
260262
...> |> page(size: {100, 50})
261263
...> |> text([
262-
...> {"heading\\n", leading: 20},
263-
...> "nounderline\\n",
264-
...> "now ",
265-
...> {"underline", underline: [width: 1]},
266-
...> " that"
264+
...> {"the\\n", leading: 20},
265+
...> "quick\\n",
266+
...> "brown fox ",
267+
...> {"jumps", underline: [width: 1]},
268+
...> " over"
267269
...> ], position: {8, 40}, font_size: 8)
268270
...> |> render()
269271
...> |> then(&File.write("examples/underlined_text.pdf", &1))
270272
271-
Produces [this PDF](examples/underlined_text.pdf?#navpanes=0).
272-
273273
<object width="400" height="130" data="examples/underlined_text.pdf?#navpanes=0" type="application/pdf"></object>
274274
275-
Underlined, right-aligned text.
275+
[Underlined, right-aligned text](examples/underlined_text_right_align.pdf?#navpanes=0).
276276
277277
iex> import Mudbrick
278278
...> new(fonts: %{bodoni: Mudbrick.TestHelper.bodoni_regular()})
279279
...> |> page(size: {100, 50})
280280
...> |> text([
281-
...> {"heading\\n", leading: 20},
282-
...> "nounderline\\n",
283-
...> "now ",
284-
...> {"underline", underline: [width: 1]},
285-
...> " that"
281+
...> {"the\\n", leading: 20},
282+
...> "quick\\n",
283+
...> "brown fox ",
284+
...> {"jumps", underline: [width: 1]},
285+
...> " over"
286286
...> ], position: {90, 40}, font_size: 8, align: :right)
287287
...> |> render()
288288
...> |> then(&File.write("examples/underlined_text_right_align.pdf", &1))
289289
290-
Produces [this PDF](examples/underlined_text_right_align.pdf?#navpanes=0).
291-
292290
<object width="400" height="130" data="examples/underlined_text_right_align.pdf?#navpanes=0" type="application/pdf"></object>
291+
292+
[Underlined, centre-aligned text](examples/underlined_text_centre_align.pdf?#navpanes=0).
293+
294+
iex> import Mudbrick
295+
...> new(fonts: %{bodoni: Mudbrick.TestHelper.bodoni_regular()})
296+
...> |> page(size: {100, 50})
297+
...> |> text([
298+
...> {"the\\n", leading: 20},
299+
...> "quick\\n",
300+
...> "brown fox ",
301+
...> {"jumps", underline: [width: 1]},
302+
...> " over"
303+
...> ], position: {50, 40}, font_size: 8, align: :centre)
304+
...> |> render()
305+
...> |> then(&File.write("examples/underlined_text_centre_align.pdf", &1))
306+
307+
<object width="400" height="130" data="examples/underlined_text_centre_align.pdf?#navpanes=0" type="application/pdf"></object>
293308
"""
294309

295310
@spec text(context(), Mudbrick.TextBlock.write(), Mudbrick.TextBlock.options()) :: context()

lib/mudbrick/text_block/output.ex

+5-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ defmodule Mudbrick.TextBlock.Output do
3030
|> end_block()
3131
|> reduce_lines(
3232
tb.lines,
33-
if(tb.align == :left, do: fn _ -> 0 end, else: &Line.width/1)
33+
case tb.align do
34+
:left -> fn _ -> 0 end
35+
:right -> &Line.width/1
36+
:centre -> fn line -> Line.width(line) / 2 end
37+
end
3438
)
3539
|> td(position)
3640
|> add(tl)

test/mudbrick/text_block_test.exs

+19
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,25 @@ defmodule Mudbrick.TextBlockTest do
520520
end
521521
end
522522

523+
describe "centre-aligned" do
524+
test "offset of a line is half the right-aligned offset" do
525+
text = "aaaa"
526+
size = 12
527+
right_td = first_td(:right, text, size)
528+
centre_td = first_td(:centre, text, size)
529+
530+
assert centre_td.tx == right_td.tx / 2
531+
end
532+
end
533+
534+
defp first_td(align, text, size) do
535+
output(fn %{fonts: fonts} ->
536+
TextBlock.new(align: align, font: fonts.regular, font_size: size)
537+
|> TextBlock.write(text)
538+
end)
539+
|> Enum.find(&match?(%Mudbrick.ContentStream.Td{}, &1))
540+
end
541+
523542
defp output(f) do
524543
Mudbrick.TestHelper.wrapped_output(f, TextBlock.Output) |> Enum.reverse()
525544
end

0 commit comments

Comments
 (0)