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.
Rotate paths
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 Gee.ArrayList<SvgTransform> transforms; 22 23 public double x = 0; 24 public double y = 0; 25 public double rotation = 0; 26 public double scale_x = 1; 27 public double scale_y = 1; 28 29 public SvgTransforms () { 30 transforms = new Gee.ArrayList<SvgTransform> (); 31 rotation_matrix = Matrix.identity (); 32 } 33 34 public void collapse_transforms () { 35 SvgTransform transform = new SvgTransform.for_matrix (rotation_matrix); 36 add (transform); 37 38 rotation_matrix = Matrix.identity (); 39 rotation = 0; 40 41 Matrix collapsed = get_matrix (); 42 SvgTransform transform_transform = new SvgTransform.for_matrix (collapsed); 43 clear (); 44 add (transform_transform); 45 } 46 47 public void clear () { 48 transforms.clear (); 49 rotation_matrix = Matrix.identity (); 50 rotation = 0; 51 } 52 53 public void rotate (double theta, double x, double y) { 54 rotation += theta; 55 56 while (rotation > 2 * Math.PI) { 57 rotation -= 2 * Math.PI; 58 } 59 60 while (rotation < -2 * Math.PI) { 61 rotation += 2 * Math.PI; 62 } 63 64 rotation_matrix = Matrix.identity (); 65 rotation_matrix.translate (x, y); 66 rotation_matrix.rotate (rotation); 67 rotation_matrix.translate (-x, -y); 68 } 69 70 public void resize (double x, double y) { 71 scale_x += x; 72 scale_y += y; 73 } 74 75 public SvgTransforms copy () { 76 SvgTransforms copy_transforms = new SvgTransforms (); 77 78 foreach (SvgTransform t in transforms) { 79 copy_transforms.add (t.copy ()); 80 } 81 82 return copy_transforms; 83 } 84 85 public void add (SvgTransform transform) { 86 transforms.add (transform); 87 } 88 89 public Matrix get_matrix () { 90 Matrix transformation_matrix = Matrix.identity (); 91 92 for (int i = 0; i < transforms.size; i++) { 93 Matrix part = transforms.get (i).get_matrix (); 94 transformation_matrix.multiply (transformation_matrix, part); 95 } 96 97 transformation_matrix.multiply (transformation_matrix, rotation_matrix); 98 99 return transformation_matrix; 100 } 101 102 public string to_string () { 103 StringBuilder sb = new StringBuilder (); 104 105 foreach (SvgTransform t in transforms) { 106 sb.append (t.to_string ()); 107 sb.append (" "); 108 } 109 110 return sb.str; 111 } 112 } 113 114 } 115