Imported Upstream version 2.6.7
[platform/upstream/harfbuzz.git] / src / gen-vowel-constraints.py
index 8ca90c8..7f23dc8 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 
 """Generator of the function to prohibit certain vowel sequences.
 
@@ -6,29 +6,25 @@ It creates ``_hb_preprocess_text_vowel_constraints``, which inserts dotted
 circles into sequences prohibited by the USE script development spec.
 This function should be used as the ``preprocess_text`` of an
 ``hb_ot_complex_shaper_t``.
-"""
 
-from __future__ import absolute_import, division, print_function, unicode_literals
+usage: ./gen-vowel-constraints.py ms-use/IndicShapingInvalidCluster.txt Scripts.txt
+
+Input file:
+* https://unicode.org/Public/UCD/latest/ucd/Scripts.txt
+"""
 
 import collections
-try:
-       from HTMLParser import HTMLParser
-       def write (s):
-               print (s.encode ('utf-8'), end='')
-except ImportError:
-       from html.parser import HTMLParser
-       def write (s):
-               sys.stdout.flush ()
-               sys.stdout.buffer.write (s.encode ('utf-8'))
+from html.parser import HTMLParser
+def write (s):
+       sys.stdout.flush ()
+       sys.stdout.buffer.write (s.encode ('utf-8'))
 import itertools
-import io
 import sys
 
 if len (sys.argv) != 3:
-       print ('usage: ./gen-vowel-constraints.py HBIndicVowelConstraints.txt Scripts.txt', file=sys.stderr)
-       sys.exit (1)
+       sys.exit (__doc__)
 
-with io.open (sys.argv[2], encoding='utf-8') as f:
+with open (sys.argv[2], encoding='utf-8') as f:
        scripts_header = [f.readline () for i in range (2)]
        scripts = {}
        script_order = {}
@@ -84,7 +80,8 @@ class ConstraintSet (object):
                        else:
                                self._c[first] = ConstraintSet (rest)
 
-       def _indent (self, depth):
+       @staticmethod
+       def _indent (depth):
                return ('  ' * depth).replace ('        ', '\t')
 
        def __str__ (self, index=0, depth=4):
@@ -92,19 +89,22 @@ class ConstraintSet (object):
                indent = self._indent (depth)
                if isinstance (self._c, list):
                        if len (self._c) == 0:
+                               assert index == 2, 'Cannot use `matched` for this constraint; the general case has not been implemented'
                                s.append ('{}matched = true;\n'.format (indent))
                        elif len (self._c) == 1:
+                               assert index == 1, 'Cannot use `matched` for this constraint; the general case has not been implemented'
                                s.append ('{}matched = 0x{:04X}u == buffer->cur ({}).codepoint;\n'.format (indent, next (iter (self._c)), index or ''))
                        else:
-                               s.append ('{}if (0x{:04X}u == buffer->cur ({}).codepoint &&\n'.format (indent, self._c[0], index))
-                               s.append ('{}buffer->idx + {} < count &&\n'.format (self._indent (depth + 2), len (self._c)))
+                               s.append ('{}if (0x{:04X}u == buffer->cur ({}).codepoint &&\n'.format (indent, self._c[0], index or ''))
+                               if index:
+                                       s.append ('{}buffer->idx + {} < count &&\n'.format (self._indent (depth + 2), index + 1))
                                for i, cp in enumerate (self._c[1:], start=1):
                                        s.append ('{}0x{:04X}u == buffer->cur ({}).codepoint{}\n'.format (
                                                self._indent (depth + 2), cp, index + i, ')' if i == len (self._c) - 1 else ' &&'))
                                s.append ('{}{{\n'.format (indent))
-                               for i in range (len (self._c)):
+                               for i in range (index):
                                        s.append ('{}buffer->next_glyph ();\n'.format (self._indent (depth + 1)))
-                               s.append ('{}_output_dotted_circle (buffer);\n'.format (self._indent (depth + 1)))
+                               s.append ('{}matched = true;\n'.format (self._indent (depth + 1)))
                                s.append ('{}}}\n'.format (indent))
                else:
                        s.append ('{}switch (buffer->cur ({}).codepoint)\n'.format(indent, index or ''))
@@ -127,8 +127,13 @@ class ConstraintSet (object):
                return ''.join (s)
 
 constraints = {}
-with io.open (sys.argv[1], encoding='utf-8') as f:
-       constraints_header = [f.readline ().strip () for i in range (2)]
+with open (sys.argv[1], encoding='utf-8') as f:
+       constraints_header = []
+       while True:
+               line = f.readline ().strip ()
+               if line == '#':
+                       break
+               constraints_header.append(line)
        for line in f:
                j = line.find ('#')
                if j >= 0:
@@ -147,7 +152,7 @@ print ('/* == Start of generated functions == */')
 print ('/*')
 print (' * The following functions are generated by running:')
 print (' *')
-print (' *   %s use Scripts.txt' % sys.argv[0])
+print (' *   %s ms-use/IndicShapingInvalidCluster.txt Scripts.txt' % sys.argv[0])
 print (' *')
 print (' * on files with these headers:')
 print (' *')
@@ -185,7 +190,7 @@ print ('_hb_preprocess_text_vowel_constraints (const hb_ot_shape_plan_t *plan HB
 print ('\t\t\t\t       hb_buffer_t              *buffer,')
 print ('\t\t\t\t       hb_font_t                *font HB_UNUSED)')
 print ('{')
-print ('#if defined(HB_NO_OT_SHAPE_COMPLEX_VOWEL_CONSTRAINTS)')
+print ('#ifdef HB_NO_OT_SHAPE_COMPLEX_VOWEL_CONSTRAINTS')
 print ('  return;')
 print ('#endif')
 print ('  if (buffer->flags & HB_BUFFER_FLAG_DO_NOT_INSERT_DOTTED_CIRCLE)')