summaryrefslogtreecommitdiff
path: root/xfa/fgas/font/fgas_gefont.cpp
blob: 7863ef715be04e480080680718c634d0d52adb2d (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
// 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 "xfa/fgas/font/fgas_gefont.h"

#include "xfa/fgas/crt/fgas_codepage.h"
#include "xfa/fgas/font/fgas_fontutils.h"
#include "xfa/fxfa/include/xfa_fontmgr.h"

// static
CFGAS_GEFont* CFGAS_GEFont::LoadFont(const FX_WCHAR* pszFontFamily,
                                     uint32_t dwFontStyles,
                                     uint16_t wCodePage,
                                     IFGAS_FontMgr* pFontMgr) {
#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
  if (NULL != pFontMgr) {
    return pFontMgr->GetFontByCodePage(wCodePage, dwFontStyles, pszFontFamily);
  }
  return NULL;
#else
  CFGAS_GEFont* pFont = new CFGAS_GEFont(pFontMgr);
  if (!pFont->LoadFontInternal(pszFontFamily, dwFontStyles, wCodePage)) {
    pFont->Release();
    return NULL;
  }
  return pFont;
#endif
}

// static
CFGAS_GEFont* CFGAS_GEFont::LoadFont(CFX_Font* pExtFont,
                                     IFGAS_FontMgr* pFontMgr) {
  CFGAS_GEFont* pFont = new CFGAS_GEFont(pFontMgr);
  if (!pFont->LoadFontInternal(pExtFont)) {
    pFont->Release();
    return NULL;
  }
  return pFont;
}

#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
// static
CFGAS_GEFont* CFGAS_GEFont::LoadFont(const uint8_t* pBuffer,
                                     int32_t iLength,
                                     IFGAS_FontMgr* pFontMgr) {
  CFGAS_GEFont* pFont = new CFGAS_GEFont(pFontMgr);
  if (!pFont->LoadFontInternal(pBuffer, iLength)) {
    pFont->Release();
    return nullptr;
  }
  return pFont;
}

// static
CFGAS_GEFont* CFGAS_GEFont::LoadFont(IFX_Stream* pFontStream,
                                     IFGAS_FontMgr* pFontMgr,
                                     FX_BOOL bSaveStream) {
  CFGAS_GEFont* pFont = new CFGAS_GEFont(pFontMgr);
  if (!pFont->LoadFontInternal(pFontStream, bSaveStream)) {
    pFont->Release();
    return nullptr;
  }
  return pFont;
}
#endif  // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_

CFGAS_GEFont::CFGAS_GEFont(IFGAS_FontMgr* pFontMgr)
    :
#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
      m_bUseLogFontStyle(FALSE),
      m_dwLogFontStyle(0),
#endif
      m_pFont(NULL),
      m_pFontMgr(pFontMgr),
      m_iRefCount(1),
      m_bExtFont(FALSE),
      m_pStream(NULL),
      m_pFileRead(NULL),
      m_pFontEncoding(NULL),
      m_pCharWidthMap(NULL),
      m_pRectArray(NULL),
      m_pBBoxMap(NULL),
      m_pProvider(NULL) {
}

CFGAS_GEFont::CFGAS_GEFont(const CFGAS_GEFont& src, uint32_t dwFontStyles)
    :
#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
      m_bUseLogFontStyle(FALSE),
      m_dwLogFontStyle(0),
#endif
      m_pFont(NULL),
      m_pFontMgr(src.m_pFontMgr),
      m_iRefCount(1),
      m_bExtFont(FALSE),
      m_pStream(NULL),
      m_pFileRead(NULL),
      m_pFontEncoding(NULL),
      m_pCharWidthMap(NULL),
      m_pRectArray(NULL),
      m_pBBoxMap(NULL),
      m_pProvider(NULL) {
  ASSERT(src.m_pFont);
  m_pFont = new CFX_Font;
  m_pFont->LoadClone(src.m_pFont);
  CFX_SubstFont* pSubst = m_pFont->GetSubstFont();
  if (!pSubst) {
    pSubst = new CFX_SubstFont;
    m_pFont->SetSubstFont(pSubst);
  }
  pSubst->m_Weight =
      (dwFontStyles & FX_FONTSTYLE_Bold) ? FXFONT_FW_BOLD : FXFONT_FW_NORMAL;
  if (dwFontStyles & FX_FONTSTYLE_Italic) {
    pSubst->m_SubstFlags |= FXFONT_SUBST_ITALIC;
  }
  InitFont();
}

CFGAS_GEFont::~CFGAS_GEFont() {
  for (int32_t i = 0; i < m_SubstFonts.GetSize(); i++)
    m_SubstFonts[i]->Release();

  m_SubstFonts.RemoveAll();
  m_FontMapper.clear();
  if (m_pFileRead)
    m_pFileRead->Release();

  if (m_pStream)
    m_pStream->Release();

  delete m_pFontEncoding;
  delete m_pCharWidthMap;
  delete m_pRectArray;
  delete m_pBBoxMap;
  if (!m_bExtFont)
    delete m_pFont;
}

void CFGAS_GEFont::Release() {
  if (--m_iRefCount < 1) {
    if (m_pFontMgr != NULL) {
      m_pFontMgr->RemoveFont(this);
    }
    delete this;
  }
}
CFGAS_GEFont* CFGAS_GEFont::Retain() {
  ++m_iRefCount;
  return this;
}

#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
FX_BOOL CFGAS_GEFont::LoadFontInternal(const FX_WCHAR* pszFontFamily,
                                       uint32_t dwFontStyles,
                                       uint16_t wCodePage) {
  if (m_pFont) {
    return FALSE;
  }
  CFX_ByteString csFontFamily;
  if (pszFontFamily != NULL) {
    csFontFamily = CFX_ByteString::FromUnicode(pszFontFamily);
  }
  uint32_t dwFlags = 0;
  if (dwFontStyles & FX_FONTSTYLE_FixedPitch) {
    dwFlags |= FXFONT_FIXED_PITCH;
  }
  if (dwFontStyles & FX_FONTSTYLE_Serif) {
    dwFlags |= FXFONT_SERIF;
  }
  if (dwFontStyles & FX_FONTSTYLE_Symbolic) {
    dwFlags |= FXFONT_SYMBOLIC;
  }
  if (dwFontStyles & FX_FONTSTYLE_Script) {
    dwFlags |= FXFONT_SCRIPT;
  }
  if (dwFontStyles & FX_FONTSTYLE_Italic) {
    dwFlags |= FXFONT_ITALIC;
  }
  if (dwFontStyles & FX_FONTSTYLE_Bold) {
    dwFlags |= FXFONT_BOLD;
  }
  if (dwFontStyles & FX_FONTSTYLE_ExactMatch) {
    dwFlags |= FXFONT_EXACTMATCH;
  }
  int32_t iWeight =
      (dwFontStyles & FX_FONTSTYLE_Bold) ? FXFONT_FW_BOLD : FXFONT_FW_NORMAL;
  m_pFont = new CFX_Font;
  if ((dwFlags & FXFONT_ITALIC) && (dwFlags & FXFONT_BOLD)) {
    csFontFamily += ",BoldItalic";
  } else if (dwFlags & FXFONT_BOLD) {
    csFontFamily += ",Bold";
  } else if (dwFlags & FXFONT_ITALIC) {
    csFontFamily += ",Italic";
  }
  m_pFont->LoadSubst(csFontFamily, TRUE, dwFlags, iWeight, 0, wCodePage);
  if (!m_pFont->GetFace())
    return false;
  return InitFont();
}

FX_BOOL CFGAS_GEFont::LoadFontInternal(const uint8_t* pBuffer, int32_t length) {
  if (m_pFont)
    return FALSE;

  m_pFont = new CFX_Font;
  if (!m_pFont->LoadEmbedded(pBuffer, length))
    return FALSE;
  return InitFont();
}

FX_BOOL CFGAS_GEFont::LoadFontInternal(IFX_Stream* pFontStream,
                                       FX_BOOL bSaveStream) {
  if (m_pFont || m_pFileRead || !pFontStream || pFontStream->GetLength() < 1) {
    return FALSE;
  }
  if (bSaveStream) {
    m_pStream = pFontStream;
  }
  m_pFileRead = FX_CreateFileRead(pFontStream);
  m_pFont = new CFX_Font;
  FX_BOOL bRet = m_pFont->LoadFile(m_pFileRead);
  if (bRet) {
    bRet = InitFont();
  } else {
    m_pFileRead->Release();
    m_pFileRead = nullptr;
  }
  return bRet;
}
#endif  // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_

FX_BOOL CFGAS_GEFont::LoadFontInternal(CFX_Font* pExtFont) {
  if (m_pFont || !pExtFont) {
    return FALSE;
  }
  m_pFont = pExtFont;
  m_bExtFont = TRUE;
  return InitFont();
}

FX_BOOL CFGAS_GEFont::InitFont() {
  if (!m_pFont)
    return FALSE;
  if (!m_pFontEncoding) {
    m_pFontEncoding = FX_CreateFontEncodingEx(m_pFont);
    if (!m_pFontEncoding)
      return FALSE;
  }
  if (!m_pCharWidthMap)
    m_pCharWidthMap = new CFX_WordDiscreteArray(1024);
  if (!m_pRectArray)
    m_pRectArray = new CFX_MassArrayTemplate<CFX_Rect>(16);
  if (!m_pBBoxMap)
    m_pBBoxMap = new CFX_MapPtrToPtr(16);

  return TRUE;
}

CFGAS_GEFont* CFGAS_GEFont::Derive(uint32_t dwFontStyles, uint16_t wCodePage) {
  if (GetFontStyles() == dwFontStyles)
    return Retain();
  return new CFGAS_GEFont(*this, dwFontStyles);
}

void CFGAS_GEFont::GetFamilyName(CFX_WideString& wsFamily) const {
  if (!m_pFont->GetSubstFont() ||
      m_pFont->GetSubstFont()->m_Family.GetLength() == 0) {
    wsFamily = CFX_WideString::FromLocal(m_pFont->GetFamilyName().AsStringC());
  } else {
    wsFamily = CFX_WideString::FromLocal(
        m_pFont->GetSubstFont()->m_Family.AsStringC());
  }
}

uint32_t CFGAS_GEFont::GetFontStyles() const {
  ASSERT(m_pFont != NULL);
#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
  if (m_bUseLogFontStyle) {
    return m_dwLogFontStyle;
  }
#endif
  uint32_t dwStyles = 0;
  if (!m_pFont->GetSubstFont()) {
    if (m_pFont->IsBold()) {
      dwStyles |= FX_FONTSTYLE_Bold;
    }
    if (m_pFont->IsItalic()) {
      dwStyles |= FX_FONTSTYLE_Italic;
    }
  } else {
    if (m_pFont->GetSubstFont()->m_Weight == FXFONT_FW_BOLD) {
      dwStyles |= FX_FONTSTYLE_Bold;
    }
    if (m_pFont->GetSubstFont()->m_SubstFlags & FXFONT_SUBST_ITALIC) {
      dwStyles |= FX_FONTSTYLE_Italic;
    }
  }
  return dwStyles;
}
FX_BOOL CFGAS_GEFont::GetCharWidth(FX_WCHAR wUnicode,
                                   int32_t& iWidth,
                                   FX_BOOL bCharCode) {
  return GetCharWidthInternal(wUnicode, iWidth, TRUE, bCharCode);
}
FX_BOOL CFGAS_GEFont::GetCharWidthInternal(FX_WCHAR wUnicode,
                                           int32_t& iWidth,
                                           FX_BOOL bRecursive,
                                           FX_BOOL bCharCode) {
  ASSERT(m_pCharWidthMap != NULL);
  iWidth = m_pCharWidthMap->GetAt(wUnicode, 0);
  if (iWidth < 1) {
    if (!m_pProvider ||
        !m_pProvider->GetCharWidth(this, wUnicode, iWidth, bCharCode)) {
      CFGAS_GEFont* pFont = NULL;
      int32_t iGlyph = GetGlyphIndex(wUnicode, TRUE, &pFont, bCharCode);
      if (iGlyph != 0xFFFF && pFont != NULL) {
        if (pFont == this) {
          iWidth = m_pFont->GetGlyphWidth(iGlyph);
          if (iWidth < 0) {
            iWidth = -1;
          }
        } else if (pFont->GetCharWidthInternal(wUnicode, iWidth, FALSE,
                                               bCharCode)) {
          return TRUE;
        }
      } else {
        iWidth = -1;
      }
    }
    m_pCharWidthMap->SetAtGrow(wUnicode, (int16_t)iWidth);
  } else if (iWidth == 65535) {
    iWidth = -1;
  }
  return iWidth > 0;
}
FX_BOOL CFGAS_GEFont::GetCharBBox(FX_WCHAR wUnicode,
                                  CFX_Rect& bbox,
                                  FX_BOOL bCharCode) {
  return GetCharBBoxInternal(wUnicode, bbox, TRUE, bCharCode);
}
FX_BOOL CFGAS_GEFont::GetCharBBoxInternal(FX_WCHAR wUnicode,
                                          CFX_Rect& bbox,
                                          FX_BOOL bRecursive,
                                          FX_BOOL bCharCode) {
  ASSERT(m_pRectArray != NULL);
  ASSERT(m_pBBoxMap != NULL);
  void* pRect = NULL;
  if (!m_pBBoxMap->Lookup((void*)(uintptr_t)wUnicode, pRect)) {
    CFGAS_GEFont* pFont = NULL;
    int32_t iGlyph = GetGlyphIndex(wUnicode, TRUE, &pFont, bCharCode);
    if (iGlyph != 0xFFFF && pFont != NULL) {
      if (pFont == this) {
        FX_RECT rtBBox;
        if (m_pFont->GetGlyphBBox(iGlyph, rtBBox)) {
          CFX_Rect rt;
          rt.Set(rtBBox.left, rtBBox.top, rtBBox.Width(), rtBBox.Height());
          int32_t index = m_pRectArray->Add(rt);
          pRect = m_pRectArray->GetPtrAt(index);
          m_pBBoxMap->SetAt((void*)(uintptr_t)wUnicode, pRect);
        }
      } else if (pFont->GetCharBBoxInternal(wUnicode, bbox, FALSE, bCharCode)) {
        return TRUE;
      }
    }
  }
  if (!pRect)
    return FALSE;

  bbox = *static_cast<const CFX_Rect*>(pRect);
  return TRUE;
}
FX_BOOL CFGAS_GEFont::GetBBox(CFX_Rect& bbox) {
  FX_RECT rt(0, 0, 0, 0);
  FX_BOOL bRet = m_pFont->GetBBox(rt);
  if (bRet) {
    bbox.left = rt.left;
    bbox.width = rt.Width();
    bbox.top = rt.bottom;
    bbox.height = -rt.Height();
  }
  return bRet;
}
int32_t CFGAS_GEFont::GetItalicAngle() const {
  if (!m_pFont->GetSubstFont()) {
    return 0;
  }
  return m_pFont->GetSubstFont()->m_ItalicAngle;
}
int32_t CFGAS_GEFont::GetGlyphIndex(FX_WCHAR wUnicode, FX_BOOL bCharCode) {
  return GetGlyphIndex(wUnicode, TRUE, NULL, bCharCode);
}
int32_t CFGAS_GEFont::GetGlyphIndex(FX_WCHAR wUnicode,
                                    FX_BOOL bRecursive,
                                    CFGAS_GEFont** ppFont,
                                    FX_BOOL bCharCode) {
  ASSERT(m_pFontEncoding != NULL);
  int32_t iGlyphIndex = m_pFontEncoding->GlyphFromCharCode(wUnicode);
  if (iGlyphIndex > 0) {
    if (ppFont != NULL) {
      *ppFont = this;
    }
    return iGlyphIndex;
  }
  const FGAS_FONTUSB* pFontUSB = FGAS_GetUnicodeBitField(wUnicode);
  if (pFontUSB == NULL) {
    return 0xFFFF;
  }
  uint16_t wBitField = pFontUSB->wBitField;
  if (wBitField >= 128) {
    return 0xFFFF;
  }
  auto it = m_FontMapper.find(wUnicode);
  CFGAS_GEFont* pFont = it != m_FontMapper.end() ? it->second : nullptr;
  if (pFont && pFont != this) {
    iGlyphIndex = pFont->GetGlyphIndex(wUnicode, FALSE, NULL, bCharCode);
    if (iGlyphIndex != 0xFFFF) {
      int32_t i = m_SubstFonts.Find(pFont);
      if (i > -1) {
        iGlyphIndex |= ((i + 1) << 24);
        if (ppFont)
          *ppFont = pFont;
        return iGlyphIndex;
      }
    }
  }
  if (m_pFontMgr && bRecursive) {
    CFX_WideString wsFamily;
    GetFamilyName(wsFamily);
#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
    CFGAS_GEFont* pFont = m_pFontMgr->GetDefFontByUnicode(
        wUnicode, GetFontStyles(), wsFamily.c_str());
#else
    CFGAS_GEFont* pFont = m_pFontMgr->GetFontByUnicode(
        wUnicode, GetFontStyles(), wsFamily.c_str());
    if (!pFont)
      pFont = m_pFontMgr->GetFontByUnicode(wUnicode, GetFontStyles(), NULL);
#endif
    if (pFont) {
      if (pFont == this) {
        pFont->Release();
        return 0xFFFF;
      }
      m_FontMapper[wUnicode] = pFont;
      int32_t i = m_SubstFonts.GetSize();
      m_SubstFonts.Add(pFont);
      iGlyphIndex = pFont->GetGlyphIndex(wUnicode, FALSE, NULL, bCharCode);
      if (iGlyphIndex != 0xFFFF) {
        iGlyphIndex |= ((i + 1) << 24);
        if (ppFont)
          *ppFont = pFont;
        return iGlyphIndex;
      }
    }
  }
  return 0xFFFF;
}
int32_t CFGAS_GEFont::GetAscent() const {
  return m_pFont->GetAscent();
}
int32_t CFGAS_GEFont::GetDescent() const {
  return m_pFont->GetDescent();
}
void CFGAS_GEFont::Reset() {
  for (int32_t i = 0; i < m_SubstFonts.GetSize(); i++)
    m_SubstFonts[i]->Reset();
  if (m_pCharWidthMap != NULL) {
    m_pCharWidthMap->RemoveAll();
  }
  if (m_pBBoxMap != NULL) {
    m_pBBoxMap->RemoveAll();
  }
  if (m_pRectArray != NULL) {
    m_pRectArray->RemoveAll();
  }
}
CFGAS_GEFont* CFGAS_GEFont::GetSubstFont(int32_t iGlyphIndex) const {
  iGlyphIndex = ((uint32_t)iGlyphIndex) >> 24;
  return iGlyphIndex == 0 ? const_cast<CFGAS_GEFont*>(this)
                          : m_SubstFonts[iGlyphIndex - 1];
}