Skip to content

Commit

Permalink
Add telegram skin recolor
Browse files Browse the repository at this point in the history
  • Loading branch information
yuraiz committed Oct 27, 2022
1 parent b2d9148 commit 3933fcf
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 21 deletions.
6 changes: 5 additions & 1 deletion rlottie-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ workspace = ".."
name = "rlottie-sys"
version = "0.2.4"
license = "MIT"
include = ["src/**/*.rs", "/build.rs", "/wrapper.h", "/LICENSE"]
include = ["src/**/*.rs", "/build.rs", "/wrapper.h", "/wrapper.hpp", "/wrapper.cpp", "/LICENSE"]

description = "A platform independent standalone library that plays Lottie Animation"
repository = "https://github.com/msrd0/rlottie-rs"
Expand All @@ -18,3 +18,7 @@ links = "rlottie"
[build-dependencies]
bindgen = { version = "0.61.0", features = ["runtime"], default-features = false }
pkg-config = "0.3.22"
cc = "1.0.73"

[features]
tg = []
21 changes: 19 additions & 2 deletions rlottie-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,22 @@ fn main() {
.probe("rlottie")
.expect("Unable to find rlottie");

println!("cargo:rerun-if-changed=wrapper.h");
let header = if cfg!(feature = "tg") {
println!("cargo:rerun-if-changed=wrapper_tg.hpp");
println!("cargo:rerun-if-changed=wrapper_tg.cpp");
"wrapper_tg.hpp"
} else {
println!("cargo:rerun-if-changed=wrapper.h");
"wrapper.h"
};

let bindings = bindgen::Builder::default()
.rustfmt_bindings(false)
.header("wrapper.h")
.header(header)
.clang_arg("-I.")
.clang_arg("-std=c++17")
.clang_arg("-x")
.clang_arg("c++")
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.newtype_enum(".*")
.size_t_is_usize(true)
Expand All @@ -20,4 +32,9 @@ fn main() {
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");

cc::Build::new()
.cpp(true)
.file("wrapper_tg.cpp")
.compile("lib_my_lib.a");
}
24 changes: 24 additions & 0 deletions rlottie-sys/wrapper_tg.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "wrapper_tg.hpp"

#include "rlottie.h"

using rlottie::Animation;
using rlottie::Surface;
// using rlottie::Lottie_Animation_S;
// using rlottie::FitzModifier;

struct Lottie_Animation_S {
std::unique_ptr<Animation> mAnimation;
std::future<Surface> mRenderTask;
uint32_t* mBufferRef;
};

LOT_EXPORT Lottie_Animation* lottie_animation_from_data_tg(const char* data, const char* key, const char* resourcePath, FitzModifier fitzModifier) {
if (auto animation = Animation::loadFromData(data, key, resourcePath, true, {}, rlottie::FitzModifier(fitzModifier))) {
Lottie_Animation_S* handle = new Lottie_Animation_S();
handle->mAnimation = std::move(animation);
return handle;
} else {
return nullptr;
}
}
26 changes: 26 additions & 0 deletions rlottie-sys/wrapper_tg.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "wrapper.h"

#if defined _WIN32 || defined __CYGWIN__
#ifdef LOT_BUILD
#define LOT_EXPORT __declspec(dllexport)
#else
#define LOT_EXPORT __declspec(dllimport)
#endif
#else
#ifdef LOT_BUILD
#define LOT_EXPORT __attribute__ ((visibility ("default")))
#else
#define LOT_EXPORT
#endif
#endif

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

LOT_EXPORT Lottie_Animation* lottie_animation_from_data_tg(const char* data, const char* key, const char* resource_path, FitzModifier fitz_modifier);
3 changes: 3 additions & 0 deletions rlottie/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ rust-version = "1.56"
[dependencies]
rgb = { version = "0.8.32", default-features = false }
rlottie-sys = { path = "../rlottie-sys", version = "0.2" }

[features]
tg = ["rlottie-sys/tg"]
64 changes: 46 additions & 18 deletions rlottie/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,22 @@ use std::{
os::unix::ffi::OsStrExt,
path::Path,
ptr::NonNull,
slice
slice,
};

#[cfg(feature = "tg")]
pub use rlottie_sys::FitzModifier;

pub type Bgra<T = u8> = BGRA<T>;
pub type Rgb<T = f64> = RGB<T>;

fn color_is_valid(Rgb { r, g, b }: Rgb) -> bool {
(0.0 ..= 1.0).contains(&r)
&& (0.0 ..= 1.0).contains(&g)
&& (0.0 ..= 1.0).contains(&b)
(0.0..=1.0).contains(&r) && (0.0..=1.0).contains(&g) && (0.0..=1.0).contains(&b)
}

fn path_to_cstr<P>(path: P) -> CString
where
P: AsRef<Path>
P: AsRef<Path>,
{
let bytes = path.as_ref().as_os_str().as_bytes().to_vec();
CString::new(bytes).expect("path must not contain nul")
Expand All @@ -58,7 +59,7 @@ where
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Size {
pub width: usize,
pub height: usize
pub height: usize,
}

impl Size {
Expand Down Expand Up @@ -97,15 +98,15 @@ mod bgra8_size {
/// the surface.
pub struct Surface {
data: Vec<Bgra>,
size: Size
size: Size,
}

impl Surface {
/// Create a new surface with a fixed size.
pub fn new(size: Size) -> Self {
Self {
data: Vec::with_capacity(size.width * size.height),
size
size,
}
}

Expand Down Expand Up @@ -142,7 +143,7 @@ impl Surface {
unsafe {
slice::from_raw_parts(
self.data.as_ptr() as *const u8,
self.data.len() * mem::size_of::<Bgra>()
self.data.len() * mem::size_of::<Bgra>(),
)
}
}
Expand Down Expand Up @@ -206,7 +207,7 @@ impl Animation {
/// Note that the rlottie library might cache the file and/or its resources.
pub fn from_file<P>(path: P) -> Option<Self>
where
P: AsRef<Path>
P: AsRef<Path>,
{
let path = path_to_cstr(path);
let ptr = unsafe { lottie_animation_from_file(path.as_ptr()) };
Expand All @@ -223,12 +224,12 @@ impl Animation {
pub fn from_data<D, K, P>(
json_data: D,
cache_key: K,
resource_path: P
resource_path: P,
) -> Option<Self>
where
D: Into<Vec<u8>>,
K: Into<Vec<u8>>,
P: AsRef<Path>
P: AsRef<Path>,
{
let json_data =
CString::new(json_data).expect("json_data must not contain nul");
Expand All @@ -238,7 +239,34 @@ impl Animation {
lottie_animation_from_data(
json_data.as_ptr(),
cache_key.as_ptr(),
resource_path.as_ptr()
resource_path.as_ptr(),
)
};
Self::from_ptr(ptr)
}

#[cfg(feature = "tg")]
pub fn from_data_tg<D, K, P>(
json_data: D,
cache_key: K,
resource_path: P,
fitz_modifier: FitzModifier,
) -> Option<Self>
where
D: Into<Vec<u8>>,
K: Into<Vec<u8>>,
P: AsRef<Path>,
{
let json_data =
CString::new(json_data).expect("json_data must not contain nul");
let cache_key = CString::new(cache_key).expect("key must not contain nul");
let resource_path = path_to_cstr(resource_path);
let ptr = unsafe {
lottie_animation_from_data_tg(
json_data.as_ptr(),
cache_key.as_ptr(),
resource_path.as_ptr(),
fitz_modifier,
)
};
Self::from_ptr(ptr)
Expand All @@ -248,13 +276,13 @@ impl Animation {
pub fn size(&self) -> Size {
let mut size = Size {
width: 0,
height: 0
height: 0,
};
unsafe {
lottie_animation_get_size(
self.0.as_ptr(),
&mut size.width,
&mut size.height
&mut size.height,
);
}
size
Expand Down Expand Up @@ -289,7 +317,7 @@ impl Animation {
surface.as_mut_ptr(),
surface.width(),
surface.height(),
surface.width() * 4
surface.width() * 4,
);
surface.set_len();
}
Expand All @@ -313,7 +341,7 @@ impl Animation {

pub fn set_fill_opacity(&mut self, keypath: &str, opacity: f64) {
assert!(
(0.0 ..= 100.0).contains(&opacity),
(0.0..=100.0).contains(&opacity),
"opacity values must be between 0.0 and 100.0"
);
let keypath = CString::new(keypath).unwrap();
Expand Down Expand Up @@ -345,7 +373,7 @@ impl Animation {

pub fn set_stroke_opacity(&mut self, keypath: &str, opacity: f64) {
assert!(
(0.0 ..= 100.0).contains(&opacity),
(0.0..=100.0).contains(&opacity),
"opacity values must be between 0.0 and 100.0"
);
let keypath = CString::new(keypath).unwrap();
Expand Down

0 comments on commit 3933fcf

Please sign in to comment.