📄 scemfdcrenderer.cpp
字号:
/*
* This file is part of the EMFexplorer projet.
* Copyright (C) 2004 Smith Charles.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*
* Extension: for commercial use, apply the Equity Public License, which
* adds to the normal terms of the GLPL a condition of donation to the author.
* If you are interested in support for this source code,
* contact Smith Charles <smith.charles@free.fr> for more information.
*/
#include "stdafx.h"
#include "SCEMFdcRenderer.h"
// GDI+ won't allow DEBUG_NEW to work
// #ifdef _DEBUG
// #define new DEBUG_NEW
// #undef THIS_FILE
// static char THIS_FILE[] = __FILE__;
// #endif
using namespace Gdiplus;
#define SC_CHECKGDIP_OBJ(pObj) \
if (pObj) ASSERT(pObj->GetLastStatus()==Ok)
///
/// g_pFontCopies contains copies of TT fonts already installed for GDI and that
/// that GDI+ accepts to use only if they are placed in a private collection.
/// We don't want to create this private collection on a per-object basis.
/// To ensure a per-module caching, the collection is ref-counted.
/// Call SCCleanFontCopies() when a new document is loaded.
///
SCFontCollection* g_pFontCopies = NULL;
SCFontCollectionHolder* g_pFontCopiesHolder = NULL;
///////////////////////////////////////////////////////////////////////////////////
// Construction/Destruction
///////////////////////////////////////////////////////////////////////////////////
CSCEMFdcRenderer::CSCEMFdcRenderer():
m_hDC(NULL),
m_pGraphics(NULL),
m_bMonochrome(FALSE),
m_pPen(NULL),
m_pBrush(NULL),
m_pFont(NULL),
m_pPath(NULL),
m_dwLastError(SC_EMFERR_NOERROR),
m_PtCurPos(0,0),
m_pPenHolder(NULL),
m_pBrushHolder(NULL),
m_pFontHolder(NULL),
m_pPathHolder(NULL),
m_dwMapMode(MM_TEXT),
m_dwBkMode(OPAQUE),
m_dwFillMode(ALTERNATE),
m_dwStretchBltMode(COLORONCOLOR),
m_dwTextAlign(TA_LEFT|TA_BASELINE),
m_dwROP2(R2_COPYPEN),
m_BkColor(RGB(255, 255, 255)),
m_TextColor(RGB(0, 0, 0)),
m_dwArcDirection(AD_COUNTERCLOCKWISE),
m_fMiterLimit(10.0),
m_pImgAttribs(NULL),
m_pFontCollection(NULL)
// no constructor
// m_WinSize(1,1),
// m_ViewSize(1,1),
// m_PtWinOrg(0,0),
// m_PtViewOrg(0,0),
// m_RcClipBox(0,0,0,0)
{
m_WinSize.cx = m_WinSize.cy = 1;
m_ViewSize.cx = m_ViewSize.cy = 1;
m_PtWinOrg.x = m_PtWinOrg.y = 0;
m_PtViewOrg.x = m_PtViewOrg.y = 0;
SetRectEmpty(&m_RcClipBox);
if (!g_pFontCopiesHolder)
{
ASSERT(NULL==g_pFontCopies);
g_pFontCopies = new SCFontCollection;
g_pFontCopiesHolder = new SCFontCollectionHolder(g_pFontCopies);
}
SC_REFCOUNTED_ADDREF(g_pFontCopiesHolder);
}
/*static*/
void CSCEMFdcRenderer::SCCleanFontCopies()
{
SC_OBJHOLDER_SAFERELEASE_T(g_pFontCopiesHolder, g_pFontCopies, SCFontCollection);
}
CSCEMFdcRenderer::~CSCEMFdcRenderer()
{
SMC_DELETE(m_pGraphics);
SMC_DELETE(m_pImgAttribs);
SC_OBJHOLDER_RELEASE_T(m_pPenHolder, m_pPen, GDPPen);
SC_OBJCONTAINER_RELEASE_T(m_pBrushHolder, m_pBrush, GDPBrush, SCBrush);
SC_OBJHOLDER_RELEASE_T(m_pFontHolder, m_pFont, SCFont);
SC_OBJHOLDER_RELEASE_T(m_pPathHolder, m_pPath, SCPath);
SC_OBJHOLDER_SAFERELEASE_T(g_pFontCopiesHolder, g_pFontCopies, SCFontCollection);
}
///////////////////////////////////////////////////////////////////////////////////
// Utilities
///////////////////////////////////////////////////////////////////////////////////
///
/// Prepare the graphic context for the rendering session.
///
void CSCEMFdcRenderer::SCSetupRendering()
{
ASSERT(m_pGraphics);
// 1. Get current DC state
m_dwMapMode = GetMapMode(m_hDC);
m_dwBkMode = GetBkMode(m_hDC);
m_dwFillMode = GetPolyFillMode(m_hDC);
m_dwStretchBltMode = GetStretchBltMode(m_hDC);
m_dwTextAlign = GetTextAlign(m_hDC);
m_dwROP2 = GetROP2(m_hDC);
m_BkColor = SC_RGBCOLOR(GetBkColor(m_hDC));
m_TextColor = SC_FINAL_COLOR(GetTextColor(m_hDC));
m_dwArcDirection = GetArcDirection(m_hDC);
m_bMonochrome = SCIsMonochromeDC(m_hDC);
GetMiterLimit(m_hDC, &m_fMiterLimit);
GetWindowExtEx(m_hDC, &m_WinSize);
GetViewportExtEx(m_hDC, &m_ViewSize);
GetWindowOrgEx(m_hDC, &m_PtWinOrg);
GetViewportOrgEx(m_hDC, &m_PtViewOrg);
// GetClipBox(m_hDC, &m_RcClipBox); // done elsewhere
// clipping
SCCropToPlayBox();
// 2. Add user-requested attributes
// texts
m_pGraphics->SetTextRenderingHint(m_DrawingAttributes.eTextRenderingHint);
m_pGraphics->SetTextContrast(m_DrawingAttributes.uiTextContrast);
// curves
m_pGraphics->SetSmoothingMode(m_DrawingAttributes.eSmoothingMode);
// images
m_pGraphics->SetInterpolationMode(m_DrawingAttributes.eInterpolationMode);
m_pGraphics->SetPixelOffsetMode(m_DrawingAttributes.ePixelOffsetMode);
// paper color
COLORREF PaperColor = m_DrawingAttributes.crPaperColor;
if (m_DrawingAttributes.bReverseVideo)
{
if ((m_DrawingAttributes.dwInkingMode & SC_REVERSE_VIDEO_MASK)!=SC_REVERSE_VIDEO_SKIPIMAGES)
{
if (PaperColor!=RGB(255, 255, 255))
{
ColorMap clrMap;
clrMap.oldColor = Color(255, 255, 255, 255);
clrMap.newColor = Color(255, GetRValue(PaperColor), GetGValue(PaperColor), GetBValue(PaperColor));
m_pImgAttribs = new ImageAttributes;
if (m_pImgAttribs)
m_pImgAttribs->SetRemapTable(1, &clrMap);
}
}
if (RGB(255, 255, 255)==m_BkColor)
m_BkColor = SC_RGBCOLOR(~PaperColor);
} else
{
if ((m_DrawingAttributes.dwInkingMode & SC_TRANSPARENCY_MASK)==SC_TRANSPARENCY_WHITECOLOR)
{
if (PaperColor!=RGB(255, 255, 255))
{
ColorMap clrMap;
clrMap.oldColor = Color(255, 255, 255, 255);
clrMap.newColor = Color(255, GetRValue(PaperColor), GetGValue(PaperColor), GetBValue(PaperColor));
m_pImgAttribs = new ImageAttributes;
if (m_pImgAttribs)
m_pImgAttribs->SetRemapTable(1, &clrMap);
}
}
if (RGB(255, 255, 255)==m_BkColor)
m_BkColor = SC_RGBCOLOR(PaperColor);
}
// printing
if (m_DrawingAttributes.bPrinting)
{
m_pGraphics->SetPageUnit(UnitPixel);
m_pGraphics->SetPageScale(m_DrawingAttributes.fPageScale);
}
// default objects
HGDIOBJ hGdiObj;
if (hGdiObj = GetCurrentObject(m_hDC, OBJ_PEN))
{
SCOnChangePen((HPEN)hGdiObj);
}
if (hGdiObj = GetCurrentObject(m_hDC, OBJ_BRUSH))
{
SCOnChangeBrush((HBRUSH)hGdiObj);
}
if (hGdiObj = GetCurrentObject(m_hDC, OBJ_FONT))
{
SCOnChangeFont((HFONT)hGdiObj);
}
// background
if (m_DrawingAttributes.bBkSolid)
{
Color BrushColor;
BrushColor.SetFromCOLORREF(m_BkColor);
SolidBrush brush(BrushColor);
CRect rcBox(m_RcClipBox);
SCDPtoLP((Point*)&rcBox, 2);
Rect rectClip(rcBox.left, rcBox.top, RECT_WIDTH(rcBox), RECT_HEIGHT(rcBox));
rectClip.Inflate(1, 1);
m_pGraphics->FillRectangle(&brush, rectClip);
}
}
void CSCEMFdcRenderer::SCDPtoLP(Point* pPoints, INT iNbPts)
{
ASSERT(pPoints);
ASSERT(m_pGraphics);
Matrix matrix;
m_pGraphics->GetTransform(&matrix);
matrix.Invert();
matrix.TransformPoints(pPoints, iNbPts);
}
void CSCEMFdcRenderer::SCLPtoDP(Point* pPoints, INT iNbPts)
{
ASSERT(pPoints);
ASSERT(m_pGraphics);
Matrix matrix;
m_pGraphics->GetTransform(&matrix);
matrix.TransformPoints(pPoints, iNbPts);
}
#if 0
///
/// Update current position
///
/// Compiler bug?
/// Other compilation units won't see this inline if it's not exported
/// (definition is discarded when it's not called from this file).
/// Linker will complain. (Note: functions like SCCropToPlayBox doesn't show this problem).
inline void CSCEMFdcRenderer::SCUpdateCurPos(INT x, INT y)
{
MoveToEx(m_hDC, x, y, NULL);
m_PtCurPos.X = x;
m_PtCurPos.Y = y;
}
/// To export it, use:
// inline __declspec( dllexport ) void CSCEMFdcRenderer::SCUpdateCurPos(INT x, INT y)
/// But I'm not sure VC6 would really inline it.
#endif
///
/// Change color to conform to drawing attributes.
///
inline COLORREF CSCEMFdcRenderer::SCGetFinalColor(COLORREF crColor)
{
if ((m_DrawingAttributes.dwInkingMode & SC_TRANSPARENCY_MASK)!=SC_TRANSPARENCY_NORMAL)
{
if (RGB(255, 255, 255)==SC_RGBCOLOR(crColor))
return SC_FINAL_COLOR(m_DrawingAttributes.crPaperColor);
}
return SC_FINAL_COLOR(crColor);
}
///
/// Replace the current brush with a new one.
///
inline void CSCEMFdcRenderer::SCRefreshBrush(Brush* pBrush)
{
ASSERT(m_pBrushHolder); // we are refreshing
ASSERT(pBrush); // can't refresh to NULL
SCBrush* pSCBrush = m_pBrushHolder->SCDetachSubObj();
ASSERT(pSCBrush);
SC_OBJCONTAINER_RELEASE_T(m_pBrushHolder, m_pBrush, GDPBrush, SCBrush);
m_pBrush = pBrush;
SC_OBJCONTAINER_RENEW_T(m_pBrushHolder, m_pBrush, GDPBrush, pSCBrush, SCBrush);
}
///////////////////////////////////////////////////////////////////////////////////
// I_EMFRasterizer
///////////////////////////////////////////////////////////////////////////////////
///
/// Create the unique graphic context for the rendering session.
/// hDestDC is where the raster is written.
/// hSrcDC is a reference DC from which information is collected
///
BOOL CSCEMFdcRenderer::SCBeginRendering(HDC hDestDC, HDC hSrcDC)
{
m_hDC = hSrcDC;
// Copy the initial source transformation to the destination
if (hDestDC != hSrcDC)
{
// TODO: revise this when DC cloning is ractivated
// Combine with the initial source transformation
XFORM xformSrc;
GetWorldTransform(m_hDC, &xformSrc);
int iGM = SetGraphicsMode(hDestDC, GM_ADVANCED);
ModifyWorldTransform(hDestDC, &xformSrc, MWT_LEFTMULTIPLY);
// should fail if current matrix is not identity
SetGraphicsMode(hDestDC, iGM);
// Save original transformation of destination
XFORM xform;
GetWorldTransform(hDestDC, &xform);
m_DestMatrix.SetElements(xform.eM11, xform.eM12, xform.eM21, xform.eM22, xform.eDx, xform.eDy);
}
// Compute clip box to use as metaregion
{
#pragma message( __FILE__ "(335): TODO: Remove MFC dependency ")
// For now it is needed to render into GDI metafiles
CDC* pDC = CDC::FromHandle(hDestDC);
HDC hAttribDC = pDC->m_hAttribDC;
ASSERT(hAttribDC);
HRGN hRgn = CreateRectRgn(0,0,0,0);
int iRes = GetMetaRgn(hAttribDC, hRgn);
ASSERT(iRes||(hAttribDC==hDestDC));
if (!iRes)
{// Sadly the meta region isn't meta enough. GetMetaRgn fails if you forget
// to set a clipping region before calling it.
iRes = GetClipBox(hAttribDC, &m_RcClipBox); // No! it's good for screen only
} else
{
iRes = GetRgnBox(hRgn, &m_RcClipBox);
DPtoLP(hAttribDC, (POINT*)&m_RcClipBox, 2); // warning: rounding errors here
ASSERT(NULLREGION!=iRes && ERROR!=iRes); // there must be a rectangle
}
DeleteObject(hRgn);
}
SCNormalizeRect(&m_RcClipBox);
if (IsRectEmpty(&m_RcClipBox))
{// Set 'infinite' metaregion
SetRect(&m_RcClipBox, -32767, -32767, 32767, 32767);
}
// Create GDI+ output surface
SMC_DELETE(m_pGraphics);
if (!SCCreateSurface(hDestDC))
{
m_dwLastError = SC_EMFERR_OUTOFMEMORY;
return FALSE;
}
SCLPtoDP((Point*)&m_RcClipBox, 2); // we need it in target (GDI+) device coordinates
// Setup graphic state (get other attributes from source DC)
SCSetupRendering();
return m_pGraphics->GetLastStatus()==Ok;
}
///
/// Push the current graphic state.
///
void CSCEMFdcRenderer::SCOnDCSaved()
{
ASSERT(m_pGraphics);
// Lock the objects matching GDI DC objects
CSCGDIState* pStat = new CSCGDIState(
m_pPenHolder,
m_pBrushHolder,
m_pFontHolder,
m_pPathHolder,
m_PtCurPos,
m_pGraphics->Save(),
m_dwMapMode,
m_dwBkMode,
m_dwFillMode,
m_dwStretchBltMode,
m_dwTextAlign,
m_dwROP2,
m_BkColor,
m_TextColor,
m_dwArcDirection,
m_fMiterLimit,
m_WinSize,
m_ViewSize,
m_PtWinOrg,
m_PtViewOrg,
m_RcClipBox
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -