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.
SVG M instruction
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 padding_top { 30 get { 31 return SvgFile.parse_number (get_css_property ("padding-top")); 32 } 33 34 set { 35 set_style ("padding-top", Doubles.round (value) + "px"); 36 } 37 } 38 39 public double padding_right { 40 get { 41 return SvgFile.parse_number (get_css_property ("padding-right")); 42 } 43 44 set { 45 set_style ("padding-right", Doubles.round (value) + "px"); 46 } 47 } 48 49 public double padding_bottom { 50 get { 51 return SvgFile.parse_number (get_css_property ("padding-bottom")); 52 } 53 54 set { 55 set_style ("padding-bottom", Doubles.round (value) + "px"); 56 } 57 } 58 59 public double padding_left { 60 get { 61 return SvgFile.parse_number (get_css_property ("padding-left")); 62 } 63 64 set { 65 set_style ("padding-left", Doubles.round (value) + "px"); 66 } 67 } 68 69 public double stroke_width = 0; 70 71 private static Gee.HashMap<string, string>? inheritance; 72 private static Mutex inheritance_mutex = new Mutex (); 73 74 public SvgStyle () { 75 } 76 77 public SvgStyle.for_properties (Defs? defs, string style) { 78 parse_key_value_pairs (style); 79 set_style_properties (defs, this); 80 } 81 82 public void set_style (string property, string style) { 83 this.style.set (property, style); 84 } 85 86 public bool property_equals (string property, string value) { 87 string? p = get_css_property (property); 88 89 if (p == null) { 90 return false; 91 } 92 93 string css_property = (!) p; 94 return css_property == value; 95 } 96 97 public bool has_css_property (string property) { 98 return style.has_key (property); 99 } 100 101 public string? get_css_property (string property) { 102 string p = property.down (); 103 104 if (!has_css_property (p)) { 105 return null; 106 } 107 108 return style.get (p); 109 } 110 111 public double get_double_property (string property) { 112 string p = property.down (); 113 114 if (!has_css_property (p)) { 115 return 0; 116 } 117 118 return SvgFile.parse_number (style.get (p)); 119 } 120 121 public static bool is_inherited (string property) { 122 inheritance_mutex.lock (); 123 if (unlikely (inheritance == null)) { 124 create_inheritance_table (); 125 } 126 127 Gee.HashMap<string, string> inheritance = (!) inheritance; 128 string? inherited = inheritance.get (property); 129 inheritance_mutex.unlock (); 130 131 if (inherited == null) { 132 return false; 133 } 134 135 string inherited_property = (!) inherited; 136 return inherited_property == "yes"; 137 } 138 139 /** Specify inheritance for a CSS property. */ 140 public static void set_inheritance (string property, bool inherit) { 141 inheritance_mutex.lock (); 142 if (unlikely (inheritance == null)) { 143 create_inheritance_table (); 144 } 145 146 Gee.HashMap<string, string> inheritance = (!) inheritance; 147 string inherit_property = inherit ? "yes" : "no"; 148 inheritance.set (property, inherit_property); 149 inheritance_mutex.unlock (); 150 } 151 152 public SvgStyle copy () { 153 SvgStyle style_copy = new SvgStyle (); 154 155 foreach (string key in style.keys) { 156 style_copy.style.set (key, style.get (key)); 157 } 158 159 if (stroke != null) { 160 style_copy.stroke = ((!) stroke).copy (); 161 } 162 163 if (fill != null) { 164 style_copy.fill = ((!) fill).copy (); 165 } 166 167 if (stroke_gradient != null) { 168 style_copy.stroke_gradient = ((!) stroke_gradient).copy (); 169 } 170 171 if (fill_gradient != null) { 172 style_copy.fill_gradient = ((!) fill_gradient).copy (); 173 } 174 175 style_copy.stroke_width = stroke_width; 176 177 return style_copy; 178 } 179 180 public LineCap get_line_cap () { 181 string l; 182 183 if (!style.has_key ("stroke-linecap")) { 184 return LineCap.BUTT; 185 } 186 187 l = style.get ("stroke-linecap"); 188 189 if (l == "round") { 190 return LineCap.ROUND; 191 } else if (l == "square") { 192 return LineCap.SQUARE; 193 } 194 195 return LineCap.BUTT; 196 } 197 198 public bool has_stroke () { 199 bool has_stroke = true; 200 201 if (style.has_key ("stroke")) { 202 has_stroke = style.get ("stroke") != "none"; 203 } 204 205 if (stroke != null) { 206 Color c = (!) stroke; 207 208 if (c.a == 0) { 209 return false; 210 } 211 } 212 213 return stroke_width > 0 && has_stroke; 214 } 215 216 public double get_stroke_width () { 217 return stroke_width; 218 } 219 220 public string to_string () { 221 StringBuilder description = new StringBuilder (); 222 223 description.append ("SvgStyle: "); 224 225 foreach (string key in style.keys) { 226 description.append (key); 227 description.append (": "); 228 description.append (style.get (key)); 229 description.append ("; "); 230 } 231 232 return description.str.strip (); 233 } 234 235 public void inherit (SvgStyle inherited) { 236 foreach (string key in inherited.style.keys) { 237 if (is_inherited (key)) { 238 style.set (key, inherited.style.get (key)); 239 } 240 } 241 } 242 243 public void apply (SvgStyle inherited) { 244 foreach (string key in inherited.style.keys) { 245 style.set (key, inherited.style.get (key)); 246 } 247 } 248 249 public static SvgStyle parse (Defs? d, SvgStyle inherited, XmlElement tag, string? psuedo_class) { 250 SvgStyle s = new SvgStyle (); 251 Attributes attributes = tag.get_attributes (); 252 253 s.style.set ("fill", "#000000"); // default fill value 254 s.inherit (inherited); 255 256 if (d != null) { 257 StyleSheet style_sheet = ((!) d).style_sheet; 258 style_sheet.apply_style (tag, s, psuedo_class); 259 } 260 261 foreach (Attribute a in attributes) { 262 if (a.get_name () == "style") { 263 s.parse_key_value_pairs (a.get_content ()); 264 } 265 266 if (a.get_name () == "stroke-width") { 267 s.style.set ("stroke-width", a.get_content ()); 268 } 269 270 if (a.get_name () == "stroke") { 271 s.style.set ("stroke", a.get_content ()); 272 } 273 274 if (a.get_name () == "fill") { 275 s.style.set ("fill", a.get_content ()); 276 } 277 278 if (a.get_name () == "fill-opacity") { 279 s.style.set ("fill-opacity", a.get_content ()); 280 } 281 282 if (a.get_name () == "stroke-opacity") { 283 s.style.set ("stroke-opacity", a.get_content ()); 284 } 285 } 286 287 set_style_properties (d, s); 288 return s; 289 } 290 291 public static void set_style_properties (Defs? d, SvgStyle s) { 292 double fill_opacity = 1; 293 double stroke_opacity = 1; 294 295 s.stroke_width = SvgFile.parse_number (s.style.get ("stroke-width")); 296 s.stroke = Color.parse (s.style.get ("stroke")); 297 s.fill = Color.parse (s.style.get ("fill")); 298 299 string? opacity = s.style.get ("fill-opacity"); 300 if (opacity != null) { 301 fill_opacity = SvgFile.parse_number ((!) opacity); 302 } 303 304 opacity = s.style.get ("stroke-opacity"); 305 if (opacity != null) { 306 stroke_opacity = SvgFile.parse_number ((!) opacity); 307 } 308 309 if (d != null) { 310 Defs defs = (!) d; 311 312 s.stroke_gradient = defs.get_gradient_for_url (s.style.get ("stroke")); 313 s.fill_gradient = defs.get_gradient_for_url (s.style.get ("fill")); 314 } 315 316 if (s.fill != null) { 317 Color color = (!) s.fill; 318 color.a = fill_opacity; 319 } 320 321 if (s.stroke != null) { 322 Color color = (!) s.stroke; 323 color.a = stroke_opacity; 324 } 325 } 326 327 void parse_key_value_pairs (string svg_style) { 328 string[] p = svg_style.strip ().split (";"); 329 string[] pair; 330 string k, v; 331 332 foreach (string kv in p) { 333 if (kv.index_of (":") != -1) { 334 pair = kv.split (":"); 335 336 if (pair.length != 2) { 337 warning ("pair.length != 2 in " + svg_style); 338 continue; 339 } 340 341 k = pair[0].strip ().down (); 342 v = pair[1].strip ().down (); 343 344 if (k == "padding") { 345 parse_padding_shorthand (v); 346 } else if (k == "margin") { 347 parse_margin_shorthand (v); 348 } else { 349 style.set (k, v); 350 } 351 } 352 } 353 } 354 355 void parse_margin_shorthand (string arguments) { 356 string[] args = StyleSheet.replace_whitespace (arguments).split (" "); 357 358 if (args.length > 0) { 359 style.set ("margin-top", args[0]); 360 } 361 362 if (args.length > 1) { 363 style.set ("margin-right", args[1]); 364 } 365 366 if (args.length > 2) { 367 style.set ("margin-bottom", args[2]); 368 } 369 370 if (args.length > 3) { 371 style.set ("margin-left", args[3]); 372 } 373 } 374 375 void parse_padding_shorthand (string arguments) { 376 string[] args = StyleSheet.replace_whitespace (arguments).split (" "); 377 378 if (args.length > 0) { 379 style.set ("padding-top", args[0]); 380 } 381 382 if (args.length > 1) { 383 style.set ("padding-right", args[1]); 384 } 385 386 if (args.length > 2) { 387 style.set ("padding-bottom", args[2]); 388 } 389 390 if (args.length > 3) { 391 style.set ("padding-left", args[3]); 392 } 393 } 394 395 private static void create_inheritance_table () { 396 inheritance = new Gee.HashMap<string, string> (); 397 398 Gee.HashMap<string, string> inherited = (!) inheritance; 399 400 inherited.set ("azimuth", "yes"); 401 inherited.set ("background-attachment", "no"); 402 inherited.set ("background-color", "no"); 403 inherited.set ("background-image", "no"); 404 inherited.set ("background-position", "no"); 405 inherited.set ("background-repeat", "no"); 406 inherited.set ("background", "no"); 407 inherited.set ("border-collapse", "yes"); 408 inherited.set ("border-color", "no"); 409 inherited.set ("border-spacing", "yes"); 410 inherited.set ("border-style", "no"); 411 inherited.set ("border-top", "no"); 412 inherited.set ("border-right", "no"); 413 inherited.set ("border-bottom", "no"); 414 inherited.set ("border-left", "no"); 415 inherited.set ("border-top-color", "no"); 416 inherited.set ("border-right-color", "no"); 417 inherited.set ("border-bottom-color", "no"); 418 inherited.set ("border-left-color", "no"); 419 inherited.set ("border-top-style", "no"); 420 inherited.set ("border-right-style", "no"); 421 inherited.set ("border-bottom-style", "no"); 422 inherited.set ("border-left-style", "no"); 423 inherited.set ("border-top-width", "no"); 424 inherited.set ("border-right-width", "no"); 425 inherited.set ("border-bottom-width", "no"); 426 inherited.set ("border-left-width", "no"); 427 inherited.set ("border-width", "no"); 428 inherited.set ("border", "no"); 429 inherited.set ("bottom", "no"); 430 inherited.set ("caption-side", "yes"); 431 inherited.set ("clear", "no"); 432 inherited.set ("clip", "no"); 433 inherited.set ("color", "yes"); 434 inherited.set ("content", "no"); 435 inherited.set ("counter-increment", "no"); 436 inherited.set ("counter-reset", "no"); 437 inherited.set ("cue-after", "no"); 438 inherited.set ("cue-before", "no"); 439 inherited.set ("cue", "no"); 440 inherited.set ("cursor", "yes"); 441 inherited.set ("direction", "yes"); 442 inherited.set ("display", "no"); 443 inherited.set ("elevation", "yes"); 444 inherited.set ("empty-cells", "yes"); 445 inherited.set ("float", "no"); 446 inherited.set ("font-family", "yes"); 447 inherited.set ("font-size", "yes"); 448 inherited.set ("font-style", "yes"); 449 inherited.set ("font-variant", "yes"); 450 inherited.set ("font-weight", "yes"); 451 inherited.set ("font", "yes"); 452 inherited.set ("height", "no"); 453 inherited.set ("left", "no"); 454 inherited.set ("letter-spacing", "yes"); 455 inherited.set ("line-height", "yes"); 456 inherited.set ("list-style-image", "yes"); 457 inherited.set ("list-style-position", "yes"); 458 inherited.set ("list-style-type", "yes"); 459 inherited.set ("list-style", "yes"); 460 inherited.set ("margin-right", "no"); 461 inherited.set ("margin-left", "no"); 462 inherited.set ("margin-top", "no"); 463 inherited.set ("margin-bottom", "no"); 464 inherited.set ("margin", "no"); 465 inherited.set ("max-height", "no"); 466 inherited.set ("max-width", "no"); 467 inherited.set ("min-height", "no"); 468 inherited.set ("min-width", "no"); 469 inherited.set ("orphans", "yes"); 470 inherited.set ("outline-color", "no"); 471 inherited.set ("outline-style", "no"); 472 inherited.set ("outline-width", "no"); 473 inherited.set ("outline", "no"); 474 inherited.set ("overflow", "no"); 475 inherited.set ("padding-top", "no"); 476 inherited.set ("padding-right", "no"); 477 inherited.set ("padding-bottom", "no"); 478 inherited.set ("padding-left", "no"); 479 inherited.set ("padding", "no"); 480 inherited.set ("page-break-after", "no"); 481 inherited.set ("page-break-before", "no"); 482 inherited.set ("page-break-inside", "no"); 483 inherited.set ("pause-after", "no"); 484 inherited.set ("pause-before", "no"); 485 inherited.set ("pause", "no"); 486 inherited.set ("pitch-range", "yes"); 487 inherited.set ("pitch", "yes"); 488 inherited.set ("play-during", "no"); 489 inherited.set ("position", "no"); 490 inherited.set ("quotes", "yes"); 491 inherited.set ("richness", "yes"); 492 inherited.set ("right", "no"); 493 inherited.set ("speak-header", "yes"); 494 inherited.set ("speak-numeral", "yes"); 495 inherited.set ("speak-punctuation", "yes"); 496 inherited.set ("speak", "yes"); 497 inherited.set ("speech-rate", "yes"); 498 inherited.set ("stress", "yes"); 499 inherited.set ("table-layout", "no"); 500 inherited.set ("text-align", "yes"); 501 inherited.set ("text-decoration", "no"); 502 inherited.set ("text-indent", "yes"); 503 inherited.set ("text-transform", "yes"); 504 inherited.set ("top", "no"); 505 inherited.set ("unicode-bidi", "no"); 506 inherited.set ("vertical-align", "no"); 507 inherited.set ("visibility", "yes"); 508 inherited.set ("voice-family", "yes"); 509 inherited.set ("volume", "yes"); 510 inherited.set ("white-space", "yes"); 511 inherited.set ("widows", "yes"); 512 inherited.set ("width", "no"); 513 inherited.set ("word-spacing", "yes"); 514 inherited.set ("z-index", "no"); 515 } 516 } 517 518 } 519