The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Layer.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/Layer.vala.
Move the SVG parser and the SVG rendering code to libsvgbird
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 namespace SvgBird { 16 17 public class Layer : GLib.Object { 18 public ObjectGroup objects; 19 20 public Gee.ArrayList<Layer> subgroups; 21 public bool visible = true; 22 public string name = "Layer"; 23 24 public SvgTransforms transforms; 25 26 public Layer () { 27 objects = new ObjectGroup (); 28 subgroups = new Gee.ArrayList<Layer> (); 29 transforms = new SvgTransforms (); 30 } 31 32 public int index_of (Layer sublayer) { 33 return subgroups.index_of (sublayer); 34 } 35 36 public ObjectGroup get_all_objects () { 37 ObjectGroup o = new ObjectGroup (); 38 39 o.append (objects); 40 41 foreach (Layer sublayer in subgroups) { 42 o.append (sublayer.get_all_objects ()); 43 } 44 45 return o; 46 } 47 48 public ObjectGroup get_visible_objects () { 49 ObjectGroup object_group = new ObjectGroup (); 50 51 if (visible) { 52 foreach (Object o in objects) { 53 object_group.add (o); 54 } 55 } 56 57 foreach (Layer sublayer in subgroups) { 58 if (sublayer.visible) { 59 object_group.append (sublayer.get_visible_objects ()); 60 } 61 } 62 63 return object_group; 64 } 65 66 public void add_layer (Layer layer) { 67 subgroups.add (layer); 68 } 69 70 public void add_object (Object object) { 71 objects.add (object); 72 } 73 74 public void remove (Object o) { 75 objects.remove (o); 76 } 77 78 public void remove_layer (Layer layer) { 79 subgroups.remove (layer); 80 81 foreach (Layer sublayer in subgroups) { 82 sublayer.remove_layer (layer); 83 } 84 } 85 86 public static void copy_layer (Layer from, Layer to) { 87 to.name = from.name; 88 to.objects = from.objects.copy (); 89 to.visible = from.visible; 90 91 foreach (Layer l in from.subgroups) { 92 to.subgroups.add (l.copy ()); 93 } 94 } 95 96 public Layer copy () { 97 Layer layer = new Layer (); 98 copy_layer (this, layer); 99 return layer; 100 } 101 102 public void get_boundaries (out double x, out double y, out double w, out double h) { 103 double px, py, px2, py2; 104 105 px = double.MAX; 106 py = double.MAX; 107 px2 = -double.MAX; 108 py2 = -double.MAX; 109 110 foreach (Object p in get_all_objects ().objects) { 111 if (px > p.xmin) { 112 px = p.xmin; 113 } 114 115 if (py > p.ymin) { 116 py = p.ymin; 117 } 118 119 if (px2 < p.xmax) { 120 px2 = p.xmax; 121 } 122 123 if (py2 < p.ymax) { 124 py2 = p.ymax; 125 } 126 } 127 128 w = px2 - px; 129 h = py2 - py; 130 x = px; 131 y = py2; 132 } 133 134 public void print (int indent = 0) { 135 stdout.printf (@"Layer: $(name)"); 136 137 if (!visible) { 138 stdout.printf (" hidden"); 139 } 140 141 stdout.printf (@"\n"); 142 143 foreach (Object o in objects) { 144 for (int i = 0; i < indent; i++) { 145 stdout.printf ("\t"); 146 } 147 stdout.printf (@"Object $(o.to_string ())"); 148 149 if (o.color != null) { 150 stdout.printf (" %s", ((!) o.color).to_rgb_hex ()); 151 } 152 153 if (!o.visible) { 154 stdout.printf (" hidden"); 155 } 156 157 stdout.printf ("\n"); 158 } 159 160 foreach (Layer l in subgroups) { 161 for (int i = 0; i < indent; i++) { 162 stdout.printf ("\t"); 163 } 164 stdout.printf ("%s\n", l.name); 165 l.print (indent + 1); 166 } 167 } 168 } 169 170 } 171