Skip to content

Use font family name for caching in #find_font - #924

Closed
maerch wants to merge 1 commit into
prawnpdf:masterfrom
maerch:more-unique-font-hash-key
Closed

Use font family name for caching in #find_font#924
maerch wants to merge 1 commit into
prawnpdf:masterfrom
maerch:more-unique-font-hash-key

Conversation

@maerch

@maerch maerch commented Dec 11, 2015

Copy link
Copy Markdown
Contributor

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.

@johncip

johncip commented Jun 4, 2016

Copy link
Copy Markdown
Contributor

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.

@pointlessone

Copy link
Copy Markdown
Member

@maerch Could you please provide specs for the change?

@pointlessone

Copy link
Copy Markdown
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants