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.
Move the SVG parser and the SVG rendering code to libsvgbird
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<Gradient> gradients = new Gee.ArrayList<Gradient> (); 22 23 public void add (Gradient g) { 24 gradients.add (g); 25 } 26 27 public Gradient? get_gradient_for_url (string? url) { 28 if (url == null) { 29 return null; 30 } 31 32 string tag_id = (!) url; 33 34 if (unlikely (!is_url (tag_id))) { 35 warning ("Not an URL: " + tag_id); 36 return null; 37 } 38 39 int p1 = tag_id.index_of ("("); 40 if (unlikely (p1 == -1)) { 41 warning ("Not an URL: " + tag_id); 42 return null; 43 } 44 45 int p2 = tag_id.index_of (")"); 46 if (unlikely (p2 == -1 || p2 < p1)) { 47 warning ("Not an URL: " + tag_id); 48 return null; 49 } 50 51 p1 += "(".length; 52 int length = p2 - p1; 53 tag_id = tag_id.substring (p1, length); 54 55 return get_gradient_for_id (tag_id); 56 } 57 58 public Gradient? get_gradient_for_id (string id) { 59 string tag_id; 60 61 if (id.has_prefix ("#")) { 62 tag_id = id.substring ("#".length); 63 } else { 64 tag_id = id; 65 } 66 67 foreach (Gradient gradient in gradients) { 68 if (gradient.id == tag_id) { 69 return gradient; 70 } 71 } 72 73 return null; 74 } 75 76 public static bool is_url (string? attribute) { 77 if (attribute == null) { 78 return false; 79 } 80 81 return ((!) attribute).has_prefix ("url"); 82 } 83 84 public Defs copy () { 85 Defs d = new Defs (); 86 87 foreach (Gradient g in gradients) { 88 d.add (g); 89 } 90 91 return d; 92 } 93 94 } 95 96 } 97