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