1
1
defmodule Mudbrick.Font do
2
- @ moduledoc false
3
-
4
2
@ type t :: % __MODULE__ {
5
3
descendant: Mudbrick.Indirect.Object . t ( ) ,
6
4
encoding: atom ( ) ,
@@ -41,6 +39,7 @@ defmodule Mudbrick.Font do
41
39
alias Mudbrick.Object
42
40
alias Mudbrick.Stream
43
41
42
+ @ doc false
44
43
def new ( opts ) do
45
44
case Keyword . fetch ( opts , :parsed ) do
46
45
{ :ok , parsed } ->
@@ -52,8 +51,10 @@ defmodule Mudbrick.Font do
52
51
end
53
52
end
54
53
54
+ @ doc false
55
55
def type! ( s ) , do: Map . fetch! ( % { "Type0" => :Type0 } , s )
56
56
57
+ @ doc false
57
58
def add_objects ( doc , fonts ) do
58
59
{ doc , font_objects , _id } =
59
60
for { human_name , font_opts } <- fonts , reduce: { doc , % { } , 0 } do
@@ -86,6 +87,27 @@ defmodule Mudbrick.Font do
86
87
{ doc , font_objects }
87
88
end
88
89
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
+
89
111
defp add_font_file ( doc , contents ) do
90
112
doc
91
113
|> Document . add (
@@ -146,26 +168,6 @@ defmodule Mudbrick.Font do
146
168
end )
147
169
end
148
170
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
-
169
171
defimpl Mudbrick.Object do
170
172
def from ( font ) do
171
173
Object . from (
0 commit comments