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