Class: Engine::FontImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/engine/importers/font_importer.rb

Constant Summary collapse

TEXTURE_SIZE =
1024
GLYPH_COUNT =
16
CELL_SIZE =
TEXTURE_SIZE / GLYPH_COUNT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, destination_image, destination_metrics) ⇒ FontImporter

Returns a new instance of FontImporter.



12
13
14
15
16
# File 'lib/engine/importers/font_importer.rb', line 12

def initialize(source, destination_image, destination_metrics)
  @source = source
  @destination_image = destination_image
  @destination_metrics = destination_metrics
end

Instance Attribute Details

#destination_imageObject (readonly)

Returns the value of attribute destination_image.



10
11
12
# File 'lib/engine/importers/font_importer.rb', line 10

def destination_image
  @destination_image
end

#destination_metricsObject (readonly)

Returns the value of attribute destination_metrics.



10
11
12
# File 'lib/engine/importers/font_importer.rb', line 10

def destination_metrics
  @destination_metrics
end

#sourceObject (readonly)

Returns the value of attribute source.



10
11
12
# File 'lib/engine/importers/font_importer.rb', line 10

def source
  @source
end

Instance Method Details

#importObject



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
# File 'lib/engine/importers/font_importer.rb', line 18

def import
  image = Magick::Image.new(TEXTURE_SIZE, TEXTURE_SIZE,) do |options|
    options.background_color = "transparent"
  end
  font_metrics = {}

  draw = Magick::Draw.new
  (0...GLYPH_COUNT).each do |x|
    (0...GLYPH_COUNT).each do |y|
      index = x * GLYPH_COUNT + y
      next if index >= 255
      character = character(index)
      write_character(character, draw, image, coord(x), coord(y))

      metric = draw.get_type_metrics(image, character)
      if character.to_s == " "
        font_metrics[index] = { width: metric.max_advance / 4.0 }
      else
        font_metrics[index] = { width: metric.width }
      end
    end
  end

  FileUtils.mkdir_p(File.dirname(destination_image)) unless File.directory?(File.dirname(destination_image))
  image.write(destination_image)

  File.write(destination_metrics, font_metrics.to_json)
end