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.

323 lines
8.6 KiB

  1. /*
  2. * @doc INTERNAL
  3. *
  4. * @module _RTFCONV.H -- RichEdit RTF Converter Base Class Definition |
  5. *
  6. * Description:
  7. * This file contains the type declarations used by both the RTF reader
  8. * and writer for the RICHEDIT control
  9. *
  10. * Authors: <nl>
  11. * Original RichEdit 1.0 RTF converter: Anthony Francisco <nl>
  12. * Conversion to C++ and RichEdit 2.0: Murray Sargent
  13. *
  14. * @devnote
  15. * sz's in the RTF*.? files usually refer to LPSTRs, not LPWSTRs
  16. *
  17. * Copyright (c) 1995-2002, Microsoft Corporation. All rights reserved.
  18. */
  19. #ifndef __RTFCONV_H
  20. #define __RTFCONV_H
  21. #include "_edit.h"
  22. #include "_array.h"
  23. #include "_range.h"
  24. #include "_rtext.h"
  25. #include "tokens.h"
  26. extern const KEYWORD rgKeyword[];
  27. extern const KEYWORD rgShapeKeyword[];
  28. #define LBRACE TEXT('{')
  29. #define BSLASH TEXT('\\')
  30. #define RBRACE TEXT('}')
  31. #define ZERO TEXT('0')
  32. // Character property bits like an ASCII-only ANSI C LC_CTYPE types
  33. #define fUC 0x01 // A-Z
  34. #define fLC 0x02 // a-z
  35. #define fDG 0x04 // 0-9
  36. #define fSP 0x08 // Space chars
  37. #define fPN 0x10 // Punctuation chars
  38. #define fCT 0x20 // Control chars
  39. #define fBL 0x40 // Blank chars
  40. #define fHX 0x80 // 0-9, a-f, or A-F
  41. #define fAlpha (fUC + fLC)
  42. #define fAlphaNum (fAlpha + fDG)
  43. extern const BYTE rgbCharClass[256];
  44. #define Classify(_ch) (rgbCharClass[_ch])
  45. #define IsLC(_ch) ((Classify(_ch) & fLC))
  46. #define IsASCIIAlpha(_ch) ((Classify(_ch) & fAlpha))
  47. #define IsDigit(_ch) ((Classify(_ch) & fDG))
  48. #define IsXDigit(_ch) ((Classify(_ch) & fHX))
  49. #define IsAlphaNum(_ch) ((Classify(_ch) & fAlphaNum))
  50. #define IsAlphaNumBlank(_ch) ((Classify(_ch) & (fAlphaNum + fBL)))
  51. template <class T> unsigned int DiffPtrs(T *pA, T *pB)
  52. {
  53. return pA - pB;
  54. }
  55. //#define DiffPtrs(_pA, _pB, _type) ((UINT) (((_type *) (_pA)) - ((_type *) (_pB))))
  56. extern INT cKeywords;
  57. extern INT cShapeKeywords;
  58. extern const COLORREF g_Colors[];
  59. extern const char szEndGroupCRLF[];
  60. #define szaCRLF (BYTE *)&szEndGroupCRLF[1]
  61. /*
  62. * Converter Error Codes
  63. */
  64. enum
  65. {
  66. ecNoError = 0, // Success
  67. ecCantUnicode,
  68. ecColorTableOverflow,
  69. ecExpectingRtfKeyword,
  70. ecExpectingStartGroup,
  71. ecFontTableOverflow,
  72. ecGeneralFailure,
  73. ecKeywordTooLong,
  74. ecLexInitFailed,
  75. ecNoMemory,
  76. ecParserBusy,
  77. ecPutCharFailed,
  78. ecStackOverflow,
  79. ecStackUnderflow,
  80. ecUnexpectedChar,
  81. ecUnexpectedEOF,
  82. ecUnexpectedToken,
  83. ecUnGetCharFailed,
  84. ecTextMax,
  85. ecStreamOutObj,
  86. ecStreamInObj,
  87. ecTruncateAtCRLF,
  88. ecFormatCache,
  89. ecUTF8NotUsed,
  90. ecAbort,
  91. ecLastError // Total error messages
  92. };
  93. typedef INT EC;
  94. /*
  95. * @struct RTFOBJECT |
  96. * Object data transfer structure
  97. */
  98. typedef struct _rtfobject
  99. {
  100. SHORT sType; // @field object type (ROT_*)
  101. SHORT sPictureType; // @field specific type of sPicture
  102. SHORT cBitsPerPixel; // @field # bits per pixel, if bitmap
  103. SHORT cColorPlanes; // @field # color planes, if bitmap
  104. SHORT cBytesPerLine; // @field # bytes per raster line, if bitmap
  105. BOOL fSetSize; // @field Let client tell server the size
  106. LONG xExt, yExt; // @field dimensions in pixels for pictures, twips for
  107. // for objects
  108. LONG xScale, yScale; // @field scaling percentage along axes
  109. SHORT xExtGoal, yExtGoal; // @field desired dimensions in twips for pictures
  110. RECT rectCrop; // @field cropping information in twips
  111. WCHAR * szClass; // @field object class
  112. WCHAR * szName; // @field object name
  113. // On RTF generation
  114. LONG xExtPict, yExtPict; // @field metafile dimensions
  115. LPBYTE pbResult; // metafile depiction of the object
  116. ULONG cbResult;
  117. } RTFOBJECT;
  118. /*
  119. * @enum ROTYPE | The values for OBJECT.sType
  120. *
  121. * Keep this in sync with rgszROT in rtfwrit.cpp
  122. */
  123. enum ROTYPE
  124. {
  125. ROT_Bitmap, // @emem Bitmap
  126. ROT_Metafile, // @emem Metafile
  127. ROT_DIB, // @emem Device-Independent Bitmap
  128. ROT_JPEG, // @emem JPEG blip
  129. ROT_PNG, // @emem PNG blip
  130. ROT_Embedded, // @emem Embedded Object
  131. ROT_Link, // @emem Linked Object
  132. ROT_AutoLink, // @emem Autolink
  133. ROT_MacEdition, // @emem Mac object
  134. ROT_EBookImage, // @emem Ebook Image object
  135. };
  136. /*
  137. * DEFINE's
  138. */
  139. #define cachBufferMost 4096
  140. #define cachTextMax ( 512 + 1 )
  141. #define cachKeywordMax ( 32 + 1 )
  142. #define cachParamMax ( 11 + 1 )
  143. #define cFooChunk 8
  144. #define MAXTABLENEST 16
  145. // Characters to give to RichEdit
  146. #if ( cachTextMax - 1 ) % 2 == 1
  147. #error "cachTextMax - 1 MUST be even"
  148. #endif
  149. #if ( cachParamMax - 1 ) < 11
  150. #error "cachParamMax MUST be >= 11"
  151. #endif
  152. /*
  153. * Some RTF defaults
  154. */
  155. #ifdef NEVER
  156. // we don't care about margins, just indents
  157. #define dxDefaultLeftMargin 1800
  158. #define dxDefaultRightMargin 1800
  159. #else
  160. #define dxDefaultLeftMargin 0
  161. #define dxDefaultRightMargin 0
  162. #endif
  163. // next two in half points
  164. #define yDefaultFontSize ( 12 * 2 )
  165. #define dyDefaultSuperscript 6
  166. #define RESERVED_FONT_HANDLES 0x800
  167. /*
  168. * @struct TEXTFONT |
  169. * text font structure
  170. */
  171. typedef struct _textfont
  172. {
  173. SHORT sHandle; // @field RTF input font handle
  174. BYTE iCharRep; // @field Font character repertoire
  175. BYTE bPitchAndFamily; // @field Font family
  176. SHORT iFont; // @field Font name index
  177. WCHAR szName[LF_FACESIZE+1]; // @field Font name
  178. SHORT sCodePage; // @field Code page for font
  179. // (INVALID_CODEPAGE == not set)
  180. BYTE fNameIsDBCS; // @field Indicates if szName is DBCS stuffed into Unicode buffer
  181. BYTE fCpgFromSystem; // @field Indicates is cpg was
  182. // retrieved from system based
  183. // on font name.
  184. } TEXTFONT;
  185. /*
  186. * Global variables for the scope of the entire parser/reader
  187. */
  188. #ifdef DEBUG
  189. extern CHAR * rgszParseError[];
  190. extern CHAR * szDest[];
  191. #endif
  192. #define cchMaxNumText 16
  193. // tagged font info
  194. typedef struct _tfi
  195. {
  196. WCHAR *szNormalName;
  197. WCHAR *szTaggedName;
  198. BYTE iCharRep;
  199. } TFI;
  200. typedef CArray<TEXTFONT> TEXTFONTS;
  201. typedef CArray<COLORREF> COLORREFS;
  202. const short INVALID_CODEPAGE = -1;
  203. const short INVALID_LANGUAGE = -1;
  204. // default value for \ucN tag
  205. const int iUnicodeCChDefault = 1;
  206. /*
  207. * CRTFConverter
  208. *
  209. * @class RTF converter base class used by CRTFRead and CRTFWrite
  210. */
  211. class CRTFConverter
  212. {
  213. //@access Protected Data Members
  214. protected:
  215. TEXTFONTS _fonts; // @cmember Font table
  216. COLORREFS _colors; // @cmember Color table
  217. EC _ecParseError; // @cmember Error code
  218. CTxtEdit * _ped; // @cmember CTxtEdit
  219. CTxtRange * _prg; // @cmember CTxtRange to replace/write from
  220. EDITSTREAM *_pes; // @cmember EDITSTREAM to use
  221. DWORD _dwFlags; // @cmember See #defines below
  222. CCharFormat _CF; // @cmember Character formatting info
  223. BYTE _iCharRep; // @cmember Converter char set (ANSI, UTF7, UTF8)
  224. char _bTableLevel; // @cmember Table level (0 for no table)
  225. char _bTableLevelIP; // @cmember Table level at insertion point
  226. static TFI *_rgtfi; // @cmember Pointer to the first font substitute record
  227. static INT _ctfi; // @cmember Number of the font substitute records
  228. static WCHAR *_pchFontSubInfo; // @cmember Font strings for substitutions
  229. //@access Protected Functions
  230. void ReadFontSubInfo(void);
  231. enum PARSEFONTNAME { PFN_SUCCESS, PFN_FAIL, PFN_EOF };
  232. PARSEFONTNAME ParseFontName(WCHAR *pchBuf,
  233. WCHAR *pchBufMax,
  234. WCHAR chDelimiter,
  235. WCHAR **pszName,
  236. BYTE &iCharRep,
  237. BOOL &fSetCharSet,
  238. WCHAR **ppchBufNew) const;
  239. BOOL FontSubstitute(WCHAR *szTaggedName,
  240. WCHAR *szNormalName,
  241. BYTE *piCharRep);
  242. BOOL FindTaggedFont(const WCHAR *szNormalName, BYTE iCharRep, WCHAR **ppchTaggedName);
  243. // @cmember Find font name with additional special tag
  244. // corresponding to szNormalName & iCharRep
  245. BOOL IsTaggedFont(const WCHAR *szName, BYTE *piCharRep, WCHAR **ppchNormalName);
  246. // @cmember Figure out is szName font name with additional tag
  247. // corresponding to piCharRep
  248. //@access Public Functions
  249. public:
  250. CRTFConverter(CTxtRange *prg, EDITSTREAM *pes, DWORD dwFlags, BOOL fRead);
  251. inline ~CRTFConverter();
  252. static void FreeFontSubInfo();
  253. protected:
  254. #if defined(DEBUG)
  255. // for capturing RTF as its read from or written to a file
  256. HANDLE _hfileCapture;
  257. #endif
  258. };
  259. #define fRTFNoObjs 1
  260. #define fRTFFE 8 // Check this
  261. #define IsUTF8 ((_dwFlags & (0xFFFF0000 | SF_USECODEPAGE)) \
  262. == ((CP_UTF8 << 16) | SF_USECODEPAGE))
  263. /*
  264. * CRTFConverter::~CRTFConverter()
  265. *
  266. * @mfunc
  267. * RTF Converter destructor
  268. */
  269. inline CRTFConverter::~CRTFConverter()
  270. {
  271. #if defined(DEBUG)
  272. if(_hfileCapture)
  273. {
  274. CloseHandle(_hfileCapture);
  275. _hfileCapture = NULL;
  276. }
  277. #endif
  278. }
  279. #endif // __RTFCONV_H