1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #include "../../../include/fpdfapi/fpdf_page.h"
8 #include "../../../include/fpdfapi/fpdf_module.h"
9 #include "../../../include/fpdfapi/fpdf_pageobj.h"
11 #include "../fpdf_page/pageint.h"
12 #include "../../../include/fxge/fx_freetype.h"
13 FX_BOOL FT_UseTTCharmap(FXFT_Face face, int platform_id, int encoding_id)
15 for (int i = 0; i < FXFT_Get_Face_CharmapCount(face); i ++) {
16 if (FXFT_Get_Charmap_PlatformID(FXFT_Get_Face_Charmaps(face)[i]) == platform_id &&
17 FXFT_Get_Charmap_EncodingID(FXFT_Get_Face_Charmaps(face)[i]) == encoding_id) {
18 FXFT_Set_Charmap(face, FXFT_Get_Face_Charmaps(face)[i]);
24 extern const FX_WORD* PDF_UnicodesForPredefinedCharSet(int);
25 CPDF_FontGlobals::CPDF_FontGlobals()
27 FXSYS_memset32(m_EmbeddedCharsets, 0, sizeof m_EmbeddedCharsets);
28 FXSYS_memset32(m_EmbeddedToUnicodes, 0, sizeof m_EmbeddedToUnicodes);
29 m_pContrastRamps = NULL;
31 CPDF_FontGlobals::~CPDF_FontGlobals()
34 if (m_pContrastRamps) {
35 FX_Free(m_pContrastRamps);
38 class CFX_StockFontArray : public CFX_Object
43 FXSYS_memset32(m_pStockFonts, 0, sizeof(CPDF_Font*) * 14);
45 CPDF_Font* m_pStockFonts[14];
47 CPDF_Font* CPDF_FontGlobals::Find(void* key, int index)
50 if (!m_pStockMap.Lookup(key, value)) {
56 return ((CFX_StockFontArray*)value)->m_pStockFonts[index];
58 void CPDF_FontGlobals::Set(void* key, int index, CPDF_Font* pFont)
61 if (m_pStockMap.Lookup(key, value)) {
62 ((CFX_StockFontArray*)value)->m_pStockFonts[index] = pFont;
65 CFX_StockFontArray* pFonts = FX_NEW CFX_StockFontArray();
67 pFonts->m_pStockFonts[index] = pFont;
69 m_pStockMap.SetAt(key, pFonts);
71 void CPDF_FontGlobals::Clear(void* key)
74 if (!m_pStockMap.Lookup(key, value)) {
78 CFX_StockFontArray* pStockFonts = (CFX_StockFontArray*)value;
79 for (int i = 0; i < 14; i ++) {
80 if (pStockFonts->m_pStockFonts[i]) {
81 pStockFonts->m_pStockFonts[i]->GetFontDict()->Release();
82 delete pStockFonts->m_pStockFonts[i];
87 m_pStockMap.RemoveKey(key);
89 void CPDF_FontGlobals::ClearAll()
91 FX_POSITION pos = m_pStockMap.GetStartPosition();
95 m_pStockMap.GetNextAssoc(pos, key, value);
97 CFX_StockFontArray* pStockFonts = (CFX_StockFontArray*)value;
98 for (int i = 0; i < 14; i ++) {
99 if (pStockFonts->m_pStockFonts[i]) {
100 pStockFonts->m_pStockFonts[i]->GetFontDict()->Release();
101 delete pStockFonts->m_pStockFonts[i];
106 m_pStockMap.RemoveKey(key);
109 CPDF_Font::CPDF_Font()
112 m_FontBBox.left = m_FontBBox.right = m_FontBBox.top = m_FontBBox.bottom = 0;
113 m_StemV = m_Ascent = m_Descent = m_ItalicAngle = 0;
116 m_pToUnicodeMap = NULL;
117 m_bToUnicodeLoaded = FALSE;
120 FX_BOOL CPDF_Font::Initialize()
122 m_pCharMap = FX_NEW CPDF_FontCharMap(this);
125 CPDF_Font::~CPDF_Font()
131 if (m_pToUnicodeMap) {
132 delete m_pToUnicodeMap;
133 m_pToUnicodeMap = NULL;
136 m_pDocument->GetPageData()->ReleaseFontFileStreamAcc((CPDF_Stream*)m_pFontFile->GetStream());
139 FX_BOOL CPDF_Font::IsVertWriting() const
141 FX_BOOL bVertWriting = FALSE;
142 CPDF_CIDFont* pCIDFont = GetCIDFont();
144 bVertWriting = pCIDFont->IsVertWriting();
146 bVertWriting = m_Font.IsVertical();
150 CFX_ByteString CPDF_Font::GetFontTypeName() const
152 switch (m_FontType) {
154 return FX_BSTRC("Type1");
155 case PDFFONT_TRUETYPE:
156 return FX_BSTRC("TrueType");
158 return FX_BSTRC("Type3");
159 case PDFFONT_CIDFONT:
160 return FX_BSTRC("Type0");
162 return CFX_ByteString();
164 void CPDF_Font::AppendChar(CFX_ByteString& str, FX_DWORD charcode) const
167 int len = AppendChar(buf, charcode);
171 str += CFX_ByteString(buf, len);
174 CFX_WideString CPDF_Font::UnicodeFromCharCode(FX_DWORD charcode) const
176 if (!m_bToUnicodeLoaded) {
177 ((CPDF_Font*)this)->LoadUnicodeMap();
179 if (m_pToUnicodeMap) {
180 CFX_WideString wsRet = m_pToUnicodeMap->Lookup(charcode);
181 if (!wsRet.IsEmpty()) {
185 FX_WCHAR unicode = _UnicodeFromCharCode(charcode);
187 return CFX_WideString();
191 FX_DWORD CPDF_Font::CharCodeFromUnicode(FX_WCHAR unicode) const
193 if (!m_bToUnicodeLoaded) {
194 ((CPDF_Font*)this)->LoadUnicodeMap();
196 if (m_pToUnicodeMap) {
197 FX_DWORD charcode = m_pToUnicodeMap->ReverseLookup(unicode);
202 return _CharCodeFromUnicode(unicode);
204 CFX_WideString CPDF_Font::DecodeString(const CFX_ByteString& str) const
206 CFX_WideString result;
207 int src_len = str.GetLength();
208 result.Reserve(src_len);
209 FX_LPCSTR src_buf = str;
211 while (src_pos < src_len) {
212 FX_DWORD charcode = GetNextChar(src_buf, src_pos);
213 CFX_WideString unicode = UnicodeFromCharCode(charcode);
214 if (!unicode.IsEmpty()) {
217 result += (FX_WCHAR)charcode;
222 CFX_ByteString CPDF_Font::EncodeString(const CFX_WideString& str) const
224 CFX_ByteString result;
225 int src_len = str.GetLength();
226 FX_LPSTR dest_buf = result.GetBuffer(src_len * 2);
227 FX_LPCWSTR src_buf = str;
229 for (int src_pos = 0; src_pos < src_len; src_pos ++) {
230 FX_DWORD charcode = CharCodeFromUnicode(src_buf[src_pos]);
231 dest_pos += AppendChar(dest_buf + dest_pos, charcode);
233 result.ReleaseBuffer(dest_pos);
236 void CPDF_Font::LoadFontDescriptor(CPDF_Dictionary* pFontDesc)
238 m_Flags = pFontDesc->GetInteger(FX_BSTRC("Flags"), PDFFONT_NONSYMBOLIC);
240 FX_BOOL bExistItalicAngle = FALSE;
241 if (pFontDesc->KeyExist(FX_BSTRC("ItalicAngle"))) {
242 ItalicAngle = pFontDesc->GetInteger(FX_BSTRC("ItalicAngle"));
243 bExistItalicAngle = TRUE;
245 if (ItalicAngle < 0) {
246 m_Flags |= PDFFONT_ITALIC;
247 m_ItalicAngle = ItalicAngle;
249 FX_BOOL bExistStemV = FALSE;
250 if (pFontDesc->KeyExist(FX_BSTRC("StemV"))) {
251 m_StemV = pFontDesc->GetInteger(FX_BSTRC("StemV"));
254 FX_BOOL bExistAscent = FALSE;
255 if (pFontDesc->KeyExist(FX_BSTRC("Ascent"))) {
256 m_Ascent = pFontDesc->GetInteger(FX_BSTRC("Ascent"));
259 FX_BOOL bExistDescent = FALSE;
260 if (pFontDesc->KeyExist(FX_BSTRC("Descent"))) {
261 m_Descent = pFontDesc->GetInteger(FX_BSTRC("Descent"));
262 bExistDescent = TRUE;
264 FX_BOOL bExistCapHeight = FALSE;
265 if (pFontDesc->KeyExist(FX_BSTRC("CapHeight"))) {
266 bExistCapHeight = TRUE;
268 if (bExistItalicAngle && bExistAscent && bExistCapHeight && bExistDescent && bExistStemV) {
269 m_Flags |= PDFFONT_USEEXTERNATTR;
271 if (m_Descent > 10) {
272 m_Descent = -m_Descent;
274 CPDF_Array* pBBox = pFontDesc->GetArray(FX_BSTRC("FontBBox"));
276 m_FontBBox.left = pBBox->GetInteger(0);
277 m_FontBBox.bottom = pBBox->GetInteger(1);
278 m_FontBBox.right = pBBox->GetInteger(2);
279 m_FontBBox.top = pBBox->GetInteger(3);
281 CPDF_Stream* pFontFile = pFontDesc->GetStream(FX_BSTRC("FontFile"));
282 if (pFontFile == NULL) {
283 pFontFile = pFontDesc->GetStream(FX_BSTRC("FontFile2"));
285 if (pFontFile == NULL) {
286 pFontFile = pFontDesc->GetStream(FX_BSTRC("FontFile3"));
289 m_pFontFile = m_pDocument->LoadFontFile(pFontFile);
290 if (m_pFontFile == NULL) {
293 FX_LPCBYTE pFontData = m_pFontFile->GetData();
294 FX_DWORD dwFontSize = m_pFontFile->GetSize();
295 m_Font.LoadEmbedded(pFontData, dwFontSize);
296 if (m_Font.m_Face == NULL) {
301 short TT2PDF(int m, FXFT_Face face)
303 int upm = FXFT_Get_Face_UnitsPerEM(face);
307 return (m * 1000 + upm / 2) / upm;
309 void CPDF_Font::CheckFontMetrics()
311 if (m_FontBBox.top == 0 && m_FontBBox.bottom == 0 && m_FontBBox.left == 0 && m_FontBBox.right == 0) {
313 m_FontBBox.left = TT2PDF(FXFT_Get_Face_xMin(m_Font.m_Face), m_Font.m_Face);
314 m_FontBBox.bottom = TT2PDF(FXFT_Get_Face_yMin(m_Font.m_Face), m_Font.m_Face);
315 m_FontBBox.right = TT2PDF(FXFT_Get_Face_xMax(m_Font.m_Face), m_Font.m_Face);
316 m_FontBBox.top = TT2PDF(FXFT_Get_Face_yMax(m_Font.m_Face), m_Font.m_Face);
317 m_Ascent = TT2PDF(FXFT_Get_Face_Ascender(m_Font.m_Face), m_Font.m_Face);
318 m_Descent = TT2PDF(FXFT_Get_Face_Descender(m_Font.m_Face), m_Font.m_Face);
320 FX_BOOL bFirst = TRUE;
321 for (int i = 0; i < 256; i ++) {
323 GetCharBBox(i, rect);
324 if (rect.left == rect.right) {
331 if (m_FontBBox.top < rect.top) {
332 m_FontBBox.top = rect.top;
334 if (m_FontBBox.right < rect.right) {
335 m_FontBBox.right = rect.right;
337 if (m_FontBBox.left > rect.left) {
338 m_FontBBox.left = rect.left;
340 if (m_FontBBox.bottom > rect.bottom) {
341 m_FontBBox.bottom = rect.bottom;
347 if (m_Ascent == 0 && m_Descent == 0) {
349 GetCharBBox('A', rect);
350 if (rect.bottom == rect.top) {
351 m_Ascent = m_FontBBox.top;
355 GetCharBBox('g', rect);
356 if (rect.bottom == rect.top) {
357 m_Descent = m_FontBBox.bottom;
359 m_Descent = rect.bottom;
363 void CPDF_Font::LoadUnicodeMap()
365 m_bToUnicodeLoaded = TRUE;
366 CPDF_Stream* pStream = m_pFontDict->GetStream(FX_BSTRC("ToUnicode"));
367 if (pStream == NULL) {
370 m_pToUnicodeMap = FX_NEW CPDF_ToUnicodeMap;
371 m_pToUnicodeMap->Load(pStream);
373 int CPDF_Font::GetStringWidth(FX_LPCSTR pString, int size)
377 while (offset < size) {
378 FX_DWORD charcode = GetNextChar(pString, offset);
379 width += GetCharWidthF(charcode);
383 int CPDF_Font::GetCharTypeWidth(FX_DWORD charcode)
385 if (m_Font.m_Face == NULL) {
388 int glyph_index = GlyphFromCharCode(charcode);
389 if (glyph_index == 0xffff) {
392 return m_Font.GetGlyphWidth(glyph_index);
394 int _PDF_GetStandardFontName(CFX_ByteString& name);
395 CPDF_Font* CPDF_Font::GetStockFont(CPDF_Document* pDoc, FX_BSTR name)
397 CFX_ByteString fontname(name);
398 int font_id = _PDF_GetStandardFontName(fontname);
402 CPDF_FontGlobals* pFontGlobals = CPDF_ModuleMgr::Get()->GetPageModule()->GetFontGlobals();
403 CPDF_Font* pFont = pFontGlobals->Find(pDoc, font_id);
407 CPDF_Dictionary* pDict = CPDF_Dictionary::Create();
408 pDict->SetAtName(FX_BSTRC("Type"), FX_BSTRC("Font"));
409 pDict->SetAtName(FX_BSTRC("Subtype"), FX_BSTRC("Type1"));
410 pDict->SetAtName(FX_BSTRC("BaseFont"), fontname);
411 pDict->SetAtName(FX_BSTRC("Encoding"), FX_BSTRC("WinAnsiEncoding"));
412 pFont = CPDF_Font::CreateFontF(NULL, pDict);
413 pFontGlobals->Set(pDoc, font_id, pFont);
416 const FX_BYTE ChineseFontNames[][5] = {
417 {0xCB, 0xCE, 0xCC, 0xE5, 0x00},
418 {0xBF, 0xAC, 0xCC, 0xE5, 0x00},
419 {0xBA, 0xDA, 0xCC, 0xE5, 0x00},
420 {0xB7, 0xC2, 0xCB, 0xCE, 0x00},
421 {0xD0, 0xC2, 0xCB, 0xCE, 0x00}
423 CPDF_Font* CPDF_Font::CreateFontF(CPDF_Document* pDoc, CPDF_Dictionary* pFontDict)
425 CFX_ByteString type = pFontDict->GetString(FX_BSTRC("Subtype"));
427 if (type == FX_BSTRC("TrueType")) {
429 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ || _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ || _FXM_PLATFORM_ == _FXM_PLATFORM_ANDROID_ || _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
430 CFX_ByteString basefont = pFontDict->GetString(FX_BSTRC("BaseFont"));
431 CFX_ByteString tag = basefont.Left(4);
433 int count = sizeof(ChineseFontNames) / sizeof(ChineseFontNames[0]);
434 for (i = 0; i < count; ++i) {
435 if (tag == CFX_ByteString((FX_LPCSTR)ChineseFontNames[i])) {
440 CPDF_Dictionary* pFontDesc = pFontDict->GetDict(FX_BSTRC("FontDescriptor"));
441 if (pFontDesc == NULL || !pFontDesc->KeyExist(FX_BSTRC("FontFile2"))) {
442 pFont = FX_NEW CPDF_CIDFont;
444 pFont->m_FontType = PDFFONT_CIDFONT;
445 pFont->m_pFontDict = pFontDict;
446 pFont->m_pDocument = pDoc;
447 if (!pFont->Load()) {
456 pFont = FX_NEW CPDF_TrueTypeFont;
458 pFont->m_FontType = PDFFONT_TRUETYPE;
459 } else if (type == FX_BSTRC("Type3")) {
460 pFont = FX_NEW CPDF_Type3Font;
462 pFont->m_FontType = PDFFONT_TYPE3;
463 } else if (type == FX_BSTRC("Type0")) {
464 pFont = FX_NEW CPDF_CIDFont;
466 pFont->m_FontType = PDFFONT_CIDFONT;
468 pFont = FX_NEW CPDF_Type1Font;
470 pFont->m_FontType = PDFFONT_TYPE1;
472 pFont->m_pFontDict = pFontDict;
473 pFont->m_pDocument = pDoc;
474 if (!pFont->Load()) {
480 FX_BOOL CPDF_Font::Load()
482 if (m_pFontDict == NULL) {
485 CFX_ByteString type = m_pFontDict->GetString(FX_BSTRC("Subtype"));
486 m_BaseFont = m_pFontDict->GetString(FX_BSTRC("BaseFont"));
487 if (type == FX_BSTRC("MMType1")) {
488 type = FX_BSTRC("Type1");
492 static CFX_WideString _FontMap_GetWideString(CFX_CharMap* pMap, const CFX_ByteString& bytestr)
494 return ((CPDF_FontCharMap*)pMap)->m_pFont->DecodeString(bytestr);
496 static CFX_ByteString _FontMap_GetByteString(CFX_CharMap* pMap, const CFX_WideString& widestr)
498 return ((CPDF_FontCharMap*)pMap)->m_pFont->EncodeString(widestr);
500 CPDF_FontCharMap::CPDF_FontCharMap(CPDF_Font* pFont)
502 m_GetByteString = _FontMap_GetByteString;
503 m_GetWideString = _FontMap_GetWideString;
506 CFX_WideString CPDF_ToUnicodeMap::Lookup(FX_DWORD charcode)
509 if (m_Map.Lookup(charcode, value)) {
510 FX_WCHAR unicode = (FX_WCHAR)(value & 0xffff);
511 if (unicode != 0xffff) {
514 FX_LPCWSTR buf = m_MultiCharBuf.GetBuffer();
515 FX_DWORD buf_len = m_MultiCharBuf.GetLength();
516 if (buf == NULL || buf_len == 0) {
517 return CFX_WideString();
519 FX_DWORD index = value >> 16;
520 if (index >= buf_len) {
521 return CFX_WideString();
523 FX_DWORD len = buf[index];
524 if (index + len < index || index + len >= buf_len) {
525 return CFX_WideString();
527 return CFX_WideString(buf + index + 1, len);
530 return m_pBaseMap->UnicodeFromCID((FX_WORD)charcode);
532 return CFX_WideString();
534 FX_DWORD CPDF_ToUnicodeMap::ReverseLookup(FX_WCHAR unicode)
536 FX_POSITION pos = m_Map.GetStartPosition();
539 m_Map.GetNextAssoc(pos, key, value);
540 if ((FX_WCHAR)value == unicode) {
546 static FX_DWORD _StringToCode(FX_BSTR str)
548 FX_LPCSTR buf = str.GetCStr();
549 int len = str.GetLength();
555 for (int i = 1; i < len; i ++) {
557 if (buf[i] >= '0' && buf[i] <= '9') {
558 digit = buf[i] - '0';
559 } else if (buf[i] >= 'a' && buf[i] <= 'f') {
560 digit = buf[i] - 'a' + 10;
561 } else if (buf[i] >= 'A' && buf[i] <= 'F') {
562 digit = buf[i] - 'A' + 10;
566 result = result * 16 + digit;
570 for (int i = 0; i < len; i ++) {
571 if (buf[i] < '0' || buf[i] > '9') {
574 result = result * 10 + buf[i] - '0';
579 static CFX_WideString _StringDataAdd(CFX_WideString str)
582 int len = str.GetLength();
584 for (int i = len - 1; i >= 0 ; --i) {
585 FX_WCHAR ch = str[i] + value;
594 ret.Insert(0, value);
598 static CFX_WideString _StringToWideString(FX_BSTR str)
600 FX_LPCSTR buf = str.GetCStr();
601 int len = str.GetLength();
603 return CFX_WideString();
605 CFX_WideString result;
609 for (int i = 1; i < len; i ++) {
611 if (buf[i] >= '0' && buf[i] <= '9') {
612 digit = buf[i] - '0';
613 } else if (buf[i] >= 'a' && buf[i] <= 'f') {
614 digit = buf[i] - 'a' + 10;
615 } else if (buf[i] >= 'A' && buf[i] <= 'F') {
616 digit = buf[i] - 'A' + 10;
620 ch = ch * 16 + digit;
634 void CPDF_ToUnicodeMap::Load(CPDF_Stream* pStream)
637 CPDF_StreamAcc stream;
638 stream.LoadAllData(pStream, FALSE);
639 CPDF_SimpleParser parser(stream.GetData(), stream.GetSize());
640 m_Map.EstimateSize(stream.GetSize() / 8, 1024);
642 CFX_ByteStringC word = parser.GetWord();
643 if (word.IsEmpty()) {
646 if (word == FX_BSTRC("beginbfchar")) {
648 word = parser.GetWord();
649 if (word.IsEmpty() || word == FX_BSTRC("endbfchar")) {
652 FX_DWORD srccode = _StringToCode(word);
653 word = parser.GetWord();
654 CFX_WideString destcode = _StringToWideString(word);
655 int len = destcode.GetLength();
660 m_Map.SetAt(srccode, destcode.GetAt(0));
662 m_Map.SetAt(srccode, m_MultiCharBuf.GetLength() * 0x10000 + 0xffff);
663 m_MultiCharBuf.AppendChar(destcode.GetLength());
664 m_MultiCharBuf << destcode;
667 } else if (word == FX_BSTRC("beginbfrange")) {
669 CFX_ByteString low, high;
670 low = parser.GetWord();
671 if (low.IsEmpty() || low == FX_BSTRC("endbfrange")) {
674 high = parser.GetWord();
675 FX_DWORD lowcode = _StringToCode(low);
676 FX_DWORD highcode = (lowcode & 0xffffff00) | (_StringToCode(high) & 0xff);
677 if (highcode == (FX_DWORD) - 1) {
680 CFX_ByteString start = parser.GetWord();
681 if (start == FX_BSTRC("[")) {
682 for (FX_DWORD code = lowcode; code <= highcode; code ++) {
683 CFX_ByteString dest = parser.GetWord();
684 CFX_WideString destcode = _StringToWideString(dest);
685 int len = destcode.GetLength();
690 m_Map.SetAt(code, destcode.GetAt(0));
692 m_Map.SetAt(code, m_MultiCharBuf.GetLength() * 0x10000 + 0xffff);
693 m_MultiCharBuf.AppendChar(destcode.GetLength());
694 m_MultiCharBuf << destcode;
699 CFX_WideString destcode = _StringToWideString(start);
700 int len = destcode.GetLength();
703 value = _StringToCode(start);
704 for (FX_DWORD code = lowcode; code <= highcode; code ++) {
705 m_Map.SetAt(code, value++);
708 for (FX_DWORD code = lowcode; code <= highcode; code ++) {
709 CFX_WideString retcode;
710 if (code == lowcode) {
713 retcode = _StringDataAdd(destcode);
715 m_Map.SetAt(code, m_MultiCharBuf.GetLength() * 0x10000 + 0xffff);
716 m_MultiCharBuf.AppendChar(retcode.GetLength());
717 m_MultiCharBuf << retcode;
723 } else if (word == FX_BSTRC("/Adobe-Korea1-UCS2")) {
724 CIDSet = CIDSET_KOREA1;
725 } else if (word == FX_BSTRC("/Adobe-Japan1-UCS2")) {
726 CIDSet = CIDSET_JAPAN1;
727 } else if (word == FX_BSTRC("/Adobe-CNS1-UCS2")) {
728 CIDSet = CIDSET_CNS1;
729 } else if (word == FX_BSTRC("/Adobe-GB1-UCS2")) {
734 m_pBaseMap = CPDF_ModuleMgr::Get()->GetPageModule()->GetFontGlobals()->m_CMapManager.GetCID2UnicodeMap(CIDSet, FALSE);
739 static FX_BOOL GetPredefinedEncoding(int& basemap, const CFX_ByteString& value)
741 if (value == FX_BSTRC("WinAnsiEncoding")) {
742 basemap = PDFFONT_ENCODING_WINANSI;
743 } else if (value == FX_BSTRC("MacRomanEncoding")) {
744 basemap = PDFFONT_ENCODING_MACROMAN;
745 } else if (value == FX_BSTRC("MacExpertEncoding")) {
746 basemap = PDFFONT_ENCODING_MACEXPERT;
747 } else if (value == FX_BSTRC("PDFDocEncoding")) {
748 basemap = PDFFONT_ENCODING_PDFDOC;
754 void CPDF_Font::LoadPDFEncoding(CPDF_Object* pEncoding, int& iBaseEncoding, CFX_ByteString*& pCharNames,
755 FX_BOOL bEmbedded, FX_BOOL bTrueType)
757 if (pEncoding == NULL) {
758 if (m_BaseFont == FX_BSTRC("Symbol")) {
759 iBaseEncoding = bTrueType ? PDFFONT_ENCODING_MS_SYMBOL : PDFFONT_ENCODING_ADOBE_SYMBOL;
760 } else if (!bEmbedded && iBaseEncoding == PDFFONT_ENCODING_BUILTIN) {
761 iBaseEncoding = PDFFONT_ENCODING_WINANSI;
765 if (pEncoding->GetType() == PDFOBJ_NAME) {
766 if (iBaseEncoding == PDFFONT_ENCODING_ADOBE_SYMBOL || iBaseEncoding == PDFFONT_ENCODING_ZAPFDINGBATS) {
769 if ((m_Flags & PDFFONT_SYMBOLIC) && m_BaseFont == FX_BSTRC("Symbol")) {
771 iBaseEncoding = PDFFONT_ENCODING_ADOBE_SYMBOL;
775 CFX_ByteString bsEncoding = pEncoding->GetString();
776 if (bsEncoding.Compare(FX_BSTRC("MacExpertEncoding")) == 0) {
777 bsEncoding = FX_BSTRC("WinAnsiEncoding");
779 GetPredefinedEncoding(iBaseEncoding, bsEncoding);
782 if (pEncoding->GetType() != PDFOBJ_DICTIONARY) {
785 CPDF_Dictionary* pDict = (CPDF_Dictionary*)pEncoding;
786 if (iBaseEncoding != PDFFONT_ENCODING_ADOBE_SYMBOL && iBaseEncoding != PDFFONT_ENCODING_ZAPFDINGBATS) {
787 CFX_ByteString bsEncoding = pDict->GetString(FX_BSTRC("BaseEncoding"));
788 if (bsEncoding.Compare(FX_BSTRC("MacExpertEncoding")) == 0 && bTrueType) {
789 bsEncoding = FX_BSTRC("WinAnsiEncoding");
791 GetPredefinedEncoding(iBaseEncoding, bsEncoding);
793 if ((!bEmbedded || bTrueType) && iBaseEncoding == PDFFONT_ENCODING_BUILTIN) {
794 iBaseEncoding = PDFFONT_ENCODING_STANDARD;
796 CPDF_Array* pDiffs = pDict->GetArray(FX_BSTRC("Differences"));
797 if (pDiffs == NULL) {
800 FX_NEW_VECTOR(pCharNames, CFX_ByteString, 256);
801 FX_DWORD cur_code = 0;
802 for (FX_DWORD i = 0; i < pDiffs->GetCount(); i ++) {
803 CPDF_Object* pElement = pDiffs->GetElementValue(i);
804 if (pElement == NULL) {
807 if (pElement->GetType() == PDFOBJ_NAME) {
808 if (cur_code < 256) {
809 pCharNames[cur_code] = ((CPDF_Name*)pElement)->GetString();
813 cur_code = pElement->GetInteger();
817 FX_BOOL CPDF_Font::IsStandardFont() const
819 if (m_FontType != PDFFONT_TYPE1) {
822 if (m_pFontFile != NULL) {
825 if (((CPDF_Type1Font*)this)->GetBase14Font() < 0) {
830 extern FX_LPCSTR PDF_CharNameFromPredefinedCharSet(int encoding, FX_BYTE charcode);
831 CPDF_SimpleFont::CPDF_SimpleFont()
833 FXSYS_memset8(m_CharBBox, 0xff, sizeof m_CharBBox);
834 FXSYS_memset8(m_CharWidth, 0xff, sizeof m_CharWidth);
835 FXSYS_memset8(m_GlyphIndex, 0xff, sizeof m_GlyphIndex);
836 FXSYS_memset8(m_ExtGID, 0xff, sizeof m_ExtGID);
838 m_BaseEncoding = PDFFONT_ENCODING_BUILTIN;
840 CPDF_SimpleFont::~CPDF_SimpleFont()
843 FX_DELETE_VECTOR(m_pCharNames, CFX_ByteString, 256);
846 int CPDF_SimpleFont::GlyphFromCharCode(FX_DWORD charcode, FX_BOOL *pVertGlyph)
851 if (charcode > 0xff) {
854 int index = m_GlyphIndex[(FX_BYTE)charcode];
855 if (index == 0xffff) {
860 void CPDF_SimpleFont::LoadCharMetrics(int charcode)
862 if (m_Font.m_Face == NULL) {
865 if (charcode < 0 || charcode > 0xff) {
868 int glyph_index = m_GlyphIndex[charcode];
869 if (glyph_index == 0xffff) {
870 if (m_pFontFile == NULL && charcode != 32) {
872 m_CharBBox[charcode] = m_CharBBox[32];
873 if (m_bUseFontWidth) {
874 m_CharWidth[charcode] = m_CharWidth[32];
879 int err = FXFT_Load_Glyph(m_Font.m_Face, glyph_index, FXFT_LOAD_NO_SCALE | FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH);
883 m_CharBBox[charcode].Left = TT2PDF(FXFT_Get_Glyph_HoriBearingX(m_Font.m_Face), m_Font.m_Face);
884 m_CharBBox[charcode].Right = TT2PDF(FXFT_Get_Glyph_HoriBearingX(m_Font.m_Face) + FXFT_Get_Glyph_Width(m_Font.m_Face), m_Font.m_Face);
885 m_CharBBox[charcode].Top = TT2PDF(FXFT_Get_Glyph_HoriBearingY(m_Font.m_Face), m_Font.m_Face);
886 m_CharBBox[charcode].Bottom = TT2PDF(FXFT_Get_Glyph_HoriBearingY(m_Font.m_Face) - FXFT_Get_Glyph_Height(m_Font.m_Face), m_Font.m_Face);
887 if (m_bUseFontWidth) {
888 int TT_Width = TT2PDF(FXFT_Get_Glyph_HoriAdvance(m_Font.m_Face), m_Font.m_Face);
889 if (m_CharWidth[charcode] == 0xffff) {
890 m_CharWidth[charcode] = TT_Width;
891 } else if (TT_Width && !IsEmbedded()) {
892 m_CharBBox[charcode].Right = m_CharBBox[charcode].Right * m_CharWidth[charcode] / TT_Width;
893 m_CharBBox[charcode].Left = m_CharBBox[charcode].Left * m_CharWidth[charcode] / TT_Width;
897 int CPDF_SimpleFont::GetCharWidthF(FX_DWORD charcode, int level)
899 if (charcode > 0xff) {
902 if (m_CharWidth[charcode] == 0xffff) {
903 LoadCharMetrics(charcode);
904 if (m_CharWidth[charcode] == 0xffff) {
905 m_CharWidth[charcode] = 0;
908 return (FX_INT16)m_CharWidth[charcode];
910 void CPDF_SimpleFont::GetCharBBox(FX_DWORD charcode, FX_RECT& rect, int level)
912 if (charcode > 0xff) {
915 if (m_CharBBox[charcode].Left == (FX_SHORT)0xffff) {
916 LoadCharMetrics(charcode);
918 rect.left = m_CharBBox[charcode].Left;
919 rect.right = m_CharBBox[charcode].Right;
920 rect.bottom = m_CharBBox[charcode].Bottom;
921 rect.top = m_CharBBox[charcode].Top;
923 FX_LPCSTR GetAdobeCharName(int iBaseEncoding, const CFX_ByteString* pCharNames, int charcode)
925 ASSERT(charcode >= 0 && charcode < 256);
926 if (charcode < 0 || charcode >= 256) {
929 FX_LPCSTR name = NULL;
931 name = pCharNames[charcode];
933 if ((name == NULL || name[0] == 0) && iBaseEncoding) {
934 name = PDF_CharNameFromPredefinedCharSet(iBaseEncoding, charcode);
936 if (name == NULL || name[0] == 0) {
941 FX_BOOL CPDF_SimpleFont::LoadCommon()
943 CPDF_Dictionary* pFontDesc = m_pFontDict->GetDict(FX_BSTRC("FontDescriptor"));
945 LoadFontDescriptor(pFontDesc);
947 CPDF_Array* pWidthArray = m_pFontDict->GetArray(FX_BSTRC("Widths"));
948 int width_start = 0, width_end = -1;
949 m_bUseFontWidth = TRUE;
951 m_bUseFontWidth = FALSE;
952 if (pFontDesc && pFontDesc->KeyExist(FX_BSTRC("MissingWidth"))) {
953 int MissingWidth = pFontDesc->GetInteger(FX_BSTRC("MissingWidth"));
954 for (int i = 0; i < 256; i ++) {
955 m_CharWidth[i] = MissingWidth;
958 width_start = m_pFontDict->GetInteger(FX_BSTRC("FirstChar"), 0);
959 width_end = m_pFontDict->GetInteger(FX_BSTRC("LastChar"), 0);
960 if (width_start >= 0 && width_start <= 255) {
961 if (width_end <= 0 || width_end >= width_start + (int)pWidthArray->GetCount()) {
962 width_end = width_start + pWidthArray->GetCount() - 1;
964 if (width_end > 255) {
967 for (int i = width_start; i <= width_end; i ++) {
968 m_CharWidth[i] = pWidthArray->GetInteger(i - width_start);
972 if (m_pFontFile == NULL) {
975 if (m_BaseFont.GetLength() > 8 && m_BaseFont[7] == '+') {
976 m_BaseFont = m_BaseFont.Mid(8);
979 if (!(m_Flags & PDFFONT_SYMBOLIC)) {
980 m_BaseEncoding = PDFFONT_ENCODING_STANDARD;
982 CPDF_Object* pEncoding = m_pFontDict->GetElementValue(FX_BSTRC("Encoding"));
983 LoadPDFEncoding(pEncoding, m_BaseEncoding, m_pCharNames, m_pFontFile != NULL, m_Font.IsTTFont());
986 FX_DELETE_VECTOR(m_pCharNames, CFX_ByteString, 256);
989 if (m_Font.m_Face == NULL) {
992 if (m_Flags & PDFFONT_ALLCAP) {
993 unsigned char lowercases[] = {'a', 'z', 0xe0, 0xf6, 0xf8, 0xfd};
994 for (int range = 0; range < sizeof lowercases / 2; range ++) {
995 for (int i = lowercases[range * 2]; i <= lowercases[range * 2 + 1]; i ++) {
996 if (m_GlyphIndex[i] != 0xffff && m_pFontFile != NULL) {
999 m_GlyphIndex[i] = m_GlyphIndex[i - 32];
1000 if (m_CharWidth[i - 32]) {
1001 m_CharWidth[i] = m_CharWidth[i - 32];
1002 m_CharBBox[i] = m_CharBBox[i - 32];
1010 void CPDF_SimpleFont::LoadSubstFont()
1012 if (!m_bUseFontWidth && !(m_Flags & PDFFONT_FIXEDPITCH)) {
1014 for (i = 0; i < 256; i ++) {
1015 if (m_CharWidth[i] == 0 || m_CharWidth[i] == 0xffff) {
1019 width = m_CharWidth[i];
1020 } else if (width != m_CharWidth[i]) {
1024 if (i == 256 && width) {
1025 m_Flags |= PDFFONT_FIXEDPITCH;
1028 int weight = m_StemV < 140 ? m_StemV * 5 : (m_StemV * 4 + 140);
1029 m_Font.LoadSubst(m_BaseFont, m_FontType == PDFFONT_TRUETYPE, m_Flags, weight, m_ItalicAngle, 0);
1030 if (m_Font.m_pSubstFont->m_SubstFlags & FXFONT_SUBST_NONSYMBOL) {
1033 FX_BOOL CPDF_SimpleFont::IsUnicodeCompatible() const
1035 return m_BaseEncoding != PDFFONT_ENCODING_BUILTIN && m_BaseEncoding != PDFFONT_ENCODING_ADOBE_SYMBOL &&
1036 m_BaseEncoding != PDFFONT_ENCODING_ZAPFDINGBATS;
1038 CPDF_Type1Font::CPDF_Type1Font()
1042 FX_BOOL CPDF_Type1Font::_Load()
1044 m_Base14Font = _PDF_GetStandardFontName(m_BaseFont);
1045 if (m_Base14Font >= 0) {
1046 CPDF_Dictionary* pFontDesc = m_pFontDict->GetDict(FX_BSTRC("FontDescriptor"));
1047 if (pFontDesc && pFontDesc->KeyExist(FX_BSTRC("Flags"))) {
1048 m_Flags = pFontDesc->GetInteger(FX_BSTRC("Flags"));
1050 m_Flags = m_Base14Font >= 12 ? PDFFONT_SYMBOLIC : PDFFONT_NONSYMBOLIC;
1052 if (m_Base14Font < 4)
1053 for (int i = 0; i < 256; i ++) {
1054 m_CharWidth[i] = 600;
1056 if (m_Base14Font == 12) {
1057 m_BaseEncoding = PDFFONT_ENCODING_ADOBE_SYMBOL;
1058 } else if (m_Base14Font == 13) {
1059 m_BaseEncoding = PDFFONT_ENCODING_ZAPFDINGBATS;
1060 } else if (m_Flags & PDFFONT_NONSYMBOLIC) {
1061 m_BaseEncoding = PDFFONT_ENCODING_STANDARD;
1064 return LoadCommon();
1066 static FX_BOOL FT_UseType1Charmap(FXFT_Face face)
1068 if (FXFT_Get_Face_CharmapCount(face) == 0) {
1071 if (FXFT_Get_Face_CharmapCount(face) == 1 &&
1072 FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[0]) == FXFT_ENCODING_UNICODE) {
1075 if (FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[0]) == FXFT_ENCODING_UNICODE) {
1076 FXFT_Set_Charmap(face, FXFT_Get_Face_Charmaps(face)[1]);
1078 FXFT_Set_Charmap(face, FXFT_Get_Face_Charmaps(face)[0]);
1082 extern FX_WCHAR FT_UnicodeFromCharCode(int encoding, FX_DWORD charcode);
1083 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
1084 #include "../../fxge/apple/apple_int.h"
1086 int CPDF_Type1Font::GlyphFromCharCodeExt(FX_DWORD charcode)
1088 if (charcode > 0xff) {
1091 int index = m_ExtGID[(FX_BYTE)charcode];
1092 if (index == 0xffff) {
1097 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
1098 struct _GlyphNameMap {
1099 FX_LPCSTR m_pStrAdobe;
1100 FX_LPCSTR m_pStrUnicode;
1102 static const _GlyphNameMap g_GlyphNameSubsts[] = {
1110 static int compareString(const void* key, const void* element)
1112 return FXSYS_stricmp((FX_LPCSTR)key, ((_GlyphNameMap*)element)->m_pStrAdobe);
1115 static FX_LPCSTR _GlyphNameRemap(FX_LPCSTR pStrAdobe)
1117 _GlyphNameMap* found = (_GlyphNameMap*)FXSYS_bsearch(pStrAdobe, g_GlyphNameSubsts,
1118 sizeof g_GlyphNameSubsts / sizeof(_GlyphNameMap), sizeof(_GlyphNameMap),
1121 return found->m_pStrUnicode;
1126 void CPDF_Type1Font::LoadGlyphMap()
1128 if (m_Font.m_Face == NULL) {
1131 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
1132 FX_BOOL bCoreText = TRUE;
1133 CQuartz2D & quartz2d = ((CApplePlatform *) CFX_GEModule::Get()->GetPlatformData())->_quartz2d;
1134 if (!m_Font.m_pPlatformFont) {
1135 if (m_Font.GetPsName() == CFX_WideString::FromLocal("DFHeiStd-W5")) {
1138 m_Font.m_pPlatformFont = quartz2d.CreateFont(m_Font.m_pFontData, m_Font.m_dwSize);
1139 if (NULL == m_Font.m_pPlatformFont) {
1144 if (!IsEmbedded() && (m_Base14Font < 12) && m_Font.IsTTFont()) {
1145 if (FT_UseTTCharmap(m_Font.m_Face, 3, 0)) {
1146 FX_BOOL bGotOne = FALSE;
1147 for (int charcode = 0; charcode < 256; charcode ++) {
1148 const FX_BYTE prefix[4] = {0x00, 0xf0, 0xf1, 0xf2};
1149 for (int j = 0; j < 4; j ++) {
1150 FX_WORD unicode = prefix[j] * 256 + charcode;
1151 m_GlyphIndex[charcode] = FXFT_Get_Char_Index(m_Font.m_Face, unicode);
1152 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
1153 FX_CHAR name_glyph[256];
1154 FXFT_Get_Glyph_Name(m_Font.m_Face, m_GlyphIndex[charcode], name_glyph, 256);
1155 name_glyph[255] = 0;
1156 CFStringRef name_ct = CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, name_glyph, kCFStringEncodingASCII, kCFAllocatorNull);
1157 m_ExtGID[charcode] = CGFontGetGlyphWithGlyphName((CGFontRef)m_Font.m_pPlatformFont, name_ct);
1162 if (m_GlyphIndex[charcode]) {
1169 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
1171 FXSYS_memcpy32(m_ExtGID, m_GlyphIndex, 256);
1177 FXFT_Select_Charmap(m_Font.m_Face, FXFT_ENCODING_UNICODE);
1178 if (m_BaseEncoding == 0) {
1179 m_BaseEncoding = PDFFONT_ENCODING_STANDARD;
1181 for (int charcode = 0; charcode < 256; charcode ++) {
1182 FX_LPCSTR name = GetAdobeCharName(m_BaseEncoding, m_pCharNames, charcode);
1186 m_Encoding.m_Unicodes[charcode] = PDF_UnicodeFromAdobeName(name);
1187 m_GlyphIndex[charcode] = FXFT_Get_Char_Index(m_Font.m_Face, m_Encoding.m_Unicodes[charcode]);
1188 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
1189 FX_CHAR name_glyph[256];
1190 FXFT_Get_Glyph_Name(m_Font.m_Face, m_GlyphIndex[charcode], name_glyph, 256);
1191 name_glyph[255] = 0;
1192 CFStringRef name_ct = CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, name_glyph, kCFStringEncodingASCII, kCFAllocatorNull);
1193 m_ExtGID[charcode] = CGFontGetGlyphWithGlyphName((CGFontRef)m_Font.m_pPlatformFont, name_ct);
1198 if (m_GlyphIndex[charcode] == 0 && FXSYS_strcmp(name, ".notdef") == 0) {
1199 m_Encoding.m_Unicodes[charcode] = 0x20;
1200 m_GlyphIndex[charcode] = FXFT_Get_Char_Index(m_Font.m_Face, 0x20);
1201 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
1202 FX_CHAR name_glyph[256];
1203 FXFT_Get_Glyph_Name(m_Font.m_Face, m_GlyphIndex[charcode], name_glyph, 256);
1204 name_glyph[255] = 0;
1205 CFStringRef name_ct = CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, name_glyph, kCFStringEncodingASCII, kCFAllocatorNull);
1206 m_ExtGID[charcode] = CGFontGetGlyphWithGlyphName((CGFontRef)m_Font.m_pPlatformFont, name_ct);
1213 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
1215 FXSYS_memcpy32(m_ExtGID, m_GlyphIndex, 256);
1220 FT_UseType1Charmap(m_Font.m_Face);
1221 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
1223 if (m_Flags & PDFFONT_SYMBOLIC) {
1224 for (int charcode = 0; charcode < 256; charcode ++) {
1225 FX_LPCSTR name = GetAdobeCharName(m_BaseEncoding, m_pCharNames, charcode);
1227 m_Encoding.m_Unicodes[charcode] = PDF_UnicodeFromAdobeName(name);
1228 m_GlyphIndex[charcode] = FXFT_Get_Name_Index(m_Font.m_Face, (char*)name);
1229 CFStringRef name_ct = CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, name, kCFStringEncodingASCII, kCFAllocatorNull);
1230 m_ExtGID[charcode] = CGFontGetGlyphWithGlyphName((CGFontRef)m_Font.m_pPlatformFont, name_ct);
1235 m_GlyphIndex[charcode] = FXFT_Get_Char_Index(m_Font.m_Face, charcode);
1236 FX_WCHAR unicode = 0;
1237 if (m_GlyphIndex[charcode]) {
1238 unicode = FT_UnicodeFromCharCode(PDFFONT_ENCODING_STANDARD, charcode);
1240 FX_CHAR name_glyph[256];
1241 FXSYS_memset32(name_glyph, 0, sizeof(name_glyph));
1242 FXFT_Get_Glyph_Name(m_Font.m_Face, m_GlyphIndex[charcode], name_glyph, 256);
1243 name_glyph[255] = 0;
1244 if (unicode == 0 && name_glyph[0] != 0) {
1245 unicode = PDF_UnicodeFromAdobeName(name_glyph);
1247 m_Encoding.m_Unicodes[charcode] = unicode;
1248 CFStringRef name_ct = CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, name_glyph, kCFStringEncodingASCII, kCFAllocatorNull);
1249 m_ExtGID[charcode] = CGFontGetGlyphWithGlyphName((CGFontRef)m_Font.m_pPlatformFont, name_ct);
1257 FX_BOOL bUnicode = FALSE;
1258 if (0 == FXFT_Select_Charmap(m_Font.m_Face, FXFT_ENCODING_UNICODE)) {
1261 for (int charcode = 0; charcode < 256; charcode ++) {
1262 FX_LPCSTR name = GetAdobeCharName(m_BaseEncoding, m_pCharNames, charcode);
1266 m_Encoding.m_Unicodes[charcode] = PDF_UnicodeFromAdobeName(name);
1267 FX_LPCSTR pStrUnicode = _GlyphNameRemap(name);
1268 if (pStrUnicode && 0 == FXFT_Get_Name_Index(m_Font.m_Face, (char*)name)) {
1271 m_GlyphIndex[charcode] = FXFT_Get_Name_Index(m_Font.m_Face, (char*)name);
1272 CFStringRef name_ct = CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, name, kCFStringEncodingASCII, kCFAllocatorNull);
1273 m_ExtGID[charcode] = CGFontGetGlyphWithGlyphName((CGFontRef)m_Font.m_pPlatformFont, name_ct);
1277 if (m_GlyphIndex[charcode] == 0) {
1278 if (FXSYS_strcmp(name, ".notdef") != 0 && FXSYS_strcmp(name, "space") != 0) {
1279 m_GlyphIndex[charcode] = FXFT_Get_Char_Index(m_Font.m_Face, bUnicode ? m_Encoding.m_Unicodes[charcode] : charcode);
1280 FX_CHAR name_glyph[256];
1281 FXFT_Get_Glyph_Name(m_Font.m_Face, m_GlyphIndex[charcode], name_glyph, 256);
1282 name_glyph[255] = 0;
1283 CFStringRef name_ct = CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, name_glyph, kCFStringEncodingASCII, kCFAllocatorNull);
1284 m_ExtGID[charcode] = CGFontGetGlyphWithGlyphName((CGFontRef)m_Font.m_pPlatformFont, name_ct);
1289 m_Encoding.m_Unicodes[charcode] = 0x20;
1290 m_GlyphIndex[charcode] = bUnicode ? FXFT_Get_Char_Index(m_Font.m_Face, 0x20) : 0xffff;
1291 FX_CHAR name_glyph[256];
1292 FXFT_Get_Glyph_Name(m_Font.m_Face, m_GlyphIndex[charcode], name_glyph, 256);
1293 name_glyph[255] = 0;
1294 CFStringRef name_ct = CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, name_glyph, kCFStringEncodingASCII, kCFAllocatorNull);
1295 m_ExtGID[charcode] = CGFontGetGlyphWithGlyphName((CGFontRef)m_Font.m_pPlatformFont, name_ct);
1305 if (m_Flags & PDFFONT_SYMBOLIC) {
1306 for (int charcode = 0; charcode < 256; charcode ++) {
1307 FX_LPCSTR name = GetAdobeCharName(m_BaseEncoding, m_pCharNames, charcode);
1309 m_Encoding.m_Unicodes[charcode] = PDF_UnicodeFromAdobeName(name);
1310 m_GlyphIndex[charcode] = FXFT_Get_Name_Index(m_Font.m_Face, (char*)name);
1312 m_GlyphIndex[charcode] = FXFT_Get_Char_Index(m_Font.m_Face, charcode);
1313 if (m_GlyphIndex[charcode]) {
1314 FX_WCHAR unicode = FT_UnicodeFromCharCode(PDFFONT_ENCODING_STANDARD, charcode);
1316 FX_CHAR name_glyph[256];
1317 FXSYS_memset32(name_glyph, 0, sizeof(name_glyph));
1318 FXFT_Get_Glyph_Name(m_Font.m_Face, m_GlyphIndex[charcode], name_glyph, 256);
1319 name_glyph[255] = 0;
1320 if (name_glyph[0] != 0) {
1321 unicode = PDF_UnicodeFromAdobeName(name_glyph);
1324 m_Encoding.m_Unicodes[charcode] = unicode;
1328 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
1330 FXSYS_memcpy32(m_ExtGID, m_GlyphIndex, 256);
1335 FX_BOOL bUnicode = FALSE;
1336 if (0 == FXFT_Select_Charmap(m_Font.m_Face, FXFT_ENCODING_UNICODE)) {
1339 for (int charcode = 0; charcode < 256; charcode ++) {
1340 FX_LPCSTR name = GetAdobeCharName(m_BaseEncoding, m_pCharNames, charcode);
1344 m_Encoding.m_Unicodes[charcode] = PDF_UnicodeFromAdobeName(name);
1345 m_GlyphIndex[charcode] = FXFT_Get_Name_Index(m_Font.m_Face, (char*)name);
1346 if (m_GlyphIndex[charcode] == 0) {
1347 if (FXSYS_strcmp(name, ".notdef") != 0 && FXSYS_strcmp(name, "space") != 0) {
1348 m_GlyphIndex[charcode] = FXFT_Get_Char_Index(m_Font.m_Face, bUnicode ? m_Encoding.m_Unicodes[charcode] : charcode);
1350 m_Encoding.m_Unicodes[charcode] = 0x20;
1351 m_GlyphIndex[charcode] = 0xffff;
1355 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
1357 FXSYS_memcpy32(m_ExtGID, m_GlyphIndex, 256);
1361 CPDF_FontEncoding::CPDF_FontEncoding()
1363 FXSYS_memset32(m_Unicodes, 0, sizeof(m_Unicodes));
1365 int CPDF_FontEncoding::CharCodeFromUnicode(FX_WCHAR unicode) const
1367 for (int i = 0; i < 256; i ++)
1368 if (m_Unicodes[i] == unicode) {
1373 CPDF_FontEncoding::CPDF_FontEncoding(int PredefinedEncoding)
1375 const FX_WORD* pSrc = PDF_UnicodesForPredefinedCharSet(PredefinedEncoding);
1377 FXSYS_memset32(m_Unicodes, 0, sizeof(m_Unicodes));
1379 for (int i = 0; i < 256; i++) {
1380 m_Unicodes[i] = pSrc[i];
1383 FX_BOOL CPDF_FontEncoding::IsIdentical(CPDF_FontEncoding* pAnother) const
1385 return FXSYS_memcmp32(m_Unicodes, pAnother->m_Unicodes, sizeof(m_Unicodes)) == 0;
1387 CPDF_Object* CPDF_FontEncoding::Realize()
1390 for (int cs = PDFFONT_ENCODING_WINANSI; cs < PDFFONT_ENCODING_ZAPFDINGBATS; cs ++) {
1391 const FX_WORD* pSrc = PDF_UnicodesForPredefinedCharSet(cs);
1392 FX_BOOL match = TRUE;
1393 for (int i = 0; i < 256; ++i) {
1394 if (m_Unicodes[i] != pSrc[i]) {
1405 if (predefined == PDFFONT_ENCODING_WINANSI) {
1406 return CPDF_Name::Create("WinAnsiEncoding");
1408 if (predefined == PDFFONT_ENCODING_MACROMAN) {
1409 return CPDF_Name::Create("MacRomanEncoding");
1411 if (predefined == PDFFONT_ENCODING_MACEXPERT) {
1412 return CPDF_Name::Create("MacExpertEncoding");
1416 CPDF_Dictionary* pDict = CPDF_Dictionary::Create();
1417 pDict->SetAtName(FX_BSTRC("BaseEncoding"), FX_BSTRC("WinAnsiEncoding"));
1418 const FX_WORD* pStandard = PDF_UnicodesForPredefinedCharSet(PDFFONT_ENCODING_WINANSI);
1419 CPDF_Array* pDiff = CPDF_Array::Create();
1420 for (int i = 0; i < 256; i ++) {
1421 if (pStandard[i] == m_Unicodes[i]) {
1424 pDiff->Add(CPDF_Number::Create(i));
1425 pDiff->Add(CPDF_Name::Create(PDF_AdobeNameFromUnicode(m_Unicodes[i])));
1427 pDict->SetAt(FX_BSTRC("Differences"), pDiff);
1430 CPDF_TrueTypeFont::CPDF_TrueTypeFont()
1433 FX_BOOL CPDF_TrueTypeFont::_Load()
1435 return LoadCommon();
1437 extern FX_DWORD FT_CharCodeFromUnicode(int encoding, FX_WCHAR unicode);
1438 void CPDF_TrueTypeFont::LoadGlyphMap()
1440 if (m_Font.m_Face == NULL) {
1443 int baseEncoding = m_BaseEncoding;
1444 if (m_pFontFile && m_Font.m_Face->num_charmaps > 0
1445 && (baseEncoding == PDFFONT_ENCODING_MACROMAN || baseEncoding == PDFFONT_ENCODING_WINANSI)
1446 && (m_Flags & PDFFONT_SYMBOLIC)) {
1447 FX_BOOL bSupportWin = FALSE;
1448 FX_BOOL bSupportMac = FALSE;
1449 for (int i = 0; i < FXFT_Get_Face_CharmapCount(m_Font.m_Face); i++) {
1450 int platform_id = FXFT_Get_Charmap_PlatformID(FXFT_Get_Face_Charmaps(m_Font.m_Face)[i]);
1451 if (platform_id == 0 || platform_id == 3) {
1453 } else if (platform_id == 0 || platform_id == 1) {
1457 if (baseEncoding == PDFFONT_ENCODING_WINANSI && !bSupportWin) {
1458 baseEncoding = bSupportMac ? PDFFONT_ENCODING_MACROMAN : PDFFONT_ENCODING_BUILTIN;
1459 } else if (baseEncoding == PDFFONT_ENCODING_MACROMAN && !bSupportMac) {
1460 baseEncoding = bSupportWin ? PDFFONT_ENCODING_WINANSI : PDFFONT_ENCODING_BUILTIN;
1463 if (((baseEncoding == PDFFONT_ENCODING_MACROMAN || baseEncoding == PDFFONT_ENCODING_WINANSI)
1464 && m_pCharNames == NULL) || (m_Flags & PDFFONT_NONSYMBOLIC)) {
1465 if (!FXFT_Has_Glyph_Names(m_Font.m_Face) && (!m_Font.m_Face->num_charmaps || !m_Font.m_Face->charmaps)) {
1466 int nStartChar = m_pFontDict->GetInteger(FX_BSTRC("FirstChar"));
1468 for (; charcode < nStartChar; charcode ++) {
1469 m_GlyphIndex[charcode] = 0;
1471 FX_WORD nGlyph = charcode - nStartChar + 3;
1472 for (; charcode < 256; charcode ++, nGlyph ++) {
1473 m_GlyphIndex[charcode] = nGlyph;
1477 FX_BOOL bMSUnicode = FT_UseTTCharmap(m_Font.m_Face, 3, 1);
1478 FX_BOOL bMacRoman = FALSE, bMSSymbol = FALSE;
1480 if (m_Flags & PDFFONT_NONSYMBOLIC) {
1481 bMacRoman = FT_UseTTCharmap(m_Font.m_Face, 1, 0);
1482 bMSSymbol = !bMacRoman && FT_UseTTCharmap(m_Font.m_Face, 3, 0);
1484 bMSSymbol = FT_UseTTCharmap(m_Font.m_Face, 3, 0);
1485 bMacRoman = !bMSSymbol && FT_UseTTCharmap(m_Font.m_Face, 1, 0);
1488 FX_BOOL bToUnicode = m_pFontDict->KeyExist(FX_BSTRC("ToUnicode"));
1489 for (int charcode = 0; charcode < 256; charcode ++) {
1490 FX_LPCSTR name = GetAdobeCharName(baseEncoding, m_pCharNames, charcode);
1492 m_GlyphIndex[charcode] = m_pFontFile ? FXFT_Get_Char_Index(m_Font.m_Face, charcode) : -1;
1495 m_Encoding.m_Unicodes[charcode] = PDF_UnicodeFromAdobeName(name);
1497 const FX_BYTE prefix[4] = {0x00, 0xf0, 0xf1, 0xf2};
1498 for (int j = 0; j < 4; j ++) {
1499 FX_WORD unicode = prefix[j] * 256 + charcode;
1500 m_GlyphIndex[charcode] = FXFT_Get_Char_Index(m_Font.m_Face, unicode);
1501 if (m_GlyphIndex[charcode]) {
1505 } else if (m_Encoding.m_Unicodes[charcode]) {
1507 m_GlyphIndex[charcode] = FXFT_Get_Char_Index(m_Font.m_Face, m_Encoding.m_Unicodes[charcode]);
1508 } else if (bMacRoman) {
1509 FX_DWORD maccode = FT_CharCodeFromUnicode(FXFT_ENCODING_APPLE_ROMAN, m_Encoding.m_Unicodes[charcode]);
1511 m_GlyphIndex[charcode] = FXFT_Get_Name_Index(m_Font.m_Face, (char *)name);
1513 m_GlyphIndex[charcode] = FXFT_Get_Char_Index(m_Font.m_Face, maccode);
1517 if ((m_GlyphIndex[charcode] == 0 || m_GlyphIndex[charcode] == 0xffff) && name != NULL) {
1518 if (name[0] == '.' && FXSYS_strcmp(name, ".notdef") == 0) {
1519 m_GlyphIndex[charcode] = FXFT_Get_Char_Index(m_Font.m_Face, 32);
1521 m_GlyphIndex[charcode] = FXFT_Get_Name_Index(m_Font.m_Face, (char*)name);
1522 if (m_GlyphIndex[charcode] == 0) {
1524 CFX_WideString wsUnicode = UnicodeFromCharCode(charcode);
1525 if (!wsUnicode.IsEmpty()) {
1526 m_GlyphIndex[charcode] = FXFT_Get_Char_Index(m_Font.m_Face, wsUnicode[0]);
1527 m_Encoding.m_Unicodes[charcode] = wsUnicode[0];
1530 if (m_GlyphIndex[charcode] == 0) {
1531 m_GlyphIndex[charcode] = FXFT_Get_Char_Index(m_Font.m_Face, charcode);
1539 if (FT_UseTTCharmap(m_Font.m_Face, 3, 0)) {
1540 const FX_BYTE prefix[4] = {0x00, 0xf0, 0xf1, 0xf2};
1541 FX_BOOL bGotOne = FALSE;
1542 for (int charcode = 0; charcode < 256; charcode ++) {
1543 for (int j = 0; j < 4; j ++) {
1544 FX_WORD unicode = prefix[j] * 256 + charcode;
1545 m_GlyphIndex[charcode] = FXFT_Get_Char_Index(m_Font.m_Face, unicode);
1546 if (m_GlyphIndex[charcode]) {
1553 if (baseEncoding != PDFFONT_ENCODING_BUILTIN) {
1554 for (int charcode = 0; charcode < 256; charcode ++) {
1555 FX_LPCSTR name = GetAdobeCharName(baseEncoding, m_pCharNames, charcode);
1559 m_Encoding.m_Unicodes[charcode] = PDF_UnicodeFromAdobeName(name);
1561 } else if (FT_UseTTCharmap(m_Font.m_Face, 1, 0)) {
1562 for (int charcode = 0; charcode < 256; charcode ++) {
1563 m_Encoding.m_Unicodes[charcode] = FT_UnicodeFromCharCode(FXFT_ENCODING_APPLE_ROMAN, charcode);
1569 if (FT_UseTTCharmap(m_Font.m_Face, 1, 0)) {
1570 FX_BOOL bGotOne = FALSE;
1571 for (int charcode = 0; charcode < 256; charcode ++) {
1572 m_GlyphIndex[charcode] = FXFT_Get_Char_Index(m_Font.m_Face, charcode);
1573 m_Encoding.m_Unicodes[charcode] = FT_UnicodeFromCharCode(FXFT_ENCODING_APPLE_ROMAN, charcode);
1574 if (m_GlyphIndex[charcode]) {
1578 if (m_pFontFile || bGotOne) {
1582 if (FXFT_Select_Charmap(m_Font.m_Face, FXFT_ENCODING_UNICODE) == 0) {
1583 FX_BOOL bGotOne = FALSE;
1584 const FX_WORD* pUnicodes = PDF_UnicodesForPredefinedCharSet(baseEncoding);
1585 for (int charcode = 0; charcode < 256; charcode ++) {
1586 if (m_pFontFile == NULL) {
1587 FX_LPCSTR name = GetAdobeCharName(0, m_pCharNames, charcode);
1589 m_Encoding.m_Unicodes[charcode] = PDF_UnicodeFromAdobeName(name);
1590 } else if (pUnicodes) {
1591 m_Encoding.m_Unicodes[charcode] = pUnicodes[charcode];
1594 m_Encoding.m_Unicodes[charcode] = charcode;
1596 m_GlyphIndex[charcode] = FXFT_Get_Char_Index(m_Font.m_Face, m_Encoding.m_Unicodes[charcode]);
1597 if (m_GlyphIndex[charcode]) {
1605 for (int charcode = 0; charcode < 256; charcode ++) {
1606 m_GlyphIndex[charcode] = charcode;
1609 CPDF_Type3Font::CPDF_Type3Font()
1611 m_pPageResources = NULL;
1612 FXSYS_memset32(m_CharWidthL, 0, sizeof m_CharWidthL);
1614 CPDF_Type3Font::~CPDF_Type3Font()
1616 FX_POSITION pos = m_CacheMap.GetStartPosition();
1618 FX_LPVOID key, value;
1619 m_CacheMap.GetNextAssoc(pos, key, value);
1620 delete (CPDF_Type3Char*)value;
1622 m_CacheMap.RemoveAll();
1623 pos = m_DeletedMap.GetStartPosition();
1625 FX_LPVOID key, value;
1626 m_DeletedMap.GetNextAssoc(pos, key, value);
1627 delete (CPDF_Type3Char*)key;
1630 FX_BOOL CPDF_Type3Font::_Load()
1632 m_pFontResources = m_pFontDict->GetDict(FX_BSTRC("Resources"));
1633 CPDF_Array* pMatrix = m_pFontDict->GetArray(FX_BSTRC("FontMatrix"));
1634 FX_FLOAT xscale = 1.0f, yscale = 1.0f;
1636 m_FontMatrix = pMatrix->GetMatrix();
1637 xscale = m_FontMatrix.a;
1638 yscale = m_FontMatrix.d;
1640 CPDF_Array* pBBox = m_pFontDict->GetArray(FX_BSTRC("FontBBox"));
1642 m_FontBBox.left = (FX_INT32)(FXSYS_Mul(pBBox->GetNumber(0), xscale) * 1000);
1643 m_FontBBox.bottom = (FX_INT32)(FXSYS_Mul(pBBox->GetNumber(1), yscale) * 1000);
1644 m_FontBBox.right = (FX_INT32)(FXSYS_Mul(pBBox->GetNumber(2), xscale) * 1000);
1645 m_FontBBox.top = (FX_INT32)(FXSYS_Mul(pBBox->GetNumber(3), yscale) * 1000);
1647 int StartChar = m_pFontDict->GetInteger(FX_BSTRC("FirstChar"));
1648 CPDF_Array* pWidthArray = m_pFontDict->GetArray(FX_BSTRC("Widths"));
1649 if (pWidthArray && (StartChar >= 0 && StartChar < 256)) {
1650 FX_DWORD count = pWidthArray->GetCount();
1654 if (StartChar + count > 256) {
1655 count = 256 - StartChar;
1657 for (FX_DWORD i = 0; i < count; i ++) {
1658 m_CharWidthL[StartChar + i] = FXSYS_round(FXSYS_Mul(pWidthArray->GetNumber(i), xscale) * 1000);
1661 m_pCharProcs = m_pFontDict->GetDict(FX_BSTRC("CharProcs"));
1662 CPDF_Object* pEncoding = m_pFontDict->GetElementValue(FX_BSTRC("Encoding"));
1664 LoadPDFEncoding(pEncoding, m_BaseEncoding, m_pCharNames, FALSE, FALSE);
1666 for (int i = 0; i < 256; i ++) {
1667 m_Encoding.m_Unicodes[i] = PDF_UnicodeFromAdobeName(m_pCharNames[i]);
1668 if (m_Encoding.m_Unicodes[i] == 0) {
1669 m_Encoding.m_Unicodes[i] = i;
1676 void CPDF_Type3Font::CheckType3FontMetrics()
1680 CPDF_Type3Char* CPDF_Type3Font::LoadChar(FX_DWORD charcode, int level)
1682 if (level >= _FPDF_MAX_TYPE3_FORM_LEVEL_) {
1685 CPDF_Type3Char* pChar = NULL;
1686 if (m_CacheMap.Lookup((FX_LPVOID)(FX_UINTPTR)charcode, (FX_LPVOID&)pChar)) {
1687 if (pChar->m_bPageRequired && m_pPageResources) {
1689 m_CacheMap.RemoveKey((FX_LPVOID)(FX_UINTPTR)charcode);
1690 return LoadChar(charcode, level + 1);
1694 FX_LPCSTR name = GetAdobeCharName(m_BaseEncoding, m_pCharNames, charcode);
1698 CPDF_Stream* pStream = (CPDF_Stream*)m_pCharProcs->GetElementValue(name);
1699 if (pStream == NULL || pStream->GetType() != PDFOBJ_STREAM) {
1702 pChar = FX_NEW CPDF_Type3Char;
1703 pChar->m_pForm = FX_NEW CPDF_Form(m_pDocument, m_pFontResources ? m_pFontResources : m_pPageResources, pStream, NULL);
1704 pChar->m_pForm->ParseContent(NULL, NULL, pChar, NULL, level + 1);
1705 FX_FLOAT scale = m_FontMatrix.GetXUnit();
1706 pChar->m_Width = (FX_INT32)(pChar->m_Width * scale + 0.5f);
1707 FX_RECT &rcBBox = pChar->m_BBox;
1708 CFX_FloatRect char_rect((FX_FLOAT)rcBBox.left / 1000.0f, (FX_FLOAT)rcBBox.bottom / 1000.0f,
1709 (FX_FLOAT)rcBBox.right / 1000.0f, (FX_FLOAT)rcBBox.top / 1000.0f);
1710 if (rcBBox.right <= rcBBox.left || rcBBox.bottom >= rcBBox.top) {
1711 char_rect = pChar->m_pForm->CalcBoundingBox();
1713 char_rect.Transform(&m_FontMatrix);
1714 rcBBox.left = FXSYS_round(char_rect.left * 1000);
1715 rcBBox.right = FXSYS_round(char_rect.right * 1000);
1716 rcBBox.top = FXSYS_round(char_rect.top * 1000);
1717 rcBBox.bottom = FXSYS_round(char_rect.bottom * 1000);
1718 m_CacheMap.SetAt((FX_LPVOID)(FX_UINTPTR)charcode, pChar);
1719 if (pChar->m_pForm->CountObjects() == 0) {
1720 delete pChar->m_pForm;
1721 pChar->m_pForm = NULL;
1725 int CPDF_Type3Font::GetCharWidthF(FX_DWORD charcode, int level)
1727 if (charcode > 0xff) {
1730 if (m_CharWidthL[charcode]) {
1731 return m_CharWidthL[charcode];
1733 CPDF_Type3Char* pChar = LoadChar(charcode, level);
1734 if (pChar == NULL) {
1737 return pChar->m_Width;
1739 void CPDF_Type3Font::GetCharBBox(FX_DWORD charcode, FX_RECT& rect, int level)
1741 CPDF_Type3Char* pChar = LoadChar(charcode, level);
1742 if (pChar == NULL) {
1743 rect.left = rect.right = rect.top = rect.bottom = 0;
1746 rect = pChar->m_BBox;
1748 CPDF_Type3Char::CPDF_Type3Char()
1752 m_bPageRequired = FALSE;
1755 CPDF_Type3Char::~CPDF_Type3Char()