The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

SvgComponent.vala in birdui

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 birdui/SvgComponent.vala.
Merge branch '2.x' of github.com:johanmattssonm/birdfont
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 SvgBird; 17 using Gee; 18 using Cairo; 19 20 namespace Bird { 21 22 class SvgComponent : Component { 23 string? path = null; 24 string file_name = ""; 25 SvgDrawing? drawing = null; 26 27 public SvgComponent (XmlElement svg_component_tag, Defs defs, string svg_file) { 28 base.embedded (svg_component_tag, defs); 29 load_svg (svg_file); 30 } 31 32 public SvgComponent.for_file (string svg_file) { 33 base.embedded (new XmlElement.empty (), new Defs ()); 34 load_svg (svg_file); 35 } 36 37 public override void get_min_size (out double min_width, out double min_height) { 38 min_width = 0; 39 min_height = 0; 40 41 if (drawing != null) { 42 SvgDrawing svg = (!) drawing; 43 min_width = svg.width + get_padding_left () + get_padding_right (); 44 min_height = svg.height + get_padding_bottom () + get_padding_top (); 45 } 46 } 47 48 public override void layout (double parent_width, double parent_height) { 49 if (unlikely (components.size > 0)) { 50 warning ("SVG files can not have subviews."); 51 } 52 53 if (drawing != null) { 54 SvgDrawing svg = (!) drawing; 55 width = svg.width; 56 height = svg.height; 57 } 58 } 59 60 public override string to_string () { 61 return "Svg: " + file_name; 62 } 63 64 private void load_svg (string file_name) { 65 this.file_name = file_name; 66 path = find_file (file_name); 67 68 if (path == null) { 69 warning (file_name + " not found."); 70 return; 71 } 72 73 string xml_data; 74 File svg_file = File.new_for_path ((!) path); 75 try { 76 FileUtils.get_contents((!) svg_file.get_path (), out xml_data); 77 } catch (GLib.Error error) { 78 warning (error.message); 79 return; 80 } 81 82 SvgFile svg_parser = new SvgFile (); 83 drawing = svg_parser.parse_svg_data (xml_data); 84 } 85 86 public override void draw (Context cairo) { 87 cairo.save (); 88 cairo.translate (padded_x, padded_y); 89 clip (cairo); 90 91 if (drawing != null) { 92 SvgDrawing svg = (!) drawing; 93 svg.draw (cairo); 94 } 95 96 cairo.restore (); 97 } 98 99 } 100 101 } 102 103