Skip to content

Commit

Permalink
make -w and -a mutually exclusive; print deprecation warning
Browse files Browse the repository at this point in the history
  • Loading branch information
anthrotype committed Jan 2, 2018
1 parent b4b7b91 commit e2872b4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 34 deletions.
39 changes: 23 additions & 16 deletions src/python/ttfautohint/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ def strong_stem_width(s):
raise argparse.ArgumentTypeError(
"string can only contain up to 3 letters")
valid = {
"g": "gray_strong_stem_width",
"G": "gdi_cleartype_strong_stem_width",
"D": "dw_cleartype_strong_stem_width"}
"g": "gray_stem_width_mode",
"G": "gdi_cleartype_stem_width_mode",
"D": "dw_cleartype_stem_width_mode"}
chars = set(s)
invalid = chars - set(valid)
if invalid:
Expand All @@ -214,7 +214,9 @@ def strong_stem_width(s):
repr(v) for v in sorted(invalid)))
result = {}
for char, opt_name in valid.items():
result[opt_name] = char in chars
is_strong = char in chars
result[opt_name] = (StemWidthMode.STRONG if is_strong
else StemWidthMode.QUANTIZED)
return result


Expand Down Expand Up @@ -313,13 +315,19 @@ def parse_args(args=None):
help="output file (default: standard output)")
parser.add_argument(
"--debug", action="store_true", help="print debugging information")
parser.add_argument(

stem_width_group = parser.add_mutually_exclusive_group(required=False)
stem_width_group.add_argument(
"-a", "--stem-width-mode", type=stem_width_mode, metavar="S",
default=STEM_WIDTH_MODE_OPTIONS,
help=("select stem width mode for grayscale, GDI ClearType, and DW "
"ClearType, where S is a string of three letters with possible "
"values 'n' for natural, 'q' for quantized, and 's' for strong "
"(default: qsq)"))
stem_width_group.add_argument( # deprecated
"-w", "--strong-stem-width", type=strong_stem_width, metavar="S",
help=argparse.SUPPRESS)

parser.add_argument(
"-c", "--composites", dest="hint_composites", action="store_true",
help="hint glyph composites also")
Expand Down Expand Up @@ -393,13 +401,6 @@ def parse_args(args=None):
"-V", "--version", action="version",
version=version_string,
help="print version information and exit")
parser.add_argument(
"-w", "--strong-stem-width", type=strong_stem_width, metavar="S",
default=STRONG_STEM_WIDTH_OPTIONS,
help=("use strong stem width routine for modes S, where S is a "
"string of up to three letters with possible values `g' for "
"grayscale, `G' for GDI ClearType, and `D' for DirectWrite "
"ClearType (default: G)"))
parser.add_argument(
"-W", "--windows-compatibility", action="store_true",
help=("add blue zones for `usWinAscent' and `usWinDescent' to avoid "
Expand Down Expand Up @@ -438,15 +439,21 @@ def parse_args(args=None):
try:
options["epoch"] = int(source_date_epoch)
except ValueError:
import logging
log = logging.getLogger("ttfautohint")
log.warning("invalid SOURCE_DATE_EPOCH: %r", source_date_epoch)
import warnings
warnings.warn(
UserWarning("invalid SOURCE_DATE_EPOCH: %r" % source_date_epoch))

if options.pop("show_TTFA_info"):
# TODO use fonttools to dump TTFA table?
raise NotImplementedError()

stem_width_options = options.pop("strong_stem_width")
stem_width_options = options.pop("stem_width_mode")
strong_stem_width_options = options.pop("strong_stem_width")
if strong_stem_width_options:
import warnings
warnings.warn(
UserWarning("Option '-w' is deprecated! Use option '-a' instead"))
stem_width_options = strong_stem_width_options
options.update(stem_width_options)

return options
36 changes: 18 additions & 18 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,41 +284,41 @@ def test_format_varargs(options, expected):
(
"",
{
"gray_strong_stem_width": False,
"gdi_cleartype_strong_stem_width": False,
"dw_cleartype_strong_stem_width": False
"gray_stem_width_mode": StemWidthMode.QUANTIZED,
"gdi_cleartype_stem_width_mode": StemWidthMode.QUANTIZED,
"dw_cleartype_stem_width_mode": StemWidthMode.QUANTIZED
}
),
(
"g",
{
"gray_strong_stem_width": True,
"gdi_cleartype_strong_stem_width": False,
"dw_cleartype_strong_stem_width": False
"gray_stem_width_mode": StemWidthMode.STRONG,
"gdi_cleartype_stem_width_mode": StemWidthMode.QUANTIZED,
"dw_cleartype_stem_width_mode": StemWidthMode.QUANTIZED
}
),
(
"G",
{
"gray_strong_stem_width": False,
"gdi_cleartype_strong_stem_width": True,
"dw_cleartype_strong_stem_width": False
"gray_stem_width_mode": StemWidthMode.QUANTIZED,
"gdi_cleartype_stem_width_mode": StemWidthMode.STRONG,
"dw_cleartype_stem_width_mode": StemWidthMode.QUANTIZED
}
),
(
"D",
{
"gray_strong_stem_width": False,
"gdi_cleartype_strong_stem_width": False,
"dw_cleartype_strong_stem_width": True
"gray_stem_width_mode": StemWidthMode.QUANTIZED,
"gdi_cleartype_stem_width_mode": StemWidthMode.QUANTIZED,
"dw_cleartype_stem_width_mode": StemWidthMode.STRONG
}
),
(
"DGg",
{
"gray_strong_stem_width": True,
"gdi_cleartype_strong_stem_width": True,
"dw_cleartype_strong_stem_width": True
"gray_stem_width_mode": StemWidthMode.STRONG,
"gdi_cleartype_stem_width_mode": StemWidthMode.STRONG,
"dw_cleartype_stem_width_mode": StemWidthMode.STRONG
}
),
],
Expand Down Expand Up @@ -514,17 +514,17 @@ def test_source_date_epoch(self, monkeypatch):

assert options["epoch"] == int(epoch)

def test_source_date_epoch_invalid(self, monkeypatch, caplog):
def test_source_date_epoch_invalid(self, monkeypatch):
invalid_epoch = "foobar"
env = dict(os.environ)
env["SOURCE_DATE_EPOCH"] = invalid_epoch
monkeypatch.setattr(os, "environ", env)

with caplog.at_level(logging.WARNING):
with pytest.warns(UserWarning,
match="invalid SOURCE_DATE_EPOCH: 'foobar'"):
options = parse_args([])

assert "epoch" not in options
assert "invalid SOURCE_DATE_EPOCH: 'foobar'" in caplog.text

def test_show_ttfa_info_unsupported(self):
with pytest.raises(NotImplementedError):
Expand Down

0 comments on commit e2872b4

Please sign in to comment.