123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751 |
- #include "stdafx.h"
- #include "EasyPrinterV1.h"
- #include <Shlwapi.h>
- CEasyPrinterV1::CEasyPrinterV1()
- {
- }
- CEasyPrinterV1::~CEasyPrinterV1()
- {
- }
- void CEasyPrinterV1::TextDownwards(IN TString strText, IN const INT& nRectWide, OUT vector<TString>& vtSubText)
- {
- if (nRectWide < 1)
- {
- TRACE("图元宽 - 2*边距值 < 0 \n");
- return;
- }
- TString str;
- SIZE fontSize;
-
- GetTextExtentPoint32(m_dcPrinter, "1", 1, &fontSize);
- const INT nCharWide = fontSize.cx;
-
- INT nCharSum = 0;
-
- const INT nCharCount = nRectWide / nCharWide;
-
- INT nIndex = 0;
- strText.append("\r\n");
- vector<TString> vtWrapText;
- do
- {
- if ((nIndex = strText.find("\r\n")) != TString::npos)
- {
- str = strText.substr(0, nIndex);
- if (str.size() == 0)
- vtWrapText.push_back("\r\n");
- else
- vtWrapText.push_back(str);
- strText = strText.substr(nIndex + strlen("\r\n"));
- }
- } while (strText.find("\r\n") != TString::npos);
- for (vector<TString>::iterator it = vtWrapText.begin(); it != vtWrapText.end(); it++)
- {
- strText = it->c_str();
- do
- {
-
- nCharSum = nCharCount;
- if (strText.length() > nCharSum)
- {
- str = strText.substr(0, nCharSum);
- GetTextExtentPoint32(m_dcPrinter, str.c_str(), str.length(), &fontSize);
- if (nRectWide >= fontSize.cx)
- {
- do
- {
- if (nCharSum >= strText.size())
- break;
-
- str = strText.substr(0, ++nCharSum);
-
- if (IsHalfChineseWord(str.c_str()))
- {
- str = strText.substr(0, ++nCharSum);
- }
-
- GetTextExtentPoint32(m_dcPrinter, str.c_str(), str.length(), &fontSize);
- } while (fontSize.cx < nRectWide);
- str = strText.substr(0, --nCharSum);
- if (IsHalfChineseWord(str.c_str()))
- {
- --nCharSum;
- str = strText.substr(0, nCharSum);
- }
- strText = strText.substr(nCharSum);
- vtSubText.push_back(str);
- }
- else
- {
- do
- {
- if (nCharSum <= 0)
- break;
-
- str = strText.substr(0, --nCharSum);
-
- if (IsHalfChineseWord(str.c_str()))
- {
- str = strText.substr(0, --nCharSum);
- }
-
- GetTextExtentPoint32(m_dcPrinter, str.c_str(), str.length(), &fontSize);
- } while (fontSize.cx > nRectWide);
- str = strText.substr(0, ++nCharSum);
- if (IsHalfChineseWord(str.c_str()))
- {
- ++nCharSum;
- str = strText.substr(0, nCharSum);
- }
- strText = strText.substr(nCharSum);
- vtSubText.push_back(str);
- }
- GetTextExtentPoint32(m_dcPrinter, strText.c_str(), strText.length(), &fontSize);
- if (fontSize.cx <= nRectWide)
- vtSubText.push_back(strText);
- }
- else
- {
-
- vtSubText.push_back(strText);
- GetTextExtentPoint32(m_dcPrinter, strText.c_str(), strText.length(), &fontSize);
- }
- } while (fontSize.cx > nRectWide);
- }
- }
- INT CEasyPrinterV1::DrawRectange(IN CRect rc, IN INT nLineWide, IN INT leftLineType, IN INT topLineType, IN INT rightLineType, IN INT bottomLineType)
- {
- INT nDrawResult = 0;
- if (rc.top >= m_printlist.rcPage.bottom)
- {
-
- return nDrawResult;
- }
-
- if (rc.bottom > m_printlist.rcPage.bottom)
- {
-
- nDrawResult = 1;
- rc.bottom = m_printlist.rcPage.bottom;
-
-
- }
- else
- {
-
- nDrawResult = 2;
- }
- CPen pen[4];
-
- if (leftLineType == 0)
- pen[0].CreatePen(PS_NULL, nLineWide, RGB(0, 0, 0));
- else if (leftLineType == 1)
- pen[0].CreatePen(PS_DASH, nLineWide, RGB(0, 0, 0));
- else if (leftLineType == 2)
- pen[0].CreatePen(PS_SOLID, nLineWide, RGB(0, 0, 0));
-
- m_dcPrinter.SelectObject(&pen[0]);
- m_dcPrinter.MoveTo(rc.left, rc.top);
- m_dcPrinter.LineTo(rc.left, rc.bottom);
- pen[0].DeleteObject();
-
-
- if (topLineType == 0)
- pen[1].CreatePen(PS_NULL, nLineWide, RGB(0, 0, 0));
- else if (topLineType == 1)
- pen[1].CreatePen(PS_DASH, nLineWide, RGB(0, 0, 0));
- else if (topLineType == 2)
- pen[1].CreatePen(PS_SOLID, nLineWide, RGB(0, 0, 0));
-
- m_dcPrinter.SelectObject(&pen[1]);
- m_dcPrinter.MoveTo(rc.left, rc.top);
- m_dcPrinter.LineTo(rc.right, rc.top);
- pen[1].DeleteObject();
-
- if (rightLineType == 0)
- pen[2].CreatePen(PS_NULL, nLineWide, RGB(0, 0, 0));
- else if (rightLineType == 1)
- pen[2].CreatePen(PS_DASH, nLineWide, RGB(0, 0, 0));
- else if (rightLineType == 2)
- pen[2].CreatePen(PS_SOLID, nLineWide, RGB(0, 0, 0));
-
- m_dcPrinter.SelectObject(&pen[2]);
- m_dcPrinter.MoveTo(rc.right, rc.top);
- m_dcPrinter.LineTo(rc.right, rc.bottom);
- pen[2].DeleteObject();
-
- if (bottomLineType == 0)
- pen[3].CreatePen(PS_NULL, nLineWide, RGB(0, 0, 0));
- else if (bottomLineType == 1)
- pen[3].CreatePen(PS_DASH, nLineWide, RGB(0, 0, 0));
- else if (bottomLineType == 2)
- pen[3].CreatePen(PS_SOLID, nLineWide, RGB(0, 0, 0));
-
- m_dcPrinter.SelectObject(&pen[3]);
- m_dcPrinter.MoveTo(rc.left, rc.bottom);
- m_dcPrinter.LineTo(rc.right, rc.bottom);
- pen[3].DeleteObject();
- return nDrawResult;
- }
- void CEasyPrinterV1::DrawText(IN TString strText, IN CRect rcText, IN INT nTextWide, IN INT nAlignType )
- {
- switch (nAlignType)
- {
- case 0:
- break;
- case 1:
- rcText.left += (rcText.Width() - nTextWide) / 2;
- break;
- case 2:
- rcText.left += rcText.Width() - nTextWide;
- break;
- case 3:
- break;
- default:
- break;
- }
- m_dcPrinter.TextOut(rcText.left, rcText.top, strText.c_str());
- }
- void CEasyPrinterV1::CalcPrinterCell(IN STPrintCell &PrintCell)
- {
-
- PrintCell.rcChg = PrintCell.rcOrg;
-
- if (PrintCell.nFillerType == 0)
- {
- SIZE fontSize;
- INT nTextlen = PrintCell.strTextContent.size();
-
- SetTextFont(PrintCell.nTextFontSize, PrintCell.bTextBold, PrintCell.bTextItalic, PrintCell.strTextFontName);
- INT nRet = GetTextExtentPoint32(m_dcPrinter, PrintCell.strTextContent.c_str(), nTextlen, &fontSize);
- if (fontSize.cx > PrintCell.rcOrg.Width())
- {
- TextDownwards(PrintCell.strTextContent, PrintCell.rcOrg.Width() - PrintCell.nCellMargins * 2, PrintCell.vtSubText);
-
- if (PrintCell.bAutoWraps && PrintCell.vtSubText.size())
- {
-
- PrintCell.strTextContent.clear();
- if (!PrintCell.rcOrg.IsRectEmpty() && !PrintCell.rcOrg.IsRectNull())
- {
-
- PrintCell.rcChg.bottom = PrintCell.rcChg.top + PrintCell.vtSubText.size()*fontSize.cy + PrintCell.nCellMargins * 2 + PrintCell.nLineSpacing*(PrintCell.vtSubText.size() - 1);
- }
- }
- }
- }
- else if (PrintCell.nFillerType == 1)
- {
- TRACE("图片显示区域大小不需要改变\n");
- }
-
- if (PrintCell.rcOrg != PrintCell.rcChg)
- PrintCell.bHasWrapped = TRUE;
- else
- PrintCell.bHasWrapped = FALSE;
- }
- void CEasyPrinterV1::CellTBTypesetting()
- {
-
- vector<STPrintCell>::iterator it = m_printlist.vtPrintCell.begin();
- for (; it != m_printlist.vtPrintCell.end(); it++)
- {
- vector<STPrintCell>::iterator it1 = m_printlist.vtPrintCell.begin();
- for (; it1 != m_printlist.vtPrintCell.end(); it1++)
- {
- if (it != it1)
- {
- if (it->rcOrg.bottom == it1->rcOrg.top)
- {
-
- if (it->rcChg.bottom != it1->rcChg.top)
- {
- if (it1->bTypesetting)
- {
- if (it->rcChg.bottom > it1->rcChg.top)
- {
- it1->rcChg.OffsetRect(0, it->rcChg.bottom - it1->rcChg.top);
- }
- }
- else
- {
- if (it->rcChg.bottom > it1->rcChg.top)
- {
- it1->rcChg.OffsetRect(0, it->rcChg.bottom - it1->rcChg.top);
- }
- }
- }
- }
- }
- }
- }
- }
- void CEasyPrinterV1::CellBBTypesetting()
- {
-
- vector<STPrintCell>::iterator it = m_printlist.vtPrintCell.begin();
- for (; it != m_printlist.vtPrintCell.end(); it++)
- {
- vector<STPrintCell>::iterator it1 = m_printlist.vtPrintCell.begin();
- for (; it1 != m_printlist.vtPrintCell.end(); it1++)
- {
- if (it != it1)
- {
- if (it->rcOrg.bottom == it1->rcOrg.bottom)
- {
- if (it->rcChg.bottom != it1->rcChg.bottom)
- {
- if (it->rcChg.bottom > it1->rcChg.bottom)
- {
- it1->rcChg.bottom = it->rcChg.bottom;
- }
- else
- {
- it->rcChg.bottom = it1->rcChg.bottom;
- }
-
- CellTBTypesetting();
- }
- }
- }
- }
- }
- }
- BOOL CEasyPrinterV1::PrintByJson()
- {
-
- if (FALSE == InitialzePrinter(m_printlist.strPrinterName.c_str()))
- return FALSE;
-
- vector<STPrintCell>::iterator it = m_printlist.vtPrintCell.begin();
- for (; it != m_printlist.vtPrintCell.end(); it++)
- {
- CalcPrinterCell(*it);
- }
- CellTBTypesetting();
- CellBBTypesetting();
- #if 1
-
-
- it = m_printlist.vtPrintCell.begin();
- for (; it != m_printlist.vtPrintCell.end(); it++)
- {
- if (!it->bAutoWraps)
- {
- SIZE fontSize;
- INT nTextlen = it->strTextContent.size();
-
- SetTextFont(it->nTextFontSize, it->bTextBold, it->bTextItalic, it->strTextFontName);
- GetTextExtentPoint32(m_dcPrinter, "1", 1, &fontSize);
-
- INT nHeight = it->rcChg.top + it->vtSubText.size()*fontSize.cy + it->nCellMargins * 2 + it->nLineSpacing*(it->vtSubText.size() - 1);
- while (nHeight > it->rcChg.bottom)
- {
- if (it->nTextFontSize < 0)
- {
- WriteTextLog("出错:固定区域文本字体缩小成负值仍无法显示");
- break;
- }
- it->nTextFontSize--;
- SetTextFont(it->nTextFontSize, it->bTextBold, it->bTextItalic, it->strTextFontName);
- GetTextExtentPoint32(m_dcPrinter, it->strTextContent.c_str(), nTextlen, &fontSize);
- it->vtSubText.clear();
- TextDownwards(it->strTextContent, it->rcChg.Width() - it->nCellMargins * 2, it->vtSubText);
- nHeight = it->rcChg.top + it->vtSubText.size()*fontSize.cy + it->nCellMargins * 2 + it->nLineSpacing*(it->vtSubText.size() - 1);
- }
- if (it->vtSubText.size())
- it->strTextContent.clear();
- }
- }
-
-
- #endif
-
- it = m_printlist.vtPrintCell.begin();
- for (; it != m_printlist.vtPrintCell.end(); it++)
- {
- it->rcOrg = it->rcChg;
- }
-
- StartPrint();
- StartPage();
-
- INT nDrawResult = 0;
- if (m_printlist.bShowFrame)
- DrawRectange(m_printlist.rcPage, m_printlist.nLineWide, 2, 2, 2, 2);
- it = m_printlist.vtPrintCell.begin();
- for (; it != m_printlist.vtPrintCell.end();)
- {
- nDrawResult = DrawCell(*it);
- if (nDrawResult == 0 || nDrawResult == 1)
- {
-
- it++;
- }
- else if (nDrawResult == 2)
- {
-
- it = m_printlist.vtPrintCell.erase(it);
- }
- }
- while (m_printlist.vtPrintCell.size())
- {
- PrintChangePage();
- }
- EndofPage();
- EndofPrint();
- return TRUE;
- }
- INT CEasyPrinterV1::DrawCell(IN STPrintCell & PrtCell)
- {
-
- BOOL bChangePage = FALSE;
-
- INT nDrawResult = DrawRectange(PrtCell.rcChg, PrtCell.nLineWide, PrtCell.nLeftLineType, PrtCell.nTopLineType, PrtCell.nRightLineType, PrtCell.nBottomLineType);
- if (nDrawResult == 0)
- {
-
- return nDrawResult;
- }
- SIZE fontSize;
- if (PrtCell.nFillerType == 0)
- {
-
- CRect rcText = PrtCell.rcChg;
- rcText.DeflateRect(PrtCell.nCellMargins, PrtCell.nCellMargins, PrtCell.nCellMargins, 0);
- SetTextFont(PrtCell.nTextFontSize, PrtCell.bTextBold, PrtCell.bTextItalic, PrtCell.strTextFontName);
-
- if (PrtCell.vtSubText.size())
- {
- GetTextExtentPoint32(m_dcPrinter, _T("1"), 1, &fontSize);
- INT nHeight = rcText.top + PrtCell.nLineSpacing + fontSize.cy;
- vector<TString>::iterator it = PrtCell.vtSubText.begin();
- for (; it != PrtCell.vtSubText.end();)
- {
-
- if (m_printlist.rcPage.bottom < nHeight)
- {
-
- it++;
- }
- else
- {
-
- GetTextExtentPoint32(m_dcPrinter, it->c_str(), it->length(), &fontSize);
- DrawText(*it, rcText, fontSize.cx, PrtCell.nTextAlignType);
- rcText.top += PrtCell.nLineSpacing + fontSize.cy;
- rcText.bottom = rcText.top + PrtCell.nLineSpacing + fontSize.cy;
-
- it = PrtCell.vtSubText.erase(it);
- nHeight = rcText.bottom;
- }
- }
- }
- else
- {
- if (PrtCell.strTextContent.length())
- {
- GetTextExtentPoint32(m_dcPrinter, PrtCell.strTextContent.c_str(), PrtCell.strTextContent.length(), &fontSize);
-
- if (m_printlist.rcPage.bottom < (rcText.top + fontSize.cy + PrtCell.nCellMargins * 2))
- {
-
-
- }
- else
- {
- DrawText(PrtCell.strTextContent, rcText, fontSize.cx, PrtCell.nTextAlignType);
-
- PrtCell.strTextContent.clear();
- }
- }
- }
- }
- else if (PrtCell.nFillerType == 1)
- {
- TString strImageBase64 = PrtCell.strImgContent;
- BYTE *pImageBuffer = NULL;
- DWORD dwImageLength = 0;
- GetImgBufferFromBase64(PrtCell.strImgContent, &pImageBuffer, dwImageLength);
- Image *pImage = NULL;
- LoadImgFromBuffer(&pImage, pImageBuffer, dwImageLength);
-
- Graphics graph(m_dcPrinter);
- graph.SetPageUnit(UnitPixel);
- graph.SetSmoothingMode(SmoothingModeHighQuality);
- RectF rf(PrtCell.rcOrg.left, PrtCell.rcOrg.top, PrtCell.rcOrg.Width(), PrtCell.rcOrg.Height());
- graph.DrawImage(pImage, rf, 0, 0, pImage->GetWidth(), pImage->GetHeight(), UnitPixel);
-
- if (pImageBuffer)
- delete[]pImageBuffer;
- if (pImage)
- delete pImage;
- }
- if (nDrawResult == 1)
- {
- PrtCell.bDrawHalf = TRUE;
-
- if (PrtCell.nFillerType == 0)
- {
- INT nHeightGap = PrtCell.rcChg.bottom - m_printlist.rcPage.bottom;
- PrtCell.rcChg.top = PrtCell.rcChg.bottom - nHeightGap;
- }
- else if (PrtCell.nFillerType == 1)
- {
-
- TRACE("暂时未处理\n");
- }
- }
- else if (nDrawResult == 2)
- {
-
- TRACE("不需要处理\n");
- }
- return nDrawResult;
- }
- void CEasyPrinterV1::PrintChangePage()
- {
- AutoChangePage();
-
- NewAnPage();
- INT nDrawResult = 0;
- vector<STPrintCell>::iterator it = m_printlist.vtPrintCell.begin();
- for (; it != m_printlist.vtPrintCell.end();)
- {
- nDrawResult = DrawCell(*it);
- if (nDrawResult == 0 || nDrawResult == 1)
- {
-
- it++;
- }
- else if (nDrawResult == 2)
- {
-
- it = m_printlist.vtPrintCell.erase(it);
- }
- }
- }
- void CEasyPrinterV1::AutoChangePage()
- {
-
- INT nOffsetGap = m_printlist.rcPage.top - m_printlist.rcPage.bottom;
- vector<STPrintCell>::iterator it = m_printlist.vtPrintCell.begin();
- for (; it != m_printlist.vtPrintCell.end(); it++)
- {
-
- if (it->bDrawHalf)
- {
- it->rcOrg.OffsetRect(0, nOffsetGap);
- it->rcOrg.top = m_printlist.rcPage.top;
- it->rcChg.OffsetRect(0, nOffsetGap);
- it->rcChg.top = m_printlist.rcPage.top;
- }
- else
- {
- it->rcOrg.OffsetRect(0, nOffsetGap);
- it->rcChg.OffsetRect(0, nOffsetGap);
- }
- }
-
- it = m_printlist.vtPrintCell.begin();
- for (; it != m_printlist.vtPrintCell.end(); it++)
- {
- if (it->nFillerType == 0)
- {
- if (it->vtSubText.size() != 0)
- {
- SIZE fontSize;
-
- SetTextFont(it->nTextFontSize, it->bTextBold, it->bTextItalic, it->strTextFontName);
- GetTextExtentPoint32(m_dcPrinter, "1", 1, &fontSize);
-
- INT nHeight = it->vtSubText.size()*fontSize.cy + it->nCellMargins * 2 + it->nLineSpacing*(it->vtSubText.size() - 1);
- if (it->rcOrg.Height() < nHeight)
- {
- if (it->bAutoWraps)
- {
- it->rcChg.bottom = it->rcChg.top + nHeight;
- }
- else
- {
- do
- {
- SetTextFont(it->nTextFontSize--, it->bTextBold, it->bTextItalic, it->strTextFontName);
- GetTextExtentPoint32(m_dcPrinter, "1", 1, &fontSize);
- nHeight = it->vtSubText.size()*fontSize.cy + it->nCellMargins * 2 + it->nLineSpacing*(it->vtSubText.size() - 1);
- } while (it->rcOrg.Height() < nHeight);
- }
- }
- }
- }
- else if (it->nFillerType == 1)
- {
- TRACE("图片显示区域大小不需要改变\n");
- }
- }
- CellTBTypesetting();
- CellBBTypesetting();
- }
|