[Libreoffice-commits] core.git: sdext/source
Kevin Suo (via logerrit)
logerrit at kemper.freedesktop.org
Mon Jul 12 18:22:27 UTC 2021
sdext/source/pdfimport/inc/wrapper.hxx | 18 +++
sdext/source/pdfimport/wrapper/wrapper.cxx | 147 +++++------------------------
2 files changed, 46 insertions(+), 119 deletions(-)
New commits:
commit cffd97193f7468f770368559d5a5c58bd0bb2327
Author: Kevin Suo <suokunlong at 126.com>
AuthorDate: Sun Jul 11 14:49:54 2021 +0800
Commit: Noel Grandin <noel.grandin at collabora.co.uk>
CommitDate: Mon Jul 12 20:21:49 2021 +0200
tdf#78427 sdext.pdfimport: refactor the conversion of font family names
Simplify the code and hopefully improves performance.
The previous code used a long for loop within which it used many duplicated parseFontRemoveSuffix. That for loop was simply intended to remove the font family name suffixes. However, the rResult.familyName is a OUString and this type already has the function of removing suffixes which is more efficient.
Also, defined a list of suffixes to be removed in the header file. New suffixes can be easily added to this list.
Change-Id: Idfa11cfe60e2e34a1f7456d29562a89eb3de7662
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/118734
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>
diff --git a/sdext/source/pdfimport/inc/wrapper.hxx b/sdext/source/pdfimport/inc/wrapper.hxx
index 68e383de6a1a..918f976ee817 100644
--- a/sdext/source/pdfimport/inc/wrapper.hxx
+++ b/sdext/source/pdfimport/inc/wrapper.hxx
@@ -52,6 +52,24 @@ namespace pdfi
const css::uno::Reference<
css::uno::XComponentContext >& xContext,
const OUString& rFilterOptions );
+
+ const OUString fontAttributesSuffixes[] = {
+ "MT",
+ "PS",
+ "PSMT",
+ "Regular",
+ "Bold",
+ "Italic",
+ "Bold",
+ "Oblique",
+ "Light",
+ "Reg",
+ "VKana",
+ "-",
+ ",",
+ ";",
+ "PS",
+ };
}
#endif // INCLUDED_SDEXT_SOURCE_PDFIMPORT_INC_WRAPPER_HXX
diff --git a/sdext/source/pdfimport/wrapper/wrapper.cxx b/sdext/source/pdfimport/wrapper/wrapper.cxx
index ae4903526c65..e22fe0aeca72 100644
--- a/sdext/source/pdfimport/wrapper/wrapper.cxx
+++ b/sdext/source/pdfimport/wrapper/wrapper.cxx
@@ -165,20 +165,14 @@ public:
};
class LineParser {
- Parser & m_parser;
- OString m_aLine;
+ Parser & m_parser;
+ OString m_aLine;
- static sal_Int32 parseFontCheckForString(const sal_Unicode* pCopy, sal_Int32 nCopyLen,
- const char* pAttrib, sal_Int32 nAttribLen,
- FontAttributes& rResult, bool bItalic, bool bBold);
- static sal_Int32 parseFontRemoveSuffix(const sal_Unicode* pCopy, sal_Int32 nCopyLen,
- const char* pAttrib, sal_Int32 nAttribLen);
- static void parseFontFamilyName( FontAttributes& aResult );
-
- void readInt32( sal_Int32& o_Value );
- void readInt64( sal_Int64& o_Value );
- void readDouble( double& o_Value );
- void readBinaryData( uno::Sequence<sal_Int8>& rBuf );
+ static void parseFontFamilyName( FontAttributes& aResult );
+ void readInt32( sal_Int32& o_Value );
+ void readInt64( sal_Int64& o_Value );
+ void readDouble( double& o_Value );
+ void readBinaryData( uno::Sequence<sal_Int8>& rBuf );
uno::Sequence<beans::PropertyValue> readImageImpl();
@@ -477,119 +471,34 @@ rendering::ARGBColor LineParser::readColor()
return aRes;
}
-sal_Int32 LineParser::parseFontCheckForString(
- const sal_Unicode* pCopy, sal_Int32 nCopyLen,
- const char* pAttrib, sal_Int32 nAttribLen,
- FontAttributes& rResult, bool bItalic, bool bBold)
-{
- if (nCopyLen < nAttribLen)
- return 0;
- for (sal_Int32 i = 0; i < nAttribLen; ++i)
- {
- sal_uInt32 nCode = pAttrib[i];
- if (rtl::toAsciiLowerCase(pCopy[i]) != nCode
- && rtl::toAsciiUpperCase(pCopy[i]) != nCode)
- return 0;
- }
- rResult.isItalic |= bItalic;
- rResult.isBold |= bBold;
- return nAttribLen;
-}
-
-sal_Int32 LineParser::parseFontRemoveSuffix(
- const sal_Unicode* pCopy, sal_Int32 nCopyLen,
- const char* pAttrib, sal_Int32 nAttribLen)
-{
- if (nCopyLen < nAttribLen)
- return 0;
- for (sal_Int32 i = 0; i < nAttribLen; ++i)
- if ( pCopy[nCopyLen - nAttribLen + i] != pAttrib[i] )
- return 0;
- return nAttribLen;
-}
+/* Parse and convert the font family name (passed from xpdfimport) to correct font names
+e.g. TimesNewRomanPSMT -> TimesNewRoman
+ TimesNewRomanPS-BoldMT -> TimesNewRoman
+ TimesNewRomanPS-BoldItalicMT -> TimesNewRoman
+During the conversion, also apply the font features (bold italic etc) to the result.
+TODO: Further convert the font names to real font names in the system rather than the PS names.
+e.g., TimesNewRoman -> Times New Roman
+*/
void LineParser::parseFontFamilyName( FontAttributes& rResult )
{
- OUStringBuffer aNewFamilyName( rResult.familyName.getLength() );
-
- const sal_Unicode* pCopy = rResult.familyName.getStr();
- sal_Int32 nLen = rResult.familyName.getLength();
-
- // TODO: Looks like this block needs to be refactored
- while( nLen )
+ SAL_INFO("sdext.pdfimport", "Processing " << rResult.familyName << " ---");
+ rResult.familyName = rResult.familyName.trim();
+ for (const OUString& fontAttributesSuffix: fontAttributesSuffixes)
{
- if (parseFontRemoveSuffix(pCopy, nLen, RTL_CONSTASCII_STRINGPARAM("PSMT")))
- {
- nLen -= RTL_CONSTASCII_LENGTH("PSMT");
- }
- else if (parseFontRemoveSuffix(pCopy, nLen, RTL_CONSTASCII_STRINGPARAM("MT")))
- {
- nLen -= RTL_CONSTASCII_LENGTH("MT");
- }
-
- if (parseFontCheckForString(pCopy, nLen, RTL_CONSTASCII_STRINGPARAM("Italic"), rResult, true, false))
- {
- sal_Int32 nAttribLen = RTL_CONSTASCII_LENGTH("Italic");
- nLen -= nAttribLen;
- pCopy += nAttribLen;
- }
- else if (parseFontCheckForString(pCopy, nLen, RTL_CONSTASCII_STRINGPARAM("-LightOblique"), rResult, true, false))
- {
- sal_Int32 nAttribLen = RTL_CONSTASCII_LENGTH("-LightOblique");
- nLen -= nAttribLen;
- pCopy += nAttribLen;
- }
- else if (parseFontCheckForString(pCopy, nLen, RTL_CONSTASCII_STRINGPARAM("-Light"), rResult, false, false))
- {
- sal_Int32 nAttribLen = RTL_CONSTASCII_LENGTH("-Light");
- nLen -= nAttribLen;
- pCopy += nAttribLen;
- }
- else if (parseFontCheckForString(pCopy, nLen, RTL_CONSTASCII_STRINGPARAM("-BoldOblique"), rResult, true, true))
- {
- sal_Int32 nAttribLen = RTL_CONSTASCII_LENGTH("-BoldOblique");
- nLen -= nAttribLen;
- pCopy += nAttribLen;
- }
- else if (parseFontCheckForString(pCopy, nLen, RTL_CONSTASCII_STRINGPARAM("-Bold"), rResult, false, true))
- {
- sal_Int32 nAttribLen = RTL_CONSTASCII_LENGTH("-Bold");
- nLen -= nAttribLen;
- pCopy += nAttribLen;
- }
- else if (parseFontCheckForString(pCopy, nLen, RTL_CONSTASCII_STRINGPARAM("Bold"), rResult, false, true))
- {
- sal_Int32 nAttribLen = RTL_CONSTASCII_LENGTH("Bold");
- nLen -= nAttribLen;
- pCopy += nAttribLen;
- }
- else if (parseFontCheckForString(pCopy, nLen, RTL_CONSTASCII_STRINGPARAM("-Roman"), rResult, false, false))
+ if ( rResult.familyName.endsWith(fontAttributesSuffix) )
{
- sal_Int32 nAttribLen = RTL_CONSTASCII_LENGTH("-Roman");
- nLen -= nAttribLen;
- pCopy += nAttribLen;
- }
- else if (parseFontCheckForString(pCopy, nLen, RTL_CONSTASCII_STRINGPARAM("-Oblique"), rResult, true, false))
- {
- sal_Int32 nAttribLen = RTL_CONSTASCII_LENGTH("-Oblique");
- nLen -= nAttribLen;
- pCopy += nAttribLen;
- }
- else if (parseFontCheckForString(pCopy, nLen, RTL_CONSTASCII_STRINGPARAM("-Reg"), rResult, false, false))
- {
- sal_Int32 nAttribLen = RTL_CONSTASCII_LENGTH("-Reg");
- nLen -= nAttribLen;
- pCopy += nAttribLen;
- }
- else if(nLen > 0)
- {
- if( *pCopy != '-' )
- aNewFamilyName.append( *pCopy );
- pCopy++;
- nLen--;
+ rResult.familyName = rResult.familyName.replaceAll(fontAttributesSuffix, "");
+ SAL_INFO("sdext.pdfimport", rResult.familyName);
+ if (fontAttributesSuffix == "Bold")
+ {
+ rResult.isBold = true;
+ } else if ( (fontAttributesSuffix == "Italic") or (fontAttributesSuffix == "Oblique") )
+ {
+ rResult.isItalic = true;
+ }
}
}
- rResult.familyName = aNewFamilyName.makeStringAndClear();
}
void LineParser::readFont()
More information about the Libreoffice-commits
mailing list