From aa15515247086fbf09284e2ceb2e2eef41deebb4 Mon Sep 17 00:00:00 2001 From: Duan Yao Date: Fri, 27 Jun 2014 11:35:00 +0800 Subject: [PATCH] Improve coding style of proof mode. --- src/BackgroundRenderer/BackgroundRenderer.cc | 29 +++++-------------- src/BackgroundRenderer/BackgroundRenderer.h | 12 ++++---- .../CairoBackgroundRenderer.cc | 4 +-- .../SplashBackgroundRenderer.cc | 4 +-- 4 files changed, 18 insertions(+), 31 deletions(-) diff --git a/src/BackgroundRenderer/BackgroundRenderer.cc b/src/BackgroundRenderer/BackgroundRenderer.cc index ba1e832..a5dda2f 100644 --- a/src/BackgroundRenderer/BackgroundRenderer.cc +++ b/src/BackgroundRenderer/BackgroundRenderer.cc @@ -49,23 +49,12 @@ BackgroundRenderer * BackgroundRenderer::getFallbackBackgroundRenderer(HTMLRende return nullptr; } -BackgroundRenderer::~BackgroundRenderer() +void BackgroundRenderer::proof_begin_string(GfxState *state, OutputDev * dev) { - //delete proof_state - if (proof_state) - delete proof_state; -} - -void BackgroundRenderer::proof_begin_string(GfxState *state) -{ - auto dev = (OutputDev*) dynamic_cast(this); - if (!dev) - dev = (OutputDev*) dynamic_cast(this); - if (!proof_state) { PDFRectangle rect(0, 0, state->getPageWidth(), state->getPageHeight()); - proof_state = new GfxState(state->getHDPI(), state->getVDPI(), &rect, state->getRotate(), dev->upsideDown()); + proof_state.reset(new GfxState(state->getHDPI(), state->getVDPI(), &rect, state->getRotate(), dev->upsideDown())); proof_state->setFillColorSpace(new GfxDeviceRGBColorSpace()); proof_state->setStrokeColorSpace(new GfxDeviceRGBColorSpace()); } @@ -78,24 +67,20 @@ void BackgroundRenderer::proof_begin_string(GfxState *state) sc = sc.distance(blue) > 0.4 ? blue : yellow; if (state->getFillColorSpace()->getMode() != csDeviceRGB) - dev->updateFillColorSpace(proof_state); + dev->updateFillColorSpace(proof_state.get()); if (state->getStrokeColorSpace()->getMode() != csDeviceRGB) - dev->updateStrokeColorSpace(proof_state); + dev->updateStrokeColorSpace(proof_state.get()); GfxColor gfc, gsc; fc.get_gfx_color(gfc); sc.get_gfx_color(gsc); proof_state->setFillColor(&gfc); proof_state->setStrokeColor(&gsc); - dev->updateFillColor(proof_state); - dev->updateStrokeColor(proof_state); + dev->updateFillColor(proof_state.get()); + dev->updateStrokeColor(proof_state.get()); } -void BackgroundRenderer::proof_end_text_object(GfxState *state) +void BackgroundRenderer::proof_end_text_object(GfxState *state, OutputDev * dev) { - auto dev = (OutputDev*) dynamic_cast(this); - if (!dev) - dev = (OutputDev*) dynamic_cast(this); - dev->updateFillColorSpace(state); dev->updateStrokeColorSpace(state); dev->updateFillColor(state); diff --git a/src/BackgroundRenderer/BackgroundRenderer.h b/src/BackgroundRenderer/BackgroundRenderer.h index e1c6179..6ae9bf5 100644 --- a/src/BackgroundRenderer/BackgroundRenderer.h +++ b/src/BackgroundRenderer/BackgroundRenderer.h @@ -10,9 +10,11 @@ #define BACKGROUND_RENDERER_H__ #include +#include class PDFDoc; class GfxState; +class OutputDev; namespace pdf2htmlEX { @@ -27,8 +29,8 @@ public: // Currently only svg bg format might need a bitmap fallback. static BackgroundRenderer * getFallbackBackgroundRenderer(HTMLRenderer * html_renderer, const Param & param); - BackgroundRenderer(): proof_state(nullptr) {} - virtual ~BackgroundRenderer(); + BackgroundRenderer() {} + virtual ~BackgroundRenderer() {} virtual void init(PDFDoc * doc) = 0; //return true on success, false otherwise (e.g. need a fallback) @@ -37,10 +39,10 @@ public: // for proof output protected: - void proof_begin_string(GfxState *state); - void proof_end_text_object(GfxState *state); + void proof_begin_string(GfxState * state, OutputDev * dev); + void proof_end_text_object(GfxState * state, OutputDev * dev); private: - GfxState * proof_state; + std::unique_ptr proof_state; }; } // namespace pdf2htmlEX diff --git a/src/BackgroundRenderer/CairoBackgroundRenderer.cc b/src/BackgroundRenderer/CairoBackgroundRenderer.cc index 1e9eabd..e5371b6 100644 --- a/src/BackgroundRenderer/CairoBackgroundRenderer.cc +++ b/src/BackgroundRenderer/CairoBackgroundRenderer.cc @@ -68,14 +68,14 @@ void CairoBackgroundRenderer::drawChar(GfxState *state, double x, double y, void CairoBackgroundRenderer::beginString(GfxState *state, GooString * str) { if (param.proof == 2) - proof_begin_string(state); + proof_begin_string(state, this); CairoOutputDev::beginString(state, str); } void CairoBackgroundRenderer::endTextObject(GfxState *state) { if (param.proof == 2) - proof_end_text_object(state); + proof_end_text_object(state, this); CairoOutputDev::endTextObject(state); } diff --git a/src/BackgroundRenderer/SplashBackgroundRenderer.cc b/src/BackgroundRenderer/SplashBackgroundRenderer.cc index 7feb043..b4cfc01 100644 --- a/src/BackgroundRenderer/SplashBackgroundRenderer.cc +++ b/src/BackgroundRenderer/SplashBackgroundRenderer.cc @@ -93,14 +93,14 @@ void SplashBackgroundRenderer::drawChar(GfxState *state, double x, double y, void SplashBackgroundRenderer::beginString(GfxState *state, GooString * str) { if (param.proof == 2) - proof_begin_string(state); + proof_begin_string(state, this); SplashOutputDev::beginString(state, str); } void SplashBackgroundRenderer::endTextObject(GfxState *state) { if (param.proof == 2) - proof_end_text_object(state); + proof_end_text_object(state, this); SplashOutputDev::endTextObject(state); }