From 84f4d3d112cdb3a81ada017a37d39f5334526385 Mon Sep 17 00:00:00 2001 From: "Jason T. Bradshaw" Date: Fri, 1 Nov 2013 10:11:10 -0500 Subject: [PATCH] Add autowidth configuration --- README.md | 1 + lib/fontcustom.rb | 1 + lib/fontcustom/cli.rb | 4 ++++ lib/fontcustom/generator/font.rb | 3 ++- lib/fontcustom/options.rb | 4 ++-- lib/fontcustom/scripts/generate.py | 21 +++++++++++++++------ lib/fontcustom/templates/fontcustom.yml | 4 ++++ 7 files changed, 29 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index b8bc3400..c4184cbf 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ preprocessor_path: "" # Font path used in CSS proprocessor templ data_cache: (same as fontcustom.yml) # Sets location of data file debug: false # Output raw messages from fontforge quiet: false # Silence all output messages +autowidth: true # Automatically size glyphs based on the width of their individual vectors templates: [ css, preview ] # Templates to generate alongside fonts # Possible values: preview, css, scss, scss-rails, bootstrap, # bootstrap-scss, bootstrap-ie7, bootstrap-ie7-scss diff --git a/lib/fontcustom.rb b/lib/fontcustom.rb index a4d71fc6..bf5780d4 100644 --- a/lib/fontcustom.rb +++ b/lib/fontcustom.rb @@ -42,6 +42,7 @@ def gem_lib :css_prefix => "icon-", :data_cache => nil, :preprocessor_path => nil, + :autowidth => true, :no_hash => false, :debug => false, :quiet => false diff --git a/lib/fontcustom/cli.rb b/lib/fontcustom/cli.rb index 81385737..2cfa341d 100644 --- a/lib/fontcustom/cli.rb +++ b/lib/fontcustom/cli.rb @@ -41,6 +41,10 @@ class CLI < Thor class_option :preprocessor_path, :aliases => "-s", :type => :string, :desc => "Optional font path for CSS proprocessor templates." + class_option :autowidth, :aliases => "-a", :type => :boolean, + :desc => "Auto-size glyphs to their individual vector widths.", + :default => DEFAULT_OPTIONS[:autowidth] + class_option :no_hash, :aliases => "-h", :type => :boolean, :desc => "Generate fonts without asset-busting hashes." diff --git a/lib/fontcustom/generator/font.rb b/lib/fontcustom/generator/font.rb index 6dba69d7..0f3ac817 100644 --- a/lib/fontcustom/generator/font.rb +++ b/lib/fontcustom/generator/font.rb @@ -54,7 +54,8 @@ def reset_output end def generate - cmd = "fontforge -script #{Fontcustom.gem_lib}/scripts/generate.py #{opts.input[:vectors]} #{opts.output[:fonts]} --name #{opts.font_name}" + cmd = "fontforge -script #{Fontcustom.gem_lib}/scripts/generate.py #{opts.input[:vectors]} #{opts.output[:fonts]} --name #{opts.font_name}" + cmd += " --autowidth" if opts.autowidth cmd += " --nohash" if opts.no_hash cmd += " --debug" if opts.debug output, err, status = execute_and_clean(cmd) diff --git a/lib/fontcustom/options.rb b/lib/fontcustom/options.rb index 7e80d261..94469c87 100644 --- a/lib/fontcustom/options.rb +++ b/lib/fontcustom/options.rb @@ -8,7 +8,7 @@ module Fontcustom class Options include Util - attr_reader :project_root, :input, :output, :config, :templates, :font_name, :css_prefix, :data_cache, :preprocessor_path, :no_hash, :debug, :quiet, :skip_first + attr_reader :project_root, :input, :output, :config, :templates, :font_name, :css_prefix, :data_cache, :preprocessor_path, :autowidth, :no_hash, :debug, :quiet, :skip_first def initialize(options = {}) check_fontforge @@ -95,7 +95,7 @@ def merge_options remove_instance_variable :@cli_options # :config is excluded since it's already been set - keys = %w|project_root input output data_cache templates font_name css_prefix preprocessor_path skip_first no_hash debug quiet| + keys = %w|project_root input output data_cache templates font_name css_prefix preprocessor_path skip_first autowidth no_hash debug quiet| keys.each { |key| instance_variable_set("@#{key}", options[key.to_sym]) } @font_name = @font_name.strip.gsub(/\W/, "-") diff --git a/lib/fontcustom/scripts/generate.py b/lib/fontcustom/scripts/generate.py index b98f5ab2..c1ed57f7 100755 --- a/lib/fontcustom/scripts/generate.py +++ b/lib/fontcustom/scripts/generate.py @@ -10,6 +10,7 @@ parser = argparse.ArgumentParser(description='Convert a directory of svg and eps files into a unified font file.') parser.add_argument('dir', metavar='directory', type=unicode, nargs=2, help='directory of vector files') parser.add_argument('--name', metavar='fontname', type=unicode, nargs='?', help='reference name of the font (no spaces)') + parser.add_argument('--autowidth', '-a', action='store_true', help='automatically size generated glyphs to their vector width') parser.add_argument('--nohash', '-n', action='store_true', help='disable hash fingerprinting of font files') parser.add_argument('--debug', '-d', action='store_true', help='display debug messages') args = parser.parse_args() @@ -20,6 +21,7 @@ import optparse parser = optparse.OptionParser(description='Convert a directory of svg and eps files into a unified font file.') parser.add_option('--name', metavar='fontname', type='string', nargs='?', help='reference name of the font (no spaces)') + parser.add_option('--autowidth', '-a', action='store_true', help='automatically size generated glyphs to their vector width') parser.add_option('--nohash', '-n', action='store_true', help='disable hash fingerprinting of font files') parser.add_argument('--debug', '-d', action='store_true', help='display debug messages') (args, posargs) = parser.parse_args() @@ -56,7 +58,7 @@ # replace the tags with 'nothing' svgtext = svgtext.replace('', '') svgtext = svgtext.replace('', '') - + tmpsvgfile.file.write(svgtext) svgfile.close() @@ -64,7 +66,7 @@ filePath = tmpsvgfile.name # end hack - + m.update(filename + str(size) + ';') glyph = f.createChar(cp) glyph.importOutlines(filePath) @@ -75,18 +77,25 @@ # glyph.left_side_bearing = KERNING # glyph.right_side_bearing = KERNING - #glyph.width = 512 # possible optimization? # glyph.simplify() # glyph.round() - glyph.left_side_bearing = glyph.right_side_bearing = 0 - glyph.round() + + # set glyph size explicitly or automatically depending on autowidth + if args.autowidth: + glyph.left_side_bearing = glyph.right_side_bearing = 0 + glyph.round() + else: + # force a manual size when autowidth is disabled + glyph.width = 512 files.append(name) cp += 1 - f.autoWidth(0, 0, 512) + # resize glyphs if autowidth is enabled + if args.autowidth: + f.autoWidth(0, 0, 512) if args.nohash: fontfile = outdir + '/' + args.name diff --git a/lib/fontcustom/templates/fontcustom.yml b/lib/fontcustom/templates/fontcustom.yml index 2ba3eacf..2b2801e2 100644 --- a/lib/fontcustom/templates/fontcustom.yml +++ b/lib/fontcustom/templates/fontcustom.yml @@ -11,6 +11,10 @@ #debug: false #quiet: false +# setting this to false allows the creation of stacking icons, since all glyphs +# will retain their original positions. +#autowidth: true + # ---------------------------------------------------------------------------- # # Project Paths