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

Немного переделана работа со шрифтами: теперь список открытых FontFace...

Немного переделана работа со шрифтами: теперь список открытых FontFace храниться в документе, и если происходит их переполнение, то самые неиспользуемые закрываются.

git-svn-id: svn://fileserver/activex/AVS/Sources/TeamlabOffice/trunk/ServerComponents@63326 954022d7-b5bf-4e40-9824-e11837661b57
parent 6af1ad10
......@@ -318,6 +318,7 @@ namespace NSOnlineOfficeBinToPdf
bIsPathOpened = false;
pPdf->EndCommand(c_nPageType);
break;
}
case ctPenColor:
......
......@@ -232,8 +232,6 @@ private:
double m_dX;
double m_dY;
int m_nUpdateFlags;
CFontDict* m_pFont;
double m_dSize;
LONG m_lColor;
......@@ -382,7 +380,7 @@ void CPdfRenderer::CCommandManager::Clear()
for (int nIndex = 0, nCount = m_vCommands.size(); nIndex < nCount; nIndex++)
{
CRendererCommandBase* pCommand = m_vCommands.at(nIndex);
delete[] pCommand;
delete pCommand;
}
m_vCommands.clear();
}
......@@ -1374,7 +1372,6 @@ bool CPdfRenderer::DrawText(unsigned int* pUnicodes, unsigned int unLen, const d
return false;
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));
......@@ -1444,8 +1441,8 @@ void CPdfRenderer::UpdateTransform()
t.m12 = -m_oTransform.m12;
t.m21 = -m_oTransform.m21;
t.m22 = m_oTransform.m22;
t.dx = MM_2_PT(t.dx + t.m21 * m_dPageHeight);
t.dy = MM_2_PT(m_dPageHeight - m_dPageHeight * t.m22 - t.dy);
t.dx = MM_2_PT(m_oTransform.dx + m_oTransform.m21 * m_dPageHeight);
t.dy = MM_2_PT(m_dPageHeight - m_dPageHeight * m_oTransform.m22 - m_oTransform.dy);
if (!t.IsIdentity())
m_pPage->Concat(t.m11, t.m12, t.m21, t.m22, t.dx, t.dy);
......@@ -1629,6 +1626,7 @@ void CPdfRenderer::UpdateBrush()
m_pShading = m_pDocument->CreateRadialShading(m_pPage, MM_2_PT(dX0), MM_2_PT(m_dPageHeight - dY0), MM_2_PT(dR0), MM_2_PT(dX1), MM_2_PT(m_dPageHeight - dY1), MM_2_PT(dR1), pColors, pAlphas, pPoints, lCount, m_pShadingExtGrState);
}
delete[] pColors;
delete[] pAlphas;
}
}
}
......
......@@ -913,8 +913,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//Wmf//", MetaFile::c_lMetaWmf);
ConvertFolder(L"D://Test Files//Emf//", MetaFile::c_lMetaEmf);
//ConvertFolder(L"D://Test Files//Wmf//", MetaFile::c_lMetaWmf);
}
void TestOnlineBin()
{
......@@ -961,8 +961,8 @@ void main()
//TestDocument7();
//TestDocument8();
//TestDocument9();
//TestMetafile();
TestOnlineBin();
TestMetafile();
//TestOnlineBin();
char q;
std::cin >> q;
......
This diff is collapsed.
......@@ -44,6 +44,7 @@ namespace PdfWriter
m_pJbig2 = NULL;
memset((void*)m_sTTFontTag, 0x00, 8);
m_pTransparencyGroup = NULL;
m_pFreeTypeLibrary = NULL;
}
CDocument::~CDocument()
{
......@@ -93,27 +94,35 @@ namespace PdfWriter
void CDocument::Close()
{
// Все объекты удаляются внутри CXref
if (m_pXref)
{
delete m_pXref;
m_pXref = NULL;
}
RELEASEOBJECT(m_pXref);
m_pTrailer = NULL;
m_pResources = NULL;
m_pCatalog = NULL;
m_pOutlines = NULL;
m_pPageTree = NULL;
m_pCurPage = NULL;
m_nCurPageNum = 0;
m_bEncrypt = false;
m_pEncryptDict = NULL;
m_pInfo = NULL;
m_unCompressMode = COMP_NONE;
m_pJbig2 = NULL;
m_pTransparencyGroup= NULL;
memset((void*)m_sTTFontTag, 0x00, 8);
m_vExtGrStates.clear();
m_vPages.clear();
m_vExtGrStates.clear();
m_vStrokeAlpha.clear();
m_vFillAlpha.clear();
m_vShadings.clear();
m_vTTFonts.clear();
m_vFreeTypeFonts.clear();
if (m_pFreeTypeLibrary)
{
FT_Done_FreeType(m_pFreeTypeLibrary);
m_pFreeTypeLibrary = NULL;
}
}
bool CDocument::SaveToFile(const std::wstring& wsPath)
{
......@@ -453,6 +462,41 @@ namespace PdfWriter
return m_sTTFontTag;
}
void CDocument::AddFreeTypeFont(CFontCidTrueType* pFont)
{
for (int nIndex = 0, nCount = m_vFreeTypeFonts.size(); nIndex < nCount; nIndex++)
{
if (pFont == m_vFreeTypeFonts.at(nIndex))
{
if (nIndex >= 10)
{
m_vFreeTypeFonts.erase(m_vFreeTypeFonts.begin() + nIndex);
m_vFreeTypeFonts.insert(m_vFreeTypeFonts.begin(), pFont);
}
return;
}
}
m_vFreeTypeFonts.insert(m_vFreeTypeFonts.begin(), pFont);
int nFontsCount = m_vFreeTypeFonts.size();
if (nFontsCount > MAX_OPENED_FT_FACES)
{
for (int nFontIndex = MAX_OPENED_FT_FACES; nFontIndex < nFontsCount; nFontIndex++)
{
CFontCidTrueType* pFont = m_vFreeTypeFonts.at(nFontIndex);
pFont->CloseFontFace();
}
m_vFreeTypeFonts.erase(m_vFreeTypeFonts.begin() + MAX_OPENED_FT_FACES, m_vFreeTypeFonts.end());
}
}
FT_Library CDocument::GetFreeTypeLibrary()
{
if (!m_pFreeTypeLibrary)
FT_Init_FreeType(&m_pFreeTypeLibrary);
return m_pFreeTypeLibrary;
}
CJbig2Global* CDocument::GetJbig2Global()
{
if (m_pJbig2 && m_pJbig2->GetImagesCount() > 4)
......
......@@ -5,6 +5,10 @@
#include <string>
#include "Types.h"
#include "../../DesktopEditor/fontengine/FontManager.h"
#include "../../DesktopEditor/common/File.h"
#include FT_TRUETYPE_TABLES_H
#ifdef CreateFont
#undef CreateFont
#endif
......@@ -79,6 +83,8 @@ namespace PdfWriter
private:
char* GetTTFontTag();
void AddFreeTypeFont(CFontCidTrueType* pFont);
FT_Library GetFreeTypeLibrary();
CExtGrState* FindExtGrState(double dAlphaStroke = -1, double dAlphaFill = -1, EBlendMode eMode = blendmode_Unknown, int nStrokeAdjustment = -1);
void SaveToStream(CStream* pStream);
void PrepareEncryption();
......@@ -124,6 +130,8 @@ namespace PdfWriter
std::vector<CShading*> m_vShadings;
std::vector<TFontInfo> m_vTTFonts;
CDictObject* m_pTransparencyGroup;
std::vector<CFontCidTrueType*> m_vFreeTypeFonts;
FT_Library m_pFreeTypeLibrary;
friend class CFontCidTrueType;
};
......
......@@ -108,29 +108,10 @@ namespace PdfWriter
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_pFace = NULL;
m_pFaceMemory = NULL;
return;
}
m_nGlyphsCount = 0;
m_nSymbolicCmap = -1;
}
CFontCidTrueType::~CFontCidTrueType()
{
......@@ -140,9 +121,6 @@ namespace PdfWriter
if (m_pFace)
FT_Done_Face(m_pFace);
if (m_pLibrary)
FT_Done_FreeType(m_pLibrary);
if (m_pFaceMemory)
delete[] m_pFaceMemory;
}
......@@ -199,7 +177,12 @@ namespace PdfWriter
}
unsigned char* CFontCidTrueType::EncodeString(unsigned int* pUnicodes, unsigned int unLen)
{
if (!OpenFontFace())
return NULL;
unsigned char* pEncodedString = new unsigned char[unLen * 2];
if (!pEncodedString)
return NULL;
// 0x0000..0xFFFF
// Gid .
......@@ -222,6 +205,9 @@ namespace PdfWriter
m_vUnicodes.push_back(unUnicode);
unsigned short ushGID = GetGID(m_pFace, unUnicode);
if (0 == ushGID && -1 != m_nSymbolicCmap)
ushGID = GetGID(m_pFace, unUnicode + 0xF000);
m_vCodeToGid.push_back(ushGID);
//
......@@ -270,7 +256,8 @@ namespace PdfWriter
unsigned char* pGlyphs;
unsigned int unGlyphsCount;
GetWidthsAndGids(&pCodeToGid, &pWidths, &pGlyphs, unGlyphsCount);
if (!GetWidthsAndGids(&pCodeToGid, &pWidths, &pGlyphs, unGlyphsCount))
return;
CStream* pStream = m_pFontFileDict->GetStream();
m_pFontFile->WriteTTF(pStream, NULL, pCodeToGid, m_ushCodesCount, pGlyphs, unGlyphsCount);
......@@ -295,106 +282,60 @@ namespace PdfWriter
pStream->WriteChar((ushGid & 0xFF));
}
RELEASEARRAYOBJECTS(pCodeToGid);
RELEASEARRAYOBJECTS(pWidths);
RELEASEARRAYOBJECTS(pGlyphs);
WriteToUnicode();
}
}
void CFontCidTrueType::GetWidthsAndGids(unsigned short** ppCodeToGid, unsigned int** ppWidths, unsigned char** ppGlyphs, unsigned int& unGlyphsCount)
bool CFontCidTrueType::GetWidthsAndGids(unsigned short** ppCodeToGid, unsigned int** ppWidths, unsigned char** ppGlyphs, unsigned int& unGlyphsCount)
{
FT_Library pLibrary = NULL;
FT_Init_FreeType(&pLibrary);
FT_Face pFace = NULL;
*ppCodeToGid = NULL;
*ppWidths = NULL;
*ppGlyphs = NULL;
unGlyphsCount = 0;
NSFile::CFileBinary oFile;
oFile.OpenFile(m_wsFontPath, false);
DWORD lFileSize = oFile.SizeFile();
FT_Byte* pMemory = new FT_Byte[lFileSize];
if (!pMemory)
{
FT_Done_FreeType(pLibrary);
return;
}
if (!m_nGlyphsCount)
return false;
NSFile::CFileBinary::ReadAllBytes(m_wsFontPath, &pMemory, lFileSize);
unsigned short* pCodeToGID = new unsigned short[m_ushCodesCount];
if (!pCodeToGID)
return false;
FT_New_Memory_Face(pLibrary, pMemory, lFileSize, m_unFontIndex, &pFace);
if (!pFace)
unsigned int* pWidths = new unsigned int[m_ushCodesCount];
if (!pWidths)
{
FT_Done_FreeType(pLibrary);
return;
delete[] pCodeToGID;
return false;
}
memset((void*)pWidths, 0x00, m_ushCodesCount * sizeof(unsigned int));
int nSymbolic = GetSymbolicCmapIndex(pFace);
unsigned short* pCodeToGID = new unsigned short[m_ushCodesCount];
for (unsigned short ushCode = 0; ushCode < m_ushCodesCount; ushCode++)
{
unsigned int unUnicode = m_vUnicodes[ushCode];
pCodeToGID[ushCode] = GetGID(pFace, unUnicode);
if (0 == pCodeToGID[ushCode] && -1 != nSymbolic)
pCodeToGID[ushCode] = GetGID(pFace, unUnicode + 0xF000);
pCodeToGID[ushCode] = m_vCodeToGid.at(ushCode);
pWidths[ushCode] = m_vWidths.at(ushCode);
}
// ,
long lGlyfsCount = pFace->num_glyphs;
unsigned char *pUseGlyf = new unsigned char[lGlyfsCount];
memset((void *)pUseGlyf, 0x00, pFace->num_glyphs * sizeof(unsigned char));
//
unsigned int* pWidths = new unsigned int[m_ushCodesCount];
memset((void*)pWidths, 0x00, m_ushCodesCount * sizeof(unsigned int));
for (unsigned short ushCode = 0; ushCode < m_ushCodesCount; ushCode++)
unsigned char *pGlyphs = new unsigned char[m_nGlyphsCount];
if (!pGlyphs)
{
unsigned short ushGID = pCodeToGID[ushCode];
if ((long)ushGID >= lGlyfsCount)
continue;
//
pUseGlyf[ushGID] = 1;
// (CompositeGlyf), (subglyfs)
if (0 == FT_Load_Glyph(pFace, ushGID, FT_LOAD_NO_SCALE | FT_LOAD_NO_RECURSE))
{
for (int nSubIndex = 0; nSubIndex < pFace->glyph->num_subglyphs; nSubIndex++)
{
FT_Int nSubGID;
FT_UInt unFlags;
FT_Int nArg1;
FT_Int nArg2;
FT_Matrix oMatrix;
FT_Get_SubGlyph_Info(pFace->glyph, nSubIndex, &nSubGID, &unFlags, &nArg1, &nArg2, &oMatrix);
pUseGlyf[nSubGID] = 1;
delete[] pCodeToGID;
delete[] pWidths;
return false;
}
if (0 != pFace->units_per_EM)
pWidths[ushCode] = (unsigned int)pFace->glyph->metrics.horiAdvance * 1000 / pFace->units_per_EM;
else
pWidths[ushCode] = (unsigned int)pFace->glyph->metrics.horiAdvance;
}
memset((void *)pGlyphs, 0x00, m_nGlyphsCount * sizeof(unsigned char));
for (auto oIt : m_mGlyphs)
{
pGlyphs[oIt.first] = 1;
}
// ,
std::string sFontName = m_pDocument->GetTTFontTag() + std::string(pFace->family_name);
if (pFace->style_flags & FT_STYLE_FLAG_ITALIC)
sFontName += "-Italic";
if (pFace->style_flags & FT_STYLE_FLAG_BOLD)
sFontName += "-Bold";
const char* sName = sFontName.c_str();
Add("BaseFont", sName);
m_pFont->Add("BaseFont", sName);
m_pFontDescriptor->Add("FontName", sName);
FT_Done_Face(pFace);
FT_Done_FreeType(pLibrary);
delete[] pMemory;
*ppCodeToGid = pCodeToGID;
*ppWidths = pWidths;
*ppGlyphs = pUseGlyf;
unGlyphsCount = lGlyfsCount;
*ppGlyphs = pGlyphs;
unGlyphsCount = m_nGlyphsCount;
return true;
}
void CFontCidTrueType::WriteToUnicode()
{
......@@ -432,4 +373,47 @@ namespace PdfWriter
pS->WriteStr("endbfchar\n");
m_pToUnicodeStream->WriteStr(c_sToUnicodeFooter);
}
void CFontCidTrueType::CloseFontFace()
{
if (m_pFace)
{
FT_Done_Face(m_pFace);
m_pFace = NULL;
}
RELEASEARRAYOBJECTS(m_pFaceMemory);
}
bool CFontCidTrueType::OpenFontFace()
{
if (m_pFace)
{
m_pDocument->AddFreeTypeFont(this);
return true;
}
m_nGlyphsCount = 0;
m_nSymbolicCmap = -1;
FT_Library pLibrary = m_pDocument->GetFreeTypeLibrary();
if (!pLibrary)
return false;
DWORD dwFileSize;
m_pFaceMemory = NULL;
NSFile::CFileBinary::ReadAllBytes(m_wsFontPath, &m_pFaceMemory, dwFileSize);
if (!m_pFaceMemory)
return false;
FT_New_Memory_Face(pLibrary, m_pFaceMemory, dwFileSize, m_unFontIndex, &m_pFace);
if (!m_pFace)
{
RELEASEARRAYOBJECTS(m_pFaceMemory);
return false;
}
m_pDocument->AddFreeTypeFont(this);
m_nGlyphsCount = m_pFace->num_glyphs;
m_nSymbolicCmap = GetSymbolicCmapIndex(m_pFace);
return true;
}
}
......@@ -33,13 +33,14 @@ namespace PdfWriter
return fontCIDType2;
}
private:
void BeforeWrite();
void GetWidthsAndGids(unsigned short** ppCodeToGid, unsigned int** pWidths, unsigned char** ppGlyphs, unsigned int& unGlyphsCount);
bool GetWidthsAndGids(unsigned short** ppCodeToGid, unsigned int** pWidths, unsigned char** ppGlyphs, unsigned int& unGlyphsCount);
void CreateCIDFont2(CDictObject* pFont);
void WriteToUnicode();
bool OpenFontFace();
void CloseFontFace();
private:
......@@ -61,9 +62,12 @@ namespace PdfWriter
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;
int m_nGlyphsCount;
int m_nSymbolicCmap;
friend class CDocument;
};
}
......
......@@ -231,6 +231,8 @@ namespace PdfWriter
else
{
double* pValues = new double[(nCount - 1) * 2];
if (pValues)
{
for (int nIndex = 0; nIndex < nCount - 1; nIndex++)
{
pValues[2 * nIndex + 0] = pColors[nIndex] / 255.0;
......@@ -242,6 +244,7 @@ namespace PdfWriter
delete[] pValues;
}
}
}
void CShading::SetExtend(bool bBegin, bool bEnd)
{
CArrayObject* pExtend = new CArrayObject();
......
......@@ -587,7 +587,7 @@ namespace PdfWriter
unsigned int unRemainBytes = m_nBufferSize - Tell();
if (unRemainBytes < unSize)
{
unsigned int unShrinkSize = max(unSize, 4096);
unsigned int unShrinkSize = max(unSize, STREAM_BUF_SIZ);
Shrink(unShrinkSize);
}
MemCpy(m_pCur, pBuffer, unSize);
......
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