1 /* AbiWord 2 * Copyright (C) 1998 AbiSource, Inc. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 2 7 * of the License, or (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 17 * 02110-1301 USA. 18 */ 19 20 21 #ifndef IE_EXP_RTF_H 22 #define IE_EXP_RTF_H 23 24 #include "ie_exp.h" 25 #include "ut_vector.h" 26 #include "ut_hash.h" 27 #include "ut_misc.h" 28 #include "ut_iconv.h" 29 #include "pl_Listener.h" 30 #include "fl_AutoLists.h" 31 #include "fl_AutoNum.h" 32 33 class PD_Document; 34 class PD_Style; 35 class PP_AttrProp; 36 class s_RTF_ListenerWriteDoc; 37 class s_RTF_ListenerGetProps; 38 class s_RTF_AttrPropAdapter; 39 class s_RTF_AttrPropAdapter_Style; 40 class ie_exp_RTF_MsWord97List; 41 class ie_exp_RTF_MsWord97ListSimple; 42 class ie_exp_RTF_MsWord97ListMulti; 43 class ie_exp_RTF_ListOveride; 44 class _rtf_font_info; 45 46 // The exporter/writer for RTF file format (based upon spec version 1.5). 47 48 class ABI_EXPORT IE_Exp_RTF_Sniffer : public IE_ExpSniffer 49 { 50 friend class IE_Exp; 51 52 public: 53 IE_Exp_RTF_Sniffer (); ~IE_Exp_RTF_Sniffer()54 virtual ~IE_Exp_RTF_Sniffer () {} 55 56 UT_Confidence_t supportsMIME (const char * szMIME); 57 58 virtual bool recognizeSuffix (const char * szSuffix); 59 virtual bool getDlgLabels (const char ** szDesc, 60 const char ** szSuffixList, 61 IEFileType * ft); 62 virtual UT_Error constructExporter (PD_Document * pDocument, 63 IE_Exp ** ppie); 64 }; 65 66 /* 67 * this is for exporting to RTF understood by attic software 68 * like WordPad and probably Word6.0. 69 */ 70 class ABI_EXPORT IE_Exp_RTF_attic_Sniffer : public IE_ExpSniffer 71 { 72 friend class IE_Exp; 73 74 public: 75 IE_Exp_RTF_attic_Sniffer (); ~IE_Exp_RTF_attic_Sniffer()76 virtual ~IE_Exp_RTF_attic_Sniffer () {} 77 78 virtual bool recognizeSuffix (const char * szSuffix); 79 virtual bool getDlgLabels (const char ** szDesc, 80 const char ** szSuffixList, 81 IEFileType * ft); 82 virtual UT_Error constructExporter (PD_Document * pDocument, 83 IE_Exp ** ppie); 84 }; 85 86 // hack for "msword" export 87 88 class ABI_EXPORT IE_Exp_MsWord_Hack_Sniffer : public IE_ExpSniffer 89 { 90 friend class IE_Exp; 91 92 public: 93 IE_Exp_MsWord_Hack_Sniffer (); ~IE_Exp_MsWord_Hack_Sniffer()94 virtual ~IE_Exp_MsWord_Hack_Sniffer () {} 95 96 virtual bool recognizeSuffix (const char * szSuffix); 97 virtual bool getDlgLabels (const char ** szDesc, 98 const char ** szSuffixList, 99 IEFileType * ft); 100 virtual UT_Error constructExporter (PD_Document * pDocument, 101 IE_Exp ** ppie); 102 }; 103 104 struct NumberedStyle; 105 106 class ABI_EXPORT IE_Exp_RTF : public IE_Exp 107 { 108 friend class s_RTF_ListenerWriteDoc; 109 friend class s_RTF_ListenerGetProps; 110 111 public: 112 IE_Exp_RTF(PD_Document * pDocument); 113 IE_Exp_RTF(PD_Document * pDocument,bool atticFormat); 114 virtual ~IE_Exp_RTF(); 115 ie_exp_RTF_MsWord97ListMulti * getNthMultiLevel(UT_uint32 i) const; 116 ie_exp_RTF_MsWord97ListSimple * getNthSimple(UT_uint32 i) const; 117 ie_exp_RTF_ListOveride * getNthOveride(UT_uint32 i) const; 118 UT_uint32 getMultiLevelCount(void) const; 119 UT_uint32 getSimpleListCount(void) const; 120 UT_uint32 getOverideCount(void) const; 121 UT_uint32 getMatchingOverideNum(UT_uint32 ID); 122 void exportHdrFtr(const char * pszHdrFtr , const char * pszHdrFtrID,const char * pszKeyword); isCharRTL()123 UT_BidiCharType isCharRTL() {return m_CharRTL;} setCharRTL(UT_BidiCharType t)124 void setCharRTL(UT_BidiCharType t) {m_CharRTL = t;} setByteBuf(UT_ByteBuf * pBuf)125 void setByteBuf(UT_ByteBuf * pBuf) 126 { _setByteBuf(pBuf);} getByteBuf(void)127 UT_ByteBuf * getByteBuf(void) 128 { return _getByteBuf();} 129 protected: 130 virtual UT_Error _writeDocument(void); 131 UT_Error _writeDocumentLocal(bool bSkipHeader); 132 UT_sint32 _findColor(const char * szColor) const; 133 UT_sint32 _findOrAddColor (const char * szColor); 134 void _addColor(const char * szColor); 135 void _rtf_open_brace(void); 136 void _rtf_close_brace(void); 137 bool _rtf_reopen_brace(void); 138 void _rtf_keyword(const char * szKey); 139 void _rtf_keyword(const char * szKey, UT_sint32 d); 140 void _rtf_keyword_space(const char * szKey, UT_sint32 d); 141 void _rtf_keyword(const char * szKey, const char * szValue); 142 void _rtf_nonascii_hex2(UT_sint32 d); 143 void _rtf_nonascii_hex2(UT_sint32 d, UT_String & pStr); 144 void _rtf_keyword_hex2(const char * szKey, UT_sint32 d); 145 void _rtf_keyword_ifnotdefault(const char * szKey, const char * szValue, UT_sint32 defaultValue); 146 void _rtf_keyword_ifnotdefault_twips(const char * szKey, const char * szValue, UT_sint32 defaultValue); 147 void _rtf_semi(void); 148 void _rtf_fontname(const char * szFontName); 149 void _rtf_chardata(const char * pbuf, UT_uint32 buflen); 150 void _rtf_chardata(const std::string& buf); 151 void _rtf_pcdata(UT_UTF8String &sPCData, bool bSupplyUC=false, UT_uint32 iAltChars=1); 152 void _rtf_pcdata(const std::string & szPCData, bool bSupplyUC=false, UT_uint32 iAltChars=1); 153 void _rtf_pcdata(const char * szPCData, bool bSupplyUC=false, UT_uint32 iAltChars=1); 154 void _rtf_nl(void); 155 const gchar * _getStyleProp(s_RTF_AttrPropAdapter_Style * pADStyle, 156 const s_RTF_AttrPropAdapter * apa, 157 const char * szProp); 158 159 bool _write_rtf_header(void); 160 bool _write_rtf_trailer(void); 161 162 void _clearStyles(); 163 void _selectStyles(); 164 UT_uint32 _getStyleNumber(const PD_Style * pStyle); 165 UT_uint32 _getStyleNumber(const gchar * szStyleName); 166 167 void _write_prop_ifnotdefault(const PD_Style * pStyle, const gchar * szPropName, const char * szRTFName); 168 void _write_prop_ifyes(const PD_Style * pStyle, const gchar * szPropName, const char * szRTFName); 169 void _write_tabdef(const char * szTabStops); 170 void _write_charfmt(const s_RTF_AttrPropAdapter &); 171 172 void _write_parafmt(const PP_AttrProp * pSpanAP, 173 const PP_AttrProp * pBlockAP, 174 const PP_AttrProp * pSectionAP, 175 bool & bStartedList, pf_Frag_Strux* sdh, UT_uint32 &iCurrID, 176 bool &bIsListBlock, UT_sint32 iNestLevel); 177 178 void _write_style_fmt(const PD_Style *); 179 void _write_stylesheets(void); 180 void _write_listtable(void); 181 void _output_MultiLevelRTF(ie_exp_RTF_MsWord97ListMulti * pMulti); 182 void _output_SimpleListRTF(ie_exp_RTF_MsWord97ListSimple * pSimple); 183 void _output_OveridesRTF(ie_exp_RTF_ListOveride * pOver, UT_uint32 i); 184 void _output_ListRTF(fl_AutoNum * pAuto, UT_uint32 iLevel); 185 void _output_LevelText(fl_AutoNum * pAuto, UT_uint32 iLevel,UT_UCSChar bulletsym); 186 void _get_LeftRight_Side(UT_String & LeftSide, UT_String & RightSide); 187 void _generate_level_Text(fl_AutoNum * pAuto,UT_String & LevelText,UT_String &LevelNumbers, UT_uint32 & lenText, UT_uint32 & ifoundLevel); 188 189 void _output_revision(const s_RTF_AttrPropAdapter & apa, bool bPara,pf_Frag_Strux* sdh, 190 UT_sint32 iNestLevel, bool & bStartedList, bool &bIsListBlock, 191 UT_uint32 &iCurrID); 192 193 UT_sint32 _findFont(const s_RTF_AttrPropAdapter * apa) const; 194 UT_sint32 _findFont(const _rtf_font_info * pfi) const; 195 void _addFont(const _rtf_font_info * pfi); 196 197 private: 198 static bool s_escapeString(UT_UTF8String &sOutStr, UT_UCS4String &sInStr, 199 UT_uint32 iAltChars); 200 static bool s_escapeString(UT_UTF8String &sOutStr, const char * szInStr, 201 UT_uint32 iSize, UT_uint32 iAltChars); 202 static bool s_escapeString( std::string& outStr, const std::string& inStr, 203 UT_uint32 iAltChars = 1 ); 204 static std::string s_escapeString( const std::string& inStr, UT_uint32 iAltChars = 1 ); 205 static std::string s_escapeXMLString( const std::string& inStr ); 206 207 s_RTF_ListenerWriteDoc * m_pListenerWriteDoc; 208 UT_Vector m_vecColors; /* vector of "const char * szColor" */ 209 UT_Vector m_vecFonts; /* vector of struct _font */ 210 bool m_bNeedUnicodeText; /* doc has unicode chars */ 211 UT_sint32 m_braceLevel; /* nesting depth of {} braces */ 212 bool m_bLastWasKeyword; /* just wrote a keyword, so need space before text data */ 213 bool m_atticFormat; /* whether to use unicode for all characters >0xff or convert to native windows encoding*/ 214 UT_GenericStringMap<NumberedStyle*> m_hashStyles; 215 /* Hash containing styles to be exported. The key is the 216 AbiWord style name. The value is a NumberedStyle object 217 (see the cpp file). */ 218 UT_Vector m_vecMultiLevel; 219 UT_Vector m_vecSimpleList; 220 UT_Vector m_vecOverides; 221 222 UT_BidiCharType m_CharRTL; 223 UT_iconv_t m_conv; 224 }; 225 226 /*****************************************************************/ 227 /*****************************************************************/ 228 229 /* This struct contains the RTF font info as needed for the 230 font table. */ 231 class ABI_EXPORT _rtf_font_info 232 { 233 public: 234 _rtf_font_info(); 235 bool init(const s_RTF_AttrPropAdapter & apa, bool bDoFieldFont = false); 236 bool init(const char * szfontName); 237 virtual ~_rtf_font_info(void); 238 bool _is_same(const _rtf_font_info & fi) const; getFontFamily(void)239 const char * getFontFamily(void) const { return szFamily;} getFontName(void)240 const char * getFontName(void) const { return m_szName.c_str();} getFontCharset(void)241 int getFontCharset(void) const { return nCharset;} getFontPitch(void)242 int getFontPitch(void) const { return nPitch;} isTrueType(void)243 bool isTrueType(void) const { return fTrueType;} 244 private: 245 const gchar * szFamily; 246 int nCharset; 247 int nPitch; 248 UT_String m_szName; 249 bool fTrueType; 250 }; 251 252 class ABI_EXPORT ie_exp_RTF_MsWord97List 253 { 254 public: 255 ie_exp_RTF_MsWord97List(fl_AutoNum * pAuto); 256 virtual ~ie_exp_RTF_MsWord97List(void); getAuto(void)257 fl_AutoNum * getAuto(void) const { return m_pAutoNum;} getID(void)258 UT_uint32 getID(void) const {return m_Id;} 259 private: 260 fl_AutoNum * m_pAutoNum; 261 UT_uint32 m_Id; 262 }; 263 264 class ABI_EXPORT ie_exp_RTF_MsWord97ListSimple : public ie_exp_RTF_MsWord97List 265 { 266 public: 267 ie_exp_RTF_MsWord97ListSimple(fl_AutoNum * pAuto); 268 ~ie_exp_RTF_MsWord97ListSimple(void); isSimple(void)269 bool isSimple(void) const { return true;} isMulti(void)270 bool isMulti(void) const { return false;} 271 private: 272 }; 273 274 class ABI_EXPORT ie_exp_RTF_MsWord97ListMulti : public ie_exp_RTF_MsWord97List 275 { 276 public: 277 ie_exp_RTF_MsWord97ListMulti(fl_AutoNum * pAuto); 278 ~ie_exp_RTF_MsWord97ListMulti(void); isSimple(void)279 bool isSimple(void) const { return false;} isMulti(void)280 bool isMulti(void) const { return true;} 281 void addLevel(UT_uint32 iLevel, ie_exp_RTF_MsWord97List * pMsWord97List); 282 ie_exp_RTF_MsWord97List * getListAtLevel(UT_uint32 iLevel, UT_uint32 nthList); 283 UT_uint32 getMatchingID(UT_uint32 listID); 284 private: 285 UT_Vector * m_vLevels[9]; 286 }; 287 288 class ABI_EXPORT ie_exp_RTF_ListOveride 289 { 290 public: 291 ie_exp_RTF_ListOveride(fl_AutoNum * pAuto); 292 ~ie_exp_RTF_ListOveride(void); setOverideID(UT_uint32 ID)293 void setOverideID(UT_uint32 ID) {m_OverideID = ID;} getOverideID(void)294 UT_uint32 getOverideID(void) const { return m_OverideID;} getAutoNum(void)295 fl_AutoNum * getAutoNum(void) const { return m_pAutoNum;} doesOverideMatch(UT_uint32 ID)296 bool doesOverideMatch(UT_uint32 ID) const { return (ID == m_AbiListID);} getAbiListID(void)297 UT_uint32 getAbiListID(void) const { return m_AbiListID;} 298 private: 299 UT_uint32 m_AbiListID; 300 UT_uint32 m_OverideID; 301 fl_AutoNum * m_pAutoNum; 302 }; 303 #endif /* IE_EXP_RTF_H */ 304 305 306 307 308 309 310 311