Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add configurable autowidth to allow creation of stacking icon sets #137

Merged
merged 1 commit into from
Nov 1, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/fontcustom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/fontcustom/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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."

Expand Down
3 changes: 2 additions & 1 deletion lib/fontcustom/generator/font.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/fontcustom/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/, "-")
Expand Down
21 changes: 15 additions & 6 deletions lib/fontcustom/scripts/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -56,15 +58,15 @@
# replace the <switch> </switch> tags with 'nothing'
svgtext = svgtext.replace('<switch>', '')
svgtext = svgtext.replace('</switch>', '')

tmpsvgfile.file.write(svgtext)

svgfile.close()
tmpsvgfile.file.close()

filePath = tmpsvgfile.name
# end hack

m.update(filename + str(size) + ';')
glyph = f.createChar(cp)
glyph.importOutlines(filePath)
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/fontcustom/templates/fontcustom.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down