Skip to content

Commit

Permalink
thorvg: Update to 0.12.7
Browse files Browse the repository at this point in the history
  • Loading branch information
akien-mga committed Mar 9, 2024
1 parent 0ace0a1 commit 6fa77e0
Show file tree
Hide file tree
Showing 28 changed files with 249 additions and 162 deletions.
4 changes: 2 additions & 2 deletions thirdparty/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -861,13 +861,13 @@ instead of `miniz.h` as an external dependency.
## thorvg

- Upstream: https://github.com/thorvg/thorvg
- Version: 0.12.5 (9c8eeaab9629b5d241b1092a3398fe6351c259cd, 2024)
- Version: 0.12.7 (cddae9966cbb48c431ea17c262d6f48393206fd7, 2024)
- License: MIT

Files extracted from upstream source:

See `thorvg/update-thorvg.sh` for extraction instructions. Set the version
number and run the script and apply patches from the `patches` folder.
number and run the script.


## ufbx
Expand Down
2 changes: 1 addition & 1 deletion thirdparty/thorvg/inc/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
// For internal debugging:
//#define THORVG_LOG_ENABLED

#define THORVG_VERSION_STRING "0.12.5"
#define THORVG_VERSION_STRING "0.12.7"
#endif
16 changes: 16 additions & 0 deletions thirdparty/thorvg/inc/thorvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,10 @@ class TVG_API Picture final : public Paint
/**
* @brief Loads a picture data directly from a file.
*
* ThorVG efficiently caches the loaded data using the specified @p path as a key.
* This means that loading the same file again will not result in duplicate operations;
* instead, ThorVG will reuse the previously loaded picture data.
*
* @param[in] path A path to the picture file.
*
* @retval Result::Success When succeed.
Expand All @@ -1238,6 +1242,10 @@ class TVG_API Picture final : public Paint
/**
* @brief Loads a picture data from a memory block of a given size.
*
* ThorVG efficiently caches the loaded data using the specified @p data address as a key
* when the @p copy has @c false. This means that loading the same data again will not result in duplicate operations
* for the sharable @p data. Instead, ThorVG will reuse the previously loaded picture data.
*
* @param[in] data A pointer to a memory location where the content of the picture file is stored.
* @param[in] size The size in bytes of the memory occupied by the @p data.
* @param[in] copy Decides whether the data should be copied into the engine local buffer.
Expand Down Expand Up @@ -1299,6 +1307,10 @@ class TVG_API Picture final : public Paint
/**
* @brief Loads a raw data from a memory block with a given size.
*
* ThorVG efficiently caches the loaded data using the specified @p data address as a key
* when the @p copy has @c false. This means that loading the same data again will not result in duplicate operations
* for the sharable @p data. Instead, ThorVG will reuse the previously loaded picture data.
*
* @param[in] paint A Tvg_Paint pointer to the picture object.
* @param[in] data A pointer to a memory location where the content of the picture raw data is stored.
* @param[in] w The width of the image @p data in pixels.
Expand Down Expand Up @@ -1544,6 +1556,10 @@ class TVG_API Text final : public Paint
/**
* @brief Loads a scalable font data(ttf) from a file.
*
* ThorVG efficiently caches the loaded data using the specified @p path as a key.
* This means that loading the same file again will not result in duplicate operations;
* instead, ThorVG will reuse the previously loaded font data.
*
* @param[in] path The path to the font file.
*
* @retval Result::Success When succeed.
Expand Down
23 changes: 0 additions & 23 deletions thirdparty/thorvg/patches/Fix_compiler_shadowing_warning.patch

This file was deleted.

10 changes: 10 additions & 0 deletions thirdparty/thorvg/src/common/tvgArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ struct Array
return data[idx];
}

const T* begin() const
{
return data;
}

T* begin()
{
return data;
}

T* end()
{
return data + count;
Expand Down
99 changes: 66 additions & 33 deletions thirdparty/thorvg/src/common/tvgBezier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
/* Internal Class Implementation */
/************************************************************************/

static float _lineLength(const Point& pt1, const Point& pt2)
static float _lineLengthApprox(const Point& pt1, const Point& pt2)
{
/* approximate sqrt(x*x + y*y) using alpha max plus beta min algorithm.
With alpha = 1, beta = 3/8, giving results with the largest error less
Expand All @@ -41,14 +41,67 @@ static float _lineLength(const Point& pt1, const Point& pt2)
}


static float _lineLength(const Point& pt1, const Point& pt2)
{
Point diff = {pt2.x - pt1.x, pt2.y - pt1.y};
return sqrtf(diff.x * diff.x + diff.y * diff.y);
}


template<typename LengthFunc>
float _bezLength(const Bezier& cur, LengthFunc lineLengthFunc)
{
Bezier left, right;
auto len = lineLengthFunc(cur.start, cur.ctrl1) + lineLengthFunc(cur.ctrl1, cur.ctrl2) + lineLengthFunc(cur.ctrl2, cur.end);
auto chord = lineLengthFunc(cur.start, cur.end);

if (fabsf(len - chord) > BEZIER_EPSILON) {
tvg::bezSplit(cur, left, right);
return _bezLength(left, lineLengthFunc) + _bezLength(right, lineLengthFunc);
}
return len;
}


template<typename LengthFunc>
float _bezAt(const Bezier& bz, float at, float length, LengthFunc lineLengthFunc)
{
auto biggest = 1.0f;
auto smallest = 0.0f;
auto t = 0.5f;

//just in case to prevent an infinite loop
if (at <= 0) return 0.0f;
if (at >= length) return 1.0f;

while (true) {
auto right = bz;
Bezier left;
bezSplitLeft(right, t, left);
length = _bezLength(left, lineLengthFunc);
if (fabsf(length - at) < BEZIER_EPSILON || fabsf(smallest - biggest) < BEZIER_EPSILON) {
break;
}
if (length < at) {
smallest = t;
t = (t + biggest) * 0.5f;
} else {
biggest = t;
t = (smallest + t) * 0.5f;
}
}
return t;
}


/************************************************************************/
/* External Class Implementation */
/************************************************************************/

namespace tvg
{

void bezSplit(const Bezier&cur, Bezier& left, Bezier& right)
void bezSplit(const Bezier& cur, Bezier& left, Bezier& right)
{
auto c = (cur.ctrl1.x + cur.ctrl2.x) * 0.5f;
left.ctrl1.x = (cur.start.x + cur.ctrl1.x) * 0.5f;
Expand All @@ -72,15 +125,13 @@ void bezSplit(const Bezier&cur, Bezier& left, Bezier& right)

float bezLength(const Bezier& cur)
{
Bezier left, right;
auto len = _lineLength(cur.start, cur.ctrl1) + _lineLength(cur.ctrl1, cur.ctrl2) + _lineLength(cur.ctrl2, cur.end);
auto chord = _lineLength(cur.start, cur.end);
return _bezLength(cur, _lineLength);
}

if (fabsf(len - chord) > BEZIER_EPSILON) {
bezSplit(cur, left, right);
return bezLength(left) + bezLength(right);
}
return len;

float bezLengthApprox(const Bezier& cur)
{
return _bezLength(cur, _lineLengthApprox);
}


Expand Down Expand Up @@ -110,31 +161,13 @@ void bezSplitLeft(Bezier& cur, float at, Bezier& left)

float bezAt(const Bezier& bz, float at, float length)
{
auto biggest = 1.0f;
auto smallest = 0.0f;
auto t = 0.5f;
return _bezAt(bz, at, length, _lineLength);
}

//just in case to prevent an infinite loop
if (at <= 0) return 0.0f;
if (at >= length) return 1.0f;

while (true) {
auto right = bz;
Bezier left;
bezSplitLeft(right, t, left);
length = bezLength(left);
if (fabsf(length - at) < BEZIER_EPSILON || fabsf(smallest - biggest) < BEZIER_EPSILON) {
break;
}
if (length < at) {
smallest = t;
t = (t + biggest) * 0.5f;
} else {
biggest = t;
t = (smallest + t) * 0.5f;
}
}
return t;
float bezAtApprox(const Bezier& bz, float at, float length)
{
return _bezAt(bz, at, length, _lineLengthApprox);
}


Expand Down
2 changes: 2 additions & 0 deletions thirdparty/thorvg/src/common/tvgBezier.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ void bezSplitAt(const Bezier& cur, float at, Bezier& left, Bezier& right);
Point bezPointAt(const Bezier& bz, float t);
float bezAngleAt(const Bezier& bz, float t);

float bezLengthApprox(const Bezier& cur);
float bezAtApprox(const Bezier& bz, float at, float length);
}

#endif //_TVG_BEZIER_H_
1 change: 1 addition & 0 deletions thirdparty/thorvg/src/common/tvgLock.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ namespace tvg {
#endif //THORVG_THREAD_SUPPORT

#endif //_TVG_LOCK_H_

1 change: 1 addition & 0 deletions thirdparty/thorvg/src/common/tvgMath.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#define MATH_PI 3.14159265358979323846f
#define MATH_PI2 1.57079632679489661923f
#define PATH_KAPPA 0.552284f

#define mathMin(x, y) (((x) < (y)) ? (x) : (y))
#define mathMax(x, y) (((x) > (y)) ? (x) : (y))
Expand Down
3 changes: 0 additions & 3 deletions thirdparty/thorvg/src/loaders/png/tvgPngLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ void PngLoader::run(unsigned tid)
surface.h = height;
surface.cs = ColorSpace::ABGR8888;
surface.channelSize = sizeof(uint32_t);

if (state.info_png.color.colortype == LCT_RGBA) surface.premultiplied = false;
else surface.premultiplied = true;
}


Expand Down
33 changes: 20 additions & 13 deletions thirdparty/thorvg/src/loaders/svg/tvgSvgLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ static char* _skipSpace(const char* str, const char* end)
static char* _copyId(const char* str)
{
if (!str) return nullptr;
if (strlen(str) == 0) return nullptr;

return strdup(str);
}
Expand Down Expand Up @@ -377,19 +378,25 @@ static void _parseDashArray(SvgLoaderData* loader, const char *str, SvgDash* das

static char* _idFromUrl(const char* url)
{
url = _skipSpace(url, nullptr);
if ((*url) == '(') {
++url;
url = _skipSpace(url, nullptr);
}
auto open = strchr(url, '(');
auto close = strchr(url, ')');
if (!open || !close || open >= close) return nullptr;

open = strchr(url, '#');
if (!open || open >= close) return nullptr;

++open;
--close;

if ((*url) == '\'') ++url;
if ((*url) == '#') ++url;
//trim the rest of the spaces if any
while (open < close && *close == ' ') --close;

//quick verification
for (auto id = open; id < close; id++) {
if (*id == ' ' || *id == '\'') return nullptr;
}

int i = 0;
while (url[i] > ' ' && url[i] != ')' && url[i] != '\'') ++i;

return strDuplicate(url, i);
return strDuplicate(open, (close - open + 1));
}


Expand Down Expand Up @@ -3494,7 +3501,7 @@ void SvgLoader::clear(bool all)
free(loaderData.svgParse);
loaderData.svgParse = nullptr;

for (auto gradient = loaderData.gradients.data; gradient < loaderData.gradients.end(); ++gradient) {
for (auto gradient = loaderData.gradients.begin(); gradient < loaderData.gradients.end(); ++gradient) {
(*gradient)->clear();
free(*gradient);
}
Expand All @@ -3506,7 +3513,7 @@ void SvgLoader::clear(bool all)

if (!all) return;

for (auto p = loaderData.images.data; p < loaderData.images.end(); ++p) {
for (auto p = loaderData.images.begin(); p < loaderData.images.end(); ++p) {
free(*p);
}
loaderData.images.reset();
Expand Down
4 changes: 2 additions & 2 deletions thirdparty/thorvg/src/loaders/svg/tvgSvgSceneBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ static bool _recognizeShape(SvgNode* node, Shape* shape)
}
case SvgNodeType::Polygon: {
if (node->node.polygon.pts.count < 2) break;
auto pts = node->node.polygon.pts.data;
auto pts = node->node.polygon.pts.begin();
shape->moveTo(pts[0], pts[1]);
for (pts += 2; pts < node->node.polygon.pts.end(); pts += 2) {
shape->lineTo(pts[0], pts[1]);
Expand All @@ -419,7 +419,7 @@ static bool _recognizeShape(SvgNode* node, Shape* shape)
}
case SvgNodeType::Polyline: {
if (node->node.polyline.pts.count < 2) break;
auto pts = node->node.polyline.pts.data;
auto pts = node->node.polyline.pts.begin();
shape->moveTo(pts[0], pts[1]);
for (pts += 2; pts < node->node.polyline.pts.end(); pts += 2) {
shape->lineTo(pts[0], pts[1]);
Expand Down
8 changes: 7 additions & 1 deletion thirdparty/thorvg/src/renderer/sw_engine/tvgSwFill.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,13 @@ bool _prepareRadial(SwFill* fill, const RadialGradient* radial, const Matrix* tr
fill->radial.fy = cy + r * (fy - cy) / dist;
fill->radial.dx = cx - fill->radial.fx;
fill->radial.dy = cy - fill->radial.fy;
fill->radial.a = fill->radial.dr * fill->radial.dr - fill->radial.dx * fill->radial.dx - fill->radial.dy * fill->radial.dy;
// Prevent loss of precision on Apple Silicon when dr=dy and dx=0 due to FMA
// https://github.com/thorvg/thorvg/issues/2014
auto dr2 = fill->radial.dr * fill->radial.dr;
auto dx2 = fill->radial.dx * fill->radial.dx;
auto dy2 = fill->radial.dy * fill->radial.dy;

fill->radial.a = dr2 - dx2 - dy2;
}

if (fill->radial.a > 0) fill->radial.invA = 1.0f / fill->radial.a;
Expand Down
Loading

0 comments on commit 6fa77e0

Please sign in to comment.