Class: Fontisan::Tables::Cbdt

Inherits:
Binary::BaseRecord show all
Defined in:
lib/fontisan/tables/cbdt.rb

Overview

CBDT (Color Bitmap Data) table.

CBDT stores the raw bitmap data blocks for color glyphs. CBLC indexes into CBDT via imageDataOffset and per-glyph offset arrays. CBDT itself is essentially a blob: the 4-byte header (majorVersion + minorVersion) followed by an opaque sequence of bitmap blocks.

This model exposes the header fields via BinData and preserves the original bytes for subsetting and direct slicing. A Cbdt constructed via Cbdt.new (no read) has no captured bytes — raw_data is nil and data_size returns 0.

Reference: OpenType CBDT specification. https://learn.microsoft.com/en-us/typography/opentype/spec/cbdt

Examples:

Reading CBDT and slicing a bitmap block

cbdt = Fontisan::Tables::Cbdt.read(font.table_data["CBDT"])
bytes = cbdt.bitmap_data_at(120, 4096)

Constant Summary collapse

TAG =
"CBDT"
VERSION_2_0 =

CBDT v2.0 — original release

0x00020000
VERSION_3_0 =

CBDT v3.0 — adds PNG image format support

0x00030000

Instance Method Summary collapse

Methods inherited from Binary::BaseRecord

read

Instance Method Details

#bitmap_data_at(offset, length) ⇒ String?

Slice length bytes starting at offset from the CBDT blob. Returns nil for out-of-range offsets/lengths so callers can use a nil check instead of rescuing IndexError.

Parameters:

  • offset (Integer, nil)

    start byte

  • length (Integer, nil)

    number of bytes

Returns:

  • (String, nil)


81
82
83
84
85
86
87
# File 'lib/fontisan/tables/cbdt.rb', line 81

def bitmap_data_at(offset, length)
  return nil if offset.nil? || length.nil?
  return nil if offset.negative? || length.negative?
  return nil if offset + length > data_size

  raw_data[offset, length]
end

#data_sizeInteger

Total byte size of the captured CBDT bytes, or 0 if none.

Returns:

  • (Integer)


60
61
62
# File 'lib/fontisan/tables/cbdt.rb', line 60

def data_size
  raw_data&.bytesize || 0
end

#has_raw_data?Boolean

Whether this instance was populated from real table bytes.

Returns:

  • (Boolean)


37
38
39
# File 'lib/fontisan/tables/cbdt.rb', line 37

def has_raw_data?
  defined?(@raw_data) && !@raw_data.nil? ? true : false
end

#raw_dataString?

Raw bytes captured at read time, or nil for a fresh instance.

Returns:

  • (String, nil)


44
45
46
47
48
# File 'lib/fontisan/tables/cbdt.rb', line 44

def raw_data
  return @raw_data if defined?(@raw_data)

  nil
end

#to_binary_sString

Serialize the captured bytes back to binary. Falls back to BinData's default serialization (4-byte header) if no bytes were captured, so consumers can construct a CBDT from scratch.

Returns:

  • (String)


94
95
96
97
98
# File 'lib/fontisan/tables/cbdt.rb', line 94

def to_binary_s
  return raw_data if has_raw_data?

  super
end

#valid?Boolean

Whether the header declares a CBDT version fontisan understands (v2.0 or v3.0) and any raw bytes were captured.

Returns:

  • (Boolean)


104
105
106
107
108
109
110
# File 'lib/fontisan/tables/cbdt.rb', line 104

def valid?
  return false unless [2, 3].include?(major_version)
  return false unless minor_version.zero?
  return false unless has_raw_data?

  true
end

#valid_offset?(offset) ⇒ Boolean

Whether offset falls within the captured CBDT byte range.

Parameters:

  • offset (Integer, nil)

Returns:

  • (Boolean)


68
69
70
71
72
# File 'lib/fontisan/tables/cbdt.rb', line 68

def valid_offset?(offset)
  return false if offset.nil? || offset.negative?

  offset < data_size
end

#versionInteger

Combined version number (e.g. 0x00030000 for v3.0).

Returns:

  • (Integer)


53
54
55
# File 'lib/fontisan/tables/cbdt.rb', line 53

def version
  (major_version << 16) | minor_version
end