diff options
author | Miklos Vajna <vmiklos@suse.cz> | 2013-02-26 17:27:11 +0100 |
---|---|---|
committer | Miklos Vajna <vmiklos@suse.cz> | 2013-02-26 18:06:40 +0100 |
commit | 3a934d928e455eca38f124072c20a624a64aa225 (patch) | |
tree | 38205a0f5ab53299566fcb2282ada5a6b05ce0e4 /filter | |
parent | 5baf32af4c18fd443c41fdf05ab0c8c110fd438e (diff) |
msfilter: support checking if conversion to legacy encoding is lossless or not
Change-Id: I69405c5883af6d3b119b177f51229c8a78f8c24d
Diffstat (limited to 'filter')
-rw-r--r-- | filter/inc/filter/msfilter/rtfutil.hxx | 23 | ||||
-rw-r--r-- | filter/source/msfilter/rtfutil.cxx | 47 |
2 files changed, 62 insertions, 8 deletions
diff --git a/filter/inc/filter/msfilter/rtfutil.hxx b/filter/inc/filter/msfilter/rtfutil.hxx index 6f5d82c37283..aa842fd94001 100644 --- a/filter/inc/filter/msfilter/rtfutil.hxx +++ b/filter/inc/filter/msfilter/rtfutil.hxx @@ -41,10 +41,27 @@ namespace rtfutil { MSFILTER_DLLPUBLIC OString OutHex(sal_uLong nHex, sal_uInt8 nLen); /// Handles correct unicode and legacy export of a single character. -MSFILTER_DLLPUBLIC OString OutChar(sal_Unicode c, int *pUCMode, rtl_TextEncoding eDestEnc); +MSFILTER_DLLPUBLIC OString OutChar(sal_Unicode c, int *pUCMode, rtl_TextEncoding eDestEnc, bool* pSuccess = 0, bool bUnicode = true); -/// Handles correct unicode and legacy export of a string. -MSFILTER_DLLPUBLIC OString OutString(const String &rStr, rtl_TextEncoding eDestEnc); +/** + * Handles correct unicode and legacy export of a string. + * + * @param rStr the string to export + * @param eDestEnc the legacy encoding to use + * @param bUnicode if unicode output is wanted as well, or just legacy + */ +MSFILTER_DLLPUBLIC OString OutString(const String &rStr, rtl_TextEncoding eDestEnc, bool bUnicode = true); + +/** + * Handles correct unicode and legacy export of a string, when a + * '{' \upr '{' keyword ansi_text '}{\*' \ud '{' keyword Unicode_text '}}}' + * construct should be used. + * + * @param pToken the keyword + * @param rStr the text to export + * @param eDestEnc the legacy encoding to use + */ +MSFILTER_DLLPUBLIC OString OutStringUpr(const sal_Char *pToken, const String &rStr, rtl_TextEncoding eDestEnc); } } diff --git a/filter/source/msfilter/rtfutil.cxx b/filter/source/msfilter/rtfutil.cxx index ebb72bdeda94..3e2dfb0fbf85 100644 --- a/filter/source/msfilter/rtfutil.cxx +++ b/filter/source/msfilter/rtfutil.cxx @@ -53,8 +53,10 @@ OString OutHex(sal_uLong nHex, sal_uInt8 nLen) return OString(pStr); } -OString OutChar(sal_Unicode c, int *pUCMode, rtl_TextEncoding eDestEnc) +OString OutChar(sal_Unicode c, int *pUCMode, rtl_TextEncoding eDestEnc, bool* pSuccess, bool bUnicode) { + if (pSuccess) + *pSuccess = true; OStringBuffer aBuf; const sal_Char* pStr = 0; // 0x0b instead of \n, etc because of the replacements in SwWW8AttrIter::GetSnippet() @@ -91,10 +93,13 @@ OString OutChar(sal_Unicode c, int *pUCMode, rtl_TextEncoding eDestEnc) else { OUString sBuf(&c, 1); OString sConverted; - sBuf.convertToString(&sConverted, eDestEnc, OUSTRING_TO_OSTRING_CVTFLAGS); + if (pSuccess) + *pSuccess &= sBuf.convertToString(&sConverted, eDestEnc, RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR); + else + sBuf.convertToString(&sConverted, eDestEnc, OUSTRING_TO_OSTRING_CVTFLAGS); const sal_Int32 nLen = sConverted.getLength(); - if (pUCMode) + if (pUCMode && bUnicode) { if (*pUCMode != nLen) { @@ -130,13 +135,13 @@ OString OutChar(sal_Unicode c, int *pUCMode, rtl_TextEncoding eDestEnc) return aBuf.makeStringAndClear(); } -OString OutString(const String &rStr, rtl_TextEncoding eDestEnc) +OString OutString(const String &rStr, rtl_TextEncoding eDestEnc, bool bUnicode) { SAL_INFO("filter.ms", OSL_THIS_FUNC << ", rStr = '" << OUString(rStr) << "'"); OStringBuffer aBuf; int nUCMode = 1; for (xub_StrLen n = 0; n < rStr.Len(); ++n) - aBuf.append(OutChar(rStr.GetChar(n), &nUCMode, eDestEnc)); + aBuf.append(OutChar(rStr.GetChar(n), &nUCMode, eDestEnc, 0, bUnicode)); if (nUCMode != 1) { aBuf.append(OOO_STRING_SVTOOLS_RTF_UC); aBuf.append((sal_Int32)1); @@ -145,6 +150,38 @@ OString OutString(const String &rStr, rtl_TextEncoding eDestEnc) return aBuf.makeStringAndClear(); } +/// Checks if lossless conversion of the string to eDestEnc is possible or not. +static bool TryOutString(const String &rStr, rtl_TextEncoding eDestEnc) +{ + int nUCMode = 1; + for (xub_StrLen n = 0; n < rStr.Len(); ++n) + { + bool bRet; + OutChar(rStr.GetChar(n), &nUCMode, eDestEnc, &bRet); + if (!bRet) + return false; + } + return true; +} + +OString OutStringUpr(const sal_Char *pToken, const String &rStr, rtl_TextEncoding eDestEnc) +{ + if (TryOutString(rStr, eDestEnc)) + return OString("{") + pToken + " " + OutString(rStr, eDestEnc) + "}"; + + OStringBuffer aRet; + aRet.append("{" OOO_STRING_SVTOOLS_RTF_UPR "{"); + aRet.append(pToken); + aRet.append(" "); + aRet.append(OutString(rStr, eDestEnc, /*bUnicode =*/ false)); + aRet.append("}{" OOO_STRING_SVTOOLS_RTF_IGNORE OOO_STRING_SVTOOLS_RTF_UD "{"); + aRet.append(pToken); + aRet.append(" "); + aRet.append(OutString(rStr, eDestEnc)); + aRet.append("}}}"); + return aRet.makeStringAndClear(); +} + } } |