sass-fontimage 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,6 @@
1
+ .DS_Store
2
+ test/scss/output
3
+ test/output
4
+ test/scss/*.css
5
+ test/scss/**/*.css
6
+ .sass-cache/
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use ruby-1.9.3-p125@sass-fontimage
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
@@ -0,0 +1,12 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ rmagick (2.13.1)
5
+ sass (3.1.18)
6
+
7
+ PLATFORMS
8
+ ruby
9
+
10
+ DEPENDENCIES
11
+ rmagick
12
+ sass
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Flurin Egger, DigitPaint (http://www.digitpaint.nl)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ # Sass-FontImage
2
+
3
+ A small extension for SASS that generates images for icon fonts. This makes it easy to use an image font with `:before` and `:after` in browser that support and let's you generate the images on the fly for
4
+ browser who don't
5
+
6
+ ## Usage
7
+
8
+ We use sass from our custom build script so we add the Rack plugin ourselves. We currently don't support the default `sass` executable.
9
+
10
+ Setting up Sass-FontImage with the Rack plugin:
11
+
12
+ # This must be done AFTER you require 'sass/plugin/rack'
13
+ require 'sass-fontimage'
14
+
15
+ # ...
16
+
17
+ # Configure the path where sass-fontimage should write the images
18
+ # the path MUST exist.
19
+ Sass::Plugin.options[:custom] ||= {}
20
+ Sass::Plugin.options[:custom][:font_image_location] = "./html/images/fonts"
21
+
22
+ ## Syntax
23
+
24
+ background: font_image(CHARACTER, FONTSIZE, COLOR, RELATIVE_PATH_TO_FONT)
25
+
26
+ ### Example
27
+
28
+ background: font_image("\E006", 16px, #f00, "../fonts/testfont-regular.ttf")
29
+
30
+ ## Copyright & license
31
+ Copyright (c) 2012 Flurin Egger, DigitPaint, MIT Style License. (see MIT-LICENSE)
@@ -0,0 +1,15 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+
4
+ require 'pathname'
5
+ require 'rmagick'
6
+
7
+ # initialize module namespace
8
+ module SassFontimage; end
9
+
10
+
11
+ require_relative "sass_fontimage/font_image"
12
+
13
+ if defined?(Sass)
14
+ require_relative "sass_fontimage/sass_extensions"
15
+ end
@@ -0,0 +1,107 @@
1
+ class SassFontimage::FontImage
2
+
3
+ # @param font String Path to a font (one that RMagick can use)
4
+ # @param options Options string
5
+ #
6
+ # @option options size Integer Default font size
7
+ # @option options color String Default color
8
+ # @option options :write_path The path to write the icon to with write (default = "")
9
+ # @option options :file_prefix Prefix to use for filename (default = icon)
10
+ # @option options :file_type Extension to use when writing file (default = png)
11
+ def initialize(font, options = {})
12
+ @font = Pathname.new(font)
13
+
14
+ raise ArgumentError, "Font '#{font}' not found on disk" unless File.exist?(font)
15
+
16
+ defaults = {
17
+ :size => 16,
18
+ :color => "#000000",
19
+ :write_path => "",
20
+ :file_prefix => "icon",
21
+ :file_type => "png"
22
+ }
23
+ @options = defaults.update(options)
24
+ end
25
+
26
+ # Renders a character on a RMagick canvas
27
+ #
28
+ # @see SassFontimage::FontImage#write
29
+ #
30
+ # @return Magick::Image the image with the charactor drawn.
31
+ def render(char, color = @options[:color], size = @options[:size])
32
+ img = Magick::Image.new(size.to_i, size.to_i, Magick::HatchFill.new('transparent', 'transparent'))
33
+
34
+ draw = Magick::Draw.new
35
+
36
+ char = convert_to_unicode(char)
37
+
38
+ draw.font = @font.to_s
39
+ draw.interline_spacing = 0
40
+ draw.pointsize = size.to_i
41
+ draw.gravity = Magick::CenterGravity
42
+ draw.fill = color
43
+ draw.text_antialias = true
44
+
45
+ draw.annotate(img, 0, 0, 0, 0, char)
46
+
47
+ img
48
+ end
49
+
50
+ # Writes the character out to a image file.
51
+ #
52
+ # The resulting filename has the following format:
53
+ # PREFIX-SIZExSIZE-COLOR-HEXCODEPOINT.FILETYPE
54
+ #
55
+ # Example:
56
+ # icon-16x16-000000-f001.png
57
+ #
58
+ #
59
+ # @param char String A one character string, can also be a CSS encoded value (\0000)
60
+ # @param color String An optional color, takes default value otherwise
61
+ # @param size Integer An optional fontsize
62
+ #
63
+ # @return String The path of the written image
64
+ def write(char, color = @options[:color], size = @options[:size])
65
+ path = image_path(char, color, size)
66
+
67
+ # Let's not regenerate the same image if the font hasn't changed
68
+ return path if(path.exist? && @font.mtime <= path.mtime)
69
+
70
+ img = self.render(char, color, size)
71
+
72
+ img.write(path.to_s) do
73
+ self.format = "PNG32"
74
+ end
75
+
76
+ path
77
+ end
78
+
79
+ protected
80
+
81
+ def image_path(char,color,size)
82
+ filename = []
83
+ filename << @options[:file_prefix]
84
+ filename << "#{size}x#{size}"
85
+ filename << color.gsub(/[^a-z0-9_]/i, "")
86
+ filename << convert_to_unicode(char).codepoints.first.to_s(16)
87
+
88
+ filename = filename.join("-") + "." + @options[:file_type]
89
+
90
+ path = Pathname.new(@options[:write_path]) + filename
91
+ end
92
+
93
+ # Converts css escape to true unicode char
94
+ # also validates if it's truly only one character
95
+ def convert_to_unicode(char)
96
+
97
+ if char =~ /\A\\/
98
+ char = char.gsub("\\","") # Fix double escaping
99
+ char = [char[0..-1].to_i(16)].pack("U")
100
+ end
101
+
102
+ raise ArgumentError, "You can only pass one character" if char.size > 1
103
+
104
+ char
105
+ end
106
+
107
+ end
@@ -0,0 +1,14 @@
1
+ # Stub module
2
+ module SassFontimage::SassExtensions; end
3
+
4
+ require_relative "sass_extensions/font_image_generator"
5
+ require_relative "sass_extensions/functions"
6
+
7
+ module Sass::Script::Functions
8
+ include SassFontimage::SassExtensions::Functions
9
+ end
10
+
11
+ # Wierd that this has to be re-included to pick up sub-modules. Ruby bug?
12
+ class Sass::Script::Functions::EvaluationContext
13
+ include Sass::Script::Functions
14
+ end
@@ -0,0 +1,115 @@
1
+ require 'fileutils'
2
+
3
+ class SassFontimage::SassExtensions::FontImageGenerator < Sass::Script::Literal
4
+
5
+ class << self
6
+
7
+ def get(font, env)
8
+ @cache ||= {}
9
+
10
+ font = font.value
11
+
12
+ # Stupid cache mechanism, doesn't normalize paths
13
+ return @cache[env][font] if @cache[env] && @cache[env].respond_to?(:[]) && @cache[env][font]
14
+
15
+ @cache[env] ||= {}
16
+ @cache[env][font] = self.new(font, env)
17
+
18
+ end
19
+
20
+ def find_font(file, env)
21
+ load_paths = env.options[:load_paths].map{|p| Pathname.new(p.to_s) }
22
+
23
+ # Escape glob patterns
24
+ file.gsub(/[\*\[\]\{\}\?]/) do |char|
25
+ "\\#{char}"
26
+ end
27
+
28
+ file = Pathname.new(file)
29
+
30
+ load_paths.each do |p|
31
+ path = file.absolute? ? file : p + file
32
+ if full_path = Dir[path.to_s].first
33
+ return Pathname.new(full_path)
34
+ end
35
+ end
36
+
37
+ Pathname.new("")
38
+ end
39
+
40
+
41
+ end
42
+
43
+ def initialize(font, env)
44
+ # @kwargs = kwargs
45
+ @environment = env
46
+ @font = font
47
+ @path = self.class.find_font(font, env)
48
+
49
+ fontname = @path.basename.to_s.split(".")[0..-2].join(".")
50
+ write_path = self.font_image_location + fontname
51
+
52
+ unless write_path.directory?
53
+ FileUtils.mkdir(write_path)
54
+ end
55
+
56
+ @fontimage = SassFontimage::FontImage.new(@path, { :write_path => write_path })
57
+ end
58
+
59
+ def to_s(kwargs = self.kwargs)
60
+ ""
61
+ end
62
+
63
+ def value
64
+ self
65
+ end
66
+
67
+ # Generate the file if needed
68
+ def generate(char, size, color)
69
+ color = "#" + color.rgb.map{|f| f.to_s(16).rjust(2, "0") }.join()
70
+ path = @fontimage.write(char.value, color, size.to_i)
71
+ font_image_url(path)
72
+ end
73
+
74
+ def respond_to?(meth)
75
+ super || @environment.respond_to?(meth)
76
+ end
77
+
78
+ def method_missing(meth, *args, &block)
79
+ if @environment.respond_to?(meth)
80
+ @environment.send(meth, *args, &block)
81
+ else
82
+ super
83
+ end
84
+ end
85
+
86
+ def font_image_url(path)
87
+
88
+ url = []
89
+ if @environment.options[:custom] && @environment.options[:custom][:font_image_url]
90
+ url << @environment.options[:custom][:font_image_url]
91
+ url << path.to_s
92
+ url.join("/").squeeze("/")
93
+ elsif @environment.options[:css_location]
94
+ base = Pathname.new(@environment.options[:css_location])
95
+ path.relative_path_from(base)
96
+ end
97
+
98
+
99
+
100
+ end
101
+
102
+
103
+ def font_image_location
104
+ if @environment.options[:custom] && @environment.options[:custom][:font_image_location]
105
+ dir = Pathname.new(@environment.options[:custom][:font_image_location])
106
+ raise "Directory :font_image_location ('#{dir}') does not seem to be a directory" unless dir.directory?
107
+ dir
108
+ else
109
+ ""
110
+ end
111
+ end
112
+
113
+
114
+
115
+ end
@@ -0,0 +1,23 @@
1
+ # The actual sass extension
2
+ #
3
+ # It has two configuration parameters that can be passed with the SASS custom
4
+ # configuration parameters.
5
+ #
6
+ # :font_image_location is the base path to write the generated images to
7
+ # :font_image_url is the base url for the directory where all font images reside, by default it's relative to the :css_location path of SASS.
8
+ module SassFontimage::SassExtensions::Functions
9
+
10
+ # Actually generate the font image
11
+ #
12
+ # @param [String] char The character to generate
13
+ # @param [Size] size Size in pixels (can be css value like 16px)
14
+ # @param [Color] color Color in hex, rgba
15
+ # @param [String] font Path to font within configuration root
16
+ def font_image(char, size, color, font)
17
+ generator = SassFontimage::SassExtensions::FontImageGenerator.get(font, self)
18
+ image_url = generator.generate(char, size, color)
19
+
20
+ Sass::Script::String.new("url(#{image_url})")
21
+ end
22
+
23
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ Gem::Specification.new do |s|
4
+ s.name = "sass-fontimage"
5
+ s.version = "0.1.0"
6
+ s.authors = ["Digitpaint"]
7
+ s.email = %q{info@digitpaint.nl}
8
+ s.homepage = %q{http://github.com/digitpaint/sass-fontimage}
9
+ s.description = %q{A small extension for SASS that generates images from icon fonts}
10
+ s.summary = %q{A small extension for SASS that generates images from icon fonts}
11
+
12
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
13
+
14
+ s.require_paths = ["lib"]
15
+
16
+ s.extra_rdoc_files = [
17
+ "README.md"
18
+ ]
19
+
20
+ s.files = `git ls-files --exclude=.gitignore --exclude-standard`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23
+
24
+ s.add_dependency "rmagick"
25
+ s.add_dependency "sass"
26
+ end
27
+
@@ -0,0 +1,33 @@
1
+ require File.dirname(File.expand_path(__FILE__)) + "/../lib/sass-fontimage.rb"
2
+
3
+ require 'sass'
4
+ require 'sass/plugin'
5
+
6
+ require File.dirname(File.expand_path(__FILE__)) + "/../lib/sass_fontimage/sass_extensions.rb"
7
+
8
+ OUT_PATH = Pathname.new(File.dirname(__FILE__)) + "out"
9
+
10
+ scss = <<EOS
11
+ $f : font_for_images("test/fonts/testfont-regular.ttf");
12
+
13
+ #main {
14
+ background: font_image("\\E006", 16px, #f00, $f)
15
+ }
16
+ EOS
17
+
18
+ # engine = Sass::Engine.new(scss, :syntax => :scss)
19
+ # puts engine.render
20
+
21
+ Sass::Plugin.options[:style] = :expanded
22
+ Sass::Plugin.options[:template_location] = File.dirname(__FILE__) + "/scss"
23
+ Sass::Plugin.options[:css_location] = File.dirname(__FILE__) + "/scss"
24
+ Sass::Plugin.options[:custom] ||= {}
25
+ Sass::Plugin.options[:custom][:font_image_location] = File.dirname(__FILE__) + "/output"
26
+ # Sass::Plugin.options[:custom][:font_image_url] = ""
27
+ Sass::Plugin.options[:always_update] = true
28
+
29
+ # compiler = Sass::Plugin::Compiler.new()
30
+
31
+ Sass::Plugin.update_stylesheets()
32
+
33
+
@@ -0,0 +1,5 @@
1
+ @import "src/sub";
2
+
3
+ #main {
4
+ background: font_image("\\E006", 16px, #f00, "../fonts/testfont-regular.ttf")
5
+ }
@@ -0,0 +1,5 @@
1
+ /* Sub file */
2
+
3
+ #sub {
4
+ background: font_image("\\E006", 16px, #f00, "../fonts/testfont-regular.ttf")
5
+ }
@@ -0,0 +1,10 @@
1
+ require File.dirname(File.expand_path(__FILE__)) + "/../lib/sass-fontimage.rb"
2
+
3
+ FONT_PATH = Pathname.new(File.dirname(__FILE__)) + "fonts/testfont-regular.ttf"
4
+ FONT_SIZE = 16
5
+ FONT_COLOR = "#000000"
6
+ CHAR = "_"
7
+
8
+ s = SassFontimage::FontImage.new(FONT_PATH)
9
+
10
+ s.write("\\E006")
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sass-fontimage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Digitpaint
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rmagick
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: sass
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: A small extension for SASS that generates images from icon fonts
47
+ email: info@digitpaint.nl
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files:
51
+ - README.md
52
+ files:
53
+ - .gitignore
54
+ - .rvmrc
55
+ - Gemfile
56
+ - Gemfile.lock
57
+ - MIT_LICENSE
58
+ - README.md
59
+ - lib/sass-fontimage.rb
60
+ - lib/sass_fontimage/font_image.rb
61
+ - lib/sass_fontimage/sass_extensions.rb
62
+ - lib/sass_fontimage/sass_extensions/font_image_generator.rb
63
+ - lib/sass_fontimage/sass_extensions/functions.rb
64
+ - sass-fontimage.gemspec
65
+ - test/fonts/testfont-regular.otf
66
+ - test/fonts/testfont-regular.ttf
67
+ - test/sass_test.rb
68
+ - test/scss/general.scss
69
+ - test/scss/src/_sub.scss
70
+ - test/test.rb
71
+ homepage: http://github.com/digitpaint/sass-fontimage
72
+ licenses: []
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 1.8.24
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: A small extension for SASS that generates images from icon fonts
95
+ test_files:
96
+ - test/fonts/testfont-regular.otf
97
+ - test/fonts/testfont-regular.ttf
98
+ - test/sass_test.rb
99
+ - test/scss/general.scss
100
+ - test/scss/src/_sub.scss
101
+ - test/test.rb