Skip to content

Commit

Permalink
Merge pull request #1426 from ONLYOFFICE/fix/bug61241
Browse files Browse the repository at this point in the history
fix bug #61241
  • Loading branch information
K0R0L authored Feb 20, 2024
2 parents 7776743 + 690d17f commit cf90cd9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 87 deletions.
102 changes: 29 additions & 73 deletions RtfFile/Format/RtfChar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,37 @@ std::wstring RtfChar::renderRtfText( std::wstring& sText, void* poDocument, RtfC
}

return renderRtfText(sText, pDocument, nCodePage, bMarker);
}
std::wstring RtfChar::renderRtfText(const std::wstring& sText)
{
std::wstring sResult;
for (size_t i = 0; i < sText.length(); i++)
{
int nUnicode = (int)sText[i];

if (0 < nUnicode && nUnicode <= 0x8000)
{
sResult += L"\\u" + std::to_wstring(nUnicode) + L"*";
}
else if (0x8000 < nUnicode && nUnicode <= 0xffff) {
sResult += L"\\u" + std::to_wstring(nUnicode - 0x10000) + L"*"; //??? font alt name china ALL FONTS NEW.docx (Mekanik LET)
}
else {
sResult += L"\\u9633*";
}
}
return sResult;
}
std::wstring RtfChar::renderRtfText( std::wstring& sText, void* poDocument, int nCodePage, bool bMarker)
{
RtfDocument* pDocument = static_cast<RtfDocument*>(poDocument);
std::wstring sResult;

//от настроек документа
if (sText.empty())
{
return sResult;
}
//от настроек документа
if( -1 == nCodePage && RtfDocumentProperty::cp_none != pDocument->m_oProperty.m_eCodePage )
{
switch ( pDocument->m_oProperty.m_eCodePage )
Expand All @@ -277,81 +300,14 @@ std::wstring RtfChar::renderRtfText( std::wstring& sText, void* poDocument, int
if ((nCodePage == CP_ACP || nCodePage == 1252) && pDocument->m_nUserLCID > 0)
{
nCodePage = pDocument->m_lcidConverter.get_codepage(pDocument->m_nUserLCID);
}

std::wstring unicodeStr (sText);
std::string ansiStr ;

if (unicodeStr.empty())
{
return sResult;
}

//ansiStr = RtfUtility::convert_string(unicodeStr.begin(), unicodeStr.end(), nCodePage);

std::wstring sTextBack = RtfUtility::convert_string_icu(ansiStr.begin(), ansiStr.end(), nCodePage);

//if (!ansiStr.empty() && sTextBack.empty())
//{
// //code page not support in icu !!!
// sTextBack = RtfUtility::convert_string(ansiStr.begin(), ansiStr.end(), nCodePage); .. to UnicodeConverter
//}

//обратное преобразование чтобы понять какие символы свонвертировались неправильно
while (sTextBack.length() < sText.length())
sTextBack += L"-";
}

if (bMarker)
sResult += L"{\\uc1";

sResult += renderRtfText(sText);

for ( size_t i = 0; i < sText.length() ; i++ )
{
bool bWriteUnicode = true;

if(sTextBack[i] == sText[i] )
{
std::wstring sUniChar; sUniChar += unicodeStr[i];

//делаем Ansi строку sUniChar
// -> sTempAnsiChars
std::string sTempAnsiChars = RtfUtility::convert_string_icu(unicodeStr.begin() + i, unicodeStr.begin() + i + 1, nCodePage);

for( size_t k = 0; k < sTempAnsiChars.length(); k++ )
{
unsigned char nCharCode = sTempAnsiChars[k];
bWriteUnicode = false;

if (nCharCode == 0x5c || nCharCode == 0x7b || nCharCode == 0x7d ||
(0x00 <= nCharCode && nCharCode < 0x20) )
{
sResult += L"\\'" + XmlUtils::ToString( nCharCode, L"%02x");
}
else if ( 0x20 <= nCharCode && nCharCode < 0x80 )
{
sResult += nCharCode;
}
else
{ // 0x80 <= nUnicode <= 0xff
sResult += L"\\'" + XmlUtils::ToString(nCharCode, L"%x" );
}
}
}
if( true == bWriteUnicode )
{
int nUnicode = (int)unicodeStr[i];

if (0 < nUnicode && nUnicode <= 0x8000)
{
sResult += L"\\u" + std::to_wstring(nUnicode) + L"*";
} else if (0x8000 < nUnicode && nUnicode <= 0xffff) {
sResult += L"\\u" + std::to_wstring(nUnicode - 0x10000) + L"*"; //??? font alt name china ALL FONTS NEW.docx (Mekanik LET)
} else {
sResult += L"\\u9633*";
}
}

}
if (bMarker)
if (bMarker)
sResult += L"}";
return sResult;
}
Expand Down Expand Up @@ -539,7 +495,7 @@ std::wstring RtfCharSpecial::RenderToRtf(RenderParameter oRenderParameter)
}
sResult += L"}";
return sResult;
}
}

std::wstring RtfCharNative::RenderToRtf(RenderParameter oRenderParameter)
{
Expand Down
2 changes: 2 additions & 0 deletions RtfFile/Format/RtfChar.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ class RtfChar : public IDocumentElement
static std::wstring renderRtfText( std::wstring& sText, void* poDocument, RtfCharProperty* oCharProperty = NULL, bool bMarker = false);
static std::wstring renderRtfText( std::wstring& sText, void* poDocument, int nCodePage, bool bMarker = false );

static std::wstring renderRtfText(const std::wstring& sText);

std::wstring RenderToRtf(RenderParameter oRenderParameter);

private:
Expand Down
28 changes: 16 additions & 12 deletions RtfFile/Format/RtfProperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1937,7 +1937,7 @@ std::wstring RtfListLevelProperty::GetFormat( int nNumFormat)
}
return sResult;
}
int RtfListLevelProperty::GetFormat( std::wstring sFormat)
int RtfListLevelProperty::GetFormat(const std::wstring& sFormat)
{
if ( L"aiueo" == sFormat ) return 12;
else if ( L"aiueoFullWidth" == sFormat ) return 20;
Expand Down Expand Up @@ -2025,10 +2025,10 @@ std::wstring RtfListLevelProperty::RenderToRtf(RenderParameter oRenderParameter)
RENDER_RTF_INT( m_nNoRestart, sResult, L"levelnorestart" )
RENDER_RTF_INT( m_nPictureIndex, sResult, L"levelpicture" )
//чтобы при последующем чтении из rtf не потерялась информация о шрифте
sResult += m_oCharProp.RenderToRtf( oRenderParameter );
sResult += m_oCharProp.RenderToRtf( oRenderParameter );

sResult += L"{\\leveltext " + RtfChar::renderRtfText( m_sText, oRenderParameter.poDocument, &m_oCharProp ) + L";}";
sResult += L"{\\levelnumbers " + RtfChar::renderRtfText( m_sNumber, oRenderParameter.poDocument, &m_oCharProp ) + L";}";
sResult += L"{\\leveltext" + m_sText + L";}";
sResult += L"{\\levelnumbers" + m_sNumber + L";}";

RENDER_RTF_INT( m_nFirstIndent, sResult, L"fi" )
RENDER_RTF_INT( m_nIndent, sResult, L"li" )
Expand Down Expand Up @@ -2080,31 +2080,35 @@ std::wstring RtfListLevelProperty::GetLevelTextOOX()
return XmlUtils::EncodeXmlString( sResult );
}

void RtfListLevelProperty::SetLevelTextOOX(std::wstring sText)
void RtfListLevelProperty::SetLevelTextOOX(const std::wstring& sText)
{
m_sText = L"";
m_sNumber = L"";

int nLevelOffsets = 0;
int nText = 0;

for (size_t i = 0; i < sText.length() ; i++ )
{
if ( (sText[i] == '%') && (i + 1 < sText.length()) && (isdigit( sText[ i + 1 ] )))
{
int nLevel = RtfUtility::ToByte( sText[ i + 1 ] );

wchar_t ch1 = nLevel - 1;
m_sText += ch1;
m_sText += L"\\'" + XmlUtils::ToString(nLevel - 1, L"%02x");
m_sNumber += L"\\'" + XmlUtils::ToString(nLevelOffsets + 1, L"%02x");
i++; //т.к. следующий симовл уже учли
wchar_t ch2 = nLevelOffsets + 1;
m_sNumber += ch2;

nText++;
}
else
m_sText += sText[i];
{
std::wstring s (sText.c_str() + i, 1);
m_sText += RtfChar::renderRtfText(s);
nText++;
}
nLevelOffsets++;
}
wchar_t ch = (wchar_t)m_sText.length();
m_sText.insert(m_sText.begin() + 0, ch );
m_sText = L"\\'" + XmlUtils::ToString(nText, L"%02x") + m_sText;
}
std::wstring RtfListLevelProperty::RenderToOOX(RenderParameter oRenderParameter)
{
Expand Down
4 changes: 2 additions & 2 deletions RtfFile/Format/RtfProperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -526,10 +526,10 @@ class RtfListLevelProperty : IRenderableProperty
std::wstring RenderToOOX2(RenderParameter oRenderParameter, _INT32 lvl = PROP_DEF);

static std::wstring GetFormat( _INT32 nNumFormat);
static _INT32 GetFormat( std::wstring sFormat);
static _INT32 GetFormat(const std::wstring& sFormat);

std::wstring GetLevelTextOOX();
void SetLevelTextOOX(std::wstring sText);
void SetLevelTextOOX(const std::wstring& sText);
};

class RtfListProperty : public IRenderableProperty, public ItemContainer<RtfListLevelProperty>
Expand Down

0 comments on commit cf90cd9

Please sign in to comment.