From 382aa477ff898e81204eed875fa8ee72f4884552 Mon Sep 17 00:00:00 2001 From: Matjaz Gregoric Date: Tue, 7 Apr 2026 18:07:24 +0200 Subject: [PATCH] Fix ToUnicode generation There was a typo in the code that generates ToUnicode mapping for font subsets, which caused prawn to generate a mapping with missing or duplicate character mappings. --- lib/prawn/fonts/to_unicode_cmap.rb | 2 +- spec/prawn/fonts/to_unicode_cmap_spec.rb | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/prawn/fonts/to_unicode_cmap.rb b/lib/prawn/fonts/to_unicode_cmap.rb index 84e856d4a..e3f87f4e6 100644 --- a/lib/prawn/fonts/to_unicode_cmap.rb +++ b/lib/prawn/fonts/to_unicode_cmap.rb @@ -140,7 +140,7 @@ def mapping_spans(mapping) discontinuous_slices .flatten(1) # Join together .slice_when { |a, b| (b[0] - a[0]) != 1 } # Slice at key discontinuity, again - .map { |span| span.length > 1 ? [:index_sorted, span] : [:short, slice] } + + .map { |span| span.length > 1 ? [:index_sorted, span] : [:short, span] } + continuous_slices.map { |span| [:fully_sorted, span] } end } # rubocop: disable Style/MultilineBlockChain diff --git a/spec/prawn/fonts/to_unicode_cmap_spec.rb b/spec/prawn/fonts/to_unicode_cmap_spec.rb index 1a4effd17..41388e1b4 100644 --- a/spec/prawn/fonts/to_unicode_cmap_spec.rb +++ b/spec/prawn/fonts/to_unicode_cmap_spec.rb @@ -70,7 +70,7 @@ expect(cmap).to include("beginbfrange\n<20><22><0030>\n") end - it 'uses ranges for continuous code rnages with non-continuous mappings' do + it 'uses ranges for continuous code ranges with non-continuous mappings' do cmap = described_class.new(0x20 => 0x32, 0x21 => 0x31, 0x22 => 0x30).generate expect(cmap).to include("beginbfrange\n<20><22>[<0032><0031><0030>]\n") @@ -82,6 +82,13 @@ expect(cmap).to include("beginbfchar\n<30><0040>\n") end + it 'splits mappings to continuous and non-coninuous code ranges' do + cmap = described_class.new(0x20 => 0x30, 0x21 => 0x31, 0x22 => 0x33).generate + + expect(cmap).to include("beginbfrange\n<20><21><0030>\n") + expect(cmap).to include("beginbfchar\n<22><0033>\n") + end + it 'splits continuous mappings into groups of 100' do mapping = (1..142).flat_map { |n| Array.new(3) { |i| [(n * 10) + i, (n * 10) + i] } }.to_h cmap = described_class.new(mapping).generate