From e2f5ab396524ea3f8a3e2d2523d7617d9213d675 Mon Sep 17 00:00:00 2001 From: Alexander Mankuta Date: Thu, 29 Jan 2026 23:02:35 +0200 Subject: [PATCH] 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. --- lib/ttfunk/table/cff/dict.rb | 10 ++++++---- spec/ttfunk/table/cff/dict_spec.rb | 15 +++++++++++++++ ttfunk.gemspec | 2 +- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/ttfunk/table/cff/dict.rb b/lib/ttfunk/table/cff/dict.rb index 7aeee25..729986a 100644 --- a/lib/ttfunk/table/cff/dict.rb +++ b/lib/ttfunk/table/cff/dict.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'bigdecimal' - module TTFunk class Table class Cff < TTFunk::Table @@ -92,7 +90,7 @@ def encode_operand(operand) case operand when Integer encode_integer(operand) - when Float, BigDecimal + when Float, ->(op) { defined?(BigDecimal) && op.is_a?(BigDecimal) } encode_float(operand) when SciForm encode_sci(operand) @@ -141,7 +139,11 @@ def encode_exponent(exp) end def encode_significand(sig) - sig.to_s.each_char.with_object([]) do |char, ret| + if defined?(BigDecimal) && sig.is_a?(BigDecimal) + sig.to_s('F') + else + sig.to_s + end.each_char.with_object([]) do |char, ret| case char when '0'..'9' ret << Integer(char) diff --git a/spec/ttfunk/table/cff/dict_spec.rb b/spec/ttfunk/table/cff/dict_spec.rb index a3ae164..abcd780 100644 --- a/spec/ttfunk/table/cff/dict_spec.rb +++ b/spec/ttfunk/table/cff/dict_spec.rb @@ -106,4 +106,19 @@ expect(dict1.encode).to eq(dict2.encode) end + + begin + require('bigdecimal') + rescue StandardError + # ignore + end + if defined?(BigDecimal) + it 'encodes BigDecimal' do + dict = described_class.new(TestFile.new(StringIO.new('')), 0, 0) + + dict[1] = BigDecimal('42') + + expect(dict.encode).to eq("\x1EB\xA0\xFF\x01".b) + end + end end diff --git a/ttfunk.gemspec b/ttfunk.gemspec index ff758a8..ab0c1e0 100644 --- a/ttfunk.gemspec +++ b/ttfunk.gemspec @@ -50,6 +50,6 @@ Gem::Specification.new do |spec| } spec.required_ruby_version = '>= 2.7' - spec.add_dependency('bigdecimal', '~> 3.1') + spec.add_development_dependency('bigdecimal', '>= 3.1') spec.add_development_dependency('prawn-dev', '~> 0.6.0') end