forked from coolwanglu/pdf2htmlEX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.cc
More file actions
361 lines (305 loc) · 10.1 KB
/
Copy pathinstall.cc
File metadata and controls
361 lines (305 loc) · 10.1 KB
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
/*
* install.cc
*
* maintaining all known styles
*
* by WangLu
* 2012.08.14
*/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <GlobalParams.h>
#include "Param.h"
#include "HTMLRenderer.h"
#include "util/namespace.h"
#include "util/math.h"
#include "util/misc.h"
namespace pdf2htmlEX {
using std::abs;
using std::cerr;
using std::endl;
const FontInfo * HTMLRenderer::install_font(GfxFont * font)
{
assert(sizeof(long long) == 2*sizeof(int));
long long fn_id = (font == nullptr) ? 0 : hash_ref(font->getID());
auto iter = font_name_map.find(fn_id);
if(iter != font_name_map.end())
return &(iter->second);
long long new_fn_id = font_name_map.size();
auto cur_info_iter = font_name_map.insert(make_pair(fn_id, FontInfo())).first;
FontInfo & new_font_info = cur_info_iter->second;
new_font_info.id = new_fn_id;
new_font_info.use_tounicode = true;
if(font == nullptr)
{
new_font_info.ascent = 0;
new_font_info.descent = 0;
new_font_info.is_type3 = false;
export_remote_default_font(new_fn_id);
return &(new_font_info);
}
new_font_info.ascent = font->getAscent();
new_font_info.descent = font->getDescent();
new_font_info.is_type3 = (font->getType() == fontType3);
if(param->debug)
{
cerr << "Install font: (" << (font->getID()->num) << ' ' << (font->getID()->gen) << ") -> " << "f" << hex << new_fn_id << dec << endl;
}
if(font->getType() == fontType3) {
cerr << "Type 3 fonts are unsupported and will be rendered as Image" << endl;
export_remote_default_font(new_fn_id);
return &(cur_info_iter->second);
}
if(font->getWMode()) {
cerr << "Writing mode is unsupported and will be rendered as Image" << endl;
export_remote_default_font(new_fn_id);
return &(cur_info_iter->second);
}
auto * font_loc = font->locateFont(xref, gTrue);
if(font_loc != nullptr)
{
switch(font_loc -> locType)
{
case gfxFontLocEmbedded:
install_embedded_font(font, cur_info_iter->second);
break;
case gfxFontLocExternal:
install_external_font(font, cur_info_iter->second);
break;
case gfxFontLocResident:
install_base_font(font, font_loc, cur_info_iter->second);
break;
default:
cerr << "TODO: other font loc" << endl;
export_remote_default_font(new_fn_id);
break;
}
delete font_loc;
}
else
{
export_remote_default_font(new_fn_id);
}
return &(cur_info_iter->second);
}
void HTMLRenderer::install_embedded_font(GfxFont * font, FontInfo & info)
{
auto path = dump_embedded_font(font, info.id);
if(path != "")
{
embed_font(path, font, info);
export_remote_font(info, param->font_suffix, font);
}
else
{
export_remote_default_font(info.id);
}
}
void HTMLRenderer::install_base_font(GfxFont * font, GfxFontLoc * font_loc, FontInfo & info)
{
string psname(font_loc->path->getCString());
string basename = psname.substr(0, psname.find('-'));
GfxFontLoc * localfontloc = font->locateFont(xref, gFalse);
if(param->embed_base_font)
{
if(localfontloc != nullptr)
{
embed_font(localfontloc->path->getCString(), font, info);
export_remote_font(info, param->font_suffix, font);
delete localfontloc;
return;
}
else
{
cerr << "Cannot embed base font: f" << hex << info.id << dec << ' ' << psname << endl;
// fallback to exporting by name
}
}
string cssfont;
auto iter = BASE_14_FONT_CSS_FONT_MAP.find(basename);
if(iter == BASE_14_FONT_CSS_FONT_MAP.end())
{
cerr << "PS Font: " << basename << " not found in the base 14 font map" << endl;
cssfont = "";
}
else
cssfont = iter->second;
// still try to get an idea of read ascent/descent
if(localfontloc != nullptr)
{
// fill in ascent/descent only, do not embed
embed_font(string(localfontloc->path->getCString()), font, info, true);
delete localfontloc;
}
else
{
info.ascent = font->getAscent();
info.descent = font->getDescent();
}
export_local_font(info, font, psname, cssfont);
}
void HTMLRenderer::install_external_font(GfxFont * font, FontInfo & info)
{
string fontname(font->getName()->getCString());
// resolve bad encodings in GB
auto iter = GB_ENCODED_FONT_NAME_MAP.find(fontname);
if(iter != GB_ENCODED_FONT_NAME_MAP.end())
{
fontname = iter->second;
cerr << "Warning: workaround for font names in bad encodings." << endl;
}
GfxFontLoc * localfontloc = font->locateFont(xref, gFalse);
if(param->embed_external_font)
{
if(localfontloc != nullptr)
{
embed_font(string(localfontloc->path->getCString()), font, info);
export_remote_font(info, param->font_suffix, font);
delete localfontloc;
return;
}
else
{
cerr << "Cannot embed external font: f" << hex << info.id << dec << ' ' << fontname << endl;
// fallback to exporting by name
}
}
// still try to get an idea of read ascent/descent
if(localfontloc != nullptr)
{
// fill in ascent/descent only, do not embed
embed_font(string(localfontloc->path->getCString()), font, info, true);
delete localfontloc;
}
else
{
info.ascent = font->getAscent();
info.descent = font->getDescent();
}
export_local_font(info, font, fontname, "");
}
long long HTMLRenderer::install_font_size(double font_size)
{
auto iter = font_size_map.lower_bound(font_size - EPS);
if((iter != font_size_map.end()) && (equal(iter->first, font_size)))
return iter->second;
long long new_fs_id = font_size_map.size();
font_size_map.insert(make_pair(font_size, new_fs_id));
export_font_size(new_fs_id, font_size);
return new_fs_id;
}
long long HTMLRenderer::install_transform_matrix(const double * tm)
{
Matrix m;
memcpy(m.m, tm, sizeof(m.m));
auto iter = transform_matrix_map.lower_bound(m);
if((iter != transform_matrix_map.end()) && (tm_equal(m.m, iter->first.m, 4)))
return iter->second;
long long new_tm_id = transform_matrix_map.size();
transform_matrix_map.insert(make_pair(m, new_tm_id));
export_transform_matrix(new_tm_id, tm);
return new_tm_id;
}
long long HTMLRenderer::install_letter_space(double letter_space)
{
auto iter = letter_space_map.lower_bound(letter_space - EPS);
if((iter != letter_space_map.end()) && (equal(iter->first, letter_space)))
return iter->second;
long long new_ls_id = letter_space_map.size();
letter_space_map.insert(make_pair(letter_space, new_ls_id));
export_letter_space(new_ls_id, letter_space);
return new_ls_id;
}
long long HTMLRenderer::install_word_space(double word_space)
{
auto iter = word_space_map.lower_bound(word_space - EPS);
if((iter != word_space_map.end()) && (equal(iter->first, word_space)))
return iter->second;
long long new_ws_id = word_space_map.size();
word_space_map.insert(make_pair(word_space, new_ws_id));
export_word_space(new_ws_id, word_space);
return new_ws_id;
}
long long HTMLRenderer::install_fill_color(const GfxRGB * rgb)
{
// transparent
if (rgb == nullptr) {
return -1;
}
const GfxRGB & c = *rgb;
auto iter = fill_color_map.find(c);
if(iter != fill_color_map.end())
return iter->second;
long long new_color_id = fill_color_map.size();
fill_color_map.insert(make_pair(c, new_color_id));
export_fill_color(new_color_id, rgb);
return new_color_id;
}
long long HTMLRenderer::install_stroke_color(const GfxRGB * rgb)
{
// transparent
if (rgb == nullptr) {
return -1;
}
const GfxRGB & c = *rgb;
auto iter = stroke_color_map.find(c);
if(iter != stroke_color_map.end())
return iter->second;
long long new_color_id = stroke_color_map.size();
stroke_color_map.insert(make_pair(c, new_color_id));
export_stroke_color(new_color_id, rgb);
return new_color_id;
}
long long HTMLRenderer::install_whitespace(double ws_width, double & actual_width)
{
// ws_width is already mulitpled by draw_scale
auto iter = whitespace_map.lower_bound(ws_width - param->h_eps);
if((iter != whitespace_map.end()) && (abs(iter->first - ws_width) <= param->h_eps))
{
actual_width = iter->first;
return iter->second;
}
actual_width = ws_width;
long long new_ws_id = whitespace_map.size();
whitespace_map.insert(make_pair(ws_width, new_ws_id));
export_whitespace(new_ws_id, ws_width);
return new_ws_id;
}
long long HTMLRenderer::install_rise(double rise)
{
auto iter = rise_map.lower_bound(rise - param->v_eps);
if((iter != rise_map.end()) && (abs(iter->first - rise) <= param->v_eps))
{
return iter->second;
}
long long new_rise_id = rise_map.size();
rise_map.insert(make_pair(rise, new_rise_id));
export_rise(new_rise_id, rise);
return new_rise_id;
}
long long HTMLRenderer::install_height(double height)
{
auto iter = height_map.lower_bound(height - EPS);
if((iter != height_map.end()) && (abs(iter->first - height) <= EPS))
{
return iter->second;
}
long long new_height_id = height_map.size();
height_map.insert(make_pair(height, new_height_id));
export_height(new_height_id, height);
return new_height_id;
}
long long HTMLRenderer::install_left(double left)
{
auto iter = left_map.lower_bound(left - param->h_eps);
if((iter != left_map.end()) && (abs(iter->first - left) <= param->h_eps))
{
return iter->second;
}
long long new_left_id = left_map.size();
left_map.insert(make_pair(left, new_left_id));
export_left(new_left_id, left);
return new_left_id;
}
} // namespace pdf2htmlEX