@@ -35,6 +35,16 @@ struct FontExtent{T}
35
35
scale:: Vec{2, T}
36
36
end
37
37
38
+ hadvance (ext:: FontExtent ) = ext. advance[1 ]
39
+ inkwidth (ext:: FontExtent ) = ext. scale[1 ]
40
+ inkheight (ext:: FontExtent ) = ext. scale[2 ]
41
+ hbearing_ori_to_left (ext:: FontExtent ) = ext. horizontal_bearing[1 ]
42
+ hbearing_ori_to_top (ext:: FontExtent ) = ext. horizontal_bearing[2 ]
43
+ leftinkbound (ext:: FontExtent ) = hbearing_ori_to_left (ext)
44
+ rightinkbound (ext:: FontExtent ) = leftinkbound (ext) + inkwidth (ext)
45
+ bottominkbound (ext:: FontExtent ) = hbearing_ori_to_top (ext) - inkheight (ext)
46
+ topinkbound (ext:: FontExtent ) = hbearing_ori_to_top (ext)
47
+
38
48
BroadcastStyle (:: Type{<: FontExtent} ) = Style {FontExtent} ()
39
49
BroadcastStyle (:: Style{FontExtent} , x) = Style {FontExtent} ()
40
50
BroadcastStyle (x, :: Style{FontExtent} ) = Style {FontExtent} ()
@@ -110,20 +120,18 @@ end
110
120
111
121
mutable struct FTFont
112
122
ft_ptr:: FreeType.FT_Face
113
- current_pixelsize:: Base.RefValue{Int}
114
123
use_cache:: Bool
115
- cache :: Dict{Tuple{Int, Char} , FontExtent{Float32}}
116
- function FTFont (ft_ptr:: FreeType.FT_Face , pixel_size :: Int = 64 , use_cache:: Bool = true )
117
- cache = Dict {Tuple{Int, Char}, FontExtent{Float32}} ()
118
- face = new (ft_ptr, Ref (pixel_size), use_cache, cache )
124
+ extent_cache :: Dict{Char, FontExtent{Float32}}
125
+ function FTFont (ft_ptr:: FreeType.FT_Face , use_cache:: Bool = true )
126
+ extent_cache = Dict {Tuple{Int, Char}, FontExtent{Float32}} ()
127
+ face = new (ft_ptr, use_cache, extent_cache )
119
128
finalizer (safe_free, face)
120
- FT_Set_Pixel_Sizes (face, pixel_size, 0 );
121
129
return face
122
130
end
123
131
end
124
132
125
133
use_cache (face:: FTFont ) = getfield (face, :use_cache )
126
- get_cache (face:: FTFont ) = getfield (face, :cache )
134
+ get_cache (face:: FTFont ) = getfield (face, :extent_cache )
127
135
128
136
function FTFont (path:: String )
129
137
return FTFont (newface (path))
159
167
# Allow broadcasting over fonts
160
168
Base. Broadcast. broadcastable (ft:: FTFont ) = Ref (ft)
161
169
162
- get_pixelsize (face:: FTFont ) = getfield (face, :current_pixelsize )[]
163
-
164
170
function set_pixelsize (face:: FTFont , size:: Integer )
165
- get_pixelsize (face) == size && return size
166
171
err = FT_Set_Pixel_Sizes (face, size, size)
167
172
check_error (err, " Couldn't set pixelsize" )
168
- getfield (face, :current_pixelsize )[] = size
169
173
return size
170
174
end
171
175
@@ -182,13 +186,13 @@ function kerning(c1::Char, c2::Char, face::FTFont)
182
186
end
183
187
184
188
function loadchar (face:: FTFont , c:: Char )
185
- err = FT_Load_Char (face, c, FT_LOAD_RENDER)
189
+ err = FT_Load_Char (face, c, FT_LOAD_RENDER | FT_LOAD_NO_HINTING )
186
190
check_error (err, " Could not load char to render." )
187
191
end
188
192
189
193
function get_extent (face:: FTFont , char:: Char )
190
194
if use_cache (face)
191
- get! (get_cache (face), ( get_pixelsize (face), char) ) do
195
+ get! (get_cache (face), char) do
192
196
return internal_get_extent (face, char)
193
197
end
194
198
else
@@ -197,9 +201,11 @@ function get_extent(face::FTFont, char::Char)
197
201
end
198
202
199
203
function internal_get_extent (face:: FTFont , char:: Char )
200
- err = FT_Load_Char (face, char, FT_LOAD_DEFAULT )
204
+ err = FT_Load_Char (face, char, FT_LOAD_NO_SCALE )
201
205
check_error (err, " Could not load char to get extend." )
202
- metrics = face. glyph. metrics
203
- # 64 since metrics are in 1/64 units (units to 26.6 fractional pixels)
204
- return FontExtent (metrics, Float32 (64 ))
206
+ # This gives us the font metrics in normalized units (-1, 1)
207
+ return FontExtent (face. glyph. metrics, Float32 (face. units_per_EM))
205
208
end
209
+
210
+ descender (font) = font. descender / font. units_per_EM
211
+ ascender (font) = font. ascender / font. units_per_EM
0 commit comments