Skip to content

Commit

Permalink
Merge pull request #103 from herrmann-intl/master
Browse files Browse the repository at this point in the history
Prevent from overwriting existing font-families
  • Loading branch information
mogest committed Jul 25, 2018
2 parents 81fe36d + b733169 commit f203ad7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/prawn/svg/font_registry.rb
Expand Up @@ -37,8 +37,9 @@ def load(family, weight = nil, style = nil)
def merge_external_fonts
if @font_case_mapping.nil?
self.class.load_external_fonts unless self.class.external_font_families
@font_families.merge!(self.class.external_font_families)

@font_families.merge!(self.class.external_font_families) do |key, v1, v2|
v1
end
@font_case_mapping = @font_families.keys.each.with_object({}) do |key, result|
result[key.downcase] = key
end
Expand Down
30 changes: 30 additions & 0 deletions spec/prawn/svg/font_registry_spec.rb
Expand Up @@ -32,6 +32,36 @@
end
end

describe "#installed_fonts" do
let(:ttf) { instance_double(Prawn::SVG::TTF, family: "Awesome Font", subfamily: "Italic") }
let(:ttf2) { instance_double(Prawn::SVG::TTF, family: "Awesome Font", subfamily: "Regular") }
before { Prawn::SVG::FontRegistry.external_font_families.clear }

let(:pdf) do
doc = Prawn::Document.new
doc.font_families.update({
"Awesome Font" => {:italic => "second.ttf", :normal => "file.ttf"}
})
doc
end

let(:font_registry) { Prawn::SVG::FontRegistry.new(pdf.font_families) }

it "does not override existing entries in pdf when loading external fonts" do
expect(Prawn::SVG::FontRegistry).to receive(:font_path).and_return(["x"])
expect(Dir).to receive(:[]).with("x/**/*").and_return(["file.ttf", "second.ttf"])
expect(Prawn::SVG::TTF).to receive(:new).with("file.ttf").and_return(ttf)
expect(Prawn::SVG::TTF).to receive(:new).with("second.ttf").and_return(ttf2)
expect(File).to receive(:file?).at_least(:once).and_return(true)

Prawn::SVG::FontRegistry.load_external_fonts
font_registry.installed_fonts

existing_font = font_registry.installed_fonts["Awesome Font"]
expect(existing_font).to eq(:italic => "second.ttf",:normal => "file.ttf")
end
end

describe "::load_external_fonts" do
let(:ttf) { instance_double(Prawn::SVG::TTF, family: "Awesome Font", subfamily: "Italic") }
let(:ttf2) { instance_double(Prawn::SVG::TTF, family: "Awesome Font", subfamily: "Regular") }
Expand Down

0 comments on commit f203ad7

Please sign in to comment.