.
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
65 public static SvgStyle parse (Attributes attributes) {
66 SvgStyle s = new SvgStyle ();
67
68 foreach (Attribute a in attributes) {
69 if (a.get_name () == "style") {
70 s.parse_key_value_pairs (a.get_content ());
71 }
72
73 if (a.get_name () == "stroke-width") {
74 s.style.set ("stroke-width", a.get_content ());
75 }
76
77 if (a.get_name () == "stroke") {
78 s.style.set ("stroke", a.get_content ());
79 }
80
81 if (a.get_name () == "fill") {
82 s.style.set ("fill", a.get_content ());
83 }
84 }
85
86 return s;
87 }
88
89 void parse_key_value_pairs (string svg_style) {
90 string[] p = svg_style.split (";");
91 string[] pair;
92 string k, v;
93
94 foreach (string kv in p) {
95 pair = kv.split (":");
96
97 if (pair.length != 2) {
98 warning ("pair.length != 2");
99 continue;
100 }
101
102 k = pair[0];
103 v = pair[1];
104
105 style.set (k, v);
106 }
107 }
108
109 public void apply (PathList path_list) {
110 foreach (Path p in path_list.paths) {
111 if (has_stroke ()) {
112 p.stroke = get_stroke_width ();
113 } else {
114 p.stroke = 0;
115 }
116
117 p.line_cap = get_line_cap ();
118 p.reset_stroke ();
119 p.update_region_boundaries ();
120 }
121 }
122 }
123
124 }
125