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

Change text box to return remaining text as UTF-8, improve Win1252 handling internally, raise errors or warnings rather than silently replacing invalid glyphs #793

Closed
wants to merge 7 commits into from
4 changes: 4 additions & 0 deletions lib/prawn/font/afm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ def normalize_encoding(text)
"Arguments to text methods must be UTF-8 encoded"
end

def to_utf8(text)
text.bytes.pack("U*")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: This may not actually be a correct way of converting back into UTF-8. Needs closer investigation.

end

# Returns the number of characters in +str+ (a WinAnsi-encoded string).
#
def character_count(str)
Expand Down
4 changes: 4 additions & 0 deletions lib/prawn/font/ttf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ def normalize_encoding(text)
end
end

def to_utf8(text)
text.encode("UTF-8")
end

def glyph_present?(char)
code = char.codepoints.first
cmap[code] > 0
Expand Down
4 changes: 3 additions & 1 deletion lib/prawn/text/formatted/box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ def render(flags={})
end
end

unprinted_text
unprinted_text.map do |e|
e.merge(:text => @document.font.to_utf8(e[:text]))
end
end

# The width available at this point in the box
Expand Down
14 changes: 2 additions & 12 deletions spec/text_box_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -676,19 +676,9 @@
end

describe "when using an AFM font" do
it "unprinted text should be in WinAnsi encoding" do
remaining_text = @text_box.render
remaining_text.should == @pdf.font.normalize_encoding(@text)
end
it "subsequent calls to Text::Box must include the" +
" :skip_encoding => true option" do
it "unprinted text should be in UTF-8 encoding" do
remaining_text = @text_box.render
lambda {
@pdf.text_box(remaining_text, :document => @pdf)
}.should raise_error(Prawn::Errors::IncompatibleStringEncoding)

@pdf.text_box(remaining_text, :skip_encoding => true,
:document => @pdf)
remaining_text.should == @text
end
end
end
Expand Down