The Birdfont Source Code


All Repositories / birdfont.git / commit – RSS feed

Parse SVG view box

These changes was commited to the Birdfont repository Sat, 02 Jul 2016 03:24:45 +0000.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git
author Johan Mattsson <johan.mattsson.m@gmail.com>
Sat, 02 Jul 2016 03:24:45 +0000 (05:24 +0200)
committer Johan Mattsson <johan.mattsson.m@gmail.com>
Sat, 02 Jul 2016 03:24:45 +0000 (05:24 +0200)
commit 3336516c6e2e065929a97b767ed70e45e6977dcd
tree deec20a40ec8ccfadf5094f066b855bf9908f0a9
parent 84cb9e1a7daffd7e99363766504dcc78574a61fa
Parse SVG view box

libsvgbird/Object.vala
libsvgbird/SvgDrawing.vala
libsvgbird/SvgFile.vala
libsvgbird/ViewBox.vala [new ]
--- a/libsvgbird/Object.vala +++ b/libsvgbird/Object.vala @@ -192,7 +192,7 @@ } } - public void apply_transform (Context cr) { + public virtual void apply_transform (Context cr) { Matrix view_matrix = cr.get_matrix (); Matrix object_matrix = transforms.get_matrix ();
--- a/libsvgbird/SvgDrawing.vala +++ b/libsvgbird/SvgDrawing.vala @@ -22,6 +22,7 @@ public class SvgDrawing : Object { public Layer root_layer = new Layer (); public Defs defs = new Defs (); + public ViewBox? view_box = null; public double width { get { @@ -60,15 +61,33 @@ public override bool is_over (double x, double y) { return false; + } + + void apply_view_box (Context cr) { + if (view_box != null) { + ViewBox box = (!) view_box; + cr.translate (box.minx, box.miny); + cr.scale (width / box.width, height / box.height); + } + } + + public override void apply_transform (Context cr) { + apply_view_box (cr); + base.apply_transform (cr); } public void draw (Context cr) { + cr.save (); apply_transform (cr); root_layer.draw (cr); + cr.restore (); } public override void draw_outline (Context cr) { + cr.save (); + apply_transform (cr); root_layer.draw_outline (cr); + cr.restore (); } public override Object copy () {
--- a/libsvgbird/SvgFile.vala +++ b/libsvgbird/SvgFile.vala @@ -43,6 +43,10 @@ if (attr.get_name () == "height") { drawing.height = parse_number (attr.get_content ()); + } + + if (attr.get_name () == "viewBox") { + drawing.view_box = parse_view_box (attr.get_content ()); } } @@ -53,6 +57,11 @@ parse_layer (drawing.root_layer, style, t); } + if (name == "svg") { + SvgDrawing embedded = parse_svg_file (t); + drawing.root_layer.add_object (embedded); + } + if (name == "defs") { parse_defs (drawing, t); } @@ -67,6 +76,28 @@ set_object_properties (drawing, new SvgStyle (), svg_tag); return drawing; + } + + ViewBox? parse_view_box (string parameters) { + string arguments = parameters.replace (",", " "); + + while (arguments.index_of (" ") > -1) { + arguments = arguments.replace (" ", " "); + } + + string[] view_box_parameters = arguments.split (" "); + + if (view_box_parameters.length != 4) { + warning ("Expecting four arguments in view box."); + return null; + } + + double minx = parse_number (view_box_parameters[0]); + double miny = parse_number (view_box_parameters[1]); + double width = parse_number (view_box_parameters[2]); + double height = parse_number (view_box_parameters[3]); + + return new ViewBox (minx, miny, width, height); } private void parse_layer (Layer layer, SvgStyle parent_style, XmlElement tag) { @@ -310,6 +341,12 @@ if (name == "line") { parse_line (layer, parent_style, tag); } + + if (name == "svg") { + SvgDrawing embedded = parse_svg_file (tag); + layer.add_object (embedded); + } + } private void parse_polygon (Layer layer, SvgStyle parent_style, XmlElement tag) { @@ -1408,8 +1445,7 @@ return data; } - } }
diff --git libsvgbird/ViewBox.vala(new)
--- /dev/null +++ b/libsvgbird/ViewBox.vala @@ -1,1 +1,39 @@ + /* + Copyright (C) 2016 Johan Mattsson + + This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 3 of the + License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + */ + + + using B; + using Cairo; + using Math; + + namespace SvgBird { + + public class ViewBox : GLib.Object { + + public double minx = 0; + public double miny = 0; + public double width = 0; + public double height = 0; + + public ViewBox (double minx, double miny, double width, double height) { + this.minx = minx; + this.miny = miny; + this.width = width; + this.height = height; + } + + } + + }