Skip to content

Commit 50eabb8

Browse files
committed
Graphics: fix cropping of images in strings when gfx instance is rotated by 90 degrees
fix espruino/BangleApps#4053
1 parent 7c8e758 commit 50eabb8

File tree

3 files changed

+68
-22
lines changed

3 files changed

+68
-22
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
JIT: Fix floating point constants
3030
Linux SDL: Use SDL2, more keys forwarded, allow resizing of gfx window
3131
Fix Linux ARM64 build
32+
Graphics: fix cropping of images in strings when gfx instance is rotated by 90 degrees
3233

3334
2v27 : nRF5x: Ensure Bluetooth notifications work correctly when two separate connections use the same handle for their characteristics
3435
nRF5x: Remove handlers from our handlers array when a device is disconnected

libs/graphics/jswrap_graphics.c

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -414,21 +414,26 @@ NO_INLINE void _jswrap_drawImageSimple(JsGraphics *gfx, int xPos, int yPos, GfxD
414414
int x1 = xPos, y1 = yPos, x2 = xPos+img->width-1, y2 = yPos+img->height-1;
415415
if (!jsvStringIteratorHasChar(it)) return; // no data
416416
#ifndef SAVE_ON_FLASH
417-
graphicsSetModifiedAndClip(gfx,&x1,&y1,&x2,&y2, true); // ensure we clip Y, coords were already rotated
418-
/* force a skip forward as many bytes as we need. Ideally we would use
419-
jsvStringIteratorGotoUTF8 but we don't have the UTF8 index or
420-
source string here. This is still better than trying to render every pixel! */
421-
if (y2<y1 || x2<x1) { // offscreen - skip everything and exit
422-
if (parseFullImage) {
423-
bits = -img->bpp*img->width*img->height;
424-
while (bits < 0) {
425-
jsvStringIteratorNextUTF8(it);
426-
bits += 8;
417+
if (!(gfx->data.flags&JSGRAPHICSFLAGS_SWAP_XY)) {
418+
/* if we've not swapped X/Y we can so some optimisations
419+
to reduce what we draw - but if we swapped XY there's no real point
420+
because of the difference in direction we scan out. Also it's hard to get right! */
421+
graphicsSetModifiedAndClip(gfx,&x1,&y1,&x2,&y2, true); // ensure we clip Y
422+
/* force a skip forward as many bytes as we need. Ideally we would use
423+
jsvStringIteratorGotoUTF8 but we don't have the UTF8 index or
424+
source string here. This is still better than trying to render every pixel! */
425+
if (y2<y1 || x2<x1) { // offscreen - skip everything and exit
426+
if (parseFullImage) {
427+
bits = -img->bpp*img->width*img->height;
428+
while (bits < 0) {
429+
jsvStringIteratorNextUTF8(it);
430+
bits += 8;
431+
}
427432
}
428-
}
429-
return;
430-
} else // onscreen. y1!=yPos if clipped - ensure we skip enough bytes
431-
bits = -(y1-yPos)*img->bpp*img->width;
433+
return;
434+
} else // onscreen. y1!=yPos if clipped - ensure we skip enough bytes
435+
bits = -(y1-yPos)*img->bpp*img->width;
436+
}
432437
#endif
433438
JsGraphicsSetPixelFn setPixel = graphicsGetSetPixelUnclippedFn(gfx, xPos, y1, xPos+img->width-1, y2, true);
434439
for (int y=y1;y<=y2;y++) {

tests/test_graphics_drawStringImage.js

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
var g = Graphics.createArrayBuffer(64,8,8);
2-
g.dump = _=>{
2+
Graphics.prototype.dump = _=>{
33
var s = "";
44
var b = new Uint8Array(g.buffer);
55
var n = 0;
66
for (var y=0;y<g.getHeight();y++) {
77
s+="\n";
88
for (var x=0;x<g.getWidth();x++)
9-
s+=".#"[b[n++]?1:0];
9+
s+=".#"[g.getPixel(x,y)?1:0];
1010
}
1111
return s;
1212
}
@@ -74,14 +74,54 @@ g.clear().setRotation(2);
7474
g.setClipRect(0,0,63,5);
7575
g.drawString(str,0,0);
7676
SHOULD_BE(`
77+
#.#.###.....#.....#....#.#..#..##..#...##.......................
78+
#.#..#.........#.......#.#.#.#.#.#.#...#.#......................
79+
###..#.........#.......###.#.#.#.#.#...#.#......................
80+
#.#..#......#.....#....###.#.#.##..#...#.#......................
81+
#.#.###......#####.....#.#..#..#.#.###.##.......................
7782
................................................................
7883
................................................................
79-
................................................................
80-
.......................##.###.#.#..#..#.#.....#####......###.#.#
81-
......................#.#...#..##.#.#.###....#.....#......#..#.#
82-
......................#.#...#.#.#.#.#.###.......#.........#..###
83-
......................#.#...#.#.#.#.#.#.#.......#.........#..#.#
84-
.......................##...#..##..#..#.#....#.....#.....###.#.#`);
84+
................................................................`);
85+
86+
// Test image draw within text *when rotated 90 degrees and clipped* - https://github.com/espruino/BangleApps/issues/4053
87+
var str = "HI \0"+img;
88+
g = Graphics.createArrayBuffer(32,32,8);
89+
g.clear().setRotation(1);
90+
g.setClipRect(0,0,63,5);
91+
g.drawString(str,0,0);
92+
SHOULD_BE(`
93+
#.#.###.....#.....#.............
94+
#.#..#.........#................
95+
###..#.........#................
96+
#.#..#......#.....#.............
97+
#.#.###......#####..............
98+
................................
99+
................................
100+
................................
101+
................................
102+
................................
103+
................................
104+
................................
105+
................................
106+
................................
107+
................................
108+
................................
109+
................................
110+
................................
111+
................................
112+
................................
113+
................................
114+
................................
115+
................................
116+
................................
117+
................................
118+
................................
119+
................................
120+
................................
121+
................................
122+
................................
123+
................................
124+
................................`);
85125

86126

87127
result = ok;

0 commit comments

Comments
 (0)