summaryrefslogtreecommitdiffstats
path: root/src/svg/svg-box.h
blob: 7cc18533640790fbbd7c20a77371781d0f3f8fa2 (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
// SPDX-License-Identifier: GPL-2.0-or-later
#ifndef SEEN_SP_SVG_BOX_H
#define SEEN_SP_SVG_BOX_H
/*
 * Authors:
 *   Martin Owens <doctormo@geek-2.com>
 *
 * Copyright (C) 2022 Martin Owens
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <glib.h>
#include <optional>
#include "svg/svg-length.h"

enum BoxSide {
    BOX_TOP,
    BOX_RIGHT,
    BOX_BOTTOM,
    BOX_LEFT
};

class SVGBox {
public:
    SVGBox();

    bool read(const std::string &value);
    void unset();
    void readOrUnset(gchar const *str);
    void update(double em, double ex, double width, double height);

    operator bool() const { return _is_set; }

    std::string write() const;
    std::string toString(const std::string &unit, std::optional<unsigned int> precision = {}, bool add_unit = true) const;
    bool fromString(const std::string &value, const std::string &unit);
    bool fromString(BoxSide side, const std::string &value, const std::string &unit);
    bool isZero() const;

    void set(BoxSide side, double value, bool confine = false);
    void set(double top, double right, double bottom, double left);
    void set(double top, double horz, double bottom) { set(top, horz, bottom, horz); }
    void set(double vert, double horz)               { set(vert, horz, vert, horz);  }
    void set(double size)                            { set(size, size, size, size);  }

    double get(BoxSide side) const { return _value[side].computed; }
    SVGLength top() const { return _value[BOX_TOP]; }
    SVGLength right() const { return _value[BOX_RIGHT] ? _value[BOX_RIGHT] : top(); }
    SVGLength bottom() const { return _value[BOX_BOTTOM] ? _value[BOX_BOTTOM] : top(); }
    SVGLength left() const { return _value[BOX_LEFT] ? _value[BOX_LEFT] : right(); }
private:
    bool _is_set = false;
    
    SVGLength _value[4];
};

#endif // SEEN_SP_SVG_BOX_H

/*
  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 :