Espruino/tests/test_graphics_fillPoly2.js
2020-07-14 10:15:11 +01:00

139 lines
9.1 KiB
JavaScript

/* We actually expect fillPoly to be different to drawPoly now. In order to
be useful for rendering more complex shapes like fonts, fillPoly needs to render
from low X,Y *up to but not including the highest pixels*.
Despite seeming odd, it's actually extremely common behaviour
*/
var g = Graphics.createArrayBuffer(100,64,8);
g.dump = _=>{
var s = "";
var b = new Uint8Array(g.buffer);
var n = 0;
for (var y=0;y<g.getHeight();y++) {
s+="\n";
for (var x=0;x<g.getWidth();x++)
s+=".#"[b[n++]?1:0];
}
return s;
}
g.print = _=>{
print("`"+g.dump()+"`");
}
var ok = true;
function SHOULD_BE(a) {
var b = g.dump();
if (a!=b) {
console.log("GOT :"+b+"\nSHOULD BE:"+a+"\n================");
ok = false;
}
}
g.clear();
// test code from http://forum.espruino.com/conversations/341492/#comment15018442
var vs2 = function(x,y,x2,y2,p) { // vertices for beveled, chk box like shape
// 4 'beveled corners' = 8 corners by 0|p inset combinations for x/y corners
return [x,y+p, x+p,y, x2-p,y, x2,y+p, x2,y2-p, x2-p,y2, x+p,y2, x,y2-p]; };
var c0 = 0, c1 = 255;
g.clear();
g.setColor(c0);
g.fillRect(0,0,127,63); // canvas (color 0)
`beveled squares and rectangles drawn and filled with polygon (color 0)`
g.setColor(c1);
g.fillRect(8,8,40,53); // frame filled (color 1)
g.setColor(c0);
g.drawPoly(vs2(10,10,18,18,1),1); // 9x 9 draw beveled [1] for x and y
g.drawPoly(vs2(21,10,38,18,1),1); // 9x19 draw beveled [1] for x and y
g.fillPoly(vs2(10,21,18,29,1),1); // 9x 9 fill beveled [1] for x and y
g.fillPoly(vs2(21,21,38,29,1),1); // 9x19 fill beveled [1] for x and y
g.drawPoly(vs2(10,32,18,40,2),1); // 9x 9 draw beveled [2] for x and y
g.drawPoly(vs2(21,32,38,40,2),1); // 9x19 draw beveled [2] for x and y
g.fillPoly(vs2(10,43,18,51,2),1); // 9x 9 fill beveled [2] for x and y
g.fillPoly(vs2(21,43,38,51,2),1); // 9x19 fill beveled [2] for x and y
`scalable beveled squares w/ borders done with two stacked, filled polygons`
g.setColor(c1);
g.fillRect(45,8,57,53); // frame filled (color 1)
g.setColor(c0);
g.fillPoly(vs2(47,21,55,29,1),1); // bot bev'd [1] square separate (color 0)
g.fillPoly(vs2(47,43,55,51,1),2); // bot bev'd [2] square separate (color 0)
g.setColor(c1);
g.drawRect(62,8,74,53); // frame hallow (0 from canvas)
g.setColor(c1);
g.fillPoly(vs2(65,22,71,28,1),1); // top bev'd [1] square separate (color 1)
g.fillPoly(vs2(65,44,71,50,1),2); // top bev'd [2] square separate (color 1)
g.setColor(c1);
g.fillRect(79,8,91,53); // frame filled (color 1)
g.setColor(c0);
g.fillPoly(vs2(81,21,89,29,1),1); // bot bev'd [1] square combined (color 0)
g.fillPoly(vs2(81,43,89,51,1),2); // bot bev'd [2] square combined (color 0)
g.setColor(c1);
g.fillPoly(vs2(82,22,88,28,1),1); // top bev'd [1] square combined (color 1)
g.fillPoly(vs2(82,44,88,50,1),2); // top bev'd [2] square combined (color 1)
SHOULD_BE(`
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
........#################################....#############....#############....#############........
........#################################....#############....#...........#....#############........
........###.......####................###....#############....#...........#....#############........
........##.#######.##.################.##....#############....#...........#....#############........
........##.#######.##.################.##....#############....#...........#....#############........
........##.#######.##.################.##....#############....#...........#....#############........
........##.#######.##.################.##....#############....#...........#....#############........
........##.#######.##.################.##....#############....#...........#....#############........
........##.#######.##.################.##....#############....#...........#....#############........
........##.#######.##.################.##....#############....#...........#....#############........
........###.......####................###....#############....#...........#....#############........
........#################################....#############....#...........#....#############........
........#################################....#############....#...........#....#############........
........###......#####...............####....###......####....#...........#....###......####........
........##........###.................###....##........###....#...####....#....##..####..###........
........##........###.................###....##........###....#..######...#....##.######.###........
........##........###.................###....##........###....#..######...#....##.######.###........
........##........###.................###....##........###....#..######...#....##.######.###........
........##........###.................###....##........###....#..######...#....##.######.###........
........##........###.................###....##........###....#..######...#....##.######.###........
........##........###.................###....##........###....#...........#....##........###........
........#################################....#############....#...........#....#############........
........#################################....#############....#...........#....#############........
........#################################....#############....#...........#....#############........
........####.....######..............####....#############....#...........#....#############........
........###.#####.####.##############.###....#############....#...........#....#############........
........##.#######.##.################.##....#############....#...........#....#############........
........##.#######.##.################.##....#############....#...........#....#############........
........##.#######.##.################.##....#############....#...........#....#############........
........##.#######.##.################.##....#############....#...........#....#############........
........##.#######.##.################.##....#############....#...........#....#############........
........###.#####.####.##############.###....#############....#...........#....#############........
........####.....######..............####....#############....#...........#....#############........
........#################################....#############....#...........#....#############........
........#################################....#############....#...........#....#############........
........####....#######.............#####....###......####....#...........#....###......####........
........###......#####...............####....##........###....#...####....#....##..####..###........
........##........###.................###....##........###....#..######...#....##.######.###........
........##........###.................###....##........###....#..######...#....##.######.###........
........##........###.................###....##........###....#..######...#....##.######.###........
........##........###.................###....##........###....#..######...#....##.######.###........
........##........###.................###....##........###....#..######...#....##.######.###........
........###......#####...............####....##........###....#...........#....##........###........
........#################################....#############....#...........#....#############........
........#################################....#############....#...........#....#############........
........#################################....#############....#############....#############........
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................`);
result = ok;