Class: HarfBuzz::Blob

Inherits:
Object
  • Object
show all
Defined in:
lib/harfbuzz/blob.rb

Overview

Wraps hb_blob_t — binary data container

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, mode: :duplicate) ⇒ Blob

Creates a Blob from binary data

Parameters:

  • data (String)

    Binary data

  • mode (Symbol) (defaults to: :duplicate)

    Memory mode (:duplicate, :readonly, :writable, :readonly_may_make_writable)

Raises:



13
14
15
16
17
18
19
20
21
# File 'lib/harfbuzz/blob.rb', line 13

def initialize(data, mode: :duplicate)
  mem = FFI::MemoryPointer.from_string(data)
  @ptr = C.hb_blob_create(mem, data.bytesize, mode, nil, nil)
  raise AllocationError, "Failed to create blob" if @ptr.null?

  # Keep mem alive until the blob is destroyed
  @mem = mem
  register_finalizer
end

Instance Attribute Details

#ptrObject (readonly)

Returns the value of attribute ptr.



6
7
8
# File 'lib/harfbuzz/blob.rb', line 6

def ptr
  @ptr
end

Class Method Details

.define_finalizer(obj, ptr) ⇒ Object



131
132
133
134
# File 'lib/harfbuzz/blob.rb', line 131

def self.define_finalizer(obj, ptr)
  destroy = C.method(:hb_blob_destroy)
  ObjectSpace.define_finalizer(obj, proc { destroy.call(ptr) })
end

.emptyBlob

Returns the empty (singleton) blob

Returns:



44
45
46
# File 'lib/harfbuzz/blob.rb', line 44

def self.empty
  wrap_borrowed(C.hb_blob_get_empty)
end

.from_file(path) ⇒ Blob

Creates a Blob from a file path. Returns an empty blob if file is missing.

Parameters:

  • path (String)

    File path

Returns:



26
27
28
29
# File 'lib/harfbuzz/blob.rb', line 26

def self.from_file(path)
  ptr = C.hb_blob_create_from_file(path)
  wrap_owned(ptr)
end

.from_file!(path) ⇒ Blob

Creates a Blob from a file path. Raises if the file cannot be read.

Parameters:

  • path (String)

    File path

Returns:

Raises:



35
36
37
38
39
40
# File 'lib/harfbuzz/blob.rb', line 35

def self.from_file!(path)
  ptr = C.hb_blob_create_from_file_or_fail(path)
  raise AllocationError, "Failed to create blob from file: #{path}" if ptr.null?

  wrap_owned(ptr)
end

.wrap_borrowed(ptr) ⇒ Object

Wraps a borrowed pointer (no finalizer — caller owns the lifetime)



116
117
118
119
120
121
# File 'lib/harfbuzz/blob.rb', line 116

def self.wrap_borrowed(ptr)
  obj = allocate
  obj.instance_variable_set(:@ptr, ptr)
  obj.instance_variable_set(:@borrowed, true)
  obj
end

.wrap_owned(ptr) ⇒ Object

Wraps an owned pointer (will be destroyed via finalizer)



108
109
110
111
112
113
# File 'lib/harfbuzz/blob.rb', line 108

def self.wrap_owned(ptr)
  obj = allocate
  obj.instance_variable_set(:@ptr, ptr)
  obj.send(:register_finalizer)
  obj
end

Instance Method Details

#dataString

Returns Blob data as a Ruby String (read-only).

Returns:

  • (String)

    Blob data as a Ruby String (read-only)



74
75
76
77
78
79
80
# File 'lib/harfbuzz/blob.rb', line 74

def data
  length_ptr = FFI::MemoryPointer.new(:uint)
  data_ptr = C.hb_blob_get_data(@ptr, length_ptr)
  return "".b if data_ptr.null?

  data_ptr.read_bytes(length_ptr.read_uint)
end

#data_writableString?

Returns Blob data as a writable Ruby String, or nil if blob is immutable.

Returns:

  • (String, nil)

    Blob data as a writable Ruby String, or nil if blob is immutable



83
84
85
86
87
88
89
# File 'lib/harfbuzz/blob.rb', line 83

def data_writable
  length_ptr = FFI::MemoryPointer.new(:uint)
  data_ptr = C.hb_blob_get_data_writable(@ptr, length_ptr)
  return nil if data_ptr.null?

  data_ptr.read_bytes(length_ptr.read_uint)
end

#immutable?Boolean

Returns true if the blob is immutable.

Returns:

  • (Boolean)

    true if the blob is immutable



92
93
94
# File 'lib/harfbuzz/blob.rb', line 92

def immutable?
  C.from_hb_bool(C.hb_blob_is_immutable(@ptr))
end

#inspectObject



103
104
105
# File 'lib/harfbuzz/blob.rb', line 103

def inspect
  "#<HarfBuzz::Blob length=#{length} immutable=#{immutable?}>"
end

#lengthInteger Also known as: size

Returns Blob length in bytes.

Returns:

  • (Integer)

    Blob length in bytes



67
68
69
# File 'lib/harfbuzz/blob.rb', line 67

def length
  C.hb_blob_get_length(@ptr)
end

#make_immutable!self

Makes the blob immutable

Returns:

  • (self)


98
99
100
101
# File 'lib/harfbuzz/blob.rb', line 98

def make_immutable!
  C.hb_blob_make_immutable(@ptr)
  self
end

#sub_blob(offset, length) ⇒ Blob

Creates a sub-blob of this blob

Parameters:

  • offset (Integer)

    Byte offset into the blob

  • length (Integer)

    Length in bytes

Returns:



52
53
54
55
# File 'lib/harfbuzz/blob.rb', line 52

def sub_blob(offset, length)
  ptr = C.hb_blob_create_sub_blob(@ptr, offset, length)
  self.class.wrap_owned(ptr)
end

#writable_copyBlob?

Returns a writable copy of this blob, or nil if it fails

Returns:



59
60
61
62
63
64
# File 'lib/harfbuzz/blob.rb', line 59

def writable_copy
  ptr = C.hb_blob_copy_writable_or_fail(@ptr)
  return nil if ptr.null?

  self.class.wrap_owned(ptr)
end