Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(260)

Side by Side Diff: third_party/WebKit/Source/platform/fonts/FontCustomPlatformData.cpp

Issue 2581083003: Initial OpenType Font Variations Support (Closed)
Patch Set: Fix hash collision tests, adjust test expectations Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2007 Apple Computer, Inc. 2 * Copyright (C) 2007 Apple Computer, Inc.
3 * Copyright (c) 2007, 2008, 2009, Google Inc. All rights reserved. 3 * Copyright (c) 2007, 2008, 2009, Google Inc. All rights reserved.
4 * Copyright (C) 2010 Company 100, Inc. 4 * Copyright (C) 2010 Company 100, Inc.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are 7 * modification, are permitted provided that the following conditions are
8 * met: 8 * met:
9 * 9 *
10 * * Redistributions of source code must retain the above copyright 10 * * Redistributions of source code must retain the above copyright
(...skipping 19 matching lines...) Expand all
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */ 31 */
32 32
33 #include "platform/fonts/FontCustomPlatformData.h" 33 #include "platform/fonts/FontCustomPlatformData.h"
34 34
35 #include "platform/LayoutTestSupport.h" 35 #include "platform/LayoutTestSupport.h"
36 #include "platform/SharedBuffer.h" 36 #include "platform/SharedBuffer.h"
37 #include "platform/fonts/FontCache.h" 37 #include "platform/fonts/FontCache.h"
38 #include "platform/fonts/FontPlatformData.h" 38 #include "platform/fonts/FontPlatformData.h"
39 #include "platform/fonts/WebFontDecoder.h" 39 #include "platform/fonts/WebFontDecoder.h"
40 #include "platform/fonts/opentype/FontSettings.h"
40 #include "third_party/skia/include/core/SkStream.h" 41 #include "third_party/skia/include/core/SkStream.h"
41 #include "third_party/skia/include/core/SkTypeface.h" 42 #include "third_party/skia/include/core/SkTypeface.h"
42 #include "wtf/PtrUtil.h" 43 #include "wtf/PtrUtil.h"
43 #include <memory> 44 #include <memory>
44 45
45 namespace blink { 46 namespace blink {
46 47
47 FontCustomPlatformData::FontCustomPlatformData(sk_sp<SkTypeface> typeface, 48 FontCustomPlatformData::FontCustomPlatformData(sk_sp<SkTypeface> typeface,
48 size_t dataSize) 49 size_t dataSize)
49 : m_typeface(typeface), m_dataSize(dataSize) {} 50 : m_baseTypeface(typeface), m_dataSize(dataSize) {}
50 51
51 FontCustomPlatformData::~FontCustomPlatformData() {} 52 FontCustomPlatformData::~FontCustomPlatformData() {}
52 53
53 FontPlatformData FontCustomPlatformData::fontPlatformData( 54 FontPlatformData FontCustomPlatformData::fontPlatformData(
54 float size, 55 float size,
55 bool bold, 56 bool bold,
56 bool italic, 57 bool italic,
57 FontOrientation orientation) { 58 FontOrientation orientation,
58 ASSERT(m_typeface); 59 const FontVariationSettings* variationSettings) {
59 return FontPlatformData(m_typeface, "", size, bold && !m_typeface->isBold(), 60 DCHECK(m_baseTypeface);
60 italic && !m_typeface->isItalic(), orientation); 61
62 sk_sp<SkTypeface> returnTypeface = m_baseTypeface;
63
64 // Maximum axis count is maximum value for the OpenType USHORT, which is a
65 // 16bit unsigned. https://www.microsoft.com/typography/otspec/fvar.htm
66 // Variation settings coming from CSS can have duplicate assignments and the
67 // list can be longer than UINT16_MAX, but ignoring this for now, going with a
68 // reasonable upper limit and leaving the deduplication for TODO(drott),
69 // crbug.com/674878 second duplicate value should supersede first..
70 if (variationSettings && variationSettings->size() < UINT16_MAX) {
71 RefPtr<SkFontMgr> fm = adoptRef(SkFontMgr::RefDefault());
kojii 2016/12/21 14:53:12 I think we prefer sk_sp<SkFontMgr> fm(SkFontMgr
drott 2016/12/21 15:22:00 Good point, thanks, done.
72 Vector<SkFontMgr::FontParameters::Axis, 0> axes;
73 axes.reserveCapacity(variationSettings->size());
74 for (size_t i = 0; i < variationSettings->size(); ++i) {
75 SkFontMgr::FontParameters::Axis axis = {
76 atomicStringToFourByteTag(variationSettings->at(i).tag()),
77 SkFloatToScalar(variationSettings->at(i).value())};
78 axes.append(axis);
79 }
80
81 sk_sp<SkTypeface> skVariationFont(fm->createFromStream(
82 m_baseTypeface->openStream(nullptr)->duplicate(),
83 SkFontMgr::FontParameters().setAxes(axes.data(), axes.size())));
84
85 if (skVariationFont) {
86 returnTypeface = skVariationFont;
87 } else {
88 SkString familyName;
89 m_baseTypeface->getFamilyName(&familyName);
90 // TODO: Surface this as a console message?
91 LOG(ERROR) << "Unable for apply variation axis properties for font: "
92 << familyName.c_str();
93 }
94 }
95
96 return FontPlatformData(returnTypeface, "", size,
97 bold && !m_baseTypeface->isBold(),
98 italic && !m_baseTypeface->isItalic(), orientation);
61 } 99 }
62 100
63 std::unique_ptr<FontCustomPlatformData> FontCustomPlatformData::create( 101 std::unique_ptr<FontCustomPlatformData> FontCustomPlatformData::create(
64 SharedBuffer* buffer, 102 SharedBuffer* buffer,
65 String& otsParseMessage) { 103 String& otsParseMessage) {
66 DCHECK(buffer); 104 DCHECK(buffer);
67 WebFontDecoder decoder; 105 WebFontDecoder decoder;
68 sk_sp<SkTypeface> typeface = decoder.decode(buffer); 106 sk_sp<SkTypeface> typeface = decoder.decode(buffer);
69 if (!typeface) { 107 if (!typeface) {
70 otsParseMessage = decoder.getErrorString(); 108 otsParseMessage = decoder.getErrorString();
71 return nullptr; 109 return nullptr;
72 } 110 }
73 return WTF::wrapUnique( 111 return WTF::wrapUnique(
74 new FontCustomPlatformData(std::move(typeface), decoder.decodedSize())); 112 new FontCustomPlatformData(std::move(typeface), decoder.decodedSize()));
75 } 113 }
76 114
77 bool FontCustomPlatformData::supportsFormat(const String& format) { 115 bool FontCustomPlatformData::supportsFormat(const String& format) {
78 return equalIgnoringCase(format, "truetype") || 116 return equalIgnoringCase(format, "truetype") ||
79 equalIgnoringCase(format, "opentype") || 117 equalIgnoringCase(format, "opentype") ||
80 WebFontDecoder::supportsFormat(format); 118 WebFontDecoder::supportsFormat(format);
81 } 119 }
82 120
83 } // namespace blink 121 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698