numworks / epsilon

Modern graphing calculator operating system.
https://www.numworks.com/resources/engineering/software/
1.75k stars 463 forks source link

[Python] Rendering a newline character with draw_string() resets the x coordinate #2106

Open yannis300307 opened 1 year ago

yannis300307 commented 1 year ago

Describe the bug

When rendering a newline character with draw_string(), the x coordinate will be resest.

Screenshots

Here is the result of draw_string("hello!\na line",100,10) :

screenshot

To Reproduce

Steps to reproduce the behavior:

  1. Go to the 'Python' app
  2. Go to the shell or create a new file
  3. Import kandinsky with from kandinsky import *
  4. Render a text with draw_string("hello!\na line",100,10) for exemple.
  5. See error

Expected behavior

The new line should be aligned with the first line of the text.

Environment

serval2412 commented 8 months ago

Help page https://www.numworks.com/manual/python/ indicates: "draw_string(text,x,y,[color1],[color2]) Displays text from the pixel x,y. The arguments color1 (text color) and color2 (background color) are optional. " So text begins to the x position but it indicates nothing about how "\n" is dealt with so going to the beginning of the line after \n isn't wrong.

Anyway, if we want to change the behaviour, the code dealing with this part is in kandinsky/src/context_text.cpp (KDContext::drawString method) 80 if (codePoint == UCodePointLineFeed) { 81 assert(position.y() < KDCOORDINATE_MAX - glyphSize.height()); 82 position = KDPoint(0, position.y() + glyphSize.height()); 83 if (origin().y() + position.y() >= Ion::Display::Height) { 84 break; 85 } 86 codePoint = decoder.nextCodePoint(); 87 }

if we change line 82 with: position = KDPoint(p.x(), position.y() + glyphSize.height());

it seems to work.

Remark: if we use position = KDPoint(position.x(), position.y() + glyphSize.height()); it doesn't work, the next line is shifted to the right.