1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

SVGParser: Add support for multiple coords per text element

This commit is contained in:
attila 2023-10-09 22:13:06 +02:00
parent 365d87ae11
commit aee5a3a45e

View file

@ -1067,12 +1067,10 @@ private:
if (! xml->hasTagName ("text") && ! xml->hasTagNameIgnoringNamespace ("tspan"))
return nullptr;
Array<float> xCoords, yCoords, dxCoords, dyCoords;
Array<float> xCoords, yCoords;
getCoordList (xCoords, getInheritedAttribute (xml, "x"), true, true);
getCoordList (yCoords, getInheritedAttribute (xml, "y"), true, false);
getCoordList (dxCoords, getInheritedAttribute (xml, "dx"), true, true);
getCoordList (dyCoords, getInheritedAttribute (xml, "dy"), true, false);
auto font = getFont (xml);
auto anchorStr = getStyleAttribute (xml, "text-anchor");
@ -1084,29 +1082,55 @@ private:
{
if (e->isTextElement())
{
auto text = e->getText().trim();
auto fullText = e->getText();
auto dt = new DrawableText();
dc->addAndMakeVisible (dt);
const auto subtextElements = [&]
{
std::vector<std::tuple<String, float, float>> result;
dt->setText (text);
dt->setFont (font, true);
if (xCoords.size() == 1)
{
result.emplace_back (fullText, xCoords[0], yCoords[0]);
return result;
}
if (additonalTransform != nullptr)
dt->setDrawableTransform (transform.followedBy (*additonalTransform));
else
dt->setDrawableTransform (transform);
if (xCoords.size() != yCoords.size() || fullText.length() != yCoords.size())
{
jassertfalse;
result.emplace_back (fullText, xCoords[0], yCoords[0]);
return result;
}
dt->setColour (parseColour (xml, "fill", Colours::black)
.withMultipliedAlpha (parseSafeFloat (getStyleAttribute (xml, "fill-opacity", "1"))));
for (int i = 0; i < xCoords.size(); ++i)
result.emplace_back (fullText.substring (i, i + 1), xCoords[i], yCoords[i]);
Rectangle<float> bounds (xCoords[0], yCoords[0] - font.getAscent(),
font.getStringWidthFloat (text), font.getHeight());
return result;
}();
if (anchorStr == "middle") bounds.setX (bounds.getX() - bounds.getWidth() / 2.0f);
else if (anchorStr == "end") bounds.setX (bounds.getX() - bounds.getWidth());
for (const auto& [text, x, y] : subtextElements)
{
auto dt = new DrawableText();
dc->addAndMakeVisible (dt);
dt->setBoundingBox (bounds);
dt->setText (text);
dt->setFont (font, true);
if (additonalTransform != nullptr)
dt->setDrawableTransform (transform.followedBy (*additonalTransform));
else
dt->setDrawableTransform (transform);
dt->setColour (parseColour (xml, "fill", Colours::black)
.withMultipliedAlpha (parseSafeFloat (getStyleAttribute (xml, "fill-opacity", "1"))));
Rectangle<float> bounds (x, y - 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"))
{