2022-10-03 16:25:42 +00:00
|
|
|
#include "paintelement.h"
|
|
|
|
#include "stopelement.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "layoutcontext.h"
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
namespace lunasvg {
|
|
|
|
|
2022-10-16 10:31:43 +00:00
|
|
|
PaintElement::PaintElement(ElementID id)
|
2022-10-03 16:25:42 +00:00
|
|
|
: StyledElement(id)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-10-16 10:31:43 +00:00
|
|
|
GradientElement::GradientElement(ElementID id)
|
2022-10-03 16:25:42 +00:00
|
|
|
: PaintElement(id)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Transform GradientElement::gradientTransform() const
|
|
|
|
{
|
2022-10-16 10:31:43 +00:00
|
|
|
auto& value = get(PropertyID::GradientTransform);
|
2022-10-03 16:25:42 +00:00
|
|
|
return Parser::parseTransform(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
SpreadMethod GradientElement::spreadMethod() const
|
|
|
|
{
|
2022-10-16 10:31:43 +00:00
|
|
|
auto& value = get(PropertyID::SpreadMethod);
|
2022-10-03 16:25:42 +00:00
|
|
|
return Parser::parseSpreadMethod(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Units GradientElement::gradientUnits() const
|
|
|
|
{
|
2022-10-16 10:31:43 +00:00
|
|
|
auto& value = get(PropertyID::GradientUnits);
|
2022-10-03 16:25:42 +00:00
|
|
|
return Parser::parseUnits(value, Units::ObjectBoundingBox);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GradientElement::href() const
|
|
|
|
{
|
2022-10-16 10:31:43 +00:00
|
|
|
auto& value = get(PropertyID::Href);
|
2022-10-03 16:25:42 +00:00
|
|
|
return Parser::parseHref(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
GradientStops GradientElement::buildGradientStops() const
|
|
|
|
{
|
|
|
|
GradientStops gradientStops;
|
|
|
|
double prevOffset = 0.0;
|
2023-07-29 10:37:05 +00:00
|
|
|
for(auto& child : children) {
|
2022-10-03 16:25:42 +00:00
|
|
|
if(child->isText())
|
|
|
|
continue;
|
|
|
|
auto element = static_cast<Element*>(child.get());
|
2022-10-16 10:31:43 +00:00
|
|
|
if(element->id != ElementID::Stop)
|
2022-10-03 16:25:42 +00:00
|
|
|
continue;
|
|
|
|
auto stop = static_cast<StopElement*>(element);
|
|
|
|
auto offset = std::max(prevOffset, stop->offset());
|
|
|
|
prevOffset = offset;
|
|
|
|
gradientStops.emplace_back(offset, stop->stopColorWithOpacity());
|
|
|
|
}
|
|
|
|
|
|
|
|
return gradientStops;
|
|
|
|
}
|
|
|
|
|
|
|
|
LinearGradientElement::LinearGradientElement()
|
2022-10-16 10:31:43 +00:00
|
|
|
: GradientElement(ElementID::LinearGradient)
|
2022-10-03 16:25:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Length LinearGradientElement::x1() const
|
|
|
|
{
|
2022-10-16 10:31:43 +00:00
|
|
|
auto& value = get(PropertyID::X1);
|
2022-10-03 16:25:42 +00:00
|
|
|
return Parser::parseLength(value, AllowNegativeLengths, Length::Zero);
|
|
|
|
}
|
|
|
|
|
|
|
|
Length LinearGradientElement::y1() const
|
|
|
|
{
|
2022-10-16 10:31:43 +00:00
|
|
|
auto& value = get(PropertyID::Y1);
|
2022-10-03 16:25:42 +00:00
|
|
|
return Parser::parseLength(value, AllowNegativeLengths, Length::Zero);
|
|
|
|
}
|
|
|
|
|
|
|
|
Length LinearGradientElement::x2() const
|
|
|
|
{
|
2022-10-16 10:31:43 +00:00
|
|
|
auto& value = get(PropertyID::X2);
|
2022-10-03 16:25:42 +00:00
|
|
|
return Parser::parseLength(value, AllowNegativeLengths, Length::HundredPercent);
|
|
|
|
}
|
|
|
|
|
|
|
|
Length LinearGradientElement::y2() const
|
|
|
|
{
|
2022-10-16 10:31:43 +00:00
|
|
|
auto& value = get(PropertyID::Y2);
|
2022-10-03 16:25:42 +00:00
|
|
|
return Parser::parseLength(value, AllowNegativeLengths, Length::Zero);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<LayoutObject> LinearGradientElement::getPainter(LayoutContext* context) const
|
|
|
|
{
|
|
|
|
LinearGradientAttributes attributes;
|
|
|
|
std::set<const GradientElement*> processedGradients;
|
|
|
|
const GradientElement* current = this;
|
|
|
|
|
2023-07-29 10:37:05 +00:00
|
|
|
while(true) {
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!attributes.hasGradientTransform() && current->has(PropertyID::GradientTransform))
|
2022-10-03 16:25:42 +00:00
|
|
|
attributes.setGradientTransform(current->gradientTransform());
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!attributes.hasSpreadMethod() && current->has(PropertyID::SpreadMethod))
|
2022-10-03 16:25:42 +00:00
|
|
|
attributes.setSpreadMethod(current->spreadMethod());
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!attributes.hasGradientUnits() && current->has(PropertyID::GradientUnits))
|
2022-10-03 16:25:42 +00:00
|
|
|
attributes.setGradientUnits(current->gradientUnits());
|
|
|
|
if(!attributes.hasGradientStops())
|
|
|
|
attributes.setGradientStops(current->buildGradientStops());
|
|
|
|
|
2023-07-29 10:37:05 +00:00
|
|
|
if(current->id == ElementID::LinearGradient) {
|
2022-10-03 16:25:42 +00:00
|
|
|
auto element = static_cast<const LinearGradientElement*>(current);
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!attributes.hasX1() && element->has(PropertyID::X1))
|
2022-10-03 16:25:42 +00:00
|
|
|
attributes.setX1(element->x1());
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!attributes.hasY1() && element->has(PropertyID::Y1))
|
2022-10-03 16:25:42 +00:00
|
|
|
attributes.setY1(element->y1());
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!attributes.hasX2() && element->has(PropertyID::X2))
|
2022-10-03 16:25:42 +00:00
|
|
|
attributes.setX2(element->x2());
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!attributes.hasY2() && element->has(PropertyID::Y2))
|
2022-10-03 16:25:42 +00:00
|
|
|
attributes.setY2(element->y2());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ref = context->getElementById(current->href());
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!ref || !(ref->id == ElementID::LinearGradient || ref->id == ElementID::RadialGradient))
|
2022-10-03 16:25:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
processedGradients.insert(current);
|
|
|
|
current = static_cast<const GradientElement*>(ref);
|
2023-07-29 10:37:05 +00:00
|
|
|
if(processedGradients.find(current) != processedGradients.end()) {
|
2022-10-03 16:25:42 +00:00
|
|
|
break;
|
2023-07-29 10:37:05 +00:00
|
|
|
}
|
2022-10-03 16:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto& stops = attributes.gradientStops();
|
|
|
|
if(stops.empty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
LengthContext lengthContext(this, attributes.gradientUnits());
|
|
|
|
auto x1 = lengthContext.valueForLength(attributes.x1(), LengthMode::Width);
|
|
|
|
auto y1 = lengthContext.valueForLength(attributes.y1(), LengthMode::Height);
|
|
|
|
auto x2 = lengthContext.valueForLength(attributes.x2(), LengthMode::Width);
|
|
|
|
auto y2 = lengthContext.valueForLength(attributes.y2(), LengthMode::Height);
|
2023-07-29 10:37:05 +00:00
|
|
|
if((x1 == x2 && y1 == y2) || stops.size() == 1) {
|
2022-10-03 16:25:42 +00:00
|
|
|
auto solid = std::make_unique<LayoutSolidColor>();
|
|
|
|
solid->color = std::get<1>(stops.back());
|
|
|
|
return std::move(solid);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto gradient = std::make_unique<LayoutLinearGradient>();
|
|
|
|
gradient->transform = attributes.gradientTransform();
|
|
|
|
gradient->spreadMethod = attributes.spreadMethod();
|
|
|
|
gradient->units = attributes.gradientUnits();
|
|
|
|
gradient->stops = attributes.gradientStops();
|
|
|
|
gradient->x1 = x1;
|
|
|
|
gradient->y1 = y1;
|
|
|
|
gradient->x2 = x2;
|
|
|
|
gradient->y2 = y2;
|
|
|
|
return std::move(gradient);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Node> LinearGradientElement::clone() const
|
|
|
|
{
|
|
|
|
return cloneElement<LinearGradientElement>();
|
|
|
|
}
|
|
|
|
|
|
|
|
RadialGradientElement::RadialGradientElement()
|
2022-10-16 10:31:43 +00:00
|
|
|
: GradientElement(ElementID::RadialGradient)
|
2022-10-03 16:25:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Length RadialGradientElement::cx() const
|
|
|
|
{
|
2022-10-16 10:31:43 +00:00
|
|
|
auto& value = get(PropertyID::Cx);
|
2022-10-03 16:25:42 +00:00
|
|
|
return Parser::parseLength(value, AllowNegativeLengths, Length::FiftyPercent);
|
|
|
|
}
|
|
|
|
|
|
|
|
Length RadialGradientElement::cy() const
|
|
|
|
{
|
2022-10-16 10:31:43 +00:00
|
|
|
auto& value = get(PropertyID::Cy);
|
2022-10-03 16:25:42 +00:00
|
|
|
return Parser::parseLength(value, AllowNegativeLengths, Length::FiftyPercent);
|
|
|
|
}
|
|
|
|
|
|
|
|
Length RadialGradientElement::r() const
|
|
|
|
{
|
2022-10-16 10:31:43 +00:00
|
|
|
auto& value = get(PropertyID::R);
|
2022-10-03 16:25:42 +00:00
|
|
|
return Parser::parseLength(value, ForbidNegativeLengths, Length::FiftyPercent);
|
|
|
|
}
|
|
|
|
|
|
|
|
Length RadialGradientElement::fx() const
|
|
|
|
{
|
2022-10-16 10:31:43 +00:00
|
|
|
auto& value = get(PropertyID::Fx);
|
2022-10-03 16:25:42 +00:00
|
|
|
return Parser::parseLength(value, AllowNegativeLengths, Length::Zero);
|
|
|
|
}
|
|
|
|
|
|
|
|
Length RadialGradientElement::fy() const
|
|
|
|
{
|
2022-10-16 10:31:43 +00:00
|
|
|
auto& value = get(PropertyID::Fy);
|
2022-10-03 16:25:42 +00:00
|
|
|
return Parser::parseLength(value, AllowNegativeLengths, Length::Zero);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<LayoutObject> RadialGradientElement::getPainter(LayoutContext* context) const
|
|
|
|
{
|
|
|
|
RadialGradientAttributes attributes;
|
|
|
|
std::set<const GradientElement*> processedGradients;
|
|
|
|
const GradientElement* current = this;
|
|
|
|
|
2023-07-29 10:37:05 +00:00
|
|
|
while(true) {
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!attributes.hasGradientTransform() && current->has(PropertyID::GradientTransform))
|
2022-10-03 16:25:42 +00:00
|
|
|
attributes.setGradientTransform(current->gradientTransform());
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!attributes.hasSpreadMethod() && current->has(PropertyID::SpreadMethod))
|
2022-10-03 16:25:42 +00:00
|
|
|
attributes.setSpreadMethod(current->spreadMethod());
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!attributes.hasGradientUnits() && current->has(PropertyID::GradientUnits))
|
2022-10-03 16:25:42 +00:00
|
|
|
attributes.setGradientUnits(current->gradientUnits());
|
|
|
|
if(!attributes.hasGradientStops())
|
|
|
|
attributes.setGradientStops(current->buildGradientStops());
|
|
|
|
|
2023-07-29 10:37:05 +00:00
|
|
|
if(current->id == ElementID::RadialGradient) {
|
2022-10-03 16:25:42 +00:00
|
|
|
auto element = static_cast<const RadialGradientElement*>(current);
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!attributes.hasCx() && element->has(PropertyID::Cx))
|
2022-10-03 16:25:42 +00:00
|
|
|
attributes.setCx(element->cx());
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!attributes.hasCy() && element->has(PropertyID::Cy))
|
2022-10-03 16:25:42 +00:00
|
|
|
attributes.setCy(element->cy());
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!attributes.hasR() && element->has(PropertyID::R))
|
2022-10-03 16:25:42 +00:00
|
|
|
attributes.setR(element->r());
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!attributes.hasFx() && element->has(PropertyID::Fx))
|
2022-10-03 16:25:42 +00:00
|
|
|
attributes.setFx(element->fx());
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!attributes.hasFy() && element->has(PropertyID::Fy))
|
2022-10-03 16:25:42 +00:00
|
|
|
attributes.setFy(element->fy());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ref = context->getElementById(current->href());
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!ref || !(ref->id == ElementID::LinearGradient || ref->id == ElementID::RadialGradient))
|
2022-10-03 16:25:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
processedGradients.insert(current);
|
|
|
|
current = static_cast<const GradientElement*>(ref);
|
2023-07-29 10:37:05 +00:00
|
|
|
if(processedGradients.find(current) != processedGradients.end()) {
|
2022-10-03 16:25:42 +00:00
|
|
|
break;
|
2023-07-29 10:37:05 +00:00
|
|
|
}
|
2022-10-03 16:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!attributes.hasFx())
|
|
|
|
attributes.setFx(attributes.cx());
|
|
|
|
if(!attributes.hasFy())
|
|
|
|
attributes.setFy(attributes.cy());
|
|
|
|
|
|
|
|
auto& stops = attributes.gradientStops();
|
|
|
|
if(stops.empty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
auto& r = attributes.r();
|
2023-07-29 10:37:05 +00:00
|
|
|
if(r.isZero() || stops.size() == 1) {
|
2022-10-03 16:25:42 +00:00
|
|
|
auto solid = std::make_unique<LayoutSolidColor>();
|
|
|
|
solid->color = std::get<1>(stops.back());
|
|
|
|
return std::move(solid);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto gradient = std::make_unique<LayoutRadialGradient>();
|
|
|
|
gradient->transform = attributes.gradientTransform();
|
|
|
|
gradient->spreadMethod = attributes.spreadMethod();
|
|
|
|
gradient->units = attributes.gradientUnits();
|
|
|
|
gradient->stops = attributes.gradientStops();
|
|
|
|
|
|
|
|
LengthContext lengthContext(this, attributes.gradientUnits());
|
|
|
|
gradient->cx = lengthContext.valueForLength(attributes.cx(), LengthMode::Width);
|
|
|
|
gradient->cy = lengthContext.valueForLength(attributes.cy(), LengthMode::Height);
|
|
|
|
gradient->r = lengthContext.valueForLength(attributes.r(), LengthMode::Both);
|
|
|
|
gradient->fx = lengthContext.valueForLength(attributes.fx(), LengthMode::Width);
|
|
|
|
gradient->fy = lengthContext.valueForLength(attributes.fy(), LengthMode::Height);
|
|
|
|
return std::move(gradient);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Node> RadialGradientElement::clone() const
|
|
|
|
{
|
|
|
|
return cloneElement<RadialGradientElement>();
|
|
|
|
}
|
|
|
|
|
|
|
|
PatternElement::PatternElement()
|
2022-10-16 10:31:43 +00:00
|
|
|
: PaintElement(ElementID::Pattern)
|
2022-10-03 16:25:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Length PatternElement::x() const
|
|
|
|
{
|
2022-10-16 10:31:43 +00:00
|
|
|
auto& value = get(PropertyID::X);
|
2022-10-03 16:25:42 +00:00
|
|
|
return Parser::parseLength(value, AllowNegativeLengths, Length::Zero);
|
|
|
|
}
|
|
|
|
|
|
|
|
Length PatternElement::y() const
|
|
|
|
{
|
2022-10-16 10:31:43 +00:00
|
|
|
auto& value = get(PropertyID::Y);
|
2022-10-03 16:25:42 +00:00
|
|
|
return Parser::parseLength(value, AllowNegativeLengths, Length::Zero);
|
|
|
|
}
|
|
|
|
|
|
|
|
Length PatternElement::width() const
|
|
|
|
{
|
2022-10-16 10:31:43 +00:00
|
|
|
auto& value = get(PropertyID::Width);
|
2022-10-03 16:25:42 +00:00
|
|
|
return Parser::parseLength(value, ForbidNegativeLengths, Length::Zero);
|
|
|
|
}
|
|
|
|
|
|
|
|
Length PatternElement::height() const
|
|
|
|
{
|
2022-10-16 10:31:43 +00:00
|
|
|
auto& value = get(PropertyID::Height);
|
2022-10-03 16:25:42 +00:00
|
|
|
return Parser::parseLength(value, ForbidNegativeLengths, Length::Zero);
|
|
|
|
}
|
|
|
|
|
|
|
|
Transform PatternElement::patternTransform() const
|
|
|
|
{
|
2022-10-16 10:31:43 +00:00
|
|
|
auto& value = get(PropertyID::PatternTransform);
|
2022-10-03 16:25:42 +00:00
|
|
|
return Parser::parseTransform(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Units PatternElement::patternUnits() const
|
|
|
|
{
|
2022-10-16 10:31:43 +00:00
|
|
|
auto& value = get(PropertyID::PatternUnits);
|
2022-10-03 16:25:42 +00:00
|
|
|
return Parser::parseUnits(value, Units::ObjectBoundingBox);
|
|
|
|
}
|
|
|
|
|
|
|
|
Units PatternElement::patternContentUnits() const
|
|
|
|
{
|
2022-10-16 10:31:43 +00:00
|
|
|
auto& value = get(PropertyID::PatternContentUnits);
|
2022-10-03 16:25:42 +00:00
|
|
|
return Parser::parseUnits(value, Units::UserSpaceOnUse);
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect PatternElement::viewBox() const
|
|
|
|
{
|
2022-10-16 10:31:43 +00:00
|
|
|
auto& value = get(PropertyID::ViewBox);
|
2022-10-03 16:25:42 +00:00
|
|
|
return Parser::parseViewBox(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
PreserveAspectRatio PatternElement::preserveAspectRatio() const
|
|
|
|
{
|
2022-10-16 10:31:43 +00:00
|
|
|
auto& value = get(PropertyID::PreserveAspectRatio);
|
2022-10-03 16:25:42 +00:00
|
|
|
return Parser::parsePreserveAspectRatio(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string PatternElement::href() const
|
|
|
|
{
|
2022-10-16 10:31:43 +00:00
|
|
|
auto& value = get(PropertyID::Href);
|
2022-10-03 16:25:42 +00:00
|
|
|
return Parser::parseHref(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<LayoutObject> PatternElement::getPainter(LayoutContext* context) const
|
|
|
|
{
|
|
|
|
if(context->hasReference(this))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
PatternAttributes attributes;
|
|
|
|
std::set<const PatternElement*> processedPatterns;
|
|
|
|
const PatternElement* current = this;
|
|
|
|
|
2023-07-29 10:37:05 +00:00
|
|
|
while(true) {
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!attributes.hasX() && current->has(PropertyID::X))
|
2022-10-03 16:25:42 +00:00
|
|
|
attributes.setX(current->x());
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!attributes.hasY() && current->has(PropertyID::Y))
|
2022-10-03 16:25:42 +00:00
|
|
|
attributes.setY(current->y());
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!attributes.hasWidth() && current->has(PropertyID::Width))
|
2022-10-03 16:25:42 +00:00
|
|
|
attributes.setWidth(current->width());
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!attributes.hasHeight() && current->has(PropertyID::Height))
|
2022-10-03 16:25:42 +00:00
|
|
|
attributes.setHeight(current->height());
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!attributes.hasPatternTransform() && current->has(PropertyID::PatternTransform))
|
2022-10-03 16:25:42 +00:00
|
|
|
attributes.setPatternTransform(current->patternTransform());
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!attributes.hasPatternUnits() && current->has(PropertyID::PatternUnits))
|
2022-10-03 16:25:42 +00:00
|
|
|
attributes.setPatternUnits(current->patternUnits());
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!attributes.hasPatternContentUnits() && current->has(PropertyID::PatternContentUnits))
|
2022-10-03 16:25:42 +00:00
|
|
|
attributes.setPatternContentUnits(current->patternContentUnits());
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!attributes.hasViewBox() && current->has(PropertyID::ViewBox))
|
2022-10-03 16:25:42 +00:00
|
|
|
attributes.setViewBox(current->viewBox());
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!attributes.hasPreserveAspectRatio() && current->has(PropertyID::PreserveAspectRatio))
|
2022-10-03 16:25:42 +00:00
|
|
|
attributes.setPreserveAspectRatio(current->preserveAspectRatio());
|
|
|
|
if(!attributes.hasPatternContentElement() && current->children.size())
|
|
|
|
attributes.setPatternContentElement(current);
|
|
|
|
|
|
|
|
auto ref = context->getElementById(current->href());
|
2022-10-16 10:31:43 +00:00
|
|
|
if(!ref || ref->id != ElementID::Pattern)
|
2022-10-03 16:25:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
processedPatterns.insert(current);
|
|
|
|
current = static_cast<const PatternElement*>(ref);
|
2023-07-29 10:37:05 +00:00
|
|
|
if(processedPatterns.find(current) != processedPatterns.end()) {
|
2022-10-03 16:25:42 +00:00
|
|
|
break;
|
2023-07-29 10:37:05 +00:00
|
|
|
}
|
2022-10-03 16:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto& width = attributes.width();
|
|
|
|
auto& height = attributes.height();
|
|
|
|
auto element = attributes.patternContentElement();
|
|
|
|
if(element == nullptr || width.isZero() || height.isZero())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
LayoutBreaker layoutBreaker(context, this);
|
|
|
|
auto pattern = std::make_unique<LayoutPattern>();
|
|
|
|
pattern->transform = attributes.patternTransform();
|
|
|
|
pattern->units = attributes.patternUnits();
|
|
|
|
pattern->contentUnits = attributes.patternContentUnits();
|
|
|
|
pattern->viewBox = attributes.viewBox();
|
|
|
|
pattern->preserveAspectRatio = attributes.preserveAspectRatio();
|
|
|
|
|
|
|
|
LengthContext lengthContext(this, attributes.patternUnits());
|
|
|
|
pattern->x = lengthContext.valueForLength(attributes.x(), LengthMode::Width);
|
|
|
|
pattern->y = lengthContext.valueForLength(attributes.y(), LengthMode::Height);
|
|
|
|
pattern->width = lengthContext.valueForLength(attributes.width(), LengthMode::Width);
|
|
|
|
pattern->height = lengthContext.valueForLength(attributes.height(), LengthMode::Height);
|
|
|
|
element->layoutChildren(context, pattern.get());
|
|
|
|
return std::move(pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Node> PatternElement::clone() const
|
|
|
|
{
|
|
|
|
return cloneElement<PatternElement>();
|
|
|
|
}
|
|
|
|
|
|
|
|
SolidColorElement::SolidColorElement()
|
2022-10-16 10:31:43 +00:00
|
|
|
: PaintElement(ElementID::SolidColor)
|
2022-10-03 16:25:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<LayoutObject> SolidColorElement::getPainter(LayoutContext*) const
|
|
|
|
{
|
|
|
|
auto solid = std::make_unique<LayoutSolidColor>();
|
|
|
|
solid->color = solid_color();
|
2022-10-16 10:31:43 +00:00
|
|
|
solid->color.combine(solid_opacity());
|
2022-10-03 16:25:42 +00:00
|
|
|
return std::move(solid);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Node> SolidColorElement::clone() const
|
|
|
|
{
|
|
|
|
return cloneElement<SolidColorElement>();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace lunasvg
|