This source file includes following definitions.
- svgFontAndFontFaceElementForFontData
- firstParentRendererForNonTextNode
- renderObjectFromRun
- activePaintingResourceFromRun
- floatWidthUsingSVGFont
- drawSVGGlyphs
- glyphDataForCharacter
#include "config.h"
#if ENABLE(SVG_FONTS)
#include "core/rendering/svg/SVGTextRunRenderingContext.h"
#include "SVGNames.h"
#include "core/rendering/RenderObject.h"
#include "core/rendering/svg/RenderSVGInlineText.h"
#include "core/rendering/svg/RenderSVGResourceSolidColor.h"
#include "core/svg/SVGFontData.h"
#include "core/svg/SVGFontElement.h"
#include "core/svg/SVGFontFaceElement.h"
#include "core/svg/SVGGlyphElement.h"
#include "platform/fonts/GlyphBuffer.h"
#include "platform/fonts/WidthIterator.h"
#include "platform/graphics/GraphicsContext.h"
namespace WebCore {
static inline const SVGFontData* svgFontAndFontFaceElementForFontData(const SimpleFontData* fontData, SVGFontFaceElement*& fontFace, SVGFontElement*& font)
{
ASSERT(fontData);
ASSERT(fontData->isCustomFont());
ASSERT(fontData->isSVGFont());
RefPtr<CustomFontData> customFontData = fontData->customFontData();
const SVGFontData* svgFontData = static_cast<const SVGFontData*>(customFontData.get());
fontFace = svgFontData->svgFontFaceElement();
ASSERT(fontFace);
font = fontFace->associatedFontElement();
return svgFontData;
}
static inline RenderObject* firstParentRendererForNonTextNode(RenderObject* renderer)
{
ASSERT(renderer);
return renderer->isText() ? renderer->parent() : renderer;
}
static inline RenderObject* renderObjectFromRun(const TextRun& run)
{
if (TextRun::RenderingContext* renderingContext = run.renderingContext())
return static_cast<SVGTextRunRenderingContext*>(renderingContext)->renderer();
return 0;
}
static inline RenderSVGResource* activePaintingResourceFromRun(const TextRun& run)
{
if (TextRun::RenderingContext* renderingContext = run.renderingContext())
return static_cast<SVGTextRunRenderingContext*>(renderingContext)->activePaintingResource();
return 0;
}
float SVGTextRunRenderingContext::floatWidthUsingSVGFont(const Font& font, const TextRun& run, int& charsConsumed, Glyph& glyphId) const
{
WidthIterator it(&font, run);
GlyphBuffer glyphBuffer;
charsConsumed += it.advance(run.length(), &glyphBuffer);
glyphId = !glyphBuffer.isEmpty() ? glyphBuffer.glyphAt(0) : 0;
return it.runWidthSoFar();
}
void SVGTextRunRenderingContext::drawSVGGlyphs(GraphicsContext* context, const TextRun& run, const SimpleFontData* fontData, const GlyphBuffer& glyphBuffer, int from, int numGlyphs, const FloatPoint& point) const
{
SVGFontElement* fontElement = 0;
SVGFontFaceElement* fontFaceElement = 0;
const SVGFontData* svgFontData = svgFontAndFontFaceElementForFontData(fontData, fontFaceElement, fontElement);
if (!fontElement || !fontFaceElement)
return;
RenderSVGResource* activePaintingResource = activePaintingResourceFromRun(run);
RenderObject* renderObject = renderObjectFromRun(run);
RenderObject* parentRenderObject = firstParentRendererForNonTextNode(renderObject);
RenderStyle* parentRenderObjectStyle = 0;
ASSERT(renderObject);
if (!activePaintingResource) {
RenderSVGResourceSolidColor* solidPaintingResource = RenderSVGResource::sharedSolidPaintingResource();
solidPaintingResource->setColor(context->fillColor());
activePaintingResource = solidPaintingResource;
}
bool isVerticalText = false;
if (parentRenderObject) {
parentRenderObjectStyle = parentRenderObject->style();
ASSERT(parentRenderObjectStyle);
isVerticalText = parentRenderObjectStyle->svgStyle()->isVerticalWritingMode();
}
float scale = scaleEmToUnits(fontData->platformData().size(), fontFaceElement->unitsPerEm());
ASSERT(activePaintingResource);
FloatPoint glyphOrigin;
glyphOrigin.setX(svgFontData->horizontalOriginX() * scale);
glyphOrigin.setY(svgFontData->horizontalOriginY() * scale);
FloatPoint currentPoint = point;
RenderSVGResourceMode resourceMode = context->textDrawingMode() == TextModeStroke ? ApplyToStrokeMode : ApplyToFillMode;
for (int i = 0; i < numGlyphs; ++i) {
Glyph glyph = glyphBuffer.glyphAt(from + i);
if (!glyph)
continue;
float advance = glyphBuffer.advanceAt(from + i).width();
SVGGlyph svgGlyph = fontElement->svgGlyphForGlyph(glyph);
ASSERT(!svgGlyph.isPartOfLigature);
ASSERT(svgGlyph.tableEntry == glyph);
SVGGlyphElement::inheritUnspecifiedAttributes(svgGlyph, svgFontData);
if (svgGlyph.pathData.isEmpty()) {
if (isVerticalText)
currentPoint.move(0, advance);
else
currentPoint.move(advance, 0);
continue;
}
if (isVerticalText) {
glyphOrigin.setX(svgGlyph.verticalOriginX * scale);
glyphOrigin.setY(svgGlyph.verticalOriginY * scale);
}
AffineTransform glyphPathTransform;
glyphPathTransform.translate(currentPoint.x() + glyphOrigin.x(), currentPoint.y() + glyphOrigin.y());
glyphPathTransform.scale(scale, -scale);
Path glyphPath = svgGlyph.pathData;
glyphPath.transform(glyphPathTransform);
if (activePaintingResource->applyResource(parentRenderObject, parentRenderObjectStyle, context, resourceMode)) {
float strokeThickness = context->strokeThickness();
if (renderObject && renderObject->isSVGInlineText())
context->setStrokeThickness(strokeThickness * toRenderSVGInlineText(renderObject)->scalingFactor());
activePaintingResource->postApplyResource(parentRenderObject, context, resourceMode, &glyphPath, 0);
context->setStrokeThickness(strokeThickness);
}
if (isVerticalText)
currentPoint.move(0, advance);
else
currentPoint.move(advance, 0);
}
}
GlyphData SVGTextRunRenderingContext::glyphDataForCharacter(const Font& font, const TextRun& run, WidthIterator& iterator, UChar32 character, bool mirror, int currentCharacter, unsigned& advanceLength)
{
const SimpleFontData* primaryFont = font.primaryFont();
ASSERT(primaryFont);
pair<GlyphData, GlyphPage*> pair = font.glyphDataAndPageForCharacter(character, mirror);
GlyphData glyphData = pair.first;
GlyphData missingGlyphData = primaryFont->missingGlyphData();
if (glyphData.glyph == missingGlyphData.glyph && glyphData.fontData == missingGlyphData.fontData) {
ASSERT(glyphData.fontData);
return glyphData;
}
FontFallbackList* fontList = font.fontList();
ASSERT(fontList);
FontFallbackList::GlyphPagesStateSaver glyphPagesSaver(*fontList);
const SimpleFontData* originalFontData = glyphData.fontData;
if (originalFontData && !originalFontData->isSVGFont()) {
if (TextRun::RenderingContext* renderingContext = run.renderingContext()) {
RenderObject* renderObject = static_cast<SVGTextRunRenderingContext*>(renderingContext)->renderer();
RenderObject* parentRenderObject = renderObject->isText() ? renderObject->parent() : renderObject;
ASSERT(parentRenderObject);
if (Element* parentRenderObjectElement = toElement(parentRenderObject->node())) {
if (isSVGAltGlyphElement(*parentRenderObjectElement))
glyphData.fontData = primaryFont;
}
}
}
const SimpleFontData* fontData = glyphData.fontData;
if (fontData) {
if (!fontData->isSVGFont())
return glyphData;
SVGFontElement* fontElement = 0;
SVGFontFaceElement* fontFaceElement = 0;
const SVGFontData* svgFontData = svgFontAndFontFaceElementForFontData(fontData, fontFaceElement, fontElement);
if (!fontElement || !fontFaceElement)
return glyphData;
if (svgFontData->applySVGGlyphSelection(iterator, glyphData, mirror, currentCharacter, advanceLength))
return glyphData;
}
GlyphPage* page = pair.second;
ASSERT(page);
page->setGlyphDataForCharacter(character, 0, 0);
GlyphData fallbackGlyphData = font.glyphDataForCharacter(character, mirror);
ASSERT(fallbackGlyphData.fontData != fontData);
page->setGlyphDataForCharacter(character, glyphData.glyph, originalFontData);
ASSERT(fallbackGlyphData.fontData);
return fallbackGlyphData;
}
}
#endif