|
| 1 | +/**************************************************************************** |
| 2 | +** |
| 3 | +** svgcleaner could help you to clean up your SVG files |
| 4 | +** from unnecessary data. |
| 5 | +** Copyright (C) 2012-2017 Evgeniy Reizner |
| 6 | +** |
| 7 | +** This program is free software; you can redistribute it and/or modify |
| 8 | +** it under the terms of the GNU General Public License as published by |
| 9 | +** the Free Software Foundation; either version 2 of the License, or |
| 10 | +** (at your option) any later version. |
| 11 | +** |
| 12 | +** This program is distributed in the hope that it will be useful, |
| 13 | +** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +** GNU General Public License for more details. |
| 16 | +** |
| 17 | +** You should have received a copy of the GNU General Public License along |
| 18 | +** with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | +** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | +** |
| 21 | +****************************************************************************/ |
| 22 | + |
| 23 | +use super::short::AId; |
| 24 | + |
| 25 | +use svgdom::{Document, AttributeValue}; |
| 26 | + |
| 27 | +use options::Options; |
| 28 | + |
| 29 | +static POW_VEC: &'static [f64] = &[ |
| 30 | + 0.0, |
| 31 | + 10.0, |
| 32 | + 100.0, |
| 33 | + 1_000.0, |
| 34 | + 10_000.0, |
| 35 | + 100_000.0, |
| 36 | + 1_000_000.0, |
| 37 | + 10_000_000.0, |
| 38 | + 100_000_000.0, |
| 39 | + 1_000_000_000.0, |
| 40 | + 10_000_000_000.0, |
| 41 | + 100_000_000_000.0, |
| 42 | + 1_000_000_000_000.0, |
| 43 | +]; |
| 44 | + |
| 45 | +pub fn round_coordinates(doc: &Document, options: &Options) { |
| 46 | + let coord_precision = options.coordinates_precision as usize; |
| 47 | + let ts_precision = options.transform_precision as usize; |
| 48 | + |
| 49 | + for node in doc.descendants().svg() { |
| 50 | + let mut attrs = node.attributes_mut(); |
| 51 | + |
| 52 | + for (aid, ref mut attr) in attrs.iter_svg_mut() { |
| 53 | + match aid { |
| 54 | + AId::X | AId::Y | |
| 55 | + AId::Dx | AId::Dy | |
| 56 | + AId::X1 | AId::Y1 | |
| 57 | + AId::X2 | AId::Y2 | |
| 58 | + AId::R | |
| 59 | + AId::Rx | AId::Ry | |
| 60 | + AId::Cx | AId::Cy | |
| 61 | + AId::Fx | AId::Fy | |
| 62 | + AId::Width | AId::Height => { |
| 63 | + match attr.value { |
| 64 | + AttributeValue::Length(ref mut v) => { |
| 65 | + round_number(&mut v.num, coord_precision); |
| 66 | + } |
| 67 | + _ => {} |
| 68 | + } |
| 69 | + } |
| 70 | + AId::Transform | |
| 71 | + AId::GradientTransform | |
| 72 | + AId::PatternTransform => { |
| 73 | + match attr.value { |
| 74 | + AttributeValue::Transform(ref mut ts) => { |
| 75 | + round_number(&mut ts.a, ts_precision); |
| 76 | + round_number(&mut ts.b, ts_precision); |
| 77 | + round_number(&mut ts.c, ts_precision); |
| 78 | + round_number(&mut ts.d, ts_precision); |
| 79 | + round_number(&mut ts.e, coord_precision); |
| 80 | + round_number(&mut ts.f, coord_precision); |
| 81 | + } |
| 82 | + _ => {} |
| 83 | + } |
| 84 | + } |
| 85 | + _ => {} |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +fn round_number(n: &mut f64, precision: usize) { |
| 92 | + *n = (*n * POW_VEC[precision]).round() / POW_VEC[precision]; |
| 93 | +} |
0 commit comments