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

GdiDrawText rounding issue #20

Wil-B opened this issue Oct 2, 2018 · 2 comments


2 participants
Copy link

commented Oct 2, 2018

y-text position

GdiDrawText seems to round down (Math.floor), whereas JSP uses round (Math.round).
Result is that text carefully positioned with GdiDrawText may not be anymore!

For comparison, gr.DrawString seems to use Math.round (SMP & JSP). So it looks like a bug with GdiDrawText?

var font = gdi.Font("segoe ui", 12, 1)
function RGB(r, g, b) {return 0xff000000 | r << 16 | g << 8 | b;}
function on_paint(gr) {
gr.FillSolidRect(0,19,50,10, RGB(255,0,0))
gr.GdiDrawText("T", font, RGB(0,0,0), 10, 15.9, 100, 40); // SMP draws @ y = 15; JSP draws @ y = 16; i.e. Math.floor vs round
gr.GdiDrawText("T", font, RGB(0,0,0), 15, 16, 100, 40);
gr.DrawString("T", font, RGB(0,0,0), 30, 15.9, 100, 40); // Both SMP & JSP draw @ y = 16; i.e. rounded
gr.DrawString("T", font, RGB(0,0,0), 35, 16, 100, 40);

@TheQwertiest TheQwertiest added this to To do: current release in Spider Monkey Panel via automation Oct 2, 2018


This comment has been minimized.

Copy link

commented Oct 2, 2018

Thanks for the report!

Not sure what to do about it though - the float/double to integer conversion is performed by JavaScript engine and currently it works as specified by ECMAScript standard:
From the standard:

Let posInt be sign(number) × floor(abs(number)).

Meaning that MS JScript engine actually violates ES specification, by performing round instead of floor.

In other words, float/double to integer rounding is gonna differ from JScript Panel everywhere (every method that accepts integers will round floats by flooring).
I'm not too keen on making hacks to every such method to deliberately break ES standard for the sake of compatibility with non-conformant MS engine :\

PS: Said hack will involve changing every method prototype to accept floats instead and rounding them manually to integer. Hopefully, you will agree, that such workaround is really ugly...
PPS: DrawString works correctly, because it uses floats:

Status DrawString(
    const WCHAR* string, 
    INT length,
    const Font *font,
    const RectF& layoutRect, ///< Rectangle with float coordinates
    const StringFormat *stringFormat,
    const Brush *brush

This comment has been minimized.

Copy link

commented Oct 2, 2018

Yes I think sticking with the standard is the right thing to do. I really wanted to know if it was a fixable bug or not, since the code can be tweaked.

@Wil-B Wil-B closed this Oct 2, 2018

Spider Monkey Panel automation moved this from To do: current release to Done Oct 2, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.