summaryrefslogtreecommitdiffstats
path: root/src/svg/svg-angle.cpp
blob: 0e4af4fb2645d9c5332bf2dd9ba1cea725b735fb (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// SPDX-License-Identifier: GPL-2.0-or-later
/**
 *  \file src/svg/svg-angle.cpp
 *  \brief SVG angle type
 */
/*
 * Authors:
 *   Tomasz Boczkowski <penginsbacon@gmail.com>
 *
 * Copyright (C) 1999-2002 Lauris Kaplinski
 * Copyright (C) 2000-2001 Ximian, Inc.
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <cstring>
#include <string>
#include <glib.h>

#include "svg/svg-angle.h"
#include "util/units.h"


static bool sp_svg_angle_read_lff(gchar const *str, SVGAngle::Unit &unit, float &val, float &computed);

SVGAngle::SVGAngle()
    : _set(false)
    , unit(Unit::NONE)
    , value(0)
    , computed(0)
{
}

/* Angle */

bool SVGAngle::read(gchar const *str)
{
    if (!str) {
        return false;
    }

    SVGAngle::Unit u;
    float v;
    float c;
    if (!sp_svg_angle_read_lff(str, u, v, c)) {
        return false;
    }

    _set = true;
    unit = u;
    value = v;
    computed = c;

    return true;
}

void SVGAngle::unset(SVGAngle::Unit u, float v, float c) {
    _set = false;
    unit = u;
    value = v;
    computed = c;
}

void SVGAngle::readOrUnset(gchar const *str, Unit u, float v, float c) {
    if (!read(str)) {
        unset(u, v, c);
    }
}

static bool sp_svg_angle_read_lff(gchar const *str, SVGAngle::Unit &unit, float &val, float &computed)
{
    if (!str) {
        return false;
    }

    gchar const *e;
    float const v = g_ascii_strtod(str, (char **) &e);
    if (e == str) {
        return false;
    }

    if (!e[0]) {
        /* Unitless (defaults to degrees)*/
        unit = SVGAngle::Unit::NONE;
        val = v;
        computed = v;
        return true;
    } else if (!g_ascii_isalnum(e[0])) {
        if (g_ascii_isspace(e[0]) && e[1] && g_ascii_isalpha(e[1])) {
            return false; // spaces between value and unit are not allowed
        } else {
            /* Unitless (defaults to degrees)*/
            unit = SVGAngle::Unit::NONE;
            val = v;
            computed = v;
            return true;
        }
    } else {
        if (strncmp(e, "deg", 3) == 0) {
            unit = SVGAngle::Unit::DEG;
            val = v;
            computed = v;
        } else if (strncmp(e, "grad", 4) == 0) {
            unit = SVGAngle::Unit::GRAD;
            val = v;
            computed = Inkscape::Util::Quantity::convert(v, "grad", "°");
        } else if (strncmp(e, "rad", 3) == 0) {
            unit = SVGAngle::Unit::RAD;
            val = v;
            computed = Inkscape::Util::Quantity::convert(v, "rad", "°");
        } else if (strncmp(e, "turn", 4) == 0) {
            unit = SVGAngle::Unit::TURN;
            val = v;
            computed = Inkscape::Util::Quantity::convert(v, "turn", "°");
        } else {
            return false;
        }
        return true;
    }

    /* Invalid */
    return false;
}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :