11use glyphon:: {
2- Attrs , Buffer , Cache , Color , ContentType , CustomGlyph , RasterizationRequest , RasterizedCustomGlyph ,
3- Family , FontSystem , Metrics , Resolution , Shaping , SwashCache , TextArea , TextAtlas , TextBounds ,
4- TextRenderer , Viewport ,
2+ Attrs , Buffer , Cache , Color , ContentType , CustomGlyph , Family , FontSystem , Metrics ,
3+ RasterizeCustomGlyphRequest , RasterizedCustomGlyph , Resolution , Shaping , SwashCache , TextArea ,
4+ TextAtlas , TextBounds , TextRenderer , Viewport ,
55} ;
66use std:: sync:: Arc ;
77use wgpu:: {
@@ -28,16 +28,13 @@ struct WindowState {
2828 queue : wgpu:: Queue ,
2929 surface : wgpu:: Surface < ' static > ,
3030 surface_config : SurfaceConfiguration ,
31-
3231 font_system : FontSystem ,
3332 swash_cache : SwashCache ,
3433 viewport : glyphon:: Viewport ,
3534 atlas : glyphon:: TextAtlas ,
3635 text_renderer : glyphon:: TextRenderer ,
3736 text_buffer : glyphon:: Buffer ,
38-
39- rasterize_svg : Box < dyn Fn ( RasterizationRequest ) -> Option < RasterizedCustomGlyph > > ,
40-
37+ rasterize_svg : Box < dyn Fn ( RasterizeCustomGlyphRequest ) -> Option < RasterizedCustomGlyph > > ,
4138 // Make sure that the winit window is last in the struct so that
4239 // it is dropped after the wgpu surface is dropped, otherwise the
4340 // program may crash when closed. This is probably a bug in wgpu.
@@ -106,46 +103,47 @@ impl WindowState {
106103 let svg_0 = resvg:: usvg:: Tree :: from_data ( LION_SVG , & Default :: default ( ) ) . unwrap ( ) ;
107104 let svg_1 = resvg:: usvg:: Tree :: from_data ( EAGLE_SVG , & Default :: default ( ) ) . unwrap ( ) ;
108105
109- let rasterize_svg = move |input : RasterizationRequest | -> Option < RasterizedCustomGlyph > {
110- // Select the svg data based on the custom glyph ID.
111- let ( svg, content_type) = match input. id {
112- 0 => ( & svg_0, ContentType :: Mask ) ,
113- 1 => ( & svg_1, ContentType :: Color ) ,
114- _ => return None ,
115- } ;
116-
117- // Calculate the scale based on the "glyph size".
118- let svg_size = svg. size ( ) ;
119- let scale_x = input. width as f32 / svg_size. width ( ) ;
120- let scale_y = input. height as f32 / svg_size. height ( ) ;
121-
122- let Some ( mut pixmap) =
123- resvg:: tiny_skia:: Pixmap :: new ( input. width as u32 , input. height as u32 )
124- else {
125- return None ;
126- } ;
127-
128- let mut transform = resvg:: usvg:: Transform :: from_scale ( scale_x, scale_y) ;
106+ let rasterize_svg =
107+ move |input : RasterizeCustomGlyphRequest | -> Option < RasterizedCustomGlyph > {
108+ // Select the svg data based on the custom glyph ID.
109+ let ( svg, content_type) = match input. id {
110+ 0 => ( & svg_0, ContentType :: Mask ) ,
111+ 1 => ( & svg_1, ContentType :: Color ) ,
112+ _ => return None ,
113+ } ;
114+
115+ // Calculate the scale based on the "glyph size".
116+ let svg_size = svg. size ( ) ;
117+ let scale_x = input. width as f32 / svg_size. width ( ) ;
118+ let scale_y = input. height as f32 / svg_size. height ( ) ;
119+
120+ let Some ( mut pixmap) =
121+ resvg:: tiny_skia:: Pixmap :: new ( input. width as u32 , input. height as u32 )
122+ else {
123+ return None ;
124+ } ;
125+
126+ let mut transform = resvg:: usvg:: Transform :: from_scale ( scale_x, scale_y) ;
127+
128+ // Offset the glyph by the subpixel amount.
129+ let offset_x = input. x_bin . as_float ( ) ;
130+ let offset_y = input. y_bin . as_float ( ) ;
131+ if offset_x != 0.0 || offset_y != 0.0 {
132+ transform = transform. post_translate ( offset_x, offset_y) ;
133+ }
129134
130- // Offset the glyph by the subpixel amount.
131- let offset_x = input. x_bin . as_float ( ) ;
132- let offset_y = input. y_bin . as_float ( ) ;
133- if offset_x != 0.0 || offset_y != 0.0 {
134- transform = transform. post_translate ( offset_x, offset_y) ;
135- }
135+ resvg:: render ( svg, transform, & mut pixmap. as_mut ( ) ) ;
136136
137- resvg:: render ( svg, transform, & mut pixmap. as_mut ( ) ) ;
137+ let data: Vec < u8 > = if let ContentType :: Mask = content_type {
138+ // Only use the alpha channel for symbolic icons.
139+ pixmap. data ( ) . iter ( ) . skip ( 3 ) . step_by ( 4 ) . copied ( ) . collect ( )
140+ } else {
141+ pixmap. data ( ) . to_vec ( )
142+ } ;
138143
139- let data: Vec < u8 > = if let ContentType :: Mask = content_type {
140- // Only use the alpha channel for symbolic icons.
141- pixmap. data ( ) . iter ( ) . skip ( 3 ) . step_by ( 4 ) . copied ( ) . collect ( )
142- } else {
143- pixmap. data ( ) . to_vec ( )
144+ Some ( RasterizedCustomGlyph { data, content_type } )
144145 } ;
145146
146- Some ( RasterizedCustomGlyph { data, content_type } )
147- } ;
148-
149147 Self {
150148 device,
151149 queue,
0 commit comments