Skip to content

Commit

Permalink
optimize(fons): Memoize Font Path
Browse files Browse the repository at this point in the history
* Memoize the `Prawn::Icon::FontData#path` method as it performs
  a filesystem glob and is currently as hot code path.
* Update the README to include Material Design Icons.
  • Loading branch information
jessedoyle committed Sep 2, 2022
1 parent 672499b commit 1d0c99b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Update our CI matrix to include recent versions of Prawn and Ruby! Thanks @petergoldstein! (#55)
* Resolve a few code smells that were flagged by Rubocop.
* [Material Design Icons](https://materialdesignicons.com) are now supported! Currently version `7.0.96` is included. Thanks @maneex! [https://github.com/jessedoyle/prawn-icon/pull/59](Pull Request).
* Memoize calls to `Prawn::Icon::FontData#path` to improve performance.

#### Material Design Icons

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Prawn::Icon provides a simple mechanism for rendering icons and icon fonts from
The following icon fonts ship with Prawn::Icon:

* FontAwesome (http://fontawesome.io/icons/)
* Material Design Icons (https://materialdesignicons.com/)
* Foundation Icons (http://zurb.com/playground/foundation-icon-fonts-3)
* PaymentFont (https://paymentfont.com)

Expand Down
22 changes: 12 additions & 10 deletions lib/prawn/icon/font_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,19 @@ def load_fonts(document)
end

def path
font = Icon.configuration.font_directory
.join(@set.to_s)
.glob('*.ttf')
.first

if font.nil?
raise Prawn::Errors::UnknownFont,
"Icon font not found for set: #{@set}"
end
@path = begin
font = Icon.configuration.font_directory
.join(@set.to_s)
.glob('*.ttf')
.first

if font.nil?
raise Prawn::Errors::UnknownFont,
"Icon font not found for set: #{@set}"
end

@path ||= font.to_s
font.to_s
end
end

def specifier
Expand Down

0 comments on commit 1d0c99b

Please sign in to comment.