The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Layer.vala in libbirdfont

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 libbirdfont/Layer.vala.
Linear gradients and matrix tranform on gradients
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 BirdFont { 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 bool is_counter = false; 25 public Gradient? gradient = null; 26 public bool single_path = false; 27 28 public Gee.ArrayList<SvgTransform> transforms; 29 30 public Layer () { 31 objects = new ObjectGroup (); 32 subgroups = new Gee.ArrayList<Layer> (); 33 transforms = new Gee.ArrayList<SvgTransform> (); 34 } 35 36 public int index_of (Layer sublayer) { 37 return subgroups.index_of (sublayer); 38 } 39 40 public ObjectGroup get_all_objects () { 41 ObjectGroup o = new ObjectGroup (); 42 43 o.append (objects); 44 45 foreach (Layer sublayer in subgroups) { 46 o.append (sublayer.get_all_objects ()); 47 } 48 49 return o; 50 } 51 52 public PathList get_all_paths () { 53 PathList paths = new PathList (); 54 55 foreach (Object o in objects) { 56 if (o is PathObject) { 57 PathObject p = (PathObject) o; 58 paths.add (p.get_path ()); 59 } 60 } 61 62 foreach (Layer sublayer in subgroups) { 63 paths.append (sublayer.get_all_paths ()); 64 } 65 66 return paths; 67 } 68 69 public ObjectGroup get_visible_objects () { 70 ObjectGroup object_group = new ObjectGroup (); 71 72 if (visible) { 73 foreach (Object o in objects) { 74 object_group.add (o); 75 } 76 } 77 78 foreach (Layer sublayer in subgroups) { 79 if (sublayer.visible) { 80 object_group.append (sublayer.get_visible_objects ()); 81 } 82 } 83 84 return object_group; 85 } 86 87 public PathList get_visible_paths () { 88 PathList paths = new PathList (); 89 90 if (visible) { 91 foreach (Object o in objects) { 92 if (o is PathObject) { 93 PathObject p = (PathObject) o; 94 paths.add (p.get_path ()); 95 } 96 } 97 } 98 99 foreach (Layer sublayer in subgroups) { 100 if (sublayer.visible) { 101 paths.append (sublayer.get_visible_paths ()); 102 } 103 } 104 105 return paths; 106 } 107 108 public void add_layer (Layer layer) { 109 subgroups.add (layer); 110 } 111 112 public void add_path (Path path) { 113 PathObject p = new PathObject.for_path (path); 114 objects.add (p); 115 } 116 117 public void add_object (Object object) { 118 objects.add (object); 119 } 120 121 public void append_paths (PathList path_list) { 122 foreach (Path p in path_list.paths) { 123 add_path (p); 124 } 125 } 126 127 private PathObject? get_fast_path (Path path) { 128 foreach (Object o in objects) { 129 if (o is PathObject) { 130 PathObject p = (PathObject) o; 131 if (p.get_path () == path) { 132 return p; 133 } 134 } 135 } 136 137 return null; 138 } 139 140 public void remove_path (Path path) { 141 PathObject? p = get_fast_path (path); 142 143 if (p != null) { 144 objects.remove ((!) p); 145 } 146 147 foreach (Layer sublayer in subgroups) { 148 sublayer.remove_path (path); 149 } 150 } 151 152 public void remove (Object o) { 153 objects.remove (o); 154 } 155 156 public void remove_layer (Layer layer) { 157 subgroups.remove (layer); 158 159 foreach (Layer sublayer in subgroups) { 160 sublayer.remove_layer (layer); 161 } 162 } 163 164 public Layer copy () { 165 Layer layer = new Layer (); 166 167 layer.name = name; 168 layer.objects = objects.copy (); 169 layer.visible = visible; 170 171 foreach (Layer l in subgroups) { 172 layer.subgroups.add (l.copy ()); 173 } 174 175 if (gradient != null) { 176 layer.gradient = ((!) gradient).copy (); 177 } 178 179 layer.single_path = single_path; 180 181 return layer; 182 } 183 184 public void get_boundaries (out double x, out double y, out double w, out double h) { 185 double px, py, px2, py2; 186 187 px = Glyph.CANVAS_MAX; 188 py = Glyph.CANVAS_MAX; 189 px2 = Glyph.CANVAS_MIN; 190 py2 = Glyph.CANVAS_MIN; 191 192 foreach (Object p in get_all_objects ().objects) { 193 if (px > p.xmin) { 194 px = p.xmin; 195 } 196 197 if (py > p.ymin) { 198 py = p.ymin; 199 } 200 201 if (px2 < p.xmax) { 202 px2 = p.xmax; 203 } 204 205 if (py2 < p.ymax) { 206 py2 = p.ymax; 207 } 208 } 209 210 w = px2 - px; 211 h = py2 - py; 212 x = px; 213 y = py2; 214 } 215 216 public void print (int indent = 0) { 217 stdout.printf (@"Layer: $(name)"); 218 219 if (!visible) { 220 stdout.printf (" hidden"); 221 } 222 223 stdout.printf (@"\n"); 224 225 foreach (Object o in objects) { 226 for (int i = 0; i < indent; i++) { 227 stdout.printf ("\t"); 228 } 229 stdout.printf (@"Object $(o.to_string ())"); 230 231 if (o.color != null) { 232 stdout.printf (" %s", ((!) o.color).to_rgb_hex ()); 233 } 234 235 if (!o.visible) { 236 stdout.printf (" hidden"); 237 } 238 239 stdout.printf ("\n"); 240 } 241 242 foreach (Layer l in subgroups) { 243 for (int i = 0; i < indent; i++) { 244 stdout.printf ("\t"); 245 } 246 stdout.printf ("%s\n", l.name); 247 l.print (indent + 1); 248 } 249 } 250 251 public PathList get_paths_in_layer () { 252 PathList paths = new PathList (); 253 254 foreach (Object object in objects) { 255 if (object is PathObject) { 256 paths.add (((PathObject) object).get_path ()); 257 } 258 } 259 260 return paths; 261 } 262 } 263 264 } 265