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.

443 lines
22 KiB

  1. /*****************************************************************************
  2. *
  3. * parser.cxx - Parser for the Win32 to Win16 metafile converter.
  4. *
  5. * Date: 8/13/91
  6. * Author: Jeffrey Newman (c-jeffn)
  7. *
  8. * Copyright 1991 Microsoft Corp
  9. *****************************************************************************/
  10. #include "precomp.h"
  11. #pragma hdrstop
  12. #define EMR_LAST_MF3216_SUPPORTED 97
  13. BOOL bGetNextRecord(PLOCALDC pLocalDC, PENHMETARECORD *pemr) ;
  14. // Call table for the translation entry points.
  15. PDOFN pdofnDrawingOrders[] = {
  16. (PDOFN) NULL,
  17. bHandleHeader, // EMR_HEADER 1
  18. bHandlePolyBezier, // EMR_POLYBEZIER 2
  19. bHandlePolygon, // EMR_POLYGON 3
  20. bHandlePolyline, // EMR_POLYLINE 4
  21. bHandlePolyBezierTo, // EMR_POLYBEZIERTO 5
  22. bHandlePolylineTo, // EMR_POLYLINETO 6
  23. bHandlePolyPolyline, // EMR_POLYPOLYLINE 7
  24. bHandlePolyPolygon, // EMR_POLYPOLYGON 8
  25. bHandleSetWindowExt, // EMR_SETWINDOWEXTEX 9
  26. bHandleSetWindowOrg, // EMR_SETWINDOWORGEX 10
  27. bHandleSetViewportExt, // EMR_SETVIEWPORTEXTEX 11
  28. bHandleSetViewportOrg, // EMR_SETVIEWPORTORGEX 12
  29. bHandleNotImplemented, // EMR_SETBRUSHORGEX 13
  30. bHandleEOF, // EMR_EOF 14
  31. bHandleSetPixel, // EMR_SETPIXELV 15
  32. bHandleSetMapperFlags, // EMR_SETMAPPERFLAGS 16
  33. bHandleSetMapMode, // EMR_SETMAPMODE 17
  34. bHandleSetBkMode, // EMR_SETBKMODE 18
  35. bHandleSetPolyFillMode, // EMR_SETPOLYFILLMODE 19
  36. bHandleSetRop2, // EMR_SETROP2 20
  37. bHandleSetStretchBltMode, // EMR_SETSTRETCHBLTMODE 21
  38. bHandleSetTextAlign, // EMR_SETTEXTALIGN 22
  39. bHandleNotImplemented, // EMR_SETCOLORADJUSTMENT 23
  40. bHandleSetTextColor, // EMR_SETTEXTCOLOR 24
  41. bHandleSetBkColor, // EMR_SETBKCOLOR 25
  42. bHandleOffsetClipRgn, // EMR_OFFSETCLIPRGN 26
  43. bHandleMoveTo, // EMR_MOVETOEX 27
  44. bHandleSetMetaRgn, // EMR_SETMETARGN 28
  45. bHandleExcludeClipRect, // EMR_EXCLUDECLIPRECT 29
  46. bHandleIntersectClipRect, // EMR_INTERSECTCLIPRECT 30
  47. bHandleScaleViewportExt, // EMR_SCALEVIEWPORTEXTEX 31
  48. bHandleScaleWindowExt, // EMR_SCALEWINDOWEXTEX 32
  49. bHandleSaveDC, // EMR_SAVEDC 33
  50. bHandleRestoreDC, // EMR_RESTOREDC 34
  51. bHandleSetWorldTransform, // EMR_SETWORLDTRANSFORM 35
  52. bHandleModifyWorldTransform, // EMR_MODIFYWORLDTRANSFORM 36
  53. bHandleSelectObject, // EMR_SELECTOBJECT 37
  54. bHandleCreatePen, // EMR_CREATEPEN 38
  55. bHandleCreateBrushIndirect, // EMR_CREATEBRUSHINDIRECT 39
  56. bHandleDeleteObject, // EMR_DELETEOBJECT 40
  57. bHandleAngleArc, // EMR_ANGLEARC 41
  58. bHandleEllipse, // EMR_ELLIPSE 42
  59. bHandleRectangle, // EMR_RECTANGLE 43
  60. bHandleRoundRect, // EMR_ROUNDRECT 44
  61. bHandleArc, // EMR_ARC 45
  62. bHandleChord, // EMR_CHORD 46
  63. bHandlePie, // EMR_PIE 47
  64. bHandleSelectPalette, // EMR_SELECTPALETTE 48
  65. bHandleCreatePalette, // EMR_CREATEPALETTE 49
  66. bHandleSetPaletteEntries, // EMR_SETPALETTEENTRIES 50
  67. bHandleResizePalette, // EMR_RESIZEPALETTE 51
  68. bHandleRealizePalette, // EMR_REALIZEPALETTE 52
  69. bHandleExtFloodFill, // EMR_EXTFLOODFILL 53
  70. bHandleLineTo, // EMR_LINETO 54
  71. bHandleArcTo, // EMR_ARCTO 55
  72. bHandlePolyDraw, // EMR_POLYDRAW 56
  73. bHandleSetArcDirection, // EMR_SETARCDIRECTION 57
  74. bHandleNotImplemented, // EMR_SETMITERLIMIT 58
  75. bHandleBeginPath, // EMR_BEGINPATH 59
  76. bHandleEndPath, // EMR_ENDPATH 60
  77. bHandleCloseFigure, // EMR_CLOSEFIGURE 61
  78. bHandleFillPath, // EMR_FILLPATH 62
  79. bHandleStrokeAndFillPath, // EMR_STROKEANDFILLPATH 63
  80. bHandleStrokePath, // EMR_STROKEPATH 64
  81. bHandleFlattenPath, // EMR_FLATTENPATH 65
  82. bHandleWidenPath, // EMR_WIDENPATH 66
  83. bHandleSelectClipPath, // EMR_SELECTCLIPPATH 67
  84. bHandleAbortPath, // EMR_ABORTPATH 68
  85. bHandleNotImplemented, // 69
  86. bHandleGdiComment, // EMR_GDICOMMENT 70
  87. bHandleFillRgn, // EMR_FILLRGN 71
  88. bHandleFrameRgn, // EMR_FRAMERGN 72
  89. bHandleInvertRgn, // EMR_INVERTRGN 73
  90. bHandlePaintRgn, // EMR_PAINTRGN 74
  91. bHandleExtSelectClipRgn, // EMR_EXTSELECTCLIPRGN 75
  92. bHandleBitBlt, // EMR_BITBLT 76
  93. bHandleStretchBlt, // EMR_STRETCHBLT 77
  94. bHandleMaskBlt, // EMR_MASKBLT 78
  95. bHandlePlgBlt, // EMR_PLGBLT 79
  96. bHandleSetDIBitsToDevice, // EMR_SETDIBITSTODEVICE 80
  97. bHandleStretchDIBits, // EMR_STRETCHDIBITS 81
  98. bHandleExtCreateFont, // EMR_EXTCREATEFONTINDIRECTW 82
  99. bHandleExtTextOut, // EMR_EXTTEXTOUTA 83
  100. bHandleExtTextOut, // EMR_EXTTEXTOUTW 84
  101. bHandlePoly16, // EMR_POLYBEZIER16 85
  102. bHandlePoly16, // EMR_POLYGON16 86
  103. bHandlePoly16, // EMR_POLYLINE16 87
  104. bHandlePoly16, // EMR_POLYBEZIERTO16 88
  105. bHandlePoly16, // EMR_POLYLINETO16 89
  106. bHandlePolyPoly16, // EMR_POLYPOLYLINE16 90
  107. bHandlePolyPoly16, // EMR_POLYPOLYGON16 91
  108. bHandlePoly16, // EMR_POLYDRAW16 92
  109. bHandleCreateMonoBrush, // EMR_CREATEMONOBRUSH 93
  110. bHandleCreateDIBPatternBrush, // EMR_CREATEDIBPATTERNBRUSHPT 94
  111. bHandleExtCreatePen, // EMR_EXTCREATEPEN 95
  112. bHandlePolyTextOut, // EMR_POLYTEXTOUTA 96
  113. bHandlePolyTextOut, // EMR_POLYTEXTOUTW 97
  114. bHandleNotImplemented, // EMR_SETICMMODE 98
  115. bHandleNotImplemented, // EMR_CREATECOLORSPACE 99
  116. bHandleNotImplemented, // EMR_SETCOLORSPACE 100
  117. bHandleNotImplemented, // EMR_DELETECOLORSPACE 101
  118. bHandleNotImplemented, // EMR_GLSRECORD 102
  119. bHandleNotImplemented, // EMR_GLSBOUNDEDRECORD 103
  120. bHandleNotImplemented, // EMR_PIXELFORMAT 104
  121. bHandleNotImplemented, // 105
  122. bHandleNotImplemented, // 106
  123. bHandleNotImplemented, // 107
  124. bHandleNotImplemented, // 108
  125. bHandleNotImplemented, // 109
  126. bHandleNotImplemented, // 110
  127. bHandleNotImplemented, // EMR_COLORCORRECTPALETTE 111
  128. bHandleNotImplemented, // EMR_ALPHABLEND 112
  129. bHandleNotImplemented, // EMR_ALPHADIBBLEND 113
  130. bHandleNotImplemented, // EMR_TRANSPARENTIMAGE 114
  131. bHandleNotImplemented, // EMR_TRANSPARENTDIBIMAGE 115
  132. bHandleNotImplemented // EMR_GRADIENTFILL 116
  133. } ;
  134. #if DBG
  135. PSZ pszMfRecords[] = {
  136. "NULL RECORD ",
  137. "EMR_HEADER ",
  138. "EMR_POLYBEZIER ",
  139. "EMR_POLYGON ",
  140. "EMR_POLYLINE ",
  141. "EMR_POLYBEZIERTO ",
  142. "EMR_POLYLINETO ",
  143. "EMR_POLYPOLYLINE ",
  144. "EMR_POLYPOLYGON ",
  145. "EMR_SETWINDOWEXTEX ",
  146. "EMR_SETWINDOWORGEX ",
  147. "EMR_SETVIEWPORTEXTEX ",
  148. "EMR_SETVIEWPORTORGEX ",
  149. "EMR_SETBRUSHORGEX ",
  150. "EMR_EOF ",
  151. "EMR_SETPIXELV ",
  152. "EMR_SETMAPPERFLAGS ",
  153. "EMR_SETMAPMODE ",
  154. "EMR_SETBKMODE ",
  155. "EMR_SETPOLYFILLMODE ",
  156. "EMR_SETROP2 ",
  157. "EMR_SETSTRETCHBLTMODE ",
  158. "EMR_SETTEXTALIGN ",
  159. "EMR_SETCOLORADJUSTMENT ",
  160. "EMR_SETTEXTCOLOR ",
  161. "EMR_SETBKCOLOR ",
  162. "EMR_OFFSETCLIPRGN ",
  163. "EMR_MOVETOEX ",
  164. "EMR_SETMETARGN ",
  165. "EMR_EXCLUDECLIPRECT ",
  166. "EMR_INTERSECTCLIPRECT ",
  167. "EMR_SCALEVIEWPORTEXTEX ",
  168. "EMR_SCALEWINDOWEXTEX ",
  169. "EMR_SAVEDC ",
  170. "EMR_RESTOREDC ",
  171. "EMR_SETWORLDTRANSFORM ",
  172. "EMR_MODIFYWORLDTRANSFORM ",
  173. "EMR_SELECTOBJECT ",
  174. "EMR_CREATEPEN ",
  175. "EMR_CREATEBRUSHINDIRECT ",
  176. "EMR_DELETEOBJECT ",
  177. "EMR_ANGLEARC ",
  178. "EMR_ELLIPSE ",
  179. "EMR_RECTANGLE ",
  180. "EMR_ROUNDRECT ",
  181. "EMR_ARC ",
  182. "EMR_CHORD ",
  183. "EMR_PIE ",
  184. "EMR_SELECTPALETTE ",
  185. "EMR_CREATEPALETTE ",
  186. "EMR_SETPALETTEENTRIES ",
  187. "EMR_RESIZEPALETTE ",
  188. "EMR_REALIZEPALETTE ",
  189. "EMR_EXTFLOODFILL ",
  190. "EMR_LINETO ",
  191. "EMR_ARCTO ",
  192. "EMR_POLYDRAW ",
  193. "EMR_SETARCDIRECTION ",
  194. "EMR_SETMITERLIMIT ",
  195. "EMR_BEGINPATH ",
  196. "EMR_ENDPATH ",
  197. "EMR_CLOSEFIGURE ",
  198. "EMR_FILLPATH ",
  199. "EMR_STROKEANDFILLPATH ",
  200. "EMR_STROKEPATH ",
  201. "EMR_FLATTENPATH ",
  202. "EMR_WIDENPATH ",
  203. "EMR_SELECTCLIPPATH ",
  204. "EMR_ABORTPATH ",
  205. "unknown record ",
  206. "EMR_GDICOMMENT ",
  207. "EMR_FILLRGN ",
  208. "EMR_FRAMERGN ",
  209. "EMR_INVERTRGN ",
  210. "EMR_PAINTRGN ",
  211. "EMR_EXTSELECTCLIPRGN ",
  212. "EMR_BITBLT ",
  213. "EMR_STRETCHBLT ",
  214. "EMR_MASKBLT ",
  215. "EMR_PLGBLT ",
  216. "EMR_SETDIBITSTODEVICE ",
  217. "EMR_STRETCHDIBITS ",
  218. "EMR_EXTCREATEFONTINDIRECTW",
  219. "EMR_EXTTEXTOUTA ",
  220. "EMR_EXTTEXTOUTW ",
  221. "EMR_POLYBEZIER16 ",
  222. "EMR_POLYGON16 ",
  223. "EMR_POLYLINE16 ",
  224. "EMR_POLYBEZIERTO16 ",
  225. "EMR_POLYLINETO16 ",
  226. "EMR_POLYPOLYLINE16 ",
  227. "EMR_POLYPOLYGON16 ",
  228. "EMR_POLYDRAW16 ",
  229. "EMR_CREATEMONOBRUSH ",
  230. "EMR_CREATEDIBPATTERNBRUSHP",
  231. "EMR_EXTCREATEPEN ",
  232. "EMR_POLYTEXTOUTA ",
  233. "EMR_POLYTEXTOUTW ",
  234. "EMR_SETICMMODE ",
  235. "EMR_CREATECOLORSPACE ",
  236. "EMR_SETCOLORSPACE ",
  237. "EMR_DELETECOLORSPACE ",
  238. "EMR_GLSRECORD ",
  239. "EMR_GLSBOUNDEDRECORD ",
  240. "EMR_PIXELFORMAT ",
  241. "105 ",
  242. "106 ",
  243. "107 ",
  244. "108 ",
  245. "109 ",
  246. "110 ",
  247. "EMR_COLORCORRECTPALETTE ",
  248. "EMR_ALPHABLEND ",
  249. "EMR_ALPHADIBBLEND ",
  250. "EMR_TRANSPARENTIMAGE ",
  251. "EMR_TRANSPARENTDIBIMAGE ",
  252. "EMR_GRADIENTFILL "
  253. };
  254. #endif
  255. /*****************************************************************************
  256. * Parse the Win32 metafile.
  257. *
  258. * The Win32 metafile is represented by the metafile bits pointed to
  259. * by pMetafileBits. The metafile bits may be obtained from a memory mapped
  260. * file, or from some shared memory (from the clipboard).
  261. *****************************************************************************/
  262. BOOL bParseWin32Metafile(PBYTE pMetafileBits, PLOCALDC pLocalDC)
  263. {
  264. INT iType ;
  265. PVOID pVoid ;
  266. PENHMETARECORD pemr ;
  267. PENHMETAHEADER pMf32Header ;
  268. DWORD nFileSize ;
  269. BOOL bRet ;
  270. INT iRecordCount,
  271. iLastError ;
  272. bRet = TRUE ;
  273. // Get the file length from the header.
  274. // Test to make sure the first record is a Win32 Metafile header.
  275. pMf32Header = (PENHMETAHEADER) pMetafileBits ;
  276. if ( (pMf32Header->iType != EMR_HEADER)
  277. || (pMf32Header->dSignature != ENHMETA_SIGNATURE)
  278. )
  279. {
  280. RIP("MF3216: bParseWin32Metafile, First Record not a Win32 Metafile Header\n") ;
  281. return(FALSE) ;
  282. }
  283. if (!IsValidEnhMetaRecord(pLocalDC->pht, pMf32Header))
  284. {
  285. EMFVALFAIL(("MF3216: bParseWin32Metafile, IsValidEnhMetaRecord failed\n"));
  286. return(FALSE);
  287. }
  288. // Record a pointer to the beginning of the Win32 metafile and
  289. // it's length incase we need to emit the Win32 metafile as a comment
  290. // record(s).
  291. pLocalDC->pMf32Bits = (PBYTE) pMf32Header ;
  292. pLocalDC->cMf32Bits = pMf32Header->nBytes ;
  293. // Get the file size for the parser.
  294. nFileSize = pMf32Header->nBytes ;
  295. // Initialize pbCurrent, & pbEnd pointers into the
  296. // metafile bits.
  297. pLocalDC->pbCurrent = pMetafileBits ;
  298. pLocalDC->pbEnd = pLocalDC->pbCurrent + nFileSize ;
  299. if (pLocalDC->pbEnd < pLocalDC->pbCurrent)
  300. {
  301. RIP("MF3216: bParseWin32Metafile, pointer arithmetic overflow\n");
  302. return(FALSE);
  303. }
  304. // Init the record count.
  305. iRecordCount = 0 ;
  306. // Go through the metafile bits. Handle each record based on
  307. // it's type. bGetNextRecord returns TRUE if pemr contains
  308. // a pointer to a record.
  309. while (bGetNextRecord(pLocalDC, &pemr))
  310. {
  311. if (!IsValidEnhMetaRecord(pLocalDC->pht, pemr))
  312. {
  313. EMFVALFAIL(("MF3216: bParseWin32Metafile, IsValidEnhMetaRecord failed\n"));
  314. bRet = FALSE;
  315. break;
  316. }
  317. iRecordCount++ ;
  318. // Set up a convienent point to the record.
  319. pVoid = (PVOID) pemr ;
  320. // Handle the record based on it's type.
  321. iType = (INT) pemr->iType ;
  322. // Check if the record type falls within the range of the
  323. // call table. Eventually, all the record handlers should
  324. // be in the call table.
  325. if (iType <= EMR_LAST_MF3216_SUPPORTED)
  326. {
  327. bRet = pdofnDrawingOrders[iType](pVoid, pLocalDC) ;
  328. #if DBG
  329. if (bRet == FALSE)
  330. {
  331. iLastError = GetLastError() ;
  332. PUTS1("MF3216: Error on Win32 Metafile record #: %d\n", iRecordCount) ;
  333. PUTS1("\tRecord type: %s\n", pszMfRecords[iType]) ;
  334. PUTS1("\tLast Error Code: %08.8X\n", iLastError) ;
  335. }
  336. #endif
  337. #if 0
  338. if (bRet == FALSE)
  339. break ;
  340. #else
  341. // In ancient times (i.e., before NT4.0), someone explicitly
  342. // removed the code above which exits the loop if the handler
  343. // fails. Possibly this was a compatibility fix in which
  344. // the app depended on the metafile conversion to continue
  345. // even in the event of a failure.
  346. //
  347. // Unfortunately, this fix also allows the parser to continue
  348. // even if the output buffer has run out of space. To
  349. // minimize the change, we will explicitly look for this case
  350. // and break out of the loop if it happens. (Refer to bEmit()
  351. // in emit.c to see where ERROR_BUFFER_OVERFLOW is set).
  352. if (pLocalDC->flags & ERR_BUFFER_OVERFLOW)
  353. break ;
  354. #endif
  355. }
  356. else
  357. {
  358. PUTS1("MF3216: bParseWin32Metafile - record not supported: %d\n", iType) ;
  359. }
  360. }
  361. #if 0
  362. // Display some statictics
  363. if (bRet == TRUE)
  364. {
  365. PUTS1("MF3216: %d Win32 Metafile records processed\n",
  366. iRecordCount) ;
  367. }
  368. #endif
  369. return(bRet) ;
  370. }
  371. /*****************************************************************************
  372. * Get next record
  373. *
  374. * This is a support routine for bParseWin32Metafile.
  375. * It is assumed that pbCurrent, & pbEnd are initialized
  376. * the first time this routine is called.
  377. *
  378. * It returns TRUE if a valid pointer to record is returned in
  379. * pemr. If there are not more records FALSE is returned.
  380. *****************************************************************************/
  381. BOOL bGetNextRecord(PLOCALDC pLocalDC, PENHMETARECORD *ppemr)
  382. {
  383. DWORD nSize ;
  384. // Check for the end of buffer.
  385. // If this is the end return FALSE and set *ppemr to 0.
  386. if (pLocalDC->pbCurrent == pLocalDC->pbEnd)
  387. {
  388. *ppemr = (PENHMETARECORD) NULL ;
  389. return (FALSE) ;
  390. }
  391. // Well it's not the end of the buffer.
  392. // So, return a pointer to this record, update pbCurrent, and
  393. // return TRUE ;
  394. *ppemr = (PENHMETARECORD) pLocalDC->pbCurrent ;
  395. nSize = ((PENHMETARECORD) pLocalDC->pbCurrent)->nSize ;
  396. pLocalDC->pbCurrent += nSize ;
  397. return(TRUE) ;
  398. }