Class: Marmot::Client

Inherits:
Object
  • Object
show all
Includes:
HTTMultiParty
Defined in:
lib/marmot/client.rb

Constant Summary collapse

@@headers_set =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



14
15
16
17
# File 'lib/marmot/client.rb', line 14

def initialize
  logger = Logger.new("/dev/null")
  logger.close
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



10
11
12
# File 'lib/marmot/client.rb', line 10

def logger
  @logger
end

#paramsObject (readonly)

Returns the value of attribute params.



11
12
13
# File 'lib/marmot/client.rb', line 11

def params
  @params
end

Instance Method Details

#convert(input_io, options = {}) ⇒ String

Convert a font file to a webfont kit

Parameters:

  • input_io (String)

    Input IO. Examples: File.new(“font.ttf”), STDOUT

  • options (Hash) (defaults to: {})

    Options

Options Hash (options):

  • :output_io (String)

    Output IO Default is a File with the name like “webfontkit-20130312-200144.zip”

  • :custom_options (Hash)

    Options that will bypass sanitization. Make sure you know what you do before trying it out.

  • :formats (Array or String)

    Allowed values are: “ttf”, “woff”, “svg”, “eotz”, “eot”. Default is [“ttf”,“woff”,“svg”,“eotz”]

  • :mode (String)

    Generator mode: “optimal”, “basic”, “expert”. Default is “optimal”

  • :tt_instructor (String)

    Truetype hinting: “default”, “keep”

  • :fix_vertical_metrics (Boolean)

    Rendering option. Fix Vertical Metrics (Normalize across browsers). Default is true

  • :fix_gasp (Boolean)

    Rendering option. Fix GASP Table (Better DirectWrite Rendering). Default is true

  • :remove_kerning (Boolean)

    Rendering option. Remove Kerning (Strip kerning data). Default is false

  • :add_spaces (Boolean)

    Fix missing glyphs option. Fix spaces. Default is true

  • :add_hyphens (Boolean)

    Fix missing glyphs option. Fix hyphens. Default is true

  • :fallback (String)

    X-height matching: “none”, “arial”, “verdana”, “trebuchet”, “georgia”, “times”, “courier”, “custom”

  • :fallback_custom (String)

    Custom x-height matching, in percents. Default is “100%”. Only applies when :fallback is “custom”

  • :webonly (Boolean)

    Disable desktop use. Default is false

  • :options_subset (String)

    Subsetting options: “basic”, “advanced”, “none”. Default is “basic”

  • :subset_range (Array)

    Custom subsetting options. Only applies when :options_subset is “advanced”. Allowed values are: “macroman”, “lowercase”, “uppercase”, “numbers”, “punctuation”, “currency”, “typographics”, “math”, “altpunctuation”, “accentedlower”, “accentedupper”, “diacriticals”, “albanian”, “bosnian”, “catalan”, “croatian”, “cyrillic”, “czech”, “danish”, “dutch”, “english”, “esperanto”, “estonian”, “faroese”, “french”, “german”, “greek”, “hebrew”, “hungarian”, “icelandic”, “italian”, “latvian”, “lithuanian”, “malagasy”, “maltese”, “norwegian”, “polish”, “portuguese”, “romanian”, “serbian”, “slovak”, “slovenian”, “spanish”, “swedish”, “turkish”, “ubasic”, “ulatin1”, “ucurrency”, “upunctuation”, “ulatina”, “ulatinb”, “ulatinaddl”

  • :subset_custom (String)

    Single characters. Only applies when :options_subset is “advanced”. Default is “”

  • :subset_custom_range (String)

    Unicode Ranges. Only applies when :options_subset is “advanced”. Comma separated values. Can be single hex values and/or ranges separated with hyphens. Example: “0021-007E,00E7,00EB,00C7,00CB”

  • :base64 (Boolean)

    CSS option. Base64 encode (Embed font in CSS). Default is false

  • :style_link (Boolean)

    CSS option. Style link (Family support in CSS). Default is false

  • :css_stylesheet (String)

    CSS Filename. Default is “stylesheet.css”

  • :ot_features (String)

    OpenType Options. If the features are available, the generator will fold them into the font. Allowed values: “onum”, “lnum”, “tnum”, “pnum”, “zero”, “ss01”, “ss02”, “ss03”, “ss04”, “ss05”

  • :filename_suffix (String)

    Filename suffix. Default is “-webfont”

  • :emsquare (String)

    Em Square Value. In units of the em square. Default is 2048

  • :spacing_adjustment (String)

    Adjust Glyph Spacing. In units of the em square. Default is 0

  • :agreement (Boolean)

    Agreement option. The fonts You’re uploading are legally eligible for web embedding. Default is true.

Returns:

  • (String)

See Also:



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/marmot/client.rb', line 137

def convert input_io, options={}
  @exception = nil
  #1
  iam "Retrieving cookies... ", false do
    response = self.class.get '/tools/webfont-generator'
    @cookies = (response.headers.get_fields("Set-Cookie") || []).join(";")
    fail "Failed to retrieve cookies" if @cookies.empty? 
    self.class.headers({"Cookie" => @cookies})
    @@headers_set = true
    response
  end unless @@headers_set

  #2
  iam "Uploading font... " do
    response = self.class.post '/uploadify/fontfacegen_uploadify.php', :query => {
      "Filedata"    => File.new(input_io)
    }
    @path_data = response.body
    @id, @original_filename = @path_data.split("|")
    fail "Failed to upload the file. Is it a valid font?" if @id.nil? || @original_filename.nil?
    response
  end

  #3
  iam "Confirming upload... " do
    response = self.class.post "/tools/insert", :query => {
      "original_filename"  => @original_filename,
      "path_data"          => @path_data
    }
    json = JSON.parse response.body
    fail (json["message"] || "Failed to confirm the upload. Is it a valid font?") if !json["name"] || !json["id"]
    response
  end

  #4
  iam "Generating webfont... " do
    custom_options = options.delete :custom_options
    options[:id] = @id
    @params = OptionsSanitizer.sanitize(options, custom_options)
    logger.debug "HTTP Params:\n#{@params.collect{|k,v| "#{k}: #{v.inspect}" }.join("\n")}"
    response = self.class.post "/tools/generate", :query => @params
    fail "Failed to generate webfont kit" if !response.body.empty?
    response
  end

  #5
  counter = 0
  while response = self.class.get("/tools/progress/#{@id}") do 
    p = JSON.parse(response.body)["progress"].to_i
    logger.info "Progress: #{p} "
    if p == 100
      break
    elsif p == 0
      fail "Progress timeout" if (counter += 1) > 10
    end
    sleep 2        
  end

  #6
  iam "Downloading fonts... ", false do
    response = self.class.post "/tools/download", :query => @params
  end

  #7
  if !options[:output_io]
    filename = response.headers["Content-Disposition"].gsub(/attachment; filename=\"(.*)\"/,'\1')
    options[:output_io] = File.new(filename, "wb")
  end
  options[:output_io] << response.response.body

end