Leaked source code of windows server 2003
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.

427 lines
15 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. destObject,
  35. destObjectClass,
  36. destObjectName,
  37. destObjectData, // Keep next 2 together
  38. destPicture, // Keep next 3 together
  39. destField,
  40. destFieldResult,
  41. destFieldInstruction,
  42. destParaNumbering,
  43. destParaNumText,
  44. destRealFontName,
  45. destFollowingPunct,
  46. destLeadingPunct,
  47. destDocumentArea,
  48. destNULL,
  49. destStyleSheet,
  50. destShapeName,
  51. destShapeValue,
  52. destNestTableProps,
  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. BYTE _f1stControlWord : 1; //@cmember TRUE if 1st ctrl word in group
  216. BYTE _fCellxOK : 1; //@cmember TRUE if \cellx okay to handle
  217. };
  218. };
  219. SHORT _sDefaultFont; //@cmember Default font to use
  220. SHORT _sDefaultBiDiFont; //@cmember Default Bidi font to use
  221. SHORT _sDefaultLanguage; //@cmember Default language to use
  222. SHORT _sDefaultLanguageFE; //@cmember Default FE language to use
  223. SHORT _sDefaultTabWidth; //@cmember Default tabwidth to use
  224. SHORT _iKeyword; //@cmember Keyword index of last token
  225. WCHAR _szNumText[cchMaxNumText]; //@cmember Scratch pad for numbered lists
  226. int _nCodePage; //@cmember default codepage (RTF-read-level)
  227. int _cchUsedNumText; //@cmember space used in szNumText
  228. RTFOBJECT * _prtfObject; //@cmember Ptr to RTF Object
  229. RTFREADOLESTREAM RTFReadOLEStream; //@cmember RTFREADOLESTREAM to use
  230. DWORD _dwFlagsShape; //@cmember Shape flags
  231. WCHAR * _szUnicode; //@cmember String to hold Unicoded chars
  232. LONG _cchUnicode; //@cmember Size of _szUnicode in WCHARs
  233. DWORD _cchMax; //@cmember Max cch that can still be inserted
  234. LONG _cpFirst; //@cmember Starting cp for insertion
  235. // Object attachment placeholder list
  236. LONG * _pcpObPos;
  237. int _cobPosFree;
  238. int _cobPos;
  239. DWORD _dwCellBrdrWdths; //@cmember Current cell border widths
  240. LONG _dyRow; //@cmember Current row height \trrh N
  241. WORD _wNumberingStyle; //@cmember Numbering style to use
  242. SHORT _iTabsLevel1; //@cmember _iTabs for table level 1
  243. BYTE _bTabType; //@cmember left/right/center/deciml/bar tab
  244. BYTE _bTabLeader; //@cmember none/dotted/dashed/underline
  245. BYTE _bBorder; //@cmember Current border segment
  246. BYTE _iCharRepBiDi; //@cmember Default system's BiDi char repertoire
  247. BYTE _bCellFlags; //@cmember Cell flags, e.g., merge flags
  248. BYTE _bShapeNameIndex; //@cmember Shape name index
  249. BYTE _bAlignment; //@cmember Alignment for tables
  250. // Lexical Analyzer Functions
  251. void DeinitLex(); //@cmember Release lexer storage
  252. BOOL InitLex(); //@cmember Alloc lexer storage
  253. EC SkipToEndOfGroup(); //@cmember Skip to matching }
  254. TOKEN TokenFindKeyword( //@cmember Find _token for szKeyword
  255. BYTE *szKeyword, const KEYWORD *prgKeyword, LONG cKeyword);
  256. TOKEN TokenGetHex(); //@cmember Get next byte from hex input
  257. TOKEN TokenGetKeyword(); //@cmember Get next control word
  258. TOKEN TokenGetText(BYTE ch); //@cmember Get text in between ctrl words
  259. TOKEN TokenGetToken(); //@cmember Get next {, }, \\, or text
  260. BOOL FInDocTextDest() const; //@cmember Is reader in document text destination
  261. //@cmember Send LowFi notif. if enabled
  262. void CheckNotifyLowFiRTF(BOOL fEnable = FALSE);
  263. // Input Functions
  264. LONG FillBuffer(); //@cmember Fill input buffer
  265. BYTE GetChar(); //@cmember Return char from input buffer
  266. BYTE GetCharEx(); //@cmember Return char from input buffer incl \'xx
  267. BYTE GetHex(); //@cmember Get next hex value from input
  268. BYTE GetHexSkipCRLF(); //@cmember Get next hex value from input
  269. void GetParam(char ach); //@cmember Get keyword's numeric parameter
  270. void ReadFontName(STATE *pstate, int iAllASCII);//@cmember Copy font name into state
  271. BOOL UngetChar(); //@cmember Decrement input buffer ptr
  272. BOOL UngetChar(UINT cch); //@cmember Decrement input buffer ptr 'cch' times
  273. // Reader Functions
  274. //@cmember Insert text into range
  275. EC AddText(WCHAR *pch, LONG cch, BOOL fNumber, BOOL fUN = FALSE);
  276. void Apply_CF(); //@cmember Apply _CF changes
  277. SHORT Apply_PF(); //@cmember Apply _PF changes
  278. COLORREF GetColor(DWORD dwMask); //@cmember Get color _iParam for mask
  279. LONG GetStandardColorIndex(); //@cmember Get std index <-> _iparam
  280. LONG GetCellColorIndex(); //@cmember Get cell index <-> _iparam
  281. EC HandleChar(WORD ch); //@cmember Insert single Unicode
  282. EC HandleEndGroup(); //@cmember Handle }
  283. EC HandleEndOfPara(); //@cmember Insert EOP into range
  284. void HandleCell(); //@cmember Handle \cell
  285. void HandleCellx(LONG iParam); //@cmember Handle \cellx
  286. //@cmember Handle Word EQ field
  287. EC HandleEq(CTxtRange &rg, CTxtPtr &tp);
  288. void HandleFieldEndGroup(); //@cmember Handle Field End Group
  289. EC HandleFieldInstruction(); //@cmember Handle field instruction
  290. EC HandleFieldSymbolFont(BYTE *pch); //@cmember Handle \\f "Facename" in symbol
  291. //@cmember Handle specific SYMBOL field instruction
  292. EC HandleFieldSymbolInstruction(BYTE *pch, BYTE *szSymbol);
  293. EC HandleNumber(); //@cmember Handle _iParam as textToken
  294. EC HandleStartGroup(); //@cmember Handle {
  295. enum { CONTAINS_NONASCII, ALL_ASCII };
  296. //@cmember Insert szText into range
  297. EC HandleText(BYTE *szText, int iAllASCII, LONG cchText = -1);
  298. EC HandleTextToken(STATE *pstate);//@cmember Handle tokenText
  299. EC HandleToken(); //@cmember Grand _token switchboard
  300. void HandleUN(STATE *pstate); //@cmember Handle \uN sequence
  301. BOOL IsLowMergedCell(); //@cmember TRUE iff low merged cell
  302. void Pard(STATE *pstate); //@cmember Set default para props
  303. void SelectCurrentFont(INT iFont);//@cmember Select font <p iFont>
  304. void SetPlain(STATE *pstate); //@cmember Setup _CF for \plain
  305. void DelimitRow(WCHAR *szRowDelimiter); //@cmember Insert start-of-row
  306. void InitializeTableRowParms(); //@cmember Restore table parms to initial state
  307. BOOL StoreDestination(STATE *pstate, LONG dest);//@cmember Store STATE destination if ok
  308. // Object functions
  309. EC HexToByte(BYTE *rgchHex, BYTE *pb);
  310. void FreeRtfObject();
  311. EC StrAlloc(WCHAR ** ppsz, BYTE * sz);
  312. BOOL ObjectReadFromEditStream(void);
  313. BOOL ObjectReadEBookImageInfoFromEditStream(void); //@to get e-book image info
  314. BOOL StaticObjectReadFromEditStream(int cb = 0);
  315. BOOL ObjectReadSiteFlags( REOBJECT * preobj);
  316. void SetBorderParm(WORD& Parm, LONG Value);
  317. BOOL CpgInfoFromFaceName(TEXTFONT *ptf); //@cmember Determines
  318. // charset/cpg based on TEXTFONT::szName
  319. void HandleSTextFlow(int mode); //@cmember Handle S Text Flow
  320. //@access Public Methods
  321. public:
  322. //@cmember RTF reader constructor
  323. CRTFRead(CTxtRange *prg, EDITSTREAM *pes, DWORD dwFlags);
  324. inline ~CRTFRead(); //@cmember CRTFRead destructor
  325. LONG ReadRtf(); //@cmember Main Entry to RTF reader
  326. LONG ReadData(BYTE *pbBuffer, LONG cbBuffer); // todo friend
  327. LONG ReadBinaryData(BYTE *pbBuffer, LONG cbBuffer);
  328. LONG SkipBinaryData(LONG cbSkip);
  329. LONG ReadRawText(char **pszRawText); //@cmember Read in raw text
  330. // Member functions/data to test coverage of RTF reader
  331. #if defined(DEBUG)
  332. public:
  333. void TestParserCoverage();
  334. private:
  335. CHAR *PszKeywordFromToken(TOKEN token);
  336. BOOL FTokIsSymbol(TOKEN tok);
  337. BOOL FTokFailsCoverageTest(TOKEN tok);
  338. BOOL _fTestingParserCoverage;
  339. private:
  340. // member data for RTF tag logging
  341. CRTFLog *_prtflg;
  342. #endif //DEBUG
  343. };
  344. /*
  345. * PointsToFontHeight(cHalfPoints)
  346. *
  347. * @func
  348. * Convert half points to font heights
  349. *
  350. * @parm int |
  351. * sPointSize | Font height in half points
  352. *
  353. * @rdesc
  354. * LONG The corresponding CCharFormat.yHeight value
  355. */
  356. #define PointsToFontHeight(cHalfPoints) (((LONG) cHalfPoints) * 10)
  357. /*
  358. * CRTFRead::~CRTFRead
  359. *
  360. * @mdesc
  361. * Destructor
  362. *
  363. */
  364. inline CRTFRead::~CRTFRead()
  365. {
  366. // TODO: Implement RTF tag logging for the Mac and WinCE
  367. #if defined(DEBUG) && !defined(NOFULLDEBUG)
  368. if(_prtflg)
  369. {
  370. delete _prtflg;
  371. _prtflg = NULL;
  372. }
  373. #endif
  374. }
  375. #endif // __RTFREAD_H