summaryrefslogtreecommitdiff
path: root/core/fxge/ge/cfx_folderfontinfo.cpp
blob: ff8cc4c29f0d1534b39eedab077d1e69175a0db8 (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
// Copyright 2016 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 "core/fxge/ge/cfx_folderfontinfo.h"

#include <limits>

#include "core/fxge/cfx_fontmapper.h"
#include "core/fxge/fx_font.h"

#include "third_party/base/stl_util.h"

namespace {

const struct {
  const char* m_pName;
  const char* m_pSubstName;
} Base14Substs[] = {
    {"Courier", "Courier New"},
    {"Courier-Bold", "Courier New Bold"},
    {"Courier-BoldOblique", "Courier New Bold Italic"},
    {"Courier-Oblique", "Courier New Italic"},
    {"Helvetica", "Arial"},
    {"Helvetica-Bold", "Arial Bold"},
    {"Helvetica-BoldOblique", "Arial Bold Italic"},
    {"Helvetica-Oblique", "Arial Italic"},
    {"Times-Roman", "Times New Roman"},
    {"Times-Bold", "Times New Roman Bold"},
    {"Times-BoldItalic", "Times New Roman Bold Italic"},
    {"Times-Italic", "Times New Roman Italic"},
};

CFX_ByteString FPDF_ReadStringFromFile(FILE* pFile, uint32_t size) {
  CFX_ByteString buffer;
  if (!fread(buffer.GetBuffer(size), size, 1, pFile))
    return CFX_ByteString();
  buffer.ReleaseBuffer(size);
  return buffer;
}

CFX_ByteString FPDF_LoadTableFromTT(FILE* pFile,
                                    const uint8_t* pTables,
                                    uint32_t nTables,
                                    uint32_t tag) {
  for (uint32_t i = 0; i < nTables; i++) {
    const uint8_t* p = pTables + i * 16;
    if (GET_TT_LONG(p) == tag) {
      uint32_t offset = GET_TT_LONG(p + 8);
      uint32_t size = GET_TT_LONG(p + 12);
      fseek(pFile, offset, SEEK_SET);
      return FPDF_ReadStringFromFile(pFile, size);
    }
  }
  return CFX_ByteString();
}

uint32_t GetCharset(int charset) {
  switch (charset) {
    case FXFONT_SHIFTJIS_CHARSET:
      return CHARSET_FLAG_SHIFTJIS;
    case FXFONT_GB2312_CHARSET:
      return CHARSET_FLAG_GB;
    case FXFONT_CHINESEBIG5_CHARSET:
      return CHARSET_FLAG_BIG5;
    case FXFONT_HANGUL_CHARSET:
      return CHARSET_FLAG_KOREAN;
    case FXFONT_SYMBOL_CHARSET:
      return CHARSET_FLAG_SYMBOL;
    case FXFONT_ANSI_CHARSET:
      return CHARSET_FLAG_ANSI;
    default:
      break;
  }
  return 0;
}

int32_t GetSimilarValue(int weight,
                        bool bItalic,
                        int pitch_family,
                        uint32_t style) {
  int32_t iSimilarValue = 0;
  if (!!(style & FXFONT_BOLD) == (weight > 400))
    iSimilarValue += 16;
  if (!!(style & FXFONT_ITALIC) == bItalic)
    iSimilarValue += 16;
  if (!!(style & FXFONT_SERIF) == !!(pitch_family & FXFONT_FF_ROMAN))
    iSimilarValue += 16;
  if (!!(style & FXFONT_SCRIPT) == !!(pitch_family & FXFONT_FF_SCRIPT))
    iSimilarValue += 8;
  if (!!(style & FXFONT_FIXED_PITCH) ==
      !!(pitch_family & FXFONT_FF_FIXEDPITCH)) {
    iSimilarValue += 8;
  }
  return iSimilarValue;
}

}  // namespace

CFX_FolderFontInfo::CFX_FolderFontInfo() {}

CFX_FolderFontInfo::~CFX_FolderFontInfo() {
  for (const auto& pair : m_FontList)
    delete pair.second;
}

void CFX_FolderFontInfo::AddPath(const CFX_ByteStringC& path) {
  m_PathList.push_back(CFX_ByteString(path));
}

bool CFX_FolderFontInfo::EnumFontList(CFX_FontMapper* pMapper) {
  m_pMapper = pMapper;
  for (const auto& path : m_PathList)
    ScanPath(path);
  return true;
}

void CFX_FolderFontInfo::ScanPath(const CFX_ByteString& path) {
  FX_FileHandle* handle = FX_OpenFolder(path.c_str());
  if (!handle)
    return;

  CFX_ByteString filename;
  bool bFolder;
  while (FX_GetNextFile(handle, &filename, &bFolder)) {
    if (bFolder) {
      if (filename == "." || filename == "..")
        continue;
    } else {
      CFX_ByteString ext = filename.Right(4);
      ext.MakeUpper();
      if (ext != ".TTF" && ext != ".OTF" && ext != ".TTC")
        continue;
    }

    CFX_ByteString fullpath = path;
#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
    fullpath += "\\";
#else
    fullpath += "/";
#endif

    fullpath += filename;
    bFolder ? ScanPath(fullpath) : ScanFile(fullpath);
  }
  FX_CloseFolder(handle);
}

void CFX_FolderFontInfo::ScanFile(const CFX_ByteString& path) {
  FILE* pFile = fopen(path.c_str(), "rb");
  if (!pFile)
    return;

  fseek(pFile, 0, SEEK_END);

  uint32_t filesize = ftell(pFile);
  uint8_t buffer[16];
  fseek(pFile, 0, SEEK_SET);

  size_t readCnt = fread(buffer, 12, 1, pFile);
  if (readCnt != 1) {
    fclose(pFile);
    return;
  }

  if (GET_TT_LONG(buffer) == kTableTTCF) {
    uint32_t nFaces = GET_TT_LONG(buffer + 8);
    if (nFaces > std::numeric_limits<uint32_t>::max() / 4) {
      fclose(pFile);
      return;
    }
    uint32_t face_bytes = nFaces * 4;
    uint8_t* offsets = FX_Alloc(uint8_t, face_bytes);
    readCnt = fread(offsets, 1, face_bytes, pFile);
    if (readCnt != face_bytes) {
      FX_Free(offsets);
      fclose(pFile);
      return;
    }
    for (uint32_t i = 0; i < nFaces; i++) {
      uint8_t* p = offsets + i * 4;
      ReportFace(path, pFile, filesize, GET_TT_LONG(p));
    }
    FX_Free(offsets);
  } else {
    ReportFace(path, pFile, filesize, 0);
  }
  fclose(pFile);
}

void CFX_FolderFontInfo::ReportFace(const CFX_ByteString& path,
                                    FILE* pFile,
                                    uint32_t filesize,
                                    uint32_t offset) {
  fseek(pFile, offset, SEEK_SET);
  char buffer[16];
  if (!fread(buffer, 12, 1, pFile))
    return;

  uint32_t nTables = GET_TT_SHORT(buffer + 4);
  CFX_ByteString tables = FPDF_ReadStringFromFile(pFile, nTables * 16);
  if (tables.IsEmpty())
    return;

  CFX_ByteString names =
      FPDF_LoadTableFromTT(pFile, tables.raw_str(), nTables, 0x6e616d65);
  if (names.IsEmpty())
    return;

  CFX_ByteString facename =
      GetNameFromTT(names.raw_str(), names.GetLength(), 1);
  if (facename.IsEmpty())
    return;

  CFX_ByteString style = GetNameFromTT(names.raw_str(), names.GetLength(), 2);
  if (style != "Regular")
    facename += " " + style;

  if (pdfium::ContainsKey(m_FontList, facename))
    return;

  CFX_FontFaceInfo* pInfo =
      new CFX_FontFaceInfo(path, facename, tables, offset, filesize);
  CFX_ByteString os2 =
      FPDF_LoadTableFromTT(pFile, tables.raw_str(), nTables, 0x4f532f32);
  if (os2.GetLength() >= 86) {
    const uint8_t* p = os2.raw_str() + 78;
    uint32_t codepages = GET_TT_LONG(p);
    if (codepages & (1 << 17)) {
      m_pMapper->AddInstalledFont(facename, FXFONT_SHIFTJIS_CHARSET);
      pInfo->m_Charsets |= CHARSET_FLAG_SHIFTJIS;
    }
    if (codepages & (1 << 18)) {
      m_pMapper->AddInstalledFont(facename, FXFONT_GB2312_CHARSET);
      pInfo->m_Charsets |= CHARSET_FLAG_GB;
    }
    if (codepages & (1 << 20)) {
      m_pMapper->AddInstalledFont(facename, FXFONT_CHINESEBIG5_CHARSET);
      pInfo->m_Charsets |= CHARSET_FLAG_BIG5;
    }
    if ((codepages & (1 << 19)) || (codepages & (1 << 21))) {
      m_pMapper->AddInstalledFont(facename, FXFONT_HANGUL_CHARSET);
      pInfo->m_Charsets |= CHARSET_FLAG_KOREAN;
    }
    if (codepages & (1 << 31)) {
      m_pMapper->AddInstalledFont(facename, FXFONT_SYMBOL_CHARSET);
      pInfo->m_Charsets |= CHARSET_FLAG_SYMBOL;
    }
  }
  m_pMapper->AddInstalledFont(facename, FXFONT_ANSI_CHARSET);
  pInfo->m_Charsets |= CHARSET_FLAG_ANSI;
  pInfo->m_Styles = 0;
  if (style.Find("Bold") > -1)
    pInfo->m_Styles |= FXFONT_BOLD;
  if (style.Find("Italic") > -1 || style.Find("Oblique") > -1)
    pInfo->m_Styles |= FXFONT_ITALIC;
  if (facename.Find("Serif") > -1)
    pInfo->m_Styles |= FXFONT_SERIF;

  m_FontList[facename] = pInfo;
}

void* CFX_FolderFontInfo::GetSubstFont(const CFX_ByteString& face) {
  for (size_t iBaseFont = 0; iBaseFont < FX_ArraySize(Base14Substs);
       iBaseFont++) {
    if (face == Base14Substs[iBaseFont].m_pName)
      return GetFont(Base14Substs[iBaseFont].m_pSubstName);
  }
  return nullptr;
}

void* CFX_FolderFontInfo::FindFont(int weight,
                                   bool bItalic,
                                   int charset,
                                   int pitch_family,
                                   const char* family,
                                   bool bMatchName) {
  CFX_FontFaceInfo* pFind = nullptr;
  if (charset == FXFONT_ANSI_CHARSET && (pitch_family & FXFONT_FF_FIXEDPITCH))
    return GetFont("Courier New");
  uint32_t charset_flag = GetCharset(charset);
  int32_t iBestSimilar = 0;
  for (const auto& it : m_FontList) {
    const CFX_ByteString& bsName = it.first;
    CFX_FontFaceInfo* pFont = it.second;
    if (!(pFont->m_Charsets & charset_flag) &&
        charset != FXFONT_DEFAULT_CHARSET) {
      continue;
    }
    int32_t index = bsName.Find(family);
    if (bMatchName && index < 0)
      continue;
    int32_t iSimilarValue =
        GetSimilarValue(weight, bItalic, pitch_family, pFont->m_Styles);
    if (iSimilarValue > iBestSimilar) {
      iBestSimilar = iSimilarValue;
      pFind = pFont;
    }
  }
  return pFind;
}

void* CFX_FolderFontInfo::MapFont(int weight,
                                  bool bItalic,
                                  int charset,
                                  int pitch_family,
                                  const char* family,
                                  int& iExact) {
  return nullptr;
}

#ifdef PDF_ENABLE_XFA
void* CFX_FolderFontInfo::MapFontByUnicode(uint32_t dwUnicode,
                                           int weight,
                                           bool bItalic,
                                           int pitch_family) {
  return nullptr;
}
#endif  // PDF_ENABLE_XFA

void* CFX_FolderFontInfo::GetFont(const char* face) {
  auto it = m_FontList.find(face);
  return it != m_FontList.end() ? it->second : nullptr;
}

uint32_t CFX_FolderFontInfo::GetFontData(void* hFont,
                                         uint32_t table,
                                         uint8_t* buffer,
                                         uint32_t size) {
  if (!hFont)
    return 0;

  const CFX_FontFaceInfo* pFont = static_cast<CFX_FontFaceInfo*>(hFont);
  uint32_t datasize = 0;
  uint32_t offset = 0;
  if (table == 0) {
    datasize = pFont->m_FontOffset ? 0 : pFont->m_FileSize;
  } else if (table == kTableTTCF) {
    datasize = pFont->m_FontOffset ? pFont->m_FileSize : 0;
  } else {
    uint32_t nTables = pFont->m_FontTables.GetLength() / 16;
    for (uint32_t i = 0; i < nTables; i++) {
      const uint8_t* p = pFont->m_FontTables.raw_str() + i * 16;
      if (GET_TT_LONG(p) == table) {
        offset = GET_TT_LONG(p + 8);
        datasize = GET_TT_LONG(p + 12);
      }
    }
  }

  if (!datasize || size < datasize)
    return datasize;

  FILE* pFile = fopen(pFont->m_FilePath.c_str(), "rb");
  if (!pFile)
    return 0;

  if (fseek(pFile, offset, SEEK_SET) < 0 ||
      fread(buffer, datasize, 1, pFile) != 1) {
    datasize = 0;
  }
  fclose(pFile);
  return datasize;
}

void CFX_FolderFontInfo::DeleteFont(void* hFont) {}

bool CFX_FolderFontInfo::GetFaceName(void* hFont, CFX_ByteString* name) {
  if (!hFont)
    return false;
  *name = static_cast<CFX_FontFaceInfo*>(hFont)->m_FaceName;
  return true;
}

bool CFX_FolderFontInfo::GetFontCharset(void* hFont, int* charset) {
  return false;
}