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