[pdf4st] AW: AW: AW: Coordinate transformation

  • From: "Christian Haider" <Christian.Haider@xxxxxxxxxxxxxxxxxxxxxxx>
  • To: <pdf4st@xxxxxxxxxxxxx>
  • Date: Fri, 20 Apr 2012 15:42:21 +0200

(Originally submitted by ChristianHaider on Thu, 2011-12-01 11:38.) 

he he :-). That's a classic - and happens to everybody (including me)
when starting to use coordinate transformations, I guess. 

It is there, but outside (below) your rectangle... Thats why you need
the translation. The following shows what happens: 

page := Graphics.PDF.Page newInBounds: (0 @ -100 corner: 165 @ 100)
colorspace: DeviceRGB new render: [:renderer |
  renderer concat: (Matrix scale: 1 @ -1).
  renderer moveTo: 0 @ 0<SEMI> lineTo: 165 @ 0<SEMI> stroke.  "draws a
line at y = 0"
  renderer textObjectDo: [
    renderer setFont: #Helvetica size: 8.
    renderer add: (Graphics.PDF.TextPositioningOperator Td operands:
#(10 10)).
    renderer showString: 'Text at #(10 10)']].
  page saveAndShowAs: 'transformation_text.pdf'.

Unfortunately, now the text is upside down... That can be fixed by using
another matrix for the text: 

page := Graphics.PDF.Page newInBounds: (0 @ -100 corner: 165 @ 100)
colorspace: DeviceRGB new render: [:renderer |
  renderer concat: (Matrix scale: 1 @ -1).
  renderer moveTo: 0 @ 0.
  renderer lineTo: 165 @ 0.
  rendere stroke.  "draws a line at y = 0"
  renderer textObjectDo: [
    renderer setFont: #Helvetica size: 8.
    renderer textMatrix: ((Matrix scale: 1 @ -1) translate: 10 @ 10).
    renderer showString: 'Text at #(10 10)']].
  page saveAndShowAs: 'transformation_text.pdf'.

So, what you should do is to invert the coordinate system, translate so
that the origin is at the top of the page and then draw the text again
inverted... 

 

 

Von: pdf4st-bounce@xxxxxxxxxxxxx [mailto:pdf4st-bounce@xxxxxxxxxxxxx] Im
Auftrag von Christian Haider
Gesendet: Freitag, 20. April 2012 15:31
An: pdf4st@xxxxxxxxxxxxx
Betreff: [pdf4st] AW: AW: Coordinate transformation 

 

(Originally submitted by bobn on Wed, 2011-11-30 22:06.) 

I tried the following code in a workspace. The firs file shows the text
as expected. The second PDF file is empty. 

        page := Graphics.PDF.Page newInBounds: (0 @ 0 corner: 165 @ 100)
colorspace: DeviceRGB new render: [:renderer |
                renderer textObjectDo: [
                        renderer setFont: #Helvetica size: 8.
                        renderer add:
(Graphics.PDF.TextPositioningOperator Td operands: #(10 10)).
                        renderer showString: 'Text at #(10 10)']].
        page saveAndShowAs: 'no_transformation_text.pdf'.


        page := Graphics.PDF.Page newInBounds: (0 @ 0 corner: 165 @ 100)
colorspace: DeviceRGB new render: [:renderer |
                renderer concat: (Matrix scale: 1 @ -1).
                renderer textObjectDo: [
                        renderer setFont: #Helvetica size: 8.
                        renderer add:
(Graphics.PDF.TextPositioningOperator Td operands: #(10 10)).
                        renderer showString: 'Text at #(10 10)']].
        page saveAndShowAs: 'transformation_text.pdf'.

The print string of both pages is... 

<<      /Type /Page

        /MediaBox [0 0 165 100]

        /TrimBox [0 0 165 100]

        /Resources <<          /Font <<                       /F1 5 0 R
>>      >>

        /Contents 6 0 R

        /Parent 3 0 R  >>

What am I missing to keep the text from rendering? 

 

Von: pdf4st-bounce@xxxxxxxxxxxxx [mailto:pdf4st-bounce@xxxxxxxxxxxxx] Im
Auftrag von Christian Haider
Gesendet: Freitag, 20. April 2012 15:16
An: pdf4st@xxxxxxxxxxxxx
Betreff: [pdf4st] AW: Coordinate transformation 

 

(Originally submitted by ChristianHaider on Wed, 2011-11-30 07:14.) 

I defined a class Matrix (a subclass of PDFArray) which can be given to
the renderer: 

page := Page newInBounds: aRectangle colorspace: aColorspace render:
[:renderer |
  renderer concat: ((Matrix scale: 1 @ -1) translate: 0 @ 819).
  "do your drawing in the new coordinate system"].

which produces 

1 0 0 -1 0 819 cm

#concat: multiplies the matrix of the current graphics state with the
new matrix. 

A Matrix can be created with 

*       #scale: aPoint 
*       #translate: aPoint 
*       #rotate: aNumber (#rotateDegrees: aNumber) or 
*       #identity 

and an existing Matrix returns a modified copy with 

*       #scale: aPoint 
*       #translate: aPoint 
*       #rotate: aNumber (#rotateDegrees: aNumber) and 
*       #concat: aMatrix. 

So, there are many ways to express the example above: 

renderer concat: ((Matrix identity scale: 1 @ -1) translate: 0 @ 819).

or 

renderer concat: ((Matrix scale: 1 @ -1) concat: (Matrix translate: 0 @
819)).

or 

renderer concat: (Matrix scale: 1 @ -1).
renderer concat: (Matrix translate: 0 @ 819).

Remember that matrix concatenation is not commutative (the order is
important): 

(((Matrix rotateDegrees: 45) scale: 10 @ 5) translate: 50 @ 100)

is different from 

(((Matrix translate: 50 @ 100) rotateDegrees: 45) scale: 10 @ 5)

 

 

Von: pdf4st-bounce@xxxxxxxxxxxxx [mailto:pdf4st-bounce@xxxxxxxxxxxxx] Im
Auftrag von Christian Haider
Gesendet: Freitag, 20. April 2012 13:01
An: pdf4st@xxxxxxxxxxxxx
Betreff: [pdf4st] Coordinate transformation 

 

(Originally submitted by bobn on Tue, 2011-11-29 21:49. Moved as new
Topic.) 

I'm reading through the PDF 1.7 spec '8.3 Coordinate Systems'. The
description makes sense, but I do not see how to specify the coordinate
transformation. It's probably self-evident, but I'm still in basic
learning mode. 

Could you please provide a Smalltalk example? 

Thanks, Bob

Other related posts:

  • » [pdf4st] AW: AW: AW: Coordinate transformation - Christian Haider