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 public Gee.HashMap<string, string> style; 23 24 public Color? stroke = null; 25 public Color? fill = null; 26 public Gradient? stroke_gradient = null; 27 public Gradient? fill_gradient = null; 28 29 public double stroke_width = 0; 30 31 public SvgStyle () { 32 style = new Gee.HashMap<string, string> (); 33 } 34 35 public LineCap get_line_cap () { 36 string l; 37 38 if (!style.has_key ("stroke-linecap")) { 39 return LineCap.BUTT; 40 } 41 42 l = style.get ("stroke-linecap"); 43 44 if (l == "round") { 45 return LineCap.ROUND; 46 } else if (l == "square") { 47 return LineCap.SQUARE; 48 } 49 50 return LineCap.BUTT; 51 } 52 53 public bool has_stroke () { 54 bool s = true; 55 56 if (style.has_key ("stroke")) { 57 s = style.get ("stroke") != "none"; 58 } 59 60 return get_stroke_width () > 0 && s; 61 } 62 63 public double get_stroke_width () { 64 if (!style.has_key ("stroke-width")) { 65 return 0; 66 } 67 68 return double.parse (style.get ("stroke-width")); 69 } 70 71 public static SvgStyle parse (Attributes attributes) { 72 SvgStyle s = new SvgStyle (); 73 double fill_opacity = 1; 74 double stroke_opacity = 1; 75 76 foreach (Attribute a in attributes) { 77 if (a.get_name () == "style") { 78 s.parse_key_value_pairs (a.get_content ()); 79 } 80 81 if (a.get_name () == "stroke-width") { 82 s.style.set ("stroke-width", a.get_content ()); 83 } 84 85 if (a.get_name () == "stroke") { 86 s.style.set ("stroke", a.get_content ()); 87 } 88 89 if (a.get_name () == "fill") { 90 s.style.set ("fill", a.get_content ()); 91 } 92 93 if (a.get_name () == "fill-opacity") { 94 fill_opacity = SvgFile.parse_number (a.get_content ()); 95 } 96 97 if (a.get_name () == "stroke-opacity") { 98 stroke_opacity = SvgFile.parse_number (a.get_content ()); 99 } 100 } 101 102 s.stroke = Color.parse (s.style.get ("stroke")); 103 104 if (s.fill != null) { 105 Color color = (!) s.fill; 106 color.a = fill_opacity; 107 } 108 109 if (s.stroke != null) { 110 Color color = (!) s.stroke; 111 color.a = stroke_opacity; 112 } 113 114 s.stroke_width = SvgFile.parse_number (s.style.get ("stroke-width")); 115 s.fill = Color.parse (s.style.get ("fill")); 116 117 return s; 118 } 119 120 void parse_key_value_pairs (string svg_style) { 121 string[] p = svg_style.split (";"); 122 string[] pair; 123 string k, v; 124 125 foreach (string kv in p) { 126 pair = kv.split (":"); 127 128 if (pair.length != 2) { 129 warning ("pair.length != 2"); 130 continue; 131 } 132 133 k = pair[0]; 134 v = pair[1]; 135 136 style.set (k, v); 137 } 138 } 139 140 public void apply (PathList path_list) { 141 foreach (Path p in path_list.paths) { 142 if (has_stroke ()) { 143 p.stroke = get_stroke_width (); 144 } else { 145 p.stroke = 0; 146 } 147 148 p.line_cap = get_line_cap (); 149 p.reset_stroke (); 150 p.update_region_boundaries (); 151 } 152 } 153 } 154 155 } 156