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