mirror of
https://github.com/keymanapp/keyman.git
synced 2026-08-10 10:55:33 +00:00
Merge pull request #12277 from keymanapp/fix/linux/12019_oskFont
fix(linux): add `keymanFacename` to .ldml file
This commit is contained in:
commit
0f2ef228ed
7 changed files with 125 additions and 7 deletions
|
|
@ -26,6 +26,7 @@ Build-Depends:
|
|||
pkgconf,
|
||||
python3-all (>= 3.5),
|
||||
python3-dbus,
|
||||
python3-fonttools,
|
||||
python3-gi,
|
||||
python3-lxml,
|
||||
python3-magic,
|
||||
|
|
@ -90,6 +91,7 @@ Depends:
|
|||
gir1.2-webkit2-4.1 | gir1.2-webkit2-4.0,
|
||||
keyman-engine,
|
||||
python3-bs4,
|
||||
python3-fonttools,
|
||||
python3-gi,
|
||||
python3-packaging,
|
||||
python3-sentry-sdk (>= 1.1),
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import atexit
|
||||
import gettext
|
||||
import importlib
|
||||
import logging
|
||||
import os
|
||||
import pathlib
|
||||
|
|
@ -53,6 +54,14 @@ def initialize_sentry():
|
|||
SentryErrorHandling().initialize_sentry()
|
||||
|
||||
|
||||
def are_requirements_missing():
|
||||
try:
|
||||
ttLib = importlib.import_module('fontTools.ttLib')
|
||||
except ImportError:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
class FileCleanup():
|
||||
"""
|
||||
Allow to register files that will be deleted when the process exits
|
||||
|
|
|
|||
|
|
@ -177,8 +177,8 @@ class InstallKmp():
|
|||
# Special handling to convert kvk into LDML
|
||||
logging.info("Converting %s to LDML and installing both as as keyman file",
|
||||
f['name'])
|
||||
ldml = convert_kvk_to_ldml(fpath)
|
||||
name, ext = os.path.splitext(f['name'])
|
||||
ldml = convert_kvk_to_ldml(name, fpath)
|
||||
ldmlfile = os.path.join(self.packageDir, f"{name}.ldml")
|
||||
output_ldml(ldmlfile, ldml)
|
||||
elif ftype == KMFileTypes.KM_ICON:
|
||||
|
|
|
|||
|
|
@ -1,11 +1,15 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
import logging
|
||||
import os
|
||||
import struct
|
||||
import sys
|
||||
|
||||
from fontTools import ttLib
|
||||
from lxml import etree
|
||||
|
||||
from keyman_config.kmpmetadata import parsemetadata
|
||||
|
||||
# .kvk file format
|
||||
# KVK files are variable length files with variable sized structures.
|
||||
|
||||
|
|
@ -311,7 +315,18 @@ def _get_modifer(key):
|
|||
return modifier
|
||||
|
||||
|
||||
def convert_ldml(kvkData):
|
||||
def _fontFacename(fontFilename):
|
||||
"""Get the facename from fontFilename's TTF tables"""
|
||||
# From https://github.com/mcfletch/ttfquery/blob/master/ttfquery/describe.py
|
||||
FONT_SPECIFIER_NAME_ID = 4
|
||||
font = ttLib.TTFont(fontFilename)
|
||||
for record in font['name'].names:
|
||||
if record.nameID == FONT_SPECIFIER_NAME_ID:
|
||||
return record.toUnicode()
|
||||
return None
|
||||
|
||||
|
||||
def convert_ldml(keyboardName, kvkData, kmpJsonFilename):
|
||||
keymaps = {}
|
||||
|
||||
for key in kvkData.Keys:
|
||||
|
|
@ -349,7 +364,19 @@ def convert_ldml(kvkData):
|
|||
else:
|
||||
keymaps["shift"] = (uskey,)
|
||||
|
||||
info, system, options, keyboards, files = parsemetadata(kmpJsonFilename)
|
||||
|
||||
ldml = etree.Element("keyboard", locale="zzz-keyman")
|
||||
for keyboard in keyboards:
|
||||
if keyboard['id'] != keyboardName:
|
||||
continue
|
||||
if 'oskFont' in keyboard:
|
||||
fontFile = os.path.join(os.path.dirname(kmpJsonFilename), keyboard['oskFont'])
|
||||
font = _fontFacename(fontFile)
|
||||
if font:
|
||||
ldml.set('keymanFacename', font)
|
||||
break
|
||||
|
||||
etree.SubElement(ldml, "version", platform="11")
|
||||
names = etree.SubElement(ldml, "names")
|
||||
names.append(etree.Element("name", value="ZZZ"))
|
||||
|
|
@ -402,6 +429,7 @@ def parse_kvk_file(kvkfile):
|
|||
return kvkData
|
||||
|
||||
|
||||
def convert_kvk_to_ldml(kvkfile):
|
||||
def convert_kvk_to_ldml(name, kvkfile):
|
||||
kvkData = parse_kvk_file(kvkfile)
|
||||
return convert_ldml(kvkData)
|
||||
kmpJsonFilename = os.path.join(os.path.dirname(kvkfile), 'kmp.json')
|
||||
return convert_ldml(name, kvkData, kmpJsonFilename)
|
||||
|
|
|
|||
|
|
@ -9,7 +9,10 @@ gi.require_version('Gtk', '3.0')
|
|||
|
||||
from gi.repository import Gtk
|
||||
|
||||
from keyman_config import __versionwithtag__, __pkgversion__, add_standard_arguments, initialize_logging, initialize_sentry, verify_dbus_running
|
||||
from keyman_config import (
|
||||
_, __versionwithtag__, __pkgversion__, add_standard_arguments,
|
||||
are_requirements_missing, initialize_logging, initialize_sentry,
|
||||
verify_dbus_running)
|
||||
from keyman_config.handle_install import download_and_install_package
|
||||
from keyman_config.ibus_util import verify_ibus_daemon
|
||||
from keyman_config.view_installed import ViewInstalledWindow
|
||||
|
|
@ -42,6 +45,18 @@ if __name__ == '__main__':
|
|||
initialize_sentry()
|
||||
verify_dbus_running()
|
||||
|
||||
if are_requirements_missing():
|
||||
if args.install or args.url:
|
||||
logging.error('Missing requirements. Please install python3-fonttools.')
|
||||
else:
|
||||
dialog = Gtk.MessageDialog(
|
||||
None, 0, Gtk.MessageType.ERROR,
|
||||
Gtk.ButtonsType.OK, _("Missing requirements. Please install python3-fonttools."))
|
||||
dialog.run()
|
||||
dialog.destroy()
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
logging.info('Keyman version %s %s', __versionwithtag__, __pkgversion__)
|
||||
|
||||
verify_ibus_daemon(False)
|
||||
|
|
|
|||
|
|
@ -5,7 +5,9 @@ import logging
|
|||
import os
|
||||
import sys
|
||||
|
||||
from keyman_config import add_standard_arguments, initialize_logging, initialize_sentry, verify_dbus_running
|
||||
from keyman_config import (
|
||||
are_requirements_missing, add_standard_arguments, initialize_logging,
|
||||
initialize_sentry, verify_dbus_running)
|
||||
from keyman_config.kvk2ldml import parse_kvk_file, print_kvk, convert_ldml, output_ldml
|
||||
|
||||
|
||||
|
|
@ -25,6 +27,10 @@ def main():
|
|||
initialize_sentry()
|
||||
verify_dbus_running()
|
||||
|
||||
if are_requirements_missing():
|
||||
logging.error('km-kvk2ldml: Missing requirements. Please install python3-fonttools.')
|
||||
sys.exit(1)
|
||||
|
||||
name, ext = os.path.splitext(args.kvkfile)
|
||||
# Check if input file extension is kvk
|
||||
if ext != ".kvk":
|
||||
|
|
@ -56,7 +62,8 @@ def main():
|
|||
|
||||
try:
|
||||
with open(outputfile, 'wb') as ldmlfile:
|
||||
ldml = convert_ldml(kvkData)
|
||||
kmpJsonFilename = os.path.join(os.path.dirname(args.kvkfile), 'kmp.json')
|
||||
ldml = convert_ldml(name, kvkData, kmpJsonFilename)
|
||||
output_ldml(ldmlfile, ldml)
|
||||
except PermissionError:
|
||||
logging.error(f'km-kvk2ldml: error, permission denied writing file `{outputfile}`')
|
||||
|
|
|
|||
57
linux/keyman-config/tests/test_kvk2ldml.py
Normal file
57
linux/keyman-config/tests/test_kvk2ldml.py
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
import os
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from keyman_config.kvk2ldml import KVKData, NFont, convert_ldml
|
||||
|
||||
class Kvk2LdmlTests(unittest.TestCase):
|
||||
def _createKmpJson(self, packagedir):
|
||||
kmpJsonFilename = os.path.join(packagedir, 'kmp.json')
|
||||
with open(kmpJsonFilename, 'w') as file:
|
||||
file.write('''{
|
||||
"system": {
|
||||
"keymanDeveloperVersion": "18.0",
|
||||
"fileVersion": "7.0"
|
||||
},
|
||||
"files": [ {
|
||||
"name": "khmer_angkor.kmx",
|
||||
"description": "Keyboard Khmer Angkor"
|
||||
}, {
|
||||
"name": "kmp.json",
|
||||
"description": "Package information (JSON)"
|
||||
} ],
|
||||
"keyboards": [ {
|
||||
"name": "Khmer Angkor",
|
||||
"id": "khmer_angkor",
|
||||
"version": "1.5",
|
||||
"oskFont": "keymanweb-osk.ttf",
|
||||
"languages": [ {
|
||||
"name": "Central Khmer (Khmer, Cambodia)",
|
||||
"id": "km"
|
||||
} ]}
|
||||
]}''')
|
||||
return kmpJsonFilename
|
||||
|
||||
def test_convert_ldml__adds_keymanFacename(self):
|
||||
# Setup
|
||||
keyboardName = 'khmer_angkor'
|
||||
kvkData = KVKData()
|
||||
kvkData.AssociatedKeyboard = keyboardName
|
||||
kvkData.UnicodeFont = NFont()
|
||||
kvkData.UnicodeFont.name='DontUseThis!'
|
||||
|
||||
workdir = tempfile.TemporaryDirectory()
|
||||
kmpJsonFilename = self._createKmpJson(workdir.name)
|
||||
shutil.copy2(os.path.join(sys.path[0], '../../../common/resources/fonts/keymanweb-osk.ttf'),
|
||||
workdir.name)
|
||||
|
||||
# Execute
|
||||
ldml = convert_ldml(keyboardName, kvkData, kmpJsonFilename)
|
||||
|
||||
# Verify
|
||||
self.assertEqual(ldml.get('locale'), "zzz-keyman")
|
||||
self.assertEqual(ldml.get('keymanFacename'), 'SymChar')
|
||||
|
||||
workdir.cleanup()
|
||||
Loading…
Add table
Reference in a new issue