mirror of
https://github.com/ONLYOFFICE/core.git
synced 2026-07-11 06:22:44 +08:00
Co-authored-by: Alexander Trofimov <alexander.trofimov@onlyoffice.com> Co-committed-by: Alexander Trofimov <alexander.trofimov@onlyoffice.com>
215 lines
6.6 KiB
C++
215 lines
6.6 KiB
C++
/*
|
|
* Copyright (C) Ascensio System SIA, 2009-2026
|
|
*
|
|
* This program is a free software product. You can redistribute it and/or
|
|
* modify it under the terms of the GNU Affero General Public License (AGPL)
|
|
* version 3 as published by the Free Software Foundation, together with the
|
|
* additional terms provided in the LICENSE file.
|
|
*
|
|
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
|
|
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
|
|
* details, see the GNU AGPL at: https://www.gnu.org/licenses/agpl-3.0.html
|
|
*
|
|
* You can contact Ascensio System SIA by email at info@onlyoffice.com
|
|
* or by postal mail at 20A-6 Ernesta Birznieka-Upisha Street, Riga,
|
|
* LV-1050, Latvia, European Union.
|
|
*
|
|
* The interactive user interfaces in modified versions of the Program
|
|
* are required to display Appropriate Legal Notices in accordance with
|
|
* Section 5 of the GNU AGPL version 3.
|
|
*
|
|
* No trademark rights are granted under this License.
|
|
*
|
|
* All non-code elements of the Product, including illustrations,
|
|
* icon sets, and technical writing content, are licensed under the
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License:
|
|
* https://creativecommons.org/licenses/by-sa/4.0/legalcode
|
|
*
|
|
* This license applies only to such non-code elements and does not
|
|
* modify or replace the licensing terms applicable to the Program's
|
|
* source code, which remains licensed under the GNU Affero General
|
|
* Public License v3.
|
|
*
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
#include "FontCutter.h"
|
|
|
|
namespace NSFontCutter
|
|
{
|
|
CEmbeddedFontsManager::CEmbeddedFontInfo::CEmbeddedFontInfo()
|
|
{
|
|
Name = _T("");
|
|
|
|
PathRegular = _T("");
|
|
FaceRegular = -1;
|
|
|
|
PathBold = _T("");
|
|
FaceBold = -1;
|
|
|
|
PathItalic = _T("");
|
|
FaceItalic = -1;
|
|
|
|
PathBoldItalic = _T("");
|
|
FaceBoldItalic = -1;
|
|
}
|
|
CEmbeddedFontsManager::CEmbeddedFontInfo::CEmbeddedFontInfo(const CEmbeddedFontsManager::CEmbeddedFontInfo& oSrc)
|
|
{
|
|
*this = oSrc;
|
|
}
|
|
CEmbeddedFontsManager::CEmbeddedFontInfo& CEmbeddedFontsManager::CEmbeddedFontInfo::operator=(const CEmbeddedFontsManager::CEmbeddedFontInfo& oSrc)
|
|
{
|
|
Name = oSrc.Name;
|
|
|
|
PathRegular = oSrc.PathRegular;
|
|
FaceRegular = oSrc.FaceRegular;
|
|
|
|
PathBold = oSrc.PathBold;
|
|
FaceBold = oSrc.FaceBold;
|
|
|
|
PathItalic = oSrc.PathItalic;
|
|
FaceItalic = oSrc.FaceItalic;
|
|
|
|
PathBoldItalic = oSrc.PathBoldItalic;
|
|
FaceBoldItalic = oSrc.FaceBoldItalic;
|
|
|
|
return *this;
|
|
}
|
|
|
|
CEmbeddedFontsManager::CEmbeddedFontsManager()
|
|
{
|
|
m_strEmbeddedFontsFolder = _T("");
|
|
}
|
|
void CEmbeddedFontsManager::CheckFont(const std::wstring& strName, NSFonts::IFontManager* pManager)
|
|
{
|
|
std::map<std::wstring, CEmbeddedFontInfo>::const_iterator pPair = m_mapFontsEmbeddded.find(strName);
|
|
if (pPair != m_mapFontsEmbeddded.end())
|
|
return;
|
|
|
|
CEmbeddedFontInfo oInfo;
|
|
oInfo.Name = strName;
|
|
|
|
std::vector<NSFonts::CFontInfo*> aFontInfos = pManager->GetAllStylesByFontName(strName);
|
|
for(std::vector<NSFonts::CFontInfo*>::iterator i = aFontInfos.begin(); i != aFontInfos.end(); i++)
|
|
{
|
|
NSFonts::CFontInfo* pFontInfo = *i;
|
|
if(0 != pFontInfo->m_bBold && 0 != pFontInfo->m_bItalic)
|
|
{
|
|
oInfo.PathBoldItalic = std::wstring(pFontInfo->m_wsFontPath.c_str());
|
|
oInfo.FaceBoldItalic = pFontInfo->m_lIndex;
|
|
}
|
|
else if(0 != pFontInfo->m_bBold)
|
|
{
|
|
oInfo.PathBold = std::wstring(pFontInfo->m_wsFontPath.c_str());
|
|
oInfo.FaceBold = pFontInfo->m_lIndex;
|
|
}
|
|
else if(0 != pFontInfo->m_bItalic)
|
|
{
|
|
oInfo.PathItalic = std::wstring(pFontInfo->m_wsFontPath.c_str());
|
|
oInfo.FaceItalic = pFontInfo->m_lIndex;
|
|
}
|
|
else
|
|
{
|
|
oInfo.PathRegular = std::wstring(pFontInfo->m_wsFontPath.c_str());
|
|
oInfo.FaceRegular = pFontInfo->m_lIndex;
|
|
}
|
|
}
|
|
|
|
m_mapFontsEmbeddded [strName] = oInfo;
|
|
}
|
|
void CEmbeddedFontsManager::CheckString(const nullable_string& val)
|
|
{
|
|
if (val.is_init())
|
|
CheckString(*val);
|
|
}
|
|
void CEmbeddedFontsManager::CheckString(const std::wstring& val)
|
|
{
|
|
size_t len = val.length();
|
|
|
|
std::wstring str_lower = XmlUtils::GetLower(val);
|
|
std::wstring str_upper = XmlUtils::GetUpper(val);
|
|
|
|
for (size_t i = 0; i < len; ++i)
|
|
{
|
|
m_CharMap [str_lower[i]] = true;
|
|
m_CharMap [str_upper[i]] = true;
|
|
}
|
|
}
|
|
bool CEmbeddedFontsManager::GenerateSafearray(USHORT **ppArray, size_t& nCount)
|
|
{
|
|
// digits needed for all. rest - each one
|
|
CheckString(_T("0123456789"));
|
|
m_CharMap [(WCHAR)0x00B0] = true;
|
|
m_CharMap [(WCHAR)0x00B7] = true;
|
|
m_CharMap [(WCHAR)0x00B6] = true;
|
|
m_CharMap [(WCHAR)0x00A4] = true;
|
|
m_CharMap [(WCHAR)0x00A0] = true;
|
|
m_CharMap [(WCHAR)0x0022] = true;
|
|
m_CharMap [(WCHAR)0x0032] = true;
|
|
m_CharMap [(WCHAR)0x0038] = true;
|
|
m_CharMap [(WCHAR)0x0097] = true;
|
|
|
|
nCount = (ULONG)m_CharMap.size();
|
|
|
|
USHORT *pArray = new USHORT [nCount];
|
|
|
|
if (NULL == pArray)
|
|
return false;
|
|
|
|
USHORT *pBuffer = pArray;
|
|
|
|
for (std::map<WCHAR, bool>::const_iterator pPair = m_CharMap.begin(); pPair != m_CharMap.end(); ++pPair)
|
|
{
|
|
*pBuffer = pPair->first;
|
|
++pBuffer;
|
|
}
|
|
*ppArray = pArray;
|
|
return true;
|
|
}
|
|
|
|
#if !defined(DONT_WRITE_EMBEDDED_FONTS)
|
|
void CEmbeddedFontsManager::WriteFont(std::wstring& strName, LONG& lFaceIndex, std::wstring& strFontPath, CFile* pFile, USHORT* pArrayUnicodes, size_t pArrayUnicodesLength, Fonts::IFontConverter* pFontConverter)
|
|
{
|
|
LONG lFontConverterFlag = 16; // truetype only
|
|
|
|
BSTR bsFontIn = strFontPath.AllocSysString();
|
|
|
|
std::wstring _strName = strName + _T("_Embedded");
|
|
//BSTR bsName = _strName.AllocSysString();
|
|
|
|
SAFEARRAYBOUND rgsab_ArrayUnicodes;
|
|
rgsab_ArrayUnicodes.lLbound = 0;
|
|
rgsab_ArrayUnicodes.cElements = pArrayUnicodesLength;
|
|
|
|
SAFEARRAY* saArrayUnicodes = SafeArrayCreate(VT_UI1, 1, &rgsab_ArrayUnicodes);
|
|
memcpy(saArrayUnicodes->pvData, pArrayUnicodes, pArrayUnicodesLength);
|
|
|
|
SAFEARRAY* pArrayData = NULL;
|
|
|
|
pFontConverter->ToOTF2(bsFontIn, saArrayUnicodes, _strName.AllocSysString(), lFontConverterFlag, lFaceIndex, &pArrayData); // TrueType only
|
|
|
|
//SysFreeString(bsFontIn);
|
|
//SysFreeString(bsName);
|
|
|
|
BYTE* pbBinBuffer = (BYTE*)pArrayData->pvData;
|
|
int nBinBufferLen = pArrayData->rgsabound[0].cElements;
|
|
int nBase64BufferLen = Base64::Base64EncodeGetRequiredLength(nBinBufferLen, Base64::B64_BASE64_FLAG_NOCRLF);
|
|
BYTE* pbBase64Buffer = new BYTE[nBase64BufferLen];
|
|
if (true == Base64::Base64Encode(pbBinBuffer, nBinBufferLen, (LPSTR)pbBase64Buffer, &nBase64BufferLen, Base64::B64_BASE64_FLAG_NOCRLF))
|
|
{
|
|
std::string s = "\"";
|
|
pFile->WriteFile(s.GetBuffer(), s.length());
|
|
|
|
pFile->WriteFile(pbBase64Buffer, nBase64BufferLen);
|
|
|
|
pFile->WriteFile(s.GetBuffer(), s.length());
|
|
}
|
|
|
|
RELEASEARRAYOBJECTS(pbBase64Buffer);
|
|
|
|
RELEASEARRAY(pArrayData);
|
|
}
|
|
#endif // #if !defined(DONT_WRITE_EMBEDDED_FONTS)
|
|
}
|
|
|