/platform/external/webkit/WebCore/css/CSSFontFaceSource.cpp

https://github.com/aharish/totoro-gb-opensource-update2 · C++ · 193 lines · 130 code · 27 blank · 36 comment · 29 complexity · ab4ca530821a3ab1fa24efbe1b318dd3 MD5 · raw file

  1. /*
  2. * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "CSSFontFaceSource.h"
  27. #include "CachedFont.h"
  28. #include "CSSFontFace.h"
  29. #include "CSSFontSelector.h"
  30. #include "DocLoader.h"
  31. #include "FontCache.h"
  32. #include "FontDescription.h"
  33. #include "GlyphPageTreeNode.h"
  34. #include "SimpleFontData.h"
  35. #if ENABLE(SVG_FONTS)
  36. #include "FontCustomPlatformData.h"
  37. #include "HTMLNames.h"
  38. #include "SVGFontData.h"
  39. #include "SVGFontElement.h"
  40. #include "SVGURIReference.h"
  41. #endif
  42. namespace WebCore {
  43. CSSFontFaceSource::CSSFontFaceSource(const String& str, CachedFont* font)
  44. : m_string(str)
  45. , m_font(font)
  46. , m_face(0)
  47. #if ENABLE(SVG_FONTS)
  48. , m_svgFontFaceElement(0)
  49. #endif
  50. {
  51. if (m_font)
  52. m_font->addClient(this);
  53. }
  54. CSSFontFaceSource::~CSSFontFaceSource()
  55. {
  56. if (m_font)
  57. m_font->removeClient(this);
  58. pruneTable();
  59. }
  60. void CSSFontFaceSource::pruneTable()
  61. {
  62. if (m_fontDataTable.isEmpty())
  63. return;
  64. HashMap<unsigned, SimpleFontData*>::iterator end = m_fontDataTable.end();
  65. for (HashMap<unsigned, SimpleFontData*>::iterator it = m_fontDataTable.begin(); it != end; ++it)
  66. GlyphPageTreeNode::pruneTreeCustomFontData(it->second);
  67. deleteAllValues(m_fontDataTable);
  68. m_fontDataTable.clear();
  69. }
  70. bool CSSFontFaceSource::isLoaded() const
  71. {
  72. if (m_font)
  73. return m_font->isLoaded();
  74. return true;
  75. }
  76. bool CSSFontFaceSource::isValid() const
  77. {
  78. if (m_font)
  79. return !m_font->errorOccurred();
  80. return true;
  81. }
  82. void CSSFontFaceSource::fontLoaded(CachedFont*)
  83. {
  84. pruneTable();
  85. if (m_face)
  86. m_face->fontLoaded(this);
  87. }
  88. SimpleFontData* CSSFontFaceSource::getFontData(const FontDescription& fontDescription, bool syntheticBold, bool syntheticItalic, CSSFontSelector* fontSelector)
  89. {
  90. // If the font hasn't loaded or an error occurred, then we've got nothing.
  91. if (!isValid())
  92. return 0;
  93. #if ENABLE(SVG_FONTS)
  94. if (!m_font && !m_svgFontFaceElement) {
  95. #else
  96. if (!m_font) {
  97. #endif
  98. SimpleFontData* fontData = fontCache()->getCachedFontData(fontDescription, m_string);
  99. // We're local. Just return a SimpleFontData from the normal cache.
  100. return fontData;
  101. }
  102. // See if we have a mapping in our FontData cache.
  103. unsigned hashKey = fontDescription.computedPixelSize() << 2 | (syntheticBold ? 2 : 0) | (syntheticItalic ? 1 : 0);
  104. if (SimpleFontData* cachedData = m_fontDataTable.get(hashKey))
  105. return cachedData;
  106. OwnPtr<SimpleFontData> fontData;
  107. // If we are still loading, then we let the system pick a font.
  108. if (isLoaded()) {
  109. if (m_font) {
  110. #if ENABLE(SVG_FONTS)
  111. if (m_font->isSVGFont()) {
  112. // For SVG fonts parse the external SVG document, and extract the <font> element.
  113. if (!m_font->ensureSVGFontData())
  114. return 0;
  115. if (!m_externalSVGFontElement)
  116. m_externalSVGFontElement = m_font->getSVGFontById(SVGURIReference::getTarget(m_string));
  117. if (!m_externalSVGFontElement)
  118. return 0;
  119. SVGFontFaceElement* fontFaceElement = 0;
  120. // Select first <font-face> child
  121. for (Node* fontChild = m_externalSVGFontElement->firstChild(); fontChild; fontChild = fontChild->nextSibling()) {
  122. if (fontChild->hasTagName(SVGNames::font_faceTag)) {
  123. fontFaceElement = static_cast<SVGFontFaceElement*>(fontChild);
  124. break;
  125. }
  126. }
  127. if (fontFaceElement) {
  128. if (!m_svgFontFaceElement) {
  129. // We're created using a CSS @font-face rule, that means we're not associated with a SVGFontFaceElement.
  130. // Use the imported <font-face> tag as referencing font-face element for these cases.
  131. m_svgFontFaceElement = fontFaceElement;
  132. }
  133. SVGFontData* svgFontData = new SVGFontData(fontFaceElement);
  134. fontData.set(new SimpleFontData(m_font->platformDataFromCustomData(fontDescription.computedPixelSize(), syntheticBold, syntheticItalic, fontDescription.renderingMode()), true, false, svgFontData));
  135. }
  136. } else
  137. #endif
  138. {
  139. // Create new FontPlatformData from our CGFontRef, point size and ATSFontRef.
  140. if (!m_font->ensureCustomFontData())
  141. return 0;
  142. fontData.set(new SimpleFontData(m_font->platformDataFromCustomData(fontDescription.computedPixelSize(), syntheticBold, syntheticItalic, fontDescription.renderingMode()), true, false));
  143. }
  144. } else {
  145. #if ENABLE(SVG_FONTS)
  146. // In-Document SVG Fonts
  147. if (m_svgFontFaceElement) {
  148. SVGFontData* svgFontData = new SVGFontData(m_svgFontFaceElement);
  149. fontData.set(new SimpleFontData(FontPlatformData(fontDescription.computedPixelSize(), syntheticBold, syntheticItalic), true, false, svgFontData));
  150. }
  151. #endif
  152. }
  153. } else {
  154. // Kick off the load now.
  155. if (DocLoader* docLoader = fontSelector->docLoader())
  156. m_font->beginLoadIfNeeded(docLoader);
  157. // FIXME: m_string is a URL so it makes no sense to pass it as a family name.
  158. SimpleFontData* tempData = fontCache()->getCachedFontData(fontDescription, m_string);
  159. if (!tempData)
  160. tempData = fontCache()->getLastResortFallbackFont(fontDescription);
  161. fontData.set(new SimpleFontData(tempData->platformData(), true, true));
  162. }
  163. m_fontDataTable.set(hashKey, fontData.get());
  164. return fontData.release();
  165. }
  166. }