forked from coolwanglu/pdf2htmlEX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTMLTextPage.cc
More file actions
149 lines (130 loc) · 3.87 KB
/
Copy pathHTMLTextPage.cc
File metadata and controls
149 lines (130 loc) · 3.87 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
/*
* HTMLTextPage.cc
*
* Generate and optimized HTML for one Page
*
* Copyright (C) 2013 Lu Wang <coolwanglu@gmail.com>
*/
#include "HTMLTextPage.h"
#include "util/css_const.h"
namespace pdf2htmlEX {
using std::ostream;
HTMLTextPage::HTMLTextPage(const Param & param, AllStateManager & all_manager)
: param(param)
, all_manager(all_manager)
, cur_line(nullptr)
, page_width(0)
, page_height(0)
{ }
HTMLTextPage::~HTMLTextPage()
{
for(auto iter = text_lines.begin(); iter != text_lines.end(); ++iter)
{
delete (*iter);
}
}
void HTMLTextPage::dump_text(ostream & out)
{
if(param.optimize_text)
{
// text lines may be split during optimization, collect them
std::vector<HTMLTextLine*> new_text_lines;
for(auto iter = text_lines.begin(); iter != text_lines.end(); ++iter)
(*iter)->optimize(new_text_lines);
std::swap(text_lines, new_text_lines);
}
for(auto iter = text_lines.begin(); iter != text_lines.end(); ++iter)
(*iter)->prepare();
if(param.optimize_text)
optimize();
HTMLClipState page_box;
page_box.xmin = page_box.ymin = 0;
page_box.xmax = page_width;
page_box.ymax = page_height;
//push a dummy entry for convenience
clips.emplace_back(page_box, text_lines.size());
Clip cur_clip(page_box, 0);
bool has_clip = false;
auto text_line_iter = text_lines.begin();
for(auto clip_iter = clips.begin(); clip_iter != clips.end(); ++clip_iter)
{
auto next_text_line_iter = text_lines.begin() + clip_iter->start_idx;
if(text_line_iter != next_text_line_iter)
{
const auto & cs = cur_clip.clip_state;
if(has_clip)
{
out << "<div class=\"" << CSS::CLIP_CN
<< " " << CSS::LEFT_CN << all_manager.left.install(cs.xmin)
<< " " << CSS::BOTTOM_CN << all_manager.bottom.install(cs.ymin)
<< " " << CSS::WIDTH_CN << all_manager.width.install(cs.xmax - cs.xmin)
<< " " << CSS::HEIGHT_CN << all_manager.height.install(cs.ymax - cs.ymin)
<< "\">";
}
while(text_line_iter != next_text_line_iter)
{
if(has_clip)
{
(*text_line_iter)->clip(cs);
}
(*text_line_iter)->dump_text(out);
++text_line_iter;
}
if(has_clip)
{
out << "</div>";
}
}
{
cur_clip = *clip_iter;
const auto & cs = cur_clip.clip_state;
has_clip = !(equal(0, cs.xmin) && equal(0, cs.ymin)
&& equal(page_width, cs.xmax) && equal(page_height, cs.ymax));
}
}
}
void HTMLTextPage::dump_css(ostream & out)
{
//TODO
}
void HTMLTextPage::clear(void)
{
text_lines.clear();
clips.clear();
cur_line = nullptr;
}
void HTMLTextPage::open_new_line(const HTMLLineState & line_state)
{
// do not reused the last text_line even if it's empty
// because the clip states may point to the next index
text_lines.emplace_back(new HTMLTextLine(line_state, param, all_manager));
cur_line = text_lines.back();
}
void HTMLTextPage::set_page_size(double width, double height)
{
page_width = width;
page_height = height;
}
void HTMLTextPage::clip(const HTMLClipState & clip_state)
{
if(!clips.empty())
{
auto & clip = clips.back();
if(clip.start_idx == text_lines.size())
{
/*
* Previous ClipBox is not used
*/
clip.clip_state = clip_state;
return;
}
}
clips.emplace_back(clip_state, text_lines.size());
}
void HTMLTextPage::optimize(void)
{
//TODO
//group lines with same x-axis
//collect common states
}
} // namespace pdf2htmlEX