2019-07-01 16:45:58 +02:00
|
|
|
#! /usr/bin/env python3
|
2019-03-21 22:54:06 +01:00
|
|
|
|
|
|
|
import getopt
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import fxconv
|
|
|
|
|
|
|
|
help_string = """
|
|
|
|
usage: fxconv [-s] <python script> [files...]
|
|
|
|
fxconv -b <bin file> -o <object file> [parameters...]
|
2019-08-04 14:04:09 +02:00
|
|
|
fxconv -i <png file> -o <object file> (--fx|--cg) [parameters...]
|
2019-03-21 22:54:06 +01:00
|
|
|
fxconv -f <png file> -o <object file> [parameters...]
|
|
|
|
|
|
|
|
fxconv converts data files such as images and fonts into gint formats
|
|
|
|
optimized for fast execution, or into object files.
|
|
|
|
|
|
|
|
Operating modes:
|
2020-03-11 19:37:27 +01:00
|
|
|
-s, --script Expose the fxconv module and run this Python script
|
|
|
|
-b, --binary Turn data into an object file, no conversion
|
|
|
|
-i, --image Convert to gint's bopti image format
|
|
|
|
-f, --font Convert to gint's topti font format
|
|
|
|
--libimg-image Convert to the libimg image format
|
2019-03-21 22:54:06 +01:00
|
|
|
|
|
|
|
When using -s, additional arguments are stored in the [fxconv.args] variable of
|
|
|
|
the module. This is intended to be a restricted list of file names specified by
|
|
|
|
a Makefile, used to convert only a subset of the files in the script.
|
|
|
|
|
2020-03-11 19:37:27 +01:00
|
|
|
The operating mode options are shortcuts to convert single files without a
|
|
|
|
script. They accept parameters with a "category.key:value" syntax, for example:
|
2019-03-21 22:54:06 +01:00
|
|
|
fxconv -f myfont.png -o myfont.o charset:ascii grid.padding:1 height:7
|
2019-08-04 14:04:09 +02:00
|
|
|
|
|
|
|
When converting images, use --fx (black-and-white calculators) or --cg (16-bit
|
|
|
|
color calculators) to specify the target machine.
|
2019-03-21 22:54:06 +01:00
|
|
|
""".strip()
|
|
|
|
|
|
|
|
# Simple error-warnings system
|
|
|
|
def err(msg):
|
2019-08-04 14:04:09 +02:00
|
|
|
print("\x1b[31;1merror:\x1b[0m", msg, file=sys.stderr)
|
2019-03-21 22:54:06 +01:00
|
|
|
def warn(msg):
|
2020-03-11 19:37:27 +01:00
|
|
|
print("\x1b[33;1mwarning:\x1b[0m", msg, file=sys.stderr)
|
2019-03-21 22:54:06 +01:00
|
|
|
|
|
|
|
def main():
|
|
|
|
# Default execution mode is to run a Python script for conversion
|
2020-03-11 19:37:27 +01:00
|
|
|
modes = "script binary image font bopti-image libimg-image"
|
2019-03-21 22:54:06 +01:00
|
|
|
mode = "s"
|
|
|
|
output = None
|
2019-09-09 09:18:59 +02:00
|
|
|
model = None
|
|
|
|
target = { 'toolchain': None, 'arch': None, 'section': None }
|
2019-03-21 22:54:06 +01:00
|
|
|
|
|
|
|
# Parse command-line arguments
|
|
|
|
|
|
|
|
if len(sys.argv) == 1:
|
|
|
|
print(help_string, file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
try:
|
|
|
|
opts, args = getopt.gnu_getopt(sys.argv[1:], "hsbifo:",
|
2019-09-09 09:18:59 +02:00
|
|
|
("help output= fx cg toolchain= arch= section= "+modes).split())
|
2019-03-21 22:54:06 +01:00
|
|
|
except getopt.GetoptError as error:
|
|
|
|
err(error)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
for name, value in opts:
|
|
|
|
# Print usage
|
|
|
|
if name == "--help":
|
|
|
|
err(help_string, file=sys.stderr)
|
|
|
|
sys.exit(0)
|
|
|
|
# TODO: fxconv: verbose mode
|
|
|
|
elif name == "--verbose":
|
|
|
|
pass
|
|
|
|
elif name in [ "-o", "--output" ]:
|
|
|
|
output = value
|
2019-08-04 14:04:09 +02:00
|
|
|
elif name in [ "--fx", "--cg" ]:
|
2019-09-09 09:18:59 +02:00
|
|
|
model = name[2:]
|
|
|
|
elif name == "--toolchain":
|
|
|
|
target['toolchain'] = value
|
|
|
|
elif name == "--arch":
|
|
|
|
target['arch'] = value
|
|
|
|
elif name == "--section":
|
|
|
|
target['section'] = value
|
2019-03-21 22:54:06 +01:00
|
|
|
# Other names are modes
|
|
|
|
else:
|
2020-03-11 19:37:27 +01:00
|
|
|
mode = name[1] if len(name)==2 else name[2:]
|
2019-03-21 22:54:06 +01:00
|
|
|
|
|
|
|
# Remaining arguments
|
|
|
|
if args == []:
|
|
|
|
err(f"execution mode -{mode} expects an input file")
|
|
|
|
sys.exit(1)
|
|
|
|
input = args.pop(0)
|
|
|
|
|
|
|
|
# In --script mode, run the Python script with an augmented PYTHONPATH
|
|
|
|
|
|
|
|
if mode == "s":
|
|
|
|
if output is not None:
|
|
|
|
warn("option --output is ignored in script mode")
|
|
|
|
args = None if args == [] else args
|
|
|
|
|
|
|
|
err("script mode not currently implemented (TODO) x_x")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# In shortcut conversion modes, read parameters from the command-line
|
|
|
|
|
|
|
|
else:
|
|
|
|
def check(arg):
|
|
|
|
if ':' not in arg:
|
|
|
|
warn(f"argument {arg} is not a valid parameter (ignored)")
|
|
|
|
return ':' in arg
|
|
|
|
|
|
|
|
def insert(params, path, value):
|
|
|
|
if len(path) == 1:
|
|
|
|
params[path[0]] = value
|
|
|
|
return
|
|
|
|
if not path[0] in params:
|
|
|
|
params[path[0]] = {}
|
|
|
|
insert(params[path[0]], path[1:], value)
|
|
|
|
|
|
|
|
args = [ arg.split(':', 1) for arg in args if check(arg) ]
|
|
|
|
params = {}
|
|
|
|
for (name, value) in args:
|
|
|
|
insert(params, name.split("."), value)
|
|
|
|
|
2020-03-11 19:37:27 +01:00
|
|
|
if "type" in params:
|
|
|
|
pass
|
|
|
|
elif(len(mode) == 1):
|
|
|
|
params["type"] = { "b": "binary", "i": "image", "f": "font" }[mode]
|
|
|
|
else:
|
|
|
|
params["type"] = mode
|
|
|
|
|
|
|
|
# Will be deprecated in the future
|
|
|
|
if params["type"] == "image":
|
|
|
|
warn("type 'image' is deprecated, use 'bopti-image' instead")
|
|
|
|
params["type"] = "bopti-image"
|
2019-03-21 22:54:06 +01:00
|
|
|
|
2019-08-04 14:04:09 +02:00
|
|
|
try:
|
2019-09-09 09:18:59 +02:00
|
|
|
fxconv.convert(input, params, target, output, model)
|
2019-08-04 14:04:09 +02:00
|
|
|
except fxconv.FxconvError as e:
|
|
|
|
err(e)
|
|
|
|
sys.exit(1)
|
2019-03-21 22:54:06 +01:00
|
|
|
|
2019-08-04 14:04:09 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|