agric 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,217 @@
1
+ require 'rubygems'
2
+ require 'fileutils'
3
+ require 'yaml'
4
+ require 'nokogiri'
5
+ require 'agric'
6
+
7
+ module Agric
8
+ module Compiler
9
+
10
+ class << self
11
+
12
+ def command(cmds)
13
+ puts "$ " + cmds
14
+ system(cmds)
15
+ end
16
+
17
+ SVG_NAMESPACES = {
18
+ :dc => "http://purl.org/dc/elements/1.1/",
19
+ :cc => "http://creativecommons.org/ns#",
20
+ :rdf => "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
21
+ :svg => "http://www.w3.org/2000/svg",
22
+ :sodipodi => "http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd",
23
+ :inkscape => "http://www.inkscape.org/namespaces/inkscape"
24
+ }
25
+
26
+ META = {
27
+ :filename => "agric",
28
+ :family => "Agric",
29
+ :version => Agric::VERSION,
30
+ :name => "agric"
31
+ }
32
+
33
+ def compile!(options = {})
34
+ root = Agric.root
35
+ sources = root.join("src")
36
+ lib = root.join("lib")
37
+ assets = lib.join("assets")
38
+ compiler_dir = Pathname.new(File.expand_path(__FILE__)).dirname.join("compiler")
39
+ convert_script = compiler_dir.join("convert.pe")
40
+
41
+ output_font_file = assets.join("fonts", "#{META[:filename]}.svg")
42
+ font_awesome_dir = Pathname.new(Dir.home).join(".font-awesome")
43
+ glyphs = root.join("tmp", "glyphs")
44
+
45
+ # Get latest Font-Awesome
46
+ FileUtils.mkdir_p(font_awesome_dir.dirname)
47
+ if font_awesome_dir.join(".git").exist?
48
+ Dir.chdir(font_awesome_dir) do
49
+ command("git pull")
50
+ end
51
+ else
52
+ command("git clone git@github.com:FortAwesome/Font-Awesome.git #{font_awesome_dir}")
53
+ end
54
+
55
+ unless options[:explode].is_a?(FalseClass)
56
+
57
+ # Normalize Font-Awesome
58
+ awesome_dir = sources.join("001-awesome")
59
+ FileUtils.rm_rf(awesome_dir)
60
+ FileUtils.mkdir_p(awesome_dir)
61
+ # FileUtils.cp(font_awesome_dir.join("build", "icons.yml"), awesome_dir.join("config.yml"))
62
+ File.open(awesome_dir.join("config.yml"), "wb") do |f|
63
+ icons = YAML.load_file(font_awesome_dir.join("src", "icons.yml"))
64
+ # raise icons.inspect
65
+ config = {"glyphs" => icons["icons"].collect{|h| {"css" => h["id"], "from" => "0x" + h["unicode"]} } }
66
+ f.write(config.to_yaml)
67
+ end
68
+
69
+ forgotten_names = {
70
+ "_279" => "info",
71
+ "_283" => "eraser",
72
+ "_303" => "rss_sign",
73
+ "_312" => "external_link_sign",
74
+ "_317" => "expand",
75
+ "_329" => "sort_by_alphabet_alt",
76
+ "_334" => "thumbs_up",
77
+ "_335" => "thumbs_down",
78
+ "_366" => "moon",
79
+ "f0fe" => "plus_sign_alt",
80
+ "f171" => "bitbucket"
81
+ }
82
+
83
+ # FileUtils.cp(font_awesome_dir.join("build", "assets", "font-awesome", "font", "fontawesome-webfont.svg"), awesome_dir.join("font.svg"))
84
+ source = font_awesome_dir.join("src", "assets", "font-awesome", "font", "FontAwesome.otf")
85
+ command("fontforge -script #{convert_script} #{source.to_s} svg")
86
+ interm = source.dirname.join("FontAwesome.svg")
87
+ File.open(interm) do |i|
88
+ doc = Nokogiri::XML(i) do |config|
89
+ config.nonet.strict.noblanks
90
+ end
91
+ doc.root.xpath("//glyph[@d]").each do |glyph|
92
+ name = glyph.attr("glyph-name")
93
+ name = forgotten_names[name] || name
94
+ puts " !Weird name: #{name}" unless name =~ /^[a-z0-9]+((\_|\-)[a-z0-9]+)*$/
95
+ name.gsub!(/[^a-z0-9]+/, '-')
96
+ glyph["glyph-name"] = name
97
+ end
98
+ doc.root.default_namespace = SVG_NAMESPACES[:svg]
99
+ for name, url in SVG_NAMESPACES
100
+ doc.root.add_namespace(name.to_s, url)
101
+ end
102
+ File.open(awesome_dir.join("font.svg"), "wb") do |f|
103
+ f.write doc.to_s
104
+ end
105
+ end
106
+
107
+ puts "-" * 80
108
+
109
+ # Explodes all font characters in one dir
110
+ FileUtils.rm_rf(glyphs)
111
+ FileUtils.mkdir_p(glyphs)
112
+ Dir.chdir(sources) do
113
+ for font_fullname in Dir["*"].sort
114
+ font_dir = sources.join(font_fullname)
115
+ font_name = font_fullname.split("-")[1..-1].join("-")
116
+ font_file = font_dir.join("font.svg")
117
+ config_file = font_dir.join("config.yml")
118
+ if font_file.exist? and config_file.exist?
119
+ command("svg-font-dump -n -c #{config_file} -f -i #{font_file} -o #{glyphs} ")
120
+ end
121
+ end
122
+ end
123
+
124
+ end
125
+
126
+
127
+ config_file = compiler_dir.join('config.yml')
128
+ config = {
129
+ "font" => {
130
+ "version" => META[:version],
131
+ "fontname" => META[:name],
132
+ "fullname" => "#{META[:family]} (#{META[:name]})",
133
+ "familyname" => META[:family],
134
+ "copyright" => "Copyright (C) 2013 by #{META[:family]}",
135
+ "ascent" => 850,
136
+ "descent" => 150,
137
+ "weight" => "Regular"
138
+ }
139
+ }
140
+
141
+ reference_file = compiler_dir.join('reference.yml')
142
+ reference = YAML.load_file(reference_file)
143
+
144
+ icons = {}
145
+
146
+ Dir.chdir(glyphs) do
147
+ config["glyphs"] = Dir.glob("*.svg").sort.collect do |cf|
148
+ name = cf.split(/\./).first
149
+ icons[name] = reference[name] || (reference.values.sort.last || "efff").to_i(16).succ.to_s(16)
150
+ reference[name] = icons[name]
151
+ {"css" => name, "code" => icons[name].to_i(16)}
152
+ end
153
+ end
154
+
155
+ # Removes undefined glyphs from reference
156
+ for ref in reference.keys
157
+ reference.delete(ref) unless icons.keys.include?(ref)
158
+ end
159
+
160
+ File.open(reference_file, "wb") do |f|
161
+ f.write reference.to_yaml
162
+ end
163
+
164
+ File.open(config_file, "wb") do |f|
165
+ f.write config.to_yaml
166
+ end
167
+
168
+ # Recompose font
169
+ command("svg-font-create -c #{config_file} -s #{compiler_dir.join('svgo.yml')} -i #{glyphs} -o #{output_font_file}")
170
+
171
+ puts "-" * 80
172
+
173
+
174
+ # Convert SVG font to all needed format
175
+ command("fontforge -script #{convert_script} #{output_font_file} ttf")
176
+ command("fontforge -script #{convert_script} #{output_font_file} woff")
177
+ command("fontforge -script #{convert_script} #{output_font_file} eot")
178
+ command("rm -f #{output_font_file.dirname.join('*.afm')}")
179
+
180
+ # Write SCSS file to manage list of icons
181
+ File.open(lib.join("agric", "compass", "stylesheets", "agric", "_paths.scss"), "wb") do |f|
182
+ f.write "/* Auto-generated. Nothing to touch */\n"
183
+ f.write "@font-face {\n"
184
+ f.write " font-family: '#{META[:family]}';\n"
185
+ f.write " font-weight: normal;\n"
186
+ f.write " font-style: normal;\n"
187
+ f.write " src: font-url('#{META[:filename]}.eot?v=#{META[:version]}');\n"
188
+ f.write " src: font-url('#{META[:filename]}.eot?#iefix&v=#{META[:version]}') format('embedded-opentype'),\n"
189
+ f.write " font-url('#{META[:filename]}.woff?v=#{META[:version]}') format('woff'),\n"
190
+ f.write " font-url('#{META[:filename]}.ttf?v=#{META[:version]}') format('truetype'),\n"
191
+ f.write " font-url('#{META[:filename]}.svg?v=#{META[:version]}') format('svg');\n"
192
+ f.write "}\n"
193
+ end
194
+
195
+ File.open(lib.join("agric", "compass", "stylesheets", "agric", "_icons.scss"), "wb") do |f|
196
+ f.write "/* Auto-generated. Nothing to touch */\n"
197
+ for name, code in icons
198
+ f.write "$agric-icons-#{name}: \"\\#{code}\";\n"
199
+ end
200
+ f.write "\n"
201
+ for name, code in icons
202
+ f.write ".icon-#{name}:before { content: $agric-icons-#{name} };\n"
203
+ end
204
+ # f.write "$agric-icons: (" + icons.collect{|k,v| "(#{k} \"\\#{v}\")"}.join(" ") + ");\n"
205
+ # f.write "$agric-icons: (" + icons.collect{|k,v| "(#{k} \"\\#{v}\")"}.join(" ") + ");\n"
206
+ # f.write "$agric-icon-names: (" + icons.keys.join(" ") + ");\n"
207
+ # f.write "@mixin icon-agric(): (" + icons.keys.join(" ") + ");\n"
208
+ end
209
+
210
+ end
211
+
212
+
213
+
214
+ end
215
+
216
+ end
217
+ end
@@ -0,0 +1,8 @@
1
+ require 'agric/compiler'
2
+
3
+ desc "Compile font Agric #{Agric::VERSION}"
4
+ task :compile do
5
+ Agric::Compiler.compile!
6
+ end
7
+
8
+ # task :build => :compile
@@ -0,0 +1,6 @@
1
+ module Agric
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module Agric
2
+ VERSION = "0.0.1"
3
+ end
data/lib/agric.rb ADDED
@@ -0,0 +1,21 @@
1
+ require "agric/version"
2
+ require "agric/engine" if defined?(::Rails)
3
+ require 'compass'
4
+ require 'pathname'
5
+
6
+ module Agric
7
+
8
+ def self.root
9
+ Pathname.new(File.expand_path(__FILE__)).dirname.dirname
10
+ end
11
+
12
+ def self.compass_extension_path
13
+ root.join("lib", "agric", "compass")
14
+ end
15
+
16
+
17
+ autoload :Compiler, 'agric/compiler'
18
+ end
19
+
20
+ # Compass registration
21
+ Compass::Frameworks.register('agric', :path => Agric.compass_extension_path)
Binary file