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.8k
Expand file tree
/
Copy pathStateManager.h
More file actions
430 lines (379 loc) · 13.8 KB
/
StateManager.h
File metadata and controls
430 lines (379 loc) · 13.8 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
/*
* StateManager.h
*
* manage reusable CSS classes
*
* Copyright (C) 2013 Lu Wang <coolwanglu@gmail.com>
*/
#ifndef STATEMANAGER_H__
#define STATEMANAGER_H__
#include <iostream>
#include <map>
#include <unordered_map>
#include "Color.h"
#include "util/math.h"
#include "util/css_const.h"
namespace pdf2htmlEX {
template<class ValueType, class Imp> class StateManager {};
template<class Imp>
class StateManager<double, Imp>
{
public:
StateManager()
: eps(0)
, imp(static_cast<Imp*>(this))
{ }
// values no farther than eps are treated as equal
void set_eps (double eps) {
this->eps = eps;
}
double get_eps (void) const {
return eps;
}
// install new_value into the map
// return the corresponding id
long long install(double new_value, double * actual_value_ptr = nullptr) {
auto iter = value_map.lower_bound(new_value - eps);
if((iter != value_map.end()) && (std::abs(iter->first - new_value) <= eps))
{
if(actual_value_ptr != nullptr)
*actual_value_ptr = iter->first;
return iter->second;
}
long long id = value_map.size();
double v = value_map.insert(iter, std::make_pair(new_value, id))->first;
if(actual_value_ptr != nullptr)
*actual_value_ptr = v;
return id;
}
void dump_css(std::ostream & out) {
for(auto & p : value_map)
{
out << "." << imp->get_css_class_name() << p.second << "{";
imp->dump_value(out, p.first);
out << "}" << std::endl;
}
}
void dump_print_css(std::ostream & out, double scale) {
for(auto & p : value_map)
{
out << "." << imp->get_css_class_name() << p.second << "{";
imp->dump_print_value(out, p.first, scale);
out << "}" << std::endl;
}
}
protected:
double eps;
Imp * imp;
std::map<double, long long> value_map;
};
// Be careful about the mixed usage of Matrix and const double *
// the input is usually double *, which might be changed, so we have to copy the content out
// in the map we use Matrix instead of double * such that the array may be automatically release when destructing
template <class Imp>
class StateManager<Matrix, Imp>
{
public:
StateManager()
: imp(static_cast<Imp*>(this))
{ }
// return id
long long install(const double * new_value) {
Matrix m;
memcpy(m.m, new_value, 4 * sizeof(double));
auto iter = value_map.lower_bound(m);
if((iter != value_map.end()) && (tm_equal(m.m, iter->first.m, 4)))
{
return iter->second;
}
long long id = value_map.size();
value_map.insert(iter, std::make_pair(m, id));
return id;
}
void dump_css(std::ostream & out) {
for(auto & p : value_map)
{
out << "." << imp->get_css_class_name() << p.second << "{";
imp->dump_value(out, p.first);
out << "}" << std::endl;
}
}
void dump_print_css(std::ostream & out, double scale) {}
protected:
Imp * imp;
struct Matrix_less
{
bool operator () (const Matrix & m1, const Matrix & m2) const
{
// Note that we only care about the first 4 elements
for(int i = 0; i < 4; ++i)
{
if(m1.m[i] < m2.m[i])
return true;
if(m1.m[i] > m2.m[i])
return false;
}
return false;
}
};
std::map<Matrix, long long, Matrix_less> value_map;
};
template <class Imp>
class StateManager<Color, Imp>
{
public:
StateManager()
: imp(static_cast<Imp*>(this))
{ }
long long install(const Color & new_value) {
auto iter = value_map.find(new_value);
if(iter != value_map.end())
{
return iter->second;
}
long long id = value_map.size();
value_map.insert(std::make_pair(new_value, id));
return id;
}
void dump_css(std::ostream & out) {
out << "." << imp->get_css_class_name() << CSS::INVALID_ID << "{";
imp->dump_transparent(out);
out << "}" << std::endl;
for(auto & p : value_map)
{
out << "." << imp->get_css_class_name() << p.second << "{";
imp->dump_value(out, p.first);
out << "}" << std::endl;
}
}
void dump_print_css(std::ostream & out, double scale) {}
protected:
Imp * imp;
struct Color_hash
{
size_t operator () (const Color & color) const
{
if(color.transparent)
{
return (~((size_t)0));
}
else
{
return ( ((((size_t)colToByte(color.rgb.r)) & 0xff) << 16)
| ((((size_t)colToByte(color.rgb.g)) & 0xff) << 8)
| (((size_t)colToByte(color.rgb.b)) & 0xff)
);
}
}
};
std::unordered_map<Color, long long, Color_hash> value_map;
};
/////////////////////////////////////
// Specific state managers
class FontSizeManager : public StateManager<double, FontSizeManager>
{
public:
static const char * get_css_class_name (void) { return CSS::FONT_SIZE_CN; }
double default_value(void) { return 0; }
void dump_value(std::ostream & out, double value) { out << "font-size:" << round(value) << "px;"; }
void dump_print_value(std::ostream & out, double value, double scale) { out << "font-size:" << round(value*scale) << "pt;"; }
};
class LetterSpaceManager : public StateManager<double, LetterSpaceManager>
{
public:
static const char * get_css_class_name (void) { return CSS::LETTER_SPACE_CN; }
double default_value(void) { return 0; }
void dump_value(std::ostream & out, double value) { out << "letter-spacing:" << round(value) << "px;"; }
void dump_print_value(std::ostream & out, double value, double scale) { out << "letter-spacing:" << round(value*scale) << "pt;"; }
};
class WordSpaceManager : public StateManager<double, WordSpaceManager>
{
public:
static const char * get_css_class_name (void) { return CSS::WORD_SPACE_CN;}
double default_value(void) { return 0; }
void dump_value(std::ostream & out, double value) { out << "word-spacing:" << round(value) << "px;"; }
void dump_print_value(std::ostream & out, double value, double scale) { out << "word-spacing:" << round(value*scale) << "pt;"; }
};
class VerticalAlignManager : public StateManager<double, VerticalAlignManager>
{
public:
static const char * get_css_class_name (void) { return CSS::VERTICAL_ALIGN_CN; }
double default_value(void) { return 0; }
void dump_value(std::ostream & out, double value) { out << "vertical-align:" << round(value) << "px;"; }
void dump_print_value(std::ostream & out, double value, double scale) { out << "vertical-align:" << round(value*scale) << "pt;"; }
};
class WhitespaceManager : public StateManager<double, WhitespaceManager>
{
public:
static const char * get_css_class_name (void) { return CSS::WHITESPACE_CN; }
double default_value(void) { return 0; }
void dump_value(std::ostream & out, double value) {
out << ((value > 0) ? "width:"
: "margin-left:")
<< round(value) << "px;";
}
void dump_print_value(std::ostream & out, double value, double scale)
{
value *= scale;
out << ((value > 0) ? "width:"
: "margin-left:")
<< round(value) << "pt;";
}
};
class WidthManager : public StateManager<double, WidthManager>
{
public:
static const char * get_css_class_name (void) { return CSS::WIDTH_CN; }
double default_value(void) { return 0; }
void dump_value(std::ostream & out, double value) { out << "width:" << round(value) << "px;"; }
void dump_print_value(std::ostream & out, double value, double scale) { out << "width:" << round(value*scale) << "pt;"; }
};
class BottomManager : public StateManager<double, BottomManager>
{
public:
static const char * get_css_class_name (void) { return CSS::BOTTOM_CN; }
double default_value(void) { return 0; }
void dump_value(std::ostream & out, double value) { out << "bottom:" << round(value) << "px;"; }
void dump_print_value(std::ostream & out, double value, double scale) { out << "bottom:" << round(value*scale) << "pt;"; }
};
class HeightManager : public StateManager<double, HeightManager>
{
public:
static const char * get_css_class_name (void) { return CSS::HEIGHT_CN; }
double default_value(void) { return 0; }
void dump_value(std::ostream & out, double value) { out << "height:" << round(value) << "px;"; }
void dump_print_value(std::ostream & out, double value, double scale) { out << "height:" << round(value*scale) << "pt;"; }
};
class LeftManager : public StateManager<double, LeftManager>
{
public:
static const char * get_css_class_name (void) { return CSS::LEFT_CN; }
double default_value(void) { return 0; }
void dump_value(std::ostream & out, double value) { out << "left:" << round(value) << "px;"; }
void dump_print_value(std::ostream & out, double value, double scale) { out << "left:" << round(value*scale) << "pt;"; }
};
class TransformMatrixManager : public StateManager<Matrix, TransformMatrixManager>
{
public:
static const char * get_css_class_name (void) { return CSS::TRANSFORM_MATRIX_CN; }
const double * default_value(void) { return ID_MATRIX; }
void dump_value(std::ostream & out, const Matrix & matrix) {
// always ignore tm[4] and tm[5] because
// we have already shifted the origin
// TODO: recognize common matrices
const auto & m = matrix.m;
auto prefixes = {"", "-ms-", "-webkit-"};
if(tm_equal(m, ID_MATRIX, 4))
{
for(auto & s : prefixes)
out << s << "transform:none;";
}
else
{
for(auto & s : prefixes)
{
// PDF use a different coordinate system from Web
out << s << "transform:matrix("
<< round(m[0]) << ','
<< round(-m[1]) << ','
<< round(-m[2]) << ','
<< round(m[3]) << ',';
out << "0,0);";
}
}
}
};
class FillColorManager : public StateManager<Color, FillColorManager>
{
public:
static const char * get_css_class_name (void) { return CSS::FILL_COLOR_CN; }
/* override base's method, as we need some workaround in CSS */
void dump_css(std::ostream & out) {
for(auto & p : value_map)
{
out << "." << get_css_class_name() << p.second
<< "{color:" << p.first << ";}" << std::endl;
}
}
};
class StrokeColorManager : public StateManager<Color, StrokeColorManager>
{
public:
static const char * get_css_class_name (void) { return CSS::STROKE_COLOR_CN; }
/* override base's method, as we need some workaround in CSS */
void dump_css(std::ostream & out) {
// normal CSS
out << "." << get_css_class_name() << CSS::INVALID_ID << "{text-shadow:none;}" << std::endl;
for(auto & p : value_map)
{
// TODO: take the stroke width from the graphics state,
// currently using 0.015em as a good default
out << "." << get_css_class_name() << p.second << "{text-shadow:"
<< "-0.015em 0 " << p.first << ","
<< "0 0.015em " << p.first << ","
<< "0.015em 0 " << p.first << ","
<< "0 -0.015em " << p.first << ";"
<< "}" << std::endl;
}
// webkit
out << CSS::WEBKIT_ONLY << "{" << std::endl;
out << "." << get_css_class_name() << CSS::INVALID_ID << "{-webkit-text-stroke:0px transparent;}" << std::endl;
for(auto & p : value_map)
{
out << "." << get_css_class_name() << p.second
<< "{-webkit-text-stroke:0.015em " << p.first << ";text-shadow:none;}" << std::endl;
}
out << "}" << std::endl;
}
};
/////////////////////////////////////
/*
* Manage the background image sizes
*
* We don't merge similar values, since they are bound with PAGE_CONTENT_BOX_number
*/
class BGImageSizeManager
{
public:
void install(int page_no, double width, double height){
value_map.insert(std::make_pair(page_no, std::make_pair(width, height)));
}
void dump_css(std::ostream & out) {
for(auto & p : value_map)
{
const auto & s = p.second;
out << "." << CSS::PAGE_CONTENT_BOX_CN << p.first << "{";
out << "background-size:" << round(s.first) << "px " << round(s.second) << "px;";
out << "}" << std::endl;
}
}
void dump_print_css(std::ostream & out, double scale) {
for(auto & p : value_map)
{
const auto & s = p.second;
out << "." << CSS::PAGE_CONTENT_BOX_CN << p.first << "{";
out << "background-size:" << round(s.first * scale) << "pt " << round(s.second * scale) << "pt;";
out << "}" << std::endl;
}
}
private:
std::unordered_map<int, std::pair<double,double>> value_map;
};
struct AllStateManager
{
TransformMatrixManager transform_matrix;
VerticalAlignManager vertical_align;
StrokeColorManager stroke_color;
LetterSpaceManager letter_space;
WhitespaceManager whitespace;
WordSpaceManager word_space;
FillColorManager fill_color;
FontSizeManager font_size;
BottomManager bottom;
HeightManager height;
WidthManager width;
LeftManager left;
BGImageSizeManager bgimage_size;
};
} // namespace pdf2htmlEX
#endif //STATEMANAGER_H__