/usr/share/pyshared/tegakitools/unipen.py is in python-tegakitools 0.3.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2009 The Tegaki project contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# Contributors to this file:
# - Mathieu Blondel
# Incomplete unipen format parser
# See http://hwr.nici.kun.nl/unipen/unipen.def for the format specification
import re
import os
from tegaki.character import Point, Stroke, Writing, Character
from tegaki.charcol import CharacterCollection
class UnipenEventParser(object):
"""SAX-like event-based parser"""
KEYWORD_LINE_REGEXP = re.compile(r"^\.[A-Z]+")
def __init__(self):
self._parsed_file = None
def parse_file(self, path):
self._parsed_file = path
f = open(path)
keyword, args = None, None
for line in f.readlines():
if self._is_keyword_line(line):
if keyword is not None and args is not None:
self.handle_keyword(keyword.strip(), args.strip())
keyword, args = None, None
arr = line.split(" ", 1)
keyword = arr[0][1:]
if len(arr) == 1:
args = ""
else:
args = arr[1]
elif keyword is not None and args is not None:
args += line
if keyword is not None and args is not None:
self.handle_keyword(keyword, args)
f.close()
self.handle_eof()
self._parsed_file = None
def handle_keyword(self, keyword, args):
# default keyword handler
print keyword, args
def handle_eof(self):
# default end-of-file handler
print "end of file"
def _is_keyword_line(self, line):
return (self.KEYWORD_LINE_REGEXP.match(line) is not None)
class UnipenProxyParser(UnipenEventParser):
def __init__(self, redirect):
UnipenEventParser.__init__(self)
self._redirect = redirect
def handle_keyword(self, keyword, args):
self._redirect(keyword, args)
def handle_eof(self):
pass
class UnipenParser(UnipenEventParser):
def __init__(self):
UnipenEventParser.__init__(self)
self._labels = []
self._characters = []
self._char = None
def _handle_SEGMENT(self, args):
seg_type, delimit, quality, label = args.split(" ")
if seg_type == "CHARACTER":
label = label.strip()[1:-1]
self._labels.append(label)
def _handle_START_BOX(self, args):
if self._char:
self._characters.append(self._char)
self._char = Character()
def _handle_PEN_DOWN(self, args):
writing = self._char.get_writing()
points = [[int(p_) for p_ in p.split(" ")] \
for p in args.strip().split("\n")]
stroke = Stroke()
for x, y in points:
stroke.append_point(Point(x,y))
writing.append_stroke(stroke)
def _handle_INCLUDE(self, args):
if not self._parsed_file: return
include_filename = args.upper()
currdir = os.path.dirname(os.path.abspath(self._parsed_file))
# FIXME: don't hardcode include paths
include1 = os.path.join(currdir, "INCLUDE")
include2 = os.path.join(currdir, "..", "INCLUDE")
for include in (include1, include2, currdir):
path = os.path.join(include, include_filename)
if os.path.exists(path):
parser = UnipenProxyParser(self.handle_keyword)
parser.parse_file(path)
break
def handle_keyword(self, keyword, args):
try:
func = getattr(self, "_handle_" + keyword)
except AttributeError:
pass
else:
func(args)
def get_character_collection(self):
charcol = CharacterCollection()
assert(len(self._labels) == len(self._characters))
# group characters with the same label into sets
sets = {}
for i in range(len(self._characters)):
utf8 = self._labels[i]
self._characters[i].set_utf8(utf8)
sets[utf8] = sets.get(utf8, []) + [self._characters[i]]
charcol.add_sets(sets.keys())
for set_name, characters in sets.items():
charcol.append_characters(set_name, characters)
return charcol
|