Class: SFT

Inherits:
Object
  • Object
show all
Defined in:
lib/skrift/sft.rb

Constant Summary collapse

DOWNWARD_Y =
0x01

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(font) ⇒ SFT

Returns a new instance of SFT.



6
7
8
9
10
11
12
13
# File 'lib/skrift/sft.rb', line 6

def initialize(font)
  @font = font
  @x_scale  = 32
  @y_scale  = 32
  @x_offset = 0
  @y_offset = 0
  @flags = SFT::DOWNWARD_Y
end

Instance Attribute Details

#flagsObject

Returns the value of attribute flags.



4
5
6
# File 'lib/skrift/sft.rb', line 4

def flags
  @flags
end

#fontObject

Returns the value of attribute font.



4
5
6
# File 'lib/skrift/sft.rb', line 4

def font
  @font
end

#x_offsetObject

Returns the value of attribute x_offset.



4
5
6
# File 'lib/skrift/sft.rb', line 4

def x_offset
  @x_offset
end

#x_scaleObject

Returns the value of attribute x_scale.



4
5
6
# File 'lib/skrift/sft.rb', line 4

def x_scale
  @x_scale
end

#y_offsetObject

Returns the value of attribute y_offset.



4
5
6
# File 'lib/skrift/sft.rb', line 4

def y_offset
  @y_offset
end

#y_scaleObject

Returns the value of attribute y_scale.



4
5
6
# File 'lib/skrift/sft.rb', line 4

def y_scale
  @y_scale
end

Instance Method Details

#glyph_bbox(outline) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/skrift/sft.rb', line 19

def glyph_bbox(outline)
  box = @font.glyph_bbox(outline)
  raise if !box
  # Transform the bounding box into SFT coordinate space
  xs = @x_scale.to_f / @font.units_per_em
  ys = @y_scale.to_f / @font.units_per_em
  box[0] = (box[0] * xs + @x_offset).floor
  box[1] = (box[1] * ys + @y_offset).floor
  box[2] = (box[2] * xs + @x_offset).ceil
  box[3] = (box[3] * ys + @y_offset).ceil
  return box
end

#gmetrics(glyph) ⇒ Object

149



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/skrift/sft.rb', line 32

def gmetrics(glyph) # 149
  return nil if glyph.nil?
  raise "out of bounds" if glyph < 0
  xs = @x_scale.to_f / @font.units_per_em
  adv, lsb = @font.hor_metrics(glyph)

  return nil if adv.nil?
  metrics = GMetrics.new(adv * xs, lsb * xs + @x_offset)

  outline = @font.outline_offset(glyph)
  return metrics if outline.nil?
  bbox = glyph_bbox(outline)
  return nil if !bbox
  metrics.min_width = bbox[2] - bbox[0] + 1
  metrics.min_height = bbox[3] - bbox[1] + 1
  metrics.y_offset = @flags & SFT::DOWNWARD_Y != 0 ? bbox[3] : bbox[1]
  return metrics
end

#kerning(left_glyph, right_glyph) ⇒ Object

176



81
82
83
# File 'lib/skrift/sft.rb', line 81

def kerning(left_glyph, right_glyph) # 176
  @font.kerning[[left_glyph,right_glyph].pack("n*")]
end

#lmetricsObject



51
52
53
54
55
56
57
58
59
# File 'lib/skrift/sft.rb', line 51

def lmetrics
  hhea = font.reqtable("hhea")
  factor = @y_scale.to_f / @font.units_per_em
  LMetrics.new(
    font.geti16(hhea + 4) * factor, # ascender
    font.geti16(hhea + 6) * factor, # descender
    font.geti16(hhea + 8) * factor  # line_gap
  ) 
end

#lookup(codepoint) ⇒ Object



15
16
17
# File 'lib/skrift/sft.rb', line 15

def lookup(codepoint)
  return font.glyph_id(codepoint)
end

#render(glyph, image) ⇒ Object

239



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/skrift/sft.rb', line 61

def render(glyph, image) # 239
  outline = @font.outline_offset(glyph)
  return false if outline.nil?
  return true if outline.nil?
  bbox = glyph_bbox(outline)
  return false if !bbox
  # Set up the transformation matrix such that
  # the transformed bounding boxes min corner lines
  # up with the (0, 0) point.
  xr = [@x_scale.to_f / @font.units_per_em, 0.0, @x_offset - bbox[0]]
  ys = @y_scale.to_f / @font.units_per_em
  if @flags.allbits?(SFT::DOWNWARD_Y)
    transform = [xr, [0.0, -ys, bbox[3] - @y_offset]]
  else
    transform = [xr, [0.0, +ys, @y_offset - bbox[1] ]]
  end
  outl = @font.decode_outline(outline)
  outl.render(transform, image) if outl
end