| 1 |
#!/usr/bin/env python3 |
| 2 |
from fontFeatures.shaperLib.Buffer import Buffer |
| 3 |
from fontFeatures.ttLib import unparse |
| 4 |
from fontTools.ttLib import TTFont |
| 5 |
from fontFeatures.shaperLib.Shaper import Shaper |
| 6 |
import sys |
| 7 |
from babelfont import load |
| 8 |
from fontTools.ttLib import TTFont |
| 9 |
import argparse |
| 10 |
import logging |
| 11 |
import re |
| 12 |
|
| 13 |
def main(args=None): |
| 14 |
parser = argparse.ArgumentParser(description='Shape text') |
| 15 |
parser.add_argument('font', metavar='OTF', |
| 16 |
help='Font file') |
| 17 |
parser.add_argument('--verbose', '-V', action="count", dest='logging', |
| 18 |
help='Output interim shaping results') |
| 19 |
parser.add_argument("--ned", "--no-extra-data", dest="ned", action='store_true', help="No Extra Data; Do not output clusters or advances") |
| 20 |
parser.add_argument("--no-glyph-names", dest="ngn", action='store_true', help="Output glyph indices instead of names") |
| 21 |
parser.add_argument("--no-positions", dest="np", action='store_true', help="Do not output glyph positions") |
| 22 |
parser.add_argument("--additional", help='Additional information') |
| 23 |
parser.add_argument('--features', help='Feature string') |
| 24 |
group = parser.add_mutually_exclusive_group(required=True) |
| 25 |
group.add_argument('-u', help='Unicodes') |
| 26 |
group.add_argument('string', metavar='STRING', |
| 27 |
help='Text to shape', nargs="?") |
| 28 |
|
| 29 |
args = parser.parse_args(args) |
| 30 |
|
| 31 |
logging.basicConfig(format='%(message)s') |
| 32 |
|
| 33 |
if args.logging: |
| 34 |
logging.getLogger("fontFeatures.shaperLib").setLevel(logging.INFO) |
| 35 |
if args.logging >= 2: |
| 36 |
logging.getLogger("fontFeatures.shaperLib").setLevel(logging.DEBUG) |
| 37 |
|
| 38 |
font = load(args.font) |
| 39 |
if args.font.endswith(".ttf") or args.font.endswith(".otf"): |
| 40 |
ff = unparse(TTFont(args.font)) |
| 41 |
if args.u: |
| 42 |
args.u = re.sub("[uU]+|0x", "", args.u) |
| 43 |
splitup = re.split(r"[\s,]", args.u) |
| 44 |
args.string = "".join([chr(int(x,16)) for x in splitup]) |
| 45 |
|
| 46 |
buf = Buffer(font, unicodes=args.string) |
| 47 |
shaper = Shaper(ff, font) |
| 48 |
if args.features: |
| 49 |
shaper.execute(buf, features=args.features) |
| 50 |
else: |
| 51 |
shaper.execute(buf) |
| 52 |
serialize_options = {} |
| 53 |
if args.ngn: |
| 54 |
serialize_options["names"] = False |
| 55 |
if args.ned: |
| 56 |
serialize_options["ned"] = True |
| 57 |
if args.np: |
| 58 |
serialize_options["position"] = False |
| 59 |
if args.additional: |
| 60 |
serialize_options["additional"] = args.additional |
| 61 |
if buf.direction == "RTL": |
| 62 |
buf.items = list(reversed(buf.items)) |
| 63 |
print(buf.serialize(**serialize_options)) |
| 64 |
|
| 65 |
if __name__ == "__main__": |
| 66 |
main() |