[varLib.avarPlanner] Use proper logging
This commit is contained in:
parent
3efd204a06
commit
a95c084355
@ -2,7 +2,11 @@ from fontTools.ttLib import newTable
|
|||||||
from fontTools.pens.areaPen import AreaPen
|
from fontTools.pens.areaPen import AreaPen
|
||||||
from fontTools.varLib.models import piecewiseLinearMap
|
from fontTools.varLib.models import piecewiseLinearMap
|
||||||
from fontTools.misc.cliTools import makeOutputFileName
|
from fontTools.misc.cliTools import makeOutputFileName
|
||||||
from math import exp, log
|
import math
|
||||||
|
import logging
|
||||||
|
from pprint import pformat
|
||||||
|
|
||||||
|
log = logging.getLogger("fontTools.varLib.avarPlanner")
|
||||||
|
|
||||||
WEIGHTS = [
|
WEIGHTS = [
|
||||||
50,
|
50,
|
||||||
@ -53,7 +57,7 @@ def getGlyphsetBlackness(glyphset, frequencies=None):
|
|||||||
def planWeightAxis(
|
def planWeightAxis(
|
||||||
font, minValue, defaultValue, maxValue, weights=WEIGHTS, frequencies=None
|
font, minValue, defaultValue, maxValue, weights=WEIGHTS, frequencies=None
|
||||||
):
|
):
|
||||||
print("Weight min/default/max:", minValue, defaultValue, maxValue)
|
log.info("Weight min %g / default %g / max %g", minValue, defaultValue, maxValue)
|
||||||
|
|
||||||
out = {}
|
out = {}
|
||||||
outNormalized = {}
|
outNormalized = {}
|
||||||
@ -66,7 +70,7 @@ def planWeightAxis(
|
|||||||
upem * upem
|
upem * upem
|
||||||
)
|
)
|
||||||
|
|
||||||
print("Calculated average glyph black ratio:", axisWeightAverage)
|
log.info("Calculated average glyph black ratio:\n%s", pformat(axisWeightAverage))
|
||||||
|
|
||||||
outNormalized[-1] = -1
|
outNormalized[-1] = -1
|
||||||
for extremeValue in sorted({minValue, maxValue} - {defaultValue}):
|
for extremeValue in sorted({minValue, maxValue} - {defaultValue}):
|
||||||
@ -78,31 +82,31 @@ def planWeightAxis(
|
|||||||
|
|
||||||
bias = -1 if extremeValue < defaultValue else 0
|
bias = -1 if extremeValue < defaultValue else 0
|
||||||
|
|
||||||
print("Planning target weights", sorted(targetWeights))
|
log.info("Planning target weights %s", sorted(targetWeights))
|
||||||
print("Sampling", SAMPLES, "points in range", rangeMin, rangeMax)
|
log.info("Sampling %u points in range %g,%g", SAMPLES, rangeMin, rangeMax)
|
||||||
weightBlackness = axisWeightAverage.copy()
|
weightBlackness = axisWeightAverage.copy()
|
||||||
for sample in range(1, SAMPLES + 1):
|
for sample in range(1, SAMPLES + 1):
|
||||||
weight = rangeMin + (rangeMax - rangeMin) * sample / (SAMPLES + 1)
|
weight = rangeMin + (rangeMax - rangeMin) * sample / (SAMPLES + 1)
|
||||||
print("Sampling weight", weight)
|
log.info("Sampling weight %g", weight)
|
||||||
glyphset = font.getGlyphSet(location={"wght": weight})
|
glyphset = font.getGlyphSet(location={"wght": weight})
|
||||||
weightBlackness[weight] = getGlyphsetBlackness(glyphset, frequencies) / (
|
weightBlackness[weight] = getGlyphsetBlackness(glyphset, frequencies) / (
|
||||||
upem * upem
|
upem * upem
|
||||||
)
|
)
|
||||||
print("Sampled average glyph black ratio:", weightBlackness)
|
log.info("Sampled average glyph black ratio:\n%s", pformat(weightBlackness))
|
||||||
|
|
||||||
blacknessWeight = {}
|
blacknessWeight = {}
|
||||||
for weight in sorted(weightBlackness):
|
for weight in sorted(weightBlackness):
|
||||||
blacknessWeight[weightBlackness[weight]] = weight
|
blacknessWeight[weightBlackness[weight]] = weight
|
||||||
|
|
||||||
logMin = log(weightBlackness[rangeMin])
|
logMin = math.log(weightBlackness[rangeMin])
|
||||||
logMax = log(weightBlackness[rangeMax])
|
logMax = math.log(weightBlackness[rangeMax])
|
||||||
out[rangeMin] = rangeMin
|
out[rangeMin] = rangeMin
|
||||||
outNormalized[bias] = bias
|
outNormalized[bias] = bias
|
||||||
for weight in sorted(targetWeights):
|
for weight in sorted(targetWeights):
|
||||||
t = (weight - rangeMin) / (rangeMax - rangeMin)
|
t = (weight - rangeMin) / (rangeMax - rangeMin)
|
||||||
targetBlackness = exp(logMin + t * (logMax - logMin))
|
targetBlackness = math.exp(logMin + t * (logMax - logMin))
|
||||||
targetWeight = piecewiseLinearMap(targetBlackness, blacknessWeight)
|
targetWeight = piecewiseLinearMap(targetBlackness, blacknessWeight)
|
||||||
print("Planned mapping weight %g to %g" % (weight, targetWeight))
|
log.info("Planned mapping weight %g to %g" % (weight, targetWeight))
|
||||||
out[weight] = targetWeight
|
out[weight] = targetWeight
|
||||||
outNormalized[t + bias] = (targetWeight - rangeMin) / (
|
outNormalized[t + bias] = (targetWeight - rangeMin) / (
|
||||||
rangeMax - rangeMin
|
rangeMax - rangeMin
|
||||||
@ -117,12 +121,14 @@ def planWeightAxis(
|
|||||||
# )
|
# )
|
||||||
# pyplot.show()
|
# pyplot.show()
|
||||||
|
|
||||||
print("Planned mapping:", out)
|
log.info("Planned mapping:\n%s", pformat(out))
|
||||||
print("Planned normalized mapping:", outNormalized)
|
log.info("Planned normalized mapping:\n%s", pformat(outNormalized))
|
||||||
return out, outNormalized
|
return out, outNormalized
|
||||||
|
|
||||||
|
|
||||||
def main(args=None):
|
def main(args=None):
|
||||||
|
from fontTools import configLogger
|
||||||
|
|
||||||
if args is None:
|
if args is None:
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
@ -137,8 +143,20 @@ def main(args=None):
|
|||||||
)
|
)
|
||||||
parser.add_argument("font", metavar="font.ttf", help="Font file.")
|
parser.add_argument("font", metavar="font.ttf", help="Font file.")
|
||||||
|
|
||||||
|
logging_group = parser.add_mutually_exclusive_group(required=False)
|
||||||
|
logging_group.add_argument(
|
||||||
|
"-v", "--verbose", action="store_true", help="Run more verbosely."
|
||||||
|
)
|
||||||
|
logging_group.add_argument(
|
||||||
|
"-q", "--quiet", action="store_true", help="Turn verbosity off."
|
||||||
|
)
|
||||||
|
|
||||||
options = parser.parse_args(args)
|
options = parser.parse_args(args)
|
||||||
|
|
||||||
|
configLogger(
|
||||||
|
level=("DEBUG" if options.verbose else "ERROR" if options.quiet else "INFO")
|
||||||
|
)
|
||||||
|
|
||||||
font = TTFont(options.font)
|
font = TTFont(options.font)
|
||||||
fvar = font["fvar"]
|
fvar = font["fvar"]
|
||||||
wghtAxis = slntAxis = None
|
wghtAxis = slntAxis = None
|
||||||
@ -159,15 +177,15 @@ def main(args=None):
|
|||||||
)
|
)
|
||||||
|
|
||||||
if existingMapping is not None:
|
if existingMapping is not None:
|
||||||
print("Existing weight mapping:", existingMapping)
|
log.info("Existing weight mapping:\n%s", pformat(existingMapping))
|
||||||
|
|
||||||
if "avar" not in font:
|
if "avar" not in font:
|
||||||
font["avar"] = newTable("avar")
|
font["avar"] = newTable("avar")
|
||||||
avar = font["avar"]
|
avar = font["avar"]
|
||||||
avar.segments["wght"] = mapping
|
avar.segments["wght"] = mapping
|
||||||
|
|
||||||
print("Saving font")
|
|
||||||
outfile = makeOutputFileName(options.font, overWrite=True, suffix=".avar")
|
outfile = makeOutputFileName(options.font, overWrite=True, suffix=".avar")
|
||||||
|
log.info("Saving %s", outfile)
|
||||||
font.save(outfile)
|
font.save(outfile)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user