Support colored emoji

This commit is contained in:
Ali
2022-12-10 00:22:38 +04:00
parent 71bed257bb
commit 2cb2717ce7
28 changed files with 411 additions and 119 deletions

View File

@@ -6,7 +6,7 @@ import RLottieBinding
import GZip
import WebPBinding
public func cacheLottieAnimation(data: Data, width: Int, height: Int, keyframeOnly: Bool, writer: AnimationCacheItemWriter, firstFrameOnly: Bool) {
public func cacheLottieAnimation(data: Data, width: Int, height: Int, keyframeOnly: Bool, writer: AnimationCacheItemWriter, firstFrameOnly: Bool, customColor: UIColor?) {
let work: () -> Void = {
let decompressedData = TGGUnzipData(data, 2 * 1024 * 1024) ?? data
guard let animation = LottieInstance(data: decompressedData, fitzModifier: .none, colorReplacements: nil, cacheKey: "") else {
@@ -32,6 +32,19 @@ public func cacheLottieAnimation(data: Data, width: Int, height: Int, keyframeOn
}
writer.add(with: { surface in
animation.renderFrame(with: i, into: surface.argb, width: Int32(surface.width), height: Int32(surface.height), bytesPerRow: Int32(surface.bytesPerRow))
if customColor != nil {
for y in 0 ..< surface.height {
for x in 0 ..< surface.width {
let pixel = surface.argb.advanced(by: y * surface.bytesPerRow + x * 4)
let a = pixel.advanced(by: 3).pointee
pixel.advanced(by: 0).pointee = a
pixel.advanced(by: 1).pointee = a
pixel.advanced(by: 2).pointee = a
pixel.advanced(by: 3).pointee = a
}
}
}
return frameDuration
}, proposedWidth: width, proposedHeight: height, insertKeyframe: i == 0 || keyframeOnly)
@@ -46,7 +59,7 @@ public func cacheLottieAnimation(data: Data, width: Int, height: Int, keyframeOn
writer.queue.async(work)
}
public func cacheStillSticker(path: String, width: Int, height: Int, writer: AnimationCacheItemWriter) {
public func cacheStillSticker(path: String, width: Int, height: Int, writer: AnimationCacheItemWriter, customColor: UIColor?) {
let work: () -> Void = {
if let data = try? Data(contentsOf: URL(fileURLWithPath: path)), let image = WebP.convert(fromWebP: data) {
writer.add(with: { surface in
@@ -55,6 +68,12 @@ public func cacheStillSticker(path: String, width: Int, height: Int, writer: Ani
}
context.withFlippedContext { c in
UIGraphicsPushContext(c)
if let customColor = customColor {
c.setFillColor(customColor.cgColor)
c.setBlendMode(.sourceIn)
}
c.draw(image.cgImage!, in: CGRect(origin: CGPoint(), size: context.size))
UIGraphicsPopContext()
}