Skip to content

Commit

Permalink
make TransformationMatrix#horizontal_displacement_multiply! public
Browse files Browse the repository at this point in the history
* allows callers to use this method directly if they know it is
  applicable, avoid the processing cost of the multiply! method
  • Loading branch information
yob committed Nov 21, 2012
1 parent a788714 commit 8628929
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
9 changes: 3 additions & 6 deletions lib/pdf/reader/page_state.rb
Expand Up @@ -323,14 +323,11 @@ def process_glyph_displacement(w0, tj, word_boundary)
# TODO: I'm pretty sure that tx shouldn't need to be divided by
# ctm[0] here, but this gets my tests green and I'm out of
# ideas for now
# TODO: support ty > 0
if ctm.a == 1
@text_matrix.multiply!(1, 0,
0, 1,
tx, ty)
@text_matrix.horizontal_displacement_multiply!(tx)
else
@text_matrix.multiply!(1, 0,
0, 1,
tx/ctm.a, ty)
@text_matrix.horizontal_displacement_multiply!(tx/ctm.a)
end
@font_size = @text_rendering_matrix = nil # invalidate cached value
end
Expand Down
14 changes: 7 additions & 7 deletions lib/pdf/reader/transformation_matrix.rb
Expand Up @@ -63,7 +63,7 @@ def multiply!(a,b=nil,c=nil, d=nil,e=nil,f=nil)
@f = f
elsif a == 1 && b == 0 && c == 0 && d == 1 && f == 0
# the other matrix is a horizontal displacement
horizontal_displacement_multiply!(a,b,c,d,e,f)
horizontal_displacement_multiply!(e)
elsif @a == 1 && @b == 0 && @c == 0 && @d == 1 && @f == 0
# I'm a horizontal displacement
horizontal_displacement_multiply_reversed!(a,b,c,d,e,f)
Expand All @@ -79,21 +79,21 @@ def multiply!(a,b=nil,c=nil, d=nil,e=nil,f=nil)
self
end

private

# Optimised method for when the second matrix in the calculation is
# a simple horizontal displacement.
#
# Like this:
#
# [ 1 2 0 ] [ 1 0 0 ]
# [ 3 4 0 ] x [ 0 1 0 ]
# [ 5 6 1 ] [ 5 0 1 ]
# [ 1 2 0 ] [ 1 0 0 ]
# [ 3 4 0 ] x [ 0 1 0 ]
# [ 5 6 1 ] [ e2 0 1 ]
#
def horizontal_displacement_multiply!(a2,b2,c2, d2,e2,f2)
def horizontal_displacement_multiply!(e2)
@e = @e + e2
end

private

# Optimised method for when the first matrix in the calculation is
# a simple horizontal displacement.
#
Expand Down
19 changes: 18 additions & 1 deletion spec/transformation_matrix_spec.rb
Expand Up @@ -5,7 +5,7 @@
# The results in these specs were generated at
# http://www.calcul.com/matrix-multiply-3x3-3x3 to ensure correctness.

describe PDF::Reader::TransformationMatrix, "#multiply_with_an_object!" do
describe PDF::Reader::TransformationMatrix, "#multiply!" do
class PDF::Reader::TransformationMatrix
# a helper method for tests
def multiply_with_an_object!(m2)
Expand Down Expand Up @@ -167,3 +167,20 @@ def multiply_with_an_object!(m2)
end

end

describe PDF::Reader::TransformationMatrix, "#horizontal_displacement_multiply!" do
context "with [2,3,0 4,5,0 6 7 1]" do
let(:matrix_one) { PDF::Reader::TransformationMatrix.new(2,3,4,5,6,7)}

context "and a horizontal displacement" do
let(:displacement) { 10 }

it "should set the new matrix values" do
matrix_one.horizontal_displacement_multiply!(displacement)

matrix_one.to_a.should == [2,3,0, 4,5,0, 16,7,1]
end

end
end
end

0 comments on commit 8628929

Please sign in to comment.