summaryrefslogtreecommitdiffstats
path: root/src/ui/widget/page-size-preview.cpp
blob: 6cf2ed36d5d28be20c6277a686402bde8d8f2ed7 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// SPDX-License-Identifier: GPL-2.0-or-later
/**
 * @file
 *
 * Page size preview widget
 */
/*
 * Authors:
 *   Mike Kowalski
 *
 * Copyright (C) 2021 Authors
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include "page-size-preview.h"
#include "display/cairo-utils.h"
#include "2geom/rect.h"

namespace Inkscape {
namespace UI {
namespace Widget {

PageSizePreview::PageSizePreview() {
    show();
}

void rounded_rectangle(const Cairo::RefPtr<Cairo::Context>& cr, double x, double y, double w, double h, double r) {
    cr->begin_new_sub_path();
    cr->arc(x + r, y + r, r, M_PI, 3 * M_PI / 2);
    cr->arc(x + w - r, y + r, r, 3 * M_PI / 2, 2 * M_PI);
    cr->arc(x + w - r, y + h - r, r, 0, M_PI / 2);
    cr->arc(x + r, y + h - r, r, M_PI / 2, M_PI);
    cr->close_path();
}

void set_source_rgba(const Cairo::RefPtr<Cairo::Context>& ctx, unsigned int rgba) {
    ctx->set_source_rgba(SP_RGBA32_R_F(rgba), SP_RGBA32_G_F(rgba), SP_RGBA32_B_F(rgba), SP_RGBA32_A_F(rgba));
}

bool PageSizePreview::on_draw(const Cairo::RefPtr<Cairo::Context>& ctx) {
    auto alloc = get_allocation();
    double width = alloc.get_width();
    double height = alloc.get_height();
    // too small to fit anything?
    if (width <= 2 || height <= 2) return false;

    double x = 0;//alloc.get_x();
    double y = 0;//alloc.get_y();

    if (_draw_checkerboard) {
        // auto device_scale = get_scale_factor();
        Cairo::RefPtr<Cairo::Pattern> pattern(new Cairo::Pattern(ink_cairo_pattern_create_checkerboard(_desk_color)));
        ctx->save();
        ctx->set_operator(Cairo::OPERATOR_SOURCE);
        ctx->set_source(pattern);
        rounded_rectangle(ctx, x, y, width, height, 2.0);
        ctx->fill();
        ctx->restore();
    }
    else {
        rounded_rectangle(ctx, x, y, width, height, 2.0);
        set_source_rgba(ctx, _desk_color);
        ctx->fill();
    }

    // use lesser dimension to prevent page from changing size when
    // switching from portrait to landscape or vice versa
    auto size = std::round(std::min(width, height) * 0.90); // 90% to leave margins
    double w, h;
    if (_width > _height) {
        w = size;
        h = std::round(size * _height / _width);
    }
    else {
        h = size;
        w = std::round(size * _width / _height);
    }
    if (w < 2) w = 2;
    if (h < 2) h = 2;

    // center page
    double ox = std::round(x + (width - w) / 2);
    double oy = std::round(y + (height - h) / 2);
    Geom::Rect rect(ox, oy, ox + w, oy + h);

    ctx->rectangle(rect.left(), rect.top(), rect.width(), rect.height());

    if (_draw_checkerboard) {
        Cairo::RefPtr<Cairo::Pattern> pattern(new Cairo::Pattern(ink_cairo_pattern_create_checkerboard(_page_color)));
        ctx->save();
        ctx->set_operator(Cairo::OPERATOR_SOURCE);
        ctx->set_source(pattern);
        ctx->rectangle(rect.left(), rect.top(), rect.width(), rect.height());
        ctx->fill();
        ctx->restore();
    }
    else {
        ctx->rectangle(rect.left(), rect.top(), rect.width(), rect.height());
        set_source_rgba(ctx, _page_color | 0xff);
        ctx->fill();
    }

    // draw cross
    /*
    {
        double gradient_size = 4;
        double cx = std::round(x + (width - gradient_size) / 2);
        double cy = std::round(y + (height - gradient_size) / 2);
        auto horz = Cairo::LinearGradient::create(x, cy, x, cy + gradient_size);
        auto vert = Cairo::LinearGradient::create(cx, y, cx + gradient_size, y);

        horz->add_color_stop_rgba(0.0, 0, 0, 0, 0.0);
        horz->add_color_stop_rgba(0.5, 0, 0, 0, 0.2);
        horz->add_color_stop_rgba(0.5, 1, 1, 1, 0.8);
        horz->add_color_stop_rgba(1.0, 1, 1, 1, 0.0);

        vert->add_color_stop_rgba(0.0, 0, 0, 0, 0.0);
        vert->add_color_stop_rgba(0.5, 0, 0, 0, 0.2);
        vert->add_color_stop_rgba(0.5, 1, 1, 1, 0.8);
        vert->add_color_stop_rgba(1.0, 1, 1, 1, 0.0);

        ctx->rectangle(x, cy, width, gradient_size);
        ctx->set_source(horz);
        ctx->fill();

        ctx->rectangle(cx, y, gradient_size, height);
        ctx->set_source(vert);
        ctx->fill();
    }

    ctx->rectangle(rect.left(), rect.top(), rect.width(), rect.height());
    set_source_rgba(ctx, _page_color);
    ctx->fill();
*/
    if (_draw_border) {
        // stoke; not pixel aligned, just like page on canvas
        ctx->rectangle(rect.left(), rect.top(), rect.width(), rect.height());
        set_source_rgba(ctx, _border_color);
        ctx->set_line_width(1);
        ctx->stroke();

        if (_draw_shadow) {
            const auto a = (exp(-3 * SP_RGBA32_A_F(_border_color)) - 1) / (exp(-3) - 1);
            ink_cairo_draw_drop_shadow(ctx, rect, 12, _border_color, a);
        }
    }

    return true;
}

void PageSizePreview::draw_border(bool border) {
    _draw_border = border;
    queue_draw();
}

void PageSizePreview::set_desk_color(unsigned int rgba) {
    _desk_color = rgba | 0xff; // desk always opaque
    queue_draw();
}
void PageSizePreview::set_page_color(unsigned int rgba) {
    _page_color = rgba;
    queue_draw();
}
void PageSizePreview::set_border_color(unsigned int rgba) {
    _border_color = rgba;
    queue_draw();
}

void PageSizePreview::enable_drop_shadow(bool shadow) {
    _draw_shadow = shadow;
    queue_draw();
}

void PageSizePreview::enable_checkerboard(bool checkerboard) {
    _draw_checkerboard = checkerboard;
    queue_draw();
}

void PageSizePreview::set_page_size(double width, double height) {
    _width = width;
    _height = height;
    queue_draw();
}

} } } // namespace Inkscape/Widget/UI