Class: HarfBuzz::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/hexapdf/layout/text_shaper.rb

Overview

:nodoc:

Constant Summary collapse

GLYPH_INFO_SIZE =
HarfBuzz::C::HbGlyphInfoT.size
GLYPH_INFO_CODEPOINT_OFFSET =
HarfBuzz::C::HbGlyphInfoT.offset_of(:codepoint)
GLYPH_INFO_CLUSTER_OFFSET =
HarfBuzz::C::HbGlyphInfoT.offset_of(:cluster)
GLYPH_POS_SIZE =
HarfBuzz::C::HbGlyphPositionT.size
GLYPH_POS_XADVANCE_OFFSET =
HarfBuzz::C::HbGlyphPositionT.offset_of(:x_advance)
GLYPH_POS_YADVANCE_OFFSET =
HarfBuzz::C::HbGlyphPositionT.offset_of(:y_advance)
GLYPH_POS_XOFFSET_OFFSET =
HarfBuzz::C::HbGlyphPositionT.offset_of(:x_offset)
GLYPH_POS_YOFFSET_OFFSET =
HarfBuzz::C::HbGlyphPositionT.offset_of(:y_offset)

Instance Method Summary collapse

Instance Method Details

#each_resultObject

Iterates efficiently over the shaping result without creating intermediary objects.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/hexapdf/layout/text_shaper.rb', line 59

def each_result
  return enum_for(__method__) unless block_given?

  length_ptr = FFI::MemoryPointer.new(:uint)
  infos_ptr = HarfBuzz::C.hb_buffer_get_glyph_infos(@ptr, length_ptr)
  length_ptr = FFI::MemoryPointer.new(:uint)
  positions_ptr = HarfBuzz::C.hb_buffer_get_glyph_positions(@ptr, length_ptr)
  length = length_ptr.read_uint

  return if infos_ptr.null? || positions_ptr.null? || length.zero?

  last_info_cluster_offset = (length - 1) * GLYPH_INFO_SIZE + GLYPH_INFO_CLUSTER_OFFSET
  i = 0
  while i < length
    info_offset = i * GLYPH_INFO_SIZE
    pos_offset  = i * GLYPH_POS_SIZE

    glyph_id = infos_ptr.get_uint32(info_offset + GLYPH_INFO_CODEPOINT_OFFSET)
    cluster  = infos_ptr.get_uint32(info_offset + GLYPH_INFO_CLUSTER_OFFSET)

    next_cluster = nil
    tmp_offset = info_offset + GLYPH_INFO_CLUSTER_OFFSET + GLYPH_INFO_SIZE
    while tmp_offset <= last_info_cluster_offset &&
          (next_cluster = infos_ptr.get_uint32(tmp_offset)) == cluster
      tmp_offset += GLYPH_INFO_SIZE
      next_cluster = nil
    end

    x_advance = positions_ptr.get_int32(pos_offset + GLYPH_POS_XADVANCE_OFFSET)
    y_advance = positions_ptr.get_int32(pos_offset + GLYPH_POS_YADVANCE_OFFSET)
    x_offset = positions_ptr.get_int32(pos_offset + GLYPH_POS_XOFFSET_OFFSET)
    y_offset = positions_ptr.get_int32(pos_offset + GLYPH_POS_YOFFSET_OFFSET)

    yield(glyph_id, cluster, next_cluster, x_advance, y_advance, x_offset, y_offset)

    i += 1
  end

  self
end