1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_extensions.hxx"
31 #define _RMPRINTER_EXT
33 #include "svgprinter.hxx"
34 #include "svgaction.hxx"
35 #include <uno/mapping.hxx>
36 #include <vcl/print.hxx>
37 #include <vcl/virdev.hxx>
39 static const char aXMLElemSVG
[] = "svg";
40 static const char aXMLElemMetaSVG
[] = "staroffice:svgElementMeta";
41 static const char aXMLElemDesc
[] = "desc";
42 static const char aXMLElemMeta
[] = "metadata";
43 static const char aXMLElemRect
[] = "rect";
45 static const char aXMLAttrMetaSVGOuter
[] = "isOuterElement";
46 static const char aXMLAttrMetaSVGPage
[] = "isPageElement";
47 static const char aXMLAttrViewBox
[] = "viewBox";
48 static const char aXMLAttrX
[] = "x";
49 static const char aXMLAttrY
[] = "y";
50 static const char aXMLAttrWidth
[] = "width";
51 static const char aXMLAttrHeight
[] = "height";
57 class SVGPrinterExport
: public SvXMLExport
62 VirtualDevice
* mpVDev
;
63 SvXMLElementExport
* mpOuterElement
;
68 SvXMLElementExport
* ImplCreateSVGElement( const JobSetup
& rSetup
, Size
& rOutputSize
);
69 void ImplWriteMetaAttr( sal_Bool bOuter
, sal_Bool bPage
);
73 virtual void _ExportMeta() {}
74 virtual void _ExportStyles( BOOL
/*bUsed*/ ) {}
75 virtual void _ExportAutoStyles() {}
76 virtual void _ExportContent() {}
77 virtual void _ExportMasterStyles() {}
78 virtual sal_uInt32
exportDoc( enum ::xmloff::token::XMLTokenEnum eClass
= ::xmloff::token::XML_TOKEN_INVALID
) { (void)eClass
; return 0; }
84 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
> xServiceFactory
,
85 const REF( NMSP_SAX::XDocumentHandler
)& rxHandler
,
86 const JobSetup
& rSetup
,
87 const NMSP_RTL::OUString
& rJobName
,
91 virtual ~SVGPrinterExport();
93 virtual void writePage( const JobSetup
& rJobSetup
, const GDIMetaFile
& rMtf
);
96 // -----------------------------------------------------------------------------
99 SVGPrinterExport::SVGPrinterExport(
100 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
> xServiceFactory
,
101 const REF( NMSP_SAX::XDocumentHandler
)& rxHandler
,
102 const JobSetup
& rSetup
,
103 const NMSP_RTL::OUString
& rJobName
,
104 sal_uInt32
/*nCopies*/,
105 sal_Bool
/*bCollate*/ )
106 : SvXMLExport( xServiceFactory
, NMSP_RTL::OUString(), rxHandler
),
110 maPrinter
.SetJobSetup( rSetup
);
112 GetDocHandler()->startDocument();
114 REF( NMSP_SAX::XExtendedDocumentHandler
) xExtDocHandler( GetDocHandler(), NMSP_UNO::UNO_QUERY
);
116 if( xExtDocHandler
.is() )
118 NMSP_RTL::OUString aString
;
119 const NMSP_RTL::OUString
aLineFeed( NMSP_RTL::OUString::valueOf( (sal_Unicode
) 0x0a ) );
122 xExtDocHandler
->unknown( ( aString
= SVG_DTD_STRING
) += aLineFeed
);
123 xExtDocHandler
->unknown( ( aString
= B2UCONST( "<!ELEMENT metadata (#PCDATA | staroffice:svgElementMeta)*> " ) += aLineFeed
) );
124 xExtDocHandler
->unknown( ( aString
= B2UCONST( "<!ELEMENT staroffice:svgElementMeta ANY> " ) += aLineFeed
) );
125 xExtDocHandler
->unknown( ( aString
= B2UCONST( "<!ATTLIST staroffice:svgElementMeta " ) += aLineFeed
) );
128 xExtDocHandler
->unknown( ( aString
= B2UCONST( "isOuterElement (true | false) \"false\" " ) += aLineFeed
) );
129 xExtDocHandler
->unknown( ( aString
= B2UCONST( "isPageElement (true | false) \"true\"" ) += aLineFeed
) );
132 xExtDocHandler
->unknown( ( aString
= B2UCONST( ">" ) += aLineFeed
) );
133 xExtDocHandler
->unknown( ( aString
= B2UCONST( "]>" ) ) );
136 // create outer element
139 mpOuterElement
= ImplCreateSVGElement( rSetup
, aOutputSize
);
142 SvXMLElementExport
* pDescElem
= new SvXMLElementExport( *this, XML_NAMESPACE_NONE
, aXMLElemDesc
, TRUE
, TRUE
);
143 NMSP_RTL::OUString
aDesc( B2UCONST( "document name: " ) );
145 GetDocHandler()->characters( aDesc
+= rJobName
);
148 // write meta attributes
149 ImplWriteMetaAttr( TRUE
, FALSE
);
152 // -----------------------------------------------------------------------------
154 SVGPrinterExport::~SVGPrinterExport()
156 delete mpOuterElement
;
157 GetDocHandler()->endDocument();
161 // -----------------------------------------------------------------------------
163 SvXMLElementExport
* SVGPrinterExport::ImplCreateSVGElement( const JobSetup
& rSetup
, Size
& rOutputSize
)
165 NMSP_RTL::OUString aAttr
;
168 mpVDev
= new VirtualDevice
;
169 mpVDev
->EnableOutput( FALSE
);
170 mpVDev
->SetMapMode( MAP_100TH_MM
);
171 maPrinter
.SetJobSetup( rSetup
);
173 rOutputSize
= maPrinter
.PixelToLogic( maPrinter
.GetOutputSizePixel(), mpVDev
->GetMapMode() );
175 aAttr
= SVGActionWriter::GetValueString( rOutputSize
.Width(), sal_True
);
176 AddAttribute( XML_NAMESPACE_NONE
, aXMLAttrWidth
, aAttr
);
178 aAttr
= SVGActionWriter::GetValueString( rOutputSize
.Height(), sal_True
);
179 AddAttribute( XML_NAMESPACE_NONE
, aXMLAttrHeight
, aAttr
);
181 aAttr
= B2UCONST( "0.0 0.0 " );
182 aAttr
+= SVGActionWriter::GetValueString( rOutputSize
.Width(), sal_True
);
183 aAttr
+= B2UCONST( " " );
184 aAttr
+= SVGActionWriter::GetValueString( rOutputSize
.Height(), sal_True
);
185 AddAttribute( XML_NAMESPACE_NONE
, aXMLAttrViewBox
, aAttr
);
187 return( new SvXMLElementExport( *this, XML_NAMESPACE_NONE
, aXMLElemSVG
, TRUE
, TRUE
) );
190 // -----------------------------------------------------------------------------
192 void SVGPrinterExport::ImplWriteMetaAttr( sal_Bool bOuter
, sal_Bool bPage
)
194 SvXMLElementExport
aMetaData( *this, XML_NAMESPACE_NONE
, aXMLElemMeta
, TRUE
, TRUE
);
195 NMSP_RTL::OUString aAttr
;
197 aAttr
= bOuter
? B2UCONST( "true" ) : B2UCONST( "false" );
198 AddAttribute( XML_NAMESPACE_NONE
, aXMLAttrMetaSVGOuter
, aAttr
);
200 aAttr
= bPage
? B2UCONST( "true" ) : B2UCONST( "false" );
201 AddAttribute( XML_NAMESPACE_NONE
, aXMLAttrMetaSVGPage
, aAttr
);
204 delete( new SvXMLElementExport( *this, XML_NAMESPACE_NONE
, aXMLElemMetaSVG
, TRUE
, TRUE
) );
208 // -----------------------------------------------------------------------------
210 void SVGPrinterExport::writePage( const JobSetup
& rSetup
, const GDIMetaFile
& rMtf
)
213 NMSP_RTL::OUString aAttr
;
214 SvXMLElementExport
* pPageElem
= ImplCreateSVGElement( rSetup
, aOutputSize
);
217 SvXMLElementExport
* pDescElem
= new SvXMLElementExport( *this, XML_NAMESPACE_NONE
, aXMLElemDesc
, TRUE
, TRUE
);
218 NMSP_RTL::OUString
aDesc( B2UCONST( "page: " ) );
220 GetDocHandler()->characters( aDesc
+= NMSP_RTL::OUString::valueOf( (sal_Int32
) ++mnPage
) );
223 // write meta attributes
224 ImplWriteMetaAttr( FALSE
, TRUE
);
226 // write dummy rect element
227 aAttr
= B2UCONST( "0.0" );
228 AddAttribute( XML_NAMESPACE_NONE
, aXMLAttrX
, aAttr
);
229 AddAttribute( XML_NAMESPACE_NONE
, aXMLAttrY
, aAttr
);
231 aAttr
= SVGActionWriter::GetValueString( aOutputSize
.Width(), sal_True
);
232 AddAttribute( XML_NAMESPACE_NONE
, aXMLAttrWidth
, aAttr
);
234 aAttr
= SVGActionWriter::GetValueString( aOutputSize
.Height(), sal_True
);
235 AddAttribute( XML_NAMESPACE_NONE
, aXMLAttrHeight
, aAttr
);
237 delete( new SvXMLElementExport( *this, XML_NAMESPACE_NONE
, aXMLElemRect
, TRUE
, TRUE
) );
238 delete( new SVGActionWriter( *this, rMtf
, mpVDev
, TRUE
) );
247 SVGPrinter::SVGPrinter( const REF( NMSP_LANG::XMultiServiceFactory
)& rxMgr
) :
253 // -----------------------------------------------------------------------------
255 SVGPrinter::~SVGPrinter()
260 // -----------------------------------------------------------------------------
263 ANY SAL_CALL
SVGPrinter::queryInterface( const NMSP_UNO::Type
& rType
) throw( NMSP_UNO::RuntimeException
)
265 const ANY
aRet( NMSP_CPPU::queryInterface( rType
, static_cast< NMSP_SVG::XSVGPrinter
* >( this ) ) );
267 return( aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
) );
270 // -----------------------------------------------------------------------------
272 void SAL_CALL
SVGPrinter::acquire() throw()
274 OWeakObject::acquire();
277 // -----------------------------------------------------------------------------
279 void SAL_CALL
SVGPrinter::release() throw()
281 OWeakObject::release();
284 // -----------------------------------------------------------------------------
286 sal_Bool SAL_CALL
SVGPrinter::startJob( const REF( NMSP_SAX::XDocumentHandler
)& rxHandler
,
287 const SEQ( sal_Int8
)& rJobSetupSeq
,
288 const NMSP_RTL::OUString
& rJobName
,
289 sal_uInt32 nCopies
, sal_Bool bCollate
) throw( NMSP_UNO::RuntimeException
)
291 const sal_Bool bRet
= ( mpWriter
== NULL
);
295 SvMemoryStream
aMemStm( (char*) rJobSetupSeq
.getConstArray(), rJobSetupSeq
.getLength(), STREAM_READ
);
298 aMemStm
.SetCompressMode( COMPRESSMODE_FULL
);
299 aMemStm
>> aJobSetup
;
301 const REF( NMSP_SAX::XDocumentHandler
) xDocumentHandler( rxHandler
);
304 // mpWriter = new SVGPrinterExport( xDocumentHandler, aJobSetup, rJobName, nCopies, bCollate );
305 mpWriter
= new SVGPrinterExport( mxFact
, xDocumentHandler
, aJobSetup
, rJobName
, nCopies
, bCollate
);
311 // -----------------------------------------------------------------------------
313 void SAL_CALL
SVGPrinter::printPage( const SEQ( sal_Int8
)& rPrintPage
) throw( NMSP_UNO::RuntimeException
)
315 SvMemoryStream
aMemStm( (char*) rPrintPage
.getConstArray(), rPrintPage
.getLength(), STREAM_READ
);
318 aMemStm
.SetCompressMode( COMPRESSMODE_FULL
);
320 mpWriter
->writePage( aPage
.GetJobSetup(), *aPage
.GetGDIMetaFile() );
323 // -----------------------------------------------------------------------------
325 void SAL_CALL
SVGPrinter::endJob() throw( NMSP_UNO::RuntimeException
)
327 delete mpWriter
, mpWriter
= NULL
;