Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don’t set font on fragments without font when processing fallback #263 #743

Merged
merged 1 commit into from
Jul 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions lib/prawn/text/formatted/box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -390,33 +390,37 @@ def analyze_glyphs_for_fallback_font_support(hash)

original_font = @document.font.family
fragment_font = hash[:font] || original_font
@document.font(fragment_font)

fallback_fonts = @fallback_fonts.dup
# always default back to the current font if the glyph is missing from
# all fonts
fallback_fonts << fragment_font

hash[:text].each_char do |char|
@document.font(fragment_font)
font_glyph_pairs << [find_font_for_this_glyph(char,
@document.font.family,
fallback_fonts.dup),
char]
@document.save_font do
hash[:text].each_char do |char|
font_glyph_pairs << [find_font_for_this_glyph(char,
fragment_font,
fallback_fonts.dup),
char]
end
end

@document.font(original_font)
# Don't add a :font to fragments if it wasn't there originally
if hash[:font].nil?
font_glyph_pairs.each do |pair|
pair[0] = nil if pair[0] == original_font
end
end

form_fragments_from_like_font_glyph_pairs(font_glyph_pairs, hash)
end

def find_font_for_this_glyph(char, current_font, fallback_fonts)
@document.font(current_font)
if fallback_fonts.length == 0 || @document.font.glyph_present?(char)
current_font
else
current_font = fallback_fonts.shift
@document.font(current_font)
find_font_for_this_glyph(char, @document.font.family, fallback_fonts)
find_font_for_this_glyph(char, fallback_fonts.shift, fallback_fonts)
end
end

Expand All @@ -426,11 +430,11 @@ def form_fragments_from_like_font_glyph_pairs(font_glyph_pairs, hash)
current_font = nil

font_glyph_pairs.each do |font, char|
if font != current_font
if font != current_font || fragments.count == 0
current_font = font
fragment = hash.dup
fragment[:text] = char
fragment[:font] = font
fragment[:font] = font unless font.nil?
fragments << fragment
else
fragment[:text] += char
Expand Down
19 changes: 19 additions & 0 deletions spec/text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,23 @@ def add_unicode_fonts(pdf)
})
pdf.fallback_fonts = ["dejavu"]
end

describe "fallback_fonts" do
it "should preserve font style" do
create_pdf

@pdf.fallback_fonts ["Helvetica"]
@pdf.font "Times-Roman", :style => :italic do
@pdf.text "hello"
end

text = PDF::Inspector::Text.analyze(@pdf.render)
fonts_used = text.font_settings.map { |e| e[:name] }

fonts_used.length.should == 1
fonts_used[0].should == :"Times-Italic"
text.strings[0].should == "hello"
end
end

end