Search code examples
postscriptskew

Centered shear/skew in postscript


I'm trying to apply skewing to an image in postscript. I have successfully done it with:

[1 -0.5 -0.5 1 0 0] concat

Where both -0.5 refers to a skew of 22.5 degrees on the vertical and horizontal axis. The problem is that the skewing is not centered, the center of the image is moved so my image doesn't stay on the same position after applying the skew.

How can I apply skewing while maintaining the position? I tried translating the image before/after skewing but I can't find how much I should translate the image so it always stays in the same position.


Solution

  • Ok, I finally find the way to do it. First of all, position the element as usual with translate:

    1135 568 translate
    

    Then, skew the only on one axis X or Y, not both at the same time. The number in the matrix is tan(skew_angle) so for example, for a 20 degree angle on the X axis:

    [1 0 0.3639 1 0 0] concat
    

    After this, translate the image because now the width of the bounding box of the image is bigger, we can calculate this additional width as additional_width = abs(object_height * tan(skew_angle)). So now we translate half this width:

    {additional_width/2} 0 translate
    

    Now we do the skew on Y on the same way:

    [1 0.3639 0 1 0 0] concat
    

    And calculate the additional height as additional_height = abs(object_width * tan(skew_angle))

    0 {additional_height/2} translate
    

    On this way, after applying the skew the object center stays in the same position.