ufo.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. # Copyright 2015 Google Inc. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Converts cubic bezier curves to quadratic splines.
  15. Conversion is performed such that the quadratic splines keep the same end-curve
  16. tangents as the original cubics. The approach is iterative, increasing the
  17. number of segments for a spline until the error gets below a bound.
  18. Respective curves from multiple fonts will be converted at once to ensure that
  19. the resulting splines are interpolation-compatible.
  20. """
  21. import logging
  22. from fontTools.pens.basePen import AbstractPen
  23. from fontTools.pens.pointPen import PointToSegmentPen
  24. from fontTools.pens.reverseContourPen import ReverseContourPen
  25. from . import curves_to_quadratic
  26. from .errors import (
  27. UnequalZipLengthsError,
  28. IncompatibleSegmentNumberError,
  29. IncompatibleSegmentTypesError,
  30. IncompatibleGlyphsError,
  31. IncompatibleFontsError,
  32. )
  33. __all__ = ["fonts_to_quadratic", "font_to_quadratic"]
  34. # The default approximation error below is a relative value (1/1000 of the EM square).
  35. # Later on, we convert it to absolute font units by multiplying it by a font's UPEM
  36. # (see fonts_to_quadratic).
  37. DEFAULT_MAX_ERR = 0.001
  38. CURVE_TYPE_LIB_KEY = "com.github.googlei18n.cu2qu.curve_type"
  39. logger = logging.getLogger(__name__)
  40. _zip = zip
  41. def zip(*args):
  42. """Ensure each argument to zip has the same length. Also make sure a list is
  43. returned for python 2/3 compatibility.
  44. """
  45. if len(set(len(a) for a in args)) != 1:
  46. raise UnequalZipLengthsError(*args)
  47. return list(_zip(*args))
  48. def _validate_positive_tolerance(value, name):
  49. if value <= 0:
  50. raise ValueError(f"{name} must be greater than zero")
  51. def _validate_positive_tolerances(values, name):
  52. for value in values:
  53. _validate_positive_tolerance(value, name)
  54. def _validate_length(values, expected, name):
  55. if len(values) != expected:
  56. raise ValueError(f"{name} must match the number of inputs")
  57. class GetSegmentsPen(AbstractPen):
  58. """Pen to collect segments into lists of points for conversion.
  59. Curves always include their initial on-curve point, so some points are
  60. duplicated between segments.
  61. """
  62. def __init__(self):
  63. self._last_pt = None
  64. self.segments = []
  65. def _add_segment(self, tag, *args):
  66. if tag in ["move", "line", "qcurve", "curve"]:
  67. self._last_pt = args[-1]
  68. self.segments.append((tag, args))
  69. def moveTo(self, pt):
  70. self._add_segment("move", pt)
  71. def lineTo(self, pt):
  72. self._add_segment("line", pt)
  73. def qCurveTo(self, *points):
  74. self._add_segment("qcurve", self._last_pt, *points)
  75. def curveTo(self, *points):
  76. self._add_segment("curve", self._last_pt, *points)
  77. def closePath(self):
  78. self._add_segment("close")
  79. def endPath(self):
  80. self._add_segment("end")
  81. def addComponent(self, glyphName, transformation):
  82. pass
  83. def _get_segments(glyph):
  84. """Get a glyph's segments as extracted by GetSegmentsPen."""
  85. pen = GetSegmentsPen()
  86. # glyph.draw(pen)
  87. # We can't simply draw the glyph with the pen, but we must initialize the
  88. # PointToSegmentPen explicitly with outputImpliedClosingLine=True.
  89. # By default PointToSegmentPen does not outputImpliedClosingLine -- unless
  90. # last and first point on closed contour are duplicated. Because we are
  91. # converting multiple glyphs at the same time, we want to make sure
  92. # this function returns the same number of segments, whether or not
  93. # the last and first point overlap.
  94. # https://github.com/googlefonts/fontmake/issues/572
  95. # https://github.com/fonttools/fonttools/pull/1720
  96. pointPen = PointToSegmentPen(pen, outputImpliedClosingLine=True)
  97. glyph.drawPoints(pointPen)
  98. return pen.segments
  99. def _set_segments(glyph, segments, reverse_direction):
  100. """Draw segments as extracted by GetSegmentsPen back to a glyph."""
  101. glyph.clearContours()
  102. pen = glyph.getPen()
  103. if reverse_direction:
  104. pen = ReverseContourPen(pen)
  105. for tag, args in segments:
  106. if tag == "move":
  107. pen.moveTo(*args)
  108. elif tag == "line":
  109. pen.lineTo(*args)
  110. elif tag == "curve":
  111. pen.curveTo(*args[1:])
  112. elif tag == "qcurve":
  113. pen.qCurveTo(*args[1:])
  114. elif tag == "close":
  115. pen.closePath()
  116. elif tag == "end":
  117. pen.endPath()
  118. else:
  119. raise AssertionError('Unhandled segment type "%s"' % tag)
  120. def _segments_to_quadratic(segments, max_err, stats, all_quadratic=True):
  121. """Return quadratic approximations of cubic segments."""
  122. assert all(s[0] == "curve" for s in segments), "Non-cubic given to convert"
  123. new_points = curves_to_quadratic([s[1] for s in segments], max_err, all_quadratic)
  124. n = len(new_points[0])
  125. assert all(len(s) == n for s in new_points[1:]), "Converted incompatibly"
  126. spline_length = str(n - 2)
  127. stats[spline_length] = stats.get(spline_length, 0) + 1
  128. if all_quadratic or n == 3:
  129. return [("qcurve", p) for p in new_points]
  130. else:
  131. return [("curve", p) for p in new_points]
  132. def _glyphs_to_quadratic(glyphs, max_err, reverse_direction, stats, all_quadratic=True):
  133. """Do the actual conversion of a set of compatible glyphs, after arguments
  134. have been set up.
  135. Empty glyphs (without contours) are ignored and passed through unchanged.
  136. Return True if the glyphs were modified, else return False.
  137. """
  138. # Skip empty glyphs (with zero contours)
  139. non_empty_indices = [i for i, g in enumerate(glyphs) if len(g) > 0]
  140. if not non_empty_indices:
  141. return False
  142. glyphs = [glyphs[i] for i in non_empty_indices]
  143. max_err = [max_err[i] for i in non_empty_indices]
  144. try:
  145. segments_by_location = zip(*[_get_segments(g) for g in glyphs])
  146. except UnequalZipLengthsError:
  147. raise IncompatibleSegmentNumberError(glyphs)
  148. if not any(segments_by_location):
  149. return False
  150. # always modify input glyphs if reverse_direction is True
  151. glyphs_modified = reverse_direction
  152. new_segments_by_location = []
  153. incompatible = {}
  154. for i, segments in enumerate(segments_by_location):
  155. tag = segments[0][0]
  156. if not all(s[0] == tag for s in segments[1:]):
  157. incompatible[i] = [s[0] for s in segments]
  158. elif tag == "curve":
  159. new_segments = _segments_to_quadratic(
  160. segments, max_err, stats, all_quadratic
  161. )
  162. if all_quadratic or new_segments != segments:
  163. glyphs_modified = True
  164. segments = new_segments
  165. new_segments_by_location.append(segments)
  166. if glyphs_modified:
  167. new_segments_by_glyph = zip(*new_segments_by_location)
  168. for glyph, new_segments in zip(glyphs, new_segments_by_glyph):
  169. _set_segments(glyph, new_segments, reverse_direction)
  170. if incompatible:
  171. raise IncompatibleSegmentTypesError(glyphs, segments=incompatible)
  172. return glyphs_modified
  173. def glyphs_to_quadratic(
  174. glyphs, max_err=None, reverse_direction=False, stats=None, all_quadratic=True
  175. ):
  176. """Convert the curves of a set of compatible of glyphs to quadratic.
  177. All curves will be converted to quadratic at once, ensuring interpolation
  178. compatibility. If this is not required, calling glyphs_to_quadratic with one
  179. glyph at a time may yield slightly more optimized results.
  180. Empty glyphs (without contours) are ignored and passed through unchanged.
  181. Return True if glyphs were modified, else return False.
  182. Raises IncompatibleGlyphsError if glyphs have non-interpolatable outlines.
  183. """
  184. if stats is None:
  185. stats = {}
  186. if max_err is None:
  187. # assume 1000 is the default UPEM
  188. max_err = DEFAULT_MAX_ERR * 1000
  189. if isinstance(max_err, (list, tuple)):
  190. max_errors = max_err
  191. else:
  192. max_errors = [max_err] * len(glyphs)
  193. _validate_length(max_errors, len(glyphs), "max_err")
  194. _validate_positive_tolerances(max_errors, "max_err")
  195. return _glyphs_to_quadratic(
  196. glyphs, max_errors, reverse_direction, stats, all_quadratic
  197. )
  198. def fonts_to_quadratic(
  199. fonts,
  200. max_err_em=None,
  201. max_err=None,
  202. reverse_direction=False,
  203. stats=None,
  204. dump_stats=False,
  205. remember_curve_type=True,
  206. all_quadratic=True,
  207. ):
  208. """Convert the curves of a collection of fonts to quadratic.
  209. All curves will be converted to quadratic at once, ensuring interpolation
  210. compatibility. If this is not required, calling fonts_to_quadratic with one
  211. font at a time may yield slightly more optimized results.
  212. Empty glyphs (without contours) are ignored and passed through unchanged.
  213. Return the set of modified glyph names if any, else return an empty set.
  214. By default, cu2qu stores the curve type in the fonts' lib, under a private
  215. key "com.github.googlei18n.cu2qu.curve_type", and will not try to convert
  216. them again if the curve type is already set to "quadratic".
  217. Setting 'remember_curve_type' to False disables this optimization.
  218. Raises IncompatibleFontsError if same-named glyphs from different fonts
  219. have non-interpolatable outlines.
  220. """
  221. if remember_curve_type:
  222. curve_types = {f.lib.get(CURVE_TYPE_LIB_KEY, "cubic") for f in fonts}
  223. if len(curve_types) == 1:
  224. curve_type = next(iter(curve_types))
  225. if curve_type in ("quadratic", "mixed"):
  226. logger.info("Curves already converted to quadratic")
  227. return False
  228. elif curve_type == "cubic":
  229. pass # keep converting
  230. else:
  231. raise NotImplementedError(curve_type)
  232. elif len(curve_types) > 1:
  233. # going to crash later if they do differ
  234. logger.warning("fonts may contain different curve types")
  235. if stats is None:
  236. stats = {}
  237. if max_err_em is not None and max_err is not None:
  238. raise TypeError("Only one of max_err and max_err_em can be specified.")
  239. if max_err_em is None and max_err is None:
  240. max_err_em = DEFAULT_MAX_ERR
  241. if isinstance(max_err, (list, tuple)):
  242. _validate_length(max_err, len(fonts), "max_err")
  243. max_errors = max_err
  244. _validate_positive_tolerances(max_errors, "max_err")
  245. elif max_err is not None:
  246. _validate_positive_tolerance(max_err, "max_err")
  247. max_errors = [max_err] * len(fonts)
  248. if isinstance(max_err_em, (list, tuple)):
  249. _validate_length(max_err_em, len(fonts), "max_err_em")
  250. _validate_positive_tolerances(max_err_em, "max_err_em")
  251. max_errors = [f.info.unitsPerEm * e for f, e in zip(fonts, max_err_em)]
  252. elif max_err_em is not None:
  253. _validate_positive_tolerance(max_err_em, "max_err_em")
  254. max_errors = [f.info.unitsPerEm * max_err_em for f in fonts]
  255. modified = set()
  256. glyph_errors = {}
  257. for name in set().union(*(f.keys() for f in fonts)):
  258. glyphs = []
  259. cur_max_errors = []
  260. for font, error in zip(fonts, max_errors):
  261. if name in font:
  262. glyphs.append(font[name])
  263. cur_max_errors.append(error)
  264. try:
  265. if _glyphs_to_quadratic(
  266. glyphs, cur_max_errors, reverse_direction, stats, all_quadratic
  267. ):
  268. modified.add(name)
  269. except IncompatibleGlyphsError as exc:
  270. logger.error(exc)
  271. glyph_errors[name] = exc
  272. if glyph_errors:
  273. raise IncompatibleFontsError(glyph_errors)
  274. if modified and dump_stats:
  275. spline_lengths = sorted(stats.keys())
  276. logger.info(
  277. "New spline lengths: %s"
  278. % (", ".join("%s: %d" % (l, stats[l]) for l in spline_lengths))
  279. )
  280. if remember_curve_type:
  281. for font in fonts:
  282. curve_type = font.lib.get(CURVE_TYPE_LIB_KEY, "cubic")
  283. new_curve_type = "quadratic" if all_quadratic else "mixed"
  284. if curve_type != new_curve_type:
  285. font.lib[CURVE_TYPE_LIB_KEY] = new_curve_type
  286. return modified
  287. def glyph_to_quadratic(glyph, **kwargs):
  288. """Convenience wrapper around glyphs_to_quadratic, for just one glyph.
  289. Return True if the glyph was modified, else return False.
  290. """
  291. return glyphs_to_quadratic([glyph], **kwargs)
  292. def font_to_quadratic(font, **kwargs):
  293. """Convenience wrapper around fonts_to_quadratic, for just one font.
  294. Return the set of modified glyph names if any, else return empty set.
  295. """
  296. return fonts_to_quadratic([font], **kwargs)