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
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Inkscape PaperSize
*
* Authors:
* Bob Jamison (2006)
* Martin Owens (2021)
*
* Copyright (C) 2021 AUTHORS
*
* Released under GNU GPL v2+, read the file 'COPYING' for more information.
*/
#include <glibmm/i18n.h>
#include "paper.h"
#include "pages-skeleton.h"
#include "io/resource.h"
namespace Inkscape {
/**
* Returns a list of page sizes.
*/
const std::vector<PaperSize>& PaperSize::getPageSizes()
{
// Static makes us only load pages once.
static std::vector<PaperSize> ret;
if (!ret.empty())
return ret;
char *path = Inkscape::IO::Resource::profile_path("pages.csv");
if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
if (!g_file_set_contents(path, pages_skeleton, -1, nullptr)) {
g_warning("%s", _("Failed to create the page file."));
}
}
gchar *content = nullptr;
if (g_file_get_contents(path, &content, nullptr, nullptr)) {
gchar **lines = g_strsplit_set(content, "\n", 0);
for (int i = 0; lines && lines[i]; ++i) {
gchar **line = g_strsplit_set(lines[i], ",", 5);
if (!line[0] || !line[1] || !line[2] || !line[3] || line[0][0]=='#')
continue;
//name, width, height, unit
double width = g_ascii_strtod(line[1], nullptr);
double height = g_ascii_strtod(line[2], nullptr);
g_strstrip(line[0]);
g_strstrip(line[3]);
Glib::ustring name = line[0];
ret.push_back(PaperSize(name, width, height, Inkscape::Util::unit_table.getUnit(line[3])));
}
g_strfreev(lines);
g_free(content);
}
g_free(path);
return ret;
}
PaperSize::PaperSize()
: name("")
, width(0.0)
, height(0.0)
{
unit = Inkscape::Util::unit_table.getUnit("px");
}
PaperSize::PaperSize(std::string name, double width, double height, Inkscape::Util::Unit const *unit)
: name(std::move(name))
, width(width)
, height(height)
, unit(unit)
{}
std::string PaperSize::getDescription(bool landscape) const {
return toDescription(name, size[landscape], size[!landscape], unit);
}
std::string PaperSize::toDescription(std::string name, double x, double y, Inkscape::Util::Unit const *unit)
{
return name + " (" + formatNumber(x) + " x " + formatNumber(y) + " " + unit->abbr + ")";
}
std::string PaperSize::formatNumber(double val)
{
char buf[20];
snprintf(buf, 19, "%0.1f", val);
auto ret = std::string(buf);
// C++ doesn't provide a good number formatting control, so hack off trailing zeros.
if ((ret.length() > 2) && (ret.back() == '0')) {
ret = ret.substr(0, ret.length() - 2);
}
return ret;
}
void PaperSize::assign(const PaperSize &other)
{
name = other.name;
width = other.width;
height = other.height;
auto [smaller, larger] = std::minmax(width, height);
size = Geom::Point(smaller, larger);
unit = other.unit;
}
/**
* Returns a matching paper size, if possible.
*/
const PaperSize *PaperSize::findPaperSize(double width, double height, Inkscape::Util::Unit const *unit)
{
auto [smaller, larger] = std::minmax(width, height);
auto size = Geom::Point(smaller, larger);
auto px = Inkscape::Util::unit_table.getUnit("px");
for (auto&& page_size : Inkscape::PaperSize::getPageSizes()) {
auto cmp = Geom::Point(
unit->convert(size[0], page_size.unit),
unit->convert(size[1], page_size.unit)
);
// We want a half a pixel tollerance to catch floating point errors
auto tollerance = px->convert(0.5, page_size.unit);
if (Geom::are_near(page_size.size, cmp, tollerance)) {
return &page_size;
}
}
return nullptr;
}
} // 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:fileencoding=utf-8:textwidth=99 :
|