From 0221daad2dfcecb4b47e67161c30c535bfef8529 Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Sun, 14 Nov 2021 16:17:51 +0100 Subject: [PATCH 1/8] Add Font#font_style and Font#font_family --- lib/prawn/document.rb | 1 + lib/prawn/font.rb | 56 +++++++++++++++- lib/prawn/fonts/afm.rb | 1 + lib/prawn/fonts/ttf.rb | 1 + manual/text/font.rb | 15 +++-- manual/text/font_style.rb | 18 ++++-- spec/prawn/font_spec.rb | 133 +++++++++++++++++++++++++++++--------- 7 files changed, 184 insertions(+), 41 deletions(-) diff --git a/lib/prawn/document.rb b/lib/prawn/document.rb index 439667d5a..f12983450 100644 --- a/lib/prawn/document.rb +++ b/lib/prawn/document.rb @@ -218,6 +218,7 @@ def initialize(options = {}, &block) @background = options[:background] @background_scale = options[:background_scale] || 1 + @font = nil @font_size = 12 @bounding_box = nil diff --git a/lib/prawn/font.rb b/lib/prawn/font.rb index 6eb4dac94..d27507774 100644 --- a/lib/prawn/font.rb +++ b/lib/prawn/font.rb @@ -47,13 +47,16 @@ class Document # font files. # See font_families for more information. # - def font(name = nil, options = DEFAULT_OPTS) - return((defined?(@font) && @font) || font('Helvetica')) if name.nil? + def font(name = nil, options = nil) + return @font || font('Helvetica') if name.nil? && options.nil? if state.pages.empty? && !state.page.in_stamp_stream? raise Prawn::Errors::NotOnPage end + name ||= font_family + options ||= DEFAULT_OPTS + new_font = find_font(name.to_s, options) if block_given? @@ -68,6 +71,51 @@ def font(name = nil, options = DEFAULT_OPTS) @font end + def font_family + @font ? @font.family : 'Helvetica' + end + + # @method font_style(points=nil) + # + # When called with no argument, returns the current font style. + # + # When called with a single argument but no block, sets the current font + # style. When a block is used, the font style is applied transactionally and + # is rolled back when the block exits. You may still change the font style + # within a transactional block for individual text segments, or nested calls + # to font_style. + # + # Prawn::Document.generate("font_style.pdf") do + # font_style :bold + # text "Bold text" + # + # font_style(:italic) do + # text "Italic text" + # text "Bold text", :style => :bold + # text "Italic text" + # end + # + # text "Bold text" + # end + # + # Font styles are not additive, i.e. if the current style is :bold, setting + # the style to :italic results in italic, not bold-italic text. Use + # :bold_italic instead. + # + # When called without an argument, this method returns the current font + # style. + # + def font_style(style = nil, &block) + return @font ? @font.style : :normal if style.nil? + + font(font.name, style: style, &block) + end + + # Sets the font style + def font_style=(style = nil) + font_style(style) + end + # @method font_size(points=nil) # # When called with no argument, returns the current font size. @@ -299,6 +347,9 @@ class Font # The current font family attr_reader :family + # The current font style + attr_reader :style + # The options hash used to initialize the font attr_reader :options @@ -334,6 +385,7 @@ def initialize(document, name, options = {}) #:nodoc: @options = options @family = options[:family] + @style = options[:style] || :normal @identifier = generate_unique_id diff --git a/lib/prawn/fonts/afm.rb b/lib/prawn/fonts/afm.rb index dabd772b9..15a2e4668 100644 --- a/lib/prawn/fonts/afm.rb +++ b/lib/prawn/fonts/afm.rb @@ -72,6 +72,7 @@ def initialize(document, name, options = {}) #:nodoc: @kern_pair_table = font_data[:kern_pair_table] @attributes = font_data[:attributes] + @family ||= @attributes['familyname'] @ascender = @attributes['ascender'].to_i @descender = @attributes['descender'].to_i @line_gap = Float(bbox[3] - bbox[1]) - (@ascender - @descender) diff --git a/lib/prawn/fonts/ttf.rb b/lib/prawn/fonts/ttf.rb index 587093350..ebd41b899 100644 --- a/lib/prawn/fonts/ttf.rb +++ b/lib/prawn/fonts/ttf.rb @@ -47,6 +47,7 @@ def initialize(document, name, options = {}) super @ttf = read_ttf_file + @family ||= @ttf.name.font_family.first @subsets = TTFunk::SubsetCollection.new(@ttf) @italic_angle = nil diff --git a/manual/text/font.rb b/manual/text/font.rb index e4b353f5b..97bc4f530 100644 --- a/manual/text/font.rb +++ b/manual/text/font.rb @@ -5,11 +5,12 @@ # If we don't pass it any arguments it will return the current font being used # to render text. # -# If we just pass it a font name it will use that font for rendering text -# through the rest of the document. +# If we just pass it a font name and an options hash, it will use that font for +# rendering text through the rest of the document. Both the font name and +# the options can be omitted. Valid option keys are :style and :size. # -# It can also be used by passing a font name and a block. In this case the -# specified font will only be used to render text inside the block. +# It can also be used by passing a font name, an options hash and a block. In this +# case the specified font will only be used to render text inside the block. # # The default font is Helvetica. @@ -18,6 +19,7 @@ filename = File.basename(__FILE__).gsub('.rb', '.pdf') Prawn::ManualBuilder::Example.generate(filename) do text "Let's see which font we are using: #{font.inspect}" + text "Family is #{font_family}, style is #{font_style}, and size is #{font_size}" move_down 20 font 'Times-Roman' @@ -31,6 +33,11 @@ move_down 20 text 'Written in Times again as we left the previous block.' + move_down 20 + font nil, size: 16, style: :bold do + text 'Written in 16pt bold, still Times.' + end + move_down 20 text "Let's see which font we are using again: #{font.inspect}" diff --git a/manual/text/font_style.rb b/manual/text/font_style.rb index c6fd844e2..33db2a4e4 100644 --- a/manual/text/font_style.rb +++ b/manual/text/font_style.rb @@ -1,11 +1,16 @@ # frozen_string_literal: true +# The font_style method works just like the font +# method. +# +# In fact we can even use font with the :style option +# to declare which size we want. +# +# Another way to change the font size is by supplying the :style +# option to the text methods. +# # Most font families come with some styles other than normal. Most common are # bold, italic and bold_italic. -# -# The style can be set the using the :style option, with either the -# font method which will set the font and style for rest of the -# document, or with the inline text methods. require_relative '../example_helper' @@ -20,6 +25,11 @@ styles.each do |style| font example_font, style: style text "I'm writing in #{example_font} (#{style})" + + font_style style + text "This is also #{style}" + + text "And this is also #{style}", style: style end end end diff --git a/spec/prawn/font_spec.rb b/spec/prawn/font_spec.rb index 60ed4ebf0..f195f96ac 100644 --- a/spec/prawn/font_spec.rb +++ b/spec/prawn/font_spec.rb @@ -111,11 +111,109 @@ end end + describe '#font' do + it 'allows setting of size directly when font is created' do + pdf.font 'Courier', size: 16 + expect(pdf.font_size).to eq(16) + end + + it 'allows temporary setting of a new font using a transaction' do + pdf.font 'Helvetica', size: 12 + + pdf.font 'Courier', size: 16, style: :bold do + expect(pdf.font_family).to eq('Courier') + expect(pdf.font_size).to eq(16) + expect(pdf.font_style).to eq(:bold) + end + + expect(pdf.font.name).to eq('Helvetica') + expect(pdf.font_size).to eq(12) + expect(pdf.font_style).to eq(:normal) + end + + it 'masks font size when using a transacation' do + pdf.font 'Courier', size: 16 do + expect(pdf.font_size).to eq(16) + end + + pdf.font 'Times-Roman' + pdf.font 'Courier' + + expect(pdf.font_size).to eq(12) + end + end + describe '#font_size' do + it 'returns default font size' do + expect(pdf.font_size).to eq(12) + end + it 'allows setting font size in DSL style' do pdf.font_size 20 expect(pdf.font_size).to eq(20) end + + it 'allows setting font size in DSL style using a transaction' do + pdf.font_size 20 do + expect(pdf.font_size).to eq(20) + end + + expect(pdf.font_size).to eq(12) + end + + it 'allows setting font size as assignment' do + pdf.font_size = 20 + expect(pdf.font_size).to eq(20) + end + end + + describe '#font_style' do + it 'returns default font style' do + expect(pdf.font_style).to eq(:normal) + end + + it 'allows setting font style in DSL style' do + expect(pdf.font_style).to eq(:normal) + pdf.font_style :bold + expect(pdf.font_style).to eq(:bold) + expect(pdf.font.name).to eq('Helvetica-Bold') + end + + it 'allows setting font style in DSL style using a transaction' do + pdf.font_style :bold do + expect(pdf.font_style).to eq(:bold) + expect(pdf.font.name).to eq('Helvetica-Bold') + end + + expect(pdf.font_style).to eq(:normal) + expect(pdf.font.name).to eq('Helvetica') + end + + it 'allows setting font style as assignment' do + pdf.font_style = :bold + expect(pdf.font_style).to eq(:bold) + end + end + + describe '#font_family' do + it 'returns default font family' do + expect(pdf.font_family).to eq('Helvetica') + end + + it 'returns font family' do + pdf.font 'Courier', style: :bold + expect(pdf.font_family).to eq('Courier') + end + + it 'returns font family for AFM font' do + pdf.font 'ZapfDingbats' + expect(pdf.font_family).to eq('ZapfDingbats') + end + + it 'returns font family for TTF font' do + pdf.font "#{Prawn::DATADIR}/fonts/DejaVuSans-Bold.ttf" + expect(pdf.font_family).to eq('DejaVu Sans') + end end describe 'font style support' do @@ -142,11 +240,14 @@ pdf.font 'Helvetica' pdf.text 'In Normal Helvetica' + pdf.font nil, style: :bold + pdf.text 'In Bold Helvetica' + text = PDF::Inspector::Text.analyze(pdf.render) expect(text.font_settings.map { |e| e[:name] }).to eq( %i[ Courier-Bold Courier-BoldOblique Courier-Oblique - Courier Helvetica + Courier Helvetica Helvetica-Bold ] ) end @@ -233,36 +334,6 @@ end end - describe 'Transactional font handling' do - it 'allows setting of size directly when font is created' do - pdf.font 'Courier', size: 16 - expect(pdf.font_size).to eq(16) - end - - it 'allows temporary setting of a new font using a transaction' do - pdf.font 'Helvetica', size: 12 - - pdf.font 'Courier', size: 16 do - expect(pdf.font.name).to eq('Courier') - expect(pdf.font_size).to eq(16) - end - - expect(pdf.font.name).to eq('Helvetica') - expect(pdf.font_size).to eq(12) - end - - it 'masks font size when using a transacation' do - pdf.font 'Courier', size: 16 do - expect(pdf.font_size).to eq(16) - end - - pdf.font 'Times-Roman' - pdf.font 'Courier' - - expect(pdf.font_size).to eq(12) - end - end - describe 'Document#page_fonts' do it 'registers fonts properly by page' do pdf.font 'Courier' From 70a89b18eef100f6fb63047f7d3a74fc3944a642 Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Thu, 3 Feb 2022 21:42:19 +0100 Subject: [PATCH 2/8] Remove duplicate docs --- lib/prawn/font.rb | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/prawn/font.rb b/lib/prawn/font.rb index d27507774..a5c4ab106 100644 --- a/lib/prawn/font.rb +++ b/lib/prawn/font.rb @@ -102,9 +102,6 @@ def font_family # the style to :italic results in italic, not bold-italic text. Use # :bold_italic instead. # - # When called without an argument, this method returns the current font - # style. - # def font_style(style = nil, &block) return @font ? @font.style : :normal if style.nil? @@ -139,9 +136,6 @@ def font_style=(style = nil) # text "At size 16" # end # - # When called without an argument, this method returns the current font - # size. - # def font_size(points = nil) return @font_size unless points From 3673da54430ef6ebfc3f5f4006901ad318d4a9b2 Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Thu, 3 Feb 2022 21:42:35 +0100 Subject: [PATCH 3/8] Remove redundant test --- spec/prawn/font_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/prawn/font_spec.rb b/spec/prawn/font_spec.rb index f195f96ac..3685aa419 100644 --- a/spec/prawn/font_spec.rb +++ b/spec/prawn/font_spec.rb @@ -173,7 +173,6 @@ end it 'allows setting font style in DSL style' do - expect(pdf.font_style).to eq(:normal) pdf.font_style :bold expect(pdf.font_style).to eq(:bold) expect(pdf.font.name).to eq('Helvetica-Bold') From ec8bb83edc9f19d3e98bb43b5521c6b54561ab9d Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Thu, 3 Feb 2022 21:43:07 +0100 Subject: [PATCH 4/8] Preserve font_family, not font.name --- lib/prawn/font.rb | 2 +- spec/prawn/font_spec.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/prawn/font.rb b/lib/prawn/font.rb index a5c4ab106..f5bbb5357 100644 --- a/lib/prawn/font.rb +++ b/lib/prawn/font.rb @@ -105,7 +105,7 @@ def font_family def font_style(style = nil, &block) return @font ? @font.style : :normal if style.nil? - font(font.name, style: style, &block) + font(font_family, style: style, &block) end # Sets the font style diff --git a/spec/prawn/font_spec.rb b/spec/prawn/font_spec.rb index 3685aa419..4f7f2a340 100644 --- a/spec/prawn/font_spec.rb +++ b/spec/prawn/font_spec.rb @@ -188,6 +188,21 @@ expect(pdf.font.name).to eq('Helvetica') end + it 'allows setting font style for a TTF font' do + pdf.font_families.update( + 'DejaVu Sans' => { + normal: "#{Prawn::DATADIR}/fonts/DejaVuSans.ttf", + bold: "#{Prawn::DATADIR}/fonts/DejaVuSans-Bold.ttf" + } + ) + pdf.font 'DejaVu Sans' + expect(pdf.font_style).to eq(:normal) + pdf.font_style :bold + expect(pdf.font_style).to eq(:bold) + expect(pdf.font_family).to eq('DejaVu Sans') + expect(pdf.font.name).to eq("#{Prawn::DATADIR}/fonts/DejaVuSans-Bold.ttf") + end + it 'allows setting font style as assignment' do pdf.font_style = :bold expect(pdf.font_style).to eq(:bold) From 04cc07ab6cc08accaa165a606517b5c441c06f73 Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Thu, 3 Feb 2022 21:43:14 +0100 Subject: [PATCH 5/8] Better examples --- manual/text/font_style.rb | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/manual/text/font_style.rb b/manual/text/font_style.rb index 33db2a4e4..19f8cc5aa 100644 --- a/manual/text/font_style.rb +++ b/manual/text/font_style.rb @@ -16,20 +16,36 @@ filename = File.basename(__FILE__).gsub('.rb', '.pdf') Prawn::ManualBuilder::Example.generate(filename) do - fonts = %w[Courier Helvetica Times-Roman] - styles = %i[bold bold_italic italic normal] + text "The default style is normal" - fonts.each do |example_font| - move_down 20 + move_down 10 + font_style :bold + text 'This is bold' - styles.each do |style| - font example_font, style: style - text "I'm writing in #{example_font} (#{style})" + move_down 10 + font_style :italic + text 'This is italic (not bold, i.e. existing style is overwritten)' - font_style style - text "This is also #{style}" + move_down 10 + font_style :bold_italic + text 'This is bold italic' - text "And this is also #{style}", style: style - end + move_down 10 + font_style :normal + text 'Back to normal' + + move_down 10 + text 'A single line of italic', style: :italic + + move_down 10 + font_style :bold do + text 'A single line of bold' end + + move_down 10 + font 'Courier', style: :bold_italic + text 'This is Courier bold italic' + + font 'Courier' + text 'This is Courier normal (style is reset when changing font)' end From d81c04aaee9e8fc856b7763819c0884babd51d2e Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Mon, 7 Feb 2022 21:19:01 +0100 Subject: [PATCH 6/8] font name/family distinction --- lib/prawn/font.rb | 20 ++++++++--- lib/prawn/font_metric_cache.rb | 4 +-- lib/prawn/fonts/afm.rb | 1 - lib/prawn/fonts/ttf.rb | 1 - spec/prawn/font_spec.rb | 61 +++++++++++++++++++++++++--------- 5 files changed, 63 insertions(+), 24 deletions(-) diff --git a/lib/prawn/font.rb b/lib/prawn/font.rb index f5bbb5357..01f4a15fe 100644 --- a/lib/prawn/font.rb +++ b/lib/prawn/font.rb @@ -54,10 +54,8 @@ def font(name = nil, options = nil) raise Prawn::Errors::NotOnPage end - name ||= font_family options ||= DEFAULT_OPTS - - new_font = find_font(name.to_s, options) + new_font = find_font(name, options) if block_given? save_font do @@ -71,6 +69,9 @@ def font(name = nil, options = nil) @font end + # Returns the family of the current font, if the font was selected using + # a font family name, or nil, if the font was selected using a specific font + # name. def font_family @font ? @font.family : 'Helvetica' end @@ -105,7 +106,7 @@ def font_family def font_style(style = nil, &block) return @font ? @font.style : :normal if style.nil? - font(font_family, style: style, &block) + font(nil, style: style, &block) end # Sets the font style @@ -279,13 +280,22 @@ def save_font # # @private def find_font(name, options = {}) #:nodoc: + name ||= font_family || font.name + style = options[:style] || :normal if font_families.key?(name) family = name - name = font_families[name][options[:style] || :normal] + if !font_families[family].key?(style) + raise Prawn::Errors::UnknownFont, + "Font family `#{family}` has no `:#{style}` style." + end + name = font_families[family][style] if name.is_a?(::Hash) options = options.merge(name) name = options[:file] end + elsif style != :normal + options[:style] = :normal + warn "Style not supported for `#{name}`." end key = "#{name}:#{options[:font] || 0}" diff --git a/lib/prawn/font_metric_cache.rb b/lib/prawn/font_metric_cache.rb index 1f49f8a53..ecf86dded 100644 --- a/lib/prawn/font_metric_cache.rb +++ b/lib/prawn/font_metric_cache.rb @@ -24,8 +24,8 @@ def initialize(document) def width_of(string, options) f = if options[:style] - # override style with :style => :bold - @document.find_font(@document.font.family, style: options[:style]) + # override style with :style => :boldd + @document.find_font(nil, style: options[:style]) else @document.font end diff --git a/lib/prawn/fonts/afm.rb b/lib/prawn/fonts/afm.rb index 15a2e4668..dabd772b9 100644 --- a/lib/prawn/fonts/afm.rb +++ b/lib/prawn/fonts/afm.rb @@ -72,7 +72,6 @@ def initialize(document, name, options = {}) #:nodoc: @kern_pair_table = font_data[:kern_pair_table] @attributes = font_data[:attributes] - @family ||= @attributes['familyname'] @ascender = @attributes['ascender'].to_i @descender = @attributes['descender'].to_i @line_gap = Float(bbox[3] - bbox[1]) - (@ascender - @descender) diff --git a/lib/prawn/fonts/ttf.rb b/lib/prawn/fonts/ttf.rb index ebd41b899..587093350 100644 --- a/lib/prawn/fonts/ttf.rb +++ b/lib/prawn/fonts/ttf.rb @@ -47,7 +47,6 @@ def initialize(document, name, options = {}) super @ttf = read_ttf_file - @family ||= @ttf.name.font_family.first @subsets = TTFunk::SubsetCollection.new(@ttf) @italic_angle = nil diff --git a/spec/prawn/font_spec.rb b/spec/prawn/font_spec.rb index 4f7f2a340..31e749993 100644 --- a/spec/prawn/font_spec.rb +++ b/spec/prawn/font_spec.rb @@ -71,12 +71,19 @@ end end - it 'reports missing font with style' do + it 'raises on missing style in family' do expect do - pdf.font('Nada', style: :bold) do - pdf.width_of('hello') + pdf.font('Helvetica') do + pdf.width_of('hello', style: :heavy) end - end.to raise_error(Prawn::Errors::UnknownFont, /Nada \(bold\)/) + end.to raise_error(Prawn::Errors::UnknownFont, /Font family `Helvetica` has no `:heavy` style/) + end + + it 'warns on style with single font' do + expect(pdf).to receive(:warn).with(/Style not supported for/).once + pdf.font('Courier-Bold') do + pdf.width_of('hello', style: :bold) + end end it 'calculates styled widths correctly using TTFs' do @@ -113,7 +120,7 @@ describe '#font' do it 'allows setting of size directly when font is created' do - pdf.font 'Courier', size: 16 + pdf.font 'Courier', size: 16 #, style: :bold expect(pdf.font_size).to eq(16) end @@ -141,6 +148,19 @@ expect(pdf.font_size).to eq(12) end + + it 'raises on missing style in family' do + expect do + pdf.font 'Helvetica', style: :heavy + end.to raise_error(Prawn::Errors::UnknownFont, /Font family `Helvetica` has no `:heavy` style/) + end + + it 'warns on style with single font' do + expect(pdf).to receive(:warn).with(/Style not supported for/).once + pdf.font 'Courier-Bold', style: :bold + expect(pdf.font.name).to eq('Courier-Bold') + expect(pdf.font_style).to eq(:normal) + end end describe '#font_size' do @@ -190,16 +210,16 @@ it 'allows setting font style for a TTF font' do pdf.font_families.update( - 'DejaVu Sans' => { + 'deja' => { normal: "#{Prawn::DATADIR}/fonts/DejaVuSans.ttf", bold: "#{Prawn::DATADIR}/fonts/DejaVuSans-Bold.ttf" } ) - pdf.font 'DejaVu Sans' + pdf.font 'deja' expect(pdf.font_style).to eq(:normal) pdf.font_style :bold expect(pdf.font_style).to eq(:bold) - expect(pdf.font_family).to eq('DejaVu Sans') + expect(pdf.font_family).to eq('deja') expect(pdf.font.name).to eq("#{Prawn::DATADIR}/fonts/DejaVuSans-Bold.ttf") end @@ -207,6 +227,22 @@ pdf.font_style = :bold expect(pdf.font_style).to eq(:bold) end + + it 'raises on missing style in family' do + expect do + pdf.font('Helvetica') do + pdf.font_style :heavy + end + end.to raise_error(Prawn::Errors::UnknownFont, /Font family `Helvetica` has no `:heavy` style/) + end + + it 'warns on style with single font' do + expect(pdf).to receive(:warn).with(/Style not supported for/).once + pdf.font('Courier-Bold') do + pdf.font_style :bold + expect(pdf.font_style).to eq(:normal) + end + end end describe '#font_family' do @@ -219,14 +255,9 @@ expect(pdf.font_family).to eq('Courier') end - it 'returns font family for AFM font' do + it 'returns nil font family for specific font' do pdf.font 'ZapfDingbats' - expect(pdf.font_family).to eq('ZapfDingbats') - end - - it 'returns font family for TTF font' do - pdf.font "#{Prawn::DATADIR}/fonts/DejaVuSans-Bold.ttf" - expect(pdf.font_family).to eq('DejaVu Sans') + expect(pdf.font_family).to be_nil end end From 8bc9f0be4ca327e1e45a486200c0211c9add9ee1 Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Mon, 7 Feb 2022 21:25:59 +0100 Subject: [PATCH 7/8] Test warnings --- lib/prawn/fonts/afm.rb | 2 +- spec/prawn/font_spec.rb | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/prawn/fonts/afm.rb b/lib/prawn/fonts/afm.rb index dabd772b9..8d7795858 100644 --- a/lib/prawn/fonts/afm.rb +++ b/lib/prawn/fonts/afm.rb @@ -55,7 +55,7 @@ def initialize(document, name, options = {}) #:nodoc: name ||= options[:family] unless BUILT_INS.include?(name) raise Prawn::Errors::UnknownFont, - "#{name} (#{options[:style] || 'normal'}) is not a known font." + "`#{name}` is not a known font." end super diff --git a/spec/prawn/font_spec.rb b/spec/prawn/font_spec.rb index 31e749993..53e920aa6 100644 --- a/spec/prawn/font_spec.rb +++ b/spec/prawn/font_spec.rb @@ -76,11 +76,11 @@ pdf.font('Helvetica') do pdf.width_of('hello', style: :heavy) end - end.to raise_error(Prawn::Errors::UnknownFont, /Font family `Helvetica` has no `:heavy` style/) + end.to raise_error(Prawn::Errors::UnknownFont, 'Font family `Helvetica` has no `:heavy` style.') end it 'warns on style with single font' do - expect(pdf).to receive(:warn).with(/Style not supported for/).once + expect(pdf).to receive(:warn).with('Style not supported for `Courier-Bold`.').once pdf.font('Courier-Bold') do pdf.width_of('hello', style: :bold) end @@ -149,14 +149,20 @@ expect(pdf.font_size).to eq(12) end + it 'raises on unknown font' do + expect do + pdf.font 'Arial Black' + end.to raise_error(Prawn::Errors::UnknownFont, '`Arial Black` is not a known font.') + end + it 'raises on missing style in family' do expect do pdf.font 'Helvetica', style: :heavy - end.to raise_error(Prawn::Errors::UnknownFont, /Font family `Helvetica` has no `:heavy` style/) + end.to raise_error(Prawn::Errors::UnknownFont, 'Font family `Helvetica` has no `:heavy` style.') end it 'warns on style with single font' do - expect(pdf).to receive(:warn).with(/Style not supported for/).once + expect(pdf).to receive(:warn).with('Style not supported for `Courier-Bold`.').once pdf.font 'Courier-Bold', style: :bold expect(pdf.font.name).to eq('Courier-Bold') expect(pdf.font_style).to eq(:normal) @@ -233,11 +239,11 @@ pdf.font('Helvetica') do pdf.font_style :heavy end - end.to raise_error(Prawn::Errors::UnknownFont, /Font family `Helvetica` has no `:heavy` style/) + end.to raise_error(Prawn::Errors::UnknownFont, 'Font family `Helvetica` has no `:heavy` style.') end it 'warns on style with single font' do - expect(pdf).to receive(:warn).with(/Style not supported for/).once + expect(pdf).to receive(:warn).with('Style not supported for `Courier-Bold`.').once pdf.font('Courier-Bold') do pdf.font_style :bold expect(pdf.font_style).to eq(:normal) From ff13eaf6b26a50b8380bf40ae07c9ff620e3037f Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Mon, 7 Feb 2022 21:29:35 +0100 Subject: [PATCH 8/8] Update comment --- lib/prawn/font.rb | 9 ++++++--- lib/prawn/font_metric_cache.rb | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/prawn/font.rb b/lib/prawn/font.rb index 01f4a15fe..0959ea355 100644 --- a/lib/prawn/font.rb +++ b/lib/prawn/font.rb @@ -69,9 +69,9 @@ def font(name = nil, options = nil) @font end - # Returns the family of the current font, if the font was selected using - # a font family name, or nil, if the font was selected using a specific font - # name. + # Returns the family name of the current font, if the font was selected using + # a font family name, or nil, if the font was specified as a specific built-in + # font or a TTF file. def font_family @font ? @font.family : 'Helvetica' end @@ -103,6 +103,9 @@ def font_family # the style to :italic results in italic, not bold-italic text. Use # :bold_italic instead. # + # Font style can only be applied, if the current font is specified using a + # font family name, not a specific built-in font or TTF file. + # def font_style(style = nil, &block) return @font ? @font.style : :normal if style.nil? diff --git a/lib/prawn/font_metric_cache.rb b/lib/prawn/font_metric_cache.rb index ecf86dded..2db8b16c2 100644 --- a/lib/prawn/font_metric_cache.rb +++ b/lib/prawn/font_metric_cache.rb @@ -24,7 +24,7 @@ def initialize(document) def width_of(string, options) f = if options[:style] - # override style with :style => :boldd + # override style with :style => :bold @document.find_font(nil, style: options[:style]) else @document.font