diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2024-12-17 11:58:59 +0900 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2024-12-26 07:52:16 +0100 |
commit | a1bc9e9d6663b4d0828dd0057968738268bd4181 (patch) | |
tree | 9645fdbd4817b2273defa9d2e2f5b6e4891b452f /vcl/source | |
parent | 5364b7b663b2be30d802474618dea3db14a2182a (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>
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/178774
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Diffstat (limited to 'vcl/source')
-rw-r--r-- | vcl/source/pdf/PDFiumLibrary.cxx | 181 |
1 files changed, 53 insertions, 128 deletions
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 <vcl/BitmapWriteAccess.hxx> #include <vcl/bitmapex.hxx> #include <vcl/dibtools.hxx> +#include <functional> 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<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 @@ -910,32 +940,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() @@ -1390,98 +1398,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 @@ -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<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() |