Skip to content

Commit

Permalink
Add Fitzpatrick skin type based color replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
laktyushin committed Nov 17, 2021
1 parent 7fdc010 commit 67f103b
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 13 deletions.
11 changes: 10 additions & 1 deletion inc/rlottie.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ struct LOTLayerNode;

namespace rlottie {

enum class FitzModifier {
None,
Type12,
Type3,
Type4,
Type5,
Type6
};

/**
* @brief Configures rlottie model cache policy.
*
Expand Down Expand Up @@ -293,7 +302,7 @@ class LOT_EXPORT Animation {
loadFromData(std::string jsonData, const std::string &key,
const std::string &resourcePath="", bool cachePolicy=true,
const std::vector<std::pair<std::uint32_t, std::uint32_t>>
&colorReplacements = {});
&colorReplacements = {}, FitzModifier fitzModifier = FitzModifier::None);

/**
* @brief Returns default framerate of the Lottie resource.
Expand Down
4 changes: 2 additions & 2 deletions src/lottie/lottieanimation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ std::unique_ptr<Animation> Animation::loadFromData(
std::string jsonData, const std::string &key,
const std::string &resourcePath, bool cachePolicy,
const std::vector<std::pair<std::uint32_t, std::uint32_t>>
&colorReplacements)
&colorReplacements, FitzModifier fitzModifier)
{
if (jsonData.empty()) {
vWarning << "jason data is empty";
Expand All @@ -252,7 +252,7 @@ std::unique_ptr<Animation> Animation::loadFromData(
LottieLoader loader;
if (loader.loadFromData(std::move(jsonData), key,
(resourcePath.empty() ? " " : resourcePath),
cachePolicy, colorReplacements)) {
cachePolicy, colorReplacements, fitzModifier)) {
auto animation = std::unique_ptr<Animation>(new Animation);
animation->d->init(loader.model());
return animation;
Expand Down
4 changes: 2 additions & 2 deletions src/lottie/lottieloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,15 @@ bool LottieLoader::loadFromData(
std::string &&jsonData, const std::string &key,
const std::string &resourcePath, bool cachePolicy,
const std::vector<std::pair<std::uint32_t, std::uint32_t>>
&colorReplacements)
&colorReplacements, rlottie::FitzModifier fitzModifier)
{
if (cachePolicy) {
mModel = LottieModelCache::instance().find(key);
if (mModel) return true;
}

LottieParser parser(const_cast<char *>(jsonData.c_str()),
resourcePath.c_str(), colorReplacements);
resourcePath.c_str(), colorReplacements, fitzModifier);
mModel = parser.model();

if (!mModel) return false;
Expand Down
4 changes: 3 additions & 1 deletion src/lottie/lottieloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include<memory>
#include<vector>

#include "rlottie.h"

class LOTModel;
class LottieLoader
{
Expand All @@ -32,7 +34,7 @@ class LottieLoader
bool loadFromData(std::string &&jsonData, const std::string &key,
const std::string &resourcePath, bool cachePolicy,
const std::vector<std::pair<std::uint32_t, std::uint32_t>>
&colorReplacements);
&colorReplacements, rlottie::FitzModifier fitzModifier);
std::shared_ptr<LOTModel> model();
private:
std::shared_ptr<LOTModel> mModel;
Expand Down
83 changes: 77 additions & 6 deletions src/lottie/lottieparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,10 @@ class LottieParserImpl : public LookaheadParserHandler {
public:
LottieParserImpl(char *str, const char *dir_path,
const std::vector<std::pair<std::uint32_t, std::uint32_t>>
&colorReplacements)
&colorReplacements, rlottie::FitzModifier fitzModifier)
: LookaheadParserHandler(str),
mColorReplacements(colorReplacements),
mFitzModifier(fitzModifier),
mDirPath(dir_path)
{
}
Expand Down Expand Up @@ -259,7 +260,10 @@ class LottieParserImpl : public LookaheadParserHandler {
void parseShapeKeyFrame(LOTAnimInfo<LottieShapeData> &obj);
void parseShapeProperty(LOTAnimatable<LottieShapeData> &obj);
void parseDashProperty(LOTDashProperty &dash);


void parseFitzColorReplacements();
void parseFitzColorReplacement();

std::shared_ptr<VInterpolator> interpolator(VPointF, VPointF, std::string);

LottieColor toColor(const char *str);
Expand All @@ -268,8 +272,9 @@ class LottieParserImpl : public LookaheadParserHandler {
void resolveLayerRefs();

protected:
const std::vector<std::pair<std::uint32_t, std::uint32_t>>
&mColorReplacements;
std::vector<std::pair<std::uint32_t, std::uint32_t>>
mColorReplacements;
const rlottie::FitzModifier mFitzModifier;
std::unordered_map<std::string, std::shared_ptr<VInterpolator>>
mInterpolatorCache;
std::shared_ptr<LOTCompositionData> mComposition;
Expand Down Expand Up @@ -590,6 +595,8 @@ void LottieParserImpl::parseComposition()
parseAssets(comp);
} else if (0 == strcmp(key, "layers")) {
parseLayers(comp);
} else if (0 == strcmp(key, "fitz")) {
parseFitzColorReplacements();
} else {
#ifdef DEBUG_PARSER
vWarning << "Composition Attribute Skipped : " << key;
Expand Down Expand Up @@ -624,6 +631,70 @@ void LottieParserImpl::parseAssets(LOTCompositionData *composition)
// update the precomp layers with the actual layer object
}

void LottieParserImpl::parseFitzColorReplacements()
{
RAPIDJSON_ASSERT(PeekType() == kArrayType);
EnterArray();
while (NextArrayValue()) {
parseFitzColorReplacement();
}
}

void LottieParserImpl::parseFitzColorReplacement()
{
uint32_t original = 0;
uint32_t type12 = 0;
uint32_t type3 = 0;
uint32_t type4 = 0;
uint32_t type5 = 0;
uint32_t type6 = 0;

EnterObject();
while (const char *key = NextObjectKey()) {
if (0 == strcmp(key, "o")) {
RAPIDJSON_ASSERT(PeekType() == kNumberType);
original = GetInt();
} else if (0 == strcmp(key, "f12")) {
RAPIDJSON_ASSERT(PeekType() == kNumberType);
type12 = GetInt();
} else if (0 == strcmp(key, "f3")) {
RAPIDJSON_ASSERT(PeekType() == kNumberType);
type3 = GetInt();
} else if (0 == strcmp(key, "f4")) {
RAPIDJSON_ASSERT(PeekType() == kNumberType);
type4 = GetInt();
} else if (0 == strcmp(key, "f5")) {
RAPIDJSON_ASSERT(PeekType() == kNumberType);
type5 = GetInt();
} else if (0 == strcmp(key, "f6")) {
RAPIDJSON_ASSERT(PeekType() == kNumberType);
type6 = GetInt();
} else {
Skip(key);
}
}

switch (mFitzModifier) {
case rlottie::FitzModifier::None:
break;
case rlottie::FitzModifier::Type12:
mColorReplacements.push_back(std::pair<uint32_t, uint32_t>(original, type12));
break;
case rlottie::FitzModifier::Type3:
mColorReplacements.push_back(std::pair<uint32_t, uint32_t>(original, type3));
break;
case rlottie::FitzModifier::Type4:
mColorReplacements.push_back(std::pair<uint32_t, uint32_t>(original, type4));
break;
case rlottie::FitzModifier::Type5:
mColorReplacements.push_back(std::pair<uint32_t, uint32_t>(original, type5));
break;
case rlottie::FitzModifier::Type6:
mColorReplacements.push_back(std::pair<uint32_t, uint32_t>(original, type6));
break;
}
}

static constexpr const unsigned char B64index[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Expand Down Expand Up @@ -2302,8 +2373,8 @@ LottieParser::~LottieParser() = default;
LottieParser::LottieParser(
char *str, const char *dir_path,
const std::vector<std::pair<std::uint32_t, std::uint32_t>>
&colorReplacements)
: d(std::make_unique<LottieParserImpl>(str, dir_path, colorReplacements))
&colorReplacements, rlottie::FitzModifier fitzModifier)
: d(std::make_unique<LottieParserImpl>(str, dir_path, colorReplacements, fitzModifier))
{
if (d->VerifyType())
d->parseComposition();
Expand Down
3 changes: 2 additions & 1 deletion src/lottie/lottieparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef LOTTIEPARSER_H
#define LOTTIEPARSER_H

#include "rlottie.h"
#include "lottiemodel.h"
#include <memory>

Expand All @@ -28,7 +29,7 @@ class LottieParser {
~LottieParser();
LottieParser(char *str, const char *dir_path,
const std::vector<std::pair<std::uint32_t, std::uint32_t>>
&colorReplacements = {});
&colorReplacements = {}, rlottie::FitzModifier fitzModifier = rlottie::FitzModifier::None);
std::shared_ptr<LOTModel> model();
private:
std::unique_ptr<LottieParserImpl> d;
Expand Down

0 comments on commit 67f103b

Please sign in to comment.