Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

380 lines
13 KiB

  1. /*
  2. * @doc INTERNAL
  3. *
  4. * @module _RTFREAD.H -- RichEdit RTF Reader Class Definition |
  5. *
  6. * This file contains the type declarations used by the RTF reader
  7. * for the RICHEDIT control
  8. *
  9. * Authors:<nl>
  10. * Original RichEdit 1.0 RTF converter: Anthony Francisco <nl>
  11. * Conversion to C++ and RichEdit 2.0: Murray Sargent
  12. *
  13. * @devnote
  14. * All sz's in the RTF*.? files refer to a LPSTRs, not LPTSTRs, unless
  15. * noted as a szUnicode.
  16. *
  17. * Copyright (c) 1995-1998, Microsoft Corporation. All rights reserved.
  18. */
  19. #ifndef __RTFREAD_H
  20. #define __RTFREAD_H
  21. #include "_rtfconv.h"
  22. // TODO: Implement RTF tag logging for the Mac
  23. #if defined(DEBUG) && !defined(MACPORT)
  24. #include "_rtflog.h"
  25. #endif
  26. typedef SHORT ALIGN;
  27. /*
  28. * Destinations of the stuff we may read in while parsing
  29. */
  30. enum
  31. {
  32. destRTF,
  33. destColorTable,
  34. destFontTable,
  35. destBinary,
  36. destObject,
  37. destObjectClass,
  38. destObjectName,
  39. destObjectData, // keep next two together
  40. destPicture,
  41. destField,
  42. destFieldResult,
  43. destFieldInstruction,
  44. destParaNumbering,
  45. destParaNumText,
  46. destRealFontName,
  47. destFollowingPunct,
  48. destLeadingPunct,
  49. destDocumentArea,
  50. destNULL,
  51. destStyleSheet
  52. };
  53. /*
  54. * Super or subscripting state
  55. */
  56. enum
  57. {
  58. sSub = -1,
  59. sNoSuperSub,
  60. sSuper
  61. };
  62. /*
  63. * @struct STATE |
  64. * Structure to save current reader state
  65. */
  66. struct STATE
  67. {
  68. SHORT sLanguage; //@field Font information
  69. SHORT iCF; //@field CF index at LBRACE
  70. // Miscellaneous flags
  71. unsigned fInTable : 1; //@field Are we parsing a table cell ?
  72. unsigned fBullet : 1; //@field group is a \\pn bullet group
  73. unsigned fRealFontName : 1; //@field found a real font name when parsing
  74. unsigned fExplicitLang : 1; //@field language explicitly declared
  75. unsigned fFieldInst : 1; //@field \fldinst being processed
  76. unsigned fFieldRslt : 1; //@field \fldrslt being processed or processed
  77. // BiDi flags
  78. unsigned fRightToLeftPara : 1; //@field Para text going right to left ?
  79. unsigned fZeroWidthJoiner : 1; //@field Zero Width Joiner ?
  80. unsigned frtlch : 1; //@field Are we going right to left ?
  81. unsigned fltrch : 1; //@field Are we going right to left ?
  82. unsigned fdbch : 1; //@field DBCS run ?
  83. // xchg 12370: keep numbering indent separate
  84. SHORT sIndentNumbering; //@field numbering indent
  85. SHORT sDest; //@field Current destination
  86. int nCodePage; //@field Current code page
  87. WORD cbSkipForUnicodeMax; //@field Bytes to skip after \uN is read
  88. // Scratch pad variables
  89. TEXTFONT * ptf; //@field Ptr to font table entry to fill
  90. BYTE bRed; //@field Color table red entry
  91. BYTE bGreen; //@field Color table green entry
  92. BYTE bBlue; //@field Color table blue entry
  93. STATE * pstateNext; //@field Next state on stack
  94. STATE * pstatePrev; //@field Previous state on stack
  95. CParaFormat *pPF; //@field PF for the state to which
  96. // delta's are applied
  97. DWORD dwMaskPF;
  98. STATE(LONG CodePage) {nCodePage = CodePage;}
  99. //@cmember Adds or applies PF to state's PF
  100. BOOL AddPF(const CParaFormat &PF,
  101. LONG lDocType, DWORD dwMask);
  102. void DeletePF(); //@cmember Deletes PF for state
  103. void SetCodePage(LONG CodePage);
  104. };
  105. class CRTFRead ;
  106. class COleObject;
  107. class RTFREADOLESTREAM : public OLESTREAM
  108. {
  109. OLESTREAMVTBL OLEStreamVtbl; // @member - memory for OLESTREAMVTBL
  110. public:
  111. CRTFRead *Reader; // @cmember EDITSTREAM to use
  112. RTFREADOLESTREAM::RTFREADOLESTREAM ()
  113. {
  114. lpstbl = & OLEStreamVtbl ;
  115. }
  116. };
  117. #define NSTYLES (NHSTYLES + 1)
  118. /*
  119. * CRTFRead
  120. *
  121. * @class RichEdit RTF reader class.
  122. *
  123. * @base public | CRTFConverter
  124. *
  125. */
  126. class CRTFRead : public CRTFConverter
  127. {
  128. //@access Private Methods and Data
  129. // Lexical analyzer outputs
  130. LONG _iParam; //@cmember Control-word parameter
  131. TOKEN _token; //@cmember Current control-word token
  132. TOKEN _tokenLast; //@cmember Previous token
  133. BYTE * _szText; //@cmember Current BYTE text string
  134. BYTE _szParam[cachParamMax]; //@cmember Current parameter string
  135. // Used for reading in
  136. BYTE _rgStyles[NSTYLES]; //@cmember Style handle table
  137. SHORT _Style; //@cmember Current style handle
  138. LONG _cbBinLeft; //@cmember cb of bin data left to read
  139. BYTE * _pchRTFBuffer; //@cmember Buffer for GetChar()
  140. BYTE * _pchRTFCurrent; //@cmember Current position in buffer
  141. BYTE * _pchRTFEnd; //@cmember End of buffer
  142. BYTE * _pchHexCurr; //@cmember Current position within
  143. // _szText when reading object data
  144. INT _nStackDepth; //@cmember Stack depth
  145. STATE * _pstateStackTop; //@cmember Stack top
  146. STATE * _pstateLast; //@cmember Last STATE allocated
  147. LONG _cpThisPara; //@cmember Start of current paragraph
  148. DWORD _dwMaskCF; //@cmember Character format mask
  149. DWORD _dwMaskCF2; //@cmember Character format mask 2
  150. CParaFormat _PF; //@cmember Paragraph format changes
  151. DWORD _dwMaskPF; //@cmember Paragraph format mask
  152. LONG _cTab; //@cmember Count of defined tabs
  153. LONG _dxCell; //@cmember Half space betw table cells
  154. LONG _cCell; //@cmember Count of cells in table row
  155. LONG _iCell; //@cmember Current cell in table row
  156. LONG _rgxCell[MAX_TAB_STOPS];//@cmember Cell right boundaries
  157. LONG _xRowOffset; //@cmember Row offset to ensure rows fall along left margin
  158. DWORD _dwBorderColor; //@cmember Border colors
  159. WORD _wBorders; //@cmember Border styles
  160. WORD _wBorderSpace; //@cmember Border/text spaces
  161. WORD _wBorderWidth; //@cmember Border widths
  162. SHORT _iTabsTable; //@cmember _iTabs used by last table
  163. COleObject *_pobj; //@cmember Pointer to our object
  164. union
  165. {
  166. DWORD _dwFlagsUnion; // All together now
  167. struct
  168. {
  169. WORD _fFailedPrevObj : 1; //@cmember Fail to get prev object ?
  170. WORD _fNeedIcon : 1; //@cmember Objects needs an icon pres
  171. WORD _fNeedPres : 1; //@cmember Use stored presenation.
  172. WORD _fGetColorYet : 1; //@cmember used for AutoColor detect
  173. WORD _fRightToLeftDoc : 1; //@cmember Document is R to L ?
  174. WORD _fReadDefFont : 1; //@cmember True if we've read a default
  175. // font from RTF input
  176. WORD _fHyperlinkField : 1; //@cmember TRUE if handling HYPERLINK field
  177. WORD _fSeenFontTable : 1; //@cmember True if \fonttbl processed
  178. WORD _fCharSet : 1; //@cmember True if \fcharset processed
  179. WORD _fNoRTFtoken : 1; //@cmember True in 1.0 mode if \rtf hasn't been seen
  180. WORD _fInTable : 1; //@cmember True if pasting into table
  181. BYTE _bDocType; //@cmember Document Type
  182. };
  183. };
  184. SHORT _sDefaultFont; //@cmember Default font to use
  185. SHORT _sDefaultBiDiFont; //@cmemeber Default Bidi font to use
  186. SHORT _sDefaultLanguage; //@cmember Default language to use
  187. SHORT _sDefaultLanguageFE; //@cmember Default FE language to use
  188. SHORT _sDefaultTabWidth; //@cmember Default tabwidth to use
  189. BYTE _bAlignment; //@cmember Alignment for tables
  190. BYTE _bInstFieldCharSet; //@cmember charset of for HYPERLINK inst field
  191. BYTE * _szSymbolFieldResult; //@cmember Buffer for SYMBOL field result
  192. CCharFormat _FieldCF; //@cmember Formatting for field result
  193. int _nFieldCodePage;
  194. DWORD _dwMaskFieldCF; //@cmember Character format mask for _FieldCF
  195. DWORD _dwMaskFieldCF2; //@cmember Character format mask 2 for _FieldCF
  196. TEXTFONT * _ptfField;
  197. BOOL _fRestoreFieldFormat; //@cmember Indicates if the caced format was restored
  198. TCHAR _szNumText[cchMaxNumText]; //@cmember Scratch pad for numbered lists
  199. int _nCodePage; //@cmember default codepage (RTF-read-level)
  200. int _cchUsedNumText; //@cmember space used in szNumText
  201. BYTE * _szHyperlinkFldinst; //@cmember Hyperlink Fldinst string buffer
  202. int _cchHyperlinkFldinst; //@cmember size of Hyperlink Fldinst string buffer
  203. int _cchHyperlinkFldinstUsed; //@cmember space used in Hyperlink Fldinst string buffer
  204. BYTE * _szHyperlinkFldrslt; //@cmember Hyperlink Fldrslt string buffer
  205. int _cchHyperlinkFldrslt; //@cmember size of Hyperlink Fldrslt string buffer
  206. int _cchHyperlinkFldrsltUsed; //@cmember space used in Hyperlink Fldrslt string buffer
  207. RTFOBJECT * _prtfObject; //@cmember Ptr to RTF Object
  208. RTFREADOLESTREAM RTFReadOLEStream; //@cmember RTFREADOLESTREAM to use
  209. TCHAR * _szUnicode; //@cmember String to hold Unicoded chars
  210. LONG _cchUnicode; //@cmember Size of _szUnicode in WCHARs
  211. DWORD _cchMax; //@cmember Max cch that can still be inserted
  212. LONG _cpFirst; //@cmember Starting cp for insertion
  213. // Object attachment placeholder list
  214. LONG * _pcpObPos;
  215. int _cobPosFree;
  216. int _cobPos;
  217. WORD _wNumberingStyle; //@cmember Numbering style to use
  218. WORD _cbSkipForUnicode; //@cmember Bytes to skip after \uN
  219. BYTE _bTabType; //@cmember left/right/center/deciml/bar tab
  220. BYTE _bTabLeader; //@cmember none/dotted/dashed/underline
  221. BYTE _bBorder; //@cmember Current border segment
  222. BYTE _bCellBrdrWdths; //@cmember Current cell border widths
  223. BYTE _bBiDiCharSet; //@cmember Default system's charset
  224. // Lexical Analyzer Functions
  225. void DeinitLex(); //@cmember Release lexer storage
  226. BOOL InitLex(); //@cmember Alloc lexer storage
  227. EC SkipToEndOfGroup(); //@cmember Skip to matching }
  228. TOKEN TokenFindKeyword(BYTE *szKeyword); //@cmember Find _token for szKeyword
  229. TOKEN TokenGetHex(); //@cmember Get next byte from hex input
  230. TOKEN TokenGetKeyword(); //@cmember Get next control word
  231. TOKEN TokenGetText(BYTE ch); //@cmember Get text in between ctrl words
  232. TOKEN TokenGetToken(); //@cmember Get next {, }, \\, or text
  233. BOOL FInDocTextDest() const; //@cmember Is reader in document text destination
  234. // Input Functions
  235. LONG FillBuffer(); //@cmember Fill input buffer
  236. BYTE GetChar(); //@cmember Return char from input buffer
  237. BYTE GetHex(); //@cmember Get next hex value from input
  238. BYTE GetHexSkipCRLF(); //@cmember Get next hex value from input
  239. void ReadFontName(STATE *pstate, int iAllASCII);//@cmember Copy font name into state
  240. BOOL UngetChar(); //@cmember Decrement input buffer ptr
  241. BOOL UngetChar(UINT cch); //@cmember Decrement input buffer ptr 'cch' times
  242. // Reader Functions
  243. EC AddText(TCHAR *pch, LONG cch, BOOL fNumber);//@cmember Insert text into range
  244. void Apply_CF(); //@cmember Apply _CF changes
  245. void Apply_PF(); //@cmember Apply _PF changes
  246. COLORREF GetColor(DWORD dwMask); //@cmember Get color _iParam for mask
  247. LONG GetStandardColorIndex(); //@cmember Get std index <-> _iparam
  248. EC HandleChar(WORD ch); //@cmember Insert single Unicode
  249. EC HandleEndGroup(); //@cmember Handle }
  250. EC HandleEndOfPara(); //@cmember Insert EOP into range
  251. EC HandleStartGroup(); //@cmember Handle {
  252. enum { CONTAINS_NONASCII, ALL_ASCII };
  253. EC HandleText(BYTE *szText, int iAllASCII, LONG cchText = -1);//@cmember Insert szText into range
  254. EC HandleToken(); //@cmember Grand _token switchboard
  255. void SelectCurrentFont(INT iFont);//@cmember Select font <p iFont>
  256. void SetPlain(STATE *pstate); //@cmember Setup _CF for \plain
  257. EC HandleFieldInstruction(); //@cmember Handle field instruction
  258. EC HandleFieldSymbolInstruction(BYTE *pch); //@cmember Handle specific SYMBOL field instruction
  259. EC HandleFieldHyperlink(BYTE *pch); //@cmember Handle hyperlink field instruction
  260. EC HandleFieldSymbolFont(BYTE *pch); //@cmember Handle \\f "Facename" in symbol
  261. void HandleFieldEndGroup(); //@cmember Handle Field End Group
  262. // Object functions
  263. EC HexToByte(BYTE *rgchHex, BYTE *pb);
  264. void FreeRtfObject();
  265. EC StrAlloc(TCHAR ** ppsz, BYTE * sz);
  266. BOOL ObjectReadFromEditStream(void);
  267. BOOL StaticObjectReadFromEditStream(int cb = 0);
  268. BOOL ObjectReadSiteFlags( REOBJECT * preobj);
  269. void SetBorderParm(WORD& Parm, LONG Value);
  270. BOOL CpgInfoFromFaceName(TEXTFONT *ptf); //@cmember Determines
  271. // charset/cpg based on TEXTFONT::szName
  272. //@access Public Methods
  273. public:
  274. //@cmember RTF reader constructor
  275. CRTFRead(CTxtRange *prg, EDITSTREAM *pes, DWORD dwFlags);
  276. inline ~CRTFRead(); //@cmember CRTFRead destructor
  277. LONG ReadRtf(); //@cmember Main Entry to RTF reader
  278. LONG ReadData(BYTE *pbBuffer, LONG cbBuffer); // todo friend
  279. LONG ReadBinaryData(BYTE *pbBuffer, LONG cbBuffer);
  280. LONG SkipBinaryData(LONG cbSkip);
  281. LONG ReadRawText(char **pszRawText); //@cmember Read in raw text
  282. // Member functions/data to test coverage of RTF reader
  283. #if defined(DEBUG)
  284. public:
  285. void TestParserCoverage();
  286. private:
  287. CHAR *PszKeywordFromToken(TOKEN token);
  288. BOOL FTokIsSymbol(TOKEN tok);
  289. BOOL FTokFailsCoverageTest(TOKEN tok);
  290. BOOL _fTestingParserCoverage;
  291. private:
  292. // member data for RTF tag logging
  293. CRTFLog *_prtflg;
  294. #endif //DEBUG
  295. };
  296. /*
  297. * PointsToFontHeight(cHalfPoints)
  298. *
  299. * @func
  300. * Convert half points to font heights
  301. *
  302. * @parm int |
  303. * sPointSize | Font height in half points
  304. *
  305. * @rdesc
  306. * LONG The corresponding CCharFormat.yHeight value
  307. */
  308. #define PointsToFontHeight(cHalfPoints) (((LONG) cHalfPoints) * 10)
  309. /*
  310. * CRTFRead::~CRTFRead
  311. *
  312. * @mdesc
  313. * Destructor
  314. *
  315. */
  316. inline CRTFRead::~CRTFRead()
  317. {
  318. // TODO: Implement RTF tag logging for the Mac and WinCE
  319. #if defined(DEBUG) && !defined(MACPORT) && !defined(PEGASUS)
  320. if(_prtflg)
  321. {
  322. delete _prtflg;
  323. _prtflg = NULL;
  324. }
  325. #endif
  326. }
  327. #endif // __RTFREAD_H