From b570f4b6717e02b4d11ff0bc2f883575ed3e8f6f Mon Sep 17 00:00:00 2001 From: "Ilya.Kirillov" Date: Fri, 19 Jun 2015 17:22:42 +0000 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B0=20=D0=BF=D1=80=D0=BE=D1=81=D1=82=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=BE=D1=82=D1=80=D0=B8=D1=81=D0=BE=D0=B2=D0=BA=D0=B0?= =?UTF-8?q?=20=D0=BF=D0=B0=D1=82=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: svn://fileserver/activex/AVS/Sources/TeamlabOffice/trunk/ServerComponents@63167 954022d7-b5bf-4e40-9824-e11837661b57 --- PdfWriter/PdfRenderer.cpp | 165 ++++++++++++++++-- PdfWriter/PdfRenderer.h | 32 +++- PdfWriter/PdfWriter.vcxproj | 4 + PdfWriter/PdfWriterTest/PdfWriterTest.cpp | 10 +- PdfWriter/PdfWriterTest/PdfWriterTest.vcxproj | 4 + PdfWriter/Src/Pages.cpp | 4 +- 6 files changed, 196 insertions(+), 23 deletions(-) diff --git a/PdfWriter/PdfRenderer.cpp b/PdfWriter/PdfRenderer.cpp index 747b50dc9e..457ded1204 100644 --- a/PdfWriter/PdfRenderer.cpp +++ b/PdfWriter/PdfRenderer.cpp @@ -15,6 +15,8 @@ #define MM_2_PT(X) ((X) * 72.0 / 25.4) #define PT_2_MM(X) ((X) * 25.4 / 72.0) +#define LONG_2_BOOL(X) ((X) ? true : false) + #ifdef DrawText #undef DrawText #endif @@ -435,6 +437,8 @@ HRESULT CPdfRenderer::CommandDrawText(const std::wstring& wsUnicodeText, const d m_pPage->GrSave(); UpdateTransform(); UpdateFont(); + UpdatePen(); + UpdateBrush(); if (!m_pFont) return S_FALSE; @@ -541,17 +545,33 @@ HRESULT CPdfRenderer::PathCommandEnd() } HRESULT CPdfRenderer::DrawPath(const LONG& lType) { + if (!IsPageValid()) + return S_FALSE; + + m_pPage->GrSave(); + UpdateTransform(); + UpdatePen(); + UpdateBrush(); + // TODO: Реализовать + + bool bStroke = LONG_2_BOOL(lType & c_nStroke); + bool bFill = LONG_2_BOOL(lType & c_nWindingFillMode); + bool bEoFill = LONG_2_BOOL(lType & c_nEvenOddFillMode); + + m_oPath.Draw(m_pPage, bStroke, bFill, bEoFill); + m_pPage->GrRestore(); + return S_OK; } HRESULT CPdfRenderer::PathCommandMoveTo(const double& dX, const double& dY) { - m_oPath.MoveTo(dX, dY); + m_oPath.MoveTo(MM_2_PT(dX), MM_2_PT(m_dPageHeight - dY)); return S_OK; } HRESULT CPdfRenderer::PathCommandLineTo(const double& dX, const double& dY) { - m_oPath.LineTo(dX, dY); + m_oPath.LineTo(MM_2_PT(dX), MM_2_PT(m_dPageHeight - dY)); return S_OK; } HRESULT CPdfRenderer::PathCommandLinesTo(double* pPoints, const int& nCount) @@ -560,19 +580,19 @@ HRESULT CPdfRenderer::PathCommandLinesTo(double* pPoints, const int& nCount) return S_OK; if (!m_oPath.IsMoveTo()) - m_oPath.MoveTo(pPoints[0], pPoints[1]); + m_oPath.MoveTo(MM_2_PT(pPoints[0]), MM_2_PT(m_dPageHeight - pPoints[1])); int nPointsCount = (nCount / 2) - 1; for (int nIndex = 1; nIndex <= nPointsCount; ++nIndex) { - m_oPath.LineTo(pPoints[nIndex * 2], pPoints[nIndex * 2 + 1]); + m_oPath.LineTo(MM_2_PT(pPoints[nIndex * 2]), MM_2_PT(m_dPageHeight - pPoints[nIndex * 2 + 1])); } return S_OK; } HRESULT CPdfRenderer::PathCommandCurveTo(const double& dX1, const double& dY1, const double& dX2, const double& dY2, const double& dXe, const double& dYe) { - m_oPath.CurveTo(dX1, dY1, dX2, dY2, dXe, dYe); + m_oPath.CurveTo(MM_2_PT(dX1), MM_2_PT(m_dPageHeight - dY1), MM_2_PT(dX2), MM_2_PT(m_dPageHeight - dY2), MM_2_PT(dXe), MM_2_PT(m_dPageHeight - dYe)); return S_OK; } HRESULT CPdfRenderer::PathCommandCurvesTo(double* pPoints, const int& nCount) @@ -581,20 +601,20 @@ HRESULT CPdfRenderer::PathCommandCurvesTo(double* pPoints, const int& nCount) return S_OK; if (!m_oPath.IsMoveTo()) - m_oPath.MoveTo(pPoints[0], pPoints[1]); + m_oPath.MoveTo(MM_2_PT(pPoints[0]), MM_2_PT(m_dPageHeight - pPoints[1])); int nPointsCount = (nCount - 2) / 6; double* pCur = pPoints + 2; for (int nIndex = 0; nIndex <= nPointsCount; ++nIndex, pCur += 6) { - m_oPath.CurveTo(pCur[0], pCur[1], pCur[2], pCur[3], pCur[4], pCur[5]); + m_oPath.CurveTo(MM_2_PT(pCur[0]), MM_2_PT(m_dPageHeight - pCur[1]), MM_2_PT(pCur[2]), MM_2_PT(m_dPageHeight - pCur[3]), MM_2_PT(pCur[4]), MM_2_PT(m_dPageHeight - pCur[5])); } return S_OK; } HRESULT CPdfRenderer::PathCommandArcTo(const double& dX, const double& dY, const double& dW, const double& dH, const double& dStartAngle, const double& dSweepAngle) { - m_oPath.ArcTo(dX, dY, dW, dH, dStartAngle, dSweepAngle); + m_oPath.ArcTo(MM_2_PT(dX), MM_2_PT(m_dPageHeight - dY - dH), MM_2_PT(dW), MM_2_PT(dH), dStartAngle, dSweepAngle); return S_OK; } HRESULT CPdfRenderer::PathCommandClose() @@ -605,26 +625,28 @@ HRESULT CPdfRenderer::PathCommandClose() HRESULT CPdfRenderer::PathCommandGetCurrentPoint(double* dX, double* dY) { m_oPath.GetLastPoint(*dX, *dY); + *dX = PT_2_MM(*dX); + *dY = PT_2_MM(*dY); return S_OK; } HRESULT CPdfRenderer::PathCommandTextCHAR(const LONG& lUnicode, const double& dX, const double& dY, const double& dW, const double& dH, const double& dBaselineOffset) { - m_oPath.AddText(m_oFont, lUnicode, dX, dY, dW, dH, dBaselineOffset); + m_oPath.AddText(m_oFont, lUnicode, MM_2_PT(dX), MM_2_PT(m_dPageHeight - dY), MM_2_PT(dW), MM_2_PT(dH), MM_2_PT(dBaselineOffset)); return S_OK; } HRESULT CPdfRenderer::PathCommandText(const std::wstring& wsText, const double& dX, const double& dY, const double& dW, const double& dH, const double& dBaselineOffset) { - m_oPath.AddText(m_oFont, wsText, dX, dY, dW, dH, dBaselineOffset); + m_oPath.AddText(m_oFont, wsText, MM_2_PT(dX), MM_2_PT(m_dPageHeight - dY), MM_2_PT(dW), MM_2_PT(dH), MM_2_PT(dBaselineOffset)); return S_OK; } HRESULT CPdfRenderer::PathCommandTextExCHAR(const LONG& lUnicode, const LONG& lGid, const double& dX, const double& dY, const double& dW, const double& dH, const double& dBaselineOffset, const DWORD& dwFlags) { - m_oPath.AddText(m_oFont, lUnicode, lGid, dX, dY, dW, dH, dBaselineOffset, dwFlags); + m_oPath.AddText(m_oFont, lUnicode, lGid, MM_2_PT(dX), MM_2_PT(m_dPageHeight - dY), MM_2_PT(dW), MM_2_PT(dH), MM_2_PT(dBaselineOffset), dwFlags); return S_OK; } HRESULT CPdfRenderer::PathCommandTextEx(const std::wstring& wsUnicodeText, const std::wstring& wsGidText, const double& dX, const double& dY, const double& dW, const double& dH, const double& dBaselineOffset, const DWORD& dwFlags) { - m_oPath.AddText(m_oFont, wsUnicodeText, wsGidText, dX, dY, dW, dH, dBaselineOffset, dwFlags); + m_oPath.AddText(m_oFont, wsUnicodeText, wsGidText, MM_2_PT(dX), MM_2_PT(m_dPageHeight - dY), MM_2_PT(dW), MM_2_PT(dH), MM_2_PT(dBaselineOffset), dwFlags); return S_OK; } //---------------------------------------------------------------------------------------- @@ -635,8 +657,6 @@ HRESULT CPdfRenderer::DrawImage(IGrObject* pImage, const double& dX, const doubl if (!IsPageValid() || !pImage) return S_OK; - // TODO: Нужно обновить трансформ - if (!DrawImage((Aggplus::CImage*)pImage, dX, dY, dW, dH, 255)) return S_FALSE; @@ -647,8 +667,6 @@ HRESULT CPdfRenderer::DrawImageFromFile(const std::wstring& wsImagePath, const d if (!IsPageValid()) return S_OK; - // TODO: Нужно обновить трансформ - Aggplus::CImage oAggImage(wsImagePath); if (!DrawImage(&oAggImage, dX, dY, dW, dH, nAlpha)) return S_FALSE; @@ -770,7 +788,10 @@ bool CPdfRenderer::DrawImage(Aggplus::CImage* pImage, const double& dX, const do pPdfImage->LoadSMask(pData, nImageW, nImageH, nAlpha); pPdfImage->LoadJpx(pBuffer, nBufferSize, nImageW, nImageH); + m_pPage->GrSave(); + UpdateTransform(); m_pPage->DrawImage(pPdfImage, MM_2_PT(dX), MM_2_PT(m_dPageHeight - dY - dH), MM_2_PT(dW), MM_2_PT(dH)); + m_pPage->GrRestore(); free(pBuffer); return true; @@ -783,8 +804,8 @@ void CPdfRenderer::UpdateFont() { CFontSelectFormat oFontSelect; oFontSelect.wsName = new std::wstring(m_oFont.GetName()); - oFontSelect.bItalic = new INT(m_oFont.IsItalic() ? 0 : 1); - oFontSelect.bItalic = new INT(m_oFont.IsBold() ? 0 : 1); + oFontSelect.bItalic = new INT(m_oFont.IsItalic() ? 1 : 0); + oFontSelect.bBold = new INT(m_oFont.IsBold() ? 1 : 0); CFontInfo* pFontInfo = m_pFontManager->GetFontInfoByParams(oFontSelect); wsFontPath = pFontInfo->m_wsFontPath; @@ -807,4 +828,112 @@ void CPdfRenderer::UpdateTransform() CTransform& t = m_oTransform; m_pPage->Concat(t.m11, -t.m12, -t.m21, t.m22, MM_2_PT(t.dx + t.m21 * m_dPageHeight), MM_2_PT(m_dPageHeight - m_dPageHeight * t.m22 - t.dy)); } +void CPdfRenderer::UpdatePen() +{ + TColor& oColor = m_oPen.GetTColor(); + m_pPage->SetStrokeColor(oColor.r, oColor.g, oColor.b); + m_pPage->SetLineWidth(MM_2_PT(m_oPen.GetSize())); + + LONG lDashCount = 0; + double* pDashPattern = NULL; + + LONG lDashStyle = m_oPen.GetDashStyle(); + if (Aggplus::DashStyleSolid == lDashStyle) + { + // Ничего не делаем + } + else if (Aggplus::DashStyleCustom == lDashStyle) + { + double *pDashPatternMM = m_oPen.GetDashPattern(lDashCount); + if (pDashPatternMM && lDashCount) + { + pDashPattern = new double[lDashCount]; + if (pDashPattern) + { + for (LONG lIndex = 0; lIndex < lDashCount; lIndex++) + { + pDashPattern[lIndex] = MM_2_PT(pDashPatternMM[lIndex]); + } + } + } + } + else + { + // TODO: Реализовать другие типы пунктирных линий + } + if (pDashPattern && lDashCount) + { + m_pPage->SetDash(pDashPattern, lDashCount, MM_2_PT(m_oPen.GetDashOffset())); + delete[] pDashPattern; + } + + + // TODO: Реализовать +} +void CPdfRenderer::UpdateBrush() +{ + TColor& oColor1 = m_oBrush.GetTColor1(); + m_pPage->SetFillColor(oColor1.r, oColor1.g, oColor1.b); + // TODO: Реализовать +} + + + +void CPdfRenderer::CPath::Draw(PdfWriter::CPage* pPage, bool bStroke, bool bFill, bool bEoFill) +{ + for (int nIndex = 0, nCount = m_vCommands.size(); nIndex < nCount; nIndex++) + { + CPathCommandBase* pCommand = m_vCommands.at(nIndex); + pCommand->Draw(pPage); + } + + if (bStroke && !bFill && !bEoFill) + pPage->Stroke(); + else if (bStroke && bFill) + pPage->FillStroke(); + else if (bStroke && bEoFill) + pPage->EoFill(); + else if (bFill) + pPage->Fill(); + else if (bEoFill) + pPage->EoFill(); + else + pPage->EndPath(); +} +void CPdfRenderer::CPath::CPathMoveTo::Draw(PdfWriter::CPage* pPage) +{ + pPage->MoveTo(x, y); +} +void CPdfRenderer::CPath::CPathLineTo::Draw(PdfWriter::CPage* pPage) +{ + pPage->LineTo(x, y); +} +void CPdfRenderer::CPath::CPathCurveTo::Draw(PdfWriter::CPage* pPage) +{ + pPage->CurveTo(x1, y1, x2, y2, xe, ye); +} +void CPdfRenderer::CPath::CPathArcTo::Draw(PdfWriter::CPage* pPage) +{ + pPage->EllipseArcTo(x + w / 2, y + h / 2, w / 2, h / 2, startAngle, sweepAngle); +} +void CPdfRenderer::CPath::CPathClose::Draw(PdfWriter::CPage* pPage) +{ + pPage->ClosePath(); +} +void CPdfRenderer::CPath::CPathTextChar::Draw(PdfWriter::CPage* pPage) +{ + // TODO: Реализовать +} +void CPdfRenderer::CPath::CPathText::Draw(PdfWriter::CPage* pPage) +{ + // TODO: Реализовать +} +void CPdfRenderer::CPath::CPathTextExChar::Draw(PdfWriter::CPage* pPage) +{ + // TODO: Реализовать +} +void CPdfRenderer::CPath::CPathTextEx::Draw(PdfWriter::CPage* pPage) +{ + // TODO: Реализовать +} \ No newline at end of file diff --git a/PdfWriter/PdfRenderer.h b/PdfWriter/PdfRenderer.h index 0edb11ff9c..26342fd402 100644 --- a/PdfWriter/PdfRenderer.h +++ b/PdfWriter/PdfRenderer.h @@ -172,6 +172,8 @@ private: bool DrawImage(Aggplus::CImage* pImage, const double& dX, const double& dY, const double& dW, const double& dH, const BYTE& nAlpha); void UpdateFont(); void UpdateTransform(); + void UpdatePen(); + void UpdateBrush(); bool IsValid() { return m_bValid; @@ -252,6 +254,10 @@ private: { m_oColor.Set(lColor); } + inline TColor GetTColor() + { + return m_oColor; + } inline LONG GetAlpha() { return m_nAlpha; @@ -349,6 +355,11 @@ private: } } } + inline double*GetDashPattern(LONG& lSize) + { + lSize = m_lDashPatternSize; + return m_pDashPattern; + } void Reset() { @@ -409,6 +420,10 @@ private: { return m_oColor1.lColor; } + inline TColor GetTColor1() + { + return m_oColor1; + } inline void SetColor1(const LONG& lColor) { m_oColor1.Set(lColor); @@ -417,6 +432,10 @@ private: { return m_oColor2.lColor; } + inline TColor GetTColor2() + { + return m_oColor2; + } inline void SetColor2(const LONG& lColor) { m_oColor2.Set(lColor); @@ -848,6 +867,7 @@ private: virtual ~CPathCommandBase() { } + virtual void Draw(PdfWriter::CPage* pPage) = 0; virtual void GetLastPoint(double& dX, double& dY) = 0; virtual EPathCommandType GetType() = 0; }; @@ -864,6 +884,7 @@ private: dX = x; dY = y; } + void Draw(PdfWriter::CPage* pPage); EPathCommandType GetType() { return rendererpathcommand_MoveTo; @@ -887,6 +908,7 @@ private: dX = x; dY = y; } + void Draw(PdfWriter::CPage* pPage); EPathCommandType GetType() { return rendererpathcommand_LineTo; @@ -904,7 +926,7 @@ private: { x1 = dX1; y1 = dY1; - x2 = dXe; + x2 = dX2; y2 = dY2; xe = dXe; ye = dYe; @@ -914,6 +936,7 @@ private: dX = xe; dY = ye; } + void Draw(PdfWriter::CPage* pPage); EPathCommandType GetType() { return rendererpathcommand_CurveTo; @@ -946,6 +969,7 @@ private: dX = x; dY = y; } + void Draw(PdfWriter::CPage* pPage); EPathCommandType GetType() { return rendererpathcommand_ArcTo; @@ -972,6 +996,7 @@ private: dX = 0; dY = 0; } + void Draw(PdfWriter::CPage* pPage); EPathCommandType GetType() { return rendererpathcommand_Close; @@ -995,6 +1020,7 @@ private: dX = x; dY = y; } + void Draw(PdfWriter::CPage* pPage); EPathCommandType GetType() { return rendererpathcommand_TextChar; @@ -1028,6 +1054,7 @@ private: dX = x; dY = y; } + void Draw(PdfWriter::CPage* pPage); EPathCommandType GetType() { return rendererpathcommand_Text; @@ -1062,6 +1089,7 @@ private: dX = x; dY = y; } + void Draw(PdfWriter::CPage* pPage); EPathCommandType GetType() { return rendererpathcommand_TextExChar; @@ -1097,6 +1125,7 @@ private: dX = x; dY = y; } + void Draw(PdfWriter::CPage* pPage); EPathCommandType GetType() { return rendererpathcommand_TextEx; @@ -1197,6 +1226,7 @@ private: m_vCommands.at(m_vCommands.size() - 1)->GetLastPoint(dX, dY); } } + void Draw(PdfWriter::CPage* pPage, bool bStroke, bool bFill, bool bEoFill); private: diff --git a/PdfWriter/PdfWriter.vcxproj b/PdfWriter/PdfWriter.vcxproj index 8ac8e1b3ba..0f7dafa955 100644 --- a/PdfWriter/PdfWriter.vcxproj +++ b/PdfWriter/PdfWriter.vcxproj @@ -91,11 +91,15 @@ true 4018;4005;4267 ..\DesktopEditor\freetype-2.5.2\include;..\DesktopEditor\agg-2.4\include;..\DesktopEditor\cximage\zlib;%(AdditionalIncludeDirectories) + true Windows true + + true + diff --git a/PdfWriter/PdfWriterTest/PdfWriterTest.cpp b/PdfWriter/PdfWriterTest/PdfWriterTest.cpp index 86f1f51fdd..bcbfb27930 100644 --- a/PdfWriter/PdfWriterTest/PdfWriterTest.cpp +++ b/PdfWriter/PdfWriterTest/PdfWriterTest.cpp @@ -887,9 +887,14 @@ void ConvertFolder(std::wstring wsFolderPath, const int nType) dX *= dPx2Mm; dY *= dPx2Mm; + double dAspect = dH / dW; + dW = 1000; + dH = dAspect * dW; + oRenderer.put_Width(dW); oRenderer.put_Height(dH); - oMetaFile.DrawOnRenderer(&oRenderer, -dX, -dY, dW, dH); + //oMetaFile.DrawOnRenderer(&oRenderer, -dX, -dY, dW, dH); + oMetaFile.DrawOnRenderer(&oRenderer, 0, 0, dW, dH); oMetaFile.Close(); } @@ -900,7 +905,8 @@ void ConvertFolder(std::wstring wsFolderPath, const int nType) } void TestMetafile() { - ConvertFolder(L"D://Test Files//Emf//", MetaFile::c_lMetaEmf); + //ConvertFolder(L"D://Test Files//Emf//", MetaFile::c_lMetaEmf); + ConvertFolder(L"D://Test Files//Wmf//", MetaFile::c_lMetaWmf); } void main() diff --git a/PdfWriter/PdfWriterTest/PdfWriterTest.vcxproj b/PdfWriter/PdfWriterTest/PdfWriterTest.vcxproj index 839ea0ab24..7ec26435db 100644 --- a/PdfWriter/PdfWriterTest/PdfWriterTest.vcxproj +++ b/PdfWriter/PdfWriterTest/PdfWriterTest.vcxproj @@ -101,11 +101,15 @@ true ..\..\DesktopEditor\freetype-2.5.2\include;..\..\DesktopEditor\agg-2.4\include;..\..\DesktopEditor\cximage\zlib;%(AdditionalIncludeDirectories) 4018;4005;4267 + true Console true + + true + diff --git a/PdfWriter/Src/Pages.cpp b/PdfWriter/Src/Pages.cpp index 7aed499b83..2acfcbd588 100644 --- a/PdfWriter/Src/Pages.cpp +++ b/PdfWriter/Src/Pages.cpp @@ -429,7 +429,7 @@ namespace PdfWriter m_pStream->WriteReal(dStartX); m_pStream->WriteChar(' '); m_pStream->WriteReal(dStartY); - m_pStream->WriteStr(" l\012"); + m_pStream->WriteStr(" m\012"); // Дальше рисуем РїРѕ четверям double dCurX = dStartX, dCurY = dStartY; @@ -503,7 +503,7 @@ namespace PdfWriter while (_dAngle2 < 0) _dAngle2 += 360; - while (_dAngle2 >= 360) + while (_dAngle2 > 360) _dAngle2 -= 360; if (!bClockDirection)