summaryrefslogtreecommitdiffstats
path: root/src/util/object-renderer.h
blob: 98f889040f3621be63ef63a96531816820970961 (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
// SPDX-License-Identifier: GPL-2.0-or-later
#ifndef SEEN_OBJECT_RENDERER_H
#define SEEN_OBJECT_RENDERER_H

#include <cairomm/refptr.h>
#include <cairomm/surface.h>
#include <gdkmm/rgba.h>
#include <glibmm/ustring.h>
#include <memory>
#include <optional>
#include "display/drawing.h"
#include "object/sp-object.h"
#include "document.h"

namespace Inkscape {

class object_renderer {
public:
    object_renderer();

    struct options {
        options() {}
        // foreground color, where used
        options& foreground(Gdk::RGBA fg) {
            _foreground = fg;
            return *this;
        }
        // background color, where used
        options& solid_background(uint32_t bg, double margin, double corner_radius = 0) {
            _add_background = true;
            _background = bg;
            _margin = margin;
            _radius = corner_radius;
            return *this;
        }
        // use checkerboard pattern for drawing background
        options& checkerboard(uint32_t color) {
            _checkerboard = color;
            return *this;
        }
        // option to add an outline to rendered image
        options& frame(uint32_t rgba, double thickness = 1) {
            _stroke = thickness;
            _draw_frame = true;
            _frame_rgba = rgba;
            return *this;
        }
        // option to reduce opacity of rendered image
        options& image_opacity(double alpha) {
            _image_opacity = alpha;
            return *this;
        }
        // for symbols only: take style from <use> element
        options& symbol_style_from_use(bool from_use_element = true) {
            _symbol_style_from_use = from_use_element;
            return *this;
        }

    private:
        friend class object_renderer;
        Gdk::RGBA _foreground;
        bool _add_background = false;
        uint32_t _background;
        double _margin = 0;
        double _radius = 0;
        bool _symbol_style_from_use = false;
        bool _draw_frame = false;
        double _stroke = 0;
        uint32_t _frame_rgba = 0;
        double _image_opacity = 1;
        std::optional<uint32_t> _checkerboard;
    };

    Cairo::RefPtr<Cairo::Surface> render(SPObject& object, double width, double height, double device_scale, options options = {});

private:
    std::unique_ptr<SPDocument> _symbol_document;
    std::unique_ptr<SPDocument> _sandbox;
};

// Place 'image' on a solid background with given color optinally adding border.
// If no image is provided, only background surface will be created.
Cairo::RefPtr<Cairo::Surface> add_background_to_image(Cairo::RefPtr<Cairo::Surface> image, uint32_t rgb, double margin, double radius, int device_scale, std::optional<uint32_t> border = std::optional<uint32_t>());

/**
 * Returns a new document containing default start, mid, and end markers.
 * Note 1: group IDs are matched against "group_id" to render correct preview object.
 * Note 2: paths/lines are kept outside of groups, so they don't inflate visible bounds
 * Note 3: invisible rects inside groups keep visual bounds from getting too small, so we can see relative marker sizes
 */
std::unique_ptr<SPDocument> ink_markers_preview_doc(const Glib::ustring& group_id);

/**
 * Creates a copy of the marker named mname, determines its visible and renderable
 * area in the bounding box, and then renders it. This allows us to fill in
 * preview images of each marker in the marker combobox.
 */
Cairo::RefPtr<Cairo::Surface> create_marker_image(
    const Glib::ustring& group_id,
    SPDocument* _sandbox,
    Gdk::RGBA marker_color,
    Geom::IntPoint pixel_size,
    const char* mname,
    SPDocument* source,
    Inkscape::Drawing& drawing,
    std::optional<guint32> checkerboard,
    bool no_clip,
    double scale,
    int device_scale);

} // namespace

#endif // SEEN_OBJECT_RENDERER_H