prawn icon indicating copy to clipboard operation
prawn copied to clipboard

Use font family name for caching in `#find_font`

Open maerch opened this issue 9 years ago • 2 comments

I am not sure about this PR, so please feel free to discuss this.

Recently I ran into an issue I debugged which was caused to the font caching mechanism. So, let us say we have the following prawn code.

require "prawn"

Prawn::Document.generate("font_caching.pdf") do
  font_families.update({
    "foo" => {
      bold:   "OpenSans-Bold.ttf",
      normal: "OpenSans-Regular.ttf",
    },
    "bar" => {
      bold:   "OpenSans-Semibold.ttf",
      normal: "OpenSans-Regular.ttf",
    }
  })

  font "foo"

  text "Open Sans Regular"
  text "Open Sans Bold", style: :bold

  font "bar" do
    text "Open Sans Regular"
    text "Open Sans SemiBold", style: :bold
  end
end

After you have defined a default font family with, e.g. font("foo"), you will run into caching problems once you try to use bar. In particular you will never actually use the OpenSans-Semibold.ttf font, but always the OpenSans-Bold.ttf. Putting the family name in the key fixes this issue.

maerch avatar Dec 11 '15 11:12 maerch

I can confirm that this happens in master and that your branch fixes it. Specifically, it happens when the normal variant of the two font families have the same filename.

While I can't think of a use case for having two different font families which share a normal font, that doesn't mean that one doesn't exist, and the current behavior is confusing. FWIW I think this is a good change.

johncip avatar Jun 04 '16 10:06 johncip

@maerch Could you please provide specs for the change?

pointlessone avatar Feb 12 '17 13:02 pointlessone

@maerch Thank you for your contribution. This has been merged outside of GitHub.

pointlessone avatar Jan 04 '24 13:01 pointlessone