[ttx] support reading font/xml file from standard input as '-'

Fixes https://github.com/fonttools/fonttools/issues/3019
This commit is contained in:
Cosimo Lupo 2023-03-03 14:04:32 +00:00
parent 48f68877a8
commit 15b450e4a6
No known key found for this signature in database
GPG Key ID: DF65A8A5A119C9A8
2 changed files with 30 additions and 12 deletions

View File

@ -146,11 +146,13 @@ class TTFont(object):
else:
# assume "file" is a readable file object
closeStream = False
file.seek(0)
if file.seekable():
file.seek(0)
if not self.lazy:
# read input file in memory and wrap a stream around it to allow overwriting
file.seek(0)
if file.seekable():
file.seek(0)
tmp = BytesIO(file.read())
if hasattr(file, "name"):
# save reference to input file name

View File

@ -278,7 +278,13 @@ def ttList(input, output, options):
@Timer(log, "Done dumping TTX in %(time).3f seconds")
def ttDump(input, output, options):
log.info('Dumping "%s" to "%s"...', input, output)
input_name = input
if input == "-":
input, input_name = sys.stdin.buffer, sys.stdin.name
output_name = output
if output == "-":
output, output_name = sys.stdout, sys.stdout.name
log.info('Dumping "%s" to "%s"...', input_name, output_name)
if options.unicodedata:
setUnicodeData(options.unicodedata)
ttf = TTFont(
@ -302,7 +308,13 @@ def ttDump(input, output, options):
@Timer(log, "Done compiling TTX in %(time).3f seconds")
def ttCompile(input, output, options):
log.info('Compiling "%s" to "%s"...' % (input, output))
input_name = input
if input == "-":
input, input_name = sys.stdin, sys.stdin.name
output_name = output
if output == "-":
output, output_name = sys.stdout.buffer, sys.stdout.name
log.info('Compiling "%s" to "%s"...' % (input_name, output))
if options.useZopfli:
from fontTools.ttLib import sfnt
@ -315,7 +327,7 @@ def ttCompile(input, output, options):
)
ttf.importXML(input)
if options.recalcTimestamp is None and "head" in ttf:
if options.recalcTimestamp is None and "head" in ttf and input is not sys.stdin:
# use TTX file modification time for head "modified" timestamp
mtime = os.path.getmtime(input)
ttf["head"].modified = timestampSinceEpoch(mtime)
@ -324,12 +336,16 @@ def ttCompile(input, output, options):
def guessFileType(fileName):
base, ext = os.path.splitext(fileName)
try:
with open(fileName, "rb") as f:
header = f.read(256)
except IOError:
return None
if fileName == "-":
header = sys.stdin.buffer.peek(256)
ext = ""
else:
base, ext = os.path.splitext(fileName)
try:
with open(fileName, "rb") as f:
header = f.read(256)
except IOError:
return None
if header.startswith(b"\xef\xbb\xbf<?xml"):
header = header.lstrip(b"\xef\xbb\xbf")
@ -381,7 +397,7 @@ def parseOptions(args):
raise getopt.GetoptError("Must specify at least one input file")
for input in files:
if not os.path.isfile(input):
if input != "-" and not os.path.isfile(input):
raise getopt.GetoptError('File not found: "%s"' % input)
tp = guessFileType(input)
if tp in ("OTF", "TTF", "TTC", "WOFF", "WOFF2"):