aboutsummaryrefslogtreecommitdiff
path: root/svg-flatten/src/svg_import_util.cpp
blob: 85db3ad9382ae3c31062739c4534166ba9dab992 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
 * This file is part of gerbolyze, a vector image preprocessing toolchain 
 * Copyright (C) 2021 Jan Sebastian Götte <gerbolyze@jaseg.de>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <cmath>
#include "base64.h"
#include "svg_import_util.h"

using namespace std;

/* Read a double value formatted like usvg formats doubles from an SVG attribute */
double gerbolyze::usvg_double_attr(const pugi::xml_node &node, const char *attr, double default_value) {
    const auto *val = node.attribute(attr).value();
    if (*val == '\0')
        return default_value;

    return atof(val);
}

/* Read an url from an usvg attribute */
string gerbolyze::usvg_id_url(string attr) {
    if (attr.rfind("url(#", 0) == string::npos)
        return string();

    attr = attr.substr(strlen("url(#"));
    attr = attr.substr(0, attr.size()-1);
    return attr;
}

gerbolyze::RelativeUnits gerbolyze::map_str_to_units(string str, gerbolyze::RelativeUnits default_val) {
    if (str == "objectBoundingBox")
        return SVG_ObjectBoundingBox;
    else if (str == "userSpaceOnUse")
        return SVG_UserSpaceOnUse;
    return default_val;
}

/* Cf. https://tools.ietf.org/html/rfc2397 */
string gerbolyze::parse_data_iri(const string &data_url) {
    if (data_url.rfind("data:", 0) == string::npos) /* check if url starts with "data:" */
        return string();

    size_t foo = data_url.find("base64,");
    if (foo == string::npos) /* check if this is actually a data URL */
        return string();

    size_t b64_begin = data_url.find_first_not_of(" ", foo + strlen("base64,"));
    assert(b64_begin != string::npos);

    bool err_out;
    string out = base64_decode(data_url.substr(b64_begin), false, &err_out);

    if (err_out)
      return "";
    return out;
}