The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

SvgStyle.vala in libbirdfont

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/SvgStyle.vala.
Parse more style attributes in SVG files
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 SvgStyle () { 25 style = new Gee.HashMap<string, string> (); 26 } 27 28 public LineCap get_line_cap () { 29 string l; 30 31 if (!style.has_key ("stroke-linecap")) { 32 return LineCap.BUTT; 33 } 34 35 l = style.get ("stroke-linecap"); 36 37 if (l == "round") { 38 return LineCap.ROUND; 39 } else if (l == "square") { 40 return LineCap.SQUARE; 41 } 42 43 return LineCap.BUTT; 44 } 45 46 public bool has_stroke () { 47 bool s = true; 48 49 if (style.has_key ("stroke")) { 50 s = style.get ("stroke") != "none"; 51 } 52 53 return get_stroke_width () > 0 && s; 54 } 55 56 public double get_stroke_width () { 57 if (!style.has_key ("stroke-width")) { 58 return 0; 59 } 60 61 return double.parse (style.get ("stroke-width")); 62 } 63 64 public static SvgStyle parse (string svg_style) { 65 string[] p = svg_style.split (";"); 66 string[] pair; 67 string k, v; 68 SvgStyle s = new SvgStyle (); 69 70 foreach (string kv in p) { 71 pair = kv.split (":"); 72 73 if (pair.length != 2) { 74 warning ("pair.length != 2"); 75 continue; 76 } 77 78 k = pair[0]; 79 v = pair[1]; 80 81 s.style.set (k, v); 82 } 83 84 return s; 85 } 86 87 public void apply (PathList path_list) { 88 foreach (Path p in path_list.paths) { 89 if (has_stroke ()) { 90 p.stroke = get_stroke_width (); 91 } else { 92 p.stroke = 0; 93 } 94 95 p.line_cap = get_line_cap (); 96 p.reset_stroke (); 97 p.update_region_boundaries (); 98 } 99 } 100 } 101 102 } 103