Class: HarfBuzz::Blob
- Inherits:
-
Object
- Object
- HarfBuzz::Blob
- Defined in:
- lib/harfbuzz/blob.rb
Overview
Wraps hb_blob_t — binary data container
Instance Attribute Summary collapse
-
#ptr ⇒ Object
readonly
Returns the value of attribute ptr.
Class Method Summary collapse
- .define_finalizer(obj, ptr) ⇒ Object
-
.empty ⇒ Blob
Returns the empty (singleton) blob.
-
.from_file(path) ⇒ Blob
Creates a Blob from a file path.
-
.from_file!(path) ⇒ Blob
Creates a Blob from a file path.
-
.wrap_borrowed(ptr) ⇒ Object
Wraps a borrowed pointer (no finalizer — caller owns the lifetime).
-
.wrap_owned(ptr) ⇒ Object
Wraps an owned pointer (will be destroyed via finalizer).
Instance Method Summary collapse
-
#data ⇒ String
Blob data as a Ruby String (read-only).
-
#data_writable ⇒ String?
Blob data as a writable Ruby String, or nil if blob is immutable.
-
#immutable? ⇒ Boolean
True if the blob is immutable.
-
#initialize(data, mode: :duplicate) ⇒ Blob
constructor
Creates a Blob from binary data.
- #inspect ⇒ Object
-
#length ⇒ Integer
(also: #size)
Blob length in bytes.
-
#make_immutable! ⇒ self
Makes the blob immutable.
-
#sub_blob(offset, length) ⇒ Blob
Creates a sub-blob of this blob.
-
#writable_copy ⇒ Blob?
Returns a writable copy of this blob, or nil if it fails.
Constructor Details
#initialize(data, mode: :duplicate) ⇒ Blob
Creates a Blob from binary data
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
#ptr ⇒ Object (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 |
.empty ⇒ Blob
Returns the empty (singleton) blob
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.
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.
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
#data ⇒ String
Returns 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_writable ⇒ String?
Returns 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.
92 93 94 |
# File 'lib/harfbuzz/blob.rb', line 92 def immutable? C.from_hb_bool(C.hb_blob_is_immutable(@ptr)) end |
#inspect ⇒ Object
103 104 105 |
# File 'lib/harfbuzz/blob.rb', line 103 def inspect "#<HarfBuzz::Blob length=#{length} immutable=#{immutable?}>" end |
#length ⇒ Integer Also known as: size
Returns 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
98 99 100 101 |
# File 'lib/harfbuzz/blob.rb', line 98 def make_immutable! C.hb_blob_make_immutable(@ptr) self end |