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
|
/*
* Copyright (C) 2009 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "third_party/blink/renderer/platform/fonts/web_font_decoder.h"
#include <hb.h>
#include <stdarg.h>
#include "base/compiler_specific.h"
#include "base/numerics/safe_conversions.h"
#include "build/build_config.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/platform/fonts/font_cache.h"
#include "third_party/blink/renderer/platform/fonts/web_font_typeface_factory.h"
#include "third_party/blink/renderer/platform/instrumentation/tracing/trace_event.h"
#include "third_party/blink/renderer/platform/wtf/shared_buffer.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/ots/src/include/ots-memory-stream.h"
#include "third_party/skia/include/core/SkStream.h"
namespace blink {
namespace {
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
const size_t kMaxDecompressedSizeMb = 30;
#else
const size_t kMaxDecompressedSizeMb = 128;
#endif
class BlinkOTSContext final : public ots::OTSContext {
DISALLOW_NEW();
public:
void Message(int level, const char* format, ...) override;
ots::TableAction GetTableAction(uint32_t tag) override;
const String& GetErrorString() { return error_string_; }
private:
String error_string_;
};
void BlinkOTSContext::Message(int level, const char* format, ...) {
va_list args;
va_start(args, format);
#if defined(COMPILER_MSVC)
int result = _vscprintf(format, args);
#else
char ch;
int result = UNSAFE_TODO(vsnprintf(&ch, 1, format, args));
#endif
va_end(args);
if (result <= 0) {
error_string_ = String("OTS Error");
} else {
Vector<char, 256> buffer;
unsigned len = result;
buffer.Grow(len + 1);
va_start(args, format);
UNSAFE_TODO(vsnprintf(buffer.data(), buffer.size(), format, args));
va_end(args);
error_string_ = StringImpl::Create(base::span(buffer).first(len));
}
}
#if !defined(HB_VERSION_ATLEAST)
#define HB_VERSION_ATLEAST(major, minor, micro) 0
#endif
ots::TableAction BlinkOTSContext::GetTableAction(uint32_t tag) {
const uint32_t kCbdtTag = OTS_TAG('C', 'B', 'D', 'T');
const uint32_t kCblcTag = OTS_TAG('C', 'B', 'L', 'C');
const uint32_t kColrTag = OTS_TAG('C', 'O', 'L', 'R');
const uint32_t kCpalTag = OTS_TAG('C', 'P', 'A', 'L');
const uint32_t kCff2Tag = OTS_TAG('C', 'F', 'F', '2');
const uint32_t kSbixTag = OTS_TAG('s', 'b', 'i', 'x');
const uint32_t kStatTag = OTS_TAG('S', 'T', 'A', 'T');
#if HB_VERSION_ATLEAST(1, 0, 0)
const uint32_t kBaseTag = OTS_TAG('B', 'A', 'S', 'E');
const uint32_t kGdefTag = OTS_TAG('G', 'D', 'E', 'F');
const uint32_t kGposTag = OTS_TAG('G', 'P', 'O', 'S');
const uint32_t kGsubTag = OTS_TAG('G', 'S', 'U', 'B');
// Font Variations related tables
// See "Variation Tables" in Terminology section of
// https://www.microsoft.com/typography/otspec/otvaroverview.htm
const uint32_t kAvarTag = OTS_TAG('a', 'v', 'a', 'r');
const uint32_t kCvarTag = OTS_TAG('c', 'v', 'a', 'r');
const uint32_t kFvarTag = OTS_TAG('f', 'v', 'a', 'r');
const uint32_t kGvarTag = OTS_TAG('g', 'v', 'a', 'r');
const uint32_t kHvarTag = OTS_TAG('H', 'V', 'A', 'R');
const uint32_t kMvarTag = OTS_TAG('M', 'V', 'A', 'R');
const uint32_t kVvarTag = OTS_TAG('V', 'V', 'A', 'R');
#endif
switch (tag) {
// Google Color Emoji Tables
case kCbdtTag:
case kCblcTag:
// Windows Color Emoji Tables
case kColrTag:
case kCpalTag:
case kCff2Tag:
case kSbixTag:
case kStatTag:
#if HB_VERSION_ATLEAST(1, 0, 0)
// Let HarfBuzz handle how to deal with broken tables.
case kAvarTag:
case kBaseTag:
case kCvarTag:
case kFvarTag:
case kGvarTag:
case kHvarTag:
case kMvarTag:
case kVvarTag:
case kGdefTag:
case kGposTag:
case kGsubTag:
#endif
return ots::TABLE_ACTION_PASSTHRU;
default:
return ots::TABLE_ACTION_DEFAULT;
}
}
} // namespace
sk_sp<SkTypeface> WebFontDecoder::Decode(SegmentedBuffer* buffer) {
if (!buffer) {
SetErrorString("Empty Buffer");
return nullptr;
}
// This is the largest web font size which we'll try to transcode.
static const size_t kMaxDecompressedSize =
kMaxDecompressedSizeMb * 1024 * 1024;
if (buffer->size() > kMaxDecompressedSize) {
String error_message =
String::Format("Web font size more than %zuMB", kMaxDecompressedSizeMb);
SetErrorString(error_message.Utf8().c_str());
return nullptr;
}
// Most web fonts are compressed, so the result can be much larger than
// the original.
ots::ExpandingMemoryStream output(buffer->size(), kMaxDecompressedSize);
BlinkOTSContext ots_context;
SegmentedBuffer::DeprecatedFlatData flattened_buffer(buffer);
TRACE_EVENT_BEGIN0("blink", "DecodeFont");
bool ok = ots_context.Process(
&output, reinterpret_cast<const uint8_t*>(flattened_buffer.data()),
buffer->size());
TRACE_EVENT_END0("blink", "DecodeFont");
if (!ok) {
SetErrorString(ots_context.GetErrorString());
return nullptr;
}
const size_t decoded_length = base::checked_cast<size_t>(output.Tell());
sk_sp<SkData> sk_data = SkData::MakeWithCopy(output.get(), decoded_length);
sk_sp<SkTypeface> new_typeface;
if (!WebFontTypefaceFactory::CreateTypeface(sk_data, new_typeface)) {
SetErrorString("Unable to instantiate font face from font data.");
return nullptr;
}
decoded_size_ = decoded_length;
return new_typeface;
}
} // namespace blink
|