From a1bc9e9d6663b4d0828dd0057968738268bd4181 Mon Sep 17 00:00:00 2001 From: Tomaž Vajngerl Date: Tue, 17 Dec 2024 11:58:59 +0900 Subject: pdfium: remove duplication of converting a unicode string MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Tested-by: Jenkins CollaboraOffice Reviewed-on: https://gerrit.libreoffice.org/c/core/+/178774 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl --- vcl/source/pdf/PDFiumLibrary.cxx | 181 ++++++++++++--------------------------- 1 file changed, 53 insertions(+), 128 deletions(-) (limited to 'vcl/source') diff --git a/vcl/source/pdf/PDFiumLibrary.cxx b/vcl/source/pdf/PDFiumLibrary.cxx index c665ce006d9e..a1552e9ca1bf 100644 --- a/vcl/source/pdf/PDFiumLibrary.cxx +++ b/vcl/source/pdf/PDFiumLibrary.cxx @@ -31,6 +31,7 @@ #include #include #include +#include using namespace com::sun::star; @@ -220,6 +221,35 @@ int CompatibleWriterCallback(FPDF_FILEWRITE* pFileWrite, const void* pData, unsi pImpl->m_rStream.WriteBytes(pData, nSize); return 1; } + +OUString getUnicodeString(std::function aPDFiumFunctionCall) +{ + OUString sReturnText; + + int nBytes = aPDFiumFunctionCall(nullptr, 0); + if (nBytes == 0) + return sReturnText; + assert(nBytes % 2 == 0); + nBytes /= 2; + + std::vector pText(nBytes, 0); + + int nActualBytes = aPDFiumFunctionCall(reinterpret_cast(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 @@ -910,32 +940,10 @@ PDFiumPageObjectImpl::PDFiumPageObjectImpl(FPDF_PAGEOBJECT pPageObject) OUString PDFiumPageObjectImpl::getText(std::unique_ptr const& rTextPage) { - OUString sReturnText; - auto pTextPage = static_cast(rTextPage.get()); - int nBytes = FPDFTextObj_GetText(mpPageObject, pTextPage->getPointer(), nullptr, 0); - assert(nBytes % 2 == 0); - nBytes /= 2; - - std::unique_ptr pText(new sal_Unicode[nBytes]); - - int nActualBytes = FPDFTextObj_GetText(mpPageObject, pTextPage->getPointer(), - reinterpret_cast(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() @@ -1390,98 +1398,36 @@ Color PDFiumAnnotationImpl::getFontColor(PDFiumDocument* pDoc) OUString PDFiumAnnotationImpl::getFormFieldAlternateName(PDFiumDocument* pDoc) { - auto pDocImpl = static_cast(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 pText(new sal_Unicode[nSize]); - unsigned long nStringSize = FPDFAnnot_GetFormFieldAlternateName( - pDocImpl->getFormHandlePointer(), mpAnnotation, - reinterpret_cast(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(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(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 pText(new sal_Unicode[nSize]); - unsigned long nStringSize - = FPDFAnnot_GetFormFieldValue(pDocImpl->getFormHandlePointer(), mpAnnotation, - reinterpret_cast(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(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(pDoc); + auto* pDocImpl = static_cast(pDoc); return FPDFAnnot_GetOptionCount(pDocImpl->getFormHandlePointer(), mpAnnotation); } OUString PDFiumAnnotationImpl::getFormAdditionalActionJavaScript(PDFiumDocument* pDoc, PDFAnnotAActionType eEvent) { - auto pDocImpl = static_cast(pDoc); - OUString aString; - unsigned long nSize = FPDFAnnot_GetFormAdditionalActionJavaScript( - pDocImpl->getFormHandlePointer(), mpAnnotation, static_cast(eEvent), nullptr, 0); - assert(nSize % 2 == 0); - nSize /= 2; - if (nSize > 1) - { - std::unique_ptr pText(new sal_Unicode[nSize]); - unsigned long nStringSize = FPDFAnnot_GetFormAdditionalActionJavaScript( - pDocImpl->getFormHandlePointer(), mpAnnotation, static_cast(eEvent), - reinterpret_cast(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(pDoc); + return getUnicodeString([this, pDocImpl, eEvent](FPDF_WCHAR* buffer, unsigned long length) { + return FPDFAnnot_GetFormAdditionalActionJavaScript(pDocImpl->getFormHandlePointer(), + mpAnnotation, static_cast(eEvent), + buffer, length); + }); } namespace @@ -1539,30 +1485,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 pText(new sal_Unicode[nSize]); - unsigned long nStringSize = FPDFAnnot_GetStringValue( - mpAnnotation, rKey.getStr(), reinterpret_cast(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> PDFiumAnnotationImpl::getInkStrokes() -- cgit