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.

465 lines
23 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. RIPS("MF3216: bParseWin32Metafile, First Record not a Win32 Metafile Header\n") ;
  281. return(FALSE) ;
  282. }
  283. // Record a pointer to the beginning of the Win32 metafile and
  284. // it's length incase we need to emit the Win32 metafile as a comment
  285. // record(s).
  286. pLocalDC->pMf32Bits = (PBYTE) pMf32Header ;
  287. pLocalDC->cMf32Bits = pMf32Header->nBytes ;
  288. // Get the file size for the parser.
  289. nFileSize = pMf32Header->nBytes ;
  290. // Initialize pbCurrent, & pbEnd pointers into the
  291. // metafile bits.
  292. pLocalDC->pbCurrent = pMetafileBits ;
  293. pLocalDC->pbEnd = pLocalDC->pbCurrent + nFileSize ;
  294. // Init the record count.
  295. iRecordCount = 0 ;
  296. // Go through the metafile bits. Handle each record based on
  297. // it's type. bGetNextRecord returns TRUE if pemr contains
  298. // a pointer to a record.
  299. while (bGetNextRecord(pLocalDC, &pemr))
  300. {
  301. iRecordCount++ ;
  302. // Set up a convienent point to the record.
  303. pVoid = (PVOID) pemr ;
  304. // Handle the record based on it's type.
  305. iType = (INT) pemr->iType ;
  306. // Check if the record type falls within the range of the
  307. // call table. Eventually, all the record handlers should
  308. // be in the call table.
  309. if (iType <= EMR_LAST_MF3216_SUPPORTED)
  310. {
  311. bRet = pdofnDrawingOrders[iType](pVoid, pLocalDC) ;
  312. #if DBG
  313. if (bRet == FALSE)
  314. {
  315. iLastError = GetLastError() ;
  316. PUTS1("MF3216: Error on Win32 Metafile record #: %d\n", iRecordCount) ;
  317. PUTS1("\tRecord type: %s\n", pszMfRecords[iType]) ;
  318. PUTS1("\tLast Error Code: %08.8X\n", iLastError) ;
  319. }
  320. #endif
  321. #if 0
  322. if (bRet == FALSE)
  323. break ;
  324. #else
  325. // In ancient times (i.e., before NT4.0), someone explicitly
  326. // removed the code above which exits the loop if the handler
  327. // fails. Possibly this was a compatibility fix in which
  328. // the app depended on the metafile conversion to continue
  329. // even in the event of a failure.
  330. //
  331. // Unfortunately, this fix also allows the parser to continue
  332. // even if the output buffer has run out of space. To
  333. // minimize the change, we will explicitly look for this case
  334. // and break out of the loop if it happens. (Refer to bEmit()
  335. // in emit.c to see where ERROR_BUFFER_OVERFLOW is set).
  336. if (pLocalDC->flags & ( ERR_BUFFER_OVERFLOW | ERR_XORCLIPPATH ) )
  337. break ;
  338. #endif
  339. }
  340. else
  341. {
  342. PUTS1("MF3216: bParseWin32Metafile - record not supported: %d\n", iType) ;
  343. }
  344. }
  345. #if 0
  346. // Display some statictics
  347. if (bRet == TRUE)
  348. {
  349. PUTS1("MF3216: %d Win32 Metafile records processed\n",
  350. iRecordCount) ;
  351. }
  352. #endif
  353. return(bRet) ;
  354. }
  355. /*****************************************************************************
  356. * Get next record
  357. *
  358. * This is a support routine for bParseWin32Metafile.
  359. * It is assumed that pbCurrent, & pbEnd are initialized
  360. * the first time this routine is called.
  361. *
  362. * It returns TRUE if a valid pointer to record is returned in
  363. * pemr. If there are not more records FALSE is returned.
  364. *
  365. * We now need to take into consideration
  366. *****************************************************************************/
  367. BOOL bGetNextRecord(PLOCALDC pLocalDC, PENHMETARECORD *ppemr)
  368. {
  369. DWORD nSize ;
  370. // if we are recreating the objects then go through our list of objects
  371. if (pLocalDC->iXORPass == OBJECTRECREATION)
  372. {
  373. if (pLocalDC->pW16RecreationSlot == NULL)
  374. {
  375. // All our objects are created... Set the next record to be the start
  376. // of the second pass
  377. pLocalDC->pbRecord = pLocalDC->pbChange ;
  378. pLocalDC->pbCurrent = pLocalDC->pbRecord ;
  379. *ppemr = (PENHMETARECORD) pLocalDC->pbCurrent ;
  380. nSize = ((PENHMETARECORD) pLocalDC->pbCurrent)->nSize ;
  381. pLocalDC->pbCurrent += nSize ;
  382. pLocalDC->iXORPass = ERASEXORPASS ;
  383. DoSelectObject(pLocalDC, pLocalDC->lholdp32);
  384. DoSelectObject(pLocalDC, pLocalDC->lholdbr32);
  385. return TRUE ;
  386. }
  387. else
  388. {
  389. PW16RECREATIONSLOT pW16RecreationSlot = pLocalDC->pW16RecreationSlot ;
  390. pLocalDC->pW16RecreationSlot = pW16RecreationSlot->pNext ;
  391. pLocalDC->pbRecord = (PBYTE) pW16RecreationSlot->pbCreatRec ;
  392. pLocalDC->pbCurrent = (PBYTE) pW16RecreationSlot->pbCreatRec ;
  393. *ppemr = (PENHMETARECORD) pLocalDC->pbCurrent ;
  394. nSize = ((PENHMETARECORD) pLocalDC->pbCurrent)->nSize ;
  395. pLocalDC->pbCurrent += nSize ;
  396. LocalFree(pW16RecreationSlot);
  397. return TRUE ;
  398. }
  399. }
  400. // Check for the end of buffer.
  401. // If this is the end return FALSE and set *ppemr to 0.
  402. if (pLocalDC->pbCurrent == pLocalDC->pbEnd)
  403. {
  404. *ppemr = (PENHMETARECORD) NULL ;
  405. pLocalDC->pbRecord = NULL ;
  406. return (FALSE) ;
  407. }
  408. // Well it's not the end of the buffer.
  409. // So, return a pointer to this record, update pbCurrent, and
  410. // return TRUE ;
  411. *ppemr = (PENHMETARECORD) pLocalDC->pbCurrent ;
  412. pLocalDC->pbRecord = pLocalDC->pbCurrent ;
  413. nSize = ((PENHMETARECORD) pLocalDC->pbCurrent)->nSize ;
  414. pLocalDC->pbCurrent += nSize ;
  415. return(TRUE) ;
  416. }