Class: Fontisan::Tables::Svg
- Inherits:
-
Binary::BaseRecord
- Object
- BinData::Record
- Binary::BaseRecord
- Fontisan::Tables::Svg
- Defined in:
- lib/fontisan/tables/svg.rb
Overview
SVG (Scalable Vector Graphics) table parser
The SVG table contains embedded SVG documents for glyphs, typically used for color emoji or graphic elements. Each document can cover a range of glyph IDs and may be compressed with gzip.
SVG Table Structure:
SVG Table = Header (10 bytes)
+ Document Index
+ SVG Documents
Header (10 bytes):
- version (uint16): Table version (0)
- svgDocumentListOffset (uint32): Offset to SVG Document Index
- reserved (uint32): Reserved, set to 0
Document Index:
- numEntries (uint16): Number of SVG Document Index Entries
- entries: Array of SVG Document Index Entries
SVG Document Index Entry (12 bytes):
- startGlyphID (uint16): First glyph ID
- endGlyphID (uint16): Last glyph ID (inclusive)
- svgDocOffset (uint32): Offset to SVG document
- svgDocLength (uint32): Length of SVG document
SVG documents may be compressed with gzip (identified by magic bytes 0x1f 0x8b).
Reference: OpenType SVG specification https://docs.microsoft.com/en-us/typography/opentype/spec/svg
Defined Under Namespace
Classes: SvgDocumentRecord
Constant Summary collapse
- TAG =
OpenType table tag for SVG (note: includes trailing space)
"SVG "
Instance Attribute Summary collapse
-
#document_records ⇒ Array<SvgDocumentRecord>
readonly
Parsed document records.
-
#num_entries ⇒ Integer
readonly
Number of SVG document entries.
-
#raw_data ⇒ String
readonly
Raw binary data for the entire SVG table.
-
#svg_document_list_offset ⇒ Integer
readonly
Offset to SVG Document Index.
-
#version ⇒ Integer
readonly
SVG table version (0).
Class Method Summary collapse
-
.read(io) ⇒ Svg
Override read to parse SVG structure.
Instance Method Summary collapse
-
#glyph_ids_with_svg ⇒ Array<Integer>
Get all glyph IDs that have SVG data.
-
#has_svg_for_glyph?(glyph_id) ⇒ Boolean
Check if glyph has SVG data.
-
#num_svg_documents ⇒ Integer
Get the number of SVG documents in this table.
-
#parse!(data) ⇒ Object
Parse the SVG table structure.
-
#svg_for_glyph(glyph_id) ⇒ String?
Get SVG document for a specific glyph ID.
-
#valid? ⇒ Boolean
Validate the SVG table structure.
Instance Attribute Details
#document_records ⇒ Array<SvgDocumentRecord> (readonly)
Returns Parsed document records.
87 88 89 |
# File 'lib/fontisan/tables/svg.rb', line 87 def document_records @document_records end |
#num_entries ⇒ Integer (readonly)
Returns Number of SVG document entries.
84 85 86 |
# File 'lib/fontisan/tables/svg.rb', line 84 def num_entries @num_entries end |
#raw_data ⇒ String (readonly)
Returns Raw binary data for the entire SVG table.
90 91 92 |
# File 'lib/fontisan/tables/svg.rb', line 90 def raw_data @raw_data end |
#svg_document_list_offset ⇒ Integer (readonly)
Returns Offset to SVG Document Index.
81 82 83 |
# File 'lib/fontisan/tables/svg.rb', line 81 def svg_document_list_offset @svg_document_list_offset end |
#version ⇒ Integer (readonly)
Returns SVG table version (0).
78 79 80 |
# File 'lib/fontisan/tables/svg.rb', line 78 def version @version end |
Class Method Details
.read(io) ⇒ Svg
Override read to parse SVG structure
96 97 98 99 100 101 102 103 |
# File 'lib/fontisan/tables/svg.rb', line 96 def self.read(io) svg = new return svg if io.nil? data = io.is_a?(String) ? io : io.read svg.parse!(data) svg end |
Instance Method Details
#glyph_ids_with_svg ⇒ Array<Integer>
Get all glyph IDs that have SVG data
149 150 151 152 153 |
# File 'lib/fontisan/tables/svg.rb', line 149 def glyph_ids_with_svg document_records.flat_map do |record| record.glyph_range.to_a end end |
#has_svg_for_glyph?(glyph_id) ⇒ Boolean
Check if glyph has SVG data
142 143 144 |
# File 'lib/fontisan/tables/svg.rb', line 142 def has_svg_for_glyph?(glyph_id) !find_document_record(glyph_id).nil? end |
#num_svg_documents ⇒ Integer
Get the number of SVG documents in this table
158 159 160 |
# File 'lib/fontisan/tables/svg.rb', line 158 def num_svg_documents num_entries end |
#parse!(data) ⇒ Object
Parse the SVG table structure
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/fontisan/tables/svg.rb', line 109 def parse!(data) @raw_data = data io = StringIO.new(data) # Parse SVG header (10 bytes) parse_header(io) validate_header! # Parse document index parse_document_index(io) rescue StandardError => e raise CorruptedTableError, "Failed to parse SVG table: #{e.}" end |
#svg_for_glyph(glyph_id) ⇒ String?
Get SVG document for a specific glyph ID
Returns the SVG XML content for the specified glyph. Automatically decompresses gzipped content. Returns nil if glyph has no SVG data.
131 132 133 134 135 136 |
# File 'lib/fontisan/tables/svg.rb', line 131 def svg_for_glyph(glyph_id) record = find_document_record(glyph_id) return nil unless record extract_svg_document(record) end |
#valid? ⇒ Boolean
Validate the SVG table structure
165 166 167 168 169 170 171 172 |
# File 'lib/fontisan/tables/svg.rb', line 165 def valid? return false if version.nil? return false if version != 0 # Only version 0 supported return false if num_entries.nil? || num_entries.negative? return false unless document_records true end |