Skip to content

Commit

Permalink
Add config options additional_fonts and default_font_name
Browse files Browse the repository at this point in the history
  • Loading branch information
westonganger committed Dec 20, 2024
1 parent c309a31 commit fa9c739
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Changelog

* `Unreleased` - [View Diff](https://github.com/cortiz/prawn-rails/compare/v1.5.0...master)
- Nothing yet
- [#55](https://github.com/cortiz/prawn-rails/pull/55) - Add config options `additional_fonts` and `default_font_name`

* `v1.5.0` - [View Diff](https://github.com/cortiz/prawn-rails/compare/v1.4.2...v1.5.0)
- [#52](https://github.com/cortiz/prawn-rails/pull/52) - Add active_support as a dependency and use the active_support lazy load hooks
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,21 @@ Add a `prawn-rails.rb` config to your Rails app under `config/initializers` like

```ruby
PrawnRails.config do |config|
# Prawn::Document options
config.page_layout = :portrait
config.page_size = "A4"
config.skip_page_creation = false

# PrawnRails options
config.additional_fonts = {
"some-custom-font" => {
normal: Rails.root.join('app/assets/fonts/print/some-custom-font.ttf'),
italic: Rails.root.join('app/assets/fonts/print/some-custom-font-italic.ttf'),
bold: Rails.root.join('app/assets/fonts/print/some-custom-font-bold.ttf'),
bold_italic: Rails.root.join('app/assets/fonts/print/some-custom-font-bold-italic.ttf'),
},
}
config.default_font_name = "some-custom-font"
end
```

Expand Down
2 changes: 2 additions & 0 deletions lib/prawn-rails/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def method_missing(method_name, *args)
page_layout: :portrait,
page_size: "A4",
skip_page_creation: false,
additional_fonts: nil,
default_font_name: nil,
)

def config(&block)
Expand Down
18 changes: 17 additions & 1 deletion lib/prawn-rails/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
module PrawnRails
class Document < Prawn::Document
def initialize(options = {})
options = PrawnRails.config.merge(options)
options = PrawnRails.config.except(:additional_fonts, :default_font_name).merge(options)

super(options)
end
Expand All @@ -16,5 +16,21 @@ def text(value, options = {})
# To circumvent this situation, we call to_s on value, and delegate action to actual Prawn::Document
super(value.to_s, options)
end

def start_new_page(options = {})
return_val = super

if state.page_count == 1
if PrawnRails.config.additional_fonts
font_families.update(PrawnRails.config.additional_fonts)
end

if PrawnRails.config.default_font_name
font(PrawnRails.config.default_font_name)
end
end

return_val
end
end
end
Binary file not shown.
14 changes: 14 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,17 @@

require 'pry'
require 'pdf/reader'

DEFAULT_CONFIG ||= PrawnRails.config.clone

def restore_default_config
PrawnRails.config do |config|
config.keys.each do |key|
config.delete(key)
end

DEFAULT_CONFIG.each do |k,v|
config[k] = v
end
end
end
30 changes: 15 additions & 15 deletions test/unit/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,18 @@

class ConfigTest < ActiveSupport::TestCase

def setup
@original_config = PrawnRails.config.clone
end

def teardown
PrawnRails.config do |config|
config.keys.each do |key|
config.delete(key)
end

@original_config.each do |k,v|
config[k] = v
end
end
restore_default_config
end

test "has a default config" do
assert_equal PrawnRails.config.symbolize_keys, {page_layout: :portrait, page_size: "A4", skip_page_creation: false}
assert_equal PrawnRails.config.symbolize_keys, {
page_layout: :portrait,
page_size: "A4",
skip_page_creation: false,
default_font_name: nil,
additional_fonts: nil,
}
end

test "config can be set with block syntax" do
Expand All @@ -28,7 +22,13 @@ def teardown
config.page_size = "A8"
end

assert_equal PrawnRails.config.symbolize_keys, {page_layout: :landscape, page_size: "A8", skip_page_creation: false}
assert_equal PrawnRails.config.symbolize_keys, {
page_layout: :landscape,
page_size: "A8",
skip_page_creation: false,
default_font_name: nil,
additional_fonts: nil,
}
end

test "allows reading any hash value via method syntax" do
Expand Down
39 changes: 39 additions & 0 deletions test/unit/rails_helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
class RailsHelperTest < ActiveSupport::TestCase
include PrawnRails::RailsHelper

def teardown
restore_default_config
end

test "matches .PDF extension regardless of case" do
["pDf", "pdf", "PDF"].each do |ext|
@filename = "test.#{ext}"
Expand All @@ -18,4 +22,39 @@ class RailsHelperTest < ActiveSupport::TestCase
end
end

test "config.default_font_name" do
font_name = "Arial"

PrawnRails.config.default_font_name = "Arial"

font_path = Rails.root.join("app/assets/stylesheets/fonts/Arial.ttf")

PrawnRails.config.additional_fonts = {
"Arial" => {
normal: font_path,
italic: font_path,
bold: font_path,
bold_italic: font_path,
},
}

prawn_document do |pdf|
assert_equal pdf.font.family, "Arial"

pdf.text "foo"
end

prawn_document(skip_page_creation: true) do |pdf|
assert_raises(Prawn::Errors::NotOnPage) do
pdf.font
end

pdf.start_new_page

assert_equal pdf.font.family, "Arial"

pdf.text "foo"
end
end

end

0 comments on commit fa9c739

Please sign in to comment.