Replies: 7 comments 20 replies
-
THanks! We should probably incorporate this in fonttools. |
Beta Was this translation helpful? Give feedback.
-
What does "if a source TTF has fractional coordinates" mean? TTF by definition cannot contain explicit fractional coordinates. However, most TTFs contain implicit fractional coordinates because of the implied on-curve point exactly half way between two consecutive off-curve points. CFF and CFF2 allow non-integer coordinates, but their representation is significantly less efficient than integers. A 100% accurate integer qu2cu operation is possible by converting each quadratic curve into a cubic curve, but only if the upem is doubled. So all of the n + 0.5 fractional coordinates become 2 n + 1 integer coordinates. It could therefore be a useful option to double the upem in this tool. |
Beta Was this translation helpful? Give feedback.
-
qu2cu introduces its own fractional values... |
Beta Was this translation helpful? Give feedback.
-
I wrote:
Scratch that. Coordinates of cubic control points converted from integer quadratics are fractional in general. |
Beta Was this translation helpful? Give feedback.
-
Workaround: if self.options.safe_mode:
# Create a temporary OTF file with T2CharStringPen without using pathops...
ttf2otf_converter_temp = TrueTypeToCFF(font)
ttf2otf_converter_temp.options.charstring_source = "t2"
ttf2otf_converter_temp.options.subroutinize = False
ttf2otf_converter_temp.options.remove_glyphs = self.options.remove_glyphs
temp_cff_font = ttf2otf_converter_temp.run()
temp_cff_font.flavor = None
temp_cff_file = makeOutputFileName(font.reader.file.name, extension=".otf", suffix="_tmp")
temp_cff_font.save(temp_cff_file)
# ... and convert it back to a temporary TTF file that will be used for conversion
temp_ttf_file = makeOutputFileName(temp_cff_file, extension=".ttf")
run_shell_command(["otf2ttf", temp_cff_file, "-e", "2.0", "-o", temp_ttf_file])
font = TTFont(temp_ttf_file, recalcTimestamp=self.options.recalc_timestamp)
os.remove(temp_cff_file)
os.remove(temp_ttf_file)
def get_t2_charstrings(self) -> dict:
"""
Get CFF charstrings using T2CharStringPen without using pathops
:return: CFF charstrings.
"""
charstrings = {}
glyph_set = self.font.getGlyphSet()
for k, v in glyph_set.items():
# Draw the glyph with T2CharStringPen and get the charstring
t2_pen = T2CharStringPen(v.width, glyphSet=glyph_set)
glyph_set[k].draw(t2_pen)
charstring = t2_pen.getCharString()
charstrings[k] = charstring
return charstrings |
Beta Was this translation helpful? Give feedback.
-
I wonder, how hard would it be to make @ftCLI's ttf2otf script also support converting TrueType-flavored VF to CFF2? cc @behdad |
Beta Was this translation helpful? Give feedback.
-
I resume this topic because I think the script is ready to be added as a snippet, but I'm not opening a PR because I don't know if it's ok to use click to build the command line and cffsubr. The workaround I found to convert oncurve-less contours to cubic is the following:
for k, v in glyph_set.items():
width = v.width
try:
t2_pen = T2CharStringPen(width=width, glyphSet={k: v})
qu2cu_pen = Qu2CuPen(t2_pen, max_err=tolerance, all_cubic=True, reverse_direction=True)
glyph_set[k].draw(qu2cu_pen)
qu2cu_charstrings[k] = t2_pen.getCharString()
except NotImplementedError:
temp_t2_pen = T2CharStringPen(width=width, glyphSet=None)
glyph_set[k].draw(temp_t2_pen)
t2_charstring = temp_t2_pen.getCharString()
tt_pen = TTGlyphPen(glyphSet=None)
cu2qu_pen = Cu2QuPen(other_pen=tt_pen, max_err=tolerance, reverse_direction=False)
t2_charstring.draw(cu2qu_pen)
tt_glyph = tt_pen.glyph()
t2_pen = T2CharStringPen(width=width, glyphSet=None)
qu2cu_pen = Qu2CuPen(t2_pen, max_err=tolerance, all_cubic=True, reverse_direction=True)
tt_glyph.draw(pen=qu2cu_pen, glyfTable=None)
log.info(
f"Failed to convert glyph {k} to cubic at first attempt, but succeeded at second "
f"attempt."
)
charstring = t2_pen.getCharString() Here's the script: ttf2otf.zip
|
Beta Was this translation helpful? Give feedback.
-
Thanks to @justvanrossum and @anthrotype help in #2836 (and thanks to @behdad for implementing qu2cu!), I made a ttf2otf snippet.
In addition to fontTools, requires:
Usage
Screenshot
Link
https://github.com/ftCLI/TTF2OTF
Beta Was this translation helpful? Give feedback.
All reactions