56 lines
1.7 KiB
C++
56 lines
1.7 KiB
C++
#include "pdfengine/document/document_normalizer.hpp"
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
|
|
namespace pdfengine::document {
|
|
|
|
RawOCRPage DocumentNormalizer::normalize(const RawOCRPage& rawPage) const {
|
|
RawOCRPage page = rawPage;
|
|
|
|
if (page.pageWidth <= 0.0) page.pageWidth = page.imageWidth > 0.0 ? page.imageWidth : 612.0;
|
|
if (page.pageHeight <= 0.0) page.pageHeight = page.imageHeight > 0.0 ? page.imageHeight : 792.0;
|
|
|
|
double scaleX = 1.0;
|
|
double scaleY = 1.0;
|
|
|
|
if (page.imageWidth > 0.0 && std::abs(page.imageWidth - page.pageWidth) > 0.01) {
|
|
scaleX = page.pageWidth / page.imageWidth;
|
|
}
|
|
if (page.imageHeight > 0.0 && std::abs(page.imageHeight - page.pageHeight) > 0.01) {
|
|
scaleY = page.pageHeight / page.imageHeight;
|
|
}
|
|
|
|
for (auto& line : page.lines) {
|
|
line.x = std::max(0.0, line.x * scaleX);
|
|
line.y = std::max(0.0, line.y * scaleY);
|
|
line.width = std::max(1.0, line.width * scaleX);
|
|
line.height = std::max(1.0, line.height * scaleY);
|
|
|
|
if (line.fontSize <= 0.0) {
|
|
line.fontSize = line.height * 0.72;
|
|
}
|
|
|
|
if (line.baselineY <= 0.0) {
|
|
line.baselineY = line.y + line.fontSize;
|
|
} else {
|
|
line.baselineY *= scaleY;
|
|
}
|
|
|
|
for (auto& word : line.words) {
|
|
word.x = std::max(0.0, word.x * scaleX);
|
|
word.y = std::max(0.0, word.y * scaleY);
|
|
word.width = std::max(0.5, word.width * scaleX);
|
|
word.height = std::max(0.5, word.height * scaleY);
|
|
|
|
for (auto& pt : word.polygon) {
|
|
pt.first *= scaleX;
|
|
pt.second *= scaleY;
|
|
}
|
|
}
|
|
}
|
|
|
|
return page;
|
|
}
|
|
|
|
} // namespace pdfengine::document
|