-
Notifications
You must be signed in to change notification settings - Fork 45
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
Vertical Alignment Off #23
Comments
You are right, these calculations are off ! This was made a long time ago and I am actually surprised it has been used for so long without anyone noticing. I can't write the code right now so if you want to give it a go, please do. It should be something along those lines (to include multi line text vertical centering) :
There is probably a check to be made in the code that the bad calculation isn't used somewhere that would mess up the display Thanks to anyone who would tackle this issue, please send a PR including your own name in a Contributors section of the README (to be created) |
I still think you need to subtract these two, or at least take the absolute value of each before adding them. I ran your code through a debugger and the descent is negative which means its decreasing the total height if adding them together. Your x-center is being calculated correctly, but for my code I am just manually passing in a Y value now instead of 'center'. I know my formula isn't accurate, but for my situation it gets me in the ballpark which is good enough and lets me move on with my project:
Dividing the font size by 1.2 for my situation solves the problem for my specific case, but I know that is likely an arbitrary value depending on the font. I think the 1.2 represents the distance between the ascent and top, and the descent and bottom since if you don't take that white space area into the calculation it tends to make it look centered to high or too low. I tried numerous font sizes and it is perfectly centering on the y-axis for me. I am using Open Sans. |
Instead of jumping through those hoops to calculate the dimensions, couldn't you just figure that from a |
What about the possibility of parsing the path itself? I found this from part of the meyfa/php-svg library: https://github.com/meyfa/php-svg/blob/main/src/Rasterization/Path/PathParser.php Feed it the 'd' attribute of the 'path' element. I added something like this to it to get maximums, minimums and relative width and height. I'm assuming the min values represent the top left for translations?:
I'm still toying with it, but you can do things like
(such attributes can be passed in to the addText, but you can't parse the path until it's generated and the svg property is protected. but presumably, something similar could be done inside the addText itself after the path is generated) |
@treii28 I am afraid this approach wouldn't work because for bezier curves (widely used in fonts) the points coordinates aren't always the max X/Y of the line described @Ambient-Impact that makes sense but you will be getting the vertical alignment based on the min/max height, and depending on the characters, on a multiline text for instance you'd get weird results. ie.
each line would be aligned differently and the end result would look off |
I am very interested in the outcome of this issue or if anyone has any tips about it. |
I am trying to vertically align text, but its definitely off. When I look at your textDimensions method I am a little confused on exactly what this calculation does:
Is this really getting the line height of the text? The ascent usually comes in as a positive number, and the descent a negative number. 1 + -1 = 0, how is this measuring distance? Could this be related to to the vertical centering issue?
The text was updated successfully, but these errors were encountered: