Add bin and edit workflow
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 9s

This commit is contained in:
2026-09-16 13:11:16 -06:00
parent c8ac4fcae5
commit 4cee170d66
17576 changed files with 895740 additions and 2 deletions
@@ -0,0 +1,73 @@
# frozen_string_literal: true
module TTFunk
class Table
class Cmap
# Format 0: Byte encoding table.
#
# This module conditionally extends {TTFunk::Table::Cmap::Subtable}.
module Format00
# Language.
# @return [Integer]
attr_reader :language
# Code map.
# @return [Array<Integer>]
attr_reader :code_map
# Encode the encoding record to format 0.
#
# @param charmap [Hash{Integer => Integer}] a hash mapping character
# codes to glyph ids (where the glyph ids are from the original font).
# @return [Hash]
# * `:charmap` (<tt>Hash{Integer => Hash}</tt>) keys are the characrers in
# `charset`, values are hashes:
# * `:old` (<tt>Integer</tt>) - glyph ID in the original font.
# * `:new` (<tt>Integer</tt>) - glyph ID in the subset font.
# that maps the characters in charmap to a
# * `:subtable` (<tt>String</tt>) - serialized encoding record.
# * `:max_glyph_id` (<tt>Integer</tt>) - maximum glyph ID in the new font.
def self.encode(charmap)
next_id = 0
glyph_indexes = Array.new(256, 0)
glyph_map = { 0 => 0 }
new_map =
charmap.keys.sort.each_with_object({}) do |code, map|
glyph_map[charmap[code]] ||= next_id += 1
map[code] = { old: charmap[code], new: glyph_map[charmap[code]] }
glyph_indexes[code] = glyph_map[charmap[code]]
map
end
# format, length, language, indices
subtable = [0, 262, 0, *glyph_indexes].pack('nnnC*')
{ charmap: new_map, subtable: subtable, max_glyph_id: next_id + 1 }
end
# Get glyph ID for character code.
#
# @param code [Integer] character code.
# @return [Integer] glyph ID.
def [](code)
@code_map[code] || 0
end
# Is this encoding record format supported?
#
# @return [true]
def supported?
true
end
private
def parse_cmap!
@language = read(4, 'x2n')
@code_map = read(256, 'C*')
end
end
end
end
end
@@ -0,0 +1,160 @@
# frozen_string_literal: true
module TTFunk
class Table
class Cmap
# Format 4: Segment mapping to delta values.
#
# This module conditionally extends {TTFunk::Table::Cmap::Subtable}.
module Format04
# Language.
# @return [Integer]
attr_reader :language
# Code map.
# @return [Hash{Integer => Integer}]
attr_reader :code_map
# Encode the encoding record to format 4.
#
# @param charmap [Hash{Integer => Integer}] a hash mapping character
# codes to glyph IDs from the original font.
# @return [Hash]
# * `:charmap` (<tt>Hash{Integer => Hash}</tt>) keys are the characrers in
# `charset`, values are hashes:
# * `:old` (<tt>Integer</tt>) - glyph ID in the original font.
# * `:new` (<tt>Integer</tt>) - glyph ID in the subset font.
# that maps the characters in charmap to a
# * `:subtable` (<tt>String</tt>) - serialized encoding record.
# * `:max_glyph_id` (<tt>Integer</tt>) - maximum glyph ID in the new font.
def self.encode(charmap)
end_codes = []
start_codes = []
next_id = 0
last = difference = nil
glyph_map = { 0 => 0 }
new_map =
charmap.keys.sort.each_with_object({}) do |code, map|
old = charmap[code]
glyph_map[old] ||= next_id += 1
map[code] = { old: old, new: glyph_map[old] }
delta = glyph_map[old] - code
if last.nil? || delta != difference
end_codes << last if last
start_codes << code
difference = delta
end
last = code
map
end
end_codes << last if last
end_codes << 0xFFFF
start_codes << 0xFFFF
segcount = start_codes.length
# build the conversion tables
deltas = []
range_offsets = []
glyph_indices = []
offset = 0
start_codes.zip(end_codes).each_with_index do |(a, b), segment|
if a == 0xFFFF
# We want the final 0xFFFF code to map to glyph 0.
# The glyph index is calculated as glyph = charcode + delta,
# which means that delta must be -0xFFFF to map character code
# 0xFFFF to glyph 0.
deltas << -0xFFFF
range_offsets << 0
break
end
start_glyph_id = new_map[a][:new]
if a - start_glyph_id >= 0x8000
deltas << 0
range_offsets << (2 * (glyph_indices.length + segcount - segment))
a.upto(b) { |code| glyph_indices << new_map[code][:new] }
else
deltas << (-a + start_glyph_id)
range_offsets << 0
end
offset += 2
end
# format, length, language
subtable = [
4, 16 + (8 * segcount) + (2 * glyph_indices.length), 0,
].pack('nnn')
search_range = 2 * (2**Integer(Math.log(segcount) / Math.log(2)))
entry_selector = Integer(Math.log(search_range / 2) / Math.log(2))
range_shift = (2 * segcount) - search_range
subtable << [
segcount * 2, search_range, entry_selector, range_shift,
].pack('nnnn')
subtable << end_codes.pack('n*') << "\0\0" << start_codes.pack('n*')
subtable << deltas.pack('n*') << range_offsets.pack('n*')
subtable << glyph_indices.pack('n*')
{ charmap: new_map, subtable: subtable, max_glyph_id: next_id + 1 }
end
# Get glyph ID for character code.
#
# @param code [Integer] character code.
# @return [Integer] glyph ID.
def [](code)
@code_map[code] || 0
end
# Is this encoding record format supported?
#
# @return [true]
def supported?
true
end
private
def parse_cmap!
length, @language, segcount_x2 = read(6, 'nnn')
segcount = segcount_x2 / 2
io.read(6) # skip searching hints
end_code = read(segcount_x2, 'n*')
io.read(2) # skip reserved value
start_code = read(segcount_x2, 'n*')
id_delta = read_signed(segcount)
id_range_offset = read(segcount_x2, 'n*')
glyph_ids = read(length - io.pos + @offset, 'n*')
@code_map = {}
end_code.each_with_index do |tail, i|
start_code[i].upto(tail) do |code|
if (id_range_offset[i]).zero?
glyph_id = code + id_delta[i]
else
index = (id_range_offset[i] / 2) + (code - start_code[i]) - (segcount - i)
# Because some TTF fonts are broken
glyph_id = glyph_ids[index] || 0
glyph_id += id_delta[i] if glyph_id != 0
end
@code_map[code] = glyph_id & 0xFFFF
end
end
end
end
end
end
end
@@ -0,0 +1,81 @@
# frozen_string_literal: true
module TTFunk
class Table
class Cmap
# Format 6: Trimmed table mapping.
#
# This module conditionally extends {TTFunk::Table::Cmap::Subtable}.
module Format06
# Language.
# @return [Integer]
attr_reader :language
# Code map.
# @return [Hash{Integer => Integer}]
attr_reader :code_map
# Encode the encoding record to format 6.
#
# @param charmap [Hash{Integer => Integer}] a hash mapping character
# codes to glyph IDs from the original font.
# @return [Hash]
# * `:charmap` (<tt>Hash{Integer => Hash}</tt>) keys are the characrers in
# `charset`, values are hashes:
# * `:old` (<tt>Integer</tt>) - glyph ID in the original font.
# * `:new` (<tt>Integer</tt>) - glyph ID in the subset font.
# that maps the characters in charmap to a
# * `:subtable` (<tt>String</tt>) - serialized encoding record.
# * `:max_glyph_id` (<tt>Integer</tt>) - maximum glyph ID in the new font.
def self.encode(charmap)
next_id = 0
glyph_map = { 0 => 0 }
sorted_chars = charmap.keys.sort
low_char = sorted_chars.first
high_char = sorted_chars.last
entry_count = 1 + high_char - low_char
glyph_indexes = Array.new(entry_count, 0)
new_map =
charmap.keys.sort.each_with_object({}) do |code, map|
glyph_map[charmap[code]] ||= next_id += 1
map[code] = { old: charmap[code], new: glyph_map[charmap[code]] }
glyph_indexes[code - low_char] = glyph_map[charmap[code]]
end
subtable = [
6, 10 + (entry_count * 2), 0, low_char, entry_count, *glyph_indexes,
].pack('n*')
{ charmap: new_map, subtable: subtable, max_glyph_id: next_id + 1 }
end
# Get glyph ID for character code.
#
# @param code [Integer] character code.
# @return [Integer] glyph ID.
def [](code)
@code_map[code] || 0
end
# Is this encoding record format supported?
#
# @return [true]
def supported?
true
end
private
def parse_cmap!
@language, firstcode, entrycount = read(8, 'x2nnn')
@code_map = {}
(firstcode...(firstcode + entrycount)).each do |code|
@code_map[code] = read(2, 'n').first & 0xFFFF
end
end
end
end
end
end
@@ -0,0 +1,87 @@
# frozen_string_literal: true
module TTFunk
class Table
class Cmap
# Format 10: Trimmed array.
#
# This module conditionally extends {TTFunk::Table::Cmap::Subtable}.
module Format10
# Language.
# @return [Integer]
attr_reader :language
# Code map.
# @return [Hash{Integer => Integer}]
attr_reader :code_map
# Encode the encoding record to format 10.
#
# @param charmap [Hash{Integer => Integer}] a hash mapping character
# codes to glyph IDs from the original font.
# @return [Hash]
# * `:charmap` (<tt>Hash{Integer => Hash}</tt>) keys are the characrers in
# `charset`, values are hashes:
# * `:old` (<tt>Integer</tt>) - glyph ID in the original font.
# * `:new` (<tt>Integer</tt>) - glyph ID in the subset font.
# that maps the characters in charmap to a
# * `:subtable` (<tt>String</tt>) - serialized encoding record.
# * `:max_glyph_id` (<tt>Integer</tt>) - maximum glyph ID in the new font.
def self.encode(charmap)
next_id = 0
glyph_map = { 0 => 0 }
sorted_chars = charmap.keys.sort
low_char = sorted_chars.first
high_char = sorted_chars.last
entry_count = 1 + high_char - low_char
glyph_indexes = Array.new(entry_count, 0)
new_map =
charmap.keys.sort.each_with_object({}) do |code, map|
glyph_map[charmap[code]] ||= next_id += 1
map[code] = { old: charmap[code], new: glyph_map[charmap[code]] }
glyph_indexes[code - low_char] = glyph_map[charmap[code]]
end
subtable = [
10, 0, 20 + (entry_count * 4), 0, low_char, entry_count,
*glyph_indexes,
].pack('nnN*')
{ charmap: new_map, subtable: subtable, max_glyph_id: next_id + 1 }
end
# Get glyph ID for character code.
#
# @param code [Integer] character code.
# @return [Integer] glyph ID.
def [](code)
@code_map[code] || 0
end
# Is this encoding record format supported?
#
# @return [true]
def supported?
true
end
private
def parse_cmap!
fractional_version, @language, firstcode, entrycount =
read(18, 'nx4NNN')
if fractional_version != 0
raise NotImplementedError,
"cmap version 10.#{fractional_version} is not supported"
end
@code_map = {}
(firstcode...(firstcode + entrycount)).each do |code|
@code_map[code] = read(2, 'n').first & 0xFFFF
end
end
end
end
end
end
@@ -0,0 +1,102 @@
# frozen_string_literal: true
module TTFunk
class Table
class Cmap
# Format 12: Segmented coverage.
#
# This module conditionally extends {TTFunk::Table::Cmap::Subtable}.
module Format12
# Language.
# @return [Integer]
attr_reader :language
# Code map.
# @return [Hash{Integer => Integer}]
attr_reader :code_map
# Encode the encoding record to format 12.
#
# @param charmap [Hash{Integer => Integer}] a hash mapping character
# codes to glyph IDs from the original font.
# @return [Hash]
# * `:charmap` (<tt>Hash{Integer => Hash}</tt>) keys are the characrers in
# `charset`, values are hashes:
# * `:old` (<tt>Integer</tt>) - glyph ID in the original font.
# * `:new` (<tt>Integer</tt>) - glyph ID in the subset font.
# that maps the characters in charmap to a
# * `:subtable` (<tt>String</tt>) - serialized encoding record.
# * `:max_glyph_id` (<tt>Integer</tt>) - maximum glyph ID in the new font.
def self.encode(charmap)
next_id = 0
glyph_map = { 0 => 0 }
range_firstglyphs = []
range_firstcodes = []
range_lengths = []
last_glyph = last_code = -999
new_map =
charmap.keys.sort.each_with_object({}) do |code, map|
glyph_map[charmap[code]] ||= next_id += 1
map[code] = { old: charmap[code], new: glyph_map[charmap[code]] }
if code > last_code + 1 || glyph_map[charmap[code]] > last_glyph + 1
range_firstcodes << code
range_firstglyphs << glyph_map[charmap[code]]
range_lengths << 1
else
range_lengths.push(range_lengths.pop) + 1
end
last_code = code
last_glyph = glyph_map[charmap[code]]
end
subtable = [
12, 0, 16 + (12 * range_lengths.size), 0, range_lengths.size,
].pack('nnNNN')
range_lengths.each_with_index do |length, i|
firstglyph = range_firstglyphs[i]
firstcode = range_firstcodes[i]
subtable << [
firstcode, firstcode + length - 1, firstglyph,
].pack('NNN')
end
{ charmap: new_map, subtable: subtable, max_glyph_id: next_id + 1 }
end
# Get glyph ID for character code.
#
# @param code [Integer] character code.
# @return [Integer] glyph ID.
def [](code)
@code_map[code] || 0
end
# Is this encoding record format supported?
#
# @return [true]
def supported?
true
end
private
def parse_cmap!
fractional_version, @language, groupcount = read(14, 'nx4NN')
if fractional_version != 0
raise NotImplementedError,
"cmap version 12.#{fractional_version} is not supported"
end
@code_map = {}
(1..groupcount).each do
startchar, endchar, startglyph = read(12, 'NNN')
(0..(endchar - startchar)).each do |offset|
@code_map[startchar + offset] = startglyph + offset
end
end
end
end
end
end
end
@@ -0,0 +1,143 @@
# frozen_string_literal: true
require_relative '../../reader'
module TTFunk
class Table
class Cmap
# Character to Glyph Index encoding record.
# This class can be extended with a format-specific
#
# @see TTFunk::Table::Cmap::Format00
# @see TTFunk::Table::Cmap::Format04
# @see TTFunk::Table::Cmap::Format06
# @see TTFunk::Table::Cmap::Format10
# @see TTFunk::Table::Cmap::Format12
class Subtable
include Reader
# Platform ID.
# @return [Integer]
attr_reader :platform_id
# Platform-specific encoding ID.
# @return [Integere]
attr_reader :encoding_id
# Record encoding format.
# @return [Integer]
attr_reader :format
# Most used encoding mappings.
ENCODING_MAPPINGS = {
mac_roman: { platform_id: 1, encoding_id: 0 }.freeze,
# Use microsoft unicode, instead of generic unicode, for optimal
# Windows support
unicode: { platform_id: 3, encoding_id: 1 }.freeze,
unicode_ucs4: { platform_id: 3, encoding_id: 10 }.freeze,
}.freeze
# Encode encoding record.
#
# @param charmap [Hash{Integer => Integer}] keys are code points in the
# used encoding, values are Unicode code points.
# @param encoding [Symbol] - one of the encodign mapping in
# {ENCODING_MAPPINGS}
# @return [Hash]
# * `:platform_id` (<tt>Integer</tt>) - Platform ID of this encoding record.
# * `:encoding_id` (<tt>Integer</tt>) - Encodign ID of this encoding record.
# * `:subtable` (<tt>String</tt>) - encoded encoding record.
# * `:max_glyph_id` (<tt>Integer</tt>) - maximum glyph ID in this encoding
# record.
# * `:charmap` (<tt>Hash{Integer => Hash}</tt>) - keys are codepoints in this
# encoding record, values are hashes:
# * `:new` - new glyph ID.
# * `:old` - glyph ID in the original font.
def self.encode(charmap, encoding)
case encoding
when :mac_roman
result = Format00.encode(charmap)
when :unicode
result = Format04.encode(charmap)
when :unicode_ucs4
result = Format12.encode(charmap)
else
raise NotImplementedError,
"encoding #{encoding.inspect} is not supported"
end
mapping = ENCODING_MAPPINGS[encoding]
# platform-id, encoding-id, offset
result.merge(
platform_id: mapping[:platform_id],
encoding_id: mapping[:encoding_id],
subtable: [
mapping[:platform_id],
mapping[:encoding_id],
12,
result[:subtable],
].pack('nnNA*'),
)
end
# @param file [TTFunk::File]
# @param table_start [Integer]
def initialize(file, table_start)
@file = file
@platform_id, @encoding_id, @offset = read(8, 'nnN')
@offset += table_start
parse_from(@offset) do
@format = read(2, 'n').first
case @format
when 0 then extend(TTFunk::Table::Cmap::Format00)
when 4 then extend(TTFunk::Table::Cmap::Format04)
when 6 then extend(TTFunk::Table::Cmap::Format06)
when 10 then extend(TTFunk::Table::Cmap::Format10)
when 12 then extend(TTFunk::Table::Cmap::Format12)
end
parse_cmap!
end
end
# Is this an encoding record for Unicode?
#
# @return [Boolean]
def unicode?
(platform_id == 3 && (encoding_id == 1 || encoding_id == 10) && format != 0) ||
(platform_id.zero? && format != 0)
end
# Is this encoding record format supported?
#
# @return [Boolean]
def supported?
false
end
# Get glyph ID for character code.
#
# @param _code [Integer] character code.
# @return [Integer] glyph ID.
def [](_code)
raise NotImplementedError, "cmap format #{@format} is not supported"
end
private
def parse_cmap!
# do nothing...
end
end
end
end
end
require_relative 'format00'
require_relative 'format04'
require_relative 'format06'
require_relative 'format10'
require_relative 'format12'