11fn main ( ) {
2+ println ! ( "cargo:rerun-if-changed=build.rs" ) ;
3+
24 #[ cfg( feature = "build-graphics" ) ]
35 build_graphics ( ) ;
46}
@@ -7,8 +9,13 @@ fn main() {
79fn build_graphics ( ) {
810 use svg:: emblem:: Emblem ;
911
12+ // Rerun if any icon files change
13+ for entry in std:: fs:: read_dir ( "icons" ) . unwrap ( ) {
14+ let entry = entry. unwrap ( ) ;
15+ println ! ( "cargo:rerun-if-changed={}" , entry. path( ) . display( ) ) ;
16+ }
17+
1018 let manifest_dir = std:: env:: var ( "CARGO_MANIFEST_DIR" ) . unwrap ( ) ;
11- let out_dir = std:: env:: var ( "OUT_DIR" ) . unwrap ( ) ;
1219
1320 for bg_style in [ true , false ] {
1421 for ( project, emblem) in [
@@ -144,27 +151,23 @@ fn build_graphics() {
144151 rect_side_px : 7 ,
145152 rect_gap_px : 1 ,
146153 rect_style : "fill:#3776c8" ,
147- icon_style : "fill:#3776c8" ,
154+ icon_style : "fill:#3776c8;stroke:#3776c8 " ,
148155 icon : include_str ! ( "icons/workset.svg" ) . to_string ( ) ,
149156 icon_width : Some ( 50 ) ,
150157 bg_style : if bg_style { Some ( "fill:#333333" ) } else { None } ,
151158 } ,
152159 ) ,
153160 ] {
154161 let emblem_svg = emblem. to_svg ( ) . unwrap ( ) ;
155- let path = format ! ( "{out_dir} /{project}.svg" ) ;
162+ let path = format ! ( "{manifest_dir}/emblems /{project}.svg" ) ;
156163
157164 // Write svg
158- if !std:: fs:: exists ( & path) . unwrap ( ) {
159- emblem_svg. write_to ( & path) . unwrap ( ) ;
160- }
165+ emblem_svg. write_to ( & path) . unwrap ( ) ;
161166
162167 // Write emblem rasters in varying sizes
163- for ( width, height) in [ ( 256 , 128 ) , ( 512 , 256 ) , ( 1024 , 512 ) , ( 2048 , 1024 ) ] {
164- let path = format ! ( "{out_dir}/{project}-{height}.png" ) ;
165- if !std:: fs:: exists ( & path) . unwrap ( ) {
166- emblem_svg. rasterize ( & path, width, height) . unwrap ( ) ;
167- }
168+ for ( width, height) in [ ( 256 , 64 ) , ( 512 , 128 ) , ( 1024 , 256 ) , ( 2048 , 512 ) ] {
169+ let path = format ! ( "{manifest_dir}/emblems/{project}-{width}.png" ) ;
170+ emblem_svg. rasterize ( & path, width, height) . unwrap ( ) ;
168171 }
169172
170173 // Write icon rasters in varying sizes
@@ -176,20 +179,6 @@ fn build_graphics() {
176179 // }
177180 }
178181 }
179-
180- // Create symlink in manifest dir to output directory
181- let symlink_path = format ! ( "{manifest_dir}/generated" ) ;
182-
183- // Remove existing symlink if it exists
184- if std:: fs:: exists ( & symlink_path) . unwrap ( ) {
185- std:: fs:: remove_file ( & symlink_path) . ok ( ) ;
186- }
187-
188- // Create new symlink pointing to current OUT_DIR
189- #[ cfg( unix) ]
190- std:: os:: unix:: fs:: symlink ( & out_dir, & symlink_path) . unwrap ( ) ;
191- #[ cfg( windows) ]
192- std:: os:: windows:: fs:: symlink_dir ( & out_dir, & symlink_path) . unwrap ( ) ;
193182}
194183
195184#[ cfg( feature = "build-graphics" ) ]
@@ -238,7 +227,13 @@ pub mod svg {
238227 {
239228 let svg = usvg:: Tree :: from_str ( & self . to_string ( ) , & usvg:: Options :: default ( ) ) ?;
240229 let mut pixmap = Pixmap :: new ( width, height) . expect ( "invalid size" ) ;
241- resvg:: render ( & svg, Transform :: default ( ) , & mut pixmap. as_mut ( ) ) ;
230+
231+ // Calculate scale to fit the SVG into the target dimensions
232+ let svg_size = svg. size ( ) ;
233+ let scale = width as f32 / svg_size. width ( ) ;
234+
235+ let transform = Transform :: from_scale ( scale, scale) ;
236+ resvg:: render ( & svg, transform, & mut pixmap. as_mut ( ) ) ;
242237 pixmap. save_png ( path. as_ref ( ) ) ?;
243238 Ok ( ( ) )
244239 }
@@ -253,6 +248,8 @@ pub mod svg {
253248 pub rect : Vec < SvgRect > ,
254249 #[ serde( default ) ]
255250 pub ellipse : Vec < SvgEllipse > ,
251+ #[ serde( default ) ]
252+ pub circle : Vec < SvgCircle > ,
256253 #[ serde( rename = "@transform" ) ]
257254 pub transform : Option < String > ,
258255 }
@@ -320,6 +317,29 @@ pub mod svg {
320317 pub style : Option < String > ,
321318 }
322319
320+ #[ derive( Serialize , Deserialize , Default , Clone ) ]
321+ #[ serde( rename = "circle" ) ]
322+ pub struct SvgCircle {
323+ #[ serde( rename = "@cx" ) ]
324+ pub cx : String ,
325+ #[ serde( rename = "@cy" ) ]
326+ pub cy : String ,
327+ #[ serde( rename = "@id" ) ]
328+ pub id : String ,
329+ #[ serde( rename = "@r" ) ]
330+ pub rx : String ,
331+ #[ serde( rename = "@stroke" , skip_serializing_if = "Option::is_none" ) ]
332+ pub stroke : Option < String > ,
333+ #[ serde( rename = "@stroke-linecap" , skip_serializing_if = "Option::is_none" ) ]
334+ pub stroke_linecap : Option < String > ,
335+ #[ serde( rename = "@stroke-linejoin" , skip_serializing_if = "Option::is_none" ) ]
336+ pub stroke_linejoin : Option < String > ,
337+ #[ serde( rename = "@stroke-width" , skip_serializing_if = "Option::is_none" ) ]
338+ pub stroke_width : Option < String > ,
339+ #[ serde( rename = "@style" ) ]
340+ pub style : Option < String > ,
341+ }
342+
323343 pub mod emblem {
324344 use super :: * ;
325345
@@ -365,12 +385,11 @@ pub mod svg {
365385 if self . icon_width . is_some ( ) {
366386 let icon: Svg = quick_xml:: de:: from_str ( self . icon . as_str ( ) ) ?;
367387
388+ let icon_group = icon. g . first ( ) . unwrap ( ) ;
389+
368390 // Position the icon
369391 svg. g . push ( SvgGroup {
370- path : icon
371- . g
372- . first ( )
373- . unwrap ( )
392+ path : icon_group
374393 . path
375394 . iter ( )
376395 . map ( |p| {
@@ -380,6 +399,36 @@ pub mod svg {
380399 p. clone ( )
381400 } )
382401 . collect ( ) ,
402+ rect : icon_group
403+ . rect
404+ . iter ( )
405+ . map ( |r| {
406+ // Replace style
407+ let mut r = r. clone ( ) ;
408+ r. style = self . icon_style . to_string ( ) ;
409+ r. clone ( )
410+ } )
411+ . collect ( ) ,
412+ ellipse : icon_group
413+ . ellipse
414+ . iter ( )
415+ . map ( |e| {
416+ // Replace style
417+ let mut e = e. clone ( ) ;
418+ e. style = Some ( self . icon_style . to_string ( ) ) ;
419+ e. clone ( )
420+ } )
421+ . collect ( ) ,
422+ circle : icon_group
423+ . circle
424+ . iter ( )
425+ . map ( |c| {
426+ // Replace style
427+ let mut c = c. clone ( ) ;
428+ c. style = Some ( self . icon_style . to_string ( ) ) ;
429+ c. clone ( )
430+ } )
431+ . collect ( ) ,
383432 transform : Some ( format ! (
384433 "translate({},{})" ,
385434 self . margin_px / 2 ,
0 commit comments