summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomaž Vajngerl <tomaz.vajngerl@collabora.co.uk>2024-12-17 11:58:59 +0900
committerMiklos Vajna <vmiklos@collabora.com>2024-12-17 11:47:23 +0100
commite37b2256d5eeac4be0adccc0a87908a029c2d113 (patch)
treeffea639a47831d9a40c47d8b4a5cb27610e3f89e
parent4b071f537aa75c37137dd8f6674c57cbd3a86c0b (diff)
pdfium: remove duplication of converting a unicode string
This adds a universal getUnicodeString function that takes a function call as a parameter, which is only a wrapped PDFium function call that returns a unicode string. The rest of conversion magic to OUString is then done by the function in one place. This reduces a lot of duplication as we had to copy the way how to convert the unicode string to OUString to every method. Change-Id: Id951e2dd0c8b98837ca096cd5f800b8df0449fa0 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/178628 Reviewed-by: Miklos Vajna <vmiklos@collabora.com> Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com>
-rw-r--r--vcl/source/pdf/PDFiumLibrary.cxx181
1 files changed, 53 insertions, 128 deletions
diff --git a/vcl/source/pdf/PDFiumLibrary.cxx b/vcl/source/pdf/PDFiumLibrary.cxx
index 29d95338b004..4ad9af1726d0 100644
--- a/vcl/source/pdf/PDFiumLibrary.cxx
+++ b/vcl/source/pdf/PDFiumLibrary.cxx
@@ -30,6 +30,7 @@
#include <vcl/BitmapWriteAccess.hxx>
#include <vcl/bitmapex.hxx>
#include <vcl/dibtools.hxx>
+#include <functional>
using namespace com::sun::star;
@@ -219,6 +220,35 @@ int CompatibleWriterCallback(FPDF_FILEWRITE* pFileWrite, const void* pData, unsi
pImpl->m_rStream.WriteBytes(pData, nSize);
return 1;
}
+
+OUString getUnicodeString(std::function<int(FPDF_WCHAR*, unsigned long)> aPDFiumFunctionCall)
+{
+ OUString sReturnText;
+
+ int nBytes = aPDFiumFunctionCall(nullptr, 0);
+ if (nBytes == 0)
+ return sReturnText;
+ assert(nBytes % 2 == 0);
+ nBytes /= 2;
+
+ std::vector<sal_Unicode> pText(nBytes, 0);
+
+ int nActualBytes = aPDFiumFunctionCall(reinterpret_cast<FPDF_WCHAR*>(pText.data()), nBytes * 2);
+ assert(nActualBytes % 2 == 0);
+ nActualBytes /= 2;
+ if (nActualBytes > 1)
+ {
+#ifdef OSL_BIGENDIAN
+ for (int i = 0; i != nActualBytes; ++i)
+ {
+ pText[i] = OSL_SWAPWORD(pText[i]);
+ }
+#endif
+ sReturnText = OUString(pText.data());
+ }
+
+ return sReturnText;
+}
}
namespace vcl::pdf
@@ -846,32 +876,10 @@ PDFiumPageObjectImpl::PDFiumPageObjectImpl(FPDF_PAGEOBJECT pPageObject)
OUString PDFiumPageObjectImpl::getText(std::unique_ptr<PDFiumTextPage> const& rTextPage)
{
- OUString sReturnText;
-
auto pTextPage = static_cast<PDFiumTextPageImpl*>(rTextPage.get());
- int nBytes = FPDFTextObj_GetText(mpPageObject, pTextPage->getPointer(), nullptr, 0);
- assert(nBytes % 2 == 0);
- nBytes /= 2;
-
- std::unique_ptr<sal_Unicode[]> pText(new sal_Unicode[nBytes]);
-
- int nActualBytes = FPDFTextObj_GetText(mpPageObject, pTextPage->getPointer(),
- reinterpret_cast<FPDF_WCHAR*>(pText.get()), nBytes * 2);
- assert(nActualBytes % 2 == 0);
- nActualBytes /= 2;
- if (nActualBytes > 1)
- {
-#if defined OSL_BIGENDIAN
- // The data returned by FPDFTextObj_GetText is documented to always be UTF-16LE:
- for (int i = 0; i != nActualBytes; ++i)
- {
- pText[i] = OSL_SWAPWORD(pText[i]);
- }
-#endif
- sReturnText = OUString(pText.get());
- }
-
- return sReturnText;
+ return getUnicodeString([this, pTextPage](FPDF_WCHAR* buffer, unsigned long length) {
+ return FPDFTextObj_GetText(mpPageObject, pTextPage->getPointer(), buffer, length);
+ });
}
PDFPageObjectType PDFiumPageObjectImpl::getType()
@@ -1326,98 +1334,36 @@ Color PDFiumAnnotationImpl::getFontColor(PDFiumDocument* pDoc)
OUString PDFiumAnnotationImpl::getFormFieldAlternateName(PDFiumDocument* pDoc)
{
- auto pDocImpl = static_cast<PDFiumDocumentImpl*>(pDoc);
- OUString aString;
- unsigned long nSize = FPDFAnnot_GetFormFieldAlternateName(pDocImpl->getFormHandlePointer(),
- mpAnnotation, nullptr, 0);
- assert(nSize % 2 == 0);
- nSize /= 2;
- if (nSize > 1)
- {
- std::unique_ptr<sal_Unicode[]> pText(new sal_Unicode[nSize]);
- unsigned long nStringSize = FPDFAnnot_GetFormFieldAlternateName(
- pDocImpl->getFormHandlePointer(), mpAnnotation,
- reinterpret_cast<FPDF_WCHAR*>(pText.get()), nSize * 2);
- assert(nStringSize % 2 == 0);
- nStringSize /= 2;
- if (nStringSize > 0)
- {
-#if defined OSL_BIGENDIAN
- for (unsigned long i = 0; i != nStringSize; ++i)
- {
- pText[i] = OSL_SWAPWORD(pText[i]);
- }
-#endif
- aString = OUString(pText.get());
- }
- }
- return aString;
+ auto* pDocImpl = static_cast<PDFiumDocumentImpl*>(pDoc);
+ return getUnicodeString([this, pDocImpl](FPDF_WCHAR* buffer, unsigned long length) {
+ return FPDFAnnot_GetFormFieldAlternateName(pDocImpl->getFormHandlePointer(), mpAnnotation,
+ buffer, length);
+ });
}
OUString PDFiumAnnotationImpl::getFormFieldValue(PDFiumDocument* pDoc)
{
- auto pDocImpl = static_cast<PDFiumDocumentImpl*>(pDoc);
- OUString aString;
- unsigned long nSize
- = FPDFAnnot_GetFormFieldValue(pDocImpl->getFormHandlePointer(), mpAnnotation, nullptr, 0);
- assert(nSize % 2 == 0);
- nSize /= 2;
- if (nSize > 1)
- {
- std::unique_ptr<sal_Unicode[]> pText(new sal_Unicode[nSize]);
- unsigned long nStringSize
- = FPDFAnnot_GetFormFieldValue(pDocImpl->getFormHandlePointer(), mpAnnotation,
- reinterpret_cast<FPDF_WCHAR*>(pText.get()), nSize * 2);
- assert(nStringSize % 2 == 0);
- nStringSize /= 2;
- if (nStringSize > 0)
- {
-#if defined OSL_BIGENDIAN
- for (unsigned long i = 0; i != nStringSize; ++i)
- {
- pText[i] = OSL_SWAPWORD(pText[i]);
- }
-#endif
- aString = OUString(pText.get());
- }
- }
- return aString;
+ auto* pDocImpl = static_cast<PDFiumDocumentImpl*>(pDoc);
+ return getUnicodeString([this, pDocImpl](FPDF_WCHAR* buffer, unsigned long length) {
+ return FPDFAnnot_GetFormFieldValue(pDocImpl->getFormHandlePointer(), mpAnnotation, buffer,
+ length);
+ });
}
int PDFiumAnnotationImpl::getOptionCount(PDFiumDocument* pDoc)
{
- auto pDocImpl = static_cast<PDFiumDocumentImpl*>(pDoc);
+ auto* pDocImpl = static_cast<PDFiumDocumentImpl*>(pDoc);
return FPDFAnnot_GetOptionCount(pDocImpl->getFormHandlePointer(), mpAnnotation);
}
OUString PDFiumAnnotationImpl::getFormAdditionalActionJavaScript(PDFiumDocument* pDoc,
PDFAnnotAActionType eEvent)
{
- auto pDocImpl = static_cast<PDFiumDocumentImpl*>(pDoc);
- OUString aString;
- unsigned long nSize = FPDFAnnot_GetFormAdditionalActionJavaScript(
- pDocImpl->getFormHandlePointer(), mpAnnotation, static_cast<int>(eEvent), nullptr, 0);
- assert(nSize % 2 == 0);
- nSize /= 2;
- if (nSize > 1)
- {
- std::unique_ptr<sal_Unicode[]> pText(new sal_Unicode[nSize]);
- unsigned long nStringSize = FPDFAnnot_GetFormAdditionalActionJavaScript(
- pDocImpl->getFormHandlePointer(), mpAnnotation, static_cast<int>(eEvent),
- reinterpret_cast<FPDF_WCHAR*>(pText.get()), nSize * 2);
- assert(nStringSize % 2 == 0);
- nStringSize /= 2;
- if (nStringSize > 0)
- {
-#if defined OSL_BIGENDIAN
- for (unsigned long i = 0; i != nStringSize; ++i)
- {
- pText[i] = OSL_SWAPWORD(pText[i]);
- }
-#endif
- aString = OUString(pText.get());
- }
- }
- return aString;
+ auto* pDocImpl = static_cast<PDFiumDocumentImpl*>(pDoc);
+ return getUnicodeString([this, pDocImpl, eEvent](FPDF_WCHAR* buffer, unsigned long length) {
+ return FPDFAnnot_GetFormAdditionalActionJavaScript(pDocImpl->getFormHandlePointer(),
+ mpAnnotation, static_cast<int>(eEvent),
+ buffer, length);
+ });
}
namespace
@@ -1475,30 +1421,9 @@ PDFObjectType PDFiumAnnotationImpl::getValueType(OString const& rKey)
OUString PDFiumAnnotationImpl::getString(OString const& rKey)
{
- OUString rString;
- unsigned long nSize = FPDFAnnot_GetStringValue(mpAnnotation, rKey.getStr(), nullptr, 0);
- assert(nSize % 2 == 0);
- nSize /= 2;
- if (nSize > 1)
- {
- std::unique_ptr<sal_Unicode[]> pText(new sal_Unicode[nSize]);
- unsigned long nStringSize = FPDFAnnot_GetStringValue(
- mpAnnotation, rKey.getStr(), reinterpret_cast<FPDF_WCHAR*>(pText.get()), nSize * 2);
- assert(nStringSize % 2 == 0);
- nStringSize /= 2;
- if (nStringSize > 0)
- {
-#if defined OSL_BIGENDIAN
- // The data returned by FPDFAnnot_GetStringValue is documented to always be UTF-16LE:
- for (unsigned long i = 0; i != nStringSize; ++i)
- {
- pText[i] = OSL_SWAPWORD(pText[i]);
- }
-#endif
- rString = OUString(pText.get());
- }
- }
- return rString;
+ return getUnicodeString([this, rKey](FPDF_WCHAR* buffer, unsigned long length) {
+ return FPDFAnnot_GetStringValue(mpAnnotation, rKey.getStr(), buffer, length);
+ });
}
std::vector<std::vector<basegfx::B2DPoint>> PDFiumAnnotationImpl::getInkStrokes()