This source file includes following definitions.
- m_customFontData
- m_customFontData
- initCharWidths
- platformGlyphInit
- fontDataForCharacter
- glyphForCharacter
- isSegmented
- verticalRightOrientationFontData
- uprightOrientationFontData
- smallCapsFontData
- emphasisMarkFontData
- brokenIdeographFontData
- description
- create
- createScaledFontData
#include "config.h"
#include "platform/fonts/SimpleFontData.h"
#include "wtf/MathExtras.h"
using namespace std;
namespace WebCore {
const float smallCapsFontSizeMultiplier = 0.7f;
const float emphasisMarkFontSizeMultiplier = 0.5f;
SimpleFontData::SimpleFontData(const FontPlatformData& platformData, PassRefPtr<CustomFontData> customData, bool isTextOrientationFallback)
: m_maxCharWidth(-1)
, m_avgCharWidth(-1)
, m_platformData(platformData)
, m_treatAsFixedPitch(false)
, m_isTextOrientationFallback(isTextOrientationFallback)
, m_isBrokenIdeographFallback(false)
#if ENABLE(OPENTYPE_VERTICAL)
, m_verticalData(nullptr)
#endif
, m_hasVerticalGlyphs(false)
, m_customFontData(customData)
{
platformInit();
platformGlyphInit();
platformCharWidthInit();
#if ENABLE(OPENTYPE_VERTICAL)
if (platformData.orientation() == Vertical && !isTextOrientationFallback) {
m_verticalData = platformData.verticalData();
m_hasVerticalGlyphs = m_verticalData.get() && m_verticalData->hasVerticalMetrics();
}
#endif
}
SimpleFontData::SimpleFontData(PassRefPtr<CustomFontData> customData, float fontSize, bool syntheticBold, bool syntheticItalic)
: m_platformData(FontPlatformData(fontSize, syntheticBold, syntheticItalic))
, m_treatAsFixedPitch(false)
, m_isTextOrientationFallback(false)
, m_isBrokenIdeographFallback(false)
#if ENABLE(OPENTYPE_VERTICAL)
, m_verticalData(nullptr)
#endif
, m_hasVerticalGlyphs(false)
, m_customFontData(customData)
{
if (m_customFontData)
m_customFontData->initializeFontData(this, fontSize);
}
void SimpleFontData::initCharWidths()
{
GlyphPage* glyphPageZero = GlyphPageTreeNode::getRootChild(this, 0)->page();
if (m_avgCharWidth <= 0.f && glyphPageZero) {
static const UChar32 digitZeroChar = '0';
Glyph digitZeroGlyph = glyphPageZero->glyphForCharacter(digitZeroChar);
if (digitZeroGlyph)
m_avgCharWidth = widthForGlyph(digitZeroGlyph);
}
if (m_avgCharWidth <= 0.f)
m_avgCharWidth = m_fontMetrics.xHeight();
if (m_maxCharWidth <= 0.f)
m_maxCharWidth = max(m_avgCharWidth, m_fontMetrics.floatAscent());
}
void SimpleFontData::platformGlyphInit()
{
GlyphPage* glyphPageZero = GlyphPageTreeNode::getRootChild(this, 0)->page();
if (!glyphPageZero) {
WTF_LOG_ERROR("Failed to get glyph page zero.");
m_spaceGlyph = 0;
m_spaceWidth = 0;
m_zeroGlyph = 0;
m_adjustedSpaceWidth = 0;
determinePitch();
m_zeroWidthSpaceGlyph = 0;
m_missingGlyphData.fontData = this;
m_missingGlyphData.glyph = 0;
return;
}
m_zeroWidthSpaceGlyph = glyphPageZero->glyphForCharacter(0);
m_spaceGlyph = glyphPageZero->glyphForCharacter(' ');
float width = widthForGlyph(m_spaceGlyph);
m_spaceWidth = width;
m_zeroGlyph = glyphPageZero->glyphForCharacter('0');
m_fontMetrics.setZeroWidth(widthForGlyph(m_zeroGlyph));
determinePitch();
m_adjustedSpaceWidth = m_treatAsFixedPitch ? ceilf(width) : roundf(width);
if (m_zeroWidthSpaceGlyph == m_spaceGlyph) {
m_zeroWidthSpaceGlyph = 0;
WTF_LOG_ERROR("Font maps SPACE and ZERO WIDTH SPACE to the same glyph. Glyph width will not be overridden.");
}
m_missingGlyphData.fontData = this;
m_missingGlyphData.glyph = 0;
}
SimpleFontData::~SimpleFontData()
{
if (!isSVGFont())
platformDestroy();
if (isCustomFont())
GlyphPageTreeNode::pruneTreeCustomFontData(this);
else
GlyphPageTreeNode::pruneTreeFontData(this);
}
const SimpleFontData* SimpleFontData::fontDataForCharacter(UChar32) const
{
return this;
}
Glyph SimpleFontData::glyphForCharacter(UChar32 character) const
{
GlyphPageTreeNode* node = GlyphPageTreeNode::getRootChild(this, character / GlyphPage::size);
return node->page() ? node->page()->glyphAt(character % GlyphPage::size) : 0;
}
bool SimpleFontData::isSegmented() const
{
return false;
}
PassRefPtr<SimpleFontData> SimpleFontData::verticalRightOrientationFontData() const
{
if (!m_derivedFontData)
m_derivedFontData = DerivedFontData::create(isCustomFont());
if (!m_derivedFontData->verticalRightOrientation) {
FontPlatformData verticalRightPlatformData(m_platformData);
verticalRightPlatformData.setOrientation(Horizontal);
m_derivedFontData->verticalRightOrientation = create(verticalRightPlatformData, isCustomFont() ? CustomFontData::create(): nullptr, true);
}
return m_derivedFontData->verticalRightOrientation;
}
PassRefPtr<SimpleFontData> SimpleFontData::uprightOrientationFontData() const
{
if (!m_derivedFontData)
m_derivedFontData = DerivedFontData::create(isCustomFont());
if (!m_derivedFontData->uprightOrientation)
m_derivedFontData->uprightOrientation = create(m_platformData, isCustomFont() ? CustomFontData::create(): nullptr, true);
return m_derivedFontData->uprightOrientation;
}
PassRefPtr<SimpleFontData> SimpleFontData::smallCapsFontData(const FontDescription& fontDescription) const
{
if (!m_derivedFontData)
m_derivedFontData = DerivedFontData::create(isCustomFont());
if (!m_derivedFontData->smallCaps)
m_derivedFontData->smallCaps = createScaledFontData(fontDescription, smallCapsFontSizeMultiplier);
return m_derivedFontData->smallCaps;
}
PassRefPtr<SimpleFontData> SimpleFontData::emphasisMarkFontData(const FontDescription& fontDescription) const
{
if (!m_derivedFontData)
m_derivedFontData = DerivedFontData::create(isCustomFont());
if (!m_derivedFontData->emphasisMark)
m_derivedFontData->emphasisMark = createScaledFontData(fontDescription, emphasisMarkFontSizeMultiplier);
return m_derivedFontData->emphasisMark;
}
PassRefPtr<SimpleFontData> SimpleFontData::brokenIdeographFontData() const
{
if (!m_derivedFontData)
m_derivedFontData = DerivedFontData::create(isCustomFont());
if (!m_derivedFontData->brokenIdeograph) {
m_derivedFontData->brokenIdeograph = create(m_platformData, isCustomFont() ? CustomFontData::create(): nullptr);
m_derivedFontData->brokenIdeograph->m_isBrokenIdeographFallback = true;
}
return m_derivedFontData->brokenIdeograph;
}
#ifndef NDEBUG
String SimpleFontData::description() const
{
if (isSVGFont())
return "[SVG font]";
if (isCustomFont())
return "[custom font]";
return platformData().description();
}
#endif
PassOwnPtr<SimpleFontData::DerivedFontData> SimpleFontData::DerivedFontData::create(bool forCustomFont)
{
return adoptPtr(new DerivedFontData(forCustomFont));
}
SimpleFontData::DerivedFontData::~DerivedFontData()
{
if (!forCustomFont)
return;
if (smallCaps)
GlyphPageTreeNode::pruneTreeCustomFontData(smallCaps.get());
if (emphasisMark)
GlyphPageTreeNode::pruneTreeCustomFontData(emphasisMark.get());
if (brokenIdeograph)
GlyphPageTreeNode::pruneTreeCustomFontData(brokenIdeograph.get());
if (verticalRightOrientation)
GlyphPageTreeNode::pruneTreeCustomFontData(verticalRightOrientation.get());
if (uprightOrientation)
GlyphPageTreeNode::pruneTreeCustomFontData(uprightOrientation.get());
}
PassRefPtr<SimpleFontData> SimpleFontData::createScaledFontData(const FontDescription& fontDescription, float scaleFactor) const
{
if (isSVGFont())
return nullptr;
return platformCreateScaledFontData(fontDescription, scaleFactor);
}
}