diff --git a/backend.go b/backend.go index 39d99b8..0c96278 100644 --- a/backend.go +++ b/backend.go @@ -69,7 +69,7 @@ func (b *Backend) Begin(width, height int) error { // Apply Y-flip transform to convert from top-left to bottom-left origin // We need to translate by height and flip Y axis - flipTransform := creator.Translate(0, b.height).Then(creator.Scale(1, -1)) + flipTransform := creator.Scale(1, -1).Then(creator.Translate(0, b.height)) b.surface.PushTransform(flipTransform) return nil diff --git a/backend_test.go b/backend_test.go index e97114a..8b3499a 100644 --- a/backend_test.go +++ b/backend_test.go @@ -6,6 +6,7 @@ import ( "path/filepath" "testing" + "github.com/coregx/gxpdf/creator" "github.com/gogpu/gg" "github.com/gogpu/gg/recording" ) @@ -56,6 +57,43 @@ func TestBackendLifecycle(t *testing.T) { } } +func TestBackendYFlipTransform(t *testing.T) { + const height = 240 + + backend := NewBackend() + if err := backend.Begin(320, height); err != nil { + t.Fatalf("Begin failed: %v", err) + } + + want := creator.Scale(1, -1).Then(creator.Translate(0, height)) + if got := backend.surface.CurrentTransform(); got != want { + t.Fatalf("Y-flip transform = %#v, want %#v", got, want) + } + + if err := backend.End(); err != nil { + t.Fatalf("End failed: %v", err) + } +} + +func TestDocumentYFlipTransform(t *testing.T) { + const height = 240 + + doc := NewDocument() + backend, ok := doc.NewPage(320, height).(*pageBackend) + if !ok { + t.Fatal("Document.NewPage returned an unexpected backend type") + } + + want := creator.Scale(1, -1).Then(creator.Translate(0, height)) + if got := backend.surface.CurrentTransform(); got != want { + t.Fatalf("Y-flip transform = %#v, want %#v", got, want) + } + + if err := doc.Finish(); err != nil { + t.Fatalf("Finish failed: %v", err) + } +} + func TestBackendSaveRestore(t *testing.T) { backend := NewBackend() err := backend.Begin(800, 600) diff --git a/document.go b/document.go index 5c587ae..3cae1e4 100644 --- a/document.go +++ b/document.go @@ -74,7 +74,7 @@ func (d *Document) NewPage(width, height int) recording.Backend { pb.currentTransform = recording.Identity() // Apply Y-flip transform - flipTransform := creator.Translate(0, float64(height)).Then(creator.Scale(1, -1)) + flipTransform := creator.Scale(1, -1).Then(creator.Translate(0, float64(height))) pb.surface.PushTransform(flipTransform) d.pages = append(d.pages, pb)