Skip to content

Commit

Permalink
Leading is customisable
Browse files Browse the repository at this point in the history
  • Loading branch information
camelpunch committed Oct 20, 2024
1 parent 09abbc5 commit db6d853
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
4 changes: 3 additions & 1 deletion lib/mudbrick.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ defmodule Mudbrick do
def font({doc, _content_stream_obj} = context, user_identifier, opts) do
import ContentStream

{leading, opts} = Keyword.pop(opts, :leading, Keyword.fetch!(opts, :size) * 1.2)

case Map.fetch(Document.root_page_tree(doc).value.fonts, user_identifier) do
{:ok, font} ->
context
Expand All @@ -42,7 +44,7 @@ defmodule Mudbrick do
font.value
)
)
|> add(ContentStream.TL, leading: Keyword.fetch!(opts, :size) * 1.2)
|> add(ContentStream.TL, leading: leading)

:error ->
raise Font.Unregistered, "Unregistered font: #{user_identifier}"
Expand Down
2 changes: 1 addition & 1 deletion test/font_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ defmodule Mudbrick.FontTest do

e =
assert_raise Font.Unregistered, fn ->
chain |> font(:bodoni, 24)
chain |> font(:bodoni, size: 24)
end

assert e.message == "Unregistered font: bodoni"
Expand Down
11 changes: 6 additions & 5 deletions test/mudbrick_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ defmodule MudbrickTest do
}
)
|> page(size: :letter)
|> image(
:flower,
scale: Mudbrick.Page.size(:letter),
position: {0, 0}
)
|> text_position(200, 700)
|> font(:bodoni, size: 14)
|> colour({1, 0, 0})
Expand All @@ -20,15 +25,11 @@ defmodule MudbrickTest do
is Carbon Dioxide
and HNO₃ is Nitric Acid
""")
|> image(
:flower,
scale: Mudbrick.Page.size(:letter),
position: {0, 0}
)
|> text("wide stuff", align: :right)
|> text("wider stuff", align: :right)
|> text("z", align: :right)
|> text_position(400, 600)
|> font(:bodoni, size: 14, leading: 14)
|> text("""
I am left again
Expand Down
21 changes: 21 additions & 0 deletions test/text_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ defmodule Mudbrick.TextTest do
alias Mudbrick.Font
alias Mudbrick.Indirect

test "can set leading" do
assert [
"/F1 10 Tf",
"14 TL",
"(black and ) Tj"
] =
new(
fonts: %{
helvetica: [
name: :Helvetica,
type: :TrueType,
encoding: :PDFDocEncoding
]
}
)
|> page()
|> font(:helvetica, size: 10, leading: 14)
|> text("black and ")
|> operations()
end

test "can set colour on a piece of text" do
{_doc, content_stream} =
new(
Expand Down

0 comments on commit db6d853

Please sign in to comment.