tpt.textwidth and newline

  • ssccsscc
    16th Jun 2017 Member 0 Permalink

    What is width of that string: "Test\nString"?

    print(tpt.textwidth("Test\nString"))

    On Windows its prints 52, but in Android its prints 27.

  • yousuck
    16th Jun 2017 Member 0 Permalink

    havnt posted in a while so

    hi

    Edited 3 times by yousuck. Last: 16th Jun 2017
  • Alexwall
    16th Jun 2017 Member 0 Permalink

    I think it's because the resolution is different on the two devices, it mesures pixels.

  • LBPHacker
    16th Jun 2017 Developer 0 Permalink

    The solution is to use the new API (sim.*, gfx.*, elem.*) whenever you can and only use the legacy API (tpt.*) when you must. The legacy API is ... well, legacy, meaning that you shouldn't trust it. tpt.textwidth is legacy, its supported counterpart is gfx.textSize, which returns the width of the widest line in the source string (lines are delimited by \n) and the height of all lines combined. Yes, it returns two integers.

  • jacob1
    17th Jun 2017 Developer 0 Permalink
    You should use gfx.textSize as LBPHacker said.

    If you want the exact reason, tpt and the android port are based on entirely different codebases. The TPT now is a rewrite of the original in c++. The android port is based off my mod, which still uses the original codebase (with changes and converted to c++ in a separate way).

    The android port ultimately calls this function to get the return value, which accounts for newlines by resetting x to 0 and taking the maximum x: https://github.com/jacob1/The-Powder-Toy/blob/c%2B%2B/src/graphics.cpp#L1411-L1440

    TPT uses this function, which is more primitive and doesn't account for newlines, only colors: https://github.com/ThePowderToy/The-Powder-Toy/blob/master/src/graphics/Graphics.cpp#L569-L587

    The android port does it correctly, so I should probably fix the TPT function to match it. But gfx.textSize works properly on both.