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

Revisions

View the latest version of libsvgbird/Defs.vala.
Ignore case in gradient URL
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 StyleSheet style_sheet = new StyleSheet (); 24 25 public void add (Gradient g) { 26 gradients.add (g); 27 } 28 29 public ClipPath? get_clip_path_for_url (string? url) { 30 if (url == null) { 31 return null; 32 } 33 34 string tag_id = get_id_from_url ((!) url); 35 return get_clip_path_for_id (tag_id); 36 } 37 38 public ClipPath? get_clip_path_for_id (string id) { 39 string tag_id; 40 41 if (id.has_prefix ("#")) { 42 tag_id = id.substring ("#".length); 43 } else { 44 tag_id = id; 45 } 46 47 foreach (ClipPath clip_path in clip_paths) { 48 if (clip_path.id == tag_id) { 49 return clip_path; 50 } 51 } 52 53 return null; 54 } 55 56 public static string get_id_from_url (string url) { 57 if (unlikely (!is_url (url))) { 58 return ""; 59 } 60 61 int p1 = url.index_of ("("); 62 if (unlikely (p1 == -1)) { 63 warning ("Not an URL: " + url); 64 return ""; 65 } 66 67 int p2 = url.index_of (")"); 68 if (unlikely (p2 == -1 || p2 < p1)) { 69 warning ("Not an URL: " + url); 70 return ""; 71 } 72 73 p1 += "(".length; 74 int length = p2 - p1; 75 return url.substring (p1, length); 76 } 77 78 public Gradient? get_gradient_for_url (string? url) { 79 if (url == null) { 80 return null; 81 } 82 83 string tag_id = get_id_from_url ((!) url); 84 return get_gradient_for_id (tag_id); 85 } 86 87 public Gradient? get_gradient_for_id (string id) { 88 string tag_id; 89 90 if (id.has_prefix ("#")) { 91 tag_id = id.substring ("#".length); 92 } else { 93 tag_id = id; 94 } 95 96 foreach (Gradient gradient in gradients) { 97 if (gradient.id == tag_id) { 98 return gradient; 99 } 100 } 101 102 return null; 103 } 104 105 public static bool is_url (string? attribute) { 106 if (attribute == null) { 107 return false; 108 } 109 110 return ((!) attribute).has_prefix ("url"); 111 } 112 113 public Defs shallow_copy () { 114 Defs d = new Defs (); 115 116 foreach (Gradient g in gradients) { 117 d.add (g); 118 } 119 120 d.style_sheet = style_sheet.shallow_copy (); 121 122 return d; 123 } 124 125 public Defs copy () { 126 Defs d = new Defs (); 127 128 foreach (Gradient g in gradients) { 129 d.add (g.copy ()); 130 } 131 132 d.style_sheet = style_sheet.copy (); 133 134 return d; 135 } 136 } 137 138 } 139