Skip to content

Commit

Permalink
Raise exception when font not set
Browse files Browse the repository at this point in the history
  • Loading branch information
camelpunch committed Oct 14, 2024
1 parent 531c3d9 commit 8d4f373
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
51 changes: 28 additions & 23 deletions lib/mudbrick.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule Mudbrick do
alias Mudbrick.ContentStream
alias Mudbrick.Document
alias Mudbrick.Font
alias Mudbrick.Page

@dpi 72
Expand Down Expand Up @@ -57,7 +58,7 @@ defmodule Mudbrick do
)

:error ->
raise Mudbrick.Font.Unregistered, "Unregistered font: #{user_identifier}"
raise Font.Unregistered, "Unregistered font: #{user_identifier}"
end
end

Expand All @@ -72,29 +73,33 @@ defmodule Mudbrick do
&match?(%ContentStream.Tf{}, &1)
)

[first_part | parts] = String.split(text, "\n")
if latest_font_setting == nil do
raise Font.NotSet, "No font chosen"
else
[first_part | parts] = String.split(text, "\n")

context
|> ContentStream.add(
ContentStream.TL,
leading: latest_font_setting.size * 1.2
)
|> ContentStream.add(
ContentStream.Tj,
font: latest_font_setting.font,
text: first_part
)
|> then(fn context ->
for part <- parts, reduce: context do
acc ->
ContentStream.add(
acc,
ContentStream.Apostrophe,
font: latest_font_setting.font,
text: part
)
end
end)
context
|> ContentStream.add(
ContentStream.TL,
leading: latest_font_setting.size * 1.2
)
|> ContentStream.add(
ContentStream.Tj,
font: latest_font_setting.font,
text: first_part
)
|> then(fn context ->
for part <- parts, reduce: context do
acc ->
ContentStream.add(
acc,
ContentStream.Apostrophe,
font: latest_font_setting.font,
text: part
)
end
end)
end
end

def render({doc, _page}) do
Expand Down
4 changes: 4 additions & 0 deletions lib/mudbrick/font.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ defmodule Mudbrick.Font do
:parsed
]

defmodule NotSet do
defexception [:message]
end

defmodule Unregistered do
defexception [:message]
end
Expand Down
24 changes: 24 additions & 0 deletions test/mudbrick/font_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,30 @@ defmodule Mudbrick.FontTest do
assert Document.object_with_ref(doc, file.ref)
end

test "forgetting to set the font is an error" do
import Mudbrick

chain =
new()
|> page(
fonts: %{
helvetica: [
name: :Helvetica,
type: :TrueType,
encoding: :PDFDocEncoding
]
}
)
|> contents()

e =
assert_raise Font.NotSet, fn ->
chain |> text("hi there")
end

assert e.message == "No font chosen"
end

test "asking for an unregistered font is an error" do
import Mudbrick

Expand Down

0 comments on commit 8d4f373

Please sign in to comment.