forked from prawnpdf/prawn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Track transformations and apply to gradients' matrices
PDF gradients/patterns take coordinates in the coordinate space of the document, not the "user space", so if you perform a scale/rotate/translate and then paint a gradient inside, it doesn't render correctly. This commit tracks transformations applied to the document, and multiplies the gradient matrix with this tracked transformation matrix so that the gradient appears in the correct place in the document.
- Loading branch information
Roger Nesbitt
committed
Jul 19, 2015
1 parent
afc263b
commit c255966
Showing
8 changed files
with
165 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# encoding: utf-8 | ||
# | ||
# transformation_stack.rb : Stores the transformations that have been applied to the document | ||
# | ||
# Copyright 2015, Roger Nesbitt. All Rights Reserved. | ||
# | ||
# This is free software. Please see the LICENSE and COPYING files for details. | ||
# | ||
|
||
require 'matrix' | ||
|
||
module Prawn | ||
module TransformationStack | ||
def add_to_transformation_stack(a, b, c, d, e, f) | ||
@transformation_stack ||= [[]] | ||
@transformation_stack.last.push([a, b, c, d, e, f].map { |x| x.to_f }) | ||
end | ||
|
||
def save_transformation_stack | ||
@transformation_stack ||= [[]] | ||
@transformation_stack.push(@transformation_stack.last.dup) | ||
end | ||
|
||
def restore_transformation_stack | ||
@transformation_stack.pop if @transformation_stack | ||
end | ||
|
||
def current_transformation_matrix_with_translation(x = 0, y = 0) | ||
transformations = (@transformation_stack || [[]]).last | ||
|
||
matrix = Matrix.identity(3) | ||
|
||
transformations.each do |a, b, c, d, e, f| | ||
matrix *= Matrix[[a, c, e], [b, d, f], [0, 0, 1]] | ||
end | ||
|
||
matrix *= Matrix[[1, 0, x], [0, 1, y], [0, 0, 1]] | ||
|
||
matrix.to_a[0..1].transpose.flatten | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# encoding: utf-8 | ||
|
||
require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper") | ||
|
||
describe Prawn::TransformationStack do | ||
before { create_pdf } | ||
before { pdf.add_to_transformation_stack(2, 0, 0, 2, 100, 100) } | ||
|
||
let(:pdf) { @pdf } | ||
let(:stack) { @pdf.instance_variable_get(:@transformation_stack) } | ||
|
||
describe "#add_to_transformation_stack" do | ||
it "creates and adds to the stack" do | ||
pdf.add_to_transformation_stack(1, 0, 0, 1, 20, 20) | ||
|
||
expect(stack).to eq [[[2, 0, 0, 2, 100, 100], [1, 0, 0, 1, 20, 20]]] | ||
end | ||
|
||
it "adds to the last stack" do | ||
pdf.save_transformation_stack | ||
pdf.add_to_transformation_stack(1, 0, 0, 1, 20, 20) | ||
|
||
expect(stack).to eq [ | ||
[[2, 0, 0, 2, 100, 100]], | ||
[[2, 0, 0, 2, 100, 100], [1, 0, 0, 1, 20, 20]] | ||
] | ||
end | ||
end | ||
|
||
describe "#save_transformation_stack" do | ||
it "clones the last stack" do | ||
pdf.save_transformation_stack | ||
|
||
expect(stack.length).to eq 2 | ||
expect(stack.first).to eq stack.last | ||
expect(stack.first).to_not be stack.last | ||
end | ||
end | ||
|
||
describe "#restore_transformation_stack" do | ||
it "pops off the last stack" do | ||
pdf.save_transformation_stack | ||
pdf.add_to_transformation_stack(1, 0, 0, 1, 20, 20) | ||
pdf.restore_transformation_stack | ||
|
||
expect(stack).to eq [[[2, 0, 0, 2, 100, 100]]] | ||
end | ||
end | ||
|
||
describe "current_transformation_matrix_with_translation" do | ||
before do | ||
pdf.add_to_transformation_stack(1, 0, 0, 1, 20, 20) | ||
end | ||
|
||
it "calculates the last transformation" do | ||
expect(pdf.current_transformation_matrix_with_translation).to eq [2, 0, 0, 2, 140, 140] | ||
end | ||
|
||
it "adds the supplied x and y coordinates to the transformation stack" do | ||
expect(pdf.current_transformation_matrix_with_translation(15, 15)).to eq [2, 0, 0, 2, 170, 170] | ||
end | ||
end | ||
end |