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