summaryrefslogtreecommitdiff
path: root/gerbonara/newstroke.py
blob: c2f3d55e2c23a395c1c4d5c5eb7da3bb44452546 (plain)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python

from pathlib import Path
import unicodedata
import re
import ast
from functools import lru_cache
import math
from importlib.resources import files

from . import data
from .utils import rotate_point, Tag


STROKE_FONT_SCALE = 1/21
FONT_OFFSET = -10
DEFAULT_SPACE_WIDTH = 0.6
DEFAULT_CHAR_GAP = 0.2

_dec = lambda c: ord(c)-ord('R') 


class Newstroke:
    def __init__(self, newstroke_cpp=None):
        if newstroke_cpp is None:
            newstroke_cpp = files(data).joinpath('newstroke_font.cpp').read_bytes()
        self.glyphs = dict(self.load_font(newstroke_cpp))

    @classmethod
    @lru_cache
    def load(kls):
        return kls()

    def render(self, text, size=1.0, x0=0, y0=0, rotation=0, h_align='left', v_align='bottom', space_width=DEFAULT_SPACE_WIDTH, char_gap=DEFAULT_CHAR_GAP, scale=(1, 1), mirror=(False, False)):
        text = unicodedata.normalize('NFC', text)
        missing_glyph = self.glyphs['?']
        sx, sy = scale
        mx, my = mirror
        x = 0
        if text in ('VDDA', 'PA9', 'VSS', 'FB3'):
            print(text, x0, y0, rotation, h_align, v_align, scale)

        if rotation >= 180:
            rotation -= 180
            h_align = {'left': 'right', 'right': 'left'}.get(h_align, h_align)
            x0, y0 = -x0, y0

        if scale == (1, 1) and rotation == 90:
            rotation = 270
            h_align = {'left': 'right', 'right': 'left'}.get(h_align, h_align)
            v_align = {'top': 'bottom', 'bottom': 'top'}.get(v_align, v_align)

        #if mx:
        #    x0 = -x0
        #    if rotation == 90:
        #        v_align = {'top': 'bottom', 'bottom': 'top'}.get(v_align, v_align)
        #    else:
        #        h_align = {'left': 'right', 'right': 'left'}.get(h_align, h_align)

        if my:
            y0 = -y0
            if rotation == 0:
                v_align = {'top': 'bottom', 'bottom': 'top'}.get(v_align, v_align)
            else:
                h_align = {'left': 'right', 'right': 'left'}.get(h_align, h_align)

        x0, y0 = rotate_point(x0, y0, math.radians(-rotation))

        alx, aly = 0, 0
        if h_align != 'left':
            (minx, miny), (maxx, maxy) = bbox = self.bounding_box(text, size, space_width, char_gap)
            w = maxx - minx
            if h_align == 'right':
                alx = -w
            elif h_align == 'center':
                alx = -w/2
            else:
                raise ValueError(f'Invalid h_align value "{h_align}"')

        if v_align == 'top':
            aly = sy*1.2*size
        elif v_align == 'middle':
            aly = sy*1.2*size/2
        elif v_align != 'bottom':
                raise ValueError(f'Invalid v_align value "{v_align}"')

        for c in text:
            if c == ' ':
                x += space_width*size
                continue

            width, strokes = self.glyphs.get(c, missing_glyph)
            glyph_w = max(width, max(x for st in strokes for x, _y in st))

            for st in strokes:
                yield self.transform_stroke(st, translate=(x0, y0), offset=(x+alx, aly), rotation=math.radians(-rotation), scale=(sx*size, sy*size))

            x += glyph_w*size

    def render_svg(self, text, size=1.0, x0=0, y0=0, rotation=0, h_align='left', v_align='bottom', space_width=DEFAULT_SPACE_WIDTH, char_gap=DEFAULT_CHAR_GAP, scale=(1, -1), mirror=(False, False), **svg_attrs):
        if 'stroke_linecap' not in svg_attrs:
            svg_attrs['stroke_linecap'] = 'round'
        if 'stroke_linejoin' not in svg_attrs:
            svg_attrs['stroke_linejoin'] = 'round'
        if 'stroke_width' not in svg_attrs:
            svg_attrs['stroke_width'] = f'{0.2*size:.3f}'
        svg_attrs['fill'] = 'none'

        strokes = ['M ' + ' L '.join(f'{x:.3f} {y:.3f}' for x, y in stroke)
                   for stroke in self.render(text, size=size, x0=x0, y0=y0, rotation=rotation, h_align=h_align,
                                             v_align=v_align, mirror=mirror, space_width=space_width, char_gap=char_gap,
                                             scale=scale)]
        return Tag('path', d=' '.join(strokes), **svg_attrs)

    def bounding_box(self, text, size=1.0, space_width=DEFAULT_SPACE_WIDTH, char_gap=DEFAULT_CHAR_GAP):
        text = unicodedata.normalize('NFC', text)
        missing_glyph = self.glyphs['?']
        x = 0
        for c in text:
            if c == ' ':
                x += space_width*size
                continue

            width, strokes = self.glyphs.get(c, missing_glyph)
            glyph_w = max(width, max(x for st in strokes for x, _y in st))
            x += glyph_w*size

        return (0, -0.2*size), (x, 1.2*size)

    @classmethod
    def transform_stroke(kls, stroke, translate, offset, scale, rotation=0):
        x0, y0 = translate
        sx, sy = scale
        dx, dy = offset
        return [rotate_point(x*sx+dx+x0, y*sy+dy+y0, rotation, x0, y0) for x, y in stroke]
            

    def load_font(self, newstroke_cpp):
        e = []
        for char, (width, strokes) in self.load_glyphs(newstroke_cpp):
            yield char, (width, strokes)

    @classmethod
    def decode_stroke(kls, stroke, start_x):
        for i in range(0, len(stroke), 2):
            x = (stroke[i]-0x52-start_x)*STROKE_FONT_SCALE
            y = (stroke[i+1]-0x52+FONT_OFFSET)*STROKE_FONT_SCALE
            yield (x, y)

    @classmethod
    def decode_glyph(kls, data):
        start_x, end_x = data[0]-0x52, data[1]-0x52
        width = end_x - start_x

        strokes = tuple(tuple(kls.decode_stroke(st, start_x)) for st in data[2:].split(b' R'))
        return width*STROKE_FONT_SCALE, strokes

    @classmethod
    def load_glyphs(kls, newstroke_cpp):
        it = iter(newstroke_cpp.splitlines())
        
        for line in it:
            if re.search(rb'char.*\*', line):
                break

        charcode = 0x20
        for line in it:
            if (match := re.search(rb'".*"', line)):
                yield chr(charcode), kls.decode_glyph(match.group(0)[1:-1].replace(b'\\\\', b'\\'))
                charcode += 1
            else:
                if b'}' in line:
                    break


if __name__ == '__main__':
    import time
    t1 = time.time()
    Newstroke()
    t2 = time.time()
    print((t2-t1)*1000)