-
Notifications
You must be signed in to change notification settings - Fork 2.2k
rendlay.cpp: Improve accuracy and performance calculating text aspect ratio. #14550
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
Conversation
… ratio,
Also adds another text alignment option to stretch the text to fill its bounds horizontally.
The current code calculating the aspect ratio for text involves looping, calculating the width of text for a variety of decreasing aspect ratios until the text's width is less than the available bounds. However, this string width calculation performs the same loop over the text each time, finally multiplying by the candidate aspect ratio. That text width calculation thus really only needs to be done once. Further, instead of trying different aspect ratios, the ratio can simply be calculated directly by dividing the width of the bounds by the string's width. This also calculates a more accurate aspect ratio, rather than always resulting in an aspect ratio of (0.95)^n.
For example, when trying to fit a 101-pixel wide text into a 100-pixel wide space, the current code would result in an aspect ratio of 0.95, making the text 96 pixels wide, leaving 4 pixels unused; the new code will instead calculate the aspect ratio as 100/101 == 0.9900990099... , making the text use the full 100 available pixels.
This in turn allows us to easily calculate the ratio also if we want to not just schrink but also stretch the text to fill the available space, so we add that as another text alignment option, number 3 (three).
This PR also demonstrates this in the VFX family of layouts, where on the default Full view, the text "MUSIC PRODUCTION SYNTHESIZER" ("DYNAMIC COMPONENT SYNTHESIZER" on the VFX), the logo-like text "ensoniq", and the keyboard-specific markers "VFX", "VFX-SD", "SD-1" and "3 2 V O I C E" can now be made to fill their available space horizontally as they should, making it all look that much more like the real thing.
|
Nice. Edit: Nevermind, I see it's an align="3" option which is new. Ok this solves the no-stretch issue. btw, in your example case SD-1 logo thing, the way to do that is remove -1, place SD and place the 1 separately further over. The triangle can be done with a simple svg path that even AI can write in 2 seconds :-) |
The stretching is absolutely not applied automatically - it has to be explicitly requested by specifying So, no, this will not break any existing layouts.
I'm not trying to recreate the actual SD-1 logo, because that would not be accepted, per this discussion on the layout for the ESQ-1. I am simply placing text in the corresponding place, specifically the name by which each keyboard is commonly known; and I want that text to take approximately the same amount of space as the log would. That's all. (If I did want to put something there that looks a lot closer to the real logos, I have significantly better options available - they just won't be sent for inclusion in MAME because they would be rejected.) |
|
Thank you! |
|
I'd like to suggest that we could try to figure out a way to automatically calculate the equivalent bound values for text that is affected, so that automatic update of the layouts can be performed to preserve the original rendering output. |
|
Alternatively, the old method could be restored and possibly the new approach activated only when a given .lay file uses a version number bumped up from 2 to 3. And then authors would use the new version whenever they work on new layouts or when they manually update the existing ones to render properly. |
|
I can confirm clipping, @cbrunschen can you fix it? Here's a simple test case
|
|
Thanks @cbrunschen for the extremely quick fix! This is now looking great ;-) |
|
Sorry for the silly mistake - thank you for reporting the issue and @happppp for help with the fix! |



Also adds another text alignment option to stretch the text to fill its bounds horizontally.
The current code calculating the aspect ratio for text involves looping, calculating the width of text for a variety of decreasing aspect ratios until the text's width is less than the available bounds. However, this string width calculation performs the same loop over the text each time, finally multiplying by the candidate aspect ratio. That text width calculation thus really only needs to be done once. Further, instead of trying different aspect ratios, the ratio can simply be calculated directly by dividing the width of the bounds by the string's width. This also calculates a more accurate aspect ratio, rather than always resulting in an aspect ratio of (0.95)^n.
For example, when trying to fit a 101-pixel wide text into a 100-pixel wide space, the current code would result in an aspect ratio of 0.95, making the text 96 pixels wide, leaving 4 pixels unused; the new code will instead calculate the aspect ratio as 100/101 == 0.9900990099... , making the text use the full 100 available pixels.
This in turn allows us to easily calculate the ratio also if we want to not just schrink but also stretch the text to fill the available space, so we add that as another text alignment option, number 3 (three).
This PR also demonstrates this in the VFX family of layouts, where on the default Full view, the text "MUSIC PRODUCTION SYNTHESIZER" ("DYNAMIC COMPONENT SYNTHESIZER" on the VFX), the logo-like text "ensoniq", and the keyboard-specific markers "VFX", "VFX-SD", "SD-1" and "3 2 V O I C E" can now be made to fill their available space horizontally as they should, making it all look that much more like the real thing. One example from the sd132, before:
after:
and the real thing:
While still not perfect, the "after" at least has the corrects widths and horizontal alignments.