aboutsummaryrefslogtreecommitdiff
path: root/xbmc/utils/FontUtils.cpp
blob: 2c23b90c690d84a81983f2f46c54c271d57fcdc9 (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
/*
 *  Copyright (C) 2022 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "FontUtils.h"

#include "FileItem.h"
#include "StringUtils.h"
#include "URIUtils.h"
#include "filesystem/Directory.h"
#include "filesystem/File.h"
#include "filesystem/SpecialProtocol.h"
#include "utils/CharsetConverter.h"
#include "utils/log.h"

#include <ft2build.h>

#include FT_FREETYPE_H
#include FT_SFNT_NAMES_H
#include FT_TRUETYPE_IDS_H

using namespace XFILE;

namespace
{
// \brief Get font family from SFNT table entries
std::string GetFamilyNameFromSfnt(FT_Face face)
{
  std::string familyName;

  for (FT_UInt index = 0; index < FT_Get_Sfnt_Name_Count(face); ++index)
  {
    FT_SfntName name;
    if (FT_Get_Sfnt_Name(face, index, &name) != 0)
    {
      CLog::LogF(LOGWARNING, "Failed to get SFNT name at index {}", index);
      continue;
    }

    // Get the unicode font family name (format-specific interface)
    // In properties there may be one or more font family names encoded for each platform.
    // NOTE: we give preference to MS/APPLE platform data, then fallback to MAC
    // because has been found some fonts that provide names not convertible MAC text to UTF8
    if (name.name_id == TT_NAME_ID_FONT_FAMILY)
    {
      const std::string nameEnc{reinterpret_cast<const char*>(name.string), name.string_len};

      if (name.platform_id == TT_PLATFORM_MICROSOFT ||
          name.platform_id == TT_PLATFORM_APPLE_UNICODE)
      {
        if (name.language_id != TT_MAC_LANGID_ENGLISH &&
            name.language_id != TT_MS_LANGID_ENGLISH_UNITED_STATES &&
            name.language_id != TT_MS_LANGID_ENGLISH_UNITED_KINGDOM)
          continue;

        if (CCharsetConverter::utf16BEtoUTF8(nameEnc, familyName))
          break; // Stop here to prefer the name given with this platform
        else
          CLog::LogF(LOGERROR, "Failed to convert the font name string encoded as \"UTF-16BE\"");
      }
      else if (name.platform_id == TT_PLATFORM_MACINTOSH && familyName.empty())
      {
        if (name.language_id != TT_MAC_LANGID_ENGLISH || name.encoding_id != TT_MAC_ID_ROMAN)
          continue;

        if (!CCharsetConverter::MacintoshToUTF8(nameEnc, familyName))
          CLog::LogF(LOGERROR, "Failed to convert the font name string encoded as \"macintosh\"");
      }
      else
      {
        CLog::LogF(LOGERROR, "Unsupported font SFNT name platform \"{}\"", name.platform_id);
      }
    }
  }
  return familyName;
}
} // unnamed namespace

bool UTILS::FONT::GetFontFamilyNames(const std::vector<uint8_t>& buffer,
                                     std::set<std::string>& familyNames)
{
  FT_Library m_library{nullptr};
  FT_Init_FreeType(&m_library);
  if (!m_library)
  {
    CLog::LogF(LOGERROR, "Unable to initialize freetype library");
    return false;
  }

  FT_Open_Args args{};
  args.flags = FT_OPEN_MEMORY;
  args.memory_base = reinterpret_cast<const FT_Byte*>(buffer.data());
  args.memory_size = static_cast<FT_Long>(buffer.size());

  FT_Long numFaces{0};
  FT_Long numInstances{0};
  FT_Long faceIndex{0};
  FT_Long instanceIndex{0};

  // Iterate over all font faces, files like .ttc (TrueType Collection) contains multiple fonts
  do
  {
    FT_Long idx = (instanceIndex << 16) + faceIndex;
    FT_Face face{nullptr};

    FT_Error error = FT_Open_Face(m_library, &args, idx, &face);
    if (error)
    {
      CLog::LogF(LOGERROR, "Failed to open font face at index {} error code {}", idx, error);
      break;
    }

    std::string familyName = GetFamilyNameFromSfnt(face);
    if (familyName.empty())
    {
      CLog::LogF(LOGWARNING, "Failed to get the unicode family name for \"{}\", fallback to ASCII",
                 face->family_name);
      // ASCII font family name may differ from the unicode one, use this as fallback only
      familyName = std::string{face->family_name};
      if (familyName.empty())
      {
        CLog::LogF(LOGERROR, "Family name missing in the font");
        return false;
      }
    }

    // We use the "set" container to avoid duplicate names that can happens
    // for example when a font have each style on different files
    familyNames.insert(familyName);

    numFaces = face->num_faces;
    numInstances = face->style_flags >> 16;

    FT_Done_Face(face);

    if (instanceIndex < numInstances)
      instanceIndex++;
    else
    {
      faceIndex++;
      instanceIndex = 0;
    }

  } while (faceIndex < numFaces);

  FT_Done_FreeType(m_library);
  return true;
}

bool UTILS::FONT::GetFontFamilyNames(const std::string& filepath,
                                     std::set<std::string>& familyNames)
{
  std::vector<uint8_t> buffer;
  if (filepath.empty())
    return false;

  if (XFILE::CFile().LoadFile(filepath, buffer) <= 0)
  {
    CLog::LogF(LOGERROR, "Failed to load file {}", filepath);
    return false;
  }
  return GetFontFamilyNames(buffer, familyNames);
}

std::string UTILS::FONT::GetFontFamily(std::vector<uint8_t>& buffer)
{
  FT_Library m_library{nullptr};
  FT_Init_FreeType(&m_library);
  if (!m_library)
  {
    CLog::LogF(LOGERROR, "Unable to initialize freetype library");
    return "";
  }

  // Load the font face
  FT_Face face;
  std::string familyName;
  if (FT_New_Memory_Face(m_library, reinterpret_cast<const FT_Byte*>(buffer.data()), buffer.size(),
                         0, &face) == 0)
  {
    familyName = GetFamilyNameFromSfnt(face);
    if (familyName.empty())
    {
      CLog::LogF(LOGWARNING, "Failed to get the unicode family name for \"{}\", fallback to ASCII",
                 face->family_name);
      // ASCII font family name may differ from the unicode one, use this as fallback only
      familyName = std::string{face->family_name};
      if (familyName.empty())
        CLog::LogF(LOGERROR, "Family name missing in the font");
    }
  }
  else
  {
    CLog::LogF(LOGERROR, "Failed to process font memory buffer");
  }

  FT_Done_Face(face);
  FT_Done_FreeType(m_library);
  return familyName;
}

std::string UTILS::FONT::GetFontFamily(const std::string& filepath)
{
  std::vector<uint8_t> buffer;
  if (filepath.empty())
    return "";
  if (XFILE::CFile().LoadFile(filepath, buffer) <= 0)
  {
    CLog::LogF(LOGERROR, "Failed to load file {}", filepath);
    return "";
  }
  return GetFontFamily(buffer);
}

bool UTILS::FONT::IsSupportedFontExtension(const std::string& filepath)
{
  return URIUtils::HasExtension(filepath, UTILS::FONT::SUPPORTED_EXTENSIONS_MASK);
}

void UTILS::FONT::ClearTemporaryFonts()
{
  if (!CDirectory::Exists(UTILS::FONT::FONTPATH::TEMP))
    return;

  CFileItemList items;
  CDirectory::GetDirectory(UTILS::FONT::FONTPATH::TEMP, items,
                           UTILS::FONT::SUPPORTED_EXTENSIONS_MASK,
                           DIR_FLAG_NO_FILE_DIRS | DIR_FLAG_BYPASS_CACHE | DIR_FLAG_GET_HIDDEN);
  for (const auto& item : items)
  {
    if (item->m_bIsFolder)
      continue;

    CFile::Delete(item->GetPath());
  }
}

std::string UTILS::FONT::FONTPATH::GetSystemFontPath(const std::string& filename)
{
  std::string fontPath = URIUtils::AddFileToFolder(
      CSpecialProtocol::TranslatePath(UTILS::FONT::FONTPATH::SYSTEM), filename);
  if (XFILE::CFile::Exists(fontPath))
  {
    return CSpecialProtocol::TranslatePath(fontPath);
  }

  CLog::LogF(LOGERROR, "Could not find application system font {}", filename);
  return "";
}