msfilter: support checking if conversion to legacy encoding is lossless or not

Change-Id: I69405c5883af6d3b119b177f51229c8a78f8c24d
This commit is contained in:
Miklos Vajna
2013-02-26 17:27:11 +01:00
parent 5baf32af4c
commit 3a934d928e
2 changed files with 62 additions and 8 deletions

View File

@@ -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);
}
}

View File

@@ -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();
}
}
}