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