home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_02 / HTMLDrv / HTMLDrv.cpp < prev    next >
C/C++ Source or Header  |  2000-05-11  |  13KB  |  448 lines

  1. //-----------------------------------------------------------------------------------//
  2. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  3. //                             ISBN  0-13-086985-6                                   //
  4. //                                                                                   //
  5. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  6. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  7. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  8. //                                                                                   //
  9. //  FileName   : htmldrv.cpp                                                         //
  10. //  Description: HTML printer driver entry points, Chapter 2                         //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define _WIN32_WINNT 0x0500 
  15. #define  WINVER      0x0500 
  16. #define NOCRYPT
  17.  
  18. #define UNICODE
  19. #define _UNICODE
  20. #define _X86_ 1
  21. #define WINNT 1 
  22.  
  23. #include <windows.h>
  24.  
  25. #pragma message("Add DDK to VC include and library directories")
  26.  
  27. #include <winddi.h>
  28.  
  29. #include "Device.h"
  30.  
  31. #pragma comment(lib, "win32k.lib")
  32.  
  33. const DRVFN DDI_Hooks[] =
  34. {
  35.     INDEX_DrvEnablePDEV,       (PFN) DrvEnablePDEV,
  36.     INDEX_DrvCompletePDEV,     (PFN) DrvCompletePDEV,
  37.     INDEX_DrvResetPDEV,        (PFN) DrvResetPDEV,
  38.     INDEX_DrvDisablePDEV,      (PFN) DrvDisablePDEV,
  39.     INDEX_DrvEnableSurface,    (PFN) DrvEnableSurface,
  40.     INDEX_DrvDisableSurface,   (PFN) DrvDisableSurface,
  41.  
  42.     INDEX_DrvStartDoc,         (PFN) DrvStartDoc,
  43.     INDEX_DrvEndDoc,           (PFN) DrvEndDoc,
  44.     INDEX_DrvStartPage,        (PFN) DrvStartPage,
  45.     INDEX_DrvSendPage,         (PFN) DrvSendPage,
  46.  
  47.     INDEX_DrvStrokePath,       (PFN) DrvStrokePath,
  48.     INDEX_DrvFillPath,         (PFN) DrvFillPath,
  49.     INDEX_DrvStrokeAndFillPath,(PFN) DrvStrokeAndFillPath,
  50.     INDEX_DrvLineTo,           (PFN) DrvLineTo,
  51.     INDEX_DrvPaint,            (PFN) DrvPaint,
  52.     INDEX_DrvBitBlt,           (PFN) DrvBitBlt,
  53.     INDEX_DrvCopyBits,         (PFN) DrvCopyBits,
  54.     INDEX_DrvStretchBlt,       (PFN) DrvStretchBlt,
  55.     INDEX_DrvTextOut,          (PFN) DrvTextOut
  56. };
  57.  
  58. #define PaperWidth   85
  59. #define PaperHeight 110
  60. #define Dpi             96
  61.  
  62.  
  63. BOOL APIENTRY DrvEnableDriver(ULONG iEngineVersion, ULONG cj, DRVENABLEDATA *pded)
  64. {
  65.     // Validate parameters.
  66.     if (iEngineVersion < DDI_DRIVER_VERSION)
  67.     {
  68.         EngSetLastError(ERROR_BAD_DRIVER_LEVEL);
  69.         return FALSE;
  70.     }
  71.  
  72.     if (cj < sizeof(DRVENABLEDATA))
  73.     {
  74.         EngSetLastError(ERROR_INVALID_PARAMETER);
  75.         return FALSE;
  76.     }
  77.  
  78.     pded->iDriverVersion = DDI_DRIVER_VERSION;
  79.     pded->c              = sizeof(DDI_Hooks) / sizeof(DDI_Hooks[0]);
  80.     pded->pdrvfn         = (DRVFN *) DDI_Hooks;
  81.  
  82.     return TRUE;
  83. }
  84.  
  85.  
  86. void APIENTRY DrvDisableDriver(void)
  87. {
  88. }
  89.  
  90.  
  91. DHPDEV APIENTRY DrvEnablePDEV(DEVMODEW *pdm,
  92.               LPWSTR    pwszLogAddress,
  93.               ULONG     cPat,
  94.               HSURF    *phsurfPatterns,
  95.               ULONG     cjCaps,
  96.               ULONG    *pdevcaps,
  97.               ULONG     cjDevInfo,
  98.               DEVINFO  *pdi,
  99.               HDEV      hdev,
  100.               PWSTR     pwszDeviceName,
  101.               HANDLE    hDriver)
  102. {
  103.     if ( (cjCaps<sizeof(GDIINFO)) || (cjDevInfo<sizeof(DEVINFO)) )
  104.     {
  105.         EngSetLastError(ERROR_INVALID_PARAMETER);
  106.         return FALSE;
  107.     }
  108.  
  109.     KDevice * pDevice;
  110.     
  111.     // Allocate physical device object. Tag = HTMD
  112.     pDevice = (KDevice *) EngAllocMem (FL_ZERO_MEMORY, sizeof(KDevice), 'DMTH');
  113.     if (pDevice == NULL)
  114.     {
  115.         EngSetLastError(ERROR_OUTOFMEMORY);
  116.         return NULL;
  117.     }
  118.  
  119.     pDevice->Create();
  120.  
  121.     pDevice->hSpooler   = hDriver;
  122.     pDevice->hPalette   = EngCreatePalette (PAL_BGR, 0, 0, 0, 0, 0);
  123.  
  124.     if (pdm == NULL || pdm->dmOrientation == DMORIENT_PORTRAIT)
  125.     {
  126.         pDevice->width  = PaperWidth;
  127.         pDevice->height = PaperHeight;
  128.     }
  129.     else
  130.     {
  131.         pDevice->width  = PaperHeight;
  132.         pDevice->height = PaperWidth;
  133.     }
  134.  
  135.     // GDIINFO initialized by GDI before calling DrvEnablePDEV
  136.     PGDIINFO pgdiinfo = (PGDIINFO) pdevcaps;
  137.     
  138.     memset(pgdiinfo, 0, sizeof(GDIINFO));
  139.  
  140.     pgdiinfo->ulVersion    = GDI_DRIVER_VERSION;
  141.     pgdiinfo->ulTechnology = DT_RASPRINTER;
  142.     pgdiinfo->ulHorzSize   = pDevice->width  * 254 / 10;
  143.     pgdiinfo->ulVertSize   = pDevice->height * 254 / 10;
  144.     pgdiinfo->ulHorzRes    = pDevice->width  * Dpi / 10;
  145.     pgdiinfo->ulVertRes    = pDevice->height * Dpi / 10;
  146.  
  147.     pgdiinfo->cBitsPixel   = 24;
  148.     pgdiinfo->cPlanes      = 1;
  149.     pgdiinfo->ulNumColors  = 512;
  150.     pgdiinfo->flRaster     = 0;
  151.     pgdiinfo->ulLogPixelsX = Dpi;
  152.     pgdiinfo->ulLogPixelsY = Dpi;
  153.  
  154.     pgdiinfo->ulAspectX    = 1000;
  155.     pgdiinfo->ulAspectY    = 1000;
  156.     pgdiinfo->ulAspectXY   = 1414;  
  157.     
  158.     pgdiinfo->xStyleStep   = 1;
  159.     pgdiinfo->yStyleStep   = 1;
  160.     pgdiinfo->denStyleStep = 10;
  161.     
  162.     pgdiinfo->ptlPhysOffset.x = Dpi / 4; // 0.25 inch
  163.     pgdiinfo->ptlPhysOffset.y = Dpi / 4; // 0.25 inch
  164.     pgdiinfo->szlPhysSize.cx  = pDevice->width  * Dpi / 10;
  165.     pgdiinfo->szlPhysSize.cy  = pDevice->height * Dpi / 10;
  166.  
  167.     pgdiinfo->ulDevicePelsDPI = Dpi;
  168.     pgdiinfo->ulPrimaryOrder  = PRIMARY_ORDER_ABC;
  169.     pgdiinfo->ulHTPatternSize = HT_PATSIZE_DEFAULT;
  170.     pgdiinfo->ulHTOutputFormat= HT_FORMAT_24BPP;
  171.     pgdiinfo->flHTFlags       = HT_FLAG_HAS_BLACK_DYE;
  172.  
  173.     // DEVINFO, initialized by GDI, driver only set what it understands
  174.     pdi->flGraphicsCaps = GCAPS_BEZIERS | 
  175.                           GCAPS_GEOMETRICWIDE |
  176.                           GCAPS_ALTERNATEFILL |
  177.                           GCAPS_WINDINGFILL |
  178.                           GCAPS_HALFTONE |
  179.                           GCAPS_HORIZSTRIKE |
  180.                           GCAPS_VERTSTRIKE |
  181.                           GCAPS_OPAQUERECT |
  182.                           GCAPS_VECTORFONT;
  183.     pdi->iDitherFormat = BMF_24BPP;
  184.     pdi->hpalDefault   = pDevice->hPalette;
  185.     
  186.     return (DHPDEV) pDevice;
  187. }
  188.  
  189.  
  190. void APIENTRY DrvCompletePDEV(DHPDEV dhpdev, HDEV hdev)
  191. {
  192.     KDevice * pDevice = (KDevice *)dhpdev;
  193.     
  194.     pDevice->hDevice = hdev;
  195. }
  196.  
  197.  
  198. BOOL APIENTRY DrvResetPDEV(DHPDEV dhpdevOld, DHPDEV dhpdevNew)
  199. {
  200.     return TRUE;
  201. }
  202.  
  203.  
  204. void APIENTRY DrvDisablePDEV(DHPDEV dhpdev)
  205. {
  206.     KDevice * pDevice = (KDevice *)dhpdev;
  207.  
  208.     if ( pDevice->hPalette )
  209.         EngDeletePalette(pDevice->hPalette);
  210.  
  211.     EngFreeMem(pDevice);
  212. }
  213.  
  214.  
  215. HSURF APIENTRY DrvEnableSurface(DHPDEV dhpdev)
  216. {
  217.     KDevice * pDevice = (KDevice *) dhpdev;
  218.  
  219.     SIZEL sizl = { pDevice->width * Dpi / 10, pDevice->height * Dpi / 10 };
  220.     
  221.     pDevice->hSurface = (HSURF) EngCreateBitmap(sizl, sizl.cy, BMF_24BPP, 
  222.                                     BMF_NOZEROINIT,  NULL);
  223.  
  224.     if (pDevice->hSurface == NULL)
  225.         return NULL;
  226.  
  227.     EngAssociateSurface(pDevice->hSurface, pDevice->hDevice, 
  228.         HOOK_BITBLT | HOOK_STRETCHBLT | HOOK_TEXTOUT | HOOK_PAINT | 
  229.         HOOK_STROKEPATH | HOOK_FILLPATH | HOOK_STROKEANDFILLPATH | 
  230.         HOOK_COPYBITS | HOOK_LINETO);
  231.  
  232.     return pDevice->hSurface;
  233. }
  234.  
  235.  
  236. void APIENTRY DrvDisableSurface(DHPDEV dhpdev)
  237. {
  238.     KDevice * pDevice = (KDevice *)dhpdev;
  239.  
  240.     EngDeleteSurface(pDevice->hSurface);
  241.     pDevice->hSurface = NULL;
  242. }
  243.  
  244.  
  245. BOOL  APIENTRY DrvStartPage(SURFOBJ *pso)
  246. {
  247.     KDevice * pDevice = (KDevice *) pso->dhpdev;
  248.  
  249.     if ( pDevice->StartPage(& pso, 1) )
  250.     {
  251.         RECTL rect = { 0, 0, pDevice->width * Dpi / 10, pDevice->height * Dpi / 10 };
  252.         
  253.         EngEraseSurface(pso, & rect, 0xFFFFFF);
  254.  
  255.         return TRUE;
  256.     }
  257.     else
  258.         return FALSE;
  259. }
  260.  
  261. BOOL APIENTRY DrvStartDoc(SURFOBJ *pso, PWSTR pwszDocName, DWORD dwJobId)
  262. {
  263.     KDevice * pDevice = (KDevice *) pso->dhpdev;
  264.  
  265.     return pDevice->StartDoc(pwszDocName, & pso, 3);
  266. }
  267.  
  268.  
  269. BOOL APIENTRY DrvEndDoc(SURFOBJ *pso, FLONG fl)
  270. {
  271.     KDevice * pDevice = (KDevice *) pso->dhpdev;
  272.  
  273.     return pDevice->EndDoc(&pso, 2);
  274. }
  275.  
  276.  
  277. BOOL APIENTRY DrvSendPage(SURFOBJ *pso)
  278. {
  279.     KDevice * pDevice = (KDevice *) pso->dhpdev;
  280.  
  281.     if ( pDevice->SendPage(& pso, 1) )
  282.     {
  283.         pDevice->DumpSurface(pso);
  284.         
  285.         return TRUE;
  286.     }
  287.     else
  288.         return FALSE;
  289. }
  290.  
  291.  
  292. BOOL APIENTRY DrvStrokePath(SURFOBJ   *pso,
  293.                             PATHOBJ   *ppo,
  294.                             CLIPOBJ   *pco,
  295.                             XFORMOBJ  *pxo, 
  296.                             BRUSHOBJ  *pbo,
  297.                             POINTL    *pptlBrushOrg,
  298.                             LINEATTRS *plineattrs,
  299.                             MIX        mix)
  300. {
  301.     KDevice * pDevice = (KDevice *) pso->dhpdev;
  302.  
  303.     if ( pDevice->CallEngine(INDEX_DrvStrokePath, & pso, 8) )
  304.         return EngStrokePath(pso, ppo, pco, pxo, pbo, pptlBrushOrg, plineattrs, mix);
  305.     else
  306.         return FALSE;
  307. }
  308.  
  309.  
  310. BOOL APIENTRY DrvFillPath(SURFOBJ  *pso,
  311.                           PATHOBJ  *ppo,
  312.                           CLIPOBJ  *pco,
  313.                           BRUSHOBJ *pbo,
  314.                           POINTL   *pptlBrushOrg,
  315.                           MIX       mix,
  316.                           FLONG     flOptions)
  317. {
  318.     KDevice * pDevice = (KDevice *) pso->dhpdev;
  319.  
  320.     if ( pDevice->CallEngine(INDEX_DrvFillPath, & pso, 7) )
  321.         return EngFillPath(pso, ppo, pco, pbo, pptlBrushOrg, mix, flOptions);
  322.     else
  323.         return FALSE;
  324. }
  325.  
  326.  
  327. BOOL APIENTRY DrvStrokeAndFillPath(SURFOBJ *pso,PATHOBJ *ppo,CLIPOBJ *pco,
  328.                                    XFORMOBJ *pxo,BRUSHOBJ *pboStroke,
  329.                                    LINEATTRS *plineattrs,BRUSHOBJ *pboFill,
  330.                                    POINTL *pptlBrushOrg,MIX mixFill,FLONG flOptions)
  331. {
  332.     KDevice * pDevice = (KDevice *) pso->dhpdev;
  333.  
  334.     if ( pDevice->CallEngine(INDEX_DrvStrokeAndFillPath, &pso, 10) )
  335.         return EngStrokeAndFillPath(pso, ppo, pco, pxo, pboStroke, plineattrs, 
  336.                    pboFill, pptlBrushOrg, mixFill, flOptions);
  337.     else
  338.         return FALSE;
  339. }
  340.  
  341.  
  342. BOOL APIENTRY DrvLineTo(SURFOBJ *pso, CLIPOBJ *pco, BRUSHOBJ *pbo, LONG x1, 
  343.                         LONG y1, LONG x2, LONG y2, RECTL *prclBounds, MIX mix)
  344. {
  345.     KDevice * pDevice = (KDevice *) pso->dhpdev;
  346.  
  347.     if ( pDevice->CallEngine(INDEX_DrvLineTo, &pso, 9) )
  348.         return EngLineTo(pso, pco, pbo, x1, y1, x2, y2, prclBounds, mix);
  349.     else
  350.         return FALSE;
  351. }
  352.  
  353.  
  354. BOOL APIENTRY DrvPaint(SURFOBJ  *pso, 
  355.                        CLIPOBJ  *pco, 
  356.                        BRUSHOBJ *pbo,
  357.                        POINTL   *pptlBrushOrg,
  358.                        MIX       mix)
  359. {
  360.     KDevice * pDevice = (KDevice *) pso->dhpdev;
  361.  
  362.     if ( pDevice->CallEngine(INDEX_DrvPaint, & pso, 5) )
  363.         return EngPaint(pso, pco, pbo, pptlBrushOrg, mix);
  364.     else
  365.         return FALSE;
  366. }
  367.  
  368.  
  369. BOOL APIENTRY DrvBitBlt(SURFOBJ  *psoTrg,
  370.                         SURFOBJ  *psoSrc,
  371.                         SURFOBJ  *psoMask,
  372.                         CLIPOBJ  *pco,
  373.                         XLATEOBJ *pxlo,
  374.                         RECTL    *prclTrg,
  375.                         POINTL   *pptlSrc,
  376.                         POINTL   *pptlMask,
  377.                         BRUSHOBJ *pbo,
  378.                         POINTL   *pptlBrush,
  379.                         ROP4      rop4)
  380. {
  381.     KDevice * pDevice = (KDevice *) psoTrg->dhpdev;
  382.  
  383.     if ( pDevice->CallEngine(INDEX_DrvBitBlt, &psoTrg, 11) )
  384.         return EngBitBlt(psoTrg, psoSrc, psoMask, pco, pxlo, prclTrg, 
  385.                     pptlSrc, pptlMask, pbo, pptlBrush, rop4);
  386.     else
  387.         return FALSE;
  388. }
  389.  
  390.  
  391. BOOL APIENTRY DrvCopyBits(SURFOBJ  *psoDest,
  392.                           SURFOBJ  *psoSrc,
  393.                           CLIPOBJ  *pco,
  394.                           XLATEOBJ *pxlo,
  395.                           RECTL    *prclDest,
  396.                           POINTL   *pptlSrc)
  397. {
  398.     KDevice * pDevice = (KDevice *) psoDest->dhpdev;
  399.  
  400.     if (pDevice->CallEngine(INDEX_DrvCopyBits, &psoDest, 6) )
  401.         return EngCopyBits(psoDest, psoSrc, pco, pxlo, prclDest, pptlSrc);
  402.     else
  403.         return FALSE;
  404. }
  405.  
  406.  
  407. BOOL APIENTRY DrvStretchBlt(SURFOBJ         *psoDest,
  408.                             SURFOBJ         *psoSrc,
  409.                             SURFOBJ         *psoMask,
  410.                             CLIPOBJ         *pco,
  411.                             XLATEOBJ        *pxlo,
  412.                             COLORADJUSTMENT *pca,
  413.                             POINTL          *pptlHTOrg,
  414.                             RECTL           *prclDest,
  415.                             RECTL           *prclSrc,
  416.                             POINTL          *pptlMask,
  417.                             ULONG            iMode)
  418. {
  419.     KDevice * pDevice = (KDevice *) psoDest->dhpdev;
  420.  
  421.     if ( pDevice->CallEngine(INDEX_DrvStretchBlt, & psoDest, 11) )
  422.         return EngStretchBlt(psoDest, psoSrc, psoMask, pco, pxlo, pca, 
  423.                    pptlHTOrg, prclDest, prclSrc, pptlMask, iMode);
  424.     else
  425.         return FALSE;
  426. }
  427.  
  428.  
  429. BOOL APIENTRY DrvTextOut(SURFOBJ  *pso,
  430.            STROBJ   *pstro,
  431.            FONTOBJ  *pfo,
  432.            CLIPOBJ  *pco,
  433.            RECTL    *prclExtra,
  434.            RECTL    *prclOpaque,
  435.            BRUSHOBJ *pboFore,
  436.            BRUSHOBJ *pboOpaque,
  437.            POINTL   *pptlOrg,
  438.            MIX       mix)
  439. {
  440.     KDevice * pdevice = (KDevice *) pso->dhpdev;
  441.  
  442.     if ( pdevice->CallEngine(INDEX_DrvTextOut, &pso, 10) )
  443.         return EngTextOut(pso, pstro, pfo, pco, prclExtra, 
  444.                    prclOpaque, pboFore, pboOpaque, pptlOrg, mix);
  445.     else
  446.         return FALSE;
  447. }
  448.