fontTableWriter.h 3.74 KB
Newer Older
1 2 3
#ifndef FONT_TABLE_WRITER
#define FONT_TABLE_WRITER

Alexey.Golubev's avatar
Alexey.Golubev committed
4 5 6 7 8 9 10 11 12
#include "Common.h"

namespace Writers
{
	static CString g_string_ft_Start = _T("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><w:fonts xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\" mc:Ignorable=\"w14\">");
	static CString g_string_ft_End = _T("</w:fonts>");

	class FontTableWriter
	{
13
		XmlUtils::CStringWriter	m_oWriter;
Alexey.Golubev's avatar
Alexey.Golubev committed
14
		CString	m_sDir;
15
		ASCGraphics::IASCFontManager* m_pFontManager;
Alexey.Golubev's avatar
Alexey.Golubev committed
16 17 18 19 20 21 22 23
	public:
		CAtlMap<CString, int> m_mapFonts;
	public:
		FontTableWriter(CString sDir, CString sFontDir):m_sDir(sDir)
		{
			m_pFontManager = NULL;
			if(!sFontDir.IsEmpty())
			{
24
				CoCreateInstance(ASCGraphics::CLSID_CASCFontManager, NULL, CLSCTX_ALL, __uuidof(ASCGraphics::IASCFontManager), (void**)&m_pFontManager);
Alexey.Golubev's avatar
Alexey.Golubev committed
25 26 27 28 29 30 31 32
				if(NULL != m_pFontManager)
				{
					VARIANT var;
					var.vt = VT_BSTR;
					var.bstrVal = sFontDir.AllocSysString();
					m_pFontManager->SetAdditionalParam(L"InitializeFromFolder", var);
					RELEASESYSSTRING(var.bstrVal);

33
#ifdef BUILD_CONFIG_FULL_VERSION
Alexey.Golubev's avatar
Alexey.Golubev committed
34 35 36 37
					CString defaultFontName = _T("Arial");
					BSTR defFontName = defaultFontName.AllocSysString();
					m_pFontManager->SetDefaultFont(defFontName);
					SysFreeString(defFontName);
38
#endif
Alexey.Golubev's avatar
Alexey.Golubev committed
39 40 41
				}
			}
		}
42 43 44 45 46
		~FontTableWriter()
		{
			RELEASEINTERFACE(m_pFontManager);
		}

Alexey.Golubev's avatar
Alexey.Golubev committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
		void Write()
		{
			m_oWriter.WriteString(g_string_ft_Start);

			//      FontTable
			bool bCalibri = false;
			bool bTimes = false;
			bool bCambria = false;
			POSITION pos = m_mapFonts.GetStartPosition();
			while(NULL != pos)
			{
				CAtlMap<CString, int>::CPair* pair = m_mapFonts.GetNext(pos);
				if(NULL != pair)
				{
					const CString& sFontName = pair->m_key;
					if(_T("Calibri") == sFontName)
						bCalibri = true;
					else if(_T("Times New Roman") == sFontName)
						bTimes = true;
					else if(_T("Cambria") == sFontName)
						bCambria = true;
					WriteFont(sFontName);
				}
			}
			if(false == bCalibri)
				WriteFont(_T("Calibri"));
			if(false == bTimes)
				WriteFont(_T("Times New Roman"));
			if(false == bCambria)
				WriteFont(_T("Cambria"));

			m_oWriter.WriteString(g_string_ft_End);

			CFile oFile;
			oFile.CreateFile(m_sDir + _T("\\word\\fontTable.xml"));
			oFile.WriteStringUTF8(m_oWriter.GetData());
			oFile.CloseFile();
		}
		void WriteFont(CString sFontName)
		{
			CString sPanose;
			if(NULL != m_pFontManager)
			{
				long index = 0;
				BSTR bstrFontName = sFontName.AllocSysString();
				SAFEARRAY *psaArray = NULL;
93 94 95 96
#ifdef BUILD_CONFIG_OPENSOURCE_VERSION
				m_pFontManager->GetParamsByFontName(bstrFontName, &psaArray, NULL);
#else
				m_pFontManager->LoadFontByName(bstrFontName, 12, 0, 72, 72);
Alexey.Golubev's avatar
Alexey.Golubev committed
97
				m_pFontManager->GetPanose(&psaArray);
98 99
#endif
				SysFreeString(bstrFontName);
Alexey.Golubev's avatar
Alexey.Golubev committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
				if(NULL != psaArray)
				{
					unsigned char* pData = static_cast<unsigned char*>(psaArray->pvData);
					for(int i = 0; i < psaArray->rgsabound->cElements; ++i)
					{
						unsigned char cElem = pData[i];
						if(cElem > 0xF)
							sPanose.AppendFormat(_T("%X"), cElem);
						else
							sPanose.AppendFormat(_T("0%X"), cElem);
					}
				}
				RELEASEARRAY(psaArray);
			}

			CorrectString(sFontName);
			m_oWriter.WriteString(_T("<w:font w:name=\"") + sFontName + _T("\">"));
			if(!sPanose.IsEmpty())
				m_oWriter.WriteString(_T("<w:panose1 w:val=\"")+sPanose+_T("\"/>"));
			m_oWriter.WriteString(CString(_T("</w:font>")));
		}
	};
122 123
}
#endif	// #ifndef FONT_TABLE_WRITER