# HG changeset patch
# User Henning Brinkmann <hbrinkm@openoffice.org>
# Date 1249314787 -7200
# Node ID f922f6bf46e55c57a35b1bb9937a7e831288e132
# Parent  349f391ae964678193c1871590655ae997d12f5f
#i103475# applied patch and verified

diff --git a/sw/source/filter/ww8/ww8par.cxx b/sw/source/filter/ww8/ww8par.cxx
--- a/sw/source/filter/ww8/ww8par.cxx
+++ b/sw/source/filter/ww8/ww8par.cxx
@@ -2177,6 +2177,66 @@
                 switch (pLang->GetLanguage())
                 {
                     case LANGUAGE_CZECH:
+                    case LANGUAGE_HUNGARIAN:
+                    case LANGUAGE_POLISH:
+                        eSrcCharSet = RTL_TEXTENCODING_MS_1250;
+                        break;
+                    case LANGUAGE_RUSSIAN:
+                        eSrcCharSet = RTL_TEXTENCODING_MS_1251;
+                        break;
+                    case LANGUAGE_GREEK:
+                        eSrcCharSet = RTL_TEXTENCODING_MS_1253;
+                        break;
+                    case LANGUAGE_TURKISH:
+                        eSrcCharSet = RTL_TEXTENCODING_MS_1254;
+                        break;
+                    default:
+                        eSrcCharSet = RTL_TEXTENCODING_MS_1252;
+                        break;
+                }
+            }
+        }
+    }
+    return eSrcCharSet;
+}
+
+//Takashi Ono for CJK
+CharSet SwWW8ImplReader::GetCurrentCJKCharSet()
+{
+    /*
+    #i2015
+    If the hard charset is set use it, if not see if there is an open
+    character run that has set the charset, if not then fallback to the
+    current underlying paragraph style.
+    */
+    CharSet eSrcCharSet = eHardCharSet;
+    if (eSrcCharSet == RTL_TEXTENCODING_DONTKNOW)
+    {
+        if (!maFontSrcCJKCharSets.empty())
+            eSrcCharSet = maFontSrcCJKCharSets.top();
+        if ((eSrcCharSet == RTL_TEXTENCODING_DONTKNOW) && (nCharFmt != -1))
+            eSrcCharSet = pCollA[nCharFmt].GetCJKCharSet();
+        if (eSrcCharSet == RTL_TEXTENCODING_DONTKNOW)
+            eSrcCharSet = pCollA[nAktColl].GetCJKCharSet();
+        if (eSrcCharSet == RTL_TEXTENCODING_DONTKNOW)
+        { // patch from cmc for #i52786#
+            /*
+             #i22206#/#i52786#
+             The (default) character set used for a run of text is the default
+             character set for the version of Word that last saved the document.
+
+             This is a bit tentative, more might be required if the concept is correct.
+             When later version of word write older 6/95 documents the charset is
+             correctly set in the character runs involved, so its hard to reproduce
+             documents that require this to be sure of the process involved.
+            */
+            const SvxLanguageItem *pLang =
+                (const SvxLanguageItem*)GetFmtAttr(RES_CHRATR_LANGUAGE);
+            if (pLang)
+            {
+                switch (pLang->GetLanguage())
+                {
+                    case LANGUAGE_CZECH:
                         eSrcCharSet = RTL_TEXTENCODING_MS_1250;
                         break;
                     default:
@@ -2335,6 +2395,8 @@
 
     const CharSet eSrcCharSet = bVer67 ? GetCurrentCharSet() :
         RTL_TEXTENCODING_MS_1252;
+    const CharSet eSrcCJKCharSet = bVer67 ? GetCurrentCJKCharSet() :
+        RTL_TEXTENCODING_MS_1252;
 
     // (re)alloc UniString data
     String sPlainCharsBuf;
@@ -2395,7 +2457,7 @@
                     sal_Char aTest[2];
                     aTest[0] = static_cast< sal_Char >((nUCode & 0xFF00) >> 8);
                     aTest[1] = static_cast< sal_Char >(nUCode & 0x00FF);
-                    String aTemp(aTest, 2, eSrcCharSet);
+                    String aTemp(aTest, 2, eSrcCJKCharSet);
                     ASSERT(aTemp.Len() == 1, "so much for that theory");
                     *pWork = aTemp.GetChar(0);
                 }
diff --git a/sw/source/filter/ww8/ww8par.hxx b/sw/source/filter/ww8/ww8par.hxx
--- a/sw/source/filter/ww8/ww8par.hxx
+++ b/sw/source/filter/ww8/ww8par.hxx
@@ -934,6 +934,7 @@
      Stack of textencoding being used as we progress through the document text
     */
     std::stack<rtl_TextEncoding> maFontSrcCharSets;
+    std::stack<rtl_TextEncoding> maFontSrcCJKCharSets;
 
     /*
      Winword numbering gets imported as SwNumRules, there is a problem that
@@ -1174,6 +1175,7 @@
         rtl_TextEncoding&);
     bool SetNewFontAttr(USHORT nFCode, bool bSetEnums, USHORT nWhich);
     void ResetCharSetVars();
+    void ResetCJKCharSetVars();
 
     const SfxPoolItem* GetFmtAttr( USHORT nWhich );
     bool JoinNode(SwPaM &rPam, bool bStealAttr = false);
@@ -1616,6 +1618,7 @@
     // Laden eines kompletten DocFiles
     ULONG LoadDoc( SwPaM&,WW8Glossary *pGloss=0);
     CharSet GetCurrentCharSet();
+    CharSet GetCurrentCJKCharSet();
 
     void PostProcessAttrs();
 };
diff --git a/sw/source/filter/ww8/ww8par2.cxx b/sw/source/filter/ww8/ww8par2.cxx
--- a/sw/source/filter/ww8/ww8par2.cxx
+++ b/sw/source/filter/ww8/ww8par2.cxx
@@ -4680,4 +4680,11 @@
     return eLTRFontSrcCharSet;
 }
 
+CharSet SwWW8StyInf::GetCJKCharSet() const
+{
+    if ((pFmt) && (pFmt->GetFrmDir().GetValue() == FRMDIR_HORI_RIGHT_TOP))
+        return eRTLFontSrcCharSet;
+    return eCJKFontSrcCharSet;
+}
+
 /* vi:set tabstop=4 shiftwidth=4 expandtab: */
diff --git a/sw/source/filter/ww8/ww8par2.hxx b/sw/source/filter/ww8/ww8par2.hxx
--- a/sw/source/filter/ww8/ww8par2.hxx
+++ b/sw/source/filter/ww8/ww8par2.hxx
@@ -224,6 +224,7 @@
         return pOutlineNumrule;
     }
     CharSet GetCharSet() const;
+    CharSet GetCJKCharSet() const;
 };
 
 class WW8RStyle: public WW8Style
diff --git a/sw/source/filter/ww8/ww8par6.cxx b/sw/source/filter/ww8/ww8par6.cxx
--- a/sw/source/filter/ww8/ww8par6.cxx
+++ b/sw/source/filter/ww8/ww8par6.cxx
@@ -3552,11 +3552,32 @@
         //off the stack will keep in sync
         if (!pAktColl && IsListOrDropcap())
         {
-            if (!maFontSrcCharSets.empty())
-                eSrcCharSet = maFontSrcCharSets.top();
+            if (nWhich == RES_CHRATR_CJK_FONT)
+            {
+                if (!maFontSrcCJKCharSets.empty())
+                {
+                    eSrcCharSet = maFontSrcCJKCharSets.top();
+                }
+                else
+                {
+                    eSrcCharSet = RTL_TEXTENCODING_DONTKNOW;
+                }
+                    
+                maFontSrcCJKCharSets.push(eSrcCharSet);
+            }
             else
-                eSrcCharSet = RTL_TEXTENCODING_DONTKNOW;
-            maFontSrcCharSets.push(eSrcCharSet);
+            {
+                if (!maFontSrcCharSets.empty())
+                {
+                    eSrcCharSet = maFontSrcCharSets.top();
+                }
+                else
+                {
+                    eSrcCharSet = RTL_TEXTENCODING_DONTKNOW;
+                }
+                    
+                maFontSrcCharSets.push(eSrcCharSet);
+            }
         }
         return false;
     }
@@ -3586,7 +3607,10 @@
         else if (IsListOrDropcap())
         {
             //Add character text encoding to stack
-            maFontSrcCharSets.push(eSrcCharSet);
+            if (nWhich  == RES_CHRATR_CJK_FONT)
+                maFontSrcCJKCharSets.push(eSrcCharSet);
+            else
+                maFontSrcCharSets.push(eSrcCharSet);
         }
     }
 
@@ -3602,6 +3626,13 @@
         maFontSrcCharSets.pop();
 }
 
+void SwWW8ImplReader::ResetCJKCharSetVars()
+{
+    ASSERT(!maFontSrcCJKCharSets.empty(),"no charset to remove");
+    if (!maFontSrcCJKCharSets.empty())
+        maFontSrcCJKCharSets.pop();
+}
+
 /*
     Font ein oder ausschalten:
 */
@@ -3632,7 +3663,10 @@
         if( nLen < 0 ) // Ende des Attributes
         {
             pCtrlStck->SetAttr( *pPaM->GetPoint(), nId );
-            ResetCharSetVars();
+            if (nId == RES_CHRATR_CJK_FONT)
+                ResetCJKCharSetVars();
+            else            
+                ResetCharSetVars();
         }
         else
         {
diff --git a/sw/source/filter/ww8/ww8scan.cxx b/sw/source/filter/ww8/ww8scan.cxx
--- a/sw/source/filter/ww8/ww8scan.cxx
+++ b/sw/source/filter/ww8/ww8scan.cxx
@@ -1498,8 +1498,15 @@
             }
             INT32 nFcStart  = SVBT32ToUInt32( ((WW8_PCD*)pData)->fc );
             if( 8 <= pWw8Fib->nVersion )
+            {
                 nFcStart = WW8PLCFx_PCD::TransformPieceAddress( nFcStart,
                                                                 bIsUnicode );
+            }
+            else
+            {
+                if (pWw8Fib->fExtChar)
+                    bIsUnicode=true;
+            }
             INT32 nLen = (nCpEnd - nCpStart) * (bIsUnicode ? 2 : 1);
 
             /*
@@ -1581,7 +1588,10 @@
 
         WW8_FC nRet = SVBT32ToUInt32( ((WW8_PCD*)pData)->fc );
         if (8 > pWw8Fib->nVersion)
-            *pIsUnicode = false;
+	    if (pWw8Fib->fExtChar)
+        	    *pIsUnicode=true;
+            else
+                    *pIsUnicode = false;
         else
             nRet = WW8PLCFx_PCD::TransformPieceAddress( nRet, *pIsUnicode );
 
