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.

424 lines
14 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-2000, Microsoft Corporation. All rights reserved.
  18. */
  19. #ifndef __RTFREAD_H
  20. #define __RTFREAD_H
  21. #include "_rtfconv.h"
  22. #if defined(DEBUG)
  23. #include "_rtflog.h"
  24. #endif
  25. typedef SHORT ALIGN;
  26. /*
  27. * Destinations of the stuff we may read in while parsing
  28. */
  29. enum
  30. {
  31. destRTF,
  32. destColorTable,
  33. destFontTable,
  34. destBinary,
  35. destObject,
  36. destObjectClass,
  37. destObjectName,
  38. destObjectData, // Keep next 2 together
  39. destPicture, // Keep next 3 together
  40. destField,
  41. destFieldResult,
  42. destFieldInstruction,
  43. destParaNumbering,
  44. destParaNumText,
  45. destRealFontName,
  46. destFollowingPunct,
  47. destLeadingPunct,
  48. destDocumentArea,
  49. destNULL,
  50. destStyleSheet,
  51. destShapeName,
  52. destShapeValue,
  53. destMAX // This must be last entry
  54. };
  55. /*
  56. * Super or subscripting state
  57. */
  58. enum
  59. {
  60. sSub = -1,
  61. sNoSuperSub,
  62. sSuper
  63. };
  64. enum DEFAULTFONT
  65. {
  66. DEFFONT_NOCH = 0,
  67. DEFFONT_LTRCH,
  68. DEFFONT_RTLCH,
  69. DEFFONT_LOCH,
  70. DEFFONT_HICH,
  71. DEFFONT_DBCH
  72. };
  73. typedef struct tagDefFont
  74. {
  75. SHORT sHandle;
  76. SHORT sSize;
  77. } DEFFONT;
  78. /*
  79. * @struct STATE |
  80. * Structure to save current reader state
  81. */
  82. struct STATE
  83. {
  84. WORD cbSkipForUnicodeMax; //@field Bytes to skip after \uN is read
  85. SHORT iCF; //@field CF index at LBRACE
  86. // Miscellaneous flags
  87. unsigned fBullet : 1; //@field group is a \\pn bullet group
  88. unsigned fRealFontName : 1; //@field found a real font name when parsing
  89. unsigned fBackground : 1; //@field background being processed
  90. unsigned fShape : 1; //@field {\shp...} being processed
  91. // BiDi flags
  92. unsigned fRightToLeftPara : 1; //@field Para text going right to left ?
  93. unsigned fZeroWidthJoiner : 1; //@field Zero Width Joiner ?
  94. // xchg 12370: keep numbering indent separate
  95. SHORT sIndentNumbering; //@field numbering indent
  96. SHORT sDest; //@field Current destination
  97. int nCodePage; //@field Current code page
  98. // Scratch pad variables
  99. TEXTFONT * ptf; //@field Ptr to font table entry to fill
  100. BYTE bRed; //@field Color table red entry
  101. BYTE bGreen; //@field Color table green entry
  102. BYTE bBlue; //@field Color table blue entry
  103. char iDefFont; //@field Default font (\dbch, \rtlch, etc.)
  104. STATE * pstateNext; //@field Next state on stack
  105. STATE * pstatePrev; //@field Previous state on stack
  106. CParaFormat *pPF; //@field PF for the state to which
  107. // delta's are applied
  108. DWORD dwMaskPF;
  109. DWORD dwMaskPF2;
  110. DEFFONT rgDefFont[6]; //@cmember Default fonts for \dbch, etc.
  111. STATE() {};
  112. //@cmember Adds or applies PF to state's PF
  113. BOOL AddPF(const CParaFormat &PF,
  114. LONG lDocType, DWORD dwMask, DWORD dwMask2);
  115. void DeletePF(); //@cmember Deletes PF for state
  116. void SetCodePage(LONG CodePage);
  117. };
  118. typedef struct TableState
  119. {
  120. BYTE _cCell;
  121. BYTE _iCell;
  122. } TABLESTATE;
  123. class CRTFRead ;
  124. class COleObject;
  125. class RTFREADOLESTREAM : public OLESTREAM
  126. {
  127. OLESTREAMVTBL OLEStreamVtbl; // @member - memory for OLESTREAMVTBL
  128. public:
  129. CRTFRead *Reader; // @cmember EDITSTREAM to use
  130. RTFREADOLESTREAM::RTFREADOLESTREAM ()
  131. {
  132. lpstbl = & OLEStreamVtbl ;
  133. }
  134. };
  135. #define NSTYLES (NHSTYLES + 1)
  136. #define CCELLS ((1 + CELL_EXTRA)*MAX_TABLE_CELLS)
  137. /*
  138. * CRTFRead
  139. *
  140. * @class RichEdit RTF reader class.
  141. *
  142. * @base public | CRTFConverter
  143. */
  144. class CRTFRead : public CRTFConverter
  145. {
  146. //@access Private Methods and Data
  147. // Lexical analyzer outputs
  148. LONG _iParam; //@cmember Control-word parameter
  149. TOKEN _token; //@cmember Current control-word token
  150. TOKEN _tokenLast; //@cmember Previous token
  151. BYTE * _szText; //@cmember Current BYTE text string
  152. // Used for reading in
  153. BYTE _rgStyles[NSTYLES]; //@cmember Style handle table
  154. SHORT _Style; //@cmember Current style handle
  155. LONG _cbBinLeft; //@cmember cb of bin data left to read
  156. BYTE * _pchRTFBuffer; //@cmember Buffer for GetChar()
  157. BYTE * _pchRTFCurrent; //@cmember Current position in buffer
  158. BYTE * _pchRTFEnd; //@cmember End of buffer
  159. BYTE * _pchHexCurr; //@cmember Current position within
  160. // _szText when reading object data
  161. INT _nStackDepth; //@cmember Stack depth
  162. STATE * _pstateStackTop; //@cmember Stack top
  163. STATE * _pstateLast; //@cmember Last STATE allocated
  164. LONG _cpThisPara; //@cmember Start of current paragraph
  165. DWORD _dwMaskCF; //@cmember Character format mask
  166. DWORD _dwMaskCF2; //@cmember Character format mask 2
  167. CParaFormat _PF; //@cmember Paragraph format changes
  168. DWORD _dwMaskPF; //@cmember Paragraph format mask
  169. DWORD _dwMaskPF2; //@cmember Paragraph format mask
  170. LONG _cTab; //@cmember Count of defined tabs
  171. LONG _dxCell; //@cmember Half space betw table cells
  172. LONG _cCell; //@cmember Count of cells in table row
  173. LONG _iCell; //@cmember Current cell in table row
  174. COLORREF _crCellCustom1; //@cmember First custom cell color
  175. COLORREF _crCellCustom2; //@cmember Second custom cell color
  176. LONG _rgxCell[CCELLS]; //@cmember Cell right boundaries
  177. LONG _xCellPrev; //@cmember Previous \cellx N
  178. LONG _xRowOffset; //@cmember Row offset to ensure rows fall along left margin
  179. DWORD _dwBorderColors; //@cmember Border colors
  180. DWORD _dwCellColors; //@cmember Cell border and back colors
  181. DWORD _dwShading; //@cmember Shading in hundredths of per cent (could be 1 byte)
  182. WORD _wBorders; //@cmember Border styles
  183. WORD _wBorderSpace; //@cmember Border/text spaces
  184. WORD _wBorderWidth; //@cmember Border widths
  185. SHORT _iTabsTable; //@cmember _iTabs used by last table
  186. TABLESTATE _rgTableState[MAXTABLENEST];
  187. DWORD _dwRowResolveFlags; //@cmember Flags for row start resolution
  188. COleObject *_pobj; //@cmember Pointer to our object
  189. union
  190. {
  191. DWORD _dwFlagsUnion; // All together now
  192. struct
  193. {
  194. WORD _fFailedPrevObj : 1; //@cmember Fail to get prev object ?
  195. WORD _fNeedIcon : 1; //@cmember Objects needs an icon pres
  196. WORD _fNeedPres : 1; //@cmember Use stored presenation.
  197. WORD _fGetColorYet : 1; //@cmember used for AutoColor detect
  198. WORD _fRightToLeftDoc : 1; //@cmember Document is R to L ?
  199. WORD _fReadDefFont : 1; //@cmember True if we've read a default
  200. // font from RTF input
  201. WORD _fSymbolField : 1; //@cmember TRUE if handling SYMBOL field
  202. WORD _fSeenFontTable : 1; //@cmember True if \fonttbl processed
  203. WORD _fCharSet : 1; //@cmember True if \fcharset processed
  204. WORD _fNoRTFtoken : 1; //@cmember True in 1.0 mode if \rtf hasn't been seen
  205. WORD _fInTable : 1; //@cmember True if pasting into table
  206. WORD _fStartRow : 1; //@cmember True if AddText should start row
  207. WORD _fNo_iTabsTable : 1; //@cmember Suppress _iTabsTable changes
  208. WORD _fParam : 1; //@cmember TRUE if token has param
  209. WORD _fNotifyLowFiRTF : 1; //@cmember TRUE if EN_LOWFIRTF
  210. WORD _fMac : 1; //@cmember TRUE if \mac file
  211. BYTE _bDocType; //@cmember Document Type
  212. BYTE _fRTLRow : 1; //@cmember RightToLeft table row
  213. BYTE _fNon0CharSet : 1; //@cmember CharSet other than ANSI_CHARSET found
  214. BYTE _fBody : 1; //@cmember TRUE when body text has started
  215. };
  216. };
  217. SHORT _sDefaultFont; //@cmember Default font to use
  218. SHORT _sDefaultBiDiFont; //@cmember Default Bidi font to use
  219. SHORT _sDefaultLanguage; //@cmember Default language to use
  220. SHORT _sDefaultLanguageFE; //@cmember Default FE language to use
  221. SHORT _sDefaultTabWidth; //@cmember Default tabwidth to use
  222. SHORT _iKeyword; //@cmember Keyword index of last token
  223. WCHAR _szNumText[cchMaxNumText]; //@cmember Scratch pad for numbered lists
  224. int _nCodePage; //@cmember default codepage (RTF-read-level)
  225. int _cchUsedNumText; //@cmember space used in szNumText
  226. RTFOBJECT * _prtfObject; //@cmember Ptr to RTF Object
  227. RTFREADOLESTREAM RTFReadOLEStream; //@cmember RTFREADOLESTREAM to use
  228. DWORD _dwFlagsShape; //@cmember Shape flags
  229. WCHAR * _szUnicode; //@cmember String to hold Unicoded chars
  230. LONG _cchUnicode; //@cmember Size of _szUnicode in WCHARs
  231. DWORD _cchMax; //@cmember Max cch that can still be inserted
  232. LONG _cpFirst; //@cmember Starting cp for insertion
  233. // Object attachment placeholder list
  234. LONG * _pcpObPos;
  235. int _cobPosFree;
  236. int _cobPos;
  237. DWORD _dwCellBrdrWdths; //@cmember Current cell border widths
  238. LONG _dyRow; //@cmember Current row height \trrh N
  239. WORD _wNumberingStyle; //@cmember Numbering style to use
  240. SHORT _iTabsLevel1; //@cmember _iTabs for table level 1
  241. BYTE _bTabType; //@cmember left/right/center/deciml/bar tab
  242. BYTE _bTabLeader; //@cmember none/dotted/dashed/underline
  243. BYTE _bBorder; //@cmember Current border segment
  244. BYTE _iCharRepBiDi; //@cmember Default system's BiDi char repertoire
  245. BYTE _bCellFlags; //@cmember Cell flags, e.g., merge flags
  246. BYTE _bShapeNameIndex; //@cmember Shape name index
  247. BYTE _bAlignment; //@cmember Alignment for tables
  248. // Lexical Analyzer Functions
  249. void DeinitLex(); //@cmember Release lexer storage
  250. BOOL InitLex(); //@cmember Alloc lexer storage
  251. EC SkipToEndOfGroup(); //@cmember Skip to matching }
  252. TOKEN TokenFindKeyword( //@cmember Find _token for szKeyword
  253. BYTE *szKeyword, const KEYWORD *prgKeyword, LONG cKeyword);
  254. TOKEN TokenGetHex(); //@cmember Get next byte from hex input
  255. TOKEN TokenGetKeyword(); //@cmember Get next control word
  256. TOKEN TokenGetText(BYTE ch); //@cmember Get text in between ctrl words
  257. TOKEN TokenGetToken(); //@cmember Get next {, }, \\, or text
  258. BOOL FInDocTextDest() const; //@cmember Is reader in document text destination
  259. //@cmember Send LowFi notif. if enabled
  260. void CheckNotifyLowFiRTF(BOOL fEnable = FALSE);
  261. // Input Functions
  262. LONG FillBuffer(); //@cmember Fill input buffer
  263. BYTE GetChar(); //@cmember Return char from input buffer
  264. BYTE GetCharEx(); //@cmember Return char from input buffer incl \'xx
  265. BYTE GetHex(); //@cmember Get next hex value from input
  266. BYTE GetHexSkipCRLF(); //@cmember Get next hex value from input
  267. void GetParam(char ach); //@cmember Get keyword's numeric parameter
  268. void ReadFontName(STATE *pstate, int iAllASCII);//@cmember Copy font name into state
  269. BOOL UngetChar(); //@cmember Decrement input buffer ptr
  270. BOOL UngetChar(UINT cch); //@cmember Decrement input buffer ptr 'cch' times
  271. // Reader Functions
  272. //@cmember Insert text into range
  273. EC AddText(WCHAR *pch, LONG cch, BOOL fNumber, BOOL fUN = FALSE);
  274. void Apply_CF(); //@cmember Apply _CF changes
  275. SHORT Apply_PF(); //@cmember Apply _PF changes
  276. COLORREF GetColor(DWORD dwMask); //@cmember Get color _iParam for mask
  277. LONG GetStandardColorIndex(); //@cmember Get std index <-> _iparam
  278. LONG GetCellColorIndex(); //@cmember Get cell index <-> _iparam
  279. EC HandleChar(WORD ch); //@cmember Insert single Unicode
  280. EC HandleEndGroup(); //@cmember Handle }
  281. EC HandleEndOfPara(); //@cmember Insert EOP into range
  282. void HandleCell(); //@cmember Handle \cell
  283. void HandleCellx(LONG iParam); //@cmember Handle \cellx
  284. //@cmember Handle Word EQ field
  285. EC HandleEq(CTxtRange &rg, CTxtPtr &tp);
  286. void HandleFieldEndGroup(); //@cmember Handle Field End Group
  287. EC HandleFieldInstruction(); //@cmember Handle field instruction
  288. EC HandleFieldSymbolFont(BYTE *pch); //@cmember Handle \\f "Facename" in symbol
  289. //@cmember Handle specific SYMBOL field instruction
  290. EC HandleFieldSymbolInstruction(BYTE *pch, BYTE *szSymbol);
  291. EC HandleNumber(); //@cmember Handle _iParam as textToken
  292. EC HandleStartGroup(); //@cmember Handle {
  293. enum { CONTAINS_NONASCII, ALL_ASCII };
  294. //@cmember Insert szText into range
  295. EC HandleText(BYTE *szText, int iAllASCII, LONG cchText = -1);
  296. EC HandleTextToken(STATE *pstate);//@cmember Handle tokenText
  297. EC HandleToken(); //@cmember Grand _token switchboard
  298. void HandleUN(STATE *pstate); //@cmember Handle \uN sequence
  299. BOOL IsLowMergedCell(); //@cmember TRUE iff low merged cell
  300. void Pard(STATE *pstate); //@cmember Set default para props
  301. void SelectCurrentFont(INT iFont);//@cmember Select font <p iFont>
  302. void SetPlain(STATE *pstate); //@cmember Setup _CF for \plain
  303. void DelimitRow(WCHAR *szRowDelimiter); //@cmember Insert start-of-row
  304. void InitializeTableRowParms(); //@cmember Restore table parms to initial state
  305. // Object functions
  306. EC HexToByte(BYTE *rgchHex, BYTE *pb);
  307. void FreeRtfObject();
  308. EC StrAlloc(WCHAR ** ppsz, BYTE * sz);
  309. BOOL ObjectReadFromEditStream(void);
  310. BOOL ObjectReadEBookImageInfoFromEditStream(void); //@to get e-book image info
  311. BOOL StaticObjectReadFromEditStream(int cb = 0);
  312. BOOL ObjectReadSiteFlags( REOBJECT * preobj);
  313. void SetBorderParm(WORD& Parm, LONG Value);
  314. BOOL CpgInfoFromFaceName(TEXTFONT *ptf); //@cmember Determines
  315. // charset/cpg based on TEXTFONT::szName
  316. void HandleSTextFlow(int mode); //@cmember Handle S Text Flow
  317. //@access Public Methods
  318. public:
  319. //@cmember RTF reader constructor
  320. CRTFRead(CTxtRange *prg, EDITSTREAM *pes, DWORD dwFlags);
  321. inline ~CRTFRead(); //@cmember CRTFRead destructor
  322. LONG ReadRtf(); //@cmember Main Entry to RTF reader
  323. LONG ReadData(BYTE *pbBuffer, LONG cbBuffer); // todo friend
  324. LONG ReadBinaryData(BYTE *pbBuffer, LONG cbBuffer);
  325. LONG SkipBinaryData(LONG cbSkip);
  326. LONG ReadRawText(char **pszRawText); //@cmember Read in raw text
  327. // Member functions/data to test coverage of RTF reader
  328. #if defined(DEBUG)
  329. public:
  330. void TestParserCoverage();
  331. private:
  332. CHAR *PszKeywordFromToken(TOKEN token);
  333. BOOL FTokIsSymbol(TOKEN tok);
  334. BOOL FTokFailsCoverageTest(TOKEN tok);
  335. BOOL _fTestingParserCoverage;
  336. private:
  337. // member data for RTF tag logging
  338. CRTFLog *_prtflg;
  339. #endif //DEBUG
  340. };
  341. /*
  342. * PointsToFontHeight(cHalfPoints)
  343. *
  344. * @func
  345. * Convert half points to font heights
  346. *
  347. * @parm int |
  348. * sPointSize | Font height in half points
  349. *
  350. * @rdesc
  351. * LONG The corresponding CCharFormat.yHeight value
  352. */
  353. #define PointsToFontHeight(cHalfPoints) (((LONG) cHalfPoints) * 10)
  354. /*
  355. * CRTFRead::~CRTFRead
  356. *
  357. * @mdesc
  358. * Destructor
  359. *
  360. */
  361. inline CRTFRead::~CRTFRead()
  362. {
  363. // TODO: Implement RTF tag logging for the Mac and WinCE
  364. #if defined(DEBUG) && !defined(NOFULLDEBUG)
  365. if(_prtflg)
  366. {
  367. delete _prtflg;
  368. _prtflg = NULL;
  369. }
  370. #endif
  371. }
  372. #endif // __RTFREAD_H