The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

SvgTransforms.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/SvgTransforms.vala.
Rotation
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 17 namespace SvgBird { 18 19 public class SvgTransforms : GLib.Object { 20 public Matrix rotation_matrix; 21 public Matrix size_matrix; 22 public Gee.ArrayList<SvgTransform> transforms; 23 24 public double rotation = 0; 25 public double total_rotation = 0; 26 public double translate_x = 0; 27 public double translate_y = 0; 28 29 public SvgTransforms () { 30 transforms = new Gee.ArrayList<SvgTransform> (); 31 rotation_matrix = Matrix.identity (); 32 size_matrix = Matrix.identity (); 33 } 34 35 public double get_rotation () { 36 Matrix m = get_matrix (); 37 double w = 1; 38 double h = 1; 39 m.transform_distance (ref w, ref h); 40 return Math.atan2 (h, w); 41 } 42 43 public void collapse_transforms () { 44 Matrix collapsed = get_matrix (); 45 46 translate_x = 0; 47 translate_y = 0; 48 49 rotation_matrix = Matrix.identity (); 50 rotation = 0; 51 52 size_matrix = Matrix.identity (); 53 54 clear (); 55 56 SvgTransform collapsed_transform = new SvgTransform.for_matrix (collapsed); 57 add (collapsed_transform); 58 } 59 60 public void clear () { 61 transforms.clear (); 62 63 rotation_matrix = Matrix.identity (); 64 rotation = 0; 65 66 size_matrix = Matrix.identity (); 67 68 translate_x = 0; 69 translate_y = 0; 70 } 71 72 public void translate (double x, double y) { 73 translate_x += x; 74 translate_y += y; 75 } 76 77 public void rotate (double theta, double x, double y) { 78 rotation += theta; 79 total_rotation += theta; 80 81 while (rotation > 2 * Math.PI) { 82 rotation -= 2 * Math.PI; 83 } 84 85 while (rotation < -2 * Math.PI) { 86 rotation += 2 * Math.PI; 87 } 88 89 while (total_rotation > 2 * Math.PI) { 90 total_rotation -= 2 * Math.PI; 91 } 92 93 while (total_rotation < -2 * Math.PI) { 94 total_rotation += 2 * Math.PI; 95 } 96 97 rotation_matrix = Matrix.identity (); 98 rotation_matrix.translate (x, y); 99 rotation_matrix.rotate (rotation); 100 rotation_matrix.translate (-x, -y); 101 } 102 103 public void resize (double scale_x, double scale_y, double x, double y) { 104 if (scale_x <= 0 || scale_y <= 0) { 105 return; 106 } 107 108 double x2 = x; 109 double y2 = y; 110 111 size_matrix = Matrix.identity (); 112 size_matrix.scale (scale_x, scale_y); 113 size_matrix.transform_point (ref x2, ref y2); 114 115 double dx = x - x2; 116 double dy = y - y2; 117 118 size_matrix.translate (dx / scale_x, dy / scale_y); 119 } 120 121 public SvgTransforms copy () { 122 SvgTransforms copy_transforms = new SvgTransforms (); 123 124 foreach (SvgTransform t in transforms) { 125 copy_transforms.add (t.copy ()); 126 } 127 128 return copy_transforms; 129 } 130 131 public void add (SvgTransform transform) { 132 transforms.add (transform); 133 } 134 135 public Matrix get_matrix () { 136 Matrix transformation_matrix = Matrix.identity (); 137 138 for (int i = 0; i < transforms.size; i++) { 139 Matrix part = transforms.get (i).get_matrix (); 140 transformation_matrix.multiply (transformation_matrix, part); 141 } 142 143 transformation_matrix.translate (translate_x, translate_y); 144 145 transformation_matrix.multiply (transformation_matrix, rotation_matrix); 146 transformation_matrix.multiply (transformation_matrix, size_matrix); 147 148 return transformation_matrix; 149 } 150 151 public string to_string () { 152 StringBuilder sb = new StringBuilder (); 153 154 foreach (SvgTransform t in transforms) { 155 sb.append (t.to_string ()); 156 sb.append (" "); 157 } 158 159 return sb.str; 160 } 161 162 public string get_xml () { 163 StringBuilder svg = new StringBuilder (); 164 bool first = true; 165 166 foreach (SvgTransform transform in transforms) { 167 if (!first) { 168 svg.append (" "); 169 } 170 171 svg.append (transform.get_xml ()); 172 first = false; 173 } 174 175 return svg.str; 176 } 177 } 178 179 } 180