Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
template-base.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Contain internal sizes of paper which can be used in various
4 * functions to make and size pages.
5 *
6 * Authors:
7 * Martin Owens <doctormo@geek-2.com>
8 *
9 * Copyright (C) 2022 Authors
10 *
11 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
12 */
13
14#include "clear-n_.h"
15#include "document.h"
17#include "extension/template.h"
18#include "page-manager.h"
19#include "template-base.h"
20#include "object/sp-page.h"
21
23
28{
29 try {
30 return Geom::Point(tmod->get_param_float("width"), tmod->get_param_float("height"));
32 g_warning("Template type should provide height and width params!");
33 }
34 return Geom::Point(100, 100);
35}
36
41{
42 auto size = get_template_size(tmod);
43 auto t_unit = this->get_template_unit(tmod);
44 auto width = Util::Quantity((double)size.x(), t_unit).value(unit);
45 auto height = Util::Quantity((double)size.y(), t_unit).value(unit);
46 return Geom::Point(width, height);
47}
48
53{
54 auto const &unit_table = Util::UnitTable::get();
55 try {
56 return unit_table.getUnit(tmod->get_param_optiongroup("unit", "cm"));
58 return unit_table.getUnit(tmod->get_param_string("unit", "cm"));
59 }
60}
61
63{
64 auto unit = this->get_template_unit(tmod);
65 auto size = this->get_template_size(tmod);
66 auto width = Util::Quantity((double)size.x(), unit);
67 auto height = Util::Quantity((double)size.y(), unit);
68
69 // If it was a template file, modify the document according to user's input.
70 auto doc = tmod->get_template_document();
71 auto nv = doc->getNamedView();
72
73 // Set the width, height and default display units for the selected template
74 doc->setWidthAndHeight(width, height, true);
75 nv->setAttribute("inkscape:document-units", unit->abbr);
76 doc->setDocumentScale(1.0);
77 return doc;
78}
79
81{
82 static auto px = Util::UnitTable::get().getUnit("px");
83 auto size = this->get_template_size(tmod, px);
84 doc->getPageManager().resizePage(page, size.x(), size.y());
85}
86
88{
89 static auto px = Util::UnitTable::get().getUnit("px");
90 auto temp_size = get_template_size(tmod, px);
91 auto page_size = Geom::Point(width, height);
92 auto rota_size = Geom::Point(height, width);
93 // We want a half a pixel tollerance to catch floating point errors
94 // We also check the rotated size, as this is a valid match (for now)
95 return Geom::are_near(temp_size, page_size, 0.5) || Geom::are_near(temp_size, rota_size, 0.5);
96}
97
98} // namespace Inkscape::Extension::Internal
99
100/*
101 Local Variables:
102 mode:c++
103 c-file-style:"stroustrup"
104 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
105 indent-tabs-mode:nil
106 fill-column:99
107 End:
108*/
109// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
uint64_t page
Definition canvas.cpp:171
Two-dimensional point that doubles as a vector.
Definition point.h:66
char const * get_param_optiongroup(char const *name, char const *alt) const
Like get_param_optiongroup but with a default on param_not_exist error.
double get_param_float(char const *name) const
Gets a float parameter identified by name with the double placed in value.
char const * get_param_string(char const *name, char const *alt) const
Like get_param_string but with a default on param_not_exist error.
void resize_to_template(Inkscape::Extension::Template *tmod, SPDocument *doc, SPPage *page) override
std::unique_ptr< SPDocument > new_from_template(Inkscape::Extension::Template *tmod) override
virtual Geom::Point get_template_size(Inkscape::Extension::Template *tmod) const
Return the width and height of the new page, the default is a fixed orientation.
bool match_template_size(Inkscape::Extension::Template *tmod, double width, double height) override
virtual const Util::Unit * get_template_unit(Inkscape::Extension::Template *tmod) const
Return the unit the size is given in.
std::unique_ptr< SPDocument > get_template_document() const
Get the raw document svg for this template (pre-processing).
Definition template.cpp:407
void resizePage(double width, double height)
double value(Unit const *u) const
Return the quantity's value in the specified unit.
Definition units.cpp:565
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
Typed SVG document implementation.
Definition document.h:101
Inkscape::PageManager & getPageManager()
Definition document.h:162
A way to clear the N_ macro, which is defined as an inline function.
Parameters for extensions.
bool are_near(Affine const &a1, Affine const &a2, Coord eps=EPSILON)
SPPage – a page object.
double height
double width