The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

SvgStyle.vala in libsvgbird

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git

Revisions

View the latest version of libsvgbird/SvgStyle.vala.
Fix bounding box for SVG files with strokes
1 /* 2 Copyright (C) 2015 2016 Johan Mattsson 3 4 This library is free software; you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 3 of the 7 License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 */ 14 15 using B; 16 using Math; 17 18 namespace SvgBird { 19 20 public class SvgStyle : GLib.Object { 21 public Gee.HashMap<string, string> style = new Gee.HashMap<string, string> (); 22 23 public Color? stroke = null; 24 public Color? fill = null; 25 public Gradient? stroke_gradient = null; 26 public Gradient? fill_gradient = null; 27 public LineCap line_cap = LineCap.BUTT; 28 29 public double stroke_width = 0; 30 31 private static Gee.HashMap<string, string>? inheritance; 32 private static Mutex inheritance_mutex = new Mutex (); 33 34 public SvgStyle () { 35 } 36 37 public SvgStyle.for_properties (Defs? defs, string style) { 38 parse_key_value_pairs (style); 39 set_style_properties (defs, this); 40 } 41 42 public void set_style (string property, string style) { 43 this.style.set (property, style); 44 } 45 46 public bool property_equals (string property, string value) { 47 string? p = get_css_property (property); 48 49 if (p == null) { 50 return false; 51 } 52 53 string css_property = (!) p; 54 return css_property == value; 55 } 56 57 public bool has_css_property (string property) { 58 return style.has_key (property); 59 } 60 61 public string? get_css_property (string property) { 62 string p = property.down (); 63 64 if (!has_css_property (p)) { 65 return null; 66 } 67 68 return style.get (p); 69 } 70 71 public static bool is_inherited (string property) { 72 inheritance_mutex.lock (); 73 if (unlikely (inheritance == null)) { 74 create_inheritance_table (); 75 } 76 77 Gee.HashMap<string, string> inheritance = (!) inheritance; 78 string? inherited = inheritance.get (property); 79 inheritance_mutex.unlock (); 80 81 if (inherited == null) { 82 return false; 83 } 84 85 string inherited_property = (!) inherited; 86 return inherited_property == "yes"; 87 } 88 89 /** Specify inheritance for a CSS property. */ 90 public static void set_inheritance (string property, bool inherit) { 91 inheritance_mutex.lock (); 92 if (unlikely (inheritance == null)) { 93 create_inheritance_table (); 94 } 95 96 Gee.HashMap<string, string> inheritance = (!) inheritance; 97 string inherit_property = inherit ? "yes" : "no"; 98 inheritance.set (property, inherit_property); 99 inheritance_mutex.unlock (); 100 } 101 102 public SvgStyle copy () { 103 SvgStyle style_copy = new SvgStyle (); 104 105 foreach (string key in style.keys) { 106 style_copy.style.set (key, style.get (key)); 107 } 108 109 if (stroke != null) { 110 style_copy.stroke = ((!) stroke).copy (); 111 } 112 113 if (fill != null) { 114 style_copy.fill = ((!) fill).copy (); 115 } 116 117 if (stroke_gradient != null) { 118 style_copy.stroke_gradient = ((!) stroke_gradient).copy (); 119 } 120 121 if (fill_gradient != null) { 122 style_copy.fill_gradient = ((!) fill_gradient).copy (); 123 } 124 125 style_copy.stroke_width = stroke_width; 126 127 return style_copy; 128 } 129 130 public LineCap get_line_cap () { 131 string l; 132 133 if (!style.has_key ("stroke-linecap")) { 134 return LineCap.BUTT; 135 } 136 137 l = style.get ("stroke-linecap"); 138 139 if (l == "round") { 140 return LineCap.ROUND; 141 } else if (l == "square") { 142 return LineCap.SQUARE; 143 } 144 145 return LineCap.BUTT; 146 } 147 148 public bool has_stroke () { 149 bool s = true; 150 151 if (style.has_key ("stroke")) { 152 s = style.get ("stroke") != "none"; 153 } 154 155 return stroke_width > 0 && s; 156 } 157 158 public double get_stroke_width () { 159 return stroke_width; 160 } 161 162 public string to_string () { 163 StringBuilder description = new StringBuilder (); 164 165 description.append ("SvgStyle: "); 166 167 foreach (string key in style.keys) { 168 description.append (key); 169 description.append (": "); 170 description.append (style.get (key)); 171 description.append ("; "); 172 } 173 174 return description.str.strip (); 175 } 176 177 public void inherit (SvgStyle inherited) { 178 foreach (string key in inherited.style.keys) { 179 if (is_inherited (key)) { 180 style.set (key, inherited.style.get (key)); 181 } 182 } 183 } 184 185 public void apply (SvgStyle inherited) { 186 foreach (string key in inherited.style.keys) { 187 style.set (key, inherited.style.get (key)); 188 } 189 } 190 191 public static SvgStyle parse (Defs? d, SvgStyle inherited, XmlElement tag) { 192 SvgStyle s = new SvgStyle (); 193 Attributes attributes = tag.get_attributes (); 194 195 s.style.set ("fill", "#000000"); // default fill value 196 s.inherit (inherited); 197 198 if (d != null) { 199 StyleSheet style_sheet = ((!) d).style_sheet; 200 style_sheet.apply_style (tag, s); 201 } 202 203 foreach (Attribute a in attributes) { 204 if (a.get_name () == "style") { 205 s.parse_key_value_pairs (a.get_content ()); 206 } 207 208 if (a.get_name () == "stroke-width") { 209 s.style.set ("stroke-width", a.get_content ()); 210 } 211 212 if (a.get_name () == "stroke") { 213 s.style.set ("stroke", a.get_content ()); 214 } 215 216 if (a.get_name () == "fill") { 217 s.style.set ("fill", a.get_content ()); 218 } 219 220 if (a.get_name () == "fill-opacity") { 221 s.style.set ("fill-opacity", a.get_content ()); 222 } 223 224 if (a.get_name () == "stroke-opacity") { 225 s.style.set ("stroke-opacity", a.get_content ()); 226 } 227 } 228 229 set_style_properties (d, s); 230 return s; 231 } 232 233 static void set_style_properties (Defs? d, SvgStyle s) { 234 double fill_opacity = 1; 235 double stroke_opacity = 1; 236 237 s.stroke_width = SvgFile.parse_number (s.style.get ("stroke-width")); 238 s.stroke = Color.parse (s.style.get ("stroke")); 239 s.fill = Color.parse (s.style.get ("fill")); 240 241 string? opacity = s.style.get ("fill-opacity"); 242 if (opacity != null) { 243 fill_opacity = SvgFile.parse_number ((!) opacity); 244 } 245 246 opacity = s.style.get ("stroke-opacity"); 247 if (opacity != null) { 248 stroke_opacity = SvgFile.parse_number ((!) opacity); 249 } 250 251 if (d != null) { 252 Defs defs = (!) d; 253 254 s.stroke_gradient = defs.get_gradient_for_url (s.style.get ("stroke")); 255 s.fill_gradient = defs.get_gradient_for_url (s.style.get ("fill")); 256 } 257 258 if (s.fill != null) { 259 Color color = (!) s.fill; 260 color.a = fill_opacity; 261 } 262 263 if (s.stroke != null) { 264 Color color = (!) s.stroke; 265 color.a = stroke_opacity; 266 } 267 } 268 269 void parse_key_value_pairs (string svg_style) { 270 string[] p = svg_style.strip ().split (";"); 271 string[] pair; 272 string k, v; 273 274 foreach (string kv in p) { 275 if (kv.index_of (":") != -1) { 276 pair = kv.split (":"); 277 278 if (pair.length != 2) { 279 warning ("pair.length != 2 in " + svg_style); 280 continue; 281 } 282 283 k = pair[0].strip ().down (); 284 v = pair[1].strip ().down (); 285 286 if (k == "padding") { 287 parse_padding_shorthand (v); 288 } else if (k == "margin") { 289 parse_margin_shorthand (v); 290 } else { 291 style.set (k, v); 292 } 293 } 294 } 295 } 296 297 void parse_margin_shorthand (string arguments) { 298 string[] args = StyleSheet.replace_whitespace (arguments).split (" "); 299 300 if (args.length > 0) { 301 style.set ("margin-top", args[0]); 302 } 303 304 if (args.length > 1) { 305 style.set ("margin-right", args[1]); 306 } 307 308 if (args.length > 2) { 309 style.set ("margin-bottom", args[2]); 310 } 311 312 if (args.length > 3) { 313 style.set ("margin-left", args[3]); 314 } 315 } 316 317 void parse_padding_shorthand (string arguments) { 318 string[] args = StyleSheet.replace_whitespace (arguments).split (" "); 319 320 if (args.length > 0) { 321 style.set ("padding-top", args[0]); 322 } 323 324 if (args.length > 1) { 325 style.set ("padding-right", args[1]); 326 } 327 328 if (args.length > 2) { 329 style.set ("padding-bottom", args[2]); 330 } 331 332 if (args.length > 3) { 333 style.set ("padding-left", args[3]); 334 } 335 } 336 337 private static void create_inheritance_table () { 338 inheritance = new Gee.HashMap<string, string> (); 339 340 Gee.HashMap<string, string> inherited = (!) inheritance; 341 342 inherited.set ("azimuth", "yes"); 343 inherited.set ("background-attachment", "no"); 344 inherited.set ("background-color", "no"); 345 inherited.set ("background-image", "no"); 346 inherited.set ("background-position", "no"); 347 inherited.set ("background-repeat", "no"); 348 inherited.set ("background", "no"); 349 inherited.set ("border-collapse", "yes"); 350 inherited.set ("border-color", "no"); 351 inherited.set ("border-spacing", "yes"); 352 inherited.set ("border-style", "no"); 353 inherited.set ("border-top", "no"); 354 inherited.set ("border-right", "no"); 355 inherited.set ("border-bottom", "no"); 356 inherited.set ("border-left", "no"); 357 inherited.set ("border-top-color", "no"); 358 inherited.set ("border-right-color", "no"); 359 inherited.set ("border-bottom-color", "no"); 360 inherited.set ("border-left-color", "no"); 361 inherited.set ("border-top-style", "no"); 362 inherited.set ("border-right-style", "no"); 363 inherited.set ("border-bottom-style", "no"); 364 inherited.set ("border-left-style", "no"); 365 inherited.set ("border-top-width", "no"); 366 inherited.set ("border-right-width", "no"); 367 inherited.set ("border-bottom-width", "no"); 368 inherited.set ("border-left-width", "no"); 369 inherited.set ("border-width", "no"); 370 inherited.set ("border", "no"); 371 inherited.set ("bottom", "no"); 372 inherited.set ("caption-side", "yes"); 373 inherited.set ("clear", "no"); 374 inherited.set ("clip", "no"); 375 inherited.set ("color", "yes"); 376 inherited.set ("content", "no"); 377 inherited.set ("counter-increment", "no"); 378 inherited.set ("counter-reset", "no"); 379 inherited.set ("cue-after", "no"); 380 inherited.set ("cue-before", "no"); 381 inherited.set ("cue", "no"); 382 inherited.set ("cursor", "yes"); 383 inherited.set ("direction", "yes"); 384 inherited.set ("display", "no"); 385 inherited.set ("elevation", "yes"); 386 inherited.set ("empty-cells", "yes"); 387 inherited.set ("float", "no"); 388 inherited.set ("font-family", "yes"); 389 inherited.set ("font-size", "yes"); 390 inherited.set ("font-style", "yes"); 391 inherited.set ("font-variant", "yes"); 392 inherited.set ("font-weight", "yes"); 393 inherited.set ("font", "yes"); 394 inherited.set ("height", "no"); 395 inherited.set ("left", "no"); 396 inherited.set ("letter-spacing", "yes"); 397 inherited.set ("line-height", "yes"); 398 inherited.set ("list-style-image", "yes"); 399 inherited.set ("list-style-position", "yes"); 400 inherited.set ("list-style-type", "yes"); 401 inherited.set ("list-style", "yes"); 402 inherited.set ("margin-right", "no"); 403 inherited.set ("margin-left", "no"); 404 inherited.set ("margin-top", "no"); 405 inherited.set ("margin-bottom", "no"); 406 inherited.set ("margin", "no"); 407 inherited.set ("max-height", "no"); 408 inherited.set ("max-width", "no"); 409 inherited.set ("min-height", "no"); 410 inherited.set ("min-width", "no"); 411 inherited.set ("orphans", "yes"); 412 inherited.set ("outline-color", "no"); 413 inherited.set ("outline-style", "no"); 414 inherited.set ("outline-width", "no"); 415 inherited.set ("outline", "no"); 416 inherited.set ("overflow", "no"); 417 inherited.set ("padding-top", "no"); 418 inherited.set ("padding-right", "no"); 419 inherited.set ("padding-bottom", "no"); 420 inherited.set ("padding-left", "no"); 421 inherited.set ("padding", "no"); 422 inherited.set ("page-break-after", "no"); 423 inherited.set ("page-break-before", "no"); 424 inherited.set ("page-break-inside", "no"); 425 inherited.set ("pause-after", "no"); 426 inherited.set ("pause-before", "no"); 427 inherited.set ("pause", "no"); 428 inherited.set ("pitch-range", "yes"); 429 inherited.set ("pitch", "yes"); 430 inherited.set ("play-during", "no"); 431 inherited.set ("position", "no"); 432 inherited.set ("quotes", "yes"); 433 inherited.set ("richness", "yes"); 434 inherited.set ("right", "no"); 435 inherited.set ("speak-header", "yes"); 436 inherited.set ("speak-numeral", "yes"); 437 inherited.set ("speak-punctuation", "yes"); 438 inherited.set ("speak", "yes"); 439 inherited.set ("speech-rate", "yes"); 440 inherited.set ("stress", "yes"); 441 inherited.set ("table-layout", "no"); 442 inherited.set ("text-align", "yes"); 443 inherited.set ("text-decoration", "no"); 444 inherited.set ("text-indent", "yes"); 445 inherited.set ("text-transform", "yes"); 446 inherited.set ("top", "no"); 447 inherited.set ("unicode-bidi", "no"); 448 inherited.set ("vertical-align", "no"); 449 inherited.set ("visibility", "yes"); 450 inherited.set ("voice-family", "yes"); 451 inherited.set ("volume", "yes"); 452 inherited.set ("white-space", "yes"); 453 inherited.set ("widows", "yes"); 454 inherited.set ("width", "no"); 455 inherited.set ("word-spacing", "yes"); 456 inherited.set ("z-index", "no"); 457 } 458 } 459 460 } 461