From d09a47d84fe83b4f1d55e3a7e405d0e4970c995f Mon Sep 17 00:00:00 2001 From: Duan Yao Date: Tue, 17 Jun 2014 11:59:45 +0800 Subject: [PATCH] Code cleaning. --- .../CairoBackgroundRenderer.cc | 16 +++++++--------- src/BackgroundRenderer/CairoBackgroundRenderer.h | 2 +- .../SplashBackgroundRenderer.cc | 11 ++++++++++- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/BackgroundRenderer/CairoBackgroundRenderer.cc b/src/BackgroundRenderer/CairoBackgroundRenderer.cc index 4279d3a..63a6a81 100644 --- a/src/BackgroundRenderer/CairoBackgroundRenderer.cc +++ b/src/BackgroundRenderer/CairoBackgroundRenderer.cc @@ -38,8 +38,7 @@ CairoBackgroundRenderer::~CairoBackgroundRenderer() { if (itr->second == 0) { - string path; - html_renderer->tmp_files.add(this->build_bitmap_path(itr->first, path)); + html_renderer->tmp_files.add(this->build_bitmap_path(itr->first)); } } } @@ -101,7 +100,7 @@ bool CairoBackgroundRenderer::render_page(PDFDoc * doc, int pageno) cairo_t * cr = cairo_create(surface); setCairo(cr); - bitmaps_in_current_page.resize(0); + bitmaps_in_current_page.clear(); bool process_annotation = param.process_annotation; doc->displayPage(this, pageno, param.h_dpi, param.v_dpi, @@ -189,10 +188,10 @@ void CairoBackgroundRenderer::embed_image(int pageno) f_page << "\"/>"; } -string & CairoBackgroundRenderer::build_bitmap_path(int id, string & path) +string CairoBackgroundRenderer::build_bitmap_path(int id) { - // "po" for "PDF Object" - return path = html_renderer->str_fmt("%s/po-%d.jpg", param.dest_dir.c_str(), id); + // "o" for "PDF Object" + return string(html_renderer->str_fmt("%s/o%d.jpg", param.dest_dir.c_str(), id)); } // Override CairoOutputDev::setMimeData() and dump bitmaps in SVG to external files. void CairoBackgroundRenderer::setMimeData(Stream *str, Object *ref, cairo_surface_t *image) @@ -212,7 +211,7 @@ void CairoBackgroundRenderer::setMimeData(Stream *str, Object *ref, cairo_surfac return; int imgId = ref->getRef().num; - auto uri = strdup((char*) html_renderer->str_fmt("po-%d.jpg", imgId)); + auto uri = strdup((char*) html_renderer->str_fmt("o%d.jpg", imgId)); auto st = cairo_surface_set_mime_data(image, CAIRO_MIME_TYPE_URI, (unsigned char*) uri, strlen(uri), free, uri); if (st) @@ -231,8 +230,7 @@ void CairoBackgroundRenderer::setMimeData(Stream *str, Object *ref, cairo_surfac int len; if (getStreamData(str->getNextStream(), &strBuffer, &len)) { - string path; - ofstream imgfile(build_bitmap_path(imgId, path), ofstream::binary); + ofstream imgfile(build_bitmap_path(imgId), ofstream::binary); imgfile.write(strBuffer, len); free(strBuffer); } diff --git a/src/BackgroundRenderer/CairoBackgroundRenderer.h b/src/BackgroundRenderer/CairoBackgroundRenderer.h index 65e4515..8abe5f3 100644 --- a/src/BackgroundRenderer/CairoBackgroundRenderer.h +++ b/src/BackgroundRenderer/CairoBackgroundRenderer.h @@ -55,7 +55,7 @@ protected: private: // convert bitmap stream id to bitmap file name. No pageno prefix, // because a bitmap may be shared by multiple pages. - std::string & build_bitmap_path(int id, std::string & path); + std::string build_bitmap_path(int id); // map // note: if a svg bg fallbacks to bitmap bg, its bitmaps are not taken into account. std::unordered_map bitmaps_ref_count; diff --git a/src/BackgroundRenderer/SplashBackgroundRenderer.cc b/src/BackgroundRenderer/SplashBackgroundRenderer.cc index 970d59e..c596508 100644 --- a/src/BackgroundRenderer/SplashBackgroundRenderer.cc +++ b/src/BackgroundRenderer/SplashBackgroundRenderer.cc @@ -177,14 +177,23 @@ void SplashBackgroundRenderer::dump_image(const char * filename, int x1, int y1, // use unique_ptr to auto delete the object upon exception unique_ptr writer; - if(format == "png") + if(false) { } +#ifdef ENABLE_LIBPNG + else if(format == "png") { writer = unique_ptr(new PNGWriter); } +#endif +#ifdef ENABLE_LIBJPEG else if(format == "jpg") { writer = unique_ptr(new JpegWriter); } +#endif + else + { + throw string("Image format not supported: ") + format; + } if(!writer->init(f, width, height, param.h_dpi, param.v_dpi)) throw "Cannot initialize PNGWriter";