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.
SVG M instruction
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 using Cairo; 16 using Math; 17 18 namespace SvgBird { 19 20 public class Layer : Object { 21 public ObjectGroup objects; 22 public string name = "Layer"; 23 24 public Layer () { 25 objects = new ObjectGroup (); 26 transforms = new SvgTransforms (); 27 } 28 29 public Layer.with_name (string name) { 30 this.name = name; 31 objects = new ObjectGroup (); 32 transforms = new SvgTransforms (); 33 } 34 35 36 public override bool update_boundaries (Context cr) { 37 if (objects.size == 0) { 38 return false; 39 } 40 41 top = CANVAS_MAX; 42 bottom = CANVAS_MIN; 43 left = CANVAS_MAX; 44 right = CANVAS_MIN; 45 46 cr.save (); 47 apply_transform (cr); 48 49 foreach (Object object in objects) { 50 bool has_size = false; 51 52 cr.save (); 53 54 if (object is Layer) { 55 Layer sublayer = (Layer) object; 56 object.view_matrix = cr.get_matrix (); 57 has_size = sublayer.update_boundaries (cr); 58 } else { 59 object.apply_transform (cr); 60 object.view_matrix = cr.get_matrix (); 61 has_size = object.update_boundaries (cr); 62 } 63 64 if (has_size) { 65 left = fmin (left, object.left); 66 right = fmax (right, object.right); 67 top = fmin (top, object.top); 68 bottom = fmax (bottom, object.bottom); 69 } 70 71 cr.restore (); 72 } 73 74 cr.restore (); 75 76 return boundaries_width != 0; 77 } 78 79 public void draw (Context cr) { 80 draw_layer (cr, true); 81 } 82 83 public override void draw_outline (Context cr) { 84 draw_layer (cr, false); 85 } 86 87 private void draw_layer (Context cr, bool paint) { 88 cr.save (); 89 apply_transform (cr); 90 91 if (clip_path != null) { 92 ClipPath clipping = (!) clip_path; 93 clipping.apply (cr); 94 } 95 96 foreach (Object object in objects) { 97 cr.save (); 98 99 if (object.clip_path != null) { 100 ClipPath clipping = (!) object.clip_path; 101 clipping.apply (cr); 102 } 103 104 if (object is Layer) { 105 Layer sublayer = (Layer) object; 106 107 if (paint) { 108 sublayer.draw (cr); 109 } else { 110 sublayer.draw_outline (cr); 111 } 112 } else if (object.visible) { 113 object.apply_transform (cr); 114 object.draw_outline (cr); 115 116 if (paint) { 117 object.paint (cr); 118 } 119 } 120 cr.restore (); 121 } 122 123 cr.restore (); 124 } 125 126 public int index_of (Layer sublayer) { 127 return objects.index_of (sublayer); 128 } 129 130 public void add_layer (Layer layer) { 131 objects.add (layer); 132 update_boundaries_for_object (); 133 } 134 135 public void add_object (Object object) { 136 objects.add (object); 137 update_boundaries_for_object (); 138 } 139 140 public void remove (Object o) { 141 objects.remove (o); 142 143 foreach (Object object in objects) { 144 if (object is Layer) { 145 Layer sublayer = (Layer) object; 146 sublayer.remove (o);; 147 } 148 } 149 150 update_boundaries_for_object (); 151 } 152 153 public void remove_layer (Layer layer) { 154 objects.remove (layer); 155 156 foreach (Object object in objects) { 157 if (object is Layer) { 158 Layer sublayer = (Layer) object; 159 sublayer.remove_layer (layer); 160 } 161 } 162 163 update_boundaries_for_object (); 164 } 165 166 public static void copy_layer (Layer from, Layer to) { 167 to.name = from.name; 168 to.objects = from.objects.copy (); 169 } 170 171 public override Object copy () { 172 Layer layer = new Layer (); 173 copy_layer (this, layer); 174 Object.copy_attributes (this, layer); 175 return layer; 176 } 177 178 public Gee.ArrayList<Layer> get_sublayers () { 179 Gee.ArrayList<Layer> sublayers = new Gee.ArrayList<Layer> (); 180 181 foreach (Object object in objects) { 182 if (likely (object is Layer)) { 183 Layer sublayer = (Layer) object; 184 sublayers.add (sublayer); 185 } else { 186 warning ("An object in the group " + name + " is not a layer."); 187 } 188 } 189 190 return sublayers; 191 } 192 193 public void print (int indent = 0) { 194 stdout.printf (@"Layer: $(name)"); 195 196 if (!visible) { 197 stdout.printf (" hidden"); 198 } 199 200 stdout.printf (@" $(transforms) $(style)"); 201 stdout.printf (@"\n"); 202 203 foreach (Object object in objects) { 204 stdout.printf (@"$(object.to_string ()) $(object.transforms) $(object.style)"); 205 206 if (!object.visible) { 207 stdout.printf (" hidden"); 208 } 209 210 stdout.printf ("\n"); 211 212 for (int i = 0; i < indent; i++) { 213 stdout.printf ("\t"); 214 } 215 216 if (object is Layer) { 217 Layer sublayer = (Layer) object; 218 sublayer.print (indent + 1); 219 } 220 } 221 } 222 223 public override bool is_over (double x, double y) { 224 return false; 225 } 226 227 public override void move (double dx, double dy) { 228 } 229 230 public override bool is_empty () { 231 return false; 232 } 233 234 public override string to_string () { 235 return "Layer: " + name; 236 } 237 } 238 239 } 240