Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
paper.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Inkscape PaperSize
4 *
5 * Authors:
6 * Bob Jamison (2006)
7 * Martin Owens (2021)
8 *
9 * Copyright (C) 2021 AUTHORS
10 *
11 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
12 */
13
14#include <glibmm/i18n.h>
15
16#include "paper.h"
17#include "pages-skeleton.h"
18
19#include "io/resource.h"
20
21namespace Inkscape {
22
26const std::vector<PaperSize>& PaperSize::getPageSizes()
27{
28 // Static makes us only load pages once.
29 static std::vector<PaperSize> ret;
30 if (!ret.empty())
31 return ret;
32 auto path = Inkscape::IO::Resource::profile_path("pages.csv");
33 if (!g_file_test(path.c_str(), G_FILE_TEST_EXISTS)) {
34 if (!g_file_set_contents(path.c_str(), pages_skeleton, -1, nullptr)) {
35 g_warning("%s", _("Failed to create the page file."));
36 }
37 }
38 gchar *content = nullptr;
39 if (g_file_get_contents(path.c_str(), &content, nullptr, nullptr)) {
40 gchar **lines = g_strsplit_set(content, "\n", 0);
41
42 for (int i = 0; lines && lines[i]; ++i) {
43 gchar **line = g_strsplit_set(lines[i], ",", 5);
44 if (!line[0] || !line[1] || !line[2] || !line[3] || line[0][0]=='#')
45 continue;
46
47 //name, width, height, unit
48 double width = g_ascii_strtod(line[1], nullptr);
49 double height = g_ascii_strtod(line[2], nullptr);
50 g_strstrip(line[0]);
51 g_strstrip(line[3]);
52 Glib::ustring name = line[0];
53 ret.push_back(PaperSize(name, width, height, Inkscape::Util::UnitTable::get().getUnit(line[3])));
54 }
55 g_strfreev(lines);
56 g_free(content);
57 }
58 return ret;
59}
60
61
63 : name("")
64 , width(0.0)
65 , height(0.0)
66{
68}
69
70
71PaperSize::PaperSize(std::string name, double width, double height, Inkscape::Util::Unit const *unit)
72 : name(std::move(name))
73 , width(width)
74 , height(height)
75 , unit(unit)
76{}
77
78std::string PaperSize::getDescription(bool landscape) const {
79 return toDescription(name, size[landscape], size[!landscape], unit);
80}
81
82std::string PaperSize::toDimsString(double x, double y, Util::Unit const *unit)
83{
84 return formatNumber(x) + " × " + formatNumber(y) + " " + unit->abbr;
85}
86
87std::string PaperSize::toDescription(std::string name, double x, double y, Inkscape::Util::Unit const *unit)
88{
89 if (!name.empty()) {
90 name = _(name.c_str());
91 }
92 return name + " (" + toDimsString(x, y, unit) + ")";
93}
94
95std::string PaperSize::formatNumber(double val)
96{
97 char buf[20];
98 snprintf(buf, 19, "%0.1f", val);
99 auto ret = std::string(buf);
100 // C++ doesn't provide a good number formatting control, so hack off trailing zeros.
101 if ((ret.length() > 2) && (ret.back() == '0')) {
102 ret = ret.substr(0, ret.length() - 2);
103 }
104 return ret;
105}
106
107void PaperSize::assign(const PaperSize &other)
108{
109 name = other.name;
110 width = other.width;
111 height = other.height;
112 auto [smaller, larger] = std::minmax(width, height);
113 size = Geom::Point(smaller, larger);
114 unit = other.unit;
115}
116
121{
122 auto [smaller, larger] = std::minmax(width, height);
123 auto size = Geom::Point(smaller, larger);
124 auto px = Inkscape::Util::UnitTable::get().getUnit("px");
125
126 for (auto&& page_size : Inkscape::PaperSize::getPageSizes()) {
127 auto cmp = Geom::Point(
128 unit->convert(size[0], page_size.unit),
129 unit->convert(size[1], page_size.unit)
130 );
131 // We want a half a pixel tollerance to catch floating point errors
132 auto tollerance = px->convert(0.5, page_size.unit);
133 if (Geom::are_near(page_size.size, cmp, tollerance)) {
134 return &page_size;
135 }
136 }
137 return nullptr;
138}
139
140} // namespace Inkscape
141
142/*
143 Local Variables:
144 mode:c++
145 c-file-style:"stroustrup"
146 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
147 indent-tabs-mode:nil
148 fill-column:99
149 End:
150*/
151// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
static bool cmp(std::pair< Glib::ustring, Glib::ustring > const &a, std::pair< Glib::ustring, Glib::ustring > const &b)
Compare function.
Two-dimensional point that doubles as a vector.
Definition point.h:66
Data class used to store common paper dimensions from pages.csv.
Definition paper.h:27
Inkscape::Util::Unit const * unit
Definition paper.h:40
Geom::Point size
Definition paper.h:37
static const std::vector< PaperSize > & getPageSizes()
Returns a list of page sizes.
Definition paper.cpp:26
static std::string toDescription(std::string name, double x, double y, Inkscape::Util::Unit const *unit)
Definition paper.cpp:87
static std::string toDimsString(double x, double y, Util::Unit const *unit)
Definition paper.cpp:82
std::string getDescription(bool landscape) const
pointer to object in UnitTable, do not delete
Definition paper.cpp:78
static const PaperSize * findPaperSize(double width, double height, Inkscape::Util::Unit const *unit)
Returns a matching paper size, if possible.
Definition paper.cpp:120
static std::string formatNumber(double val)
Definition paper.cpp:95
std::string name
Definition paper.h:36
void assign(const PaperSize &other)
Definition paper.cpp:107
static UnitTable & get()
Definition units.cpp:441
Unit const * getUnit(Glib::ustring const &name) const
Retrieve a given unit based on its string identifier.
Definition units.cpp:316
double convert(double from_dist, Unit const *to) const
Convert value from this unit.
Definition units.cpp:229
Glib::ustring abbr
Definition units.h:81
bool are_near(Affine const &a1, Affine const &a2, Coord eps=EPSILON)
std::string profile_path()
Definition resource.cpp:415
Helper class to stream background task notifications as a series of messages.
STL namespace.
List of paper sizes.
static char const pages_skeleton[]
int buf
Inkscape::IO::Resource - simple resource API.
double height
double width
Glib::ustring name
Definition toolbars.cpp:55