Module: NSWTopo::Font::Chrome

Includes:
Log
Defined in:
lib/nswtopo/font.rb

Constant Summary collapse

ATTRIBUTES =
%w[font-family font-variant font-style font-weight font-size letter-spacing word-spacing]
SVG =
<<~XML
  <?xml version='1.0' encoding='UTF-8'?>
  <svg xmlns='http://www.w3.org/2000/svg' width='1mm' height='1mm' viewBox='0 0 1 1' text-rendering='geometricPrecision'>
    <rect width='1' height='1' stroke='none' />
    <text>placeholder</text>
  </svg>
XML

Constants included from Log

Log::FAILURE, Log::NEUTRAL, Log::SUCCESS, Log::UPDATE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

#log_abort, #log_neutral, #log_success, #log_update, #log_warn

Class Method Details

.extended(instance) ⇒ Object



22
23
24
# File 'lib/nswtopo/font.rb', line 22

def self.extended(instance)
  instance.start_chrome
end

Instance Method Details

#glyph_length(string, attributes) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/nswtopo/font.rb', line 37

def glyph_length(string, attributes)
  validate attributes
  style = attributes.slice(*ATTRIBUTES).map do |pair|
    pair.join ?:
  end.join(?;)
  @text[:style] = style
  @text.value = string
  @text.width / @scale
end

#start_chromeObject



14
15
16
17
18
19
20
# File 'lib/nswtopo/font.rb', line 14

def start_chrome
  @families = Set[]
  NSWTopo::Chrome.new("data:image/svg+xml;base64,#{Base64.encode64 SVG}").tap do |browser|
    @scale = browser.query_selector("rect").width
    @text = browser.query_selector "text"
  end
end

#validate(attributes) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/nswtopo/font.rb', line 26

def validate(attributes)
  return unless family = attributes["font-family"]
  return unless @families.add? family
  @text.value = "abcdefghijklmnopqrstuvwxyz"
  @text[:style] = "font-family:#{family}"
  styled_width = @text.width
  @text[:style] = nil
  unstyled_width = @text.width
  log_neutral "font '#{family}' doesn't appear to be available" if styled_width == unstyled_width
end