summaryrefslogtreecommitdiff
path: root/sdext/source
diff options
context:
space:
mode:
authorKevin Suo <suokunlong@126.com>2021-07-11 14:49:54 +0800
committerNoel Grandin <noel.grandin@collabora.co.uk>2021-07-12 20:21:49 +0200
commitcffd97193f7468f770368559d5a5c58bd0bb2327 (patch)
tree07bec4b3e27745682bdc33cf52edbb99eed426e2 /sdext/source
parenta023a967978a334fd5016ab325864796def295cc (diff)
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@collabora.co.uk>
Diffstat (limited to 'sdext/source')
-rw-r--r--sdext/source/pdfimport/inc/wrapper.hxx18
-rw-r--r--sdext/source/pdfimport/wrapper/wrapper.cxx147
2 files changed, 46 insertions, 119 deletions
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()