File: Shaper.py

package info (click to toggle)
python-fontfeatures 1.9.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,096 kB
  • sloc: python: 9,112; makefile: 22
file content (287 lines) | stat: -rw-r--r-- 8,836 bytes parent folder | download
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
"""A Python Unicode Shaping Engine."""

from fontFeatures import FontFeatures
from .BaseShaper import BaseShaper
from .ArabicShaper import ArabicShaper
from .IndicShaper import IndicShaper
from .MyanmarShaper import MyanmarShaper
from .HangulShaper import HangulShaper
from .KhmerShaper import KhmerShaper
from .USEShaper import USEShaper
import logging
import re


class Shaper:
    """Initialize a shaping engine.

    Args:
        ff: A :py:class:`fontFeatures.FontFeatures` object.
        font: A ``Babelfont`` font object.
        message_function: A function called with a message and buffer object.
    """

    def __init__(self, ff, font, message_function=None):
        assert isinstance(ff, FontFeatures)
        self.fontfeatures = ff
        self.babelfont = font
        if message_function:
            self.msg = message_function
        else:
            self.msg = self.default_message_function

    def execute(self, buf, features=[]):
        """Run the shaper on a given buffer.

        Args:
            buf: :py:class:`fontFeatures.shaperLib.buffer.Buffer` object.
            features: either a list of feature tags or a feature string ("+foox,-barx")
        """
        # Choose complex shaper
        self.complexshaper = self.categorize(buf)(self, self.babelfont, buf, features)
        self.msg("Using %s" % type(self.complexshaper).__name__)
        self.stages = [[]]
        if isinstance(features, str):
            self.user_features = self._parse_user_feature_string(features)
        else:
            self.user_features = features
        self.collect_features(buf)
        self.complexshaper.shape()
        buf.clear_mask()
        return buf

    def default_message_function(self, msg, buffer=None, serialize_options=None):
        """A logger function to be used if one is not provided in the constructor.

        By default, debugging and informational log messages are reported to the
        ``fontFeatures.shaperLib`` facility."""
        ser = ""
        if buffer:
            ser = buffer.serialize(additional=serialize_options)
            msg = msg + " : " + ser
        logging.getLogger("fontFeatures.shaperLib").info(msg)

    def _parse_user_feature_string(self, features):
        features = features.split(",")
        outfeat = []
        for f in features:
            f = f.rstrip()
            m = re.match(r"^([+\-]?)(\w+)", f)
            if m:
                outfeat.append({"tag": m[2], "value": m[1] != "-"})
                continue
            m = re.match(r"^(\w+)=([10])", f)
            if m:
                outfeat.append({"tag": m[1], "value": m[2] == "1"})
            else:
                outfeat.append({"tag": f, "value": True})
        return outfeat

    def add_pause(self, thing=None):
        """Add a pause in the shaping stage. Used internally."""
        if thing:
            self.stages.append(thing)
        self.stages.append([])

    def add_features(self, *tags):
        """Adds feature tags to the current shaping stage."""
        for t in tags:
            if any([isinstance(x, list) and t in x for x in self.stages]):
                continue
            self.stages[-1].append(t)

    def disable_feature(self, tag):
        """Removes a feature from processing."""
        for s in self.stages:
            if isinstance(s, list) and tag in s:
                s.remove(tag)

    def collect_features(self, buf):
        """Determine the features, and their order, to process the buffer."""
        self.add_features("rvrn")
        self.add_pause()
        if buf.direction == "LTR":
            self.add_features("ltra", "ltrm")
        elif buf.direction == "RTL":
            self.add_features("rtla", "rtlm")
        self.add_features("frac", "numr", "dnom", "rand")
        # trak?
        self.complexshaper.collect_features(self)
        # common features
        self.add_features("abvm", "blwm", "ccmp", "locl", "mark", "mkmk", "rlig")
        if buf.direction == "LTR" or buf.direction == "RTL":
            self.add_features("calt", "clig", "curs", "dist", "kern", "liga", "rclt")
        else:
            self.add_features("vert")
        for uf in self.user_features:
            if not uf["value"]:  # Turn it off if it's already on
                self.disable_feature(uf["tag"])
            else:
                self.add_features(uf["tag"])
        if hasattr(self.complexshaper, "override_features"):
            self.complexshaper.override_features(self)

    def categorize(self, buf):
        """Returns the appropriate complex shaper class to shape this buffer."""
        if buf.script == "Arabic":
            return ArabicShaper

        connected_scripts = {
            "Mongolian": "mong",
            "Syriac": "syrc",
            "Nko": "nko ",
            "Phags_Pa": "phag",
            "Mandaic": "mand",
            "Manichaean": "mand",
            "Psalter_Pahlavi": "phlp",
            "Adlam": "adlm",
            "Hanifi_Rohingya": "rohg",
            "Sogdian": "sogd",
        }
        if buf.script in connected_scripts:
            if self.fontfeatures.hasScriptSupport(connected_scripts[buf.script]):
                return ArabicShaper
            else:
                return BaseShaper

        # if buf.script in ["Thai", "Lao"]:
        #     return ThaiShaper
        if buf.script == "Hangul":
            return HangulShaper
        # if buf.script == "Hebrew":
        #     return HebrewShaper
        if buf.script in [
            "Bengali",
            "Devanagari",
            "Gujarati",
            "Gurmukhi",
            "Kannada",
            "Malayalam",
            "Oriya",
            "Tamil",
            "Telugu",
            "Sinhala",
        ]:
            indic23map = {
                "Bengali": "bng",
                "Devanagari": "dev",
                "Gujarati": "gjr",
                "Gurmukhi": "gur",
                "Kannada": "knd",
                "Malayalam": "mlm",
                "Oriya": "ory",
                "Tamil": "tml",
                "Telugu": "tel",
            }
            # Sinhala is different
            if buf.script in indic23map and self.fontfeatures.hasScriptSupport(
                indic23map[buf.script] + "3"
            ):
                return USEShaper
            else:
                return IndicShaper
        if buf.script == "Khmer":
            return KhmerShaper

        if buf.script == "Myanmar":
            if self.fontfeatures.hasScriptSupport("mymr"):
                return BaseShaper
            else:
                return MyanmarShaper

        # if buf.script = "Qaag": return MyanmarZawgyiShaper
        if buf.script in [
            "Tibetan",
            "Buhid",
            "Hanunoo",
            "Tagalog",
            "Tagbanwa",
            "Limbu",
            "Tai_Le",
            "Buginese",
            "Kharoshthi",
            "Syloti_Nagri",
            "Tifinagh",
            "Balinese",
            "Cham",
            "Kayah_Li",
            "Lepcha",
            "Rejang",
            "Saurashtra",
            "Sundanese",
            "Egyptian_Hieroglyphs",
            "Javanese",
            "Kaithi",
            "Meetei_Mayek",
            "Tai_Tham",
            "Tai_Viet",
            "Batak",
            "Brahmi",
            "Chakma",
            "Sharada",
            "Takri",
            "Duployan",
            "Grantha",
            "Khojki",
            "Khudawadi",
            "Mahajani",
            "Modi",
            "Pahawh_Hmong",
            "Siddham",
            "Tirhuta",
            "Ahom",
            "Bhaiksuki",
            "Marchen",
            "Newa",
            "Masaram_Gondi",
            "Soyombo",
            "Zanabazar_Square",
            "Dogra",
            "Gunjala_Gondi",
            "Makasar",
            "Nandinagari",
        ]:
            return USEShaper
        return BaseShaper


def _script_direction(script):
    if script in [
        "Arabic",
        "Hebrew",
        "Syriac",
        "Thaana",
        "Cypriot",
        "Kharoshthi",
        "Phoenician",
        "Nko",
        "Lydian",
        "Avestan",
        "Imperial_Aramaic",
        "Inscriptional_Parthian",
        "Inscriptional_Pahlavi",
        "Old_South_Arabian",
        "Old_Turkic",
        "Samaritan",
        "Mandaic",
        "Meroitic_Cursive",
        "Meroitic_Hieroglyphs",
        "Manichaean",
        "Mende_Kikakui",
        "Nabataean",
        "Old_North_Arabian",
        "Palmyrene",
        "Psalter_Pahlavi",
        "Hatran",
        "Adlam",
        "Hanifi_Rohingya",
        "Old_Sogdian",
        "Sogdian",
        "Elymaic",
        "Chorasmian",
        "Yezidi",
    ]:
        return "RTL"
    if script in ["Old_Hungarian", "Old_Italic", "Runic"]:
        return "invalid"
    return "LTR"