/src/libreoffice/sdext/source/pdfimport/inc/pdfihelper.hxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice 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 | | * This file incorporates work covered by the following license notice: |
10 | | * |
11 | | * Licensed to the Apache Software Foundation (ASF) under one or more |
12 | | * contributor license agreements. See the NOTICE file distributed |
13 | | * with this work for additional information regarding copyright |
14 | | * ownership. The ASF licenses this file to you under the Apache |
15 | | * License, Version 2.0 (the "License"); you may not use this file |
16 | | * except in compliance with the License. You may obtain a copy of |
17 | | * the License at http://www.apache.org/licenses/LICENSE-2.0 . |
18 | | */ |
19 | | |
20 | | #ifndef INCLUDED_SDEXT_SOURCE_PDFIMPORT_INC_PDFIHELPER_HXX |
21 | | #define INCLUDED_SDEXT_SOURCE_PDFIMPORT_INC_PDFIHELPER_HXX |
22 | | |
23 | | #include "contentsink.hxx" |
24 | | |
25 | | #include <rtl/ustring.hxx> |
26 | | #include <rtl/math.h> |
27 | | #include <basegfx/matrix/b2dhommatrix.hxx> |
28 | | #include <basegfx/polygon/b2dpolypolygon.hxx> |
29 | | #include <basegfx/polygon/b2dpolygon.hxx> |
30 | | #include <basegfx/vector/b2enums.hxx> |
31 | | #include <com/sun/star/rendering/PathCapType.hpp> |
32 | | |
33 | | #include <unordered_map> |
34 | | #include <vector> |
35 | | #include <o3tl/hash_combine.hxx> |
36 | | |
37 | | // virtual resolution of the PDF OutputDev in dpi |
38 | 0 | #define PDFI_OUTDEV_RESOLUTION 7200 |
39 | | |
40 | | namespace com::sun::star::task { class XInteractionHandler; } |
41 | | |
42 | | namespace pdfi |
43 | | { |
44 | | typedef std::unordered_map< OUString, OUString > PropertyMap; |
45 | | typedef sal_Int32 ImageId; |
46 | | |
47 | | /// What to do with a polygon. values can be ORed together |
48 | | enum PolygonAction { PATH_STROKE=1, PATH_FILL=2, PATH_EOFILL=4 }; |
49 | | |
50 | | OUString unitMMString( double fMM ); |
51 | | OUString convertPixelToUnitString( double fPix ); |
52 | | |
53 | | inline double convPx2mm( double fPix ) |
54 | 0 | { |
55 | 0 | const double px2mm = 25.4/PDFI_OUTDEV_RESOLUTION; |
56 | 0 | fPix *= px2mm; |
57 | 0 | return fPix; |
58 | 0 | } |
59 | | |
60 | | inline double convmm2Px( double fMM ) |
61 | 0 | { |
62 | 0 | const double mm2px = PDFI_OUTDEV_RESOLUTION/25.4; |
63 | 0 | fMM *= mm2px; |
64 | 0 | return fMM; |
65 | 0 | } |
66 | | |
67 | | /// round to 2 decimal places |
68 | | inline double convPx2mmPrec2( double fPix ) |
69 | 0 | { |
70 | 0 | constexpr double px2mm = 25.4/PDFI_OUTDEV_RESOLUTION; |
71 | 0 | double mm = fPix * ( px2mm * 100); |
72 | 0 | return std::floor(mm) / 100; |
73 | 0 | } |
74 | | |
75 | | /// Convert color to "#FEFEFE" color notation |
76 | | OUString getColorString( const css::rendering::ARGBColor& ); |
77 | | OUString getPercentString(double value); |
78 | | |
79 | | double GetAverageTransformationScale(const basegfx::B2DHomMatrix& matrix); |
80 | | void FillDashStyleProps(PropertyMap& props, const std::vector<double>& dashArray, double scale); |
81 | | |
82 | | struct FontAttrHash |
83 | | { |
84 | | size_t operator()(const FontAttributes& rFont ) const |
85 | 0 | { |
86 | 0 | std::size_t seed = 0; |
87 | 0 | o3tl::hash_combine(seed, rFont.familyName.hashCode()); |
88 | 0 | o3tl::hash_combine(seed, rFont.fontWeight); |
89 | 0 | o3tl::hash_combine(seed, rFont.isItalic); |
90 | 0 | o3tl::hash_combine(seed, rFont.isUnderline); |
91 | 0 | o3tl::hash_combine(seed, rFont.isOutline); |
92 | 0 | o3tl::hash_combine(seed, rFont.size); |
93 | 0 | return seed; |
94 | 0 | } |
95 | | }; |
96 | | |
97 | | struct GraphicsContext |
98 | | { |
99 | | css::rendering::ARGBColor LineColor; |
100 | | css::rendering::ARGBColor FillColor; |
101 | | basegfx::B2DLineJoin LineJoin; |
102 | | sal_Int8 LineCap; |
103 | | sal_Int8 BlendMode; |
104 | | double Flatness; |
105 | | double LineWidth; |
106 | | double MiterLimit; |
107 | | std::vector<double> DashArray; |
108 | | sal_Int32 FontId; |
109 | | sal_Int32 TextRenderMode; |
110 | | basegfx::B2DHomMatrix Transformation; |
111 | | basegfx::B2DPolyPolygon Clip; |
112 | | |
113 | | GraphicsContext() : |
114 | 0 | LineColor(), |
115 | 0 | FillColor(), |
116 | 0 | LineJoin(basegfx::B2DLineJoin::NONE), |
117 | 0 | LineCap(0), |
118 | 0 | BlendMode(0), |
119 | 0 | Flatness(0.0), |
120 | 0 | LineWidth(1.0), |
121 | 0 | MiterLimit(10.0), |
122 | 0 | DashArray(), |
123 | 0 | FontId(0), |
124 | 0 | TextRenderMode(0), |
125 | 0 | Transformation(), |
126 | 0 | Clip() |
127 | 0 | {} |
128 | | |
129 | | bool operator==(const GraphicsContext& rRight ) const |
130 | 0 | { |
131 | 0 | return LineColor.Red == rRight.LineColor.Red && |
132 | 0 | LineColor.Green == rRight.LineColor.Green && |
133 | 0 | LineColor.Blue == rRight.LineColor.Blue && |
134 | 0 | LineColor.Alpha == rRight.LineColor.Alpha && |
135 | 0 | FillColor.Red == rRight.FillColor.Red && |
136 | 0 | FillColor.Green == rRight.FillColor.Green && |
137 | 0 | FillColor.Blue == rRight.FillColor.Blue && |
138 | 0 | FillColor.Alpha == rRight.FillColor.Alpha && |
139 | 0 | LineJoin == rRight.LineJoin && |
140 | 0 | LineCap == rRight.LineCap && |
141 | 0 | BlendMode == rRight.BlendMode && |
142 | 0 | LineWidth == rRight.LineWidth && |
143 | 0 | Flatness == rRight.Flatness && |
144 | 0 | MiterLimit == rRight.MiterLimit && |
145 | 0 | DashArray == rRight.DashArray && |
146 | 0 | FontId == rRight.FontId && |
147 | 0 | TextRenderMode == rRight.TextRenderMode && |
148 | 0 | Transformation == rRight.Transformation && |
149 | 0 | Clip == rRight.Clip; |
150 | 0 | } |
151 | | |
152 | | OUString GetLineJoinString() const |
153 | 0 | { |
154 | 0 | switch (LineJoin) |
155 | 0 | { |
156 | 0 | default: |
157 | 0 | case basegfx::B2DLineJoin::Miter: |
158 | 0 | return u"miter"_ustr; |
159 | 0 | case basegfx::B2DLineJoin::Round: |
160 | 0 | return u"round"_ustr; |
161 | 0 | case basegfx::B2DLineJoin::Bevel: |
162 | 0 | return u"bevel"_ustr; |
163 | 0 | } |
164 | 0 | } |
165 | | |
166 | | OUString GetLineCapString() const |
167 | 0 | { |
168 | 0 | switch (LineCap) |
169 | 0 | { |
170 | 0 | default: |
171 | 0 | case css::rendering::PathCapType::BUTT: |
172 | 0 | return u"butt"_ustr; |
173 | 0 | case css::rendering::PathCapType::ROUND: |
174 | 0 | return u"round"_ustr; |
175 | 0 | case css::rendering::PathCapType::SQUARE: |
176 | 0 | return u"square"_ustr; |
177 | 0 | } |
178 | 0 | } |
179 | | |
180 | | bool isRotatedOrSkewed() const |
181 | 0 | { return Transformation.get( 0, 1 ) != 0.0 || |
182 | 0 | Transformation.get( 1, 0 ) != 0.0; } |
183 | | }; |
184 | | |
185 | | struct GraphicsContextHash |
186 | | { |
187 | | size_t operator()(const GraphicsContext& rGC ) const |
188 | 0 | { |
189 | 0 | std::size_t seed = 0; |
190 | 0 | o3tl::hash_combine(seed, rGC.LineColor.Red); |
191 | 0 | o3tl::hash_combine(seed, rGC.LineColor.Green); |
192 | 0 | o3tl::hash_combine(seed, rGC.LineColor.Blue); |
193 | 0 | o3tl::hash_combine(seed, rGC.LineColor.Alpha); |
194 | 0 | o3tl::hash_combine(seed, rGC.FillColor.Red); |
195 | 0 | o3tl::hash_combine(seed, rGC.FillColor.Green); |
196 | 0 | o3tl::hash_combine(seed, rGC.FillColor.Blue); |
197 | 0 | o3tl::hash_combine(seed, rGC.FillColor.Alpha); |
198 | 0 | o3tl::hash_combine(seed, rGC.LineJoin); |
199 | 0 | o3tl::hash_combine(seed, rGC.LineCap); |
200 | 0 | o3tl::hash_combine(seed, rGC.BlendMode); |
201 | 0 | o3tl::hash_combine(seed, rGC.LineWidth); |
202 | 0 | o3tl::hash_combine(seed, rGC.Flatness); |
203 | 0 | o3tl::hash_combine(seed, rGC.MiterLimit); |
204 | 0 | o3tl::hash_combine(seed, rGC.DashArray.size()); |
205 | 0 | o3tl::hash_combine(seed, rGC.FontId); |
206 | 0 | o3tl::hash_combine(seed, rGC.TextRenderMode); |
207 | 0 | o3tl::hash_combine(seed, rGC.Transformation.get( 0, 0 )); |
208 | 0 | o3tl::hash_combine(seed, rGC.Transformation.get( 1, 0 )); |
209 | 0 | o3tl::hash_combine(seed, rGC.Transformation.get( 0, 1 )); |
210 | 0 | o3tl::hash_combine(seed, rGC.Transformation.get( 1, 1 )); |
211 | 0 | o3tl::hash_combine(seed, rGC.Transformation.get( 0, 2 )); |
212 | 0 | o3tl::hash_combine(seed, rGC.Transformation.get( 1, 2 )); |
213 | 0 | o3tl::hash_combine(seed, rGC.Clip.count() ? rGC.Clip.getB2DPolygon(0).count() : 0); |
214 | 0 | return seed; |
215 | 0 | } |
216 | | }; |
217 | | } |
218 | | |
219 | | #endif |
220 | | |
221 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |