Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
camelpunch committed Oct 14, 2024
1 parent 8f90c77 commit 448b4e3
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 70 deletions.
95 changes: 93 additions & 2 deletions lib/mudbrick/font.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ defmodule Mudbrick.Font do
:parsed
]

defmodule Unregistered do
defexception [:message]
end

alias Mudbrick.Document
alias Mudbrick.Font

def new(opts) do
case Keyword.fetch(opts, :parsed) do
{:ok, parsed} ->
Expand All @@ -28,8 +35,92 @@ defmodule Mudbrick.Font do

def type!(s), do: Map.fetch!(%{"Type0" => :Type0}, s)

defmodule Unregistered do
defexception [:message]
def add_objects(doc, fonts) do
{doc, font_objects, _id} =
for {human_name, font_opts} <- fonts, reduce: {doc, %{}, 0} do
{doc, font_objects, id} ->
font_opts =
Keyword.put(font_opts, :resource_identifier, :"F#{id + 1}")

{doc, font} =
case Keyword.pop(font_opts, :file) do
{nil, font_opts} ->
Document.add(doc, new(font_opts))

{file_contents, font_opts} ->
opentype =
OpenType.new()
|> OpenType.parse(file_contents)

font_name = String.to_atom(opentype.name)

doc
|> add_font_file(file_contents)
|> add_descriptor(opentype, font_name)
|> add_cid_font(opentype, font_name)
|> add_font(opentype, font_name, font_opts)
end

{doc, Map.put(font_objects, human_name, font), id + 1}
end

{doc, font_objects}
end

defp add_font_file(doc, contents) do
doc
|> Document.add(
Mudbrick.Stream.new(
data: contents,
additional_entries: %{
Length1: byte_size(contents),
Subtype: :OpenType
}
)
)
end

defp add_descriptor(doc, opentype, font_name) do
doc
|> Document.add(
&Font.Descriptor.new(
ascent: opentype.ascent,
cap_height: opentype.capHeight,
descent: opentype.descent,
file: &1,
flags: opentype.flags,
font_name: font_name,
bounding_box: opentype.bbox,
italic_angle: opentype.italicAngle,
stem_vertical: opentype.stemV
)
)
end

defp add_cid_font(doc, opentype, font_name) do
doc
|> Document.add(
&Font.CIDFont.new(
default_width: opentype.defaultWidth,
descriptor: &1,
type: :CIDFontType0,
font_name: font_name,
widths: opentype.glyphWidths
)
)
end

defp add_font(doc, opentype, font_name, font_opts) do
doc
|> Document.add(
&Font.new(
Keyword.merge(font_opts,
descendant: &1,
name: font_name,
parsed: opentype
)
)
)
end

defmodule CIDFont do
Expand Down
72 changes: 4 additions & 68 deletions lib/mudbrick/page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,75 +22,11 @@ defmodule Mudbrick.Page do
Document.add(doc, new_at_root(opts, doc))

{fonts, opts} ->
{doc, font_objects, _id} =
for {human_name, font_opts} <- fonts, reduce: {doc, %{}, 0} do
{doc, font_objects, id} ->
font_opts =
Keyword.put(font_opts, :resource_identifier, :"F#{id + 1}")

{doc, font} =
case Keyword.pop(font_opts, :file) do
{nil, font_opts} ->
Document.add(doc, Font.new(font_opts))

{file_contents, font_opts} ->
opentype =
OpenType.new()
|> OpenType.parse(file_contents)

font_name = String.to_atom(opentype.name)

doc
|> Document.add(
Mudbrick.Stream.new(
data: file_contents,
additional_entries: %{
Length1: byte_size(file_contents),
Subtype: :OpenType
}
)
)
|> Document.add(
&Font.Descriptor.new(
ascent: opentype.ascent,
cap_height: opentype.capHeight,
descent: opentype.descent,
file: &1,
flags: opentype.flags,
font_name: font_name,
bounding_box: opentype.bbox,
italic_angle: opentype.italicAngle,
stem_vertical: opentype.stemV
)
)
|> Document.add(
&Font.CIDFont.new(
default_width: opentype.defaultWidth,
descriptor: &1,
type: :CIDFontType0,
font_name: font_name,
widths: opentype.glyphWidths
)
)
|> Document.add(
&Font.new(
Keyword.merge(font_opts,
descendant: &1,
name: font_name,
parsed: opentype
)
)
)
end

{doc, Map.put(font_objects, human_name, font), id + 1}
end

doc
Font.add_objects(doc, fonts)
|> Document.add(
opts
|> Keyword.put(:fonts, font_objects)
|> new_at_root(doc)
&(opts
|> Keyword.put(:fonts, &1)
|> new_at_root(doc))
)
end
end
Expand Down

0 comments on commit 448b4e3

Please sign in to comment.