Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef INCLUDED_FILTER_SOURCE_SVG_SVGWRITER_HXX
21 : #define INCLUDED_FILTER_SOURCE_SVG_SVGWRITER_HXX
22 :
23 : #include <stack>
24 : #include <cppuhelper/implbase1.hxx>
25 : #include <rtl/ustring.hxx>
26 : #include <tools/stream.hxx>
27 : #include <vcl/gdimtf.hxx>
28 : #include <vcl/metaact.hxx>
29 : #include <vcl/metric.hxx>
30 : #include <vcl/virdev.hxx>
31 : #include <vcl/cvtgrf.hxx>
32 : #include <vcl/graphictools.hxx>
33 : #include <xmloff/xmlexp.hxx>
34 : #include <xmloff/nmspmap.hxx>
35 :
36 : #include <com/sun/star/uno/Reference.h>
37 : #include <com/sun/star/container/XEnumerationAccess.hpp>
38 : #include <com/sun/star/container/XContentEnumerationAccess.hpp>
39 : #include <com/sun/star/container/XEnumeration.hpp>
40 : #include <com/sun/star/container/XIndexReplace.hpp>
41 : #include <com/sun/star/lang/XServiceInfo.hpp>
42 : #include <com/sun/star/beans/XPropertySet.hpp>
43 : #include <com/sun/star/beans/XPropertySetInfo.hpp>
44 : #include <com/sun/star/uno/RuntimeException.hpp>
45 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
46 : #include <com/sun/star/lang/XComponent.hpp>
47 : #include <com/sun/star/registry/XRegistryKey.hpp>
48 : #include <com/sun/star/io/XInputStream.hpp>
49 : #include <com/sun/star/io/XOutputStream.hpp>
50 : #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
51 : #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
52 : #include <com/sun/star/i18n/CharacterIteratorMode.hpp>
53 : #include <com/sun/star/i18n/XBreakIterator.hpp>
54 : #include <com/sun/star/drawing/XShape.hpp>
55 : #include <com/sun/star/text/XText.hpp>
56 : #include <com/sun/star/text/XTextContent.hpp>
57 : #include <com/sun/star/text/XTextRange.hpp>
58 : #include <com/sun/star/text/XTextField.hpp>
59 : #include <com/sun/star/style/NumberingType.hpp>
60 : #include <com/sun/star/svg/XSVGWriter.hpp>
61 :
62 :
63 :
64 : using namespace ::com::sun::star::uno;
65 : using namespace ::com::sun::star::container;
66 : using namespace ::com::sun::star::lang;
67 : using namespace ::com::sun::star::text;
68 : using namespace ::com::sun::star::drawing;
69 : using namespace ::com::sun::star::style;
70 : using namespace ::com::sun::star::svg;
71 : using namespace ::com::sun::star::xml::sax;
72 :
73 :
74 :
75 : #define SVG_DTD_STRING OUString( "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">" )
76 : #define SVG_TINY_DTD_STRING OUString( "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG Tiny 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd\">" )
77 :
78 : #define SVGWRITER_WRITE_NONE 0x00000000
79 : #define SVGWRITER_WRITE_FILL 0x00000001
80 : #define SVGWRITER_WRITE_TEXT 0x00000002
81 : #define SVGWRITER_NO_SHAPE_COMMENTS 0x01000000
82 :
83 :
84 : // - SVGAttributeWriter -
85 :
86 :
87 : class SVGActionWriter;
88 : class SVGExport;
89 : class SVGFontExport;
90 :
91 : class SVGAttributeWriter
92 : {
93 : private:
94 :
95 : Font maCurFont;
96 : Color maCurLineColor;
97 : Color maCurFillColor;
98 : SVGExport& mrExport;
99 : SVGFontExport& mrFontExport;
100 : SvXMLElementExport* mpElemFont;
101 : SvXMLElementExport* mpElemPaint;
102 :
103 : basegfx::B2DLineJoin maLineJoin;
104 : com::sun::star::drawing::LineCap maLineCap;
105 :
106 : SVGAttributeWriter();
107 :
108 : double ImplRound( double fVal, sal_Int32 nDecs = 3 );
109 :
110 : public:
111 :
112 : SVGAttributeWriter( SVGExport& rExport, SVGFontExport& rFontExport );
113 : virtual ~SVGAttributeWriter();
114 :
115 : void AddColorAttr( const char* pColorAttrName, const char* pColorOpacityAttrName, const Color& rColor );
116 : void AddGradientDef( const Rectangle& rObjRect,const Gradient& rGradient, OUString& rGradientId );
117 : void AddPaintAttr( const Color& rLineColor, const Color& rFillColor,
118 : const Rectangle* pObjBoundRect = NULL, const Gradient* pFillGradient = NULL );
119 :
120 : void SetFontAttr( const Font& rFont );
121 : void startFontSettings();
122 : void endFontSettings();
123 : void setFontFamily();
124 :
125 : static void ImplGetColorStr( const Color& rColor, OUString& rColorStr );
126 : };
127 :
128 0 : struct SVGShapeDescriptor
129 : {
130 : PolyPolygon maShapePolyPoly;
131 : Color maShapeFillColor;
132 : Color maShapeLineColor;
133 : sal_Int32 mnStrokeWidth;
134 : SvtGraphicStroke::DashArray maDashArray;
135 : ::std::auto_ptr< Gradient > mapShapeGradient;
136 : OUString maId;
137 :
138 : basegfx::B2DLineJoin maLineJoin;
139 : com::sun::star::drawing::LineCap maLineCap;
140 :
141 :
142 :
143 0 : SVGShapeDescriptor() :
144 : maShapeFillColor( Color( COL_TRANSPARENT ) ),
145 : maShapeLineColor( Color( COL_TRANSPARENT ) ),
146 : mnStrokeWidth( 0 ),
147 : maLineJoin(basegfx::B2DLINEJOIN_MITER), // miter is Svg 'stroke-linejoin' default
148 0 : maLineCap(com::sun::star::drawing::LineCap_BUTT) // butt is Svg 'stroke-linecap' default
149 : {
150 0 : }
151 : };
152 :
153 :
154 :
155 : class SVGAttributeWriter;
156 : class SVGExport;
157 : class GDIMetaFile;
158 :
159 :
160 :
161 : // - BulletListItemInfo -
162 :
163 0 : struct BulletListItemInfo
164 : {
165 : long nFontSize;
166 : Color aColor;
167 : Point aPos;
168 : sal_Unicode cBulletChar;
169 : };
170 :
171 :
172 : // - OUStringHasher -
173 :
174 :
175 : struct OUStringHasher
176 : {
177 0 : size_t operator()( const OUString& oustr ) const { return static_cast< size_t >( oustr.hashCode() ); }
178 : };
179 :
180 :
181 :
182 : // - SVGTextWriter -
183 :
184 : class SVGTextWriter
185 : {
186 : public:
187 : typedef ::boost::unordered_map< OUString, BulletListItemInfo, OUStringHasher > BulletListItemInfoMap;
188 :
189 : private:
190 : SVGExport& mrExport;
191 : SVGAttributeWriter* mpContext;
192 : VirtualDevice* mpVDev;
193 : sal_Bool mbIsTextShapeStarted;
194 : Reference<XText> mrTextShape;
195 : OUString msShapeId;
196 : Reference<XEnumeration> mrParagraphEnumeration;
197 : Reference<XTextContent> mrCurrentTextParagraph;
198 : Reference<XEnumeration> mrTextPortionEnumeration;
199 : Reference<XTextRange> mrCurrentTextPortion;
200 : const GDIMetaFile* mpTextEmbeddedBitmapMtf;
201 : MapMode* mpTargetMapMode;
202 : SvXMLElementExport* mpTextShapeElem;
203 : SvXMLElementExport* mpTextParagraphElem;
204 : SvXMLElementExport* mpTextPositionElem;
205 : sal_Int32 mnLeftTextPortionLength;
206 : Point maTextPos;
207 : long int mnTextWidth;
208 : sal_Bool mbPositioningNeeded;
209 : sal_Bool mbIsNewListItem;
210 : sal_Int16 meNumberingType;
211 : sal_Unicode mcBulletChar;
212 : BulletListItemInfoMap maBulletListItemMap;
213 : sal_Bool mbIsListLevelStyleImage;
214 : sal_Bool mbLineBreak;
215 : sal_Bool mbIsURLField;
216 : OUString msUrl;
217 : OUString msHyperlinkIdList;
218 : sal_Bool mbIsPlacehlolderShape;
219 : sal_Bool mbIWS;
220 : Font maCurrentFont;
221 : Font maParentFont;
222 :
223 : public:
224 : SVGTextWriter( SVGExport& rExport );
225 : virtual ~SVGTextWriter();
226 :
227 : sal_Int32 setTextPosition( const GDIMetaFile& rMtf, sal_uLong& nCurAction );
228 : void setTextProperties( const GDIMetaFile& rMtf, sal_uLong nCurAction );
229 : void addFontAttributes( sal_Bool bIsTextContainer );
230 :
231 : sal_Bool createParagraphEnumeration();
232 : sal_Bool nextParagraph();
233 : sal_Bool nextTextPortion();
234 :
235 0 : sal_Bool isTextShapeStarted() { return mbIsTextShapeStarted; }
236 : void startTextShape();
237 : void endTextShape();
238 : void startTextParagraph();
239 : void endTextParagraph();
240 : void startTextPosition( sal_Bool bExportX = sal_True, sal_Bool bExportY = sal_True);
241 : void endTextPosition();
242 : void implExportHyperlinkIds();
243 : void implWriteBulletChars();
244 : template< typename MetaBitmapActionType >
245 : void writeBitmapPlaceholder( const MetaBitmapActionType* pAction );
246 : void implWriteEmbeddedBitmaps();
247 : void writeTextPortion( const Point& rPos, const OUString& rText,
248 : sal_Bool bApplyMapping = sal_True );
249 : void implWriteTextPortion( const Point& rPos, const OUString& rText,
250 : Color aTextColor, sal_Bool bApplyMapping );
251 :
252 0 : void setVirtualDevice( VirtualDevice* pVDev, MapMode& rTargetMapMode )
253 : {
254 : if( !pVDev )
255 : OSL_FAIL( "SVGTextWriter::setVirtualDevice: invalid virtual device." );
256 0 : mpVDev = pVDev;
257 0 : mpTargetMapMode = &rTargetMapMode;
258 0 : }
259 :
260 0 : void setContext( SVGAttributeWriter* pContext )
261 : {
262 0 : mpContext = pContext;
263 0 : }
264 :
265 0 : void setTextShape( const Reference<XText>& rxText,
266 : const GDIMetaFile* pTextEmbeddedBitmapMtf )
267 : {
268 0 : mrTextShape.set( rxText );
269 0 : mpTextEmbeddedBitmapMtf = pTextEmbeddedBitmapMtf;
270 0 : }
271 :
272 : const Reference<XText>& getTextShape() const
273 : {
274 : return mrTextShape;
275 : }
276 :
277 :
278 : void setPlaceholderShapeFlag( sal_Bool bState )
279 : {
280 : mbIsPlacehlolderShape = bState;
281 : }
282 :
283 : private:
284 : void implMap( const Size& rSz, Size& rDstSz ) const;
285 : void implMap( const Point& rPt, Point& rDstPt ) const;
286 : void implSetCurrentFont();
287 : void implSetFontFamily();
288 :
289 : template< typename SubType >
290 : sal_Bool implGetTextPosition( const MetaAction* pAction, Point& raPos, sal_Bool& bEmpty );
291 : template< typename SubType >
292 : sal_Bool implGetTextPositionFromBitmap( const MetaAction* pAction, Point& raPos, sal_Bool& rbEmpty );
293 :
294 : void implRegisterInterface( const Reference< XInterface >& rxIf );
295 : const OUString & implGetValidIDFromInterface( const Reference< XInterface >& rxIf );
296 :
297 :
298 : };
299 :
300 :
301 : // - SVGActionWriter -
302 :
303 :
304 : class SVGActionWriter
305 : {
306 : private:
307 :
308 : sal_Int32 mnCurGradientId;
309 : sal_Int32 mnCurMaskId;
310 : sal_Int32 mnCurPatternId;
311 : ::std::stack< SVGAttributeWriter* > maContextStack;
312 : ::std::auto_ptr< SVGShapeDescriptor > mapCurShape;
313 : SVGExport& mrExport;
314 : SVGFontExport& mrFontExport;
315 : SVGAttributeWriter* mpContext;
316 : SVGTextWriter maTextWriter;
317 : VirtualDevice* mpVDev;
318 : MapMode maTargetMapMode;
319 : sal_uInt32 mnInnerMtfCount;
320 : sal_Bool mbDestroyVDev;
321 : sal_Bool mbPaintAttrChanged;
322 : sal_Bool mbFontAttrChanged;
323 : sal_Bool mbClipAttrChanged;
324 : sal_Bool mbIsPlacehlolderShape;
325 :
326 :
327 0 : SVGAttributeWriter* ImplAcquireContext()
328 : {
329 0 : maContextStack.push( mpContext = new SVGAttributeWriter( mrExport, mrFontExport ) );
330 0 : maTextWriter.setContext( mpContext );
331 0 : return mpContext;
332 : }
333 0 : void ImplReleaseContext()
334 : {
335 0 : if (!maContextStack.empty())
336 : {
337 0 : delete maContextStack.top();
338 0 : maContextStack.pop();
339 : }
340 0 : mpContext = (maContextStack.empty() ? NULL : maContextStack.top());
341 0 : maTextWriter.setContext( mpContext );
342 0 : }
343 :
344 : long ImplMap( sal_Int32 nVal ) const;
345 : Point& ImplMap( const Point& rPt, Point& rDstPt ) const;
346 : Size& ImplMap( const Size& rSz, Size& rDstSz ) const;
347 : Rectangle& ImplMap( const Rectangle& rRect, Rectangle& rDstRect ) const;
348 : Polygon& ImplMap( const Polygon& rPoly, Polygon& rDstPoly ) const;
349 : PolyPolygon& ImplMap( const PolyPolygon& rPolyPoly, PolyPolygon& rDstPolyPoly ) const;
350 :
351 : void ImplWriteLine( const Point& rPt1, const Point& rPt2, const Color* pLineColor = NULL,
352 : sal_Bool bApplyMapping = sal_True );
353 : void ImplWriteRect( const Rectangle& rRect, long nRadX = 0, long nRadY = 0,
354 : sal_Bool bApplyMapping = sal_True );
355 : void ImplWriteEllipse( const Point& rCenter, long nRadX, long nRadY,
356 : sal_Bool bApplyMapping = sal_True );
357 : void ImplWritePattern( const PolyPolygon& rPolyPoly, const Hatch* pHatch, const Gradient* pGradient, sal_uInt32 nWriteFlags );
358 : void ImplAddLineAttr( const LineInfo &rAttrs,
359 : sal_Bool bApplyMapping = sal_True );
360 : void ImplWritePolyPolygon( const PolyPolygon& rPolyPoly, sal_Bool bLineOnly,
361 : sal_Bool bApplyMapping = sal_True );
362 : void ImplWriteShape( const SVGShapeDescriptor& rShape, sal_Bool bApplyMapping = sal_True );
363 : void ImplWriteGradientEx( const PolyPolygon& rPolyPoly, const Gradient& rGradient, sal_uInt32 nWriteFlags);
364 : void ImplWriteGradientLinear( const PolyPolygon& rPolyPoly, const Gradient& rGradient );
365 : void ImplWriteGradientStop( const Color& rColor, double fOffset );
366 : Color ImplGetColorWithIntensity( const Color& rColor, sal_uInt16 nIntensity );
367 : Color ImplGetGradientColor( const Color& rStartColor, const Color& rEndColor, double fOffset );
368 : void ImplWriteMask( GDIMetaFile& rMtf, const Point& rDestPt, const Size& rDestSize, const Gradient& rGradient, sal_uInt32 nWriteFlags );
369 : void ImplWriteText( const Point& rPos, const OUString& rText, const sal_Int32* pDXArray, long nWidth, sal_Bool bApplyMapping = sal_True );
370 : void ImplWriteText( const Point& rPos, const OUString& rText, const sal_Int32* pDXArray, long nWidth, Color aTextColor, sal_Bool bApplyMapping );
371 : void ImplWriteBmp( const BitmapEx& rBmpEx, const Point& rPt, const Size& rSz, const Point& rSrcPt, const Size& rSrcSz,
372 : sal_Bool bApplyMapping = sal_True );
373 :
374 : void ImplCheckFontAttributes();
375 : void ImplCheckPaintAttributes();
376 :
377 : void ImplWriteActions( const GDIMetaFile& rMtf,
378 : sal_uInt32 nWriteFlags,
379 : const OUString* pElementId,
380 : const Reference< XShape >* pXShape = NULL,
381 : const GDIMetaFile* pTextEmbeddedBitmapMtf = NULL );
382 :
383 : Font ImplSetCorrectFontHeight() const;
384 :
385 : public:
386 :
387 : static OUString GetPathString( const PolyPolygon& rPolyPoly, sal_Bool bLine );
388 : static sal_uLong GetChecksum( const MetaAction* pAction );
389 :
390 : public:
391 :
392 : SVGActionWriter( SVGExport& rExport, SVGFontExport& rFontExport );
393 : virtual ~SVGActionWriter();
394 :
395 : void WriteMetaFile( const Point& rPos100thmm,
396 : const Size& rSize100thmm,
397 : const GDIMetaFile& rMtf,
398 : sal_uInt32 nWriteFlags,
399 : const OUString* pElementId = NULL,
400 : const Reference< XShape >* pXShape = NULL,
401 : const GDIMetaFile* pTextEmbeddedBitmapMtf = NULL );
402 : };
403 :
404 : class SVGWriter : public cppu::WeakImplHelper1< XSVGWriter >
405 : {
406 : private:
407 : Reference< XComponentContext > mxContext;
408 : Sequence< com::sun::star::beans::PropertyValue > maFilterData;
409 : SVGWriter();
410 :
411 : public:
412 : explicit SVGWriter( const Sequence<Any>& args,
413 : const Reference< XComponentContext >& rxCtx );
414 : virtual ~SVGWriter();
415 :
416 : // XSVGWriter
417 : virtual void SAL_CALL write( const Reference<XDocumentHandler>& rxDocHandler,
418 : const Sequence<sal_Int8>& rMtfSeq ) throw( RuntimeException, std::exception ) SAL_OVERRIDE;
419 : };
420 :
421 : #endif
422 :
423 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|