xref: /AOO41X/main/filter/source/svg/svgwriter.hxx (revision bac862b165139ae201da2a815e2d7d3b3cf83a2d)
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 SVGWRITER_HXX
25 #define SVGWRITER_HXX
26 
27 #include <cppuhelper/weak.hxx>
28 #include <rtl/ustring.hxx>
29 #include <tools/debug.hxx>
30 #include <tools/stream.hxx>
31 #include <tools/string.hxx>
32 #include <tools/urlobj.hxx>
33 #include <tools/stack.hxx>
34 #ifndef _SALBTYPE_HXX
35 #include <vcl/salbtype.hxx>
36 #endif
37 #ifndef _GDIMTF_HXX
38 #include <vcl/gdimtf.hxx>
39 #endif
40 #ifndef _METAACT_HXX
41 #include <vcl/metaact.hxx>
42 #endif
43 #ifndef _METAACT_HXX
44 #include <vcl/metric.hxx>
45 #endif
46 #ifndef _VIRDEV_HXX
47 #include <vcl/virdev.hxx>
48 #endif
49 #ifndef _CVTGRF_HXX
50 #include <vcl/cvtgrf.hxx>
51 #endif
52 #include <vcl/graphictools.hxx>
53 #include <xmloff/xmlexp.hxx>
54 #include <xmloff/nmspmap.hxx>
55 
56 #include <com/sun/star/uno/Reference.h>
57 #include <com/sun/star/uno/RuntimeException.hpp>
58 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
59 #include <com/sun/star/lang/XComponent.hpp>
60 #include <com/sun/star/registry/XRegistryKey.hpp>
61 #include <com/sun/star/lang/XComponent.hpp>
62 #include <com/sun/star/io/XInputStream.hpp>
63 #include <com/sun/star/io/XOutputStream.hpp>
64 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
65 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
66 #include <com/sun/star/i18n/CharacterIteratorMode.hpp>
67 #include <com/sun/star/i18n/XBreakIterator.hpp>
68 #include <com/sun/star/svg/XSVGWriter.hpp>
69 #include <com/sun/star/lang/XInitialization.hpp>
70 
71 // -----------------------------------------------------------------------------
72 
73 #define _SVG_WRITE_EXTENTS      1
74 #undef _SVG_WRITE_TEXT_DESC
75 #undef _SVG_USE_CONFIG
76 
77 // -----------------------------------------------------------------------------
78 
79 #define NMSP_CPPU               cppu
80 #define NMSP_UNO                com::sun::star::uno
81 #define NMSP_LANG               com::sun::star::lang
82 #define NMSP_SAX                com::sun::star::xml::sax
83 #define NMSP_SVG                com::sun::star::svg
84 #define NMSP_REGISTRY           com::sun::star::registry
85 
86 
87 #define REF( _def_Obj )         NMSP_UNO::Reference< _def_Obj >
88 #define SEQ( _def_Obj )         NMSP_UNO::Sequence< _def_Obj >
89 #define B2UCONST( _def_pChar )  (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(_def_pChar )))
90 #define SVG_DTD_STRING          B2UCONST( "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">" )
91 #define SVG_TINY_DTD_STRING     B2UCONST( "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG Tiny 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd\">" )
92 
93 #define SVGWRITER_WRITE_NONE        0x00000000
94 #define SVGWRITER_WRITE_FILL        0x00000001
95 #define SVGWRITER_WRITE_TEXT        0x00000002
96 #define SVGWRITER_NO_SHAPE_COMMENTS 0x01000000
97 
98 // ----------------------
99 // - SVGAttributeWriter -
100 // ----------------------
101 
102 class SVGActionWriter;
103 class SVGExport;
104 class SVGFontExport;
105 
106 class SVGAttributeWriter
107 {
108 private:
109 
110     Font                    maCurFont;
111     Color                   maCurLineColor;
112     Color                   maCurFillColor;
113     SVGExport&              mrExport;
114     SVGFontExport&          mrFontExport;
115     SvXMLElementExport*     mpElemFont;
116     SvXMLElementExport*     mpElemPaint;
117 
118                             SVGAttributeWriter();
119 
120     void                    ImplGetColorStr( const Color& rColor, ::rtl::OUString& rColorStr );
121     double                  ImplRound( double fVal, sal_Int32 nDecs = 3 );
122 
123 public:
124 
125                             SVGAttributeWriter( SVGExport& rExport, SVGFontExport& rFontExport );
126     virtual                 ~SVGAttributeWriter();
127 
128     void                    AddColorAttr( const char* pColorAttrName, const char* pColorOpacityAttrName, const Color& rColor );
129     void                    AddGradientDef( const Rectangle& rObjRect,const Gradient& rGradient, ::rtl::OUString& rGradientId );
130     void                    AddPaintAttr( const Color& rLineColor, const Color& rFillColor,
131                                           const Rectangle* pObjBoundRect = NULL, const Gradient* pFillGradient = NULL );
132 
133     void                    SetFontAttr( const Font& rFont );
134 };
135 
136 // ----------------------
137 // - SVGShapeDescriptor -
138 // ----------------------
139 
140 struct SVGShapeDescriptor
141 {
142     PolyPolygon                         maShapePolyPoly;
143     Color                               maShapeFillColor;
144     Color                               maShapeLineColor;
145     sal_Int32                           mnStrokeWidth;
146     SvtGraphicStroke::DashArray         maDashArray;
147     ::std::auto_ptr< Gradient >         mapShapeGradient;
148     ::rtl::OUString                     maId;
149 
150     // added support for LineJoin and LineCap
151     basegfx::B2DLineJoin                maLineJoin;
152     com::sun::star::drawing::LineCap    maLineCap;
153 
154     // -------------------------------------------------------------------------
155 
SVGShapeDescriptorSVGShapeDescriptor156     SVGShapeDescriptor() :
157         maShapeFillColor( Color( COL_TRANSPARENT ) ),
158         maShapeLineColor( Color( COL_TRANSPARENT ) ),
159         mnStrokeWidth( 0 ),
160         maLineJoin(basegfx::B2DLINEJOIN_MITER), // miter is Svg 'stroke-linejoin' default
161         maLineCap(com::sun::star::drawing::LineCap_BUTT) // butt is Svg 'stroke-linecap' default
162     {
163     }
164 };
165 
166 // -------------------
167 // - SVGActionWriter -
168 // -------------------
169 
170 class SVGAttributeWriter;
171 class SVGExport;
172 class GDIMetaFile;
173 
174 class SVGActionWriter
175 {
176 private:
177 
178     Stack                                   maContextStack;
179     ::std::auto_ptr< SVGShapeDescriptor >   mapCurShape;
180     SVGExport&                              mrExport;
181     SVGFontExport&                          mrFontExport;
182     SVGAttributeWriter*                     mpContext;
183     VirtualDevice*                          mpVDev;
184     MapMode                                 maTargetMapMode;
185     sal_uInt32                              mnInnerMtfCount;
186     sal_Bool                                mbDestroyVDev;
187     sal_Bool                                mbPaintAttrChanged;
188     sal_Bool                                mbFontAttrChanged;
189     sal_Bool                                mbClipAttrChanged;
190 
ImplAcquireContext()191     SVGAttributeWriter*     ImplAcquireContext() { maContextStack.Push( mpContext = new SVGAttributeWriter( mrExport, mrFontExport ) ); return mpContext; }
ImplReleaseContext()192     void                    ImplReleaseContext() { delete (SVGAttributeWriter*) maContextStack.Pop(); mpContext = (SVGAttributeWriter*) maContextStack.Top(); }
193 
194     long                    ImplMap( sal_Int32 nVal ) const;
195     Point&                  ImplMap( const Point& rPt, Point& rDstPt ) const;
196     Size&                   ImplMap( const Size& rSz, Size& rDstSz ) const;
197     Rectangle&              ImplMap( const Rectangle& rRect, Rectangle& rDstRect ) const;
198     Polygon&                ImplMap( const Polygon& rPoly, Polygon& rDstPoly ) const;
199     PolyPolygon&            ImplMap( const PolyPolygon& rPolyPoly, PolyPolygon& rDstPolyPoly ) const;
200 
201     void                    ImplWriteLine( const Point& rPt1, const Point& rPt2, const Color* pLineColor = NULL,
202                                            sal_Bool bApplyMapping = sal_True );
203     void                    ImplWriteRect( const Rectangle& rRect, long nRadX = 0, long nRadY = 0,
204                                            sal_Bool bApplyMapping = sal_True );
205     void                    ImplWriteEllipse( const Point& rCenter, long nRadX, long nRadY,
206                                               sal_Bool bApplyMapping = sal_True );
207     void                    ImplWritePolyPolygon( const PolyPolygon& rPolyPoly, sal_Bool bLineOnly,
208                                                   sal_Bool bApplyMapping = sal_True );
209     void                    ImplWriteShape( const SVGShapeDescriptor& rShape, sal_Bool bApplyMapping = sal_True );
210     void                    ImplWriteGradientEx( const PolyPolygon& rPolyPoly, const Gradient& rGradient, sal_uInt32 nWriteFlags,
211                                                  sal_Bool bApplyMapping = sal_True );
212     void                    ImplWriteText( const Point& rPos, const String& rText, const sal_Int32* pDXArray, long nWidth,
213                                            sal_Bool bApplyMapping = sal_True );
214     void                    ImplWriteBmp( const BitmapEx& rBmpEx, const Point& rPt, const Size& rSz, const Point& rSrcPt, const Size& rSrcSz,
215                                           sal_Bool bApplyMapping = sal_True );
216 
217     void                    ImplCheckFontAttributes();
218     void                    ImplCheckPaintAttributes();
219 
220     void                    ImplWriteActions( const GDIMetaFile& rMtf, sal_uInt32 nWriteFlags, const ::rtl::OUString* pElementId );
221 
222 public:
223 
224     static ::rtl::OUString  GetPathString( const PolyPolygon& rPolyPoly, sal_Bool bLine );
225 
226 public:
227 
228                             SVGActionWriter( SVGExport& rExport, SVGFontExport& rFontExport );
229     virtual                 ~SVGActionWriter();
230 
231     void                    WriteMetaFile( const Point& rPos100thmm,
232                                            const Size& rSize100thmm,
233                                            const GDIMetaFile& rMtf,
234                                            sal_uInt32 nWriteFlags,
235                                            const ::rtl::OUString* pElementId = NULL );
236 };
237 
238 class SVGWriter : public NMSP_CPPU::OWeakObject, NMSP_SVG::XSVGWriter, com::sun::star::lang::XInitialization
239 {
240 private:
241 
242     REF( NMSP_LANG::XMultiServiceFactory )                                  mxFact;
243     com::sun::star::uno::Sequence< com::sun::star::beans::PropertyValue >   maFilterData;
244 
245                                             SVGWriter();
246 
247 public:
248 
249                                             SVGWriter( const REF( NMSP_LANG::XMultiServiceFactory )& rxMgr );
250     virtual                                 ~SVGWriter();
251 
252     // XInterface
253     virtual NMSP_UNO::Any SAL_CALL          queryInterface( const NMSP_UNO::Type & rType ) throw( NMSP_UNO::RuntimeException );
254     virtual void SAL_CALL                   acquire() throw();
255     virtual void SAL_CALL                   release() throw();
256 
257     // XSVGWriter
258     virtual void SAL_CALL                   write( const REF( NMSP_SAX::XDocumentHandler )& rxDocHandler,
259                                                    const SEQ( sal_Int8 )& rMtfSeq ) throw( NMSP_UNO::RuntimeException );
260 
261     // ::com::sun::star::lang::XInitialization
262     void SAL_CALL initialize( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments ) throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
263 
264 };
265 
266 ::rtl::OUString SVGWriter_getImplementationName ()
267     throw ( ::com::sun::star::uno::RuntimeException );
268 
269 // -----------------------------------------------------------------------------
270 
271 sal_Bool SAL_CALL SVGWriter_supportsService( const ::rtl::OUString& ServiceName )
272     throw ( ::com::sun::star::uno::RuntimeException );
273 
274 // -----------------------------------------------------------------------------
275 
276 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL SVGWriter_getSupportedServiceNames(  )
277     throw ( ::com::sun::star::uno::RuntimeException );
278 
279 // -----------------------------------------------------------------------------
280 
281 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
282     SAL_CALL SVGWriter_createInstance( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > & rSMgr)
283         throw ( ::com::sun::star::uno::Exception );
284 
285 #endif
286