Commit c02df701 authored by Ilya.Kirillov's avatar Ilya.Kirillov Committed by Alexander Trofimov

Сделано ускорение записи последовательного текста.

git-svn-id: svn://fileserver/activex/AVS/Sources/TeamlabOffice/trunk/ServerComponents@63292 954022d7-b5bf-4e40-9824-e11837661b57
parent 97c3e9ed
......@@ -243,8 +243,72 @@ void CPdfRenderer::CCommandManager::Flush()
double dTextSize = -1;
LONG lTextColor = 0;
BYTE nTextAlpha = 255;
double dTextSpace = 0;
double dTextSpace = 0;
double dPrevX = -1000;
double dPrevY = -1000;
unsigned short ushPrevCode = 0;
class CContiniousText
{
public:
CContiniousText()
{
m_nIndex = 0;
}
void Reset()
{
m_nIndex = 0;
}
bool Add(unsigned char* pCodes, unsigned int unLen, double dX, double dY, double dWidth)
{
if (2 != unLen)
return false;
if (0 == m_nIndex)
{
m_pText[0] = pCodes[0];
m_pText[1] = pCodes[1];
m_nIndex++;
m_dStartX = dX;
m_dStartY = dY;
m_dCurX = dX + dWidth;
}
else
{
if (abs(dY - m_dStartY) > 0.001 || abs(dX - m_dCurX) > 0.01)
return false;
m_pText[m_nIndex * 2 + 0] = pCodes[0];
m_pText[m_nIndex * 2 + 1] = pCodes[1];
m_nIndex++;
m_dCurX = dX + dWidth;
}
return true;
}
void Flush(PdfWriter::CPage* pPage)
{
if (m_nIndex > 0)
pPage->DrawText(m_dStartX, m_dStartY, m_pText, m_nIndex * 2);
m_nIndex = 0;
}
private:
unsigned char m_pText[200];
int m_nIndex;
double m_dStartX;
double m_dStartY;
double m_dCurX;
};
CContiniousText oContText;
for (int nIndex = 0; nIndex < nCommandsCount; nIndex++)
{
pText = (CRendererTextCommand*)m_vCommands.at(nIndex);
......@@ -271,32 +335,57 @@ void CPdfRenderer::CCommandManager::Flush()
if (pTextFont != pText->GetFont() || abs(dTextSize - pText->GetSize()) > 0.001)
{
oContText.Flush(pPage);
pTextFont = pText->GetFont();
dTextSize = pText->GetSize();
pPage->SetFontAndSize(pTextFont, dTextSize);
pPage->SetFontAndSize(pTextFont, dTextSize);
}
if (lTextColor != pText->GetColor())
{
oContText.Flush(pPage);
lTextColor = pText->GetColor();
TColor oColor = lTextColor;
pPage->SetFillColor(oColor.r, oColor.g, oColor.b);
pPage->SetFillColor(oColor.r, oColor.g, oColor.b);
}
if (nTextAlpha != pText->GetAlpha())
{
oContText.Flush(pPage);
nTextAlpha = pText->GetAlpha();
pPage->SetFillAlpha(nTextAlpha);
pPage->SetFillAlpha(nTextAlpha);
}
if (abs(dTextSpace - pText->GetSpace()) > 0.001)
{
oContText.Flush(pPage);
dTextSpace = pText->GetSpace();
pPage->SetCharSpace(dTextSpace);
pPage->SetCharSpace(dTextSpace);
}
//------------------------------------
unsigned char* pCodes = pText->GetCodes();
unsigned short ushCode = (pCodes[0] << 8) + pCodes[1];
unsigned int unLen = pText->GetCodesLen();
double dX = pText->GetX();
double dY = pText->GetY();
double dWidth = ((CFontCidTrueType*)pText->GetFont())->GetWidth(ushCode) / 1000.0 * pText->GetSize();
if (!oContText.Add(pCodes, unLen, dX, dY, dWidth))
{
oContText.Flush(pPage);
if (!oContText.Add(pCodes, unLen, dX, dY, dWidth))
{
pPage->DrawText(dX, dY, pCodes, unLen);
}
}
//-----------------------------------
pPage->DrawText(pText->GetX(), pText->GetY(), pText->GetCodes(), pText->GetCodesLen() * 2);
//pPage->DrawText(pText->GetX(), pText->GetY(), pText->GetCodes(), pText->GetCodesLen());
}
oContText.Flush(pPage);
pPage->EndText();
}
......@@ -319,6 +408,10 @@ void CPdfRenderer::CCommandManager::SetTransform(const CTransform& oTransform)
{
m_oTransform = oTransform;
}
void CPdfRenderer::CCommandManager::SetTransform(const double& m11, const double& m12, const double& m21, const double& m22, const double& dx, const double& dy)
{
m_oTransform.Set(m11, m12, m21, m22, dx, dy);
}
//----------------------------------------------------------------------------------------
//
// CPdfRenderer
......@@ -879,12 +972,15 @@ HRESULT CPdfRenderer::CommandDrawText(const std::wstring& wsUnicodeText, const d
unsigned char* pCodes = m_pFont->EncodeString(pUnicodes, unLen);
delete[] pUnicodes;
CTransform& t = m_oTransform;
m_oCommandManager.SetTransform(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));
CRendererTextCommand* pText = m_oCommandManager.AddText(pCodes, unLen * 2, MM_2_PT(dX), MM_2_PT(m_dPageHeight - dY));
pText->SetFont(m_pFont);
pText->SetSize(m_oFont.GetSize());
pText->SetColor(m_oBrush.GetColor1());
pText->SetAlpha((BYTE)m_oBrush.GetAlpha1());
pText->SetCharSpace(m_oFont.GetCharSpace());
pText->SetCharSpace(MM_2_PT(m_oFont.GetCharSpace()));
return S_OK;
}
......
......@@ -1450,6 +1450,7 @@ private:
CRendererTextCommand* AddText(unsigned char* pCodes, unsigned int nLen, const double& dX, const double& dY);
void Flush();
void SetTransform(const CTransform& oTransform);
void SetTransform(const double& m11, const double& m12, const double& m21, const double& m22, const double& dx, const double& dy);
private:
void Add(CRendererCommandBase* pCommand);
void Clear();
......
......@@ -35,6 +35,8 @@
#include "../../DesktopEditor/fontengine/ApplicationFonts.h"
#include "../../DesktopEditor/raster/Metafile/MetaFile.h"
#include <vector>
#include <ctime>
#ifdef DrawText
......@@ -924,6 +926,8 @@ void TestOnlineBin()
CApplicationFonts oFonts;
oFonts.Initialize();
clock_t oBeginTime = clock();
double dPx2Mm = 25.4 / 96;
std::vector<std::wstring> vFiles = GetAllFilesInFolder(wsFolderPath, L"txt");
for (int nIndex = 0; nIndex < vFiles.size(); nIndex++)
......@@ -938,6 +942,11 @@ void TestOnlineBin()
printf("%d of %d %S\n", nIndex, vFiles.size(), vFiles.at(nIndex).c_str());
}
clock_t oEndTime = clock();
double dElapsedSecs = double(oEndTime - oBeginTime) / CLOCKS_PER_SEC;
printf("%f\n", dElapsedSecs);
}
void main()
......@@ -957,4 +966,7 @@ void main()
//TestDocument9();
//TestMetafile();
TestOnlineBin();
char q;
std::cin >> q;
}
......@@ -107,13 +107,46 @@ namespace PdfWriter
m_pToUnicodeStream = pToUnicodeDict->GetStream();
CreateCIDFont2(pFont);
//-----------------------------------------------------------------------------------------
FT_Init_FreeType(&m_pLibrary);
NSFile::CFileBinary oFile;
oFile.OpenFile(m_wsFontPath, false);
DWORD lFileSize = oFile.SizeFile();
m_pFaceMemory = new FT_Byte[lFileSize];
if (!m_pFaceMemory)
{
FT_Done_FreeType(m_pLibrary);
return;
}
NSFile::CFileBinary::ReadAllBytes(m_wsFontPath, &m_pFaceMemory, lFileSize);
FT_New_Memory_Face(m_pLibrary, m_pFaceMemory, lFileSize, m_unFontIndex, &m_pFace);
if (!m_pFace)
{
FT_Done_FreeType(m_pLibrary);
delete[] m_pFaceMemory;
m_pFaceMemory = NULL;
return;
}
}
CFontCidTrueType::~CFontCidTrueType()
{
if (m_pFontFile)
delete m_pFontFile;
if (m_pFace)
FT_Done_Face(m_pFace);
if (m_pLibrary)
FT_Done_FreeType(m_pLibrary);
if (m_pFaceMemory)
delete[] m_pFaceMemory;
}
void CFontCidTrueType::CreateCIDFont2(CDictObject* pFont)
void CFontCidTrueType::CreateCIDFont2(CDictObject* pFont)
{
m_pFont = pFont;
pFont->Add("Subtype", "CIDFontType2");
......@@ -164,7 +197,7 @@ namespace PdfWriter
pCIDToGIDMapDict->SetFilter(STREAM_FILTER_FLATE_DECODE);
m_pCidToGidMapStream = pCIDToGIDMapDict->GetStream();
}
unsigned char* CFontCidTrueType::EncodeString(unsigned int* pUnicodes, unsigned int unLen)
unsigned char* CFontCidTrueType::EncodeString(unsigned int* pUnicodes, unsigned int unLen)
{
unsigned char* pEncodedString = new unsigned char[unLen * 2];
......@@ -181,11 +214,39 @@ namespace PdfWriter
}
else
{
m_mUnicodeToCode.insert(std::pair<unsigned int, unsigned short>(pUnicodes[unIndex], m_ushCodesCount));
unsigned int unUnicode = pUnicodes[unIndex];
ushCode = m_ushCodesCount;
m_vUnicodes.push_back(pUnicodes[unIndex]);
m_ushCodesCount++;
m_mUnicodeToCode.insert(std::pair<unsigned int, unsigned short>(unUnicode, ushCode));
m_vUnicodes.push_back(unUnicode);
unsigned short ushGID = GetGID(m_pFace, unUnicode);
m_vCodeToGid.push_back(ushGID);
//
m_mGlyphs.insert(std::pair<unsigned short, bool>(ushGID, true));
// (CompositeGlyf), (subglyfs)
if (0 == FT_Load_Glyph(m_pFace, ushGID, FT_LOAD_NO_SCALE | FT_LOAD_NO_RECURSE))
{
for (int nSubIndex = 0; nSubIndex < m_pFace->glyph->num_subglyphs; nSubIndex++)
{
FT_Int nSubGID;
FT_UInt unFlags;
FT_Int nArg1;
FT_Int nArg2;
FT_Matrix oMatrix;
FT_Get_SubGlyph_Info(m_pFace->glyph, nSubIndex, &nSubGID, &unFlags, &nArg1, &nArg2, &oMatrix);
m_mGlyphs.insert(std::pair<unsigned short, bool>(nSubGID, true));
}
if (0 != m_pFace->units_per_EM)
m_vWidths.push_back((unsigned int)m_pFace->glyph->metrics.horiAdvance * 1000 / m_pFace->units_per_EM);
else
m_vWidths.push_back((unsigned int)m_pFace->glyph->metrics.horiAdvance);
}
}
pEncodedString[2 * unIndex + 0] = (ushCode >> 8) & 0xFF;
......@@ -193,7 +254,14 @@ namespace PdfWriter
}
return pEncodedString;
}
void CFontCidTrueType::BeforeWrite()
unsigned int CFontCidTrueType::GetWidth(unsigned short ushCode)
{
if (ushCode >= m_vWidths.size())
return 0;
return m_vWidths.at(ushCode);
}
void CFontCidTrueType::BeforeWrite()
{
if (m_pFontFile)
{
......@@ -230,7 +298,7 @@ namespace PdfWriter
WriteToUnicode();
}
}
void CFontCidTrueType::GetWidthsAndGids(unsigned short** ppCodeToGid, unsigned int** ppWidths, unsigned char** ppGlyphs, unsigned int& unGlyphsCount)
void CFontCidTrueType::GetWidthsAndGids(unsigned short** ppCodeToGid, unsigned int** ppWidths, unsigned char** ppGlyphs, unsigned int& unGlyphsCount)
{
FT_Library pLibrary = NULL;
FT_Init_FreeType(&pLibrary);
......@@ -321,13 +389,14 @@ namespace PdfWriter
FT_Done_Face(pFace);
FT_Done_FreeType(pLibrary);
delete[] pMemory;
*ppCodeToGid = pCodeToGID;
*ppWidths = pWidths;
*ppGlyphs = pUseGlyf;
unGlyphsCount = lGlyfsCount;
}
void CFontCidTrueType::WriteToUnicode()
void CFontCidTrueType::WriteToUnicode()
{
CStream* pS = m_pToUnicodeStream;
......
......@@ -7,6 +7,10 @@
#include <map>
#include <vector>
#include "../../DesktopEditor/fontengine/FontManager.h"
#include "../../DesktopEditor/common/File.h"
#include FT_TRUETYPE_TABLES_H
namespace PdfWriter
{
class CXref;
......@@ -23,6 +27,7 @@ namespace PdfWriter
CFontCidTrueType(CXref* pXref, CDocument* pDocument, const std::wstring& wsFontPath, unsigned int unIndex);
~CFontCidTrueType();
unsigned char* EncodeString(unsigned int* pUnicodes, unsigned int unLen);
unsigned int GetWidth(unsigned short ushCode);
EFontType GetFontType()
{
return fontCIDType2;
......@@ -51,6 +56,14 @@ namespace PdfWriter
unsigned short m_ushCodesCount; //
std::map<unsigned int, unsigned short> m_mUnicodeToCode; // ->
std::vector<unsigned int> m_vUnicodes; // ->
std::vector<unsigned short> m_vCodeToGid;
std::vector<unsigned int> m_vWidths;
std::map<unsigned short, bool> m_mGlyphs;
FT_Library m_pLibrary;
FT_Face m_pFace;
FT_Byte* m_pFaceMemory;
};
}
......
......@@ -925,6 +925,34 @@ namespace PdfWriter
MoveTextPos(dX, dY);
ShowText(sText, unLen);
}
void CPage::DrawTextArray(double dXpos, double dYpos, const BYTE** ppTexts, unsigned int* pLens, unsigned int unCount, double* pShifts)
{
CheckGrMode(grmode_TEXT);
double dX = 0.0;
double dY = 0.0;
if (0 == m_oTextMatrix.m11)
{
dY = (dXpos - m_oTextMatrix.x) / m_oTextMatrix.m21;
dX = (dYpos - m_oTextMatrix.y - (dXpos - m_oTextMatrix.x) * m_oTextMatrix.m22 / m_oTextMatrix.m21) / m_oTextMatrix.m12;
}
else
{
dY = (dYpos - m_oTextMatrix.y - (dXpos - m_oTextMatrix.x) * m_oTextMatrix.m12 / m_oTextMatrix.m11) / (m_oTextMatrix.m22 - m_oTextMatrix.m21 * m_oTextMatrix.m12 / m_oTextMatrix.m11);
dX = (dXpos - m_oTextMatrix.x - dY * m_oTextMatrix.m21) / m_oTextMatrix.m11;
}
MoveTextPos(dX, dY);
m_pStream->WriteChar('[');
for (unsigned int unIndex = 0; unIndex < unCount; unIndex++)
{
WriteText(ppTexts[unIndex], pLens[unIndex]);
if (unIndex != unCount - 1)
m_pStream->WriteReal(pShifts[unIndex]);
}
m_pStream->WriteStr("]TJ\012");
}
void CPage::SetCharSpace(double dValue)
{
// Operator : Tc
......
......@@ -97,6 +97,9 @@ namespace PdfWriter
void SetTextRenderingMode(ETextRenderingMode eMode);
void SetTextMatrix(double dM11, double dM12, double dM21, double dM22, double dX, double dY);
void DrawTextArray(double dX, double dY, const BYTE** ppTexts, unsigned int* pLens, unsigned int unCount, double* pShifts);
void ExecuteXObject(CXObject* pXObject);
void DrawImage(CImageDict* pImage, double dX, double dY, double dWidth, double dHeight);
void SetPatternColorSpace(CImageTilePattern* pPattern);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment