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