Skip to content

Commit 2c5ac03

Browse files
committed
Make bigdecimal an optional dependency
Since we already support BigDecimal in CFF we'll keep it. That said, it seems like a very farfetched scenario. It doesn't lay in the re-encoding path. It can only be there by manually construction a CFF Dict which is a very specific an unconventionl thing to do. Anyway, since a BigDecimal can only come from the user code we'll leave it to the user to add the dependency.
1 parent f55c10f commit 2c5ac03

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

lib/ttfunk/table/cff/dict.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# frozen_string_literal: true
22

3-
require 'bigdecimal'
4-
53
module TTFunk
64
class Table
75
class Cff < TTFunk::Table
@@ -92,7 +90,7 @@ def encode_operand(operand)
9290
case operand
9391
when Integer
9492
encode_integer(operand)
95-
when Float, BigDecimal
93+
when Float, ->(op) { defined?(BigDecimal) && op.is_a?(BigDecimal) }
9694
encode_float(operand)
9795
when SciForm
9896
encode_sci(operand)
@@ -141,7 +139,11 @@ def encode_exponent(exp)
141139
end
142140

143141
def encode_significand(sig)
144-
sig.to_s.each_char.with_object([]) do |char, ret|
142+
if defined?(BigDecimal) && sig.is_a?(BigDecimal)
143+
sig.to_s('F')
144+
else
145+
sig.to_s
146+
end.each_char.with_object([]) do |char, ret|
145147
case char
146148
when '0'..'9'
147149
ret << Integer(char)

spec/ttfunk/table/cff/dict_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,19 @@
106106

107107
expect(dict1.encode).to eq(dict2.encode)
108108
end
109+
110+
begin
111+
require('bigdecimal')
112+
rescue StandardError
113+
# ignore
114+
end
115+
if defined?(BigDecimal)
116+
it 'encodes BigDecimal' do
117+
dict = described_class.new(TestFile.new(StringIO.new('')), 0, 0)
118+
119+
dict[1] = BigDecimal('42')
120+
121+
expect(dict.encode).to eq("\x1EB\xA0\xFF\x01".b)
122+
end
123+
end
109124
end

ttfunk.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ Gem::Specification.new do |spec|
5050
}
5151

5252
spec.required_ruby_version = '>= 2.7'
53-
spec.add_dependency('bigdecimal', '~> 3.1')
53+
spec.add_development_dependency('bigdecimal')
5454
spec.add_development_dependency('prawn-dev', '~> 0.6.0')
5555
end

0 commit comments

Comments
 (0)