Class: Cytoplasm::FontsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/cytoplasm/fonts_controller.rb

Constant Summary collapse

@@directories =

Configuration

{"fontsquirrel" => "FontSquirrel","googlewebfonts" => "Google Web Fonts"}
@@fontsdir =
"public/fonts"
@@gwf_file =
@@fontsdir + "/googlewebfonts.yml"
@@enabled_file =
@@fontsdir + "/enabled.yml"
@@cache =

For internal usage, DO NOT MODIFY

false

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.directoriesObject



22
23
24
# File 'app/controllers/cytoplasm/fonts_controller.rb', line 22

def directories
	@@directories
end

.disable(dir, fam) ⇒ Object



171
172
173
174
175
176
177
178
# File 'app/controllers/cytoplasm/fonts_controller.rb', line 171

def disable(dir,fam)
	output = {}
	@@cache = fetch_fonts() if @@cache == false
	@@cache["enabled"][dir].delete(fam)
	write_enabled_file()
	@@cache = fetch_fonts()
	return output
end

.enable(dir, fam) ⇒ Object



163
164
165
166
167
168
169
170
# File 'app/controllers/cytoplasm/fonts_controller.rb', line 163

def enable(dir,fam)
	output = {}
	@@cache = fetch_fonts() if @@cache == false
	@@cache["enabled"][dir][fam] = fetch_family(dir,fam)
	write_enabled_file()
	@@cache = fetch_fonts()
	return output
end

.fetch_enabledObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/controllers/cytoplasm/fonts_controller.rb', line 94

def fetch_enabled
	enabled = {"fontsquirrel" => {}, "googlewebfonts" => {}}

	# Attempt to load YAML from @@enabled_file
	yaml = fetch_yaml(@@enabled_file)
	return enabled if yaml == false
	
	# Check that FontSquirrel fonts actually exist in @@fontsdir
	notfound = 0
	if yaml.is_a? Hash and yaml["imported"]
		yaml["fontsquirrel"].each do |fam,variants|
			unless File.exists?(@@fontsdir+"/"+fam)
				yaml["fontsquirrel"].delete(fam)
				notfound += 1
			end
		end
	end
	if notfound>0
		open(@@enabled_file,"w+") {|f| f.write(yaml.to_yaml)}
		return fetch_enabled()
	end
	
	# Fetch enabled fonts lists
	["fontsquirrel","googlewebfonts"].each {|dir| yaml[dir].each {|fam,variants| enabled[dir][fam] = fetch_family(dir,fam)} if yaml.is_a? Hash and yaml[dir]}
	
	return enabled
end

.fetch_family(dir, fam) ⇒ Object



44
45
46
47
# File 'app/controllers/cytoplasm/fonts_controller.rb', line 44

def fetch_family(dir,fam)
	return fetch_fs_family(fam) if dir=="fontsquirrel"
	return fetch_gwf_family(fam) if dir=="googlewebfonts"
end

.fetch_fontsObject



29
30
31
32
# File 'app/controllers/cytoplasm/fonts_controller.rb', line 29

def fetch_fonts
	@fonts = {"fontsquirrel" => fetch_fs_all(), "googlewebfonts" => fetch_gwf_all(),"imported" => fetch_imported(),"enabled" => fetch_enabled()}
	return @fonts
end

.fetch_fs_allObject



33
34
35
36
37
# File 'app/controllers/cytoplasm/fonts_controller.rb', line 33

def fetch_fs_all
	fs = {}
	fetch_json("http://www.fontsquirrel.com/api/fontlist/all").each {|f| fs[f["family_urlname"]] = f}
	return fs
end

.fetch_fs_family(family) ⇒ Object



48
49
50
51
# File 'app/controllers/cytoplasm/fonts_controller.rb', line 48

def fetch_fs_family(family)
	fs = (@@cache==false or @@cache["fontsquirrel"]==false) ? fetch_fs_all() : @@cache["fontsquirrel"]
	return fs[family] unless fs[family].nil?
end

.fetch_fs_familyinfo(family) ⇒ Object



52
53
54
# File 'app/controllers/cytoplasm/fonts_controller.rb', line 52

def fetch_fs_familyinfo(family)
	fetch_json("http://www.fontsquirrel.com/api/familyinfo/"+family)
end

.fetch_gwf_allObject



38
39
40
41
42
43
# File 'app/controllers/cytoplasm/fonts_controller.rb', line 38

def fetch_gwf_all
	gwf = {}
	puts "CONFIG: "+Cytoplasm.vars.to_s
	fetch_json("https://www.googleapis.com/webfonts/v1/webfonts?key="+Cytoplasm.vars("setup.fontloader.googlewebfonts_apikey"))["items"].each {|f| gwf[f["family"]] = f}
	return gwf
end

.fetch_gwf_family(family) ⇒ Object



55
56
57
58
# File 'app/controllers/cytoplasm/fonts_controller.rb', line 55

def fetch_gwf_family(family)
	gwf = (@@cache==false) ? fetch_gwf_all() : @@cache["googlewebfonts"]
	return gwf[family] unless gwf[family].nil?
end

.fetch_importedObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/cytoplasm/fonts_controller.rb', line 59

def fetch_imported
	imported = {"fontsquirrel" => {}, "googlewebfonts" => {}}

	# Create @@fontsdir if it doesn't exist	
	unless File.exists?(@@fontsdir)
		count = 0
		@@fontsdir.split("/").each do |dir|
			Dir.mkdir(dir) unless File.exists?(dir)
			Dir.chdir(dir)
			count += 1
		end
		while count > 0 do
			Dir.chdir("../")
			count -= 1
		end
	end
	
	# Create @@gwf_file and @@enabled_file if it doesn't exist
	File.open(@@gwf_file,'w') {|f| f.write("")} unless File.exists?(@@gwf_file)
	File.open(@@enabled_file,'w') {|f| f.write("")} unless File.exists?(@@enabled_file)
	
	# Fetch imported FontSquirrel fonts
	Dir.open(@@fontsdir).each do |f|
		if File.directory?(@@fontsdir+"/"+f) and f!="." and f!=".."
			imported["fontsquirrel"][f] = fetch_fs_family(f)
			imported["fontsquirrel"][f]["familyinfo"] = fetch_fs_familyinfo(f)
		end
	end
	
	# Fetch imported GoogleWebFonts fonts
	yaml = fetch_yaml(@@gwf_file)
	yaml["imported"].each {|f| imported["googlewebfonts"][f] = fetch_gwf_family(f)} if yaml.is_a? Hash and yaml["imported"]
	
	return imported
end

.fontsObject



25
26
27
28
# File 'app/controllers/cytoplasm/fonts_controller.rb', line 25

def fonts
	@@cache = fetch_fonts() if @@cache == false
	return @fonts
end

.import(dir, fam) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/controllers/cytoplasm/fonts_controller.rb', line 126

def import(dir,fam)
	output = {}
	@@cache = fetch_fonts() if @@cache == false
	if dir == "fontsquirrel"
		output["dir"] = @@fontsdir+"/"+fam
		output["url"] = "http://www.fontsquirrel.com/fontfacekit/"+fam
		open(output["url"]) do |f|
			Zip::ZipFile.open(f) do |zipfile|
				zipfile.each do |entry|
					fpath = File.join(output["dir"],entry.to_s)
					FileUtils.mkdir_p(File.dirname(fpath))
					zipfile.extract(entry,fpath){true}
				end
			end
		end
		fix_fs_stylesheet_family_names(fam)
	end
	@@cache["imported"][dir][fam] = fetch_family(dir,fam)
	write_gwf_file() if dir=="googlewebfonts"
	@@cache = fetch_fonts()
	return output;
end

.is_enabled?(dir, fam) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
124
# File 'app/controllers/cytoplasm/fonts_controller.rb', line 121

def is_enabled?(dir,fam)
	enabled = (@@cache==false) ? fetch_enabled() : @@cache["enabled"]
	return !enabled[dir][fam].nil?
end

.remove(dir, fam) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/controllers/cytoplasm/fonts_controller.rb', line 148

def remove(dir,fam)
	output = {}
	@@cache = fetch_fonts() if @@cache == false
	disable(dir,fam) if is_enabled?(dir,fam)
	case dir
		when "fontsquirrel"
			output["dir"] = @@fontsdir+"/"+fam
			FileUtils.rm_rf(output["dir"]) if File.exists?(output["dir"])
		when "googlewebfonts"
			@@cache["imported"]["googlewebfonts"].delete(fam)
			write_gwf_file()
	end
	@@cache = fetch_fonts()
	return output
end

Instance Method Details

#disableObject



353
354
355
# File 'app/controllers/cytoplasm/fonts_controller.rb', line 353

def disable
	render :text => Cytoplasm::Ajax.success(Cytoplasm::FontsController.disable(params[:directory],params[:family]))
end

#enableObject



350
351
352
# File 'app/controllers/cytoplasm/fonts_controller.rb', line 350

def enable
	render :text => Cytoplasm::Ajax.success(Cytoplasm::FontsController.enable(params[:directory],params[:family]))
end

#fetch_allObject



322
323
324
# File 'app/controllers/cytoplasm/fonts_controller.rb', line 322

def fetch_all
	render :text => Cytoplasm::Ajax.success(@@cache)
end

#importObject



344
345
346
# File 'app/controllers/cytoplasm/fonts_controller.rb', line 344

def import
	render :text => Cytoplasm::Ajax.success(Cytoplasm::FontsController.import(params[:directory],params[:family]))
end

#indexObject



326
327
328
329
# File 'app/controllers/cytoplasm/fonts_controller.rb', line 326

def index
	@installed = Cytoplasm::FontsController.fetch_imported()
	@enabled = Cytoplasm::FontsController.fetch_enabled()
end

#installObject



335
336
337
# File 'app/controllers/cytoplasm/fonts_controller.rb', line 335

def install
	
end

#manageObject



339
340
341
342
# File 'app/controllers/cytoplasm/fonts_controller.rb', line 339

def manage
	@installed = Cytoplasm::FontsController.fetch_imported()
	@enabled = Cytoplasm::FontsController.fetch_enabled()
end

#removeObject



347
348
349
# File 'app/controllers/cytoplasm/fonts_controller.rb', line 347

def remove
	render :text => Cytoplasm::Ajax.success(Cytoplasm::FontsController.remove(params[:directory],params[:family]))
end

#showObject



331
332
333
# File 'app/controllers/cytoplasm/fonts_controller.rb', line 331

def show
	@font = Cytoplasm::FontsController.fetch_family(params[:directory],params[:family])
end