summaryrefslogtreecommitdiffstats
path: root/filters/kword/pdf/data.cpp
blob: a6abf92975eb78972cd39fde3a60150fb490457d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * Copyright (c) 2002-2003 Nicolas HADACEK (hadacek@kde.org)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "data.h"

#include <kglobal.h>
#include <kdebug.h>
#include <klocale.h>


using namespace PDFImport;

static const char *TEXT_FRAMESET_NAMES[Nb_ParagraphTypes] = {
    I18N_NOOP("Body Frameset #%1"), I18N_NOOP("Header Frameset #%1"),
    I18N_NOOP("Footer Frameset #%1")
};

Data::Data(KoFilterChain *chain, const DRect &pageRect,
           KoPageLayout page, const Options &options)
    : pageIndex(0), _chain(chain), _imageIndex(1), _textIndex(1),
      _textFramesets(Nb_ParagraphTypes),
      _pageRect(pageRect), _options(options)
{
    _document = TQDomDocument("DOC");
    _document.appendChild(
        _document.createProcessingInstruction(
            "xml","version=\"1.0\" encoding=\"UTF-8\""));

    _mainElement = _document.createElement("DOC");
    _mainElement.setAttribute("editor", "KWord's PDF Import Filter");
    _mainElement.setAttribute("mime", "application/x-kword");
    _mainElement.setAttribute("syntaxVersion", 2);
    _document.appendChild(_mainElement);

    TQDomElement element = _document.createElement("ATTRIBUTES");
    element.setAttribute("processing", 1);
    element.setAttribute("hasHeader", 0);
    element.setAttribute("hasFooter", 0);
    element.setAttribute("hasTOC", 0);
    element.setAttribute("unit", "mm");
    _mainElement.appendChild(element);

    _paper = _document.createElement("PAPER");
    _paper.setAttribute("format", page.format);
    _paper.setAttribute("width", pageRect.width());
    _paper.setAttribute("height", pageRect.height());
    _paper.setAttribute("orientation", page.orientation);
    _paper.setAttribute("columns", 1);
    _paper.setAttribute("hType", 0);
    _paper.setAttribute("fType", 0);
    _mainElement.appendChild(_paper);

    // framesets
    _framesets = _document.createElement("FRAMESETS");
    _mainElement.appendChild(_framesets);

    // standard style
    TQDomElement styles = _document.createElement("STYLES");
    _mainElement.appendChild(styles);

    TQDomElement style = _document.createElement("STYLE");
    styles.appendChild(style);

    element = _document.createElement("FORMAT");
    Font font;
    font.format(_document, element, 0, 0, true);
    style.appendChild(element);

    element = _document.createElement("NAME");
    element.setAttribute("value","Standard");
    style.appendChild(element);

    element = _document.createElement("FOLLOWING");
    element.setAttribute("name","Standard");
    style.appendChild(element);

    // pictures
    _pictures = _document.createElement("PICTURES");
    _mainElement.appendChild(_pictures);

    // treat pages
    _bookmarks = _document.createElement("BOOKMARKS");
    _mainElement.appendChild(_bookmarks);
}

TQDomElement Data::pictureFrameset(const DRect &r)
{
    TQDomElement frameset = createFrameset(Picture, TQString());
    TQDomElement frame = createFrame(Picture, r, false);
    frameset.appendChild(frame);
    return frameset;
}

TQDomElement Data::createFrameset(FramesetType type, const TQString &n)
{
    bool text = (type==Text);
    uint &index = (text ? _textIndex : _imageIndex);

    TQDomElement frameset = _document.createElement("FRAMESET");
    frameset.setAttribute("frameType", (text ? 1 : 2));
    TQString name = n;
    if ( name.isNull() )
        name = (text ? i18n("Text Frameset %1")
                : i18n("Picture %1")).tqarg(index);
    frameset.setAttribute("name", name);
    frameset.setAttribute("frameInfo", 0);

//    kdDebug(30516) << "new frameset " << index << (text ? " text" : " image")
//                   << endl;
    index++;
    return frameset;
}

TQDomElement Data::createFrame(FramesetType type, const DRect &r,
                              bool forceMainFrameset)
{
    bool text = (type==Text);
    bool mainFrameset =
        (text ? (forceMainFrameset ? true : _textIndex==1) : false);

    TQDomElement frame = _document.createElement("FRAME");
    if (text) frame.setAttribute("autoCreateNewFrame", 0);
    frame.setAttribute("newFrameBehavior", 1);
    frame.setAttribute("runaround", 0);
    frame.setAttribute("left", r.left());
    frame.setAttribute("right", r.right());
    double offset = pageIndex * _pageRect.height();
    frame.setAttribute("top", r.top() + offset);
    frame.setAttribute("bottom", r.bottom() + offset);
    if ( text && !mainFrameset ) frame.setAttribute("bkStyle", 0);
    return frame;
}

void Data::initPage(const TQValueVector<DRect> &rects,
                    const TQValueList<TQDomElement> &pictures)
{
    for (uint i=0; i<Nb_ParagraphTypes; i++) {
//        kdDebug(30516) << "page #" << pageIndex << " rect #" << i
//                       << ": " << rects[i].toString() << endl;
        if ( !rects[i].isValid() ) continue;
        TQString name = i18n(TEXT_FRAMESET_NAMES[i]).tqarg(pageIndex);
        _textFramesets[i] = createFrameset(Text, name);
        _framesets.appendChild(_textFramesets[i]);
        TQDomElement frame = createFrame(Text, rects[i], true);
        _textFramesets[i].appendChild(frame);
    }

    TQValueList<TQDomElement>::const_iterator it;
    for (it = pictures.begin(); it!=pictures.end(); ++it)
        _framesets.appendChild(*it);

    // page bookmark
    TQDomElement element = createElement("BOOKMARKITEM");
    element.setAttribute("name", Link::pageLinkName(pageIndex));
    element.setAttribute("cursorIndexStart", 0); // ?
    element.setAttribute("cursorIndexEnd", 0); // ?
    element.setAttribute("frameset", "Text Frameset 1");
    element.setAttribute("startparag", 0); // #### FIXME
    element.setAttribute("endparag", 0); // ?
    bookmarks().appendChild(element);

    _marginRect.unite(rects[Body]);
}

void Data::createParagraph(const TQString &text, ParagraphType type,
                           const TQValueVector<TQDomElement> &layouts,
                           const TQValueVector<TQDomElement> &formats)
{
    TQDomElement paragraph = _document.createElement("PARAGRAPH");
    _textFramesets[type].appendChild(paragraph);

    TQDomElement textElement = _document.createElement("TEXT");
    textElement.appendChild( _document.createTextNode(text) );
    paragraph.appendChild(textElement);

    TQDomElement tqlayout = _document.createElement("LAYOUT");
    paragraph.appendChild(tqlayout);
    TQDomElement element = _document.createElement("NAME");
    element.setAttribute("value", "Standard");
    tqlayout.appendChild(element);
    for (uint i=0; i<layouts.size(); i++)
        tqlayout.appendChild(layouts[i]);

    if ( formats.size() ) {
        TQDomElement format = _document.createElement("FORMATS");
        paragraph.appendChild(format);
        for (uint i=0; i<formats.size(); i++)
            format.appendChild(formats[i]);
    }
}

void Data::endDump()
{
    if ( !_marginRect.isValid() ) _marginRect = _pageRect;
    TQDomElement element = _document.createElement("PAPERBORDERS");
    element.setAttribute("left", _marginRect.left() - _pageRect.left());
    element.setAttribute("top", _marginRect.top() - _pageRect.top());
    element.setAttribute("right", _pageRect.right() - _marginRect.right());
    element.setAttribute("bottom", _pageRect.bottom() - _marginRect.bottom());
    _paper.appendChild(element);
}