Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String rotation #47

Open
c4da opened this issue Jan 7, 2023 · 3 comments
Open

String rotation #47

c4da opened this issue Jan 7, 2023 · 3 comments

Comments

@c4da
Copy link

c4da commented Jan 7, 2023

It seems that string rotation is not working for 0.42.

        AffineTransform affineTransform = horizontalFont.getTransform();
        affineTransform.rotate(Math.toRadians(-90), 0, 0);
        Font rotatedFont = horizontalFont.deriveFont(affineTransform);
        gc.setFont(rotatedFont);
        gc.drawString(text, x, y);

Is there some workaround or am I doing something wrong?
I have fonts registered, and I have checked that Im drawing it as a string.
Maybe if I could force it to draw rotated text using shapes?

rototor added a commit that referenced this issue Jan 8, 2023
@rototor
Copy link
Owner

rototor commented Jan 8, 2023

You can always force drawing text with shape by using the default TextDrawer, or setting it to PdfBoxGraphics2DFontTextDrawer, i.e.

                gfx.setFontTextDrawer(new PdfBoxGraphics2DFontTextDrawer());

But you could instead just use a normal transform on the Graphics2D to rotate the text:

                AffineTransform saveTF = gfx.getTransform();
                AffineTransform at = AffineTransform.getTranslateInstance(100, 150);
                at.rotate(Math.toRadians(-90), 0, 0);
                gfx.setColor(Color.RED);
                gfx.setFont(horizontalFont);
                gfx.setTransform(at);
                gfx.drawString("Some sample text", 0, 0);
                gfx.setTransform(saveTF);

I.e. you do a:

  • save the current (transform) state
  • move to the place where the text should be drawn
  • now rotate
  • draw the text
  • restore the current (transform) state

But I'll try to look into this. Maybe this can be made work with font files.

@c4da
Copy link
Author

c4da commented Jan 8, 2023

Thanks for the answer, I did just what you have suggested, which was rotating the gc -90 deg and back.

I have tried setting PdfBoxGraphics2DFontTextDrawer which also worked, it may be good to access the used FontTextDrawer, sou you could switch them back and forth as you go.

Is there a reason why you havent accesed the get method for used FontTextDrawer? Im just curious.

BTW, I really appreciate that you have answered on sunday!

@rototor
Copy link
Owner

rototor commented Jan 8, 2023

The gfx has currently no getFontTextDrawer() method, and I'm not sure if this is method is really needed. I got your basic sample working with fonts, I'm just need to debug the case with attributed text, thats not really working yet.

Well on sundays it's more likely that I've time for this project ;-)

rototor added a commit that referenced this issue Jan 8, 2023
But this only works for the simple case of a transformed font at the moment...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants