karbon/png/pngexport.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002, The Karbon Developers
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include <tqcstring.h>
21#include <tqdom.h>
22#include <tqfile.h>
23#include <tqstring.h>
24#include <tqvaluelist.h>
25#include <tqimage.h>
26
27#include <kgenericfactory.h>
28#include <KoFilter.h>
29#include <KoFilterChain.h>
30#include <KoStore.h>
31
32#include "pngexport.h"
33#include "vdocument.h"
34#include "vselection.h"
35#include "vkopainter.h"
36#include "vlayer.h"
37#include "vcomputeboundingbox.h"
38
39#include <kdebug.h>
40
41
42typedef KGenericFactory<PngExport, KoFilter> PngExportFactory;
43K_EXPORT_COMPONENT_FACTORY( libkarbonpngexport, PngExportFactory( "kofficefilters" ) )
44
45
46PngExport::PngExport( KoFilter*, const char*, const TQStringList& )
47 : KoFilter()
48{
49}
50
51KoFilter::ConversionStatus
52PngExport::convert( const TQCString& from, const TQCString& to )
53{
54 if ( to != "image/png" || from != "application/x-karbon" )
55 {
56 return KoFilter::NotImplemented;
57 }
58
59 KoStoreDevice* storeIn = m_chain->storageFile( "root", KoStore::Read );
60 if( !storeIn )
61 return KoFilter::StupidError;
62
63 TQDomDocument domIn;
64 domIn.setContent( storeIn );
65 TQDomElement docNode = domIn.documentElement();
66
67 // load the document and export it:
68 VDocument doc;
69 doc.load( docNode );
70
71 // calculate the documents bounding box
72 VComputeBoundingBox bbox( true );
73 doc.accept( bbox );
74 const KoRect &rect = bbox.boundingRect();
75
76 // create image with correct width and height
77 TQImage img( int( rect.width() ), int( rect.height() ), 32 );
78 //img.setAlphaBuffer( true );
79
80 // Create painter and set up objects to draw
81 VKoPainter p( img.bits(), rect.width(), rect.height() );
82 p.clear( tqRgba( 0xFF, 0xFF, 0xFF, 0xFF ) );
83 p.setWorldMatrix( TQWMatrix().translate( -rect.x(), -rect.y() ) );
84
85 doc.draw( &p, &rect );
86
87 TQImage image = img.swapRGB();
88 TQImage mirrored = image.mirror( false, true );
89 // save png
90 mirrored.save( m_chain->outputFile(), "PNG" );
91
92 return KoFilter::OK;
93}
94
95#include "pngexport.moc"
96