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