forked from coolwanglu/pdf2htmlEX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextLineBuffer.cc
More file actions
266 lines (216 loc) · 6.75 KB
/
Copy pathTextLineBuffer.cc
File metadata and controls
266 lines (216 loc) · 6.75 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
/*
* TextLineBuffer.cc
*
* Generate and optimized HTML for one line
*
* by WangLu
* 2012.09.04
*/
#include <vector>
#include "HTMLRenderer.h"
#include "TextLineBuffer.h"
#include "util/namespace.h"
#include "util/unicode.h"
#include "util/math.h"
namespace pdf2htmlEX {
using std::min;
using std::max;
using std::vector;
using std::ostream;
using std::cerr;
using std::endl;
void HTMLRenderer::TextLineBuffer::reset(GfxState * state)
{
state->transform(state->getCurX(), state->getCurY(), &x, &y);
tm_id = renderer->cur_ttm_id;
}
void HTMLRenderer::TextLineBuffer::append_unicodes(const Unicode * u, int l)
{
text.insert(text.end(), u, u+l);
}
void HTMLRenderer::TextLineBuffer::append_offset(double width)
{
if((!offsets.empty()) && (offsets.back().start_idx == text.size()))
offsets.back().width += width;
else
offsets.push_back(Offset({text.size(), width}));
}
void HTMLRenderer::TextLineBuffer::append_state(void)
{
if(states.empty() || (states.back().start_idx != text.size()))
{
states.resize(states.size() + 1);
states.back().start_idx = text.size();
}
set_state(states.back());
}
void HTMLRenderer::TextLineBuffer::flush(void)
{
/*
* 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;
}
for(auto iter = states.begin(); iter != states.end(); ++iter)
iter->hash();
states.resize(states.size() + 1);
states.back().start_idx = text.size();
offsets.push_back(Offset({text.size(), 0}));
double max_ascent = 0;
for(auto iter = states.begin(); iter != states.end(); ++iter)
{
const auto & s = *iter;
max_ascent = max<double>(max_ascent, s.ascent * s.draw_font_size);
}
ostream & out = renderer->f_pages.fs;
out << "<div style=\""
<< "bottom:" << round(y) << "px;"
<< "\""
<< " class=\"l"
<< " t" << tm_id
<< " L" << renderer->install_left(x)
<< " h" << renderer->install_height(max_ascent)
<< "\">";
auto cur_state_iter = states.begin();
auto cur_offset_iter = offsets.begin();
//accumulated horizontal offset;
double dx = 0;
stack.clear();
stack.push_back(nullptr);
// 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;
while(cur_text_idx < text.size())
{
if(cur_text_idx >= cur_state_iter->start_idx)
{
// greedy
int best_cost = State::ID_COUNT;
// we have a nullptr at the beginning, so no need to check for rend
for(auto iter = stack.rbegin(); *iter; ++iter)
{
int cost = cur_state_iter->diff(**iter);
if(cost < best_cost)
{
while(stack.back() != *iter)
{
stack.back()->end(out);
stack.pop_back();
}
best_cost = cost;
if(best_cost == 0)
break;
}
// cannot go further
if((*iter)->start_idx <= last_text_pos_with_negative_offset)
break;
}
cur_state_iter->begin(out, stack.back());
stack.push_back(&*cur_state_iter);
++ cur_state_iter;
}
if(cur_text_idx >= cur_offset_iter->start_idx)
{
double target = cur_offset_iter->width + dx;
double w;
auto wid = renderer->install_whitespace(target, w);
if(w < 0)
last_text_pos_with_negative_offset = cur_text_idx;
auto * p = stack.back();
double threshold = p->draw_font_size * (p->ascent - p->descent) * (renderer->param->space_threshold);
out << "<span class=\"_ _" << wid << "\">" << (target > (threshold - EPS) ? " " : "") << "</span>";
dx = target - w;
++ cur_offset_iter;
}
size_t next_text_idx = min<size_t>(cur_state_iter->start_idx, cur_offset_iter->start_idx);
outputUnicodes(out, (&text.front()) + 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>";
states.clear();
offsets.clear();
text.clear();
}
void HTMLRenderer::TextLineBuffer::set_state (State & state)
{
state.ids[State::FONT_ID] = renderer->cur_font_info->id;
state.ids[State::FONT_SIZE_ID] = renderer->cur_fs_id;
state.ids[State::COLOR_ID] = renderer->cur_color_id;
state.ids[State::LETTER_SPACE_ID] = renderer->cur_ls_id;
state.ids[State::WORD_SPACE_ID] = renderer->cur_ws_id;
state.ids[State::RISE_ID] = renderer->cur_rise_id;
const FontInfo * info = renderer->cur_font_info;
state.ascent = info->ascent;
state.descent = info->descent;
state.draw_font_size = renderer->draw_font_size;
}
void HTMLRenderer::TextLineBuffer::State::begin (ostream & out, const State * prev_state)
{
bool first = true;
for(int i = 0; i < ID_COUNT; ++i)
{
if(prev_state && (prev_state->ids[i] == ids[i]))
continue;
if(first)
{
out << "<span class=\"";
first = false;
}
else
{
out << ' ';
}
// out should has set hex
out << format_str[i] << ids[i];
}
if(first)
{
need_close = false;
}
else
{
out << "\">";
need_close = true;
}
}
void HTMLRenderer::TextLineBuffer::State::end(ostream & out) const
{
if(need_close)
out << "</span>";
}
void HTMLRenderer::TextLineBuffer::State::hash(void)
{
hash_value = 0;
for(int i = 0; i < ID_COUNT; ++i)
{
hash_value = (hash_value << 8) | (ids[i] & 0xff);
}
}
int HTMLRenderer::TextLineBuffer::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
*/
if(hash_value == s.hash_value) return 0;
int d = 0;
for(int i = 0; i < ID_COUNT; ++i)
if(ids[i] != s.ids[i])
++ d;
return d;
}
const char * HTMLRenderer::TextLineBuffer::State::format_str = "fsclwr";
} //namespace pdf2htmlEX