summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAshod Nakashian <ashod.nakashian@collabora.co.uk>2018-04-13 20:26:11 -0400
committerAshod Nakashian <ashod.nakashian@collabora.co.uk>2018-06-04 12:36:28 -0400
commit0e66e793546f3c93d6e794c93ddf9e96cb897111 (patch)
treebefee6ce376559aeb421b3fe4c1067f5b2a9aa7f
parentd27e516c8451037fac9ff5039c12038171a0834e (diff)
svx: improved text importing from PDF
Change-Id: I9a2fc2c8511655c1aa362c1a03a5e82ae3ba697e (cherry picked from commit d057cf3d9184cc5d96af9c957411911f5e788f4d)
-rw-r--r--external/pdfium/edit.patch.123
-rw-r--r--svx/source/svdraw/svdpdf.cxx84
2 files changed, 61 insertions, 46 deletions
diff --git a/external/pdfium/edit.patch.1 b/external/pdfium/edit.patch.1
index 86cda347f262..982560a3fab7 100644
--- a/external/pdfium/edit.patch.1
+++ b/external/pdfium/edit.patch.1
@@ -66,7 +66,7 @@ index 0d7ba56..37bdf99 100644
FPDFImageObj_GetImageDataDecoded(FPDF_PAGEOBJECT image_object,
void* buffer,
diff --git a/fpdfsdk/fpdfeditpage.cpp b/fpdfsdk/fpdfeditpage.cpp
-index ca2cf3f..8073a18 100644
+index ca2cf3f..e7d633f 100644
--- a/fpdfsdk/fpdfeditpage.cpp
+++ b/fpdfsdk/fpdfeditpage.cpp
@@ -11,12 +11,14 @@
@@ -84,7 +84,7 @@ index ca2cf3f..8073a18 100644
#include "core/fpdfapi/page/cpdf_shadingobject.h"
#include "core/fpdfapi/parser/cpdf_array.h"
#include "core/fpdfapi/parser/cpdf_document.h"
-@@ -363,3 +365,123 @@ FPDFPageObj_GetBounds(FPDF_PAGEOBJECT pageObject,
+@@ -363,3 +365,128 @@ FPDFPageObj_GetBounds(FPDF_PAGEOBJECT pageObject,
*top = bbox.top;
return true;
}
@@ -114,8 +114,11 @@ index ca2cf3f..8073a18 100644
+ double* a,
+ double* b,
+ double* c,
-+ double* d) {
-+ if (!text_object)
++ double* d,
++ double* e,
++ double* f)
++{
++ if (!text_object || !a || !b || !c || !d || !e || !f)
+ return;
+
+ CPDF_TextObject* pTxtObj = CPDFTextObjectFromFPDFPageObject(text_object);
@@ -124,6 +127,8 @@ index ca2cf3f..8073a18 100644
+ *b = matrix.b;
+ *c = matrix.c;
+ *d = matrix.d;
++ *e = matrix.e;
++ *f = matrix.f;
+}
+
+FPDF_EXPORT int FPDF_CALLCONV
@@ -357,7 +362,7 @@ index 77c2315..db3e734 100644
CPDF_PageObject* CPDFPageObjectFromFPDFPageObject(FPDF_PAGEOBJECT page_object);
diff --git a/public/fpdf_edit.h b/public/fpdf_edit.h
-index 54735a3..a415c98 100644
+index 54735a3..c86b638 100644
--- a/public/fpdf_edit.h
+++ b/public/fpdf_edit.h
@@ -520,6 +520,15 @@ FPDFPath_GetStrokeColor(FPDF_PAGEOBJECT path,
@@ -413,7 +418,7 @@ index 54735a3..a415c98 100644
// Create a new text object using one of the standard PDF fonts.
//
// document - handle to the document.
-@@ -761,6 +800,73 @@ FPDFPageObj_CreateTextObj(FPDF_DOCUMENT document,
+@@ -761,6 +800,77 @@ FPDFPageObj_CreateTextObj(FPDF_DOCUMENT document,
FPDF_FONT font,
float font_size);
@@ -445,12 +450,16 @@ index 54735a3..a415c98 100644
+// b - Pointer to a double value receiving coefficient "b" of the matrix.
+// c - Pointer to a double value receiving coefficient "c" of the matrix.
+// d - Pointer to a double value receiving coefficient "d" of the matrix.
++// e - Pointer to a double value receiving coefficient "e" of the matrix.
++// f - Pointer to a double value receiving coefficient "f" of the matrix.
+FPDF_EXPORT void FPDF_CALLCONV
+FPDFTextObj_GetMatrix(FPDF_PAGEOBJECT text_object,
+ double* a,
+ double* b,
+ double* c,
-+ double* d);
++ double* d,
++ double* e,
++ double* f);
+
+// Get the unicode of a special character in a text object.
+//
diff --git a/svx/source/svdraw/svdpdf.cxx b/svx/source/svdraw/svdpdf.cxx
index e2dd66f076f8..b5221a12bb84 100644
--- a/svx/source/svdraw/svdpdf.cxx
+++ b/svx/source/svdraw/svdpdf.cxx
@@ -105,6 +105,8 @@ static inline long lcl_ToLogic(double value)
const long out = OutputDevice::LogicToLogic(in, MapUnit::MapPixel, MapUnit::Map100thMM);
return out / 100;
}
+
+static inline double sqrt2(double a, double b) { return sqrt(a * a + b * b); }
}
struct FPDFBitmapDeleter
@@ -1027,53 +1029,56 @@ void ImpSdrPdfImport::ImportText(FPDF_PAGEOBJECT pPageObject, int nPageObjectInd
SAL_WARN("sd.filter", "FAILED to get TEXT bounds");
}
- SAL_WARN("sd.filter", "Got TEXT bounds left: " << left << ", right: " << right
- << ", top: " << top << ", bottom: " << bottom);
- tools::Rectangle aRect = PointsToLogic(left, right, top, bottom);
+ if (left == right || top == bottom)
+ {
+ SAL_WARN("sd.filter", "Skipping empty TEXT #" << nPageObjectIndex << " left: " << left
+ << ", right: " << right << ", top: " << top
+ << ", bottom: " << bottom);
+ return;
+ }
- double dFontScale = 1.0;
- geometry::Matrix2D aMatrix;
- FPDFTextObj_GetMatrix(pPageObject, &aMatrix.m00, &aMatrix.m01, &aMatrix.m10, &aMatrix.m11);
- if (aMatrix.m00 != aMatrix.m11 || aMatrix.m00 <= 0)
+ const int nChars = FPDFTextObj_CountChars(pPageObject);
+ std::unique_ptr<sal_Unicode[]> pText(new sal_Unicode[nChars + 1]); // + terminating null
+
+ unsigned short* pShortText = reinterpret_cast<unsigned short*>(pText.get());
+ const int nActualChars = FPDFTextObj_GetText(pPageObject, 0, nChars, pShortText);
+ if (nActualChars <= 0)
{
- SAL_WARN("sd.filter", "Bogus font scale matrix (" << aMatrix.m00 << ',' << aMatrix.m11
- << "), will use heuristic height of "
- << aRect.GetHeight() << ".");
- dFontScale = aRect.GetHeight();
+ SAL_WARN("sd.filter", "Got not TEXT");
+ return;
}
- else
- dFontScale = aMatrix.m00;
- double dFontSize = FPDFTextObj_GetFontSize(pPageObject);
- SAL_WARN("sd.filter", "Got Font Size: " << dFontSize);
- dFontSize *= dFontScale;
- SAL_WARN("sd.filter", "Got Font Size Scaled: " << dFontSize);
- dFontSize = lcl_PointToPixel(dFontSize);
- SAL_WARN("sd.filter", "Got Font Pixel Size: " << dFontSize);
- dFontSize = lcl_ToLogic(dFontSize);
- SAL_WARN("sd.filter", "Got Font Logic Size: " << dFontSize);
+ OUString sText(pText.get(), nActualChars);
+ SAL_WARN("sd.filter", "Got Text (" << nChars << "): [" << sText << "].");
+
+ double a, b, c, d, e, f;
+ FPDFTextObj_GetMatrix(pPageObject, &a, &b, &c, &d, &e, &f);
+ SAL_WARN("sd.filter", "Got font scale matrix (" << a << ", " << b << ", " << c << ", " << d
+ << ", " << e << ", " << f << ')');
+ Point aPos = PointsToLogic(e, f);
+ SAL_WARN("sd.filter", "Got TEXT origin: " << aPos);
+
+ const double dFontSize = FPDFTextObj_GetFontSize(pPageObject);
+ double dFontSizeH = fabs(sqrt2(a, c) * dFontSize);
+ double dFontSizeV = fabs(sqrt2(b, d) * dFontSize);
+ SAL_WARN("sd.filter", "Got Font Size: " << dFontSize << ", Scaled Font Size H: " << dFontSizeH
+ << ", V: " << dFontSizeV);
+ dFontSizeH = lcl_PointToPixel(dFontSizeH);
+ dFontSizeV = lcl_PointToPixel(dFontSizeV);
+ SAL_WARN("sd.filter", "Got Pixel Font Size H: " << dFontSizeH << ", V: " << dFontSizeV);
+ dFontSizeH = lcl_ToLogic(dFontSizeH);
+ dFontSizeV = lcl_ToLogic(dFontSizeV);
+ SAL_WARN("sd.filter", "Got Logic Font Size H: " << dFontSizeH << ", V: " << dFontSizeV);
unsigned int nR, nG, nB, nA;
if (FPDFTextObj_GetStrokeColor(pPageObject, &nR, &nG, &nB, &nA))
mpVD->SetTextColor(Color(nR, nG, nB));
vcl::Font aFnt = mpVD->GetFont();
- aFnt.SetFontSize(Size(dFontSize, dFontSize));
+ aFnt.SetFontSize(Size(dFontSizeH, dFontSizeV));
mpVD->SetFont(aFnt);
- const int nChars = FPDFTextObj_CountChars(pPageObject);
- std::unique_ptr<sal_Unicode[]> pText(new sal_Unicode[nChars + 1]); // + terminating null
-
- unsigned short* pShortText = reinterpret_cast<unsigned short*>(pText.get());
- const int nActualChars = FPDFTextObj_GetText(pPageObject, 0, nChars, pShortText);
- OUString sText(pText.get(), nActualChars);
-
- // for (int nChar = 0; nChar < nChars; ++nChar)
- // pText[nChar] = static_cast<sal_Unicode>(FPDFTextObj_GetUnicode(pPageObject, nChar));
- // OUString sText(pText.get(), nChars);
- SAL_WARN("sd.filter", "Got Text (" << nChars << "): [" << sText << "].");
-
- ImportText(aRect.TopLeft(), sText);
+ ImportText(aPos, sText);
}
void ImpSdrPdfImport::ImportText(const Point& rPos, const OUString& rStr)
@@ -1086,11 +1091,12 @@ void ImpSdrPdfImport::ImportText(const Point& rPos, const OUString& rStr)
sal_Int32 nTextWidth = static_cast<sal_Int32>(mpVD->GetTextWidth(rStr) * mfScaleX);
sal_Int32 nTextHeight = static_cast<sal_Int32>(mpVD->GetTextHeight() * mfScaleY);
- SAL_WARN("sd.filter", "TextWidth: " << nTextWidth << ", TextHeight: " << nTextHeight);
+ SAL_WARN("sd.filter",
+ "Unscaled text size: " << mpVD->GetTextWidth(rStr) << 'x' << mpVD->GetTextHeight()
+ << ", Scaled: " << nTextWidth << 'x' << nTextHeight);
Point aPos(FRound(rPos.X() * mfScaleX + maOfs.X()), FRound(rPos.Y() * mfScaleY + maOfs.Y()));
Size aSize(nTextWidth, nTextHeight);
- SAL_WARN("sd.filter", "Text Pos: " << aPos << ", Size: " << aSize);
if (eAlg == ALIGN_BASELINE)
aPos.AdjustY(-FRound(aFontMetric.GetAscent() * mfScaleY));
@@ -1327,7 +1333,7 @@ void ImpSdrPdfImport::ImportPath(FPDF_PAGEOBJECT pPageObject, int nPageObjectInd
}
const basegfx::B2DHomMatrix aTransform(
- basegfx::tools::createScaleTranslateB2DHomMatrix(mfScaleX, mfScaleY, maOfs.X(), maOfs.Y()));
+ basegfx::utils::createScaleTranslateB2DHomMatrix(mfScaleX, mfScaleY, maOfs.X(), maOfs.Y()));
aPoly.transform(aTransform);
float fWidth = 1;
@@ -1355,7 +1361,7 @@ void ImpSdrPdfImport::ImportPath(FPDF_PAGEOBJECT pPageObject, int nPageObjectInd
// if(!mbLastObjWasPolyWithoutLine || !CheckLastPolyLineAndFillMerge(basegfx::B2DPolyPolygon(aSource)))
aPoly.setClosed(true); // TODO: Review
- SdrPathObj* pPath = new SdrPathObj(OBJ_POLY, basegfx::B2DPolyPolygon(aPoly));
+ SdrPathObj* pPath = new SdrPathObj(*mpModel, OBJ_POLY, basegfx::B2DPolyPolygon(aPoly));
SetAttributes(pPath);
InsertObj(pPath, false);
}