11defmodule Mudbrick.Font do
2- @ moduledoc false
3-
42 @ type t :: % __MODULE__ {
53 descendant: Mudbrick.Indirect.Object . t ( ) ,
64 encoding: atom ( ) ,
@@ -41,6 +39,7 @@ defmodule Mudbrick.Font do
4139 alias Mudbrick.Object
4240 alias Mudbrick.Stream
4341
42+ @ doc false
4443 def new ( opts ) do
4544 case Keyword . fetch ( opts , :parsed ) do
4645 { :ok , parsed } ->
@@ -52,8 +51,10 @@ defmodule Mudbrick.Font do
5251 end
5352 end
5453
54+ @ doc false
5555 def type! ( s ) , do: Map . fetch! ( % { "Type0" => :Type0 } , s )
5656
57+ @ doc false
5758 def add_objects ( doc , fonts ) do
5859 { doc , font_objects , _id } =
5960 for { human_name , font_opts } <- fonts , reduce: { doc , % { } , 0 } do
@@ -86,6 +87,27 @@ defmodule Mudbrick.Font do
8687 { doc , font_objects }
8788 end
8889
90+ @ doc false
91+ def width ( % Font { parsed: nil } , _size , _text ) do
92+ raise Font.NotMeasured , "Built-in fonts aren't alignable yet"
93+ end
94+
95+ def width ( _font , _size , "" ) do
96+ 0
97+ end
98+
99+ def width ( font , size , text ) do
100+ { glyph_ids , _positions } = OpenType . layout_text ( font . parsed , text )
101+
102+ for id <- glyph_ids , reduce: 0 do
103+ acc ->
104+ glyph_width = Enum . at ( font . parsed . glyphWidths , id )
105+ width_in_points = glyph_width / 1000 * size
106+
107+ acc + width_in_points
108+ end
109+ end
110+
89111 defp add_font_file ( doc , contents ) do
90112 doc
91113 |> Document . add (
@@ -146,26 +168,6 @@ defmodule Mudbrick.Font do
146168 end )
147169 end
148170
149- def width ( % Font { parsed: nil } , _size , _text ) do
150- raise Font.NotMeasured , "Built-in fonts aren't alignable yet"
151- end
152-
153- def width ( _font , _size , "" ) do
154- 0
155- end
156-
157- def width ( font , size , text ) do
158- { glyph_ids , _positions } = OpenType . layout_text ( font . parsed , text )
159-
160- for id <- glyph_ids , reduce: 0 do
161- acc ->
162- glyph_width = Enum . at ( font . parsed . glyphWidths , id )
163- width_in_points = glyph_width / 1000 * size
164-
165- acc + width_in_points
166- end
167- end
168-
169171 defimpl Mudbrick.Object do
170172 def from ( font ) do
171173 Object . from (
0 commit comments