fix(area): increase coordinate percent range beyond +-1000 (#6051)

This commit is contained in:
Liam
2024-04-21 03:05:35 -04:00
committed by GitHub
parent 1dfd782714
commit e373b705db
2 changed files with 134 additions and 8 deletions

View File

@@ -15,6 +15,7 @@ extern "C" {
*********************/ *********************/
#include "../lv_conf_internal.h" #include "../lv_conf_internal.h"
#include "lv_types.h" #include "lv_types.h"
#include "lv_math.h"
/********************* /*********************
* DEFINES * DEFINES
@@ -343,20 +344,24 @@ static inline void lv_point_precise_swap(lv_point_precise_t * p1, lv_point_preci
#define LV_COORD_SET_SPEC(x) ((x) | _LV_COORD_TYPE_SPEC) #define LV_COORD_SET_SPEC(x) ((x) | _LV_COORD_TYPE_SPEC)
/*Special coordinates*/
#define LV_PCT(x) (x < 0 ? LV_COORD_SET_SPEC(1000 - (x)) : LV_COORD_SET_SPEC(x))
#define LV_COORD_IS_PCT(x) ((LV_COORD_IS_SPEC(x) && _LV_COORD_PLAIN(x) <= 2000))
#define LV_COORD_GET_PCT(x) (_LV_COORD_PLAIN(x) > 1000 ? 1000 - _LV_COORD_PLAIN(x) : _LV_COORD_PLAIN(x))
#define LV_SIZE_CONTENT LV_COORD_SET_SPEC(2001)
LV_EXPORT_CONST_INT(LV_SIZE_CONTENT);
/*Max coordinate value*/ /*Max coordinate value*/
#define LV_COORD_MAX ((1 << _LV_COORD_TYPE_SHIFT) - 1) #define LV_COORD_MAX ((1 << _LV_COORD_TYPE_SHIFT) - 1)
#define LV_COORD_MIN (-LV_COORD_MAX) #define LV_COORD_MIN (-LV_COORD_MAX)
/*Special coordinates*/
#define LV_SIZE_CONTENT LV_COORD_SET_SPEC(LV_COORD_MAX)
#define _LV_PCT_STORED_MAX (LV_COORD_MAX - 1)
#if _LV_PCT_STORED_MAX % 2 != 0
#error _LV_PCT_STORED_MAX should be an even number
#endif
#define _LV_PCT_POS_MAX (_LV_PCT_STORED_MAX / 2)
#define LV_PCT(x) (LV_COORD_SET_SPEC(((x) < 0 ? (_LV_PCT_POS_MAX - LV_MAX((x), -_LV_PCT_POS_MAX)) : LV_MIN((x), _LV_PCT_POS_MAX))))
#define LV_COORD_IS_PCT(x) ((LV_COORD_IS_SPEC(x) && _LV_COORD_PLAIN(x) <= _LV_PCT_STORED_MAX))
#define LV_COORD_GET_PCT(x) (_LV_COORD_PLAIN(x) > _LV_PCT_POS_MAX ? _LV_PCT_POS_MAX - _LV_COORD_PLAIN(x) : _LV_COORD_PLAIN(x))
LV_EXPORT_CONST_INT(LV_COORD_MAX); LV_EXPORT_CONST_INT(LV_COORD_MAX);
LV_EXPORT_CONST_INT(LV_COORD_MIN); LV_EXPORT_CONST_INT(LV_COORD_MIN);
LV_EXPORT_CONST_INT(LV_SIZE_CONTENT);
/** /**
* Convert a percentage value to `int32_t`. * Convert a percentage value to `int32_t`.

View File

@@ -0,0 +1,121 @@
#if LV_BUILD_TEST
#include "../lvgl.h"
#include "unity/unity.h"
#define PCT_MAX_VALUE 268435455
void setUp(void)
{
/* Function run before every test */
}
void tearDown(void)
{
/* Function run after every test */
lv_obj_clean(lv_screen_active());
}
void test_pct(void)
{
int32_t pct_val;
int32_t pct_coord;
pct_val = 0;
pct_coord = lv_pct(pct_val);
TEST_ASSERT_TRUE(LV_COORD_IS_PCT(pct_coord));
TEST_ASSERT_FALSE(LV_COORD_IS_PX(pct_coord));
TEST_ASSERT_TRUE(LV_COORD_IS_SPEC(pct_coord));
TEST_ASSERT_EQUAL_INT32(pct_val, LV_COORD_GET_PCT(pct_coord));
pct_val = 1;
pct_coord = lv_pct(pct_val);
TEST_ASSERT_TRUE(LV_COORD_IS_PCT(pct_coord));
TEST_ASSERT_FALSE(LV_COORD_IS_PX(pct_coord));
TEST_ASSERT_TRUE(LV_COORD_IS_SPEC(pct_coord));
TEST_ASSERT_EQUAL_INT32(pct_val, LV_COORD_GET_PCT(pct_coord));
pct_val = 100;
pct_coord = lv_pct(pct_val);
TEST_ASSERT_TRUE(LV_COORD_IS_PCT(pct_coord));
TEST_ASSERT_FALSE(LV_COORD_IS_PX(pct_coord));
TEST_ASSERT_TRUE(LV_COORD_IS_SPEC(pct_coord));
TEST_ASSERT_EQUAL_INT32(pct_val, LV_COORD_GET_PCT(pct_coord));
pct_val = 111111111;
pct_coord = lv_pct(pct_val);
TEST_ASSERT_TRUE(LV_COORD_IS_PCT(pct_coord));
TEST_ASSERT_FALSE(LV_COORD_IS_PX(pct_coord));
TEST_ASSERT_TRUE(LV_COORD_IS_SPEC(pct_coord));
TEST_ASSERT_EQUAL_INT32(pct_val, LV_COORD_GET_PCT(pct_coord));
pct_val = PCT_MAX_VALUE;
pct_coord = lv_pct(pct_val);
TEST_ASSERT_TRUE(LV_COORD_IS_PCT(pct_coord));
TEST_ASSERT_FALSE(LV_COORD_IS_PX(pct_coord));
TEST_ASSERT_TRUE(LV_COORD_IS_SPEC(pct_coord));
TEST_ASSERT_EQUAL_INT32(pct_val, LV_COORD_GET_PCT(pct_coord));
pct_val = -1;
pct_coord = lv_pct(pct_val);
TEST_ASSERT_TRUE(LV_COORD_IS_PCT(pct_coord));
TEST_ASSERT_FALSE(LV_COORD_IS_PX(pct_coord));
TEST_ASSERT_TRUE(LV_COORD_IS_SPEC(pct_coord));
TEST_ASSERT_EQUAL_INT32(pct_val, LV_COORD_GET_PCT(pct_coord));
pct_val = -100;
pct_coord = lv_pct(pct_val);
TEST_ASSERT_TRUE(LV_COORD_IS_PCT(pct_coord));
TEST_ASSERT_FALSE(LV_COORD_IS_PX(pct_coord));
TEST_ASSERT_TRUE(LV_COORD_IS_SPEC(pct_coord));
TEST_ASSERT_EQUAL_INT32(pct_val, LV_COORD_GET_PCT(pct_coord));
pct_val = -111111111;
pct_coord = lv_pct(pct_val);
TEST_ASSERT_TRUE(LV_COORD_IS_PCT(pct_coord));
TEST_ASSERT_FALSE(LV_COORD_IS_PX(pct_coord));
TEST_ASSERT_TRUE(LV_COORD_IS_SPEC(pct_coord));
TEST_ASSERT_EQUAL_INT32(pct_val, LV_COORD_GET_PCT(pct_coord));
pct_val = -PCT_MAX_VALUE;
pct_coord = lv_pct(pct_val);
TEST_ASSERT_TRUE(LV_COORD_IS_PCT(pct_coord));
TEST_ASSERT_FALSE(LV_COORD_IS_PX(pct_coord));
TEST_ASSERT_TRUE(LV_COORD_IS_SPEC(pct_coord));
TEST_ASSERT_EQUAL_INT32(pct_val, LV_COORD_GET_PCT(pct_coord));
/**
* Out of bounds behavior.
* The pct value will be clamped to the max/min value if it's out of bounds.
*/
pct_val = PCT_MAX_VALUE + 1;
pct_coord = lv_pct(pct_val);
TEST_ASSERT_TRUE(LV_COORD_IS_PCT(pct_coord));
TEST_ASSERT_FALSE(LV_COORD_IS_PX(pct_coord));
TEST_ASSERT_TRUE(LV_COORD_IS_SPEC(pct_coord));
TEST_ASSERT_EQUAL_INT32(PCT_MAX_VALUE, LV_COORD_GET_PCT(pct_coord));
pct_val = PCT_MAX_VALUE + 100;
pct_coord = lv_pct(pct_val);
TEST_ASSERT_TRUE(LV_COORD_IS_PCT(pct_coord));
TEST_ASSERT_FALSE(LV_COORD_IS_PX(pct_coord));
TEST_ASSERT_TRUE(LV_COORD_IS_SPEC(pct_coord));
TEST_ASSERT_EQUAL_INT32(PCT_MAX_VALUE, LV_COORD_GET_PCT(pct_coord));
pct_val = -PCT_MAX_VALUE - 1;
pct_coord = lv_pct(pct_val);
TEST_ASSERT_TRUE(LV_COORD_IS_PCT(pct_coord));
TEST_ASSERT_FALSE(LV_COORD_IS_PX(pct_coord));
TEST_ASSERT_TRUE(LV_COORD_IS_SPEC(pct_coord));
TEST_ASSERT_EQUAL_INT32(-PCT_MAX_VALUE, LV_COORD_GET_PCT(pct_coord));
pct_val = -PCT_MAX_VALUE - 100;
pct_coord = lv_pct(pct_val);
TEST_ASSERT_TRUE(LV_COORD_IS_PCT(pct_coord));
TEST_ASSERT_FALSE(LV_COORD_IS_PX(pct_coord));
TEST_ASSERT_TRUE(LV_COORD_IS_SPEC(pct_coord));
TEST_ASSERT_EQUAL_INT32(-PCT_MAX_VALUE, LV_COORD_GET_PCT(pct_coord));
}
#endif