Class: WOFF::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/woff/builder.rb

Overview

Used in generation of WOFF files with modified metadata for licensee and license id information.

woff = WOFF::Builder.new("/Users/Josh/Desktop/sample.woff")
woff.font_with_licensee_and_id("The Friends", "L012356093901")

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Builder

Returns a new instance of Builder.


9
10
11
# File 'lib/woff/builder.rb', line 9

def initialize(file)
  @location = file
end

Instance Method Details

#font_with_licensee_and_id(name, id) ⇒ Object


13
14
15
# File 'lib/woff/builder.rb', line 13

def font_with_licensee_and_id(name, id)
  (licensee: name, license_id: id)
end

#font_with_metadata(licensee: nil, license_id: nil, license_text: nil, description: nil) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
75
# File 'lib/woff/builder.rb', line 17

def (licensee: nil, license_id: nil, license_text: nil, description: nil)
   = data..length > 0 ? compressor.inflate(data.) : 
   = REXML::Document.new()

  if licensee
    if .root.elements["licensee"]
      .root.elements["licensee"].attributes["name"] = licensee
    else
      .root.add_element "licensee", { "name" => licensee }
    end
  end

  if license_id
    if .root.elements["license"]
      .root.elements["license"].attributes["id"] = license_id
    else
      .root.add_element "license", { "id" => license_id }
    end
  end

  if license_text
    license_el = .root.elements["license"]
    unless license_el
      license_el = .root.add_element "license"
    end

    license_text_el = license_el.elements["text"]
    unless license_text_el
      license_text_el = license_el.add_element("text", { "lang" => "en "})
    end

    license_text_el.text = license_text
  end

  if description
    description_el = .root.elements["description"]
    unless description_el
      description_el = .root.add_element "description"
    end

    description_text_el = description_el.elements["text"]
    unless description_text_el
      description_text_el = description_el.add_element("text", { "lang" => "en "})
    end

    description_text_el.text = description
  end

   = compressor.deflate(.to_s)

  data.meta_orig_length = .to_s.bytesize
  data. = 
  data.meta_length = .bytesize
  data.meta_offset = data..abs_offset # "Offset to metadata block, from beginning of WOFF file."

  data.data_length = data.num_bytes

  data.to_binary_s
end