Esempio n. 1
0
    def to_stroke_collection(self, dictionary, silent=True):
        """
        @type dictionary: L{CharacterStrokeDictionary
        """
        strokecol = CharacterCollection()
        for char in self.get_all_characters_gen():
            stroke_labels = dictionary.get_strokes(char.get_unicode())[0]
            strokes = char.get_writing().get_strokes(full=True)

            if len(strokes) != len(stroke_labels):
                if silent:
                    continue
                else:
                    raise ValueError, "The number of strokes doesn't " \
                                      "match with reference character"

            for stroke, label in zip(strokes, stroke_labels):
                utf8 = label.encode("utf-8")
                strokecol.add_set(utf8)
                writing = Writing()
                writing.append_stroke(stroke)
                writing.normalize_position()
                schar = Character()
                schar.set_utf8(utf8)
                schar.set_writing(writing)
                strokecol.append_character(utf8, schar)

        return strokecol
Esempio n. 2
0
    def _start_element(self, name, attrs):
        self._tag = name

        if self._first_tag:
            self._first_tag = False
            if self._tag != "dictionary":
                raise ValueError, "The very first tag should be <dictionary>"

        if self._tag == "character":
            self._writing = Writing()

        if self._tag == "stroke":
            self._stroke = Stroke()

        elif self._tag == "point":
            point = Point()

            for key in ("x", "y", "pressure", "xtilt", "ytilt", "timestamp"):
                if attrs.has_key(key):
                    value = attrs[key].encode("UTF-8")
                    if key in ("pressure", "xtilt", "ytilt"):
                        value = float(value)
                    else:
                        value = int(float(value))
                else:
                    value = None

                setattr(point, key, value)

            self._stroke.append_point(point)
Esempio n. 3
0
    def testWritingEquality(self):
        s1 = Stroke()
        s1.append_point(Point(x=2, y=3))
        s1.append_point(Point(x=3, y=4))

        s2 = Stroke()
        s2.append_point(Point(x=2, y=3))
        s2.append_point(Point(x=3, y=4))

        w1 = Writing()
        w1.append_stroke(s1)
        w1.append_stroke(s2)

        s1 = Stroke()
        s1.append_point(Point(x=2, y=3))
        s1.append_point(Point(x=3, y=4))

        s2 = Stroke()
        s2.append_point(Point(x=2, y=3))
        s2.append_point(Point(x=3, y=4))

        w2 = Writing()
        w2.append_stroke(s1)
        w2.append_stroke(s2)

        s1 = Stroke()
        s1.append_point(Point(x=2, y=3))
        s1.append_point(Point(x=3, y=4))

        s2 = Stroke()
        s2.append_point(Point(x=2, y=3))
        s2.append_point(Point(x=3, y=5))

        w3 = Writing()
        w3.append_stroke(s1)
        w3.append_stroke(s2)

        self.assertEquals(w1, w2)
        self.assertNotEqual(w1, w3)
Esempio n. 4
0
    def testWritingEqualityNone(self):
        s1 = Stroke()
        s1.append_point(Point(x=2, y=3))
        s1.append_point(Point(x=3, y=4))

        s2 = Stroke()
        s2.append_point(Point(x=2, y=3))
        s2.append_point(Point(x=3, y=4))

        w1 = Writing()
        w1.append_stroke(s1)
        w1.append_stroke(s2)

        self.assertTrue(w1 != None)
        self.assertFalse(w1 == None)
Esempio n. 5
0
    def testRemoveStroke(self):
        s1 = Stroke()
        s1.append_point(Point(x=2, y=3))
        s1.append_point(Point(x=3, y=4))

        s2 = Stroke()
        s2.append_point(Point(x=2, y=3))
        s2.append_point(Point(x=3, y=4))

        w = Writing()
        w.append_stroke(s1)
        w.append_stroke(s2)

        w.remove_stroke(1)

        self.assertEquals(w.get_strokes(), [[(2, 3), (3, 4)]])
Esempio n. 6
0
    def testWritingCopy(self):
        s1 = Stroke()
        s1.append_point(Point(x=2, y=3))
        s1.append_point(Point(x=3, y=4))

        s2 = Stroke()
        s2.append_point(Point(x=2, y=3))
        s2.append_point(Point(x=3, y=4))

        w1 = Writing()
        w1.append_stroke(s1)
        w1.append_stroke(s2)

        w2 = w1.copy()

        self.assertTrue(w1 == w2)
Esempio n. 7
0
    def _getWriting(self):
        point = Point()
        point.x = 1
        point.y = 2
        point.timestamp = 3

        point2 = Point()
        point2.x = 4
        point2.y = 5
        point2.pressure = 0.1

        stroke = Stroke()
        stroke.append_point(point)
        stroke.append_point(point2)

        writing = Writing()
        writing.append_stroke(stroke)

        return writing
Esempio n. 8
0
    def testReplaceStroke(self):
        s1 = Stroke()
        s1.append_point(Point(x=2, y=3))
        s1.append_point(Point(x=3, y=4))

        s2 = Stroke()
        s2.append_point(Point(x=2, y=3))
        s2.append_point(Point(x=3, y=4))

        w = Writing()
        w.append_stroke(s1)
        w.append_stroke(s2)

        s3 = Stroke()
        s3.append_point(Point(x=22, y=33))
        s3.append_point(Point(x=33, y=44))

        w.replace_stroke(1, s3)
        self.assertEquals(w.get_strokes(), [[(2, 3),
                                             (3, 4)], [(22, 33), (33, 44)]])
Esempio n. 9
0
    def testNewWriting(self):
        writing = Writing()

        writing.move_to(0, 0)
        writing.line_to(1, 1)
        writing.line_to(2, 2)
        writing.line_to(3, 3)

        writing.move_to(4, 4)
        writing.line_to(5, 5)

        writing.move_to(6, 6)
        writing.line_to(7, 7)
        writing.line_to(8, 8)

        strokes = writing.get_strokes()
        expected = [[(0, 0), (1, 1), (2, 2), (3, 3)], [(4, 4), (5, 5)],
                    [(6, 6), (7, 7), (8, 8)]]

        self.assertEquals(strokes, expected)
Esempio n. 10
0
    def _start_element(self, name, attrs):
        self._tag = name

        if self._first_tag:
            self._first_tag = False
            if self._tag != "kanjis":
                raise ValueError, "The very first tag should be <kanjis>"

        if self._tag == "kanji":
            self._writing = Writing()
            self._utf8 = attrs["midashi"].encode("UTF-8")

        if self._tag == "stroke":
            self._stroke = Stroke()
            if attrs.has_key("path"):
                self._stroke_svg = attrs["path"].encode("UTF-8")
                svg_parser = SVG_Parser(self._stroke_svg) 
                svg_parser.parse()
                self._stroke.append_points(svg_parser.get_points())
            else:
                sys.stderr.write("Missing path in <stroke> element: " + self._utf8 + "\n")
Esempio n. 11
0
    def _start_element(self, name, attrs):
        self._tag = name

        if self._first_tag:
            self._first_tag = False
            if self._tag != "kanjivg":
                raise ValueError, "The very first tag should be <kanjivg>"

        if self._tag == "kanji":
            self._writing = Writing()
            self._utf8 = unichr(int(attrs["id"].split('_')[1],
                                    16)).encode("UTF-8")

        if self._tag == "path":
            self._stroke = Stroke()
            if attrs.has_key("d"):
                self._stroke_svg = attrs["d"].encode("UTF-8")
                svg_parser = SVG_Parser(self._stroke_svg)
                svg_parser.parse()
                self._stroke.append_points(svg_parser.get_points())
            else:
                sys.stderr.write("Missing data in <path> element: " +
                                 self._utf8 + "\n")
Esempio n. 12
0
    def testDuration(self):
        point = Point()
        point.x = 1
        point.y = 2
        point.timestamp = 0

        point2 = Point()
        point2.x = 4
        point2.y = 5
        point2.timestamp = 5

        stroke = Stroke()
        stroke.append_point(point)
        stroke.append_point(point2)

        point3 = Point()
        point3.x = 1
        point3.y = 2
        point3.timestamp = 7

        point4 = Point()
        point4.x = 4
        point4.y = 5
        point4.timestamp = 10

        stroke2 = Stroke()
        stroke2.append_point(point3)
        stroke2.append_point(point4)

        self.assertEquals(stroke2.get_duration(), 3)

        writing = Writing()
        writing.append_stroke(stroke)
        writing.append_stroke(stroke2)

        self.assertEquals(writing.get_duration(), 10)