This repository was archived by the owner on Dec 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathHTMLTextLine.cc
More file actions
748 lines (665 loc) · 24.7 KB
/
HTMLTextLine.cc
File metadata and controls
748 lines (665 loc) · 24.7 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/*
* HTMLTextLine.cc
*
* Generate and optimized HTML for one line
*
* Copyright (C) 2012,2013 Lu Wang <coolwanglu@gmail.com>
*/
#include <cmath>
#include <algorithm>
#include "HTMLTextLine.h"
#include "util/encoding.h"
#include "util/css_const.h"
#include "util/unicode.h"
namespace pdf2htmlEX {
using std::min;
using std::max;
using std::vector;
using std::ostream;
using std::cerr;
using std::endl;
using std::find;
using std::abs;
HTMLTextLine::HTMLTextLine (const HTMLLineState & line_state, const Param & param, AllStateManager & all_manager)
:param(param)
,all_manager(all_manager)
,line_state(line_state)
,clip_x1(0)
,clip_y1(0)
,width(0)
,last_output_unicode(0)
{ }
void HTMLTextLine::append_unicodes(const Unicode * u, int l, double width)
{
if (l == 1)
text.push_back(min(u[0], (unsigned)INT_MAX));
else if (l > 1)
{
text.push_back(- decomposed_text.size() - 1);
decomposed_text.emplace_back();
decomposed_text.back().assign(u, u + l);
}
this->width += width;
}
void HTMLTextLine::append_offset(double width)
{
/*
* If the last offset is very thin, we can ignore it and directly use it
* But this should not happen often, and we will also filter near-zero offsets when outputting them
* So don't check it.
*
* Offset must be appended immediately after the last real (non-padding) char, or the text optimizing
* algorithm may be confused: it may wrongly convert offsets at the beginning of a line to word-space.
*/
auto offset_idx = text.size();
while (offset_idx > 0 && text[offset_idx - 1] == 0)
--offset_idx;
if((!offsets.empty()) && (offsets.back().start_idx == offset_idx))
offsets.back().width += width;
else
offsets.emplace_back(offset_idx, width);
this->width += width;
}
void HTMLTextLine::append_state(const HTMLTextState & text_state)
{
if(states.empty() || (states.back().start_idx != text.size()))
{
states.emplace_back();
states.back().start_idx = text.size();
states.back().hash_umask = 0;
}
HTMLTextState & last_state = states.back();
last_state = text_state;
//apply font scale
last_state.font_size *= last_state.font_info->font_size_scale;
}
void HTMLTextLine::dump_char(std::ostream & out, int pos)
{
int c = text[pos];
if (c > 0)
{
dump_unicode(out, c);
}
else if (c < 0)
{
auto dt = decomposed_text[- c - 1];
for (auto it = dt.begin(), end = dt.end(); it != end; it++)
dump_unicode(out, *it);
}
}
void HTMLTextLine::dump_unicode(std::ostream & out, Unicode u)
{
// ZWSP following space can be optimized out.
if (u == zero_width_space && last_output_unicode == ' ')
return;
writeUnicodes(out, &u, 1);
last_output_unicode = u;
}
void HTMLTextLine::dump_chars(ostream & out, int begin, int len)
{
static const Color transparent(0, 0, 0, true);
if (line_state.first_char_index < 0)
{
for (int i = 0; i < len; i++)
dump_char(out, begin + i);
return;
}
bool invisible_group_open = false;
for(int i = 0; i < len; i++)
{
if (!line_state.is_char_covered(line_state.first_char_index + begin + i)) //visible
{
if (invisible_group_open)
{
invisible_group_open = false;
out << "</span>";
}
dump_char(out, begin + i);
}
else
{
if (!invisible_group_open)
{
out << "<span class=\"" << all_manager.fill_color.get_css_class_name()
<< all_manager.fill_color.install(transparent) << " " << all_manager.stroke_color.get_css_class_name()
<< all_manager.stroke_color.install(transparent) << "\">";
invisible_group_open = true;
}
dump_char(out, begin + i);
}
}
if (invisible_group_open)
out << "</span>";
}
void HTMLTextLine::dump_text(ostream & out)
{
/*
* Each Line is an independent absolute positioned block
* so even we have a few states or offsets, we may omit them
*/
if(text.empty())
return;
if(states.empty() || (states[0].start_idx != 0))
{
cerr << "Warning: text without a style! Must be a bug in pdf2htmlEX" << endl;
return;
}
// Start Output
{
// open <div> for the current text line
out << "<div class=\"" << CSS::LINE_CN
<< " " << CSS::TRANSFORM_MATRIX_CN << all_manager.transform_matrix.install(line_state.transform_matrix)
<< " " << CSS::LEFT_CN << all_manager.left.install(line_state.x - clip_x1)
<< " " << CSS::HEIGHT_CN << all_manager.height.install(ascent)
<< " " << CSS::BOTTOM_CN << all_manager.bottom.install(line_state.y - clip_y1)
;
// it will be closed by the first state
last_output_unicode = 0;
}
std::vector<State*> stack;
// a special safeguard in the bottom
stack.push_back(nullptr);
//accumulated horizontal offset;
double dx = 0;
// whenever a negative offset appears, we should not pop out that <span>
// otherwise the effect of negative margin-left would disappear
size_t last_text_pos_with_negative_offset = 0;
size_t cur_text_idx = 0;
auto cur_offset_iter = offsets.begin();
for(auto state_iter2 = states.begin(), state_iter1 = state_iter2++;
state_iter1 != states.end();
++state_iter1, ++state_iter2)
{
// export current state, find a closest parent
{
// greedy
double vertical_align = state_iter1->vertical_align;
int best_cost = State::HASH_ID_COUNT + 1;
// we have a nullptr at the beginning, so no need to check for rend
for(auto iter = stack.rbegin(); *iter; ++iter)
{
int cost = state_iter1->diff(**iter);
if(!equal(vertical_align,0))
++cost;
if(cost < best_cost)
{
while(stack.back() != *iter)
{
stack.back()->end(out);
stack.pop_back();
}
best_cost = cost;
state_iter1->vertical_align = vertical_align;
if(best_cost == 0)
break;
}
// cannot go further
if((*iter)->start_idx <= last_text_pos_with_negative_offset)
break;
vertical_align += (*iter)->vertical_align;
}
//
state_iter1->ids[State::VERTICAL_ALIGN_ID] = all_manager.vertical_align.install(state_iter1->vertical_align);
// export the diff between *state_iter1 and stack.back()
state_iter1->begin(out, stack.back());
stack.push_back(&*state_iter1);
}
// [state_iter1->start_idx, text_idx2) are covered by the current state
size_t text_idx2 = (state_iter2 == states.end()) ? text.size() : state_iter2->start_idx;
// dump all text and offsets before next state
while(true)
{
if((cur_offset_iter != offsets.end())
&& (cur_offset_iter->start_idx <= cur_text_idx))
{
if(cur_offset_iter->start_idx > text_idx2)
break;
// next is offset
double target = cur_offset_iter->width + dx;
double actual_offset = 0;
//ignore near-zero offsets
if(std::abs(target) <= param.h_eps)
{
actual_offset = 0;
}
else
{
bool done = false;
// check if the offset is equivalent to a single ' '
if(!(state_iter1->hash_umask & State::umask_by_id(State::WORD_SPACE_ID)))
{
double space_off = state_iter1->single_space_offset();
if(std::abs(target - space_off) <= param.h_eps)
{
dump_unicode(out, ' ');
actual_offset = space_off;
done = true;
}
}
// finally, just dump it
if(!done)
{
long long wid = all_manager.whitespace.install(target, &actual_offset);
if(!equal(actual_offset, 0))
{
if(is_positive(-actual_offset))
last_text_pos_with_negative_offset = cur_text_idx;
double threshold = state_iter1->em_size() * (param.space_threshold);
out << "<span class=\"" << CSS::WHITESPACE_CN
<< ' ' << CSS::WHITESPACE_CN << wid << "\">";
if (target > (threshold - EPS))
dump_unicode(out, ' ');
out << "</span>";
}
}
}
dx = target - actual_offset;
++ cur_offset_iter;
}
else
{
if(cur_text_idx >= text_idx2)
break;
// next is text
size_t next_text_idx = text_idx2;
if((cur_offset_iter != offsets.end()) && (cur_offset_iter->start_idx) < next_text_idx)
next_text_idx = cur_offset_iter->start_idx;
dump_chars(out, cur_text_idx, next_text_idx - cur_text_idx);
cur_text_idx = next_text_idx;
}
}
}
// we have a nullptr in the bottom
while(stack.back())
{
stack.back()->end(out);
stack.pop_back();
}
out << "</div>";
}
void HTMLTextLine::clear(void)
{
states.clear();
offsets.clear();
text.clear();
}
void HTMLTextLine::clip(const HTMLClipState & clip_state)
{
clip_x1 = clip_state.xmin;
clip_y1 = clip_state.ymin;
}
void HTMLTextLine::prepare(void)
{
// max_ascent determines the height of the div
double accum_vertical_align = 0; // accumulated
ascent = 0;
descent = 0;
// note that vertical_align cannot be calculated here
for(auto iter = states.begin(); iter != states.end(); ++iter)
{
auto font_info = iter->font_info;
iter->ids[State::FONT_ID] = font_info->id;
iter->ids[State::FONT_SIZE_ID] = all_manager.font_size.install(iter->font_size);
iter->ids[State::FILL_COLOR_ID] = all_manager.fill_color.install(iter->fill_color);
iter->ids[State::STROKE_COLOR_ID] = all_manager.stroke_color.install(iter->stroke_color);
iter->ids[State::LETTER_SPACE_ID] = all_manager.letter_space.install(iter->letter_space);
iter->ids[State::WORD_SPACE_ID] = all_manager.word_space.install(iter->word_space);
iter->hash();
accum_vertical_align += iter->vertical_align;
double cur_ascent = accum_vertical_align + font_info->ascent * iter->font_size;
if(cur_ascent > ascent)
ascent = cur_ascent;
double cur_descent = accum_vertical_align + font_info->descent * iter->font_size;
if(cur_descent < descent)
descent = cur_descent;
}
}
void HTMLTextLine::optimize(std::vector<HTMLTextLine*> & lines)
{
if(param.optimize_text == 3)
{
optimize_aggressive(lines);
}
else
{
optimize_normal(lines);
}
}
/*
* Adjust letter space and word space in order to reduce the number of HTML elements
* May also unmask word space
*/
void HTMLTextLine::optimize_normal(std::vector<HTMLTextLine*> & lines)
{
// remove useless states in the end
while((!states.empty()) && (states.back().start_idx >= text.size()))
states.pop_back();
assert(!states.empty());
const long long word_space_umask = State::umask_by_id(State::WORD_SPACE_ID);
// for optimization, we need accurate values
auto & ls_manager = all_manager.letter_space;
auto & ws_manager = all_manager.word_space;
// statistics of widths
std::map<double, size_t> width_map;
// store optimized offsets
std::vector<Offset> new_offsets;
new_offsets.reserve(offsets.size());
auto offset_iter1 = offsets.begin();
for(auto state_iter2 = states.begin(), state_iter1 = state_iter2++;
state_iter1 != states.end();
++state_iter1, ++state_iter2)
{
const size_t text_idx1 = state_iter1->start_idx;
const size_t text_idx2 = (state_iter2 == states.end()) ? text.size() : state_iter2->start_idx;
size_t text_count = text_idx2 - text_idx1;
// there might be some offsets before the first state
while((offset_iter1 != offsets.end())
&& (offset_iter1->start_idx <= text_idx1))
{
new_offsets.push_back(*(offset_iter1++));
}
// find the last offset covered by the current state
auto offset_iter2 = offset_iter1;
for(; (offset_iter2 != offsets.end()) && (offset_iter2->start_idx <= text_idx2); ++offset_iter2) { }
// There are `offset_count` <span>'s, the target is to reduce this number
size_t offset_count = offset_iter2 - offset_iter1;
assert(text_count >= offset_count);
// Optimize letter space
// how much letter_space is changed
// will be later used for optimizing word space
double letter_space_diff = 0;
width_map.clear();
// In some PDF files all letter spaces are implemented as position shifts between each letter
// try to simplify it with a proper letter space
if(offset_count > 0)
{
// mark the current letter_space
if(text_count > offset_count)
width_map.insert(std::make_pair(0, text_count - offset_count));
for(auto off_iter = offset_iter1; off_iter != offset_iter2; ++off_iter)
{
const double target = off_iter->width;
auto iter = width_map.lower_bound(target-EPS);
if((iter != width_map.end()) && (std::abs(iter->first - target) <= EPS))
{
++ iter->second;
}
else
{
width_map.insert(iter, std::make_pair(target, 1));
}
}
// TODO snapping the widths may result a better result
// e.g. for (-0.7 0.6 -0.2 0.3 10 10), 0 is better than 10
double most_used_width = 0;
size_t max_count = 0;
for(auto iter = width_map.begin(); iter != width_map.end(); ++iter)
{
if(iter->second > max_count)
{
most_used_width = iter->first;
max_count = iter->second;
}
}
// negative letter space may cause problems
if((max_count <= text_count / 2) || (!is_positive(state_iter1->letter_space + most_used_width)))
{
// the old value is the best
// just copy old offsets
new_offsets.insert(new_offsets.end(), offset_iter1, offset_iter2);
}
else
{
// now we would like to adjust letter space to most_used width
// install new letter space
const double old_ls = state_iter1->letter_space;
state_iter1->ids[State::LETTER_SPACE_ID] = ls_manager.install(old_ls + most_used_width, &(state_iter1->letter_space));
letter_space_diff = old_ls - state_iter1->letter_space;
// update offsets
auto off_iter = offset_iter1;
// re-count number of offsets
offset_count = 0;
for(size_t cur_text_idx = text_idx1; cur_text_idx < text_idx2; ++cur_text_idx)
{
double cur_width = 0;
if((off_iter != offset_iter2) && (off_iter->start_idx == cur_text_idx + 1))
{
cur_width = off_iter->width + letter_space_diff;
++off_iter;
}
else
{
cur_width = letter_space_diff ;
}
if(!equal(cur_width, 0))
{
new_offsets.emplace_back(cur_text_idx+1, cur_width);
++ offset_count;
}
}
}
}
// Optimize word space
// In some PDF files all spaces are converted into positioning shift
// We may try to change (some of) them to ' ' by adjusting word_space
// for now, we consider only the no-space scenario
// which also includes the case when param.space_as_offset is set
// get the text segment covered by current state (*state_iter1)
const auto text_iter1 = text.begin() + text_idx1;
const auto text_iter2 = text.begin() + text_idx2;
if(find(text_iter1, text_iter2, ' ') == text_iter2)
{
// if there is not any space, we may change the value of word_space arbitrarily
// note that we may only change word space, no offset will be affected
// The actual effect will emerge during flushing, where it could be detected that an offset can be optimized as a single space character
if(offset_count > 0)
{
double threshold = (state_iter1->em_size()) * (param.space_threshold);
// set word_space for the most frequently used offset
double most_used_width = 0;
size_t max_count = 0;
// if offset_count > 0, we must have updated width_map in the previous step
// find the most frequent width, with new letter space applied
for(auto iter = width_map.begin(); iter != width_map.end(); ++iter)
{
double fixed_width = iter->first + letter_space_diff; // this is the actual offset in HTML
// we don't want to add spaces for tiny gaps, or even negative shifts
if((fixed_width >= threshold - EPS) && (iter->second > max_count))
{
max_count = iter->second;
most_used_width = fixed_width;
}
}
state_iter1->word_space = 0; // clear word_space for single_space_offset
double new_word_space = most_used_width - state_iter1->single_space_offset();
state_iter1->ids[State::WORD_SPACE_ID] = ws_manager.install(new_word_space, &(state_iter1->word_space)); // install new word_space
state_iter1->hash_umask &= (~word_space_umask); // mark that the word_space is not free
}
else // there is no offset at all
{
state_iter1->hash_umask |= word_space_umask; // we just free word_space
}
}
offset_iter1 = offset_iter2;
}
// apply optimization
std::swap(offsets, new_offsets);
lines.push_back(this);
}
// for optimize-text == 3
void HTMLTextLine::optimize_aggressive(std::vector<HTMLTextLine*> & lines)
{
/*
HTMLLineState original_line_state = line_state;
// break the line if there are a large (positive or negative) shift
// letter space / word space are not taken into consideration (yet)
while(true)
{
}
// aggressive optimization
if(target > state_iter1->em_size() * (param.space_threshold) - EPS)
out << ' ';
dx = 0;
lines.push_back(this);
*/
}
// this state will be converted to a child node of the node of prev_state
// dump the difference between previous state
// also clone corresponding states
void HTMLTextLine::State::begin (ostream & out, const State * prev_state)
{
if(prev_state)
{
long long cur_mask = 0xff;
bool first = true;
for(int i = 0; i < HASH_ID_COUNT; ++i, cur_mask<<=8)
{
if(hash_umask & cur_mask) // we don't care about this ID
{
if (prev_state->hash_umask & cur_mask) // if prev_state do not care about it either
continue;
// otherwise
// we have to inherit it
ids[i] = prev_state->ids[i];
hash_umask &= (~cur_mask);
//copy the corresponding value
//TODO: this is so ugly
switch(i)
{
case FONT_SIZE_ID:
font_size = prev_state->font_size;
break;
case LETTER_SPACE_ID:
letter_space = prev_state->letter_space;
break;
case WORD_SPACE_ID:
word_space = prev_state->word_space;
break;
default:
cerr << "unexpected state mask" << endl;
break;
}
}
// now we care about the ID
// if the value from prev_state is the same, we don't need to dump it
if((!(prev_state->hash_umask & cur_mask)) && (prev_state->ids[i] == ids[i]))
continue;
// so we have to dump it
if(first)
{
out << "<span class=\"";
first = false;
}
else
{
out << ' ';
}
// out should have hex set
out << css_class_names[i];
if (ids[i] == -1)
out << CSS::INVALID_ID;
else
out << ids[i];
}
// vertical align
if(!equal(vertical_align, 0))
{
// so we have to dump it
if(first)
{
out << "<span class=\"";
first = false;
}
else
{
out << ' ';
}
// out should have hex set
out << CSS::VERTICAL_ALIGN_CN;
auto id = ids[VERTICAL_ALIGN_ID];
if (id == -1)
out << CSS::INVALID_ID;
else
out << id;
}
if(first) // we actually just inherit the whole prev_state
{
need_close = false;
}
else
{
out << "\">";
need_close = true;
}
}
else
{
// prev_state == nullptr
// which means this is the first state of the line
// there should be a open pending <div> left there
// it is not necessary to output vertical align
long long cur_mask = 0xff;
for(int i = 0; i < HASH_ID_COUNT; ++i, cur_mask<<=8)
{
if(hash_umask & cur_mask) // we don't care about this ID
continue;
// now we care about the ID
out << ' ';
// out should have hex set
out << css_class_names[i];
if (ids[i] == -1)
out << CSS::INVALID_ID;
else
out << ids[i];
}
out << "\">";
need_close = false;
}
}
void HTMLTextLine::State::end(ostream & out) const
{
if(need_close)
out << "</span>";
}
void HTMLTextLine::State::hash(void)
{
hash_value = 0;
for(int i = 0; i < ID_COUNT; ++i)
{
hash_value = (hash_value << 8) | (ids[i] & 0xff);
}
}
int HTMLTextLine::State::diff(const State & s) const
{
/*
* A quick check based on hash_value
* it could be wrong when there are more then 256 classes,
* in which case the output may not be optimal, but still 'correct' in terms of HTML
*/
long long common_mask = ~(hash_umask | s.hash_umask);
if((hash_value & common_mask) == (s.hash_value & common_mask)) return 0;
long long cur_mask = 0xff;
int d = 0;
for(int i = 0; i < ID_COUNT; ++i)
{
if((common_mask & cur_mask) && (ids[i] != s.ids[i]))
++ d;
cur_mask <<= 8;
}
return d;
}
long long HTMLTextLine::State::umask_by_id(int id)
{
return (((long long)0xff) << (8*id));
}
// the order should be the same as in the enum
const char * const HTMLTextLine::State::css_class_names [] = {
CSS::FONT_FAMILY_CN,
CSS::FONT_SIZE_CN,
CSS::FILL_COLOR_CN,
CSS::STROKE_COLOR_CN,
CSS::LETTER_SPACE_CN,
CSS::WORD_SPACE_CN,
CSS::VERTICAL_ALIGN_CN,
};
} //namespace pdf2htmlEX