FontCascadeCoreText.cpp [plain text]
#include "config.h"
#include "FontCascade.h"
#include "ComplexTextController.h"
#include "DashArray.h"
#include "Font.h"
#include "GlyphBuffer.h"
#include "GraphicsContext.h"
#include "LayoutRect.h"
#include "Logging.h"
#include <pal/spi/cg/CoreGraphicsSPI.h>
#include <wtf/MathExtras.h>
#if PLATFORM(COCOA)
#include <pal/spi/cocoa/CoreTextSPI.h>
#else
#include <pal/spi/win/CoreTextSPIWin.h>
#endif
namespace WebCore {
static const AffineTransform& rotateLeftTransform()
{
static AffineTransform result(0, -1, 1, 0, 0, 0);
return result;
}
AffineTransform computeOverallTextMatrix(const Font& font)
{
auto& platformData = font.platformData();
AffineTransform result;
if (!platformData.isColorBitmapFont())
result = CTFontGetMatrix(platformData.ctFont());
result.setB(-result.b());
result.setD(-result.d());
if (platformData.syntheticOblique()) {
float obliqueSkew = tanf(deg2rad(FontCascade::syntheticObliqueAngle()));
if (platformData.orientation() == FontOrientation::Vertical) {
if (font.isTextOrientationFallback())
result = AffineTransform(1, obliqueSkew, 0, 1, 0, 0) * result;
else
result = AffineTransform(1, -obliqueSkew, 0, 1, 0, 0) * result;
} else
result = AffineTransform(1, 0, -obliqueSkew, 1, 0, 0) * result;
}
result.setE(0);
result.setF(0);
return result;
}
AffineTransform computeVerticalTextMatrix(const Font& font, const AffineTransform& previousTextMatrix)
{
ASSERT_UNUSED(font, font.platformData().orientation() == FontOrientation::Vertical);
return rotateLeftTransform() * previousTextMatrix;
}
#if !PLATFORM(WIN)
bool FontCascade::isSubpixelAntialiasingAvailable()
{
#if HAVE(CG_FONT_RENDERING_GET_FONT_SMOOTHING_DISABLED)
static bool subpixelAntialiasingEnabled;
static std::once_flag onceFlag;
std::call_once(onceFlag, [&]() {
ALLOW_DEPRECATED_DECLARATIONS_BEGIN
subpixelAntialiasingEnabled = !CGFontRenderingGetFontSmoothingDisabled();
ALLOW_DEPRECATED_DECLARATIONS_END
});
return subpixelAntialiasingEnabled;
#else
return false;
#endif
}
void fillVectorWithHorizontalGlyphPositions(Vector<CGPoint, 256>& positions, CGContextRef context, const CGSize* advances, unsigned count, const FloatPoint& point)
{
CGAffineTransform matrix = CGAffineTransformInvert(CGContextGetTextMatrix(context));
positions[0] = CGPointApplyAffineTransform(point, matrix);
for (unsigned i = 1; i < count; ++i) {
CGSize advance = CGSizeApplyAffineTransform(advances[i - 1], matrix);
positions[i].x = positions[i - 1].x + advance.width;
positions[i].y = positions[i - 1].y + advance.height;
}
}
static void fillVectorWithVerticalGlyphPositions(Vector<CGPoint, 256>& positions, CGContextRef context, const CGSize* translations, const CGSize* advances, unsigned count, const FloatPoint& point, float ascentDelta)
{
CGAffineTransform transform = CGAffineTransformInvert(CGContextGetTextMatrix(context));
auto position = CGPointMake(point.x(), point.y() + ascentDelta);
for (unsigned i = 0; i < count; ++i) {
CGSize translation = CGSizeApplyAffineTransform(translations[i], rotateLeftTransform());
positions[i] = CGPointApplyAffineTransform(CGPointMake(position.x - translation.width, position.y + translation.height), transform);
position.x += advances[i].width;
position.y += advances[i].height;
}
}
static inline bool shouldUseLetterpressEffect(const GraphicsContext& context)
{
#if ENABLE(LETTERPRESS)
return context.textDrawingMode().contains(TextDrawingMode::Letterpress);
#else
UNUSED_PARAM(context);
return false;
#endif
}
static void showGlyphsWithAdvances(const FloatPoint& point, const Font& font, CGContextRef context, const CGGlyph* glyphs, const CGSize* advances, unsigned count, const AffineTransform& textMatrix)
{
if (!count)
return;
const FontPlatformData& platformData = font.platformData();
Vector<CGPoint, 256> positions(count);
if (platformData.orientation() == FontOrientation::Vertical) {
ScopedTextMatrix savedMatrix(computeVerticalTextMatrix(font, textMatrix), context);
Vector<CGSize, 256> translations(count);
CTFontGetVerticalTranslationsForGlyphs(platformData.ctFont(), glyphs, translations.data(), count);
auto ascentDelta = font.fontMetrics().floatAscent(IdeographicBaseline) - font.fontMetrics().floatAscent();
fillVectorWithVerticalGlyphPositions(positions, context, translations.data(), advances, count, point, ascentDelta);
CTFontDrawGlyphs(platformData.ctFont(), glyphs, positions.data(), count, context);
} else {
fillVectorWithHorizontalGlyphPositions(positions, context, advances, count, point);
CTFontDrawGlyphs(platformData.ctFont(), glyphs, positions.data(), count, context);
}
}
static void setCGFontRenderingMode(GraphicsContext& context)
{
CGContextRef cgContext = context.platformContext();
CGContextSetShouldAntialiasFonts(cgContext, true);
CGAffineTransform contextTransform = CGContextGetCTM(cgContext);
bool isTranslationOrIntegralScale = WTF::isIntegral(contextTransform.a) && WTF::isIntegral(contextTransform.d) && contextTransform.b == 0.f && contextTransform.c == 0.f;
bool isRotated = ((contextTransform.b || contextTransform.c) && (contextTransform.a || contextTransform.d));
bool doSubpixelQuantization = isTranslationOrIntegralScale || (!isRotated && context.shouldSubpixelQuantizeFonts());
CGContextSetShouldSubpixelPositionFonts(cgContext, true);
CGContextSetShouldSubpixelQuantizeFonts(cgContext, doSubpixelQuantization);
}
void FontCascade::drawGlyphs(GraphicsContext& context, const Font& font, const GlyphBuffer& glyphBuffer, unsigned from, unsigned numGlyphs, const FloatPoint& anchorPoint, FontSmoothingMode smoothingMode)
{
const auto& platformData = font.platformData();
if (!platformData.size())
return;
CGContextRef cgContext = context.platformContext();
bool shouldAntialias = true;
bool shouldSmoothFonts = true;
if (!font.allowsAntialiasing())
smoothingMode = FontSmoothingMode::NoSmoothing;
switch (smoothingMode) {
case FontSmoothingMode::Antialiased:
shouldSmoothFonts = false;
break;
case FontSmoothingMode::AutoSmoothing:
case FontSmoothingMode::SubpixelAntialiased:
shouldAntialias = true;
break;
case FontSmoothingMode::NoSmoothing:
shouldAntialias = false;
shouldSmoothFonts = false;
break;
}
if (!shouldUseSmoothing())
shouldSmoothFonts = false;
#if !PLATFORM(IOS_FAMILY)
bool originalShouldUseFontSmoothing = CGContextGetShouldSmoothFonts(cgContext);
if (shouldSmoothFonts != originalShouldUseFontSmoothing)
CGContextSetShouldSmoothFonts(cgContext, shouldSmoothFonts);
#endif
bool originalShouldAntialias = CGContextGetShouldAntialias(cgContext);
if (shouldAntialias != originalShouldAntialias)
CGContextSetShouldAntialias(cgContext, shouldAntialias);
bool useLetterpressEffect = shouldUseLetterpressEffect(context);
UNUSED_VARIABLE(useLetterpressEffect);
FloatPoint point = anchorPoint;
auto textMatrix = computeOverallTextMatrix(font);
ScopedTextMatrix restorer(textMatrix, cgContext);
setCGFontRenderingMode(context);
CGContextSetFontSize(cgContext, platformData.size());
FloatSize shadowOffset;
float shadowBlur;
Color shadowColor;
context.getShadow(shadowOffset, shadowBlur, shadowColor);
AffineTransform contextCTM = context.getCTM();
float syntheticBoldOffset = font.syntheticBoldOffset();
if (syntheticBoldOffset && !contextCTM.isIdentityOrTranslationOrFlipped()) {
FloatSize horizontalUnitSizeInDevicePixels = contextCTM.mapSize(FloatSize(1, 0));
float horizontalUnitLengthInDevicePixels = sqrtf(horizontalUnitSizeInDevicePixels.width() * horizontalUnitSizeInDevicePixels.width() + horizontalUnitSizeInDevicePixels.height() * horizontalUnitSizeInDevicePixels.height());
if (horizontalUnitLengthInDevicePixels) {
syntheticBoldOffset = std::min(syntheticBoldOffset, syntheticBoldOffset / horizontalUnitLengthInDevicePixels);
}
};
bool hasSimpleShadow = context.textDrawingMode() == TextDrawingMode::Fill && shadowColor.isValid() && !shadowBlur && !platformData.isColorBitmapFont() && (!context.shadowsIgnoreTransforms() || contextCTM.isIdentityOrTranslationOrFlipped()) && !context.isInTransparencyLayer();
if (hasSimpleShadow) {
context.clearShadow();
Color fillColor = context.fillColor();
Color shadowFillColor = shadowColor.colorWithAlphaMultipliedBy(fillColor.alphaAsFloat());
context.setFillColor(shadowFillColor);
float shadowTextX = point.x() + shadowOffset.width();
float shadowTextY = point.y() + shadowOffset.height() * (context.shadowsIgnoreTransforms() ? -1 : 1);
showGlyphsWithAdvances(FloatPoint(shadowTextX, shadowTextY), font, cgContext, glyphBuffer.glyphs(from), glyphBuffer.advances(from), numGlyphs, textMatrix);
if (syntheticBoldOffset)
showGlyphsWithAdvances(FloatPoint(shadowTextX + syntheticBoldOffset, shadowTextY), font, cgContext, glyphBuffer.glyphs(from), glyphBuffer.advances(from), numGlyphs, textMatrix);
context.setFillColor(fillColor);
}
#if ENABLE(LETTERPRESS)
if (useLetterpressEffect)
showLetterpressedGlyphsWithAdvances(point, font, context, glyphBuffer.glyphs(from), glyphBuffer.advances(from), numGlyphs);
else
#endif
{
showGlyphsWithAdvances(point, font, cgContext, glyphBuffer.glyphs(from), glyphBuffer.advances(from), numGlyphs, textMatrix);
}
if (syntheticBoldOffset)
showGlyphsWithAdvances(FloatPoint(point.x() + syntheticBoldOffset, point.y()), font, cgContext, glyphBuffer.glyphs(from), glyphBuffer.advances(from), numGlyphs, textMatrix);
if (hasSimpleShadow)
context.setShadow(shadowOffset, shadowBlur, shadowColor);
#if !PLATFORM(IOS_FAMILY)
if (shouldSmoothFonts != originalShouldUseFontSmoothing)
CGContextSetShouldSmoothFonts(cgContext, originalShouldUseFontSmoothing);
#endif
if (shouldAntialias != originalShouldAntialias)
CGContextSetShouldAntialias(cgContext, originalShouldAntialias);
}
bool FontCascade::primaryFontIsSystemFont() const
{
const auto& fontData = primaryFont();
return isSystemFont(fontData.platformData().ctFont());
}
const Font* FontCascade::fontForCombiningCharacterSequence(const UChar* characters, size_t length) const
{
UChar32 baseCharacter;
size_t baseCharacterLength = 0;
U16_NEXT(characters, baseCharacterLength, length, baseCharacter);
GlyphData baseCharacterGlyphData = glyphDataForCharacter(baseCharacter, false, NormalVariant);
if (!baseCharacterGlyphData.glyph)
return nullptr;
if (length == baseCharacterLength)
return baseCharacterGlyphData.font;
bool triedBaseCharacterFont = false;
for (unsigned i = 0; !fallbackRangesAt(i).isNull(); ++i) {
const Font* font = fallbackRangesAt(i).fontForCharacter(baseCharacter);
if (!font)
continue;
#if PLATFORM(IOS_FAMILY)
if (baseCharacter >= 0x0600 && baseCharacter <= 0x06ff && font->shouldNotBeUsedForArabic())
continue;
#endif
if (font->platformData().orientation() == FontOrientation::Vertical) {
if (isCJKIdeographOrSymbol(baseCharacter)) {
if (!font->hasVerticalGlyphs())
font = &font->brokenIdeographFont();
} else if (m_fontDescription.nonCJKGlyphOrientation() == NonCJKGlyphOrientation::Mixed) {
const Font& verticalRightFont = font->verticalRightOrientationFont();
Glyph verticalRightGlyph = verticalRightFont.glyphForCharacter(baseCharacter);
if (verticalRightGlyph == baseCharacterGlyphData.glyph)
font = &verticalRightFont;
} else {
const Font& uprightFont = font->uprightOrientationFont();
Glyph uprightGlyph = uprightFont.glyphForCharacter(baseCharacter);
if (uprightGlyph != baseCharacterGlyphData.glyph)
font = &uprightFont;
}
}
if (font == baseCharacterGlyphData.font)
triedBaseCharacterFont = true;
if (font->canRenderCombiningCharacterSequence(characters, length))
return font;
}
if (!triedBaseCharacterFont && baseCharacterGlyphData.font && baseCharacterGlyphData.font->canRenderCombiningCharacterSequence(characters, length))
return baseCharacterGlyphData.font;
return Font::systemFallback();
}
#endif
}