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

text VerticalAlignment does not work correctly #88

Closed
matyasforian opened this issue Sep 15, 2020 · 2 comments
Closed

text VerticalAlignment does not work correctly #88

matyasforian opened this issue Sep 15, 2020 · 2 comments

Comments

@matyasforian
Copy link

Description

VerticalAlignment.Top places the text a bit below the top, VerticalAlignment.CENTER places it a bit above the top, VerticalAlignment.BOTTOM does not render anything.

Steps to Reproduce

var _fontRobotoMono = Fonts.Install(PerlinUtils.LoadEmbeddedResourceAsString("RobotoMono-Regular.ttf"));
var _font = _fontRobotoMono .CreateFont(20);
var _image = new Image<Rgba32>(50,60);
_image.Mutate(ctx =>
{
                ctx.BackgroundColor(Color.Brown);
                var to = new TextOptions();
                to.VerticalAlignment = VerticalAlignment.Top;
                var opt = new TextGraphicsOptions(new GraphicsOptions(), to);
                ctx.DrawText(opt, "TOP" ,_font, Color.White, new PointF(0, 0));
                to.VerticalAlignment = VerticalAlignment.Center;
                ctx.DrawText(opt, "CEN" ,_font, Color.White, new PointF(0, 0));
                to.VerticalAlignment = VerticalAlignment.Bottom;
                ctx.DrawText(opt, "BOT" ,_font, Color.White, new PointF(0, 0));
});
_image.SaveAsPng("eerrff.png");

the resulting image:
eerrff

System Configuration

  • ImageSharp.Drawing version: 1.0.0-beta0010
  • Other ImageSharp packages and versions: ImageSharp 1.0.1
  • Environment (Operating system, version and so on):
  • .NET Framework version: .NET Core 3.0
  • Additional information: latest Windows 10
@tocsoft
Copy link
Member

tocsoft commented Sep 15, 2020

You are rendering them relative the the top left corner so what your code is saying is, align the text so the bottom is at the bottom most point I have told you about and wrap going upwards from the bottom to the top, which being ends up positioning you off the top of the canvas. so for bottom to work as you expect correctly you need to position at (0, image.Height).
As for center it moves up and down from the point you provide, so half up and half down.

As you will be able to see from this version where I've moved the drawing origin for each or your calls then you will see it renders correctly.

var _fontRobotoMono = Fonts.Install(PerlinUtils.LoadEmbeddedResourceAsString("RobotoMono-Regular.ttf"));
var _font = _fontRobotoMono .CreateFont(20);
var _image = new Image<Rgba32>(50,60);
_image.Mutate(ctx =>
{
                ctx.BackgroundColor(Color.Brown);
                var to = new TextOptions();
                to.VerticalAlignment = VerticalAlignment.Top;
                var opt = new TextGraphicsOptions(new GraphicsOptions(), to);
                ctx.DrawText(opt, "TOP" ,_font, Color.White, new PointF(0, 0)); // top of image
                to.VerticalAlignment = VerticalAlignment.Center;
                ctx.DrawText(opt, "CEN" ,_font, Color.White, new PointF(0, 30)); // half way down the image
                to.VerticalAlignment = VerticalAlignment.Bottom;
                ctx.DrawText(opt, "BOT" ,_font, Color.White, new PointF(0, 60)); // bottom of the image
});
_image.SaveAsPng("eerrff.png");

@matyasforian
Copy link
Author

oh I see, so the VerticalAlignment property is more like setting the pivot's vertical coordinates. Perhaps it'd be good to rename it to something like PivotVerticalAlignment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants