new ip
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
# Copyright 2016 Google Inc. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from .cu2qu import *
|
||||
@@ -0,0 +1,6 @@
|
||||
import sys
|
||||
from .cli import _main as main
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
@@ -0,0 +1,54 @@
|
||||
"""Benchmark the cu2qu algorithm performance."""
|
||||
|
||||
from .cu2qu import *
|
||||
import random
|
||||
import timeit
|
||||
|
||||
MAX_ERR = 0.05
|
||||
|
||||
|
||||
def generate_curve():
|
||||
return [
|
||||
tuple(float(random.randint(0, 2048)) for coord in range(2))
|
||||
for point in range(4)
|
||||
]
|
||||
|
||||
|
||||
def setup_curve_to_quadratic():
|
||||
return generate_curve(), MAX_ERR
|
||||
|
||||
|
||||
def setup_curves_to_quadratic():
|
||||
num_curves = 3
|
||||
return ([generate_curve() for curve in range(num_curves)], [MAX_ERR] * num_curves)
|
||||
|
||||
|
||||
def run_benchmark(module, function, setup_suffix="", repeat=5, number=1000):
|
||||
setup_func = "setup_" + function
|
||||
if setup_suffix:
|
||||
print("%s with %s:" % (function, setup_suffix), end="")
|
||||
setup_func += "_" + setup_suffix
|
||||
else:
|
||||
print("%s:" % function, end="")
|
||||
|
||||
def wrapper(function, setup_func):
|
||||
function = globals()[function]
|
||||
setup_func = globals()[setup_func]
|
||||
|
||||
def wrapped():
|
||||
return function(*setup_func())
|
||||
|
||||
return wrapped
|
||||
|
||||
results = timeit.repeat(wrapper(function, setup_func), repeat=repeat, number=number)
|
||||
print("\t%5.1fus" % (min(results) * 1000000.0 / number))
|
||||
|
||||
|
||||
def main():
|
||||
run_benchmark("cu2qu", "curve_to_quadratic")
|
||||
run_benchmark("cu2qu", "curves_to_quadratic")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
random.seed(1)
|
||||
main()
|
||||
@@ -0,0 +1,198 @@
|
||||
import os
|
||||
import argparse
|
||||
import logging
|
||||
import shutil
|
||||
import multiprocessing as mp
|
||||
from contextlib import closing
|
||||
from functools import partial
|
||||
|
||||
import fontTools
|
||||
from .ufo import font_to_quadratic, fonts_to_quadratic
|
||||
|
||||
ufo_module = None
|
||||
try:
|
||||
import ufoLib2 as ufo_module
|
||||
except ImportError:
|
||||
try:
|
||||
import defcon as ufo_module
|
||||
except ImportError as e:
|
||||
pass
|
||||
|
||||
|
||||
logger = logging.getLogger("fontTools.cu2qu")
|
||||
|
||||
|
||||
def _cpu_count():
|
||||
try:
|
||||
return mp.cpu_count()
|
||||
except NotImplementedError: # pragma: no cover
|
||||
return 1
|
||||
|
||||
|
||||
def open_ufo(path):
|
||||
if hasattr(ufo_module.Font, "open"): # ufoLib2
|
||||
return ufo_module.Font.open(path)
|
||||
return ufo_module.Font(path) # defcon
|
||||
|
||||
|
||||
def _font_to_quadratic(input_path, output_path=None, **kwargs):
|
||||
ufo = open_ufo(input_path)
|
||||
logger.info("Converting curves for %s", input_path)
|
||||
if font_to_quadratic(ufo, **kwargs):
|
||||
logger.info("Saving %s", output_path)
|
||||
if output_path:
|
||||
ufo.save(output_path)
|
||||
else:
|
||||
ufo.save() # save in-place
|
||||
elif output_path:
|
||||
_copytree(input_path, output_path)
|
||||
|
||||
|
||||
def _samepath(path1, path2):
|
||||
# TODO on python3+, there's os.path.samefile
|
||||
path1 = os.path.normcase(os.path.abspath(os.path.realpath(path1)))
|
||||
path2 = os.path.normcase(os.path.abspath(os.path.realpath(path2)))
|
||||
return path1 == path2
|
||||
|
||||
|
||||
def _copytree(input_path, output_path):
|
||||
if _samepath(input_path, output_path):
|
||||
logger.debug("input and output paths are the same file; skipped copy")
|
||||
return
|
||||
if os.path.exists(output_path):
|
||||
shutil.rmtree(output_path)
|
||||
shutil.copytree(input_path, output_path)
|
||||
|
||||
|
||||
def _main(args=None):
|
||||
"""Convert a UFO font from cubic to quadratic curves"""
|
||||
parser = argparse.ArgumentParser(prog="cu2qu")
|
||||
parser.add_argument("--version", action="version", version=fontTools.__version__)
|
||||
parser.add_argument(
|
||||
"infiles",
|
||||
nargs="+",
|
||||
metavar="INPUT",
|
||||
help="one or more input UFO source file(s).",
|
||||
)
|
||||
parser.add_argument("-v", "--verbose", action="count", default=0)
|
||||
parser.add_argument(
|
||||
"-e",
|
||||
"--conversion-error",
|
||||
type=float,
|
||||
metavar="ERROR",
|
||||
default=None,
|
||||
help="maxiumum approximation error measured in EM (default: 0.001)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-m",
|
||||
"--mixed",
|
||||
default=False,
|
||||
action="store_true",
|
||||
help="whether to used mixed quadratic and cubic curves",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--keep-direction",
|
||||
dest="reverse_direction",
|
||||
action="store_false",
|
||||
help="do not reverse the contour direction",
|
||||
)
|
||||
|
||||
mode_parser = parser.add_mutually_exclusive_group()
|
||||
mode_parser.add_argument(
|
||||
"-i",
|
||||
"--interpolatable",
|
||||
action="store_true",
|
||||
help="whether curve conversion should keep interpolation compatibility",
|
||||
)
|
||||
mode_parser.add_argument(
|
||||
"-j",
|
||||
"--jobs",
|
||||
type=int,
|
||||
nargs="?",
|
||||
default=1,
|
||||
const=_cpu_count(),
|
||||
metavar="N",
|
||||
help="Convert using N multiple processes (default: %(default)s)",
|
||||
)
|
||||
|
||||
output_parser = parser.add_mutually_exclusive_group()
|
||||
output_parser.add_argument(
|
||||
"-o",
|
||||
"--output-file",
|
||||
default=None,
|
||||
metavar="OUTPUT",
|
||||
help=(
|
||||
"output filename for the converted UFO. By default fonts are "
|
||||
"modified in place. This only works with a single input."
|
||||
),
|
||||
)
|
||||
output_parser.add_argument(
|
||||
"-d",
|
||||
"--output-dir",
|
||||
default=None,
|
||||
metavar="DIRECTORY",
|
||||
help="output directory where to save converted UFOs",
|
||||
)
|
||||
|
||||
options = parser.parse_args(args)
|
||||
|
||||
if ufo_module is None:
|
||||
parser.error("Either ufoLib2 or defcon are required to run this script.")
|
||||
|
||||
if not options.verbose:
|
||||
level = "WARNING"
|
||||
elif options.verbose == 1:
|
||||
level = "INFO"
|
||||
else:
|
||||
level = "DEBUG"
|
||||
logging.basicConfig(level=level)
|
||||
|
||||
if len(options.infiles) > 1 and options.output_file:
|
||||
parser.error("-o/--output-file can't be used with multile inputs")
|
||||
|
||||
if options.output_dir:
|
||||
output_dir = options.output_dir
|
||||
if not os.path.exists(output_dir):
|
||||
os.mkdir(output_dir)
|
||||
elif not os.path.isdir(output_dir):
|
||||
parser.error("'%s' is not a directory" % output_dir)
|
||||
output_paths = [
|
||||
os.path.join(output_dir, os.path.basename(p)) for p in options.infiles
|
||||
]
|
||||
elif options.output_file:
|
||||
output_paths = [options.output_file]
|
||||
else:
|
||||
# save in-place
|
||||
output_paths = [None] * len(options.infiles)
|
||||
|
||||
kwargs = dict(
|
||||
dump_stats=options.verbose > 0,
|
||||
max_err_em=options.conversion_error,
|
||||
reverse_direction=options.reverse_direction,
|
||||
all_quadratic=False if options.mixed else True,
|
||||
)
|
||||
|
||||
if options.interpolatable:
|
||||
logger.info("Converting curves compatibly")
|
||||
ufos = [open_ufo(infile) for infile in options.infiles]
|
||||
if fonts_to_quadratic(ufos, **kwargs):
|
||||
for ufo, output_path in zip(ufos, output_paths):
|
||||
logger.info("Saving %s", output_path)
|
||||
if output_path:
|
||||
ufo.save(output_path)
|
||||
else:
|
||||
ufo.save()
|
||||
else:
|
||||
for input_path, output_path in zip(options.infiles, output_paths):
|
||||
if output_path:
|
||||
_copytree(input_path, output_path)
|
||||
else:
|
||||
jobs = min(len(options.infiles), options.jobs) if options.jobs > 1 else 1
|
||||
if jobs > 1:
|
||||
func = partial(_font_to_quadratic, **kwargs)
|
||||
logger.info("Running %d parallel processes", jobs)
|
||||
with closing(mp.Pool(jobs)) as pool:
|
||||
pool.starmap(func, zip(options.infiles, output_paths))
|
||||
else:
|
||||
for input_path, output_path in zip(options.infiles, output_paths):
|
||||
_font_to_quadratic(input_path, output_path, **kwargs)
|
||||
File diff suppressed because it is too large
Load Diff
Executable
BIN
Binary file not shown.
@@ -0,0 +1,534 @@
|
||||
# cython: language_level=3
|
||||
# distutils: define_macros=CYTHON_TRACE_NOGIL=1
|
||||
|
||||
# Copyright 2015 Google Inc. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
try:
|
||||
import cython
|
||||
|
||||
COMPILED = cython.compiled
|
||||
except (AttributeError, ImportError):
|
||||
# if cython not installed, use mock module with no-op decorators and types
|
||||
from fontTools.misc import cython
|
||||
|
||||
COMPILED = False
|
||||
|
||||
import math
|
||||
|
||||
from .errors import Error as Cu2QuError, ApproxNotFoundError
|
||||
|
||||
|
||||
__all__ = ["curve_to_quadratic", "curves_to_quadratic"]
|
||||
|
||||
MAX_N = 100
|
||||
|
||||
NAN = float("NaN")
|
||||
|
||||
|
||||
@cython.cfunc
|
||||
@cython.inline
|
||||
@cython.returns(cython.double)
|
||||
@cython.locals(v1=cython.complex, v2=cython.complex)
|
||||
def dot(v1, v2):
|
||||
"""Return the dot product of two vectors.
|
||||
|
||||
Args:
|
||||
v1 (complex): First vector.
|
||||
v2 (complex): Second vector.
|
||||
|
||||
Returns:
|
||||
double: Dot product.
|
||||
"""
|
||||
return (v1 * v2.conjugate()).real
|
||||
|
||||
|
||||
@cython.cfunc
|
||||
@cython.inline
|
||||
@cython.locals(a=cython.complex, b=cython.complex, c=cython.complex, d=cython.complex)
|
||||
@cython.locals(
|
||||
_1=cython.complex, _2=cython.complex, _3=cython.complex, _4=cython.complex
|
||||
)
|
||||
def calc_cubic_points(a, b, c, d):
|
||||
_1 = d
|
||||
_2 = (c / 3.0) + d
|
||||
_3 = (b + c) / 3.0 + _2
|
||||
_4 = a + d + c + b
|
||||
return _1, _2, _3, _4
|
||||
|
||||
|
||||
@cython.cfunc
|
||||
@cython.inline
|
||||
@cython.locals(
|
||||
p0=cython.complex, p1=cython.complex, p2=cython.complex, p3=cython.complex
|
||||
)
|
||||
@cython.locals(a=cython.complex, b=cython.complex, c=cython.complex, d=cython.complex)
|
||||
def calc_cubic_parameters(p0, p1, p2, p3):
|
||||
c = (p1 - p0) * 3.0
|
||||
b = (p2 - p1) * 3.0 - c
|
||||
d = p0
|
||||
a = p3 - d - c - b
|
||||
return a, b, c, d
|
||||
|
||||
|
||||
@cython.cfunc
|
||||
@cython.inline
|
||||
@cython.locals(
|
||||
p0=cython.complex, p1=cython.complex, p2=cython.complex, p3=cython.complex
|
||||
)
|
||||
def split_cubic_into_n_iter(p0, p1, p2, p3, n):
|
||||
"""Split a cubic Bezier into n equal parts.
|
||||
|
||||
Splits the curve into `n` equal parts by curve time.
|
||||
(t=0..1/n, t=1/n..2/n, ...)
|
||||
|
||||
Args:
|
||||
p0 (complex): Start point of curve.
|
||||
p1 (complex): First handle of curve.
|
||||
p2 (complex): Second handle of curve.
|
||||
p3 (complex): End point of curve.
|
||||
|
||||
Returns:
|
||||
An iterator yielding the control points (four complex values) of the
|
||||
subcurves.
|
||||
"""
|
||||
# Hand-coded special-cases
|
||||
if n == 2:
|
||||
return iter(split_cubic_into_two(p0, p1, p2, p3))
|
||||
if n == 3:
|
||||
return iter(split_cubic_into_three(p0, p1, p2, p3))
|
||||
if n == 4:
|
||||
a, b = split_cubic_into_two(p0, p1, p2, p3)
|
||||
return iter(
|
||||
split_cubic_into_two(a[0], a[1], a[2], a[3])
|
||||
+ split_cubic_into_two(b[0], b[1], b[2], b[3])
|
||||
)
|
||||
if n == 6:
|
||||
a, b = split_cubic_into_two(p0, p1, p2, p3)
|
||||
return iter(
|
||||
split_cubic_into_three(a[0], a[1], a[2], a[3])
|
||||
+ split_cubic_into_three(b[0], b[1], b[2], b[3])
|
||||
)
|
||||
|
||||
return _split_cubic_into_n_gen(p0, p1, p2, p3, n)
|
||||
|
||||
|
||||
@cython.locals(
|
||||
p0=cython.complex,
|
||||
p1=cython.complex,
|
||||
p2=cython.complex,
|
||||
p3=cython.complex,
|
||||
n=cython.int,
|
||||
)
|
||||
@cython.locals(a=cython.complex, b=cython.complex, c=cython.complex, d=cython.complex)
|
||||
@cython.locals(
|
||||
dt=cython.double, delta_2=cython.double, delta_3=cython.double, i=cython.int
|
||||
)
|
||||
@cython.locals(
|
||||
a1=cython.complex, b1=cython.complex, c1=cython.complex, d1=cython.complex
|
||||
)
|
||||
def _split_cubic_into_n_gen(p0, p1, p2, p3, n):
|
||||
a, b, c, d = calc_cubic_parameters(p0, p1, p2, p3)
|
||||
dt = 1 / n
|
||||
delta_2 = dt * dt
|
||||
delta_3 = dt * delta_2
|
||||
for i in range(n):
|
||||
t1 = i * dt
|
||||
t1_2 = t1 * t1
|
||||
# calc new a, b, c and d
|
||||
a1 = a * delta_3
|
||||
b1 = (3 * a * t1 + b) * delta_2
|
||||
c1 = (2 * b * t1 + c + 3 * a * t1_2) * dt
|
||||
d1 = a * t1 * t1_2 + b * t1_2 + c * t1 + d
|
||||
yield calc_cubic_points(a1, b1, c1, d1)
|
||||
|
||||
|
||||
@cython.cfunc
|
||||
@cython.inline
|
||||
@cython.locals(
|
||||
p0=cython.complex, p1=cython.complex, p2=cython.complex, p3=cython.complex
|
||||
)
|
||||
@cython.locals(mid=cython.complex, deriv3=cython.complex)
|
||||
def split_cubic_into_two(p0, p1, p2, p3):
|
||||
"""Split a cubic Bezier into two equal parts.
|
||||
|
||||
Splits the curve into two equal parts at t = 0.5
|
||||
|
||||
Args:
|
||||
p0 (complex): Start point of curve.
|
||||
p1 (complex): First handle of curve.
|
||||
p2 (complex): Second handle of curve.
|
||||
p3 (complex): End point of curve.
|
||||
|
||||
Returns:
|
||||
tuple: Two cubic Beziers (each expressed as a tuple of four complex
|
||||
values).
|
||||
"""
|
||||
mid = (p0 + 3 * (p1 + p2) + p3) * 0.125
|
||||
deriv3 = (p3 + p2 - p1 - p0) * 0.125
|
||||
return (
|
||||
(p0, (p0 + p1) * 0.5, mid - deriv3, mid),
|
||||
(mid, mid + deriv3, (p2 + p3) * 0.5, p3),
|
||||
)
|
||||
|
||||
|
||||
@cython.cfunc
|
||||
@cython.inline
|
||||
@cython.locals(
|
||||
p0=cython.complex,
|
||||
p1=cython.complex,
|
||||
p2=cython.complex,
|
||||
p3=cython.complex,
|
||||
)
|
||||
@cython.locals(
|
||||
mid1=cython.complex,
|
||||
deriv1=cython.complex,
|
||||
mid2=cython.complex,
|
||||
deriv2=cython.complex,
|
||||
)
|
||||
def split_cubic_into_three(p0, p1, p2, p3):
|
||||
"""Split a cubic Bezier into three equal parts.
|
||||
|
||||
Splits the curve into three equal parts at t = 1/3 and t = 2/3
|
||||
|
||||
Args:
|
||||
p0 (complex): Start point of curve.
|
||||
p1 (complex): First handle of curve.
|
||||
p2 (complex): Second handle of curve.
|
||||
p3 (complex): End point of curve.
|
||||
|
||||
Returns:
|
||||
tuple: Three cubic Beziers (each expressed as a tuple of four complex
|
||||
values).
|
||||
"""
|
||||
mid1 = (8 * p0 + 12 * p1 + 6 * p2 + p3) * (1 / 27)
|
||||
deriv1 = (p3 + 3 * p2 - 4 * p0) * (1 / 27)
|
||||
mid2 = (p0 + 6 * p1 + 12 * p2 + 8 * p3) * (1 / 27)
|
||||
deriv2 = (4 * p3 - 3 * p1 - p0) * (1 / 27)
|
||||
return (
|
||||
(p0, (2 * p0 + p1) / 3.0, mid1 - deriv1, mid1),
|
||||
(mid1, mid1 + deriv1, mid2 - deriv2, mid2),
|
||||
(mid2, mid2 + deriv2, (p2 + 2 * p3) / 3.0, p3),
|
||||
)
|
||||
|
||||
|
||||
@cython.cfunc
|
||||
@cython.inline
|
||||
@cython.returns(cython.complex)
|
||||
@cython.locals(
|
||||
t=cython.double,
|
||||
p0=cython.complex,
|
||||
p1=cython.complex,
|
||||
p2=cython.complex,
|
||||
p3=cython.complex,
|
||||
)
|
||||
@cython.locals(_p1=cython.complex, _p2=cython.complex)
|
||||
def cubic_approx_control(t, p0, p1, p2, p3):
|
||||
"""Approximate a cubic Bezier using a quadratic one.
|
||||
|
||||
Args:
|
||||
t (double): Position of control point.
|
||||
p0 (complex): Start point of curve.
|
||||
p1 (complex): First handle of curve.
|
||||
p2 (complex): Second handle of curve.
|
||||
p3 (complex): End point of curve.
|
||||
|
||||
Returns:
|
||||
complex: Location of candidate control point on quadratic curve.
|
||||
"""
|
||||
_p1 = p0 + (p1 - p0) * 1.5
|
||||
_p2 = p3 + (p2 - p3) * 1.5
|
||||
return _p1 + (_p2 - _p1) * t
|
||||
|
||||
|
||||
@cython.cfunc
|
||||
@cython.inline
|
||||
@cython.returns(cython.complex)
|
||||
@cython.locals(a=cython.complex, b=cython.complex, c=cython.complex, d=cython.complex)
|
||||
@cython.locals(ab=cython.complex, cd=cython.complex, p=cython.complex, h=cython.double)
|
||||
def calc_intersect(a, b, c, d):
|
||||
"""Calculate the intersection of two lines.
|
||||
|
||||
Args:
|
||||
a (complex): Start point of first line.
|
||||
b (complex): End point of first line.
|
||||
c (complex): Start point of second line.
|
||||
d (complex): End point of second line.
|
||||
|
||||
Returns:
|
||||
complex: Location of intersection if one present, ``complex(NaN,NaN)``
|
||||
if no intersection was found.
|
||||
"""
|
||||
ab = b - a
|
||||
cd = d - c
|
||||
p = ab * 1j
|
||||
try:
|
||||
h = dot(p, a - c) / dot(p, cd)
|
||||
except ZeroDivisionError:
|
||||
return complex(NAN, NAN)
|
||||
return c + cd * h
|
||||
|
||||
|
||||
@cython.cfunc
|
||||
@cython.returns(cython.int)
|
||||
@cython.locals(
|
||||
tolerance=cython.double,
|
||||
p0=cython.complex,
|
||||
p1=cython.complex,
|
||||
p2=cython.complex,
|
||||
p3=cython.complex,
|
||||
)
|
||||
@cython.locals(mid=cython.complex, deriv3=cython.complex)
|
||||
def cubic_farthest_fit_inside(p0, p1, p2, p3, tolerance):
|
||||
"""Check if a cubic Bezier lies within a given distance of the origin.
|
||||
|
||||
"Origin" means *the* origin (0,0), not the start of the curve. Note that no
|
||||
checks are made on the start and end positions of the curve; this function
|
||||
only checks the inside of the curve.
|
||||
|
||||
Args:
|
||||
p0 (complex): Start point of curve.
|
||||
p1 (complex): First handle of curve.
|
||||
p2 (complex): Second handle of curve.
|
||||
p3 (complex): End point of curve.
|
||||
tolerance (double): Distance from origin.
|
||||
|
||||
Returns:
|
||||
bool: True if the cubic Bezier ``p`` entirely lies within a distance
|
||||
``tolerance`` of the origin, False otherwise.
|
||||
"""
|
||||
# First check p2 then p1, as p2 has higher error early on.
|
||||
if abs(p2) <= tolerance and abs(p1) <= tolerance:
|
||||
return True
|
||||
|
||||
# Split.
|
||||
mid = (p0 + 3 * (p1 + p2) + p3) * 0.125
|
||||
if abs(mid) > tolerance:
|
||||
return False
|
||||
deriv3 = (p3 + p2 - p1 - p0) * 0.125
|
||||
return cubic_farthest_fit_inside(
|
||||
p0, (p0 + p1) * 0.5, mid - deriv3, mid, tolerance
|
||||
) and cubic_farthest_fit_inside(mid, mid + deriv3, (p2 + p3) * 0.5, p3, tolerance)
|
||||
|
||||
|
||||
@cython.cfunc
|
||||
@cython.inline
|
||||
@cython.locals(tolerance=cython.double)
|
||||
@cython.locals(
|
||||
q1=cython.complex,
|
||||
c0=cython.complex,
|
||||
c1=cython.complex,
|
||||
c2=cython.complex,
|
||||
c3=cython.complex,
|
||||
)
|
||||
def cubic_approx_quadratic(cubic, tolerance):
|
||||
"""Approximate a cubic Bezier with a single quadratic within a given tolerance.
|
||||
|
||||
Args:
|
||||
cubic (sequence): Four complex numbers representing control points of
|
||||
the cubic Bezier curve.
|
||||
tolerance (double): Permitted deviation from the original curve.
|
||||
|
||||
Returns:
|
||||
Three complex numbers representing control points of the quadratic
|
||||
curve if it fits within the given tolerance, or ``None`` if no suitable
|
||||
curve could be calculated.
|
||||
"""
|
||||
|
||||
q1 = calc_intersect(cubic[0], cubic[1], cubic[2], cubic[3])
|
||||
if math.isnan(q1.imag):
|
||||
return None
|
||||
c0 = cubic[0]
|
||||
c3 = cubic[3]
|
||||
c1 = c0 + (q1 - c0) * (2 / 3)
|
||||
c2 = c3 + (q1 - c3) * (2 / 3)
|
||||
if not cubic_farthest_fit_inside(0, c1 - cubic[1], c2 - cubic[2], 0, tolerance):
|
||||
return None
|
||||
return c0, q1, c3
|
||||
|
||||
|
||||
@cython.cfunc
|
||||
@cython.locals(n=cython.int, tolerance=cython.double)
|
||||
@cython.locals(i=cython.int)
|
||||
@cython.locals(all_quadratic=cython.int)
|
||||
@cython.locals(
|
||||
c0=cython.complex, c1=cython.complex, c2=cython.complex, c3=cython.complex
|
||||
)
|
||||
@cython.locals(
|
||||
q0=cython.complex,
|
||||
q1=cython.complex,
|
||||
next_q1=cython.complex,
|
||||
q2=cython.complex,
|
||||
d1=cython.complex,
|
||||
)
|
||||
def cubic_approx_spline(cubic, n, tolerance, all_quadratic):
|
||||
"""Approximate a cubic Bezier curve with a spline of n quadratics.
|
||||
|
||||
Args:
|
||||
cubic (sequence): Four complex numbers representing control points of
|
||||
the cubic Bezier curve.
|
||||
n (int): Number of quadratic Bezier curves in the spline.
|
||||
tolerance (double): Permitted deviation from the original curve.
|
||||
|
||||
Returns:
|
||||
A list of ``n+2`` complex numbers, representing control points of the
|
||||
quadratic spline if it fits within the given tolerance, or ``None`` if
|
||||
no suitable spline could be calculated.
|
||||
"""
|
||||
|
||||
if n == 1:
|
||||
return cubic_approx_quadratic(cubic, tolerance)
|
||||
if n == 2 and all_quadratic == False:
|
||||
return cubic
|
||||
|
||||
cubics = split_cubic_into_n_iter(cubic[0], cubic[1], cubic[2], cubic[3], n)
|
||||
|
||||
# calculate the spline of quadratics and check errors at the same time.
|
||||
next_cubic = next(cubics)
|
||||
next_q1 = cubic_approx_control(
|
||||
0, next_cubic[0], next_cubic[1], next_cubic[2], next_cubic[3]
|
||||
)
|
||||
q2 = cubic[0]
|
||||
d1 = 0j
|
||||
spline = [cubic[0], next_q1]
|
||||
for i in range(1, n + 1):
|
||||
# Current cubic to convert
|
||||
c0, c1, c2, c3 = next_cubic
|
||||
|
||||
# Current quadratic approximation of current cubic
|
||||
q0 = q2
|
||||
q1 = next_q1
|
||||
if i < n:
|
||||
next_cubic = next(cubics)
|
||||
next_q1 = cubic_approx_control(
|
||||
i / (n - 1), next_cubic[0], next_cubic[1], next_cubic[2], next_cubic[3]
|
||||
)
|
||||
spline.append(next_q1)
|
||||
q2 = (q1 + next_q1) * 0.5
|
||||
else:
|
||||
q2 = c3
|
||||
|
||||
# End-point deltas
|
||||
d0 = d1
|
||||
d1 = q2 - c3
|
||||
|
||||
if abs(d1) > tolerance or not cubic_farthest_fit_inside(
|
||||
d0,
|
||||
q0 + (q1 - q0) * (2 / 3) - c1,
|
||||
q2 + (q1 - q2) * (2 / 3) - c2,
|
||||
d1,
|
||||
tolerance,
|
||||
):
|
||||
return None
|
||||
spline.append(cubic[3])
|
||||
|
||||
return spline
|
||||
|
||||
|
||||
@cython.locals(max_err=cython.double)
|
||||
@cython.locals(n=cython.int)
|
||||
@cython.locals(all_quadratic=cython.int)
|
||||
def curve_to_quadratic(curve, max_err, all_quadratic=True):
|
||||
"""Approximate a cubic Bezier curve with a spline of n quadratics.
|
||||
|
||||
Args:
|
||||
cubic (sequence): Four 2D tuples representing control points of
|
||||
the cubic Bezier curve.
|
||||
max_err (double): Permitted deviation from the original curve.
|
||||
all_quadratic (bool): If True (default) returned value is a
|
||||
quadratic spline. If False, it's either a single quadratic
|
||||
curve or a single cubic curve.
|
||||
|
||||
Returns:
|
||||
If all_quadratic is True: A list of 2D tuples, representing
|
||||
control points of the quadratic spline if it fits within the
|
||||
given tolerance, or ``None`` if no suitable spline could be
|
||||
calculated.
|
||||
|
||||
If all_quadratic is False: Either a quadratic curve (if length
|
||||
of output is 3), or a cubic curve (if length of output is 4).
|
||||
"""
|
||||
|
||||
curve = [complex(*p) for p in curve]
|
||||
|
||||
for n in range(1, MAX_N + 1):
|
||||
spline = cubic_approx_spline(curve, n, max_err, all_quadratic)
|
||||
if spline is not None:
|
||||
# done. go home
|
||||
return [(s.real, s.imag) for s in spline]
|
||||
|
||||
raise ApproxNotFoundError(curve)
|
||||
|
||||
|
||||
@cython.locals(l=cython.int, last_i=cython.int, i=cython.int)
|
||||
@cython.locals(all_quadratic=cython.int)
|
||||
def curves_to_quadratic(curves, max_errors, all_quadratic=True):
|
||||
"""Return quadratic Bezier splines approximating the input cubic Beziers.
|
||||
|
||||
Args:
|
||||
curves: A sequence of *n* curves, each curve being a sequence of four
|
||||
2D tuples.
|
||||
max_errors: A sequence of *n* floats representing the maximum permissible
|
||||
deviation from each of the cubic Bezier curves.
|
||||
all_quadratic (bool): If True (default) returned values are a
|
||||
quadratic spline. If False, they are either a single quadratic
|
||||
curve or a single cubic curve.
|
||||
|
||||
Example::
|
||||
|
||||
>>> curves_to_quadratic( [
|
||||
... [ (50,50), (100,100), (150,100), (200,50) ],
|
||||
... [ (75,50), (120,100), (150,75), (200,60) ]
|
||||
... ], [1,1] )
|
||||
[[(50.0, 50.0), (75.0, 75.0), (125.0, 91.66666666666666), (175.0, 75.0), (200.0, 50.0)], [(75.0, 50.0), (97.5, 75.0), (135.41666666666666, 82.08333333333333), (175.0, 67.5), (200.0, 60.0)]]
|
||||
|
||||
The returned splines have "implied oncurve points" suitable for use in
|
||||
TrueType ``glif`` outlines - i.e. in the first spline returned above,
|
||||
the first quadratic segment runs from (50,50) to
|
||||
( (75 + 125)/2 , (120 + 91.666..)/2 ) = (100, 83.333...).
|
||||
|
||||
Returns:
|
||||
If all_quadratic is True, a list of splines, each spline being a list
|
||||
of 2D tuples.
|
||||
|
||||
If all_quadratic is False, a list of curves, each curve being a quadratic
|
||||
(length 3), or cubic (length 4).
|
||||
|
||||
Raises:
|
||||
fontTools.cu2qu.Errors.ApproxNotFoundError: if no suitable approximation
|
||||
can be found for all curves with the given parameters.
|
||||
"""
|
||||
|
||||
curves = [[complex(*p) for p in curve] for curve in curves]
|
||||
assert len(max_errors) == len(curves)
|
||||
|
||||
l = len(curves)
|
||||
splines = [None] * l
|
||||
last_i = i = 0
|
||||
n = 1
|
||||
while True:
|
||||
spline = cubic_approx_spline(curves[i], n, max_errors[i], all_quadratic)
|
||||
if spline is None:
|
||||
if n == MAX_N:
|
||||
break
|
||||
n += 1
|
||||
last_i = i
|
||||
continue
|
||||
splines[i] = spline
|
||||
i = (i + 1) % l
|
||||
if i == last_i:
|
||||
# done. go home
|
||||
return [[(s.real, s.imag) for s in spline] for spline in splines]
|
||||
|
||||
raise ApproxNotFoundError(curves)
|
||||
@@ -0,0 +1,77 @@
|
||||
# Copyright 2016 Google Inc. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
class Error(Exception):
|
||||
"""Base Cu2Qu exception class for all other errors."""
|
||||
|
||||
|
||||
class ApproxNotFoundError(Error):
|
||||
def __init__(self, curve):
|
||||
message = "no approximation found: %s" % curve
|
||||
super().__init__(message)
|
||||
self.curve = curve
|
||||
|
||||
|
||||
class UnequalZipLengthsError(Error):
|
||||
pass
|
||||
|
||||
|
||||
class IncompatibleGlyphsError(Error):
|
||||
def __init__(self, glyphs):
|
||||
assert len(glyphs) > 1
|
||||
self.glyphs = glyphs
|
||||
names = set(repr(g.name) for g in glyphs)
|
||||
if len(names) > 1:
|
||||
self.combined_name = "{%s}" % ", ".join(sorted(names))
|
||||
else:
|
||||
self.combined_name = names.pop()
|
||||
|
||||
def __repr__(self):
|
||||
return "<%s %s>" % (type(self).__name__, self.combined_name)
|
||||
|
||||
|
||||
class IncompatibleSegmentNumberError(IncompatibleGlyphsError):
|
||||
def __str__(self):
|
||||
return "Glyphs named %s have different number of segments" % (
|
||||
self.combined_name
|
||||
)
|
||||
|
||||
|
||||
class IncompatibleSegmentTypesError(IncompatibleGlyphsError):
|
||||
def __init__(self, glyphs, segments):
|
||||
IncompatibleGlyphsError.__init__(self, glyphs)
|
||||
self.segments = segments
|
||||
|
||||
def __str__(self):
|
||||
lines = []
|
||||
ndigits = len(str(max(self.segments)))
|
||||
for i, tags in sorted(self.segments.items()):
|
||||
lines.append(
|
||||
"%s: (%s)" % (str(i).rjust(ndigits), ", ".join(repr(t) for t in tags))
|
||||
)
|
||||
return "Glyphs named %s have incompatible segment types:\n %s" % (
|
||||
self.combined_name,
|
||||
"\n ".join(lines),
|
||||
)
|
||||
|
||||
|
||||
class IncompatibleFontsError(Error):
|
||||
def __init__(self, glyph_errors):
|
||||
self.glyph_errors = glyph_errors
|
||||
|
||||
def __str__(self):
|
||||
return "fonts contains incompatible glyphs: %s" % (
|
||||
", ".join(repr(g) for g in sorted(self.glyph_errors.keys()))
|
||||
)
|
||||
@@ -0,0 +1,349 @@
|
||||
# Copyright 2015 Google Inc. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
"""Converts cubic bezier curves to quadratic splines.
|
||||
|
||||
Conversion is performed such that the quadratic splines keep the same end-curve
|
||||
tangents as the original cubics. The approach is iterative, increasing the
|
||||
number of segments for a spline until the error gets below a bound.
|
||||
|
||||
Respective curves from multiple fonts will be converted at once to ensure that
|
||||
the resulting splines are interpolation-compatible.
|
||||
"""
|
||||
|
||||
import logging
|
||||
from fontTools.pens.basePen import AbstractPen
|
||||
from fontTools.pens.pointPen import PointToSegmentPen
|
||||
from fontTools.pens.reverseContourPen import ReverseContourPen
|
||||
|
||||
from . import curves_to_quadratic
|
||||
from .errors import (
|
||||
UnequalZipLengthsError,
|
||||
IncompatibleSegmentNumberError,
|
||||
IncompatibleSegmentTypesError,
|
||||
IncompatibleGlyphsError,
|
||||
IncompatibleFontsError,
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["fonts_to_quadratic", "font_to_quadratic"]
|
||||
|
||||
# The default approximation error below is a relative value (1/1000 of the EM square).
|
||||
# Later on, we convert it to absolute font units by multiplying it by a font's UPEM
|
||||
# (see fonts_to_quadratic).
|
||||
DEFAULT_MAX_ERR = 0.001
|
||||
CURVE_TYPE_LIB_KEY = "com.github.googlei18n.cu2qu.curve_type"
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
_zip = zip
|
||||
|
||||
|
||||
def zip(*args):
|
||||
"""Ensure each argument to zip has the same length. Also make sure a list is
|
||||
returned for python 2/3 compatibility.
|
||||
"""
|
||||
|
||||
if len(set(len(a) for a in args)) != 1:
|
||||
raise UnequalZipLengthsError(*args)
|
||||
return list(_zip(*args))
|
||||
|
||||
|
||||
class GetSegmentsPen(AbstractPen):
|
||||
"""Pen to collect segments into lists of points for conversion.
|
||||
|
||||
Curves always include their initial on-curve point, so some points are
|
||||
duplicated between segments.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self._last_pt = None
|
||||
self.segments = []
|
||||
|
||||
def _add_segment(self, tag, *args):
|
||||
if tag in ["move", "line", "qcurve", "curve"]:
|
||||
self._last_pt = args[-1]
|
||||
self.segments.append((tag, args))
|
||||
|
||||
def moveTo(self, pt):
|
||||
self._add_segment("move", pt)
|
||||
|
||||
def lineTo(self, pt):
|
||||
self._add_segment("line", pt)
|
||||
|
||||
def qCurveTo(self, *points):
|
||||
self._add_segment("qcurve", self._last_pt, *points)
|
||||
|
||||
def curveTo(self, *points):
|
||||
self._add_segment("curve", self._last_pt, *points)
|
||||
|
||||
def closePath(self):
|
||||
self._add_segment("close")
|
||||
|
||||
def endPath(self):
|
||||
self._add_segment("end")
|
||||
|
||||
def addComponent(self, glyphName, transformation):
|
||||
pass
|
||||
|
||||
|
||||
def _get_segments(glyph):
|
||||
"""Get a glyph's segments as extracted by GetSegmentsPen."""
|
||||
|
||||
pen = GetSegmentsPen()
|
||||
# glyph.draw(pen)
|
||||
# We can't simply draw the glyph with the pen, but we must initialize the
|
||||
# PointToSegmentPen explicitly with outputImpliedClosingLine=True.
|
||||
# By default PointToSegmentPen does not outputImpliedClosingLine -- unless
|
||||
# last and first point on closed contour are duplicated. Because we are
|
||||
# converting multiple glyphs at the same time, we want to make sure
|
||||
# this function returns the same number of segments, whether or not
|
||||
# the last and first point overlap.
|
||||
# https://github.com/googlefonts/fontmake/issues/572
|
||||
# https://github.com/fonttools/fonttools/pull/1720
|
||||
pointPen = PointToSegmentPen(pen, outputImpliedClosingLine=True)
|
||||
glyph.drawPoints(pointPen)
|
||||
return pen.segments
|
||||
|
||||
|
||||
def _set_segments(glyph, segments, reverse_direction):
|
||||
"""Draw segments as extracted by GetSegmentsPen back to a glyph."""
|
||||
|
||||
glyph.clearContours()
|
||||
pen = glyph.getPen()
|
||||
if reverse_direction:
|
||||
pen = ReverseContourPen(pen)
|
||||
for tag, args in segments:
|
||||
if tag == "move":
|
||||
pen.moveTo(*args)
|
||||
elif tag == "line":
|
||||
pen.lineTo(*args)
|
||||
elif tag == "curve":
|
||||
pen.curveTo(*args[1:])
|
||||
elif tag == "qcurve":
|
||||
pen.qCurveTo(*args[1:])
|
||||
elif tag == "close":
|
||||
pen.closePath()
|
||||
elif tag == "end":
|
||||
pen.endPath()
|
||||
else:
|
||||
raise AssertionError('Unhandled segment type "%s"' % tag)
|
||||
|
||||
|
||||
def _segments_to_quadratic(segments, max_err, stats, all_quadratic=True):
|
||||
"""Return quadratic approximations of cubic segments."""
|
||||
|
||||
assert all(s[0] == "curve" for s in segments), "Non-cubic given to convert"
|
||||
|
||||
new_points = curves_to_quadratic([s[1] for s in segments], max_err, all_quadratic)
|
||||
n = len(new_points[0])
|
||||
assert all(len(s) == n for s in new_points[1:]), "Converted incompatibly"
|
||||
|
||||
spline_length = str(n - 2)
|
||||
stats[spline_length] = stats.get(spline_length, 0) + 1
|
||||
|
||||
if all_quadratic or n == 3:
|
||||
return [("qcurve", p) for p in new_points]
|
||||
else:
|
||||
return [("curve", p) for p in new_points]
|
||||
|
||||
|
||||
def _glyphs_to_quadratic(glyphs, max_err, reverse_direction, stats, all_quadratic=True):
|
||||
"""Do the actual conversion of a set of compatible glyphs, after arguments
|
||||
have been set up.
|
||||
|
||||
Return True if the glyphs were modified, else return False.
|
||||
"""
|
||||
|
||||
try:
|
||||
segments_by_location = zip(*[_get_segments(g) for g in glyphs])
|
||||
except UnequalZipLengthsError:
|
||||
raise IncompatibleSegmentNumberError(glyphs)
|
||||
if not any(segments_by_location):
|
||||
return False
|
||||
|
||||
# always modify input glyphs if reverse_direction is True
|
||||
glyphs_modified = reverse_direction
|
||||
|
||||
new_segments_by_location = []
|
||||
incompatible = {}
|
||||
for i, segments in enumerate(segments_by_location):
|
||||
tag = segments[0][0]
|
||||
if not all(s[0] == tag for s in segments[1:]):
|
||||
incompatible[i] = [s[0] for s in segments]
|
||||
elif tag == "curve":
|
||||
new_segments = _segments_to_quadratic(
|
||||
segments, max_err, stats, all_quadratic
|
||||
)
|
||||
if all_quadratic or new_segments != segments:
|
||||
glyphs_modified = True
|
||||
segments = new_segments
|
||||
new_segments_by_location.append(segments)
|
||||
|
||||
if glyphs_modified:
|
||||
new_segments_by_glyph = zip(*new_segments_by_location)
|
||||
for glyph, new_segments in zip(glyphs, new_segments_by_glyph):
|
||||
_set_segments(glyph, new_segments, reverse_direction)
|
||||
|
||||
if incompatible:
|
||||
raise IncompatibleSegmentTypesError(glyphs, segments=incompatible)
|
||||
return glyphs_modified
|
||||
|
||||
|
||||
def glyphs_to_quadratic(
|
||||
glyphs, max_err=None, reverse_direction=False, stats=None, all_quadratic=True
|
||||
):
|
||||
"""Convert the curves of a set of compatible of glyphs to quadratic.
|
||||
|
||||
All curves will be converted to quadratic at once, ensuring interpolation
|
||||
compatibility. If this is not required, calling glyphs_to_quadratic with one
|
||||
glyph at a time may yield slightly more optimized results.
|
||||
|
||||
Return True if glyphs were modified, else return False.
|
||||
|
||||
Raises IncompatibleGlyphsError if glyphs have non-interpolatable outlines.
|
||||
"""
|
||||
if stats is None:
|
||||
stats = {}
|
||||
|
||||
if not max_err:
|
||||
# assume 1000 is the default UPEM
|
||||
max_err = DEFAULT_MAX_ERR * 1000
|
||||
|
||||
if isinstance(max_err, (list, tuple)):
|
||||
max_errors = max_err
|
||||
else:
|
||||
max_errors = [max_err] * len(glyphs)
|
||||
assert len(max_errors) == len(glyphs)
|
||||
|
||||
return _glyphs_to_quadratic(
|
||||
glyphs, max_errors, reverse_direction, stats, all_quadratic
|
||||
)
|
||||
|
||||
|
||||
def fonts_to_quadratic(
|
||||
fonts,
|
||||
max_err_em=None,
|
||||
max_err=None,
|
||||
reverse_direction=False,
|
||||
stats=None,
|
||||
dump_stats=False,
|
||||
remember_curve_type=True,
|
||||
all_quadratic=True,
|
||||
):
|
||||
"""Convert the curves of a collection of fonts to quadratic.
|
||||
|
||||
All curves will be converted to quadratic at once, ensuring interpolation
|
||||
compatibility. If this is not required, calling fonts_to_quadratic with one
|
||||
font at a time may yield slightly more optimized results.
|
||||
|
||||
Return the set of modified glyph names if any, else return an empty set.
|
||||
|
||||
By default, cu2qu stores the curve type in the fonts' lib, under a private
|
||||
key "com.github.googlei18n.cu2qu.curve_type", and will not try to convert
|
||||
them again if the curve type is already set to "quadratic".
|
||||
Setting 'remember_curve_type' to False disables this optimization.
|
||||
|
||||
Raises IncompatibleFontsError if same-named glyphs from different fonts
|
||||
have non-interpolatable outlines.
|
||||
"""
|
||||
|
||||
if remember_curve_type:
|
||||
curve_types = {f.lib.get(CURVE_TYPE_LIB_KEY, "cubic") for f in fonts}
|
||||
if len(curve_types) == 1:
|
||||
curve_type = next(iter(curve_types))
|
||||
if curve_type in ("quadratic", "mixed"):
|
||||
logger.info("Curves already converted to quadratic")
|
||||
return False
|
||||
elif curve_type == "cubic":
|
||||
pass # keep converting
|
||||
else:
|
||||
raise NotImplementedError(curve_type)
|
||||
elif len(curve_types) > 1:
|
||||
# going to crash later if they do differ
|
||||
logger.warning("fonts may contain different curve types")
|
||||
|
||||
if stats is None:
|
||||
stats = {}
|
||||
|
||||
if max_err_em and max_err:
|
||||
raise TypeError("Only one of max_err and max_err_em can be specified.")
|
||||
if not (max_err_em or max_err):
|
||||
max_err_em = DEFAULT_MAX_ERR
|
||||
|
||||
if isinstance(max_err, (list, tuple)):
|
||||
assert len(max_err) == len(fonts)
|
||||
max_errors = max_err
|
||||
elif max_err:
|
||||
max_errors = [max_err] * len(fonts)
|
||||
|
||||
if isinstance(max_err_em, (list, tuple)):
|
||||
assert len(fonts) == len(max_err_em)
|
||||
max_errors = [f.info.unitsPerEm * e for f, e in zip(fonts, max_err_em)]
|
||||
elif max_err_em:
|
||||
max_errors = [f.info.unitsPerEm * max_err_em for f in fonts]
|
||||
|
||||
modified = set()
|
||||
glyph_errors = {}
|
||||
for name in set().union(*(f.keys() for f in fonts)):
|
||||
glyphs = []
|
||||
cur_max_errors = []
|
||||
for font, error in zip(fonts, max_errors):
|
||||
if name in font:
|
||||
glyphs.append(font[name])
|
||||
cur_max_errors.append(error)
|
||||
try:
|
||||
if _glyphs_to_quadratic(
|
||||
glyphs, cur_max_errors, reverse_direction, stats, all_quadratic
|
||||
):
|
||||
modified.add(name)
|
||||
except IncompatibleGlyphsError as exc:
|
||||
logger.error(exc)
|
||||
glyph_errors[name] = exc
|
||||
|
||||
if glyph_errors:
|
||||
raise IncompatibleFontsError(glyph_errors)
|
||||
|
||||
if modified and dump_stats:
|
||||
spline_lengths = sorted(stats.keys())
|
||||
logger.info(
|
||||
"New spline lengths: %s"
|
||||
% (", ".join("%s: %d" % (l, stats[l]) for l in spline_lengths))
|
||||
)
|
||||
|
||||
if remember_curve_type:
|
||||
for font in fonts:
|
||||
curve_type = font.lib.get(CURVE_TYPE_LIB_KEY, "cubic")
|
||||
new_curve_type = "quadratic" if all_quadratic else "mixed"
|
||||
if curve_type != new_curve_type:
|
||||
font.lib[CURVE_TYPE_LIB_KEY] = new_curve_type
|
||||
return modified
|
||||
|
||||
|
||||
def glyph_to_quadratic(glyph, **kwargs):
|
||||
"""Convenience wrapper around glyphs_to_quadratic, for just one glyph.
|
||||
Return True if the glyph was modified, else return False.
|
||||
"""
|
||||
|
||||
return glyphs_to_quadratic([glyph], **kwargs)
|
||||
|
||||
|
||||
def font_to_quadratic(font, **kwargs):
|
||||
"""Convenience wrapper around fonts_to_quadratic, for just one font.
|
||||
Return the set of modified glyph names if any, else return empty set.
|
||||
"""
|
||||
|
||||
return fonts_to_quadratic([font], **kwargs)
|
||||
Reference in New Issue
Block a user