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