summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/fonts/web_font_typeface_factory.cc
blob: bb6ef79ff02aae0b6c879568e9c48024dba5de28 (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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/platform/fonts/web_font_typeface_factory.h"

#include "base/logging.h"
#include "base/metrics/histogram_macros.h"
#include "third_party/blink/renderer/platform/fonts/font_cache.h"
#include "third_party/blink/renderer/platform/fonts/opentype/font_format_check.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/wtf/assertions.h"
#include "third_party/skia/include/core/SkStream.h"
#include "third_party/skia/include/core/SkTypeface.h"

#if defined(OS_WIN)
#include "third_party/blink/public/common/dwrite_rasterizer_support/dwrite_rasterizer_support.h"
#include "third_party/blink/renderer/platform/fonts/win/dwrite_font_format_support.h"
#endif

#if defined(OS_WIN) || defined(OS_MAC)
#include "third_party/skia/include/ports/SkFontMgr_empty.h"
#endif

#if defined(OS_MAC)
#include "third_party/blink/renderer/platform/fonts/mac/core_text_font_format_support.h"
#endif

namespace blink {

bool WebFontTypefaceFactory::CreateTypeface(sk_sp<SkData> sk_data,
                                            sk_sp<SkTypeface>& typeface) {
  CHECK(!typeface);

  FontFormatCheck format_check(sk_data);

  std::unique_ptr<SkStreamAsset> stream(new SkMemoryStream(sk_data));

  if (!format_check.IsVariableFont() && !format_check.IsColorFont()) {
    typeface = DefaultFontManager()->makeFromStream(std::move(stream));
    if (typeface) {
      ReportInstantiationResult(
          InstantiationResult::kSuccessConventionalWebFont);
      return true;
    }
    // Not UMA reporting general decoding errors as these are already recorded
    // as kPackageFormatUnknown in FontResource.cpp.
    return false;
  }

  // We don't expect variable CBDT/CBLC or Sbix variable fonts for now.
  if (format_check.IsCbdtCblcColorFont()) {
    typeface = FreeTypeFontManager()->makeFromStream(std::move(stream));
    if (typeface) {
      ReportInstantiationResult(InstantiationResult::kSuccessCbdtCblcColorFont);
    }
    return typeface.get();
  }

  if (format_check.IsColrCpalColorFontV1()) {
    if (RuntimeEnabledFeatures::COLRV1FontsEnabled()) {
      typeface = FreeTypeFontManager()->makeFromStream(std::move(stream));
      if (typeface) {
        ReportInstantiationResult(InstantiationResult::kSuccessColrV1Font);
      }
      return typeface.get();
    } else {
      // Always reject COLRv1 fonts when the feature is off.
      return false;
    }
  }

  if (format_check.IsSbixColorFont()) {
    typeface = FontManagerForSbix()->makeFromStream(std::move(stream));
    if (typeface) {
      ReportInstantiationResult(InstantiationResult::kSuccessSbixFont);
    }
    return typeface.get();
  }

  // CFF2 must always go through the FreeTypeFontManager, even on Mac OS, as it
  // is not natively supported.
  if (format_check.IsCff2OutlineFont()) {
    typeface = FreeTypeFontManager()->makeFromStream(std::move(stream));
    if (typeface)
      ReportInstantiationResult(InstantiationResult::kSuccessCff2Font);
    return typeface.get();
  }

  // Variable COLR/CPAL fonts must go through the Variations
  // FontManager, which is FreeType on Windows.
  if (format_check.IsVariableFont()) {
    typeface = FontManagerForVariations()->makeFromStream(std::move(stream));
    if (typeface) {
      ReportInstantiationResult(InstantiationResult::kSuccessVariableWebFont);
    } else {
      ReportInstantiationResult(
          InstantiationResult::kErrorInstantiatingVariableFont);
    }
    return typeface.get();
  }

  if (format_check.IsColrCpalColorFontV0()) {
    typeface = FontManagerForColrCpal()->makeFromStream(std::move(stream));
    if (typeface) {
      ReportInstantiationResult(InstantiationResult::kSuccessColrCpalFont);
    }
    return typeface.get();
  }

  return false;
}

sk_sp<SkFontMgr> WebFontTypefaceFactory::FontManagerForVariations() {
#if defined(OS_WIN)
  if (DWriteVersionSupportsVariations())
    return DefaultFontManager();
  return FreeTypeFontManager();
#else
#if defined(OS_MAC)
  if (!CoreTextVersionSupportsVariations())
    return FreeTypeFontManager();
#endif
  return DefaultFontManager();
#endif
}

sk_sp<SkFontMgr> WebFontTypefaceFactory::FontManagerForSbix() {
#if defined(OS_MAC)
  return DefaultFontManager();
#endif
  return FreeTypeFontManager();
}

sk_sp<SkFontMgr> WebFontTypefaceFactory::DefaultFontManager() {
#if defined(OS_WIN)
  return FontCache::GetFontCache()->FontManager();
#else
  return sk_sp<SkFontMgr>(SkFontMgr::RefDefault());
#endif
}

sk_sp<SkFontMgr> WebFontTypefaceFactory::FreeTypeFontManager() {
#if defined(OS_WIN) || defined(OS_MAC)
  return sk_sp<SkFontMgr>(SkFontMgr_New_Custom_Empty());
#else
  return DefaultFontManager();
#endif
}

sk_sp<SkFontMgr> WebFontTypefaceFactory::FontManagerForColrCpal() {
#if defined(OS_WIN)
  if (!blink::DWriteRasterizerSupport::IsDWriteFactory2Available())
    return FreeTypeFontManager();
#endif
#if defined(OS_MAC)
  if (!CoreTextVersionSupportsColrCpal())
    return FreeTypeFontManager();
#endif
  return DefaultFontManager();
}

void WebFontTypefaceFactory::ReportInstantiationResult(
    InstantiationResult result) {
  UMA_HISTOGRAM_ENUMERATION("Blink.Fonts.VariableFontsRatio", result);
}

}  // namespace blink