CSSFontFaceSource.cpp [plain text]
#include "config.h"
#include "CSSFontFaceSource.h"
#include "CachedFont.h"
#include "CSSFontFace.h"
#include "CSSFontSelector.h"
#include "CachedResourceLoader.h"
#include "FontCache.h"
#include "FontDescription.h"
#include "GlyphPageTreeNode.h"
#include "SimpleFontData.h"
#if ENABLE(SVG_FONTS)
#include "FontCustomPlatformData.h"
#include "SVGFontData.h"
#include "SVGFontElement.h"
#include "SVGFontFaceElement.h"
#include "SVGNames.h"
#include "SVGURIReference.h"
#endif
namespace WebCore {
CSSFontFaceSource::CSSFontFaceSource(const String& str, CachedFont* font)
: m_string(str)
, m_font(font)
, m_face(0)
#if ENABLE(SVG_FONTS)
, m_hasExternalSVGFont(false)
#endif
{
if (m_font)
m_font->addClient(this);
}
CSSFontFaceSource::~CSSFontFaceSource()
{
if (m_font)
m_font->removeClient(this);
pruneTable();
}
void CSSFontFaceSource::pruneTable()
{
if (m_fontDataTable.isEmpty())
return;
HashMap<unsigned, SimpleFontData*>::iterator end = m_fontDataTable.end();
for (HashMap<unsigned, SimpleFontData*>::iterator it = m_fontDataTable.begin(); it != end; ++it)
GlyphPageTreeNode::pruneTreeCustomFontData(it->second);
deleteAllValues(m_fontDataTable);
m_fontDataTable.clear();
}
bool CSSFontFaceSource::isLoaded() const
{
if (m_font)
return m_font->isLoaded();
return true;
}
bool CSSFontFaceSource::isValid() const
{
if (m_font)
return !m_font->errorOccurred();
return true;
}
void CSSFontFaceSource::fontLoaded(CachedFont*)
{
pruneTable();
if (m_face)
m_face->fontLoaded(this);
}
SimpleFontData* CSSFontFaceSource::getFontData(const FontDescription& fontDescription, bool syntheticBold, bool syntheticItalic, CSSFontSelector* fontSelector)
{
if (!isValid())
return 0;
#if ENABLE(SVG_FONTS)
if (!m_font && !m_svgFontFaceElement) {
#else
if (!m_font) {
#endif
SimpleFontData* fontData = fontCache()->getCachedFontData(fontDescription, m_string);
return fontData;
}
unsigned hashKey = (fontDescription.computedPixelSize() + 1) << 6 | fontDescription.widthVariant() << 4
| (fontDescription.textOrientation() == TextOrientationUpright ? 8 : 0) | (fontDescription.orientation() == Vertical ? 4 : 0) | (syntheticBold ? 2 : 0) | (syntheticItalic ? 1 : 0);
if (SimpleFontData* cachedData = m_fontDataTable.get(hashKey))
return cachedData;
OwnPtr<SimpleFontData> fontData;
if (isLoaded()) {
if (m_font) {
#if ENABLE(SVG_FONTS)
if (m_hasExternalSVGFont) {
if (!m_font->ensureSVGFontData())
return 0;
if (!m_externalSVGFontElement)
m_externalSVGFontElement = m_font->getSVGFontById(SVGURIReference::getTarget(m_string));
if (!m_externalSVGFontElement)
return 0;
SVGFontFaceElement* fontFaceElement = 0;
for (Node* fontChild = m_externalSVGFontElement->firstChild(); fontChild; fontChild = fontChild->nextSibling()) {
if (fontChild->hasTagName(SVGNames::font_faceTag)) {
fontFaceElement = static_cast<SVGFontFaceElement*>(fontChild);
break;
}
}
if (fontFaceElement) {
if (!m_svgFontFaceElement) {
m_svgFontFaceElement = fontFaceElement;
}
fontData = adoptPtr(new SimpleFontData(adoptPtr(new SVGFontData(fontFaceElement)), fontDescription.computedPixelSize(), syntheticBold, syntheticItalic));
}
} else
#endif
{
if (!m_font->ensureCustomFontData())
return 0;
fontData = adoptPtr(new SimpleFontData(m_font->platformDataFromCustomData(fontDescription.computedPixelSize(), syntheticBold, syntheticItalic, fontDescription.orientation(),
fontDescription.textOrientation(), fontDescription.widthVariant(), fontDescription.renderingMode()), true, false));
}
} else {
#if ENABLE(SVG_FONTS)
if (m_svgFontFaceElement)
fontData = adoptPtr(new SimpleFontData(adoptPtr(new SVGFontData(m_svgFontFaceElement.get())), fontDescription.computedPixelSize(), syntheticBold, syntheticItalic));
#endif
}
} else {
if (CachedResourceLoader* cachedResourceLoader = fontSelector->cachedResourceLoader())
m_font->beginLoadIfNeeded(cachedResourceLoader);
SimpleFontData* tempData = fontCache()->getCachedFontData(fontDescription, m_string);
if (!tempData)
tempData = fontCache()->getLastResortFallbackFont(fontDescription);
fontData = adoptPtr(new SimpleFontData(tempData->platformData(), true, true));
}
SimpleFontData* fontDataRawPtr = fontData.leakPtr();
m_fontDataTable.set(hashKey, fontDataRawPtr);
return fontDataRawPtr;
}
#if ENABLE(SVG_FONTS)
SVGFontFaceElement* CSSFontFaceSource::svgFontFaceElement() const
{
return m_svgFontFaceElement.get();
}
void CSSFontFaceSource::setSVGFontFaceElement(PassRefPtr<SVGFontFaceElement> element)
{
m_svgFontFaceElement = element;
}
bool CSSFontFaceSource::isSVGFontFaceSource() const
{
return m_svgFontFaceElement || m_hasExternalSVGFont;
}
#endif
}