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.

2884 lines
87 KiB

  1. /**************************************************************************\
  2. *
  3. * Copyright (c) 1998-1999 Microsoft Corporation
  4. *
  5. * Abstract:
  6. *
  7. * Handle all the permutations of the creation and deletion of the
  8. * GpGraphics class.
  9. *
  10. * Revision History:
  11. *
  12. * 12/03/1998 andrewgo
  13. * Created it.
  14. *
  15. \**************************************************************************/
  16. #include "precomp.hpp"
  17. #include "..\Render\HtTables.hpp"
  18. #include "printer.hpp"
  19. #include "winspool.h"
  20. #include "winbase.h"
  21. /**************************************************************************\
  22. *
  23. * Function Description:
  24. *
  25. * Updates the draw bounds of the graphics. Resets the clipping.
  26. *
  27. * Arguments:
  28. *
  29. * [IN] x, y, width, height - Specifies the client drawable boundaries
  30. *
  31. * History:
  32. *
  33. * 03/30/2000 agodfrey
  34. * Created it.
  35. *
  36. \**************************************************************************/
  37. VOID
  38. GpGraphics::UpdateDrawBounds(
  39. INT x,
  40. INT y,
  41. INT width,
  42. INT height
  43. )
  44. {
  45. DpContext *context = Context;
  46. // Set up the surface bounds and the clip regions:
  47. SurfaceBounds.X = x;
  48. SurfaceBounds.Y = y;
  49. SurfaceBounds.Width = width;
  50. SurfaceBounds.Height = height;
  51. WindowClip.Set(x, y, width, height);
  52. context->VisibleClip.Set(x, y, width, height);
  53. // ContainerClip always contains the clipping for the container,
  54. // intersected with the WindowClip. Currently, the container is
  55. // infinite, so just set it to the WindowClip.
  56. context->ContainerClip.Set(x, y, width, height);
  57. context->AppClip.SetInfinite();
  58. }
  59. /**************************************************************************\
  60. *
  61. * Function Description:
  62. *
  63. * Resets the graphics state to its defaults.
  64. *
  65. * Arguments:
  66. *
  67. * [IN] x, y, width, height - Specifies the client drawable boundaries
  68. *
  69. * History:
  70. *
  71. * 12/04/1998 andrewgo
  72. * Created it.
  73. *
  74. \**************************************************************************/
  75. VOID
  76. GpGraphics::ResetState(
  77. INT x,
  78. INT y,
  79. INT width,
  80. INT height
  81. )
  82. {
  83. DpContext *context = Context;
  84. context->CompositingMode = CompositingModeSourceOver;
  85. context->CompositingQuality = CompositingQualityDefault;
  86. context->AntiAliasMode = FALSE;
  87. context->TextRenderHint = TextRenderingHintSystemDefault;
  88. context->TextContrast = DEFAULT_TEXT_CONTRAST;
  89. context->FilterType = InterpolationModeDefaultInternal;
  90. context->PixelOffset = PixelOffsetModeDefault;
  91. context->InverseOk = FALSE;
  92. context->WorldToPage.Reset();
  93. context->ContainerToDevice.Reset();
  94. this->SetPageTransform(UnitDisplay, 1.0f); // updates the matrix
  95. UpdateDrawBounds(x, y, width, height);
  96. }
  97. /**************************************************************************\
  98. *
  99. * Function Description:
  100. *
  101. * Get the drawing bounds of a DC. Only intended for use by
  102. * GpGraphics::GpGraphics(HWND, HDC).
  103. *
  104. * Arguments:
  105. *
  106. * [IN] hdc - Specifies the DC
  107. * [OUT] rect - The returned client rectangle.
  108. *
  109. * Return Value:
  110. *
  111. * Status code
  112. *
  113. * Notes:
  114. *
  115. * See bug #93012. We used to just call GetClipBox, convert to device
  116. * coordinates, then boost the rectangle by one pixel on each side to cover
  117. * rounding error. But this was causing AV's - we really do need the exact
  118. * client rectangle.
  119. *
  120. * But we need good perf in common cases. So we do a two-step process
  121. * - check if the transform is such that there won't be rounding error
  122. * (and simply use GetClipBox if so).
  123. * Otherwise, save the DC, reset the transform, and then query.
  124. *
  125. * I tried an alternative - calling LPtoDP on 3 points to infer the transform
  126. * (as we do in InheritAppClippingAndTransform).
  127. * But because of rounding, and made worse by
  128. * NT bug #133322 (in the old NT RAID), it's nearly impossible
  129. * to infer the transform unambiguously. The particularly bad case is
  130. * when the world-to-device transform is a shrink, but the scale factor
  131. * is very close to 1. We'd decide it was a one-to-one transform, but it
  132. * would be susceptible to bug #133322.
  133. *
  134. * So, to round off this novel: We're using a much simpler approach,
  135. * which restricts the cases in which we can use the fast method, but
  136. * should be ok.
  137. *
  138. * Notes:
  139. *
  140. * This should really be a member of DpContext (bug #98174).
  141. *
  142. * History:
  143. *
  144. * 03/28/2000 agodfrey
  145. * Created it.
  146. *
  147. \**************************************************************************/
  148. #if 0 // not used
  149. GpStatus
  150. GpGraphics::GetDCDrawBounds(
  151. HDC hdc,
  152. RECT *rect
  153. )
  154. {
  155. BOOL hackResetClipping = FALSE;
  156. // Check if the transform is translation-only. If it is, we can avoid the
  157. // expense of cleaning the DC. GetGraphicsMode and GetMapMode are both
  158. // handled in user mode on NT.
  159. if ( (GetGraphicsMode(hdc) != GM_COMPATIBLE)
  160. || (GetMapMode(hdc) != MM_TEXT))
  161. {
  162. // Clean the DC, to set the transform back to translation-only.
  163. ASSERT(Context->SaveDc == 0);
  164. Context->SaveDc = ::SaveDC(hdc);
  165. if (!Context->SaveDc)
  166. {
  167. return GenericError;
  168. }
  169. // CleanTheHdc shouldn't be resetting the clipping, but it does,
  170. // which messes up GetClipBox below.
  171. // So until bug #99338 is resolved, we must work around it.
  172. hackResetClipping = TRUE;
  173. Context->CleanTheHdc(hdc, FALSE);
  174. }
  175. // The code above is necessary because GetClipBox returns
  176. // logical coordinates, but we want device coordinates.
  177. // By this point, we've made sure that the transform is translation-only.
  178. if (GetClipBox(hdc, rect) == ERROR)
  179. {
  180. return GenericError;
  181. }
  182. // See bug #99338. We must reset the clipping, because that's what
  183. // CleanTheHdc normally does, and apparently some of our code relies on it.
  184. // If #99338 is resolved as suggested, this should go away.
  185. if (hackResetClipping)
  186. {
  187. SelectClipRgn(hdc, NULL);
  188. }
  189. #if DBG
  190. // Save the world-coordinate rectangle.
  191. RECT checkRect = *rect;
  192. #endif
  193. // Convert to device coordinates.
  194. if (!LPtoDP(hdc, reinterpret_cast<POINT *>(rect), 2))
  195. {
  196. return GenericError;
  197. }
  198. // NT can sometimes return poorly-ordered rectangles,
  199. // but I don't think this applies to translation-only tranforms.
  200. ASSERT( (rect->left <= rect->right)
  201. && (rect->top <= rect->bottom));
  202. // Verify that the transform was translation-only.
  203. // Note that this sanity check could fail to catch some transforms
  204. // which are 'almost' translation-only. But ask me if I care.
  205. ASSERT( ( (rect->right - rect->left)
  206. == (checkRect.right - checkRect.left))
  207. && ( (rect->bottom - rect->top)
  208. == (checkRect.bottom - checkRect.top)));
  209. return Ok;
  210. }
  211. #endif
  212. /**************************************************************************\
  213. *
  214. * Function Description:
  215. *
  216. * Create a GpGraphics class from a window handle.
  217. *
  218. * The advantage of this call over that of GetFromHdc is that
  219. * it can avoid the (slow) process of cleaning the DC.
  220. *
  221. * NOTE: This does not provide BeginPaint/EndPaint functionality, so
  222. * the app will still have to call BeginPaint/EndPaint in its
  223. * WM_PAINT call.
  224. *
  225. * Arguments:
  226. *
  227. * [IN] hwnd - Specifies the window
  228. *
  229. * Return Value:
  230. *
  231. * NULL if failure (such as with an invalid hwnd).
  232. *
  233. * History:
  234. *
  235. * 12/04/1998 andrewgo
  236. * Created it.
  237. *
  238. \**************************************************************************/
  239. GpGraphics::GpGraphics(
  240. HWND hwnd,
  241. HDC hdc,
  242. INT clientWidth,
  243. INT clientHeight,
  244. HdcIcmMode icmMode,
  245. BOOL gdiLayered
  246. ) : BottomContext((hwnd != NULL) ||
  247. (GetDeviceCaps(hdc, TECHNOLOGY) == DT_RASDISPLAY))
  248. {
  249. ASSERT((hdc != NULL) || (hwnd != NULL));
  250. //User doesn't have any protection against negative client areas so we
  251. //should consider them to be valid although empty.
  252. clientWidth = max(clientWidth, 0);
  253. clientHeight = max(clientHeight, 0);
  254. SetValid(TRUE);
  255. Context = &BottomContext;
  256. Type = GraphicsScreen;
  257. Metafile = NULL;
  258. DownLevel = FALSE;
  259. Printer = FALSE;
  260. LockedByGetDC = 0;
  261. Driver = Globals::DesktopDriver;
  262. Surface = Globals::DesktopSurface;
  263. Device = Globals::DesktopDevice;
  264. GdipBitmap = NULL;
  265. CreatedDevice = FALSE;
  266. // We don't do GetDC(hwnd) here and store that here because,
  267. // among other things, we don't want to hit the cached DC
  268. // limit on Win9x:
  269. Context->Hdc = hdc;
  270. Context->Hwnd = hwnd;
  271. Context->IcmMode = icmMode;
  272. Context->GdiLayered = gdiLayered;
  273. HDC tempHdc = (hdc != NULL) ? hdc : Globals::DesktopDevice->DeviceHdc;
  274. if (GetDeviceCaps(tempHdc, BITSPIXEL) <= 8)
  275. {
  276. Context->PaletteMap = new EpPaletteMap(tempHdc);
  277. if (!Context->PaletteMap ||
  278. !Context->PaletteMap->IsValid())
  279. {
  280. WARNING(("Unable to compute palette translation vector"));
  281. SetValid(FALSE);
  282. return;
  283. }
  284. Context->PaletteMap->SetUniqueness(Globals::PaletteChangeCount);
  285. }
  286. ResetState(0, 0, clientWidth, clientHeight);
  287. // Now inherit state from the HDC:
  288. if (hwnd == NULL)
  289. {
  290. // In addition to extracting the HDC's transform state, this
  291. // will also extract app-specified clipping and combine it
  292. // with our other clipping state:
  293. if (IsValid())
  294. {
  295. SetValid(InheritAppClippingAndTransform(hdc) == Ok);
  296. }
  297. // Check the ICM Mode on the hdc - The ICM state in the HDC
  298. // passed in should override the flag setting.
  299. // IcmModeOn -> screen rendering will avoid using the
  300. // DCI codepath and instead render using GDI with the ICM enabled
  301. // HDC.
  302. if(::SetICMMode(hdc, ICM_QUERY)==ICM_ON)
  303. {
  304. Context->IcmMode = IcmModeOn;
  305. }
  306. else
  307. {
  308. // If the ICM mode is off or we failed somehow to query
  309. // the ICM mode, then set it to OFF.
  310. Context->IcmMode = IcmModeOff;
  311. }
  312. }
  313. else // non-NULL hwnd
  314. {
  315. // Since the window could have CS_OWNDC style, we still have to
  316. // inherit from it.
  317. HDC hdc = ::GetDC(hwnd);
  318. if (hdc != NULL)
  319. {
  320. if (IsValid())
  321. {
  322. SetValid(InheritAppClippingAndTransform(hdc) == Ok);
  323. }
  324. ::ReleaseDC(hwnd, hdc);
  325. }
  326. }
  327. }
  328. /******************************Public*Routine******************************\
  329. *
  330. * Function Description:
  331. *
  332. * Create a GpGraphics class from a DpBitmap.
  333. *
  334. * Arguments:
  335. *
  336. * [IN] surface - Specifies the DpBitmap
  337. *
  338. * Return Value:
  339. *
  340. * NULL if failure
  341. *
  342. \**************************************************************************/
  343. GpGraphics::GpGraphics(DpBitmap * surface)
  344. : BottomContext(surface->IsDisplay)
  345. {
  346. Surface = surface;
  347. BottomContext.ContainerDpiX = surface->DpiX;
  348. BottomContext.ContainerDpiY = surface->DpiY;
  349. Context = &BottomContext;
  350. Metafile = NULL;
  351. DownLevel = FALSE;
  352. Printer = FALSE;
  353. PrintInit = NULL;
  354. LockedByGetDC = 0;
  355. CreatedDevice = FALSE;
  356. GdipBitmap = NULL;
  357. Device = Globals::DesktopDevice;
  358. // Fail the creation of the destination if EpAlphaBlender
  359. // cannot convert to the DpBitmap pixel format.
  360. // The only reason to create a graphics around a bitmap is to be
  361. // able to draw onto it. If we can't convert the format to it,
  362. // we can't draw on it.
  363. if( (surface->Type != DpBitmap::GPBITMAP) ||
  364. (EpAlphaBlender::IsSupportedPixelFormat(surface->PixelFormat) &&
  365. surface->PixelFormat != PixelFormat8bppIndexed))
  366. {
  367. SetValid(TRUE);
  368. }
  369. else
  370. {
  371. SetValid(FALSE);
  372. }
  373. }
  374. /******************************Public*Routine******************************\
  375. *
  376. * Function Description:
  377. *
  378. * Check whether the HWND has windows layering set.
  379. *
  380. * Arguments:
  381. *
  382. * [IN] hwnd - Specifies the HWND
  383. *
  384. * [OUT] isLayeredWindow - Points to BOOL that returns layering property
  385. *
  386. * Return Value:
  387. *
  388. * FALSE if failure.
  389. *
  390. \**************************************************************************/
  391. BOOL
  392. CheckWindowsLayering(
  393. HWND hwnd,
  394. BOOL *isLayered
  395. )
  396. {
  397. BOOL bRet = TRUE;
  398. // Assume no layering.
  399. *isLayered = FALSE;
  400. // Layering is only supported on NT5 or better.
  401. if ((Globals::IsNt) && (Globals::OsVer.dwMajorVersion >= 5)
  402. && (Globals::GetWindowInfoFunction))
  403. {
  404. WINDOWINFO wndInfo;
  405. // Initialize the structure with the appropriate size.
  406. GpMemset(&wndInfo, 0, sizeof(WINDOWINFO));
  407. wndInfo.cbSize = sizeof(WINDOWINFO);
  408. // NTRAID#NTBUG9-385929-2001/05/05-asecchia
  409. // See JasonSch's comments in the bug report.
  410. // Perf [agodfrey]: JStall pointed out that GetWindowInfo is very
  411. // slow (he quoted 2,700,000 clocks). Much better would be
  412. // GetWindowLong(hwnd, GWL_EXSTYLE).
  413. if (Globals::GetWindowInfoFunction(hwnd, &wndInfo))
  414. {
  415. *isLayered = ((wndInfo.dwExStyle & WS_EX_LAYERED) != 0);
  416. // An app using layered windows might only have the property set
  417. // on the topmost or root window. So if we didn't find the
  418. // layered property on the window itself, need to check the root
  419. // window.
  420. if ((!*isLayered) && (Globals::GetAncestorFunction))
  421. {
  422. HWND hwndRoot = Globals::GetAncestorFunction(hwnd, GA_ROOT);
  423. // It's OK for GetAncestor to fail, which indicates that
  424. // hwnd is already the top level window. If it succeeds,
  425. // then hwnd is a child window and we need to check the
  426. // root for layering.
  427. if (hwndRoot)
  428. {
  429. // Perf [agodfrey]: Ditto here - GetWindowLong is better.
  430. if (Globals::GetWindowInfoFunction(hwndRoot, &wndInfo))
  431. {
  432. *isLayered = ((wndInfo.dwExStyle & WS_EX_LAYERED) != 0);
  433. }
  434. else
  435. {
  436. WARNING(("GetWindowInfo failed"));
  437. bRet = FALSE;
  438. }
  439. }
  440. }
  441. }
  442. else
  443. {
  444. WARNING(("GetWindowInfo failed"));
  445. bRet = FALSE;
  446. }
  447. }
  448. return bRet;
  449. }
  450. /******************************Public*Routine******************************\
  451. *
  452. * Function Description:
  453. *
  454. * Create a GpGraphics for a window.
  455. *
  456. * Arguments:
  457. *
  458. * [IN] hwnd - Specifies the window
  459. *
  460. * [IN] icmMode - Specifies the GDI ICM mode associated with this
  461. *
  462. * Return Value:
  463. *
  464. * NULL if failure.
  465. *
  466. \**************************************************************************/
  467. GpGraphics*
  468. GpGraphics::GetFromHwnd(
  469. HWND hwnd,
  470. HdcIcmMode icmMode
  471. )
  472. {
  473. // If hwnd is NULL, caller really meant that desktop
  474. // window should be used (Windows convention treats NULL hwnd
  475. // as a reference to desktop window).
  476. if (hwnd == NULL)
  477. {
  478. hwnd = GetDesktopWindow();
  479. ASSERT(hwnd != NULL);
  480. }
  481. RECT rect;
  482. // Check if hwnd has layering enabled. Need to let GpGraphics know
  483. // about it. Only on NT5 or better. Also note that GetWindowInfo
  484. // is only on NT4SP3 (or later) or Win98 (or later).
  485. BOOL isLayeredWindow;
  486. if (!CheckWindowsLayering(hwnd, &isLayeredWindow))
  487. {
  488. WARNING(("CheckWindowsLayering failed"));
  489. return NULL;
  490. }
  491. // GetClientRect is nice and fast (entirely user-mode on NT).
  492. if (::GetClientRect(hwnd, &rect))
  493. {
  494. ASSERT((rect.top == 0) && (rect.left == 0));
  495. GpGraphics *g = new GpGraphics(hwnd, NULL, rect.right, rect.bottom,
  496. icmMode, isLayeredWindow);
  497. CheckValid(g);
  498. return g;
  499. }
  500. else
  501. {
  502. WARNING(("GetClientRect failed"));
  503. }
  504. return NULL;
  505. }
  506. /******************************Public*Routine******************************\
  507. *
  508. * Function Description:
  509. *
  510. * Create a GpGraphics for a screen DC.
  511. *
  512. * Arguments:
  513. *
  514. * [IN] hdc - Specifies the DC
  515. *
  516. * Return Value:
  517. *
  518. * NULL if failure.
  519. *
  520. \**************************************************************************/
  521. GpGraphics*
  522. GpGraphics::GetFromGdiScreenDC(
  523. HDC hdc
  524. )
  525. {
  526. // If hdc is NULL, caller really meant that desktop
  527. // window should be used (Windows convention treats NULL hwnd
  528. // as a reference to desktop window).
  529. if (hdc == NULL)
  530. {
  531. return GpGraphics::GetFromHwnd(NULL);
  532. }
  533. ASSERT(GetDCType(hdc) == OBJ_DC);
  534. HWND hwnd = WindowFromDC(hdc);
  535. if (hwnd != NULL)
  536. {
  537. RECT windowRect;
  538. POINT dcOrg;
  539. // Check if hwnd has layering enabled. Need to let GpGraphics know
  540. // about it. Only on NT5 or better. Also note that GetWindowInfo
  541. // is only on NT4SP3 (or later) or Win98 (or later).
  542. BOOL isLayeredWindow;
  543. if (!CheckWindowsLayering(hwnd, &isLayeredWindow))
  544. {
  545. WARNING(("CheckWindowsLayering failed"));
  546. return NULL;
  547. }
  548. // If the user did a GetWindowFromDC call, then they want to be
  549. // able to draw to the entire window, not just to the client area.
  550. // In that case we use the WindowRect for the surface size, instead
  551. // of using the ClientRect. We determine this by seeing where the
  552. // DC origin is (the window rect or the client rect).
  553. if (::GetWindowRect(hwnd, &windowRect))
  554. {
  555. if (::GetDCOrgEx(hdc, &dcOrg))
  556. {
  557. if ((dcOrg.x == windowRect.left) && (dcOrg.y == windowRect.top))
  558. {
  559. windowRect.right -= windowRect.left;
  560. windowRect.bottom -= windowRect.top;
  561. GpGraphics *g = new GpGraphics(NULL,
  562. hdc,
  563. windowRect.right,
  564. windowRect.bottom,
  565. IcmModeOff,
  566. isLayeredWindow);
  567. CheckValid(g);
  568. return g;
  569. }
  570. RECT clientRect;
  571. // GetClientRect is nice and fast (entirely user-mode on NT).
  572. if (::GetClientRect(hwnd, &clientRect))
  573. {
  574. ASSERT((clientRect.top == 0) && (clientRect.left == 0));
  575. GpGraphics *g = new GpGraphics(NULL,
  576. hdc,
  577. clientRect.right,
  578. clientRect.bottom,
  579. IcmModeOff,
  580. isLayeredWindow);
  581. CheckValid(g);
  582. return g;
  583. }
  584. else
  585. {
  586. WARNING(("GetClientRect failed"));
  587. }
  588. }
  589. else
  590. {
  591. WARNING(("GetDCOrgEx failed"));
  592. }
  593. }
  594. else
  595. {
  596. WARNING(("GetWindowRect failed"));
  597. }
  598. }
  599. else // WindowFromDC failed
  600. {
  601. // The client must have used CreateDC("DISPLAY") to get this hdc,
  602. // so we'll use the full bounds of the screen to create the graphics.
  603. INT screenWidth;
  604. INT screenHeight;
  605. screenWidth = ::GetDeviceCaps(hdc, HORZRES);
  606. screenHeight = ::GetDeviceCaps(hdc, VERTRES);
  607. if ((screenWidth > 0) && (screenHeight > 0))
  608. {
  609. GpGraphics *g = new GpGraphics(NULL, hdc, screenWidth, screenHeight);
  610. CheckValid(g);
  611. return g;
  612. }
  613. }
  614. return NULL;
  615. }
  616. /**************************************************************************\
  617. *
  618. * Function Description:
  619. *
  620. * Set the Graphics container transform to copy the transform set in
  621. * the DC.
  622. *
  623. * NOTE: This function will be called a lot, and is therefore rather
  624. * performance critical. Do not add gratuitous GDI or GDI+
  625. * calls!
  626. *
  627. * Arguments:
  628. *
  629. * [IN] hdc - Specifies the DC to be copied
  630. *
  631. * Notes:
  632. *
  633. * This should really be a member of DpContext (bug #98174).
  634. *
  635. * Return Value:
  636. *
  637. * Ok if successful
  638. *
  639. * History:
  640. *
  641. * 12/04/1998 andrewgo
  642. * Created it.
  643. *
  644. \**************************************************************************/
  645. GpStatus
  646. GpGraphics::InheritAppClippingAndTransform(
  647. HDC hdc
  648. )
  649. {
  650. POINT points[3];
  651. GpPointF destPoints[3];
  652. GpRectF srcRect;
  653. GpRectF destRect;
  654. GpStatus infer = GenericError;
  655. GpStatus status;
  656. BYTE stackBuffer[1024];
  657. // It would take a lot of time to call all the Win32 APIs to query
  658. // the transform: we would minimally have to call GetMapMode,
  659. // GetWindowOrgEx, and GetViewportOrgEx; and maximally also have to
  660. // call GetWorldTransform, GetViewportExtEx, and GetWindowExtEx.
  661. //
  662. // We cheat a little by making a single call to LPtoDP with a
  663. // parallelogram, and then inferring the result. Note that we do
  664. // run the risk of some error, and on Win9x of overflow, since Win9x
  665. // only supports 16-bit coordinates. To counteract this, we try to
  666. // choose large values that won't overflow.
  667. // There is a common scenario when LPtoDP will overflow returning
  668. // bad saturated values. In printing to high DPI devices to avoid
  669. // overflow on Win9x, apps will use a large translate in the
  670. // window org to reposition the graphics. In such cases, we do
  671. // the expensive work of determining the real WorldToDevice.
  672. if (!Globals::IsNt && Context->ContainerDpiX > 600.0f)
  673. {
  674. INT mapMode = GetMapMode(hdc);
  675. if (mapMode == MM_ANISOTROPIC ||
  676. mapMode == MM_ISOTROPIC)
  677. {
  678. POINT viewOrg, windOrg;
  679. GetViewportOrgEx(hdc, &viewOrg);
  680. GetWindowOrgEx(hdc, &windOrg);
  681. SIZE viewExt, windExt;
  682. GetViewportExtEx(hdc, &viewExt);
  683. GetWindowExtEx(hdc, &windExt);
  684. GpRectF windRect(TOREAL(windOrg.x), TOREAL(windOrg.y),
  685. TOREAL(windExt.cx), TOREAL(windExt.cy));
  686. GpRectF viewRect(TOREAL(viewOrg.x), TOREAL(viewOrg.y),
  687. TOREAL(viewExt.cx), TOREAL(viewExt.cy));
  688. infer = Context->ContainerToDevice.InferAffineMatrix(viewRect,
  689. windRect);
  690. }
  691. }
  692. if (infer != Ok)
  693. {
  694. points[0].x = 0;
  695. points[0].y = 0;
  696. points[1].x = 8192;
  697. points[1].y = 0;
  698. points[2].x = 0;
  699. points[2].y = 8192;
  700. if (!LPtoDP(hdc, points, 3))
  701. return(GenericError);
  702. srcRect.X = TOREAL(0.0);
  703. srcRect.Y = TOREAL(0.0);
  704. srcRect.Width = TOREAL(8192.0);
  705. srcRect.Height = TOREAL(8192.0);
  706. if ((points[0].x == points[2].x) && (points[0].y == points[1].y))
  707. {
  708. // Win9x doesn't support rotation, and even on NT it will be
  709. // pretty rare. Having a special-case like this for scaling
  710. // saves us some work in 'InferAffineMatrix':
  711. destRect.X = LTOF(points[0].x);
  712. destRect.Y = LTOF(points[0].y);
  713. destRect.Width = LTOF(points[1].x - points[0].x);
  714. destRect.Height = LTOF(points[2].y - points[0].y);
  715. infer = Context->ContainerToDevice.InferAffineMatrix(destRect,
  716. srcRect);
  717. }
  718. else
  719. {
  720. destPoints[0].X = LTOF(points[0].x);
  721. destPoints[0].Y = LTOF(points[0].y);
  722. destPoints[1].X = LTOF(points[1].x);
  723. destPoints[1].Y = LTOF(points[1].y);
  724. destPoints[2].X = LTOF(points[2].x);
  725. destPoints[2].Y = LTOF(points[2].y);
  726. infer = Context->ContainerToDevice.InferAffineMatrix(destPoints,
  727. srcRect);
  728. }
  729. }
  730. if (infer != Ok)
  731. return(infer);
  732. Context->UpdateWorldToDeviceMatrix();
  733. // Quickly get a GDI region object:
  734. HRGN regionHandle = GetCachedGdiRegion();
  735. if (regionHandle == NULL)
  736. return(OutOfMemory);
  737. // Verify that our cache is working properly, and we have a valid region:
  738. ASSERT(GetObjectTypeInternal(regionHandle) == OBJ_REGION);
  739. // No early-outs from here-in, because we have to cleanup:
  740. status = Ok;
  741. // Query the application clip region, if there is one. The value of '1'
  742. // as a parameter is a magic value used by the metafile code on both
  743. // Win9x and NT to query the application clipping. If a value of zero
  744. // is returned, there is no application-set clipping.
  745. //
  746. // Note that if we had passed in SYSRGN (a value of '4') instead of '1',
  747. // the result does NOT include the application level clipping. (In other
  748. // words, SYSRGN is not equivalent to the Rao region, which is why we have
  749. // to explicitly query the application clipping here.)
  750. INT getResult = GetRandomRgn(hdc, regionHandle, 1);
  751. if (getResult == TRUE)
  752. {
  753. // If our stack buffer is big enough, get the clipping contents
  754. // in one gulp:
  755. INT newSize = GetRegionData(regionHandle,
  756. sizeof(stackBuffer),
  757. (RGNDATA*) &stackBuffer[0]);
  758. RGNDATA *regionBuffer = (RGNDATA*) &stackBuffer[0];
  759. // The spec says that GetRegionData returns '1' in the event of
  760. // success, but NT returns the actual number of bytes written if
  761. // successful, and returns '0' if the buffer wasn't large enough:
  762. if ((newSize < 1) || (newSize > sizeof(stackBuffer)))
  763. {
  764. // Our stack buffer wasn't big enough. Figure out the required
  765. // size:
  766. newSize = GetRegionData(regionHandle, 0, NULL);
  767. if (newSize > 1)
  768. {
  769. regionBuffer = (RGNDATA*) GpMalloc(newSize);
  770. if (regionBuffer == NULL)
  771. return OutOfMemory;
  772. // Initialize to a decent result in the unlikely event of
  773. // failure of GetRegionData:
  774. regionBuffer->rdh.nCount = 0;
  775. GetRegionData(regionHandle, newSize, regionBuffer);
  776. }
  777. }
  778. // Set our GDI+ container clipping to be the same thing as the
  779. // GDI application clipping:
  780. status = Context->ContainerClip.Set((RECT*) &regionBuffer->Buffer[0],
  781. regionBuffer->rdh.nCount);
  782. if (status == Ok)
  783. {
  784. // the ContainerClip must always be intersected with the WindowClip
  785. status = Context->ContainerClip.And(&WindowClip);
  786. }
  787. if (status != Ok)
  788. {
  789. // use the best fall-back solution we can
  790. // guaranteed to succeed
  791. Context->ContainerClip.Set(&WindowClip);
  792. }
  793. // Now calculate the combined result:
  794. status = this->AndVisibleClip();
  795. // Free the temporary buffer if one was allocated:
  796. if (regionBuffer != (RGNDATA*) &stackBuffer[0])
  797. GpFree(regionBuffer);
  798. }
  799. ReleaseCachedGdiRegion(regionHandle);
  800. return(status);
  801. }
  802. /**************************************************************************\
  803. *
  804. * Function Description:
  805. *
  806. * Create a GpGraphics class from a bitmap DC.
  807. *
  808. * History:
  809. *
  810. * 12/06/1998 andrewgo
  811. * Created it.
  812. *
  813. * 11/21/2000 minliu
  814. * Change the way GDI+ using the palette inside the DIBSection
  815. *
  816. \**************************************************************************/
  817. GpGraphics*
  818. GpGraphics::GetFromGdiBitmap(
  819. HDC hdc
  820. )
  821. {
  822. ASSERT((hdc != NULL) && (GetDCType(hdc) == OBJ_MEMDC));
  823. HBITMAP hbitmap = (HBITMAP) GetCurrentObject(hdc, OBJ_BITMAP);
  824. if (hbitmap)
  825. {
  826. DpBitmap *bitmap = new DpBitmap(hdc); // initializes Dpi
  827. if (CheckValid(bitmap))
  828. {
  829. INT bitmapWidth;
  830. INT bitmapHeight;
  831. EpPaletteMap* pPaletteMap = NULL;
  832. DIBSECTION dibInfo;
  833. INT infoSize = GetObjectA(hbitmap, sizeof(dibInfo),
  834. &dibInfo);
  835. BOOL initialized = FALSE;
  836. BOOL isHalftoneDIB = FALSE;
  837. DpDriver* driver = NULL;
  838. ColorPalette* pPalette = NULL;
  839. // WinNT/Win95 differences in GetObject:
  840. //
  841. // WinNT always returns the number of bytes filled, either
  842. // sizeof(BITMAP) or sizeof(DIBSECTION).
  843. //
  844. // Win95 always returns the original requested size (filling the
  845. // remainder with NULLs). So if it is a DIBSECTION, we expect
  846. // dibInfo.dsBmih.biSize != 0; otherwise it is a BITMAP.
  847. if ( (infoSize == sizeof(DIBSECTION) )
  848. &&(Globals::IsNt || dibInfo.dsBmih.biSize != 0) )
  849. {
  850. // If this is an 8 bpp DIB, get its color palette and make a
  851. // matching palette map from our halftone palette.
  852. if ( dibInfo.dsBmih.biBitCount == 8 )
  853. {
  854. // Create a new EpPaletteMap object.
  855. // Note: If the colorTable is exactly the same as our
  856. // GDI+ halftone palette, we will have a 1 to 1 color
  857. // translation table in the EpPaletteMap object. If the
  858. // color palette doesn't match exactly with our GDI+
  859. // halftone palette and also within a certain
  860. // mismatching range, we will have a translation table
  861. // in EpPaletteMap object.
  862. // Also, EpPaletteMap object will set up a IsVGAOnly()
  863. // to tell us if GDI+ can do the halftone dithering or
  864. // not (if IsVGAOnly() returns FALSE, it means GDI+ can
  865. // do it
  866. // NOTE: EpPaletteMap may allocate storage for pPalette
  867. // which must be freed with GpFree.
  868. pPaletteMap = new EpPaletteMap(hdc, &pPalette, TRUE);
  869. if ( pPaletteMap == NULL )
  870. {
  871. WARNING(("FromGdiBmp()-new EpPaletteMap failed"));
  872. }
  873. else if ( (pPaletteMap->IsValid() == TRUE)
  874. &&(pPaletteMap->IsVGAOnly() == FALSE) )
  875. {
  876. ASSERT(pPalette != NULL);
  877. // GDI+ can do the halftone dithering
  878. isHalftoneDIB = TRUE;
  879. }
  880. else
  881. {
  882. // The supplied palette has insufficient
  883. // matching colors for our halftone dithering,
  884. // but we can still do VGA dithering. However,
  885. // we'll use the GDI bitmap path instead, to
  886. // be safe, since this is what we were doing
  887. // before.
  888. if (pPaletteMap->IsValid())
  889. {
  890. ASSERT(pPalette != NULL);
  891. GpFree(pPalette);
  892. pPalette = NULL;
  893. }
  894. delete pPaletteMap;
  895. pPaletteMap = NULL;
  896. }
  897. }// if ( dibInfo.dsBmih.biBitCount == 8 )
  898. // Up to this point, we will either have isHalftoneDIB = TRUE,
  899. // which means GDI+ can do the dithering or FALSE otherwise.
  900. if ((dibInfo.dsBmih.biBitCount > 8) || (isHalftoneDIB == TRUE) )
  901. {
  902. initialized = bitmap->InitializeForDibsection(
  903. hdc,
  904. hbitmap,
  905. Globals::DesktopDevice,
  906. &dibInfo,
  907. &bitmapWidth,
  908. &bitmapHeight,
  909. &driver
  910. );
  911. }
  912. }// if it is a DIBSection
  913. if ( initialized == FALSE )
  914. {
  915. // Use GDI code path
  916. bitmapWidth = dibInfo.dsBm.bmWidth;
  917. bitmapHeight = dibInfo.dsBm.bmHeight;
  918. bitmap->InitializeForGdiBitmap(Globals::DesktopDevice,
  919. bitmapWidth,
  920. bitmapHeight);
  921. driver = Globals::GdiDriver;
  922. }
  923. GpGraphics *g = new GpGraphics(bitmap);
  924. if (g)
  925. {
  926. // NTRAID#NTBUG9-370409-2001/04/17-asecchia
  927. // This is error-prone code. The GpGraphics and the DpContext
  928. // objects should properly encapsulate their own construction.
  929. g->Type = GraphicsBitmap;
  930. g->Driver = driver;
  931. g->Context->Hdc = hdc;
  932. g->Context->PaletteMap = NULL;
  933. g->Context->Palette = NULL;
  934. g->ResetState(0, 0, bitmapWidth, bitmapHeight);
  935. if (g->InheritAppClippingAndTransform(hdc) == Ok)
  936. {
  937. // If this is our special DIB, set the original palette in
  938. // the Context so that later on when we doing alpha blend
  939. // etc., we can use it to read pixel data from the
  940. // DIBSection correctly
  941. if ( isHalftoneDIB == TRUE )
  942. {
  943. g->Context->Palette = pPalette;
  944. g->Context->PaletteMap = pPaletteMap;
  945. return(g);
  946. }
  947. else if (GetDeviceCaps(hdc, BITSPIXEL) <= 8)
  948. {
  949. ASSERT(pPaletteMap == NULL);
  950. pPaletteMap = new EpPaletteMap(hdc);
  951. if ( NULL != pPaletteMap )
  952. {
  953. pPaletteMap->SetUniqueness(
  954. Globals::PaletteChangeCount
  955. );
  956. if ( pPaletteMap->IsValid() )
  957. {
  958. // Now that we know that the pPaletteMap is
  959. // valid and we're returning a valid GpGraphics
  960. // we can give up ownership of the pPaletteMap
  961. // to the GpGraphics and return without
  962. // deleting it.
  963. g->Context->PaletteMap = pPaletteMap;
  964. return(g);
  965. }
  966. }
  967. }
  968. else
  969. {
  970. // Higher than 8 bpp, this graphics object is fine
  971. return(g);
  972. }
  973. }// if (g->InheritAppClippingAndTransform(hdc) == Ok)
  974. delete g;
  975. }// if (g)
  976. else
  977. {
  978. delete bitmap;
  979. }
  980. // We fall into here only we failed to create the Graphics object
  981. if ( NULL != pPaletteMap )
  982. {
  983. delete pPaletteMap;
  984. }
  985. if ( NULL != pPalette )
  986. {
  987. GpFree(pPalette);
  988. }
  989. }// if (CheckValid(bitmap))
  990. }// if ( hbitmap )
  991. else
  992. {
  993. RIP(("GetCurrentObject failed"));
  994. }
  995. return(NULL);
  996. }// GetFromGdiBitmap()
  997. /**************************************************************************\
  998. *
  999. * Function Description:
  1000. *
  1001. * Create a GpGraphics class from a GpBitmap.
  1002. *
  1003. * History:
  1004. *
  1005. * 09/22/1999 gilmanw
  1006. * Created it.
  1007. *
  1008. \**************************************************************************/
  1009. GpGraphics*
  1010. GpGraphics::GetFromGdipBitmap(
  1011. GpBitmap * bitmap,
  1012. ImageInfo * imageInfo,
  1013. EpScanBitmap * scanBitmap,
  1014. BOOL isDisplay
  1015. )
  1016. {
  1017. DpBitmap *surface = new DpBitmap();
  1018. if (CheckValid(surface))
  1019. {
  1020. // This call initializes the DPI and IsDisplay members
  1021. surface->InitializeForGdipBitmap(imageInfo->Width, imageInfo->Height, imageInfo, scanBitmap, isDisplay);
  1022. GpGraphics *g = new GpGraphics(surface);
  1023. if (g)
  1024. {
  1025. g->Type = GraphicsBitmap;
  1026. g->Driver = Globals::EngineDriver;
  1027. g->Context->Hdc = NULL;
  1028. g->GdipBitmap = bitmap;
  1029. g->ResetState(0, 0, imageInfo->Width, imageInfo->Height);
  1030. return g;
  1031. }
  1032. else
  1033. {
  1034. delete surface;
  1035. }
  1036. }
  1037. return(NULL);
  1038. }
  1039. /**************************************************************************\
  1040. *
  1041. * Function Description:
  1042. *
  1043. * Create a GpGraphics class from a direct draw surface.
  1044. *
  1045. * History:
  1046. *
  1047. * 10/06/1999 bhouse
  1048. * Created it.
  1049. *
  1050. \**************************************************************************/
  1051. GpGraphics*
  1052. GpGraphics::GetFromDirectDrawSurface(
  1053. IDirectDrawSurface7 * surface
  1054. )
  1055. {
  1056. INT bitmapWidth;
  1057. INT bitmapHeight;
  1058. GpGraphics *g;
  1059. DpDriver *driver;
  1060. DpBitmap *bitmap = new DpBitmap();
  1061. if (CheckValid(bitmap))
  1062. {
  1063. // Leave bitmap->IsDisplay and Dpi params at their default values.
  1064. if( bitmap->InitializeForD3D(surface,
  1065. &bitmapWidth,
  1066. &bitmapHeight,
  1067. &driver))
  1068. {
  1069. GpGraphics *g = new GpGraphics(bitmap);
  1070. if (g)
  1071. {
  1072. g->Type = GraphicsBitmap;
  1073. g->Driver = driver;
  1074. g->Context->Hdc = NULL;
  1075. g->ResetState(0, 0, bitmapWidth, bitmapHeight);
  1076. return(g);
  1077. }
  1078. else
  1079. {
  1080. delete bitmap;
  1081. }
  1082. }
  1083. }
  1084. return(NULL);
  1085. }
  1086. /**************************************************************************\
  1087. *
  1088. * Function Description:
  1089. *
  1090. * This should only be called by the GetFromHdc() for a printer DC.
  1091. *
  1092. * Arguments:
  1093. *
  1094. *
  1095. * Return Value:
  1096. *
  1097. *
  1098. * History:
  1099. * 6/1/1999 ericvan Created it.
  1100. *
  1101. \**************************************************************************/
  1102. GpStatus
  1103. GpGraphics::StartPrinterEMF()
  1104. {
  1105. IStream *stream = NULL;
  1106. INT result;
  1107. // Send escape to printer to determine if this supports the UNIDRV
  1108. // escape codes.
  1109. GDIPPRINTINIT printInit;
  1110. printInit.dwSize = sizeof(GDIPPRINTINIT);
  1111. // !! Query whether escape is supported first.
  1112. result = ExtEscape(Context->Hdc,
  1113. GDIPLUS_UNI_INIT,
  1114. 0,
  1115. NULL,
  1116. sizeof(GDIPPRINTINIT),
  1117. (LPSTR)&printInit);
  1118. if (result<=0)
  1119. return NotImplemented;
  1120. // save printer data in structure
  1121. PrintInit = new GDIPPRINTINIT;
  1122. if(!PrintInit)
  1123. {
  1124. return OutOfMemory;
  1125. }
  1126. memcpy((LPVOID)PrintInit, (LPVOID)&printInit, sizeof(GDIPPRINTINIT));
  1127. PrinterEMF = GlobalAlloc(GMEM_MOVEABLE, 1);
  1128. if (!PrinterEMF)
  1129. {
  1130. return OutOfMemory;
  1131. }
  1132. if (CreateStreamOnHGlobal(PrinterEMF, FALSE, &stream) != S_OK)
  1133. {
  1134. GlobalFree(PrinterEMF);
  1135. PrinterEMF = NULL;
  1136. return Win32Error;
  1137. }
  1138. FPUStateSaver fpuState;
  1139. PrinterMetafile = new GpMetafile(stream, Context->Hdc, EmfTypeEmfPlusOnly);
  1140. stream->Release();
  1141. if (!PrinterMetafile)
  1142. {
  1143. GlobalFree(PrinterEMF);
  1144. PrinterEMF = NULL;
  1145. return OutOfMemory;
  1146. }
  1147. PrinterGraphics = PrinterMetafile->GetGraphicsContext();
  1148. Metafile = PrinterGraphics->Metafile;
  1149. return Ok;
  1150. }
  1151. /**************************************************************************\
  1152. *
  1153. * Function Description:
  1154. *
  1155. * Arguments:
  1156. *
  1157. * Return Value:
  1158. *
  1159. * History:
  1160. * 6/1/1999 ericvan Created it.
  1161. *
  1162. \**************************************************************************/
  1163. GpStatus
  1164. GpGraphics::EndPrinterEMF()
  1165. {
  1166. LPVOID emfBlock;
  1167. INT result = -1;
  1168. GpStatus status;
  1169. if (PrinterGraphics)
  1170. {
  1171. // end recording to metafile graphics context
  1172. delete PrinterGraphics;
  1173. PrinterGraphics = NULL;
  1174. }
  1175. // Disposing of metafile also Release() stream interface.
  1176. if (PrinterMetafile)
  1177. {
  1178. PrinterMetafile->Dispose();
  1179. PrinterMetafile = NULL;
  1180. Metafile = NULL;
  1181. }
  1182. if (PrinterEMF)
  1183. {
  1184. emfBlock = GlobalLock(PrinterEMF);
  1185. if (emfBlock)
  1186. result = ExtEscape(Context->Hdc,
  1187. GDIPLUS_UNI_ESCAPE,
  1188. // This is a downcast on IA64, but I don't believe
  1189. // PrinterEMF will be bigger than MAXINT
  1190. (ULONG)GlobalSize(PrinterEMF),
  1191. (LPCSTR)emfBlock,
  1192. sizeof(GpStatus),
  1193. (LPSTR)&status);
  1194. GlobalUnlock(PrinterEMF);
  1195. GlobalFree(PrinterEMF);
  1196. PrinterEMF = NULL;
  1197. if (result>0)
  1198. return status;
  1199. else
  1200. return Win32Error;
  1201. }
  1202. else
  1203. return Ok;
  1204. }
  1205. /**************************************************************************\
  1206. *
  1207. * Function Description:
  1208. *
  1209. * Uses HDC and HANDLE for printer to determine the postscript level. The
  1210. * caller must ensure this is a PS HDC so we don't waste our time.
  1211. *
  1212. * Arguments:
  1213. *
  1214. * HDC - handle to device context for printer
  1215. * HANDLE - handle to printer device (may be NULL)
  1216. *
  1217. * Return Value:
  1218. *
  1219. * Postscript Level - (-1) if not found, must provide downlevel support
  1220. *
  1221. * History:
  1222. * 10/26/1999 ericvan Created it.
  1223. *
  1224. \**************************************************************************/
  1225. INT
  1226. GpGraphics::GetPostscriptLevel(HDC hDC, HANDLE hPrinter)
  1227. {
  1228. // Place this code under the load library critical section. We make
  1229. // extensive use of Globals::variables and need protection.
  1230. LoadLibraryCriticalSection llcs;
  1231. INT feature = FEATURESETTING_PSLEVEL;
  1232. INT level;
  1233. if ((Globals::hCachedPrinter != 0) &&
  1234. (Globals::hCachedPrinter == hPrinter))
  1235. {
  1236. return Globals::CachedPSLevel;
  1237. }
  1238. // !! Re-examine this, Nolan said he would add this to the HP ps driver
  1239. // so we may have this working on Win9x
  1240. if (Globals::IsNt && Globals::OsVer.dwMajorVersion >= 5)
  1241. {
  1242. DWORD EscapeValue = POSTSCRIPT_IDENTIFY;
  1243. if (ExtEscape(hDC,
  1244. QUERYESCSUPPORT,
  1245. sizeof(DWORD),
  1246. (LPSTR)&EscapeValue,
  1247. 0,
  1248. NULL) != 0)
  1249. {
  1250. // must be in GDI centric mode to get PS feature settings...
  1251. DWORD Mode = PSIDENT_GDICENTRIC;
  1252. if (ExtEscape(hDC,
  1253. POSTSCRIPT_IDENTIFY,
  1254. sizeof(DWORD),
  1255. (LPSTR)&Mode,
  1256. 0,
  1257. NULL)>0)
  1258. {
  1259. if (ExtEscape(hDC,
  1260. GET_PS_FEATURESETTING,
  1261. sizeof(INT),
  1262. (LPSTR)&feature,
  1263. sizeof(INT),
  1264. (LPSTR)&level)>0)
  1265. {
  1266. Globals::hCachedPrinter = hPrinter;
  1267. Globals::CachedPSLevel = level;
  1268. return level;
  1269. }
  1270. }
  1271. }
  1272. }
  1273. if (hPrinter == NULL)
  1274. return -1;
  1275. // get name of the PPD file.
  1276. union
  1277. {
  1278. DRIVER_INFO_2A driverInfo;
  1279. CHAR buftmp[1024];
  1280. };
  1281. DWORD size;
  1282. // we require GetPrinterDriver() API to get the .PPD path+file.
  1283. // unfortunately this API is buried in winspool.drv (112KB), so we
  1284. // lazy load it here.
  1285. if (Globals::WinspoolHandle == NULL)
  1286. {
  1287. Globals::WinspoolHandle = LoadLibraryA("winspool.drv");
  1288. if (Globals::WinspoolHandle == NULL)
  1289. return -1;
  1290. Globals::GetPrinterDriverFunction = (WINSPOOLGETPRINTERDRIVERFUNCTION)
  1291. GetProcAddress(
  1292. Globals::WinspoolHandle,
  1293. "GetPrinterDriverA");
  1294. }
  1295. if (Globals::GetPrinterDriverFunction == NULL)
  1296. return -1;
  1297. if (Globals::GetPrinterDriverFunction(hPrinter,
  1298. NULL,
  1299. 2,
  1300. (BYTE*)&driverInfo,
  1301. 1024,
  1302. &size) == 0)
  1303. return -1;
  1304. HANDLE hFile;
  1305. hFile = CreateFileA(driverInfo.pDataFile,
  1306. GENERIC_READ,
  1307. FILE_SHARE_READ,
  1308. NULL,
  1309. OPEN_EXISTING,
  1310. FILE_ATTRIBUTE_NORMAL,
  1311. NULL);
  1312. if (hFile == INVALID_HANDLE_VALUE)
  1313. return -1;
  1314. // Obtain the file size
  1315. // NOTE: We don't support files larger than 4GB.
  1316. DWORD sizeLow, sizeHigh;
  1317. sizeLow = GetFileSize(hFile, &sizeHigh);
  1318. // impose a 4GB limit (certainly reasonable)
  1319. if (sizeLow == 0xffffffff || sizeHigh != 0)
  1320. {
  1321. CloseHandle(hFile);
  1322. return -1;
  1323. }
  1324. // Map the file into memory
  1325. HANDLE hFilemap;
  1326. LPSTR fileview = NULL;
  1327. hFilemap = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
  1328. if (hFilemap != NULL)
  1329. {
  1330. fileview = (LPSTR) MapViewOfFile(hFilemap, FILE_MAP_READ, 0, 0, 0);
  1331. CloseHandle(hFilemap);
  1332. }
  1333. else
  1334. {
  1335. CloseHandle(hFile);
  1336. return -1;
  1337. }
  1338. LPSTR buf = fileview;
  1339. LPSTR topbuf = fileview + (sizeLow-16);
  1340. // we actually expect the LanguageLevel to be early
  1341. // in the file (likely in the first 2K of data).
  1342. // !! What if this appears in comments (read starting at carriage returns?!
  1343. level = -1;
  1344. while (buf < topbuf)
  1345. {
  1346. if (*buf == 'L' &&
  1347. GpMemcmp(buf, "LanguageLevel", 13) == 0)
  1348. {
  1349. while ((*buf < '0' || *buf > '9') && buf < topbuf)
  1350. buf++;
  1351. CHAR ch = *buf;
  1352. if (ch >= '0' && ch <= '9')
  1353. level = (INT)ch - (INT)'0';
  1354. break;
  1355. }
  1356. buf++;
  1357. }
  1358. UnmapViewOfFile((LPCVOID)fileview);
  1359. CloseHandle(hFile);
  1360. Globals::hCachedPrinter = hPrinter;
  1361. Globals::CachedPSLevel = level;
  1362. return level;
  1363. }
  1364. /**************************************************************************\
  1365. *
  1366. * Function Description:
  1367. *
  1368. * Arguments:
  1369. *
  1370. * Return Value:
  1371. *
  1372. * History:
  1373. * 6/1/1999 ericvan Created it.
  1374. *
  1375. \**************************************************************************/
  1376. GpGraphics*
  1377. GpGraphics::GetFromGdiPrinterDC(
  1378. HDC hdc,
  1379. HANDLE hPrinter
  1380. )
  1381. {
  1382. ASSERT((hdc != NULL) &&
  1383. ((GetDCType(hdc) == OBJ_DC) ||
  1384. (GetDCType(hdc) == OBJ_ENHMETADC))&&
  1385. (GetDeviceCaps(hdc, TECHNOLOGY) == DT_RASPRINTER));
  1386. // !! Change to useNewPrinterCode when we've fully switched
  1387. DriverPrint *driver = NULL;
  1388. DpBitmap *bitmap = new DpBitmap(hdc); // initializes Dpi
  1389. if (CheckValid(bitmap))
  1390. {
  1391. GpPrinterDevice *pdevice;
  1392. { // FPU Sandbox for potentially unsafe FPU code.
  1393. FPUStateSandbox fpsb;
  1394. pdevice = new GpPrinterDevice(hdc);
  1395. } // FPU Sandbox for potentially unsafe FPU code.
  1396. if (CheckValid(pdevice))
  1397. {
  1398. // we defer creating driver until we know which to create
  1399. // change to use DIBSECTION instead of GDI operations
  1400. // Check if this is a postscript printer
  1401. CHAR strTech[30];
  1402. strTech[0] = '\0';
  1403. INT ScaleX; // ratio of device DPI : capped DPI
  1404. INT ScaleY;
  1405. // It is a PostScript printer if POSTSCRIPT_PASSTHROUGH or
  1406. // POSTSCRIPT_IGNORE is available. For some reason querying
  1407. // GETTECHNOLOGY for postscript fails in MS Publisher, it may
  1408. // be because we are in GDI centric mode.
  1409. int iWant1 = POSTSCRIPT_PASSTHROUGH;
  1410. int iWant2 = POSTSCRIPT_IGNORE;
  1411. BOOL postscript;
  1412. { // FPU Sandbox for potentially unsafe FPU code.
  1413. FPUStateSandbox fpsb;
  1414. postscript = (
  1415. (Escape(hdc, QUERYESCSUPPORT, sizeof(iWant1), (LPCSTR)&iWant1, NULL) != 0) ||
  1416. (Escape(hdc, QUERYESCSUPPORT, sizeof(iWant2), (LPCSTR)&iWant2, NULL) != 0));
  1417. } // FPU Sandbox for potentially unsafe FPU code.
  1418. SIZEL szlDevice;
  1419. szlDevice.cx = GetDeviceCaps(hdc, HORZRES);
  1420. szlDevice.cy = GetDeviceCaps(hdc, VERTRES);
  1421. // ScaleX and ScaleY should be power of two (2, 4, 8)
  1422. if (bitmap->DpiX <= 100)
  1423. {
  1424. ScaleX = 1;
  1425. ScaleY = 1;
  1426. }
  1427. else
  1428. {
  1429. if (bitmap->DpiX >= 1200)
  1430. {
  1431. ScaleX = GpRound(TOREAL(bitmap->DpiX) / 200);
  1432. ScaleY = GpRound(TOREAL(bitmap->DpiY) / 200);
  1433. }
  1434. else
  1435. {
  1436. ScaleX = 3;
  1437. ScaleY = 3; // cap 600 to 200 dpi or 3:1
  1438. }
  1439. }
  1440. // We no longer keep capped dpi -- we use the device dpi as
  1441. // capped dpi so that the world to
  1442. // device transformation is correct for the clipped region and
  1443. // path transformation. ScaleX and ScaleY are used to scale
  1444. // the output rectangle region.
  1445. bitmap->InitializeForPrinter(pdevice,
  1446. szlDevice.cx,
  1447. szlDevice.cy);
  1448. GpGraphics *g = new GpGraphics(bitmap);
  1449. if (g)
  1450. {
  1451. g->Printer = TRUE;
  1452. g->Context->Hdc = hdc;
  1453. g->Context->IsPrinter = TRUE;
  1454. // Note: Both 'Device' and 'Driver' are freed at
  1455. // ~GpGraphics time when 'CreatedDevice' is set:
  1456. g->PrinterMetafile = NULL;
  1457. g->PrinterGraphics = NULL;
  1458. g->PrinterEMF = NULL;
  1459. if (postscript)
  1460. {
  1461. g->Type = GraphicsBitmap;
  1462. INT level = GetPostscriptLevel(hdc, hPrinter);
  1463. driver = new DriverPS(pdevice, level);
  1464. // !! Should this stuff be shifted into some driver
  1465. // initialization routine?!
  1466. // !! Interop - what about redefines or conflicts
  1467. // (conflicts aren't likely, but are
  1468. // theoretically possible with
  1469. // GetDC/ReleaseDC)
  1470. }
  1471. #if 0
  1472. else if (g->StartPrinterEMF() == Ok)
  1473. {
  1474. g->Type = GraphicsMetafile;
  1475. RIP(("Setting CreatedDevice will free Driver"));
  1476. driver = NULL; new DriverMeta(pdevice);
  1477. // GDI has some optimization code to check the page for color
  1478. // content, if none is found, on play back, it sets the device
  1479. // as being monochrome.
  1480. //
  1481. // Unfortunately, since our stuff is encoded in escapes, we end up
  1482. // playing back in monochrome. The work-around, is to call a GDI
  1483. // API that forces a color flag to be set in the EMF code. A
  1484. // simple one is SetTextColor().
  1485. // !!! Might want to remove this SetTextColor stuff for version one:
  1486. COLORREF colorRef = GetTextColor(hdc);
  1487. SetTextColor(hdc, 0x00808080);
  1488. SetTextColor(hdc, colorRef);
  1489. }
  1490. #endif
  1491. else
  1492. {
  1493. // We can't use escapes for optimization,
  1494. // Map to GDI HDC operations.
  1495. g->Type = GraphicsBitmap;
  1496. driver = new DriverNonPS(pdevice);
  1497. }
  1498. if (CheckValid(driver))
  1499. {
  1500. g->Driver = driver;
  1501. g->Device = pdevice;
  1502. driver->ScaleX = ScaleX;
  1503. driver->ScaleY = ScaleY;
  1504. // check for supporting print clip escapes
  1505. // These may be supported on PCL as well as Postscript
  1506. DWORD EscapeValue1 = CLIP_TO_PATH;
  1507. DWORD EscapeValue2 = BEGIN_PATH;
  1508. DWORD EscapeValue3 = END_PATH;
  1509. // Although some PCL drivers support CLIP_TO_PATH we
  1510. // currently disable their use due to some outstanding
  1511. // bugs in HP and Lexmark PCL drivers. See bug #182972
  1512. { // FPU Sandbox for potentially unsafe FPU code.
  1513. FPUStateSandbox fpsb;
  1514. driver->UseClipEscapes = postscript &&
  1515. (ExtEscape(hdc,
  1516. QUERYESCSUPPORT,
  1517. sizeof(DWORD),
  1518. (LPSTR)&EscapeValue1,
  1519. 0,
  1520. NULL) != 0) &&
  1521. (ExtEscape(hdc,
  1522. QUERYESCSUPPORT,
  1523. sizeof(DWORD),
  1524. (LPSTR)&EscapeValue2,
  1525. 0,
  1526. NULL) != 0) &&
  1527. (ExtEscape(hdc,
  1528. QUERYESCSUPPORT,
  1529. sizeof(DWORD),
  1530. (LPSTR)&EscapeValue3,
  1531. 0,
  1532. NULL) != 0);
  1533. } // FPU Sandbox for potentially unsafe FPU code.
  1534. DWORD EscapeValue = CHECKJPEGFORMAT;
  1535. { // FPU Sandbox for potentially unsafe FPU code.
  1536. FPUStateSandbox fpsb;
  1537. driver->SupportJPEGpassthrough = ExtEscape(
  1538. hdc,
  1539. QUERYESCSUPPORT,
  1540. sizeof(DWORD),
  1541. (LPSTR)&EscapeValue,
  1542. 0,
  1543. NULL) != 0;
  1544. } // FPU Sandbox for potentially unsafe FPU code.
  1545. EscapeValue = CHECKPNGFORMAT;
  1546. { // FPU Sandbox for potentially unsafe FPU code.
  1547. FPUStateSandbox fpsb;
  1548. driver->SupportPNGpassthrough = ExtEscape(
  1549. hdc,
  1550. QUERYESCSUPPORT,
  1551. sizeof(DWORD),
  1552. (LPSTR)&EscapeValue,
  1553. 0,
  1554. NULL) != 0;
  1555. } // FPU Sandbox for potentially unsafe FPU code.
  1556. driver->NumColors = GetDeviceCaps(hdc, NUMCOLORS);
  1557. driver->UseVDP = FALSE;
  1558. // !! VDP not supported in v1
  1559. //VDP_GetFormSupport(hdc,
  1560. // (WORD*)&(driver->SupportVDP));
  1561. g->CreatedDevice = TRUE;
  1562. g->ResetState(0, 0, szlDevice.cx, szlDevice.cy);
  1563. if (g->InheritAppClippingAndTransform(hdc) == Ok)
  1564. {
  1565. return(g);
  1566. }
  1567. else
  1568. {
  1569. // ~GpGraphics implicitly deletes bitmap, device, and driver
  1570. delete g;
  1571. return NULL;
  1572. }
  1573. }
  1574. delete g;
  1575. delete pdevice;
  1576. return NULL;
  1577. }
  1578. delete pdevice;
  1579. }
  1580. delete bitmap;
  1581. }
  1582. return NULL;
  1583. }
  1584. /**************************************************************************\
  1585. *
  1586. * Function Description:
  1587. *
  1588. * This constructor is used internally for printer callback routine.
  1589. *
  1590. * Arguments:
  1591. *
  1592. *
  1593. * Return Value:
  1594. *
  1595. *
  1596. * History:
  1597. * 6/1/1999 ericvan Created it.
  1598. *
  1599. \**************************************************************************/
  1600. GpGraphics*
  1601. GpGraphics::GetFromHdcSurf(
  1602. HDC hdc,
  1603. SURFOBJ* surfObj,
  1604. RECTL* bandClip
  1605. )
  1606. {
  1607. static UINT16 PixelCount[] = { 1, 4, 8, 16, 24, 32 };
  1608. INT width;
  1609. INT height;
  1610. GpGraphics* g;
  1611. DpDriver *driver;
  1612. // This is a weird surface. It is a banded surface, so we set up a
  1613. // clip and direct bits pointer. We also have an HDC for it if we
  1614. // decide to punt to GDI.
  1615. DpBitmap *bitmap = new DpBitmap(hdc); // initializes Dpi
  1616. if (CheckValid(bitmap))
  1617. {
  1618. GpGraphics *g = new GpGraphics(bitmap);
  1619. if (g)
  1620. {
  1621. width = GetDeviceCaps(hdc, HORZRES);
  1622. height = GetDeviceCaps(hdc, VERTRES);
  1623. // create DIB section for direct rendering of bits
  1624. if (surfObj->iBitmapFormat < BMF_1BPP ||
  1625. surfObj->iBitmapFormat > BMF_32BPP)
  1626. {
  1627. InitializeHdcOnlyUse:
  1628. // we don't support direct rendering to this type of
  1629. // surface format. Do everything through GDI HDC.
  1630. driver = Globals::GdiDriver;
  1631. bitmap->InitializeForGdiBitmap(Globals::DesktopDevice,
  1632. width,
  1633. height);
  1634. g->Type = GraphicsBitmap;
  1635. g->Driver = driver;
  1636. g->Context->Hdc = hdc;
  1637. g->ResetState(0, 0, 1, 1);
  1638. }
  1639. else
  1640. {
  1641. DIBSECTION dibSec;
  1642. dibSec.dsBm.bmType = 0;
  1643. dibSec.dsBm.bmWidth = surfObj->sizlBitmap.cx;
  1644. if (surfObj->lDelta < 0)
  1645. {
  1646. // bits pointer at top of frame buffer (scans down)
  1647. dibSec.dsBm.bmWidthBytes = -surfObj->lDelta;
  1648. dibSec.dsBm.bmHeight = -surfObj->sizlBitmap.cy;
  1649. }
  1650. else
  1651. {
  1652. // bits pointer at base of frame buffer (scans up)
  1653. dibSec.dsBm.bmWidthBytes = surfObj->lDelta;
  1654. dibSec.dsBm.bmHeight = surfObj->sizlBitmap.cy;
  1655. }
  1656. dibSec.dsBm.bmPlanes = 1;
  1657. dibSec.dsBm.bmBitsPixel = PixelCount[surfObj->iBitmapFormat-1];
  1658. dibSec.dsBmih.biSize = sizeof(BITMAPINFOHEADER);
  1659. dibSec.dsBmih.biWidth = width;
  1660. dibSec.dsBmih.biHeight = height;
  1661. dibSec.dsBmih.biPlanes = 1;
  1662. dibSec.dsBmih.biBitCount = PixelCount[surfObj->iBitmapFormat-1];
  1663. dibSec.dsBmih.biCompression = BI_BITFIELDS;
  1664. dibSec.dsBmih.biSize = 0;
  1665. dibSec.dsBitfields[0] = 0x000000FF;
  1666. dibSec.dsBitfields[1] = 0x0000FF00;
  1667. dibSec.dsBitfields[2] = 0x00FF0000;
  1668. if (bitmap->InitializeForDibsection( hdc,
  1669. (HBITMAP) NULL,
  1670. Globals::DesktopDevice,
  1671. &dibSec,
  1672. &width,
  1673. &height,
  1674. &driver) == FALSE)
  1675. goto InitializeHdcOnlyUse;
  1676. // Init Valid now so later calls won't fail
  1677. g->Type = GraphicsBitmap;
  1678. g->Driver = driver;
  1679. g->Context->Hdc = hdc;
  1680. // How do we clip and map to correct band?
  1681. // GDI has set the WorldToContainer transform to translate the
  1682. // correct band to position (0,0) on the device surface. So we
  1683. // clip the size of the band relative to the surface. The image
  1684. // is mapped via the transform into this clipped region.
  1685. // set visible client clip region for surface
  1686. g->ResetState(0, 0, // bandClip->left, bandClip->top,
  1687. bandClip->right - bandClip->left,
  1688. bandClip->bottom - bandClip->top);
  1689. // Set the destination Graphics to represent device co-ordinates
  1690. g->SetPageTransform(UnitPixel);
  1691. if (g->InheritAppClippingAndTransform(hdc) == Ok)
  1692. {
  1693. return(g);
  1694. }
  1695. else
  1696. {
  1697. delete g;
  1698. }
  1699. }
  1700. }
  1701. }
  1702. return(NULL);
  1703. }
  1704. GpGraphics*
  1705. GpGraphics::GetFromGdiEmfDC(
  1706. HDC hdc
  1707. )
  1708. {
  1709. ASSERT (hdc != NULL);
  1710. DpBitmap *bitmap = new DpBitmap(hdc); // initializes Dpi
  1711. if (CheckValid(bitmap))
  1712. {
  1713. bitmap->InitializeForMetafile(Globals::DesktopDevice);
  1714. GpGraphics *g = new GpGraphics(bitmap);
  1715. if (g)
  1716. {
  1717. g->Type = GraphicsMetafile;
  1718. g->DownLevel = TRUE;
  1719. g->Driver = Globals::MetaDriver;
  1720. g->Context->Hdc = hdc;
  1721. g->Context->IsEmfPlusHdc = TRUE;
  1722. g->ResetState(0, 0, 1, 1);
  1723. // Override some state, as we don't want anything to be
  1724. // clipped out of a metafile, unless there is clipping
  1725. // in the hdc.
  1726. g->WindowClip.SetInfinite();
  1727. g->Context->ContainerClip.SetInfinite();
  1728. g->Context->VisibleClip.SetInfinite();
  1729. if (g->InheritAppClippingAndTransform(hdc) == Ok)
  1730. {
  1731. return g;
  1732. }
  1733. delete g;
  1734. }
  1735. }
  1736. return NULL;
  1737. }
  1738. /**************************************************************************\
  1739. *
  1740. * Function Description:
  1741. *
  1742. *
  1743. * Arguments:
  1744. *
  1745. *
  1746. * Return Value:
  1747. *
  1748. *
  1749. * History:
  1750. *
  1751. *
  1752. \**************************************************************************/
  1753. GpGraphics*
  1754. GpGraphics::GetForMetafile(
  1755. IMetafileRecord * metafile,
  1756. EmfType type,
  1757. HDC hdc
  1758. )
  1759. {
  1760. ASSERT ((metafile != NULL) && (hdc != NULL));
  1761. DpBitmap *bitmap = new DpBitmap(hdc); // initializes Dpi
  1762. if (CheckValid(bitmap))
  1763. {
  1764. bitmap->InitializeForMetafile(Globals::DesktopDevice);
  1765. GpGraphics *g = new GpGraphics(bitmap);
  1766. if (g)
  1767. {
  1768. g->Type = GraphicsMetafile;
  1769. g->Metafile = metafile;
  1770. g->DownLevel = (type != EmfTypeEmfPlusOnly);
  1771. g->Driver = Globals::MetaDriver;
  1772. g->Context->Hdc = hdc;
  1773. g->Context->IsEmfPlusHdc = TRUE;
  1774. g->ResetState(0, 0, 1, 1);
  1775. // Override some state, as we don't want anything to be
  1776. // clipped out of a metafile
  1777. g->WindowClip.SetInfinite();
  1778. g->Context->ContainerClip.SetInfinite();
  1779. g->Context->VisibleClip.SetInfinite();
  1780. return(g);
  1781. }
  1782. }
  1783. return(NULL);
  1784. }
  1785. /**************************************************************************\
  1786. *
  1787. * Function Description:
  1788. *
  1789. * Create a GpGraphics class from a DC.
  1790. *
  1791. * Arguments:
  1792. *
  1793. * [IN] hdc - Specifies the DC.
  1794. *
  1795. * Return Value:
  1796. *
  1797. * NULL if failure (such as with an invalid DC).
  1798. *
  1799. * History:
  1800. *
  1801. * 12/04/1998 andrewgo
  1802. * Created it.
  1803. *
  1804. \**************************************************************************/
  1805. GpGraphics*
  1806. GpGraphics::GetFromHdc(
  1807. HDC hdc,
  1808. HANDLE hDevice
  1809. )
  1810. {
  1811. GpGraphics *g = NULL;
  1812. // GetObjectType is nice and fast (entirely user-mode on NT):
  1813. switch (GetDCType(hdc))
  1814. {
  1815. case OBJ_DC:
  1816. if (GetDeviceCaps(hdc, TECHNOLOGY) == DT_RASPRINTER )
  1817. {
  1818. g = GpGraphics::GetFromGdiPrinterDC(hdc, hDevice);
  1819. }
  1820. else
  1821. {
  1822. g = GpGraphics::GetFromGdiScreenDC(hdc);
  1823. }
  1824. break;
  1825. case OBJ_MEMDC:
  1826. g = GpGraphics::GetFromGdiBitmap(hdc);
  1827. break;
  1828. case OBJ_ENHMETADC:
  1829. // When metafile spooling, the printer DC will be of type
  1830. // OBJ_ENHMETADC on Win9x and NT4 (but not NT5 due to a fix
  1831. // to NT bug 98810). We need to do some more work to figure
  1832. // out whether it's really a printer DC or a true metafile
  1833. // DC:
  1834. BOOL printDC;
  1835. { // FPU Sandbox for potentially unsafe FPU code.
  1836. FPUStateSandbox fpsb;
  1837. printDC = Globals::GdiIsMetaPrintDCFunction(hdc);
  1838. } // FPU Sandbox for potentially unsafe FPU code.
  1839. if (printDC)
  1840. {
  1841. g = GpGraphics::GetFromGdiPrinterDC(hdc, hDevice);
  1842. }
  1843. else
  1844. {
  1845. g = GpGraphics::GetFromGdiEmfDC(hdc);
  1846. }
  1847. break;
  1848. case OBJ_METADC:
  1849. TERSE(("16-bit metafile DC support not yet implemented"));
  1850. break;
  1851. }
  1852. return(g);
  1853. }
  1854. /**************************************************************************\
  1855. *
  1856. * Function Description:
  1857. *
  1858. * Dispose of a GpGraphics object
  1859. *
  1860. * Arguments:
  1861. *
  1862. * NONE
  1863. *
  1864. * Return Value:
  1865. *
  1866. * NONE
  1867. *
  1868. \**************************************************************************/
  1869. GpGraphics::~GpGraphics()
  1870. {
  1871. // How do we ensure that no one is using the graphics when we are here?
  1872. // Flush any pending drawing commands before we go out of scope:
  1873. Flush(FlushIntentionFlush);
  1874. if (IsPrinter())
  1875. {
  1876. EndPrinterEMF();
  1877. if (PrintInit)
  1878. delete PrintInit;
  1879. }
  1880. BOOL doResetHdc = TRUE;
  1881. // Handle Graphics-type specific functionality:
  1882. switch (Type)
  1883. {
  1884. case GraphicsMetafile:
  1885. if (Metafile != NULL)
  1886. {
  1887. Metafile->EndRecording();
  1888. doResetHdc = FALSE; // EndRecording closes the metafile HDC
  1889. }
  1890. // FALLTHRU
  1891. case GraphicsBitmap:
  1892. // if this is created on a GdipBitmap, dec the ref count
  1893. // delete the bitmap if ref count <= 0
  1894. if (GdipBitmap)
  1895. {
  1896. GdipBitmap->Dispose();
  1897. }
  1898. // We have to delete the temporary surface that we created:
  1899. delete Surface;
  1900. break;
  1901. }
  1902. // Restore the DC that we were derived from (if any).
  1903. // We must NOT do this before the call to EndRecording because
  1904. // EndRecording needs the context->Hdc to be in the saved state
  1905. // so that the transforms are still reset like GDI+ requires.
  1906. if (doResetHdc)
  1907. {
  1908. Context->ResetHdc();
  1909. }
  1910. // Free any device and driver objects that had to be created only for the
  1911. // lifespan of the Graphics object:
  1912. if (CreatedDevice)
  1913. {
  1914. delete Driver;
  1915. delete Device;
  1916. }
  1917. SetValid(FALSE); // so we don't use a deleted object
  1918. }
  1919. /**************************************************************************\
  1920. *
  1921. * Function Description:
  1922. *
  1923. * Return a GDI DC handle associated with the current graphics context.
  1924. *
  1925. * Arguments:
  1926. *
  1927. * NONE
  1928. *
  1929. * Return Value:
  1930. *
  1931. * GDI DC handle associated with the current graphics context
  1932. * NULL if there is an error
  1933. *
  1934. * NOTE: We assume the caller has already obtained a lock on the
  1935. * current graphics context.
  1936. *
  1937. * NOTE: This function does not return a clean DC! That is, expect it
  1938. * to have a funky transform, weird ROP mode, etc. If you want
  1939. * to use this internally, you should probably call Context->GetHdc()
  1940. * directly.
  1941. *
  1942. \**************************************************************************/
  1943. HDC
  1944. GpGraphics::GetHdc()
  1945. {
  1946. // We must flush the output of the Graphics before returning an HDC.
  1947. this->Flush(FlushIntentionFlush);
  1948. HDC hdc = NULL;
  1949. if (Context->Hdc)
  1950. {
  1951. // If the Graphics was originally derived from an HDC, we simply
  1952. // return back the original HDC (this avoids some issues as to
  1953. // what to do about inherited transforms, etc.). We may have
  1954. // mucked with some of the DC state, though, so reset it back to
  1955. // what it was originally:
  1956. Context->ResetHdc();
  1957. hdc = Context->Hdc;
  1958. }
  1959. else if (Context->Hwnd)
  1960. {
  1961. // The Graphics was originally derived from an HWND:
  1962. hdc = GetDC(Context->Hwnd);
  1963. }
  1964. else if (Surface && (Surface->Type == DpBitmap::CreationType::GPBITMAP))
  1965. {
  1966. // The GpBitmap is accessible from the EpScanBitmap. It will
  1967. // create an HDC and GDI bitmap appropriate for interop.
  1968. EpScanBitmap *scan = static_cast<EpScanBitmap*>(Surface->Scan);
  1969. hdc = scan->GetBitmap()->GetHdc();
  1970. }
  1971. if (IsRecording() && (hdc != NULL))
  1972. {
  1973. if (IsPrinter())
  1974. {
  1975. EndPrinterEMF();
  1976. }
  1977. else
  1978. {
  1979. GpStatus status = Metafile->RecordGetDC();
  1980. }
  1981. }
  1982. return(hdc);
  1983. }
  1984. /**************************************************************************\
  1985. *
  1986. * Function Description:
  1987. *
  1988. * Release the GDI DC handle associated with the current graphics context.
  1989. *
  1990. * Arguments:
  1991. *
  1992. * hdc - GDI DC handle returned by a previous GetHdc() call
  1993. *
  1994. * Return Value:
  1995. *
  1996. * NONE
  1997. *
  1998. * Notes:
  1999. *
  2000. * We assume the caller has already obtained a lock on the
  2001. * current graphics context.
  2002. *
  2003. \**************************************************************************/
  2004. VOID
  2005. GpGraphics::ReleaseHdc(
  2006. HDC hdc
  2007. )
  2008. {
  2009. if (Context->Hdc)
  2010. {
  2011. // The Graphics was originally derived from an HDC. We don't
  2012. // have to do anything here; ResetHdc() already marked the
  2013. // DC as dirty.
  2014. }
  2015. else if (Context->Hwnd)
  2016. {
  2017. // The Graphics was originally derived from an HWND:
  2018. ReleaseDC(Context->Hwnd, hdc);
  2019. }
  2020. else if (Surface && (Surface->Type == DpBitmap::CreationType::GPBITMAP))
  2021. {
  2022. // The GpBitmap is accessible from the EpScanBitmap.
  2023. EpScanBitmap *scan = static_cast<EpScanBitmap*>(Surface->Scan);
  2024. scan->GetBitmap()->ReleaseHdc(hdc);
  2025. }
  2026. if (IsRecording() && IsPrinter())
  2027. {
  2028. StartPrinterEMF();
  2029. }
  2030. }
  2031. /**************************************************************************\
  2032. *
  2033. * Function Description:
  2034. *
  2035. * Save (push) the graphics context state. Return the ID of the current
  2036. * state (before the push) for the app to restore to later on.
  2037. *
  2038. * Arguments:
  2039. *
  2040. * NONE
  2041. *
  2042. * Return Value:
  2043. *
  2044. * gstate - the state to restore the context to later
  2045. *
  2046. * Created:
  2047. *
  2048. * 3/4/1999 DCurtis
  2049. *
  2050. \**************************************************************************/
  2051. INT
  2052. GpGraphics::Save()
  2053. {
  2054. DpContext * newContext = new DpContext(Context);
  2055. if ((newContext != NULL) &&
  2056. (newContext->AppClip.Set(&(Context->AppClip), TRUE) == Ok) &&
  2057. (newContext->ContainerClip.Set(&(Context->ContainerClip), TRUE)
  2058. == Ok) &&
  2059. (newContext->VisibleClip.Set(&(Context->VisibleClip), TRUE) == Ok))
  2060. {
  2061. INT gstate = newContext->Id;
  2062. newContext->InverseOk = Context->InverseOk;
  2063. newContext->PageUnit = Context->PageUnit;
  2064. newContext->PageScale = Context->PageScale;
  2065. newContext->PageMultiplierX = Context->PageMultiplierX;
  2066. newContext->PageMultiplierY = Context->PageMultiplierY;
  2067. newContext->WorldToPage = Context->WorldToPage;
  2068. newContext->ContainerToDevice = Context->ContainerToDevice;
  2069. newContext->WorldToDevice = Context->WorldToDevice;
  2070. newContext->DeviceToWorld = Context->DeviceToWorld;
  2071. newContext->IcmMode = Context->IcmMode;
  2072. newContext->GdiLayered = Context->GdiLayered;
  2073. Context->Next = newContext;
  2074. Context = newContext;
  2075. if (IsRecording())
  2076. {
  2077. GpStatus status = Metafile->RecordSave(gstate);
  2078. if (status != Ok)
  2079. {
  2080. SetValid(FALSE); // Prevent any more recording
  2081. }
  2082. }
  2083. return gstate;
  2084. }
  2085. delete newContext;
  2086. return 0;
  2087. }
  2088. #define CONTAINER_ID 0x00008000
  2089. /**************************************************************************\
  2090. *
  2091. * Function Description:
  2092. *
  2093. * Restore (pop) the context to the state before the specified one.
  2094. *
  2095. * Arguments:
  2096. *
  2097. * gstate - the pushed state (restore to state before this)
  2098. *
  2099. * Return Value:
  2100. *
  2101. * NONE
  2102. *
  2103. * Created:
  2104. *
  2105. * 3/4/1999 DCurtis
  2106. *
  2107. \**************************************************************************/
  2108. VOID
  2109. GpGraphics::Restore(
  2110. INT gstate
  2111. )
  2112. {
  2113. DpContext * cur = Context;
  2114. DpContext * prev;
  2115. for (;;)
  2116. {
  2117. if ((prev = cur->Prev) == NULL)
  2118. {
  2119. return;
  2120. }
  2121. if (cur->Id == (UINT)gstate)
  2122. {
  2123. // don't double record EndContainer calls
  2124. if (IsRecording() && ((gstate & CONTAINER_ID) == 0))
  2125. {
  2126. GpStatus status = Metafile->RecordRestore(gstate);
  2127. if (status != Ok)
  2128. {
  2129. SetValid(FALSE); // Prevent any more recording
  2130. }
  2131. }
  2132. prev->Next = NULL;
  2133. prev->SaveDc = cur->SaveDc;
  2134. Context = prev;
  2135. delete cur;
  2136. return;
  2137. }
  2138. cur = prev;
  2139. }
  2140. }
  2141. /**************************************************************************\
  2142. *
  2143. * Function Description:
  2144. *
  2145. * End a container. Restores the state back to what it was before the
  2146. * container was started. The CONTAINER_ID bit is used to make sure that
  2147. * Restore is not used with BeginContainer and that EndContainer is not
  2148. * used with Save.
  2149. *
  2150. * Arguments:
  2151. *
  2152. * [IN] containerState - the pushed container state
  2153. *
  2154. * Return Value:
  2155. *
  2156. * NONE
  2157. *
  2158. * Created:
  2159. *
  2160. * 4/7/1999 DCurtis
  2161. *
  2162. \**************************************************************************/
  2163. VOID
  2164. GpGraphics::EndContainer(
  2165. INT containerState
  2166. )
  2167. {
  2168. if (IsRecording())
  2169. {
  2170. GpStatus status = Metafile->RecordEndContainer(containerState);
  2171. if (status != Ok)
  2172. {
  2173. SetValid(FALSE); // Prevent any more recording
  2174. }
  2175. }
  2176. Restore(containerState | CONTAINER_ID);
  2177. }
  2178. /**************************************************************************\
  2179. *
  2180. * Function Description:
  2181. *
  2182. * Begin a container. This sets the container transform and the
  2183. * container clip based on the current transform and the current clip.
  2184. *
  2185. * We have to have a container transform for 2 reasons:
  2186. * 1) If we tried to do it in the world transform, then a call
  2187. * to (Re)SetWorldTransform would erase the container transform.
  2188. *
  2189. * 2) We have APIs for setting the text size and the line width that
  2190. * are based on the page units, so they are not affected by the
  2191. * world transform, but they are affected by the container transform.
  2192. *
  2193. * Arguments:
  2194. *
  2195. * NONE
  2196. *
  2197. * Return Value:
  2198. *
  2199. * gstate - the state to restore the context to later
  2200. *
  2201. * Created:
  2202. *
  2203. * 3/9/1999 DCurtis
  2204. *
  2205. \**************************************************************************/
  2206. INT
  2207. GpGraphics::BeginContainer(
  2208. const GpRectF & destRect,
  2209. const GpRectF & srcRect,
  2210. GpPageUnit srcUnit,
  2211. REAL srcDpiX, // only set by metafile enumeration
  2212. REAL srcDpiY,
  2213. BOOL srcIsDisplay
  2214. )
  2215. {
  2216. GpMatrix identityMatrix;
  2217. DpContext * newContext = new DpContext(Context);
  2218. if (newContext == NULL)
  2219. {
  2220. return 0;
  2221. }
  2222. // leave newContext->AppClip set to identity
  2223. if ((Context->AppClip.UpdateDeviceRegion(&identityMatrix) == Ok) &&
  2224. (newContext->ContainerClip.Set(&(Context->AppClip.DeviceRegion),
  2225. TRUE) == Ok) &&
  2226. (newContext->ContainerClip.And(&(Context->ContainerClip)) == Ok) &&
  2227. (newContext->VisibleClip.Set(&(Context->VisibleClip), TRUE) == Ok))
  2228. {
  2229. REAL unitMultiplierX;
  2230. REAL unitMultiplierY;
  2231. GpRectF deviceSrc;
  2232. newContext->GetPageMultipliers(&unitMultiplierX, &unitMultiplierY,
  2233. srcUnit);
  2234. deviceSrc.X = unitMultiplierX * srcRect.X;
  2235. deviceSrc.Y = unitMultiplierY * srcRect.Y;
  2236. deviceSrc.Width = unitMultiplierX * srcRect.Width;
  2237. deviceSrc.Height = unitMultiplierY * srcRect.Height;
  2238. if (newContext->ContainerToDevice.InferAffineMatrix(
  2239. destRect, deviceSrc) == Ok)
  2240. {
  2241. newContext->AntiAliasMode = 0;
  2242. newContext->TextRenderHint = TextRenderingHintSystemDefault;
  2243. newContext->TextContrast = DEFAULT_TEXT_CONTRAST;
  2244. newContext->CompositingMode = CompositingModeSourceOver;
  2245. newContext->CompositingQuality = CompositingQualityDefault;
  2246. newContext->FilterType = InterpolationModeDefaultInternal;
  2247. newContext->PixelOffset = PixelOffsetModeDefault;
  2248. // Note that the world to device transform includes the previous
  2249. // container to device transform.
  2250. newContext->ContainerToDevice.Append(Context->WorldToDevice);
  2251. newContext->InverseOk = FALSE;
  2252. newContext->PageUnit = UnitDisplay;
  2253. newContext->PageScale = 1.0f;
  2254. if ((srcDpiX > 0.0f) && (srcDpiY > 0.0f))
  2255. {
  2256. // When playing a metafile, we have to guarantee that
  2257. // a unit inch is played now as it would have been
  2258. // when it was recorded. For example, if we recorded
  2259. // the metafile at 300 dpi, then an inch was 300 pixels.
  2260. // Even if we're playing it back to a 96-dpi display,
  2261. // that metafile inch must still be transformed to
  2262. // 300 pixels before going throught the container
  2263. // transformation, so that all graphics are scaled
  2264. // the same, whether pixel units or some other units.
  2265. newContext->ContainerDpiX = srcDpiX;
  2266. newContext->ContainerDpiY = srcDpiY;
  2267. newContext->IsDisplay = srcIsDisplay;
  2268. }
  2269. newContext->GetPageMultipliers();
  2270. newContext->WorldToPage.Reset();
  2271. // Have to inherit the ICM and layering state:
  2272. newContext->IcmMode = Context->IcmMode;
  2273. newContext->GdiLayered = Context->GdiLayered;
  2274. INT containerState = newContext->Id;
  2275. newContext->Id |= CONTAINER_ID;
  2276. Context->Next = newContext;
  2277. Context = newContext;
  2278. if (IsRecording())
  2279. {
  2280. GpStatus status = Metafile->RecordBeginContainer(destRect,
  2281. srcRect, srcUnit, containerState);
  2282. if (status != Ok)
  2283. {
  2284. SetValid(FALSE); // Prevent any more recording
  2285. }
  2286. }
  2287. // Do this after switching over the context!
  2288. Context->UpdateWorldToDeviceMatrix();
  2289. return containerState;
  2290. }
  2291. }
  2292. delete newContext;
  2293. return 0;
  2294. }
  2295. /**************************************************************************\
  2296. *
  2297. * Function Description:
  2298. *
  2299. * Begin a container. This sets the container transform and the
  2300. * container clip based on the current transform and the current clip.
  2301. *
  2302. * We have to have a container transform for 2 reasons:
  2303. * 1) If we tried to do it in the world transform, then a call
  2304. * to (Re)SetWorldTransform would erase the container transform.
  2305. *
  2306. * 2) We have APIs for setting the text size and the line width that
  2307. * are based on the page units, so they are not affected by the
  2308. * world transform, but they are affected by the container transform.
  2309. *
  2310. * Arguments:
  2311. *
  2312. * NONE
  2313. *
  2314. * Return Value:
  2315. *
  2316. * gstate - the state to restore the context to later
  2317. *
  2318. * Created:
  2319. *
  2320. * 3/9/1999 DCurtis
  2321. *
  2322. \**************************************************************************/
  2323. INT
  2324. GpGraphics::BeginContainer(
  2325. BOOL forceIdentityTransform, // only set by metafile player
  2326. REAL srcDpiX,
  2327. REAL srcDpiY,
  2328. BOOL srcIsDisplay
  2329. )
  2330. {
  2331. GpMatrix identityMatrix;
  2332. DpContext * newContext = new DpContext(Context);
  2333. if (newContext == NULL)
  2334. {
  2335. return 0;
  2336. }
  2337. // leave newContext->AppClip set to identity
  2338. if ((Context->AppClip.UpdateDeviceRegion(&identityMatrix) == Ok) &&
  2339. (newContext->ContainerClip.Set(&(Context->AppClip.DeviceRegion),
  2340. TRUE) == Ok) &&
  2341. (newContext->ContainerClip.And(&(Context->ContainerClip)) == Ok) &&
  2342. (newContext->VisibleClip.Set(&(Context->VisibleClip), TRUE) == Ok))
  2343. {
  2344. // Note that the world to device transform includes the previous
  2345. // container to device transform.
  2346. GpMatrix worldToDevice = Context->WorldToDevice;
  2347. // We append the world to device transform below, which already
  2348. // has the container transform in it. We don't want to apply
  2349. // the same transform twice, so we need to reset the container
  2350. // transform here.
  2351. newContext->ContainerToDevice.Reset();
  2352. // When playing a GDI+ metafile into another metafile, we have to guarantee
  2353. // that the transform is the identity so that the GDI+ records don't get
  2354. // transformed by GDI+ and then get transformed again by GDI.
  2355. if (forceIdentityTransform)
  2356. {
  2357. worldToDevice.Reset();
  2358. }
  2359. else
  2360. {
  2361. // The coordinates that the container transform is expecting are
  2362. // world coordinates, but they will already have gone through
  2363. // the new page to device transform, so convert from device
  2364. // units back to page units before running them through the
  2365. // the container to device transform.
  2366. // In the routine above, this is done through an inferred transform
  2367. // between device unit src rect and world unit dest rect.
  2368. newContext->ContainerToDevice.Scale(1.0f / Context->PageMultiplierX,
  2369. 1.0f / Context->PageMultiplierY);
  2370. }
  2371. newContext->AntiAliasMode = 0;
  2372. newContext->TextRenderHint = TextRenderingHintSystemDefault;
  2373. newContext->TextContrast = DEFAULT_TEXT_CONTRAST;
  2374. newContext->CompositingMode = CompositingModeSourceOver;
  2375. newContext->CompositingQuality = CompositingQualityDefault;
  2376. newContext->FilterType = InterpolationModeDefaultInternal;
  2377. newContext->PixelOffset = PixelOffsetModeDefault;
  2378. newContext->ContainerToDevice.Append(worldToDevice);
  2379. newContext->InverseOk = FALSE;
  2380. newContext->PageUnit = UnitDisplay;
  2381. newContext->PageScale = 1.0f;
  2382. if ((srcDpiX > 0.0f) && (srcDpiY > 0.0f))
  2383. {
  2384. // When playing a metafile, we have to guarantee that
  2385. // a unit inch is played now as it would have been
  2386. // when it was recorded. For example, if we recorded
  2387. // the metafile at 300 dpi, then an inch was 300 pixels.
  2388. // Even if we're playing it back to a 96-dpi display,
  2389. // that metafile inch must still be transformed to
  2390. // 300 pixels before going throught the container
  2391. // transformation, so that all graphics are scaled
  2392. // the same, whether pixel units or some other units.
  2393. newContext->ContainerDpiX = srcDpiX;
  2394. newContext->ContainerDpiY = srcDpiY;
  2395. newContext->IsDisplay = srcIsDisplay;
  2396. }
  2397. newContext->GetPageMultipliers();
  2398. newContext->WorldToPage.Reset();
  2399. // Have to inherit the ICM and layering state:
  2400. newContext->IcmMode = Context->IcmMode;
  2401. newContext->GdiLayered = Context->GdiLayered;
  2402. INT containerState = newContext->Id;
  2403. newContext->Id |= CONTAINER_ID;
  2404. Context->Next = newContext;
  2405. Context = newContext;
  2406. if (IsRecording())
  2407. {
  2408. GpStatus status = Metafile->RecordBeginContainer(containerState);
  2409. if (status != Ok)
  2410. {
  2411. SetValid(FALSE); // Prevent any more recording
  2412. }
  2413. }
  2414. // Do this after switching over the context!
  2415. Context->UpdateWorldToDeviceMatrix();
  2416. return containerState;
  2417. }
  2418. delete newContext;
  2419. return 0;
  2420. }