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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Inkscape pages implementation
*
* Authors:
* Martin Owens <doctormo@geek-2.com>
*
* Copyright (C) 2021 Martin Owens
*
* Released under GNU GPL v2+, read the file 'COPYING' for more information.
*/
#include "canvas-page.h"
#include "canvas-item-rect.h"
#include "canvas-item-text.h"
#include "color.h"
namespace Inkscape {
CanvasPage::CanvasPage() = default;
CanvasPage::~CanvasPage() = default;
/**
* Add the page canvas to the given canvas item groups (canvas view is implicit)
*/
void CanvasPage::add(Geom::Rect size, CanvasItemGroup *background_group, CanvasItemGroup *border_group)
{
// Foreground 'border'
if (auto item = new CanvasItemRect(border_group, size)) {
item->set_name("foreground");
item->set_is_page(true);
canvas_items.emplace_back(item);
}
// Background rectangle 'fill'
if (auto item = new CanvasItemRect(background_group, size)) {
item->set_name("background");
item->set_is_page(true);
item->set_dashed(false);
item->set_inverted(false);
item->set_stroke(0x00000000);
canvas_items.emplace_back(item);
}
if (auto item = new CanvasItemRect(border_group, size)) {
item->set_name("margin");
item->set_dashed(false);
item->set_inverted(false);
item->set_stroke(_margin_color);
canvas_items.emplace_back(item);
}
if (auto item = new CanvasItemRect(border_group, size)) {
item->set_name("bleed");
item->set_dashed(false);
item->set_inverted(false);
item->set_stroke(_bleed_color);
canvas_items.emplace_back(item);
}
if (auto label = new CanvasItemText(border_group, Geom::Point(0, 0), "{Page Label}")) {
label->set_fixed_line(false);
canvas_items.emplace_back(label);
}
}
/**
* Hide the page in the given canvas widget.
*/
void CanvasPage::remove(UI::Widget::Canvas *canvas)
{
g_assert(canvas != nullptr);
for (auto it = canvas_items.begin(); it != canvas_items.end();) {
if (canvas == (*it)->get_canvas()) {
it = canvas_items.erase(it);
} else {
++it;
}
}
}
void CanvasPage::show()
{
for (auto &item : canvas_items) {
item->show();
}
}
void CanvasPage::hide()
{
for (auto &item : canvas_items) {
item->hide();
}
}
void CanvasPage::set_guides_visible(bool show) {
for (auto& item: canvas_items) {
if (item->get_name() == "margin" || item->get_name() == "bleed") {
item->set_visible(show);
}
}
}
/**
* Update the visual representation of a page on screen.
*
* @param size - The size of the page in desktop units
* @param txt - An optional label for the page
* @param outline - Disable normal rendering and show as an outline.
*/
void CanvasPage::update(Geom::Rect size, Geom::OptRect margin, Geom::OptRect bleed, const char *txt, bool outline)
{
// Put these in the preferences?
bool border_on_top = _border_on_top;
guint32 shadow_color = _border_color; // there's no separate shadow color in the UI, border color is used
guint32 select_color = 0x000000cc;
guint32 border_color = _border_color;
guint32 margin_color = _margin_color;
guint32 bleed_color = _bleed_color;
// This is used when showing the viewport as *not a page* it's mostly
// never used as the first page is normally the viewport too.
if (outline) {
border_on_top = false;
_shadow_size = 0;
border_color = select_color;
}
for (auto &item : canvas_items) {
if (auto rect = dynamic_cast<CanvasItemRect *>(item.get())) {
if (rect->get_name() == "margin") {
rect->set_stroke(margin_color);
bool vis = margin && *margin != size;
rect->set_visible(vis);
if (vis) {
rect->set_rect(*margin);
}
continue;
}
if (rect->get_name() == "bleed") {
rect->set_stroke(bleed_color);
bool vis = bleed && *bleed != size;
rect->set_visible(vis);
if (vis) {
rect->set_rect(*bleed);
}
continue;
}
rect->set_rect(size);
bool is_foreground = (rect->get_name() == "foreground");
// This will put the border on the background OR foreground layer as needed.
if (is_foreground == border_on_top) {
rect->show();
rect->set_stroke(is_selected ? select_color : border_color);
} else {
rect->hide();
rect->set_stroke(0x0);
}
// This undoes the hide for the background rect, and additionally gives it a fill and shadow.
if (!is_foreground) {
rect->show();
/*
if (_checkerboard) {
// draw checkerboard pattern, ignore alpha (background color doesn't support it)
rect->set_background_checkerboard(_background_color, false);
}
else {
// Background color does not support transparency; draw opaque pages
rect->set_background(_background_color | 0xff);
}
*/
rect->set_fill(_background_color);
rect->set_shadow(shadow_color, _shadow_size);
} else {
rect->set_fill(0x0);
rect->set_shadow(0x0, 0);
}
} else if (auto label = dynamic_cast<CanvasItemText *>(item.get())) {
_updateTextItem(label, size, txt ? txt : "");
}
}
}
/**
* Update the page's textual label.
*/
void CanvasPage::_updateTextItem(CanvasItemText *label, Geom::Rect page, std::string txt)
{
// Default style for the label
int fontsize = 10.0;
uint32_t foreground = 0xffffffff;
uint32_t background = 0x00000099;
uint32_t selected = 0x0e5bf199;
Geom::Point anchor(0.0, 1.0);
Geom::Point coord = page.corner(0);
double radius = 0.2;
// Change the colors for whiter/lighter backgrounds
unsigned char luminance = SP_RGBA32_LUMINANCE(_canvas_color);
if (luminance < 0x88) {
foreground = 0x000000ff;
background = 0xffffff99;
selected = 0x50afe7ff;
}
if (_label_style == "below") {
radius = 1.0;
fontsize = 14.0;
anchor = Geom::Point(0.5, -0.2);
coord = Geom::Point(page.midpoint()[Geom::X], page.bottom());
if (!txt.empty()) {
std::string bullet = is_selected ? " \u2022 " : " ";
txt = bullet + txt + bullet;
}
}
label->set_fontsize(fontsize);
label->set_fill(foreground);
label->set_background(is_selected ? selected : background);
label->set_bg_radius(radius);
label->set_anchor(anchor);
label->set_coord(coord);
label->set_visible(!txt.empty());
label->set_text(std::move(txt));
label->set_border(4.0);
}
bool CanvasPage::setOnTop(bool on_top)
{
if (on_top != _border_on_top) {
_border_on_top = on_top;
return true;
}
return false;
}
bool CanvasPage::setShadow(int shadow)
{
if (_shadow_size != shadow) {
_shadow_size = shadow;
return true;
}
return false;
}
bool CanvasPage::setPageColor(uint32_t border, uint32_t bg, uint32_t canvas, uint32_t margin, uint32_t bleed)
{
if (border != _border_color || bg != _background_color || canvas != _canvas_color) {
_border_color = border;
_background_color = bg;
_canvas_color = canvas;
_margin_color = margin;
_bleed_color = bleed;
return true;
}
return false;
}
bool CanvasPage::setLabelStyle(const std::string &style)
{
if (style != _label_style) {
_label_style = style;
return true;
}
return false;
}
} // namespace Inkscape
/*
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 :
|