forked from coolwanglu/pdf2htmlEX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreprocessor.cc
107 lines (89 loc) · 2.49 KB
/
Preprocessor.cc
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
/*
* Preprocessor.cc
*
* Check used codes for each font
*
* by WangLu
* 2012.09.07
*/
#include <cstring>
#include <iostream>
#include <algorithm>
#include <GfxState.h>
#include <GfxFont.h>
#include "Preprocessor.h"
#include "util/misc.h"
#include "util/const.h"
namespace pdf2htmlEX {
using std::cerr;
using std::endl;
using std::flush;
using std::max;
Preprocessor::Preprocessor(const Param & param)
: OutputDev()
, param(param)
, max_width(0)
, max_height(0)
, cur_font_id(0)
, cur_code_map(nullptr)
{ }
Preprocessor::~Preprocessor(void)
{
for(auto & p : code_maps)
delete [] p.second;
}
void Preprocessor::process(PDFDoc * doc)
{
int page_count = (param.last_page - param.first_page + 1);
for(int i = param.first_page; i <= param.last_page ; ++i)
{
cerr << "Preprocessing: " << (i-param.first_page) << "/" << page_count << '\r' << flush;
doc->displayPage(this, i, DEFAULT_DPI, DEFAULT_DPI,
0,
(!(param.use_cropbox)),
true, // crop
false, // printing
nullptr, nullptr, nullptr, nullptr);
}
if(page_count >= 0)
cerr << "Preprocessing: " << page_count << "/" << page_count;
cerr << endl;
}
void Preprocessor::drawChar(GfxState *state, double x, double y,
double dx, double dy,
double originX, double originY,
CharCode code, int nBytes, Unicode *u, int uLen)
{
GfxFont * font = state->getFont();
if(!font) return;
long long fn_id = hash_ref(font->getID());
if(fn_id != cur_font_id)
{
cur_font_id = fn_id;
auto p = code_maps.insert(std::make_pair(cur_font_id, (char*)nullptr));
if(p.second)
{
// this is a new font
int len = font->isCIDFont() ? 0x10000 : 0x100;
p.first->second = new char [len];
memset(p.first->second, 0, len * sizeof(char));
}
cur_code_map = p.first->second;
}
cur_code_map[code] = 1;
}
void Preprocessor::startPage(int pageNum, GfxState *state)
{
startPage(pageNum, state, nullptr);
}
void Preprocessor::startPage(int pageNum, GfxState *state, XRef * xref)
{
max_width = max<double>(max_width, state->getPageWidth());
max_height = max<double>(max_height, state->getPageHeight());
}
const char * Preprocessor::get_code_map (long long font_id) const
{
auto iter = code_maps.find(font_id);
return (iter == code_maps.end()) ? nullptr : (iter->second);
}
} // namespace pdf2htmlEX