From b432aa5fed5d06085609cfabafa6c12df7589f5c Mon Sep 17 00:00:00 2001 From: yu Date: Mon, 25 Jun 2018 17:36:48 +0800 Subject: [PATCH] better to resize content in proportion to source when Paper size is not same as source --- qt/src/hocr/HOCRPdfExporter.cc | 33 +++++++++++++++++++++++++-------- qt/src/hocr/HOCRPdfExporter.hh | 2 +- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/qt/src/hocr/HOCRPdfExporter.cc b/qt/src/hocr/HOCRPdfExporter.cc index e4c6b3ee..8af91f14 100644 --- a/qt/src/hocr/HOCRPdfExporter.cc +++ b/qt/src/hocr/HOCRPdfExporter.cc @@ -75,10 +75,16 @@ class HOCRPdfExporter::QPainterPDFPainter : public HOCRPdfExporter::PDFPainter { m_curFont.setItalic(italic); m_painter->setFont(m_curFont); } - void setFontSize(double pointSize) override { - if(pointSize != m_curFont.pointSize()) { - m_curFont.setPointSize(pointSize); - m_painter->setFont(m_curFont); + void setFontSize(double pointSize, bool defaultFont = false) override { + if(defaultFont) { + if(pointSize != m_defaultFont.pointSize()) { + m_defaultFont.setPointSize(pointSize); + } + } else { + if(pointSize != m_curFont.pointSize()) { + m_curFont.setPointSize(pointSize); + m_painter->setFont(m_curFont); + } } } void drawText(double x, double y, const QString& text) override { @@ -232,8 +238,12 @@ class HOCRPdfExporter::PoDoFoPDFPainter : public HOCRPdfExporter::PDFPainter { m_painter.SetFont(getFont(family, bold, italic)); m_painter.GetFont()->SetFontSize(curSize); } - void setFontSize(double pointSize) override { - m_painter.GetFont()->SetFontSize(pointSize); + void setFontSize(double pointSize, bool defaultFont = false) override { + if(defaultFont) { + m_defaultFontSize = pointSize; + } else { + m_painter.GetFont()->SetFontSize(pointSize); + } } void drawText(double x, double y, const QString& text) override { PoDoFo::PdfString pdfString(reinterpret_cast(text.toUtf8().data())); @@ -512,7 +522,6 @@ bool HOCRPdfExporter::run(QString& filebasename) { } break; } - MAIN->getDisplayer()->scene()->removeItem(m_preview); delete m_preview; m_preview = nullptr; @@ -560,7 +569,15 @@ bool HOCRPdfExporter::run(QString& filebasename) { // [pt] = 72 * [in] // [in] = 1 / dpi * [px] // => [pt] = 72 / dpi * [px] - double px2pt = (72.0 / sourceDpi); + double sourceSizeToOutSize; + if( paperSize == "source") { + sourceSizeToOutSize = 1; + } else { + sourceSizeToOutSize = pageWidth / (72.0 / sourceDpi * bbox.width()); + } + painter->setFontSize(ui.spinBoxFontSize->value() * sourceSizeToOutSize, true); + double px2pt = (72.0 / sourceDpi) * sourceSizeToOutSize; + pdfSettings.detectedFontScaling *= sourceSizeToOutSize; double imgScale = double(outputDpi) / sourceDpi; if(paperSize == "source") { pageWidth = bbox.width() * px2pt; diff --git a/qt/src/hocr/HOCRPdfExporter.hh b/qt/src/hocr/HOCRPdfExporter.hh index 4515883b..cbd734c4 100644 --- a/qt/src/hocr/HOCRPdfExporter.hh +++ b/qt/src/hocr/HOCRPdfExporter.hh @@ -61,7 +61,7 @@ private: public: virtual ~PDFPainter() {} virtual void setFontFamily(const QString& family, bool bold, bool italic) = 0; - virtual void setFontSize(double pointSize) = 0; + virtual void setFontSize(double pointSize, bool defaultFont = false) = 0; virtual void drawText(double x, double y, const QString& text) = 0; virtual void drawImage(const QRect& bbox, const QImage& image, const PDFSettings& settings) = 0; virtual double getAverageCharWidth() const = 0;