The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

SvgTransform.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/SvgTransform.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 B; 16 using Cairo; 17 18 namespace SvgBird { 19 20 public enum TransformType { 21 NONE, 22 TRANSLATE, 23 MATRIX, 24 SCALE, 25 ROTATE 26 } 27 28 public class SvgTransform : GLib.Object { 29 public TransformType type = TransformType.NONE; 30 public Doubles arguments = new Doubles.for_capacity (10); 31 32 public SvgTransform () { 33 } 34 35 public SvgTransform.for_matrix (Matrix matrix) { 36 type = TransformType.MATRIX; 37 38 arguments.add (matrix.xx); 39 arguments.add (matrix.yx); 40 arguments.add (matrix.xy); 41 arguments.add (matrix.yy); 42 arguments.add (matrix.x0); 43 arguments.add (matrix.y0); 44 } 45 46 public SvgTransform copy () { 47 SvgTransform transform = new SvgTransform (); 48 transform.type = type; 49 transform.arguments = arguments.copy (); 50 return transform; 51 } 52 53 public string to_string () { 54 StringBuilder sb = new StringBuilder (); 55 56 sb.append (@"$type"); 57 sb.append (" "); 58 59 for (int i = 0; i < arguments.size; i++) { 60 sb.append (@"$(arguments.get_double (i)) "); 61 } 62 63 return sb.str; 64 } 65 66 public Matrix get_matrix () { 67 Matrix matrix; 68 69 matrix = Matrix.identity (); 70 71 if (type == TransformType.ROTATE) { 72 if (arguments.size == 1) { 73 double r = arguments.get_double (0); 74 r *= Math.PI / 180; 75 matrix.rotate (r); 76 return matrix; 77 } 78 } else if (type == TransformType.SCALE) { 79 if (arguments.size == 1) { 80 double s = arguments.get_double (0); 81 matrix.scale (s, s); 82 return matrix; 83 } else if (arguments.size == 2) { 84 double s0 = arguments.get_double (0); 85 double s1 = arguments.get_double (1); 86 matrix.scale (s0, s1); 87 } 88 } else if (type == TransformType.TRANSLATE) { 89 if (arguments.size == 1) { 90 double s = arguments.get_double (0); 91 matrix.translate (s, 0); 92 } else if (arguments.size == 2) { 93 double s0 = arguments.get_double (0); 94 double s1 = arguments.get_double (1); 95 matrix.translate (s0, s1); 96 } 97 } else if (type == TransformType.MATRIX) { 98 if (arguments.size == 6) { 99 double s0 = arguments.get_double (0); 100 double s1 = arguments.get_double (1); 101 double s2 = arguments.get_double (2); 102 double s3 = arguments.get_double (3); 103 double s4 = arguments.get_double (4); 104 double s5 = arguments.get_double (5); 105 106 matrix = Matrix (s0, s1, s2, s3, s4, s5); 107 } 108 } 109 110 return matrix; 111 } 112 113 public string get_xml () { 114 if (type == TransformType.SCALE) { 115 if (arguments.size == 1) { 116 string s = arguments.get_string (0); 117 return @"scale ($s)"; 118 } else if (arguments.size == 2) { 119 string s0 = arguments.get_string (0); 120 string s1 = arguments.get_string (1); 121 return @"scale ($s0, $s1)"; 122 } 123 } else if (type == TransformType.TRANSLATE) { 124 if (arguments.size == 1) { 125 string s = arguments.get_string (0); 126 return @"translate ($s)"; 127 } else if (arguments.size == 2) { 128 string s0 = arguments.get_string (0); 129 string s1 = arguments.get_string (1); 130 return @"translate ($s0, $s1)"; 131 } 132 } else if (type == TransformType.MATRIX) { 133 if (arguments.size == 6) { 134 string s0 = arguments.get_string (0); 135 string s1 = arguments.get_string (1); 136 string s2 = arguments.get_string (2); 137 string s3 = arguments.get_string (3); 138 string s4 = arguments.get_string (4); 139 string s5 = arguments.get_string (5); 140 return @"matrix ($s0, $s1, $s2, $s3, $s4, $s5)"; 141 } 142 } 143 144 warning ("Invalid transform."); 145 146 return ""; 147 } 148 } 149 150 } 151