filters

data.cpp
1/*
2 * Copyright (c) 2002-2003 Nicolas HADACEK (hadacek@kde.org)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8
9 * This program 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
12 * GNU General Public License for more details.
13
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "data.h"
21
22#include <tdeglobal.h>
23#include <kdebug.h>
24#include <tdelocale.h>
25
26
27using namespace PDFImport;
28
29static const char *TEXT_FRAMESET_NAMES[Nb_ParagraphTypes] = {
30 I18N_NOOP("Body Frameset #%1"), I18N_NOOP("Header Frameset #%1"),
31 I18N_NOOP("Footer Frameset #%1")
32};
33
34Data::Data(KoFilterChain *chain, const DRect &pageRect,
35 KoPageLayout page, const Options &options)
36 : pageIndex(0), _chain(chain), _imageIndex(1), _textIndex(1),
37 _textFramesets(Nb_ParagraphTypes),
38 _pageRect(pageRect), _options(options)
39{
40 _document = TQDomDocument("DOC");
41 _document.appendChild(
42 _document.createProcessingInstruction(
43 "xml","version=\"1.0\" encoding=\"UTF-8\""));
44
45 _mainElement = _document.createElement("DOC");
46 _mainElement.setAttribute("editor", "KWord's PDF Import Filter");
47 _mainElement.setAttribute("mime", "application/x-kword");
48 _mainElement.setAttribute("syntaxVersion", 2);
49 _document.appendChild(_mainElement);
50
51 TQDomElement element = _document.createElement("ATTRIBUTES");
52 element.setAttribute("processing", 1);
53 element.setAttribute("hasHeader", 0);
54 element.setAttribute("hasFooter", 0);
55 element.setAttribute("hasTOC", 0);
56 element.setAttribute("unit", "mm");
57 _mainElement.appendChild(element);
58
59 _paper = _document.createElement("PAPER");
60 _paper.setAttribute("format", page.format);
61 _paper.setAttribute("width", pageRect.width());
62 _paper.setAttribute("height", pageRect.height());
63 _paper.setAttribute("orientation", page.orientation);
64 _paper.setAttribute("columns", 1);
65 _paper.setAttribute("hType", 0);
66 _paper.setAttribute("fType", 0);
67 _mainElement.appendChild(_paper);
68
69 // framesets
70 _framesets = _document.createElement("FRAMESETS");
71 _mainElement.appendChild(_framesets);
72
73 // standard style
74 TQDomElement styles = _document.createElement("STYLES");
75 _mainElement.appendChild(styles);
76
77 TQDomElement style = _document.createElement("STYLE");
78 styles.appendChild(style);
79
80 element = _document.createElement("FORMAT");
81 Font font;
82 font.format(_document, element, 0, 0, true);
83 style.appendChild(element);
84
85 element = _document.createElement("NAME");
86 element.setAttribute("value","Standard");
87 style.appendChild(element);
88
89 element = _document.createElement("FOLLOWING");
90 element.setAttribute("name","Standard");
91 style.appendChild(element);
92
93 // pictures
94 _pictures = _document.createElement("PICTURES");
95 _mainElement.appendChild(_pictures);
96
97 // treat pages
98 _bookmarks = _document.createElement("BOOKMARKS");
99 _mainElement.appendChild(_bookmarks);
100}
101
102TQDomElement Data::pictureFrameset(const DRect &r)
103{
104 TQDomElement frameset = createFrameset(Picture, TQString());
105 TQDomElement frame = createFrame(Picture, r, false);
106 frameset.appendChild(frame);
107 return frameset;
108}
109
110TQDomElement Data::createFrameset(FramesetType type, const TQString &n)
111{
112 bool text = (type==Text);
113 uint &index = (text ? _textIndex : _imageIndex);
114
115 TQDomElement frameset = _document.createElement("FRAMESET");
116 frameset.setAttribute("frameType", (text ? 1 : 2));
117 TQString name = n;
118 if ( name.isNull() )
119 name = (text ? i18n("Text Frameset %1")
120 : i18n("Picture %1")).arg(index);
121 frameset.setAttribute("name", name);
122 frameset.setAttribute("frameInfo", 0);
123
124// kdDebug(30516) << "new frameset " << index << (text ? " text" : " image")
125// << endl;
126 index++;
127 return frameset;
128}
129
130TQDomElement Data::createFrame(FramesetType type, const DRect &r,
131 bool forceMainFrameset)
132{
133 bool text = (type==Text);
134 bool mainFrameset =
135 (text ? (forceMainFrameset ? true : _textIndex==1) : false);
136
137 TQDomElement frame = _document.createElement("FRAME");
138 if (text) frame.setAttribute("autoCreateNewFrame", 0);
139 frame.setAttribute("newFrameBehavior", 1);
140 frame.setAttribute("runaround", 0);
141 frame.setAttribute("left", r.left());
142 frame.setAttribute("right", r.right());
143 double offset = pageIndex * _pageRect.height();
144 frame.setAttribute("top", r.top() + offset);
145 frame.setAttribute("bottom", r.bottom() + offset);
146 if ( text && !mainFrameset ) frame.setAttribute("bkStyle", 0);
147 return frame;
148}
149
150void Data::initPage(const TQValueVector<DRect> &rects,
151 const TQValueList<TQDomElement> &pictures)
152{
153 for (uint i=0; i<Nb_ParagraphTypes; i++) {
154// kdDebug(30516) << "page #" << pageIndex << " rect #" << i
155// << ": " << rects[i].toString() << endl;
156 if ( !rects[i].isValid() ) continue;
157 TQString name = i18n(TEXT_FRAMESET_NAMES[i]).arg(pageIndex);
158 _textFramesets[i] = createFrameset(Text, name);
159 _framesets.appendChild(_textFramesets[i]);
160 TQDomElement frame = createFrame(Text, rects[i], true);
161 _textFramesets[i].appendChild(frame);
162 }
163
164 TQValueList<TQDomElement>::const_iterator it;
165 for (it = pictures.begin(); it!=pictures.end(); ++it)
166 _framesets.appendChild(*it);
167
168 // page bookmark
169 TQDomElement element = createElement("BOOKMARKITEM");
170 element.setAttribute("name", Link::pageLinkName(pageIndex));
171 element.setAttribute("cursorIndexStart", 0); // ?
172 element.setAttribute("cursorIndexEnd", 0); // ?
173 element.setAttribute("frameset", "Text Frameset 1");
174 element.setAttribute("startparag", 0); // #### FIXME
175 element.setAttribute("endparag", 0); // ?
176 bookmarks().appendChild(element);
177
178 _marginRect.unite(rects[Body]);
179}
180
181void Data::createParagraph(const TQString &text, ParagraphType type,
182 const TQValueVector<TQDomElement> &layouts,
183 const TQValueVector<TQDomElement> &formats)
184{
185 TQDomElement paragraph = _document.createElement("PARAGRAPH");
186 _textFramesets[type].appendChild(paragraph);
187
188 TQDomElement textElement = _document.createElement("TEXT");
189 textElement.appendChild( _document.createTextNode(text) );
190 paragraph.appendChild(textElement);
191
192 TQDomElement layout = _document.createElement("LAYOUT");
193 paragraph.appendChild(layout);
194 TQDomElement element = _document.createElement("NAME");
195 element.setAttribute("value", "Standard");
196 layout.appendChild(element);
197 for (uint i=0; i<layouts.size(); i++)
198 layout.appendChild(layouts[i]);
199
200 if ( formats.size() ) {
201 TQDomElement format = _document.createElement("FORMATS");
202 paragraph.appendChild(format);
203 for (uint i=0; i<formats.size(); i++)
204 format.appendChild(formats[i]);
205 }
206}
207
208void Data::endDump()
209{
210 if ( !_marginRect.isValid() ) _marginRect = _pageRect;
211 TQDomElement element = _document.createElement("PAPERBORDERS");
212 element.setAttribute("left", _marginRect.left() - _pageRect.left());
213 element.setAttribute("top", _marginRect.top() - _pageRect.top());
214 element.setAttribute("right", _pageRect.right() - _marginRect.right());
215 element.setAttribute("bottom", _pageRect.bottom() - _marginRect.bottom());
216 _paper.appendChild(element);
217}
The body is a frameset.
Definition body.h:38