1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "bmstroke_p.h"
5
6#include <QLoggingCategory>
7
8#include "bmconstants_p.h"
9
10QT_BEGIN_NAMESPACE
11
12BMStroke::BMStroke(const BMStroke &other)
13 : BMShape(other)
14{
15 m_opacity = other.m_opacity;
16 m_width = other.m_width;
17 m_color = other.m_color;
18 m_capStyle = other.m_capStyle;
19 m_joinStyle = other.m_joinStyle;
20 m_miterLimit = other.m_miterLimit;
21}
22
23BMStroke::BMStroke(const QJsonObject &definition, const QVersionNumber &version, BMBase *parent)
24{
25 setParent(parent);
26
27 BMBase::parse(definition);
28 if (m_hidden)
29 return;
30
31 qCDebug(lcLottieQtBodymovinParser) << "BMStroke::BMStroke()" << m_name;
32
33 int lineCap = definition.value(key: QLatin1String("lc")).toVariant().toInt();
34 switch (lineCap) {
35 case 1:
36 m_capStyle = Qt::FlatCap;
37 break;
38 case 2:
39 m_capStyle = Qt::RoundCap;
40 break;
41 case 3:
42 m_capStyle = Qt::SquareCap;
43 break;
44 default:
45 qCDebug(lcLottieQtBodymovinParser) << "Unknown line cap style in BMStroke";
46 }
47
48 int lineJoin = definition.value(key: QLatin1String("lj")).toVariant().toInt();
49 switch (lineJoin) {
50 case 1:
51 m_joinStyle = Qt::MiterJoin;
52 m_miterLimit = definition.value(key: QLatin1String("ml")).toVariant().toReal();
53 break;
54 case 2:
55 m_joinStyle = Qt::RoundJoin;
56 break;
57 case 3:
58 m_joinStyle = Qt::BevelJoin;
59 break;
60 default:
61 qCDebug(lcLottieQtBodymovinParser) << "Unknown line join style in BMStroke";
62 }
63
64 QJsonObject opacity = definition.value(key: QLatin1String("o")).toObject();
65 opacity = resolveExpression(definition: opacity);
66 m_opacity.construct(definition: opacity, version);
67
68 QJsonObject width = definition.value(key: QLatin1String("w")).toObject();
69 width = resolveExpression(definition: width);
70 m_width.construct(definition: width, version);
71
72 QJsonObject color = definition.value(key: QLatin1String("c")).toObject();
73 color = resolveExpression(definition: color);
74 m_color.construct(definition: color, version);
75}
76
77BMBase *BMStroke::clone() const
78{
79 return new BMStroke(*this);
80}
81
82void BMStroke::updateProperties(int frame)
83{
84 m_opacity.update(frame);
85 m_width.update(frame);
86 m_color.update(frame);
87}
88
89void BMStroke::render(LottieRenderer &renderer) const
90{
91 renderer.render(stroke: *this);
92}
93
94QPen BMStroke::pen() const
95{
96 qreal width = m_width.value();
97 if (qFuzzyIsNull(d: width))
98 return QPen(Qt::NoPen);
99 QPen pen;
100 pen.setColor(getColor());
101 pen.setWidthF(width);
102 pen.setCapStyle(m_capStyle);
103 pen.setJoinStyle(m_joinStyle);
104 pen.setMiterLimit(m_miterLimit);
105 return pen;
106}
107
108QColor BMStroke::getColor() const
109{
110 QVector4D cVec = m_color.value();
111 QColor color;
112 qreal r = static_cast<qreal>(cVec.x());
113 qreal g = static_cast<qreal>(cVec.y());
114 qreal b = static_cast<qreal>(cVec.z());
115 qreal a = static_cast<qreal>(cVec.w());
116 color.setRgbF(r, g, b, a);
117 return color;
118}
119
120qreal BMStroke::opacity() const
121{
122 return m_opacity.value();
123}
124
125QT_END_NAMESPACE
126

source code of qtlottie/src/bodymovin/bmstroke.cpp