forked from pdf2htmlEX/pdf2htmlEX
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSplashBackgroundRenderer.cc
More file actions
165 lines (136 loc) · 4.78 KB
/
SplashBackgroundRenderer.cc
File metadata and controls
165 lines (136 loc) · 4.78 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
/*
* SplashBackgroundRenderer.cc
*
* Copyright (C) 2012,2013 Lu Wang <coolwanglu@gmail.com>
*/
#include <fstream>
#include <poppler-config.h>
#include <splash/SplashErrorCodes.h>
#include "Base64Stream.h"
#include "SplashBackgroundRenderer.h"
namespace pdf2htmlEX {
using std::string;
using std::ifstream;
const SplashColor SplashBackgroundRenderer::white = {255,255,255};
SplashBackgroundRenderer::SplashBackgroundRenderer(const string & imgFormat, HTMLRenderer * html_renderer, const Param & param)
: SplashOutputDev(splashModeRGB8, 4, false, (SplashColorPtr)(&white), true, splashThinLineSolid) // DCRH: Make thin line mode = solid
, html_renderer(html_renderer)
, param(param)
, format(imgFormat)
{
bool supported = false;
// ENABLE_LIBPNG and ENABLE_LIBJPEG are defines coming in from poppler-config.h
#ifdef ENABLE_LIBPNG
if (format.empty())
format = "png";
supported = supported || format == "png";
#endif
#ifdef ENABLE_LIBJPEG
if (format.empty())
format = "jpg";
supported = supported || format == "jpg";
#endif
if (!supported)
{
throw string("Image format not supported by Poppler: ") + format;
}
}
/*
* SplashOutputDev::startPage would paint the whole page with the background color
* And thus have modified region set to the whole page area
* We do not want that.
*/
void SplashBackgroundRenderer::startPage(int pageNum, GfxState *state, XRef *xrefA)
{
SplashOutputDev::startPage(pageNum, state, xrefA);
}
void SplashBackgroundRenderer::drawChar(GfxState *state, double x, double y,
double dx, double dy,
double originX, double originY,
CharCode code, int nBytes, const Unicode *u, int uLen)
{
if (param.proof || html_renderer->is_char_covered(drawn_char_count)) {
SplashOutputDev::drawChar(state,x,y,dx,dy,originX,originY,code,nBytes,u,uLen);
}
drawn_char_count++;
}
void SplashBackgroundRenderer::beginTextObject(GfxState *state)
{
if (param.proof == 2)
proof_begin_text_object(state, this);
SplashOutputDev::beginTextObject(state);
}
void SplashBackgroundRenderer::beginString(GfxState *state, const GooString * str)
{
if (param.proof == 2)
proof_begin_string(state, this);
SplashOutputDev::beginString(state, str);
}
void SplashBackgroundRenderer::endTextObject(GfxState *state)
{
if (param.proof == 2)
proof_end_text_object(state, this);
SplashOutputDev::endTextObject(state);
}
void SplashBackgroundRenderer::updateRender(GfxState *state)
{
if (param.proof == 2)
proof_update_render(state, this);
SplashOutputDev::updateRender(state);
}
void SplashBackgroundRenderer::init(PDFDoc * doc)
{
startDoc(doc);
}
static bool annot_cb(Annot *, void * pflag) {
return (*((bool*)pflag)) ? true : false;
};
bool SplashBackgroundRenderer::render_page(PDFDoc * doc, int pageno)
{
drawn_char_count = 0;
bool process_annotation = param.process_annotation;
doc->displayPage(this, pageno, param.actual_dpi, param.actual_dpi,
0,
(!(param.use_cropbox)),
false, false,
nullptr, nullptr, &annot_cb, &process_annotation);
auto * bitmap = getBitmap();
auto fn = html_renderer->str_fmt("%s/bg%x.%s", (param.embed_image ? param.tmp_dir : param.dest_dir).c_str(), pageno, format.c_str());
SplashImageFileFormat splashImageFileFormat;
if(format == "png")
splashImageFileFormat = splashFormatPng;
else if(format == "jpg")
splashImageFileFormat = splashFormatJpeg;
else
throw string("Image format not supported: ") + format;
SplashError e = bitmap->writeImgFile(splashImageFileFormat, (const char *)fn, param.actual_dpi, param.actual_dpi);
if (e != splashOk)
throw string("Cannot write background image. SplashErrorCode: ") + std::to_string(e);
if(param.embed_image)
html_renderer->tmp_files.add((const char *)fn);
return true;
}
void SplashBackgroundRenderer::embed_image(int pageno)
{
auto & f_page = *(html_renderer->f_curpage);
f_page << "<img class=\"" << CSS::FULL_BACKGROUND_IMAGE_CN
<< "\" alt=\"\" src=\"";
if(param.embed_image)
{
auto path = html_renderer->str_fmt("%s/bg%x.%s", param.tmp_dir.c_str(), pageno, format.c_str());
ifstream fin((char*)path, ifstream::binary);
if(!fin)
throw string("Cannot read background image ") + (char*)path;
auto iter = FORMAT_MIME_TYPE_MAP.find(format);
if(iter == FORMAT_MIME_TYPE_MAP.end())
throw string("Image format not supported: ") + format;
string mime_type = iter->second;
f_page << "data:" << mime_type << ";base64," << Base64Stream(fin);
}
else
{
f_page << (char*)html_renderer->str_fmt("bg%x.%s", pageno, format.c_str());
}
f_page << "\"/>";
}
} // namespace pdf2htmlEX