diff --git a/src/codec/Attributes.cpp b/src/codec/Attributes.cpp new file mode 100644 index 0000000000..dda64ea508 --- /dev/null +++ b/src/codec/Attributes.cpp @@ -0,0 +1,441 @@ +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// Tencent is pleased to support the open source community by making libpag available. +// +// Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file +// except in compliance with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// unless required by applicable law or agreed to in writing, software distributed under the +// license is distributed on an "as is" basis, without warranties or conditions of any kind, +// either express or implied. see the license for the specific language governing permissions +// and limitations under the license. +// +///////////////////////////////////////////////////////////////////////////////////////////////// + +#include "Attributes.h" + +namespace pag { +// ----------------------------------------------------------------------------- +// AttributeConfig +// ----------------------------------------------------------------------------- +template <> +float AttributeConfig::readValue(DecodeStream* stream) const { + return stream->readFloat(); +} + +template <> +void AttributeConfig::writeValue(EncodeStream* stream, const float& value) const { + stream->writeFloat(value); +} + +template <> +Keyframe* AttributeConfig::newKeyframe(const AttributeFlag&) const { + return new SingleEaseKeyframe(); +} + +// ----------------------------------------------------------------------------- +// AttributeConfig +// ----------------------------------------------------------------------------- +template <> +bool AttributeConfig::readValue(DecodeStream* stream) const { + return stream->readBoolean(); +} + +template <> +void AttributeConfig::writeValue(EncodeStream* stream, const bool& value) const { + stream->writeBoolean(value); +} + +template <> +void AttributeConfig::readValueList(DecodeStream* stream, bool* list, uint32_t count) const { + for (uint32_t i = 0; i < count; i++) { + list[i] = stream->readBitBoolean(); + } +} + +template <> +void AttributeConfig::writeValueList(EncodeStream* stream, const bool* list, + uint32_t count) const { + for (uint32_t i = 0; i < count; i++) { + stream->writeBitBoolean(list[i]); + } +} + +// ----------------------------------------------------------------------------- +// AttributeConfig +// ----------------------------------------------------------------------------- +template <> +uint8_t AttributeConfig::readValue(DecodeStream* stream) const { + return stream->readUint8(); +} + +template <> +void AttributeConfig::writeValue(EncodeStream* stream, const uint8_t& value) const { + stream->writeUint8(value); +} + +template <> +void AttributeConfig::readValueList(DecodeStream* stream, uint8_t* list, + uint32_t count) const { + auto valueList = new uint32_t[count]; + stream->readUint32List(valueList, count); + for (uint32_t i = 0; i < count; i++) { + list[i] = static_cast(valueList[i]); + } + delete[] valueList; +} + +template <> +void AttributeConfig::writeValueList(EncodeStream* stream, const uint8_t* list, + uint32_t count) const { + auto valueList = new uint32_t[count]; + for (uint32_t i = 0; i < count; i++) { + valueList[i] = list[i]; + } + stream->writeUint32List(valueList, count); + delete[] valueList; +} + +template <> +Keyframe* AttributeConfig::newKeyframe(const AttributeFlag&) const { + return new SingleEaseKeyframe(); +} + +// ----------------------------------------------------------------------------- +// AttributeConfig +// ----------------------------------------------------------------------------- +template <> +uint16_t AttributeConfig::readValue(DecodeStream* stream) const { + auto value = stream->readEncodedUint32(); + return static_cast(value); +} + +template <> +void AttributeConfig::writeValue(EncodeStream* stream, const uint16_t& value) const { + stream->writeEncodedUint32(static_cast(value)); +} + +template <> +void AttributeConfig::readValueList(DecodeStream* stream, uint16_t* list, + uint32_t count) const { + auto valueList = new uint32_t[count]; + stream->readUint32List(valueList, count); + for (uint32_t i = 0; i < count; i++) { + list[i] = static_cast(valueList[i]); + } + delete[] valueList; +} + +template <> +void AttributeConfig::writeValueList(EncodeStream* stream, const uint16_t* list, + uint32_t count) const { + auto valueList = new uint32_t[count]; + for (uint32_t i = 0; i < count; i++) { + valueList[i] = list[i]; + } + stream->writeUint32List(valueList, count); + delete[] valueList; +} + +template <> +Keyframe* AttributeConfig::newKeyframe(const AttributeFlag&) const { + return new SingleEaseKeyframe(); +} + +// ----------------------------------------------------------------------------- +// AttributeConfig +// ----------------------------------------------------------------------------- +template <> +uint32_t AttributeConfig::readValue(DecodeStream* stream) const { + return stream->readEncodedUint32(); +} + +template <> +void AttributeConfig::writeValue(EncodeStream* stream, const uint32_t& value) const { + stream->writeEncodedUint32(value); +} + +template <> +void AttributeConfig::readValueList(DecodeStream* stream, uint32_t* list, + uint32_t count) const { + stream->readUint32List(list, count); +} + +template <> +void AttributeConfig::writeValueList(EncodeStream* stream, const uint32_t* list, + uint32_t count) const { + stream->writeUint32List(list, count); +} + +template <> +Keyframe* AttributeConfig::newKeyframe(const AttributeFlag&) const { + return new SingleEaseKeyframe(); +} + +// ----------------------------------------------------------------------------- +// AttributeConfig +// ----------------------------------------------------------------------------- +template <> +int32_t AttributeConfig::readValue(DecodeStream* stream) const { + return stream->readEncodedInt32(); +} + +template <> +void AttributeConfig::writeValue(EncodeStream* stream, const int32_t& value) const { + stream->writeEncodedInt32(value); +} + +template <> +void AttributeConfig::readValueList(DecodeStream* stream, int32_t* list, + uint32_t count) const { + stream->readInt32List(list, count); +} + +template <> +void AttributeConfig::writeValueList(EncodeStream* stream, const int32_t* list, + uint32_t count) const { + stream->writeInt32List(list, count); +} + +template <> +Keyframe* AttributeConfig::newKeyframe(const AttributeFlag&) const { + return new SingleEaseKeyframe(); +} + +// ----------------------------------------------------------------------------- +// AttributeConfig +// ----------------------------------------------------------------------------- +template <> +Frame AttributeConfig::readValue(DecodeStream* stream) const { + return ReadTime(stream); +} + +template <> +void AttributeConfig::writeValue(EncodeStream* stream, const Frame& value) const { + WriteTime(stream, value); +} + +template <> +Keyframe* AttributeConfig::newKeyframe(const AttributeFlag&) const { + return new SingleEaseKeyframe(); +} + +// ----------------------------------------------------------------------------- +// AttributeConfig +// ----------------------------------------------------------------------------- +template <> +int AttributeConfig::dimensionality() const { + return 2; +} + +template <> +Point AttributeConfig::readValue(DecodeStream* stream) const { + return ReadPoint(stream); +} + +template <> +void AttributeConfig::writeValue(EncodeStream* stream, const Point& value) const { + WritePoint(stream, value); +} + +template <> +void AttributeConfig::readValueList(DecodeStream* stream, Point* list, + uint32_t count) const { + if (attributeType == AttributeType::SpatialProperty) { + stream->readFloatList(&(list[0].x), count * 2, SPATIAL_PRECISION); + } else { + for (uint32_t i = 0; i < count; i++) { + list[i] = ReadPoint(stream); + } + } +} + +template <> +void AttributeConfig::writeValueList(EncodeStream* stream, const Point* list, + uint32_t count) const { + if (attributeType == AttributeType::SpatialProperty) { + stream->writeFloatList(&(list[0].x), count * 2, SPATIAL_PRECISION); + } else { + for (uint32_t i = 0; i < count; i++) { + WritePoint(stream, list[i]); + } + } +} + +template <> +Keyframe* AttributeConfig::newKeyframe(const AttributeFlag& flag) const { + switch (attributeType) { + case AttributeType::MultiDimensionProperty: + return new MultiDimensionPointKeyframe(); + case AttributeType::SpatialProperty: + if (flag.hasSpatial) { + return new SpatialPointKeyframe(); + } + default: + return new SingleEaseKeyframe(); + } +} + +// ----------------------------------------------------------------------------- +// AttributeConfig +// ----------------------------------------------------------------------------- +template <> +int AttributeConfig::dimensionality() const { + return 3; +} + +template <> +Color AttributeConfig::readValue(DecodeStream* stream) const { + return ReadColor(stream); +} + +template <> +void AttributeConfig::writeValue(EncodeStream* stream, const Color& value) const { + WriteColor(stream, value); +} + +template <> +Keyframe* AttributeConfig::newKeyframe(const AttributeFlag&) const { + return new SingleEaseKeyframe(); +} + +// ----------------------------------------------------------------------------- +// AttributeConfig +// ----------------------------------------------------------------------------- +template <> +Ratio AttributeConfig::readValue(DecodeStream* stream) const { + return ReadRatio(stream); +} + +template <> +void AttributeConfig::writeValue(EncodeStream* stream, const Ratio& value) const { + WriteRatio(stream, value); +} + +// ----------------------------------------------------------------------------- +// AttributeConfig +// ----------------------------------------------------------------------------- +template <> +std::string AttributeConfig::readValue(DecodeStream* stream) const { + return stream->readUTF8String(); +} + +template <> +void AttributeConfig::writeValue(EncodeStream* stream, + const std::string& value) const { + stream->writeUTF8String(value); +} + +// ----------------------------------------------------------------------------- +// AttributeConfig +// ----------------------------------------------------------------------------- +template <> +PathHandle AttributeConfig::readValue(DecodeStream* stream) const { + return ReadPath(stream); +} + +template <> +void AttributeConfig::writeValue(EncodeStream* stream, const PathHandle& value) const { + WritePath(stream, value); +} + +template <> +Keyframe* AttributeConfig::newKeyframe(const AttributeFlag&) const { + return new SingleEaseKeyframe(); +} + +// ----------------------------------------------------------------------------- +// AttributeConfig +// ----------------------------------------------------------------------------- +template <> +TextDocumentHandle AttributeConfig::readValue(DecodeStream* stream) const { + // 外面根据 tag V1/V2/V3 设置了 defaultValue, + // 这里根据 defaultValue 决定使用哪个 TAG 来读 + if (defaultValue->direction != TextDirection::Default) { + return ReadTextDocumentV3(stream); + } else if (defaultValue->backgroundAlpha != 0) { + return ReadTextDocumentV2(stream); + } else { + return ReadTextDocument(stream); + } +} + +template <> +void AttributeConfig::writeValue(EncodeStream* stream, + const TextDocumentHandle& value) const { + // 外面根据 tag V1/V2/V3 设置了 defaultValue, + // 这里根据 defaultValue 决定使用哪个 TAG 来写 + if (defaultValue->direction != TextDirection::Default) { + WriteTextDocumentV3(stream, value); + } else if (defaultValue->backgroundAlpha != 0) { + WriteTextDocumentV2(stream, value); + } else { + WriteTextDocument(stream, value); + } +} + +// ----------------------------------------------------------------------------- +// AttributeConfig +// ----------------------------------------------------------------------------- +template <> +GradientColorHandle AttributeConfig::readValue(DecodeStream* stream) const { + return ReadGradientColor(stream); +} + +template <> +void AttributeConfig::writeValue(EncodeStream* stream, + const GradientColorHandle& value) const { + WriteGradientColor(stream, value); +} + +template <> +Keyframe* AttributeConfig::newKeyframe( + const AttributeFlag&) const { + return new SingleEaseKeyframe(); +} + +// ----------------------------------------------------------------------------- +// AttributeConfig +// ----------------------------------------------------------------------------- +template <> +Layer* AttributeConfig::readValue(DecodeStream* stream) const { + return ReadLayerID(stream); +} + +template <> +void AttributeConfig::writeValue(EncodeStream* stream, Layer* const& value) const { + WriteLayerID(stream, const_cast(value)); +} + +// ----------------------------------------------------------------------------- +// AttributeConfig +// ----------------------------------------------------------------------------- +template <> +MaskData* AttributeConfig::readValue(DecodeStream* stream) const { + return ReadMaskID(stream); +} + +template <> +void AttributeConfig::writeValue(EncodeStream* stream, MaskData* const& value) const { + WriteMaskID(stream, const_cast(value)); +} + +// ----------------------------------------------------------------------------- +// AttributeConfig +// ----------------------------------------------------------------------------- +template <> +Composition* AttributeConfig::readValue(DecodeStream* stream) const { + return ReadCompositionID(stream); +} + +template <> +void AttributeConfig::writeValue(EncodeStream* stream, + Composition* const& value) const { + WriteCompositionID(stream, const_cast(value)); +} + +} // namespace pag diff --git a/src/codec/Attributes.h b/src/codec/Attributes.h index cdbdf33931..d450867d4a 100644 --- a/src/codec/Attributes.h +++ b/src/codec/Attributes.h @@ -22,729 +22,36 @@ namespace pag { template -class AttributeConfigBase { +class AttributeConfig : public AttributeBase { public: - virtual ~AttributeConfigBase() { - } - - virtual int dimensionality() const { - return 1; - } - - virtual Keyframe* newKeyframe(const AttributeFlag&) const { - return new Keyframe(); - } -}; - -template <> -class AttributeConfig : public AttributeBase, public AttributeConfigBase { - public: - float defaultValue; - - AttributeConfig(AttributeType attributeType, float defaultValue) - : AttributeBase(attributeType), defaultValue(defaultValue) { - } - - void readAttribute(DecodeStream* stream, const AttributeFlag& flag, void* target) const override { - ReadAttribute(stream, flag, target, *this); - } - - void writeAttribute(EncodeStream* flagBytes, EncodeStream* stream, void* target) const override { - WriteAttribute(flagBytes, stream, target, *this); - } - - float readValue(DecodeStream* stream) const { - return stream->readFloat(); - } - - void writeValue(EncodeStream* stream, const float& value) const { - stream->writeFloat(value); - } - - void readValueList(DecodeStream* stream, float* list, uint32_t count) const { - for (uint32_t i = 0; i < count; i++) { - list[i] = readValue(stream); - } - } - - void writeValueList(EncodeStream* stream, const float* list, uint32_t count) const { - for (uint32_t i = 0; i < count; i++) { - writeValue(stream, list[i]); - } - } - - Keyframe* newKeyframe(const AttributeFlag&) const override { - return new SingleEaseKeyframe(); - } -}; - -template <> -class AttributeConfig : public AttributeBase, public AttributeConfigBase { - public: - bool defaultValue; - - AttributeConfig(AttributeType attributeType, bool defaultValue) - : AttributeBase(attributeType), defaultValue(defaultValue) { - } - - void readAttribute(DecodeStream* stream, const AttributeFlag& flag, void* target) const override { - ReadAttribute(stream, flag, target, *this); - } - - void writeAttribute(EncodeStream* flagBytes, EncodeStream* stream, void* target) const override { - WriteAttribute(flagBytes, stream, target, *this); - } - - bool readValue(DecodeStream* stream) const { - return stream->readBoolean(); - } - - void writeValue(EncodeStream* stream, const bool& value) const { - stream->writeBoolean(value); - } - - void readValueList(DecodeStream* stream, bool* list, uint32_t count) const { - for (uint32_t i = 0; i < count; i++) { - list[i] = stream->readBitBoolean(); - } - } - - void writeValueList(EncodeStream* stream, const bool* list, uint32_t count) const { - for (uint32_t i = 0; i < count; i++) { - stream->writeBitBoolean(list[i]); - } - } -}; - -template <> -class AttributeConfig : public AttributeBase, public AttributeConfigBase { - public: - uint8_t defaultValue; - - AttributeConfig(AttributeType attributeType, uint8_t defaultValue) - : AttributeBase(attributeType), defaultValue(defaultValue) { - } - - void readAttribute(DecodeStream* stream, const AttributeFlag& flag, void* target) const override { - ReadAttribute(stream, flag, target, *this); - } - - void writeAttribute(EncodeStream* flagBytes, EncodeStream* stream, void* target) const override { - WriteAttribute(flagBytes, stream, target, *this); - } - - uint8_t readValue(DecodeStream* stream) const { - return stream->readUint8(); - } - - void writeValue(EncodeStream* stream, const uint8_t& value) const { - stream->writeUint8(value); - } - - void readValueList(DecodeStream* stream, uint8_t* list, uint32_t count) const { - auto valueList = new uint32_t[count]; - stream->readUint32List(valueList, count); - for (uint32_t i = 0; i < count; i++) { - list[i] = static_cast(valueList[i]); - } - delete[] valueList; - } - - void writeValueList(EncodeStream* stream, const uint8_t* list, uint32_t count) const { - auto valueList = new uint32_t[count]; - for (uint32_t i = 0; i < count; i++) { - valueList[i] = list[i]; - } - stream->writeUint32List(valueList, count); - delete[] valueList; - } - - Keyframe* newKeyframe(const AttributeFlag&) const override { - return new SingleEaseKeyframe(); - } -}; - -template <> -class AttributeConfig : public AttributeBase, public AttributeConfigBase { - public: - uint16_t defaultValue; - - AttributeConfig(AttributeType attributeType, uint16_t defaultValue) - : AttributeBase(attributeType), defaultValue(defaultValue) { - } - - void readAttribute(DecodeStream* stream, const AttributeFlag& flag, void* target) const override { - ReadAttribute(stream, flag, target, *this); - } - - void writeAttribute(EncodeStream* flagBytes, EncodeStream* stream, void* target) const override { - WriteAttribute(flagBytes, stream, target, *this); - } - - uint16_t readValue(DecodeStream* stream) const { - auto value = stream->readEncodedUint32(); - return static_cast(value); - } - - void writeValue(EncodeStream* stream, const uint16_t& value) const { - stream->writeEncodedUint32(static_cast(value)); - } - - void readValueList(DecodeStream* stream, uint16_t* list, uint32_t count) const { - auto valueList = new uint32_t[count]; - stream->readUint32List(valueList, count); - for (uint32_t i = 0; i < count; i++) { - list[i] = static_cast(valueList[i]); - } - delete[] valueList; - } - - void writeValueList(EncodeStream* stream, const uint16_t* list, uint32_t count) const { - auto valueList = new uint32_t[count]; - for (uint32_t i = 0; i < count; i++) { - valueList[i] = list[i]; - } - stream->writeUint32List(valueList, count); - delete[] valueList; - } - - Keyframe* newKeyframe(const AttributeFlag&) const override { - return new SingleEaseKeyframe(); - } -}; - -template <> -class AttributeConfig : public AttributeBase, public AttributeConfigBase { - public: - uint32_t defaultValue; - - AttributeConfig(AttributeType attributeType, uint32_t defaultValue) - : AttributeBase(attributeType), defaultValue(defaultValue) { - } - - void readAttribute(DecodeStream* stream, const AttributeFlag& flag, void* target) const override { - ReadAttribute(stream, flag, target, *this); - } - - void writeAttribute(EncodeStream* flagBytes, EncodeStream* stream, void* target) const override { - WriteAttribute(flagBytes, stream, target, *this); - } - - uint32_t readValue(DecodeStream* stream) const { - return stream->readEncodedUint32(); - } - - void writeValue(EncodeStream* stream, const uint32_t& value) const { - stream->writeEncodedUint32(value); - } - - void readValueList(DecodeStream* stream, uint32_t* list, uint32_t count) const { - stream->readUint32List(list, count); - } - - void writeValueList(EncodeStream* stream, const uint32_t* list, uint32_t count) const { - stream->writeUint32List(list, count); - } - - Keyframe* newKeyframe(const AttributeFlag&) const override { - return new SingleEaseKeyframe(); - } -}; - -template <> -class AttributeConfig : public AttributeBase, public AttributeConfigBase { - public: - int32_t defaultValue; - - AttributeConfig(AttributeType attributeType, int32_t defaultValue) - : AttributeBase(attributeType), defaultValue(defaultValue) { - } - - void readAttribute(DecodeStream* stream, const AttributeFlag& flag, void* target) const override { - ReadAttribute(stream, flag, target, *this); - } - - void writeAttribute(EncodeStream* flagBytes, EncodeStream* stream, void* target) const override { - WriteAttribute(flagBytes, stream, target, *this); - } - - int32_t readValue(DecodeStream* stream) const { - return stream->readEncodedInt32(); - } - - void writeValue(EncodeStream* stream, const int32_t& value) const { - stream->writeEncodedInt32(value); - } - - void readValueList(DecodeStream* stream, int32_t* list, uint32_t count) const { - stream->readInt32List(list, count); - } - - void writeValueList(EncodeStream* stream, const int32_t* list, uint32_t count) const { - stream->writeInt32List(list, count); - } - - Keyframe* newKeyframe(const AttributeFlag&) const override { - return new SingleEaseKeyframe(); - } -}; - -template <> -class AttributeConfig : public AttributeBase, public AttributeConfigBase { - public: - Frame defaultValue; - - AttributeConfig(AttributeType attributeType, Frame defaultValue) - : AttributeBase(attributeType), defaultValue(defaultValue) { - } - - void readAttribute(DecodeStream* stream, const AttributeFlag& flag, void* target) const override { - ReadAttribute(stream, flag, target, *this); - } - - void writeAttribute(EncodeStream* flagBytes, EncodeStream* stream, void* target) const override { - WriteAttribute(flagBytes, stream, target, *this); - } - - Frame readValue(DecodeStream* stream) const { - return ReadTime(stream); - } - - void writeValue(EncodeStream* stream, const Frame& value) const { - WriteTime(stream, value); - } - - void readValueList(DecodeStream* stream, Frame* list, uint32_t count) const { - for (uint32_t i = 0; i < count; i++) { - list[i] = readValue(stream); - } - } - - void writeValueList(EncodeStream* stream, const Frame* list, uint32_t count) const { - for (uint32_t i = 0; i < count; i++) { - writeValue(stream, list[i]); - } - } - - Keyframe* newKeyframe(const AttributeFlag&) const override { - return new SingleEaseKeyframe(); - } -}; - -template <> -class AttributeConfig : public AttributeBase, public AttributeConfigBase { - public: - Point defaultValue; - - AttributeConfig(AttributeType attributeType, Point defaultValue) - : AttributeBase(attributeType), defaultValue(defaultValue) { - } - - void readAttribute(DecodeStream* stream, const AttributeFlag& flag, void* target) const override { - ReadAttribute(stream, flag, target, *this); - } - - void writeAttribute(EncodeStream* flagBytes, EncodeStream* stream, void* target) const override { - WriteAttribute(flagBytes, stream, target, *this); - } - - int dimensionality() const override { - return 2; - } - - Point readValue(DecodeStream* stream) const { - return ReadPoint(stream); - } - - void writeValue(EncodeStream* stream, const Point& value) const { - WritePoint(stream, value); - } - - void readValueList(DecodeStream* stream, Point* list, uint32_t count) const { - if (attributeType == AttributeType::SpatialProperty) { - stream->readFloatList(&(list[0].x), count * 2, SPATIAL_PRECISION); - } else { - for (uint32_t i = 0; i < count; i++) { - list[i] = ReadPoint(stream); - } - } - } - - void writeValueList(EncodeStream* stream, const Point* list, uint32_t count) const { - if (attributeType == AttributeType::SpatialProperty) { - stream->writeFloatList(&(list[0].x), count * 2, SPATIAL_PRECISION); - } else { - for (uint32_t i = 0; i < count; i++) { - WritePoint(stream, list[i]); - } - } - } - - Keyframe* newKeyframe(const AttributeFlag& flag) const override { - switch (attributeType) { - case AttributeType::MultiDimensionProperty: - return new MultiDimensionPointKeyframe(); - case AttributeType::SpatialProperty: - if (flag.hasSpatial) { - return new SpatialPointKeyframe(); - } - default: - return new SingleEaseKeyframe(); - } - } -}; - -template <> -class AttributeConfig : public AttributeBase, public AttributeConfigBase { - public: - Color defaultValue; - - AttributeConfig(AttributeType attributeType, Color defaultValue) - : AttributeBase(attributeType), defaultValue(defaultValue) { - } + T defaultValue; - void readAttribute(DecodeStream* stream, const AttributeFlag& flag, void* target) const override { - ReadAttribute(stream, flag, target, *this); - } - - void writeAttribute(EncodeStream* flagBytes, EncodeStream* stream, void* target) const override { - WriteAttribute(flagBytes, stream, target, *this); - } - - int dimensionality() const override { - return 3; - } - - Color readValue(DecodeStream* stream) const { - return ReadColor(stream); - } - - void writeValue(EncodeStream* stream, const Color& value) const { - WriteColor(stream, value); - } - - void readValueList(DecodeStream* stream, Color* list, uint32_t count) const { - for (uint32_t i = 0; i < count; i++) { - list[i] = readValue(stream); - } - } - - void writeValueList(EncodeStream* stream, const Color* list, uint32_t count) const { - for (uint32_t i = 0; i < count; i++) { - writeValue(stream, list[i]); - } - } - - Keyframe* newKeyframe(const AttributeFlag&) const override { - return new SingleEaseKeyframe(); - } -}; - -template <> -class AttributeConfig : public AttributeBase, public AttributeConfigBase { - public: - Ratio defaultValue; - - AttributeConfig(AttributeType attributeType, Ratio defaultValue) - : AttributeBase(attributeType), defaultValue(defaultValue) { - } - - void readAttribute(DecodeStream* stream, const AttributeFlag& flag, void* target) const override { - ReadAttribute(stream, flag, target, *this); - } - - void writeAttribute(EncodeStream* flagBytes, EncodeStream* stream, void* target) const override { - WriteAttribute(flagBytes, stream, target, *this); - } - - Ratio readValue(DecodeStream* stream) const { - return ReadRatio(stream); - } - - void writeValue(EncodeStream* stream, const Ratio& value) const { - WriteRatio(stream, value); - } - - void readValueList(DecodeStream* stream, Ratio* list, uint32_t count) const { - for (uint32_t i = 0; i < count; i++) { - list[i] = readValue(stream); - } - } - - void writeValueList(EncodeStream* stream, const Ratio* list, uint32_t count) const { - for (uint32_t i = 0; i < count; i++) { - writeValue(stream, list[i]); - } - } -}; - -template <> -class AttributeConfig : public AttributeBase, public AttributeConfigBase { - public: - std::string defaultValue; - - AttributeConfig(AttributeType attributeType, std::string defaultValue) - : AttributeBase(attributeType), defaultValue(defaultValue) { - } - - void readAttribute(DecodeStream* stream, const AttributeFlag& flag, void* target) const override { - ReadAttribute(stream, flag, target, *this); - } - - void writeAttribute(EncodeStream* flagBytes, EncodeStream* stream, void* target) const override { - WriteAttribute(flagBytes, stream, target, *this); - } - - std::string readValue(DecodeStream* stream) const { - return stream->readUTF8String(); - } - - void writeValue(EncodeStream* stream, const std::string& value) const { - stream->writeUTF8String(value); - } - - void readValueList(DecodeStream* stream, std::string* list, uint32_t count) const { - for (uint32_t i = 0; i < count; i++) { - list[i] = readValue(stream); - } - } - - void writeValueList(EncodeStream* stream, const std::string* list, uint32_t count) const { - for (uint32_t i = 0; i < count; i++) { - writeValue(stream, list[i]); - } - } - - Keyframe* newKeyframe(const AttributeFlag&) const override { - return new Keyframe(); - } -}; - -template <> -class AttributeConfig : public AttributeBase, public AttributeConfigBase { - public: - PathHandle defaultValue; - - AttributeConfig(AttributeType attributeType, PathHandle defaultValue) - : AttributeBase(attributeType), defaultValue(defaultValue) { - } - - void readAttribute(DecodeStream* stream, const AttributeFlag& flag, void* target) const override { - ReadAttribute(stream, flag, target, *this); - } - - void writeAttribute(EncodeStream* flagBytes, EncodeStream* stream, void* target) const override { - WriteAttribute(flagBytes, stream, target, *this); - } - - PathHandle readValue(DecodeStream* stream) const { - return ReadPath(stream); - } - - void writeValue(EncodeStream* stream, const PathHandle& value) const { - WritePath(stream, value); - } - - void readValueList(DecodeStream* stream, PathHandle* list, uint32_t count) const { - for (uint32_t i = 0; i < count; i++) { - list[i] = readValue(stream); - } - } - - void writeValueList(EncodeStream* stream, const PathHandle* list, uint32_t count) const { - for (uint32_t i = 0; i < count; i++) { - writeValue(stream, list[i]); - } - } - - Keyframe* newKeyframe(const AttributeFlag&) const override { - return new SingleEaseKeyframe(); - } -}; - -template <> -class AttributeConfig : public AttributeBase, - public AttributeConfigBase { - public: - TextDocumentHandle defaultValue; - - AttributeConfig(AttributeType attributeType, TextDocumentHandle defaultValue) - : AttributeBase(attributeType), defaultValue(defaultValue) { - } - - void readAttribute(DecodeStream* stream, const AttributeFlag& flag, void* target) const override { - ReadAttribute(stream, flag, target, *this); - } - - void writeAttribute(EncodeStream* flagBytes, EncodeStream* stream, void* target) const override { - WriteAttribute(flagBytes, stream, target, *this); - } - - TextDocumentHandle readValue(DecodeStream* stream) const { - // 外面根据 tag V1/V2/V3 设置了 defaultValue, - // 这里根据 defaultValue 决定使用哪个 TAG 来读 - if (defaultValue->direction != TextDirection::Default) { - return ReadTextDocumentV3(stream); - } else if (defaultValue->backgroundAlpha != 0) { - return ReadTextDocumentV2(stream); - } else { - return ReadTextDocument(stream); - } - } - - void writeValue(EncodeStream* stream, const TextDocumentHandle& value) const { - // 外面根据 tag V1/V2/V3 设置了 defaultValue, - // 这里根据 defaultValue 决定使用哪个 TAG 来写 - if (defaultValue->direction != TextDirection::Default) { - WriteTextDocumentV3(stream, value); - } else if (defaultValue->backgroundAlpha != 0) { - WriteTextDocumentV2(stream, value); - } else { - WriteTextDocument(stream, value); - } - } - - void readValueList(DecodeStream* stream, TextDocumentHandle* list, uint32_t count) const { - for (uint32_t i = 0; i < count; i++) { - list[i] = readValue(stream); - } - } - - void writeValueList(EncodeStream* stream, const TextDocumentHandle* list, uint32_t count) const { - for (uint32_t i = 0; i < count; i++) { - writeValue(stream, list[i]); - } - } -}; - -template <> -class AttributeConfig : public AttributeBase, - public AttributeConfigBase { - public: - GradientColorHandle defaultValue; - - AttributeConfig(AttributeType attributeType, GradientColorHandle defaultValue) - : AttributeBase(attributeType), defaultValue(defaultValue) { - } - - void readAttribute(DecodeStream* stream, const AttributeFlag& flag, void* target) const override { - ReadAttribute(stream, flag, target, *this); - } - - void writeAttribute(EncodeStream* flagBytes, EncodeStream* stream, void* target) const override { - WriteAttribute(flagBytes, stream, target, *this); - } - - GradientColorHandle readValue(DecodeStream* stream) const { - return ReadGradientColor(stream); - } - - void writeValue(EncodeStream* stream, const GradientColorHandle& value) const { - WriteGradientColor(stream, value); - } - - void readValueList(DecodeStream* stream, GradientColorHandle* list, uint32_t count) const { - for (uint32_t i = 0; i < count; i++) { - list[i] = readValue(stream); - } - } - - void writeValueList(EncodeStream* stream, const GradientColorHandle* list, uint32_t count) const { - for (uint32_t i = 0; i < count; i++) { - writeValue(stream, list[i]); - } - } - - Keyframe* newKeyframe(const AttributeFlag&) const override { - return new SingleEaseKeyframe(); - } -}; - -template <> -class AttributeConfig : public AttributeBase, public AttributeConfigBase { - public: - Layer* defaultValue; - - AttributeConfig(AttributeType attributeType, Layer* defaultValue) - : AttributeBase(attributeType), defaultValue(defaultValue) { - } - - void readAttribute(DecodeStream* stream, const AttributeFlag& flag, void* target) const override { - ReadAttribute(stream, flag, target, *this); - } - - void writeAttribute(EncodeStream* flagBytes, EncodeStream* stream, void* target) const override { - WriteAttribute(flagBytes, stream, target, *this); - } - - Layer* readValue(DecodeStream* stream) const { - return ReadLayerID(stream); - } - - void writeValue(EncodeStream* stream, const Layer* value) const { - WriteLayerID(stream, const_cast(value)); - } - - void readValueList(DecodeStream* stream, Layer** list, uint32_t count) const { - for (uint32_t i = 0; i < count; i++) { - list[i] = readValue(stream); - } - } - - void writeValueList(EncodeStream* stream, Layer** list, uint32_t count) const { - for (uint32_t i = 0; i < count; i++) { - writeValue(stream, list[i]); - } - } -}; - -template <> -class AttributeConfig : public AttributeBase, public AttributeConfigBase { - public: - MaskData* defaultValue; - - AttributeConfig(AttributeType attributeType, MaskData* defaultValue) + AttributeConfig(AttributeType attributeType, T defaultValue) : AttributeBase(attributeType), defaultValue(defaultValue) { } - void readAttribute(DecodeStream* stream, const AttributeFlag& flag, void* target) const override { - ReadAttribute(stream, flag, target, *this); - } - - void writeAttribute(EncodeStream* flagBytes, EncodeStream* stream, void* target) const override { - WriteAttribute(flagBytes, stream, target, *this); + virtual ~AttributeConfig() { } - MaskData* readValue(DecodeStream* stream) const { - return ReadMaskID(stream); + int dimensionality() const { + return 1; } - void writeValue(EncodeStream* stream, const MaskData* value) const { - WriteMaskID(stream, const_cast(value)); + Keyframe* newKeyframe(const AttributeFlag&) const { + return new Keyframe(); } - void readValueList(DecodeStream* stream, MaskData** list, uint32_t count) const { + void readValueList(DecodeStream* stream, T* list, uint32_t count) const { for (uint32_t i = 0; i < count; i++) { list[i] = readValue(stream); } } - void writeValueList(EncodeStream* stream, MaskData** list, uint32_t count) const { + void writeValueList(EncodeStream* stream, const T* list, uint32_t count) const { for (uint32_t i = 0; i < count; i++) { writeValue(stream, list[i]); } } -}; - -template <> -class AttributeConfig : public AttributeBase, - public AttributeConfigBase { - public: - Composition* defaultValue; - - AttributeConfig(AttributeType attributeType, Composition* defaultValue) - : AttributeBase(attributeType), defaultValue(defaultValue) { - } void readAttribute(DecodeStream* stream, const AttributeFlag& flag, void* target) const override { ReadAttribute(stream, flag, target, *this); @@ -754,25 +61,9 @@ class AttributeConfig : public AttributeBase, WriteAttribute(flagBytes, stream, target, *this); } - Composition* readValue(DecodeStream* stream) const { - return ReadCompositionID(stream); - } - - void writeValue(EncodeStream* stream, const Composition* value) const { - WriteCompositionID(stream, const_cast(value)); - } - - void readValueList(DecodeStream* stream, Composition** list, uint32_t count) const { - for (uint32_t i = 0; i < count; i++) { - list[i] = readValue(stream); - } - } - - void writeValueList(EncodeStream* stream, Composition** list, uint32_t count) const { - for (uint32_t i = 0; i < count; i++) { - writeValue(stream, list[i]); - } - } + // partial specialization functions + T readValue(DecodeStream* stream) const; + void writeValue(EncodeStream* stream, const T& value) const; }; } // namespace pag