Skip to content

Commit 2012924

Browse files
committed
Add ability to convert all glyphs
The UI still needs work, but we can now produce an output TTF with all glyphs converted to a dotted version.
1 parent 9683d01 commit 2012924

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

src/extractpoints.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
)
3939

4040
DEFAULT_FONT = '/usr/share/fonts/truetype/padauk/Padauk.ttf'
41-
DEFAULT_GLYPH = 'u1021'
41+
DEFAULT_GLYPH = None
4242

4343
# ==============
4444
# This section is for functions that calculate and return a different data type
@@ -513,6 +513,23 @@ def silent_fontopen(fname):
513513
os.close(origstderr)
514514
return fontobj
515515

516+
def create_dotted_font(fname):
517+
input_font = silent_fontopen(fname)
518+
global args
519+
args.em = input_font.em
520+
new_font = fontforge.font()
521+
for glyphname in input_font:
522+
print "Processing glyph named", glyphname
523+
if glyphname in ('.notdef', '.null'): continue
524+
glyph = input_font[glyphname]
525+
glyph.unlinkRef()
526+
new_glyph = new_font.createChar(glyph.encoding)
527+
copy_glyph(glyph, new_glyph)
528+
new_font.generate(args.output)
529+
dots = extract_dots(glyph)
530+
print "{} dots found".format(len(dots))
531+
wait_for_keypress(args.em, args.zoom)
532+
516533
def extraction_demo(fname, letter):
517534
font = silent_fontopen(fname)
518535
global args
@@ -528,7 +545,7 @@ def extraction_demo(fname, letter):
528545
new_font = fontforge.font()
529546
new_glyph = new_font.createChar(glyph.encoding)
530547
copy_glyph(glyph, new_glyph)
531-
new_font.generate('output.ttf') # TODO: Make this a configurable parameter
548+
new_font.generate(args.output)
532549
dots = extract_dots(glyph)
533550
print "{} dots found".format(len(dots))
534551
wait_for_keypress(args.em, args.zoom)
@@ -541,6 +558,7 @@ def extract_dots(glyph, show_glyph=True):
541558
alltriangles = []
542559
allmidpoints = []
543560
allmidlines = []
561+
dots = [] # Default value for blank glyphs; will be overwritten if glyph is non-blank
544562
# Calculate stroke width by first extracting vectors with no subdivision;
545563
# then convert to a Shapely polygon and calculate stroke width via the
546564
# 2*area / length algorithm. Then re-extract vectors with the real
@@ -668,6 +686,7 @@ def parse_args():
668686
parser.add_argument('-v', '--verbose', action = "store_true", help = "Give more verbose error messages")
669687
parser.add_argument("inputfilename", nargs = "?", default = DEFAULT_FONT, help = "Font file (SFD or TTF format)")
670688
parser.add_argument("glyphname", nargs = "?", default = DEFAULT_GLYPH, help = "Glyph name (or Unicode codepoint in U+89AB format)")
689+
parser.add_argument('-o', '--output', action = "store", default = "output.ttf", help = "Filename of output dotted TTF")
671690
parser.add_argument('-z', '--zoom', action = "store", type = float, default = 1.0, help = "Zoom level (default 1.0)")
672691
parser.add_argument('-m', '--minstrokewidth', action = "store", type = float, default = 1, help = "The minimum stroke width (useful for fine-tuning the triangulation for certain glyphs, not required most of the time)")
673692
parser.add_argument('-M', '--maxstrokewidth', action = "store", type = float, default = 1e100, help = "The maximum stroke width (useful for fine-tuning the triangulation for certain glyphs, not required most of the time)")
@@ -678,8 +697,6 @@ def parse_args():
678697
parser.add_argument('-r', '--radius', action = "store", type = float, default = 5, help = "Radius of dots, in em units (default 5)")
679698
parser.add_argument('-s', '--spacing', action = "store", type = float, default = 3.0, help = "Spacing of dots, as a multiple of dot radius (default 3.0 for 300%%)")
680699
args = parser.parse_args()
681-
args.svgfilename = args.glyphname + '.svg'
682-
args.datfilename = args.glyphname + '.dat'
683700
if not (args.show_triangles or args.show_lines or args.show_dots):
684701
parser.print_help()
685702
print
@@ -692,7 +709,10 @@ def main():
692709
a sanity check to make sure everything works properly."""
693710
global args
694711
args = parse_args()
695-
extraction_demo(args.inputfilename, args.glyphname)
712+
if args.glyphname is None:
713+
create_dotted_font(args.inputfilename)
714+
else:
715+
extraction_demo(args.inputfilename, args.glyphname)
696716
return 0
697717

698718
if __name__ == "__main__":

0 commit comments

Comments
 (0)