Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistently validate location on VectorShape #5327

Merged
merged 4 commits into from
Sep 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions shared-bindings/vectorio/Circle.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ static mp_obj_t vectorio_circle_make_new(const mp_obj_type_t *type, size_t n_arg

// VectorShape parts
mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj;
int16_t x = args[ARG_x].u_int;
int16_t y = args[ARG_y].u_int;
int32_t x = args[ARG_x].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);
self->draw_protocol_instance = vector_shape;

Expand Down
4 changes: 2 additions & 2 deletions shared-bindings/vectorio/Polygon.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ static mp_obj_t vectorio_polygon_make_new(const mp_obj_type_t *type, size_t n_ar

// VectorShape parts
mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj;
int16_t x = args[ARG_x].u_int;
int16_t y = args[ARG_y].u_int;
int32_t x = args[ARG_x].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);
self->draw_protocol_instance = vector_shape;

Expand Down
4 changes: 2 additions & 2 deletions shared-bindings/vectorio/Rectangle.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ static mp_obj_t vectorio_rectangle_make_new(const mp_obj_type_t *type, size_t n_

// VectorShape parts
mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj;
int16_t x = args[ARG_x].u_int;
int16_t y = args[ARG_y].u_int;
int32_t x = args[ARG_x].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);
self->draw_protocol_instance = vector_shape;

Expand Down
2 changes: 1 addition & 1 deletion shared-bindings/vectorio/VectorShape.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
// 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."""
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) &&
!mp_obj_is_type(pixel_shader, &displayio_palette_type)) {
mp_raise_TypeError_varg(translate("unsupported %q type"), MP_QSTR_pixel_shader);
Expand Down
4 changes: 2 additions & 2 deletions shared-bindings/vectorio/VectorShape.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
extern const mp_obj_type_t vectorio_vector_shape_type;

// 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
void common_hal_vectorio_vector_shape_construct(vectorio_vector_shape_t *self,
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);

Expand Down
2 changes: 1 addition & 1 deletion shared-module/vectorio/Polygon.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple
VECTORIO_POLYGON_DEBUG(" self.len: %d, len: %d, ", self->len, len);

if (len < 3) {
mp_raise_TypeError_varg(translate("Polygon needs at least 3 points"));
mp_raise_TypeError(translate("Polygon needs at least 3 points"));
}

if (self->len < 2 * len) {
Expand Down
49 changes: 37 additions & 12 deletions shared-module/vectorio/VectorShape.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,24 @@ static void _get_screen_area(vectorio_vector_shape_t *self, displayio_area_t *ou
}


STATIC
void check_bounds_and_set_x(vectorio_vector_shape_t *self, mp_int_t x) {
if (x < SHRT_MIN || x > SHRT_MAX) {
mp_raise_ValueError_varg(translate("%q must be between %d and %d"), MP_QSTR_x, SHRT_MIN, SHRT_MAX);
}
self->x = x;
}


STATIC
void check_bounds_and_set_y(vectorio_vector_shape_t *self, mp_int_t y) {
if (y < SHRT_MIN || y > SHRT_MAX) {
mp_raise_ValueError_varg(translate("%q must be between %d and %d"), MP_QSTR_y, SHRT_MIN, SHRT_MAX);
}
self->y = y;
}


// For use by Group to know where it needs to redraw on layer removal.
bool vectorio_vector_shape_get_dirty_area(vectorio_vector_shape_t *self, displayio_area_t *out_area) {
out_area->x1 = out_area->x2;
Expand Down Expand Up @@ -164,10 +182,10 @@ void common_hal_vectorio_vector_shape_set_dirty(void *vector_shape) {

void common_hal_vectorio_vector_shape_construct(vectorio_vector_shape_t *self,
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);
self->x = x;
self->y = y;
check_bounds_and_set_x(self, x);
check_bounds_and_set_y(self, y);
self->pixel_shader = pixel_shader;
self->ishape = ishape;
self->absolute_transform = &null_transform; // Critical to have a valid transform before getting screen area.
Expand All @@ -189,7 +207,7 @@ void common_hal_vectorio_vector_shape_set_x(vectorio_vector_shape_t *self, mp_in
if (self->x == x) {
return;
}
self->x = x;
check_bounds_and_set_x(self, x);
common_hal_vectorio_vector_shape_set_dirty(self);
}

Expand All @@ -205,7 +223,7 @@ void common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_in
if (self->y == y) {
return;
}
self->y = y;
check_bounds_and_set_y(self, y);
common_hal_vectorio_vector_shape_set_dirty(self);
}

Expand All @@ -224,20 +242,27 @@ void common_hal_vectorio_vector_shape_set_location(vectorio_vector_shape_t *self
mp_obj_t *tuple_items;
mp_obj_tuple_get(xy, &tuple_len, &tuple_items);
if (tuple_len != 2) {
mp_raise_TypeError_varg(translate("(x,y) integers required"));
mp_raise_TypeError(translate("(x,y) integers required"));
}

mp_int_t x;
mp_int_t y;
if (!mp_obj_get_int_maybe(tuple_items[ 0 ], &x)
|| !mp_obj_get_int_maybe(tuple_items[ 1 ], &y)
|| x < SHRT_MIN || x > SHRT_MAX || y < SHRT_MIN || y > SHRT_MAX
) {
|| !mp_obj_get_int_maybe(tuple_items[ 1 ], &y)) {
mp_raise_ValueError_varg(translate("unsupported %q type"), MP_QSTR_point);
}
self->x = (int16_t)x;
self->y = (int16_t)y;
common_hal_vectorio_vector_shape_set_dirty(self);
bool dirty = false;
if (self->x != x) {
check_bounds_and_set_x(self, x);
dirty = true;
}
if (self->y != y) {
check_bounds_and_set_y(self, y);
dirty = true;
}
if (dirty) {
common_hal_vectorio_vector_shape_set_dirty(self);
}
}


Expand Down