Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
svg-box.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2#ifndef SEEN_SP_SVG_BOX_H
3#define SEEN_SP_SVG_BOX_H
4/*
5 * Authors:
6 * Martin Owens <doctormo@geek-2.com>
7 *
8 * Copyright (C) 2022 Martin Owens
9 *
10 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
11 */
12
13#include <glib.h>
14#include <optional>
15#include "svg/svg-length.h"
16#include <2geom/transforms.h>
17#include <2geom/rect.h>
18
25
26class SVGBox {
27public:
28 SVGBox();
29 SVGBox(Geom::OptRect box) { set(box); }
30
31 bool read(const std::string &value, const Geom::Scale &doc_scale);
32 void unset();
33 void readOrUnset(gchar const *str, const Geom::Scale &doc_scale);
34 void update(double em, double ex, double width, double height);
35
36 operator bool() const { return _is_set; }
37
38 std::string write() const;
39 std::string toString(const std::string &unit, const Geom::Scale &doc_scale, std::optional<unsigned int> precision = {}, bool add_unit = true) const;
40 bool fromString(const std::string &value, const std::string &unit, const Geom::Scale &doc_scale);
41 bool fromString(BoxSide side, const std::string &value, const std::string &unit, const Geom::Scale &doc_scale);
42 bool isZero() const;
43
44 void set(BoxSide side, double value, bool confine = false);
45 void set(double top, double right, double bottom, double left);
46 void set(double top, double horz, double bottom) { set(top, horz, bottom, horz); }
47 void set(double vert, double horz) { set(vert, horz, vert, horz); }
48 void set(double size) { set(size, size, size, size); }
49
50 void set(Geom::Rect box) { set(box.top(), box.right(), box.bottom(), box.left()); }
51 void set(Geom::OptRect box) {
52 unset();
53 if (box) {
54 set(*box);
55 }
56 }
57
58 double get(BoxSide side) const { return _value[side].computed; }
59 SVGLength top() const { return _value[BOX_TOP]; }
60 SVGLength right() const { return _value[BOX_RIGHT] ? _value[BOX_RIGHT] : top(); }
62 SVGLength left() const { return _value[BOX_LEFT] ? _value[BOX_LEFT] : right(); }
63
64 static Geom::Dim2 get_scale_axis(BoxSide side) { return (side & 1) ? Geom::X : Geom::Y; }
65private:
66 bool _is_set = false;
67
69};
70
71#endif // SEEN_SP_SVG_BOX_H
72
73/*
74 Local Variables:
75 mode:c++
76 c-file-style:"stroustrup"
77 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
78 indent-tabs-mode:nil
79 fill-column:99
80 End:
81*/
82// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
C right() const
Return rightmost coordinate of the rectangle (+X is to the right).
C top() const
Return top coordinate of the rectangle (+Y is downwards).
C left() const
Return leftmost coordinate of the rectangle (+X is to the right).
C bottom() const
Return bottom coordinate of the rectangle (+Y is downwards).
Axis-aligned rectangle that can be empty.
Definition rect.h:203
Axis aligned, non-empty rectangle.
Definition rect.h:92
Scaling from the origin.
Definition transforms.h:150
SVGLength bottom() const
Definition svg-box.h:61
SVGBox(Geom::OptRect box)
Definition svg-box.h:29
void unset()
Definition svg-box.cpp:159
void set(Geom::Rect box)
Definition svg-box.h:50
void set(double size)
Definition svg-box.h:48
void set(double vert, double horz)
Definition svg-box.h:47
SVGLength left() const
Definition svg-box.h:62
SVGLength _value[4]
Definition svg-box.h:68
void set(double top, double horz, double bottom)
Definition svg-box.h:46
static Geom::Dim2 get_scale_axis(BoxSide side)
Definition svg-box.h:64
void set(Geom::OptRect box)
Definition svg-box.h:51
bool fromString(const std::string &value, const std::string &unit, const Geom::Scale &doc_scale)
Set the svg box from user input, with a default unit.
Definition svg-box.cpp:86
bool isZero() const
Returns true if the box is set, but all values are zero.
Definition svg-box.cpp:125
void readOrUnset(gchar const *str, const Geom::Scale &doc_scale)
Definition svg-box.cpp:163
std::string write() const
Write out the values into a compact form.
Definition svg-box.cpp:54
bool _is_set
Definition svg-box.h:66
void update(double em, double ex, double width, double height)
Update box with em, ex and percentage scaling.
Definition svg-box.cpp:43
SVGBox()
An svg box is a type of css/html type which contains up to 4 svg lengths.
Definition svg-box.cpp:29
bool read(const std::string &value, const Geom::Scale &doc_scale)
Read in the value, may be an array of four.
Definition svg-box.cpp:35
std::string toString(const std::string &unit, const Geom::Scale &doc_scale, std::optional< unsigned int > precision={}, bool add_unit=true) const
Write as specific unit for user display.
Definition svg-box.cpp:62
SVGLength top() const
Definition svg-box.h:59
SVGLength right() const
Definition svg-box.h:60
double get(BoxSide side) const
Definition svg-box.h:58
SVG length type.
Definition svg-length.h:22
float computed
Definition svg-length.h:50
Dim2
2D axis enumeration (X or Y).
Definition coord.h:48
@ Y
Definition coord.h:48
@ X
Definition coord.h:48
int size
Axis-aligned rectangle.
BoxSide
Definition svg-box.h:19
@ BOX_BOTTOM
Definition svg-box.h:22
@ BOX_LEFT
Definition svg-box.h:23
@ BOX_TOP
Definition svg-box.h:20
@ BOX_RIGHT
Definition svg-box.h:21
double height
double width
Affine transformation classes.