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 #define BASEGFX_STATICLIBRARY
28 
29 #include "contentsink.hxx"
30 
31 #include <rtl/ustring.hxx>
32 #include <rtl/math.h>
33 #include <basegfx/matrix/b2dhommatrix.hxx>
34 #include <basegfx/polygon/b2dpolypolygon.hxx>
35 #include <basegfx/polygon/b2dpolygon.hxx>
36 #include <com/sun/star/rendering/XColorSpace.hpp>
37 
38 #include <vector>
39 #include <hash_map>
40 
41 // virtual resolution of the PDF OutputDev in dpi
42 #define PDFI_OUTDEV_RESOLUTION 7200
43 
44 namespace com { namespace sun { namespace star { namespace task
45 { class XInteractionHandler; }}}}
46 
47 namespace pdfi
48 {
49     typedef std::hash_map< rtl::OUString, rtl::OUString, rtl::OUStringHash > PropertyMap;
50     typedef sal_Int32 ImageId;
51 
52     /// What to do with a polygon. values can be ORed together
53     enum PolygonAction { PATH_STROKE=1, PATH_FILL=2, PATH_EOFILL=4 };
54 
55     rtl::OUString unitMMString( double fMM );
56     rtl::OUString convertPixelToUnitString( double fPix );
57 
convPx2mm(double fPix)58     inline double convPx2mm( double fPix )
59     {
60         const double px2mm = 25.4/PDFI_OUTDEV_RESOLUTION;
61         fPix *= px2mm;
62         return fPix;
63     }
64 
convmm2Px(double fMM)65     inline double convmm2Px( double fMM )
66     {
67         const double mm2px = PDFI_OUTDEV_RESOLUTION/25.4;
68         fMM *= mm2px;
69         return fMM;
70     }
71 
convPx2mmPrec2(double fPix)72     inline double convPx2mmPrec2( double fPix )
73     {
74         return rtl_math_round( convPx2mm( fPix ), 2, rtl_math_RoundingMode_Floor );
75     }
76 
77     /// Convert color to "#FEFEFE" color notation
78     rtl::OUString getColorString( const ::com::sun::star::rendering::ARGBColor& );
79 
80     struct FontAttrHash
81     {
operator ()pdfi::FontAttrHash82         size_t operator()(const FontAttributes& rFont ) const
83         {
84             return (size_t)rFont.familyName.hashCode()
85                 ^  size_t(rFont.isBold ? 0xd47be593 : 0)
86                 ^  size_t(rFont.isItalic ? 0x1efd51a1 : 0)
87                 ^  size_t(rFont.isUnderline ? 0xf6bd325a : 0)
88                 ^  size_t(rFont.isOutline ?  0x12345678 : 0)
89                 ^  size_t(rFont.size)
90                 ;
91         }
92     };
93 
94     struct GraphicsContext
95     {
96         ::com::sun::star::rendering::ARGBColor     LineColor;
97         ::com::sun::star::rendering::ARGBColor     FillColor;
98         sal_Int8                                   LineJoin;
99         sal_Int8                                   LineCap;
100         sal_Int8                                   BlendMode;
101         double                                     Flatness;
102         double                                     LineWidth;
103         double                                     MiterLimit;
104         std::vector<double>                        DashArray;
105         sal_Int32                                  FontId;
106         sal_Int32                                  TextRenderMode;
107         basegfx::B2DHomMatrix                      Transformation;
108         basegfx::B2DPolyPolygon                    Clip;
109 
GraphicsContextpdfi::GraphicsContext110         GraphicsContext() :
111             LineColor(),
112             FillColor(),
113             LineJoin(0),
114             LineCap(0),
115             BlendMode(0),
116             Flatness(0.0),
117             LineWidth(1.0),
118             MiterLimit(10.0),
119             DashArray(),
120             FontId(0),
121             TextRenderMode(0),
122             Transformation(),
123             Clip()
124         {}
125 
operator ==pdfi::GraphicsContext126         bool operator==(const GraphicsContext& rRight ) const
127         {
128             return LineColor.Red == rRight.LineColor.Red &&
129                 LineColor.Green == rRight.LineColor.Green &&
130                 LineColor.Blue == rRight.LineColor.Blue &&
131                 LineColor.Alpha == rRight.LineColor.Alpha &&
132                 FillColor.Red == rRight.FillColor.Red &&
133                 FillColor.Green == rRight.FillColor.Green &&
134                 FillColor.Blue == rRight.FillColor.Blue &&
135                 FillColor.Alpha == rRight.FillColor.Alpha &&
136                 LineJoin  == rRight.LineJoin &&
137                 LineCap   == rRight.LineCap &&
138                 BlendMode == rRight.BlendMode &&
139                 LineWidth == rRight.LineWidth &&
140                 Flatness == rRight.Flatness &&
141                 MiterLimit == rRight.MiterLimit &&
142                 DashArray == rRight.DashArray &&
143                 FontId    == rRight.FontId &&
144                 TextRenderMode == rRight.TextRenderMode &&
145                 Transformation == rRight.Transformation &&
146                 Clip == rRight.Clip;
147         }
148 
isRotatedOrSkewedpdfi::GraphicsContext149         bool isRotatedOrSkewed() const
150         { return Transformation.get( 0, 1 ) != 0.0 ||
151                 Transformation.get( 1, 0 ) != 0.0; }
152     };
153 
154     struct GraphicsContextHash
155     {
operator ()pdfi::GraphicsContextHash156         size_t operator()(const GraphicsContext& rGC ) const
157         {
158             return size_t(rGC.LineColor.Red)
159                 ^  size_t(rGC.LineColor.Green)
160                 ^  size_t(rGC.LineColor.Blue)
161                 ^  size_t(rGC.LineColor.Alpha)
162                 ^  size_t(rGC.FillColor.Red)
163                 ^  size_t(rGC.FillColor.Green)
164                 ^  size_t(rGC.FillColor.Blue)
165                 ^  size_t(rGC.FillColor.Alpha)
166                 ^  size_t(rGC.LineJoin)
167                 ^  size_t(rGC.LineCap)
168                 ^  size_t(rGC.BlendMode)
169                 ^  size_t(rGC.LineWidth)
170                 ^  size_t(rGC.Flatness)
171                 ^  size_t(rGC.MiterLimit)
172                 ^  rGC.DashArray.size()
173                 ^  size_t(rGC.FontId)
174                 ^  size_t(rGC.TextRenderMode)
175                 ^  size_t(rGC.Transformation.get( 0, 0 ))
176                 ^  size_t(rGC.Transformation.get( 1, 0 ))
177                 ^  size_t(rGC.Transformation.get( 0, 1 ))
178                 ^  size_t(rGC.Transformation.get( 1, 1 ))
179                 ^  size_t(rGC.Transformation.get( 0, 2 ))
180                 ^  size_t(rGC.Transformation.get( 1, 2 ))
181                 ^  size_t(rGC.Clip.count() ? rGC.Clip.getB2DPolygon(0).count() : 0)
182                 ;
183         }
184     };
185 
186     /** retrieve password from user
187      */
188     bool getPassword( const ::com::sun::star::uno::Reference<
189                             ::com::sun::star::task::XInteractionHandler >& xHandler,
190                       rtl::OUString&                                       rOutPwd,
191                       bool                                                 bFirstTry,
192                       const rtl::OUString&                                 rDocName
193                       );
194 }
195 
196 #define USTR(x) rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
197 
198 #endif
199