Class: FontProcessor::ProcessedFontIterator

Inherits:
Object
  • Object
show all
Defined in:
lib/fontprocessor/processed_font_iterator.rb

Instance Method Summary collapse

Constructor Details

#initialize(fontbase_id, charset_id, fpv, directory, convert) ⇒ ProcessedFontIterator

fontbase_id - The unique fontbase id of the desired font. charset_id - The integer representing the character set contained within

this font. 1 is all, 2 is default and 3 is upper-and-lower.

fpv - The integer representing the fontprocessing version. directory - Local directory of files to iterate over.



11
12
13
14
15
16
17
# File 'lib/fontprocessor/processed_font_iterator.rb', line 11

def initialize(fontbase_id, charset_id, fpv, directory, convert)
  @fontbase_id = fontbase_id
  @charset_id = charset_id
  @fpv = fpv
  @directory = directory
  @convert = convert
end

Instance Method Details

#eachObject

Public: Iterates over the directory, ensures all of the expected files are present and maps each file into a proper S3 key.

Examples

p = ProcessedFileIterator.new(...)
begin
  p.each do |file, s3_key|
    puts s3
  end
rescue MissingFilesException => e
  puts "The processed file directory is missing some expected files."
end

Returns nothing. Raises MissingFilesException if an expected files is missing



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fontprocessor/processed_font_iterator.rb', line 35

def each
  raise MissingFilesException unless valid?
  Dir.glob(File.join(@directory, "*")) do |file|
    next unless file =~ /charset-\d+-(.*?)\.(.*)/
    container = $1
    outline = $2

    s3_key = s3_key(container, outline)
    yield file, s3_key
  end
end

#s3_key(container, outline) ⇒ Object

Generates a fully defined S3 key which incorporates all variables used to process a font.

container - One of “dyna_base”, woff“, ”woff2“, ”otf“, ”inst“, ”svg“, ”woff_raw“ outline - One of ”ttf“, ”otf“

Returns a string containing an S3 object key of the object described by the given attributes.



84
85
86
87
# File 'lib/fontprocessor/processed_font_iterator.rb', line 84

def s3_key(container, outline)
  outline = outline == 'otf' ? 'cff' : outline
  "#{@fontbase_id}/#{@fpv}/#{@charset_id}/#{container}.#{outline}"
end

#valid?Boolean

Checks whether all expected files are present.

Returns true if all expected files are present and false otherwise.

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fontprocessor/processed_font_iterator.rb', line 50

def valid?
  files = Dir.glob(File.join(@directory, "*")).map { |f| File.basename(f) }
  only_truetype = files.select { |f| f.include?(".cff") || f.include?(".otf")  }.empty?

  expected_truetype_files = ["dyna_base.ttf", "otf.ttf", "inst.ttf", "woff.ttf", "woff2.ttf", "woff_raw.ttf"]
  expected_postscript_files = files.select { |f| f.include?("cff")  }.empty? ? ["dyna_base.otf", "otf.otf", "inst.otf", "woff.otf", "woff2.otf", "woff_raw.otf"] : ["dyna_base.cff", "otf.cff", "inst.cff", "woff.cff", "woff2.cff", "woff_raw.cff"]

  if only_truetype
    expected_files = expected_truetype_files
  else
    expected_files = expected_postscript_files
    if @convert
      expected_files += expected_truetype_files
    end
  end

  expected_files = expected_files.map { |f| "charset-#{@charset_id}-#{f}" }
  missing_files = expected_files.reject { |f| files.include?(f) }

  if missing_files.empty?
    true
  else
    false
  end
end