SVG support via SVGKit

This commit is contained in:
Ali 2019-12-26 15:45:35 +04:00
parent 18d5ce777e
commit b8abe590f2
7 changed files with 91 additions and 7 deletions

View File

@ -21,6 +21,7 @@ def apple_lib(
deps = [],
exported_deps = [],
additional_linker_flags = None,
exported_preprocessor_flags = [],
frameworks = [],
weak_frameworks = [],
swift_version = None,
@ -140,6 +141,7 @@ def apple_lib(
platform_compiler_flags = platform_compiler_flags,
swift_compiler_flags = swift_compiler_flags,
preferred_linkage = "static",
exported_preprocessor_flags = exported_preprocessor_flags,
)
def static_library(
@ -152,6 +154,7 @@ def static_library(
extra_xcode_files = [],
deps = [],
additional_linker_flags = None,
exported_preprocessor_flags = [],
frameworks = [],
weak_frameworks = [],
info_plist = None,
@ -161,7 +164,8 @@ def static_library(
platform_compiler_flags = None,
swift_compiler_flags = None,
warning_as_error = False,
suppress_warnings = True):
suppress_warnings = True
):
apple_lib(
name = name,
srcs = srcs,
@ -175,6 +179,7 @@ def static_library(
extra_xcode_files = extra_xcode_files,
deps = deps,
additional_linker_flags = additional_linker_flags,
exported_preprocessor_flags = exported_preprocessor_flags,
frameworks = frameworks,
weak_frameworks = weak_frameworks,
warning_as_error = warning_as_error,

View File

@ -244,7 +244,7 @@ final class WallpaperGalleryItemNode: GalleryItemNode {
colorSignal = chatServiceBackgroundColor(wallpaper: wallpaper, mediaBox: self.context.account.postbox.mediaBox)
isBlurrable = false
case let .file(file):
let dimensions = file.file.dimensions ?? PixelDimensions(width: 100, height: 100)
let dimensions = file.file.dimensions ?? PixelDimensions(width: 2000, height: 4000)
contentSize = dimensions.cgSize
displaySize = dimensions.cgSize.dividedByScreenScale().integralFloor

29
submodules/Svg/BUCK Normal file
View File

@ -0,0 +1,29 @@
load("//Config:buck_rule_macros.bzl", "static_library")
static_library(
name = "Svg",
srcs = glob([
"Sources/**/*.m",
"Sources/**/*.mm",
"Sources/**/*.c",
"Sources/**/*.cpp",
]),
compiler_flags = [
"-DSVGKitLogVerbose(...)=1",
"-DSVGKitLogWarn(...)=1",
"-DSVGKitLogError(...)=1",
"-DSVGKitLogInfo(...)=1",
],
headers = glob([
"Sources/**/*.h",
]),
exported_headers = glob([
"Sources/*.h",
]),
exported_preprocessor_flags = [
"-I${SDKROOT}/usr/include/libxml2",
],
frameworks = [
"$SDKROOT/System/Library/Frameworks/libxml2.tbd",
],
)

9
submodules/Svg/Sources/Svg.h Executable file
View File

@ -0,0 +1,9 @@
#ifndef Lottie_h
#define Lottie_h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
UIImage * _Nullable drawSvgImage(NSData * _Nonnull data, CGSize size);
#endif /* Lottie_h */

35
submodules/Svg/Sources/Svg.m Executable file
View File

@ -0,0 +1,35 @@
#import "Svg.h"
#import "SVGKit.h"
#import "SVGKExporterUIImage.h"
UIImage * _Nullable drawSvgImage(NSData * _Nonnull data, CGSize size) {
NSDate *startTime = [NSDate date];
SVGKImage *image = [[SVGKImage alloc] initWithData:data];
if (image == nil) {
return;
}
double deltaTime = -1.0f * [startTime timeIntervalSinceNow];
//printf("parseTime = %f\n", deltaTime);
UIGraphicsBeginImageContextWithOptions(size, true, 1.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextFillRect(context, CGRectMake(0.0f, 0.0f, size.width, size.height));
startTime = [NSDate date];
[image renderToContext:context antiAliased:true curveFlatnessFactor:1.0 interpolationQuality:kCGInterpolationDefault flipYaxis:false];
deltaTime = -1.0f * [startTime timeIntervalSinceNow];
//printf("drawingTime = %f\n", deltaTime);
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultImage;
}

View File

@ -18,6 +18,7 @@ static_library(
"//submodules/PhotoResources:PhotoResources",
"//submodules/PersistentStringHash:PersistentStringHash",
"//submodules/AppBundle:AppBundle",
"//submodules/Svg:Svg",
],
frameworks = [
"$SDKROOT/System/Library/Frameworks/Foundation.framework",

View File

@ -13,6 +13,7 @@ import LocalMediaResources
import TelegramPresentationData
import TelegramUIPreferences
import AppBundle
import Svg
public func wallpaperDatas(account: Account, accountManager: AccountManager, fileReference: FileMediaReference? = nil, representations: [ImageRepresentationWithReference], alwaysShowThumbnailFirst: Bool = false, thumbnail: Bool = false, onlyFullSize: Bool = false, autoFetchFullSize: Bool = false, synchronousLoad: Bool = false) -> Signal<(Data?, Data?, Bool), NoError> {
if let smallestRepresentation = smallestImageRepresentation(representations.map({ $0.representation })), let largestRepresentation = largestImageRepresentation(representations.map({ $0.representation })), let smallestIndex = representations.firstIndex(where: { $0.representation == smallestRepresentation }), let largestIndex = representations.firstIndex(where: { $0.representation == largestRepresentation }) {
@ -193,11 +194,15 @@ public func wallpaperImage(account: Account, accountManager: AccountManager, fil
var imageOrientation: UIImage.Orientation = .up
if let fullSizeData = fullSizeData {
if fullSizeComplete {
let options = NSMutableDictionary()
options[kCGImageSourceShouldCache as NSString] = false as NSNumber
if let imageSource = CGImageSourceCreateWithData(fullSizeData as CFData, nil), let image = CGImageSourceCreateImageAtIndex(imageSource, 0, options as CFDictionary) {
imageOrientation = imageOrientationFromSource(imageSource)
fullSizeImage = image
if fullSizeData.count > 5, let string = String(data: fullSizeData.subdata(in: 0 ..< 5), encoding: .utf8), string == "<?xml" {
fullSizeImage = drawSvgImage(fullSizeData, fittedRect.size)?.cgImage
} else {
let options = NSMutableDictionary()
options[kCGImageSourceShouldCache as NSString] = false as NSNumber
if let imageSource = CGImageSourceCreateWithData(fullSizeData as CFData, nil), let image = CGImageSourceCreateImageAtIndex(imageSource, 0, options as CFDictionary) {
imageOrientation = imageOrientationFromSource(imageSource)
fullSizeImage = image
}
}
} else {
let imageSource = CGImageSourceCreateIncremental(nil)