pdf2htmlEX/pdf2htmlEX/src/BackgroundRenderer/SplashBackgroundRenderer.cc

177 lines
5.7 KiB
C++
Raw Normal View History

2012-08-06 06:21:37 +00:00
/*
2012-10-02 12:56:40 +00:00
* SplashBackgroundRenderer.cc
2012-08-06 06:21:37 +00:00
*
2013-09-18 10:01:56 +00:00
* Copyright (C) 2012,2013 Lu Wang <coolwanglu@gmail.com>
2012-08-06 06:21:37 +00:00
*/
2013-09-16 08:30:00 +00:00
#include <fstream>
2013-09-18 12:24:48 +00:00
#include <poppler-config.h>
#include <splash/SplashErrorCodes.h>
2012-10-02 13:11:00 +00:00
2013-09-16 08:30:00 +00:00
#include "Base64Stream.h"
2012-10-02 12:56:40 +00:00
#include "SplashBackgroundRenderer.h"
2012-08-20 21:48:21 +00:00
2012-10-02 12:56:40 +00:00
namespace pdf2htmlEX {
2012-08-06 06:21:37 +00:00
2012-10-02 13:11:00 +00:00
using std::string;
2013-09-16 08:30:00 +00:00
using std::ifstream;
2012-10-02 13:11:00 +00:00
2012-10-02 12:56:40 +00:00
const SplashColor SplashBackgroundRenderer::white = {255,255,255};
2012-08-20 21:48:21 +00:00
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;
}
}
2013-09-16 08:30:00 +00:00
/*
* 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.
*/
2013-09-18 12:24:48 +00:00
void SplashBackgroundRenderer::startPage(int pageNum, GfxState *state, XRef *xrefA)
{
2013-09-16 08:30:00 +00:00
SplashOutputDev::startPage(pageNum, state, xrefA);
}
2012-10-02 12:56:40 +00:00
void SplashBackgroundRenderer::drawChar(GfxState *state, double x, double y,
2012-08-06 06:21:37 +00:00
double dx, double dy,
double originX, double originY,
CharCode code, int nBytes, const Unicode *u, int uLen)
2012-08-06 06:21:37 +00:00
{
New master (#2) * Show header in font map files * fix a usage of unique_ptr with array * Added '--quiet' argument to hide progress messages (resolves #503) * Revert cout messages to cerr (see #622) * bump version * fix build; fix some coverity warnings * Many bug fixes and improvements, including: - Incorporated latest Cairo files from cairo-0.15.2 - Moved build to out-of-source - Added clean script - Rewritten correct_text_visibility option to improve accuracy - Transparent characters drawn on background layer - Improved bad unicode detection * Many bug fixes and improvements, including: - Incorporated latest Cairo files from cairo-0.15.2 - Moved build to out-of-source - Added clean script - Rewritten correct_text_visibility option to improve accuracy - Transparent characters drawn on background layer - Improved bad unicode detection * Rationlise DPI to single number. Implement actual_dpi - clamp maximum background image size in cases of huge PDF pages * DPI fixes - increase DPI when partially covered text to covered-text-dpi Add font-style italic for oblique fonts Reduce char bbox for occlusion tests * Don't shrink bbox - not required if zoom=25 used * Ignore occlusion from stroke/fill with opacity < 0.5 Better compute char bbox for occlusion Use 10% inset for char bbox for occlusion Back out adding font-weight: bold to potentially bold fonts Fix bug to ensure CID ascent/descent matches subfont values * Removed zero char logging * Remove forced italic - missing italic is due to fontforge bug which needs fixing * Typos fixed, readme updated * Typos * Increase maximum background image width Fix private use range to avoid stupid mobile safari switching to emoji font * included -pthread switch to link included 3rdparty poppler files. * Updated files from poppler 0.59.0 and adjusted includes. * Support updated "Object" class from poppler 0.59.0
2018-01-10 19:31:38 +00:00
if (param.proof || html_renderer->is_char_covered(drawn_char_count)) {
2013-03-08 17:45:13 +00:00
SplashOutputDev::drawChar(state,x,y,dx,dy,originX,originY,code,nBytes,u,uLen);
}
New master (#2) * Show header in font map files * fix a usage of unique_ptr with array * Added '--quiet' argument to hide progress messages (resolves #503) * Revert cout messages to cerr (see #622) * bump version * fix build; fix some coverity warnings * Many bug fixes and improvements, including: - Incorporated latest Cairo files from cairo-0.15.2 - Moved build to out-of-source - Added clean script - Rewritten correct_text_visibility option to improve accuracy - Transparent characters drawn on background layer - Improved bad unicode detection * Many bug fixes and improvements, including: - Incorporated latest Cairo files from cairo-0.15.2 - Moved build to out-of-source - Added clean script - Rewritten correct_text_visibility option to improve accuracy - Transparent characters drawn on background layer - Improved bad unicode detection * Rationlise DPI to single number. Implement actual_dpi - clamp maximum background image size in cases of huge PDF pages * DPI fixes - increase DPI when partially covered text to covered-text-dpi Add font-style italic for oblique fonts Reduce char bbox for occlusion tests * Don't shrink bbox - not required if zoom=25 used * Ignore occlusion from stroke/fill with opacity < 0.5 Better compute char bbox for occlusion Use 10% inset for char bbox for occlusion Back out adding font-weight: bold to potentially bold fonts Fix bug to ensure CID ascent/descent matches subfont values * Removed zero char logging * Remove forced italic - missing italic is due to fontforge bug which needs fixing * Typos fixed, readme updated * Typos * Increase maximum background image width Fix private use range to avoid stupid mobile safari switching to emoji font * included -pthread switch to link included 3rdparty poppler files. * Updated files from poppler 0.59.0 and adjusted includes. * Support updated "Object" class from poppler 0.59.0
2018-01-10 19:31:38 +00:00
drawn_char_count++;
2012-08-06 06:21:37 +00:00
}
void SplashBackgroundRenderer::beginTextObject(GfxState *state)
{
if (param.proof == 2)
proof_begin_text_object(state, this);
SplashOutputDev::beginTextObject(state);
}
2019-06-29 11:42:55 +00:00
void SplashBackgroundRenderer::beginString(GfxState *state, const GooString * str)
2014-06-26 13:28:32 +00:00
{
if (param.proof == 2)
2014-06-27 03:35:00 +00:00
proof_begin_string(state, this);
2014-06-26 13:28:32 +00:00
SplashOutputDev::beginString(state, str);
}
void SplashBackgroundRenderer::endTextObject(GfxState *state)
{
if (param.proof == 2)
2014-06-27 03:35:00 +00:00
proof_end_text_object(state, this);
2014-06-26 13:28:32 +00:00
SplashOutputDev::endTextObject(state);
}
void SplashBackgroundRenderer::updateRender(GfxState *state)
{
if (param.proof == 2)
proof_update_render(state, this);
SplashOutputDev::updateRender(state);
}
2013-09-18 10:01:56 +00:00
void SplashBackgroundRenderer::init(PDFDoc * doc)
{
startDoc(doc);
}
static bool annot_cb(Annot *, void * pflag) {
return (*((bool*)pflag)) ? true : false;
2012-10-02 13:11:00 +00:00
};
bool SplashBackgroundRenderer::render_page(PDFDoc * doc, int pageno)
2012-10-02 13:11:00 +00:00
{
drawn_char_count = 0;
2014-06-07 04:43:53 +00:00
bool process_annotation = param.process_annotation;
New master (#2) * Show header in font map files * fix a usage of unique_ptr with array * Added '--quiet' argument to hide progress messages (resolves #503) * Revert cout messages to cerr (see #622) * bump version * fix build; fix some coverity warnings * Many bug fixes and improvements, including: - Incorporated latest Cairo files from cairo-0.15.2 - Moved build to out-of-source - Added clean script - Rewritten correct_text_visibility option to improve accuracy - Transparent characters drawn on background layer - Improved bad unicode detection * Many bug fixes and improvements, including: - Incorporated latest Cairo files from cairo-0.15.2 - Moved build to out-of-source - Added clean script - Rewritten correct_text_visibility option to improve accuracy - Transparent characters drawn on background layer - Improved bad unicode detection * Rationlise DPI to single number. Implement actual_dpi - clamp maximum background image size in cases of huge PDF pages * DPI fixes - increase DPI when partially covered text to covered-text-dpi Add font-style italic for oblique fonts Reduce char bbox for occlusion tests * Don't shrink bbox - not required if zoom=25 used * Ignore occlusion from stroke/fill with opacity < 0.5 Better compute char bbox for occlusion Use 10% inset for char bbox for occlusion Back out adding font-weight: bold to potentially bold fonts Fix bug to ensure CID ascent/descent matches subfont values * Removed zero char logging * Remove forced italic - missing italic is due to fontforge bug which needs fixing * Typos fixed, readme updated * Typos * Increase maximum background image width Fix private use range to avoid stupid mobile safari switching to emoji font * included -pthread switch to link included 3rdparty poppler files. * Updated files from poppler 0.59.0 and adjusted includes. * Support updated "Object" class from poppler 0.59.0
2018-01-10 19:31:38 +00:00
doc->displayPage(this, pageno, param.actual_dpi, param.actual_dpi,
2012-12-07 12:31:09 +00:00
0,
2013-04-06 09:01:05 +00:00
(!(param.use_cropbox)),
2012-12-07 12:31:09 +00:00
false, false,
2014-06-07 04:43:53 +00:00
nullptr, nullptr, &annot_cb, &process_annotation);
return true;
2013-09-16 08:30:00 +00:00
}
void SplashBackgroundRenderer::embed_image(int pageno)
{
auto * bitmap = getBitmap();
2013-09-16 08:30:00 +00:00
// dump the background image only when it is not empty
if(bitmap->getWidth() >= 0 && bitmap->getHeight() >= 0)
2013-09-16 08:30:00 +00:00
{
{
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());
2013-09-16 08:30:00 +00:00
if(param.embed_image)
html_renderer->tmp_files.add((const char *)fn);
SplashImageFileFormat splashImageFileFormat;
if(format == "png")
splashImageFileFormat = splashFormatPng;
else if(format == "jpg")
splashImageFileFormat = splashFormatJpeg;
else
throw string("Image format not supported: ") + format;
2013-09-16 08:30:00 +00:00
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);
2013-09-16 08:30:00 +00:00
}
New master (#2) * Show header in font map files * fix a usage of unique_ptr with array * Added '--quiet' argument to hide progress messages (resolves #503) * Revert cout messages to cerr (see #622) * bump version * fix build; fix some coverity warnings * Many bug fixes and improvements, including: - Incorporated latest Cairo files from cairo-0.15.2 - Moved build to out-of-source - Added clean script - Rewritten correct_text_visibility option to improve accuracy - Transparent characters drawn on background layer - Improved bad unicode detection * Many bug fixes and improvements, including: - Incorporated latest Cairo files from cairo-0.15.2 - Moved build to out-of-source - Added clean script - Rewritten correct_text_visibility option to improve accuracy - Transparent characters drawn on background layer - Improved bad unicode detection * Rationlise DPI to single number. Implement actual_dpi - clamp maximum background image size in cases of huge PDF pages * DPI fixes - increase DPI when partially covered text to covered-text-dpi Add font-style italic for oblique fonts Reduce char bbox for occlusion tests * Don't shrink bbox - not required if zoom=25 used * Ignore occlusion from stroke/fill with opacity < 0.5 Better compute char bbox for occlusion Use 10% inset for char bbox for occlusion Back out adding font-weight: bold to potentially bold fonts Fix bug to ensure CID ascent/descent matches subfont values * Removed zero char logging * Remove forced italic - missing italic is due to fontforge bug which needs fixing * Typos fixed, readme updated * Typos * Increase maximum background image width Fix private use range to avoid stupid mobile safari switching to emoji font * included -pthread switch to link included 3rdparty poppler files. * Updated files from poppler 0.59.0 and adjusted includes. * Support updated "Object" class from poppler 0.59.0
2018-01-10 19:31:38 +00:00
double h_scale = html_renderer->text_zoom_factor() * DEFAULT_DPI / param.actual_dpi;
double v_scale = html_renderer->text_zoom_factor() * DEFAULT_DPI / param.actual_dpi;
2013-09-16 08:30:00 +00:00
auto & f_page = *(html_renderer->f_curpage);
auto & all_manager = html_renderer->all_manager;
2013-09-16 08:30:00 +00:00
f_page << "<img class=\"" << CSS::BACKGROUND_IMAGE_CN
<< " " << CSS::LEFT_CN << all_manager.left.install(0.0L)
<< " " << CSS::BOTTOM_CN << all_manager.bottom.install(0.0L)
<< " " << CSS::WIDTH_CN << all_manager.width.install(h_scale * bitmap->getWidth())
<< " " << CSS::HEIGHT_CN << all_manager.height.install(v_scale * bitmap->getHeight())
2013-09-16 08:30:00 +00:00
<< "\" alt=\"\" src=\"";
2013-09-16 08:30:00 +00:00
if(param.embed_image)
{
auto path = html_renderer->str_fmt("%s/bg%x.%s", param.tmp_dir.c_str(), pageno, format.c_str());
2013-09-16 08:30:00 +00:00
ifstream fin((char*)path, ifstream::binary);
if(!fin)
throw string("Cannot read background image ") + (char*)path;
2013-09-18 12:24:48 +00:00
auto iter = FORMAT_MIME_TYPE_MAP.find(format);
2013-09-18 12:24:48 +00:00
if(iter == FORMAT_MIME_TYPE_MAP.end())
throw string("Image format not supported: ") + format;
2013-09-18 12:24:48 +00:00
string mime_type = iter->second;
f_page << "data:" << mime_type << ";base64," << Base64Stream(fin);
2013-09-16 08:30:00 +00:00
}
else
{
f_page << (char*)html_renderer->str_fmt("bg%x.%s", pageno, format.c_str());
2013-09-16 08:30:00 +00:00
}
f_page << "\"/>";
}
2012-10-02 13:11:00 +00:00
}
2012-10-02 12:56:40 +00:00
} // namespace pdf2htmlEX