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.
Bounding box for SVG rectangles
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 update_boundaries (Matrix.identity ()); 119 } 120 121 public void remove_layer (Layer layer) { 122 objects.remove (layer); 123 124 foreach (Object object in objects) { 125 if (object is Layer) { 126 Layer sublayer = (Layer) object; 127 sublayer.remove_layer (layer); 128 } 129 } 130 131 update_boundaries (Matrix.identity ()); 132 } 133 134 public static void copy_layer (Layer from, Layer to) { 135 to.name = from.name; 136 to.objects = from.objects.copy (); 137 } 138 139 public override Object copy () { 140 Layer layer = new Layer (); 141 copy_layer (this, layer); 142 Object.copy_attributes (this, layer); 143 return layer; 144 } 145 146 public Gee.ArrayList<Layer> get_sublayers () { 147 Gee.ArrayList<Layer> sublayers = new Gee.ArrayList<Layer> (); 148 149 foreach (Object object in objects) { 150 if (likely (object is Layer)) { 151 Layer sublayer = (Layer) object; 152 sublayers.add (sublayer); 153 } else { 154 warning ("An object in the group " + name + " is not a layer."); 155 } 156 } 157 158 return sublayers; 159 } 160 161 public void print (int indent = 0) { 162 stdout.printf (@"Layer: $(name)"); 163 164 if (!visible) { 165 stdout.printf (" hidden"); 166 } 167 168 stdout.printf (@" $(transforms) $(style)"); 169 stdout.printf (@"\n"); 170 171 foreach (Object object in objects) { 172 stdout.printf (@"$(object.to_string ()) $(object.transforms) $(object.style)"); 173 174 if (!object.visible) { 175 stdout.printf (" hidden"); 176 } 177 178 stdout.printf ("\n"); 179 180 for (int i = 0; i < indent; i++) { 181 stdout.printf ("\t"); 182 } 183 184 if (object is Layer) { 185 Layer sublayer = (Layer) object; 186 sublayer.print (indent + 1); 187 } 188 } 189 } 190 191 public override bool is_over (double x, double y) { 192 return false; 193 } 194 195 public override void move (double dx, double dy) { 196 } 197 198 public override void rotate (double theta, double xc, double yc) { 199 } 200 201 public override bool is_empty () { 202 return false; 203 } 204 205 public override void resize (double ratio_x, double ratio_y) { 206 } 207 208 public override string to_string () { 209 return "Layer: " + name; 210 } 211 } 212 213 } 214