4 # Handles finding and reading font files.
7 # No commandline usage.
15 #Handles finding and reading font files.
18 #Recursively searches through a directory for fonts.
20 def Font.find_in_dir(directory, extensions)
22 if File.directory?(directory) then
23 Dir.glob("#{directory}/*") do |file|
25 if File.directory? file
26 paths.push(*Font.find_in_dir(file, extensions))
27 elsif font?(file, extensions)
35 #Recursively searches through an array of directories for fonts.
37 def Font.find(directories, extensions)
39 directories.each do |directory|
40 paths.push(Font.find_in_dir(directory, extensions))
45 #Get metadata from a TTF file.
59 #Stop using this library, its slow and bulky for what needs to be done.
60 font = FontTTF::TTF::File.new(path)
61 meta = font.get_table(:name).name_records.values_at(0,1,2,4,8,10,14,19)
62 meta.collect!{|meta_item|
65 #Escape nasty characters, get rid of quotation marks around data,
66 #replace ' with '', remove trailing and leading whitespace,
67 #get rid of successive spaces
68 meta_item.to_s.dump.gsub("\\000","")[1..-2].gsub("'","''") \
69 .gsub(/^\s+/, "").gsub(/\s+$/, $/).gsub(/ +/," ")
76 #Returns true if a path has one of the given extensions.
78 def Font.font?(path, extensions)
79 extensions.each do |extension|
80 if path[-extension.length..-1] == extension then