Browse Source

Rudimentary implementation of SVG text elements.

tags/2021-05-28
jules 10 years ago
parent
commit
036c6cb4a2
1 changed files with 49 additions and 11 deletions
  1. +49
    -11
      modules/juce_gui_basics/drawables/juce_SVGParser.cpp

+ 49
- 11
modules/juce_gui_basics/drawables/juce_SVGParser.cpp View File

@@ -372,7 +372,7 @@ private:
if (tag == "line") return parseLine (xml);
if (tag == "polyline") return parsePolygon (xml, true);
if (tag == "polygon") return parsePolygon (xml, false);
if (tag == "text") return parseText (xml);
if (tag == "text") return parseText (xml, true);
if (tag == "switch") return parseSwitch (xml);
if (tag == "style") parseCSSStyle (xml);
@@ -804,8 +804,16 @@ private:
}
//==============================================================================
Drawable* parseText (const XmlPath& xml)
Drawable* parseText (const XmlPath& xml, bool shouldParseTransform)
{
if (shouldParseTransform && xml->hasAttribute ("transform"))
{
SVGState newState (*this);
newState.addTransform (xml);
return newState.parseText (xml, false);
}
Array<float> xCoords, yCoords, dxCoords, dyCoords;
getCoordList (xCoords, getInheritedAttribute (xml, "x"), true, true);
@@ -813,28 +821,58 @@ private:
getCoordList (dxCoords, getInheritedAttribute (xml, "dx"), true, true);
getCoordList (dyCoords, getInheritedAttribute (xml, "dy"), true, false);
const Font font (getFont (xml));
const String anchorStr = getStyleAttribute(xml, "text-anchor");
//xxx not done text yet!
DrawableComposite* dc = new DrawableComposite();
forEachXmlChildElement (*xml, e)
{
if (e->isTextElement())
{
const String text (e->getText());
const String text (e->getText().trim());
DrawableText* dt = new DrawableText();
dc->addAndMakeVisible (dt);
dt->setText (text);
dt->setFont (font, true);
dt->setTransform (transform);
int i = 0;
dt->setColour (parseColour (getStyleAttribute (xml, "fill"), i, Colours::black)
.withMultipliedAlpha (getStyleAttribute (xml, "fill-opacity", "1").getFloatValue()));
Path path;
Drawable* s = parseShape (xml.getChild (e), path);
delete s; // xxx not finished!
Rectangle<float> bounds (xCoords[0], yCoords[0] - font.getAscent(),
font.getStringWidthFloat (text), font.getHeight());
if (anchorStr == "middle") bounds.setX (bounds.getX() - bounds.getWidth() / 2.0f);
else if (anchorStr == "end") bounds.setX (bounds.getX() - bounds.getWidth());
dt->setBoundingBox (bounds);
}
else if (e->hasTagNameIgnoringNamespace ("tspan"))
{
Drawable* s = parseText (xml.getChild (e));
delete s; // xxx not finished!
dc->addAndMakeVisible (parseText (xml.getChild (e), true));
}
}
return nullptr;
return dc;
}
Font getFont (const XmlPath& xml) const
{
const float fontSize = getCoordLength (getStyleAttribute (xml, "font-size"), 1.0f);
int style = getStyleAttribute (xml, "font-style").containsIgnoreCase ("italic") ? Font::italic : Font::plain;
if (getStyleAttribute (xml, "font-weight").containsIgnoreCase ("bold"))
style |= Font::bold;
const String family (getStyleAttribute (xml, "font-family"));
return family.isEmpty() ? Font (fontSize, style)
: Font (family, fontSize, style);
}
//==============================================================================


Loading…
Cancel
Save