From cb72f56977faccbda9490bc16ea3cbf7a22c01b0 Mon Sep 17 00:00:00 2001 From: Henry Castro Date: Tue, 25 Jul 2023 15:36:52 -0400 Subject: [PATCH] sc: filter: rtf: add method "WriteFontTable" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Write the font table while visiting column/row and get the unique index to reference it. "The \fonttbl control word introduces the font table group. Unique \fN control words define each font available in the document, and are used to reference that font throughout the document." Signed-off-by: Henry Castro Change-Id: I20c5d1128972f5ec9b9b2e246f466bdb173ef8a4 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/154906 Tested-by: Jenkins CollaboraOffice Reviewed-by: Caolán McNamara Reviewed-on: https://gerrit.libreoffice.org/c/core/+/156415 Tested-by: Jenkins --- sc/source/filter/inc/rtfexp.hxx | 2 ++ sc/source/filter/rtf/rtfexp.cxx | 58 +++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/sc/source/filter/inc/rtfexp.hxx b/sc/source/filter/inc/rtfexp.hxx index 1c9f1bd7a4af..9d0b204540c7 100644 --- a/sc/source/filter/inc/rtfexp.hxx +++ b/sc/source/filter/inc/rtfexp.hxx @@ -28,12 +28,14 @@ class ScRTFExport : public ScExportBase { std::unique_ptr m_pCellX; // cumulative range in a table std::map m_pFontTable; + SvMemoryStream m_aFontStrm; SvMemoryStream m_aDocStrm; int AddFont( const SvxFontItem& rFontItem ); void WriteTab( SCTAB nTab ); void WriteRow( SCTAB nTab, SCROW nRow ); void WriteCell( SCTAB nTab, SCROW nRow, SCCOL nCol ); + void WriteFontTable(const SvxFontItem& rFontItem, int nIndex); public: diff --git a/sc/source/filter/rtf/rtfexp.cxx b/sc/source/filter/rtf/rtfexp.cxx index 8d38def244b3..7349aa698591 100644 --- a/sc/source/filter/rtf/rtfexp.cxx +++ b/sc/source/filter/rtf/rtfexp.cxx @@ -19,6 +19,9 @@ #include +#include +#include + #include #include #include @@ -60,6 +63,8 @@ void ScRTFExport::Write() rStrm.WriteChar( '{' ).WriteOString( OOO_STRING_SVTOOLS_RTF_RTF ); rStrm.WriteOString( OOO_STRING_SVTOOLS_RTF_ANSI ).WriteOString( SAL_NEWLINE_STRING ); + m_aFontStrm.WriteChar( '{' ).WriteOString( OOO_STRING_SVTOOLS_RTF_FONTTBL ); + // Data for ( SCTAB nTab = aRange.aStart.Tab(); nTab <= aRange.aEnd.Tab(); nTab++ ) { @@ -68,6 +73,9 @@ void ScRTFExport::Write() WriteTab( nTab ); } + m_aFontStrm.WriteChar( '}' ); + m_aFontStrm.Seek(0); + rStrm.WriteStream(m_aFontStrm); m_aDocStrm.Seek(0); rStrm.WriteStream(m_aDocStrm); rStrm.WriteChar( '}' ).WriteOString( SAL_NEWLINE_STRING ); @@ -148,6 +156,51 @@ void ScRTFExport::WriteRow( SCTAB nTab, SCROW nRow ) m_aDocStrm.WriteOString( OOO_STRING_SVTOOLS_RTF_ROW ).WriteOString( SAL_NEWLINE_STRING ); } +void ScRTFExport::WriteFontTable(const SvxFontItem& rFontItem, int nIndex) +{ + m_aFontStrm.WriteChar( '{' ); + m_aFontStrm.WriteOString( OOO_STRING_SVTOOLS_RTF_F ); + m_aFontStrm.WriteOString( OString::number(nIndex) ); + + FontFamily eFamily = rFontItem.GetFamily(); + if (eFamily == FAMILY_DONTKNOW) + m_aFontStrm.WriteOString( OOO_STRING_SVTOOLS_RTF_FNIL ); + else if (eFamily == FAMILY_DECORATIVE) + m_aFontStrm.WriteOString( OOO_STRING_SVTOOLS_RTF_FDECOR ); + else if (eFamily == FAMILY_MODERN) + m_aFontStrm.WriteOString( OOO_STRING_SVTOOLS_RTF_FMODERN ); + else if (eFamily == FAMILY_ROMAN) + m_aFontStrm.WriteOString( OOO_STRING_SVTOOLS_RTF_FROMAN ); + else if (eFamily == FAMILY_SCRIPT) + m_aFontStrm.WriteOString( OOO_STRING_SVTOOLS_RTF_FSCRIPT ); + else if (eFamily == FAMILY_SWISS) + m_aFontStrm.WriteOString( OOO_STRING_SVTOOLS_RTF_FSWISS ); + + m_aFontStrm.WriteOString( OOO_STRING_SVTOOLS_RTF_FPRQ ); + + sal_uInt16 nVal = 0; + FontPitch ePitch = rFontItem.GetPitch(); + if ( ePitch == PITCH_FIXED ) + nVal = 1; + else if ( ePitch == PITCH_VARIABLE ) + nVal = 2; + m_aFontStrm.WriteOString( OString::number(nVal) ); + + rtl_TextEncoding eDestEnc = RTL_TEXTENCODING_MS_1252; + rtl_TextEncoding eChrSet = rFontItem.GetCharSet(); + if (IsOpenSymbol(rFontItem.GetFamilyName())) + eChrSet = RTL_TEXTENCODING_UTF8; + else if( RTL_TEXTENCODING_DONTKNOW == eChrSet ) + eChrSet = osl_getThreadTextEncoding(); + + m_aFontStrm.WriteOString( OOO_STRING_SVTOOLS_RTF_FCHARSET ); + m_aFontStrm.WriteOString( OString::number(rtl_getBestWindowsCharsetFromTextEncoding( eChrSet )) ); + + m_aFontStrm.WriteChar( ' ' ); + RTFOutFuncs::Out_String( m_aFontStrm, rFontItem.GetFamilyName(), eDestEnc ); + m_aFontStrm.WriteOString( ";}" ); +} + int ScRTFExport::AddFont(const SvxFontItem& rFontItem) { auto nRet = m_pFontTable.size(); @@ -155,6 +208,7 @@ int ScRTFExport::AddFont(const SvxFontItem& rFontItem) if (itFont == m_pFontTable.end()) { m_pFontTable[rFontItem.GetFamilyName()] = nRet; + WriteFontTable(rFontItem, nRet); } else { @@ -204,11 +258,15 @@ void ScRTFExport::WriteCell( SCTAB nTab, SCROW nRow, SCCOL nCol ) bool bResetAttr(false); + const SvxFontItem& rFontItem = pAttr->GetItem( ATTR_FONT ); const SvxHorJustifyItem& rHorJustifyItem = pAttr->GetItem( ATTR_HOR_JUSTIFY ); const SvxWeightItem& rWeightItem = pAttr->GetItem( ATTR_FONT_WEIGHT ); const SvxPostureItem& rPostureItem = pAttr->GetItem( ATTR_FONT_POSTURE ); const SvxUnderlineItem& rUnderlineItem = pAttr->GetItem( ATTR_FONT_UNDERLINE ); + m_aDocStrm.WriteOString( OOO_STRING_SVTOOLS_RTF_F ) + .WriteOString( OString::number(AddFont(rFontItem)) ); + const char* pChar; switch( rHorJustifyItem.GetValue() )