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>
This commit is contained in:
Kevin Suo
2021-07-12 20:21:49 +02:00
committed by Noel Grandin
parent a023a96797
commit cffd97193f
2 changed files with 46 additions and 119 deletions
+18
View File
@@ -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
+28 -119
View File
@@ -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")))
if ( rResult.familyName.endsWith(fontAttributesSuffix) )
{
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))
{
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()