summaryrefslogtreecommitdiff
path: root/core/src/fxge/ge/fx_ge_font.cpp
blob: c61119d5fe4a527ce32ee7bb5398cef5b42c6da0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
 
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include "../../../include/fxge/fx_ge.h"
#include "../../../include/fxge/fx_freetype.h"
#include "text_int.h"
#define EM_ADJUST(em, a) (em == 0?(a): (a)*1000/em)
extern void _FPDFAPI_GetInternalFontData(int id1, const uint8_t*& data, FX_DWORD& size);
CFX_Font::CFX_Font()
{
    m_pSubstFont = NULL;
    m_Face = NULL;
    m_bEmbedded = FALSE;
    m_bVertical = FALSE;
    m_pFontData = NULL;
    m_pFontDataAllocation = NULL;
    m_dwSize = 0;
    m_pOwnedStream = NULL;
    m_pGsubData = NULL;
    m_pPlatformFont = NULL;
    m_pPlatformFontCollection = NULL;
    m_pDwFont = NULL;
    m_hHandle = NULL;
    m_bDwLoaded = FALSE;
    m_bLogic = FALSE;
}
FX_BOOL CFX_Font::LoadClone(const CFX_Font* pFont)
{
    if (pFont == NULL) {
        return FALSE;
    }
    m_bLogic = TRUE;
    if (pFont->m_pSubstFont) {
        m_pSubstFont = FX_NEW CFX_SubstFont;
        if (!m_pSubstFont) {
            return FALSE;
        }
        m_pSubstFont->m_Charset = pFont->m_pSubstFont->m_Charset;
        m_pSubstFont->m_ExtHandle = pFont->m_pSubstFont->m_ExtHandle;
        m_pSubstFont->m_SubstFlags = pFont->m_pSubstFont->m_SubstFlags;
        m_pSubstFont->m_Weight = pFont->m_pSubstFont->m_Weight;
        m_pSubstFont->m_Family = pFont->m_pSubstFont->m_Family;
        m_pSubstFont->m_ItalicAngle = pFont->m_pSubstFont->m_ItalicAngle;
    }
    if (pFont->m_OtfFontData.GetSize()) {
        m_OtfFontData.AttachData(pFont->m_OtfFontData.GetBuffer(), pFont->m_OtfFontData.GetSize());
    }
    m_Face = pFont->m_Face;
    m_bEmbedded = pFont->m_bEmbedded;
    m_bVertical = pFont->m_bVertical;
    m_dwSize = pFont->m_dwSize;
    m_pFontData = pFont->m_pFontData;
    m_pGsubData = pFont->m_pGsubData;
    m_pPlatformFont = pFont->m_pPlatformFont;
    m_pPlatformFontCollection = pFont->m_pPlatformFontCollection;
    m_pDwFont = pFont->m_pDwFont;
    m_hHandle = pFont->m_hHandle;
    m_bDwLoaded = pFont->m_bDwLoaded;
    m_pOwnedStream = pFont->m_pOwnedStream;
    return TRUE;
}
CFX_Font::~CFX_Font()
{
    if (m_pSubstFont) {
        delete m_pSubstFont;
        m_pSubstFont = NULL;
    }
    if (m_bLogic) {
        m_OtfFontData.DetachBuffer();
        return;
    }
    if (m_Face) {
        if(m_bEmbedded) {
            DeleteFace();
        } else {
            CFX_GEModule::Get()->GetFontMgr()->ReleaseFace(m_Face);
        }
    }
    if (m_pOwnedStream) {
        FX_Free(m_pOwnedStream); 
        m_pOwnedStream = NULL;
    }
    if (m_pGsubData) {
        FX_Free(m_pGsubData);
        m_pGsubData = NULL;
    }
#if _FXM_PLATFORM_  == _FXM_PLATFORM_APPLE_
    ReleasePlatformResource();
#endif
}
void CFX_Font::DeleteFace()
{
    FXFT_Done_Face(m_Face);
    m_Face = NULL;
}
FX_BOOL CFX_Font::LoadSubst(const CFX_ByteString& face_name, FX_BOOL bTrueType, FX_DWORD flags,
                            int weight, int italic_angle, int CharsetCP, FX_BOOL bVertical)
{
    m_bEmbedded = FALSE;
    m_bVertical = bVertical;
    m_pSubstFont = FX_NEW CFX_SubstFont;
    if (!m_pSubstFont) {
        return FALSE;
    }
    m_Face = CFX_GEModule::Get()->GetFontMgr()->FindSubstFont(face_name, bTrueType, flags, weight, italic_angle,
             CharsetCP, m_pSubstFont);
#if _FXM_PLATFORM_  == _FXM_PLATFORM_APPLE_
    if(m_pSubstFont->m_ExtHandle) {
        m_pPlatformFont = m_pSubstFont->m_ExtHandle;
        m_pSubstFont->m_ExtHandle = NULL;
    }
#endif
    if (m_Face) {
        m_pFontData = FXFT_Get_Face_Stream_Base(m_Face);
        m_dwSize = FXFT_Get_Face_Stream_Size(m_Face);
    }
    return TRUE;
}
extern "C" {
    unsigned long _FTStreamRead(FXFT_Stream stream, unsigned long offset,
                                unsigned char* buffer, unsigned long count)
    {
        if (count == 0) {
            return 0;
        }
        IFX_FileRead* pFile = (IFX_FileRead*)stream->descriptor.pointer;
        int res = pFile->ReadBlock(buffer, offset, count);
        if (res) {
            return count;
        }
        return 0;
    }
    void _FTStreamClose(FXFT_Stream stream)
    {
    }
};
FX_BOOL _LoadFile(FXFT_Library library, FXFT_Face* Face, IFX_FileRead* pFile, FXFT_Stream* stream, int32_t faceIndex = 0)
{
    FXFT_Stream stream1 = (FXFT_Stream)FX_Alloc(uint8_t, sizeof (FXFT_StreamRec));
    stream1->base = NULL;
    stream1->size = (unsigned long)pFile->GetSize();
    stream1->pos = 0;
    stream1->descriptor.pointer = pFile;
    stream1->close = _FTStreamClose;
    stream1->read = _FTStreamRead;
    FXFT_Open_Args args;
    args.flags = FT_OPEN_STREAM;
    args.stream = stream1;
    if (FXFT_Open_Face(library, &args, faceIndex, Face)) {
        FX_Free(stream1);
        return FALSE;
    }
    if (stream) {
        *stream = stream1;
    }
    return TRUE;
}
FX_BOOL CFX_Font::LoadFile(IFX_FileRead* pFile, int nFaceIndex, int* pFaceCount)
{
    m_bEmbedded = FALSE;
    FXFT_Library library;
    if (CFX_GEModule::Get()->GetFontMgr()->m_FTLibrary == NULL)
        FXFT_Init_FreeType(&CFX_GEModule::Get()->GetFontMgr()->m_FTLibrary);
    library = CFX_GEModule::Get()->GetFontMgr()->m_FTLibrary;
    FXFT_Stream stream = NULL;
    if (!_LoadFile(library, &m_Face, pFile, &stream, nFaceIndex))
        return FALSE;
    if (pFaceCount)
        *pFaceCount = (int)m_Face->num_faces;
    m_pOwnedStream = stream;
    FXFT_Set_Pixel_Sizes(m_Face, 0, 64);
    return TRUE;
}
int CFX_Font::GetGlyphWidth(FX_DWORD glyph_index)
{
    if (!m_Face) {
        return 0;
    }
    if (m_pSubstFont && (m_pSubstFont->m_SubstFlags & FXFONT_SUBST_MM)) {
        AdjustMMParams(glyph_index, 0, 0);
    }
    int err = FXFT_Load_Glyph(m_Face, glyph_index, FXFT_LOAD_NO_SCALE | FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH);
    if (err) {
        return 0;
    }
    int width = EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), FXFT_Get_Glyph_HoriAdvance(m_Face));
    return width;
}
static FXFT_Face FT_LoadFont(uint8_t* pData, int size)
{
    FXFT_Library library;
    if (CFX_GEModule::Get()->GetFontMgr()->m_FTLibrary == NULL) {
        FXFT_Init_FreeType(&CFX_GEModule::Get()->GetFontMgr()->m_FTLibrary);
    }
    library = CFX_GEModule::Get()->GetFontMgr()->m_FTLibrary;
    FXFT_Face face = NULL;
    int error = FXFT_New_Memory_Face(library, pData, size, 0, &face);
    if (error) {
        return NULL;
    }
    error = FXFT_Set_Pixel_Sizes(face, 64, 64);
    if (error) {
        return NULL;
    }
    return face;
}
FX_BOOL CFX_Font::LoadEmbedded(const uint8_t* data, FX_DWORD size)
{
    m_pFontDataAllocation = FX_Alloc(uint8_t, size);
    FXSYS_memcpy32(m_pFontDataAllocation, data, size);
    m_Face = FT_LoadFont((uint8_t*)m_pFontDataAllocation, size);
    m_pFontData = (uint8_t*)m_pFontDataAllocation;
    m_bEmbedded = TRUE;
    m_dwSize = size;
    return m_Face != NULL;
}
FX_BOOL CFX_Font::IsTTFont()
{
    if (m_Face == NULL) {
        return FALSE;
    }
    return FXFT_Is_Face_TT_OT(m_Face) == FXFT_FACE_FLAG_SFNT;
}
int CFX_Font::GetAscent() const
{
    if (m_Face == NULL) {
        return 0;
    }
    int ascent = EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), FXFT_Get_Face_Ascender(m_Face));
    return ascent;
}
int CFX_Font::GetDescent() const
{
    if (m_Face == NULL) {
        return 0;
    }
    int descent = EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), FXFT_Get_Face_Descender(m_Face));
    return descent;
}
FX_BOOL CFX_Font::GetGlyphBBox(FX_DWORD glyph_index, FX_RECT &bbox)
{
    if (m_Face == NULL) {
        return FALSE;
    }
    if (FXFT_Is_Face_Tricky(m_Face)) {
        int error = FXFT_Set_Char_Size(m_Face, 0, 1000 * 64, 72, 72);
        if (error) {
            return FALSE;
        }
        error = FXFT_Load_Glyph(m_Face, glyph_index, FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH);
        if (error) {
            return FALSE;
        }
        FXFT_BBox cbox;
        FT_Glyph glyph;
        error = FXFT_Get_Glyph(((FXFT_Face)m_Face)->glyph, &glyph);
        if (error) {
            return FALSE;
        }
        FXFT_Glyph_Get_CBox(glyph, FXFT_GLYPH_BBOX_PIXELS, &cbox);
        int pixel_size_x = ((FXFT_Face)m_Face)->size->metrics.x_ppem,
            pixel_size_y = ((FXFT_Face)m_Face)->size->metrics.y_ppem;
        if (pixel_size_x == 0 || pixel_size_y == 0) {
            bbox.left = cbox.xMin;
            bbox.right = cbox.xMax;
            bbox.top = cbox.yMax;
            bbox.bottom = cbox.yMin;
        } else {
            bbox.left = cbox.xMin * 1000 / pixel_size_x;
            bbox.right = cbox.xMax * 1000 / pixel_size_x;
            bbox.top = cbox.yMax * 1000 / pixel_size_y;
            bbox.bottom = cbox.yMin * 1000 / pixel_size_y;
        }
        if (bbox.top > FXFT_Get_Face_Ascender(m_Face)) {
            bbox.top = FXFT_Get_Face_Ascender(m_Face);
        }
        if (bbox.bottom < FXFT_Get_Face_Descender(m_Face)) {
            bbox.bottom = FXFT_Get_Face_Descender(m_Face);
        }
        FT_Done_Glyph(glyph);
        return FXFT_Set_Pixel_Sizes(m_Face, 0, 64) == 0;
    }
    if (FXFT_Load_Glyph(m_Face, glyph_index, FXFT_LOAD_NO_SCALE | FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH)) {
        return FALSE;
    }
    int em = FXFT_Get_Face_UnitsPerEM(m_Face);
    if (em == 0) {
        bbox.left = FXFT_Get_Glyph_HoriBearingX(m_Face);
        bbox.bottom = FXFT_Get_Glyph_HoriBearingY(m_Face);
        bbox.top = bbox.bottom - FXFT_Get_Glyph_Height(m_Face);
        bbox.right = bbox.left + FXFT_Get_Glyph_Width(m_Face);
    } else {
        bbox.left = FXFT_Get_Glyph_HoriBearingX(m_Face) * 1000 / em;
        bbox.top = (FXFT_Get_Glyph_HoriBearingY(m_Face) - FXFT_Get_Glyph_Height(m_Face)) * 1000 / em;
        bbox.right = (FXFT_Get_Glyph_HoriBearingX(m_Face) + FXFT_Get_Glyph_Width(m_Face)) * 1000 / em;
        bbox.bottom = (FXFT_Get_Glyph_HoriBearingY(m_Face)) * 1000 / em;
    }
    return TRUE;
}
FX_BOOL CFX_Font::IsItalic()
{
    if (m_Face == NULL) {
        return FALSE;
    }
    FX_BOOL ret = FXFT_Is_Face_Italic(m_Face) == FXFT_STYLE_FLAG_ITALIC;
    if (!ret) {
        CFX_ByteString str(FXFT_Get_Face_Style_Name(m_Face));
        str.MakeLower();
        if (str.Find("italic") != -1) {
            ret = TRUE;
        }
    }
    return ret;
}
FX_BOOL CFX_Font::IsBold()
{
    if (m_Face == NULL) {
        return FALSE;
    }
    return FXFT_Is_Face_Bold(m_Face) == FXFT_STYLE_FLAG_BOLD;
}
FX_BOOL CFX_Font::IsFixedWidth()
{
    if (m_Face == NULL) {
        return FALSE;
    }
    return FXFT_Is_Face_fixedwidth(m_Face);
}
CFX_WideString CFX_Font::GetPsName() const
{
    if (m_Face == NULL) {
        return CFX_WideString();
    }
    CFX_WideString psName = CFX_WideString::FromLocal(FXFT_Get_Postscript_Name(m_Face));
    if (psName.IsEmpty()) {
        psName =  CFX_WideString::FromLocal("Untitled");
    }
    return psName;
}
CFX_ByteString CFX_Font::GetFamilyName() const
{
    if (m_Face == NULL && m_pSubstFont == NULL) {
        return CFX_ByteString();
    }
    if (m_Face) {
        return CFX_ByteString(FXFT_Get_Face_Family_Name(m_Face));
    } else {
        return m_pSubstFont->m_Family;
    }
}
CFX_ByteString CFX_Font::GetFaceName() const
{
    if (m_Face == NULL && m_pSubstFont == NULL) {
        return CFX_ByteString();
    }
    if (m_Face) {
        CFX_ByteString facename;
        CFX_ByteString style = CFX_ByteString(FXFT_Get_Face_Style_Name(m_Face));
        facename = GetFamilyName();
        if (facename.IsEmpty()) {
            facename = "Untitled";
        }
        if (!style.IsEmpty() && style != "Regular") {
            facename += " " + style;
        }
        return facename;
    } else {
        return m_pSubstFont->m_Family;
    }
}
FX_BOOL CFX_Font::GetBBox(FX_RECT &bbox)
{
    if (m_Face == NULL) {
        return FALSE;
    }
    int em = FXFT_Get_Face_UnitsPerEM(m_Face);
    if (em == 0) {
        bbox.left = FXFT_Get_Face_xMin(m_Face);
        bbox.bottom = FXFT_Get_Face_yMax(m_Face);
        bbox.top = FXFT_Get_Face_yMin(m_Face);
        bbox.right = FXFT_Get_Face_xMax(m_Face);
    } else {
        bbox.left = FXFT_Get_Face_xMin(m_Face) * 1000 / em;
        bbox.top = FXFT_Get_Face_yMin(m_Face) * 1000 / em;
        bbox.right = FXFT_Get_Face_xMax(m_Face) * 1000 / em;
        bbox.bottom = FXFT_Get_Face_yMax(m_Face) * 1000 / em;
    }
    return TRUE;
}
int CFX_Font::GetHeight()
{
    if (m_Face == NULL) {
        return 0;
    }
    int height = EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), FXFT_Get_Face_Height(m_Face));
    return height;
}
int CFX_Font::GetMaxAdvanceWidth()
{
    if (m_Face == NULL) {
        return 0;
    }
    int width = EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), FXFT_Get_Face_MaxAdvanceWidth(m_Face));
    return width;
}
int CFX_Font::GetULPos()
{
    if (m_Face == NULL) {
        return 0;
    }
    int pos = EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), FXFT_Get_Face_UnderLinePosition(m_Face));
    return pos;
}
int CFX_Font::GetULthickness()
{
    if (m_Face == NULL) {
        return 0;
    }
    int thickness = EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), FXFT_Get_Face_UnderLineThickness(m_Face));
    return thickness;
}
CFX_UnicodeEncoding::CFX_UnicodeEncoding(CFX_Font* pFont)
{
    m_pFont = pFont;
}
FX_DWORD CFX_UnicodeEncoding::GlyphFromCharCode(FX_DWORD charcode)
{
    FXFT_Face face =  m_pFont->GetFace();
    if (!face) {
        return charcode;
    }
    if (FXFT_Select_Charmap(face, FXFT_ENCODING_UNICODE) == 0) {
        return FXFT_Get_Char_Index(face, charcode);
    }
    if (m_pFont->m_pSubstFont && m_pFont->m_pSubstFont->m_Charset == 2) {
        FX_DWORD index = 0;
        if (FXFT_Select_Charmap(face, FXFT_ENCODING_MS_SYMBOL) == 0) {
            index = FXFT_Get_Char_Index(face, charcode);
        }
        if (!index && !FXFT_Select_Charmap(face, FXFT_ENCODING_APPLE_ROMAN)) {
            return FXFT_Get_Char_Index(face, charcode);
        }
    }
    return charcode;
}
FX_DWORD CFX_UnicodeEncoding::GlyphFromCharCodeEx(FX_DWORD charcode, int encoding)
{
    FXFT_Face face =  m_pFont->GetFace();
    if (!face) {
        return charcode;
    }
    if (encoding == ENCODING_UNICODE) {
        return	GlyphFromCharCode(charcode);
    } else {
        int nmaps = FXFT_Get_Face_CharmapCount(m_pFont->m_Face);
        int i = 0;
        while (i < nmaps) {
            int encoding = FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[i++]);
            if (encoding != FXFT_ENCODING_UNICODE) {
                FXFT_Select_Charmap(face, encoding);
                break;
            }
        }
    }
    return FXFT_Get_Char_Index(face, charcode);
}
IFX_FontEncoding* FXGE_CreateUnicodeEncoding(CFX_Font* pFont)
{
    CFX_UnicodeEncoding* pEncoding = NULL;
    pEncoding = FX_NEW CFX_UnicodeEncoding(pFont);
    return pEncoding;
}
CFX_FontEncodingEX::CFX_FontEncodingEX()
{
    m_pFont = NULL;
    m_nEncodingID = FXFM_ENCODING_NONE;
}
FX_BOOL CFX_FontEncodingEX::Init(CFX_Font* pFont, FX_DWORD EncodingID)
{
    if (!pFont) {
        return FALSE;
    }
    m_pFont = pFont;
    m_nEncodingID = EncodingID;
    return TRUE;
}
FX_DWORD CFX_FontEncodingEX::GlyphFromCharCode(FX_DWORD charcode)
{
    FXFT_Face face = m_pFont->m_Face;
    FT_UInt nIndex = FXFT_Get_Char_Index(face, charcode);
    if (nIndex > 0) {
        return nIndex;
    }
    int nmaps = FXFT_Get_Face_CharmapCount(face);
    int m = 0;
    while (m < nmaps) {
        int nEncodingID = FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[m++]);
        if (m_nEncodingID == nEncodingID) {
            continue;
        }
        int error = FXFT_Select_Charmap(face, nEncodingID);
        if (error) {
            continue;
        }
        nIndex = FXFT_Get_Char_Index(face, charcode);
        if (nIndex > 0) {
            m_nEncodingID = nEncodingID;
            return nIndex;
        }
    }
    FXFT_Select_Charmap(face, m_nEncodingID);
    return 0;
}
CFX_WideString CFX_FontEncodingEX::UnicodeFromCharCode(FX_DWORD charcode) const
{
    if (m_nEncodingID == FXFM_ENCODING_UNICODE) {
        return CFX_WideString((FX_WCHAR)charcode);
    }
    return CFX_WideString((FX_WCHAR)0);
}
FX_DWORD CFX_FontEncodingEX::CharCodeFromUnicode(FX_WCHAR Unicode) const
{
    if (m_nEncodingID == FXFM_ENCODING_UNICODE || m_nEncodingID == FXFM_ENCODING_MS_SYMBOL) {
        return Unicode;
    }
    FXFT_Face face = m_pFont->m_Face;
    int nmaps = FXFT_Get_Face_CharmapCount(face);
    for (int i = 0; i < nmaps; i++) {
        int nEncodingID = FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[i]);
        if (nEncodingID == FXFM_ENCODING_UNICODE || nEncodingID == FXFM_ENCODING_MS_SYMBOL) {
            return Unicode;
        }
    }
    return -1;
}
FX_BOOL CFX_FontEncodingEX::IsUnicodeCompatible() const
{
    return m_nEncodingID == FXFM_ENCODING_UNICODE;
}
FX_DWORD CFX_FontEncodingEX::GlyphIndexFromName(const FX_CHAR* pStrName)
{
    FXFT_Face face = m_pFont->m_Face;
    return FT_Get_Name_Index(face, (FT_String*)pStrName);
}
CFX_ByteString CFX_FontEncodingEX::NameFromGlyphIndex(FX_DWORD dwGlyphIndex)
{
    FXFT_Face face = m_pFont->m_Face;
    CFX_ByteString glyphName("                ");
    if (FT_HAS_GLYPH_NAMES(((FT_Face)face))) {
        if (FT_Get_Glyph_Name((FT_Face)face, dwGlyphIndex, (FT_Pointer)(const FX_CHAR*)glyphName, 16)) {
            glyphName.Empty();
            return glyphName;
        }
        return glyphName;
    } else {
        return glyphName;
    }
}
FX_DWORD CFX_FontEncodingEX::CharCodeFromGlyphIndex(FX_DWORD dwGlyphIndex)
{
    FXFT_Face face = m_pFont->GetFace();
    FX_DWORD  charcode;
    FT_UInt  gid;
    charcode = FT_Get_First_Char((FT_Face)face, &gid);
    while (gid != 0) {
        if (dwGlyphIndex == gid) {
            return charcode;
        }
        charcode = FT_Get_Next_Char((FT_Face)face, charcode, &gid);
    }
    int nmaps = FXFT_Get_Face_CharmapCount(face);
    int m = 0;
    while (m < nmaps) {
        int nEncodingID = FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[m++]);
        if (m_nEncodingID == nEncodingID) {
            continue;
        }
        int error = FXFT_Select_Charmap(face, nEncodingID);
        if (error) {
            continue;
        }
        charcode = FT_Get_First_Char((FT_Face)face, &gid);
        while (gid != 0) {
            if (dwGlyphIndex == gid) {
                m_nEncodingID = nEncodingID;
                return charcode;
            }
            charcode = FT_Get_Next_Char((FT_Face)face, charcode, &gid);
        }
    }
    return (FX_DWORD) - 1;
}
static const FX_DWORD gs_EncodingID[] = {
    FXFM_ENCODING_MS_SYMBOL,
    FXFM_ENCODING_UNICODE,
    FXFM_ENCODING_MS_SJIS,
    FXFM_ENCODING_MS_GB2312,
    FXFM_ENCODING_MS_BIG5,
    FXFM_ENCODING_MS_WANSUNG,
    FXFM_ENCODING_MS_JOHAB,
    FXFM_ENCODING_ADOBE_STANDARD,
    FXFM_ENCODING_ADOBE_EXPERT,
    FXFM_ENCODING_ADOBE_CUSTOM,
    FXFM_ENCODING_ADOBE_LATIN_1,
    FXFM_ENCODING_OLD_LATIN_2,
    FXFM_ENCODING_APPLE_ROMAN
};
static IFX_FontEncodingEx* _FXFM_CreateFontEncoding(CFX_Font* pFont, FX_DWORD nEncodingID)
{
    int error = FXFT_Select_Charmap(pFont->m_Face, nEncodingID);
    if (error) {
        return NULL;
    }
    CFX_FontEncodingEX* pFontEncoding = FX_NEW CFX_FontEncodingEX;
    if (pFontEncoding && !pFontEncoding->Init(pFont, nEncodingID)) {
        delete pFontEncoding;
        pFontEncoding = NULL;
    }
    return pFontEncoding;
}
IFX_FontEncodingEx* FX_CreateFontEncodingEx(CFX_Font* pFont, FX_DWORD nEncodingID)
{
    if (!pFont || !pFont->m_Face) {
        return NULL;
    }
    if (nEncodingID != FXFM_ENCODING_NONE) {
        return _FXFM_CreateFontEncoding(pFont, nEncodingID);
    }
    static int s_count = sizeof(gs_EncodingID) / sizeof(FX_DWORD);
    for (int i = 0; i < s_count; i++) {
        IFX_FontEncodingEx* pFontEncoding = _FXFM_CreateFontEncoding(pFont, gs_EncodingID[i]);
        if (pFontEncoding) {
            return pFontEncoding;
        }
    }
    return NULL;
}