Skip to content

Commit

Permalink
Update code style
Browse files Browse the repository at this point in the history
  • Loading branch information
pointlessone committed Mar 1, 2024
1 parent 88b7a19 commit 0a0d447
Show file tree
Hide file tree
Showing 81 changed files with 542 additions and 754 deletions.
13 changes: 13 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ RSpec/MultipleMemoizedHelpers:
Exclude:
- spec/ttfunk/table/os2_spec.rb

RSpec/SpecFilePathFormat:
CustomTransform:
TTFunk: ttfunk
Exclude:
- spec/ttfunk/table/cff/charstrings_index_spec.rb
- spec/ttfunk/table/cff/name_index_spec.rb

Style/FormatStringToken:
Exclude:
- examples/metrics.rb


Style/Documentation:
Exclude:
- examples/**/*.rb
- spec/**/*.rb
14 changes: 7 additions & 7 deletions examples/metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
require_relative '../lib/ttfunk'

def character_lookup(file, character)
puts "character : #{character}"
puts("character : #{character}")

character_code = character.unpack1('U*')
puts "character code: #{character_code}"
puts("character code: #{character_code}")

glyph_id = file.cmap.unicode.first[character_code]
puts "glyph id : #{glyph_id}"
puts("glyph id : #{glyph_id}")

glyph = file.glyph_outlines.for(glyph_id)
puts format('glyph type : %s', glyph.class.name.split(/::/).last.downcase)
puts format('glyph size : %db', glyph.raw.length)
puts format('glyph bbox : (%d,%d)-(%d,%d)', glyph.x_min, glyph.y_min, glyph.x_max, glyph.y_max)
puts(format('glyph type : %s', glyph.class.name.split('::').last.downcase))
puts(format('glyph size : %db', glyph.raw.length))
puts(format('glyph bbox : (%d,%d)-(%d,%d)', glyph.x_min, glyph.y_min, glyph.x_max, glyph.y_max))

if glyph.compound?
puts format('components : %d %s', glyph.glyph_ids.length, glyph.glyph_ids.inspect)
puts(format('components : %d %s', glyph.glyph_ids.length, glyph.glyph_ids.inspect))
end
end

Expand Down
4 changes: 2 additions & 2 deletions examples/string_width.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ def initialize(path_to_file)
end

def width_of(string)
string.split('').sum { |char| character_width(char) }
string.chars.sum { |char| character_width(char) }
end

def character_width(character)
width_in_units = horizontal_metrics.for(glyph_id(character)).advance_width
width_in_units.to_f / units_per_em
Float(width_in_units) / units_per_em
end

def units_per_em
Expand Down
8 changes: 4 additions & 4 deletions lib/ttfunk/bin_utils.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

module TTFunk
module TTFunk # rubocop: disable Style/Documentation # false positive
# Bit crunching utility methods.
module BinUtils
# Turn a bunch of small integers into one big integer. Assumes big-endian.
Expand All @@ -12,7 +12,7 @@ def stitch_int(arr, bit_width:)
value = 0

arr.each_with_index do |element, index|
value |= element << bit_width * index
value |= element << (bit_width * index)
end

value
Expand All @@ -26,10 +26,10 @@ def stitch_int(arr, bit_width:)
# needed for cases where top bits are zero.
# @return [Array<Integer>]
def slice_int(value, bit_width:, slice_count:)
mask = 2**bit_width - 1
mask = (2**bit_width) - 1

Array.new(slice_count) do |i|
(value >> bit_width * i) & mask
(value >> (bit_width * i)) & mask
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/ttfunk/bit_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ def on(pos)
# @param pos [Integer]
# @return [Boolean]
def on?(pos)
(value & 2**pos).positive?
(value & (2**pos)).positive?
end

# Set bit off.
#
# @param pos [Integer]
# @return [void]
def off(pos)
@value &= 2**Math.log2(value).ceil - 2**pos - 1
@value &= (2**Math.log2(value).ceil) - (2**pos) - 1
end

# Is bit off?
Expand Down
6 changes: 3 additions & 3 deletions lib/ttfunk/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ class Collection
# @return [any] whatever the block returns
def self.open(path)
if path.respond_to?(:read)
result = yield new(path)
result = yield(new(path))
path.rewind
result
else
::File.open(path, 'rb') do |io|
yield new(io)
yield(new(io))
end
end
end
Expand Down Expand Up @@ -55,7 +55,7 @@ def count
# @return [self]
def each
count.times do |index|
yield self[index]
yield(self[index])
end
self
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ttfunk/directory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def initialize(io, offset = 0)
tag: tag,
checksum: checksum,
offset: offset,
length: length
length: length,
}
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/ttfunk/encoded_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class DuplicatePlaceholderError < StandardError
class EncodedString
# @yieldparam [self]
def initialize
yield self if block_given?
yield(self) if block_given?
end

# Append to string.
Expand All @@ -35,7 +35,7 @@ def <<(obj)
io << obj
when Placeholder
add_placeholder(obj)
io << "\0" * obj.length
io << ("\0" * obj.length)
when self.class
# adjust placeholders to be relative to the entire encoded string
obj.placeholders.each_pair do |_, placeholder|
Expand Down Expand Up @@ -65,7 +65,7 @@ def concat(*objs)
# @return [self]
def align!(width = 4)
if (length % width).positive?
self << "\0" * (width - length % width)
self << ("\0" * (width - (length % width)))
end

self
Expand Down
6 changes: 2 additions & 4 deletions lib/ttfunk/otf_encoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ module TTFunk
# Encodes a CFF-based OpenType font subset to its binary representation.
class OTFEncoder < TTFEncoder
# Optimal table order according to OpenType specification.
OPTIMAL_TABLE_ORDER = [
'head', 'hhea', 'maxp', 'OS/2', 'name', 'cmap', 'post', 'CFF '
].freeze
OPTIMAL_TABLE_ORDER = ['head', 'hhea', 'maxp', 'OS/2', 'name', 'cmap', 'post', 'CFF '].freeze

private

Expand Down Expand Up @@ -40,7 +38,7 @@ def tables
@tables ||= super.merge(
'BASE' => base_table,
'VORG' => vorg_table,
'CFF ' => cff_table
'CFF ' => cff_table,
).compact
end

Expand Down
8 changes: 4 additions & 4 deletions lib/ttfunk/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def to_signed(number)
def parse_from(position)
saved = io.pos
io.pos = position
result = yield position
result = yield(position)
io.pos = saved
result
end
Expand All @@ -38,12 +38,12 @@ def hexdump(string)
if ((i + 1) % 16).zero?
puts
elsif ((i + 1) % 8).zero?
print ' '
print(' ')
else
print ' '
print(' ')
end
end
puts unless (bytes.length % 16).zero?
puts if (bytes.length % 16) != 0
end
end
end
10 changes: 5 additions & 5 deletions lib/ttfunk/resource_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class ResourceFile
# @yieldparam resource_file [TTFunk::ResourceFile]
# @return [any] result of the block
def self.open(path)
::File.open(path, 'rb') do |io|
::File.open(path, 'rb') { |io|
file = new(io)
yield file
end
yield(file)
}
end

# @param io [IO]
Expand Down Expand Up @@ -51,7 +51,7 @@ def initialize(io)
id: id,
attributes: attr,
offset: data_ofs,
handle: handle
handle: handle,
}

if name_list_offset + name_ofs < map_offset + map_length
Expand Down Expand Up @@ -95,7 +95,7 @@ def [](type, index = 0)
# @param type [String]
# @return [Array<String>]
def resources_for(type)
(@map[type] && @map[type][:named] || {}).keys
((@map[type] && @map[type][:named]) || {}).keys
end

private
Expand Down
5 changes: 3 additions & 2 deletions lib/ttfunk/sci_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module TTFunk
# Scientific number representation
class SciForm
alias eql? ==
# Significand
# @return [Float, Integer]
attr_reader :significand
Expand All @@ -23,7 +22,7 @@ def initialize(significand, exponent = 0)
#
# @return [Float]
def to_f
significand * 10**exponent
significand * (10**exponent)
end

# Check equality to another number.
Expand All @@ -41,5 +40,7 @@ def ==(other)
false
end
end

alias eql? ==
end
end
2 changes: 1 addition & 1 deletion lib/ttfunk/sub_table.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require_relative './reader'
require_relative 'reader'

module TTFunk
# SFNT sub-table
Expand Down
1 change: 0 additions & 1 deletion lib/ttfunk/subset/code_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ def new_cmap_table
end
end


# Get the list of Glyph IDs from the original font that are in this
# subset.
#
Expand Down
2 changes: 1 addition & 1 deletion lib/ttfunk/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def raw
#
# @return [String]
def tag
self.class.name.split(/::/).last.downcase
self.class.name.split('::').last.downcase
end

private
Expand Down
17 changes: 4 additions & 13 deletions lib/ttfunk/table/cff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def encode(subset)
name_index.encode,
top_index.encode,
string_index.encode,
global_subr_index.encode
global_subr_index.encode,
)

charmap = subset.new_cmap_table[:charmap]
Expand All @@ -80,18 +80,9 @@ def encode(subset)
def parse!
@header = Header.new(file, offset)
@name_index = Index.new(file, @header.table_offset + @header.length)

@top_index = TopIndex.new(
file, @name_index.table_offset + @name_index.length
)

@string_index = OneBasedIndex.new(
file, @top_index.table_offset + @top_index.length
)

@global_subr_index = SubrIndex.new(
file, @string_index.table_offset + @string_index.length
)
@top_index = TopIndex.new(file, @name_index.table_offset + @name_index.length)
@string_index = OneBasedIndex.new(file, @top_index.table_offset + @top_index.length)
@global_subr_index = SubrIndex.new(file, @string_index.table_offset + @string_index.length)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/ttfunk/table/cff/charset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def each
return to_enum(__method__) unless block_given?

# +1 adjusts for the implicit .notdef glyph
(items_count + 1).times { |i| yield self[i] }
(items_count + 1).times { |i| yield(self[i]) }
end

# Get character name for glyph index.
Expand Down Expand Up @@ -256,15 +256,15 @@ def element_width(fmt = format_sym)
{
array_format: 2, # SID
range_format8: 3, # SID + Card8
range_format16: 4 # SID + Card16
range_format16: 4, # SID + Card16
}[fmt]
end

def element_format(fmt = format_sym)
{
array_format: 'n',
range_format8: 'nC',
range_format16: 'nn'
range_format16: 'nn',
}[fmt]
end

Expand All @@ -282,7 +282,7 @@ def format_int(sym = format_sym)
{
array_format: ARRAY_FORMAT,
range_format8: RANGE_FORMAT_8,
range_format16: RANGE_FORMAT_16
range_format16: RANGE_FORMAT_16,
}[sym]
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/ttfunk/table/cff/charsets/expert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ module Charsets
'Ucircumflexsmall',
'Udieresissmall',
'Yacutesmall',
'Thornsmall'
]
'Thornsmall',
],
).freeze
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/ttfunk/table/cff/charsets/expert_subset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ module Charsets
'nineinferior',
'centinferior',
'dollarinferior',
'periodinferior'
]
'periodinferior',
],
).freeze
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/ttfunk/table/cff/charsets/iso_adobe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ module Charsets
'ugrave',
'yacute',
'ydieresis',
'zcaron'
]
'zcaron',
],
).freeze
end
end
Expand Down
Loading

0 comments on commit 0a0d447

Please sign in to comment.