From 3e51156023c409d406d4beb66bb35752f5859556 Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Wed, 8 Aug 2001 10:22:18 +0000 Subject: [PATCH] #90485# import of formulas stored in 5.0 format with multi-byte encoding fixed --- starmath/inc/utility.hxx | 10 ++- starmath/source/cfgitem.cxx | 8 +- starmath/source/document.cxx | 25 +++--- starmath/source/format.cxx | 6 +- starmath/source/node.cxx | 8 +- starmath/source/symbol.cxx | 13 ++- starmath/source/utility.cxx | 148 +++++++++++++++++++++++++++++------ 7 files changed, 169 insertions(+), 49 deletions(-) diff --git a/starmath/inc/utility.hxx b/starmath/inc/utility.hxx index e54c0645d394..5a9389e4e29f 100644 --- a/starmath/inc/utility.hxx +++ b/starmath/inc/utility.hxx @@ -2,9 +2,9 @@ * * $RCSfile: utility.hxx,v $ * - * $Revision: 1.2 $ + * $Revision: 1.3 $ * - * last change: $Author: tl $ $Date: 2000-11-02 15:07:16 $ + * last change: $Author: tl $ $Date: 2001-08-08 11:22:18 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -86,8 +86,12 @@ class String; #define C2S(cChar) String::CreateFromAscii(RTL_CONSTASCII_STRINGPARAM(cChar)) -const ByteString ExportString(const String& rString); +///////////////////////////////////////////////////////////////// +const ByteString ExportString( const String& rString ); +const String ImportString( const ByteString& rByteString ); + +///////////////////////////////////////////////////////////////// inline long SmPtsTo100th_mm(long nNumPts) // returns the length (in 100th of mm) that corresponds to the length diff --git a/starmath/source/cfgitem.cxx b/starmath/source/cfgitem.cxx index 1a1b32ae8a87..a0b1c7baca22 100644 --- a/starmath/source/cfgitem.cxx +++ b/starmath/source/cfgitem.cxx @@ -2,9 +2,9 @@ * * $RCSfile: cfgitem.cxx,v $ * - * $Revision: 1.8 $ + * $Revision: 1.9 $ * - * last change: $Author: tl $ $Date: 2001-07-17 08:28:20 $ + * last change: $Author: tl $ $Date: 2001-08-08 11:22:18 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -776,7 +776,7 @@ SmFontFormat SmMathConfig::ReadFontFormat( SmMathConfigItem &rCfg, bOK = FALSE; ++pValue; if (pValue->hasValue() && (*pValue >>= nTmp16)) - aRes.nCharSet = nTmp16; + aRes.nCharSet = nTmp16; // 6.0 file-format GetSOLoadTextEncoding not needed else bOK = FALSE; ++pValue; @@ -848,7 +848,7 @@ void SmMathConfig::SaveFontFormatList() // CharSet pVal->Name = aNodeNameDelim; pVal->Name += *pName++; - pVal->Value <<= (INT16) aFntFmt.nCharSet; + pVal->Value <<= (INT16) aFntFmt.nCharSet; // 6.0 file-format GetSOStoreTextEncoding not needed pVal++; // Family pVal->Name = aNodeNameDelim; diff --git a/starmath/source/document.cxx b/starmath/source/document.cxx index 0ff1ab4e589d..6900acdc9160 100644 --- a/starmath/source/document.cxx +++ b/starmath/source/document.cxx @@ -2,9 +2,9 @@ * * $RCSfile: document.cxx,v $ * - * $Revision: 1.39 $ + * $Revision: 1.40 $ * - * last change: $Author: tl $ $Date: 2001-08-02 15:36:37 $ + * last change: $Author: tl $ $Date: 2001-08-08 11:22:18 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -1083,6 +1083,7 @@ BOOL SmDocShell::ImportSM20File(SvStream *pStream, BOOL bInsert) long lTime; ULONG lDate; String aBuffer; + ByteString aByteStr; SmSymSet *pSymbolSet; ULONG FilePos = pStream->Tell(); @@ -1093,13 +1094,14 @@ BOOL SmDocShell::ImportSM20File(SvStream *pStream, BOOL bInsert) DBG_ASSERT((lVersion == FRMVERSION), "Illegal file version"); *pStream >> cTag; - rtl_TextEncoding eEnc = gsl_getSystemTextEncoding(); + rtl_TextEncoding eEnc = RTL_TEXTENCODING_MS_1252; while (cTag && !pStream->IsEof()) { switch (cTag) { case 'T': - pStream->ReadByteString(aBuffer, eEnc); + pStream->ReadByteString( aByteStr ); + aBuffer = ImportString( aByteStr ); if (! bInsert) { aText = aBuffer; @@ -1589,6 +1591,7 @@ BOOL SmDocShell::Try3x (SvStorage *pStor, long lTime; ULONG lDate; String aBuffer; + ByteString aByteStr; *pSvStream >> lIdent >> lVersion; @@ -1598,13 +1601,14 @@ BOOL SmDocShell::Try3x (SvStorage *pStor, (lVersion == SM50VERSION), "Illegal file version"); *pSvStream >> cTag; - rtl_TextEncoding eEnc = gsl_getSystemTextEncoding(); + rtl_TextEncoding eEnc = RTL_TEXTENCODING_MS_1252; while (cTag && !pSvStream->IsEof()) { switch (cTag) { case 'T': - pSvStream->ReadByteString(aText, eEnc); + pSvStream->ReadByteString( aByteStr ); + aText = ImportString( aByteStr ); Parse(); break; @@ -1634,7 +1638,7 @@ BOOL SmDocShell::Try3x (SvStorage *pStor, { String aTmp; USHORT n; - pSvStream->ReadByteString(aTmp, gsl_getSystemTextEncoding()); + pSvStream->ReadByteString(aTmp, eEnc); *pSvStream >> n; break; } @@ -1689,6 +1693,7 @@ BOOL SmDocShell::Try2x (SvStorage *pStor, ULONG lDate; UINT32 lDataSize; String aBuffer; + ByteString aByteStr; SmSymSet *pSymbolSet; *pSvStream >> lDataSize >> lIdent >> lVersion; @@ -1698,14 +1703,14 @@ BOOL SmDocShell::Try2x (SvStorage *pStor, DBG_ASSERT((lVersion == FRMVERSION), "Illegal file version"); *pSvStream >> cTag; - rtl_TextEncoding eEnc = gsl_getSystemTextEncoding(); + rtl_TextEncoding eEnc = RTL_TEXTENCODING_MS_1252; while (cTag && !pSvStream->IsEof()) { switch (cTag) { case 'T': - pSvStream->ReadByteString(aBuffer, eEnc); - aText = aBuffer; + pSvStream->ReadByteString( aByteStr ); + aText = ImportString( aByteStr ); Parse(); break; diff --git a/starmath/source/format.cxx b/starmath/source/format.cxx index 2f86a4819141..fb34f69d10e0 100644 --- a/starmath/source/format.cxx +++ b/starmath/source/format.cxx @@ -2,9 +2,9 @@ * * $RCSfile: format.cxx,v $ * - * $Revision: 1.3 $ + * $Revision: 1.4 $ * - * last change: $Author: tl $ $Date: 2001-05-02 16:58:48 $ + * last change: $Author: tl $ $Date: 2001-08-08 11:22:18 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -117,6 +117,8 @@ SmFormat::SmFormat() vFont[FNT_FIXED] = SmFace(C2S(FNTNAME_COUR), aBaseSize); vFont[FNT_MATH] = SmFace(C2S(FNTNAME_MATH), aBaseSize); + vFont[FNT_MATH].SetCharSet( RTL_TEXTENCODING_UNICODE ); + vFont[FNT_VARIABLE].SetItalic(ITALIC_NORMAL); vFont[FNT_FUNCTION].SetItalic(ITALIC_NONE); vFont[FNT_TEXT].SetItalic(ITALIC_NONE); diff --git a/starmath/source/node.cxx b/starmath/source/node.cxx index 4c7888bfbba9..910a4192e721 100644 --- a/starmath/source/node.cxx +++ b/starmath/source/node.cxx @@ -2,9 +2,9 @@ * * $RCSfile: node.cxx,v $ * - * $Revision: 1.9 $ + * $Revision: 1.10 $ * - * last change: $Author: cmc $ $Date: 2001-07-23 09:07:42 $ + * last change: $Author: tl $ $Date: 2001-08-08 11:22:18 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -385,7 +385,9 @@ void SmNode::Prepare(const SmFormat &rFormat, const SmDocShell &rDocShell) } GetFont() = rFormat.GetFont(FNT_MATH); - GetFont().SetCharSet(RTL_TEXTENCODING_SYMBOL); + //GetFont().SetCharSet(RTL_TEXTENCODING_SYMBOL); + DBG_ASSERT( GetFont().GetCharSet() == RTL_TEXTENCODING_UNICODE, + "unexpected CharSet" ); GetFont().SetWeight(WEIGHT_NORMAL); GetFont().SetItalic(ITALIC_NONE); diff --git a/starmath/source/symbol.cxx b/starmath/source/symbol.cxx index beca93a17bda..76777240d686 100644 --- a/starmath/source/symbol.cxx +++ b/starmath/source/symbol.cxx @@ -2,9 +2,9 @@ * * $RCSfile: symbol.cxx,v $ * - * $Revision: 1.14 $ + * $Revision: 1.15 $ * - * last change: $Author: tl $ $Date: 2001-08-02 15:37:39 $ + * last change: $Author: tl $ $Date: 2001-08-08 11:22:18 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -295,7 +295,10 @@ SvStream& operator << (SvStream& rStream, const SmSym& rSymbol) SvStream& operator >> (SvStream& rStream, SmSym& rSymbol) { - rStream.ReadByteString( rSymbol.Name, gsl_getSystemTextEncoding() ); + ByteString aByteStr; + + rStream.ReadByteString( aByteStr ); + rSymbol.Name = ImportString( aByteStr ); if (SF_Ident == SF_SM20IDENT) ReadSM20Font(rStream, rSymbol.Face); else @@ -422,8 +425,10 @@ SvStream& operator >> (SvStream& rStream, SmSymSet& rSymbolSet) { USHORT n; SmSym *pSymbol; + ByteString aByteStr; - rStream.ReadByteString(rSymbolSet.Name, gsl_getSystemTextEncoding()); + rStream.ReadByteString( aByteStr ); + rSymbolSet.Name = ImportString( aByteStr ); rStream >> n; for (int i = 0; i < n; i++) diff --git a/starmath/source/utility.cxx b/starmath/source/utility.cxx index f51d3a052ca8..42040db6dbab 100644 --- a/starmath/source/utility.cxx +++ b/starmath/source/utility.cxx @@ -2,9 +2,9 @@ * * $RCSfile: utility.cxx,v $ * - * $Revision: 1.2 $ + * $Revision: 1.3 $ * - * last change: $Author: tl $ $Date: 2001-04-09 09:47:44 $ + * last change: $Author: tl $ $Date: 2001-08-08 11:22:18 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -70,6 +70,15 @@ #ifndef NOOLDSV //autogen #include #endif +#ifndef _STRING_HXX +#include +#endif +#ifndef _TOOLS_TENCCVT_HXX +#include +#endif +#ifndef _OSL_THREAD_H_ +#include +#endif #include "starmath.hrc" @@ -95,7 +104,7 @@ SvStream& operator << (SvStream& rStream, const SmFace& rFont) { rStream.WriteByteString(ExportString(rFont.GetName())); rStream << (ULONG)rFont.GetFamily(); - rStream << (ULONG)rFont.GetCharSet(); + rStream << (ULONG)GetSOStoreTextEncoding( rFont.GetCharSet() ); rStream << (ULONG)rFont.GetWeight(); rStream << (ULONG)rFont.GetItalic(); @@ -105,15 +114,14 @@ SvStream& operator << (SvStream& rStream, const SmFace& rFont) SvStream& operator >> (SvStream& rStream, SmFace& rFont) { ULONG nData; - String aString; + ByteString aByteStr; - rtl_TextEncoding eEnc = gsl_getSystemTextEncoding(); - rStream.ReadByteString(aString); - rFont.SetName(aString); + rStream.ReadByteString( aByteStr ); + rFont.SetName( ImportString( aByteStr ) ); rStream >> nData; rFont.SetFamily((FontFamily)nData); rStream >> nData; - rFont.SetCharSet((CharSet)nData); + rFont.SetCharSet( GetSOLoadTextEncoding( (rtl_TextEncoding) nData ) ); rStream >> nData; rFont.SetWeight((FontWeight)nData); rStream >> nData; @@ -126,15 +134,14 @@ void ReadSM20Font(SvStream& rStream, Font& rFont) { BOOL bData; ULONG nData; - String aString; + ByteString aByteStr; - rtl_TextEncoding eEnc = gsl_getSystemTextEncoding(); - rStream.ReadByteString(aString); - rFont.SetName(aString); + rStream.ReadByteString( aByteStr ); + rFont.SetName( ImportString( aByteStr ) ); rStream >> nData; rFont.SetFamily((FontFamily)nData); rStream >> nData; - rFont.SetCharSet((CharSet)nData); + rFont.SetCharSet( GetSOLoadTextEncoding( (rtl_TextEncoding) nData ) ); rStream >> nData; rFont.SetWeight((FontWeight)nData); rStream >> bData; @@ -234,40 +241,135 @@ void SmRectCache::Reset() //////////////////////////////////////////////////////////// +#define TE_UCS2 "UCS2" +#define PRE_TE "" + + ByteString ConvertUnknownCharacter(sal_Unicode ch) { - ByteString aString(""; - + aString += POST_TE; return aString; } -const ByteString ExportString(const String& rString) + +const ByteString ExportString( const String& rString ) { ByteString aString; - rtl_TextEncoding eEnc = gsl_getSystemTextEncoding(); - + rtl_TextEncoding nEnc = RTL_TEXTENCODING_MS_1252; for (xub_StrLen i = 0; i < rString.Len(); i++) { sal_Unicode ch = rString.GetChar(i); if ((ch != '\r') && (ch != '\n') && (ch != '\t')) { - sal_Char cChar = ByteString::ConvertFromUnicode( ch, eEnc ); + sal_Char cChar = ByteString::ConvertFromUnicode( ch, nEnc, FALSE ); if (cChar == 0) aString += ConvertUnknownCharacter(ch); else aString += cChar; } else - aString += ch; + aString += (sal_Char) ch; } aString.ConvertLineEnd(LINEEND_CRLF); + return aString; +} - return (aString); +#define TEXTENCODINGTAB_LEN 12 + +static const struct +{ + const char *pText; + rtl_TextEncoding nEnc; +} aTextEncodingTab[ TEXTENCODINGTAB_LEN ] = +{ + { TE_UCS2, RTL_TEXTENCODING_UCS2 }, // is RTL_TEXTENCODING_UNICODE in 6.0 + { "DONTKNOW", RTL_TEXTENCODING_DONTKNOW }, + { "ANSI", RTL_TEXTENCODING_MS_1252 }, + { "MAC", RTL_TEXTENCODING_APPLE_ROMAN }, + { "PC437", RTL_TEXTENCODING_IBM_437 }, + { "PC850", RTL_TEXTENCODING_ASCII_US }, + { "PC860", RTL_TEXTENCODING_IBM_860 }, + { "PC861", RTL_TEXTENCODING_IBM_861 }, + { "PC863", RTL_TEXTENCODING_IBM_863 }, + { "PC865", RTL_TEXTENCODING_IBM_865 }, + { "SYSTEM", RTL_TEXTENCODING_DONTKNOW }, + { "SYMBOL", RTL_TEXTENCODING_SYMBOL } +}; + +int GetTextEncodingTabIndex( const String &rTxt, xub_StrLen nPos ) +{ + int nRes = -1; + for (int i = 0; i < TEXTENCODINGTAB_LEN && nRes == -1; ++i) + { + if (nPos == rTxt.SearchAscii( aTextEncodingTab[i].pText , nPos )) + nRes = i; + } + return nRes; +} + +const String ImportString( const ByteString& rByteString ) +{ + String aString( rByteString, RTL_TEXTENCODING_MS_1252 ); + const xub_StrLen nPreLen = sizeof( PRE_TE ) - 1; + const xub_StrLen nPostLen = sizeof( POST_TE ) - 1; + + xub_StrLen nPreStart = 0; + while( STRING_NOTFOUND != ( nPreStart = + aString.SearchAscii( PRE_TE, nPreStart )) ) + { + // + // convert 'unknown character' to unicode character + // + xub_StrLen nTeStart = nPreStart + nPreLen; + xub_StrLen nTeLen = 0; + int nIdx = GetTextEncodingTabIndex( aString, nTeStart ); + DBG_ASSERT( nIdx >= 0, "text-encoding is missing" ); + rtl_TextEncoding nEnc = RTL_TEXTENCODING_DONTKNOW; + if (nIdx >= 0) + { + nEnc = aTextEncodingTab[ nIdx ].nEnc; + nTeLen = strlen( aTextEncodingTab[ nIdx ].pText ); + } + if (RTL_TEXTENCODING_DONTKNOW == nEnc) + nEnc = osl_getThreadTextEncoding(); + // + xub_StrLen nNumStart = nTeStart + nTeLen + 1, // +1 because of "(" + nReplLen; + xub_StrLen nPostStart = aString.SearchAscii( POST_TE, nNumStart ); + String sRepl; + if( STRING_NOTFOUND != nPostStart ) + { + INT32 nCharVal = aString.Copy( nNumStart, nPostStart - nNumStart ).ToInt32(); + DBG_ASSERT( nCharVal != 0, "String -> Int32 failed ?" ); + if (RTL_TEXTENCODING_UNICODE == nEnc) + sRepl = (sal_Unicode) nCharVal; + else + { + DBG_ASSERT( 0 <= nCharVal && nCharVal <= 256, + "character value out of range" ); + sRepl = ByteString::ConvertToUnicode( nCharVal, nEnc ); + } + DBG_ASSERT( sRepl.Len(), "conversion failed" ); + nReplLen = nPostStart + nPostLen - nPreStart; + } + else + { + DBG_ERROR( "import error: 'unknown character' delimiter missing" ); + sRepl.AssignAscii( RTL_CONSTASCII_STRINGPARAM( "" ) ); + nReplLen = nPreLen; + } + + aString.Replace( nPreStart, nReplLen, sRepl ); + nPreStart += sRepl.Len(); + } + + aString.ConvertLineEnd(); + return aString; } ////////////////////////////////////////////////////////////