Align left, center, right and justify text

Now we can measure text it is straightforward to align text to right.

Javascript Editor

As it is to align center

Javascript Editor

But to justify, we need a new operator that can modify the width o space. It is widthshow which adds an x-y Offset after each occurence of a chosen character.

Javascript Editor

But widthshow does not calculate the amount of offset we have to apply. For this, we need to calculate in PostScript width, deduct this from the target width and divide it by the number of spaces. We need the PostScript operators loop, exit amd search for it.

Javascript Editor

This seems to work.

Javascript Editor

But what happens in a rotated context? The text is not aligned

Javascript Editor

It looks like some older operators were not correct. Stringdwidth should run from the current point and currentpoint should give back the user space coordinates. We add context.itransform() to it. Run the hotfix below and render the above code again.

Javascript Editor

The codebase has now 1948 lines ps20240823.js

My Journey to PostScript