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.
SVG boundaries for ellipses
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 = boundaries_height - value; 80 } 81 } 82 83 public double ymax { 84 get { 85 return -top; 86 } 87 88 set { 89 top = -value; 90 } 91 } 92 93 // FIXME: DELETE 94 public virtual double rotation { get; set; } 95 public virtual double stroke { get; set; } 96 public virtual LineCap line_cap { get; set; default = LineCap.BUTT; } 97 public virtual bool fill { get; set; } 98 99 public const double CANVAS_MAX = 100000; 100 public const double CANVAS_MIN = -100000; 101 102 public Object () { 103 } 104 105 public Object.create_copy (Object o) { 106 open = o.open; 107 } 108 109 public void set_open (bool open) { 110 this.open = open; 111 } 112 113 public bool is_open () { 114 return open; 115 } 116 117 public abstract void update_region_boundaries (); 118 public abstract bool is_over (double x, double y); 119 public abstract void draw_outline (Context cr); 120 121 public abstract Object copy (); 122 public abstract void move (double dx, double dy); 123 public abstract void rotate (double theta, double xc, double yc); 124 public abstract bool is_empty (); 125 public abstract void resize (double ratio_x, double ratio_y); 126 127 public static void copy_attributes (Object from, Object to) { 128 to.open = from.open; 129 130 if (from.color != null) { 131 to.color = ((!) from.color).copy (); 132 } else { 133 to.color = null; 134 } 135 136 if (from.stroke_color != null) { 137 to.stroke_color = ((!) from.stroke_color).copy (); 138 } else { 139 to.stroke_color = null; 140 } 141 142 if (to.gradient != null) { 143 to.gradient = ((!) from.gradient).copy (); 144 } else { 145 to.gradient = null; 146 } 147 148 to.xmax = from.xmax; 149 to.xmin = from.xmin; 150 to.ymax = from.ymax; 151 to.ymin = from.ymin; 152 153 to.rotation = from.rotation; 154 to.stroke = from.stroke; 155 to.line_cap = from.line_cap; 156 to.fill = from.fill; 157 158 to.style = from.style.copy (); 159 to.transforms = from.transforms.copy (); 160 161 if (from.clip_path != null) { 162 to.clip_path = ((!) from.clip_path).copy (); 163 } 164 } 165 166 public virtual string to_string () { 167 return "Object"; 168 } 169 170 public void paint (Context cr) { 171 Color fill, stroke; 172 bool need_fill = style.fill_gradient != null || style.fill != null; 173 bool need_stroke = style.stroke_gradient != null || style.stroke != null; 174 175 cr.set_line_width (style.stroke_width); 176 177 if (style.fill_gradient != null) { 178 apply_gradient (cr, (!) style.fill_gradient); 179 } else if (style.fill != null) { 180 fill = (!) style.fill; 181 cr.set_source_rgba (fill.r, fill.g, fill.b, fill.a); 182 } 183 184 if (need_fill) { 185 if (need_stroke) { 186 cr.fill_preserve (); 187 } else { 188 cr.fill (); 189 } 190 } 191 192 if (style.stroke_gradient != null) { 193 apply_gradient (cr, (!) style.stroke_gradient); 194 } else if (style.stroke != null) { 195 stroke = (!) style.stroke; 196 cr.set_source_rgba (stroke.r, stroke.g, stroke.b, stroke.a); 197 } 198 199 if (need_stroke) { 200 cr.stroke (); 201 } 202 } 203 204 public void apply_gradient (Context cr, Gradient? gradient) { 205 Cairo.Pattern pattern; 206 Gradient g; 207 208 if (gradient != null) { 209 g = (!) gradient; 210 211 pattern = new Cairo.Pattern.linear (g.x1, g.y1, g.x2, g.y2); 212 213 Matrix gradient_matrix = g.get_matrix (); 214 gradient_matrix.invert (); 215 pattern.set_matrix (gradient_matrix); 216 217 foreach (Stop s in g.stops) { 218 Color c = s.color; 219 pattern.add_color_stop_rgba (s.offset, c.r, c.g, c.b, c.a); 220 } 221 222 cr.set_source (pattern); 223 } 224 } 225 226 public void apply_transform (Context cr) { 227 Matrix view_matrix = cr.get_matrix (); 228 Matrix object_matrix = transforms.get_matrix (); 229 230 object_matrix.multiply (object_matrix, view_matrix); 231 cr.set_matrix (object_matrix); 232 } 233 234 public virtual void update_boundaries () { 235 } 236 } 237 238 } 239