Improve the bounds checking on the location (the x, y co-ordinates) of a

VectorShape object so that it is consistent no matter where it is set from:
 * the constructor
 * the x and y setters
 * the location setter
This commit is contained in:
James Carr 2021-09-10 09:24:33 +01:00
parent 0ee0ed7035
commit 1f446916c3
No known key found for this signature in database
GPG Key ID: 04CEE584D0B924E0
6 changed files with 40 additions and 28 deletions

View File

@ -42,8 +42,8 @@ static mp_obj_t vectorio_circle_make_new(const mp_obj_type_t *type, size_t n_arg
// VectorShape parts // VectorShape parts
mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj; mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj;
int16_t x = args[ARG_x].u_int; int32_t x = args[ARG_x].u_int;
int16_t y = args[ARG_y].u_int; int32_t y = args[ARG_y].u_int;
mp_obj_t vector_shape = vectorio_vector_shape_make_new(self, pixel_shader, x, y); mp_obj_t vector_shape = vectorio_vector_shape_make_new(self, pixel_shader, x, y);
self->draw_protocol_instance = vector_shape; self->draw_protocol_instance = vector_shape;

View File

@ -47,8 +47,8 @@ static mp_obj_t vectorio_polygon_make_new(const mp_obj_type_t *type, size_t n_ar
// VectorShape parts // VectorShape parts
mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj; mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj;
int16_t x = args[ARG_x].u_int; int32_t x = args[ARG_x].u_int;
int16_t y = args[ARG_y].u_int; int32_t y = args[ARG_y].u_int;
mp_obj_t vector_shape = vectorio_vector_shape_make_new(self, pixel_shader, x, y); mp_obj_t vector_shape = vectorio_vector_shape_make_new(self, pixel_shader, x, y);
self->draw_protocol_instance = vector_shape; self->draw_protocol_instance = vector_shape;

View File

@ -46,8 +46,8 @@ static mp_obj_t vectorio_rectangle_make_new(const mp_obj_type_t *type, size_t n_
// VectorShape parts // VectorShape parts
mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj; mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj;
int16_t x = args[ARG_x].u_int; int32_t x = args[ARG_x].u_int;
int16_t y = args[ARG_y].u_int; int32_t y = args[ARG_y].u_int;
mp_obj_t vector_shape = vectorio_vector_shape_make_new(self, pixel_shader, x, y); mp_obj_t vector_shape = vectorio_vector_shape_make_new(self, pixel_shader, x, y);
self->draw_protocol_instance = vector_shape; self->draw_protocol_instance = vector_shape;

View File

@ -23,7 +23,7 @@
// pixel_shader: The pixel shader that produces colors from values. The shader can be a displayio.Palette(1); it will be asked to color pixel value 0. // pixel_shader: The pixel shader that produces colors from values. The shader can be a displayio.Palette(1); it will be asked to color pixel value 0.
// x: Initial x position of the center axis of the shape within the parent. // x: Initial x position of the center axis of the shape within the parent.
// y: Initial y position of the center axis of the shape within the parent.""" // y: Initial y position of the center axis of the shape within the parent."""
mp_obj_t vectorio_vector_shape_make_new(const mp_obj_t shape, const mp_obj_t pixel_shader, int16_t x, int16_t y) { mp_obj_t vectorio_vector_shape_make_new(const mp_obj_t shape, const mp_obj_t pixel_shader, int32_t x, int32_t y) {
if (!mp_obj_is_type(pixel_shader, &displayio_colorconverter_type) && if (!mp_obj_is_type(pixel_shader, &displayio_colorconverter_type) &&
!mp_obj_is_type(pixel_shader, &displayio_palette_type)) { !mp_obj_is_type(pixel_shader, &displayio_palette_type)) {
mp_raise_TypeError_varg(translate("unsupported %q type"), MP_QSTR_pixel_shader); mp_raise_TypeError_varg(translate("unsupported %q type"), MP_QSTR_pixel_shader);
@ -99,7 +99,10 @@ STATIC mp_obj_t vectorio_vector_shape_obj_set_x(mp_obj_t wrapper_shape, mp_obj_t
vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape)); vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape));
mp_int_t x = mp_obj_get_int(x_obj); mp_int_t x = mp_obj_get_int(x_obj);
common_hal_vectorio_vector_shape_set_x(self, x); bool dirty = common_hal_vectorio_vector_shape_set_x(self, x);
if (dirty) {
common_hal_vectorio_vector_shape_set_dirty(self);
}
return mp_const_none; return mp_const_none;
} }
MP_DEFINE_CONST_FUN_OBJ_2(vectorio_vector_shape_set_x_obj, vectorio_vector_shape_obj_set_x); MP_DEFINE_CONST_FUN_OBJ_2(vectorio_vector_shape_set_x_obj, vectorio_vector_shape_obj_set_x);
@ -130,7 +133,10 @@ STATIC mp_obj_t vectorio_vector_shape_obj_set_y(mp_obj_t wrapper_shape, mp_obj_t
vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape)); vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape));
mp_int_t y = mp_obj_get_int(y_obj); mp_int_t y = mp_obj_get_int(y_obj);
common_hal_vectorio_vector_shape_set_y(self, y); bool dirty = common_hal_vectorio_vector_shape_set_y(self, y);
if (dirty) {
common_hal_vectorio_vector_shape_set_dirty(self);
}
return mp_const_none; return mp_const_none;
} }
MP_DEFINE_CONST_FUN_OBJ_2(vectorio_vector_shape_set_y_obj, vectorio_vector_shape_obj_set_y); MP_DEFINE_CONST_FUN_OBJ_2(vectorio_vector_shape_set_y_obj, vectorio_vector_shape_obj_set_y);

View File

@ -11,23 +11,23 @@
extern const mp_obj_type_t vectorio_vector_shape_type; extern const mp_obj_type_t vectorio_vector_shape_type;
// Python shared bindings constructor // Python shared bindings constructor
mp_obj_t vectorio_vector_shape_make_new(const mp_obj_t shape, const mp_obj_t pixel_shader, int16_t x, int16_t y); mp_obj_t vectorio_vector_shape_make_new(const mp_obj_t shape, const mp_obj_t pixel_shader, int32_t x, int32_t y);
// C data constructor // C data constructor
void common_hal_vectorio_vector_shape_construct(vectorio_vector_shape_t *self, void common_hal_vectorio_vector_shape_construct(vectorio_vector_shape_t *self,
vectorio_ishape_t ishape, vectorio_ishape_t ishape,
mp_obj_t pixel_shader, uint16_t x, uint16_t y); mp_obj_t pixel_shader, int32_t x, int32_t y);
void common_hal_vectorio_vector_shape_set_dirty(void *self); void common_hal_vectorio_vector_shape_set_dirty(void *self);
mp_int_t common_hal_vectorio_vector_shape_get_x(vectorio_vector_shape_t *self); mp_int_t common_hal_vectorio_vector_shape_get_x(vectorio_vector_shape_t *self);
void common_hal_vectorio_vector_shape_set_x(vectorio_vector_shape_t *self, mp_int_t x); bool common_hal_vectorio_vector_shape_set_x(vectorio_vector_shape_t *self, mp_int_t x);
mp_obj_tuple_t *common_hal_vectorio_vector_shape_get_location(vectorio_vector_shape_t *self); mp_obj_tuple_t *common_hal_vectorio_vector_shape_get_location(vectorio_vector_shape_t *self);
void common_hal_vectorio_vector_shape_set_location(vectorio_vector_shape_t *self, mp_obj_t xy); void common_hal_vectorio_vector_shape_set_location(vectorio_vector_shape_t *self, mp_obj_t xy);
mp_int_t common_hal_vectorio_vector_shape_get_y(vectorio_vector_shape_t *self); mp_int_t common_hal_vectorio_vector_shape_get_y(vectorio_vector_shape_t *self);
void common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_int_t y); bool common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_int_t y);
mp_obj_t common_hal_vectorio_vector_shape_get_pixel_shader(vectorio_vector_shape_t *self); mp_obj_t common_hal_vectorio_vector_shape_get_pixel_shader(vectorio_vector_shape_t *self);
void common_hal_vectorio_vector_shape_set_pixel_shader(vectorio_vector_shape_t *self, mp_obj_t pixel_shader); void common_hal_vectorio_vector_shape_set_pixel_shader(vectorio_vector_shape_t *self, mp_obj_t pixel_shader);

View File

@ -164,10 +164,10 @@ void common_hal_vectorio_vector_shape_set_dirty(void *vector_shape) {
void common_hal_vectorio_vector_shape_construct(vectorio_vector_shape_t *self, void common_hal_vectorio_vector_shape_construct(vectorio_vector_shape_t *self,
vectorio_ishape_t ishape, vectorio_ishape_t ishape,
mp_obj_t pixel_shader, uint16_t x, uint16_t y) { mp_obj_t pixel_shader, int32_t x, int32_t y) {
VECTORIO_SHAPE_DEBUG("%p vector_shape_construct x:%3d, y:%3d\n", self, x, y); VECTORIO_SHAPE_DEBUG("%p vector_shape_construct x:%3d, y:%3d\n", self, x, y);
self->x = x; common_hal_vectorio_vector_shape_set_x(self, x);
self->y = y; common_hal_vectorio_vector_shape_set_y(self, y);
self->pixel_shader = pixel_shader; self->pixel_shader = pixel_shader;
self->ishape = ishape; self->ishape = ishape;
self->absolute_transform = &null_transform; // Critical to have a valid transform before getting screen area. self->absolute_transform = &null_transform; // Critical to have a valid transform before getting screen area.
@ -184,13 +184,16 @@ mp_int_t common_hal_vectorio_vector_shape_get_x(vectorio_vector_shape_t *self) {
} }
void common_hal_vectorio_vector_shape_set_x(vectorio_vector_shape_t *self, mp_int_t x) { bool common_hal_vectorio_vector_shape_set_x(vectorio_vector_shape_t *self, mp_int_t x) {
VECTORIO_SHAPE_DEBUG("%p set_x %d\n", self, x); VECTORIO_SHAPE_DEBUG("%p set_x %d\n", self, x);
if (self->x == x) { if (self->x == x) {
return; return false; // it's not dirty
}
if (x < SHRT_MIN || x > SHRT_MAX) {
mp_raise_ValueError_varg(translate("unsupported %q type"), MP_QSTR_point);
} }
self->x = x; self->x = x;
common_hal_vectorio_vector_shape_set_dirty(self); return true; // it's dirty
} }
@ -200,13 +203,16 @@ mp_int_t common_hal_vectorio_vector_shape_get_y(vectorio_vector_shape_t *self) {
} }
void common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_int_t y) { bool common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_int_t y) {
VECTORIO_SHAPE_DEBUG("%p set_y %d\n", self, y); VECTORIO_SHAPE_DEBUG("%p set_y %d\n", self, y);
if (self->y == y) { if (self->y == y) {
return; return false; // it's not dirty
}
if (y < SHRT_MIN || y > SHRT_MAX) {
mp_raise_ValueError_varg(translate("unsupported %q type"), MP_QSTR_point);
} }
self->y = y; self->y = y;
common_hal_vectorio_vector_shape_set_dirty(self); return true; // it's dirty
} }
mp_obj_tuple_t *common_hal_vectorio_vector_shape_get_location(vectorio_vector_shape_t *self) { mp_obj_tuple_t *common_hal_vectorio_vector_shape_get_location(vectorio_vector_shape_t *self) {
@ -230,14 +236,14 @@ void common_hal_vectorio_vector_shape_set_location(vectorio_vector_shape_t *self
mp_int_t x; mp_int_t x;
mp_int_t y; mp_int_t y;
if (!mp_obj_get_int_maybe(tuple_items[ 0 ], &x) if (!mp_obj_get_int_maybe(tuple_items[ 0 ], &x)
|| !mp_obj_get_int_maybe(tuple_items[ 1 ], &y) || !mp_obj_get_int_maybe(tuple_items[ 1 ], &y)) {
|| x < SHRT_MIN || x > SHRT_MAX || y < SHRT_MIN || y > SHRT_MAX
) {
mp_raise_ValueError_varg(translate("unsupported %q type"), MP_QSTR_point); mp_raise_ValueError_varg(translate("unsupported %q type"), MP_QSTR_point);
} }
self->x = (int16_t)x; bool dirty = common_hal_vectorio_vector_shape_set_x(self, x);
self->y = (int16_t)y; dirty |= common_hal_vectorio_vector_shape_set_y(self, y);
common_hal_vectorio_vector_shape_set_dirty(self); if (dirty) {
common_hal_vectorio_vector_shape_set_dirty(self);
}
} }