1
1
use super :: { FontData , FontFamily , FontStyle , LayoutBox } ;
2
+ use :: ab_glyph:: { Font , FontRef , ScaleFont } ;
2
3
use :: core:: fmt:: { self , Display } ;
3
- use :: std :: error :: Error ;
4
+ use :: once_cell :: sync :: Lazy ;
4
5
use :: std:: collections:: HashMap ;
6
+ use :: std:: error:: Error ;
5
7
use :: std:: sync:: RwLock ;
6
- use :: once_cell:: sync:: Lazy ;
7
- use :: ab_glyph:: { FontRef , Font , ScaleFont } ;
8
8
9
- static FONTS : Lazy < RwLock < HashMap < String , FontRef < ' static > > > > = Lazy :: new ( || RwLock :: new ( HashMap :: new ( ) ) ) ;
9
+ static FONTS : Lazy < RwLock < HashMap < String , FontRef < ' static > > > > =
10
+ Lazy :: new ( || RwLock :: new ( HashMap :: new ( ) ) ) ;
10
11
pub struct InvalidFont {
11
12
_priv : ( ) ,
12
13
}
@@ -21,7 +22,7 @@ pub fn register_font(name: &str, bytes: &'static [u8]) -> Result<(), InvalidFont
21
22
22
23
#[ derive( Clone ) ]
23
24
pub struct FontDataInternal {
24
- font_ref : FontRef < ' static >
25
+ font_ref : FontRef < ' static > ,
25
26
}
26
27
27
28
#[ derive( Debug , Clone ) ]
@@ -45,7 +46,12 @@ impl FontData for FontDataInternal {
45
46
type ErrorType = FontError ;
46
47
fn new ( family : FontFamily < ' _ > , style : FontStyle ) -> Result < Self , Self :: ErrorType > {
47
48
Ok ( Self {
48
- font_ref : FONTS . read ( ) . unwrap ( ) . get ( family. as_str ( ) ) . ok_or ( FontError :: FontUnavailable ) ?. clone ( )
49
+ font_ref : FONTS
50
+ . read ( )
51
+ . unwrap ( )
52
+ . get ( family. as_str ( ) )
53
+ . ok_or ( FontError :: FontUnavailable ) ?
54
+ . clone ( ) ,
49
55
} )
50
56
}
51
57
// TODO: ngl, it makes no sense that this uses the same error type as `new`
@@ -77,8 +83,7 @@ impl FontData for FontDataInternal {
77
83
mut draw : DrawFunc ,
78
84
) -> Result < Result < ( ) , E > , Self :: ErrorType > {
79
85
let font = self . font_ref . as_scaled ( size as f32 ) ;
80
- let mut draw = |x : u32 , y : u32 , c| {
81
- let ( x, y) = ( x as i32 , y as i32 ) ;
86
+ let mut draw = |x : i32 , y : i32 , c| {
82
87
let ( base_x, base_y) = pos;
83
88
draw ( base_x + x, base_y + y, c)
84
89
} ;
@@ -93,23 +98,20 @@ impl FontData for FontDataInternal {
93
98
if let Some ( q) = font. outline_glyph ( glyph) {
94
99
use :: std:: panic:: { self , AssertUnwindSafe } ;
95
100
let rect = q. px_bounds ( ) ;
96
- // Vertically center the things.
97
- let y_shift = ( size as f32 - rect. height ( ) ) / 2.0 ;
98
- let y_shift = y_shift as u32 ;
101
+ let y_shift = ( ( size as f32 ) / 2.0 + rect. min . y ) as i32 ;
102
+ let x_shift = x_shift as i32 ;
99
103
let res = panic:: catch_unwind ( AssertUnwindSafe ( || {
100
104
q. draw ( |x, y, c| {
101
- if let Err ( _) = draw ( x + ( x_shift as u32 ) , y + y_shift, c) {
105
+ if let Err ( _) = draw ( x as i32 + x_shift , y as i32 + y_shift, c) {
102
106
panic ! ( "fail" )
103
107
}
104
108
} ) ;
105
109
} ) ) ;
106
110
if let Err ( _) = res {
107
- return Err ( FontError :: Unknown )
111
+ return Err ( FontError :: Unknown ) ;
108
112
}
109
- x_shift += font. h_advance ( font. glyph_id ( c) ) ;
110
- } else {
111
- x_shift += font. h_advance ( font. glyph_id ( c) ) ;
112
113
}
114
+ x_shift += font. h_advance ( font. glyph_id ( c) ) ;
113
115
}
114
116
Ok ( Ok ( ( ) ) )
115
117
}
0 commit comments