From a06ca8de40efd7a5c5e1ad68a7bfc6136cb79f65 Mon Sep 17 00:00:00 2001 From: Subin Siby Date: Sun, 14 Nov 2021 17:22:27 +0530 Subject: [PATCH] Bundle language support within .app as resources (#3) * Use Logger * Improve varnam initing * Include VST, VLF in bundle resources * Get list of languages from varnam * Move to schemeID instead of scriptName * Remove dependency on LipikaEngine from IME * Use Logger from Lipika-engine * Close varnam if out of focus, better consistent DB * Add Kannada support * Don't gzip .vlf * Import VLF on postinstall --- .gitmodules | 3 + GoVarnam/.gitignore | 3 + GoVarnam/Varnam.swift | 82 +++++++++++++- GoVarnam/schemes | 1 + GoVarnam/update_assets.py | 47 ++++++++ Input Source/AppDelegate.swift | 14 ++- Input Source/ClientManager.swift | 17 ++- Input Source/Common.swift | 71 ++++++++++++ Input Source/Config.swift | 44 ++++++++ Input Source/Logger.swift | 164 ++++++++++++++++++++++++++++ Input Source/MappingStore.swift | 73 ------------- Input Source/VarnamConfig.swift | 28 ++--- Input Source/VarnamController.swift | 71 +++++++----- Installation/Scripts/postinstall | 1 + VarnamIME.xcodeproj/project.pbxproj | 37 +++++-- 15 files changed, 515 insertions(+), 141 deletions(-) create mode 160000 GoVarnam/schemes create mode 100755 GoVarnam/update_assets.py create mode 100644 Input Source/Common.swift create mode 100644 Input Source/Config.swift create mode 100644 Input Source/Logger.swift delete mode 100644 Input Source/MappingStore.swift diff --git a/.gitmodules b/.gitmodules index d5900aa..d537bf4 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "GoVarnam/govarnam"] path = GoVarnam/govarnam url = git@github.com:varnamproject/govarnam.git +[submodule "GoVarnam/schemes"] + path = GoVarnam/schemes + url = https://github.com/varnamproject/schemes.git diff --git a/GoVarnam/.gitignore b/GoVarnam/.gitignore index e5d0467..b0d44ce 100644 --- a/GoVarnam/.gitignore +++ b/GoVarnam/.gitignore @@ -1 +1,4 @@ *.dylib +assets/ +*.vst +*.vlf diff --git a/GoVarnam/Varnam.swift b/GoVarnam/Varnam.swift index 6c5f429..f843426 100644 --- a/GoVarnam/Varnam.swift +++ b/GoVarnam/Varnam.swift @@ -8,7 +8,7 @@ import Foundation -struct VarnamException: Error { +public struct VarnamException: Error { let message: String init(_ message: String) { @@ -20,10 +20,57 @@ struct VarnamException: Error { } } +public struct SchemeDetails { + var Identifier: String + var LangCode: String + var DisplayName: String + var Author: String + var CompiledDate: String + var IsStable: Bool +} + +extension String { + func toCStr() -> UnsafeMutablePointer? { + return UnsafeMutablePointer(mutating: (self as NSString).utf8String) + } +} + public class Varnam { private var varnamHandle: Int32 = 0; + static let assetsFolderPath = Bundle.main.resourceURL!.appendingPathComponent("assets").path + static func importAllVLFInAssets() { + // TODO import only necessary ones + let fm = FileManager.default + for scheme in getAllSchemeDetails() { + do { + let varnam = try! Varnam(scheme.Identifier) + let items = try fm.contentsOfDirectory(atPath: assetsFolderPath) + + for item in items { + if item.hasSuffix(".vlf") && item.hasPrefix(scheme.Identifier) { + let path = assetsFolderPath + "/" + item + varnam.importFromFile(path) + } + } + } catch { + Logger.log.error("Couldn't import") + } + } + } + + // This will only run once + struct VarnamInit { + static let once = VarnamInit() + init() { + print(assetsFolderPath) + varnam_set_vst_lookup_dir(assetsFolderPath.toCStr()) + } + } + internal init(_ schemeID: String = "ml") throws { + _ = VarnamInit.once + schemeID.withCString { let rc = varnam_init_from_id(UnsafeMutablePointer(mutating: $0), &varnamHandle) try! checkError(rc) @@ -39,14 +86,17 @@ public class Varnam { throw VarnamException(getLastError()) } } + + public func close() { + varnam_close(varnamHandle) + } public func transliterate(_ input: String) -> [String] { var arr: UnsafeMutablePointer? = varray_init() - let cInput = (input as NSString).utf8String varnam_transliterate( varnamHandle, 1, - UnsafeMutablePointer(mutating: cInput), + input.toCStr(), &arr ) @@ -59,4 +109,30 @@ public class Varnam { } return results } + + public func importFromFile(_ path: String) { + varnam_import(varnamHandle, path.toCStr()) + } + + public static func getAllSchemeDetails() -> [SchemeDetails] { + _ = VarnamInit.once + + var schemes = [SchemeDetails]() + + let arr = varnam_get_all_scheme_details() + for i in (0..