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 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
|
/*
* Copyright (C) 2007-2022 Apple 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:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``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 APPLE INC. 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 "config.h"
#include "CSSFontFace.h"
#include "CSSFontFaceSource.h"
#include "CSSFontFaceSrcValue.h"
#include "CSSFontFeatureValue.h"
#include "CSSFontSelector.h"
#include "CSSFontStyleRangeValue.h"
#include "CSSPrimitiveValueMappings.h"
#include "CSSUnicodeRangeValue.h"
#include "CSSValue.h"
#include "CSSValueList.h"
#include "CachedFont.h"
#include "Document.h"
#include "DocumentInlines.h"
#include "Font.h"
#include "FontCache.h"
#include "FontDescription.h"
#include "FontFace.h"
#include "FontPaletteValues.h"
#include "Settings.h"
#include "SharedBuffer.h"
#include "StyleProperties.h"
#include "StyleResolveForFont.h"
#include "StyleRule.h"
namespace WebCore {
DEFINE_ALLOCATOR_WITH_HEAP_IDENTIFIER(CSSFontFace);
template<typename T> void iterateClients(WeakHashSet<CSSFontFaceClient>& clients, T callback)
{
for (auto& client : copyToVectorOf<Ref<CSSFontFaceClient>>(clients))
callback(client);
}
void CSSFontFace::appendSources(CSSFontFace& fontFace, CSSValueList& srcList, ScriptExecutionContext* context, bool isInitiatingElementInUserAgentShadowTree)
{
bool allowDownloading = context && (context->settingsValues().downloadableBinaryFontTrustedTypes != DownloadableBinaryFontTrustedTypes::None);
for (auto& src : srcList) {
// An item in the list either specifies a string (local font name) or a URL (remote font to download).
if (auto local = dynamicDowncast<CSSFontFaceSrcLocalValue>(src)) {
if (!local->svgFontFaceElement())
fontFace.adoptSource(makeUnique<CSSFontFaceSource>(fontFace, local->fontFaceName()));
else if (allowDownloading)
fontFace.adoptSource(makeUnique<CSSFontFaceSource>(fontFace, local->fontFaceName(), *local->svgFontFaceElement()));
} else {
if (allowDownloading) {
if (auto request = downcast<CSSFontFaceSrcResourceValue>(const_cast<CSSValue&>(src)).fontLoadRequest(*context, isInitiatingElementInUserAgentShadowTree))
fontFace.adoptSource(makeUnique<CSSFontFaceSource>(fontFace, *context->cssFontSelector(), makeUniqueRefFromNonNullUniquePtr(WTFMove(request))));
}
}
}
fontFace.sourcesPopulated();
}
Ref<CSSFontFace> CSSFontFace::create(CSSFontSelector& fontSelector, StyleRuleFontFace* cssConnection, FontFace* wrapper, bool isLocalFallback)
{
auto* context = fontSelector.scriptExecutionContext();
const auto* settings = context ? &context->settingsValues() : nullptr;
auto result = adoptRef(*new CSSFontFace(settings, cssConnection, wrapper, isLocalFallback));
result->addClient(fontSelector);
return result;
}
static std::variant<Ref<MutableStyleProperties>, Ref<StyleRuleFontFace>> propertiesOrCSSConnection(StyleRuleFontFace* connection)
{
if (connection)
return Ref { *connection };
return MutableStyleProperties::create();
}
CSSFontFace::CSSFontFace(const Settings::Values* settings, StyleRuleFontFace* connection, FontFace* wrapper, bool isLocalFallback)
: m_propertiesOrCSSConnection(propertiesOrCSSConnection(connection))
, m_wrapper(wrapper)
, m_isLocalFallback(isLocalFallback)
, m_mayBePurged(!wrapper)
, m_shouldIgnoreFontLoadCompletions(settings && settings->shouldIgnoreFontLoadCompletions)
, m_fontLoadTimingOverride(settings ? settings->fontLoadTimingOverride : FontLoadTimingOverride::None)
, m_allowUserInstalledFonts(settings && !settings->shouldAllowUserInstalledFonts ? AllowUserInstalledFonts::No : AllowUserInstalledFonts::Yes)
, m_timeoutTimer(*this, &CSSFontFace::timeoutFired)
{
}
CSSFontFace::~CSSFontFace() = default;
const StyleProperties& CSSFontFace::properties() const
{
return WTF::switchOn(m_propertiesOrCSSConnection,
[] (const Ref<MutableStyleProperties>& properties) -> const StyleProperties& {
return properties;
},
[] (const Ref<StyleRuleFontFace>& connection) -> const StyleProperties& {
return connection->properties();
}
);
}
MutableStyleProperties& CSSFontFace::mutableProperties()
{
return WTF::switchOn(m_propertiesOrCSSConnection,
[] (const Ref<MutableStyleProperties>& properties) -> MutableStyleProperties& {
return properties;
},
[] (const Ref<StyleRuleFontFace>& connection) -> MutableStyleProperties& {
return connection->mutableProperties();
}
);
}
StyleRuleFontFace* CSSFontFace::cssConnection() const
{
return WTF::switchOn(m_propertiesOrCSSConnection,
[] (const Ref<MutableStyleProperties>&) -> StyleRuleFontFace* {
return nullptr;
},
[] (const Ref<StyleRuleFontFace>& connection) {
return connection.ptr();
}
);
}
// FIXME: Don't use a list here and rename to setFamily. https://bugs.webkit.org/show_bug.cgi?id=196381
void CSSFontFace::setFamilies(CSSValueList& family)
{
ASSERT(family.length());
RefPtr oldFamily = std::exchange(m_families, &family);
mutableProperties().setProperty(CSSPropertyFontFamily, family);
iterateClients(m_clients, [&](CSSFontFaceClient& client) {
client.fontPropertyChanged(*this, oldFamily.get());
});
}
FontFace* CSSFontFace::existingWrapper()
{
return m_wrapper.get();
}
static FontSelectionRange calculateWeightRange(CSSValue& value)
{
if (auto* valueList = dynamicDowncast<CSSValueList>(value)) {
ASSERT(valueList->length() == 2);
if (valueList->length() != 2)
return { normalWeightValue(), normalWeightValue() };
ASSERT(valueList->item(0)->isPrimitiveValue());
ASSERT(valueList->item(1)->isPrimitiveValue());
auto& value0 = downcast<CSSPrimitiveValue>(*valueList->item(0));
auto& value1 = downcast<CSSPrimitiveValue>(*valueList->item(1));
auto result0 = Style::fontWeightFromCSSValueDeprecated(value0);
auto result1 = Style::fontWeightFromCSSValueDeprecated(value1);
return { result0, result1 };
}
auto& primitiveValue = downcast<CSSPrimitiveValue>(value);
FontSelectionValue result = Style::fontWeightFromCSSValueDeprecated(primitiveValue);
return { result, result };
}
void CSSFontFace::setWeight(CSSValue& weight)
{
mutableProperties().setProperty(CSSPropertyFontWeight, weight);
auto range = calculateWeightRange(weight);
if (m_fontSelectionCapabilities.weight == range)
return;
m_fontSelectionCapabilities.weight = range;
iterateClients(m_clients, [&](CSSFontFaceClient& client) {
client.fontPropertyChanged(*this);
});
}
static FontSelectionRange calculateWidthRange(CSSValue& value)
{
if (auto* valueList = dynamicDowncast<CSSValueList>(value)) {
ASSERT(valueList->length() == 2);
if (valueList->length() != 2)
return { normalWidthValue(), normalWidthValue() };
ASSERT(valueList->item(0)->isPrimitiveValue());
ASSERT(valueList->item(1)->isPrimitiveValue());
auto& value0 = downcast<CSSPrimitiveValue>(*valueList->item(0));
auto& value1 = downcast<CSSPrimitiveValue>(*valueList->item(1));
auto result0 = Style::fontStretchFromCSSValueDeprecated(value0);
auto result1 = Style::fontStretchFromCSSValueDeprecated(value1);
return { result0, result1 };
}
const auto& primitiveValue = downcast<CSSPrimitiveValue>(value);
FontSelectionValue result = Style::fontStretchFromCSSValueDeprecated(primitiveValue);
return { result, result };
}
void CSSFontFace::setWidth(CSSValue& style)
{
mutableProperties().setProperty(CSSPropertyFontWidth, style);
auto range = calculateWidthRange(style);
if (m_fontSelectionCapabilities.width == range)
return;
m_fontSelectionCapabilities.width = range;
iterateClients(m_clients, [&](CSSFontFaceClient& client) {
client.fontPropertyChanged(*this);
});
}
static FontSelectionRange calculateItalicRange(CSSValue& value)
{
auto* rangeValue = dynamicDowncast<CSSFontStyleRangeValue>(value);
if (!rangeValue)
return FontSelectionRange { Style::fontStyleFromCSSValueDeprecated(value).value_or(normalItalicValue()) };
auto keyword = rangeValue->fontStyleValue->valueID();
if (!rangeValue->obliqueValues) {
if (keyword == CSSValueNormal)
return FontSelectionRange { normalItalicValue() };
ASSERT(keyword == CSSValueItalic || keyword == CSSValueOblique);
return FontSelectionRange { italicValue() };
}
ASSERT(keyword == CSSValueOblique);
auto length = rangeValue->obliqueValues->length();
ASSERT(length == 1 || length == 2);
auto angleAtIndex = [&] (size_t index) {
return Style::fontStyleAngleFromCSSValueDeprecated(*rangeValue->obliqueValues->itemWithoutBoundsCheck(index));
};
if (length == 1)
return FontSelectionRange { angleAtIndex(0) };
return { angleAtIndex(0), angleAtIndex(1) };
}
void CSSFontFace::setStyle(CSSValue& style)
{
mutableProperties().setProperty(CSSPropertyFontStyle, style);
auto range = calculateItalicRange(style);
if (m_fontSelectionCapabilities.slope == range)
return;
m_fontSelectionCapabilities.slope = range;
iterateClients(m_clients, [&](CSSFontFaceClient& client) {
client.fontPropertyChanged(*this);
});
}
void CSSFontFace::setUnicodeRange(CSSValueList& list)
{
mutableProperties().setProperty(CSSPropertyUnicodeRange, list);
auto ranges = WTF::map(list, [](auto& rangeValue) {
auto& range = downcast<CSSUnicodeRangeValue>(rangeValue);
return UnicodeRange { range.from(), range.to() };
});
if (ranges == m_ranges)
return;
m_ranges = WTFMove(ranges);
iterateClients(m_clients, [&](CSSFontFaceClient& client) {
client.fontPropertyChanged(*this);
});
}
void CSSFontFace::setFeatureSettings(CSSValue& featureSettings)
{
// Can only call this with a primitive value of normal, or a value list containing font feature values.
ASSERT(is<CSSPrimitiveValue>(featureSettings) || is<CSSValueList>(featureSettings));
mutableProperties().setProperty(CSSPropertyFontFeatureSettings, featureSettings);
FontFeatureSettings settings;
if (auto* list = dynamicDowncast<CSSValueList>(featureSettings)) {
for (auto& rangeValue : *list) {
auto& feature = downcast<CSSFontFeatureValue>(rangeValue);
settings.insert({ feature.tag(), feature.value().resolveAsIntegerDeprecated() });
}
}
if (m_featureSettings == settings)
return;
m_featureSettings = WTFMove(settings);
iterateClients(m_clients, [&](CSSFontFaceClient& client) {
client.fontPropertyChanged(*this);
});
}
void CSSFontFace::setSizeAdjust(CSSValue& value)
{
mutableProperties().setProperty(CSSPropertySizeAdjust, value);
auto& sizeAdjustValue = downcast<CSSPrimitiveValue>(value);
auto sizeAdjust = sizeAdjustValue.resolveAsPercentageDeprecated<float>() / 100;
if (m_sizeAdjust == sizeAdjust)
return;
m_sizeAdjust = sizeAdjust;
iterateClients(m_clients, [&](CSSFontFaceClient& client) {
client.fontPropertyChanged(*this);
});
}
void CSSFontFace::setDisplay(CSSValue& loadingBehaviorValue)
{
mutableProperties().setProperty(CSSPropertyFontDisplay, loadingBehaviorValue);
auto loadingBehavior = fromCSSValue<FontLoadingBehavior>(loadingBehaviorValue);
if (m_loadingBehavior == loadingBehavior)
return;
m_loadingBehavior = loadingBehavior;
iterateClients(m_clients, [&](CSSFontFaceClient& client) {
client.fontPropertyChanged(*this);
});
}
String CSSFontFace::family() const
{
// Code to extract the name of the first family is needed because we incorrectly use a list of families.
// FIXME: Consider switching to getPropertyValue after https://bugs.webkit.org/show_bug.cgi?id=196381 is fixed.
auto family = properties().getPropertyCSSValue(CSSPropertyFontFamily);
auto familyList = dynamicDowncast<CSSValueList>(family.get());
if (!familyList)
return { };
auto firstFamily = dynamicDowncast<CSSPrimitiveValue>(familyList->item(0));
if (!firstFamily || !firstFamily->isFontFamily())
return { };
return firstFamily->stringValue();
}
String CSSFontFace::style() const
{
return properties().getPropertyValue(CSSPropertyFontStyle);
}
String CSSFontFace::weight() const
{
return properties().getPropertyValue(CSSPropertyFontWeight);
}
String CSSFontFace::width() const
{
return properties().getPropertyValue(CSSPropertyFontWidth);
}
String CSSFontFace::unicodeRange() const
{
return properties().getPropertyValue(CSSPropertyUnicodeRange);
}
String CSSFontFace::featureSettings() const
{
return properties().getPropertyValue(CSSPropertyFontFeatureSettings);
}
String CSSFontFace::sizeAdjust() const
{
return properties().getPropertyValue(CSSPropertySizeAdjust);
}
String CSSFontFace::display() const
{
return properties().getPropertyValue(CSSPropertyFontDisplay);
}
RefPtr<CSSValueList> CSSFontFace::families() const
{
return m_families;
}
bool CSSFontFace::rangesMatchCodePoint(char32_t character) const
{
if (m_ranges.isEmpty())
return true;
for (auto& range : m_ranges) {
if (range.from <= character && character <= range.to)
return true;
}
return false;
}
void CSSFontFace::fontLoadEventOccurred()
{
// If the font is already in the cache, CSSFontFaceSource may report it's loaded before it is added here as a source.
// Let's not pump the state machine until we've got all our sources. font() and load() are smart enough to act correctly
// when a source is failed or succeeded before we have asked it to load.
if (m_sourcesPopulated)
pump(ExternalResourceDownloadPolicy::Forbid);
iterateClients(m_clients, [&](CSSFontFaceClient& client) {
client.fontLoaded(*this);
});
}
void CSSFontFace::timeoutFired()
{
switch (status()) {
case Status::Loading:
setStatus(Status::TimedOut);
break;
case Status::TimedOut:
// Cancelling the network request here could lead to a situation where a site's font never gets
// shown as the user navigates around to different pages on the site. This would occur if the
// download always takes longer than the timeout (even though the user may spend substantial time
// on each page). Therefore, we shouldn't cancel the network request here, but should use the
// loading infrastructure's timeout policies instead.
setStatus(Status::Failure);
break;
default:
ASSERT_NOT_REACHED();
break;
}
fontLoadEventOccurred();
}
Document* CSSFontFace::document()
{
if (m_wrapper)
return dynamicDowncast<Document>(m_wrapper->scriptExecutionContext());
return nullptr;
}
bool CSSFontFace::computeFailureState() const
{
if (status() == Status::Failure)
return true;
for (auto& source : m_sources) {
if (source->status() != CSSFontFaceSource::Status::Failure)
return false;
}
return true;
}
void CSSFontFace::addClient(CSSFontFaceClient& client)
{
m_clients.add(client);
}
void CSSFontFace::removeClient(CSSFontFaceClient& client)
{
ASSERT(m_clients.contains(client));
m_clients.remove(client);
}
void CSSFontFace::initializeWrapper()
{
switch (m_status) {
case Status::Pending:
break;
case Status::Loading:
m_wrapper->fontStateChanged(*this, Status::Pending, Status::Loading);
break;
case Status::TimedOut:
m_wrapper->fontStateChanged(*this, Status::Pending, Status::Loading);
m_wrapper->fontStateChanged(*this, Status::Loading, Status::TimedOut);
break;
case Status::Success:
m_wrapper->fontStateChanged(*this, Status::Pending, Status::Loading);
m_wrapper->fontStateChanged(*this, Status::Pending, Status::Success);
break;
case Status::Failure:
m_wrapper->fontStateChanged(*this, Status::Pending, Status::Loading);
m_wrapper->fontStateChanged(*this, Status::Pending, Status::Failure);
break;
}
m_mayBePurged = false;
}
Ref<FontFace> CSSFontFace::wrapper(ScriptExecutionContext* context)
{
if (m_wrapper) {
ASSERT(m_wrapper->scriptExecutionContext() == context);
return *m_wrapper.get();
}
auto wrapper = FontFace::create(context, *this);
m_wrapper = wrapper;
initializeWrapper();
return wrapper;
}
void CSSFontFace::setWrapper(FontFace& newWrapper)
{
m_wrapper = newWrapper;
initializeWrapper();
}
void CSSFontFace::adoptSource(std::unique_ptr<CSSFontFaceSource>&& source)
{
m_sources.append(WTFMove(source));
// We should never add sources in the middle of loading.
ASSERT(!m_sourcesPopulated);
}
auto CSSFontFace::fontLoadTiming() const -> FontLoadTiming
{
switch (m_fontLoadTimingOverride) {
case FontLoadTimingOverride::None:
switch (m_loadingBehavior) {
case FontLoadingBehavior::Auto:
case FontLoadingBehavior::Block:
return { 3_s, Seconds::infinity() };
case FontLoadingBehavior::Swap:
return { 0_s, Seconds::infinity() };
case FontLoadingBehavior::Fallback:
return { 0.1_s, 3_s };
case FontLoadingBehavior::Optional:
return { 0.1_s, 0_s };
}
RELEASE_ASSERT_NOT_REACHED();
case FontLoadTimingOverride::Block:
return { Seconds::infinity(), 0_s };
case FontLoadTimingOverride::Swap:
return { 0_s, Seconds::infinity() };
case FontLoadTimingOverride::Failure:
return { 0_s, 0_s };
}
RELEASE_ASSERT_NOT_REACHED();
}
void CSSFontFace::setStatus(Status newStatus)
{
switch (newStatus) {
case Status::Pending:
ASSERT_NOT_REACHED();
break;
case Status::Loading:
ASSERT(m_status == Status::Pending);
break;
case Status::TimedOut:
ASSERT(m_status == Status::Loading);
break;
case Status::Success:
ASSERT(m_status == Status::Loading || m_status == Status::TimedOut);
break;
case Status::Failure:
ASSERT(m_status == Status::Loading || m_status == Status::TimedOut);
break;
}
iterateClients(m_clients, [&](CSSFontFaceClient& client) {
client.fontStateChanged(*this, m_status, newStatus);
});
m_status = newStatus;
Seconds blockPeriodTimeout;
Seconds swapPeriodTimeout;
auto timeouts = fontLoadTiming();
blockPeriodTimeout = timeouts.blockPeriod;
swapPeriodTimeout = timeouts.swapPeriod;
// Transfer across 0-delay timers synchronously. Layouts/script may
// take arbitrarily long time, and we shouldn't be in a 0-duration
// state for an arbitrarily long time. Also it's necessary for
// testing so we don't have a race with the font load.
switch (newStatus) {
case Status::Pending:
ASSERT_NOT_REACHED();
break;
case Status::Loading:
if (blockPeriodTimeout == 0_s)
setStatus(Status::TimedOut);
else if (std::isfinite(blockPeriodTimeout.value()))
m_timeoutTimer.startOneShot(blockPeriodTimeout);
break;
case Status::TimedOut:
if (swapPeriodTimeout == 0_s)
setStatus(Status::Failure);
else if (std::isfinite(swapPeriodTimeout.value()))
m_timeoutTimer.startOneShot(swapPeriodTimeout);
break;
case Status::Success:
case Status::Failure:
m_timeoutTimer.stop();
break;
}
}
void CSSFontFace::fontLoaded(CSSFontFaceSource&)
{
Ref<CSSFontFace> protectedThis(*this);
fontLoadEventOccurred();
}
void CSSFontFace::opportunisticallyStartFontDataURLLoading()
{
// We don't want to go crazy here and blow the cache. Usually these data URLs are the first item in the src: list, so let's just check that one.
if (!m_sources.isEmpty())
m_sources[0]->opportunisticallyStartFontDataURLLoading();
}
size_t CSSFontFace::pump(ExternalResourceDownloadPolicy policy)
{
if (status() == Status::Failure)
return 0;
size_t i;
for (i = 0; i < m_sources.size(); ++i) {
auto& source = m_sources[i];
if (source->status() == CSSFontFaceSource::Status::Pending) {
ASSERT(m_status == Status::Pending || m_status == Status::Loading || m_status == Status::TimedOut);
// This is a little tricky. After calling CSSFontFace::font(Forbid), a font must never fail later in
// this turn of the runloop because the return value of CSSFontFace::font() shouldn't get nulled out
// from under an existing FontRanges object. Remote fonts are all downloaded asynchronously, so this
// isn't a problem for them because CSSFontFace::font() will always return the interstitial font.
// However, local fonts may synchronously fail when you call load() on them. Therefore, we have to call
// load() here in order to guarantee that, if the font synchronously fails, it happens now during the
// first call to CSSFontFace::font() and the FontRanges object sees a consistent view of the
// CSSFontFace. This means we eagerly create some internal font objects when they may not be needed,
// but it seems that this behavior is a requirement of the design of FontRanges. FIXME: Perhaps rethink
// this design.
if (policy == ExternalResourceDownloadPolicy::Allow || !source->requiresExternalResource()) {
if (policy == ExternalResourceDownloadPolicy::Allow && m_status == Status::Pending)
setStatus(Status::Loading);
source->load(document());
}
}
switch (source->status()) {
case CSSFontFaceSource::Status::Pending:
ASSERT(policy == ExternalResourceDownloadPolicy::Forbid);
return i;
case CSSFontFaceSource::Status::Loading:
ASSERT(m_status == Status::Pending || m_status == Status::Loading || m_status == Status::TimedOut || m_status == Status::Failure);
if (policy == ExternalResourceDownloadPolicy::Allow && m_status == Status::Pending)
setStatus(Status::Loading);
return i;
case CSSFontFaceSource::Status::Success:
ASSERT(m_status == Status::Pending || m_status == Status::Loading || m_status == Status::TimedOut || m_status == Status::Success || m_status == Status::Failure);
if (m_status == Status::Pending)
setStatus(Status::Loading);
if (m_status == Status::Loading || m_status == Status::TimedOut)
setStatus(Status::Success);
return i;
case CSSFontFaceSource::Status::Failure:
if (policy == ExternalResourceDownloadPolicy::Allow && m_status == Status::Pending)
setStatus(Status::Loading);
break;
}
}
if (m_sources.isEmpty() && m_status == Status::Pending)
setStatus(Status::Loading);
if (m_status == Status::Loading || m_status == Status::TimedOut)
setStatus(Status::Failure);
return m_sources.size();
}
void CSSFontFace::load()
{
pump(ExternalResourceDownloadPolicy::Allow);
}
static Font::Visibility visibility(CSSFontFace::Status status, CSSFontFace::FontLoadTiming timing)
{
switch (status) {
case CSSFontFace::Status::Pending:
return timing.blockPeriod == 0_s ? Font::Visibility::Visible : Font::Visibility::Invisible;
case CSSFontFace::Status::Loading:
return Font::Visibility::Invisible;
case CSSFontFace::Status::TimedOut:
case CSSFontFace::Status::Failure:
case CSSFontFace::Status::Success:
default:
return Font::Visibility::Visible;
}
}
RefPtr<Font> CSSFontFace::font(const FontDescription& fontDescription, bool syntheticBold, bool syntheticItalic, ExternalResourceDownloadPolicy policy, const FontPaletteValues& fontPaletteValues, RefPtr<FontFeatureValues> fontFeatureValues)
{
if (computeFailureState())
return nullptr;
Ref<CSSFontFace> protectedThis(*this);
// Our status is derived from the first non-failed source. However, this source may
// return null from font(), which means we need to continue looping through the remainder
// of the sources to try to find a font to use. These subsequent tries should not affect
// our own state, though.
size_t startIndex = pump(policy);
if (computeFailureState())
return nullptr;
for (size_t i = startIndex; i < m_sources.size(); ++i) {
auto& source = m_sources[i];
if (source->status() == CSSFontFaceSource::Status::Pending && (policy == ExternalResourceDownloadPolicy::Allow || !source->requiresExternalResource()))
source->load(document());
switch (source->status()) {
case CSSFontFaceSource::Status::Pending:
case CSSFontFaceSource::Status::Loading: {
Font::Visibility visibility = WebCore::visibility(status(), fontLoadTiming());
return Font::create(FontCache::forCurrentThread().lastResortFallbackFont(fontDescription)->platformData(), Font::Origin::Local, Font::IsInterstitial::Yes, visibility);
}
case CSSFontFaceSource::Status::Success: {
FontCreationContext fontCreationContext { m_featureSettings, m_fontSelectionCapabilities, fontPaletteValues, fontFeatureValues, m_sizeAdjust };
if (auto result = source->font(fontDescription, syntheticBold, syntheticItalic, fontCreationContext))
return result;
break;
}
case CSSFontFaceSource::Status::Failure:
break;
}
}
return nullptr;
}
bool CSSFontFace::purgeable() const
{
return cssConnection() && m_mayBePurged;
}
void CSSFontFace::updateStyleIfNeeded()
{
iterateClients(m_clients, [&](CSSFontFaceClient& client) {
client.updateStyleIfNeeded(*this);
});
}
bool CSSFontFace::hasSVGFontFaceSource() const
{
size_t size = m_sources.size();
for (size_t i = 0; i < size; i++) {
if (m_sources[i]->isSVGFontFaceSource())
return true;
}
return false;
}
void CSSFontFace::setErrorState()
{
switch (m_status) {
case Status::Pending:
setStatus(Status::Loading);
break;
case Status::Success:
ASSERT_NOT_REACHED();
break;
case Status::Failure:
return;
default:
break;
}
setStatus(Status::Failure);
}
}
|