The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

SvgStyle.vala in libbirdfont/Svg

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 libbirdfont/Svg/SvgStyle.vala.
Use SVG transform, parse opacity and gradients
1 /* 2 Copyright (C) 2015 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 BirdFont { 19 20 public class SvgStyle { 21 22 Gee.HashMap<string, string> style; 23 24 public Color? stroke = null; 25 public Color? fill = null; 26 27 public SvgStyle () { 28 style = new Gee.HashMap<string, string> (); 29 } 30 31 public LineCap get_line_cap () { 32 string l; 33 34 if (!style.has_key ("stroke-linecap")) { 35 return LineCap.BUTT; 36 } 37 38 l = style.get ("stroke-linecap"); 39 40 if (l == "round") { 41 return LineCap.ROUND; 42 } else if (l == "square") { 43 return LineCap.SQUARE; 44 } 45 46 return LineCap.BUTT; 47 } 48 49 public bool has_stroke () { 50 bool s = true; 51 52 if (style.has_key ("stroke")) { 53 s = style.get ("stroke") != "none"; 54 } 55 56 return get_stroke_width () > 0 && s; 57 } 58 59 public double get_stroke_width () { 60 if (!style.has_key ("stroke-width")) { 61 return 0; 62 } 63 64 return double.parse (style.get ("stroke-width")); 65 } 66 67 68 public static SvgStyle parse (Attributes attributes) { 69 SvgStyle s = new SvgStyle (); 70 71 foreach (Attribute a in attributes) { 72 if (a.get_name () == "style") { 73 s.parse_key_value_pairs (a.get_content ()); 74 } 75 76 if (a.get_name () == "stroke-width") { 77 s.style.set ("stroke-width", a.get_content ()); 78 } 79 80 if (a.get_name () == "stroke") { 81 s.style.set ("stroke", a.get_content ()); 82 } 83 84 if (a.get_name () == "fill") { 85 s.style.set ("fill", a.get_content ()); 86 } 87 } 88 89 s.stroke = Color.parse (s.style.get ("stroke")); 90 s.fill = Color.parse (s.style.get ("fill")); 91 92 return s; 93 } 94 95 void parse_key_value_pairs (string svg_style) { 96 string[] p = svg_style.split (";"); 97 string[] pair; 98 string k, v; 99 100 foreach (string kv in p) { 101 pair = kv.split (":"); 102 103 if (pair.length != 2) { 104 warning ("pair.length != 2"); 105 continue; 106 } 107 108 k = pair[0]; 109 v = pair[1]; 110 111 style.set (k, v); 112 } 113 } 114 115 public void apply (PathList path_list) { 116 foreach (Path p in path_list.paths) { 117 if (has_stroke ()) { 118 p.stroke = get_stroke_width (); 119 } else { 120 p.stroke = 0; 121 } 122 123 p.line_cap = get_line_cap (); 124 p.reset_stroke (); 125 p.update_region_boundaries (); 126 } 127 } 128 } 129 130 } 131