Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
svg-angle.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
6/*
7 * Authors:
8 * Tomasz Boczkowski <penginsbacon@gmail.com>
9 *
10 * Copyright (C) 1999-2002 Lauris Kaplinski
11 * Copyright (C) 2000-2001 Ximian, Inc.
12 *
13 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
14 */
15
16#include <cstring>
17#include <string>
18#include <glib.h>
19
20#include "svg/svg-angle.h"
21#include "util/units.h"
22
23
24static bool sp_svg_angle_read_lff(gchar const *str, SVGAngle::Unit &unit, double &val, double &computed);
25
27 : _set(false)
28 , unit(Unit::NONE)
29 , value(0)
30 , computed(0)
31{
32}
33
34/* Angle */
35
36bool SVGAngle::read(gchar const *str)
37{
38 if (!str) {
39 return false;
40 }
41
43 double v;
44 double c;
45 if (!sp_svg_angle_read_lff(str, u, v, c)) {
46 return false;
47 }
48
49 _set = true;
50 unit = u;
51 value = v;
52 computed = c;
53
54 return true;
55}
56
57void SVGAngle::unset(SVGAngle::Unit u, double v, double c) {
58 _set = false;
59 unit = u;
60 value = v;
61 computed = c;
62}
63
64void SVGAngle::readOrUnset(gchar const *str, Unit u, double v, double c) {
65 if (!read(str)) {
66 unset(u, v, c);
67 }
68}
69
70static bool sp_svg_angle_read_lff(gchar const *str, SVGAngle::Unit &unit, double &val, double &computed)
71{
72 if (!str) {
73 return false;
74 }
75
76 gchar const *e;
77 double const v = g_ascii_strtod(str, (char **) &e);
78 if (e == str) {
79 return false;
80 }
81
82 if (!e[0]) {
83 /* Unitless (defaults to degrees)*/
85 val = v;
86 computed = v;
87 return true;
88 } else if (!g_ascii_isalnum(e[0])) {
89 if (g_ascii_isspace(e[0]) && e[1] && g_ascii_isalpha(e[1])) {
90 return false; // spaces between value and unit are not allowed
91 } else {
92 /* Unitless (defaults to degrees)*/
94 val = v;
95 computed = v;
96 return true;
97 }
98 } else {
99 if (strncmp(e, "deg", 3) == 0) {
100 unit = SVGAngle::Unit::DEG;
101 val = v;
102 computed = v;
103 } else if (strncmp(e, "grad", 4) == 0) {
105 val = v;
106 computed = Inkscape::Util::Quantity::convert(v, "grad", "°");
107 } else if (strncmp(e, "rad", 3) == 0) {
108 unit = SVGAngle::Unit::RAD;
109 val = v;
110 computed = Inkscape::Util::Quantity::convert(v, "rad", "°");
111 } else if (strncmp(e, "turn", 4) == 0) {
113 val = v;
114 computed = Inkscape::Util::Quantity::convert(v, "turn", "°");
115 } else {
116 return false;
117 }
118 return true;
119 }
120
121 /* Invalid */
122 return false;
123}
124
125/*
126 Local Variables:
127 mode:c++
128 c-file-style:"stroustrup"
129 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
130 indent-tabs-mode:nil
131 fill-column:99
132 End:
133*/
134// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
static double convert(double from_dist, Unit const *from, Unit const *to)
Convert distances.
Definition units.cpp:588
void unset(Unit u=Unit::NONE, double v=0, double c=0)
Definition svg-angle.cpp:57
double value
Definition svg-angle.h:42
double computed
Definition svg-angle.h:45
bool _set
Definition svg-angle.h:36
bool read(gchar const *str)
Definition svg-angle.cpp:36
Unit unit
Definition svg-angle.h:39
void readOrUnset(gchar const *str, Unit u=Unit::NONE, double v=0, double c=0)
Definition svg-angle.cpp:64
double c[8][4]
static bool sp_svg_angle_read_lff(gchar const *str, SVGAngle::Unit &unit, double &val, double &computed)
Definition svg-angle.cpp:70
SVG angle type.