The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Defs.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
Circle boundaries heads/master
1 /* 2 Copyright (C) 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 Defs { 21 public Gee.ArrayList<ClipPath> clip_paths = new Gee.ArrayList<ClipPath> (); 22 public Gee.ArrayList<Gradient> gradients = new Gee.ArrayList<Gradient> (); 23 public Gee.ArrayList<RadialGradient> radial_gradients = new Gee.ArrayList<RadialGradient> (); 24 public Gee.ArrayList<LinearGradient> linear_gradients = new Gee.ArrayList<LinearGradient> (); 25 public StyleSheet style_sheet = new StyleSheet (); 26 27 public void add_linear_gradient (LinearGradient g) { 28 gradients.add (g); 29 linear_gradients.add (g); 30 } 31 32 public void add_radial_gradient (RadialGradient g) { 33 gradients.add (g); 34 radial_gradients.add (g); 35 } 36 37 public ClipPath? get_clip_path_for_url (string? url) { 38 if (url == null) { 39 return null; 40 } 41 42 string tag_id = get_id_from_url ((!) url); 43 return get_clip_path_for_id (tag_id); 44 } 45 46 public ClipPath? get_clip_path_for_id (string id) { 47 string tag_id; 48 49 if (id.has_prefix ("#")) { 50 tag_id = id.substring ("#".length); 51 } else { 52 tag_id = id; 53 } 54 55 foreach (ClipPath clip_path in clip_paths) { 56 if (clip_path.id == tag_id) { 57 return clip_path; 58 } 59 } 60 61 return null; 62 } 63 64 public static string get_id_from_url (string url) { 65 if (unlikely (!is_url (url))) { 66 return ""; 67 } 68 69 int p1 = url.index_of ("("); 70 if (unlikely (p1 == -1)) { 71 warning ("Not an URL: " + url); 72 return ""; 73 } 74 75 int p2 = url.index_of (")"); 76 if (unlikely (p2 == -1 || p2 < p1)) { 77 warning ("Not an URL: " + url); 78 return ""; 79 } 80 81 p1 += "(".length; 82 int length = p2 - p1; 83 return url.substring (p1, length); 84 } 85 86 public Gradient? get_gradient_for_url (string? url) { 87 if (url == null) { 88 return null; 89 } 90 91 string tag_id = get_id_from_url ((!) url); 92 return get_gradient_for_id (tag_id); 93 } 94 95 public Gradient? get_gradient_for_id (string id) { 96 string tag_id; 97 98 if (id.has_prefix ("#")) { 99 tag_id = id.substring ("#".length); 100 } else { 101 tag_id = id; 102 } 103 104 tag_id = tag_id.down (); 105 106 foreach (Gradient gradient in gradients) { 107 if (gradient.id.down () == tag_id) { 108 return gradient; 109 } 110 } 111 112 return null; 113 } 114 115 public static bool is_url (string? attribute) { 116 if (attribute == null) { 117 return false; 118 } 119 120 return ((!) attribute).has_prefix ("url"); 121 } 122 123 public Defs shallow_copy () { 124 Defs d = new Defs (); 125 126 foreach (Gradient g in gradients) { 127 d.gradients.add (g); 128 } 129 130 foreach (RadialGradient g in radial_gradients) { 131 d.radial_gradients.add (g); 132 } 133 134 foreach (LinearGradient g in linear_gradients) { 135 d.linear_gradients.add (g); 136 } 137 138 d.style_sheet = style_sheet.shallow_copy (); 139 140 return d; 141 } 142 143 public Defs copy () { 144 Defs d = new Defs (); 145 146 foreach (Gradient g in gradients) { 147 Gradient gradient_copy = g.copy (); 148 d.gradients.add (gradient_copy); 149 150 if (gradient_copy is LinearGradient) { 151 d.linear_gradients.add ((LinearGradient) gradient_copy); 152 } 153 154 if (gradient_copy is RadialGradient) { 155 d.radial_gradients.add ((RadialGradient) gradient_copy); 156 } 157 } 158 159 d.style_sheet = style_sheet.copy (); 160 161 return d; 162 } 163 } 164 165 } 166