📄 path.c
字号:
/*
* ReactOS W32 Subsystem
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* $Id: path.c 28405 2007-08-18 16:32:20Z cfinck $ */
#include <w32k.h>
#include "math.h"
#define NDEBUG
#include <debug.h>
#define NUM_ENTRIES_INITIAL 16 /* Initial size of points / flags arrays */
#define GROW_FACTOR_NUMER 2 /* Numerator of grow factor for the array */
#define GROW_FACTOR_DENOM 1 /* Denominator of grow factor */
BOOL FASTCALL PATH_AddEntry (GdiPath *pPath, const POINT *pPoint, BYTE flags);
BOOL FASTCALL PATH_AddFlatBezier (GdiPath *pPath, POINT *pt, BOOL closed);
BOOL FASTCALL PATH_DoArcPart (GdiPath *pPath, FLOAT_POINT corners[], double angleStart, double angleEnd, BOOL addMoveTo);
BOOL FASTCALL PATH_FillPath( PDC dc, GdiPath *pPath );
BOOL FASTCALL PATH_FlattenPath (GdiPath *pPath);
VOID FASTCALL PATH_NormalizePoint (FLOAT_POINT corners[], const FLOAT_POINT *pPoint, double *pX, double *pY);
BOOL FASTCALL PATH_PathToRegion (GdiPath *pPath, INT nPolyFillMode, HRGN *pHrgn);
BOOL FASTCALL PATH_ReserveEntries (GdiPath *pPath, INT numEntries);
VOID FASTCALL PATH_ScaleNormalizedPoint (FLOAT_POINT corners[], double x, double y, POINT *pPoint);
BOOL FASTCALL PATH_StrokePath(DC *dc, GdiPath *pPath);
BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2);
VOID FASTCALL
IntGetCurrentPositionEx(PDC dc, LPPOINT pt);
BOOL
STDCALL
NtGdiAbortPath(HDC hDC)
{
BOOL ret = TRUE;
PDC dc = DC_LockDc ( hDC );
if( !dc ) return FALSE;
PATH_EmptyPath(&dc->w.path);
DC_UnlockDc ( dc );
return ret;
}
BOOL
STDCALL
NtGdiBeginPath( HDC hDC )
{
BOOL ret = TRUE;
PDC dc = DC_LockDc ( hDC );
if( !dc ) return FALSE;
/* If path is already open, do nothing */
if ( dc->w.path.state != PATH_Open )
{
/* Make sure that path is empty */
PATH_EmptyPath( &dc->w.path );
/* Initialize variables for new path */
dc->w.path.newStroke = TRUE;
dc->w.path.state = PATH_Open;
}
DC_UnlockDc ( dc );
return ret;
}
VOID
FASTCALL
IntGdiCloseFigure(PDC pDc)
{
ASSERT(pDc);
ASSERT(pDc->w.path.state == PATH_Open);
// FIXME: Shouldn't we draw a line to the beginning of the figure?
// Set PT_CLOSEFIGURE on the last entry and start a new stroke
if(pDc->w.path.numEntriesUsed)
{
pDc->w.path.pFlags[pDc->w.path.numEntriesUsed-1]|=PT_CLOSEFIGURE;
pDc->w.path.newStroke=TRUE;
}
}
BOOL
STDCALL
NtGdiCloseFigure(HDC hDC)
{
BOOL Ret = FALSE; // default to failure
PDC pDc;
DPRINT("Enter %s\n", __FUNCTION__);
pDc = DC_LockDc(hDC);
if(!pDc) return FALSE;
if(pDc->w.path.state==PATH_Open)
{
IntGdiCloseFigure(pDc);
Ret = TRUE;
}
else
{
// FIXME: check if lasterror is set correctly
SetLastWin32Error(ERROR_CAN_NOT_COMPLETE);
}
DC_UnlockDc(pDc);
return Ret;
}
BOOL
STDCALL
NtGdiEndPath(HDC hDC)
{
BOOL ret = TRUE;
PDC dc = DC_LockDc ( hDC );
if ( !dc ) return FALSE;
/* Check that path is currently being constructed */
if( dc->w.path.state != PATH_Open )
{
ret = FALSE;
}
/* Set flag to indicate that path is finished */
else dc->w.path.state = PATH_Closed;
DC_UnlockDc ( dc );
return ret;
}
BOOL
STDCALL
NtGdiFillPath(HDC hDC)
{
BOOL ret = TRUE;
PDC dc = DC_LockDc ( hDC );
if ( !dc ) return FALSE;
ret = PATH_FillPath( dc, &dc->w.path );
if( ret )
{
/* FIXME: Should the path be emptied even if conversion
failed? */
PATH_EmptyPath( &dc->w.path );
}
DC_UnlockDc ( dc );
return ret;
}
BOOL
STDCALL
NtGdiFlattenPath(HDC hDC)
{
BOOL Ret = FALSE;
DC *pDc;
DPRINT("Enter %s\n", __FUNCTION__);
pDc = DC_LockDc(hDC);
if(!pDc) return FALSE;
if(pDc->w.path.state == PATH_Open)
Ret = PATH_FlattenPath(&pDc->w.path);
DC_UnlockDc(pDc);
return Ret;
}
BOOL
APIENTRY
NtGdiGetMiterLimit(
IN HDC hdc,
OUT PDWORD pdwOut)
{
UNIMPLEMENTED;
return FALSE;
}
INT
STDCALL
NtGdiGetPath(
HDC hDC,
LPPOINT Points,
LPBYTE Types,
INT nSize)
{
INT ret = -1;
GdiPath *pPath;
DC *dc = DC_LockDc(hDC);
if(!dc)
{
DPRINT1("Can't lock dc!\n");
return -1;
}
pPath = &dc->w.path;
if(pPath->state != PATH_Closed)
{
SetLastWin32Error(ERROR_CAN_NOT_COMPLETE);
goto done;
}
if(nSize==0)
{
ret = pPath->numEntriesUsed;
}
else if(nSize<pPath->numEntriesUsed)
{
SetLastWin32Error(ERROR_INVALID_PARAMETER);
goto done;
}
else
{
_SEH_TRY
{
memcpy(Points, pPath->pPoints, sizeof(POINT)*pPath->numEntriesUsed);
memcpy(Types, pPath->pFlags, sizeof(BYTE)*pPath->numEntriesUsed);
/* Convert the points to logical coordinates */
IntDPtoLP(dc, Points, pPath->numEntriesUsed);
ret = pPath->numEntriesUsed;
}
_SEH_HANDLE
{
SetLastNtError(_SEH_GetExceptionCode());
}
_SEH_END
}
done:
DC_UnlockDc(dc);
return ret;
}
HRGN
STDCALL
NtGdiPathToRegion(HDC hDC)
{
GdiPath *pPath;
HRGN hrgnRval = 0;
DC *pDc;
DPRINT("Enter %s\n", __FUNCTION__);
pDc = DC_LockDc(hDC);
if(!pDc) return NULL;
pPath = &pDc->w.path;
if(pPath->state!=PATH_Closed)
{
//FIXME: check that setlasterror is being called correctly
SetLastWin32Error(ERROR_CAN_NOT_COMPLETE);
}
else
{
/* FIXME: Should we empty the path even if conversion failed? */
if(PATH_PathToRegion(pPath, pDc->Dc_Attr.jFillMode, &hrgnRval))
PATH_EmptyPath(pPath);
}
DC_UnlockDc(pDc);
return hrgnRval;
}
BOOL
APIENTRY
NtGdiSetMiterLimit(
IN HDC hdc,
IN DWORD dwNew,
IN OUT OPTIONAL PDWORD pdwOut)
{
UNIMPLEMENTED;
return FALSE;
}
BOOL
STDCALL
NtGdiStrokeAndFillPath(HDC hDC)
{
DC *pDc;
BOOL bRet = FALSE;
DPRINT("Enter %s\n", __FUNCTION__);
if(!(pDc = DC_LockDc(hDC))) return FALSE;
bRet = PATH_FillPath(pDc, &pDc->w.path);
if(bRet) bRet = PATH_StrokePath(pDc, &pDc->w.path);
if(bRet) PATH_EmptyPath(&pDc->w.path);
DC_UnlockDc(pDc);
return bRet;
}
BOOL
STDCALL
NtGdiStrokePath(HDC hDC)
{
DC *pDc;
BOOL bRet = FALSE;
DPRINT("Enter %s\n", __FUNCTION__);
if(!(pDc = DC_LockDc(hDC))) return FALSE;
bRet = PATH_StrokePath(pDc, &pDc->w.path);
PATH_EmptyPath(&pDc->w.path);
DC_UnlockDc(pDc);
return bRet;
}
BOOL
STDCALL
NtGdiWidenPath(HDC hDC)
{
UNIMPLEMENTED;
return FALSE;
}
BOOL STDCALL NtGdiSelectClipPath(HDC hDC,
int Mode)
{
HRGN hrgnPath;
BOOL success = FALSE;
PDC dc = DC_LockDc ( hDC );
if( !dc ) return FALSE;
/* Check that path is closed */
if( dc->w.path.state != PATH_Closed )
{
SetLastWin32Error(ERROR_CAN_NOT_COMPLETE);
return FALSE;
}
/* Construct a region from the path */
else if( PATH_PathToRegion( &dc->w.path, dc->Dc_Attr.jFillMode, &hrgnPath ) )
{
success = IntGdiExtSelectClipRgn( dc, hrgnPath, Mode ) != ERROR;
NtGdiDeleteObject( hrgnPath );
/* Empty the path */
if( success )
PATH_EmptyPath( &dc->w.path);
/* FIXME: Should this function delete the path even if it failed? */
}
DC_UnlockDc ( dc );
return success;
}
/***********************************************************************
* Exported functions
*/
/* PATH_FillPath
*
*
*/
BOOL
FASTCALL
PATH_FillPath( PDC dc, GdiPath *pPath )
{
INT mapMode, graphicsMode;
SIZE ptViewportExt, ptWindowExt;
POINT ptViewportOrg, ptWindowOrg;
XFORM xform;
HRGN hrgn;
if( pPath->state != PATH_Closed )
{
SetLastWin32Error(ERROR_CAN_NOT_COMPLETE);
return FALSE;
}
if( PATH_PathToRegion( pPath, dc->Dc_Attr.jFillMode, &hrgn ))
{
/* Since PaintRgn interprets the region as being in logical coordinates
* but the points we store for the path are already in device
* coordinates, we have to set the mapping mode to MM_TEXT temporarily.
* Using SaveDC to save information about the mapping mode / world
* transform would be easier but would require more overhead, especially
* now that SaveDC saves the current path.
*/
/* Save the information about the old mapping mode */
mapMode = NtGdiGetMapMode( dc->hSelf );
NtGdiGetViewportExtEx( dc->hSelf, &ptViewportExt );
NtGdiGetViewportOrgEx( dc->hSelf, &ptViewportOrg );
NtGdiGetWindowExtEx( dc->hSelf, &ptWindowExt );
NtGdiGetWindowOrgEx( dc->hSelf, &ptWindowOrg );
/* Save world transform
* NB: The Windows documentation on world transforms would lead one to
* believe that this has to be done only in GM_ADVANCED; however, my
* tests show that resetting the graphics mode to GM_COMPATIBLE does
* not reset the world transform.
*/
NtGdiGetWorldTransform( dc->hSelf, &xform );
/* Set MM_TEXT */
NtGdiSetMapMode( dc->hSelf, MM_TEXT );
NtGdiSetViewportOrgEx( dc->hSelf, 0, 0, NULL );
NtGdiSetWindowOrgEx( dc->hSelf, 0, 0, NULL );
graphicsMode = NtGdiGetGraphicsMode( dc->hSelf );
NtGdiSetGraphicsMode( dc->hSelf, GM_ADVANCED );
NtGdiModifyWorldTransform( dc->hSelf, &xform, MWT_IDENTITY );
NtGdiSetGraphicsMode( dc->hSelf, graphicsMode );
/* Paint the region */
NtGdiPaintRgn( dc->hSelf, hrgn );
NtGdiDeleteObject( hrgn );
/* Restore the old mapping mode */
NtGdiSetMapMode( dc->hSelf, mapMode );
NtGdiSetViewportExtEx( dc->hSelf, ptViewportExt.cx, ptViewportExt.cy, NULL );
NtGdiSetViewportOrgEx( dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL );
NtGdiSetWindowExtEx( dc->hSelf, ptWindowExt.cx, ptWindowExt.cy, NULL );
NtGdiSetWindowOrgEx( dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL );
/* Go to GM_ADVANCED temporarily to restore the world transform */
graphicsMode = NtGdiGetGraphicsMode( dc->hSelf );
NtGdiSetGraphicsMode( dc->hSelf, GM_ADVANCED );
NtGdiModifyWorldTransform( dc->hSelf, &xform, MWT_MAX+1 );
NtGdiSetGraphicsMode( dc->hSelf, graphicsMode );
return TRUE;
}
return FALSE;
}
/* PATH_InitGdiPath
*
* Initializes the GdiPath structure.
*/
VOID
FASTCALL
PATH_InitGdiPath ( GdiPath *pPath )
{
ASSERT(pPath!=NULL);
pPath->state=PATH_Null;
pPath->pPoints=NULL;
pPath->pFlags=NULL;
pPath->numEntriesUsed=0;
pPath->numEntriesAllocated=0;
}
/* PATH_DestroyGdiPath
*
* Destroys a GdiPath structure (frees the memory in the arrays).
*/
VOID
FASTCALL
PATH_DestroyGdiPath ( GdiPath *pPath )
{
ASSERT(pPath!=NULL);
ExFreePool(pPath->pPoints);
ExFreePool(pPath->pFlags);
}
/* PATH_AssignGdiPath
*
* Copies the GdiPath structure "pPathSrc" to "pPathDest". A deep copy is
* performed, i.e. the contents of the pPoints and pFlags arrays are copied,
* not just the pointers. Since this means that the arrays in pPathDest may
* need to be resized, pPathDest should have been initialized using
* PATH_InitGdiPath (in C++, this function would be an assignment operator,
* not a copy constructor).
* Returns TRUE if successful, else FALSE.
*/
BOOL
FASTCALL
PATH_AssignGdiPath ( GdiPath *pPathDest, const GdiPath *pPathSrc )
{
ASSERT(pPathDest!=NULL && pPathSrc!=NULL);
/* Make sure destination arrays are big enough */
if ( !PATH_ReserveEntries(pPathDest, pPathSrc->numEntriesUsed) )
return FALSE;
/* Perform the copy operation */
memcpy(pPathDest->pPoints, pPathSrc->pPoints,
sizeof(POINT)*pPathSrc->numEntriesUsed);
memcpy(pPathDest->pFlags, pPathSrc->pFlags,
sizeof(BYTE)*pPathSrc->numEntriesUsed);
pPathDest->state=pPathSrc->state;
pPathDest->numEntriesUsed=pPathSrc->numEntriesUsed;
pPathDest->newStroke=pPathSrc->newStroke;
return TRUE;
}
/* PATH_MoveTo
*
* Should be called when a MoveTo is performed on a DC that has an
* open path. This starts a new stroke. Returns TRUE if successful, else
* FALSE.
*/
BOOL
FASTCALL
PATH_MoveTo ( PDC dc )
{
/* Check that path is open */
if ( dc->w.path.state != PATH_Open )
/* FIXME: Do we have to call SetLastError? */
return FALSE;
/* Start a new stroke */
dc->w.path.newStroke = TRUE;
return TRUE;
}
/* PATH_LineTo
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -