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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
/*
* Copyright (C) 2004-2012 Geometer Plus <[email protected]>
*
* 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 <vector>
#include <string>
#include <ZLInputStream.h>
#include <ZLLogger.h>
#include <ZLFile.h>
#include <ZLStringUtil.h>
#include <ZLFileImage.h>
#include "DocBookReader.h"
#include "../../bookmodel/BookModel.h"
#include "../../library/Book.h"
#include "OleStorage.h"
#include "OleMainStream.h"
DocBookReader::DocBookReader(BookModel &model, const std::string &encoding) :
myModelReader(model),
myPictureCounter(0),
myEncoding(encoding) {
myReadState = READ_TEXT;
}
bool DocBookReader::readBook() {
const ZLFile &file = myModelReader.model().book()->file();
shared_ptr<ZLInputStream> stream = file.inputStream();
if (stream.isNull() || !stream->open()) {
return false;
}
myModelReader.setMainTextModel();
myModelReader.pushKind(REGULAR);
myModelReader.beginParagraph();
if (!readDocument(stream, true)) {
return false;
}
myModelReader.insertEndOfTextParagraph();
return true;
}
void DocBookReader::handleChar(ZLUnicodeUtil::Ucs2Char ucs2char) {
if (myReadState == READ_FIELD && myReadFieldState == READ_FIELD_INFO) {
myFieldInfoBuffer.push_back(ucs2char);
return;
}
if (myReadState == READ_FIELD && myReadFieldState == DONT_READ_FIELD_TEXT) {
return;
}
if (myReadState == READ_FIELD && myReadFieldState == READ_FIELD_TEXT && ucs2char == WORD_HORIZONTAL_TAB) {
//to remove pagination from TOC (from doc saved in OpenOffice)
myReadFieldState = DONT_READ_FIELD_TEXT;
return;
}
std::string utf8String;
ZLUnicodeUtil::Ucs2String ucs2String;
ucs2String.push_back(ucs2char);
ZLUnicodeUtil::ucs2ToUtf8(utf8String, ucs2String);
if (!myModelReader.paragraphIsOpen()) {
myModelReader.beginParagraph();
}
myModelReader.addData(utf8String);
}
void DocBookReader::handleHardLinebreak() {
if (myModelReader.paragraphIsOpen()) {
myModelReader.endParagraph();
}
myModelReader.beginParagraph();
if (!myCurrentStyleEntry.isNull()) {
myModelReader.addStyleEntry(*myCurrentStyleEntry);
}
for (std::size_t i = 0; i < myKindStack.size(); ++i) {
myModelReader.addControl(myKindStack.at(i), true);
}
}
void DocBookReader::handleParagraphEnd() {
if (myModelReader.paragraphIsOpen()) {
myModelReader.endParagraph();
}
myModelReader.beginParagraph();
myCurrentStyleEntry = 0;
}
void DocBookReader::handlePageBreak() {
if (myModelReader.paragraphIsOpen()) {
myModelReader.endParagraph();
}
myCurrentStyleEntry = 0;
myModelReader.insertEndOfSectionParagraph();
myModelReader.beginParagraph();
}
void DocBookReader::handleTableSeparator() {
handleChar(SPACE);
handleChar(VERTICAL_LINE);
handleChar(SPACE);
}
void DocBookReader::handleTableEndRow() {
handleParagraphEnd();
}
void DocBookReader::handleFootNoteMark() {
//TODO implement
}
void DocBookReader::handleStartField() {
if (myReadState == READ_FIELD) { //for nested fields
handleEndField();
}
myReadState = READ_FIELD;
myReadFieldState = READ_FIELD_INFO;
myHyperlinkTypeState = NO_HYPERLINK;
}
void DocBookReader::handleSeparatorField() {
static const std::string HYPERLINK = "HYPERLINK";
static const std::string SEQUENCE = "SEQ";
// static const std::string PAGE = "PAGE";
// static const std::string PAGEREF = "PAGEREF";
// static const std::string SHAPE = "SHAPE";
static const std::string SPACE_DELIMETER = " ";
static const std::string LOCAL_LINK = "\\l";
static const std::string QUOTE = "\"";
myReadFieldState = READ_FIELD_TEXT;
myHyperlinkTypeState = NO_HYPERLINK;
ZLUnicodeUtil::Ucs2String buffer = myFieldInfoBuffer;
myFieldInfoBuffer.clear();
std::string utf8String;
ZLUnicodeUtil::ucs2ToUtf8(utf8String, buffer);
ZLUnicodeUtil::utf8Trim(utf8String);
if (utf8String.empty()) {
return;
}
std::vector<std::string> result = ZLStringUtil::split(utf8String, SPACE_DELIMETER);
//TODO split function can returns empty string, maybe fix it
std::vector<std::string> splitted;
for (std::size_t i = 0; i < result.size(); ++i) {
if (!result.at(i).empty()) {
splitted.push_back(result.at(i));
}
}
if (!splitted.empty() && splitted.at(0) == SEQUENCE) {
myReadFieldState = READ_FIELD_TEXT;
myHyperlinkTypeState = NO_HYPERLINK;
return;
}
if (splitted.size() < 2 || splitted.at(0) != HYPERLINK) {
myReadFieldState = DONT_READ_FIELD_TEXT;
//to remove pagination from TOC and not hyperlink fields
return;
}
if (splitted.at(1) == LOCAL_LINK) {
std::string link = parseLink(buffer);
if (!link.empty()) {
myModelReader.addHyperlinkControl(INTERNAL_HYPERLINK, link);
myHyperlinkTypeState = INT_HYPERLINK_INSERTED;
}
} else {
std::string link = parseLink(buffer, true);
if (!link.empty()) {
myModelReader.addHyperlinkControl(EXTERNAL_HYPERLINK, link);
myHyperlinkTypeState = EXT_HYPERLINK_INSERTED;
}
}
}
void DocBookReader::handleEndField() {
myFieldInfoBuffer.clear();
if (myReadState == READ_TEXT) {
return;
}
if (myHyperlinkTypeState == EXT_HYPERLINK_INSERTED) {
myModelReader.addControl(EXTERNAL_HYPERLINK, false);
} else if (myHyperlinkTypeState == INT_HYPERLINK_INSERTED) {
myModelReader.addControl(INTERNAL_HYPERLINK, false);
}
myReadState = READ_TEXT;
myHyperlinkTypeState = NO_HYPERLINK;
}
void DocBookReader::handleImage(const ZLFileImage::Blocks &blocks) {
std::string number;
ZLStringUtil::appendNumber(number, myPictureCounter++);
myModelReader.addImageReference(number);
ZLFile file(myModelReader.model().book()->file().path(), ZLMimeType::IMAGE_AUTO);
myModelReader.addImage(number, new ZLFileImage(file, blocks, ZLFileImage::ENCODING_NONE));
}
void DocBookReader::handleOtherControlChar(ZLUnicodeUtil::Ucs2Char ucs2char) {
if (ucs2char == WORD_MINUS) {
handleChar(MINUS);
} else if (ucs2char == WORD_SOFT_HYPHEN) {
//skip
} else if (ucs2char == WORD_HORIZONTAL_TAB) {
handleChar(ucs2char);
} else {
// myTextBuffer.clear();
}
}
void DocBookReader::handleFontStyle(unsigned int fontStyle) {
if (myReadState == READ_FIELD && myReadFieldState == READ_FIELD_TEXT && myHyperlinkTypeState != NO_HYPERLINK) {
//to fix bug with hyperlink, that's only bold and doesn't looks like hyperlink
return;
}
while (!myKindStack.empty()) {
myModelReader.addControl(myKindStack.back(), false);
myKindStack.pop_back();
}
if (fontStyle & OleMainStream::CharInfo::FONT_BOLD) {
myKindStack.push_back(BOLD);
}
if (fontStyle & OleMainStream::CharInfo::FONT_ITALIC) {
myKindStack.push_back(ITALIC);
}
for (std::size_t i = 0; i < myKindStack.size(); ++i) {
myModelReader.addControl(myKindStack.at(i), true);
}
}
void DocBookReader::handleParagraphStyle(const OleMainStream::Style &styleInfo) {
if (styleInfo.HasPageBreakBefore) {
handlePageBreak();
}
shared_ptr<ZLTextStyleEntry> entry = new ZLTextStyleEntry(ZLTextStyleEntry::STYLE_OTHER_ENTRY);
switch (styleInfo.Alignment) {
default: // in that case, use default alignment type
break;
case OleMainStream::Style::ALIGNMENT_LEFT:
entry->setAlignmentType(ALIGN_LEFT);
break;
case OleMainStream::Style::ALIGNMENT_RIGHT:
entry->setAlignmentType(ALIGN_RIGHT);
break;
case OleMainStream::Style::ALIGNMENT_CENTER:
entry->setAlignmentType(ALIGN_CENTER);
break;
case OleMainStream::Style::ALIGNMENT_JUSTIFY:
entry->setAlignmentType(ALIGN_JUSTIFY);
break;
}
//TODO in case, where style is heading, but size is small it works wrong
const ZLTextStyleEntry::SizeUnit unit = ZLTextStyleEntry::SIZE_UNIT_PERCENT;
switch (styleInfo.StyleIdCurrent) {
default:
break;
case OleMainStream::Style::STYLE_H1:
entry->setLength(ZLTextStyleEntry::LENGTH_FONT_SIZE, 140, unit);
break;
case OleMainStream::Style::STYLE_H2:
entry->setLength(ZLTextStyleEntry::LENGTH_FONT_SIZE, 120, unit);
break;
case OleMainStream::Style::STYLE_H3:
entry->setLength(ZLTextStyleEntry::LENGTH_FONT_SIZE, 110, unit);
break;
}
myCurrentStyleEntry = entry;
myModelReader.addStyleEntry(*myCurrentStyleEntry);
// we should have the same font style, as for the previous paragraph,
// if it has the same StyleIdCurrent
if (myCurrentStyleInfo.StyleIdCurrent != OleMainStream::Style::STYLE_INVALID &&
myCurrentStyleInfo.StyleIdCurrent == styleInfo.StyleIdCurrent) {
for (std::size_t i = 0; i < myKindStack.size(); ++i) {
myModelReader.addControl(myKindStack.at(i), true);
}
} else {
myKindStack.clear();
// fill by the fontstyle, that was got from Stylesheet
handleFontStyle(styleInfo.CurrentCharInfo.FontStyle);
}
myCurrentStyleInfo = styleInfo;
}
void DocBookReader::handleBookmark(const std::string &name) {
myModelReader.addHyperlinkLabel(name);
}
std::string DocBookReader::parseLink(ZLUnicodeUtil::Ucs2String s, bool urlencode) {
//TODO add support for HYPERLINK like that:
// [0x13] HYPERLINK "http://site.ru/some text" \t "_blank" [0x14] text [0x15]
//Current implementation search for last QUOTE, so, it reads \t and _blank as part of link
//Last quote searching is need to handle link like that:
// [0x13] HYPERLINK "http://yandex.ru/yandsearch?text='some text' и "some text2"" [0x14] link text [0x15]
static const ZLUnicodeUtil::Ucs2Char QUOTE = 0x22;
std::size_t i, first = 0;
//TODO maybe functions findFirstOf and findLastOf should be in ZLUnicodeUtil class
for (i = 0; i < s.size(); ++i) {
if (s.at(i) == QUOTE) {
first = i;
break;
}
}
if (i == s.size()) {
return std::string();
}
std::size_t j, last = 0;
for (j = s.size(); j > 0 ; --j) {
if (s.at(j - 1) == QUOTE) {
last = j - 1;
break;
}
}
if (j == 0 || last == first) {
return std::string();
}
ZLUnicodeUtil::Ucs2String link;
for (std::size_t k = first + 1; k < last; ++k) {
ZLUnicodeUtil::Ucs2Char ch = s.at(k);
if (urlencode && ZLUnicodeUtil::isSpace(ch)) {
//TODO maybe implement function for encoding all signs in url, not only spaces and quotes
//TODO maybe add backslash support
link.push_back('%');
link.push_back('2');
link.push_back('0');
} else if (urlencode && ch == QUOTE) {
link.push_back('%');
link.push_back('2');
link.push_back('2');
} else {
link.push_back(ch);
}
}
std::string utf8String;
ZLUnicodeUtil::ucs2ToUtf8(utf8String, link);
return utf8String;
}
void DocBookReader::footnotesStartHandler() {
handlePageBreak();
}
void DocBookReader::ansiDataHandler(const char *buffer, std::size_t len) {
if (myConverter.isNull()) {
// lazy converter initialization
ZLEncodingCollection &collection = ZLEncodingCollection::Instance();
ZLEncodingConverterInfoPtr info = collection.info(myEncoding);
myConverter = info.isNull() ? collection.defaultConverter() : info->createConverter();
}
std::string utf8String;
myConverter->convert(utf8String, buffer, buffer + len);
ZLUnicodeUtil::utf8ToUcs2(myBuffer, utf8String);
}
void DocBookReader::ucs2SymbolHandler(ZLUnicodeUtil::Ucs2Char symbol) {
myBuffer.push_back(symbol);
}
|