Skip to content

Instantly share code, notes, and snippets.

@ospalh
Last active August 29, 2015 14:16
Show Gist options
  • Save ospalh/fe2ade6f746ceec9e627 to your computer and use it in GitHub Desktop.
Save ospalh/fe2ade6f746ceec9e627 to your computer and use it in GitHub Desktop.
Script to copy KanjiVG characters and half the canvas size.
#! /usr/bin/env python3
# -*- coding: utf-8 ; mode: python -*-
#
# © Copyright 2015 Roland Sieker <ospalh@gmail.com>
#
# License: GNU AGPL, version 3 or later;
# http://www.gnu.org/licenses/agpl.html
"""
"""
import argparse
import re
import unicodedata as ucd
fw_box = '<svg xmlns="http://www.w3.org/2000/svg" width="109" height="109" viewBox="0 0 109 109">'
hw_box = '<svg xmlns="http://www.w3.org/2000/svg" width="54.5" height="109" viewBox="27.25 0 54.5 109">'
def munge_file(args):
fw_num = ord(args.kana)
fw_code = '{0:05x}'.format(fw_num)
fw_char = chr(fw_num)
fw_name = ucd.name(fw_char)
fw_file_name = '{}.svg'.format(fw_code)
# N.B.: There is no fixed offset between full-width and half-width
# katakana. Somehow we need a list or have to do this one by one.
hw_num = int(args.hw_code, 16)
hw_code = '{0:05x}'.format(hw_num)
hw_char = chr(hw_num)
hw_name = ucd.name(hw_char)
hw_file_name = '{}.svg'.format(hw_code)
if fw_name not in hw_name:
raise RuntimeError('name miss match for {}'.format(fw_name))
# I had to take this test out because we don’t have characters
# 03099  ゙ and 0309a, ゚, and i just used バ and パ instead
# and cut out the ハ.
print('Full width file: {}'.format(fw_file_name))
with open(fw_file_name) as fw_file:
with open(hw_file_name, 'w') as hw_file:
for line in fw_file.readlines():
line = line.replace(fw_box, hw_box)
hw_file.write(
re.sub(fw_code, hw_code, line))
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=u"""Copy files to half-width kana . """)
parser.add_argument('--kana', '-k', type=str)
parser.add_argument('--hw_code', '-c', type=str)
args = parser.parse_args()
munge_file(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment