forked from sumatrapdfreader/sumatrapdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEbookFormatter.cpp
380 lines (351 loc) · 11.5 KB
/
EbookFormatter.cpp
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
378
379
380
/* Copyright 2021 the SumatraPDF project authors (see AUTHORS file).
License: GPLv3 */
#include "utils/BaseUtil.h"
#include "utils/ScopedWin.h"
#include "utils/GdiPlusUtil.h"
#include "utils/WinUtil.h"
#include "utils/Archive.h"
#include "utils/HtmlParserLookup.h"
#include "utils/HtmlPullParser.h"
#include "mui/Mui.h"
#include "wingui/TreeModel.h"
#include "DisplayMode.h"
#include "Controller.h"
#include "EngineBase.h"
#include "EbookBase.h"
#include "EbookDoc.h"
#include "PalmDbReader.h"
#include "MobiDoc.h"
#include "HtmlFormatter.h"
#include "EbookFormatter.h"
/* Mobi-specific formatting methods */
MobiFormatter::MobiFormatter(HtmlFormatterArgs* args, MobiDoc* doc) : HtmlFormatter(args), doc(doc) {
bool fromBeginning = (0 == args->reparseIdx);
if (!doc || !fromBeginning) {
return;
}
ByteSlice* img = doc->GetCoverImage();
if (!img) {
return;
}
// TODO: vertically center the cover image?
EmitImage(img);
// only add a new page if the image isn't broken
if (currLineInstr.size() > 0) {
ForceNewPage();
}
}
// parses size in the form "1em" or "3pt". To interpret ems we need emInPoints
// to be passed by the caller
static float ParseSizeAsPixels(const char* s, size_t len, float emInPoints) {
float sizeInPoints = 0;
if (str::Parse(s, len, "%fem", &sizeInPoints)) {
sizeInPoints *= emInPoints;
} else if (str::Parse(s, len, "%fin", &sizeInPoints)) {
sizeInPoints *= 72;
} else if (str::Parse(s, len, "%fpt", &sizeInPoints)) {
// no conversion needed
} else if (str::Parse(s, len, "%fpx", &sizeInPoints)) {
return sizeInPoints;
} else {
return 0;
}
// TODO: take dpi into account
float sizeInPixels = sizeInPoints;
return sizeInPixels;
}
void MobiFormatter::HandleSpacing_Mobi(HtmlToken* t) {
if (!t->IsStartTag()) {
return;
}
// best I can tell, in mobi <p width="1em" height="3pt> means that
// the first line of the paragrap is indented by 1em and there's
// 3pt top padding (the same seems to apply for <blockquote>)
AttrInfo* attr = t->GetAttrByName("width");
if (attr) {
float lineIndent = ParseSizeAsPixels(attr->val, attr->valLen, CurrFont()->GetSize());
// there are files with negative width which produces partially invisible
// text, so don't allow that
if (lineIndent > 0) {
// this should replace the previously emitted paragraph/quote block
EmitParagraph(lineIndent);
}
}
attr = t->GetAttrByName("height");
if (attr) {
// for use it in FlushCurrLine()
currLineTopPadding = ParseSizeAsPixels(attr->val, attr->valLen, CurrFont()->GetSize());
}
}
// mobi format has image tags in the form:
// <img recindex="0000n" alt=""/>
// where recindex is the record number of pdb record
// that holds the image (within image record array, not a
// global record)
void MobiFormatter::HandleTagImg(HtmlToken* t) {
// we allow formatting raw html which can't require doc
if (!doc) {
return;
}
bool needAlt = true;
AttrInfo* attr = t->GetAttrByName("recindex");
if (attr) {
int n;
if (str::Parse(attr->val, attr->valLen, "%d", &n)) {
ByteSlice* img = doc->GetImage(n);
needAlt = !img || !EmitImage(img);
}
}
if (needAlt && (attr = t->GetAttrByName("alt")) != nullptr) {
HandleText(attr->val, attr->valLen);
}
}
void MobiFormatter::HandleHtmlTag(HtmlToken* t) {
CrashIf(!t->IsTag());
if (Tag_P == t->tag || Tag_Blockquote == t->tag) {
HtmlFormatter::HandleHtmlTag(t);
HandleSpacing_Mobi(t);
} else if (Tag_Mbp_Pagebreak == t->tag) {
ForceNewPage();
} else if (Tag_A == t->tag) {
HandleAnchorAttr(t);
// handle internal and external links (prefer internal ones)
if (!HandleTagA(t, "filepos")) {
HandleTagA(t);
}
} else if (Tag_Hr == t->tag) {
// imitating Kindle: hr is proceeded by an empty line
FlushCurrLine(false);
EmitEmptyLine(lineSpacing);
EmitHr();
} else {
HtmlFormatter::HandleHtmlTag(t);
}
}
/* EPUB-specific formatting methods */
void EpubFormatter::HandleTagImg(HtmlToken* t) {
CrashIf(!epubDoc);
if (t->IsEndTag()) {
return;
}
bool needAlt = true;
AttrInfo* attr = t->GetAttrByName("src");
if (attr) {
AutoFree src(str::Dup(attr->val, attr->valLen));
url::DecodeInPlace(src);
ByteSlice* img = epubDoc->GetImageData(src, pagePath);
needAlt = !img || !EmitImage(img);
}
if (needAlt && (attr = t->GetAttrByName("alt")) != nullptr) {
HandleText(attr->val, attr->valLen);
}
}
void EpubFormatter::HandleTagPagebreak(HtmlToken* t) {
AttrInfo* attr = t->GetAttrByName("page_path");
if (!attr || pagePath) {
ForceNewPage();
}
if (attr) {
Gdiplus::RectF bbox(0, currY, pageDx, 0);
currPage->instructions.Append(DrawInstr::Anchor(attr->val, attr->valLen, bbox));
pagePath.Set(str::Dup(attr->val, attr->valLen));
// reset CSS style rules for the new document
styleRules.Reset();
}
}
void EpubFormatter::HandleTagLink(HtmlToken* t) {
CrashIf(!epubDoc);
if (t->IsEndTag()) {
return;
}
AttrInfo* attr = t->GetAttrByName("rel");
if (!attr || !attr->ValIs("stylesheet")) {
return;
}
attr = t->GetAttrByName("type");
if (attr && !attr->ValIs("text/css")) {
return;
}
attr = t->GetAttrByName("href");
if (!attr) {
return;
}
AutoFree src(str::Dup(attr->val, attr->valLen));
url::DecodeInPlace(src);
AutoFree data(epubDoc->GetFileData(src, pagePath));
if (data.data) {
ParseStyleSheet(data.data, data.size());
}
}
void EpubFormatter::HandleTagSvgImage(HtmlToken* t) {
CrashIf(!epubDoc);
if (t->IsEndTag()) {
return;
}
if (!tagNesting.Contains(Tag_Svg) && Tag_Svg_Image != t->tag) {
return;
}
AttrInfo* attr = t->GetAttrByNameNS("href", "http://www.w3.org/1999/xlink");
if (!attr) {
return;
}
AutoFree src(str::Dup(attr->val, attr->valLen));
url::DecodeInPlace(src);
ByteSlice* img = epubDoc->GetImageData(src, pagePath);
if (img) {
EmitImage(img);
}
}
void EpubFormatter::HandleHtmlTag(HtmlToken* t) {
CrashIf(!t->IsTag());
if (hiddenDepth && t->IsEndTag() && tagNesting.size() == hiddenDepth && t->tag == tagNesting.Last()) {
hiddenDepth = 0;
UpdateTagNesting(t);
return;
}
if (0 == hiddenDepth && t->IsStartTag() && t->GetAttrByName("hidden")) {
hiddenDepth = tagNesting.size() + 1;
}
if (hiddenDepth > 0) {
UpdateTagNesting(t);
} else if (Tag_Image == t->tag || Tag_Svg_Image == t->tag) {
HandleTagSvgImage(t);
} else {
HtmlFormatter::HandleHtmlTag(t);
}
}
bool EpubFormatter::IgnoreText() {
return hiddenDepth > 0 || HtmlFormatter::IgnoreText();
}
/* FictionBook-specific formatting methods */
Fb2Formatter::Fb2Formatter(HtmlFormatterArgs* args, Fb2Doc* doc)
: HtmlFormatter(args), fb2Doc(doc), section(1), titleCount(0) {
if (args->reparseIdx != 0) {
return;
}
ByteSlice* cover = doc->GetCoverImage();
if (!cover) {
return;
}
EmitImage(cover);
// render larger images alone on the cover page,
// smaller images just separated by a horizontal line
if (0 == currLineInstr.size()) {
/* the image was broken */;
} else if (currLineInstr.Last().bbox.dy > args->pageDy / 2) {
ForceNewPage();
} else {
EmitHr();
}
}
void Fb2Formatter::HandleTagImg(HtmlToken* t) {
CrashIf(!fb2Doc);
if (t->IsEndTag()) {
return;
}
ByteSlice* img = nullptr;
AttrInfo* attr = t->GetAttrByNameNS("href", "http://www.w3.org/1999/xlink");
if (attr) {
AutoFree src(str::Dup(attr->val, attr->valLen));
url::DecodeInPlace(src);
img = fb2Doc->GetImageData(src);
}
if (img) {
EmitImage(img);
}
}
void Fb2Formatter::HandleTagAsHtml(HtmlToken* t, const char* name) {
HtmlToken tok;
tok.SetTag(t->type, name, name + str::Len(name));
HtmlFormatter::HandleHtmlTag(&tok);
}
// the name doesn't quite fit: this handles FB2 tags
void Fb2Formatter::HandleHtmlTag(HtmlToken* t) {
if (Tag_Title == t->tag || Tag_Subtitle == t->tag) {
bool isSubtitle = Tag_Subtitle == t->tag;
AutoFree name(str::Format("h%d", section + (isSubtitle ? 1 : 0)));
HtmlToken tok;
tok.SetTag(t->type, name, name + str::Len(name));
HandleTagHx(&tok);
HandleAnchorAttr(t);
if (!isSubtitle && t->IsStartTag()) {
char* link = (char*)Allocator::Alloc(textAllocator, 24);
sprintf_s(link, 24, FB2_TOC_ENTRY_MARK "%d", ++titleCount);
currPage->instructions.Append(DrawInstr::Anchor(link, str::Len(link), Gdiplus::RectF(0, currY, pageDx, 0)));
}
} else if (Tag_Section == t->tag) {
if (t->IsStartTag()) {
section++;
} else if (t->IsEndTag() && section > 1) {
section--;
}
FlushCurrLine(true);
HandleAnchorAttr(t);
} else if (Tag_P == t->tag) {
if (!tagNesting.Contains(Tag_Title)) {
HtmlFormatter::HandleHtmlTag(t);
}
} else if (Tag_Image == t->tag) {
HandleTagImg(t);
HandleAnchorAttr(t);
} else if (Tag_A == t->tag) {
HandleTagA(t, "href", "http://www.w3.org/1999/xlink");
HandleAnchorAttr(t, true);
} else if (Tag_Pagebreak == t->tag) {
ForceNewPage();
} else if (Tag_Strong == t->tag) {
HandleTagAsHtml(t, "b");
} else if (t->NameIs("emphasis")) {
HandleTagAsHtml(t, "i");
} else if (t->NameIs("epigraph")) {
HandleTagAsHtml(t, "blockquote");
} else if (t->NameIs("empty-line")) {
if (!t->IsEndTag()) {
EmitParagraph(0);
}
} else if (t->NameIs("stylesheet")) {
HandleTagAsHtml(t, "style");
}
}
/* standalone HTML-specific formatting methods */
void HtmlFileFormatter::HandleTagImg(HtmlToken* t) {
CrashIf(!htmlDoc);
if (t->IsEndTag()) {
return;
}
bool needAlt = true;
AttrInfo* attr = t->GetAttrByName("src");
if (attr) {
AutoFree src(str::Dup(attr->val, attr->valLen));
url::DecodeInPlace(src);
ByteSlice* img = htmlDoc->GetImageData(src);
needAlt = !img || !EmitImage(img);
}
if (needAlt && (attr = t->GetAttrByName("alt")) != nullptr) {
HandleText(attr->val, attr->valLen);
}
}
void HtmlFileFormatter::HandleTagLink(HtmlToken* t) {
CrashIf(!htmlDoc);
if (t->IsEndTag()) {
return;
}
AttrInfo* attr = t->GetAttrByName("rel");
if (!attr || !attr->ValIs("stylesheet")) {
return;
}
attr = t->GetAttrByName("type");
if (attr && !attr->ValIs("text/css")) {
return;
}
attr = t->GetAttrByName("href");
if (!attr) {
return;
}
AutoFree src(str::Dup(attr->val, attr->valLen));
url::DecodeInPlace(src);
AutoFree data(htmlDoc->GetFileData(src));
if (data.data) {
ParseStyleSheet(data.data, data.size());
}
}