Debian Codegraph @

src/lib/QXPTypes.cpp

raw
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is part of the libqxp project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10 #include "QXPTypes.h"
11
12 #include <boost/math/constants/constants.hpp>
13 #include <cmath>
14
15 namespace libqxp
16 {
17
18 bool operator==(const Point &lhs, const Point &rhs)
19 {
20 return QXP_ALMOST_ZERO(lhs.x - rhs.x) && QXP_ALMOST_ZERO(lhs.y - rhs.y);
21 }
22
23 bool operator!=(const Point &lhs, const Point &rhs)
24 {
25 return !(lhs == rhs);
26 }
27
28 Point Point::move(double dx, double dy) const
29 {
30 return Point(x + dx, y + dy);
31 }
32
33 Point Point::rotateDeg(double rotationDeg, const Point &center) const
34 {
35 if (QXP_ALMOST_ZERO(rotationDeg))
36 {
37 return *this;
38 }
39 const double rotation = deg2rad(rotationDeg);
40 const double rotatedX = (x - center.x) * std::cos(rotation) - (y - center.y) * std::sin(rotation) + center.x;
41 const double rotatedY = (y - center.y) * std::cos(rotation) + (x - center.x) * std::sin(rotation) + center.y;
42 return Point(rotatedX, rotatedY);
43 }
44
45 double Point::distance(const Point &p2) const
46 {
47 return std::hypot(p2.x - x, p2.y - y);
48 }
49
50 Rect::Rect()
51 : top(0.0), right(0.0), bottom(0.0), left(0.0)
52 { }
53
54 Rect::Rect(double t, double r, double b, double l)
55 : top(t), right(r), bottom(b), left(l)
56 { }
57
58 double Rect::width() const
59 {
60 return right - left;
61 }
62
63 double Rect::height() const
64 {
65 return bottom - top;
66 }
67
68 Point Rect::center() const
69 {
70 return topLeft().move(width() / 2, height() / 2);
71 }
72
73 Point Rect::topLeft() const
74 {
75 return Point(left, top);
76 }
77
78 Point Rect::topRight() const
79 {
80 return Point(right, top);
81 }
82
83 Point Rect::bottomRight() const
84 {
85 return Point(right, bottom);
86 }
87
88 Point Rect::bottomLeft() const
89 {
90 return Point(left, bottom);
91 }
92
93 Rect Rect::shrink(const double diff) const
94 {
95 return Rect(top + diff, right - diff, bottom - diff, left + diff);
96 }
97
98 librevenge::RVNGString Color::toString() const
99 {
100 librevenge::RVNGString colorStr;
101 colorStr.sprintf("#%.2x%.2x%.2x", red, green, blue);
102 return colorStr;
103 }
104
105 Color Color::applyShade(double shade) const
106 {
107 if (shade < 0.0 || shade > 1.0)
108 {
109 QXP_DEBUG_MSG(("Invalid shade %lf\n", shade));
110 return *this;
111 }
112 const double tint = 1 - shade;
113 return Color(uint8_t(std::round(red + (255 - red) * tint)),
114 uint8_t(std::round(green + (255 - green) * tint)),
115 uint8_t(std::round(blue + (255 - blue) * tint)));
116 }
117
118 bool TextSpec::overlaps(const TextSpec &other) const
119 {
120 return startIndex <= other.endIndex() && other.startIndex <= endIndex();
121 }
122
123 double Text::maxFontSize() const
124 {
125 if (charFormats.empty())
126 {
127 QXP_DEBUG_MSG(("Text::maxFontSize no char formats\n"));
128 }
129
130 double maxSize = 0;
131 for (auto &charFormat : charFormats)
132 {
133 if (!charFormat.format->isControlChars && charFormat.format->fontSize > maxSize)
134 {
135 maxSize = charFormat.format->fontSize;
136 }
137 }
138 return maxSize;
139 }
140
141 double Text::maxFontSize(const ParagraphSpec &paragraph) const
142 {
143 double maxSize = 0;
144 for (auto &charFormat : charFormats)
145 {
146 if (!charFormat.format->isControlChars && charFormat.overlaps(paragraph) && charFormat.format->fontSize > maxSize)
147 {
148 maxSize = charFormat.format->fontSize;
149 }
150 }
151 return maxSize;
152 }
153
154 bool TextObject::isLinked() const
155 {
156 return linkSettings.linkedIndex > 0 || linkSettings.nextLinkedIndex > 0;
157 }
158
159 void QXPDocumentProperties::setAutoLeading(const double val)
160 {
161 if (val >= -63 && val <= 63)
162 {
163 m_autoLeading = val;
164 }
165 else
166 {
167 QXP_DEBUG_MSG(("Invalid auto leading %lf\n", val));
168 }
169 }
170
171 double QXPDocumentProperties::autoLeading() const
172 {
173 return m_autoLeading;
174 }
175
176 }
177
178 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */