filters

data.cpp

00001 /*
00002  * Copyright (c) 2002-2003 Nicolas HADACEK (hadacek@kde.org)
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008 
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include "data.h"
00021 
00022 #include <kglobal.h>
00023 #include <kdebug.h>
00024 #include <klocale.h>
00025 
00026 
00027 using namespace PDFImport;
00028 
00029 static const char *TEXT_FRAMESET_NAMES[Nb_ParagraphTypes] = {
00030     I18N_NOOP("Body Frameset #%1"), I18N_NOOP("Header Frameset #%1"),
00031     I18N_NOOP("Footer Frameset #%1")
00032 };
00033 
00034 Data::Data(KoFilterChain *chain, const DRect &pageRect,
00035            KoPageLayout page, const Options &options)
00036     : pageIndex(0), _chain(chain), _imageIndex(1), _textIndex(1),
00037       _textFramesets(Nb_ParagraphTypes),
00038       _pageRect(pageRect), _options(options)
00039 {
00040     _document = QDomDocument("DOC");
00041     _document.appendChild(
00042         _document.createProcessingInstruction(
00043             "xml","version=\"1.0\" encoding=\"UTF-8\""));
00044 
00045     _mainElement = _document.createElement("DOC");
00046     _mainElement.setAttribute("editor", "KWord's PDF Import Filter");
00047     _mainElement.setAttribute("mime", "application/x-kword");
00048     _mainElement.setAttribute("syntaxVersion", 2);
00049     _document.appendChild(_mainElement);
00050 
00051     QDomElement element = _document.createElement("ATTRIBUTES");
00052     element.setAttribute("processing", 1);
00053     element.setAttribute("hasHeader", 0);
00054     element.setAttribute("hasFooter", 0);
00055     element.setAttribute("hasTOC", 0);
00056     element.setAttribute("unit", "mm");
00057     _mainElement.appendChild(element);
00058 
00059     _paper = _document.createElement("PAPER");
00060     _paper.setAttribute("format", page.format);
00061     _paper.setAttribute("width", pageRect.width());
00062     _paper.setAttribute("height", pageRect.height());
00063     _paper.setAttribute("orientation", page.orientation);
00064     _paper.setAttribute("columns", 1);
00065     _paper.setAttribute("hType", 0);
00066     _paper.setAttribute("fType", 0);
00067     _mainElement.appendChild(_paper);
00068 
00069     // framesets
00070     _framesets = _document.createElement("FRAMESETS");
00071     _mainElement.appendChild(_framesets);
00072 
00073     // standard style
00074     QDomElement styles = _document.createElement("STYLES");
00075     _mainElement.appendChild(styles);
00076 
00077     QDomElement style = _document.createElement("STYLE");
00078     styles.appendChild(style);
00079 
00080     element = _document.createElement("FORMAT");
00081     Font font;
00082     font.format(_document, element, 0, 0, true);
00083     style.appendChild(element);
00084 
00085     element = _document.createElement("NAME");
00086     element.setAttribute("value","Standard");
00087     style.appendChild(element);
00088 
00089     element = _document.createElement("FOLLOWING");
00090     element.setAttribute("name","Standard");
00091     style.appendChild(element);
00092 
00093     // pictures
00094     _pictures = _document.createElement("PICTURES");
00095     _mainElement.appendChild(_pictures);
00096 
00097     // treat pages
00098     _bookmarks = _document.createElement("BOOKMARKS");
00099     _mainElement.appendChild(_bookmarks);
00100 }
00101 
00102 QDomElement Data::pictureFrameset(const DRect &r)
00103 {
00104     QDomElement frameset = createFrameset(Picture, QString::null);
00105     QDomElement frame = createFrame(Picture, r, false);
00106     frameset.appendChild(frame);
00107     return frameset;
00108 }
00109 
00110 QDomElement Data::createFrameset(FramesetType type, const QString &n)
00111 {
00112     bool text = (type==Text);
00113     uint &index = (text ? _textIndex : _imageIndex);
00114 
00115     QDomElement frameset = _document.createElement("FRAMESET");
00116     frameset.setAttribute("frameType", (text ? 1 : 2));
00117     QString name = n;
00118     if ( name.isNull() )
00119         name = (text ? i18n("Text Frameset %1")
00120                 : i18n("Picture %1")).arg(index);
00121     frameset.setAttribute("name", name);
00122     frameset.setAttribute("frameInfo", 0);
00123 
00124 //    kdDebug(30516) << "new frameset " << index << (text ? " text" : " image")
00125 //                   << endl;
00126     index++;
00127     return frameset;
00128 }
00129 
00130 QDomElement Data::createFrame(FramesetType type, const DRect &r,
00131                               bool forceMainFrameset)
00132 {
00133     bool text = (type==Text);
00134     bool mainFrameset =
00135         (text ? (forceMainFrameset ? true : _textIndex==1) : false);
00136 
00137     QDomElement frame = _document.createElement("FRAME");
00138     if (text) frame.setAttribute("autoCreateNewFrame", 0);
00139     frame.setAttribute("newFrameBehavior", 1);
00140     frame.setAttribute("runaround", 0);
00141     frame.setAttribute("left", r.left());
00142     frame.setAttribute("right", r.right());
00143     double offset = pageIndex * _pageRect.height();
00144     frame.setAttribute("top", r.top() + offset);
00145     frame.setAttribute("bottom", r.bottom() + offset);
00146     if ( text && !mainFrameset ) frame.setAttribute("bkStyle", 0);
00147     return frame;
00148 }
00149 
00150 void Data::initPage(const QValueVector<DRect> &rects,
00151                     const QValueList<QDomElement> &pictures)
00152 {
00153     for (uint i=0; i<Nb_ParagraphTypes; i++) {
00154 //        kdDebug(30516) << "page #" << pageIndex << " rect #" << i
00155 //                       << ": " << rects[i].toString() << endl;
00156         if ( !rects[i].isValid() ) continue;
00157         QString name = i18n(TEXT_FRAMESET_NAMES[i]).arg(pageIndex);
00158         _textFramesets[i] = createFrameset(Text, name);
00159         _framesets.appendChild(_textFramesets[i]);
00160         QDomElement frame = createFrame(Text, rects[i], true);
00161         _textFramesets[i].appendChild(frame);
00162     }
00163 
00164     QValueList<QDomElement>::const_iterator it;
00165     for (it = pictures.begin(); it!=pictures.end(); ++it)
00166         _framesets.appendChild(*it);
00167 
00168     // page bookmark
00169     QDomElement element = createElement("BOOKMARKITEM");
00170     element.setAttribute("name", Link::pageLinkName(pageIndex));
00171     element.setAttribute("cursorIndexStart", 0); // ?
00172     element.setAttribute("cursorIndexEnd", 0); // ?
00173     element.setAttribute("frameset", "Text Frameset 1");
00174     element.setAttribute("startparag", 0); // #### FIXME
00175     element.setAttribute("endparag", 0); // ?
00176     bookmarks().appendChild(element);
00177 
00178     _marginRect.unite(rects[Body]);
00179 }
00180 
00181 void Data::createParagraph(const QString &text, ParagraphType type,
00182                            const QValueVector<QDomElement> &layouts,
00183                            const QValueVector<QDomElement> &formats)
00184 {
00185     QDomElement paragraph = _document.createElement("PARAGRAPH");
00186     _textFramesets[type].appendChild(paragraph);
00187 
00188     QDomElement textElement = _document.createElement("TEXT");
00189     textElement.appendChild( _document.createTextNode(text) );
00190     paragraph.appendChild(textElement);
00191 
00192     QDomElement layout = _document.createElement("LAYOUT");
00193     paragraph.appendChild(layout);
00194     QDomElement element = _document.createElement("NAME");
00195     element.setAttribute("value", "Standard");
00196     layout.appendChild(element);
00197     for (uint i=0; i<layouts.size(); i++)
00198         layout.appendChild(layouts[i]);
00199 
00200     if ( formats.size() ) {
00201         QDomElement format = _document.createElement("FORMATS");
00202         paragraph.appendChild(format);
00203         for (uint i=0; i<formats.size(); i++)
00204             format.appendChild(formats[i]);
00205     }
00206 }
00207 
00208 void Data::endDump()
00209 {
00210     if ( !_marginRect.isValid() ) _marginRect = _pageRect;
00211     QDomElement element = _document.createElement("PAPERBORDERS");
00212     element.setAttribute("left", _marginRect.left() - _pageRect.left());
00213     element.setAttribute("top", _marginRect.top() - _pageRect.top());
00214     element.setAttribute("right", _pageRect.right() - _marginRect.right());
00215     element.setAttribute("bottom", _pageRect.bottom() - _marginRect.bottom());
00216     _paper.appendChild(element);
00217 }
KDE Home | KDE Accessibility Home | Description of Access Keys