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 : vcl::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 vcl::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 : tools::PolyPolygon maShapePolyPoly;
131 : Color maShapeFillColor;
132 : Color maShapeLineColor;
133 : sal_Int32 mnStrokeWidth;
134 : SvtGraphicStroke::DashArray maDashArray;
135 : ::std::unique_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 :
173 : // - SVGTextWriter -
174 :
175 : class SVGTextWriter
176 : {
177 : public:
178 : typedef ::boost::unordered_map< OUString, BulletListItemInfo, OUStringHash > BulletListItemInfoMap;
179 :
180 : private:
181 : SVGExport& mrExport;
182 : SVGAttributeWriter* mpContext;
183 : VirtualDevice* mpVDev;
184 : bool mbIsTextShapeStarted;
185 : Reference<XText> mrTextShape;
186 : OUString msShapeId;
187 : Reference<XEnumeration> mrParagraphEnumeration;
188 : Reference<XTextContent> mrCurrentTextParagraph;
189 : Reference<XEnumeration> mrTextPortionEnumeration;
190 : Reference<XTextRange> mrCurrentTextPortion;
191 : const GDIMetaFile* mpTextEmbeddedBitmapMtf;
192 : MapMode* mpTargetMapMode;
193 : SvXMLElementExport* mpTextShapeElem;
194 : SvXMLElementExport* mpTextParagraphElem;
195 : SvXMLElementExport* mpTextPositionElem;
196 : sal_Int32 mnLeftTextPortionLength;
197 : Point maTextPos;
198 : long int mnTextWidth;
199 : bool mbPositioningNeeded;
200 : bool mbIsNewListItem;
201 : sal_Int16 meNumberingType;
202 : sal_Unicode mcBulletChar;
203 : BulletListItemInfoMap maBulletListItemMap;
204 : bool mbIsListLevelStyleImage;
205 : bool mbLineBreak;
206 : bool mbIsURLField;
207 : OUString msUrl;
208 : OUString msHyperlinkIdList;
209 : bool mbIsPlacehlolderShape;
210 : bool mbIWS;
211 : vcl::Font maCurrentFont;
212 : vcl::Font maParentFont;
213 :
214 : public:
215 : SVGTextWriter( SVGExport& rExport );
216 : virtual ~SVGTextWriter();
217 :
218 : sal_Int32 setTextPosition( const GDIMetaFile& rMtf, sal_uLong& nCurAction );
219 : void setTextProperties( const GDIMetaFile& rMtf, sal_uLong nCurAction );
220 : void addFontAttributes( bool bIsTextContainer );
221 :
222 : bool createParagraphEnumeration();
223 : bool nextParagraph();
224 : bool nextTextPortion();
225 :
226 0 : bool isTextShapeStarted() { return mbIsTextShapeStarted; }
227 : void startTextShape();
228 : void endTextShape();
229 : void startTextParagraph();
230 : void endTextParagraph();
231 : void startTextPosition( bool bExportX = true, bool bExportY = true);
232 : void endTextPosition();
233 : void implExportHyperlinkIds();
234 : void implWriteBulletChars();
235 : template< typename MetaBitmapActionType >
236 : void writeBitmapPlaceholder( const MetaBitmapActionType* pAction );
237 : void implWriteEmbeddedBitmaps();
238 : void writeTextPortion( const Point& rPos, const OUString& rText,
239 : bool bApplyMapping = true );
240 : void implWriteTextPortion( const Point& rPos, const OUString& rText,
241 : Color aTextColor, bool bApplyMapping );
242 :
243 0 : void setVirtualDevice( VirtualDevice* pVDev, MapMode& rTargetMapMode )
244 : {
245 : if( !pVDev )
246 : OSL_FAIL( "SVGTextWriter::setVirtualDevice: invalid virtual device." );
247 0 : mpVDev = pVDev;
248 0 : mpTargetMapMode = &rTargetMapMode;
249 0 : }
250 :
251 0 : void setContext( SVGAttributeWriter* pContext )
252 : {
253 0 : mpContext = pContext;
254 0 : }
255 :
256 0 : void setTextShape( const Reference<XText>& rxText,
257 : const GDIMetaFile* pTextEmbeddedBitmapMtf )
258 : {
259 0 : mrTextShape.set( rxText );
260 0 : mpTextEmbeddedBitmapMtf = pTextEmbeddedBitmapMtf;
261 0 : }
262 :
263 : const Reference<XText>& getTextShape() const
264 : {
265 : return mrTextShape;
266 : }
267 :
268 :
269 : void setPlaceholderShapeFlag( bool bState )
270 : {
271 : mbIsPlacehlolderShape = bState;
272 : }
273 :
274 : private:
275 : void implMap( const Size& rSz, Size& rDstSz ) const;
276 : void implMap( const Point& rPt, Point& rDstPt ) const;
277 : void implSetCurrentFont();
278 : void implSetFontFamily();
279 :
280 : template< typename SubType >
281 : bool implGetTextPosition( const MetaAction* pAction, Point& raPos, bool& bEmpty );
282 : template< typename SubType >
283 : bool implGetTextPositionFromBitmap( const MetaAction* pAction, Point& raPos, bool& rbEmpty );
284 :
285 : void implRegisterInterface( const Reference< XInterface >& rxIf );
286 : const OUString & implGetValidIDFromInterface( const Reference< XInterface >& rxIf );
287 :
288 :
289 : };
290 :
291 :
292 : // - SVGActionWriter -
293 :
294 :
295 : class SVGActionWriter
296 : {
297 : private:
298 :
299 : sal_Int32 mnCurGradientId;
300 : sal_Int32 mnCurMaskId;
301 : sal_Int32 mnCurPatternId;
302 : ::std::stack< SVGAttributeWriter* > maContextStack;
303 : ::std::unique_ptr< SVGShapeDescriptor > mapCurShape;
304 : SVGExport& mrExport;
305 : SVGFontExport& mrFontExport;
306 : SVGAttributeWriter* mpContext;
307 : SVGTextWriter maTextWriter;
308 : VirtualDevice* mpVDev;
309 : MapMode maTargetMapMode;
310 : sal_uInt32 mnInnerMtfCount;
311 : bool mbClipAttrChanged;
312 : bool mbIsPlacehlolderShape;
313 :
314 :
315 0 : SVGAttributeWriter* ImplAcquireContext()
316 : {
317 0 : maContextStack.push( mpContext = new SVGAttributeWriter( mrExport, mrFontExport ) );
318 0 : maTextWriter.setContext( mpContext );
319 0 : return mpContext;
320 : }
321 0 : void ImplReleaseContext()
322 : {
323 0 : if (!maContextStack.empty())
324 : {
325 0 : delete maContextStack.top();
326 0 : maContextStack.pop();
327 : }
328 0 : mpContext = (maContextStack.empty() ? NULL : maContextStack.top());
329 0 : maTextWriter.setContext( mpContext );
330 0 : }
331 :
332 : long ImplMap( sal_Int32 nVal ) const;
333 : Point& ImplMap( const Point& rPt, Point& rDstPt ) const;
334 : Size& ImplMap( const Size& rSz, Size& rDstSz ) const;
335 : Rectangle& ImplMap( const Rectangle& rRect, Rectangle& rDstRect ) const;
336 : Polygon& ImplMap( const Polygon& rPoly, Polygon& rDstPoly ) const;
337 : tools::PolyPolygon& ImplMap( const tools::PolyPolygon& rPolyPoly, tools::PolyPolygon& rDstPolyPoly ) const;
338 :
339 : void ImplWriteLine( const Point& rPt1, const Point& rPt2, const Color* pLineColor = NULL,
340 : bool bApplyMapping = true );
341 : void ImplWriteRect( const Rectangle& rRect, long nRadX = 0, long nRadY = 0,
342 : bool bApplyMapping = true );
343 : void ImplWriteEllipse( const Point& rCenter, long nRadX, long nRadY,
344 : bool bApplyMapping = true );
345 : void ImplWritePattern( const tools::PolyPolygon& rPolyPoly, const Hatch* pHatch, const Gradient* pGradient, sal_uInt32 nWriteFlags );
346 : void ImplAddLineAttr( const LineInfo &rAttrs,
347 : bool bApplyMapping = true );
348 : void ImplWritePolyPolygon( const tools::PolyPolygon& rPolyPoly, bool bLineOnly,
349 : bool bApplyMapping = true );
350 : void ImplWriteShape( const SVGShapeDescriptor& rShape, bool bApplyMapping = true );
351 : void ImplWriteGradientEx( const tools::PolyPolygon& rPolyPoly, const Gradient& rGradient, sal_uInt32 nWriteFlags);
352 : void ImplWriteGradientLinear( const tools::PolyPolygon& rPolyPoly, const Gradient& rGradient );
353 : void ImplWriteGradientStop( const Color& rColor, double fOffset );
354 : Color ImplGetColorWithIntensity( const Color& rColor, sal_uInt16 nIntensity );
355 : Color ImplGetGradientColor( const Color& rStartColor, const Color& rEndColor, double fOffset );
356 : void ImplWriteMask( GDIMetaFile& rMtf, const Point& rDestPt, const Size& rDestSize, const Gradient& rGradient, sal_uInt32 nWriteFlags );
357 : void ImplWriteText( const Point& rPos, const OUString& rText, const long* pDXArray, long nWidth, bool bApplyMapping = true );
358 : void ImplWriteText( const Point& rPos, const OUString& rText, const long* pDXArray, long nWidth, Color aTextColor, bool bApplyMapping );
359 : void ImplWriteBmp( const BitmapEx& rBmpEx, const Point& rPt, const Size& rSz, const Point& rSrcPt, const Size& rSrcSz,
360 : bool bApplyMapping = true );
361 :
362 : void ImplCheckFontAttributes();
363 : void ImplCheckPaintAttributes();
364 :
365 : void ImplWriteActions( const GDIMetaFile& rMtf,
366 : sal_uInt32 nWriteFlags,
367 : const OUString* pElementId,
368 : const Reference< XShape >* pXShape = NULL,
369 : const GDIMetaFile* pTextEmbeddedBitmapMtf = NULL );
370 :
371 : vcl::Font ImplSetCorrectFontHeight() const;
372 :
373 : public:
374 :
375 : static OUString GetPathString( const tools::PolyPolygon& rPolyPoly, bool bLine );
376 : static sal_uLong GetChecksum( const MetaAction* pAction );
377 :
378 : public:
379 :
380 : SVGActionWriter( SVGExport& rExport, SVGFontExport& rFontExport );
381 : virtual ~SVGActionWriter();
382 :
383 : void WriteMetaFile( const Point& rPos100thmm,
384 : const Size& rSize100thmm,
385 : const GDIMetaFile& rMtf,
386 : sal_uInt32 nWriteFlags,
387 : const OUString* pElementId = NULL,
388 : const Reference< XShape >* pXShape = NULL,
389 : const GDIMetaFile* pTextEmbeddedBitmapMtf = NULL );
390 : };
391 :
392 : class SVGWriter : public cppu::WeakImplHelper1< XSVGWriter >
393 : {
394 : private:
395 : Reference< XComponentContext > mxContext;
396 : Sequence< com::sun::star::beans::PropertyValue > maFilterData;
397 : SVGWriter();
398 :
399 : public:
400 : explicit SVGWriter( const Sequence<Any>& args,
401 : const Reference< XComponentContext >& rxCtx );
402 : virtual ~SVGWriter();
403 :
404 : // XSVGWriter
405 : virtual void SAL_CALL write( const Reference<XDocumentHandler>& rxDocHandler,
406 : const Sequence<sal_Int8>& rMtfSeq ) throw( RuntimeException, std::exception ) SAL_OVERRIDE;
407 : };
408 :
409 : #endif
410 :
411 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|