1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef INCLUDED_PDFI_HELPER_HXX
25 #define INCLUDED_PDFI_HELPER_HXX
26 
27 #include "contentsink.hxx"
28 
29 #include <rtl/ustring.hxx>
30 #include <rtl/math.h>
31 #include <basegfx/matrix/b2dhommatrix.hxx>
32 #include <basegfx/polygon/b2dpolypolygon.hxx>
33 #include <basegfx/polygon/b2dpolygon.hxx>
34 #include <com/sun/star/rendering/XColorSpace.hpp>
35 
36 #include <vector>
37 #include <hash_map>
38 
39 // virtual resolution of the PDF OutputDev in dpi
40 #define PDFI_OUTDEV_RESOLUTION 7200
41 
42 namespace com { namespace sun { namespace star { namespace task
43 { class XInteractionHandler; }}}}
44 
45 namespace pdfi
46 {
47     typedef std::hash_map< rtl::OUString, rtl::OUString, rtl::OUStringHash > PropertyMap;
48     typedef sal_Int32 ImageId;
49 
50     /// What to do with a polygon. values can be ORed together
51     enum PolygonAction { PATH_STROKE=1, PATH_FILL=2, PATH_EOFILL=4 };
52 
53     rtl::OUString unitMMString( double fMM );
54     rtl::OUString convertPixelToUnitString( double fPix );
55 
convPx2mm(double fPix)56     inline double convPx2mm( double fPix )
57     {
58         const double px2mm = 25.4/PDFI_OUTDEV_RESOLUTION;
59         fPix *= px2mm;
60         return fPix;
61     }
62 
convmm2Px(double fMM)63     inline double convmm2Px( double fMM )
64     {
65         const double mm2px = PDFI_OUTDEV_RESOLUTION/25.4;
66         fMM *= mm2px;
67         return fMM;
68     }
69 
convPx2mmPrec2(double fPix)70     inline double convPx2mmPrec2( double fPix )
71     {
72         return rtl_math_round( convPx2mm( fPix ), 2, rtl_math_RoundingMode_Floor );
73     }
74 
75     /// Convert color to "#FEFEFE" color notation
76     rtl::OUString getColorString( const ::com::sun::star::rendering::ARGBColor& );
77 
78     struct FontAttrHash
79     {
operator ()pdfi::FontAttrHash80         size_t operator()(const FontAttributes& rFont ) const
81         {
82             return (size_t)rFont.familyName.hashCode()
83                 ^  size_t(rFont.isBold ? 0xd47be593 : 0)
84                 ^  size_t(rFont.isItalic ? 0x1efd51a1 : 0)
85                 ^  size_t(rFont.isUnderline ? 0xf6bd325a : 0)
86                 ^  size_t(rFont.isOutline ?  0x12345678 : 0)
87                 ^  size_t(rFont.size)
88                 ;
89         }
90     };
91 
92     struct GraphicsContext
93     {
94         ::com::sun::star::rendering::ARGBColor     LineColor;
95         ::com::sun::star::rendering::ARGBColor     FillColor;
96         sal_Int8                                   LineJoin;
97         sal_Int8                                   LineCap;
98         sal_Int8                                   BlendMode;
99         double                                     Flatness;
100         double                                     LineWidth;
101         double                                     MiterLimit;
102         std::vector<double>                        DashArray;
103         sal_Int32                                  FontId;
104         sal_Int32                                  TextRenderMode;
105         basegfx::B2DHomMatrix                      Transformation;
106         basegfx::B2DPolyPolygon                    Clip;
107 
GraphicsContextpdfi::GraphicsContext108         GraphicsContext() :
109             LineColor(),
110             FillColor(),
111             LineJoin(0),
112             LineCap(0),
113             BlendMode(0),
114             Flatness(0.0),
115             LineWidth(1.0),
116             MiterLimit(10.0),
117             DashArray(),
118             FontId(0),
119             TextRenderMode(0),
120             Transformation(),
121             Clip()
122         {}
123 
operator ==pdfi::GraphicsContext124         bool operator==(const GraphicsContext& rRight ) const
125         {
126             return LineColor.Red == rRight.LineColor.Red &&
127                 LineColor.Green == rRight.LineColor.Green &&
128                 LineColor.Blue == rRight.LineColor.Blue &&
129                 LineColor.Alpha == rRight.LineColor.Alpha &&
130                 FillColor.Red == rRight.FillColor.Red &&
131                 FillColor.Green == rRight.FillColor.Green &&
132                 FillColor.Blue == rRight.FillColor.Blue &&
133                 FillColor.Alpha == rRight.FillColor.Alpha &&
134                 LineJoin  == rRight.LineJoin &&
135                 LineCap   == rRight.LineCap &&
136                 BlendMode == rRight.BlendMode &&
137                 LineWidth == rRight.LineWidth &&
138                 Flatness == rRight.Flatness &&
139                 MiterLimit == rRight.MiterLimit &&
140                 DashArray == rRight.DashArray &&
141                 FontId    == rRight.FontId &&
142                 TextRenderMode == rRight.TextRenderMode &&
143                 Transformation == rRight.Transformation &&
144                 Clip == rRight.Clip;
145         }
146 
isRotatedOrSkewedpdfi::GraphicsContext147         bool isRotatedOrSkewed() const
148         { return Transformation.get( 0, 1 ) != 0.0 ||
149                 Transformation.get( 1, 0 ) != 0.0; }
150     };
151 
152     struct GraphicsContextHash
153     {
operator ()pdfi::GraphicsContextHash154         size_t operator()(const GraphicsContext& rGC ) const
155         {
156             return size_t(rGC.LineColor.Red)
157                 ^  size_t(rGC.LineColor.Green)
158                 ^  size_t(rGC.LineColor.Blue)
159                 ^  size_t(rGC.LineColor.Alpha)
160                 ^  size_t(rGC.FillColor.Red)
161                 ^  size_t(rGC.FillColor.Green)
162                 ^  size_t(rGC.FillColor.Blue)
163                 ^  size_t(rGC.FillColor.Alpha)
164                 ^  size_t(rGC.LineJoin)
165                 ^  size_t(rGC.LineCap)
166                 ^  size_t(rGC.BlendMode)
167                 ^  size_t(rGC.LineWidth)
168                 ^  size_t(rGC.Flatness)
169                 ^  size_t(rGC.MiterLimit)
170                 ^  rGC.DashArray.size()
171                 ^  size_t(rGC.FontId)
172                 ^  size_t(rGC.TextRenderMode)
173                 ^  size_t(rGC.Transformation.get( 0, 0 ))
174                 ^  size_t(rGC.Transformation.get( 1, 0 ))
175                 ^  size_t(rGC.Transformation.get( 0, 1 ))
176                 ^  size_t(rGC.Transformation.get( 1, 1 ))
177                 ^  size_t(rGC.Transformation.get( 0, 2 ))
178                 ^  size_t(rGC.Transformation.get( 1, 2 ))
179                 ^  size_t(rGC.Clip.count() ? rGC.Clip.getB2DPolygon(0).count() : 0)
180                 ;
181         }
182     };
183 
184     /** retrieve password from user
185      */
186     bool getPassword( const ::com::sun::star::uno::Reference<
187                             ::com::sun::star::task::XInteractionHandler >& xHandler,
188                       rtl::OUString&                                       rOutPwd,
189                       bool                                                 bFirstTry,
190                       const rtl::OUString&                                 rDocName
191                       );
192 }
193 
194 #define USTR(x) rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
195 
196 #endif
197