The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Object.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/Object.vala.
Store boundaries in SVG coordinates
1 /* 2 Copyright (C) 2016 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 abstract class Object : GLib.Object { 21 bool open = false; 22 23 public bool visible = true; 24 public SvgStyle style = new SvgStyle (); 25 public SvgTransforms transforms = new SvgTransforms (); 26 public ClipPath? clip_path = null; 27 public string id = ""; 28 public string css_class = ""; 29 30 public virtual Color? color { get; set; } // FIXME: keep this in svg style 31 public virtual Color? stroke_color { get; set; } 32 public virtual Gradient? gradient { get; set; } 33 34 /** Path boundaries */ 35 public virtual double left { get; set; } 36 public virtual double right { get; set; } 37 public virtual double top { get; set; } 38 public virtual double bottom { get; set; } 39 40 public virtual double boundaries_height { 41 get { 42 return bottom - top; 43 } 44 } 45 46 public virtual double boundaries_width { 47 get { 48 return right - left; 49 } 50 } 51 52 /** Cartesian coordinates for the old BirdFont system. */ 53 public double xmax { 54 get { 55 return right; 56 } 57 58 set { 59 right = value; 60 } 61 } 62 63 public double xmin { 64 get { 65 return left; 66 } 67 68 set { 69 left = value; 70 } 71 } 72 73 public double ymin { 74 get { 75 return top - boundaries_height; 76 } 77 78 set { 79 top = value + boundaries_height; 80 } 81 } 82 83 public double ymax { 84 get { 85 return top; 86 } 87 88 set { 89 top = value; 90 } 91 } 92 93 public virtual double rotation { get; set; } 94 public virtual double stroke { get; set; } 95 public virtual LineCap line_cap { get; set; default = LineCap.BUTT; } 96 public virtual bool fill { get; set; } 97 98 public Object () { 99 } 100 101 public Object.create_copy (Object o) { 102 open = o.open; 103 } 104 105 public void set_open (bool open) { 106 this.open = open; 107 } 108 109 public bool is_open () { 110 return open; 111 } 112 113 public abstract void update_region_boundaries (); 114 public abstract bool is_over (double x, double y); 115 public abstract void draw_outline (Context cr); 116 117 public abstract Object copy (); 118 public abstract void move (double dx, double dy); 119 public abstract void rotate (double theta, double xc, double yc); 120 public abstract bool is_empty (); 121 public abstract void resize (double ratio_x, double ratio_y); 122 123 public static void copy_attributes (Object from, Object to) { 124 to.open = from.open; 125 126 if (from.color != null) { 127 to.color = ((!) from.color).copy (); 128 } else { 129 to.color = null; 130 } 131 132 if (from.stroke_color != null) { 133 to.stroke_color = ((!) from.stroke_color).copy (); 134 } else { 135 to.stroke_color = null; 136 } 137 138 if (to.gradient != null) { 139 to.gradient = ((!) from.gradient).copy (); 140 } else { 141 to.gradient = null; 142 } 143 144 to.xmax = from.xmax; 145 to.xmin = from.xmin; 146 to.ymax = from.ymax; 147 to.ymin = from.ymin; 148 149 to.rotation = from.rotation; 150 to.stroke = from.stroke; 151 to.line_cap = from.line_cap; 152 to.fill = from.fill; 153 154 to.style = from.style.copy (); 155 to.transforms = from.transforms.copy (); 156 157 if (from.clip_path != null) { 158 to.clip_path = ((!) from.clip_path).copy (); 159 } 160 } 161 162 public virtual string to_string () { 163 return "Object"; 164 } 165 166 public void paint (Context cr) { 167 Color fill, stroke; 168 bool need_fill = style.fill_gradient != null || style.fill != null; 169 bool need_stroke = style.stroke_gradient != null || style.stroke != null; 170 171 cr.set_line_width (style.stroke_width); 172 173 if (style.fill_gradient != null) { 174 apply_gradient (cr, (!) style.fill_gradient); 175 } else if (style.fill != null) { 176 fill = (!) style.fill; 177 cr.set_source_rgba (fill.r, fill.g, fill.b, fill.a); 178 } 179 180 if (need_fill) { 181 if (need_stroke) { 182 cr.fill_preserve (); 183 } else { 184 cr.fill (); 185 } 186 } 187 188 if (style.stroke_gradient != null) { 189 apply_gradient (cr, (!) style.stroke_gradient); 190 } else if (style.stroke != null) { 191 stroke = (!) style.stroke; 192 cr.set_source_rgba (stroke.r, stroke.g, stroke.b, stroke.a); 193 } 194 195 if (need_stroke) { 196 cr.stroke (); 197 } 198 } 199 200 public void apply_gradient (Context cr, Gradient? gradient) { 201 Cairo.Pattern pattern; 202 Gradient g; 203 204 if (gradient != null) { 205 g = (!) gradient; 206 207 pattern = new Cairo.Pattern.linear (g.x1, g.y1, g.x2, g.y2); 208 209 Matrix gradient_matrix = g.get_matrix (); 210 gradient_matrix.invert (); 211 pattern.set_matrix (gradient_matrix); 212 213 foreach (Stop s in g.stops) { 214 Color c = s.color; 215 pattern.add_color_stop_rgba (s.offset, c.r, c.g, c.b, c.a); 216 } 217 218 cr.set_source (pattern); 219 } 220 } 221 222 public void apply_transform (Context cr) { 223 Matrix view_matrix = cr.get_matrix (); 224 Matrix object_matrix = transforms.get_matrix (); 225 226 object_matrix.multiply (object_matrix, view_matrix); 227 cr.set_matrix (object_matrix); 228 } 229 230 } 231 232 } 233