1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: svgprinter.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
34 #define _RMPRINTER_EXT
36 #include "svgprinter.hxx"
37 #include "svgaction.hxx"
38 #include <uno/mapping.hxx>
39 #include <vcl/print.hxx>
40 #include <vcl/virdev.hxx>
42 static const char aXMLElemSVG
[] = "svg";
43 static const char aXMLElemMetaSVG
[] = "staroffice:svgElementMeta";
44 static const char aXMLElemDesc
[] = "desc";
45 static const char aXMLElemMeta
[] = "metadata";
46 static const char aXMLElemRect
[] = "rect";
48 static const char aXMLAttrMetaSVGOuter
[] = "isOuterElement";
49 static const char aXMLAttrMetaSVGPage
[] = "isPageElement";
50 static const char aXMLAttrViewBox
[] = "viewBox";
51 static const char aXMLAttrX
[] = "x";
52 static const char aXMLAttrY
[] = "y";
53 static const char aXMLAttrWidth
[] = "width";
54 static const char aXMLAttrHeight
[] = "height";
60 class SVGPrinterExport
: public SvXMLExport
65 VirtualDevice
* mpVDev
;
66 SvXMLElementExport
* mpOuterElement
;
71 SvXMLElementExport
* ImplCreateSVGElement( const JobSetup
& rSetup
, Size
& rOutputSize
);
72 void ImplWriteMetaAttr( sal_Bool bOuter
, sal_Bool bPage
);
76 virtual void _ExportMeta() {}
77 virtual void _ExportStyles( BOOL
/*bUsed*/ ) {}
78 virtual void _ExportAutoStyles() {}
79 virtual void _ExportContent() {}
80 virtual void _ExportMasterStyles() {}
81 virtual sal_uInt32
exportDoc( enum ::xmloff::token::XMLTokenEnum eClass
= ::xmloff::token::XML_TOKEN_INVALID
) { (void)eClass
; return 0; }
87 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
> xServiceFactory
,
88 const REF( NMSP_SAX::XDocumentHandler
)& rxHandler
,
89 const JobSetup
& rSetup
,
90 const NMSP_RTL::OUString
& rJobName
,
94 virtual ~SVGPrinterExport();
96 virtual void writePage( const JobSetup
& rJobSetup
, const GDIMetaFile
& rMtf
);
99 // -----------------------------------------------------------------------------
102 SVGPrinterExport::SVGPrinterExport(
103 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
> xServiceFactory
,
104 const REF( NMSP_SAX::XDocumentHandler
)& rxHandler
,
105 const JobSetup
& rSetup
,
106 const NMSP_RTL::OUString
& rJobName
,
107 sal_uInt32
/*nCopies*/,
108 sal_Bool
/*bCollate*/ )
109 : SvXMLExport( xServiceFactory
, NMSP_RTL::OUString(), rxHandler
),
113 maPrinter
.SetJobSetup( rSetup
);
115 GetDocHandler()->startDocument();
117 REF( NMSP_SAX::XExtendedDocumentHandler
) xExtDocHandler( GetDocHandler(), NMSP_UNO::UNO_QUERY
);
119 if( xExtDocHandler
.is() )
121 NMSP_RTL::OUString aString
;
122 const NMSP_RTL::OUString
aLineFeed( NMSP_RTL::OUString::valueOf( (sal_Unicode
) 0x0a ) );
125 xExtDocHandler
->unknown( ( aString
= SVG_DTD_STRING
) += aLineFeed
);
126 xExtDocHandler
->unknown( ( aString
= B2UCONST( "<!ELEMENT metadata (#PCDATA | staroffice:svgElementMeta)*> " ) += aLineFeed
) );
127 xExtDocHandler
->unknown( ( aString
= B2UCONST( "<!ELEMENT staroffice:svgElementMeta ANY> " ) += aLineFeed
) );
128 xExtDocHandler
->unknown( ( aString
= B2UCONST( "<!ATTLIST staroffice:svgElementMeta " ) += aLineFeed
) );
131 xExtDocHandler
->unknown( ( aString
= B2UCONST( "isOuterElement (true | false) \"false\" " ) += aLineFeed
) );
132 xExtDocHandler
->unknown( ( aString
= B2UCONST( "isPageElement (true | false) \"true\"" ) += aLineFeed
) );
135 xExtDocHandler
->unknown( ( aString
= B2UCONST( ">" ) += aLineFeed
) );
136 xExtDocHandler
->unknown( ( aString
= B2UCONST( "]>" ) ) );
139 // create outer element
142 mpOuterElement
= ImplCreateSVGElement( rSetup
, aOutputSize
);
145 SvXMLElementExport
* pDescElem
= new SvXMLElementExport( *this, XML_NAMESPACE_NONE
, aXMLElemDesc
, TRUE
, TRUE
);
146 NMSP_RTL::OUString
aDesc( B2UCONST( "document name: " ) );
148 GetDocHandler()->characters( aDesc
+= rJobName
);
151 // write meta attributes
152 ImplWriteMetaAttr( TRUE
, FALSE
);
155 // -----------------------------------------------------------------------------
157 SVGPrinterExport::~SVGPrinterExport()
159 delete mpOuterElement
;
160 GetDocHandler()->endDocument();
164 // -----------------------------------------------------------------------------
166 SvXMLElementExport
* SVGPrinterExport::ImplCreateSVGElement( const JobSetup
& rSetup
, Size
& rOutputSize
)
168 NMSP_RTL::OUString aAttr
;
171 mpVDev
= new VirtualDevice
;
172 mpVDev
->EnableOutput( FALSE
);
173 mpVDev
->SetMapMode( MAP_100TH_MM
);
174 maPrinter
.SetJobSetup( rSetup
);
176 rOutputSize
= maPrinter
.PixelToLogic( maPrinter
.GetOutputSizePixel(), mpVDev
->GetMapMode() );
178 aAttr
= SVGActionWriter::GetValueString( rOutputSize
.Width(), sal_True
);
179 AddAttribute( XML_NAMESPACE_NONE
, aXMLAttrWidth
, aAttr
);
181 aAttr
= SVGActionWriter::GetValueString( rOutputSize
.Height(), sal_True
);
182 AddAttribute( XML_NAMESPACE_NONE
, aXMLAttrHeight
, aAttr
);
184 aAttr
= B2UCONST( "0.0 0.0 " );
185 aAttr
+= SVGActionWriter::GetValueString( rOutputSize
.Width(), sal_True
);
186 aAttr
+= B2UCONST( " " );
187 aAttr
+= SVGActionWriter::GetValueString( rOutputSize
.Height(), sal_True
);
188 AddAttribute( XML_NAMESPACE_NONE
, aXMLAttrViewBox
, aAttr
);
190 return( new SvXMLElementExport( *this, XML_NAMESPACE_NONE
, aXMLElemSVG
, TRUE
, TRUE
) );
193 // -----------------------------------------------------------------------------
195 void SVGPrinterExport::ImplWriteMetaAttr( sal_Bool bOuter
, sal_Bool bPage
)
197 SvXMLElementExport
aMetaData( *this, XML_NAMESPACE_NONE
, aXMLElemMeta
, TRUE
, TRUE
);
198 NMSP_RTL::OUString aAttr
;
200 aAttr
= bOuter
? B2UCONST( "true" ) : B2UCONST( "false" );
201 AddAttribute( XML_NAMESPACE_NONE
, aXMLAttrMetaSVGOuter
, aAttr
);
203 aAttr
= bPage
? B2UCONST( "true" ) : B2UCONST( "false" );
204 AddAttribute( XML_NAMESPACE_NONE
, aXMLAttrMetaSVGPage
, aAttr
);
207 delete( new SvXMLElementExport( *this, XML_NAMESPACE_NONE
, aXMLElemMetaSVG
, TRUE
, TRUE
) );
211 // -----------------------------------------------------------------------------
213 void SVGPrinterExport::writePage( const JobSetup
& rSetup
, const GDIMetaFile
& rMtf
)
216 NMSP_RTL::OUString aAttr
;
217 SvXMLElementExport
* pPageElem
= ImplCreateSVGElement( rSetup
, aOutputSize
);
220 SvXMLElementExport
* pDescElem
= new SvXMLElementExport( *this, XML_NAMESPACE_NONE
, aXMLElemDesc
, TRUE
, TRUE
);
221 NMSP_RTL::OUString
aDesc( B2UCONST( "page: " ) );
223 GetDocHandler()->characters( aDesc
+= NMSP_RTL::OUString::valueOf( (sal_Int32
) ++mnPage
) );
226 // write meta attributes
227 ImplWriteMetaAttr( FALSE
, TRUE
);
229 // write dummy rect element
230 aAttr
= B2UCONST( "0.0" );
231 AddAttribute( XML_NAMESPACE_NONE
, aXMLAttrX
, aAttr
);
232 AddAttribute( XML_NAMESPACE_NONE
, aXMLAttrY
, aAttr
);
234 aAttr
= SVGActionWriter::GetValueString( aOutputSize
.Width(), sal_True
);
235 AddAttribute( XML_NAMESPACE_NONE
, aXMLAttrWidth
, aAttr
);
237 aAttr
= SVGActionWriter::GetValueString( aOutputSize
.Height(), sal_True
);
238 AddAttribute( XML_NAMESPACE_NONE
, aXMLAttrHeight
, aAttr
);
240 delete( new SvXMLElementExport( *this, XML_NAMESPACE_NONE
, aXMLElemRect
, TRUE
, TRUE
) );
241 delete( new SVGActionWriter( *this, rMtf
, mpVDev
, TRUE
) );
250 SVGPrinter::SVGPrinter( const REF( NMSP_LANG::XMultiServiceFactory
)& rxMgr
) :
256 // -----------------------------------------------------------------------------
258 SVGPrinter::~SVGPrinter()
263 // -----------------------------------------------------------------------------
266 ANY SAL_CALL
SVGPrinter::queryInterface( const NMSP_UNO::Type
& rType
) throw( NMSP_UNO::RuntimeException
)
268 const ANY
aRet( NMSP_CPPU::queryInterface( rType
, static_cast< NMSP_SVG::XSVGPrinter
* >( this ) ) );
270 return( aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
) );
273 // -----------------------------------------------------------------------------
275 void SAL_CALL
SVGPrinter::acquire() throw()
277 OWeakObject::acquire();
280 // -----------------------------------------------------------------------------
282 void SAL_CALL
SVGPrinter::release() throw()
284 OWeakObject::release();
287 // -----------------------------------------------------------------------------
289 sal_Bool SAL_CALL
SVGPrinter::startJob( const REF( NMSP_SAX::XDocumentHandler
)& rxHandler
,
290 const SEQ( sal_Int8
)& rJobSetupSeq
,
291 const NMSP_RTL::OUString
& rJobName
,
292 sal_uInt32 nCopies
, sal_Bool bCollate
) throw( NMSP_UNO::RuntimeException
)
294 const sal_Bool bRet
= ( mpWriter
== NULL
);
298 SvMemoryStream
aMemStm( (char*) rJobSetupSeq
.getConstArray(), rJobSetupSeq
.getLength(), STREAM_READ
);
301 aMemStm
.SetCompressMode( COMPRESSMODE_FULL
);
302 aMemStm
>> aJobSetup
;
304 const REF( NMSP_SAX::XDocumentHandler
) xDocumentHandler( rxHandler
);
307 // mpWriter = new SVGPrinterExport( xDocumentHandler, aJobSetup, rJobName, nCopies, bCollate );
308 mpWriter
= new SVGPrinterExport( mxFact
, xDocumentHandler
, aJobSetup
, rJobName
, nCopies
, bCollate
);
314 // -----------------------------------------------------------------------------
316 void SAL_CALL
SVGPrinter::printPage( const SEQ( sal_Int8
)& rPrintPage
) throw( NMSP_UNO::RuntimeException
)
318 SvMemoryStream
aMemStm( (char*) rPrintPage
.getConstArray(), rPrintPage
.getLength(), STREAM_READ
);
321 aMemStm
.SetCompressMode( COMPRESSMODE_FULL
);
323 mpWriter
->writePage( aPage
.GetJobSetup(), *aPage
.GetGDIMetaFile() );
326 // -----------------------------------------------------------------------------
328 void SAL_CALL
SVGPrinter::endJob() throw( NMSP_UNO::RuntimeException
)
330 delete mpWriter
, mpWriter
= NULL
;