Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rinsuki committed Feb 1, 2025
1 parent aab661d commit d54945b
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//
// EmojiToAdaptiveImageGlyph.swift
//
// iMast https://github.com/cinderella-project/iMast
//
// Created by user on 2024/11/08.
//
// ------------------------------------------------------------------------
//
// Copyright 2017-2024 rinsuki and other contributors.
//
// 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.

// Heavily thanks to Zenmoji by noppe https://github.com/noppefoxwolf
// Licensed under MIT license, https://github.com/noppefoxwolf/Zenmoji/tree/cd86805ab3fc715a43cfaa7a7a37134d3a1951b2

// Zenmoji resizing image to multiple variants (maybe Apple Genmoji also does? didn't confirmed currently since I don't have a device installed iOS 18.2)
// but iOS 18.0 accepts even with one variant.
// also, in Mastodon, emoji (file) size is pretty limited
// so **i guess** we don't need to create small size

import UIKit
#if RESIZE_IMAGE_FOR_ADAPTIVE_GLYPH
import Accelerate
#endif


#if RESIZE_IMAGE_FOR_ADAPTIVE_GLYPH
private let imageSizes = [160, 40, 64, 96, 320]
#endif

@available(iOSApplicationExtension 18.0, *)
func emojiToAdaptiveImageGlyph(image: CGImage) throws -> NSAdaptiveImageGlyph {
let destData = NSMutableData()
#if RESIZE_IMAGE_FOR_ADAPTIVE_GLYPH
let imageVariantsCount = imageSizes.count
#else
let imageVariantsCount = 1
#endif
print("NSAdaptiveImageGlyph.contentType.identifier", NSAdaptiveImageGlyph.contentType.identifier)
let dest = CGImageDestinationCreateWithData(
destData, NSAdaptiveImageGlyph.contentType.identifier as CFString,
imageVariantsCount,
nil
)!
let uuid = UUID().uuidString

#if RESIZE_IMAGE_FOR_ADAPTIVE_GLYPH
// preparing resize for vImage
let format = vImage_CGImageFormat(
bitsPerComponent: 8, bitsPerPixel: 32,
colorSpace: nil,
bitmapInfo: .init(rawValue: CGImageAlphaInfo.first.rawValue),
version: 0,
decode: nil, renderingIntent: .defaultIntent
)
var sourceBuffer = try vImage_Buffer(cgImage: image, format: format)
defer {
sourceBuffer.data.deallocate()
}

for size in imageSizes {
var destBuffer = try vImage_Buffer(width: size, height: size, bitsPerPixel: 32)
defer {
destBuffer.data.deallocate()
}

// TODO: check errors
vImageScale_ARGB8888(&sourceBuffer, &destBuffer, nil, .init(kvImageHighQualityResampling))

let resizedImage = try destBuffer.createCGImage(format: format)

let metadata = CGImageMetadataCreateMutable()
CGImageMetadataSetValueWithPath(metadata, nil, "tiff:TileLength" as CFString, size as CFNumber)
CGImageMetadataSetValueWithPath(metadata, nil, "tiff:TileWidth" as CFString, size as CFNumber)
CGImageMetadataSetValueWithPath(metadata, nil, "tiff:DocumentName" as CFString, uuid as CFString)
CGImageDestinationAddImageAndMetadata(dest, resizedImage, metadata, nil)
print("added \(size) \(resizedImage)")
}
#else
let metadata = CGImageMetadataCreateMutable()
CGImageMetadataSetValueWithPath(metadata, nil, "tiff:TileLength" as CFString, image.height as CFNumber)
CGImageMetadataSetValueWithPath(metadata, nil, "tiff:TileWidth" as CFString, image.width as CFNumber)
CGImageMetadataSetValueWithPath(metadata, nil, "tiff:DocumentName" as CFString, uuid as CFString)
CGImageDestinationAddImageAndMetadata(dest, image, metadata, nil)
#endif
CGImageDestinationFinalize(dest)

let destURL = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true).appending(path: "emoji.bin")
try (destData as Data).write(to: destURL)
print(destURL.absoluteString)

return .init(imageContent: destData as Data)
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,18 @@ class NotificationService: UNNotificationServiceExtension {
attachments: nil
)
let old = self.bestAttemptContent!
let new = try old.updating(from: intent).mutableCopy() as! UNMutableNotificationContent
let new: UNMutableNotificationContent
if #available(iOSApplicationExtension 18.0, *) {
let image = UIImage(data: try Data(contentsOf: URL(string: "https://cdn.rinsuki.net/internal/mstdn-rinsuki-net/custom_emojis/images/000/025/830/original/7cf79b185474b792.png")!))!.cgImage!
let glyph = try! emojiToAdaptiveImageGlyph(image: image)
print(glyph.contentIdentifier, glyph.contentDescription)
var attrString = NSMutableAttributedString(string: "@test ")
attrString.append(.init(adaptiveImageGlyph: glyph))
let context = UNNotificationAttributedMessageContext(sendMessageIntent: intent, attributedContent: attrString)
new = try old.updating(from: context).mutableCopy() as! UNMutableNotificationContent
} else {
new = try old.updating(from: intent).mutableCopy() as! UNMutableNotificationContent
}
self.bestAttemptContent = new
}
}
Expand Down
4 changes: 4 additions & 0 deletions iMast.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
CE90CCF8237804CD001C8E80 /* ShareViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE90CCF7237804CD001C8E80 /* ShareViewController.swift */; };
CE90CD0523780803001C8E80 /* PreferencesWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE90CD03237807A7001C8E80 /* PreferencesWindow.swift */; };
CE90CD0923780AA6001C8E80 /* AccountsPreferencesPaneViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE90CD0723780AA2001C8E80 /* AccountsPreferencesPaneViewController.swift */; };
CE9345CB2CDD2D53000FC217 /* EmojiToAdaptiveImageGlyph.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE9345CA2CDD2D48000FC217 /* EmojiToAdaptiveImageGlyph.swift */; };
CE97D5262340ABF2002DA831 /* ModalNavigationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE97D5252340ABF2002DA831 /* ModalNavigationViewController.swift */; };
CE97D5282340ADDC002DA831 /* EditListInfoViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE97D5272340ADDC002DA831 /* EditListInfoViewController.swift */; };
CE97DB5822D44F4000D2BDC6 /* MastodonMemoryStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE97DB5722D44F4000D2BDC6 /* MastodonMemoryStore.swift */; };
Expand Down Expand Up @@ -838,6 +839,7 @@
CE90CCFA237804CD001C8E80 /* iMastMacShare.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = iMastMacShare.entitlements; sourceTree = "<group>"; };
CE90CD03237807A7001C8E80 /* PreferencesWindow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesWindow.swift; sourceTree = "<group>"; };
CE90CD0723780AA2001C8E80 /* AccountsPreferencesPaneViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountsPreferencesPaneViewController.swift; sourceTree = "<group>"; };
CE9345CA2CDD2D48000FC217 /* EmojiToAdaptiveImageGlyph.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmojiToAdaptiveImageGlyph.swift; sourceTree = "<group>"; };
CE97D5252340ABF2002DA831 /* ModalNavigationViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ModalNavigationViewController.swift; sourceTree = "<group>"; };
CE97D5272340ADDC002DA831 /* EditListInfoViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditListInfoViewController.swift; sourceTree = "<group>"; };
CE97DB5722D44F4000D2BDC6 /* MastodonMemoryStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MastodonMemoryStore.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1838,6 +1840,7 @@
CEC6BC0221074EFB003ECE10 /* iMastNotifyService */ = {
isa = PBXGroup;
children = (
CE9345CA2CDD2D48000FC217 /* EmojiToAdaptiveImageGlyph.swift */,
CEC6BC2221085F7E003ECE10 /* iMastNotifyService.entitlements */,
CEC6BC0321074EFB003ECE10 /* NotificationService.swift */,
CEC6BC0521074EFB003ECE10 /* Info.plist */,
Expand Down Expand Up @@ -3638,6 +3641,7 @@
files = (
CEC6BC1021076DF4003ECE10 /* ReportError_AppExtension.swift in Sources */,
CEC6BC0421074EFB003ECE10 /* NotificationService.swift in Sources */,
CE9345CB2CDD2D53000FC217 /* EmojiToAdaptiveImageGlyph.swift in Sources */,
CE2C9807210A579C00A4FECF /* UIView+ignoreSmartInvert.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down

0 comments on commit d54945b

Please sign in to comment.