The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

EmbeddedSvg.vala in libbirdfont

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
Circle boundaries heads/master
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 Math; 17 using Cairo; 18 using SvgBird; 19 20 namespace BirdFont { 21 22 public class EmbeddedSvg : SvgBird.Object { 23 public string svg_data = ""; 24 25 // the view matrix belongs to the drawing 26 public SvgDrawing drawing = new SvgDrawing (); 27 28 public double x; 29 public double y; 30 31 public EmbeddedSvg (SvgDrawing drawing) { 32 this.drawing = drawing; 33 } 34 35 public override bool update_boundaries (Context context) { 36 drawing.update_boundaries (context); 37 38 left = x + drawing.left; 39 right = x + drawing.right; 40 top = -y + drawing.top; 41 bottom = -y + drawing.bottom; 42 43 return true; 44 } 45 46 public override bool is_over (double x, double y) { 47 return (xmin <= x <= xmax) 48 && (ymin <= y <= ymax); 49 } 50 51 public void draw_embedded_svg (Context cr) { 52 cr.save (); 53 cr.translate (Glyph.xc () + x, Glyph.yc () - y); 54 apply_transform (cr); 55 drawing.draw (cr); 56 cr.restore (); 57 } 58 59 public override void draw_outline (Context cr) { 60 drawing.draw_outline (cr); 61 } 62 63 public override void move (double dx, double dy) { 64 x += dx; 65 y += dy; 66 move_bounding_box (dx, -dy); 67 } 68 69 public override bool is_empty () { 70 return drawing.is_empty (); 71 } 72 73 public override SvgBird.Object copy () { 74 EmbeddedSvg svg = new EmbeddedSvg ((SvgDrawing) drawing.copy ()); 75 SvgBird.Object.copy_attributes (this, svg); 76 svg.svg_data = svg_data; 77 svg.x = x; 78 svg.y = y; 79 return svg; 80 } 81 82 public override string to_string () { 83 return "Embedded SVG"; 84 } 85 86 public string get_transformed_svg_data () { 87 StringBuilder svg = new StringBuilder (); 88 89 svg.append ("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"); 90 svg.append ("""<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">"""); 91 svg.append ("\n"); 92 93 string transforms = drawing.transforms.get_xml (); 94 95 if (transforms != "") { 96 svg.append ("<g"); 97 svg.append (@" transform=\"$(transforms)\""); 98 svg.append (">\n"); 99 } 100 101 svg.append (remove_xml_header (svg_data)); 102 103 if (transforms != "") { 104 svg.append ("</g>\n"); 105 } 106 107 svg.append ("</svg>\n"); 108 109 return svg.str; 110 } 111 112 public string remove_xml_header (string xml_data) { 113 string xml = xml_data; 114 115 int start = xml.index_of ("<?"); 116 while (start > -1) { 117 int end = xml.index_of ("?>"); 118 119 if (end == -1) { 120 return xml; 121 } 122 123 end += "?>".length; 124 125 xml = xml.substring (0, start) + xml.substring (end); 126 start = xml.index_of ("<?"); 127 } 128 129 return xml; 130 } 131 } 132 133 } 134