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. * All sz's in the RTF*.? files refer to a LPSTRs, not LPTSTRs, unless
  16. * noted as a szUnicode.
  17. *
  18. * Copyright (c) 1995-1997, Microsoft Corporation. All rights reserved.
  19. */
  20. #ifndef __RTFCONV_H
  21. #define __RTFCONV_H
  22. #include "_edit.h"
  23. #include "_array.h"
  24. #include "_range.h"
  25. #include "_rtext.h"
  26. #include "tokens.h"
  27. extern const KEYWORD rgKeyword[];
  28. // Convert between Twips and Himetric
  29. // Ratio is 1440 twips/in, 2540 him/in, therefore 1440/2540 = 72/127 him/twips
  30. // Use muldiv() to include rounding and 64-bit intermediate result
  31. #define TwipsFromHimetric(_hm) MulDiv(_hm, 72, 127)
  32. #define HimetricFromTwips(_tw) MulDiv(_tw, 127, 72)
  33. #define LBRACE TEXT('{')
  34. #define BSLASH TEXT('\\')
  35. #define RBRACE TEXT('}')
  36. #define ZERO TEXT('0')
  37. // Character property bits like an ASCII-only ANSI C LC_CTYPE types
  38. #define fUC 0x01 // A-Z
  39. #define fLC 0x02 // a-z
  40. #define fDG 0x04 // 0-9
  41. #define fSP 0x08 // Space chars
  42. #define fPN 0x10 // Punctuation chars
  43. #define fCT 0x20 // Control chars
  44. #define fBL 0x40 // Blank chars
  45. #define fHX 0x80 // 0-9, a-f, or A-F
  46. #define fAlpha (fUC + fLC)
  47. #define fAlphaNum (fAlpha + fDG)
  48. extern const BYTE rgbCharClass[256];
  49. #define Classify(_ch) (rgbCharClass[_ch])
  50. #define IsLC(_ch) ((Classify(_ch) & fLC))
  51. #define IsAlpha(_ch) ((Classify(_ch) & fAlpha))
  52. #define IsDigit(_ch) ((Classify(_ch) & fDG))
  53. #define IsXDigit(_ch) ((Classify(_ch) & fHX))
  54. #define IsAlphaNum(_ch) ((Classify(_ch) & fAlphaNum))
  55. #define IsAlphaNumBlank(_ch) ((Classify(_ch) & (fAlphaNum + fBL)))
  56. template <class T> unsigned int DiffPtrs(T *pA, T *pB)
  57. {
  58. return pA - pB;
  59. }
  60. //#define DiffPtrs(_pA, _pB, _type) ((UINT) (((_type *) (_pA)) - ((_type *) (_pB))))
  61. extern INT cKeywords;
  62. extern const COLORREF g_Colors[];
  63. extern const char szEndGroupCRLF[];
  64. #define szaCRLF (BYTE *)&szEndGroupCRLF[1]
  65. /*
  66. * Converter Error Codes
  67. */
  68. enum
  69. {
  70. ecNoError = 0, // Success
  71. ecCantUnicode,
  72. ecColorTableOverflow,
  73. ecExpectingRtfKeyword,
  74. ecExpectingStartGroup,
  75. ecFontTableOverflow,
  76. ecGeneralFailure,
  77. ecKeywordTooLong,
  78. ecLexInitFailed,
  79. ecNoMemory,
  80. ecParserBusy,
  81. ecPutCharFailed,
  82. ecStackOverflow,
  83. ecStackUnderflow,
  84. ecUnexpectedChar,
  85. ecUnexpectedEOF,
  86. ecUnexpectedToken,
  87. ecUnGetCharFailed,
  88. ecTextMax,
  89. ecStreamOutObj,
  90. ecStreamInObj,
  91. ecTruncateAtCRLF,
  92. ecFormatCache,
  93. ecLastError // Total error messages
  94. };
  95. typedef INT EC;
  96. /*
  97. * @struct RTFOBJECT |
  98. * Object data transfer structure
  99. */
  100. typedef struct _rtfobject
  101. {
  102. SHORT sType; // @field object type (ROT_*)
  103. SHORT sPictureType; // @field specific type of sPicture
  104. SHORT cBitsPerPixel; // @field # bits per pixel, if bitmap
  105. SHORT cColorPlanes; // @field # color planes, if bitmap
  106. SHORT cBytesPerLine; // @field # bytes per raster line, if bitmap
  107. BOOL fSetSize; // @field Let client tell server the size
  108. LONG xExt, yExt; // @field dimensions in pixels for pictures, twips for
  109. // for objects
  110. LONG xScale, yScale; // @field scaling percentage along axes
  111. SHORT xExtGoal, yExtGoal; // @field desired dimensions in twips for pictures
  112. RECT rectCrop; // @field cropping information in twips
  113. TCHAR * szClass; // @field object class
  114. TCHAR * szName; // @field object name
  115. // On RTF generation
  116. LONG xExtPict, yExtPict; // @field metafile dimensions
  117. LPBYTE pbResult; // metafile depiction of the object
  118. ULONG cbResult;
  119. } RTFOBJECT;
  120. /*
  121. * @enum ROTYPE | The values for OBJECT.sType
  122. *
  123. * Keep this in sync with rgszROT in rtfwrit.c
  124. */
  125. enum ROTYPE
  126. {
  127. ROT_Bitmap, // @emem Bitmap
  128. ROT_Metafile, // @emem Metafile
  129. ROT_DIB, // @emem Device-Independent Bitmap
  130. ROT_Embedded, // @emem Embedded Object
  131. ROT_Link, // @emem Linked Object
  132. ROT_AutoLink, // @emem Autolink
  133. ROT_MacEdition // @emem Mac object
  134. };
  135. /*
  136. * DEFINE's
  137. */
  138. #define cachBufferMost 4096
  139. #define cachTextMax ( 512 + 1 )
  140. #define cachKeywordMax ( 32 + 1 )
  141. #define cachParamMax ( 11 + 1 )
  142. #define cFooChunk 8
  143. // Characters to give to RichEdit
  144. #define chNonBreakingSpace 160
  145. #define chOptionalHyphen 173
  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 bCharSet; // @field Font character set
  175. BYTE bPitchAndFamily; // @field Font family
  176. SHORT iFont; // @field Font name index
  177. TCHAR 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. TCHAR *szNormalName;
  197. TCHAR *szTaggedName;
  198. BYTE bCharSet;
  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 _bCharSet; // @cmember Converter char set (ANSI, UTF7, UTF8)
  224. static TFI *_rgtfi; // @cmember Pointer to the first font substitute record
  225. static INT _ctfi; // @cmember Number of the font substitute records
  226. static TCHAR *_pchFontSubInfo; // @cmember Font strings for substitutions
  227. //@access Protected Functions
  228. void ReadFontSubInfo(void);
  229. enum PARSEFONTNAME { PFN_SUCCESS, PFN_FAIL, PFN_EOF };
  230. PARSEFONTNAME ParseFontName(TCHAR *pchBuf,
  231. TCHAR *pchBufMax,
  232. TCHAR chDelimiter,
  233. TCHAR **pszName,
  234. BYTE &bCharSet,
  235. BOOL &fSetCharSet,
  236. TCHAR **ppchBufNew) const;
  237. BOOL FontSubstitute(TCHAR *szTaggedName,
  238. TCHAR *szNormalName,
  239. BYTE *pbCharSet);
  240. BOOL FindTaggedFont(const TCHAR *szNormalName, BYTE bCharSet, TCHAR **ppchTaggedName);
  241. // @cmember Find font name with additional special tag
  242. // corresponding to szNormalName & bCharSet
  243. BOOL IsTaggedFont(const TCHAR *szName, BYTE *pbCharSet, TCHAR **ppchNormalName);
  244. // @cmember Figure out is szName font name with additional tag
  245. // corresponding to pbCharSet
  246. //@access Public Functions
  247. public:
  248. CRTFConverter(CTxtRange *prg, EDITSTREAM *pes, DWORD dwFlags, BOOL fRead);
  249. inline ~CRTFConverter();
  250. static void FreeFontSubInfo();
  251. protected:
  252. #if defined(DEBUG) && !defined(MACPORT)
  253. // for capturing RTF as its read from or written to a file
  254. HANDLE _hfileCapture;
  255. #endif
  256. };
  257. #define fRTFNoObjs 1
  258. #define fRTFFE 8 // Check this
  259. #define IsUTF8 ((_dwFlags & (0xFFFF0000 | SF_USECODEPAGE)) \
  260. == ((CP_UTF8 << 16) | SF_USECODEPAGE))
  261. /*
  262. * CRTFConverter::CRTFConverter()
  263. *
  264. * @mfunc
  265. * RTF Converter constructor
  266. */
  267. inline CRTFConverter::~CRTFConverter()
  268. {
  269. #if defined(DEBUG) && !defined(MACPORT)
  270. if(_hfileCapture)
  271. {
  272. CloseHandle(_hfileCapture);
  273. _hfileCapture = NULL;
  274. }
  275. #endif
  276. }
  277. #endif // __RTFCONV_H