diff --git a/.github/workflows/build-project.yml b/.github/workflows/build-project.yml index d14ac80d..471b4727 100644 --- a/.github/workflows/build-project.yml +++ b/.github/workflows/build-project.yml @@ -12,9 +12,9 @@ jobs: runs-on: ubuntu-latest steps: - name: Install ARM Toolchain - uses: carlosperate/arm-none-eabi-gcc-action@v1.8.1 + uses: carlosperate/arm-none-eabi-gcc-action@v1.11.0 with: - release: "10.3-2021.10" + release: "14.3.Rel1" - name: Setup Python uses: actions/setup-python@v5 with: @@ -23,4 +23,4 @@ jobs: - name: Checkout uses: actions/checkout@v4 - name: Build Project - run: pros make clean all \ No newline at end of file + run: pros make clean all diff --git a/Makefile b/Makefile index 80ee609c..aa5bbf8b 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ INCDIR=$(ROOT)/include WARNFLAGS+= EXTRA_CFLAGS= -EXTRA_CXXFLAGS= +EXTRA_CXXFLAGS=-Wno-deprecated-enum-enum-conversion # Set to 1 to enable hot/cold linking USE_PACKAGE:=1 diff --git a/common.mk b/common.mk index 33efe871..40da70c9 100644 --- a/common.mk +++ b/common.mk @@ -1,7 +1,7 @@ ARCHTUPLE=arm-none-eabi- DEVICE=VEX EDR V5 -MFLAGS=-mcpu=cortex-a9 -mfpu=neon-fp16 -mfloat-abi=softfp -Os -g +MFLAGS=-mcpu=cortex-a9 -mfpu=neon-fp16 -mfloat-abi=hard -Os -g -mthumb CPPFLAGS=-D_POSIX_THREADS -D_UNIX98_THREAD_MUTEX_ATTRIBUTES -D_POSIX_TIMERS -D_POSIX_MONOTONIC_CLOCK GCCFLAGS=-ffunction-sections -fdata-sections -fdiagnostics-color -funwind-tables @@ -20,8 +20,8 @@ WARNFLAGS+=-Wno-psabi SPACE := $() $() COMMA := , -C_STANDARD?=gnu11 -CXX_STANDARD?=gnu++20 +C_STANDARD?=gnu2x +CXX_STANDARD?=gnu++23 DEPDIR := .d $(shell mkdir -p $(DEPDIR)) @@ -34,7 +34,7 @@ LIBRARIES+=$(wildcard $(FWDIR)/*.a) EXCLUDE_COLD_LIBRARIES+=$(FWDIR)/libc.a $(FWDIR)/libm.a COLD_LIBRARIES=$(filter-out $(EXCLUDE_COLD_LIBRARIES), $(LIBRARIES)) wlprefix=-Wl,$(subst $(SPACE),$(COMMA),$1) -LNK_FLAGS=--gc-sections --start-group $(strip $(LIBRARIES)) -lgcc -lstdc++ --end-group -T$(FWDIR)/v5-common.ld +LNK_FLAGS=--gc-sections --start-group $(strip $(LIBRARIES)) -lgcc -lstdc++ --end-group -T$(FWDIR)/v5-common.ld --no-warn-rwx-segments --sort-section=alignment --sort-common ASMFLAGS=$(MFLAGS) $(WARNFLAGS) CFLAGS=$(MFLAGS) $(CPPFLAGS) $(WARNFLAGS) $(GCCFLAGS) --std=$(C_STANDARD) @@ -188,14 +188,14 @@ quick: $(DEFAULT_BIN) all: clean $(DEFAULT_BIN) -clean: +clean:: @echo Cleaning project -$Drm -rf $(BINDIR) -$Drm -rf $(DEPDIR) ifeq ($(IS_LIBRARY),1) ifeq ($(LIBNAME),libbest) -$(errror "You should rename your library! libbest is the default library name and should be changed") +$(error "You should rename your library! libbest is the default library name and should be changed") endif LIBAR=$(BINDIR)/$(LIBNAME).a @@ -206,6 +206,7 @@ clean-template: -$Drm -rf $(TEMPLATE_DIR) $(LIBAR): $(call GETALLOBJ,$(EXCLUDE_SRC_FROM_LIB)) $(EXTRA_LIB_DEPS) + -$Dmkdir $(BINDIR) -$Drm -f $@ $(call test_output_2,Creating $@ ,$(AR) rcs $@ $^, $(DONE_STRING)) @@ -213,7 +214,7 @@ $(LIBAR): $(call GETALLOBJ,$(EXCLUDE_SRC_FROM_LIB)) $(EXTRA_LIB_DEPS) library: $(LIBAR) .PHONY: template -template: clean-template $(LIBAR) +template:: clean-template $(LIBAR) $Dpros c create-template . $(LIBNAME) $(VERSION) $(foreach file,$(TEMPLATE_FILES) $(LIBAR),--system "$(file)") --target v5 $(CREATE_TEMPLATE_FLAGS) endif @@ -262,7 +263,7 @@ $(foreach asmext,$(ASMEXTS),$(eval $(call asm_rule,$(asmext)))) define c_rule $(BINDIR)/%.$1.o: $(SRCDIR)/%.$1 -$(BINDIR)/%.$1.o: $(SRCDIR)/%.$1 $(DEPDIR)/$(basename $1).d +$(BINDIR)/%.$1.o: $(SRCDIR)/%.$1 $(DEPDIR)/$(basename %).d $(VV)mkdir -p $$(dir $$@) $(MAKEDEPFOLDER) $$(call test_output_2,Compiled $$< ,$(CC) -c $(INCLUDE) -iquote"$(INCDIR)/$$(dir $$*)" $(CFLAGS) $(EXTRA_CFLAGS) $(DEPFLAGS) -o $$@ $$<,$(OK_STRING)) @@ -305,4 +306,4 @@ cxx-sysroot: $(DEPDIR)/%.d: ; .PRECIOUS: $(DEPDIR)/%.d -include $(wildcard $(patsubst $(SRCDIR)/%,$(DEPDIR)/%.d,$(CSRC) $(CXXSRC))) +include $(wildcard $(patsubst $(SRCDIR)/%,$(DEPDIR)/%.d,$(CSRC) $(CXXSRC))) \ No newline at end of file diff --git a/firmware/libc.a b/firmware/libc.a index 51439b99..a2e1cdb7 100644 Binary files a/firmware/libc.a and b/firmware/libc.a differ diff --git a/firmware/liblvgl.a b/firmware/liblvgl.a index 81e5f1a9..e339c940 100644 Binary files a/firmware/liblvgl.a and b/firmware/liblvgl.a differ diff --git a/firmware/libm.a b/firmware/libm.a index 3d4066d5..63c9951f 100644 Binary files a/firmware/libm.a and b/firmware/libm.a differ diff --git a/firmware/libpros.a b/firmware/libpros.a index 956b8ad2..b316dae6 100644 Binary files a/firmware/libpros.a and b/firmware/libpros.a differ diff --git a/firmware/v5-common.ld b/firmware/v5-common.ld index 762a9055..7f262fc3 100644 --- a/firmware/v5-common.ld +++ b/firmware/v5-common.ld @@ -132,12 +132,6 @@ SECTIONS *(.gcc_except_table) } > RAM -.mmu_tbl (ALIGN(16384)) : { - __mmu_tbl_start = .; - *(.mmu_tbl) - __mmu_tbl_end = .; -} > RAM - .ARM.exidx : { __exidx_start = .; *(.ARM.exidx*) @@ -166,11 +160,9 @@ SECTIONS __fini_array_end = .; } > RAM -.ARM.attributes : { - __ARM.attributes_start = .; - *(.ARM.attributes) - __ARM.attributes_end = .; -} > RAM +/DISCARD/ : { + *(.ARM.attributes*) +} .sdata : { __sdata_start = .; @@ -228,36 +220,7 @@ _SDA2_BASE_ = __sdata2_start + ((__sbss2_end - __sdata2_start) / 2 ); _heap_end = .; HeapLimit = .; } > HEAP - -.stack (NOLOAD) : { - . = ALIGN(16); - _stack_end = .; - . += _STACK_SIZE; - . = ALIGN(16); - _stack = .; - __stack = _stack; - . = ALIGN(16); - _irq_stack_end = .; - . += _IRQ_STACK_SIZE; - . = ALIGN(16); - __irq_stack = .; - _supervisor_stack_end = .; - . += _SUPERVISOR_STACK_SIZE; - . = ALIGN(16); - __supervisor_stack = .; - _abort_stack_end = .; - . += _ABORT_STACK_SIZE; - . = ALIGN(16); - __abort_stack = .; - _fiq_stack_end = .; - . += _FIQ_STACK_SIZE; - . = ALIGN(16); - __fiq_stack = .; - _undef_stack_end = .; - . += _UNDEF_STACK_SIZE; - . = ALIGN(16); - __undef_stack = .; -} > COLD_MEMORY - +/* There are no sections for the stack. This is intentional, since VEXOs sets up a stack before + starting the program, and FreeRTOS task stacks are dynamically allocated. */ _end = .; } diff --git a/include/api.h b/include/api.h index 017b5226..5ba908b3 100644 --- a/include/api.h +++ b/include/api.h @@ -8,7 +8,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright Copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright Copyright (c) 2017-2024, Purdue University ACM SIGBots. * All rights reserved. * * This Source Code Form is subject to the terms of the Mozilla Public @@ -39,12 +39,8 @@ #include #endif /* __cplusplus */ -#define PROS_VERSION_MAJOR 4 -#define PROS_VERSION_MINOR 0 -#define PROS_VERSION_PATCH 7 -#define PROS_VERSION_STRING "4.0.7" - #include "pros/adi.h" +#include "pros/ai_vision.h" #include "pros/colors.h" #include "pros/device.h" #include "pros/distance.h" @@ -64,6 +60,7 @@ #ifdef __cplusplus #include "pros/adi.hpp" +#include "pros/ai_vision.hpp" #include "pros/colors.hpp" #include "pros/device.hpp" #include "pros/distance.hpp" diff --git a/include/liblvgl/core/lv_disp.h b/include/liblvgl/core/lv_disp.h deleted file mode 100644 index cd6efcc2..00000000 --- a/include/liblvgl/core/lv_disp.h +++ /dev/null @@ -1,264 +0,0 @@ -/** - * @file lv_disp.h - * - */ - -#ifndef LV_DISP_H -#define LV_DISP_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/hal/lv_hal.h" -#include "lv_obj.h" -#include "lv_theme.h" - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -typedef enum { - LV_SCR_LOAD_ANIM_NONE, - LV_SCR_LOAD_ANIM_OVER_LEFT, - LV_SCR_LOAD_ANIM_OVER_RIGHT, - LV_SCR_LOAD_ANIM_OVER_TOP, - LV_SCR_LOAD_ANIM_OVER_BOTTOM, - LV_SCR_LOAD_ANIM_MOVE_LEFT, - LV_SCR_LOAD_ANIM_MOVE_RIGHT, - LV_SCR_LOAD_ANIM_MOVE_TOP, - LV_SCR_LOAD_ANIM_MOVE_BOTTOM, - LV_SCR_LOAD_ANIM_FADE_IN, - LV_SCR_LOAD_ANIM_FADE_ON = LV_SCR_LOAD_ANIM_FADE_IN, /*For backward compatibility*/ - LV_SCR_LOAD_ANIM_FADE_OUT, - LV_SCR_LOAD_ANIM_OUT_LEFT, - LV_SCR_LOAD_ANIM_OUT_RIGHT, - LV_SCR_LOAD_ANIM_OUT_TOP, - LV_SCR_LOAD_ANIM_OUT_BOTTOM, -} lv_scr_load_anim_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Return with a pointer to the active screen - * @param disp pointer to display which active screen should be get. (NULL to use the default - * screen) - * @return pointer to the active screen object (loaded by 'lv_scr_load()') - */ -lv_obj_t * lv_disp_get_scr_act(lv_disp_t * disp); - -/** - * Return with a pointer to the previous screen. Only used during screen transitions. - * @param disp pointer to display which previous screen should be get. (NULL to use the default - * screen) - * @return pointer to the previous screen object or NULL if not used now - */ -lv_obj_t * lv_disp_get_scr_prev(lv_disp_t * disp); - -/** - * Make a screen active - * @param scr pointer to a screen - */ -void lv_disp_load_scr(lv_obj_t * scr); - -/** - * Return with the top layer. (Same on every screen and it is above the normal screen layer) - * @param disp pointer to display which top layer should be get. (NULL to use the default screen) - * @return pointer to the top layer object (transparent screen sized lv_obj) - */ -lv_obj_t * lv_disp_get_layer_top(lv_disp_t * disp); - -/** - * Return with the sys. layer. (Same on every screen and it is above the normal screen and the top - * layer) - * @param disp pointer to display which sys. layer should be retrieved. (NULL to use the default screen) - * @return pointer to the sys layer object (transparent screen sized lv_obj) - */ -lv_obj_t * lv_disp_get_layer_sys(lv_disp_t * disp); - -/** - * Set the theme of a display - * @param disp pointer to a display - */ -void lv_disp_set_theme(lv_disp_t * disp, lv_theme_t * th); - -/** - * Get the theme of a display - * @param disp pointer to a display - * @return the display's theme (can be NULL) - */ -lv_theme_t * lv_disp_get_theme(lv_disp_t * disp); - -/** - * Set the background color of a display - * @param disp pointer to a display - * @param color color of the background - */ -void lv_disp_set_bg_color(lv_disp_t * disp, lv_color_t color); - -/** - * Set the background image of a display - * @param disp pointer to a display - * @param img_src path to file or pointer to an `lv_img_dsc_t` variable - */ -void lv_disp_set_bg_image(lv_disp_t * disp, const void * img_src); - -/** - * Set opacity of the background - * @param disp pointer to a display - * @param opa opacity (0..255) - */ -void lv_disp_set_bg_opa(lv_disp_t * disp, lv_opa_t opa); - -/** - * Switch screen with animation - * @param scr pointer to the new screen to load - * @param anim_type type of the animation from `lv_scr_load_anim_t`, e.g. `LV_SCR_LOAD_ANIM_MOVE_LEFT` - * @param time time of the animation - * @param delay delay before the transition - * @param auto_del true: automatically delete the old screen - */ -void lv_scr_load_anim(lv_obj_t * scr, lv_scr_load_anim_t anim_type, uint32_t time, uint32_t delay, bool auto_del); - -/** - * Get elapsed time since last user activity on a display (e.g. click) - * @param disp pointer to a display (NULL to get the overall smallest inactivity) - * @return elapsed ticks (milliseconds) since the last activity - */ -uint32_t lv_disp_get_inactive_time(const lv_disp_t * disp); - -/** - * Manually trigger an activity on a display - * @param disp pointer to a display (NULL to use the default display) - */ -void lv_disp_trig_activity(lv_disp_t * disp); - -/** - * Clean any CPU cache that is related to the display. - * @param disp pointer to a display (NULL to use the default display) - */ -void lv_disp_clean_dcache(lv_disp_t * disp); - -/** - * Temporarily enable and disable the invalidation of the display. - * @param disp pointer to a display (NULL to use the default display) - * @param en true: enable invalidation; false: invalidation - */ -void lv_disp_enable_invalidation(lv_disp_t * disp, bool en); - -/** - * Get display invalidation is enabled. - * @param disp pointer to a display (NULL to use the default display) - * @return return true if invalidation is enabled - */ -bool lv_disp_is_invalidation_enabled(lv_disp_t * disp); - -/** - * Get a pointer to the screen refresher timer to - * modify its parameters with `lv_timer_...` functions. - * @param disp pointer to a display - * @return pointer to the display refresher timer. (NULL on error) - */ -lv_timer_t * _lv_disp_get_refr_timer(lv_disp_t * disp); - -/*------------------------------------------------ - * To improve backward compatibility - * Recommended only if you have one display - *------------------------------------------------*/ - -/** - * Get the active screen of the default display - * @return pointer to the active screen - */ -static inline lv_obj_t * lv_scr_act(void) -{ - return lv_disp_get_scr_act(lv_disp_get_default()); -} - -/** - * Get the top layer of the default display - * @return pointer to the top layer - */ -static inline lv_obj_t * lv_layer_top(void) -{ - return lv_disp_get_layer_top(lv_disp_get_default()); -} - -/** - * Get the active screen of the default display - * @return pointer to the sys layer - */ -static inline lv_obj_t * lv_layer_sys(void) -{ - return lv_disp_get_layer_sys(lv_disp_get_default()); -} - -static inline void lv_scr_load(lv_obj_t * scr) -{ - lv_disp_load_scr(scr); -} - -/********************** - * MACROS - **********************/ - -/*------------------------------------------------ - * To improve backward compatibility - * Recommended only if you have one display - *------------------------------------------------*/ - -#ifndef LV_HOR_RES -/** - * The horizontal resolution of the currently active display. - */ -#define LV_HOR_RES lv_disp_get_hor_res(lv_disp_get_default()) -#endif - -#ifndef LV_VER_RES -/** - * The vertical resolution of the currently active display. - */ -#define LV_VER_RES lv_disp_get_ver_res(lv_disp_get_default()) -#endif - -/** - * Scale the given number of pixels (a distance or size) relative to a 160 DPI display - * considering the DPI of the default display. - * It ensures that e.g. `lv_dpx(100)` will have the same physical size regardless to the - * DPI of the display. - * @param n the number of pixels to scale - * @return `n x current_dpi/160` - */ -static inline lv_coord_t lv_dpx(lv_coord_t n) -{ - return LV_DPX(n); -} - -/** - * Scale the given number of pixels (a distance or size) relative to a 160 DPI display - * considering the DPI of the given display. - * It ensures that e.g. `lv_dpx(100)` will have the same physical size regardless to the - * DPI of the display. - * @param obj a display whose dpi should be considered - * @param n the number of pixels to scale - * @return `n x current_dpi/160` - */ -static inline lv_coord_t lv_disp_dpx(const lv_disp_t * disp, lv_coord_t n) -{ - return _LV_DPX_CALC(lv_disp_get_dpi(disp), n); -} - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_DISP_H*/ diff --git a/include/liblvgl/core/lv_event.h b/include/liblvgl/core/lv_event.h deleted file mode 100644 index d5a9eb6b..00000000 --- a/include/liblvgl/core/lv_event.h +++ /dev/null @@ -1,363 +0,0 @@ -/** - * @file lv_event.h - * - */ - -#ifndef LV_EVENT_H -#define LV_EVENT_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -struct _lv_obj_t; -struct _lv_event_dsc_t; - -/** - * Type of event being sent to the object. - */ -typedef enum { - LV_EVENT_ALL = 0, - - /** Input device events*/ - LV_EVENT_PRESSED, /**< The object has been pressed*/ - LV_EVENT_PRESSING, /**< The object is being pressed (called continuously while pressing)*/ - LV_EVENT_PRESS_LOST, /**< The object is still being pressed but slid cursor/finger off of the object */ - LV_EVENT_SHORT_CLICKED, /**< The object was pressed for a short period of time, then released it. Not called if scrolled.*/ - LV_EVENT_LONG_PRESSED, /**< Object has been pressed for at least `long_press_time`. Not called if scrolled.*/ - LV_EVENT_LONG_PRESSED_REPEAT, /**< Called after `long_press_time` in every `long_press_repeat_time` ms. Not called if scrolled.*/ - LV_EVENT_CLICKED, /**< Called on release if not scrolled (regardless to long press)*/ - LV_EVENT_RELEASED, /**< Called in every cases when the object has been released*/ - LV_EVENT_SCROLL_BEGIN, /**< Scrolling begins. The event parameter is a pointer to the animation of the scroll. Can be modified*/ - LV_EVENT_SCROLL_END, /**< Scrolling ends*/ - LV_EVENT_SCROLL, /**< Scrolling*/ - LV_EVENT_GESTURE, /**< A gesture is detected. Get the gesture with `lv_indev_get_gesture_dir(lv_indev_get_act());` */ - LV_EVENT_KEY, /**< A key is sent to the object. Get the key with `lv_indev_get_key(lv_indev_get_act());`*/ - LV_EVENT_FOCUSED, /**< The object is focused*/ - LV_EVENT_DEFOCUSED, /**< The object is defocused*/ - LV_EVENT_LEAVE, /**< The object is defocused but still selected*/ - LV_EVENT_HIT_TEST, /**< Perform advanced hit-testing*/ - - /** Drawing events*/ - LV_EVENT_COVER_CHECK, /**< Check if the object fully covers an area. The event parameter is `lv_cover_check_info_t *`.*/ - LV_EVENT_REFR_EXT_DRAW_SIZE, /**< Get the required extra draw area around the object (e.g. for shadow). The event parameter is `lv_coord_t *` to store the size.*/ - LV_EVENT_DRAW_MAIN_BEGIN, /**< Starting the main drawing phase*/ - LV_EVENT_DRAW_MAIN, /**< Perform the main drawing*/ - LV_EVENT_DRAW_MAIN_END, /**< Finishing the main drawing phase*/ - LV_EVENT_DRAW_POST_BEGIN, /**< Starting the post draw phase (when all children are drawn)*/ - LV_EVENT_DRAW_POST, /**< Perform the post draw phase (when all children are drawn)*/ - LV_EVENT_DRAW_POST_END, /**< Finishing the post draw phase (when all children are drawn)*/ - LV_EVENT_DRAW_PART_BEGIN, /**< Starting to draw a part. The event parameter is `lv_obj_draw_dsc_t *`. */ - LV_EVENT_DRAW_PART_END, /**< Finishing to draw a part. The event parameter is `lv_obj_draw_dsc_t *`. */ - - /** Special events*/ - LV_EVENT_VALUE_CHANGED, /**< The object's value has changed (i.e. slider moved)*/ - LV_EVENT_INSERT, /**< A text is inserted to the object. The event data is `char *` being inserted.*/ - LV_EVENT_REFRESH, /**< Notify the object to refresh something on it (for the user)*/ - LV_EVENT_READY, /**< A process has finished*/ - LV_EVENT_CANCEL, /**< A process has been cancelled */ - - /** Other events*/ - LV_EVENT_DELETE, /**< Object is being deleted*/ - LV_EVENT_CHILD_CHANGED, /**< Child was removed, added, or its size, position were changed */ - LV_EVENT_CHILD_CREATED, /**< Child was created, always bubbles up to all parents*/ - LV_EVENT_CHILD_DELETED, /**< Child was deleted, always bubbles up to all parents*/ - LV_EVENT_SCREEN_UNLOAD_START, /**< A screen unload started, fired immediately when scr_load is called*/ - LV_EVENT_SCREEN_LOAD_START, /**< A screen load started, fired when the screen change delay is expired*/ - LV_EVENT_SCREEN_LOADED, /**< A screen was loaded*/ - LV_EVENT_SCREEN_UNLOADED, /**< A screen was unloaded*/ - LV_EVENT_SIZE_CHANGED, /**< Object coordinates/size have changed*/ - LV_EVENT_STYLE_CHANGED, /**< Object's style has changed*/ - LV_EVENT_LAYOUT_CHANGED, /**< The children position has changed due to a layout recalculation*/ - LV_EVENT_GET_SELF_SIZE, /**< Get the internal size of a widget*/ - - _LV_EVENT_LAST, /** Number of default events*/ - - - LV_EVENT_PREPROCESS = 0x80, /** This is a flag that can be set with an event so it's processed - before the class default event processing */ -} lv_event_code_t; - -typedef struct _lv_event_t { - struct _lv_obj_t * target; - struct _lv_obj_t * current_target; - lv_event_code_t code; - void * user_data; - void * param; - struct _lv_event_t * prev; - uint8_t deleted : 1; - uint8_t stop_processing : 1; - uint8_t stop_bubbling : 1; -} lv_event_t; - -/** - * @brief Event callback. - * Events are used to notify the user of some action being taken on the object. - * For details, see ::lv_event_t. - */ -typedef void (*lv_event_cb_t)(lv_event_t * e); - -/** - * Used as the event parameter of ::LV_EVENT_HIT_TEST to check if an `point` can click the object or not. - * `res` should be set like this: - * - If already set to `false` an other event wants that point non clickable. If you want to respect it leave it as `false` or set `true` to overwrite it. - * - If already set `true` and `point` shouldn't be clickable set to `false` - * - If already set to `true` you agree that `point` can click the object leave it as `true` - */ -typedef struct { - const lv_point_t * point; /**< A point relative to screen to check if it can click the object or not*/ - bool res; /**< true: `point` can click the object; false: it cannot*/ -} lv_hit_test_info_t; - -/** - * Used as the event parameter of ::LV_EVENT_COVER_CHECK to check if an area is covered by the object or not. - * In the event use `const lv_area_t * area = lv_event_get_cover_area(e)` to get the area to check - * and `lv_event_set_cover_res(e, res)` to set the result. - */ -typedef struct { - lv_cover_res_t res; - const lv_area_t * area; -} lv_cover_check_info_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Send an event to the object - * @param obj pointer to an object - * @param event_code the type of the event from `lv_event_t` - * @param param arbitrary data depending on the widget type and the event. (Usually `NULL`) - * @return LV_RES_OK: `obj` was not deleted in the event; LV_RES_INV: `obj` was deleted in the event_code - */ -lv_res_t lv_event_send(struct _lv_obj_t * obj, lv_event_code_t event_code, void * param); - -/** - * Used by the widgets internally to call the ancestor widget types's event handler - * @param class_p pointer to the class of the widget (NOT the ancestor class) - * @param e pointer to the event descriptor - * @return LV_RES_OK: the target object was not deleted in the event; LV_RES_INV: it was deleted in the event_code - */ -lv_res_t lv_obj_event_base(const lv_obj_class_t * class_p, lv_event_t * e); - -/** - * Get the object originally targeted by the event. It's the same even if the event is bubbled. - * @param e pointer to the event descriptor - * @return the target of the event_code - */ -struct _lv_obj_t * lv_event_get_target(lv_event_t * e); - -/** - * Get the current target of the event. It's the object which event handler being called. - * If the event is not bubbled it's the same as "normal" target. - * @param e pointer to the event descriptor - * @return pointer to the current target of the event_code - */ -struct _lv_obj_t * lv_event_get_current_target(lv_event_t * e); - -/** - * Get the event code of an event - * @param e pointer to the event descriptor - * @return the event code. (E.g. `LV_EVENT_CLICKED`, `LV_EVENT_FOCUSED`, etc) - */ -lv_event_code_t lv_event_get_code(lv_event_t * e); - -/** - * Get the parameter passed when the event was sent - * @param e pointer to the event descriptor - * @return pointer to the parameter - */ -void * lv_event_get_param(lv_event_t * e); - -/** - * Get the user_data passed when the event was registered on the object - * @param e pointer to the event descriptor - * @return pointer to the user_data - */ -void * lv_event_get_user_data(lv_event_t * e); - -/** - * Stop the event from bubbling. - * This is only valid when called in the middle of an event processing chain. - * @param e pointer to the event descriptor - */ -void lv_event_stop_bubbling(lv_event_t * e); - -/** - * Stop processing this event. - * This is only valid when called in the middle of an event processing chain. - * @param e pointer to the event descriptor - */ -void lv_event_stop_processing(lv_event_t * e); - -/** - * Register a new, custom event ID. - * It can be used the same way as e.g. `LV_EVENT_CLICKED` to send custom events - * @return the new event id - * @example - * uint32_t LV_EVENT_MINE = 0; - * ... - * e = lv_event_register_id(); - * ... - * lv_event_send(obj, LV_EVENT_MINE, &some_data); - */ -uint32_t lv_event_register_id(void); - -/** - * Nested events can be called and one of them might belong to an object that is being deleted. - * Mark this object's `event_temp_data` deleted to know that its `lv_event_send` should return `LV_RES_INV` - * @param obj pointer to an object to mark as deleted - */ -void _lv_event_mark_deleted(struct _lv_obj_t * obj); - - -/** - * Add an event handler function for an object. - * Used by the user to react on event which happens with the object. - * An object can have multiple event handler. They will be called in the same order as they were added. - * @param obj pointer to an object - * @param filter and event code (e.g. `LV_EVENT_CLICKED`) on which the event should be called. `LV_EVENT_ALL` can be sued the receive all the events. - * @param event_cb the new event function - * @param user_data custom data data will be available in `event_cb` - * @return a pointer the event descriptor. Can be used in ::lv_obj_remove_event_dsc - */ -struct _lv_event_dsc_t * lv_obj_add_event_cb(struct _lv_obj_t * obj, lv_event_cb_t event_cb, lv_event_code_t filter, - void * user_data); - -/** - * Remove an event handler function for an object. - * @param obj pointer to an object - * @param event_cb the event function to remove, or `NULL` to remove the firstly added event callback - * @return true if any event handlers were removed - */ -bool lv_obj_remove_event_cb(struct _lv_obj_t * obj, lv_event_cb_t event_cb); - -/** - * Remove an event handler function with a specific user_data from an object. - * @param obj pointer to an object - * @param event_cb the event function to remove, or `NULL` only `user_data` matters. - * @param event_user_data the user_data specified in ::lv_obj_add_event_cb - * @return true if any event handlers were removed - */ -bool lv_obj_remove_event_cb_with_user_data(struct _lv_obj_t * obj, lv_event_cb_t event_cb, - const void * event_user_data); - -/** - * DEPRECATED because doesn't work if multiple event handlers are added to an object. - * Remove an event handler function for an object. - * @param obj pointer to an object - * @param event_dsc pointer to an event descriptor to remove (returned by ::lv_obj_add_event_cb) - * @return true if any event handlers were removed - */ -bool lv_obj_remove_event_dsc(struct _lv_obj_t * obj, struct _lv_event_dsc_t * event_dsc); - -/** - * The user data of an event object event callback. Always the first match with `event_cb` will be returned. - * @param obj pointer to an object - * @param event_cb the event function - * @return the user_data - */ -void * lv_obj_get_event_user_data(struct _lv_obj_t * obj, lv_event_cb_t event_cb); - -/** - * Get the input device passed as parameter to indev related events. - * @param e pointer to an event - * @return the indev that triggered the event or NULL if called on a not indev related event - */ -lv_indev_t * lv_event_get_indev(lv_event_t * e); - -/** - * Get the part draw descriptor passed as parameter to `LV_EVENT_DRAW_PART_BEGIN/END`. - * @param e pointer to an event - * @return the part draw descriptor to hook the drawing or NULL if called on an unrelated event - */ -lv_obj_draw_part_dsc_t * lv_event_get_draw_part_dsc(lv_event_t * e); - -/** - * Get the draw context which should be the first parameter of the draw functions. - * Namely: `LV_EVENT_DRAW_MAIN/POST`, `LV_EVENT_DRAW_MAIN/POST_BEGIN`, `LV_EVENT_DRAW_MAIN/POST_END` - * @param e pointer to an event - * @return pointer to a draw context or NULL if called on an unrelated event - */ -lv_draw_ctx_t * lv_event_get_draw_ctx(lv_event_t * e); - -/** - * Get the old area of the object before its size was changed. Can be used in `LV_EVENT_SIZE_CHANGED` - * @param e pointer to an event - * @return the old absolute area of the object or NULL if called on an unrelated event - */ -const lv_area_t * lv_event_get_old_size(lv_event_t * e); - -/** - * Get the key passed as parameter to an event. Can be used in `LV_EVENT_KEY` - * @param e pointer to an event - * @return the triggering key or NULL if called on an unrelated event - */ -uint32_t lv_event_get_key(lv_event_t * e); - -/** - * Get the animation descriptor of a scrolling. Can be used in `LV_EVENT_SCROLL_BEGIN` - * @param e pointer to an event - * @return the animation that will scroll the object. (can be modified as required) - */ -lv_anim_t * lv_event_get_scroll_anim(lv_event_t * e); - -/** - * Set the new extra draw size. Can be used in `LV_EVENT_REFR_EXT_DRAW_SIZE` - * @param e pointer to an event - * @param size The new extra draw size - */ -void lv_event_set_ext_draw_size(lv_event_t * e, lv_coord_t size); - -/** - * Get a pointer to an `lv_point_t` variable in which the self size should be saved (width in `point->x` and height `point->y`). - * Can be used in `LV_EVENT_GET_SELF_SIZE` - * @param e pointer to an event - * @return pointer to `lv_point_t` or NULL if called on an unrelated event - */ -lv_point_t * lv_event_get_self_size_info(lv_event_t * e); - -/** - * Get a pointer to an `lv_hit_test_info_t` variable in which the hit test result should be saved. Can be used in `LV_EVENT_HIT_TEST` - * @param e pointer to an event - * @return pointer to `lv_hit_test_info_t` or NULL if called on an unrelated event - */ -lv_hit_test_info_t * lv_event_get_hit_test_info(lv_event_t * e); - -/** - * Get a pointer to an area which should be examined whether the object fully covers it or not. - * Can be used in `LV_EVENT_HIT_TEST` - * @param e pointer to an event - * @return an area with absolute coordinates to check - */ -const lv_area_t * lv_event_get_cover_area(lv_event_t * e); - -/** - * Set the result of cover checking. Can be used in `LV_EVENT_COVER_CHECK` - * @param e pointer to an event - * @param res an element of ::lv_cover_check_info_t - */ -void lv_event_set_cover_res(lv_event_t * e, lv_cover_res_t res); - -/********************** - * MACROS - **********************/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_EVENT_H*/ diff --git a/include/liblvgl/core/lv_global.h b/include/liblvgl/core/lv_global.h new file mode 100644 index 00000000..5d37b394 --- /dev/null +++ b/include/liblvgl/core/lv_global.h @@ -0,0 +1,266 @@ +/** + * @file lv_global.h + * + */ + +#ifndef LV_GLOBAL_H +#define LV_GLOBAL_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../lv_conf_internal.h" + +#include "../misc/lv_types.h" +#include "../draw/lv_draw.h" +#if LV_USE_DRAW_SW +#include "../draw/sw/lv_draw_sw.h" +#endif +#include "../misc/lv_anim.h" +#include "../misc/lv_area.h" +#include "../misc/lv_color_op.h" +#include "../misc/lv_ll.h" +#include "../misc/lv_log.h" +#include "../misc/lv_style.h" +#include "../misc/lv_timer.h" +#include "../osal/lv_os.h" +#include "../others/sysmon/lv_sysmon.h" +#include "../stdlib/builtin/lv_tlsf.h" + +#if LV_USE_FONT_COMPRESSED +#include "../font/lv_font_fmt_txt_private.h" +#endif + +#include "../tick/lv_tick.h" +#include "../layouts/lv_layout.h" + +#include "../misc/lv_types.h" + +#include "../misc/lv_timer_private.h" +#include "../misc/lv_anim_private.h" +#include "../tick/lv_tick_private.h" +#include "../draw/lv_draw_buf_private.h" +#include "../draw/lv_draw_private.h" +#include "../draw/sw/lv_draw_sw_private.h" +#include "../draw/sw/lv_draw_sw_mask_private.h" +#include "../stdlib/builtin/lv_tlsf_private.h" +#include "../others/sysmon/lv_sysmon_private.h" +#include "../layouts/lv_layout_private.h" + +/********************* + * DEFINES + *********************/ +#define ZERO_MEM_SENTINEL 0xa1b2c3d4 + +/********************** + * TYPEDEFS + **********************/ + +#if LV_USE_SPAN != 0 +struct _snippet_stack; +#endif + +#if LV_USE_FREETYPE +struct _lv_freetype_context_t; +#endif + +#if LV_USE_PROFILER && LV_USE_PROFILER_BUILTIN +struct _lv_profiler_builtin_ctx_t; +#endif + +#if LV_USE_NUTTX +struct _lv_nuttx_ctx_t; +#endif + +typedef struct _lv_global_t { + bool inited; + bool deinit_in_progress; /**< Can be used e.g. in the LV_EVENT_DELETE to deinit the drivers too */ + + lv_ll_t disp_ll; + lv_display_t * disp_refresh; + lv_display_t * disp_default; + + lv_ll_t style_trans_ll; + bool style_refresh; + uint32_t style_custom_table_size; + uint32_t style_last_custom_prop_id; + uint8_t * style_custom_prop_flag_lookup_table; + + lv_ll_t group_ll; + lv_group_t * group_default; + + lv_ll_t indev_ll; + lv_indev_t * indev_active; + lv_obj_t * indev_obj_active; + + uint32_t layout_count; + lv_layout_dsc_t * layout_list; + bool layout_update_mutex; + + uint32_t memory_zero; + uint32_t math_rand_seed; + + lv_event_t * event_header; + uint32_t event_last_register_id; + + lv_timer_state_t timer_state; + lv_anim_state_t anim_state; + lv_tick_state_t tick_state; + + lv_draw_buf_handlers_t draw_buf_handlers; + lv_draw_buf_handlers_t font_draw_buf_handlers; + lv_draw_buf_handlers_t image_cache_draw_buf_handlers; /**< Ensure that all assigned draw buffers + * can be managed by image cache. */ + + lv_ll_t img_decoder_ll; + + lv_cache_t * img_cache; + lv_cache_t * img_header_cache; + + lv_draw_global_info_t draw_info; +#if defined(LV_DRAW_SW_SHADOW_CACHE_SIZE) && LV_DRAW_SW_SHADOW_CACHE_SIZE > 0 + lv_draw_sw_shadow_cache_t sw_shadow_cache; +#endif +#if LV_DRAW_SW_COMPLEX + lv_draw_sw_mask_radius_circle_dsc_arr_t sw_circle_cache; +#endif + +#if LV_USE_LOG + lv_log_print_g_cb_t custom_log_print_cb; +#endif + +#if LV_USE_LOG && LV_LOG_USE_TIMESTAMP + uint32_t log_last_log_time; +#endif + +#if LV_USE_THEME_SIMPLE + void * theme_simple; +#endif + +#if LV_USE_THEME_DEFAULT + void * theme_default; +#endif + +#if LV_USE_THEME_MONO + void * theme_mono; +#endif + +#if LV_USE_STDLIB_MALLOC == LV_STDLIB_BUILTIN + lv_tlsf_state_t tlsf_state; +#endif + + lv_ll_t fsdrv_ll; +#if LV_USE_FS_STDIO != '\0' + lv_fs_drv_t stdio_fs_drv; +#endif +#if LV_USE_FS_POSIX + lv_fs_drv_t posix_fs_drv; +#endif + +#if LV_USE_FS_FATFS + lv_fs_drv_t fatfs_fs_drv; +#endif + +#if LV_USE_FS_WIN32 != '\0' + lv_fs_drv_t win32_fs_drv; +#endif + +#if LV_USE_FS_LITTLEFS + lv_fs_drv_t littlefs_fs_drv; +#endif + +#if LV_USE_FS_ARDUINO_ESP_LITTLEFS + lv_fs_drv_t arduino_esp_littlefs_fs_drv; +#endif + +#if LV_USE_FS_ARDUINO_SD + lv_fs_drv_t arduino_sd_fs_drv; +#endif + +#if LV_USE_FREETYPE + struct _lv_freetype_context_t * ft_context; +#endif + +#if LV_USE_FONT_COMPRESSED + lv_font_fmt_rle_t font_fmt_rle; +#endif + +#if LV_USE_SPAN != 0 + struct _snippet_stack * span_snippet_stack; +#endif + +#if LV_USE_PROFILER && LV_USE_PROFILER_BUILTIN + struct _lv_profiler_builtin_ctx_t * profiler_context; +#endif + +#if LV_USE_FILE_EXPLORER != 0 + lv_style_t fe_list_button_style; +#endif + +#if LV_USE_MEM_MONITOR + lv_sysmon_backend_data_t sysmon_mem; +#endif + +#if LV_USE_IME_PINYIN != 0 + size_t ime_cand_len; +#endif + +#if LV_USE_OBJ_ID_BUILTIN + void * objid_array; + uint32_t objid_count; +#endif + +#if LV_USE_NUTTX + struct _lv_nuttx_ctx_t * nuttx_ctx; +#endif + +#if LV_USE_OS != LV_OS_NONE + lv_mutex_t lv_general_mutex; +#endif + +#if LV_USE_OS == LV_OS_FREERTOS + uint32_t freertos_idle_time_sum; + uint32_t freertos_non_idle_time_sum; + uint32_t freertos_task_switch_timestamp; + bool freertos_idle_task_running; +#endif + + + void * user_data; +} lv_global_t; + +/********************** + * MACROS + **********************/ + +#if LV_ENABLE_GLOBAL_CUSTOM +#include LV_GLOBAL_CUSTOM_INCLUDE + +#ifndef LV_GLOBAL_CUSTOM +#define LV_GLOBAL_CUSTOM() lv_global_default() +#endif +#define LV_GLOBAL_DEFAULT() LV_GLOBAL_CUSTOM() +#else +LV_ATTRIBUTE_EXTERN_DATA extern lv_global_t lv_global; +#define LV_GLOBAL_DEFAULT() (&lv_global) +#endif + +/********************** + * GLOBAL PROTOTYPES + **********************/ +#if LV_ENABLE_GLOBAL_CUSTOM +/** + * Get the default global object for current thread + * @return pointer to the default global object + */ +lv_global_t * lv_global_default(void); +#endif +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_GLOBAL_H*/ diff --git a/include/liblvgl/core/lv_group.h b/include/liblvgl/core/lv_group.h index 85df325a..e5f267d0 100644 --- a/include/liblvgl/core/lv_group.h +++ b/include/liblvgl/core/lv_group.h @@ -13,20 +13,16 @@ extern "C" { /********************* * INCLUDES *********************/ +#include "../lv_conf_internal.h" -#include "liblvgl/lv_conf_internal.h" - -#include -#include -#include "liblvgl/misc/lv_ll.h" -#include "liblvgl/misc/lv_types.h" +#include "../misc/lv_types.h" +#include "../misc/lv_ll.h" /********************* * DEFINES *********************/ -/*Predefined keys to control the focused object via lv_group_send(group, c)*/ - -enum { +/** Predefined keys to control focused object via lv_group_send(group, c) */ +typedef enum { LV_KEY_UP = 17, /*0x11*/ LV_KEY_DOWN = 18, /*0x12*/ LV_KEY_RIGHT = 19, /*0x13*/ @@ -39,44 +35,14 @@ enum { LV_KEY_PREV = 11, /*0x0B, '*/ LV_KEY_HOME = 2, /*0x02, STX*/ LV_KEY_END = 3, /*0x03, ETX*/ -}; -typedef uint8_t lv_key_t; +} lv_key_t; /********************** * TYPEDEFS **********************/ -struct _lv_obj_t; -struct _lv_group_t; - -typedef void (*lv_group_focus_cb_t)(struct _lv_group_t *); -typedef void (*lv_group_edge_cb_t)(struct _lv_group_t *, bool); - -/** - * Groups can be used to logically hold objects so that they can be individually focused. - * They are NOT for laying out objects on a screen (try layouts for that). - */ -typedef struct _lv_group_t { - lv_ll_t obj_ll; /**< Linked list to store the objects in the group*/ - struct _lv_obj_t ** obj_focus; /**< The object in focus*/ - - lv_group_focus_cb_t focus_cb; /**< A function to call when a new object is focused (optional)*/ - lv_group_edge_cb_t edge_cb; /**< A function to call when an edge is reached, no more focus - targets are available in this direction (to allow edge feedback - like a sound or a scroll bounce) */ - -#if LV_USE_USER_DATA - void * user_data; -#endif - - uint8_t frozen : 1; /**< 1: can't focus to new object*/ - uint8_t editing : 1; /**< 1: Edit mode, 0: Navigate mode*/ - uint8_t refocus_policy : 1; /**< 1: Focus prev if focused on deletion. 0: Focus next if focused on - deletion.*/ - uint8_t wrap : 1; /**< 1: Focus next/prev can wrap at end of list. 0: Focus next/prev stops at end - of list.*/ -} lv_group_t; - +typedef void (*lv_group_focus_cb_t)(lv_group_t *); +typedef void (*lv_group_edge_cb_t)(lv_group_t *, bool); typedef enum { LV_GROUP_REFOCUS_POLICY_NEXT = 0, @@ -87,12 +53,6 @@ typedef enum { * GLOBAL PROTOTYPES **********************/ -/** - * Init. the group module - * @remarks Internal function, do not call directly. - */ -void _lv_group_init(void); - /** * Create a new object group * @return pointer to the new object group @@ -103,7 +63,7 @@ lv_group_t * lv_group_create(void); * Delete a group object * @param group pointer to a group */ -void lv_group_del(lv_group_t * group); +void lv_group_delete(lv_group_t * group); /** * Set a default group. New object are added to this group if it's enabled in their class with `add_to_def_group = true` @@ -122,20 +82,20 @@ lv_group_t * lv_group_get_default(void); * @param group pointer to a group * @param obj pointer to an object to add */ -void lv_group_add_obj(lv_group_t * group, struct _lv_obj_t * obj); +void lv_group_add_obj(lv_group_t * group, lv_obj_t * obj); /** * Swap 2 object in a group. The object must be in the same group * @param obj1 pointer to an object - * @param obj2 pointer to an other object + * @param obj2 pointer to another object */ -void lv_group_swap_obj(struct _lv_obj_t * obj1, struct _lv_obj_t * obj2); +void lv_group_swap_obj(lv_obj_t * obj1, lv_obj_t * obj2); /** * Remove an object from its group * @param obj pointer to an object to remove */ -void lv_group_remove_obj(struct _lv_obj_t * obj); +void lv_group_remove_obj(lv_obj_t * obj); /** * Remove all objects from a group @@ -147,7 +107,7 @@ void lv_group_remove_all_objs(lv_group_t * group); * Focus on an object (defocus the current) * @param obj pointer to an object to focus on */ -void lv_group_focus_obj(struct _lv_obj_t * obj); +void lv_group_focus_obj(lv_obj_t * obj); /** * Focus the next object in a group (defocus the current) @@ -174,7 +134,7 @@ void lv_group_focus_freeze(lv_group_t * group, bool en); * @param c a character (use LV_KEY_.. to navigate) * @return result of focused object in group. */ -lv_res_t lv_group_send_data(lv_group_t * group, uint32_t c); +lv_result_t lv_group_send_data(lv_group_t * group, uint32_t c); /** * Set a function for a group which will be called when a new object is focused @@ -190,7 +150,6 @@ void lv_group_set_focus_cb(lv_group_t * group, lv_group_focus_cb_t focus_cb); */ void lv_group_set_edge_cb(lv_group_t * group, lv_group_edge_cb_t edge_cb); - /** * Set whether the next or previous item in a group is focused if the currently focused obj is * deleted. @@ -218,7 +177,7 @@ void lv_group_set_wrap(lv_group_t * group, bool en); * @param group pointer to a group * @return pointer to the focused object */ -struct _lv_obj_t * lv_group_get_focused(const lv_group_t * group); +lv_obj_t * lv_group_get_focused(const lv_group_t * group); /** * Get the focus callback function of a group @@ -244,7 +203,6 @@ bool lv_group_get_editing(const lv_group_t * group); /** * Get whether focus next/prev will allow wrapping from first->last or last->first object. * @param group pointer to group - * @param en true: wrapping enabled; false: wrapping disabled */ bool lv_group_get_wrap(lv_group_t * group); @@ -255,6 +213,27 @@ bool lv_group_get_wrap(lv_group_t * group); */ uint32_t lv_group_get_obj_count(lv_group_t * group); +/** + * Get the nth object within a group + * @param group pointer to a group + * @param index index of object within the group + * @return pointer to the object + */ +lv_obj_t * lv_group_get_obj_by_index(lv_group_t * group, uint32_t index); + +/** + * Get the number of groups + * @return number of groups + */ +uint32_t lv_group_get_count(void); + +/** + * Get a group by its index + * @param index index of the group + * @return pointer to the group + */ +lv_group_t * lv_group_by_index(uint32_t index); + /********************** * MACROS **********************/ diff --git a/include/liblvgl/core/lv_group_private.h b/include/liblvgl/core/lv_group_private.h new file mode 100644 index 00000000..fe5f22b0 --- /dev/null +++ b/include/liblvgl/core/lv_group_private.h @@ -0,0 +1,75 @@ +/** + * @file lv_group_private.h + * + */ + +#ifndef LV_GROUP_PRIVATE_H +#define LV_GROUP_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_group.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** + * Groups can be used to logically hold objects so that they can be individually focused. + * They are NOT for laying out objects on a screen (try layouts for that). + */ +struct _lv_group_t { + lv_ll_t obj_ll; /**< Linked list to store the objects in the group*/ + lv_obj_t ** obj_focus; /**< The object in focus*/ + + lv_group_focus_cb_t focus_cb; /**< A function to call when a new object is focused (optional)*/ + lv_group_edge_cb_t edge_cb; /**< A function to call when an edge is reached, no more focus + targets are available in this direction (to allow edge feedback + like a sound or a scroll bounce) */ + + void * user_data; + + uint8_t frozen : 1; /**< 1: can't focus to new object*/ + uint8_t editing : 1; /**< 1: Edit mode, 0: Navigate mode*/ + uint8_t refocus_policy : 1; /**< 1: Focus prev if focused on deletion. 0: Focus next if focused on + deletion.*/ + uint8_t wrap : 1; /**< 1: Focus next/prev can wrap at end of list. 0: Focus next/prev stops at end + of list.*/ +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Init the group module + * @remarks Internal function, do not call directly. + */ +void lv_group_init(void); + +/** + * Deinit the group module + * @remarks Internal function, do not call directly. + */ +void lv_group_deinit(void); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_GROUP_PRIVATE_H*/ diff --git a/include/liblvgl/core/lv_indev.h b/include/liblvgl/core/lv_indev.h deleted file mode 100644 index 4692ef37..00000000 --- a/include/liblvgl/core/lv_indev.h +++ /dev/null @@ -1,176 +0,0 @@ -/** - * @file lv_indev.h - * - */ - -#ifndef LV_INDEV_H -#define LV_INDEV_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "lv_obj.h" -#include "liblvgl/hal/lv_hal_indev.h" -#include "lv_group.h" - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Called periodically to read the input devices - * @param timer pointer to a timer to read - */ -void lv_indev_read_timer_cb(lv_timer_t * timer); - -/** - * Enable or disable one or all input devices (default enabled) - * @param indev pointer to an input device or NULL to enable/disable all of them - * @param en true to enable, false to disable - */ -void lv_indev_enable(lv_indev_t * indev, bool en); - -/** - * Get the currently processed input device. Can be used in action functions too. - * @return pointer to the currently processed input device or NULL if no input device processing - * right now - */ -lv_indev_t * lv_indev_get_act(void); - -/** - * Get the type of an input device - * @param indev pointer to an input device - * @return the type of the input device from `lv_hal_indev_type_t` (`LV_INDEV_TYPE_...`) - */ -lv_indev_type_t lv_indev_get_type(const lv_indev_t * indev); - -/** - * Reset one or all input devices - * @param indev pointer to an input device to reset or NULL to reset all of them - * @param obj pointer to an object which triggers the reset. - */ -void lv_indev_reset(lv_indev_t * indev, lv_obj_t * obj); - -/** - * Reset the long press state of an input device - * @param indev pointer to an input device - */ -void lv_indev_reset_long_press(lv_indev_t * indev); - -/** - * Set a cursor for a pointer input device (for LV_INPUT_TYPE_POINTER and LV_INPUT_TYPE_BUTTON) - * @param indev pointer to an input device - * @param cur_obj pointer to an object to be used as cursor - */ -void lv_indev_set_cursor(lv_indev_t * indev, lv_obj_t * cur_obj); - -/** - * Set a destination group for a keypad input device (for LV_INDEV_TYPE_KEYPAD) - * @param indev pointer to an input device - * @param group point to a group - */ -void lv_indev_set_group(lv_indev_t * indev, lv_group_t * group); - -/** - * Set the an array of points for LV_INDEV_TYPE_BUTTON. - * These points will be assigned to the buttons to press a specific point on the screen - * @param indev pointer to an input device - * @param group point to a group - */ -void lv_indev_set_button_points(lv_indev_t * indev, const lv_point_t points[]); - -/** - * Get the last point of an input device (for LV_INDEV_TYPE_POINTER and LV_INDEV_TYPE_BUTTON) - * @param indev pointer to an input device - * @param point pointer to a point to store the result - */ -void lv_indev_get_point(const lv_indev_t * indev, lv_point_t * point); - -/** -* Get the current gesture direct -* @param indev pointer to an input device -* @return current gesture direct -*/ -lv_dir_t lv_indev_get_gesture_dir(const lv_indev_t * indev); - -/** - * Get the last pressed key of an input device (for LV_INDEV_TYPE_KEYPAD) - * @param indev pointer to an input device - * @return the last pressed key (0 on error) - */ -uint32_t lv_indev_get_key(const lv_indev_t * indev); - -/** - * Check the current scroll direction of an input device (for LV_INDEV_TYPE_POINTER and - * LV_INDEV_TYPE_BUTTON) - * @param indev pointer to an input device - * @return LV_DIR_NONE: no scrolling now - * LV_DIR_HOR/VER - */ -lv_dir_t lv_indev_get_scroll_dir(const lv_indev_t * indev); - -/** - * Get the currently scrolled object (for LV_INDEV_TYPE_POINTER and - * LV_INDEV_TYPE_BUTTON) - * @param indev pointer to an input device - * @return pointer to the currently scrolled object or NULL if no scrolling by this indev - */ -lv_obj_t * lv_indev_get_scroll_obj(const lv_indev_t * indev); - -/** - * Get the movement vector of an input device (for LV_INDEV_TYPE_POINTER and - * LV_INDEV_TYPE_BUTTON) - * @param indev pointer to an input device - * @param point pointer to a point to store the types.pointer.vector - */ -void lv_indev_get_vect(const lv_indev_t * indev, lv_point_t * point); - -/** - * Do nothing until the next release - * @param indev pointer to an input device - */ -void lv_indev_wait_release(lv_indev_t * indev); - -/** - * Gets a pointer to the currently active object in the currently processed input device. - * @return pointer to currently active object or NULL if no active object - */ -lv_obj_t * lv_indev_get_obj_act(void); - -/** - * Get a pointer to the indev read timer to - * modify its parameters with `lv_timer_...` functions. - * @param indev pointer to an input device - * @return pointer to the indev read refresher timer. (NULL on error) - */ -lv_timer_t * lv_indev_get_read_timer(lv_disp_t * indev); - -/** - * Search the most top, clickable object by a point - * @param obj pointer to a start object, typically the screen - * @param point pointer to a point for searching the most top child - * @return pointer to the found object or NULL if there was no suitable object - */ -lv_obj_t * lv_indev_search_obj(lv_obj_t * obj, lv_point_t * point); - -/********************** - * MACROS - **********************/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_INDEV_H*/ diff --git a/include/liblvgl/core/lv_obj.h b/include/liblvgl/core/lv_obj.h index c89e4609..4ae648ff 100644 --- a/include/liblvgl/core/lv_obj.h +++ b/include/liblvgl/core/lv_obj.h @@ -15,14 +15,21 @@ extern "C" { *********************/ #include "../lv_conf_internal.h" -#include -#include -#include "../misc/lv_style.h" #include "../misc/lv_types.h" +#include "../misc/lv_style.h" #include "../misc/lv_area.h" #include "../misc/lv_color.h" #include "../misc/lv_assert.h" -#include "../hal/lv_hal.h" + +#include "lv_obj_tree.h" +#include "lv_obj_pos.h" +#include "lv_obj_scroll.h" +#include "lv_obj_style.h" +#include "lv_obj_draw.h" +#include "lv_obj_class.h" +#include "lv_obj_event.h" +#include "lv_obj_property.h" +#include "lv_group.h" /********************* * DEFINES @@ -32,8 +39,6 @@ extern "C" { * TYPEDEFS **********************/ -struct _lv_obj_t; - /** * Possible states of a widget. * OR-ed values are possible @@ -48,7 +53,6 @@ enum { LV_STATE_PRESSED = 0x0020, LV_STATE_SCROLLED = 0x0040, LV_STATE_DISABLED = 0x0080, - LV_STATE_USER_1 = 0x1000, LV_STATE_USER_2 = 0x2000, LV_STATE_USER_3 = 0x4000, @@ -57,14 +61,13 @@ enum { LV_STATE_ANY = 0xFFFF, /**< Special value can be used in some functions to target all states*/ }; -typedef uint16_t lv_state_t; - /** * The possible parts of widgets. * The parts can be considered as the internal building block of the widgets. * E.g. slider = background + indicator + knob * Not all parts are used by every widget */ + enum { LV_PART_MAIN = 0x000000, /**< A background like rectangle*/ LV_PART_SCROLLBAR = 0x010000, /**< The scrollbar(s)*/ @@ -72,21 +75,21 @@ enum { LV_PART_KNOB = 0x030000, /**< Like handle to grab to adjust the value*/ LV_PART_SELECTED = 0x040000, /**< Indicate the currently selected option or section*/ LV_PART_ITEMS = 0x050000, /**< Used if the widget has multiple similar elements (e.g. table cells)*/ - LV_PART_TICKS = 0x060000, /**< Ticks on scale e.g. for a chart or meter*/ - LV_PART_CURSOR = 0x070000, /**< Mark a specific place e.g. for text area's cursor or on a chart*/ + LV_PART_CURSOR = 0x060000, /**< Mark a specific place e.g. for text area's cursor or on a chart*/ LV_PART_CUSTOM_FIRST = 0x080000, /**< Extension point for custom widgets*/ LV_PART_ANY = 0x0F0000, /**< Special value can be used in some functions to target all parts*/ }; -typedef uint32_t lv_part_t; - /** * On/Off features controlling the object's behavior. * OR-ed values are possible + * + * Note: update obj flags corresponding properties below + * whenever add/remove flags or change bit definition of flags. */ -enum { +typedef enum { LV_OBJ_FLAG_HIDDEN = (1L << 0), /**< Make the object hidden. (Like it wasn't there at all)*/ LV_OBJ_FLAG_CLICKABLE = (1L << 1), /**< Make the object clickable by the input devices*/ LV_OBJ_FLAG_CLICK_FOCUSABLE = (1L << 2), /**< Add focused state to the object when clicked*/ @@ -105,9 +108,13 @@ enum { LV_OBJ_FLAG_EVENT_BUBBLE = (1L << 14), /**< Propagate the events to the parent too*/ LV_OBJ_FLAG_GESTURE_BUBBLE = (1L << 15), /**< Propagate the gestures to the parent*/ LV_OBJ_FLAG_ADV_HITTEST = (1L << 16), /**< Allow performing more accurate hit (click) test. E.g. consider rounded corners.*/ - LV_OBJ_FLAG_IGNORE_LAYOUT = (1L << 17), /**< Make the object position-able by the layouts*/ + LV_OBJ_FLAG_IGNORE_LAYOUT = (1L << 17), /**< Make the object not positioned by the layouts*/ LV_OBJ_FLAG_FLOATING = (1L << 18), /**< Do not scroll the object when the parent scrolls and ignore layout*/ - LV_OBJ_FLAG_OVERFLOW_VISIBLE = (1L << 19), /**< Do not clip the children's content to the parent's boundary*/ + LV_OBJ_FLAG_SEND_DRAW_TASK_EVENTS = (1L << 19), /**< Send `LV_EVENT_DRAW_TASK_ADDED` events*/ + LV_OBJ_FLAG_OVERFLOW_VISIBLE = (1L << 20),/**< Do not clip the children to the parent's ext draw size*/ +#if LV_USE_FLEX + LV_OBJ_FLAG_FLEX_IN_NEW_TRACK = (1L << 21), /**< Start a new flex track on this item*/ +#endif LV_OBJ_FLAG_LAYOUT_1 = (1L << 23), /**< Custom flag, free to use by layouts*/ LV_OBJ_FLAG_LAYOUT_2 = (1L << 24), /**< Custom flag, free to use by layouts*/ @@ -118,104 +125,102 @@ enum { LV_OBJ_FLAG_USER_2 = (1L << 28), /**< Custom flag, free to use by user*/ LV_OBJ_FLAG_USER_3 = (1L << 29), /**< Custom flag, free to use by user*/ LV_OBJ_FLAG_USER_4 = (1L << 30), /**< Custom flag, free to use by user*/ +} lv_obj_flag_t; +#if LV_USE_OBJ_PROPERTY +enum { + /*OBJ flag properties */ + LV_PROPERTY_ID(OBJ, FLAG_START, LV_PROPERTY_TYPE_INT, 0), + LV_PROPERTY_ID(OBJ, FLAG_HIDDEN, LV_PROPERTY_TYPE_INT, 0), + LV_PROPERTY_ID(OBJ, FLAG_CLICKABLE, LV_PROPERTY_TYPE_INT, 1), + LV_PROPERTY_ID(OBJ, FLAG_CLICK_FOCUSABLE, LV_PROPERTY_TYPE_INT, 2), + LV_PROPERTY_ID(OBJ, FLAG_CHECKABLE, LV_PROPERTY_TYPE_INT, 3), + LV_PROPERTY_ID(OBJ, FLAG_SCROLLABLE, LV_PROPERTY_TYPE_INT, 4), + LV_PROPERTY_ID(OBJ, FLAG_SCROLL_ELASTIC, LV_PROPERTY_TYPE_INT, 5), + LV_PROPERTY_ID(OBJ, FLAG_SCROLL_MOMENTUM, LV_PROPERTY_TYPE_INT, 6), + LV_PROPERTY_ID(OBJ, FLAG_SCROLL_ONE, LV_PROPERTY_TYPE_INT, 7), + LV_PROPERTY_ID(OBJ, FLAG_SCROLL_CHAIN_HOR, LV_PROPERTY_TYPE_INT, 8), + LV_PROPERTY_ID(OBJ, FLAG_SCROLL_CHAIN_VER, LV_PROPERTY_TYPE_INT, 9), + LV_PROPERTY_ID(OBJ, FLAG_SCROLL_ON_FOCUS, LV_PROPERTY_TYPE_INT, 10), + LV_PROPERTY_ID(OBJ, FLAG_SCROLL_WITH_ARROW, LV_PROPERTY_TYPE_INT, 11), + LV_PROPERTY_ID(OBJ, FLAG_SNAPPABLE, LV_PROPERTY_TYPE_INT, 12), + LV_PROPERTY_ID(OBJ, FLAG_PRESS_LOCK, LV_PROPERTY_TYPE_INT, 13), + LV_PROPERTY_ID(OBJ, FLAG_EVENT_BUBBLE, LV_PROPERTY_TYPE_INT, 14), + LV_PROPERTY_ID(OBJ, FLAG_GESTURE_BUBBLE, LV_PROPERTY_TYPE_INT, 15), + LV_PROPERTY_ID(OBJ, FLAG_ADV_HITTEST, LV_PROPERTY_TYPE_INT, 16), + LV_PROPERTY_ID(OBJ, FLAG_IGNORE_LAYOUT, LV_PROPERTY_TYPE_INT, 17), + LV_PROPERTY_ID(OBJ, FLAG_FLOATING, LV_PROPERTY_TYPE_INT, 18), + LV_PROPERTY_ID(OBJ, FLAG_SEND_DRAW_TASK_EVENTS, LV_PROPERTY_TYPE_INT, 19), + LV_PROPERTY_ID(OBJ, FLAG_OVERFLOW_VISIBLE, LV_PROPERTY_TYPE_INT, 20), + LV_PROPERTY_ID(OBJ, FLAG_FLEX_IN_NEW_TRACK, LV_PROPERTY_TYPE_INT, 21), + LV_PROPERTY_ID(OBJ, FLAG_LAYOUT_1, LV_PROPERTY_TYPE_INT, 23), + LV_PROPERTY_ID(OBJ, FLAG_LAYOUT_2, LV_PROPERTY_TYPE_INT, 24), + LV_PROPERTY_ID(OBJ, FLAG_WIDGET_1, LV_PROPERTY_TYPE_INT, 25), + LV_PROPERTY_ID(OBJ, FLAG_WIDGET_2, LV_PROPERTY_TYPE_INT, 26), + LV_PROPERTY_ID(OBJ, FLAG_USER_1, LV_PROPERTY_TYPE_INT, 27), + LV_PROPERTY_ID(OBJ, FLAG_USER_2, LV_PROPERTY_TYPE_INT, 28), + LV_PROPERTY_ID(OBJ, FLAG_USER_3, LV_PROPERTY_TYPE_INT, 29), + LV_PROPERTY_ID(OBJ, FLAG_USER_4, LV_PROPERTY_TYPE_INT, 30), + LV_PROPERTY_ID(OBJ, FLAG_END, LV_PROPERTY_TYPE_INT, 30), + + LV_PROPERTY_ID(OBJ, STATE_START, LV_PROPERTY_TYPE_INT, 31), + LV_PROPERTY_ID(OBJ, STATE_CHECKED, LV_PROPERTY_TYPE_INT, 31), + LV_PROPERTY_ID(OBJ, STATE_FOCUSED, LV_PROPERTY_TYPE_INT, 32), + LV_PROPERTY_ID(OBJ, STATE_FOCUS_KEY, LV_PROPERTY_TYPE_INT, 33), + LV_PROPERTY_ID(OBJ, STATE_EDITED, LV_PROPERTY_TYPE_INT, 34), + LV_PROPERTY_ID(OBJ, STATE_HOVERED, LV_PROPERTY_TYPE_INT, 35), + LV_PROPERTY_ID(OBJ, STATE_PRESSED, LV_PROPERTY_TYPE_INT, 36), + LV_PROPERTY_ID(OBJ, STATE_SCROLLED, LV_PROPERTY_TYPE_INT, 37), + LV_PROPERTY_ID(OBJ, STATE_DISABLED, LV_PROPERTY_TYPE_INT, 38), + /*not used bit8-bit11*/ + LV_PROPERTY_ID(OBJ, STATE_USER_1, LV_PROPERTY_TYPE_INT, 43), + LV_PROPERTY_ID(OBJ, STATE_USER_2, LV_PROPERTY_TYPE_INT, 44), + LV_PROPERTY_ID(OBJ, STATE_USER_3, LV_PROPERTY_TYPE_INT, 45), + LV_PROPERTY_ID(OBJ, STATE_USER_4, LV_PROPERTY_TYPE_INT, 46), + LV_PROPERTY_ID(OBJ, STATE_ANY, LV_PROPERTY_TYPE_INT, 47), + LV_PROPERTY_ID(OBJ, STATE_END, LV_PROPERTY_TYPE_INT, 47), + + /*OBJ normal properties*/ + LV_PROPERTY_ID(OBJ, PARENT, LV_PROPERTY_TYPE_OBJ, 48), + LV_PROPERTY_ID(OBJ, X, LV_PROPERTY_TYPE_INT, 49), + LV_PROPERTY_ID(OBJ, Y, LV_PROPERTY_TYPE_INT, 50), + LV_PROPERTY_ID(OBJ, W, LV_PROPERTY_TYPE_INT, 51), + LV_PROPERTY_ID(OBJ, H, LV_PROPERTY_TYPE_INT, 52), + LV_PROPERTY_ID(OBJ, CONTENT_WIDTH, LV_PROPERTY_TYPE_INT, 53), + LV_PROPERTY_ID(OBJ, CONTENT_HEIGHT, LV_PROPERTY_TYPE_INT, 54), + LV_PROPERTY_ID(OBJ, LAYOUT, LV_PROPERTY_TYPE_INT, 55), + LV_PROPERTY_ID(OBJ, ALIGN, LV_PROPERTY_TYPE_INT, 56), + LV_PROPERTY_ID(OBJ, SCROLLBAR_MODE, LV_PROPERTY_TYPE_INT, 57), + LV_PROPERTY_ID(OBJ, SCROLL_DIR, LV_PROPERTY_TYPE_INT, 58), + LV_PROPERTY_ID(OBJ, SCROLL_SNAP_X, LV_PROPERTY_TYPE_INT, 59), + LV_PROPERTY_ID(OBJ, SCROLL_SNAP_Y, LV_PROPERTY_TYPE_INT, 60), + LV_PROPERTY_ID(OBJ, SCROLL_X, LV_PROPERTY_TYPE_INT, 61), + LV_PROPERTY_ID(OBJ, SCROLL_Y, LV_PROPERTY_TYPE_INT, 62), + LV_PROPERTY_ID(OBJ, SCROLL_TOP, LV_PROPERTY_TYPE_INT, 63), + LV_PROPERTY_ID(OBJ, SCROLL_BOTTOM, LV_PROPERTY_TYPE_INT, 64), + LV_PROPERTY_ID(OBJ, SCROLL_LEFT, LV_PROPERTY_TYPE_INT, 65), + LV_PROPERTY_ID(OBJ, SCROLL_RIGHT, LV_PROPERTY_TYPE_INT, 66), + LV_PROPERTY_ID(OBJ, SCROLL_END, LV_PROPERTY_TYPE_INT, 67), + LV_PROPERTY_ID(OBJ, EXT_DRAW_SIZE, LV_PROPERTY_TYPE_INT, 68), + LV_PROPERTY_ID(OBJ, EVENT_COUNT, LV_PROPERTY_TYPE_INT, 69), + LV_PROPERTY_ID(OBJ, SCREEN, LV_PROPERTY_TYPE_OBJ, 70), + LV_PROPERTY_ID(OBJ, DISPLAY, LV_PROPERTY_TYPE_POINTER, 71), + LV_PROPERTY_ID(OBJ, CHILD_COUNT, LV_PROPERTY_TYPE_INT, 72), + LV_PROPERTY_ID(OBJ, INDEX, LV_PROPERTY_TYPE_INT, 73), + + LV_PROPERTY_OBJ_END, }; - - -typedef uint32_t lv_obj_flag_t; - -/** - * `type` field in `lv_obj_draw_part_dsc_t` if `class_p = lv_obj_class` - * Used in `LV_EVENT_DRAW_PART_BEGIN` and `LV_EVENT_DRAW_PART_END` - */ -typedef enum { - LV_OBJ_DRAW_PART_RECTANGLE, /**< The main rectangle*/ - LV_OBJ_DRAW_PART_BORDER_POST,/**< The border if style_border_post = true*/ - LV_OBJ_DRAW_PART_SCROLLBAR, /**< The scrollbar*/ -} lv_obj_draw_part_type_t; - -#include "lv_obj_tree.h" -#include "lv_obj_pos.h" -#include "lv_obj_scroll.h" -#include "lv_obj_style.h" -#include "lv_obj_draw.h" -#include "lv_obj_class.h" -#include "lv_event.h" -#include "lv_group.h" +#endif /** * Make the base object's class publicly available. */ -extern const lv_obj_class_t lv_obj_class; - -/** - * Special, rarely used attributes. - * They are allocated automatically if any elements is set. - */ -typedef struct { - struct _lv_obj_t ** children; /**< Store the pointer of the children in an array.*/ - uint32_t child_cnt; /**< Number of children*/ - lv_group_t * group_p; - - struct _lv_event_dsc_t * event_dsc; /**< Dynamically allocated event callback and user data array*/ - lv_point_t scroll; /**< The current X/Y scroll offset*/ - - lv_coord_t ext_click_pad; /**< Extra click padding in all direction*/ - lv_coord_t ext_draw_size; /**< EXTend the size in every direction for drawing.*/ - - lv_scrollbar_mode_t scrollbar_mode : 2; /**< How to display scrollbars*/ - lv_scroll_snap_t scroll_snap_x : 2; /**< Where to align the snappable children horizontally*/ - lv_scroll_snap_t scroll_snap_y : 2; /**< Where to align the snappable children vertically*/ - lv_dir_t scroll_dir : 4; /**< The allowed scroll direction(s)*/ - uint8_t event_dsc_cnt : 6; /**< Number of event callbacks stored in `event_dsc` array*/ - uint8_t layer_type : 2; /**< Cache the layer type here. Element of @lv_intermediate_layer_type_t */ -} _lv_obj_spec_attr_t; - -typedef struct _lv_obj_t { - const lv_obj_class_t * class_p; - struct _lv_obj_t * parent; - _lv_obj_spec_attr_t * spec_attr; - _lv_obj_style_t * styles; -#if LV_USE_USER_DATA - void * user_data; -#endif - lv_area_t coords; - lv_obj_flag_t flags; - lv_state_t state; - uint16_t layout_inv : 1; - uint16_t scr_layout_inv : 1; - uint16_t skip_trans : 1; - uint16_t style_cnt : 6; - uint16_t h_layout : 1; - uint16_t w_layout : 1; -} lv_obj_t; - +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_obj_class; /********************** * GLOBAL PROTOTYPES **********************/ -/** - * Initialize LVGL library. - * Should be called before any other LVGL related function. - */ -void lv_init(void); - -#if LV_ENABLE_GC || !LV_MEM_CUSTOM - -/** - * Deinit the 'lv' library - * Currently only implemented when not using custom allocators, or GC is enabled. - */ -void lv_deinit(void); - -#endif - -/** - * Returns whether the 'lv' library is currently initialized - */ -bool lv_is_initialized(void); - /** * Create a base object (a rectangle) * @param parent pointer to a parent object. If NULL then a screen will be created. @@ -223,7 +228,6 @@ bool lv_is_initialized(void); */ lv_obj_t * lv_obj_create(lv_obj_t * parent); - /*===================== * Setter functions *====================*/ @@ -231,17 +235,24 @@ lv_obj_t * lv_obj_create(lv_obj_t * parent); /** * Set one or more flags * @param obj pointer to an object - * @param f R-ed values from `lv_obj_flag_t` to set. + * @param f OR-ed values from `lv_obj_flag_t` to set. */ void lv_obj_add_flag(lv_obj_t * obj, lv_obj_flag_t f); /** - * Clear one or more flags + * Remove one or more flags * @param obj pointer to an object - * @param f OR-ed values from `lv_obj_flag_t` to set. + * @param f OR-ed values from `lv_obj_flag_t` to clear. */ -void lv_obj_clear_flag(lv_obj_t * obj, lv_obj_flag_t f); +void lv_obj_remove_flag(lv_obj_t * obj, lv_obj_flag_t f); +/** + * Set add or remove one or more flags. + * @param obj pointer to an object + * @param f OR-ed values from `lv_obj_flag_t` to update. + * @param v true: add the flags; false: remove the flags + */ +void lv_obj_update_flag(lv_obj_t * obj, lv_obj_flag_t f, bool v); /** * Add one or more states to the object. The other state bits will remain unchanged. @@ -257,19 +268,22 @@ void lv_obj_add_state(lv_obj_t * obj, lv_state_t state); * @param obj pointer to an object * @param state the states to add. E.g `LV_STATE_PRESSED | LV_STATE_FOCUSED` */ -void lv_obj_clear_state(lv_obj_t * obj, lv_state_t state); +void lv_obj_remove_state(lv_obj_t * obj, lv_state_t state); + +/** + * Add or remove one or more states to the object. The other state bits will remain unchanged. + * @param obj pointer to an object + * @param state the states to add. E.g `LV_STATE_PRESSED | LV_STATE_FOCUSED` + * @param v true: add the states; false: remove the states + */ +void lv_obj_set_state(lv_obj_t * obj, lv_state_t state, bool v); /** * Set the user_data field of the object * @param obj pointer to an object * @param user_data pointer to the new user_data. */ -#if LV_USE_USER_DATA -static inline void lv_obj_set_user_data(lv_obj_t * obj, void * user_data) -{ - obj->user_data = user_data; -} -#endif +void lv_obj_set_user_data(lv_obj_t * obj, void * user_data); /*======================= * Getter functions @@ -287,7 +301,7 @@ bool lv_obj_has_flag(const lv_obj_t * obj, lv_obj_flag_t f); * Check if a given flag or any of the flags are set on an object. * @param obj pointer to an object * @param f the flag(s) to check (OR-ed values can be used) - * @return true: at lest one flag flag is set; false: none of the flags are set + * @return true: at least one flag is set; false: none of the flags are set */ bool lv_obj_has_flag_any(const lv_obj_t * obj, lv_obj_flag_t f); @@ -311,19 +325,14 @@ bool lv_obj_has_state(const lv_obj_t * obj, lv_state_t state); * @param obj pointer to an object * @return the pointer to group of the object */ -void * lv_obj_get_group(const lv_obj_t * obj); +lv_group_t * lv_obj_get_group(const lv_obj_t * obj); /** * Get the user_data field of the object * @param obj pointer to an object * @return the pointer to the user_data of the object */ -#if LV_USE_USER_DATA -static inline void * lv_obj_get_user_data(lv_obj_t * obj) -{ - return obj->user_data; -} -#endif +void * lv_obj_get_user_data(lv_obj_t * obj); /*======================= * Other functions @@ -367,18 +376,87 @@ const lv_obj_class_t * lv_obj_get_class(const lv_obj_t * obj); bool lv_obj_is_valid(const lv_obj_t * obj); /** - * Scale the given number of pixels (a distance or size) relative to a 160 DPI display - * considering the DPI of the `obj`'s display. - * It ensures that e.g. `lv_dpx(100)` will have the same physical size regardless to the - * DPI of the display. - * @param obj an object whose display's dpi should be considered - * @param n the number of pixels to scale - * @return `n x current_dpi/160` + * Utility to set an object reference to NULL when it gets deleted. + * The reference should be in a location that will not become invalid + * during the object's lifetime, i.e. static or allocated. + * @param obj_ptr a pointer to a pointer to an object */ -static inline lv_coord_t lv_obj_dpx(const lv_obj_t * obj, lv_coord_t n) -{ - return _LV_DPX_CALC(lv_disp_get_dpi(lv_obj_get_disp(obj)), n); -} +void lv_obj_null_on_delete(lv_obj_t ** obj_ptr); + +#if LV_USE_OBJ_ID +/** + * Set an id for an object. + * @param obj pointer to an object + * @param id the id of the object + */ +void lv_obj_set_id(lv_obj_t * obj, void * id); + +/** + * Get the id of an object. + * @param obj pointer to an object + * @return the id of the object + */ +void * lv_obj_get_id(const lv_obj_t * obj); + +/** + * Get the child object by its id. + * It will check children and grandchildren recursively. + * Function `lv_obj_id_compare` is used to matched obj id with given id. + * + * @param obj pointer to an object + * @param id the id of the child object + * @return pointer to the child object or NULL if not found + */ +lv_obj_t * lv_obj_get_child_by_id(const lv_obj_t * obj, const void * id); + +/** + * Assign id to object if not previously assigned. + * This function gets called automatically when LV_OBJ_ID_AUTO_ASSIGN is enabled. + * + * Set `LV_USE_OBJ_ID_BUILTIN` to use the builtin method to generate object ID. + * Otherwise, these functions including `lv_obj_[set|assign|free|stringify]_id` and + * `lv_obj_id_compare`should be implemented externally. + * + * @param class_p the class this obj belongs to. Note obj->class_p is the class currently being constructed. + * @param obj pointer to an object + */ +void lv_obj_assign_id(const lv_obj_class_t * class_p, lv_obj_t * obj); + +/** + * Free resources allocated by `lv_obj_assign_id` or `lv_obj_set_id`. + * This function is also called automatically when object is deleted. + * @param obj pointer to an object + */ +void lv_obj_free_id(lv_obj_t * obj); + +/** + * Compare two obj id, return 0 if they are equal. + * + * Set `LV_USE_OBJ_ID_BUILTIN` to use the builtin method for compare. + * Otherwise, it must be implemented externally. + * + * @param id1: the first id + * @param id2: the second id + * @return 0 if they are equal, non-zero otherwise. + */ +int lv_obj_id_compare(const void * id1, const void * id2); + +/** + * Format an object's id into a string. + * @param obj pointer to an object + * @param buf buffer to write the string into + * @param len length of the buffer + */ +const char * lv_obj_stringify_id(lv_obj_t * obj, char * buf, uint32_t len); + +#if LV_USE_OBJ_ID_BUILTIN +/** + * Free resources used by builtin ID generator. + */ +void lv_objid_builtin_destroy(void); +#endif + +#endif /*LV_USE_OBJ_ID*/ /********************** * MACROS @@ -401,7 +479,6 @@ static inline lv_coord_t lv_obj_dpx(const lv_obj_t * obj, lv_coord_t n) # define LV_TRACE_OBJ_CREATE(...) #endif - #ifdef __cplusplus } /*extern "C"*/ #endif diff --git a/include/liblvgl/core/lv_obj_class.h b/include/liblvgl/core/lv_obj_class.h index 01a7248b..4c3bdcb2 100644 --- a/include/liblvgl/core/lv_obj_class.h +++ b/include/liblvgl/core/lv_obj_class.h @@ -13,22 +13,18 @@ extern "C" { /********************* * INCLUDES *********************/ -#include -#include +#include "../misc/lv_types.h" +#include "../misc/lv_area.h" +#include "lv_obj_property.h" /********************* * DEFINES *********************/ - /********************** * TYPEDEFS **********************/ -struct _lv_obj_t; -struct _lv_obj_class_t; -struct _lv_event_t; - typedef enum { LV_OBJ_CLASS_EDITABLE_INHERIT, /**< Check the base class. Must have 0 value to let zero initialized class inherit*/ LV_OBJ_CLASS_EDITABLE_TRUE, @@ -41,27 +37,12 @@ typedef enum { LV_OBJ_CLASS_GROUP_DEF_FALSE, } lv_obj_class_group_def_t; -typedef void (*lv_obj_class_event_cb_t)(struct _lv_obj_class_t * class_p, struct _lv_event_t * e); -/** - * Describe the common methods of every object. - * Similar to a C++ class. - */ -typedef struct _lv_obj_class_t { - const struct _lv_obj_class_t * base_class; - void (*constructor_cb)(const struct _lv_obj_class_t * class_p, struct _lv_obj_t * obj); - void (*destructor_cb)(const struct _lv_obj_class_t * class_p, struct _lv_obj_t * obj); -#if LV_USE_USER_DATA - void * user_data; -#endif - void (*event_cb)(const struct _lv_obj_class_t * class_p, - struct _lv_event_t * e); /**< Widget type specific event function*/ - lv_coord_t width_def; - lv_coord_t height_def; - uint32_t editable : 2; /**< Value from ::lv_obj_class_editable_t*/ - uint32_t group_def : 2; /**< Value from ::lv_obj_class_group_def_t*/ - uint32_t instance_size : 16; -} lv_obj_class_t; +typedef enum { + LV_OBJ_CLASS_THEME_INHERITABLE_FALSE, /**< Do not inherit theme from base class. */ + LV_OBJ_CLASS_THEME_INHERITABLE_TRUE, +} lv_obj_class_theme_inheritable_t; +typedef void (*lv_obj_class_event_cb_t)(lv_obj_class_t * class_p, lv_event_t * e); /********************** * GLOBAL PROTOTYPES **********************/ @@ -72,21 +53,18 @@ typedef struct _lv_obj_class_t { * @param parent pointer to an object where the new object should be created * @return pointer to the created object */ -struct _lv_obj_t * lv_obj_class_create_obj(const struct _lv_obj_class_t * class_p, struct _lv_obj_t * parent); +lv_obj_t * lv_obj_class_create_obj(const lv_obj_class_t * class_p, lv_obj_t * parent); -void lv_obj_class_init_obj(struct _lv_obj_t * obj); +void lv_obj_class_init_obj(lv_obj_t * obj); -void _lv_obj_destruct(struct _lv_obj_t * obj); +bool lv_obj_is_editable(lv_obj_t * obj); -bool lv_obj_is_editable(struct _lv_obj_t * obj); - -bool lv_obj_is_group_def(struct _lv_obj_t * obj); +bool lv_obj_is_group_def(lv_obj_t * obj); /********************** * MACROS **********************/ - #ifdef __cplusplus } /*extern "C"*/ #endif diff --git a/include/liblvgl/core/lv_obj_class_private.h b/include/liblvgl/core/lv_obj_class_private.h new file mode 100644 index 00000000..de2b1738 --- /dev/null +++ b/include/liblvgl/core/lv_obj_class_private.h @@ -0,0 +1,78 @@ +/** + * @file lv_obj_class_private.h + * + */ + +#ifndef LV_OBJ_CLASS_PRIVATE_H +#define LV_OBJ_CLASS_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_obj_class.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** + * Describe the common methods of every object. + * Similar to a C++ class. + */ +struct _lv_obj_class_t { + const lv_obj_class_t * base_class; + /** class_p is the final class while obj->class_p is the class currently being [de]constructed. */ + void (*constructor_cb)(const lv_obj_class_t * class_p, lv_obj_t * obj); + void (*destructor_cb)(const lv_obj_class_t * class_p, lv_obj_t * obj); + + /** class_p is the class in which event is being processed. */ + void (*event_cb)(const lv_obj_class_t * class_p, lv_event_t * e); /**< Widget type specific event function*/ + +#if LV_USE_OBJ_PROPERTY + uint32_t prop_index_start; + uint32_t prop_index_end; + const lv_property_ops_t * properties; + uint32_t properties_count; + +#if LV_USE_OBJ_PROPERTY_NAME + /* An array of property ID and name */ + const lv_property_name_t * property_names; + uint32_t names_count; +#endif +#endif + + void * user_data; + const char * name; + int32_t width_def; + int32_t height_def; + uint32_t editable : 2; /**< Value from ::lv_obj_class_editable_t*/ + uint32_t group_def : 2; /**< Value from ::lv_obj_class_group_def_t*/ + uint32_t instance_size : 16; + uint32_t theme_inheritable : 1; /**< Value from ::lv_obj_class_theme_inheritable_t*/ +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void lv_obj_destruct(lv_obj_t * obj); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_OBJ_CLASS_PRIVATE_H*/ diff --git a/include/liblvgl/core/lv_obj_draw.h b/include/liblvgl/core/lv_obj_draw.h index a107e556..c0dbee43 100644 --- a/include/liblvgl/core/lv_obj_draw.h +++ b/include/liblvgl/core/lv_obj_draw.h @@ -13,7 +13,12 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/draw/lv_draw.h" +#include "../misc/lv_types.h" +#include "../draw/lv_draw_rect.h" +#include "../draw/lv_draw_label.h" +#include "../draw/lv_draw_image.h" +#include "../draw/lv_draw_line.h" +#include "../draw/lv_draw_arc.h" /********************* * DEFINES @@ -23,49 +28,12 @@ extern "C" { * TYPEDEFS **********************/ -struct _lv_obj_t; -struct _lv_obj_class_t; - -/** Cover check results.*/ -typedef enum { - LV_COVER_RES_COVER = 0, - LV_COVER_RES_NOT_COVER = 1, - LV_COVER_RES_MASKED = 2, -} lv_cover_res_t; - typedef enum { LV_LAYER_TYPE_NONE, LV_LAYER_TYPE_SIMPLE, LV_LAYER_TYPE_TRANSFORM, } lv_layer_type_t; -typedef struct { - lv_draw_ctx_t * draw_ctx; /**< Draw context*/ - const struct _lv_obj_class_t * class_p; /**< The class that sent the event */ - uint32_t type; /**< The type if part being draw. Element of `lv__draw_part_type_t` */ - lv_area_t * draw_area; /**< The area of the part being drawn*/ - lv_draw_rect_dsc_t * - rect_dsc; /**< A draw descriptor that can be modified to changed what LVGL will draw. Set only for rectangle-like parts*/ - lv_draw_label_dsc_t * - label_dsc; /**< A draw descriptor that can be modified to changed what LVGL will draw. Set only for text-like parts*/ - lv_draw_line_dsc_t * - line_dsc; /**< A draw descriptor that can be modified to changed what LVGL will draw. Set only for line-like parts*/ - lv_draw_img_dsc_t * - img_dsc; /**< A draw descriptor that can be modified to changed what LVGL will draw. Set only for image-like parts*/ - lv_draw_arc_dsc_t * - arc_dsc; /**< A draw descriptor that can be modified to changed what LVGL will draw. Set only for arc-like parts*/ - const lv_point_t * - p1; /**< A point calculated during drawing. E.g. a point of chart or the center of an arc.*/ - const lv_point_t * p2; /**< A point calculated during drawing. E.g. a point of chart.*/ - char * text; /**< A text calculated during drawing. Can be modified. E.g. tick labels on a chart axis.*/ - uint32_t text_length; /**< Size of the text buffer containing null-terminated text string calculated during drawing.*/ - uint32_t part; /**< The current part for which the event is sent*/ - uint32_t id; /**< The index of the part. E.g. a button's index on button matrix or table cell index.*/ - lv_coord_t radius; /**< E.g. the radius of an arc (not the corner radius).*/ - int32_t value; /**< A value calculated during drawing. E.g. Chart's tick line value.*/ - const void * sub_part_ptr; /**< A pointer the identifies something in the part. E.g. chart series. */ -} lv_obj_draw_part_dsc_t; - /********************** * GLOBAL PROTOTYPES **********************/ @@ -80,7 +48,7 @@ typedef struct { * @note Only the relevant fields will be set. * E.g. if `border width == 0` the other border properties won't be evaluated. */ -void lv_obj_init_draw_rect_dsc(struct _lv_obj_t * obj, uint32_t part, lv_draw_rect_dsc_t * draw_dsc); +void lv_obj_init_draw_rect_dsc(lv_obj_t * obj, lv_part_t part, lv_draw_rect_dsc_t * draw_dsc); /** * Initialize a label draw descriptor from an object's styles in its current state @@ -90,7 +58,7 @@ void lv_obj_init_draw_rect_dsc(struct _lv_obj_t * obj, uint32_t part, lv_draw_re * If the `opa` field is set to or the property is equal to `LV_OPA_TRANSP` the rest won't be initialized. * Should be initialized with `lv_draw_label_dsc_init(draw_dsc)`. */ -void lv_obj_init_draw_label_dsc(struct _lv_obj_t * obj, uint32_t part, lv_draw_label_dsc_t * draw_dsc); +void lv_obj_init_draw_label_dsc(lv_obj_t * obj, lv_part_t part, lv_draw_label_dsc_t * draw_dsc); /** * Initialize an image draw descriptor from an object's styles in its current state @@ -99,8 +67,7 @@ void lv_obj_init_draw_label_dsc(struct _lv_obj_t * obj, uint32_t part, lv_draw_l * @param draw_dsc the descriptor to initialize. * Should be initialized with `lv_draw_image_dsc_init(draw_dsc)`. */ -void lv_obj_init_draw_img_dsc(struct _lv_obj_t * obj, uint32_t part, lv_draw_img_dsc_t * draw_dsc); - +void lv_obj_init_draw_image_dsc(lv_obj_t * obj, lv_part_t part, lv_draw_image_dsc_t * draw_dsc); /** * Initialize a line draw descriptor from an object's styles in its current state @@ -109,7 +76,7 @@ void lv_obj_init_draw_img_dsc(struct _lv_obj_t * obj, uint32_t part, lv_draw_img * @param draw_dsc the descriptor to initialize. * Should be initialized with `lv_draw_line_dsc_init(draw_dsc)`. */ -void lv_obj_init_draw_line_dsc(struct _lv_obj_t * obj, uint32_t part, lv_draw_line_dsc_t * draw_dsc); +void lv_obj_init_draw_line_dsc(lv_obj_t * obj, lv_part_t part, lv_draw_line_dsc_t * draw_dsc); /** * Initialize an arc draw descriptor from an object's styles in its current state @@ -118,7 +85,7 @@ void lv_obj_init_draw_line_dsc(struct _lv_obj_t * obj, uint32_t part, lv_draw_li * @param draw_dsc the descriptor to initialize. * Should be initialized with `lv_draw_arc_dsc_init(draw_dsc)`. */ -void lv_obj_init_draw_arc_dsc(struct _lv_obj_t * obj, uint32_t part, lv_draw_arc_dsc_t * draw_dsc); +void lv_obj_init_draw_arc_dsc(lv_obj_t * obj, lv_part_t part, lv_draw_arc_dsc_t * draw_dsc); /** * Get the required extra size (around the object's part) to draw shadow, outline, value etc. @@ -126,40 +93,14 @@ void lv_obj_init_draw_arc_dsc(struct _lv_obj_t * obj, uint32_t part, lv_draw_arc * @param part part of the object * @return the extra size required around the object */ -lv_coord_t lv_obj_calculate_ext_draw_size(struct _lv_obj_t * obj, uint32_t part); - -/** - * Initialize a draw descriptor used in events. - * @param dsc pointer to a descriptor. Later it should be passed as parameter to an `LV_EVENT_DRAW_PART_BEGIN/END` event. - * @param draw the current draw context. (usually returned by `lv_event_get_draw_ctx(e)`) - */ -void lv_obj_draw_dsc_init(lv_obj_draw_part_dsc_t * dsc, lv_draw_ctx_t * draw_ctx); - -/** - * Check the type obj a part draw descriptor - * @param dsc the descriptor (normally the event parameter) - * @param class_p pointer to class to which `type` is related - * @param type element of `lv__draw_part_type_t` - * @return true if ::dsc is related to ::class_p and ::type - */ -bool lv_obj_draw_part_check_type(lv_obj_draw_part_dsc_t * dsc, const struct _lv_obj_class_t * class_p, uint32_t type); +int32_t lv_obj_calculate_ext_draw_size(lv_obj_t * obj, lv_part_t part); /** * Send a 'LV_EVENT_REFR_EXT_DRAW_SIZE' Call the ancestor's event handler to the object to refresh the value of the extended draw size. * The result will be saved in `obj`. * @param obj pointer to an object */ -void lv_obj_refresh_ext_draw_size(struct _lv_obj_t * obj); - -/** - * Get the extended draw area of an object. - * @param obj pointer to an object - * @return the size extended draw area around the real coordinates - */ -lv_coord_t _lv_obj_get_ext_draw_size(const struct _lv_obj_t * obj); - - -lv_layer_type_t _lv_obj_get_layer_type(const struct _lv_obj_t * obj); +void lv_obj_refresh_ext_draw_size(lv_obj_t * obj); /********************** * MACROS diff --git a/include/liblvgl/core/lv_obj_draw_private.h b/include/liblvgl/core/lv_obj_draw_private.h new file mode 100644 index 00000000..36c8bff9 --- /dev/null +++ b/include/liblvgl/core/lv_obj_draw_private.h @@ -0,0 +1,48 @@ +/** + * @file lv_obj_draw_private.h + * + */ + +#ifndef LV_OBJ_DRAW_PRIVATE_H +#define LV_OBJ_DRAW_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_obj_draw.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Get the extended draw area of an object. + * @param obj pointer to an object + * @return the size extended draw area around the real coordinates + */ +int32_t lv_obj_get_ext_draw_size(const lv_obj_t * obj); + +lv_layer_type_t lv_obj_get_layer_type(const lv_obj_t * obj); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_OBJ_DRAW_PRIVATE_H*/ diff --git a/include/liblvgl/core/lv_obj_event.h b/include/liblvgl/core/lv_obj_event.h new file mode 100644 index 00000000..2035f176 --- /dev/null +++ b/include/liblvgl/core/lv_obj_event.h @@ -0,0 +1,198 @@ +/** + * @file lv_obj_event.h + * + */ + +#ifndef LV_OBJ_EVENT_H +#define LV_OBJ_EVENT_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../misc/lv_types.h" +#include "../misc/lv_event.h" +#include "../indev/lv_indev.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** Cover check results.*/ +typedef enum { + LV_COVER_RES_COVER = 0, + LV_COVER_RES_NOT_COVER = 1, + LV_COVER_RES_MASKED = 2, +} lv_cover_res_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Send an event to the object + * @param obj pointer to an object + * @param event_code the type of the event from `lv_event_t` + * @param param arbitrary data depending on the widget type and the event. (Usually `NULL`) + * @return LV_RESULT_OK: `obj` was not deleted in the event; LV_RESULT_INVALID: `obj` was deleted in the event_code + */ +lv_result_t lv_obj_send_event(lv_obj_t * obj, lv_event_code_t event_code, void * param); + +/** + * Used by the widgets internally to call the ancestor widget types's event handler + * @param class_p pointer to the class of the widget (NOT the ancestor class) + * @param e pointer to the event descriptor + * @return LV_RESULT_OK: the target object was not deleted in the event; LV_RESULT_INVALID: it was deleted in the event_code + */ +lv_result_t lv_obj_event_base(const lv_obj_class_t * class_p, lv_event_t * e); + +/** + * Get the current target of the event. It's the object which event handler being called. + * If the event is not bubbled it's the same as "original" target. + * @param e pointer to the event descriptor + * @return the target of the event_code + */ +lv_obj_t * lv_event_get_current_target_obj(lv_event_t * e); + +/** + * Get the object originally targeted by the event. It's the same even if the event is bubbled. + * @param e pointer to the event descriptor + * @return pointer to the original target of the event_code + */ +lv_obj_t * lv_event_get_target_obj(lv_event_t * e); + +/** + * Add an event handler function for an object. + * Used by the user to react on event which happens with the object. + * An object can have multiple event handler. They will be called in the same order as they were added. + * @param obj pointer to an object + * @param filter an event code (e.g. `LV_EVENT_CLICKED`) on which the event should be called. `LV_EVENT_ALL` can be used to receive all the events. + * @param event_cb the new event function + * @param user_data custom data will be available in `event_cb` + * @return handler to the event. It can be used in `lv_obj_remove_event_dsc`. + */ +lv_event_dsc_t * lv_obj_add_event_cb(lv_obj_t * obj, lv_event_cb_t event_cb, lv_event_code_t filter, void * user_data); + +uint32_t lv_obj_get_event_count(lv_obj_t * obj); + +lv_event_dsc_t * lv_obj_get_event_dsc(lv_obj_t * obj, uint32_t index); + +bool lv_obj_remove_event(lv_obj_t * obj, uint32_t index); + +bool lv_obj_remove_event_cb(lv_obj_t * obj, lv_event_cb_t event_cb); + +bool lv_obj_remove_event_dsc(lv_obj_t * obj, lv_event_dsc_t * dsc); + +/** + * Remove an event_cb with user_data + * @param obj pointer to a obj + * @param event_cb the event_cb of the event to remove + * @param user_data user_data + * @return the count of the event removed + */ +uint32_t lv_obj_remove_event_cb_with_user_data(lv_obj_t * obj, lv_event_cb_t event_cb, void * user_data); + +/** + * Get the input device passed as parameter to indev related events. + * @param e pointer to an event + * @return the indev that triggered the event or NULL if called on a not indev related event + */ +lv_indev_t * lv_event_get_indev(lv_event_t * e); + +/** + * Get the draw context which should be the first parameter of the draw functions. + * Namely: `LV_EVENT_DRAW_MAIN/POST`, `LV_EVENT_DRAW_MAIN/POST_BEGIN`, `LV_EVENT_DRAW_MAIN/POST_END` + * @param e pointer to an event + * @return pointer to a draw context or NULL if called on an unrelated event + */ +lv_layer_t * lv_event_get_layer(lv_event_t * e); + +/** + * Get the old area of the object before its size was changed. Can be used in `LV_EVENT_SIZE_CHANGED` + * @param e pointer to an event + * @return the old absolute area of the object or NULL if called on an unrelated event + */ +const lv_area_t * lv_event_get_old_size(lv_event_t * e); + +/** + * Get the key passed as parameter to an event. Can be used in `LV_EVENT_KEY` + * @param e pointer to an event + * @return the triggering key or NULL if called on an unrelated event + */ +uint32_t lv_event_get_key(lv_event_t * e); + +/** + * Get the signed rotary encoder diff. passed as parameter to an event. Can be used in `LV_EVENT_ROTARY` + * @param e pointer to an event + * @return the triggering key or NULL if called on an unrelated event + */ +int32_t lv_event_get_rotary_diff(lv_event_t * e); + +/** + * Get the animation descriptor of a scrolling. Can be used in `LV_EVENT_SCROLL_BEGIN` + * @param e pointer to an event + * @return the animation that will scroll the object. (can be modified as required) + */ +lv_anim_t * lv_event_get_scroll_anim(lv_event_t * e); + +/** + * Set the new extra draw size. Can be used in `LV_EVENT_REFR_EXT_DRAW_SIZE` + * @param e pointer to an event + * @param size The new extra draw size + */ +void lv_event_set_ext_draw_size(lv_event_t * e, int32_t size); + +/** + * Get a pointer to an `lv_point_t` variable in which the self size should be saved (width in `point->x` and height `point->y`). + * Can be used in `LV_EVENT_GET_SELF_SIZE` + * @param e pointer to an event + * @return pointer to `lv_point_t` or NULL if called on an unrelated event + */ +lv_point_t * lv_event_get_self_size_info(lv_event_t * e); + +/** + * Get a pointer to an `lv_hit_test_info_t` variable in which the hit test result should be saved. Can be used in `LV_EVENT_HIT_TEST` + * @param e pointer to an event + * @return pointer to `lv_hit_test_info_t` or NULL if called on an unrelated event + */ +lv_hit_test_info_t * lv_event_get_hit_test_info(lv_event_t * e); + +/** + * Get a pointer to an area which should be examined whether the object fully covers it or not. + * Can be used in `LV_EVENT_HIT_TEST` + * @param e pointer to an event + * @return an area with absolute coordinates to check + */ +const lv_area_t * lv_event_get_cover_area(lv_event_t * e); + +/** + * Set the result of cover checking. Can be used in `LV_EVENT_COVER_CHECK` + * @param e pointer to an event + * @param res an element of ::lv_cover_check_info_t + */ +void lv_event_set_cover_res(lv_event_t * e, lv_cover_res_t res); + +/** + * Get the draw task which was just added. + * Can be used in `LV_EVENT_DRAW_TASK_ADDED event` + * @param e pointer to an event + * @return the added draw task + */ +lv_draw_task_t * lv_event_get_draw_task(lv_event_t * e); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_OBJ_EVENT_H*/ diff --git a/include/liblvgl/core/lv_obj_event_private.h b/include/liblvgl/core/lv_obj_event_private.h new file mode 100644 index 00000000..2199717c --- /dev/null +++ b/include/liblvgl/core/lv_obj_event_private.h @@ -0,0 +1,62 @@ +/** + * @file lv_obj_event_private.h + * + */ + +#ifndef LV_OBJ_EVENT_PRIVATE_H +#define LV_OBJ_EVENT_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_obj_event.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** + * Used as the event parameter of ::LV_EVENT_HIT_TEST to check if an `point` can click the object or not. + * `res` should be set like this: + * - If already set to `false` another event wants that point non clickable. If you want to respect it leave it as `false` or set `true` to overwrite it. + * - If already set `true` and `point` shouldn't be clickable set to `false` + * - If already set to `true` you agree that `point` can click the object leave it as `true` + */ +struct _lv_hit_test_info_t { + const lv_point_t * point; /**< A point relative to screen to check if it can click the object or not*/ + bool res; /**< true: `point` can click the object; false: it cannot*/ +}; + +/** + * Used as the event parameter of ::LV_EVENT_COVER_CHECK to check if an area is covered by the object or not. + * In the event use `const lv_area_t * area = lv_event_get_cover_area(e)` to get the area to check + * and `lv_event_set_cover_res(e, res)` to set the result. + */ +struct _lv_cover_check_info_t { + lv_cover_res_t res; + const lv_area_t * area; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_OBJ_EVENT_PRIVATE_H*/ diff --git a/include/liblvgl/core/lv_obj_pos.h b/include/liblvgl/core/lv_obj_pos.h index fdc64767..584f0b7e 100644 --- a/include/liblvgl/core/lv_obj_pos.h +++ b/include/liblvgl/core/lv_obj_pos.h @@ -13,7 +13,7 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/misc/lv_area.h" +#include "../misc/lv_area.h" /********************* * DEFINES @@ -22,13 +22,20 @@ extern "C" { /********************** * TYPEDEFS **********************/ -struct _lv_obj_t; -typedef void (*lv_layout_update_cb_t)(struct _lv_obj_t *, void * user_data); -typedef struct { - lv_layout_update_cb_t cb; - void * user_data; -} lv_layout_dsc_t; +typedef enum { + /** No flags */ + LV_OBJ_POINT_TRANSFORM_FLAG_NONE = 0x00, + + /** Consider the transformation properties of the parents too */ + LV_OBJ_POINT_TRANSFORM_FLAG_RECURSIVE = 0x01, + + /** Execute the inverse of the transformation (-angle and 1/zoom) */ + LV_OBJ_POINT_TRANSFORM_FLAG_INVERSE = 0x02, + + /** Both inverse and recursive*/ + LV_OBJ_POINT_TRANSFORM_FLAG_INVERSE_RECURSIVE = 0x03, +} lv_obj_point_transform_flag_t; /********************** * GLOBAL PROTOTYPES @@ -44,7 +51,7 @@ typedef struct { * @note The position is interpreted on the content area of the parent * @note The values can be set in pixel or in percentage of parent size with `lv_pct(v)` */ -void lv_obj_set_pos(struct _lv_obj_t * obj, lv_coord_t x, lv_coord_t y); +void lv_obj_set_pos(lv_obj_t * obj, int32_t x, int32_t y); /** * Set the x coordinate of an object @@ -55,7 +62,7 @@ void lv_obj_set_pos(struct _lv_obj_t * obj, lv_coord_t x, lv_coord_t y); * @note The position is interpreted on the content area of the parent * @note The values can be set in pixel or in percentage of parent size with `lv_pct(v)` */ -void lv_obj_set_x(struct _lv_obj_t * obj, lv_coord_t x); +void lv_obj_set_x(lv_obj_t * obj, int32_t x); /** * Set the y coordinate of an object @@ -66,7 +73,7 @@ void lv_obj_set_x(struct _lv_obj_t * obj, lv_coord_t x); * @note The position is interpreted on the content area of the parent * @note The values can be set in pixel or in percentage of parent size with `lv_pct(v)` */ -void lv_obj_set_y(struct _lv_obj_t * obj, lv_coord_t y); +void lv_obj_set_y(lv_obj_t * obj, int32_t y); /** * Set the size of an object. @@ -76,17 +83,17 @@ void lv_obj_set_y(struct _lv_obj_t * obj, lv_coord_t y); * @note possible values are: * pixel simple set the size accordingly * LV_SIZE_CONTENT set the size to involve all children in the given direction - * LV_SIZE_PCT(x) to set size in percentage of the parent's content area size (the size without paddings). + * lv_pct(x) to set size in percentage of the parent's content area size (the size without paddings). * x should be in [0..1000]% range */ -void lv_obj_set_size(struct _lv_obj_t * obj, lv_coord_t w, lv_coord_t h); +void lv_obj_set_size(lv_obj_t * obj, int32_t w, int32_t h); /** * Recalculate the size of the object * @param obj pointer to an object * @return true: the size has been changed */ -bool lv_obj_refr_size(struct _lv_obj_t * obj); +bool lv_obj_refr_size(lv_obj_t * obj); /** * Set the width of an object @@ -98,7 +105,7 @@ bool lv_obj_refr_size(struct _lv_obj_t * obj); * lv_pct(x) to set size in percentage of the parent's content area size (the size without paddings). * x should be in [0..1000]% range */ -void lv_obj_set_width(struct _lv_obj_t * obj, lv_coord_t w); +void lv_obj_set_width(lv_obj_t * obj, int32_t w); /** * Set the height of an object @@ -110,62 +117,54 @@ void lv_obj_set_width(struct _lv_obj_t * obj, lv_coord_t w); * lv_pct(x) to set size in percentage of the parent's content area size (the size without paddings). * x should be in [0..1000]% range */ -void lv_obj_set_height(struct _lv_obj_t * obj, lv_coord_t h); +void lv_obj_set_height(lv_obj_t * obj, int32_t h); /** * Set the width reduced by the left and right padding and the border width. * @param obj pointer to an object * @param w the width without paddings in pixels */ -void lv_obj_set_content_width(struct _lv_obj_t * obj, lv_coord_t w); +void lv_obj_set_content_width(lv_obj_t * obj, int32_t w); /** * Set the height reduced by the top and bottom padding and the border width. * @param obj pointer to an object * @param h the height without paddings in pixels */ -void lv_obj_set_content_height(struct _lv_obj_t * obj, lv_coord_t h); +void lv_obj_set_content_height(lv_obj_t * obj, int32_t h); /** * Set a layout for an object * @param obj pointer to an object * @param layout pointer to a layout descriptor to set */ -void lv_obj_set_layout(struct _lv_obj_t * obj, uint32_t layout); +void lv_obj_set_layout(lv_obj_t * obj, uint32_t layout); /** * Test whether the and object is positioned by a layout or not * @param obj pointer to an object to test * @return true: positioned by a layout; false: not positioned by a layout */ -bool lv_obj_is_layout_positioned(const struct _lv_obj_t * obj); +bool lv_obj_is_layout_positioned(const lv_obj_t * obj); /** * Mark the object for layout update. * @param obj pointer to an object whose children needs to be updated */ -void lv_obj_mark_layout_as_dirty(struct _lv_obj_t * obj); +void lv_obj_mark_layout_as_dirty(lv_obj_t * obj); /** * Update the layout of an object. * @param obj pointer to an object whose children needs to be updated */ -void lv_obj_update_layout(const struct _lv_obj_t * obj); - -/** - * Register a new layout - * @param cb the layout update callback - * @param user_data custom data that will be passed to `cb` - * @return the ID of the new layout - */ -uint32_t lv_layout_register(lv_layout_update_cb_t cb, void * user_data); +void lv_obj_update_layout(const lv_obj_t * obj); /** * Change the alignment of an object. * @param obj pointer to an object to align * @param align type of alignment (see 'lv_align_t' enum) `LV_ALIGN_OUT_...` can't be used. */ -void lv_obj_set_align(struct _lv_obj_t * obj, lv_align_t align); +void lv_obj_set_align(lv_obj_t * obj, lv_align_t align); /** * Change the alignment of an object and set new coordinates. @@ -177,37 +176,33 @@ void lv_obj_set_align(struct _lv_obj_t * obj, lv_align_t align); * @param x_ofs x coordinate offset after alignment * @param y_ofs y coordinate offset after alignment */ -void lv_obj_align(struct _lv_obj_t * obj, lv_align_t align, lv_coord_t x_ofs, lv_coord_t y_ofs); +void lv_obj_align(lv_obj_t * obj, lv_align_t align, int32_t x_ofs, int32_t y_ofs); /** - * Align an object to an other object. + * Align an object to another object. * @param obj pointer to an object to align - * @param base pointer to an other object (if NULL `obj`s parent is used). 'obj' will be aligned to it. + * @param base pointer to another object (if NULL `obj`s parent is used). 'obj' will be aligned to it. * @param align type of alignment (see 'lv_align_t' enum) * @param x_ofs x coordinate offset after alignment * @param y_ofs y coordinate offset after alignment * @note if the position or size of `base` changes `obj` needs to be aligned manually again */ -void lv_obj_align_to(struct _lv_obj_t * obj, const struct _lv_obj_t * base, lv_align_t align, lv_coord_t x_ofs, - lv_coord_t y_ofs); +void lv_obj_align_to(lv_obj_t * obj, const lv_obj_t * base, lv_align_t align, int32_t x_ofs, + int32_t y_ofs); /** * Align an object to the center on its parent. * @param obj pointer to an object to align * @note if the parent size changes `obj` needs to be aligned manually again */ -static inline void lv_obj_center(struct _lv_obj_t * obj) -{ - lv_obj_align(obj, LV_ALIGN_CENTER, 0, 0); -} - +void lv_obj_center(lv_obj_t * obj); /** * Copy the coordinates of an object to an area * @param obj pointer to an object * @param coords pointer to an area to store the coordinates */ -void lv_obj_get_coords(const struct _lv_obj_t * obj, lv_area_t * coords); +void lv_obj_get_coords(const lv_obj_t * obj, lv_area_t * coords); /** * Get the x coordinate of object. @@ -219,7 +214,7 @@ void lv_obj_get_coords(const struct _lv_obj_t * obj, lv_area_t * coords); * @note Scrolling of the parent doesn't change the returned value. * @note The returned value is always the distance from the parent even if `obj` is positioned by a layout. */ -lv_coord_t lv_obj_get_x(const struct _lv_obj_t * obj); +int32_t lv_obj_get_x(const lv_obj_t * obj); /** * Get the x2 coordinate of object. @@ -231,7 +226,7 @@ lv_coord_t lv_obj_get_x(const struct _lv_obj_t * obj); * @note Scrolling of the parent doesn't change the returned value. * @note The returned value is always the distance from the parent even if `obj` is positioned by a layout. */ -lv_coord_t lv_obj_get_x2(const struct _lv_obj_t * obj); +int32_t lv_obj_get_x2(const lv_obj_t * obj); /** * Get the y coordinate of object. @@ -243,7 +238,7 @@ lv_coord_t lv_obj_get_x2(const struct _lv_obj_t * obj); * @note Scrolling of the parent doesn't change the returned value. * @note The returned value is always the distance from the parent even if `obj` is positioned by a layout. */ -lv_coord_t lv_obj_get_y(const struct _lv_obj_t * obj); +int32_t lv_obj_get_y(const lv_obj_t * obj); /** * Get the y2 coordinate of object. @@ -255,21 +250,21 @@ lv_coord_t lv_obj_get_y(const struct _lv_obj_t * obj); * @note Scrolling of the parent doesn't change the returned value. * @note The returned value is always the distance from the parent even if `obj` is positioned by a layout. */ -lv_coord_t lv_obj_get_y2(const struct _lv_obj_t * obj); +int32_t lv_obj_get_y2(const lv_obj_t * obj); /** - * Get the actually set x coordinate of object, i.e. the offset form the set alignment + * Get the actually set x coordinate of object, i.e. the offset from the set alignment * @param obj pointer to an object * @return the set x coordinate */ -lv_coord_t lv_obj_get_x_aligned(const struct _lv_obj_t * obj); +int32_t lv_obj_get_x_aligned(const lv_obj_t * obj); /** - * Get the actually set y coordinate of object, i.e. the offset form the set alignment + * Get the actually set y coordinate of object, i.e. the offset from the set alignment * @param obj pointer to an object * @return the set y coordinate */ -lv_coord_t lv_obj_get_y_aligned(const struct _lv_obj_t * obj); +int32_t lv_obj_get_y_aligned(const lv_obj_t * obj); /** * Get the width of an object @@ -278,7 +273,7 @@ lv_coord_t lv_obj_get_y_aligned(const struct _lv_obj_t * obj); * call `lv_obj_update_layout(obj)`. * @return the width in pixels */ -lv_coord_t lv_obj_get_width(const struct _lv_obj_t * obj); +int32_t lv_obj_get_width(const lv_obj_t * obj); /** * Get the height of an object @@ -287,7 +282,7 @@ lv_coord_t lv_obj_get_width(const struct _lv_obj_t * obj); * call `lv_obj_update_layout(obj)`. * @return the height in pixels */ -lv_coord_t lv_obj_get_height(const struct _lv_obj_t * obj); +int32_t lv_obj_get_height(const lv_obj_t * obj); /** * Get the width reduced by the left and right padding and the border width. @@ -296,7 +291,7 @@ lv_coord_t lv_obj_get_height(const struct _lv_obj_t * obj); * call `lv_obj_update_layout(obj)`. * @return the width which still fits into its parent without causing overflow (making the parent scrollable) */ -lv_coord_t lv_obj_get_content_width(const struct _lv_obj_t * obj); +int32_t lv_obj_get_content_width(const lv_obj_t * obj); /** * Get the height reduced by the top and bottom padding and the border width. @@ -305,7 +300,7 @@ lv_coord_t lv_obj_get_content_width(const struct _lv_obj_t * obj); * call `lv_obj_update_layout(obj)`. * @return the height which still fits into the parent without causing overflow (making the parent scrollable) */ -lv_coord_t lv_obj_get_content_height(const struct _lv_obj_t * obj); +int32_t lv_obj_get_content_height(const lv_obj_t * obj); /** * Get the area reduced by the paddings and the border width. @@ -314,7 +309,7 @@ lv_coord_t lv_obj_get_content_height(const struct _lv_obj_t * obj); * call `lv_obj_update_layout(obj)`. * @param area the area which still fits into the parent without causing overflow (making the parent scrollable) */ -void lv_obj_get_content_coords(const struct _lv_obj_t * obj, lv_area_t * area); +void lv_obj_get_content_coords(const lv_obj_t * obj, lv_area_t * area); /** * Get the width occupied by the "parts" of the widget. E.g. the width of all columns of a table. @@ -323,7 +318,7 @@ void lv_obj_get_content_coords(const struct _lv_obj_t * obj, lv_area_t * area); * @note This size independent from the real size of the widget. * It just tells how large the internal ("virtual") content is. */ -lv_coord_t lv_obj_get_self_width(const struct _lv_obj_t * obj); +int32_t lv_obj_get_self_width(const lv_obj_t * obj); /** * Get the height occupied by the "parts" of the widget. E.g. the height of all rows of a table. @@ -332,39 +327,46 @@ lv_coord_t lv_obj_get_self_width(const struct _lv_obj_t * obj); * @note This size independent from the real size of the widget. * It just tells how large the internal ("virtual") content is. */ -lv_coord_t lv_obj_get_self_height(const struct _lv_obj_t * obj); +int32_t lv_obj_get_self_height(const lv_obj_t * obj); /** * Handle if the size of the internal ("virtual") content of an object has changed. * @param obj pointer to an object * @return false: nothing happened; true: refresh happened */ -bool lv_obj_refresh_self_size(struct _lv_obj_t * obj); - -void lv_obj_refr_pos(struct _lv_obj_t * obj); +bool lv_obj_refresh_self_size(lv_obj_t * obj); -void lv_obj_move_to(struct _lv_obj_t * obj, lv_coord_t x, lv_coord_t y); +void lv_obj_refr_pos(lv_obj_t * obj); +void lv_obj_move_to(lv_obj_t * obj, int32_t x, int32_t y); -void lv_obj_move_children_by(struct _lv_obj_t * obj, lv_coord_t x_diff, lv_coord_t y_diff, bool ignore_floating); +void lv_obj_move_children_by(lv_obj_t * obj, int32_t x_diff, int32_t y_diff, bool ignore_floating); /** * Transform a point using the angle and zoom style properties of an object * @param obj pointer to an object whose style properties should be used * @param p a point to transform, the result will be written back here too - * @param recursive consider the transformation properties of the parents too - * @param inv do the inverse of the transformation (-angle and 1/zoom) + * @param flags OR-ed valued of :cpp:enum:`lv_obj_point_transform_flag_t` + */ +void lv_obj_transform_point(const lv_obj_t * obj, lv_point_t * p, lv_obj_point_transform_flag_t flags); + +/** + * Transform an array of points using the angle and zoom style properties of an object + * @param obj pointer to an object whose style properties should be used + * @param points the array of points to transform, the result will be written back here too + * @param count number of points in the array + * @param flags OR-ed valued of :cpp:enum:`lv_obj_point_transform_flag_t` */ -void lv_obj_transform_point(const struct _lv_obj_t * obj, lv_point_t * p, bool recursive, bool inv); +void lv_obj_transform_point_array(const lv_obj_t * obj, lv_point_t points[], size_t count, + lv_obj_point_transform_flag_t flags); /** * Transform an area using the angle and zoom style properties of an object * @param obj pointer to an object whose style properties should be used * @param area an area to transform, the result will be written back here too - * @param recursive consider the transformation properties of the parents too - * @param inv do the inverse of the transformation (-angle and 1/zoom) + * @param flags OR-ed valued of :cpp:enum:`lv_obj_point_transform_flag_t` */ -void lv_obj_get_transformed_area(const struct _lv_obj_t * obj, lv_area_t * area, bool recursive, bool inv); +void lv_obj_get_transformed_area(const lv_obj_t * obj, lv_area_t * area, lv_obj_point_transform_flag_t flags); /** * Mark an area of an object as invalid. @@ -372,13 +374,13 @@ void lv_obj_get_transformed_area(const struct _lv_obj_t * obj, lv_area_t * area, * @param obj pointer to an object * @param area the area to redraw */ -void lv_obj_invalidate_area(const struct _lv_obj_t * obj, const lv_area_t * area); +void lv_obj_invalidate_area(const lv_obj_t * obj, const lv_area_t * area); /** * Mark the object as invalid to redrawn its area * @param obj pointer to an object */ -void lv_obj_invalidate(const struct _lv_obj_t * obj); +void lv_obj_invalidate(const lv_obj_t * obj); /** * Tell whether an area of an object is visible (even partially) now or not @@ -386,21 +388,21 @@ void lv_obj_invalidate(const struct _lv_obj_t * obj); * @param area the are to check. The visible part of the area will be written back here. * @return true visible; false not visible (hidden, out of parent, on other screen, etc) */ -bool lv_obj_area_is_visible(const struct _lv_obj_t * obj, lv_area_t * area); +bool lv_obj_area_is_visible(const lv_obj_t * obj, lv_area_t * area); /** * Tell whether an object is visible (even partially) now or not * @param obj pointer to an object * @return true: visible; false not visible (hidden, out of parent, on other screen, etc) */ -bool lv_obj_is_visible(const struct _lv_obj_t * obj); +bool lv_obj_is_visible(const lv_obj_t * obj); /** * Set the size of an extended clickable area * @param obj pointer to an object * @param size extended clickable area in all 4 directions [px] */ -void lv_obj_set_ext_click_area(struct _lv_obj_t * obj, lv_coord_t size); +void lv_obj_set_ext_click_area(lv_obj_t * obj, int32_t size); /** * Get the an area where to object can be clicked. @@ -408,7 +410,7 @@ void lv_obj_set_ext_click_area(struct _lv_obj_t * obj, lv_coord_t size); * @param obj pointer to an object * @param area store the result area here */ -void lv_obj_get_click_area(const struct _lv_obj_t * obj, lv_area_t * area); +void lv_obj_get_click_area(const lv_obj_t * obj, lv_area_t * area); /** * Hit-test an object given a particular point in screen space. @@ -416,7 +418,7 @@ void lv_obj_get_click_area(const struct _lv_obj_t * obj, lv_area_t * area); * @param point screen-space point (absolute coordinate) * @return true: if the object is considered under the point */ -bool lv_obj_hit_test(struct _lv_obj_t * obj, const lv_point_t * point); +bool lv_obj_hit_test(lv_obj_t * obj, const lv_point_t * point); /** * Clamp a width between min and max width. If the min/max width is in percentage value use the ref_width @@ -426,7 +428,7 @@ bool lv_obj_hit_test(struct _lv_obj_t * obj, const lv_point_t * point); * @param ref_width the reference width used when min/max width is in percentage * @return the clamped width */ -lv_coord_t lv_clamp_width(lv_coord_t width, lv_coord_t min_width, lv_coord_t max_width, lv_coord_t ref_width); +int32_t lv_clamp_width(int32_t width, int32_t min_width, int32_t max_width, int32_t ref_width); /** * Clamp a height between min and max height. If the min/max height is in percentage value use the ref_height @@ -436,7 +438,7 @@ lv_coord_t lv_clamp_width(lv_coord_t width, lv_coord_t min_width, lv_coord_t max * @param ref_height the reference height used when min/max height is in percentage * @return the clamped height */ -lv_coord_t lv_clamp_height(lv_coord_t height, lv_coord_t min_height, lv_coord_t max_height, lv_coord_t ref_height); +int32_t lv_clamp_height(int32_t height, int32_t min_height, int32_t max_height, int32_t ref_height); /********************** * MACROS diff --git a/include/liblvgl/core/lv_obj_private.h b/include/liblvgl/core/lv_obj_private.h new file mode 100644 index 00000000..865b9771 --- /dev/null +++ b/include/liblvgl/core/lv_obj_private.h @@ -0,0 +1,88 @@ +/** + * @file lv_obj_private.h + * + */ + +#ifndef LV_OBJ_PRIVATE_H +#define LV_OBJ_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_obj.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** + * Special, rarely used attributes. + * They are allocated automatically if any elements is set. + */ +struct _lv_obj_spec_attr_t { + lv_obj_t ** children; /**< Store the pointer of the children in an array.*/ + lv_group_t * group_p; + lv_event_list_t event_list; + + lv_point_t scroll; /**< The current X/Y scroll offset*/ + + int32_t ext_click_pad; /**< Extra click padding in all direction*/ + int32_t ext_draw_size; /**< EXTend the size in every direction for drawing.*/ + + uint16_t child_cnt; /**< Number of children*/ + uint16_t scrollbar_mode : 2; /**< How to display scrollbars, see `lv_scrollbar_mode_t`*/ + uint16_t scroll_snap_x : 2; /**< Where to align the snappable children horizontally, see `lv_scroll_snap_t`*/ + uint16_t scroll_snap_y : 2; /**< Where to align the snappable children vertically*/ + uint16_t scroll_dir : 4; /**< The allowed scroll direction(s), see `lv_dir_t`*/ + uint16_t layer_type : 2; /**< Cache the layer type here. Element of lv_intermediate_layer_type_t */ +}; + +struct _lv_obj_t { + const lv_obj_class_t * class_p; + lv_obj_t * parent; + lv_obj_spec_attr_t * spec_attr; + lv_obj_style_t * styles; +#if LV_OBJ_STYLE_CACHE + uint32_t style_main_prop_is_set; + uint32_t style_other_prop_is_set; +#endif + void * user_data; +#if LV_USE_OBJ_ID + void * id; +#endif + lv_area_t coords; + lv_obj_flag_t flags; + lv_state_t state; + uint16_t layout_inv : 1; + uint16_t readjust_scroll_after_layout : 1; + uint16_t scr_layout_inv : 1; + uint16_t skip_trans : 1; + uint16_t style_cnt : 6; + uint16_t h_layout : 1; + uint16_t w_layout : 1; + uint16_t is_deleting : 1; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_OBJ_PRIVATE_H*/ diff --git a/include/liblvgl/core/lv_obj_property.h b/include/liblvgl/core/lv_obj_property.h new file mode 100644 index 00000000..2053a5c2 --- /dev/null +++ b/include/liblvgl/core/lv_obj_property.h @@ -0,0 +1,209 @@ +/** + * @file lv_obj_property.h + * + */ + +#ifndef LV_OBJ_PROPERTY_H +#define LV_OBJ_PROPERTY_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../misc/lv_types.h" +#include "../misc/lv_style.h" + +#if LV_USE_OBJ_PROPERTY + +/********************* + * DEFINES + *********************/ + +/*All possible property value types*/ +#define LV_PROPERTY_TYPE_INVALID 0 /*Use default 0 as invalid to detect program outliers*/ +#define LV_PROPERTY_TYPE_INT 1 /*int32_t type*/ +#define LV_PROPERTY_TYPE_PRECISE 2 /*lv_value_precise_t, int32_t or float depending on LV_USE_FLOAT*/ +#define LV_PROPERTY_TYPE_COLOR 3 /*ARGB8888 type*/ +#define LV_PROPERTY_TYPE_POINT 4 /*lv_point_t */ +#define LV_PROPERTY_TYPE_POINTER 5 /*void * pointer*/ +#define LV_PROPERTY_TYPE_IMGSRC 6 /*Special pointer for image*/ +#define LV_PROPERTY_TYPE_TEXT 7 /*Special pointer of char* */ +#define LV_PROPERTY_TYPE_OBJ 8 /*Special pointer of lv_obj_t* */ +#define LV_PROPERTY_TYPE_DISPLAY 9 /*Special pointer of lv_display_t* */ +#define LV_PROPERTY_TYPE_FONT 10 /*Special pointer of lv_font_t* */ +#define LV_PROPERTY_TYPE_BOOL 11 /*int32_t type*/ + +#define LV_PROPERTY_TYPE_SHIFT 28 +#define LV_PROPERTY_ID(clz, name, type, index) LV_PROPERTY_## clz ##_##name = (LV_PROPERTY_## clz ##_START + (index)) | ((type) << LV_PROPERTY_TYPE_SHIFT) + +#define LV_PROPERTY_ID_TYPE(id) ((id) >> LV_PROPERTY_TYPE_SHIFT) +#define LV_PROPERTY_ID_INDEX(id) ((id) & 0xfffffff) + +/*Set properties from an array of lv_property_t*/ +#define LV_OBJ_SET_PROPERTY_ARRAY(obj, array) lv_obj_set_properties(obj, array, sizeof(array)/sizeof(array[0])) + + +/********************** + * TYPEDEFS + **********************/ + +/** + * Group of predefined widget ID start value. + */ +enum { + LV_PROPERTY_ID_INVALID = 0, + + /*ID 0x01 to 0xff are style ID, check lv_style_prop_t*/ + LV_PROPERTY_STYLE_START = 0x00, + + LV_PROPERTY_ID_START = 0x0100, /*ID smaller than 0xff is style ID*/ + /*Define the property ID for every widget here. */ + LV_PROPERTY_OBJ_START = 0x0100, /* lv_obj.c */ + LV_PROPERTY_IMAGE_START = 0x0200, /* lv_image.c */ + LV_PROPERTY_LABEL_START = 0x0300, /* lv_label.c */ + LV_PROPERTY_KEYBOARD_START = 0x0400, /* lv_keyboard.c */ + LV_PROPERTY_TEXTAREA_START = 0x0500, /* lv_textarea.c */ + LV_PROPERTY_ROLLER_START = 0x0600, /* lv_roller.c */ + LV_PROPERTY_DROPDOWN_START = 0x0700, /* lv_dropdown.c */ + + /*Special ID, use it to extend ID and make sure it's unique and compile time determinant*/ + LV_PROPERTY_ID_BUILTIN_LAST = 0xffff, /*ID of 0x10000 ~ 0xfffffff is reserved for user*/ + + LV_PROPERTY_ID_ANY = 0x7ffffffe, /*Special ID used by lvgl to intercept all setter/getter call.*/ +}; + +struct _lv_property_name_t { + const char * name; + lv_prop_id_t id; +}; + +typedef struct { + lv_prop_id_t id; + union { + int32_t num; /**< Number integer number (opacity, enums, booleans or "normal" numbers)*/ + bool enable; /**< booleans*/ + const void * ptr; /**< Constant pointers (font, cone text, etc)*/ + lv_color_t color; /**< Colors*/ + lv_value_precise_t precise; /**< float or int for precise value*/ + lv_point_t point; /**< Point*/ + struct { + /** + * Note that place struct member `style` at first place is intended. + * `style` shares same memory with `num`, `ptr`, `color`. + * So we set the style value directly without using `prop.style.num`. + * + * E.g. + * + * static const lv_property_t obj_pos_x = { + * .id = LV_PROPERTY_STYLE_X, + * .num = 123, + * .selector = LV_STATE_PRESSED, + * } + * + * instead of: + * static const lv_property_t obj_pos_x = { + * .id = LV_PROPERTY_STYLE_X, + * .style.num = 123, // note this line. + * .selector = LV_STATE_PRESSED, + * } + */ + lv_style_value_t style; /**< Make sure it's the first element in struct. */ + uint32_t selector; /**< Style selector, lv_part_t | lv_state_t */ + }; + }; +} lv_property_t; + +typedef struct { + lv_prop_id_t id; + + void * setter; /**< Callback used to set property. */ + void * getter; /**< Callback used to get property. */ +} lv_property_ops_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/*===================== + * Setter functions + *====================*/ + +/** + * Set widget property. + * @param obj pointer to an object + * @param value The property value to set + * @return return LV_RESULT_OK if success + */ +lv_result_t lv_obj_set_property(lv_obj_t * obj, const lv_property_t * value); + +/** + * Set multiple widget properties. Helper `LV_OBJ_SET_PROPERTY_ARRAY` can be used for constant property array. + * @param obj pointer to an object + * @param value The property value array to set + * @param count The count of the property value array + * @return return LV_RESULT_OK if success + */ +lv_result_t lv_obj_set_properties(lv_obj_t * obj, const lv_property_t * value, uint32_t count); + +/*===================== + * Getter functions + *====================*/ + +/** + * Read property value from object. + * If id is a style property, the style selector is default to 0. + * @param obj pointer to an object + * @param id ID of which property to read + * @return return the property value read. The returned property ID is set to `LV_PROPERTY_ID_INVALID` if failed. + */ +lv_property_t lv_obj_get_property(lv_obj_t * obj, lv_prop_id_t id); + +/** + * Read a style property value from object + * @param obj pointer to an object + * @param id ID of style property + * @param selector selector for the style property. + * @return return the property value read. The returned property ID is set to `LV_PROPERTY_ID_INVALID` if failed. + */ +lv_property_t lv_obj_get_style_property(lv_obj_t * obj, lv_prop_id_t id, uint32_t selector); + +/** + * Get the property ID by name recursively to base classes. Requires to enable `LV_USE_OBJ_PROPERTY_NAME`. + * @param obj pointer to an object that has specified property or base class has. + * @param name property name + * @return property ID found or `LV_PROPERTY_ID_INVALID` if not found. + */ +lv_prop_id_t lv_obj_property_get_id(const lv_obj_t * obj, const char * name); + +/** + * Get the property ID by name without check base class recursively. Requires to enable `LV_USE_OBJ_PROPERTY_NAME`. + * @param clz pointer to an object class that has specified property or base class has. + * @param name property name + * @return property ID found or `LV_PROPERTY_ID_INVALID` if not found. + */ +lv_prop_id_t lv_obj_class_property_get_id(const lv_obj_class_t * clz, const char * name); + +/** + * Get the style property ID by name. Requires to enable `LV_USE_OBJ_PROPERTY_NAME`. + * @param name property name + * @return property ID found or `LV_PROPERTY_ID_INVALID` if not found. + */ +lv_prop_id_t lv_style_property_get_id(const char * name); + +/********************** + * MACROS + **********************/ + +#include "../widgets/property/lv_obj_property_names.h" +#include "../widgets/property/lv_style_properties.h" + +#endif /*LV_USE_OBJ_PROPERTY*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_OBJ_PROPERTY_H*/ diff --git a/include/liblvgl/core/lv_obj_scroll.h b/include/liblvgl/core/lv_obj_scroll.h index 3717dec4..f2cb2f10 100644 --- a/include/liblvgl/core/lv_obj_scroll.h +++ b/include/liblvgl/core/lv_obj_scroll.h @@ -13,9 +13,9 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/misc/lv_area.h" -#include "liblvgl/misc/lv_anim.h" -#include "liblvgl/misc/lv_types.h" +#include "../misc/lv_area.h" +#include "../misc/lv_anim.h" +#include "../misc/lv_types.h" /********************* * DEFINES @@ -26,26 +26,22 @@ extern "C" { **********************/ /*Can't include lv_obj.h because it includes this header file*/ -struct _lv_obj_t; /** Scrollbar modes: shows when should the scrollbars be visible*/ -enum { +typedef enum { LV_SCROLLBAR_MODE_OFF, /**< Never show scrollbars*/ LV_SCROLLBAR_MODE_ON, /**< Always show scrollbars*/ LV_SCROLLBAR_MODE_ACTIVE, /**< Show scroll bars when object is being scrolled*/ LV_SCROLLBAR_MODE_AUTO, /**< Show scroll bars when the content is large enough to be scrolled*/ -}; -typedef uint8_t lv_scrollbar_mode_t; - +} lv_scrollbar_mode_t; /** Scroll span align options. Tells where to align the snappable children when scroll stops.*/ -enum { +typedef enum { LV_SCROLL_SNAP_NONE, /**< Do not align, leave where it is*/ LV_SCROLL_SNAP_START, /**< Align to the left/top*/ LV_SCROLL_SNAP_END, /**< Align to the right/bottom*/ LV_SCROLL_SNAP_CENTER /**< Align to the center*/ -}; -typedef uint8_t lv_scroll_snap_t; +} lv_scroll_snap_t; /********************** * GLOBAL PROTOTYPES @@ -60,28 +56,28 @@ typedef uint8_t lv_scroll_snap_t; * @param obj pointer to an object * @param mode LV_SCROLL_MODE_ON/OFF/AUTO/ACTIVE */ -void lv_obj_set_scrollbar_mode(struct _lv_obj_t * obj, lv_scrollbar_mode_t mode); +void lv_obj_set_scrollbar_mode(lv_obj_t * obj, lv_scrollbar_mode_t mode); /** * Set the object in which directions can be scrolled * @param obj pointer to an object * @param dir the allow scroll directions. An element or OR-ed values of `lv_dir_t` */ -void lv_obj_set_scroll_dir(struct _lv_obj_t * obj, lv_dir_t dir); +void lv_obj_set_scroll_dir(lv_obj_t * obj, lv_dir_t dir); /** * Set where to snap the children when scrolling ends horizontally * @param obj pointer to an object * @param align the snap align to set from `lv_scroll_snap_t` */ -void lv_obj_set_scroll_snap_x(struct _lv_obj_t * obj, lv_scroll_snap_t align); +void lv_obj_set_scroll_snap_x(lv_obj_t * obj, lv_scroll_snap_t align); /** * Set where to snap the children when scrolling ends vertically * @param obj pointer to an object * @param align the snap align to set from `lv_scroll_snap_t` */ -void lv_obj_set_scroll_snap_y(struct _lv_obj_t * obj, lv_scroll_snap_t align); +void lv_obj_set_scroll_snap_y(lv_obj_t * obj, lv_scroll_snap_t align); /*===================== * Getter functions @@ -92,28 +88,27 @@ void lv_obj_set_scroll_snap_y(struct _lv_obj_t * obj, lv_scroll_snap_t align); * @param obj pointer to an object * @return the current scroll mode from `lv_scrollbar_mode_t` */ -lv_scrollbar_mode_t lv_obj_get_scrollbar_mode(const struct _lv_obj_t * obj); +lv_scrollbar_mode_t lv_obj_get_scrollbar_mode(const lv_obj_t * obj); /** * Get the object in which directions can be scrolled * @param obj pointer to an object - * @param dir the allow scroll directions. An element or OR-ed values of `lv_dir_t` */ -lv_dir_t lv_obj_get_scroll_dir(const struct _lv_obj_t * obj); +lv_dir_t lv_obj_get_scroll_dir(const lv_obj_t * obj); /** * Get where to snap the children when scrolling ends horizontally * @param obj pointer to an object * @return the current snap align from `lv_scroll_snap_t` */ -lv_scroll_snap_t lv_obj_get_scroll_snap_x(const struct _lv_obj_t * obj); +lv_scroll_snap_t lv_obj_get_scroll_snap_x(const lv_obj_t * obj); /** * Get where to snap the children when scrolling ends vertically * @param obj pointer to an object * @return the current snap align from `lv_scroll_snap_t` */ -lv_scroll_snap_t lv_obj_get_scroll_snap_y(const struct _lv_obj_t * obj); +lv_scroll_snap_t lv_obj_get_scroll_snap_y(const lv_obj_t * obj); /** * Get current X scroll position. @@ -123,7 +118,7 @@ lv_scroll_snap_t lv_obj_get_scroll_snap_y(const struct _lv_obj_t * obj); * If scrolled return > 0 * If scrolled in (elastic scroll) return < 0 */ -lv_coord_t lv_obj_get_scroll_x(const struct _lv_obj_t * obj); +int32_t lv_obj_get_scroll_x(const lv_obj_t * obj); /** * Get current Y scroll position. @@ -133,7 +128,7 @@ lv_coord_t lv_obj_get_scroll_x(const struct _lv_obj_t * obj); * If scrolled return > 0 * If scrolled inside return < 0 */ -lv_coord_t lv_obj_get_scroll_y(const struct _lv_obj_t * obj); +int32_t lv_obj_get_scroll_y(const lv_obj_t * obj); /** * Return the height of the area above the object. @@ -142,7 +137,7 @@ lv_coord_t lv_obj_get_scroll_y(const struct _lv_obj_t * obj); * @param obj pointer to an object * @return the scrollable area above the object in pixels */ -lv_coord_t lv_obj_get_scroll_top(struct _lv_obj_t * obj); +int32_t lv_obj_get_scroll_top(lv_obj_t * obj); /** * Return the height of the area below the object. @@ -151,7 +146,7 @@ lv_coord_t lv_obj_get_scroll_top(struct _lv_obj_t * obj); * @param obj pointer to an object * @return the scrollable area below the object in pixels */ -lv_coord_t lv_obj_get_scroll_bottom(struct _lv_obj_t * obj); +int32_t lv_obj_get_scroll_bottom(lv_obj_t * obj); /** * Return the width of the area on the left the object. @@ -160,7 +155,7 @@ lv_coord_t lv_obj_get_scroll_bottom(struct _lv_obj_t * obj); * @param obj pointer to an object * @return the scrollable area on the left the object in pixels */ -lv_coord_t lv_obj_get_scroll_left(struct _lv_obj_t * obj); +int32_t lv_obj_get_scroll_left(lv_obj_t * obj); /** * Return the width of the area on the right the object. @@ -169,7 +164,7 @@ lv_coord_t lv_obj_get_scroll_left(struct _lv_obj_t * obj); * @param obj pointer to an object * @return the scrollable area on the right the object in pixels */ -lv_coord_t lv_obj_get_scroll_right(struct _lv_obj_t * obj); +int32_t lv_obj_get_scroll_right(lv_obj_t * obj); /** * Get the X and Y coordinates where the scrolling will end for this object if a scrolling animation is in progress. @@ -177,7 +172,7 @@ lv_coord_t lv_obj_get_scroll_right(struct _lv_obj_t * obj); * @param obj pointer to an object * @param end pointer to store the result */ -void lv_obj_get_scroll_end(struct _lv_obj_t * obj, lv_point_t * end); +void lv_obj_get_scroll_end(lv_obj_t * obj, lv_point_t * end); /*===================== * Other functions @@ -186,13 +181,13 @@ void lv_obj_get_scroll_end(struct _lv_obj_t * obj, lv_point_t * end); /** * Scroll by a given amount of pixels * @param obj pointer to an object to scroll - * @param dx pixels to scroll horizontally - * @param dy pixels to scroll vertically + * @param x pixels to scroll horizontally + * @param y pixels to scroll vertically * @param anim_en LV_ANIM_ON: scroll with animation; LV_ANIM_OFF: scroll immediately * @note > 0 value means scroll right/bottom (show the more content on the right/bottom) * @note e.g. dy = -20 means scroll down 20 px */ -void lv_obj_scroll_by(struct _lv_obj_t * obj, lv_coord_t x, lv_coord_t y, lv_anim_enable_t anim_en); +void lv_obj_scroll_by(lv_obj_t * obj, int32_t x, int32_t y, lv_anim_enable_t anim_en); /** * Scroll by a given amount of pixels. @@ -203,7 +198,7 @@ void lv_obj_scroll_by(struct _lv_obj_t * obj, lv_coord_t x, lv_coord_t y, lv_ani * @param anim_en LV_ANIM_ON: scroll with animation; LV_ANIM_OFF: scroll immediately * @note e.g. dy = -20 means scroll down 20 px */ -void lv_obj_scroll_by_bounded(struct _lv_obj_t * obj, lv_coord_t dx, lv_coord_t dy, lv_anim_enable_t anim_en); +void lv_obj_scroll_by_bounded(lv_obj_t * obj, int32_t dx, int32_t dy, lv_anim_enable_t anim_en); /** * Scroll to a given coordinate on an object. @@ -213,7 +208,7 @@ void lv_obj_scroll_by_bounded(struct _lv_obj_t * obj, lv_coord_t dx, lv_coord_t * @param y pixels to scroll vertically * @param anim_en LV_ANIM_ON: scroll with animation; LV_ANIM_OFF: scroll immediately */ -void lv_obj_scroll_to(struct _lv_obj_t * obj, lv_coord_t x, lv_coord_t y, lv_anim_enable_t anim_en); +void lv_obj_scroll_to(lv_obj_t * obj, int32_t x, int32_t y, lv_anim_enable_t anim_en); /** * Scroll to a given X coordinate on an object. @@ -222,7 +217,7 @@ void lv_obj_scroll_to(struct _lv_obj_t * obj, lv_coord_t x, lv_coord_t y, lv_ani * @param x pixels to scroll horizontally * @param anim_en LV_ANIM_ON: scroll with animation; LV_ANIM_OFF: scroll immediately */ -void lv_obj_scroll_to_x(struct _lv_obj_t * obj, lv_coord_t x, lv_anim_enable_t anim_en); +void lv_obj_scroll_to_x(lv_obj_t * obj, int32_t x, lv_anim_enable_t anim_en); /** * Scroll to a given Y coordinate on an object @@ -231,14 +226,14 @@ void lv_obj_scroll_to_x(struct _lv_obj_t * obj, lv_coord_t x, lv_anim_enable_t a * @param y pixels to scroll vertically * @param anim_en LV_ANIM_ON: scroll with animation; LV_ANIM_OFF: scroll immediately */ -void lv_obj_scroll_to_y(struct _lv_obj_t * obj, lv_coord_t y, lv_anim_enable_t anim_en); +void lv_obj_scroll_to_y(lv_obj_t * obj, int32_t y, lv_anim_enable_t anim_en); /** * Scroll to an object until it becomes visible on its parent * @param obj pointer to an object to scroll into view * @param anim_en LV_ANIM_ON: scroll with animation; LV_ANIM_OFF: scroll immediately */ -void lv_obj_scroll_to_view(struct _lv_obj_t * obj, lv_anim_enable_t anim_en); +void lv_obj_scroll_to_view(lv_obj_t * obj, lv_anim_enable_t anim_en); /** * Scroll to an object until it becomes visible on its parent. @@ -247,33 +242,21 @@ void lv_obj_scroll_to_view(struct _lv_obj_t * obj, lv_anim_enable_t anim_en); * @param obj pointer to an object to scroll into view * @param anim_en LV_ANIM_ON: scroll with animation; LV_ANIM_OFF: scroll immediately */ -void lv_obj_scroll_to_view_recursive(struct _lv_obj_t * obj, lv_anim_enable_t anim_en); - - -/** - * Low level function to scroll by given x and y coordinates. - * `LV_EVENT_SCROLL` is sent. - * @param obj pointer to an object to scroll - * @param x pixels to scroll horizontally - * @param y pixels to scroll vertically - * @return `LV_RES_INV`: to object was deleted in `LV_EVENT_SCROLL`; - * `LV_RES_OK`: if the object is still valid - */ -lv_res_t _lv_obj_scroll_by_raw(struct _lv_obj_t * obj, lv_coord_t x, lv_coord_t y); +void lv_obj_scroll_to_view_recursive(lv_obj_t * obj, lv_anim_enable_t anim_en); /** * Tell whether an object is being scrolled or not at this moment * @param obj pointer to an object * @return true: `obj` is being scrolled */ -bool lv_obj_is_scrolling(const struct _lv_obj_t * obj); +bool lv_obj_is_scrolling(const lv_obj_t * obj); /** * Check the children of `obj` and scroll `obj` to fulfill the scroll_snap settings * @param obj an object whose children needs to checked and snapped * @param anim_en LV_ANIM_ON/OFF */ -void lv_obj_update_snap(struct _lv_obj_t * obj, lv_anim_enable_t anim_en); +void lv_obj_update_snap(lv_obj_t * obj, lv_anim_enable_t anim_en); /** * Get the area of the scrollbars @@ -281,20 +264,20 @@ void lv_obj_update_snap(struct _lv_obj_t * obj, lv_anim_enable_t anim_en); * @param hor pointer to store the area of the horizontal scrollbar * @param ver pointer to store the area of the vertical scrollbar */ -void lv_obj_get_scrollbar_area(struct _lv_obj_t * obj, lv_area_t * hor, lv_area_t * ver); +void lv_obj_get_scrollbar_area(lv_obj_t * obj, lv_area_t * hor, lv_area_t * ver); /** * Invalidate the area of the scrollbars * @param obj pointer to an object */ -void lv_obj_scrollbar_invalidate(struct _lv_obj_t * obj); +void lv_obj_scrollbar_invalidate(lv_obj_t * obj); /** - * Checked if the content is scrolled "in" and adjusts it to a normal position. + * Checks if the content is scrolled "in" and adjusts it to a normal position. * @param obj pointer to an object * @param anim_en LV_ANIM_ON/OFF */ -void lv_obj_readjust_scroll(struct _lv_obj_t * obj, lv_anim_enable_t anim_en); +void lv_obj_readjust_scroll(lv_obj_t * obj, lv_anim_enable_t anim_en); /********************** * MACROS diff --git a/include/liblvgl/core/lv_obj_scroll_private.h b/include/liblvgl/core/lv_obj_scroll_private.h new file mode 100644 index 00000000..9ae6332d --- /dev/null +++ b/include/liblvgl/core/lv_obj_scroll_private.h @@ -0,0 +1,50 @@ +/** + * @file lv_obj_scroll_private.h + * + */ + +#ifndef LV_OBJ_SCROLL_PRIVATE_H +#define LV_OBJ_SCROLL_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_obj_scroll.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Low level function to scroll by given x and y coordinates. + * `LV_EVENT_SCROLL` is sent. + * @param obj pointer to an object to scroll + * @param x pixels to scroll horizontally + * @param y pixels to scroll vertically + * @return `LV_RESULT_INVALID`: to object was deleted in `LV_EVENT_SCROLL`; + * `LV_RESULT_OK`: if the object is still valid + */ +lv_result_t lv_obj_scroll_by_raw(lv_obj_t * obj, int32_t x, int32_t y); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_OBJ_SCROLL_PRIVATE_H*/ diff --git a/include/liblvgl/core/lv_obj_style.h b/include/liblvgl/core/lv_obj_style.h index 18e530a6..ff4e365d 100644 --- a/include/liblvgl/core/lv_obj_style.h +++ b/include/liblvgl/core/lv_obj_style.h @@ -13,9 +13,9 @@ extern "C" { /********************* * INCLUDES *********************/ -#include -#include -#include "liblvgl/misc/lv_bidi.h" +#include "../misc/lv_bidi.h" +#include "../misc/lv_style.h" +#include "../misc/lv_types.h" /********************* * DEFINES @@ -24,75 +24,74 @@ extern "C" { /********************** * TYPEDEFS **********************/ -/*Can't include lv_obj.h because it includes this header file*/ -struct _lv_obj_t; typedef enum { - _LV_STYLE_STATE_CMP_SAME, /*The style properties in the 2 states are identical*/ - _LV_STYLE_STATE_CMP_DIFF_REDRAW, /*The differences can be shown with a simple redraw*/ - _LV_STYLE_STATE_CMP_DIFF_DRAW_PAD, /*The differences can be shown with a simple redraw*/ - _LV_STYLE_STATE_CMP_DIFF_LAYOUT, /*The differences can be shown with a simple redraw*/ -} _lv_style_state_cmp_t; + LV_STYLE_STATE_CMP_SAME, /**< The style properties in the 2 states are identical */ + LV_STYLE_STATE_CMP_DIFF_REDRAW, /**< The differences can be shown with a simple redraw */ + LV_STYLE_STATE_CMP_DIFF_DRAW_PAD, /**< The differences can be shown with a simple redraw */ + LV_STYLE_STATE_CMP_DIFF_LAYOUT, /**< The differences can be shown with a simple redraw */ +} lv_style_state_cmp_t; typedef uint32_t lv_style_selector_t; -typedef struct { - lv_style_t * style; - uint32_t selector : 24; - uint32_t is_local : 1; - uint32_t is_trans : 1; -} _lv_obj_style_t; - -typedef struct { - uint16_t time; - uint16_t delay; - lv_style_selector_t selector; - lv_style_prop_t prop; - lv_anim_path_cb_t path_cb; -#if LV_USE_USER_DATA - void * user_data; -#endif -} _lv_obj_style_transition_dsc_t; - /********************** * GLOBAL PROTOTYPES **********************/ -/** - * Initialize the object related style manager module. - * Called by LVGL in `lv_init()` - */ -void _lv_obj_style_init(void); - /** * Add a style to an object. * @param obj pointer to an object * @param style pointer to a style to add * @param selector OR-ed value of parts and state to which the style should be added - * @example lv_obj_add_style(btn, &style_btn, 0); //Default button style - * @example lv_obj_add_style(btn, &btn_red, LV_STATE_PRESSED); //Overwrite only some colors to red when pressed + * + * Examples: + * @code + * lv_obj_add_style(btn, &style_btn, 0); //Default button style + * + * lv_obj_add_style(btn, &btn_red, LV_STATE_PRESSED); //Overwrite only some colors to red when pressed + * @endcode */ -void lv_obj_add_style(struct _lv_obj_t * obj, lv_style_t * style, lv_style_selector_t selector); +void lv_obj_add_style(lv_obj_t * obj, const lv_style_t * style, lv_style_selector_t selector); /** - * Add a style to an object. + * Replaces a style of an object, preserving the order of the style stack (local styles and transitions are ignored). + * @param obj pointer to an object + * @param old_style pointer to a style to replace. + * @param new_style pointer to a style to replace the old style with. + * @param selector OR-ed values of states and a part to replace only styles with matching selectors. LV_STATE_ANY and LV_PART_ANY can be used + * + * Examples: + * @code + * lv_obj_replace_style(obj, &yellow_style, &blue_style, LV_PART_ANY | LV_STATE_ANY); //Replace a specific style + * + * lv_obj_replace_style(obj, &yellow_style, &blue_style, LV_PART_MAIN | LV_STATE_PRESSED); //Replace a specific style assigned to the main part when it is pressed + * @endcode + */ +bool lv_obj_replace_style(lv_obj_t * obj, const lv_style_t * old_style, const lv_style_t * new_style, + lv_style_selector_t selector); + +/** + * Remove a style from an object. * @param obj pointer to an object * @param style pointer to a style to remove. Can be NULL to check only the selector * @param selector OR-ed values of states and a part to remove only styles with matching selectors. LV_STATE_ANY and LV_PART_ANY can be used - * @example lv_obj_remove_style(obj, &style, LV_PART_ANY | LV_STATE_ANY); //Remove a specific style - * @example lv_obj_remove_style(obj, NULL, LV_PART_MAIN | LV_STATE_ANY); //Remove all styles from the main part - * @example lv_obj_remove_style(obj, NULL, LV_PART_ANY | LV_STATE_ANY); //Remove all styles + * + * Examples: + * @code + * lv_obj_remove_style(obj, &style, LV_PART_ANY | LV_STATE_ANY); //Remove a specific style + * + * lv_obj_remove_style(obj, NULL, LV_PART_MAIN | LV_STATE_ANY); //Remove all styles from the main part + * + * lv_obj_remove_style(obj, NULL, LV_PART_ANY | LV_STATE_ANY); //Remove all styles + * @endcode */ -void lv_obj_remove_style(struct _lv_obj_t * obj, lv_style_t * style, lv_style_selector_t selector); +void lv_obj_remove_style(lv_obj_t * obj, const lv_style_t * style, lv_style_selector_t selector); /** * Remove all styles from an object * @param obj pointer to an object */ -static inline void lv_obj_remove_style_all(struct _lv_obj_t * obj) -{ - lv_obj_remove_style(obj, NULL, LV_PART_ANY | LV_STATE_ANY); -} +void lv_obj_remove_style_all(lv_obj_t * obj); /** * Notify all object if a style is modified @@ -109,7 +108,7 @@ void lv_obj_report_style_change(lv_style_t * style); * It is used to optimize what needs to be refreshed. * `LV_STYLE_PROP_INV` to perform only a style cache update */ -void lv_obj_refresh_style(struct _lv_obj_t * obj, lv_part_t part, lv_style_prop_t prop); +void lv_obj_refresh_style(lv_obj_t * obj, lv_part_t part, lv_style_prop_t prop); /** * Enable or disable automatic style refreshing when a new style is added/removed to/from an object @@ -128,7 +127,16 @@ void lv_obj_enable_style_refresh(bool en); * @return the value of the property. * Should be read from the correct field of the `lv_style_value_t` according to the type of the property. */ -lv_style_value_t lv_obj_get_style_prop(const struct _lv_obj_t * obj, lv_part_t part, lv_style_prop_t prop); +lv_style_value_t lv_obj_get_style_prop(const lv_obj_t * obj, lv_part_t part, lv_style_prop_t prop); + +/** + * Check if an object has a specified style property for a given style selector. + * @param obj pointer to an object + * @param selector the style selector to be checked, defining the scope of the style to be examined. + * @param prop the property to be checked. + * @return true if the object has the specified selector and property, false otherwise. + */ +bool lv_obj_has_style_prop(const lv_obj_t * obj, lv_style_selector_t selector, lv_style_prop_t prop); /** * Set local style property on an object's part and state. @@ -137,13 +145,10 @@ lv_style_value_t lv_obj_get_style_prop(const struct _lv_obj_t * obj, lv_part_t p * @param value value of the property. The correct element should be set according to the type of the property * @param selector OR-ed value of parts and state for which the style should be set */ -void lv_obj_set_local_style_prop(struct _lv_obj_t * obj, lv_style_prop_t prop, lv_style_value_t value, +void lv_obj_set_local_style_prop(lv_obj_t * obj, lv_style_prop_t prop, lv_style_value_t value, lv_style_selector_t selector); -void lv_obj_set_local_style_prop_meta(struct _lv_obj_t * obj, lv_style_prop_t prop, uint16_t meta, - lv_style_selector_t selector); - -lv_style_res_t lv_obj_get_local_style_prop(struct _lv_obj_t * obj, lv_style_prop_t prop, lv_style_value_t * value, +lv_style_res_t lv_obj_get_local_style_prop(lv_obj_t * obj, lv_style_prop_t prop, lv_style_value_t * value, lv_style_selector_t selector); /** @@ -153,32 +158,12 @@ lv_style_res_t lv_obj_get_local_style_prop(struct _lv_obj_t * obj, lv_style_prop * @param selector OR-ed value of parts and state for which the style should be removed * @return true the property was found and removed; false: the property was not found */ -bool lv_obj_remove_local_style_prop(struct _lv_obj_t * obj, lv_style_prop_t prop, lv_style_selector_t selector); +bool lv_obj_remove_local_style_prop(lv_obj_t * obj, lv_style_prop_t prop, lv_style_selector_t selector); /** * Used internally for color filtering */ -lv_style_value_t _lv_obj_style_apply_color_filter(const struct _lv_obj_t * obj, uint32_t part, lv_style_value_t v); - -/** - * Used internally to create a style transition - * @param obj - * @param part - * @param prev_state - * @param new_state - * @param tr - */ -void _lv_obj_style_create_transition(struct _lv_obj_t * obj, lv_part_t part, lv_state_t prev_state, - lv_state_t new_state, const _lv_obj_style_transition_dsc_t * tr); - -/** - * Used internally to compare the appearance of an object in 2 states - * @param obj - * @param state1 - * @param state2 - * @return - */ -_lv_style_state_cmp_t _lv_obj_style_state_compare(struct _lv_obj_t * obj, lv_state_t state1, lv_state_t state2); +lv_style_value_t lv_obj_style_apply_color_filter(const lv_obj_t * obj, lv_part_t part, lv_style_value_t v); /** * Fade in an an object and all its children. @@ -186,7 +171,7 @@ _lv_style_state_cmp_t _lv_obj_style_state_compare(struct _lv_obj_t * obj, lv_sta * @param time time of fade * @param delay delay to start the animation */ -void lv_obj_fade_in(struct _lv_obj_t * obj, uint32_t time, uint32_t delay); +void lv_obj_fade_in(lv_obj_t * obj, uint32_t time, uint32_t delay); /** * Fade out an an object and all its children. @@ -194,15 +179,21 @@ void lv_obj_fade_in(struct _lv_obj_t * obj, uint32_t time, uint32_t delay); * @param time time of fade * @param delay delay to start the animation */ -void lv_obj_fade_out(struct _lv_obj_t * obj, uint32_t time, uint32_t delay); +void lv_obj_fade_out(lv_obj_t * obj, uint32_t time, uint32_t delay); -lv_state_t lv_obj_style_get_selector_state(lv_style_selector_t selector); +static inline lv_state_t lv_obj_style_get_selector_state(lv_style_selector_t selector) +{ + return selector & 0xFFFF; +} -lv_part_t lv_obj_style_get_selector_part(lv_style_selector_t selector); +static inline lv_part_t lv_obj_style_get_selector_part(lv_style_selector_t selector) +{ + return selector & 0xFF0000; +} #include "lv_obj_style_gen.h" -static inline void lv_obj_set_style_pad_all(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector) +static inline void lv_obj_set_style_pad_all(lv_obj_t * obj, int32_t value, lv_style_selector_t selector) { lv_obj_set_style_pad_left(obj, value, selector); lv_obj_set_style_pad_right(obj, value, selector); @@ -210,32 +201,111 @@ static inline void lv_obj_set_style_pad_all(struct _lv_obj_t * obj, lv_coord_t v lv_obj_set_style_pad_bottom(obj, value, selector); } -static inline void lv_obj_set_style_pad_hor(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector) +static inline void lv_obj_set_style_pad_hor(lv_obj_t * obj, int32_t value, lv_style_selector_t selector) { lv_obj_set_style_pad_left(obj, value, selector); lv_obj_set_style_pad_right(obj, value, selector); } -static inline void lv_obj_set_style_pad_ver(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector) +static inline void lv_obj_set_style_pad_ver(lv_obj_t * obj, int32_t value, lv_style_selector_t selector) { lv_obj_set_style_pad_top(obj, value, selector); lv_obj_set_style_pad_bottom(obj, value, selector); } -static inline void lv_obj_set_style_pad_gap(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector) +static inline void lv_obj_set_style_margin_all(lv_obj_t * obj, int32_t value, lv_style_selector_t selector) +{ + lv_obj_set_style_margin_left(obj, value, selector); + lv_obj_set_style_margin_right(obj, value, selector); + lv_obj_set_style_margin_top(obj, value, selector); + lv_obj_set_style_margin_bottom(obj, value, selector); +} + +static inline void lv_obj_set_style_margin_hor(lv_obj_t * obj, int32_t value, lv_style_selector_t selector) +{ + lv_obj_set_style_margin_left(obj, value, selector); + lv_obj_set_style_margin_right(obj, value, selector); +} + +static inline void lv_obj_set_style_margin_ver(lv_obj_t * obj, int32_t value, lv_style_selector_t selector) +{ + lv_obj_set_style_margin_top(obj, value, selector); + lv_obj_set_style_margin_bottom(obj, value, selector); +} + +static inline void lv_obj_set_style_pad_gap(lv_obj_t * obj, int32_t value, lv_style_selector_t selector) { lv_obj_set_style_pad_row(obj, value, selector); lv_obj_set_style_pad_column(obj, value, selector); } -static inline void lv_obj_set_style_size(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector) +static inline void lv_obj_set_style_size(lv_obj_t * obj, int32_t width, int32_t height, + lv_style_selector_t selector) +{ + lv_obj_set_style_width(obj, width, selector); + lv_obj_set_style_height(obj, height, selector); +} + +static inline void lv_obj_set_style_transform_scale(lv_obj_t * obj, int32_t value, + lv_style_selector_t selector) +{ + lv_obj_set_style_transform_scale_x(obj, value, selector); + lv_obj_set_style_transform_scale_y(obj, value, selector); +} + +static inline int32_t lv_obj_get_style_space_left(const lv_obj_t * obj, lv_part_t part) +{ + int32_t padding = lv_obj_get_style_pad_left(obj, part); + int32_t border_width = lv_obj_get_style_border_width(obj, part); + lv_border_side_t border_side = lv_obj_get_style_border_side(obj, part); + return (border_side & LV_BORDER_SIDE_LEFT) ? padding + border_width : padding; +} + +static inline int32_t lv_obj_get_style_space_right(const lv_obj_t * obj, lv_part_t part) +{ + int32_t padding = lv_obj_get_style_pad_right(obj, part); + int32_t border_width = lv_obj_get_style_border_width(obj, part); + lv_border_side_t border_side = lv_obj_get_style_border_side(obj, part); + return (border_side & LV_BORDER_SIDE_RIGHT) ? padding + border_width : padding; +} + +static inline int32_t lv_obj_get_style_space_top(const lv_obj_t * obj, lv_part_t part) +{ + int32_t padding = lv_obj_get_style_pad_top(obj, part); + int32_t border_width = lv_obj_get_style_border_width(obj, part); + lv_border_side_t border_side = lv_obj_get_style_border_side(obj, part); + return (border_side & LV_BORDER_SIDE_TOP) ? padding + border_width : padding; +} + +static inline int32_t lv_obj_get_style_space_bottom(const lv_obj_t * obj, lv_part_t part) { - lv_obj_set_style_width(obj, value, selector); - lv_obj_set_style_height(obj, value, selector); + int32_t padding = lv_obj_get_style_pad_bottom(obj, part); + int32_t border_width = lv_obj_get_style_border_width(obj, part); + lv_border_side_t border_side = lv_obj_get_style_border_side(obj, part); + return (border_side & LV_BORDER_SIDE_BOTTOM) ? padding + border_width : padding; } -lv_text_align_t lv_obj_calculate_style_text_align(const struct _lv_obj_t * obj, lv_part_t part, const char * txt); +lv_text_align_t lv_obj_calculate_style_text_align(const lv_obj_t * obj, lv_part_t part, const char * txt); +static inline int32_t lv_obj_get_style_transform_scale_x_safe(const lv_obj_t * obj, lv_part_t part) +{ + int32_t scale = lv_obj_get_style_transform_scale_x(obj, part); + return scale > 0 ? scale : 1; +} + +static inline int32_t lv_obj_get_style_transform_scale_y_safe(const lv_obj_t * obj, lv_part_t part) +{ + int32_t scale = lv_obj_get_style_transform_scale_y(obj, part); + return scale > 0 ? scale : 1; +} + +/** + * Get the `opa` style property from all parents and multiply and `>> 8` them. + * @param obj the object whose opacity should be get + * @param part the part whose opacity should be get. Non-MAIN parts will consider the `opa` of the MAIN part too + * @return the final opacity considering the parents' opacity too + */ +lv_opa_t lv_obj_get_style_opa_recursive(const lv_obj_t * obj, lv_part_t part); /********************** * MACROS diff --git a/include/liblvgl/core/lv_obj_style_gen.h b/include/liblvgl/core/lv_obj_style_gen.h index 576927d9..77cf5378 100644 --- a/include/liblvgl/core/lv_obj_style_gen.h +++ b/include/liblvgl/core/lv_obj_style_gen.h @@ -1,648 +1,875 @@ -static inline lv_coord_t lv_obj_get_style_width(const struct _lv_obj_t * obj, uint32_t part) + +/* + ********************************************************************** + * DO NOT EDIT + * This file is automatically generated by "style_api_gen.py" + ********************************************************************** + */ + + +#ifndef LV_OBJ_STYLE_GEN_H +#define LV_OBJ_STYLE_GEN_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "../misc/lv_area.h" +#include "../misc/lv_style.h" +#include "../core/lv_obj_style.h" +#include "../misc/lv_types.h" + +static inline int32_t lv_obj_get_style_width(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_WIDTH); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_min_width(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_min_width(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_MIN_WIDTH); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_max_width(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_max_width(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_MAX_WIDTH); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_height(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_height(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_HEIGHT); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_min_height(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_min_height(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_MIN_HEIGHT); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_max_height(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_max_height(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_MAX_HEIGHT); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_x(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_length(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_LENGTH); + return (int32_t)v.num; +} + +static inline int32_t lv_obj_get_style_x(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_X); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_y(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_y(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_Y); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_align_t lv_obj_get_style_align(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_align_t lv_obj_get_style_align(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_ALIGN); return (lv_align_t)v.num; } -static inline lv_coord_t lv_obj_get_style_transform_width(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_transform_width(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_TRANSFORM_WIDTH); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_transform_height(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_transform_height(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_TRANSFORM_HEIGHT); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_translate_x(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_translate_x(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_TRANSLATE_X); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_translate_y(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_translate_y(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_TRANSLATE_Y); - return (lv_coord_t)v.num; + return (int32_t)v.num; +} + +static inline int32_t lv_obj_get_style_transform_scale_x(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_TRANSFORM_SCALE_X); + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_transform_zoom(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_transform_scale_y(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_TRANSFORM_ZOOM); - return (lv_coord_t)v.num; + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_TRANSFORM_SCALE_Y); + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_transform_angle(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_transform_rotation(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_TRANSFORM_ANGLE); - return (lv_coord_t)v.num; + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_TRANSFORM_ROTATION); + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_transform_pivot_x(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_transform_pivot_x(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_TRANSFORM_PIVOT_X); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_transform_pivot_y(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_transform_pivot_y(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_TRANSFORM_PIVOT_Y); - return (lv_coord_t)v.num; + return (int32_t)v.num; +} + +static inline int32_t lv_obj_get_style_transform_skew_x(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_TRANSFORM_SKEW_X); + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_pad_top(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_transform_skew_y(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_TRANSFORM_SKEW_Y); + return (int32_t)v.num; +} + +static inline int32_t lv_obj_get_style_pad_top(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_PAD_TOP); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_pad_bottom(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_pad_bottom(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_PAD_BOTTOM); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_pad_left(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_pad_left(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_PAD_LEFT); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_pad_right(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_pad_right(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_PAD_RIGHT); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_pad_row(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_pad_row(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_PAD_ROW); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_pad_column(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_pad_column(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_PAD_COLUMN); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_color_t lv_obj_get_style_bg_color(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_margin_top(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_MARGIN_TOP); + return (int32_t)v.num; +} + +static inline int32_t lv_obj_get_style_margin_bottom(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_MARGIN_BOTTOM); + return (int32_t)v.num; +} + +static inline int32_t lv_obj_get_style_margin_left(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_MARGIN_LEFT); + return (int32_t)v.num; +} + +static inline int32_t lv_obj_get_style_margin_right(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_MARGIN_RIGHT); + return (int32_t)v.num; +} + +static inline lv_color_t lv_obj_get_style_bg_color(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BG_COLOR); return v.color; } -static inline lv_color_t lv_obj_get_style_bg_color_filtered(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_color_t lv_obj_get_style_bg_color_filtered(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = _lv_obj_style_apply_color_filter(obj, part, lv_obj_get_style_prop(obj, part, LV_STYLE_BG_COLOR)); + lv_style_value_t v = lv_obj_style_apply_color_filter(obj, part, lv_obj_get_style_prop(obj, part, LV_STYLE_BG_COLOR)); return v.color; } -static inline lv_opa_t lv_obj_get_style_bg_opa(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_opa_t lv_obj_get_style_bg_opa(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BG_OPA); return (lv_opa_t)v.num; } -static inline lv_color_t lv_obj_get_style_bg_grad_color(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_color_t lv_obj_get_style_bg_grad_color(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BG_GRAD_COLOR); return v.color; } -static inline lv_color_t lv_obj_get_style_bg_grad_color_filtered(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_color_t lv_obj_get_style_bg_grad_color_filtered(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = _lv_obj_style_apply_color_filter(obj, part, lv_obj_get_style_prop(obj, part, LV_STYLE_BG_GRAD_COLOR)); + lv_style_value_t v = lv_obj_style_apply_color_filter(obj, part, lv_obj_get_style_prop(obj, part, + LV_STYLE_BG_GRAD_COLOR)); return v.color; } -static inline lv_grad_dir_t lv_obj_get_style_bg_grad_dir(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_grad_dir_t lv_obj_get_style_bg_grad_dir(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BG_GRAD_DIR); return (lv_grad_dir_t)v.num; } -static inline lv_coord_t lv_obj_get_style_bg_main_stop(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_bg_main_stop(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BG_MAIN_STOP); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_bg_grad_stop(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_bg_grad_stop(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BG_GRAD_STOP); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline const lv_grad_dsc_t * lv_obj_get_style_bg_grad(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_opa_t lv_obj_get_style_bg_main_opa(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BG_GRAD); - return (const lv_grad_dsc_t *)v.ptr; + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BG_MAIN_OPA); + return (lv_opa_t)v.num; } -static inline lv_dither_mode_t lv_obj_get_style_bg_dither_mode(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_opa_t lv_obj_get_style_bg_grad_opa(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BG_DITHER_MODE); - return (lv_dither_mode_t)v.num; + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BG_GRAD_OPA); + return (lv_opa_t)v.num; +} + +static inline const lv_grad_dsc_t * lv_obj_get_style_bg_grad(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BG_GRAD); + return (const lv_grad_dsc_t *)v.ptr; } -static inline const void * lv_obj_get_style_bg_img_src(const struct _lv_obj_t * obj, uint32_t part) +static inline const void * lv_obj_get_style_bg_image_src(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BG_IMG_SRC); + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BG_IMAGE_SRC); return (const void *)v.ptr; } -static inline lv_opa_t lv_obj_get_style_bg_img_opa(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_opa_t lv_obj_get_style_bg_image_opa(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BG_IMG_OPA); + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BG_IMAGE_OPA); return (lv_opa_t)v.num; } -static inline lv_color_t lv_obj_get_style_bg_img_recolor(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_color_t lv_obj_get_style_bg_image_recolor(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BG_IMG_RECOLOR); + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BG_IMAGE_RECOLOR); return v.color; } -static inline lv_color_t lv_obj_get_style_bg_img_recolor_filtered(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_color_t lv_obj_get_style_bg_image_recolor_filtered(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = _lv_obj_style_apply_color_filter(obj, part, lv_obj_get_style_prop(obj, part, LV_STYLE_BG_IMG_RECOLOR)); + lv_style_value_t v = lv_obj_style_apply_color_filter(obj, part, lv_obj_get_style_prop(obj, part, + LV_STYLE_BG_IMAGE_RECOLOR)); return v.color; } -static inline lv_opa_t lv_obj_get_style_bg_img_recolor_opa(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_opa_t lv_obj_get_style_bg_image_recolor_opa(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BG_IMG_RECOLOR_OPA); + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BG_IMAGE_RECOLOR_OPA); return (lv_opa_t)v.num; } -static inline bool lv_obj_get_style_bg_img_tiled(const struct _lv_obj_t * obj, uint32_t part) +static inline bool lv_obj_get_style_bg_image_tiled(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BG_IMG_TILED); + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BG_IMAGE_TILED); return (bool)v.num; } -static inline lv_color_t lv_obj_get_style_border_color(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_color_t lv_obj_get_style_border_color(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BORDER_COLOR); return v.color; } -static inline lv_color_t lv_obj_get_style_border_color_filtered(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_color_t lv_obj_get_style_border_color_filtered(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = _lv_obj_style_apply_color_filter(obj, part, lv_obj_get_style_prop(obj, part, LV_STYLE_BORDER_COLOR)); + lv_style_value_t v = lv_obj_style_apply_color_filter(obj, part, lv_obj_get_style_prop(obj, part, + LV_STYLE_BORDER_COLOR)); return v.color; } -static inline lv_opa_t lv_obj_get_style_border_opa(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_opa_t lv_obj_get_style_border_opa(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BORDER_OPA); return (lv_opa_t)v.num; } -static inline lv_coord_t lv_obj_get_style_border_width(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_border_width(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BORDER_WIDTH); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_border_side_t lv_obj_get_style_border_side(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_border_side_t lv_obj_get_style_border_side(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BORDER_SIDE); return (lv_border_side_t)v.num; } -static inline bool lv_obj_get_style_border_post(const struct _lv_obj_t * obj, uint32_t part) +static inline bool lv_obj_get_style_border_post(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BORDER_POST); return (bool)v.num; } -static inline lv_coord_t lv_obj_get_style_outline_width(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_outline_width(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_OUTLINE_WIDTH); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_color_t lv_obj_get_style_outline_color(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_color_t lv_obj_get_style_outline_color(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_OUTLINE_COLOR); return v.color; } -static inline lv_color_t lv_obj_get_style_outline_color_filtered(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_color_t lv_obj_get_style_outline_color_filtered(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = _lv_obj_style_apply_color_filter(obj, part, lv_obj_get_style_prop(obj, part, LV_STYLE_OUTLINE_COLOR)); + lv_style_value_t v = lv_obj_style_apply_color_filter(obj, part, lv_obj_get_style_prop(obj, part, + LV_STYLE_OUTLINE_COLOR)); return v.color; } -static inline lv_opa_t lv_obj_get_style_outline_opa(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_opa_t lv_obj_get_style_outline_opa(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_OUTLINE_OPA); return (lv_opa_t)v.num; } -static inline lv_coord_t lv_obj_get_style_outline_pad(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_outline_pad(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_OUTLINE_PAD); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_shadow_width(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_shadow_width(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_SHADOW_WIDTH); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_shadow_ofs_x(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_shadow_offset_x(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_SHADOW_OFS_X); - return (lv_coord_t)v.num; + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_SHADOW_OFFSET_X); + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_shadow_ofs_y(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_shadow_offset_y(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_SHADOW_OFS_Y); - return (lv_coord_t)v.num; + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_SHADOW_OFFSET_Y); + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_shadow_spread(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_shadow_spread(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_SHADOW_SPREAD); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_color_t lv_obj_get_style_shadow_color(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_color_t lv_obj_get_style_shadow_color(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_SHADOW_COLOR); return v.color; } -static inline lv_color_t lv_obj_get_style_shadow_color_filtered(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_color_t lv_obj_get_style_shadow_color_filtered(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = _lv_obj_style_apply_color_filter(obj, part, lv_obj_get_style_prop(obj, part, LV_STYLE_SHADOW_COLOR)); + lv_style_value_t v = lv_obj_style_apply_color_filter(obj, part, lv_obj_get_style_prop(obj, part, + LV_STYLE_SHADOW_COLOR)); return v.color; } -static inline lv_opa_t lv_obj_get_style_shadow_opa(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_opa_t lv_obj_get_style_shadow_opa(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_SHADOW_OPA); return (lv_opa_t)v.num; } -static inline lv_opa_t lv_obj_get_style_img_opa(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_opa_t lv_obj_get_style_image_opa(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_IMG_OPA); + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_IMAGE_OPA); return (lv_opa_t)v.num; } -static inline lv_color_t lv_obj_get_style_img_recolor(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_color_t lv_obj_get_style_image_recolor(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_IMG_RECOLOR); + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_IMAGE_RECOLOR); return v.color; } -static inline lv_color_t lv_obj_get_style_img_recolor_filtered(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_color_t lv_obj_get_style_image_recolor_filtered(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = _lv_obj_style_apply_color_filter(obj, part, lv_obj_get_style_prop(obj, part, LV_STYLE_IMG_RECOLOR)); + lv_style_value_t v = lv_obj_style_apply_color_filter(obj, part, lv_obj_get_style_prop(obj, part, + LV_STYLE_IMAGE_RECOLOR)); return v.color; } -static inline lv_opa_t lv_obj_get_style_img_recolor_opa(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_opa_t lv_obj_get_style_image_recolor_opa(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_IMG_RECOLOR_OPA); + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_IMAGE_RECOLOR_OPA); return (lv_opa_t)v.num; } -static inline lv_coord_t lv_obj_get_style_line_width(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_line_width(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_LINE_WIDTH); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_line_dash_width(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_line_dash_width(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_LINE_DASH_WIDTH); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_line_dash_gap(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_line_dash_gap(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_LINE_DASH_GAP); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline bool lv_obj_get_style_line_rounded(const struct _lv_obj_t * obj, uint32_t part) +static inline bool lv_obj_get_style_line_rounded(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_LINE_ROUNDED); return (bool)v.num; } -static inline lv_color_t lv_obj_get_style_line_color(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_color_t lv_obj_get_style_line_color(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_LINE_COLOR); return v.color; } -static inline lv_color_t lv_obj_get_style_line_color_filtered(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_color_t lv_obj_get_style_line_color_filtered(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = _lv_obj_style_apply_color_filter(obj, part, lv_obj_get_style_prop(obj, part, LV_STYLE_LINE_COLOR)); + lv_style_value_t v = lv_obj_style_apply_color_filter(obj, part, lv_obj_get_style_prop(obj, part, LV_STYLE_LINE_COLOR)); return v.color; } -static inline lv_opa_t lv_obj_get_style_line_opa(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_opa_t lv_obj_get_style_line_opa(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_LINE_OPA); return (lv_opa_t)v.num; } -static inline lv_coord_t lv_obj_get_style_arc_width(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_arc_width(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_ARC_WIDTH); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline bool lv_obj_get_style_arc_rounded(const struct _lv_obj_t * obj, uint32_t part) +static inline bool lv_obj_get_style_arc_rounded(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_ARC_ROUNDED); return (bool)v.num; } -static inline lv_color_t lv_obj_get_style_arc_color(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_color_t lv_obj_get_style_arc_color(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_ARC_COLOR); return v.color; } -static inline lv_color_t lv_obj_get_style_arc_color_filtered(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_color_t lv_obj_get_style_arc_color_filtered(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = _lv_obj_style_apply_color_filter(obj, part, lv_obj_get_style_prop(obj, part, LV_STYLE_ARC_COLOR)); + lv_style_value_t v = lv_obj_style_apply_color_filter(obj, part, lv_obj_get_style_prop(obj, part, LV_STYLE_ARC_COLOR)); return v.color; } -static inline lv_opa_t lv_obj_get_style_arc_opa(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_opa_t lv_obj_get_style_arc_opa(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_ARC_OPA); return (lv_opa_t)v.num; } -static inline const void * lv_obj_get_style_arc_img_src(const struct _lv_obj_t * obj, uint32_t part) +static inline const void * lv_obj_get_style_arc_image_src(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_ARC_IMG_SRC); + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_ARC_IMAGE_SRC); return (const void *)v.ptr; } -static inline lv_color_t lv_obj_get_style_text_color(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_color_t lv_obj_get_style_text_color(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_TEXT_COLOR); return v.color; } -static inline lv_color_t lv_obj_get_style_text_color_filtered(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_color_t lv_obj_get_style_text_color_filtered(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = _lv_obj_style_apply_color_filter(obj, part, lv_obj_get_style_prop(obj, part, LV_STYLE_TEXT_COLOR)); + lv_style_value_t v = lv_obj_style_apply_color_filter(obj, part, lv_obj_get_style_prop(obj, part, LV_STYLE_TEXT_COLOR)); return v.color; } -static inline lv_opa_t lv_obj_get_style_text_opa(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_opa_t lv_obj_get_style_text_opa(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_TEXT_OPA); return (lv_opa_t)v.num; } -static inline const lv_font_t * lv_obj_get_style_text_font(const struct _lv_obj_t * obj, uint32_t part) +static inline const lv_font_t * lv_obj_get_style_text_font(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_TEXT_FONT); return (const lv_font_t *)v.ptr; } -static inline lv_coord_t lv_obj_get_style_text_letter_space(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_text_letter_space(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_TEXT_LETTER_SPACE); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_coord_t lv_obj_get_style_text_line_space(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_text_line_space(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_TEXT_LINE_SPACE); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline lv_text_decor_t lv_obj_get_style_text_decor(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_text_decor_t lv_obj_get_style_text_decor(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_TEXT_DECOR); return (lv_text_decor_t)v.num; } -static inline lv_text_align_t lv_obj_get_style_text_align(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_text_align_t lv_obj_get_style_text_align(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_TEXT_ALIGN); return (lv_text_align_t)v.num; } -static inline lv_coord_t lv_obj_get_style_radius(const struct _lv_obj_t * obj, uint32_t part) +static inline int32_t lv_obj_get_style_radius(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_RADIUS); - return (lv_coord_t)v.num; + return (int32_t)v.num; } -static inline bool lv_obj_get_style_clip_corner(const struct _lv_obj_t * obj, uint32_t part) +static inline bool lv_obj_get_style_clip_corner(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_CLIP_CORNER); return (bool)v.num; } -static inline lv_opa_t lv_obj_get_style_opa(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_opa_t lv_obj_get_style_opa(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_OPA); return (lv_opa_t)v.num; } -static inline const lv_color_filter_dsc_t * lv_obj_get_style_color_filter_dsc(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_opa_t lv_obj_get_style_opa_layered(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_OPA_LAYERED); + return (lv_opa_t)v.num; +} + +static inline const lv_color_filter_dsc_t * lv_obj_get_style_color_filter_dsc(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_COLOR_FILTER_DSC); return (const lv_color_filter_dsc_t *)v.ptr; } -static inline lv_opa_t lv_obj_get_style_color_filter_opa(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_opa_t lv_obj_get_style_color_filter_opa(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_COLOR_FILTER_OPA); return (lv_opa_t)v.num; } -static inline const lv_anim_t * lv_obj_get_style_anim(const struct _lv_obj_t * obj, uint32_t part) +static inline const lv_anim_t * lv_obj_get_style_anim(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_ANIM); return (const lv_anim_t *)v.ptr; } -static inline uint32_t lv_obj_get_style_anim_time(const struct _lv_obj_t * obj, uint32_t part) -{ - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_ANIM_TIME); - return (uint32_t)v.num; -} - -static inline uint32_t lv_obj_get_style_anim_speed(const struct _lv_obj_t * obj, uint32_t part) +static inline uint32_t lv_obj_get_style_anim_duration(const lv_obj_t * obj, lv_part_t part) { - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_ANIM_SPEED); + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_ANIM_DURATION); return (uint32_t)v.num; } -static inline const lv_style_transition_dsc_t * lv_obj_get_style_transition(const struct _lv_obj_t * obj, uint32_t part) +static inline const lv_style_transition_dsc_t * lv_obj_get_style_transition(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_TRANSITION); return (const lv_style_transition_dsc_t *)v.ptr; } -static inline lv_blend_mode_t lv_obj_get_style_blend_mode(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_blend_mode_t lv_obj_get_style_blend_mode(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BLEND_MODE); return (lv_blend_mode_t)v.num; } -static inline uint16_t lv_obj_get_style_layout(const struct _lv_obj_t * obj, uint32_t part) +static inline uint16_t lv_obj_get_style_layout(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_LAYOUT); return (uint16_t)v.num; } -static inline lv_base_dir_t lv_obj_get_style_base_dir(const struct _lv_obj_t * obj, uint32_t part) +static inline lv_base_dir_t lv_obj_get_style_base_dir(const lv_obj_t * obj, lv_part_t part) { lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BASE_DIR); return (lv_base_dir_t)v.num; } -void lv_obj_set_style_width(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_min_width(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_max_width(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_height(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_min_height(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_max_height(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_x(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_y(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_align(struct _lv_obj_t * obj, lv_align_t value, lv_style_selector_t selector); -void lv_obj_set_style_transform_width(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_transform_height(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_translate_x(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_translate_y(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_transform_zoom(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_transform_angle(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_transform_pivot_x(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_transform_pivot_y(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_pad_top(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_pad_bottom(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_pad_left(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_pad_right(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_pad_row(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_pad_column(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_bg_color(struct _lv_obj_t * obj, lv_color_t value, lv_style_selector_t selector); -void lv_obj_set_style_bg_opa(struct _lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); -void lv_obj_set_style_bg_grad_color(struct _lv_obj_t * obj, lv_color_t value, lv_style_selector_t selector); -void lv_obj_set_style_bg_grad_dir(struct _lv_obj_t * obj, lv_grad_dir_t value, lv_style_selector_t selector); -void lv_obj_set_style_bg_main_stop(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_bg_grad_stop(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_bg_grad(struct _lv_obj_t * obj, const lv_grad_dsc_t * value, lv_style_selector_t selector); -void lv_obj_set_style_bg_dither_mode(struct _lv_obj_t * obj, lv_dither_mode_t value, lv_style_selector_t selector); -void lv_obj_set_style_bg_img_src(struct _lv_obj_t * obj, const void * value, lv_style_selector_t selector); -void lv_obj_set_style_bg_img_opa(struct _lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); -void lv_obj_set_style_bg_img_recolor(struct _lv_obj_t * obj, lv_color_t value, lv_style_selector_t selector); -void lv_obj_set_style_bg_img_recolor_opa(struct _lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); -void lv_obj_set_style_bg_img_tiled(struct _lv_obj_t * obj, bool value, lv_style_selector_t selector); -void lv_obj_set_style_border_color(struct _lv_obj_t * obj, lv_color_t value, lv_style_selector_t selector); -void lv_obj_set_style_border_opa(struct _lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); -void lv_obj_set_style_border_width(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_border_side(struct _lv_obj_t * obj, lv_border_side_t value, lv_style_selector_t selector); -void lv_obj_set_style_border_post(struct _lv_obj_t * obj, bool value, lv_style_selector_t selector); -void lv_obj_set_style_outline_width(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_outline_color(struct _lv_obj_t * obj, lv_color_t value, lv_style_selector_t selector); -void lv_obj_set_style_outline_opa(struct _lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); -void lv_obj_set_style_outline_pad(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_shadow_width(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_shadow_ofs_x(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_shadow_ofs_y(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_shadow_spread(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_shadow_color(struct _lv_obj_t * obj, lv_color_t value, lv_style_selector_t selector); -void lv_obj_set_style_shadow_opa(struct _lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); -void lv_obj_set_style_img_opa(struct _lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); -void lv_obj_set_style_img_recolor(struct _lv_obj_t * obj, lv_color_t value, lv_style_selector_t selector); -void lv_obj_set_style_img_recolor_opa(struct _lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); -void lv_obj_set_style_line_width(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_line_dash_width(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_line_dash_gap(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_line_rounded(struct _lv_obj_t * obj, bool value, lv_style_selector_t selector); -void lv_obj_set_style_line_color(struct _lv_obj_t * obj, lv_color_t value, lv_style_selector_t selector); -void lv_obj_set_style_line_opa(struct _lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); -void lv_obj_set_style_arc_width(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_arc_rounded(struct _lv_obj_t * obj, bool value, lv_style_selector_t selector); -void lv_obj_set_style_arc_color(struct _lv_obj_t * obj, lv_color_t value, lv_style_selector_t selector); -void lv_obj_set_style_arc_opa(struct _lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); -void lv_obj_set_style_arc_img_src(struct _lv_obj_t * obj, const void * value, lv_style_selector_t selector); -void lv_obj_set_style_text_color(struct _lv_obj_t * obj, lv_color_t value, lv_style_selector_t selector); -void lv_obj_set_style_text_opa(struct _lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); -void lv_obj_set_style_text_font(struct _lv_obj_t * obj, const lv_font_t * value, lv_style_selector_t selector); -void lv_obj_set_style_text_letter_space(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_text_line_space(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_text_decor(struct _lv_obj_t * obj, lv_text_decor_t value, lv_style_selector_t selector); -void lv_obj_set_style_text_align(struct _lv_obj_t * obj, lv_text_align_t value, lv_style_selector_t selector); -void lv_obj_set_style_radius(struct _lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_clip_corner(struct _lv_obj_t * obj, bool value, lv_style_selector_t selector); -void lv_obj_set_style_opa(struct _lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); -void lv_obj_set_style_color_filter_dsc(struct _lv_obj_t * obj, const lv_color_filter_dsc_t * value, lv_style_selector_t selector); -void lv_obj_set_style_color_filter_opa(struct _lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); -void lv_obj_set_style_anim(struct _lv_obj_t * obj, const lv_anim_t * value, lv_style_selector_t selector); -void lv_obj_set_style_anim_time(struct _lv_obj_t * obj, uint32_t value, lv_style_selector_t selector); -void lv_obj_set_style_anim_speed(struct _lv_obj_t * obj, uint32_t value, lv_style_selector_t selector); -void lv_obj_set_style_transition(struct _lv_obj_t * obj, const lv_style_transition_dsc_t * value, lv_style_selector_t selector); -void lv_obj_set_style_blend_mode(struct _lv_obj_t * obj, lv_blend_mode_t value, lv_style_selector_t selector); -void lv_obj_set_style_layout(struct _lv_obj_t * obj, uint16_t value, lv_style_selector_t selector); -void lv_obj_set_style_base_dir(struct _lv_obj_t * obj, lv_base_dir_t value, lv_style_selector_t selector); +static inline const void * lv_obj_get_style_bitmap_mask_src(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_BITMAP_MASK_SRC); + return (const void *)v.ptr; +} + +static inline uint32_t lv_obj_get_style_rotary_sensitivity(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_ROTARY_SENSITIVITY); + return (uint32_t)v.num; +} + +#if LV_USE_FLEX +static inline lv_flex_flow_t lv_obj_get_style_flex_flow(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_FLEX_FLOW); + return (lv_flex_flow_t)v.num; +} + +static inline lv_flex_align_t lv_obj_get_style_flex_main_place(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_FLEX_MAIN_PLACE); + return (lv_flex_align_t)v.num; +} + +static inline lv_flex_align_t lv_obj_get_style_flex_cross_place(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_FLEX_CROSS_PLACE); + return (lv_flex_align_t)v.num; +} + +static inline lv_flex_align_t lv_obj_get_style_flex_track_place(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_FLEX_TRACK_PLACE); + return (lv_flex_align_t)v.num; +} + +static inline uint8_t lv_obj_get_style_flex_grow(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_FLEX_GROW); + return (uint8_t)v.num; +} + +#endif /*LV_USE_FLEX*/ + +#if LV_USE_GRID +static inline const int32_t * lv_obj_get_style_grid_column_dsc_array(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_GRID_COLUMN_DSC_ARRAY); + return (const int32_t *)v.ptr; +} + +static inline lv_grid_align_t lv_obj_get_style_grid_column_align(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_GRID_COLUMN_ALIGN); + return (lv_grid_align_t)v.num; +} + +static inline const int32_t * lv_obj_get_style_grid_row_dsc_array(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_GRID_ROW_DSC_ARRAY); + return (const int32_t *)v.ptr; +} + +static inline lv_grid_align_t lv_obj_get_style_grid_row_align(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_GRID_ROW_ALIGN); + return (lv_grid_align_t)v.num; +} + +static inline int32_t lv_obj_get_style_grid_cell_column_pos(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_GRID_CELL_COLUMN_POS); + return (int32_t)v.num; +} + +static inline lv_grid_align_t lv_obj_get_style_grid_cell_x_align(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_GRID_CELL_X_ALIGN); + return (lv_grid_align_t)v.num; +} + +static inline int32_t lv_obj_get_style_grid_cell_column_span(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_GRID_CELL_COLUMN_SPAN); + return (int32_t)v.num; +} + +static inline int32_t lv_obj_get_style_grid_cell_row_pos(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_GRID_CELL_ROW_POS); + return (int32_t)v.num; +} + +static inline lv_grid_align_t lv_obj_get_style_grid_cell_y_align(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_GRID_CELL_Y_ALIGN); + return (lv_grid_align_t)v.num; +} + +static inline int32_t lv_obj_get_style_grid_cell_row_span(const lv_obj_t * obj, lv_part_t part) +{ + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_GRID_CELL_ROW_SPAN); + return (int32_t)v.num; +} + +#endif /*LV_USE_GRID*/ + +void lv_obj_set_style_width(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_min_width(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_max_width(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_height(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_min_height(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_max_height(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_length(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_x(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_y(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_align(lv_obj_t * obj, lv_align_t value, lv_style_selector_t selector); +void lv_obj_set_style_transform_width(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_transform_height(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_translate_x(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_translate_y(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_transform_scale_x(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_transform_scale_y(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_transform_rotation(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_transform_pivot_x(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_transform_pivot_y(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_transform_skew_x(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_transform_skew_y(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_pad_top(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_pad_bottom(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_pad_left(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_pad_right(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_pad_row(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_pad_column(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_margin_top(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_margin_bottom(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_margin_left(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_margin_right(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_bg_color(lv_obj_t * obj, lv_color_t value, lv_style_selector_t selector); +void lv_obj_set_style_bg_opa(lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); +void lv_obj_set_style_bg_grad_color(lv_obj_t * obj, lv_color_t value, lv_style_selector_t selector); +void lv_obj_set_style_bg_grad_dir(lv_obj_t * obj, lv_grad_dir_t value, lv_style_selector_t selector); +void lv_obj_set_style_bg_main_stop(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_bg_grad_stop(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_bg_main_opa(lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); +void lv_obj_set_style_bg_grad_opa(lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); +void lv_obj_set_style_bg_grad(lv_obj_t * obj, const lv_grad_dsc_t * value, lv_style_selector_t selector); +void lv_obj_set_style_bg_image_src(lv_obj_t * obj, const void * value, lv_style_selector_t selector); +void lv_obj_set_style_bg_image_opa(lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); +void lv_obj_set_style_bg_image_recolor(lv_obj_t * obj, lv_color_t value, lv_style_selector_t selector); +void lv_obj_set_style_bg_image_recolor_opa(lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); +void lv_obj_set_style_bg_image_tiled(lv_obj_t * obj, bool value, lv_style_selector_t selector); +void lv_obj_set_style_border_color(lv_obj_t * obj, lv_color_t value, lv_style_selector_t selector); +void lv_obj_set_style_border_opa(lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); +void lv_obj_set_style_border_width(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_border_side(lv_obj_t * obj, lv_border_side_t value, lv_style_selector_t selector); +void lv_obj_set_style_border_post(lv_obj_t * obj, bool value, lv_style_selector_t selector); +void lv_obj_set_style_outline_width(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_outline_color(lv_obj_t * obj, lv_color_t value, lv_style_selector_t selector); +void lv_obj_set_style_outline_opa(lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); +void lv_obj_set_style_outline_pad(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_shadow_width(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_shadow_offset_x(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_shadow_offset_y(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_shadow_spread(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_shadow_color(lv_obj_t * obj, lv_color_t value, lv_style_selector_t selector); +void lv_obj_set_style_shadow_opa(lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); +void lv_obj_set_style_image_opa(lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); +void lv_obj_set_style_image_recolor(lv_obj_t * obj, lv_color_t value, lv_style_selector_t selector); +void lv_obj_set_style_image_recolor_opa(lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); +void lv_obj_set_style_line_width(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_line_dash_width(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_line_dash_gap(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_line_rounded(lv_obj_t * obj, bool value, lv_style_selector_t selector); +void lv_obj_set_style_line_color(lv_obj_t * obj, lv_color_t value, lv_style_selector_t selector); +void lv_obj_set_style_line_opa(lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); +void lv_obj_set_style_arc_width(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_arc_rounded(lv_obj_t * obj, bool value, lv_style_selector_t selector); +void lv_obj_set_style_arc_color(lv_obj_t * obj, lv_color_t value, lv_style_selector_t selector); +void lv_obj_set_style_arc_opa(lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); +void lv_obj_set_style_arc_image_src(lv_obj_t * obj, const void * value, lv_style_selector_t selector); +void lv_obj_set_style_text_color(lv_obj_t * obj, lv_color_t value, lv_style_selector_t selector); +void lv_obj_set_style_text_opa(lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); +void lv_obj_set_style_text_font(lv_obj_t * obj, const lv_font_t * value, lv_style_selector_t selector); +void lv_obj_set_style_text_letter_space(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_text_line_space(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_text_decor(lv_obj_t * obj, lv_text_decor_t value, lv_style_selector_t selector); +void lv_obj_set_style_text_align(lv_obj_t * obj, lv_text_align_t value, lv_style_selector_t selector); +void lv_obj_set_style_radius(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_clip_corner(lv_obj_t * obj, bool value, lv_style_selector_t selector); +void lv_obj_set_style_opa(lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); +void lv_obj_set_style_opa_layered(lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); +void lv_obj_set_style_color_filter_dsc(lv_obj_t * obj, const lv_color_filter_dsc_t * value, lv_style_selector_t selector); +void lv_obj_set_style_color_filter_opa(lv_obj_t * obj, lv_opa_t value, lv_style_selector_t selector); +void lv_obj_set_style_anim(lv_obj_t * obj, const lv_anim_t * value, lv_style_selector_t selector); +void lv_obj_set_style_anim_duration(lv_obj_t * obj, uint32_t value, lv_style_selector_t selector); +void lv_obj_set_style_transition(lv_obj_t * obj, const lv_style_transition_dsc_t * value, lv_style_selector_t selector); +void lv_obj_set_style_blend_mode(lv_obj_t * obj, lv_blend_mode_t value, lv_style_selector_t selector); +void lv_obj_set_style_layout(lv_obj_t * obj, uint16_t value, lv_style_selector_t selector); +void lv_obj_set_style_base_dir(lv_obj_t * obj, lv_base_dir_t value, lv_style_selector_t selector); +void lv_obj_set_style_bitmap_mask_src(lv_obj_t * obj, const void * value, lv_style_selector_t selector); +void lv_obj_set_style_rotary_sensitivity(lv_obj_t * obj, uint32_t value, lv_style_selector_t selector); +#if LV_USE_FLEX +void lv_obj_set_style_flex_flow(lv_obj_t * obj, lv_flex_flow_t value, lv_style_selector_t selector); +void lv_obj_set_style_flex_main_place(lv_obj_t * obj, lv_flex_align_t value, lv_style_selector_t selector); +void lv_obj_set_style_flex_cross_place(lv_obj_t * obj, lv_flex_align_t value, lv_style_selector_t selector); +void lv_obj_set_style_flex_track_place(lv_obj_t * obj, lv_flex_align_t value, lv_style_selector_t selector); +void lv_obj_set_style_flex_grow(lv_obj_t * obj, uint8_t value, lv_style_selector_t selector); +#endif /*LV_USE_FLEX*/ + +#if LV_USE_GRID +void lv_obj_set_style_grid_column_dsc_array(lv_obj_t * obj, const int32_t * value, lv_style_selector_t selector); +void lv_obj_set_style_grid_column_align(lv_obj_t * obj, lv_grid_align_t value, lv_style_selector_t selector); +void lv_obj_set_style_grid_row_dsc_array(lv_obj_t * obj, const int32_t * value, lv_style_selector_t selector); +void lv_obj_set_style_grid_row_align(lv_obj_t * obj, lv_grid_align_t value, lv_style_selector_t selector); +void lv_obj_set_style_grid_cell_column_pos(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_grid_cell_x_align(lv_obj_t * obj, lv_grid_align_t value, lv_style_selector_t selector); +void lv_obj_set_style_grid_cell_column_span(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_grid_cell_row_pos(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +void lv_obj_set_style_grid_cell_y_align(lv_obj_t * obj, lv_grid_align_t value, lv_style_selector_t selector); +void lv_obj_set_style_grid_cell_row_span(lv_obj_t * obj, int32_t value, lv_style_selector_t selector); +#endif /*LV_USE_GRID*/ + + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LV_OBJ_STYLE_GEN_H */ diff --git a/include/liblvgl/core/lv_obj_style_private.h b/include/liblvgl/core/lv_obj_style_private.h new file mode 100644 index 00000000..e6be1279 --- /dev/null +++ b/include/liblvgl/core/lv_obj_style_private.h @@ -0,0 +1,95 @@ +/** + * @file lv_obj_style_private.h + * + */ + +#ifndef LV_OBJ_STYLE_PRIVATE_H +#define LV_OBJ_STYLE_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_obj_style.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_obj_style_t { + const lv_style_t * style; + uint32_t selector : 24; + uint32_t is_local : 1; + uint32_t is_trans : 1; +}; + +struct _lv_obj_style_transition_dsc_t { + uint16_t time; + uint16_t delay; + lv_style_selector_t selector; + lv_style_prop_t prop; + lv_anim_path_cb_t path_cb; + void * user_data; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Initialize the object related style manager module. + * Called by LVGL in `lv_init()` + */ +void lv_obj_style_init(void); + +/** + * Deinitialize the object related style manager module. + * Called by LVGL in `lv_deinit()` + */ +void lv_obj_style_deinit(void); + +/** + * Used internally to create a style transition + * @param obj + * @param part + * @param prev_state + * @param new_state + * @param tr + */ +void lv_obj_style_create_transition(lv_obj_t * obj, lv_part_t part, lv_state_t prev_state, + lv_state_t new_state, const lv_obj_style_transition_dsc_t * tr); + +/** + * Used internally to compare the appearance of an object in 2 states + * @param obj + * @param state1 + * @param state2 + * @return + */ +lv_style_state_cmp_t lv_obj_style_state_compare(lv_obj_t * obj, lv_state_t state1, lv_state_t state2); + +/** + * Update the layer type of a widget bayed on its current styles. + * The result will be stored in `obj->spec_attr->layer_type` + * @param obj the object whose layer should be updated + */ +void lv_obj_update_layer_type(lv_obj_t * obj); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_OBJ_STYLE_PRIVATE_H*/ diff --git a/include/liblvgl/core/lv_obj_tree.h b/include/liblvgl/core/lv_obj_tree.h index bee9e160..9f679dd4 100644 --- a/include/liblvgl/core/lv_obj_tree.h +++ b/include/liblvgl/core/lv_obj_tree.h @@ -1,5 +1,5 @@ /** - * @file struct _lv_obj_tree.h + * @file lv_obj_tree.h * */ @@ -13,28 +13,25 @@ extern "C" { /********************* * INCLUDES *********************/ -#include -#include +#include "../misc/lv_types.h" +#include "../misc/lv_anim.h" +#include "../display/lv_display.h" /********************* * DEFINES *********************/ - /********************** * TYPEDEFS **********************/ -struct _lv_obj_t; -struct _lv_obj_class_t; - typedef enum { LV_OBJ_TREE_WALK_NEXT, LV_OBJ_TREE_WALK_SKIP_CHILDREN, LV_OBJ_TREE_WALK_END, } lv_obj_tree_walk_res_t; -typedef lv_obj_tree_walk_res_t (*lv_obj_tree_walk_cb_t)(struct _lv_obj_t *, void *); +typedef lv_obj_tree_walk_res_t (*lv_obj_tree_walk_cb_t)(lv_obj_t *, void *); /********************** * GLOBAL PROTOTYPES @@ -46,7 +43,7 @@ typedef lv_obj_tree_walk_res_t (*lv_obj_tree_walk_cb_t)(struct _lv_obj_t *, void * Send `LV_EVENT_DELETED` to deleted objects. * @param obj pointer to an object */ -void lv_obj_del(struct _lv_obj_t * obj); +void lv_obj_delete(lv_obj_t * obj); /** * Delete all children of an object. @@ -54,20 +51,20 @@ void lv_obj_del(struct _lv_obj_t * obj); * Send `LV_EVENT_DELETED` to deleted objects. * @param obj pointer to an object */ -void lv_obj_clean(struct _lv_obj_t * obj); +void lv_obj_clean(lv_obj_t * obj); /** * Delete an object after some delay * @param obj pointer to an object * @param delay_ms time to wait before delete in milliseconds */ -void lv_obj_del_delayed(struct _lv_obj_t * obj, uint32_t delay_ms); +void lv_obj_delete_delayed(lv_obj_t * obj, uint32_t delay_ms); /** * A function to be easily used in animation ready callback to delete an object when the animation is ready * @param a pointer to the animation */ -void lv_obj_del_anim_ready_cb(lv_anim_t * a); +void lv_obj_delete_anim_completed_cb(lv_anim_t * a); /** * Helper function for asynchronously deleting objects. @@ -75,7 +72,7 @@ void lv_obj_del_anim_ready_cb(lv_anim_t * a); * @param obj object to delete * @see lv_async_call */ -void lv_obj_del_async(struct _lv_obj_t * obj); +void lv_obj_delete_async(lv_obj_t * obj); /** * Move the parent of an object. The relative coordinates will be kept. @@ -83,7 +80,7 @@ void lv_obj_del_async(struct _lv_obj_t * obj); * @param obj pointer to an object whose parent needs to be changed * @param parent pointer to the new parent */ -void lv_obj_set_parent(struct _lv_obj_t * obj, struct _lv_obj_t * parent); +void lv_obj_set_parent(lv_obj_t * obj, lv_obj_t * parent); /** * Swap the positions of two objects. @@ -91,7 +88,7 @@ void lv_obj_set_parent(struct _lv_obj_t * obj, struct _lv_obj_t * parent); * @param obj1 pointer to the first object * @param obj2 pointer to the second object */ -void lv_obj_swap(struct _lv_obj_t * obj1, struct _lv_obj_t * obj2); +void lv_obj_swap(lv_obj_t * obj1, lv_obj_t * obj2); /** * moves the object to the given index in its parent. @@ -101,33 +98,33 @@ void lv_obj_swap(struct _lv_obj_t * obj1, struct _lv_obj_t * obj2); * @note to move to the background: lv_obj_move_to_index(obj, 0) * @note to move forward (up): lv_obj_move_to_index(obj, lv_obj_get_index(obj) - 1) */ -void lv_obj_move_to_index(struct _lv_obj_t * obj, int32_t index); +void lv_obj_move_to_index(lv_obj_t * obj, int32_t index); /** * Get the screen of an object * @param obj pointer to an object * @return pointer to the object's screen */ -struct _lv_obj_t * lv_obj_get_screen(const struct _lv_obj_t * obj); +lv_obj_t * lv_obj_get_screen(const lv_obj_t * obj); /** * Get the display of the object * @param obj pointer to an object * @return pointer to the object's display */ -lv_disp_t * lv_obj_get_disp(const struct _lv_obj_t * obj); +lv_display_t * lv_obj_get_display(const lv_obj_t * obj); /** * Get the parent of an object * @param obj pointer to an object * @return the parent of the object. (NULL if `obj` was a screen) */ -struct _lv_obj_t * lv_obj_get_parent(const struct _lv_obj_t * obj); +lv_obj_t * lv_obj_get_parent(const lv_obj_t * obj); /** * Get the child of an object by the child's index. * @param obj pointer to an object whose child should be get - * @param id the index of the child. + * @param idx the index of the child. * 0: the oldest (firstly created) child * 1: the second oldest * child count-1: the youngest @@ -135,22 +132,85 @@ struct _lv_obj_t * lv_obj_get_parent(const struct _lv_obj_t * obj); * -2: the second youngest * @return pointer to the child or NULL if the index was invalid */ -struct _lv_obj_t * lv_obj_get_child(const struct _lv_obj_t * obj, int32_t id); +lv_obj_t * lv_obj_get_child(const lv_obj_t * obj, int32_t idx); + +/** + * Get the child of an object by the child's index. Consider the children only with a given type. + * @param obj pointer to an object whose child should be get + * @param idx the index of the child. + * 0: the oldest (firstly created) child + * 1: the second oldest + * child count-1: the youngest + * -1: the youngest + * -2: the second youngest + * @param class_p the type of the children to check + * @return pointer to the child or NULL if the index was invalid + */ +lv_obj_t * lv_obj_get_child_by_type(const lv_obj_t * obj, int32_t idx, + const lv_obj_class_t * class_p); + +/** + * Return a sibling of an object + * @param obj pointer to an object whose sibling should be get + * @param idx 0: `obj` itself + * -1: the first older sibling + * -2: the next older sibling + * 1: the first younger sibling + * 2: the next younger sibling + * etc + * @return pointer to the requested sibling or NULL if there is no such sibling + */ +lv_obj_t * lv_obj_get_sibling(const lv_obj_t * obj, int32_t idx); + +/** + * Return a sibling of an object. Consider the siblings only with a given type. + * @param obj pointer to an object whose sibling should be get + * @param idx 0: `obj` itself + * -1: the first older sibling + * -2: the next older sibling + * 1: the first younger sibling + * 2: the next younger sibling + * etc + * @param class_p the type of the children to check + * @return pointer to the requested sibling or NULL if there is no such sibling + */ +lv_obj_t * lv_obj_get_sibling_by_type(const lv_obj_t * obj, int32_t idx, + const lv_obj_class_t * class_p); /** * Get the number of children * @param obj pointer to an object * @return the number of children */ -uint32_t lv_obj_get_child_cnt(const struct _lv_obj_t * obj); +uint32_t lv_obj_get_child_count(const lv_obj_t * obj); + +/** + * Get the number of children having a given type. + * @param obj pointer to an object + * @param class_p the type of the children to check + * @return the number of children + */ + +uint32_t lv_obj_get_child_count_by_type(const lv_obj_t * obj, const lv_obj_class_t * class_p); /** * Get the index of a child. * @param obj pointer to an object * @return the child index of the object. - * E.g. 0: the oldest (firstly created child) + * E.g. 0: the oldest (firstly created child). + * (-1 if child could not be found or no parent exists) */ -uint32_t lv_obj_get_index(const struct _lv_obj_t * obj); +int32_t lv_obj_get_index(const lv_obj_t * obj); + +/** + * Get the index of a child. Consider the children only with a given type. + * @param obj pointer to an object + * @param class_p the type of the children to check + * @return the child index of the object. + * E.g. 0: the oldest (firstly created child with the given class). + * (-1 if child could not be found or no parent exists) + */ +int32_t lv_obj_get_index_by_type(const lv_obj_t * obj, const lv_obj_class_t * class_p); /** * Iterate through all children of any object. @@ -158,13 +218,18 @@ uint32_t lv_obj_get_index(const struct _lv_obj_t * obj); * @param cb call this callback on the objects * @param user_data pointer to any user related data (will be passed to `cb`) */ -void lv_obj_tree_walk(struct _lv_obj_t * start_obj, lv_obj_tree_walk_cb_t cb, void * user_data); +void lv_obj_tree_walk(lv_obj_t * start_obj, lv_obj_tree_walk_cb_t cb, void * user_data); + +/** + * Iterate through all children of any object and print their ID. + * @param start_obj start integrating from this object + */ +void lv_obj_dump_tree(lv_obj_t * start_obj); /********************** * MACROS **********************/ - #ifdef __cplusplus } /*extern "C"*/ #endif diff --git a/include/liblvgl/core/lv_refr.h b/include/liblvgl/core/lv_refr.h index 72e8d6c3..6b756edb 100644 --- a/include/liblvgl/core/lv_refr.h +++ b/include/liblvgl/core/lv_refr.h @@ -14,14 +14,13 @@ extern "C" { * INCLUDES *********************/ #include "lv_obj.h" -#include +#include "../display/lv_display.h" +#include "../misc/lv_types.h" /********************* * DEFINES *********************/ -#define LV_REFR_TASK_PRIO LV_TASK_PRIO_MID - /********************** * TYPEDEFS **********************/ @@ -42,11 +41,6 @@ extern "C" { * GLOBAL FUNCTIONS **********************/ -/** - * Initialize the screen refresh subsystem - */ -void _lv_refr_init(void); - /** * Redraw the invalidated areas now. * Normally the redrawing is periodically executed in `lv_timer_handler` but a long blocking process @@ -54,55 +48,14 @@ void _lv_refr_init(void); * (e.g. progress bar) this function can be called when the screen should be updated. * @param disp pointer to display to refresh. NULL to refresh all displays. */ -void lv_refr_now(lv_disp_t * disp); +void lv_refr_now(lv_display_t * disp); /** - * Redrawn on object an all its children using the passed draw context - * @param draw pointer to an initialized draw context + * Redrawn on object and all its children using the passed draw context + * @param layer pointer to a layer where to draw. * @param obj the start object from the redraw should start */ -void lv_obj_redraw(lv_draw_ctx_t * draw_ctx, lv_obj_t * obj); - -/** - * Invalidate an area on display to redraw it - * @param area_p pointer to area which should be invalidated (NULL: delete the invalidated areas) - * @param disp pointer to display where the area should be invalidated (NULL can be used if there is - * only one display) - */ -void _lv_inv_area(lv_disp_t * disp, const lv_area_t * area_p); - -/** - * Get the display which is being refreshed - * @return the display being refreshed - */ -lv_disp_t * _lv_refr_get_disp_refreshing(void); - -/** - * Set the display which is being refreshed. - * It shouldn't be used directly by the user. - * It can be used to trick the drawing functions about there is an active display. - * @param the display being refreshed - */ -void _lv_refr_set_disp_refreshing(lv_disp_t * disp); - -#if LV_USE_PERF_MONITOR -/** - * Reset FPS counter - */ -void lv_refr_reset_fps_counter(void); - -/** - * Get the average FPS - * @return the average FPS - */ -uint32_t lv_refr_get_fps_avg(void); -#endif - -/** - * Called periodically to handle the refreshing - * @param timer pointer to the timer itself - */ -void _lv_disp_refr_timer(lv_timer_t * timer); +void lv_obj_redraw(lv_layer_t * layer, lv_obj_t * obj); /********************** * STATIC FUNCTIONS diff --git a/include/liblvgl/core/lv_refr_private.h b/include/liblvgl/core/lv_refr_private.h new file mode 100644 index 00000000..8f840780 --- /dev/null +++ b/include/liblvgl/core/lv_refr_private.h @@ -0,0 +1,75 @@ +/** + * @file lv_refr_private.h + * + */ + +#ifndef LV_REFR_PRIVATE_H +#define LV_REFR_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_refr.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Initialize the screen refresh subsystem + */ +void lv_refr_init(void); + +/** + * Deinitialize the screen refresh subsystem + */ +void lv_refr_deinit(void); + +/** + * Invalidate an area on display to redraw it + * @param area_p pointer to area which should be invalidated (NULL: delete the invalidated areas) + * @param disp pointer to display where the area should be invalidated (NULL can be used if there is + * only one display) + */ +void lv_inv_area(lv_display_t * disp, const lv_area_t * area_p); + +/** + * Get the display which is being refreshed + * @return the display being refreshed + */ +lv_display_t * lv_refr_get_disp_refreshing(void); + +/** + * Set the display which is being refreshed + * @param disp the display being refreshed + */ +void lv_refr_set_disp_refreshing(lv_display_t * disp); + +/** + * Called periodically to handle the refreshing + * @param timer pointer to the timer itself + */ +void lv_display_refr_timer(lv_timer_t * timer); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_REFR_PRIVATE_H*/ diff --git a/include/liblvgl/display/lv_display.h b/include/liblvgl/display/lv_display.h new file mode 100644 index 00000000..2887ab69 --- /dev/null +++ b/include/liblvgl/display/lv_display.h @@ -0,0 +1,629 @@ +/** + * @file lv_display.h + * + */ + +#ifndef LV_DISPLAY_H +#define LV_DISPLAY_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../misc/lv_types.h" +#include "../misc/lv_timer.h" +#include "../misc/lv_event.h" +#include "../misc/lv_color.h" +#include "../draw/lv_draw.h" + +/********************* + * DEFINES + *********************/ + +#ifndef LV_ATTRIBUTE_FLUSH_READY +#define LV_ATTRIBUTE_FLUSH_READY +#endif + +/********************** + * TYPEDEFS + **********************/ + +typedef enum { + LV_DISPLAY_ROTATION_0 = 0, + LV_DISPLAY_ROTATION_90, + LV_DISPLAY_ROTATION_180, + LV_DISPLAY_ROTATION_270 +} lv_display_rotation_t; + +typedef enum { + /** + * Use the buffer(s) to render the screen is smaller parts. + * This way the buffers can be smaller then the display to save RAM. At least 1/10 screen size buffer(s) are recommended. + */ + LV_DISPLAY_RENDER_MODE_PARTIAL, + + /** + * The buffer(s) has to be screen sized and LVGL will render into the correct location of the buffer. + * This way the buffer always contain the whole image. Only the changed ares will be updated. + * With 2 buffers the buffers' content are kept in sync automatically and in flush_cb only address change is required. + */ + LV_DISPLAY_RENDER_MODE_DIRECT, + + /** + * Always redraw the whole screen even if only one pixel has been changed. + * With 2 buffers in flush_cb only and address change is required. + */ + LV_DISPLAY_RENDER_MODE_FULL, +} lv_display_render_mode_t; + +typedef enum { + LV_SCR_LOAD_ANIM_NONE, + LV_SCR_LOAD_ANIM_OVER_LEFT, + LV_SCR_LOAD_ANIM_OVER_RIGHT, + LV_SCR_LOAD_ANIM_OVER_TOP, + LV_SCR_LOAD_ANIM_OVER_BOTTOM, + LV_SCR_LOAD_ANIM_MOVE_LEFT, + LV_SCR_LOAD_ANIM_MOVE_RIGHT, + LV_SCR_LOAD_ANIM_MOVE_TOP, + LV_SCR_LOAD_ANIM_MOVE_BOTTOM, + LV_SCR_LOAD_ANIM_FADE_IN, + LV_SCR_LOAD_ANIM_FADE_ON = LV_SCR_LOAD_ANIM_FADE_IN, /*For backward compatibility*/ + LV_SCR_LOAD_ANIM_FADE_OUT, + LV_SCR_LOAD_ANIM_OUT_LEFT, + LV_SCR_LOAD_ANIM_OUT_RIGHT, + LV_SCR_LOAD_ANIM_OUT_TOP, + LV_SCR_LOAD_ANIM_OUT_BOTTOM, +} lv_screen_load_anim_t; + +typedef void (*lv_display_flush_cb_t)(lv_display_t * disp, const lv_area_t * area, uint8_t * px_map); +typedef void (*lv_display_flush_wait_cb_t)(lv_display_t * disp); + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create a new display with the given resolution + * @param hor_res horizontal resolution in pixels + * @param ver_res vertical resolution in pixels + * @return pointer to a display object or `NULL` on error + */ +lv_display_t * lv_display_create(int32_t hor_res, int32_t ver_res); + +/** + * Remove a display + * @param disp pointer to display + */ +void lv_display_delete(lv_display_t * disp); + +/** + * Set a default display. The new screens will be created on it by default. + * @param disp pointer to a display + */ +void lv_display_set_default(lv_display_t * disp); + +/** + * Get the default display + * @return pointer to the default display + */ +lv_display_t * lv_display_get_default(void); + +/** + * Get the next display. + * @param disp pointer to the current display. NULL to initialize. + * @return the next display or NULL if no more. Gives the first display when the parameter is NULL. + */ +lv_display_t * lv_display_get_next(lv_display_t * disp); + +/*--------------------- + * RESOLUTION + *--------------------*/ + +/** + * Sets the resolution of a display. `LV_EVENT_RESOLUTION_CHANGED` event will be sent. + * Here the native resolution of the device should be set. If the display will be rotated later with + * `lv_display_set_rotation` LVGL will swap the hor. and ver. resolution automatically. + * @param disp pointer to a display + * @param hor_res the new horizontal resolution + * @param ver_res the new vertical resolution + */ +void lv_display_set_resolution(lv_display_t * disp, int32_t hor_res, int32_t ver_res); + +/** + * It's not mandatory to use the whole display for LVGL, however in some cases physical resolution is important. + * For example the touchpad still sees whole resolution and the values needs to be converted + * to the active LVGL display area. + * @param disp pointer to a display + * @param hor_res the new physical horizontal resolution, or -1 to assume it's the same as the normal hor. res. + * @param ver_res the new physical vertical resolution, or -1 to assume it's the same as the normal hor. res. + */ +void lv_display_set_physical_resolution(lv_display_t * disp, int32_t hor_res, int32_t ver_res); + +/** + * If physical resolution is not the same as the normal resolution + * the offset of the active display area can be set here. + * @param disp pointer to a display + * @param x X offset + * @param y Y offset + */ +void lv_display_set_offset(lv_display_t * disp, int32_t x, int32_t y); + +/** + * Set the rotation of this display. LVGL will swap the horizontal and vertical resolutions internally. + * @param disp pointer to a display (NULL to use the default display) + * @param rotation `LV_DISPLAY_ROTATION_0/90/180/270` + */ +void lv_display_set_rotation(lv_display_t * disp, lv_display_rotation_t rotation); + +/** + * Set the DPI (dot per inch) of the display. + * dpi = sqrt(hor_res^2 + ver_res^2) / diagonal" + * @param disp pointer to a display + * @param dpi the new DPI + */ +void lv_display_set_dpi(lv_display_t * disp, int32_t dpi); + +/** + * Get the horizontal resolution of a display. + * @param disp pointer to a display (NULL to use the default display) + * @return the horizontal resolution of the display. + */ +int32_t lv_display_get_horizontal_resolution(const lv_display_t * disp); + +/** + * Get the vertical resolution of a display + * @param disp pointer to a display (NULL to use the default display) + * @return the vertical resolution of the display + */ +int32_t lv_display_get_vertical_resolution(const lv_display_t * disp); + +/** + * Get the physical horizontal resolution of a display + * @param disp pointer to a display (NULL to use the default display) + * @return the physical horizontal resolution of the display + */ +int32_t lv_display_get_physical_horizontal_resolution(const lv_display_t * disp); + +/** + * Get the physical vertical resolution of a display + * @param disp pointer to a display (NULL to use the default display) + * @return the physical vertical resolution of the display + */ +int32_t lv_display_get_physical_vertical_resolution(const lv_display_t * disp); + +/** + * Get the horizontal offset from the full / physical display + * @param disp pointer to a display (NULL to use the default display) + * @return the horizontal offset from the physical display + */ +int32_t lv_display_get_offset_x(const lv_display_t * disp); + +/** + * Get the vertical offset from the full / physical display + * @param disp pointer to a display (NULL to use the default display) + * @return the horizontal offset from the physical display + */ +int32_t lv_display_get_offset_y(const lv_display_t * disp); + +/** + * Get the current rotation of this display. + * @param disp pointer to a display (NULL to use the default display) + * @return the current rotation + */ +lv_display_rotation_t lv_display_get_rotation(lv_display_t * disp); + +/** + * Get the DPI of the display + * @param disp pointer to a display (NULL to use the default display) + * @return dpi of the display + */ +int32_t lv_display_get_dpi(const lv_display_t * disp); + +/*--------------------- + * BUFFERING + *--------------------*/ + +/** + * Set the buffers for a display, similarly to `lv_display_set_draw_buffers`, but accept the raw buffer pointers. + * For DIRECT/FULL rending modes, the buffer size must be at least + * `hor_res * ver_res * lv_color_format_get_size(lv_display_get_color_format(disp))` + * @param disp pointer to a display + * @param buf1 first buffer + * @param buf2 second buffer (can be `NULL`) + * @param buf_size buffer size in byte + * @param render_mode LV_DISPLAY_RENDER_MODE_PARTIAL/DIRECT/FULL + */ +void lv_display_set_buffers(lv_display_t * disp, void * buf1, void * buf2, uint32_t buf_size, + lv_display_render_mode_t render_mode); + +/** + * Set the frame buffers for a display, similarly to `lv_display_set_buffers`, but allow + * for a custom stride as required by a display controller. + * This allows the frame buffers to have a stride alignment different from the rest of + * the buffers` + * @param disp pointer to a display + * @param buf1 first buffer + * @param buf2 second buffer (can be `NULL`) + * @param buf_size buffer size in byte + * @param stride buffer stride in bytes + * @param render_mode LV_DISPLAY_RENDER_MODE_PARTIAL/DIRECT/FULL + */ +void lv_display_set_buffers_with_stride(lv_display_t * disp, void * buf1, void * buf2, uint32_t buf_size, + uint32_t stride, lv_display_render_mode_t render_mode); + +/** + * Set the buffers for a display, accept a draw buffer pointer. + * Normally use `lv_display_set_buffers` is enough for most cases. + * Use this function when an existing lv_draw_buf_t is available. + * @param disp pointer to a display + * @param buf1 first buffer + * @param buf2 second buffer (can be `NULL`) + */ +void lv_display_set_draw_buffers(lv_display_t * disp, lv_draw_buf_t * buf1, lv_draw_buf_t * buf2); + +/** + * Set display render mode + * @param disp pointer to a display + * @param render_mode LV_DISPLAY_RENDER_MODE_PARTIAL/DIRECT/FULL + */ +void lv_display_set_render_mode(lv_display_t * disp, lv_display_render_mode_t render_mode); + +/** + * Set the flush callback which will be called to copy the rendered image to the display. + * @param disp pointer to a display + * @param flush_cb the flush callback (`px_map` contains the rendered image as raw pixel map and it should be copied to `area` on the display) + */ +void lv_display_set_flush_cb(lv_display_t * disp, lv_display_flush_cb_t flush_cb); + +/** + * Set a callback to be used while LVGL is waiting flushing to be finished. + * It can do any complex logic to wait, including semaphores, mutexes, polling flags, etc. + * If not set the `disp->flushing` flag is used which can be cleared with `lv_display_flush_ready()` + * @param disp pointer to a display + * @param wait_cb a callback to call while LVGL is waiting for flush ready. + * If NULL `lv_display_flush_ready()` can be used to signal that flushing is ready. + */ +void lv_display_set_flush_wait_cb(lv_display_t * disp, lv_display_flush_wait_cb_t wait_cb); + +/** + * Set the color format of the display. + * @param disp pointer to a display + * @param color_format Possible values are + * - LV_COLOR_FORMAT_RGB565 + * - LV_COLOR_FORMAT_RGB888 + * - LV_COLOR_FORMAT_XRGB888 + * - LV_COLOR_FORMAT_ARGB888 + *@note To change the endianness of the rendered image in case of RGB565 format + * (i.e. swap the 2 bytes) call `lv_draw_sw_rgb565_swap` in the flush_cb + */ +void lv_display_set_color_format(lv_display_t * disp, lv_color_format_t color_format); + +/** + * Get the color format of the display + * @param disp pointer to a display + * @return the color format + */ +lv_color_format_t lv_display_get_color_format(lv_display_t * disp); + +/** + * Set the number of tiles for parallel rendering. + * @param disp pointer to a display + * @param tile_cnt number of tiles (1 =< tile_cnt < 256) + */ +void lv_display_set_tile_cnt(lv_display_t * disp, uint32_t tile_cnt); + +/** + * Get the number of tiles used for parallel rendering + * @param disp pointer to a display + * @return number of tiles + */ +uint32_t lv_display_get_tile_cnt(lv_display_t * disp); + +/** + * Enable anti-aliasing for the render engine + * @param disp pointer to a display + * @param en true/false + */ +void lv_display_set_antialiasing(lv_display_t * disp, bool en); + +/** + * Get if anti-aliasing is enabled for a display or not + * @param disp pointer to a display (NULL to use the default display) + * @return true/false + */ +bool lv_display_get_antialiasing(lv_display_t * disp); + +//! @cond Doxygen_Suppress + +/** + * Call from the display driver when the flushing is finished + * @param disp pointer to display whose `flush_cb` was called + */ +LV_ATTRIBUTE_FLUSH_READY void lv_display_flush_ready(lv_display_t * disp); + +/** + * Tell if it's the last area of the refreshing process. + * Can be called from `flush_cb` to execute some special display refreshing if needed when all areas area flushed. + * @param disp pointer to display + * @return true: it's the last area to flush; + * false: there are other areas too which will be refreshed soon + */ +LV_ATTRIBUTE_FLUSH_READY bool lv_display_flush_is_last(lv_display_t * disp); + +//! @endcond + +bool lv_display_is_double_buffered(lv_display_t * disp); + +/*--------------------- + * SCREENS + *--------------------*/ + +/** + * Return a pointer to the active screen on a display + * @param disp pointer to display which active screen should be get. + * (NULL to use the default screen) + * @return pointer to the active screen object (loaded by 'lv_screen_load()') + */ +lv_obj_t * lv_display_get_screen_active(lv_display_t * disp); + +/** + * Return with a pointer to the previous screen. Only used during screen transitions. + * @param disp pointer to display which previous screen should be get. + * (NULL to use the default screen) + * @return pointer to the previous screen object or NULL if not used now + */ +lv_obj_t * lv_display_get_screen_prev(lv_display_t * disp); + +/** + * Return the top layer. The top layer is the same on all screens and it is above the normal screen layer. + * @param disp pointer to display which top layer should be get. (NULL to use the default screen) + * @return pointer to the top layer object + */ +lv_obj_t * lv_display_get_layer_top(lv_display_t * disp); + +/** + * Return the sys. layer. The system layer is the same on all screen and it is above the normal screen and the top layer. + * @param disp pointer to display which sys. layer should be retrieved. (NULL to use the default screen) + * @return pointer to the sys layer object + */ +lv_obj_t * lv_display_get_layer_sys(lv_display_t * disp); + +/** + * Return the bottom layer. The bottom layer is the same on all screen and it is under the normal screen layer. + * It's visible only if the screen is transparent. + * @param disp pointer to display (NULL to use the default screen) + * @return pointer to the bottom layer object + */ +lv_obj_t * lv_display_get_layer_bottom(lv_display_t * disp); + +/** + * Load a screen on the default display + * @param scr pointer to a screen + */ +void lv_screen_load(struct _lv_obj_t * scr); + +/** + * Switch screen with animation + * @param scr pointer to the new screen to load + * @param anim_type type of the animation from `lv_screen_load_anim_t`, e.g. `LV_SCR_LOAD_ANIM_MOVE_LEFT` + * @param time time of the animation + * @param delay delay before the transition + * @param auto_del true: automatically delete the old screen + */ +void lv_screen_load_anim(lv_obj_t * scr, lv_screen_load_anim_t anim_type, uint32_t time, uint32_t delay, + bool auto_del); + +/** + * Get the active screen of the default display + * @return pointer to the active screen + */ +lv_obj_t * lv_screen_active(void); + +/** + * Get the top layer of the default display + * @return pointer to the top layer + */ +lv_obj_t * lv_layer_top(void); + +/** + * Get the system layer of the default display + * @return pointer to the sys layer + */ +lv_obj_t * lv_layer_sys(void); + +/** + * Get the bottom layer of the default display + * @return pointer to the bottom layer + */ +lv_obj_t * lv_layer_bottom(void); + +/*--------------------- + * OTHERS + *--------------------*/ + +/** + * Add an event handler to the display + * @param disp pointer to a display + * @param event_cb an event callback + * @param filter event code to react or `LV_EVENT_ALL` + * @param user_data optional user_data + */ +void lv_display_add_event_cb(lv_display_t * disp, lv_event_cb_t event_cb, lv_event_code_t filter, void * user_data); + +/** + * Get the number of event attached to a display + * @param disp pointer to a display + * @return number of events + */ +uint32_t lv_display_get_event_count(lv_display_t * disp); + +/** + * Get an event descriptor for an event + * @param disp pointer to a display + * @param index the index of the event + * @return the event descriptor + */ +lv_event_dsc_t * lv_display_get_event_dsc(lv_display_t * disp, uint32_t index); + +/** + * Remove an event + * @param disp pointer to a display + * @param index the index of the event to remove + * @return true: and event was removed; false: no event was removed + */ +bool lv_display_delete_event(lv_display_t * disp, uint32_t index); + +/** + * Remove an event_cb with user_data + * @param disp pointer to a display + * @param event_cb the event_cb of the event to remove + * @param user_data user_data + * @return the count of the event removed + */ +uint32_t lv_display_remove_event_cb_with_user_data(lv_display_t * disp, lv_event_cb_t event_cb, void * user_data); + +/** + * Send an event to a display + * @param disp pointer to a display + * @param code an event code. LV_EVENT_... + * @param param optional param + * @return LV_RESULT_OK: disp wasn't deleted in the event. + */ +lv_result_t lv_display_send_event(lv_display_t * disp, lv_event_code_t code, void * param); + +/** + * Get the area to be invalidated. Can be used in `LV_EVENT_INVALIDATE_AREA` + * @param e pointer to an event + * @return the area to invalidated (can be modified as required) + */ +lv_area_t * lv_event_get_invalidated_area(lv_event_t * e); + +/** + * Set the theme of a display. If there are no user created widgets yet the screens' theme will be updated + * @param disp pointer to a display + * @param th pointer to a theme + */ +void lv_display_set_theme(lv_display_t * disp, lv_theme_t * th); + +/** + * Get the theme of a display + * @param disp pointer to a display + * @return the display's theme (can be NULL) + */ +lv_theme_t * lv_display_get_theme(lv_display_t * disp); + +/** + * Get elapsed time since last user activity on a display (e.g. click) + * @param disp pointer to a display (NULL to get the overall smallest inactivity) + * @return elapsed ticks (milliseconds) since the last activity + */ +uint32_t lv_display_get_inactive_time(const lv_display_t * disp); + +/** + * Manually trigger an activity on a display + * @param disp pointer to a display (NULL to use the default display) + */ +void lv_display_trigger_activity(lv_display_t * disp); + +/** + * Temporarily enable and disable the invalidation of the display. + * @param disp pointer to a display (NULL to use the default display) + * @param en true: enable invalidation; false: invalidation + */ +void lv_display_enable_invalidation(lv_display_t * disp, bool en); + +/** + * Get display invalidation is enabled. + * @param disp pointer to a display (NULL to use the default display) + * @return return true if invalidation is enabled + */ +bool lv_display_is_invalidation_enabled(lv_display_t * disp); + +/** + * Get a pointer to the screen refresher timer to + * modify its parameters with `lv_timer_...` functions. + * @param disp pointer to a display + * @return pointer to the display refresher timer. (NULL on error) + */ +lv_timer_t * lv_display_get_refr_timer(lv_display_t * disp); + +/** + * Delete screen refresher timer + * @param disp pointer to a display + */ +void lv_display_delete_refr_timer(lv_display_t * disp); + +void lv_display_set_user_data(lv_display_t * disp, void * user_data); +void lv_display_set_driver_data(lv_display_t * disp, void * driver_data); +void * lv_display_get_user_data(lv_display_t * disp); +void * lv_display_get_driver_data(lv_display_t * disp); +lv_draw_buf_t * lv_display_get_buf_active(lv_display_t * disp); + +/** + * Rotate an area in-place according to the display's rotation + * @param disp pointer to a display + * @param area pointer to an area to rotate + */ +void lv_display_rotate_area(lv_display_t * disp, lv_area_t * area); + +/********************** + * MACROS + **********************/ + +/*------------------------------------------------ + * To improve backward compatibility + * Recommended only if you have one display + *------------------------------------------------*/ + +#ifndef LV_HOR_RES +/** + * The horizontal resolution of the currently active display. + */ +#define LV_HOR_RES lv_display_get_horizontal_resolution(lv_display_get_default()) +#endif + +#ifndef LV_VER_RES +/** + * The vertical resolution of the currently active display. + */ +#define LV_VER_RES lv_display_get_vertical_resolution(lv_display_get_default()) +#endif + +/** + * Same as Android's DIP. (Different name is chosen to avoid mistype between LV_DPI and LV_DIP) + * 1 dip is 1 px on a 160 DPI screen + * 1 dip is 2 px on a 320 DPI screen + * https://stackoverflow.com/questions/2025282/what-is-the-difference-between-px-dip-dp-and-sp + */ +#define LV_DPX_CALC(dpi, n) ((n) == 0 ? 0 :LV_MAX((( (dpi) * (n) + 80) / 160), 1)) /*+80 for rounding*/ +#define LV_DPX(n) LV_DPX_CALC(lv_display_get_dpi(NULL), n) + +/** + * Scale the given number of pixels (a distance or size) relative to a 160 DPI display + * considering the DPI of the default display. + * It ensures that e.g. `lv_dpx(100)` will have the same physical size regardless to the + * DPI of the display. + * @param n the number of pixels to scale + * @return `n x current_dpi/160` + */ +int32_t lv_dpx(int32_t n); + +/** + * Scale the given number of pixels (a distance or size) relative to a 160 DPI display + * considering the DPI of the given display. + * It ensures that e.g. `lv_dpx(100)` will have the same physical size regardless to the + * DPI of the display. + * @param disp a display whose dpi should be considered + * @param n the number of pixels to scale + * @return `n x current_dpi/160` + */ +int32_t lv_display_dpx(const lv_display_t * disp, int32_t n); + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DISPLAY_H*/ diff --git a/include/liblvgl/display/lv_display_private.h b/include/liblvgl/display/lv_display_private.h new file mode 100644 index 00000000..d831ec48 --- /dev/null +++ b/include/liblvgl/display/lv_display_private.h @@ -0,0 +1,183 @@ +/** + * @file lv_display_private.h + * + */ + +#ifndef LV_DISPLAY_PRIVATE_H +#define LV_DISPLAY_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../misc/lv_types.h" +#include "../core/lv_obj.h" +#include "../draw/lv_draw.h" +#include "lv_display.h" + +#if LV_USE_SYSMON +#include "../others/sysmon/lv_sysmon_private.h" +#endif + +/********************* + * DEFINES + *********************/ +#ifndef LV_INV_BUF_SIZE +#define LV_INV_BUF_SIZE 32 /**< Buffer size for invalid areas */ +#endif + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_display_t { + + /*--------------------- + * Resolution + *--------------------*/ + + /** Horizontal resolution.*/ + int32_t hor_res; + + /** Vertical resolution.*/ + int32_t ver_res; + + /** Horizontal resolution of the full / physical display. Set to -1 for fullscreen mode.*/ + int32_t physical_hor_res; + + /** Vertical resolution of the full / physical display. Set to -1 for fullscreen mode.*/ + int32_t physical_ver_res; + + /** Horizontal offset from the full / physical display. Set to 0 for fullscreen mode.*/ + int32_t offset_x; + + /** Vertical offset from the full / physical display. Set to 0 for fullscreen mode.*/ + int32_t offset_y; + + /** DPI (dot per inch) of the display. Default value is `LV_DPI_DEF`.*/ + uint32_t dpi; + + /*--------------------- + * Buffering + *--------------------*/ + lv_draw_buf_t * buf_1; + lv_draw_buf_t * buf_2; + + /** Internal, used by the library*/ + lv_draw_buf_t * buf_act; + + /** MANDATORY: Write the internal buffer (draw_buf) to the display. 'lv_display_flush_ready()' has to be + * called when finished*/ + lv_display_flush_cb_t flush_cb; + + /** + * Used to wait while flushing is ready. + * It can do any complex logic to wait, including semaphores, mutexes, polling flags, etc. + * If not set `flushing` flag is used which can be cleared with `lv_display_flush_ready()` */ + lv_display_flush_wait_cb_t flush_wait_cb; + + /** 1: flushing is in progress. (It can't be a bit field because when it's cleared from IRQ + * Read-Modify-Write issue might occur) */ + volatile int flushing; + + /** 1: It was the last chunk to flush. (It can't be a bit field because when it's cleared + * from IRQ Read-Modify-Write issue might occur) */ + volatile int flushing_last; + volatile uint32_t last_area : 1; /**< 1: last area is being rendered */ + volatile uint32_t last_part : 1; /**< 1: last part of the current area is being rendered */ + + lv_display_render_mode_t render_mode; + uint32_t antialiasing : 1; /**< 1: anti-aliasing is enabled on this display.*/ + uint32_t tile_cnt : 8; /**< Divide the display buffer into these number of tiles */ + + + /** 1: The current screen rendering is in progress*/ + uint32_t rendering_in_progress : 1; + + lv_color_format_t color_format; + + /** Invalidated (marked to redraw) areas*/ + lv_area_t inv_areas[LV_INV_BUF_SIZE]; + uint8_t inv_area_joined[LV_INV_BUF_SIZE]; + uint32_t inv_p; + int32_t inv_en_cnt; + + /** Double buffer sync areas (redrawn during last refresh) */ + lv_ll_t sync_areas; + + lv_draw_buf_t _static_buf1; /**< Used when user pass in a raw buffer as display draw buffer */ + lv_draw_buf_t _static_buf2; + /*--------------------- + * Layer + *--------------------*/ + lv_layer_t * layer_head; + void (*layer_init)(lv_display_t * disp, lv_layer_t * layer); + void (*layer_deinit)(lv_display_t * disp, lv_layer_t * layer); + + /*--------------------- + * Screens + *--------------------*/ + + /** Screens of the display*/ + lv_obj_t ** screens; /**< Array of screen objects.*/ + lv_obj_t * sys_layer; /**< @see lv_display_get_layer_sys*/ + lv_obj_t * top_layer; /**< @see lv_display_get_layer_top*/ + lv_obj_t * act_scr; /**< Currently active screen on this display*/ + lv_obj_t * bottom_layer;/**< @see lv_display_get_layer_bottom*/ + lv_obj_t * prev_scr; /**< Previous screen. Used during screen animations*/ + lv_obj_t * scr_to_load; /**< The screen prepared to load in lv_screen_load_anim*/ + uint32_t screen_cnt; + uint8_t draw_prev_over_act : 1;/** 1: Draw previous screen over active screen*/ + uint8_t del_prev : 1; /** 1: Automatically delete the previous screen when the screen load animation is ready*/ + + /*--------------------- + * Others + *--------------------*/ + + void * driver_data; /**< Custom user data*/ + + void * user_data; /**< Custom user data*/ + + lv_event_list_t event_list; + + uint32_t rotation : 3; /**< Element of lv_display_rotation_t*/ + + lv_theme_t * theme; /**< The theme assigned to the screen*/ + + /** A timer which periodically checks the dirty areas and refreshes them*/ + lv_timer_t * refr_timer; + + /*Miscellaneous data*/ + uint32_t last_activity_time; /**< Last time when there was activity on this display*/ + + /** The area being refreshed*/ + lv_area_t refreshed_area; + +#if LV_USE_PERF_MONITOR + lv_obj_t * perf_label; + lv_sysmon_backend_data_t perf_sysmon_backend; + lv_sysmon_perf_info_t perf_sysmon_info; +#endif + +#if LV_USE_MEM_MONITOR + lv_obj_t * mem_label; +#endif + +}; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DISPLAY_PRIVATE_H*/ diff --git a/include/liblvgl/draw/arm2d/lv_gpu_arm2d.h b/include/liblvgl/draw/arm2d/lv_gpu_arm2d.h deleted file mode 100644 index cc669901..00000000 --- a/include/liblvgl/draw/arm2d/lv_gpu_arm2d.h +++ /dev/null @@ -1,51 +0,0 @@ -/** - * @file lv_gpu_arm2d.h - * - */ - -#ifndef LV_GPU_ARM2D_H -#define LV_GPU_ARM2D_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/misc/lv_color.h" -#include "liblvgl/hal/lv_hal_disp.h" -#include "liblvgl/draw/sw/lv_draw_sw.h" - -#if LV_USE_GPU_ARM2D - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ -typedef lv_draw_sw_ctx_t lv_draw_arm2d_ctx_t; - -struct _lv_disp_drv_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -void lv_draw_arm2d_ctx_init(struct _lv_disp_drv_t * drv, lv_draw_ctx_t * draw_ctx); - -void lv_draw_arm2d_ctx_deinit(struct _lv_disp_drv_t * drv, lv_draw_ctx_t * draw_ctx); - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_GPU_ARM2D*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_GPU_ARM2D_H*/ diff --git a/include/liblvgl/draw/dma2d/lv_draw_dma2d.h b/include/liblvgl/draw/dma2d/lv_draw_dma2d.h new file mode 100644 index 00000000..af175f1f --- /dev/null +++ b/include/liblvgl/draw/dma2d/lv_draw_dma2d.h @@ -0,0 +1,49 @@ +/** + * @file lv_draw_dma2d.h + * + */ + +#ifndef LV_DRAW_DMA2D_H +#define LV_DRAW_DMA2D_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../lv_conf_internal.h" +#if LV_USE_DRAW_DMA2D + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void lv_draw_dma2d_init(void); +void lv_draw_dma2d_deinit(void); + +#if LV_USE_DRAW_DMA2D_INTERRUPT +void lv_draw_dma2d_transfer_complete_interrupt_handler(void); +#endif + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_DRAW_DMA2D*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_DMA2D_H*/ diff --git a/include/liblvgl/draw/dma2d/lv_draw_dma2d_private.h b/include/liblvgl/draw/dma2d/lv_draw_dma2d_private.h new file mode 100644 index 00000000..051d02c1 --- /dev/null +++ b/include/liblvgl/draw/dma2d/lv_draw_dma2d_private.h @@ -0,0 +1,155 @@ +/** + * @file lv_draw_dma2d_private.h + * + */ + +#ifndef LV_DRAW_DMA2D_PRIVATE_H +#define LV_DRAW_DMA2D_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_draw_dma2d.h" +#if LV_USE_DRAW_DMA2D + +#include "../lv_draw_private.h" +#include LV_DRAW_DMA2D_HAL_INCLUDE + +/********************* + * DEFINES + *********************/ + +#if LV_USE_DRAW_DMA2D_INTERRUPT && LV_USE_OS +#define LV_DRAW_DMA2D_ASYNC 1 +#else +#define LV_DRAW_DMA2D_ASYNC 0 +#endif + +#if defined(__CORTEX_M) && (__CORTEX_M == 7) +#define LV_DRAW_DMA2D_CACHE 1 +#else +#define LV_DRAW_DMA2D_CACHE 0 +#endif + +/********************** + * TYPEDEFS + **********************/ + +typedef enum { + LV_DRAW_DMA2D_OUTPUT_CF_ARGB8888 = 0, + LV_DRAW_DMA2D_OUTPUT_CF_RGB888, + LV_DRAW_DMA2D_OUTPUT_CF_RGB565, + LV_DRAW_DMA2D_OUTPUT_CF_ARGB1555, + LV_DRAW_DMA2D_OUTPUT_CF_ARGB4444 +} lv_draw_dma2d_output_cf_t; + +typedef enum { + LV_DRAW_DMA2D_FGBG_CF_ARGB8888 = 0, + LV_DRAW_DMA2D_FGBG_CF_RGB888, + LV_DRAW_DMA2D_FGBG_CF_RGB565, + LV_DRAW_DMA2D_FGBG_CF_ARGB1555, + LV_DRAW_DMA2D_FGBG_CF_ARGB4444, + LV_DRAW_DMA2D_FGBG_CF_L8, + LV_DRAW_DMA2D_FGBG_CF_AL44, + LV_DRAW_DMA2D_FGBG_CF_AL88, + LV_DRAW_DMA2D_FGBG_CF_L4, + LV_DRAW_DMA2D_FGBG_CF_A8, + LV_DRAW_DMA2D_FGBG_CF_A4, + LV_DRAW_DMA2D_FGBG_CF_YCBCR +} lv_draw_dma2d_fgbg_cf_t; + +typedef enum { + LV_DRAW_DMA2D_MODE_MEMORY_TO_MEMORY = 0, + LV_DRAW_DMA2D_MODE_MEMORY_TO_MEMORY_WITH_PFC, + LV_DRAW_DMA2D_MODE_MEMORY_TO_MEMORY_WITH_BLENDING, + LV_DRAW_DMA2D_MODE_REGISTER_TO_MEMORY, + LV_DRAW_DMA2D_MODE_MEMORY_TO_MEMORY_WITH_BLENDING_AND_FIXED_COLOR_FG, + LV_DRAW_DMA2D_MODE_MEMORY_TO_MEMORY_WITH_BLENDING_AND_FIXED_COLOR_BG +} lv_draw_dma2d_mode_t; + +typedef enum { + LV_DRAW_DMA2D_ALPHA_MODE_NO_MODIFY_IMAGE_ALPHA_CHANNEL = 0, + LV_DRAW_DMA2D_ALPHA_MODE_REPLACE_ALPHA_CHANNEL, + LV_DRAW_DMA2D_ALPHA_MODE_MULTIPLY_IMAGE_ALPHA_CHANNEL +} lv_draw_dma2d_alpha_mode_t; + +typedef struct { + lv_draw_dma2d_mode_t mode; + uint32_t w; + uint32_t h; + + void * output_address; + uint32_t output_offset; + lv_draw_dma2d_output_cf_t output_cf; + + uint32_t reg_to_mem_mode_color; + + const void * fg_address; + uint32_t fg_offset; + lv_draw_dma2d_fgbg_cf_t fg_cf; + uint32_t fg_color; + uint32_t fg_alpha_mode; + uint32_t fg_alpha; + + const void * bg_address; + uint32_t bg_offset; + lv_draw_dma2d_fgbg_cf_t bg_cf; + uint32_t bg_color; + uint32_t bg_alpha_mode; + uint32_t bg_alpha; + +} lv_draw_dma2d_configuration_t; + +typedef struct { + const void * first_byte; + uint32_t width_bytes; + uint32_t height; + uint32_t stride; +} lv_draw_dma2d_cache_area_t; + +typedef struct { + lv_draw_unit_t base_unit; + lv_draw_task_t * volatile task_act; +#if LV_DRAW_DMA2D_CACHE + lv_draw_dma2d_cache_area_t writing_area; +#endif +#if LV_DRAW_DMA2D_ASYNC + lv_thread_t thread; + lv_thread_sync_t interrupt_signal; +#endif +} lv_draw_dma2d_unit_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void lv_draw_dma2d_opaque_fill(lv_draw_dma2d_unit_t * u, void * first_pixel, int32_t w, int32_t h, int32_t stride); +void lv_draw_dma2d_fill(lv_draw_dma2d_unit_t * u, void * first_pixel, int32_t w, int32_t h, int32_t stride); +void lv_draw_dma2d_opaque_image(lv_draw_dma2d_unit_t * u, void * dest_first_pixel, lv_area_t * clipped_coords, + int32_t dest_stride); +void lv_draw_dma2d_image(lv_draw_dma2d_unit_t * u, void * dest_first_pixel, lv_area_t * clipped_coords, + int32_t dest_stride); +lv_draw_dma2d_output_cf_t lv_draw_dma2d_cf_to_dma2d_output_cf(lv_color_format_t cf); +uint32_t lv_draw_dma2d_color_to_dma2d_ocolr(lv_draw_dma2d_output_cf_t cf, lv_color_t color); +void lv_draw_dma2d_configure_and_start_transfer(const lv_draw_dma2d_configuration_t * conf); +#if LV_DRAW_DMA2D_CACHE +void lv_draw_dma2d_invalidate_cache(const lv_draw_dma2d_cache_area_t * mem_area); +void lv_draw_dma2d_clean_cache(const lv_draw_dma2d_cache_area_t * mem_area); +#endif + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_DRAW_DMA2D*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_DMA2D_PRIVATE_H*/ diff --git a/include/liblvgl/draw/lv_draw.h b/include/liblvgl/draw/lv_draw.h index c8aed722..6f69f69d 100644 --- a/include/liblvgl/draw/lv_draw.h +++ b/include/liblvgl/draw/lv_draw.h @@ -3,6 +3,10 @@ * */ +/** + * Modified by NXP in 2024 + */ + #ifndef LV_DRAW_H #define LV_DRAW_H @@ -13,191 +17,256 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" - -#include "liblvgl/misc/lv_style.h" -#include "liblvgl/misc/lv_txt.h" -#include "lv_img_decoder.h" -#include "lv_img_cache.h" - -#include "lv_draw_rect.h" -#include "lv_draw_label.h" -#include "lv_draw_img.h" -#include "lv_draw_line.h" -#include "lv_draw_triangle.h" -#include "lv_draw_arc.h" -#include "lv_draw_mask.h" -#include "lv_draw_transform.h" -#include "lv_draw_layer.h" +#include "../lv_conf_internal.h" + +#include "../misc/lv_types.h" +#include "../misc/lv_style.h" +#include "../misc/lv_text.h" +#include "../misc/lv_profiler.h" +#include "../misc/lv_matrix.h" +#include "lv_image_decoder.h" +#include "../osal/lv_os.h" +#include "lv_draw_buf.h" /********************* * DEFINES *********************/ +#define LV_DRAW_UNIT_NONE 0 +#define LV_DRAW_UNIT_IDLE -1 /**< The draw unit is idle, new dispatching might be requested to try again */ + +#if LV_DRAW_TRANSFORM_USE_MATRIX +#if !LV_USE_MATRIX +#error "LV_DRAW_TRANSFORM_USE_MATRIX requires LV_USE_MATRIX = 1" +#endif +#endif /********************** * TYPEDEFS **********************/ -typedef struct { - void * user_data; -} lv_draw_mask_t; - -typedef struct _lv_draw_layer_ctx_t { - lv_area_t area_full; - lv_area_t area_act; - lv_coord_t max_row_with_alpha; - lv_coord_t max_row_with_no_alpha; - void * buf; - struct { - const lv_area_t * clip_area; - lv_area_t * buf_area; - void * buf; - bool screen_transp; - } original; -} lv_draw_layer_ctx_t; - -typedef struct _lv_draw_ctx_t { - /** - * Pointer to a buffer to draw into - */ - void * buf; +typedef enum { + LV_DRAW_TASK_TYPE_NONE = 0, + LV_DRAW_TASK_TYPE_FILL, + LV_DRAW_TASK_TYPE_BORDER, + LV_DRAW_TASK_TYPE_BOX_SHADOW, + LV_DRAW_TASK_TYPE_LABEL, + LV_DRAW_TASK_TYPE_IMAGE, + LV_DRAW_TASK_TYPE_LAYER, + LV_DRAW_TASK_TYPE_LINE, + LV_DRAW_TASK_TYPE_ARC, + LV_DRAW_TASK_TYPE_TRIANGLE, + LV_DRAW_TASK_TYPE_MASK_RECTANGLE, + LV_DRAW_TASK_TYPE_MASK_BITMAP, + LV_DRAW_TASK_TYPE_VECTOR, +} lv_draw_task_type_t; + +typedef enum { + LV_DRAW_TASK_STATE_WAITING, /*Waiting for something to be finished. E.g. rendering a layer*/ + LV_DRAW_TASK_STATE_QUEUED, + LV_DRAW_TASK_STATE_IN_PROGRESS, + LV_DRAW_TASK_STATE_READY, +} lv_draw_task_state_t; + +struct _lv_layer_t { + + /** Target draw buffer of the layer*/ + lv_draw_buf_t * draw_buf; + + /** The absolute coordinates of the buffer */ + lv_area_t buf_area; + + /** The color format of the layer. LV_COLOR_FORMAT_... */ + lv_color_format_t color_format; /** - * The position and size of `buf` (absolute coordinates) + * NEVER USE IT DRAW UNITS. USED INTERNALLY DURING DRAW TASK CREATION. + * The current clip area with absolute coordinates, always the same or smaller than `buf_area` + * Can be set before new draw tasks are added to indicate the clip area of the draw tasks. + * Therefore `lv_draw_add_task()` always saves it in the new draw task to know the clip area when the draw task was added. + * During drawing the draw units also sees the saved clip_area and should use it during drawing. + * During drawing the layer's clip area shouldn't be used as it might be already changed for other draw tasks. */ - lv_area_t * buf_area; + lv_area_t _clip_area; /** - * The current clip area with absolute coordinates, always the same or smaller than `buf_area` + * The physical clipping area relative to the display. */ - const lv_area_t * clip_area; - + lv_area_t phy_clip_area; - void (*draw_rect)(struct _lv_draw_ctx_t * draw_ctx, const lv_draw_rect_dsc_t * dsc, const lv_area_t * coords); +#if LV_DRAW_TRANSFORM_USE_MATRIX + /** Transform matrix to be applied when rendering the layer */ + lv_matrix_t matrix; +#endif - void (*draw_arc)(struct _lv_draw_ctx_t * draw_ctx, const lv_draw_arc_dsc_t * dsc, const lv_point_t * center, - uint16_t radius, uint16_t start_angle, uint16_t end_angle); + /** Linked list of draw tasks */ + lv_draw_task_t * draw_task_head; - void (*draw_img_decoded)(struct _lv_draw_ctx_t * draw_ctx, const lv_draw_img_dsc_t * dsc, - const lv_area_t * coords, const uint8_t * map_p, lv_img_cf_t color_format); + lv_layer_t * parent; + lv_layer_t * next; + bool all_tasks_added; + void * user_data; +}; - lv_res_t (*draw_img)(struct _lv_draw_ctx_t * draw_ctx, const lv_draw_img_dsc_t * draw_dsc, - const lv_area_t * coords, const void * src); +typedef struct { + lv_obj_t * obj; + lv_part_t part; + uint32_t id1; + uint32_t id2; + lv_layer_t * layer; + size_t dsc_size; + void * user_data; +} lv_draw_dsc_base_t; - void (*draw_letter)(struct _lv_draw_ctx_t * draw_ctx, const lv_draw_label_dsc_t * dsc, const lv_point_t * pos_p, - uint32_t letter); +/********************** + * GLOBAL PROTOTYPES + **********************/ +/** + * Used internally to initialize the drawing module + */ +void lv_draw_init(void); - void (*draw_line)(struct _lv_draw_ctx_t * draw_ctx, const lv_draw_line_dsc_t * dsc, const lv_point_t * point1, - const lv_point_t * point2); +/** + * Deinitialize the drawing module + */ +void lv_draw_deinit(void); +/** + * Allocate a new draw unit with the given size and appends it to the list of draw units + * @param size the size to allocate. E.g. `sizeof(my_draw_unit_t)`, + * where the first element of `my_draw_unit_t` is `lv_draw_unit_t`. + */ +void * lv_draw_create_unit(size_t size); - void (*draw_polygon)(struct _lv_draw_ctx_t * draw_ctx, const lv_draw_rect_dsc_t * draw_dsc, - const lv_point_t * points, uint16_t point_cnt); +/** + * Add an empty draw task to the draw task list of a layer. + * @param layer pointer to a layer + * @param coords the coordinates of the draw task + * @return the created draw task which needs to be + * further configured e.g. by added a draw descriptor + */ +lv_draw_task_t * lv_draw_add_task(lv_layer_t * layer, const lv_area_t * coords); +/** + * Needs to be called when a draw task is created and configured. + * It will send an event about the new draw task to the widget + * and assign it to a draw unit. + * @param layer pointer to a layer + * @param t pointer to a draw task + */ +void lv_draw_finalize_task_creation(lv_layer_t * layer, lv_draw_task_t * t); - /** - * Get an area of a transformed image (zoomed and/or rotated) - * @param draw_ctx pointer to a draw context - * @param dest_area get this area of the result image. It assumes that the original image is placed to the 0;0 position. - * @param src_buf the source image - * @param src_w width of the source image in [px] - * @param src_h height of the source image in [px] - * @param src_stride the stride in [px]. - * @param draw_dsc an `lv_draw_img_dsc_t` descriptor containing the transformation parameters - * @param cf the color format of `src_buf` - * @param cbuf place the colors of the pixels on `dest_area` here in RGB format - * @param abuf place the opacity of the pixels on `dest_area` here - */ - void (*draw_transform)(struct _lv_draw_ctx_t * draw_ctx, const lv_area_t * dest_area, const void * src_buf, - lv_coord_t src_w, lv_coord_t src_h, lv_coord_t src_stride, - const lv_draw_img_dsc_t * draw_dsc, lv_img_cf_t cf, lv_color_t * cbuf, lv_opa_t * abuf); +/** + * Try dispatching draw tasks to draw units + */ +void lv_draw_dispatch(void); - /** - * Replace the buffer with a rect without decoration like radius or borders - */ - void (*draw_bg)(struct _lv_draw_ctx_t * draw_ctx, const lv_draw_rect_dsc_t * draw_dsc, const lv_area_t * coords); +/** + * Used internally to try dispatching draw tasks of a specific layer + * @param disp pointer to a display on which the dispatching was requested + * @param layer pointer to a layer + * @return at least one draw task is being rendered (maybe it was taken earlier) + */ +bool lv_draw_dispatch_layer(lv_display_t * disp, lv_layer_t * layer); - /** - * Wait until all background operations are finished. (E.g. GPU operations) - */ - void (*wait_for_finish)(struct _lv_draw_ctx_t * draw_ctx); +/** + * Wait for a new dispatch request. + * It's blocking if `LV_USE_OS == 0` else it yields + */ +void lv_draw_dispatch_wait_for_request(void); - /** - * Copy an area from buffer to an other - * @param draw_ctx pointer to a draw context - * @param dest_buf copy the buffer into this buffer - * @param dest_stride the width of the dest_buf in pixels - * @param dest_area the destination area - * @param src_buf copy from this buffer - * @param src_stride the width of src_buf in pixels - * @param src_area the source area. - * - * @note dest_area and src_area must have the same width and height - * but can have different x and y position. - * @note dest_area and src_area must be clipped to the real dimensions of the buffers - */ - void (*buffer_copy)(struct _lv_draw_ctx_t * draw_ctx, void * dest_buf, lv_coord_t dest_stride, - const lv_area_t * dest_area, - void * src_buf, lv_coord_t src_stride, const lv_area_t * src_area); +/** + * Wait for draw finish in case of asynchronous task execution. + * If `LV_USE_OS == 0` it just return. + */ +void lv_draw_wait_for_finish(void); - /** - * Initialize a new layer context. - * The original buffer and area data are already saved from `draw_ctx` to `layer_ctx` - * @param draw_ctx pointer to the current draw context - * @param layer_area the coordinates of the layer - * @param flags OR-ed flags from @lv_draw_layer_flags_t - * @return pointer to the layer context, or NULL on error - */ - struct _lv_draw_layer_ctx_t * (*layer_init)(struct _lv_draw_ctx_t * draw_ctx, struct _lv_draw_layer_ctx_t * layer_ctx, - lv_draw_layer_flags_t flags); +/** + * When a draw unit finished a draw task it needs to request dispatching + * to let LVGL assign a new draw task to it + */ +void lv_draw_dispatch_request(void); - /** - * Adjust the layer_ctx and/or draw_ctx based on the `layer_ctx->area_act`. - * It's called only if flags has `LV_DRAW_LAYER_FLAG_CAN_SUBDIVIDE` - * @param draw_ctx pointer to the current draw context - * @param layer_ctx pointer to a layer context - * @param flags OR-ed flags from @lv_draw_layer_flags_t - */ - void (*layer_adjust)(struct _lv_draw_ctx_t * draw_ctx, struct _lv_draw_layer_ctx_t * layer_ctx, - lv_draw_layer_flags_t flags); +/** + * Get the total number of draw units. + */ +uint32_t lv_draw_get_unit_count(void); - /** - * Blend a rendered layer to `layer_ctx->area_act` - * @param draw_ctx pointer to the current draw context - * @param layer_ctx pointer to a layer context - * @param draw_dsc pointer to an image draw descriptor - */ - void (*layer_blend)(struct _lv_draw_ctx_t * draw_ctx, struct _lv_draw_layer_ctx_t * layer_ctx, - const lv_draw_img_dsc_t * draw_dsc); +/** + * Find and available draw task + * @param layer the draw ctx to search in + * @param t_prev continue searching from this task + * @param draw_unit_id check the task where `preferred_draw_unit_id` equals this value or `LV_DRAW_UNIT_NONE` + * @return tan available draw task or NULL if there is no any + */ +lv_draw_task_t * lv_draw_get_next_available_task(lv_layer_t * layer, lv_draw_task_t * t_prev, uint8_t draw_unit_id); - /** - * Destroy a layer context. The original buffer and area data of the `draw_ctx` will be restored - * and the `layer_ctx` itself will be freed automatically. - * @param draw_ctx pointer to the current draw context - * @param layer_ctx pointer to a layer context - */ - void (*layer_destroy)(struct _lv_draw_ctx_t * draw_ctx, lv_draw_layer_ctx_t * layer_ctx); +/** + * Tell how many draw task are waiting to be drawn on the area of `t_check`. + * It can be used to determine if a GPU shall combine many draw tasks into one or not. + * If a lot of tasks are waiting for the current ones it makes sense to draw them one-by-one + * to not block the dependent tasks' rendering + * @param t_check the task whose dependent tasks shall be counted + * @return number of tasks depending on `t_check` + */ +uint32_t lv_draw_get_dependent_count(lv_draw_task_t * t_check); - /** - * Size of a layer context in bytes. - */ - size_t layer_instance_size; +/** + * Create (allocate) a new layer on a parent layer + * @param parent_layer the parent layer to which the layer will be merged when it's rendered + * @param color_format the color format of the layer + * @param area the areas of the layer (absolute coordinates) + * @return the new target_layer or NULL on error + */ +lv_layer_t * lv_draw_layer_create(lv_layer_t * parent_layer, lv_color_format_t color_format, const lv_area_t * area); -#if LV_USE_USER_DATA - void * user_data; -#endif +/** + * Initialize a layer which is allocated by the user + * @param layer pointer the layer to initialize (its lifetime needs to be managed by the user) + * @param parent_layer the parent layer to which the layer will be merged when it's rendered + * @param color_format the color format of the layer + * @param area the areas of the layer (absolute coordinates) + * @return the new target_layer or NULL on error + */ +void lv_draw_layer_init(lv_layer_t * layer, lv_layer_t * parent_layer, lv_color_format_t color_format, + const lv_area_t * area); -} lv_draw_ctx_t; +/** + * Try to allocate a buffer for the layer. + * @param layer pointer to a layer + * @return pointer to the allocated aligned buffer or NULL on failure + */ +void * lv_draw_layer_alloc_buf(lv_layer_t * layer); -/********************** - * GLOBAL PROTOTYPES - **********************/ +/** + * Got to a pixel at X and Y coordinate on a layer + * @param layer pointer to a layer + * @param x the target X coordinate + * @param y the target X coordinate + * @return `buf` offset to point to the given X and Y coordinate + */ +void * lv_draw_layer_go_to_xy(lv_layer_t * layer, int32_t x, int32_t y); -void lv_draw_init(void); +/** + * Get the type of a draw task + * @param t the draw task to get the type of + * @return the draw task type +*/ +lv_draw_task_type_t lv_draw_task_get_type(const lv_draw_task_t * t); +/** + * Get the draw descriptor of a draw task + * @param t the draw task to get the draw descriptor of + * @return a void pointer to the draw descriptor +*/ +void * lv_draw_task_get_draw_dsc(const lv_draw_task_t * t); -void lv_draw_wait_for_finish(lv_draw_ctx_t * draw_ctx); +/** + * Get the draw area of a draw task + * @param t the draw task to get the draw area of + * @param area the destination where the draw area will be stored +*/ +void lv_draw_task_get_area(const lv_draw_task_t * t, lv_area_t * area); /********************** * GLOBAL VARIABLES @@ -207,10 +276,6 @@ void lv_draw_wait_for_finish(lv_draw_ctx_t * draw_ctx); * MACROS **********************/ -/********************** - * POST INCLUDES - *********************/ - #ifdef __cplusplus } /*extern "C"*/ #endif diff --git a/include/liblvgl/draw/lv_draw_arc.h b/include/liblvgl/draw/lv_draw_arc.h index 8633af5f..726bd68c 100644 --- a/include/liblvgl/draw/lv_draw_arc.h +++ b/include/liblvgl/draw/lv_draw_arc.h @@ -13,10 +13,10 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" -#include "liblvgl/misc/lv_color.h" -#include "liblvgl/misc/lv_area.h" -#include "liblvgl/misc/lv_style.h" +#include "../lv_conf_internal.h" +#include "../misc/lv_color.h" +#include "../misc/lv_area.h" +#include "../misc/lv_style.h" /********************* * DEFINES @@ -25,38 +25,44 @@ extern "C" { /********************** * TYPEDEFS **********************/ + typedef struct { + lv_draw_dsc_base_t base; + lv_color_t color; - lv_coord_t width; - uint16_t start_angle; - uint16_t end_angle; + int32_t width; + lv_value_precise_t start_angle; + lv_value_precise_t end_angle; + lv_point_t center; + uint16_t radius; const void * img_src; lv_opa_t opa; - lv_blend_mode_t blend_mode : 2; uint8_t rounded : 1; } lv_draw_arc_dsc_t; -struct _lv_draw_ctx_t; - /********************** * GLOBAL PROTOTYPES **********************/ +/** + * Initialize an arc draw descriptor. + * @param dsc pointer to a draw descriptor + */ void lv_draw_arc_dsc_init(lv_draw_arc_dsc_t * dsc); /** - * Draw an arc. (Can draw pie too with great thickness.) - * @param center_x the x coordinate of the center of the arc - * @param center_y the y coordinate of the center of the arc - * @param radius the radius of the arc - * @param mask the arc will be drawn only in this mask - * @param start_angle the start angle of the arc (0 deg on the bottom, 90 deg on the right) - * @param end_angle the end angle of the arc - * @param clip_area the arc will be drawn only in this area - * @param dsc pointer to an initialized `lv_draw_line_dsc_t` variable + * Try to get an arc draw descriptor from a draw task. + * @param task draw task + * @return the task's draw descriptor or NULL if the task is not of type LV_DRAW_TASK_TYPE_ARC + */ +lv_draw_arc_dsc_t * lv_draw_task_get_arc_dsc(lv_draw_task_t * task); + +/** + * Create an arc draw task. + * @param layer pointer to a layer + * @param dsc pointer to an initialized draw descriptor variable */ -void lv_draw_arc(struct _lv_draw_ctx_t * draw_ctx, const lv_draw_arc_dsc_t * dsc, const lv_point_t * center, - uint16_t radius, uint16_t start_angle, uint16_t end_angle); +void lv_draw_arc(lv_layer_t * layer, const lv_draw_arc_dsc_t * dsc); /** * Get an area the should be invalidated when the arcs angle changed between start_angle and end_ange @@ -69,8 +75,9 @@ void lv_draw_arc(struct _lv_draw_ctx_t * draw_ctx, const lv_draw_arc_dsc_t * dsc * @param rounded true: the arc is rounded * @param area store the area to invalidate here */ -void lv_draw_arc_get_area(lv_coord_t x, lv_coord_t y, uint16_t radius, uint16_t start_angle, uint16_t end_angle, - lv_coord_t w, bool rounded, lv_area_t * area); +void lv_draw_arc_get_area(int32_t x, int32_t y, uint16_t radius, lv_value_precise_t start_angle, + lv_value_precise_t end_angle, + int32_t w, bool rounded, lv_area_t * area); /********************** * MACROS diff --git a/include/liblvgl/draw/lv_draw_buf.h b/include/liblvgl/draw/lv_draw_buf.h new file mode 100644 index 00000000..0a608696 --- /dev/null +++ b/include/liblvgl/draw/lv_draw_buf.h @@ -0,0 +1,356 @@ +/** + * @file lv_draw_buf.h + * + */ + +#ifndef LV_DRAW_BUF_H +#define LV_DRAW_BUF_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../misc/lv_types.h" +#include "../misc/lv_area.h" +#include "../misc/lv_color.h" +#include "../stdlib/lv_string.h" +#include "lv_image_dsc.h" + +/********************* + * DEFINES + *********************/ + +/** Use this value to let LVGL calculate stride automatically */ +#define LV_STRIDE_AUTO 0 +LV_EXPORT_CONST_INT(LV_STRIDE_AUTO); + +/** + * Stride alignment for draw buffers. + * It may vary between different color formats and hardware. + * Refine it to suit your needs. + */ + +#define LV_DRAW_BUF_STRIDE(w, cf) \ + LV_ROUND_UP(((w) * LV_COLOR_FORMAT_GET_BPP(cf) + 7) / 8, LV_DRAW_BUF_STRIDE_ALIGN) + +/** Allocate a slightly larger buffer, so we can adjust the start address to meet alignment */ +#define LV_DRAW_BUF_SIZE(w, h, cf) \ + (LV_DRAW_BUF_STRIDE(w, cf) * (h) + LV_DRAW_BUF_ALIGN + \ + LV_COLOR_INDEXED_PALETTE_SIZE(cf) * sizeof(lv_color32_t)) + +/** + * Define a static draw buffer with the given width, height, and color format. + * Stride alignment is set to LV_DRAW_BUF_STRIDE_ALIGN. + * + * For platform that needs special buffer alignment, call LV_DRAW_BUF_INIT_STATIC. + */ +#define LV_DRAW_BUF_DEFINE_STATIC(name, _w, _h, _cf) \ + static uint8_t buf_##name[LV_DRAW_BUF_SIZE(_w, _h, _cf)]; \ + static lv_draw_buf_t name = { \ + .header = { \ + .magic = LV_IMAGE_HEADER_MAGIC, \ + .cf = (_cf), \ + .flags = LV_IMAGE_FLAGS_MODIFIABLE, \ + .w = (_w), \ + .h = (_h), \ + .stride = LV_DRAW_BUF_STRIDE(_w, _cf), \ + .reserved_2 = 0, \ + }, \ + .data_size = sizeof(buf_##name), \ + .data = buf_##name, \ + .unaligned_data = buf_##name, \ + } + +#define LV_DRAW_BUF_INIT_STATIC(name) \ + do { \ + lv_image_header_t * header = &name.header; \ + lv_draw_buf_init(&name, header->w, header->h, (lv_color_format_t)header->cf, header->stride, buf_##name, sizeof(buf_##name)); \ + lv_draw_buf_set_flag(&name, LV_IMAGE_FLAGS_MODIFIABLE); \ + } while(0) + +/********************** + * TYPEDEFS + **********************/ + +typedef void * (*lv_draw_buf_malloc_cb)(size_t size, lv_color_format_t color_format); + +typedef void (*lv_draw_buf_free_cb)(void * draw_buf); + +typedef void * (*lv_draw_buf_align_cb)(void * buf, lv_color_format_t color_format); + +typedef void (*lv_draw_buf_cache_operation_cb)(const lv_draw_buf_t * draw_buf, const lv_area_t * area); + +typedef uint32_t (*lv_draw_buf_width_to_stride_cb)(uint32_t w, lv_color_format_t color_format); + +struct _lv_draw_buf_t { + lv_image_header_t header; + uint32_t data_size; /**< Total buf size in bytes */ + uint8_t * data; + void * unaligned_data; /**< Unaligned address of `data`, used internally by lvgl */ + const lv_draw_buf_handlers_t * handlers; /**< draw buffer alloc/free ops. */ +}; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Initialize the draw buffer with the default handlers. + * + * @param handlers the draw buffer handlers to set + */ +void lv_draw_buf_init_with_default_handlers(lv_draw_buf_handlers_t * handlers); + +/** + * Initialize the draw buffer with given handlers. + * + * @param handlers the draw buffer handlers to set + * @param buf_malloc_cb the callback to allocate memory for the buffer + * @param buf_free_cb the callback to free memory of the buffer + * @param align_pointer_cb the callback to align the buffer + * @param invalidate_cache_cb the callback to invalidate the cache of the buffer + * @param flush_cache_cb the callback to flush buffer + * @param width_to_stride_cb the callback to calculate the stride based on the width and color format + */ +void lv_draw_buf_handlers_init(lv_draw_buf_handlers_t * handlers, + lv_draw_buf_malloc_cb buf_malloc_cb, + lv_draw_buf_free_cb buf_free_cb, + lv_draw_buf_align_cb align_pointer_cb, + lv_draw_buf_cache_operation_cb invalidate_cache_cb, + lv_draw_buf_cache_operation_cb flush_cache_cb, + lv_draw_buf_width_to_stride_cb width_to_stride_cb); + +/** + * Get the struct which holds the callbacks for draw buf management. + * Custom callback can be set on the returned value + * @return pointer to the struct of handlers + */ +lv_draw_buf_handlers_t * lv_draw_buf_get_handlers(void); +lv_draw_buf_handlers_t * lv_draw_buf_get_font_handlers(void); +lv_draw_buf_handlers_t * lv_draw_buf_get_image_handlers(void); + + +/** + * Align the address of a buffer. The buffer needs to be large enough for the real data after alignment + * @param buf the data to align + * @param color_format the color format of the buffer + * @return the aligned buffer + */ +void * lv_draw_buf_align(void * buf, lv_color_format_t color_format); + +/** + * Align the address of a buffer with custom draw buffer handlers. + * The buffer needs to be large enough for the real data after alignment + * @param handlers the draw buffer handlers + * @param buf the data to align + * @param color_format the color format of the buffer + * @return the aligned buffer + */ +void * lv_draw_buf_align_ex(const lv_draw_buf_handlers_t * handlers, void * buf, lv_color_format_t color_format); + +/** + * Invalidate the cache of the buffer + * @param draw_buf the draw buffer needs to be invalidated + * @param area the area to invalidate in the buffer, + * use NULL to invalidate the whole draw buffer address range + */ +void lv_draw_buf_invalidate_cache(const lv_draw_buf_t * draw_buf, const lv_area_t * area); + +/** + * Flush the cache of the buffer + * @param draw_buf the draw buffer needs to be flushed + * @param area the area to flush in the buffer, + * use NULL to flush the whole draw buffer address range + */ +void lv_draw_buf_flush_cache(const lv_draw_buf_t * draw_buf, const lv_area_t * area); + +/** + * Calculate the stride in bytes based on a width and color format + * @param w the width in pixels + * @param color_format the color format + * @return the stride in bytes + */ +uint32_t lv_draw_buf_width_to_stride(uint32_t w, lv_color_format_t color_format); + +/** + * Calculate the stride in bytes based on a width and color format + * @param handlers the draw buffer handlers + * @param w the width in pixels + * @param color_format the color format + * @return the stride in bytes + */ +uint32_t lv_draw_buf_width_to_stride_ex(const lv_draw_buf_handlers_t * handlers, uint32_t w, + lv_color_format_t color_format); + +/** + * Clear an area on the buffer + * @param draw_buf pointer to draw buffer + * @param a the area to clear, or NULL to clear the whole buffer + */ +void lv_draw_buf_clear(lv_draw_buf_t * draw_buf, const lv_area_t * a); + +/** + * Copy an area from a buffer to another + * @param dest pointer to the destination draw buffer + * @param dest_area the area to copy from the destination buffer, if NULL, use the whole buffer + * @param src pointer to the source draw buffer + * @param src_area the area to copy from the destination buffer, if NULL, use the whole buffer + * @note `dest_area` and `src_area` should have the same width and height + * @note `dest` and `src` should have same color format. Color converting is not supported fow now. + */ +void lv_draw_buf_copy(lv_draw_buf_t * dest, const lv_area_t * dest_area, + const lv_draw_buf_t * src, const lv_area_t * src_area); + +/** + * Note: Eventually, lv_draw_buf_malloc/free will be kept as private. + * For now, we use `create` to distinguish with malloc. + * + * Create an draw buf by allocating struct for `lv_draw_buf_t` and allocating a buffer for it + * that meets specified requirements. + * + * @param w the buffer width in pixels + * @param h the buffer height in pixels + * @param cf the color format for image + * @param stride the stride in bytes for image. Use 0 for automatic calculation based on + * w, cf, and global stride alignment configuration. + */ +lv_draw_buf_t * lv_draw_buf_create(uint32_t w, uint32_t h, lv_color_format_t cf, uint32_t stride); + +/** + * Note: Eventually, lv_draw_buf_malloc/free will be kept as private. + * For now, we use `create` to distinguish with malloc. + * + * Create an draw buf by allocating struct for `lv_draw_buf_t` and allocating a buffer for it + * that meets specified requirements. + * + * @param handlers the draw buffer handlers + * @param w the buffer width in pixels + * @param h the buffer height in pixels + * @param cf the color format for image + * @param stride the stride in bytes for image. Use 0 for automatic calculation based on + * w, cf, and global stride alignment configuration. + */ +lv_draw_buf_t * lv_draw_buf_create_ex(const lv_draw_buf_handlers_t * handlers, uint32_t w, uint32_t h, + lv_color_format_t cf, uint32_t stride); + +/** + * Duplicate a draw buf with same image size, stride and color format. Copy the image data too. + * @param draw_buf the draw buf to duplicate + * @return the duplicated draw buf on success, NULL if failed + */ +lv_draw_buf_t * lv_draw_buf_dup(const lv_draw_buf_t * draw_buf); + +/** + * Duplicate a draw buf with same image size, stride and color format. Copy the image data too. + * @param handlers the draw buffer handlers + * @param draw_buf the draw buf to duplicate + * @return the duplicated draw buf on success, NULL if failed + */ +lv_draw_buf_t * lv_draw_buf_dup_ex(const lv_draw_buf_handlers_t * handlers, const lv_draw_buf_t * draw_buf); + +/** + * Initialize a draw buf with the given buffer and parameters. Clear draw buffer flag to zero. + * @param draw_buf the draw buf to initialize + * @param w the buffer width in pixels + * @param h the buffer height in pixels + * @param cf the color format + * @param stride the stride in bytes. Use 0 for automatic calculation + * @param data the buffer used for drawing. Unaligned `data` will be aligned internally + * @param data_size the size of the buffer in bytes + * @return return LV_RESULT_OK on success, LV_RESULT_INVALID otherwise + */ +lv_result_t lv_draw_buf_init(lv_draw_buf_t * draw_buf, uint32_t w, uint32_t h, lv_color_format_t cf, uint32_t stride, + void * data, uint32_t data_size); + +/** + * Keep using the existing memory, reshape the draw buffer to the given width and height. + * Return NULL if data_size is smaller than the required size. + * @param draw_buf pointer to a draw buffer + * @param cf the new color format, use 0 or LV_COLOR_FORMAT_UNKNOWN to keep using the original color format. + * @param w the new width in pixels + * @param h the new height in pixels + * @param stride the stride in bytes for image. Use 0 for automatic calculation. + */ +lv_draw_buf_t * lv_draw_buf_reshape(lv_draw_buf_t * draw_buf, lv_color_format_t cf, uint32_t w, uint32_t h, + uint32_t stride); + +/** + * Destroy a draw buf by freeing the actual buffer if it's marked as LV_IMAGE_FLAGS_ALLOCATED in header. + * Then free the lv_draw_buf_t struct. + * + * @param draw_buf the draw buffer to destroy + */ +void lv_draw_buf_destroy(lv_draw_buf_t * draw_buf); + +/** + * Return pointer to the buffer at the given coordinates + */ +void * lv_draw_buf_goto_xy(const lv_draw_buf_t * buf, uint32_t x, uint32_t y); + +/** + * Adjust the stride of a draw buf in place. + * @param src pointer to a draw buffer + * @param stride the new stride in bytes for image. Use LV_STRIDE_AUTO for automatic calculation. + * @return LV_RESULT_OK: success or LV_RESULT_INVALID: failed + */ +lv_result_t lv_draw_buf_adjust_stride(lv_draw_buf_t * src, uint32_t stride); + +/** + * Premultiply draw buffer color with alpha channel. + * If it's already premultiplied, return directly. + * Only color formats with alpha channel will be processed. + * + * @return LV_RESULT_OK: premultiply success + */ +lv_result_t lv_draw_buf_premultiply(lv_draw_buf_t * draw_buf); + +bool lv_draw_buf_has_flag(const lv_draw_buf_t * draw_buf, lv_image_flags_t flag); + +void lv_draw_buf_set_flag(lv_draw_buf_t * draw_buf, lv_image_flags_t flag); + +void lv_draw_buf_clear_flag(lv_draw_buf_t * draw_buf, lv_image_flags_t flag); + +/** + * As of now, draw buf share same definition as `lv_image_dsc_t`. + * And is interchangeable with `lv_image_dsc_t`. + */ + +void lv_draw_buf_from_image(lv_draw_buf_t * buf, const lv_image_dsc_t * img); + +void lv_draw_buf_to_image(const lv_draw_buf_t * buf, lv_image_dsc_t * img); + +/** + * Set the palette color of an indexed image. Valid only for `LV_COLOR_FORMAT_I1/2/4/8` + * @param draw_buf pointer to an image descriptor + * @param index the palette color to set: + * - for `LV_COLOR_FORMAT_I1`: 0..1 + * - for `LV_COLOR_FORMAT_I2`: 0..3 + * - for `LV_COLOR_FORMAT_I4`: 0..15 + * - for `LV_COLOR_FORMAT_I8`: 0..255 + * @param color the color to set in lv_color32_t format + */ +void lv_draw_buf_set_palette(lv_draw_buf_t * draw_buf, uint8_t index, lv_color32_t color); + +/** + * @deprecated Use lv_draw_buf_set_palette instead. + */ +void lv_image_buf_set_palette(lv_image_dsc_t * dsc, uint8_t id, lv_color32_t c); + +/** + * @deprecated Use lv_draw_buffer_create/destroy instead. + * Free the data pointer and dsc struct of an image. + */ +void lv_image_buf_free(lv_image_dsc_t * dsc); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_BUF_H*/ diff --git a/include/liblvgl/draw/lv_draw_buf_private.h b/include/liblvgl/draw/lv_draw_buf_private.h new file mode 100644 index 00000000..a2264031 --- /dev/null +++ b/include/liblvgl/draw/lv_draw_buf_private.h @@ -0,0 +1,53 @@ +/** + * @file lv_draw_buf_private.h + * + */ + +#ifndef LV_DRAW_BUF_PRIVATE_H +#define LV_DRAW_BUF_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_draw_buf.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_draw_buf_handlers_t { + lv_draw_buf_malloc_cb buf_malloc_cb; + lv_draw_buf_free_cb buf_free_cb; + lv_draw_buf_align_cb align_pointer_cb; + lv_draw_buf_cache_operation_cb invalidate_cache_cb; + lv_draw_buf_cache_operation_cb flush_cache_cb; + lv_draw_buf_width_to_stride_cb width_to_stride_cb; +}; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Called internally to initialize the draw_buf_handlers in lv_global + */ +void lv_draw_buf_init_handlers(void); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_BUF_PRIVATE_H*/ diff --git a/include/liblvgl/draw/lv_draw_image.h b/include/liblvgl/draw/lv_draw_image.h new file mode 100644 index 00000000..d755604e --- /dev/null +++ b/include/liblvgl/draw/lv_draw_image.h @@ -0,0 +1,129 @@ +/** + * @file lv_draw_image.h + * + */ + +#ifndef LV_DRAW_IMAGE_H +#define LV_DRAW_IMAGE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "lv_draw.h" +#include "lv_image_decoder.h" +#include "lv_draw_buf.h" +#include "../misc/lv_style.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * MACROS + **********************/ + +typedef struct _lv_draw_image_dsc_t { + lv_draw_dsc_base_t base; + + const void * src; + lv_image_header_t header; + + int32_t rotation; + int32_t scale_x; + int32_t scale_y; + int32_t skew_x; + int32_t skew_y; + lv_point_t pivot; + + lv_color_t recolor; + lv_opa_t recolor_opa; + + lv_opa_t opa; + lv_blend_mode_t blend_mode : 4; + + uint16_t antialias : 1; + uint16_t tile : 1; + lv_draw_image_sup_t * sup; + + /** Used to indicate the entire original, non-clipped area where the image is to be drawn. + * This is important for: + * 1. Layer rendering, where it might happen that only a smaller area of the layer is rendered. + * 2. Tiled images, where the target draw area is larger than the image to be tiled. + */ + lv_area_t image_area; + + int32_t clip_radius; + + const lv_image_dsc_t * bitmap_mask_src; +} lv_draw_image_dsc_t; + +/** + * PErform the actual rendering of a decoded image + * @param draw_unit pointer to a draw unit + * @param draw_dsc the draw descriptor of the image + * @param decoder_dsc pointer to the decoded image's descriptor + * @param sup supplementary data + * @param img_coords the absolute coordinates of the image + * @param clipped_img_area the absolute clip coordinates + */ +typedef void (*lv_draw_image_core_cb)(lv_draw_unit_t * draw_unit, const lv_draw_image_dsc_t * draw_dsc, + const lv_image_decoder_dsc_t * decoder_dsc, lv_draw_image_sup_t * sup, + const lv_area_t * img_coords, const lv_area_t * clipped_img_area); + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Initialize an image draw descriptor. + * @param dsc pointer to a draw descriptor + */ +void lv_draw_image_dsc_init(lv_draw_image_dsc_t * dsc); + +/** + * Try to get an image draw descriptor from a draw task. + * @param task draw task + * @return the task's draw descriptor or NULL if the task is not of type LV_DRAW_TASK_TYPE_IMAGE + */ +lv_draw_image_dsc_t * lv_draw_task_get_image_dsc(lv_draw_task_t * task); + +/** + * Create an image draw task + * @param layer pointer to a layer + * @param dsc pointer to an initialized draw descriptor + * @param coords the coordinates of the image + * @note `coords` can be small than the real image area + * (if only a part of the image is rendered) + * or can be larger (in case of tiled images). . + */ +void lv_draw_image(lv_layer_t * layer, const lv_draw_image_dsc_t * dsc, const lv_area_t * coords); + +/** + * Create a draw task to blend a layer to another layer + * @param layer pointer to a layer + * @param dsc pointer to an initialized draw descriptor + * @param coords the coordinates of the layer. + * @note `coords` can be small than the total widget area from which the layer is created + * (if only a part of the widget was rendered to a layer) + */ +void lv_draw_layer(lv_layer_t * layer, const lv_draw_image_dsc_t * dsc, const lv_area_t * coords); + +/** + * Get the type of an image source + * @param src pointer to an image source: + * - pointer to an 'lv_image_t' variable (image stored internally and compiled into the code) + * - a path to a file (e.g. "S:/folder/image.bin") + * - or a symbol (e.g. LV_SYMBOL_CLOSE) + * @return type of the image source LV_IMAGE_SRC_VARIABLE/FILE/SYMBOL/UNKNOWN + */ +lv_image_src_t lv_image_src_get_type(const void * src); + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_IMAGE_H*/ diff --git a/include/liblvgl/draw/lv_draw_image_private.h b/include/liblvgl/draw/lv_draw_image_private.h new file mode 100644 index 00000000..ee22040b --- /dev/null +++ b/include/liblvgl/draw/lv_draw_image_private.h @@ -0,0 +1,85 @@ +/** + * @file lv_draw_image_private.h + * + */ + +#ifndef LV_DRAW_IMAGE_PRIVATE_H +#define LV_DRAW_IMAGE_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_draw_image.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_draw_image_sup_t { + lv_color_t alpha_color; + const lv_color32_t * palette; + uint32_t palette_size : 9; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Can be used by draw units to handle the decoding and + * prepare everything for the actual image rendering + * @param draw_unit pointer to a draw unit + * @param draw_dsc the draw descriptor of the image + * @param coords the absolute coordinates of the image + * @param draw_core_cb a callback to perform the actual rendering + */ +void lv_draw_image_normal_helper(lv_draw_unit_t * draw_unit, const lv_draw_image_dsc_t * draw_dsc, + const lv_area_t * coords, lv_draw_image_core_cb draw_core_cb); + +/** + * Can be used by draw units for TILED images to handle the decoding and + * prepare everything for the actual image rendering + * @param draw_unit pointer to a draw unit + * @param draw_dsc the draw descriptor of the image + * @param coords the absolute coordinates of the image + * @param draw_core_cb a callback to perform the actual rendering + */ +void lv_draw_image_tiled_helper(lv_draw_unit_t * draw_unit, const lv_draw_image_dsc_t * draw_dsc, + const lv_area_t * coords, lv_draw_image_core_cb draw_core_cb); + +/** + * Get the area of a rectangle if its rotated and scaled + * @param res store the coordinates here + * @param w width of the rectangle to transform + * @param h height of the rectangle to transform + * @param angle angle of rotation + * @param scale_x zoom in x direction, (256 no zoom) + * @param scale_y zoom in y direction, (256 no zoom) + * @param pivot x,y pivot coordinates of rotation + */ +void lv_image_buf_get_transformed_area(lv_area_t * res, int32_t w, int32_t h, int32_t angle, + uint16_t scale_x, uint16_t scale_y, const lv_point_t * pivot); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_IMAGE_PRIVATE_H*/ diff --git a/include/liblvgl/draw/lv_draw_img.h b/include/liblvgl/draw/lv_draw_img.h deleted file mode 100644 index dcb71136..00000000 --- a/include/liblvgl/draw/lv_draw_img.h +++ /dev/null @@ -1,104 +0,0 @@ -/** - * @file lv_draw_img.h - * - */ - -#ifndef LV_DRAW_IMG_H -#define LV_DRAW_IMG_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "lv_img_decoder.h" -#include "lv_img_buf.h" -#include "liblvgl/misc/lv_style.h" - -/********************* - * DEFINES - *********************/ - -/********************** - * MACROS - **********************/ - -/********************** - * TYPEDEFS - **********************/ - -typedef struct { - - int16_t angle; - uint16_t zoom; - lv_point_t pivot; - - lv_color_t recolor; - lv_opa_t recolor_opa; - - lv_opa_t opa; - lv_blend_mode_t blend_mode : 4; - - int32_t frame_id; - uint8_t antialias : 1; -} lv_draw_img_dsc_t; - -struct _lv_draw_ctx_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -void lv_draw_img_dsc_init(lv_draw_img_dsc_t * dsc); -/** - * Draw an image - * @param coords the coordinates of the image - * @param mask the image will be drawn only in this area - * @param src pointer to a lv_color_t array which contains the pixels of the image - * @param dsc pointer to an initialized `lv_draw_img_dsc_t` variable - */ -void lv_draw_img(struct _lv_draw_ctx_t * draw_ctx, const lv_draw_img_dsc_t * dsc, const lv_area_t * coords, - const void * src); - - -void lv_draw_img_decoded(struct _lv_draw_ctx_t * draw_ctx, const lv_draw_img_dsc_t * dsc, - const lv_area_t * coords, const uint8_t * map_p, lv_img_cf_t color_format); - -/** - * Get the type of an image source - * @param src pointer to an image source: - * - pointer to an 'lv_img_t' variable (image stored internally and compiled into the code) - * - a path to a file (e.g. "S:/folder/image.bin") - * - or a symbol (e.g. LV_SYMBOL_CLOSE) - * @return type of the image source LV_IMG_SRC_VARIABLE/FILE/SYMBOL/UNKNOWN - */ -lv_img_src_t lv_img_src_get_type(const void * src); - -/** - * Get the pixel size of a color format in bits - * @param cf a color format (`LV_IMG_CF_...`) - * @return the pixel size in bits - */ -uint8_t lv_img_cf_get_px_size(lv_img_cf_t cf); - -/** - * Check if a color format is chroma keyed or not - * @param cf a color format (`LV_IMG_CF_...`) - * @return true: chroma keyed; false: not chroma keyed - */ -bool lv_img_cf_is_chroma_keyed(lv_img_cf_t cf); - -/** - * Check if a color format has alpha channel or not - * @param cf a color format (`LV_IMG_CF_...`) - * @return true: has alpha channel; false: doesn't have alpha channel - */ -bool lv_img_cf_has_alpha(lv_img_cf_t cf); - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_DRAW_IMG_H*/ diff --git a/include/liblvgl/draw/lv_draw_label.h b/include/liblvgl/draw/lv_draw_label.h index 736aa728..e0e46290 100644 --- a/include/liblvgl/draw/lv_draw_label.h +++ b/include/liblvgl/draw/lv_draw_label.h @@ -13,10 +13,12 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/misc/lv_bidi.h" -#include "liblvgl/misc/lv_txt.h" -#include "liblvgl/misc/lv_color.h" -#include "liblvgl/misc/lv_style.h" +#include "lv_draw.h" +#include "lv_draw_rect.h" +#include "../misc/lv_bidi.h" +#include "../misc/lv_text.h" +#include "../misc/lv_color.h" +#include "../misc/lv_style.h" /********************* * DEFINES @@ -28,62 +30,105 @@ extern "C" { **********************/ typedef struct { + lv_draw_dsc_base_t base; + + const char * text; const lv_font_t * font; uint32_t sel_start; uint32_t sel_end; lv_color_t color; lv_color_t sel_color; lv_color_t sel_bg_color; - lv_coord_t line_space; - lv_coord_t letter_space; - lv_coord_t ofs_x; - lv_coord_t ofs_y; + int32_t line_space; + int32_t letter_space; + int32_t ofs_x; + int32_t ofs_y; lv_opa_t opa; lv_base_dir_t bidi_dir; lv_text_align_t align; lv_text_flag_t flag; lv_text_decor_t decor : 3; - lv_blend_mode_t blend_mode: 3; + lv_blend_mode_t blend_mode : 3; + /** + * < 1: malloc buffer and copy `text` there. + * 0: `text` is const and it's pointer will be valid during rendering.*/ + uint8_t text_local : 1; + + /** + * Indicate that the text is constant and its pointer can be safely saved e.g. in a cache. + */ + uint8_t text_static : 1; + lv_draw_label_hint_t * hint; } lv_draw_label_dsc_t; -/** Store some info to speed up drawing of very large texts - * It takes a lot of time to get the first visible character because - * all the previous characters needs to be checked to calculate the positions. - * This structure stores an earlier (e.g. at -1000 px) coordinate and the index of that line. - * Therefore the calculations can start from here.*/ -typedef struct _lv_draw_label_hint_t { - /** Index of the line at `y` coordinate*/ - int32_t line_start; - - /** Give the `y` coordinate of the first letter at `line start` index. Relative to the label's coordinates*/ - int32_t y; - - /** The 'y1' coordinate of the label when the hint was saved. - * Used to invalidate the hint if the label has moved too much.*/ - int32_t coord_y; -} lv_draw_label_hint_t; +/** + * Passed as a parameter to `lv_draw_label_iterate_characters` to + * draw the characters one by one + * @param draw_unit pointer to a draw unit + * @param dsc pointer to `lv_draw_glyph_dsc_t` to describe the character to draw + * if NULL don't draw character + * @param fill_dsc pointer to a fill descriptor to draw a background for the character or + * underline or strike through + * if NULL do not fill anything + * @param fill_area the area to fill + * if NULL do not fill anything + */ +typedef void(*lv_draw_glyph_cb_t)(lv_draw_unit_t * draw_unit, lv_draw_glyph_dsc_t * dsc, lv_draw_fill_dsc_t * fill_dsc, + const lv_area_t * fill_area); -struct _lv_draw_ctx_t; /********************** * GLOBAL PROTOTYPES **********************/ -LV_ATTRIBUTE_FAST_MEM void lv_draw_label_dsc_init(lv_draw_label_dsc_t * dsc); +/** + * Initialize a label draw descriptor + * @param dsc pointer to a draw descriptor + */ +void /* LV_ATTRIBUTE_FAST_MEM */ lv_draw_label_dsc_init(lv_draw_label_dsc_t * dsc); + +/** + * Try to get a label draw descriptor from a draw task. + * @param task draw task + * @return the task's draw descriptor or NULL if the task is not of type LV_DRAW_TASK_TYPE_LABEL + */ +lv_draw_label_dsc_t * lv_draw_task_get_label_dsc(lv_draw_task_t * task); + +/** + * Initialize a glyph draw descriptor. + * Used internally. + * @param dsc pointer to a draw descriptor + */ +void lv_draw_glyph_dsc_init(lv_draw_glyph_dsc_t * dsc); /** - * Write a text - * @param coords coordinates of the label - * @param mask the label will be drawn only in this area - * @param dsc pointer to draw descriptor - * @param txt `\0` terminated text to write - * @param hint pointer to a `lv_draw_label_hint_t` variable. - * It is managed by the draw to speed up the drawing of very long texts (thousands of lines). + * Crate a draw task to render a text + * @param layer pointer to a layer + * @param dsc pointer to draw descriptor + * @param coords coordinates of the character */ -LV_ATTRIBUTE_FAST_MEM void lv_draw_label(struct _lv_draw_ctx_t * draw_ctx, const lv_draw_label_dsc_t * dsc, - const lv_area_t * coords, const char * txt, lv_draw_label_hint_t * hint); +void /* LV_ATTRIBUTE_FAST_MEM */ lv_draw_label(lv_layer_t * layer, const lv_draw_label_dsc_t * dsc, + const lv_area_t * coords); -void lv_draw_letter(struct _lv_draw_ctx_t * draw_ctx, const lv_draw_label_dsc_t * dsc, const lv_point_t * pos_p, - uint32_t letter); +/** + * Crate a draw task to render a single character + * @param layer pointer to a layer + * @param dsc pointer to draw descriptor + * @param point position of the label + * @param unicode_letter the letter to draw + */ +void /* LV_ATTRIBUTE_FAST_MEM */ lv_draw_character(lv_layer_t * layer, lv_draw_label_dsc_t * dsc, + const lv_point_t * point, uint32_t unicode_letter); + +/** + * Should be used during rendering the characters to get the position and other + * parameters of the characters + * @param draw_unit pointer to a draw unit + * @param dsc pointer to draw descriptor + * @param coords coordinates of the label + * @param cb a callback to call to draw each glyphs one by one + */ +void lv_draw_label_iterate_characters(lv_draw_unit_t * draw_unit, const lv_draw_label_dsc_t * dsc, + const lv_area_t * coords, lv_draw_glyph_cb_t cb); /*********************** * GLOBAL VARIABLES diff --git a/include/liblvgl/draw/lv_draw_label_private.h b/include/liblvgl/draw/lv_draw_label_private.h new file mode 100644 index 00000000..4d225d5e --- /dev/null +++ b/include/liblvgl/draw/lv_draw_label_private.h @@ -0,0 +1,68 @@ +/** + * @file lv_draw_label_private.h + * + */ + +#ifndef LV_DRAW_LABEL_PRIVATE_H +#define LV_DRAW_LABEL_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_draw_label.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** Store some info to speed up drawing of very large texts + * It takes a lot of time to get the first visible character because + * all the previous characters needs to be checked to calculate the positions. + * This structure stores an earlier (e.g. at -1000 px) coordinate and the index of that line. + * Therefore the calculations can start from here.*/ +struct _lv_draw_label_hint_t { + /** Index of the line at `y` coordinate*/ + int32_t line_start; + + /** Give the `y` coordinate of the first letter at `line start` index. Relative to the label's coordinates*/ + int32_t y; + + /** The 'y1' coordinate of the label when the hint was saved. + * Used to invalidate the hint if the label has moved too much.*/ + int32_t coord_y; +}; + +struct _lv_draw_glyph_dsc_t { + void * glyph_data; /**< Depends on `format` field, it could be image source or draw buf of bitmap or vector data. */ + lv_font_glyph_format_t format; + const lv_area_t * letter_coords; + const lv_area_t * bg_coords; + const lv_font_glyph_dsc_t * g; + lv_color_t color; + lv_opa_t opa; + lv_draw_buf_t * _draw_buf; /**< a shared draw buf for get_bitmap, do not use it directly, use glyph_data instead */ +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_LABEL_PRIVATE_H*/ diff --git a/include/liblvgl/draw/lv_draw_layer.h b/include/liblvgl/draw/lv_draw_layer.h deleted file mode 100644 index 1280d33a..00000000 --- a/include/liblvgl/draw/lv_draw_layer.h +++ /dev/null @@ -1,83 +0,0 @@ -/** - * @file lv_draw_layer.h - * - */ - -#ifndef LV_DRAW_LAYER_H -#define LV_DRAW_LAYER_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lv_conf_internal.h" - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ -struct _lv_draw_ctx_t; -struct _lv_draw_layer_ctx_t; - -typedef enum { - LV_DRAW_LAYER_FLAG_NONE, - LV_DRAW_LAYER_FLAG_HAS_ALPHA, - LV_DRAW_LAYER_FLAG_CAN_SUBDIVIDE, -} lv_draw_layer_flags_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Create a new layer context. It is used to start and independent rendering session - * with the current draw_ctx - * @param draw_ctx pointer to the current draw context - * @param layer_area the coordinates of the layer - * @param flags OR-ed flags from @lv_draw_layer_flags_t - * @return pointer to the layer context, or NULL on error - */ -struct _lv_draw_layer_ctx_t * lv_draw_layer_create(struct _lv_draw_ctx_t * draw_ctx, const lv_area_t * layer_area, - lv_draw_layer_flags_t flags); - -/** - * Adjust the layer_ctx and/or draw_ctx based on the `layer_ctx->area_act`. - * It's called only if flags has `LV_DRAW_LAYER_FLAG_CAN_SUBDIVIDE` - * @param draw_ctx pointer to the current draw context - * @param layer_ctx pointer to a layer context - * @param flags OR-ed flags from @lv_draw_layer_flags_t - */ -void lv_draw_layer_adjust(struct _lv_draw_ctx_t * draw_ctx, struct _lv_draw_layer_ctx_t * layer_ctx, - lv_draw_layer_flags_t flags); - -/** - * Blend a rendered layer to `layer_ctx->area_act` - * @param draw_ctx pointer to the current draw context - * @param layer_ctx pointer to a layer context - * @param draw_dsc pointer to an image draw descriptor - */ -void lv_draw_layer_blend(struct _lv_draw_ctx_t * draw_ctx, struct _lv_draw_layer_ctx_t * layer_ctx, - lv_draw_img_dsc_t * draw_dsc); - -/** - * Destroy a layer context. - * @param draw_ctx pointer to the current draw context - * @param layer_ctx pointer to a layer context - */ -void lv_draw_layer_destroy(struct _lv_draw_ctx_t * draw_ctx, struct _lv_draw_layer_ctx_t * layer_ctx); - -/********************** - * MACROS - **********************/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_DRAW_LAYER_H*/ diff --git a/include/liblvgl/draw/lv_draw_line.h b/include/liblvgl/draw/lv_draw_line.h index 79ca0dcf..72430fa7 100644 --- a/include/liblvgl/draw/lv_draw_line.h +++ b/include/liblvgl/draw/lv_draw_line.h @@ -13,10 +13,10 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" -#include "liblvgl/misc/lv_color.h" -#include "liblvgl/misc/lv_area.h" -#include "liblvgl/misc/lv_style.h" +#include "../lv_conf_internal.h" +#include "../misc/lv_color.h" +#include "../misc/lv_area.h" +#include "../misc/lv_style.h" /********************* * DEFINES @@ -26,35 +26,44 @@ extern "C" { * TYPEDEFS **********************/ typedef struct { + lv_draw_dsc_base_t base; + + lv_point_precise_t p1; + lv_point_precise_t p2; lv_color_t color; - lv_coord_t width; - lv_coord_t dash_width; - lv_coord_t dash_gap; + int32_t width; + int32_t dash_width; + int32_t dash_gap; lv_opa_t opa; lv_blend_mode_t blend_mode : 2; uint8_t round_start : 1; uint8_t round_end : 1; - uint8_t raw_end : 1; /*Do not bother with perpendicular line ending if it's not visible for any reason*/ + uint8_t raw_end : 1; /**< Do not bother with perpendicular line ending if it's not visible for any reason */ } lv_draw_line_dsc_t; -struct _lv_draw_ctx_t; - /********************** * GLOBAL PROTOTYPES **********************/ -LV_ATTRIBUTE_FAST_MEM void lv_draw_line_dsc_init(lv_draw_line_dsc_t * dsc); +/** + * Initialize a line draw descriptor + * @param dsc pointer to a draw descriptor + */ +void lv_draw_line_dsc_init(lv_draw_line_dsc_t * dsc); /** - * Draw a line - * @param point1 first point of the line - * @param point2 second point of the line - * @param clip the line will be drawn only in this area - * @param dsc pointer to an initialized `lv_draw_line_dsc_t` variable + * Try to get a line draw descriptor from a draw task. + * @param task draw task + * @return the task's draw descriptor or NULL if the task is not of type LV_DRAW_TASK_TYPE_LINE */ -void lv_draw_line(struct _lv_draw_ctx_t * draw_ctx, const lv_draw_line_dsc_t * dsc, const lv_point_t * point1, - const lv_point_t * point2); +lv_draw_line_dsc_t * lv_draw_task_get_line_dsc(lv_draw_task_t * task); +/** + * Create a line draw task + * @param layer pointer to a layer + * @param dsc pointer to an initialized `lv_draw_line_dsc_t` variable + */ +void lv_draw_line(lv_layer_t * layer, const lv_draw_line_dsc_t * dsc); /********************** * MACROS diff --git a/include/liblvgl/draw/lv_draw_mask.h b/include/liblvgl/draw/lv_draw_mask.h index f3c49e5d..6dfed79b 100644 --- a/include/liblvgl/draw/lv_draw_mask.h +++ b/include/liblvgl/draw/lv_draw_mask.h @@ -10,378 +10,40 @@ extern "C" { #endif - /********************* * INCLUDES *********************/ -#include -#include "liblvgl/misc/lv_area.h" -#include "liblvgl/misc/lv_color.h" -#include "liblvgl/misc/lv_math.h" +#include "../misc/lv_color.h" +#include "../misc/lv_area.h" +#include "../misc/lv_style.h" /********************* * DEFINES *********************/ -#define LV_MASK_ID_INV (-1) -#if LV_DRAW_COMPLEX -# define _LV_MASK_MAX_NUM 16 -#else -# define _LV_MASK_MAX_NUM 1 -#endif - -/********************** - * TYPEDEFS - **********************/ - -enum { - LV_DRAW_MASK_RES_TRANSP, - LV_DRAW_MASK_RES_FULL_COVER, - LV_DRAW_MASK_RES_CHANGED, - LV_DRAW_MASK_RES_UNKNOWN -}; - -typedef uint8_t lv_draw_mask_res_t; - -typedef struct { - void * param; - void * custom_id; -} _lv_draw_mask_saved_t; - -typedef _lv_draw_mask_saved_t _lv_draw_mask_saved_arr_t[_LV_MASK_MAX_NUM]; - - - -#if LV_DRAW_COMPLEX == 0 -static inline uint8_t lv_draw_mask_get_cnt(void) -{ - return 0; -} - -static inline bool lv_draw_mask_is_any(const lv_area_t * a) -{ - LV_UNUSED(a); - return false; -} - -#endif - -#if LV_DRAW_COMPLEX - -enum { - LV_DRAW_MASK_TYPE_LINE, - LV_DRAW_MASK_TYPE_ANGLE, - LV_DRAW_MASK_TYPE_RADIUS, - LV_DRAW_MASK_TYPE_FADE, - LV_DRAW_MASK_TYPE_MAP, - LV_DRAW_MASK_TYPE_POLYGON, -}; - -typedef uint8_t lv_draw_mask_type_t; - -enum { - LV_DRAW_MASK_LINE_SIDE_LEFT = 0, - LV_DRAW_MASK_LINE_SIDE_RIGHT, - LV_DRAW_MASK_LINE_SIDE_TOP, - LV_DRAW_MASK_LINE_SIDE_BOTTOM, -}; - -/** - * A common callback type for every mask type. - * Used internally by the library. - */ -typedef lv_draw_mask_res_t (*lv_draw_mask_xcb_t)(lv_opa_t * mask_buf, lv_coord_t abs_x, lv_coord_t abs_y, - lv_coord_t len, - void * p); - -typedef uint8_t lv_draw_mask_line_side_t; - -typedef struct { - lv_draw_mask_xcb_t cb; - lv_draw_mask_type_t type; -} _lv_draw_mask_common_dsc_t; - -typedef struct { - /*The first element must be the common descriptor*/ - _lv_draw_mask_common_dsc_t dsc; - - struct { - /*First point*/ - lv_point_t p1; - - /*Second point*/ - lv_point_t p2; - - /*Which side to keep?*/ - lv_draw_mask_line_side_t side : 2; - } cfg; - - /*A point of the line*/ - lv_point_t origo; - - /*X / (1024*Y) steepness (X is 0..1023 range). What is the change of X in 1024 Y?*/ - int32_t xy_steep; - - /*Y / (1024*X) steepness (Y is 0..1023 range). What is the change of Y in 1024 X?*/ - int32_t yx_steep; - - /*Helper which stores yx_steep for flat lines and xy_steep for steep (non flat) lines*/ - int32_t steep; - - /*Steepness in 1 px in 0..255 range. Used only by flat lines.*/ - int32_t spx; - - /*1: It's a flat line? (Near to horizontal)*/ - uint8_t flat : 1; - - /*Invert the mask. The default is: Keep the left part. - *It is used to select left/right/top/bottom*/ - uint8_t inv: 1; -} lv_draw_mask_line_param_t; - -typedef struct { - /*The first element must be the common descriptor*/ - _lv_draw_mask_common_dsc_t dsc; - - struct { - lv_point_t vertex_p; - lv_coord_t start_angle; - lv_coord_t end_angle; - } cfg; - - lv_draw_mask_line_param_t start_line; - lv_draw_mask_line_param_t end_line; - uint16_t delta_deg; -} lv_draw_mask_angle_param_t; - -typedef struct { - uint8_t * buf; - lv_opa_t * cir_opa; /*Opacity of values on the circumference of an 1/4 circle*/ - uint16_t * x_start_on_y; /*The x coordinate of the circle for each y value*/ - uint16_t * opa_start_on_y; /*The index of `cir_opa` for each y value*/ - int32_t life; /*How many times the entry way used*/ - uint32_t used_cnt; /*Like a semaphore to count the referencing masks*/ - lv_coord_t radius; /*The radius of the entry*/ -} _lv_draw_mask_radius_circle_dsc_t; - -typedef _lv_draw_mask_radius_circle_dsc_t _lv_draw_mask_radius_circle_dsc_arr_t[LV_CIRCLE_CACHE_SIZE]; - -typedef struct { - /*The first element must be the common descriptor*/ - _lv_draw_mask_common_dsc_t dsc; - - struct { - lv_area_t rect; - lv_coord_t radius; - /*Invert the mask. 0: Keep the pixels inside.*/ - uint8_t outer: 1; - } cfg; - - _lv_draw_mask_radius_circle_dsc_t * circle; -} lv_draw_mask_radius_param_t; - - -typedef struct { - /*The first element must be the common descriptor*/ - _lv_draw_mask_common_dsc_t dsc; - - struct { - lv_area_t coords; - lv_coord_t y_top; - lv_coord_t y_bottom; - lv_opa_t opa_top; - lv_opa_t opa_bottom; - } cfg; - -} lv_draw_mask_fade_param_t; - - -typedef struct _lv_draw_mask_map_param_t { - /*The first element must be the common descriptor*/ - _lv_draw_mask_common_dsc_t dsc; - - struct { - lv_area_t coords; - const lv_opa_t * map; - } cfg; -} lv_draw_mask_map_param_t; - -typedef struct { - /*The first element must be the common descriptor*/ - _lv_draw_mask_common_dsc_t dsc; - - struct { - lv_point_t * points; - uint16_t point_cnt; - } cfg; -} lv_draw_mask_polygon_param_t; - /********************** * GLOBAL PROTOTYPES **********************/ /** - * Add a draw mask. Everything drawn after it (until removing the mask) will be affected by the mask. - * @param param an initialized mask parameter. Only the pointer is saved. - * @param custom_id a custom pointer to identify the mask. Used in `lv_draw_mask_remove_custom`. - * @return the an integer, the ID of the mask. Can be used in `lv_draw_mask_remove_id`. - */ -int16_t lv_draw_mask_add(void * param, void * custom_id); - -//! @cond Doxygen_Suppress - -/** - * Apply the added buffers on a line. Used internally by the library's drawing routines. - * @param mask_buf store the result mask here. Has to be `len` byte long. Should be initialized with `0xFF`. - * @param abs_x absolute X coordinate where the line to calculate start - * @param abs_y absolute Y coordinate where the line to calculate start - * @param len length of the line to calculate (in pixel count) - * @return One of these values: - * - `LV_DRAW_MASK_RES_FULL_TRANSP`: the whole line is transparent. `mask_buf` is not set to zero - * - `LV_DRAW_MASK_RES_FULL_COVER`: the whole line is fully visible. `mask_buf` is unchanged - * - `LV_DRAW_MASK_RES_CHANGED`: `mask_buf` has changed, it shows the desired opacity of each pixel in the given line - */ -LV_ATTRIBUTE_FAST_MEM lv_draw_mask_res_t lv_draw_mask_apply(lv_opa_t * mask_buf, lv_coord_t abs_x, lv_coord_t abs_y, - lv_coord_t len); - -/** - * Apply the specified buffers on a line. Used internally by the library's drawing routines. - * @param mask_buf store the result mask here. Has to be `len` byte long. Should be initialized with `0xFF`. - * @param abs_x absolute X coordinate where the line to calculate start - * @param abs_y absolute Y coordinate where the line to calculate start - * @param len length of the line to calculate (in pixel count) - * @param ids ID array of added buffers - * @param ids_count number of ID array - * @return One of these values: - * - `LV_DRAW_MASK_RES_FULL_TRANSP`: the whole line is transparent. `mask_buf` is not set to zero - * - `LV_DRAW_MASK_RES_FULL_COVER`: the whole line is fully visible. `mask_buf` is unchanged - * - `LV_DRAW_MASK_RES_CHANGED`: `mask_buf` has changed, it shows the desired opacity of each pixel in the given line - */ -LV_ATTRIBUTE_FAST_MEM lv_draw_mask_res_t lv_draw_mask_apply_ids(lv_opa_t * mask_buf, lv_coord_t abs_x, lv_coord_t abs_y, - lv_coord_t len, const int16_t * ids, int16_t ids_count); - -//! @endcond - -/** - * Remove a mask with a given ID - * @param id the ID of the mask. Returned by `lv_draw_mask_add` - * @return the parameter of the removed mask. - * If more masks have `custom_id` ID then the last mask's parameter will be returned - */ -void * lv_draw_mask_remove_id(int16_t id); - -/** - * Remove all mask with a given custom ID - * @param custom_id a pointer used in `lv_draw_mask_add` - * @return return the parameter of the removed mask. - * If more masks have `custom_id` ID then the last mask's parameter will be returned - */ -void * lv_draw_mask_remove_custom(void * custom_id); - -/** - * Free the data from the parameter. - * It's called inside `lv_draw_mask_remove_id` and `lv_draw_mask_remove_custom` - * Needs to be called only in special cases when the mask is not added by `lv_draw_mask_add` - * and not removed by `lv_draw_mask_remove_id` or `lv_draw_mask_remove_custom` - * @param p pointer to a mask parameter - */ -void lv_draw_mask_free_param(void * p); - -/** - * Called by LVGL the rendering of a screen is ready to clean up - * the temporal (cache) data of the masks - */ -void _lv_draw_mask_cleanup(void); - -//! @cond Doxygen_Suppress - -/** - * Count the currently added masks - * @return number of active masks - */ -LV_ATTRIBUTE_FAST_MEM uint8_t lv_draw_mask_get_cnt(void); - - -/** - * Check if there is any added draw mask - * @param a an area to test for affecting masks. - * @return true: there is t least 1 draw mask; false: there are no draw masks - */ -bool lv_draw_mask_is_any(const lv_area_t * a); - -//! @endcond - -/** - *Initialize a line mask from two points. - * @param param pointer to a `lv_draw_mask_param_t` to initialize - * @param p1x X coordinate of the first point of the line - * @param p1y Y coordinate of the first point of the line - * @param p2x X coordinate of the second point of the line - * @param p2y y coordinate of the second point of the line - * @param side and element of `lv_draw_mask_line_side_t` to describe which side to keep. - * With `LV_DRAW_MASK_LINE_SIDE_LEFT/RIGHT` and horizontal line all pixels are kept - * With `LV_DRAW_MASK_LINE_SIDE_TOP/BOTTOM` and vertical line all pixels are kept - */ -void lv_draw_mask_line_points_init(lv_draw_mask_line_param_t * param, lv_coord_t p1x, lv_coord_t p1y, lv_coord_t p2x, - lv_coord_t p2y, lv_draw_mask_line_side_t side); - -/** - *Initialize a line mask from a point and an angle. - * @param param pointer to a `lv_draw_mask_param_t` to initialize - * @param px X coordinate of a point of the line - * @param py X coordinate of a point of the line - * @param angle right 0 deg, bottom: 90 - * @param side and element of `lv_draw_mask_line_side_t` to describe which side to keep. - * With `LV_DRAW_MASK_LINE_SIDE_LEFT/RIGHT` and horizontal line all pixels are kept - * With `LV_DRAW_MASK_LINE_SIDE_TOP/BOTTOM` and vertical line all pixels are kept - */ -void lv_draw_mask_line_angle_init(lv_draw_mask_line_param_t * param, lv_coord_t p1x, lv_coord_t py, int16_t angle, - lv_draw_mask_line_side_t side); - -/** - * Initialize an angle mask. - * @param param pointer to a `lv_draw_mask_param_t` to initialize - * @param vertex_x X coordinate of the angle vertex (absolute coordinates) - * @param vertex_y Y coordinate of the angle vertex (absolute coordinates) - * @param start_angle start angle in degrees. 0 deg on the right, 90 deg, on the bottom - * @param end_angle end angle - */ -void lv_draw_mask_angle_init(lv_draw_mask_angle_param_t * param, lv_coord_t vertex_x, lv_coord_t vertex_y, - lv_coord_t start_angle, lv_coord_t end_angle); - -/** - * Initialize a fade mask. - * @param param pointer to an `lv_draw_mask_radius_param_t` to initialize - * @param rect coordinates of the rectangle to affect (absolute coordinates) - * @param radius radius of the rectangle - * @param inv true: keep the pixels inside the rectangle; keep the pixels outside of the rectangle + * Initialize a rectangle mask draw descriptor. + * @param dsc pointer to a draw descriptor */ -void lv_draw_mask_radius_init(lv_draw_mask_radius_param_t * param, const lv_area_t * rect, lv_coord_t radius, bool inv); +void /* LV_ATTRIBUTE_FAST_MEM */ lv_draw_mask_rect_dsc_init(lv_draw_mask_rect_dsc_t * dsc); /** - * Initialize a fade mask. - * @param param pointer to a `lv_draw_mask_param_t` to initialize - * @param coords coordinates of the area to affect (absolute coordinates) - * @param opa_top opacity on the top - * @param y_top at which coordinate start to change to opacity to `opa_bottom` - * @param opa_bottom opacity at the bottom - * @param y_bottom at which coordinate reach `opa_bottom`. + * Try to get a rectangle mask draw descriptor from a draw task. + * @param task draw task + * @return the task's draw descriptor or NULL if the task is not of type LV_DRAW_TASK_TYPE_MASK_RECTANGLE */ -void lv_draw_mask_fade_init(lv_draw_mask_fade_param_t * param, const lv_area_t * coords, lv_opa_t opa_top, - lv_coord_t y_top, - lv_opa_t opa_bottom, lv_coord_t y_bottom); +lv_draw_mask_rect_dsc_t * lv_draw_task_get_mask_rect_dsc(lv_draw_task_t * task); /** - * Initialize a map mask. - * @param param pointer to a `lv_draw_mask_param_t` to initialize - * @param coords coordinates of the map (absolute coordinates) - * @param map array of bytes with the mask values + * Create a draw task to mask a rectangle from the buffer + * @param layer pointer to a layer + * @param dsc pointer to a draw descriptor */ -void lv_draw_mask_map_init(lv_draw_mask_map_param_t * param, const lv_area_t * coords, const lv_opa_t * map); - -void lv_draw_mask_polygon_init(lv_draw_mask_polygon_param_t * param, const lv_point_t * points, uint16_t point_cnt); - -#endif /*LV_DRAW_COMPLEX*/ +void lv_draw_mask_rect(lv_layer_t * layer, const lv_draw_mask_rect_dsc_t * dsc); /********************** * MACROS diff --git a/include/liblvgl/draw/lv_draw_mask_private.h b/include/liblvgl/draw/lv_draw_mask_private.h new file mode 100644 index 00000000..9863e365 --- /dev/null +++ b/include/liblvgl/draw/lv_draw_mask_private.h @@ -0,0 +1,51 @@ +/** + * @file lv_draw_mask_private.h + * + */ + +#ifndef LV_DRAW_MASK_PRIVATE_H +#define LV_DRAW_MASK_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_draw_mask.h" +#include "lv_draw.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * TYPEDEFS + **********************/ +struct _lv_draw_mask_rect_dsc_t { + lv_draw_dsc_base_t base; + + lv_area_t area; + int32_t radius; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_MASK_PRIVATE_H*/ diff --git a/include/liblvgl/draw/lv_draw_private.h b/include/liblvgl/draw/lv_draw_private.h new file mode 100644 index 00000000..e3fd0345 --- /dev/null +++ b/include/liblvgl/draw/lv_draw_private.h @@ -0,0 +1,196 @@ +/** + * @file lv_draw_private.h + * + */ + +/** + * Modified by NXP in 2024 + */ + +#ifndef LV_DRAW_PRIVATE_H +#define LV_DRAW_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_draw.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_draw_task_t { + lv_draw_task_t * next; + + lv_draw_task_type_t type; + + /** + * The area where to draw + */ + lv_area_t area; + + /** + * The real draw area. E.g. for shadow, outline, or transformed images it's different from `area` + */ + lv_area_t _real_area; + + /** The original area which is updated*/ + lv_area_t clip_area_original; + + /** + * The clip area of the layer is saved here when the draw task is created. + * As the clip area of the layer can be changed as new draw tasks are added its current value needs to be saved. + * Therefore during drawing the layer's clip area shouldn't be used as it might be already changed for other draw tasks. + */ + lv_area_t clip_area; + +#if LV_DRAW_TRANSFORM_USE_MATRIX + /** Transform matrix to be applied when rendering the layer */ + lv_matrix_t matrix; +#endif + + volatile int state; /** int instead of lv_draw_task_state_t to be sure its atomic */ + + void * draw_dsc; + + /** + * The ID of the draw_unit which should take this task + */ + uint8_t preferred_draw_unit_id; + + /** + * Set to which extent `preferred_draw_unit_id` is good at this task. + * 80: means 20% better (faster) than software rendering + * 100: the default value + * 110: means 10% worse (slower) than software rendering + */ + uint8_t preference_score; + +}; + +struct _lv_draw_mask_t { + void * user_data; +}; + +struct _lv_draw_unit_t { + lv_draw_unit_t * next; + + /** + * The target_layer on which drawing should happen + */ + lv_layer_t * target_layer; + + const lv_area_t * clip_area; + + /** + * Called to try to assign a draw task to itself. + * `lv_draw_get_next_available_task` can be used to get an independent draw task. + * A draw task should be assign only if the draw unit can draw it too + * @param draw_unit pointer to the draw unit + * @param layer pointer to a layer on which the draw task should be drawn + * @return >=0: The number of taken draw task: + * 0 means the task has not yet been completed. + * 1 means a new task has been accepted. + * -1: The draw unit wanted to work on a task but couldn't do that + * due to some errors (e.g. out of memory). + * It signals that LVGL should call the dispatcher later again + * to let draw unit try to start the rendering again. + */ + int32_t (*dispatch_cb)(lv_draw_unit_t * draw_unit, lv_layer_t * layer); + + /** + * + * @param draw_unit + * @param task + * @return + */ + int32_t (*evaluate_cb)(lv_draw_unit_t * draw_unit, lv_draw_task_t * task); + + /** + * Called to signal the unit to complete all tasks in order to return their ready status. + * This callback can be implemented in case of asynchronous task processing. + * Below is an example to show the difference between synchronous and asynchronous: + * + * Synchronous: + * LVGL thread DRAW thread HW + * + * task1 --> submit --> Receive task1 + * wait_for_finish() + * <-- task1->state = READY <-- Complete task1 + * task2 --> submit --> Receive task2 + * wait_for_finish() + * task2->state = READY <-- Complete task2 + * task3 --> submit --> Receive task3 + * wait_for_finish() + * <-- task3->state = READY <-- Complete task3 + * task4 --> submit --> Receive task4 + * wait_for_finish() + * <-- task4->state = READY <-- Complete task4 + * NO MORE TASKS + * + * + * Asynchronous: + * LVGL thread DRAW thread HW + * is IDLE + * task1 --> queue task1 + * submit --> Receive task1 + * task2 --> queue task2 is BUSY (with task1) + * task3 --> queue task3 still BUSY (with task1) + * task4 --> queue task4 becomes IDLE + * <-- task1->state = READY <-- Complete task1 + * submit --> Receive task2, task3, task4 + * NO MORE TASKS + * wait_for_finish_cb() wait_for_finish() + * <-- Complete task2, task3, task4 + * <-- task2->state = READY <-- + * <-- task3->state = READY <-- + * <-- task4->state = READY <-- + * + * @param draw_unit + * @return + */ + int32_t (*wait_for_finish_cb)(lv_draw_unit_t * draw_unit); + + /** + * Called to delete draw unit. + * @param draw_unit + * @return + */ + int32_t (*delete_cb)(lv_draw_unit_t * draw_unit); +}; + +typedef struct { + lv_draw_unit_t * unit_head; + uint32_t unit_cnt; + uint32_t used_memory_for_layers_kb; +#if LV_USE_OS + lv_thread_sync_t sync; +#else + volatile int dispatch_req; +#endif + lv_mutex_t circle_cache_mutex; + bool task_running; +} lv_draw_global_info_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_PRIVATE_H*/ diff --git a/include/liblvgl/draw/lv_draw_rect.h b/include/liblvgl/draw/lv_draw_rect.h index e259446e..6fe3a82e 100644 --- a/include/liblvgl/draw/lv_draw_rect.h +++ b/include/liblvgl/draw/lv_draw_rect.h @@ -13,10 +13,10 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" -#include "liblvgl/misc/lv_color.h" -#include "liblvgl/misc/lv_area.h" -#include "liblvgl/misc/lv_style.h" +#include "lv_draw.h" +#include "../misc/lv_color.h" +#include "../misc/lv_area.h" +#include "../misc/lv_style.h" #include "sw/lv_draw_sw_gradient.h" /********************* @@ -30,8 +30,9 @@ LV_EXPORT_CONST_INT(LV_RADIUS_CIRCLE); **********************/ typedef struct { - lv_coord_t radius; - lv_blend_mode_t blend_mode; + lv_draw_dsc_base_t base; + + int32_t radius; /*Background*/ lv_opa_t bg_opa; @@ -39,51 +40,128 @@ typedef struct { lv_grad_dsc_t bg_grad; /*Background img*/ - const void * bg_img_src; - const void * bg_img_symbol_font; - lv_color_t bg_img_recolor; - lv_opa_t bg_img_opa; - lv_opa_t bg_img_recolor_opa; - uint8_t bg_img_tiled; + const void * bg_image_src; + const void * bg_image_symbol_font; + lv_color_t bg_image_recolor; + lv_opa_t bg_image_opa; + lv_opa_t bg_image_recolor_opa; + uint8_t bg_image_tiled; /*Border*/ lv_color_t border_color; - lv_coord_t border_width; + int32_t border_width; lv_opa_t border_opa; - uint8_t border_post : 1; /*There is a border it will be drawn later.*/ lv_border_side_t border_side : 5; + uint8_t border_post : 1; /*The border will be drawn later*/ /*Outline*/ lv_color_t outline_color; - lv_coord_t outline_width; - lv_coord_t outline_pad; + int32_t outline_width; + int32_t outline_pad; lv_opa_t outline_opa; /*Shadow*/ lv_color_t shadow_color; - lv_coord_t shadow_width; - lv_coord_t shadow_ofs_x; - lv_coord_t shadow_ofs_y; - lv_coord_t shadow_spread; + int32_t shadow_width; + int32_t shadow_offset_x; + int32_t shadow_offset_y; + int32_t shadow_spread; lv_opa_t shadow_opa; } lv_draw_rect_dsc_t; -struct _lv_draw_ctx_t; +typedef struct { + lv_draw_dsc_base_t base; + + int32_t radius; + + lv_opa_t opa; + lv_color_t color; + lv_grad_dsc_t grad; +} lv_draw_fill_dsc_t; + +typedef struct { + lv_draw_dsc_base_t base; + + int32_t radius; + + lv_color_t color; + int32_t width; + lv_opa_t opa; + lv_border_side_t side : 5; + +} lv_draw_border_dsc_t; + +typedef struct { + lv_draw_dsc_base_t base; + + int32_t radius; + + lv_color_t color; + int32_t width; + int32_t spread; + int32_t ofs_x; + int32_t ofs_y; + lv_opa_t opa; + uint8_t bg_cover : 1; +} lv_draw_box_shadow_dsc_t; /********************** * GLOBAL PROTOTYPES **********************/ -LV_ATTRIBUTE_FAST_MEM void lv_draw_rect_dsc_init(lv_draw_rect_dsc_t * dsc); +/** + * Initialize a rectangle draw descriptor. + * @param dsc pointer to a draw descriptor + */ +void /* LV_ATTRIBUTE_FAST_MEM */ lv_draw_rect_dsc_init(lv_draw_rect_dsc_t * dsc); + +/** + * Initialize a fill draw descriptor. + * @param dsc pointer to a draw descriptor + */ +void lv_draw_fill_dsc_init(lv_draw_fill_dsc_t * dsc); + +/** + * Try to get a fill draw descriptor from a draw task. + * @param task draw task + * @return the task's draw descriptor or NULL if the task is not of type LV_DRAW_TASK_TYPE_FILL + */ +lv_draw_fill_dsc_t * lv_draw_task_get_fill_dsc(lv_draw_task_t * task); + +/** + * Initialize a border draw descriptor. + * @param dsc pointer to a draw descriptor + */ +void lv_draw_border_dsc_init(lv_draw_border_dsc_t * dsc); +/** + * Try to get a border draw descriptor from a draw task. + * @param task draw task + * @return the task's draw descriptor or NULL if the task is not of type LV_DRAW_TASK_TYPE_BORDER + */ +lv_draw_border_dsc_t * lv_draw_task_get_border_dsc(lv_draw_task_t * task); + +/** + * Initialize a box shadow draw descriptor. + * @param dsc pointer to a draw descriptor + */ +void lv_draw_box_shadow_dsc_init(lv_draw_box_shadow_dsc_t * dsc); + +/** + * Try to get a box shadow draw descriptor from a draw task. + * @param task draw task + * @return the task's draw descriptor or NULL if the task is not of type LV_DRAW_TASK_TYPE_BOX_SHADOW + */ +lv_draw_box_shadow_dsc_t * lv_draw_task_get_box_shadow_dsc(lv_draw_task_t * task); /** - * Draw a rectangle - * @param coords the coordinates of the rectangle - * @param clip the rectangle will be drawn only in this area - * @param dsc pointer to an initialized `lv_draw_rect_dsc_t` variable + * The rectangle is a wrapper for fill, border, bg. image and box shadow. + * Internally fill, border, image and box shadow draw tasks will be created. + * @param layer pointer to a layer + * @param dsc pointer to an initialized draw descriptor variable + * @param coords the coordinates of the rectangle */ -void lv_draw_rect(struct _lv_draw_ctx_t * draw_ctx, const lv_draw_rect_dsc_t * dsc, const lv_area_t * coords); +void lv_draw_rect(lv_layer_t * layer, const lv_draw_rect_dsc_t * dsc, const lv_area_t * coords); /********************** * MACROS diff --git a/include/liblvgl/extra/themes/lv_themes.h b/include/liblvgl/draw/lv_draw_rect_private.h similarity index 69% rename from include/liblvgl/extra/themes/lv_themes.h rename to include/liblvgl/draw/lv_draw_rect_private.h index 372f6260..ad75b535 100644 --- a/include/liblvgl/extra/themes/lv_themes.h +++ b/include/liblvgl/draw/lv_draw_rect_private.h @@ -1,10 +1,10 @@ /** - * @file lv_themes.h + * @file lv_draw_rect_private.h * */ -#ifndef LV_THEMES_H -#define LV_THEMES_H +#ifndef LV_DRAW_RECT_PRIVATE_H +#define LV_DRAW_RECT_PRIVATE_H #ifdef __cplusplus extern "C" { @@ -13,9 +13,8 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "default/lv_theme_default.h" -#include "mono/lv_theme_mono.h" -#include "basic/lv_theme_basic.h" + +#include "lv_draw_rect.h" /********************* * DEFINES @@ -37,4 +36,4 @@ extern "C" { } /*extern "C"*/ #endif -#endif /*LV_THEMES_H*/ +#endif /*LV_DRAW_RECT_PRIVATE_H*/ diff --git a/include/liblvgl/draw/lv_draw_transform.h b/include/liblvgl/draw/lv_draw_transform.h deleted file mode 100644 index 6e6236ca..00000000 --- a/include/liblvgl/draw/lv_draw_transform.h +++ /dev/null @@ -1,44 +0,0 @@ -/** - * @file lv_draw_transform.h - * - */ - -#ifndef LV_DRAW_TRANSFORM_H -#define LV_DRAW_TRANSFORM_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lv_conf_internal.h" -#include "liblvgl/misc/lv_area.h" - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ -struct _lv_draw_ctx_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -void lv_draw_transform(struct _lv_draw_ctx_t * draw_ctx, const lv_area_t * dest_area, const void * src_buf, - lv_coord_t src_w, lv_coord_t src_h, - lv_coord_t src_stride, const lv_draw_img_dsc_t * draw_dsc, lv_img_cf_t cf, lv_color_t * cbuf, lv_opa_t * abuf); - -/********************** - * MACROS - **********************/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_DRAW_TRANSFORM_H*/ diff --git a/include/liblvgl/draw/lv_draw_triangle.h b/include/liblvgl/draw/lv_draw_triangle.h index e8d85750..b1ad8e58 100644 --- a/include/liblvgl/draw/lv_draw_triangle.h +++ b/include/liblvgl/draw/lv_draw_triangle.h @@ -22,15 +22,40 @@ extern "C" { /********************** * TYPEDEFS **********************/ +typedef struct { + lv_draw_dsc_base_t base; + + lv_opa_t bg_opa; + lv_color_t bg_color; + lv_grad_dsc_t bg_grad; + + lv_point_precise_t p[3]; +} lv_draw_triangle_dsc_t; /********************** * GLOBAL PROTOTYPES **********************/ -void lv_draw_polygon(struct _lv_draw_ctx_t * draw_ctx, const lv_draw_rect_dsc_t * draw_dsc, const lv_point_t points[], - uint16_t point_cnt); +/** + * Initialize a triangle draw descriptor + * @param draw_dsc pointer to a draw descriptor + */ +void lv_draw_triangle_dsc_init(lv_draw_triangle_dsc_t * draw_dsc); + +/** + * Try to get a triangle draw descriptor from a draw task. + * @param task draw task + * @return the task's draw descriptor or NULL if the task is not of type LV_DRAW_TASK_TYPE_TRIANGLE + */ +lv_draw_triangle_dsc_t * lv_draw_task_get_triangle_dsc(lv_draw_task_t * task); + +/** + * Create a triangle draw task + * @param layer pointer to a layer + * @param draw_dsc pointer to an initialized `lv_draw_triangle_dsc_t` object + */ +void lv_draw_triangle(lv_layer_t * layer, const lv_draw_triangle_dsc_t * draw_dsc); -void lv_draw_triangle(struct _lv_draw_ctx_t * draw_ctx, const lv_draw_rect_dsc_t * draw_dsc, const lv_point_t points[]); /********************** * MACROS **********************/ diff --git a/include/liblvgl/draw/lv_draw_triangle_private.h b/include/liblvgl/draw/lv_draw_triangle_private.h new file mode 100644 index 00000000..7fc8ed5a --- /dev/null +++ b/include/liblvgl/draw/lv_draw_triangle_private.h @@ -0,0 +1,43 @@ +/** + * @file lv_draw_triangle_private.h + * + */ + +#ifndef LV_DRAW_TRIANGLE_PRIVATE_H +#define LV_DRAW_TRIANGLE_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_draw_triangle.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_TRIANGLE_PRIVATE_H*/ diff --git a/include/liblvgl/draw/lv_draw_vector.h b/include/liblvgl/draw/lv_draw_vector.h new file mode 100644 index 00000000..5a59e525 --- /dev/null +++ b/include/liblvgl/draw/lv_draw_vector.h @@ -0,0 +1,489 @@ +/** + * @file lv_draw_vector.h + * + */ + +#ifndef LV_DRAW_VECTOR_H +#define LV_DRAW_VECTOR_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../misc/lv_array.h" +#include "../misc/lv_matrix.h" +#include "lv_draw_image.h" + +#if LV_USE_VECTOR_GRAPHIC + +#if !LV_USE_MATRIX +#error "lv_draw_vector needs LV_USE_MATRIX = 1" +#endif + +/********************** + * TYPEDEFS + **********************/ +typedef enum { + LV_VECTOR_FILL_NONZERO = 0, + LV_VECTOR_FILL_EVENODD, +} lv_vector_fill_t; + +typedef enum { + LV_VECTOR_STROKE_CAP_BUTT = 0, + LV_VECTOR_STROKE_CAP_SQUARE, + LV_VECTOR_STROKE_CAP_ROUND, +} lv_vector_stroke_cap_t; + +typedef enum { + LV_VECTOR_STROKE_JOIN_MITER = 0, + LV_VECTOR_STROKE_JOIN_BEVEL, + LV_VECTOR_STROKE_JOIN_ROUND, +} lv_vector_stroke_join_t; + +typedef enum { + LV_VECTOR_PATH_QUALITY_MEDIUM = 0, /* default*/ + LV_VECTOR_PATH_QUALITY_HIGH, + LV_VECTOR_PATH_QUALITY_LOW, +} lv_vector_path_quality_t; + +typedef enum { + LV_VECTOR_BLEND_SRC_OVER = 0, + LV_VECTOR_BLEND_SRC_IN, + LV_VECTOR_BLEND_DST_OVER, + LV_VECTOR_BLEND_DST_IN, + LV_VECTOR_BLEND_SCREEN, + LV_VECTOR_BLEND_MULTIPLY, + LV_VECTOR_BLEND_NONE, + LV_VECTOR_BLEND_ADDITIVE, + LV_VECTOR_BLEND_SUBTRACTIVE, +} lv_vector_blend_t; + +typedef enum { + LV_VECTOR_PATH_OP_MOVE_TO = 0, + LV_VECTOR_PATH_OP_LINE_TO, + LV_VECTOR_PATH_OP_QUAD_TO, + LV_VECTOR_PATH_OP_CUBIC_TO, + LV_VECTOR_PATH_OP_CLOSE, +} lv_vector_path_op_t; + +typedef enum { + LV_VECTOR_DRAW_STYLE_SOLID = 0, + LV_VECTOR_DRAW_STYLE_PATTERN, + LV_VECTOR_DRAW_STYLE_GRADIENT, +} lv_vector_draw_style_t; + +typedef enum { + LV_VECTOR_GRADIENT_SPREAD_PAD = 0, + LV_VECTOR_GRADIENT_SPREAD_REPEAT, + LV_VECTOR_GRADIENT_SPREAD_REFLECT, +} lv_vector_gradient_spread_t; + +typedef enum { + LV_VECTOR_GRADIENT_STYLE_LINEAR = 0, + LV_VECTOR_GRADIENT_STYLE_RADIAL, +} lv_vector_gradient_style_t; + +struct _lv_fpoint_t { + float x; + float y; +}; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Transform the coordinates of a point using given matrix + * @param matrix pointer to a matrix + * @param point pointer to a point + */ +void lv_matrix_transform_point(const lv_matrix_t * matrix, lv_fpoint_t * point); + +/** + * Transform all the coordinates of a path using given matrix + * @param matrix pointer to a matrix + * @param path pointer to a path + */ +void lv_matrix_transform_path(const lv_matrix_t * matrix, lv_vector_path_t * path); + +/** + * Create a vector graphic path object + * @param quality the quality hint of path + * @return pointer to the created path object + */ +lv_vector_path_t * lv_vector_path_create(lv_vector_path_quality_t quality); + +/** + * Copy a path data to another + * @param target_path pointer to a path + * @param path pointer to source path + */ +void lv_vector_path_copy(lv_vector_path_t * target_path, const lv_vector_path_t * path); + +/** + * Clear path data + * @param path pointer to a path + */ +void lv_vector_path_clear(lv_vector_path_t * path); + +/** + * Delete the graphic path object + * @param path pointer to a path + */ +void lv_vector_path_delete(lv_vector_path_t * path); + +/** + * Begin a new sub path and set a point to path + * @param path pointer to a path + * @param p pointer to a `lv_fpoint_t` variable + */ +void lv_vector_path_move_to(lv_vector_path_t * path, const lv_fpoint_t * p); + +/** + * Add a line to the path from last point to the point + * @param path pointer to a path + * @param p pointer to a `lv_fpoint_t` variable + */ +void lv_vector_path_line_to(lv_vector_path_t * path, const lv_fpoint_t * p); + +/** + * Add a quadratic bezier line to the path from last point to the point + * @param path pointer to a path + * @param p1 pointer to a `lv_fpoint_t` variable for control point + * @param p2 pointer to a `lv_fpoint_t` variable for end point + */ +void lv_vector_path_quad_to(lv_vector_path_t * path, const lv_fpoint_t * p1, const lv_fpoint_t * p2); + +/** + * Add a cubic bezier line to the path from last point to the point + * @param path pointer to a path + * @param p1 pointer to a `lv_fpoint_t` variable for first control point + * @param p2 pointer to a `lv_fpoint_t` variable for second control point + * @param p3 pointer to a `lv_fpoint_t` variable for end point + */ +void lv_vector_path_cubic_to(lv_vector_path_t * path, const lv_fpoint_t * p1, const lv_fpoint_t * p2, + const lv_fpoint_t * p3); + +/** + * Close the sub path + * @param path pointer to a path + */ +void lv_vector_path_close(lv_vector_path_t * path); + +/** + * Get the bounding box of a path + * @param path pointer to a path + * @param area pointer to a `lv_area_t` variable for bounding box + */ +void lv_vector_path_get_bounding(const lv_vector_path_t * path, lv_area_t * area); + +/** + * Add a rectangle to the path + * @param path pointer to a path + * @param rect pointer to a `lv_area_t` variable + * @param rx the horizontal radius for rounded rectangle + * @param ry the vertical radius for rounded rectangle + */ +void lv_vector_path_append_rect(lv_vector_path_t * path, const lv_area_t * rect, float rx, float ry); + +/** + * Add a circle to the path + * @param path pointer to a path + * @param c pointer to a `lv_fpoint_t` variable for center of the circle + * @param rx the horizontal radius for circle + * @param ry the vertical radius for circle + */ +void lv_vector_path_append_circle(lv_vector_path_t * path, const lv_fpoint_t * c, float rx, float ry); + +/** + * Add a arc to the path + * @param path pointer to a path + * @param c pointer to a `lv_fpoint_t` variable for center of the circle + * @param radius the radius for arc + * @param start_angle the start angle for arc + * @param sweep the sweep angle for arc, could be negative + * @param pie true: draw a pie, false: draw a arc + */ +void lv_vector_path_append_arc(lv_vector_path_t * path, const lv_fpoint_t * c, float radius, float start_angle, + float sweep, bool pie); + +/** + * Add an sub path to the path + * @param path pointer to a path + * @param subpath pointer to another path which will be added + */ +void lv_vector_path_append_path(lv_vector_path_t * path, const lv_vector_path_t * subpath); + +/** + * Create a vector graphic descriptor + * @param layer pointer to a layer + * @return pointer to the created descriptor + */ +lv_vector_dsc_t * lv_vector_dsc_create(lv_layer_t * layer); + +/** + * Delete the vector graphic descriptor + * @param dsc pointer to a vector graphic descriptor + */ +void lv_vector_dsc_delete(lv_vector_dsc_t * dsc); + +/** + * Set a matrix to current transformation matrix + * @param dsc pointer to a vector graphic descriptor + * @param matrix pointer to a matrix + */ +void lv_vector_dsc_set_transform(lv_vector_dsc_t * dsc, const lv_matrix_t * matrix); + +/** + * Set blend mode for descriptor + * @param dsc pointer to a vector graphic descriptor + * @param blend the blend mode to be set in `lv_vector_blend_t` + */ +void lv_vector_dsc_set_blend_mode(lv_vector_dsc_t * dsc, lv_vector_blend_t blend); + +/** + * Set fill color for descriptor + * @param dsc pointer to a vector graphic descriptor + * @param color the color to be set in lv_color32_t format + */ +void lv_vector_dsc_set_fill_color32(lv_vector_dsc_t * dsc, lv_color32_t color); + +/** + * Set fill color for descriptor + * @param dsc pointer to a vector graphic descriptor + * @param color the color to be set in lv_color_t format + */ +void lv_vector_dsc_set_fill_color(lv_vector_dsc_t * dsc, lv_color_t color); + +/** + * Set fill opacity for descriptor + * @param dsc pointer to a vector graphic descriptor + * @param opa the opacity to be set in lv_opa_t format + */ +void lv_vector_dsc_set_fill_opa(lv_vector_dsc_t * dsc, lv_opa_t opa); + +/** + * Set fill rule for descriptor + * @param dsc pointer to a vector graphic descriptor + * @param rule the fill rule to be set in lv_vector_fill_t format + */ +void lv_vector_dsc_set_fill_rule(lv_vector_dsc_t * dsc, lv_vector_fill_t rule); + +/** + * Set fill image for descriptor + * @param dsc pointer to a vector graphic descriptor + * @param img_dsc pointer to a `lv_draw_image_dsc_t` variable + */ +void lv_vector_dsc_set_fill_image(lv_vector_dsc_t * dsc, const lv_draw_image_dsc_t * img_dsc); + +/** + * Set fill linear gradient for descriptor + * @param dsc pointer to a vector graphic descriptor + * @param x1 the x for start point + * @param y1 the y for start point + * @param x2 the x for end point + * @param y2 the y for end point + */ +void lv_vector_dsc_set_fill_linear_gradient(lv_vector_dsc_t * dsc, float x1, float y1, float x2, float y2); + +/** + + * Set fill radial gradient radius for descriptor + * @param dsc pointer to a vector graphic descriptor + * @param cx the x for center of the circle + * @param cy the y for center of the circle + * @param radius the radius for circle + */ +void lv_vector_dsc_set_fill_radial_gradient(lv_vector_dsc_t * dsc, float cx, float cy, float radius); + +/** + * Set fill radial gradient spread for descriptor + * @param dsc pointer to a vector graphic descriptor + * @param spread the gradient spread to be set in lv_vector_gradient_spread_t format + */ +void lv_vector_dsc_set_fill_gradient_spread(lv_vector_dsc_t * dsc, lv_vector_gradient_spread_t spread); + +/** + * Set fill gradient color stops for descriptor + * @param dsc pointer to a vector graphic descriptor + * @param stops an array of `lv_gradient_stop_t` variables + * @param count the number of stops in the array, range: 0..LV_GRADIENT_MAX_STOPS + */ +void lv_vector_dsc_set_fill_gradient_color_stops(lv_vector_dsc_t * dsc, const lv_gradient_stop_t * stops, + uint16_t count); + +/** + * Set a matrix to current fill transformation matrix + * @param dsc pointer to a vector graphic descriptor + * @param matrix pointer to a matrix + */ +void lv_vector_dsc_set_fill_transform(lv_vector_dsc_t * dsc, const lv_matrix_t * matrix); + +/** + * Set stroke color for descriptor + * @param dsc pointer to a vector graphic descriptor + * @param color the color to be set in lv_color32_t format + */ +void lv_vector_dsc_set_stroke_color32(lv_vector_dsc_t * dsc, lv_color32_t color); + +/** + * Set stroke color for descriptor + * @param dsc pointer to a vector graphic descriptor + * @param color the color to be set in lv_color_t format + */ +void lv_vector_dsc_set_stroke_color(lv_vector_dsc_t * dsc, lv_color_t color); + +/** + * Set stroke opacity for descriptor + * @param dsc pointer to a vector graphic descriptor + * @param opa the opacity to be set in lv_opa_t format + */ +void lv_vector_dsc_set_stroke_opa(lv_vector_dsc_t * dsc, lv_opa_t opa); + +/** + * Set stroke line width for descriptor + * @param dsc pointer to a vector graphic descriptor + * @param width the stroke line width + */ +void lv_vector_dsc_set_stroke_width(lv_vector_dsc_t * dsc, float width); + +/** + * Set stroke line dash pattern for descriptor + * @param dsc pointer to a vector graphic descriptor + * @param dash_pattern an array of values that specify the segments of dash line + * @param dash_count the length of dash pattern array + */ +void lv_vector_dsc_set_stroke_dash(lv_vector_dsc_t * dsc, float * dash_pattern, uint16_t dash_count); + +/** + * Set stroke line cap style for descriptor + * @param dsc pointer to a vector graphic descriptor + * @param cap the line cap to be set in lv_vector_stroke_cap_t format + */ +void lv_vector_dsc_set_stroke_cap(lv_vector_dsc_t * dsc, lv_vector_stroke_cap_t cap); + +/** + * Set stroke line join style for descriptor + * @param dsc pointer to a vector graphic descriptor + * @param join the line join to be set in lv_vector_stroke_join_t format + */ +void lv_vector_dsc_set_stroke_join(lv_vector_dsc_t * dsc, lv_vector_stroke_join_t join); + +/** + * Set stroke miter limit for descriptor + * @param dsc pointer to a vector graphic descriptor + * @param miter_limit the stroke miter_limit + */ +void lv_vector_dsc_set_stroke_miter_limit(lv_vector_dsc_t * dsc, uint16_t miter_limit); + +/** + * Set stroke linear gradient for descriptor + * @param dsc pointer to a vector graphic descriptor + * @param x1 the x for start point + * @param y1 the y for start point + * @param x2 the x for end point + * @param y2 the y for end point + */ +void lv_vector_dsc_set_stroke_linear_gradient(lv_vector_dsc_t * dsc, float x1, float y1, float x2, float y2); +/** + * Set stroke radial gradient for descriptor + * @param dsc pointer to a vector graphic descriptor + * @param cx the x for center of the circle + * @param cy the y for center of the circle + * @param radius the radius for circle + */ +void lv_vector_dsc_set_stroke_radial_gradient(lv_vector_dsc_t * dsc, float cx, float cy, float radius); + +/** + * Set stroke color stops for descriptor + * @param dsc pointer to a vector graphic descriptor + * @param spread the gradient spread to be set in lv_vector_gradient_spread_t format + */ +void lv_vector_dsc_set_stroke_gradient_spread(lv_vector_dsc_t * dsc, lv_vector_gradient_spread_t spread); + +/** + * Set stroke color stops for descriptor + * @param dsc pointer to a vector graphic descriptor + * @param stops an array of `lv_gradient_stop_t` variables + * @param count the number of stops in the array + */ +void lv_vector_dsc_set_stroke_gradient_color_stops(lv_vector_dsc_t * dsc, const lv_gradient_stop_t * stops, + uint16_t count); + +/** + * Set a matrix to current stroke transformation matrix + * @param dsc pointer to a vector graphic descriptor + * @param matrix pointer to a matrix + */ +void lv_vector_dsc_set_stroke_transform(lv_vector_dsc_t * dsc, const lv_matrix_t * matrix); + +/** + * Set current transformation matrix to identity matrix + * @param dsc pointer to a vector graphic descriptor + */ +void lv_vector_dsc_identity(lv_vector_dsc_t * dsc); + +/** + * Change the scale factor of current transformation matrix + * @param dsc pointer to a vector graphic descriptor + * @param scale_x the scale factor for the X direction + * @param scale_y the scale factor for the Y direction + */ +void lv_vector_dsc_scale(lv_vector_dsc_t * dsc, float scale_x, float scale_y); + +/** + * Rotate current transformation matrix with origin + * @param dsc pointer to a vector graphic descriptor + * @param degree angle to rotate + */ +void lv_vector_dsc_rotate(lv_vector_dsc_t * dsc, float degree); + +/** + * Translate current transformation matrix to new position + * @param dsc pointer to a vector graphic descriptor + * @param tx the amount of translate in x direction + * @param tx the amount of translate in y direction + */ +void lv_vector_dsc_translate(lv_vector_dsc_t * dsc, float tx, float ty); + +/** + * Change the skew factor of current transformation matrix + * @param dsc pointer to a vector graphic descriptor + * @param skew_x the skew factor for x direction + * @param skew_y the skew factor for y direction + */ +void lv_vector_dsc_skew(lv_vector_dsc_t * dsc, float skew_x, float skew_y); + +/** + * Add a graphic path to the draw list + * @param dsc pointer to a vector graphic descriptor + * @param path pointer to a path + */ +void lv_vector_dsc_add_path(lv_vector_dsc_t * dsc, const lv_vector_path_t * path); + +/** + * Clear a rectangle area use current fill color + * @param dsc pointer to a vector graphic descriptor + * @param rect the area to clear in the buffer + */ +void lv_vector_clear_area(lv_vector_dsc_t * dsc, const lv_area_t * rect); + +/** + * Draw all the vector graphic paths + * @param dsc pointer to a vector graphic descriptor + */ +void lv_draw_vector(lv_vector_dsc_t * dsc); + +/* Traverser for task list */ +typedef void (*vector_draw_task_cb)(void * ctx, const lv_vector_path_t * path, const lv_vector_draw_dsc_t * dsc); + +#endif /* LV_USE_VECTOR_GRAPHIC */ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LV_DRAW_VECTOR_H */ diff --git a/include/liblvgl/draw/lv_draw_vector_private.h b/include/liblvgl/draw/lv_draw_vector_private.h new file mode 100644 index 00000000..9257736a --- /dev/null +++ b/include/liblvgl/draw/lv_draw_vector_private.h @@ -0,0 +1,109 @@ +/** + * @file lv_draw_vector_private.h + * + */ + +#ifndef LV_DRAW_VECTOR_PRIVATE_H +#define LV_DRAW_VECTOR_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_draw_vector.h" + +#if LV_USE_VECTOR_GRAPHIC + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_vector_path_t { + lv_vector_path_quality_t quality; + lv_array_t ops; + lv_array_t points; +}; + +struct _lv_vector_gradient_t { + lv_vector_gradient_style_t style; + lv_gradient_stop_t stops[LV_GRADIENT_MAX_STOPS]; /**< A gradient stop array */ + uint16_t stops_count; /**< The number of used stops in the array */ + float x1; + float y1; + float x2; + float y2; + float cx; + float cy; + float cr; + lv_vector_gradient_spread_t spread; +}; + +struct _lv_vector_fill_dsc_t { + lv_vector_draw_style_t style; + lv_color32_t color; + lv_opa_t opa; + lv_vector_fill_t fill_rule; + lv_draw_image_dsc_t img_dsc; + lv_vector_gradient_t gradient; + lv_matrix_t matrix; +}; + +struct _lv_vector_stroke_dsc_t { + lv_vector_draw_style_t style; + lv_color32_t color; + lv_opa_t opa; + float width; + lv_array_t dash_pattern; + lv_vector_stroke_cap_t cap; + lv_vector_stroke_join_t join; + uint16_t miter_limit; + lv_vector_gradient_t gradient; + lv_matrix_t matrix; +}; + +struct _lv_vector_draw_dsc_t { + lv_vector_fill_dsc_t fill_dsc; + lv_vector_stroke_dsc_t stroke_dsc; + lv_matrix_t matrix; + lv_vector_blend_t blend_mode; + lv_area_t scissor_area; +}; + +struct _lv_draw_vector_task_dsc_t { + lv_draw_dsc_base_t base; + lv_ll_t * task_list; /*draw task list.*/ +}; + +struct _lv_vector_dsc_t { + lv_layer_t * layer; + lv_vector_draw_dsc_t current_dsc; + /* private data */ + lv_draw_vector_task_dsc_t tasks; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void lv_vector_for_each_destroy_tasks(lv_ll_t * task_list, vector_draw_task_cb cb, void * data); + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_VECTOR_GRAPHIC */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_VECTOR_PRIVATE_H*/ diff --git a/include/liblvgl/draw/lv_image_decoder.h b/include/liblvgl/draw/lv_image_decoder.h new file mode 100644 index 00000000..e898edc9 --- /dev/null +++ b/include/liblvgl/draw/lv_image_decoder.h @@ -0,0 +1,194 @@ +/** + * @file lv_image_decoder.h + * + */ + +#ifndef LV_IMAGE_DECODER_H +#define LV_IMAGE_DECODER_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../lv_conf_internal.h" + +#include "lv_draw_buf.h" +#include "../misc/lv_fs.h" +#include "../misc/lv_types.h" +#include "../misc/lv_area.h" +#include "../misc/cache/lv_cache.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** + * Source of image.*/ +typedef enum { + LV_IMAGE_SRC_VARIABLE, /** Binary/C variable*/ + LV_IMAGE_SRC_FILE, /** File in filesystem*/ + LV_IMAGE_SRC_SYMBOL, /** Symbol (@ref lv_symbol_def.h)*/ + LV_IMAGE_SRC_UNKNOWN, /** Unknown source*/ +} lv_image_src_t; + +/** + * Get info from an image and store in the `header` + * @param decoder pointer to decoder object + * @param dsc pointer to decoder descriptor + * @param header store the info here + * @return LV_RESULT_OK: info written correctly; LV_RESULT_INVALID: failed + */ +typedef lv_result_t (*lv_image_decoder_info_f_t)(lv_image_decoder_t * decoder, lv_image_decoder_dsc_t * dsc, + lv_image_header_t * header); + +/** + * Open an image for decoding. Prepare it as it is required to read it later + * @param decoder pointer to the decoder the function associated with + * @param dsc pointer to decoder descriptor. `src`, `color` are already initialized in it. + */ +typedef lv_result_t (*lv_image_decoder_open_f_t)(lv_image_decoder_t * decoder, lv_image_decoder_dsc_t * dsc); + +/** + * Decode `full_area` pixels incrementally by calling in a loop. Set `decoded_area` values to `LV_COORD_MIN` on first call. + * Required only if the "open" function can't return with the whole decoded pixel array. + * @param decoder pointer to the decoder the function associated with + * @param dsc pointer to decoder descriptor + * @param full_area input parameter. the full area to decode after enough subsequent calls + * @param decoded_area input+output parameter. set the values to `LV_COORD_MIN` for the first call and to reset decoding. + * the decoded area is stored here after each call. + * @return LV_RESULT_OK: ok; LV_RESULT_INVALID: failed or there is nothing left to decode + */ +typedef lv_result_t (*lv_image_decoder_get_area_cb_t)(lv_image_decoder_t * decoder, + lv_image_decoder_dsc_t * dsc, + const lv_area_t * full_area, lv_area_t * decoded_area); + +/** + * Close the pending decoding. Free resources etc. + * @param decoder pointer to the decoder the function associated with + * @param dsc pointer to decoder descriptor + */ +typedef void (*lv_image_decoder_close_f_t)(lv_image_decoder_t * decoder, lv_image_decoder_dsc_t * dsc); + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Get information about an image. + * Try the created image decoder one by one. Once one is able to get info that info will be used. + * @param src the image source. Can be + * 1) File name: E.g. "S:folder/img1.png" (The drivers needs to registered via `lv_fs_drv_register()`) + * 2) Variable: Pointer to an `lv_image_dsc_t` variable + * 3) Symbol: E.g. `LV_SYMBOL_OK` + * @param header the image info will be stored here + * @return LV_RESULT_OK: success; LV_RESULT_INVALID: wasn't able to get info about the image + */ +lv_result_t lv_image_decoder_get_info(const void * src, lv_image_header_t * header); + +/** + * Open an image. + * Try the created image decoders one by one. Once one is able to open the image that decoder is saved in `dsc` + * @param dsc describes a decoding session. Simply a pointer to an `lv_image_decoder_dsc_t` variable. + * @param src the image source. Can be + * 1) File name: E.g. "S:folder/img1.png" (The drivers needs to registered via `lv_fs_drv_register())`) + * 2) Variable: Pointer to an `lv_image_dsc_t` variable + * 3) Symbol: E.g. `LV_SYMBOL_OK` + * @param args args about how the image should be opened. + * @return LV_RESULT_OK: opened the image. `dsc->decoded` and `dsc->header` are set. + * LV_RESULT_INVALID: none of the registered image decoders were able to open the image. + */ +lv_result_t lv_image_decoder_open(lv_image_decoder_dsc_t * dsc, const void * src, const lv_image_decoder_args_t * args); + +/** + * Decode `full_area` pixels incrementally by calling in a loop. Set `decoded_area` to `LV_COORD_MIN` on first call. + * @param dsc image decoder descriptor + * @param full_area input parameter. the full area to decode after enough subsequent calls + * @param decoded_area input+output parameter. set the values to `LV_COORD_MIN` for the first call and to reset decoding. + * the decoded area is stored here after each call. + * @return LV_RESULT_OK: success; LV_RESULT_INVALID: an error occurred or there is nothing left to decode + */ +lv_result_t lv_image_decoder_get_area(lv_image_decoder_dsc_t * dsc, const lv_area_t * full_area, + lv_area_t * decoded_area); + +/** + * Close a decoding session + * @param dsc pointer to `lv_image_decoder_dsc_t` used in `lv_image_decoder_open` + */ +void lv_image_decoder_close(lv_image_decoder_dsc_t * dsc); + +/** + * Create a new image decoder + * @return pointer to the new image decoder + */ +lv_image_decoder_t * lv_image_decoder_create(void); + +/** + * Delete an image decoder + * @param decoder pointer to an image decoder + */ +void lv_image_decoder_delete(lv_image_decoder_t * decoder); + +/** + * Get the next image decoder in the linked list of image decoders + * @param decoder pointer to an image decoder or NULL to get the first one + * @return the next image decoder or NULL if no more image decoder exists + */ +lv_image_decoder_t * lv_image_decoder_get_next(lv_image_decoder_t * decoder); + +/** + * Set a callback to get information about the image + * @param decoder pointer to an image decoder + * @param info_cb a function to collect info about an image (fill an `lv_image_header_t` struct) + */ +void lv_image_decoder_set_info_cb(lv_image_decoder_t * decoder, lv_image_decoder_info_f_t info_cb); + +/** + * Set a callback to open an image + * @param decoder pointer to an image decoder + * @param open_cb a function to open an image + */ +void lv_image_decoder_set_open_cb(lv_image_decoder_t * decoder, lv_image_decoder_open_f_t open_cb); + +/** + * Set a callback to a decoded line of an image + * @param decoder pointer to an image decoder + * @param read_line_cb a function to read a line of an image + */ +void lv_image_decoder_set_get_area_cb(lv_image_decoder_t * decoder, lv_image_decoder_get_area_cb_t read_line_cb); + +/** + * Set a callback to close a decoding session. E.g. close files and free other resources. + * @param decoder pointer to an image decoder + * @param close_cb a function to close a decoding session + */ +void lv_image_decoder_set_close_cb(lv_image_decoder_t * decoder, lv_image_decoder_close_f_t close_cb); + +lv_cache_entry_t * lv_image_decoder_add_to_cache(lv_image_decoder_t * decoder, + lv_image_cache_data_t * search_key, + const lv_draw_buf_t * decoded, void * user_data); + +/** + * Check the decoded image, make any modification if decoder `args` requires. + * @note A new draw buf will be allocated if provided `decoded` is not modifiable or stride mismatch etc. + * @param dsc pointer to a decoder descriptor + * @param decoded pointer to a decoded image to post process to meet dsc->args requirement. + * @return post processed draw buffer, when it differs with `decoded`, it's newly allocated. + */ +lv_draw_buf_t * lv_image_decoder_post_process(lv_image_decoder_dsc_t * dsc, lv_draw_buf_t * decoded); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_IMAGE_DECODER_H*/ diff --git a/include/liblvgl/draw/lv_image_decoder_private.h b/include/liblvgl/draw/lv_image_decoder_private.h new file mode 100644 index 00000000..c5bcb0d6 --- /dev/null +++ b/include/liblvgl/draw/lv_image_decoder_private.h @@ -0,0 +1,142 @@ +/** + * @file lv_image_decoder_private.h + * + */ + +#ifndef LV_IMAGE_DECODER_PRIVATE_H +#define LV_IMAGE_DECODER_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_image_decoder.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** + * Image decoder args. + * It determines how to decoder an image, e.g. whether to premultiply the alpha or not. + * It should be passed to lv_img_decoder_open() function. If NULL is provided, default + * args are used. + * + * Default args: + * all field are zero or false. + */ +struct _lv_image_decoder_args_t { + bool stride_align; /**< Whether stride should be aligned */ + bool premultiply; /**< Whether image should be premultiplied or not after decoding */ + bool no_cache; /**< When set, decoded image won't be put to cache, and decoder open will also ignore cache. */ + bool use_indexed; /**< Decoded indexed image as is. Convert to ARGB8888 if false. */ + bool flush_cache; /**< Whether to flush the data cache after decoding */ +}; + +struct _lv_image_decoder_t { + lv_image_decoder_info_f_t info_cb; + lv_image_decoder_open_f_t open_cb; + lv_image_decoder_get_area_cb_t get_area_cb; + lv_image_decoder_close_f_t close_cb; + + const char * name; + + void * user_data; +}; + +struct _lv_image_cache_data_t { + lv_cache_slot_size_t slot; + + const void * src; + lv_image_src_t src_type; + + const lv_draw_buf_t * decoded; + const lv_image_decoder_t * decoder; + void * user_data; +}; + +struct _lv_image_header_cache_data_t { + const void * src; + lv_image_src_t src_type; + + lv_image_header_t header; + lv_image_decoder_t * decoder; +}; + +/**Describe an image decoding session. Stores data about the decoding*/ +struct _lv_image_decoder_dsc_t { + /**The decoder which was able to open the image source*/ + lv_image_decoder_t * decoder; + + /**A copy of parameters of how this image is decoded*/ + lv_image_decoder_args_t args; + + /**The image source. A file path like "S:my_img.png" or pointer to an `lv_image_dsc_t` variable*/ + const void * src; + + /**Type of the source: file or variable. Can be set in `open` function if required*/ + lv_image_src_t src_type; + + lv_fs_file_t file; + + /**Info about the opened image: color format, size, etc. MUST be set in `open` function*/ + lv_image_header_t header; + + /** Pointer to a draw buffer where the image's data (pixels) are stored in a decoded, plain format. + * MUST be set in `open` or `get_area_cb`function*/ + const lv_draw_buf_t * decoded; + + const lv_color32_t * palette; + uint32_t palette_size; + + /** How much time did it take to open the image. [ms] + * If not set `lv_image_cache` will measure and set the time to open*/ + uint32_t time_to_open; + + /**A text to display instead of the image when the image can't be opened. + * Can be set in `open` function or set NULL.*/ + const char * error_msg; + + lv_cache_t * cache; + + /**Point to cache entry information*/ + lv_cache_entry_t * cache_entry; + + /**Store any custom data here is required*/ + void * user_data; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Initialize the image decoder module + * @param image_cache_size Image cache size in bytes. 0 to disable cache. + * @param image_header_count Number of header cache entries. 0 to disable header cache. + */ +void lv_image_decoder_init(uint32_t image_cache_size, uint32_t image_header_count); + +/** + * Deinitialize the image decoder module + */ +void lv_image_decoder_deinit(void); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_IMAGE_DECODER_PRIVATE_H*/ diff --git a/include/liblvgl/draw/lv_image_dsc.h b/include/liblvgl/draw/lv_image_dsc.h new file mode 100644 index 00000000..535f72cd --- /dev/null +++ b/include/liblvgl/draw/lv_image_dsc.h @@ -0,0 +1,141 @@ +/** + * @file lv_image_dsc.h + * + */ + +#ifndef LV_IMAGE_DSC_H +#define LV_IMAGE_DSC_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../lv_conf_internal.h" + +/********************* + * DEFINES + *********************/ + +/** Magic number for lvgl image, 9 means lvgl version 9 + * It must be neither a valid ASCII character nor larger than 0x80. See `lv_image_src_get_type`. + */ +#define LV_IMAGE_HEADER_MAGIC (0x19) +LV_EXPORT_CONST_INT(LV_IMAGE_HEADER_MAGIC); + +/********************** + * TYPEDEFS + **********************/ + +typedef enum _lvimage_flags_t { + /** + * For RGB map of the image data, mark if it's pre-multiplied with alpha. + * For indexed image, this bit indicated palette data is pre-multiplied with alpha. + */ + LV_IMAGE_FLAGS_PREMULTIPLIED = 0x0001, + /** + * The image data is compressed, so decoder needs to decode image firstly. + * If this flag is set, the whole image will be decompressed upon decode, and + * `get_area_cb` won't be necessary. + */ + LV_IMAGE_FLAGS_COMPRESSED = 0x0008, + + /*Below flags are applicable only for draw buffer header.*/ + + /** + * The image is allocated from heap, thus should be freed after use. + */ + LV_IMAGE_FLAGS_ALLOCATED = 0x0010, + + /** + * If the image data is malloced and can be processed in place. + * In image decoder post processing, this flag means we modify it in-place. + */ + LV_IMAGE_FLAGS_MODIFIABLE = 0x0020, + + /** + * Flags reserved for user, lvgl won't use these bits. + */ + LV_IMAGE_FLAGS_USER1 = 0x0100, + LV_IMAGE_FLAGS_USER2 = 0x0200, + LV_IMAGE_FLAGS_USER3 = 0x0400, + LV_IMAGE_FLAGS_USER4 = 0x0800, + LV_IMAGE_FLAGS_USER5 = 0x1000, + LV_IMAGE_FLAGS_USER6 = 0x2000, + LV_IMAGE_FLAGS_USER7 = 0x4000, + LV_IMAGE_FLAGS_USER8 = 0x8000, +} lv_image_flags_t; + +typedef enum { + LV_IMAGE_COMPRESS_NONE = 0, + LV_IMAGE_COMPRESS_RLE, /**< LVGL custom RLE compression */ + LV_IMAGE_COMPRESS_LZ4, +} lv_image_compress_t; + +#if LV_BIG_ENDIAN_SYSTEM +typedef struct { + uint32_t reserved_2: 16; /**< Reserved to be used later*/ + uint32_t stride: 16; /**< Number of bytes in a row*/ + uint32_t h: 16; + uint32_t w: 16; + uint32_t flags: 16; /**< Image flags, see `lv_image_flags_t`*/ + uint32_t cf : 8; /**< Color format: See `lv_color_format_t`*/ + uint32_t magic: 8; /**< Magic number. Must be LV_IMAGE_HEADER_MAGIC*/ +} lv_image_header_t; +#else +typedef struct { + uint32_t magic: 8; /**< Magic number. Must be LV_IMAGE_HEADER_MAGIC*/ + uint32_t cf : 8; /**< Color format: See `lv_color_format_t`*/ + uint32_t flags: 16; /**< Image flags, see `lv_image_flags_t`*/ + + uint32_t w: 16; + uint32_t h: 16; + uint32_t stride: 16; /**< Number of bytes in a row*/ + uint32_t reserved_2: 16; /**< Reserved to be used later*/ +} lv_image_header_t; +#endif + +typedef struct { + void * buf; + uint32_t stride; /**< Number of bytes in a row*/ +} lv_yuv_plane_t; + +typedef union { + lv_yuv_plane_t yuv; /**< packed format*/ + struct { + lv_yuv_plane_t y; + lv_yuv_plane_t u; + lv_yuv_plane_t v; + } planar; /**< planar format with 3 plane*/ + struct { + lv_yuv_plane_t y; + lv_yuv_plane_t uv; + } semi_planar; /**< planar format with 2 plane*/ +} lv_yuv_buf_t; + +/** + * Struct to describe a constant image resource. + * It's similar to lv_draw_buf_t, but the data is constant. + */ +typedef struct { + lv_image_header_t header; /**< A header describing the basics of the image*/ + uint32_t data_size; /**< Size of the image in bytes*/ + const uint8_t * data; /**< Pointer to the data of the image*/ + const void * reserved; /**< A reserved field to make it has same size as lv_draw_buf_t*/ +} lv_image_dsc_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_IMAGE_DSC_H*/ diff --git a/include/liblvgl/draw/lv_img_buf.h b/include/liblvgl/draw/lv_img_buf.h deleted file mode 100644 index 8998839b..00000000 --- a/include/liblvgl/draw/lv_img_buf.h +++ /dev/null @@ -1,249 +0,0 @@ -/** - * @file lv_img_buf.h - * - */ - -#ifndef LV_IMG_BUF_H -#define LV_IMG_BUF_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include -#include "liblvgl/misc/lv_color.h" -#include "liblvgl/misc/lv_area.h" - -/********************* - * DEFINES - *********************/ -/*If image pixels contains alpha we need to know how much byte is a pixel*/ -#if LV_COLOR_DEPTH == 1 || LV_COLOR_DEPTH == 8 -#define LV_IMG_PX_SIZE_ALPHA_BYTE 2 -#elif LV_COLOR_DEPTH == 16 -#define LV_IMG_PX_SIZE_ALPHA_BYTE 3 -#elif LV_COLOR_DEPTH == 32 -#define LV_IMG_PX_SIZE_ALPHA_BYTE 4 -#endif - -#define LV_IMG_BUF_SIZE_TRUE_COLOR(w, h) ((LV_COLOR_SIZE / 8) * w * h) -#define LV_IMG_BUF_SIZE_TRUE_COLOR_CHROMA_KEYED(w, h) ((LV_COLOR_SIZE / 8) * w * h) -#define LV_IMG_BUF_SIZE_TRUE_COLOR_ALPHA(w, h) (LV_IMG_PX_SIZE_ALPHA_BYTE * w * h) - -/*+ 1: to be sure no fractional row*/ -#define LV_IMG_BUF_SIZE_ALPHA_1BIT(w, h) ((((w / 8) + 1) * h)) -#define LV_IMG_BUF_SIZE_ALPHA_2BIT(w, h) ((((w / 4) + 1) * h)) -#define LV_IMG_BUF_SIZE_ALPHA_4BIT(w, h) ((((w / 2) + 1) * h)) -#define LV_IMG_BUF_SIZE_ALPHA_8BIT(w, h) ((w * h)) - -/*4 * X: for palette*/ -#define LV_IMG_BUF_SIZE_INDEXED_1BIT(w, h) (LV_IMG_BUF_SIZE_ALPHA_1BIT(w, h) + 4 * 2) -#define LV_IMG_BUF_SIZE_INDEXED_2BIT(w, h) (LV_IMG_BUF_SIZE_ALPHA_2BIT(w, h) + 4 * 4) -#define LV_IMG_BUF_SIZE_INDEXED_4BIT(w, h) (LV_IMG_BUF_SIZE_ALPHA_4BIT(w, h) + 4 * 16) -#define LV_IMG_BUF_SIZE_INDEXED_8BIT(w, h) (LV_IMG_BUF_SIZE_ALPHA_8BIT(w, h) + 4 * 256) - -#define _LV_ZOOM_INV_UPSCALE 5 - -/********************** - * TYPEDEFS - **********************/ - -/*Image color format*/ -enum { - LV_IMG_CF_UNKNOWN = 0, - - LV_IMG_CF_RAW, /**< Contains the file as it is. Needs custom decoder function*/ - LV_IMG_CF_RAW_ALPHA, /**< Contains the file as it is. The image has alpha. Needs custom decoder - function*/ - LV_IMG_CF_RAW_CHROMA_KEYED, /**< Contains the file as it is. The image is chroma keyed. Needs - custom decoder function*/ - - LV_IMG_CF_TRUE_COLOR, /**< Color format and depth should match with LV_COLOR settings*/ - LV_IMG_CF_TRUE_COLOR_ALPHA, /**< Same as `LV_IMG_CF_TRUE_COLOR` but every pixel has an alpha byte*/ - LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED, /**< Same as `LV_IMG_CF_TRUE_COLOR` but LV_COLOR_TRANSP pixels - will be transparent*/ - - LV_IMG_CF_INDEXED_1BIT, /**< Can have 2 different colors in a palette (can't be chroma keyed)*/ - LV_IMG_CF_INDEXED_2BIT, /**< Can have 4 different colors in a palette (can't be chroma keyed)*/ - LV_IMG_CF_INDEXED_4BIT, /**< Can have 16 different colors in a palette (can't be chroma keyed)*/ - LV_IMG_CF_INDEXED_8BIT, /**< Can have 256 different colors in a palette (can't be chroma keyed)*/ - - LV_IMG_CF_ALPHA_1BIT, /**< Can have one color and it can be drawn or not*/ - LV_IMG_CF_ALPHA_2BIT, /**< Can have one color but 4 different alpha value*/ - LV_IMG_CF_ALPHA_4BIT, /**< Can have one color but 16 different alpha value*/ - LV_IMG_CF_ALPHA_8BIT, /**< Can have one color but 256 different alpha value*/ - - LV_IMG_CF_RGB888, - LV_IMG_CF_RGBA8888, - LV_IMG_CF_RGBX8888, - LV_IMG_CF_RGB565, - LV_IMG_CF_RGBA5658, - LV_IMG_CF_RGB565A8, - - LV_IMG_CF_RESERVED_15, /**< Reserved for further use.*/ - LV_IMG_CF_RESERVED_16, /**< Reserved for further use.*/ - LV_IMG_CF_RESERVED_17, /**< Reserved for further use.*/ - LV_IMG_CF_RESERVED_18, /**< Reserved for further use.*/ - LV_IMG_CF_RESERVED_19, /**< Reserved for further use.*/ - LV_IMG_CF_RESERVED_20, /**< Reserved for further use.*/ - LV_IMG_CF_RESERVED_21, /**< Reserved for further use.*/ - LV_IMG_CF_RESERVED_22, /**< Reserved for further use.*/ - LV_IMG_CF_RESERVED_23, /**< Reserved for further use.*/ - - LV_IMG_CF_USER_ENCODED_0, /**< User holder encoding format.*/ - LV_IMG_CF_USER_ENCODED_1, /**< User holder encoding format.*/ - LV_IMG_CF_USER_ENCODED_2, /**< User holder encoding format.*/ - LV_IMG_CF_USER_ENCODED_3, /**< User holder encoding format.*/ - LV_IMG_CF_USER_ENCODED_4, /**< User holder encoding format.*/ - LV_IMG_CF_USER_ENCODED_5, /**< User holder encoding format.*/ - LV_IMG_CF_USER_ENCODED_6, /**< User holder encoding format.*/ - LV_IMG_CF_USER_ENCODED_7, /**< User holder encoding format.*/ -}; -typedef uint8_t lv_img_cf_t; - - -/** - * The first 8 bit is very important to distinguish the different source types. - * For more info see `lv_img_get_src_type()` in lv_img.c - * On big endian systems the order is reversed so cf and always_zero must be at - * the end of the struct. - */ -#if LV_BIG_ENDIAN_SYSTEM -typedef struct { - - uint32_t h : 11; /*Height of the image map*/ - uint32_t w : 11; /*Width of the image map*/ - uint32_t reserved : 2; /*Reserved to be used later*/ - uint32_t always_zero : 3; /*It the upper bits of the first byte. Always zero to look like a - non-printable character*/ - uint32_t cf : 5; /*Color format: See `lv_img_color_format_t`*/ - -} lv_img_header_t; -#else -typedef struct { - - uint32_t cf : 5; /*Color format: See `lv_img_color_format_t`*/ - uint32_t always_zero : 3; /*It the upper bits of the first byte. Always zero to look like a - non-printable character*/ - - uint32_t reserved : 2; /*Reserved to be used later*/ - - uint32_t w : 11; /*Width of the image map*/ - uint32_t h : 11; /*Height of the image map*/ -} lv_img_header_t; -#endif - -/** Image header it is compatible with - * the result from image converter utility*/ -typedef struct { - lv_img_header_t header; /**< A header describing the basics of the image*/ - uint32_t data_size; /**< Size of the image in bytes*/ - const uint8_t * data; /**< Pointer to the data of the image*/ -} lv_img_dsc_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Allocate an image buffer in RAM - * @param w width of image - * @param h height of image - * @param cf a color format (`LV_IMG_CF_...`) - * @return an allocated image, or NULL on failure - */ -lv_img_dsc_t * lv_img_buf_alloc(lv_coord_t w, lv_coord_t h, lv_img_cf_t cf); - -/** - * Get the color of an image's pixel - * @param dsc an image descriptor - * @param x x coordinate of the point to get - * @param y x coordinate of the point to get - * @param color the color of the image. In case of `LV_IMG_CF_ALPHA_1/2/4/8` this color is used. - * Not used in other cases. - * @param safe true: check out of bounds - * @return color of the point - */ -lv_color_t lv_img_buf_get_px_color(lv_img_dsc_t * dsc, lv_coord_t x, lv_coord_t y, lv_color_t color); - -/** - * Get the alpha value of an image's pixel - * @param dsc pointer to an image descriptor - * @param x x coordinate of the point to set - * @param y x coordinate of the point to set - * @param safe true: check out of bounds - * @return alpha value of the point - */ -lv_opa_t lv_img_buf_get_px_alpha(lv_img_dsc_t * dsc, lv_coord_t x, lv_coord_t y); - -/** - * Set the color of a pixel of an image. The alpha channel won't be affected. - * @param dsc pointer to an image descriptor - * @param x x coordinate of the point to set - * @param y x coordinate of the point to set - * @param c color of the point - * @param safe true: check out of bounds - */ -void lv_img_buf_set_px_color(lv_img_dsc_t * dsc, lv_coord_t x, lv_coord_t y, lv_color_t c); - -/** - * Set the alpha value of a pixel of an image. The color won't be affected - * @param dsc pointer to an image descriptor - * @param x x coordinate of the point to set - * @param y x coordinate of the point to set - * @param opa the desired opacity - * @param safe true: check out of bounds - */ -void lv_img_buf_set_px_alpha(lv_img_dsc_t * dsc, lv_coord_t x, lv_coord_t y, lv_opa_t opa); - -/** - * Set the palette color of an indexed image. Valid only for `LV_IMG_CF_INDEXED1/2/4/8` - * @param dsc pointer to an image descriptor - * @param id the palette color to set: - * - for `LV_IMG_CF_INDEXED1`: 0..1 - * - for `LV_IMG_CF_INDEXED2`: 0..3 - * - for `LV_IMG_CF_INDEXED4`: 0..15 - * - for `LV_IMG_CF_INDEXED8`: 0..255 - * @param c the color to set - */ -void lv_img_buf_set_palette(lv_img_dsc_t * dsc, uint8_t id, lv_color_t c); - -/** - * Free an allocated image buffer - * @param dsc image buffer to free - */ -void lv_img_buf_free(lv_img_dsc_t * dsc); - -/** - * Get the memory consumption of a raw bitmap, given color format and dimensions. - * @param w width - * @param h height - * @param cf color format - * @return size in bytes - */ -uint32_t lv_img_buf_get_img_size(lv_coord_t w, lv_coord_t h, lv_img_cf_t cf); - -/** - * Get the area of a rectangle if its rotated and scaled - * @param res store the coordinates here - * @param w width of the rectangle to transform - * @param h height of the rectangle to transform - * @param angle angle of rotation - * @param zoom zoom, (256 no zoom) - * @param pivot x,y pivot coordinates of rotation - */ -void _lv_img_buf_get_transformed_area(lv_area_t * res, lv_coord_t w, lv_coord_t h, int16_t angle, uint16_t zoom, - const lv_point_t * pivot); - -/********************** - * MACROS - **********************/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_IMG_BUF_H*/ diff --git a/include/liblvgl/draw/lv_img_cache.h b/include/liblvgl/draw/lv_img_cache.h deleted file mode 100644 index dc0c5d98..00000000 --- a/include/liblvgl/draw/lv_img_cache.h +++ /dev/null @@ -1,78 +0,0 @@ -/** - * @file lv_img_cache.h - * - */ - -#ifndef LV_IMG_CACHE_H -#define LV_IMG_CACHE_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "lv_img_decoder.h" - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -/** - * When loading images from the network it can take a long time to download and decode the image. - * - * To avoid repeating this heavy load images can be cached. - */ -typedef struct { - lv_img_decoder_dsc_t dec_dsc; /**< Image information*/ - - /** Count the cache entries's life. Add `time_to_open` to `life` when the entry is used. - * Decrement all lifes by one every in every ::lv_img_cache_open. - * If life == 0 the entry can be reused*/ - int32_t life; -} _lv_img_cache_entry_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Open an image using the image decoder interface and cache it. - * The image will be left open meaning if the image decoder open callback allocated memory then it will remain. - * The image is closed if a new image is opened and the new image takes its place in the cache. - * @param src source of the image. Path to file or pointer to an `lv_img_dsc_t` variable - * @param color The color of the image with `LV_IMG_CF_ALPHA_...` - * @param frame_id the index of the frame. Used only with animated images, set 0 for normal images - * @return pointer to the cache entry or NULL if can open the image - */ -_lv_img_cache_entry_t * _lv_img_cache_open(const void * src, lv_color_t color, int32_t frame_id); - -/** - * Set the number of images to be cached. - * More cached images mean more opened image at same time which might mean more memory usage. - * E.g. if 20 PNG or JPG images are open in the RAM they consume memory while opened in the cache. - * @param new_entry_cnt number of image to cache - */ -void lv_img_cache_set_size(uint16_t new_slot_num); - -/** - * Invalidate an image source in the cache. - * Useful if the image source is updated therefore it needs to be cached again. - * @param src an image source path to a file or pointer to an `lv_img_dsc_t` variable. - */ -void lv_img_cache_invalidate_src(const void * src); - -/********************** - * MACROS - **********************/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_IMG_CACHE_H*/ diff --git a/include/liblvgl/draw/lv_img_decoder.h b/include/liblvgl/draw/lv_img_decoder.h deleted file mode 100644 index c0c5860e..00000000 --- a/include/liblvgl/draw/lv_img_decoder.h +++ /dev/null @@ -1,274 +0,0 @@ -/** - * @file lv_img_decoder.h - * - */ - -#ifndef LV_IMG_DECODER_H -#define LV_IMG_DECODER_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lv_conf_internal.h" - -#include -#include "lv_img_buf.h" -#include "liblvgl/misc/lv_fs.h" -#include "liblvgl/misc/lv_types.h" -#include "liblvgl/misc/lv_area.h" - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -/** - * Source of image.*/ -enum { - LV_IMG_SRC_VARIABLE, /** Binary/C variable*/ - LV_IMG_SRC_FILE, /** File in filesystem*/ - LV_IMG_SRC_SYMBOL, /** Symbol (@ref lv_symbol_def.h)*/ - LV_IMG_SRC_UNKNOWN, /** Unknown source*/ -}; - -typedef uint8_t lv_img_src_t; - -/*Decoder function definitions*/ -struct _lv_img_decoder_dsc_t; -struct _lv_img_decoder_t; - -/** - * Get info from an image and store in the `header` - * @param src the image source. Can be a pointer to a C array or a file name (Use - * `lv_img_src_get_type` to determine the type) - * @param header store the info here - * @return LV_RES_OK: info written correctly; LV_RES_INV: failed - */ -typedef lv_res_t (*lv_img_decoder_info_f_t)(struct _lv_img_decoder_t * decoder, const void * src, - lv_img_header_t * header); - -/** - * Open an image for decoding. Prepare it as it is required to read it later - * @param decoder pointer to the decoder the function associated with - * @param dsc pointer to decoder descriptor. `src`, `color` are already initialized in it. - */ -typedef lv_res_t (*lv_img_decoder_open_f_t)(struct _lv_img_decoder_t * decoder, struct _lv_img_decoder_dsc_t * dsc); - -/** - * Decode `len` pixels starting from the given `x`, `y` coordinates and store them in `buf`. - * Required only if the "open" function can't return with the whole decoded pixel array. - * @param decoder pointer to the decoder the function associated with - * @param dsc pointer to decoder descriptor - * @param x start x coordinate - * @param y start y coordinate - * @param len number of pixels to decode - * @param buf a buffer to store the decoded pixels - * @return LV_RES_OK: ok; LV_RES_INV: failed - */ -typedef lv_res_t (*lv_img_decoder_read_line_f_t)(struct _lv_img_decoder_t * decoder, struct _lv_img_decoder_dsc_t * dsc, - lv_coord_t x, lv_coord_t y, lv_coord_t len, uint8_t * buf); - -/** - * Close the pending decoding. Free resources etc. - * @param decoder pointer to the decoder the function associated with - * @param dsc pointer to decoder descriptor - */ -typedef void (*lv_img_decoder_close_f_t)(struct _lv_img_decoder_t * decoder, struct _lv_img_decoder_dsc_t * dsc); - - -typedef struct _lv_img_decoder_t { - lv_img_decoder_info_f_t info_cb; - lv_img_decoder_open_f_t open_cb; - lv_img_decoder_read_line_f_t read_line_cb; - lv_img_decoder_close_f_t close_cb; - -#if LV_USE_USER_DATA - void * user_data; -#endif -} lv_img_decoder_t; - - -/**Describe an image decoding session. Stores data about the decoding*/ -typedef struct _lv_img_decoder_dsc_t { - /**The decoder which was able to open the image source*/ - lv_img_decoder_t * decoder; - - /**The image source. A file path like "S:my_img.png" or pointer to an `lv_img_dsc_t` variable*/ - const void * src; - - /**Color to draw the image. USed when the image has alpha channel only*/ - lv_color_t color; - - /**Frame of the image, using with animated images*/ - int32_t frame_id; - - /**Type of the source: file or variable. Can be set in `open` function if required*/ - lv_img_src_t src_type; - - /**Info about the opened image: color format, size, etc. MUST be set in `open` function*/ - lv_img_header_t header; - - /** Pointer to a buffer where the image's data (pixels) are stored in a decoded, plain format. - * MUST be set in `open` function*/ - const uint8_t * img_data; - - /** How much time did it take to open the image. [ms] - * If not set `lv_img_cache` will measure and set the time to open*/ - uint32_t time_to_open; - - /**A text to display instead of the image when the image can't be opened. - * Can be set in `open` function or set NULL.*/ - const char * error_msg; - - /**Store any custom data here is required*/ - void * user_data; -} lv_img_decoder_dsc_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Initialize the image decoder module - */ -void _lv_img_decoder_init(void); - -/** - * Get information about an image. - * Try the created image decoder one by one. Once one is able to get info that info will be used. - * @param src the image source. Can be - * 1) File name: E.g. "S:folder/img1.png" (The drivers needs to registered via `lv_fs_drv_register()`) - * 2) Variable: Pointer to an `lv_img_dsc_t` variable - * 3) Symbol: E.g. `LV_SYMBOL_OK` - * @param header the image info will be stored here - * @return LV_RES_OK: success; LV_RES_INV: wasn't able to get info about the image - */ -lv_res_t lv_img_decoder_get_info(const void * src, lv_img_header_t * header); - -/** - * Open an image. - * Try the created image decoders one by one. Once one is able to open the image that decoder is saved in `dsc` - * @param dsc describes a decoding session. Simply a pointer to an `lv_img_decoder_dsc_t` variable. - * @param src the image source. Can be - * 1) File name: E.g. "S:folder/img1.png" (The drivers needs to registered via `lv_fs_drv_register())`) - * 2) Variable: Pointer to an `lv_img_dsc_t` variable - * 3) Symbol: E.g. `LV_SYMBOL_OK` - * @param color The color of the image with `LV_IMG_CF_ALPHA_...` - * @param frame_id the index of the frame. Used only with animated images, set 0 for normal images - * @return LV_RES_OK: opened the image. `dsc->img_data` and `dsc->header` are set. - * LV_RES_INV: none of the registered image decoders were able to open the image. - */ -lv_res_t lv_img_decoder_open(lv_img_decoder_dsc_t * dsc, const void * src, lv_color_t color, int32_t frame_id); - -/** - * Read a line from an opened image - * @param dsc pointer to `lv_img_decoder_dsc_t` used in `lv_img_decoder_open` - * @param x start X coordinate (from left) - * @param y start Y coordinate (from top) - * @param len number of pixels to read - * @param buf store the data here - * @return LV_RES_OK: success; LV_RES_INV: an error occurred - */ -lv_res_t lv_img_decoder_read_line(lv_img_decoder_dsc_t * dsc, lv_coord_t x, lv_coord_t y, lv_coord_t len, - uint8_t * buf); - -/** - * Close a decoding session - * @param dsc pointer to `lv_img_decoder_dsc_t` used in `lv_img_decoder_open` - */ -void lv_img_decoder_close(lv_img_decoder_dsc_t * dsc); - -/** - * Create a new image decoder - * @return pointer to the new image decoder - */ -lv_img_decoder_t * lv_img_decoder_create(void); - -/** - * Delete an image decoder - * @param decoder pointer to an image decoder - */ -void lv_img_decoder_delete(lv_img_decoder_t * decoder); - -/** - * Set a callback to get information about the image - * @param decoder pointer to an image decoder - * @param info_cb a function to collect info about an image (fill an `lv_img_header_t` struct) - */ -void lv_img_decoder_set_info_cb(lv_img_decoder_t * decoder, lv_img_decoder_info_f_t info_cb); - -/** - * Set a callback to open an image - * @param decoder pointer to an image decoder - * @param open_cb a function to open an image - */ -void lv_img_decoder_set_open_cb(lv_img_decoder_t * decoder, lv_img_decoder_open_f_t open_cb); - -/** - * Set a callback to a decoded line of an image - * @param decoder pointer to an image decoder - * @param read_line_cb a function to read a line of an image - */ -void lv_img_decoder_set_read_line_cb(lv_img_decoder_t * decoder, lv_img_decoder_read_line_f_t read_line_cb); - -/** - * Set a callback to close a decoding session. E.g. close files and free other resources. - * @param decoder pointer to an image decoder - * @param close_cb a function to close a decoding session - */ -void lv_img_decoder_set_close_cb(lv_img_decoder_t * decoder, lv_img_decoder_close_f_t close_cb); - -/** - * Get info about a built-in image - * @param decoder the decoder where this function belongs - * @param src the image source: pointer to an `lv_img_dsc_t` variable, a file path or a symbol - * @param header store the image data here - * @return LV_RES_OK: the info is successfully stored in `header`; LV_RES_INV: unknown format or other error. - */ -lv_res_t lv_img_decoder_built_in_info(lv_img_decoder_t * decoder, const void * src, lv_img_header_t * header); - -/** - * Open a built in image - * @param decoder the decoder where this function belongs - * @param dsc pointer to decoder descriptor. `src`, `style` are already initialized in it. - * @return LV_RES_OK: the info is successfully stored in `header`; LV_RES_INV: unknown format or other error. - */ -lv_res_t lv_img_decoder_built_in_open(lv_img_decoder_t * decoder, lv_img_decoder_dsc_t * dsc); - -/** - * Decode `len` pixels starting from the given `x`, `y` coordinates and store them in `buf`. - * Required only if the "open" function can't return with the whole decoded pixel array. - * @param decoder pointer to the decoder the function associated with - * @param dsc pointer to decoder descriptor - * @param x start x coordinate - * @param y start y coordinate - * @param len number of pixels to decode - * @param buf a buffer to store the decoded pixels - * @return LV_RES_OK: ok; LV_RES_INV: failed - */ -lv_res_t lv_img_decoder_built_in_read_line(lv_img_decoder_t * decoder, lv_img_decoder_dsc_t * dsc, lv_coord_t x, - lv_coord_t y, lv_coord_t len, uint8_t * buf); - -/** - * Close the pending decoding. Free resources etc. - * @param decoder pointer to the decoder the function associated with - * @param dsc pointer to decoder descriptor - */ -void lv_img_decoder_built_in_close(lv_img_decoder_t * decoder, lv_img_decoder_dsc_t * dsc); - -/********************** - * MACROS - **********************/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_IMG_DECODER_H*/ diff --git a/include/liblvgl/draw/nema_gfx/lv_draw_nema_gfx.h b/include/liblvgl/draw/nema_gfx/lv_draw_nema_gfx.h new file mode 100644 index 00000000..e455bb77 --- /dev/null +++ b/include/liblvgl/draw/nema_gfx/lv_draw_nema_gfx.h @@ -0,0 +1,122 @@ +/** + * MIT License + * + * ----------------------------------------------------------------------------- + * Copyright (c) 2008-24 Think Silicon Single Member PC + * ----------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next paragraph) + * shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE + * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ + +/** + * @file lv_draw_nema_gfx.h + * + */ + +#ifndef LV_DRAW_NEMA_GFX_H +#define LV_DRAW_NEMA_GFX_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../lv_conf_internal.h" + +#if LV_USE_NEMA_GFX + +#include "lv_draw_nema_gfx_utils.h" + +#include "../lv_draw_private.h" +#include "../lv_draw_buf_private.h" +#include "../lv_draw_image_private.h" +#include "../lv_image_decoder_private.h" +#include "../lv_draw_label_private.h" +#include "../lv_draw_mask_private.h" +#include "../lv_draw_rect_private.h" +#include "../lv_draw_triangle_private.h" +#include "../lv_draw_vector_private.h" + +#include "../../misc/lv_area_private.h" + +/********************** + * TYPEDEFS + **********************/ +typedef struct { + lv_draw_unit_t base_unit; + lv_draw_task_t * task_act; +#if LV_USE_OS + lv_thread_sync_t sync; + lv_thread_t thread; + volatile bool inited; + volatile bool exit_status; +#endif + uint32_t idx; + nema_cmdlist_t cl; +#if LV_USE_NEMA_VG + NEMA_VG_PAINT_HANDLE paint; + NEMA_VG_GRAD_HANDLE gradient; + NEMA_VG_PATH_HANDLE path; +#endif +} lv_draw_nema_gfx_unit_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void lv_draw_nema_gfx_init(void); + +void lv_draw_nema_gfx_deinit(void); + +void lv_draw_nema_gfx_fill(lv_draw_unit_t * draw_unit, + const lv_draw_fill_dsc_t * dsc, const lv_area_t * coords); + +void lv_draw_nema_gfx_triangle(lv_draw_unit_t * draw_unit, const lv_draw_triangle_dsc_t * dsc); + +void lv_draw_nema_gfx_img(lv_draw_unit_t * draw_unit, const lv_draw_image_dsc_t * dsc, + const lv_area_t * coords); + +void lv_draw_nema_gfx_label(lv_draw_unit_t * draw_unit, const lv_draw_label_dsc_t * dsc, + const lv_area_t * coords); + +void lv_draw_nema_gfx_layer(lv_draw_unit_t * draw_unit, const lv_draw_image_dsc_t * draw_dsc, + const lv_area_t * coords); + +void lv_draw_nema_gfx_line(lv_draw_unit_t * draw_unit, const lv_draw_line_dsc_t * dsc); + +void lv_draw_nema_gfx_border(lv_draw_unit_t * draw_unit, const lv_draw_border_dsc_t * dsc, + const lv_area_t * coords); + +void lv_draw_nema_gfx_arc(lv_draw_unit_t * draw_unit, const lv_draw_arc_dsc_t * dsc, + const lv_area_t * coords); + + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_NEMA_GFX*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_NEMA_GFX_H*/ \ No newline at end of file diff --git a/include/liblvgl/draw/nema_gfx/lv_draw_nema_gfx_utils.h b/include/liblvgl/draw/nema_gfx/lv_draw_nema_gfx_utils.h new file mode 100644 index 00000000..f77807a1 --- /dev/null +++ b/include/liblvgl/draw/nema_gfx/lv_draw_nema_gfx_utils.h @@ -0,0 +1,125 @@ +/** + * MIT License + * + * ----------------------------------------------------------------------------- + * Copyright (c) 2008-24 Think Silicon Single Member PC + * ----------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next paragraph) + * shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE + * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ + +/** + * @file lv_draw_nema_gfx.h + * + */ + +#ifndef LV_DRAW_NEMA_GFX_UTILS_H +#define LV_DRAW_NEMA_GFX_UTILS_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../lv_conf_internal.h" + +#if LV_USE_NEMA_GFX +#include "../sw/lv_draw_sw.h" + +#include "nema_core.h" +#include "nema_utils.h" +#include "nema_error.h" +#include "nema_provisional.h" +#include "nema_vg.h" + +/********************* + * DEFINES + *********************/ + +#ifndef NEMA_VIRT2PHYS +#define NEMA_VIRT2PHYS +#else +uintptr_t NEMA_VIRT2PHYS(void * addr); +#endif + +/*Color depth: 1 (1 byte per pixel), 8 (RGB332), 16 (RGB565), 32 (ARGB8888)*/ +#if LV_COLOR_DEPTH == 8 +#define LV_NEMA_GFX_COLOR_FORMAT NEMA_L8 +#define LV_NEMA_GFX_FORMAT_MULTIPLIER 1 +#elif LV_COLOR_DEPTH == 16 +#define LV_NEMA_GFX_COLOR_FORMAT NEMA_RGB565 +#define LV_NEMA_GFX_FORMAT_MULTIPLIER 2 +#elif LV_COLOR_DEPTH == 24 +#define LV_NEMA_GFX_COLOR_FORMAT NEMA_BGR24 +#define LV_NEMA_GFX_FORMAT_MULTIPLIER 3 +#elif LV_COLOR_DEPTH == 32 +#define LV_NEMA_GFX_COLOR_FORMAT NEMA_BGRA8888 +#define LV_NEMA_GFX_FORMAT_MULTIPLIER 4 +#else +/*Can't use GPU with other formats*/ +#error Selected Color Depth Not Supported +#endif + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Convert a `lv_color_format_t` to a Nema color format. + * @param cf The LVGL color format + * @return The Nema color format + */ +uint32_t lv_nemagfx_cf_to_nema(lv_color_format_t cf); + +/** + * Get NemaGFX blending mode + * + * @param[in] lv_blend_mode The LVGL blend mode + * + * @return NemaGFX blending mode + * + */ +uint32_t lv_nemagfx_blending_mode(lv_blend_mode_t lv_blend_mode); + + +/** + * Get NemaGFX blending mode + * + * @param[in] gradient NemaGFX Gradient Buffer + * + * @param[in] lv_grad Gradient descripto + * + * @param[in] opa Descriptor's opacity + * +*/ +void lv_nemagfx_grad_set(NEMA_VG_GRAD_HANDLE gradient, lv_grad_dsc_t lv_grad, lv_opa_t opa); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_NEMA_GFX*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_NEMA_GFX_UTILS_H*/ \ No newline at end of file diff --git a/include/liblvgl/draw/nxp/lv_gpu_nxp.h b/include/liblvgl/draw/nxp/lv_gpu_nxp.h deleted file mode 100644 index 853da566..00000000 --- a/include/liblvgl/draw/nxp/lv_gpu_nxp.h +++ /dev/null @@ -1,71 +0,0 @@ -/** - * @file lv_gpu_nxp.h - * - */ - -/** - * MIT License - * - * Copyright 2022 NXP - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of - * the Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice (including the next paragraph) - * shall be included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, - * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A - * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF - * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE - * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - */ - -#ifndef LV_GPU_NXP_H -#define LV_GPU_NXP_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ - -#include "liblvgl/lv_conf_internal.h" -#if LV_USE_GPU_NXP_PXP || LV_USE_GPU_NXP_VG_LITE -#include "../sw/lv_draw_sw.h" - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ -typedef lv_draw_sw_ctx_t lv_draw_nxp_ctx_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -void lv_draw_nxp_ctx_init(struct _lv_disp_drv_t * drv, lv_draw_ctx_t * draw_ctx); - -void lv_draw_nxp_ctx_deinit(struct _lv_disp_drv_t * drv, lv_draw_ctx_t * draw_ctx); - -/********************** - * MACROS - **********************/ -#endif /*LV_USE_GPU_NXP_PXP || LV_USE_GPU_NXP_VG_LITE*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_GPU_NXP_H*/ diff --git a/include/liblvgl/draw/nxp/pxp/lv_draw_pxp.h b/include/liblvgl/draw/nxp/pxp/lv_draw_pxp.h new file mode 100644 index 00000000..d34ad3f8 --- /dev/null +++ b/include/liblvgl/draw/nxp/pxp/lv_draw_pxp.h @@ -0,0 +1,75 @@ +/** + * @file lv_draw_pxp.h + * + */ + +/** + * Copyright 2022-2024 NXP + * + * SPDX-License-Identifier: MIT + */ + +#ifndef LV_DRAW_PXP_H +#define LV_DRAW_PXP_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../../lv_conf_internal.h" + +#if LV_USE_PXP +#if LV_USE_DRAW_PXP || LV_USE_ROTATE_PXP +#include "../../sw/lv_draw_sw_private.h" +#include "../../../misc/lv_area_private.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef lv_draw_sw_unit_t lv_draw_pxp_unit_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void lv_draw_pxp_init(void); + +void lv_draw_pxp_deinit(void); + +void lv_draw_pxp_rotate(const void * src_buf, void * dest_buf, int32_t src_width, int32_t src_height, + int32_t src_stride, int32_t dest_stride, lv_display_rotation_t rotation, + lv_color_format_t cf); + +#if LV_USE_DRAW_PXP +void lv_draw_buf_pxp_init_handlers(void); + +void lv_draw_pxp_fill(lv_draw_unit_t * draw_unit, const lv_draw_fill_dsc_t * dsc, + const lv_area_t * coords); + +void lv_draw_pxp_img(lv_draw_unit_t * draw_unit, const lv_draw_image_dsc_t * dsc, + const lv_area_t * coords); + +void lv_draw_pxp_layer(lv_draw_unit_t * draw_unit, const lv_draw_image_dsc_t * draw_dsc, + const lv_area_t * coords); + +/********************** + * MACROS + **********************/ +#endif /*LV_USE_DRAW_PXP*/ +#endif /*LV_USE_DRAW_PXP || LV_USE_ROTATE_PXP*/ +#endif /*LV_USE_PXP*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_PXP_H*/ diff --git a/include/liblvgl/draw/nxp/pxp/lv_draw_pxp_blend.h b/include/liblvgl/draw/nxp/pxp/lv_draw_pxp_blend.h deleted file mode 100644 index acce8713..00000000 --- a/include/liblvgl/draw/nxp/pxp/lv_draw_pxp_blend.h +++ /dev/null @@ -1,143 +0,0 @@ -/** - * @file lv_draw_pxp_blend.h - * - */ - -/** - * MIT License - * - * Copyright 2020-2022 NXP - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of - * the Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice (including the next paragraph) - * shall be included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, - * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A - * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF - * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE - * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - */ - -#ifndef LV_DRAW_PXP_BLEND_H -#define LV_DRAW_PXP_BLEND_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ - -#include "liblvgl/lv_conf_internal.h" - -#if LV_USE_GPU_NXP_PXP -#include "lv_gpu_nxp_pxp.h" -#include "../../sw/lv_draw_sw.h" - -/********************* - * DEFINES - *********************/ - -#ifndef LV_GPU_NXP_PXP_BLIT_SIZE_LIMIT -/** Minimum area (in pixels) for image copy with 100% opacity to be handled by PXP*/ -#define LV_GPU_NXP_PXP_BLIT_SIZE_LIMIT 5000 -#endif - -#ifndef LV_GPU_NXP_PXP_BLIT_OPA_SIZE_LIMIT -/** Minimum area (in pixels) for image copy with transparency to be handled by PXP*/ -#define LV_GPU_NXP_PXP_BLIT_OPA_SIZE_LIMIT 5000 -#endif - -#ifndef LV_GPU_NXP_PXP_BUFF_SYNC_BLIT_SIZE_LIMIT -/** Minimum invalidated area (in pixels) to be synchronized by PXP during buffer sync */ -#define LV_GPU_NXP_PXP_BUFF_SYNC_BLIT_SIZE_LIMIT 5000 -#endif - -#ifndef LV_GPU_NXP_PXP_FILL_SIZE_LIMIT -/** Minimum area (in pixels) to be filled by PXP with 100% opacity*/ -#define LV_GPU_NXP_PXP_FILL_SIZE_LIMIT 5000 -#endif - -#ifndef LV_GPU_NXP_PXP_FILL_OPA_SIZE_LIMIT -/** Minimum area (in pixels) to be filled by PXP with transparency*/ -#define LV_GPU_NXP_PXP_FILL_OPA_SIZE_LIMIT 5000 -#endif - -/********************** - * TYPEDEFS - **********************/ - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Fill area, with optional opacity. - * - * @param[in/out] dest_buf destination buffer - * @param[in] dest_stride width (stride) of destination buffer in pixels - * @param[in] fill_area area to fill - * @param[in] color color - * @param[in] opa transparency of the color - * @retval LV_RES_OK Fill completed - * @retval LV_RES_INV Error occurred (\see LV_GPU_NXP_PXP_LOG_ERRORS) - */ -lv_res_t lv_gpu_nxp_pxp_fill(lv_color_t * dest_buf, lv_coord_t dest_stride, const lv_area_t * fill_area, - lv_color_t color, lv_opa_t opa); - -/** - * BLock Image Transfer - copy rectangular image from src_buf to dst_buf with effects. - * By default, image is copied directly, with optional opacity. This function can also - * rotate the display output buffer to a specified angle (90x step). - * - * @param[in/out] dest_buf destination buffer - * @param[in] dest_area destination area - * @param[in] dest_stride width (stride) of destination buffer in pixels - * @param[in] src_buf source buffer - * @param[in] src_area source area with absolute coordinates to draw on destination buffer - * @param[in] opa opacity of the result - * @param[in] angle display rotation angle (90x) - * @retval LV_RES_OK Fill completed - * @retval LV_RES_INV Error occurred (\see LV_GPU_NXP_PXP_LOG_ERRORS) - */ -lv_res_t lv_gpu_nxp_pxp_blit(lv_color_t * dest_buf, const lv_area_t * dest_area, lv_coord_t dest_stride, - const lv_color_t * src_buf, const lv_area_t * src_area, lv_opa_t opa, lv_disp_rot_t angle); - -/** - * BLock Image Transfer - copy rectangular image from src_buf to dst_buf with transformation. - * - * - * @param[in/out] dest_buf destination buffer - * @param[in] dest_area destination area - * @param[in] dest_stride width (stride) of destination buffer in pixels - * @param[in] src_buf source buffer - * @param[in] src_area source area with absolute coordinates to draw on destination buffer - * @param[in] dsc image descriptor - * @param[in] cf color format - * @retval LV_RES_OK Fill completed - * @retval LV_RES_INV Error occurred (\see LV_GPU_NXP_PXP_LOG_ERRORS) - */ -lv_res_t lv_gpu_nxp_pxp_blit_transform(lv_color_t * dest_buf, const lv_area_t * dest_area, lv_coord_t dest_stride, - const lv_color_t * src_buf, const lv_area_t * src_area, const lv_draw_img_dsc_t * dsc, lv_img_cf_t cf); - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_GPU_NXP_PXP*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_DRAW_PXP_BLEND_H*/ diff --git a/include/liblvgl/draw/nxp/pxp/lv_gpu_nxp_pxp.h b/include/liblvgl/draw/nxp/pxp/lv_gpu_nxp_pxp.h deleted file mode 100644 index aeb9d6c0..00000000 --- a/include/liblvgl/draw/nxp/pxp/lv_gpu_nxp_pxp.h +++ /dev/null @@ -1,153 +0,0 @@ -/** - * @file lv_gpu_nxp_pxp.h - * - */ - -/** - * MIT License - * - * Copyright 2020-2022 NXP - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of - * the Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice (including the next paragraph) - * shall be included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, - * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A - * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF - * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE - * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - */ - -#ifndef LV_GPU_NXP_PXP_H -#define LV_GPU_NXP_PXP_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ - -#include "liblvgl/lv_conf_internal.h" - -#if LV_USE_GPU_NXP_PXP -#include "fsl_cache.h" -#include "fsl_pxp.h" - -#include "../../../misc/lv_log.h" - -/********************* - * DEFINES - *********************/ - -/** PXP module instance to use*/ -#define LV_GPU_NXP_PXP_ID PXP - -/** PXP interrupt line ID*/ -#define LV_GPU_NXP_PXP_IRQ_ID PXP_IRQn - -#ifndef LV_GPU_NXP_PXP_LOG_ERRORS -/** Enable logging of PXP errors (\see LV_LOG_ERROR)*/ -#define LV_GPU_NXP_PXP_LOG_ERRORS 1 -#endif - -#ifndef LV_GPU_NXP_PXP_LOG_TRACES -/** Enable logging of PXP errors (\see LV_LOG_ERROR)*/ -#define LV_GPU_NXP_PXP_LOG_TRACES 0 -#endif - -/********************** - * TYPEDEFS - **********************/ - -/** - * NXP PXP device configuration - call-backs used for - * interrupt init/wait/deinit. - */ -typedef struct { - /** Callback for PXP interrupt initialization*/ - lv_res_t (*pxp_interrupt_init)(void); - - /** Callback for PXP interrupt de-initialization*/ - void (*pxp_interrupt_deinit)(void); - - /** Callback that should start PXP and wait for operation complete*/ - void (*pxp_run)(void); -} lv_nxp_pxp_cfg_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Reset and initialize PXP device. This function should be called as a part - * of display init sequence. - * - * @retval LV_RES_OK PXP init completed - * @retval LV_RES_INV Error occurred (\see LV_GPU_NXP_PXP_LOG_ERRORS) - */ -lv_res_t lv_gpu_nxp_pxp_init(void); - -/** - * Disable PXP device. Should be called during display deinit sequence. - */ -void lv_gpu_nxp_pxp_deinit(void); - -/** - * Start PXP job and wait for completion. - */ -void lv_gpu_nxp_pxp_run(void); - -/********************** - * MACROS - **********************/ - -#define PXP_COND_STOP(cond, txt) \ - do { \ - if (cond) { \ - LV_LOG_ERROR("%s. STOP!", txt); \ - for ( ; ; ); \ - } \ - } while(0) - -#if LV_GPU_NXP_PXP_LOG_ERRORS -#define PXP_RETURN_INV(fmt, ...) \ - do { \ - LV_LOG_ERROR(fmt, ##__VA_ARGS__); \ - return LV_RES_INV; \ - } while (0) -#else -#define PXP_RETURN_INV(fmt, ...) \ - do { \ - return LV_RES_INV; \ - }while(0) -#endif /*LV_GPU_NXP_PXP_LOG_ERRORS*/ - -#if LV_GPU_NXP_PXP_LOG_TRACES -#define PXP_LOG_TRACE(fmt, ...) \ - do { \ - LV_LOG_ERROR(fmt, ##__VA_ARGS__); \ - } while (0) -#else -#define PXP_LOG_TRACE(fmt, ...) \ - do { \ - } while (0) -#endif /*LV_GPU_NXP_PXP_LOG_TRACES*/ - -#endif /*LV_USE_GPU_NXP_PXP*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_GPU_NXP_PXP_H*/ diff --git a/include/liblvgl/draw/nxp/pxp/lv_gpu_nxp_pxp_osa.h b/include/liblvgl/draw/nxp/pxp/lv_gpu_nxp_pxp_osa.h deleted file mode 100644 index d9074472..00000000 --- a/include/liblvgl/draw/nxp/pxp/lv_gpu_nxp_pxp_osa.h +++ /dev/null @@ -1,78 +0,0 @@ -/** - * @file lv_gpu_nxp_pxp_osa.h - * - */ - -/** - * MIT License - * - * Copyright 2020, 2022 NXP - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of - * the Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice (including the next paragraph) - * shall be included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, - * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A - * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF - * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE - * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - */ - -#ifndef LV_GPU_NXP_PXP_OSA_H -#define LV_GPU_NXP_PXP_OSA_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ - -#include "liblvgl/lv_conf_internal.h" - -#if LV_USE_GPU_NXP_PXP && LV_USE_GPU_NXP_PXP_AUTO_INIT -#include "lv_gpu_nxp_pxp.h" - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * PXP device interrupt handler. Used to check PXP task completion status. - */ -void PXP_IRQHandler(void); - -/** - * Helper function to get the PXP default configuration. - */ -lv_nxp_pxp_cfg_t * lv_gpu_nxp_pxp_get_cfg(void); - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_GPU_NXP_PXP && LV_USE_GPU_NXP_PXP_AUTO_INIT*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_GPU_NXP_PXP_OSA_H*/ diff --git a/include/liblvgl/draw/nxp/pxp/lv_pxp_cfg.h b/include/liblvgl/draw/nxp/pxp/lv_pxp_cfg.h new file mode 100644 index 00000000..67d13585 --- /dev/null +++ b/include/liblvgl/draw/nxp/pxp/lv_pxp_cfg.h @@ -0,0 +1,104 @@ +/** + * @file lv_pxp_cfg.h + * + */ + +/** + * Copyright 2020-2023 NXP + * + * SPDX-License-Identifier: MIT + */ + +#ifndef LV_PXP_CFG_H +#define LV_PXP_CFG_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../../lv_conf_internal.h" + +#if LV_USE_PXP +#if LV_USE_DRAW_PXP || LV_USE_ROTATE_PXP +#include "fsl_cache.h" +#include "fsl_pxp.h" + +#include "../../../misc/lv_log.h" + +/********************* + * DEFINES + *********************/ + +/** PXP module instance to use*/ +#define PXP_ID PXP + +/** PXP interrupt line ID*/ +#define PXP_IRQ_ID PXP_IRQn + +/********************** + * TYPEDEFS + **********************/ + +/** + * NXP PXP device configuration. + */ +typedef struct { + /** Callback for PXP interrupt initialization*/ + void (*pxp_interrupt_init)(void); + + /** Callback for PXP interrupt de-initialization*/ + void (*pxp_interrupt_deinit)(void); + + /** Callback for PXP start*/ + void (*pxp_run)(void); + + /** Callback for waiting of PXP completion*/ + void (*pxp_wait)(void); +} pxp_cfg_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Reset and initialize PXP device. This function should be called as a part + * of display init sequence. + */ +void lv_pxp_init(void); + +/** + * Disable PXP device. Should be called during display deinit sequence. + */ +void lv_pxp_deinit(void); + +/** + * Reset PXP device. + */ +void lv_pxp_reset(void); + +/** + * Clear cache and start PXP. + */ +void lv_pxp_run(void); + +/** + * Wait for PXP completion. + */ +void lv_pxp_wait(void); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_DRAW_PXP || LV_USE_ROTATE_PXP*/ +#endif /*LV_USE_PXP*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_PXP_CFG_H*/ diff --git a/include/liblvgl/draw/nxp/pxp/lv_pxp_osa.h b/include/liblvgl/draw/nxp/pxp/lv_pxp_osa.h new file mode 100644 index 00000000..5e3d6669 --- /dev/null +++ b/include/liblvgl/draw/nxp/pxp/lv_pxp_osa.h @@ -0,0 +1,62 @@ +/** + * @file lv_pxp_osa.h + * + */ + +/** + * Copyright 2020, 2022-2023 NXP + * + * SPDX-License-Identifier: MIT + */ + +#ifndef LV_PXP_OSA_H +#define LV_PXP_OSA_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../../lv_conf_internal.h" + +#if LV_USE_PXP +#if LV_USE_DRAW_PXP || LV_USE_ROTATE_PXP +#include "lv_pxp_cfg.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * PXP device interrupt handler. Used to check PXP task completion status. + */ +void PXP_IRQHandler(void); + +/** + * Get the PXP default configuration. + */ +pxp_cfg_t * pxp_get_default_cfg(void); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_DRAW_PXP || LV_USE_ROTATE_PXP*/ +#endif /*LV_USE_PXP*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_PXP_OSA_H*/ diff --git a/include/liblvgl/draw/nxp/pxp/lv_pxp_utils.h b/include/liblvgl/draw/nxp/pxp/lv_pxp_utils.h new file mode 100644 index 00000000..c9ba7baf --- /dev/null +++ b/include/liblvgl/draw/nxp/pxp/lv_pxp_utils.h @@ -0,0 +1,84 @@ +/** + * @file lv_pxp_utils.h + * + */ + +/** + * Copyright 2023-2024 NXP + * + * SPDX-License-Identifier: MIT + */ + +#ifndef LV_PXP_UTILS_H +#define LV_PXP_UTILS_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../../lv_conf_internal.h" + +#if LV_USE_PXP +#if LV_USE_DRAW_PXP || LV_USE_ROTATE_PXP +#include "fsl_pxp.h" +#include "../../../misc/lv_color.h" + +/********************* + * DEFINES + *********************/ + +#if LV_USE_PXP_ASSERT +#define PXP_ASSERT(expr) LV_ASSERT(expr) +#else +#define PXP_ASSERT(expr) +#endif + +#define PXP_ASSERT_MSG(expr, msg) \ + do { \ + if(!(expr)) { \ + LV_LOG_ERROR(msg); \ + PXP_ASSERT(false); \ + } \ + } while(0) + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * STATIC PROTOTYPES + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +pxp_output_pixel_format_t pxp_get_out_px_format(lv_color_format_t cf); + +pxp_as_pixel_format_t pxp_get_as_px_format(lv_color_format_t cf); + +#if LV_USE_DRAW_PXP +pxp_ps_pixel_format_t pxp_get_ps_px_format(lv_color_format_t cf); + +bool pxp_buf_aligned(const void * buf, uint32_t stride); + +/********************** + * MACROS + **********************/ + +/********************** + * STATIC FUNCTIONS + **********************/ + +#endif /*LV_USE_DRAW_PXP*/ +#endif /*LV_USE_DRAW_PXP || LV_USE_ROTATE_PXP*/ +#endif /*LV_USE_PXP*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_PXP_UTILS_H*/ diff --git a/include/liblvgl/draw/nxp/vglite/lv_draw_vglite.h b/include/liblvgl/draw/nxp/vglite/lv_draw_vglite.h new file mode 100644 index 00000000..93b18cd7 --- /dev/null +++ b/include/liblvgl/draw/nxp/vglite/lv_draw_vglite.h @@ -0,0 +1,86 @@ +/** + * @file lv_draw_vglite.h + * + */ + +/** + * Copyright 2023-2024 NXP + * + * SPDX-License-Identifier: MIT + */ + +#ifndef LV_DRAW_VGLITE_H +#define LV_DRAW_VGLITE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../../lv_conf_internal.h" + +#if LV_USE_DRAW_VGLITE +#include "../../lv_draw_private.h" +#include "../../sw/lv_draw_sw_private.h" +#include "../../../misc/lv_area_private.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef struct lv_draw_vglite_unit { + lv_draw_sw_unit_t; +#if LV_USE_VGLITE_DRAW_ASYNC + volatile bool wait_for_finish; +#endif +} lv_draw_vglite_unit_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void lv_draw_buf_vglite_init_handlers(void); + +void lv_draw_vglite_init(void); + +void lv_draw_vglite_deinit(void); + +void lv_draw_vglite_arc(lv_draw_unit_t * draw_unit, const lv_draw_arc_dsc_t * dsc, + const lv_area_t * coords); + +void lv_draw_vglite_border(lv_draw_unit_t * draw_unit, const lv_draw_border_dsc_t * dsc, + const lv_area_t * coords); + +void lv_draw_vglite_fill(lv_draw_unit_t * draw_unit, const lv_draw_fill_dsc_t * dsc, + const lv_area_t * coords); + +void lv_draw_vglite_img(lv_draw_unit_t * draw_unit, const lv_draw_image_dsc_t * dsc, + const lv_area_t * coords); + +void lv_draw_vglite_label(lv_draw_unit_t * draw_unit, const lv_draw_label_dsc_t * dsc, + const lv_area_t * coords); + +void lv_draw_vglite_layer(lv_draw_unit_t * draw_unit, const lv_draw_image_dsc_t * draw_dsc, + const lv_area_t * coords); + +void lv_draw_vglite_line(lv_draw_unit_t * draw_unit, const lv_draw_line_dsc_t * dsc); + +void lv_draw_vglite_triangle(lv_draw_unit_t * draw_unit, const lv_draw_triangle_dsc_t * dsc); + +/********************** + * MACROS + **********************/ +#endif /*LV_USE_DRAW_VGLITE*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_VGLITE_H*/ diff --git a/include/liblvgl/draw/nxp/vglite/lv_draw_vglite_arc.h b/include/liblvgl/draw/nxp/vglite/lv_draw_vglite_arc.h deleted file mode 100644 index e2da4190..00000000 --- a/include/liblvgl/draw/nxp/vglite/lv_draw_vglite_arc.h +++ /dev/null @@ -1,79 +0,0 @@ -/** - * @file lv_draw_vglite_arc.h - * - */ - -/** - * MIT License - * - * Copyright 2021, 2022 NXP - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of - * the Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice (including the next paragraph) - * shall be included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, - * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A - * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF - * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE - * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - */ - -#ifndef LV_DRAW_VGLITE_ARC_H -#define LV_DRAW_VGLITE_ARC_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lv_conf_internal.h" - -#if LV_USE_GPU_NXP_VG_LITE -#include "lv_gpu_nxp_vglite.h" - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/*** - * Draw arc shape with effects - * @param draw_ctx drawing context - * @param dsc the arc description structure (width, rounded ending, opacity) - * @param center the coordinates of the arc center - * @param radius the radius of external arc - * @param start_angle the starting angle in degrees - * @param end_angle the ending angle in degrees - */ -lv_res_t lv_gpu_nxp_vglite_draw_arc(lv_draw_ctx_t * draw_ctx, const lv_draw_arc_dsc_t * dsc, const lv_point_t * center, - int32_t radius, int32_t start_angle, int32_t end_angle); - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_GPU_NXP_VG_LITE*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_DRAW_VGLITE_ARC_H*/ diff --git a/include/liblvgl/draw/nxp/vglite/lv_draw_vglite_blend.h b/include/liblvgl/draw/nxp/vglite/lv_draw_vglite_blend.h deleted file mode 100644 index 1726dcd9..00000000 --- a/include/liblvgl/draw/nxp/vglite/lv_draw_vglite_blend.h +++ /dev/null @@ -1,149 +0,0 @@ -/** - * @file lv_draw_vglite_blend.h - * - */ - -/** - * MIT License - * - * Copyright 2020-2022 NXP - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of - * the Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice (including the next paragraph) - * shall be included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, - * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A - * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF - * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE - * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - */ - -#ifndef LV_DRAW_VGLITE_BLEND_H -#define LV_DRAW_VGLITE_BLEND_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ - -#include "liblvgl/lv_conf_internal.h" - -#if LV_USE_GPU_NXP_VG_LITE -#include "lv_gpu_nxp_vglite.h" - -/********************* - * DEFINES - *********************/ - -#ifndef LV_GPU_NXP_VG_LITE_FILL_SIZE_LIMIT -/** Minimum area (in pixels) to be filled by VG-Lite with 100% opacity*/ -#define LV_GPU_NXP_VG_LITE_FILL_SIZE_LIMIT 5000 -#endif - -#ifndef LV_GPU_NXP_VG_LITE_FILL_OPA_SIZE_LIMIT -/** Minimum area (in pixels) to be filled by VG-Lite with transparency*/ -#define LV_GPU_NXP_VG_LITE_FILL_OPA_SIZE_LIMIT 5000 -#endif - -#ifndef LV_GPU_NXP_VG_LITE_BLIT_SIZE_LIMIT -/** Minimum area (in pixels) for image copy with 100% opacity to be handled by VG-Lite*/ -#define LV_GPU_NXP_VG_LITE_BLIT_SIZE_LIMIT 5000 -#endif - -#ifndef LV_GPU_NXP_VG_LITE_BUFF_SYNC_BLIT_SIZE_LIMIT -/** Minimum invalidated area (in pixels) to be synchronized by VG-Lite during buffer sync */ -#define LV_GPU_NXP_VG_LITE_BUFF_SYNC_BLIT_SIZE_LIMIT 5000 -#endif - -#ifndef LV_GPU_NXP_VG_LITE_BLIT_OPA_SIZE_LIMIT -/** Minimum area (in pixels) for image copy with transparency to be handled by VG-Lite*/ -#define LV_GPU_NXP_VG_LITE_BLIT_OPA_SIZE_LIMIT 5000 -#endif - -/********************** - * TYPEDEFS - **********************/ - -/** - * BLock Image Transfer descriptor structure - */ -typedef struct { - - const lv_color_t * src; /**< Source buffer pointer (must be aligned on 32 bytes)*/ - lv_area_t src_area; /**< Area to be copied from source*/ - lv_coord_t src_width; /**< Source buffer width*/ - lv_coord_t src_height; /**< Source buffer height*/ - int32_t src_stride; /**< Source buffer stride in bytes (must be aligned on 16 px)*/ - - const lv_color_t * dst; /**< Destination buffer pointer (must be aligned on 32 bytes)*/ - lv_area_t dst_area; /**< Target area in destination buffer (must be the same as src_area)*/ - lv_coord_t dst_width; /**< Destination buffer width*/ - lv_coord_t dst_height; /**< Destination buffer height*/ - int32_t dst_stride; /**< Destination buffer stride in bytes (must be aligned on 16 px)*/ - - lv_opa_t opa; /**< Opacity - alpha mix (0 = source not copied, 255 = 100% opaque)*/ - uint32_t angle; /**< Rotation angle (1/10 of degree)*/ - uint32_t zoom; /**< 256 = no zoom (1:1 scale ratio)*/ - lv_point_t pivot; /**< The coordinates of rotation pivot in source image buffer*/ -} lv_gpu_nxp_vglite_blit_info_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Fill area, with optional opacity. - * - * @param[in/out] dest_buf Destination buffer pointer (must be aligned on 32 bytes) - * @param[in] dest_width Destination buffer width in pixels (must be aligned on 16 px) - * @param[in] dest_height Destination buffer height in pixels - * @param[in] fill_area Area to be filled - * @param[in] color Fill color - * @param[in] opa Opacity (255 = full, 128 = 50% background/50% color, 0 = no fill) - * @retval LV_RES_OK Fill completed - * @retval LV_RES_INV Error occurred (\see LV_GPU_NXP_VG_LITE_LOG_ERRORS) - */ -lv_res_t lv_gpu_nxp_vglite_fill(lv_color_t * dest_buf, lv_coord_t dest_width, lv_coord_t dest_height, - const lv_area_t * fill_area, lv_color_t color, lv_opa_t opa); - -/** - * BLock Image Transfer. - * - * @param[in] blit Description of the transfer - * @retval LV_RES_OK Transfer complete - * @retval LV_RES_INV Error occurred (\see LV_GPU_NXP_VG_LITE_LOG_ERRORS) - */ -lv_res_t lv_gpu_nxp_vglite_blit(lv_gpu_nxp_vglite_blit_info_t * blit); - -/** - * BLock Image Transfer with transformation. - * - * @param[in] blit Description of the transfer - * @retval LV_RES_OK Transfer complete - * @retval LV_RES_INV Error occurred (\see LV_GPU_NXP_VG_LITE_LOG_ERRORS) - */ -lv_res_t lv_gpu_nxp_vglite_blit_transform(lv_gpu_nxp_vglite_blit_info_t * blit); - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_GPU_NXP_VG_LITE*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_DRAW_VGLITE_BLEND_H*/ diff --git a/include/liblvgl/draw/nxp/vglite/lv_draw_vglite_rect.h b/include/liblvgl/draw/nxp/vglite/lv_draw_vglite_rect.h deleted file mode 100644 index 8a641707..00000000 --- a/include/liblvgl/draw/nxp/vglite/lv_draw_vglite_rect.h +++ /dev/null @@ -1,77 +0,0 @@ -/** - * @file lv_draw_vglite_rect.h - * - */ - -/** - * MIT License - * - * Copyright 2021, 2022 NXP - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of - * the Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice (including the next paragraph) - * shall be included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, - * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A - * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF - * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE - * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - */ - -#ifndef LV_DRAW_VGLITE_RECT_H -#define LV_DRAW_VGLITE_RECT_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lv_conf_internal.h" - -#if LV_USE_GPU_NXP_VG_LITE -#include "lv_gpu_nxp_vglite.h" -#include "../../lv_draw_rect.h" - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Draw rectangle shape with effects (rounded corners, gradient) - * - * @param draw_ctx drawing context - * @param dsc description of the rectangle - * @param coords the area where rectangle is clipped - */ -lv_res_t lv_gpu_nxp_vglite_draw_bg(lv_draw_ctx_t * draw_ctx, const lv_draw_rect_dsc_t * dsc, const lv_area_t * coords); - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_GPU_NXP_VG_LITE*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_DRAW_VGLITE_RECT_H*/ diff --git a/include/liblvgl/draw/nxp/vglite/lv_gpu_nxp_vglite.h b/include/liblvgl/draw/nxp/vglite/lv_gpu_nxp_vglite.h deleted file mode 100644 index 87544925..00000000 --- a/include/liblvgl/draw/nxp/vglite/lv_gpu_nxp_vglite.h +++ /dev/null @@ -1,185 +0,0 @@ -/** - * @file lv_gpu_nxp_vglite.h - * - */ - -/** - * MIT License - * - * Copyright 2020-2022 NXP - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of - * the Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice (including the next paragraph) - * shall be included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, - * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A - * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF - * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE - * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - */ - -#ifndef LV_GPU_NXP_VGLITE_H -#define LV_GPU_NXP_VGLITE_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lv_conf_internal.h" - -#if LV_USE_GPU_NXP_VG_LITE -#include "vg_lite.h" -#include "../../sw/lv_draw_sw.h" -#include "../../../misc/lv_log.h" -#include "fsl_debug_console.h" - -/********************* - * DEFINES - *********************/ - -/** Use this symbol as limit to disable feature (value has to be larger than supported resolution) */ -#define LV_GPU_NXP_VG_LITE_FEATURE_DISABLED (1920*1080+1) - -/** Stride in px required by VG-Lite HW. Don't change this. */ -#define LV_GPU_NXP_VG_LITE_STRIDE_ALIGN_PX 16U - -#ifndef LV_GPU_NXP_VG_LITE_LOG_ERRORS -/** Enable logging of VG-Lite errors (\see LV_LOG_ERROR)*/ -#define LV_GPU_NXP_VG_LITE_LOG_ERRORS 1 -#endif - -#ifndef LV_GPU_NXP_VG_LITE_LOG_TRACES -/** Enable logging of VG-Lite errors (\see LV_LOG_ERROR)*/ -#define LV_GPU_NXP_VG_LITE_LOG_TRACES 0 -#endif - -/* Draw rectangles around BLIT tiles */ -#define BLIT_DBG_AREAS 0 - -/* Print detailed info to SDK console (NOT to LVGL log system) */ -#define BLIT_DBG_VERBOSE 0 - -/* Verbose debug print */ -#if BLIT_DBG_VERBOSE -#define PRINT_BLT PRINTF -#else -#define PRINT_BLT(...) -#endif - -/* The optimal Bezier control point offset for radial unit - * see: https://spencermortensen.com/articles/bezier-circle/ - **/ -#define BEZIER_OPTIM_CIRCLE 0.551915024494f - -/* Draw lines for control points of Bezier curves */ -#define BEZIER_DBG_CONTROL_POINTS 0 - -/********************** - * TYPEDEFS - **********************/ - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Fills vg_lite_buffer_t structure according given parameters. - * - * @param[in/out] vgbuf Buffer structure to be filled - * @param[in] width Width of buffer in pixels - * @param[in] height Height of buffer in pixels - * @param[in] stride Stride of the buffer in bytes - * @param[in] ptr Pointer to the buffer (must be aligned according VG-Lite requirements) - * @param[in] source Boolean to check if this is a source buffer - */ -lv_res_t lv_vglite_init_buf(vg_lite_buffer_t * vgbuf, uint32_t width, uint32_t height, uint32_t stride, - const lv_color_t * ptr, bool source); - -#if BLIT_DBG_AREAS -/** - * Draw a simple rectangle, 1 px line width. - * - * @param dest_buf Destination buffer - * @param dest_width Destination buffer width (must be aligned on 16px) - * @param dest_height Destination buffer height - * @param fill_area Rectangle coordinates - * @param color Rectangle color - */ -void lv_vglite_dbg_draw_rectangle(lv_color_t * dest_buf, lv_coord_t dest_width, lv_coord_t dest_height, - lv_area_t * fill_area, lv_color_t color); -#endif - -/** - * Clean & invalidate cache. - */ -void lv_vglite_invalidate_cache(void); - -/********************** - * MACROS - **********************/ - -#define VG_LITE_COND_STOP(cond, txt) \ - do { \ - if (cond) { \ - LV_LOG_ERROR("%s. STOP!", txt); \ - for ( ; ; ); \ - } \ - } while(0) - -#if LV_GPU_NXP_VG_LITE_LOG_ERRORS -#define VG_LITE_ERR_RETURN_INV(err, fmt, ...) \ - do { \ - if(err != VG_LITE_SUCCESS) { \ - LV_LOG_ERROR(fmt, ##__VA_ARGS__); \ - return LV_RES_INV; \ - } \ - } while (0) -#else -#define VG_LITE_ERR_RETURN_INV(err, fmt, ...) \ - do { \ - if(err != VG_LITE_SUCCESS) { \ - return LV_RES_INV; \ - } \ - }while(0) -#endif /*LV_GPU_NXP_VG_LITE_LOG_ERRORS*/ - -#if LV_GPU_NXP_VG_LITE_LOG_TRACES -#define VG_LITE_LOG_TRACE(fmt, ...) \ - do { \ - LV_LOG_ERROR(fmt, ##__VA_ARGS__); \ - } while (0) - -#define VG_LITE_RETURN_INV(fmt, ...) \ - do { \ - LV_LOG_ERROR(fmt, ##__VA_ARGS__); \ - return LV_RES_INV; \ - } while (0) -#else -#define VG_LITE_LOG_TRACE(fmt, ...) \ - do { \ - } while (0) -#define VG_LITE_RETURN_INV(fmt, ...) \ - do { \ - return LV_RES_INV; \ - }while(0) -#endif /*LV_GPU_NXP_VG_LITE_LOG_TRACES*/ - -#endif /*LV_USE_GPU_NXP_VG_LITE*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_GPU_NXP_VGLITE_H*/ diff --git a/include/liblvgl/draw/nxp/vglite/lv_vglite_buf.h b/include/liblvgl/draw/nxp/vglite/lv_vglite_buf.h new file mode 100644 index 00000000..3ef43474 --- /dev/null +++ b/include/liblvgl/draw/nxp/vglite/lv_vglite_buf.h @@ -0,0 +1,124 @@ +/** + * @file lv_vglite_buf.h + * + */ + +/** + * Copyright 2023 NXP + * + * SPDX-License-Identifier: MIT + */ + +#ifndef LV_VGLITE_BUF_H +#define LV_VGLITE_BUF_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../../lv_conf_internal.h" + +#if LV_USE_DRAW_VGLITE +#include "../../sw/lv_draw_sw.h" + +#include "vg_lite.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Get vglite destination buffer pointer. + * + * @retval The vglite destination buffer + * + */ +vg_lite_buffer_t * vglite_get_dest_buf(void); + +/** + * Get vglite source buffer pointer. + * + * @retval The vglite source buffer + * + */ +vg_lite_buffer_t * vglite_get_src_buf(void); + +/** + * Set vglite destination buffer address only. + * + * @param[in] buf Destination buffer address (does not require alignment for VG_LITE_LINEAR mode) + * + */ +void vglite_set_dest_buf_ptr(void * buf); + +/** + * Set vglite source buffer address only. + * + * @param[in] buf Source buffer address + * + */ +void vglite_set_src_buf_ptr(const void * buf); + +/** + * Set vglite destination buffer. + * + * @param[in] buf Destination buffer address + * @param[in] width Destination buffer width + * @param[in] height Destination buffer height + * @param[in] stride Destination buffer stride in bytes + * @param[in] cf Destination buffer color format + * + */ +void vglite_set_dest_buf(const void * buf, uint32_t width, uint32_t height, uint32_t stride, + lv_color_format_t cf); + +/** + * Set vglite source buffer. + * + * @param[in] buf Source buffer address + * @param[in] width Source buffer width + * @param[in] height Source buffer height + * @param[in] stride Source buffer stride in bytes + * @param[in] cf Source buffer color format + * + */ +void vglite_set_src_buf(const void * buf, uint32_t width, uint32_t height, uint32_t stride, + lv_color_format_t cf); + +/** + * Set vglite buffer. + * + * @param[in] vgbuf Address of the VGLite buffer object + * @param[in] buf Address of the memory for the VGLite buffer + * @param[in] width Buffer width + * @param[in] height Buffer height + * @param[in] stride Buffer stride in bytes + * @param[in] cf Buffer color format + * + */ +void vglite_set_buf(vg_lite_buffer_t * vgbuf, void * buf, + uint32_t width, uint32_t height, uint32_t stride, + lv_color_format_t cf); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_DRAW_VGLITE*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_VGLITE_BUF_H*/ diff --git a/include/liblvgl/draw/nxp/vglite/lv_vglite_matrix.h b/include/liblvgl/draw/nxp/vglite/lv_vglite_matrix.h new file mode 100644 index 00000000..832cd86b --- /dev/null +++ b/include/liblvgl/draw/nxp/vglite/lv_vglite_matrix.h @@ -0,0 +1,78 @@ +/** + * @file lv_vglite_matrix.h + * + */ + +/** + * Copyright 2023 NXP + * + * SPDX-License-Identifier: MIT + */ + +#ifndef LV_VGLITE_MATRIX_H +#define LV_VGLITE_MATRIX_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../../lv_conf_internal.h" + +#if LV_USE_DRAW_VGLITE +#include "../../sw/lv_draw_sw.h" + +#include "vg_lite.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * STATIC PROTOTYPES + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +vg_lite_matrix_t * vglite_get_matrix(void); + +/** + * Creates matrix that translates to origin of given destination area. + * + * @param[in] dest_area Area with relative coordinates of destination buffer + * + */ +void vglite_set_translation_matrix(const lv_area_t * dest_area); + +/** + * Creates matrix that translates to origin of given destination area with transformation (scale or rotate). + * + * @param[in] dest_area Area with relative coordinates of destination buffer + * @param[in] dsc Image descriptor + * + */ +void vglite_set_transformation_matrix(const lv_area_t * dest_area, const lv_draw_image_dsc_t * dsc); + +/********************** + * MACROS + **********************/ + +/********************** + * STATIC FUNCTIONS + **********************/ + +#endif /*LV_USE_DRAW_VGLITE*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_VGLITE_MATRIX_H*/ diff --git a/include/liblvgl/draw/nxp/vglite/lv_vglite_path.h b/include/liblvgl/draw/nxp/vglite/lv_vglite_path.h new file mode 100644 index 00000000..f38593ae --- /dev/null +++ b/include/liblvgl/draw/nxp/vglite/lv_vglite_path.h @@ -0,0 +1,97 @@ +/** + * @file lv_vglite_path.h + * + */ + +/** + * Copyright 2023 NXP + * + * SPDX-License-Identifier: MIT + */ + +#ifndef LV_VGLITE_PATH_H +#define LV_VGLITE_PATH_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../../lv_conf_internal.h" + +#if LV_USE_DRAW_VGLITE +#include "../../sw/lv_draw_sw.h" + +/********************* + * DEFINES + *********************/ + +/* The optimal Bezier control point offset for radial unit + * see: https://spencermortensen.com/articles/bezier-circle/ + **/ +#define BEZIER_OPTIM_CIRCLE 0.551915024494f + +/* Draw lines for control points of Bezier curves */ +#define BEZIER_DBG_CONTROL_POINTS 0 + +/* Path data sizes for different elements */ +#define CUBIC_PATH_DATA_SIZE 7 /* 1 opcode, 6 arguments */ +#define LINE_PATH_DATA_SIZE 3 /* 1 opcode, 2 arguments */ +#define MOVE_PATH_DATA_SIZE 3 /* 1 opcode, 2 arguments */ +#define END_PATH_DATA_SIZE 1 /* 1 opcode, 0 arguments */ +/* Maximum possible rectangle path size + * is in the rounded rectangle case: + * - 1 move for the path start + * - 4 cubics for the corners + * - 4 lines for the sides + * - 1 end for the path end */ +#define RECT_PATH_DATA_MAX_SIZE (1 * MOVE_PATH_DATA_SIZE + 4 * CUBIC_PATH_DATA_SIZE + 4 * LINE_PATH_DATA_SIZE + 1 * END_PATH_DATA_SIZE) + +/* Maximum possible arc path size + * is in the rounded arc case: + * - 1 move for the path start + * - 16 cubics for the arc (5 inner, 5 outer) and corners (3 per corner) + * - 1 end for the path end */ +#define ARC_PATH_DATA_MAX_SIZE (1 * MOVE_PATH_DATA_SIZE + 16 * CUBIC_PATH_DATA_SIZE + 1 * END_PATH_DATA_SIZE) +/********************** + * TYPEDEFS + **********************/ + +/********************** + * STATIC PROTOTYPES + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Generates path data for rectangle drawing. + * + * @param[in/out] path The path data to initialize + * @param[in/out] path_size The resulting size of the created path data + * @param[in] dsc The style descriptor for the rectangle to be drawn + * @param[in] coords The coordinates of the rectangle to be drawn + * + */ +void vglite_create_rect_path_data(int32_t * path_data, uint32_t * path_data_size, + int32_t radius, + const lv_area_t * coords); + +/********************** + * MACROS + **********************/ + +/********************** + * STATIC FUNCTIONS + **********************/ + +#endif /*LV_USE_DRAW_VGLITE*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_VGLITE_PATH_H*/ diff --git a/include/liblvgl/draw/nxp/vglite/lv_vglite_utils.h b/include/liblvgl/draw/nxp/vglite/lv_vglite_utils.h new file mode 100644 index 00000000..6615c4c7 --- /dev/null +++ b/include/liblvgl/draw/nxp/vglite/lv_vglite_utils.h @@ -0,0 +1,179 @@ +/** + * @file lv_vglite_utils.h + * + */ + +/** + * Copyright 2022-2024 NXP + * + * SPDX-License-Identifier: MIT + */ + +#ifndef LV_VGLITE_UTILS_H +#define LV_VGLITE_UTILS_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../../lv_conf_internal.h" + +#if LV_USE_DRAW_VGLITE +#include "../../sw/lv_draw_sw.h" + +#include "vg_lite.h" +#include "vg_lite_options.h" + +/********************* + * DEFINES + *********************/ + +#define ENUM_TO_STRING(e) \ + case (e): \ + return #e + +#if LV_USE_VGLITE_ASSERT +#define VGLITE_ASSERT(expr) LV_ASSERT(expr) +#else +#define VGLITE_ASSERT(expr) +#endif + +#define VGLITE_ASSERT_MSG(expr, msg) \ + do { \ + if(!(expr)) { \ + LV_LOG_ERROR(msg); \ + VGLITE_ASSERT(false); \ + } \ + } while(0) + +#define VGLITE_CHECK_ERROR(function) \ + do { \ + vg_lite_error_t error = function; \ + if(error != VG_LITE_SUCCESS) { \ + LV_LOG_ERROR("Execute '" #function "' error(%d): %s", \ + (int)error, vglite_error_to_string(error)); \ + VGLITE_ASSERT(false); \ + } \ + } while (0) + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * STATIC PROTOTYPES + **********************/ + +/** + * Set the clipping box. + * + * @param[in] clip_area Clip area with relative coordinates of destination buffer + * + */ +static inline void vglite_set_scissor(const lv_area_t * clip_area); + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +const char * vglite_error_to_string(vg_lite_error_t error); + +#if LV_USE_VGLITE_DRAW_ASYNC +/** + * Get VG-Lite command buffer flushed status. + * + */ +bool vglite_cmd_buf_is_flushed(void); +#endif + +/** + * Flush command to VG-Lite. + * + */ +void vglite_run(void); + +/** + * Wait for VG-Lite finish. + * + */ +#if LV_USE_VGLITE_DRAW_ASYNC +void vglite_wait_for_finish(void); +#endif + +/** + * Get vglite color. Premultiplies (if not hw already) and swizzles the given + * LVGL 32bit color to obtain vglite color. + * + * @param[in] lv_col32 The initial LVGL 32bit color + * @param[in] gradient True for gradient color + * + * @retval The vglite 32-bit color value: + * + */ +vg_lite_color_t vglite_get_color(lv_color32_t lv_col32, bool gradient); + +/** + * Get vglite blend mode. + * + * @param[in] lv_blend_mode The LVGL blend mode + * + * @retval The vglite blend mode + * + */ +vg_lite_blend_t vglite_get_blend_mode(lv_blend_mode_t lv_blend_mode); + +/** + * Get vglite buffer format. + * + * @param[in] cf Color format + * + * @retval The vglite buffer format + * + */ +vg_lite_buffer_format_t vglite_get_buf_format(lv_color_format_t cf); + +/** + * Get vglite stride alignment. + * + * @param[in] cf Color format + * + * @retval Alignment requirement in bytes + * + */ +uint8_t vglite_get_stride_alignment(lv_color_format_t cf); + +/** + * Check source start address and stride alignment. + * + * @param[in] buf Buffer address + * @param[in] stride Stride of buffer in bytes + * @param[in] cf Color format - to calculate the expected alignment + * + * @retval true Alignment OK + * + */ +bool vglite_src_buf_aligned(const void * buf, uint32_t stride, lv_color_format_t cf); + +/********************** + * MACROS + **********************/ + +/********************** + * STATIC FUNCTIONS + **********************/ + +static inline void vglite_set_scissor(const lv_area_t * clip_area) +{ + vg_lite_set_scissor(clip_area->x1, clip_area->y1, clip_area->x2 + 1, clip_area->y2 + 1); +} + +#endif /*LV_USE_DRAW_VGLITE*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_VGLITE_UTILS_H*/ diff --git a/include/liblvgl/extra/layouts/lv_layouts.h b/include/liblvgl/draw/opengles/lv_draw_opengles.h similarity index 60% rename from include/liblvgl/extra/layouts/lv_layouts.h rename to include/liblvgl/draw/opengles/lv_draw_opengles.h index 9c1e958d..1285c93e 100644 --- a/include/liblvgl/extra/layouts/lv_layouts.h +++ b/include/liblvgl/draw/opengles/lv_draw_opengles.h @@ -1,10 +1,10 @@ /** - * @file lv_layouts.h + * @file lv_draw_opengles.h * */ -#ifndef LV_LAYOUTS_H -#define LV_LAYOUTS_H +#ifndef LV_DRAW_OPENGLES_H +#define LV_DRAW_OPENGLES_H #ifdef __cplusplus extern "C" { @@ -13,8 +13,9 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "flex/lv_flex.h" -#include "grid/lv_grid.h" + +#include "../../lv_conf_internal.h" +#if LV_USE_DRAW_OPENGLES /********************* * DEFINES @@ -28,17 +29,17 @@ extern "C" { * GLOBAL PROTOTYPES **********************/ +void lv_draw_opengles_init(void); +void lv_draw_opengles_deinit(void); + /********************** * MACROS **********************/ -#if LV_USE_LOG && LV_LOG_TRACE_LAYOUT -# define LV_TRACE_LAYOUT(...) LV_LOG_TRACE(__VA_ARGS__) -#else -# define LV_TRACE_LAYOUT(...) -#endif + +#endif /*LV_USE_DRAW_OPENGLES*/ #ifdef __cplusplus } /*extern "C"*/ #endif -#endif /*LV_LAYOUTS_H*/ +#endif /*LV_DRAW_OPENGLES_H*/ diff --git a/include/liblvgl/draw/renesas/dave2d/lv_draw_dave2d.h b/include/liblvgl/draw/renesas/dave2d/lv_draw_dave2d.h new file mode 100644 index 00000000..3a52289a --- /dev/null +++ b/include/liblvgl/draw/renesas/dave2d/lv_draw_dave2d.h @@ -0,0 +1,105 @@ +/** + * @file lv_draw_dave2d.h + * + */ + +#ifndef LV_DRAW_DAVE2D_H +#define LV_DRAW_DAVE2D_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../../lv_conf_internal.h" +#if LV_USE_DRAW_DAVE2D +#include "../../lv_draw.h" +#include "../../lv_draw_private.h" +#include "hal_data.h" +#include "lv_draw_dave2d_utils.h" +#include "../../lv_draw_rect.h" +#include "../../lv_draw_line.h" +#include "../../lv_draw_arc.h" +#include "../../lv_draw_label.h" +#include "../../lv_draw_image.h" +#include "../../lv_draw_triangle.h" +#include "../../lv_draw_buf.h" + + +/********************* + * DEFINES + *********************/ + +#define D2_RENDER_EACH_OPERATION (1) + +/********************** + * TYPEDEFS + **********************/ + +typedef struct { + lv_draw_unit_t base_unit; + lv_draw_task_t * task_act; +#if LV_USE_OS + lv_thread_sync_t sync; + lv_thread_t thread; +#endif + uint32_t idx; + d2_device * d2_handle; + d2_renderbuffer * renderbuffer; +#if LV_USE_OS + lv_mutex_t * pd2Mutex; +#endif +} lv_draw_dave2d_unit_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void lv_draw_dave2d_init(void); + +void lv_draw_dave2d_image(lv_draw_dave2d_unit_t * draw_unit, const lv_draw_image_dsc_t * draw_dsc, + const lv_area_t * coords); + +void lv_draw_dave2d_fill(lv_draw_dave2d_unit_t * draw_unit, const lv_draw_fill_dsc_t * dsc, const lv_area_t * coords); + +void lv_draw_dave2d_border(lv_draw_dave2d_unit_t * draw_unit, const lv_draw_border_dsc_t * dsc, + const lv_area_t * coords); + +void lv_draw_dave2d_box_shadow(lv_draw_dave2d_unit_t * draw_unit, const lv_draw_box_shadow_dsc_t * dsc, + const lv_area_t * coords); + +void lv_draw_dave2d_label(lv_draw_dave2d_unit_t * draw_unit, const lv_draw_label_dsc_t * dsc, const lv_area_t * coords); + +void lv_draw_dave2d_arc(lv_draw_dave2d_unit_t * draw_unit, const lv_draw_arc_dsc_t * dsc, const lv_area_t * coords); + +void lv_draw_dave2d_line(lv_draw_dave2d_unit_t * draw_unit, const lv_draw_line_dsc_t * dsc); + +void lv_draw_dave2d_layer(lv_draw_dave2d_unit_t * draw_unit, const lv_draw_image_dsc_t * draw_dsc, + const lv_area_t * coords); + +void lv_draw_dave2d_triangle(lv_draw_dave2d_unit_t * draw_unit, const lv_draw_triangle_dsc_t * dsc); + +void lv_draw_dave2d_mask_rect(lv_draw_dave2d_unit_t * draw_unit, const lv_draw_mask_rect_dsc_t * dsc, + const lv_area_t * coords); + +void lv_draw_dave2d_transform(lv_draw_dave2d_unit_t * draw_unit, const lv_area_t * dest_area, const void * src_buf, + int32_t src_w, int32_t src_h, int32_t src_stride, + const lv_draw_image_dsc_t * draw_dsc, const lv_draw_image_sup_t * sup, lv_color_format_t cf, void * dest_buf); + +/*********************** + * GLOBAL VARIABLES + ***********************/ + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_DRAW_DAVE2D*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_USE_DRAW_DAVE2D*/ diff --git a/include/liblvgl/draw/renesas/dave2d/lv_draw_dave2d_utils.h b/include/liblvgl/draw/renesas/dave2d/lv_draw_dave2d_utils.h new file mode 100644 index 00000000..67c51026 --- /dev/null +++ b/include/liblvgl/draw/renesas/dave2d/lv_draw_dave2d_utils.h @@ -0,0 +1,45 @@ +/** + * @file lv_draw_dave2d_utils.h + * + */ + +#ifndef LV_DRAW_DAVE2D_UTILS_H +#define LV_DRAW_DAVE2D_UTILS_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +d2_color lv_draw_dave2d_lv_colour_to_d2_colour(lv_color_t color); + +d2_s32 lv_draw_dave2d_cf_fb_get(void); + +d2_u32 lv_draw_dave2d_lv_colour_fmt_to_d2_fmt(lv_color_format_t colour_format); + +void d2_framebuffer_from_layer(d2_device * handle, lv_layer_t * layer); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_DAVE2D_UTILS_H*/ diff --git a/include/liblvgl/draw/sdl/lv_draw_sdl.h b/include/liblvgl/draw/sdl/lv_draw_sdl.h index 7708d5b7..cb370a1b 100644 --- a/include/liblvgl/draw/sdl/lv_draw_sdl.h +++ b/include/liblvgl/draw/sdl/lv_draw_sdl.h @@ -6,7 +6,6 @@ #ifndef LV_DRAW_SDL_H #define LV_DRAW_SDL_H - #ifdef __cplusplus extern "C" { #endif @@ -14,80 +13,72 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" +#include "../lv_draw.h" -#if LV_USE_GPU_SDL +#if LV_USE_DRAW_SDL -#include LV_GPU_SDL_INCLUDE_PATH - -#include "../lv_draw.h" -#include "../../core/lv_disp.h" +#include "../../misc/cache/lv_cache.h" +#include "../../misc/lv_area.h" +#include "../../misc/lv_color.h" +#include "../../display/lv_display.h" +#include "../../osal/lv_os.h" +#include "../../draw/lv_draw_label.h" +#include "../../draw/lv_draw_rect.h" +#include "../../draw/lv_draw_arc.h" +#include "../../draw/lv_draw_image.h" +#include "../../draw/lv_draw_triangle.h" +#include "../../draw/lv_draw_line.h" /********************* * DEFINES *********************/ -#if SDL_BYTEORDER == SDL_BIG_ENDIAN -#define LV_DRAW_SDL_TEXTURE_FORMAT SDL_PIXELFORMAT_ARGB8888 -#else -#define LV_DRAW_SDL_TEXTURE_FORMAT SDL_PIXELFORMAT_RGBA8888 -#endif - /********************** * TYPEDEFS **********************/ -struct lv_draw_sdl_context_internals_t; - -typedef struct { - /** - * Render for display driver - */ - SDL_Renderer * renderer; - void * user_data; -} lv_draw_sdl_drv_param_t; - typedef struct { - lv_draw_ctx_t base_draw; - SDL_Renderer * renderer; - struct lv_draw_sdl_context_internals_t * internals; -} lv_draw_sdl_ctx_t; + lv_draw_unit_t base_unit; + lv_draw_task_t * task_act; + lv_cache_t * texture_cache; +} lv_draw_sdl_unit_t; /********************** * GLOBAL PROTOTYPES **********************/ -void lv_draw_sdl_init_ctx(lv_disp_drv_t * disp_drv, lv_draw_ctx_t * draw_ctx); +void lv_draw_sdl_init(void); -/** - * @brief Free caches - * - */ -void lv_draw_sdl_deinit_ctx(lv_disp_drv_t * disp_drv, lv_draw_ctx_t * draw_ctx); +void /* LV_ATTRIBUTE_FAST_MEM */ lv_draw_sdl_image(lv_draw_unit_t * draw_unit, const lv_draw_image_dsc_t * draw_dsc, + const lv_area_t * coords); + +void lv_draw_sdl_fill(lv_draw_unit_t * draw_unit, const lv_draw_fill_dsc_t * dsc, const lv_area_t * coords); + +void lv_draw_sdl_border(lv_draw_unit_t * draw_unit, const lv_draw_border_dsc_t * dsc, const lv_area_t * coords); + +void lv_draw_sdl_box_shadow(lv_draw_unit_t * draw_unit, const lv_draw_box_shadow_dsc_t * dsc, const lv_area_t * coords); + +void lv_draw_sdl_label(lv_draw_unit_t * draw_unit, const lv_draw_label_dsc_t * dsc, const lv_area_t * coords); + +void lv_draw_sdl_arc(lv_draw_unit_t * draw_unit, const lv_draw_arc_dsc_t * dsc, const lv_area_t * coords); -SDL_Texture * lv_draw_sdl_create_screen_texture(SDL_Renderer * renderer, lv_coord_t hor, lv_coord_t ver); +void /* LV_ATTRIBUTE_FAST_MEM */ lv_draw_sdl_line(lv_draw_unit_t * draw_unit, const lv_draw_line_dsc_t * dsc); -/*====================== - * Add/remove functions - *=====================*/ +void lv_draw_sdl_layer(lv_draw_unit_t * draw_unit, const lv_draw_image_dsc_t * draw_dsc, const lv_area_t * coords); -/*===================== - * Setter functions - *====================*/ +void lv_draw_sdl_triangle(lv_draw_unit_t * draw_unit, const lv_draw_triangle_dsc_t * dsc); -/*===================== - * Getter functions - *====================*/ +void lv_draw_sdl_mask_rect(lv_draw_unit_t * draw_unit, const lv_draw_mask_rect_dsc_t * dsc, const lv_area_t * coords); -/*===================== - * Other functions - *====================*/ +/*********************** + * GLOBAL VARIABLES + ***********************/ /********************** * MACROS **********************/ -#endif /*LV_USE_GPU_SDL*/ +#endif /*LV_USE_DRAW_SDL*/ #ifdef __cplusplus } /*extern "C"*/ diff --git a/include/liblvgl/draw/sdl/lv_draw_sdl_composite.h b/include/liblvgl/draw/sdl/lv_draw_sdl_composite.h deleted file mode 100644 index 3a598d76..00000000 --- a/include/liblvgl/draw/sdl/lv_draw_sdl_composite.h +++ /dev/null @@ -1,73 +0,0 @@ -/** - * @file lv_draw_sdl_composite.h - * - */ - -#ifndef LV_DRAW_SDL_COMPOSITE_H -#define LV_DRAW_SDL_COMPOSITE_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ - -#include "liblvgl/lv_conf_internal.h" - -#include LV_GPU_SDL_INCLUDE_PATH - -#include "lv_draw_sdl.h" -#include "liblvgl/misc/lv_area.h" -#include "liblvgl/misc/lv_color.h" - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -typedef enum lv_draw_sdl_composite_texture_id_t { - LV_DRAW_SDL_COMPOSITE_TEXTURE_ID_STREAM0, - LV_DRAW_SDL_COMPOSITE_TEXTURE_ID_STREAM1, - LV_DRAW_SDL_COMPOSITE_TEXTURE_ID_TARGET0, - LV_DRAW_SDL_COMPOSITE_TEXTURE_ID_TARGET1, - LV_DRAW_SDL_COMPOSITE_TEXTURE_ID_TRANSFORM0, -} lv_draw_sdl_composite_texture_id_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Begin drawing with mask. Render target will be switched to a temporary texture, - * and drawing coordinates may get clipped or translated - * @param coords_in Original coordinates - * @param clip_in Original clip area - * @param extension Useful for shadows or outlines, can be NULL - * @param coords_out Translated coords - * @param clip_out Translated clip area - * @param apply_area Area of actual composited texture will be drawn - * @return true if there are any mask needs to be drawn, false otherwise - */ -bool lv_draw_sdl_composite_begin(lv_draw_sdl_ctx_t * ctx, const lv_area_t * coords_in, const lv_area_t * clip_in, - const lv_area_t * extension, lv_blend_mode_t blend_mode, lv_area_t * coords_out, - lv_area_t * clip_out, lv_area_t * apply_area); - -void lv_draw_sdl_composite_end(lv_draw_sdl_ctx_t * ctx, const lv_area_t * apply_area, lv_blend_mode_t blend_mode); - -SDL_Texture * lv_draw_sdl_composite_texture_obtain(lv_draw_sdl_ctx_t * ctx, lv_draw_sdl_composite_texture_id_t id, - lv_coord_t w, lv_coord_t h); - -/********************** - * MACROS - **********************/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_DRAW_SDL_COMPOSITE_H*/ diff --git a/include/liblvgl/draw/sdl/lv_draw_sdl_img.h b/include/liblvgl/draw/sdl/lv_draw_sdl_img.h deleted file mode 100644 index d6e3758c..00000000 --- a/include/liblvgl/draw/sdl/lv_draw_sdl_img.h +++ /dev/null @@ -1,72 +0,0 @@ -/** - * @file lv_draw_sdl_img.h - * - */ - -#ifndef LV_DRAW_SDL_IMG_H -#define LV_DRAW_SDL_IMG_H - - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lv_conf_internal.h" - -#if LV_USE_GPU_SDL - -#include LV_GPU_SDL_INCLUDE_PATH - -#include "../lv_draw.h" - -#include "lv_draw_sdl_texture_cache.h" - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -typedef struct lv_draw_sdl_img_header_t { - lv_img_header_t base; - SDL_Rect rect; -} lv_draw_sdl_img_header_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/*====================== - * Add/remove functions - *=====================*/ - -/*===================== - * Setter functions - *====================*/ - -/*===================== - * Getter functions - *====================*/ - -/*===================== - * Other functions - *====================*/ -bool lv_draw_sdl_img_load_texture(lv_draw_sdl_ctx_t * ctx, lv_draw_sdl_cache_key_head_img_t * key, size_t key_size, - const void * src, int32_t frame_id, SDL_Texture ** texture, - lv_draw_sdl_img_header_t ** header); -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_GPU_SDL*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_DRAW_SDL_IMG_H*/ diff --git a/include/liblvgl/draw/sdl/lv_draw_sdl_layer.h b/include/liblvgl/draw/sdl/lv_draw_sdl_layer.h deleted file mode 100644 index b60303c4..00000000 --- a/include/liblvgl/draw/sdl/lv_draw_sdl_layer.h +++ /dev/null @@ -1,55 +0,0 @@ -/** - * @file lv_draw_sdl_refr.h - * - */ - -#ifndef LV_TEMPL_H -#define LV_TEMPL_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "lv_draw_sdl.h" - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ -typedef struct _lv_draw_sdl_layer_ctx_t { - lv_draw_layer_ctx_t base; - - SDL_Texture * orig_target; - SDL_Texture * target; - SDL_Rect target_rect; - lv_draw_layer_flags_t flags; -} lv_draw_sdl_layer_ctx_t; -/********************** - * GLOBAL PROTOTYPES - **********************/ - -lv_draw_layer_ctx_t * lv_draw_sdl_layer_init(lv_draw_ctx_t * draw_ctx, lv_draw_layer_ctx_t * layer_ctx, - lv_draw_layer_flags_t flags); - -void lv_draw_sdl_layer_blend(lv_draw_ctx_t * draw_ctx, lv_draw_layer_ctx_t * transform_ctx, - const lv_draw_img_dsc_t * draw_dsc); - -void lv_draw_sdl_layer_destroy(lv_draw_ctx_t * draw_ctx, lv_draw_layer_ctx_t * layer_ctx); - -void lv_draw_sdl_transform_areas_offset(lv_draw_sdl_ctx_t * ctx, bool has_composite, lv_area_t * apply_area, - lv_area_t * coords, lv_area_t * clip); -/********************** - * MACROS - **********************/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_TEMPL_H*/ diff --git a/include/liblvgl/draw/sdl/lv_draw_sdl_mask.h b/include/liblvgl/draw/sdl/lv_draw_sdl_mask.h deleted file mode 100644 index aa4dd2b8..00000000 --- a/include/liblvgl/draw/sdl/lv_draw_sdl_mask.h +++ /dev/null @@ -1,51 +0,0 @@ -/** - * @file lv_draw_sdl_mask.h - * - */ - -#ifndef LV_DRAW_SDL_MASK_H -#define LV_DRAW_SDL_MASK_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ - -#include "liblvgl/lv_conf_internal.h" - -#include LV_GPU_SDL_INCLUDE_PATH - -#include "lv_draw_sdl.h" -#include "liblvgl/misc/lv_area.h" -#include "liblvgl/misc/lv_color.h" - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -lv_opa_t * lv_draw_sdl_mask_dump_opa(const lv_area_t * coords, const int16_t * ids, int16_t ids_count); - -SDL_Texture * lv_draw_sdl_mask_dump_texture(SDL_Renderer * renderer, const lv_area_t * coords, const int16_t * ids, - int16_t ids_count); - - -/********************** - * MACROS - **********************/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_DRAW_SDL_MASK_H*/ diff --git a/include/liblvgl/draw/sdl/lv_draw_sdl_priv.h b/include/liblvgl/draw/sdl/lv_draw_sdl_priv.h deleted file mode 100644 index 736fe89f..00000000 --- a/include/liblvgl/draw/sdl/lv_draw_sdl_priv.h +++ /dev/null @@ -1,72 +0,0 @@ -/** - * @file lv_draw_sdl_priv.h - * - */ - -#ifndef LV_DRAW_SDL_PRIV_H -#define LV_DRAW_SDL_PRIV_H - - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lv_conf_internal.h" - -#if LV_USE_GPU_SDL - -#include LV_GPU_SDL_INCLUDE_PATH - -#include "../lv_draw.h" -#include "../../misc/lv_lru.h" - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -typedef struct lv_draw_sdl_context_internals_t { - lv_lru_t * texture_cache; - SDL_Texture * mask; - SDL_Texture * composition; - SDL_Texture * target_backup; - uint8_t transform_count; -} lv_draw_sdl_context_internals_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/*====================== - * Add/remove functions - *=====================*/ - -/*===================== - * Setter functions - *====================*/ - -/*===================== - * Getter functions - *====================*/ - -/*===================== - * Other functions - *====================*/ - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_GPU_SDL*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_DRAW_SDL_PRIV_H*/ diff --git a/include/liblvgl/draw/sdl/lv_draw_sdl_rect.h b/include/liblvgl/draw/sdl/lv_draw_sdl_rect.h deleted file mode 100644 index fc968945..00000000 --- a/include/liblvgl/draw/sdl/lv_draw_sdl_rect.h +++ /dev/null @@ -1,75 +0,0 @@ -/** - * @file lv_draw_sdl_rect.h - * - */ - -#ifndef LV_DRAW_SDL_RECT_H -#define LV_DRAW_SDL_RECT_H - - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lv_conf_internal.h" - -#if LV_USE_GPU_SDL - -#include LV_GPU_SDL_INCLUDE_PATH - -#include "../lv_draw.h" - -#include "lv_draw_sdl_texture_cache.h" - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -typedef struct lv_draw_sdl_rect_header_t { - lv_img_header_t base; - SDL_Rect rect; -} lv_draw_sdl_rect_header_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/*====================== - * Add/remove functions - *=====================*/ - -/*===================== - * Setter functions - *====================*/ - -/*===================== - * Getter functions - *====================*/ - -/*===================== - * Other functions - *====================*/ - -SDL_Texture * lv_draw_sdl_rect_bg_frag_obtain(lv_draw_sdl_ctx_t * ctx, lv_coord_t radius); - -void lv_draw_sdl_rect_bg_frag_draw_corners(lv_draw_sdl_ctx_t * ctx, SDL_Texture * frag, lv_coord_t frag_size, - const lv_area_t * coords, const lv_area_t * clip, bool full); - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_GPU_SDL*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_DRAW_SDL_RECT_H*/ diff --git a/include/liblvgl/draw/sdl/lv_draw_sdl_texture_cache.h b/include/liblvgl/draw/sdl/lv_draw_sdl_texture_cache.h deleted file mode 100644 index 0b54246f..00000000 --- a/include/liblvgl/draw/sdl/lv_draw_sdl_texture_cache.h +++ /dev/null @@ -1,102 +0,0 @@ -/** - * @file lv_draw_sdl_texture_cache.h - * - */ - -#ifndef LV_DRAW_SDL_TEXTURE_CACHE_H -#define LV_DRAW_SDL_TEXTURE_CACHE_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ - -#include "liblvgl/lv_conf_internal.h" - -#if LV_USE_GPU_SDL - -#include LV_GPU_SDL_INCLUDE_PATH -#include "lv_draw_sdl.h" -#include "lv_draw_sdl_priv.h" -#include "../../draw/lv_img_decoder.h" -#include "../../misc/lv_area.h" - -/********************* - * DEFINES - *********************/ - -#define LV_DRAW_SDL_DEC_DSC_TEXTURE_HEAD "@LVSDLTex" - -/********************** - * TYPEDEFS - **********************/ - -typedef struct { - char head[8]; - SDL_Texture * texture; - SDL_Rect rect; - bool texture_managed; - bool texture_referenced; -} lv_draw_sdl_dec_dsc_userdata_t; - -typedef enum { - LV_GPU_CACHE_KEY_MAGIC_ARC = 0x01, - LV_GPU_CACHE_KEY_MAGIC_IMG = 0x11, - LV_GPU_CACHE_KEY_MAGIC_IMG_ROUNDED_CORNERS = 0x12, - LV_GPU_CACHE_KEY_MAGIC_LINE = 0x21, - LV_GPU_CACHE_KEY_MAGIC_RECT_BG = 0x31, - LV_GPU_CACHE_KEY_MAGIC_RECT_SHADOW = 0x32, - LV_GPU_CACHE_KEY_MAGIC_RECT_BORDER = 0x33, - LV_GPU_CACHE_KEY_MAGIC_FONT_GLYPH = 0x41, - LV_GPU_CACHE_KEY_MAGIC_MASK = 0x51, -} lv_sdl_cache_key_magic_t; - -typedef enum { - LV_DRAW_SDL_CACHE_FLAG_NONE = 0, - LV_DRAW_SDL_CACHE_FLAG_MANAGED = 1, -} lv_draw_sdl_cache_flag_t; - -typedef struct { - lv_sdl_cache_key_magic_t magic; - lv_img_src_t type; - int32_t frame_id; -} lv_draw_sdl_cache_key_head_img_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -void lv_draw_sdl_texture_cache_init(lv_draw_sdl_ctx_t * ctx); - -void lv_draw_sdl_texture_cache_deinit(lv_draw_sdl_ctx_t * ctx); - -/** - * Find cached texture by key. The texture can be destroyed during usage. - */ -SDL_Texture * lv_draw_sdl_texture_cache_get(lv_draw_sdl_ctx_t * ctx, const void * key, size_t key_length, bool * found); - -SDL_Texture * lv_draw_sdl_texture_cache_get_with_userdata(lv_draw_sdl_ctx_t * ctx, const void * key, size_t key_length, - bool * found, void ** userdata); - -void lv_draw_sdl_texture_cache_put(lv_draw_sdl_ctx_t * ctx, const void * key, size_t key_length, SDL_Texture * texture); - -void lv_draw_sdl_texture_cache_put_advanced(lv_draw_sdl_ctx_t * ctx, const void * key, size_t key_length, - SDL_Texture * texture, void * userdata, void userdata_free(void *), - lv_draw_sdl_cache_flag_t flags); - -lv_draw_sdl_cache_key_head_img_t * lv_draw_sdl_texture_img_key_create(const void * src, int32_t frame_id, - size_t * size); - -/********************** - * MACROS - **********************/ -#endif /*LV_USE_GPU_SDL*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_DRAW_SDL_TEXTURE_CACHE_H*/ diff --git a/include/liblvgl/draw/sdl/lv_draw_sdl_utils.h b/include/liblvgl/draw/sdl/lv_draw_sdl_utils.h deleted file mode 100644 index 943bda3b..00000000 --- a/include/liblvgl/draw/sdl/lv_draw_sdl_utils.h +++ /dev/null @@ -1,65 +0,0 @@ -/** - * @file lv_draw_sdl_utils.h - * - */ -#ifndef LV_DRAW_SDL_UTILS_H -#define LV_DRAW_SDL_UTILS_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ - -#include "liblvgl/lv_conf_internal.h" -#if LV_USE_GPU_SDL - -#include "lv_draw_sdl.h" -#include "../../misc/lv_color.h" -#include "../../misc/lv_area.h" - -#include LV_GPU_SDL_INCLUDE_PATH - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -void _lv_draw_sdl_utils_init(); - -void _lv_draw_sdl_utils_deinit(); - -void lv_area_to_sdl_rect(const lv_area_t * in, SDL_Rect * out); - -void lv_color_to_sdl_color(const lv_color_t * in, SDL_Color * out); - -void lv_area_zoom_to_sdl_rect(const lv_area_t * in, SDL_Rect * out, uint16_t zoom, const lv_point_t * pivot); - -SDL_Palette * lv_sdl_alloc_palette_for_bpp(const uint8_t * mapping, uint8_t bpp); - -SDL_Surface * lv_sdl_create_opa_surface(lv_opa_t * opa, lv_coord_t width, lv_coord_t height, lv_coord_t stride); - -SDL_Texture * lv_sdl_create_opa_texture(SDL_Renderer * renderer, lv_opa_t * pixels, lv_coord_t width, - lv_coord_t height, lv_coord_t stride); - -void lv_sdl_to_8bpp(uint8_t * dest, const uint8_t * src, int width, int height, int stride, uint8_t bpp); - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_GPU_SDL*/ -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_DRAW_SDL_UTILS_H*/ diff --git a/include/liblvgl/draw/stm32_dma2d/lv_gpu_stm32_dma2d.h b/include/liblvgl/draw/stm32_dma2d/lv_gpu_stm32_dma2d.h deleted file mode 100644 index f2bcdbea..00000000 --- a/include/liblvgl/draw/stm32_dma2d/lv_gpu_stm32_dma2d.h +++ /dev/null @@ -1,70 +0,0 @@ -/** - * @file lv_gpu_stm32_dma2d.h - * - */ - -#ifndef LV_GPU_STM32_DMA2D_H -#define LV_GPU_STM32_DMA2D_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/misc/lv_color.h" -#include "liblvgl/hal/lv_hal_disp.h" -#include "liblvgl/draw/sw/lv_draw_sw.h" - -#if LV_USE_GPU_STM32_DMA2D - -/********************* - * DEFINES - *********************/ - -#define LV_DMA2D_ARGB8888 0 -#define LV_DMA2D_RGB888 1 -#define LV_DMA2D_RGB565 2 -#define LV_DMA2D_ARGB1555 3 -#define LV_DMA2D_ARGB4444 4 - -/********************** - * TYPEDEFS - **********************/ -typedef lv_draw_sw_ctx_t lv_draw_stm32_dma2d_ctx_t; - -struct _lv_disp_drv_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Turn on the peripheral and set output color mode, this only needs to be done once - */ -void lv_draw_stm32_dma2d_init(void); - -void lv_draw_stm32_dma2d_ctx_init(struct _lv_disp_drv_t * drv, lv_draw_ctx_t * draw_ctx); - -void lv_draw_stm32_dma2d_ctx_deinit(struct _lv_disp_drv_t * drv, lv_draw_ctx_t * draw_ctx); - -void lv_draw_stm32_dma2d_blend(lv_draw_ctx_t * draw_ctx, const lv_draw_sw_blend_dsc_t * dsc); - -void lv_draw_stm32_dma2d_buffer_copy(lv_draw_ctx_t * draw_ctx, - void * dest_buf, lv_coord_t dest_stride, const lv_area_t * dest_area, - void * src_buf, lv_coord_t src_stride, const lv_area_t * src_area); - -void lv_gpu_stm32_dma2d_wait_cb(lv_draw_ctx_t * draw_ctx); - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_GPU_STM32_DMA2D*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_GPU_STM32_DMA2D_H*/ diff --git a/include/liblvgl/draw/sw/arm2d/lv_draw_sw_arm2d.h b/include/liblvgl/draw/sw/arm2d/lv_draw_sw_arm2d.h new file mode 100644 index 00000000..77c6cc4d --- /dev/null +++ b/include/liblvgl/draw/sw/arm2d/lv_draw_sw_arm2d.h @@ -0,0 +1,589 @@ +/** + * @file lv_draw_sw_arm2d.h + * + */ + +#ifndef LV_DRAW_SW_ARM2D_H +#define LV_DRAW_SW_ARM2D_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* *INDENT-OFF* */ + +/********************* + * INCLUDES + *********************/ + +#include "../../../lv_conf_internal.h" +#include "../../../misc/lv_area_private.h" + +#if LV_USE_DRAW_ARM2D_SYNC + +#define __ARM_2D_IMPL__ +#include "arm_2d.h" +#include "__arm_2d_impl.h" + +#if defined(__IS_COMPILER_ARM_COMPILER_5__) +#pragma diag_suppress 174,177,188,68,513,144,1296 +#elif defined(__IS_COMPILER_IAR__) +#pragma diag_suppress=Pa093 +#elif defined(__IS_COMPILER_GCC__) +#pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" +#endif + +/********************* + * DEFINES + *********************/ +#ifndef LV_DRAW_SW_RGB565_SWAP + #define LV_DRAW_SW_RGB565_SWAP(__buf_ptr, __buf_size_px) \ + lv_draw_sw_rgb565_swap_helium((__buf_ptr), (__buf_size_px)) +#endif + +#ifndef LV_DRAW_SW_IMAGE + #define LV_DRAW_SW_IMAGE(__transformed, \ + __cf, \ + __src_buf, \ + __img_coords, \ + __src_stride, \ + __blend_area, \ + __draw_unit, \ + __draw_dsc) \ + lv_draw_sw_image_helium( (__transformed), \ + (__cf), \ + (uint8_t *)(__src_buf), \ + (__img_coords), \ + (__src_stride), \ + (__blend_area), \ + (__draw_unit), \ + (__draw_dsc)) +#endif + +#ifndef LV_DRAW_SW_RGB565_RECOLOR + #define LV_DRAW_SW_RGB565_RECOLOR(__src_buf, __blend_area, __color, __opa) \ + lv_draw_sw_image_recolor_rgb565( (__src_buf), \ + &(__blend_area), \ + (__color), \ + (__opa)) +#endif + +#ifndef LV_DRAW_SW_RGB888_RECOLOR + #define LV_DRAW_SW_RGB888_RECOLOR( __src_buf, \ + __blend_area, \ + __color, \ + __opa, \ + __cf) \ + lv_draw_sw_image_recolor_rgb888( (__src_buf), \ + &(__blend_area), \ + (__color), \ + (__opa), \ + (__cf)) +#endif + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ +extern void arm_2d_helper_swap_rgb16(uint16_t * phwBuffer, uint32_t wCount); + +/********************** + * MACROS + **********************/ + +#define __RECOLOUR_BEGIN() \ + do { \ + lv_color_t *rgb_tmp_buf = NULL; \ + if(draw_dsc->recolor_opa > LV_OPA_MIN) { \ + if(LV_COLOR_FORMAT_RGB565 == des_cf) { \ + rgb_tmp_buf \ + = lv_malloc(src_w * src_h * sizeof(uint16_t)); \ + if (NULL == rgb_tmp_buf) { \ + LV_LOG_WARN( \ + "Failed to allocate memory for accelerating recolor, " \ + "use normal route instead."); \ + break; \ + } \ + lv_memcpy( rgb_tmp_buf, \ + src_buf, \ + src_w * src_h * sizeof(uint16_t)); \ + arm_2d_size_t copy_size = { \ + .iWidth = src_w, \ + .iHeight = src_h, \ + }; \ + /* apply re-color */ \ + __arm_2d_impl_rgb565_colour_filling_with_opacity( \ + (uint16_t *)rgb_tmp_buf, \ + src_w, \ + ©_size, \ + lv_color_to_u16(draw_dsc->recolor), \ + draw_dsc->recolor_opa); \ + \ + /* replace src_buf for the following operation */ \ + src_buf = (const uint8_t *)rgb_tmp_buf; \ + } \ + else if(LV_COLOR_FORMAT_XRGB8888 == des_cf) { \ + rgb_tmp_buf \ + = lv_malloc(src_w * src_h * sizeof(uint32_t)); \ + if (NULL == rgb_tmp_buf) { \ + LV_LOG_WARN( \ + "Failed to allocate memory for accelerating recolor, " \ + "use normal route instead."); \ + break; \ + } \ + lv_memcpy( rgb_tmp_buf, \ + src_buf, \ + src_w * src_h * sizeof(uint32_t)); \ + arm_2d_size_t copy_size = { \ + .iWidth = src_w, \ + .iHeight = src_h, \ + }; \ + /* apply re-color */ \ + __arm_2d_impl_cccn888_colour_filling_with_opacity( \ + (uint32_t *)rgb_tmp_buf, \ + src_w, \ + ©_size, \ + lv_color_to_u32(draw_dsc->recolor), \ + draw_dsc->recolor_opa); \ + \ + /* replace src_buf for the following operation */ \ + src_buf = (const uint8_t *)rgb_tmp_buf; \ + } \ + } \ + do { + +#define __RECOLOUR_END() \ + } while(0); \ + if (NULL != rgb_tmp_buf) { \ + lv_free(rgb_tmp_buf); \ + } \ + } while(0); + +static inline lv_result_t lv_draw_sw_rgb565_swap_helium(void * buf, uint32_t buf_size_px) +{ + arm_2d_helper_swap_rgb16((uint16_t *)buf, buf_size_px); + return LV_RESULT_OK; +} + +static inline lv_result_t lv_draw_sw_image_helium( + bool is_transform, + lv_color_format_t src_cf, + const uint8_t *src_buf, + const lv_area_t * coords, + int32_t src_stride, + const lv_area_t * des_area, + lv_draw_unit_t * draw_unit, + const lv_draw_image_dsc_t * draw_dsc) +{ + lv_result_t result = LV_RESULT_INVALID; + lv_layer_t * layer = draw_unit->target_layer; + lv_color_format_t des_cf = layer->color_format; + static bool arm_2d_initialized = false; + + if (!arm_2d_initialized) { + arm_2d_initialized = true; + arm_2d_init(); + } + + do { + if (!is_transform) { + break; + } + if(draw_dsc->scale_x != draw_dsc->scale_y) { + break; + } + /* filter the unsupported colour format combination */ + if((LV_COLOR_FORMAT_RGB565 == des_cf) + && !( (LV_COLOR_FORMAT_RGB565 == src_cf) + || (LV_COLOR_FORMAT_RGB565A8 == src_cf))) { + break; + } + #if 0 /* a temporary patch */ + if((LV_COLOR_FORMAT_XRGB8888 == des_cf) + && !( (LV_COLOR_FORMAT_ARGB8888 == src_cf) + || (LV_COLOR_FORMAT_XRGB8888 == src_cf))) { + break; + } + #else + if((LV_COLOR_FORMAT_XRGB8888 == des_cf) + || (LV_COLOR_FORMAT_RGB888 == des_cf) + || (LV_COLOR_FORMAT_ARGB8888 == des_cf)) { + break; + } + #endif + + /* ------------- prepare parameters for arm-2d APIs - BEGIN --------- */ + + lv_area_t blend_area; + if(!lv_area_intersect(&blend_area, des_area, draw_unit->clip_area)) { + break; + } + + int32_t src_w = lv_area_get_width(coords); + int32_t src_h = lv_area_get_height(coords); + + arm_2d_size_t src_size = { + .iWidth = (int16_t)src_w, + .iHeight = (int16_t)src_h, + }; + +// arm_2d_size_t des_size; + +// do{ +// int32_t des_w = lv_area_get_width(&blend_area); +// int32_t des_h = lv_area_get_height(&blend_area); + +// LV_ASSERT(des_w <= INT16_MAX); +// LV_ASSERT(des_h <= INT16_MAX); + +// des_size.iWidth = (int16_t)des_w; +// des_size.iHeight = (int16_t)des_h; +// } while(0); +// +// arm_2d_size_t copy_size = { +// .iWidth = MIN(des_size.iWidth, src_size.iWidth), +// .iHeight = MIN(des_size.iHeight, src_size.iHeight), +// }; +// +// int32_t des_stride = lv_draw_buf_width_to_stride( +// lv_area_get_width(&layer->buf_area), +// des_cf); +// uint8_t *des_buf_moved = (uint8_t *)lv_draw_layer_go_to_xy( +// layer, +// blend_area.x1 - layer->buf_area.x1, +// blend_area.y1 - layer->buf_area.y1); + uint8_t *des_buf = (uint8_t *)lv_draw_layer_go_to_xy(layer, 0, 0); + uint8_t opa = draw_dsc->opa; + + /* ------------- prepare parameters for arm-2d APIs - END ----------- */ + __RECOLOUR_BEGIN() + + static arm_2d_tile_t target_tile_origin; + static arm_2d_tile_t target_tile; + arm_2d_region_t clip_region; + static arm_2d_region_t target_region; + + target_region = (arm_2d_region_t) { + .tLocation = { + .iX = (int16_t)(coords->x1 - draw_unit->clip_area->x1), + .iY = (int16_t)(coords->y1 - draw_unit->clip_area->y1), + }, + .tSize = src_size, + }; + + target_tile_origin = (arm_2d_tile_t) { + .tRegion = { + .tSize = { + .iWidth = (int16_t)lv_area_get_width(&layer->buf_area), + .iHeight = (int16_t)lv_area_get_height(&layer->buf_area), + }, + }, + .tInfo = { + .bIsRoot = true, + }, + .phwBuffer = (uint16_t *)des_buf, + }; + + clip_region = (arm_2d_region_t) { + .tLocation = { + .iX = (int16_t)(draw_unit->clip_area->x1 - layer->buf_area.x1), + .iY = (int16_t)(draw_unit->clip_area->y1 - layer->buf_area.y1), + }, + .tSize = { + .iWidth = (int16_t)lv_area_get_width(draw_unit->clip_area), + .iHeight = (int16_t)lv_area_get_height(draw_unit->clip_area), + }, + }; + + arm_2d_tile_generate_child(&target_tile_origin, + &clip_region, + &target_tile, + false); + + static arm_2d_tile_t source_tile; + + source_tile = (arm_2d_tile_t) { + .tRegion = { + .tSize = { + .iWidth = (int16_t)src_w, + .iHeight = (int16_t)src_h, + }, + }, + .tInfo = { + .bIsRoot = true, + }, + .pchBuffer = (uint8_t *)src_buf, + }; + + static arm_2d_location_t source_center, target_center; + source_center.iX = draw_dsc->pivot.x; + source_center.iY = draw_dsc->pivot.y; + target_center = target_region.tLocation; + target_center.iX += draw_dsc->pivot.x; + target_center.iY += draw_dsc->pivot.y; + + if(LV_COLOR_FORMAT_A8 == src_cf) { + + source_tile.tInfo.bHasEnforcedColour = true; + source_tile.tInfo.tColourInfo.chScheme = ARM_2D_COLOUR_GRAY8; + + if(LV_COLOR_FORMAT_RGB565 == des_cf) { + + arm_2d_rgb565_fill_colour_with_mask_opacity_and_transform( + &source_tile, + &target_tile, + NULL, + source_center, + ARM_2D_ANGLE((draw_dsc->rotation / 10.0f)), + draw_dsc->scale_x / 256.0f, + lv_color_to_u16(draw_dsc->recolor), + opa, + &target_center + ); + + } + else if(LV_COLOR_FORMAT_XRGB8888 == des_cf) { + arm_2d_cccn888_fill_colour_with_mask_opacity_and_transform( + &source_tile, + &target_tile, + NULL, + source_center, + ARM_2D_ANGLE((draw_dsc->rotation / 10.0f)), + draw_dsc->scale_x / 256.0f, + lv_color_to_int(draw_dsc->recolor), + opa, + &target_center + ); + } + else { + break; + } + + } + else if(LV_COLOR_FORMAT_RGB565A8 == src_cf) { + LV_ASSERT(LV_COLOR_FORMAT_RGB565 == des_cf); + + /* mask_buf = src_buf + src_stride * src_w / header->w * src_h; */ + const uint8_t *mask_buf = src_buf + src_stride * src_h; + int32_t mask_stride = src_stride / 2; + + static arm_2d_tile_t mask_tile; + mask_tile = source_tile; + + mask_tile.tInfo.bHasEnforcedColour = true; + mask_tile.tInfo.tColourInfo.chScheme = ARM_2D_COLOUR_GRAY8; + mask_tile.pchBuffer = (uint8_t *)mask_buf; + + if(opa >= LV_OPA_MAX) { + arm_2d_rgb565_tile_transform_with_src_mask( + &source_tile, + &mask_tile, + &target_tile, + NULL, + source_center, + ARM_2D_ANGLE((draw_dsc->rotation / 10.0f)), + draw_dsc->scale_x / 256.0f, + &target_center + ); + } + else { + arm_2d_rgb565_tile_transform_with_src_mask_and_opacity( + &source_tile, + &mask_tile, + &target_tile, + NULL, + source_center, + ARM_2D_ANGLE((draw_dsc->rotation / 10.0f)), + draw_dsc->scale_x / 256.0f, + opa, + &target_center + ); + } + + } + else if(LV_COLOR_FORMAT_RGB565 == src_cf) { + LV_ASSERT(LV_COLOR_FORMAT_RGB565 == des_cf); + + if(opa >= LV_OPA_MAX) { + #if ARM_2D_VERSION >= 10106 + arm_2d_rgb565_tile_transform_only( + &source_tile, + &target_tile, + NULL, + source_center, + ARM_2D_ANGLE((draw_dsc->rotation / 10.0f)), + draw_dsc->scale_x / 256.0f, + &target_center); + #else + + arm_2dp_rgb565_tile_transform_only_prepare( + NULL, + &source_tile, + source_center, + ARM_2D_ANGLE((draw_dsc->rotation / 10.0f)), + (float)(draw_dsc->scale_x / 256.0f)); + + arm_2dp_tile_transform(NULL, + &target_tile, + NULL, + &target_center); + #endif + } + else { + arm_2d_rgb565_tile_transform_only_with_opacity( + &source_tile, + &target_tile, + NULL, + source_center, + ARM_2D_ANGLE((draw_dsc->rotation / 10.0f)), + draw_dsc->scale_x / 256.0f, + opa, + &target_center + ); + } + + } + #if 0 /* a temporary patch */ + else if(LV_COLOR_FORMAT_ARGB8888 == src_cf) { + LV_ASSERT(LV_COLOR_FORMAT_XRGB8888 == des_cf); + + static arm_2d_tile_t mask_tile; + mask_tile = source_tile; + + mask_tile.tInfo.bHasEnforcedColour = true; + mask_tile.tInfo.tColourInfo.chScheme = ARM_2D_CHANNEL_8in32; + mask_tile.pchBuffer = (uint8_t *)src_buf + 3; + + if(opa >= LV_OPA_MAX) { + arm_2d_cccn888_tile_transform_with_src_mask( + &source_tile, + &mask_tile, + &target_tile, + NULL, + source_center, + ARM_2D_ANGLE((draw_dsc->rotation / 10.0f)), + draw_dsc->scale_x / 256.0f, + &target_center + ); + } + else { + arm_2d_cccn888_tile_transform_with_src_mask_and_opacity( + &source_tile, + &mask_tile, + &target_tile, + NULL, + source_center, + ARM_2D_ANGLE((draw_dsc->rotation / 10.0f)), + draw_dsc->scale_x / 256.0f, + opa, + &target_center + ); + } + + } + else if(LV_COLOR_FORMAT_XRGB8888 == src_cf) { + LV_ASSERT(LV_COLOR_FORMAT_XRGB8888 == des_cf); + + if(opa >= LV_OPA_MAX) { + arm_2d_cccn888_tile_transform_only( + &source_tile, + &target_tile, + NULL, + source_center, + ARM_2D_ANGLE((draw_dsc->rotation / 10.0f)), + draw_dsc->scale_x / 256.0f, + &target_center + ); + } + else { + arm_2d_cccn888_tile_transform_only_with_opacity( + &source_tile, + &target_tile, + NULL, + source_center, + ARM_2D_ANGLE((draw_dsc->rotation / 10.0f)), + draw_dsc->scale_x / 256.0f, + opa, + &target_center + ); + } + + } + #endif + else { + break; + } + + result = LV_RESULT_OK; + + __RECOLOUR_END() + } while(0); + + return result; +} + +static inline lv_result_t lv_draw_sw_image_recolor_rgb565( + const uint8_t *src_buf, + const lv_area_t * blend_area, + lv_color_t color, + lv_opa_t opa) +{ + int32_t src_w = lv_area_get_width(blend_area); + int32_t src_h = lv_area_get_height(blend_area); + + arm_2d_size_t copy_size = { + .iWidth = (int16_t)src_w, + .iHeight = (int16_t)src_h, + }; + + __arm_2d_impl_rgb565_colour_filling_with_opacity( + (uint16_t *)src_buf, + src_w, + ©_size, + lv_color_to_u16(color), + opa); + + return LV_RESULT_OK; +} + +static inline lv_result_t lv_draw_sw_image_recolor_rgb888( + const uint8_t *src_buf, + const lv_area_t * blend_area, + lv_color_t color, + lv_opa_t opa, + lv_color_format_t src_cf) +{ + if(LV_COLOR_FORMAT_XRGB8888 != src_cf) { + return LV_RESULT_INVALID; + } + + int32_t src_w = lv_area_get_width(blend_area); + int32_t src_h = lv_area_get_height(blend_area); + + arm_2d_size_t copy_size = { + .iWidth = (int16_t)src_w, + .iHeight = (int16_t)src_h, + }; + + __arm_2d_impl_cccn888_colour_filling_with_opacity( + (uint32_t *)src_buf, + src_w, + ©_size, + lv_color_to_u32(color), + opa); + + return LV_RESULT_OK; +} + +#endif /* LV_USE_DRAW_ARM2D_SYNC */ + +/* *INDENT-ON* */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_SW_ARM2D_H */ diff --git a/include/liblvgl/draw/sw/arm2d/lv_draw_sw_helium.h b/include/liblvgl/draw/sw/arm2d/lv_draw_sw_helium.h new file mode 100644 index 00000000..c35a06eb --- /dev/null +++ b/include/liblvgl/draw/sw/arm2d/lv_draw_sw_helium.h @@ -0,0 +1,60 @@ +/** + * @file lv_draw_sw_helium.h + * + */ + +#ifndef LV_DRAW_SW_HELIUM_H +#define LV_DRAW_SW_HELIUM_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* *INDENT-OFF* */ + +/********************* + * INCLUDES + *********************/ + +#include "../../../lv_conf_internal.h" + +/* detect whether helium is available based on arm compilers' standard */ +#if defined(__ARM_FEATURE_MVE) && __ARM_FEATURE_MVE + +#ifdef LV_DRAW_SW_HELIUM_CUSTOM_INCLUDE +#include LV_DRAW_SW_HELIUM_CUSTOM_INCLUDE +#endif + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************* + * POST INCLUDES + *********************/ +/* use arm-2d as the default helium acceleration */ +#include "lv_draw_sw_arm2d.h" + +#endif /* defined(__ARM_FEATURE_MVE) && __ARM_FEATURE_MVE */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_SW_HELIUM_H*/ diff --git a/include/liblvgl/draw/sw/blend/arm2d/lv_blend_arm2d.h b/include/liblvgl/draw/sw/blend/arm2d/lv_blend_arm2d.h new file mode 100644 index 00000000..0eb29b85 --- /dev/null +++ b/include/liblvgl/draw/sw/blend/arm2d/lv_blend_arm2d.h @@ -0,0 +1,937 @@ +/** + * @file lv_blend_arm2d.h + * + */ + +#ifndef LV_BLEND_ARM2D_H +#define LV_BLEND_ARM2D_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../../../lv_conf_internal.h" + +#if LV_USE_DRAW_ARM2D_SYNC + +#define __ARM_2D_IMPL__ +#include "arm_2d.h" +#include "__arm_2d_impl.h" + +#if defined(__IS_COMPILER_ARM_COMPILER_5__) +#pragma diag_suppress 174,177,188,68,513,144,1296 +#elif defined(__IS_COMPILER_IAR__) +#pragma diag_suppress=Pa093 +#elif defined(__IS_COMPILER_GCC__) +#pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" +#endif + + +#if ARM_2D_VERSION < 10106ul +#error Please upgrade to Arm-2D v1.1.6 or above +#endif + +#ifndef LV_ARM2D_XRGB888_ALPHA_ALWAYS_FF +#define LV_ARM2D_XRGB888_ALPHA_ALWAYS_FF 1 +#endif + +/********************* + * DEFINES + *********************/ + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_RGB565 +#define LV_DRAW_SW_COLOR_BLEND_TO_RGB565(dsc) \ + lv_color_blend_to_rgb565_arm2d(dsc) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_RGB565_WITH_OPA +#define LV_DRAW_SW_COLOR_BLEND_TO_RGB565_WITH_OPA(dsc) \ + lv_color_blend_to_rgb565_with_opa_arm2d(dsc) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_RGB565_WITH_MASK +#define LV_DRAW_SW_COLOR_BLEND_TO_RGB565_WITH_MASK(dsc) \ + lv_color_blend_to_rgb565_with_mask_arm2d(dsc) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_RGB565_MIX_MASK_OPA +#define LV_DRAW_SW_COLOR_BLEND_TO_RGB565_MIX_MASK_OPA(dsc) \ + lv_color_blend_to_rgb565_mix_mask_opa_arm2d(dsc) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB565 +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB565(dsc) \ + lv_rgb565_blend_normal_to_rgb565_arm2d(dsc) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB565_WITH_OPA +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB565_WITH_OPA(dsc) \ + lv_rgb565_blend_normal_to_rgb565_with_opa_arm2d(dsc) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB565_WITH_MASK +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB565_WITH_MASK(dsc) \ + lv_rgb565_blend_normal_to_rgb565_with_mask_arm2d(dsc) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB565_MIX_MASK_OPA +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB565_MIX_MASK_OPA(dsc) \ + lv_rgb565_blend_normal_to_rgb565_mix_mask_opa_arm2d(dsc) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB565 +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB565(dsc, src_px_size) \ + lv_rgb888_blend_normal_to_rgb565_arm2d(dsc, src_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB565_WITH_OPA +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB565_WITH_OPA(dsc, src_px_size) \ + lv_rgb888_blend_normal_to_rgb565_with_opa_arm2d(dsc, src_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB565_WITH_MASK +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB565_WITH_MASK(dsc, src_px_size) \ + lv_rgb888_blend_normal_to_rgb565_with_mask_arm2d(dsc, src_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB565_MIX_MASK_OPA +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB565_MIX_MASK_OPA(dsc, src_px_size) \ + lv_rgb888_blend_normal_to_rgb565_mix_mask_opa_arm2d(dsc, src_px_size) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB565 +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB565(dsc) \ + lv_argb8888_blend_normal_to_rgb565_arm2d(dsc) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB565_WITH_OPA +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB565_WITH_OPA(dsc) \ + lv_argb8888_blend_normal_to_rgb565_with_opa_arm2d(dsc) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB565_WITH_MASK +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB565_WITH_MASK(dsc) \ + lv_argb8888_blend_normal_to_rgb565_with_mask_arm2d(dsc) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB565_MIX_MASK_OPA +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB565_MIX_MASK_OPA(dsc) \ + lv_argb8888_blend_normal_to_rgb565_mix_mask_opa_arm2d(dsc) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_RGB888 +#define LV_DRAW_SW_COLOR_BLEND_TO_RGB888(dsc, dst_px_size) \ + lv_color_blend_to_rgb888_arm2d(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_RGB888_WITH_OPA +#define LV_DRAW_SW_COLOR_BLEND_TO_RGB888_WITH_OPA(dsc, dst_px_size) \ + lv_color_blend_to_rgb888_with_opa_arm2d(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_RGB888_WITH_MASK +#define LV_DRAW_SW_COLOR_BLEND_TO_RGB888_WITH_MASK(dsc, dst_px_size) \ + lv_color_blend_to_rgb888_with_mask_arm2d(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_RGB888_MIX_MASK_OPA +#define LV_DRAW_SW_COLOR_BLEND_TO_RGB888_MIX_MASK_OPA(dsc, dst_px_size) \ + lv_color_blend_to_rgb888_mix_mask_opa_arm2d(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB888 +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB888(dsc, dst_px_size) \ + lv_rgb565_blend_normal_to_rgb888_arm2d(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB888_WITH_OPA +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB888_WITH_OPA(dsc, dst_px_size) \ + lv_rgb565_blend_normal_to_rgb888_with_opa_arm2d(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB888_WITH_MASK +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB888_WITH_MASK(dsc, dst_px_size) \ + lv_rgb565_blend_normal_to_rgb888_with_mask_arm2d(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB888_MIX_MASK_OPA +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB888_MIX_MASK_OPA(dsc, dst_px_size) \ + lv_rgb565_blend_normal_to_rgb888_mix_mask_opa_arm2d(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB888 +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB888(dsc, dst_px_size, src_px_size) \ + lv_rgb888_blend_normal_to_rgb888_arm2d(dsc, dst_px_size, src_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB888_WITH_OPA +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB888_WITH_OPA(dsc, dst_px_size, src_px_size) \ + lv_rgb888_blend_normal_to_rgb888_with_opa_arm2d(dsc, dst_px_size, src_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB888_WITH_MASK +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB888_WITH_MASK(dsc, dst_px_size, src_px_size) \ + lv_rgb888_blend_normal_to_rgb888_with_mask_arm2d(dsc, dst_px_size, src_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB888_MIX_MASK_OPA +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB888_MIX_MASK_OPA(dsc, dst_px_size, src_px_size) \ + lv_rgb888_blend_normal_to_rgb888_mix_mask_opa_arm2d(dsc, dst_px_size, src_px_size) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB888 +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB888(dsc, dst_px_size) \ + lv_argb8888_blend_normal_to_rgb888_arm2d(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB888_WITH_OPA +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB888_WITH_OPA(dsc, dst_px_size) \ + lv_argb8888_blend_normal_to_rgb888_with_opa_arm2d(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB888_WITH_MASK +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB888_WITH_MASK(dsc, dst_px_size) \ + lv_argb8888_blend_normal_to_rgb888_with_mask_arm2d(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB888_MIX_MASK_OPA +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB888_MIX_MASK_OPA(dsc, dst_px_size) \ + lv_argb8888_blend_normal_to_rgb888_mix_mask_opa_arm2d(dsc, dst_px_size) +#endif + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +static inline lv_result_t lv_color_blend_to_rgb565_arm2d(lv_draw_sw_blend_fill_dsc_t * dsc) +{ + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t stride = (dsc->dest_stride) / sizeof(uint16_t); + __arm_2d_impl_rgb16_colour_filling((uint16_t *)dsc->dest_buf, + stride, + &draw_size, + lv_color_to_u16(dsc->color)); + return LV_RESULT_OK; +} + +static inline lv_result_t lv_color_blend_to_rgb565_with_opa_arm2d(lv_draw_sw_blend_fill_dsc_t * dsc) +{ + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t stride = (dsc->dest_stride) / sizeof(uint16_t); + __arm_2d_impl_rgb565_colour_filling_with_opacity((uint16_t *)dsc->dest_buf, + stride, + &draw_size, + lv_color_to_u16(dsc->color), + dsc->opa); + return LV_RESULT_OK; +} + +static inline lv_result_t lv_color_blend_to_rgb565_with_mask_arm2d(lv_draw_sw_blend_fill_dsc_t * dsc) +{ + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t stride = (dsc->dest_stride) / sizeof(uint16_t); + __arm_2d_impl_rgb565_colour_filling_mask((uint16_t *)dsc->dest_buf, + stride, + (uint8_t *)dsc->mask_buf, + dsc->mask_stride, + &draw_size, + lv_color_to_u16(dsc->color)); + return LV_RESULT_OK; +} + +static inline lv_result_t lv_color_blend_to_rgb565_mix_mask_opa_arm2d(lv_draw_sw_blend_fill_dsc_t * dsc) +{ + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t stride = (dsc->dest_stride) / sizeof(uint16_t); + __arm_2d_impl_rgb565_colour_filling_mask_opacity((uint16_t *)dsc->dest_buf, + stride, + (uint8_t *)dsc->mask_buf, + dsc->mask_stride, + &draw_size, + lv_color_to_u16(dsc->color), + dsc->opa); + return LV_RESULT_OK; +} + +static inline lv_result_t lv_rgb565_blend_normal_to_rgb565_arm2d(lv_draw_sw_blend_image_dsc_t * dsc) +{ + + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t des_stride = dsc->dest_stride / sizeof(uint16_t); + int16_t src_stride = dsc->src_stride / sizeof(uint16_t); + __arm_2d_impl_rgb16_copy((uint16_t *)dsc->src_buf, + src_stride, + (uint16_t *)dsc->dest_buf, + des_stride, + &draw_size); + return LV_RESULT_OK; +} + +static inline lv_result_t lv_rgb565_blend_normal_to_rgb565_with_opa_arm2d(lv_draw_sw_blend_image_dsc_t * dsc) +{ + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t des_stride = dsc->dest_stride / sizeof(uint16_t); + int16_t src_stride = dsc->src_stride / sizeof(uint16_t); + __arm_2d_impl_rgb565_tile_copy_opacity((uint16_t *)dsc->src_buf, + src_stride, + (uint16_t *)dsc->dest_buf, + des_stride, + &draw_size, + dsc->opa); + return LV_RESULT_OK; +} + +static inline lv_result_t lv_rgb565_blend_normal_to_rgb565_with_mask_arm2d(lv_draw_sw_blend_image_dsc_t * dsc) +{ + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t des_stride = dsc->dest_stride / sizeof(uint16_t); + int16_t src_stride = dsc->src_stride / sizeof(uint16_t); + __arm_2d_impl_rgb565_src_msk_copy((uint16_t *)dsc->src_buf, + src_stride, + (uint8_t *)dsc->mask_buf, + dsc->mask_stride, + &draw_size, + (uint16_t *)dsc->dest_buf, + des_stride, + &draw_size); + return LV_RESULT_OK; +} + +static inline lv_result_t lv_rgb565_blend_normal_to_rgb565_mix_mask_opa_arm2d(lv_draw_sw_blend_image_dsc_t * dsc) +{ + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t des_stride = dsc->dest_stride / sizeof(uint16_t); + int16_t src_stride = dsc->src_stride / sizeof(uint16_t); + + __arm_2d_impl_rgb565_tile_copy_with_src_mask_and_opacity((uint16_t *)dsc->src_buf, + src_stride, + (uint8_t *)dsc->mask_buf, + dsc->mask_stride, + &draw_size, + (uint16_t *)dsc->dest_buf, + des_stride, + &draw_size, + dsc->opa); + return LV_RESULT_OK; +} + +static inline lv_result_t lv_rgb888_blend_normal_to_rgb565_arm2d(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t src_px_size) +{ + if(src_px_size == 3) { + return LV_RESULT_INVALID; + } + + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t des_stride = dsc->dest_stride / sizeof(uint16_t); + int16_t src_stride = dsc->src_stride / sizeof(uint32_t); + + __arm_2d_impl_cccn888_to_rgb565((uint32_t *)dsc->src_buf, + src_stride, + (uint16_t *)dsc->dest_buf, + des_stride, + &draw_size); + + return LV_RESULT_OK; +} + +static inline lv_result_t lv_rgb888_blend_normal_to_rgb565_with_opa_arm2d(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t src_px_size) +{ + if(src_px_size == 3) { + return LV_RESULT_INVALID; + } + + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t des_stride = dsc->dest_stride / sizeof(uint16_t); + int16_t src_stride = dsc->src_stride / sizeof(uint32_t); + + uint16_t * tmp_buf = (uint16_t *)lv_malloc(dsc->dest_stride * dsc->dest_h); + if(NULL == tmp_buf) { + return LV_RESULT_INVALID; + } + +#if !LV_ARM2D_XRGB888_ALPHA_ALWAYS_FF + __arm_2d_impl_cccn888_to_rgb565((uint32_t *)dsc->src_buf, + src_stride, + (uint16_t *)tmp_buf, + des_stride, + &draw_size); + + __arm_2d_impl_rgb565_tile_copy_opacity(tmp_buf, + des_stride, + (uint16_t *)dsc->dest_buf, + des_stride, + &draw_size, + dsc->opa); +#else + __arm_2d_impl_ccca8888_tile_copy_to_rgb565_with_opacity((uint32_t *)dsc->src_buf, + src_stride, + (uint16_t *)dsc->dest_buf, + des_stride, + &draw_size, + dsc->opa); +#endif + lv_free(tmp_buf); + return LV_RESULT_OK; +} + +static inline lv_result_t lv_rgb888_blend_normal_to_rgb565_with_mask_arm2d(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t src_px_size) +{ + if(src_px_size == 3) { + return LV_RESULT_INVALID; + } + + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t des_stride = dsc->dest_stride / sizeof(uint16_t); + int16_t src_stride = dsc->src_stride / sizeof(uint32_t); + + uint16_t * tmp_buf = (uint16_t *)lv_malloc(dsc->dest_stride * dsc->dest_h); + if(NULL == tmp_buf) { + return LV_RESULT_INVALID; + } + +#if !LV_ARM2D_XRGB888_ALPHA_ALWAYS_FF + __arm_2d_impl_cccn888_to_rgb565((uint32_t *)dsc->src_buf, + src_stride, + (uint16_t *)tmp_buf, + des_stride, + &draw_size); + + __arm_2d_impl_rgb565_src_msk_copy(tmp_buf, + des_stride, + (uint8_t *)dsc->mask_buf, + dsc->mask_stride, + &draw_size, + (uint16_t *)dsc->dest_buf, + des_stride, + &draw_size); +#else + __arm_2d_impl_ccca8888_tile_copy_to_rgb565_with_src_mask((uint32_t *)dsc->src_buf, + src_stride, + (uint8_t *)dsc->mask_buf, + dsc->mask_stride, + &draw_size, + (uint16_t *)dsc->dest_buf, + des_stride, + &draw_size); +#endif + + lv_free(tmp_buf); + return LV_RESULT_OK; +} + +static inline lv_result_t lv_rgb888_blend_normal_to_rgb565_mix_mask_opa_arm2d(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t src_px_size) +{ + if(src_px_size == 3) { + return LV_RESULT_INVALID; + } + + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t des_stride = dsc->dest_stride / sizeof(uint16_t); + int16_t src_stride = dsc->src_stride / sizeof(uint32_t); + + uint16_t * tmp_buf = (uint16_t *)lv_malloc(dsc->dest_stride * dsc->dest_h); + if(NULL == tmp_buf) { + return LV_RESULT_INVALID; + } + +#if !LV_ARM2D_XRGB888_ALPHA_ALWAYS_FF + __arm_2d_impl_cccn888_to_rgb565((uint32_t *)dsc->src_buf, + src_stride, + (uint16_t *)tmp_buf, + des_stride, + &draw_size); + + __arm_2d_impl_rgb565_tile_copy_with_src_mask_and_opacity(tmp_buf, + des_stride, + (uint8_t *)dsc->mask_buf, + dsc->mask_stride, + &draw_size, + (uint16_t *)dsc->dest_buf, + des_stride, + &draw_size, + dsc->opa); +#else + __arm_2d_impl_ccca8888_tile_copy_to_rgb565_with_src_mask_and_opacity((uint32_t *)dsc->src_buf, + src_stride, + (uint8_t *)dsc->mask_buf, + dsc->mask_stride, + &draw_size, + (uint16_t *)dsc->dest_buf, + des_stride, + &draw_size, + dsc->opa); +#endif + + lv_free(tmp_buf); + return LV_RESULT_OK; +} + +static inline lv_result_t lv_argb8888_blend_normal_to_rgb565_arm2d(lv_draw_sw_blend_image_dsc_t * dsc) +{ + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t des_stride = dsc->dest_stride / sizeof(uint16_t); + int16_t src_stride = dsc->src_stride / sizeof(uint32_t); + + __arm_2d_impl_ccca8888_to_rgb565((uint32_t *)dsc->src_buf, + src_stride, + (uint16_t *)dsc->dest_buf, + des_stride, + &draw_size); + + return LV_RESULT_OK; +} + +static inline lv_result_t lv_argb8888_blend_normal_to_rgb565_with_opa_arm2d(lv_draw_sw_blend_image_dsc_t * dsc) +{ + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t des_stride = dsc->dest_stride / sizeof(uint16_t); + int16_t src_stride = dsc->src_stride / sizeof(uint32_t); + + __arm_2d_impl_ccca8888_tile_copy_to_rgb565_with_opacity((uint32_t *)dsc->src_buf, + src_stride, + (uint16_t *)dsc->dest_buf, + des_stride, + &draw_size, + dsc->opa); + + return LV_RESULT_OK; +} + +static inline lv_result_t lv_argb8888_blend_normal_to_rgb565_with_mask_arm2d(lv_draw_sw_blend_image_dsc_t * dsc) +{ + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t des_stride = dsc->dest_stride / sizeof(uint16_t); + int16_t src_stride = dsc->src_stride / sizeof(uint32_t); + + __arm_2d_impl_ccca8888_tile_copy_to_rgb565_with_src_mask((uint32_t *)dsc->src_buf, + src_stride, + (uint8_t *)dsc->mask_buf, + dsc->mask_stride, + &draw_size, + (uint16_t *)dsc->dest_buf, + des_stride, + &draw_size); + + return LV_RESULT_OK; +} + +static inline lv_result_t lv_argb8888_blend_normal_to_rgb565_mix_mask_opa_arm2d(lv_draw_sw_blend_image_dsc_t * dsc) +{ + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t des_stride = dsc->dest_stride / sizeof(uint16_t); + int16_t src_stride = dsc->src_stride / sizeof(uint32_t); + + __arm_2d_impl_ccca8888_tile_copy_to_rgb565_with_src_mask_and_opacity((uint32_t *)dsc->src_buf, + src_stride, + (uint8_t *)dsc->mask_buf, + dsc->mask_stride, + &draw_size, + (uint16_t *)dsc->dest_buf, + des_stride, + &draw_size, + dsc->opa); + + return LV_RESULT_OK; +} + +static inline lv_result_t lv_color_blend_to_rgb888_arm2d(lv_draw_sw_blend_fill_dsc_t * dsc, uint32_t dst_px_size) +{ + if(dst_px_size == 3) { + return LV_RESULT_INVALID; + } + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t stride = (dsc->dest_stride) / sizeof(uint32_t); + __arm_2d_impl_rgb32_colour_filling((uint32_t *)dsc->dest_buf, + stride, + &draw_size, + lv_color_to_u32(dsc->color)); + return LV_RESULT_OK; + +} + +static inline lv_result_t lv_color_blend_to_rgb888_with_opa_arm2d(lv_draw_sw_blend_fill_dsc_t * dsc, + uint32_t dst_px_size) +{ + if(dst_px_size == 3) { + return LV_RESULT_INVALID; + } + + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t stride = (dsc->dest_stride) / sizeof(uint32_t); + __arm_2d_impl_cccn888_colour_filling_with_opacity((uint32_t *)dsc->dest_buf, + stride, + &draw_size, + lv_color_to_u32(dsc->color), + dsc->opa); + return LV_RESULT_OK; +} + +static inline lv_result_t lv_color_blend_to_rgb888_with_mask_arm2d(lv_draw_sw_blend_fill_dsc_t * dsc, + uint32_t dst_px_size) +{ + + if(dst_px_size == 3) { + return LV_RESULT_INVALID; + } + + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t stride = (dsc->dest_stride) / sizeof(uint32_t); + __arm_2d_impl_cccn888_colour_filling_mask((uint32_t *)dsc->dest_buf, + stride, + (uint8_t *)dsc->mask_buf, + dsc->mask_stride, + &draw_size, + lv_color_to_u32(dsc->color)); + return LV_RESULT_OK; +} + +static inline lv_result_t lv_color_blend_to_rgb888_mix_mask_opa_arm2d(lv_draw_sw_blend_fill_dsc_t * dsc, + uint32_t dst_px_size) +{ + + if(dst_px_size == 3) { + return LV_RESULT_INVALID; + } + + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t stride = (dsc->dest_stride) / sizeof(uint32_t); + __arm_2d_impl_cccn888_colour_filling_mask_opacity((uint32_t *)dsc->dest_buf, + stride, + (uint8_t *)dsc->mask_buf, + dsc->mask_stride, + &draw_size, + lv_color_to_u32(dsc->color), + dsc->opa); + return LV_RESULT_OK; +} + +static inline lv_result_t lv_rgb565_blend_normal_to_rgb888_arm2d(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size) +{ + + if(dst_px_size == 3) { + return LV_RESULT_INVALID; + } + + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t des_stride = dsc->dest_stride / sizeof(uint32_t); + int16_t src_stride = dsc->src_stride / sizeof(uint16_t); + + __arm_2d_impl_rgb565_to_cccn888((uint16_t *)dsc->src_buf, + src_stride, + (uint32_t *)dsc->dest_buf, + des_stride, + &draw_size); + + return LV_RESULT_OK; + +} + +static inline lv_result_t lv_rgb565_blend_normal_to_rgb888_with_opa_arm2d(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size) +{ + + if(dst_px_size == 3) { + return LV_RESULT_INVALID; + } + + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t des_stride = dsc->dest_stride / sizeof(uint32_t); + int16_t src_stride = dsc->src_stride / sizeof(uint16_t); + + uint32_t * tmp_buf = (uint32_t *)lv_malloc(dsc->dest_stride * dsc->dest_h); + if(NULL == tmp_buf) { + return LV_RESULT_INVALID; + } + + /* get rgb565 */ + __arm_2d_impl_rgb565_to_cccn888((uint16_t *)dsc->src_buf, + src_stride, + (uint32_t *)tmp_buf, + des_stride, + &draw_size); + + __arm_2d_impl_cccn888_tile_copy_opacity(tmp_buf, + des_stride, + (uint32_t *)dsc->dest_buf, + des_stride, + &draw_size, + dsc->opa); + + lv_free(tmp_buf); + return LV_RESULT_OK; +} + +static inline lv_result_t lv_rgb565_blend_normal_to_rgb888_with_mask_arm2d(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size) +{ + + if(dst_px_size == 3) { + return LV_RESULT_INVALID; + } + + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t des_stride = dsc->dest_stride / sizeof(uint32_t); + int16_t src_stride = dsc->src_stride / sizeof(uint16_t); + + uint32_t * tmp_buf = (uint32_t *)lv_malloc(dsc->dest_stride * dsc->dest_h); + if(NULL == tmp_buf) { + return LV_RESULT_INVALID; + } + + __arm_2d_impl_rgb565_to_cccn888((uint16_t *)dsc->src_buf, + src_stride, + (uint32_t *)tmp_buf, + des_stride, + &draw_size); + + __arm_2d_impl_cccn888_src_msk_copy(tmp_buf, + des_stride, + (uint8_t *)dsc->mask_buf, + dsc->mask_stride, + &draw_size, + (uint32_t *)dsc->dest_buf, + des_stride, + &draw_size); + + lv_free(tmp_buf); + return LV_RESULT_OK; +} + +static inline lv_result_t lv_rgb565_blend_normal_to_rgb888_mix_mask_opa_arm2d(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size) +{ + + if(dst_px_size == 3) { + return LV_RESULT_INVALID; + } + + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t des_stride = dsc->dest_stride / sizeof(uint32_t); + int16_t src_stride = dsc->src_stride / sizeof(uint16_t); + + uint32_t * tmp_buf = (uint32_t *)lv_malloc(dsc->dest_stride * dsc->dest_h); + if(NULL == tmp_buf) { + return LV_RESULT_INVALID; + } + + __arm_2d_impl_rgb565_to_cccn888((uint16_t *)dsc->src_buf, + src_stride, + (uint32_t *)tmp_buf, + des_stride, + &draw_size); + + __arm_2d_impl_cccn888_tile_copy_with_src_mask_and_opacity(tmp_buf, + des_stride, + (uint8_t *)dsc->mask_buf, + dsc->mask_stride, + &draw_size, + (uint32_t *)dsc->dest_buf, + des_stride, + &draw_size, + dsc->opa); + + lv_free(tmp_buf); + return LV_RESULT_OK; +} + +static inline lv_result_t lv_rgb888_blend_normal_to_rgb888_arm2d(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size, + uint32_t src_px_size) +{ + if((dst_px_size == 3) || (src_px_size == 3)) { + return LV_RESULT_INVALID; + } + + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t des_stride = dsc->dest_stride / sizeof(uint32_t); + int16_t src_stride = dsc->src_stride / sizeof(uint32_t); + + __arm_2d_impl_rgb32_copy((uint32_t *)dsc->src_buf, + src_stride, + (uint32_t *)dsc->dest_buf, + des_stride, + &draw_size); + + return LV_RESULT_OK; +} + +static inline lv_result_t lv_rgb888_blend_normal_to_rgb888_with_opa_arm2d(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size, uint32_t src_px_size) +{ + if((dst_px_size == 3) || (src_px_size == 3)) { + return LV_RESULT_INVALID; + } + + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t des_stride = dsc->dest_stride / sizeof(uint32_t); + int16_t src_stride = dsc->src_stride / sizeof(uint32_t); + + __arm_2d_impl_cccn888_tile_copy_opacity((uint32_t *)dsc->src_buf, + src_stride, + (uint32_t *)dsc->dest_buf, + des_stride, + &draw_size, + dsc->opa); + + return LV_RESULT_OK; +} + +static inline lv_result_t lv_rgb888_blend_normal_to_rgb888_with_mask_arm2d(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size, uint32_t src_px_size) +{ + if((dst_px_size == 3) || (src_px_size == 3)) { + return LV_RESULT_INVALID; + } + + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t des_stride = dsc->dest_stride / sizeof(uint32_t); + int16_t src_stride = dsc->src_stride / sizeof(uint32_t); + + __arm_2d_impl_cccn888_src_msk_copy((uint32_t *)dsc->src_buf, + src_stride, + (uint8_t *)dsc->mask_buf, + dsc->mask_stride, + &draw_size, + (uint32_t *)dsc->dest_buf, + des_stride, + &draw_size); + return LV_RESULT_OK; +} + +static inline lv_result_t lv_rgb888_blend_normal_to_rgb888_mix_mask_opa_arm2d(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size, uint32_t src_px_size) +{ + if((dst_px_size == 3) || (src_px_size == 3)) { + return LV_RESULT_INVALID; + } + + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t des_stride = dsc->dest_stride / sizeof(uint32_t); + int16_t src_stride = dsc->src_stride / sizeof(uint32_t); + + __arm_2d_impl_cccn888_tile_copy_with_src_mask_and_opacity((uint32_t *)dsc->src_buf, + src_stride, + (uint8_t *)dsc->mask_buf, + dsc->mask_stride, + &draw_size, + (uint32_t *)dsc->dest_buf, + des_stride, + &draw_size, + dsc->opa); + return LV_RESULT_OK; +} + +static inline lv_result_t lv_argb8888_blend_normal_to_rgb888_arm2d(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size) +{ + + if(dst_px_size == 3) { + return LV_RESULT_INVALID; + } + + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t des_stride = dsc->dest_stride / sizeof(uint32_t); + int16_t src_stride = dsc->src_stride / sizeof(uint32_t); + + __arm_2d_impl_ccca8888_to_cccn888((uint32_t *)dsc->src_buf, + src_stride, + (uint32_t *)dsc->dest_buf, + des_stride, + &draw_size); + + return LV_RESULT_OK; +} + +static inline lv_result_t lv_argb8888_blend_normal_to_rgb888_with_opa_arm2d(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size) +{ + if(dst_px_size == 3) { + return LV_RESULT_INVALID; + } + + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t des_stride = dsc->dest_stride / sizeof(uint32_t); + int16_t src_stride = dsc->src_stride / sizeof(uint32_t); + + __arm_2d_impl_ccca8888_tile_copy_to_cccn888_with_opacity((uint32_t *)dsc->src_buf, + src_stride, + (uint32_t *)dsc->dest_buf, + des_stride, + &draw_size, + dsc->opa); + + return LV_RESULT_OK; +} + +static inline lv_result_t lv_argb8888_blend_normal_to_rgb888_with_mask_arm2d(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size) +{ + + if(dst_px_size == 3) { + return LV_RESULT_INVALID; + } + + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t des_stride = dsc->dest_stride / sizeof(uint32_t); + int16_t src_stride = dsc->src_stride / sizeof(uint32_t); + + __arm_2d_impl_ccca8888_tile_copy_to_cccn888_with_src_mask((uint32_t *)dsc->src_buf, + src_stride, + (uint8_t *)dsc->mask_buf, + dsc->mask_stride, + &draw_size, + (uint32_t *)dsc->dest_buf, + des_stride, + &draw_size); + + return LV_RESULT_OK; +} + +static inline lv_result_t lv_argb8888_blend_normal_to_rgb888_mix_mask_opa_arm2d(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size) +{ + if(dst_px_size == 3) { + return LV_RESULT_INVALID; + } + + arm_2d_size_t draw_size = {dsc->dest_w, dsc->dest_h}; + int16_t des_stride = dsc->dest_stride / sizeof(uint32_t); + int16_t src_stride = dsc->src_stride / sizeof(uint32_t); + + __arm_2d_impl_ccca8888_tile_copy_to_cccn888_with_src_mask_and_opacity((uint32_t *)dsc->src_buf, + src_stride, + (uint8_t *)dsc->mask_buf, + dsc->mask_stride, + &draw_size, + (uint32_t *)dsc->dest_buf, + des_stride, + &draw_size, + dsc->opa); + + return LV_RESULT_OK; +} + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_DRAW_ARM2D_SYNC */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_BLEND_ARM2D_H*/ diff --git a/include/liblvgl/draw/sw/blend/helium/lv_blend_helium.h b/include/liblvgl/draw/sw/blend/helium/lv_blend_helium.h new file mode 100644 index 00000000..69b999ef --- /dev/null +++ b/include/liblvgl/draw/sw/blend/helium/lv_blend_helium.h @@ -0,0 +1,1313 @@ +/** + * @file lv_blend_helium.h + * + */ + +#ifndef LV_BLEND_HELIUM_H +#define LV_BLEND_HELIUM_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#if defined(_RTE_) +#include "Pre_Include_Global.h" +#include "lv_conf_cmsis.h" +#endif + +#include "../../../../lv_conf_internal.h" + +/* detect whether helium is available based on arm compilers' standard */ +#if defined(__ARM_FEATURE_MVE) && __ARM_FEATURE_MVE + +#ifdef LV_DRAW_SW_HELIUM_CUSTOM_INCLUDE +#include LV_DRAW_SW_HELIUM_CUSTOM_INCLUDE +#endif + +#if !defined(__ASSEMBLY__) + +/* Use arm2d functions if present */ +#include "../arm2d/lv_blend_arm2d.h" + +/********************* + * DEFINES + *********************/ + +#if LV_USE_NATIVE_HELIUM_ASM + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_RGB565 +#define LV_DRAW_SW_COLOR_BLEND_TO_RGB565(dsc) \ + lv_color_blend_to_rgb565_helium(dsc) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_RGB565_WITH_OPA +#define LV_DRAW_SW_COLOR_BLEND_TO_RGB565_WITH_OPA(dsc) \ + lv_color_blend_to_rgb565_with_opa_helium(dsc) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_RGB565_WITH_MASK +#define LV_DRAW_SW_COLOR_BLEND_TO_RGB565_WITH_MASK(dsc) \ + lv_color_blend_to_rgb565_with_mask_helium(dsc) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_RGB565_MIX_MASK_OPA +#define LV_DRAW_SW_COLOR_BLEND_TO_RGB565_MIX_MASK_OPA(dsc) \ + lv_color_blend_to_rgb565_mix_mask_opa_helium(dsc) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB565 +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB565(dsc) \ + lv_rgb565_blend_normal_to_rgb565_helium(dsc) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB565_WITH_OPA +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB565_WITH_OPA(dsc) \ + lv_rgb565_blend_normal_to_rgb565_with_opa_helium(dsc) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB565_WITH_MASK +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB565_WITH_MASK(dsc) \ + lv_rgb565_blend_normal_to_rgb565_with_mask_helium(dsc) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB565_MIX_MASK_OPA +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB565_MIX_MASK_OPA(dsc) \ + lv_rgb565_blend_normal_to_rgb565_mix_mask_opa_helium(dsc) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB565 +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB565(dsc, src_px_size) \ + lv_rgb888_blend_normal_to_rgb565_helium(dsc, src_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB565_WITH_OPA +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB565_WITH_OPA(dsc, src_px_size) \ + lv_rgb888_blend_normal_to_rgb565_with_opa_helium(dsc, src_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB565_WITH_MASK +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB565_WITH_MASK(dsc, src_px_size) \ + lv_rgb888_blend_normal_to_rgb565_with_mask_helium(dsc, src_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB565_MIX_MASK_OPA +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB565_MIX_MASK_OPA(dsc, src_px_size) \ + lv_rgb888_blend_normal_to_rgb565_mix_mask_opa_helium(dsc, src_px_size) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB565 +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB565(dsc) \ + lv_argb8888_blend_normal_to_rgb565_helium(dsc) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB565_WITH_OPA +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB565_WITH_OPA(dsc) \ + lv_argb8888_blend_normal_to_rgb565_with_opa_helium(dsc) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB565_WITH_MASK +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB565_WITH_MASK(dsc) \ + lv_argb8888_blend_normal_to_rgb565_with_mask_helium(dsc) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB565_MIX_MASK_OPA +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB565_MIX_MASK_OPA(dsc) \ + lv_argb8888_blend_normal_to_rgb565_mix_mask_opa_helium(dsc) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_RGB888 +#define LV_DRAW_SW_COLOR_BLEND_TO_RGB888(dsc, dst_px_size) \ + lv_color_blend_to_rgb888_helium(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_RGB888_WITH_OPA +#define LV_DRAW_SW_COLOR_BLEND_TO_RGB888_WITH_OPA(dsc, dst_px_size) \ + lv_color_blend_to_rgb888_with_opa_helium(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_RGB888_WITH_MASK +#define LV_DRAW_SW_COLOR_BLEND_TO_RGB888_WITH_MASK(dsc, dst_px_size) \ + lv_color_blend_to_rgb888_with_mask_helium(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_RGB888_MIX_MASK_OPA +#define LV_DRAW_SW_COLOR_BLEND_TO_RGB888_MIX_MASK_OPA(dsc, dst_px_size) \ + lv_color_blend_to_rgb888_mix_mask_opa_helium(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB888 +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB888(dsc, dst_px_size) \ + lv_rgb565_blend_normal_to_rgb888_helium(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB888_WITH_OPA +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB888_WITH_OPA(dsc, dst_px_size) \ + lv_rgb565_blend_normal_to_rgb888_with_opa_helium(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB888_WITH_MASK +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB888_WITH_MASK(dsc, dst_px_size) \ + lv_rgb565_blend_normal_to_rgb888_with_mask_helium(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB888_MIX_MASK_OPA +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB888_MIX_MASK_OPA(dsc, dst_px_size) \ + lv_rgb565_blend_normal_to_rgb888_mix_mask_opa_helium(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB888 +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB888(dsc, dst_px_size, src_px_size) \ + lv_rgb888_blend_normal_to_rgb888_helium(dsc, dst_px_size, src_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB888_WITH_OPA +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB888_WITH_OPA(dsc, dst_px_size, src_px_size) \ + lv_rgb888_blend_normal_to_rgb888_with_opa_helium(dsc, dst_px_size, src_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB888_WITH_MASK +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB888_WITH_MASK(dsc, dst_px_size, src_px_size) \ + lv_rgb888_blend_normal_to_rgb888_with_mask_helium(dsc, dst_px_size, src_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB888_MIX_MASK_OPA +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB888_MIX_MASK_OPA(dsc, dst_px_size, src_px_size) \ + lv_rgb888_blend_normal_to_rgb888_mix_mask_opa_helium(dsc, dst_px_size, src_px_size) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB888 +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB888(dsc, dst_px_size) \ + lv_argb8888_blend_normal_to_rgb888_helium(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB888_WITH_OPA +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB888_WITH_OPA(dsc, dst_px_size) \ + lv_argb8888_blend_normal_to_rgb888_with_opa_helium(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB888_WITH_MASK +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB888_WITH_MASK(dsc, dst_px_size) \ + lv_argb8888_blend_normal_to_rgb888_with_mask_helium(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB888_MIX_MASK_OPA +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB888_MIX_MASK_OPA(dsc, dst_px_size) \ + lv_argb8888_blend_normal_to_rgb888_mix_mask_opa_helium(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_ARGB8888 +#define LV_DRAW_SW_COLOR_BLEND_TO_ARGB8888(dsc) \ + lv_color_blend_to_argb8888_helium(dsc) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_ARGB8888_WITH_OPA +#define LV_DRAW_SW_COLOR_BLEND_TO_ARGB8888_WITH_OPA(dsc) \ + lv_color_blend_to_argb8888_with_opa_helium(dsc) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_ARGB8888_WITH_MASK +#define LV_DRAW_SW_COLOR_BLEND_TO_ARGB8888_WITH_MASK(dsc) \ + lv_color_blend_to_argb8888_with_mask_helium(dsc) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_ARGB8888_MIX_MASK_OPA +#define LV_DRAW_SW_COLOR_BLEND_TO_ARGB8888_MIX_MASK_OPA(dsc) \ + lv_color_blend_to_argb8888_mix_mask_opa_helium(dsc) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_ARGB8888 +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_ARGB8888(dsc) \ + lv_rgb565_blend_normal_to_argb8888_helium(dsc) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_ARGB8888_WITH_OPA +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_ARGB8888_WITH_OPA(dsc) \ + lv_rgb565_blend_normal_to_argb8888_with_opa_helium(dsc) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_ARGB8888_WITH_MASK +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_ARGB8888_WITH_MASK(dsc) \ + lv_rgb565_blend_normal_to_argb8888_with_mask_helium(dsc) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_ARGB8888_MIX_MASK_OPA +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_ARGB8888_MIX_MASK_OPA(dsc) \ + lv_rgb565_blend_normal_to_argb8888_mix_mask_opa_helium(dsc) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_ARGB8888 +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_ARGB8888(dsc, src_px_size) \ + lv_rgb888_blend_normal_to_argb8888_helium(dsc, src_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_ARGB8888_WITH_OPA +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_ARGB8888_WITH_OPA(dsc, src_px_size) \ + lv_rgb888_blend_normal_to_argb8888_with_opa_helium(dsc, src_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_ARGB8888_WITH_MASK +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_ARGB8888_WITH_MASK(dsc, src_px_size) \ + lv_rgb888_blend_normal_to_argb8888_with_mask_helium(dsc, src_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_ARGB8888_MIX_MASK_OPA +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_ARGB8888_MIX_MASK_OPA(dsc, src_px_size) \ + lv_rgb888_blend_normal_to_argb8888_mix_mask_opa_helium(dsc, src_px_size) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_ARGB8888 +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_ARGB8888(dsc) \ + lv_argb8888_blend_normal_to_argb8888_helium(dsc) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_ARGB8888_WITH_OPA +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_ARGB8888_WITH_OPA(dsc) \ + lv_argb8888_blend_normal_to_argb8888_with_opa_helium(dsc) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_ARGB8888_WITH_MASK +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_ARGB8888_WITH_MASK(dsc) \ + lv_argb8888_blend_normal_to_argb8888_with_mask_helium(dsc) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_ARGB8888_MIX_MASK_OPA +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_ARGB8888_MIX_MASK_OPA(dsc) \ + lv_argb8888_blend_normal_to_argb8888_mix_mask_opa_helium(dsc) +#endif + +/********************** + * TYPEDEFS + **********************/ + +typedef struct { + uint32_t opa; + void * dst_buf; + uint32_t dst_w; + uint32_t dst_h; + uint32_t dst_stride; + const void * src_buf; + uint32_t src_stride; + const lv_opa_t * mask_buf; + uint32_t mask_stride; +} asm_dsc_t; +/********************** + * GLOBAL PROTOTYPES + **********************/ + +extern void lv_color_blend_to_rgb565_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_color_blend_to_rgb565_helium(lv_draw_sw_blend_fill_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = &dsc->color + }; + + lv_color_blend_to_rgb565_helium(&asm_dsc); + return LV_RESULT_OK; +} + +extern void lv_color_blend_to_rgb565_with_opa_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_color_blend_to_rgb565_with_opa_helium(lv_draw_sw_blend_fill_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = &dsc->color + }; + lv_color_blend_to_rgb565_with_opa_helium(&asm_dsc); + return LV_RESULT_OK; +} + +extern void lv_color_blend_to_rgb565_with_mask_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_color_blend_to_rgb565_with_mask_helium(lv_draw_sw_blend_fill_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = &dsc->color, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + lv_color_blend_to_rgb565_with_mask_helium(&asm_dsc); + return LV_RESULT_OK; +} + +extern void lv_color_blend_to_rgb565_mix_mask_opa_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_color_blend_to_rgb565_mix_mask_opa_helium(lv_draw_sw_blend_fill_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = &dsc->color, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + lv_color_blend_to_rgb565_mix_mask_opa_helium(&asm_dsc); + return LV_RESULT_OK; +} + +extern void lv_rgb565_blend_normal_to_rgb565_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb565_blend_normal_to_rgb565_helium(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + lv_rgb565_blend_normal_to_rgb565_helium(&asm_dsc); + return LV_RESULT_OK; +} + +extern void lv_rgb565_blend_normal_to_rgb565_with_opa_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb565_blend_normal_to_rgb565_with_opa_helium(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + lv_rgb565_blend_normal_to_rgb565_with_opa_helium(&asm_dsc); + return LV_RESULT_OK; +} + +extern void lv_rgb565_blend_normal_to_rgb565_with_mask_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb565_blend_normal_to_rgb565_with_mask_helium(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + lv_rgb565_blend_normal_to_rgb565_with_mask_helium(&asm_dsc); + return LV_RESULT_OK; +} + +extern void lv_rgb565_blend_normal_to_rgb565_mix_mask_opa_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb565_blend_normal_to_rgb565_mix_mask_opa_helium(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + lv_rgb565_blend_normal_to_rgb565_mix_mask_opa_helium(&asm_dsc); + return LV_RESULT_OK; +} + +extern void lv_rgb888_blend_normal_to_rgb565_helium(asm_dsc_t * dsc); +extern void lv_xrgb8888_blend_normal_to_rgb565_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb888_blend_normal_to_rgb565_helium(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t src_px_size) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + if(src_px_size == 3) { + lv_rgb888_blend_normal_to_rgb565_helium(&asm_dsc); + } + else { + lv_xrgb8888_blend_normal_to_rgb565_helium(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern void lv_rgb888_blend_normal_to_rgb565_with_opa_helium(asm_dsc_t * dsc); +extern void lv_xrgb8888_blend_normal_to_rgb565_with_opa_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb888_blend_normal_to_rgb565_with_opa_helium(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t src_px_size) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + if(src_px_size == 3) { + lv_rgb888_blend_normal_to_rgb565_with_opa_helium(&asm_dsc); + } + else { + lv_xrgb8888_blend_normal_to_rgb565_with_opa_helium(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern void lv_rgb888_blend_normal_to_rgb565_with_mask_helium(asm_dsc_t * dsc); +extern void lv_xrgb8888_blend_normal_to_rgb565_with_mask_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb888_blend_normal_to_rgb565_with_mask_helium(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t src_px_size) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + if(src_px_size == 3) { + lv_rgb888_blend_normal_to_rgb565_with_mask_helium(&asm_dsc); + } + else { + lv_xrgb8888_blend_normal_to_rgb565_with_mask_helium(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern void lv_rgb888_blend_normal_to_rgb565_mix_mask_opa_helium(asm_dsc_t * dsc); +extern void lv_xrgb8888_blend_normal_to_rgb565_mix_mask_opa_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb888_blend_normal_to_rgb565_mix_mask_opa_helium(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t src_px_size) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + if(src_px_size == 3) { + lv_rgb888_blend_normal_to_rgb565_mix_mask_opa_helium(&asm_dsc); + } + else { + lv_xrgb8888_blend_normal_to_rgb565_mix_mask_opa_helium(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern void lv_argb8888_blend_normal_to_rgb565_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_argb8888_blend_normal_to_rgb565_helium(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + lv_argb8888_blend_normal_to_rgb565_helium(&asm_dsc); + return LV_RESULT_OK; +} + +extern void lv_argb8888_blend_normal_to_rgb565_with_opa_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_argb8888_blend_normal_to_rgb565_with_opa_helium(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + lv_argb8888_blend_normal_to_rgb565_with_opa_helium(&asm_dsc); + return LV_RESULT_OK; +} + +extern void lv_argb8888_blend_normal_to_rgb565_with_mask_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_argb8888_blend_normal_to_rgb565_with_mask_helium(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + lv_argb8888_blend_normal_to_rgb565_with_mask_helium(&asm_dsc); + return LV_RESULT_OK; +} + +extern void lv_argb8888_blend_normal_to_rgb565_mix_mask_opa_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_argb8888_blend_normal_to_rgb565_mix_mask_opa_helium(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + lv_argb8888_blend_normal_to_rgb565_mix_mask_opa_helium(&asm_dsc); + return LV_RESULT_OK; +} + +extern void lv_color_blend_to_rgb888_helium(asm_dsc_t * dsc); +extern void lv_color_blend_to_xrgb8888_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_color_blend_to_rgb888_helium(lv_draw_sw_blend_fill_dsc_t * dsc, uint32_t dst_px_size) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = &dsc->color + }; + if(dst_px_size == 3) { + lv_color_blend_to_rgb888_helium(&asm_dsc); + } + else { + lv_color_blend_to_xrgb8888_helium(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern void lv_color_blend_to_rgb888_with_opa_helium(asm_dsc_t * dsc); +extern void lv_color_blend_to_xrgb8888_with_opa_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_color_blend_to_rgb888_with_opa_helium(lv_draw_sw_blend_fill_dsc_t * dsc, + uint32_t dst_px_size) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = &dsc->color + }; + if(dst_px_size == 3) { + lv_color_blend_to_rgb888_with_opa_helium(&asm_dsc); + } + else { + lv_color_blend_to_xrgb8888_with_opa_helium(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern void lv_color_blend_to_rgb888_with_mask_helium(asm_dsc_t * dsc); +extern void lv_color_blend_to_xrgb8888_with_mask_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_color_blend_to_rgb888_with_mask_helium(lv_draw_sw_blend_fill_dsc_t * dsc, + uint32_t dst_px_size) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = &dsc->color, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + if(dst_px_size == 3) { + lv_color_blend_to_rgb888_with_mask_helium(&asm_dsc); + } + else { + lv_color_blend_to_xrgb8888_with_mask_helium(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern void lv_color_blend_to_rgb888_mix_mask_opa_helium(asm_dsc_t * dsc); +extern void lv_color_blend_to_xrgb8888_mix_mask_opa_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_color_blend_to_rgb888_mix_mask_opa_helium(lv_draw_sw_blend_fill_dsc_t * dsc, + uint32_t dst_px_size) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = &dsc->color, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + if(dst_px_size == 3) { + lv_color_blend_to_rgb888_mix_mask_opa_helium(&asm_dsc); + } + else { + lv_color_blend_to_xrgb8888_mix_mask_opa_helium(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern void lv_rgb565_blend_normal_to_rgb888_helium(asm_dsc_t * dsc); +extern void lv_rgb565_blend_normal_to_xrgb8888_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb565_blend_normal_to_rgb888_helium(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + if(dst_px_size == 3) { + lv_rgb565_blend_normal_to_rgb888_helium(&asm_dsc); + } + else { + lv_rgb565_blend_normal_to_xrgb8888_helium(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern void lv_rgb565_blend_normal_to_rgb888_with_opa_helium(asm_dsc_t * dsc); +extern void lv_rgb565_blend_normal_to_xrgb8888_with_opa_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb565_blend_normal_to_rgb888_with_opa_helium(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + if(dst_px_size == 3) { + lv_rgb565_blend_normal_to_rgb888_with_opa_helium(&asm_dsc); + } + else { + lv_rgb565_blend_normal_to_xrgb8888_with_opa_helium(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern void lv_rgb565_blend_normal_to_rgb888_with_mask_helium(asm_dsc_t * dsc); +extern void lv_rgb565_blend_normal_to_xrgb8888_with_mask_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb565_blend_normal_to_rgb888_with_mask_helium(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + if(dst_px_size == 3) { + lv_rgb565_blend_normal_to_rgb888_with_mask_helium(&asm_dsc); + } + else { + lv_rgb565_blend_normal_to_xrgb8888_with_mask_helium(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern void lv_rgb565_blend_normal_to_rgb888_mix_mask_opa_helium(asm_dsc_t * dsc); +extern void lv_rgb565_blend_normal_to_xrgb8888_mix_mask_opa_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb565_blend_normal_to_rgb888_mix_mask_opa_helium(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + if(dst_px_size == 3) { + lv_rgb565_blend_normal_to_rgb888_mix_mask_opa_helium(&asm_dsc); + } + else { + lv_rgb565_blend_normal_to_xrgb8888_mix_mask_opa_helium(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern void lv_rgb888_blend_normal_to_rgb888_helium(asm_dsc_t * dsc); +extern void lv_rgb888_blend_normal_to_xrgb8888_helium(asm_dsc_t * dsc); +extern void lv_xrgb8888_blend_normal_to_rgb888_helium(asm_dsc_t * dsc); +extern void lv_xrgb8888_blend_normal_to_xrgb8888_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb888_blend_normal_to_rgb888_helium(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size, + uint32_t src_px_size) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + if(dst_px_size == 3) { + if(src_px_size == 3) { + lv_rgb888_blend_normal_to_rgb888_helium(&asm_dsc); + } + else { + lv_xrgb8888_blend_normal_to_rgb888_helium(&asm_dsc); + } + } + else { + if(src_px_size == 3) { + lv_rgb888_blend_normal_to_xrgb8888_helium(&asm_dsc); + } + else { + lv_xrgb8888_blend_normal_to_xrgb8888_helium(&asm_dsc); + } + } + return LV_RESULT_OK; +} + +extern void lv_rgb888_blend_normal_to_rgb888_with_opa_helium(asm_dsc_t * dsc); +extern void lv_rgb888_blend_normal_to_xrgb8888_with_opa_helium(asm_dsc_t * dsc); +extern void lv_xrgb8888_blend_normal_to_rgb888_with_opa_helium(asm_dsc_t * dsc); +extern void lv_xrgb8888_blend_normal_to_xrgb8888_with_opa_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb888_blend_normal_to_rgb888_with_opa_helium(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size, uint32_t src_px_size) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + if(dst_px_size == 3) { + if(src_px_size == 3) { + lv_rgb888_blend_normal_to_rgb888_with_opa_helium(&asm_dsc); + } + else { + lv_xrgb8888_blend_normal_to_rgb888_with_opa_helium(&asm_dsc); + } + } + else { + if(src_px_size == 3) { + lv_rgb888_blend_normal_to_xrgb8888_with_opa_helium(&asm_dsc); + } + else { + lv_xrgb8888_blend_normal_to_xrgb8888_with_opa_helium(&asm_dsc); + } + } + return LV_RESULT_OK; +} + +extern void lv_rgb888_blend_normal_to_rgb888_with_mask_helium(asm_dsc_t * dsc); +extern void lv_rgb888_blend_normal_to_xrgb8888_with_mask_helium(asm_dsc_t * dsc); +extern void lv_xrgb8888_blend_normal_to_rgb888_with_mask_helium(asm_dsc_t * dsc); +extern void lv_xrgb8888_blend_normal_to_xrgb8888_with_mask_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb888_blend_normal_to_rgb888_with_mask_helium(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size, uint32_t src_px_size) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + if(dst_px_size == 3) { + if(src_px_size == 3) { + lv_rgb888_blend_normal_to_rgb888_with_mask_helium(&asm_dsc); + } + else { + lv_xrgb8888_blend_normal_to_rgb888_with_mask_helium(&asm_dsc); + } + } + else { + if(src_px_size == 3) { + lv_rgb888_blend_normal_to_xrgb8888_with_mask_helium(&asm_dsc); + } + else { + lv_xrgb8888_blend_normal_to_xrgb8888_with_mask_helium(&asm_dsc); + } + } + return LV_RESULT_OK; +} + +extern void lv_rgb888_blend_normal_to_rgb888_mix_mask_opa_helium(asm_dsc_t * dsc); +extern void lv_rgb888_blend_normal_to_xrgb8888_mix_mask_opa_helium(asm_dsc_t * dsc); +extern void lv_xrgb8888_blend_normal_to_rgb888_mix_mask_opa_helium(asm_dsc_t * dsc); +extern void lv_xrgb8888_blend_normal_to_xrgb8888_mix_mask_opa_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb888_blend_normal_to_rgb888_mix_mask_opa_helium(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size, uint32_t src_px_size) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + if(dst_px_size == 3) { + if(src_px_size == 3) { + lv_rgb888_blend_normal_to_rgb888_mix_mask_opa_helium(&asm_dsc); + } + else { + lv_xrgb8888_blend_normal_to_rgb888_mix_mask_opa_helium(&asm_dsc); + } + } + else { + if(src_px_size == 3) { + lv_rgb888_blend_normal_to_xrgb8888_mix_mask_opa_helium(&asm_dsc); + } + else { + lv_xrgb8888_blend_normal_to_xrgb8888_mix_mask_opa_helium(&asm_dsc); + } + } + return LV_RESULT_OK; +} + +extern void lv_argb8888_blend_normal_to_rgb888_helium(asm_dsc_t * dsc); +extern void lv_argb8888_blend_normal_to_xrgb8888_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_argb8888_blend_normal_to_rgb888_helium(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + if(dst_px_size == 3) { + lv_argb8888_blend_normal_to_rgb888_helium(&asm_dsc); + } + else { + lv_argb8888_blend_normal_to_xrgb8888_helium(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern void lv_argb8888_blend_normal_to_rgb888_with_opa_helium(asm_dsc_t * dsc); +extern void lv_argb8888_blend_normal_to_xrgb8888_with_opa_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_argb8888_blend_normal_to_rgb888_with_opa_helium(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + if(dst_px_size == 3) { + lv_argb8888_blend_normal_to_rgb888_with_opa_helium(&asm_dsc); + } + else { + lv_argb8888_blend_normal_to_xrgb8888_with_opa_helium(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern void lv_argb8888_blend_normal_to_rgb888_with_mask_helium(asm_dsc_t * dsc); +extern void lv_argb8888_blend_normal_to_xrgb8888_with_mask_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_argb8888_blend_normal_to_rgb888_with_mask_helium(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + if(dst_px_size == 3) { + lv_argb8888_blend_normal_to_rgb888_with_mask_helium(&asm_dsc); + } + else { + lv_argb8888_blend_normal_to_xrgb8888_with_mask_helium(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern void lv_argb8888_blend_normal_to_rgb888_mix_mask_opa_helium(asm_dsc_t * dsc); +extern void lv_argb8888_blend_normal_to_xrgb8888_mix_mask_opa_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_argb8888_blend_normal_to_rgb888_mix_mask_opa_helium(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + if(dst_px_size == 3) { + lv_argb8888_blend_normal_to_rgb888_mix_mask_opa_helium(&asm_dsc); + } + else { + lv_argb8888_blend_normal_to_xrgb8888_mix_mask_opa_helium(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern void lv_color_blend_to_argb8888_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_color_blend_to_argb8888_helium(lv_draw_sw_blend_fill_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = &dsc->color + }; + + lv_color_blend_to_argb8888_helium(&asm_dsc); + return LV_RESULT_OK; +} + +extern void lv_color_blend_to_argb8888_with_opa_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_color_blend_to_argb8888_with_opa_helium(lv_draw_sw_blend_fill_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = &dsc->color + }; + lv_color_blend_to_argb8888_with_opa_helium(&asm_dsc); + return LV_RESULT_OK; +} + +extern void lv_color_blend_to_argb8888_with_mask_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_color_blend_to_argb8888_with_mask_helium(lv_draw_sw_blend_fill_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = &dsc->color, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + lv_color_blend_to_argb8888_with_mask_helium(&asm_dsc); + return LV_RESULT_OK; +} + +extern void lv_color_blend_to_argb8888_mix_mask_opa_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_color_blend_to_argb8888_mix_mask_opa_helium(lv_draw_sw_blend_fill_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = &dsc->color, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + lv_color_blend_to_argb8888_mix_mask_opa_helium(&asm_dsc); + return LV_RESULT_OK; +} + +extern void lv_rgb565_blend_normal_to_argb8888_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb565_blend_normal_to_argb8888_helium(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + lv_rgb565_blend_normal_to_argb8888_helium(&asm_dsc); + return LV_RESULT_OK; +} + +extern void lv_rgb565_blend_normal_to_argb8888_with_opa_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb565_blend_normal_to_argb8888_with_opa_helium(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + lv_rgb565_blend_normal_to_argb8888_with_opa_helium(&asm_dsc); + return LV_RESULT_OK; +} + +extern void lv_rgb565_blend_normal_to_argb8888_with_mask_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb565_blend_normal_to_argb8888_with_mask_helium(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + lv_rgb565_blend_normal_to_argb8888_with_mask_helium(&asm_dsc); + return LV_RESULT_OK; +} + +extern void lv_rgb565_blend_normal_to_argb8888_mix_mask_opa_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb565_blend_normal_to_argb8888_mix_mask_opa_helium(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + lv_rgb565_blend_normal_to_argb8888_mix_mask_opa_helium(&asm_dsc); + return LV_RESULT_OK; +} + +extern void lv_rgb888_blend_normal_to_argb8888_helium(asm_dsc_t * dsc); +extern void lv_xrgb8888_blend_normal_to_argb8888_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb888_blend_normal_to_argb8888_helium(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t src_px_size) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + if(src_px_size == 3) { + lv_rgb888_blend_normal_to_argb8888_helium(&asm_dsc); + } + else { + lv_xrgb8888_blend_normal_to_argb8888_helium(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern void lv_rgb888_blend_normal_to_argb8888_with_opa_helium(asm_dsc_t * dsc); +extern void lv_xrgb8888_blend_normal_to_argb8888_with_opa_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb888_blend_normal_to_argb8888_with_opa_helium(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t src_px_size) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + if(src_px_size == 3) { + lv_rgb888_blend_normal_to_argb8888_with_opa_helium(&asm_dsc); + } + else { + lv_xrgb8888_blend_normal_to_argb8888_with_opa_helium(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern void lv_rgb888_blend_normal_to_argb8888_with_mask_helium(asm_dsc_t * dsc); +extern void lv_xrgb8888_blend_normal_to_argb8888_with_mask_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb888_blend_normal_to_argb8888_with_mask_helium(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t src_px_size) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + if(src_px_size == 3) { + lv_rgb888_blend_normal_to_argb8888_with_mask_helium(&asm_dsc); + } + else { + lv_xrgb8888_blend_normal_to_argb8888_with_mask_helium(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern void lv_rgb888_blend_normal_to_argb8888_mix_mask_opa_helium(asm_dsc_t * dsc); +extern void lv_xrgb8888_blend_normal_to_argb8888_mix_mask_opa_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb888_blend_normal_to_argb8888_mix_mask_opa_helium(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t src_px_size) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + if(src_px_size == 3) { + lv_rgb888_blend_normal_to_argb8888_mix_mask_opa_helium(&asm_dsc); + } + else { + lv_xrgb8888_blend_normal_to_argb8888_mix_mask_opa_helium(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern void lv_argb8888_blend_normal_to_argb8888_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_argb8888_blend_normal_to_argb8888_helium(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + lv_argb8888_blend_normal_to_argb8888_helium(&asm_dsc); + return LV_RESULT_OK; +} + +extern void lv_argb8888_blend_normal_to_argb8888_with_opa_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_argb8888_blend_normal_to_argb8888_with_opa_helium(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + lv_argb8888_blend_normal_to_argb8888_with_opa_helium(&asm_dsc); + return LV_RESULT_OK; +} + +extern void lv_argb8888_blend_normal_to_argb8888_with_mask_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_argb8888_blend_normal_to_argb8888_with_mask_helium(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + lv_argb8888_blend_normal_to_argb8888_with_mask_helium(&asm_dsc); + return LV_RESULT_OK; +} + +extern void lv_argb8888_blend_normal_to_argb8888_mix_mask_opa_helium(asm_dsc_t * dsc); +static inline lv_result_t lv_argb8888_blend_normal_to_argb8888_mix_mask_opa_helium(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + lv_argb8888_blend_normal_to_argb8888_mix_mask_opa_helium(&asm_dsc); + return LV_RESULT_OK; +} + +#endif /* LV_USE_NATIVE_HELIUM_ASM */ + +#endif /* !defined(__ASSEMBLY__) */ + +#endif /* defined(__ARM_FEATURE_MVE) && __ARM_FEATURE_MVE */ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_BLEND_HELIUM_H*/ diff --git a/include/liblvgl/draw/sw/blend/lv_draw_sw_blend.h b/include/liblvgl/draw/sw/blend/lv_draw_sw_blend.h new file mode 100644 index 00000000..1528cacc --- /dev/null +++ b/include/liblvgl/draw/sw/blend/lv_draw_sw_blend.h @@ -0,0 +1,52 @@ +/** + * @file lv_draw_sw_blend.h + * + */ + +#ifndef LV_DRAW_SW_BLEND_H +#define LV_DRAW_SW_BLEND_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../lv_draw_sw_mask.h" +#if LV_USE_DRAW_SW + +#include "../../../misc/lv_color.h" +#include "../../../misc/lv_area.h" +#include "../../../misc/lv_style.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Call the blend function of the `layer`. + * @param draw_unit pointer to a draw unit + * @param dsc pointer to an initialized blend descriptor + */ +void lv_draw_sw_blend(lv_draw_unit_t * draw_unit, const lv_draw_sw_blend_dsc_t * dsc); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_DRAW_SW*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_SW_BLEND_H*/ diff --git a/include/liblvgl/draw/sw/blend/lv_draw_sw_blend_private.h b/include/liblvgl/draw/sw/blend/lv_draw_sw_blend_private.h new file mode 100644 index 00000000..731f9ad0 --- /dev/null +++ b/include/liblvgl/draw/sw/blend/lv_draw_sw_blend_private.h @@ -0,0 +1,92 @@ +/** + * @file lv_draw_sw_blend_private.h + * + */ + +#ifndef LV_DRAW_SW_BLEND_PRIVATE_H +#define LV_DRAW_SW_BLEND_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_draw_sw_blend.h" + +#if LV_USE_DRAW_SW + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_draw_sw_blend_dsc_t { + const lv_area_t * blend_area; /**< The area with absolute coordinates to draw on `layer->buf` + * will be clipped to `layer->clip_area` */ + const void * src_buf; /**< Pointer to an image to blend. If set `fill_color` is ignored */ + uint32_t src_stride; + lv_color_format_t src_color_format; + const lv_area_t * src_area; + lv_opa_t opa; /**< The overall opacity*/ + lv_color_t color; /**< Fill color*/ + const lv_opa_t * mask_buf; /**< NULL if ignored, or an alpha mask to apply on `blend_area`*/ + lv_draw_sw_mask_res_t mask_res; /**< The result of the previous mask operation */ + const lv_area_t * mask_area; /**< The area of `mask_buf` with absolute coordinates*/ + int32_t mask_stride; + lv_blend_mode_t blend_mode; /**< E.g. LV_BLEND_MODE_ADDITIVE*/ +}; + +struct _lv_draw_sw_blend_fill_dsc_t { + void * dest_buf; + int32_t dest_w; + int32_t dest_h; + int32_t dest_stride; + const lv_opa_t * mask_buf; + int32_t mask_stride; + lv_color_t color; + lv_opa_t opa; + lv_area_t relative_area; +}; + +struct _lv_draw_sw_blend_image_dsc_t { + void * dest_buf; + int32_t dest_w; + int32_t dest_h; + int32_t dest_stride; + const lv_opa_t * mask_buf; + int32_t mask_stride; + const void * src_buf; + int32_t src_stride; + lv_color_format_t src_color_format; + lv_opa_t opa; + lv_blend_mode_t blend_mode; + lv_area_t relative_area; /**< The blend area relative to the layer's buffer area. */ + lv_area_t src_area; /**< The original src area. */ +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_DRAW_SW */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_SW_BLEND_PRIVATE_H*/ diff --git a/include/liblvgl/draw/sw/blend/lv_draw_sw_blend_to_al88.h b/include/liblvgl/draw/sw/blend/lv_draw_sw_blend_to_al88.h new file mode 100644 index 00000000..63197c91 --- /dev/null +++ b/include/liblvgl/draw/sw/blend/lv_draw_sw_blend_to_al88.h @@ -0,0 +1,45 @@ +/** + * @file lv_draw_sw_blend_to_al88.h + * + */ + +#ifndef LV_DRAW_SW_BLEND_TO_AL88_H +#define LV_DRAW_SW_BLEND_TO_AL88_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../lv_draw_sw.h" +#if LV_USE_DRAW_SW + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void /* LV_ATTRIBUTE_FAST_MEM */ lv_draw_sw_blend_color_to_al88(lv_draw_sw_blend_fill_dsc_t * dsc); + +void /* LV_ATTRIBUTE_FAST_MEM */ lv_draw_sw_blend_image_to_al88(lv_draw_sw_blend_image_dsc_t * dsc); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_DRAW_SW*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_SW_BLEND_TO_AL88_H*/ diff --git a/include/liblvgl/draw/sw/blend/lv_draw_sw_blend_to_argb8888.h b/include/liblvgl/draw/sw/blend/lv_draw_sw_blend_to_argb8888.h new file mode 100644 index 00000000..2046c23f --- /dev/null +++ b/include/liblvgl/draw/sw/blend/lv_draw_sw_blend_to_argb8888.h @@ -0,0 +1,45 @@ +/** + * @file lv_draw_sw_blend_to_argb8888.h + * + */ + +#ifndef LV_DRAW_SW_BLEND_TO_ARGB8888_H +#define LV_DRAW_SW_BLEND_TO_ARGB8888_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../lv_draw_sw.h" +#if LV_USE_DRAW_SW + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void /* LV_ATTRIBUTE_FAST_MEM */ lv_draw_sw_blend_color_to_argb8888(lv_draw_sw_blend_fill_dsc_t * dsc); + +void /* LV_ATTRIBUTE_FAST_MEM */ lv_draw_sw_blend_image_to_argb8888(lv_draw_sw_blend_image_dsc_t * dsc); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_DRAW_SW*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_SW_BLEND_TO_ARGB8888_H*/ diff --git a/include/liblvgl/draw/sw/blend/lv_draw_sw_blend_to_i1.h b/include/liblvgl/draw/sw/blend/lv_draw_sw_blend_to_i1.h new file mode 100644 index 00000000..5d72523d --- /dev/null +++ b/include/liblvgl/draw/sw/blend/lv_draw_sw_blend_to_i1.h @@ -0,0 +1,45 @@ +/** + * @file lv_draw_sw_blend_to_i1.h + * + */ + +#ifndef LV_DRAW_SW_BLEND_TO_I1_H +#define LV_DRAW_SW_BLEND_TO_I1_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../lv_draw_sw.h" +#if LV_USE_DRAW_SW + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void /* LV_ATTRIBUTE_FAST_MEM */ lv_draw_sw_blend_color_to_i1(lv_draw_sw_blend_fill_dsc_t * dsc); + +void /* LV_ATTRIBUTE_FAST_MEM */ lv_draw_sw_blend_image_to_i1(lv_draw_sw_blend_image_dsc_t * dsc); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_DRAW_SW*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_SW_BLEND_TO_I1_H*/ diff --git a/include/liblvgl/draw/sw/blend/lv_draw_sw_blend_to_l8.h b/include/liblvgl/draw/sw/blend/lv_draw_sw_blend_to_l8.h new file mode 100644 index 00000000..04f4b44a --- /dev/null +++ b/include/liblvgl/draw/sw/blend/lv_draw_sw_blend_to_l8.h @@ -0,0 +1,45 @@ +/** + * @file lv_draw_sw_blend_to_l8.h + * + */ + +#ifndef LV_DRAW_SW_BLEND_TO_L8_H +#define LV_DRAW_SW_BLEND_TO_L8_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../lv_draw_sw.h" +#if LV_USE_DRAW_SW + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void /* LV_ATTRIBUTE_FAST_MEM */ lv_draw_sw_blend_color_to_l8(lv_draw_sw_blend_fill_dsc_t * dsc); + +void /* LV_ATTRIBUTE_FAST_MEM */ lv_draw_sw_blend_image_to_l8(lv_draw_sw_blend_image_dsc_t * dsc); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_DRAW_SW*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_SW_BLEND_TO_L8_H*/ diff --git a/include/liblvgl/draw/sw/blend/lv_draw_sw_blend_to_rgb565.h b/include/liblvgl/draw/sw/blend/lv_draw_sw_blend_to_rgb565.h new file mode 100644 index 00000000..d14bfd94 --- /dev/null +++ b/include/liblvgl/draw/sw/blend/lv_draw_sw_blend_to_rgb565.h @@ -0,0 +1,45 @@ +/** + * @file lv_draw_sw_blend_to_rgb565.h + * + */ + +#ifndef LV_DRAW_SW_BLEND_TO_RGB565_H +#define LV_DRAW_SW_BLEND_TO_RGB565_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../lv_draw_sw.h" +#if LV_USE_DRAW_SW + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void /* LV_ATTRIBUTE_FAST_MEM */ lv_draw_sw_blend_color_to_rgb565(lv_draw_sw_blend_fill_dsc_t * dsc); + +void /* LV_ATTRIBUTE_FAST_MEM */ lv_draw_sw_blend_image_to_rgb565(lv_draw_sw_blend_image_dsc_t * dsc); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_DRAW_SW*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_SW_BLEND_TO_RGB565_H*/ diff --git a/include/liblvgl/draw/sw/blend/lv_draw_sw_blend_to_rgb888.h b/include/liblvgl/draw/sw/blend/lv_draw_sw_blend_to_rgb888.h new file mode 100644 index 00000000..441c0d3e --- /dev/null +++ b/include/liblvgl/draw/sw/blend/lv_draw_sw_blend_to_rgb888.h @@ -0,0 +1,47 @@ +/** + * @file lv_draw_sw_blend_to_rgb888.h + * + */ + +#ifndef LV_DRAW_SW_BLEND_TO_RGB888_H +#define LV_DRAW_SW_BLEND_TO_RGB888_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../lv_draw_sw.h" +#if LV_USE_DRAW_SW + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void /* LV_ATTRIBUTE_FAST_MEM */ lv_draw_sw_blend_color_to_rgb888(lv_draw_sw_blend_fill_dsc_t * dsc, + uint32_t dest_px_size); + +void /* LV_ATTRIBUTE_FAST_MEM */ lv_draw_sw_blend_image_to_rgb888(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dest_px_size); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_DRAW_SW*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_SW_BLEND_TO_RGB888_H*/ diff --git a/include/liblvgl/draw/sw/blend/neon/lv_blend_neon.h b/include/liblvgl/draw/sw/blend/neon/lv_blend_neon.h new file mode 100644 index 00000000..fd52647b --- /dev/null +++ b/include/liblvgl/draw/sw/blend/neon/lv_blend_neon.h @@ -0,0 +1,1301 @@ +/** + * @file lv_blend_neon.h + * + */ + +#ifndef LV_BLEND_NEON_H +#define LV_BLEND_NEON_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../../../lv_conf_internal.h" + +#ifdef LV_DRAW_SW_NEON_CUSTOM_INCLUDE +#include LV_DRAW_SW_NEON_CUSTOM_INCLUDE +#endif + +/********************* + * DEFINES + *********************/ +#if !defined(__ASSEMBLY__) + +#if __GNUC__ >= 4 +#define LVGL_HIDDEN __attribute__((visibility("hidden"))) +#else +#define LVGL_HIDDEN +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_RGB565 +#define LV_DRAW_SW_COLOR_BLEND_TO_RGB565(dsc) \ + lv_color_blend_to_rgb565_neon(dsc) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_RGB565_WITH_OPA +#define LV_DRAW_SW_COLOR_BLEND_TO_RGB565_WITH_OPA(dsc) \ + lv_color_blend_to_rgb565_with_opa_neon(dsc) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_RGB565_WITH_MASK +#define LV_DRAW_SW_COLOR_BLEND_TO_RGB565_WITH_MASK(dsc) \ + lv_color_blend_to_rgb565_with_mask_neon(dsc) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_RGB565_MIX_MASK_OPA +#define LV_DRAW_SW_COLOR_BLEND_TO_RGB565_MIX_MASK_OPA(dsc) \ + lv_color_blend_to_rgb565_mix_mask_opa_neon(dsc) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB565 +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB565(dsc) \ + lv_rgb565_blend_normal_to_rgb565_neon(dsc) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB565_WITH_OPA +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB565_WITH_OPA(dsc) \ + lv_rgb565_blend_normal_to_rgb565_with_opa_neon(dsc) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB565_WITH_MASK +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB565_WITH_MASK(dsc) \ + lv_rgb565_blend_normal_to_rgb565_with_mask_neon(dsc) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB565_MIX_MASK_OPA +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB565_MIX_MASK_OPA(dsc) \ + lv_rgb565_blend_normal_to_rgb565_mix_mask_opa_neon(dsc) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB565 +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB565(dsc, src_px_size) \ + lv_rgb888_blend_normal_to_rgb565_neon(dsc, src_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB565_WITH_OPA +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB565_WITH_OPA(dsc, src_px_size) \ + lv_rgb888_blend_normal_to_rgb565_with_opa_neon(dsc, src_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB565_WITH_MASK +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB565_WITH_MASK(dsc, src_px_size) \ + lv_rgb888_blend_normal_to_rgb565_with_mask_neon(dsc, src_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB565_MIX_MASK_OPA +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB565_MIX_MASK_OPA(dsc, src_px_size) \ + lv_rgb888_blend_normal_to_rgb565_mix_mask_opa_neon(dsc, src_px_size) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB565 +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB565(dsc) \ + lv_argb8888_blend_normal_to_rgb565_neon(dsc) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB565_WITH_OPA +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB565_WITH_OPA(dsc) \ + lv_argb8888_blend_normal_to_rgb565_with_opa_neon(dsc) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB565_WITH_MASK +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB565_WITH_MASK(dsc) \ + lv_argb8888_blend_normal_to_rgb565_with_mask_neon(dsc) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB565_MIX_MASK_OPA +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB565_MIX_MASK_OPA(dsc) \ + lv_argb8888_blend_normal_to_rgb565_mix_mask_opa_neon(dsc) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_RGB888 +#define LV_DRAW_SW_COLOR_BLEND_TO_RGB888(dsc, dst_px_size) \ + lv_color_blend_to_rgb888_neon(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_RGB888_WITH_OPA +#define LV_DRAW_SW_COLOR_BLEND_TO_RGB888_WITH_OPA(dsc, dst_px_size) \ + lv_color_blend_to_rgb888_with_opa_neon(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_RGB888_WITH_MASK +#define LV_DRAW_SW_COLOR_BLEND_TO_RGB888_WITH_MASK(dsc, dst_px_size) \ + lv_color_blend_to_rgb888_with_mask_neon(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_RGB888_MIX_MASK_OPA +#define LV_DRAW_SW_COLOR_BLEND_TO_RGB888_MIX_MASK_OPA(dsc, dst_px_size) \ + lv_color_blend_to_rgb888_mix_mask_opa_neon(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB888 +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB888(dsc, dst_px_size) \ + lv_rgb565_blend_normal_to_rgb888_neon(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB888_WITH_OPA +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB888_WITH_OPA(dsc, dst_px_size) \ + lv_rgb565_blend_normal_to_rgb888_with_opa_neon(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB888_WITH_MASK +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB888_WITH_MASK(dsc, dst_px_size) \ + lv_rgb565_blend_normal_to_rgb888_with_mask_neon(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB888_MIX_MASK_OPA +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_RGB888_MIX_MASK_OPA(dsc, dst_px_size) \ + lv_rgb565_blend_normal_to_rgb888_mix_mask_opa_neon(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB888 +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB888(dsc, dst_px_size, src_px_size) \ + lv_rgb888_blend_normal_to_rgb888_neon(dsc, dst_px_size, src_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB888_WITH_OPA +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB888_WITH_OPA(dsc, dst_px_size, src_px_size) \ + lv_rgb888_blend_normal_to_rgb888_with_opa_neon(dsc, dst_px_size, src_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB888_WITH_MASK +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB888_WITH_MASK(dsc, dst_px_size, src_px_size) \ + lv_rgb888_blend_normal_to_rgb888_with_mask_neon(dsc, dst_px_size, src_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB888_MIX_MASK_OPA +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_RGB888_MIX_MASK_OPA(dsc, dst_px_size, src_px_size) \ + lv_rgb888_blend_normal_to_rgb888_mix_mask_opa_neon(dsc, dst_px_size, src_px_size) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB888 +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB888(dsc, dst_px_size) \ + lv_argb8888_blend_normal_to_rgb888_neon(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB888_WITH_OPA +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB888_WITH_OPA(dsc, dst_px_size) \ + lv_argb8888_blend_normal_to_rgb888_with_opa_neon(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB888_WITH_MASK +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB888_WITH_MASK(dsc, dst_px_size) \ + lv_argb8888_blend_normal_to_rgb888_with_mask_neon(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB888_MIX_MASK_OPA +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_RGB888_MIX_MASK_OPA(dsc, dst_px_size) \ + lv_argb8888_blend_normal_to_rgb888_mix_mask_opa_neon(dsc, dst_px_size) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_ARGB8888 +#define LV_DRAW_SW_COLOR_BLEND_TO_ARGB8888(dsc) \ + lv_color_blend_to_argb8888_neon(dsc) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_ARGB8888_WITH_OPA +#define LV_DRAW_SW_COLOR_BLEND_TO_ARGB8888_WITH_OPA(dsc) \ + lv_color_blend_to_argb8888_with_opa_neon(dsc) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_ARGB8888_WITH_MASK +#define LV_DRAW_SW_COLOR_BLEND_TO_ARGB8888_WITH_MASK(dsc) \ + lv_color_blend_to_argb8888_with_mask_neon(dsc) +#endif + +#ifndef LV_DRAW_SW_COLOR_BLEND_TO_ARGB8888_MIX_MASK_OPA +#define LV_DRAW_SW_COLOR_BLEND_TO_ARGB8888_MIX_MASK_OPA(dsc) \ + lv_color_blend_to_argb8888_mix_mask_opa_neon(dsc) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_ARGB8888 +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_ARGB8888(dsc) \ + lv_rgb565_blend_normal_to_argb8888_neon(dsc) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_ARGB8888_WITH_OPA +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_ARGB8888_WITH_OPA(dsc) \ + lv_rgb565_blend_normal_to_argb8888_with_opa_neon(dsc) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_ARGB8888_WITH_MASK +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_ARGB8888_WITH_MASK(dsc) \ + lv_rgb565_blend_normal_to_argb8888_with_mask_neon(dsc) +#endif + +#ifndef LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_ARGB8888_MIX_MASK_OPA +#define LV_DRAW_SW_RGB565_BLEND_NORMAL_TO_ARGB8888_MIX_MASK_OPA(dsc) \ + lv_rgb565_blend_normal_to_argb8888_mix_mask_opa_neon(dsc) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_ARGB8888 +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_ARGB8888(dsc, src_px_size) \ + lv_rgb888_blend_normal_to_argb8888_neon(dsc, src_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_ARGB8888_WITH_OPA +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_ARGB8888_WITH_OPA(dsc, src_px_size) \ + lv_rgb888_blend_normal_to_argb8888_with_opa_neon(dsc, src_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_ARGB8888_WITH_MASK +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_ARGB8888_WITH_MASK(dsc, src_px_size) \ + lv_rgb888_blend_normal_to_argb8888_with_mask_neon(dsc, src_px_size) +#endif + +#ifndef LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_ARGB8888_MIX_MASK_OPA +#define LV_DRAW_SW_RGB888_BLEND_NORMAL_TO_ARGB8888_MIX_MASK_OPA(dsc, src_px_size) \ + lv_rgb888_blend_normal_to_argb8888_mix_mask_opa_neon(dsc, src_px_size) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_ARGB8888 +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_ARGB8888(dsc) \ + lv_argb8888_blend_normal_to_argb8888_neon(dsc) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_ARGB8888_WITH_OPA +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_ARGB8888_WITH_OPA(dsc) \ + lv_argb8888_blend_normal_to_argb8888_with_opa_neon(dsc) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_ARGB8888_WITH_MASK +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_ARGB8888_WITH_MASK(dsc) \ + lv_argb8888_blend_normal_to_argb8888_with_mask_neon(dsc) +#endif + +#ifndef LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_ARGB8888_MIX_MASK_OPA +#define LV_DRAW_SW_ARGB8888_BLEND_NORMAL_TO_ARGB8888_MIX_MASK_OPA(dsc) \ + lv_argb8888_blend_normal_to_argb8888_mix_mask_opa_neon(dsc) +#endif + +/********************** + * TYPEDEFS + **********************/ + +typedef struct { + uint32_t opa; + void * dst_buf; + uint32_t dst_w; + uint32_t dst_h; + uint32_t dst_stride; + const void * src_buf; + uint32_t src_stride; + const lv_opa_t * mask_buf; + uint32_t mask_stride; +} asm_dsc_t; +/********************** + * GLOBAL PROTOTYPES + **********************/ + +extern LVGL_HIDDEN void _lv_color_blend_to_rgb565_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_color_blend_to_rgb565_neon(lv_draw_sw_blend_fill_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = &dsc->color + }; + + _lv_color_blend_to_rgb565_neon(&asm_dsc); + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_color_blend_to_rgb565_with_opa_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_color_blend_to_rgb565_with_opa_neon(lv_draw_sw_blend_fill_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = &dsc->color + }; + _lv_color_blend_to_rgb565_with_opa_neon(&asm_dsc); + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_color_blend_to_rgb565_with_mask_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_color_blend_to_rgb565_with_mask_neon(lv_draw_sw_blend_fill_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = &dsc->color, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + _lv_color_blend_to_rgb565_with_mask_neon(&asm_dsc); + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_color_blend_to_rgb565_mix_mask_opa_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_color_blend_to_rgb565_mix_mask_opa_neon(lv_draw_sw_blend_fill_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = &dsc->color, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + _lv_color_blend_to_rgb565_mix_mask_opa_neon(&asm_dsc); + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_rgb565_blend_normal_to_rgb565_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb565_blend_normal_to_rgb565_neon(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + _lv_rgb565_blend_normal_to_rgb565_neon(&asm_dsc); + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_rgb565_blend_normal_to_rgb565_with_opa_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb565_blend_normal_to_rgb565_with_opa_neon(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + _lv_rgb565_blend_normal_to_rgb565_with_opa_neon(&asm_dsc); + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_rgb565_blend_normal_to_rgb565_with_mask_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb565_blend_normal_to_rgb565_with_mask_neon(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + _lv_rgb565_blend_normal_to_rgb565_with_mask_neon(&asm_dsc); + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_rgb565_blend_normal_to_rgb565_mix_mask_opa_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb565_blend_normal_to_rgb565_mix_mask_opa_neon(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + _lv_rgb565_blend_normal_to_rgb565_mix_mask_opa_neon(&asm_dsc); + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_rgb888_blend_normal_to_rgb565_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_xrgb8888_blend_normal_to_rgb565_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb888_blend_normal_to_rgb565_neon(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t src_px_size) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + if(src_px_size == 3) { + _lv_rgb888_blend_normal_to_rgb565_neon(&asm_dsc); + } + else { + _lv_xrgb8888_blend_normal_to_rgb565_neon(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_rgb888_blend_normal_to_rgb565_with_opa_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_xrgb8888_blend_normal_to_rgb565_with_opa_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb888_blend_normal_to_rgb565_with_opa_neon(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t src_px_size) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + if(src_px_size == 3) { + _lv_rgb888_blend_normal_to_rgb565_with_opa_neon(&asm_dsc); + } + else { + _lv_xrgb8888_blend_normal_to_rgb565_with_opa_neon(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_rgb888_blend_normal_to_rgb565_with_mask_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_xrgb8888_blend_normal_to_rgb565_with_mask_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb888_blend_normal_to_rgb565_with_mask_neon(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t src_px_size) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + if(src_px_size == 3) { + _lv_rgb888_blend_normal_to_rgb565_with_mask_neon(&asm_dsc); + } + else { + _lv_xrgb8888_blend_normal_to_rgb565_with_mask_neon(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_rgb888_blend_normal_to_rgb565_mix_mask_opa_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_xrgb8888_blend_normal_to_rgb565_mix_mask_opa_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb888_blend_normal_to_rgb565_mix_mask_opa_neon(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t src_px_size) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + if(src_px_size == 3) { + _lv_rgb888_blend_normal_to_rgb565_mix_mask_opa_neon(&asm_dsc); + } + else { + _lv_xrgb8888_blend_normal_to_rgb565_mix_mask_opa_neon(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_argb8888_blend_normal_to_rgb565_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_argb8888_blend_normal_to_rgb565_neon(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + _lv_argb8888_blend_normal_to_rgb565_neon(&asm_dsc); + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_argb8888_blend_normal_to_rgb565_with_opa_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_argb8888_blend_normal_to_rgb565_with_opa_neon(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + _lv_argb8888_blend_normal_to_rgb565_with_opa_neon(&asm_dsc); + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_argb8888_blend_normal_to_rgb565_with_mask_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_argb8888_blend_normal_to_rgb565_with_mask_neon(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + _lv_argb8888_blend_normal_to_rgb565_with_mask_neon(&asm_dsc); + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_argb8888_blend_normal_to_rgb565_mix_mask_opa_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_argb8888_blend_normal_to_rgb565_mix_mask_opa_neon(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + _lv_argb8888_blend_normal_to_rgb565_mix_mask_opa_neon(&asm_dsc); + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_color_blend_to_rgb888_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_color_blend_to_xrgb8888_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_color_blend_to_rgb888_neon(lv_draw_sw_blend_fill_dsc_t * dsc, uint32_t dst_px_size) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = &dsc->color + }; + if(dst_px_size == 3) { + _lv_color_blend_to_rgb888_neon(&asm_dsc); + } + else { + _lv_color_blend_to_xrgb8888_neon(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_color_blend_to_rgb888_with_opa_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_color_blend_to_xrgb8888_with_opa_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_color_blend_to_rgb888_with_opa_neon(lv_draw_sw_blend_fill_dsc_t * dsc, + uint32_t dst_px_size) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = &dsc->color + }; + if(dst_px_size == 3) { + _lv_color_blend_to_rgb888_with_opa_neon(&asm_dsc); + } + else { + _lv_color_blend_to_xrgb8888_with_opa_neon(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_color_blend_to_rgb888_with_mask_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_color_blend_to_xrgb8888_with_mask_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_color_blend_to_rgb888_with_mask_neon(lv_draw_sw_blend_fill_dsc_t * dsc, + uint32_t dst_px_size) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = &dsc->color, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + if(dst_px_size == 3) { + _lv_color_blend_to_rgb888_with_mask_neon(&asm_dsc); + } + else { + _lv_color_blend_to_xrgb8888_with_mask_neon(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_color_blend_to_rgb888_mix_mask_opa_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_color_blend_to_xrgb8888_mix_mask_opa_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_color_blend_to_rgb888_mix_mask_opa_neon(lv_draw_sw_blend_fill_dsc_t * dsc, + uint32_t dst_px_size) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = &dsc->color, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + if(dst_px_size == 3) { + _lv_color_blend_to_rgb888_mix_mask_opa_neon(&asm_dsc); + } + else { + _lv_color_blend_to_xrgb8888_mix_mask_opa_neon(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_rgb565_blend_normal_to_rgb888_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_rgb565_blend_normal_to_xrgb8888_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb565_blend_normal_to_rgb888_neon(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + if(dst_px_size == 3) { + _lv_rgb565_blend_normal_to_rgb888_neon(&asm_dsc); + } + else { + _lv_rgb565_blend_normal_to_xrgb8888_neon(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_rgb565_blend_normal_to_rgb888_with_opa_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_rgb565_blend_normal_to_xrgb8888_with_opa_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb565_blend_normal_to_rgb888_with_opa_neon(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + if(dst_px_size == 3) { + _lv_rgb565_blend_normal_to_rgb888_with_opa_neon(&asm_dsc); + } + else { + _lv_rgb565_blend_normal_to_xrgb8888_with_opa_neon(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_rgb565_blend_normal_to_rgb888_with_mask_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_rgb565_blend_normal_to_xrgb8888_with_mask_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb565_blend_normal_to_rgb888_with_mask_neon(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + if(dst_px_size == 3) { + _lv_rgb565_blend_normal_to_rgb888_with_mask_neon(&asm_dsc); + } + else { + _lv_rgb565_blend_normal_to_xrgb8888_with_mask_neon(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_rgb565_blend_normal_to_rgb888_mix_mask_opa_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_rgb565_blend_normal_to_xrgb8888_mix_mask_opa_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb565_blend_normal_to_rgb888_mix_mask_opa_neon(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + if(dst_px_size == 3) { + _lv_rgb565_blend_normal_to_rgb888_mix_mask_opa_neon(&asm_dsc); + } + else { + _lv_rgb565_blend_normal_to_xrgb8888_mix_mask_opa_neon(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_rgb888_blend_normal_to_rgb888_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_rgb888_blend_normal_to_xrgb8888_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_xrgb8888_blend_normal_to_rgb888_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_xrgb8888_blend_normal_to_xrgb8888_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb888_blend_normal_to_rgb888_neon(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size, + uint32_t src_px_size) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + if(dst_px_size == 3) { + if(src_px_size == 3) { + _lv_rgb888_blend_normal_to_rgb888_neon(&asm_dsc); + } + else { + _lv_xrgb8888_blend_normal_to_rgb888_neon(&asm_dsc); + } + } + else { + if(src_px_size == 3) { + _lv_rgb888_blend_normal_to_xrgb8888_neon(&asm_dsc); + } + else { + _lv_xrgb8888_blend_normal_to_xrgb8888_neon(&asm_dsc); + } + } + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_rgb888_blend_normal_to_rgb888_with_opa_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_rgb888_blend_normal_to_xrgb8888_with_opa_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_xrgb8888_blend_normal_to_rgb888_with_opa_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_xrgb8888_blend_normal_to_xrgb8888_with_opa_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb888_blend_normal_to_rgb888_with_opa_neon(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size, uint32_t src_px_size) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + if(dst_px_size == 3) { + if(src_px_size == 3) { + _lv_rgb888_blend_normal_to_rgb888_with_opa_neon(&asm_dsc); + } + else { + _lv_xrgb8888_blend_normal_to_rgb888_with_opa_neon(&asm_dsc); + } + } + else { + if(src_px_size == 3) { + _lv_rgb888_blend_normal_to_xrgb8888_with_opa_neon(&asm_dsc); + } + else { + _lv_xrgb8888_blend_normal_to_xrgb8888_with_opa_neon(&asm_dsc); + } + } + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_rgb888_blend_normal_to_rgb888_with_mask_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_rgb888_blend_normal_to_xrgb8888_with_mask_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_xrgb8888_blend_normal_to_rgb888_with_mask_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_xrgb8888_blend_normal_to_xrgb8888_with_mask_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb888_blend_normal_to_rgb888_with_mask_neon(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size, uint32_t src_px_size) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + if(dst_px_size == 3) { + if(src_px_size == 3) { + _lv_rgb888_blend_normal_to_rgb888_with_mask_neon(&asm_dsc); + } + else { + _lv_xrgb8888_blend_normal_to_rgb888_with_mask_neon(&asm_dsc); + } + } + else { + if(src_px_size == 3) { + _lv_rgb888_blend_normal_to_xrgb8888_with_mask_neon(&asm_dsc); + } + else { + _lv_xrgb8888_blend_normal_to_xrgb8888_with_mask_neon(&asm_dsc); + } + } + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_rgb888_blend_normal_to_rgb888_mix_mask_opa_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_rgb888_blend_normal_to_xrgb8888_mix_mask_opa_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_xrgb8888_blend_normal_to_rgb888_mix_mask_opa_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_xrgb8888_blend_normal_to_xrgb8888_mix_mask_opa_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb888_blend_normal_to_rgb888_mix_mask_opa_neon(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size, uint32_t src_px_size) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + if(dst_px_size == 3) { + if(src_px_size == 3) { + _lv_rgb888_blend_normal_to_rgb888_mix_mask_opa_neon(&asm_dsc); + } + else { + _lv_xrgb8888_blend_normal_to_rgb888_mix_mask_opa_neon(&asm_dsc); + } + } + else { + if(src_px_size == 3) { + _lv_rgb888_blend_normal_to_xrgb8888_mix_mask_opa_neon(&asm_dsc); + } + else { + _lv_xrgb8888_blend_normal_to_xrgb8888_mix_mask_opa_neon(&asm_dsc); + } + } + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_argb8888_blend_normal_to_rgb888_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_argb8888_blend_normal_to_xrgb8888_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_argb8888_blend_normal_to_rgb888_neon(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + if(dst_px_size == 3) { + _lv_argb8888_blend_normal_to_rgb888_neon(&asm_dsc); + } + else { + _lv_argb8888_blend_normal_to_xrgb8888_neon(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_argb8888_blend_normal_to_rgb888_with_opa_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_argb8888_blend_normal_to_xrgb8888_with_opa_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_argb8888_blend_normal_to_rgb888_with_opa_neon(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + if(dst_px_size == 3) { + _lv_argb8888_blend_normal_to_rgb888_with_opa_neon(&asm_dsc); + } + else { + _lv_argb8888_blend_normal_to_xrgb8888_with_opa_neon(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_argb8888_blend_normal_to_rgb888_with_mask_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_argb8888_blend_normal_to_xrgb8888_with_mask_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_argb8888_blend_normal_to_rgb888_with_mask_neon(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + if(dst_px_size == 3) { + _lv_argb8888_blend_normal_to_rgb888_with_mask_neon(&asm_dsc); + } + else { + _lv_argb8888_blend_normal_to_xrgb8888_with_mask_neon(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_argb8888_blend_normal_to_rgb888_mix_mask_opa_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_argb8888_blend_normal_to_xrgb8888_mix_mask_opa_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_argb8888_blend_normal_to_rgb888_mix_mask_opa_neon(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t dst_px_size) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + if(dst_px_size == 3) { + _lv_argb8888_blend_normal_to_rgb888_mix_mask_opa_neon(&asm_dsc); + } + else { + _lv_argb8888_blend_normal_to_xrgb8888_mix_mask_opa_neon(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_color_blend_to_argb8888_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_color_blend_to_argb8888_neon(lv_draw_sw_blend_fill_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = &dsc->color + }; + + _lv_color_blend_to_argb8888_neon(&asm_dsc); + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_color_blend_to_argb8888_with_opa_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_color_blend_to_argb8888_with_opa_neon(lv_draw_sw_blend_fill_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = &dsc->color + }; + _lv_color_blend_to_argb8888_with_opa_neon(&asm_dsc); + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_color_blend_to_argb8888_with_mask_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_color_blend_to_argb8888_with_mask_neon(lv_draw_sw_blend_fill_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = &dsc->color, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + _lv_color_blend_to_argb8888_with_mask_neon(&asm_dsc); + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_color_blend_to_argb8888_mix_mask_opa_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_color_blend_to_argb8888_mix_mask_opa_neon(lv_draw_sw_blend_fill_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = &dsc->color, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + _lv_color_blend_to_argb8888_mix_mask_opa_neon(&asm_dsc); + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_rgb565_blend_normal_to_argb8888_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb565_blend_normal_to_argb8888_neon(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + _lv_rgb565_blend_normal_to_argb8888_neon(&asm_dsc); + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_rgb565_blend_normal_to_argb8888_with_opa_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb565_blend_normal_to_argb8888_with_opa_neon(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + _lv_rgb565_blend_normal_to_argb8888_with_opa_neon(&asm_dsc); + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_rgb565_blend_normal_to_argb8888_with_mask_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb565_blend_normal_to_argb8888_with_mask_neon(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + _lv_rgb565_blend_normal_to_argb8888_with_mask_neon(&asm_dsc); + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_rgb565_blend_normal_to_argb8888_mix_mask_opa_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb565_blend_normal_to_argb8888_mix_mask_opa_neon(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + _lv_rgb565_blend_normal_to_argb8888_mix_mask_opa_neon(&asm_dsc); + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_rgb888_blend_normal_to_argb8888_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_xrgb8888_blend_normal_to_argb8888_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb888_blend_normal_to_argb8888_neon(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t src_px_size) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + if(src_px_size == 3) { + _lv_rgb888_blend_normal_to_argb8888_neon(&asm_dsc); + } + else { + _lv_xrgb8888_blend_normal_to_argb8888_neon(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_rgb888_blend_normal_to_argb8888_with_opa_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_xrgb8888_blend_normal_to_argb8888_with_opa_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb888_blend_normal_to_argb8888_with_opa_neon(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t src_px_size) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + if(src_px_size == 3) { + _lv_rgb888_blend_normal_to_argb8888_with_opa_neon(&asm_dsc); + } + else { + _lv_xrgb8888_blend_normal_to_argb8888_with_opa_neon(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_rgb888_blend_normal_to_argb8888_with_mask_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_xrgb8888_blend_normal_to_argb8888_with_mask_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb888_blend_normal_to_argb8888_with_mask_neon(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t src_px_size) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + if(src_px_size == 3) { + _lv_rgb888_blend_normal_to_argb8888_with_mask_neon(&asm_dsc); + } + else { + _lv_xrgb8888_blend_normal_to_argb8888_with_mask_neon(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_rgb888_blend_normal_to_argb8888_mix_mask_opa_neon(asm_dsc_t * dsc); +extern LVGL_HIDDEN void _lv_xrgb8888_blend_normal_to_argb8888_mix_mask_opa_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_rgb888_blend_normal_to_argb8888_mix_mask_opa_neon(lv_draw_sw_blend_image_dsc_t * dsc, + uint32_t src_px_size) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + if(src_px_size == 3) { + _lv_rgb888_blend_normal_to_argb8888_mix_mask_opa_neon(&asm_dsc); + } + else { + _lv_xrgb8888_blend_normal_to_argb8888_mix_mask_opa_neon(&asm_dsc); + } + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_argb8888_blend_normal_to_argb8888_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_argb8888_blend_normal_to_argb8888_neon(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + _lv_argb8888_blend_normal_to_argb8888_neon(&asm_dsc); + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_argb8888_blend_normal_to_argb8888_with_opa_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_argb8888_blend_normal_to_argb8888_with_opa_neon(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride + }; + _lv_argb8888_blend_normal_to_argb8888_with_opa_neon(&asm_dsc); + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_argb8888_blend_normal_to_argb8888_with_mask_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_argb8888_blend_normal_to_argb8888_with_mask_neon(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + _lv_argb8888_blend_normal_to_argb8888_with_mask_neon(&asm_dsc); + return LV_RESULT_OK; +} + +extern LVGL_HIDDEN void _lv_argb8888_blend_normal_to_argb8888_mix_mask_opa_neon(asm_dsc_t * dsc); +static inline lv_result_t lv_argb8888_blend_normal_to_argb8888_mix_mask_opa_neon(lv_draw_sw_blend_image_dsc_t * dsc) +{ + asm_dsc_t asm_dsc = { + .opa = dsc->opa, + .dst_buf = dsc->dest_buf, + .dst_w = dsc->dest_w, + .dst_h = dsc->dest_h, + .dst_stride = dsc->dest_stride, + .src_buf = dsc->src_buf, + .src_stride = dsc->src_stride, + .mask_buf = dsc->mask_buf, + .mask_stride = dsc->mask_stride + }; + _lv_argb8888_blend_normal_to_argb8888_mix_mask_opa_neon(&asm_dsc); + return LV_RESULT_OK; +} + +#endif /* !defined(__ASSEMBLY__) */ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_BLEND_NEON_H*/ diff --git a/include/liblvgl/draw/sw/lv_draw_sw.h b/include/liblvgl/draw/sw/lv_draw_sw.h index 4496612d..1152fe58 100644 --- a/include/liblvgl/draw/sw/lv_draw_sw.h +++ b/include/liblvgl/draw/sw/lv_draw_sw.h @@ -13,81 +13,177 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "lv_draw_sw_blend.h" -#include "liblvgl/draw/lv_draw.h" -#include "liblvgl/misc/lv_area.h" -#include "liblvgl/misc/lv_color.h" -#include "liblvgl/hal/lv_hal_disp.h" +#include "../lv_draw.h" +#if LV_USE_DRAW_SW + +#include "../../misc/lv_area.h" +#include "../../misc/lv_color.h" +#include "../../display/lv_display.h" +#include "../../osal/lv_os.h" + +#include "../lv_draw_vector.h" +#include "../lv_draw_triangle.h" +#include "../lv_draw_label.h" +#include "../lv_draw_image.h" +#include "../lv_draw_line.h" +#include "../lv_draw_arc.h" /********************* * DEFINES *********************/ /********************** - * TYPEDEFS + * GLOBAL PROTOTYPES **********************/ -struct _lv_disp_drv_t; - -typedef struct { - lv_draw_ctx_t base_draw; - - /** Fill an area of the destination buffer with a color*/ - void (*blend)(lv_draw_ctx_t * draw_ctx, const lv_draw_sw_blend_dsc_t * dsc); -} lv_draw_sw_ctx_t; - -typedef struct { - lv_draw_layer_ctx_t base_draw; +/** + * Initialize the SW renderer. Called in internally. + * It creates as many SW renderers as defined in LV_DRAW_SW_DRAW_UNIT_CNT + */ +void lv_draw_sw_init(void); - uint32_t buf_size_bytes: 31; - uint32_t has_alpha : 1; -} lv_draw_sw_layer_ctx_t; +/** + * Deinitialize the SW renderers + */ +void lv_draw_sw_deinit(void); -/********************** - * GLOBAL PROTOTYPES - **********************/ +/** + * Fill an area using SW render. Handle gradient and radius. + * @param draw_unit pointer to a draw unit + * @param dsc the draw descriptor + * @param coords the coordinates of the rectangle + */ +void lv_draw_sw_fill(lv_draw_unit_t * draw_unit, lv_draw_fill_dsc_t * dsc, const lv_area_t * coords); -void lv_draw_sw_init_ctx(struct _lv_disp_drv_t * drv, lv_draw_ctx_t * draw_ctx); -void lv_draw_sw_deinit_ctx(struct _lv_disp_drv_t * drv, lv_draw_ctx_t * draw_ctx); +/** + * Draw border with SW render. + * @param draw_unit pointer to a draw unit + * @param dsc the draw descriptor + * @param coords the coordinates of the rectangle + */ +void lv_draw_sw_border(lv_draw_unit_t * draw_unit, const lv_draw_border_dsc_t * dsc, const lv_area_t * coords); -void lv_draw_sw_wait_for_finish(lv_draw_ctx_t * draw_ctx); +/** + * Draw box shadow with SW render. + * @param draw_unit pointer to a draw unit + * @param dsc the draw descriptor + * @param coords the coordinates of the rectangle for which the box shadow should be drawn + */ +void lv_draw_sw_box_shadow(lv_draw_unit_t * draw_unit, const lv_draw_box_shadow_dsc_t * dsc, const lv_area_t * coords); -void lv_draw_sw_arc(lv_draw_ctx_t * draw_ctx, const lv_draw_arc_dsc_t * dsc, const lv_point_t * center, uint16_t radius, - uint16_t start_angle, uint16_t end_angle); +/** + * Draw an image with SW render. It handles image decoding, tiling, transformations, and recoloring. + * @param draw_unit pointer to a draw unit + * @param draw_dsc the draw descriptor + * @param coords the coordinates of the image + */ +void lv_draw_sw_image(lv_draw_unit_t * draw_unit, const lv_draw_image_dsc_t * draw_dsc, + const lv_area_t * coords); -void lv_draw_sw_rect(lv_draw_ctx_t * draw_ctx, const lv_draw_rect_dsc_t * dsc, const lv_area_t * coords); +/** + * Draw a label with SW render. + * @param draw_unit pointer to a draw unit + * @param dsc the draw descriptor + * @param coords the coordinates of the label + */ +void lv_draw_sw_label(lv_draw_unit_t * draw_unit, const lv_draw_label_dsc_t * dsc, const lv_area_t * coords); -void lv_draw_sw_bg(lv_draw_ctx_t * draw_ctx, const lv_draw_rect_dsc_t * dsc, const lv_area_t * coords); -void lv_draw_sw_letter(lv_draw_ctx_t * draw_ctx, const lv_draw_label_dsc_t * dsc, const lv_point_t * pos_p, - uint32_t letter); +/** + * Draw an arc with SW render. + * @param draw_unit pointer to a draw unit + * @param dsc the draw descriptor + * @param coords the coordinates of the arc + */ +void lv_draw_sw_arc(lv_draw_unit_t * draw_unit, const lv_draw_arc_dsc_t * dsc, const lv_area_t * coords); -LV_ATTRIBUTE_FAST_MEM void lv_draw_sw_img_decoded(struct _lv_draw_ctx_t * draw_ctx, const lv_draw_img_dsc_t * draw_dsc, - const lv_area_t * coords, const uint8_t * src_buf, lv_img_cf_t cf); +/** + * Draw a line with SW render. + * @param draw_unit pointer to a draw unit + * @param dsc the draw descriptor + */ +void lv_draw_sw_line(lv_draw_unit_t * draw_unit, const lv_draw_line_dsc_t * dsc); -LV_ATTRIBUTE_FAST_MEM void lv_draw_sw_line(struct _lv_draw_ctx_t * draw_ctx, const lv_draw_line_dsc_t * dsc, - const lv_point_t * point1, const lv_point_t * point2); +/** + * Blend a layer with SW render + * @param draw_unit pointer to a draw unit + * @param draw_dsc the draw descriptor + * @param coords the coordinates of the layer + */ +void lv_draw_sw_layer(lv_draw_unit_t * draw_unit, const lv_draw_image_dsc_t * draw_dsc, const lv_area_t * coords); -void lv_draw_sw_polygon(struct _lv_draw_ctx_t * draw_ctx, const lv_draw_rect_dsc_t * draw_dsc, - const lv_point_t * points, uint16_t point_cnt); +/** + * Draw a triangle with SW render. + * @param draw_unit pointer to a draw unit + * @param dsc the draw descriptor + */ +void lv_draw_sw_triangle(lv_draw_unit_t * draw_unit, const lv_draw_triangle_dsc_t * dsc); -void lv_draw_sw_buffer_copy(lv_draw_ctx_t * draw_ctx, - void * dest_buf, lv_coord_t dest_stride, const lv_area_t * dest_area, - void * src_buf, lv_coord_t src_stride, const lv_area_t * src_area); +/** + * Mask out a rectangle with radius from a current layer + * @param draw_unit pointer to a draw unit + * @param dsc the draw descriptor + * @param coords the coordinates of the mask + */ +void lv_draw_sw_mask_rect(lv_draw_unit_t * draw_unit, const lv_draw_mask_rect_dsc_t * dsc, const lv_area_t * coords); -void lv_draw_sw_transform(lv_draw_ctx_t * draw_ctx, const lv_area_t * dest_area, const void * src_buf, - lv_coord_t src_w, lv_coord_t src_h, lv_coord_t src_stride, - const lv_draw_img_dsc_t * draw_dsc, lv_img_cf_t cf, lv_color_t * cbuf, lv_opa_t * abuf); +/** + * Used internally to get a transformed are of an image + * @param draw_unit pointer to a draw unit + * @param dest_area area to calculate, i.e. get this area from the transformed image + * @param src_buf source buffer + * @param src_w source buffer width in pixels + * @param src_h source buffer height in pixels + * @param src_stride source buffer stride in bytes + * @param draw_dsc draw descriptor + * @param sup supplementary data + * @param cf color format of the source buffer + * @param dest_buf the destination buffer + */ +void lv_draw_sw_transform(lv_draw_unit_t * draw_unit, const lv_area_t * dest_area, const void * src_buf, + int32_t src_w, int32_t src_h, int32_t src_stride, + const lv_draw_image_dsc_t * draw_dsc, const lv_draw_image_sup_t * sup, lv_color_format_t cf, void * dest_buf); -struct _lv_draw_layer_ctx_t * lv_draw_sw_layer_create(struct _lv_draw_ctx_t * draw_ctx, lv_draw_layer_ctx_t * layer_ctx, - lv_draw_layer_flags_t flags); +#if LV_USE_VECTOR_GRAPHIC && LV_USE_THORVG +/** + * Draw vector graphics with SW render. + * @param draw_unit pointer to a draw unit + * @param dsc the draw descriptor + */ +void lv_draw_sw_vector(lv_draw_unit_t * draw_unit, const lv_draw_vector_task_dsc_t * dsc); +#endif -void lv_draw_sw_layer_adjust(struct _lv_draw_ctx_t * draw_ctx, struct _lv_draw_layer_ctx_t * layer_ctx, - lv_draw_layer_flags_t flags); +/** + * Swap the upper and lower byte of an RGB565 buffer. + * Might be required if a 8bit parallel port or an SPI port send the bytes in the wrong order. + * The bytes will be swapped in place. + * @param buf pointer to buffer + * @param buf_size_px number of pixels in the buffer + */ +void lv_draw_sw_rgb565_swap(void * buf, uint32_t buf_size_px); -void lv_draw_sw_layer_blend(struct _lv_draw_ctx_t * draw_ctx, struct _lv_draw_layer_ctx_t * layer_ctx, - const lv_draw_img_dsc_t * draw_dsc); +/** + * Invert a draw buffer in the I1 color format. + * Conventionally, a bit is set to 1 during blending if the luminance is greater than 127. + * Depending on the display controller used, you might want to have different behavior. + * The inversion will be performed in place. + * @param buf pointer to the buffer to be inverted + * @param buf_size size of the buffer in bytes + */ +void lv_draw_sw_i1_invert(void * buf, uint32_t buf_size); -void lv_draw_sw_layer_destroy(lv_draw_ctx_t * draw_ctx, lv_draw_layer_ctx_t * layer_ctx); +/** + * Rotate a buffer into another buffer + * @param src the source buffer + * @param dest the destination buffer + * @param src_width source width in pixels + * @param src_height source height in pixels + * @param src_stride source stride in bytes (number of bytes in a row) + * @param dest_stride destination stride in bytes (number of bytes in a row) + * @param rotation LV_DISPLAY_ROTATION_0/90/180/270 + * @param color_format LV_COLOR_FORMAT_RGB565/RGB888/XRGB8888/ARGB8888 + */ +void lv_draw_sw_rotate(const void * src, void * dest, int32_t src_width, int32_t src_height, int32_t src_stride, + int32_t dest_stride, lv_display_rotation_t rotation, lv_color_format_t color_format); /*********************** * GLOBAL VARIABLES @@ -97,6 +193,10 @@ void lv_draw_sw_layer_destroy(lv_draw_ctx_t * draw_ctx, lv_draw_layer_ctx_t * la * MACROS **********************/ +#include "blend/lv_draw_sw_blend.h" + +#endif /*LV_USE_DRAW_SW*/ + #ifdef __cplusplus } /*extern "C"*/ #endif diff --git a/include/liblvgl/draw/sw/lv_draw_sw_blend.h b/include/liblvgl/draw/sw/lv_draw_sw_blend.h deleted file mode 100644 index 6fa859c9..00000000 --- a/include/liblvgl/draw/sw/lv_draw_sw_blend.h +++ /dev/null @@ -1,69 +0,0 @@ -/** - * @file lv_draw_sw_blend.h - * - */ - -#ifndef LV_DRAW_SW_BLEND_H -#define LV_DRAW_SW_BLEND_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/misc/lv_color.h" -#include "liblvgl/misc/lv_area.h" -#include "liblvgl/misc/lv_style.h" -#include "liblvgl/draw/lv_draw_mask.h" - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -typedef struct { - const lv_area_t * blend_area; /**< The area with absolute coordinates to draw on `draw_ctx->buf` - * will be clipped to `draw_ctx->clip_area` */ - const lv_color_t * src_buf; /**< Pointer to an image to blend. If set `fill_color` is ignored */ - lv_color_t color; /**< Fill color*/ - lv_opa_t * mask_buf; /**< NULL if ignored, or an alpha mask to apply on `blend_area`*/ - lv_draw_mask_res_t mask_res; /**< The result of the previous mask operation */ - const lv_area_t * mask_area; /**< The area of `mask_buf` with absolute coordinates*/ - lv_opa_t opa; /**< The overall opacity*/ - lv_blend_mode_t blend_mode; /**< E.g. LV_BLEND_MODE_ADDITIVE*/ -} lv_draw_sw_blend_dsc_t; - -struct _lv_draw_ctx_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Call the blend function of the `draw_ctx`. - * @param draw_ctx pointer to a draw context - * @param dsc pointer to an initialized blend descriptor - */ -void lv_draw_sw_blend(struct _lv_draw_ctx_t * draw_ctx, const lv_draw_sw_blend_dsc_t * dsc); - -/** - * The basic blend function used with software rendering. - * @param draw_ctx pointer to a draw context - * @param dsc pointer to an initialized blend descriptor - */ -LV_ATTRIBUTE_FAST_MEM void lv_draw_sw_blend_basic(struct _lv_draw_ctx_t * draw_ctx, const lv_draw_sw_blend_dsc_t * dsc); - -/********************** - * MACROS - **********************/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_DRAW_SW_BLEND_H*/ diff --git a/include/liblvgl/draw/sw/lv_draw_sw_dither.h b/include/liblvgl/draw/sw/lv_draw_sw_dither.h deleted file mode 100644 index 9785e187..00000000 --- a/include/liblvgl/draw/sw/lv_draw_sw_dither.h +++ /dev/null @@ -1,70 +0,0 @@ -/** - * @file lv_draw_sw_dither.h - * - */ - -#ifndef LV_DRAW_SW_DITHER_H -#define LV_DRAW_SW_DITHER_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/core/lv_obj_pos.h" - - -/********************* - * DEFINES - *********************/ -#if LV_COLOR_DEPTH < 32 && LV_DITHER_GRADIENT == 1 -#define _DITHER_GRADIENT 1 -#else -#define _DITHER_GRADIENT 0 -#endif - -/********************** - * TYPEDEFS - **********************/ -#if _DITHER_GRADIENT -/*A signed error color component*/ -typedef struct { - int8_t r, g, b; -} lv_scolor24_t; - -struct _lv_gradient_cache_t; -typedef void (*lv_dither_func_t)(struct _lv_gradient_cache_t * grad, lv_coord_t x, lv_coord_t y, lv_coord_t w); - -#endif - - -/********************** - * PROTOTYPES - **********************/ -#if LV_DRAW_COMPLEX -#if _DITHER_GRADIENT -LV_ATTRIBUTE_FAST_MEM void lv_dither_none(struct _lv_gradient_cache_t * grad, lv_coord_t x, lv_coord_t y, lv_coord_t w); - -LV_ATTRIBUTE_FAST_MEM void lv_dither_ordered_hor(struct _lv_gradient_cache_t * grad, const lv_coord_t xs, - const lv_coord_t y, const lv_coord_t w); -LV_ATTRIBUTE_FAST_MEM void lv_dither_ordered_ver(struct _lv_gradient_cache_t * grad, const lv_coord_t xs, - const lv_coord_t y, const lv_coord_t w); - -#if LV_DITHER_ERROR_DIFFUSION == 1 -LV_ATTRIBUTE_FAST_MEM void lv_dither_err_diff_hor(struct _lv_gradient_cache_t * grad, const lv_coord_t xs, - const lv_coord_t y, const lv_coord_t w); -LV_ATTRIBUTE_FAST_MEM void lv_dither_err_diff_ver(struct _lv_gradient_cache_t * grad, const lv_coord_t xs, - const lv_coord_t y, const lv_coord_t w); -#endif /* LV_DITHER_ERROR_DIFFUSION */ - -#endif /* _DITHER_GRADIENT */ -#endif - - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif diff --git a/include/liblvgl/draw/sw/lv_draw_sw_gradient.h b/include/liblvgl/draw/sw/lv_draw_sw_gradient.h index 0860d50d..e68301bd 100644 --- a/include/liblvgl/draw/sw/lv_draw_sw_gradient.h +++ b/include/liblvgl/draw/sw/lv_draw_sw_gradient.h @@ -13,9 +13,10 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/misc/lv_color.h" -#include "liblvgl/misc/lv_style.h" -#include "lv_draw_sw_dither.h" +#include "../../misc/lv_color.h" +#include "../../misc/lv_style.h" + +#if LV_USE_DRAW_SW /********************* * DEFINES @@ -24,41 +25,16 @@ extern "C" { #error LVGL needs at least 2 stops for gradients. Please increase the LV_GRADIENT_MAX_STOPS #endif +#define LV_GRAD_LEFT LV_PCT(0) +#define LV_GRAD_RIGHT LV_PCT(100) +#define LV_GRAD_TOP LV_PCT(0) +#define LV_GRAD_BOTTOM LV_PCT(100) +#define LV_GRAD_CENTER LV_PCT(50) /********************** * TYPEDEFS **********************/ -#if _DITHER_GRADIENT -typedef lv_color32_t lv_grad_color_t; -#else typedef lv_color_t lv_grad_color_t; -#endif - -/** To avoid recomputing gradient for each draw operation, - * it's possible to cache the computation in this structure instance. - * Whenever possible, this structure is reused instead of recomputing the gradient map */ -typedef struct _lv_gradient_cache_t { - uint32_t key; /**< A discriminating key that's built from the drawing operation. - * If the key does not match, the cache item is not used */ - uint32_t life : 30; /**< A life counter that's incremented on usage. Higher counter is - * less likely to be evicted from the cache */ - uint32_t filled : 1; /**< Used to skip dithering in it if already done */ - uint32_t not_cached: 1; /**< The cache was too small so this item is not managed by the cache*/ - lv_color_t * map; /**< The computed gradient low bitdepth color map, points into the - * cache's buffer, no free needed */ - lv_coord_t alloc_size; /**< The map allocated size in colors */ - lv_coord_t size; /**< The computed gradient color map size, in colors */ -#if _DITHER_GRADIENT - lv_color32_t * hmap; /**< If dithering, we need to store the current, high bitdepth gradient - * map too, points to the cache's buffer, no free needed */ -#if LV_DITHER_ERROR_DIFFUSION == 1 - lv_scolor24_t * error_acc; /**< Error diffusion dithering algorithm requires storing the last error - * drawn, points to the cache's buffer, no free needed */ - lv_coord_t w; /**< The error array width in pixels */ -#endif -#endif -} lv_grad_t; - /********************** * PROTOTYPES @@ -68,21 +44,15 @@ typedef struct _lv_gradient_cache_t { * @param dsc The gradient descriptor to use * @param range The range to use in computation. * @param frac The current part used in the range. frac is in [0; range] + * @param color_out Calculated gradient color + * @param opa_out Calculated opacity */ -LV_ATTRIBUTE_FAST_MEM lv_grad_color_t lv_gradient_calculate(const lv_grad_dsc_t * dsc, lv_coord_t range, - lv_coord_t frac); - -/** - * Set the gradient cache size - * @param max_bytes Max cahce size - */ -void lv_gradient_set_cache_size(size_t max_bytes); -/** Free the gradient cache */ -void lv_gradient_free_cache(void); +void /* LV_ATTRIBUTE_FAST_MEM */ lv_gradient_color_calculate(const lv_grad_dsc_t * dsc, int32_t range, + int32_t frac, lv_grad_color_t * color_out, lv_opa_t * opa_out); /** Get a gradient cache from the given parameters */ -lv_grad_t * lv_gradient_get(const lv_grad_dsc_t * gradient, lv_coord_t w, lv_coord_t h); +lv_grad_t * lv_gradient_get(const lv_grad_dsc_t * gradient, int32_t w, int32_t h); /** * Clean up the gradient item after it was get with `lv_grad_get_from_cache`. @@ -90,6 +60,142 @@ lv_grad_t * lv_gradient_get(const lv_grad_dsc_t * gradient, lv_coord_t w, lv_coo */ void lv_gradient_cleanup(lv_grad_t * grad); +/** + * Initialize gradient color map from a table + * @param grad pointer to a gradient descriptor + * @param colors color array + * @param fracs position array (0..255): if NULL, then colors are distributed evenly + * @param opa opacity array: if NULL, then LV_OPA_COVER is assumed + * @param num_stops number of gradient stops (1..LV_GRADIENT_MAX_STOPS) + */ +void lv_gradient_init_stops(lv_grad_dsc_t * grad, const lv_color_t colors[], const lv_opa_t opa[], + const uint8_t fracs[], int num_stops); + +#if LV_USE_DRAW_SW_COMPLEX_GRADIENTS + +/** + * Helper function to initialize linear gradient + * @param dsc gradient descriptor + * @param from_x start x position: can be a coordinate or an lv_pct() value + * predefined constants LV_GRAD_LEFT, LV_GRAD_RIGHT, LV_GRAD_TOP, LV_GRAD_BOTTOM, LV_GRAD_CENTER can be used as well + * @param from_y start y position + * @param to_x end x position + * @param to_y end y position + * @param extend one of LV_GRAD_EXTEND_PAD, LV_GRAD_EXTEND_REPEAT or LV_GRAD_EXTEND_REFLECT + */ +void lv_grad_linear_init(lv_grad_dsc_t * dsc, int32_t from_x, int32_t from_y, int32_t to_x, int32_t to_y, + lv_grad_extend_t extend); + +/** + * Helper function to initialize radial gradient + * @param dsc gradient descriptor + * @param center_x center x position: can be a coordinate or an lv_pct() value + * predefined constants LV_GRAD_LEFT, LV_GRAD_RIGHT, LV_GRAD_TOP, LV_GRAD_BOTTOM, LV_GRAD_CENTER can be used as well + * @param center_y center y position + * @param to_x point on the end circle x position + * @param to_y point on the end circle y position + * @param extend one of LV_GRAD_EXTEND_PAD, LV_GRAD_EXTEND_REPEAT or LV_GRAD_EXTEND_REFLECT + */ +void lv_grad_radial_init(lv_grad_dsc_t * dsc, int32_t center_x, int32_t center_y, int32_t to_x, int32_t to_y, + lv_grad_extend_t extend); + +/** + * Set focal (starting) circle of a radial gradient + * @param dsc gradient descriptor + * @param center_x center x position: can be a coordinate or an lv_pct() value + * predefined constants LV_GRAD_LEFT, LV_GRAD_RIGHT, LV_GRAD_TOP, LV_GRAD_BOTTOM, LV_GRAD_CENTER can be used as well + * @param center_y center y position + * @param radius radius of the starting circle (NOTE: this must be a scalar number, not percentage) + */ +void lv_grad_radial_set_focal(lv_grad_dsc_t * dsc, int32_t center_x, int32_t center_y, int32_t radius); + +/** + * Helper function to initialize conical gradient + * @param dsc gradient descriptor + * @param center_x center x position: can be a coordinate or an lv_pct() value + * predefined constants LV_GRAD_LEFT, LV_GRAD_RIGHT, LV_GRAD_TOP, LV_GRAD_BOTTOM, LV_GRAD_CENTER can be used as well + * @param center_y center y position + * @param start_angle start angle in degrees + * @param end_angle end angle in degrees + * @param extend one of LV_GRAD_EXTEND_PAD, LV_GRAD_EXTEND_REPEAT or LV_GRAD_EXTEND_REFLECT + */ +void lv_grad_conical_init(lv_grad_dsc_t * dsc, int32_t center_x, int32_t center_y, int32_t start_angle, + int32_t end_angle, lv_grad_extend_t extend); + +/** + * Calculate constants from the given parameters that are used during rendering + * @param dsc gradient descriptor + */ +void lv_gradient_linear_setup(lv_grad_dsc_t * dsc, const lv_area_t * coords); + +/** + * Free up the allocated memory for the gradient calculation + * @param dsc gradient descriptor + */ +void lv_gradient_linear_cleanup(lv_grad_dsc_t * dsc); + +/** + * Calculate a line segment of a linear gradient + * @param dsc gradient descriptor + * @param xp starting point x coordinate in gradient space + * @param yp starting point y coordinate in gradient space + * @param width width of the line segment in pixels + * @param result color buffer for the resulting line segment + */ +void /* LV_ATTRIBUTE_FAST_MEM */ lv_gradient_linear_get_line(lv_grad_dsc_t * dsc, int32_t xp, int32_t yp, int32_t width, + lv_grad_t * result); + +/** + * Calculate constants from the given parameters that are used during rendering + * @param dsc gradient descriptor + */ +void lv_gradient_radial_setup(lv_grad_dsc_t * dsc, const lv_area_t * coords); + +/** + * Free up the allocated memory for the gradient calculation + * @param dsc gradient descriptor + */ +void lv_gradient_radial_cleanup(lv_grad_dsc_t * dsc); + +/** + * Calculate a line segment of a radial gradient + * @param dsc gradient descriptor + * @param xp starting point x coordinate in gradient space + * @param yp starting point y coordinate in gradient space + * @param width width of the line segment in pixels + * @param result color buffer for the resulting line segment + */ +void /* LV_ATTRIBUTE_FAST_MEM */ lv_gradient_radial_get_line(lv_grad_dsc_t * dsc, int32_t xp, int32_t yp, int32_t width, + lv_grad_t * result); + +/** + * Calculate constants from the given parameters that are used during rendering + * @param dsc gradient descriptor + */ +void lv_gradient_conical_setup(lv_grad_dsc_t * dsc, const lv_area_t * coords); + +/** + * Free up the allocated memory for the gradient calculation + * @param dsc gradient descriptor + */ +void lv_gradient_conical_cleanup(lv_grad_dsc_t * dsc); + +/** + * Calculate a line segment of a conical gradient + * @param dsc gradient descriptor + * @param xp starting point x coordinate in gradient space + * @param yp starting point y coordinate in gradient space + * @param width width of the line segment in pixels + * @param result color buffer for the resulting line segment + */ +void /* LV_ATTRIBUTE_FAST_MEM */ lv_gradient_conical_get_line(lv_grad_dsc_t * dsc, int32_t xp, int32_t yp, + int32_t width, + lv_grad_t * result); + +#endif /*LV_USE_DRAW_SW_COMPLEX_GRADIENTS*/ + +#endif /*LV_USE_DRAW_SW*/ + #ifdef __cplusplus } /*extern "C"*/ #endif diff --git a/include/liblvgl/draw/sdl/lv_draw_sdl_stack_blur.h b/include/liblvgl/draw/sw/lv_draw_sw_gradient_private.h similarity index 56% rename from include/liblvgl/draw/sdl/lv_draw_sdl_stack_blur.h rename to include/liblvgl/draw/sw/lv_draw_sw_gradient_private.h index 3d854e3a..a1e14932 100644 --- a/include/liblvgl/draw/sdl/lv_draw_sdl_stack_blur.h +++ b/include/liblvgl/draw/sw/lv_draw_sw_gradient_private.h @@ -1,9 +1,10 @@ /** - * @file lv_draw_sdl_stack_blur.h + * @file lv_draw_sw_gradient_private.h * */ -#ifndef LV_DRAW_SDL_STACK_BLUR_H -#define LV_DRAW_SDL_STACK_BLUR_H + +#ifndef LV_DRAW_SW_GRADIENT_PRIVATE_H +#define LV_DRAW_SW_GRADIENT_PRIVATE_H #ifdef __cplusplus extern "C" { @@ -13,11 +14,9 @@ extern "C" { * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" - -#if LV_USE_GPU_SDL +#include "lv_draw_sw_gradient.h" -#include "../../misc/lv_color.h" +#if LV_USE_DRAW_SW /********************* * DEFINES @@ -27,20 +26,25 @@ extern "C" { * TYPEDEFS **********************/ +struct _lv_grad_t { + lv_color_t * color_map; + lv_opa_t * opa_map; + uint32_t size; +}; + + /********************** * GLOBAL PROTOTYPES **********************/ -void lv_stack_blur_grayscale(lv_opa_t * buf, uint16_t w, uint16_t h, uint16_t r); - /********************** * MACROS **********************/ -#endif /*LV_USE_GPU_SDL*/ +#endif /* LV_USE_DRAW_SW */ #ifdef __cplusplus } /*extern "C"*/ #endif -#endif /*LV_DRAW_SDL_STACK_BLUR_H*/ +#endif /*LV_DRAW_SW_GRADIENT_PRIVATE_H*/ diff --git a/include/liblvgl/draw/sw/lv_draw_sw_mask.h b/include/liblvgl/draw/sw/lv_draw_sw_mask.h new file mode 100644 index 00000000..3f7b306e --- /dev/null +++ b/include/liblvgl/draw/sw/lv_draw_sw_mask.h @@ -0,0 +1,185 @@ +/** + * @file lv_draw_sw_mask.h + * + */ + +#ifndef LV_DRAW_SW_MASK_H +#define LV_DRAW_SW_MASK_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../misc/lv_area.h" +#include "../../misc/lv_color.h" +#include "../../misc/lv_math.h" +#include "../../misc/lv_types.h" + +/********************* + * DEFINES + *********************/ +#define LV_MASK_ID_INV (-1) +#if LV_DRAW_SW_COMPLEX +# define LV_MASK_MAX_NUM 16 +#else +# define LV_MASK_MAX_NUM 1 +#endif + +/********************** + * TYPEDEFS + **********************/ + +typedef enum { + LV_DRAW_SW_MASK_RES_TRANSP, + LV_DRAW_SW_MASK_RES_FULL_COVER, + LV_DRAW_SW_MASK_RES_CHANGED, + LV_DRAW_SW_MASK_RES_UNKNOWN +} lv_draw_sw_mask_res_t; + +#if LV_DRAW_SW_COMPLEX + +typedef enum { + LV_DRAW_SW_MASK_TYPE_LINE, + LV_DRAW_SW_MASK_TYPE_ANGLE, + LV_DRAW_SW_MASK_TYPE_RADIUS, + LV_DRAW_SW_MASK_TYPE_FADE, + LV_DRAW_SW_MASK_TYPE_MAP, +} lv_draw_sw_mask_type_t; + +typedef enum { + LV_DRAW_SW_MASK_LINE_SIDE_LEFT = 0, + LV_DRAW_SW_MASK_LINE_SIDE_RIGHT, + LV_DRAW_SW_MASK_LINE_SIDE_TOP, + LV_DRAW_SW_MASK_LINE_SIDE_BOTTOM, +} lv_draw_sw_mask_line_side_t; + +/** + * A common callback type for every mask type. + * Used internally by the library. + */ +typedef lv_draw_sw_mask_res_t (*lv_draw_sw_mask_xcb_t)(lv_opa_t * mask_buf, int32_t abs_x, int32_t abs_y, + int32_t len, + void * p); + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void lv_draw_sw_mask_init(void); + +void lv_draw_sw_mask_deinit(void); + +//! @cond Doxygen_Suppress + +/** + * Apply the added buffers on a line. Used internally by the library's drawing routines. + * @param masks the masks list to apply, must be ended with NULL pointer in array. + * @param mask_buf store the result mask here. Has to be `len` byte long. Should be initialized with `0xFF`. + * @param abs_x absolute X coordinate where the line to calculate start + * @param abs_y absolute Y coordinate where the line to calculate start + * @param len length of the line to calculate (in pixel count) + * @return One of these values: + * - `LV_DRAW_MASK_RES_FULL_TRANSP`: the whole line is transparent. `mask_buf` is not set to zero + * - `LV_DRAW_MASK_RES_FULL_COVER`: the whole line is fully visible. `mask_buf` is unchanged + * - `LV_DRAW_MASK_RES_CHANGED`: `mask_buf` has changed, it shows the desired opacity of each pixel in the given line + */ +lv_draw_sw_mask_res_t /* LV_ATTRIBUTE_FAST_MEM */ lv_draw_sw_mask_apply(void * masks[], lv_opa_t * mask_buf, + int32_t abs_x, + int32_t abs_y, + int32_t len); + +//! @endcond + +/** + * Free the data from the parameter. + * It's called inside `lv_draw_sw_mask_remove_id` and `lv_draw_sw_mask_remove_custom` + * Needs to be called only in special cases when the mask is not added by `lv_draw_mask_add` + * and not removed by `lv_draw_mask_remove_id` or `lv_draw_mask_remove_custom` + * @param p pointer to a mask parameter + */ +void lv_draw_sw_mask_free_param(void * p); + +/** + *Initialize a line mask from two points. + * @param param pointer to a `lv_draw_mask_param_t` to initialize + * @param p1x X coordinate of the first point of the line + * @param p1y Y coordinate of the first point of the line + * @param p2x X coordinate of the second point of the line + * @param p2y y coordinate of the second point of the line + * @param side and element of `lv_draw_mask_line_side_t` to describe which side to keep. + * With `LV_DRAW_MASK_LINE_SIDE_LEFT/RIGHT` and horizontal line all pixels are kept + * With `LV_DRAW_MASK_LINE_SIDE_TOP/BOTTOM` and vertical line all pixels are kept + */ +void lv_draw_sw_mask_line_points_init(lv_draw_sw_mask_line_param_t * param, int32_t p1x, int32_t p1y, + int32_t p2x, + int32_t p2y, lv_draw_sw_mask_line_side_t side); + +/** + *Initialize a line mask from a point and an angle. + * @param param pointer to a `lv_draw_mask_param_t` to initialize + * @param px X coordinate of a point of the line + * @param py X coordinate of a point of the line + * @param angle right 0 deg, bottom: 90 + * @param side an element of `lv_draw_mask_line_side_t` to describe which side to keep. + * With `LV_DRAW_MASK_LINE_SIDE_LEFT/RIGHT` and horizontal line all pixels are kept + * With `LV_DRAW_MASK_LINE_SIDE_TOP/BOTTOM` and vertical line all pixels are kept + */ +void lv_draw_sw_mask_line_angle_init(lv_draw_sw_mask_line_param_t * param, int32_t px, int32_t py, int16_t angle, + lv_draw_sw_mask_line_side_t side); + +/** + * Initialize an angle mask. + * @param param pointer to a `lv_draw_mask_param_t` to initialize + * @param vertex_x X coordinate of the angle vertex (absolute coordinates) + * @param vertex_y Y coordinate of the angle vertex (absolute coordinates) + * @param start_angle start angle in degrees. 0 deg on the right, 90 deg, on the bottom + * @param end_angle end angle + */ +void lv_draw_sw_mask_angle_init(lv_draw_sw_mask_angle_param_t * param, int32_t vertex_x, int32_t vertex_y, + int32_t start_angle, int32_t end_angle); + +/** + * Initialize a fade mask. + * @param param pointer to an `lv_draw_mask_radius_param_t` to initialize + * @param rect coordinates of the rectangle to affect (absolute coordinates) + * @param radius radius of the rectangle + * @param inv true: keep the pixels inside the rectangle; keep the pixels outside of the rectangle + */ +void lv_draw_sw_mask_radius_init(lv_draw_sw_mask_radius_param_t * param, const lv_area_t * rect, int32_t radius, + bool inv); + +/** + * Initialize a fade mask. + * @param param pointer to a `lv_draw_mask_param_t` to initialize + * @param coords coordinates of the area to affect (absolute coordinates) + * @param opa_top opacity on the top + * @param y_top at which coordinate start to change to opacity to `opa_bottom` + * @param opa_bottom opacity at the bottom + * @param y_bottom at which coordinate reach `opa_bottom`. + */ +void lv_draw_sw_mask_fade_init(lv_draw_sw_mask_fade_param_t * param, const lv_area_t * coords, lv_opa_t opa_top, + int32_t y_top, + lv_opa_t opa_bottom, int32_t y_bottom); + +/** + * Initialize a map mask. + * @param param pointer to a `lv_draw_mask_param_t` to initialize + * @param coords coordinates of the map (absolute coordinates) + * @param map array of bytes with the mask values + */ +void lv_draw_sw_mask_map_init(lv_draw_sw_mask_map_param_t * param, const lv_area_t * coords, const lv_opa_t * map); + +#endif /*LV_DRAW_SW_COMPLEX*/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_SW_MASK_H*/ diff --git a/include/liblvgl/draw/sw/lv_draw_sw_mask_private.h b/include/liblvgl/draw/sw/lv_draw_sw_mask_private.h new file mode 100644 index 00000000..e3c1dd34 --- /dev/null +++ b/include/liblvgl/draw/sw/lv_draw_sw_mask_private.h @@ -0,0 +1,157 @@ +/** + * @file lv_draw_sw_mask_private.h + * + */ + +#ifndef LV_DRAW_SW_MASK_PRIVATE_H +#define LV_DRAW_SW_MASK_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_draw_sw_mask.h" + +#if LV_DRAW_SW_COMPLEX + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef struct { + uint8_t * buf; + lv_opa_t * cir_opa; /**< Opacity of values on the circumference of an 1/4 circle */ + uint16_t * x_start_on_y; /**< The x coordinate of the circle for each y value */ + uint16_t * opa_start_on_y; /**< The index of `cir_opa` for each y value */ + int32_t life; /**< How many times the entry way used */ + uint32_t used_cnt; /**< Like a semaphore to count the referencing masks */ + int32_t radius; /**< The radius of the entry */ +} lv_draw_sw_mask_radius_circle_dsc_t; + +struct _lv_draw_sw_mask_common_dsc_t { + lv_draw_sw_mask_xcb_t cb; + lv_draw_sw_mask_type_t type; +}; + +struct _lv_draw_sw_mask_line_param_t { + /** The first element must be the common descriptor */ + lv_draw_sw_mask_common_dsc_t dsc; + + struct { + /*First point*/ + lv_point_t p1; + + /*Second point*/ + lv_point_t p2; + + /*Which side to keep?*/ + lv_draw_sw_mask_line_side_t side : 2; + } cfg; + + /** A point of the line */ + lv_point_t origo; + + /** X / (1024*Y) steepness (X is 0..1023 range). What is the change of X in 1024 Y? */ + int32_t xy_steep; + + /** Y / (1024*X) steepness (Y is 0..1023 range). What is the change of Y in 1024 X? */ + int32_t yx_steep; + + /** Helper which stores yx_steep for flat lines and xy_steep for steep (non flat) lines */ + int32_t steep; + + /** Steepness in 1 px in 0..255 range. Used only by flat lines. */ + int32_t spx; + + /** 1: It's a flat line? (Near to horizontal) */ + uint8_t flat : 1; + + /** Invert the mask. The default is: Keep the left part. + *It is used to select left/right/top/bottom */ + uint8_t inv: 1; +}; + +struct _lv_draw_sw_mask_angle_param_t { + /** The first element must be the common descriptor */ + lv_draw_sw_mask_common_dsc_t dsc; + + struct { + lv_point_t vertex_p; + int32_t start_angle; + int32_t end_angle; + } cfg; + + lv_draw_sw_mask_line_param_t start_line; + lv_draw_sw_mask_line_param_t end_line; + uint16_t delta_deg; +}; + +struct _lv_draw_sw_mask_radius_param_t { + /** The first element must be the common descriptor */ + lv_draw_sw_mask_common_dsc_t dsc; + + struct { + lv_area_t rect; + int32_t radius; + /** Invert the mask. 0: Keep the pixels inside. */ + uint8_t outer: 1; + } cfg; + + lv_draw_sw_mask_radius_circle_dsc_t * circle; +}; + +struct _lv_draw_sw_mask_fade_param_t { + /** The first element must be the common descriptor */ + lv_draw_sw_mask_common_dsc_t dsc; + + struct { + lv_area_t coords; + int32_t y_top; + int32_t y_bottom; + lv_opa_t opa_top; + lv_opa_t opa_bottom; + } cfg; + +}; + +struct _lv_draw_sw_mask_map_param_t { + /** The first element must be the common descriptor */ + lv_draw_sw_mask_common_dsc_t dsc; + + struct { + lv_area_t coords; + const lv_opa_t * map; + } cfg; +}; + +typedef lv_draw_sw_mask_radius_circle_dsc_t lv_draw_sw_mask_radius_circle_dsc_arr_t[LV_DRAW_SW_CIRCLE_CACHE_SIZE]; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Called by LVGL the rendering of a screen is ready to clean up + * the temporal (cache) data of the masks + */ +void lv_draw_sw_mask_cleanup(void); + +/********************** + * MACROS + **********************/ + +#endif /*LV_DRAW_SW_COMPLEX*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_SW_MASK_PRIVATE_H*/ diff --git a/include/liblvgl/draw/sw/lv_draw_sw_private.h b/include/liblvgl/draw/sw/lv_draw_sw_private.h new file mode 100644 index 00000000..4339a780 --- /dev/null +++ b/include/liblvgl/draw/sw/lv_draw_sw_private.h @@ -0,0 +1,68 @@ +/** + * @file lv_draw_sw_private.h + * + */ + +#ifndef LV_DRAW_SW_PRIVATE_H +#define LV_DRAW_SW_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_draw_sw.h" +#include "../lv_draw_private.h" + +#if LV_USE_DRAW_SW + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_draw_sw_unit_t { + lv_draw_unit_t base_unit; + lv_draw_task_t * task_act; +#if LV_USE_OS + lv_thread_sync_t sync; + lv_thread_t thread; + volatile bool inited; + volatile bool exit_status; +#endif + uint32_t idx; +}; + +#if LV_DRAW_SW_SHADOW_CACHE_SIZE +typedef struct { + uint8_t cache[LV_DRAW_SW_SHADOW_CACHE_SIZE * LV_DRAW_SW_SHADOW_CACHE_SIZE]; + int32_t cache_size; + int32_t cache_r; +} lv_draw_sw_shadow_cache_t; +#endif + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_DRAW_SW */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_SW_PRIVATE_H*/ diff --git a/include/liblvgl/draw/swm341_dma2d/lv_gpu_swm341_dma2d.h b/include/liblvgl/draw/swm341_dma2d/lv_gpu_swm341_dma2d.h deleted file mode 100644 index baec7fea..00000000 --- a/include/liblvgl/draw/swm341_dma2d/lv_gpu_swm341_dma2d.h +++ /dev/null @@ -1,64 +0,0 @@ -/** - * @file lv_gpu_swm341_dma2d.h - * - */ - -#ifndef LV_GPU_SWM341_DMA2D_H -#define LV_GPU_SWM341_DMA2D_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/misc/lv_color.h" -#include "liblvgl/hal/lv_hal_disp.h" -#include "liblvgl/draw/sw/lv_draw_sw.h" - -#if LV_USE_GPU_SWM341_DMA2D - -/********************* - * DEFINES - *********************/ - -#define LV_SWM341_DMA2D_ARGB8888 0 -#define LV_SWM341_DMA2D_RGB888 1 -#define LV_SWM341_DMA2D_RGB565 2 - -/********************** - * TYPEDEFS - **********************/ -typedef lv_draw_sw_ctx_t lv_draw_swm341_dma2d_ctx_t; - -struct _lv_disp_drv_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Turn on the peripheral and set output color mode, this only needs to be done once - */ -void lv_draw_swm341_dma2d_init(void); - -void lv_draw_swm341_dma2d_ctx_init(struct _lv_disp_drv_t * drv, lv_draw_ctx_t * draw_ctx); - -void lv_draw_swm341_dma2d_ctx_deinit(struct _lv_disp_drv_t * drv, lv_draw_ctx_t * draw_ctx); - -void lv_draw_swm341_dma2d_blend(lv_draw_ctx_t * draw_ctx, const lv_draw_sw_blend_dsc_t * dsc); - -void lv_gpu_swm341_dma2d_wait_cb(lv_draw_ctx_t * draw_ctx); - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_GPU_SWM341_DMA2D*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_GPU_SWM341_DMA2D_H*/ diff --git a/include/liblvgl/draw/vg_lite/lv_draw_vg_lite.h b/include/liblvgl/draw/vg_lite/lv_draw_vg_lite.h new file mode 100644 index 00000000..63ff7f50 --- /dev/null +++ b/include/liblvgl/draw/vg_lite/lv_draw_vg_lite.h @@ -0,0 +1,90 @@ +/** + * @file lv_draw_vg_lite.h + * + */ + +#ifndef LV_DRAW_VG_LITE_H +#define LV_DRAW_VG_LITE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../lv_conf_internal.h" + +#if LV_USE_DRAW_VG_LITE + +#include "../lv_draw.h" +#include "../../draw/lv_draw_vector.h" +#include "../../draw/lv_draw_arc.h" +#include "../../draw/lv_draw_rect.h" +#include "../../draw/lv_draw_image.h" +#include "../../draw/lv_draw_label.h" +#include "../../draw/lv_draw_line.h" +#include "../../draw/lv_draw_triangle.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void lv_draw_buf_vg_lite_init_handlers(void); + +void lv_draw_vg_lite_init(void); + +void lv_draw_vg_lite_deinit(void); + +void lv_draw_vg_lite_arc(lv_draw_unit_t * draw_unit, const lv_draw_arc_dsc_t * dsc, + const lv_area_t * coords); + +void lv_draw_vg_lite_box_shadow(lv_draw_unit_t * draw_unit, const lv_draw_box_shadow_dsc_t * dsc, + const lv_area_t * coords); + +void lv_draw_vg_lite_border(lv_draw_unit_t * draw_unit, const lv_draw_border_dsc_t * dsc, + const lv_area_t * coords); + +void lv_draw_vg_lite_fill(lv_draw_unit_t * draw_unit, const lv_draw_fill_dsc_t * dsc, + const lv_area_t * coords); + +void lv_draw_vg_lite_img(lv_draw_unit_t * draw_unit, const lv_draw_image_dsc_t * dsc, + const lv_area_t * coords, bool no_cache); + +void lv_draw_vg_lite_label(lv_draw_unit_t * draw_unit, const lv_draw_label_dsc_t * dsc, + const lv_area_t * coords); + +void lv_draw_vg_lite_layer(lv_draw_unit_t * draw_unit, const lv_draw_image_dsc_t * draw_dsc, + const lv_area_t * coords); + +void lv_draw_vg_lite_line(lv_draw_unit_t * draw_unit, const lv_draw_line_dsc_t * dsc); + +void lv_draw_vg_lite_triangle(lv_draw_unit_t * draw_unit, const lv_draw_triangle_dsc_t * dsc); + +void lv_draw_vg_lite_mask_rect(lv_draw_unit_t * draw_unit, const lv_draw_mask_rect_dsc_t * dsc, + const lv_area_t * coords); + +#if LV_USE_VECTOR_GRAPHIC +void lv_draw_vg_lite_vector(lv_draw_unit_t * draw_unit, const lv_draw_vector_task_dsc_t * dsc); +#endif + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_DRAW_VG_LITE*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_VG_LITE_H*/ diff --git a/include/liblvgl/draw/vg_lite/lv_draw_vg_lite_type.h b/include/liblvgl/draw/vg_lite/lv_draw_vg_lite_type.h new file mode 100644 index 00000000..e06fc75d --- /dev/null +++ b/include/liblvgl/draw/vg_lite/lv_draw_vg_lite_type.h @@ -0,0 +1,73 @@ +/** + * @file lv_draw_vg_lite_type.h + * + */ + +#ifndef LV_DRAW_VG_LITE_TYPE_H +#define LV_DRAW_VG_LITE_TYPE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../lv_conf_internal.h" + +#if LV_USE_DRAW_VG_LITE + +#include "../lv_draw_private.h" +#include "../../misc/lv_array.h" + +#if LV_USE_VG_LITE_THORVG +#include "../../others/vg_lite_tvg/vg_lite.h" +#else +#include +#endif + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_vg_lite_pending_t; + +struct _lv_draw_vg_lite_unit_t { + lv_draw_unit_t base_unit; + lv_draw_task_t * task_act; + + struct _lv_vg_lite_pending_t * image_dsc_pending; + + lv_cache_t * grad_cache; + struct _lv_vg_lite_pending_t * grad_pending; + + lv_cache_t * stroke_cache; + + uint16_t flush_count; + uint16_t letter_count; + vg_lite_buffer_t target_buffer; + vg_lite_matrix_t global_matrix; + struct _lv_vg_lite_path_t * global_path; + bool path_in_use; +}; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_DRAW_VG_LITE*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRAW_VG_LITE_TYPE_H*/ diff --git a/include/liblvgl/extra/lv_extra.h b/include/liblvgl/draw/vg_lite/lv_vg_lite_decoder.h similarity index 59% rename from include/liblvgl/extra/lv_extra.h rename to include/liblvgl/draw/vg_lite/lv_vg_lite_decoder.h index c0306a98..9bab540c 100644 --- a/include/liblvgl/extra/lv_extra.h +++ b/include/liblvgl/draw/vg_lite/lv_vg_lite_decoder.h @@ -1,10 +1,10 @@ /** - * @file lv_extra.h + * @file lv_vg_lite_decoder.h * */ -#ifndef LV_EXTRA_H -#define LV_EXTRA_H +#ifndef LV_VG_LITE_DECODER_H +#define LV_VG_LITE_DECODER_H #ifdef __cplusplus extern "C" { @@ -14,11 +14,9 @@ extern "C" { * INCLUDES *********************/ -#include "layouts/lv_layouts.h" -#include "libs/lv_libs.h" -#include "others/lv_others.h" -#include "themes/lv_themes.h" -#include "widgets/lv_widgets.h" +#include "../lv_image_decoder.h" + +#if LV_USE_DRAW_VG_LITE /********************* * DEFINES @@ -32,17 +30,18 @@ extern "C" { * GLOBAL PROTOTYPES **********************/ -/** - * Initialize the extra components - */ -void lv_extra_init(void); +void lv_vg_lite_decoder_init(void); + +void lv_vg_lite_decoder_deinit(void); /********************** * MACROS **********************/ +#endif /*LV_USE_DRAW_VG_LITE*/ + #ifdef __cplusplus } /*extern "C"*/ #endif -#endif /*LV_EXTRA_H*/ +#endif /*LV_VG_LITE_DECODER_H*/ diff --git a/include/liblvgl/draw/vg_lite/lv_vg_lite_grad.h b/include/liblvgl/draw/vg_lite/lv_vg_lite_grad.h new file mode 100644 index 00000000..d9bde91e --- /dev/null +++ b/include/liblvgl/draw/vg_lite/lv_vg_lite_grad.h @@ -0,0 +1,69 @@ +/** + * @file lv_vg_lite_grad.h + * + */ + +#ifndef LV_VG_LITE_GRAD_H +#define LV_VG_LITE_GRAD_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../lvgl.h" + +#if LV_USE_DRAW_VG_LITE && LV_USE_VECTOR_GRAPHIC + +#include "lv_vg_lite_utils.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void lv_vg_lite_grad_init(struct _lv_draw_vg_lite_unit_t * u, uint32_t cache_cnt); + +void lv_vg_lite_grad_deinit(struct _lv_draw_vg_lite_unit_t * u); + +bool lv_vg_lite_draw_grad( + struct _lv_draw_vg_lite_unit_t * u, + vg_lite_buffer_t * buffer, + vg_lite_path_t * path, + const lv_vector_gradient_t * grad, + const vg_lite_matrix_t * grad_matrix, + const vg_lite_matrix_t * matrix, + vg_lite_fill_t fill, + vg_lite_blend_t blend); + +bool lv_vg_lite_draw_grad_helper( + struct _lv_draw_vg_lite_unit_t * u, + vg_lite_buffer_t * buffer, + vg_lite_path_t * path, + const lv_area_t * area, + const lv_grad_dsc_t * grad_dsc, + const vg_lite_matrix_t * matrix, + vg_lite_fill_t fill, + vg_lite_blend_t blend); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_DRAW_VG_LITE && LV_USE_VECTOR_GRAPHIC*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_VG_LITE_GRAD_H*/ diff --git a/include/liblvgl/draw/vg_lite/lv_vg_lite_math.h b/include/liblvgl/draw/vg_lite/lv_vg_lite_math.h new file mode 100644 index 00000000..e2afc793 --- /dev/null +++ b/include/liblvgl/draw/vg_lite/lv_vg_lite_math.h @@ -0,0 +1,76 @@ +/** + * @file lv_vg_lite_math.h + * + */ + +#ifndef LV_VG_LITE_MATH_H +#define LV_VG_LITE_MATH_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../lv_conf_internal.h" + +#if LV_USE_DRAW_VG_LITE + +#include +#include +#include + +/********************* + * DEFINES + *********************/ + +#define MATH_PI 3.14159265358979323846f +#define MATH_HALF_PI 1.57079632679489661923f +#define MATH_TWO_PI 6.28318530717958647692f +#define DEG_TO_RAD 0.017453292519943295769236907684886f +#define RAD_TO_DEG 57.295779513082320876798154814105f + +#define MATH_TANF(x) tanf(x) +#define MATH_SINF(x) sinf(x) +#define MATH_COSF(x) cosf(x) +#define MATH_ASINF(x) asinf(x) +#define MATH_ACOSF(x) acosf(x) +#define MATH_FABSF(x) fabsf(x) +#define MATH_SQRTF(x) sqrtf(x) + +#define MATH_RADIANS(deg) ((deg) * DEG_TO_RAD) +#define MATH_DEGREES(rad) ((rad) * RAD_TO_DEG) + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +static inline bool math_zero(float a) +{ + return (MATH_FABSF(a) < FLT_EPSILON); +} + +static inline bool math_equal(float a, float b) +{ + return math_zero(a - b); +} + +float math_fast_inv_sqrtf(float number); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_DRAW_VG_LITE*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_VG_LITE_MATH_H*/ diff --git a/include/liblvgl/draw/vg_lite/lv_vg_lite_path.h b/include/liblvgl/draw/vg_lite/lv_vg_lite_path.h new file mode 100644 index 00000000..5781d513 --- /dev/null +++ b/include/liblvgl/draw/vg_lite/lv_vg_lite_path.h @@ -0,0 +1,128 @@ +/** + * @file lv_vg_lite_path.h + * + */ + +#ifndef LV_VG_LITE_PATH_H +#define LV_VG_LITE_PATH_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_vg_lite_utils.h" + +#if LV_USE_DRAW_VG_LITE + +/********************* + * DEFINES + *********************/ + +typedef struct _lv_vg_lite_path_t lv_vg_lite_path_t; +typedef struct _lv_draw_vg_lite_unit_t lv_draw_vg_lite_unit_t; + +typedef void (*lv_vg_lite_path_iter_cb_t)(void * user_data, uint8_t op_code, const float * data, uint32_t len); + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void lv_vg_lite_path_init(lv_draw_vg_lite_unit_t * unit); + +void lv_vg_lite_path_deinit(lv_draw_vg_lite_unit_t * unit); + +lv_vg_lite_path_t * lv_vg_lite_path_create(vg_lite_format_t data_format); + +void lv_vg_lite_path_destroy(lv_vg_lite_path_t * path); + +lv_vg_lite_path_t * lv_vg_lite_path_get(lv_draw_vg_lite_unit_t * unit, vg_lite_format_t data_format); + +void lv_vg_lite_path_drop(lv_draw_vg_lite_unit_t * unit, lv_vg_lite_path_t * path); + +void lv_vg_lite_path_reset(lv_vg_lite_path_t * path, vg_lite_format_t data_format); + +void lv_vg_lite_path_set_bonding_box_area(lv_vg_lite_path_t * path, const lv_area_t * area); + +void lv_vg_lite_path_set_bonding_box(lv_vg_lite_path_t * path, + float min_x, float min_y, + float max_x, float max_y); + +void lv_vg_lite_path_get_bonding_box(lv_vg_lite_path_t * path, + float * min_x, float * min_y, + float * max_x, float * max_y); + +bool lv_vg_lite_path_update_bonding_box(lv_vg_lite_path_t * path); + +void lv_vg_lite_path_set_transform(lv_vg_lite_path_t * path, const vg_lite_matrix_t * matrix); + +void lv_vg_lite_path_set_quality(lv_vg_lite_path_t * path, vg_lite_quality_t quality); + +vg_lite_path_t * lv_vg_lite_path_get_path(lv_vg_lite_path_t * path); + +void lv_vg_lite_path_move_to(lv_vg_lite_path_t * path, + float x, float y); + +void lv_vg_lite_path_line_to(lv_vg_lite_path_t * path, + float x, float y); + +void lv_vg_lite_path_quad_to(lv_vg_lite_path_t * path, + float cx, float cy, + float x, float y); + +void lv_vg_lite_path_cubic_to(lv_vg_lite_path_t * path, + float cx1, float cy1, + float cx2, float cy2, + float x, float y); + +void lv_vg_lite_path_close(lv_vg_lite_path_t * path); + +void lv_vg_lite_path_end(lv_vg_lite_path_t * path); + +void lv_vg_lite_path_append_rect(lv_vg_lite_path_t * path, + float x, float y, + float w, float h, + float r); + +void lv_vg_lite_path_append_circle(lv_vg_lite_path_t * path, + float cx, float cy, + float rx, float ry); + +void lv_vg_lite_path_append_arc_right_angle(lv_vg_lite_path_t * path, + float start_x, float start_y, + float center_x, float center_y, + float end_x, float end_y); + +void lv_vg_lite_path_append_arc(lv_vg_lite_path_t * path, + float cx, float cy, + float radius, + float start_angle, + float sweep, + bool pie); + +void lv_vg_lite_path_append_path(lv_vg_lite_path_t * dest, const lv_vg_lite_path_t * src); + +uint8_t lv_vg_lite_vlc_op_arg_len(uint8_t vlc_op); + +uint8_t lv_vg_lite_path_format_len(vg_lite_format_t format); + +void lv_vg_lite_path_for_each_data(const vg_lite_path_t * path, lv_vg_lite_path_iter_cb_t cb, void * user_data); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_DRAW_VG_LITE*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_VG_LITE_PATH_H*/ diff --git a/include/liblvgl/draw/vg_lite/lv_vg_lite_pending.h b/include/liblvgl/draw/vg_lite/lv_vg_lite_pending.h new file mode 100644 index 00000000..8def9dcf --- /dev/null +++ b/include/liblvgl/draw/vg_lite/lv_vg_lite_pending.h @@ -0,0 +1,83 @@ +/** + * @file lv_vg_lite_pending.h + * + */ + +#ifndef LV_VG_LITE_PENDING_H +#define LV_VG_LITE_PENDING_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../lvgl.h" + +#if LV_USE_DRAW_VG_LITE + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef struct _lv_vg_lite_pending_t lv_vg_lite_pending_t; + +typedef void (*lv_vg_lite_pending_free_cb_t)(void * obj, void * user_data); + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create a pending list + * @param obj_size the size of the objects in the list + * @param capacity_default the default capacity of the list + * @return a pointer to the pending list + */ +lv_vg_lite_pending_t * lv_vg_lite_pending_create(size_t obj_size, uint32_t capacity_default); + +/** + * Destroy a pending list + * @param pending pointer to the pending list + */ +void lv_vg_lite_pending_destroy(lv_vg_lite_pending_t * pending); + +/** + * Set a free callback for the pending list + * @param pending pointer to the pending list + * @param free_cb the free callback + * @param user_data user data to pass to the free callback + */ +void lv_vg_lite_pending_set_free_cb(lv_vg_lite_pending_t * pending, lv_vg_lite_pending_free_cb_t free_cb, + void * user_data); + +/** + * Add an object to the pending list + * @param pending pointer to the pending list + * @param obj pointer to the object to add + */ +void lv_vg_lite_pending_add(lv_vg_lite_pending_t * pending, void * obj); + +/** + * Remove all objects from the pending list + * @param pending pointer to the pending list + */ +void lv_vg_lite_pending_remove_all(lv_vg_lite_pending_t * pending); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_DRAW_VG_LITE*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_VG_LITE_PENDING_H*/ diff --git a/include/liblvgl/draw/vg_lite/lv_vg_lite_stroke.h b/include/liblvgl/draw/vg_lite/lv_vg_lite_stroke.h new file mode 100644 index 00000000..81c7e510 --- /dev/null +++ b/include/liblvgl/draw/vg_lite/lv_vg_lite_stroke.h @@ -0,0 +1,84 @@ +/** + * @file lv_vg_lite_stroke.h + * + */ + +#ifndef LV_VG_LITE_STROKE_H +#define LV_VG_LITE_STROKE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_vg_lite_utils.h" + +#if LV_USE_DRAW_VG_LITE && LV_USE_VECTOR_GRAPHIC + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_vg_lite_path_t; + +struct _lv_draw_vg_lite_unit_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Initialize the stroke module + * @param unit pointer to the unit + */ +void lv_vg_lite_stroke_init(struct _lv_draw_vg_lite_unit_t * unit, uint32_t cache_cnt); + +/** + * Deinitialize the stroke module + * @param unit pointer to the unit + */ +void lv_vg_lite_stroke_deinit(struct _lv_draw_vg_lite_unit_t * unit); + +/** + * Get the stroke cache entry + * @param unit pointer to the unit + * @param path pointer to the path + * @param dsc pointer to the stroke descriptor + * @return pointer to the stroke cache entry + */ +lv_cache_entry_t * lv_vg_lite_stroke_get(struct _lv_draw_vg_lite_unit_t * unit, + struct _lv_vg_lite_path_t * path, + const lv_vector_stroke_dsc_t * dsc); + +/** + * Get the path of a stroke + * @param cache_entry pointer to the stroke cache entry + * @return pointer to the path + */ +struct _lv_vg_lite_path_t * lv_vg_lite_stroke_get_path(lv_cache_entry_t * cache_entry); + +/** + * Drop the stroke cache entry + * @param unit pointer to the unit + * @param stroke pointer to the stroke + */ +void lv_vg_lite_stroke_drop(struct _lv_draw_vg_lite_unit_t * unit, lv_cache_entry_t * cache_entry); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_DRAW_VG_LITE && LV_USE_VECTOR_GRAPHIC*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_VG_LITE_STROKE_H*/ diff --git a/include/liblvgl/draw/vg_lite/lv_vg_lite_utils.h b/include/liblvgl/draw/vg_lite/lv_vg_lite_utils.h new file mode 100644 index 00000000..7d397632 --- /dev/null +++ b/include/liblvgl/draw/vg_lite/lv_vg_lite_utils.h @@ -0,0 +1,188 @@ +/** + * @file lv_vg_lite_utils.h + * + */ + +#ifndef LV_VG_LITE_UTILS_H +#define LV_VG_LITE_UTILS_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../lvgl.h" + +#if LV_USE_DRAW_VG_LITE + +#include "../../misc/lv_profiler.h" + +#include +#if LV_USE_VG_LITE_THORVG +#include "../../others/vg_lite_tvg/vg_lite.h" +#else +#include +#endif + +/********************* + * DEFINES + *********************/ + +#define LV_VG_LITE_IS_ERROR(err) (err > 0) + +#define VLC_GET_OP_CODE(ptr) (*((uint8_t*)ptr)) + +#if LV_VG_LITE_USE_ASSERT +#define LV_VG_LITE_ASSERT(expr) LV_ASSERT(expr) +#else +#define LV_VG_LITE_ASSERT(expr) +#endif + +#define LV_VG_LITE_CHECK_ERROR(expr) \ + do { \ + vg_lite_error_t error = expr; \ + if (LV_VG_LITE_IS_ERROR(error)) { \ + LV_LOG_ERROR("Execute '" #expr "' error(%d): %s", \ + (int)error, lv_vg_lite_error_string(error)); \ + LV_VG_LITE_ASSERT(false); \ + } \ + } while (0) + +#define LV_VG_LITE_ASSERT_PATH(path) LV_VG_LITE_ASSERT(lv_vg_lite_path_check(path)) +#define LV_VG_LITE_ASSERT_SRC_BUFFER(buffer) LV_VG_LITE_ASSERT(lv_vg_lite_buffer_check(buffer, true)) +#define LV_VG_LITE_ASSERT_DEST_BUFFER(buffer) LV_VG_LITE_ASSERT(lv_vg_lite_buffer_check(buffer, false)) +#define LV_VG_LITE_ASSERT_MATRIX(matrix) LV_VG_LITE_ASSERT(lv_vg_lite_matrix_check(matrix)) + +#define LV_VG_LITE_ALIGN(number, align_bytes) \ + (((number) + ((align_bytes)-1)) & ~((align_bytes)-1)) + +#define LV_VG_LITE_IS_ALIGNED(num, align) (((uintptr_t)(num) & ((align)-1)) == 0) + +#define LV_VG_LITE_IS_INDEX_FMT(fmt) \ + ((fmt) == VG_LITE_INDEX_1 \ + || (fmt) == VG_LITE_INDEX_2 \ + || (fmt) == VG_LITE_INDEX_4 \ + || (fmt) == VG_LITE_INDEX_8) + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_draw_vg_lite_unit_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/* Print info */ + +void lv_vg_lite_dump_info(void); + +const char * lv_vg_lite_error_string(vg_lite_error_t error); + +const char * lv_vg_lite_feature_string(vg_lite_feature_t feature); + +const char * lv_vg_lite_buffer_format_string(vg_lite_buffer_format_t format); + +const char * lv_vg_lite_vlc_op_string(uint8_t vlc_op); + +void lv_vg_lite_path_dump_info(const vg_lite_path_t * path); + +void lv_vg_lite_stroke_dump_info(const vg_lite_stroke_t * stroke); + +void lv_vg_lite_buffer_dump_info(const vg_lite_buffer_t * buffer); + +void lv_vg_lite_matrix_dump_info(const vg_lite_matrix_t * matrix); + +bool lv_vg_lite_is_dest_cf_supported(lv_color_format_t cf); + +bool lv_vg_lite_is_src_cf_supported(lv_color_format_t cf); + +/* Converter */ + +vg_lite_buffer_format_t lv_vg_lite_vg_fmt(lv_color_format_t cf); + +void lv_vg_lite_buffer_format_bytes( + vg_lite_buffer_format_t format, + uint32_t * mul, + uint32_t * div, + uint32_t * bytes_align); + +uint32_t lv_vg_lite_width_to_stride(uint32_t w, vg_lite_buffer_format_t color_format); + +uint32_t lv_vg_lite_width_align(uint32_t w); + +void lv_vg_lite_buffer_init( + vg_lite_buffer_t * buffer, + const void * ptr, + int32_t width, + int32_t height, + uint32_t stride, + vg_lite_buffer_format_t format, + bool tiled); + +void lv_vg_lite_buffer_from_draw_buf(vg_lite_buffer_t * buffer, const lv_draw_buf_t * draw_buf); + +void lv_vg_lite_image_matrix(vg_lite_matrix_t * matrix, int32_t x, int32_t y, const lv_draw_image_dsc_t * dsc); + +void lv_vg_lite_image_dec_init(vg_lite_matrix_t * matrix, int32_t x, int32_t y, const lv_draw_image_dsc_t * dsc); + +bool lv_vg_lite_buffer_open_image(vg_lite_buffer_t * buffer, lv_image_decoder_dsc_t * decoder_dsc, const void * src, + bool no_cache, bool premultiply); + +void lv_vg_lite_image_dsc_init(struct _lv_draw_vg_lite_unit_t * unit); + +void lv_vg_lite_image_dsc_deinit(struct _lv_draw_vg_lite_unit_t * unit); + +vg_lite_blend_t lv_vg_lite_blend_mode(lv_blend_mode_t blend_mode, bool has_pre_mul); + +uint32_t lv_vg_lite_get_palette_size(vg_lite_buffer_format_t format); + +vg_lite_color_t lv_vg_lite_color(lv_color_t color, lv_opa_t opa, bool pre_mul); + +void lv_vg_lite_rect(vg_lite_rectangle_t * rect, const lv_area_t * area); + +void lv_vg_lite_matrix(vg_lite_matrix_t * dest, const lv_matrix_t * src); + +/* Param checker */ + +bool lv_vg_lite_buffer_check(const vg_lite_buffer_t * buffer, bool is_src); + +bool lv_vg_lite_path_check(const vg_lite_path_t * path); + +bool lv_vg_lite_matrix_check(const vg_lite_matrix_t * matrix); + +/* Wrapper */ + +bool lv_vg_lite_support_blend_normal(void); + +bool lv_vg_lite_16px_align(void); + +void lv_vg_lite_matrix_multiply(vg_lite_matrix_t * matrix, const vg_lite_matrix_t * mult); + +bool lv_vg_lite_matrix_inverse(vg_lite_matrix_t * result, const vg_lite_matrix_t * matrix); + +lv_point_precise_t lv_vg_lite_matrix_transform_point(const vg_lite_matrix_t * matrix, const lv_point_precise_t * point); + +void lv_vg_lite_set_scissor_area(const lv_area_t * area); + +void lv_vg_lite_disable_scissor(void); + +void lv_vg_lite_flush(struct _lv_draw_vg_lite_unit_t * u); + +void lv_vg_lite_finish(struct _lv_draw_vg_lite_unit_t * u); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_DRAW_VG_LITE*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*VG_LITE_UTILS_H*/ diff --git a/include/liblvgl/drivers/README.md b/include/liblvgl/drivers/README.md new file mode 100644 index 00000000..f755a3ab --- /dev/null +++ b/include/liblvgl/drivers/README.md @@ -0,0 +1 @@ +High level drivers for display controllers, frame buffers, etc \ No newline at end of file diff --git a/include/liblvgl/drivers/display/drm/lv_linux_drm.h b/include/liblvgl/drivers/display/drm/lv_linux_drm.h new file mode 100644 index 00000000..ee32466c --- /dev/null +++ b/include/liblvgl/drivers/display/drm/lv_linux_drm.h @@ -0,0 +1,46 @@ +/** + * @file lv_linux_drm.h + * + */ + +#ifndef LV_LINUX_DRM_H +#define LV_LINUX_DRM_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../../display/lv_display.h" + +#if LV_USE_LINUX_DRM + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ +lv_display_t * lv_linux_drm_create(void); + +void lv_linux_drm_set_file(lv_display_t * disp, const char * file, int64_t connector_id); + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_LINUX_DRM */ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LV_LINUX_DRM_H */ diff --git a/include/liblvgl/drivers/display/fb/lv_linux_fbdev.h b/include/liblvgl/drivers/display/fb/lv_linux_fbdev.h new file mode 100644 index 00000000..2c41aec1 --- /dev/null +++ b/include/liblvgl/drivers/display/fb/lv_linux_fbdev.h @@ -0,0 +1,52 @@ +/** + * @file lv_linux_fbdev.h + * + */ + +#ifndef LV_LINUX_FBDEV_H +#define LV_LINUX_FBDEV_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../../display/lv_display.h" + +#if LV_USE_LINUX_FBDEV + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ +lv_display_t * lv_linux_fbdev_create(void); + +void lv_linux_fbdev_set_file(lv_display_t * disp, const char * file); + +/** + * Force the display to be refreshed on every change. + * Expected to be used with LV_DISPLAY_RENDER_MODE_DIRECT or LV_DISPLAY_RENDER_MODE_FULL. + */ +void lv_linux_fbdev_set_force_refresh(lv_display_t * disp, bool enabled); + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_LINUX_FBDEV */ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LV_LINUX_FBDEV_H */ diff --git a/include/liblvgl/drivers/display/ili9341/lv_ili9341.h b/include/liblvgl/drivers/display/ili9341/lv_ili9341.h new file mode 100644 index 00000000..90fe5daf --- /dev/null +++ b/include/liblvgl/drivers/display/ili9341/lv_ili9341.h @@ -0,0 +1,94 @@ +/** + * @file lv_ili9341.h + * + * This driver is just a wrapper around the generic MIPI compatible LCD controller driver + * + */ + +#ifndef LV_ILI9341_H +#define LV_ILI9341_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../lcd/lv_lcd_generic_mipi.h" + +#if LV_USE_ILI9341 + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef lv_lcd_send_cmd_cb_t lv_ili9341_send_cmd_cb_t; +typedef lv_lcd_send_color_cb_t lv_ili9341_send_color_cb_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create an LCD display with ILI9341 driver + * @param hor_res horizontal resolution + * @param ver_res vertical resolution + * @param flags default configuration settings (mirror, RGB ordering, etc.) + * @param send_cmd platform-dependent function to send a command to the LCD controller (usually uses polling transfer) + * @param send_color platform-dependent function to send pixel data to the LCD controller (usually uses DMA transfer: must implement a 'ready' callback) + * @return pointer to the created display + */ +lv_display_t * lv_ili9341_create(uint32_t hor_res, uint32_t ver_res, lv_lcd_flag_t flags, + lv_ili9341_send_cmd_cb_t send_cmd_cb, lv_ili9341_send_color_cb_t send_color_cb); + +/** + * Set gap, i.e., the offset of the (0,0) pixel in the VRAM + * @param disp display object + * @param x x offset + * @param y y offset + */ +void lv_ili9341_set_gap(lv_display_t * disp, uint16_t x, uint16_t y); + +/** + * Set color inversion + * @param disp display object + * @param invert false: normal, true: invert + */ +void lv_ili9341_set_invert(lv_display_t * disp, bool invert); + +/** + * Set gamma curve + * @param disp display object + * @param gamma gamma curve + */ +void lv_ili9341_set_gamma_curve(lv_display_t * disp, uint8_t gamma); + +/** + * Send list of commands. + * @param disp display object + * @param cmd_list controller and panel-specific commands + */ +void lv_ili9341_send_cmd_list(lv_display_t * disp, const uint8_t * cmd_list); + +/********************** + * OTHERS + **********************/ + +/********************** + * MACROS + **********************/ + + +#endif /*LV_USE_ILI9341*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_ILI9341_H*/ diff --git a/include/liblvgl/drivers/display/lcd/lv_lcd_generic_mipi.h b/include/liblvgl/drivers/display/lcd/lv_lcd_generic_mipi.h new file mode 100644 index 00000000..95fbf679 --- /dev/null +++ b/include/liblvgl/drivers/display/lcd/lv_lcd_generic_mipi.h @@ -0,0 +1,242 @@ +/** + * @file lv_lcd_generic_mipi.h + * + * Generic driver for controllers adhering to the MIPI DBI/DCS specification + * + * Works with: + * + * ST7735 + * ST7789 + * ST7796 + * ILI9341 + * ILI9488 (NOTE: in SPI mode ILI9488 only supports RGB666 mode, which is currently not supported) + * + * any probably many more + * + */ + +#ifndef LV_LCD_GENERIC_MIPI_H +#define LV_LCD_GENERIC_MIPI_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../../display/lv_display.h" + +#if LV_USE_GENERIC_MIPI + +/********************* + * DEFINES + *********************/ + +/* MIPI DCS (Display Command Set) v1.02.00 User Command Set */ +#define LV_LCD_CMD_NOP 0x00 /* No Operation */ +#define LV_LCD_CMD_SOFT_RESET 0x01 /* Software Reset */ +#define LV_LCD_CMD_GET_POWER_MODE 0x0A /* Get the current power mode */ +#define LV_LCD_CMD_GET_ADDRESS_MODE 0x0B /* Get the data order for transfers from the Host to the display module and from the frame memory to the display device */ +#define LV_LCD_CMD_GET_PIXEL_FORMAT 0x0C /* Get the current pixel format */ +#define LV_LCD_CMD_GET_DISPLAY_MODE 0x0D /* Get the current display mode from the peripheral */ +#define LV_LCD_CMD_GET_SIGNAL_MODE 0x0E /* Get display module signaling mode */ +#define LV_LCD_CMD_GET_DIAGNOSTIC_RESULT 0x0F /* Get Peripheral Self-Diagnostic Result */ +#define LV_LCD_CMD_ENTER_SLEEP_MODE 0x10 /* Power for the display panel is off */ +#define LV_LCD_CMD_EXIT_SLEEP_MODE 0x11 /* Power for the display panel is on */ +#define LV_LCD_CMD_ENTER_PARTIAL_MODE 0x12 /* Part of the display area is used for image display */ +#define LV_LCD_CMD_ENTER_NORMAL_MODE 0x13 /* The whole display area is used for image display */ +#define LV_LCD_CMD_EXIT_INVERT_MODE 0x20 /* Displayed image colors are not inverted */ +#define LV_LCD_CMD_ENTER_INVERT_MODE 0x21 /* Displayed image colors are inverted */ +#define LV_LCD_CMD_SET_GAMMA_CURVE 0x26 /* Selects the gamma curve used by the display device */ +#define LV_LCD_CMD_SET_DISPLAY_OFF 0x28 /* Blanks the display device */ +#define LV_LCD_CMD_SET_DISPLAY_ON 0x29 /* Show the image on the display device */ +#define LV_LCD_CMD_SET_COLUMN_ADDRESS 0x2A /* Set the column extent */ +#define LV_LCD_CMD_SET_PAGE_ADDRESS 0x2B /* Set the page extent */ +#define LV_LCD_CMD_WRITE_MEMORY_START 0x2C /* Transfer image data from the Host Processor to the peripheral starting at the location provided by set_column_address and set_page_address */ +#define LV_LCD_CMD_READ_MEMORY_START 0x2E /* Transfer image data from the peripheral to the Host Processor interface starting at the location provided by set_column_address and set_page_address */ +#define LV_LCD_CMD_SET_PARTIAL_ROWS 0x30 /* Defines the number of rows in the partial display area on the display device */ +#define LV_LCD_CMD_SET_PARTIAL_COLUMNS 0x31 /* Defines the number of columns in the partial display area on the display device */ +#define LV_LCD_CMD_SET_SCROLL_AREA 0x33 /* Defines the vertical scrolling and fixed area on display device */ +#define LV_LCD_CMD_SET_TEAR_OFF 0x34 /* Synchronization information is not sent from the display module to the host processor */ +#define LV_LCD_CMD_SET_TEAR_ON 0x35 /* Synchronization information is sent from the display module to the host processor at the start of VFP */ +#define LV_LCD_CMD_SET_ADDRESS_MODE 0x36 /* Set the data order for transfers from the Host to the display module and from the frame memory to the display device */ +#define LV_LCD_CMD_SET_SCROLL_START 0x37 /* Defines the vertical scrolling starting point */ +#define LV_LCD_CMD_EXIT_IDLE_MODE 0x38 /* Full color depth is used on the display panel */ +#define LV_LCD_CMD_ENTER_IDLE_MODE 0x39 /* Reduced color depth is used on the display panel */ +#define LV_LCD_CMD_SET_PIXEL_FORMAT 0x3A /* Defines how many bits per pixel are used in the interface */ +#define LV_LCD_CMD_WRITE_MEMORY_CONTINUE 0x3C /* Transfer image information from the Host Processor interface to the peripheral from the last written location */ +#define LV_LCD_CMD_READ_MEMORY_CONTINUE 0x3E /* Read image data from the peripheral continuing after the last read_memory_continue or read_memory_start */ +#define LV_LCD_CMD_SET_TEAR_SCANLINE 0x44 /* Synchronization information is sent from the display module to the host processor when the display device refresh reaches the provided scanline */ +#define LV_LCD_CMD_GET_SCANLINE 0x45 /* Get the current scanline */ +#define LV_LCD_CMD_READ_DDB_CONTINUE 0xA8 /* Continue reading the DDB from the last read location */ +#define LV_LCD_CMD_READ_DDB_START 0xA1 /* Read the DDB from the provided location */ + +/* address mode flag masks */ +#define LV_LCD_MASK_FLIP_VERTICAL (1 << 0) /* This bit flips the image shown on the display device top to bottom. No change is made to the frame memory */ +#define LV_LCD_MASK_FLIP_HORIZONTAL (1 << 1) /* This bit flips the image shown on the display device left to right. No change is made to the frame memory */ +#define LV_LCD_MASK_DATA_LATCH_DATA_ORDER (1 << 2) /* Display Data Latch Order */ +#define LV_LCD_MASK_RGB_ORDER (1 << 3) /* RGB/BGR Order */ +#define LV_LCD_MASK_LINE_ADDRESS_ORDER (1 << 4) /* Line Address Order */ +#define LV_LCD_MASK_PAGE_COLUMN_ORDER (1 << 5) /* Page/Column Order */ +#define LV_LCD_MASK_COLUMN_ADDRESS_ORDER (1 << 6) /* Column Address Order */ +#define LV_LCD_MASK_PAGE_ADDRESS_ORDER (1 << 7) /* Page Address Order */ + +#define LV_LCD_BIT_FLIP_VERTICAL__NOT_FLIPPED 0 +#define LV_LCD_BIT_FLIP_VERTICAL__FLIPPED LV_LCD_MASK_FLIP_VERTICAL /* This bit flips the image shown on the display device top to bottom. No change is made to the frame memory */ +#define LV_LCD_BIT_FLIP_HORIZONTAL__NOT_FLIPPED 0 +#define LV_LCD_BIT_FLIP_HORIZONTAL__FLIPPED LV_LCD_MASK_FLIP_HORIZONTAL /* This bit flips the image shown on the display device left to right. No change is made to the frame memory */ +#define LV_LCD_BIT_DATA_LATCH_DATA_ORDER__LTOR 0 /* Display Data Latch Order: LCD Refresh Left to Right */ +#define LV_LCD_BIT_DATA_LATCH_DATA_ORDER__RTOL LV_LCD_MASK_DATA_LATCH_DATA_ORDER /* Display Data Latch Order: LCD Refresh Right to Left */ +#define LV_LCD_BIT_RGB_ORDER__RGB 0 /* RGB/BGR Order: RGB */ +#define LV_LCD_BIT_RGB_ORDER__BGR LV_LCD_MASK_RGB_ORDER /* RGB/BGR Order: BGR */ +#define LV_LCD_BIT_LINE_ADDRESS_ORDER__TTOB 0 /* Line Address Order: LCD Refresh Top to Bottom */ +#define LV_LCD_BIT_LINE_ADDRESS_ORDER__BTOT LV_LCD_MASK_LINE_ADDRESS_ORDER /* Line Address Order: LCD Refresh Bottom to Top */ +#define LV_LCD_BIT_PAGE_COLUMN_ORDER__NORMAL 0 /* Page/Column Order: Normal Mode */ +#define LV_LCD_BIT_PAGE_COLUMN_ORDER__REVERSE LV_LCD_MASK_PAGE_COLUMN_ORDER /* Page/Column Order: Reverse Mode */ +#define LV_LCD_BIT_COLUMN_ADDRESS_ORDER__LTOR 0 /* Column Address Order: Left to Right */ +#define LV_LCD_BIT_COLUMN_ADDRESS_ORDER__RTOL LV_LCD_MASK_COLUMN_ADDRESS_ORDER /* Column Address Order: Right to Left */ +#define LV_LCD_BIT_PAGE_ADDRESS_ORDER__TTOB 0 /* Page Address Order: Top to Bottom */ +#define LV_LCD_BIT_PAGE_ADDRESS_ORDER__BTOT LV_LCD_MASK_PAGE_ADDRESS_ORDER /* Page Address Order: Bottom to Top */ + +/* predefined gamma curves */ +#define LV_LCD_GAMMA_2_2 0x01 /* 2.2 */ +#define LV_LCD_GAMMA_1_8 0x02 /* 1.8 */ +#define LV_LCD_GAMMA_2_5 0x04 /* 2.5 */ +#define LV_LCD_GAMMA_1_0 0x08 /* 1.0 */ + +/* common pixel formats */ +#define LV_LCD_PIXEL_FORMAT_RGB565 0x55 /* bus: 16 bits, pixel: 16 bits */ +#define LV_LCD_PIXEL_FORMAT_RGB666 0x66 /* bus: 18 bits, pixel: 18 bits */ + +/* flags for lv_lcd_xxx_create() */ +#define LV_LCD_FLAG_NONE 0x00000000UL +#define LV_LCD_FLAG_MIRROR_X 0x00000001UL +#define LV_LCD_FLAG_MIRROR_Y 0x00000002UL +#define LV_LCD_FLAG_BGR 0x00000008UL +#define LV_LCD_FLAG_RGB666 0x00000010UL + +/* command list */ +#define LV_LCD_CMD_DELAY_MS 0xff +#define LV_LCD_CMD_EOF 0xff + +/********************** + * TYPEDEFS + **********************/ + +/** + * Configuration flags for lv_lcd_xxx_create() + * + */ +typedef uint32_t lv_lcd_flag_t; + +/** + * Prototype of a platform-dependent callback to transfer commands and data to the LCD controller. + * @param disp display object + * @param cmd command buffer (can handle 16 bit commands as well) + * @param cmd_size number of bytes of the command + * @param param parameter buffer + * @param param_size number of bytes of the parameters + */ +typedef void (*lv_lcd_send_cmd_cb_t)(lv_display_t * disp, const uint8_t * cmd, size_t cmd_size, const uint8_t * param, + size_t param_size); + +/** + * Prototype of a platform-dependent callback to transfer pixel data to the LCD controller. + * @param disp display object + * @param cmd command buffer (can handle 16 bit commands as well) + * @param cmd_size number of bytes of the command + * @param param parameter buffer + * @param param_size number of bytes of the parameters + */ +typedef void (*lv_lcd_send_color_cb_t)(lv_display_t * disp, const uint8_t * cmd, size_t cmd_size, uint8_t * param, + size_t param_size); + +/** + * Generic MIPI compatible LCD driver + */ +typedef struct { + lv_display_t * disp; /* the associated LVGL display object */ + lv_lcd_send_cmd_cb_t send_cmd; /* platform-specific implementation to send a command to the LCD controller */ + lv_lcd_send_color_cb_t send_color; /* platform-specific implementation to send pixel data to the LCD controller */ + uint16_t x_gap; /* x offset of the (0,0) pixel in VRAM */ + uint16_t y_gap; /* y offset of the (0,0) pixel in VRAM */ + uint8_t madctl_reg; /* current value of MADCTL register */ + uint8_t colmod_reg; /* current value of COLMOD register */ + bool mirror_x; + bool mirror_y; + bool swap_xy; +} lv_lcd_generic_mipi_driver_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create a MIPI DCS compatible LCD display + * @param hor_res horizontal resolution + * @param ver_res vertical resolution + * @param flags default configuration settings (mirror, RGB ordering, etc.) + * @param send_cmd platform-dependent function to send a command to the LCD controller (usually uses polling transfer) + * @param send_color platform-dependent function to send pixel data to the LCD controller (usually uses DMA transfer: must implement a 'ready' callback) + * @return pointer to the created display + */ +lv_display_t * lv_lcd_generic_mipi_create(uint32_t hor_res, uint32_t ver_res, lv_lcd_flag_t flags, + lv_lcd_send_cmd_cb_t send_cmd_cb, lv_lcd_send_color_cb_t send_color_cb); + +/** + * Set gap, i.e., the offset of the (0,0) pixel in the VRAM + * @param disp display object + * @param x x offset + * @param y y offset + */ +void lv_lcd_generic_mipi_set_gap(lv_display_t * disp, uint16_t x, uint16_t y); + +/** + * Set color inversion + * @param disp display object + * @param invert false: normal, true: invert + */ +void lv_lcd_generic_mipi_set_invert(lv_display_t * disp, bool invert); + +/** + * Set address mode + * @param disp display object + * @param mirror_x horizontal mirror (false: normal, true: mirrored) + * @param mirror_y vertical mirror (false: normal, true: mirrored) + * @param swap_xy swap axes (false: normal, true: swap) + * @param bgr RGB/BGR order (false: RGB, true: BGR) + */ +void lv_lcd_generic_mipi_set_address_mode(lv_display_t * disp, bool mirror_x, bool mirror_y, bool swap_xy, bool bgr); + +/** + * Set gamma curve + * @param disp display object + * @param gamma gamma curve + */ +void lv_lcd_generic_mipi_set_gamma_curve(lv_display_t * disp, uint8_t gamma); + +/** + * Send list of commands. + * @param disp display object + * @param cmd_list controller and panel-specific commands + */ +void lv_lcd_generic_mipi_send_cmd_list(lv_display_t * disp, const uint8_t * cmd_list); + +/********************** + * OTHERS + **********************/ + +/********************** + * MACROS + **********************/ + + +#endif /*LV_USE_GENERIC_MIPI*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_LCD_GENERIC_MIPI_H*/ diff --git a/include/liblvgl/drivers/display/renesas_glcdc/lv_renesas_glcdc.h b/include/liblvgl/drivers/display/renesas_glcdc/lv_renesas_glcdc.h new file mode 100644 index 00000000..0558614e --- /dev/null +++ b/include/liblvgl/drivers/display/renesas_glcdc/lv_renesas_glcdc.h @@ -0,0 +1,57 @@ +/** + * @file lv_renesas_glcdc.h + * + */ + +#ifndef LV_RENESAS_GLCDC_H +#define LV_RENESAS_GLCDC_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../../display/lv_display.h" + +#if LV_USE_RENESAS_GLCDC + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create a display using Renesas' GLCDC peripherial in DIRECT render mode + * @return pointer to the created display + */ +lv_display_t * lv_renesas_glcdc_direct_create(void); + +/** + * Create a display using Renesas' GLCDC peripherial in PARTIAL render mode + * @param buf1 first buffer + * @param buf2 second buffer (can be `NULL`) + * @param buf_size buffer size in byte + * @return pointer to the created display + */ +lv_display_t * lv_renesas_glcdc_partial_create(void * buf1, void * buf2, size_t buf_size); + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_RENESAS_GLCDC */ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LV_RENESAS_GLCDC_H */ diff --git a/include/liblvgl/drivers/display/st7735/lv_st7735.h b/include/liblvgl/drivers/display/st7735/lv_st7735.h new file mode 100644 index 00000000..c19e9fad --- /dev/null +++ b/include/liblvgl/drivers/display/st7735/lv_st7735.h @@ -0,0 +1,93 @@ +/** + * @file lv_st7735.h + * + * This driver is just a wrapper around the generic MIPI compatible LCD controller driver + * + */ + +#ifndef LV_ST7735_H +#define LV_ST7735_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../lcd/lv_lcd_generic_mipi.h" + +#if LV_USE_ST7735 + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef lv_lcd_send_cmd_cb_t lv_st7735_send_cmd_cb_t; +typedef lv_lcd_send_color_cb_t lv_st7735_send_color_cb_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create an LCD display with ST7735 driver + * @param hor_res horizontal resolution + * @param ver_res vertical resolution + * @param flags default configuration settings (mirror, RGB ordering, etc.) + * @param send_cmd platform-dependent function to send a command to the LCD controller (usually uses polling transfer) + * @param send_color platform-dependent function to send pixel data to the LCD controller (usually uses DMA transfer: must implement a 'ready' callback) + * @return pointer to the created display + */ +lv_display_t * lv_st7735_create(uint32_t hor_res, uint32_t ver_res, lv_lcd_flag_t flags, + lv_st7735_send_cmd_cb_t send_cmd_cb, lv_st7735_send_color_cb_t send_color_cb); + +/** + * Set gap, i.e., the offset of the (0,0) pixel in the VRAM + * @param disp display object + * @param x x offset + * @param y y offset + */ +void lv_st7735_set_gap(lv_display_t * disp, uint16_t x, uint16_t y); + +/** + * Set color inversion + * @param disp display object + * @param invert false: normal, true: invert + */ +void lv_st7735_set_invert(lv_display_t * disp, bool invert); + +/** + * Set gamma curve + * @param disp display object + * @param gamma gamma curve + */ +void lv_st7735_set_gamma_curve(lv_display_t * disp, uint8_t gamma); + +/** + * Send list of commands. + * @param disp display object + * @param cmd_list controller and panel-specific commands + */ +void lv_st7735_send_cmd_list(lv_display_t * disp, const uint8_t * cmd_list); + +/********************** + * OTHERS + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_ST7735*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_ST7735_H*/ diff --git a/include/liblvgl/drivers/display/st7789/lv_st7789.h b/include/liblvgl/drivers/display/st7789/lv_st7789.h new file mode 100644 index 00000000..9770432a --- /dev/null +++ b/include/liblvgl/drivers/display/st7789/lv_st7789.h @@ -0,0 +1,94 @@ +/** + * @file lv_st7789.h + * + * This driver is just a wrapper around the generic MIPI compatible LCD controller driver + * + */ + +#ifndef LV_ST7789_H +#define LV_ST7789_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../lcd/lv_lcd_generic_mipi.h" + +#if LV_USE_ST7789 + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef lv_lcd_send_cmd_cb_t lv_st7789_send_cmd_cb_t; +typedef lv_lcd_send_color_cb_t lv_st7789_send_color_cb_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create an LCD display with ST7789 driver + * @param hor_res horizontal resolution + * @param ver_res vertical resolution + * @param flags default configuration settings (mirror, RGB ordering, etc.) + * @param send_cmd platform-dependent function to send a command to the LCD controller (usually uses polling transfer) + * @param send_color platform-dependent function to send pixel data to the LCD controller (usually uses DMA transfer: must implement a 'ready' callback) + * @return pointer to the created display + */ +lv_display_t * lv_st7789_create(uint32_t hor_res, uint32_t ver_res, lv_lcd_flag_t flags, + lv_st7789_send_cmd_cb_t send_cmd_cb, lv_st7789_send_color_cb_t send_color_cb); + +/** + * Set gap, i.e., the offset of the (0,0) pixel in the VRAM + * @param disp display object + * @param x x offset + * @param y y offset + */ +void lv_st7789_set_gap(lv_display_t * disp, uint16_t x, uint16_t y); + +/** + * Set color inversion + * @param disp display object + * @param invert false: normal, true: invert + */ +void lv_st7789_set_invert(lv_display_t * disp, bool invert); + +/** + * Set gamma curve + * @param disp display object + * @param gamma gamma curve + */ +void lv_st7789_set_gamma_curve(lv_display_t * disp, uint8_t gamma); + +/** + * Send list of commands. + * @param disp display object + * @param cmd_list controller and panel-specific commands + */ +void lv_st7789_send_cmd_list(lv_display_t * disp, const uint8_t * cmd_list); + +/********************** + * OTHERS + **********************/ + +/********************** + * MACROS + **********************/ + + +#endif /*LV_USE_ST7789*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_ST7789_H*/ diff --git a/include/liblvgl/drivers/display/st7796/lv_st7796.h b/include/liblvgl/drivers/display/st7796/lv_st7796.h new file mode 100644 index 00000000..79d7a883 --- /dev/null +++ b/include/liblvgl/drivers/display/st7796/lv_st7796.h @@ -0,0 +1,93 @@ +/** + * @file lv_st7796.h + * + * This driver is just a wrapper around the generic MIPI compatible LCD controller driver + * + */ + +#ifndef LV_ST7796_H +#define LV_ST7796_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../lcd/lv_lcd_generic_mipi.h" + +#if LV_USE_ST7796 + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef lv_lcd_send_cmd_cb_t lv_st7796_send_cmd_cb_t; +typedef lv_lcd_send_color_cb_t lv_st7796_send_color_cb_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create an LCD display with ST7796 driver + * @param hor_res horizontal resolution + * @param ver_res vertical resolution + * @param flags default configuration settings (mirror, RGB ordering, etc.) + * @param send_cmd platform-dependent function to send a command to the LCD controller (usually uses polling transfer) + * @param send_color platform-dependent function to send pixel data to the LCD controller (usually uses DMA transfer: must implement a 'ready' callback) + * @return pointer to the created display + */ +lv_display_t * lv_st7796_create(uint32_t hor_res, uint32_t ver_res, lv_lcd_flag_t flags, + lv_st7796_send_cmd_cb_t send_cmd_cb, lv_st7796_send_color_cb_t send_color_cb); + +/** + * Set gap, i.e., the offset of the (0,0) pixel in the VRAM + * @param disp display object + * @param x x offset + * @param y y offset + */ +void lv_st7796_set_gap(lv_display_t * disp, uint16_t x, uint16_t y); + +/** + * Set color inversion + * @param disp display object + * @param invert false: normal, true: invert + */ +void lv_st7796_set_invert(lv_display_t * disp, bool invert); + +/** + * Set gamma curve + * @param disp display object + * @param gamma gamma curve + */ +void lv_st7796_set_gamma_curve(lv_display_t * disp, uint8_t gamma); + +/** + * Send list of commands. + * @param disp display object + * @param cmd_list controller and panel-specific commands + */ +void lv_st7796_send_cmd_list(lv_display_t * disp, const uint8_t * cmd_list); + +/********************** + * OTHERS + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_ST7796*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_ST7796_H*/ diff --git a/include/liblvgl/drivers/display/st_ltdc/lv_st_ltdc.h b/include/liblvgl/drivers/display/st_ltdc/lv_st_ltdc.h new file mode 100644 index 00000000..69a0e7b7 --- /dev/null +++ b/include/liblvgl/drivers/display/st_ltdc/lv_st_ltdc.h @@ -0,0 +1,65 @@ +/** + * @file lv_st_ltdc.h + * + */ + +#ifndef LV_ST_LTDC_H +#define LV_ST_LTDC_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../../lv_conf_internal.h" +#if LV_USE_ST_LTDC + +#include "../../../display/lv_display.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create a direct render mode display bound to a LTDC layer. + * @param fb_adr_1 The LTDC layer's framebuffer memory address. + * @param fb_adr_2 An additional framebuffer-sized buffer to use for double buffering, or `NULL`. + * @param layer_idx The LTDC layer number to bind the display to. Typically 0 or 1. + * @return The display. + */ +lv_display_t * lv_st_ltdc_create_direct(void * fb_adr_1, void * fb_adr_2, uint32_t layer_idx); + +/** + * Create a partial render mode display bound to a LTDC layer. The layer's framebuffer is flushed to internally. + * Enable `LV_ST_LTDC_USE_DMA2D_FLUSH` for parallel flushing. + * @param render_buf_1 A render buffer. + * @param render_buf_2 An additional render buffer for double-buffering, or `NULL`. + * @param buf_size The size of the buffer(s) in bytes. + * @param layer_idx The LTDC layer number to bind the display to. Typically 0 or 1. + * @return The display. + */ +lv_display_t * lv_st_ltdc_create_partial(void * render_buf_1, void * render_buf_2, uint32_t buf_size, + uint32_t layer_idx); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_ST_LTDC*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_ST_LTDC_H*/ diff --git a/include/liblvgl/drivers/display/tft_espi/lv_tft_espi.cpp b/include/liblvgl/drivers/display/tft_espi/lv_tft_espi.cpp new file mode 100644 index 00000000..1cb7b45f --- /dev/null +++ b/include/liblvgl/drivers/display/tft_espi/lv_tft_espi.cpp @@ -0,0 +1,110 @@ +/** + * @file lv_tft_espi.cpp + * + */ + +/********************* + * INCLUDES + *********************/ +#include "lv_tft_espi.h" +#if LV_USE_TFT_ESPI + +#include + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ +typedef struct { + TFT_eSPI * tft; +} lv_tft_espi_t; + +/********************** + * STATIC PROTOTYPES + **********************/ +static void flush_cb(lv_display_t * disp, const lv_area_t * area, uint8_t * px_map); +static void resolution_changed_event_cb(lv_event_t * e); + +/********************** + * STATIC VARIABLES + **********************/ + +/********************** + * MACROS + **********************/ + +/********************** + * GLOBAL FUNCTIONS + **********************/ + +lv_display_t * lv_tft_espi_create(uint32_t hor_res, uint32_t ver_res, void * buf, uint32_t buf_size_bytes) +{ + lv_tft_espi_t * dsc = (lv_tft_espi_t *)lv_malloc_zeroed(sizeof(lv_tft_espi_t)); + LV_ASSERT_MALLOC(dsc); + if(dsc == NULL) return NULL; + + lv_display_t * disp = lv_display_create(hor_res, ver_res); + if(disp == NULL) { + lv_free(dsc); + return NULL; + } + + dsc->tft = new TFT_eSPI(hor_res, ver_res); + dsc->tft->begin(); /* TFT init */ + dsc->tft->setRotation(0); + lv_display_set_driver_data(disp, (void *)dsc); + lv_display_set_flush_cb(disp, flush_cb); + lv_display_add_event_cb(disp, resolution_changed_event_cb, LV_EVENT_RESOLUTION_CHANGED, NULL); + lv_display_set_buffers(disp, (void *)buf, NULL, buf_size_bytes, LV_DISPLAY_RENDER_MODE_PARTIAL); + return disp; +} + +/********************** + * STATIC FUNCTIONS + **********************/ + +static void flush_cb(lv_display_t * disp, const lv_area_t * area, uint8_t * px_map) +{ + lv_tft_espi_t * dsc = (lv_tft_espi_t *)lv_display_get_driver_data(disp); + + uint32_t w = (area->x2 - area->x1 + 1); + uint32_t h = (area->y2 - area->y1 + 1); + + dsc->tft->startWrite(); + dsc->tft->setAddrWindow(area->x1, area->y1, w, h); + dsc->tft->pushColors((uint16_t *)px_map, w * h, true); + dsc->tft->endWrite(); + + lv_display_flush_ready(disp); + +} + +static void resolution_changed_event_cb(lv_event_t * e) +{ + lv_display_t * disp = (lv_display_t *)lv_event_get_target(e); + lv_tft_espi_t * dsc = (lv_tft_espi_t *)lv_display_get_driver_data(disp); + int32_t hor_res = lv_display_get_horizontal_resolution(disp); + int32_t ver_res = lv_display_get_vertical_resolution(disp); + lv_display_rotation_t rot = lv_display_get_rotation(disp); + + /* handle rotation */ + switch(rot) { + case LV_DISPLAY_ROTATION_0: + dsc->tft->setRotation(0); /* Portrait orientation */ + break; + case LV_DISPLAY_ROTATION_90: + dsc->tft->setRotation(1); /* Landscape orientation */ + break; + case LV_DISPLAY_ROTATION_180: + dsc->tft->setRotation(2); /* Portrait orientation, flipped */ + break; + case LV_DISPLAY_ROTATION_270: + dsc->tft->setRotation(3); /* Landscape orientation, flipped */ + break; + } +} + +#endif /*LV_USE_TFT_ESPI*/ diff --git a/include/liblvgl/drivers/display/tft_espi/lv_tft_espi.h b/include/liblvgl/drivers/display/tft_espi/lv_tft_espi.h new file mode 100644 index 00000000..a8f49f89 --- /dev/null +++ b/include/liblvgl/drivers/display/tft_espi/lv_tft_espi.h @@ -0,0 +1,43 @@ +/** + * @file lv_tft_espi.h + * + */ + +#ifndef LV_TFT_ESPI_H +#define LV_TFT_ESPI_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../../display/lv_display.h" + +#if LV_USE_TFT_ESPI + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ +lv_display_t * lv_tft_espi_create(uint32_t hor_res, uint32_t ver_res, void * buf, uint32_t buf_size_bytes); + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_TFT_ESPI */ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LV_TFT_ESPI_H */ diff --git a/include/liblvgl/drivers/evdev/lv_evdev.h b/include/liblvgl/drivers/evdev/lv_evdev.h new file mode 100644 index 00000000..95ab1544 --- /dev/null +++ b/include/liblvgl/drivers/evdev/lv_evdev.h @@ -0,0 +1,64 @@ +/** + * @file lv_evdev.h + * + */ + +#ifndef LV_EVDEV_H +#define LV_EVDEV_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../indev/lv_indev.h" + +#if LV_USE_EVDEV + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create evdev input device. + * @param type LV_INDEV_TYPE_POINTER or LV_INDEV_TYPE_KEYPAD + * @param dev_path device path, e.g., /dev/input/event0 + * @return pointer to input device or NULL if opening failed + */ +lv_indev_t * lv_evdev_create(lv_indev_type_t indev_type, const char * dev_path); + +/** + * Set whether coordinates of pointer device should be swapped. Defaults to + * false. + * @param indev evdev input device + * @param swap_axes whether to swap x and y axes + */ +void lv_evdev_set_swap_axes(lv_indev_t * indev, bool swap_axes); + +/** + * Configure a coordinate transformation for pointer devices. Applied after + * axis swap, if any. Defaults to apply no transformation. + * @param indev evdev input device + * @param min_x pointer coordinate mapped to min x of display + * @param min_y pointer coordinate mapped to min y of display + * @param max_x pointer coordinate mapped to max x of display + * @param max_y pointer coordinate mapped to max y of display + */ +void lv_evdev_set_calibration(lv_indev_t * indev, int min_x, int min_y, int max_x, int max_y); + +/** + * Remove evdev input device. + * @param indev evdev input device to close and free + */ +void lv_evdev_delete(lv_indev_t * indev); + +#endif /*LV_USE_EVDEV*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_EVDEV_H*/ diff --git a/include/liblvgl/drivers/glfw/lv_glfw_window.h b/include/liblvgl/drivers/glfw/lv_glfw_window.h new file mode 100644 index 00000000..e8e51610 --- /dev/null +++ b/include/liblvgl/drivers/glfw/lv_glfw_window.h @@ -0,0 +1,108 @@ +/** + * @file lv_glfw_window.h + * + */ + +#ifndef LV_GLFW_WINDOW_H +#define LV_GLFW_WINDOW_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../lv_conf_internal.h" +#if LV_USE_OPENGLES + +#include "../../misc/lv_types.h" +#include "../../display/lv_display.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create a GLFW window with no textures and initialize OpenGL + * @param hor_res width in pixels of the window + * @param ver_res height in pixels of the window + * @param use_mouse_indev send pointer indev input to LVGL display textures + * @return the new GLFW window handle + */ +lv_glfw_window_t * lv_glfw_window_create(int32_t hor_res, int32_t ver_res, bool use_mouse_indev); + +/** + * Delete a GLFW window. If it is the last one, the process will exit + * @param window GLFW window to delete + */ +void lv_glfw_window_delete(lv_glfw_window_t * window); + +/** + * Add a texture to the GLFW window. It can be an LVGL display texture, or any OpenGL texture + * @param window GLFW window + * @param texture_id OpenGL texture ID + * @param w width in pixels of the texture + * @param h height in pixels of the texture + * @return the new texture handle + */ +lv_glfw_texture_t * lv_glfw_window_add_texture(lv_glfw_window_t * window, unsigned int texture_id, int32_t w, + int32_t h); + +/** + * Remove a texture from its GLFW window and delete it + * @param texture handle of a GLFW window texture + */ +void lv_glfw_texture_remove(lv_glfw_texture_t * texture); + +/** + * Set the x position of a texture within its GLFW window + * @param texture handle of a GLFW window texture + * @param x new x position of the texture + */ +void lv_glfw_texture_set_x(lv_glfw_texture_t * texture, int32_t x); + +/** + * Set the y position of a texture within its GLFW window + * @param texture handle of a GLFW window texture + * @param y new y position of the texture + */ +void lv_glfw_texture_set_y(lv_glfw_texture_t * texture, int32_t y); + +/** + * Set the opacity of a texture in a GLFW window + * @param texture handle of a GLFW window texture + * @param opa new opacity of the texture + */ +void lv_glfw_texture_set_opa(lv_glfw_texture_t * texture, lv_opa_t opa); + +/** + * Get the mouse indev associated with a texture in a GLFW window, if it exists + * @param texture handle of a GLFW window texture + * @return the indev or `NULL` + * @note there will only be an indev if the texture is based on an + * LVGL display texture and the window was created with + * `use_mouse_indev` as `true` + */ +lv_indev_t * lv_glfw_texture_get_mouse_indev(lv_glfw_texture_t * texture); + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_OPENGLES */ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LV_GLFW_WINDOW_H */ diff --git a/include/liblvgl/drivers/glfw/lv_glfw_window_private.h b/include/liblvgl/drivers/glfw/lv_glfw_window_private.h new file mode 100644 index 00000000..cc6cac09 --- /dev/null +++ b/include/liblvgl/drivers/glfw/lv_glfw_window_private.h @@ -0,0 +1,70 @@ +/** + * @file lv_glfw_window_private.h + * + */ + +#ifndef LV_GLFW_WINDOW_PRIVATE_H +#define LV_GLFW_WINDOW_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_glfw_window.h" +#if LV_USE_OPENGLES + +#include +#include + +#include "../../misc/lv_area.h" +#include "../../display/lv_display.h" +#include "../../indev/lv_indev.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_glfw_window_t { + GLFWwindow * window; + int32_t hor_res; + int32_t ver_res; + lv_ll_t textures; + lv_point_t mouse_last_point; + lv_indev_state_t mouse_last_state; + uint8_t use_indev : 1; + uint8_t closing : 1; +}; + +struct _lv_glfw_texture_t { + lv_glfw_window_t * window; + unsigned int texture_id; + lv_area_t area; + lv_opa_t opa; + lv_indev_t * indev; + lv_point_t indev_last_point; + lv_indev_state_t indev_last_state; +}; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_OPENGLES*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_GLFW_WINDOW_PRIVATE_H*/ diff --git a/include/liblvgl/drivers/glfw/lv_opengles_debug.h b/include/liblvgl/drivers/glfw/lv_opengles_debug.h new file mode 100644 index 00000000..aa8bbebe --- /dev/null +++ b/include/liblvgl/drivers/glfw/lv_opengles_debug.h @@ -0,0 +1,40 @@ +/** + * @file lv_opengles_debug.h + * + */ + +#ifndef LV_OPENGLES_DEBUG_H +#define LV_OPENGLES_DEBUG_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "../../lv_conf_internal.h" +#if LV_USE_OPENGLES + +#include +#include +#include + +void GLClearError(void); + +void GLLogCall(const char * function, const char * file, int line); + +#if LV_USE_OPENGLES_DEBUG +#define GL_CALL(x) do {\ + GLClearError();\ + x;\ + GLLogCall(#x, __FILE__, __LINE__);\ + } while(0) +#else +#define GL_CALL(x) x +#endif + +#endif /* LV_USE_OPENGLES */ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LV_OPENGLES_DEBUG_H */ diff --git a/include/liblvgl/drivers/glfw/lv_opengles_driver.h b/include/liblvgl/drivers/glfw/lv_opengles_driver.h new file mode 100644 index 00000000..34bda861 --- /dev/null +++ b/include/liblvgl/drivers/glfw/lv_opengles_driver.h @@ -0,0 +1,92 @@ +/** + * @file lv_opengles_driver.h + * + */ + +#ifndef LV_OPENGLES_DRIVER_H +#define LV_OPENGLES_DRIVER_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../lv_conf_internal.h" +#if LV_USE_OPENGLES + +#include "../../misc/lv_area.h" +#include "../../misc/lv_color.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Initialize OpenGL + * @note it is not necessary to call this if you use `lv_glfw_window_create` + */ +void lv_opengles_init(void); + +/** + * Deinitialize OpenGL + * @note it is not necessary to call this if you use `lv_glfw_window_create` + */ +void lv_opengles_deinit(void); + +/** + * Render a texture + * @param texture OpenGL texture ID + * @param texture_area the area in the window to render the texture in + * @param opa opacity to blend the texture with existing contents + * @param disp_w width of the window/framebuffer being rendered to + * @param disp_h height of the window/framebuffer being rendered to + */ +void lv_opengles_render_texture(unsigned int texture, const lv_area_t * texture_area, lv_opa_t opa, int32_t disp_w, + int32_t disp_h, const lv_area_t * texture_clip_area, bool flip); + +/** + * Render a fill + * @param color the color of the fill + * @param area the area in the window to render the fill + * @param opa opacity to blend the fill with existing contents + * @param disp_w width of the window/framebuffer being rendered to + * @param disp_h height of the window/framebuffer being rendered to + */ +void lv_opengles_render_fill(lv_color_t color, const lv_area_t * area, lv_opa_t opa, int32_t disp_w, int32_t disp_h); + +/** + * Clear the window/display + */ +void lv_opengles_render_clear(void); + +/** + * Set the OpenGL viewport + * @param x x position of the viewport + * @param y y position of the viewport + * @param w width of the viewport + * @param h height of the viewport + */ +void lv_opengles_viewport(int32_t x, int32_t y, int32_t w, int32_t h); + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_OPENGLES */ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LV_OPENGLES_DRIVER_H */ diff --git a/include/liblvgl/drivers/glfw/lv_opengles_texture.h b/include/liblvgl/drivers/glfw/lv_opengles_texture.h new file mode 100644 index 00000000..0d53b2b1 --- /dev/null +++ b/include/liblvgl/drivers/glfw/lv_opengles_texture.h @@ -0,0 +1,66 @@ +/** + * @file lv_opengles_texture.h + * + */ + +#ifndef LV_OPENGLES_TEXTURE_H +#define LV_OPENGLES_TEXTURE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../lv_conf_internal.h" +#if LV_USE_OPENGLES + +#include "../../display/lv_display.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create a display that flushes to an OpenGL texture + * @param w width in pixels of the texture + * @param h height in pixels of the texture + * @return the new display + */ +lv_display_t * lv_opengles_texture_create(int32_t w, int32_t h); + +/** + * Get the OpenGL texture ID of the display + * @param disp display + * @return texture ID + */ +unsigned int lv_opengles_texture_get_texture_id(lv_display_t * disp); + +/** + * Get the display of an OpenGL texture if it is associated with one + * @param texture_id OpenGL texture ID + * @return display or `NULL` if there no display with that texture ID + */ +lv_display_t * lv_opengles_texture_get_from_texture_id(unsigned int texture_id); + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_OPENGLES */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_OPENGLES_TEXTURE_H*/ diff --git a/include/liblvgl/drivers/libinput/lv_libinput.h b/include/liblvgl/drivers/libinput/lv_libinput.h new file mode 100644 index 00000000..ce19a33b --- /dev/null +++ b/include/liblvgl/drivers/libinput/lv_libinput.h @@ -0,0 +1,101 @@ +/** + * @file lv_libinput.h + * + */ + +#ifndef LV_LIBINPUT_H +#define LV_LIBINPUT_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../indev/lv_indev.h" + +#if LV_USE_LIBINPUT + +#include +#include + +#if LV_LIBINPUT_XKB +#include "lv_xkb.h" +#endif /* LV_LIBINPUT_XKB */ + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ +typedef enum { + LV_LIBINPUT_CAPABILITY_NONE = 0, + LV_LIBINPUT_CAPABILITY_KEYBOARD = 1U << 0, + LV_LIBINPUT_CAPABILITY_POINTER = 1U << 1, + LV_LIBINPUT_CAPABILITY_TOUCH = 1U << 2 +} lv_libinput_capability; + +struct libinput_device; + +#define LV_LIBINPUT_MAX_EVENTS 32 + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Determine the capabilities of a specific libinput device. + * @param device the libinput device to query + * @return the supported input capabilities + */ +lv_libinput_capability lv_libinput_query_capability(struct libinput_device * device); + +/** + * Find connected input device with specific capabilities + * @param capabilities required device capabilities + * @param force_rescan erase the device cache (if any) and rescan the file system for available devices + * @return device node path (e.g. /dev/input/event0) for the first matching device or NULL if no device was found. + * The pointer is safe to use until the next forceful device search. + */ +char * lv_libinput_find_dev(lv_libinput_capability capabilities, bool force_rescan); + +/** + * Find connected input devices with specific capabilities + * @param capabilities required device capabilities + * @param devices pre-allocated array to store the found device node paths (e.g. /dev/input/event0). The pointers are + * safe to use until the next forceful device search. + * @param count maximum number of devices to find (the devices array should be at least this long) + * @param force_rescan erase the device cache (if any) and rescan the file system for available devices + * @return number of devices that were found + */ +size_t lv_libinput_find_devs(lv_libinput_capability capabilities, char ** found, size_t count, bool force_rescan); + +/** + * Create a new libinput input device + * @param type LV_INDEV_TYPE_POINTER or LV_INDEV_TYPE_KEYPAD + * @param dev_path device path, e.g. /dev/input/event0 + * @return pointer to input device or NULL if opening failed + */ +lv_indev_t * lv_libinput_create(lv_indev_type_t indev_type, const char * dev_path); + +/** + * Delete a libinput input device + * @param indev pointer to input device + */ +void lv_libinput_delete(lv_indev_t * indev); + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_LIBINPUT */ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LV_LIBINPUT_H */ diff --git a/include/liblvgl/drivers/libinput/lv_libinput_private.h b/include/liblvgl/drivers/libinput/lv_libinput_private.h new file mode 100644 index 00000000..2856bbef --- /dev/null +++ b/include/liblvgl/drivers/libinput/lv_libinput_private.h @@ -0,0 +1,83 @@ +/** + * @file lv_libinput_private.h + * + */ + +#ifndef LV_LIBINPUT_PRIVATE_H +#define LV_LIBINPUT_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_libinput.h" + +#if LV_USE_LIBINPUT + +#if LV_LIBINPUT_XKB +#include "lv_xkb_private.h" +#endif + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_libinput_event_t { + lv_indev_state_t pressed; + int key_val; + lv_point_t point; +}; + +struct _lv_libinput_t { + int fd; + struct pollfd fds[1]; + + /* The points array is implemented as a circular LIFO queue */ + lv_libinput_event_t points[LV_LIBINPUT_MAX_EVENTS]; /* Event buffer */ + lv_libinput_event_t slots[2]; /* Realtime state of up to 2 fingers to handle multitouch */ + + /* Pointer devices work a bit differently in libinput which requires us to store their last known state */ + lv_point_t pointer_position; + bool pointer_button_down; + + int start; /* Index of start of event queue */ + int end; /* Index of end of queue*/ + lv_libinput_event_t last_event; /* Report when no new events + * to keep indev state consistent + */ + bool deinit; /* Tell worker thread to quit */ + pthread_mutex_t event_lock; + pthread_t worker_thread; + + struct libinput * libinput_context; + struct libinput_device * libinput_device; + +#if LV_LIBINPUT_XKB + lv_xkb_t xkb; +#endif /* LV_LIBINPUT_XKB */ +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_LIBINPUT */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_LIBINPUT_PRIVATE_H*/ diff --git a/include/liblvgl/drivers/libinput/lv_xkb.h b/include/liblvgl/drivers/libinput/lv_xkb.h new file mode 100644 index 00000000..3f9fd86e --- /dev/null +++ b/include/liblvgl/drivers/libinput/lv_xkb.h @@ -0,0 +1,64 @@ +/** + * @file lv_xkb.h + * + */ + +#ifndef LV_XKB_H +#define LV_XKB_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../lv_conf_internal.h" + +#if defined(LV_LIBINPUT_XKB) && LV_LIBINPUT_XKB + +#include "../../misc/lv_types.h" +#include +#include + +/********************* + * DEFINES + *********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Initialise an XKB descriptor. + * @return true if the initialisation was successful + */ +bool lv_xkb_init(lv_xkb_t * dsc, struct xkb_rule_names names); + +/** + * De-initialise an XKB descriptor. + * @param dsc Pointer to descriptor + */ +void lv_xkb_deinit(lv_xkb_t * dsc); + +/** + * Process an evdev scancode using a specific XKB descriptor. + * @param state XKB descriptor to use + * @param scancode evdev scancode to process + * @param down true if the key was pressed, false if it was releases + * @return the (first) UTF-8 character produced by the event or 0 if no output was produced + */ +uint32_t lv_xkb_process_key(lv_xkb_t * dsc, uint32_t scancode, bool down); + +/********************** + * MACROS + **********************/ + +#endif /* LV_LIBINPUT_XKB */ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* defined(LV_LIBINPUT_XKB) && LV_LIBINPUT_XKB */ diff --git a/include/liblvgl/drivers/libinput/lv_xkb_private.h b/include/liblvgl/drivers/libinput/lv_xkb_private.h new file mode 100644 index 00000000..97aad749 --- /dev/null +++ b/include/liblvgl/drivers/libinput/lv_xkb_private.h @@ -0,0 +1,53 @@ +/** + * @file lv_xkb_private.h + * + */ + +#ifndef LV_XKB_PRIVATE_H +#define LV_XKB_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_xkb.h" + +#if defined(LV_LIBINPUT_XKB) && LV_LIBINPUT_XKB + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_xkb_t { + struct xkb_keymap * keymap; + struct xkb_state * state; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* defined(LV_LIBINPUT_XKB) && LV_LIBINPUT_XKB */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_XKB_PRIVATE_H*/ diff --git a/include/liblvgl/drivers/lv_drivers.h b/include/liblvgl/drivers/lv_drivers.h new file mode 100644 index 00000000..621f1565 --- /dev/null +++ b/include/liblvgl/drivers/lv_drivers.h @@ -0,0 +1,77 @@ +/** + * @file lv_drivers.h + * + */ + +#ifndef LV_DRIVERS_H +#define LV_DRIVERS_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "sdl/lv_sdl_window.h" +#include "sdl/lv_sdl_mouse.h" +#include "sdl/lv_sdl_mousewheel.h" +#include "sdl/lv_sdl_keyboard.h" + +#include "x11/lv_x11.h" + +#include "display/drm/lv_linux_drm.h" +#include "display/fb/lv_linux_fbdev.h" + +#include "display/tft_espi/lv_tft_espi.h" + +#include "display/lcd/lv_lcd_generic_mipi.h" +#include "display/ili9341/lv_ili9341.h" +#include "display/st7735/lv_st7735.h" +#include "display/st7789/lv_st7789.h" +#include "display/st7796/lv_st7796.h" + +#include "display/renesas_glcdc/lv_renesas_glcdc.h" +#include "display/st_ltdc/lv_st_ltdc.h" + +#include "nuttx/lv_nuttx_entry.h" +#include "nuttx/lv_nuttx_fbdev.h" +#include "nuttx/lv_nuttx_touchscreen.h" +#include "nuttx/lv_nuttx_lcd.h" +#include "nuttx/lv_nuttx_libuv.h" + +#include "evdev/lv_evdev.h" +#include "libinput/lv_libinput.h" + +#include "windows/lv_windows_input.h" +#include "windows/lv_windows_display.h" + +#include "glfw/lv_glfw_window.h" +#include "glfw/lv_opengles_texture.h" +#include "glfw/lv_opengles_driver.h" + +#include "qnx/lv_qnx.h" + +#include "wayland/lv_wayland.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DRIVERS_H*/ diff --git a/include/liblvgl/font/lv_font_loader.h b/include/liblvgl/drivers/nuttx/lv_nuttx_cache.h similarity index 69% rename from include/liblvgl/font/lv_font_loader.h rename to include/liblvgl/drivers/nuttx/lv_nuttx_cache.h index 783cb2e7..74be6d3a 100644 --- a/include/liblvgl/font/lv_font_loader.h +++ b/include/liblvgl/drivers/nuttx/lv_nuttx_cache.h @@ -1,10 +1,10 @@ /** - * @file lv_font_loader.h + * @file lv_nuttx_cache.h * */ -#ifndef LV_FONT_LOADER_H -#define LV_FONT_LOADER_H +#ifndef LV_NUTTX_CACHE_H +#define LV_NUTTX_CACHE_H #ifdef __cplusplus extern "C" { @@ -26,8 +26,9 @@ extern "C" { * GLOBAL PROTOTYPES **********************/ -lv_font_t * lv_font_load(const char * fontName); -void lv_font_free(lv_font_t * font); +void lv_nuttx_cache_init(void); + +void lv_nuttx_cache_deinit(void); /********************** * MACROS @@ -37,4 +38,4 @@ void lv_font_free(lv_font_t * font); } /*extern "C"*/ #endif -#endif /*LV_FONT_LOADER_H*/ +#endif /*LV_NUTTX_CACHE_H*/ diff --git a/include/liblvgl/drivers/nuttx/lv_nuttx_entry.h b/include/liblvgl/drivers/nuttx/lv_nuttx_entry.h new file mode 100644 index 00000000..049e741e --- /dev/null +++ b/include/liblvgl/drivers/nuttx/lv_nuttx_entry.h @@ -0,0 +1,112 @@ +/** + * @file lv_nuttx_entry.h + * + */ + +/********************* + * INCLUDES + *********************/ + +#ifndef LV_NUTTX_ENTRY_H +#define LV_NUTTX_ENTRY_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../lv_conf_internal.h" +#include "../../display/lv_display.h" +#include "../../indev/lv_indev.h" + +#if LV_USE_NUTTX + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ +typedef struct { + const char * fb_path; + const char * input_path; + const char * utouch_path; +} lv_nuttx_dsc_t; + +typedef struct { + lv_display_t * disp; + lv_indev_t * indev; + lv_indev_t * utouch_indev; +} lv_nuttx_result_t; + +typedef struct _lv_nuttx_ctx_t { + void * image_cache; +} lv_nuttx_ctx_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Initialize the lv_nuttx_dsc_t structure with default values for the NuttX port of LVGL. + * @param dsc Pointer to the lv_nuttx_dsc_t structure to be initialized. + */ +void lv_nuttx_dsc_init(lv_nuttx_dsc_t * dsc); + +/** + * Initialize the LVGL display driver for NuttX using the provided configuration information. + * @param dsc Pointer to the lv_nuttx_dsc_t structure containing the configuration information for the display driver. + * @param result Pointer to the lv_nuttx_result_t structure containing display and input device handler. + */ +void lv_nuttx_init(const lv_nuttx_dsc_t * dsc, lv_nuttx_result_t * result); + +/** + * Deinitialize the LVGL display driver for NuttX. + * @param result Pointer to the lv_nuttx_result_t structure containing display and input device handler. + */ +void lv_nuttx_deinit(lv_nuttx_result_t * result); + +#if LV_USE_NUTTX_CUSTOM_INIT +/** + * Initialize the LVGL display driver for NuttX using the provided custom configuration information. + * @param dsc Pointer to the lv_nuttx_dsc_t structure containing the custom configuration for the display driver. + * @param result Pointer to the lv_nuttx_result_t structure containing display and input device handler. + */ +void lv_nuttx_init_custom(const lv_nuttx_dsc_t * dsc, lv_nuttx_result_t * result); + +/** + * Deinitialize the LVGL display driver for NuttX using the provided custom configuration information. + * @param result Pointer to the lv_nuttx_result_t structure containing display and input device handler. + */ +void lv_nuttx_deinit_custom(lv_nuttx_result_t * result); +#endif /* LV_USE_NUTTX_CUSTOM_INIT */ + +/** + * Call `lv_timer_handler()` (LVGL's super loop) in an endless loop. + * If LV_USE_NUTTX_LIBUV is enabled an UV timer will be created, + * else `lv_timer_handler()` will be called in a loop with some sleep. + * @param result pointer to a variable initialized by `lv_nuttx_init()` or `lv_nuttx_init_custom()` + */ +void lv_nuttx_run(lv_nuttx_result_t * result); + +/** + * Get the idle percentage of the system. + * @return The idle percentage of the system. + */ +uint32_t lv_nuttx_get_idle(void); + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_NUTTX*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LV_NUTTX_ENTRY_H */ diff --git a/include/liblvgl/drivers/nuttx/lv_nuttx_fbdev.h b/include/liblvgl/drivers/nuttx/lv_nuttx_fbdev.h new file mode 100644 index 00000000..4310aa9c --- /dev/null +++ b/include/liblvgl/drivers/nuttx/lv_nuttx_fbdev.h @@ -0,0 +1,55 @@ +/** + * @file lv_nuttx_fbdev.h + * + */ + +#ifndef LV_NUTTX_FBDEV_H +#define LV_NUTTX_FBDEV_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../display/lv_display.h" + +#if LV_USE_NUTTX + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create a new display with NuttX backend. + */ +lv_display_t * lv_nuttx_fbdev_create(void); + +/** + * Initialize display with specified framebuffer device + * @param disp pointer to display with NuttX backend + * @param file the name of framebuffer device + */ +int lv_nuttx_fbdev_set_file(lv_display_t * disp, const char * file); + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_NUTTX */ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LV_NUTTX_FBDEV_H */ diff --git a/include/liblvgl/drivers/nuttx/lv_nuttx_image_cache.h b/include/liblvgl/drivers/nuttx/lv_nuttx_image_cache.h new file mode 100644 index 00000000..0e056b4a --- /dev/null +++ b/include/liblvgl/drivers/nuttx/lv_nuttx_image_cache.h @@ -0,0 +1,44 @@ +/** + * @file lv_nuttx_image_cache.h + * + */ + +#ifndef LV_NUTTX_IMAGE_CACHE_H +#define LV_NUTTX_IMAGE_CACHE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../lv_conf_internal.h" +#include LV_STDBOOL_INCLUDE + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void lv_nuttx_image_cache_init(bool use_independent_image_heap); + +void lv_nuttx_image_cache_deinit(void); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_NUTTX_IMAGE_CACHE_H*/ diff --git a/include/liblvgl/drivers/nuttx/lv_nuttx_lcd.h b/include/liblvgl/drivers/nuttx/lv_nuttx_lcd.h new file mode 100644 index 00000000..16e24ff8 --- /dev/null +++ b/include/liblvgl/drivers/nuttx/lv_nuttx_lcd.h @@ -0,0 +1,49 @@ +/** + * @file lv_nuttx_lcd.h + * + */ + +#ifndef LV_NUTTX_LCD_H +#define LV_NUTTX_LCD_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../display/lv_display.h" + +#if LV_USE_NUTTX + +#if LV_USE_NUTTX_LCD + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +lv_display_t * lv_nuttx_lcd_create(const char * dev_path); + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_NUTTX_LCD */ + +#endif /* LV_USE_NUTTX*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LV_NUTTX_LCD_H */ diff --git a/include/liblvgl/drivers/nuttx/lv_nuttx_libuv.h b/include/liblvgl/drivers/nuttx/lv_nuttx_libuv.h new file mode 100644 index 00000000..0799e67b --- /dev/null +++ b/include/liblvgl/drivers/nuttx/lv_nuttx_libuv.h @@ -0,0 +1,66 @@ +/** + * @file lv_nuttx_libuv.h + * + */ + +#ifndef LV_NUTTX_LIBUV_H +#define LV_NUTTX_LIBUV_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../display/lv_display.h" +#include "../../indev/lv_indev.h" + +#if LV_USE_NUTTX + +#if LV_USE_NUTTX_LIBUV + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef struct { + void * loop; + lv_display_t * disp; + lv_indev_t * indev; +} lv_nuttx_uv_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Initialize the uv_loop using the provided configuration information. + * @param uv_info Pointer to the lv_nuttx_uv_t structure to be initialized. + */ +void * lv_nuttx_uv_init(lv_nuttx_uv_t * uv_info); + +/** + * Deinitialize the uv_loop configuration for NuttX porting layer. + * @param data Pointer to user data. + */ +void lv_nuttx_uv_deinit(void ** data); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_NUTTX_LIBUV*/ + +#endif /*LV_USE_NUTTX*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /*LV_NUTTX_LIBUV_H*/ diff --git a/include/liblvgl/drivers/nuttx/lv_nuttx_profiler.h b/include/liblvgl/drivers/nuttx/lv_nuttx_profiler.h new file mode 100644 index 00000000..8b8cf652 --- /dev/null +++ b/include/liblvgl/drivers/nuttx/lv_nuttx_profiler.h @@ -0,0 +1,39 @@ +/** + * @file lv_nuttx_profiler.h + * + */ + +#ifndef LV_NUTTX_PROFILER_H +#define LV_NUTTX_PROFILER_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void lv_nuttx_profiler_init(void); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_NUTTX_PROFILER_H*/ diff --git a/include/liblvgl/drivers/nuttx/lv_nuttx_touchscreen.h b/include/liblvgl/drivers/nuttx/lv_nuttx_touchscreen.h new file mode 100644 index 00000000..e0dd1f14 --- /dev/null +++ b/include/liblvgl/drivers/nuttx/lv_nuttx_touchscreen.h @@ -0,0 +1,57 @@ +/** + * @file lv_nuttx_touchscreen.h + * + */ + +/********************* + * INCLUDES + *********************/ + +#ifndef LV_NUTTX_TOUCHSCREEN_H +#define LV_NUTTX_TOUCHSCREEN_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../indev/lv_indev.h" + +#if LV_USE_NUTTX + +#if LV_USE_NUTTX_TOUCHSCREEN + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Initialize indev with specified input device. + * @param dev_path path of input device + */ +lv_indev_t * lv_nuttx_touchscreen_create(const char * dev_path); + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_NUTTX_TOUCHSCREEN */ + +#endif /* LV_USE_NUTTX*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LV_NUTTX_TOUCHSCREEN_H */ diff --git a/include/liblvgl/drivers/qnx/lv_qnx.h b/include/liblvgl/drivers/qnx/lv_qnx.h new file mode 100644 index 00000000..bb8f9409 --- /dev/null +++ b/include/liblvgl/drivers/qnx/lv_qnx.h @@ -0,0 +1,86 @@ +/** + * @file lv_qnx.h + * @brief LVGL driver for the QNX Screen compositing window manager + */ + +#ifndef LV_QNX_H +#define LV_QNX_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../display/lv_display.h" +#include "../../indev/lv_indev.h" + +#if LV_USE_QNX + +#include +#include + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create a window to use as a display for LVGL. + * @param hor_res The horizontal resolution (size) of the window + * @param ver_res The vertical resolution (size) of the window + * @return A pointer to a new display object if successful, NULL otherwise + */ +lv_display_t * lv_qnx_window_create(int32_t hor_res, int32_t ver_res); + +/** + * Set the title of the window identified by the given display. + * @param disp The display object for the window + * @param title The new title to set + */ +void lv_qnx_window_set_title(lv_display_t * disp, const char * title); + +/** + * Create a pointer input device for the display. + * Only one pointer object is currently supported. + * @param disp The display object associated with the device + * @return true if successful, false otherwise + */ +bool lv_qnx_add_pointer_device(lv_display_t * disp); + +/** + * Create a keyboard input device for the display. + * Only one keyboard object is currently supported. + * @param disp The display object associated with the device + * @return true if successful, false otherwise + */ +bool lv_qnx_add_keyboard_device(lv_display_t * disp); + +/** + * Runs the event loop for the display. + * The function only returns in response to a close event. + * @param disp The display for the event loop + * @return Exit code + */ +int lv_qnx_event_loop(lv_display_t * disp); + +/********************** + * MACROS + **********************/ + +#endif /* LV_DRV_QNX */ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LV_QNX_H */ diff --git a/include/liblvgl/drivers/sdl/lv_sdl_keyboard.h b/include/liblvgl/drivers/sdl/lv_sdl_keyboard.h new file mode 100644 index 00000000..a18b09d8 --- /dev/null +++ b/include/liblvgl/drivers/sdl/lv_sdl_keyboard.h @@ -0,0 +1,46 @@ +/** + * @file lv_sdl_keyboard.h + * + */ + +#ifndef LV_SDL_KEYBOARD_H +#define LV_SDL_KEYBOARD_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "lv_sdl_window.h" +#if LV_USE_SDL + +/********************* + * DEFINES + *********************/ +#ifndef KEYBOARD_BUFFER_SIZE +#define KEYBOARD_BUFFER_SIZE 32 +#endif + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +lv_indev_t * lv_sdl_keyboard_create(void); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_SDL*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LV_SDL_KEYBOARD_H */ diff --git a/include/liblvgl/drivers/sdl/lv_sdl_mouse.h b/include/liblvgl/drivers/sdl/lv_sdl_mouse.h new file mode 100644 index 00000000..ef60685f --- /dev/null +++ b/include/liblvgl/drivers/sdl/lv_sdl_mouse.h @@ -0,0 +1,43 @@ +/** + * @file lv_sdl_mouse.h + * + */ + +#ifndef LV_SDL_MOUSE_H +#define LV_SDL_MOUSE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "lv_sdl_window.h" +#if LV_USE_SDL + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +lv_indev_t * lv_sdl_mouse_create(void); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_SDL*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LV_SDL_MOUSE_H */ diff --git a/include/liblvgl/drivers/sdl/lv_sdl_mousewheel.h b/include/liblvgl/drivers/sdl/lv_sdl_mousewheel.h new file mode 100644 index 00000000..7ca34bdb --- /dev/null +++ b/include/liblvgl/drivers/sdl/lv_sdl_mousewheel.h @@ -0,0 +1,43 @@ +/** + * @file lv_sdl_mousewheel.h + * + */ + +#ifndef LV_SDL_MOUSEWHEEL_H +#define LV_SDL_MOUSEWHEEL_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "lv_sdl_window.h" +#if LV_USE_SDL && LV_SDL_MOUSEWHEEL_MODE == LV_SDL_MOUSEWHEEL_MODE_ENCODER + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +lv_indev_t * lv_sdl_mousewheel_create(void); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_SDL*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LV_DEV_SDL_MOUSEWHEEL_H */ diff --git a/include/liblvgl/drivers/sdl/lv_sdl_private.h b/include/liblvgl/drivers/sdl/lv_sdl_private.h new file mode 100644 index 00000000..1fd519bf --- /dev/null +++ b/include/liblvgl/drivers/sdl/lv_sdl_private.h @@ -0,0 +1,49 @@ +/** + * @file lv_sdl_private.h + * + */ + +#ifndef LV_SDL_PRIVATE_H +#define LV_SDL_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../misc/lv_types.h" + +#if LV_USE_SDL + +#include LV_SDL_INCLUDE_PATH + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void lv_sdl_keyboard_handler(SDL_Event * event); +void lv_sdl_mouse_handler(SDL_Event * event); +void lv_sdl_mousewheel_handler(SDL_Event * event); +lv_display_t * lv_sdl_get_disp_from_win_id(uint32_t win_id); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_SDL*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LV_SDL_PRIVATE_H */ diff --git a/include/liblvgl/drivers/sdl/lv_sdl_window.h b/include/liblvgl/drivers/sdl/lv_sdl_window.h new file mode 100644 index 00000000..f2e243cd --- /dev/null +++ b/include/liblvgl/drivers/sdl/lv_sdl_window.h @@ -0,0 +1,62 @@ +/** + * @file lv_sdl_window.h + * + */ + +#ifndef LV_SDL_WINDOW_H +#define LV_SDL_WINDOW_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../display/lv_display.h" +#include "../../indev/lv_indev.h" + +#if LV_USE_SDL + +/********************* + * DEFINES + *********************/ + +/* Possible values of LV_SDL_MOUSEWHEEL_MODE */ +#define LV_SDL_MOUSEWHEEL_MODE_ENCODER 0 /* The mousewheel emulates an encoder input device*/ +#define LV_SDL_MOUSEWHEEL_MODE_CROWN 1 /* The mousewheel emulates a smart watch crown*/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +lv_display_t * lv_sdl_window_create(int32_t hor_res, int32_t ver_res); + +void lv_sdl_window_set_resizeable(lv_display_t * disp, bool value); + +void lv_sdl_window_set_zoom(lv_display_t * disp, uint8_t zoom); + +uint8_t lv_sdl_window_get_zoom(lv_display_t * disp); + +void lv_sdl_window_set_title(lv_display_t * disp, const char * title); + +void * lv_sdl_window_get_renderer(lv_display_t * disp); + +void lv_sdl_quit(void); + +/********************** + * MACROS + **********************/ + +#endif /* LV_DRV_SDL */ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LV_SDL_WINDOW_H */ diff --git a/include/liblvgl/drivers/wayland/lv_wayland.h b/include/liblvgl/drivers/wayland/lv_wayland.h new file mode 100644 index 00000000..f3d340b0 --- /dev/null +++ b/include/liblvgl/drivers/wayland/lv_wayland.h @@ -0,0 +1,142 @@ +/******************************************************************* + * + * @file lv_wayland.h - Public functions of the LVGL Wayland client + * + * Based on the original file from the repository. + * + * Porting to LVGL 9.1 + * 2024 EDGEMTech Ltd. + * + * See LICENCE.txt for details + * + * Author(s): EDGEMTech Ltd, Erik Tagirov (erik.tagirov@edgemtech.ch) + * + ******************************************************************/ +#ifndef LV_WAYLAND_H +#define LV_WAYLAND_H + +#ifndef _WIN32 + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../display/lv_display.h" +#include "../../indev/lv_indev.h" + +#if LV_USE_WAYLAND + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef bool (*lv_wayland_display_close_f_t)(lv_display_t * disp); + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Retrieves the file descriptor of the wayland socket + */ +int lv_wayland_get_fd(void); + +/** + * Creates a window + * @param hor_res The width of the window in pixels + * @param ver_res The height of the window in pixels + * @param title The title of the window + * @param close_cb The callback that will be execute when the user closes the window + * @return The LVGL display associated to the window + */ +lv_display_t * lv_wayland_window_create(uint32_t hor_res, uint32_t ver_res, char * title, + lv_wayland_display_close_f_t close_cb); + +/** + * Closes the window programmatically + * @param disp Reference to the LVGL display associated to the window + */ +void lv_wayland_window_close(lv_display_t * disp); + +/** + * Check if the window is open + * @param disp Reference to the LVGL display associated to the window + * @return true: The window is open + */ +bool lv_wayland_window_is_open(lv_display_t * disp); + +/** + * Sets the fullscreen state of the window + * @param disp Reference to the LVGL display associated to the window + * @param fullscreen If true the window enters fullscreen + */ +void lv_wayland_window_set_fullscreen(lv_display_t * disp, bool fullscreen); + +/** + * Sets the maximized state of the window + * @param disp Reference to the LVGL display associated to the window + * @param fullscreen If true the window is maximized + */ +void lv_wayland_window_set_maximized(lv_display_t * disp, bool maximize); + +/** + * Obtains the input device of the mouse pointer + * @note It is used to create an input group on application start + * @param disp Reference to the LVGL display associated to the window + * @return The input device + */ +lv_indev_t * lv_wayland_get_pointer(lv_display_t * disp); + +/** + * Obtains the input device of the encoder + * @note It is used to create an input group on application start + * @param disp Reference to the LVGL display associated to the window + * @return The input device + */ +lv_indev_t * lv_wayland_get_pointeraxis(lv_display_t * disp); + +/** + * Obtains the input device of the keyboard + * @note It is used to create an input group on application start + * @param disp Reference to the LVGL display associated to the window + * @return The input device + */ +lv_indev_t * lv_wayland_get_keyboard(lv_display_t * disp); + +/** + * Obtains the input device of the touch screen + * @note It is used to create an input group on application start + * @param disp Reference to the LVGL display associated to the window + * @return The input device + */ +lv_indev_t * lv_wayland_get_touchscreen(lv_display_t * disp); + +/** + * Wrapper around lv_timer_handler + * @note Must be called in the application run loop instead of the + * regular lv_timer_handler provided by LVGL + * @return true: if the cycle was completed, false if the application + * went to sleep because the last frame wasn't completed + */ +bool lv_wayland_timer_handler(void); + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_WAYLAND */ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* _WIN32 */ +#endif /* WAYLAND_H */ diff --git a/include/liblvgl/drivers/wayland/lv_wayland_smm.h b/include/liblvgl/drivers/wayland/lv_wayland_smm.h new file mode 100644 index 00000000..617244f1 --- /dev/null +++ b/include/liblvgl/drivers/wayland/lv_wayland_smm.h @@ -0,0 +1,105 @@ +/** + * @file lv_wayland_smm.h + * + */ +#ifndef LV_WAYLAND_SMM_H +#define LV_WAYLAND_SMM_H + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _WIN32 + +/********************* + * INCLUDES + *********************/ + +#include "../../display/lv_display.h" +#include LV_STDDEF_INCLUDE +#include LV_STDBOOL_INCLUDE + +#if LV_USE_WAYLAND + +/********************* + * DEFINES + *********************/ + +#define SMM_FD_NAME "lvgl-wayland" +#define SMM_POOL_TAGS (1) +#define SMM_BUFFER_TAGS (2) +#define SMM_GROUP_TAGS (1) + +/********************** + * TYPEDEFS + **********************/ + +typedef void smm_pool_t; +typedef void smm_buffer_t; +typedef void smm_group_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +struct smm_events { + void * ctx; + bool (*new_pool)(void * ctx, smm_pool_t * pool); + void (*expand_pool)(void * ctx, smm_pool_t * pool); + void (*free_pool)(void * ctx, smm_pool_t * pool); + bool (*new_buffer)(void * ctx, smm_buffer_t * buf); + bool (*init_buffer)(void * ctx, smm_buffer_t * buf); + void (*free_buffer)(void * ctx, smm_buffer_t * buf); +}; + +struct smm_pool_properties { + void * tag[SMM_POOL_TAGS]; + size_t size; + int fd; +}; + +struct smm_buffer_properties { + void * tag[SMM_BUFFER_TAGS]; + smm_group_t * const group; + smm_pool_t * const pool; + size_t offset; +}; + +struct smm_group_properties { + void * tag[SMM_GROUP_TAGS]; +}; + +void smm_init(struct smm_events * evs); +void smm_setctx(void * ctx); +void smm_deinit(void); +smm_group_t * smm_create(void); +void smm_resize(smm_group_t * grp, size_t sz); +void smm_destroy(smm_group_t * grp); +smm_buffer_t * smm_acquire(smm_group_t * grp); +void * smm_map(smm_buffer_t * buf); +void smm_release(smm_buffer_t * buf); +smm_buffer_t * smm_latest(smm_group_t * grp); +smm_buffer_t * smm_next(smm_buffer_t * buf); + +/********************** + * MACROS + **********************/ + +#define SMM_POOL_PROPERTIES(p) ((const struct smm_pool_properties *)(p)) +#define SMM_BUFFER_PROPERTIES(b) ((const struct smm_buffer_properties *)(b)) +#define SMM_GROUP_PROPERTIES(g) ((const struct smm_group_properties *)(g)) +#define SMM_TAG(o, n, v) \ + do { \ + void **smm_tag = (void **)((char *)o + (n * sizeof(void *))); \ + *smm_tag = (v); \ + } while(0) + + +#endif /* LV_USE_WAYLAND */ +#endif /* _WIN32 */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /* LV_WAYLAND_SMM_H */ diff --git a/include/liblvgl/drivers/windows/lv_windows_context.h b/include/liblvgl/drivers/windows/lv_windows_context.h new file mode 100644 index 00000000..d080ca56 --- /dev/null +++ b/include/liblvgl/drivers/windows/lv_windows_context.h @@ -0,0 +1,134 @@ +/** + * @file lv_windows_context.h + * + */ + +#ifndef LV_WINDOWS_CONTEXT_H +#define LV_WINDOWS_CONTEXT_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../display/lv_display.h" +#include "../../indev/lv_indev.h" + +#if LV_USE_WINDOWS + +#if LV_USE_OS != LV_OS_WINDOWS +#error [lv_windows] LV_OS_WINDOWS is required. Enable it in lv_conf.h (LV_USE_OS LV_OS_WINDOWS) +#endif + +#include + +#ifndef CREATE_WAITABLE_TIMER_MANUAL_RESET +#define CREATE_WAITABLE_TIMER_MANUAL_RESET 0x00000001 +#endif + +#ifndef CREATE_WAITABLE_TIMER_HIGH_RESOLUTION +#define CREATE_WAITABLE_TIMER_HIGH_RESOLUTION 0x00000002 +#endif + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef struct _lv_windows_pointer_context_t { + lv_indev_state_t state; + lv_point_t point; + lv_indev_t * indev; +} lv_windows_pointer_context_t; + +typedef struct _lv_windows_keypad_queue_item_t { + uint32_t key; + lv_indev_state_t state; +} lv_windows_keypad_queue_item_t; + +typedef struct _lv_windows_keypad_context_t { + lv_ll_t queue; + uint16_t utf16_high_surrogate; + uint16_t utf16_low_surrogate; + lv_indev_t * indev; +} lv_windows_keypad_context_t; + +typedef struct _lv_windows_encoder_context_t { + lv_indev_state_t state; + int16_t enc_diff; + lv_indev_t * indev; +} lv_windows_encoder_context_t; + +typedef struct _lv_windows_window_context_t { + lv_display_t * display_device_object; + lv_timer_t * display_timer_object; + + int32_t window_dpi; + int32_t zoom_level; + bool allow_dpi_override; + bool simulator_mode; + bool display_resolution_changed; + lv_point_t requested_display_resolution; + + HDC display_framebuffer_context_handle; + uint32_t * display_framebuffer_base; + size_t display_framebuffer_size; + + lv_windows_pointer_context_t pointer; + lv_windows_keypad_context_t keypad; + lv_windows_encoder_context_t encoder; + +} lv_windows_window_context_t; + +typedef struct _lv_windows_create_display_data_t { + const wchar_t * title; + int32_t hor_res; + int32_t ver_res; + int32_t zoom_level; + bool allow_dpi_override; + bool simulator_mode; + HANDLE mutex; + lv_display_t * display; +} lv_windows_create_display_data_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * @brief Initialize the LVGL Windows backend. + * @remark This is a private API which is used for LVGL Windows backend + * implementation. LVGL users shouldn't use that because the + * LVGL has already used it in lv_init. +*/ +void lv_windows_platform_init(void); + +/** + * @brief Get the window context from specific LVGL display window. + * @param window_handle The window handle of specific LVGL display window. + * @return The window context from specific LVGL display window. + * @remark This is a private API which is used for LVGL Windows backend + * implementation. LVGL users shouldn't use that because the + * maintainer doesn't promise the application binary interface + * compatibility for this API. +*/ +lv_windows_window_context_t * lv_windows_get_window_context( + HWND window_handle); + +/********************** + * MACROS + **********************/ + +#endif // LV_USE_WINDOWS + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_WINDOWS_CONTEXT_H*/ diff --git a/include/liblvgl/drivers/windows/lv_windows_display.h b/include/liblvgl/drivers/windows/lv_windows_display.h new file mode 100644 index 00000000..b9c6f8e9 --- /dev/null +++ b/include/liblvgl/drivers/windows/lv_windows_display.h @@ -0,0 +1,127 @@ +/** + * @file lv_windows_display.h + * + */ + +#ifndef LV_WINDOWS_DISPLAY_H +#define LV_WINDOWS_DISPLAY_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../display/lv_display.h" +#include "../../indev/lv_indev.h" + +#if LV_USE_WINDOWS + +#include + +/********************* + * DEFINES + *********************/ + +#define LV_WINDOWS_ZOOM_BASE_LEVEL 100 + +#ifndef USER_DEFAULT_SCREEN_DPI +#define USER_DEFAULT_SCREEN_DPI 96 +#endif + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * @brief Create a LVGL display object. + * @param title The window title of LVGL display. + * @param hor_res The horizontal resolution value of LVGL display. + * @param ver_res The vertical resolution value of LVGL display. + * @param zoom_level The zoom level value. Base value is 100 a.k.a 100%. + * @param allow_dpi_override Allow DPI override if true, or follow the + * Windows DPI scaling setting dynamically. + * @param simulator_mode Create simulator mode display if true, or create + * application mode display. + * @return The created LVGL display object. +*/ +lv_display_t * lv_windows_create_display( + const wchar_t * title, + int32_t hor_res, + int32_t ver_res, + int32_t zoom_level, + bool allow_dpi_override, + bool simulator_mode); + +/** + * @brief Get the window handle from specific LVGL display object. + * @param display The specific LVGL display object. + * @return The window handle from specific LVGL display object. +*/ +HWND lv_windows_get_display_window_handle(lv_display_t * display); + +/** + * @brief Get logical pixel value from physical pixel value taken account + * with zoom level. + * @param physical The physical pixel value taken account with zoom level. + * @param zoom_level The zoom level value. Base value is 100 a.k.a 100%. + * @return The logical pixel value. + * @remark It uses the same calculation style as Windows OS implementation. + * It will be useful for integrate LVGL Windows backend to other + * Windows applications. +*/ +int32_t lv_windows_zoom_to_logical(int32_t physical, int32_t zoom_level); + +/** + * @brief Get physical pixel value taken account with zoom level from + * logical pixel value. + * @param logical The logical pixel value. + * @param zoom_level The zoom level value. Base value is 100 a.k.a 100%. + * @return The physical pixel value taken account with zoom level. + * @remark It uses the same calculation style as Windows OS implementation. + * It will be useful for integrate LVGL Windows backend to other + * Windows applications. +*/ +int32_t lv_windows_zoom_to_physical(int32_t logical, int32_t zoom_level); + +/** + * @brief Get logical pixel value from physical pixel value taken account + * with DPI scaling. + * @param physical The physical pixel value taken account with DPI scaling. + * @param dpi The DPI scaling value. Base value is USER_DEFAULT_SCREEN_DPI. + * @return The logical pixel value. + * @remark It uses the same calculation style as Windows OS implementation. + * It will be useful for integrate LVGL Windows backend to other + * Windows applications. +*/ +int32_t lv_windows_dpi_to_logical(int32_t physical, int32_t dpi); + +/** + * @brief Get physical pixel value taken account with DPI scaling from + * logical pixel value. + * @param logical The logical pixel value. + * @param dpi The DPI scaling value. Base value is USER_DEFAULT_SCREEN_DPI. + * @return The physical pixel value taken account with DPI scaling. + * @remark It uses the same calculation style as Windows OS implementation. + * It will be useful for integrate LVGL Windows backend to other + * Windows applications. +*/ +int32_t lv_windows_dpi_to_physical(int32_t logical, int32_t dpi); + +/********************** + * MACROS + **********************/ + +#endif // LV_USE_WINDOWS + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_WINDOWS_DISPLAY_H*/ diff --git a/include/liblvgl/drivers/windows/lv_windows_input.h b/include/liblvgl/drivers/windows/lv_windows_input.h new file mode 100644 index 00000000..892014ea --- /dev/null +++ b/include/liblvgl/drivers/windows/lv_windows_input.h @@ -0,0 +1,83 @@ +/** + * @file lv_windows_input.h + * + */ + +#ifndef LV_WINDOWS_INPUT_H +#define LV_WINDOWS_INPUT_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../display/lv_display.h" +#include "../../indev/lv_indev.h" + +#if LV_USE_WINDOWS + +#include + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * @brief Get the window handle from specific LVGL input device object. + * @param indev The specific LVGL input device object. + * @return The window handle from specific LVGL input device object. +*/ +HWND lv_windows_get_indev_window_handle(lv_indev_t * indev); + +/** + * @brief Open a LVGL pointer input device object for the specific LVGL + * display object, or create it if the LVGL pointer input device + * object is not created or removed before. + * @param display The specific LVGL display object. + * @return The LVGL pointer input device object for the specific LVGL + * display object. +*/ +lv_indev_t * lv_windows_acquire_pointer_indev(lv_display_t * display); + +/** + * @brief Open a LVGL keypad input device object for the specific LVGL + * display object, or create it if the LVGL keypad input device + * object is not created or removed before. + * @param display The specific LVGL display object. + * @return The LVGL keypad input device object for the specific LVGL + * display object. +*/ +lv_indev_t * lv_windows_acquire_keypad_indev(lv_display_t * display); + +/** + * @brief Open a LVGL encoder input device object for the specific LVGL + * display object, or create it if the LVGL encoder input device + * object is not created or removed before. + * @param display The specific LVGL display object. + * @return The LVGL encoder input device object for the specific LVGL + * display object. +*/ +lv_indev_t * lv_windows_acquire_encoder_indev(lv_display_t * display); + +/********************** + * MACROS + **********************/ + +#endif // LV_USE_WINDOWS + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_WINDOWS_INPUT_H*/ diff --git a/include/liblvgl/drivers/windows/lv_windows_input_private.h b/include/liblvgl/drivers/windows/lv_windows_input_private.h new file mode 100644 index 00000000..7d1096ed --- /dev/null +++ b/include/liblvgl/drivers/windows/lv_windows_input_private.h @@ -0,0 +1,65 @@ +/** + * @file lv_windows_input_private.h + * + */ + +#ifndef LV_WINDOWS_INPUT_PRIVATE_H +#define LV_WINDOWS_INPUT_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#if LV_USE_WINDOWS + +#include +#include + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +bool lv_windows_pointer_device_window_message_handler( + HWND hWnd, + UINT uMsg, + WPARAM wParam, + LPARAM lParam, + LRESULT * plResult); + +bool lv_windows_keypad_device_window_message_handler( + HWND hWnd, + UINT uMsg, + WPARAM wParam, + LPARAM lParam, + LRESULT * plResult); + +bool lv_windows_encoder_device_window_message_handler( + HWND hWnd, + UINT uMsg, + WPARAM wParam, + LPARAM lParam, + LRESULT * plResult); + +/********************** + * MACROS + **********************/ + +#endif // LV_USE_WINDOWS + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_WINDOWS_INPUT_PRIVATE_H*/ diff --git a/include/liblvgl/drivers/x11/lv_x11.h b/include/liblvgl/drivers/x11/lv_x11.h new file mode 100644 index 00000000..7daabe3e --- /dev/null +++ b/include/liblvgl/drivers/x11/lv_x11.h @@ -0,0 +1,81 @@ +/** + * @file lv_x11.h + * + */ + +#ifndef LV_X11_H +#define LV_X11_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../display/lv_display.h" +#include "../../indev/lv_indev.h" + +#if LV_USE_X11 + +/********************* + * DEFINES + *********************/ + +/** Header of private display driver user data - for internal use only */ +typedef struct { + struct _XDisplay * display; /**< X11 display object */ + struct _x11_inp_data * inp_data; /**< input user data object */ +} _x11_user_hdr_t; + +/** optional window close callback function type + * @see lv_x11_window_set_close_cb +*/ +typedef void(*lv_x11_close_cb)(void * user_data); + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * create and add keyboard, mouse and scrollwheel objects and connect them to x11 display. + * + * This is a convenience method handling the typical input initialisation of an X11 window: + * - create keyboard (lv_x11_keyboard_create) + * - create mouse (with scrollwheel, lv_x11_mouse_create lv_x11_mousewheel_create) + * + * @param[in] disp the created X11 display object from @ref lv_x11_window_create + * @param[in] mouse_img optional image description for the mouse cursor (NULL for no/invisible mouse cursor) + */ +void lv_x11_inputs_create(lv_display_t * disp, lv_image_dsc_t const * mouse_img); + +/** + * create the X11 display + * + * The minimal initialisation for initializing the X11 display driver with keyboard/mouse support: + * @code + * lv_display_t* disp = lv_x11_window_create("My Window Title", window_width, window_width); + * lv_x11_inputs_create(disp, NULL); + * @endcode + * or with mouse cursor icon: + * @code + * lv_image_dsc_t mouse_symbol = {.....}; + * lv_display_t* disp = lv_x11_window_create("My Window Title", window_width, window_width); + * lv_x11_inputs_create(disp, &mouse_symbol); + * @endcode + * + * @param[in] title title of the created X11 window + * @param[in] hor_res horizontal resolution (=width) of the X11 window + * @param[in] ver_res vertical resolution (=height) of the X11 window + * @return pointer to the display object + */ +lv_display_t * lv_x11_window_create(char const * title, int32_t hor_res, int32_t ver_res); + +#endif /* LV_USE_X11 */ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LV_X11_H */ diff --git a/include/liblvgl/extra/layouts/flex/lv_flex.h b/include/liblvgl/extra/layouts/flex/lv_flex.h deleted file mode 100644 index d9619610..00000000 --- a/include/liblvgl/extra/layouts/flex/lv_flex.h +++ /dev/null @@ -1,152 +0,0 @@ -/** - * @file lv_flex.h - * - */ - -#ifndef LV_FLEX_H -#define LV_FLEX_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/core/lv_obj.h" -#if LV_USE_FLEX - -/********************* - * DEFINES - *********************/ - -#define LV_OBJ_FLAG_FLEX_IN_NEW_TRACK LV_OBJ_FLAG_LAYOUT_1 -LV_EXPORT_CONST_INT(LV_OBJ_FLAG_FLEX_IN_NEW_TRACK); - -#define _LV_FLEX_COLUMN (1 << 0) -#define _LV_FLEX_WRAP (1 << 2) -#define _LV_FLEX_REVERSE (1 << 3) - -/********************** - * TYPEDEFS - **********************/ - -/*Can't include lv_obj.h because it includes this header file*/ -struct _lv_obj_t; - -typedef enum { - LV_FLEX_ALIGN_START, - LV_FLEX_ALIGN_END, - LV_FLEX_ALIGN_CENTER, - LV_FLEX_ALIGN_SPACE_EVENLY, - LV_FLEX_ALIGN_SPACE_AROUND, - LV_FLEX_ALIGN_SPACE_BETWEEN, -} lv_flex_align_t; - -typedef enum { - LV_FLEX_FLOW_ROW = 0x00, - LV_FLEX_FLOW_COLUMN = _LV_FLEX_COLUMN, - LV_FLEX_FLOW_ROW_WRAP = LV_FLEX_FLOW_ROW | _LV_FLEX_WRAP, - LV_FLEX_FLOW_ROW_REVERSE = LV_FLEX_FLOW_ROW | _LV_FLEX_REVERSE, - LV_FLEX_FLOW_ROW_WRAP_REVERSE = LV_FLEX_FLOW_ROW | _LV_FLEX_WRAP | _LV_FLEX_REVERSE, - LV_FLEX_FLOW_COLUMN_WRAP = LV_FLEX_FLOW_COLUMN | _LV_FLEX_WRAP, - LV_FLEX_FLOW_COLUMN_REVERSE = LV_FLEX_FLOW_COLUMN | _LV_FLEX_REVERSE, - LV_FLEX_FLOW_COLUMN_WRAP_REVERSE = LV_FLEX_FLOW_COLUMN | _LV_FLEX_WRAP | _LV_FLEX_REVERSE, -} lv_flex_flow_t; - -/********************** - * GLOBAL VARIABLES - **********************/ -extern uint16_t LV_LAYOUT_FLEX; -extern lv_style_prop_t LV_STYLE_FLEX_FLOW; -extern lv_style_prop_t LV_STYLE_FLEX_MAIN_PLACE; -extern lv_style_prop_t LV_STYLE_FLEX_CROSS_PLACE; -extern lv_style_prop_t LV_STYLE_FLEX_TRACK_PLACE; -extern lv_style_prop_t LV_STYLE_FLEX_GROW; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Initialize a flex layout the default values - * @param flex pointer to a flex layout descriptor - */ -void lv_flex_init(void); - -/** - * Set hot the item should flow - * @param flex pointer to a flex layout descriptor - * @param flow an element of `lv_flex_flow_t`. - */ -void lv_obj_set_flex_flow(lv_obj_t * obj, lv_flex_flow_t flow); - -/** - * Set how to place (where to align) the items and tracks - * @param flex pointer: to a flex layout descriptor - * @param main_place where to place the items on main axis (in their track). Any value of `lv_flex_align_t`. - * @param cross_place where to place the item in their track on the cross axis. `LV_FLEX_ALIGN_START/END/CENTER` - * @param track_place where to place the tracks in the cross direction. Any value of `lv_flex_align_t`. - */ -void lv_obj_set_flex_align(lv_obj_t * obj, lv_flex_align_t main_place, lv_flex_align_t cross_place, - lv_flex_align_t track_cross_place); - -/** - * Sets the width or height (on main axis) to grow the object in order fill the free space - * @param obj pointer to an object. The parent must have flex layout else nothing will happen. - * @param grow a value to set how much free space to take proportionally to other growing items. - */ -void lv_obj_set_flex_grow(lv_obj_t * obj, uint8_t grow); - -void lv_style_set_flex_flow(lv_style_t * style, lv_flex_flow_t value); -void lv_style_set_flex_main_place(lv_style_t * style, lv_flex_align_t value); -void lv_style_set_flex_cross_place(lv_style_t * style, lv_flex_align_t value); -void lv_style_set_flex_track_place(lv_style_t * style, lv_flex_align_t value); -void lv_style_set_flex_grow(lv_style_t * style, uint8_t value); -void lv_obj_set_style_flex_flow(lv_obj_t * obj, lv_flex_flow_t value, lv_style_selector_t selector); -void lv_obj_set_style_flex_main_place(lv_obj_t * obj, lv_flex_align_t value, lv_style_selector_t selector); -void lv_obj_set_style_flex_cross_place(lv_obj_t * obj, lv_flex_align_t value, lv_style_selector_t selector); -void lv_obj_set_style_flex_track_place(lv_obj_t * obj, lv_flex_align_t value, lv_style_selector_t selector); -void lv_obj_set_style_flex_grow(lv_obj_t * obj, uint8_t value, lv_style_selector_t selector); - -static inline lv_flex_flow_t lv_obj_get_style_flex_flow(const lv_obj_t * obj, uint32_t part) -{ - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_FLEX_FLOW); - return (lv_flex_flow_t)v.num; -} - -static inline lv_flex_align_t lv_obj_get_style_flex_main_place(const lv_obj_t * obj, uint32_t part) -{ - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_FLEX_MAIN_PLACE); - return (lv_flex_align_t)v.num; -} - -static inline lv_flex_align_t lv_obj_get_style_flex_cross_place(const lv_obj_t * obj, uint32_t part) -{ - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_FLEX_CROSS_PLACE); - return (lv_flex_align_t)v.num; -} - -static inline lv_flex_align_t lv_obj_get_style_flex_track_place(const lv_obj_t * obj, uint32_t part) -{ - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_FLEX_TRACK_PLACE); - return (lv_flex_align_t)v.num; -} - -static inline uint8_t lv_obj_get_style_flex_grow(const lv_obj_t * obj, uint32_t part) -{ - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_FLEX_GROW); - return (uint8_t)v.num; -} - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_FLEX*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_FLEX_H*/ diff --git a/include/liblvgl/extra/layouts/grid/lv_grid.h b/include/liblvgl/extra/layouts/grid/lv_grid.h deleted file mode 100644 index 8e57fd9c..00000000 --- a/include/liblvgl/extra/layouts/grid/lv_grid.h +++ /dev/null @@ -1,194 +0,0 @@ -/** - * @file lv_grid.h - * - */ - -#ifndef LV_GRID_H -#define LV_GRID_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/core/lv_obj.h" -#if LV_USE_GRID - -/********************* - * DEFINES - *********************/ -/** - * Can be used track size to make the track fill the free space. - * @param x how much space to take proportionally to other FR tracks - * @return a special track size - */ -#define LV_GRID_FR(x) (LV_COORD_MAX - 100 + x) - -#define LV_GRID_CONTENT (LV_COORD_MAX - 101) -LV_EXPORT_CONST_INT(LV_GRID_CONTENT); - -#define LV_GRID_TEMPLATE_LAST (LV_COORD_MAX) -LV_EXPORT_CONST_INT(LV_GRID_TEMPLATE_LAST); - -/********************** - * TYPEDEFS - **********************/ - -/*Can't include lv_obj.h because it includes this header file*/ -struct _lv_obj_t; - -typedef enum { - LV_GRID_ALIGN_START, - LV_GRID_ALIGN_CENTER, - LV_GRID_ALIGN_END, - LV_GRID_ALIGN_STRETCH, - LV_GRID_ALIGN_SPACE_EVENLY, - LV_GRID_ALIGN_SPACE_AROUND, - LV_GRID_ALIGN_SPACE_BETWEEN, -} lv_grid_align_t; - -/********************** - * GLOBAL VARIABLES - **********************/ - -extern uint16_t LV_LAYOUT_GRID; -extern lv_style_prop_t LV_STYLE_GRID_COLUMN_DSC_ARRAY; -extern lv_style_prop_t LV_STYLE_GRID_COLUMN_ALIGN; -extern lv_style_prop_t LV_STYLE_GRID_ROW_DSC_ARRAY; -extern lv_style_prop_t LV_STYLE_GRID_ROW_ALIGN; -extern lv_style_prop_t LV_STYLE_GRID_CELL_COLUMN_POS; -extern lv_style_prop_t LV_STYLE_GRID_CELL_COLUMN_SPAN; -extern lv_style_prop_t LV_STYLE_GRID_CELL_X_ALIGN; -extern lv_style_prop_t LV_STYLE_GRID_CELL_ROW_POS; -extern lv_style_prop_t LV_STYLE_GRID_CELL_ROW_SPAN; -extern lv_style_prop_t LV_STYLE_GRID_CELL_Y_ALIGN; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -void lv_grid_init(void); - -void lv_obj_set_grid_dsc_array(lv_obj_t * obj, const lv_coord_t col_dsc[], const lv_coord_t row_dsc[]); - -void lv_obj_set_grid_align(lv_obj_t * obj, lv_grid_align_t column_align, lv_grid_align_t row_align); - -/** - * Set the cell of an object. The object's parent needs to have grid layout, else nothing will happen - * @param obj pointer to an object - * @param column_align the vertical alignment in the cell. `LV_GRID_START/END/CENTER/STRETCH` - * @param col_pos column ID - * @param col_span number of columns to take (>= 1) - * @param row_align the horizontal alignment in the cell. `LV_GRID_START/END/CENTER/STRETCH` - * @param row_pos row ID - * @param row_span number of rows to take (>= 1) - */ -void lv_obj_set_grid_cell(lv_obj_t * obj, lv_grid_align_t column_align, uint8_t col_pos, uint8_t col_span, - lv_grid_align_t row_align, uint8_t row_pos, uint8_t row_span); - -/** - * Just a wrapper to `LV_GRID_FR` for bindings. - */ -static inline lv_coord_t lv_grid_fr(uint8_t x) -{ - return LV_GRID_FR(x); -} - -void lv_style_set_grid_row_dsc_array(lv_style_t * style, const lv_coord_t value[]); -void lv_style_set_grid_column_dsc_array(lv_style_t * style, const lv_coord_t value[]); -void lv_style_set_grid_row_align(lv_style_t * style, lv_grid_align_t value); -void lv_style_set_grid_column_align(lv_style_t * style, lv_grid_align_t value); -void lv_style_set_grid_cell_column_pos(lv_style_t * style, lv_coord_t value); -void lv_style_set_grid_cell_column_span(lv_style_t * style, lv_coord_t value); -void lv_style_set_grid_cell_row_pos(lv_style_t * style, lv_coord_t value); -void lv_style_set_grid_cell_row_span(lv_style_t * style, lv_coord_t value); -void lv_style_set_grid_cell_x_align(lv_style_t * style, lv_coord_t value); -void lv_style_set_grid_cell_y_align(lv_style_t * style, lv_coord_t value); - -void lv_obj_set_style_grid_row_dsc_array(lv_obj_t * obj, const lv_coord_t value[], lv_style_selector_t selector); -void lv_obj_set_style_grid_column_dsc_array(lv_obj_t * obj, const lv_coord_t value[], lv_style_selector_t selector); -void lv_obj_set_style_grid_row_align(lv_obj_t * obj, lv_grid_align_t value, lv_style_selector_t selector); -void lv_obj_set_style_grid_column_align(lv_obj_t * obj, lv_grid_align_t value, lv_style_selector_t selector); -void lv_obj_set_style_grid_cell_column_pos(lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_grid_cell_column_span(lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_grid_cell_row_pos(lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_grid_cell_row_span(lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_grid_cell_x_align(lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); -void lv_obj_set_style_grid_cell_y_align(lv_obj_t * obj, lv_coord_t value, lv_style_selector_t selector); - -static inline const lv_coord_t * lv_obj_get_style_grid_row_dsc_array(const lv_obj_t * obj, uint32_t part) -{ - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_GRID_ROW_DSC_ARRAY); - return (const lv_coord_t *)v.ptr; -} - -static inline const lv_coord_t * lv_obj_get_style_grid_column_dsc_array(const lv_obj_t * obj, uint32_t part) -{ - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_GRID_COLUMN_DSC_ARRAY); - return (const lv_coord_t *)v.ptr; -} - -static inline lv_grid_align_t lv_obj_get_style_grid_row_align(const lv_obj_t * obj, uint32_t part) -{ - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_GRID_ROW_ALIGN); - return (lv_grid_align_t)v.num; -} - -static inline lv_grid_align_t lv_obj_get_style_grid_column_align(const lv_obj_t * obj, uint32_t part) -{ - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_GRID_COLUMN_ALIGN); - return (lv_grid_align_t)v.num; -} - -static inline lv_coord_t lv_obj_get_style_grid_cell_column_pos(const lv_obj_t * obj, uint32_t part) -{ - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_GRID_CELL_COLUMN_POS); - return (lv_coord_t)v.num; -} - -static inline lv_coord_t lv_obj_get_style_grid_cell_column_span(const lv_obj_t * obj, uint32_t part) -{ - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_GRID_CELL_COLUMN_SPAN); - return (lv_coord_t)v.num; -} - -static inline lv_coord_t lv_obj_get_style_grid_cell_row_pos(const lv_obj_t * obj, uint32_t part) -{ - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_GRID_CELL_ROW_POS); - return (lv_coord_t)v.num; -} - -static inline lv_coord_t lv_obj_get_style_grid_cell_row_span(const lv_obj_t * obj, uint32_t part) -{ - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_GRID_CELL_ROW_SPAN); - return (lv_coord_t)v.num; -} - -static inline lv_coord_t lv_obj_get_style_grid_cell_x_align(const lv_obj_t * obj, uint32_t part) -{ - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_GRID_CELL_X_ALIGN); - return (lv_coord_t)v.num; -} - -static inline lv_coord_t lv_obj_get_style_grid_cell_y_align(const lv_obj_t * obj, uint32_t part) -{ - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_GRID_CELL_Y_ALIGN); - return (lv_coord_t)v.num; -} - -/********************** - * GLOBAL VARIABLES - **********************/ - -/********************** - * MACROS - **********************/ -#endif /*LV_USE_GRID*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_GRID_H*/ diff --git a/include/liblvgl/extra/libs/freetype/lv_freetype.h b/include/liblvgl/extra/libs/freetype/lv_freetype.h deleted file mode 100644 index e576c2e1..00000000 --- a/include/liblvgl/extra/libs/freetype/lv_freetype.h +++ /dev/null @@ -1,83 +0,0 @@ -/** - * @file lv_freetype.h - * - */ -#ifndef LV_FREETYPE_H -#define LV_FREETYPE_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lvgl.h" -#if LV_USE_FREETYPE - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ -typedef enum { - FT_FONT_STYLE_NORMAL = 0, - FT_FONT_STYLE_ITALIC = 1 << 0, - FT_FONT_STYLE_BOLD = 1 << 1 -} LV_FT_FONT_STYLE; - -typedef struct { - const char * name; /* The name of the font file */ - const void * mem; /* The pointer of the font file */ - size_t mem_size; /* The size of the memory */ - lv_font_t * font; /* point to lvgl font */ - uint16_t weight; /* font size */ - uint16_t style; /* font style */ -} lv_ft_info_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * init freetype library - * @param max_faces Maximum number of opened FT_Face objects managed by this cache instance. Use 0 for defaults. - * @param max_sizes Maximum number of opened FT_Size objects managed by this cache instance. Use 0 for defaults. - * @param max_bytes Maximum number of bytes to use for cached data nodes. Use 0 for defaults. - * Note that this value does not account for managed FT_Face and FT_Size objects. - * @return true on success, otherwise false. - */ -bool lv_freetype_init(uint16_t max_faces, uint16_t max_sizes, uint32_t max_bytes); - -/** - * Destroy freetype library - */ -void lv_freetype_destroy(void); - -/** - * Creates a font with info parameter specified. - * @param info See lv_ft_info_t for details. - * when success, lv_ft_info_t->font point to the font you created. - * @return true on success, otherwise false. - */ -bool lv_ft_font_init(lv_ft_info_t * info); - -/** - * Destroy a font that has been created. - * @param font pointer to font. - */ -void lv_ft_font_destroy(lv_font_t * font); - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_FREETYPE*/ - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#endif /* LV_FREETYPE_H */ diff --git a/include/liblvgl/extra/libs/gif/gifdec.h b/include/liblvgl/extra/libs/gif/gifdec.h deleted file mode 100644 index eaa4cadd..00000000 --- a/include/liblvgl/extra/libs/gif/gifdec.h +++ /dev/null @@ -1,60 +0,0 @@ -#ifndef GIFDEC_H -#define GIFDEC_H - -#include -#include "liblvgl/misc/lv_fs.h" - -#if LV_USE_GIF - -typedef struct gd_Palette { - int size; - uint8_t colors[0x100 * 3]; -} gd_Palette; - -typedef struct gd_GCE { - uint16_t delay; - uint8_t tindex; - uint8_t disposal; - int input; - int transparency; -} gd_GCE; - - - -typedef struct gd_GIF { - lv_fs_file_t fd; - const char * data; - uint8_t is_file; - uint32_t f_rw_p; - int32_t anim_start; - uint16_t width, height; - uint16_t depth; - uint16_t loop_count; - gd_GCE gce; - gd_Palette *palette; - gd_Palette lct, gct; - void (*plain_text)( - struct gd_GIF *gif, uint16_t tx, uint16_t ty, - uint16_t tw, uint16_t th, uint8_t cw, uint8_t ch, - uint8_t fg, uint8_t bg - ); - void (*comment)(struct gd_GIF *gif); - void (*application)(struct gd_GIF *gif, char id[8], char auth[3]); - uint16_t fx, fy, fw, fh; - uint8_t bgindex; - uint8_t *canvas, *frame; -} gd_GIF; - -gd_GIF * gd_open_gif_file(const char *fname); - -gd_GIF * gd_open_gif_data(const void *data); - -void gd_render_frame(gd_GIF *gif, uint8_t *buffer); - -int gd_get_frame(gd_GIF *gif); -void gd_rewind(gd_GIF *gif); -void gd_close_gif(gd_GIF *gif); - -#endif /*LV_USE_GIF*/ - -#endif /* GIFDEC_H */ diff --git a/include/liblvgl/extra/libs/qrcode/lv_qrcode.h b/include/liblvgl/extra/libs/qrcode/lv_qrcode.h deleted file mode 100644 index c8db4e5e..00000000 --- a/include/liblvgl/extra/libs/qrcode/lv_qrcode.h +++ /dev/null @@ -1,69 +0,0 @@ -/** - * @file lv_qrcode - * - */ - -#ifndef LV_QRCODE_H -#define LV_QRCODE_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lvgl.h" -#if LV_USE_QRCODE - -/********************* - * DEFINES - *********************/ - -extern const lv_obj_class_t lv_qrcode_class; - -/********************** - * TYPEDEFS - **********************/ - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Create an empty QR code (an `lv_canvas`) object. - * @param parent point to an object where to create the QR code - * @param size width and height of the QR code - * @param dark_color dark color of the QR code - * @param light_color light color of the QR code - * @return pointer to the created QR code object - */ -lv_obj_t * lv_qrcode_create(lv_obj_t * parent, lv_coord_t size, lv_color_t dark_color, lv_color_t light_color); - -/** - * Set the data of a QR code object - * @param qrcode pointer to aQ code object - * @param data data to display - * @param data_len length of data in bytes - * @return LV_RES_OK: if no error; LV_RES_INV: on error - */ -lv_res_t lv_qrcode_update(lv_obj_t * qrcode, const void * data, uint32_t data_len); - -/** - * DEPRECATED: Use normal lv_obj_del instead - * Delete a QR code object - * @param qrcode pointer to a QR code object - */ -void lv_qrcode_delete(lv_obj_t * qrcode); - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_QRCODE*/ - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#endif /*LV_QRCODE_H*/ diff --git a/include/liblvgl/extra/libs/sjpg/tjpgd.h b/include/liblvgl/extra/libs/sjpg/tjpgd.h deleted file mode 100644 index 866e6b32..00000000 --- a/include/liblvgl/extra/libs/sjpg/tjpgd.h +++ /dev/null @@ -1,93 +0,0 @@ -/*----------------------------------------------------------------------------/ -/ TJpgDec - Tiny JPEG Decompressor R0.03 include file (C)ChaN, 2021 -/----------------------------------------------------------------------------*/ -#ifndef DEF_TJPGDEC -#define DEF_TJPGDEC - -#ifdef __cplusplus -extern "C" { -#endif - -#include "liblvgl/lv_conf_internal.h" -#if LV_USE_SJPG - -#include "tjpgdcnf.h" -#include -#include - -#if JD_FASTDECODE >= 1 -typedef int16_t jd_yuv_t; -#else -typedef uint8_t jd_yuv_t; -#endif - - -/* Error code */ -typedef enum { - JDR_OK = 0, /* 0: Succeeded */ - JDR_INTR, /* 1: Interrupted by output function */ - JDR_INP, /* 2: Device error or wrong termination of input stream */ - JDR_MEM1, /* 3: Insufficient memory pool for the image */ - JDR_MEM2, /* 4: Insufficient stream input buffer */ - JDR_PAR, /* 5: Parameter error */ - JDR_FMT1, /* 6: Data format error (may be broken data) */ - JDR_FMT2, /* 7: Right format but not supported */ - JDR_FMT3 /* 8: Not supported JPEG standard */ -} JRESULT; - -/* Rectangular region in the output image */ -typedef struct { - uint16_t left; /* Left end */ - uint16_t right; /* Right end */ - uint16_t top; /* Top end */ - uint16_t bottom; /* Bottom end */ -} JRECT; - -/* Decompressor object structure */ -typedef struct JDEC JDEC; -struct JDEC { - size_t dctr; /* Number of bytes available in the input buffer */ - uint8_t* dptr; /* Current data read ptr */ - uint8_t* inbuf; /* Bit stream input buffer */ - uint8_t dbit; /* Number of bits availavble in wreg or reading bit mask */ - uint8_t scale; /* Output scaling ratio */ - uint8_t msx, msy; /* MCU size in unit of block (width, height) */ - uint8_t qtid[3]; /* Quantization table ID of each component, Y, Cb, Cr */ - uint8_t ncomp; /* Number of color components 1:grayscale, 3:color */ - int16_t dcv[3]; /* Previous DC element of each component */ - uint16_t nrst; /* Restart inverval */ - uint16_t width, height; /* Size of the input image (pixel) */ - uint8_t* huffbits[2][2]; /* Huffman bit distribution tables [id][dcac] */ - uint16_t* huffcode[2][2]; /* Huffman code word tables [id][dcac] */ - uint8_t* huffdata[2][2]; /* Huffman decoded data tables [id][dcac] */ - int32_t* qttbl[4]; /* Dequantizer tables [id] */ -#if JD_FASTDECODE >= 1 - uint32_t wreg; /* Working shift register */ - uint8_t marker; /* Detected marker (0:None) */ -#if JD_FASTDECODE == 2 - uint8_t longofs[2][2]; /* Table offset of long code [id][dcac] */ - uint16_t* hufflut_ac[2]; /* Fast huffman decode tables for AC short code [id] */ - uint8_t* hufflut_dc[2]; /* Fast huffman decode tables for DC short code [id] */ -#endif -#endif - void* workbuf; /* Working buffer for IDCT and RGB output */ - jd_yuv_t* mcubuf; /* Working buffer for the MCU */ - void* pool; /* Pointer to available memory pool */ - size_t sz_pool; /* Size of momory pool (bytes available) */ - size_t (*infunc)(JDEC*, uint8_t*, size_t); /* Pointer to jpeg stream input function */ - void* device; /* Pointer to I/O device identifiler for the session */ -}; - - - -/* TJpgDec API functions */ -JRESULT jd_prepare (JDEC* jd, size_t (*infunc)(JDEC*,uint8_t*,size_t), void* pool, size_t sz_pool, void* dev); -JRESULT jd_decomp (JDEC* jd, int (*outfunc)(JDEC*,void*,JRECT*), uint8_t scale); - -#endif /*LV_USE_SJPG*/ - -#ifdef __cplusplus -} -#endif - -#endif /* _TJPGDEC */ diff --git a/include/liblvgl/extra/others/msg/lv_msg.h b/include/liblvgl/extra/others/msg/lv_msg.h deleted file mode 100644 index 64dc0d45..00000000 --- a/include/liblvgl/extra/others/msg/lv_msg.h +++ /dev/null @@ -1,124 +0,0 @@ -/** - * @file lv_msg.h - * - */ - -#ifndef LV_MSG_H -#define LV_MSG_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/core/lv_obj.h" -#if LV_USE_MSG - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -typedef struct { - uint32_t id; /*Identifier of the message*/ - void * user_data; /*Set the the user_data set in `lv_msg_subscribe`*/ - void * _priv_data; /*Used internally*/ - const void * payload; /*Pointer to the data of the message*/ -} lv_msg_t; - -typedef void (*lv_msg_subscribe_cb_t)(void * s, lv_msg_t * msg); - -typedef void (*lv_msg_request_cb_t)(void * r, uint32_t msg_id); - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Called internally to initialize the message module - */ -void lv_msg_init(void); - -/** - * Subscribe to an `msg_id` - * @param msg_id the message ID to listen to - * @param cb callback to call if a message with `msg_id` was sent - * @param user_data arbitrary data which will be available in `cb` too - * @return pointer to a "subscribe object". It can be used the unsubscribe. - */ -void * lv_msg_subsribe(uint32_t msg_id, lv_msg_subscribe_cb_t cb, void * user_data); - -/** - * Subscribe an `lv_obj` to a message. - * `LV_EVENT_MSG_RECEIVED` will be triggered if a message with matching ID was sent - * @param msg_id the message ID to listen to - * @param obj pointer to an `lv_obj` - * @param user_data arbitrary data which will be available in `cb` too - * @return pointer to a "subscribe object". It can be used the unsubscribe. - */ -void * lv_msg_subsribe_obj(uint32_t msg_id, lv_obj_t * obj, void * user_data); - -/** - * Cancel a previous subscription - * @param s pointer to a "subscibe object". - * Return value of `lv_msg_subsribe` or `lv_msg_subsribe_obj` - */ -void lv_msg_unsubscribe(void * s); - -/** - * Send a message with a given ID and payload - * @param msg_id ID of the message to send - * @param data pointer to the data to send - */ -void lv_msg_send(uint32_t msg_id, const void * payload); - -/** - * Get the ID of a message object. Typically used in the subscriber callback. - * @param m pointer to a message object - * @return the ID of the message - */ -uint32_t lv_msg_get_id(lv_msg_t * m); - -/** - * Get the payload of a message object. Typically used in the subscriber callback. - * @param m pointer to a message object - * @return the payload of the message - */ -const void * lv_msg_get_payload(lv_msg_t * m); - -/** - * Get the user data of a message object. Typically used in the subscriber callback. - * @param m pointer to a message object - * @return the user data of the message - */ -void * lv_msg_get_user_data(lv_msg_t * m); - -/** - * Get the message object from an event object. Can be used in `LV_EVENT_MSG_RECEIVED` events. - * @param e pointer to an event object - * @return the message object or NULL if called with unrelated event code. - */ -lv_msg_t * lv_event_get_msg(lv_event_t * e); - -/********************** - * GLOBAL VARIABLES - **********************/ - -extern lv_event_code_t LV_EVENT_MSG_RECEIVED; - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_MSG*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_MSG_H*/ diff --git a/include/liblvgl/extra/others/snapshot/lv_snapshot.h b/include/liblvgl/extra/others/snapshot/lv_snapshot.h deleted file mode 100644 index 33d97412..00000000 --- a/include/liblvgl/extra/others/snapshot/lv_snapshot.h +++ /dev/null @@ -1,84 +0,0 @@ -/** - * @file lv_snapshot.h - * - */ - -#ifndef LV_SNAPSHOT_H -#define LV_SNAPSHOT_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include -#include - -#include "liblvgl/lv_conf_internal.h" -#include "liblvgl/core/lv_obj.h" - -/********************* - * DEFINES - *********************/ - -#if LV_USE_SNAPSHOT -/********************** - * TYPEDEFS - **********************/ - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** Take snapshot for object with its children. - * - * @param obj The object to generate snapshot. - * @param cf color format for generated image. - * - * @return a pointer to an image descriptor, or NULL if failed. - */ -lv_img_dsc_t * lv_snapshot_take(lv_obj_t * obj, lv_img_cf_t cf); - -/** Free the snapshot image returned by @ref lv_snapshot_take - * - * It will firstly free the data image takes, then the image descriptor. - * - * @param dsc The image descriptor generated by lv_snapshot_take. - * - */ -void lv_snapshot_free(lv_img_dsc_t * dsc); - -/** Get the buffer needed for object snapshot image. - * - * @param obj The object to generate snapshot. - * @param cf color format for generated image. - * - * @return the buffer size needed in bytes - */ -uint32_t lv_snapshot_buf_size_needed(lv_obj_t * obj, lv_img_cf_t cf); - -/** Take snapshot for object with its children, save image info to provided buffer. - * - * @param obj The object to generate snapshot. - * @param cf color format for generated image. - * @param dsc image descriptor to store the image result. - * @param buff the buffer to store image data. - * @param buff_size provided buffer size in bytes. - * - * @return LV_RES_OK on success, LV_RES_INV on error. - */ -lv_res_t lv_snapshot_take_to_buf(lv_obj_t * obj, lv_img_cf_t cf, lv_img_dsc_t * dsc, void * buf, uint32_t buff_size); - - -/********************** - * MACROS - **********************/ -#endif /*LV_USE_SNAPSHOT*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif diff --git a/include/liblvgl/extra/widgets/animimg/lv_animimg.h b/include/liblvgl/extra/widgets/animimg/lv_animimg.h deleted file mode 100644 index 9ffe6217..00000000 --- a/include/liblvgl/extra/widgets/animimg/lv_animimg.h +++ /dev/null @@ -1,103 +0,0 @@ -/** - * @file lv_animimg.h - * - */ - -#ifndef LV_ANIM_IMG_H -#define LV_ANIM_IMG_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lvgl.h" - -#if LV_USE_ANIMIMG != 0 - -/*Testing of dependencies*/ -#if LV_USE_IMG == 0 -#error "lv_animimg: lv_img is required. Enable it in lv_conf.h (LV_USE_IMG 1)" -#endif - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -extern const lv_obj_class_t lv_animimg_class; - -/*Data of image*/ -typedef struct { - lv_img_t img; - lv_anim_t anim; - /*picture sequence */ - lv_img_dsc_t ** dsc; - int8_t pic_count; -} lv_animimg_t; - - -/*Image parts*/ -enum { - LV_ANIM_IMG_PART_MAIN, -}; -typedef uint8_t lv_animimg_part_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Create an animation image objects - * @param parent pointer to an object, it will be the parent of the new button - * @return pointer to the created animation image object - */ -lv_obj_t * lv_animimg_create(lv_obj_t * parent); - -/*===================== - * Setter functions - *====================*/ - -/** - * Set the image animation images source. - * @param img pointer to an animation image object - * @param dsc pointer to a series images - * @param num images' number - */ -void lv_animimg_set_src(lv_obj_t * img, lv_img_dsc_t * dsc[], uint8_t num); - -/** - * Startup the image animation. - * @param obj pointer to an animation image object - */ -void lv_animimg_start(lv_obj_t * obj); - -/** - * Set the image animation duration time. unit:ms - * @param img pointer to an animation image object - */ -void lv_animimg_set_duration(lv_obj_t * img, uint32_t duration); - -/** - * Set the image animation reapeatly play times. - * @param img pointer to an animation image object - * @param count the number of times to repeat the animation - */ -void lv_animimg_set_repeat_count(lv_obj_t * img, uint16_t count); - -/*===================== - * Getter functions - *====================*/ - -#endif /*LV_USE_ANIMIMG*/ - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#endif /*LV_ANIM_IMG_H*/ diff --git a/include/liblvgl/extra/widgets/colorwheel/lv_colorwheel.h b/include/liblvgl/extra/widgets/colorwheel/lv_colorwheel.h deleted file mode 100644 index 38266d01..00000000 --- a/include/liblvgl/extra/widgets/colorwheel/lv_colorwheel.h +++ /dev/null @@ -1,142 +0,0 @@ -/** - * @file lv_colorwheel.h - * - */ - -#ifndef LV_COLORWHEEL_H -#define LV_COLORWHEEL_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lvgl.h" - -#if LV_USE_COLORWHEEL - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -enum { - LV_COLORWHEEL_MODE_HUE, - LV_COLORWHEEL_MODE_SATURATION, - LV_COLORWHEEL_MODE_VALUE -}; -typedef uint8_t lv_colorwheel_mode_t; - - -/*Data of color picker*/ -typedef struct { - lv_obj_t obj; - lv_color_hsv_t hsv; - struct { - lv_point_t pos; - uint8_t recolor : 1; - } knob; - uint32_t last_click_time; - uint32_t last_change_time; - lv_point_t last_press_point; - lv_colorwheel_mode_t mode : 2; - uint8_t mode_fixed : 1; -} lv_colorwheel_t; - -extern const lv_obj_class_t lv_colorwheel_class; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Create a color picker object with disc shape - * @param parent pointer to an object, it will be the parent of the new color picker - * @param knob_recolor true: set the knob's color to the current color - * @return pointer to the created color picker - */ -lv_obj_t * lv_colorwheel_create(lv_obj_t * parent, bool knob_recolor); - -/*===================== - * Setter functions - *====================*/ - -/** - * Set the current hsv of a color wheel. - * @param colorwheel pointer to color wheel object - * @param color current selected hsv - * @return true if changed, otherwise false - */ -bool lv_colorwheel_set_hsv(lv_obj_t * obj, lv_color_hsv_t hsv); - -/** - * Set the current color of a color wheel. - * @param colorwheel pointer to color wheel object - * @param color current selected color - * @return true if changed, otherwise false - */ -bool lv_colorwheel_set_rgb(lv_obj_t * obj, lv_color_t color); - -/** - * Set the current color mode. - * @param colorwheel pointer to color wheel object - * @param mode color mode (hue/sat/val) - */ -void lv_colorwheel_set_mode(lv_obj_t * obj, lv_colorwheel_mode_t mode); - -/** - * Set if the color mode is changed on long press on center - * @param colorwheel pointer to color wheel object - * @param fixed color mode cannot be changed on long press - */ -void lv_colorwheel_set_mode_fixed(lv_obj_t * obj, bool fixed); - -/*===================== - * Getter functions - *====================*/ - -/** - * Get the current selected hsv of a color wheel. - * @param colorwheel pointer to color wheel object - * @return current selected hsv - */ -lv_color_hsv_t lv_colorwheel_get_hsv(lv_obj_t * obj); - -/** - * Get the current selected color of a color wheel. - * @param colorwheel pointer to color wheel object - * @return color current selected color - */ -lv_color_t lv_colorwheel_get_rgb(lv_obj_t * obj); - -/** - * Get the current color mode. - * @param colorwheel pointer to color wheel object - * @return color mode (hue/sat/val) - */ -lv_colorwheel_mode_t lv_colorwheel_get_color_mode(lv_obj_t * obj); - -/** - * Get if the color mode is changed on long press on center - * @param colorwheel pointer to color wheel object - * @return mode cannot be changed on long press - */ -bool lv_colorwheel_get_color_mode_fixed(lv_obj_t * obj); - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_COLORWHEEL*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_COLORWHEEL_H*/ - diff --git a/include/liblvgl/extra/widgets/imgbtn/lv_imgbtn.h b/include/liblvgl/extra/widgets/imgbtn/lv_imgbtn.h deleted file mode 100644 index 38bcc033..00000000 --- a/include/liblvgl/extra/widgets/imgbtn/lv_imgbtn.h +++ /dev/null @@ -1,131 +0,0 @@ -/** - * @file lv_imgbtn.h - * - */ - -#ifndef LV_IMGBTN_H -#define LV_IMGBTN_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lvgl.h" - -#if LV_USE_IMGBTN != 0 - -/********************* - * DEFINES - *********************/ -typedef enum { - LV_IMGBTN_STATE_RELEASED, - LV_IMGBTN_STATE_PRESSED, - LV_IMGBTN_STATE_DISABLED, - LV_IMGBTN_STATE_CHECKED_RELEASED, - LV_IMGBTN_STATE_CHECKED_PRESSED, - LV_IMGBTN_STATE_CHECKED_DISABLED, - _LV_IMGBTN_STATE_NUM, -} lv_imgbtn_state_t; - -/********************** - * TYPEDEFS - **********************/ -/*Data of image button*/ -typedef struct { - lv_obj_t obj; - const void * img_src_mid[_LV_IMGBTN_STATE_NUM]; /*Store center images to each state*/ - const void * img_src_left[_LV_IMGBTN_STATE_NUM]; /*Store left side images to each state*/ - const void * img_src_right[_LV_IMGBTN_STATE_NUM]; /*Store right side images to each state*/ - lv_img_cf_t act_cf; /*Color format of the currently active image*/ -} lv_imgbtn_t; - -extern const lv_obj_class_t lv_imgbtn_class; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Create an image button object - * @param parent pointer to an object, it will be the parent of the new image button - * @return pointer to the created image button - */ -lv_obj_t * lv_imgbtn_create(lv_obj_t * parent); - -/*====================== - * Add/remove functions - *=====================*/ - -/*===================== - * Setter functions - *====================*/ - -/** - * Set images for a state of the image button - * @param imgbtn pointer to an image button object - * @param state for which state set the new image - * @param src_left pointer to an image source for the left side of the button (a C array or path to - * a file) - * @param src_mid pointer to an image source for the middle of the button (ideally 1px wide) (a C - * array or path to a file) - * @param src_right pointer to an image source for the right side of the button (a C array or path - * to a file) - */ -void lv_imgbtn_set_src(lv_obj_t * imgbtn, lv_imgbtn_state_t state, const void * src_left, const void * src_mid, - const void * src_right); - - -/** - * Use this function instead of `lv_obj_add/clear_state` to set a state manually - * @param imgbtn pointer to an image button object - * @param state the new state - */ -void lv_imgbtn_set_state(lv_obj_t * imgbtn, lv_imgbtn_state_t state); - -/*===================== - * Getter functions - *====================*/ - -/** - * Get the left image in a given state - * @param imgbtn pointer to an image button object - * @param state the state where to get the image (from `lv_btn_state_t`) ` - * @return pointer to the left image source (a C array or path to a file) - */ -const void * lv_imgbtn_get_src_left(lv_obj_t * imgbtn, lv_imgbtn_state_t state); - -/** - * Get the middle image in a given state - * @param imgbtn pointer to an image button object - * @param state the state where to get the image (from `lv_btn_state_t`) ` - * @return pointer to the middle image source (a C array or path to a file) - */ -const void * lv_imgbtn_get_src_middle(lv_obj_t * imgbtn, lv_imgbtn_state_t state); - -/** - * Get the right image in a given state - * @param imgbtn pointer to an image button object - * @param state the state where to get the image (from `lv_btn_state_t`) ` - * @return pointer to the left image source (a C array or path to a file) - */ -const void * lv_imgbtn_get_src_right(lv_obj_t * imgbtn, lv_imgbtn_state_t state); - - -/*===================== - * Other functions - *====================*/ - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_IMGBTN*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_IMGBTN_H*/ diff --git a/include/liblvgl/extra/widgets/list/lv_list.h b/include/liblvgl/extra/widgets/list/lv_list.h deleted file mode 100644 index e9bb6266..00000000 --- a/include/liblvgl/extra/widgets/list/lv_list.h +++ /dev/null @@ -1,54 +0,0 @@ -/** - * @file lv_win.h - * - */ - -#ifndef LV_LIST_H -#define LV_LIST_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/core/lv_obj.h" -#include "liblvgl/extra/layouts/flex/lv_flex.h" - -#if LV_USE_LIST - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -extern const lv_obj_class_t lv_list_class; -extern const lv_obj_class_t lv_list_text_class; -extern const lv_obj_class_t lv_list_btn_class; -/********************** - * GLOBAL PROTOTYPES - **********************/ - -lv_obj_t * lv_list_create(lv_obj_t * parent); - -lv_obj_t * lv_list_add_text(lv_obj_t * list, const char * txt); - -lv_obj_t * lv_list_add_btn(lv_obj_t * list, const void * icon, const char * txt); - -const char * lv_list_get_btn_text(lv_obj_t * list, lv_obj_t * btn); - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_LIST*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_LIST_H*/ diff --git a/include/liblvgl/extra/widgets/lv_widgets.h b/include/liblvgl/extra/widgets/lv_widgets.h deleted file mode 100644 index 11418102..00000000 --- a/include/liblvgl/extra/widgets/lv_widgets.h +++ /dev/null @@ -1,56 +0,0 @@ -/** - * @file lv_widgets.h - * - */ - -#ifndef LV_WIDGETS_H -#define LV_WIDGETS_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "animimg/lv_animimg.h" -#include "calendar/lv_calendar.h" -#include "calendar/lv_calendar_header_arrow.h" -#include "calendar/lv_calendar_header_dropdown.h" -#include "chart/lv_chart.h" -#include "keyboard/lv_keyboard.h" -#include "list/lv_list.h" -#include "menu/lv_menu.h" -#include "msgbox/lv_msgbox.h" -#include "meter/lv_meter.h" -#include "spinbox/lv_spinbox.h" -#include "spinner/lv_spinner.h" -#include "tabview/lv_tabview.h" -#include "tileview/lv_tileview.h" -#include "win/lv_win.h" -#include "colorwheel/lv_colorwheel.h" -#include "led/lv_led.h" -#include "imgbtn/lv_imgbtn.h" -#include "span/lv_span.h" - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/********************** - * MACROS - **********************/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_WIDGETS_H*/ diff --git a/include/liblvgl/extra/widgets/menu/lv_menu.h b/include/liblvgl/extra/widgets/menu/lv_menu.h deleted file mode 100644 index 42099792..00000000 --- a/include/liblvgl/extra/widgets/menu/lv_menu.h +++ /dev/null @@ -1,233 +0,0 @@ -/** - * @file lv_menu.h - * - */ - -#ifndef LV_MENU_H -#define LV_MENU_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/core/lv_obj.h" - -#if LV_USE_MENU - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -enum { - LV_MENU_HEADER_TOP_FIXED, /* Header is positioned at the top */ - LV_MENU_HEADER_TOP_UNFIXED, /* Header is positioned at the top and can be scrolled out of view*/ - LV_MENU_HEADER_BOTTOM_FIXED /* Header is positioned at the bottom */ -}; -typedef uint8_t lv_menu_mode_header_t; - -enum { - LV_MENU_ROOT_BACK_BTN_DISABLED, - LV_MENU_ROOT_BACK_BTN_ENABLED -}; -typedef uint8_t lv_menu_mode_root_back_btn_t; - -typedef struct lv_menu_load_page_event_data_t { - lv_obj_t * menu; - lv_obj_t * page; -} lv_menu_load_page_event_data_t; - -typedef struct { - lv_obj_t * page; -} lv_menu_history_t; - -typedef struct { - lv_obj_t obj; - lv_obj_t * storage; /* a pointer to obj that is the parent of all pages not displayed */ - lv_obj_t * main; - lv_obj_t * main_page; - lv_obj_t * main_header; - lv_obj_t * - main_header_back_btn; /* a pointer to obj that on click triggers back btn event handler, can be same as 'main_header' */ - lv_obj_t * main_header_title; - lv_obj_t * sidebar; - lv_obj_t * sidebar_page; - lv_obj_t * sidebar_header; - lv_obj_t * - sidebar_header_back_btn; /* a pointer to obj that on click triggers back btn event handler, can be same as 'sidebar_header' */ - lv_obj_t * sidebar_header_title; - lv_obj_t * selected_tab; - lv_ll_t history_ll; - uint8_t cur_depth; - uint8_t prev_depth; - uint8_t sidebar_generated : 1; - lv_menu_mode_header_t mode_header : 2; - lv_menu_mode_root_back_btn_t mode_root_back_btn : 1; -} lv_menu_t; - -typedef struct { - lv_obj_t obj; - char * title; -} lv_menu_page_t; - -extern const lv_obj_class_t lv_menu_class; -extern const lv_obj_class_t lv_menu_page_class; -extern const lv_obj_class_t lv_menu_cont_class; -extern const lv_obj_class_t lv_menu_section_class; -extern const lv_obj_class_t lv_menu_separator_class; -extern const lv_obj_class_t lv_menu_sidebar_cont_class; -extern const lv_obj_class_t lv_menu_main_cont_class; -extern const lv_obj_class_t lv_menu_sidebar_header_cont_class; -extern const lv_obj_class_t lv_menu_main_header_cont_class; -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Create a menu object - * @param parent pointer to an object, it will be the parent of the new menu - * @return pointer to the created menu - */ -lv_obj_t * lv_menu_create(lv_obj_t * parent); - -/** - * Create a menu page object - * @param parent pointer to menu object - * @param title pointer to text for title in header (NULL to not display title) - * @return pointer to the created menu page - */ -lv_obj_t * lv_menu_page_create(lv_obj_t * parent, char * title); - -/** - * Create a menu cont object - * @param parent pointer to an object, it will be the parent of the new menu cont object - * @return pointer to the created menu cont - */ -lv_obj_t * lv_menu_cont_create(lv_obj_t * parent); - -/** - * Create a menu section object - * @param parent pointer to an object, it will be the parent of the new menu section object - * @return pointer to the created menu section - */ -lv_obj_t * lv_menu_section_create(lv_obj_t * parent); - -/** - * Create a menu separator object - * @param parent pointer to an object, it will be the parent of the new menu separator object - * @return pointer to the created menu separator - */ -lv_obj_t * lv_menu_separator_create(lv_obj_t * parent); -/*===================== - * Setter functions - *====================*/ -/** - * Set menu page to display in main - * @param obj pointer to the menu - * @param page pointer to the menu page to set (NULL to clear main and clear menu history) - */ -void lv_menu_set_page(lv_obj_t * obj, lv_obj_t * page); - -/** - * Set menu page to display in sidebar - * @param obj pointer to the menu - * @param page pointer to the menu page to set (NULL to clear sidebar) - */ -void lv_menu_set_sidebar_page(lv_obj_t * obj, lv_obj_t * page); - -/** - * Set the how the header should behave and its position - * @param obj pointer to a menu - * @param mode_header - */ -void lv_menu_set_mode_header(lv_obj_t * obj, lv_menu_mode_header_t mode_header); - -/** - * Set whether back button should appear at root - * @param obj pointer to a menu - * @param mode_root_back_btn - */ -void lv_menu_set_mode_root_back_btn(lv_obj_t * obj, lv_menu_mode_root_back_btn_t mode_root_back_btn); - -/** - * Add menu to the menu item - * @param menu pointer to the menu - * @param obj pointer to the obj - * @param page pointer to the page to load when obj is clicked - */ -void lv_menu_set_load_page_event(lv_obj_t * menu, lv_obj_t * obj, lv_obj_t * page); - -/*===================== - * Getter functions - *====================*/ -/** -* Get a pointer to menu page that is currently displayed in main -* @param obj pointer to the menu -* @return pointer to current page -*/ -lv_obj_t * lv_menu_get_cur_main_page(lv_obj_t * obj); - -/** -* Get a pointer to menu page that is currently displayed in sidebar -* @param obj pointer to the menu -* @return pointer to current page -*/ -lv_obj_t * lv_menu_get_cur_sidebar_page(lv_obj_t * obj); - -/** -* Get a pointer to main header obj -* @param obj pointer to the menu -* @return pointer to main header obj -*/ -lv_obj_t * lv_menu_get_main_header(lv_obj_t * obj); - -/** -* Get a pointer to main header back btn obj -* @param obj pointer to the menu -* @return pointer to main header back btn obj -*/ -lv_obj_t * lv_menu_get_main_header_back_btn(lv_obj_t * obj); - -/** -* Get a pointer to sidebar header obj -* @param obj pointer to the menu -* @return pointer to sidebar header obj -*/ -lv_obj_t * lv_menu_get_sidebar_header(lv_obj_t * obj); - -/** -* Get a pointer to sidebar header obj -* @param obj pointer to the menu -* @return pointer to sidebar header back btn obj -*/ -lv_obj_t * lv_menu_get_sidebar_header_back_btn(lv_obj_t * obj); - -/** - * Check if an obj is a root back btn - * @param menu pointer to the menu - * @return true if it is a root back btn - */ -bool lv_menu_back_btn_is_root(lv_obj_t * menu, lv_obj_t * obj); - -/** - * Clear menu history - * @param obj pointer to the menu - */ -void lv_menu_clear_history(lv_obj_t * obj); -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_MENU*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_MENU_H*/ diff --git a/include/liblvgl/extra/widgets/meter/lv_meter.h b/include/liblvgl/extra/widgets/meter/lv_meter.h deleted file mode 100644 index d64ad0d5..00000000 --- a/include/liblvgl/extra/widgets/meter/lv_meter.h +++ /dev/null @@ -1,267 +0,0 @@ -/** - * @file lv_meter.h - * - */ - -#ifndef LV_METER_H -#define LV_METER_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lvgl.h" - -#if LV_USE_METER != 0 - -/*Testing of dependencies*/ -#if LV_DRAW_COMPLEX == 0 -#error "lv_meter: Complex drawing is required. Enable it in lv_conf.h (LV_DRAW_COMPLEX 1)" -#endif - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -typedef struct { - lv_color_t tick_color; - uint16_t tick_cnt; - uint16_t tick_length; - uint16_t tick_width; - - lv_color_t tick_major_color; - uint16_t tick_major_nth; - uint16_t tick_major_length; - uint16_t tick_major_width; - - int16_t label_gap; - int16_t label_color; - - int32_t min; - int32_t max; - int16_t r_mod; - uint16_t angle_range; - int16_t rotation; -} lv_meter_scale_t; - -enum { - LV_METER_INDICATOR_TYPE_NEEDLE_IMG, - LV_METER_INDICATOR_TYPE_NEEDLE_LINE, - LV_METER_INDICATOR_TYPE_SCALE_LINES, - LV_METER_INDICATOR_TYPE_ARC, -}; -typedef uint8_t lv_meter_indicator_type_t; - -typedef struct { - lv_meter_scale_t * scale; - lv_meter_indicator_type_t type; - lv_opa_t opa; - int32_t start_value; - int32_t end_value; - union { - struct { - const void * src; - lv_point_t pivot; - } needle_img; - struct { - uint16_t width; - int16_t r_mod; - lv_color_t color; - } needle_line; - struct { - uint16_t width; - const void * src; - lv_color_t color; - int16_t r_mod; - } arc; - struct { - int16_t width_mod; - lv_color_t color_start; - lv_color_t color_end; - uint8_t local_grad : 1; - } scale_lines; - } type_data; -} lv_meter_indicator_t; - -/*Data of line meter*/ -typedef struct { - lv_obj_t obj; - lv_ll_t scale_ll; - lv_ll_t indicator_ll; -} lv_meter_t; - -extern const lv_obj_class_t lv_meter_class; - -/** - * `type` field in `lv_obj_draw_part_dsc_t` if `class_p = lv_meter_class` - * Used in `LV_EVENT_DRAW_PART_BEGIN` and `LV_EVENT_DRAW_PART_END` - */ -typedef enum { - LV_METER_DRAW_PART_ARC, /**< The arc indicator*/ - LV_METER_DRAW_PART_NEEDLE_LINE, /**< The needle lines*/ - LV_METER_DRAW_PART_NEEDLE_IMG, /**< The needle images*/ - LV_METER_DRAW_PART_TICK, /**< The tick lines and labels*/ -} lv_meter_draw_part_type_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Create a Meter object - * @param parent pointer to an object, it will be the parent of the new bar. - * @return pointer to the created meter - */ -lv_obj_t * lv_meter_create(lv_obj_t * parent); - -/*===================== - * Add scale - *====================*/ - -/** - * Add a new scale to the meter. - * @param obj pointer to a meter object - * @return the new scale - * @note Indicators can be attached to scales. - */ -lv_meter_scale_t * lv_meter_add_scale(lv_obj_t * obj); - -/** - * Set the properties of the ticks of a scale - * @param obj pointer to a meter object - * @param scale pointer to scale (added to `meter`) - * @param cnt number of tick lines - * @param width width of tick lines - * @param len length of tick lines - * @param color color of tick lines - */ -void lv_meter_set_scale_ticks(lv_obj_t * obj, lv_meter_scale_t * scale, uint16_t cnt, uint16_t width, uint16_t len, - lv_color_t color); - -/** - * Make some "normal" ticks major ticks and set their attributes. - * Texts with the current value are also added to the major ticks. - * @param obj pointer to a meter object - * @param scale pointer to scale (added to `meter`) - * @param nth make every Nth normal tick major tick. (start from the first on the left) - * @param width width of the major ticks - * @param len length of the major ticks - * @param color color of the major ticks - * @param label_gap gap between the major ticks and the labels - */ -void lv_meter_set_scale_major_ticks(lv_obj_t * obj, lv_meter_scale_t * scale, uint16_t nth, uint16_t width, - uint16_t len, lv_color_t color, int16_t label_gap); - -/** - * Set the value and angular range of a scale. - * @param obj pointer to a meter object - * @param scale pointer to scale (added to `meter`) - * @param min the minimum value - * @param max the maximal value - * @param angle_range the angular range of the scale - * @param rotation the angular offset from the 3 o'clock position (clock-wise) - */ -void lv_meter_set_scale_range(lv_obj_t * obj, lv_meter_scale_t * scale, int32_t min, int32_t max, uint32_t angle_range, - uint32_t rotation); - -/*===================== - * Add indicator - *====================*/ - -/** - * Add a needle line indicator the scale - * @param obj pointer to a meter object - * @param scale pointer to scale (added to `meter`) - * @param width width of the line - * @param color color of the line - * @param r_mod the radius modifier (added to the scale's radius) to get the lines length - * @return the new indicator - */ -lv_meter_indicator_t * lv_meter_add_needle_line(lv_obj_t * obj, lv_meter_scale_t * scale, uint16_t width, - lv_color_t color, int16_t r_mod); - -/** - * Add a needle image indicator the scale - * @param obj pointer to a meter object - * @param scale pointer to scale (added to `meter`) - * @param src the image source of the indicator. path or pointer to ::lv_img_dsc_t - * @param pivot_x the X pivot point of the needle - * @param pivot_y the Y pivot point of the needle - * @return the new indicator - * @note the needle image should point to the right, like -O-----> - */ -lv_meter_indicator_t * lv_meter_add_needle_img(lv_obj_t * obj, lv_meter_scale_t * scale, const void * src, - lv_coord_t pivot_x, lv_coord_t pivot_y); - -/** - * Add an arc indicator the scale - * @param obj pointer to a meter object - * @param scale pointer to scale (added to `meter`) - * @param width width of the arc - * @param color color of the arc - * @param r_mod the radius modifier (added to the scale's radius) to get the outer radius of the arc - * @return the new indicator - */ -lv_meter_indicator_t * lv_meter_add_arc(lv_obj_t * obj, lv_meter_scale_t * scale, uint16_t width, lv_color_t color, - int16_t r_mod); - - -/** - * Add a scale line indicator the scale. It will modify the ticks. - * @param obj pointer to a meter object - * @param scale pointer to scale (added to `meter`) - * @param color_start the start color - * @param color_end the end color - * @param local tell how to map start and end color. true: the indicator's start and end_value; false: the scale's min max value - * @param width_mod add this the affected tick's width - * @return the new indicator - */ -lv_meter_indicator_t * lv_meter_add_scale_lines(lv_obj_t * obj, lv_meter_scale_t * scale, lv_color_t color_start, - lv_color_t color_end, bool local, int16_t width_mod); - -/*===================== - * Set indicator value - *====================*/ - -/** - * Set the value of the indicator. It will set start and and value to the same value - * @param obj pointer to a meter object - * @param indic pointer to an indicator - * @param value the new value - */ -void lv_meter_set_indicator_value(lv_obj_t * obj, lv_meter_indicator_t * indic, int32_t value); - -/** - * Set the start value of the indicator. - * @param obj pointer to a meter object - * @param indic pointer to an indicator - * @param value the new value - */ -void lv_meter_set_indicator_start_value(lv_obj_t * obj, lv_meter_indicator_t * indic, int32_t value); - -/** - * Set the start value of the indicator. - * @param obj pointer to a meter object - * @param indic pointer to an indicator - * @param value the new value - */ -void lv_meter_set_indicator_end_value(lv_obj_t * obj, lv_meter_indicator_t * indic, int32_t value); - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_METER*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_METER_H*/ diff --git a/include/liblvgl/extra/widgets/msgbox/lv_msgbox.h b/include/liblvgl/extra/widgets/msgbox/lv_msgbox.h deleted file mode 100644 index 6b8ccd61..00000000 --- a/include/liblvgl/extra/widgets/msgbox/lv_msgbox.h +++ /dev/null @@ -1,99 +0,0 @@ -/** - * @file lv_mbox.h - * - */ - -#ifndef LV_MSGBOX_H -#define LV_MSGBOX_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lvgl.h" - -#if LV_USE_MSGBOX - -/*Testing of dependencies*/ -#if LV_USE_BTNMATRIX == 0 -#error "lv_mbox: lv_btnm is required. Enable it in lv_conf.h (LV_USE_BTNMATRIX 1) " -#endif - -#if LV_USE_LABEL == 0 -#error "lv_mbox: lv_label is required. Enable it in lv_conf.h (LV_USE_LABEL 1) " -#endif - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -typedef struct { - lv_obj_t obj; - lv_obj_t * title; - lv_obj_t * close_btn; - lv_obj_t * content; - lv_obj_t * text; - lv_obj_t * btns; -} lv_msgbox_t; - -extern const lv_obj_class_t lv_msgbox_class; -extern const lv_obj_class_t lv_msgbox_content_class; -extern const lv_obj_class_t lv_msgbox_backdrop_class; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Create a message box object - * @param parent pointer to parent or NULL to create a full screen modal message box - * @param title the title of the message box - * @param txt the text of the message box - * @param btn_txts the buttons as an array of texts terminated by an "" element. E.g. {"btn1", "btn2", ""} - * @param add_close_btn true: add a close button - * @return pointer to the message box object - */ -lv_obj_t * lv_msgbox_create(lv_obj_t * parent, const char * title, const char * txt, const char * btn_txts[], - bool add_close_btn); - -lv_obj_t * lv_msgbox_get_title(lv_obj_t * obj); - -lv_obj_t * lv_msgbox_get_close_btn(lv_obj_t * obj); - -lv_obj_t * lv_msgbox_get_text(lv_obj_t * obj); - -lv_obj_t * lv_msgbox_get_content(lv_obj_t * obj); - -lv_obj_t * lv_msgbox_get_btns(lv_obj_t * obj); - -/** - * Get the index of the selected button - * @param mbox message box object - * @return index of the button (LV_BTNMATRIX_BTN_NONE: if unset) - */ -uint16_t lv_msgbox_get_active_btn(lv_obj_t * mbox); - -const char * lv_msgbox_get_active_btn_text(lv_obj_t * mbox); - -void lv_msgbox_close(lv_obj_t * mbox); - -void lv_msgbox_close_async(lv_obj_t * mbox); - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_MSGBOX*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_MSGBOX_H*/ diff --git a/include/liblvgl/extra/widgets/span/lv_span.h b/include/liblvgl/extra/widgets/span/lv_span.h deleted file mode 100644 index bbf65acb..00000000 --- a/include/liblvgl/extra/widgets/span/lv_span.h +++ /dev/null @@ -1,245 +0,0 @@ -/** - * @file lv_span.h - * - */ - -#ifndef LV_SPAN_H -#define LV_SPAN_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lvgl.h" - -#if LV_USE_SPAN != 0 - -/********************* - * DEFINES - *********************/ -#ifndef LV_SPAN_SNIPPET_STACK_SIZE -#define LV_SPAN_SNIPPET_STACK_SIZE 64 -#endif - -/********************** - * TYPEDEFS - **********************/ -enum { - LV_SPAN_OVERFLOW_CLIP, - LV_SPAN_OVERFLOW_ELLIPSIS, -}; -typedef uint8_t lv_span_overflow_t; - -enum { - LV_SPAN_MODE_FIXED, /**< fixed the obj size*/ - LV_SPAN_MODE_EXPAND, /**< Expand the object size to the text size*/ - LV_SPAN_MODE_BREAK, /**< Keep width, break the too long lines and expand height*/ -}; -typedef uint8_t lv_span_mode_t; - -typedef struct { - char * txt; /* a pointer to display text */ - lv_obj_t * spangroup; /* a pointer to spangroup */ - lv_style_t style; /* display text style */ - uint8_t static_flag : 1;/* the text is static flag */ -} lv_span_t; - -/** Data of label*/ -typedef struct { - lv_obj_t obj; - int32_t lines; - lv_coord_t indent; /* first line indent */ - lv_coord_t cache_w; /* the cache automatically calculates the width */ - lv_coord_t cache_h; /* similar cache_w */ - lv_ll_t child_ll; - uint8_t mode : 2; /* details see lv_span_mode_t */ - uint8_t overflow : 1; /* details see lv_span_overflow_t */ - uint8_t refresh : 1; /* the spangroup need refresh cache_w and cache_h */ -} lv_spangroup_t; - -extern const lv_obj_class_t lv_spangroup_class; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Create a spangroup object - * @param par pointer to an object, it will be the parent of the new spangroup - * @return pointer to the created spangroup - */ -lv_obj_t * lv_spangroup_create(lv_obj_t * par); - -/** - * Create a span string descriptor and add to spangroup. - * @param obj pointer to a spangroup object. - * @return pointer to the created span. - */ -lv_span_t * lv_spangroup_new_span(lv_obj_t * obj); - -/** - * Remove the span from the spangroup and free memory. - * @param obj pointer to a spangroup object. - * @param span pointer to a span. - */ -void lv_spangroup_del_span(lv_obj_t * obj, lv_span_t * span); - -/*===================== - * Setter functions - *====================*/ - -/** - * Set a new text for a span. Memory will be allocated to store the text by the span. - * @param span pointer to a span. - * @param text pointer to a text. - */ -void lv_span_set_text(lv_span_t * span, const char * text); - -/** - * Set a static text. It will not be saved by the span so the 'text' variable - * has to be 'alive' while the span exist. - * @param span pointer to a span. - * @param text pointer to a text. - */ -void lv_span_set_text_static(lv_span_t * span, const char * text); - -/** - * Set the align of the spangroup. - * @param obj pointer to a spangroup object. - * @param align see lv_text_align_t for details. - */ -void lv_spangroup_set_align(lv_obj_t * obj, lv_text_align_t align); - -/** - * Set the overflow of the spangroup. - * @param obj pointer to a spangroup object. - * @param overflow see lv_span_overflow_t for details. - */ -void lv_spangroup_set_overflow(lv_obj_t * obj, lv_span_overflow_t overflow); - -/** - * Set the indent of the spangroup. - * @param obj pointer to a spangroup object. - * @param indent The first line indentation - */ -void lv_spangroup_set_indent(lv_obj_t * obj, lv_coord_t indent); - -/** - * Set the mode of the spangroup. - * @param obj pointer to a spangroup object. - * @param mode see lv_span_mode_t for details. - */ -void lv_spangroup_set_mode(lv_obj_t * obj, lv_span_mode_t mode); - -/** - * Set lines of the spangroup. - * @param obj pointer to a spangroup object. - * @param lines max lines that can be displayed in LV_SPAN_MODE_BREAK mode. < 0 means no limit. - */ -void lv_spangroup_set_lines(lv_obj_t * obj, int32_t lines); - -/*===================== - * Getter functions - *====================*/ - -/** - * Get a spangroup child by its index. - * - * @param obj The spangroup object - * @param id the index of the child. - * 0: the oldest (firstly created) child - * 1: the second oldest - * child count-1: the youngest - * -1: the youngest - * -2: the second youngest - * @return The child span at index `id`, or NULL if the ID does not exist - */ -lv_span_t * lv_spangroup_get_child(const lv_obj_t * obj, int32_t id); - -/** - * - * @param obj The spangroup object to get the child count of. - * @return The span count of the spangroup. - */ -uint32_t lv_spangroup_get_child_cnt(const lv_obj_t * obj); - -/** - * get the align of the spangroup. - * @param obj pointer to a spangroup object. - * @return the align value. - */ -lv_text_align_t lv_spangroup_get_align(lv_obj_t * obj); - -/** - * get the overflow of the spangroup. - * @param obj pointer to a spangroup object. - * @return the overflow value. - */ -lv_span_overflow_t lv_spangroup_get_overflow(lv_obj_t * obj); - -/** - * get the indent of the spangroup. - * @param obj pointer to a spangroup object. - * @return the indent value. - */ -lv_coord_t lv_spangroup_get_indent(lv_obj_t * obj); - -/** - * get the mode of the spangroup. - * @param obj pointer to a spangroup object. - */ -lv_span_mode_t lv_spangroup_get_mode(lv_obj_t * obj); - -/** - * get lines of the spangroup. - * @param obj pointer to a spangroup object. - * @return the lines value. - */ -int32_t lv_spangroup_get_lines(lv_obj_t * obj); - -/** - * get max line height of all span in the spangroup. - * @param obj pointer to a spangroup object. - */ -lv_coord_t lv_spangroup_get_max_line_h(lv_obj_t * obj); - -/** - * get the text content width when all span of spangroup on a line. - * @param obj pointer to a spangroup object. - * @param max_width if text content width >= max_width, return max_width - * to reduce computation, if max_width == 0, returns the text content width. - * @return text content width or max_width. - */ -uint32_t lv_spangroup_get_expand_width(lv_obj_t * obj, uint32_t max_width); - -/** - * get the text content height with width fixed. - * @param obj pointer to a spangroup object. - */ -lv_coord_t lv_spangroup_get_expand_height(lv_obj_t * obj, lv_coord_t width); - - -/*===================== - * Other functions - *====================*/ - -/** - * update the mode of the spangroup. - * @param obj pointer to a spangroup object. - */ -void lv_spangroup_refr_mode(lv_obj_t * obj); - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_SPAN*/ - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#endif /*LV_SPAN_H*/ diff --git a/include/liblvgl/extra/widgets/tabview/lv_tabview.h b/include/liblvgl/extra/widgets/tabview/lv_tabview.h deleted file mode 100644 index bcee5a13..00000000 --- a/include/liblvgl/extra/widgets/tabview/lv_tabview.h +++ /dev/null @@ -1,65 +0,0 @@ -/** - * @file lv_templ.h - * - */ - -#ifndef LV_TABVIEW_H -#define LV_TABVIEW_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lvgl.h" - -#if LV_USE_TABVIEW - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -typedef struct { - lv_obj_t obj; - char ** map; - uint16_t tab_cnt; - uint16_t tab_cur; - lv_dir_t tab_pos; -} lv_tabview_t; - -extern const lv_obj_class_t lv_tabview_class; - -/********************** - * GLOBAL PROTOTYPES - **********************/ -lv_obj_t * lv_tabview_create(lv_obj_t * parent, lv_dir_t tab_pos, lv_coord_t tab_size); - -lv_obj_t * lv_tabview_add_tab(lv_obj_t * tv, const char * name); - -void lv_tabview_rename_tab(lv_obj_t * obj, uint32_t tab_id, const char * new_name); - -lv_obj_t * lv_tabview_get_content(lv_obj_t * tv); - -lv_obj_t * lv_tabview_get_tab_btns(lv_obj_t * tv); - -void lv_tabview_set_act(lv_obj_t * obj, uint32_t id, lv_anim_enable_t anim_en); - -uint16_t lv_tabview_get_tab_act(lv_obj_t * tv); - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_TABVIEW*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_TABVIEW_H*/ diff --git a/include/liblvgl/extra/widgets/tileview/lv_tileview.h b/include/liblvgl/extra/widgets/tileview/lv_tileview.h deleted file mode 100644 index 34a1bf18..00000000 --- a/include/liblvgl/extra/widgets/tileview/lv_tileview.h +++ /dev/null @@ -1,72 +0,0 @@ -/** - * @file lv_tileview.h - * - */ - -#ifndef LV_TILEVIEW_H -#define LV_TILEVIEW_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/core/lv_obj.h" - -#if LV_USE_TILEVIEW - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ -typedef struct { - lv_obj_t obj; - lv_obj_t * tile_act; -} lv_tileview_t; - -typedef struct { - lv_obj_t obj; - lv_dir_t dir; -} lv_tileview_tile_t; - -extern const lv_obj_class_t lv_tileview_class; -extern const lv_obj_class_t lv_tileview_tile_class; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Create a Tileview object - * @param parent pointer to an object, it will be the parent of the new tileview - * @return pointer to the created tileview - */ -lv_obj_t * lv_tileview_create(lv_obj_t * parent); - -lv_obj_t * lv_tileview_add_tile(lv_obj_t * tv, uint8_t col_id, uint8_t row_id, lv_dir_t dir); - -void lv_obj_set_tile(lv_obj_t * tv, lv_obj_t * tile_obj, lv_anim_enable_t anim_en); -void lv_obj_set_tile_id(lv_obj_t * tv, uint32_t col_id, uint32_t row_id, lv_anim_enable_t anim_en); - -lv_obj_t * lv_tileview_get_tile_act(lv_obj_t * obj); - -/*===================== - * Other functions - *====================*/ - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_TILEVIEW*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_TILEVIEW_H*/ diff --git a/include/liblvgl/extra/widgets/win/lv_win.h b/include/liblvgl/extra/widgets/win/lv_win.h deleted file mode 100644 index 2ea907d3..00000000 --- a/include/liblvgl/extra/widgets/win/lv_win.h +++ /dev/null @@ -1,51 +0,0 @@ -/** - * @file lv_win.h - * - */ - -#ifndef LV_WIN_H -#define LV_WIN_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lvgl.h" - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ -typedef struct { - lv_obj_t obj; -} lv_win_t; - -extern const lv_obj_class_t lv_win_class; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -lv_obj_t * lv_win_create(lv_obj_t * parent, lv_coord_t header_height); - - -lv_obj_t * lv_win_add_title(lv_obj_t * win, const char * txt); -lv_obj_t * lv_win_add_btn(lv_obj_t * win, const void * icon, lv_coord_t btn_w); - -lv_obj_t * lv_win_get_header(lv_obj_t * win); -lv_obj_t * lv_win_get_content(lv_obj_t * win); -/********************** - * MACROS - **********************/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_WIN_H*/ diff --git a/include/liblvgl/font/lv_binfont_loader.h b/include/liblvgl/font/lv_binfont_loader.h new file mode 100644 index 00000000..25e7b837 --- /dev/null +++ b/include/liblvgl/font/lv_binfont_loader.h @@ -0,0 +1,61 @@ +/** + * @file lv_binfont_loader.h + * + */ + +#ifndef LV_BINFONT_LOADER_H +#define LV_BINFONT_LOADER_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Loads a `lv_font_t` object from a binary font file + * @param path path to font file + * @return pointer to font where to load + */ +lv_font_t * lv_binfont_create(const char * path); + +#if LV_USE_FS_MEMFS +/** + * Loads a `lv_font_t` object from a memory buffer containing the binary font file. + * Requires LV_USE_FS_MEMFS + * @param buffer address of the font file in the memory + * @param size size of the font file buffer + * @return pointer to font where to load + */ +lv_font_t * lv_binfont_create_from_buffer(void * buffer, uint32_t size); +#endif + +/** + * Frees the memory allocated by the `lv_binfont_create()` function + * @param font lv_font_t object created by the lv_binfont_create function + */ +void lv_binfont_destroy(lv_font_t * font); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /* LV_BINFONT_LOADER_H */ diff --git a/include/liblvgl/font/lv_font.h b/include/liblvgl/font/lv_font.h index 3c0a689e..9a043a78 100644 --- a/include/liblvgl/font/lv_font.h +++ b/include/liblvgl/font/lv_font.h @@ -13,19 +13,18 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" -#include -#include -#include +#include "../lv_conf_internal.h" +#include "../misc/lv_types.h" #include "lv_symbol_def.h" -#include "liblvgl/misc/lv_area.h" +#include "../draw/lv_draw_buf.h" +#include "../misc/lv_area.h" +#include "../misc/cache/lv_cache.h" /********************* * DEFINES *********************/ -/* imgfont identifier */ #define LV_IMGFONT_BPP 9 /********************** @@ -36,52 +35,81 @@ extern "C" { * General types *-----------------*/ -struct _lv_font_t; +/** The font format.*/ +typedef enum { + LV_FONT_GLYPH_FORMAT_NONE = 0, /**< Maybe not visible*/ + + /**< Legacy simple formats*/ + LV_FONT_GLYPH_FORMAT_A1 = 0x01, /**< 1 bit per pixel*/ + LV_FONT_GLYPH_FORMAT_A2 = 0x02, /**< 2 bit per pixel*/ + LV_FONT_GLYPH_FORMAT_A4 = 0x04, /**< 4 bit per pixel*/ + LV_FONT_GLYPH_FORMAT_A8 = 0x08, /**< 8 bit per pixel*/ + + LV_FONT_GLYPH_FORMAT_IMAGE = 0x09, /**< Image format*/ + + /**< Advanced formats*/ + LV_FONT_GLYPH_FORMAT_VECTOR = 0x0A, /**< Vectorial format*/ + LV_FONT_GLYPH_FORMAT_SVG = 0x0B, /**< SVG format*/ + LV_FONT_GLYPH_FORMAT_CUSTOM = 0xFF, /**< Custom format*/ +} lv_font_glyph_format_t; + /** Describes the properties of a glyph.*/ typedef struct { - const struct _lv_font_t * - resolved_font; /**< Pointer to a font where the glyph was actually found after handling fallbacks*/ + const lv_font_t * + resolved_font; /**< Pointer to a font where the glyph was actually found after handling fallbacks*/ uint16_t adv_w; /**< The glyph needs this space. Draw the next glyph after this width.*/ uint16_t box_w; /**< Width of the glyph's bounding box*/ uint16_t box_h; /**< Height of the glyph's bounding box*/ int16_t ofs_x; /**< x offset of the bounding box*/ int16_t ofs_y; /**< y offset of the bounding box*/ - uint8_t bpp: 4; /**< Bit-per-pixel: 1, 2, 4, 8*/ - uint8_t is_placeholder: 1; /** Glyph is missing. But placeholder will still be displayed */ + lv_font_glyph_format_t format; /**< Font format of the glyph see lv_font_glyph_format_t */ + uint8_t is_placeholder: 1; /**< Glyph is missing. But placeholder will still be displayed*/ + + union { + uint32_t index; /**< Unicode code point*/ + const void * src; /**< Pointer to the source data used by image fonts*/ + } gid; /**< The index of the glyph in the font file. Used by the font cache*/ + lv_cache_entry_t * entry; /**< The cache entry of the glyph draw data. Used by the font cache*/ } lv_font_glyph_dsc_t; /** The bitmaps might be upscaled by 3 to achieve subpixel rendering.*/ -enum { +typedef enum { LV_FONT_SUBPX_NONE, LV_FONT_SUBPX_HOR, LV_FONT_SUBPX_VER, LV_FONT_SUBPX_BOTH, -}; +} lv_font_subpx_t; -typedef uint8_t lv_font_subpx_t; +/** Adjust letter spacing for specific character pairs.*/ +typedef enum { + LV_FONT_KERNING_NORMAL, + LV_FONT_KERNING_NONE, +} lv_font_kerning_t; /** Describe the properties of a font*/ -typedef struct _lv_font_t { +struct _lv_font_t { /** Get a glyph's descriptor from a font*/ - bool (*get_glyph_dsc)(const struct _lv_font_t *, lv_font_glyph_dsc_t *, uint32_t letter, uint32_t letter_next); + bool (*get_glyph_dsc)(const lv_font_t *, lv_font_glyph_dsc_t *, uint32_t letter, uint32_t letter_next); /** Get a glyph's bitmap from a font*/ - const uint8_t * (*get_glyph_bitmap)(const struct _lv_font_t *, uint32_t); + const void * (*get_glyph_bitmap)(lv_font_glyph_dsc_t *, lv_draw_buf_t *); + + /** Release a glyph*/ + void (*release_glyph)(const lv_font_t *, lv_font_glyph_dsc_t *); /*Pointer to the font in a font pack (must have the same line height)*/ - lv_coord_t line_height; /**< The real line height where any text fits*/ - lv_coord_t base_line; /**< Base line measured from the top of the line_height*/ - uint8_t subpx : 2; /**< An element of `lv_font_subpx_t`*/ + int32_t line_height; /**< The real line height where any text fits*/ + int32_t base_line; /**< Base line measured from the bottom of the line_height*/ + uint8_t subpx : 2; /**< An element of `lv_font_subpx_t`*/ + uint8_t kerning : 1; /**< An element of `lv_font_kerning_t`*/ int8_t underline_position; /**< Distance between the top of the underline and base line (< 0 means below the base line)*/ int8_t underline_thickness; /**< Thickness of the underline*/ const void * dsc; /**< Store implementation specific or run_time data or caching here*/ - const struct _lv_font_t * fallback; /**< Fallback font for missing glyph. Resolved recursively */ -#if LV_USE_USER_DATA + const lv_font_t * fallback; /**< Fallback font for missing glyph. Resolved recursively */ void * user_data; /**< Custom user data for font.*/ -#endif -} lv_font_t; +}; /********************** * GLOBAL PROTOTYPES @@ -89,48 +117,60 @@ typedef struct _lv_font_t { /** * Return with the bitmap of a font. - * @param font_p pointer to a font - * @param letter a UNICODE character code - * @return pointer to the bitmap of the letter + * @note You must call lv_font_get_glyph_dsc() to get `g_dsc` (lv_font_glyph_dsc_t) before you can call this function. + * @param g_dsc the glyph descriptor including which font to use, which supply the glyph_index and the format. + * @param draw_buf a draw buffer that can be used to store the bitmap of the glyph, it's OK not to use it. + * @return pointer to the glyph's data. It can be a draw buffer for bitmap fonts or an image source for imgfonts. */ -const uint8_t * lv_font_get_glyph_bitmap(const lv_font_t * font_p, uint32_t letter); +const void * lv_font_get_glyph_bitmap(lv_font_glyph_dsc_t * g_dsc, lv_draw_buf_t * draw_buf); /** * Get the descriptor of a glyph - * @param font_p pointer to font - * @param dsc_out store the result descriptor here - * @param letter a UNICODE letter code - * @param letter_next the next letter after `letter`. Used for kerning + * @param font pointer to font + * @param dsc_out store the result descriptor here + * @param letter a UNICODE letter code + * @param letter_next the next letter after `letter`. Used for kerning * @return true: descriptor is successfully loaded into `dsc_out`. * false: the letter was not found, no data is loaded to `dsc_out` */ -bool lv_font_get_glyph_dsc(const lv_font_t * font_p, lv_font_glyph_dsc_t * dsc_out, uint32_t letter, +bool lv_font_get_glyph_dsc(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out, uint32_t letter, uint32_t letter_next); +/** + * Release the bitmap of a font. + * @note You must call lv_font_get_glyph_dsc() to get `g_dsc` (lv_font_glyph_dsc_t) before you can call this function. + * @param g_dsc the glyph descriptor including which font to use, which supply the glyph_index and the format. + */ +void lv_font_glyph_release_draw_data(lv_font_glyph_dsc_t * g_dsc); + /** * Get the width of a glyph with kerning - * @param font pointer to a font - * @param letter a UNICODE letter - * @param letter_next the next letter after `letter`. Used for kerning + * @param font pointer to a font + * @param letter a UNICODE letter + * @param letter_next the next letter after `letter`. Used for kerning * @return the width of the glyph */ uint16_t lv_font_get_glyph_width(const lv_font_t * font, uint32_t letter, uint32_t letter_next); /** * Get the line height of a font. All characters fit into this height - * @param font_p pointer to a font + * @param font pointer to a font * @return the height of a font */ -static inline lv_coord_t lv_font_get_line_height(const lv_font_t * font_p) -{ - return font_p->line_height; -} +int32_t lv_font_get_line_height(const lv_font_t * font); + +/** + * Configure the use of kerning information stored in a font + * @param font pointer to a font + * @param kerning `LV_FONT_KERNING_NORMAL` (default) or `LV_FONT_KERNING_NONE` + */ +void lv_font_set_kerning(lv_font_t * font, lv_font_kerning_t kerning); /********************** * MACROS **********************/ -#define LV_FONT_DECLARE(font_name) extern const lv_font_t font_name; +#define LV_FONT_DECLARE(font_name) LV_ATTRIBUTE_EXTERN_DATA extern const lv_font_t font_name; #if LV_FONT_MONTSERRAT_8 LV_FONT_DECLARE(lv_font_montserrat_8) @@ -216,10 +256,6 @@ LV_FONT_DECLARE(lv_font_montserrat_46) LV_FONT_DECLARE(lv_font_montserrat_48) #endif -#if LV_FONT_MONTSERRAT_12_SUBPX -LV_FONT_DECLARE(lv_font_montserrat_12_subpx) -#endif - #if LV_FONT_MONTSERRAT_28_COMPRESSED LV_FONT_DECLARE(lv_font_montserrat_28_compressed) #endif @@ -228,6 +264,10 @@ LV_FONT_DECLARE(lv_font_montserrat_28_compressed) LV_FONT_DECLARE(lv_font_dejavu_16_persian_hebrew) #endif +#if LV_FONT_SIMSUN_14_CJK +LV_FONT_DECLARE(lv_font_simsun_14_cjk) +#endif + #if LV_FONT_SIMSUN_16_CJK LV_FONT_DECLARE(lv_font_simsun_16_cjk) #endif @@ -275,10 +315,7 @@ LV_FONT_DECLARE(pros_font_dejavu_mono_40_latin_sup); * Just a wrapper around LV_FONT_DEFAULT because it might be more convenient to use a function in some cases * @return pointer to LV_FONT_DEFAULT */ -static inline const lv_font_t * lv_font_default(void) -{ - return LV_FONT_DEFAULT; -} +const lv_font_t * lv_font_default(void); #ifdef __cplusplus } /*extern "C"*/ diff --git a/include/liblvgl/font/lv_font_fmt_txt.h b/include/liblvgl/font/lv_font_fmt_txt.h index 86546a35..4eeb0c6e 100644 --- a/include/liblvgl/font/lv_font_fmt_txt.h +++ b/include/liblvgl/font/lv_font_fmt_txt.h @@ -13,10 +13,8 @@ extern "C" { /********************* * INCLUDES *********************/ -#include -#include -#include #include "lv_font.h" +#include "../misc/lv_types.h" /********************* * DEFINES @@ -46,14 +44,12 @@ typedef struct { } lv_font_fmt_txt_glyph_dsc_t; /** Format of font character map.*/ -enum { +typedef enum { LV_FONT_FMT_TXT_CMAP_FORMAT0_FULL, LV_FONT_FMT_TXT_CMAP_SPARSE_FULL, LV_FONT_FMT_TXT_CMAP_FORMAT0_TINY, LV_FONT_FMT_TXT_CMAP_SPARSE_TINY, -}; - -typedef uint8_t lv_font_fmt_txt_cmap_type_t; +} lv_font_fmt_txt_cmap_type_t; /** * Map codepoints to a `glyph_dsc`s @@ -117,14 +113,14 @@ typedef struct { /*To get a kern value of two code points: 1. Get the `glyph_id_left` and `glyph_id_right` from `lv_font_fmt_txt_cmap_t 2. for(i = 0; i < pair_cnt * 2; i += 2) - if(gylph_ids[i] == glyph_id_left && - gylph_ids[i+1] == glyph_id_right) + if(glyph_ids[i] == glyph_id_left && + glyph_ids[i+1] == glyph_id_right) return values[i / 2]; */ const void * glyph_ids; const int8_t * values; uint32_t pair_cnt : 30; - uint32_t glyph_ids_size : 2; /*0: `glyph_ids` is stored as `uint8_t`; 1: as `uint16_t`*/ + uint32_t glyph_ids_size : 2; /**< 0: `glyph_ids` is stored as `uint8_t`; 1: as `uint16_t` */ } lv_font_fmt_txt_kern_pair_t; /** More complex but more optimal class based kern value storage*/ @@ -137,9 +133,9 @@ typedef struct { 3. value = class_pair_values[(left_class-1)*right_class_cnt + (right_class-1)] */ - const int8_t * class_pair_values; /*left_class_cnt * right_class_cnt value*/ - const uint8_t * left_class_mapping; /*Map the glyph_ids to classes: index -> glyph_id -> class_id*/ - const uint8_t * right_class_mapping; /*Map the glyph_ids to classes: index -> glyph_id -> class_id*/ + const int8_t * class_pair_values; /**< left_class_cnt * right_class_cnt value */ + const uint8_t * left_class_mapping; /**< Map the glyph_ids to classes: index -> glyph_id -> class_id */ + const uint8_t * right_class_mapping; /**< Map the glyph_ids to classes: index -> glyph_id -> class_id */ uint8_t left_class_cnt; uint8_t right_class_cnt; } lv_font_fmt_txt_kern_classes_t; @@ -151,21 +147,16 @@ typedef enum { LV_FONT_FMT_TXT_COMPRESSED_NO_PREFILTER = 1, } lv_font_fmt_txt_bitmap_format_t; +/** Describe store for additional data for fonts */ typedef struct { - uint32_t last_letter; - uint32_t last_glyph_id; -} lv_font_fmt_txt_glyph_cache_t; - -/*Describe store additional data for fonts*/ -typedef struct { - /*The bitmaps of all glyphs*/ + /** The bitmaps of all glyphs */ const uint8_t * glyph_bitmap; - /*Describe the glyphs*/ + /** Describe the glyphs */ const lv_font_fmt_txt_glyph_dsc_t * glyph_dsc; - /*Map the glyphs to Unicode characters. - *Array of `lv_font_cmap_fmt_txt_t` variables*/ + /** Map the glyphs to Unicode characters. + *Array of `lv_font_cmap_fmt_txt_t` variables */ const lv_font_fmt_txt_cmap_t * cmaps; /** @@ -175,26 +166,23 @@ typedef struct { */ const void * kern_dsc; - /*Scale kern values in 12.4 format*/ + /** Scale kern values in 12.4 format */ uint16_t kern_scale; - /*Number of cmap tables*/ + /** Number of cmap tables */ uint16_t cmap_num : 9; - /*Bit per pixel: 1, 2, 3, 4, 8*/ + /** Bit per pixel: 1, 2, 3, 4, 8 */ uint16_t bpp : 4; - /*Type of `kern_dsc`*/ + /** Type of `kern_dsc` */ uint16_t kern_classes : 1; - /* + /** * storage format of the bitmap * from `lv_font_fmt_txt_bitmap_format_t` */ uint16_t bitmap_format : 2; - - /*Cache the last letter and is glyph id*/ - lv_font_fmt_txt_glyph_cache_t * cache; } lv_font_fmt_txt_dsc_t; /********************** @@ -202,29 +190,25 @@ typedef struct { **********************/ /** - * Used as `get_glyph_bitmap` callback in LittelvGL's native font format if the font is uncompressed. - * @param font pointer to font - * @param unicode_letter a unicode letter which bitmap should be get - * @return pointer to the bitmap or NULL if not found + * Used as `get_glyph_bitmap` callback in lvgl's native font format if the font is uncompressed. + * @param g_dsc the glyph descriptor including which font to use, which supply the glyph_index and format. + * @param draw_buf a draw buffer that can be used to store the bitmap of the glyph, it's OK not to use it. + * @return pointer to an A8 bitmap (not necessarily bitmap_out) or NULL if `unicode_letter` not found */ -const uint8_t * lv_font_get_bitmap_fmt_txt(const lv_font_t * font, uint32_t letter); +const void * lv_font_get_bitmap_fmt_txt(lv_font_glyph_dsc_t * g_dsc, lv_draw_buf_t * draw_buf); /** - * Used as `get_glyph_dsc` callback in LittelvGL's native font format if the font is uncompressed. - * @param font_p pointer to font + * Used as `get_glyph_dsc` callback in lvgl's native font format if the font is uncompressed. + * @param font pointer to font * @param dsc_out store the result descriptor here - * @param letter a UNICODE letter code + * @param unicode_letter a UNICODE letter code + * @param unicode_letter_next the unicode letter succeeding the letter under test * @return true: descriptor is successfully loaded into `dsc_out`. * false: the letter was not found, no data is loaded to `dsc_out` */ bool lv_font_get_glyph_dsc_fmt_txt(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out, uint32_t unicode_letter, uint32_t unicode_letter_next); -/** - * Free the allocated memories. - */ -void _lv_font_clean_up_fmt_txt(void); - /********************** * MACROS **********************/ diff --git a/include/liblvgl/font/lv_font_fmt_txt_private.h b/include/liblvgl/font/lv_font_fmt_txt_private.h new file mode 100644 index 00000000..66df402d --- /dev/null +++ b/include/liblvgl/font/lv_font_fmt_txt_private.h @@ -0,0 +1,56 @@ +/** + * @file lv_font_fmt_txt_private.h + * + */ + +#ifndef LV_FONT_FMT_TXT_PRIVATE_H +#define LV_FONT_FMT_TXT_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_font_fmt_txt.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +#if LV_USE_FONT_COMPRESSED +typedef enum { + RLE_STATE_SINGLE = 0, + RLE_STATE_REPEATED, + RLE_STATE_COUNTER, +} lv_font_fmt_rle_state_t; + +typedef struct { + uint32_t rdp; + const uint8_t * in; + uint8_t bpp; + uint8_t prev_v; + uint8_t count; + lv_font_fmt_rle_state_t state; +} lv_font_fmt_rle_t; +#endif + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_FONT_FMT_TXT_PRIVATE_H*/ diff --git a/include/liblvgl/font/lv_symbol_def.h b/include/liblvgl/font/lv_symbol_def.h index 1055392d..d78f3892 100644 --- a/include/liblvgl/font/lv_symbol_def.h +++ b/include/liblvgl/font/lv_symbol_def.h @@ -1,3 +1,8 @@ +/** + * @file lv_symbol_def.h + * + */ + #ifndef LV_SYMBOL_DEF_H #define LV_SYMBOL_DEF_H @@ -5,7 +10,7 @@ extern "C" { #endif -#include "liblvgl/lv_conf_internal.h" +#include "../lv_conf_internal.h" /*------------------------------- * Symbols from "normal" font @@ -27,11 +32,10 @@ extern "C" { 62018, 62019, 62020, 62087, 62099, 62189, 62212, 62810, 63426, 63650 */ -/* These symbols can be prefined in the lv_conf.h file. +/* These symbols can be predefined in the lv_conf.h file. * If they are not predefined, they will use the following values */ - #if !defined LV_SYMBOL_AUDIO #define LV_SYMBOL_AUDIO "\xEF\x80\x81" /*61441, 0xF001*/ #endif @@ -279,71 +283,71 @@ extern "C" { /* * The following list is generated using - * cat src/font/lv_symbol_def.h | sed -E -n 's/^#define\s+LV_(SYMBOL_\w+).*".*$/ _LV_STR_\1,/p' + * cat src/font/lv_symbol_def.h | sed -E -n 's/^#define\s+LV_(SYMBOL_\w+).*".*$/ LV_STR_\1,/p' */ enum { - _LV_STR_SYMBOL_BULLET, - _LV_STR_SYMBOL_AUDIO, - _LV_STR_SYMBOL_VIDEO, - _LV_STR_SYMBOL_LIST, - _LV_STR_SYMBOL_OK, - _LV_STR_SYMBOL_CLOSE, - _LV_STR_SYMBOL_POWER, - _LV_STR_SYMBOL_SETTINGS, - _LV_STR_SYMBOL_HOME, - _LV_STR_SYMBOL_DOWNLOAD, - _LV_STR_SYMBOL_DRIVE, - _LV_STR_SYMBOL_REFRESH, - _LV_STR_SYMBOL_MUTE, - _LV_STR_SYMBOL_VOLUME_MID, - _LV_STR_SYMBOL_VOLUME_MAX, - _LV_STR_SYMBOL_IMAGE, - _LV_STR_SYMBOL_TINT, - _LV_STR_SYMBOL_PREV, - _LV_STR_SYMBOL_PLAY, - _LV_STR_SYMBOL_PAUSE, - _LV_STR_SYMBOL_STOP, - _LV_STR_SYMBOL_NEXT, - _LV_STR_SYMBOL_EJECT, - _LV_STR_SYMBOL_LEFT, - _LV_STR_SYMBOL_RIGHT, - _LV_STR_SYMBOL_PLUS, - _LV_STR_SYMBOL_MINUS, - _LV_STR_SYMBOL_EYE_OPEN, - _LV_STR_SYMBOL_EYE_CLOSE, - _LV_STR_SYMBOL_WARNING, - _LV_STR_SYMBOL_SHUFFLE, - _LV_STR_SYMBOL_UP, - _LV_STR_SYMBOL_DOWN, - _LV_STR_SYMBOL_LOOP, - _LV_STR_SYMBOL_DIRECTORY, - _LV_STR_SYMBOL_UPLOAD, - _LV_STR_SYMBOL_CALL, - _LV_STR_SYMBOL_CUT, - _LV_STR_SYMBOL_COPY, - _LV_STR_SYMBOL_SAVE, - _LV_STR_SYMBOL_BARS, - _LV_STR_SYMBOL_ENVELOPE, - _LV_STR_SYMBOL_CHARGE, - _LV_STR_SYMBOL_PASTE, - _LV_STR_SYMBOL_BELL, - _LV_STR_SYMBOL_KEYBOARD, - _LV_STR_SYMBOL_GPS, - _LV_STR_SYMBOL_FILE, - _LV_STR_SYMBOL_WIFI, - _LV_STR_SYMBOL_BATTERY_FULL, - _LV_STR_SYMBOL_BATTERY_3, - _LV_STR_SYMBOL_BATTERY_2, - _LV_STR_SYMBOL_BATTERY_1, - _LV_STR_SYMBOL_BATTERY_EMPTY, - _LV_STR_SYMBOL_USB, - _LV_STR_SYMBOL_BLUETOOTH, - _LV_STR_SYMBOL_TRASH, - _LV_STR_SYMBOL_EDIT, - _LV_STR_SYMBOL_BACKSPACE, - _LV_STR_SYMBOL_SD_CARD, - _LV_STR_SYMBOL_NEW_LINE, - _LV_STR_SYMBOL_DUMMY, + LV_STR_SYMBOL_BULLET, + LV_STR_SYMBOL_AUDIO, + LV_STR_SYMBOL_VIDEO, + LV_STR_SYMBOL_LIST, + LV_STR_SYMBOL_OK, + LV_STR_SYMBOL_CLOSE, + LV_STR_SYMBOL_POWER, + LV_STR_SYMBOL_SETTINGS, + LV_STR_SYMBOL_HOME, + LV_STR_SYMBOL_DOWNLOAD, + LV_STR_SYMBOL_DRIVE, + LV_STR_SYMBOL_REFRESH, + LV_STR_SYMBOL_MUTE, + LV_STR_SYMBOL_VOLUME_MID, + LV_STR_SYMBOL_VOLUME_MAX, + LV_STR_SYMBOL_IMAGE, + LV_STR_SYMBOL_TINT, + LV_STR_SYMBOL_PREV, + LV_STR_SYMBOL_PLAY, + LV_STR_SYMBOL_PAUSE, + LV_STR_SYMBOL_STOP, + LV_STR_SYMBOL_NEXT, + LV_STR_SYMBOL_EJECT, + LV_STR_SYMBOL_LEFT, + LV_STR_SYMBOL_RIGHT, + LV_STR_SYMBOL_PLUS, + LV_STR_SYMBOL_MINUS, + LV_STR_SYMBOL_EYE_OPEN, + LV_STR_SYMBOL_EYE_CLOSE, + LV_STR_SYMBOL_WARNING, + LV_STR_SYMBOL_SHUFFLE, + LV_STR_SYMBOL_UP, + LV_STR_SYMBOL_DOWN, + LV_STR_SYMBOL_LOOP, + LV_STR_SYMBOL_DIRECTORY, + LV_STR_SYMBOL_UPLOAD, + LV_STR_SYMBOL_CALL, + LV_STR_SYMBOL_CUT, + LV_STR_SYMBOL_COPY, + LV_STR_SYMBOL_SAVE, + LV_STR_SYMBOL_BARS, + LV_STR_SYMBOL_ENVELOPE, + LV_STR_SYMBOL_CHARGE, + LV_STR_SYMBOL_PASTE, + LV_STR_SYMBOL_BELL, + LV_STR_SYMBOL_KEYBOARD, + LV_STR_SYMBOL_GPS, + LV_STR_SYMBOL_FILE, + LV_STR_SYMBOL_WIFI, + LV_STR_SYMBOL_BATTERY_FULL, + LV_STR_SYMBOL_BATTERY_3, + LV_STR_SYMBOL_BATTERY_2, + LV_STR_SYMBOL_BATTERY_1, + LV_STR_SYMBOL_BATTERY_EMPTY, + LV_STR_SYMBOL_USB, + LV_STR_SYMBOL_BLUETOOTH, + LV_STR_SYMBOL_TRASH, + LV_STR_SYMBOL_EDIT, + LV_STR_SYMBOL_BACKSPACE, + LV_STR_SYMBOL_SD_CARD, + LV_STR_SYMBOL_NEW_LINE, + LV_STR_SYMBOL_DUMMY, }; #ifdef __cplusplus diff --git a/include/liblvgl/hal/lv_hal.h b/include/liblvgl/hal/lv_hal.h deleted file mode 100644 index 167da1f4..00000000 --- a/include/liblvgl/hal/lv_hal.h +++ /dev/null @@ -1,48 +0,0 @@ -/** - * @file lv_hal.h - * - */ - -#ifndef LV_HAL_H -#define LV_HAL_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "lv_hal_disp.h" -#include "lv_hal_indev.h" -#include "lv_hal_tick.h" - -/********************* - * DEFINES - *********************/ -/** - * Same as Android's DIP. (Different name is chosen to avoid mistype between LV_DPI and LV_DIP) - * 1 dip is 1 px on a 160 DPI screen - * 1 dip is 2 px on a 320 DPI screen - * https://stackoverflow.com/questions/2025282/what-is-the-difference-between-px-dip-dp-and-sp - */ -#define _LV_DPX_CALC(dpi, n) ((n) == 0 ? 0 :LV_MAX((( (dpi) * (n) + 80) / 160), 1)) /*+80 for rounding*/ -#define LV_DPX(n) _LV_DPX_CALC(lv_disp_get_dpi(NULL), n) - -/********************** - * TYPEDEFS - **********************/ - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/********************** - * MACROS - **********************/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif diff --git a/include/liblvgl/hal/lv_hal_disp.h b/include/liblvgl/hal/lv_hal_disp.h deleted file mode 100644 index 5b36916c..00000000 --- a/include/liblvgl/hal/lv_hal_disp.h +++ /dev/null @@ -1,371 +0,0 @@ -/** - * @file lv_hal_disp.h - * - * @description Display Driver HAL interface header file - * - */ - -#ifndef LV_HAL_DISP_H -#define LV_HAL_DISP_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include -#include -#include "lv_hal.h" -#include "liblvgl/draw/lv_draw.h" -#include "liblvgl/misc/lv_color.h" -#include "liblvgl/misc/lv_area.h" -#include "liblvgl/misc/lv_ll.h" -#include "liblvgl/misc/lv_timer.h" - -/********************* - * DEFINES - *********************/ -#ifndef LV_INV_BUF_SIZE -#define LV_INV_BUF_SIZE 32 /*Buffer size for invalid areas*/ -#endif - -#ifndef LV_ATTRIBUTE_FLUSH_READY -#define LV_ATTRIBUTE_FLUSH_READY -#endif - -/********************** - * TYPEDEFS - **********************/ - -struct _lv_obj_t; -struct _lv_disp_t; -struct _lv_disp_drv_t; -struct _lv_theme_t; - -/** - * Structure for holding display buffer information. - */ -typedef struct _lv_disp_draw_buf_t { - void * buf1; /**< First display buffer.*/ - void * buf2; /**< Second display buffer.*/ - - /*Internal, used by the library*/ - void * buf_act; - uint32_t size; /*In pixel count*/ - /*1: flushing is in progress. (It can't be a bit field because when it's cleared from IRQ Read-Modify-Write issue might occur)*/ - volatile int flushing; - /*1: It was the last chunk to flush. (It can't be a bit field because when it's cleared from IRQ Read-Modify-Write issue might occur)*/ - volatile int flushing_last; - volatile uint32_t last_area : 1; /*1: the last area is being rendered*/ - volatile uint32_t last_part : 1; /*1: the last part of the current area is being rendered*/ -} lv_disp_draw_buf_t; - -typedef enum { - LV_DISP_ROT_NONE = 0, - LV_DISP_ROT_90, - LV_DISP_ROT_180, - LV_DISP_ROT_270 -} lv_disp_rot_t; - -/** - * Display Driver structure to be registered by HAL. - * Only its pointer will be saved in `lv_disp_t` so it should be declared as - * `static lv_disp_drv_t my_drv` or allocated dynamically. - */ -typedef struct _lv_disp_drv_t { - - lv_coord_t hor_res; /**< Horizontal resolution.*/ - lv_coord_t ver_res; /**< Vertical resolution.*/ - - lv_coord_t - physical_hor_res; /**< Horizontal resolution of the full / physical display. Set to -1 for fullscreen mode.*/ - lv_coord_t - physical_ver_res; /**< Vertical resolution of the full / physical display. Set to -1 for fullscreen mode.*/ - lv_coord_t - offset_x; /**< Horizontal offset from the full / physical display. Set to 0 for fullscreen mode.*/ - lv_coord_t offset_y; /**< Vertical offset from the full / physical display. Set to 0 for fullscreen mode.*/ - - /** Pointer to a buffer initialized with `lv_disp_draw_buf_init()`. - * LVGL will use this buffer(s) to draw the screens contents*/ - lv_disp_draw_buf_t * draw_buf; - - uint32_t direct_mode : 1; /**< 1: Use screen-sized buffers and draw to absolute coordinates*/ - uint32_t full_refresh : 1; /**< 1: Always make the whole screen redrawn*/ - uint32_t sw_rotate : 1; /**< 1: use software rotation (slower)*/ - uint32_t antialiasing : 1; /**< 1: anti-aliasing is enabled on this display.*/ - uint32_t rotated : 2; /**< 1: turn the display by 90 degree. @warning Does not update coordinates for you!*/ - uint32_t screen_transp : 1; /**Handle if the screen doesn't have a solid (opa == LV_OPA_COVER) background. - * Use only if required because it's slower.*/ - - uint32_t dpi : 10; /** DPI (dot per inch) of the display. Default value is `LV_DPI_DEF`.*/ - - /** MANDATORY: Write the internal buffer (draw_buf) to the display. 'lv_disp_flush_ready()' has to be - * called when finished*/ - void (*flush_cb)(struct _lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p); - - /** OPTIONAL: Extend the invalidated areas to match with the display drivers requirements - * E.g. round `y` to, 8, 16 ..) on a monochrome display*/ - void (*rounder_cb)(struct _lv_disp_drv_t * disp_drv, lv_area_t * area); - - /** OPTIONAL: Set a pixel in a buffer according to the special requirements of the display - * Can be used for color format not supported in LittelvGL. E.g. 2 bit -> 4 gray scales - * @note Much slower then drawing with supported color formats.*/ - void (*set_px_cb)(struct _lv_disp_drv_t * disp_drv, uint8_t * buf, lv_coord_t buf_w, lv_coord_t x, lv_coord_t y, - lv_color_t color, lv_opa_t opa); - - void (*clear_cb)(struct _lv_disp_drv_t * disp_drv, uint8_t * buf, uint32_t size); - - - /** OPTIONAL: Called after every refresh cycle to tell the rendering and flushing time + the - * number of flushed pixels*/ - void (*monitor_cb)(struct _lv_disp_drv_t * disp_drv, uint32_t time, uint32_t px); - - /** OPTIONAL: Called periodically while lvgl waits for operation to be completed. - * For example flushing or GPU - * User can execute very simple tasks here or yield the task*/ - void (*wait_cb)(struct _lv_disp_drv_t * disp_drv); - - /** OPTIONAL: Called when lvgl needs any CPU cache that affects rendering to be cleaned*/ - void (*clean_dcache_cb)(struct _lv_disp_drv_t * disp_drv); - - /** OPTIONAL: called when driver parameters are updated */ - void (*drv_update_cb)(struct _lv_disp_drv_t * disp_drv); - - /** OPTIONAL: called when start rendering */ - void (*render_start_cb)(struct _lv_disp_drv_t * disp_drv); - - /** On CHROMA_KEYED images this color will be transparent. - * `LV_COLOR_CHROMA_KEY` by default. (lv_conf.h)*/ - lv_color_t color_chroma_key; - - lv_draw_ctx_t * draw_ctx; - void (*draw_ctx_init)(struct _lv_disp_drv_t * disp_drv, lv_draw_ctx_t * draw_ctx); - void (*draw_ctx_deinit)(struct _lv_disp_drv_t * disp_drv, lv_draw_ctx_t * draw_ctx); - size_t draw_ctx_size; - -#if LV_USE_USER_DATA - void * user_data; /**< Custom display driver user data*/ -#endif - -} lv_disp_drv_t; - -/** - * Display structure. - * @note `lv_disp_drv_t` should be the first member of the structure. - */ -typedef struct _lv_disp_t { - /**< Driver to the display*/ - struct _lv_disp_drv_t * driver; - - /**< A timer which periodically checks the dirty areas and refreshes them*/ - lv_timer_t * refr_timer; - - /**< The theme assigned to the screen*/ - struct _lv_theme_t * theme; - - /** Screens of the display*/ - struct _lv_obj_t ** screens; /**< Array of screen objects.*/ - struct _lv_obj_t * act_scr; /**< Currently active screen on this display*/ - struct _lv_obj_t * prev_scr; /**< Previous screen. Used during screen animations*/ - struct _lv_obj_t * scr_to_load; /**< The screen prepared to load in lv_scr_load_anim*/ - struct _lv_obj_t * top_layer; /**< @see lv_disp_get_layer_top*/ - struct _lv_obj_t * sys_layer; /**< @see lv_disp_get_layer_sys*/ - uint32_t screen_cnt; -uint8_t draw_prev_over_act : - 1; /**< 1: Draw previous screen over active screen*/ -uint8_t del_prev : - 1; /**< 1: Automatically delete the previous screen when the screen load animation is ready*/ - uint8_t rendering_in_progress : 1; /**< 1: The current screen rendering is in progress*/ - - lv_opa_t bg_opa; /**flush` you should use DMA or similar hardware to send - * the image to the display in the background. - * It lets LVGL to render next frame into the other buffer while previous is being - * sent. Set to `NULL` if unused. - * @param size_in_px_cnt size of the `buf1` and `buf2` in pixel count. - */ -void lv_disp_draw_buf_init(lv_disp_draw_buf_t * draw_buf, void * buf1, void * buf2, uint32_t size_in_px_cnt); - -/** - * Register an initialized display driver. - * Automatically set the first display as active. - * @param driver pointer to an initialized 'lv_disp_drv_t' variable. Only its pointer is saved! - * @return pointer to the new display or NULL on error - */ -lv_disp_t * lv_disp_drv_register(lv_disp_drv_t * driver); - -/** - * Update the driver in run time. - * @param disp pointer to a display. (return value of `lv_disp_drv_register`) - * @param new_drv pointer to the new driver - */ -void lv_disp_drv_update(lv_disp_t * disp, lv_disp_drv_t * new_drv); - -/** - * Remove a display - * @param disp pointer to display - */ -void lv_disp_remove(lv_disp_t * disp); - -/** - * Set a default display. The new screens will be created on it by default. - * @param disp pointer to a display - */ -void lv_disp_set_default(lv_disp_t * disp); - -/** - * Get the default display - * @return pointer to the default display - */ -lv_disp_t * lv_disp_get_default(void); - -/** - * Get the horizontal resolution of a display - * @param disp pointer to a display (NULL to use the default display) - * @return the horizontal resolution of the display - */ -lv_coord_t lv_disp_get_hor_res(lv_disp_t * disp); - -/** - * Get the vertical resolution of a display - * @param disp pointer to a display (NULL to use the default display) - * @return the vertical resolution of the display - */ -lv_coord_t lv_disp_get_ver_res(lv_disp_t * disp); - -/** - * Get the full / physical horizontal resolution of a display - * @param disp pointer to a display (NULL to use the default display) - * @return the full / physical horizontal resolution of the display - */ -lv_coord_t lv_disp_get_physical_hor_res(lv_disp_t * disp); - -/** - * Get the full / physical vertical resolution of a display - * @param disp pointer to a display (NULL to use the default display) - * @return the full / physical vertical resolution of the display - */ -lv_coord_t lv_disp_get_physical_ver_res(lv_disp_t * disp); - -/** - * Get the horizontal offset from the full / physical display - * @param disp pointer to a display (NULL to use the default display) - * @return the horizontal offset from the full / physical display - */ -lv_coord_t lv_disp_get_offset_x(lv_disp_t * disp); - -/** - * Get the vertical offset from the full / physical display - * @param disp pointer to a display (NULL to use the default display) - * @return the horizontal offset from the full / physical display - */ -lv_coord_t lv_disp_get_offset_y(lv_disp_t * disp); - -/** - * Get if anti-aliasing is enabled for a display or not - * @param disp pointer to a display (NULL to use the default display) - * @return true: anti-aliasing is enabled; false: disabled - */ -bool lv_disp_get_antialiasing(lv_disp_t * disp); - -/** - * Get the DPI of the display - * @param disp pointer to a display (NULL to use the default display) - * @return dpi of the display - */ -lv_coord_t lv_disp_get_dpi(const lv_disp_t * disp); - - -/** - * Set the rotation of this display. - * @param disp pointer to a display (NULL to use the default display) - * @param rotation rotation angle - */ -void lv_disp_set_rotation(lv_disp_t * disp, lv_disp_rot_t rotation); - -/** - * Get the current rotation of this display. - * @param disp pointer to a display (NULL to use the default display) - * @return rotation angle - */ -lv_disp_rot_t lv_disp_get_rotation(lv_disp_t * disp); - -//! @cond Doxygen_Suppress - -/** - * Call in the display driver's `flush_cb` function when the flushing is finished - * @param disp_drv pointer to display driver in `flush_cb` where this function is called - */ -LV_ATTRIBUTE_FLUSH_READY void lv_disp_flush_ready(lv_disp_drv_t * disp_drv); - -/** - * Tell if it's the last area of the refreshing process. - * Can be called from `flush_cb` to execute some special display refreshing if needed when all areas area flushed. - * @param disp_drv pointer to display driver - * @return true: it's the last area to flush; false: there are other areas too which will be refreshed soon - */ -LV_ATTRIBUTE_FLUSH_READY bool lv_disp_flush_is_last(lv_disp_drv_t * disp_drv); - -//! @endcond - -/** - * Get the next display. - * @param disp pointer to the current display. NULL to initialize. - * @return the next display or NULL if no more. Give the first display when the parameter is NULL - */ -lv_disp_t * lv_disp_get_next(lv_disp_t * disp); - -/** - * Get the internal buffer of a display - * @param disp pointer to a display - * @return pointer to the internal buffers - */ -lv_disp_draw_buf_t * lv_disp_get_draw_buf(lv_disp_t * disp); - -void lv_disp_drv_use_generic_set_px_cb(lv_disp_drv_t * disp_drv, lv_img_cf_t cf); - -/********************** - * MACROS - **********************/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif diff --git a/include/liblvgl/hal/lv_hal_indev.h b/include/liblvgl/hal/lv_hal_indev.h deleted file mode 100644 index 630d471a..00000000 --- a/include/liblvgl/hal/lv_hal_indev.h +++ /dev/null @@ -1,239 +0,0 @@ -/** - * @file lv_hal_indev.h - * - * @description Input Device HAL interface layer header file - * - */ - -#ifndef LV_HAL_INDEV_H -#define LV_HAL_INDEV_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lv_conf_internal.h" - -#include -#include -#include "liblvgl/misc/lv_area.h" -#include "liblvgl/misc/lv_timer.h" - -/********************* - * DEFINES - *********************/ - -/*Drag threshold in pixels*/ -#define LV_INDEV_DEF_SCROLL_LIMIT 10 - -/*Drag throw slow-down in [%]. Greater value -> faster slow-down*/ -#define LV_INDEV_DEF_SCROLL_THROW 10 - -/*Long press time in milliseconds. - *Time to send `LV_EVENT_LONG_PRESSSED`)*/ -#define LV_INDEV_DEF_LONG_PRESS_TIME 400 - -/*Repeated trigger period in long press [ms] - *Time between `LV_EVENT_LONG_PRESSED_REPEAT*/ -#define LV_INDEV_DEF_LONG_PRESS_REP_TIME 100 - - -/*Gesture threshold in pixels*/ -#define LV_INDEV_DEF_GESTURE_LIMIT 50 - -/*Gesture min velocity at release before swipe (pixels)*/ -#define LV_INDEV_DEF_GESTURE_MIN_VELOCITY 3 - - -/********************** - * TYPEDEFS - **********************/ - -struct _lv_obj_t; -struct _lv_disp_t; -struct _lv_group_t; -struct _lv_indev_t; -struct _lv_indev_drv_t; - -/** Possible input device types*/ -typedef enum { - LV_INDEV_TYPE_NONE, /**< Uninitialized state*/ - LV_INDEV_TYPE_POINTER, /**< Touch pad, mouse, external button*/ - LV_INDEV_TYPE_KEYPAD, /**< Keypad or keyboard*/ - LV_INDEV_TYPE_BUTTON, /**< External (hardware button) which is assigned to a specific point of the screen*/ - LV_INDEV_TYPE_ENCODER, /**< Encoder with only Left, Right turn and a Button*/ -} lv_indev_type_t; - -/** States for input devices*/ -typedef enum { - LV_INDEV_STATE_RELEASED = 0, - LV_INDEV_STATE_PRESSED -} lv_indev_state_t; - -/** Data structure passed to an input driver to fill*/ -typedef struct { - lv_point_t point; /**< For LV_INDEV_TYPE_POINTER the currently pressed point*/ - uint32_t key; /**< For LV_INDEV_TYPE_KEYPAD the currently pressed key*/ - uint32_t btn_id; /**< For LV_INDEV_TYPE_BUTTON the currently pressed button*/ - int16_t enc_diff; /**< For LV_INDEV_TYPE_ENCODER number of steps since the previous read*/ - - lv_indev_state_t state; /**< LV_INDEV_STATE_REL or LV_INDEV_STATE_PR*/ - bool continue_reading; /**< If set to true, the read callback is invoked again*/ -} lv_indev_data_t; - -/** Initialized by the user and registered by 'lv_indev_add()'*/ -typedef struct _lv_indev_drv_t { - - /**< Input device type*/ - lv_indev_type_t type; - - /**< Function pointer to read input device data.*/ - void (*read_cb)(struct _lv_indev_drv_t * indev_drv, lv_indev_data_t * data); - - /** Called when an action happened on the input device. - * The second parameter is the event from `lv_event_t`*/ - void (*feedback_cb)(struct _lv_indev_drv_t *, uint8_t); - -#if LV_USE_USER_DATA - void * user_data; -#endif - - /**< Pointer to the assigned display*/ - struct _lv_disp_t * disp; - - /**< Timer to periodically read the input device*/ - lv_timer_t * read_timer; - - /**< Number of pixels to slide before actually drag the object*/ - uint8_t scroll_limit; - - /**< Drag throw slow-down in [%]. Greater value means faster slow-down*/ - uint8_t scroll_throw; - - /**< At least this difference should be between two points to evaluate as gesture*/ - uint8_t gesture_min_velocity; - - /**< At least this difference should be to send a gesture*/ - uint8_t gesture_limit; - - /**< Long press time in milliseconds*/ - uint16_t long_press_time; - - /**< Repeated trigger period in long press [ms]*/ - uint16_t long_press_repeat_time; -} lv_indev_drv_t; - -/** Run time data of input devices - * Internally used by the library, you should not need to touch it. - */ -typedef struct _lv_indev_proc_t { - lv_indev_state_t state; /**< Current state of the input device.*/ - /*Flags*/ - uint8_t long_pr_sent : 1; - uint8_t reset_query : 1; - uint8_t disabled : 1; - uint8_t wait_until_release : 1; - - union { - struct { - /*Pointer and button data*/ - lv_point_t act_point; /**< Current point of input device.*/ - lv_point_t last_point; /**< Last point of input device.*/ - lv_point_t last_raw_point; /**< Last point read from read_cb. */ - lv_point_t vect; /**< Difference between `act_point` and `last_point`.*/ - lv_point_t scroll_sum; /*Count the dragged pixels to check LV_INDEV_DEF_SCROLL_LIMIT*/ - lv_point_t scroll_throw_vect; - lv_point_t scroll_throw_vect_ori; - struct _lv_obj_t * act_obj; /*The object being pressed*/ - struct _lv_obj_t * last_obj; /*The last object which was pressed*/ - struct _lv_obj_t * scroll_obj; /*The object being scrolled*/ - struct _lv_obj_t * last_pressed; /*The lastly pressed object*/ - lv_area_t scroll_area; - - lv_point_t gesture_sum; /*Count the gesture pixels to check LV_INDEV_DEF_GESTURE_LIMIT*/ - /*Flags*/ - lv_dir_t scroll_dir : 4; - lv_dir_t gesture_dir : 4; - uint8_t gesture_sent : 1; - } pointer; - struct { - /*Keypad data*/ - lv_indev_state_t last_state; - uint32_t last_key; - } keypad; - } types; - - uint32_t pr_timestamp; /**< Pressed time stamp*/ - uint32_t longpr_rep_timestamp; /**< Long press repeat time stamp*/ -} _lv_indev_proc_t; - -/** The main input device descriptor with driver, runtime data ('proc') and some additional - * information*/ -typedef struct _lv_indev_t { - struct _lv_indev_drv_t * driver; - _lv_indev_proc_t proc; - struct _lv_obj_t * cursor; /**< Cursor for LV_INPUT_TYPE_POINTER*/ - struct _lv_group_t * group; /**< Keypad destination group*/ - const lv_point_t * btn_points; /**< Array points assigned to the button ()screen will be pressed - here by the buttons*/ -} lv_indev_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Initialize an input device driver with default values. - * It is used to surely have known values in the fields and not memory junk. - * After it you can set the fields. - * @param driver pointer to driver variable to initialize - */ -void lv_indev_drv_init(struct _lv_indev_drv_t * driver); - -/** - * Register an initialized input device driver. - * @param driver pointer to an initialized 'lv_indev_drv_t' variable (can be local variable) - * @return pointer to the new input device or NULL on error - */ -lv_indev_t * lv_indev_drv_register(struct _lv_indev_drv_t * driver); - -/** - * Update the driver in run time. - * @param indev pointer to an input device. (return value of `lv_indev_drv_register`) - * @param new_drv pointer to the new driver - */ -void lv_indev_drv_update(lv_indev_t * indev, struct _lv_indev_drv_t * new_drv); - -/** -* Remove the provided input device. Make sure not to use the provided input device afterwards anymore. -* @param indev pointer to delete -*/ -void lv_indev_delete(lv_indev_t * indev); - -/** - * Get the next input device. - * @param indev pointer to the current input device. NULL to initialize. - * @return the next input device or NULL if there are no more. Provide the first input device when - * the parameter is NULL - */ -lv_indev_t * lv_indev_get_next(lv_indev_t * indev); - -/** - * Read data from an input device. - * @param indev pointer to an input device - * @param data input device will write its data here - */ -void _lv_indev_read(lv_indev_t * indev, lv_indev_data_t * data); - -/********************** - * MACROS - **********************/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif diff --git a/include/liblvgl/hal/lv_hal_tick.h b/include/liblvgl/hal/lv_hal_tick.h deleted file mode 100644 index 407fa427..00000000 --- a/include/liblvgl/hal/lv_hal_tick.h +++ /dev/null @@ -1,69 +0,0 @@ -/** - * @file lv_hal_tick.h - * Provide access to the system tick with 1 millisecond resolution - */ - -#ifndef LV_HAL_TICK_H -#define LV_HAL_TICK_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lv_conf_internal.h" - -#include -#include - -/********************* - * DEFINES - *********************/ -#ifndef LV_ATTRIBUTE_TICK_INC -#define LV_ATTRIBUTE_TICK_INC -#endif - -/********************** - * TYPEDEFS - **********************/ - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -//! @cond Doxygen_Suppress - -#if !LV_TICK_CUSTOM -/** - * You have to call this function periodically - * @param tick_period the call period of this function in milliseconds - */ -LV_ATTRIBUTE_TICK_INC void lv_tick_inc(uint32_t tick_period); -#endif - -//! @endcond - -/** - * Get the elapsed milliseconds since start up - * @return the elapsed milliseconds - */ -uint32_t lv_tick_get(void); - -/** - * Get the elapsed milliseconds since a previous time stamp - * @param prev_tick a previous time stamp (return value of lv_tick_get() ) - * @return the elapsed milliseconds since 'prev_tick' - */ -uint32_t lv_tick_elaps(uint32_t prev_tick); - -/********************** - * MACROS - **********************/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_HAL_TICK_H*/ diff --git a/include/liblvgl/indev/lv_indev.h b/include/liblvgl/indev/lv_indev.h new file mode 100644 index 00000000..bee5ec1a --- /dev/null +++ b/include/liblvgl/indev/lv_indev.h @@ -0,0 +1,422 @@ +/** + * @file lv_indev.h + * + */ + +#ifndef LV_INDEV_H +#define LV_INDEV_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../core/lv_group.h" +#include "../misc/lv_area.h" +#include "../misc/lv_timer.h" +#include "../misc/lv_event.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** Possible input device types*/ +typedef enum { + LV_INDEV_TYPE_NONE, /**< Uninitialized state*/ + LV_INDEV_TYPE_POINTER, /**< Touch pad, mouse, external button*/ + LV_INDEV_TYPE_KEYPAD, /**< Keypad or keyboard*/ + LV_INDEV_TYPE_BUTTON, /**< External (hardware button) which is assigned to a specific point of the screen*/ + LV_INDEV_TYPE_ENCODER, /**< Encoder with only Left, Right turn and a Button*/ +} lv_indev_type_t; + +/** States for input devices*/ +typedef enum { + LV_INDEV_STATE_RELEASED = 0, + LV_INDEV_STATE_PRESSED +} lv_indev_state_t; + +typedef enum { + LV_INDEV_MODE_NONE = 0, + LV_INDEV_MODE_TIMER, + LV_INDEV_MODE_EVENT, +} lv_indev_mode_t; + +/** Data structure passed to an input driver to fill*/ +typedef struct { + lv_point_t point; /**< For LV_INDEV_TYPE_POINTER the currently pressed point*/ + uint32_t key; /**< For LV_INDEV_TYPE_KEYPAD the currently pressed key*/ + uint32_t btn_id; /**< For LV_INDEV_TYPE_BUTTON the currently pressed button*/ + int16_t enc_diff; /**< For LV_INDEV_TYPE_ENCODER number of steps since the previous read*/ + + lv_indev_state_t state; /**< LV_INDEV_STATE_RELEASED or LV_INDEV_STATE_PRESSED*/ + bool continue_reading; /**< If set to true, the read callback is invoked again, unless the device is in event-driven mode*/ +} lv_indev_data_t; + +typedef void (*lv_indev_read_cb_t)(lv_indev_t * indev, lv_indev_data_t * data); + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create an indev + * @return Pointer to the created indev or NULL when allocation failed + */ +lv_indev_t * lv_indev_create(void); + +/** + * Remove the provided input device. Make sure not to use the provided input device afterwards anymore. + * @param indev pointer to delete + */ +void lv_indev_delete(lv_indev_t * indev); + +/** + * Get the next input device. + * @param indev pointer to the current input device. NULL to initialize. + * @return the next input device or NULL if there are no more. Provide the first input device when + * the parameter is NULL + */ +lv_indev_t * lv_indev_get_next(lv_indev_t * indev); + +/** + * Read data from an input device. + * @param indev pointer to an input device + */ +void lv_indev_read(lv_indev_t * indev); + +/** + * Called periodically to read the input devices + * @param timer pointer to a timer to read + */ +void lv_indev_read_timer_cb(lv_timer_t * timer); + +/** + * Enable or disable one or all input devices (default enabled) + * @param indev pointer to an input device or NULL to enable/disable all of them + * @param enable true to enable, false to disable + */ +void lv_indev_enable(lv_indev_t * indev, bool enable); + +/** + * Get the currently processed input device. Can be used in action functions too. + * @return pointer to the currently processed input device or NULL if no input device processing + * right now + */ +lv_indev_t * lv_indev_active(void); + +/** + * Set the type of an input device + * @param indev pointer to an input device + * @param indev_type the type of the input device from `lv_indev_type_t` (`LV_INDEV_TYPE_...`) + */ +void lv_indev_set_type(lv_indev_t * indev, lv_indev_type_t indev_type); + +/** + * Set a callback function to read input device data to the indev + * @param indev pointer to an input device + * @param read_cb pointer to callback function to read input device data + */ +void lv_indev_set_read_cb(lv_indev_t * indev, lv_indev_read_cb_t read_cb); + +/** + * Set user data to the indev + * @param indev pointer to an input device + * @param user_data pointer to user data + */ +void lv_indev_set_user_data(lv_indev_t * indev, void * user_data); + +/** + * Set driver data to the indev + * @param indev pointer to an input device + * @param driver_data pointer to driver data + */ +void lv_indev_set_driver_data(lv_indev_t * indev, void * driver_data); + +/** + * Assign a display to the indev + * @param indev pointer to an input device + * @param disp pointer to an display + */ +void lv_indev_set_display(lv_indev_t * indev, struct _lv_display_t * disp); + +/** + * Set long press time to indev + * @param indev pointer to input device + * @param long_press_time time long press time in ms + */ +void lv_indev_set_long_press_time(lv_indev_t * indev, uint16_t long_press_time); + +/** + * Set scroll limit to the input device + * @param indev pointer to an input device + * @param scroll_limit the number of pixels to slide before actually drag the object + */ +void lv_indev_set_scroll_limit(lv_indev_t * indev, uint8_t scroll_limit); + +/** + * Set scroll throw slow-down to the indev. Greater value means faster slow-down + * @param indev pointer to an input device + * @param scroll_throw the slow-down in [%] + */ +void lv_indev_set_scroll_throw(lv_indev_t * indev, uint8_t scroll_throw); + +/** + * Get the type of an input device + * @param indev pointer to an input device + * @return the type of the input device from `lv_hal_indev_type_t` (`LV_INDEV_TYPE_...`) + */ +lv_indev_type_t lv_indev_get_type(const lv_indev_t * indev); + +/** + * Get the callback function to read input device data to the indev + * @param indev pointer to an input device + * @return Pointer to callback function to read input device data or NULL if indev is NULL + */ +lv_indev_read_cb_t lv_indev_get_read_cb(lv_indev_t * indev); + +/** + * Get the indev state + * @param indev pointer to an input device + * @return Indev state or LV_INDEV_STATE_RELEASED if indev is NULL + */ +lv_indev_state_t lv_indev_get_state(const lv_indev_t * indev); + +/** + * Get the indev assigned group + * @param indev pointer to an input device + * @return Pointer to indev assigned group or NULL if indev is NULL + */ +lv_group_t * lv_indev_get_group(const lv_indev_t * indev); + +/** + * Get a pointer to the assigned display of the indev + * @param indev pointer to an input device + * @return pointer to the assigned display or NULL if indev is NULL + */ +lv_display_t * lv_indev_get_display(const lv_indev_t * indev); + +/** + * Get a pointer to the user data of the indev + * @param indev pointer to an input device + * @return pointer to the user data or NULL if indev is NULL + */ +void * lv_indev_get_user_data(const lv_indev_t * indev); + +/** + * Get a pointer to the driver data of the indev + * @param indev pointer to an input device + * @return pointer to the driver data or NULL if indev is NULL + */ +void * lv_indev_get_driver_data(const lv_indev_t * indev); + +/** + * Get whether indev is moved while pressed + * @param indev pointer to an input device + * @return true: indev is moved while pressed; false: indev is not moved while pressed + */ +bool lv_indev_get_press_moved(const lv_indev_t * indev); + +/** + * Reset one or all input devices + * @param indev pointer to an input device to reset or NULL to reset all of them + * @param obj pointer to an object which triggers the reset. + */ +void lv_indev_reset(lv_indev_t * indev, lv_obj_t * obj); + +/** + * Touch and key related events are sent to the input device first and to the widget after that. + * If this functions called in an indev event, the event won't be sent to the widget. + * @param indev pointer to an input device + */ +void lv_indev_stop_processing(lv_indev_t * indev); + +/** + * Reset the long press state of an input device + * @param indev pointer to an input device + */ +void lv_indev_reset_long_press(lv_indev_t * indev); + +/** + * Set a cursor for a pointer input device (for LV_INPUT_TYPE_POINTER and LV_INPUT_TYPE_BUTTON) + * @param indev pointer to an input device + * @param cur_obj pointer to an object to be used as cursor + */ +void lv_indev_set_cursor(lv_indev_t * indev, lv_obj_t * cur_obj); + +/** + * Set a destination group for a keypad input device (for LV_INDEV_TYPE_KEYPAD) + * @param indev pointer to an input device + * @param group pointer to a group + */ +void lv_indev_set_group(lv_indev_t * indev, lv_group_t * group); + +/** + * Set the an array of points for LV_INDEV_TYPE_BUTTON. + * These points will be assigned to the buttons to press a specific point on the screen + * @param indev pointer to an input device + * @param points array of points + */ +void lv_indev_set_button_points(lv_indev_t * indev, const lv_point_t points[]); + +/** + * Get the last point of an input device (for LV_INDEV_TYPE_POINTER and LV_INDEV_TYPE_BUTTON) + * @param indev pointer to an input device + * @param point pointer to a point to store the result + */ +void lv_indev_get_point(const lv_indev_t * indev, lv_point_t * point); + +/** +* Get the current gesture direct +* @param indev pointer to an input device +* @return current gesture direct +*/ +lv_dir_t lv_indev_get_gesture_dir(const lv_indev_t * indev); + +/** + * Get the last pressed key of an input device (for LV_INDEV_TYPE_KEYPAD) + * @param indev pointer to an input device + * @return the last pressed key (0 on error) + */ +uint32_t lv_indev_get_key(const lv_indev_t * indev); + + +/** + * Get the counter for consecutive clicks within a short distance and time. + * The counter is updated before LV_EVENT_SHORT_CLICKED is fired. + * @param indev pointer to an input device + * @return short click streak counter + */ +uint8_t lv_indev_get_short_click_streak(const lv_indev_t * indev); + +/** + * Check the current scroll direction of an input device (for LV_INDEV_TYPE_POINTER and + * LV_INDEV_TYPE_BUTTON) + * @param indev pointer to an input device + * @return LV_DIR_NONE: no scrolling now + * LV_DIR_HOR/VER + */ +lv_dir_t lv_indev_get_scroll_dir(const lv_indev_t * indev); + +/** + * Get the currently scrolled object (for LV_INDEV_TYPE_POINTER and + * LV_INDEV_TYPE_BUTTON) + * @param indev pointer to an input device + * @return pointer to the currently scrolled object or NULL if no scrolling by this indev + */ +lv_obj_t * lv_indev_get_scroll_obj(const lv_indev_t * indev); + +/** + * Get the movement vector of an input device (for LV_INDEV_TYPE_POINTER and + * LV_INDEV_TYPE_BUTTON) + * @param indev pointer to an input device + * @param point pointer to a point to store the types.pointer.vector + */ +void lv_indev_get_vect(const lv_indev_t * indev, lv_point_t * point); + +/** + * Do nothing until the next release + * @param indev pointer to an input device + */ +void lv_indev_wait_release(lv_indev_t * indev); + +/** + * Gets a pointer to the currently active object in the currently processed input device. + * @return pointer to currently active object or NULL if no active object + */ +lv_obj_t * lv_indev_get_active_obj(void); + +/** + * Get a pointer to the indev read timer to + * modify its parameters with `lv_timer_...` functions. + * @param indev pointer to an input device + * @return pointer to the indev read refresher timer. (NULL on error) + */ +lv_timer_t * lv_indev_get_read_timer(lv_indev_t * indev); + +/** +* Set the input device's event model: event-driven mode or timer mode. +* @param indev pointer to an input device +* @param mode the mode of input device +*/ +void lv_indev_set_mode(lv_indev_t * indev, lv_indev_mode_t mode); + +/** + * Get the input device's running mode. + * @param indev pointer to an input device + * @return the running mode for the specified input device. + */ +lv_indev_mode_t lv_indev_get_mode(lv_indev_t * indev); + +/** + * Search the most top, clickable object by a point + * @param obj pointer to a start object, typically the screen + * @param point pointer to a point for searching the most top child + * @return pointer to the found object or NULL if there was no suitable object + */ +lv_obj_t * lv_indev_search_obj(lv_obj_t * obj, lv_point_t * point); + +/** + * Add an event handler to the indev + * @param indev pointer to an indev + * @param event_cb an event callback + * @param filter event code to react or `LV_EVENT_ALL` + * @param user_data optional user_data + */ +void lv_indev_add_event_cb(lv_indev_t * indev, lv_event_cb_t event_cb, lv_event_code_t filter, void * user_data); + +/** + * Get the number of event attached to an indev + * @param indev pointer to an indev + * @return number of events + */ +uint32_t lv_indev_get_event_count(lv_indev_t * indev); + +/** + * Get an event descriptor for an event + * @param indev pointer to an indev + * @param index the index of the event + * @return the event descriptor + */ +lv_event_dsc_t * lv_indev_get_event_dsc(lv_indev_t * indev, uint32_t index); + +/** + * Remove an event + * @param indev pointer to an indev + * @param index the index of the event to remove + * @return true: and event was removed; false: no event was removed + */ +bool lv_indev_remove_event(lv_indev_t * indev, uint32_t index); + +/** + * Remove an event_cb with user_data + * @param indev pointer to a indev + * @param event_cb the event_cb of the event to remove + * @param user_data user_data + * @return the count of the event removed + */ +uint32_t lv_indev_remove_event_cb_with_user_data(lv_indev_t * indev, lv_event_cb_t event_cb, void * user_data); + +/** + * Send an event to an indev + * @param indev pointer to an indev + * @param code an event code. LV_EVENT_... + * @param param optional param + * @return LV_RESULT_OK: indev wasn't deleted in the event. + */ +lv_result_t lv_indev_send_event(lv_indev_t * indev, lv_event_code_t code, void * param); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_INDEV_H*/ diff --git a/include/liblvgl/indev/lv_indev_private.h b/include/liblvgl/indev/lv_indev_private.h new file mode 100644 index 00000000..9bf98091 --- /dev/null +++ b/include/liblvgl/indev/lv_indev_private.h @@ -0,0 +1,137 @@ +/** + * @file lv_indev_private.h + * + */ + +#ifndef LV_INDEV_PRIVATE_H +#define LV_INDEV_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "lv_indev.h" +#include "../misc/lv_anim.h" +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_indev_t { + /** Input device type*/ + lv_indev_type_t type; + + /** Function pointer to read input device data.*/ + lv_indev_read_cb_t read_cb; + + lv_indev_state_t state; /**< Current state of the input device.*/ + lv_indev_mode_t mode; + + /*Flags*/ + uint8_t long_pr_sent : 1; + uint8_t reset_query : 1; + uint8_t enabled : 1; + uint8_t wait_until_release : 1; + uint8_t stop_processing_query : 1; + + uint32_t pr_timestamp; /**< Pressed time stamp*/ + uint32_t longpr_rep_timestamp; /**< Long press repeat time stamp*/ + + void * driver_data; + void * user_data; + + /**< Pointer to the assigned display*/ + lv_display_t * disp; + + /**< Timer to periodically read the input device*/ + lv_timer_t * read_timer; + + /**< Number of pixels to slide before actually drag the object*/ + uint8_t scroll_limit; + + /**< Drag throw slow-down in [%]. Greater value means faster slow-down*/ + uint8_t scroll_throw; + + /**< At least this difference should be between two points to evaluate as gesture*/ + uint8_t gesture_min_velocity; + + /**< At least this difference should be to send a gesture*/ + uint8_t gesture_limit; + + /**< Long press time in milliseconds*/ + uint16_t long_press_time; + + /**< Repeated trigger period in long press [ms]*/ + uint16_t long_press_repeat_time; + + /**< Rotary diff count will be multiplied by this value and divided by 256*/ + int32_t rotary_sensitivity; + + struct { + /*Pointer and button data*/ + lv_point_t act_point; /**< Current point of input device.*/ + lv_point_t last_point; /**< Last point of input device.*/ + lv_point_t last_raw_point; /**< Last point read from read_cb. */ + lv_point_t vect; /**< Difference between `act_point` and `last_point`.*/ + lv_point_t scroll_sum; /*Count the dragged pixels to check LV_INDEV_DEF_SCROLL_LIMIT*/ + lv_point_t scroll_throw_vect; + lv_point_t scroll_throw_vect_ori; + lv_obj_t * act_obj; /*The object being pressed*/ + lv_obj_t * last_obj; /*The last object which was pressed*/ + lv_obj_t * scroll_obj; /*The object being scrolled*/ + lv_obj_t * last_pressed; /*The lastly pressed object*/ + lv_obj_t * last_hovered; /*The lastly hovered object*/ + lv_area_t scroll_area; + lv_point_t gesture_sum; /*Count the gesture pixels to check LV_INDEV_DEF_GESTURE_LIMIT*/ + int32_t diff; + /*Short click streaks*/ + uint8_t short_click_streak; + lv_point_t last_short_click_point; + uint32_t last_short_click_timestamp; + /*Flags*/ + uint8_t scroll_dir : 4; + uint8_t gesture_dir : 4; + uint8_t gesture_sent : 1; + uint8_t press_moved : 1; + } pointer; + struct { + /*Keypad data*/ + lv_indev_state_t last_state; + uint32_t last_key; + } keypad; + + lv_obj_t * cursor; /**< Cursor for LV_INPUT_TYPE_POINTER*/ + lv_group_t * group; /**< Keypad destination group*/ + const lv_point_t * btn_points; /**< Array points assigned to the button ()screen will be pressed + here by the buttons*/ + lv_event_list_t event_list; + lv_anim_t * scroll_throw_anim; +}; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Find a scrollable object based on the current scroll vector in the indev. + * In handles scroll propagation to the parent if needed, and scroll directions too. + * @param indev pointer to an indev + * @return the found scrollable object or NULL if not found. + */ +lv_obj_t * lv_indev_find_scroll_obj(lv_indev_t * indev); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_INDEV_PRIVATE_H*/ diff --git a/include/liblvgl/core/lv_indev_scroll.h b/include/liblvgl/indev/lv_indev_scroll.h similarity index 78% rename from include/liblvgl/core/lv_indev_scroll.h rename to include/liblvgl/indev/lv_indev_scroll.h index 76c64d17..413376f1 100644 --- a/include/liblvgl/core/lv_indev_scroll.h +++ b/include/liblvgl/indev/lv_indev_scroll.h @@ -13,7 +13,7 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "lv_obj.h" +#include "../core/lv_obj.h" /********************* * DEFINES @@ -29,15 +29,15 @@ extern "C" { /** * Handle scrolling. Called by LVGL during input device processing - * @param proc pointer to an input device's proc field + * @param indev pointer to an input device */ -void _lv_indev_scroll_handler(_lv_indev_proc_t * proc); +void lv_indev_scroll_handler(lv_indev_t * indev); /** * Handle throwing after scrolling. Called by LVGL during input device processing - * @param proc pointer to an input device's proc field + * @param indev pointer to an input device */ -void _lv_indev_scroll_throw_handler(_lv_indev_proc_t * proc); +void lv_indev_scroll_throw_handler(lv_indev_t * indev); /** * Predict where would a scroll throw end @@ -45,7 +45,7 @@ void _lv_indev_scroll_throw_handler(_lv_indev_proc_t * proc); * @param dir ` LV_DIR_VER` or `LV_DIR_HOR` * @return the difference compared to the current position when the throw would be finished */ -lv_coord_t lv_indev_scroll_throw_predict(lv_indev_t * indev, lv_dir_t dir); +int32_t lv_indev_scroll_throw_predict(lv_indev_t * indev, lv_dir_t dir); /** * Get the distance of the nearest snap point diff --git a/include/liblvgl/layouts/flex/lv_flex.h b/include/liblvgl/layouts/flex/lv_flex.h new file mode 100644 index 00000000..9724f9b1 --- /dev/null +++ b/include/liblvgl/layouts/flex/lv_flex.h @@ -0,0 +1,102 @@ +/** + * @file lv_flex.h + * + */ + +#ifndef LV_FLEX_H +#define LV_FLEX_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../lv_conf_internal.h" +#include "../../misc/lv_area.h" + +#if LV_USE_FLEX + +/********************* + * DEFINES + *********************/ + +#define LV_FLEX_COLUMN (1 << 0) +#define LV_FLEX_WRAP (1 << 2) +#define LV_FLEX_REVERSE (1 << 3) + +/********************** + * TYPEDEFS + **********************/ + +/*Can't include lv_obj.h because it includes this header file*/ + +typedef enum { + LV_FLEX_ALIGN_START, + LV_FLEX_ALIGN_END, + LV_FLEX_ALIGN_CENTER, + LV_FLEX_ALIGN_SPACE_EVENLY, + LV_FLEX_ALIGN_SPACE_AROUND, + LV_FLEX_ALIGN_SPACE_BETWEEN, +} lv_flex_align_t; + +typedef enum { + LV_FLEX_FLOW_ROW = 0x00, + LV_FLEX_FLOW_COLUMN = LV_FLEX_COLUMN, + LV_FLEX_FLOW_ROW_WRAP = LV_FLEX_FLOW_ROW | LV_FLEX_WRAP, + LV_FLEX_FLOW_ROW_REVERSE = LV_FLEX_FLOW_ROW | LV_FLEX_REVERSE, + LV_FLEX_FLOW_ROW_WRAP_REVERSE = LV_FLEX_FLOW_ROW | LV_FLEX_WRAP | LV_FLEX_REVERSE, + LV_FLEX_FLOW_COLUMN_WRAP = LV_FLEX_FLOW_COLUMN | LV_FLEX_WRAP, + LV_FLEX_FLOW_COLUMN_REVERSE = LV_FLEX_FLOW_COLUMN | LV_FLEX_REVERSE, + LV_FLEX_FLOW_COLUMN_WRAP_REVERSE = LV_FLEX_FLOW_COLUMN | LV_FLEX_WRAP | LV_FLEX_REVERSE, +} lv_flex_flow_t; + +/********************** + * GLOBAL VARIABLES + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Initialize a flex layout to default values + */ +void lv_flex_init(void); + +/** + * Set how the item should flow + * @param obj pointer to an object. The parent must have flex layout else nothing will happen. + * @param flow an element of `lv_flex_flow_t`. + */ +void lv_obj_set_flex_flow(lv_obj_t * obj, lv_flex_flow_t flow); + +/** + * Set how to place (where to align) the items and tracks + * @param obj pointer to an object. The parent must have flex layout else nothing will happen. + * @param main_place where to place the items on main axis (in their track). Any value of `lv_flex_align_t`. + * @param cross_place where to place the item in their track on the cross axis. `LV_FLEX_ALIGN_START/END/CENTER` + * @param track_cross_place where to place the tracks in the cross direction. Any value of `lv_flex_align_t`. + */ +void lv_obj_set_flex_align(lv_obj_t * obj, lv_flex_align_t main_place, lv_flex_align_t cross_place, + lv_flex_align_t track_cross_place); + +/** + * Sets the width or height (on main axis) to grow the object in order fill the free space + * @param obj pointer to an object. The parent must have flex layout else nothing will happen. + * @param grow a value to set how much free space to take proportionally to other growing items. + */ +void lv_obj_set_flex_grow(lv_obj_t * obj, uint8_t grow); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_FLEX*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_FLEX_H*/ diff --git a/include/liblvgl/layouts/grid/lv_grid.h b/include/liblvgl/layouts/grid/lv_grid.h new file mode 100644 index 00000000..a7bf7382 --- /dev/null +++ b/include/liblvgl/layouts/grid/lv_grid.h @@ -0,0 +1,99 @@ +/** + * @file lv_grid.h + * + */ + +#ifndef LV_GRID_H +#define LV_GRID_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../lv_conf_internal.h" +#include "../../misc/lv_area.h" + +#if LV_USE_GRID + +/********************* + * DEFINES + *********************/ +/** + * Can be used track size to make the track fill the free space. + * @param x how much space to take proportionally to other FR tracks + * @return a special track size + */ +#define LV_GRID_FR(x) (LV_COORD_MAX - 100 + x) + +#define LV_GRID_CONTENT (LV_COORD_MAX - 101) +LV_EXPORT_CONST_INT(LV_GRID_CONTENT); + +#define LV_GRID_TEMPLATE_LAST (LV_COORD_MAX) +LV_EXPORT_CONST_INT(LV_GRID_TEMPLATE_LAST); + +/********************** + * TYPEDEFS + **********************/ + +/*Can't include lv_obj.h because it includes this header file*/ + +typedef enum { + LV_GRID_ALIGN_START, + LV_GRID_ALIGN_CENTER, + LV_GRID_ALIGN_END, + LV_GRID_ALIGN_STRETCH, + LV_GRID_ALIGN_SPACE_EVENLY, + LV_GRID_ALIGN_SPACE_AROUND, + LV_GRID_ALIGN_SPACE_BETWEEN, +} lv_grid_align_t; + +/********************** + * GLOBAL VARIABLES + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void lv_grid_init(void); + +void lv_obj_set_grid_dsc_array(lv_obj_t * obj, const int32_t col_dsc[], const int32_t row_dsc[]); + +void lv_obj_set_grid_align(lv_obj_t * obj, lv_grid_align_t column_align, lv_grid_align_t row_align); + +/** + * Set the cell of an object. The object's parent needs to have grid layout, else nothing will happen + * @param obj pointer to an object + * @param column_align the vertical alignment in the cell. `LV_GRID_START/END/CENTER/STRETCH` + * @param col_pos column ID + * @param col_span number of columns to take (>= 1) + * @param row_align the horizontal alignment in the cell. `LV_GRID_START/END/CENTER/STRETCH` + * @param row_pos row ID + * @param row_span number of rows to take (>= 1) + */ +void lv_obj_set_grid_cell(lv_obj_t * obj, lv_grid_align_t column_align, int32_t col_pos, int32_t col_span, + lv_grid_align_t row_align, int32_t row_pos, int32_t row_span); + +/** + * Just a wrapper to `LV_GRID_FR` for bindings. + */ +int32_t lv_grid_fr(uint8_t x); + +/********************** + * GLOBAL VARIABLES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_GRID*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_GRID_H*/ diff --git a/include/liblvgl/layouts/lv_layout.h b/include/liblvgl/layouts/lv_layout.h new file mode 100644 index 00000000..a783d2b2 --- /dev/null +++ b/include/liblvgl/layouts/lv_layout.h @@ -0,0 +1,67 @@ +/** + * @file lv_layout.h + * + */ + +#ifndef LV_LAYOUT_H +#define LV_LAYOUT_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../lv_conf_internal.h" +#include "../misc/lv_types.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef void (*lv_layout_update_cb_t)(lv_obj_t *, void * user_data); + +typedef enum { + LV_LAYOUT_NONE = 0, + +#if LV_USE_FLEX + LV_LAYOUT_FLEX, +#endif + +#if LV_USE_GRID + LV_LAYOUT_GRID, +#endif + + LV_LAYOUT_LAST +} lv_layout_t; + +/** + * Register a new layout + * @param cb the layout update callback + * @param user_data custom data that will be passed to `cb` + * @return the ID of the new layout + */ +uint32_t lv_layout_register(lv_layout_update_cb_t cb, void * user_data); + +/********************** + * MACROS + **********************/ + +#if LV_USE_FLEX +#include "flex/lv_flex.h" +#endif /* LV_USE_FLEX */ + +#if LV_USE_GRID +#include "grid/lv_grid.h" +#endif /* LV_USE_GRID */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_LAYOUT_H*/ diff --git a/include/liblvgl/layouts/lv_layout_private.h b/include/liblvgl/layouts/lv_layout_private.h new file mode 100644 index 00000000..dace2fcc --- /dev/null +++ b/include/liblvgl/layouts/lv_layout_private.h @@ -0,0 +1,58 @@ +/** + * @file lv_layout_private.h + * + */ + +#ifndef LV_LAYOUT_PRIVATE_H +#define LV_LAYOUT_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_layout.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef struct { + lv_layout_update_cb_t cb; + void * user_data; +} lv_layout_dsc_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void lv_layout_init(void); + +void lv_layout_deinit(void); + +/** + * Update the layout of a widget + * @param obj pointer to a widget + */ +void lv_layout_apply(lv_obj_t * obj); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_LAYOUT_PRIVATE_H*/ diff --git a/include/liblvgl/libs/barcode/code128.h b/include/liblvgl/libs/barcode/code128.h new file mode 100644 index 00000000..51d53d0c --- /dev/null +++ b/include/liblvgl/libs/barcode/code128.h @@ -0,0 +1,47 @@ +// Copyright (c) 2013-15, LKC Technologies, Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. Redistributions in binary +// form must reproduce the above copyright notice, this list of conditions and +// the following disclaimer in the documentation and/or other materials +// provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT +// HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, +// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, +// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, +// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#ifndef CODE128_H +#define CODE128_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +// Since the FNCn characters are not ASCII, define versions here to +// simplify encoding strings that include them. +#define CODE128_FNC1 '\xf1' +#define CODE128_FNC2 '\xf2' +#define CODE128_FNC3 '\xf3' +#define CODE128_FNC4 '\xf4' + +size_t code128_estimate_len(const char * s); +size_t code128_encode_gs1(const char * s, char * out, size_t maxlength); +size_t code128_encode_raw(const char * s, char * out, size_t maxlength); + +#ifdef __cplusplus +} +#endif + +#endif // CODE128_H diff --git a/include/liblvgl/libs/barcode/lv_barcode.h b/include/liblvgl/libs/barcode/lv_barcode.h new file mode 100644 index 00000000..c9c0ce65 --- /dev/null +++ b/include/liblvgl/libs/barcode/lv_barcode.h @@ -0,0 +1,118 @@ +/** + * @file lv_barcode.h + * + */ + +#ifndef LV_BARCODE_H +#define LV_BARCODE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../lv_conf_internal.h" +#include "../../misc/lv_types.h" +#include "../../misc/lv_color.h" +#include "../../widgets/canvas/lv_canvas.h" + +#if LV_USE_BARCODE + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_barcode_class; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create an empty barcode (an `lv_canvas`) object. + * @param parent point to an object where to create the barcode + * @return pointer to the created barcode object + */ +lv_obj_t * lv_barcode_create(lv_obj_t * parent); + +/** + * Set the dark color of a barcode object + * @param obj pointer to barcode object + * @param color dark color of the barcode + */ +void lv_barcode_set_dark_color(lv_obj_t * obj, lv_color_t color); + +/** + * Set the light color of a barcode object + * @param obj pointer to barcode object + * @param color light color of the barcode + */ +void lv_barcode_set_light_color(lv_obj_t * obj, lv_color_t color); + +/** + * Set the scale of a barcode object + * @param obj pointer to barcode object + * @param scale scale factor + */ +void lv_barcode_set_scale(lv_obj_t * obj, uint16_t scale); + +/** + * Set the direction of a barcode object + * @param obj pointer to barcode object + * @param direction draw direction (`LV_DIR_HOR` or `LB_DIR_VER`) + */ +void lv_barcode_set_direction(lv_obj_t * obj, lv_dir_t direction); + +/** + * Set the tiled mode of a barcode object + * @param obj pointer to barcode object + * @param tiled true: tiled mode, false: normal mode (default) + */ +void lv_barcode_set_tiled(lv_obj_t * obj, bool tiled); + +/** + * Set the data of a barcode object + * @param obj pointer to barcode object + * @param data data to display + * @return LV_RESULT_OK: if no error; LV_RESULT_INVALID: on error + */ +lv_result_t lv_barcode_update(lv_obj_t * obj, const char * data); + +/** + * Get the dark color of a barcode object + * @param obj pointer to barcode object + * @return dark color of the barcode + */ +lv_color_t lv_barcode_get_dark_color(lv_obj_t * obj); + +/** + * Get the light color of a barcode object + * @param obj pointer to barcode object + * @return light color of the barcode + */ +lv_color_t lv_barcode_get_light_color(lv_obj_t * obj); + +/** + * Get the scale of a barcode object + * @param obj pointer to barcode object + * @return scale factor + */ +uint16_t lv_barcode_get_scale(lv_obj_t * obj); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_BARCODE*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /*LV_BARCODE_H*/ diff --git a/include/liblvgl/libs/barcode/lv_barcode_private.h b/include/liblvgl/libs/barcode/lv_barcode_private.h new file mode 100644 index 00000000..dadf0684 --- /dev/null +++ b/include/liblvgl/libs/barcode/lv_barcode_private.h @@ -0,0 +1,55 @@ +/** + * @file lv_barcode_private.h + * + */ + +#ifndef LV_BARCODE_PRIVATE_H +#define LV_BARCODE_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../widgets/canvas/lv_canvas_private.h" +#include "lv_barcode.h" + +#if LV_USE_BARCODE + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/*Data of barcode*/ +struct _lv_barcode_t { + lv_canvas_t canvas; + lv_color_t dark_color; + lv_color_t light_color; + uint16_t scale; + lv_dir_t direction; + bool tiled; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_BARCODE */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_BARCODE_PRIVATE_H*/ diff --git a/include/liblvgl/libs/bin_decoder/lv_bin_decoder.h b/include/liblvgl/libs/bin_decoder/lv_bin_decoder.h new file mode 100644 index 00000000..d441230d --- /dev/null +++ b/include/liblvgl/libs/bin_decoder/lv_bin_decoder.h @@ -0,0 +1,70 @@ +/** + * @file lv_bin_decoder.h + * + */ + +#ifndef LV_BIN_DECODER_H +#define LV_BIN_DECODER_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../draw/lv_image_decoder.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Initialize the binary image decoder module + */ +void lv_bin_decoder_init(void); + +/** + * Get info about a lvgl binary image + * @param decoder the decoder where this function belongs + * @param dsc image descriptor containing the source and type of the image and other info. + * @param header store the image data here + * @return LV_RESULT_OK: the info is successfully stored in `header`; LV_RESULT_INVALID: unknown format or other error. + */ +lv_result_t lv_bin_decoder_info(lv_image_decoder_t * decoder, lv_image_decoder_dsc_t * dsc, lv_image_header_t * header); + +lv_result_t lv_bin_decoder_get_area(lv_image_decoder_t * decoder, lv_image_decoder_dsc_t * dsc, + const lv_area_t * full_area, lv_area_t * decoded_area); + +/** + * Open a lvgl binary image + * @param decoder the decoder where this function belongs + * @param dsc pointer to decoder descriptor. `src`, `style` are already initialized in it. + * @return LV_RESULT_OK: the info is successfully stored in `header`; LV_RESULT_INVALID: unknown format or other error. + */ +lv_result_t lv_bin_decoder_open(lv_image_decoder_t * decoder, lv_image_decoder_dsc_t * dsc); + +/** + * Close the pending decoding. Free resources etc. + * @param decoder pointer to the decoder the function associated with + * @param dsc pointer to decoder descriptor + */ +void lv_bin_decoder_close(lv_image_decoder_t * decoder, lv_image_decoder_dsc_t * dsc); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_BIN_DECODER_H*/ diff --git a/include/liblvgl/extra/libs/bmp/lv_bmp.h b/include/liblvgl/libs/bmp/lv_bmp.h similarity index 90% rename from include/liblvgl/extra/libs/bmp/lv_bmp.h rename to include/liblvgl/libs/bmp/lv_bmp.h index 3d9e9484..a7dbe277 100644 --- a/include/liblvgl/extra/libs/bmp/lv_bmp.h +++ b/include/liblvgl/libs/bmp/lv_bmp.h @@ -13,7 +13,7 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" +#include "../../lv_conf_internal.h" #if LV_USE_BMP /********************* @@ -28,6 +28,7 @@ extern "C" { * GLOBAL PROTOTYPES **********************/ void lv_bmp_init(void); +void lv_bmp_deinit(void); /********************** * MACROS diff --git a/include/liblvgl/extra/libs/ffmpeg/lv_ffmpeg.h b/include/liblvgl/libs/ffmpeg/lv_ffmpeg.h similarity index 81% rename from include/liblvgl/extra/libs/ffmpeg/lv_ffmpeg.h rename to include/liblvgl/libs/ffmpeg/lv_ffmpeg.h index f3a365c3..1cb86b9d 100644 --- a/include/liblvgl/extra/libs/ffmpeg/lv_ffmpeg.h +++ b/include/liblvgl/libs/ffmpeg/lv_ffmpeg.h @@ -12,8 +12,9 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lvgl.h" +#include "../../lv_conf_internal.h" #if LV_USE_FFMPEG != 0 +#include "../../misc/lv_types.h" /********************* * DEFINES @@ -24,22 +25,14 @@ extern "C" { **********************/ struct ffmpeg_context_s; -extern const lv_obj_class_t lv_ffmpeg_player_class; - -typedef struct { - lv_img_t img; - lv_timer_t * timer; - lv_img_dsc_t imgdsc; - bool auto_restart; - struct ffmpeg_context_s * ffmpeg_ctx; -} lv_ffmpeg_player_t; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_ffmpeg_player_class; typedef enum { LV_FFMPEG_PLAYER_CMD_START, LV_FFMPEG_PLAYER_CMD_STOP, LV_FFMPEG_PLAYER_CMD_PAUSE, LV_FFMPEG_PLAYER_CMD_RESUME, - _LV_FFMPEG_PLAYER_CMD_LAST + LV_FFMPEG_PLAYER_CMD_LAST } lv_ffmpeg_player_cmd_t; /********************** @@ -69,9 +62,9 @@ lv_obj_t * lv_ffmpeg_player_create(lv_obj_t * parent); * Set the path of the file to be played * @param obj pointer to a ffmpeg_player object * @param path video file path - * @return LV_RES_OK: no error; LV_RES_INV: can't get the info. + * @return LV_RESULT_OK: no error; LV_RESULT_INVALID: can't get the info. */ -lv_res_t lv_ffmpeg_player_set_src(lv_obj_t * obj, const char * path); +lv_result_t lv_ffmpeg_player_set_src(lv_obj_t * obj, const char * path); /** * Set command control video player diff --git a/include/liblvgl/libs/ffmpeg/lv_ffmpeg_private.h b/include/liblvgl/libs/ffmpeg/lv_ffmpeg_private.h new file mode 100644 index 00000000..e4a0488a --- /dev/null +++ b/include/liblvgl/libs/ffmpeg/lv_ffmpeg_private.h @@ -0,0 +1,51 @@ +/** + * @file lv_ffmpeg_private.h + * + */ + +#ifndef LV_FFMPEG_PRIVATE_H +#define LV_FFMPEG_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_ffmpeg.h" +#if LV_USE_FFMPEG != 0 +#include "../../widgets/image/lv_image_private.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_ffmpeg_player_t { + lv_image_t img; + lv_timer_t * timer; + lv_image_dsc_t imgdsc; + bool auto_restart; + struct ffmpeg_context_s * ffmpeg_ctx; +}; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_FFMPEG*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_FFMPEG_PRIVATE_H*/ diff --git a/include/liblvgl/libs/freetype/arial.ttf b/include/liblvgl/libs/freetype/arial.ttf new file mode 100644 index 00000000..886789b8 Binary files /dev/null and b/include/liblvgl/libs/freetype/arial.ttf differ diff --git a/include/liblvgl/libs/freetype/ftmodule.h b/include/liblvgl/libs/freetype/ftmodule.h new file mode 100644 index 00000000..b804b6e3 --- /dev/null +++ b/include/liblvgl/libs/freetype/ftmodule.h @@ -0,0 +1,33 @@ +/* + * This file registers the FreeType modules compiled into the library. + * + * If you use GNU make, this file IS NOT USED! Instead, it is created in + * the objects directory (normally `/objs/`) based on information + * from `/modules.cfg`. + * + * Please read `docs/INSTALL.ANY` and `docs/CUSTOMIZE` how to compile + * FreeType without GNU make. + * + */ + +/* FT_USE_MODULE( FT_Module_Class, autofit_module_class ) */ +FT_USE_MODULE(FT_Driver_ClassRec, tt_driver_class) +/* FT_USE_MODULE( FT_Driver_ClassRec, t1_driver_class ) */ +/* FT_USE_MODULE( FT_Driver_ClassRec, cff_driver_class ) */ +/* FT_USE_MODULE( FT_Driver_ClassRec, t1cid_driver_class ) */ +/* FT_USE_MODULE( FT_Driver_ClassRec, pfr_driver_class ) */ +/* FT_USE_MODULE( FT_Driver_ClassRec, t42_driver_class ) */ +/* FT_USE_MODULE( FT_Driver_ClassRec, winfnt_driver_class ) */ +/* FT_USE_MODULE( FT_Driver_ClassRec, pcf_driver_class ) */ +/* FT_USE_MODULE( FT_Driver_ClassRec, bdf_driver_class ) */ +/* FT_USE_MODULE( FT_Module_Class, psaux_module_class ) */ +/* FT_USE_MODULE( FT_Module_Class, psnames_module_class ) */ +/* FT_USE_MODULE( FT_Module_Class, pshinter_module_class ) */ +FT_USE_MODULE(FT_Module_Class, sfnt_module_class) +FT_USE_MODULE(FT_Renderer_Class, ft_smooth_renderer_class) +/* FT_USE_MODULE( FT_Renderer_Class, ft_raster1_renderer_class ) */ +/* FT_USE_MODULE( FT_Renderer_Class, ft_sdf_renderer_class ) */ +/* FT_USE_MODULE( FT_Renderer_Class, ft_bitmap_sdf_renderer_class ) */ +/* FT_USE_MODULE( FT_Renderer_Class, ft_svg_renderer_class ) */ + +/* EOF */ diff --git a/include/liblvgl/libs/freetype/ftoption.h b/include/liblvgl/libs/freetype/ftoption.h new file mode 100644 index 00000000..1a762bb0 --- /dev/null +++ b/include/liblvgl/libs/freetype/ftoption.h @@ -0,0 +1,964 @@ +/**************************************************************************** + * + * ftoption.h + * + * User-selectable configuration macros (specification only). + * + * Copyright (C) 1996-2022 by + * David Turner, Robert Wilhelm, and Werner Lemberg. + * + * This file is part of the FreeType project, and may only be used, + * modified, and distributed under the terms of the FreeType project + * license, LICENSE.TXT. By continuing to use, modify, or distribute + * this file you indicate that you have read the license and + * understand and accept it fully. + * + */ + +#ifndef FTOPTION_H_ +#define FTOPTION_H_ + +#include + +FT_BEGIN_HEADER + +/************************************************************************** + * + * USER-SELECTABLE CONFIGURATION MACROS + * + * This file contains the default configuration macro definitions for a + * standard build of the FreeType library. There are three ways to use + * this file to build project-specific versions of the library: + * + * - You can modify this file by hand, but this is not recommended in + * cases where you would like to build several versions of the library + * from a single source directory. + * + * - You can put a copy of this file in your build directory, more + * precisely in `$BUILD/freetype/config/ftoption.h`, where `$BUILD` is + * the name of a directory that is included _before_ the FreeType include + * path during compilation. + * + * The default FreeType Makefiles use the build directory + * `builds/` by default, but you can easily change that for your + * own projects. + * + * - Copy the file to `$BUILD/ft2build.h` and modify it + * slightly to pre-define the macro `FT_CONFIG_OPTIONS_H` used to locate + * this file during the build. For example, + * + * ``` + * #define FT_CONFIG_OPTIONS_H + * #include + * ``` + * + * will use `$BUILD/myftoptions.h` instead of this file for macro + * definitions. + * + * Note also that you can similarly pre-define the macro + * `FT_CONFIG_MODULES_H` used to locate the file listing of the modules + * that are statically linked to the library at compile time. By + * default, this file is ``. + * + * We highly recommend using the third method whenever possible. + * + */ + +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** G E N E R A L F R E E T Y P E 2 C O N F I G U R A T I O N ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ + +/*#************************************************************************ + * + * If you enable this configuration option, FreeType recognizes an + * environment variable called `FREETYPE_PROPERTIES`, which can be used to + * control the various font drivers and modules. The controllable + * properties are listed in the section @properties. + * + * You have to undefine this configuration option on platforms that lack + * the concept of environment variables (and thus don't have the `getenv` + * function), for example Windows CE. + * + * `FREETYPE_PROPERTIES` has the following syntax form (broken here into + * multiple lines for better readability). + * + * ``` + * + * ':' + * '=' + * + * ':' + * '=' + * ... + * ``` + * + * Example: + * + * ``` + * FREETYPE_PROPERTIES=truetype:interpreter-version=35 \ + * cff:no-stem-darkening=1 + * ``` + * + */ +#define FT_CONFIG_OPTION_ENVIRONMENT_PROPERTIES + +/************************************************************************** + * + * Uncomment the line below if you want to activate LCD rendering + * technology similar to ClearType in this build of the library. This + * technology triples the resolution in the direction color subpixels. To + * mitigate color fringes inherent to this technology, you also need to + * explicitly set up LCD filtering. + * + * When this macro is not defined, FreeType offers alternative LCD + * rendering technology that produces excellent output. + */ +/* #define FT_CONFIG_OPTION_SUBPIXEL_RENDERING */ + +/************************************************************************** + * + * Many compilers provide a non-ANSI 64-bit data type that can be used by + * FreeType to speed up some computations. However, this will create some + * problems when compiling the library in strict ANSI mode. + * + * For this reason, the use of 64-bit integers is normally disabled when + * the `__STDC__` macro is defined. You can however disable this by + * defining the macro `FT_CONFIG_OPTION_FORCE_INT64` here. + * + * For most compilers, this will only create compilation warnings when + * building the library. + * + * ObNote: The compiler-specific 64-bit integers are detected in the + * file `ftconfig.h` either statically or through the `configure` + * script on supported platforms. + */ +#undef FT_CONFIG_OPTION_FORCE_INT64 + +/************************************************************************** + * + * If this macro is defined, do not try to use an assembler version of + * performance-critical functions (e.g., @FT_MulFix). You should only do + * that to verify that the assembler function works properly, or to execute + * benchmark tests of the various implementations. + */ +/* #define FT_CONFIG_OPTION_NO_ASSEMBLER */ + +/************************************************************************** + * + * If this macro is defined, try to use an inlined assembler version of the + * @FT_MulFix function, which is a 'hotspot' when loading and hinting + * glyphs, and which should be executed as fast as possible. + * + * Note that if your compiler or CPU is not supported, this will default to + * the standard and portable implementation found in `ftcalc.c`. + */ +#define FT_CONFIG_OPTION_INLINE_MULFIX + +/************************************************************************** + * + * LZW-compressed file support. + * + * FreeType now handles font files that have been compressed with the + * `compress` program. This is mostly used to parse many of the PCF + * files that come with various X11 distributions. The implementation + * uses NetBSD's `zopen` to partially uncompress the file on the fly (see + * `src/lzw/ftgzip.c`). + * + * Define this macro if you want to enable this 'feature'. + */ +#define FT_CONFIG_OPTION_USE_LZW + +/************************************************************************** + * + * Gzip-compressed file support. + * + * FreeType now handles font files that have been compressed with the + * `gzip` program. This is mostly used to parse many of the PCF files + * that come with XFree86. The implementation uses 'zlib' to partially + * uncompress the file on the fly (see `src/gzip/ftgzip.c`). + * + * Define this macro if you want to enable this 'feature'. See also the + * macro `FT_CONFIG_OPTION_SYSTEM_ZLIB` below. + */ +#define FT_CONFIG_OPTION_USE_ZLIB + +/************************************************************************** + * + * ZLib library selection + * + * This macro is only used when `FT_CONFIG_OPTION_USE_ZLIB` is defined. + * It allows FreeType's 'ftgzip' component to link to the system's + * installation of the ZLib library. This is useful on systems like + * Unix or VMS where it generally is already available. + * + * If you let it undefined, the component will use its own copy of the + * zlib sources instead. These have been modified to be included + * directly within the component and **not** export external function + * names. This allows you to link any program with FreeType _and_ ZLib + * without linking conflicts. + * + * Do not `#undef` this macro here since the build system might define + * it for certain configurations only. + * + * If you use a build system like cmake or the `configure` script, + * options set by those programs have precedence, overwriting the value + * here with the configured one. + * + * If you use the GNU make build system directly (that is, without the + * `configure` script) and you define this macro, you also have to pass + * `SYSTEM_ZLIB=yes` as an argument to make. + */ +/* #define FT_CONFIG_OPTION_SYSTEM_ZLIB */ + +/************************************************************************** + * + * Bzip2-compressed file support. + * + * FreeType now handles font files that have been compressed with the + * `bzip2` program. This is mostly used to parse many of the PCF files + * that come with XFree86. The implementation uses `libbz2` to partially + * uncompress the file on the fly (see `src/bzip2/ftbzip2.c`). Contrary + * to gzip, bzip2 currently is not included and need to use the system + * available bzip2 implementation. + * + * Define this macro if you want to enable this 'feature'. + * + * If you use a build system like cmake or the `configure` script, + * options set by those programs have precedence, overwriting the value + * here with the configured one. + */ +/* #define FT_CONFIG_OPTION_USE_BZIP2 */ + +/************************************************************************** + * + * Define to disable the use of file stream functions and types, `FILE`, + * `fopen`, etc. Enables the use of smaller system libraries on embedded + * systems that have multiple system libraries, some with or without file + * stream support, in the cases where file stream support is not necessary + * such as memory loading of font files. + */ +/* #define FT_CONFIG_OPTION_DISABLE_STREAM_SUPPORT */ + +/************************************************************************** + * + * PNG bitmap support. + * + * FreeType now handles loading color bitmap glyphs in the PNG format. + * This requires help from the external libpng library. Uncompressed + * color bitmaps do not need any external libraries and will be supported + * regardless of this configuration. + * + * Define this macro if you want to enable this 'feature'. + * + * If you use a build system like cmake or the `configure` script, + * options set by those programs have precedence, overwriting the value + * here with the configured one. + */ +#define FT_CONFIG_OPTION_USE_PNG + +/************************************************************************** + * + * HarfBuzz support. + * + * FreeType uses the HarfBuzz library to improve auto-hinting of OpenType + * fonts. If available, many glyphs not directly addressable by a font's + * character map will be hinted also. + * + * Define this macro if you want to enable this 'feature'. + * + * If you use a build system like cmake or the `configure` script, + * options set by those programs have precedence, overwriting the value + * here with the configured one. + */ +/* #define FT_CONFIG_OPTION_USE_HARFBUZZ */ + +/************************************************************************** + * + * Brotli support. + * + * FreeType uses the Brotli library to provide support for decompressing + * WOFF2 streams. + * + * Define this macro if you want to enable this 'feature'. + * + * If you use a build system like cmake or the `configure` script, + * options set by those programs have precedence, overwriting the value + * here with the configured one. + */ +/* #define FT_CONFIG_OPTION_USE_BROTLI */ + +/************************************************************************** + * + * Glyph Postscript Names handling + * + * By default, FreeType 2 is compiled with the 'psnames' module. This + * module is in charge of converting a glyph name string into a Unicode + * value, or return a Macintosh standard glyph name for the use with the + * TrueType 'post' table. + * + * Undefine this macro if you do not want 'psnames' compiled in your + * build of FreeType. This has the following effects: + * + * - The TrueType driver will provide its own set of glyph names, if you + * build it to support postscript names in the TrueType 'post' table, + * but will not synthesize a missing Unicode charmap. + * + * - The Type~1 driver will not be able to synthesize a Unicode charmap + * out of the glyphs found in the fonts. + * + * You would normally undefine this configuration macro when building a + * version of FreeType that doesn't contain a Type~1 or CFF driver. + */ +#define FT_CONFIG_OPTION_POSTSCRIPT_NAMES + +/************************************************************************** + * + * Postscript Names to Unicode Values support + * + * By default, FreeType~2 is built with the 'psnames' module compiled in. + * Among other things, the module is used to convert a glyph name into a + * Unicode value. This is especially useful in order to synthesize on + * the fly a Unicode charmap from the CFF/Type~1 driver through a big + * table named the 'Adobe Glyph List' (AGL). + * + * Undefine this macro if you do not want the Adobe Glyph List compiled + * in your 'psnames' module. The Type~1 driver will not be able to + * synthesize a Unicode charmap out of the glyphs found in the fonts. + */ +#define FT_CONFIG_OPTION_ADOBE_GLYPH_LIST + +/************************************************************************** + * + * Support for Mac fonts + * + * Define this macro if you want support for outline fonts in Mac format + * (mac dfont, mac resource, macbinary containing a mac resource) on + * non-Mac platforms. + * + * Note that the 'FOND' resource isn't checked. + */ +/* #define FT_CONFIG_OPTION_MAC_FONTS */ + +/************************************************************************** + * + * Guessing methods to access embedded resource forks + * + * Enable extra Mac fonts support on non-Mac platforms (e.g., GNU/Linux). + * + * Resource forks which include fonts data are stored sometimes in + * locations which users or developers don't expected. In some cases, + * resource forks start with some offset from the head of a file. In + * other cases, the actual resource fork is stored in file different from + * what the user specifies. If this option is activated, FreeType tries + * to guess whether such offsets or different file names must be used. + * + * Note that normal, direct access of resource forks is controlled via + * the `FT_CONFIG_OPTION_MAC_FONTS` option. + */ +#ifdef FT_CONFIG_OPTION_MAC_FONTS + #define FT_CONFIG_OPTION_GUESSING_EMBEDDED_RFORK +#endif + +/************************************************************************** + * + * Allow the use of `FT_Incremental_Interface` to load typefaces that + * contain no glyph data, but supply it via a callback function. This is + * required by clients supporting document formats which supply font data + * incrementally as the document is parsed, such as the Ghostscript + * interpreter for the PostScript language. + */ +#define FT_CONFIG_OPTION_INCREMENTAL + +/************************************************************************** + * + * The size in bytes of the render pool used by the scan-line converter to + * do all of its work. + */ +#define FT_RENDER_POOL_SIZE 16384L + +/************************************************************************** + * + * FT_MAX_MODULES + * + * The maximum number of modules that can be registered in a single + * FreeType library object. 32~is the default. + */ +#define FT_MAX_MODULES 32 + +/************************************************************************** + * + * Debug level + * + * FreeType can be compiled in debug or trace mode. In debug mode, + * errors are reported through the 'ftdebug' component. In trace mode, + * additional messages are sent to the standard output during execution. + * + * Define `FT_DEBUG_LEVEL_ERROR` to build the library in debug mode. + * Define `FT_DEBUG_LEVEL_TRACE` to build it in trace mode. + * + * Don't define any of these macros to compile in 'release' mode! + * + * Do not `#undef` these macros here since the build system might define + * them for certain configurations only. + */ +/* #define FT_DEBUG_LEVEL_ERROR */ +/* #define FT_DEBUG_LEVEL_TRACE */ + +/************************************************************************** + * + * Logging + * + * Compiling FreeType in debug or trace mode makes FreeType write error + * and trace log messages to `stderr`. Enabling this macro + * automatically forces the `FT_DEBUG_LEVEL_ERROR` and + * `FT_DEBUG_LEVEL_TRACE` macros and allows FreeType to write error and + * trace log messages to a file instead of `stderr`. For writing logs + * to a file, FreeType uses an the external `dlg` library (the source + * code is in `src/dlg`). + * + * This option needs a C99 compiler. + */ +/* #define FT_DEBUG_LOGGING */ + +/************************************************************************** + * + * Autofitter debugging + * + * If `FT_DEBUG_AUTOFIT` is defined, FreeType provides some means to + * control the autofitter behaviour for debugging purposes with global + * boolean variables (consequently, you should **never** enable this + * while compiling in 'release' mode): + * + * ``` + * _af_debug_disable_horz_hints + * _af_debug_disable_vert_hints + * _af_debug_disable_blue_hints + * ``` + * + * Additionally, the following functions provide dumps of various + * internal autofit structures to stdout (using `printf`): + * + * ``` + * af_glyph_hints_dump_points + * af_glyph_hints_dump_segments + * af_glyph_hints_dump_edges + * af_glyph_hints_get_num_segments + * af_glyph_hints_get_segment_offset + * ``` + * + * As an argument, they use another global variable: + * + * ``` + * _af_debug_hints + * ``` + * + * Please have a look at the `ftgrid` demo program to see how those + * variables and macros should be used. + * + * Do not `#undef` these macros here since the build system might define + * them for certain configurations only. + */ +/* #define FT_DEBUG_AUTOFIT */ + +/************************************************************************** + * + * Memory Debugging + * + * FreeType now comes with an integrated memory debugger that is capable + * of detecting simple errors like memory leaks or double deletes. To + * compile it within your build of the library, you should define + * `FT_DEBUG_MEMORY` here. + * + * Note that the memory debugger is only activated at runtime when when + * the _environment_ variable `FT2_DEBUG_MEMORY` is defined also! + * + * Do not `#undef` this macro here since the build system might define it + * for certain configurations only. + */ +/* #define FT_DEBUG_MEMORY */ + +/************************************************************************** + * + * Module errors + * + * If this macro is set (which is _not_ the default), the higher byte of + * an error code gives the module in which the error has occurred, while + * the lower byte is the real error code. + * + * Setting this macro makes sense for debugging purposes only, since it + * would break source compatibility of certain programs that use + * FreeType~2. + * + * More details can be found in the files `ftmoderr.h` and `fterrors.h`. + */ +#undef FT_CONFIG_OPTION_USE_MODULE_ERRORS + +/************************************************************************** + * + * OpenType SVG Glyph Support + * + * Setting this macro enables support for OpenType SVG glyphs. By + * default, FreeType can only fetch SVG documents. However, it can also + * render them if external rendering hook functions are plugged in at + * runtime. + * + * More details on the hooks can be found in file `otsvg.h`. + */ +#define FT_CONFIG_OPTION_SVG + +/************************************************************************** + * + * Error Strings + * + * If this macro is set, `FT_Error_String` will return meaningful + * descriptions. This is not enabled by default to reduce the overall + * size of FreeType. + * + * More details can be found in the file `fterrors.h`. + */ +#define FT_CONFIG_OPTION_ERROR_STRINGS + +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** S F N T D R I V E R C O N F I G U R A T I O N ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ + +/************************************************************************** + * + * Define `TT_CONFIG_OPTION_EMBEDDED_BITMAPS` if you want to support + * embedded bitmaps in all formats using the 'sfnt' module (namely + * TrueType~& OpenType). + */ +#define TT_CONFIG_OPTION_EMBEDDED_BITMAPS + +/************************************************************************** + * + * Define `TT_CONFIG_OPTION_COLOR_LAYERS` if you want to support colored + * outlines (from the 'COLR'/'CPAL' tables) in all formats using the 'sfnt' + * module (namely TrueType~& OpenType). + */ +#define TT_CONFIG_OPTION_COLOR_LAYERS + +/************************************************************************** + * + * Define `TT_CONFIG_OPTION_POSTSCRIPT_NAMES` if you want to be able to + * load and enumerate the glyph Postscript names in a TrueType or OpenType + * file. + * + * Note that when you do not compile the 'psnames' module by undefining the + * above `FT_CONFIG_OPTION_POSTSCRIPT_NAMES`, the 'sfnt' module will + * contain additional code used to read the PS Names table from a font. + * + * (By default, the module uses 'psnames' to extract glyph names.) + */ +#define TT_CONFIG_OPTION_POSTSCRIPT_NAMES + +/************************************************************************** + * + * Define `TT_CONFIG_OPTION_SFNT_NAMES` if your applications need to access + * the internal name table in a SFNT-based format like TrueType or + * OpenType. The name table contains various strings used to describe the + * font, like family name, copyright, version, etc. It does not contain + * any glyph name though. + * + * Accessing SFNT names is done through the functions declared in + * `ftsnames.h`. + */ +#define TT_CONFIG_OPTION_SFNT_NAMES + +/************************************************************************** + * + * TrueType CMap support + * + * Here you can fine-tune which TrueType CMap table format shall be + * supported. + */ +#define TT_CONFIG_CMAP_FORMAT_0 +#define TT_CONFIG_CMAP_FORMAT_2 +#define TT_CONFIG_CMAP_FORMAT_4 +#define TT_CONFIG_CMAP_FORMAT_6 +#define TT_CONFIG_CMAP_FORMAT_8 +#define TT_CONFIG_CMAP_FORMAT_10 +#define TT_CONFIG_CMAP_FORMAT_12 +#define TT_CONFIG_CMAP_FORMAT_13 +#define TT_CONFIG_CMAP_FORMAT_14 + +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** T R U E T Y P E D R I V E R C O N F I G U R A T I O N ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ + +/************************************************************************** + * + * Define `TT_CONFIG_OPTION_BYTECODE_INTERPRETER` if you want to compile a + * bytecode interpreter in the TrueType driver. + * + * By undefining this, you will only compile the code necessary to load + * TrueType glyphs without hinting. + * + * Do not `#undef` this macro here, since the build system might define it + * for certain configurations only. + */ +#define TT_CONFIG_OPTION_BYTECODE_INTERPRETER + +/************************************************************************** + * + * Define `TT_CONFIG_OPTION_SUBPIXEL_HINTING` if you want to compile + * subpixel hinting support into the TrueType driver. This modifies the + * TrueType hinting mechanism when anything but `FT_RENDER_MODE_MONO` is + * requested. + * + * In particular, it modifies the bytecode interpreter to interpret (or + * not) instructions in a certain way so that all TrueType fonts look like + * they do in a Windows ClearType (DirectWrite) environment. See [1] for a + * technical overview on what this means. See `ttinterp.h` for more + * details on the LEAN option. + * + * There are three possible values. + * + * Value 1: + * This value is associated with the 'Infinality' moniker, contributed by + * an individual nicknamed Infinality with the goal of making TrueType + * fonts render better than on Windows. A high amount of configurability + * and flexibility, down to rules for single glyphs in fonts, but also + * very slow. Its experimental and slow nature and the original + * developer losing interest meant that this option was never enabled in + * default builds. + * + * The corresponding interpreter version is v38. + * + * Value 2: + * The new default mode for the TrueType driver. The Infinality code + * base was stripped to the bare minimum and all configurability removed + * in the name of speed and simplicity. The configurability was mainly + * aimed at legacy fonts like 'Arial', 'Times New Roman', or 'Courier'. + * Legacy fonts are fonts that modify vertical stems to achieve clean + * black-and-white bitmaps. The new mode focuses on applying a minimal + * set of rules to all fonts indiscriminately so that modern and web + * fonts render well while legacy fonts render okay. + * + * The corresponding interpreter version is v40. + * + * Value 3: + * Compile both, making both v38 and v40 available (the latter is the + * default). + * + * By undefining these, you get rendering behavior like on Windows without + * ClearType, i.e., Windows XP without ClearType enabled and Win9x + * (interpreter version v35). Or not, depending on how much hinting blood + * and testing tears the font designer put into a given font. If you + * define one or both subpixel hinting options, you can switch between + * between v35 and the ones you define (using `FT_Property_Set`). + * + * This option requires `TT_CONFIG_OPTION_BYTECODE_INTERPRETER` to be + * defined. + * + * [1] + * https://www.microsoft.com/typography/cleartype/truetypecleartype.aspx + */ +/* #define TT_CONFIG_OPTION_SUBPIXEL_HINTING 1 */ +#define TT_CONFIG_OPTION_SUBPIXEL_HINTING 2 +/* #define TT_CONFIG_OPTION_SUBPIXEL_HINTING ( 1 | 2 ) */ + +/************************************************************************** + * + * Define `TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED` to compile the + * TrueType glyph loader to use Apple's definition of how to handle + * component offsets in composite glyphs. + * + * Apple and MS disagree on the default behavior of component offsets in + * composites. Apple says that they should be scaled by the scaling + * factors in the transformation matrix (roughly, it's more complex) while + * MS says they should not. OpenType defines two bits in the composite + * flags array which can be used to disambiguate, but old fonts will not + * have them. + * + * https://www.microsoft.com/typography/otspec/glyf.htm + * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6glyf.html + */ +#undef TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED + +/************************************************************************** + * + * Define `TT_CONFIG_OPTION_GX_VAR_SUPPORT` if you want to include support + * for Apple's distortable font technology ('fvar', 'gvar', 'cvar', and + * 'avar' tables). Tagged 'Font Variations', this is now part of OpenType + * also. This has many similarities to Type~1 Multiple Masters support. + */ +#define TT_CONFIG_OPTION_GX_VAR_SUPPORT + +/************************************************************************** + * + * Define `TT_CONFIG_OPTION_BDF` if you want to include support for an + * embedded 'BDF~' table within SFNT-based bitmap formats. + */ +#define TT_CONFIG_OPTION_BDF + +/************************************************************************** + * + * Option `TT_CONFIG_OPTION_MAX_RUNNABLE_OPCODES` controls the maximum + * number of bytecode instructions executed for a single run of the + * bytecode interpreter, needed to prevent infinite loops. You don't want + * to change this except for very special situations (e.g., making a + * library fuzzer spend less time to handle broken fonts). + * + * It is not expected that this value is ever modified by a configuring + * script; instead, it gets surrounded with `#ifndef ... #endif` so that + * the value can be set as a preprocessor option on the compiler's command + * line. + */ +#ifndef TT_CONFIG_OPTION_MAX_RUNNABLE_OPCODES + #define TT_CONFIG_OPTION_MAX_RUNNABLE_OPCODES 1000000L +#endif + +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** T Y P E 1 D R I V E R C O N F I G U R A T I O N ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ + +/************************************************************************** + * + * `T1_MAX_DICT_DEPTH` is the maximum depth of nest dictionaries and arrays + * in the Type~1 stream (see `t1load.c`). A minimum of~4 is required. + */ +#define T1_MAX_DICT_DEPTH 5 + +/************************************************************************** + * + * `T1_MAX_SUBRS_CALLS` details the maximum number of nested sub-routine + * calls during glyph loading. + */ +#define T1_MAX_SUBRS_CALLS 16 + +/************************************************************************** + * + * `T1_MAX_CHARSTRING_OPERANDS` is the charstring stack's capacity. A + * minimum of~16 is required. + * + * The Chinese font 'MingTiEG-Medium' (covering the CNS 11643 character + * set) needs 256. + */ +#define T1_MAX_CHARSTRINGS_OPERANDS 256 + +/************************************************************************** + * + * Define this configuration macro if you want to prevent the compilation + * of the 't1afm' module, which is in charge of reading Type~1 AFM files + * into an existing face. Note that if set, the Type~1 driver will be + * unable to produce kerning distances. + */ +#undef T1_CONFIG_OPTION_NO_AFM + +/************************************************************************** + * + * Define this configuration macro if you want to prevent the compilation + * of the Multiple Masters font support in the Type~1 driver. + */ +#undef T1_CONFIG_OPTION_NO_MM_SUPPORT + +/************************************************************************** + * + * `T1_CONFIG_OPTION_OLD_ENGINE` controls whether the pre-Adobe Type~1 + * engine gets compiled into FreeType. If defined, it is possible to + * switch between the two engines using the `hinting-engine` property of + * the 'type1' driver module. + */ +/* #define T1_CONFIG_OPTION_OLD_ENGINE */ + +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** C F F D R I V E R C O N F I G U R A T I O N ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ + +/************************************************************************** + * + * Using `CFF_CONFIG_OPTION_DARKENING_PARAMETER_{X,Y}{1,2,3,4}` it is + * possible to set up the default values of the four control points that + * define the stem darkening behaviour of the (new) CFF engine. For more + * details please read the documentation of the `darkening-parameters` + * property (file `ftdriver.h`), which allows the control at run-time. + * + * Do **not** undefine these macros! + */ +#define CFF_CONFIG_OPTION_DARKENING_PARAMETER_X1 500 +#define CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y1 400 + +#define CFF_CONFIG_OPTION_DARKENING_PARAMETER_X2 1000 +#define CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y2 275 + +#define CFF_CONFIG_OPTION_DARKENING_PARAMETER_X3 1667 +#define CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y3 275 + +#define CFF_CONFIG_OPTION_DARKENING_PARAMETER_X4 2333 +#define CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y4 0 + +/************************************************************************** + * + * `CFF_CONFIG_OPTION_OLD_ENGINE` controls whether the pre-Adobe CFF engine + * gets compiled into FreeType. If defined, it is possible to switch + * between the two engines using the `hinting-engine` property of the 'cff' + * driver module. + */ +/* #define CFF_CONFIG_OPTION_OLD_ENGINE */ + +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** P C F D R I V E R C O N F I G U R A T I O N ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ + +/************************************************************************** + * + * There are many PCF fonts just called 'Fixed' which look completely + * different, and which have nothing to do with each other. When selecting + * 'Fixed' in KDE or Gnome one gets results that appear rather random, the + * style changes often if one changes the size and one cannot select some + * fonts at all. This option makes the 'pcf' module prepend the foundry + * name (plus a space) to the family name. + * + * We also check whether we have 'wide' characters; all put together, we + * get family names like 'Sony Fixed' or 'Misc Fixed Wide'. + * + * If this option is activated, it can be controlled with the + * `no-long-family-names` property of the 'pcf' driver module. + */ +/* #define PCF_CONFIG_OPTION_LONG_FAMILY_NAMES */ + +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** A U T O F I T M O D U L E C O N F I G U R A T I O N ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ + +/************************************************************************** + * + * Compile 'autofit' module with CJK (Chinese, Japanese, Korean) script + * support. + */ +#define AF_CONFIG_OPTION_CJK + +/************************************************************************** + * + * Compile 'autofit' module with fallback Indic script support, covering + * some scripts that the 'latin' submodule of the 'autofit' module doesn't + * (yet) handle. Currently, this needs option `AF_CONFIG_OPTION_CJK`. + */ +#ifdef AF_CONFIG_OPTION_CJK + #define AF_CONFIG_OPTION_INDIC +#endif + +/************************************************************************** + * + * Use TrueType-like size metrics for 'light' auto-hinting. + * + * It is strongly recommended to avoid this option, which exists only to + * help some legacy applications retain its appearance and behaviour with + * respect to auto-hinted TrueType fonts. + * + * The very reason this option exists at all are GNU/Linux distributions + * like Fedora that did not un-patch the following change (which was + * present in FreeType between versions 2.4.6 and 2.7.1, inclusive). + * + * ``` + * 2011-07-16 Steven Chu + * + * [truetype] Fix metrics on size request for scalable fonts. + * ``` + * + * This problematic commit is now reverted (more or less). + */ +/* #define AF_CONFIG_OPTION_TT_SIZE_METRICS */ + +/* */ + +/* + * This macro is obsolete. Support has been removed in FreeType version + * 2.5. + */ +/* #define FT_CONFIG_OPTION_OLD_INTERNALS */ + +/* + * The next three macros are defined if native TrueType hinting is + * requested by the definitions above. Don't change this. + */ +#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER + #define TT_USE_BYTECODE_INTERPRETER + + #ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING + #if TT_CONFIG_OPTION_SUBPIXEL_HINTING & 1 + #define TT_SUPPORT_SUBPIXEL_HINTING_INFINALITY + #endif + + #if TT_CONFIG_OPTION_SUBPIXEL_HINTING & 2 + #define TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL + #endif + #endif +#endif + +/* + * The TT_SUPPORT_COLRV1 macro is defined to indicate to clients that this + * version of FreeType has support for 'COLR' v1 API. This definition is + * useful to FreeType clients that want to build in support for 'COLR' v1 + * depending on a tip-of-tree checkout before it is officially released in + * FreeType, and while the feature cannot yet be tested against using + * version macros. Don't change this macro. This may be removed once the + * feature is in a FreeType release version and version macros can be used + * to test for availability. + */ +#ifdef TT_CONFIG_OPTION_COLOR_LAYERS + #define TT_SUPPORT_COLRV1 +#endif + +/* + * Check CFF darkening parameters. The checks are the same as in function + * `cff_property_set` in file `cffdrivr.c`. + */ +#if CFF_CONFIG_OPTION_DARKENING_PARAMETER_X1 < 0 || \ + CFF_CONFIG_OPTION_DARKENING_PARAMETER_X2 < 0 || \ + CFF_CONFIG_OPTION_DARKENING_PARAMETER_X3 < 0 || \ + CFF_CONFIG_OPTION_DARKENING_PARAMETER_X4 < 0 || \ + \ + CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y1 < 0 || \ + CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y2 < 0 || \ + CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y3 < 0 || \ + CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y4 < 0 || \ + \ + CFF_CONFIG_OPTION_DARKENING_PARAMETER_X1 > \ + CFF_CONFIG_OPTION_DARKENING_PARAMETER_X2 || \ + CFF_CONFIG_OPTION_DARKENING_PARAMETER_X2 > \ + CFF_CONFIG_OPTION_DARKENING_PARAMETER_X3 || \ + CFF_CONFIG_OPTION_DARKENING_PARAMETER_X3 > \ + CFF_CONFIG_OPTION_DARKENING_PARAMETER_X4 || \ + \ + CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y1 > 500 || \ + CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y2 > 500 || \ + CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y3 > 500 || \ + CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y4 > 500 + #error "Invalid CFF darkening parameters!" +#endif + +FT_END_HEADER + +#endif /* FTOPTION_H_ */ + +/* END */ diff --git a/include/liblvgl/libs/freetype/lv_freetype.h b/include/liblvgl/libs/freetype/lv_freetype.h new file mode 100644 index 00000000..76b9f7b5 --- /dev/null +++ b/include/liblvgl/libs/freetype/lv_freetype.h @@ -0,0 +1,127 @@ +/** + * @file lv_freetype.h + * + */ +#ifndef LV_FREETYPE_H +#define LV_FREETYPE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../lv_conf_internal.h" +#include "../../misc/lv_types.h" +#include "../../misc/lv_event.h" +#include LV_STDBOOL_INCLUDE + +#if LV_USE_FREETYPE + +/********************* + * DEFINES + *********************/ + +#define LV_FREETYPE_F26DOT6_TO_INT(x) ((x) >> 6) +#define LV_FREETYPE_F26DOT6_TO_FLOAT(x) ((float)(x) / 64) + +#define FT_FONT_STYLE_NORMAL LV_FREETYPE_FONT_STYLE_NORMAL +#define FT_FONT_STYLE_ITALIC LV_FREETYPE_FONT_STYLE_ITALIC +#define FT_FONT_STYLE_BOLD LV_FREETYPE_FONT_STYLE_BOLD + +/********************** + * TYPEDEFS + **********************/ + +typedef enum { + LV_FREETYPE_FONT_STYLE_NORMAL = 0, + LV_FREETYPE_FONT_STYLE_ITALIC = 1 << 0, + LV_FREETYPE_FONT_STYLE_BOLD = 1 << 1, +} lv_freetype_font_style_t; + +typedef lv_freetype_font_style_t LV_FT_FONT_STYLE; + +typedef enum { + LV_FREETYPE_FONT_RENDER_MODE_BITMAP = 0, + LV_FREETYPE_FONT_RENDER_MODE_OUTLINE = 1, +} lv_freetype_font_render_mode_t; + +typedef void * lv_freetype_outline_t; + +typedef enum { + LV_FREETYPE_OUTLINE_END, + LV_FREETYPE_OUTLINE_MOVE_TO, + LV_FREETYPE_OUTLINE_LINE_TO, + LV_FREETYPE_OUTLINE_CUBIC_TO, + LV_FREETYPE_OUTLINE_CONIC_TO, +} lv_freetype_outline_type_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Initialize the freetype library. + * @return LV_RESULT_OK on success, otherwise LV_RESULT_INVALID. + */ +lv_result_t lv_freetype_init(uint32_t max_glyph_cnt); + +/** + * Uninitialize the freetype library + */ +void lv_freetype_uninit(void); + +/** + * Create a freetype font. + * @param pathname font file path. + * @param render_mode font render mode(see @lv_freetype_font_render_mode_t for details). + * @param size font size. + * @param style font style(see lv_freetype_font_style_t for details). + * @return Created font, or NULL on failure. + */ +lv_font_t * lv_freetype_font_create(const char * pathname, lv_freetype_font_render_mode_t render_mode, uint32_t size, + lv_freetype_font_style_t style); + +/** + * Delete a freetype font. + * @param font freetype font to be deleted. + */ +void lv_freetype_font_delete(lv_font_t * font); + +/** + * Register a callback function to generate outlines for FreeType fonts. + * + * @param cb The callback function to be registered. + * @param user_data User data to be passed to the callback function. + * @return The ID of the registered callback function, or a negative value on failure. + */ +void lv_freetype_outline_add_event(lv_event_cb_t event_cb, lv_event_code_t filter, void * user_data); + +/** + * Get the scale of a FreeType font. + * + * @param font The FreeType font to get the scale of. + * @return The scale of the FreeType font. + */ +uint32_t lv_freetype_outline_get_scale(const lv_font_t * font); + +/** + * Check if the font is an outline font. + * + * @param font The FreeType font. + * @return Is outline font on success, otherwise false. + */ +bool lv_freetype_is_outline_font(const lv_font_t * font); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_FREETYPE*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LV_FREETYPE_H */ diff --git a/include/liblvgl/libs/freetype/lv_freetype_private.h b/include/liblvgl/libs/freetype/lv_freetype_private.h new file mode 100644 index 00000000..86c87025 --- /dev/null +++ b/include/liblvgl/libs/freetype/lv_freetype_private.h @@ -0,0 +1,151 @@ +/** + * @file lv_freetype_private.h + * + */ + +#ifndef LV_FREETYPE_PRIVATE_H +#define LV_FREETYPE_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_freetype.h" +#include "../../misc/cache/lv_cache.h" +#include "../../misc/lv_ll.h" +#include "../../font/lv_font.h" + +#if LV_USE_FREETYPE + +#include "ft2build.h" +#include FT_FREETYPE_H +#include FT_GLYPH_H +#include FT_CACHE_H +#include FT_SIZES_H +#include FT_IMAGE_H +#include FT_OUTLINE_H + +/********************* + * DEFINES + *********************/ + +#ifdef FT_CONFIG_OPTION_ERROR_STRINGS +#define FT_ERROR_MSG(msg, error_code) \ + LV_LOG_ERROR(msg " error(0x%x): %s", (int)error_code, FT_Error_String(error_code)) +#else +#define FT_ERROR_MSG(msg, error_code) \ + LV_LOG_ERROR(msg " error(0x%x)", (int)error_code) +#endif + +#define LV_FREETYPE_FONT_DSC_MAGIC_NUM 0x5F5F4654 /* '__FT' */ +#define LV_FREETYPE_FONT_DSC_HAS_MAGIC_NUM(dsc) ((dsc)->magic_num == LV_FREETYPE_FONT_DSC_MAGIC_NUM) +#define LV_ASSERT_FREETYPE_FONT_DSC(dsc) \ + do { \ + LV_ASSERT_NULL(dsc); \ + LV_ASSERT_FORMAT_MSG(LV_FREETYPE_FONT_DSC_HAS_MAGIC_NUM(dsc), \ + "Invalid font descriptor: 0x%" LV_PRIx32, (dsc)->magic_num); \ + } while (0) + +#define FT_INT_TO_F26DOT6(x) ((x) << 6) +#define FT_F26DOT6_TO_INT(x) ((x) >> 6) + +#define FT_INT_TO_F16DOT16(x) ((x) << 16) +#define FT_F16DOT16_TO_INT(x) ((x) >> 16) + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_freetype_outline_vector_t { + int32_t x; + int32_t y; +}; + +struct _lv_freetype_outline_event_param_t { + lv_freetype_outline_t outline; + lv_freetype_outline_type_t type; + lv_freetype_outline_vector_t to; + lv_freetype_outline_vector_t control1; + lv_freetype_outline_vector_t control2; +}; + + +typedef struct _lv_freetype_cache_node_t lv_freetype_cache_node_t; + +struct _lv_freetype_cache_node_t { + const char * pathname; + lv_freetype_font_style_t style; + lv_freetype_font_render_mode_t render_mode; + + uint32_t ref_size; /**< Reference size for calculating outline glyph's real size.*/ + + FT_Face face; + lv_mutex_t face_lock; + + /*glyph cache*/ + lv_cache_t * glyph_cache; + + /*draw data cache*/ + lv_cache_t * draw_data_cache; +}; + +typedef struct _lv_freetype_context_t { + FT_Library library; + lv_ll_t face_id_ll; + lv_event_cb_t event_cb; + + uint32_t max_glyph_cnt; + + lv_cache_t * cache_node_cache; +} lv_freetype_context_t; + +typedef struct _lv_freetype_font_dsc_t { + uint32_t magic_num; + lv_font_t font; + uint32_t size; + lv_freetype_font_style_t style; + lv_freetype_font_render_mode_t render_mode; + lv_freetype_context_t * context; + lv_freetype_cache_node_t * cache_node; + lv_cache_entry_t * cache_node_entry; + FTC_FaceID face_id; +} lv_freetype_font_dsc_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Get the FreeType context. + * + * @return A pointer to the FreeType context used by LittlevGL. + */ +lv_freetype_context_t * lv_freetype_get_context(void); + +void lv_freetype_italic_transform(FT_Face face); +int32_t lv_freetype_italic_transform_on_pos(lv_point_t point); + +lv_cache_t * lv_freetype_create_glyph_cache(uint32_t cache_size); +void lv_freetype_set_cbs_glyph(lv_freetype_font_dsc_t * dsc); + +lv_cache_t * lv_freetype_create_draw_data_image(uint32_t cache_size); +void lv_freetype_set_cbs_image_font(lv_freetype_font_dsc_t * dsc); + +lv_cache_t * lv_freetype_create_draw_data_outline(uint32_t cache_size); +void lv_freetype_set_cbs_outline_font(lv_freetype_font_dsc_t * dsc); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_FREETYPE*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_FREETYPE_PRIVATE_H*/ diff --git a/include/liblvgl/libs/fsdrv/lv_fs_arduino_esp_littlefs.cpp b/include/liblvgl/libs/fsdrv/lv_fs_arduino_esp_littlefs.cpp new file mode 100644 index 00000000..ca80d902 --- /dev/null +++ b/include/liblvgl/libs/fsdrv/lv_fs_arduino_esp_littlefs.cpp @@ -0,0 +1,201 @@ +#include "liblvgl/lvgl.h" +#if LV_USE_FS_ARDUINO_ESP_LITTLEFS + +#include "../../core/lv_global.h" +#include "LittleFS.h" + +#if LV_FS_ARDUINO_ESP_LITTLEFS_LETTER == '\0' + #error "LV_FS_ARDUINO_ESP_LITTLEFS_LETTER must be set to a valid value" +#else + #if (LV_FS_ARDUINO_ESP_LITTLEFS_LETTER < 'A') || (LV_FS_ARDUINO_ESP_LITTLEFS_LETTER > 'Z') + #if LV_FS_DEFAULT_DRIVE_LETTER != '\0' /*When using default drive letter, strict format (X:) is mandatory*/ + #error "LV_FS_ARDUINO_ESP_LITTLEFS_LETTER must be an upper case ASCII letter" + #else /*Lean rules for backward compatibility*/ + #warning LV_FS_ARDUINO_ESP_LITTLEFS_LETTER should be an upper case ASCII letter. \ + Using a slash symbol as drive letter should be replaced with LV_FS_DEFAULT_DRIVE_LETTER mechanism + #endif + #endif +#endif + +typedef struct ArduinoEspLittleFile { + File file; +} ArduinoEspLittleFile; + +/********************** + * STATIC PROTOTYPES + **********************/ +static void fs_init(void); +static void * fs_open(lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode); +static lv_fs_res_t fs_close(lv_fs_drv_t * drv, void * file_p); +static lv_fs_res_t fs_read(lv_fs_drv_t * drv, void * file_p, void * buf, uint32_t btr, uint32_t * br); +static lv_fs_res_t fs_write(lv_fs_drv_t * drv, void * file_p, const void * buf, uint32_t btw, uint32_t * bw); +static lv_fs_res_t fs_seek(lv_fs_drv_t * drv, void * file_p, uint32_t pos, lv_fs_whence_t whence); +static lv_fs_res_t fs_tell(lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p); + +/** + * Register a driver for the LittleFS File System interface + */ +extern "C" void lv_fs_arduino_esp_littlefs_init(void) +{ + fs_init(); + + lv_fs_drv_t * fs_drv = &(LV_GLOBAL_DEFAULT()->arduino_esp_littlefs_fs_drv); + lv_fs_drv_init(fs_drv); + + fs_drv->letter = LV_FS_ARDUINO_ESP_LITTLEFS_LETTER; + fs_drv->open_cb = fs_open; + fs_drv->close_cb = fs_close; + fs_drv->read_cb = fs_read; + fs_drv->write_cb = fs_write; + fs_drv->seek_cb = fs_seek; + fs_drv->tell_cb = fs_tell; + + fs_drv->dir_close_cb = NULL; + fs_drv->dir_open_cb = NULL; + fs_drv->dir_read_cb = NULL; + + lv_fs_drv_register(fs_drv); +} + +/********************** + * STATIC FUNCTIONS + **********************/ + +/*Initialize your Storage device and File system.*/ +static void fs_init(void) +{ + LittleFS.begin(); +} + +/** + * Open a file + * @param drv pointer to a driver where this function belongs + * @param path path to the file beginning with the driver letter (e.g. S:/folder/file.txt) + * @param mode read: FS_MODE_RD, write: FS_MODE_WR, both: FS_MODE_RD | FS_MODE_WR + * @return a file descriptor or NULL on error + */ +static void * fs_open(lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode) +{ + LV_UNUSED(drv); + + const char * flags; + if(mode == LV_FS_MODE_WR) + flags = FILE_WRITE; + else if(mode == LV_FS_MODE_RD) + flags = FILE_READ; + else if(mode == (LV_FS_MODE_WR | LV_FS_MODE_RD)) + flags = FILE_WRITE; + + File file = LittleFS.open(path, flags); + if(!file) { + return NULL; + } + + ArduinoEspLittleFile * lf = new ArduinoEspLittleFile{file}; + + return (void *)lf; +} + +/** + * Close an opened file + * @param drv pointer to a driver where this function belongs + * @param file_p pointer to a file_t variable. (opened with fs_open) + * @return LV_FS_RES_OK: no error or any error from @lv_fs_res_t enum + */ +static lv_fs_res_t fs_close(lv_fs_drv_t * drv, void * file_p) +{ + LV_UNUSED(drv); + ArduinoEspLittleFile * lf = (ArduinoEspLittleFile *)file_p; + lf->file.close(); + delete lf; + + return LV_FS_RES_OK; +} + +/** + * Read data from an opened file + * @param drv pointer to a driver where this function belongs + * @param file_p pointer to a file_t variable. + * @param buf pointer to a memory block where to store the read data + * @param btr number of Bytes To Read + * @param br the real number of read bytes (Byte Read) + * @return LV_FS_RES_OK: no error or any error from @lv_fs_res_t enum + */ +static lv_fs_res_t fs_read(lv_fs_drv_t * drv, void * file_p, void * buf, uint32_t btr, uint32_t * br) +{ + LV_UNUSED(drv); + ArduinoEspLittleFile * lf = (ArduinoEspLittleFile *)file_p; + *br = lf->file.read((uint8_t *)buf, btr); + + return (int32_t)(*br) < 0 ? LV_FS_RES_UNKNOWN : LV_FS_RES_OK; +} + +/** + * Write into a file + * @param drv pointer to a driver where this function belongs + * @param file_p pointer to a file_t variable + * @param buf pointer to a buffer with the bytes to write + * @param btw Bytes To Write + * @param bw the number of real written bytes (Bytes Written) + * @return LV_FS_RES_OK: no error or any error from @lv_fs_res_t enum + */ +static lv_fs_res_t fs_write(lv_fs_drv_t * drv, void * file_p, const void * buf, uint32_t btw, uint32_t * bw) +{ + LV_UNUSED(drv); + ArduinoEspLittleFile * lf = (ArduinoEspLittleFile *)file_p; + *bw = lf->file.write((uint8_t *)buf, btw); + + return (int32_t)(*bw) < 0 ? LV_FS_RES_UNKNOWN : LV_FS_RES_OK; +} + +/** + * Set the read write pointer. Also expand the file size if necessary. + * @param drv pointer to a driver where this function belongs + * @param file_p pointer to a file_t variable. (opened with fs_open ) + * @param pos the new position of read write pointer + * @param whence tells from where to interpret the `pos`. See @lv_fs_whence_t + * @return LV_FS_RES_OK: no error or any error from @lv_fs_res_t enum + */ +static lv_fs_res_t fs_seek(lv_fs_drv_t * drv, void * file_p, uint32_t pos, lv_fs_whence_t whence) +{ + LV_UNUSED(drv); + SeekMode mode; + if(whence == LV_FS_SEEK_SET) + mode = SeekSet; + else if(whence == LV_FS_SEEK_CUR) + mode = SeekCur; + else if(whence == LV_FS_SEEK_END) + mode = SeekEnd; + + ArduinoEspLittleFile * lf = (ArduinoEspLittleFile *)file_p; + + int rc = lf->file.seek(pos, mode); + + return rc < 0 ? LV_FS_RES_UNKNOWN : LV_FS_RES_OK; +} + +/** + * Give the position of the read write pointer + * @param drv pointer to a driver where this function belongs + * @param file_p pointer to a file_p variable + * @param pos_p pointer to store the result + * @return LV_FS_RES_OK: no error or any error from @lv_fs_res_t enum + */ +static lv_fs_res_t fs_tell(lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p) +{ + LV_UNUSED(drv); + ArduinoEspLittleFile * lf = (ArduinoEspLittleFile *)file_p; + + *pos_p = lf->file.position(); + + return (int32_t)(*pos_p) < 0 ? LV_FS_RES_UNKNOWN : LV_FS_RES_OK; +} + +#else /*LV_USE_FS_ARDUINO_ESP_LITTLEFS == 0*/ + +#if defined(LV_FS_ARDUINO_ESP_LITTLEFS_LETTER) && LV_FS_ARDUINO_ESP_LITTLEFS_LETTER != '\0' + #warning "LV_USE_FS_ARDUINO_ESP_LITTLEFS is not enabled but LV_FS_ARDUINO_ESP_LITTLEFS_LETTER is set" +#endif + +#endif /*LV_USE_FS_ARDUINO_ESP_LITTLEFS*/ + diff --git a/include/liblvgl/libs/fsdrv/lv_fs_arduino_sd.cpp b/include/liblvgl/libs/fsdrv/lv_fs_arduino_sd.cpp new file mode 100644 index 00000000..88974d6f --- /dev/null +++ b/include/liblvgl/libs/fsdrv/lv_fs_arduino_sd.cpp @@ -0,0 +1,192 @@ +#include "liblvgl/lvgl.h" +#if LV_USE_FS_ARDUINO_SD + +#include "../../core/lv_global.h" +#include +#include "SD.h" + +#if LV_FS_ARDUINO_SD_LETTER == '\0' + #error "LV_FS_ARDUINO_SD_LETTER must be set to a valid value" +#else + #if (LV_FS_ARDUINO_SD_LETTER < 'A') || (LV_FS_ARDUINO_SD_LETTER > 'Z') + #if LV_FS_DEFAULT_DRIVE_LETTER != '\0' /*When using default drive letter, strict format (X:) is mandatory*/ + #error "LV_FS_ARDUINO_SD_LETTER must be an upper case ASCII letter" + #else /*Lean rules for backward compatibility*/ + #warning LV_FS_ARDUINO_SD_LETTER should be an upper case ASCII letter. \ + Using a slash symbol as drive letter should be replaced with LV_FS_DEFAULT_DRIVE_LETTER mechanism + #endif + #endif +#endif + +typedef struct SdFile { + File file; +} SdFile; + +/********************** + * STATIC PROTOTYPES + **********************/ +static void * fs_open(lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode); +static lv_fs_res_t fs_close(lv_fs_drv_t * drv, void * file_p); +static lv_fs_res_t fs_read(lv_fs_drv_t * drv, void * file_p, void * buf, uint32_t btr, uint32_t * br); +static lv_fs_res_t fs_write(lv_fs_drv_t * drv, void * file_p, const void * buf, uint32_t btw, uint32_t * bw); +static lv_fs_res_t fs_seek(lv_fs_drv_t * drv, void * file_p, uint32_t pos, lv_fs_whence_t whence); +static lv_fs_res_t fs_tell(lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p); + +/** + * Register a driver for the SD File System interface + */ +extern "C" void lv_fs_arduino_sd_init(void) +{ + lv_fs_drv_t * fs_drv = &(LV_GLOBAL_DEFAULT()->arduino_sd_fs_drv); + lv_fs_drv_init(fs_drv); + + fs_drv->letter = LV_FS_ARDUINO_SD_LETTER; + fs_drv->open_cb = fs_open; + fs_drv->close_cb = fs_close; + fs_drv->read_cb = fs_read; + fs_drv->write_cb = fs_write; + fs_drv->seek_cb = fs_seek; + fs_drv->tell_cb = fs_tell; + + fs_drv->dir_close_cb = NULL; + fs_drv->dir_open_cb = NULL; + fs_drv->dir_read_cb = NULL; + + lv_fs_drv_register(fs_drv); +} + +/********************** + * STATIC FUNCTIONS + **********************/ + +/** + * Open a file + * @param drv pointer to a driver where this function belongs + * @param path path to the file beginning with the driver letter (e.g. S:/folder/file.txt) + * @param mode read: FS_MODE_RD, write: FS_MODE_WR, both: FS_MODE_RD | FS_MODE_WR + * @return a file descriptor or NULL on error + */ +static void * fs_open(lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode) +{ + LV_UNUSED(drv); + + const char * flags; + if(mode == LV_FS_MODE_WR) + flags = FILE_WRITE; + else if(mode == LV_FS_MODE_RD) + flags = FILE_READ; + else if(mode == (LV_FS_MODE_WR | LV_FS_MODE_RD)) + flags = FILE_WRITE; + + File file = SD.open(path, flags); + if(!file) { + return NULL; + } + + SdFile * lf = new SdFile{file}; + + return (void *)lf; +} + +/** + * Close an opened file + * @param drv pointer to a driver where this function belongs + * @param file_p pointer to a file_t variable. (opened with fs_open) + * @return LV_FS_RES_OK: no error or any error from @lv_fs_res_t enum + */ +static lv_fs_res_t fs_close(lv_fs_drv_t * drv, void * file_p) +{ + LV_UNUSED(drv); + SdFile * lf = (SdFile *)file_p; + lf->file.close(); + delete lf; + + return LV_FS_RES_OK; +} + +/** + * Read data from an opened file + * @param drv pointer to a driver where this function belongs + * @param file_p pointer to a file_t variable. + * @param buf pointer to a memory block where to store the read data + * @param btr number of Bytes To Read + * @param br the real number of read bytes (Byte Read) + * @return LV_FS_RES_OK: no error or any error from @lv_fs_res_t enum + */ +static lv_fs_res_t fs_read(lv_fs_drv_t * drv, void * file_p, void * buf, uint32_t btr, uint32_t * br) +{ + LV_UNUSED(drv); + SdFile * lf = (SdFile *)file_p; + *br = lf->file.read((uint8_t *)buf, btr); + + return (int32_t)(*br) < 0 ? LV_FS_RES_UNKNOWN : LV_FS_RES_OK; +} + +/** + * Write into a file + * @param drv pointer to a driver where this function belongs + * @param file_p pointer to a file_t variable + * @param buf pointer to a buffer with the bytes to write + * @param btw Bytes To Write + * @param bw the number of real written bytes (Bytes Written) + * @return LV_FS_RES_OK: no error or any error from @lv_fs_res_t enum + */ +static lv_fs_res_t fs_write(lv_fs_drv_t * drv, void * file_p, const void * buf, uint32_t btw, uint32_t * bw) +{ + LV_UNUSED(drv); + SdFile * lf = (SdFile *)file_p; + *bw = lf->file.write((uint8_t *)buf, btw); + + return (int32_t)(*bw) < 0 ? LV_FS_RES_UNKNOWN : LV_FS_RES_OK; +} + +/** + * Set the read write pointer. Also expand the file size if necessary. + * @param drv pointer to a driver where this function belongs + * @param file_p pointer to a file_t variable. (opened with fs_open ) + * @param pos the new position of read write pointer + * @param whence tells from where to interpret the `pos`. See @lv_fs_whence_t + * @return LV_FS_RES_OK: no error or any error from @lv_fs_res_t enum + */ +static lv_fs_res_t fs_seek(lv_fs_drv_t * drv, void * file_p, uint32_t pos, lv_fs_whence_t whence) +{ + LV_UNUSED(drv); + SeekMode mode; + if(whence == LV_FS_SEEK_SET) + mode = SeekSet; + else if(whence == LV_FS_SEEK_CUR) + mode = SeekCur; + else if(whence == LV_FS_SEEK_END) + mode = SeekEnd; + + SdFile * lf = (SdFile *)file_p; + + int rc = lf->file.seek(pos, mode); + + return rc < 0 ? LV_FS_RES_UNKNOWN : LV_FS_RES_OK; +} + +/** + * Give the position of the read write pointer + * @param drv pointer to a driver where this function belongs + * @param file_p pointer to a file_p variable + * @param pos_p pointer to store the result + * @return LV_FS_RES_OK: no error or any error from @lv_fs_res_t enum + */ +static lv_fs_res_t fs_tell(lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p) +{ + LV_UNUSED(drv); + SdFile * lf = (SdFile *)file_p; + + *pos_p = lf->file.position(); + + return (int32_t)(*pos_p) < 0 ? LV_FS_RES_UNKNOWN : LV_FS_RES_OK; +} + +#else /*LV_USE_FS_ARDUINO_SD == 0*/ + +#if defined(LV_FS_ARDUINO_SD_LETTER) && LV_FS_ARDUINO_SD_LETTER != '\0' + #warning "LV_USE_FS_ARDUINO_SD is not enabled but LV_FS_ARDUINO_SD_LETTER is set" +#endif + +#endif /*LV_USE_FS_ARDUINO_SD*/ diff --git a/include/liblvgl/extra/libs/fsdrv/lv_fsdrv.h b/include/liblvgl/libs/fsdrv/lv_fsdrv.h similarity index 59% rename from include/liblvgl/extra/libs/fsdrv/lv_fsdrv.h rename to include/liblvgl/libs/fsdrv/lv_fsdrv.h index 2b0adfd0..f830c3a2 100644 --- a/include/liblvgl/extra/libs/fsdrv/lv_fsdrv.h +++ b/include/liblvgl/libs/fsdrv/lv_fsdrv.h @@ -13,7 +13,7 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" +#include "../../lv_conf_internal.h" /********************* * DEFINES @@ -27,22 +27,40 @@ extern "C" { * GLOBAL PROTOTYPES **********************/ -#if LV_USE_FS_FATFS != '\0' +#if LV_USE_FS_FATFS void lv_fs_fatfs_init(void); #endif -#if LV_USE_FS_STDIO != '\0' +#if LV_USE_FS_STDIO void lv_fs_stdio_init(void); #endif -#if LV_USE_FS_POSIX != '\0' +#if LV_USE_FS_POSIX void lv_fs_posix_init(void); #endif -#if LV_USE_FS_WIN32 != '\0' +#if LV_USE_FS_WIN32 void lv_fs_win32_init(void); #endif +#if LV_USE_FS_MEMFS +void lv_fs_memfs_init(void); +#endif + +#if LV_USE_FS_LITTLEFS +struct lfs; +void lv_littlefs_set_handler(struct lfs *); +void lv_fs_littlefs_init(void); +#endif + +#if LV_USE_FS_ARDUINO_ESP_LITTLEFS +void lv_fs_arduino_esp_littlefs_init(void); +#endif + +#if LV_USE_FS_ARDUINO_SD +void lv_fs_arduino_sd_init(void); +#endif + /********************** * MACROS **********************/ @@ -52,4 +70,3 @@ void lv_fs_win32_init(void); #endif #endif /*LV_FSDRV_H*/ - diff --git a/include/liblvgl/libs/gif/gifdec.h b/include/liblvgl/libs/gif/gifdec.h new file mode 100644 index 00000000..21ff210a --- /dev/null +++ b/include/liblvgl/libs/gif/gifdec.h @@ -0,0 +1,71 @@ +#ifndef GIFDEC_H +#define GIFDEC_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "../../misc/lv_fs.h" + +#if LV_USE_GIF +#include + +typedef struct _gd_Palette { + int size; + uint8_t colors[0x100 * 3]; +} gd_Palette; + +typedef struct _gd_GCE { + uint16_t delay; + uint8_t tindex; + uint8_t disposal; + int input; + int transparency; +} gd_GCE; + + + +typedef struct _gd_GIF { + lv_fs_file_t fd; + const char * data; + uint8_t is_file; + uint32_t f_rw_p; + int32_t anim_start; + uint16_t width, height; + uint16_t depth; + int32_t loop_count; + gd_GCE gce; + gd_Palette * palette; + gd_Palette lct, gct; + void (*plain_text)( + struct _gd_GIF * gif, uint16_t tx, uint16_t ty, + uint16_t tw, uint16_t th, uint8_t cw, uint8_t ch, + uint8_t fg, uint8_t bg + ); + void (*comment)(struct _gd_GIF * gif); + void (*application)(struct _gd_GIF * gif, char id[8], char auth[3]); + uint16_t fx, fy, fw, fh; + uint8_t bgindex; + uint8_t * canvas, * frame; + #if LV_GIF_CACHE_DECODE_DATA + uint8_t *lzw_cache; + #endif +} gd_GIF; + +gd_GIF * gd_open_gif_file(const char * fname); + +gd_GIF * gd_open_gif_data(const void * data); + +void gd_render_frame(gd_GIF * gif, uint8_t * buffer); + +int gd_get_frame(gd_GIF * gif); +void gd_rewind(gd_GIF * gif); +void gd_close_gif(gd_GIF * gif); + +#endif /*LV_USE_GIF*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* GIFDEC_H */ diff --git a/include/liblvgl/libs/gif/gifdec_mve.h b/include/liblvgl/libs/gif/gifdec_mve.h new file mode 100644 index 00000000..6d833934 --- /dev/null +++ b/include/liblvgl/libs/gif/gifdec_mve.h @@ -0,0 +1,140 @@ +/** + * @file gifdec_mve.h + * + */ + +#ifndef GIFDEC_MVE_H +#define GIFDEC_MVE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include +#include "../../misc/lv_color.h" + +/********************* + * DEFINES + *********************/ + +#define GIFDEC_FILL_BG(dst, w, h, stride, color, opa) \ + _gifdec_fill_bg_mve(dst, w, h, stride, color, opa) + +#define GIFDEC_RENDER_FRAME(dst, w, h, stride, frame, pattern, tindex) \ + _gifdec_render_frame_mve(dst, w, h, stride, frame, pattern, tindex) + +/********************** + * MACROS + **********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +static inline void _gifdec_fill_bg_mve(uint8_t * dst, uint16_t w, uint16_t h, uint16_t stride, uint8_t * color, + uint8_t opa) +{ + lv_color32_t c = lv_color32_make(*(color + 0), *(color + 1), *(color + 2), opa); + uint32_t color_32 = *(uint32_t *)&c; + + __asm volatile( + ".p2align 2 \n" + "vdup.32 q0, %[src] \n" + "3: \n" + "mov r0, %[dst] \n" + + "wlstp.32 lr, %[w], 1f \n" + "2: \n" + + "vstrw.32 q0, [r0], #16 \n" + "letp lr, 2b \n" + "1: \n" + "add %[dst], %[iTargetStride] \n" + "subs %[h], #1 \n" + "bne 3b \n" + : [dst] "+r"(dst), + [h] "+r"(h) + : [src] "r"(color_32), + [w] "r"(w), + [iTargetStride] "r"(stride * sizeof(uint32_t)) + : "r0", "q0", "memory", "r14", "cc"); +} + +static inline void _gifdec_render_frame_mve(uint8_t * dst, uint16_t w, uint16_t h, uint16_t stride, uint8_t * frame, + uint8_t * pattern, uint16_t tindex) +{ + if(w == 0 || h == 0) { + return; + } + + __asm volatile( + "vmov.u16 q3, #255 \n" + "vshl.u16 q3, q3, #8 \n" /* left shift 8 for a*/ + + "mov r0, #2 \n" + "vidup.u16 q6, r0, #4 \n" /* [2, 6, 10, 14, 18, 22, 26, 30] */ + "mov r0, #0 \n" + "vidup.u16 q7, r0, #4 \n" /* [0, 4, 8, 12, 16, 20, 24, 28] */ + + "3: \n" + "mov r1, %[dst] \n" + "mov r2, %[frame] \n" + + "wlstp.16 lr, %[w], 1f \n" + "2: \n" + + "mov r0, #3 \n" + "vldrb.u16 q4, [r2], #8 \n" + "vmul.u16 q5, q4, r0 \n" + + "mov r0, #1 \n" + "vldrb.u16 q2, [%[pattern], q5] \n" /* load 8 pixel r*/ + + "vadd.u16 q5, q5, r0 \n" + "vldrb.u16 q1, [%[pattern], q5] \n" /* load 8 pixel g*/ + + "vadd.u16 q5, q5, r0 \n" + "vldrb.u16 q0, [%[pattern], q5] \n" /* load 8 pixel b*/ + + "vshl.u16 q1, q1, #8 \n" /* left shift 8 for g*/ + + "vorr.u16 q0, q0, q1 \n" /* make 8 pixel gb*/ + "vorr.u16 q1, q2, q3 \n" /* make 8 pixel ar*/ + + "vcmp.i16 ne, q4, %[tindex] \n" + "vpstt \n" + "vstrht.16 q0, [r1, q7] \n" + "vstrht.16 q1, [r1, q6] \n" + "add r1, r1, #32 \n" + + "letp lr, 2b \n" + + "1: \n" + "mov r0, %[stride], LSL #2 \n" + "add %[dst], r0 \n" + "add %[frame], %[stride] \n" + "subs %[h], #1 \n" + "bne 3b \n" + + : [dst] "+r"(dst), + [frame] "+r"(frame), + [h] "+r"(h) + : [pattern] "r"(pattern), + [w] "r"(w), + [stride] "r"(stride), + [tindex] "r"(tindex) + : "r0", "r1", "r2", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "memory", "r14", "cc"); +} + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*GIFDEC_MVE_H*/ diff --git a/include/liblvgl/libs/gif/lv_gif.h b/include/liblvgl/libs/gif/lv_gif.h new file mode 100644 index 00000000..cf1bee47 --- /dev/null +++ b/include/liblvgl/libs/gif/lv_gif.h @@ -0,0 +1,104 @@ +/** + * @file lv_gif.h + * + */ + +#ifndef LV_GIF_H +#define LV_GIF_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../lv_conf_internal.h" +#include "../../misc/lv_types.h" +#include "../../draw/lv_draw_buf.h" +#include "../../widgets/image/lv_image.h" +#include "../../core/lv_obj_class.h" +#include LV_STDBOOL_INCLUDE +#include LV_STDINT_INCLUDE +#if LV_USE_GIF + +#include "gifdec.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_gif_class; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create a gif object + * @param parent pointer to an object, it will be the parent of the new gif. + * @return pointer to the gif obj + */ +lv_obj_t * lv_gif_create(lv_obj_t * parent); + +/** + * Set the gif data to display on the object + * @param obj pointer to a gif object + * @param src 1) pointer to an ::lv_image_dsc_t descriptor (which contains gif raw data) or + * 2) path to a gif file (e.g. "S:/dir/anim.gif") + */ +void lv_gif_set_src(lv_obj_t * obj, const void * src); + +/** + * Restart a gif animation. + * @param obj pointer to a gif obj + */ +void lv_gif_restart(lv_obj_t * obj); + +/** + * Pause a gif animation. + * @param obj pointer to a gif obj + */ +void lv_gif_pause(lv_obj_t * obj); + +/** + * Resume a gif animation. + * @param obj pointer to a gif obj + */ +void lv_gif_resume(lv_obj_t * obj); + +/** + * Checks if the GIF was loaded correctly. + * @param obj pointer to a gif obj + */ +bool lv_gif_is_loaded(lv_obj_t * obj); + +/** + * Get the loop count for the GIF. + * @param obj pointer to a gif obj + */ +int32_t lv_gif_get_loop_count(lv_obj_t * obj); + +/** + * Set the loop count for the GIF. + * @param obj pointer to a gif obj + * @param count the loop count to set + */ +void lv_gif_set_loop_count(lv_obj_t * obj, int32_t count); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_GIF*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /*LV_GIF_H*/ diff --git a/include/liblvgl/extra/libs/gif/lv_gif.h b/include/liblvgl/libs/gif/lv_gif_private.h similarity index 54% rename from include/liblvgl/extra/libs/gif/lv_gif.h rename to include/liblvgl/libs/gif/lv_gif_private.h index b40b690a..c88507f1 100644 --- a/include/liblvgl/extra/libs/gif/lv_gif.h +++ b/include/liblvgl/libs/gif/lv_gif_private.h @@ -1,10 +1,10 @@ /** - * @file lv_gif.h + * @file lv_gif_private.h * */ -#ifndef LV_GIF_H -#define LV_GIF_H +#ifndef LV_GIF_PRIVATE_H +#define LV_GIF_PRIVATE_H #ifdef __cplusplus extern "C" { @@ -14,10 +14,10 @@ extern "C" { * INCLUDES *********************/ -#include "liblvgl/lvgl.h" -#if LV_USE_GIF +#include "../../widgets/image/lv_image_private.h" +#include "lv_gif.h" -#include "gifdec.h" +#if LV_USE_GIF /********************* * DEFINES @@ -27,32 +27,31 @@ extern "C" { * TYPEDEFS **********************/ -typedef struct { - lv_img_t img; +/********************** + * TYPEDEFS + **********************/ + +struct _lv_gif_t { + lv_image_t img; gd_GIF * gif; lv_timer_t * timer; - lv_img_dsc_t imgdsc; + lv_image_dsc_t imgdsc; uint32_t last_call; -} lv_gif_t; +}; -extern const lv_obj_class_t lv_gif_class; /********************** * GLOBAL PROTOTYPES **********************/ -lv_obj_t * lv_gif_create(lv_obj_t * parent); -void lv_gif_set_src(lv_obj_t * obj, const void * src); -void lv_gif_restart(lv_obj_t * gif); - /********************** * MACROS **********************/ -#endif /*LV_USE_GIF*/ +#endif /* LV_USE_GIF */ #ifdef __cplusplus -} /* extern "C" */ +} /*extern "C"*/ #endif -#endif /*LV_GIF_H*/ +#endif /*LV_GIF_PRIVATE_H*/ diff --git a/include/liblvgl/libs/libjpeg_turbo/lv_libjpeg_turbo.h b/include/liblvgl/libs/libjpeg_turbo/lv_libjpeg_turbo.h new file mode 100644 index 00000000..177c83a6 --- /dev/null +++ b/include/liblvgl/libs/libjpeg_turbo/lv_libjpeg_turbo.h @@ -0,0 +1,48 @@ +/** + * @file lv_libjpeg_turbo.h + * + */ + +#ifndef LV_LIBJPEG_TURBO_H +#define LV_LIBJPEG_TURBO_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../lv_conf_internal.h" +#if LV_USE_LIBJPEG_TURBO + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Register the JPEG-Turbo decoder functions in LVGL + */ +void lv_libjpeg_turbo_init(void); + +void lv_libjpeg_turbo_deinit(void); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_LIBJPEG_TURBO*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /*LV_LIBJPEG_TURBO_H*/ diff --git a/include/liblvgl/extra/libs/png/lv_png.h b/include/liblvgl/libs/libpng/lv_libpng.h similarity index 68% rename from include/liblvgl/extra/libs/png/lv_png.h rename to include/liblvgl/libs/libpng/lv_libpng.h index ef634540..12794585 100644 --- a/include/liblvgl/extra/libs/png/lv_png.h +++ b/include/liblvgl/libs/libpng/lv_libpng.h @@ -1,10 +1,10 @@ /** - * @file lv_png.h + * @file lv_libpng.h * */ -#ifndef LV_PNG_H -#define LV_PNG_H +#ifndef LV_LIBPNG_H +#define LV_LIBPNG_H #ifdef __cplusplus extern "C" { @@ -13,8 +13,8 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" -#if LV_USE_PNG +#include "../../lv_conf_internal.h" +#if LV_USE_LIBPNG /********************* * DEFINES @@ -31,16 +31,18 @@ extern "C" { /** * Register the PNG decoder functions in LVGL */ -void lv_png_init(void); +void lv_libpng_init(void); + +void lv_libpng_deinit(void); /********************** * MACROS **********************/ -#endif /*LV_USE_PNG*/ +#endif /*LV_USE_LIBPNG*/ #ifdef __cplusplus } /* extern "C" */ #endif -#endif /*LV_PNG_H*/ +#endif /*LV_LIBPNG_H*/ diff --git a/include/liblvgl/extra/libs/png/lodepng.h b/include/liblvgl/libs/lodepng/lodepng.h similarity index 61% rename from include/liblvgl/extra/libs/png/lodepng.h rename to include/liblvgl/libs/lodepng/lodepng.h index 4068d4b1..624eb269 100644 --- a/include/liblvgl/extra/libs/png/lodepng.h +++ b/include/liblvgl/libs/lodepng/lodepng.h @@ -1,7 +1,7 @@ /* -LodePNG version 20201017 +LodePNG version 20230410 -Copyright (c) 2005-2020 Lode Vandevenne +Copyright (c) 2005-2023 Lode Vandevenne This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages @@ -26,90 +26,115 @@ freely, subject to the following restrictions: #ifndef LODEPNG_H #define LODEPNG_H -#include /*for size_t*/ +#ifdef __cplusplus +extern "C" { +#endif #include "liblvgl/lvgl.h" -#if LV_USE_PNG -extern const char* LODEPNG_VERSION_STRING; +#if LV_USE_LODEPNG +#include LV_STDDEF_INCLUDE /*for size_t*/ +LV_ATTRIBUTE_EXTERN_DATA extern const char * LODEPNG_VERSION_STRING; /* The following #defines are used to create code sections. They can be disabled to disable code sections, which can give faster compile time and smaller binary. The "NO_COMPILE" defines are designed to be used to pass as defines to the compiler command to disable them without modifying this header, e.g. --DLODEPNG_NO_COMPILE_ZLIB for gcc. -In addition to those below, you can also define LODEPNG_NO_COMPILE_CRC to -allow implementing a custom lodepng_crc32. +-DLODEPNG_NO_COMPILE_ZLIB for gcc or clang. */ /*deflate & zlib. If disabled, you must specify alternative zlib functions in the custom_zlib field of the compress and decompress settings*/ #ifndef LODEPNG_NO_COMPILE_ZLIB -#define LODEPNG_COMPILE_ZLIB + /*pass -DLODEPNG_NO_COMPILE_ZLIB to the compiler to disable this, or comment out LODEPNG_COMPILE_ZLIB below*/ + #define LODEPNG_COMPILE_ZLIB #endif /*png encoder and png decoder*/ #ifndef LODEPNG_NO_COMPILE_PNG -#define LODEPNG_COMPILE_PNG + /*pass -DLODEPNG_NO_COMPILE_PNG to the compiler to disable this, or comment out LODEPNG_COMPILE_PNG below*/ + #define LODEPNG_COMPILE_PNG #endif /*deflate&zlib decoder and png decoder*/ #ifndef LODEPNG_NO_COMPILE_DECODER -#define LODEPNG_COMPILE_DECODER + /*pass -DLODEPNG_NO_COMPILE_DECODER to the compiler to disable this, or comment out LODEPNG_COMPILE_DECODER below*/ + #define LODEPNG_COMPILE_DECODER #endif /*deflate&zlib encoder and png encoder*/ #ifndef LODEPNG_NO_COMPILE_ENCODER -#define LODEPNG_COMPILE_ENCODER + /*pass -DLODEPNG_NO_COMPILE_ENCODER to the compiler to disable this, or comment out LODEPNG_COMPILE_ENCODER below*/ + #define LODEPNG_COMPILE_ENCODER #endif /*the optional built in harddisk file loading and saving functions*/ #ifndef LODEPNG_NO_COMPILE_DISK -#define LODEPNG_COMPILE_DISK + /*pass -DLODEPNG_NO_COMPILE_DISK to the compiler to disable this, or comment out LODEPNG_COMPILE_DISK below*/ + #define LODEPNG_COMPILE_DISK #endif /*support for chunks other than IHDR, IDAT, PLTE, tRNS, IEND: ancillary and unknown chunks*/ #ifndef LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS -#define LODEPNG_COMPILE_ANCILLARY_CHUNKS + /*pass -DLODEPNG_NO_COMPILE_ANCILLARY_CHUNKS to the compiler to disable this, + or comment out LODEPNG_COMPILE_ANCILLARY_CHUNKS below*/ + #define LODEPNG_COMPILE_ANCILLARY_CHUNKS #endif /*ability to convert error numerical codes to English text string*/ #ifndef LODEPNG_NO_COMPILE_ERROR_TEXT -#define LODEPNG_COMPILE_ERROR_TEXT + /*pass -DLODEPNG_NO_COMPILE_ERROR_TEXT to the compiler to disable this, + or comment out LODEPNG_COMPILE_ERROR_TEXT below*/ + #define LODEPNG_COMPILE_ERROR_TEXT #endif /*Compile the default allocators (C's free, malloc and realloc). If you disable this, you can define the functions lodepng_free, lodepng_malloc and lodepng_realloc in your source files with custom allocators.*/ #ifndef LODEPNG_NO_COMPILE_ALLOCATORS -#define LODEPNG_COMPILE_ALLOCATORS + /*pass -DLODEPNG_NO_COMPILE_ALLOCATORS to the compiler to disable the built-in ones, + or comment out LODEPNG_COMPILE_ALLOCATORS below*/ + #define LODEPNG_COMPILE_ALLOCATORS +#endif + +/*Disable built-in CRC function, in that case a custom implementation of +lodepng_crc32 must be defined externally so that it can be linked in. +The default built-in CRC code comes with 8KB of lookup tables, so for memory constrained environment you may want it +disabled and provide a much smaller implementation externally as said above. You can find such an example implementation +in a comment in the lodepng.c(pp) file in the 'else' case of the searchable LODEPNG_COMPILE_CRC section.*/ +#ifndef LODEPNG_NO_COMPILE_CRC + /*pass -DLODEPNG_NO_COMPILE_CRC to the compiler to disable the built-in one, + or comment out LODEPNG_COMPILE_CRC below*/ + #define LODEPNG_COMPILE_CRC #endif /*compile the C++ version (you can disable the C++ wrapper here even when compiling for C++)*/ #ifdef __cplusplus -#ifndef LODEPNG_NO_COMPILE_CPP -#define LODEPNG_COMPILE_CPP -#endif + #ifndef LODEPNG_NO_COMPILE_CPP + /*pass -DLODEPNG_NO_COMPILE_CPP to the compiler to disable C++ (not needed if a C-only compiler), + or comment out LODEPNG_COMPILE_CPP below*/ + #define LODEPNG_COMPILE_CPP + #endif #endif #ifdef LODEPNG_COMPILE_CPP -#include -#include + #include + #include #endif /*LODEPNG_COMPILE_CPP*/ #ifdef LODEPNG_COMPILE_PNG /*The PNG color types (also used for raw image).*/ typedef enum LodePNGColorType { - LCT_GREY = 0, /*grayscale: 1,2,4,8,16 bit*/ - LCT_RGB = 2, /*RGB: 8,16 bit*/ - LCT_PALETTE = 3, /*palette: 1,2,4,8 bit*/ - LCT_GREY_ALPHA = 4, /*grayscale with alpha: 8,16 bit*/ - LCT_RGBA = 6, /*RGB with alpha: 8,16 bit*/ - /*LCT_MAX_OCTET_VALUE lets the compiler allow this enum to represent any invalid - byte value from 0 to 255 that could be present in an invalid PNG file header. Do - not use, compare with or set the name LCT_MAX_OCTET_VALUE, instead either use - the valid color type names above, or numeric values like 1 or 7 when checking for - particular disallowed color type byte values, or cast to integer to print it.*/ - LCT_MAX_OCTET_VALUE = 255 + LCT_GREY = 0, /*grayscale: 1,2,4,8,16 bit*/ + LCT_RGB = 2, /*RGB: 8,16 bit*/ + LCT_PALETTE = 3, /*palette: 1,2,4,8 bit*/ + LCT_GREY_ALPHA = 4, /*grayscale with alpha: 8,16 bit*/ + LCT_RGBA = 6, /*RGB with alpha: 8,16 bit*/ + /*LCT_MAX_OCTET_VALUE lets the compiler allow this enum to represent any invalid + byte value from 0 to 255 that could be present in an invalid PNG file header. Do + not use, compare with or set the name LCT_MAX_OCTET_VALUE, instead either use + the valid color type names above, or numeric values like 1 or 7 when checking for + particular disallowed color type byte values, or cast to integer to print it.*/ + LCT_MAX_OCTET_VALUE = 255 } LodePNGColorType; #ifdef LODEPNG_COMPILE_DECODER @@ -128,34 +153,42 @@ colortype: the desired color type for the raw output image. See explanation on P bitdepth: the desired bit depth for the raw output image. See explanation on PNG color types. Return value: LodePNG error code (0 means no error). */ -unsigned lodepng_decode_memory(unsigned char** out, unsigned* w, unsigned* h, - const unsigned char* in, size_t insize, +unsigned lodepng_decode_memory(unsigned char ** out, unsigned * w, unsigned * h, + const unsigned char * in, size_t insize, LodePNGColorType colortype, unsigned bitdepth); /*Same as lodepng_decode_memory, but always decodes to 32-bit RGBA raw image*/ -unsigned lodepng_decode32(unsigned char** out, unsigned* w, unsigned* h, - const unsigned char* in, size_t insize); +unsigned lodepng_decode32(unsigned char ** out, unsigned * w, unsigned * h, + const unsigned char * in, size_t insize); /*Same as lodepng_decode_memory, but always decodes to 24-bit RGB raw image*/ -unsigned lodepng_decode24(unsigned char** out, unsigned* w, unsigned* h, - const unsigned char* in, size_t insize); +unsigned lodepng_decode24(unsigned char ** out, unsigned * w, unsigned * h, + const unsigned char * in, size_t insize); #ifdef LODEPNG_COMPILE_DISK /* Load PNG from disk, from file with given name. Same as the other decode functions, but instead takes a filename as input. -*/ -unsigned lodepng_decode_file(unsigned char** out, unsigned* w, unsigned* h, - const char* filename, + +NOTE: Wide-character filenames are not supported, you can use an external method +to handle such files and decode in-memory.*/ +unsigned lodepng_decode_file(unsigned char ** out, unsigned * w, unsigned * h, + const char * filename, LodePNGColorType colortype, unsigned bitdepth); -/*Same as lodepng_decode_file, but always decodes to 32-bit RGBA raw image.*/ -unsigned lodepng_decode32_file(unsigned char** out, unsigned* w, unsigned* h, - const char* filename); +/*Same as lodepng_decode_file, but always decodes to 32-bit RGBA raw image. + +NOTE: Wide-character filenames are not supported, you can use an external method +to handle such files and decode in-memory.*/ +unsigned lodepng_decode32_file(unsigned char ** out, unsigned * w, unsigned * h, + const char * filename); + +/*Same as lodepng_decode_file, but always decodes to 24-bit RGB raw image. -/*Same as lodepng_decode_file, but always decodes to 24-bit RGB raw image.*/ -unsigned lodepng_decode24_file(unsigned char** out, unsigned* w, unsigned* h, - const char* filename); +NOTE: Wide-character filenames are not supported, you can use an external method +to handle such files and decode in-memory.*/ +unsigned lodepng_decode24_file(unsigned char ** out, unsigned * w, unsigned * h, + const char * filename); #endif /*LODEPNG_COMPILE_DISK*/ #endif /*LODEPNG_COMPILE_DECODER*/ @@ -177,57 +210,70 @@ colortype: the color type of the raw input image. See explanation on PNG color t bitdepth: the bit depth of the raw input image. See explanation on PNG color types. Return value: LodePNG error code (0 means no error). */ -unsigned lodepng_encode_memory(unsigned char** out, size_t* outsize, - const unsigned char* image, unsigned w, unsigned h, +unsigned lodepng_encode_memory(unsigned char ** out, size_t * outsize, + const unsigned char * image, unsigned w, unsigned h, LodePNGColorType colortype, unsigned bitdepth); /*Same as lodepng_encode_memory, but always encodes from 32-bit RGBA raw image.*/ -unsigned lodepng_encode32(unsigned char** out, size_t* outsize, - const unsigned char* image, unsigned w, unsigned h); +unsigned lodepng_encode32(unsigned char ** out, size_t * outsize, + const unsigned char * image, unsigned w, unsigned h); /*Same as lodepng_encode_memory, but always encodes from 24-bit RGB raw image.*/ -unsigned lodepng_encode24(unsigned char** out, size_t* outsize, - const unsigned char* image, unsigned w, unsigned h); +unsigned lodepng_encode24(unsigned char ** out, size_t * outsize, + const unsigned char * image, unsigned w, unsigned h); #ifdef LODEPNG_COMPILE_DISK /* Converts raw pixel data into a PNG file on disk. Same as the other encode functions, but instead takes a filename as output. + NOTE: This overwrites existing files without warning! -*/ -unsigned lodepng_encode_file(const char* filename, - const unsigned char* image, unsigned w, unsigned h, + +NOTE: Wide-character filenames are not supported, you can use an external method +to handle such files and encode in-memory.*/ +unsigned lodepng_encode_file(const char * filename, + const unsigned char * image, unsigned w, unsigned h, LodePNGColorType colortype, unsigned bitdepth); -/*Same as lodepng_encode_file, but always encodes from 32-bit RGBA raw image.*/ -unsigned lodepng_encode32_file(const char* filename, - const unsigned char* image, unsigned w, unsigned h); +/*Same as lodepng_encode_file, but always encodes from 32-bit RGBA raw image. + +NOTE: Wide-character filenames are not supported, you can use an external method +to handle such files and encode in-memory.*/ +unsigned lodepng_encode32_file(const char * filename, + const unsigned char * image, unsigned w, unsigned h); -/*Same as lodepng_encode_file, but always encodes from 24-bit RGB raw image.*/ -unsigned lodepng_encode24_file(const char* filename, - const unsigned char* image, unsigned w, unsigned h); +/*Same as lodepng_encode_file, but always encodes from 24-bit RGB raw image. + +NOTE: Wide-character filenames are not supported, you can use an external method +to handle such files and encode in-memory.*/ +unsigned lodepng_encode24_file(const char * filename, + const unsigned char * image, unsigned w, unsigned h); #endif /*LODEPNG_COMPILE_DISK*/ #endif /*LODEPNG_COMPILE_ENCODER*/ #ifdef LODEPNG_COMPILE_CPP -namespace lodepng { +namespace lodepng +{ #ifdef LODEPNG_COMPILE_DECODER /*Same as lodepng_decode_memory, but decodes to an std::vector. The colortype is the format to output the pixels to. Default is RGBA 8-bit per channel.*/ -unsigned decode(std::vector& out, unsigned& w, unsigned& h, - const unsigned char* in, size_t insize, +unsigned decode(std::vector & out, unsigned & w, unsigned & h, + const unsigned char * in, size_t insize, LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8); -unsigned decode(std::vector& out, unsigned& w, unsigned& h, - const std::vector& in, +unsigned decode(std::vector & out, unsigned & w, unsigned & h, + const std::vector & in, LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8); #ifdef LODEPNG_COMPILE_DISK /* Converts PNG file from disk to raw pixel data in memory. Same as the other decode functions, but instead takes a filename as input. + +NOTE: Wide-character filenames are not supported, you can use an external method +to handle such files and decode in-memory. */ -unsigned decode(std::vector& out, unsigned& w, unsigned& h, - const std::string& filename, +unsigned decode(std::vector & out, unsigned & w, unsigned & h, + const std::string & filename, LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8); #endif /* LODEPNG_COMPILE_DISK */ #endif /* LODEPNG_COMPILE_DECODER */ @@ -235,23 +281,27 @@ unsigned decode(std::vector& out, unsigned& w, unsigned& h, #ifdef LODEPNG_COMPILE_ENCODER /*Same as lodepng_encode_memory, but encodes to an std::vector. colortype is that of the raw input data. The output PNG color type will be auto chosen.*/ -unsigned encode(std::vector& out, - const unsigned char* in, unsigned w, unsigned h, +unsigned encode(std::vector & out, + const unsigned char * in, unsigned w, unsigned h, LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8); -unsigned encode(std::vector& out, - const std::vector& in, unsigned w, unsigned h, +unsigned encode(std::vector & out, + const std::vector & in, unsigned w, unsigned h, LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8); #ifdef LODEPNG_COMPILE_DISK /* Converts 32-bit RGBA raw pixel data into a PNG file on disk. Same as the other encode functions, but instead takes a filename as output. + NOTE: This overwrites existing files without warning! + +NOTE: Wide-character filenames are not supported, you can use an external method +to handle such files and decode in-memory. */ -unsigned encode(const std::string& filename, - const unsigned char* in, unsigned w, unsigned h, +unsigned encode(const std::string & filename, + const unsigned char * in, unsigned w, unsigned h, LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8); -unsigned encode(const std::string& filename, - const std::vector& in, unsigned w, unsigned h, +unsigned encode(const std::string & filename, + const std::vector & in, unsigned w, unsigned h, LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8); #endif /* LODEPNG_COMPILE_DISK */ #endif /* LODEPNG_COMPILE_ENCODER */ @@ -260,42 +310,42 @@ unsigned encode(const std::string& filename, #endif /*LODEPNG_COMPILE_PNG*/ #ifdef LODEPNG_COMPILE_ERROR_TEXT -/*Returns an English description of the numerical error code.*/ -const char* lodepng_error_text(unsigned code); + /*Returns an English description of the numerical error code.*/ + const char * lodepng_error_text(unsigned code); #endif /*LODEPNG_COMPILE_ERROR_TEXT*/ #ifdef LODEPNG_COMPILE_DECODER /*Settings for zlib decompression*/ typedef struct LodePNGDecompressSettings LodePNGDecompressSettings; struct LodePNGDecompressSettings { - /* Check LodePNGDecoderSettings for more ignorable errors such as ignore_crc */ - unsigned ignore_adler32; /*if 1, continue and don't give an error message if the Adler32 checksum is corrupted*/ - unsigned ignore_nlen; /*ignore complement of len checksum in uncompressed blocks*/ - - /*Maximum decompressed size, beyond this the decoder may (and is encouraged to) stop decoding, - return an error, output a data size > max_output_size and all the data up to that point. This is - not hard limit nor a guarantee, but can prevent excessive memory usage. This setting is - ignored by the PNG decoder, but is used by the deflate/zlib decoder and can be used by custom ones. - Set to 0 to impose no limit (the default).*/ - size_t max_output_size; - - /*use custom zlib decoder instead of built in one (default: null). - Should return 0 if success, any non-0 if error (numeric value not exposed).*/ - unsigned (*custom_zlib)(unsigned char**, size_t*, - const unsigned char*, size_t, - const LodePNGDecompressSettings*); - /*use custom deflate decoder instead of built in one (default: null) - if custom_zlib is not null, custom_inflate is ignored (the zlib format uses deflate). - Should return 0 if success, any non-0 if error (numeric value not exposed).*/ - unsigned (*custom_inflate)(unsigned char**, size_t*, - const unsigned char*, size_t, - const LodePNGDecompressSettings*); - - const void* custom_context; /*optional custom settings for custom functions*/ + /* Check LodePNGDecoderSettings for more ignorable errors such as ignore_crc */ + unsigned ignore_adler32; /*if 1, continue and don't give an error message if the Adler32 checksum is corrupted*/ + unsigned ignore_nlen; /*ignore complement of len checksum in uncompressed blocks*/ + + /*Maximum decompressed size, beyond this the decoder may (and is encouraged to) stop decoding, + return an error, output a data size > max_output_size and all the data up to that point. This is + neither a hard limit nor a guarantee, but can prevent excessive memory usage. This setting is + ignored by the PNG decoder, but is used by the deflate/zlib decoder and can be used by custom ones. + Set to 0 to impose no limit (the default).*/ + size_t max_output_size; + + /*use custom zlib decoder instead of built in one (default: null). + Should return 0 if success, any non-0 if error (numeric value not exposed).*/ + unsigned(*custom_zlib)(unsigned char **, size_t *, + const unsigned char *, size_t, + const LodePNGDecompressSettings *); + /*use custom deflate decoder instead of built in one (default: null) + if custom_zlib is not null, custom_inflate is ignored (the zlib format uses deflate). + Should return 0 if success, any non-0 if error (numeric value not exposed).*/ + unsigned(*custom_inflate)(unsigned char **, size_t *, + const unsigned char *, size_t, + const LodePNGDecompressSettings *); + + const void * custom_context; /*optional custom settings for custom functions*/ }; -extern const LodePNGDecompressSettings lodepng_default_decompress_settings; -void lodepng_decompress_settings_init(LodePNGDecompressSettings* settings); +LV_ATTRIBUTE_EXTERN_DATA extern const LodePNGDecompressSettings lodepng_default_decompress_settings; +void lodepng_decompress_settings_init(LodePNGDecompressSettings * settings); #endif /*LODEPNG_COMPILE_DECODER*/ #ifdef LODEPNG_COMPILE_ENCODER @@ -304,31 +354,31 @@ Settings for zlib compression. Tweaking these settings tweaks the balance between speed and compression ratio. */ typedef struct LodePNGCompressSettings LodePNGCompressSettings; -struct LodePNGCompressSettings /*deflate = compress*/ { - /*LZ77 related settings*/ - unsigned btype; /*the block type for LZ (0, 1, 2 or 3, see zlib standard). Should be 2 for proper compression.*/ - unsigned use_lz77; /*whether or not to use LZ77. Should be 1 for proper compression.*/ - unsigned windowsize; /*must be a power of two <= 32768. higher compresses more but is slower. Default value: 2048.*/ - unsigned minmatch; /*minimum lz77 length. 3 is normally best, 6 can be better for some PNGs. Default: 0*/ - unsigned nicematch; /*stop searching if >= this length found. Set to 258 for best compression. Default: 128*/ - unsigned lazymatching; /*use lazy matching: better compression but a bit slower. Default: true*/ - - /*use custom zlib encoder instead of built in one (default: null)*/ - unsigned (*custom_zlib)(unsigned char**, size_t*, - const unsigned char*, size_t, - const LodePNGCompressSettings*); - /*use custom deflate encoder instead of built in one (default: null) - if custom_zlib is used, custom_deflate is ignored since only the built in - zlib function will call custom_deflate*/ - unsigned (*custom_deflate)(unsigned char**, size_t*, - const unsigned char*, size_t, - const LodePNGCompressSettings*); - - const void* custom_context; /*optional custom settings for custom functions*/ +struct LodePNGCompressSettings { /*deflate = compress*/ + /*LZ77 related settings*/ + unsigned btype; /*the block type for LZ (0, 1, 2 or 3, see zlib standard). Should be 2 for proper compression.*/ + unsigned use_lz77; /*whether or not to use LZ77. Should be 1 for proper compression.*/ + unsigned windowsize; /*must be a power of two <= 32768. higher compresses more but is slower. Default value: 2048.*/ + unsigned minmatch; /*minimum lz77 length. 3 is normally best, 6 can be better for some PNGs. Default: 0*/ + unsigned nicematch; /*stop searching if >= this length found. Set to 258 for best compression. Default: 128*/ + unsigned lazymatching; /*use lazy matching: better compression but a bit slower. Default: true*/ + + /*use custom zlib encoder instead of built in one (default: null)*/ + unsigned(*custom_zlib)(unsigned char **, size_t *, + const unsigned char *, size_t, + const LodePNGCompressSettings *); + /*use custom deflate encoder instead of built in one (default: null) + if custom_zlib is used, custom_deflate is ignored since only the built in + zlib function will call custom_deflate*/ + unsigned(*custom_deflate)(unsigned char **, size_t *, + const unsigned char *, size_t, + const LodePNGCompressSettings *); + + const void * custom_context; /*optional custom settings for custom functions*/ }; -extern const LodePNGCompressSettings lodepng_default_compress_settings; -void lodepng_compress_settings_init(LodePNGCompressSettings* settings); +LV_ATTRIBUTE_EXTERN_DATA extern const LodePNGCompressSettings lodepng_default_compress_settings; +void lodepng_compress_settings_init(LodePNGCompressSettings * settings); #endif /*LODEPNG_COMPILE_ENCODER*/ #ifdef LODEPNG_COMPILE_PNG @@ -338,72 +388,74 @@ bits to RGBA colors. This information is the same as used in the PNG file format, and is used both for PNG and raw image data in LodePNG. */ typedef struct LodePNGColorMode { - /*header (IHDR)*/ - LodePNGColorType colortype; /*color type, see PNG standard or documentation further in this header file*/ - unsigned bitdepth; /*bits per sample, see PNG standard or documentation further in this header file*/ + /*header (IHDR)*/ + LodePNGColorType colortype; /*color type, see PNG standard or documentation further in this header file*/ + unsigned bitdepth; /*bits per sample, see PNG standard or documentation further in this header file*/ - /* - palette (PLTE and tRNS) + /* + palette (PLTE and tRNS) - Dynamically allocated with the colors of the palette, including alpha. - This field may not be allocated directly, use lodepng_color_mode_init first, - then lodepng_palette_add per color to correctly initialize it (to ensure size - of exactly 1024 bytes). + Dynamically allocated with the colors of the palette, including alpha. + This field may not be allocated directly, use lodepng_color_mode_init first, + then lodepng_palette_add per color to correctly initialize it (to ensure size + of exactly 1024 bytes). - The alpha channels must be set as well, set them to 255 for opaque images. + The alpha channels must be set as well, set them to 255 for opaque images. - When decoding, by default you can ignore this palette, since LodePNG already - fills the palette colors in the pixels of the raw RGBA output. + When decoding, with the default settings you can ignore this palette, since + LodePNG already fills the palette colors in the pixels of the raw RGBA output, + but when decoding to the original PNG color mode it is needed to reconstruct + the colors. - The palette is only supported for color type 3. - */ - unsigned char* palette; /*palette in RGBARGBA... order. Must be either 0, or when allocated must have 1024 bytes*/ - size_t palettesize; /*palette size in number of colors (amount of used bytes is 4 * palettesize)*/ + The palette is only supported for color type 3. + */ + unsigned char * palette; /*palette in RGBARGBA... order. Must be either 0, or when allocated must have 1024 bytes*/ + size_t palettesize; /*palette size in number of colors (amount of used bytes is 4 * palettesize)*/ - /* - transparent color key (tRNS) + /* + transparent color key (tRNS) - This color uses the same bit depth as the bitdepth value in this struct, which can be 1-bit to 16-bit. - For grayscale PNGs, r, g and b will all 3 be set to the same. + This color uses the same bit depth as the bitdepth value in this struct, which can be 1-bit to 16-bit. + For grayscale PNGs, r, g and b will all 3 be set to the same. - When decoding, by default you can ignore this information, since LodePNG sets - pixels with this key to transparent already in the raw RGBA output. + When decoding, by default you can ignore this information, since LodePNG sets + pixels with this key to transparent already in the raw RGBA output. - The color key is only supported for color types 0 and 2. - */ - unsigned key_defined; /*is a transparent color key given? 0 = false, 1 = true*/ - unsigned key_r; /*red/grayscale component of color key*/ - unsigned key_g; /*green component of color key*/ - unsigned key_b; /*blue component of color key*/ + The color key is only supported for color types 0 and 2. + */ + unsigned key_defined; /*is a transparent color key given? 0 = false, 1 = true*/ + unsigned key_r; /*red/grayscale component of color key*/ + unsigned key_g; /*green component of color key*/ + unsigned key_b; /*blue component of color key*/ } LodePNGColorMode; /*init, cleanup and copy functions to use with this struct*/ -void lodepng_color_mode_init(LodePNGColorMode* info); -void lodepng_color_mode_cleanup(LodePNGColorMode* info); +void lodepng_color_mode_init(LodePNGColorMode * info); +void lodepng_color_mode_cleanup(LodePNGColorMode * info); /*return value is error code (0 means no error)*/ -unsigned lodepng_color_mode_copy(LodePNGColorMode* dest, const LodePNGColorMode* source); +unsigned lodepng_color_mode_copy(LodePNGColorMode * dest, const LodePNGColorMode * source); /* Makes a temporary LodePNGColorMode that does not need cleanup (no palette) */ LodePNGColorMode lodepng_color_mode_make(LodePNGColorType colortype, unsigned bitdepth); -void lodepng_palette_clear(LodePNGColorMode* info); +void lodepng_palette_clear(LodePNGColorMode * info); /*add 1 color to the palette*/ -unsigned lodepng_palette_add(LodePNGColorMode* info, +unsigned lodepng_palette_add(LodePNGColorMode * info, unsigned char r, unsigned char g, unsigned char b, unsigned char a); /*get the total amount of bits per pixel, based on colortype and bitdepth in the struct*/ -unsigned lodepng_get_bpp(const LodePNGColorMode* info); +unsigned lodepng_get_bpp(const LodePNGColorMode * info); /*get the amount of color channels used, based on colortype in the struct. If a palette is used, it counts as 1 channel.*/ -unsigned lodepng_get_channels(const LodePNGColorMode* info); +unsigned lodepng_get_channels(const LodePNGColorMode * info); /*is it a grayscale type? (only colortype 0 or 4)*/ -unsigned lodepng_is_greyscale_type(const LodePNGColorMode* info); +unsigned lodepng_is_greyscale_type(const LodePNGColorMode * info); /*has it got an alpha channel? (only colortype 2 or 6)*/ -unsigned lodepng_is_alpha_type(const LodePNGColorMode* info); +unsigned lodepng_is_alpha_type(const LodePNGColorMode * info); /*has it got a palette? (only colortype 3)*/ -unsigned lodepng_is_palette_type(const LodePNGColorMode* info); +unsigned lodepng_is_palette_type(const LodePNGColorMode * info); /*only returns true if there is a palette and there is a value in the palette with alpha < 255. Loops through the palette to check this.*/ -unsigned lodepng_has_palette_alpha(const LodePNGColorMode* info); +unsigned lodepng_has_palette_alpha(const LodePNGColorMode * info); /* Check if the given color info indicates the possibility of having non-opaque pixels in the PNG image. Returns true if the image can have translucent or invisible pixels (it still be opaque if it doesn't use such pixels). @@ -411,213 +463,254 @@ Returns false if the image can only have opaque pixels. In detail, it returns true only if it's a color type with alpha, or has a palette with non-opaque values, or if "key_defined" is true. */ -unsigned lodepng_can_have_alpha(const LodePNGColorMode* info); +unsigned lodepng_can_have_alpha(const LodePNGColorMode * info); /*Returns the byte size of a raw image buffer with given width, height and color mode*/ -size_t lodepng_get_raw_size(unsigned w, unsigned h, const LodePNGColorMode* color); +size_t lodepng_get_raw_size(unsigned w, unsigned h, const LodePNGColorMode * color); #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS /*The information of a Time chunk in PNG.*/ typedef struct LodePNGTime { - unsigned year; /*2 bytes used (0-65535)*/ - unsigned month; /*1-12*/ - unsigned day; /*1-31*/ - unsigned hour; /*0-23*/ - unsigned minute; /*0-59*/ - unsigned second; /*0-60 (to allow for leap seconds)*/ + unsigned year; /*2 bytes used (0-65535)*/ + unsigned month; /*1-12*/ + unsigned day; /*1-31*/ + unsigned hour; /*0-23*/ + unsigned minute; /*0-59*/ + unsigned second; /*0-60 (to allow for leap seconds)*/ } LodePNGTime; #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ /*Information about the PNG image, except pixels, width and height.*/ typedef struct LodePNGInfo { - /*header (IHDR), palette (PLTE) and transparency (tRNS) chunks*/ - unsigned compression_method;/*compression method of the original file. Always 0.*/ - unsigned filter_method; /*filter method of the original file*/ - unsigned interlace_method; /*interlace method of the original file: 0=none, 1=Adam7*/ - LodePNGColorMode color; /*color type and bits, palette and transparency of the PNG file*/ + /*header (IHDR), palette (PLTE) and transparency (tRNS) chunks*/ + unsigned compression_method;/*compression method of the original file. Always 0.*/ + unsigned filter_method; /*filter method of the original file*/ + unsigned interlace_method; /*interlace method of the original file: 0=none, 1=Adam7*/ + LodePNGColorMode color; /*color type and bits, palette and transparency of the PNG file*/ #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS - /* - Suggested background color chunk (bKGD) - - This uses the same color mode and bit depth as the PNG (except no alpha channel), - with values truncated to the bit depth in the unsigned integer. - - For grayscale and palette PNGs, the value is stored in background_r. The values - in background_g and background_b are then unused. - - So when decoding, you may get these in a different color mode than the one you requested - for the raw pixels. - - When encoding with auto_convert, you must use the color model defined in info_png.color for - these values. The encoder normally ignores info_png.color when auto_convert is on, but will - use it to interpret these values (and convert copies of them to its chosen color model). - - When encoding, avoid setting this to an expensive color, such as a non-gray value - when the image is gray, or the compression will be worse since it will be forced to - write the PNG with a more expensive color mode (when auto_convert is on). - - The decoder does not use this background color to edit the color of pixels. This is a - completely optional metadata feature. - */ - unsigned background_defined; /*is a suggested background color given?*/ - unsigned background_r; /*red/gray/palette component of suggested background color*/ - unsigned background_g; /*green component of suggested background color*/ - unsigned background_b; /*blue component of suggested background color*/ - - /* - Non-international text chunks (tEXt and zTXt) - - The char** arrays each contain num strings. The actual messages are in - text_strings, while text_keys are keywords that give a short description what - the actual text represents, e.g. Title, Author, Description, or anything else. - - All the string fields below including strings, keys, names and language tags are null terminated. - The PNG specification uses null characters for the keys, names and tags, and forbids null - characters to appear in the main text which is why we can use null termination everywhere here. - - A keyword is minimum 1 character and maximum 79 characters long (plus the - additional null terminator). It's discouraged to use a single line length - longer than 79 characters for texts. - - Don't allocate these text buffers yourself. Use the init/cleanup functions - correctly and use lodepng_add_text and lodepng_clear_text. - - Standard text chunk keywords and strings are encoded using Latin-1. - */ - size_t text_num; /*the amount of texts in these char** buffers (there may be more texts in itext)*/ - char** text_keys; /*the keyword of a text chunk (e.g. "Comment")*/ - char** text_strings; /*the actual text*/ - - /* - International text chunks (iTXt) - Similar to the non-international text chunks, but with additional strings - "langtags" and "transkeys", and the following text encodings are used: - keys: Latin-1, langtags: ASCII, transkeys and strings: UTF-8. - keys must be 1-79 characters (plus the additional null terminator), the other - strings are any length. - */ - size_t itext_num; /*the amount of international texts in this PNG*/ - char** itext_keys; /*the English keyword of the text chunk (e.g. "Comment")*/ - char** itext_langtags; /*language tag for this text's language, ISO/IEC 646 string, e.g. ISO 639 language tag*/ - char** itext_transkeys; /*keyword translated to the international language - UTF-8 string*/ - char** itext_strings; /*the actual international text - UTF-8 string*/ - - /*time chunk (tIME)*/ - unsigned time_defined; /*set to 1 to make the encoder generate a tIME chunk*/ - LodePNGTime time; - - /*phys chunk (pHYs)*/ - unsigned phys_defined; /*if 0, there is no pHYs chunk and the values below are undefined, if 1 else there is one*/ - unsigned phys_x; /*pixels per unit in x direction*/ - unsigned phys_y; /*pixels per unit in y direction*/ - unsigned phys_unit; /*may be 0 (unknown unit) or 1 (metre)*/ - - /* - Color profile related chunks: gAMA, cHRM, sRGB, iCPP - - LodePNG does not apply any color conversions on pixels in the encoder or decoder and does not interpret these color - profile values. It merely passes on the information. If you wish to use color profiles and convert colors, please - use these values with a color management library. - - See the PNG, ICC and sRGB specifications for more information about the meaning of these values. - */ - - /* gAMA chunk: optional, overridden by sRGB or iCCP if those are present. */ - unsigned gama_defined; /* Whether a gAMA chunk is present (0 = not present, 1 = present). */ - unsigned gama_gamma; /* Gamma exponent times 100000 */ - - /* cHRM chunk: optional, overridden by sRGB or iCCP if those are present. */ - unsigned chrm_defined; /* Whether a cHRM chunk is present (0 = not present, 1 = present). */ - unsigned chrm_white_x; /* White Point x times 100000 */ - unsigned chrm_white_y; /* White Point y times 100000 */ - unsigned chrm_red_x; /* Red x times 100000 */ - unsigned chrm_red_y; /* Red y times 100000 */ - unsigned chrm_green_x; /* Green x times 100000 */ - unsigned chrm_green_y; /* Green y times 100000 */ - unsigned chrm_blue_x; /* Blue x times 100000 */ - unsigned chrm_blue_y; /* Blue y times 100000 */ - - /* - sRGB chunk: optional. May not appear at the same time as iCCP. - If gAMA is also present gAMA must contain value 45455. - If cHRM is also present cHRM must contain respectively 31270,32900,64000,33000,30000,60000,15000,6000. - */ - unsigned srgb_defined; /* Whether an sRGB chunk is present (0 = not present, 1 = present). */ - unsigned srgb_intent; /* Rendering intent: 0=perceptual, 1=rel. colorimetric, 2=saturation, 3=abs. colorimetric */ - - /* - iCCP chunk: optional. May not appear at the same time as sRGB. - - LodePNG does not parse or use the ICC profile (except its color space header field for an edge case), a - separate library to handle the ICC data (not included in LodePNG) format is needed to use it for color - management and conversions. - - For encoding, if iCCP is present, gAMA and cHRM are recommended to be added as well with values that match the ICC - profile as closely as possible, if you wish to do this you should provide the correct values for gAMA and cHRM and - enable their '_defined' flags since LodePNG will not automatically compute them from the ICC profile. - - For encoding, the ICC profile is required by the PNG specification to be an "RGB" profile for non-gray - PNG color types and a "GRAY" profile for gray PNG color types. If you disable auto_convert, you must ensure - the ICC profile type matches your requested color type, else the encoder gives an error. If auto_convert is - enabled (the default), and the ICC profile is not a good match for the pixel data, this will result in an encoder - error if the pixel data has non-gray pixels for a GRAY profile, or a silent less-optimal compression of the pixel - data if the pixels could be encoded as grayscale but the ICC profile is RGB. - - To avoid this do not set an ICC profile in the image unless there is a good reason for it, and when doing so - make sure you compute it carefully to avoid the above problems. - */ - unsigned iccp_defined; /* Whether an iCCP chunk is present (0 = not present, 1 = present). */ - char* iccp_name; /* Null terminated string with profile name, 1-79 bytes */ - /* - The ICC profile in iccp_profile_size bytes. - Don't allocate this buffer yourself. Use the init/cleanup functions - correctly and use lodepng_set_icc and lodepng_clear_icc. - */ - unsigned char* iccp_profile; - unsigned iccp_profile_size; /* The size of iccp_profile in bytes */ - - /* End of color profile related chunks */ - - - /* - unknown chunks: chunks not known by LodePNG, passed on byte for byte. - - There are 3 buffers, one for each position in the PNG where unknown chunks can appear. - Each buffer contains all unknown chunks for that position consecutively. - The 3 positions are: - 0: between IHDR and PLTE, 1: between PLTE and IDAT, 2: between IDAT and IEND. - - For encoding, do not store critical chunks or known chunks that are enabled with a "_defined" flag - above in here, since the encoder will blindly follow this and could then encode an invalid PNG file - (such as one with two IHDR chunks or the disallowed combination of sRGB with iCCP). But do use - this if you wish to store an ancillary chunk that is not supported by LodePNG (such as sPLT or hIST), - or any non-standard PNG chunk. - - Do not allocate or traverse this data yourself. Use the chunk traversing functions declared - later, such as lodepng_chunk_next and lodepng_chunk_append, to read/write this struct. - */ - unsigned char* unknown_chunks_data[3]; - size_t unknown_chunks_size[3]; /*size in bytes of the unknown chunks, given for protection*/ + /* + Suggested background color chunk (bKGD) + + This uses the same color mode and bit depth as the PNG (except no alpha channel), + with values truncated to the bit depth in the unsigned integer. + + For grayscale and palette PNGs, the value is stored in background_r. The values + in background_g and background_b are then unused. The decoder will set them + equal to background_r, the encoder ignores them in this case. + + When decoding, you may get these in a different color mode than the one you requested + for the raw pixels: the colortype and bitdepth defined by info_png.color, that is the + ones defined in the header of the PNG image, are used. + + When encoding with auto_convert, you must use the color model defined in info_png.color for + these values. The encoder normally ignores info_png.color when auto_convert is on, but will + use it to interpret these values (and convert copies of them to its chosen color model). + + When encoding, avoid setting this to an expensive color, such as a non-gray value + when the image is gray, or the compression will be worse since it will be forced to + write the PNG with a more expensive color mode (when auto_convert is on). + + The decoder does not use this background color to edit the color of pixels. This is a + completely optional metadata feature. + */ + unsigned background_defined; /*is a suggested background color given?*/ + unsigned background_r; /*red/gray/palette component of suggested background color*/ + unsigned background_g; /*green component of suggested background color*/ + unsigned background_b; /*blue component of suggested background color*/ + + /* + Non-international text chunks (tEXt and zTXt) + + The char** arrays each contain num strings. The actual messages are in + text_strings, while text_keys are keywords that give a short description what + the actual text represents, e.g. Title, Author, Description, or anything else. + + All the string fields below including strings, keys, names and language tags are null terminated. + The PNG specification uses null characters for the keys, names and tags, and forbids null + characters to appear in the main text which is why we can use null termination everywhere here. + + A keyword is minimum 1 character and maximum 79 characters long (plus the + additional null terminator). It's discouraged to use a single line length + longer than 79 characters for texts. + + Don't allocate these text buffers yourself. Use the init/cleanup functions + correctly and use lodepng_add_text and lodepng_clear_text. + + Standard text chunk keywords and strings are encoded using Latin-1. + */ + size_t text_num; /*the amount of texts in these char** buffers (there may be more texts in itext)*/ + char ** text_keys; /*the keyword of a text chunk (e.g. "Comment")*/ + char ** text_strings; /*the actual text*/ + + /* + International text chunks (iTXt) + Similar to the non-international text chunks, but with additional strings + "langtags" and "transkeys", and the following text encodings are used: + keys: Latin-1, langtags: ASCII, transkeys and strings: UTF-8. + keys must be 1-79 characters (plus the additional null terminator), the other + strings are any length. + */ + size_t itext_num; /*the amount of international texts in this PNG*/ + char ** itext_keys; /*the English keyword of the text chunk (e.g. "Comment")*/ + char ** itext_langtags; /*language tag for this text's language, ISO/IEC 646 string, e.g. ISO 639 language tag*/ + char ** itext_transkeys; /*keyword translated to the international language - UTF-8 string*/ + char ** itext_strings; /*the actual international text - UTF-8 string*/ + + /*time chunk (tIME)*/ + unsigned time_defined; /*set to 1 to make the encoder generate a tIME chunk*/ + LodePNGTime time; + + /*phys chunk (pHYs)*/ + unsigned phys_defined; /*if 0, there is no pHYs chunk and the values below are undefined, if 1 else there is one*/ + unsigned phys_x; /*pixels per unit in x direction*/ + unsigned phys_y; /*pixels per unit in y direction*/ + unsigned phys_unit; /*may be 0 (unknown unit) or 1 (metre)*/ + + /* + Color profile related chunks: gAMA, cHRM, sRGB, iCPP, sBIT + + LodePNG does not apply any color conversions on pixels in the encoder or decoder and does not interpret these color + profile values. It merely passes on the information. If you wish to use color profiles and convert colors, please + use these values with a color management library. + + See the PNG, ICC and sRGB specifications for more information about the meaning of these values. + */ + + /* gAMA chunk: optional, overridden by sRGB or iCCP if those are present. */ + unsigned gama_defined; /* Whether a gAMA chunk is present (0 = not present, 1 = present). */ + unsigned gama_gamma; /* Gamma exponent times 100000 */ + + /* cHRM chunk: optional, overridden by sRGB or iCCP if those are present. */ + unsigned chrm_defined; /* Whether a cHRM chunk is present (0 = not present, 1 = present). */ + unsigned chrm_white_x; /* White Point x times 100000 */ + unsigned chrm_white_y; /* White Point y times 100000 */ + unsigned chrm_red_x; /* Red x times 100000 */ + unsigned chrm_red_y; /* Red y times 100000 */ + unsigned chrm_green_x; /* Green x times 100000 */ + unsigned chrm_green_y; /* Green y times 100000 */ + unsigned chrm_blue_x; /* Blue x times 100000 */ + unsigned chrm_blue_y; /* Blue y times 100000 */ + + /* + sRGB chunk: optional. May not appear at the same time as iCCP. + If gAMA is also present gAMA must contain value 45455. + If cHRM is also present cHRM must contain respectively 31270,32900,64000,33000,30000,60000,15000,6000. + */ + unsigned srgb_defined; /* Whether an sRGB chunk is present (0 = not present, 1 = present). */ + unsigned srgb_intent; /* Rendering intent: 0=perceptual, 1=rel. colorimetric, 2=saturation, 3=abs. colorimetric */ + + /* + iCCP chunk: optional. May not appear at the same time as sRGB. + + LodePNG does not parse or use the ICC profile (except its color space header field for an edge case), a + separate library to handle the ICC data (not included in LodePNG) format is needed to use it for color + management and conversions. + + For encoding, if iCCP is present, gAMA and cHRM are recommended to be added as well with values that match the ICC + profile as closely as possible, if you wish to do this you should provide the correct values for gAMA and cHRM and + enable their '_defined' flags since LodePNG will not automatically compute them from the ICC profile. + + For encoding, the ICC profile is required by the PNG specification to be an "RGB" profile for non-gray + PNG color types and a "GRAY" profile for gray PNG color types. If you disable auto_convert, you must ensure + the ICC profile type matches your requested color type, else the encoder gives an error. If auto_convert is + enabled (the default), and the ICC profile is not a good match for the pixel data, this will result in an encoder + error if the pixel data has non-gray pixels for a GRAY profile, or a silent less-optimal compression of the pixel + data if the pixels could be encoded as grayscale but the ICC profile is RGB. + + To avoid this do not set an ICC profile in the image unless there is a good reason for it, and when doing so + make sure you compute it carefully to avoid the above problems. + */ + unsigned iccp_defined; /* Whether an iCCP chunk is present (0 = not present, 1 = present). */ + char * iccp_name; /* Null terminated string with profile name, 1-79 bytes */ + /* + The ICC profile in iccp_profile_size bytes. + Don't allocate this buffer yourself. Use the init/cleanup functions + correctly and use lodepng_set_icc and lodepng_clear_icc. + */ + unsigned char * iccp_profile; + unsigned iccp_profile_size; /* The size of iccp_profile in bytes */ + + /* + sBIT chunk: significant bits. Optional metadata, only set this if needed. + + If defined, these values give the bit depth of the original data. Since PNG only stores 1, 2, 4, 8 or 16-bit + per channel data, the significant bits value can be used to indicate the original encoded data has another + sample depth, such as 10 or 12. + + Encoders using this value, when storing the pixel data, should use the most significant bits + of the data to store the original bits, and use a good sample depth scaling method such as + "left bit replication" to fill in the least significant bits, rather than fill zeroes. + + Decoders using this value, if able to work with data that's e.g. 10-bit or 12-bit, should right + shift the data to go back to the original bit depth, but decoders are also allowed to ignore + sbit and work e.g. with the 8-bit or 16-bit data from the PNG directly, since thanks + to the encoder contract, the values encoded in PNG are in valid range for the PNG bit depth. + + For grayscale images, sbit_g and sbit_b are not used, and for images that don't use color + type RGBA or grayscale+alpha, sbit_a is not used (it's not used even for palette images with + translucent palette values, or images with color key). The values that are used must be + greater than zero and smaller than or equal to the PNG bit depth. + + The color type from the header in the PNG image defines these used and unused fields: if + decoding with a color mode conversion, such as always decoding to RGBA, this metadata still + only uses the color type of the original PNG, and may e.g. lack the alpha channel info + if the PNG was RGB. When encoding with auto_convert (as well as without), also always the + color model defined in info_png.color determines this. + + NOTE: enabling sbit can hurt compression, because the encoder can then not always use + auto_convert to choose a more optimal color mode for the data, because the PNG format has + strict requirements for the allowed sbit values in combination with color modes. + For example, setting these fields to 10-bit will force the encoder to keep using a 16-bit per channel + color mode, even if the pixel data would in fact fit in a more efficient 8-bit mode. + */ + unsigned sbit_defined; /*is significant bits given? if not, the values below are unused*/ + unsigned sbit_r; /*red or gray component of significant bits*/ + unsigned sbit_g; /*green component of significant bits*/ + unsigned sbit_b; /*blue component of significant bits*/ + unsigned sbit_a; /*alpha component of significant bits*/ + + /* End of color profile related chunks */ + + + /* + unknown chunks: chunks not known by LodePNG, passed on byte for byte. + + There are 3 buffers, one for each position in the PNG where unknown chunks can appear. + Each buffer contains all unknown chunks for that position consecutively. + The 3 positions are: + 0: between IHDR and PLTE, 1: between PLTE and IDAT, 2: between IDAT and IEND. + + For encoding, do not store critical chunks or known chunks that are enabled with a "_defined" flag + above in here, since the encoder will blindly follow this and could then encode an invalid PNG file + (such as one with two IHDR chunks or the disallowed combination of sRGB with iCCP). But do use + this if you wish to store an ancillary chunk that is not supported by LodePNG (such as sPLT or hIST), + or any non-standard PNG chunk. + + Do not allocate or traverse this data yourself. Use the chunk traversing functions declared + later, such as lodepng_chunk_next and lodepng_chunk_append, to read/write this struct. + */ + unsigned char * unknown_chunks_data[3]; + size_t unknown_chunks_size[3]; /*size in bytes of the unknown chunks, given for protection*/ #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ } LodePNGInfo; /*init, cleanup and copy functions to use with this struct*/ -void lodepng_info_init(LodePNGInfo* info); -void lodepng_info_cleanup(LodePNGInfo* info); +void lodepng_info_init(LodePNGInfo * info); +void lodepng_info_cleanup(LodePNGInfo * info); /*return value is error code (0 means no error)*/ -unsigned lodepng_info_copy(LodePNGInfo* dest, const LodePNGInfo* source); +unsigned lodepng_info_copy(LodePNGInfo * dest, const LodePNGInfo * source); #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS -unsigned lodepng_add_text(LodePNGInfo* info, const char* key, const char* str); /*push back both texts at once*/ -void lodepng_clear_text(LodePNGInfo* info); /*use this to clear the texts again after you filled them in*/ +unsigned lodepng_add_text(LodePNGInfo * info, const char * key, const char * str); /*push back both texts at once*/ +void lodepng_clear_text(LodePNGInfo * info); /*use this to clear the texts again after you filled them in*/ -unsigned lodepng_add_itext(LodePNGInfo* info, const char* key, const char* langtag, - const char* transkey, const char* str); /*push back the 4 texts of 1 chunk at once*/ -void lodepng_clear_itext(LodePNGInfo* info); /*use this to clear the itexts again after you filled them in*/ +unsigned lodepng_add_itext(LodePNGInfo * info, const char * key, const char * langtag, + const char * transkey, const char * str); /*push back the 4 texts of 1 chunk at once*/ +void lodepng_clear_itext(LodePNGInfo * info); /*use this to clear the itexts again after you filled them in*/ /*replaces if exists*/ -unsigned lodepng_set_icc(LodePNGInfo* info, const char* name, const unsigned char* profile, unsigned profile_size); -void lodepng_clear_icc(LodePNGInfo* info); /*use this to clear the texts again after you filled them in*/ +unsigned lodepng_set_icc(LodePNGInfo * info, const char * name, const unsigned char * profile, unsigned profile_size); +void lodepng_clear_icc(LodePNGInfo * info); /*use this to clear the texts again after you filled them in*/ #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ /* @@ -631,8 +724,8 @@ For < 8 bpp images, there should not be padding bits at the end of scanlines. For 16-bit per channel colors, uses big endian format like PNG does. Return value is LodePNG error code */ -unsigned lodepng_convert(unsigned char* out, const unsigned char* in, - const LodePNGColorMode* mode_out, const LodePNGColorMode* mode_in, +unsigned lodepng_convert(unsigned char * out, const unsigned char * in, + const LodePNGColorMode * mode_out, const LodePNGColorMode * mode_in, unsigned w, unsigned h); #ifdef LODEPNG_COMPILE_DECODER @@ -641,124 +734,129 @@ Settings for the decoder. This contains settings for the PNG and the Zlib decoder, but not the Info settings from the Info structs. */ typedef struct LodePNGDecoderSettings { - LodePNGDecompressSettings zlibsettings; /*in here is the setting to ignore Adler32 checksums*/ + LodePNGDecompressSettings zlibsettings; /*in here is the setting to ignore Adler32 checksums*/ - /* Check LodePNGDecompressSettings for more ignorable errors such as ignore_adler32 */ - unsigned ignore_crc; /*ignore CRC checksums*/ - unsigned ignore_critical; /*ignore unknown critical chunks*/ - unsigned ignore_end; /*ignore issues at end of file if possible (missing IEND chunk, too large chunk, ...)*/ - /* TODO: make a system involving warnings with levels and a strict mode instead. Other potentially recoverable - errors: srgb rendering intent value, size of content of ancillary chunks, more than 79 characters for some - strings, placement/combination rules for ancillary chunks, crc of unknown chunks, allowed characters - in string keys, etc... */ + /* Check LodePNGDecompressSettings for more ignorable errors such as ignore_adler32 */ + unsigned ignore_crc; /*ignore CRC checksums*/ + unsigned ignore_critical; /*ignore unknown critical chunks*/ + unsigned ignore_end; /*ignore issues at end of file if possible (missing IEND chunk, too large chunk, ...)*/ + /* TODO: make a system involving warnings with levels and a strict mode instead. Other potentially recoverable + errors: srgb rendering intent value, size of content of ancillary chunks, more than 79 characters for some + strings, placement/combination rules for ancillary chunks, crc of unknown chunks, allowed characters + in string keys, etc... */ - unsigned color_convert; /*whether to convert the PNG to the color type you want. Default: yes*/ + unsigned color_convert; /*whether to convert the PNG to the color type you want. Default: yes*/ #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS - unsigned read_text_chunks; /*if false but remember_unknown_chunks is true, they're stored in the unknown chunks*/ + unsigned read_text_chunks; /*if false but remember_unknown_chunks is true, they're stored in the unknown chunks*/ - /*store all bytes from unknown chunks in the LodePNGInfo (off by default, useful for a png editor)*/ - unsigned remember_unknown_chunks; + /*store all bytes from unknown chunks in the LodePNGInfo (off by default, useful for a png editor)*/ + unsigned remember_unknown_chunks; - /* maximum size for decompressed text chunks. If a text chunk's text is larger than this, an error is returned, - unless reading text chunks is disabled or this limit is set higher or disabled. Set to 0 to allow any size. - By default it is a value that prevents unreasonably large strings from hogging memory. */ - size_t max_text_size; + /* maximum size for decompressed text chunks. If a text chunk's text is larger than this, an error is returned, + unless reading text chunks is disabled or this limit is set higher or disabled. Set to 0 to allow any size. + By default it is a value that prevents unreasonably large strings from hogging memory. */ + size_t max_text_size; - /* maximum size for compressed ICC chunks. If the ICC profile is larger than this, an error will be returned. Set to - 0 to allow any size. By default this is a value that prevents ICC profiles that would be much larger than any - legitimate profile could be to hog memory. */ - size_t max_icc_size; + /* maximum size for compressed ICC chunks. If the ICC profile is larger than this, an error will be returned. Set to + 0 to allow any size. By default this is a value that prevents ICC profiles that would be much larger than any + legitimate profile could be to hog memory. */ + size_t max_icc_size; #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ } LodePNGDecoderSettings; -void lodepng_decoder_settings_init(LodePNGDecoderSettings* settings); +void lodepng_decoder_settings_init(LodePNGDecoderSettings * settings); #endif /*LODEPNG_COMPILE_DECODER*/ #ifdef LODEPNG_COMPILE_ENCODER /*automatically use color type with less bits per pixel if losslessly possible. Default: AUTO*/ typedef enum LodePNGFilterStrategy { - /*every filter at zero*/ - LFS_ZERO = 0, - /*every filter at 1, 2, 3 or 4 (paeth), unlike LFS_ZERO not a good choice, but for testing*/ - LFS_ONE = 1, - LFS_TWO = 2, - LFS_THREE = 3, - LFS_FOUR = 4, - /*Use filter that gives minimum sum, as described in the official PNG filter heuristic.*/ - LFS_MINSUM, - /*Use the filter type that gives smallest Shannon entropy for this scanline. Depending - on the image, this is better or worse than minsum.*/ - LFS_ENTROPY, - /* - Brute-force-search PNG filters by compressing each filter for each scanline. - Experimental, very slow, and only rarely gives better compression than MINSUM. - */ - LFS_BRUTE_FORCE, - /*use predefined_filters buffer: you specify the filter type for each scanline*/ - LFS_PREDEFINED + /*every filter at zero*/ + LFS_ZERO = 0, + /*every filter at 1, 2, 3 or 4 (paeth), unlike LFS_ZERO not a good choice, but for testing*/ + LFS_ONE = 1, + LFS_TWO = 2, + LFS_THREE = 3, + LFS_FOUR = 4, + /*Use filter that gives minimum sum, as described in the official PNG filter heuristic.*/ + LFS_MINSUM, + /*Use the filter type that gives smallest Shannon entropy for this scanline. Depending + on the image, this is better or worse than minsum.*/ + LFS_ENTROPY, + /* + Brute-force-search PNG filters by compressing each filter for each scanline. + Experimental, very slow, and only rarely gives better compression than MINSUM. + */ + LFS_BRUTE_FORCE, + /*use predefined_filters buffer: you specify the filter type for each scanline*/ + LFS_PREDEFINED } LodePNGFilterStrategy; /*Gives characteristics about the integer RGBA colors of the image (count, alpha channel usage, bit depth, ...), which helps decide which color model to use for encoding. Used internally by default if "auto_convert" is enabled. Public because it's useful for custom algorithms.*/ typedef struct LodePNGColorStats { - unsigned colored; /*not grayscale*/ - unsigned key; /*image is not opaque and color key is possible instead of full alpha*/ - unsigned short key_r; /*key values, always as 16-bit, in 8-bit case the byte is duplicated, e.g. 65535 means 255*/ - unsigned short key_g; - unsigned short key_b; - unsigned alpha; /*image is not opaque and alpha channel or alpha palette required*/ - unsigned numcolors; /*amount of colors, up to 257. Not valid if bits == 16 or allow_palette is disabled.*/ - unsigned char palette[1024]; /*Remembers up to the first 256 RGBA colors, in no particular order, only valid when numcolors is valid*/ - unsigned bits; /*bits per channel (not for palette). 1,2 or 4 for grayscale only. 16 if 16-bit per channel required.*/ - size_t numpixels; - - /*user settings for computing/using the stats*/ - unsigned allow_palette; /*default 1. if 0, disallow choosing palette colortype in auto_choose_color, and don't count numcolors*/ - unsigned allow_greyscale; /*default 1. if 0, choose RGB or RGBA even if the image only has gray colors*/ + unsigned colored; /*not grayscale*/ + unsigned key; /*image is not opaque and color key is possible instead of full alpha*/ + unsigned short key_r; /*key values, always as 16-bit, in 8-bit case the byte is duplicated, e.g. 65535 means 255*/ + unsigned short key_g; + unsigned short key_b; + unsigned alpha; /*image is not opaque and alpha channel or alpha palette required*/ + unsigned numcolors; /*amount of colors, up to 257. Not valid if bits == 16 or allow_palette is disabled.*/ + unsigned char + palette[1024]; /*Remembers up to the first 256 RGBA colors, in no particular order, only valid when numcolors is valid*/ + unsigned bits; /*bits per channel (not for palette). 1,2 or 4 for grayscale only. 16 if 16-bit per channel required.*/ + size_t numpixels; + + /*user settings for computing/using the stats*/ + unsigned allow_palette; /*default 1. if 0, disallow choosing palette colortype in auto_choose_color, and don't count numcolors*/ + unsigned allow_greyscale; /*default 1. if 0, choose RGB or RGBA even if the image only has gray colors*/ } LodePNGColorStats; -void lodepng_color_stats_init(LodePNGColorStats* stats); +void lodepng_color_stats_init(LodePNGColorStats * stats); /*Get a LodePNGColorStats of the image. The stats must already have been inited. Returns error code (e.g. alloc fail) or 0 if ok.*/ -unsigned lodepng_compute_color_stats(LodePNGColorStats* stats, - const unsigned char* image, unsigned w, unsigned h, - const LodePNGColorMode* mode_in); +unsigned lodepng_compute_color_stats(LodePNGColorStats * stats, + const unsigned char * image, unsigned w, unsigned h, + const LodePNGColorMode * mode_in); /*Settings for the encoder.*/ typedef struct LodePNGEncoderSettings { - LodePNGCompressSettings zlibsettings; /*settings for the zlib encoder, such as window size, ...*/ - - unsigned auto_convert; /*automatically choose output PNG color type. Default: true*/ - - /*If true, follows the official PNG heuristic: if the PNG uses a palette or lower than - 8 bit depth, set all filters to zero. Otherwise use the filter_strategy. Note that to - completely follow the official PNG heuristic, filter_palette_zero must be true and - filter_strategy must be LFS_MINSUM*/ - unsigned filter_palette_zero; - /*Which filter strategy to use when not using zeroes due to filter_palette_zero. - Set filter_palette_zero to 0 to ensure always using your chosen strategy. Default: LFS_MINSUM*/ - LodePNGFilterStrategy filter_strategy; - /*used if filter_strategy is LFS_PREDEFINED. In that case, this must point to a buffer with - the same length as the amount of scanlines in the image, and each value must <= 5. You - have to cleanup this buffer, LodePNG will never free it. Don't forget that filter_palette_zero - must be set to 0 to ensure this is also used on palette or low bitdepth images.*/ - const unsigned char* predefined_filters; - - /*force creating a PLTE chunk if colortype is 2 or 6 (= a suggested palette). - If colortype is 3, PLTE is _always_ created.*/ - unsigned force_palette; + LodePNGCompressSettings zlibsettings; /*settings for the zlib encoder, such as window size, ...*/ + + unsigned auto_convert; /*automatically choose output PNG color type. Default: true*/ + + /*If true, follows the official PNG heuristic: if the PNG uses a palette or lower than + 8 bit depth, set all filters to zero. Otherwise use the filter_strategy. Note that to + completely follow the official PNG heuristic, filter_palette_zero must be true and + filter_strategy must be LFS_MINSUM*/ + unsigned filter_palette_zero; + /*Which filter strategy to use when not using zeroes due to filter_palette_zero. + Set filter_palette_zero to 0 to ensure always using your chosen strategy. Default: LFS_MINSUM*/ + LodePNGFilterStrategy filter_strategy; + /*used if filter_strategy is LFS_PREDEFINED. In that case, this must point to a buffer with + the same length as the amount of scanlines in the image, and each value must <= 5. You + have to cleanup this buffer, LodePNG will never free it. Don't forget that filter_palette_zero + must be set to 0 to ensure this is also used on palette or low bitdepth images.*/ + const unsigned char * predefined_filters; + + /*force creating a PLTE chunk if colortype is 2 or 6 (= a suggested palette). + If colortype is 3, PLTE is always created. If color type is explicitely set + to a grayscale type (1 or 4), this is not done and is ignored. If enabling this, + a palette must be present in the info_png. + NOTE: enabling this may worsen compression if auto_convert is used to choose + optimal color mode, because it cannot use grayscale color modes in this case*/ + unsigned force_palette; #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS - /*add LodePNG identifier and version as a text chunk, for debugging*/ - unsigned add_id; - /*encode text chunks as zTXt chunks instead of tEXt chunks, and use compression in iTXt chunks*/ - unsigned text_compression; + /*add LodePNG identifier and version as a text chunk, for debugging*/ + unsigned add_id; + /*encode text chunks as zTXt chunks instead of tEXt chunks, and use compression in iTXt chunks*/ + unsigned text_compression; #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ } LodePNGEncoderSettings; -void lodepng_encoder_settings_init(LodePNGEncoderSettings* settings); +void lodepng_encoder_settings_init(LodePNGEncoderSettings * settings); #endif /*LODEPNG_COMPILE_ENCODER*/ @@ -766,20 +864,20 @@ void lodepng_encoder_settings_init(LodePNGEncoderSettings* settings); /*The settings, state and information for extended encoding and decoding.*/ typedef struct LodePNGState { #ifdef LODEPNG_COMPILE_DECODER - LodePNGDecoderSettings decoder; /*the decoding settings*/ + LodePNGDecoderSettings decoder; /*the decoding settings*/ #endif /*LODEPNG_COMPILE_DECODER*/ #ifdef LODEPNG_COMPILE_ENCODER - LodePNGEncoderSettings encoder; /*the encoding settings*/ + LodePNGEncoderSettings encoder; /*the encoding settings*/ #endif /*LODEPNG_COMPILE_ENCODER*/ - LodePNGColorMode info_raw; /*specifies the format in which you would like to get the raw pixel buffer*/ - LodePNGInfo info_png; /*info of the PNG image obtained after decoding*/ - unsigned error; + LodePNGColorMode info_raw; /*specifies the format in which you would like to get the raw pixel buffer*/ + LodePNGInfo info_png; /*info of the PNG image obtained after decoding*/ + unsigned error; } LodePNGState; /*init, cleanup and copy functions to use with this struct*/ -void lodepng_state_init(LodePNGState* state); -void lodepng_state_cleanup(LodePNGState* state); -void lodepng_state_copy(LodePNGState* dest, const LodePNGState* source); +void lodepng_state_init(LodePNGState * state); +void lodepng_state_cleanup(LodePNGState * state); +void lodepng_state_copy(LodePNGState * dest, const LodePNGState * source); #endif /* defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_ENCODER) */ #ifdef LODEPNG_COMPILE_DECODER @@ -787,23 +885,23 @@ void lodepng_state_copy(LodePNGState* dest, const LodePNGState* source); Same as lodepng_decode_memory, but uses a LodePNGState to allow custom settings and getting much more information about the PNG image and color mode. */ -unsigned lodepng_decode(unsigned char** out, unsigned* w, unsigned* h, - LodePNGState* state, - const unsigned char* in, size_t insize); +unsigned lodepng_decode(unsigned char ** out, unsigned * w, unsigned * h, + LodePNGState * state, + const unsigned char * in, size_t insize); /* Read the PNG header, but not the actual data. This returns only the information that is in the IHDR chunk of the PNG, such as width, height and color type. The information is placed in the info_png field of the LodePNGState. */ -unsigned lodepng_inspect(unsigned* w, unsigned* h, - LodePNGState* state, - const unsigned char* in, size_t insize); +unsigned lodepng_inspect(unsigned * w, unsigned * h, + LodePNGState * state, + const unsigned char * in, size_t insize); #endif /*LODEPNG_COMPILE_DECODER*/ /* -Reads one metadata chunk (other than IHDR) of the PNG file and outputs what it -read in the state. Returns error code on failure. +Reads one metadata chunk (other than IHDR, which is handled by lodepng_inspect) +of the PNG file and outputs what it read in the state. Returns error code on failure. Use lodepng_inspect first with a new state, then e.g. lodepng_chunk_find_const to find the desired chunk type, and if non null use lodepng_inspect_chunk (with chunk_pointer - start_of_file as pos). @@ -813,14 +911,14 @@ Requirements: &in[pos] must point to start of a chunk, must use regular lodepng_inspect first since format of most other chunks depends on IHDR, and if there is a PLTE chunk, that one must be inspected before tRNS or bKGD. */ -unsigned lodepng_inspect_chunk(LodePNGState* state, size_t pos, - const unsigned char* in, size_t insize); +unsigned lodepng_inspect_chunk(LodePNGState * state, size_t pos, + const unsigned char * in, size_t insize); #ifdef LODEPNG_COMPILE_ENCODER /*This function allocates the out buffer with standard malloc and stores the size in *outsize.*/ -unsigned lodepng_encode(unsigned char** out, size_t* outsize, - const unsigned char* image, unsigned w, unsigned h, - LodePNGState* state); +unsigned lodepng_encode(unsigned char ** out, size_t * outsize, + const unsigned char * image, unsigned w, unsigned h, + LodePNGState * state); #endif /*LODEPNG_COMPILE_ENCODER*/ /* @@ -852,32 +950,32 @@ Gets the length of the data of the chunk. Total chunk length has 12 bytes more. There must be at least 4 bytes to read from. If the result value is too large, it may be corrupt data. */ -unsigned lodepng_chunk_length(const unsigned char* chunk); +unsigned lodepng_chunk_length(const unsigned char * chunk); /*puts the 4-byte type in null terminated string*/ -void lodepng_chunk_type(char type[5], const unsigned char* chunk); +void lodepng_chunk_type(char type[5], const unsigned char * chunk); /*check if the type is the given type*/ -unsigned char lodepng_chunk_type_equals(const unsigned char* chunk, const char* type); +unsigned char lodepng_chunk_type_equals(const unsigned char * chunk, const char * type); /*0: it's one of the critical chunk types, 1: it's an ancillary chunk (see PNG standard)*/ -unsigned char lodepng_chunk_ancillary(const unsigned char* chunk); +unsigned char lodepng_chunk_ancillary(const unsigned char * chunk); /*0: public, 1: private (see PNG standard)*/ -unsigned char lodepng_chunk_private(const unsigned char* chunk); +unsigned char lodepng_chunk_private(const unsigned char * chunk); /*0: the chunk is unsafe to copy, 1: the chunk is safe to copy (see PNG standard)*/ -unsigned char lodepng_chunk_safetocopy(const unsigned char* chunk); +unsigned char lodepng_chunk_safetocopy(const unsigned char * chunk); /*get pointer to the data of the chunk, where the input points to the header of the chunk*/ -unsigned char* lodepng_chunk_data(unsigned char* chunk); -const unsigned char* lodepng_chunk_data_const(const unsigned char* chunk); +unsigned char * lodepng_chunk_data(unsigned char * chunk); +const unsigned char * lodepng_chunk_data_const(const unsigned char * chunk); /*returns 0 if the crc is correct, 1 if it's incorrect (0 for OK as usual!)*/ -unsigned lodepng_chunk_check_crc(const unsigned char* chunk); +unsigned lodepng_chunk_check_crc(const unsigned char * chunk); /*generates the correct CRC from the data and puts it in the last 4 bytes of the chunk*/ -void lodepng_chunk_generate_crc(unsigned char* chunk); +void lodepng_chunk_generate_crc(unsigned char * chunk); /* Iterate to next chunks, allows iterating through all chunks of the PNG file. @@ -890,19 +988,20 @@ is no more chunk after this or possibly if the chunk is corrupt. Start this process at the 8th byte of the PNG file. In a non-corrupt PNG file, the last chunk should have name "IEND". */ -unsigned char* lodepng_chunk_next(unsigned char* chunk, unsigned char* end); -const unsigned char* lodepng_chunk_next_const(const unsigned char* chunk, const unsigned char* end); +unsigned char * lodepng_chunk_next(unsigned char * chunk, unsigned char * end); +const unsigned char * lodepng_chunk_next_const(const unsigned char * chunk, const unsigned char * end); /*Finds the first chunk with the given type in the range [chunk, end), or returns NULL if not found.*/ -unsigned char* lodepng_chunk_find(unsigned char* chunk, unsigned char* end, const char type[5]); -const unsigned char* lodepng_chunk_find_const(const unsigned char* chunk, const unsigned char* end, const char type[5]); +unsigned char * lodepng_chunk_find(unsigned char * chunk, unsigned char * end, const char type[5]); +const unsigned char * lodepng_chunk_find_const(const unsigned char * chunk, const unsigned char * end, + const char type[5]); /* Appends chunk to the data in out. The given chunk should already have its chunk header. The out variable and outsize are updated to reflect the new reallocated buffer. Returns error code (0 if it went ok) */ -unsigned lodepng_chunk_append(unsigned char** out, size_t* outsize, const unsigned char* chunk); +unsigned lodepng_chunk_append(unsigned char ** out, size_t * outsize, const unsigned char * chunk); /* Appends new chunk to out. The chunk to append is given by giving its length, type @@ -910,12 +1009,12 @@ and data separately. The type is a 4-letter string. The out variable and outsize are updated to reflect the new reallocated buffer. Returne error code (0 if it went ok) */ -unsigned lodepng_chunk_create(unsigned char** out, size_t* outsize, unsigned length, - const char* type, const unsigned char* data); +unsigned lodepng_chunk_create(unsigned char ** out, size_t * outsize, size_t length, + const char * type, const unsigned char * data); /*Calculate CRC32 of buffer*/ -unsigned lodepng_crc32(const unsigned char* buf, size_t len); +unsigned lodepng_crc32(const unsigned char * buf, size_t len); #endif /*LODEPNG_COMPILE_PNG*/ @@ -928,9 +1027,9 @@ part of zlib that is required for PNG, it does not support dictionaries. #ifdef LODEPNG_COMPILE_DECODER /*Inflate a buffer. Inflate is the decompression step of deflate. Out buffer must be freed after use.*/ -unsigned lodepng_inflate(unsigned char** out, size_t* outsize, - const unsigned char* in, size_t insize, - const LodePNGDecompressSettings* settings); +unsigned lodepng_inflate(unsigned char ** out, size_t * outsize, + const unsigned char * in, size_t insize, + const LodePNGDecompressSettings * settings); /* Decompresses Zlib data. Reallocates the out buffer and appends the data. The @@ -938,9 +1037,9 @@ data must be according to the zlib specification. Either, *out must be NULL and *outsize must be 0, or, *out must be a valid buffer and *outsize its size in bytes. out must be freed by user after usage. */ -unsigned lodepng_zlib_decompress(unsigned char** out, size_t* outsize, - const unsigned char* in, size_t insize, - const LodePNGDecompressSettings* settings); +unsigned lodepng_zlib_decompress(unsigned char ** out, size_t * outsize, + const unsigned char * in, size_t insize, + const LodePNGDecompressSettings * settings); #endif /*LODEPNG_COMPILE_DECODER*/ #ifdef LODEPNG_COMPILE_ENCODER @@ -951,113 +1050,127 @@ The data is output in the format of the zlib specification. Either, *out must be NULL and *outsize must be 0, or, *out must be a valid buffer and *outsize its size in bytes. out must be freed by user after usage. */ -unsigned lodepng_zlib_compress(unsigned char** out, size_t* outsize, - const unsigned char* in, size_t insize, - const LodePNGCompressSettings* settings); +unsigned lodepng_zlib_compress(unsigned char ** out, size_t * outsize, + const unsigned char * in, size_t insize, + const LodePNGCompressSettings * settings); /* Find length-limited Huffman code for given frequencies. This function is in the public interface only for tests, it's used internally by lodepng_deflate. */ -unsigned lodepng_huffman_code_lengths(unsigned* lengths, const unsigned* frequencies, +unsigned lodepng_huffman_code_lengths(unsigned * lengths, const unsigned * frequencies, size_t numcodes, unsigned maxbitlen); /*Compress a buffer with deflate. See RFC 1951. Out buffer must be freed after use.*/ -unsigned lodepng_deflate(unsigned char** out, size_t* outsize, - const unsigned char* in, size_t insize, - const LodePNGCompressSettings* settings); +unsigned lodepng_deflate(unsigned char ** out, size_t * outsize, + const unsigned char * in, size_t insize, + const LodePNGCompressSettings * settings); #endif /*LODEPNG_COMPILE_ENCODER*/ #endif /*LODEPNG_COMPILE_ZLIB*/ #ifdef LODEPNG_COMPILE_DISK -/* -Load a file from disk into buffer. The function allocates the out buffer, and -after usage you should free it. -out: output parameter, contains pointer to loaded buffer. -outsize: output parameter, size of the allocated out buffer -filename: the path to the file to load -return value: error code (0 means ok) -*/ -unsigned lodepng_load_file(unsigned char** out, size_t* outsize, const char* filename); - -/* -Save a file from buffer to disk. Warning, if it exists, this function overwrites -the file without warning! -buffer: the buffer to write -buffersize: size of the buffer to write -filename: the path to the file to save to -return value: error code (0 means ok) -*/ -unsigned lodepng_save_file(const unsigned char* buffer, size_t buffersize, const char* filename); + /* + Load a file from disk into buffer. The function allocates the out buffer, and + after usage you should free it. + out: output parameter, contains pointer to loaded buffer. + outsize: output parameter, size of the allocated out buffer + filename: the path to the file to load + return value: error code (0 means ok) + + NOTE: Wide-character filenames are not supported, you can use an external method + to handle such files and decode in-memory. + */ + unsigned lodepng_load_file(unsigned char ** out, size_t * outsize, const char * filename); + + /* + Save a file from buffer to disk. Warning, if it exists, this function overwrites + the file without warning! + buffer: the buffer to write + buffersize: size of the buffer to write + filename: the path to the file to save to + return value: error code (0 means ok) + + NOTE: Wide-character filenames are not supported, you can use an external method + to handle such files and encode in-memory + */ + unsigned lodepng_save_file(const unsigned char * buffer, size_t buffersize, const char * filename); #endif /*LODEPNG_COMPILE_DISK*/ #ifdef LODEPNG_COMPILE_CPP /* The LodePNG C++ wrapper uses std::vectors instead of manually allocated memory buffers. */ -namespace lodepng { +namespace lodepng +{ #ifdef LODEPNG_COMPILE_PNG -class State : public LodePNGState { - public: - State(); - State(const State& other); - ~State(); - State& operator=(const State& other); +class State : public LodePNGState +{ + public: + State(); + State(const State & other); + ~State(); + State & operator=(const State & other); }; #ifdef LODEPNG_COMPILE_DECODER /* Same as other lodepng::decode, but using a State for more settings and information. */ -unsigned decode(std::vector& out, unsigned& w, unsigned& h, - State& state, - const unsigned char* in, size_t insize); -unsigned decode(std::vector& out, unsigned& w, unsigned& h, - State& state, - const std::vector& in); +unsigned decode(std::vector & out, unsigned & w, unsigned & h, + State & state, + const unsigned char * in, size_t insize); +unsigned decode(std::vector & out, unsigned & w, unsigned & h, + State & state, + const std::vector & in); #endif /*LODEPNG_COMPILE_DECODER*/ #ifdef LODEPNG_COMPILE_ENCODER /* Same as other lodepng::encode, but using a State for more settings and information. */ -unsigned encode(std::vector& out, - const unsigned char* in, unsigned w, unsigned h, - State& state); -unsigned encode(std::vector& out, - const std::vector& in, unsigned w, unsigned h, - State& state); +unsigned encode(std::vector & out, + const unsigned char * in, unsigned w, unsigned h, + State & state); +unsigned encode(std::vector & out, + const std::vector & in, unsigned w, unsigned h, + State & state); #endif /*LODEPNG_COMPILE_ENCODER*/ #ifdef LODEPNG_COMPILE_DISK -/* -Load a file from disk into an std::vector. -return value: error code (0 means ok) -*/ -unsigned load_file(std::vector& buffer, const std::string& filename); - -/* -Save the binary data in an std::vector to a file on disk. The file is overwritten -without warning. -*/ -unsigned save_file(const std::vector& buffer, const std::string& filename); + /* + Load a file from disk into an std::vector. + return value: error code (0 means ok) + + NOTE: Wide-character filenames are not supported, you can use an external method + to handle such files and decode in-memory + */ + unsigned load_file(std::vector & buffer, const std::string & filename); + + /* + Save the binary data in an std::vector to a file on disk. The file is overwritten + without warning. + + NOTE: Wide-character filenames are not supported, you can use an external method + to handle such files and encode in-memory + */ + unsigned save_file(const std::vector & buffer, const std::string & filename); #endif /* LODEPNG_COMPILE_DISK */ #endif /* LODEPNG_COMPILE_PNG */ #ifdef LODEPNG_COMPILE_ZLIB #ifdef LODEPNG_COMPILE_DECODER /* Zlib-decompress an unsigned char buffer */ -unsigned decompress(std::vector& out, const unsigned char* in, size_t insize, - const LodePNGDecompressSettings& settings = lodepng_default_decompress_settings); +unsigned decompress(std::vector & out, const unsigned char * in, size_t insize, + const LodePNGDecompressSettings & settings = lodepng_default_decompress_settings); /* Zlib-decompress an std::vector */ -unsigned decompress(std::vector& out, const std::vector& in, - const LodePNGDecompressSettings& settings = lodepng_default_decompress_settings); +unsigned decompress(std::vector & out, const std::vector & in, + const LodePNGDecompressSettings & settings = lodepng_default_decompress_settings); #endif /* LODEPNG_COMPILE_DECODER */ #ifdef LODEPNG_COMPILE_ENCODER /* Zlib-compress an unsigned char buffer */ -unsigned compress(std::vector& out, const unsigned char* in, size_t insize, - const LodePNGCompressSettings& settings = lodepng_default_compress_settings); +unsigned compress(std::vector & out, const unsigned char * in, size_t insize, + const LodePNGCompressSettings & settings = lodepng_default_compress_settings); /* Zlib-compress an std::vector */ -unsigned compress(std::vector& out, const std::vector& in, - const LodePNGCompressSettings& settings = lodepng_default_compress_settings); +unsigned compress(std::vector & out, const std::vector & in, + const LodePNGCompressSettings & settings = lodepng_default_compress_settings); #endif /* LODEPNG_COMPILE_ENCODER */ #endif /* LODEPNG_COMPILE_ZLIB */ } /* namespace lodepng */ @@ -1069,7 +1182,7 @@ unsigned compress(std::vector& out, const std::vector (2^31)-1 [ ] partial decoding (stream processing) [X] let the "isFullyOpaque" function check color keys and transparent palettes too @@ -1085,7 +1198,11 @@ unsigned compress(std::vector& out, const std::vector /* size_t */ + + +/** + Introduction + + LZ4 is lossless compression algorithm, providing compression speed >500 MB/s per core, + scalable with multi-cores CPU. It features an extremely fast decoder, with speed in + multiple GB/s per core, typically reaching RAM speed limits on multi-core systems. + + The LZ4 compression library provides in-memory compression and decompression functions. + It gives full buffer control to user. + Compression can be done in: + - a single step (described as Simple Functions) + - a single step, reusing a context (described in Advanced Functions) + - unbounded multiple steps (described as Streaming compression) + + lz4.h generates and decodes LZ4-compressed blocks (doc/lz4_Block_format.md). + Decompressing such a compressed block requires additional metadata. + Exact metadata depends on exact decompression function. + For the typical case of LZ4_decompress_safe(), + metadata includes block's compressed size, and maximum bound of decompressed size. + Each application is free to encode and pass such metadata in whichever way it wants. + + lz4.h only handle blocks, it cannot generate Frames. + + Blocks are different from Frames (doc/lz4_Frame_format.md). + Frames bundle both blocks and metadata in a specified manner. + Embedding metadata is required for compressed data to be self-contained and portable. + Frame format is delivered through a companion API, declared in lz4frame.h. + The `lz4` CLI can only manage frames. +*/ + +/*^*************************************************************** +* Export parameters +*****************************************************************/ +/* +* LZ4_DLL_EXPORT : +* Enable exporting of functions when building a Windows DLL +* LZ4LIB_VISIBILITY : +* Control library symbols visibility. +*/ +#ifndef LZ4LIB_VISIBILITY +# if defined(__GNUC__) && (__GNUC__ >= 4) +# define LZ4LIB_VISIBILITY __attribute__ ((visibility ("default"))) +# else +# define LZ4LIB_VISIBILITY +# endif +#endif +#if defined(LZ4_DLL_EXPORT) && (LZ4_DLL_EXPORT==1) +# define LZ4LIB_API __declspec(dllexport) LZ4LIB_VISIBILITY +#elif defined(LZ4_DLL_IMPORT) && (LZ4_DLL_IMPORT==1) +# define LZ4LIB_API __declspec(dllimport) LZ4LIB_VISIBILITY /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/ +#else +# define LZ4LIB_API LZ4LIB_VISIBILITY +#endif + +/*! LZ4_FREESTANDING : + * When this macro is set to 1, it enables "freestanding mode" that is + * suitable for typical freestanding environment which doesn't support + * standard C library. + * + * - LZ4_FREESTANDING is a compile-time switch. + * - It requires the following macros to be defined: + * LZ4_memcpy, LZ4_memmove, LZ4_memset. + * - It only enables LZ4/HC functions which don't use heap. + * All LZ4F_* functions are not supported. + * - See tests/freestanding.c to check its basic setup. + */ +#if defined(LZ4_FREESTANDING) && (LZ4_FREESTANDING == 1) +# define LZ4_HEAPMODE 0 +# define LZ4HC_HEAPMODE 0 +# define LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION 1 +# if !defined(LZ4_memcpy) +# error "LZ4_FREESTANDING requires macro 'LZ4_memcpy'." +# endif +# if !defined(LZ4_memset) +# error "LZ4_FREESTANDING requires macro 'LZ4_memset'." +# endif +# if !defined(LZ4_memmove) +# error "LZ4_FREESTANDING requires macro 'LZ4_memmove'." +# endif +#elif ! defined(LZ4_FREESTANDING) +# define LZ4_FREESTANDING 0 +#endif + + +/*------ Version ------*/ +#define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */ +#define LZ4_VERSION_MINOR 9 /* for new (non-breaking) interface capabilities */ +#define LZ4_VERSION_RELEASE 5 /* for tweaks, bug-fixes, or development */ + +#define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR *100*100 + LZ4_VERSION_MINOR *100 + LZ4_VERSION_RELEASE) + +#define LZ4_LIB_VERSION LZ4_VERSION_MAJOR.LZ4_VERSION_MINOR.LZ4_VERSION_RELEASE +#define LZ4_QUOTE(str) #str +#define LZ4_EXPAND_AND_QUOTE(str) LZ4_QUOTE(str) +#define LZ4_VERSION_STRING LZ4_EXPAND_AND_QUOTE(LZ4_LIB_VERSION) /* requires v1.7.3+ */ + +LZ4LIB_API int LZ4_versionNumber (void); /**< library version number; useful to check dll version; requires v1.3.0+ */ +LZ4LIB_API const char* LZ4_versionString (void); /**< library version string; useful to check dll version; requires v1.7.5+ */ + + +/*-************************************ +* Tuning parameter +**************************************/ +#define LZ4_MEMORY_USAGE_MIN 10 +#define LZ4_MEMORY_USAGE_DEFAULT 14 +#define LZ4_MEMORY_USAGE_MAX 20 + +/*! + * LZ4_MEMORY_USAGE : + * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; ) + * Increasing memory usage improves compression ratio, at the cost of speed. + * Reduced memory usage may improve speed at the cost of ratio, thanks to better cache locality. + * Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache + */ +#ifndef LZ4_MEMORY_USAGE +# define LZ4_MEMORY_USAGE LZ4_MEMORY_USAGE_DEFAULT +#endif + +#if (LZ4_MEMORY_USAGE < LZ4_MEMORY_USAGE_MIN) +# error "LZ4_MEMORY_USAGE is too small !" +#endif + +#if (LZ4_MEMORY_USAGE > LZ4_MEMORY_USAGE_MAX) +# error "LZ4_MEMORY_USAGE is too large !" +#endif + +/*-************************************ +* Simple Functions +**************************************/ +/*! LZ4_compress_default() : + * Compresses 'srcSize' bytes from buffer 'src' + * into already allocated 'dst' buffer of size 'dstCapacity'. + * Compression is guaranteed to succeed if 'dstCapacity' >= LZ4_compressBound(srcSize). + * It also runs faster, so it's a recommended setting. + * If the function cannot compress 'src' into a more limited 'dst' budget, + * compression stops *immediately*, and the function result is zero. + * In which case, 'dst' content is undefined (invalid). + * srcSize : max supported value is LZ4_MAX_INPUT_SIZE. + * dstCapacity : size of buffer 'dst' (which must be already allocated) + * @return : the number of bytes written into buffer 'dst' (necessarily <= dstCapacity) + * or 0 if compression fails + * Note : This function is protected against buffer overflow scenarios (never writes outside 'dst' buffer, nor read outside 'source' buffer). + */ +LZ4LIB_API int LZ4_compress_default(const char* src, char* dst, int srcSize, int dstCapacity); + +/*! LZ4_decompress_safe() : + * @compressedSize : is the exact complete size of the compressed block. + * @dstCapacity : is the size of destination buffer (which must be already allocated), + * is an upper bound of decompressed size. + * @return : the number of bytes decompressed into destination buffer (necessarily <= dstCapacity) + * If destination buffer is not large enough, decoding will stop and output an error code (negative value). + * If the source stream is detected malformed, the function will stop decoding and return a negative result. + * Note 1 : This function is protected against malicious data packets : + * it will never writes outside 'dst' buffer, nor read outside 'source' buffer, + * even if the compressed block is maliciously modified to order the decoder to do these actions. + * In such case, the decoder stops immediately, and considers the compressed block malformed. + * Note 2 : compressedSize and dstCapacity must be provided to the function, the compressed block does not contain them. + * The implementation is free to send / store / derive this information in whichever way is most beneficial. + * If there is a need for a different format which bundles together both compressed data and its metadata, consider looking at lz4frame.h instead. + */ +LZ4LIB_API int LZ4_decompress_safe (const char* src, char* dst, int compressedSize, int dstCapacity); + + +/*-************************************ +* Advanced Functions +**************************************/ +#define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */ +#define LZ4_COMPRESSBOUND(isize) ((unsigned)(isize) > (unsigned)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16) + +/*! LZ4_compressBound() : + Provides the maximum size that LZ4 compression may output in a "worst case" scenario (input data not compressible) + This function is primarily useful for memory allocation purposes (destination buffer size). + Macro LZ4_COMPRESSBOUND() is also provided for compilation-time evaluation (stack memory allocation for example). + Note that LZ4_compress_default() compresses faster when dstCapacity is >= LZ4_compressBound(srcSize) + inputSize : max supported value is LZ4_MAX_INPUT_SIZE + return : maximum output size in a "worst case" scenario + or 0, if input size is incorrect (too large or negative) +*/ +LZ4LIB_API int LZ4_compressBound(int inputSize); + +/*! LZ4_compress_fast() : + Same as LZ4_compress_default(), but allows selection of "acceleration" factor. + The larger the acceleration value, the faster the algorithm, but also the lesser the compression. + It's a trade-off. It can be fine tuned, with each successive value providing roughly +~3% to speed. + An acceleration value of "1" is the same as regular LZ4_compress_default() + Values <= 0 will be replaced by LZ4_ACCELERATION_DEFAULT (currently == 1, see lz4.c). + Values > LZ4_ACCELERATION_MAX will be replaced by LZ4_ACCELERATION_MAX (currently == 65537, see lz4.c). +*/ +LZ4LIB_API int LZ4_compress_fast (const char* src, char* dst, int srcSize, int dstCapacity, int acceleration); + + +/*! LZ4_compress_fast_extState() : + * Same as LZ4_compress_fast(), using an externally allocated memory space for its state. + * Use LZ4_sizeofState() to know how much memory must be allocated, + * and allocate it on 8-bytes boundaries (using `malloc()` typically). + * Then, provide this buffer as `void* state` to compression function. + */ +LZ4LIB_API int LZ4_sizeofState(void); +LZ4LIB_API int LZ4_compress_fast_extState (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration); + + +/*! LZ4_compress_destSize() : + * Reverse the logic : compresses as much data as possible from 'src' buffer + * into already allocated buffer 'dst', of size >= 'targetDestSize'. + * This function either compresses the entire 'src' content into 'dst' if it's large enough, + * or fill 'dst' buffer completely with as much data as possible from 'src'. + * note: acceleration parameter is fixed to "default". + * + * *srcSizePtr : will be modified to indicate how many bytes where read from 'src' to fill 'dst'. + * New value is necessarily <= input value. + * @return : Nb bytes written into 'dst' (necessarily <= targetDestSize) + * or 0 if compression fails. + * + * Note : from v1.8.2 to v1.9.1, this function had a bug (fixed in v1.9.2+): + * the produced compressed content could, in specific circumstances, + * require to be decompressed into a destination buffer larger + * by at least 1 byte than the content to decompress. + * If an application uses `LZ4_compress_destSize()`, + * it's highly recommended to update liblz4 to v1.9.2 or better. + * If this can't be done or ensured, + * the receiving decompression function should provide + * a dstCapacity which is > decompressedSize, by at least 1 byte. + * See https://github.com/lz4/lz4/issues/859 for details + */ +LZ4LIB_API int LZ4_compress_destSize (const char* src, char* dst, int* srcSizePtr, int targetDstSize); + + +/*! LZ4_decompress_safe_partial() : + * Decompress an LZ4 compressed block, of size 'srcSize' at position 'src', + * into destination buffer 'dst' of size 'dstCapacity'. + * Up to 'targetOutputSize' bytes will be decoded. + * The function stops decoding on reaching this objective. + * This can be useful to boost performance + * whenever only the beginning of a block is required. + * + * @return : the number of bytes decoded in `dst` (necessarily <= targetOutputSize) + * If source stream is detected malformed, function returns a negative result. + * + * Note 1 : @return can be < targetOutputSize, if compressed block contains less data. + * + * Note 2 : targetOutputSize must be <= dstCapacity + * + * Note 3 : this function effectively stops decoding on reaching targetOutputSize, + * so dstCapacity is kind of redundant. + * This is because in older versions of this function, + * decoding operation would still write complete sequences. + * Therefore, there was no guarantee that it would stop writing at exactly targetOutputSize, + * it could write more bytes, though only up to dstCapacity. + * Some "margin" used to be required for this operation to work properly. + * Thankfully, this is no longer necessary. + * The function nonetheless keeps the same signature, in an effort to preserve API compatibility. + * + * Note 4 : If srcSize is the exact size of the block, + * then targetOutputSize can be any value, + * including larger than the block's decompressed size. + * The function will, at most, generate block's decompressed size. + * + * Note 5 : If srcSize is _larger_ than block's compressed size, + * then targetOutputSize **MUST** be <= block's decompressed size. + * Otherwise, *silent corruption will occur*. + */ +LZ4LIB_API int LZ4_decompress_safe_partial (const char* src, char* dst, int srcSize, int targetOutputSize, int dstCapacity); + + +/*-********************************************* +* Streaming Compression Functions +***********************************************/ +typedef union LZ4_stream_u LZ4_stream_t; /* incomplete type (defined later) */ + +/** + Note about RC_INVOKED + + - RC_INVOKED is predefined symbol of rc.exe (the resource compiler which is part of MSVC/Visual Studio). + https://docs.microsoft.com/en-us/windows/win32/menurc/predefined-macros + + - Since rc.exe is a legacy compiler, it truncates long symbol (> 30 chars) + and reports warning "RC4011: identifier truncated". + + - To eliminate the warning, we surround long preprocessor symbol with + "#if !defined(RC_INVOKED) ... #endif" block that means + "skip this block when rc.exe is trying to read it". +*/ +#if !defined(RC_INVOKED) /* https://docs.microsoft.com/en-us/windows/win32/menurc/predefined-macros */ +#if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) +LZ4LIB_API LZ4_stream_t* LZ4_createStream(void); +LZ4LIB_API int LZ4_freeStream (LZ4_stream_t* streamPtr); +#endif /* !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) */ +#endif + +/*! LZ4_resetStream_fast() : v1.9.0+ + * Use this to prepare an LZ4_stream_t for a new chain of dependent blocks + * (e.g., LZ4_compress_fast_continue()). + * + * An LZ4_stream_t must be initialized once before usage. + * This is automatically done when created by LZ4_createStream(). + * However, should the LZ4_stream_t be simply declared on stack (for example), + * it's necessary to initialize it first, using LZ4_initStream(). + * + * After init, start any new stream with LZ4_resetStream_fast(). + * A same LZ4_stream_t can be re-used multiple times consecutively + * and compress multiple streams, + * provided that it starts each new stream with LZ4_resetStream_fast(). + * + * LZ4_resetStream_fast() is much faster than LZ4_initStream(), + * but is not compatible with memory regions containing garbage data. + * + * Note: it's only useful to call LZ4_resetStream_fast() + * in the context of streaming compression. + * The *extState* functions perform their own resets. + * Invoking LZ4_resetStream_fast() before is redundant, and even counterproductive. + */ +LZ4LIB_API void LZ4_resetStream_fast (LZ4_stream_t* streamPtr); + +/*! LZ4_loadDict() : + * Use this function to reference a static dictionary into LZ4_stream_t. + * The dictionary must remain available during compression. + * LZ4_loadDict() triggers a reset, so any previous data will be forgotten. + * The same dictionary will have to be loaded on decompression side for successful decoding. + * Dictionary are useful for better compression of small data (KB range). + * While LZ4 accept any input as dictionary, + * results are generally better when using Zstandard's Dictionary Builder. + * Loading a size of 0 is allowed, and is the same as reset. + * @return : loaded dictionary size, in bytes (necessarily <= 64 KB) + */ +LZ4LIB_API int LZ4_loadDict (LZ4_stream_t* streamPtr, const char* dictionary, int dictSize); + +/*! LZ4_compress_fast_continue() : + * Compress 'src' content using data from previously compressed blocks, for better compression ratio. + * 'dst' buffer must be already allocated. + * If dstCapacity >= LZ4_compressBound(srcSize), compression is guaranteed to succeed, and runs faster. + * + * @return : size of compressed block + * or 0 if there is an error (typically, cannot fit into 'dst'). + * + * Note 1 : Each invocation to LZ4_compress_fast_continue() generates a new block. + * Each block has precise boundaries. + * Each block must be decompressed separately, calling LZ4_decompress_*() with relevant metadata. + * It's not possible to append blocks together and expect a single invocation of LZ4_decompress_*() to decompress them together. + * + * Note 2 : The previous 64KB of source data is __assumed__ to remain present, unmodified, at same address in memory ! + * + * Note 3 : When input is structured as a double-buffer, each buffer can have any size, including < 64 KB. + * Make sure that buffers are separated, by at least one byte. + * This construction ensures that each block only depends on previous block. + * + * Note 4 : If input buffer is a ring-buffer, it can have any size, including < 64 KB. + * + * Note 5 : After an error, the stream status is undefined (invalid), it can only be reset or freed. + */ +LZ4LIB_API int LZ4_compress_fast_continue (LZ4_stream_t* streamPtr, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration); + +/*! LZ4_saveDict() : + * If last 64KB data cannot be guaranteed to remain available at its current memory location, + * save it into a safer place (char* safeBuffer). + * This is schematically equivalent to a memcpy() followed by LZ4_loadDict(), + * but is much faster, because LZ4_saveDict() doesn't need to rebuild tables. + * @return : saved dictionary size in bytes (necessarily <= maxDictSize), or 0 if error. + */ +LZ4LIB_API int LZ4_saveDict (LZ4_stream_t* streamPtr, char* safeBuffer, int maxDictSize); + + +/*-********************************************** +* Streaming Decompression Functions +* Bufferless synchronous API +************************************************/ +typedef union LZ4_streamDecode_u LZ4_streamDecode_t; /* tracking context */ + +/*! LZ4_createStreamDecode() and LZ4_freeStreamDecode() : + * creation / destruction of streaming decompression tracking context. + * A tracking context can be re-used multiple times. + */ +#if !defined(RC_INVOKED) /* https://docs.microsoft.com/en-us/windows/win32/menurc/predefined-macros */ +#if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) +LZ4LIB_API LZ4_streamDecode_t* LZ4_createStreamDecode(void); +LZ4LIB_API int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream); +#endif /* !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) */ +#endif + +/*! LZ4_setStreamDecode() : + * An LZ4_streamDecode_t context can be allocated once and re-used multiple times. + * Use this function to start decompression of a new stream of blocks. + * A dictionary can optionally be set. Use NULL or size 0 for a reset order. + * Dictionary is presumed stable : it must remain accessible and unmodified during next decompression. + * @return : 1 if OK, 0 if error + */ +LZ4LIB_API int LZ4_setStreamDecode (LZ4_streamDecode_t* LZ4_streamDecode, const char* dictionary, int dictSize); + +/*! LZ4_decoderRingBufferSize() : v1.8.2+ + * Note : in a ring buffer scenario (optional), + * blocks are presumed decompressed next to each other + * up to the moment there is not enough remaining space for next block (remainingSize < maxBlockSize), + * at which stage it resumes from beginning of ring buffer. + * When setting such a ring buffer for streaming decompression, + * provides the minimum size of this ring buffer + * to be compatible with any source respecting maxBlockSize condition. + * @return : minimum ring buffer size, + * or 0 if there is an error (invalid maxBlockSize). + */ +LZ4LIB_API int LZ4_decoderRingBufferSize(int maxBlockSize); +#define LZ4_DECODER_RING_BUFFER_SIZE(maxBlockSize) (65536 + 14 + (maxBlockSize)) /* for static allocation; maxBlockSize presumed valid */ + +/*! LZ4_decompress_safe_continue() : + * This decoding function allows decompression of consecutive blocks in "streaming" mode. + * The difference with the usual independent blocks is that + * new blocks are allowed to find references into former blocks. + * A block is an unsplittable entity, and must be presented entirely to the decompression function. + * LZ4_decompress_safe_continue() only accepts one block at a time. + * It's modeled after `LZ4_decompress_safe()` and behaves similarly. + * + * @LZ4_streamDecode : decompression state, tracking the position in memory of past data + * @compressedSize : exact complete size of one compressed block. + * @dstCapacity : size of destination buffer (which must be already allocated), + * must be an upper bound of decompressed size. + * @return : number of bytes decompressed into destination buffer (necessarily <= dstCapacity) + * If destination buffer is not large enough, decoding will stop and output an error code (negative value). + * If the source stream is detected malformed, the function will stop decoding and return a negative result. + * + * The last 64KB of previously decoded data *must* remain available and unmodified + * at the memory position where they were previously decoded. + * If less than 64KB of data has been decoded, all the data must be present. + * + * Special : if decompression side sets a ring buffer, it must respect one of the following conditions : + * - Decompression buffer size is _at least_ LZ4_decoderRingBufferSize(maxBlockSize). + * maxBlockSize is the maximum size of any single block. It can have any value > 16 bytes. + * In which case, encoding and decoding buffers do not need to be synchronized. + * Actually, data can be produced by any source compliant with LZ4 format specification, and respecting maxBlockSize. + * - Synchronized mode : + * Decompression buffer size is _exactly_ the same as compression buffer size, + * and follows exactly same update rule (block boundaries at same positions), + * and decoding function is provided with exact decompressed size of each block (exception for last block of the stream), + * _then_ decoding & encoding ring buffer can have any size, including small ones ( < 64 KB). + * - Decompression buffer is larger than encoding buffer, by a minimum of maxBlockSize more bytes. + * In which case, encoding and decoding buffers do not need to be synchronized, + * and encoding ring buffer can have any size, including small ones ( < 64 KB). + * + * Whenever these conditions are not possible, + * save the last 64KB of decoded data into a safe buffer where it can't be modified during decompression, + * then indicate where this data is saved using LZ4_setStreamDecode(), before decompressing next block. +*/ +LZ4LIB_API int +LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, + const char* src, char* dst, + int srcSize, int dstCapacity); + + +/*! LZ4_decompress_safe_usingDict() : + * Works the same as + * a combination of LZ4_setStreamDecode() followed by LZ4_decompress_safe_continue() + * However, it's stateless: it doesn't need any LZ4_streamDecode_t state. + * Dictionary is presumed stable : it must remain accessible and unmodified during decompression. + * Performance tip : Decompression speed can be substantially increased + * when dst == dictStart + dictSize. + */ +LZ4LIB_API int +LZ4_decompress_safe_usingDict(const char* src, char* dst, + int srcSize, int dstCapacity, + const char* dictStart, int dictSize); + +/*! LZ4_decompress_safe_partial_usingDict() : + * Behaves the same as LZ4_decompress_safe_partial() + * with the added ability to specify a memory segment for past data. + * Performance tip : Decompression speed can be substantially increased + * when dst == dictStart + dictSize. + */ +LZ4LIB_API int +LZ4_decompress_safe_partial_usingDict(const char* src, char* dst, + int compressedSize, + int targetOutputSize, int maxOutputSize, + const char* dictStart, int dictSize); + +#endif /* LZ4_H_2983827168210 */ + + +/*^************************************* + * !!!!!! STATIC LINKING ONLY !!!!!! + ***************************************/ + +/*-**************************************************************************** + * Experimental section + * + * Symbols declared in this section must be considered unstable. Their + * signatures or semantics may change, or they may be removed altogether in the + * future. They are therefore only safe to depend on when the caller is + * statically linked against the library. + * + * To protect against unsafe usage, not only are the declarations guarded, + * the definitions are hidden by default + * when building LZ4 as a shared/dynamic library. + * + * In order to access these declarations, + * define LZ4_STATIC_LINKING_ONLY in your application + * before including LZ4's headers. + * + * In order to make their implementations accessible dynamically, you must + * define LZ4_PUBLISH_STATIC_FUNCTIONS when building the LZ4 library. + ******************************************************************************/ + +#ifdef LZ4_STATIC_LINKING_ONLY + +#ifndef LZ4_STATIC_3504398509 +#define LZ4_STATIC_3504398509 + +#ifdef LZ4_PUBLISH_STATIC_FUNCTIONS +#define LZ4LIB_STATIC_API LZ4LIB_API +#else +#define LZ4LIB_STATIC_API +#endif + + +/*! LZ4_compress_fast_extState_fastReset() : + * A variant of LZ4_compress_fast_extState(). + * + * Using this variant avoids an expensive initialization step. + * It is only safe to call if the state buffer is known to be correctly initialized already + * (see above comment on LZ4_resetStream_fast() for a definition of "correctly initialized"). + * From a high level, the difference is that + * this function initializes the provided state with a call to something like LZ4_resetStream_fast() + * while LZ4_compress_fast_extState() starts with a call to LZ4_resetStream(). + */ +LZ4LIB_STATIC_API int LZ4_compress_fast_extState_fastReset (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration); + +/*! LZ4_attach_dictionary() : + * This is an experimental API that allows + * efficient use of a static dictionary many times. + * + * Rather than re-loading the dictionary buffer into a working context before + * each compression, or copying a pre-loaded dictionary's LZ4_stream_t into a + * working LZ4_stream_t, this function introduces a no-copy setup mechanism, + * in which the working stream references the dictionary stream in-place. + * + * Several assumptions are made about the state of the dictionary stream. + * Currently, only streams which have been prepared by LZ4_loadDict() should + * be expected to work. + * + * Alternatively, the provided dictionaryStream may be NULL, + * in which case any existing dictionary stream is unset. + * + * If a dictionary is provided, it replaces any preexisting stream history. + * The dictionary contents are the only history that can be referenced and + * logically immediately precede the data compressed in the first subsequent + * compression call. + * + * The dictionary will only remain attached to the working stream through the + * first compression call, at the end of which it is cleared. The dictionary + * stream (and source buffer) must remain in-place / accessible / unchanged + * through the completion of the first compression call on the stream. + */ +LZ4LIB_STATIC_API void +LZ4_attach_dictionary(LZ4_stream_t* workingStream, + const LZ4_stream_t* dictionaryStream); + + +/*! In-place compression and decompression + * + * It's possible to have input and output sharing the same buffer, + * for highly constrained memory environments. + * In both cases, it requires input to lay at the end of the buffer, + * and decompression to start at beginning of the buffer. + * Buffer size must feature some margin, hence be larger than final size. + * + * |<------------------------buffer--------------------------------->| + * |<-----------compressed data--------->| + * |<-----------decompressed size------------------>| + * |<----margin---->| + * + * This technique is more useful for decompression, + * since decompressed size is typically larger, + * and margin is short. + * + * In-place decompression will work inside any buffer + * which size is >= LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize). + * This presumes that decompressedSize > compressedSize. + * Otherwise, it means compression actually expanded data, + * and it would be more efficient to store such data with a flag indicating it's not compressed. + * This can happen when data is not compressible (already compressed, or encrypted). + * + * For in-place compression, margin is larger, as it must be able to cope with both + * history preservation, requiring input data to remain unmodified up to LZ4_DISTANCE_MAX, + * and data expansion, which can happen when input is not compressible. + * As a consequence, buffer size requirements are much higher, + * and memory savings offered by in-place compression are more limited. + * + * There are ways to limit this cost for compression : + * - Reduce history size, by modifying LZ4_DISTANCE_MAX. + * Note that it is a compile-time constant, so all compressions will apply this limit. + * Lower values will reduce compression ratio, except when input_size < LZ4_DISTANCE_MAX, + * so it's a reasonable trick when inputs are known to be small. + * - Require the compressor to deliver a "maximum compressed size". + * This is the `dstCapacity` parameter in `LZ4_compress*()`. + * When this size is < LZ4_COMPRESSBOUND(inputSize), then compression can fail, + * in which case, the return code will be 0 (zero). + * The caller must be ready for these cases to happen, + * and typically design a backup scheme to send data uncompressed. + * The combination of both techniques can significantly reduce + * the amount of margin required for in-place compression. + * + * In-place compression can work in any buffer + * which size is >= (maxCompressedSize) + * with maxCompressedSize == LZ4_COMPRESSBOUND(srcSize) for guaranteed compression success. + * LZ4_COMPRESS_INPLACE_BUFFER_SIZE() depends on both maxCompressedSize and LZ4_DISTANCE_MAX, + * so it's possible to reduce memory requirements by playing with them. + */ + +#define LZ4_DECOMPRESS_INPLACE_MARGIN(compressedSize) (((compressedSize) >> 8) + 32) +#define LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize) ((decompressedSize) + LZ4_DECOMPRESS_INPLACE_MARGIN(decompressedSize)) /**< note: presumes that compressedSize < decompressedSize. note2: margin is overestimated a bit, since it could use compressedSize instead */ + +#ifndef LZ4_DISTANCE_MAX /* history window size; can be user-defined at compile time */ +# define LZ4_DISTANCE_MAX 65535 /* set to maximum value by default */ +#endif + +#define LZ4_COMPRESS_INPLACE_MARGIN (LZ4_DISTANCE_MAX + 32) /* LZ4_DISTANCE_MAX can be safely replaced by srcSize when it's smaller */ +#define LZ4_COMPRESS_INPLACE_BUFFER_SIZE(maxCompressedSize) ((maxCompressedSize) + LZ4_COMPRESS_INPLACE_MARGIN) /**< maxCompressedSize is generally LZ4_COMPRESSBOUND(inputSize), but can be set to any lower value, with the risk that compression can fail (return code 0(zero)) */ + +#endif /* LZ4_STATIC_3504398509 */ +#endif /* LZ4_STATIC_LINKING_ONLY */ + + + +#ifndef LZ4_H_98237428734687 +#define LZ4_H_98237428734687 + +/*-************************************************************ + * Private Definitions + ************************************************************** + * Do not use these definitions directly. + * They are only exposed to allow static allocation of `LZ4_stream_t` and `LZ4_streamDecode_t`. + * Accessing members will expose user code to API and/or ABI break in future versions of the library. + **************************************************************/ +#define LZ4_HASHLOG (LZ4_MEMORY_USAGE-2) +#define LZ4_HASHTABLESIZE (1 << LZ4_MEMORY_USAGE) +#define LZ4_HASH_SIZE_U32 (1 << LZ4_HASHLOG) /* required as macro for static allocation */ + +#if defined(__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) +# include + typedef int8_t LZ4_i8; + typedef uint8_t LZ4_byte; + typedef uint16_t LZ4_u16; + typedef uint32_t LZ4_u32; +#else + typedef signed char LZ4_i8; + typedef unsigned char LZ4_byte; + typedef unsigned short LZ4_u16; + typedef unsigned int LZ4_u32; +#endif + +/*! LZ4_stream_t : + * Never ever use below internal definitions directly ! + * These definitions are not API/ABI safe, and may change in future versions. + * If you need static allocation, declare or allocate an LZ4_stream_t object. +**/ + +typedef struct LZ4_stream_t_internal LZ4_stream_t_internal; +struct LZ4_stream_t_internal { + LZ4_u32 hashTable[LZ4_HASH_SIZE_U32]; + const LZ4_byte* dictionary; + const LZ4_stream_t_internal* dictCtx; + LZ4_u32 currentOffset; + LZ4_u32 tableType; + LZ4_u32 dictSize; + /* Implicit padding to ensure structure is aligned */ +}; + +#define LZ4_STREAM_MINSIZE ((1UL << LZ4_MEMORY_USAGE) + 32) /* static size, for inter-version compatibility */ +union LZ4_stream_u { + char minStateSize[LZ4_STREAM_MINSIZE]; + LZ4_stream_t_internal internal_donotuse; +}; /* previously typedef'd to LZ4_stream_t */ + + +/*! LZ4_initStream() : v1.9.0+ + * An LZ4_stream_t structure must be initialized at least once. + * This is automatically done when invoking LZ4_createStream(), + * but it's not when the structure is simply declared on stack (for example). + * + * Use LZ4_initStream() to properly initialize a newly declared LZ4_stream_t. + * It can also initialize any arbitrary buffer of sufficient size, + * and will @return a pointer of proper type upon initialization. + * + * Note : initialization fails if size and alignment conditions are not respected. + * In which case, the function will @return NULL. + * Note2: An LZ4_stream_t structure guarantees correct alignment and size. + * Note3: Before v1.9.0, use LZ4_resetStream() instead +**/ +LZ4LIB_API LZ4_stream_t* LZ4_initStream (void* buffer, size_t size); + + +/*! LZ4_streamDecode_t : + * Never ever use below internal definitions directly ! + * These definitions are not API/ABI safe, and may change in future versions. + * If you need static allocation, declare or allocate an LZ4_streamDecode_t object. +**/ +typedef struct { + const LZ4_byte* externalDict; + const LZ4_byte* prefixEnd; + size_t extDictSize; + size_t prefixSize; +} LZ4_streamDecode_t_internal; + +#define LZ4_STREAMDECODE_MINSIZE 32 +union LZ4_streamDecode_u { + char minStateSize[LZ4_STREAMDECODE_MINSIZE]; + LZ4_streamDecode_t_internal internal_donotuse; +} ; /* previously typedef'd to LZ4_streamDecode_t */ + + + +/*-************************************ +* Obsolete Functions +**************************************/ + +/*! Deprecation warnings + * + * Deprecated functions make the compiler generate a warning when invoked. + * This is meant to invite users to update their source code. + * Should deprecation warnings be a problem, it is generally possible to disable them, + * typically with -Wno-deprecated-declarations for gcc + * or _CRT_SECURE_NO_WARNINGS in Visual. + * + * Another method is to define LZ4_DISABLE_DEPRECATE_WARNINGS + * before including the header file. + */ +#ifdef LZ4_DISABLE_DEPRECATE_WARNINGS +# define LZ4_DEPRECATED(message) /* disable deprecation warnings */ +#else +# if defined (__cplusplus) && (__cplusplus >= 201402) /* C++14 or greater */ +# define LZ4_DEPRECATED(message) [[deprecated(message)]] +# elif defined(_MSC_VER) +# define LZ4_DEPRECATED(message) __declspec(deprecated(message)) +# elif defined(__clang__) || (defined(__GNUC__) && (__GNUC__ * 10 + __GNUC_MINOR__ >= 45)) +# define LZ4_DEPRECATED(message) __attribute__((deprecated(message))) +# elif defined(__GNUC__) && (__GNUC__ * 10 + __GNUC_MINOR__ >= 31) +# define LZ4_DEPRECATED(message) __attribute__((deprecated)) +# else +# pragma message("WARNING: LZ4_DEPRECATED needs custom implementation for this compiler") +# define LZ4_DEPRECATED(message) /* disabled */ +# endif +#endif /* LZ4_DISABLE_DEPRECATE_WARNINGS */ + +/*! Obsolete compression functions (since v1.7.3) */ +LZ4_DEPRECATED("use LZ4_compress_default() instead") LZ4LIB_API int LZ4_compress (const char* src, char* dest, int srcSize); +LZ4_DEPRECATED("use LZ4_compress_default() instead") LZ4LIB_API int LZ4_compress_limitedOutput (const char* src, char* dest, int srcSize, int maxOutputSize); +LZ4_DEPRECATED("use LZ4_compress_fast_extState() instead") LZ4LIB_API int LZ4_compress_withState (void* state, const char* source, char* dest, int inputSize); +LZ4_DEPRECATED("use LZ4_compress_fast_extState() instead") LZ4LIB_API int LZ4_compress_limitedOutput_withState (void* state, const char* source, char* dest, int inputSize, int maxOutputSize); +LZ4_DEPRECATED("use LZ4_compress_fast_continue() instead") LZ4LIB_API int LZ4_compress_continue (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize); +LZ4_DEPRECATED("use LZ4_compress_fast_continue() instead") LZ4LIB_API int LZ4_compress_limitedOutput_continue (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize, int maxOutputSize); + +/*! Obsolete decompression functions (since v1.8.0) */ +LZ4_DEPRECATED("use LZ4_decompress_fast() instead") LZ4LIB_API int LZ4_uncompress (const char* source, char* dest, int outputSize); +LZ4_DEPRECATED("use LZ4_decompress_safe() instead") LZ4LIB_API int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize); + +/* Obsolete streaming functions (since v1.7.0) + * degraded functionality; do not use! + * + * In order to perform streaming compression, these functions depended on data + * that is no longer tracked in the state. They have been preserved as well as + * possible: using them will still produce a correct output. However, they don't + * actually retain any history between compression calls. The compression ratio + * achieved will therefore be no better than compressing each chunk + * independently. + */ +LZ4_DEPRECATED("Use LZ4_createStream() instead") LZ4LIB_API void* LZ4_create (char* inputBuffer); +LZ4_DEPRECATED("Use LZ4_createStream() instead") LZ4LIB_API int LZ4_sizeofStreamState(void); +LZ4_DEPRECATED("Use LZ4_resetStream() instead") LZ4LIB_API int LZ4_resetStreamState(void* state, char* inputBuffer); +LZ4_DEPRECATED("Use LZ4_saveDict() instead") LZ4LIB_API char* LZ4_slideInputBuffer (void* state); + +/*! Obsolete streaming decoding functions (since v1.7.0) */ +LZ4_DEPRECATED("use LZ4_decompress_safe_usingDict() instead") LZ4LIB_API int LZ4_decompress_safe_withPrefix64k (const char* src, char* dst, int compressedSize, int maxDstSize); +LZ4_DEPRECATED("use LZ4_decompress_fast_usingDict() instead") LZ4LIB_API int LZ4_decompress_fast_withPrefix64k (const char* src, char* dst, int originalSize); + +/*! Obsolete LZ4_decompress_fast variants (since v1.9.0) : + * These functions used to be faster than LZ4_decompress_safe(), + * but this is no longer the case. They are now slower. + * This is because LZ4_decompress_fast() doesn't know the input size, + * and therefore must progress more cautiously into the input buffer to not read beyond the end of block. + * On top of that `LZ4_decompress_fast()` is not protected vs malformed or malicious inputs, making it a security liability. + * As a consequence, LZ4_decompress_fast() is strongly discouraged, and deprecated. + * + * The last remaining LZ4_decompress_fast() specificity is that + * it can decompress a block without knowing its compressed size. + * Such functionality can be achieved in a more secure manner + * by employing LZ4_decompress_safe_partial(). + * + * Parameters: + * originalSize : is the uncompressed size to regenerate. + * `dst` must be already allocated, its size must be >= 'originalSize' bytes. + * @return : number of bytes read from source buffer (== compressed size). + * The function expects to finish at block's end exactly. + * If the source stream is detected malformed, the function stops decoding and returns a negative result. + * note : LZ4_decompress_fast*() requires originalSize. Thanks to this information, it never writes past the output buffer. + * However, since it doesn't know its 'src' size, it may read an unknown amount of input, past input buffer bounds. + * Also, since match offsets are not validated, match reads from 'src' may underflow too. + * These issues never happen if input (compressed) data is correct. + * But they may happen if input data is invalid (error or intentional tampering). + * As a consequence, use these functions in trusted environments with trusted data **only**. + */ +LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe() instead") +LZ4LIB_API int LZ4_decompress_fast (const char* src, char* dst, int originalSize); +LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_continue() instead") +LZ4LIB_API int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* src, char* dst, int originalSize); +LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_usingDict() instead") +LZ4LIB_API int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize, const char* dictStart, int dictSize); + +/*! LZ4_resetStream() : + * An LZ4_stream_t structure must be initialized at least once. + * This is done with LZ4_initStream(), or LZ4_resetStream(). + * Consider switching to LZ4_initStream(), + * invoking LZ4_resetStream() will trigger deprecation warnings in the future. + */ +LZ4LIB_API void LZ4_resetStream (LZ4_stream_t* streamPtr); + + +#endif /* LZ4_H_98237428734687 */ + + +#if defined (__cplusplus) +} +#endif + +#endif /* LV_USE_LZ4_INTERNAL */ + diff --git a/include/liblvgl/libs/qrcode/lv_qrcode.h b/include/liblvgl/libs/qrcode/lv_qrcode.h new file mode 100644 index 00000000..ba9b0e86 --- /dev/null +++ b/include/liblvgl/libs/qrcode/lv_qrcode.h @@ -0,0 +1,85 @@ +/** + * @file lv_qrcode.h + * + */ + +#ifndef LV_QRCODE_H +#define LV_QRCODE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../lv_conf_internal.h" +#include "../../misc/lv_color.h" +#include "../../misc/lv_types.h" +#include "../../widgets/canvas/lv_canvas.h" +#include LV_STDBOOL_INCLUDE +#include LV_STDINT_INCLUDE +#if LV_USE_QRCODE + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_qrcode_class; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create an empty QR code (an `lv_canvas`) object. + * @param parent point to an object where to create the QR code + * @return pointer to the created QR code object + */ +lv_obj_t * lv_qrcode_create(lv_obj_t * parent); + +/** + * Set QR code size. + * @param obj pointer to a QR code object + * @param size width and height of the QR code + */ +void lv_qrcode_set_size(lv_obj_t * obj, int32_t size); + +/** + * Set QR code dark color. + * @param obj pointer to a QR code object + * @param color dark color of the QR code + */ +void lv_qrcode_set_dark_color(lv_obj_t * obj, lv_color_t color); + +/** + * Set QR code light color. + * @param obj pointer to a QR code object + * @param color light color of the QR code + */ +void lv_qrcode_set_light_color(lv_obj_t * obj, lv_color_t color); + +/** + * Set the data of a QR code object + * @param obj pointer to a QR code object + * @param data data to display + * @param data_len length of data in bytes + * @return LV_RESULT_OK: if no error; LV_RESULT_INVALID: on error + */ +lv_result_t lv_qrcode_update(lv_obj_t * obj, const void * data, uint32_t data_len); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_QRCODE*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /*LV_QRCODE_H*/ diff --git a/include/liblvgl/libs/qrcode/lv_qrcode_private.h b/include/liblvgl/libs/qrcode/lv_qrcode_private.h new file mode 100644 index 00000000..bcd2c8c5 --- /dev/null +++ b/include/liblvgl/libs/qrcode/lv_qrcode_private.h @@ -0,0 +1,52 @@ +/** + * @file lv_qrcode_private.h + * + */ + +#ifndef LV_QRCODE_PRIVATE_H +#define LV_QRCODE_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../widgets/canvas/lv_canvas_private.h" +#include "lv_qrcode.h" + +#if LV_USE_QRCODE + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/*Data of qrcode*/ +struct _lv_qrcode_t { + lv_canvas_t canvas; + lv_color_t dark_color; + lv_color_t light_color; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_QRCODE */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_QRCODE_PRIVATE_H*/ diff --git a/include/liblvgl/extra/libs/qrcode/qrcodegen.h b/include/liblvgl/libs/qrcode/qrcodegen.h similarity index 82% rename from include/liblvgl/extra/libs/qrcode/qrcodegen.h rename to include/liblvgl/libs/qrcode/qrcodegen.h index b484e917..0c7fd115 100644 --- a/include/liblvgl/extra/libs/qrcode/qrcodegen.h +++ b/include/liblvgl/libs/qrcode/qrcodegen.h @@ -23,6 +23,9 @@ #pragma once +#include "liblvgl/lvgl.h" +#ifdef LV_USE_QRCODE + #include #include #include @@ -55,12 +58,12 @@ extern "C" { * The error correction level in a QR Code symbol. */ enum qrcodegen_Ecc { - // Must be declared in ascending order of error protection - // so that an internal qrcodegen function works properly - qrcodegen_Ecc_LOW = 0 , // The QR Code can tolerate about 7% erroneous codewords - qrcodegen_Ecc_MEDIUM , // The QR Code can tolerate about 15% erroneous codewords - qrcodegen_Ecc_QUARTILE, // The QR Code can tolerate about 25% erroneous codewords - qrcodegen_Ecc_HIGH , // The QR Code can tolerate about 30% erroneous codewords + // Must be declared in ascending order of error protection + // so that an internal qrcodegen function works properly + qrcodegen_Ecc_LOW = 0, // The QR Code can tolerate about 7% erroneous codewords + qrcodegen_Ecc_MEDIUM, // The QR Code can tolerate about 15% erroneous codewords + qrcodegen_Ecc_QUARTILE, // The QR Code can tolerate about 25% erroneous codewords + qrcodegen_Ecc_HIGH, // The QR Code can tolerate about 30% erroneous codewords }; @@ -68,18 +71,18 @@ enum qrcodegen_Ecc { * The mask pattern used in a QR Code symbol. */ enum qrcodegen_Mask { - // A special value to tell the QR Code encoder to - // automatically select an appropriate mask pattern - qrcodegen_Mask_AUTO = -1, - // The eight actual mask patterns - qrcodegen_Mask_0 = 0, - qrcodegen_Mask_1, - qrcodegen_Mask_2, - qrcodegen_Mask_3, - qrcodegen_Mask_4, - qrcodegen_Mask_5, - qrcodegen_Mask_6, - qrcodegen_Mask_7, + // A special value to tell the QR Code encoder to + // automatically select an appropriate mask pattern + qrcodegen_Mask_AUTO = -1, + // The eight actual mask patterns + qrcodegen_Mask_0 = 0, + qrcodegen_Mask_1, + qrcodegen_Mask_2, + qrcodegen_Mask_3, + qrcodegen_Mask_4, + qrcodegen_Mask_5, + qrcodegen_Mask_6, + qrcodegen_Mask_7, }; @@ -87,11 +90,11 @@ enum qrcodegen_Mask { * Describes how a segment's data bits are interpreted. */ enum qrcodegen_Mode { - qrcodegen_Mode_NUMERIC = 0x1, - qrcodegen_Mode_ALPHANUMERIC = 0x2, - qrcodegen_Mode_BYTE = 0x4, - qrcodegen_Mode_KANJI = 0x8, - qrcodegen_Mode_ECI = 0x7, + qrcodegen_Mode_NUMERIC = 0x1, + qrcodegen_Mode_ALPHANUMERIC = 0x2, + qrcodegen_Mode_BYTE = 0x4, + qrcodegen_Mode_KANJI = 0x8, + qrcodegen_Mode_ECI = 0x7, }; @@ -107,22 +110,22 @@ enum qrcodegen_Mode { * the largest QR Code (version 40) has 31329 modules. */ struct qrcodegen_Segment { - // The mode indicator of this segment. - enum qrcodegen_Mode mode; - - // The length of this segment's unencoded data. Measured in characters for - // numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode. - // Always zero or positive. Not the same as the data's bit length. - int numChars; - - // The data bits of this segment, packed in bitwise big endian. - // Can be null if the bit length is zero. - uint8_t *data; - - // The number of valid data bits used in the buffer. Requires - // 0 <= bitLength <= 32767, and bitLength <= (capacity of data array) * 8. - // The character count (numChars) must agree with the mode and the bit buffer length. - int bitLength; + // The mode indicator of this segment. + enum qrcodegen_Mode mode; + + // The length of this segment's unencoded data. Measured in characters for + // numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode. + // Always zero or positive. Not the same as the data's bit length. + int numChars; + + // The data bits of this segment, packed in bitwise big endian. + // Can be null if the bit length is zero. + uint8_t * data; + + // The number of valid data bits used in the buffer. Requires + // 0 <= bitLength <= 32767, and bitLength <= (capacity of data array) * 8. + // The character count (numChars) must agree with the mode and the bit buffer length. + int bitLength; }; @@ -166,8 +169,8 @@ struct qrcodegen_Segment { * - Please consult the QR Code specification for information on * data capacities per version, ECC level, and text encoding mode. */ -bool qrcodegen_encodeText(const char *text, uint8_t tempBuffer[], uint8_t qrcode[], - enum qrcodegen_Ecc ecl, int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl); +bool qrcodegen_encodeText(const char * text, uint8_t tempBuffer[], uint8_t qrcode[], + enum qrcodegen_Ecc ecl, int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl); /* @@ -189,7 +192,7 @@ bool qrcodegen_encodeText(const char *text, uint8_t tempBuffer[], uint8_t qrcode * data capacities per version, ECC level, and text encoding mode. */ bool qrcodegen_encodeBinary(uint8_t dataAndTemp[], size_t dataLen, uint8_t qrcode[], - enum qrcodegen_Ecc ecl, int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl); + enum qrcodegen_Ecc ecl, int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl); /*---- Functions (low level) to generate QR Codes ----*/ @@ -207,7 +210,7 @@ bool qrcodegen_encodeBinary(uint8_t dataAndTemp[], size_t dataLen, uint8_t qrcod * But the qrcode array must not overlap tempBuffer or any segment's data buffer. */ bool qrcodegen_encodeSegments(const struct qrcodegen_Segment segs[], size_t len, - enum qrcodegen_Ecc ecl, uint8_t tempBuffer[], uint8_t qrcode[]); + enum qrcodegen_Ecc ecl, uint8_t tempBuffer[], uint8_t qrcode[]); /* @@ -226,7 +229,7 @@ bool qrcodegen_encodeSegments(const struct qrcodegen_Segment segs[], size_t len, * But the qrcode array must not overlap tempBuffer or any segment's data buffer. */ bool qrcodegen_encodeSegmentsAdvanced(const struct qrcodegen_Segment segs[], size_t len, enum qrcodegen_Ecc ecl, - int minVersion, int maxVersion, int mask, bool boostEcl, uint8_t tempBuffer[], uint8_t qrcode[]); + int minVersion, int maxVersion, int mask, bool boostEcl, uint8_t tempBuffer[], uint8_t qrcode[]); /* @@ -234,14 +237,14 @@ bool qrcodegen_encodeSegmentsAdvanced(const struct qrcodegen_Segment segs[], siz * A string is encodable iff each character is in the following set: 0 to 9, A to Z * (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon. */ -bool qrcodegen_isAlphanumeric(const char *text); +bool qrcodegen_isAlphanumeric(const char * text); /* * Tests whether the given string can be encoded as a segment in numeric mode. * A string is encodable iff each character is in the range 0 to 9. */ -bool qrcodegen_isNumeric(const char *text); +bool qrcodegen_isNumeric(const char * text); /* @@ -269,7 +272,7 @@ struct qrcodegen_Segment qrcodegen_makeBytes(const uint8_t data[], size_t len, u /* * Returns a segment representing the given string of decimal digits encoded in numeric mode. */ -struct qrcodegen_Segment qrcodegen_makeNumeric(const char *digits, uint8_t buf[]); +struct qrcodegen_Segment qrcodegen_makeNumeric(const char * digits, uint8_t buf[]); /* @@ -277,7 +280,7 @@ struct qrcodegen_Segment qrcodegen_makeNumeric(const char *digits, uint8_t buf[] * The characters allowed are: 0 to 9, A to Z (uppercase only), space, * dollar, percent, asterisk, plus, hyphen, period, slash, colon. */ -struct qrcodegen_Segment qrcodegen_makeAlphanumeric(const char *text, uint8_t buf[]); +struct qrcodegen_Segment qrcodegen_makeAlphanumeric(const char * text, uint8_t buf[]); /* @@ -317,3 +320,5 @@ int qrcodegen_getMinFitVersion(enum qrcodegen_Ecc ecl, size_t dataLen); #ifdef __cplusplus } #endif + +#endif diff --git a/include/liblvgl/libs/rle/lv_rle.h b/include/liblvgl/libs/rle/lv_rle.h new file mode 100644 index 00000000..02ab6a34 --- /dev/null +++ b/include/liblvgl/libs/rle/lv_rle.h @@ -0,0 +1,46 @@ +/** + * @file lv_rle.h + * + */ + +#ifndef LV_RLE_H +#define LV_RLE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../lv_conf_internal.h" + +#if LV_USE_RLE + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +uint32_t lv_rle_decompress(const uint8_t * input, + uint32_t input_buff_len, uint8_t * output, + uint32_t output_buff_len, uint8_t blk_size); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_RLE*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /*LV_RLE_H*/ diff --git a/include/liblvgl/extra/libs/rlottie/lv_rlottie.h b/include/liblvgl/libs/rlottie/lv_rlottie.h similarity index 59% rename from include/liblvgl/extra/libs/rlottie/lv_rlottie.h rename to include/liblvgl/libs/rlottie/lv_rlottie.h index a8aca7bc..44bcf2ef 100644 --- a/include/liblvgl/extra/libs/rlottie/lv_rlottie.h +++ b/include/liblvgl/libs/rlottie/lv_rlottie.h @@ -13,7 +13,7 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lvgl.h" +#include "../../lv_conf_internal.h" #if LV_USE_RLOTTIE /********************* @@ -31,32 +31,15 @@ typedef enum { LV_RLOTTIE_CTRL_LOOP = 8, } lv_rlottie_ctrl_t; -/** definition in lottieanimation_capi.c */ -struct Lottie_Animation_S; -typedef struct { - lv_img_t img_ext; - struct Lottie_Animation_S * animation; - lv_timer_t * task; - lv_img_dsc_t imgdsc; - size_t total_frames; - size_t current_frame; - size_t framerate; - uint32_t * allocated_buf; - size_t allocated_buffer_size; - size_t scanline_width; - lv_rlottie_ctrl_t play_ctrl; - size_t dest_frame; -} lv_rlottie_t; - -extern const lv_obj_class_t lv_rlottie_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_rlottie_class; /********************** * GLOBAL PROTOTYPES **********************/ -lv_obj_t * lv_rlottie_create_from_file(lv_obj_t * parent, lv_coord_t width, lv_coord_t height, const char * path); +lv_obj_t * lv_rlottie_create_from_file(lv_obj_t * parent, int32_t width, int32_t height, const char * path); -lv_obj_t * lv_rlottie_create_from_raw(lv_obj_t * parent, lv_coord_t width, lv_coord_t height, +lv_obj_t * lv_rlottie_create_from_raw(lv_obj_t * parent, int32_t width, int32_t height, const char * rlottie_desc); void lv_rlottie_set_play_mode(lv_obj_t * rlottie, const lv_rlottie_ctrl_t ctrl); diff --git a/include/liblvgl/libs/rlottie/lv_rlottie_private.h b/include/liblvgl/libs/rlottie/lv_rlottie_private.h new file mode 100644 index 00000000..344c3909 --- /dev/null +++ b/include/liblvgl/libs/rlottie/lv_rlottie_private.h @@ -0,0 +1,61 @@ +/** + * @file lv_rlottie_private.h + * + */ + +#ifndef LV_RLOTTIE_PRIVATE_H +#define LV_RLOTTIE_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_rlottie.h" +#if LV_USE_RLOTTIE +#include "../../widgets/image/lv_image_private.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** definition in lottieanimation_capi.c */ +struct Lottie_Animation_S; + +struct _lv_rlottie_t { + lv_image_t img_ext; + struct Lottie_Animation_S * animation; + lv_timer_t * task; + lv_image_dsc_t imgdsc; + size_t total_frames; + size_t current_frame; + size_t framerate; + uint32_t * allocated_buf; + size_t allocated_buffer_size; + size_t scanline_width; + lv_rlottie_ctrl_t play_ctrl; + size_t dest_frame; +}; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_RLOTTIE*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_RLOTTIE_PRIVATE_H*/ diff --git a/include/liblvgl/libs/svg/lv_svg.h b/include/liblvgl/libs/svg/lv_svg.h new file mode 100644 index 00000000..b5a8965c --- /dev/null +++ b/include/liblvgl/libs/svg/lv_svg.h @@ -0,0 +1,334 @@ +/** + * @file lv_svg.h + * + */ + +#ifndef LV_SVG_H +#define LV_SVG_H + +/********************* + * INCLUDES + *********************/ +#include "../../lv_conf_internal.h" +#if LV_USE_SVG + +#include "../../misc/lv_array.h" +#include "../../misc/lv_tree.h" +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ +enum { + LV_SVG_TAG_INVALID = -1, + LV_SVG_TAG_CONTENT, + LV_SVG_TAG_SVG, + LV_SVG_TAG_USE, + LV_SVG_TAG_G, + LV_SVG_TAG_PATH, + LV_SVG_TAG_RECT, + LV_SVG_TAG_CIRCLE, + LV_SVG_TAG_ELLIPSE, + LV_SVG_TAG_LINE, + LV_SVG_TAG_POLYLINE, + LV_SVG_TAG_POLYGON, + LV_SVG_TAG_SOLID_COLOR, + LV_SVG_TAG_LINEAR_GRADIENT, + LV_SVG_TAG_RADIAL_GRADIENT, + LV_SVG_TAG_STOP, + LV_SVG_TAG_DEFS, + LV_SVG_TAG_IMAGE, +#if LV_USE_SVG_ANIMATION + LV_SVG_TAG_MPATH, + LV_SVG_TAG_SET, + LV_SVG_TAG_ANIMATE, + LV_SVG_TAG_ANIMATE_COLOR, + LV_SVG_TAG_ANIMATE_TRANSFORM, + LV_SVG_TAG_ANIMATE_MOTION, +#endif + LV_SVG_TAG_TEXT, + LV_SVG_TAG_TSPAN, + LV_SVG_TAG_TEXT_AREA, +}; +typedef int8_t lv_svg_tag_t; + +enum { + LV_SVG_ATTR_INVALID = 0, + LV_SVG_ATTR_ID, + LV_SVG_ATTR_XML_ID, + LV_SVG_ATTR_VERSION, + LV_SVG_ATTR_BASE_PROFILE, + LV_SVG_ATTR_VIEWBOX, + LV_SVG_ATTR_PRESERVE_ASPECT_RATIO, + LV_SVG_ATTR_VIEWPORT_FILL, + LV_SVG_ATTR_VIEWPORT_FILL_OPACITY, + LV_SVG_ATTR_DISPLAY, + LV_SVG_ATTR_VISIBILITY, + LV_SVG_ATTR_X, + LV_SVG_ATTR_Y, + LV_SVG_ATTR_WIDTH, + LV_SVG_ATTR_HEIGHT, + LV_SVG_ATTR_RX, + LV_SVG_ATTR_RY, + LV_SVG_ATTR_CX, + LV_SVG_ATTR_CY, + LV_SVG_ATTR_R, + LV_SVG_ATTR_X1, + LV_SVG_ATTR_Y1, + LV_SVG_ATTR_X2, + LV_SVG_ATTR_Y2, + LV_SVG_ATTR_POINTS, + LV_SVG_ATTR_D, + LV_SVG_ATTR_PATH_LENGTH, + LV_SVG_ATTR_XLINK_HREF, + LV_SVG_ATTR_FILL, + LV_SVG_ATTR_FILL_RULE, + LV_SVG_ATTR_FILL_OPACITY, + LV_SVG_ATTR_STROKE, + LV_SVG_ATTR_STROKE_WIDTH, + LV_SVG_ATTR_STROKE_LINECAP, + LV_SVG_ATTR_STROKE_LINEJOIN, + LV_SVG_ATTR_STROKE_MITER_LIMIT, + LV_SVG_ATTR_STROKE_DASH_ARRAY, + LV_SVG_ATTR_STROKE_DASH_OFFSET, + LV_SVG_ATTR_STROKE_OPACITY, + LV_SVG_ATTR_OPACITY, + LV_SVG_ATTR_SOLID_COLOR, + LV_SVG_ATTR_SOLID_OPACITY, + LV_SVG_ATTR_GRADIENT_UNITS, + LV_SVG_ATTR_GRADIENT_STOP_OFFSET, + LV_SVG_ATTR_GRADIENT_STOP_COLOR, + LV_SVG_ATTR_GRADIENT_STOP_OPACITY, + LV_SVG_ATTR_FONT_FAMILY, + LV_SVG_ATTR_FONT_STYLE, + LV_SVG_ATTR_FONT_VARIANT, + LV_SVG_ATTR_FONT_WEIGHT, + LV_SVG_ATTR_FONT_SIZE, + LV_SVG_ATTR_TRANSFORM, + LV_SVG_ATTR_TEXT_ANCHOR, +#if LV_USE_SVG_ANIMATION + LV_SVG_ATTR_ATTRIBUTE_NAME, + LV_SVG_ATTR_ATTRIBUTE_TYPE, + LV_SVG_ATTR_BEGIN, + LV_SVG_ATTR_END, + LV_SVG_ATTR_DUR, + LV_SVG_ATTR_MIN, + LV_SVG_ATTR_MAX, + LV_SVG_ATTR_RESTART, + LV_SVG_ATTR_REPEAT_COUNT, + LV_SVG_ATTR_REPEAT_DUR, + LV_SVG_ATTR_CALC_MODE, + LV_SVG_ATTR_VALUES, + LV_SVG_ATTR_KEY_TIMES, + LV_SVG_ATTR_KEY_SPLINES, + LV_SVG_ATTR_KEY_POINTS, + LV_SVG_ATTR_FROM, + LV_SVG_ATTR_TO, + LV_SVG_ATTR_BY, + LV_SVG_ATTR_ADDITIVE, + LV_SVG_ATTR_ACCUMULATE, + LV_SVG_ATTR_PATH, + LV_SVG_ATTR_ROTATE, + LV_SVG_ATTR_TRANSFORM_TYPE, +#endif +}; +typedef uint8_t lv_svg_attr_type_t; + +enum { + LV_SVG_TRANSFORM_TYPE_MATRIX = 1, + LV_SVG_TRANSFORM_TYPE_TRANSLATE, + LV_SVG_TRANSFORM_TYPE_ROTATE, + LV_SVG_TRANSFORM_TYPE_SCALE, + LV_SVG_TRANSFORM_TYPE_SKEW_X, + LV_SVG_TRANSFORM_TYPE_SKEW_Y, +}; +typedef uint8_t lv_svg_transform_type_t; + +#if LV_USE_SVG_ANIMATION +enum { + LV_SVG_ANIM_REMOVE = 0, + LV_SVG_ANIM_FREEZE, +}; + +enum { + LV_SVG_ANIM_RESTART_ALWAYS = 0, + LV_SVG_ANIM_RESTART_WHEN_NOT_ACTIVE, + LV_SVG_ANIM_RESTART_NEVER, +}; + +enum { + LV_SVG_ANIM_CALC_MODE_LINEAR = 0, + LV_SVG_ANIM_CALC_MODE_PACED, + LV_SVG_ANIM_CALC_MODE_SPLINE, + LV_SVG_ANIM_CALC_MODE_DISCRETE, +}; + +enum { + LV_SVG_ANIM_ADDITIVE_REPLACE = 0, + LV_SVG_ANIM_ADDITIVE_SUM, +}; + +enum { + LV_SVG_ANIM_ACCUMULATE_NONE = 0, + LV_SVG_ANIM_ACCUMULATE_SUM, +}; +#endif + +enum { + LV_SVG_ASPECT_RATIO_NONE = 0, + LV_SVG_ASPECT_RATIO_XMIN_YMIN = (1 << 1), + LV_SVG_ASPECT_RATIO_XMID_YMIN = (2 << 1), + LV_SVG_ASPECT_RATIO_XMAX_YMIN = (3 << 1), + LV_SVG_ASPECT_RATIO_XMIN_YMID = (4 << 1), + LV_SVG_ASPECT_RATIO_XMID_YMID = (5 << 1), + LV_SVG_ASPECT_RATIO_XMAX_YMID = (6 << 1), + LV_SVG_ASPECT_RATIO_XMIN_YMAX = (7 << 1), + LV_SVG_ASPECT_RATIO_XMID_YMAX = (8 << 1), + LV_SVG_ASPECT_RATIO_XMAX_YMAX = (9 << 1), +}; + +enum { + LV_SVG_ASPECT_RATIO_OPT_MEET = 0, + LV_SVG_ASPECT_RATIO_OPT_SLICE, +}; +typedef uint32_t lv_svg_aspect_ratio_t; + +typedef struct { + float x; + float y; +} lv_svg_point_t; + +typedef struct { + float m[3][3]; +} lv_svg_matrix_t; + +typedef uint32_t lv_svg_color_t; + +enum { + LV_SVG_FILL_NONZERO = 0, + LV_SVG_FILL_EVENODD, +}; +typedef uint8_t lv_svg_fill_rule_t; + +enum { + LV_SVG_LINE_CAP_BUTT = 0, + LV_SVG_LINE_CAP_SQUARE, + LV_SVG_LINE_CAP_ROUND, +}; +typedef uint8_t lv_svg_line_cap_t; + +enum { + LV_SVG_LINE_JOIN_MITER = 0, + LV_SVG_LINE_JOIN_BEVEL, + LV_SVG_LINE_JOIN_ROUND, +}; +typedef uint8_t lv_svg_line_join_t; + +enum { + LV_SVG_GRADIENT_UNITS_OBJECT = 0, + LV_SVG_GRADIENT_UNITS_USER_SPACE, +}; +typedef uint8_t lv_svg_gradient_units_t; + +typedef union { + int32_t ival; + uint32_t uval; + float fval; + char * sval; + void * val; +} lv_svg_attr_value_t; + +/* + * to simplify list buffer management, allocate enough memory for all data and length. + * | size | data[0] | data[1] | data[2] | ... | + */ +typedef struct { + uint32_t length; + uint8_t data[1]; +} lv_svg_attr_values_list_t; + +/* https://www.w3.org/TR/SVGTiny12/svgudomidl.html */ +enum { + LV_SVG_PATH_CMD_MOVE_TO = 77, + LV_SVG_PATH_CMD_LINE_TO = 76, + LV_SVG_PATH_CMD_CURVE_TO = 67, + LV_SVG_PATH_CMD_QUAD_TO = 81, + LV_SVG_PATH_CMD_CLOSE = 90, +}; + +/* + * to simplify list buffer management, allocate enough memory for all path data and cmd. + * | cmd | data[0] | data[1] | data[2] | ... | + */ +typedef struct { + uint32_t cmd; + uint8_t data[1]; +} lv_svg_attr_path_value_t; + +enum { + LV_SVG_ATTR_VALUE_DATA = 0, + LV_SVG_ATTR_VALUE_PTR, +}; +typedef uint8_t lv_svg_attr_value_type_t; + +enum { + LV_SVG_ATTR_VALUE_NONE = 0, + LV_SVG_ATTR_VALUE_INITIAL, + LV_SVG_ATTR_VALUE_INHERIT, +}; +typedef uint8_t lv_svg_attr_value_class_t; + +typedef struct { + lv_svg_attr_type_t id; + lv_svg_attr_value_type_t val_type; + lv_svg_attr_value_class_t class_type; + lv_svg_attr_value_t value; +} lv_svg_attr_t; + +struct _lv_svg_render_obj; + +typedef struct { + lv_tree_node_t base; + char * xml_id; /* xml_id or content */ + lv_svg_tag_t type; + lv_array_t attrs; + struct _lv_svg_render_obj * render_obj; +} lv_svg_node_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * @brief Loading SVG data and creating the DOM tree + * @param svg_data pointer to the SVG data + * @param data_len the SVG data length + */ +lv_svg_node_t * lv_svg_load_data(const char * svg_data, uint32_t data_len); + +/** + * @brief Create an SVG DOM node + * @param parent pointer to the parent node + * @return true: an new SVG DOM node, false: NULL + */ +lv_svg_node_t * lv_svg_node_create(lv_svg_node_t * parent); + +/** + * @brief Delete an SVG DOM subtree + * @param node pointer to an SVG DOM subtree + */ +void lv_svg_node_delete(lv_svg_node_t * node); + +/********************** + * MACROS + **********************/ +#define LV_SVG_NODE_CHILD(n, i) \ + ((lv_svg_node_t *)(LV_TREE_NODE((n))->children[i])) + +#define LV_SVG_NODE(n) ((lv_svg_node_t*)(n)) + +#endif /*LV_USE_SVG*/ + +#endif /*LV_SVG_H*/ diff --git a/include/liblvgl/libs/svg/lv_svg_parser.h b/include/liblvgl/libs/svg/lv_svg_parser.h new file mode 100644 index 00000000..fc99fe8b --- /dev/null +++ b/include/liblvgl/libs/svg/lv_svg_parser.h @@ -0,0 +1,84 @@ +/** + * @file lv_svg_parser.h + * + */ + +#ifndef LV_SVG_PARSER_H +#define LV_SVG_PARSER_H + +/********************* + * INCLUDES + *********************/ +#include "../../lv_conf_internal.h" +#if LV_USE_SVG + +#include "lv_svg.h" +#include "lv_svg_token.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef enum { + LV_SVG_PARSER_PROCESS = 0, + LV_SVG_PARSER_IGNORE, +} _lv_svg_parser_state_t; + +typedef struct { + uint16_t state; + char * ignore_name; + uint32_t ignore_len; + int32_t dpi; + lv_svg_node_t * doc_root; + lv_svg_node_t * cur_node; +} _lv_svg_parser_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * @brief Initialize the SVG parser + * @param parser pointer to a parser object + */ +void _lv_svg_parser_init(_lv_svg_parser_t * parser); + +/** + * @brief Deinitialize the SVG parser + * @param parser pointer to a parser object + */ +void _lv_svg_parser_deinit(_lv_svg_parser_t * parser); + +/** + * @brief Parse an SVG document + * @param parser pointer to a parser object + * @param token pointer to a token object + * @return true: the parsing is finished, false: the parsing is not finished yet. + */ +bool _lv_svg_parser_token(_lv_svg_parser_t * parser, const _lv_svg_token_t * token); + +/** + * @brief Check if the parsing is finished + * @param parser pointer to a parser object + * @return true: the parsing is finished, false: the parsing is not finished yet. + */ +bool _lv_svg_parser_is_finish(_lv_svg_parser_t * parser); + +/** + * @brief Dump the SVG tree + * @param root pointer to the root of the SVG tree + * @param depth the depth of the current node in the tree + */ +void _lv_svg_dump_tree(lv_svg_node_t * root, int depth); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_SVG*/ + +#endif /*LV_SVG_PARSER_H*/ diff --git a/include/liblvgl/libs/svg/lv_svg_render.h b/include/liblvgl/libs/svg/lv_svg_render.h new file mode 100644 index 00000000..13530548 --- /dev/null +++ b/include/liblvgl/libs/svg/lv_svg_render.h @@ -0,0 +1,98 @@ +/** + * @file lv_svg_render.h + * + */ + +#ifndef LV_SVG_RENDER_H +#define LV_SVG_RENDER_H + +/********************* + * INCLUDES + *********************/ +#include "../../lv_conf_internal.h" + +#if LV_USE_SVG && LV_USE_VECTOR_GRAPHIC +#include "lv_svg.h" +#include "../../misc/lv_types.h" +#include "../../draw/lv_draw_vector_private.h" + +/********************* + * DEFINES + *********************/ + +#define LV_SVG_RENDER_OBJ(n) ((lv_svg_render_obj_t*)(n)) + +/********************** + * TYPEDEFS + **********************/ + +typedef struct _lv_svg_render_obj { + struct _lv_svg_render_obj * next; + uint32_t flags; + char * id; + lv_vector_draw_dsc_t dsc; + lv_matrix_t matrix; + + /* for url(XXX) reference */ + struct _lv_svg_render_obj * head; + char * fill_ref; + char * stroke_ref; + void (*set_paint_ref)(struct _lv_svg_render_obj * obj, lv_vector_draw_dsc_t * dsc, + const struct _lv_svg_render_obj * target_obj, bool fill); + + void (*init)(struct _lv_svg_render_obj * obj, const lv_svg_node_t * node); + void (*render)(const struct _lv_svg_render_obj * obj, lv_vector_dsc_t * dsc, const lv_matrix_t * matrix); + void (*set_attr)(struct _lv_svg_render_obj * obj, lv_vector_draw_dsc_t * dsc, const lv_svg_attr_t * attr); + void (*get_bounds)(const struct _lv_svg_render_obj * obj, lv_area_t * area); + void (*destroy)(struct _lv_svg_render_obj * obj); +} lv_svg_render_obj_t; + +typedef struct _lv_svg_render_hal { + void (*load_image)(const char * image_url, lv_draw_image_dsc_t * img_dsc); + const char * (*get_font_path)(const char * font_family); +} lv_svg_render_hal_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * @brief Initialize the SVG render + * @param hal pointer to a structure with rendering functions + */ +void lv_svg_render_init(const lv_svg_render_hal_t * hal); + +/** + * @brief Create a new SVG render from an SVG document + * @param svg_doc pointer to the SVG document + * @return pointer to the new SVG render object + */ +lv_svg_render_obj_t * lv_svg_render_create(const lv_svg_node_t * svg_doc); + +/** + * @brief Delete an SVG render object + * @param render pointer to the SVG render object to delete + */ +void lv_svg_render_delete(lv_svg_render_obj_t * render); + +/** + * @brief Render an SVG object to a vector graphics + * @param dsc pointer to the vector graphics descriptor + * @param render pointer to the SVG render object to render + */ +void lv_draw_svg_render(lv_vector_dsc_t * dsc, const lv_svg_render_obj_t * render); + +/** + * @brief Draw an SVG document to a layer + * @param layer pointer to the target layer + * @param svg_doc pointer to the SVG document to draw + */ +void lv_draw_svg(lv_layer_t * layer, const lv_svg_node_t * svg_doc); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_SVG*/ + +#endif /*LV_SVG_RENDER_H*/ diff --git a/include/liblvgl/libs/svg/lv_svg_token.h b/include/liblvgl/libs/svg/lv_svg_token.h new file mode 100644 index 00000000..1c57b91e --- /dev/null +++ b/include/liblvgl/libs/svg/lv_svg_token.h @@ -0,0 +1,70 @@ +/** + * @file lv_svg_token.h + * + */ + +#ifndef LV_SVG_TOKEN_H +#define LV_SVG_TOKEN_H + +/********************* + * INCLUDES + *********************/ +#include "../../lv_conf_internal.h" +#if LV_USE_SVG + +#include "../../misc/lv_array.h" + +/********************* + * DEFINES + *********************/ + +#define SVG_TOKEN_LEN(t) ((t)->end - (t)->start) + +/********************** + * TYPEDEFS + **********************/ + +typedef enum { + LV_SVG_TOKEN_BEGIN = 0, + LV_SVG_TOKEN_END, + LV_SVG_TOKEN_CONTENT, +} _lv_svg_token_type_t; + +typedef struct { + const char * name_start; + const char * name_end; + const char * value_start; + const char * value_end; +} _lv_svg_token_attr_t; + +typedef struct { + const char * start; + const char * end; + _lv_svg_token_type_t type; + bool flat; + _lv_svg_token_attr_t * cur_attr; + lv_array_t attrs; +} _lv_svg_token_t; + +typedef bool (*svg_token_process)(_lv_svg_token_t * token, void * user_data); + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * @brief Parse SVG data and call a callback for each token + * @param svg_data pointer to SVG data + * @param len length of the SVG data + * @param cb callback function to be called for each token + * @param user_data custom data to be passed to the callback function + * @return true: SVG data successfully parsed, false: error occurred + */ +bool _lv_svg_tokenizer(const char * svg_data, uint32_t len, svg_token_process cb, void * user_data); + +/********************** + * MACROS + **********************/ +#endif /*LV_USE_SVG*/ + +#endif /*LV_SVG_TOKEN_H*/ diff --git a/include/liblvgl/libs/thorvg/add_lvgl_if.sh b/include/liblvgl/libs/thorvg/add_lvgl_if.sh new file mode 100644 index 00000000..43ce9030 --- /dev/null +++ b/include/liblvgl/libs/thorvg/add_lvgl_if.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +#Add LVGL #if LV_USE_THORVG_INTERNAL guard +#Usage +# find -name "*.cpp" | xargs ./add_lvgl_if.sh +# find -name "t*.h" | xargs ./add_lvgl_if.sh + +sed '0,/\*\/$/ {/\*\/$/ {n; s|^|\n#include "../../lv_conf_internal.h"\n#if LV_USE_THORVG_INTERNAL\n|}}' $@ -i + +sed -i -e '$a\ +\ +#endif /* LV_USE_THORVG_INTERNAL */\ +' $@ -i diff --git a/include/liblvgl/libs/thorvg/config.h b/include/liblvgl/libs/thorvg/config.h new file mode 100644 index 00000000..baa258a4 --- /dev/null +++ b/include/liblvgl/libs/thorvg/config.h @@ -0,0 +1,15 @@ +/* + * Autogenerated by the Meson build system. + * Do not edit, your changes will be lost. + */ + +#pragma once + +#define THORVG_SW_RASTER_SUPPORT 1 + +#define THORVG_SVG_LOADER_SUPPORT LV_USE_LOTTIE + +#define THORVG_LOTTIE_LOADER_SUPPORT LV_USE_LOTTIE + +#define THORVG_VERSION_STRING "0.13.5" + diff --git a/include/liblvgl/libs/thorvg/rapidjson/allocators.h b/include/liblvgl/libs/thorvg/rapidjson/allocators.h new file mode 100644 index 00000000..a943cf19 --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/allocators.h @@ -0,0 +1,693 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ALLOCATORS_H_ +#define RAPIDJSON_ALLOCATORS_H_ + +#include "rapidjson.h" +#include "internal/meta.h" + +#include +#include + +#if RAPIDJSON_HAS_CXX11 +#include +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// Allocator + +/*! \class rapidjson::Allocator + \brief Concept for allocating, resizing and freeing memory block. + + Note that Malloc() and Realloc() are non-static but Free() is static. + + So if an allocator need to support Free(), it needs to put its pointer in + the header of memory block. + +\code +concept Allocator { + static const bool kNeedFree; //!< Whether this allocator needs to call Free(). + + // Allocate a memory block. + // \param size of the memory block in bytes. + // \returns pointer to the memory block. + void* Malloc(size_t size); + + // Resize a memory block. + // \param originalPtr The pointer to current memory block. Null pointer is permitted. + // \param originalSize The current size in bytes. (Design issue: since some allocator may not book-keep this, explicitly pass to it can save memory.) + // \param newSize the new size in bytes. + void* Realloc(void* originalPtr, size_t originalSize, size_t newSize); + + // Free a memory block. + // \param pointer to the memory block. Null pointer is permitted. + static void Free(void *ptr); +}; +\endcode +*/ + + +/*! \def RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY + \ingroup RAPIDJSON_CONFIG + \brief User-defined kDefaultChunkCapacity definition. + + User can define this as any \c size that is a power of 2. +*/ + +#ifndef RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY +#define RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY (64 * 1024) +#endif + + +/////////////////////////////////////////////////////////////////////////////// +// CrtAllocator + +//! C-runtime library allocator. +/*! This class is just wrapper for standard C library memory routines. + \note implements Allocator concept +*/ +class CrtAllocator { +public: + static const bool kNeedFree = true; + void* Malloc(size_t size) { + if (size) // behavior of malloc(0) is implementation defined. + return RAPIDJSON_MALLOC(size); + else + return NULL; // standardize to returning NULL. + } + void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { + (void)originalSize; + if (newSize == 0) { + RAPIDJSON_FREE(originalPtr); + return NULL; + } + return RAPIDJSON_REALLOC(originalPtr, newSize); + } + static void Free(void *ptr) RAPIDJSON_NOEXCEPT { RAPIDJSON_FREE(ptr); } + + bool operator==(const CrtAllocator&) const RAPIDJSON_NOEXCEPT { + return true; + } + bool operator!=(const CrtAllocator&) const RAPIDJSON_NOEXCEPT { + return false; + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// MemoryPoolAllocator + +//! Default memory allocator used by the parser and DOM. +/*! This allocator allocate memory blocks from pre-allocated memory chunks. + + It does not free memory blocks. And Realloc() only allocate new memory. + + The memory chunks are allocated by BaseAllocator, which is CrtAllocator by default. + + User may also supply a buffer as the first chunk. + + If the user-buffer is full then additional chunks are allocated by BaseAllocator. + + The user-buffer is not deallocated by this allocator. + + \tparam BaseAllocator the allocator type for allocating memory chunks. Default is CrtAllocator. + \note implements Allocator concept +*/ +template +class MemoryPoolAllocator { + //! Chunk header for perpending to each chunk. + /*! Chunks are stored as a singly linked list. + */ + struct ChunkHeader { + size_t capacity; //!< Capacity of the chunk in bytes (excluding the header itself). + size_t size; //!< Current size of allocated memory in bytes. + ChunkHeader *next; //!< Next chunk in the linked list. + }; + + struct SharedData { + ChunkHeader *chunkHead; //!< Head of the chunk linked-list. Only the head chunk serves allocation. + BaseAllocator* ownBaseAllocator; //!< base allocator created by this object. + size_t refcount; + bool ownBuffer; + }; + + static const size_t SIZEOF_SHARED_DATA = RAPIDJSON_ALIGN(sizeof(SharedData)); + static const size_t SIZEOF_CHUNK_HEADER = RAPIDJSON_ALIGN(sizeof(ChunkHeader)); + + static inline ChunkHeader *GetChunkHead(SharedData *shared) + { + return reinterpret_cast(reinterpret_cast(shared) + SIZEOF_SHARED_DATA); + } + static inline uint8_t *GetChunkBuffer(SharedData *shared) + { + return reinterpret_cast(shared->chunkHead) + SIZEOF_CHUNK_HEADER; + } + + static const size_t kDefaultChunkCapacity = RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY; //!< Default chunk capacity. + +public: + static const bool kNeedFree = false; //!< Tell users that no need to call Free() with this allocator. (concept Allocator) + static const bool kRefCounted = true; //!< Tell users that this allocator is reference counted on copy + + //! Constructor with chunkSize. + /*! \param chunkSize The size of memory chunk. The default is kDefaultChunkSize. + \param baseAllocator The allocator for allocating memory chunks. + */ + explicit + MemoryPoolAllocator(size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) : + chunk_capacity_(chunkSize), + baseAllocator_(baseAllocator ? baseAllocator : RAPIDJSON_NEW(BaseAllocator)()), + shared_(static_cast(baseAllocator_ ? baseAllocator_->Malloc(SIZEOF_SHARED_DATA + SIZEOF_CHUNK_HEADER) : 0)) + { + RAPIDJSON_ASSERT(baseAllocator_ != 0); + RAPIDJSON_ASSERT(shared_ != 0); + if (baseAllocator) { + shared_->ownBaseAllocator = 0; + } + else { + shared_->ownBaseAllocator = baseAllocator_; + } + shared_->chunkHead = GetChunkHead(shared_); + shared_->chunkHead->capacity = 0; + shared_->chunkHead->size = 0; + shared_->chunkHead->next = 0; + shared_->ownBuffer = true; + shared_->refcount = 1; + } + + //! Constructor with user-supplied buffer. + /*! The user buffer will be used firstly. When it is full, memory pool allocates new chunk with chunk size. + + The user buffer will not be deallocated when this allocator is destructed. + + \param buffer User supplied buffer. + \param size Size of the buffer in bytes. It must at least larger than sizeof(ChunkHeader). + \param chunkSize The size of memory chunk. The default is kDefaultChunkSize. + \param baseAllocator The allocator for allocating memory chunks. + */ + MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) : + chunk_capacity_(chunkSize), + baseAllocator_(baseAllocator), + shared_(static_cast(AlignBuffer(buffer, size))) + { + RAPIDJSON_ASSERT(size >= SIZEOF_SHARED_DATA + SIZEOF_CHUNK_HEADER); + shared_->chunkHead = GetChunkHead(shared_); + shared_->chunkHead->capacity = size - SIZEOF_SHARED_DATA - SIZEOF_CHUNK_HEADER; + shared_->chunkHead->size = 0; + shared_->chunkHead->next = 0; + shared_->ownBaseAllocator = 0; + shared_->ownBuffer = false; + shared_->refcount = 1; + } + + MemoryPoolAllocator(const MemoryPoolAllocator& rhs) RAPIDJSON_NOEXCEPT : + chunk_capacity_(rhs.chunk_capacity_), + baseAllocator_(rhs.baseAllocator_), + shared_(rhs.shared_) + { + RAPIDJSON_NOEXCEPT_ASSERT(shared_->refcount > 0); + ++shared_->refcount; + } + MemoryPoolAllocator& operator=(const MemoryPoolAllocator& rhs) RAPIDJSON_NOEXCEPT + { + RAPIDJSON_NOEXCEPT_ASSERT(rhs.shared_->refcount > 0); + ++rhs.shared_->refcount; + this->~MemoryPoolAllocator(); + baseAllocator_ = rhs.baseAllocator_; + chunk_capacity_ = rhs.chunk_capacity_; + shared_ = rhs.shared_; + return *this; + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + MemoryPoolAllocator(MemoryPoolAllocator&& rhs) RAPIDJSON_NOEXCEPT : + chunk_capacity_(rhs.chunk_capacity_), + baseAllocator_(rhs.baseAllocator_), + shared_(rhs.shared_) + { + RAPIDJSON_NOEXCEPT_ASSERT(rhs.shared_->refcount > 0); + rhs.shared_ = 0; + } + MemoryPoolAllocator& operator=(MemoryPoolAllocator&& rhs) RAPIDJSON_NOEXCEPT + { + RAPIDJSON_NOEXCEPT_ASSERT(rhs.shared_->refcount > 0); + this->~MemoryPoolAllocator(); + baseAllocator_ = rhs.baseAllocator_; + chunk_capacity_ = rhs.chunk_capacity_; + shared_ = rhs.shared_; + rhs.shared_ = 0; + return *this; + } +#endif + + //! Destructor. + /*! This deallocates all memory chunks, excluding the user-supplied buffer. + */ + ~MemoryPoolAllocator() RAPIDJSON_NOEXCEPT { + if (!shared_) { + // do nothing if moved + return; + } + if (shared_->refcount > 1) { + --shared_->refcount; + return; + } + Clear(); + BaseAllocator *a = shared_->ownBaseAllocator; + if (shared_->ownBuffer) { + baseAllocator_->Free(shared_); + } + RAPIDJSON_DELETE(a); + } + + //! Deallocates all memory chunks, excluding the first/user one. + void Clear() RAPIDJSON_NOEXCEPT { + RAPIDJSON_NOEXCEPT_ASSERT(shared_->refcount > 0); + for (;;) { + ChunkHeader* c = shared_->chunkHead; + if (!c->next) { + break; + } + shared_->chunkHead = c->next; + baseAllocator_->Free(c); + } + shared_->chunkHead->size = 0; + } + + //! Computes the total capacity of allocated memory chunks. + /*! \return total capacity in bytes. + */ + size_t Capacity() const RAPIDJSON_NOEXCEPT { + RAPIDJSON_NOEXCEPT_ASSERT(shared_->refcount > 0); + size_t capacity = 0; + for (ChunkHeader* c = shared_->chunkHead; c != 0; c = c->next) + capacity += c->capacity; + return capacity; + } + + //! Computes the memory blocks allocated. + /*! \return total used bytes. + */ + size_t Size() const RAPIDJSON_NOEXCEPT { + RAPIDJSON_NOEXCEPT_ASSERT(shared_->refcount > 0); + size_t size = 0; + for (ChunkHeader* c = shared_->chunkHead; c != 0; c = c->next) + size += c->size; + return size; + } + + //! Whether the allocator is shared. + /*! \return true or false. + */ + bool Shared() const RAPIDJSON_NOEXCEPT { + RAPIDJSON_NOEXCEPT_ASSERT(shared_->refcount > 0); + return shared_->refcount > 1; + } + + //! Allocates a memory block. (concept Allocator) + void* Malloc(size_t size) { + RAPIDJSON_NOEXCEPT_ASSERT(shared_->refcount > 0); + if (!size) + return NULL; + + size = RAPIDJSON_ALIGN(size); + if (RAPIDJSON_UNLIKELY(shared_->chunkHead->size + size > shared_->chunkHead->capacity)) + if (!AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size)) + return NULL; + + void *buffer = GetChunkBuffer(shared_) + shared_->chunkHead->size; + shared_->chunkHead->size += size; + return buffer; + } + + //! Resizes a memory block (concept Allocator) + void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { + if (originalPtr == 0) + return Malloc(newSize); + + RAPIDJSON_NOEXCEPT_ASSERT(shared_->refcount > 0); + if (newSize == 0) + return NULL; + + originalSize = RAPIDJSON_ALIGN(originalSize); + newSize = RAPIDJSON_ALIGN(newSize); + + // Do not shrink if new size is smaller than original + if (originalSize >= newSize) + return originalPtr; + + // Simply expand it if it is the last allocation and there is sufficient space + if (originalPtr == GetChunkBuffer(shared_) + shared_->chunkHead->size - originalSize) { + size_t increment = static_cast(newSize - originalSize); + if (shared_->chunkHead->size + increment <= shared_->chunkHead->capacity) { + shared_->chunkHead->size += increment; + return originalPtr; + } + } + + // Realloc process: allocate and copy memory, do not free original buffer. + if (void* newBuffer = Malloc(newSize)) { + if (originalSize) + std::memcpy(newBuffer, originalPtr, originalSize); + return newBuffer; + } + else + return NULL; + } + + //! Frees a memory block (concept Allocator) + static void Free(void *ptr) RAPIDJSON_NOEXCEPT { (void)ptr; } // Do nothing + + //! Compare (equality) with another MemoryPoolAllocator + bool operator==(const MemoryPoolAllocator& rhs) const RAPIDJSON_NOEXCEPT { + RAPIDJSON_NOEXCEPT_ASSERT(shared_->refcount > 0); + RAPIDJSON_NOEXCEPT_ASSERT(rhs.shared_->refcount > 0); + return shared_ == rhs.shared_; + } + //! Compare (inequality) with another MemoryPoolAllocator + bool operator!=(const MemoryPoolAllocator& rhs) const RAPIDJSON_NOEXCEPT { + return !operator==(rhs); + } + +private: + //! Creates a new chunk. + /*! \param capacity Capacity of the chunk in bytes. + \return true if success. + */ + bool AddChunk(size_t capacity) { + if (!baseAllocator_) + shared_->ownBaseAllocator = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator)(); + if (ChunkHeader* chunk = static_cast(baseAllocator_->Malloc(SIZEOF_CHUNK_HEADER + capacity))) { + chunk->capacity = capacity; + chunk->size = 0; + chunk->next = shared_->chunkHead; + shared_->chunkHead = chunk; + return true; + } + else + return false; + } + + static inline void* AlignBuffer(void* buf, size_t &size) + { + RAPIDJSON_NOEXCEPT_ASSERT(buf != 0); + const uintptr_t mask = sizeof(void*) - 1; + const uintptr_t ubuf = reinterpret_cast(buf); + if (RAPIDJSON_UNLIKELY(ubuf & mask)) { + const uintptr_t abuf = (ubuf + mask) & ~mask; + RAPIDJSON_ASSERT(size >= abuf - ubuf); + buf = reinterpret_cast(abuf); + size -= abuf - ubuf; + } + return buf; + } + + size_t chunk_capacity_; //!< The minimum capacity of chunk when they are allocated. + BaseAllocator* baseAllocator_; //!< base allocator for allocating memory chunks. + SharedData *shared_; //!< The shared data of the allocator +}; + +namespace internal { + template + struct IsRefCounted : + public FalseType + { }; + template + struct IsRefCounted::Type> : + public TrueType + { }; +} + +template +inline T* Realloc(A& a, T* old_p, size_t old_n, size_t new_n) +{ + RAPIDJSON_NOEXCEPT_ASSERT(old_n <= (std::numeric_limits::max)() / sizeof(T) && new_n <= (std::numeric_limits::max)() / sizeof(T)); + return static_cast(a.Realloc(old_p, old_n * sizeof(T), new_n * sizeof(T))); +} + +template +inline T *Malloc(A& a, size_t n = 1) +{ + return Realloc(a, NULL, 0, n); +} + +template +inline void Free(A& a, T *p, size_t n = 1) +{ + static_cast(Realloc(a, p, n, 0)); +} + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) // std::allocator can safely be inherited +#endif + +template +class StdAllocator : + public std::allocator +{ + typedef std::allocator allocator_type; +#if RAPIDJSON_HAS_CXX11 + typedef std::allocator_traits traits_type; +#else + typedef allocator_type traits_type; +#endif + +public: + typedef BaseAllocator BaseAllocatorType; + + StdAllocator() RAPIDJSON_NOEXCEPT : + allocator_type(), + baseAllocator_() + { } + + StdAllocator(const StdAllocator& rhs) RAPIDJSON_NOEXCEPT : + allocator_type(rhs), + baseAllocator_(rhs.baseAllocator_) + { } + + template + StdAllocator(const StdAllocator& rhs) RAPIDJSON_NOEXCEPT : + allocator_type(rhs), + baseAllocator_(rhs.baseAllocator_) + { } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + StdAllocator(StdAllocator&& rhs) RAPIDJSON_NOEXCEPT : + allocator_type(std::move(rhs)), + baseAllocator_(std::move(rhs.baseAllocator_)) + { } +#endif +#if RAPIDJSON_HAS_CXX11 + using propagate_on_container_move_assignment = std::true_type; + using propagate_on_container_swap = std::true_type; +#endif + + /* implicit */ + StdAllocator(const BaseAllocator& allocator) RAPIDJSON_NOEXCEPT : + allocator_type(), + baseAllocator_(allocator) + { } + + ~StdAllocator() RAPIDJSON_NOEXCEPT + { } + + template + struct rebind { + typedef StdAllocator other; + }; + + typedef typename traits_type::size_type size_type; + typedef typename traits_type::difference_type difference_type; + + typedef typename traits_type::value_type value_type; + typedef typename traits_type::pointer pointer; + typedef typename traits_type::const_pointer const_pointer; + +#if RAPIDJSON_HAS_CXX11 + + typedef typename std::add_lvalue_reference::type &reference; + typedef typename std::add_lvalue_reference::type>::type &const_reference; + + pointer address(reference r) const RAPIDJSON_NOEXCEPT + { + return std::addressof(r); + } + const_pointer address(const_reference r) const RAPIDJSON_NOEXCEPT + { + return std::addressof(r); + } + + size_type max_size() const RAPIDJSON_NOEXCEPT + { + return traits_type::max_size(*this); + } + + template + void construct(pointer p, Args&&... args) + { + traits_type::construct(*this, p, std::forward(args)...); + } + void destroy(pointer p) + { + traits_type::destroy(*this, p); + } + +#else // !RAPIDJSON_HAS_CXX11 + + typedef typename allocator_type::reference reference; + typedef typename allocator_type::const_reference const_reference; + + pointer address(reference r) const RAPIDJSON_NOEXCEPT + { + return allocator_type::address(r); + } + const_pointer address(const_reference r) const RAPIDJSON_NOEXCEPT + { + return allocator_type::address(r); + } + + size_type max_size() const RAPIDJSON_NOEXCEPT + { + return allocator_type::max_size(); + } + + void construct(pointer p, const_reference r) + { + allocator_type::construct(p, r); + } + void destroy(pointer p) + { + allocator_type::destroy(p); + } + +#endif // !RAPIDJSON_HAS_CXX11 + + template + U* allocate(size_type n = 1, const void* = 0) + { + return RAPIDJSON_NAMESPACE::Malloc(baseAllocator_, n); + } + template + void deallocate(U* p, size_type n = 1) + { + RAPIDJSON_NAMESPACE::Free(baseAllocator_, p, n); + } + + pointer allocate(size_type n = 1, const void* = 0) + { + return allocate(n); + } + void deallocate(pointer p, size_type n = 1) + { + deallocate(p, n); + } + +#if RAPIDJSON_HAS_CXX11 + using is_always_equal = std::is_empty; +#endif + + template + bool operator==(const StdAllocator& rhs) const RAPIDJSON_NOEXCEPT + { + return baseAllocator_ == rhs.baseAllocator_; + } + template + bool operator!=(const StdAllocator& rhs) const RAPIDJSON_NOEXCEPT + { + return !operator==(rhs); + } + + //! rapidjson Allocator concept + static const bool kNeedFree = BaseAllocator::kNeedFree; + static const bool kRefCounted = internal::IsRefCounted::Value; + void* Malloc(size_t size) + { + return baseAllocator_.Malloc(size); + } + void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) + { + return baseAllocator_.Realloc(originalPtr, originalSize, newSize); + } + static void Free(void *ptr) RAPIDJSON_NOEXCEPT + { + BaseAllocator::Free(ptr); + } + +private: + template + friend class StdAllocator; // access to StdAllocator.* + + BaseAllocator baseAllocator_; +}; + +#if !RAPIDJSON_HAS_CXX17 // std::allocator deprecated in C++17 +template +class StdAllocator : + public std::allocator +{ + typedef std::allocator allocator_type; + +public: + typedef BaseAllocator BaseAllocatorType; + + StdAllocator() RAPIDJSON_NOEXCEPT : + allocator_type(), + baseAllocator_() + { } + + StdAllocator(const StdAllocator& rhs) RAPIDJSON_NOEXCEPT : + allocator_type(rhs), + baseAllocator_(rhs.baseAllocator_) + { } + + template + StdAllocator(const StdAllocator& rhs) RAPIDJSON_NOEXCEPT : + allocator_type(rhs), + baseAllocator_(rhs.baseAllocator_) + { } + + /* implicit */ + StdAllocator(const BaseAllocator& baseAllocator) RAPIDJSON_NOEXCEPT : + allocator_type(), + baseAllocator_(baseAllocator) + { } + + ~StdAllocator() RAPIDJSON_NOEXCEPT + { } + + template + struct rebind { + typedef StdAllocator other; + }; + + typedef typename allocator_type::value_type value_type; + +private: + template + friend class StdAllocator; // access to StdAllocator.* + + BaseAllocator baseAllocator_; +}; +#endif + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_ENCODINGS_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/cursorstreamwrapper.h b/include/liblvgl/libs/thorvg/rapidjson/cursorstreamwrapper.h new file mode 100644 index 00000000..fd6513db --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/cursorstreamwrapper.h @@ -0,0 +1,78 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_CURSORSTREAMWRAPPER_H_ +#define RAPIDJSON_CURSORSTREAMWRAPPER_H_ + +#include "stream.h" + +#if defined(__GNUC__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +#if defined(_MSC_VER) && _MSC_VER <= 1800 +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4702) // unreachable code +RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated +#endif + +RAPIDJSON_NAMESPACE_BEGIN + + +//! Cursor stream wrapper for counting line and column number if error exists. +/*! + \tparam InputStream Any stream that implements Stream Concept +*/ +template > +class CursorStreamWrapper : public GenericStreamWrapper { +public: + typedef typename Encoding::Ch Ch; + + CursorStreamWrapper(InputStream& is): + GenericStreamWrapper(is), line_(1), col_(0) {} + + // counting line and column number + Ch Take() { + Ch ch = this->is_.Take(); + if(ch == '\n') { + line_ ++; + col_ = 0; + } else { + col_ ++; + } + return ch; + } + + //! Get the error line number, if error exists. + size_t GetLine() const { return line_; } + //! Get the error column number, if error exists. + size_t GetColumn() const { return col_; } + +private: + size_t line_; //!< Current Line + size_t col_; //!< Current Column +}; + +#if defined(_MSC_VER) && _MSC_VER <= 1800 +RAPIDJSON_DIAG_POP +#endif + +#if defined(__GNUC__) +RAPIDJSON_DIAG_POP +#endif + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_CURSORSTREAMWRAPPER_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/document.h b/include/liblvgl/libs/thorvg/rapidjson/document.h new file mode 100644 index 00000000..f18fbd5f --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/document.h @@ -0,0 +1,3043 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_DOCUMENT_H_ +#define RAPIDJSON_DOCUMENT_H_ + +/*! \file document.h */ + +#include "reader.h" +#include "internal/meta.h" +#include "internal/strfunc.h" +#include "memorystream.h" +#include "encodedstream.h" +#include // placement new +#include +#ifdef __cpp_lib_three_way_comparison +#include +#endif + +RAPIDJSON_DIAG_PUSH +#ifdef __clang__ +RAPIDJSON_DIAG_OFF(padded) +RAPIDJSON_DIAG_OFF(switch-enum) +RAPIDJSON_DIAG_OFF(c++98-compat) +#elif defined(_MSC_VER) +RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant +RAPIDJSON_DIAG_OFF(4244) // conversion from kXxxFlags to 'uint16_t', possible loss of data +#endif + +#ifdef __GNUC__ +RAPIDJSON_DIAG_OFF(effc++) +#endif // __GNUC__ + +#ifdef GetObject +// see https://github.com/Tencent/rapidjson/issues/1448 +// a former included windows.h might have defined a macro called GetObject, which affects +// GetObject defined here. This ensures the macro does not get applied +#pragma push_macro("GetObject") +#define RAPIDJSON_WINDOWS_GETOBJECT_WORKAROUND_APPLIED +#undef GetObject +#endif + +#ifndef RAPIDJSON_NOMEMBERITERATORCLASS +#include // std::random_access_iterator_tag +#endif + +#if RAPIDJSON_USE_MEMBERSMAP +#include // std::multimap +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +// Forward declaration. +template +class GenericValue; + +template +class GenericDocument; + +/*! \def RAPIDJSON_DEFAULT_ALLOCATOR + \ingroup RAPIDJSON_CONFIG + \brief Allows to choose default allocator. + + User can define this to use CrtAllocator or MemoryPoolAllocator. +*/ +#ifndef RAPIDJSON_DEFAULT_ALLOCATOR +#define RAPIDJSON_DEFAULT_ALLOCATOR ::RAPIDJSON_NAMESPACE::MemoryPoolAllocator<::RAPIDJSON_NAMESPACE::CrtAllocator> +#endif + +/*! \def RAPIDJSON_DEFAULT_STACK_ALLOCATOR + \ingroup RAPIDJSON_CONFIG + \brief Allows to choose default stack allocator for Document. + + User can define this to use CrtAllocator or MemoryPoolAllocator. +*/ +#ifndef RAPIDJSON_DEFAULT_STACK_ALLOCATOR +#define RAPIDJSON_DEFAULT_STACK_ALLOCATOR ::RAPIDJSON_NAMESPACE::CrtAllocator +#endif + +/*! \def RAPIDJSON_VALUE_DEFAULT_OBJECT_CAPACITY + \ingroup RAPIDJSON_CONFIG + \brief User defined kDefaultObjectCapacity value. + + User can define this as any natural number. +*/ +#ifndef RAPIDJSON_VALUE_DEFAULT_OBJECT_CAPACITY +// number of objects that rapidjson::Value allocates memory for by default +#define RAPIDJSON_VALUE_DEFAULT_OBJECT_CAPACITY 16 +#endif + +/*! \def RAPIDJSON_VALUE_DEFAULT_ARRAY_CAPACITY + \ingroup RAPIDJSON_CONFIG + \brief User defined kDefaultArrayCapacity value. + + User can define this as any natural number. +*/ +#ifndef RAPIDJSON_VALUE_DEFAULT_ARRAY_CAPACITY +// number of array elements that rapidjson::Value allocates memory for by default +#define RAPIDJSON_VALUE_DEFAULT_ARRAY_CAPACITY 16 +#endif + +//! Name-value pair in a JSON object value. +/*! + This class was internal to GenericValue. It used to be a inner struct. + But a compiler (IBM XL C/C++ for AIX) have reported to have problem with that so it moved as a namespace scope struct. + https://code.google.com/p/rapidjson/issues/detail?id=64 +*/ +template +class GenericMember { +public: + GenericValue name; //!< name of member (must be a string) + GenericValue value; //!< value of member. + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move constructor in C++11 + GenericMember(GenericMember&& rhs) RAPIDJSON_NOEXCEPT + : name(std::move(rhs.name)), + value(std::move(rhs.value)) + { + } + + //! Move assignment in C++11 + GenericMember& operator=(GenericMember&& rhs) RAPIDJSON_NOEXCEPT { + return *this = static_cast(rhs); + } +#endif + + //! Assignment with move semantics. + /*! \param rhs Source of the assignment. Its name and value will become a null value after assignment. + */ + GenericMember& operator=(GenericMember& rhs) RAPIDJSON_NOEXCEPT { + if (RAPIDJSON_LIKELY(this != &rhs)) { + name = rhs.name; + value = rhs.value; + } + return *this; + } + + // swap() for std::sort() and other potential use in STL. + friend inline void swap(GenericMember& a, GenericMember& b) RAPIDJSON_NOEXCEPT { + a.name.Swap(b.name); + a.value.Swap(b.value); + } + +private: + //! Copy constructor is not permitted. + GenericMember(const GenericMember& rhs); +}; + +/////////////////////////////////////////////////////////////////////////////// +// GenericMemberIterator + +#ifndef RAPIDJSON_NOMEMBERITERATORCLASS + +//! (Constant) member iterator for a JSON object value +/*! + \tparam Const Is this a constant iterator? + \tparam Encoding Encoding of the value. (Even non-string values need to have the same encoding in a document) + \tparam Allocator Allocator type for allocating memory of object, array and string. + + This class implements a Random Access Iterator for GenericMember elements + of a GenericValue, see ISO/IEC 14882:2003(E) C++ standard, 24.1 [lib.iterator.requirements]. + + \note This iterator implementation is mainly intended to avoid implicit + conversions from iterator values to \c NULL, + e.g. from GenericValue::FindMember. + + \note Define \c RAPIDJSON_NOMEMBERITERATORCLASS to fall back to a + pointer-based implementation, if your platform doesn't provide + the C++ header. + + \see GenericMember, GenericValue::MemberIterator, GenericValue::ConstMemberIterator + */ +template +class GenericMemberIterator { + + friend class GenericValue; + template friend class GenericMemberIterator; + + typedef GenericMember PlainType; + typedef typename internal::MaybeAddConst::Type ValueType; + +public: + //! Iterator type itself + typedef GenericMemberIterator Iterator; + //! Constant iterator type + typedef GenericMemberIterator ConstIterator; + //! Non-constant iterator type + typedef GenericMemberIterator NonConstIterator; + + /** \name std::iterator_traits support */ + //@{ + typedef ValueType value_type; + typedef ValueType * pointer; + typedef ValueType & reference; + typedef std::ptrdiff_t difference_type; + typedef std::random_access_iterator_tag iterator_category; + //@} + + //! Pointer to (const) GenericMember + typedef pointer Pointer; + //! Reference to (const) GenericMember + typedef reference Reference; + //! Signed integer type (e.g. \c ptrdiff_t) + typedef difference_type DifferenceType; + + //! Default constructor (singular value) + /*! Creates an iterator pointing to no element. + \note All operations, except for comparisons, are undefined on such values. + */ + GenericMemberIterator() : ptr_() {} + + //! Iterator conversions to more const + /*! + \param it (Non-const) iterator to copy from + + Allows the creation of an iterator from another GenericMemberIterator + that is "less const". Especially, creating a non-constant iterator + from a constant iterator are disabled: + \li const -> non-const (not ok) + \li const -> const (ok) + \li non-const -> const (ok) + \li non-const -> non-const (ok) + + \note If the \c Const template parameter is already \c false, this + constructor effectively defines a regular copy-constructor. + Otherwise, the copy constructor is implicitly defined. + */ + GenericMemberIterator(const NonConstIterator & it) : ptr_(it.ptr_) {} + Iterator& operator=(const NonConstIterator & it) { ptr_ = it.ptr_; return *this; } + + //! @name stepping + //@{ + Iterator& operator++(){ ++ptr_; return *this; } + Iterator& operator--(){ --ptr_; return *this; } + Iterator operator++(int){ Iterator old(*this); ++ptr_; return old; } + Iterator operator--(int){ Iterator old(*this); --ptr_; return old; } + //@} + + //! @name increment/decrement + //@{ + Iterator operator+(DifferenceType n) const { return Iterator(ptr_+n); } + Iterator operator-(DifferenceType n) const { return Iterator(ptr_-n); } + + Iterator& operator+=(DifferenceType n) { ptr_+=n; return *this; } + Iterator& operator-=(DifferenceType n) { ptr_-=n; return *this; } + //@} + + //! @name relations + //@{ + template bool operator==(const GenericMemberIterator& that) const { return ptr_ == that.ptr_; } + template bool operator!=(const GenericMemberIterator& that) const { return ptr_ != that.ptr_; } + template bool operator<=(const GenericMemberIterator& that) const { return ptr_ <= that.ptr_; } + template bool operator>=(const GenericMemberIterator& that) const { return ptr_ >= that.ptr_; } + template bool operator< (const GenericMemberIterator& that) const { return ptr_ < that.ptr_; } + template bool operator> (const GenericMemberIterator& that) const { return ptr_ > that.ptr_; } + +#ifdef __cpp_lib_three_way_comparison + template std::strong_ordering operator<=>(const GenericMemberIterator& that) const { return ptr_ <=> that.ptr_; } +#endif + //@} + + //! @name dereference + //@{ + Reference operator*() const { return *ptr_; } + Pointer operator->() const { return ptr_; } + Reference operator[](DifferenceType n) const { return ptr_[n]; } + //@} + + //! Distance + DifferenceType operator-(ConstIterator that) const { return ptr_-that.ptr_; } + +private: + //! Internal constructor from plain pointer + explicit GenericMemberIterator(Pointer p) : ptr_(p) {} + + Pointer ptr_; //!< raw pointer +}; + +#else // RAPIDJSON_NOMEMBERITERATORCLASS + +// class-based member iterator implementation disabled, use plain pointers + +template +class GenericMemberIterator; + +//! non-const GenericMemberIterator +template +class GenericMemberIterator { +public: + //! use plain pointer as iterator type + typedef GenericMember* Iterator; +}; +//! const GenericMemberIterator +template +class GenericMemberIterator { +public: + //! use plain const pointer as iterator type + typedef const GenericMember* Iterator; +}; + +#endif // RAPIDJSON_NOMEMBERITERATORCLASS + +/////////////////////////////////////////////////////////////////////////////// +// GenericStringRef + +//! Reference to a constant string (not taking a copy) +/*! + \tparam CharType character type of the string + + This helper class is used to automatically infer constant string + references for string literals, especially from \c const \b (!) + character arrays. + + The main use is for creating JSON string values without copying the + source string via an \ref Allocator. This requires that the referenced + string pointers have a sufficient lifetime, which exceeds the lifetime + of the associated GenericValue. + + \b Example + \code + Value v("foo"); // ok, no need to copy & calculate length + const char foo[] = "foo"; + v.SetString(foo); // ok + + const char* bar = foo; + // Value x(bar); // not ok, can't rely on bar's lifetime + Value x(StringRef(bar)); // lifetime explicitly guaranteed by user + Value y(StringRef(bar, 3)); // ok, explicitly pass length + \endcode + + \see StringRef, GenericValue::SetString +*/ +template +struct GenericStringRef { + typedef CharType Ch; //!< character type of the string + + //! Create string reference from \c const character array +#ifndef __clang__ // -Wdocumentation + /*! + This constructor implicitly creates a constant string reference from + a \c const character array. It has better performance than + \ref StringRef(const CharType*) by inferring the string \ref length + from the array length, and also supports strings containing null + characters. + + \tparam N length of the string, automatically inferred + + \param str Constant character array, lifetime assumed to be longer + than the use of the string in e.g. a GenericValue + + \post \ref s == str + + \note Constant complexity. + \note There is a hidden, private overload to disallow references to + non-const character arrays to be created via this constructor. + By this, e.g. function-scope arrays used to be filled via + \c snprintf are excluded from consideration. + In such cases, the referenced string should be \b copied to the + GenericValue instead. + */ +#endif + template + GenericStringRef(const CharType (&str)[N]) RAPIDJSON_NOEXCEPT + : s(str), length(N-1) {} + + //! Explicitly create string reference from \c const character pointer +#ifndef __clang__ // -Wdocumentation + /*! + This constructor can be used to \b explicitly create a reference to + a constant string pointer. + + \see StringRef(const CharType*) + + \param str Constant character pointer, lifetime assumed to be longer + than the use of the string in e.g. a GenericValue + + \post \ref s == str + + \note There is a hidden, private overload to disallow references to + non-const character arrays to be created via this constructor. + By this, e.g. function-scope arrays used to be filled via + \c snprintf are excluded from consideration. + In such cases, the referenced string should be \b copied to the + GenericValue instead. + */ +#endif + explicit GenericStringRef(const CharType* str) + : s(str), length(NotNullStrLen(str)) {} + + //! Create constant string reference from pointer and length +#ifndef __clang__ // -Wdocumentation + /*! \param str constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue + \param len length of the string, excluding the trailing NULL terminator + + \post \ref s == str && \ref length == len + \note Constant complexity. + */ +#endif + GenericStringRef(const CharType* str, SizeType len) + : s(RAPIDJSON_LIKELY(str) ? str : emptyString), length(len) { RAPIDJSON_ASSERT(str != 0 || len == 0u); } + + GenericStringRef(const GenericStringRef& rhs) : s(rhs.s), length(rhs.length) {} + + //! implicit conversion to plain CharType pointer + operator const Ch *() const { return s; } + + const Ch* const s; //!< plain CharType pointer + const SizeType length; //!< length of the string (excluding the trailing NULL terminator) + +private: + SizeType NotNullStrLen(const CharType* str) { + RAPIDJSON_ASSERT(str != 0); + return internal::StrLen(str); + } + + /// Empty string - used when passing in a NULL pointer + static const Ch emptyString[]; + + //! Disallow construction from non-const array + template + GenericStringRef(CharType (&str)[N]) /* = delete */; + //! Copy assignment operator not permitted - immutable type + GenericStringRef& operator=(const GenericStringRef& rhs) /* = delete */; +}; + +template +const CharType GenericStringRef::emptyString[] = { CharType() }; + +//! Mark a character pointer as constant string +/*! Mark a plain character pointer as a "string literal". This function + can be used to avoid copying a character string to be referenced as a + value in a JSON GenericValue object, if the string's lifetime is known + to be valid long enough. + \tparam CharType Character type of the string + \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue + \return GenericStringRef string reference object + \relatesalso GenericStringRef + + \see GenericValue::GenericValue(StringRefType), GenericValue::operator=(StringRefType), GenericValue::SetString(StringRefType), GenericValue::PushBack(StringRefType, Allocator&), GenericValue::AddMember +*/ +template +inline GenericStringRef StringRef(const CharType* str) { + return GenericStringRef(str); +} + +//! Mark a character pointer as constant string +/*! Mark a plain character pointer as a "string literal". This function + can be used to avoid copying a character string to be referenced as a + value in a JSON GenericValue object, if the string's lifetime is known + to be valid long enough. + + This version has better performance with supplied length, and also + supports string containing null characters. + + \tparam CharType character type of the string + \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue + \param length The length of source string. + \return GenericStringRef string reference object + \relatesalso GenericStringRef +*/ +template +inline GenericStringRef StringRef(const CharType* str, size_t length) { + return GenericStringRef(str, SizeType(length)); +} + +#if RAPIDJSON_HAS_STDSTRING +//! Mark a string object as constant string +/*! Mark a string object (e.g. \c std::string) as a "string literal". + This function can be used to avoid copying a string to be referenced as a + value in a JSON GenericValue object, if the string's lifetime is known + to be valid long enough. + + \tparam CharType character type of the string + \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue + \return GenericStringRef string reference object + \relatesalso GenericStringRef + \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. +*/ +template +inline GenericStringRef StringRef(const std::basic_string& str) { + return GenericStringRef(str.data(), SizeType(str.size())); +} +#endif + +/////////////////////////////////////////////////////////////////////////////// +// GenericValue type traits +namespace internal { + +template +struct IsGenericValueImpl : FalseType {}; + +// select candidates according to nested encoding and allocator types +template struct IsGenericValueImpl::Type, typename Void::Type> + : IsBaseOf, T>::Type {}; + +// helper to match arbitrary GenericValue instantiations, including derived classes +template struct IsGenericValue : IsGenericValueImpl::Type {}; + +} // namespace internal + +/////////////////////////////////////////////////////////////////////////////// +// TypeHelper + +namespace internal { + +template +struct TypeHelper {}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsBool(); } + static bool Get(const ValueType& v) { return v.GetBool(); } + static ValueType& Set(ValueType& v, bool data) { return v.SetBool(data); } + static ValueType& Set(ValueType& v, bool data, typename ValueType::AllocatorType&) { return v.SetBool(data); } +}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsInt(); } + static int Get(const ValueType& v) { return v.GetInt(); } + static ValueType& Set(ValueType& v, int data) { return v.SetInt(data); } + static ValueType& Set(ValueType& v, int data, typename ValueType::AllocatorType&) { return v.SetInt(data); } +}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsUint(); } + static unsigned Get(const ValueType& v) { return v.GetUint(); } + static ValueType& Set(ValueType& v, unsigned data) { return v.SetUint(data); } + static ValueType& Set(ValueType& v, unsigned data, typename ValueType::AllocatorType&) { return v.SetUint(data); } +}; + +#ifdef _MSC_VER +RAPIDJSON_STATIC_ASSERT(sizeof(long) == sizeof(int)); +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsInt(); } + static long Get(const ValueType& v) { return v.GetInt(); } + static ValueType& Set(ValueType& v, long data) { return v.SetInt(data); } + static ValueType& Set(ValueType& v, long data, typename ValueType::AllocatorType&) { return v.SetInt(data); } +}; + +RAPIDJSON_STATIC_ASSERT(sizeof(unsigned long) == sizeof(unsigned)); +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsUint(); } + static unsigned long Get(const ValueType& v) { return v.GetUint(); } + static ValueType& Set(ValueType& v, unsigned long data) { return v.SetUint(data); } + static ValueType& Set(ValueType& v, unsigned long data, typename ValueType::AllocatorType&) { return v.SetUint(data); } +}; +#endif + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsInt64(); } + static int64_t Get(const ValueType& v) { return v.GetInt64(); } + static ValueType& Set(ValueType& v, int64_t data) { return v.SetInt64(data); } + static ValueType& Set(ValueType& v, int64_t data, typename ValueType::AllocatorType&) { return v.SetInt64(data); } +}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsUint64(); } + static uint64_t Get(const ValueType& v) { return v.GetUint64(); } + static ValueType& Set(ValueType& v, uint64_t data) { return v.SetUint64(data); } + static ValueType& Set(ValueType& v, uint64_t data, typename ValueType::AllocatorType&) { return v.SetUint64(data); } +}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsDouble(); } + static double Get(const ValueType& v) { return v.GetDouble(); } + static ValueType& Set(ValueType& v, double data) { return v.SetDouble(data); } + static ValueType& Set(ValueType& v, double data, typename ValueType::AllocatorType&) { return v.SetDouble(data); } +}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsFloat(); } + static float Get(const ValueType& v) { return v.GetFloat(); } + static ValueType& Set(ValueType& v, float data) { return v.SetFloat(data); } + static ValueType& Set(ValueType& v, float data, typename ValueType::AllocatorType&) { return v.SetFloat(data); } +}; + +template +struct TypeHelper { + typedef const typename ValueType::Ch* StringType; + static bool Is(const ValueType& v) { return v.IsString(); } + static StringType Get(const ValueType& v) { return v.GetString(); } + static ValueType& Set(ValueType& v, const StringType data) { return v.SetString(typename ValueType::StringRefType(data)); } + static ValueType& Set(ValueType& v, const StringType data, typename ValueType::AllocatorType& a) { return v.SetString(data, a); } +}; + +#if RAPIDJSON_HAS_STDSTRING +template +struct TypeHelper > { + typedef std::basic_string StringType; + static bool Is(const ValueType& v) { return v.IsString(); } + static StringType Get(const ValueType& v) { return StringType(v.GetString(), v.GetStringLength()); } + static ValueType& Set(ValueType& v, const StringType& data, typename ValueType::AllocatorType& a) { return v.SetString(data, a); } +}; +#endif + +template +struct TypeHelper { + typedef typename ValueType::Array ArrayType; + static bool Is(const ValueType& v) { return v.IsArray(); } + static ArrayType Get(ValueType& v) { return v.GetArray(); } + static ValueType& Set(ValueType& v, ArrayType data) { return v = data; } + static ValueType& Set(ValueType& v, ArrayType data, typename ValueType::AllocatorType&) { return v = data; } +}; + +template +struct TypeHelper { + typedef typename ValueType::ConstArray ArrayType; + static bool Is(const ValueType& v) { return v.IsArray(); } + static ArrayType Get(const ValueType& v) { return v.GetArray(); } +}; + +template +struct TypeHelper { + typedef typename ValueType::Object ObjectType; + static bool Is(const ValueType& v) { return v.IsObject(); } + static ObjectType Get(ValueType& v) { return v.GetObject(); } + static ValueType& Set(ValueType& v, ObjectType data) { return v = data; } + static ValueType& Set(ValueType& v, ObjectType data, typename ValueType::AllocatorType&) { return v = data; } +}; + +template +struct TypeHelper { + typedef typename ValueType::ConstObject ObjectType; + static bool Is(const ValueType& v) { return v.IsObject(); } + static ObjectType Get(const ValueType& v) { return v.GetObject(); } +}; + +} // namespace internal + +// Forward declarations +template class GenericArray; +template class GenericObject; + +/////////////////////////////////////////////////////////////////////////////// +// GenericValue + +//! Represents a JSON value. Use Value for UTF8 encoding and default allocator. +/*! + A JSON value can be one of 7 types. This class is a variant type supporting + these types. + + Use the Value if UTF8 and default allocator + + \tparam Encoding Encoding of the value. (Even non-string values need to have the same encoding in a document) + \tparam Allocator Allocator type for allocating memory of object, array and string. +*/ +template +class GenericValue { +public: + //! Name-value pair in an object. + typedef GenericMember Member; + typedef Encoding EncodingType; //!< Encoding type from template parameter. + typedef Allocator AllocatorType; //!< Allocator type from template parameter. + typedef typename Encoding::Ch Ch; //!< Character type derived from Encoding. + typedef GenericStringRef StringRefType; //!< Reference to a constant string + typedef typename GenericMemberIterator::Iterator MemberIterator; //!< Member iterator for iterating in object. + typedef typename GenericMemberIterator::Iterator ConstMemberIterator; //!< Constant member iterator for iterating in object. + typedef GenericValue* ValueIterator; //!< Value iterator for iterating in array. + typedef const GenericValue* ConstValueIterator; //!< Constant value iterator for iterating in array. + typedef GenericValue ValueType; //!< Value type of itself. + typedef GenericArray Array; + typedef GenericArray ConstArray; + typedef GenericObject Object; + typedef GenericObject ConstObject; + + //!@name Constructors and destructor. + //@{ + + //! Default constructor creates a null value. + GenericValue() RAPIDJSON_NOEXCEPT : data_() { data_.f.flags = kNullFlag; } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move constructor in C++11 + GenericValue(GenericValue&& rhs) RAPIDJSON_NOEXCEPT : data_(rhs.data_) { + rhs.data_.f.flags = kNullFlag; // give up contents + } +#endif + +private: + //! Copy constructor is not permitted. + GenericValue(const GenericValue& rhs); + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Moving from a GenericDocument is not permitted. + template + GenericValue(GenericDocument&& rhs); + + //! Move assignment from a GenericDocument is not permitted. + template + GenericValue& operator=(GenericDocument&& rhs); +#endif + +public: + + //! Constructor with JSON value type. + /*! This creates a Value of specified type with default content. + \param type Type of the value. + \note Default content for number is zero. + */ + explicit GenericValue(Type type) RAPIDJSON_NOEXCEPT : data_() { + static const uint16_t defaultFlags[] = { + kNullFlag, kFalseFlag, kTrueFlag, kObjectFlag, kArrayFlag, kShortStringFlag, + kNumberAnyFlag + }; + RAPIDJSON_NOEXCEPT_ASSERT(type >= kNullType && type <= kNumberType); + data_.f.flags = defaultFlags[type]; + + // Use ShortString to store empty string. + if (type == kStringType) + data_.ss.SetLength(0); + } + + //! Explicit copy constructor (with allocator) + /*! Creates a copy of a Value by using the given Allocator + \tparam SourceAllocator allocator of \c rhs + \param rhs Value to copy from (read-only) + \param allocator Allocator for allocating copied elements and buffers. Commonly use GenericDocument::GetAllocator(). + \param copyConstStrings Force copying of constant strings (e.g. referencing an in-situ buffer) + \see CopyFrom() + */ + template + GenericValue(const GenericValue& rhs, Allocator& allocator, bool copyConstStrings = false) { + switch (rhs.GetType()) { + case kObjectType: + DoCopyMembers(rhs, allocator, copyConstStrings); + break; + case kArrayType: { + SizeType count = rhs.data_.a.size; + GenericValue* le = reinterpret_cast(allocator.Malloc(count * sizeof(GenericValue))); + const GenericValue* re = rhs.GetElementsPointer(); + for (SizeType i = 0; i < count; i++) + new (&le[i]) GenericValue(re[i], allocator, copyConstStrings); + data_.f.flags = kArrayFlag; + data_.a.size = data_.a.capacity = count; + SetElementsPointer(le); + } + break; + case kStringType: + if (rhs.data_.f.flags == kConstStringFlag && !copyConstStrings) { + data_.f.flags = rhs.data_.f.flags; + data_ = *reinterpret_cast(&rhs.data_); + } + else + SetStringRaw(StringRef(rhs.GetString(), rhs.GetStringLength()), allocator); + break; + default: + data_.f.flags = rhs.data_.f.flags; + data_ = *reinterpret_cast(&rhs.data_); + break; + } + } + + //! Constructor for boolean value. + /*! \param b Boolean value + \note This constructor is limited to \em real boolean values and rejects + implicitly converted types like arbitrary pointers. Use an explicit cast + to \c bool, if you want to construct a boolean JSON value in such cases. + */ +#ifndef RAPIDJSON_DOXYGEN_RUNNING // hide SFINAE from Doxygen + template + explicit GenericValue(T b, RAPIDJSON_ENABLEIF((internal::IsSame))) RAPIDJSON_NOEXCEPT // See #472 +#else + explicit GenericValue(bool b) RAPIDJSON_NOEXCEPT +#endif + : data_() { + // safe-guard against failing SFINAE + RAPIDJSON_STATIC_ASSERT((internal::IsSame::Value)); + data_.f.flags = b ? kTrueFlag : kFalseFlag; + } + + //! Constructor for int value. + explicit GenericValue(int i) RAPIDJSON_NOEXCEPT : data_() { + data_.n.i64 = i; + data_.f.flags = (i >= 0) ? (kNumberIntFlag | kUintFlag | kUint64Flag) : kNumberIntFlag; + } + + //! Constructor for unsigned value. + explicit GenericValue(unsigned u) RAPIDJSON_NOEXCEPT : data_() { + data_.n.u64 = u; + data_.f.flags = (u & 0x80000000) ? kNumberUintFlag : (kNumberUintFlag | kIntFlag | kInt64Flag); + } + + //! Constructor for int64_t value. + explicit GenericValue(int64_t i64) RAPIDJSON_NOEXCEPT : data_() { + data_.n.i64 = i64; + data_.f.flags = kNumberInt64Flag; + if (i64 >= 0) { + data_.f.flags |= kNumberUint64Flag; + if (!(static_cast(i64) & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x00000000))) + data_.f.flags |= kUintFlag; + if (!(static_cast(i64) & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000))) + data_.f.flags |= kIntFlag; + } + else if (i64 >= static_cast(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000))) + data_.f.flags |= kIntFlag; + } + + //! Constructor for uint64_t value. + explicit GenericValue(uint64_t u64) RAPIDJSON_NOEXCEPT : data_() { + data_.n.u64 = u64; + data_.f.flags = kNumberUint64Flag; + if (!(u64 & RAPIDJSON_UINT64_C2(0x80000000, 0x00000000))) + data_.f.flags |= kInt64Flag; + if (!(u64 & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x00000000))) + data_.f.flags |= kUintFlag; + if (!(u64 & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000))) + data_.f.flags |= kIntFlag; + } + + //! Constructor for double value. + explicit GenericValue(double d) RAPIDJSON_NOEXCEPT : data_() { data_.n.d = d; data_.f.flags = kNumberDoubleFlag; } + + //! Constructor for float value. + explicit GenericValue(float f) RAPIDJSON_NOEXCEPT : data_() { data_.n.d = static_cast(f); data_.f.flags = kNumberDoubleFlag; } + + //! Constructor for constant string (i.e. do not make a copy of string) + GenericValue(const Ch* s, SizeType length) RAPIDJSON_NOEXCEPT : data_() { SetStringRaw(StringRef(s, length)); } + + //! Constructor for constant string (i.e. do not make a copy of string) + explicit GenericValue(StringRefType s) RAPIDJSON_NOEXCEPT : data_() { SetStringRaw(s); } + + //! Constructor for copy-string (i.e. do make a copy of string) + GenericValue(const Ch* s, SizeType length, Allocator& allocator) : data_() { SetStringRaw(StringRef(s, length), allocator); } + + //! Constructor for copy-string (i.e. do make a copy of string) + GenericValue(const Ch*s, Allocator& allocator) : data_() { SetStringRaw(StringRef(s), allocator); } + +#if RAPIDJSON_HAS_STDSTRING + //! Constructor for copy-string from a string object (i.e. do make a copy of string) + /*! \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. + */ + GenericValue(const std::basic_string& s, Allocator& allocator) : data_() { SetStringRaw(StringRef(s), allocator); } +#endif + + //! Constructor for Array. + /*! + \param a An array obtained by \c GetArray(). + \note \c Array is always pass-by-value. + \note the source array is moved into this value and the source array becomes empty. + */ + GenericValue(Array a) RAPIDJSON_NOEXCEPT : data_(a.value_.data_) { + a.value_.data_ = Data(); + a.value_.data_.f.flags = kArrayFlag; + } + + //! Constructor for Object. + /*! + \param o An object obtained by \c GetObject(). + \note \c Object is always pass-by-value. + \note the source object is moved into this value and the source object becomes empty. + */ + GenericValue(Object o) RAPIDJSON_NOEXCEPT : data_(o.value_.data_) { + o.value_.data_ = Data(); + o.value_.data_.f.flags = kObjectFlag; + } + + //! Destructor. + /*! Need to destruct elements of array, members of object, or copy-string. + */ + ~GenericValue() { + // With RAPIDJSON_USE_MEMBERSMAP, the maps need to be destroyed to release + // their Allocator if it's refcounted (e.g. MemoryPoolAllocator). + if (Allocator::kNeedFree || (RAPIDJSON_USE_MEMBERSMAP+0 && + internal::IsRefCounted::Value)) { + switch(data_.f.flags) { + case kArrayFlag: + { + GenericValue* e = GetElementsPointer(); + for (GenericValue* v = e; v != e + data_.a.size; ++v) + v->~GenericValue(); + if (Allocator::kNeedFree) { // Shortcut by Allocator's trait + Allocator::Free(e); + } + } + break; + + case kObjectFlag: + DoFreeMembers(); + break; + + case kCopyStringFlag: + if (Allocator::kNeedFree) { // Shortcut by Allocator's trait + Allocator::Free(const_cast(GetStringPointer())); + } + break; + + default: + break; // Do nothing for other types. + } + } + } + + //@} + + //!@name Assignment operators + //@{ + + //! Assignment with move semantics. + /*! \param rhs Source of the assignment. It will become a null value after assignment. + */ + GenericValue& operator=(GenericValue& rhs) RAPIDJSON_NOEXCEPT { + if (RAPIDJSON_LIKELY(this != &rhs)) { + // Can't destroy "this" before assigning "rhs", otherwise "rhs" + // could be used after free if it's an sub-Value of "this", + // hence the temporary dance. + GenericValue temp; + temp.RawAssign(rhs); + this->~GenericValue(); + RawAssign(temp); + } + return *this; + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move assignment in C++11 + GenericValue& operator=(GenericValue&& rhs) RAPIDJSON_NOEXCEPT { + return *this = rhs.Move(); + } +#endif + + //! Assignment of constant string reference (no copy) + /*! \param str Constant string reference to be assigned + \note This overload is needed to avoid clashes with the generic primitive type assignment overload below. + \see GenericStringRef, operator=(T) + */ + GenericValue& operator=(StringRefType str) RAPIDJSON_NOEXCEPT { + GenericValue s(str); + return *this = s; + } + + //! Assignment with primitive types. + /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t + \param value The value to be assigned. + + \note The source type \c T explicitly disallows all pointer types, + especially (\c const) \ref Ch*. This helps avoiding implicitly + referencing character strings with insufficient lifetime, use + \ref SetString(const Ch*, Allocator&) (for copying) or + \ref StringRef() (to explicitly mark the pointer as constant) instead. + All other pointer types would implicitly convert to \c bool, + use \ref SetBool() instead. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::IsPointer), (GenericValue&)) + operator=(T value) { + GenericValue v(value); + return *this = v; + } + + //! Deep-copy assignment from Value + /*! Assigns a \b copy of the Value to the current Value object + \tparam SourceAllocator Allocator type of \c rhs + \param rhs Value to copy from (read-only) + \param allocator Allocator to use for copying + \param copyConstStrings Force copying of constant strings (e.g. referencing an in-situ buffer) + */ + template + GenericValue& CopyFrom(const GenericValue& rhs, Allocator& allocator, bool copyConstStrings = false) { + RAPIDJSON_ASSERT(static_cast(this) != static_cast(&rhs)); + this->~GenericValue(); + new (this) GenericValue(rhs, allocator, copyConstStrings); + return *this; + } + + //! Exchange the contents of this value with those of other. + /*! + \param other Another value. + \note Constant complexity. + */ + GenericValue& Swap(GenericValue& other) RAPIDJSON_NOEXCEPT { + GenericValue temp; + temp.RawAssign(*this); + RawAssign(other); + other.RawAssign(temp); + return *this; + } + + //! free-standing swap function helper + /*! + Helper function to enable support for common swap implementation pattern based on \c std::swap: + \code + void swap(MyClass& a, MyClass& b) { + using std::swap; + swap(a.value, b.value); + // ... + } + \endcode + \see Swap() + */ + friend inline void swap(GenericValue& a, GenericValue& b) RAPIDJSON_NOEXCEPT { a.Swap(b); } + + //! Prepare Value for move semantics + /*! \return *this */ + GenericValue& Move() RAPIDJSON_NOEXCEPT { return *this; } + //@} + + //!@name Equal-to and not-equal-to operators + //@{ + //! Equal-to operator + /*! + \note If an object contains duplicated named member, comparing equality with any object is always \c false. + \note Complexity is quadratic in Object's member number and linear for the rest (number of all values in the subtree and total lengths of all strings). + */ + template + bool operator==(const GenericValue& rhs) const { + typedef GenericValue RhsType; + if (GetType() != rhs.GetType()) + return false; + + switch (GetType()) { + case kObjectType: // Warning: O(n^2) inner-loop + if (data_.o.size != rhs.data_.o.size) + return false; + for (ConstMemberIterator lhsMemberItr = MemberBegin(); lhsMemberItr != MemberEnd(); ++lhsMemberItr) { + typename RhsType::ConstMemberIterator rhsMemberItr = rhs.FindMember(lhsMemberItr->name); + if (rhsMemberItr == rhs.MemberEnd() || lhsMemberItr->value != rhsMemberItr->value) + return false; + } + return true; + + case kArrayType: + if (data_.a.size != rhs.data_.a.size) + return false; + for (SizeType i = 0; i < data_.a.size; i++) + if ((*this)[i] != rhs[i]) + return false; + return true; + + case kStringType: + return StringEqual(rhs); + + case kNumberType: + if (IsDouble() || rhs.IsDouble()) { + double a = GetDouble(); // May convert from integer to double. + double b = rhs.GetDouble(); // Ditto + return a >= b && a <= b; // Prevent -Wfloat-equal + } + else + return data_.n.u64 == rhs.data_.n.u64; + + default: + return true; + } + } + + //! Equal-to operator with const C-string pointer + bool operator==(const Ch* rhs) const { return *this == GenericValue(StringRef(rhs)); } + +#if RAPIDJSON_HAS_STDSTRING + //! Equal-to operator with string object + /*! \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. + */ + bool operator==(const std::basic_string& rhs) const { return *this == GenericValue(StringRef(rhs)); } +#endif + + //! Equal-to operator with primitive types + /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c double, \c true, \c false + */ + template RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr,internal::IsGenericValue >), (bool)) operator==(const T& rhs) const { return *this == GenericValue(rhs); } + +#ifndef __cpp_impl_three_way_comparison + //! Not-equal-to operator + /*! \return !(*this == rhs) + */ + template + bool operator!=(const GenericValue& rhs) const { return !(*this == rhs); } + + //! Not-equal-to operator with const C-string pointer + bool operator!=(const Ch* rhs) const { return !(*this == rhs); } + + //! Not-equal-to operator with arbitrary types + /*! \return !(*this == rhs) + */ + template RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool)) operator!=(const T& rhs) const { return !(*this == rhs); } + + //! Equal-to operator with arbitrary types (symmetric version) + /*! \return (rhs == lhs) + */ + template friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool)) operator==(const T& lhs, const GenericValue& rhs) { return rhs == lhs; } + + //! Not-Equal-to operator with arbitrary types (symmetric version) + /*! \return !(rhs == lhs) + */ + template friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool)) operator!=(const T& lhs, const GenericValue& rhs) { return !(rhs == lhs); } + //@} +#endif + + //!@name Type + //@{ + + Type GetType() const { return static_cast(data_.f.flags & kTypeMask); } + bool IsNull() const { return data_.f.flags == kNullFlag; } + bool IsFalse() const { return data_.f.flags == kFalseFlag; } + bool IsTrue() const { return data_.f.flags == kTrueFlag; } + bool IsBool() const { return (data_.f.flags & kBoolFlag) != 0; } + bool IsObject() const { return data_.f.flags == kObjectFlag; } + bool IsArray() const { return data_.f.flags == kArrayFlag; } + bool IsNumber() const { return (data_.f.flags & kNumberFlag) != 0; } + bool IsInt() const { return (data_.f.flags & kIntFlag) != 0; } + bool IsUint() const { return (data_.f.flags & kUintFlag) != 0; } + bool IsInt64() const { return (data_.f.flags & kInt64Flag) != 0; } + bool IsUint64() const { return (data_.f.flags & kUint64Flag) != 0; } + bool IsDouble() const { return (data_.f.flags & kDoubleFlag) != 0; } + bool IsString() const { return (data_.f.flags & kStringFlag) != 0; } + + // Checks whether a number can be losslessly converted to a double. + bool IsLosslessDouble() const { + if (!IsNumber()) return false; + if (IsUint64()) { + uint64_t u = GetUint64(); + volatile double d = static_cast(u); + return (d >= 0.0) + && (d < static_cast((std::numeric_limits::max)())) + && (u == static_cast(d)); + } + if (IsInt64()) { + int64_t i = GetInt64(); + volatile double d = static_cast(i); + return (d >= static_cast((std::numeric_limits::min)())) + && (d < static_cast((std::numeric_limits::max)())) + && (i == static_cast(d)); + } + return true; // double, int, uint are always lossless + } + + // Checks whether a number is a float (possible lossy). + bool IsFloat() const { + if ((data_.f.flags & kDoubleFlag) == 0) + return false; + double d = GetDouble(); + return d >= -3.4028234e38 && d <= 3.4028234e38; + } + // Checks whether a number can be losslessly converted to a float. + bool IsLosslessFloat() const { + if (!IsNumber()) return false; + double a = GetDouble(); + if (a < static_cast(-(std::numeric_limits::max)()) + || a > static_cast((std::numeric_limits::max)())) + return false; + double b = static_cast(static_cast(a)); + return a >= b && a <= b; // Prevent -Wfloat-equal + } + + //@} + + //!@name Null + //@{ + + GenericValue& SetNull() { this->~GenericValue(); new (this) GenericValue(); return *this; } + + //@} + + //!@name Bool + //@{ + + bool GetBool() const { RAPIDJSON_ASSERT(IsBool()); return data_.f.flags == kTrueFlag; } + //!< Set boolean value + /*! \post IsBool() == true */ + GenericValue& SetBool(bool b) { this->~GenericValue(); new (this) GenericValue(b); return *this; } + + //@} + + //!@name Object + //@{ + + //! Set this value as an empty object. + /*! \post IsObject() == true */ + GenericValue& SetObject() { this->~GenericValue(); new (this) GenericValue(kObjectType); return *this; } + + //! Get the number of members in the object. + SizeType MemberCount() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size; } + + //! Get the capacity of object. + SizeType MemberCapacity() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.capacity; } + + //! Check whether the object is empty. + bool ObjectEmpty() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size == 0; } + + //! Get a value from an object associated with the name. + /*! \pre IsObject() == true + \tparam T Either \c Ch or \c const \c Ch (template used for disambiguation with \ref operator[](SizeType)) + \note In version 0.1x, if the member is not found, this function returns a null value. This makes issue 7. + Since 0.2, if the name is not correct, it will assert. + If user is unsure whether a member exists, user should use HasMember() first. + A better approach is to use FindMember(). + \note Linear time complexity. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::NotExpr::Type, Ch> >),(GenericValue&)) operator[](T* name) { + GenericValue n(StringRef(name)); + return (*this)[n]; + } + template + RAPIDJSON_DISABLEIF_RETURN((internal::NotExpr::Type, Ch> >),(const GenericValue&)) operator[](T* name) const { return const_cast(*this)[name]; } + + //! Get a value from an object associated with the name. + /*! \pre IsObject() == true + \tparam SourceAllocator Allocator of the \c name value + + \note Compared to \ref operator[](T*), this version is faster because it does not need a StrLen(). + And it can also handle strings with embedded null characters. + + \note Linear time complexity. + */ + template + GenericValue& operator[](const GenericValue& name) { + MemberIterator member = FindMember(name); + if (member != MemberEnd()) + return member->value; + else { + RAPIDJSON_ASSERT(false); // see above note + +#if RAPIDJSON_HAS_CXX11 + // Use thread-local storage to prevent races between threads. + // Use static buffer and placement-new to prevent destruction, with + // alignas() to ensure proper alignment. + alignas(GenericValue) thread_local static char buffer[sizeof(GenericValue)]; + return *new (buffer) GenericValue(); +#elif defined(_MSC_VER) && _MSC_VER < 1900 + // There's no way to solve both thread locality and proper alignment + // simultaneously. + __declspec(thread) static char buffer[sizeof(GenericValue)]; + return *new (buffer) GenericValue(); +#elif defined(__GNUC__) || defined(__clang__) + // This will generate -Wexit-time-destructors in clang, but that's + // better than having under-alignment. + __thread static GenericValue buffer; + return buffer; +#else + // Don't know what compiler this is, so don't know how to ensure + // thread-locality. + static GenericValue buffer; + return buffer; +#endif + } + } + template + const GenericValue& operator[](const GenericValue& name) const { return const_cast(*this)[name]; } + +#if RAPIDJSON_HAS_STDSTRING + //! Get a value from an object associated with name (string object). + GenericValue& operator[](const std::basic_string& name) { return (*this)[GenericValue(StringRef(name))]; } + const GenericValue& operator[](const std::basic_string& name) const { return (*this)[GenericValue(StringRef(name))]; } +#endif + + //! Const member iterator + /*! \pre IsObject() == true */ + ConstMemberIterator MemberBegin() const { RAPIDJSON_ASSERT(IsObject()); return ConstMemberIterator(GetMembersPointer()); } + //! Const \em past-the-end member iterator + /*! \pre IsObject() == true */ + ConstMemberIterator MemberEnd() const { RAPIDJSON_ASSERT(IsObject()); return ConstMemberIterator(GetMembersPointer() + data_.o.size); } + //! Member iterator + /*! \pre IsObject() == true */ + MemberIterator MemberBegin() { RAPIDJSON_ASSERT(IsObject()); return MemberIterator(GetMembersPointer()); } + //! \em Past-the-end member iterator + /*! \pre IsObject() == true */ + MemberIterator MemberEnd() { RAPIDJSON_ASSERT(IsObject()); return MemberIterator(GetMembersPointer() + data_.o.size); } + + //! Request the object to have enough capacity to store members. + /*! \param newCapacity The capacity that the object at least need to have. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \note Linear time complexity. + */ + GenericValue& MemberReserve(SizeType newCapacity, Allocator &allocator) { + RAPIDJSON_ASSERT(IsObject()); + DoReserveMembers(newCapacity, allocator); + return *this; + } + + //! Check whether a member exists in the object. + /*! + \param name Member name to be searched. + \pre IsObject() == true + \return Whether a member with that name exists. + \note It is better to use FindMember() directly if you need the obtain the value as well. + \note Linear time complexity. + */ + bool HasMember(const Ch* name) const { return FindMember(name) != MemberEnd(); } + +#if RAPIDJSON_HAS_STDSTRING + //! Check whether a member exists in the object with string object. + /*! + \param name Member name to be searched. + \pre IsObject() == true + \return Whether a member with that name exists. + \note It is better to use FindMember() directly if you need the obtain the value as well. + \note Linear time complexity. + */ + bool HasMember(const std::basic_string& name) const { return FindMember(name) != MemberEnd(); } +#endif + + //! Check whether a member exists in the object with GenericValue name. + /*! + This version is faster because it does not need a StrLen(). It can also handle string with null character. + \param name Member name to be searched. + \pre IsObject() == true + \return Whether a member with that name exists. + \note It is better to use FindMember() directly if you need the obtain the value as well. + \note Linear time complexity. + */ + template + bool HasMember(const GenericValue& name) const { return FindMember(name) != MemberEnd(); } + + //! Find member by name. + /*! + \param name Member name to be searched. + \pre IsObject() == true + \return Iterator to member, if it exists. + Otherwise returns \ref MemberEnd(). + + \note Earlier versions of Rapidjson returned a \c NULL pointer, in case + the requested member doesn't exist. For consistency with e.g. + \c std::map, this has been changed to MemberEnd() now. + \note Linear time complexity. + */ + MemberIterator FindMember(const Ch* name) { + GenericValue n(StringRef(name)); + return FindMember(n); + } + + ConstMemberIterator FindMember(const Ch* name) const { return const_cast(*this).FindMember(name); } + + //! Find member by name. + /*! + This version is faster because it does not need a StrLen(). It can also handle string with null character. + \param name Member name to be searched. + \pre IsObject() == true + \return Iterator to member, if it exists. + Otherwise returns \ref MemberEnd(). + + \note Earlier versions of Rapidjson returned a \c NULL pointer, in case + the requested member doesn't exist. For consistency with e.g. + \c std::map, this has been changed to MemberEnd() now. + \note Linear time complexity. + */ + template + MemberIterator FindMember(const GenericValue& name) { + RAPIDJSON_ASSERT(IsObject()); + RAPIDJSON_ASSERT(name.IsString()); + return DoFindMember(name); + } + template ConstMemberIterator FindMember(const GenericValue& name) const { return const_cast(*this).FindMember(name); } + +#if RAPIDJSON_HAS_STDSTRING + //! Find member by string object name. + /*! + \param name Member name to be searched. + \pre IsObject() == true + \return Iterator to member, if it exists. + Otherwise returns \ref MemberEnd(). + */ + MemberIterator FindMember(const std::basic_string& name) { return FindMember(GenericValue(StringRef(name))); } + ConstMemberIterator FindMember(const std::basic_string& name) const { return FindMember(GenericValue(StringRef(name))); } +#endif + + //! Add a member (name-value pair) to the object. + /*! \param name A string value as name of member. + \param value Value of any type. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \note The ownership of \c name and \c value will be transferred to this object on success. + \pre IsObject() && name.IsString() + \post name.IsNull() && value.IsNull() + \note Amortized Constant time complexity. + */ + GenericValue& AddMember(GenericValue& name, GenericValue& value, Allocator& allocator) { + RAPIDJSON_ASSERT(IsObject()); + RAPIDJSON_ASSERT(name.IsString()); + DoAddMember(name, value, allocator); + return *this; + } + + //! Add a constant string value as member (name-value pair) to the object. + /*! \param name A string value as name of member. + \param value constant string reference as value of member. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \pre IsObject() + \note This overload is needed to avoid clashes with the generic primitive type AddMember(GenericValue&,T,Allocator&) overload below. + \note Amortized Constant time complexity. + */ + GenericValue& AddMember(GenericValue& name, StringRefType value, Allocator& allocator) { + GenericValue v(value); + return AddMember(name, v, allocator); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Add a string object as member (name-value pair) to the object. + /*! \param name A string value as name of member. + \param value constant string reference as value of member. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \pre IsObject() + \note This overload is needed to avoid clashes with the generic primitive type AddMember(GenericValue&,T,Allocator&) overload below. + \note Amortized Constant time complexity. + */ + GenericValue& AddMember(GenericValue& name, std::basic_string& value, Allocator& allocator) { + GenericValue v(value, allocator); + return AddMember(name, v, allocator); + } +#endif + + //! Add any primitive value as member (name-value pair) to the object. + /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t + \param name A string value as name of member. + \param value Value of primitive type \c T as value of member + \param allocator Allocator for reallocating memory. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \pre IsObject() + + \note The source type \c T explicitly disallows all pointer types, + especially (\c const) \ref Ch*. This helps avoiding implicitly + referencing character strings with insufficient lifetime, use + \ref AddMember(StringRefType, GenericValue&, Allocator&) or \ref + AddMember(StringRefType, StringRefType, Allocator&). + All other pointer types would implicitly convert to \c bool, + use an explicit cast instead, if needed. + \note Amortized Constant time complexity. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (GenericValue&)) + AddMember(GenericValue& name, T value, Allocator& allocator) { + GenericValue v(value); + return AddMember(name, v, allocator); + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericValue& AddMember(GenericValue&& name, GenericValue&& value, Allocator& allocator) { + return AddMember(name, value, allocator); + } + GenericValue& AddMember(GenericValue&& name, GenericValue& value, Allocator& allocator) { + return AddMember(name, value, allocator); + } + GenericValue& AddMember(GenericValue& name, GenericValue&& value, Allocator& allocator) { + return AddMember(name, value, allocator); + } + GenericValue& AddMember(StringRefType name, GenericValue&& value, Allocator& allocator) { + GenericValue n(name); + return AddMember(n, value, allocator); + } +#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS + + + //! Add a member (name-value pair) to the object. + /*! \param name A constant string reference as name of member. + \param value Value of any type. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \note The ownership of \c value will be transferred to this object on success. + \pre IsObject() + \post value.IsNull() + \note Amortized Constant time complexity. + */ + GenericValue& AddMember(StringRefType name, GenericValue& value, Allocator& allocator) { + GenericValue n(name); + return AddMember(n, value, allocator); + } + + //! Add a constant string value as member (name-value pair) to the object. + /*! \param name A constant string reference as name of member. + \param value constant string reference as value of member. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \pre IsObject() + \note This overload is needed to avoid clashes with the generic primitive type AddMember(StringRefType,T,Allocator&) overload below. + \note Amortized Constant time complexity. + */ + GenericValue& AddMember(StringRefType name, StringRefType value, Allocator& allocator) { + GenericValue v(value); + return AddMember(name, v, allocator); + } + + //! Add any primitive value as member (name-value pair) to the object. + /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t + \param name A constant string reference as name of member. + \param value Value of primitive type \c T as value of member + \param allocator Allocator for reallocating memory. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \pre IsObject() + + \note The source type \c T explicitly disallows all pointer types, + especially (\c const) \ref Ch*. This helps avoiding implicitly + referencing character strings with insufficient lifetime, use + \ref AddMember(StringRefType, GenericValue&, Allocator&) or \ref + AddMember(StringRefType, StringRefType, Allocator&). + All other pointer types would implicitly convert to \c bool, + use an explicit cast instead, if needed. + \note Amortized Constant time complexity. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (GenericValue&)) + AddMember(StringRefType name, T value, Allocator& allocator) { + GenericValue n(name); + return AddMember(n, value, allocator); + } + + //! Remove all members in the object. + /*! This function do not deallocate memory in the object, i.e. the capacity is unchanged. + \note Linear time complexity. + */ + void RemoveAllMembers() { + RAPIDJSON_ASSERT(IsObject()); + DoClearMembers(); + } + + //! Remove a member in object by its name. + /*! \param name Name of member to be removed. + \return Whether the member existed. + \note This function may reorder the object members. Use \ref + EraseMember(ConstMemberIterator) if you need to preserve the + relative order of the remaining members. + \note Linear time complexity. + */ + bool RemoveMember(const Ch* name) { + GenericValue n(StringRef(name)); + return RemoveMember(n); + } + +#if RAPIDJSON_HAS_STDSTRING + bool RemoveMember(const std::basic_string& name) { return RemoveMember(GenericValue(StringRef(name))); } +#endif + + template + bool RemoveMember(const GenericValue& name) { + MemberIterator m = FindMember(name); + if (m != MemberEnd()) { + RemoveMember(m); + return true; + } + else + return false; + } + + //! Remove a member in object by iterator. + /*! \param m member iterator (obtained by FindMember() or MemberBegin()). + \return the new iterator after removal. + \note This function may reorder the object members. Use \ref + EraseMember(ConstMemberIterator) if you need to preserve the + relative order of the remaining members. + \note Constant time complexity. + */ + MemberIterator RemoveMember(MemberIterator m) { + RAPIDJSON_ASSERT(IsObject()); + RAPIDJSON_ASSERT(data_.o.size > 0); + RAPIDJSON_ASSERT(GetMembersPointer() != 0); + RAPIDJSON_ASSERT(m >= MemberBegin() && m < MemberEnd()); + return DoRemoveMember(m); + } + + //! Remove a member from an object by iterator. + /*! \param pos iterator to the member to remove + \pre IsObject() == true && \ref MemberBegin() <= \c pos < \ref MemberEnd() + \return Iterator following the removed element. + If the iterator \c pos refers to the last element, the \ref MemberEnd() iterator is returned. + \note This function preserves the relative order of the remaining object + members. If you do not need this, use the more efficient \ref RemoveMember(MemberIterator). + \note Linear time complexity. + */ + MemberIterator EraseMember(ConstMemberIterator pos) { + return EraseMember(pos, pos +1); + } + + //! Remove members in the range [first, last) from an object. + /*! \param first iterator to the first member to remove + \param last iterator following the last member to remove + \pre IsObject() == true && \ref MemberBegin() <= \c first <= \c last <= \ref MemberEnd() + \return Iterator following the last removed element. + \note This function preserves the relative order of the remaining object + members. + \note Linear time complexity. + */ + MemberIterator EraseMember(ConstMemberIterator first, ConstMemberIterator last) { + RAPIDJSON_ASSERT(IsObject()); + RAPIDJSON_ASSERT(data_.o.size > 0); + RAPIDJSON_ASSERT(GetMembersPointer() != 0); + RAPIDJSON_ASSERT(first >= MemberBegin()); + RAPIDJSON_ASSERT(first <= last); + RAPIDJSON_ASSERT(last <= MemberEnd()); + return DoEraseMembers(first, last); + } + + //! Erase a member in object by its name. + /*! \param name Name of member to be removed. + \return Whether the member existed. + \note Linear time complexity. + */ + bool EraseMember(const Ch* name) { + GenericValue n(StringRef(name)); + return EraseMember(n); + } + +#if RAPIDJSON_HAS_STDSTRING + bool EraseMember(const std::basic_string& name) { return EraseMember(GenericValue(StringRef(name))); } +#endif + + template + bool EraseMember(const GenericValue& name) { + MemberIterator m = FindMember(name); + if (m != MemberEnd()) { + EraseMember(m); + return true; + } + else + return false; + } + + Object GetObject() { RAPIDJSON_ASSERT(IsObject()); return Object(*this); } + Object GetObj() { RAPIDJSON_ASSERT(IsObject()); return Object(*this); } + ConstObject GetObject() const { RAPIDJSON_ASSERT(IsObject()); return ConstObject(*this); } + ConstObject GetObj() const { RAPIDJSON_ASSERT(IsObject()); return ConstObject(*this); } + + //@} + + //!@name Array + //@{ + + //! Set this value as an empty array. + /*! \post IsArray == true */ + GenericValue& SetArray() { this->~GenericValue(); new (this) GenericValue(kArrayType); return *this; } + + //! Get the number of elements in array. + SizeType Size() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.size; } + + //! Get the capacity of array. + SizeType Capacity() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.capacity; } + + //! Check whether the array is empty. + bool Empty() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.size == 0; } + + //! Remove all elements in the array. + /*! This function do not deallocate memory in the array, i.e. the capacity is unchanged. + \note Linear time complexity. + */ + void Clear() { + RAPIDJSON_ASSERT(IsArray()); + GenericValue* e = GetElementsPointer(); + for (GenericValue* v = e; v != e + data_.a.size; ++v) + v->~GenericValue(); + data_.a.size = 0; + } + + //! Get an element from array by index. + /*! \pre IsArray() == true + \param index Zero-based index of element. + \see operator[](T*) + */ + GenericValue& operator[](SizeType index) { + RAPIDJSON_ASSERT(IsArray()); + RAPIDJSON_ASSERT(index < data_.a.size); + return GetElementsPointer()[index]; + } + const GenericValue& operator[](SizeType index) const { return const_cast(*this)[index]; } + + //! Element iterator + /*! \pre IsArray() == true */ + ValueIterator Begin() { RAPIDJSON_ASSERT(IsArray()); return GetElementsPointer(); } + //! \em Past-the-end element iterator + /*! \pre IsArray() == true */ + ValueIterator End() { RAPIDJSON_ASSERT(IsArray()); return GetElementsPointer() + data_.a.size; } + //! Constant element iterator + /*! \pre IsArray() == true */ + ConstValueIterator Begin() const { return const_cast(*this).Begin(); } + //! Constant \em past-the-end element iterator + /*! \pre IsArray() == true */ + ConstValueIterator End() const { return const_cast(*this).End(); } + + //! Request the array to have enough capacity to store elements. + /*! \param newCapacity The capacity that the array at least need to have. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \note Linear time complexity. + */ + GenericValue& Reserve(SizeType newCapacity, Allocator &allocator) { + RAPIDJSON_ASSERT(IsArray()); + if (newCapacity > data_.a.capacity) { + SetElementsPointer(reinterpret_cast(allocator.Realloc(GetElementsPointer(), data_.a.capacity * sizeof(GenericValue), newCapacity * sizeof(GenericValue)))); + data_.a.capacity = newCapacity; + } + return *this; + } + + //! Append a GenericValue at the end of the array. + /*! \param value Value to be appended. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \pre IsArray() == true + \post value.IsNull() == true + \return The value itself for fluent API. + \note The ownership of \c value will be transferred to this array on success. + \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient. + \note Amortized constant time complexity. + */ + GenericValue& PushBack(GenericValue& value, Allocator& allocator) { + RAPIDJSON_ASSERT(IsArray()); + if (data_.a.size >= data_.a.capacity) + Reserve(data_.a.capacity == 0 ? kDefaultArrayCapacity : (data_.a.capacity + (data_.a.capacity + 1) / 2), allocator); + GetElementsPointer()[data_.a.size++].RawAssign(value); + return *this; + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericValue& PushBack(GenericValue&& value, Allocator& allocator) { + return PushBack(value, allocator); + } +#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS + + //! Append a constant string reference at the end of the array. + /*! \param value Constant string reference to be appended. + \param allocator Allocator for reallocating memory. It must be the same one used previously. Commonly use GenericDocument::GetAllocator(). + \pre IsArray() == true + \return The value itself for fluent API. + \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient. + \note Amortized constant time complexity. + \see GenericStringRef + */ + GenericValue& PushBack(StringRefType value, Allocator& allocator) { + return (*this).template PushBack(value, allocator); + } + + //! Append a primitive value at the end of the array. + /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t + \param value Value of primitive type T to be appended. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \pre IsArray() == true + \return The value itself for fluent API. + \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient. + + \note The source type \c T explicitly disallows all pointer types, + especially (\c const) \ref Ch*. This helps avoiding implicitly + referencing character strings with insufficient lifetime, use + \ref PushBack(GenericValue&, Allocator&) or \ref + PushBack(StringRefType, Allocator&). + All other pointer types would implicitly convert to \c bool, + use an explicit cast instead, if needed. + \note Amortized constant time complexity. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (GenericValue&)) + PushBack(T value, Allocator& allocator) { + GenericValue v(value); + return PushBack(v, allocator); + } + + //! Remove the last element in the array. + /*! + \note Constant time complexity. + */ + GenericValue& PopBack() { + RAPIDJSON_ASSERT(IsArray()); + RAPIDJSON_ASSERT(!Empty()); + GetElementsPointer()[--data_.a.size].~GenericValue(); + return *this; + } + + //! Remove an element of array by iterator. + /*! + \param pos iterator to the element to remove + \pre IsArray() == true && \ref Begin() <= \c pos < \ref End() + \return Iterator following the removed element. If the iterator pos refers to the last element, the End() iterator is returned. + \note Linear time complexity. + */ + ValueIterator Erase(ConstValueIterator pos) { + return Erase(pos, pos + 1); + } + + //! Remove elements in the range [first, last) of the array. + /*! + \param first iterator to the first element to remove + \param last iterator following the last element to remove + \pre IsArray() == true && \ref Begin() <= \c first <= \c last <= \ref End() + \return Iterator following the last removed element. + \note Linear time complexity. + */ + ValueIterator Erase(ConstValueIterator first, ConstValueIterator last) { + RAPIDJSON_ASSERT(IsArray()); + RAPIDJSON_ASSERT(data_.a.size > 0); + RAPIDJSON_ASSERT(GetElementsPointer() != 0); + RAPIDJSON_ASSERT(first >= Begin()); + RAPIDJSON_ASSERT(first <= last); + RAPIDJSON_ASSERT(last <= End()); + ValueIterator pos = Begin() + (first - Begin()); + for (ValueIterator itr = pos; itr != last; ++itr) + itr->~GenericValue(); + std::memmove(static_cast(pos), last, static_cast(End() - last) * sizeof(GenericValue)); + data_.a.size -= static_cast(last - first); + return pos; + } + + Array GetArray() { RAPIDJSON_ASSERT(IsArray()); return Array(*this); } + ConstArray GetArray() const { RAPIDJSON_ASSERT(IsArray()); return ConstArray(*this); } + + //@} + + //!@name Number + //@{ + + int GetInt() const { RAPIDJSON_ASSERT(data_.f.flags & kIntFlag); return data_.n.i.i; } + unsigned GetUint() const { RAPIDJSON_ASSERT(data_.f.flags & kUintFlag); return data_.n.u.u; } + int64_t GetInt64() const { RAPIDJSON_ASSERT(data_.f.flags & kInt64Flag); return data_.n.i64; } + uint64_t GetUint64() const { RAPIDJSON_ASSERT(data_.f.flags & kUint64Flag); return data_.n.u64; } + + //! Get the value as double type. + /*! \note If the value is 64-bit integer type, it may lose precision. Use \c IsLosslessDouble() to check whether the conversion is lossless. + */ + double GetDouble() const { + RAPIDJSON_ASSERT(IsNumber()); + if ((data_.f.flags & kDoubleFlag) != 0) return data_.n.d; // exact type, no conversion. + if ((data_.f.flags & kIntFlag) != 0) return data_.n.i.i; // int -> double + if ((data_.f.flags & kUintFlag) != 0) return data_.n.u.u; // unsigned -> double + if ((data_.f.flags & kInt64Flag) != 0) return static_cast(data_.n.i64); // int64_t -> double (may lose precision) + RAPIDJSON_ASSERT((data_.f.flags & kUint64Flag) != 0); return static_cast(data_.n.u64); // uint64_t -> double (may lose precision) + } + + //! Get the value as float type. + /*! \note If the value is 64-bit integer type, it may lose precision. Use \c IsLosslessFloat() to check whether the conversion is lossless. + */ + float GetFloat() const { + return static_cast(GetDouble()); + } + + GenericValue& SetInt(int i) { this->~GenericValue(); new (this) GenericValue(i); return *this; } + GenericValue& SetUint(unsigned u) { this->~GenericValue(); new (this) GenericValue(u); return *this; } + GenericValue& SetInt64(int64_t i64) { this->~GenericValue(); new (this) GenericValue(i64); return *this; } + GenericValue& SetUint64(uint64_t u64) { this->~GenericValue(); new (this) GenericValue(u64); return *this; } + GenericValue& SetDouble(double d) { this->~GenericValue(); new (this) GenericValue(d); return *this; } + GenericValue& SetFloat(float f) { this->~GenericValue(); new (this) GenericValue(static_cast(f)); return *this; } + + //@} + + //!@name String + //@{ + + const Ch* GetString() const { RAPIDJSON_ASSERT(IsString()); return DataString(data_); } + + //! Get the length of string. + /*! Since rapidjson permits "\\u0000" in the json string, strlen(v.GetString()) may not equal to v.GetStringLength(). + */ + SizeType GetStringLength() const { RAPIDJSON_ASSERT(IsString()); return DataStringLength(data_); } + + //! Set this value as a string without copying source string. + /*! This version has better performance with supplied length, and also support string containing null character. + \param s source string pointer. + \param length The length of source string, excluding the trailing null terminator. + \return The value itself for fluent API. + \post IsString() == true && GetString() == s && GetStringLength() == length + \see SetString(StringRefType) + */ + GenericValue& SetString(const Ch* s, SizeType length) { return SetString(StringRef(s, length)); } + + //! Set this value as a string without copying source string. + /*! \param s source string reference + \return The value itself for fluent API. + \post IsString() == true && GetString() == s && GetStringLength() == s.length + */ + GenericValue& SetString(StringRefType s) { this->~GenericValue(); SetStringRaw(s); return *this; } + + //! Set this value as a string by copying from source string. + /*! This version has better performance with supplied length, and also support string containing null character. + \param s source string. + \param length The length of source string, excluding the trailing null terminator. + \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \post IsString() == true && GetString() != s && strcmp(GetString(),s) == 0 && GetStringLength() == length + */ + GenericValue& SetString(const Ch* s, SizeType length, Allocator& allocator) { return SetString(StringRef(s, length), allocator); } + + //! Set this value as a string by copying from source string. + /*! \param s source string. + \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \post IsString() == true && GetString() != s && strcmp(GetString(),s) == 0 && GetStringLength() == length + */ + GenericValue& SetString(const Ch* s, Allocator& allocator) { return SetString(StringRef(s), allocator); } + + //! Set this value as a string by copying from source string. + /*! \param s source string reference + \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \post IsString() == true && GetString() != s.s && strcmp(GetString(),s) == 0 && GetStringLength() == length + */ + GenericValue& SetString(StringRefType s, Allocator& allocator) { this->~GenericValue(); SetStringRaw(s, allocator); return *this; } + +#if RAPIDJSON_HAS_STDSTRING + //! Set this value as a string by copying from source string. + /*! \param s source string. + \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \post IsString() == true && GetString() != s.data() && strcmp(GetString(),s.data() == 0 && GetStringLength() == s.size() + \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. + */ + GenericValue& SetString(const std::basic_string& s, Allocator& allocator) { return SetString(StringRef(s), allocator); } +#endif + + //@} + + //!@name Array + //@{ + + //! Templated version for checking whether this value is type T. + /*! + \tparam T Either \c bool, \c int, \c unsigned, \c int64_t, \c uint64_t, \c double, \c float, \c const \c char*, \c std::basic_string + */ + template + bool Is() const { return internal::TypeHelper::Is(*this); } + + template + T Get() const { return internal::TypeHelper::Get(*this); } + + template + T Get() { return internal::TypeHelper::Get(*this); } + + template + ValueType& Set(const T& data) { return internal::TypeHelper::Set(*this, data); } + + template + ValueType& Set(const T& data, AllocatorType& allocator) { return internal::TypeHelper::Set(*this, data, allocator); } + + //@} + + //! Generate events of this value to a Handler. + /*! This function adopts the GoF visitor pattern. + Typical usage is to output this JSON value as JSON text via Writer, which is a Handler. + It can also be used to deep clone this value via GenericDocument, which is also a Handler. + \tparam Handler type of handler. + \param handler An object implementing concept Handler. + */ + template + bool Accept(Handler& handler) const { + switch(GetType()) { + case kNullType: return handler.Null(); + case kFalseType: return handler.Bool(false); + case kTrueType: return handler.Bool(true); + + case kObjectType: + if (RAPIDJSON_UNLIKELY(!handler.StartObject())) + return false; + for (ConstMemberIterator m = MemberBegin(); m != MemberEnd(); ++m) { + RAPIDJSON_ASSERT(m->name.IsString()); // User may change the type of name by MemberIterator. + if (RAPIDJSON_UNLIKELY(!handler.Key(m->name.GetString(), m->name.GetStringLength(), (m->name.data_.f.flags & kCopyFlag) != 0))) + return false; + if (RAPIDJSON_UNLIKELY(!m->value.Accept(handler))) + return false; + } + return handler.EndObject(data_.o.size); + + case kArrayType: + if (RAPIDJSON_UNLIKELY(!handler.StartArray())) + return false; + for (ConstValueIterator v = Begin(); v != End(); ++v) + if (RAPIDJSON_UNLIKELY(!v->Accept(handler))) + return false; + return handler.EndArray(data_.a.size); + + case kStringType: + return handler.String(GetString(), GetStringLength(), (data_.f.flags & kCopyFlag) != 0); + + default: + RAPIDJSON_ASSERT(GetType() == kNumberType); + if (IsDouble()) return handler.Double(data_.n.d); + else if (IsInt()) return handler.Int(data_.n.i.i); + else if (IsUint()) return handler.Uint(data_.n.u.u); + else if (IsInt64()) return handler.Int64(data_.n.i64); + else return handler.Uint64(data_.n.u64); + } + } + +private: + template friend class GenericValue; + template friend class GenericDocument; + + enum { + kBoolFlag = 0x0008, + kNumberFlag = 0x0010, + kIntFlag = 0x0020, + kUintFlag = 0x0040, + kInt64Flag = 0x0080, + kUint64Flag = 0x0100, + kDoubleFlag = 0x0200, + kStringFlag = 0x0400, + kCopyFlag = 0x0800, + kInlineStrFlag = 0x1000, + + // Initial flags of different types. + kNullFlag = kNullType, + // These casts are added to suppress the warning on MSVC about bitwise operations between enums of different types. + kTrueFlag = static_cast(kTrueType) | static_cast(kBoolFlag), + kFalseFlag = static_cast(kFalseType) | static_cast(kBoolFlag), + kNumberIntFlag = static_cast(kNumberType) | static_cast(kNumberFlag | kIntFlag | kInt64Flag), + kNumberUintFlag = static_cast(kNumberType) | static_cast(kNumberFlag | kUintFlag | kUint64Flag | kInt64Flag), + kNumberInt64Flag = static_cast(kNumberType) | static_cast(kNumberFlag | kInt64Flag), + kNumberUint64Flag = static_cast(kNumberType) | static_cast(kNumberFlag | kUint64Flag), + kNumberDoubleFlag = static_cast(kNumberType) | static_cast(kNumberFlag | kDoubleFlag), + kNumberAnyFlag = static_cast(kNumberType) | static_cast(kNumberFlag | kIntFlag | kInt64Flag | kUintFlag | kUint64Flag | kDoubleFlag), + kConstStringFlag = static_cast(kStringType) | static_cast(kStringFlag), + kCopyStringFlag = static_cast(kStringType) | static_cast(kStringFlag | kCopyFlag), + kShortStringFlag = static_cast(kStringType) | static_cast(kStringFlag | kCopyFlag | kInlineStrFlag), + kObjectFlag = kObjectType, + kArrayFlag = kArrayType, + + kTypeMask = 0x07 + }; + + static const SizeType kDefaultArrayCapacity = RAPIDJSON_VALUE_DEFAULT_ARRAY_CAPACITY; + static const SizeType kDefaultObjectCapacity = RAPIDJSON_VALUE_DEFAULT_OBJECT_CAPACITY; + + struct Flag { +#if RAPIDJSON_48BITPOINTER_OPTIMIZATION + char payload[sizeof(SizeType) * 2 + 6]; // 2 x SizeType + lower 48-bit pointer +#elif RAPIDJSON_64BIT + char payload[sizeof(SizeType) * 2 + sizeof(void*) + 6]; // 6 padding bytes +#else + char payload[sizeof(SizeType) * 2 + sizeof(void*) + 2]; // 2 padding bytes +#endif + uint16_t flags; + }; + + struct String { + SizeType length; + SizeType hashcode; //!< reserved + const Ch* str; + }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode + + // implementation detail: ShortString can represent zero-terminated strings up to MaxSize chars + // (excluding the terminating zero) and store a value to determine the length of the contained + // string in the last character str[LenPos] by storing "MaxSize - length" there. If the string + // to store has the maximal length of MaxSize then str[LenPos] will be 0 and therefore act as + // the string terminator as well. For getting the string length back from that value just use + // "MaxSize - str[LenPos]". + // This allows to store 13-chars strings in 32-bit mode, 21-chars strings in 64-bit mode, + // 13-chars strings for RAPIDJSON_48BITPOINTER_OPTIMIZATION=1 inline (for `UTF8`-encoded strings). + struct ShortString { + enum { MaxChars = sizeof(static_cast(0)->payload) / sizeof(Ch), MaxSize = MaxChars - 1, LenPos = MaxSize }; + Ch str[MaxChars]; + + inline static bool Usable(SizeType len) { return (MaxSize >= len); } + inline void SetLength(SizeType len) { str[LenPos] = static_cast(MaxSize - len); } + inline SizeType GetLength() const { return static_cast(MaxSize - str[LenPos]); } + }; // at most as many bytes as "String" above => 12 bytes in 32-bit mode, 16 bytes in 64-bit mode + + // By using proper binary layout, retrieval of different integer types do not need conversions. + union Number { +#if RAPIDJSON_ENDIAN == RAPIDJSON_LITTLEENDIAN + struct I { + int i; + char padding[4]; + }i; + struct U { + unsigned u; + char padding2[4]; + }u; +#else + struct I { + char padding[4]; + int i; + }i; + struct U { + char padding2[4]; + unsigned u; + }u; +#endif + int64_t i64; + uint64_t u64; + double d; + }; // 8 bytes + + struct ObjectData { + SizeType size; + SizeType capacity; + Member* members; + }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode + + struct ArrayData { + SizeType size; + SizeType capacity; + GenericValue* elements; + }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode + + union Data { + String s; + ShortString ss; + Number n; + ObjectData o; + ArrayData a; + Flag f; + }; // 16 bytes in 32-bit mode, 24 bytes in 64-bit mode, 16 bytes in 64-bit with RAPIDJSON_48BITPOINTER_OPTIMIZATION + + static RAPIDJSON_FORCEINLINE const Ch* DataString(const Data& data) { + return (data.f.flags & kInlineStrFlag) ? data.ss.str : RAPIDJSON_GETPOINTER(Ch, data.s.str); + } + static RAPIDJSON_FORCEINLINE SizeType DataStringLength(const Data& data) { + return (data.f.flags & kInlineStrFlag) ? data.ss.GetLength() : data.s.length; + } + + RAPIDJSON_FORCEINLINE const Ch* GetStringPointer() const { return RAPIDJSON_GETPOINTER(Ch, data_.s.str); } + RAPIDJSON_FORCEINLINE const Ch* SetStringPointer(const Ch* str) { return RAPIDJSON_SETPOINTER(Ch, data_.s.str, str); } + RAPIDJSON_FORCEINLINE GenericValue* GetElementsPointer() const { return RAPIDJSON_GETPOINTER(GenericValue, data_.a.elements); } + RAPIDJSON_FORCEINLINE GenericValue* SetElementsPointer(GenericValue* elements) { return RAPIDJSON_SETPOINTER(GenericValue, data_.a.elements, elements); } + RAPIDJSON_FORCEINLINE Member* GetMembersPointer() const { return RAPIDJSON_GETPOINTER(Member, data_.o.members); } + RAPIDJSON_FORCEINLINE Member* SetMembersPointer(Member* members) { return RAPIDJSON_SETPOINTER(Member, data_.o.members, members); } + +#if RAPIDJSON_USE_MEMBERSMAP + + struct MapTraits { + struct Less { + bool operator()(const Data& s1, const Data& s2) const { + SizeType n1 = DataStringLength(s1), n2 = DataStringLength(s2); + int cmp = std::memcmp(DataString(s1), DataString(s2), sizeof(Ch) * (n1 < n2 ? n1 : n2)); + return cmp < 0 || (cmp == 0 && n1 < n2); + } + }; + typedef std::pair Pair; + typedef std::multimap > Map; + typedef typename Map::iterator Iterator; + }; + typedef typename MapTraits::Map Map; + typedef typename MapTraits::Less MapLess; + typedef typename MapTraits::Pair MapPair; + typedef typename MapTraits::Iterator MapIterator; + + // + // Layout of the members' map/array, re(al)located according to the needed capacity: + // + // {Map*}<>{capacity}<>{Member[capacity]}<>{MapIterator[capacity]} + // + // (where <> stands for the RAPIDJSON_ALIGN-ment, if needed) + // + + static RAPIDJSON_FORCEINLINE size_t GetMapLayoutSize(SizeType capacity) { + return RAPIDJSON_ALIGN(sizeof(Map*)) + + RAPIDJSON_ALIGN(sizeof(SizeType)) + + RAPIDJSON_ALIGN(capacity * sizeof(Member)) + + capacity * sizeof(MapIterator); + } + + static RAPIDJSON_FORCEINLINE SizeType &GetMapCapacity(Map* &map) { + return *reinterpret_cast(reinterpret_cast(&map) + + RAPIDJSON_ALIGN(sizeof(Map*))); + } + + static RAPIDJSON_FORCEINLINE Member* GetMapMembers(Map* &map) { + return reinterpret_cast(reinterpret_cast(&map) + + RAPIDJSON_ALIGN(sizeof(Map*)) + + RAPIDJSON_ALIGN(sizeof(SizeType))); + } + + static RAPIDJSON_FORCEINLINE MapIterator* GetMapIterators(Map* &map) { + return reinterpret_cast(reinterpret_cast(&map) + + RAPIDJSON_ALIGN(sizeof(Map*)) + + RAPIDJSON_ALIGN(sizeof(SizeType)) + + RAPIDJSON_ALIGN(GetMapCapacity(map) * sizeof(Member))); + } + + static RAPIDJSON_FORCEINLINE Map* &GetMap(Member* members) { + RAPIDJSON_ASSERT(members != 0); + return *reinterpret_cast(reinterpret_cast(members) - + RAPIDJSON_ALIGN(sizeof(SizeType)) - + RAPIDJSON_ALIGN(sizeof(Map*))); + } + + // Some compilers' debug mechanisms want all iterators to be destroyed, for their accounting.. + RAPIDJSON_FORCEINLINE MapIterator DropMapIterator(MapIterator& rhs) { +#if RAPIDJSON_HAS_CXX11 + MapIterator ret = std::move(rhs); +#else + MapIterator ret = rhs; +#endif + rhs.~MapIterator(); + return ret; + } + + Map* &DoReallocMap(Map** oldMap, SizeType newCapacity, Allocator& allocator) { + Map **newMap = static_cast(allocator.Malloc(GetMapLayoutSize(newCapacity))); + GetMapCapacity(*newMap) = newCapacity; + if (!oldMap) { + *newMap = new (allocator.Malloc(sizeof(Map))) Map(MapLess(), allocator); + } + else { + *newMap = *oldMap; + size_t count = (*oldMap)->size(); + std::memcpy(static_cast(GetMapMembers(*newMap)), + static_cast(GetMapMembers(*oldMap)), + count * sizeof(Member)); + MapIterator *oldIt = GetMapIterators(*oldMap), + *newIt = GetMapIterators(*newMap); + while (count--) { + new (&newIt[count]) MapIterator(DropMapIterator(oldIt[count])); + } + Allocator::Free(oldMap); + } + return *newMap; + } + + RAPIDJSON_FORCEINLINE Member* DoAllocMembers(SizeType capacity, Allocator& allocator) { + return GetMapMembers(DoReallocMap(0, capacity, allocator)); + } + + void DoReserveMembers(SizeType newCapacity, Allocator& allocator) { + ObjectData& o = data_.o; + if (newCapacity > o.capacity) { + Member* oldMembers = GetMembersPointer(); + Map **oldMap = oldMembers ? &GetMap(oldMembers) : 0, + *&newMap = DoReallocMap(oldMap, newCapacity, allocator); + RAPIDJSON_SETPOINTER(Member, o.members, GetMapMembers(newMap)); + o.capacity = newCapacity; + } + } + + template + MemberIterator DoFindMember(const GenericValue& name) { + if (Member* members = GetMembersPointer()) { + Map* &map = GetMap(members); + MapIterator mit = map->find(reinterpret_cast(name.data_)); + if (mit != map->end()) { + return MemberIterator(&members[mit->second]); + } + } + return MemberEnd(); + } + + void DoClearMembers() { + if (Member* members = GetMembersPointer()) { + Map* &map = GetMap(members); + MapIterator* mit = GetMapIterators(map); + for (SizeType i = 0; i < data_.o.size; i++) { + map->erase(DropMapIterator(mit[i])); + members[i].~Member(); + } + data_.o.size = 0; + } + } + + void DoFreeMembers() { + if (Member* members = GetMembersPointer()) { + GetMap(members)->~Map(); + for (SizeType i = 0; i < data_.o.size; i++) { + members[i].~Member(); + } + if (Allocator::kNeedFree) { // Shortcut by Allocator's trait + Map** map = &GetMap(members); + Allocator::Free(*map); + Allocator::Free(map); + } + } + } + +#else // !RAPIDJSON_USE_MEMBERSMAP + + RAPIDJSON_FORCEINLINE Member* DoAllocMembers(SizeType capacity, Allocator& allocator) { + return Malloc(allocator, capacity); + } + + void DoReserveMembers(SizeType newCapacity, Allocator& allocator) { + ObjectData& o = data_.o; + if (newCapacity > o.capacity) { + Member* newMembers = Realloc(allocator, GetMembersPointer(), o.capacity, newCapacity); + RAPIDJSON_SETPOINTER(Member, o.members, newMembers); + o.capacity = newCapacity; + } + } + + template + MemberIterator DoFindMember(const GenericValue& name) { + MemberIterator member = MemberBegin(); + for ( ; member != MemberEnd(); ++member) + if (name.StringEqual(member->name)) + break; + return member; + } + + void DoClearMembers() { + for (MemberIterator m = MemberBegin(); m != MemberEnd(); ++m) + m->~Member(); + data_.o.size = 0; + } + + void DoFreeMembers() { + for (MemberIterator m = MemberBegin(); m != MemberEnd(); ++m) + m->~Member(); + Allocator::Free(GetMembersPointer()); + } + +#endif // !RAPIDJSON_USE_MEMBERSMAP + + void DoAddMember(GenericValue& name, GenericValue& value, Allocator& allocator) { + ObjectData& o = data_.o; + if (o.size >= o.capacity) + DoReserveMembers(o.capacity ? (o.capacity + (o.capacity + 1) / 2) : kDefaultObjectCapacity, allocator); + Member* members = GetMembersPointer(); + Member* m = members + o.size; + m->name.RawAssign(name); + m->value.RawAssign(value); +#if RAPIDJSON_USE_MEMBERSMAP + Map* &map = GetMap(members); + MapIterator* mit = GetMapIterators(map); + new (&mit[o.size]) MapIterator(map->insert(MapPair(m->name.data_, o.size))); +#endif + ++o.size; + } + + MemberIterator DoRemoveMember(MemberIterator m) { + ObjectData& o = data_.o; + Member* members = GetMembersPointer(); +#if RAPIDJSON_USE_MEMBERSMAP + Map* &map = GetMap(members); + MapIterator* mit = GetMapIterators(map); + SizeType mpos = static_cast(&*m - members); + map->erase(DropMapIterator(mit[mpos])); +#endif + MemberIterator last(members + (o.size - 1)); + if (o.size > 1 && m != last) { +#if RAPIDJSON_USE_MEMBERSMAP + new (&mit[mpos]) MapIterator(DropMapIterator(mit[&*last - members])); + mit[mpos]->second = mpos; +#endif + *m = *last; // Move the last one to this place + } + else { + m->~Member(); // Only one left, just destroy + } + --o.size; + return m; + } + + MemberIterator DoEraseMembers(ConstMemberIterator first, ConstMemberIterator last) { + ObjectData& o = data_.o; + MemberIterator beg = MemberBegin(), + pos = beg + (first - beg), + end = MemberEnd(); +#if RAPIDJSON_USE_MEMBERSMAP + Map* &map = GetMap(GetMembersPointer()); + MapIterator* mit = GetMapIterators(map); +#endif + for (MemberIterator itr = pos; itr != last; ++itr) { +#if RAPIDJSON_USE_MEMBERSMAP + map->erase(DropMapIterator(mit[itr - beg])); +#endif + itr->~Member(); + } +#if RAPIDJSON_USE_MEMBERSMAP + if (first != last) { + // Move remaining members/iterators + MemberIterator next = pos + (last - first); + for (MemberIterator itr = pos; next != end; ++itr, ++next) { + std::memcpy(static_cast(&*itr), &*next, sizeof(Member)); + SizeType mpos = static_cast(itr - beg); + new (&mit[mpos]) MapIterator(DropMapIterator(mit[next - beg])); + mit[mpos]->second = mpos; + } + } +#else + std::memmove(static_cast(&*pos), &*last, + static_cast(end - last) * sizeof(Member)); +#endif + o.size -= static_cast(last - first); + return pos; + } + + template + void DoCopyMembers(const GenericValue& rhs, Allocator& allocator, bool copyConstStrings) { + RAPIDJSON_ASSERT(rhs.GetType() == kObjectType); + + data_.f.flags = kObjectFlag; + SizeType count = rhs.data_.o.size; + Member* lm = DoAllocMembers(count, allocator); + const typename GenericValue::Member* rm = rhs.GetMembersPointer(); +#if RAPIDJSON_USE_MEMBERSMAP + Map* &map = GetMap(lm); + MapIterator* mit = GetMapIterators(map); +#endif + for (SizeType i = 0; i < count; i++) { + new (&lm[i].name) GenericValue(rm[i].name, allocator, copyConstStrings); + new (&lm[i].value) GenericValue(rm[i].value, allocator, copyConstStrings); +#if RAPIDJSON_USE_MEMBERSMAP + new (&mit[i]) MapIterator(map->insert(MapPair(lm[i].name.data_, i))); +#endif + } + data_.o.size = data_.o.capacity = count; + SetMembersPointer(lm); + } + + // Initialize this value as array with initial data, without calling destructor. + void SetArrayRaw(GenericValue* values, SizeType count, Allocator& allocator) { + data_.f.flags = kArrayFlag; + if (count) { + GenericValue* e = static_cast(allocator.Malloc(count * sizeof(GenericValue))); + SetElementsPointer(e); + std::memcpy(static_cast(e), values, count * sizeof(GenericValue)); + } + else + SetElementsPointer(0); + data_.a.size = data_.a.capacity = count; + } + + //! Initialize this value as object with initial data, without calling destructor. + void SetObjectRaw(Member* members, SizeType count, Allocator& allocator) { + data_.f.flags = kObjectFlag; + if (count) { + Member* m = DoAllocMembers(count, allocator); + SetMembersPointer(m); + std::memcpy(static_cast(m), members, count * sizeof(Member)); +#if RAPIDJSON_USE_MEMBERSMAP + Map* &map = GetMap(m); + MapIterator* mit = GetMapIterators(map); + for (SizeType i = 0; i < count; i++) { + new (&mit[i]) MapIterator(map->insert(MapPair(m[i].name.data_, i))); + } +#endif + } + else + SetMembersPointer(0); + data_.o.size = data_.o.capacity = count; + } + + //! Initialize this value as constant string, without calling destructor. + void SetStringRaw(StringRefType s) RAPIDJSON_NOEXCEPT { + data_.f.flags = kConstStringFlag; + SetStringPointer(s); + data_.s.length = s.length; + } + + //! Initialize this value as copy string with initial data, without calling destructor. + void SetStringRaw(StringRefType s, Allocator& allocator) { + Ch* str = 0; + if (ShortString::Usable(s.length)) { + data_.f.flags = kShortStringFlag; + data_.ss.SetLength(s.length); + str = data_.ss.str; + } else { + data_.f.flags = kCopyStringFlag; + data_.s.length = s.length; + str = static_cast(allocator.Malloc((s.length + 1) * sizeof(Ch))); + SetStringPointer(str); + } + std::memcpy(str, s, s.length * sizeof(Ch)); + str[s.length] = '\0'; + } + + //! Assignment without calling destructor + void RawAssign(GenericValue& rhs) RAPIDJSON_NOEXCEPT { + data_ = rhs.data_; + // data_.f.flags = rhs.data_.f.flags; + rhs.data_.f.flags = kNullFlag; + } + + template + bool StringEqual(const GenericValue& rhs) const { + RAPIDJSON_ASSERT(IsString()); + RAPIDJSON_ASSERT(rhs.IsString()); + + const SizeType len1 = GetStringLength(); + const SizeType len2 = rhs.GetStringLength(); + if(len1 != len2) { return false; } + + const Ch* const str1 = GetString(); + const Ch* const str2 = rhs.GetString(); + if(str1 == str2) { return true; } // fast path for constant string + + return (std::memcmp(str1, str2, sizeof(Ch) * len1) == 0); + } + + Data data_; +}; + +//! GenericValue with UTF8 encoding +typedef GenericValue > Value; + +/////////////////////////////////////////////////////////////////////////////// +// GenericDocument + +//! A document for parsing JSON text as DOM. +/*! + \note implements Handler concept + \tparam Encoding Encoding for both parsing and string storage. + \tparam Allocator Allocator for allocating memory for the DOM + \tparam StackAllocator Allocator for allocating memory for stack during parsing. + \warning Although GenericDocument inherits from GenericValue, the API does \b not provide any virtual functions, especially no virtual destructor. To avoid memory leaks, do not \c delete a GenericDocument object via a pointer to a GenericValue. +*/ +template +class GenericDocument : public GenericValue { +public: + typedef typename Encoding::Ch Ch; //!< Character type derived from Encoding. + typedef GenericValue ValueType; //!< Value type of the document. + typedef Allocator AllocatorType; //!< Allocator type from template parameter. + typedef StackAllocator StackAllocatorType; //!< StackAllocator type from template parameter. + + //! Constructor + /*! Creates an empty document of specified type. + \param type Mandatory type of object to create. + \param allocator Optional allocator for allocating memory. + \param stackCapacity Optional initial capacity of stack in bytes. + \param stackAllocator Optional allocator for allocating memory for stack. + */ + explicit GenericDocument(Type type, Allocator* allocator = 0, size_t stackCapacity = kDefaultStackCapacity, StackAllocator* stackAllocator = 0) : + GenericValue(type), allocator_(allocator), ownAllocator_(0), stack_(stackAllocator, stackCapacity), parseResult_() + { + if (!allocator_) + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)(); + } + + //! Constructor + /*! Creates an empty document which type is Null. + \param allocator Optional allocator for allocating memory. + \param stackCapacity Optional initial capacity of stack in bytes. + \param stackAllocator Optional allocator for allocating memory for stack. + */ + GenericDocument(Allocator* allocator = 0, size_t stackCapacity = kDefaultStackCapacity, StackAllocator* stackAllocator = 0) : + allocator_(allocator), ownAllocator_(0), stack_(stackAllocator, stackCapacity), parseResult_() + { + if (!allocator_) + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)(); + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move constructor in C++11 + GenericDocument(GenericDocument&& rhs) RAPIDJSON_NOEXCEPT + : ValueType(std::forward(rhs)), // explicit cast to avoid prohibited move from Document + allocator_(rhs.allocator_), + ownAllocator_(rhs.ownAllocator_), + stack_(std::move(rhs.stack_)), + parseResult_(rhs.parseResult_) + { + rhs.allocator_ = 0; + rhs.ownAllocator_ = 0; + rhs.parseResult_ = ParseResult(); + } +#endif + + ~GenericDocument() { + // Clear the ::ValueType before ownAllocator is destroyed, ~ValueType() + // runs last and may access its elements or members which would be freed + // with an allocator like MemoryPoolAllocator (CrtAllocator does not + // free its data when destroyed, but MemoryPoolAllocator does). + if (ownAllocator_) { + ValueType::SetNull(); + } + Destroy(); + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move assignment in C++11 + GenericDocument& operator=(GenericDocument&& rhs) RAPIDJSON_NOEXCEPT + { + // The cast to ValueType is necessary here, because otherwise it would + // attempt to call GenericValue's templated assignment operator. + ValueType::operator=(std::forward(rhs)); + + // Calling the destructor here would prematurely call stack_'s destructor + Destroy(); + + allocator_ = rhs.allocator_; + ownAllocator_ = rhs.ownAllocator_; + stack_ = std::move(rhs.stack_); + parseResult_ = rhs.parseResult_; + + rhs.allocator_ = 0; + rhs.ownAllocator_ = 0; + rhs.parseResult_ = ParseResult(); + + return *this; + } +#endif + + //! Exchange the contents of this document with those of another. + /*! + \param rhs Another document. + \note Constant complexity. + \see GenericValue::Swap + */ + GenericDocument& Swap(GenericDocument& rhs) RAPIDJSON_NOEXCEPT { + ValueType::Swap(rhs); + stack_.Swap(rhs.stack_); + internal::Swap(allocator_, rhs.allocator_); + internal::Swap(ownAllocator_, rhs.ownAllocator_); + internal::Swap(parseResult_, rhs.parseResult_); + return *this; + } + + // Allow Swap with ValueType. + // Refer to Effective C++ 3rd Edition/Item 33: Avoid hiding inherited names. + using ValueType::Swap; + + //! free-standing swap function helper + /*! + Helper function to enable support for common swap implementation pattern based on \c std::swap: + \code + void swap(MyClass& a, MyClass& b) { + using std::swap; + swap(a.doc, b.doc); + // ... + } + \endcode + \see Swap() + */ + friend inline void swap(GenericDocument& a, GenericDocument& b) RAPIDJSON_NOEXCEPT { a.Swap(b); } + + //! Populate this document by a generator which produces SAX events. + /*! \tparam Generator A functor with bool f(Handler) prototype. + \param g Generator functor which sends SAX events to the parameter. + \return The document itself for fluent API. + */ + template + GenericDocument& Populate(Generator& g) { + ClearStackOnExit scope(*this); + if (g(*this)) { + RAPIDJSON_ASSERT(stack_.GetSize() == sizeof(ValueType)); // Got one and only one root object + ValueType::operator=(*stack_.template Pop(1));// Move value from stack to document + } + return *this; + } + + //!@name Parse from stream + //!@{ + + //! Parse JSON text from an input stream (with Encoding conversion) + /*! \tparam parseFlags Combination of \ref ParseFlag. + \tparam SourceEncoding Encoding of input stream + \tparam InputStream Type of input stream, implementing Stream concept + \param is Input stream to be parsed. + \return The document itself for fluent API. + */ + template + GenericDocument& ParseStream(InputStream& is) { + GenericReader reader( + stack_.HasAllocator() ? &stack_.GetAllocator() : 0); + ClearStackOnExit scope(*this); + parseResult_ = reader.template Parse(is, *this); + if (parseResult_) { + RAPIDJSON_ASSERT(stack_.GetSize() == sizeof(ValueType)); // Got one and only one root object + ValueType::operator=(*stack_.template Pop(1));// Move value from stack to document + } + return *this; + } + + //! Parse JSON text from an input stream + /*! \tparam parseFlags Combination of \ref ParseFlag. + \tparam InputStream Type of input stream, implementing Stream concept + \param is Input stream to be parsed. + \return The document itself for fluent API. + */ + template + GenericDocument& ParseStream(InputStream& is) { + return ParseStream(is); + } + + //! Parse JSON text from an input stream (with \ref kParseDefaultFlags) + /*! \tparam InputStream Type of input stream, implementing Stream concept + \param is Input stream to be parsed. + \return The document itself for fluent API. + */ + template + GenericDocument& ParseStream(InputStream& is) { + return ParseStream(is); + } + //!@} + + //!@name Parse in-place from mutable string + //!@{ + + //! Parse JSON text from a mutable string + /*! \tparam parseFlags Combination of \ref ParseFlag. + \param str Mutable zero-terminated string to be parsed. + \return The document itself for fluent API. + */ + template + GenericDocument& ParseInsitu(Ch* str) { + GenericInsituStringStream s(str); + return ParseStream(s); + } + + //! Parse JSON text from a mutable string (with \ref kParseDefaultFlags) + /*! \param str Mutable zero-terminated string to be parsed. + \return The document itself for fluent API. + */ + GenericDocument& ParseInsitu(Ch* str) { + return ParseInsitu(str); + } + //!@} + + //!@name Parse from read-only string + //!@{ + + //! Parse JSON text from a read-only string (with Encoding conversion) + /*! \tparam parseFlags Combination of \ref ParseFlag (must not contain \ref kParseInsituFlag). + \tparam SourceEncoding Transcoding from input Encoding + \param str Read-only zero-terminated string to be parsed. + */ + template + GenericDocument& Parse(const typename SourceEncoding::Ch* str) { + RAPIDJSON_ASSERT(!(parseFlags & kParseInsituFlag)); + GenericStringStream s(str); + return ParseStream(s); + } + + //! Parse JSON text from a read-only string + /*! \tparam parseFlags Combination of \ref ParseFlag (must not contain \ref kParseInsituFlag). + \param str Read-only zero-terminated string to be parsed. + */ + template + GenericDocument& Parse(const Ch* str) { + return Parse(str); + } + + //! Parse JSON text from a read-only string (with \ref kParseDefaultFlags) + /*! \param str Read-only zero-terminated string to be parsed. + */ + GenericDocument& Parse(const Ch* str) { + return Parse(str); + } + + template + GenericDocument& Parse(const typename SourceEncoding::Ch* str, size_t length) { + RAPIDJSON_ASSERT(!(parseFlags & kParseInsituFlag)); + MemoryStream ms(reinterpret_cast(str), length * sizeof(typename SourceEncoding::Ch)); + EncodedInputStream is(ms); + ParseStream(is); + return *this; + } + + template + GenericDocument& Parse(const Ch* str, size_t length) { + return Parse(str, length); + } + + GenericDocument& Parse(const Ch* str, size_t length) { + return Parse(str, length); + } + +#if RAPIDJSON_HAS_STDSTRING + template + GenericDocument& Parse(const std::basic_string& str) { + // c_str() is constant complexity according to standard. Should be faster than Parse(const char*, size_t) + return Parse(str.c_str()); + } + + template + GenericDocument& Parse(const std::basic_string& str) { + return Parse(str.c_str()); + } + + GenericDocument& Parse(const std::basic_string& str) { + return Parse(str); + } +#endif // RAPIDJSON_HAS_STDSTRING + + //!@} + + //!@name Handling parse errors + //!@{ + + //! Whether a parse error has occurred in the last parsing. + bool HasParseError() const { return parseResult_.IsError(); } + + //! Get the \ref ParseErrorCode of last parsing. + ParseErrorCode GetParseError() const { return parseResult_.Code(); } + + //! Get the position of last parsing error in input, 0 otherwise. + size_t GetErrorOffset() const { return parseResult_.Offset(); } + + //! Implicit conversion to get the last parse result +#ifndef __clang // -Wdocumentation + /*! \return \ref ParseResult of the last parse operation + + \code + Document doc; + ParseResult ok = doc.Parse(json); + if (!ok) + printf( "JSON parse error: %s (%u)\n", GetParseError_En(ok.Code()), ok.Offset()); + \endcode + */ +#endif + operator ParseResult() const { return parseResult_; } + //!@} + + //! Get the allocator of this document. + Allocator& GetAllocator() { + RAPIDJSON_ASSERT(allocator_); + return *allocator_; + } + + //! Get the capacity of stack in bytes. + size_t GetStackCapacity() const { return stack_.GetCapacity(); } + +private: + // clear stack on any exit from ParseStream, e.g. due to exception + struct ClearStackOnExit { + explicit ClearStackOnExit(GenericDocument& d) : d_(d) {} + ~ClearStackOnExit() { d_.ClearStack(); } + private: + ClearStackOnExit(const ClearStackOnExit&); + ClearStackOnExit& operator=(const ClearStackOnExit&); + GenericDocument& d_; + }; + + // callers of the following private Handler functions + // template friend class GenericReader; // for parsing + template friend class GenericValue; // for deep copying + +public: + // Implementation of Handler + bool Null() { new (stack_.template Push()) ValueType(); return true; } + bool Bool(bool b) { new (stack_.template Push()) ValueType(b); return true; } + bool Int(int i) { new (stack_.template Push()) ValueType(i); return true; } + bool Uint(unsigned i) { new (stack_.template Push()) ValueType(i); return true; } + bool Int64(int64_t i) { new (stack_.template Push()) ValueType(i); return true; } + bool Uint64(uint64_t i) { new (stack_.template Push()) ValueType(i); return true; } + bool Double(double d) { new (stack_.template Push()) ValueType(d); return true; } + + bool RawNumber(const Ch* str, SizeType length, bool copy) { + if (copy) + new (stack_.template Push()) ValueType(str, length, GetAllocator()); + else + new (stack_.template Push()) ValueType(str, length); + return true; + } + + bool String(const Ch* str, SizeType length, bool copy) { + if (copy) + new (stack_.template Push()) ValueType(str, length, GetAllocator()); + else + new (stack_.template Push()) ValueType(str, length); + return true; + } + + bool StartObject() { new (stack_.template Push()) ValueType(kObjectType); return true; } + + bool Key(const Ch* str, SizeType length, bool copy) { return String(str, length, copy); } + + bool EndObject(SizeType memberCount) { + typename ValueType::Member* members = stack_.template Pop(memberCount); + stack_.template Top()->SetObjectRaw(members, memberCount, GetAllocator()); + return true; + } + + bool StartArray() { new (stack_.template Push()) ValueType(kArrayType); return true; } + + bool EndArray(SizeType elementCount) { + ValueType* elements = stack_.template Pop(elementCount); + stack_.template Top()->SetArrayRaw(elements, elementCount, GetAllocator()); + return true; + } + +private: + //! Prohibit copying + GenericDocument(const GenericDocument&); + //! Prohibit assignment + GenericDocument& operator=(const GenericDocument&); + + void ClearStack() { + if (Allocator::kNeedFree) + while (stack_.GetSize() > 0) // Here assumes all elements in stack array are GenericValue (Member is actually 2 GenericValue objects) + (stack_.template Pop(1))->~ValueType(); + else + stack_.Clear(); + stack_.ShrinkToFit(); + } + + void Destroy() { + RAPIDJSON_DELETE(ownAllocator_); + } + + static const size_t kDefaultStackCapacity = 1024; + Allocator* allocator_; + Allocator* ownAllocator_; + internal::Stack stack_; + ParseResult parseResult_; +}; + +//! GenericDocument with UTF8 encoding +typedef GenericDocument > Document; + + +//! Helper class for accessing Value of array type. +/*! + Instance of this helper class is obtained by \c GenericValue::GetArray(). + In addition to all APIs for array type, it provides range-based for loop if \c RAPIDJSON_HAS_CXX11_RANGE_FOR=1. +*/ +template +class GenericArray { +public: + typedef GenericArray ConstArray; + typedef GenericArray Array; + typedef ValueT PlainType; + typedef typename internal::MaybeAddConst::Type ValueType; + typedef ValueType* ValueIterator; // This may be const or non-const iterator + typedef const ValueT* ConstValueIterator; + typedef typename ValueType::AllocatorType AllocatorType; + typedef typename ValueType::StringRefType StringRefType; + + template + friend class GenericValue; + + GenericArray(const GenericArray& rhs) : value_(rhs.value_) {} + GenericArray& operator=(const GenericArray& rhs) { value_ = rhs.value_; return *this; } + ~GenericArray() {} + + operator ValueType&() const { return value_; } + SizeType Size() const { return value_.Size(); } + SizeType Capacity() const { return value_.Capacity(); } + bool Empty() const { return value_.Empty(); } + void Clear() const { value_.Clear(); } + ValueType& operator[](SizeType index) const { return value_[index]; } + ValueIterator Begin() const { return value_.Begin(); } + ValueIterator End() const { return value_.End(); } + GenericArray Reserve(SizeType newCapacity, AllocatorType &allocator) const { value_.Reserve(newCapacity, allocator); return *this; } + GenericArray PushBack(ValueType& value, AllocatorType& allocator) const { value_.PushBack(value, allocator); return *this; } +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericArray PushBack(ValueType&& value, AllocatorType& allocator) const { value_.PushBack(value, allocator); return *this; } +#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericArray PushBack(StringRefType value, AllocatorType& allocator) const { value_.PushBack(value, allocator); return *this; } + template RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (const GenericArray&)) PushBack(T value, AllocatorType& allocator) const { value_.PushBack(value, allocator); return *this; } + GenericArray PopBack() const { value_.PopBack(); return *this; } + ValueIterator Erase(ConstValueIterator pos) const { return value_.Erase(pos); } + ValueIterator Erase(ConstValueIterator first, ConstValueIterator last) const { return value_.Erase(first, last); } + +#if RAPIDJSON_HAS_CXX11_RANGE_FOR + ValueIterator begin() const { return value_.Begin(); } + ValueIterator end() const { return value_.End(); } +#endif + +private: + GenericArray(); + GenericArray(ValueType& value) : value_(value) {} + ValueType& value_; +}; + +//! Helper class for accessing Value of object type. +/*! + Instance of this helper class is obtained by \c GenericValue::GetObject(). + In addition to all APIs for array type, it provides range-based for loop if \c RAPIDJSON_HAS_CXX11_RANGE_FOR=1. +*/ +template +class GenericObject { +public: + typedef GenericObject ConstObject; + typedef GenericObject Object; + typedef ValueT PlainType; + typedef typename internal::MaybeAddConst::Type ValueType; + typedef GenericMemberIterator MemberIterator; // This may be const or non-const iterator + typedef GenericMemberIterator ConstMemberIterator; + typedef typename ValueType::AllocatorType AllocatorType; + typedef typename ValueType::StringRefType StringRefType; + typedef typename ValueType::EncodingType EncodingType; + typedef typename ValueType::Ch Ch; + + template + friend class GenericValue; + + GenericObject(const GenericObject& rhs) : value_(rhs.value_) {} + GenericObject& operator=(const GenericObject& rhs) { value_ = rhs.value_; return *this; } + ~GenericObject() {} + + operator ValueType&() const { return value_; } + SizeType MemberCount() const { return value_.MemberCount(); } + SizeType MemberCapacity() const { return value_.MemberCapacity(); } + bool ObjectEmpty() const { return value_.ObjectEmpty(); } + template ValueType& operator[](T* name) const { return value_[name]; } + template ValueType& operator[](const GenericValue& name) const { return value_[name]; } +#if RAPIDJSON_HAS_STDSTRING + ValueType& operator[](const std::basic_string& name) const { return value_[name]; } +#endif + MemberIterator MemberBegin() const { return value_.MemberBegin(); } + MemberIterator MemberEnd() const { return value_.MemberEnd(); } + GenericObject MemberReserve(SizeType newCapacity, AllocatorType &allocator) const { value_.MemberReserve(newCapacity, allocator); return *this; } + bool HasMember(const Ch* name) const { return value_.HasMember(name); } +#if RAPIDJSON_HAS_STDSTRING + bool HasMember(const std::basic_string& name) const { return value_.HasMember(name); } +#endif + template bool HasMember(const GenericValue& name) const { return value_.HasMember(name); } + MemberIterator FindMember(const Ch* name) const { return value_.FindMember(name); } + template MemberIterator FindMember(const GenericValue& name) const { return value_.FindMember(name); } +#if RAPIDJSON_HAS_STDSTRING + MemberIterator FindMember(const std::basic_string& name) const { return value_.FindMember(name); } +#endif + GenericObject AddMember(ValueType& name, ValueType& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + GenericObject AddMember(ValueType& name, StringRefType value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } +#if RAPIDJSON_HAS_STDSTRING + GenericObject AddMember(ValueType& name, std::basic_string& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } +#endif + template RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (ValueType&)) AddMember(ValueType& name, T value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericObject AddMember(ValueType&& name, ValueType&& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + GenericObject AddMember(ValueType&& name, ValueType& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + GenericObject AddMember(ValueType& name, ValueType&& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + GenericObject AddMember(StringRefType name, ValueType&& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } +#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericObject AddMember(StringRefType name, ValueType& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + GenericObject AddMember(StringRefType name, StringRefType value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + template RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (GenericObject)) AddMember(StringRefType name, T value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + void RemoveAllMembers() { value_.RemoveAllMembers(); } + bool RemoveMember(const Ch* name) const { return value_.RemoveMember(name); } +#if RAPIDJSON_HAS_STDSTRING + bool RemoveMember(const std::basic_string& name) const { return value_.RemoveMember(name); } +#endif + template bool RemoveMember(const GenericValue& name) const { return value_.RemoveMember(name); } + MemberIterator RemoveMember(MemberIterator m) const { return value_.RemoveMember(m); } + MemberIterator EraseMember(ConstMemberIterator pos) const { return value_.EraseMember(pos); } + MemberIterator EraseMember(ConstMemberIterator first, ConstMemberIterator last) const { return value_.EraseMember(first, last); } + bool EraseMember(const Ch* name) const { return value_.EraseMember(name); } +#if RAPIDJSON_HAS_STDSTRING + bool EraseMember(const std::basic_string& name) const { return EraseMember(ValueType(StringRef(name))); } +#endif + template bool EraseMember(const GenericValue& name) const { return value_.EraseMember(name); } + +#if RAPIDJSON_HAS_CXX11_RANGE_FOR + MemberIterator begin() const { return value_.MemberBegin(); } + MemberIterator end() const { return value_.MemberEnd(); } +#endif + +private: + GenericObject(); + GenericObject(ValueType& value) : value_(value) {} + ValueType& value_; +}; + +RAPIDJSON_NAMESPACE_END +RAPIDJSON_DIAG_POP + +#ifdef RAPIDJSON_WINDOWS_GETOBJECT_WORKAROUND_APPLIED +#pragma pop_macro("GetObject") +#undef RAPIDJSON_WINDOWS_GETOBJECT_WORKAROUND_APPLIED +#endif + +#endif // RAPIDJSON_DOCUMENT_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/encodedstream.h b/include/liblvgl/libs/thorvg/rapidjson/encodedstream.h new file mode 100644 index 00000000..309499dc --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/encodedstream.h @@ -0,0 +1,299 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ENCODEDSTREAM_H_ +#define RAPIDJSON_ENCODEDSTREAM_H_ + +#include "stream.h" +#include "memorystream.h" + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Input byte stream wrapper with a statically bound encoding. +/*! + \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE. + \tparam InputByteStream Type of input byte stream. For example, FileReadStream. +*/ +template +class EncodedInputStream { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); +public: + typedef typename Encoding::Ch Ch; + + EncodedInputStream(InputByteStream& is) : is_(is) { + current_ = Encoding::TakeBOM(is_); + } + + Ch Peek() const { return current_; } + Ch Take() { Ch c = current_; current_ = Encoding::Take(is_); return c; } + size_t Tell() const { return is_.Tell(); } + + // Not implemented + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + EncodedInputStream(const EncodedInputStream&); + EncodedInputStream& operator=(const EncodedInputStream&); + + InputByteStream& is_; + Ch current_; +}; + +//! Specialized for UTF8 MemoryStream. +template <> +class EncodedInputStream, MemoryStream> { +public: + typedef UTF8<>::Ch Ch; + + EncodedInputStream(MemoryStream& is) : is_(is) { + if (static_cast(is_.Peek()) == 0xEFu) is_.Take(); + if (static_cast(is_.Peek()) == 0xBBu) is_.Take(); + if (static_cast(is_.Peek()) == 0xBFu) is_.Take(); + } + Ch Peek() const { return is_.Peek(); } + Ch Take() { return is_.Take(); } + size_t Tell() const { return is_.Tell(); } + + // Not implemented + void Put(Ch) {} + void Flush() {} + Ch* PutBegin() { return 0; } + size_t PutEnd(Ch*) { return 0; } + + MemoryStream& is_; + +private: + EncodedInputStream(const EncodedInputStream&); + EncodedInputStream& operator=(const EncodedInputStream&); +}; + +//! Output byte stream wrapper with statically bound encoding. +/*! + \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE. + \tparam OutputByteStream Type of input byte stream. For example, FileWriteStream. +*/ +template +class EncodedOutputStream { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); +public: + typedef typename Encoding::Ch Ch; + + EncodedOutputStream(OutputByteStream& os, bool putBOM = true) : os_(os) { + if (putBOM) + Encoding::PutBOM(os_); + } + + void Put(Ch c) { Encoding::Put(os_, c); } + void Flush() { os_.Flush(); } + + // Not implemented + Ch Peek() const { RAPIDJSON_ASSERT(false); return 0;} + Ch Take() { RAPIDJSON_ASSERT(false); return 0;} + size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + EncodedOutputStream(const EncodedOutputStream&); + EncodedOutputStream& operator=(const EncodedOutputStream&); + + OutputByteStream& os_; +}; + +#define RAPIDJSON_ENCODINGS_FUNC(x) UTF8::x, UTF16LE::x, UTF16BE::x, UTF32LE::x, UTF32BE::x + +//! Input stream wrapper with dynamically bound encoding and automatic encoding detection. +/*! + \tparam CharType Type of character for reading. + \tparam InputByteStream type of input byte stream to be wrapped. +*/ +template +class AutoUTFInputStream { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); +public: + typedef CharType Ch; + + //! Constructor. + /*! + \param is input stream to be wrapped. + \param type UTF encoding type if it is not detected from the stream. + */ + AutoUTFInputStream(InputByteStream& is, UTFType type = kUTF8) : is_(&is), type_(type), hasBOM_(false) { + RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE); + DetectType(); + static const TakeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Take) }; + takeFunc_ = f[type_]; + current_ = takeFunc_(*is_); + } + + UTFType GetType() const { return type_; } + bool HasBOM() const { return hasBOM_; } + + Ch Peek() const { return current_; } + Ch Take() { Ch c = current_; current_ = takeFunc_(*is_); return c; } + size_t Tell() const { return is_->Tell(); } + + // Not implemented + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + AutoUTFInputStream(const AutoUTFInputStream&); + AutoUTFInputStream& operator=(const AutoUTFInputStream&); + + // Detect encoding type with BOM or RFC 4627 + void DetectType() { + // BOM (Byte Order Mark): + // 00 00 FE FF UTF-32BE + // FF FE 00 00 UTF-32LE + // FE FF UTF-16BE + // FF FE UTF-16LE + // EF BB BF UTF-8 + + const unsigned char* c = reinterpret_cast(is_->Peek4()); + if (!c) + return; + + unsigned bom = static_cast(c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24)); + hasBOM_ = false; + if (bom == 0xFFFE0000) { type_ = kUTF32BE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); } + else if (bom == 0x0000FEFF) { type_ = kUTF32LE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); } + else if ((bom & 0xFFFF) == 0xFFFE) { type_ = kUTF16BE; hasBOM_ = true; is_->Take(); is_->Take(); } + else if ((bom & 0xFFFF) == 0xFEFF) { type_ = kUTF16LE; hasBOM_ = true; is_->Take(); is_->Take(); } + else if ((bom & 0xFFFFFF) == 0xBFBBEF) { type_ = kUTF8; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); } + + // RFC 4627: Section 3 + // "Since the first two characters of a JSON text will always be ASCII + // characters [RFC0020], it is possible to determine whether an octet + // stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking + // at the pattern of nulls in the first four octets." + // 00 00 00 xx UTF-32BE + // 00 xx 00 xx UTF-16BE + // xx 00 00 00 UTF-32LE + // xx 00 xx 00 UTF-16LE + // xx xx xx xx UTF-8 + + if (!hasBOM_) { + int pattern = (c[0] ? 1 : 0) | (c[1] ? 2 : 0) | (c[2] ? 4 : 0) | (c[3] ? 8 : 0); + switch (pattern) { + case 0x08: type_ = kUTF32BE; break; + case 0x0A: type_ = kUTF16BE; break; + case 0x01: type_ = kUTF32LE; break; + case 0x05: type_ = kUTF16LE; break; + case 0x0F: type_ = kUTF8; break; + default: break; // Use type defined by user. + } + } + + // Runtime check whether the size of character type is sufficient. It only perform checks with assertion. + if (type_ == kUTF16LE || type_ == kUTF16BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 2); + if (type_ == kUTF32LE || type_ == kUTF32BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 4); + } + + typedef Ch (*TakeFunc)(InputByteStream& is); + InputByteStream* is_; + UTFType type_; + Ch current_; + TakeFunc takeFunc_; + bool hasBOM_; +}; + +//! Output stream wrapper with dynamically bound encoding and automatic encoding detection. +/*! + \tparam CharType Type of character for writing. + \tparam OutputByteStream type of output byte stream to be wrapped. +*/ +template +class AutoUTFOutputStream { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); +public: + typedef CharType Ch; + + //! Constructor. + /*! + \param os output stream to be wrapped. + \param type UTF encoding type. + \param putBOM Whether to write BOM at the beginning of the stream. + */ + AutoUTFOutputStream(OutputByteStream& os, UTFType type, bool putBOM) : os_(&os), type_(type) { + RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE); + + // Runtime check whether the size of character type is sufficient. It only perform checks with assertion. + if (type_ == kUTF16LE || type_ == kUTF16BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 2); + if (type_ == kUTF32LE || type_ == kUTF32BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 4); + + static const PutFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Put) }; + putFunc_ = f[type_]; + + if (putBOM) + PutBOM(); + } + + UTFType GetType() const { return type_; } + + void Put(Ch c) { putFunc_(*os_, c); } + void Flush() { os_->Flush(); } + + // Not implemented + Ch Peek() const { RAPIDJSON_ASSERT(false); return 0;} + Ch Take() { RAPIDJSON_ASSERT(false); return 0;} + size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + AutoUTFOutputStream(const AutoUTFOutputStream&); + AutoUTFOutputStream& operator=(const AutoUTFOutputStream&); + + void PutBOM() { + typedef void (*PutBOMFunc)(OutputByteStream&); + static const PutBOMFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(PutBOM) }; + f[type_](*os_); + } + + typedef void (*PutFunc)(OutputByteStream&, Ch); + + OutputByteStream* os_; + UTFType type_; + PutFunc putFunc_; +}; + +#undef RAPIDJSON_ENCODINGS_FUNC + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_FILESTREAM_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/encodings.h b/include/liblvgl/libs/thorvg/rapidjson/encodings.h new file mode 100644 index 00000000..5ee169e1 --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/encodings.h @@ -0,0 +1,716 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ENCODINGS_H_ +#define RAPIDJSON_ENCODINGS_H_ + +#include "rapidjson.h" + +#if defined(_MSC_VER) && !defined(__clang__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4244) // conversion from 'type1' to 'type2', possible loss of data +RAPIDJSON_DIAG_OFF(4702) // unreachable code +#elif defined(__GNUC__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +RAPIDJSON_DIAG_OFF(overflow) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// Encoding + +/*! \class rapidjson::Encoding + \brief Concept for encoding of Unicode characters. + +\code +concept Encoding { + typename Ch; //! Type of character. A "character" is actually a code unit in unicode's definition. + + enum { supportUnicode = 1 }; // or 0 if not supporting unicode + + //! \brief Encode a Unicode codepoint to an output stream. + //! \param os Output stream. + //! \param codepoint An unicode codepoint, ranging from 0x0 to 0x10FFFF inclusively. + template + static void Encode(OutputStream& os, unsigned codepoint); + + //! \brief Decode a Unicode codepoint from an input stream. + //! \param is Input stream. + //! \param codepoint Output of the unicode codepoint. + //! \return true if a valid codepoint can be decoded from the stream. + template + static bool Decode(InputStream& is, unsigned* codepoint); + + //! \brief Validate one Unicode codepoint from an encoded stream. + //! \param is Input stream to obtain codepoint. + //! \param os Output for copying one codepoint. + //! \return true if it is valid. + //! \note This function just validating and copying the codepoint without actually decode it. + template + static bool Validate(InputStream& is, OutputStream& os); + + // The following functions are deal with byte streams. + + //! Take a character from input byte stream, skip BOM if exist. + template + static CharType TakeBOM(InputByteStream& is); + + //! Take a character from input byte stream. + template + static Ch Take(InputByteStream& is); + + //! Put BOM to output byte stream. + template + static void PutBOM(OutputByteStream& os); + + //! Put a character to output byte stream. + template + static void Put(OutputByteStream& os, Ch c); +}; +\endcode +*/ + +/////////////////////////////////////////////////////////////////////////////// +// UTF8 + +//! UTF-8 encoding. +/*! http://en.wikipedia.org/wiki/UTF-8 + http://tools.ietf.org/html/rfc3629 + \tparam CharType Code unit for storing 8-bit UTF-8 data. Default is char. + \note implements Encoding concept +*/ +template +struct UTF8 { + typedef CharType Ch; + + enum { supportUnicode = 1 }; + + template + static void Encode(OutputStream& os, unsigned codepoint) { + if (codepoint <= 0x7F) + os.Put(static_cast(codepoint & 0xFF)); + else if (codepoint <= 0x7FF) { + os.Put(static_cast(0xC0 | ((codepoint >> 6) & 0xFF))); + os.Put(static_cast(0x80 | ((codepoint & 0x3F)))); + } + else if (codepoint <= 0xFFFF) { + os.Put(static_cast(0xE0 | ((codepoint >> 12) & 0xFF))); + os.Put(static_cast(0x80 | ((codepoint >> 6) & 0x3F))); + os.Put(static_cast(0x80 | (codepoint & 0x3F))); + } + else { + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + os.Put(static_cast(0xF0 | ((codepoint >> 18) & 0xFF))); + os.Put(static_cast(0x80 | ((codepoint >> 12) & 0x3F))); + os.Put(static_cast(0x80 | ((codepoint >> 6) & 0x3F))); + os.Put(static_cast(0x80 | (codepoint & 0x3F))); + } + } + + template + static void EncodeUnsafe(OutputStream& os, unsigned codepoint) { + if (codepoint <= 0x7F) + PutUnsafe(os, static_cast(codepoint & 0xFF)); + else if (codepoint <= 0x7FF) { + PutUnsafe(os, static_cast(0xC0 | ((codepoint >> 6) & 0xFF))); + PutUnsafe(os, static_cast(0x80 | ((codepoint & 0x3F)))); + } + else if (codepoint <= 0xFFFF) { + PutUnsafe(os, static_cast(0xE0 | ((codepoint >> 12) & 0xFF))); + PutUnsafe(os, static_cast(0x80 | ((codepoint >> 6) & 0x3F))); + PutUnsafe(os, static_cast(0x80 | (codepoint & 0x3F))); + } + else { + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + PutUnsafe(os, static_cast(0xF0 | ((codepoint >> 18) & 0xFF))); + PutUnsafe(os, static_cast(0x80 | ((codepoint >> 12) & 0x3F))); + PutUnsafe(os, static_cast(0x80 | ((codepoint >> 6) & 0x3F))); + PutUnsafe(os, static_cast(0x80 | (codepoint & 0x3F))); + } + } + + template + static bool Decode(InputStream& is, unsigned* codepoint) { +#define RAPIDJSON_COPY() c = is.Take(); *codepoint = (*codepoint << 6) | (static_cast(c) & 0x3Fu) +#define RAPIDJSON_TRANS(mask) result &= ((GetRange(static_cast(c)) & mask) != 0) +#define RAPIDJSON_TAIL() RAPIDJSON_COPY(); RAPIDJSON_TRANS(0x70) + typename InputStream::Ch c = is.Take(); + if (!(c & 0x80)) { + *codepoint = static_cast(c); + return true; + } + + unsigned char type = GetRange(static_cast(c)); + if (type >= 32) { + *codepoint = 0; + } else { + *codepoint = (0xFFu >> type) & static_cast(c); + } + bool result = true; + switch (type) { + case 2: RAPIDJSON_TAIL(); return result; + case 3: RAPIDJSON_TAIL(); RAPIDJSON_TAIL(); return result; + case 4: RAPIDJSON_COPY(); RAPIDJSON_TRANS(0x50); RAPIDJSON_TAIL(); return result; + case 5: RAPIDJSON_COPY(); RAPIDJSON_TRANS(0x10); RAPIDJSON_TAIL(); RAPIDJSON_TAIL(); return result; + case 6: RAPIDJSON_TAIL(); RAPIDJSON_TAIL(); RAPIDJSON_TAIL(); return result; + case 10: RAPIDJSON_COPY(); RAPIDJSON_TRANS(0x20); RAPIDJSON_TAIL(); return result; + case 11: RAPIDJSON_COPY(); RAPIDJSON_TRANS(0x60); RAPIDJSON_TAIL(); RAPIDJSON_TAIL(); return result; + default: return false; + } +#undef RAPIDJSON_COPY +#undef RAPIDJSON_TRANS +#undef RAPIDJSON_TAIL + } + + template + static bool Validate(InputStream& is, OutputStream& os) { +#define RAPIDJSON_COPY() os.Put(c = is.Take()) +#define RAPIDJSON_TRANS(mask) result &= ((GetRange(static_cast(c)) & mask) != 0) +#define RAPIDJSON_TAIL() RAPIDJSON_COPY(); RAPIDJSON_TRANS(0x70) + Ch c; + RAPIDJSON_COPY(); + if (!(c & 0x80)) + return true; + + bool result = true; + switch (GetRange(static_cast(c))) { + case 2: RAPIDJSON_TAIL(); return result; + case 3: RAPIDJSON_TAIL(); RAPIDJSON_TAIL(); return result; + case 4: RAPIDJSON_COPY(); RAPIDJSON_TRANS(0x50); RAPIDJSON_TAIL(); return result; + case 5: RAPIDJSON_COPY(); RAPIDJSON_TRANS(0x10); RAPIDJSON_TAIL(); RAPIDJSON_TAIL(); return result; + case 6: RAPIDJSON_TAIL(); RAPIDJSON_TAIL(); RAPIDJSON_TAIL(); return result; + case 10: RAPIDJSON_COPY(); RAPIDJSON_TRANS(0x20); RAPIDJSON_TAIL(); return result; + case 11: RAPIDJSON_COPY(); RAPIDJSON_TRANS(0x60); RAPIDJSON_TAIL(); RAPIDJSON_TAIL(); return result; + default: return false; + } +#undef RAPIDJSON_COPY +#undef RAPIDJSON_TRANS +#undef RAPIDJSON_TAIL + } + + static unsigned char GetRange(unsigned char c) { + // Referring to DFA of http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ + // With new mapping 1 -> 0x10, 7 -> 0x20, 9 -> 0x40, such that AND operation can test multiple types. + static const unsigned char type[] = { + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10, + 0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40, + 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, + 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, + 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, + 10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8, + }; + return type[c]; + } + + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + typename InputByteStream::Ch c = Take(is); + if (static_cast(c) != 0xEFu) return c; + c = is.Take(); + if (static_cast(c) != 0xBBu) return c; + c = is.Take(); + if (static_cast(c) != 0xBFu) return c; + c = is.Take(); + return c; + } + + template + static Ch Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + return static_cast(is.Take()); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(0xEFu)); + os.Put(static_cast(0xBBu)); + os.Put(static_cast(0xBFu)); + } + + template + static void Put(OutputByteStream& os, Ch c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(c)); + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// UTF16 + +//! UTF-16 encoding. +/*! http://en.wikipedia.org/wiki/UTF-16 + http://tools.ietf.org/html/rfc2781 + \tparam CharType Type for storing 16-bit UTF-16 data. Default is wchar_t. C++11 may use char16_t instead. + \note implements Encoding concept + + \note For in-memory access, no need to concern endianness. The code units and code points are represented by CPU's endianness. + For streaming, use UTF16LE and UTF16BE, which handle endianness. +*/ +template +struct UTF16 { + typedef CharType Ch; + RAPIDJSON_STATIC_ASSERT(sizeof(Ch) >= 2); + + enum { supportUnicode = 1 }; + + template + static void Encode(OutputStream& os, unsigned codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2); + if (codepoint <= 0xFFFF) { + RAPIDJSON_ASSERT(codepoint < 0xD800 || codepoint > 0xDFFF); // Code point itself cannot be surrogate pair + os.Put(static_cast(codepoint)); + } + else { + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + unsigned v = codepoint - 0x10000; + os.Put(static_cast((v >> 10) | 0xD800)); + os.Put(static_cast((v & 0x3FF) | 0xDC00)); + } + } + + + template + static void EncodeUnsafe(OutputStream& os, unsigned codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2); + if (codepoint <= 0xFFFF) { + RAPIDJSON_ASSERT(codepoint < 0xD800 || codepoint > 0xDFFF); // Code point itself cannot be surrogate pair + PutUnsafe(os, static_cast(codepoint)); + } + else { + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + unsigned v = codepoint - 0x10000; + PutUnsafe(os, static_cast((v >> 10) | 0xD800)); + PutUnsafe(os, static_cast((v & 0x3FF) | 0xDC00)); + } + } + + template + static bool Decode(InputStream& is, unsigned* codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2); + typename InputStream::Ch c = is.Take(); + if (c < 0xD800 || c > 0xDFFF) { + *codepoint = static_cast(c); + return true; + } + else if (c <= 0xDBFF) { + *codepoint = (static_cast(c) & 0x3FF) << 10; + c = is.Take(); + *codepoint |= (static_cast(c) & 0x3FF); + *codepoint += 0x10000; + return c >= 0xDC00 && c <= 0xDFFF; + } + return false; + } + + template + static bool Validate(InputStream& is, OutputStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2); + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2); + typename InputStream::Ch c; + os.Put(static_cast(c = is.Take())); + if (c < 0xD800 || c > 0xDFFF) + return true; + else if (c <= 0xDBFF) { + os.Put(c = is.Take()); + return c >= 0xDC00 && c <= 0xDFFF; + } + return false; + } +}; + +//! UTF-16 little endian encoding. +template +struct UTF16LE : UTF16 { + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + CharType c = Take(is); + return static_cast(c) == 0xFEFFu ? Take(is) : c; + } + + template + static CharType Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + unsigned c = static_cast(is.Take()); + c |= static_cast(static_cast(is.Take())) << 8; + return static_cast(c); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(0xFFu)); + os.Put(static_cast(0xFEu)); + } + + template + static void Put(OutputByteStream& os, CharType c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(static_cast(c) & 0xFFu)); + os.Put(static_cast((static_cast(c) >> 8) & 0xFFu)); + } +}; + +//! UTF-16 big endian encoding. +template +struct UTF16BE : UTF16 { + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + CharType c = Take(is); + return static_cast(c) == 0xFEFFu ? Take(is) : c; + } + + template + static CharType Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + unsigned c = static_cast(static_cast(is.Take())) << 8; + c |= static_cast(static_cast(is.Take())); + return static_cast(c); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(0xFEu)); + os.Put(static_cast(0xFFu)); + } + + template + static void Put(OutputByteStream& os, CharType c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast((static_cast(c) >> 8) & 0xFFu)); + os.Put(static_cast(static_cast(c) & 0xFFu)); + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// UTF32 + +//! UTF-32 encoding. +/*! http://en.wikipedia.org/wiki/UTF-32 + \tparam CharType Type for storing 32-bit UTF-32 data. Default is unsigned. C++11 may use char32_t instead. + \note implements Encoding concept + + \note For in-memory access, no need to concern endianness. The code units and code points are represented by CPU's endianness. + For streaming, use UTF32LE and UTF32BE, which handle endianness. +*/ +template +struct UTF32 { + typedef CharType Ch; + RAPIDJSON_STATIC_ASSERT(sizeof(Ch) >= 4); + + enum { supportUnicode = 1 }; + + template + static void Encode(OutputStream& os, unsigned codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 4); + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + os.Put(codepoint); + } + + template + static void EncodeUnsafe(OutputStream& os, unsigned codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 4); + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + PutUnsafe(os, codepoint); + } + + template + static bool Decode(InputStream& is, unsigned* codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 4); + Ch c = is.Take(); + *codepoint = c; + return c <= 0x10FFFF; + } + + template + static bool Validate(InputStream& is, OutputStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 4); + Ch c; + os.Put(c = is.Take()); + return c <= 0x10FFFF; + } +}; + +//! UTF-32 little endian encoding. +template +struct UTF32LE : UTF32 { + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + CharType c = Take(is); + return static_cast(c) == 0x0000FEFFu ? Take(is) : c; + } + + template + static CharType Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + unsigned c = static_cast(is.Take()); + c |= static_cast(static_cast(is.Take())) << 8; + c |= static_cast(static_cast(is.Take())) << 16; + c |= static_cast(static_cast(is.Take())) << 24; + return static_cast(c); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(0xFFu)); + os.Put(static_cast(0xFEu)); + os.Put(static_cast(0x00u)); + os.Put(static_cast(0x00u)); + } + + template + static void Put(OutputByteStream& os, CharType c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(c & 0xFFu)); + os.Put(static_cast((c >> 8) & 0xFFu)); + os.Put(static_cast((c >> 16) & 0xFFu)); + os.Put(static_cast((c >> 24) & 0xFFu)); + } +}; + +//! UTF-32 big endian encoding. +template +struct UTF32BE : UTF32 { + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + CharType c = Take(is); + return static_cast(c) == 0x0000FEFFu ? Take(is) : c; + } + + template + static CharType Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + unsigned c = static_cast(static_cast(is.Take())) << 24; + c |= static_cast(static_cast(is.Take())) << 16; + c |= static_cast(static_cast(is.Take())) << 8; + c |= static_cast(static_cast(is.Take())); + return static_cast(c); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(0x00u)); + os.Put(static_cast(0x00u)); + os.Put(static_cast(0xFEu)); + os.Put(static_cast(0xFFu)); + } + + template + static void Put(OutputByteStream& os, CharType c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast((c >> 24) & 0xFFu)); + os.Put(static_cast((c >> 16) & 0xFFu)); + os.Put(static_cast((c >> 8) & 0xFFu)); + os.Put(static_cast(c & 0xFFu)); + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// ASCII + +//! ASCII encoding. +/*! http://en.wikipedia.org/wiki/ASCII + \tparam CharType Code unit for storing 7-bit ASCII data. Default is char. + \note implements Encoding concept +*/ +template +struct ASCII { + typedef CharType Ch; + + enum { supportUnicode = 0 }; + + template + static void Encode(OutputStream& os, unsigned codepoint) { + RAPIDJSON_ASSERT(codepoint <= 0x7F); + os.Put(static_cast(codepoint & 0xFF)); + } + + template + static void EncodeUnsafe(OutputStream& os, unsigned codepoint) { + RAPIDJSON_ASSERT(codepoint <= 0x7F); + PutUnsafe(os, static_cast(codepoint & 0xFF)); + } + + template + static bool Decode(InputStream& is, unsigned* codepoint) { + uint8_t c = static_cast(is.Take()); + *codepoint = c; + return c <= 0X7F; + } + + template + static bool Validate(InputStream& is, OutputStream& os) { + uint8_t c = static_cast(is.Take()); + os.Put(static_cast(c)); + return c <= 0x7F; + } + + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + uint8_t c = static_cast(Take(is)); + return static_cast(c); + } + + template + static Ch Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + return static_cast(is.Take()); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + (void)os; + } + + template + static void Put(OutputByteStream& os, Ch c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(c)); + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// AutoUTF + +//! Runtime-specified UTF encoding type of a stream. +enum UTFType { + kUTF8 = 0, //!< UTF-8. + kUTF16LE = 1, //!< UTF-16 little endian. + kUTF16BE = 2, //!< UTF-16 big endian. + kUTF32LE = 3, //!< UTF-32 little endian. + kUTF32BE = 4 //!< UTF-32 big endian. +}; + +//! Dynamically select encoding according to stream's runtime-specified UTF encoding type. +/*! \note This class can be used with AutoUTFInputStream and AutoUTFOutputStream, which provides GetType(). +*/ +template +struct AutoUTF { + typedef CharType Ch; + + enum { supportUnicode = 1 }; + +#define RAPIDJSON_ENCODINGS_FUNC(x) UTF8::x, UTF16LE::x, UTF16BE::x, UTF32LE::x, UTF32BE::x + + template + static RAPIDJSON_FORCEINLINE void Encode(OutputStream& os, unsigned codepoint) { + typedef void (*EncodeFunc)(OutputStream&, unsigned); + static const EncodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Encode) }; + (*f[os.GetType()])(os, codepoint); + } + + template + static RAPIDJSON_FORCEINLINE void EncodeUnsafe(OutputStream& os, unsigned codepoint) { + typedef void (*EncodeFunc)(OutputStream&, unsigned); + static const EncodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(EncodeUnsafe) }; + (*f[os.GetType()])(os, codepoint); + } + + template + static RAPIDJSON_FORCEINLINE bool Decode(InputStream& is, unsigned* codepoint) { + typedef bool (*DecodeFunc)(InputStream&, unsigned*); + static const DecodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Decode) }; + return (*f[is.GetType()])(is, codepoint); + } + + template + static RAPIDJSON_FORCEINLINE bool Validate(InputStream& is, OutputStream& os) { + typedef bool (*ValidateFunc)(InputStream&, OutputStream&); + static const ValidateFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Validate) }; + return (*f[is.GetType()])(is, os); + } + +#undef RAPIDJSON_ENCODINGS_FUNC +}; + +/////////////////////////////////////////////////////////////////////////////// +// Transcoder + +//! Encoding conversion. +template +struct Transcoder { + //! Take one Unicode codepoint from source encoding, convert it to target encoding and put it to the output stream. + template + static RAPIDJSON_FORCEINLINE bool Transcode(InputStream& is, OutputStream& os) { + unsigned codepoint; + if (!SourceEncoding::Decode(is, &codepoint)) + return false; + TargetEncoding::Encode(os, codepoint); + return true; + } + + template + static RAPIDJSON_FORCEINLINE bool TranscodeUnsafe(InputStream& is, OutputStream& os) { + unsigned codepoint; + if (!SourceEncoding::Decode(is, &codepoint)) + return false; + TargetEncoding::EncodeUnsafe(os, codepoint); + return true; + } + + //! Validate one Unicode codepoint from an encoded stream. + template + static RAPIDJSON_FORCEINLINE bool Validate(InputStream& is, OutputStream& os) { + return Transcode(is, os); // Since source/target encoding is different, must transcode. + } +}; + +// Forward declaration. +template +inline void PutUnsafe(Stream& stream, typename Stream::Ch c); + +//! Specialization of Transcoder with same source and target encoding. +template +struct Transcoder { + template + static RAPIDJSON_FORCEINLINE bool Transcode(InputStream& is, OutputStream& os) { + os.Put(is.Take()); // Just copy one code unit. This semantic is different from primary template class. + return true; + } + + template + static RAPIDJSON_FORCEINLINE bool TranscodeUnsafe(InputStream& is, OutputStream& os) { + PutUnsafe(os, is.Take()); // Just copy one code unit. This semantic is different from primary template class. + return true; + } + + template + static RAPIDJSON_FORCEINLINE bool Validate(InputStream& is, OutputStream& os) { + return Encoding::Validate(is, os); // source/target encoding are the same + } +}; + +RAPIDJSON_NAMESPACE_END + +#if defined(__GNUC__) || (defined(_MSC_VER) && !defined(__clang__)) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_ENCODINGS_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/error/en.h b/include/liblvgl/libs/thorvg/rapidjson/error/en.h new file mode 100644 index 00000000..c87b04eb --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/error/en.h @@ -0,0 +1,176 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ERROR_EN_H_ +#define RAPIDJSON_ERROR_EN_H_ + +#include "error.h" + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(switch-enum) +RAPIDJSON_DIAG_OFF(covered-switch-default) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Maps error code of parsing into error message. +/*! + \ingroup RAPIDJSON_ERRORS + \param parseErrorCode Error code obtained in parsing. + \return the error message. + \note User can make a copy of this function for localization. + Using switch-case is safer for future modification of error codes. +*/ +inline const RAPIDJSON_ERROR_CHARTYPE* GetParseError_En(ParseErrorCode parseErrorCode) { + switch (parseErrorCode) { + case kParseErrorNone: return RAPIDJSON_ERROR_STRING("No error."); + + case kParseErrorDocumentEmpty: return RAPIDJSON_ERROR_STRING("The document is empty."); + case kParseErrorDocumentRootNotSingular: return RAPIDJSON_ERROR_STRING("The document root must not be followed by other values."); + + case kParseErrorValueInvalid: return RAPIDJSON_ERROR_STRING("Invalid value."); + + case kParseErrorObjectMissName: return RAPIDJSON_ERROR_STRING("Missing a name for object member."); + case kParseErrorObjectMissColon: return RAPIDJSON_ERROR_STRING("Missing a colon after a name of object member."); + case kParseErrorObjectMissCommaOrCurlyBracket: return RAPIDJSON_ERROR_STRING("Missing a comma or '}' after an object member."); + + case kParseErrorArrayMissCommaOrSquareBracket: return RAPIDJSON_ERROR_STRING("Missing a comma or ']' after an array element."); + + case kParseErrorStringUnicodeEscapeInvalidHex: return RAPIDJSON_ERROR_STRING("Incorrect hex digit after \\u escape in string."); + case kParseErrorStringUnicodeSurrogateInvalid: return RAPIDJSON_ERROR_STRING("The surrogate pair in string is invalid."); + case kParseErrorStringEscapeInvalid: return RAPIDJSON_ERROR_STRING("Invalid escape character in string."); + case kParseErrorStringMissQuotationMark: return RAPIDJSON_ERROR_STRING("Missing a closing quotation mark in string."); + case kParseErrorStringInvalidEncoding: return RAPIDJSON_ERROR_STRING("Invalid encoding in string."); + + case kParseErrorNumberTooBig: return RAPIDJSON_ERROR_STRING("Number too big to be stored in double."); + case kParseErrorNumberMissFraction: return RAPIDJSON_ERROR_STRING("Miss fraction part in number."); + case kParseErrorNumberMissExponent: return RAPIDJSON_ERROR_STRING("Miss exponent in number."); + + case kParseErrorTermination: return RAPIDJSON_ERROR_STRING("Terminate parsing due to Handler error."); + case kParseErrorUnspecificSyntaxError: return RAPIDJSON_ERROR_STRING("Unspecific syntax error."); + + default: return RAPIDJSON_ERROR_STRING("Unknown error."); + } +} + +//! Maps error code of validation into error message. +/*! + \ingroup RAPIDJSON_ERRORS + \param validateErrorCode Error code obtained from validator. + \return the error message. + \note User can make a copy of this function for localization. + Using switch-case is safer for future modification of error codes. +*/ +inline const RAPIDJSON_ERROR_CHARTYPE* GetValidateError_En(ValidateErrorCode validateErrorCode) { + switch (validateErrorCode) { + case kValidateErrors: return RAPIDJSON_ERROR_STRING("One or more validation errors have occurred"); + case kValidateErrorNone: return RAPIDJSON_ERROR_STRING("No error."); + + case kValidateErrorMultipleOf: return RAPIDJSON_ERROR_STRING("Number '%actual' is not a multiple of the 'multipleOf' value '%expected'."); + case kValidateErrorMaximum: return RAPIDJSON_ERROR_STRING("Number '%actual' is greater than the 'maximum' value '%expected'."); + case kValidateErrorExclusiveMaximum: return RAPIDJSON_ERROR_STRING("Number '%actual' is greater than or equal to the 'exclusiveMaximum' value '%expected'."); + case kValidateErrorMinimum: return RAPIDJSON_ERROR_STRING("Number '%actual' is less than the 'minimum' value '%expected'."); + case kValidateErrorExclusiveMinimum: return RAPIDJSON_ERROR_STRING("Number '%actual' is less than or equal to the 'exclusiveMinimum' value '%expected'."); + + case kValidateErrorMaxLength: return RAPIDJSON_ERROR_STRING("String '%actual' is longer than the 'maxLength' value '%expected'."); + case kValidateErrorMinLength: return RAPIDJSON_ERROR_STRING("String '%actual' is shorter than the 'minLength' value '%expected'."); + case kValidateErrorPattern: return RAPIDJSON_ERROR_STRING("String '%actual' does not match the 'pattern' regular expression."); + + case kValidateErrorMaxItems: return RAPIDJSON_ERROR_STRING("Array of length '%actual' is longer than the 'maxItems' value '%expected'."); + case kValidateErrorMinItems: return RAPIDJSON_ERROR_STRING("Array of length '%actual' is shorter than the 'minItems' value '%expected'."); + case kValidateErrorUniqueItems: return RAPIDJSON_ERROR_STRING("Array has duplicate items at indices '%duplicates' but 'uniqueItems' is true."); + case kValidateErrorAdditionalItems: return RAPIDJSON_ERROR_STRING("Array has an additional item at index '%disallowed' that is not allowed by the schema."); + + case kValidateErrorMaxProperties: return RAPIDJSON_ERROR_STRING("Object has '%actual' members which is more than 'maxProperties' value '%expected'."); + case kValidateErrorMinProperties: return RAPIDJSON_ERROR_STRING("Object has '%actual' members which is less than 'minProperties' value '%expected'."); + case kValidateErrorRequired: return RAPIDJSON_ERROR_STRING("Object is missing the following members required by the schema: '%missing'."); + case kValidateErrorAdditionalProperties: return RAPIDJSON_ERROR_STRING("Object has an additional member '%disallowed' that is not allowed by the schema."); + case kValidateErrorPatternProperties: return RAPIDJSON_ERROR_STRING("Object has 'patternProperties' that are not allowed by the schema."); + case kValidateErrorDependencies: return RAPIDJSON_ERROR_STRING("Object has missing property or schema dependencies, refer to following errors."); + + case kValidateErrorEnum: return RAPIDJSON_ERROR_STRING("Property has a value that is not one of its allowed enumerated values."); + case kValidateErrorType: return RAPIDJSON_ERROR_STRING("Property has a type '%actual' that is not in the following list: '%expected'."); + + case kValidateErrorOneOf: return RAPIDJSON_ERROR_STRING("Property did not match any of the sub-schemas specified by 'oneOf', refer to following errors."); + case kValidateErrorOneOfMatch: return RAPIDJSON_ERROR_STRING("Property matched more than one of the sub-schemas specified by 'oneOf', indices '%matches'."); + case kValidateErrorAllOf: return RAPIDJSON_ERROR_STRING("Property did not match all of the sub-schemas specified by 'allOf', refer to following errors."); + case kValidateErrorAnyOf: return RAPIDJSON_ERROR_STRING("Property did not match any of the sub-schemas specified by 'anyOf', refer to following errors."); + case kValidateErrorNot: return RAPIDJSON_ERROR_STRING("Property matched the sub-schema specified by 'not'."); + + case kValidateErrorReadOnly: return RAPIDJSON_ERROR_STRING("Property is read-only but has been provided when validation is for writing."); + case kValidateErrorWriteOnly: return RAPIDJSON_ERROR_STRING("Property is write-only but has been provided when validation is for reading."); + + default: return RAPIDJSON_ERROR_STRING("Unknown error."); + } +} + +//! Maps error code of schema document compilation into error message. +/*! + \ingroup RAPIDJSON_ERRORS + \param schemaErrorCode Error code obtained from compiling the schema document. + \return the error message. + \note User can make a copy of this function for localization. + Using switch-case is safer for future modification of error codes. +*/ + inline const RAPIDJSON_ERROR_CHARTYPE* GetSchemaError_En(SchemaErrorCode schemaErrorCode) { + switch (schemaErrorCode) { + case kSchemaErrorNone: return RAPIDJSON_ERROR_STRING("No error."); + + case kSchemaErrorStartUnknown: return RAPIDJSON_ERROR_STRING("Pointer '%value' to start of schema does not resolve to a location in the document."); + case kSchemaErrorRefPlainName: return RAPIDJSON_ERROR_STRING("$ref fragment '%value' must be a JSON pointer."); + case kSchemaErrorRefInvalid: return RAPIDJSON_ERROR_STRING("$ref must not be an empty string."); + case kSchemaErrorRefPointerInvalid: return RAPIDJSON_ERROR_STRING("$ref fragment '%value' is not a valid JSON pointer at offset '%offset'."); + case kSchemaErrorRefUnknown: return RAPIDJSON_ERROR_STRING("$ref '%value' does not resolve to a location in the target document."); + case kSchemaErrorRefCyclical: return RAPIDJSON_ERROR_STRING("$ref '%value' is cyclical."); + case kSchemaErrorRefNoRemoteProvider: return RAPIDJSON_ERROR_STRING("$ref is remote but there is no remote provider."); + case kSchemaErrorRefNoRemoteSchema: return RAPIDJSON_ERROR_STRING("$ref '%value' is remote but the remote provider did not return a schema."); + case kSchemaErrorRegexInvalid: return RAPIDJSON_ERROR_STRING("Invalid regular expression '%value' in 'pattern' or 'patternProperties'."); + case kSchemaErrorSpecUnknown: return RAPIDJSON_ERROR_STRING("JSON schema draft or OpenAPI version is not recognized."); + case kSchemaErrorSpecUnsupported: return RAPIDJSON_ERROR_STRING("JSON schema draft or OpenAPI version is not supported."); + case kSchemaErrorSpecIllegal: return RAPIDJSON_ERROR_STRING("Both JSON schema draft and OpenAPI version found in document."); + case kSchemaErrorReadOnlyAndWriteOnly: return RAPIDJSON_ERROR_STRING("Property must not be both 'readOnly' and 'writeOnly'."); + + default: return RAPIDJSON_ERROR_STRING("Unknown error."); + } + } + +//! Maps error code of pointer parse into error message. +/*! + \ingroup RAPIDJSON_ERRORS + \param pointerParseErrorCode Error code obtained from pointer parse. + \return the error message. + \note User can make a copy of this function for localization. + Using switch-case is safer for future modification of error codes. +*/ +inline const RAPIDJSON_ERROR_CHARTYPE* GetPointerParseError_En(PointerParseErrorCode pointerParseErrorCode) { + switch (pointerParseErrorCode) { + case kPointerParseErrorNone: return RAPIDJSON_ERROR_STRING("No error."); + + case kPointerParseErrorTokenMustBeginWithSolidus: return RAPIDJSON_ERROR_STRING("A token must begin with a '/'."); + case kPointerParseErrorInvalidEscape: return RAPIDJSON_ERROR_STRING("Invalid escape."); + case kPointerParseErrorInvalidPercentEncoding: return RAPIDJSON_ERROR_STRING("Invalid percent encoding in URI fragment."); + case kPointerParseErrorCharacterMustPercentEncode: return RAPIDJSON_ERROR_STRING("A character must be percent encoded in a URI fragment."); + + default: return RAPIDJSON_ERROR_STRING("Unknown error."); + } +} + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_ERROR_EN_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/error/error.h b/include/liblvgl/libs/thorvg/rapidjson/error/error.h new file mode 100644 index 00000000..cae345db --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/error/error.h @@ -0,0 +1,285 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ERROR_ERROR_H_ +#define RAPIDJSON_ERROR_ERROR_H_ + +#include "../rapidjson.h" + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +#endif + +/*! \file error.h */ + +/*! \defgroup RAPIDJSON_ERRORS RapidJSON error handling */ + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_ERROR_CHARTYPE + +//! Character type of error messages. +/*! \ingroup RAPIDJSON_ERRORS + The default character type is \c char. + On Windows, user can define this macro as \c TCHAR for supporting both + unicode/non-unicode settings. +*/ +#ifndef RAPIDJSON_ERROR_CHARTYPE +#define RAPIDJSON_ERROR_CHARTYPE char +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_ERROR_STRING + +//! Macro for converting string literal to \ref RAPIDJSON_ERROR_CHARTYPE[]. +/*! \ingroup RAPIDJSON_ERRORS + By default this conversion macro does nothing. + On Windows, user can define this macro as \c _T(x) for supporting both + unicode/non-unicode settings. +*/ +#ifndef RAPIDJSON_ERROR_STRING +#define RAPIDJSON_ERROR_STRING(x) x +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// ParseErrorCode + +//! Error code of parsing. +/*! \ingroup RAPIDJSON_ERRORS + \see GenericReader::Parse, GenericReader::GetParseErrorCode +*/ +enum ParseErrorCode { + kParseErrorNone = 0, //!< No error. + + kParseErrorDocumentEmpty, //!< The document is empty. + kParseErrorDocumentRootNotSingular, //!< The document root must not follow by other values. + + kParseErrorValueInvalid, //!< Invalid value. + + kParseErrorObjectMissName, //!< Missing a name for object member. + kParseErrorObjectMissColon, //!< Missing a colon after a name of object member. + kParseErrorObjectMissCommaOrCurlyBracket, //!< Missing a comma or '}' after an object member. + + kParseErrorArrayMissCommaOrSquareBracket, //!< Missing a comma or ']' after an array element. + + kParseErrorStringUnicodeEscapeInvalidHex, //!< Incorrect hex digit after \\u escape in string. + kParseErrorStringUnicodeSurrogateInvalid, //!< The surrogate pair in string is invalid. + kParseErrorStringEscapeInvalid, //!< Invalid escape character in string. + kParseErrorStringMissQuotationMark, //!< Missing a closing quotation mark in string. + kParseErrorStringInvalidEncoding, //!< Invalid encoding in string. + + kParseErrorNumberTooBig, //!< Number too big to be stored in double. + kParseErrorNumberMissFraction, //!< Miss fraction part in number. + kParseErrorNumberMissExponent, //!< Miss exponent in number. + + kParseErrorTermination, //!< Parsing was terminated. + kParseErrorUnspecificSyntaxError //!< Unspecific syntax error. +}; + +//! Result of parsing (wraps ParseErrorCode) +/*! + \ingroup RAPIDJSON_ERRORS + \code + Document doc; + ParseResult ok = doc.Parse("[42]"); + if (!ok) { + fprintf(stderr, "JSON parse error: %s (%u)", + GetParseError_En(ok.Code()), ok.Offset()); + exit(EXIT_FAILURE); + } + \endcode + \see GenericReader::Parse, GenericDocument::Parse +*/ +struct ParseResult { + //!! Unspecified boolean type + typedef bool (ParseResult::*BooleanType)() const; +public: + //! Default constructor, no error. + ParseResult() : code_(kParseErrorNone), offset_(0) {} + //! Constructor to set an error. + ParseResult(ParseErrorCode code, size_t offset) : code_(code), offset_(offset) {} + + //! Get the error code. + ParseErrorCode Code() const { return code_; } + //! Get the error offset, if \ref IsError(), 0 otherwise. + size_t Offset() const { return offset_; } + + //! Explicit conversion to \c bool, returns \c true, iff !\ref IsError(). + operator BooleanType() const { return !IsError() ? &ParseResult::IsError : NULL; } + //! Whether the result is an error. + bool IsError() const { return code_ != kParseErrorNone; } + + bool operator==(const ParseResult& that) const { return code_ == that.code_; } + bool operator==(ParseErrorCode code) const { return code_ == code; } + friend bool operator==(ParseErrorCode code, const ParseResult & err) { return code == err.code_; } + + bool operator!=(const ParseResult& that) const { return !(*this == that); } + bool operator!=(ParseErrorCode code) const { return !(*this == code); } + friend bool operator!=(ParseErrorCode code, const ParseResult & err) { return err != code; } + + //! Reset error code. + void Clear() { Set(kParseErrorNone); } + //! Update error code and offset. + void Set(ParseErrorCode code, size_t offset = 0) { code_ = code; offset_ = offset; } + +private: + ParseErrorCode code_; + size_t offset_; +}; + +//! Function pointer type of GetParseError(). +/*! \ingroup RAPIDJSON_ERRORS + + This is the prototype for \c GetParseError_X(), where \c X is a locale. + User can dynamically change locale in runtime, e.g.: +\code + GetParseErrorFunc GetParseError = GetParseError_En; // or whatever + const RAPIDJSON_ERROR_CHARTYPE* s = GetParseError(document.GetParseErrorCode()); +\endcode +*/ +typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetParseErrorFunc)(ParseErrorCode); + +/////////////////////////////////////////////////////////////////////////////// +// ValidateErrorCode + +//! Error codes when validating. +/*! \ingroup RAPIDJSON_ERRORS + \see GenericSchemaValidator +*/ +enum ValidateErrorCode { + kValidateErrors = -1, //!< Top level error code when kValidateContinueOnErrorsFlag set. + kValidateErrorNone = 0, //!< No error. + + kValidateErrorMultipleOf, //!< Number is not a multiple of the 'multipleOf' value. + kValidateErrorMaximum, //!< Number is greater than the 'maximum' value. + kValidateErrorExclusiveMaximum, //!< Number is greater than or equal to the 'maximum' value. + kValidateErrorMinimum, //!< Number is less than the 'minimum' value. + kValidateErrorExclusiveMinimum, //!< Number is less than or equal to the 'minimum' value. + + kValidateErrorMaxLength, //!< String is longer than the 'maxLength' value. + kValidateErrorMinLength, //!< String is longer than the 'maxLength' value. + kValidateErrorPattern, //!< String does not match the 'pattern' regular expression. + + kValidateErrorMaxItems, //!< Array is longer than the 'maxItems' value. + kValidateErrorMinItems, //!< Array is shorter than the 'minItems' value. + kValidateErrorUniqueItems, //!< Array has duplicate items but 'uniqueItems' is true. + kValidateErrorAdditionalItems, //!< Array has additional items that are not allowed by the schema. + + kValidateErrorMaxProperties, //!< Object has more members than 'maxProperties' value. + kValidateErrorMinProperties, //!< Object has less members than 'minProperties' value. + kValidateErrorRequired, //!< Object is missing one or more members required by the schema. + kValidateErrorAdditionalProperties, //!< Object has additional members that are not allowed by the schema. + kValidateErrorPatternProperties, //!< See other errors. + kValidateErrorDependencies, //!< Object has missing property or schema dependencies. + + kValidateErrorEnum, //!< Property has a value that is not one of its allowed enumerated values. + kValidateErrorType, //!< Property has a type that is not allowed by the schema. + + kValidateErrorOneOf, //!< Property did not match any of the sub-schemas specified by 'oneOf'. + kValidateErrorOneOfMatch, //!< Property matched more than one of the sub-schemas specified by 'oneOf'. + kValidateErrorAllOf, //!< Property did not match all of the sub-schemas specified by 'allOf'. + kValidateErrorAnyOf, //!< Property did not match any of the sub-schemas specified by 'anyOf'. + kValidateErrorNot, //!< Property matched the sub-schema specified by 'not'. + + kValidateErrorReadOnly, //!< Property is read-only but has been provided when validation is for writing + kValidateErrorWriteOnly //!< Property is write-only but has been provided when validation is for reading +}; + +//! Function pointer type of GetValidateError(). +/*! \ingroup RAPIDJSON_ERRORS + + This is the prototype for \c GetValidateError_X(), where \c X is a locale. + User can dynamically change locale in runtime, e.g.: +\code + GetValidateErrorFunc GetValidateError = GetValidateError_En; // or whatever + const RAPIDJSON_ERROR_CHARTYPE* s = GetValidateError(validator.GetInvalidSchemaCode()); +\endcode +*/ +typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetValidateErrorFunc)(ValidateErrorCode); + +/////////////////////////////////////////////////////////////////////////////// +// SchemaErrorCode + +//! Error codes when validating. +/*! \ingroup RAPIDJSON_ERRORS + \see GenericSchemaValidator +*/ +enum SchemaErrorCode { + kSchemaErrorNone = 0, //!< No error. + + kSchemaErrorStartUnknown, //!< Pointer to start of schema does not resolve to a location in the document + kSchemaErrorRefPlainName, //!< $ref fragment must be a JSON pointer + kSchemaErrorRefInvalid, //!< $ref must not be an empty string + kSchemaErrorRefPointerInvalid, //!< $ref fragment is not a valid JSON pointer at offset + kSchemaErrorRefUnknown, //!< $ref does not resolve to a location in the target document + kSchemaErrorRefCyclical, //!< $ref is cyclical + kSchemaErrorRefNoRemoteProvider, //!< $ref is remote but there is no remote provider + kSchemaErrorRefNoRemoteSchema, //!< $ref is remote but the remote provider did not return a schema + kSchemaErrorRegexInvalid, //!< Invalid regular expression in 'pattern' or 'patternProperties' + kSchemaErrorSpecUnknown, //!< JSON schema draft or OpenAPI version is not recognized + kSchemaErrorSpecUnsupported, //!< JSON schema draft or OpenAPI version is not supported + kSchemaErrorSpecIllegal, //!< Both JSON schema draft and OpenAPI version found in document + kSchemaErrorReadOnlyAndWriteOnly //!< Property must not be both 'readOnly' and 'writeOnly' +}; + +//! Function pointer type of GetSchemaError(). +/*! \ingroup RAPIDJSON_ERRORS + + This is the prototype for \c GetSchemaError_X(), where \c X is a locale. + User can dynamically change locale in runtime, e.g.: +\code + GetSchemaErrorFunc GetSchemaError = GetSchemaError_En; // or whatever + const RAPIDJSON_ERROR_CHARTYPE* s = GetSchemaError(validator.GetInvalidSchemaCode()); +\endcode +*/ +typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetSchemaErrorFunc)(SchemaErrorCode); + +/////////////////////////////////////////////////////////////////////////////// +// PointerParseErrorCode + +//! Error code of JSON pointer parsing. +/*! \ingroup RAPIDJSON_ERRORS + \see GenericPointer::GenericPointer, GenericPointer::GetParseErrorCode +*/ +enum PointerParseErrorCode { + kPointerParseErrorNone = 0, //!< The parse is successful + + kPointerParseErrorTokenMustBeginWithSolidus, //!< A token must begin with a '/' + kPointerParseErrorInvalidEscape, //!< Invalid escape + kPointerParseErrorInvalidPercentEncoding, //!< Invalid percent encoding in URI fragment + kPointerParseErrorCharacterMustPercentEncode //!< A character must percent encoded in URI fragment +}; + +//! Function pointer type of GetPointerParseError(). +/*! \ingroup RAPIDJSON_ERRORS + + This is the prototype for \c GetPointerParseError_X(), where \c X is a locale. + User can dynamically change locale in runtime, e.g.: +\code + GetPointerParseErrorFunc GetPointerParseError = GetPointerParseError_En; // or whatever + const RAPIDJSON_ERROR_CHARTYPE* s = GetPointerParseError(pointer.GetParseErrorCode()); +\endcode +*/ +typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetPointerParseErrorFunc)(PointerParseErrorCode); + + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_ERROR_ERROR_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/filereadstream.h b/include/liblvgl/libs/thorvg/rapidjson/filereadstream.h new file mode 100644 index 00000000..3daff092 --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/filereadstream.h @@ -0,0 +1,99 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_FILEREADSTREAM_H_ +#define RAPIDJSON_FILEREADSTREAM_H_ + +#include "stream.h" +#include + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +RAPIDJSON_DIAG_OFF(unreachable-code) +RAPIDJSON_DIAG_OFF(missing-noreturn) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! File byte stream for input using fread(). +/*! + \note implements Stream concept +*/ +class FileReadStream { +public: + typedef char Ch; //!< Character type (byte). + + //! Constructor. + /*! + \param fp File pointer opened for read. + \param buffer user-supplied buffer. + \param bufferSize size of buffer in bytes. Must >=4 bytes. + */ + FileReadStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferSize_(bufferSize), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) { + RAPIDJSON_ASSERT(fp_ != 0); + RAPIDJSON_ASSERT(bufferSize >= 4); + Read(); + } + + Ch Peek() const { return *current_; } + Ch Take() { Ch c = *current_; Read(); return c; } + size_t Tell() const { return count_ + static_cast(current_ - buffer_); } + + // Not implemented + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + + // For encoding detection only. + const Ch* Peek4() const { + return (current_ + 4 - !eof_ <= bufferLast_) ? current_ : 0; + } + +private: + void Read() { + if (current_ < bufferLast_) + ++current_; + else if (!eof_) { + count_ += readCount_; + readCount_ = std::fread(buffer_, 1, bufferSize_, fp_); + bufferLast_ = buffer_ + readCount_ - 1; + current_ = buffer_; + + if (readCount_ < bufferSize_) { + buffer_[readCount_] = '\0'; + ++bufferLast_; + eof_ = true; + } + } + } + + std::FILE* fp_; + Ch *buffer_; + size_t bufferSize_; + Ch *bufferLast_; + Ch *current_; + size_t readCount_; + size_t count_; //!< Number of characters read + bool eof_; +}; + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_FILESTREAM_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/filewritestream.h b/include/liblvgl/libs/thorvg/rapidjson/filewritestream.h new file mode 100644 index 00000000..8a78ef7f --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/filewritestream.h @@ -0,0 +1,104 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_FILEWRITESTREAM_H_ +#define RAPIDJSON_FILEWRITESTREAM_H_ + +#include "stream.h" +#include + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(unreachable-code) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Wrapper of C file stream for output using fwrite(). +/*! + \note implements Stream concept +*/ +class FileWriteStream { +public: + typedef char Ch; //!< Character type. Only support char. + + FileWriteStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferEnd_(buffer + bufferSize), current_(buffer_) { + RAPIDJSON_ASSERT(fp_ != 0); + } + + void Put(char c) { + if (current_ >= bufferEnd_) + Flush(); + + *current_++ = c; + } + + void PutN(char c, size_t n) { + size_t avail = static_cast(bufferEnd_ - current_); + while (n > avail) { + std::memset(current_, c, avail); + current_ += avail; + Flush(); + n -= avail; + avail = static_cast(bufferEnd_ - current_); + } + + if (n > 0) { + std::memset(current_, c, n); + current_ += n; + } + } + + void Flush() { + if (current_ != buffer_) { + size_t result = std::fwrite(buffer_, 1, static_cast(current_ - buffer_), fp_); + if (result < static_cast(current_ - buffer_)) { + // failure deliberately ignored at this time + // added to avoid warn_unused_result build errors + } + current_ = buffer_; + } + } + + // Not implemented + char Peek() const { RAPIDJSON_ASSERT(false); return 0; } + char Take() { RAPIDJSON_ASSERT(false); return 0; } + size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } + char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + // Prohibit copy constructor & assignment operator. + FileWriteStream(const FileWriteStream&); + FileWriteStream& operator=(const FileWriteStream&); + + std::FILE* fp_; + char *buffer_; + char *bufferEnd_; + char *current_; +}; + +//! Implement specialized version of PutN() with memset() for better performance. +template<> +inline void PutN(FileWriteStream& stream, char c, size_t n) { + stream.PutN(c, n); +} + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_FILESTREAM_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/fwd.h b/include/liblvgl/libs/thorvg/rapidjson/fwd.h new file mode 100644 index 00000000..07358d8c --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/fwd.h @@ -0,0 +1,151 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_FWD_H_ +#define RAPIDJSON_FWD_H_ + +#include "rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN + +// encodings.h + +template struct UTF8; +template struct UTF16; +template struct UTF16BE; +template struct UTF16LE; +template struct UTF32; +template struct UTF32BE; +template struct UTF32LE; +template struct ASCII; +template struct AutoUTF; + +template +struct Transcoder; + +// allocators.h + +class CrtAllocator; + +template +class MemoryPoolAllocator; + +// stream.h + +template +struct GenericStringStream; + +typedef GenericStringStream > StringStream; + +template +struct GenericInsituStringStream; + +typedef GenericInsituStringStream > InsituStringStream; + +// stringbuffer.h + +template +class GenericStringBuffer; + +typedef GenericStringBuffer, CrtAllocator> StringBuffer; + +// filereadstream.h + +class FileReadStream; + +// filewritestream.h + +class FileWriteStream; + +// memorybuffer.h + +template +struct GenericMemoryBuffer; + +typedef GenericMemoryBuffer MemoryBuffer; + +// memorystream.h + +struct MemoryStream; + +// reader.h + +template +struct BaseReaderHandler; + +template +class GenericReader; + +typedef GenericReader, UTF8, CrtAllocator> Reader; + +// writer.h + +template +class Writer; + +// prettywriter.h + +template +class PrettyWriter; + +// document.h + +template +class GenericMember; + +template +class GenericMemberIterator; + +template +struct GenericStringRef; + +template +class GenericValue; + +typedef GenericValue, MemoryPoolAllocator > Value; + +template +class GenericDocument; + +typedef GenericDocument, MemoryPoolAllocator, CrtAllocator> Document; + +// pointer.h + +template +class GenericPointer; + +typedef GenericPointer Pointer; + +// schema.h + +template +class IGenericRemoteSchemaDocumentProvider; + +template +class GenericSchemaDocument; + +typedef GenericSchemaDocument SchemaDocument; +typedef IGenericRemoteSchemaDocumentProvider IRemoteSchemaDocumentProvider; + +template < + typename SchemaDocumentType, + typename OutputHandler, + typename StateAllocator> +class GenericSchemaValidator; + +typedef GenericSchemaValidator, void>, CrtAllocator> SchemaValidator; + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_RAPIDJSONFWD_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/internal/biginteger.h b/include/liblvgl/libs/thorvg/rapidjson/internal/biginteger.h new file mode 100644 index 00000000..09d0387f --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/internal/biginteger.h @@ -0,0 +1,297 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_BIGINTEGER_H_ +#define RAPIDJSON_BIGINTEGER_H_ + +#include "../rapidjson.h" + +#if defined(_MSC_VER) && !defined(__INTEL_COMPILER) && defined(_M_AMD64) +#include // for _umul128 +#if !defined(_ARM64EC_) +#pragma intrinsic(_umul128) +#else +#pragma comment(lib,"softintrin") +#endif +#endif + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +class BigInteger { +public: + typedef uint64_t Type; + + BigInteger(const BigInteger& rhs) : count_(rhs.count_) { + std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type)); + } + + explicit BigInteger(uint64_t u) : count_(1) { + digits_[0] = u; + } + + template + BigInteger(const Ch* decimals, size_t length) : count_(1) { + RAPIDJSON_ASSERT(length > 0); + digits_[0] = 0; + size_t i = 0; + const size_t kMaxDigitPerIteration = 19; // 2^64 = 18446744073709551616 > 10^19 + while (length >= kMaxDigitPerIteration) { + AppendDecimal64(decimals + i, decimals + i + kMaxDigitPerIteration); + length -= kMaxDigitPerIteration; + i += kMaxDigitPerIteration; + } + + if (length > 0) + AppendDecimal64(decimals + i, decimals + i + length); + } + + BigInteger& operator=(const BigInteger &rhs) + { + if (this != &rhs) { + count_ = rhs.count_; + std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type)); + } + return *this; + } + + BigInteger& operator=(uint64_t u) { + digits_[0] = u; + count_ = 1; + return *this; + } + + BigInteger& operator+=(uint64_t u) { + Type backup = digits_[0]; + digits_[0] += u; + for (size_t i = 0; i < count_ - 1; i++) { + if (digits_[i] >= backup) + return *this; // no carry + backup = digits_[i + 1]; + digits_[i + 1] += 1; + } + + // Last carry + if (digits_[count_ - 1] < backup) + PushBack(1); + + return *this; + } + + BigInteger& operator*=(uint64_t u) { + if (u == 0) return *this = 0; + if (u == 1) return *this; + if (*this == 1) return *this = u; + + uint64_t k = 0; + for (size_t i = 0; i < count_; i++) { + uint64_t hi; + digits_[i] = MulAdd64(digits_[i], u, k, &hi); + k = hi; + } + + if (k > 0) + PushBack(k); + + return *this; + } + + BigInteger& operator*=(uint32_t u) { + if (u == 0) return *this = 0; + if (u == 1) return *this; + if (*this == 1) return *this = u; + + uint64_t k = 0; + for (size_t i = 0; i < count_; i++) { + const uint64_t c = digits_[i] >> 32; + const uint64_t d = digits_[i] & 0xFFFFFFFF; + const uint64_t uc = u * c; + const uint64_t ud = u * d; + const uint64_t p0 = ud + k; + const uint64_t p1 = uc + (p0 >> 32); + digits_[i] = (p0 & 0xFFFFFFFF) | (p1 << 32); + k = p1 >> 32; + } + + if (k > 0) + PushBack(k); + + return *this; + } + + BigInteger& operator<<=(size_t shift) { + if (IsZero() || shift == 0) return *this; + + size_t offset = shift / kTypeBit; + size_t interShift = shift % kTypeBit; + RAPIDJSON_ASSERT(count_ + offset <= kCapacity); + + if (interShift == 0) { + std::memmove(digits_ + offset, digits_, count_ * sizeof(Type)); + count_ += offset; + } + else { + digits_[count_] = 0; + for (size_t i = count_; i > 0; i--) + digits_[i + offset] = (digits_[i] << interShift) | (digits_[i - 1] >> (kTypeBit - interShift)); + digits_[offset] = digits_[0] << interShift; + count_ += offset; + if (digits_[count_]) + count_++; + } + + std::memset(digits_, 0, offset * sizeof(Type)); + + return *this; + } + + bool operator==(const BigInteger& rhs) const { + return count_ == rhs.count_ && std::memcmp(digits_, rhs.digits_, count_ * sizeof(Type)) == 0; + } + + bool operator==(const Type rhs) const { + return count_ == 1 && digits_[0] == rhs; + } + + BigInteger& MultiplyPow5(unsigned exp) { + static const uint32_t kPow5[12] = { + 5, + 5 * 5, + 5 * 5 * 5, + 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 + }; + if (exp == 0) return *this; + for (; exp >= 27; exp -= 27) *this *= RAPIDJSON_UINT64_C2(0X6765C793, 0XFA10079D); // 5^27 + for (; exp >= 13; exp -= 13) *this *= static_cast(1220703125u); // 5^13 + if (exp > 0) *this *= kPow5[exp - 1]; + return *this; + } + + // Compute absolute difference of this and rhs. + // Assume this != rhs + bool Difference(const BigInteger& rhs, BigInteger* out) const { + int cmp = Compare(rhs); + RAPIDJSON_ASSERT(cmp != 0); + const BigInteger *a, *b; // Makes a > b + bool ret; + if (cmp < 0) { a = &rhs; b = this; ret = true; } + else { a = this; b = &rhs; ret = false; } + + Type borrow = 0; + for (size_t i = 0; i < a->count_; i++) { + Type d = a->digits_[i] - borrow; + if (i < b->count_) + d -= b->digits_[i]; + borrow = (d > a->digits_[i]) ? 1 : 0; + out->digits_[i] = d; + if (d != 0) + out->count_ = i + 1; + } + + return ret; + } + + int Compare(const BigInteger& rhs) const { + if (count_ != rhs.count_) + return count_ < rhs.count_ ? -1 : 1; + + for (size_t i = count_; i-- > 0;) + if (digits_[i] != rhs.digits_[i]) + return digits_[i] < rhs.digits_[i] ? -1 : 1; + + return 0; + } + + size_t GetCount() const { return count_; } + Type GetDigit(size_t index) const { RAPIDJSON_ASSERT(index < count_); return digits_[index]; } + bool IsZero() const { return count_ == 1 && digits_[0] == 0; } + +private: + template + void AppendDecimal64(const Ch* begin, const Ch* end) { + uint64_t u = ParseUint64(begin, end); + if (IsZero()) + *this = u; + else { + unsigned exp = static_cast(end - begin); + (MultiplyPow5(exp) <<= exp) += u; // *this = *this * 10^exp + u + } + } + + void PushBack(Type digit) { + RAPIDJSON_ASSERT(count_ < kCapacity); + digits_[count_++] = digit; + } + + template + static uint64_t ParseUint64(const Ch* begin, const Ch* end) { + uint64_t r = 0; + for (const Ch* p = begin; p != end; ++p) { + RAPIDJSON_ASSERT(*p >= Ch('0') && *p <= Ch('9')); + r = r * 10u + static_cast(*p - Ch('0')); + } + return r; + } + + // Assume a * b + k < 2^128 + static uint64_t MulAdd64(uint64_t a, uint64_t b, uint64_t k, uint64_t* outHigh) { +#if defined(_MSC_VER) && defined(_M_AMD64) + uint64_t low = _umul128(a, b, outHigh) + k; + if (low < k) + (*outHigh)++; + return low; +#elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__) + __extension__ typedef unsigned __int128 uint128; + uint128 p = static_cast(a) * static_cast(b); + p += k; + *outHigh = static_cast(p >> 64); + return static_cast(p); +#else + const uint64_t a0 = a & 0xFFFFFFFF, a1 = a >> 32, b0 = b & 0xFFFFFFFF, b1 = b >> 32; + uint64_t x0 = a0 * b0, x1 = a0 * b1, x2 = a1 * b0, x3 = a1 * b1; + x1 += (x0 >> 32); // can't give carry + x1 += x2; + if (x1 < x2) + x3 += (static_cast(1) << 32); + uint64_t lo = (x1 << 32) + (x0 & 0xFFFFFFFF); + uint64_t hi = x3 + (x1 >> 32); + + lo += k; + if (lo < k) + hi++; + *outHigh = hi; + return lo; +#endif + } + + static const size_t kBitCount = 3328; // 64bit * 54 > 10^1000 + static const size_t kCapacity = kBitCount / sizeof(Type); + static const size_t kTypeBit = sizeof(Type) * 8; + + Type digits_[kCapacity]; + size_t count_; +}; + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_BIGINTEGER_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/internal/clzll.h b/include/liblvgl/libs/thorvg/rapidjson/internal/clzll.h new file mode 100644 index 00000000..8fc5118a --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/internal/clzll.h @@ -0,0 +1,71 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_CLZLL_H_ +#define RAPIDJSON_CLZLL_H_ + +#include "../rapidjson.h" + +#if defined(_MSC_VER) && !defined(UNDER_CE) +#include +#if defined(_WIN64) +#pragma intrinsic(_BitScanReverse64) +#else +#pragma intrinsic(_BitScanReverse) +#endif +#endif + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +inline uint32_t clzll(uint64_t x) { + // Passing 0 to __builtin_clzll is UB in GCC and results in an + // infinite loop in the software implementation. + RAPIDJSON_ASSERT(x != 0); + +#if defined(_MSC_VER) && !defined(UNDER_CE) + unsigned long r = 0; +#if defined(_WIN64) + _BitScanReverse64(&r, x); +#else + // Scan the high 32 bits. + if (_BitScanReverse(&r, static_cast(x >> 32))) + return 63 - (r + 32); + + // Scan the low 32 bits. + _BitScanReverse(&r, static_cast(x & 0xFFFFFFFF)); +#endif // _WIN64 + + return 63 - r; +#elif (defined(__GNUC__) && __GNUC__ >= 4) || RAPIDJSON_HAS_BUILTIN(__builtin_clzll) + // __builtin_clzll wrapper + return static_cast(__builtin_clzll(x)); +#else + // naive version + uint32_t r = 0; + while (!(x & (static_cast(1) << 63))) { + x <<= 1; + ++r; + } + + return r; +#endif // _MSC_VER +} + +#define RAPIDJSON_CLZLL RAPIDJSON_NAMESPACE::internal::clzll + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_CLZLL_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/internal/diyfp.h b/include/liblvgl/libs/thorvg/rapidjson/internal/diyfp.h new file mode 100644 index 00000000..1f60fb60 --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/internal/diyfp.h @@ -0,0 +1,261 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +// This is a C++ header-only implementation of Grisu2 algorithm from the publication: +// Loitsch, Florian. "Printing floating-point numbers quickly and accurately with +// integers." ACM Sigplan Notices 45.6 (2010): 233-243. + +#ifndef RAPIDJSON_DIYFP_H_ +#define RAPIDJSON_DIYFP_H_ + +#include "../rapidjson.h" +#include "clzll.h" +#include + +#if defined(_MSC_VER) && defined(_M_AMD64) && !defined(__INTEL_COMPILER) +#include +#if !defined(_ARM64EC_) +#pragma intrinsic(_umul128) +#else +#pragma comment(lib,"softintrin") +#endif +#endif + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +#endif + +struct DiyFp { + DiyFp() : f(), e() {} + + DiyFp(uint64_t fp, int exp) : f(fp), e(exp) {} + + explicit DiyFp(double d) { + union { + double d; + uint64_t u64; + } u = { d }; + + int biased_e = static_cast((u.u64 & kDpExponentMask) >> kDpSignificandSize); + uint64_t significand = (u.u64 & kDpSignificandMask); + if (biased_e != 0) { + f = significand + kDpHiddenBit; + e = biased_e - kDpExponentBias; + } + else { + f = significand; + e = kDpMinExponent + 1; + } + } + + DiyFp operator-(const DiyFp& rhs) const { + return DiyFp(f - rhs.f, e); + } + + DiyFp operator*(const DiyFp& rhs) const { +#if defined(_MSC_VER) && defined(_M_AMD64) + uint64_t h; + uint64_t l = _umul128(f, rhs.f, &h); + if (l & (uint64_t(1) << 63)) // rounding + h++; + return DiyFp(h, e + rhs.e + 64); +#elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__) + __extension__ typedef unsigned __int128 uint128; + uint128 p = static_cast(f) * static_cast(rhs.f); + uint64_t h = static_cast(p >> 64); + uint64_t l = static_cast(p); + if (l & (uint64_t(1) << 63)) // rounding + h++; + return DiyFp(h, e + rhs.e + 64); +#else + const uint64_t M32 = 0xFFFFFFFF; + const uint64_t a = f >> 32; + const uint64_t b = f & M32; + const uint64_t c = rhs.f >> 32; + const uint64_t d = rhs.f & M32; + const uint64_t ac = a * c; + const uint64_t bc = b * c; + const uint64_t ad = a * d; + const uint64_t bd = b * d; + uint64_t tmp = (bd >> 32) + (ad & M32) + (bc & M32); + tmp += 1U << 31; /// mult_round + return DiyFp(ac + (ad >> 32) + (bc >> 32) + (tmp >> 32), e + rhs.e + 64); +#endif + } + + DiyFp Normalize() const { + int s = static_cast(clzll(f)); + return DiyFp(f << s, e - s); + } + + DiyFp NormalizeBoundary() const { + DiyFp res = *this; + while (!(res.f & (kDpHiddenBit << 1))) { + res.f <<= 1; + res.e--; + } + res.f <<= (kDiySignificandSize - kDpSignificandSize - 2); + res.e = res.e - (kDiySignificandSize - kDpSignificandSize - 2); + return res; + } + + void NormalizedBoundaries(DiyFp* minus, DiyFp* plus) const { + DiyFp pl = DiyFp((f << 1) + 1, e - 1).NormalizeBoundary(); + DiyFp mi = (f == kDpHiddenBit) ? DiyFp((f << 2) - 1, e - 2) : DiyFp((f << 1) - 1, e - 1); + mi.f <<= mi.e - pl.e; + mi.e = pl.e; + *plus = pl; + *minus = mi; + } + + double ToDouble() const { + union { + double d; + uint64_t u64; + }u; + RAPIDJSON_ASSERT(f <= kDpHiddenBit + kDpSignificandMask); + if (e < kDpDenormalExponent) { + // Underflow. + return 0.0; + } + if (e >= kDpMaxExponent) { + // Overflow. + return std::numeric_limits::infinity(); + } + const uint64_t be = (e == kDpDenormalExponent && (f & kDpHiddenBit) == 0) ? 0 : + static_cast(e + kDpExponentBias); + u.u64 = (f & kDpSignificandMask) | (be << kDpSignificandSize); + return u.d; + } + + static const int kDiySignificandSize = 64; + static const int kDpSignificandSize = 52; + static const int kDpExponentBias = 0x3FF + kDpSignificandSize; + static const int kDpMaxExponent = 0x7FF - kDpExponentBias; + static const int kDpMinExponent = -kDpExponentBias; + static const int kDpDenormalExponent = -kDpExponentBias + 1; + static const uint64_t kDpExponentMask = RAPIDJSON_UINT64_C2(0x7FF00000, 0x00000000); + static const uint64_t kDpSignificandMask = RAPIDJSON_UINT64_C2(0x000FFFFF, 0xFFFFFFFF); + static const uint64_t kDpHiddenBit = RAPIDJSON_UINT64_C2(0x00100000, 0x00000000); + + uint64_t f; + int e; +}; + +inline DiyFp GetCachedPowerByIndex(size_t index) { + // 10^-348, 10^-340, ..., 10^340 + static const uint64_t kCachedPowers_F[] = { + RAPIDJSON_UINT64_C2(0xfa8fd5a0, 0x081c0288), RAPIDJSON_UINT64_C2(0xbaaee17f, 0xa23ebf76), + RAPIDJSON_UINT64_C2(0x8b16fb20, 0x3055ac76), RAPIDJSON_UINT64_C2(0xcf42894a, 0x5dce35ea), + RAPIDJSON_UINT64_C2(0x9a6bb0aa, 0x55653b2d), RAPIDJSON_UINT64_C2(0xe61acf03, 0x3d1a45df), + RAPIDJSON_UINT64_C2(0xab70fe17, 0xc79ac6ca), RAPIDJSON_UINT64_C2(0xff77b1fc, 0xbebcdc4f), + RAPIDJSON_UINT64_C2(0xbe5691ef, 0x416bd60c), RAPIDJSON_UINT64_C2(0x8dd01fad, 0x907ffc3c), + RAPIDJSON_UINT64_C2(0xd3515c28, 0x31559a83), RAPIDJSON_UINT64_C2(0x9d71ac8f, 0xada6c9b5), + RAPIDJSON_UINT64_C2(0xea9c2277, 0x23ee8bcb), RAPIDJSON_UINT64_C2(0xaecc4991, 0x4078536d), + RAPIDJSON_UINT64_C2(0x823c1279, 0x5db6ce57), RAPIDJSON_UINT64_C2(0xc2109436, 0x4dfb5637), + RAPIDJSON_UINT64_C2(0x9096ea6f, 0x3848984f), RAPIDJSON_UINT64_C2(0xd77485cb, 0x25823ac7), + RAPIDJSON_UINT64_C2(0xa086cfcd, 0x97bf97f4), RAPIDJSON_UINT64_C2(0xef340a98, 0x172aace5), + RAPIDJSON_UINT64_C2(0xb23867fb, 0x2a35b28e), RAPIDJSON_UINT64_C2(0x84c8d4df, 0xd2c63f3b), + RAPIDJSON_UINT64_C2(0xc5dd4427, 0x1ad3cdba), RAPIDJSON_UINT64_C2(0x936b9fce, 0xbb25c996), + RAPIDJSON_UINT64_C2(0xdbac6c24, 0x7d62a584), RAPIDJSON_UINT64_C2(0xa3ab6658, 0x0d5fdaf6), + RAPIDJSON_UINT64_C2(0xf3e2f893, 0xdec3f126), RAPIDJSON_UINT64_C2(0xb5b5ada8, 0xaaff80b8), + RAPIDJSON_UINT64_C2(0x87625f05, 0x6c7c4a8b), RAPIDJSON_UINT64_C2(0xc9bcff60, 0x34c13053), + RAPIDJSON_UINT64_C2(0x964e858c, 0x91ba2655), RAPIDJSON_UINT64_C2(0xdff97724, 0x70297ebd), + RAPIDJSON_UINT64_C2(0xa6dfbd9f, 0xb8e5b88f), RAPIDJSON_UINT64_C2(0xf8a95fcf, 0x88747d94), + RAPIDJSON_UINT64_C2(0xb9447093, 0x8fa89bcf), RAPIDJSON_UINT64_C2(0x8a08f0f8, 0xbf0f156b), + RAPIDJSON_UINT64_C2(0xcdb02555, 0x653131b6), RAPIDJSON_UINT64_C2(0x993fe2c6, 0xd07b7fac), + RAPIDJSON_UINT64_C2(0xe45c10c4, 0x2a2b3b06), RAPIDJSON_UINT64_C2(0xaa242499, 0x697392d3), + RAPIDJSON_UINT64_C2(0xfd87b5f2, 0x8300ca0e), RAPIDJSON_UINT64_C2(0xbce50864, 0x92111aeb), + RAPIDJSON_UINT64_C2(0x8cbccc09, 0x6f5088cc), RAPIDJSON_UINT64_C2(0xd1b71758, 0xe219652c), + RAPIDJSON_UINT64_C2(0x9c400000, 0x00000000), RAPIDJSON_UINT64_C2(0xe8d4a510, 0x00000000), + RAPIDJSON_UINT64_C2(0xad78ebc5, 0xac620000), RAPIDJSON_UINT64_C2(0x813f3978, 0xf8940984), + RAPIDJSON_UINT64_C2(0xc097ce7b, 0xc90715b3), RAPIDJSON_UINT64_C2(0x8f7e32ce, 0x7bea5c70), + RAPIDJSON_UINT64_C2(0xd5d238a4, 0xabe98068), RAPIDJSON_UINT64_C2(0x9f4f2726, 0x179a2245), + RAPIDJSON_UINT64_C2(0xed63a231, 0xd4c4fb27), RAPIDJSON_UINT64_C2(0xb0de6538, 0x8cc8ada8), + RAPIDJSON_UINT64_C2(0x83c7088e, 0x1aab65db), RAPIDJSON_UINT64_C2(0xc45d1df9, 0x42711d9a), + RAPIDJSON_UINT64_C2(0x924d692c, 0xa61be758), RAPIDJSON_UINT64_C2(0xda01ee64, 0x1a708dea), + RAPIDJSON_UINT64_C2(0xa26da399, 0x9aef774a), RAPIDJSON_UINT64_C2(0xf209787b, 0xb47d6b85), + RAPIDJSON_UINT64_C2(0xb454e4a1, 0x79dd1877), RAPIDJSON_UINT64_C2(0x865b8692, 0x5b9bc5c2), + RAPIDJSON_UINT64_C2(0xc83553c5, 0xc8965d3d), RAPIDJSON_UINT64_C2(0x952ab45c, 0xfa97a0b3), + RAPIDJSON_UINT64_C2(0xde469fbd, 0x99a05fe3), RAPIDJSON_UINT64_C2(0xa59bc234, 0xdb398c25), + RAPIDJSON_UINT64_C2(0xf6c69a72, 0xa3989f5c), RAPIDJSON_UINT64_C2(0xb7dcbf53, 0x54e9bece), + RAPIDJSON_UINT64_C2(0x88fcf317, 0xf22241e2), RAPIDJSON_UINT64_C2(0xcc20ce9b, 0xd35c78a5), + RAPIDJSON_UINT64_C2(0x98165af3, 0x7b2153df), RAPIDJSON_UINT64_C2(0xe2a0b5dc, 0x971f303a), + RAPIDJSON_UINT64_C2(0xa8d9d153, 0x5ce3b396), RAPIDJSON_UINT64_C2(0xfb9b7cd9, 0xa4a7443c), + RAPIDJSON_UINT64_C2(0xbb764c4c, 0xa7a44410), RAPIDJSON_UINT64_C2(0x8bab8eef, 0xb6409c1a), + RAPIDJSON_UINT64_C2(0xd01fef10, 0xa657842c), RAPIDJSON_UINT64_C2(0x9b10a4e5, 0xe9913129), + RAPIDJSON_UINT64_C2(0xe7109bfb, 0xa19c0c9d), RAPIDJSON_UINT64_C2(0xac2820d9, 0x623bf429), + RAPIDJSON_UINT64_C2(0x80444b5e, 0x7aa7cf85), RAPIDJSON_UINT64_C2(0xbf21e440, 0x03acdd2d), + RAPIDJSON_UINT64_C2(0x8e679c2f, 0x5e44ff8f), RAPIDJSON_UINT64_C2(0xd433179d, 0x9c8cb841), + RAPIDJSON_UINT64_C2(0x9e19db92, 0xb4e31ba9), RAPIDJSON_UINT64_C2(0xeb96bf6e, 0xbadf77d9), + RAPIDJSON_UINT64_C2(0xaf87023b, 0x9bf0ee6b) + }; + static const int16_t kCachedPowers_E[] = { + -1220, -1193, -1166, -1140, -1113, -1087, -1060, -1034, -1007, -980, + -954, -927, -901, -874, -847, -821, -794, -768, -741, -715, + -688, -661, -635, -608, -582, -555, -529, -502, -475, -449, + -422, -396, -369, -343, -316, -289, -263, -236, -210, -183, + -157, -130, -103, -77, -50, -24, 3, 30, 56, 83, + 109, 136, 162, 189, 216, 242, 269, 295, 322, 348, + 375, 402, 428, 455, 481, 508, 534, 561, 588, 614, + 641, 667, 694, 720, 747, 774, 800, 827, 853, 880, + 907, 933, 960, 986, 1013, 1039, 1066 + }; + RAPIDJSON_ASSERT(index < 87); + return DiyFp(kCachedPowers_F[index], kCachedPowers_E[index]); +} + +inline DiyFp GetCachedPower(int e, int* K) { + + //int k = static_cast(ceil((-61 - e) * 0.30102999566398114)) + 374; + double dk = (-61 - e) * 0.30102999566398114 + 347; // dk must be positive, so can do ceiling in positive + int k = static_cast(dk); + if (dk - k > 0.0) + k++; + + unsigned index = static_cast((k >> 3) + 1); + *K = -(-348 + static_cast(index << 3)); // decimal exponent no need lookup table + + return GetCachedPowerByIndex(index); +} + +inline DiyFp GetCachedPower10(int exp, int *outExp) { + RAPIDJSON_ASSERT(exp >= -348); + unsigned index = static_cast(exp + 348) / 8u; + *outExp = -348 + static_cast(index) * 8; + return GetCachedPowerByIndex(index); +} + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +RAPIDJSON_DIAG_OFF(padded) +#endif + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_DIYFP_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/internal/dtoa.h b/include/liblvgl/libs/thorvg/rapidjson/internal/dtoa.h new file mode 100644 index 00000000..91c5756d --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/internal/dtoa.h @@ -0,0 +1,249 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +// This is a C++ header-only implementation of Grisu2 algorithm from the publication: +// Loitsch, Florian. "Printing floating-point numbers quickly and accurately with +// integers." ACM Sigplan Notices 45.6 (2010): 233-243. + +#ifndef RAPIDJSON_DTOA_ +#define RAPIDJSON_DTOA_ + +#include "itoa.h" // GetDigitsLut() +#include "diyfp.h" +#include "ieee754.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +RAPIDJSON_DIAG_OFF(array-bounds) // some gcc versions generate wrong warnings https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59124 +#endif + +inline void GrisuRound(char* buffer, int len, uint64_t delta, uint64_t rest, uint64_t ten_kappa, uint64_t wp_w) { + while (rest < wp_w && delta - rest >= ten_kappa && + (rest + ten_kappa < wp_w || /// closer + wp_w - rest > rest + ten_kappa - wp_w)) { + buffer[len - 1]--; + rest += ten_kappa; + } +} + +inline int CountDecimalDigit32(uint32_t n) { + // Simple pure C++ implementation was faster than __builtin_clz version in this situation. + if (n < 10) return 1; + if (n < 100) return 2; + if (n < 1000) return 3; + if (n < 10000) return 4; + if (n < 100000) return 5; + if (n < 1000000) return 6; + if (n < 10000000) return 7; + if (n < 100000000) return 8; + // Will not reach 10 digits in DigitGen() + //if (n < 1000000000) return 9; + //return 10; + return 9; +} + +inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buffer, int* len, int* K) { + static const uint64_t kPow10[] = { 1ULL, 10ULL, 100ULL, 1000ULL, 10000ULL, 100000ULL, 1000000ULL, 10000000ULL, 100000000ULL, + 1000000000ULL, 10000000000ULL, 100000000000ULL, 1000000000000ULL, + 10000000000000ULL, 100000000000000ULL, 1000000000000000ULL, + 10000000000000000ULL, 100000000000000000ULL, 1000000000000000000ULL, + 10000000000000000000ULL }; + const DiyFp one(uint64_t(1) << -Mp.e, Mp.e); + const DiyFp wp_w = Mp - W; + uint32_t p1 = static_cast(Mp.f >> -one.e); + uint64_t p2 = Mp.f & (one.f - 1); + int kappa = CountDecimalDigit32(p1); // kappa in [0, 9] + *len = 0; + + while (kappa > 0) { + uint32_t d = 0; + switch (kappa) { + case 9: d = p1 / 100000000; p1 %= 100000000; break; + case 8: d = p1 / 10000000; p1 %= 10000000; break; + case 7: d = p1 / 1000000; p1 %= 1000000; break; + case 6: d = p1 / 100000; p1 %= 100000; break; + case 5: d = p1 / 10000; p1 %= 10000; break; + case 4: d = p1 / 1000; p1 %= 1000; break; + case 3: d = p1 / 100; p1 %= 100; break; + case 2: d = p1 / 10; p1 %= 10; break; + case 1: d = p1; p1 = 0; break; + default:; + } + if (d || *len) + buffer[(*len)++] = static_cast('0' + static_cast(d)); + kappa--; + uint64_t tmp = (static_cast(p1) << -one.e) + p2; + if (tmp <= delta) { + *K += kappa; + GrisuRound(buffer, *len, delta, tmp, kPow10[kappa] << -one.e, wp_w.f); + return; + } + } + + // kappa = 0 + for (;;) { + p2 *= 10; + delta *= 10; + char d = static_cast(p2 >> -one.e); + if (d || *len) + buffer[(*len)++] = static_cast('0' + d); + p2 &= one.f - 1; + kappa--; + if (p2 < delta) { + *K += kappa; + int index = -kappa; + GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * (index < 20 ? kPow10[index] : 0)); + return; + } + } +} + +inline void Grisu2(double value, char* buffer, int* length, int* K) { + const DiyFp v(value); + DiyFp w_m, w_p; + v.NormalizedBoundaries(&w_m, &w_p); + + const DiyFp c_mk = GetCachedPower(w_p.e, K); + const DiyFp W = v.Normalize() * c_mk; + DiyFp Wp = w_p * c_mk; + DiyFp Wm = w_m * c_mk; + Wm.f++; + Wp.f--; + DigitGen(W, Wp, Wp.f - Wm.f, buffer, length, K); +} + +inline char* WriteExponent(int K, char* buffer) { + if (K < 0) { + *buffer++ = '-'; + K = -K; + } + + if (K >= 100) { + *buffer++ = static_cast('0' + static_cast(K / 100)); + K %= 100; + const char* d = GetDigitsLut() + K * 2; + *buffer++ = d[0]; + *buffer++ = d[1]; + } + else if (K >= 10) { + const char* d = GetDigitsLut() + K * 2; + *buffer++ = d[0]; + *buffer++ = d[1]; + } + else + *buffer++ = static_cast('0' + static_cast(K)); + + return buffer; +} + +inline char* Prettify(char* buffer, int length, int k, int maxDecimalPlaces) { + const int kk = length + k; // 10^(kk-1) <= v < 10^kk + + if (0 <= k && kk <= 21) { + // 1234e7 -> 12340000000 + for (int i = length; i < kk; i++) + buffer[i] = '0'; + buffer[kk] = '.'; + buffer[kk + 1] = '0'; + return &buffer[kk + 2]; + } + else if (0 < kk && kk <= 21) { + // 1234e-2 -> 12.34 + std::memmove(&buffer[kk + 1], &buffer[kk], static_cast(length - kk)); + buffer[kk] = '.'; + if (0 > k + maxDecimalPlaces) { + // When maxDecimalPlaces = 2, 1.2345 -> 1.23, 1.102 -> 1.1 + // Remove extra trailing zeros (at least one) after truncation. + for (int i = kk + maxDecimalPlaces; i > kk + 1; i--) + if (buffer[i] != '0') + return &buffer[i + 1]; + return &buffer[kk + 2]; // Reserve one zero + } + else + return &buffer[length + 1]; + } + else if (-6 < kk && kk <= 0) { + // 1234e-6 -> 0.001234 + const int offset = 2 - kk; + std::memmove(&buffer[offset], &buffer[0], static_cast(length)); + buffer[0] = '0'; + buffer[1] = '.'; + for (int i = 2; i < offset; i++) + buffer[i] = '0'; + if (length - kk > maxDecimalPlaces) { + // When maxDecimalPlaces = 2, 0.123 -> 0.12, 0.102 -> 0.1 + // Remove extra trailing zeros (at least one) after truncation. + for (int i = maxDecimalPlaces + 1; i > 2; i--) + if (buffer[i] != '0') + return &buffer[i + 1]; + return &buffer[3]; // Reserve one zero + } + else + return &buffer[length + offset]; + } + else if (kk < -maxDecimalPlaces) { + // Truncate to zero + buffer[0] = '0'; + buffer[1] = '.'; + buffer[2] = '0'; + return &buffer[3]; + } + else if (length == 1) { + // 1e30 + buffer[1] = 'e'; + return WriteExponent(kk - 1, &buffer[2]); + } + else { + // 1234e30 -> 1.234e33 + std::memmove(&buffer[2], &buffer[1], static_cast(length - 1)); + buffer[1] = '.'; + buffer[length + 1] = 'e'; + return WriteExponent(kk - 1, &buffer[0 + length + 2]); + } +} + +inline char* dtoa(double value, char* buffer, int maxDecimalPlaces = 324) { + RAPIDJSON_ASSERT(maxDecimalPlaces >= 1); + Double d(value); + if (d.IsZero()) { + if (d.Sign()) + *buffer++ = '-'; // -0.0, Issue #289 + buffer[0] = '0'; + buffer[1] = '.'; + buffer[2] = '0'; + return &buffer[3]; + } + else { + if (value < 0) { + *buffer++ = '-'; + value = -value; + } + int length, K; + Grisu2(value, buffer, &length, &K); + return Prettify(buffer, length, K, maxDecimalPlaces); + } +} + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_DTOA_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/internal/ieee754.h b/include/liblvgl/libs/thorvg/rapidjson/internal/ieee754.h new file mode 100644 index 00000000..f887d1b2 --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/internal/ieee754.h @@ -0,0 +1,78 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_IEEE754_ +#define RAPIDJSON_IEEE754_ + +#include "../rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +class Double { +public: + Double() {} + Double(double d) : d_(d) {} + Double(uint64_t u) : u_(u) {} + + double Value() const { return d_; } + uint64_t Uint64Value() const { return u_; } + + double NextPositiveDouble() const { + RAPIDJSON_ASSERT(!Sign()); + return Double(u_ + 1).Value(); + } + + bool Sign() const { return (u_ & kSignMask) != 0; } + uint64_t Significand() const { return u_ & kSignificandMask; } + int Exponent() const { return static_cast(((u_ & kExponentMask) >> kSignificandSize) - kExponentBias); } + + bool IsNan() const { return (u_ & kExponentMask) == kExponentMask && Significand() != 0; } + bool IsInf() const { return (u_ & kExponentMask) == kExponentMask && Significand() == 0; } + bool IsNanOrInf() const { return (u_ & kExponentMask) == kExponentMask; } + bool IsNormal() const { return (u_ & kExponentMask) != 0 || Significand() == 0; } + bool IsZero() const { return (u_ & (kExponentMask | kSignificandMask)) == 0; } + + uint64_t IntegerSignificand() const { return IsNormal() ? Significand() | kHiddenBit : Significand(); } + int IntegerExponent() const { return (IsNormal() ? Exponent() : kDenormalExponent) - kSignificandSize; } + uint64_t ToBias() const { return (u_ & kSignMask) ? ~u_ + 1 : u_ | kSignMask; } + + static int EffectiveSignificandSize(int order) { + if (order >= -1021) + return 53; + else if (order <= -1074) + return 0; + else + return order + 1074; + } + +private: + static const int kSignificandSize = 52; + static const int kExponentBias = 0x3FF; + static const int kDenormalExponent = 1 - kExponentBias; + static const uint64_t kSignMask = RAPIDJSON_UINT64_C2(0x80000000, 0x00000000); + static const uint64_t kExponentMask = RAPIDJSON_UINT64_C2(0x7FF00000, 0x00000000); + static const uint64_t kSignificandMask = RAPIDJSON_UINT64_C2(0x000FFFFF, 0xFFFFFFFF); + static const uint64_t kHiddenBit = RAPIDJSON_UINT64_C2(0x00100000, 0x00000000); + + union { + double d_; + uint64_t u_; + }; +}; + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_IEEE754_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/internal/itoa.h b/include/liblvgl/libs/thorvg/rapidjson/internal/itoa.h new file mode 100644 index 00000000..9fe8c932 --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/internal/itoa.h @@ -0,0 +1,308 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ITOA_ +#define RAPIDJSON_ITOA_ + +#include "../rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +inline const char* GetDigitsLut() { + static const char cDigitsLut[200] = { + '0','0','0','1','0','2','0','3','0','4','0','5','0','6','0','7','0','8','0','9', + '1','0','1','1','1','2','1','3','1','4','1','5','1','6','1','7','1','8','1','9', + '2','0','2','1','2','2','2','3','2','4','2','5','2','6','2','7','2','8','2','9', + '3','0','3','1','3','2','3','3','3','4','3','5','3','6','3','7','3','8','3','9', + '4','0','4','1','4','2','4','3','4','4','4','5','4','6','4','7','4','8','4','9', + '5','0','5','1','5','2','5','3','5','4','5','5','5','6','5','7','5','8','5','9', + '6','0','6','1','6','2','6','3','6','4','6','5','6','6','6','7','6','8','6','9', + '7','0','7','1','7','2','7','3','7','4','7','5','7','6','7','7','7','8','7','9', + '8','0','8','1','8','2','8','3','8','4','8','5','8','6','8','7','8','8','8','9', + '9','0','9','1','9','2','9','3','9','4','9','5','9','6','9','7','9','8','9','9' + }; + return cDigitsLut; +} + +inline char* u32toa(uint32_t value, char* buffer) { + RAPIDJSON_ASSERT(buffer != 0); + + const char* cDigitsLut = GetDigitsLut(); + + if (value < 10000) { + const uint32_t d1 = (value / 100) << 1; + const uint32_t d2 = (value % 100) << 1; + + if (value >= 1000) + *buffer++ = cDigitsLut[d1]; + if (value >= 100) + *buffer++ = cDigitsLut[d1 + 1]; + if (value >= 10) + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + } + else if (value < 100000000) { + // value = bbbbcccc + const uint32_t b = value / 10000; + const uint32_t c = value % 10000; + + const uint32_t d1 = (b / 100) << 1; + const uint32_t d2 = (b % 100) << 1; + + const uint32_t d3 = (c / 100) << 1; + const uint32_t d4 = (c % 100) << 1; + + if (value >= 10000000) + *buffer++ = cDigitsLut[d1]; + if (value >= 1000000) + *buffer++ = cDigitsLut[d1 + 1]; + if (value >= 100000) + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + + *buffer++ = cDigitsLut[d3]; + *buffer++ = cDigitsLut[d3 + 1]; + *buffer++ = cDigitsLut[d4]; + *buffer++ = cDigitsLut[d4 + 1]; + } + else { + // value = aabbbbcccc in decimal + + const uint32_t a = value / 100000000; // 1 to 42 + value %= 100000000; + + if (a >= 10) { + const unsigned i = a << 1; + *buffer++ = cDigitsLut[i]; + *buffer++ = cDigitsLut[i + 1]; + } + else + *buffer++ = static_cast('0' + static_cast(a)); + + const uint32_t b = value / 10000; // 0 to 9999 + const uint32_t c = value % 10000; // 0 to 9999 + + const uint32_t d1 = (b / 100) << 1; + const uint32_t d2 = (b % 100) << 1; + + const uint32_t d3 = (c / 100) << 1; + const uint32_t d4 = (c % 100) << 1; + + *buffer++ = cDigitsLut[d1]; + *buffer++ = cDigitsLut[d1 + 1]; + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + *buffer++ = cDigitsLut[d3]; + *buffer++ = cDigitsLut[d3 + 1]; + *buffer++ = cDigitsLut[d4]; + *buffer++ = cDigitsLut[d4 + 1]; + } + return buffer; +} + +inline char* i32toa(int32_t value, char* buffer) { + RAPIDJSON_ASSERT(buffer != 0); + uint32_t u = static_cast(value); + if (value < 0) { + *buffer++ = '-'; + u = ~u + 1; + } + + return u32toa(u, buffer); +} + +inline char* u64toa(uint64_t value, char* buffer) { + RAPIDJSON_ASSERT(buffer != 0); + const char* cDigitsLut = GetDigitsLut(); + const uint64_t kTen8 = 100000000; + const uint64_t kTen9 = kTen8 * 10; + const uint64_t kTen10 = kTen8 * 100; + const uint64_t kTen11 = kTen8 * 1000; + const uint64_t kTen12 = kTen8 * 10000; + const uint64_t kTen13 = kTen8 * 100000; + const uint64_t kTen14 = kTen8 * 1000000; + const uint64_t kTen15 = kTen8 * 10000000; + const uint64_t kTen16 = kTen8 * kTen8; + + if (value < kTen8) { + uint32_t v = static_cast(value); + if (v < 10000) { + const uint32_t d1 = (v / 100) << 1; + const uint32_t d2 = (v % 100) << 1; + + if (v >= 1000) + *buffer++ = cDigitsLut[d1]; + if (v >= 100) + *buffer++ = cDigitsLut[d1 + 1]; + if (v >= 10) + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + } + else { + // value = bbbbcccc + const uint32_t b = v / 10000; + const uint32_t c = v % 10000; + + const uint32_t d1 = (b / 100) << 1; + const uint32_t d2 = (b % 100) << 1; + + const uint32_t d3 = (c / 100) << 1; + const uint32_t d4 = (c % 100) << 1; + + if (value >= 10000000) + *buffer++ = cDigitsLut[d1]; + if (value >= 1000000) + *buffer++ = cDigitsLut[d1 + 1]; + if (value >= 100000) + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + + *buffer++ = cDigitsLut[d3]; + *buffer++ = cDigitsLut[d3 + 1]; + *buffer++ = cDigitsLut[d4]; + *buffer++ = cDigitsLut[d4 + 1]; + } + } + else if (value < kTen16) { + const uint32_t v0 = static_cast(value / kTen8); + const uint32_t v1 = static_cast(value % kTen8); + + const uint32_t b0 = v0 / 10000; + const uint32_t c0 = v0 % 10000; + + const uint32_t d1 = (b0 / 100) << 1; + const uint32_t d2 = (b0 % 100) << 1; + + const uint32_t d3 = (c0 / 100) << 1; + const uint32_t d4 = (c0 % 100) << 1; + + const uint32_t b1 = v1 / 10000; + const uint32_t c1 = v1 % 10000; + + const uint32_t d5 = (b1 / 100) << 1; + const uint32_t d6 = (b1 % 100) << 1; + + const uint32_t d7 = (c1 / 100) << 1; + const uint32_t d8 = (c1 % 100) << 1; + + if (value >= kTen15) + *buffer++ = cDigitsLut[d1]; + if (value >= kTen14) + *buffer++ = cDigitsLut[d1 + 1]; + if (value >= kTen13) + *buffer++ = cDigitsLut[d2]; + if (value >= kTen12) + *buffer++ = cDigitsLut[d2 + 1]; + if (value >= kTen11) + *buffer++ = cDigitsLut[d3]; + if (value >= kTen10) + *buffer++ = cDigitsLut[d3 + 1]; + if (value >= kTen9) + *buffer++ = cDigitsLut[d4]; + + *buffer++ = cDigitsLut[d4 + 1]; + *buffer++ = cDigitsLut[d5]; + *buffer++ = cDigitsLut[d5 + 1]; + *buffer++ = cDigitsLut[d6]; + *buffer++ = cDigitsLut[d6 + 1]; + *buffer++ = cDigitsLut[d7]; + *buffer++ = cDigitsLut[d7 + 1]; + *buffer++ = cDigitsLut[d8]; + *buffer++ = cDigitsLut[d8 + 1]; + } + else { + const uint32_t a = static_cast(value / kTen16); // 1 to 1844 + value %= kTen16; + + if (a < 10) + *buffer++ = static_cast('0' + static_cast(a)); + else if (a < 100) { + const uint32_t i = a << 1; + *buffer++ = cDigitsLut[i]; + *buffer++ = cDigitsLut[i + 1]; + } + else if (a < 1000) { + *buffer++ = static_cast('0' + static_cast(a / 100)); + + const uint32_t i = (a % 100) << 1; + *buffer++ = cDigitsLut[i]; + *buffer++ = cDigitsLut[i + 1]; + } + else { + const uint32_t i = (a / 100) << 1; + const uint32_t j = (a % 100) << 1; + *buffer++ = cDigitsLut[i]; + *buffer++ = cDigitsLut[i + 1]; + *buffer++ = cDigitsLut[j]; + *buffer++ = cDigitsLut[j + 1]; + } + + const uint32_t v0 = static_cast(value / kTen8); + const uint32_t v1 = static_cast(value % kTen8); + + const uint32_t b0 = v0 / 10000; + const uint32_t c0 = v0 % 10000; + + const uint32_t d1 = (b0 / 100) << 1; + const uint32_t d2 = (b0 % 100) << 1; + + const uint32_t d3 = (c0 / 100) << 1; + const uint32_t d4 = (c0 % 100) << 1; + + const uint32_t b1 = v1 / 10000; + const uint32_t c1 = v1 % 10000; + + const uint32_t d5 = (b1 / 100) << 1; + const uint32_t d6 = (b1 % 100) << 1; + + const uint32_t d7 = (c1 / 100) << 1; + const uint32_t d8 = (c1 % 100) << 1; + + *buffer++ = cDigitsLut[d1]; + *buffer++ = cDigitsLut[d1 + 1]; + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + *buffer++ = cDigitsLut[d3]; + *buffer++ = cDigitsLut[d3 + 1]; + *buffer++ = cDigitsLut[d4]; + *buffer++ = cDigitsLut[d4 + 1]; + *buffer++ = cDigitsLut[d5]; + *buffer++ = cDigitsLut[d5 + 1]; + *buffer++ = cDigitsLut[d6]; + *buffer++ = cDigitsLut[d6 + 1]; + *buffer++ = cDigitsLut[d7]; + *buffer++ = cDigitsLut[d7 + 1]; + *buffer++ = cDigitsLut[d8]; + *buffer++ = cDigitsLut[d8 + 1]; + } + + return buffer; +} + +inline char* i64toa(int64_t value, char* buffer) { + RAPIDJSON_ASSERT(buffer != 0); + uint64_t u = static_cast(value); + if (value < 0) { + *buffer++ = '-'; + u = ~u + 1; + } + + return u64toa(u, buffer); +} + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_ITOA_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/internal/meta.h b/include/liblvgl/libs/thorvg/rapidjson/internal/meta.h new file mode 100644 index 00000000..76a1d5be --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/internal/meta.h @@ -0,0 +1,186 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_INTERNAL_META_H_ +#define RAPIDJSON_INTERNAL_META_H_ + +#include "../rapidjson.h" + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +#if defined(_MSC_VER) && !defined(__clang__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(6334) +#endif + +#if RAPIDJSON_HAS_CXX11_TYPETRAITS +#include +#endif + +//@cond RAPIDJSON_INTERNAL +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +// Helper to wrap/convert arbitrary types to void, useful for arbitrary type matching +template struct Void { typedef void Type; }; + +/////////////////////////////////////////////////////////////////////////////// +// BoolType, TrueType, FalseType +// +template struct BoolType { + static const bool Value = Cond; + typedef BoolType Type; +}; +typedef BoolType TrueType; +typedef BoolType FalseType; + + +/////////////////////////////////////////////////////////////////////////////// +// SelectIf, BoolExpr, NotExpr, AndExpr, OrExpr +// + +template struct SelectIfImpl { template struct Apply { typedef T1 Type; }; }; +template <> struct SelectIfImpl { template struct Apply { typedef T2 Type; }; }; +template struct SelectIfCond : SelectIfImpl::template Apply {}; +template struct SelectIf : SelectIfCond {}; + +template struct AndExprCond : FalseType {}; +template <> struct AndExprCond : TrueType {}; +template struct OrExprCond : TrueType {}; +template <> struct OrExprCond : FalseType {}; + +template struct BoolExpr : SelectIf::Type {}; +template struct NotExpr : SelectIf::Type {}; +template struct AndExpr : AndExprCond::Type {}; +template struct OrExpr : OrExprCond::Type {}; + + +/////////////////////////////////////////////////////////////////////////////// +// AddConst, MaybeAddConst, RemoveConst +template struct AddConst { typedef const T Type; }; +template struct MaybeAddConst : SelectIfCond {}; +template struct RemoveConst { typedef T Type; }; +template struct RemoveConst { typedef T Type; }; + + +/////////////////////////////////////////////////////////////////////////////// +// IsSame, IsConst, IsMoreConst, IsPointer +// +template struct IsSame : FalseType {}; +template struct IsSame : TrueType {}; + +template struct IsConst : FalseType {}; +template struct IsConst : TrueType {}; + +template +struct IsMoreConst + : AndExpr::Type, typename RemoveConst::Type>, + BoolType::Value >= IsConst::Value> >::Type {}; + +template struct IsPointer : FalseType {}; +template struct IsPointer : TrueType {}; + +/////////////////////////////////////////////////////////////////////////////// +// IsBaseOf +// +#if RAPIDJSON_HAS_CXX11_TYPETRAITS + +template struct IsBaseOf + : BoolType< ::std::is_base_of::value> {}; + +#else // simplified version adopted from Boost + +template struct IsBaseOfImpl { + RAPIDJSON_STATIC_ASSERT(sizeof(B) != 0); + RAPIDJSON_STATIC_ASSERT(sizeof(D) != 0); + + typedef char (&Yes)[1]; + typedef char (&No) [2]; + + template + static Yes Check(const D*, T); + static No Check(const B*, int); + + struct Host { + operator const B*() const; + operator const D*(); + }; + + enum { Value = (sizeof(Check(Host(), 0)) == sizeof(Yes)) }; +}; + +template struct IsBaseOf + : OrExpr, BoolExpr > >::Type {}; + +#endif // RAPIDJSON_HAS_CXX11_TYPETRAITS + + +////////////////////////////////////////////////////////////////////////// +// EnableIf / DisableIf +// +template struct EnableIfCond { typedef T Type; }; +template struct EnableIfCond { /* empty */ }; + +template struct DisableIfCond { typedef T Type; }; +template struct DisableIfCond { /* empty */ }; + +template +struct EnableIf : EnableIfCond {}; + +template +struct DisableIf : DisableIfCond {}; + +// SFINAE helpers +struct SfinaeTag {}; +template struct RemoveSfinaeTag; +template struct RemoveSfinaeTag { typedef T Type; }; + +#define RAPIDJSON_REMOVEFPTR_(type) \ + typename ::RAPIDJSON_NAMESPACE::internal::RemoveSfinaeTag \ + < ::RAPIDJSON_NAMESPACE::internal::SfinaeTag&(*) type>::Type + +#define RAPIDJSON_ENABLEIF(cond) \ + typename ::RAPIDJSON_NAMESPACE::internal::EnableIf \ + ::Type * = NULL + +#define RAPIDJSON_DISABLEIF(cond) \ + typename ::RAPIDJSON_NAMESPACE::internal::DisableIf \ + ::Type * = NULL + +#define RAPIDJSON_ENABLEIF_RETURN(cond,returntype) \ + typename ::RAPIDJSON_NAMESPACE::internal::EnableIf \ + ::Type + +#define RAPIDJSON_DISABLEIF_RETURN(cond,returntype) \ + typename ::RAPIDJSON_NAMESPACE::internal::DisableIf \ + ::Type + +} // namespace internal +RAPIDJSON_NAMESPACE_END +//@endcond + +#if defined(_MSC_VER) && !defined(__clang__) +RAPIDJSON_DIAG_POP +#endif + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_INTERNAL_META_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/internal/pow10.h b/include/liblvgl/libs/thorvg/rapidjson/internal/pow10.h new file mode 100644 index 00000000..04ee1f6b --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/internal/pow10.h @@ -0,0 +1,55 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_POW10_ +#define RAPIDJSON_POW10_ + +#include "../rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +//! Computes integer powers of 10 in double (10.0^n). +/*! This function uses lookup table for fast and accurate results. + \param n non-negative exponent. Must <= 308. + \return 10.0^n +*/ +inline double Pow10(int n) { + static const double e[] = { // 1e-0...1e308: 309 * 8 bytes = 2472 bytes + 1e+0, + 1e+1, 1e+2, 1e+3, 1e+4, 1e+5, 1e+6, 1e+7, 1e+8, 1e+9, 1e+10, 1e+11, 1e+12, 1e+13, 1e+14, 1e+15, 1e+16, 1e+17, 1e+18, 1e+19, 1e+20, + 1e+21, 1e+22, 1e+23, 1e+24, 1e+25, 1e+26, 1e+27, 1e+28, 1e+29, 1e+30, 1e+31, 1e+32, 1e+33, 1e+34, 1e+35, 1e+36, 1e+37, 1e+38, 1e+39, 1e+40, + 1e+41, 1e+42, 1e+43, 1e+44, 1e+45, 1e+46, 1e+47, 1e+48, 1e+49, 1e+50, 1e+51, 1e+52, 1e+53, 1e+54, 1e+55, 1e+56, 1e+57, 1e+58, 1e+59, 1e+60, + 1e+61, 1e+62, 1e+63, 1e+64, 1e+65, 1e+66, 1e+67, 1e+68, 1e+69, 1e+70, 1e+71, 1e+72, 1e+73, 1e+74, 1e+75, 1e+76, 1e+77, 1e+78, 1e+79, 1e+80, + 1e+81, 1e+82, 1e+83, 1e+84, 1e+85, 1e+86, 1e+87, 1e+88, 1e+89, 1e+90, 1e+91, 1e+92, 1e+93, 1e+94, 1e+95, 1e+96, 1e+97, 1e+98, 1e+99, 1e+100, + 1e+101,1e+102,1e+103,1e+104,1e+105,1e+106,1e+107,1e+108,1e+109,1e+110,1e+111,1e+112,1e+113,1e+114,1e+115,1e+116,1e+117,1e+118,1e+119,1e+120, + 1e+121,1e+122,1e+123,1e+124,1e+125,1e+126,1e+127,1e+128,1e+129,1e+130,1e+131,1e+132,1e+133,1e+134,1e+135,1e+136,1e+137,1e+138,1e+139,1e+140, + 1e+141,1e+142,1e+143,1e+144,1e+145,1e+146,1e+147,1e+148,1e+149,1e+150,1e+151,1e+152,1e+153,1e+154,1e+155,1e+156,1e+157,1e+158,1e+159,1e+160, + 1e+161,1e+162,1e+163,1e+164,1e+165,1e+166,1e+167,1e+168,1e+169,1e+170,1e+171,1e+172,1e+173,1e+174,1e+175,1e+176,1e+177,1e+178,1e+179,1e+180, + 1e+181,1e+182,1e+183,1e+184,1e+185,1e+186,1e+187,1e+188,1e+189,1e+190,1e+191,1e+192,1e+193,1e+194,1e+195,1e+196,1e+197,1e+198,1e+199,1e+200, + 1e+201,1e+202,1e+203,1e+204,1e+205,1e+206,1e+207,1e+208,1e+209,1e+210,1e+211,1e+212,1e+213,1e+214,1e+215,1e+216,1e+217,1e+218,1e+219,1e+220, + 1e+221,1e+222,1e+223,1e+224,1e+225,1e+226,1e+227,1e+228,1e+229,1e+230,1e+231,1e+232,1e+233,1e+234,1e+235,1e+236,1e+237,1e+238,1e+239,1e+240, + 1e+241,1e+242,1e+243,1e+244,1e+245,1e+246,1e+247,1e+248,1e+249,1e+250,1e+251,1e+252,1e+253,1e+254,1e+255,1e+256,1e+257,1e+258,1e+259,1e+260, + 1e+261,1e+262,1e+263,1e+264,1e+265,1e+266,1e+267,1e+268,1e+269,1e+270,1e+271,1e+272,1e+273,1e+274,1e+275,1e+276,1e+277,1e+278,1e+279,1e+280, + 1e+281,1e+282,1e+283,1e+284,1e+285,1e+286,1e+287,1e+288,1e+289,1e+290,1e+291,1e+292,1e+293,1e+294,1e+295,1e+296,1e+297,1e+298,1e+299,1e+300, + 1e+301,1e+302,1e+303,1e+304,1e+305,1e+306,1e+307,1e+308 + }; + RAPIDJSON_ASSERT(n >= 0 && n <= 308); + return e[n]; +} + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_POW10_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/internal/regex.h b/include/liblvgl/libs/thorvg/rapidjson/internal/regex.h new file mode 100644 index 00000000..e3712610 --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/internal/regex.h @@ -0,0 +1,739 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_INTERNAL_REGEX_H_ +#define RAPIDJSON_INTERNAL_REGEX_H_ + +#include "../allocators.h" +#include "../stream.h" +#include "stack.h" + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +RAPIDJSON_DIAG_OFF(switch-enum) +#elif defined(_MSC_VER) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated +#endif + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +#ifndef RAPIDJSON_REGEX_VERBOSE +#define RAPIDJSON_REGEX_VERBOSE 0 +#endif + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +/////////////////////////////////////////////////////////////////////////////// +// DecodedStream + +template +class DecodedStream { +public: + DecodedStream(SourceStream& ss) : ss_(ss), codepoint_() { Decode(); } + unsigned Peek() { return codepoint_; } + unsigned Take() { + unsigned c = codepoint_; + if (c) // No further decoding when '\0' + Decode(); + return c; + } + +private: + void Decode() { + if (!Encoding::Decode(ss_, &codepoint_)) + codepoint_ = 0; + } + + SourceStream& ss_; + unsigned codepoint_; +}; + +/////////////////////////////////////////////////////////////////////////////// +// GenericRegex + +static const SizeType kRegexInvalidState = ~SizeType(0); //!< Represents an invalid index in GenericRegex::State::out, out1 +static const SizeType kRegexInvalidRange = ~SizeType(0); + +template +class GenericRegexSearch; + +//! Regular expression engine with subset of ECMAscript grammar. +/*! + Supported regular expression syntax: + - \c ab Concatenation + - \c a|b Alternation + - \c a? Zero or one + - \c a* Zero or more + - \c a+ One or more + - \c a{3} Exactly 3 times + - \c a{3,} At least 3 times + - \c a{3,5} 3 to 5 times + - \c (ab) Grouping + - \c ^a At the beginning + - \c a$ At the end + - \c . Any character + - \c [abc] Character classes + - \c [a-c] Character class range + - \c [a-z0-9_] Character class combination + - \c [^abc] Negated character classes + - \c [^a-c] Negated character class range + - \c [\b] Backspace (U+0008) + - \c \\| \\\\ ... Escape characters + - \c \\f Form feed (U+000C) + - \c \\n Line feed (U+000A) + - \c \\r Carriage return (U+000D) + - \c \\t Tab (U+0009) + - \c \\v Vertical tab (U+000B) + + \note This is a Thompson NFA engine, implemented with reference to + Cox, Russ. "Regular Expression Matching Can Be Simple And Fast (but is slow in Java, Perl, PHP, Python, Ruby,...).", + https://swtch.com/~rsc/regexp/regexp1.html +*/ +template +class GenericRegex { +public: + typedef Encoding EncodingType; + typedef typename Encoding::Ch Ch; + template friend class GenericRegexSearch; + + GenericRegex(const Ch* source, Allocator* allocator = 0) : + ownAllocator_(allocator ? 0 : RAPIDJSON_NEW(Allocator)()), allocator_(allocator ? allocator : ownAllocator_), + states_(allocator_, 256), ranges_(allocator_, 256), root_(kRegexInvalidState), stateCount_(), rangeCount_(), + anchorBegin_(), anchorEnd_() + { + GenericStringStream ss(source); + DecodedStream, Encoding> ds(ss); + Parse(ds); + } + + ~GenericRegex() + { + RAPIDJSON_DELETE(ownAllocator_); + } + + bool IsValid() const { + return root_ != kRegexInvalidState; + } + +private: + enum Operator { + kZeroOrOne, + kZeroOrMore, + kOneOrMore, + kConcatenation, + kAlternation, + kLeftParenthesis + }; + + static const unsigned kAnyCharacterClass = 0xFFFFFFFF; //!< For '.' + static const unsigned kRangeCharacterClass = 0xFFFFFFFE; + static const unsigned kRangeNegationFlag = 0x80000000; + + struct Range { + unsigned start; // + unsigned end; + SizeType next; + }; + + struct State { + SizeType out; //!< Equals to kInvalid for matching state + SizeType out1; //!< Equals to non-kInvalid for split + SizeType rangeStart; + unsigned codepoint; + }; + + struct Frag { + Frag(SizeType s, SizeType o, SizeType m) : start(s), out(o), minIndex(m) {} + SizeType start; + SizeType out; //!< link-list of all output states + SizeType minIndex; + }; + + State& GetState(SizeType index) { + RAPIDJSON_ASSERT(index < stateCount_); + return states_.template Bottom()[index]; + } + + const State& GetState(SizeType index) const { + RAPIDJSON_ASSERT(index < stateCount_); + return states_.template Bottom()[index]; + } + + Range& GetRange(SizeType index) { + RAPIDJSON_ASSERT(index < rangeCount_); + return ranges_.template Bottom()[index]; + } + + const Range& GetRange(SizeType index) const { + RAPIDJSON_ASSERT(index < rangeCount_); + return ranges_.template Bottom()[index]; + } + + template + void Parse(DecodedStream& ds) { + Stack operandStack(allocator_, 256); // Frag + Stack operatorStack(allocator_, 256); // Operator + Stack atomCountStack(allocator_, 256); // unsigned (Atom per parenthesis) + + *atomCountStack.template Push() = 0; + + unsigned codepoint; + while (ds.Peek() != 0) { + switch (codepoint = ds.Take()) { + case '^': + anchorBegin_ = true; + break; + + case '$': + anchorEnd_ = true; + break; + + case '|': + while (!operatorStack.Empty() && *operatorStack.template Top() < kAlternation) + if (!Eval(operandStack, *operatorStack.template Pop(1))) + return; + *operatorStack.template Push() = kAlternation; + *atomCountStack.template Top() = 0; + break; + + case '(': + *operatorStack.template Push() = kLeftParenthesis; + *atomCountStack.template Push() = 0; + break; + + case ')': + while (!operatorStack.Empty() && *operatorStack.template Top() != kLeftParenthesis) + if (!Eval(operandStack, *operatorStack.template Pop(1))) + return; + if (operatorStack.Empty()) + return; + operatorStack.template Pop(1); + atomCountStack.template Pop(1); + ImplicitConcatenation(atomCountStack, operatorStack); + break; + + case '?': + if (!Eval(operandStack, kZeroOrOne)) + return; + break; + + case '*': + if (!Eval(operandStack, kZeroOrMore)) + return; + break; + + case '+': + if (!Eval(operandStack, kOneOrMore)) + return; + break; + + case '{': + { + unsigned n, m; + if (!ParseUnsigned(ds, &n)) + return; + + if (ds.Peek() == ',') { + ds.Take(); + if (ds.Peek() == '}') + m = kInfinityQuantifier; + else if (!ParseUnsigned(ds, &m) || m < n) + return; + } + else + m = n; + + if (!EvalQuantifier(operandStack, n, m) || ds.Peek() != '}') + return; + ds.Take(); + } + break; + + case '.': + PushOperand(operandStack, kAnyCharacterClass); + ImplicitConcatenation(atomCountStack, operatorStack); + break; + + case '[': + { + SizeType range; + if (!ParseRange(ds, &range)) + return; + SizeType s = NewState(kRegexInvalidState, kRegexInvalidState, kRangeCharacterClass); + GetState(s).rangeStart = range; + *operandStack.template Push() = Frag(s, s, s); + } + ImplicitConcatenation(atomCountStack, operatorStack); + break; + + case '\\': // Escape character + if (!CharacterEscape(ds, &codepoint)) + return; // Unsupported escape character + // fall through to default + RAPIDJSON_DELIBERATE_FALLTHROUGH; + + default: // Pattern character + PushOperand(operandStack, codepoint); + ImplicitConcatenation(atomCountStack, operatorStack); + } + } + + while (!operatorStack.Empty()) + if (!Eval(operandStack, *operatorStack.template Pop(1))) + return; + + // Link the operand to matching state. + if (operandStack.GetSize() == sizeof(Frag)) { + Frag* e = operandStack.template Pop(1); + Patch(e->out, NewState(kRegexInvalidState, kRegexInvalidState, 0)); + root_ = e->start; + +#if RAPIDJSON_REGEX_VERBOSE + printf("root: %d\n", root_); + for (SizeType i = 0; i < stateCount_ ; i++) { + State& s = GetState(i); + printf("[%2d] out: %2d out1: %2d c: '%c'\n", i, s.out, s.out1, (char)s.codepoint); + } + printf("\n"); +#endif + } + } + + SizeType NewState(SizeType out, SizeType out1, unsigned codepoint) { + State* s = states_.template Push(); + s->out = out; + s->out1 = out1; + s->codepoint = codepoint; + s->rangeStart = kRegexInvalidRange; + return stateCount_++; + } + + void PushOperand(Stack& operandStack, unsigned codepoint) { + SizeType s = NewState(kRegexInvalidState, kRegexInvalidState, codepoint); + *operandStack.template Push() = Frag(s, s, s); + } + + void ImplicitConcatenation(Stack& atomCountStack, Stack& operatorStack) { + if (*atomCountStack.template Top()) + *operatorStack.template Push() = kConcatenation; + (*atomCountStack.template Top())++; + } + + SizeType Append(SizeType l1, SizeType l2) { + SizeType old = l1; + while (GetState(l1).out != kRegexInvalidState) + l1 = GetState(l1).out; + GetState(l1).out = l2; + return old; + } + + void Patch(SizeType l, SizeType s) { + for (SizeType next; l != kRegexInvalidState; l = next) { + next = GetState(l).out; + GetState(l).out = s; + } + } + + bool Eval(Stack& operandStack, Operator op) { + switch (op) { + case kConcatenation: + RAPIDJSON_ASSERT(operandStack.GetSize() >= sizeof(Frag) * 2); + { + Frag e2 = *operandStack.template Pop(1); + Frag e1 = *operandStack.template Pop(1); + Patch(e1.out, e2.start); + *operandStack.template Push() = Frag(e1.start, e2.out, Min(e1.minIndex, e2.minIndex)); + } + return true; + + case kAlternation: + if (operandStack.GetSize() >= sizeof(Frag) * 2) { + Frag e2 = *operandStack.template Pop(1); + Frag e1 = *operandStack.template Pop(1); + SizeType s = NewState(e1.start, e2.start, 0); + *operandStack.template Push() = Frag(s, Append(e1.out, e2.out), Min(e1.minIndex, e2.minIndex)); + return true; + } + return false; + + case kZeroOrOne: + if (operandStack.GetSize() >= sizeof(Frag)) { + Frag e = *operandStack.template Pop(1); + SizeType s = NewState(kRegexInvalidState, e.start, 0); + *operandStack.template Push() = Frag(s, Append(e.out, s), e.minIndex); + return true; + } + return false; + + case kZeroOrMore: + if (operandStack.GetSize() >= sizeof(Frag)) { + Frag e = *operandStack.template Pop(1); + SizeType s = NewState(kRegexInvalidState, e.start, 0); + Patch(e.out, s); + *operandStack.template Push() = Frag(s, s, e.minIndex); + return true; + } + return false; + + case kOneOrMore: + if (operandStack.GetSize() >= sizeof(Frag)) { + Frag e = *operandStack.template Pop(1); + SizeType s = NewState(kRegexInvalidState, e.start, 0); + Patch(e.out, s); + *operandStack.template Push() = Frag(e.start, s, e.minIndex); + return true; + } + return false; + + default: + // syntax error (e.g. unclosed kLeftParenthesis) + return false; + } + } + + bool EvalQuantifier(Stack& operandStack, unsigned n, unsigned m) { + RAPIDJSON_ASSERT(n <= m); + RAPIDJSON_ASSERT(operandStack.GetSize() >= sizeof(Frag)); + + if (n == 0) { + if (m == 0) // a{0} not support + return false; + else if (m == kInfinityQuantifier) + Eval(operandStack, kZeroOrMore); // a{0,} -> a* + else { + Eval(operandStack, kZeroOrOne); // a{0,5} -> a? + for (unsigned i = 0; i < m - 1; i++) + CloneTopOperand(operandStack); // a{0,5} -> a? a? a? a? a? + for (unsigned i = 0; i < m - 1; i++) + Eval(operandStack, kConcatenation); // a{0,5} -> a?a?a?a?a? + } + return true; + } + + for (unsigned i = 0; i < n - 1; i++) // a{3} -> a a a + CloneTopOperand(operandStack); + + if (m == kInfinityQuantifier) + Eval(operandStack, kOneOrMore); // a{3,} -> a a a+ + else if (m > n) { + CloneTopOperand(operandStack); // a{3,5} -> a a a a + Eval(operandStack, kZeroOrOne); // a{3,5} -> a a a a? + for (unsigned i = n; i < m - 1; i++) + CloneTopOperand(operandStack); // a{3,5} -> a a a a? a? + for (unsigned i = n; i < m; i++) + Eval(operandStack, kConcatenation); // a{3,5} -> a a aa?a? + } + + for (unsigned i = 0; i < n - 1; i++) + Eval(operandStack, kConcatenation); // a{3} -> aaa, a{3,} -> aaa+, a{3.5} -> aaaa?a? + + return true; + } + + static SizeType Min(SizeType a, SizeType b) { return a < b ? a : b; } + + void CloneTopOperand(Stack& operandStack) { + const Frag src = *operandStack.template Top(); // Copy constructor to prevent invalidation + SizeType count = stateCount_ - src.minIndex; // Assumes top operand contains states in [src->minIndex, stateCount_) + State* s = states_.template Push(count); + memcpy(s, &GetState(src.minIndex), count * sizeof(State)); + for (SizeType j = 0; j < count; j++) { + if (s[j].out != kRegexInvalidState) + s[j].out += count; + if (s[j].out1 != kRegexInvalidState) + s[j].out1 += count; + } + *operandStack.template Push() = Frag(src.start + count, src.out + count, src.minIndex + count); + stateCount_ += count; + } + + template + bool ParseUnsigned(DecodedStream& ds, unsigned* u) { + unsigned r = 0; + if (ds.Peek() < '0' || ds.Peek() > '9') + return false; + while (ds.Peek() >= '0' && ds.Peek() <= '9') { + if (r >= 429496729 && ds.Peek() > '5') // 2^32 - 1 = 4294967295 + return false; // overflow + r = r * 10 + (ds.Take() - '0'); + } + *u = r; + return true; + } + + template + bool ParseRange(DecodedStream& ds, SizeType* range) { + bool isBegin = true; + bool negate = false; + int step = 0; + SizeType start = kRegexInvalidRange; + SizeType current = kRegexInvalidRange; + unsigned codepoint; + while ((codepoint = ds.Take()) != 0) { + if (isBegin) { + isBegin = false; + if (codepoint == '^') { + negate = true; + continue; + } + } + + switch (codepoint) { + case ']': + if (start == kRegexInvalidRange) + return false; // Error: nothing inside [] + if (step == 2) { // Add trailing '-' + SizeType r = NewRange('-'); + RAPIDJSON_ASSERT(current != kRegexInvalidRange); + GetRange(current).next = r; + } + if (negate) + GetRange(start).start |= kRangeNegationFlag; + *range = start; + return true; + + case '\\': + if (ds.Peek() == 'b') { + ds.Take(); + codepoint = 0x0008; // Escape backspace character + } + else if (!CharacterEscape(ds, &codepoint)) + return false; + // fall through to default + RAPIDJSON_DELIBERATE_FALLTHROUGH; + + default: + switch (step) { + case 1: + if (codepoint == '-') { + step++; + break; + } + // fall through to step 0 for other characters + RAPIDJSON_DELIBERATE_FALLTHROUGH; + + case 0: + { + SizeType r = NewRange(codepoint); + if (current != kRegexInvalidRange) + GetRange(current).next = r; + if (start == kRegexInvalidRange) + start = r; + current = r; + } + step = 1; + break; + + default: + RAPIDJSON_ASSERT(step == 2); + GetRange(current).end = codepoint; + step = 0; + } + } + } + return false; + } + + SizeType NewRange(unsigned codepoint) { + Range* r = ranges_.template Push(); + r->start = r->end = codepoint; + r->next = kRegexInvalidRange; + return rangeCount_++; + } + + template + bool CharacterEscape(DecodedStream& ds, unsigned* escapedCodepoint) { + unsigned codepoint; + switch (codepoint = ds.Take()) { + case '^': + case '$': + case '|': + case '(': + case ')': + case '?': + case '*': + case '+': + case '.': + case '[': + case ']': + case '{': + case '}': + case '\\': + *escapedCodepoint = codepoint; return true; + case 'f': *escapedCodepoint = 0x000C; return true; + case 'n': *escapedCodepoint = 0x000A; return true; + case 'r': *escapedCodepoint = 0x000D; return true; + case 't': *escapedCodepoint = 0x0009; return true; + case 'v': *escapedCodepoint = 0x000B; return true; + default: + return false; // Unsupported escape character + } + } + + Allocator* ownAllocator_; + Allocator* allocator_; + Stack states_; + Stack ranges_; + SizeType root_; + SizeType stateCount_; + SizeType rangeCount_; + + static const unsigned kInfinityQuantifier = ~0u; + + // For SearchWithAnchoring() + bool anchorBegin_; + bool anchorEnd_; +}; + +template +class GenericRegexSearch { +public: + typedef typename RegexType::EncodingType Encoding; + typedef typename Encoding::Ch Ch; + + GenericRegexSearch(const RegexType& regex, Allocator* allocator = 0) : + regex_(regex), allocator_(allocator), ownAllocator_(0), + state0_(allocator, 0), state1_(allocator, 0), stateSet_() + { + RAPIDJSON_ASSERT(regex_.IsValid()); + if (!allocator_) + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)(); + stateSet_ = static_cast(allocator_->Malloc(GetStateSetSize())); + state0_.template Reserve(regex_.stateCount_); + state1_.template Reserve(regex_.stateCount_); + } + + ~GenericRegexSearch() { + Allocator::Free(stateSet_); + RAPIDJSON_DELETE(ownAllocator_); + } + + template + bool Match(InputStream& is) { + return SearchWithAnchoring(is, true, true); + } + + bool Match(const Ch* s) { + GenericStringStream is(s); + return Match(is); + } + + template + bool Search(InputStream& is) { + return SearchWithAnchoring(is, regex_.anchorBegin_, regex_.anchorEnd_); + } + + bool Search(const Ch* s) { + GenericStringStream is(s); + return Search(is); + } + +private: + typedef typename RegexType::State State; + typedef typename RegexType::Range Range; + + template + bool SearchWithAnchoring(InputStream& is, bool anchorBegin, bool anchorEnd) { + DecodedStream ds(is); + + state0_.Clear(); + Stack *current = &state0_, *next = &state1_; + const size_t stateSetSize = GetStateSetSize(); + std::memset(stateSet_, 0, stateSetSize); + + bool matched = AddState(*current, regex_.root_); + unsigned codepoint; + while (!current->Empty() && (codepoint = ds.Take()) != 0) { + std::memset(stateSet_, 0, stateSetSize); + next->Clear(); + matched = false; + for (const SizeType* s = current->template Bottom(); s != current->template End(); ++s) { + const State& sr = regex_.GetState(*s); + if (sr.codepoint == codepoint || + sr.codepoint == RegexType::kAnyCharacterClass || + (sr.codepoint == RegexType::kRangeCharacterClass && MatchRange(sr.rangeStart, codepoint))) + { + matched = AddState(*next, sr.out) || matched; + if (!anchorEnd && matched) + return true; + } + if (!anchorBegin) + AddState(*next, regex_.root_); + } + internal::Swap(current, next); + } + + return matched; + } + + size_t GetStateSetSize() const { + return (regex_.stateCount_ + 31) / 32 * 4; + } + + // Return whether the added states is a match state + bool AddState(Stack& l, SizeType index) { + RAPIDJSON_ASSERT(index != kRegexInvalidState); + + const State& s = regex_.GetState(index); + if (s.out1 != kRegexInvalidState) { // Split + bool matched = AddState(l, s.out); + return AddState(l, s.out1) || matched; + } + else if (!(stateSet_[index >> 5] & (1u << (index & 31)))) { + stateSet_[index >> 5] |= (1u << (index & 31)); + *l.template PushUnsafe() = index; + } + return s.out == kRegexInvalidState; // by using PushUnsafe() above, we can ensure s is not validated due to reallocation. + } + + bool MatchRange(SizeType rangeIndex, unsigned codepoint) const { + bool yes = (regex_.GetRange(rangeIndex).start & RegexType::kRangeNegationFlag) == 0; + while (rangeIndex != kRegexInvalidRange) { + const Range& r = regex_.GetRange(rangeIndex); + if (codepoint >= (r.start & ~RegexType::kRangeNegationFlag) && codepoint <= r.end) + return yes; + rangeIndex = r.next; + } + return !yes; + } + + const RegexType& regex_; + Allocator* allocator_; + Allocator* ownAllocator_; + Stack state0_; + Stack state1_; + uint32_t* stateSet_; +}; + +typedef GenericRegex > Regex; +typedef GenericRegexSearch RegexSearch; + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +#if defined(__clang__) || defined(_MSC_VER) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_INTERNAL_REGEX_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/internal/stack.h b/include/liblvgl/libs/thorvg/rapidjson/internal/stack.h new file mode 100644 index 00000000..ceead449 --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/internal/stack.h @@ -0,0 +1,232 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_INTERNAL_STACK_H_ +#define RAPIDJSON_INTERNAL_STACK_H_ + +#include "../allocators.h" +#include "swap.h" +#include + +#if defined(__clang__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(c++98-compat) +#endif + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +/////////////////////////////////////////////////////////////////////////////// +// Stack + +//! A type-unsafe stack for storing different types of data. +/*! \tparam Allocator Allocator for allocating stack memory. +*/ +template +class Stack { +public: + // Optimization note: Do not allocate memory for stack_ in constructor. + // Do it lazily when first Push() -> Expand() -> Resize(). + Stack(Allocator* allocator, size_t stackCapacity) : allocator_(allocator), ownAllocator_(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) { + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + Stack(Stack&& rhs) + : allocator_(rhs.allocator_), + ownAllocator_(rhs.ownAllocator_), + stack_(rhs.stack_), + stackTop_(rhs.stackTop_), + stackEnd_(rhs.stackEnd_), + initialCapacity_(rhs.initialCapacity_) + { + rhs.allocator_ = 0; + rhs.ownAllocator_ = 0; + rhs.stack_ = 0; + rhs.stackTop_ = 0; + rhs.stackEnd_ = 0; + rhs.initialCapacity_ = 0; + } +#endif + + ~Stack() { + Destroy(); + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + Stack& operator=(Stack&& rhs) { + if (&rhs != this) + { + Destroy(); + + allocator_ = rhs.allocator_; + ownAllocator_ = rhs.ownAllocator_; + stack_ = rhs.stack_; + stackTop_ = rhs.stackTop_; + stackEnd_ = rhs.stackEnd_; + initialCapacity_ = rhs.initialCapacity_; + + rhs.allocator_ = 0; + rhs.ownAllocator_ = 0; + rhs.stack_ = 0; + rhs.stackTop_ = 0; + rhs.stackEnd_ = 0; + rhs.initialCapacity_ = 0; + } + return *this; + } +#endif + + void Swap(Stack& rhs) RAPIDJSON_NOEXCEPT { + internal::Swap(allocator_, rhs.allocator_); + internal::Swap(ownAllocator_, rhs.ownAllocator_); + internal::Swap(stack_, rhs.stack_); + internal::Swap(stackTop_, rhs.stackTop_); + internal::Swap(stackEnd_, rhs.stackEnd_); + internal::Swap(initialCapacity_, rhs.initialCapacity_); + } + + void Clear() { stackTop_ = stack_; } + + void ShrinkToFit() { + if (Empty()) { + // If the stack is empty, completely deallocate the memory. + Allocator::Free(stack_); // NOLINT (+clang-analyzer-unix.Malloc) + stack_ = 0; + stackTop_ = 0; + stackEnd_ = 0; + } + else + Resize(GetSize()); + } + + // Optimization note: try to minimize the size of this function for force inline. + // Expansion is run very infrequently, so it is moved to another (probably non-inline) function. + template + RAPIDJSON_FORCEINLINE void Reserve(size_t count = 1) { + // Expand the stack if needed + if (RAPIDJSON_UNLIKELY(static_cast(sizeof(T) * count) > (stackEnd_ - stackTop_))) + Expand(count); + } + + template + RAPIDJSON_FORCEINLINE T* Push(size_t count = 1) { + Reserve(count); + return PushUnsafe(count); + } + + template + RAPIDJSON_FORCEINLINE T* PushUnsafe(size_t count = 1) { + RAPIDJSON_ASSERT(stackTop_); + RAPIDJSON_ASSERT(static_cast(sizeof(T) * count) <= (stackEnd_ - stackTop_)); + T* ret = reinterpret_cast(stackTop_); + stackTop_ += sizeof(T) * count; + return ret; + } + + template + T* Pop(size_t count) { + RAPIDJSON_ASSERT(GetSize() >= count * sizeof(T)); + stackTop_ -= count * sizeof(T); + return reinterpret_cast(stackTop_); + } + + template + T* Top() { + RAPIDJSON_ASSERT(GetSize() >= sizeof(T)); + return reinterpret_cast(stackTop_ - sizeof(T)); + } + + template + const T* Top() const { + RAPIDJSON_ASSERT(GetSize() >= sizeof(T)); + return reinterpret_cast(stackTop_ - sizeof(T)); + } + + template + T* End() { return reinterpret_cast(stackTop_); } + + template + const T* End() const { return reinterpret_cast(stackTop_); } + + template + T* Bottom() { return reinterpret_cast(stack_); } + + template + const T* Bottom() const { return reinterpret_cast(stack_); } + + bool HasAllocator() const { + return allocator_ != 0; + } + + Allocator& GetAllocator() { + RAPIDJSON_ASSERT(allocator_); + return *allocator_; + } + + bool Empty() const { return stackTop_ == stack_; } + size_t GetSize() const { return static_cast(stackTop_ - stack_); } + size_t GetCapacity() const { return static_cast(stackEnd_ - stack_); } + +private: + template + void Expand(size_t count) { + // Only expand the capacity if the current stack exists. Otherwise just create a stack with initial capacity. + size_t newCapacity; + if (stack_ == 0) { + if (!allocator_) + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)(); + newCapacity = initialCapacity_; + } else { + newCapacity = GetCapacity(); + newCapacity += (newCapacity + 1) / 2; + } + size_t newSize = GetSize() + sizeof(T) * count; + if (newCapacity < newSize) + newCapacity = newSize; + + Resize(newCapacity); + } + + void Resize(size_t newCapacity) { + const size_t size = GetSize(); // Backup the current size + stack_ = static_cast(allocator_->Realloc(stack_, GetCapacity(), newCapacity)); + stackTop_ = stack_ + size; + stackEnd_ = stack_ + newCapacity; + } + + void Destroy() { + Allocator::Free(stack_); + RAPIDJSON_DELETE(ownAllocator_); // Only delete if it is owned by the stack + } + + // Prohibit copy constructor & assignment operator. + Stack(const Stack&); + Stack& operator=(const Stack&); + + Allocator* allocator_; + Allocator* ownAllocator_; + char *stack_; + char *stackTop_; + char *stackEnd_; + size_t initialCapacity_; +}; + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#if defined(__clang__) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_STACK_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/internal/strfunc.h b/include/liblvgl/libs/thorvg/rapidjson/internal/strfunc.h new file mode 100644 index 00000000..1a88f49b --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/internal/strfunc.h @@ -0,0 +1,83 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_INTERNAL_STRFUNC_H_ +#define RAPIDJSON_INTERNAL_STRFUNC_H_ + +#include "../stream.h" +#include + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +//! Custom strlen() which works on different character types. +/*! \tparam Ch Character type (e.g. char, wchar_t, short) + \param s Null-terminated input string. + \return Number of characters in the string. + \note This has the same semantics as strlen(), the return value is not number of Unicode codepoints. +*/ +template +inline SizeType StrLen(const Ch* s) { + RAPIDJSON_ASSERT(s != 0); + const Ch* p = s; + while (*p) ++p; + return SizeType(p - s); +} + +template <> +inline SizeType StrLen(const char* s) { + return SizeType(std::strlen(s)); +} + +template <> +inline SizeType StrLen(const wchar_t* s) { + return SizeType(std::wcslen(s)); +} + +//! Custom strcmpn() which works on different character types. +/*! \tparam Ch Character type (e.g. char, wchar_t, short) + \param s1 Null-terminated input string. + \param s2 Null-terminated input string. + \return 0 if equal +*/ +template +inline int StrCmp(const Ch* s1, const Ch* s2) { + RAPIDJSON_ASSERT(s1 != 0); + RAPIDJSON_ASSERT(s2 != 0); + while(*s1 && (*s1 == *s2)) { s1++; s2++; } + return static_cast(*s1) < static_cast(*s2) ? -1 : static_cast(*s1) > static_cast(*s2); +} + +//! Returns number of code points in a encoded string. +template +bool CountStringCodePoint(const typename Encoding::Ch* s, SizeType length, SizeType* outCount) { + RAPIDJSON_ASSERT(s != 0); + RAPIDJSON_ASSERT(outCount != 0); + GenericStringStream is(s); + const typename Encoding::Ch* end = s + length; + SizeType count = 0; + while (is.src_ < end) { + unsigned codepoint; + if (!Encoding::Decode(is, &codepoint)) + return false; + count++; + } + *outCount = count; + return true; +} + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_INTERNAL_STRFUNC_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/internal/strtod.h b/include/liblvgl/libs/thorvg/rapidjson/internal/strtod.h new file mode 100644 index 00000000..62a42c69 --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/internal/strtod.h @@ -0,0 +1,293 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_STRTOD_ +#define RAPIDJSON_STRTOD_ + +#include "ieee754.h" +#include "biginteger.h" +#include "diyfp.h" +#include "pow10.h" +#include +#include + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +inline double FastPath(double significand, int exp) { + if (exp < -308) + return 0.0; + else if (exp >= 0) + return significand * internal::Pow10(exp); + else + return significand / internal::Pow10(-exp); +} + +inline double StrtodNormalPrecision(double d, int p) { + if (p < -308) { + // Prevent expSum < -308, making Pow10(p) = 0 + d = FastPath(d, -308); + d = FastPath(d, p + 308); + } + else + d = FastPath(d, p); + return d; +} + +template +inline T Min3(T a, T b, T c) { + T m = a; + if (m > b) m = b; + if (m > c) m = c; + return m; +} + +inline int CheckWithinHalfULP(double b, const BigInteger& d, int dExp) { + const Double db(b); + const uint64_t bInt = db.IntegerSignificand(); + const int bExp = db.IntegerExponent(); + const int hExp = bExp - 1; + + int dS_Exp2 = 0, dS_Exp5 = 0, bS_Exp2 = 0, bS_Exp5 = 0, hS_Exp2 = 0, hS_Exp5 = 0; + + // Adjust for decimal exponent + if (dExp >= 0) { + dS_Exp2 += dExp; + dS_Exp5 += dExp; + } + else { + bS_Exp2 -= dExp; + bS_Exp5 -= dExp; + hS_Exp2 -= dExp; + hS_Exp5 -= dExp; + } + + // Adjust for binary exponent + if (bExp >= 0) + bS_Exp2 += bExp; + else { + dS_Exp2 -= bExp; + hS_Exp2 -= bExp; + } + + // Adjust for half ulp exponent + if (hExp >= 0) + hS_Exp2 += hExp; + else { + dS_Exp2 -= hExp; + bS_Exp2 -= hExp; + } + + // Remove common power of two factor from all three scaled values + int common_Exp2 = Min3(dS_Exp2, bS_Exp2, hS_Exp2); + dS_Exp2 -= common_Exp2; + bS_Exp2 -= common_Exp2; + hS_Exp2 -= common_Exp2; + + BigInteger dS = d; + dS.MultiplyPow5(static_cast(dS_Exp5)) <<= static_cast(dS_Exp2); + + BigInteger bS(bInt); + bS.MultiplyPow5(static_cast(bS_Exp5)) <<= static_cast(bS_Exp2); + + BigInteger hS(1); + hS.MultiplyPow5(static_cast(hS_Exp5)) <<= static_cast(hS_Exp2); + + BigInteger delta(0); + dS.Difference(bS, &delta); + + return delta.Compare(hS); +} + +inline bool StrtodFast(double d, int p, double* result) { + // Use fast path for string-to-double conversion if possible + // see http://www.exploringbinary.com/fast-path-decimal-to-floating-point-conversion/ + if (p > 22 && p < 22 + 16) { + // Fast Path Cases In Disguise + d *= internal::Pow10(p - 22); + p = 22; + } + + if (p >= -22 && p <= 22 && d <= 9007199254740991.0) { // 2^53 - 1 + *result = FastPath(d, p); + return true; + } + else + return false; +} + +// Compute an approximation and see if it is within 1/2 ULP +template +inline bool StrtodDiyFp(const Ch* decimals, int dLen, int dExp, double* result) { + uint64_t significand = 0; + int i = 0; // 2^64 - 1 = 18446744073709551615, 1844674407370955161 = 0x1999999999999999 + for (; i < dLen; i++) { + if (significand > RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) || + (significand == RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) && decimals[i] > Ch('5'))) + break; + significand = significand * 10u + static_cast(decimals[i] - Ch('0')); + } + + if (i < dLen && decimals[i] >= Ch('5')) // Rounding + significand++; + + int remaining = dLen - i; + const int kUlpShift = 3; + const int kUlp = 1 << kUlpShift; + int64_t error = (remaining == 0) ? 0 : kUlp / 2; + + DiyFp v(significand, 0); + v = v.Normalize(); + error <<= -v.e; + + dExp += remaining; + + int actualExp; + DiyFp cachedPower = GetCachedPower10(dExp, &actualExp); + if (actualExp != dExp) { + static const DiyFp kPow10[] = { + DiyFp(RAPIDJSON_UINT64_C2(0xa0000000, 0x00000000), -60), // 10^1 + DiyFp(RAPIDJSON_UINT64_C2(0xc8000000, 0x00000000), -57), // 10^2 + DiyFp(RAPIDJSON_UINT64_C2(0xfa000000, 0x00000000), -54), // 10^3 + DiyFp(RAPIDJSON_UINT64_C2(0x9c400000, 0x00000000), -50), // 10^4 + DiyFp(RAPIDJSON_UINT64_C2(0xc3500000, 0x00000000), -47), // 10^5 + DiyFp(RAPIDJSON_UINT64_C2(0xf4240000, 0x00000000), -44), // 10^6 + DiyFp(RAPIDJSON_UINT64_C2(0x98968000, 0x00000000), -40) // 10^7 + }; + int adjustment = dExp - actualExp; + RAPIDJSON_ASSERT(adjustment >= 1 && adjustment < 8); + v = v * kPow10[adjustment - 1]; + if (dLen + adjustment > 19) // has more digits than decimal digits in 64-bit + error += kUlp / 2; + } + + v = v * cachedPower; + + error += kUlp + (error == 0 ? 0 : 1); + + const int oldExp = v.e; + v = v.Normalize(); + error <<= oldExp - v.e; + + const int effectiveSignificandSize = Double::EffectiveSignificandSize(64 + v.e); + int precisionSize = 64 - effectiveSignificandSize; + if (precisionSize + kUlpShift >= 64) { + int scaleExp = (precisionSize + kUlpShift) - 63; + v.f >>= scaleExp; + v.e += scaleExp; + error = (error >> scaleExp) + 1 + kUlp; + precisionSize -= scaleExp; + } + + DiyFp rounded(v.f >> precisionSize, v.e + precisionSize); + const uint64_t precisionBits = (v.f & ((uint64_t(1) << precisionSize) - 1)) * kUlp; + const uint64_t halfWay = (uint64_t(1) << (precisionSize - 1)) * kUlp; + if (precisionBits >= halfWay + static_cast(error)) { + rounded.f++; + if (rounded.f & (DiyFp::kDpHiddenBit << 1)) { // rounding overflows mantissa (issue #340) + rounded.f >>= 1; + rounded.e++; + } + } + + *result = rounded.ToDouble(); + + return halfWay - static_cast(error) >= precisionBits || precisionBits >= halfWay + static_cast(error); +} + +template +inline double StrtodBigInteger(double approx, const Ch* decimals, int dLen, int dExp) { + RAPIDJSON_ASSERT(dLen >= 0); + const BigInteger dInt(decimals, static_cast(dLen)); + Double a(approx); + int cmp = CheckWithinHalfULP(a.Value(), dInt, dExp); + if (cmp < 0) + return a.Value(); // within half ULP + else if (cmp == 0) { + // Round towards even + if (a.Significand() & 1) + return a.NextPositiveDouble(); + else + return a.Value(); + } + else // adjustment + return a.NextPositiveDouble(); +} + +template +inline double StrtodFullPrecision(double d, int p, const Ch* decimals, size_t length, size_t decimalPosition, int exp) { + RAPIDJSON_ASSERT(d >= 0.0); + RAPIDJSON_ASSERT(length >= 1); + + double result = 0.0; + if (StrtodFast(d, p, &result)) + return result; + + RAPIDJSON_ASSERT(length <= INT_MAX); + int dLen = static_cast(length); + + RAPIDJSON_ASSERT(length >= decimalPosition); + RAPIDJSON_ASSERT(length - decimalPosition <= INT_MAX); + int dExpAdjust = static_cast(length - decimalPosition); + + RAPIDJSON_ASSERT(exp >= INT_MIN + dExpAdjust); + int dExp = exp - dExpAdjust; + + // Make sure length+dExp does not overflow + RAPIDJSON_ASSERT(dExp <= INT_MAX - dLen); + + // Trim leading zeros + while (dLen > 0 && *decimals == '0') { + dLen--; + decimals++; + } + + // Trim trailing zeros + while (dLen > 0 && decimals[dLen - 1] == '0') { + dLen--; + dExp++; + } + + if (dLen == 0) { // Buffer only contains zeros. + return 0.0; + } + + // Trim right-most digits + const int kMaxDecimalDigit = 767 + 1; + if (dLen > kMaxDecimalDigit) { + dExp += dLen - kMaxDecimalDigit; + dLen = kMaxDecimalDigit; + } + + // If too small, underflow to zero. + // Any x <= 10^-324 is interpreted as zero. + if (dLen + dExp <= -324) + return 0.0; + + // If too large, overflow to infinity. + // Any x >= 10^309 is interpreted as +infinity. + if (dLen + dExp > 309) + return std::numeric_limits::infinity(); + + if (StrtodDiyFp(decimals, dLen, dExp, &result)) + return result; + + // Use approximation from StrtodDiyFp and make adjustment with BigInteger comparison + return StrtodBigInteger(result, decimals, dLen, dExp); +} + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_STRTOD_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/internal/swap.h b/include/liblvgl/libs/thorvg/rapidjson/internal/swap.h new file mode 100644 index 00000000..2cf92f93 --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/internal/swap.h @@ -0,0 +1,46 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_INTERNAL_SWAP_H_ +#define RAPIDJSON_INTERNAL_SWAP_H_ + +#include "../rapidjson.h" + +#if defined(__clang__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(c++98-compat) +#endif + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +//! Custom swap() to avoid dependency on C++ header +/*! \tparam T Type of the arguments to swap, should be instantiated with primitive C++ types only. + \note This has the same semantics as std::swap(). +*/ +template +inline void Swap(T& a, T& b) RAPIDJSON_NOEXCEPT { + T tmp = a; + a = b; + b = tmp; +} + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#if defined(__clang__) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_INTERNAL_SWAP_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/istreamwrapper.h b/include/liblvgl/libs/thorvg/rapidjson/istreamwrapper.h new file mode 100644 index 00000000..23dd83e9 --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/istreamwrapper.h @@ -0,0 +1,128 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ISTREAMWRAPPER_H_ +#define RAPIDJSON_ISTREAMWRAPPER_H_ + +#include "stream.h" +#include +#include + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +#elif defined(_MSC_VER) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4351) // new behavior: elements of array 'array' will be default initialized +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Wrapper of \c std::basic_istream into RapidJSON's Stream concept. +/*! + The classes can be wrapped including but not limited to: + + - \c std::istringstream + - \c std::stringstream + - \c std::wistringstream + - \c std::wstringstream + - \c std::ifstream + - \c std::fstream + - \c std::wifstream + - \c std::wfstream + + \tparam StreamType Class derived from \c std::basic_istream. +*/ + +template +class BasicIStreamWrapper { +public: + typedef typename StreamType::char_type Ch; + + //! Constructor. + /*! + \param stream stream opened for read. + */ + BasicIStreamWrapper(StreamType &stream) : stream_(stream), buffer_(peekBuffer_), bufferSize_(4), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) { + Read(); + } + + //! Constructor. + /*! + \param stream stream opened for read. + \param buffer user-supplied buffer. + \param bufferSize size of buffer in bytes. Must >=4 bytes. + */ + BasicIStreamWrapper(StreamType &stream, char* buffer, size_t bufferSize) : stream_(stream), buffer_(buffer), bufferSize_(bufferSize), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) { + RAPIDJSON_ASSERT(bufferSize >= 4); + Read(); + } + + Ch Peek() const { return *current_; } + Ch Take() { Ch c = *current_; Read(); return c; } + size_t Tell() const { return count_ + static_cast(current_ - buffer_); } + + // Not implemented + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + + // For encoding detection only. + const Ch* Peek4() const { + return (current_ + 4 - !eof_ <= bufferLast_) ? current_ : 0; + } + +private: + BasicIStreamWrapper(); + BasicIStreamWrapper(const BasicIStreamWrapper&); + BasicIStreamWrapper& operator=(const BasicIStreamWrapper&); + + void Read() { + if (current_ < bufferLast_) + ++current_; + else if (!eof_) { + count_ += readCount_; + readCount_ = bufferSize_; + bufferLast_ = buffer_ + readCount_ - 1; + current_ = buffer_; + + if (!stream_.read(buffer_, static_cast(bufferSize_))) { + readCount_ = static_cast(stream_.gcount()); + *(bufferLast_ = buffer_ + readCount_) = '\0'; + eof_ = true; + } + } + } + + StreamType &stream_; + Ch peekBuffer_[4], *buffer_; + size_t bufferSize_; + Ch *bufferLast_; + Ch *current_; + size_t readCount_; + size_t count_; //!< Number of characters read + bool eof_; +}; + +typedef BasicIStreamWrapper IStreamWrapper; +typedef BasicIStreamWrapper WIStreamWrapper; + +#if defined(__clang__) || defined(_MSC_VER) +RAPIDJSON_DIAG_POP +#endif + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_ISTREAMWRAPPER_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/memorybuffer.h b/include/liblvgl/libs/thorvg/rapidjson/memorybuffer.h new file mode 100644 index 00000000..14aad686 --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/memorybuffer.h @@ -0,0 +1,70 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_MEMORYBUFFER_H_ +#define RAPIDJSON_MEMORYBUFFER_H_ + +#include "stream.h" +#include "internal/stack.h" + +RAPIDJSON_NAMESPACE_BEGIN + +//! Represents an in-memory output byte stream. +/*! + This class is mainly for being wrapped by EncodedOutputStream or AutoUTFOutputStream. + + It is similar to FileWriteBuffer but the destination is an in-memory buffer instead of a file. + + Differences between MemoryBuffer and StringBuffer: + 1. StringBuffer has Encoding but MemoryBuffer is only a byte buffer. + 2. StringBuffer::GetString() returns a null-terminated string. MemoryBuffer::GetBuffer() returns a buffer without terminator. + + \tparam Allocator type for allocating memory buffer. + \note implements Stream concept +*/ +template +struct GenericMemoryBuffer { + typedef char Ch; // byte + + GenericMemoryBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {} + + void Put(Ch c) { *stack_.template Push() = c; } + void Flush() {} + + void Clear() { stack_.Clear(); } + void ShrinkToFit() { stack_.ShrinkToFit(); } + Ch* Push(size_t count) { return stack_.template Push(count); } + void Pop(size_t count) { stack_.template Pop(count); } + + const Ch* GetBuffer() const { + return stack_.template Bottom(); + } + + size_t GetSize() const { return stack_.GetSize(); } + + static const size_t kDefaultCapacity = 256; + mutable internal::Stack stack_; +}; + +typedef GenericMemoryBuffer<> MemoryBuffer; + +//! Implement specialized version of PutN() with memset() for better performance. +template<> +inline void PutN(MemoryBuffer& memoryBuffer, char c, size_t n) { + std::memset(memoryBuffer.stack_.Push(n), c, n * sizeof(c)); +} + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_MEMORYBUFFER_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/memorystream.h b/include/liblvgl/libs/thorvg/rapidjson/memorystream.h new file mode 100644 index 00000000..1bc393f2 --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/memorystream.h @@ -0,0 +1,71 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_MEMORYSTREAM_H_ +#define RAPIDJSON_MEMORYSTREAM_H_ + +#include "stream.h" + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(unreachable-code) +RAPIDJSON_DIAG_OFF(missing-noreturn) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Represents an in-memory input byte stream. +/*! + This class is mainly for being wrapped by EncodedInputStream or AutoUTFInputStream. + + It is similar to FileReadBuffer but the source is an in-memory buffer instead of a file. + + Differences between MemoryStream and StringStream: + 1. StringStream has encoding but MemoryStream is a byte stream. + 2. MemoryStream needs size of the source buffer and the buffer don't need to be null terminated. StringStream assume null-terminated string as source. + 3. MemoryStream supports Peek4() for encoding detection. StringStream is specified with an encoding so it should not have Peek4(). + \note implements Stream concept +*/ +struct MemoryStream { + typedef char Ch; // byte + + MemoryStream(const Ch *src, size_t size) : src_(src), begin_(src), end_(src + size), size_(size) {} + + Ch Peek() const { return RAPIDJSON_UNLIKELY(src_ == end_) ? '\0' : *src_; } + Ch Take() { return RAPIDJSON_UNLIKELY(src_ == end_) ? '\0' : *src_++; } + size_t Tell() const { return static_cast(src_ - begin_); } + + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + + // For encoding detection only. + const Ch* Peek4() const { + return Tell() + 4 <= size_ ? src_ : 0; + } + + const Ch* src_; //!< Current read position. + const Ch* begin_; //!< Original head of the string. + const Ch* end_; //!< End of stream. + size_t size_; //!< Size of the stream. +}; + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_MEMORYBUFFER_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/msinttypes/inttypes.h b/include/liblvgl/libs/thorvg/rapidjson/msinttypes/inttypes.h new file mode 100644 index 00000000..1620402e --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/msinttypes/inttypes.h @@ -0,0 +1,316 @@ +// ISO C9x compliant inttypes.h for Microsoft Visual Studio +// Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124 +// +// Copyright (c) 2006-2013 Alexander Chemeris +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// 3. Neither the name of the product nor the names of its contributors may +// be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +/////////////////////////////////////////////////////////////////////////////// + +// The above software in this distribution may have been modified by +// THL A29 Limited ("Tencent Modifications"). +// All Tencent Modifications are Copyright (C) 2015 THL A29 Limited. + +#ifndef _MSC_VER // [ +#error "Use this header only with Microsoft Visual C++ compilers!" +#endif // _MSC_VER ] + +#ifndef _MSC_INTTYPES_H_ // [ +#define _MSC_INTTYPES_H_ + +#if _MSC_VER > 1000 +#pragma once +#endif + +#include "stdint.h" + +// miloyip: VC supports inttypes.h since VC2013 +#if _MSC_VER >= 1800 +#include +#else + +// 7.8 Format conversion of integer types + +typedef struct { + intmax_t quot; + intmax_t rem; +} imaxdiv_t; + +// 7.8.1 Macros for format specifiers + +#if !defined(__cplusplus) || defined(__STDC_FORMAT_MACROS) // [ See footnote 185 at page 198 + +// The fprintf macros for signed integers are: +#define PRId8 "d" +#define PRIi8 "i" +#define PRIdLEAST8 "d" +#define PRIiLEAST8 "i" +#define PRIdFAST8 "d" +#define PRIiFAST8 "i" + +#define PRId16 "hd" +#define PRIi16 "hi" +#define PRIdLEAST16 "hd" +#define PRIiLEAST16 "hi" +#define PRIdFAST16 "hd" +#define PRIiFAST16 "hi" + +#define PRId32 "I32d" +#define PRIi32 "I32i" +#define PRIdLEAST32 "I32d" +#define PRIiLEAST32 "I32i" +#define PRIdFAST32 "I32d" +#define PRIiFAST32 "I32i" + +#define PRId64 "I64d" +#define PRIi64 "I64i" +#define PRIdLEAST64 "I64d" +#define PRIiLEAST64 "I64i" +#define PRIdFAST64 "I64d" +#define PRIiFAST64 "I64i" + +#define PRIdMAX "I64d" +#define PRIiMAX "I64i" + +#define PRIdPTR "Id" +#define PRIiPTR "Ii" + +// The fprintf macros for unsigned integers are: +#define PRIo8 "o" +#define PRIu8 "u" +#define PRIx8 "x" +#define PRIX8 "X" +#define PRIoLEAST8 "o" +#define PRIuLEAST8 "u" +#define PRIxLEAST8 "x" +#define PRIXLEAST8 "X" +#define PRIoFAST8 "o" +#define PRIuFAST8 "u" +#define PRIxFAST8 "x" +#define PRIXFAST8 "X" + +#define PRIo16 "ho" +#define PRIu16 "hu" +#define PRIx16 "hx" +#define PRIX16 "hX" +#define PRIoLEAST16 "ho" +#define PRIuLEAST16 "hu" +#define PRIxLEAST16 "hx" +#define PRIXLEAST16 "hX" +#define PRIoFAST16 "ho" +#define PRIuFAST16 "hu" +#define PRIxFAST16 "hx" +#define PRIXFAST16 "hX" + +#define PRIo32 "I32o" +#define PRIu32 "I32u" +#define PRIx32 "I32x" +#define PRIX32 "I32X" +#define PRIoLEAST32 "I32o" +#define PRIuLEAST32 "I32u" +#define PRIxLEAST32 "I32x" +#define PRIXLEAST32 "I32X" +#define PRIoFAST32 "I32o" +#define PRIuFAST32 "I32u" +#define PRIxFAST32 "I32x" +#define PRIXFAST32 "I32X" + +#define PRIo64 "I64o" +#define PRIu64 "I64u" +#define PRIx64 "I64x" +#define PRIX64 "I64X" +#define PRIoLEAST64 "I64o" +#define PRIuLEAST64 "I64u" +#define PRIxLEAST64 "I64x" +#define PRIXLEAST64 "I64X" +#define PRIoFAST64 "I64o" +#define PRIuFAST64 "I64u" +#define PRIxFAST64 "I64x" +#define PRIXFAST64 "I64X" + +#define PRIoMAX "I64o" +#define PRIuMAX "I64u" +#define PRIxMAX "I64x" +#define PRIXMAX "I64X" + +#define PRIoPTR "Io" +#define PRIuPTR "Iu" +#define PRIxPTR "Ix" +#define PRIXPTR "IX" + +// The fscanf macros for signed integers are: +#define SCNd8 "d" +#define SCNi8 "i" +#define SCNdLEAST8 "d" +#define SCNiLEAST8 "i" +#define SCNdFAST8 "d" +#define SCNiFAST8 "i" + +#define SCNd16 "hd" +#define SCNi16 "hi" +#define SCNdLEAST16 "hd" +#define SCNiLEAST16 "hi" +#define SCNdFAST16 "hd" +#define SCNiFAST16 "hi" + +#define SCNd32 "ld" +#define SCNi32 "li" +#define SCNdLEAST32 "ld" +#define SCNiLEAST32 "li" +#define SCNdFAST32 "ld" +#define SCNiFAST32 "li" + +#define SCNd64 "I64d" +#define SCNi64 "I64i" +#define SCNdLEAST64 "I64d" +#define SCNiLEAST64 "I64i" +#define SCNdFAST64 "I64d" +#define SCNiFAST64 "I64i" + +#define SCNdMAX "I64d" +#define SCNiMAX "I64i" + +#ifdef _WIN64 // [ +# define SCNdPTR "I64d" +# define SCNiPTR "I64i" +#else // _WIN64 ][ +# define SCNdPTR "ld" +# define SCNiPTR "li" +#endif // _WIN64 ] + +// The fscanf macros for unsigned integers are: +#define SCNo8 "o" +#define SCNu8 "u" +#define SCNx8 "x" +#define SCNX8 "X" +#define SCNoLEAST8 "o" +#define SCNuLEAST8 "u" +#define SCNxLEAST8 "x" +#define SCNXLEAST8 "X" +#define SCNoFAST8 "o" +#define SCNuFAST8 "u" +#define SCNxFAST8 "x" +#define SCNXFAST8 "X" + +#define SCNo16 "ho" +#define SCNu16 "hu" +#define SCNx16 "hx" +#define SCNX16 "hX" +#define SCNoLEAST16 "ho" +#define SCNuLEAST16 "hu" +#define SCNxLEAST16 "hx" +#define SCNXLEAST16 "hX" +#define SCNoFAST16 "ho" +#define SCNuFAST16 "hu" +#define SCNxFAST16 "hx" +#define SCNXFAST16 "hX" + +#define SCNo32 "lo" +#define SCNu32 "lu" +#define SCNx32 "lx" +#define SCNX32 "lX" +#define SCNoLEAST32 "lo" +#define SCNuLEAST32 "lu" +#define SCNxLEAST32 "lx" +#define SCNXLEAST32 "lX" +#define SCNoFAST32 "lo" +#define SCNuFAST32 "lu" +#define SCNxFAST32 "lx" +#define SCNXFAST32 "lX" + +#define SCNo64 "I64o" +#define SCNu64 "I64u" +#define SCNx64 "I64x" +#define SCNX64 "I64X" +#define SCNoLEAST64 "I64o" +#define SCNuLEAST64 "I64u" +#define SCNxLEAST64 "I64x" +#define SCNXLEAST64 "I64X" +#define SCNoFAST64 "I64o" +#define SCNuFAST64 "I64u" +#define SCNxFAST64 "I64x" +#define SCNXFAST64 "I64X" + +#define SCNoMAX "I64o" +#define SCNuMAX "I64u" +#define SCNxMAX "I64x" +#define SCNXMAX "I64X" + +#ifdef _WIN64 // [ +# define SCNoPTR "I64o" +# define SCNuPTR "I64u" +# define SCNxPTR "I64x" +# define SCNXPTR "I64X" +#else // _WIN64 ][ +# define SCNoPTR "lo" +# define SCNuPTR "lu" +# define SCNxPTR "lx" +# define SCNXPTR "lX" +#endif // _WIN64 ] + +#endif // __STDC_FORMAT_MACROS ] + +// 7.8.2 Functions for greatest-width integer types + +// 7.8.2.1 The imaxabs function +#define imaxabs _abs64 + +// 7.8.2.2 The imaxdiv function + +// This is modified version of div() function from Microsoft's div.c found +// in %MSVC.NET%\crt\src\div.c +#ifdef STATIC_IMAXDIV // [ +static +#else // STATIC_IMAXDIV ][ +_inline +#endif // STATIC_IMAXDIV ] +imaxdiv_t __cdecl imaxdiv(intmax_t numer, intmax_t denom) +{ + imaxdiv_t result; + + result.quot = numer / denom; + result.rem = numer % denom; + + if (numer < 0 && result.rem > 0) { + // did division wrong; must fix up + ++result.quot; + result.rem -= denom; + } + + return result; +} + +// 7.8.2.3 The strtoimax and strtoumax functions +#define strtoimax _strtoi64 +#define strtoumax _strtoui64 + +// 7.8.2.4 The wcstoimax and wcstoumax functions +#define wcstoimax _wcstoi64 +#define wcstoumax _wcstoui64 + +#endif // _MSC_VER >= 1800 + +#endif // _MSC_INTTYPES_H_ ] diff --git a/include/liblvgl/libs/thorvg/rapidjson/msinttypes/stdint.h b/include/liblvgl/libs/thorvg/rapidjson/msinttypes/stdint.h new file mode 100644 index 00000000..1c266ecf --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/msinttypes/stdint.h @@ -0,0 +1,300 @@ +// ISO C9x compliant stdint.h for Microsoft Visual Studio +// Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124 +// +// Copyright (c) 2006-2013 Alexander Chemeris +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// 3. Neither the name of the product nor the names of its contributors may +// be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +/////////////////////////////////////////////////////////////////////////////// + +// The above software in this distribution may have been modified by +// THL A29 Limited ("Tencent Modifications"). +// All Tencent Modifications are Copyright (C) 2015 THL A29 Limited. + +#ifndef _MSC_VER // [ +#error "Use this header only with Microsoft Visual C++ compilers!" +#endif // _MSC_VER ] + +#ifndef _MSC_STDINT_H_ // [ +#define _MSC_STDINT_H_ + +#if _MSC_VER > 1000 +#pragma once +#endif + +// miloyip: Originally Visual Studio 2010 uses its own stdint.h. However it generates warning with INT64_C(), so change to use this file for vs2010. +#if _MSC_VER >= 1600 // [ +#include + +#if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) // [ See footnote 224 at page 260 + +#undef INT8_C +#undef INT16_C +#undef INT32_C +#undef INT64_C +#undef UINT8_C +#undef UINT16_C +#undef UINT32_C +#undef UINT64_C + +// 7.18.4.1 Macros for minimum-width integer constants + +#define INT8_C(val) val##i8 +#define INT16_C(val) val##i16 +#define INT32_C(val) val##i32 +#define INT64_C(val) val##i64 + +#define UINT8_C(val) val##ui8 +#define UINT16_C(val) val##ui16 +#define UINT32_C(val) val##ui32 +#define UINT64_C(val) val##ui64 + +// 7.18.4.2 Macros for greatest-width integer constants +// These #ifndef's are needed to prevent collisions with . +// Check out Issue 9 for the details. +#ifndef INTMAX_C // [ +# define INTMAX_C INT64_C +#endif // INTMAX_C ] +#ifndef UINTMAX_C // [ +# define UINTMAX_C UINT64_C +#endif // UINTMAX_C ] + +#endif // __STDC_CONSTANT_MACROS ] + +#else // ] _MSC_VER >= 1700 [ + +#include + +// For Visual Studio 6 in C++ mode and for many Visual Studio versions when +// compiling for ARM we have to wrap include with 'extern "C++" {}' +// or compiler would give many errors like this: +// error C2733: second C linkage of overloaded function 'wmemchr' not allowed +#if defined(__cplusplus) && !defined(_M_ARM) +extern "C" { +#endif +# include +#if defined(__cplusplus) && !defined(_M_ARM) +} +#endif + +// Define _W64 macros to mark types changing their size, like intptr_t. +#ifndef _W64 +# if !defined(__midl) && (defined(_X86_) || defined(_M_IX86)) && _MSC_VER >= 1300 +# define _W64 __w64 +# else +# define _W64 +# endif +#endif + + +// 7.18.1 Integer types + +// 7.18.1.1 Exact-width integer types + +// Visual Studio 6 and Embedded Visual C++ 4 doesn't +// realize that, e.g. char has the same size as __int8 +// so we give up on __intX for them. +#if (_MSC_VER < 1300) + typedef signed char int8_t; + typedef signed short int16_t; + typedef signed int int32_t; + typedef unsigned char uint8_t; + typedef unsigned short uint16_t; + typedef unsigned int uint32_t; +#else + typedef signed __int8 int8_t; + typedef signed __int16 int16_t; + typedef signed __int32 int32_t; + typedef unsigned __int8 uint8_t; + typedef unsigned __int16 uint16_t; + typedef unsigned __int32 uint32_t; +#endif +typedef signed __int64 int64_t; +typedef unsigned __int64 uint64_t; + + +// 7.18.1.2 Minimum-width integer types +typedef int8_t int_least8_t; +typedef int16_t int_least16_t; +typedef int32_t int_least32_t; +typedef int64_t int_least64_t; +typedef uint8_t uint_least8_t; +typedef uint16_t uint_least16_t; +typedef uint32_t uint_least32_t; +typedef uint64_t uint_least64_t; + +// 7.18.1.3 Fastest minimum-width integer types +typedef int8_t int_fast8_t; +typedef int16_t int_fast16_t; +typedef int32_t int_fast32_t; +typedef int64_t int_fast64_t; +typedef uint8_t uint_fast8_t; +typedef uint16_t uint_fast16_t; +typedef uint32_t uint_fast32_t; +typedef uint64_t uint_fast64_t; + +// 7.18.1.4 Integer types capable of holding object pointers +#ifdef _WIN64 // [ + typedef signed __int64 intptr_t; + typedef unsigned __int64 uintptr_t; +#else // _WIN64 ][ + typedef _W64 signed int intptr_t; + typedef _W64 unsigned int uintptr_t; +#endif // _WIN64 ] + +// 7.18.1.5 Greatest-width integer types +typedef int64_t intmax_t; +typedef uint64_t uintmax_t; + + +// 7.18.2 Limits of specified-width integer types + +#if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS) // [ See footnote 220 at page 257 and footnote 221 at page 259 + +// 7.18.2.1 Limits of exact-width integer types +#define INT8_MIN ((int8_t)_I8_MIN) +#define INT8_MAX _I8_MAX +#define INT16_MIN ((int16_t)_I16_MIN) +#define INT16_MAX _I16_MAX +#define INT32_MIN ((int32_t)_I32_MIN) +#define INT32_MAX _I32_MAX +#define INT64_MIN ((int64_t)_I64_MIN) +#define INT64_MAX _I64_MAX +#define UINT8_MAX _UI8_MAX +#define UINT16_MAX _UI16_MAX +#define UINT32_MAX _UI32_MAX +#define UINT64_MAX _UI64_MAX + +// 7.18.2.2 Limits of minimum-width integer types +#define INT_LEAST8_MIN INT8_MIN +#define INT_LEAST8_MAX INT8_MAX +#define INT_LEAST16_MIN INT16_MIN +#define INT_LEAST16_MAX INT16_MAX +#define INT_LEAST32_MIN INT32_MIN +#define INT_LEAST32_MAX INT32_MAX +#define INT_LEAST64_MIN INT64_MIN +#define INT_LEAST64_MAX INT64_MAX +#define UINT_LEAST8_MAX UINT8_MAX +#define UINT_LEAST16_MAX UINT16_MAX +#define UINT_LEAST32_MAX UINT32_MAX +#define UINT_LEAST64_MAX UINT64_MAX + +// 7.18.2.3 Limits of fastest minimum-width integer types +#define INT_FAST8_MIN INT8_MIN +#define INT_FAST8_MAX INT8_MAX +#define INT_FAST16_MIN INT16_MIN +#define INT_FAST16_MAX INT16_MAX +#define INT_FAST32_MIN INT32_MIN +#define INT_FAST32_MAX INT32_MAX +#define INT_FAST64_MIN INT64_MIN +#define INT_FAST64_MAX INT64_MAX +#define UINT_FAST8_MAX UINT8_MAX +#define UINT_FAST16_MAX UINT16_MAX +#define UINT_FAST32_MAX UINT32_MAX +#define UINT_FAST64_MAX UINT64_MAX + +// 7.18.2.4 Limits of integer types capable of holding object pointers +#ifdef _WIN64 // [ +# define INTPTR_MIN INT64_MIN +# define INTPTR_MAX INT64_MAX +# define UINTPTR_MAX UINT64_MAX +#else // _WIN64 ][ +# define INTPTR_MIN INT32_MIN +# define INTPTR_MAX INT32_MAX +# define UINTPTR_MAX UINT32_MAX +#endif // _WIN64 ] + +// 7.18.2.5 Limits of greatest-width integer types +#define INTMAX_MIN INT64_MIN +#define INTMAX_MAX INT64_MAX +#define UINTMAX_MAX UINT64_MAX + +// 7.18.3 Limits of other integer types + +#ifdef _WIN64 // [ +# define PTRDIFF_MIN _I64_MIN +# define PTRDIFF_MAX _I64_MAX +#else // _WIN64 ][ +# define PTRDIFF_MIN _I32_MIN +# define PTRDIFF_MAX _I32_MAX +#endif // _WIN64 ] + +#define SIG_ATOMIC_MIN INT_MIN +#define SIG_ATOMIC_MAX INT_MAX + +#ifndef SIZE_MAX // [ +# ifdef _WIN64 // [ +# define SIZE_MAX _UI64_MAX +# else // _WIN64 ][ +# define SIZE_MAX _UI32_MAX +# endif // _WIN64 ] +#endif // SIZE_MAX ] + +// WCHAR_MIN and WCHAR_MAX are also defined in +#ifndef WCHAR_MIN // [ +# define WCHAR_MIN 0 +#endif // WCHAR_MIN ] +#ifndef WCHAR_MAX // [ +# define WCHAR_MAX _UI16_MAX +#endif // WCHAR_MAX ] + +#define WINT_MIN 0 +#define WINT_MAX _UI16_MAX + +#endif // __STDC_LIMIT_MACROS ] + + +// 7.18.4 Limits of other integer types + +#if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) // [ See footnote 224 at page 260 + +// 7.18.4.1 Macros for minimum-width integer constants + +#define INT8_C(val) val##i8 +#define INT16_C(val) val##i16 +#define INT32_C(val) val##i32 +#define INT64_C(val) val##i64 + +#define UINT8_C(val) val##ui8 +#define UINT16_C(val) val##ui16 +#define UINT32_C(val) val##ui32 +#define UINT64_C(val) val##ui64 + +// 7.18.4.2 Macros for greatest-width integer constants +// These #ifndef's are needed to prevent collisions with . +// Check out Issue 9 for the details. +#ifndef INTMAX_C // [ +# define INTMAX_C INT64_C +#endif // INTMAX_C ] +#ifndef UINTMAX_C // [ +# define UINTMAX_C UINT64_C +#endif // UINTMAX_C ] + +#endif // __STDC_CONSTANT_MACROS ] + +#endif // _MSC_VER >= 1600 ] + +#endif // _MSC_STDINT_H_ ] diff --git a/include/liblvgl/libs/thorvg/rapidjson/ostreamwrapper.h b/include/liblvgl/libs/thorvg/rapidjson/ostreamwrapper.h new file mode 100644 index 00000000..bfd4d6da --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/ostreamwrapper.h @@ -0,0 +1,81 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_OSTREAMWRAPPER_H_ +#define RAPIDJSON_OSTREAMWRAPPER_H_ + +#include "stream.h" +#include + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Wrapper of \c std::basic_ostream into RapidJSON's Stream concept. +/*! + The classes can be wrapped including but not limited to: + + - \c std::ostringstream + - \c std::stringstream + - \c std::wpstringstream + - \c std::wstringstream + - \c std::ifstream + - \c std::fstream + - \c std::wofstream + - \c std::wfstream + + \tparam StreamType Class derived from \c std::basic_ostream. +*/ + +template +class BasicOStreamWrapper { +public: + typedef typename StreamType::char_type Ch; + BasicOStreamWrapper(StreamType& stream) : stream_(stream) {} + + void Put(Ch c) { + stream_.put(c); + } + + void Flush() { + stream_.flush(); + } + + // Not implemented + char Peek() const { RAPIDJSON_ASSERT(false); return 0; } + char Take() { RAPIDJSON_ASSERT(false); return 0; } + size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } + char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + BasicOStreamWrapper(const BasicOStreamWrapper&); + BasicOStreamWrapper& operator=(const BasicOStreamWrapper&); + + StreamType& stream_; +}; + +typedef BasicOStreamWrapper OStreamWrapper; +typedef BasicOStreamWrapper WOStreamWrapper; + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_OSTREAMWRAPPER_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/pointer.h b/include/liblvgl/libs/thorvg/rapidjson/pointer.h new file mode 100644 index 00000000..d0d762d2 --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/pointer.h @@ -0,0 +1,1470 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_POINTER_H_ +#define RAPIDJSON_POINTER_H_ + +#include "document.h" +#include "uri.h" +#include "internal/itoa.h" +#include "error/error.h" // PointerParseErrorCode + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(switch-enum) +#elif defined(_MSC_VER) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +static const SizeType kPointerInvalidIndex = ~SizeType(0); //!< Represents an invalid index in GenericPointer::Token + +/////////////////////////////////////////////////////////////////////////////// +// GenericPointer + +//! Represents a JSON Pointer. Use Pointer for UTF8 encoding and default allocator. +/*! + This class implements RFC 6901 "JavaScript Object Notation (JSON) Pointer" + (https://tools.ietf.org/html/rfc6901). + + A JSON pointer is for identifying a specific value in a JSON document + (GenericDocument). It can simplify coding of DOM tree manipulation, because it + can access multiple-level depth of DOM tree with single API call. + + After it parses a string representation (e.g. "/foo/0" or URI fragment + representation (e.g. "#/foo/0") into its internal representation (tokens), + it can be used to resolve a specific value in multiple documents, or sub-tree + of documents. + + Contrary to GenericValue, Pointer can be copy constructed and copy assigned. + Apart from assignment, a Pointer cannot be modified after construction. + + Although Pointer is very convenient, please aware that constructing Pointer + involves parsing and dynamic memory allocation. A special constructor with user- + supplied tokens eliminates these. + + GenericPointer depends on GenericDocument and GenericValue. + + \tparam ValueType The value type of the DOM tree. E.g. GenericValue > + \tparam Allocator The allocator type for allocating memory for internal representation. + + \note GenericPointer uses same encoding of ValueType. + However, Allocator of GenericPointer is independent of Allocator of Value. +*/ +template +class GenericPointer { +public: + typedef typename ValueType::EncodingType EncodingType; //!< Encoding type from Value + typedef typename ValueType::Ch Ch; //!< Character type from Value + typedef GenericUri UriType; + + + //! A token is the basic units of internal representation. + /*! + A JSON pointer string representation "/foo/123" is parsed to two tokens: + "foo" and 123. 123 will be represented in both numeric form and string form. + They are resolved according to the actual value type (object or array). + + For token that are not numbers, or the numeric value is out of bound + (greater than limits of SizeType), they are only treated as string form + (i.e. the token's index will be equal to kPointerInvalidIndex). + + This struct is public so that user can create a Pointer without parsing and + allocation, using a special constructor. + */ + struct Token { + const Ch* name; //!< Name of the token. It has null character at the end but it can contain null character. + SizeType length; //!< Length of the name. + SizeType index; //!< A valid array index, if it is not equal to kPointerInvalidIndex. + }; + + //!@name Constructors and destructor. + //@{ + + //! Default constructor. + GenericPointer(Allocator* allocator = 0) : allocator_(allocator), ownAllocator_(), nameBuffer_(), tokens_(), tokenCount_(), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) {} + + //! Constructor that parses a string or URI fragment representation. + /*! + \param source A null-terminated, string or URI fragment representation of JSON pointer. + \param allocator User supplied allocator for this pointer. If no allocator is provided, it creates a self-owned one. + */ + explicit GenericPointer(const Ch* source, Allocator* allocator = 0) : allocator_(allocator), ownAllocator_(), nameBuffer_(), tokens_(), tokenCount_(), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) { + Parse(source, internal::StrLen(source)); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Constructor that parses a string or URI fragment representation. + /*! + \param source A string or URI fragment representation of JSON pointer. + \param allocator User supplied allocator for this pointer. If no allocator is provided, it creates a self-owned one. + \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. + */ + explicit GenericPointer(const std::basic_string& source, Allocator* allocator = 0) : allocator_(allocator), ownAllocator_(), nameBuffer_(), tokens_(), tokenCount_(), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) { + Parse(source.c_str(), source.size()); + } +#endif + + //! Constructor that parses a string or URI fragment representation, with length of the source string. + /*! + \param source A string or URI fragment representation of JSON pointer. + \param length Length of source. + \param allocator User supplied allocator for this pointer. If no allocator is provided, it creates a self-owned one. + \note Slightly faster than the overload without length. + */ + GenericPointer(const Ch* source, size_t length, Allocator* allocator = 0) : allocator_(allocator), ownAllocator_(), nameBuffer_(), tokens_(), tokenCount_(), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) { + Parse(source, length); + } + + //! Constructor with user-supplied tokens. + /*! + This constructor let user supplies const array of tokens. + This prevents the parsing process and eliminates allocation. + This is preferred for memory constrained environments. + + \param tokens An constant array of tokens representing the JSON pointer. + \param tokenCount Number of tokens. + + \b Example + \code + #define NAME(s) { s, sizeof(s) / sizeof(s[0]) - 1, kPointerInvalidIndex } + #define INDEX(i) { #i, sizeof(#i) - 1, i } + + static const Pointer::Token kTokens[] = { NAME("foo"), INDEX(123) }; + static const Pointer p(kTokens, sizeof(kTokens) / sizeof(kTokens[0])); + // Equivalent to static const Pointer p("/foo/123"); + + #undef NAME + #undef INDEX + \endcode + */ + GenericPointer(const Token* tokens, size_t tokenCount) : allocator_(), ownAllocator_(), nameBuffer_(), tokens_(const_cast(tokens)), tokenCount_(tokenCount), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) {} + + //! Copy constructor. + GenericPointer(const GenericPointer& rhs) : allocator_(), ownAllocator_(), nameBuffer_(), tokens_(), tokenCount_(), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) { + *this = rhs; + } + + //! Copy constructor. + GenericPointer(const GenericPointer& rhs, Allocator* allocator) : allocator_(allocator), ownAllocator_(), nameBuffer_(), tokens_(), tokenCount_(), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) { + *this = rhs; + } + + //! Destructor. + ~GenericPointer() { + if (nameBuffer_) // If user-supplied tokens constructor is used, nameBuffer_ is nullptr and tokens_ are not deallocated. + Allocator::Free(tokens_); + RAPIDJSON_DELETE(ownAllocator_); + } + + //! Assignment operator. + GenericPointer& operator=(const GenericPointer& rhs) { + if (this != &rhs) { + // Do not delete ownAllocator + if (nameBuffer_) + Allocator::Free(tokens_); + + tokenCount_ = rhs.tokenCount_; + parseErrorOffset_ = rhs.parseErrorOffset_; + parseErrorCode_ = rhs.parseErrorCode_; + + if (rhs.nameBuffer_) + CopyFromRaw(rhs); // Normally parsed tokens. + else { + tokens_ = rhs.tokens_; // User supplied const tokens. + nameBuffer_ = 0; + } + } + return *this; + } + + //! Swap the content of this pointer with another. + /*! + \param other The pointer to swap with. + \note Constant complexity. + */ + GenericPointer& Swap(GenericPointer& other) RAPIDJSON_NOEXCEPT { + internal::Swap(allocator_, other.allocator_); + internal::Swap(ownAllocator_, other.ownAllocator_); + internal::Swap(nameBuffer_, other.nameBuffer_); + internal::Swap(tokens_, other.tokens_); + internal::Swap(tokenCount_, other.tokenCount_); + internal::Swap(parseErrorOffset_, other.parseErrorOffset_); + internal::Swap(parseErrorCode_, other.parseErrorCode_); + return *this; + } + + //! free-standing swap function helper + /*! + Helper function to enable support for common swap implementation pattern based on \c std::swap: + \code + void swap(MyClass& a, MyClass& b) { + using std::swap; + swap(a.pointer, b.pointer); + // ... + } + \endcode + \see Swap() + */ + friend inline void swap(GenericPointer& a, GenericPointer& b) RAPIDJSON_NOEXCEPT { a.Swap(b); } + + //@} + + //!@name Append token + //@{ + + //! Append a token and return a new Pointer + /*! + \param token Token to be appended. + \param allocator Allocator for the newly return Pointer. + \return A new Pointer with appended token. + */ + GenericPointer Append(const Token& token, Allocator* allocator = 0) const { + GenericPointer r; + r.allocator_ = allocator; + Ch *p = r.CopyFromRaw(*this, 1, token.length + 1); + std::memcpy(p, token.name, (token.length + 1) * sizeof(Ch)); + r.tokens_[tokenCount_].name = p; + r.tokens_[tokenCount_].length = token.length; + r.tokens_[tokenCount_].index = token.index; + return r; + } + + //! Append a name token with length, and return a new Pointer + /*! + \param name Name to be appended. + \param length Length of name. + \param allocator Allocator for the newly return Pointer. + \return A new Pointer with appended token. + */ + GenericPointer Append(const Ch* name, SizeType length, Allocator* allocator = 0) const { + Token token = { name, length, kPointerInvalidIndex }; + return Append(token, allocator); + } + + //! Append a name token without length, and return a new Pointer + /*! + \param name Name (const Ch*) to be appended. + \param allocator Allocator for the newly return Pointer. + \return A new Pointer with appended token. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::NotExpr::Type, Ch> >), (GenericPointer)) + Append(T* name, Allocator* allocator = 0) const { + return Append(name, internal::StrLen(name), allocator); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Append a name token, and return a new Pointer + /*! + \param name Name to be appended. + \param allocator Allocator for the newly return Pointer. + \return A new Pointer with appended token. + */ + GenericPointer Append(const std::basic_string& name, Allocator* allocator = 0) const { + return Append(name.c_str(), static_cast(name.size()), allocator); + } +#endif + + //! Append a index token, and return a new Pointer + /*! + \param index Index to be appended. + \param allocator Allocator for the newly return Pointer. + \return A new Pointer with appended token. + */ + GenericPointer Append(SizeType index, Allocator* allocator = 0) const { + char buffer[21]; + char* end = sizeof(SizeType) == 4 ? internal::u32toa(index, buffer) : internal::u64toa(index, buffer); + SizeType length = static_cast(end - buffer); + buffer[length] = '\0'; + + if (sizeof(Ch) == 1) { + Token token = { reinterpret_cast(buffer), length, index }; + return Append(token, allocator); + } + else { + Ch name[21]; + for (size_t i = 0; i <= length; i++) + name[i] = static_cast(buffer[i]); + Token token = { name, length, index }; + return Append(token, allocator); + } + } + + //! Append a token by value, and return a new Pointer + /*! + \param token token to be appended. + \param allocator Allocator for the newly return Pointer. + \return A new Pointer with appended token. + */ + GenericPointer Append(const ValueType& token, Allocator* allocator = 0) const { + if (token.IsString()) + return Append(token.GetString(), token.GetStringLength(), allocator); + else { + RAPIDJSON_ASSERT(token.IsUint64()); + RAPIDJSON_ASSERT(token.GetUint64() <= SizeType(~0)); + return Append(static_cast(token.GetUint64()), allocator); + } + } + + //!@name Handling Parse Error + //@{ + + //! Check whether this is a valid pointer. + bool IsValid() const { return parseErrorCode_ == kPointerParseErrorNone; } + + //! Get the parsing error offset in code unit. + size_t GetParseErrorOffset() const { return parseErrorOffset_; } + + //! Get the parsing error code. + PointerParseErrorCode GetParseErrorCode() const { return parseErrorCode_; } + + //@} + + //! Get the allocator of this pointer. + Allocator& GetAllocator() { return *allocator_; } + + //!@name Tokens + //@{ + + //! Get the token array (const version only). + const Token* GetTokens() const { return tokens_; } + + //! Get the number of tokens. + size_t GetTokenCount() const { return tokenCount_; } + + //@} + + //!@name Equality/inequality operators + //@{ + + //! Equality operator. + /*! + \note When any pointers are invalid, always returns false. + */ + bool operator==(const GenericPointer& rhs) const { + if (!IsValid() || !rhs.IsValid() || tokenCount_ != rhs.tokenCount_) + return false; + + for (size_t i = 0; i < tokenCount_; i++) { + if (tokens_[i].index != rhs.tokens_[i].index || + tokens_[i].length != rhs.tokens_[i].length || + (tokens_[i].length != 0 && std::memcmp(tokens_[i].name, rhs.tokens_[i].name, sizeof(Ch)* tokens_[i].length) != 0)) + { + return false; + } + } + + return true; + } + + //! Inequality operator. + /*! + \note When any pointers are invalid, always returns true. + */ + bool operator!=(const GenericPointer& rhs) const { return !(*this == rhs); } + + //! Less than operator. + /*! + \note Invalid pointers are always greater than valid ones. + */ + bool operator<(const GenericPointer& rhs) const { + if (!IsValid()) + return false; + if (!rhs.IsValid()) + return true; + + if (tokenCount_ != rhs.tokenCount_) + return tokenCount_ < rhs.tokenCount_; + + for (size_t i = 0; i < tokenCount_; i++) { + if (tokens_[i].index != rhs.tokens_[i].index) + return tokens_[i].index < rhs.tokens_[i].index; + + if (tokens_[i].length != rhs.tokens_[i].length) + return tokens_[i].length < rhs.tokens_[i].length; + + if (int cmp = std::memcmp(tokens_[i].name, rhs.tokens_[i].name, sizeof(Ch) * tokens_[i].length)) + return cmp < 0; + } + + return false; + } + + //@} + + //!@name Stringify + //@{ + + //! Stringify the pointer into string representation. + /*! + \tparam OutputStream Type of output stream. + \param os The output stream. + */ + template + bool Stringify(OutputStream& os) const { + return Stringify(os); + } + + //! Stringify the pointer into URI fragment representation. + /*! + \tparam OutputStream Type of output stream. + \param os The output stream. + */ + template + bool StringifyUriFragment(OutputStream& os) const { + return Stringify(os); + } + + //@} + + //!@name Create value + //@{ + + //! Create a value in a subtree. + /*! + If the value is not exist, it creates all parent values and a JSON Null value. + So it always succeed and return the newly created or existing value. + + Remind that it may change types of parents according to tokens, so it + potentially removes previously stored values. For example, if a document + was an array, and "/foo" is used to create a value, then the document + will be changed to an object, and all existing array elements are lost. + + \param root Root value of a DOM subtree to be resolved. It can be any value other than document root. + \param allocator Allocator for creating the values if the specified value or its parents are not exist. + \param alreadyExist If non-null, it stores whether the resolved value is already exist. + \return The resolved newly created (a JSON Null value), or already exists value. + */ + ValueType& Create(ValueType& root, typename ValueType::AllocatorType& allocator, bool* alreadyExist = 0) const { + RAPIDJSON_ASSERT(IsValid()); + ValueType* v = &root; + bool exist = true; + for (const Token *t = tokens_; t != tokens_ + tokenCount_; ++t) { + if (v->IsArray() && t->name[0] == '-' && t->length == 1) { + v->PushBack(ValueType().Move(), allocator); + v = &((*v)[v->Size() - 1]); + exist = false; + } + else { + if (t->index == kPointerInvalidIndex) { // must be object name + if (!v->IsObject()) + v->SetObject(); // Change to Object + } + else { // object name or array index + if (!v->IsArray() && !v->IsObject()) + v->SetArray(); // Change to Array + } + + if (v->IsArray()) { + if (t->index >= v->Size()) { + v->Reserve(t->index + 1, allocator); + while (t->index >= v->Size()) + v->PushBack(ValueType().Move(), allocator); + exist = false; + } + v = &((*v)[t->index]); + } + else { + typename ValueType::MemberIterator m = v->FindMember(GenericValue(GenericStringRef(t->name, t->length))); + if (m == v->MemberEnd()) { + v->AddMember(ValueType(t->name, t->length, allocator).Move(), ValueType().Move(), allocator); + m = v->MemberEnd(); + v = &(--m)->value; // Assumes AddMember() appends at the end + exist = false; + } + else + v = &m->value; + } + } + } + + if (alreadyExist) + *alreadyExist = exist; + + return *v; + } + + //! Creates a value in a document. + /*! + \param document A document to be resolved. + \param alreadyExist If non-null, it stores whether the resolved value is already exist. + \return The resolved newly created, or already exists value. + */ + template + ValueType& Create(GenericDocument& document, bool* alreadyExist = 0) const { + return Create(document, document.GetAllocator(), alreadyExist); + } + + //@} + + //!@name Compute URI + //@{ + + //! Compute the in-scope URI for a subtree. + // For use with JSON pointers into JSON schema documents. + /*! + \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. + \param rootUri Root URI + \param unresolvedTokenIndex If the pointer cannot resolve a token in the pointer, this parameter can obtain the index of unresolved token. + \param allocator Allocator for Uris + \return Uri if it can be resolved. Otherwise null. + + \note + There are only 3 situations when a URI cannot be resolved: + 1. A value in the path is neither an array nor object. + 2. An object value does not contain the token. + 3. A token is out of range of an array value. + + Use unresolvedTokenIndex to retrieve the token index. + */ + UriType GetUri(ValueType& root, const UriType& rootUri, size_t* unresolvedTokenIndex = 0, Allocator* allocator = 0) const { + static const Ch kIdString[] = { 'i', 'd', '\0' }; + static const ValueType kIdValue(kIdString, 2); + UriType base = UriType(rootUri, allocator); + RAPIDJSON_ASSERT(IsValid()); + ValueType* v = &root; + for (const Token *t = tokens_; t != tokens_ + tokenCount_; ++t) { + switch (v->GetType()) { + case kObjectType: + { + // See if we have an id, and if so resolve with the current base + typename ValueType::MemberIterator m = v->FindMember(kIdValue); + if (m != v->MemberEnd() && (m->value).IsString()) { + UriType here = UriType(m->value, allocator).Resolve(base, allocator); + base = here; + } + m = v->FindMember(GenericValue(GenericStringRef(t->name, t->length))); + if (m == v->MemberEnd()) + break; + v = &m->value; + } + continue; + case kArrayType: + if (t->index == kPointerInvalidIndex || t->index >= v->Size()) + break; + v = &((*v)[t->index]); + continue; + default: + break; + } + + // Error: unresolved token + if (unresolvedTokenIndex) + *unresolvedTokenIndex = static_cast(t - tokens_); + return UriType(allocator); + } + return base; + } + + UriType GetUri(const ValueType& root, const UriType& rootUri, size_t* unresolvedTokenIndex = 0, Allocator* allocator = 0) const { + return GetUri(const_cast(root), rootUri, unresolvedTokenIndex, allocator); + } + + + //!@name Query value + //@{ + + //! Query a value in a subtree. + /*! + \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. + \param unresolvedTokenIndex If the pointer cannot resolve a token in the pointer, this parameter can obtain the index of unresolved token. + \return Pointer to the value if it can be resolved. Otherwise null. + + \note + There are only 3 situations when a value cannot be resolved: + 1. A value in the path is neither an array nor object. + 2. An object value does not contain the token. + 3. A token is out of range of an array value. + + Use unresolvedTokenIndex to retrieve the token index. + */ + ValueType* Get(ValueType& root, size_t* unresolvedTokenIndex = 0) const { + RAPIDJSON_ASSERT(IsValid()); + ValueType* v = &root; + for (const Token *t = tokens_; t != tokens_ + tokenCount_; ++t) { + switch (v->GetType()) { + case kObjectType: + { + typename ValueType::MemberIterator m = v->FindMember(GenericValue(GenericStringRef(t->name, t->length))); + if (m == v->MemberEnd()) + break; + v = &m->value; + } + continue; + case kArrayType: + if (t->index == kPointerInvalidIndex || t->index >= v->Size()) + break; + v = &((*v)[t->index]); + continue; + default: + break; + } + + // Error: unresolved token + if (unresolvedTokenIndex) + *unresolvedTokenIndex = static_cast(t - tokens_); + return 0; + } + return v; + } + + //! Query a const value in a const subtree. + /*! + \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. + \return Pointer to the value if it can be resolved. Otherwise null. + */ + const ValueType* Get(const ValueType& root, size_t* unresolvedTokenIndex = 0) const { + return Get(const_cast(root), unresolvedTokenIndex); + } + + //@} + + //!@name Query a value with default + //@{ + + //! Query a value in a subtree with default value. + /*! + Similar to Get(), but if the specified value do not exists, it creates all parents and clone the default value. + So that this function always succeed. + + \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. + \param defaultValue Default value to be cloned if the value was not exists. + \param allocator Allocator for creating the values if the specified value or its parents are not exist. + \see Create() + */ + ValueType& GetWithDefault(ValueType& root, const ValueType& defaultValue, typename ValueType::AllocatorType& allocator) const { + bool alreadyExist; + ValueType& v = Create(root, allocator, &alreadyExist); + return alreadyExist ? v : v.CopyFrom(defaultValue, allocator); + } + + //! Query a value in a subtree with default null-terminated string. + ValueType& GetWithDefault(ValueType& root, const Ch* defaultValue, typename ValueType::AllocatorType& allocator) const { + bool alreadyExist; + ValueType& v = Create(root, allocator, &alreadyExist); + return alreadyExist ? v : v.SetString(defaultValue, allocator); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Query a value in a subtree with default std::basic_string. + ValueType& GetWithDefault(ValueType& root, const std::basic_string& defaultValue, typename ValueType::AllocatorType& allocator) const { + bool alreadyExist; + ValueType& v = Create(root, allocator, &alreadyExist); + return alreadyExist ? v : v.SetString(defaultValue, allocator); + } +#endif + + //! Query a value in a subtree with default primitive value. + /*! + \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c bool + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (ValueType&)) + GetWithDefault(ValueType& root, T defaultValue, typename ValueType::AllocatorType& allocator) const { + return GetWithDefault(root, ValueType(defaultValue).Move(), allocator); + } + + //! Query a value in a document with default value. + template + ValueType& GetWithDefault(GenericDocument& document, const ValueType& defaultValue) const { + return GetWithDefault(document, defaultValue, document.GetAllocator()); + } + + //! Query a value in a document with default null-terminated string. + template + ValueType& GetWithDefault(GenericDocument& document, const Ch* defaultValue) const { + return GetWithDefault(document, defaultValue, document.GetAllocator()); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Query a value in a document with default std::basic_string. + template + ValueType& GetWithDefault(GenericDocument& document, const std::basic_string& defaultValue) const { + return GetWithDefault(document, defaultValue, document.GetAllocator()); + } +#endif + + //! Query a value in a document with default primitive value. + /*! + \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c bool + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (ValueType&)) + GetWithDefault(GenericDocument& document, T defaultValue) const { + return GetWithDefault(document, defaultValue, document.GetAllocator()); + } + + //@} + + //!@name Set a value + //@{ + + //! Set a value in a subtree, with move semantics. + /*! + It creates all parents if they are not exist or types are different to the tokens. + So this function always succeeds but potentially remove existing values. + + \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. + \param value Value to be set. + \param allocator Allocator for creating the values if the specified value or its parents are not exist. + \see Create() + */ + ValueType& Set(ValueType& root, ValueType& value, typename ValueType::AllocatorType& allocator) const { + return Create(root, allocator) = value; + } + + //! Set a value in a subtree, with copy semantics. + ValueType& Set(ValueType& root, const ValueType& value, typename ValueType::AllocatorType& allocator) const { + return Create(root, allocator).CopyFrom(value, allocator); + } + + //! Set a null-terminated string in a subtree. + ValueType& Set(ValueType& root, const Ch* value, typename ValueType::AllocatorType& allocator) const { + return Create(root, allocator) = ValueType(value, allocator).Move(); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Set a std::basic_string in a subtree. + ValueType& Set(ValueType& root, const std::basic_string& value, typename ValueType::AllocatorType& allocator) const { + return Create(root, allocator) = ValueType(value, allocator).Move(); + } +#endif + + //! Set a primitive value in a subtree. + /*! + \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c bool + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (ValueType&)) + Set(ValueType& root, T value, typename ValueType::AllocatorType& allocator) const { + return Create(root, allocator) = ValueType(value).Move(); + } + + //! Set a value in a document, with move semantics. + template + ValueType& Set(GenericDocument& document, ValueType& value) const { + return Create(document) = value; + } + + //! Set a value in a document, with copy semantics. + template + ValueType& Set(GenericDocument& document, const ValueType& value) const { + return Create(document).CopyFrom(value, document.GetAllocator()); + } + + //! Set a null-terminated string in a document. + template + ValueType& Set(GenericDocument& document, const Ch* value) const { + return Create(document) = ValueType(value, document.GetAllocator()).Move(); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Sets a std::basic_string in a document. + template + ValueType& Set(GenericDocument& document, const std::basic_string& value) const { + return Create(document) = ValueType(value, document.GetAllocator()).Move(); + } +#endif + + //! Set a primitive value in a document. + /*! + \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c bool + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (ValueType&)) + Set(GenericDocument& document, T value) const { + return Create(document) = value; + } + + //@} + + //!@name Swap a value + //@{ + + //! Swap a value with a value in a subtree. + /*! + It creates all parents if they are not exist or types are different to the tokens. + So this function always succeeds but potentially remove existing values. + + \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. + \param value Value to be swapped. + \param allocator Allocator for creating the values if the specified value or its parents are not exist. + \see Create() + */ + ValueType& Swap(ValueType& root, ValueType& value, typename ValueType::AllocatorType& allocator) const { + return Create(root, allocator).Swap(value); + } + + //! Swap a value with a value in a document. + template + ValueType& Swap(GenericDocument& document, ValueType& value) const { + return Create(document).Swap(value); + } + + //@} + + //! Erase a value in a subtree. + /*! + \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. + \return Whether the resolved value is found and erased. + + \note Erasing with an empty pointer \c Pointer(""), i.e. the root, always fail and return false. + */ + bool Erase(ValueType& root) const { + RAPIDJSON_ASSERT(IsValid()); + if (tokenCount_ == 0) // Cannot erase the root + return false; + + ValueType* v = &root; + const Token* last = tokens_ + (tokenCount_ - 1); + for (const Token *t = tokens_; t != last; ++t) { + switch (v->GetType()) { + case kObjectType: + { + typename ValueType::MemberIterator m = v->FindMember(GenericValue(GenericStringRef(t->name, t->length))); + if (m == v->MemberEnd()) + return false; + v = &m->value; + } + break; + case kArrayType: + if (t->index == kPointerInvalidIndex || t->index >= v->Size()) + return false; + v = &((*v)[t->index]); + break; + default: + return false; + } + } + + switch (v->GetType()) { + case kObjectType: + return v->EraseMember(GenericStringRef(last->name, last->length)); + case kArrayType: + if (last->index == kPointerInvalidIndex || last->index >= v->Size()) + return false; + v->Erase(v->Begin() + last->index); + return true; + default: + return false; + } + } + +private: + //! Clone the content from rhs to this. + /*! + \param rhs Source pointer. + \param extraToken Extra tokens to be allocated. + \param extraNameBufferSize Extra name buffer size (in number of Ch) to be allocated. + \return Start of non-occupied name buffer, for storing extra names. + */ + Ch* CopyFromRaw(const GenericPointer& rhs, size_t extraToken = 0, size_t extraNameBufferSize = 0) { + if (!allocator_) // allocator is independently owned. + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)(); + + size_t nameBufferSize = rhs.tokenCount_; // null terminators for tokens + for (Token *t = rhs.tokens_; t != rhs.tokens_ + rhs.tokenCount_; ++t) + nameBufferSize += t->length; + + tokenCount_ = rhs.tokenCount_ + extraToken; + tokens_ = static_cast(allocator_->Malloc(tokenCount_ * sizeof(Token) + (nameBufferSize + extraNameBufferSize) * sizeof(Ch))); + nameBuffer_ = reinterpret_cast(tokens_ + tokenCount_); + if (rhs.tokenCount_ > 0) { + std::memcpy(tokens_, rhs.tokens_, rhs.tokenCount_ * sizeof(Token)); + } + if (nameBufferSize > 0) { + std::memcpy(nameBuffer_, rhs.nameBuffer_, nameBufferSize * sizeof(Ch)); + } + + // Adjust pointers to name buffer + std::ptrdiff_t diff = nameBuffer_ - rhs.nameBuffer_; + for (Token *t = tokens_; t != tokens_ + rhs.tokenCount_; ++t) + t->name += diff; + + return nameBuffer_ + nameBufferSize; + } + + //! Check whether a character should be percent-encoded. + /*! + According to RFC 3986 2.3 Unreserved Characters. + \param c The character (code unit) to be tested. + */ + bool NeedPercentEncode(Ch c) const { + return !((c >= '0' && c <= '9') || (c >= 'A' && c <='Z') || (c >= 'a' && c <= 'z') || c == '-' || c == '.' || c == '_' || c =='~'); + } + + //! Parse a JSON String or its URI fragment representation into tokens. +#ifndef __clang__ // -Wdocumentation + /*! + \param source Either a JSON Pointer string, or its URI fragment representation. Not need to be null terminated. + \param length Length of the source string. + \note Source cannot be JSON String Representation of JSON Pointer, e.g. In "/\u0000", \u0000 will not be unescaped. + */ +#endif + void Parse(const Ch* source, size_t length) { + RAPIDJSON_ASSERT(source != NULL); + RAPIDJSON_ASSERT(nameBuffer_ == 0); + RAPIDJSON_ASSERT(tokens_ == 0); + + // Create own allocator if user did not supply. + if (!allocator_) + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)(); + + // Count number of '/' as tokenCount + tokenCount_ = 0; + for (const Ch* s = source; s != source + length; s++) + if (*s == '/') + tokenCount_++; + + Token* token = tokens_ = static_cast(allocator_->Malloc(tokenCount_ * sizeof(Token) + length * sizeof(Ch))); + Ch* name = nameBuffer_ = reinterpret_cast(tokens_ + tokenCount_); + size_t i = 0; + + // Detect if it is a URI fragment + bool uriFragment = false; + if (source[i] == '#') { + uriFragment = true; + i++; + } + + if (i != length && source[i] != '/') { + parseErrorCode_ = kPointerParseErrorTokenMustBeginWithSolidus; + goto error; + } + + while (i < length) { + RAPIDJSON_ASSERT(source[i] == '/'); + i++; // consumes '/' + + token->name = name; + bool isNumber = true; + + while (i < length && source[i] != '/') { + Ch c = source[i]; + if (uriFragment) { + // Decoding percent-encoding for URI fragment + if (c == '%') { + PercentDecodeStream is(&source[i], source + length); + GenericInsituStringStream os(name); + Ch* begin = os.PutBegin(); + if (!Transcoder, EncodingType>().Validate(is, os) || !is.IsValid()) { + parseErrorCode_ = kPointerParseErrorInvalidPercentEncoding; + goto error; + } + size_t len = os.PutEnd(begin); + i += is.Tell() - 1; + if (len == 1) + c = *name; + else { + name += len; + isNumber = false; + i++; + continue; + } + } + else if (NeedPercentEncode(c)) { + parseErrorCode_ = kPointerParseErrorCharacterMustPercentEncode; + goto error; + } + } + + i++; + + // Escaping "~0" -> '~', "~1" -> '/' + if (c == '~') { + if (i < length) { + c = source[i]; + if (c == '0') c = '~'; + else if (c == '1') c = '/'; + else { + parseErrorCode_ = kPointerParseErrorInvalidEscape; + goto error; + } + i++; + } + else { + parseErrorCode_ = kPointerParseErrorInvalidEscape; + goto error; + } + } + + // First check for index: all of characters are digit + if (c < '0' || c > '9') + isNumber = false; + + *name++ = c; + } + token->length = static_cast(name - token->name); + if (token->length == 0) + isNumber = false; + *name++ = '\0'; // Null terminator + + // Second check for index: more than one digit cannot have leading zero + if (isNumber && token->length > 1 && token->name[0] == '0') + isNumber = false; + + // String to SizeType conversion + SizeType n = 0; + if (isNumber) { + for (size_t j = 0; j < token->length; j++) { + SizeType m = n * 10 + static_cast(token->name[j] - '0'); + if (m < n) { // overflow detection + isNumber = false; + break; + } + n = m; + } + } + + token->index = isNumber ? n : kPointerInvalidIndex; + token++; + } + + RAPIDJSON_ASSERT(name <= nameBuffer_ + length); // Should not overflow buffer + parseErrorCode_ = kPointerParseErrorNone; + return; + + error: + Allocator::Free(tokens_); + nameBuffer_ = 0; + tokens_ = 0; + tokenCount_ = 0; + parseErrorOffset_ = i; + return; + } + + //! Stringify to string or URI fragment representation. + /*! + \tparam uriFragment True for stringifying to URI fragment representation. False for string representation. + \tparam OutputStream type of output stream. + \param os The output stream. + */ + template + bool Stringify(OutputStream& os) const { + RAPIDJSON_ASSERT(IsValid()); + + if (uriFragment) + os.Put('#'); + + for (Token *t = tokens_; t != tokens_ + tokenCount_; ++t) { + os.Put('/'); + for (size_t j = 0; j < t->length; j++) { + Ch c = t->name[j]; + if (c == '~') { + os.Put('~'); + os.Put('0'); + } + else if (c == '/') { + os.Put('~'); + os.Put('1'); + } + else if (uriFragment && NeedPercentEncode(c)) { + // Transcode to UTF8 sequence + GenericStringStream source(&t->name[j]); + PercentEncodeStream target(os); + if (!Transcoder >().Validate(source, target)) + return false; + j += source.Tell() - 1; + } + else + os.Put(c); + } + } + return true; + } + + //! A helper stream for decoding a percent-encoded sequence into code unit. + /*! + This stream decodes %XY triplet into code unit (0-255). + If it encounters invalid characters, it sets output code unit as 0 and + mark invalid, and to be checked by IsValid(). + */ + class PercentDecodeStream { + public: + typedef typename ValueType::Ch Ch; + + //! Constructor + /*! + \param source Start of the stream + \param end Past-the-end of the stream. + */ + PercentDecodeStream(const Ch* source, const Ch* end) : src_(source), head_(source), end_(end), valid_(true) {} + + Ch Take() { + if (*src_ != '%' || src_ + 3 > end_) { // %XY triplet + valid_ = false; + return 0; + } + src_++; + Ch c = 0; + for (int j = 0; j < 2; j++) { + c = static_cast(c << 4); + Ch h = *src_; + if (h >= '0' && h <= '9') c = static_cast(c + h - '0'); + else if (h >= 'A' && h <= 'F') c = static_cast(c + h - 'A' + 10); + else if (h >= 'a' && h <= 'f') c = static_cast(c + h - 'a' + 10); + else { + valid_ = false; + return 0; + } + src_++; + } + return c; + } + + size_t Tell() const { return static_cast(src_ - head_); } + bool IsValid() const { return valid_; } + + private: + const Ch* src_; //!< Current read position. + const Ch* head_; //!< Original head of the string. + const Ch* end_; //!< Past-the-end position. + bool valid_; //!< Whether the parsing is valid. + }; + + //! A helper stream to encode character (UTF-8 code unit) into percent-encoded sequence. + template + class PercentEncodeStream { + public: + PercentEncodeStream(OutputStream& os) : os_(os) {} + void Put(char c) { // UTF-8 must be byte + unsigned char u = static_cast(c); + static const char hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + os_.Put('%'); + os_.Put(static_cast(hexDigits[u >> 4])); + os_.Put(static_cast(hexDigits[u & 15])); + } + private: + OutputStream& os_; + }; + + Allocator* allocator_; //!< The current allocator. It is either user-supplied or equal to ownAllocator_. + Allocator* ownAllocator_; //!< Allocator owned by this Pointer. + Ch* nameBuffer_; //!< A buffer containing all names in tokens. + Token* tokens_; //!< A list of tokens. + size_t tokenCount_; //!< Number of tokens in tokens_. + size_t parseErrorOffset_; //!< Offset in code unit when parsing fail. + PointerParseErrorCode parseErrorCode_; //!< Parsing error code. +}; + +//! GenericPointer for Value (UTF-8, default allocator). +typedef GenericPointer Pointer; + +//!@name Helper functions for GenericPointer +//@{ + +////////////////////////////////////////////////////////////////////////////// + +template +typename T::ValueType& CreateValueByPointer(T& root, const GenericPointer& pointer, typename T::AllocatorType& a) { + return pointer.Create(root, a); +} + +template +typename T::ValueType& CreateValueByPointer(T& root, const CharType(&source)[N], typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Create(root, a); +} + +// No allocator parameter + +template +typename DocumentType::ValueType& CreateValueByPointer(DocumentType& document, const GenericPointer& pointer) { + return pointer.Create(document); +} + +template +typename DocumentType::ValueType& CreateValueByPointer(DocumentType& document, const CharType(&source)[N]) { + return GenericPointer(source, N - 1).Create(document); +} + +////////////////////////////////////////////////////////////////////////////// + +template +typename T::ValueType* GetValueByPointer(T& root, const GenericPointer& pointer, size_t* unresolvedTokenIndex = 0) { + return pointer.Get(root, unresolvedTokenIndex); +} + +template +const typename T::ValueType* GetValueByPointer(const T& root, const GenericPointer& pointer, size_t* unresolvedTokenIndex = 0) { + return pointer.Get(root, unresolvedTokenIndex); +} + +template +typename T::ValueType* GetValueByPointer(T& root, const CharType (&source)[N], size_t* unresolvedTokenIndex = 0) { + return GenericPointer(source, N - 1).Get(root, unresolvedTokenIndex); +} + +template +const typename T::ValueType* GetValueByPointer(const T& root, const CharType(&source)[N], size_t* unresolvedTokenIndex = 0) { + return GenericPointer(source, N - 1).Get(root, unresolvedTokenIndex); +} + +////////////////////////////////////////////////////////////////////////////// + +template +typename T::ValueType& GetValueByPointerWithDefault(T& root, const GenericPointer& pointer, const typename T::ValueType& defaultValue, typename T::AllocatorType& a) { + return pointer.GetWithDefault(root, defaultValue, a); +} + +template +typename T::ValueType& GetValueByPointerWithDefault(T& root, const GenericPointer& pointer, const typename T::Ch* defaultValue, typename T::AllocatorType& a) { + return pointer.GetWithDefault(root, defaultValue, a); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename T::ValueType& GetValueByPointerWithDefault(T& root, const GenericPointer& pointer, const std::basic_string& defaultValue, typename T::AllocatorType& a) { + return pointer.GetWithDefault(root, defaultValue, a); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename T::ValueType&)) +GetValueByPointerWithDefault(T& root, const GenericPointer& pointer, T2 defaultValue, typename T::AllocatorType& a) { + return pointer.GetWithDefault(root, defaultValue, a); +} + +template +typename T::ValueType& GetValueByPointerWithDefault(T& root, const CharType(&source)[N], const typename T::ValueType& defaultValue, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).GetWithDefault(root, defaultValue, a); +} + +template +typename T::ValueType& GetValueByPointerWithDefault(T& root, const CharType(&source)[N], const typename T::Ch* defaultValue, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).GetWithDefault(root, defaultValue, a); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename T::ValueType& GetValueByPointerWithDefault(T& root, const CharType(&source)[N], const std::basic_string& defaultValue, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).GetWithDefault(root, defaultValue, a); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename T::ValueType&)) +GetValueByPointerWithDefault(T& root, const CharType(&source)[N], T2 defaultValue, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).GetWithDefault(root, defaultValue, a); +} + +// No allocator parameter + +template +typename DocumentType::ValueType& GetValueByPointerWithDefault(DocumentType& document, const GenericPointer& pointer, const typename DocumentType::ValueType& defaultValue) { + return pointer.GetWithDefault(document, defaultValue); +} + +template +typename DocumentType::ValueType& GetValueByPointerWithDefault(DocumentType& document, const GenericPointer& pointer, const typename DocumentType::Ch* defaultValue) { + return pointer.GetWithDefault(document, defaultValue); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename DocumentType::ValueType& GetValueByPointerWithDefault(DocumentType& document, const GenericPointer& pointer, const std::basic_string& defaultValue) { + return pointer.GetWithDefault(document, defaultValue); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename DocumentType::ValueType&)) +GetValueByPointerWithDefault(DocumentType& document, const GenericPointer& pointer, T2 defaultValue) { + return pointer.GetWithDefault(document, defaultValue); +} + +template +typename DocumentType::ValueType& GetValueByPointerWithDefault(DocumentType& document, const CharType(&source)[N], const typename DocumentType::ValueType& defaultValue) { + return GenericPointer(source, N - 1).GetWithDefault(document, defaultValue); +} + +template +typename DocumentType::ValueType& GetValueByPointerWithDefault(DocumentType& document, const CharType(&source)[N], const typename DocumentType::Ch* defaultValue) { + return GenericPointer(source, N - 1).GetWithDefault(document, defaultValue); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename DocumentType::ValueType& GetValueByPointerWithDefault(DocumentType& document, const CharType(&source)[N], const std::basic_string& defaultValue) { + return GenericPointer(source, N - 1).GetWithDefault(document, defaultValue); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename DocumentType::ValueType&)) +GetValueByPointerWithDefault(DocumentType& document, const CharType(&source)[N], T2 defaultValue) { + return GenericPointer(source, N - 1).GetWithDefault(document, defaultValue); +} + +////////////////////////////////////////////////////////////////////////////// + +template +typename T::ValueType& SetValueByPointer(T& root, const GenericPointer& pointer, typename T::ValueType& value, typename T::AllocatorType& a) { + return pointer.Set(root, value, a); +} + +template +typename T::ValueType& SetValueByPointer(T& root, const GenericPointer& pointer, const typename T::ValueType& value, typename T::AllocatorType& a) { + return pointer.Set(root, value, a); +} + +template +typename T::ValueType& SetValueByPointer(T& root, const GenericPointer& pointer, const typename T::Ch* value, typename T::AllocatorType& a) { + return pointer.Set(root, value, a); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename T::ValueType& SetValueByPointer(T& root, const GenericPointer& pointer, const std::basic_string& value, typename T::AllocatorType& a) { + return pointer.Set(root, value, a); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename T::ValueType&)) +SetValueByPointer(T& root, const GenericPointer& pointer, T2 value, typename T::AllocatorType& a) { + return pointer.Set(root, value, a); +} + +template +typename T::ValueType& SetValueByPointer(T& root, const CharType(&source)[N], typename T::ValueType& value, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Set(root, value, a); +} + +template +typename T::ValueType& SetValueByPointer(T& root, const CharType(&source)[N], const typename T::ValueType& value, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Set(root, value, a); +} + +template +typename T::ValueType& SetValueByPointer(T& root, const CharType(&source)[N], const typename T::Ch* value, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Set(root, value, a); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename T::ValueType& SetValueByPointer(T& root, const CharType(&source)[N], const std::basic_string& value, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Set(root, value, a); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename T::ValueType&)) +SetValueByPointer(T& root, const CharType(&source)[N], T2 value, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Set(root, value, a); +} + +// No allocator parameter + +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const GenericPointer& pointer, typename DocumentType::ValueType& value) { + return pointer.Set(document, value); +} + +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const GenericPointer& pointer, const typename DocumentType::ValueType& value) { + return pointer.Set(document, value); +} + +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const GenericPointer& pointer, const typename DocumentType::Ch* value) { + return pointer.Set(document, value); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const GenericPointer& pointer, const std::basic_string& value) { + return pointer.Set(document, value); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename DocumentType::ValueType&)) +SetValueByPointer(DocumentType& document, const GenericPointer& pointer, T2 value) { + return pointer.Set(document, value); +} + +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const CharType(&source)[N], typename DocumentType::ValueType& value) { + return GenericPointer(source, N - 1).Set(document, value); +} + +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const CharType(&source)[N], const typename DocumentType::ValueType& value) { + return GenericPointer(source, N - 1).Set(document, value); +} + +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const CharType(&source)[N], const typename DocumentType::Ch* value) { + return GenericPointer(source, N - 1).Set(document, value); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const CharType(&source)[N], const std::basic_string& value) { + return GenericPointer(source, N - 1).Set(document, value); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename DocumentType::ValueType&)) +SetValueByPointer(DocumentType& document, const CharType(&source)[N], T2 value) { + return GenericPointer(source, N - 1).Set(document, value); +} + +////////////////////////////////////////////////////////////////////////////// + +template +typename T::ValueType& SwapValueByPointer(T& root, const GenericPointer& pointer, typename T::ValueType& value, typename T::AllocatorType& a) { + return pointer.Swap(root, value, a); +} + +template +typename T::ValueType& SwapValueByPointer(T& root, const CharType(&source)[N], typename T::ValueType& value, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Swap(root, value, a); +} + +template +typename DocumentType::ValueType& SwapValueByPointer(DocumentType& document, const GenericPointer& pointer, typename DocumentType::ValueType& value) { + return pointer.Swap(document, value); +} + +template +typename DocumentType::ValueType& SwapValueByPointer(DocumentType& document, const CharType(&source)[N], typename DocumentType::ValueType& value) { + return GenericPointer(source, N - 1).Swap(document, value); +} + +////////////////////////////////////////////////////////////////////////////// + +template +bool EraseValueByPointer(T& root, const GenericPointer& pointer) { + return pointer.Erase(root); +} + +template +bool EraseValueByPointer(T& root, const CharType(&source)[N]) { + return GenericPointer(source, N - 1).Erase(root); +} + +//@} + +RAPIDJSON_NAMESPACE_END + +#if defined(__clang__) || defined(_MSC_VER) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_POINTER_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/prettywriter.h b/include/liblvgl/libs/thorvg/rapidjson/prettywriter.h new file mode 100644 index 00000000..cea596ef --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/prettywriter.h @@ -0,0 +1,277 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_PRETTYWRITER_H_ +#define RAPIDJSON_PRETTYWRITER_H_ + +#include "writer.h" + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +#if defined(__clang__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(c++98-compat) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Combination of PrettyWriter format flags. +/*! \see PrettyWriter::SetFormatOptions + */ +enum PrettyFormatOptions { + kFormatDefault = 0, //!< Default pretty formatting. + kFormatSingleLineArray = 1 //!< Format arrays on a single line. +}; + +//! Writer with indentation and spacing. +/*! + \tparam OutputStream Type of output os. + \tparam SourceEncoding Encoding of source string. + \tparam TargetEncoding Encoding of output stream. + \tparam StackAllocator Type of allocator for allocating memory of stack. +*/ +template, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator, unsigned writeFlags = kWriteDefaultFlags> +class PrettyWriter : public Writer { +public: + typedef Writer Base; + typedef typename Base::Ch Ch; + + //! Constructor + /*! \param os Output stream. + \param allocator User supplied allocator. If it is null, it will create a private one. + \param levelDepth Initial capacity of stack. + */ + explicit PrettyWriter(OutputStream& os, StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) : + Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4), formatOptions_(kFormatDefault) {} + + + explicit PrettyWriter(StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) : + Base(allocator, levelDepth), indentChar_(' '), indentCharCount_(4), formatOptions_(kFormatDefault) {} + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + PrettyWriter(PrettyWriter&& rhs) : + Base(std::forward(rhs)), indentChar_(rhs.indentChar_), indentCharCount_(rhs.indentCharCount_), formatOptions_(rhs.formatOptions_) {} +#endif + + //! Set custom indentation. + /*! \param indentChar Character for indentation. Must be whitespace character (' ', '\\t', '\\n', '\\r'). + \param indentCharCount Number of indent characters for each indentation level. + \note The default indentation is 4 spaces. + */ + PrettyWriter& SetIndent(Ch indentChar, unsigned indentCharCount) { + RAPIDJSON_ASSERT(indentChar == ' ' || indentChar == '\t' || indentChar == '\n' || indentChar == '\r'); + indentChar_ = indentChar; + indentCharCount_ = indentCharCount; + return *this; + } + + //! Set pretty writer formatting options. + /*! \param options Formatting options. + */ + PrettyWriter& SetFormatOptions(PrettyFormatOptions options) { + formatOptions_ = options; + return *this; + } + + /*! @name Implementation of Handler + \see Handler + */ + //@{ + + bool Null() { PrettyPrefix(kNullType); return Base::EndValue(Base::WriteNull()); } + bool Bool(bool b) { PrettyPrefix(b ? kTrueType : kFalseType); return Base::EndValue(Base::WriteBool(b)); } + bool Int(int i) { PrettyPrefix(kNumberType); return Base::EndValue(Base::WriteInt(i)); } + bool Uint(unsigned u) { PrettyPrefix(kNumberType); return Base::EndValue(Base::WriteUint(u)); } + bool Int64(int64_t i64) { PrettyPrefix(kNumberType); return Base::EndValue(Base::WriteInt64(i64)); } + bool Uint64(uint64_t u64) { PrettyPrefix(kNumberType); return Base::EndValue(Base::WriteUint64(u64)); } + bool Double(double d) { PrettyPrefix(kNumberType); return Base::EndValue(Base::WriteDouble(d)); } + + bool RawNumber(const Ch* str, SizeType length, bool copy = false) { + RAPIDJSON_ASSERT(str != 0); + (void)copy; + PrettyPrefix(kNumberType); + return Base::EndValue(Base::WriteString(str, length)); + } + + bool String(const Ch* str, SizeType length, bool copy = false) { + RAPIDJSON_ASSERT(str != 0); + (void)copy; + PrettyPrefix(kStringType); + return Base::EndValue(Base::WriteString(str, length)); + } + +#if RAPIDJSON_HAS_STDSTRING + bool String(const std::basic_string& str) { + return String(str.data(), SizeType(str.size())); + } +#endif + + bool StartObject() { + PrettyPrefix(kObjectType); + new (Base::level_stack_.template Push()) typename Base::Level(false); + return Base::WriteStartObject(); + } + + bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); } + +#if RAPIDJSON_HAS_STDSTRING + bool Key(const std::basic_string& str) { + return Key(str.data(), SizeType(str.size())); + } +#endif + + bool EndObject(SizeType memberCount = 0) { + (void)memberCount; + RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level)); // not inside an Object + RAPIDJSON_ASSERT(!Base::level_stack_.template Top()->inArray); // currently inside an Array, not Object + RAPIDJSON_ASSERT(0 == Base::level_stack_.template Top()->valueCount % 2); // Object has a Key without a Value + + bool empty = Base::level_stack_.template Pop(1)->valueCount == 0; + + if (!empty) { + Base::os_->Put('\n'); + WriteIndent(); + } + bool ret = Base::EndValue(Base::WriteEndObject()); + (void)ret; + RAPIDJSON_ASSERT(ret == true); + if (Base::level_stack_.Empty()) // end of json text + Base::Flush(); + return true; + } + + bool StartArray() { + PrettyPrefix(kArrayType); + new (Base::level_stack_.template Push()) typename Base::Level(true); + return Base::WriteStartArray(); + } + + bool EndArray(SizeType memberCount = 0) { + (void)memberCount; + RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level)); + RAPIDJSON_ASSERT(Base::level_stack_.template Top()->inArray); + bool empty = Base::level_stack_.template Pop(1)->valueCount == 0; + + if (!empty && !(formatOptions_ & kFormatSingleLineArray)) { + Base::os_->Put('\n'); + WriteIndent(); + } + bool ret = Base::EndValue(Base::WriteEndArray()); + (void)ret; + RAPIDJSON_ASSERT(ret == true); + if (Base::level_stack_.Empty()) // end of json text + Base::Flush(); + return true; + } + + //@} + + /*! @name Convenience extensions */ + //@{ + + //! Simpler but slower overload. + bool String(const Ch* str) { return String(str, internal::StrLen(str)); } + bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); } + + //@} + + //! Write a raw JSON value. + /*! + For user to write a stringified JSON as a value. + + \param json A well-formed JSON value. It should not contain null character within [0, length - 1] range. + \param length Length of the json. + \param type Type of the root of json. + \note When using PrettyWriter::RawValue(), the result json may not be indented correctly. + */ + bool RawValue(const Ch* json, size_t length, Type type) { + RAPIDJSON_ASSERT(json != 0); + PrettyPrefix(type); + return Base::EndValue(Base::WriteRawValue(json, length)); + } + +protected: + void PrettyPrefix(Type type) { + (void)type; + if (Base::level_stack_.GetSize() != 0) { // this value is not at root + typename Base::Level* level = Base::level_stack_.template Top(); + + if (level->inArray) { + if (level->valueCount > 0) { + Base::os_->Put(','); // add comma if it is not the first element in array + if (formatOptions_ & kFormatSingleLineArray) + Base::os_->Put(' '); + } + + if (!(formatOptions_ & kFormatSingleLineArray)) { + Base::os_->Put('\n'); + WriteIndent(); + } + } + else { // in object + if (level->valueCount > 0) { + if (level->valueCount % 2 == 0) { + Base::os_->Put(','); + Base::os_->Put('\n'); + } + else { + Base::os_->Put(':'); + Base::os_->Put(' '); + } + } + else + Base::os_->Put('\n'); + + if (level->valueCount % 2 == 0) + WriteIndent(); + } + if (!level->inArray && level->valueCount % 2 == 0) + RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even number should be a name + level->valueCount++; + } + else { + RAPIDJSON_ASSERT(!Base::hasRoot_); // Should only has one and only one root. + Base::hasRoot_ = true; + } + } + + void WriteIndent() { + size_t count = (Base::level_stack_.GetSize() / sizeof(typename Base::Level)) * indentCharCount_; + PutN(*Base::os_, static_cast(indentChar_), count); + } + + Ch indentChar_; + unsigned indentCharCount_; + PrettyFormatOptions formatOptions_; + +private: + // Prohibit copy constructor & assignment operator. + PrettyWriter(const PrettyWriter&); + PrettyWriter& operator=(const PrettyWriter&); +}; + +RAPIDJSON_NAMESPACE_END + +#if defined(__clang__) +RAPIDJSON_DIAG_POP +#endif + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_RAPIDJSON_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/rapidjson.h b/include/liblvgl/libs/thorvg/rapidjson/rapidjson.h new file mode 100644 index 00000000..89de8386 --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/rapidjson.h @@ -0,0 +1,741 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_RAPIDJSON_H_ +#define RAPIDJSON_RAPIDJSON_H_ + +/*!\file rapidjson.h + \brief common definitions and configuration + + \see RAPIDJSON_CONFIG + */ + +/*! \defgroup RAPIDJSON_CONFIG RapidJSON configuration + \brief Configuration macros for library features + + Some RapidJSON features are configurable to adapt the library to a wide + variety of platforms, environments and usage scenarios. Most of the + features can be configured in terms of overridden or predefined + preprocessor macros at compile-time. + + Some additional customization is available in the \ref RAPIDJSON_ERRORS APIs. + + \note These macros should be given on the compiler command-line + (where applicable) to avoid inconsistent values when compiling + different translation units of a single application. + */ + +#include // malloc(), realloc(), free(), size_t +#include // memset(), memcpy(), memmove(), memcmp() + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_VERSION_STRING +// +// ALWAYS synchronize the following 3 macros with corresponding variables in /CMakeLists.txt. +// + +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +// token stringification +#define RAPIDJSON_STRINGIFY(x) RAPIDJSON_DO_STRINGIFY(x) +#define RAPIDJSON_DO_STRINGIFY(x) #x + +// token concatenation +#define RAPIDJSON_JOIN(X, Y) RAPIDJSON_DO_JOIN(X, Y) +#define RAPIDJSON_DO_JOIN(X, Y) RAPIDJSON_DO_JOIN2(X, Y) +#define RAPIDJSON_DO_JOIN2(X, Y) X##Y +//!@endcond + +/*! \def RAPIDJSON_MAJOR_VERSION + \ingroup RAPIDJSON_CONFIG + \brief Major version of RapidJSON in integer. +*/ +/*! \def RAPIDJSON_MINOR_VERSION + \ingroup RAPIDJSON_CONFIG + \brief Minor version of RapidJSON in integer. +*/ +/*! \def RAPIDJSON_PATCH_VERSION + \ingroup RAPIDJSON_CONFIG + \brief Patch version of RapidJSON in integer. +*/ +/*! \def RAPIDJSON_VERSION_STRING + \ingroup RAPIDJSON_CONFIG + \brief Version of RapidJSON in ".." string format. +*/ +#define RAPIDJSON_MAJOR_VERSION 1 +#define RAPIDJSON_MINOR_VERSION 1 +#define RAPIDJSON_PATCH_VERSION 0 +#define RAPIDJSON_VERSION_STRING \ + RAPIDJSON_STRINGIFY(RAPIDJSON_MAJOR_VERSION.RAPIDJSON_MINOR_VERSION.RAPIDJSON_PATCH_VERSION) + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_NAMESPACE_(BEGIN|END) +/*! \def RAPIDJSON_NAMESPACE + \ingroup RAPIDJSON_CONFIG + \brief provide custom rapidjson namespace + + In order to avoid symbol clashes and/or "One Definition Rule" errors + between multiple inclusions of (different versions of) RapidJSON in + a single binary, users can customize the name of the main RapidJSON + namespace. + + In case of a single nesting level, defining \c RAPIDJSON_NAMESPACE + to a custom name (e.g. \c MyRapidJSON) is sufficient. If multiple + levels are needed, both \ref RAPIDJSON_NAMESPACE_BEGIN and \ref + RAPIDJSON_NAMESPACE_END need to be defined as well: + + \code + // in some .cpp file + #define RAPIDJSON_NAMESPACE my::rapidjson + #define RAPIDJSON_NAMESPACE_BEGIN namespace my { namespace rapidjson { + #define RAPIDJSON_NAMESPACE_END } } + #include "rapidjson/..." + \endcode + + \see rapidjson + */ +/*! \def RAPIDJSON_NAMESPACE_BEGIN + \ingroup RAPIDJSON_CONFIG + \brief provide custom rapidjson namespace (opening expression) + \see RAPIDJSON_NAMESPACE +*/ +/*! \def RAPIDJSON_NAMESPACE_END + \ingroup RAPIDJSON_CONFIG + \brief provide custom rapidjson namespace (closing expression) + \see RAPIDJSON_NAMESPACE +*/ +#ifndef RAPIDJSON_NAMESPACE +#define RAPIDJSON_NAMESPACE rapidjson +#endif +#ifndef RAPIDJSON_NAMESPACE_BEGIN +#define RAPIDJSON_NAMESPACE_BEGIN namespace RAPIDJSON_NAMESPACE { +#endif +#ifndef RAPIDJSON_NAMESPACE_END +#define RAPIDJSON_NAMESPACE_END } +#endif + +/////////////////////////////////////////////////////////////////////////////// +// __cplusplus macro + +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN + +#if defined(_MSC_VER) +#define RAPIDJSON_CPLUSPLUS _MSVC_LANG +#else +#define RAPIDJSON_CPLUSPLUS __cplusplus +#endif + +//!@endcond + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_HAS_STDSTRING + +#ifndef RAPIDJSON_HAS_STDSTRING +#ifdef RAPIDJSON_DOXYGEN_RUNNING +#define RAPIDJSON_HAS_STDSTRING 1 // force generation of documentation +#else +#define RAPIDJSON_HAS_STDSTRING 0 // no std::string support by default +#endif +/*! \def RAPIDJSON_HAS_STDSTRING + \ingroup RAPIDJSON_CONFIG + \brief Enable RapidJSON support for \c std::string + + By defining this preprocessor symbol to \c 1, several convenience functions for using + \ref rapidjson::GenericValue with \c std::string are enabled, especially + for construction and comparison. + + \hideinitializer +*/ +#endif // !defined(RAPIDJSON_HAS_STDSTRING) + +#if RAPIDJSON_HAS_STDSTRING +#include +#endif // RAPIDJSON_HAS_STDSTRING + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_USE_MEMBERSMAP + +/*! \def RAPIDJSON_USE_MEMBERSMAP + \ingroup RAPIDJSON_CONFIG + \brief Enable RapidJSON support for object members handling in a \c std::multimap + + By defining this preprocessor symbol to \c 1, \ref rapidjson::GenericValue object + members are stored in a \c std::multimap for faster lookup and deletion times, a + trade off with a slightly slower insertion time and a small object allocat(or)ed + memory overhead. + + \hideinitializer +*/ +#ifndef RAPIDJSON_USE_MEMBERSMAP +#define RAPIDJSON_USE_MEMBERSMAP 0 // not by default +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_NO_INT64DEFINE + +/*! \def RAPIDJSON_NO_INT64DEFINE + \ingroup RAPIDJSON_CONFIG + \brief Use external 64-bit integer types. + + RapidJSON requires the 64-bit integer types \c int64_t and \c uint64_t types + to be available at global scope. + + If users have their own definition, define RAPIDJSON_NO_INT64DEFINE to + prevent RapidJSON from defining its own types. +*/ +#ifndef RAPIDJSON_NO_INT64DEFINE +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#if defined(_MSC_VER) && (_MSC_VER < 1800) // Visual Studio 2013 +#include "msinttypes/stdint.h" +#include "msinttypes/inttypes.h" +#else +// Other compilers should have this. +#include +#include +#endif +//!@endcond +#ifdef RAPIDJSON_DOXYGEN_RUNNING +#define RAPIDJSON_NO_INT64DEFINE +#endif +#endif // RAPIDJSON_NO_INT64TYPEDEF + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_FORCEINLINE + +#ifndef RAPIDJSON_FORCEINLINE +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#if defined(_MSC_VER) && defined(NDEBUG) +#define RAPIDJSON_FORCEINLINE __forceinline +#elif defined(__GNUC__) && __GNUC__ >= 4 && defined(NDEBUG) +#define RAPIDJSON_FORCEINLINE __attribute__((always_inline)) +#else +#define RAPIDJSON_FORCEINLINE +#endif +//!@endcond +#endif // RAPIDJSON_FORCEINLINE + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_ENDIAN +#define RAPIDJSON_LITTLEENDIAN 0 //!< Little endian machine +#define RAPIDJSON_BIGENDIAN 1 //!< Big endian machine + +//! Endianness of the machine. +/*! + \def RAPIDJSON_ENDIAN + \ingroup RAPIDJSON_CONFIG + + GCC 4.6 provided macro for detecting endianness of the target machine. But other + compilers may not have this. User can define RAPIDJSON_ENDIAN to either + \ref RAPIDJSON_LITTLEENDIAN or \ref RAPIDJSON_BIGENDIAN. + + Default detection implemented with reference to + \li https://gcc.gnu.org/onlinedocs/gcc-4.6.0/cpp/Common-Predefined-Macros.html + \li http://www.boost.org/doc/libs/1_42_0/boost/detail/endian.hpp +*/ +#ifndef RAPIDJSON_ENDIAN +// Detect with GCC 4.6's macro +# ifdef __BYTE_ORDER__ +# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN +# elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN +# else +# error Unknown machine endianness detected. User needs to define RAPIDJSON_ENDIAN. +# endif // __BYTE_ORDER__ +// Detect with GLIBC's endian.h +# elif defined(__GLIBC__) +# include +# if (__BYTE_ORDER == __LITTLE_ENDIAN) +# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN +# elif (__BYTE_ORDER == __BIG_ENDIAN) +# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN +# else +# error Unknown machine endianness detected. User needs to define RAPIDJSON_ENDIAN. +# endif // __GLIBC__ +// Detect with _LITTLE_ENDIAN and _BIG_ENDIAN macro +# elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN) +# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN +# elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN) +# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN +// Detect with architecture macros +# elif defined(__sparc) || defined(__sparc__) || defined(_POWER) || defined(__powerpc__) || defined(__ppc__) || defined(__hpux) || defined(__hppa) || defined(_MIPSEB) || defined(_POWER) || defined(__s390__) +# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN +# elif defined(__i386__) || defined(__alpha__) || defined(__ia64) || defined(__ia64__) || defined(_M_IX86) || defined(_M_IA64) || defined(_M_ALPHA) || defined(__amd64) || defined(__amd64__) || defined(_M_AMD64) || defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || defined(__bfin__) +# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN +# elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64)) +# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN +# elif defined(RAPIDJSON_DOXYGEN_RUNNING) +# define RAPIDJSON_ENDIAN +# else +# error Unknown machine endianness detected. User needs to define RAPIDJSON_ENDIAN. +# endif +#endif // RAPIDJSON_ENDIAN + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_64BIT + +//! Whether using 64-bit architecture +#ifndef RAPIDJSON_64BIT +#if defined(__LP64__) || (defined(__x86_64__) && defined(__ILP32__)) || defined(_WIN64) || defined(__EMSCRIPTEN__) +#define RAPIDJSON_64BIT 1 +#else +#define RAPIDJSON_64BIT 0 +#endif +#endif // RAPIDJSON_64BIT + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_ALIGN + +//! Data alignment of the machine. +/*! \ingroup RAPIDJSON_CONFIG + \param x pointer to align + + Some machines require strict data alignment. The default is 8 bytes. + User can customize by defining the RAPIDJSON_ALIGN function macro. +*/ +#ifndef RAPIDJSON_ALIGN +#define RAPIDJSON_ALIGN(x) (((x) + static_cast(7u)) & ~static_cast(7u)) +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_UINT64_C2 + +//! Construct a 64-bit literal by a pair of 32-bit integer. +/*! + 64-bit literal with or without ULL suffix is prone to compiler warnings. + UINT64_C() is C macro which cause compilation problems. + Use this macro to define 64-bit constants by a pair of 32-bit integer. +*/ +#ifndef RAPIDJSON_UINT64_C2 +#define RAPIDJSON_UINT64_C2(high32, low32) ((static_cast(high32) << 32) | static_cast(low32)) +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_48BITPOINTER_OPTIMIZATION + +//! Use only lower 48-bit address for some pointers. +/*! + \ingroup RAPIDJSON_CONFIG + + This optimization uses the fact that current X86-64 architecture only implement lower 48-bit virtual address. + The higher 16-bit can be used for storing other data. + \c GenericValue uses this optimization to reduce its size form 24 bytes to 16 bytes in 64-bit architecture. +*/ +#ifndef RAPIDJSON_48BITPOINTER_OPTIMIZATION +#if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64) +#define RAPIDJSON_48BITPOINTER_OPTIMIZATION 1 +#else +#define RAPIDJSON_48BITPOINTER_OPTIMIZATION 0 +#endif +#endif // RAPIDJSON_48BITPOINTER_OPTIMIZATION + +#if RAPIDJSON_48BITPOINTER_OPTIMIZATION == 1 +#if RAPIDJSON_64BIT != 1 +#error RAPIDJSON_48BITPOINTER_OPTIMIZATION can only be set to 1 when RAPIDJSON_64BIT=1 +#endif +#define RAPIDJSON_SETPOINTER(type, p, x) (p = reinterpret_cast((reinterpret_cast(p) & static_cast(RAPIDJSON_UINT64_C2(0xFFFF0000, 0x00000000))) | reinterpret_cast(reinterpret_cast(x)))) +#define RAPIDJSON_GETPOINTER(type, p) (reinterpret_cast(reinterpret_cast(p) & static_cast(RAPIDJSON_UINT64_C2(0x0000FFFF, 0xFFFFFFFF)))) +#else +#define RAPIDJSON_SETPOINTER(type, p, x) (p = (x)) +#define RAPIDJSON_GETPOINTER(type, p) (p) +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_SSE2/RAPIDJSON_SSE42/RAPIDJSON_NEON/RAPIDJSON_SIMD + +/*! \def RAPIDJSON_SIMD + \ingroup RAPIDJSON_CONFIG + \brief Enable SSE2/SSE4.2/Neon optimization. + + RapidJSON supports optimized implementations for some parsing operations + based on the SSE2, SSE4.2 or NEon SIMD extensions on modern Intel + or ARM compatible processors. + + To enable these optimizations, three different symbols can be defined; + \code + // Enable SSE2 optimization. + #define RAPIDJSON_SSE2 + + // Enable SSE4.2 optimization. + #define RAPIDJSON_SSE42 + \endcode + + // Enable ARM Neon optimization. + #define RAPIDJSON_NEON + \endcode + + \c RAPIDJSON_SSE42 takes precedence over SSE2, if both are defined. + + If any of these symbols is defined, RapidJSON defines the macro + \c RAPIDJSON_SIMD to indicate the availability of the optimized code. +*/ +#if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) \ + || defined(RAPIDJSON_NEON) || defined(RAPIDJSON_DOXYGEN_RUNNING) +#define RAPIDJSON_SIMD +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_NO_SIZETYPEDEFINE + +#ifndef RAPIDJSON_NO_SIZETYPEDEFINE +/*! \def RAPIDJSON_NO_SIZETYPEDEFINE + \ingroup RAPIDJSON_CONFIG + \brief User-provided \c SizeType definition. + + In order to avoid using 32-bit size types for indexing strings and arrays, + define this preprocessor symbol and provide the type rapidjson::SizeType + before including RapidJSON: + \code + #define RAPIDJSON_NO_SIZETYPEDEFINE + namespace rapidjson { typedef ::std::size_t SizeType; } + #include "rapidjson/..." + \endcode + + \see rapidjson::SizeType +*/ +#ifdef RAPIDJSON_DOXYGEN_RUNNING +#define RAPIDJSON_NO_SIZETYPEDEFINE +#endif +RAPIDJSON_NAMESPACE_BEGIN +//! Size type (for string lengths, array sizes, etc.) +/*! RapidJSON uses 32-bit array/string indices even on 64-bit platforms, + instead of using \c size_t. Users may override the SizeType by defining + \ref RAPIDJSON_NO_SIZETYPEDEFINE. +*/ +typedef unsigned SizeType; +RAPIDJSON_NAMESPACE_END +#endif + +// always import std::size_t to rapidjson namespace +RAPIDJSON_NAMESPACE_BEGIN +using std::size_t; +RAPIDJSON_NAMESPACE_END + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_ASSERT + +//! Assertion. +/*! \ingroup RAPIDJSON_CONFIG + By default, rapidjson uses C \c assert() for internal assertions. + User can override it by defining RAPIDJSON_ASSERT(x) macro. + + \note Parsing errors are handled and can be customized by the + \ref RAPIDJSON_ERRORS APIs. +*/ +#ifndef RAPIDJSON_ASSERT +#include +#define RAPIDJSON_ASSERT(x) assert(x) +#endif // RAPIDJSON_ASSERT + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_STATIC_ASSERT + +// Prefer C++11 static_assert, if available +#ifndef RAPIDJSON_STATIC_ASSERT +#if RAPIDJSON_CPLUSPLUS >= 201103L || ( defined(_MSC_VER) && _MSC_VER >= 1800 ) +#define RAPIDJSON_STATIC_ASSERT(x) \ + static_assert(x, RAPIDJSON_STRINGIFY(x)) +#endif // C++11 +#endif // RAPIDJSON_STATIC_ASSERT + +// Adopt C++03 implementation from boost +#ifndef RAPIDJSON_STATIC_ASSERT +#ifndef __clang__ +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#endif +RAPIDJSON_NAMESPACE_BEGIN +template struct STATIC_ASSERTION_FAILURE; +template <> struct STATIC_ASSERTION_FAILURE { enum { value = 1 }; }; +template struct StaticAssertTest {}; +RAPIDJSON_NAMESPACE_END + +#if defined(__GNUC__) || defined(__clang__) +#define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE __attribute__((unused)) +#else +#define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE +#endif +#ifndef __clang__ +//!@endcond +#endif + +/*! \def RAPIDJSON_STATIC_ASSERT + \brief (Internal) macro to check for conditions at compile-time + \param x compile-time condition + \hideinitializer + */ +#define RAPIDJSON_STATIC_ASSERT(x) \ + typedef ::RAPIDJSON_NAMESPACE::StaticAssertTest< \ + sizeof(::RAPIDJSON_NAMESPACE::STATIC_ASSERTION_FAILURE)> \ + RAPIDJSON_JOIN(StaticAssertTypedef, __LINE__) RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE +#endif // RAPIDJSON_STATIC_ASSERT + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_LIKELY, RAPIDJSON_UNLIKELY + +//! Compiler branching hint for expression with high probability to be true. +/*! + \ingroup RAPIDJSON_CONFIG + \param x Boolean expression likely to be true. +*/ +#ifndef RAPIDJSON_LIKELY +#if defined(__GNUC__) || defined(__clang__) +#define RAPIDJSON_LIKELY(x) __builtin_expect(!!(x), 1) +#else +#define RAPIDJSON_LIKELY(x) (x) +#endif +#endif + +//! Compiler branching hint for expression with low probability to be true. +/*! + \ingroup RAPIDJSON_CONFIG + \param x Boolean expression unlikely to be true. +*/ +#ifndef RAPIDJSON_UNLIKELY +#if defined(__GNUC__) || defined(__clang__) +#define RAPIDJSON_UNLIKELY(x) __builtin_expect(!!(x), 0) +#else +#define RAPIDJSON_UNLIKELY(x) (x) +#endif +#endif + +/////////////////////////////////////////////////////////////////////////////// +// Helpers + +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN + +#define RAPIDJSON_MULTILINEMACRO_BEGIN do { +#define RAPIDJSON_MULTILINEMACRO_END \ +} while((void)0, 0) + +// adopted from Boost +#define RAPIDJSON_VERSION_CODE(x,y,z) \ + (((x)*100000) + ((y)*100) + (z)) + +#if defined(__has_builtin) +#define RAPIDJSON_HAS_BUILTIN(x) __has_builtin(x) +#else +#define RAPIDJSON_HAS_BUILTIN(x) 0 +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_DIAG_PUSH/POP, RAPIDJSON_DIAG_OFF + +#if defined(__GNUC__) +#define RAPIDJSON_GNUC \ + RAPIDJSON_VERSION_CODE(__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__) +#endif + +#if defined(__clang__) || (defined(RAPIDJSON_GNUC) && RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,2,0)) + +#define RAPIDJSON_PRAGMA(x) _Pragma(RAPIDJSON_STRINGIFY(x)) +#define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(GCC diagnostic x) +#define RAPIDJSON_DIAG_OFF(x) \ + RAPIDJSON_DIAG_PRAGMA(ignored RAPIDJSON_STRINGIFY(RAPIDJSON_JOIN(-W,x))) + +// push/pop support in Clang and GCC>=4.6 +#if defined(__clang__) || (defined(RAPIDJSON_GNUC) && RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,6,0)) +#define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push) +#define RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_PRAGMA(pop) +#else // GCC >= 4.2, < 4.6 +#define RAPIDJSON_DIAG_PUSH /* ignored */ +#define RAPIDJSON_DIAG_POP /* ignored */ +#endif + +#elif defined(_MSC_VER) + +// pragma (MSVC specific) +#define RAPIDJSON_PRAGMA(x) __pragma(x) +#define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(warning(x)) + +#define RAPIDJSON_DIAG_OFF(x) RAPIDJSON_DIAG_PRAGMA(disable: x) +#define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push) +#define RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_PRAGMA(pop) + +#else + +#define RAPIDJSON_DIAG_OFF(x) /* ignored */ +#define RAPIDJSON_DIAG_PUSH /* ignored */ +#define RAPIDJSON_DIAG_POP /* ignored */ + +#endif // RAPIDJSON_DIAG_* + +/////////////////////////////////////////////////////////////////////////////// +// C++11 features + +#ifndef RAPIDJSON_HAS_CXX11 +#define RAPIDJSON_HAS_CXX11 (RAPIDJSON_CPLUSPLUS >= 201103L) +#endif + +#ifndef RAPIDJSON_HAS_CXX11_RVALUE_REFS +#if RAPIDJSON_HAS_CXX11 +#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 1 +#elif defined(__clang__) +#if __has_feature(cxx_rvalue_references) && \ + (defined(_MSC_VER) || defined(_LIBCPP_VERSION) || defined(__GLIBCXX__) && __GLIBCXX__ >= 20080306) +#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 1 +#else +#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 0 +#endif +#elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,3,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \ + (defined(_MSC_VER) && _MSC_VER >= 1600) || \ + (defined(__SUNPRO_CC) && __SUNPRO_CC >= 0x5140 && defined(__GXX_EXPERIMENTAL_CXX0X__)) + +#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 1 +#else +#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 0 +#endif +#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS +#include // std::move +#endif + +#ifndef RAPIDJSON_HAS_CXX11_NOEXCEPT +#if RAPIDJSON_HAS_CXX11 +#define RAPIDJSON_HAS_CXX11_NOEXCEPT 1 +#elif defined(__clang__) +#define RAPIDJSON_HAS_CXX11_NOEXCEPT __has_feature(cxx_noexcept) +#elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,6,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \ + (defined(_MSC_VER) && _MSC_VER >= 1900) || \ + (defined(__SUNPRO_CC) && __SUNPRO_CC >= 0x5140 && defined(__GXX_EXPERIMENTAL_CXX0X__)) +#define RAPIDJSON_HAS_CXX11_NOEXCEPT 1 +#else +#define RAPIDJSON_HAS_CXX11_NOEXCEPT 0 +#endif +#endif +#ifndef RAPIDJSON_NOEXCEPT +#if RAPIDJSON_HAS_CXX11_NOEXCEPT +#define RAPIDJSON_NOEXCEPT noexcept +#else +#define RAPIDJSON_NOEXCEPT throw() +#endif // RAPIDJSON_HAS_CXX11_NOEXCEPT +#endif + +// no automatic detection, yet +#ifndef RAPIDJSON_HAS_CXX11_TYPETRAITS +#if (defined(_MSC_VER) && _MSC_VER >= 1700) +#define RAPIDJSON_HAS_CXX11_TYPETRAITS 1 +#else +#define RAPIDJSON_HAS_CXX11_TYPETRAITS 0 +#endif +#endif + +#ifndef RAPIDJSON_HAS_CXX11_RANGE_FOR +#if defined(__clang__) +#define RAPIDJSON_HAS_CXX11_RANGE_FOR __has_feature(cxx_range_for) +#elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,6,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \ + (defined(_MSC_VER) && _MSC_VER >= 1700) || \ + (defined(__SUNPRO_CC) && __SUNPRO_CC >= 0x5140 && defined(__GXX_EXPERIMENTAL_CXX0X__)) +#define RAPIDJSON_HAS_CXX11_RANGE_FOR 1 +#else +#define RAPIDJSON_HAS_CXX11_RANGE_FOR 0 +#endif +#endif // RAPIDJSON_HAS_CXX11_RANGE_FOR + +/////////////////////////////////////////////////////////////////////////////// +// C++17 features + +#ifndef RAPIDJSON_HAS_CXX17 +#define RAPIDJSON_HAS_CXX17 (RAPIDJSON_CPLUSPLUS >= 201703L) +#endif + +#if RAPIDJSON_HAS_CXX17 +# define RAPIDJSON_DELIBERATE_FALLTHROUGH [[fallthrough]] +#elif defined(__has_cpp_attribute) +# if __has_cpp_attribute(clang::fallthrough) +# define RAPIDJSON_DELIBERATE_FALLTHROUGH [[clang::fallthrough]] +# elif __has_cpp_attribute(fallthrough) +# define RAPIDJSON_DELIBERATE_FALLTHROUGH __attribute__((fallthrough)) +# else +# define RAPIDJSON_DELIBERATE_FALLTHROUGH +# endif +#else +# define RAPIDJSON_DELIBERATE_FALLTHROUGH +#endif + +//!@endcond + +//! Assertion (in non-throwing contexts). + /*! \ingroup RAPIDJSON_CONFIG + Some functions provide a \c noexcept guarantee, if the compiler supports it. + In these cases, the \ref RAPIDJSON_ASSERT macro cannot be overridden to + throw an exception. This macro adds a separate customization point for + such cases. + + Defaults to C \c assert() (as \ref RAPIDJSON_ASSERT), if \c noexcept is + supported, and to \ref RAPIDJSON_ASSERT otherwise. + */ + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_NOEXCEPT_ASSERT + +#ifndef RAPIDJSON_NOEXCEPT_ASSERT +#ifdef RAPIDJSON_ASSERT_THROWS +#include +#define RAPIDJSON_NOEXCEPT_ASSERT(x) assert(x) +#else +#define RAPIDJSON_NOEXCEPT_ASSERT(x) RAPIDJSON_ASSERT(x) +#endif // RAPIDJSON_ASSERT_THROWS +#endif // RAPIDJSON_NOEXCEPT_ASSERT + +/////////////////////////////////////////////////////////////////////////////// +// malloc/realloc/free + +#ifndef RAPIDJSON_MALLOC +///! customization point for global \c malloc +#define RAPIDJSON_MALLOC(size) std::malloc(size) +#endif +#ifndef RAPIDJSON_REALLOC +///! customization point for global \c realloc +#define RAPIDJSON_REALLOC(ptr, new_size) std::realloc(ptr, new_size) +#endif +#ifndef RAPIDJSON_FREE +///! customization point for global \c free +#define RAPIDJSON_FREE(ptr) std::free(ptr) +#endif + +/////////////////////////////////////////////////////////////////////////////// +// new/delete + +#ifndef RAPIDJSON_NEW +///! customization point for global \c new +#define RAPIDJSON_NEW(TypeName) new TypeName +#endif +#ifndef RAPIDJSON_DELETE +///! customization point for global \c delete +#define RAPIDJSON_DELETE(x) delete x +#endif + +/////////////////////////////////////////////////////////////////////////////// +// Type + +/*! \namespace rapidjson + \brief main RapidJSON namespace + \see RAPIDJSON_NAMESPACE +*/ +RAPIDJSON_NAMESPACE_BEGIN + +//! Type of JSON value +enum Type { + kNullType = 0, //!< null + kFalseType = 1, //!< false + kTrueType = 2, //!< true + kObjectType = 3, //!< object + kArrayType = 4, //!< array + kStringType = 5, //!< string + kNumberType = 6 //!< number +}; + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_RAPIDJSON_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/reader.h b/include/liblvgl/libs/thorvg/rapidjson/reader.h new file mode 100644 index 00000000..86c1f28e --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/reader.h @@ -0,0 +1,2246 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_READER_H_ +#define RAPIDJSON_READER_H_ + +/*! \file reader.h */ + +#include "allocators.h" +#include "stream.h" +#include "encodedstream.h" +#include "internal/clzll.h" +#include "internal/meta.h" +#include "internal/stack.h" +#include "internal/strtod.h" +#include + +#if defined(RAPIDJSON_SIMD) && defined(_MSC_VER) +#include +#pragma intrinsic(_BitScanForward) +#endif +#ifdef RAPIDJSON_SSE42 +#include +#elif defined(RAPIDJSON_SSE2) +#include +#elif defined(RAPIDJSON_NEON) +#include +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(old-style-cast) +RAPIDJSON_DIAG_OFF(padded) +RAPIDJSON_DIAG_OFF(switch-enum) +#elif defined(_MSC_VER) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant +RAPIDJSON_DIAG_OFF(4702) // unreachable code +#endif + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#define RAPIDJSON_NOTHING /* deliberately empty */ +#ifndef RAPIDJSON_PARSE_ERROR_EARLY_RETURN +#define RAPIDJSON_PARSE_ERROR_EARLY_RETURN(value) \ + RAPIDJSON_MULTILINEMACRO_BEGIN \ + if (RAPIDJSON_UNLIKELY(HasParseError())) { return value; } \ + RAPIDJSON_MULTILINEMACRO_END +#endif +#define RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID \ + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(RAPIDJSON_NOTHING) +//!@endcond + +/*! \def RAPIDJSON_PARSE_ERROR_NORETURN + \ingroup RAPIDJSON_ERRORS + \brief Macro to indicate a parse error. + \param parseErrorCode \ref rapidjson::ParseErrorCode of the error + \param offset position of the error in JSON input (\c size_t) + + This macros can be used as a customization point for the internal + error handling mechanism of RapidJSON. + + A common usage model is to throw an exception instead of requiring the + caller to explicitly check the \ref rapidjson::GenericReader::Parse's + return value: + + \code + #define RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode,offset) \ + throw ParseException(parseErrorCode, #parseErrorCode, offset) + + #include // std::runtime_error + #include "rapidjson/error/error.h" // rapidjson::ParseResult + + struct ParseException : std::runtime_error, rapidjson::ParseResult { + ParseException(rapidjson::ParseErrorCode code, const char* msg, size_t offset) + : std::runtime_error(msg), ParseResult(code, offset) {} + }; + + #include "rapidjson/reader.h" + \endcode + + \see RAPIDJSON_PARSE_ERROR, rapidjson::GenericReader::Parse + */ +#ifndef RAPIDJSON_PARSE_ERROR_NORETURN +#define RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode, offset) \ + RAPIDJSON_MULTILINEMACRO_BEGIN \ + RAPIDJSON_ASSERT(!HasParseError()); /* Error can only be assigned once */ \ + SetParseError(parseErrorCode, offset); \ + RAPIDJSON_MULTILINEMACRO_END +#endif + +/*! \def RAPIDJSON_PARSE_ERROR + \ingroup RAPIDJSON_ERRORS + \brief (Internal) macro to indicate and handle a parse error. + \param parseErrorCode \ref rapidjson::ParseErrorCode of the error + \param offset position of the error in JSON input (\c size_t) + + Invokes RAPIDJSON_PARSE_ERROR_NORETURN and stops the parsing. + + \see RAPIDJSON_PARSE_ERROR_NORETURN + \hideinitializer + */ +#ifndef RAPIDJSON_PARSE_ERROR +#define RAPIDJSON_PARSE_ERROR(parseErrorCode, offset) \ + RAPIDJSON_MULTILINEMACRO_BEGIN \ + RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode, offset); \ + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; \ + RAPIDJSON_MULTILINEMACRO_END +#endif + +#include "error/error.h" // ParseErrorCode, ParseResult + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// ParseFlag + +/*! \def RAPIDJSON_PARSE_DEFAULT_FLAGS + \ingroup RAPIDJSON_CONFIG + \brief User-defined kParseDefaultFlags definition. + + User can define this as any \c ParseFlag combinations. +*/ +#ifndef RAPIDJSON_PARSE_DEFAULT_FLAGS +#define RAPIDJSON_PARSE_DEFAULT_FLAGS kParseNoFlags +#endif + +//! Combination of parseFlags +/*! \see Reader::Parse, Document::Parse, Document::ParseInsitu, Document::ParseStream + */ +enum ParseFlag { + kParseNoFlags = 0, //!< No flags are set. + kParseInsituFlag = 1, //!< In-situ(destructive) parsing. + kParseValidateEncodingFlag = 2, //!< Validate encoding of JSON strings. + kParseIterativeFlag = 4, //!< Iterative(constant complexity in terms of function call stack size) parsing. + kParseStopWhenDoneFlag = 8, //!< After parsing a complete JSON root from stream, stop further processing the rest of stream. When this flag is used, parser will not generate kParseErrorDocumentRootNotSingular error. + kParseFullPrecisionFlag = 16, //!< Parse number in full precision (but slower). + kParseCommentsFlag = 32, //!< Allow one-line (//) and multi-line (/**/) comments. + kParseNumbersAsStringsFlag = 64, //!< Parse all numbers (ints/doubles) as strings. + kParseTrailingCommasFlag = 128, //!< Allow trailing commas at the end of objects and arrays. + kParseNanAndInfFlag = 256, //!< Allow parsing NaN, Inf, Infinity, -Inf and -Infinity as doubles. + kParseEscapedApostropheFlag = 512, //!< Allow escaped apostrophe in strings. + kParseDefaultFlags = RAPIDJSON_PARSE_DEFAULT_FLAGS //!< Default parse flags. Can be customized by defining RAPIDJSON_PARSE_DEFAULT_FLAGS +}; + +/////////////////////////////////////////////////////////////////////////////// +// Handler + +/*! \class rapidjson::Handler + \brief Concept for receiving events from GenericReader upon parsing. + The functions return true if no error occurs. If they return false, + the event publisher should terminate the process. +\code +concept Handler { + typename Ch; + + bool Null(); + bool Bool(bool b); + bool Int(int i); + bool Uint(unsigned i); + bool Int64(int64_t i); + bool Uint64(uint64_t i); + bool Double(double d); + /// enabled via kParseNumbersAsStringsFlag, string is not null-terminated (use length) + bool RawNumber(const Ch* str, SizeType length, bool copy); + bool String(const Ch* str, SizeType length, bool copy); + bool StartObject(); + bool Key(const Ch* str, SizeType length, bool copy); + bool EndObject(SizeType memberCount); + bool StartArray(); + bool EndArray(SizeType elementCount); +}; +\endcode +*/ +/////////////////////////////////////////////////////////////////////////////// +// BaseReaderHandler + +//! Default implementation of Handler. +/*! This can be used as base class of any reader handler. + \note implements Handler concept +*/ +template, typename Derived = void> +struct BaseReaderHandler { + typedef typename Encoding::Ch Ch; + + typedef typename internal::SelectIf, BaseReaderHandler, Derived>::Type Override; + + bool Default() { return true; } + bool Null() { return static_cast(*this).Default(); } + bool Bool(bool) { return static_cast(*this).Default(); } + bool Int(int) { return static_cast(*this).Default(); } + bool Uint(unsigned) { return static_cast(*this).Default(); } + bool Int64(int64_t) { return static_cast(*this).Default(); } + bool Uint64(uint64_t) { return static_cast(*this).Default(); } + bool Double(double) { return static_cast(*this).Default(); } + /// enabled via kParseNumbersAsStringsFlag, string is not null-terminated (use length) + bool RawNumber(const Ch* str, SizeType len, bool copy) { return static_cast(*this).String(str, len, copy); } + bool String(const Ch*, SizeType, bool) { return static_cast(*this).Default(); } + bool StartObject() { return static_cast(*this).Default(); } + bool Key(const Ch* str, SizeType len, bool copy) { return static_cast(*this).String(str, len, copy); } + bool EndObject(SizeType) { return static_cast(*this).Default(); } + bool StartArray() { return static_cast(*this).Default(); } + bool EndArray(SizeType) { return static_cast(*this).Default(); } +}; + +/////////////////////////////////////////////////////////////////////////////// +// StreamLocalCopy + +namespace internal { + +template::copyOptimization> +class StreamLocalCopy; + +//! Do copy optimization. +template +class StreamLocalCopy { +public: + StreamLocalCopy(Stream& original) : s(original), original_(original) {} + ~StreamLocalCopy() { original_ = s; } + + Stream s; + +private: + StreamLocalCopy& operator=(const StreamLocalCopy&) /* = delete */; + + Stream& original_; +}; + +//! Keep reference. +template +class StreamLocalCopy { +public: + StreamLocalCopy(Stream& original) : s(original) {} + + Stream& s; + +private: + StreamLocalCopy& operator=(const StreamLocalCopy&) /* = delete */; +}; + +} // namespace internal + +/////////////////////////////////////////////////////////////////////////////// +// SkipWhitespace + +//! Skip the JSON white spaces in a stream. +/*! \param is A input stream for skipping white spaces. + \note This function has SSE2/SSE4.2 specialization. +*/ +template +void SkipWhitespace(InputStream& is) { + internal::StreamLocalCopy copy(is); + InputStream& s(copy.s); + + typename InputStream::Ch c; + while ((c = s.Peek()) == ' ' || c == '\n' || c == '\r' || c == '\t') + s.Take(); +} + +inline const char* SkipWhitespace(const char* p, const char* end) { + while (p != end && (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t')) + ++p; + return p; +} + +#ifdef RAPIDJSON_SSE42 +//! Skip whitespace with SSE 4.2 pcmpistrm instruction, testing 16 8-byte characters at once. +inline const char *SkipWhitespace_SIMD(const char* p) { + // Fast return for single non-whitespace + if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') + ++p; + else + return p; + + // 16-byte align to the next boundary + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + while (p != nextAligned) + if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') + ++p; + else + return p; + + // The rest of string using SIMD + static const char whitespace[16] = " \n\r\t"; + const __m128i w = _mm_loadu_si128(reinterpret_cast(&whitespace[0])); + + for (;; p += 16) { + const __m128i s = _mm_load_si128(reinterpret_cast(p)); + const int r = _mm_cmpistri(w, s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT | _SIDD_NEGATIVE_POLARITY); + if (r != 16) // some of characters is non-whitespace + return p + r; + } +} + +inline const char *SkipWhitespace_SIMD(const char* p, const char* end) { + // Fast return for single non-whitespace + if (p != end && (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t')) + ++p; + else + return p; + + // The middle of string using SIMD + static const char whitespace[16] = " \n\r\t"; + const __m128i w = _mm_loadu_si128(reinterpret_cast(&whitespace[0])); + + for (; p <= end - 16; p += 16) { + const __m128i s = _mm_loadu_si128(reinterpret_cast(p)); + const int r = _mm_cmpistri(w, s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT | _SIDD_NEGATIVE_POLARITY); + if (r != 16) // some of characters is non-whitespace + return p + r; + } + + return SkipWhitespace(p, end); +} + +#elif defined(RAPIDJSON_SSE2) + +//! Skip whitespace with SSE2 instructions, testing 16 8-byte characters at once. +inline const char *SkipWhitespace_SIMD(const char* p) { + // Fast return for single non-whitespace + if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') + ++p; + else + return p; + + // 16-byte align to the next boundary + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + while (p != nextAligned) + if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') + ++p; + else + return p; + + // The rest of string + #define C16(c) { c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c } + static const char whitespaces[4][16] = { C16(' '), C16('\n'), C16('\r'), C16('\t') }; + #undef C16 + + const __m128i w0 = _mm_loadu_si128(reinterpret_cast(&whitespaces[0][0])); + const __m128i w1 = _mm_loadu_si128(reinterpret_cast(&whitespaces[1][0])); + const __m128i w2 = _mm_loadu_si128(reinterpret_cast(&whitespaces[2][0])); + const __m128i w3 = _mm_loadu_si128(reinterpret_cast(&whitespaces[3][0])); + + for (;; p += 16) { + const __m128i s = _mm_load_si128(reinterpret_cast(p)); + __m128i x = _mm_cmpeq_epi8(s, w0); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w1)); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w2)); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w3)); + unsigned short r = static_cast(~_mm_movemask_epi8(x)); + if (r != 0) { // some of characters may be non-whitespace +#ifdef _MSC_VER // Find the index of first non-whitespace + unsigned long offset; + _BitScanForward(&offset, r); + return p + offset; +#else + return p + __builtin_ffs(r) - 1; +#endif + } + } +} + +inline const char *SkipWhitespace_SIMD(const char* p, const char* end) { + // Fast return for single non-whitespace + if (p != end && (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t')) + ++p; + else + return p; + + // The rest of string + #define C16(c) { c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c } + static const char whitespaces[4][16] = { C16(' '), C16('\n'), C16('\r'), C16('\t') }; + #undef C16 + + const __m128i w0 = _mm_loadu_si128(reinterpret_cast(&whitespaces[0][0])); + const __m128i w1 = _mm_loadu_si128(reinterpret_cast(&whitespaces[1][0])); + const __m128i w2 = _mm_loadu_si128(reinterpret_cast(&whitespaces[2][0])); + const __m128i w3 = _mm_loadu_si128(reinterpret_cast(&whitespaces[3][0])); + + for (; p <= end - 16; p += 16) { + const __m128i s = _mm_loadu_si128(reinterpret_cast(p)); + __m128i x = _mm_cmpeq_epi8(s, w0); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w1)); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w2)); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w3)); + unsigned short r = static_cast(~_mm_movemask_epi8(x)); + if (r != 0) { // some of characters may be non-whitespace +#ifdef _MSC_VER // Find the index of first non-whitespace + unsigned long offset; + _BitScanForward(&offset, r); + return p + offset; +#else + return p + __builtin_ffs(r) - 1; +#endif + } + } + + return SkipWhitespace(p, end); +} + +#elif defined(RAPIDJSON_NEON) + +//! Skip whitespace with ARM Neon instructions, testing 16 8-byte characters at once. +inline const char *SkipWhitespace_SIMD(const char* p) { + // Fast return for single non-whitespace + if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') + ++p; + else + return p; + + // 16-byte align to the next boundary + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + while (p != nextAligned) + if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') + ++p; + else + return p; + + const uint8x16_t w0 = vmovq_n_u8(' '); + const uint8x16_t w1 = vmovq_n_u8('\n'); + const uint8x16_t w2 = vmovq_n_u8('\r'); + const uint8x16_t w3 = vmovq_n_u8('\t'); + + for (;; p += 16) { + const uint8x16_t s = vld1q_u8(reinterpret_cast(p)); + uint8x16_t x = vceqq_u8(s, w0); + x = vorrq_u8(x, vceqq_u8(s, w1)); + x = vorrq_u8(x, vceqq_u8(s, w2)); + x = vorrq_u8(x, vceqq_u8(s, w3)); + + x = vmvnq_u8(x); // Negate + x = vrev64q_u8(x); // Rev in 64 + uint64_t low = vgetq_lane_u64(vreinterpretq_u64_u8(x), 0); // extract + uint64_t high = vgetq_lane_u64(vreinterpretq_u64_u8(x), 1); // extract + + if (low == 0) { + if (high != 0) { + uint32_t lz = internal::clzll(high); + return p + 8 + (lz >> 3); + } + } else { + uint32_t lz = internal::clzll(low); + return p + (lz >> 3); + } + } +} + +inline const char *SkipWhitespace_SIMD(const char* p, const char* end) { + // Fast return for single non-whitespace + if (p != end && (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t')) + ++p; + else + return p; + + const uint8x16_t w0 = vmovq_n_u8(' '); + const uint8x16_t w1 = vmovq_n_u8('\n'); + const uint8x16_t w2 = vmovq_n_u8('\r'); + const uint8x16_t w3 = vmovq_n_u8('\t'); + + for (; p <= end - 16; p += 16) { + const uint8x16_t s = vld1q_u8(reinterpret_cast(p)); + uint8x16_t x = vceqq_u8(s, w0); + x = vorrq_u8(x, vceqq_u8(s, w1)); + x = vorrq_u8(x, vceqq_u8(s, w2)); + x = vorrq_u8(x, vceqq_u8(s, w3)); + + x = vmvnq_u8(x); // Negate + x = vrev64q_u8(x); // Rev in 64 + uint64_t low = vgetq_lane_u64(vreinterpretq_u64_u8(x), 0); // extract + uint64_t high = vgetq_lane_u64(vreinterpretq_u64_u8(x), 1); // extract + + if (low == 0) { + if (high != 0) { + uint32_t lz = internal::clzll(high); + return p + 8 + (lz >> 3); + } + } else { + uint32_t lz = internal::clzll(low); + return p + (lz >> 3); + } + } + + return SkipWhitespace(p, end); +} + +#endif // RAPIDJSON_NEON + +#ifdef RAPIDJSON_SIMD +//! Template function specialization for InsituStringStream +template<> inline void SkipWhitespace(InsituStringStream& is) { + is.src_ = const_cast(SkipWhitespace_SIMD(is.src_)); +} + +//! Template function specialization for StringStream +template<> inline void SkipWhitespace(StringStream& is) { + is.src_ = SkipWhitespace_SIMD(is.src_); +} + +template<> inline void SkipWhitespace(EncodedInputStream, MemoryStream>& is) { + is.is_.src_ = SkipWhitespace_SIMD(is.is_.src_, is.is_.end_); +} +#endif // RAPIDJSON_SIMD + +/////////////////////////////////////////////////////////////////////////////// +// GenericReader + +//! SAX-style JSON parser. Use \ref Reader for UTF8 encoding and default allocator. +/*! GenericReader parses JSON text from a stream, and send events synchronously to an + object implementing Handler concept. + + It needs to allocate a stack for storing a single decoded string during + non-destructive parsing. + + For in-situ parsing, the decoded string is directly written to the source + text string, no temporary buffer is required. + + A GenericReader object can be reused for parsing multiple JSON text. + + \tparam SourceEncoding Encoding of the input stream. + \tparam TargetEncoding Encoding of the parse output. + \tparam StackAllocator Allocator type for stack. +*/ +template +class GenericReader { +public: + typedef typename SourceEncoding::Ch Ch; //!< SourceEncoding character type + + //! Constructor. + /*! \param stackAllocator Optional allocator for allocating stack memory. (Only use for non-destructive parsing) + \param stackCapacity stack capacity in bytes for storing a single decoded string. (Only use for non-destructive parsing) + */ + GenericReader(StackAllocator* stackAllocator = 0, size_t stackCapacity = kDefaultStackCapacity) : + stack_(stackAllocator, stackCapacity), parseResult_(), state_(IterativeParsingStartState) {} + + //! Parse JSON text. + /*! \tparam parseFlags Combination of \ref ParseFlag. + \tparam InputStream Type of input stream, implementing Stream concept. + \tparam Handler Type of handler, implementing Handler concept. + \param is Input stream to be parsed. + \param handler The handler to receive events. + \return Whether the parsing is successful. + */ + template + ParseResult Parse(InputStream& is, Handler& handler) { + if (parseFlags & kParseIterativeFlag) + return IterativeParse(is, handler); + + parseResult_.Clear(); + + ClearStackOnExit scope(*this); + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + + if (RAPIDJSON_UNLIKELY(is.Peek() == '\0')) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorDocumentEmpty, is.Tell()); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + } + else { + ParseValue(is, handler); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + + if (!(parseFlags & kParseStopWhenDoneFlag)) { + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + + if (RAPIDJSON_UNLIKELY(is.Peek() != '\0')) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorDocumentRootNotSingular, is.Tell()); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + } + } + } + + return parseResult_; + } + + //! Parse JSON text (with \ref kParseDefaultFlags) + /*! \tparam InputStream Type of input stream, implementing Stream concept + \tparam Handler Type of handler, implementing Handler concept. + \param is Input stream to be parsed. + \param handler The handler to receive events. + \return Whether the parsing is successful. + */ + template + ParseResult Parse(InputStream& is, Handler& handler) { + return Parse(is, handler); + } + + //! Initialize JSON text token-by-token parsing + /*! + */ + void IterativeParseInit() { + parseResult_.Clear(); + state_ = IterativeParsingStartState; + } + + //! Parse one token from JSON text + /*! \tparam InputStream Type of input stream, implementing Stream concept + \tparam Handler Type of handler, implementing Handler concept. + \param is Input stream to be parsed. + \param handler The handler to receive events. + \return Whether the parsing is successful. + */ + template + bool IterativeParseNext(InputStream& is, Handler& handler) { + while (RAPIDJSON_LIKELY(is.Peek() != '\0')) { + SkipWhitespaceAndComments(is); + + Token t = Tokenize(is.Peek()); + IterativeParsingState n = Predict(state_, t); + IterativeParsingState d = Transit(state_, t, n, is, handler); + + // If we've finished or hit an error... + if (RAPIDJSON_UNLIKELY(IsIterativeParsingCompleteState(d))) { + // Report errors. + if (d == IterativeParsingErrorState) { + HandleError(state_, is); + return false; + } + + // Transition to the finish state. + RAPIDJSON_ASSERT(d == IterativeParsingFinishState); + state_ = d; + + // If StopWhenDone is not set... + if (!(parseFlags & kParseStopWhenDoneFlag)) { + // ... and extra non-whitespace data is found... + SkipWhitespaceAndComments(is); + if (is.Peek() != '\0') { + // ... this is considered an error. + HandleError(state_, is); + return false; + } + } + + // Success! We are done! + return true; + } + + // Transition to the new state. + state_ = d; + + // If we parsed anything other than a delimiter, we invoked the handler, so we can return true now. + if (!IsIterativeParsingDelimiterState(n)) + return true; + } + + // We reached the end of file. + stack_.Clear(); + + if (state_ != IterativeParsingFinishState) { + HandleError(state_, is); + return false; + } + + return true; + } + + //! Check if token-by-token parsing JSON text is complete + /*! \return Whether the JSON has been fully decoded. + */ + RAPIDJSON_FORCEINLINE bool IterativeParseComplete() const { + return IsIterativeParsingCompleteState(state_); + } + + //! Whether a parse error has occurred in the last parsing. + bool HasParseError() const { return parseResult_.IsError(); } + + //! Get the \ref ParseErrorCode of last parsing. + ParseErrorCode GetParseErrorCode() const { return parseResult_.Code(); } + + //! Get the position of last parsing error in input, 0 otherwise. + size_t GetErrorOffset() const { return parseResult_.Offset(); } + +protected: + void SetParseError(ParseErrorCode code, size_t offset) { parseResult_.Set(code, offset); } + +private: + // Prohibit copy constructor & assignment operator. + GenericReader(const GenericReader&); + GenericReader& operator=(const GenericReader&); + + void ClearStack() { stack_.Clear(); } + + // clear stack on any exit from ParseStream, e.g. due to exception + struct ClearStackOnExit { + explicit ClearStackOnExit(GenericReader& r) : r_(r) {} + ~ClearStackOnExit() { r_.ClearStack(); } + private: + GenericReader& r_; + ClearStackOnExit(const ClearStackOnExit&); + ClearStackOnExit& operator=(const ClearStackOnExit&); + }; + + template + void SkipWhitespaceAndComments(InputStream& is) { + SkipWhitespace(is); + + if (parseFlags & kParseCommentsFlag) { + while (RAPIDJSON_UNLIKELY(Consume(is, '/'))) { + if (Consume(is, '*')) { + while (true) { + if (RAPIDJSON_UNLIKELY(is.Peek() == '\0')) + RAPIDJSON_PARSE_ERROR(kParseErrorUnspecificSyntaxError, is.Tell()); + else if (Consume(is, '*')) { + if (Consume(is, '/')) + break; + } + else + is.Take(); + } + } + else if (RAPIDJSON_LIKELY(Consume(is, '/'))) + while (is.Peek() != '\0' && is.Take() != '\n') {} + else + RAPIDJSON_PARSE_ERROR(kParseErrorUnspecificSyntaxError, is.Tell()); + + SkipWhitespace(is); + } + } + } + + // Parse object: { string : value, ... } + template + void ParseObject(InputStream& is, Handler& handler) { + RAPIDJSON_ASSERT(is.Peek() == '{'); + is.Take(); // Skip '{' + + if (RAPIDJSON_UNLIKELY(!handler.StartObject())) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + if (Consume(is, '}')) { + if (RAPIDJSON_UNLIKELY(!handler.EndObject(0))) // empty object + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + return; + } + + for (SizeType memberCount = 0;;) { + if (RAPIDJSON_UNLIKELY(is.Peek() != '"')) + RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissName, is.Tell()); + + ParseString(is, handler, true); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + if (RAPIDJSON_UNLIKELY(!Consume(is, ':'))) + RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissColon, is.Tell()); + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + ParseValue(is, handler); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + ++memberCount; + + switch (is.Peek()) { + case ',': + is.Take(); + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + break; + case '}': + is.Take(); + if (RAPIDJSON_UNLIKELY(!handler.EndObject(memberCount))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + return; + default: + RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, is.Tell()); break; // This useless break is only for making warning and coverage happy + } + + if (parseFlags & kParseTrailingCommasFlag) { + if (is.Peek() == '}') { + if (RAPIDJSON_UNLIKELY(!handler.EndObject(memberCount))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + is.Take(); + return; + } + } + } + } + + // Parse array: [ value, ... ] + template + void ParseArray(InputStream& is, Handler& handler) { + RAPIDJSON_ASSERT(is.Peek() == '['); + is.Take(); // Skip '[' + + if (RAPIDJSON_UNLIKELY(!handler.StartArray())) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + if (Consume(is, ']')) { + if (RAPIDJSON_UNLIKELY(!handler.EndArray(0))) // empty array + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + return; + } + + for (SizeType elementCount = 0;;) { + ParseValue(is, handler); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + ++elementCount; + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + if (Consume(is, ',')) { + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + } + else if (Consume(is, ']')) { + if (RAPIDJSON_UNLIKELY(!handler.EndArray(elementCount))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + return; + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorArrayMissCommaOrSquareBracket, is.Tell()); + + if (parseFlags & kParseTrailingCommasFlag) { + if (is.Peek() == ']') { + if (RAPIDJSON_UNLIKELY(!handler.EndArray(elementCount))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + is.Take(); + return; + } + } + } + } + + template + void ParseNull(InputStream& is, Handler& handler) { + RAPIDJSON_ASSERT(is.Peek() == 'n'); + is.Take(); + + if (RAPIDJSON_LIKELY(Consume(is, 'u') && Consume(is, 'l') && Consume(is, 'l'))) { + if (RAPIDJSON_UNLIKELY(!handler.Null())) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell()); + } + + template + void ParseTrue(InputStream& is, Handler& handler) { + RAPIDJSON_ASSERT(is.Peek() == 't'); + is.Take(); + + if (RAPIDJSON_LIKELY(Consume(is, 'r') && Consume(is, 'u') && Consume(is, 'e'))) { + if (RAPIDJSON_UNLIKELY(!handler.Bool(true))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell()); + } + + template + void ParseFalse(InputStream& is, Handler& handler) { + RAPIDJSON_ASSERT(is.Peek() == 'f'); + is.Take(); + + if (RAPIDJSON_LIKELY(Consume(is, 'a') && Consume(is, 'l') && Consume(is, 's') && Consume(is, 'e'))) { + if (RAPIDJSON_UNLIKELY(!handler.Bool(false))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell()); + } + + template + RAPIDJSON_FORCEINLINE static bool Consume(InputStream& is, typename InputStream::Ch expect) { + if (RAPIDJSON_LIKELY(is.Peek() == expect)) { + is.Take(); + return true; + } + else + return false; + } + + // Helper function to parse four hexadecimal digits in \uXXXX in ParseString(). + template + unsigned ParseHex4(InputStream& is, size_t escapeOffset) { + unsigned codepoint = 0; + for (int i = 0; i < 4; i++) { + Ch c = is.Peek(); + codepoint <<= 4; + codepoint += static_cast(c); + if (c >= '0' && c <= '9') + codepoint -= '0'; + else if (c >= 'A' && c <= 'F') + codepoint -= 'A' - 10; + else if (c >= 'a' && c <= 'f') + codepoint -= 'a' - 10; + else { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorStringUnicodeEscapeInvalidHex, escapeOffset); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(0); + } + is.Take(); + } + return codepoint; + } + + template + class StackStream { + public: + typedef CharType Ch; + + StackStream(internal::Stack& stack) : stack_(stack), length_(0) {} + RAPIDJSON_FORCEINLINE void Put(Ch c) { + *stack_.template Push() = c; + ++length_; + } + + RAPIDJSON_FORCEINLINE void* Push(SizeType count) { + length_ += count; + return stack_.template Push(count); + } + + size_t Length() const { return length_; } + + Ch* Pop() { + return stack_.template Pop(length_); + } + + private: + StackStream(const StackStream&); + StackStream& operator=(const StackStream&); + + internal::Stack& stack_; + SizeType length_; + }; + + // Parse string and generate String event. Different code paths for kParseInsituFlag. + template + void ParseString(InputStream& is, Handler& handler, bool isKey = false) { + internal::StreamLocalCopy copy(is); + InputStream& s(copy.s); + + RAPIDJSON_ASSERT(s.Peek() == '\"'); + s.Take(); // Skip '\"' + + bool success = false; + if (parseFlags & kParseInsituFlag) { + typename InputStream::Ch *head = s.PutBegin(); + ParseStringToStream(s, s); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + size_t length = s.PutEnd(head) - 1; + RAPIDJSON_ASSERT(length <= 0xFFFFFFFF); + const typename TargetEncoding::Ch* const str = reinterpret_cast(head); + success = (isKey ? handler.Key(str, SizeType(length), false) : handler.String(str, SizeType(length), false)); + } + else { + StackStream stackStream(stack_); + ParseStringToStream(s, stackStream); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + SizeType length = static_cast(stackStream.Length()) - 1; + const typename TargetEncoding::Ch* const str = stackStream.Pop(); + success = (isKey ? handler.Key(str, length, true) : handler.String(str, length, true)); + } + if (RAPIDJSON_UNLIKELY(!success)) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, s.Tell()); + } + + // Parse string to an output is + // This function handles the prefix/suffix double quotes, escaping, and optional encoding validation. + template + RAPIDJSON_FORCEINLINE void ParseStringToStream(InputStream& is, OutputStream& os) { +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 + static const char escape[256] = { + Z16, Z16, 0, 0,'\"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '/', + Z16, Z16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'\\', 0, 0, 0, + 0, 0,'\b', 0, 0, 0,'\f', 0, 0, 0, 0, 0, 0, 0,'\n', 0, + 0, 0,'\r', 0,'\t', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16 + }; +#undef Z16 +//!@endcond + + for (;;) { + // Scan and copy string before "\\\"" or < 0x20. This is an optional optimization. + if (!(parseFlags & kParseValidateEncodingFlag)) + ScanCopyUnescapedString(is, os); + + Ch c = is.Peek(); + if (RAPIDJSON_UNLIKELY(c == '\\')) { // Escape + size_t escapeOffset = is.Tell(); // For invalid escaping, report the initial '\\' as error offset + is.Take(); + Ch e = is.Peek(); + if ((sizeof(Ch) == 1 || unsigned(e) < 256) && RAPIDJSON_LIKELY(escape[static_cast(e)])) { + is.Take(); + os.Put(static_cast(escape[static_cast(e)])); + } + else if ((parseFlags & kParseEscapedApostropheFlag) && RAPIDJSON_LIKELY(e == '\'')) { // Allow escaped apostrophe + is.Take(); + os.Put('\''); + } + else if (RAPIDJSON_LIKELY(e == 'u')) { // Unicode + is.Take(); + unsigned codepoint = ParseHex4(is, escapeOffset); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + if (RAPIDJSON_UNLIKELY(codepoint >= 0xD800 && codepoint <= 0xDFFF)) { + // high surrogate, check if followed by valid low surrogate + if (RAPIDJSON_LIKELY(codepoint <= 0xDBFF)) { + // Handle UTF-16 surrogate pair + if (RAPIDJSON_UNLIKELY(!Consume(is, '\\') || !Consume(is, 'u'))) + RAPIDJSON_PARSE_ERROR(kParseErrorStringUnicodeSurrogateInvalid, escapeOffset); + unsigned codepoint2 = ParseHex4(is, escapeOffset); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + if (RAPIDJSON_UNLIKELY(codepoint2 < 0xDC00 || codepoint2 > 0xDFFF)) + RAPIDJSON_PARSE_ERROR(kParseErrorStringUnicodeSurrogateInvalid, escapeOffset); + codepoint = (((codepoint - 0xD800) << 10) | (codepoint2 - 0xDC00)) + 0x10000; + } + // single low surrogate + else + { + RAPIDJSON_PARSE_ERROR(kParseErrorStringUnicodeSurrogateInvalid, escapeOffset); + } + } + TEncoding::Encode(os, codepoint); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorStringEscapeInvalid, escapeOffset); + } + else if (RAPIDJSON_UNLIKELY(c == '"')) { // Closing double quote + is.Take(); + os.Put('\0'); // null-terminate the string + return; + } + else if (RAPIDJSON_UNLIKELY(static_cast(c) < 0x20)) { // RFC 4627: unescaped = %x20-21 / %x23-5B / %x5D-10FFFF + if (c == '\0') + RAPIDJSON_PARSE_ERROR(kParseErrorStringMissQuotationMark, is.Tell()); + else + RAPIDJSON_PARSE_ERROR(kParseErrorStringInvalidEncoding, is.Tell()); + } + else { + size_t offset = is.Tell(); + if (RAPIDJSON_UNLIKELY((parseFlags & kParseValidateEncodingFlag ? + !Transcoder::Validate(is, os) : + !Transcoder::Transcode(is, os)))) + RAPIDJSON_PARSE_ERROR(kParseErrorStringInvalidEncoding, offset); + } + } + } + + template + static RAPIDJSON_FORCEINLINE void ScanCopyUnescapedString(InputStream&, OutputStream&) { + // Do nothing for generic version + } + +#if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) + // StringStream -> StackStream + static RAPIDJSON_FORCEINLINE void ScanCopyUnescapedString(StringStream& is, StackStream& os) { + const char* p = is.src_; + + // Scan one by one until alignment (unaligned load may cross page boundary and cause crash) + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + while (p != nextAligned) + if (RAPIDJSON_UNLIKELY(*p == '\"') || RAPIDJSON_UNLIKELY(*p == '\\') || RAPIDJSON_UNLIKELY(static_cast(*p) < 0x20)) { + is.src_ = p; + return; + } + else + os.Put(*p++); + + // The rest of string using SIMD + static const char dquote[16] = { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' }; + static const char bslash[16] = { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' }; + static const char space[16] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F }; + const __m128i dq = _mm_loadu_si128(reinterpret_cast(&dquote[0])); + const __m128i bs = _mm_loadu_si128(reinterpret_cast(&bslash[0])); + const __m128i sp = _mm_loadu_si128(reinterpret_cast(&space[0])); + + for (;; p += 16) { + const __m128i s = _mm_load_si128(reinterpret_cast(p)); + const __m128i t1 = _mm_cmpeq_epi8(s, dq); + const __m128i t2 = _mm_cmpeq_epi8(s, bs); + const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x1F) == 0x1F + const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3); + unsigned short r = static_cast(_mm_movemask_epi8(x)); + if (RAPIDJSON_UNLIKELY(r != 0)) { // some of characters is escaped + SizeType length; + #ifdef _MSC_VER // Find the index of first escaped + unsigned long offset; + _BitScanForward(&offset, r); + length = offset; + #else + length = static_cast(__builtin_ffs(r) - 1); + #endif + if (length != 0) { + char* q = reinterpret_cast(os.Push(length)); + for (size_t i = 0; i < length; i++) + q[i] = p[i]; + + p += length; + } + break; + } + _mm_storeu_si128(reinterpret_cast<__m128i *>(os.Push(16)), s); + } + + is.src_ = p; + } + + // InsituStringStream -> InsituStringStream + static RAPIDJSON_FORCEINLINE void ScanCopyUnescapedString(InsituStringStream& is, InsituStringStream& os) { + RAPIDJSON_ASSERT(&is == &os); + (void)os; + + if (is.src_ == is.dst_) { + SkipUnescapedString(is); + return; + } + + char* p = is.src_; + char *q = is.dst_; + + // Scan one by one until alignment (unaligned load may cross page boundary and cause crash) + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + while (p != nextAligned) + if (RAPIDJSON_UNLIKELY(*p == '\"') || RAPIDJSON_UNLIKELY(*p == '\\') || RAPIDJSON_UNLIKELY(static_cast(*p) < 0x20)) { + is.src_ = p; + is.dst_ = q; + return; + } + else + *q++ = *p++; + + // The rest of string using SIMD + static const char dquote[16] = { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' }; + static const char bslash[16] = { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' }; + static const char space[16] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F }; + const __m128i dq = _mm_loadu_si128(reinterpret_cast(&dquote[0])); + const __m128i bs = _mm_loadu_si128(reinterpret_cast(&bslash[0])); + const __m128i sp = _mm_loadu_si128(reinterpret_cast(&space[0])); + + for (;; p += 16, q += 16) { + const __m128i s = _mm_load_si128(reinterpret_cast(p)); + const __m128i t1 = _mm_cmpeq_epi8(s, dq); + const __m128i t2 = _mm_cmpeq_epi8(s, bs); + const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x1F) == 0x1F + const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3); + unsigned short r = static_cast(_mm_movemask_epi8(x)); + if (RAPIDJSON_UNLIKELY(r != 0)) { // some of characters is escaped + size_t length; +#ifdef _MSC_VER // Find the index of first escaped + unsigned long offset; + _BitScanForward(&offset, r); + length = offset; +#else + length = static_cast(__builtin_ffs(r) - 1); +#endif + for (const char* pend = p + length; p != pend; ) + *q++ = *p++; + break; + } + _mm_storeu_si128(reinterpret_cast<__m128i *>(q), s); + } + + is.src_ = p; + is.dst_ = q; + } + + // When read/write pointers are the same for insitu stream, just skip unescaped characters + static RAPIDJSON_FORCEINLINE void SkipUnescapedString(InsituStringStream& is) { + RAPIDJSON_ASSERT(is.src_ == is.dst_); + char* p = is.src_; + + // Scan one by one until alignment (unaligned load may cross page boundary and cause crash) + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + for (; p != nextAligned; p++) + if (RAPIDJSON_UNLIKELY(*p == '\"') || RAPIDJSON_UNLIKELY(*p == '\\') || RAPIDJSON_UNLIKELY(static_cast(*p) < 0x20)) { + is.src_ = is.dst_ = p; + return; + } + + // The rest of string using SIMD + static const char dquote[16] = { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' }; + static const char bslash[16] = { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' }; + static const char space[16] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F }; + const __m128i dq = _mm_loadu_si128(reinterpret_cast(&dquote[0])); + const __m128i bs = _mm_loadu_si128(reinterpret_cast(&bslash[0])); + const __m128i sp = _mm_loadu_si128(reinterpret_cast(&space[0])); + + for (;; p += 16) { + const __m128i s = _mm_load_si128(reinterpret_cast(p)); + const __m128i t1 = _mm_cmpeq_epi8(s, dq); + const __m128i t2 = _mm_cmpeq_epi8(s, bs); + const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x1F) == 0x1F + const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3); + unsigned short r = static_cast(_mm_movemask_epi8(x)); + if (RAPIDJSON_UNLIKELY(r != 0)) { // some of characters is escaped + size_t length; +#ifdef _MSC_VER // Find the index of first escaped + unsigned long offset; + _BitScanForward(&offset, r); + length = offset; +#else + length = static_cast(__builtin_ffs(r) - 1); +#endif + p += length; + break; + } + } + + is.src_ = is.dst_ = p; + } +#elif defined(RAPIDJSON_NEON) + // StringStream -> StackStream + static RAPIDJSON_FORCEINLINE void ScanCopyUnescapedString(StringStream& is, StackStream& os) { + const char* p = is.src_; + + // Scan one by one until alignment (unaligned load may cross page boundary and cause crash) + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + while (p != nextAligned) + if (RAPIDJSON_UNLIKELY(*p == '\"') || RAPIDJSON_UNLIKELY(*p == '\\') || RAPIDJSON_UNLIKELY(static_cast(*p) < 0x20)) { + is.src_ = p; + return; + } + else + os.Put(*p++); + + // The rest of string using SIMD + const uint8x16_t s0 = vmovq_n_u8('"'); + const uint8x16_t s1 = vmovq_n_u8('\\'); + const uint8x16_t s2 = vmovq_n_u8('\b'); + const uint8x16_t s3 = vmovq_n_u8(32); + + for (;; p += 16) { + const uint8x16_t s = vld1q_u8(reinterpret_cast(p)); + uint8x16_t x = vceqq_u8(s, s0); + x = vorrq_u8(x, vceqq_u8(s, s1)); + x = vorrq_u8(x, vceqq_u8(s, s2)); + x = vorrq_u8(x, vcltq_u8(s, s3)); + + x = vrev64q_u8(x); // Rev in 64 + uint64_t low = vgetq_lane_u64(vreinterpretq_u64_u8(x), 0); // extract + uint64_t high = vgetq_lane_u64(vreinterpretq_u64_u8(x), 1); // extract + + SizeType length = 0; + bool escaped = false; + if (low == 0) { + if (high != 0) { + uint32_t lz = internal::clzll(high); + length = 8 + (lz >> 3); + escaped = true; + } + } else { + uint32_t lz = internal::clzll(low); + length = lz >> 3; + escaped = true; + } + if (RAPIDJSON_UNLIKELY(escaped)) { // some of characters is escaped + if (length != 0) { + char* q = reinterpret_cast(os.Push(length)); + for (size_t i = 0; i < length; i++) + q[i] = p[i]; + + p += length; + } + break; + } + vst1q_u8(reinterpret_cast(os.Push(16)), s); + } + + is.src_ = p; + } + + // InsituStringStream -> InsituStringStream + static RAPIDJSON_FORCEINLINE void ScanCopyUnescapedString(InsituStringStream& is, InsituStringStream& os) { + RAPIDJSON_ASSERT(&is == &os); + (void)os; + + if (is.src_ == is.dst_) { + SkipUnescapedString(is); + return; + } + + char* p = is.src_; + char *q = is.dst_; + + // Scan one by one until alignment (unaligned load may cross page boundary and cause crash) + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + while (p != nextAligned) + if (RAPIDJSON_UNLIKELY(*p == '\"') || RAPIDJSON_UNLIKELY(*p == '\\') || RAPIDJSON_UNLIKELY(static_cast(*p) < 0x20)) { + is.src_ = p; + is.dst_ = q; + return; + } + else + *q++ = *p++; + + // The rest of string using SIMD + const uint8x16_t s0 = vmovq_n_u8('"'); + const uint8x16_t s1 = vmovq_n_u8('\\'); + const uint8x16_t s2 = vmovq_n_u8('\b'); + const uint8x16_t s3 = vmovq_n_u8(32); + + for (;; p += 16, q += 16) { + const uint8x16_t s = vld1q_u8(reinterpret_cast(p)); + uint8x16_t x = vceqq_u8(s, s0); + x = vorrq_u8(x, vceqq_u8(s, s1)); + x = vorrq_u8(x, vceqq_u8(s, s2)); + x = vorrq_u8(x, vcltq_u8(s, s3)); + + x = vrev64q_u8(x); // Rev in 64 + uint64_t low = vgetq_lane_u64(vreinterpretq_u64_u8(x), 0); // extract + uint64_t high = vgetq_lane_u64(vreinterpretq_u64_u8(x), 1); // extract + + SizeType length = 0; + bool escaped = false; + if (low == 0) { + if (high != 0) { + uint32_t lz = internal::clzll(high); + length = 8 + (lz >> 3); + escaped = true; + } + } else { + uint32_t lz = internal::clzll(low); + length = lz >> 3; + escaped = true; + } + if (RAPIDJSON_UNLIKELY(escaped)) { // some of characters is escaped + for (const char* pend = p + length; p != pend; ) { + *q++ = *p++; + } + break; + } + vst1q_u8(reinterpret_cast(q), s); + } + + is.src_ = p; + is.dst_ = q; + } + + // When read/write pointers are the same for insitu stream, just skip unescaped characters + static RAPIDJSON_FORCEINLINE void SkipUnescapedString(InsituStringStream& is) { + RAPIDJSON_ASSERT(is.src_ == is.dst_); + char* p = is.src_; + + // Scan one by one until alignment (unaligned load may cross page boundary and cause crash) + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + for (; p != nextAligned; p++) + if (RAPIDJSON_UNLIKELY(*p == '\"') || RAPIDJSON_UNLIKELY(*p == '\\') || RAPIDJSON_UNLIKELY(static_cast(*p) < 0x20)) { + is.src_ = is.dst_ = p; + return; + } + + // The rest of string using SIMD + const uint8x16_t s0 = vmovq_n_u8('"'); + const uint8x16_t s1 = vmovq_n_u8('\\'); + const uint8x16_t s2 = vmovq_n_u8('\b'); + const uint8x16_t s3 = vmovq_n_u8(32); + + for (;; p += 16) { + const uint8x16_t s = vld1q_u8(reinterpret_cast(p)); + uint8x16_t x = vceqq_u8(s, s0); + x = vorrq_u8(x, vceqq_u8(s, s1)); + x = vorrq_u8(x, vceqq_u8(s, s2)); + x = vorrq_u8(x, vcltq_u8(s, s3)); + + x = vrev64q_u8(x); // Rev in 64 + uint64_t low = vgetq_lane_u64(vreinterpretq_u64_u8(x), 0); // extract + uint64_t high = vgetq_lane_u64(vreinterpretq_u64_u8(x), 1); // extract + + if (low == 0) { + if (high != 0) { + uint32_t lz = internal::clzll(high); + p += 8 + (lz >> 3); + break; + } + } else { + uint32_t lz = internal::clzll(low); + p += lz >> 3; + break; + } + } + + is.src_ = is.dst_ = p; + } +#endif // RAPIDJSON_NEON + + template + class NumberStream; + + template + class NumberStream { + public: + typedef typename InputStream::Ch Ch; + + NumberStream(GenericReader& reader, InputStream& s) : is(s) { (void)reader; } + + RAPIDJSON_FORCEINLINE Ch Peek() const { return is.Peek(); } + RAPIDJSON_FORCEINLINE Ch TakePush() { return is.Take(); } + RAPIDJSON_FORCEINLINE Ch Take() { return is.Take(); } + RAPIDJSON_FORCEINLINE void Push(char) {} + + size_t Tell() { return is.Tell(); } + size_t Length() { return 0; } + const StackCharacter* Pop() { return 0; } + + protected: + NumberStream& operator=(const NumberStream&); + + InputStream& is; + }; + + template + class NumberStream : public NumberStream { + typedef NumberStream Base; + public: + NumberStream(GenericReader& reader, InputStream& s) : Base(reader, s), stackStream(reader.stack_) {} + + RAPIDJSON_FORCEINLINE Ch TakePush() { + stackStream.Put(static_cast(Base::is.Peek())); + return Base::is.Take(); + } + + RAPIDJSON_FORCEINLINE void Push(StackCharacter c) { + stackStream.Put(c); + } + + size_t Length() { return stackStream.Length(); } + + const StackCharacter* Pop() { + stackStream.Put('\0'); + return stackStream.Pop(); + } + + private: + StackStream stackStream; + }; + + template + class NumberStream : public NumberStream { + typedef NumberStream Base; + public: + NumberStream(GenericReader& reader, InputStream& s) : Base(reader, s) {} + + RAPIDJSON_FORCEINLINE Ch Take() { return Base::TakePush(); } + }; + + template + void ParseNumber(InputStream& is, Handler& handler) { + typedef typename internal::SelectIf, typename TargetEncoding::Ch, char>::Type NumberCharacter; + + internal::StreamLocalCopy copy(is); + NumberStream s(*this, copy.s); + + size_t startOffset = s.Tell(); + double d = 0.0; + bool useNanOrInf = false; + + // Parse minus + bool minus = Consume(s, '-'); + + // Parse int: zero / ( digit1-9 *DIGIT ) + unsigned i = 0; + uint64_t i64 = 0; + bool use64bit = false; + int significandDigit = 0; + if (RAPIDJSON_UNLIKELY(s.Peek() == '0')) { + i = 0; + s.TakePush(); + } + else if (RAPIDJSON_LIKELY(s.Peek() >= '1' && s.Peek() <= '9')) { + i = static_cast(s.TakePush() - '0'); + + if (minus) + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (RAPIDJSON_UNLIKELY(i >= 214748364)) { // 2^31 = 2147483648 + if (RAPIDJSON_LIKELY(i != 214748364 || s.Peek() > '8')) { + i64 = i; + use64bit = true; + break; + } + } + i = i * 10 + static_cast(s.TakePush() - '0'); + significandDigit++; + } + else + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (RAPIDJSON_UNLIKELY(i >= 429496729)) { // 2^32 - 1 = 4294967295 + if (RAPIDJSON_LIKELY(i != 429496729 || s.Peek() > '5')) { + i64 = i; + use64bit = true; + break; + } + } + i = i * 10 + static_cast(s.TakePush() - '0'); + significandDigit++; + } + } + // Parse NaN or Infinity here + else if ((parseFlags & kParseNanAndInfFlag) && RAPIDJSON_LIKELY((s.Peek() == 'I' || s.Peek() == 'N'))) { + if (Consume(s, 'N')) { + if (Consume(s, 'a') && Consume(s, 'N')) { + d = std::numeric_limits::quiet_NaN(); + useNanOrInf = true; + } + } + else if (RAPIDJSON_LIKELY(Consume(s, 'I'))) { + if (Consume(s, 'n') && Consume(s, 'f')) { + d = (minus ? -std::numeric_limits::infinity() : std::numeric_limits::infinity()); + useNanOrInf = true; + + if (RAPIDJSON_UNLIKELY(s.Peek() == 'i' && !(Consume(s, 'i') && Consume(s, 'n') + && Consume(s, 'i') && Consume(s, 't') && Consume(s, 'y')))) { + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell()); + } + } + } + + if (RAPIDJSON_UNLIKELY(!useNanOrInf)) { + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell()); + } + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell()); + + // Parse 64bit int + bool useDouble = false; + if (use64bit) { + if (minus) + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (RAPIDJSON_UNLIKELY(i64 >= RAPIDJSON_UINT64_C2(0x0CCCCCCC, 0xCCCCCCCC))) // 2^63 = 9223372036854775808 + if (RAPIDJSON_LIKELY(i64 != RAPIDJSON_UINT64_C2(0x0CCCCCCC, 0xCCCCCCCC) || s.Peek() > '8')) { + d = static_cast(i64); + useDouble = true; + break; + } + i64 = i64 * 10 + static_cast(s.TakePush() - '0'); + significandDigit++; + } + else + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (RAPIDJSON_UNLIKELY(i64 >= RAPIDJSON_UINT64_C2(0x19999999, 0x99999999))) // 2^64 - 1 = 18446744073709551615 + if (RAPIDJSON_LIKELY(i64 != RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) || s.Peek() > '5')) { + d = static_cast(i64); + useDouble = true; + break; + } + i64 = i64 * 10 + static_cast(s.TakePush() - '0'); + significandDigit++; + } + } + + // Force double for big integer + if (useDouble) { + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + d = d * 10 + (s.TakePush() - '0'); + } + } + + // Parse frac = decimal-point 1*DIGIT + int expFrac = 0; + size_t decimalPosition; + if (Consume(s, '.')) { + decimalPosition = s.Length(); + + if (RAPIDJSON_UNLIKELY(!(s.Peek() >= '0' && s.Peek() <= '9'))) + RAPIDJSON_PARSE_ERROR(kParseErrorNumberMissFraction, s.Tell()); + + if (!useDouble) { +#if RAPIDJSON_64BIT + // Use i64 to store significand in 64-bit architecture + if (!use64bit) + i64 = i; + + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (i64 > RAPIDJSON_UINT64_C2(0x1FFFFF, 0xFFFFFFFF)) // 2^53 - 1 for fast path + break; + else { + i64 = i64 * 10 + static_cast(s.TakePush() - '0'); + --expFrac; + if (i64 != 0) + significandDigit++; + } + } + + d = static_cast(i64); +#else + // Use double to store significand in 32-bit architecture + d = static_cast(use64bit ? i64 : i); +#endif + useDouble = true; + } + + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (significandDigit < 17) { + d = d * 10.0 + (s.TakePush() - '0'); + --expFrac; + if (RAPIDJSON_LIKELY(d > 0.0)) + significandDigit++; + } + else + s.TakePush(); + } + } + else + decimalPosition = s.Length(); // decimal position at the end of integer. + + // Parse exp = e [ minus / plus ] 1*DIGIT + int exp = 0; + if (Consume(s, 'e') || Consume(s, 'E')) { + if (!useDouble) { + d = static_cast(use64bit ? i64 : i); + useDouble = true; + } + + bool expMinus = false; + if (Consume(s, '+')) + ; + else if (Consume(s, '-')) + expMinus = true; + + if (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + exp = static_cast(s.Take() - '0'); + if (expMinus) { + // (exp + expFrac) must not underflow int => we're detecting when -exp gets + // dangerously close to INT_MIN (a pessimistic next digit 9 would push it into + // underflow territory): + // + // -(exp * 10 + 9) + expFrac >= INT_MIN + // <=> exp <= (expFrac - INT_MIN - 9) / 10 + RAPIDJSON_ASSERT(expFrac <= 0); + int maxExp = (expFrac + 2147483639) / 10; + + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + exp = exp * 10 + static_cast(s.Take() - '0'); + if (RAPIDJSON_UNLIKELY(exp > maxExp)) { + while (RAPIDJSON_UNLIKELY(s.Peek() >= '0' && s.Peek() <= '9')) // Consume the rest of exponent + s.Take(); + } + } + } + else { // positive exp + int maxExp = 308 - expFrac; + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + exp = exp * 10 + static_cast(s.Take() - '0'); + if (RAPIDJSON_UNLIKELY(exp > maxExp)) + RAPIDJSON_PARSE_ERROR(kParseErrorNumberTooBig, startOffset); + } + } + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorNumberMissExponent, s.Tell()); + + if (expMinus) + exp = -exp; + } + + // Finish parsing, call event according to the type of number. + bool cont = true; + + if (parseFlags & kParseNumbersAsStringsFlag) { + if (parseFlags & kParseInsituFlag) { + s.Pop(); // Pop stack no matter if it will be used or not. + typename InputStream::Ch* head = is.PutBegin(); + const size_t length = s.Tell() - startOffset; + RAPIDJSON_ASSERT(length <= 0xFFFFFFFF); + // unable to insert the \0 character here, it will erase the comma after this number + const typename TargetEncoding::Ch* const str = reinterpret_cast(head); + cont = handler.RawNumber(str, SizeType(length), false); + } + else { + SizeType numCharsToCopy = static_cast(s.Length()); + GenericStringStream > srcStream(s.Pop()); + StackStream dstStream(stack_); + while (numCharsToCopy--) { + Transcoder, TargetEncoding>::Transcode(srcStream, dstStream); + } + dstStream.Put('\0'); + const typename TargetEncoding::Ch* str = dstStream.Pop(); + const SizeType length = static_cast(dstStream.Length()) - 1; + cont = handler.RawNumber(str, SizeType(length), true); + } + } + else { + size_t length = s.Length(); + const NumberCharacter* decimal = s.Pop(); // Pop stack no matter if it will be used or not. + + if (useDouble) { + int p = exp + expFrac; + if (parseFlags & kParseFullPrecisionFlag) + d = internal::StrtodFullPrecision(d, p, decimal, length, decimalPosition, exp); + else + d = internal::StrtodNormalPrecision(d, p); + + // Use > max, instead of == inf, to fix bogus warning -Wfloat-equal + if (d > (std::numeric_limits::max)()) { + // Overflow + // TODO: internal::StrtodX should report overflow (or underflow) + RAPIDJSON_PARSE_ERROR(kParseErrorNumberTooBig, startOffset); + } + + cont = handler.Double(minus ? -d : d); + } + else if (useNanOrInf) { + cont = handler.Double(d); + } + else { + if (use64bit) { + if (minus) + cont = handler.Int64(static_cast(~i64 + 1)); + else + cont = handler.Uint64(i64); + } + else { + if (minus) + cont = handler.Int(static_cast(~i + 1)); + else + cont = handler.Uint(i); + } + } + } + if (RAPIDJSON_UNLIKELY(!cont)) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, startOffset); + } + + // Parse any JSON value + template + void ParseValue(InputStream& is, Handler& handler) { + switch (is.Peek()) { + case 'n': ParseNull (is, handler); break; + case 't': ParseTrue (is, handler); break; + case 'f': ParseFalse (is, handler); break; + case '"': ParseString(is, handler); break; + case '{': ParseObject(is, handler); break; + case '[': ParseArray (is, handler); break; + default : + ParseNumber(is, handler); + break; + + } + } + + // Iterative Parsing + + // States + enum IterativeParsingState { + IterativeParsingFinishState = 0, // sink states at top + IterativeParsingErrorState, // sink states at top + IterativeParsingStartState, + + // Object states + IterativeParsingObjectInitialState, + IterativeParsingMemberKeyState, + IterativeParsingMemberValueState, + IterativeParsingObjectFinishState, + + // Array states + IterativeParsingArrayInitialState, + IterativeParsingElementState, + IterativeParsingArrayFinishState, + + // Single value state + IterativeParsingValueState, + + // Delimiter states (at bottom) + IterativeParsingElementDelimiterState, + IterativeParsingMemberDelimiterState, + IterativeParsingKeyValueDelimiterState, + + cIterativeParsingStateCount + }; + + // Tokens + enum Token { + LeftBracketToken = 0, + RightBracketToken, + + LeftCurlyBracketToken, + RightCurlyBracketToken, + + CommaToken, + ColonToken, + + StringToken, + FalseToken, + TrueToken, + NullToken, + NumberToken, + + kTokenCount + }; + + RAPIDJSON_FORCEINLINE Token Tokenize(Ch c) const { + +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#define N NumberToken +#define N16 N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N + // Maps from ASCII to Token + static const unsigned char tokenMap[256] = { + N16, // 00~0F + N16, // 10~1F + N, N, StringToken, N, N, N, N, N, N, N, N, N, CommaToken, N, N, N, // 20~2F + N, N, N, N, N, N, N, N, N, N, ColonToken, N, N, N, N, N, // 30~3F + N16, // 40~4F + N, N, N, N, N, N, N, N, N, N, N, LeftBracketToken, N, RightBracketToken, N, N, // 50~5F + N, N, N, N, N, N, FalseToken, N, N, N, N, N, N, N, NullToken, N, // 60~6F + N, N, N, N, TrueToken, N, N, N, N, N, N, LeftCurlyBracketToken, N, RightCurlyBracketToken, N, N, // 70~7F + N16, N16, N16, N16, N16, N16, N16, N16 // 80~FF + }; +#undef N +#undef N16 +//!@endcond + + if (sizeof(Ch) == 1 || static_cast(c) < 256) + return static_cast(tokenMap[static_cast(c)]); + else + return NumberToken; + } + + RAPIDJSON_FORCEINLINE IterativeParsingState Predict(IterativeParsingState state, Token token) const { + // current state x one lookahead token -> new state + static const char G[cIterativeParsingStateCount][kTokenCount] = { + // Finish(sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + }, + // Error(sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + }, + // Start + { + IterativeParsingArrayInitialState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingObjectInitialState, // Left curly bracket + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingValueState, // String + IterativeParsingValueState, // False + IterativeParsingValueState, // True + IterativeParsingValueState, // Null + IterativeParsingValueState // Number + }, + // ObjectInitial + { + IterativeParsingErrorState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingObjectFinishState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingMemberKeyState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number + }, + // MemberKey + { + IterativeParsingErrorState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingKeyValueDelimiterState, // Colon + IterativeParsingErrorState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number + }, + // MemberValue + { + IterativeParsingErrorState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingObjectFinishState, // Right curly bracket + IterativeParsingMemberDelimiterState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingErrorState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number + }, + // ObjectFinish(sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + }, + // ArrayInitial + { + IterativeParsingArrayInitialState, // Left bracket(push Element state) + IterativeParsingArrayFinishState, // Right bracket + IterativeParsingObjectInitialState, // Left curly bracket(push Element state) + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingElementState, // String + IterativeParsingElementState, // False + IterativeParsingElementState, // True + IterativeParsingElementState, // Null + IterativeParsingElementState // Number + }, + // Element + { + IterativeParsingErrorState, // Left bracket + IterativeParsingArrayFinishState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingErrorState, // Right curly bracket + IterativeParsingElementDelimiterState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingErrorState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number + }, + // ArrayFinish(sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + }, + // Single Value (sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + }, + // ElementDelimiter + { + IterativeParsingArrayInitialState, // Left bracket(push Element state) + IterativeParsingArrayFinishState, // Right bracket + IterativeParsingObjectInitialState, // Left curly bracket(push Element state) + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingElementState, // String + IterativeParsingElementState, // False + IterativeParsingElementState, // True + IterativeParsingElementState, // Null + IterativeParsingElementState // Number + }, + // MemberDelimiter + { + IterativeParsingErrorState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingObjectFinishState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingMemberKeyState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number + }, + // KeyValueDelimiter + { + IterativeParsingArrayInitialState, // Left bracket(push MemberValue state) + IterativeParsingErrorState, // Right bracket + IterativeParsingObjectInitialState, // Left curly bracket(push MemberValue state) + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingMemberValueState, // String + IterativeParsingMemberValueState, // False + IterativeParsingMemberValueState, // True + IterativeParsingMemberValueState, // Null + IterativeParsingMemberValueState // Number + }, + }; // End of G + + return static_cast(G[state][token]); + } + + // Make an advance in the token stream and state based on the candidate destination state which was returned by Transit(). + // May return a new state on state pop. + template + RAPIDJSON_FORCEINLINE IterativeParsingState Transit(IterativeParsingState src, Token token, IterativeParsingState dst, InputStream& is, Handler& handler) { + (void)token; + + switch (dst) { + case IterativeParsingErrorState: + return dst; + + case IterativeParsingObjectInitialState: + case IterativeParsingArrayInitialState: + { + // Push the state(Element or MemberValue) if we are nested in another array or value of member. + // In this way we can get the correct state on ObjectFinish or ArrayFinish by frame pop. + IterativeParsingState n = src; + if (src == IterativeParsingArrayInitialState || src == IterativeParsingElementDelimiterState) + n = IterativeParsingElementState; + else if (src == IterativeParsingKeyValueDelimiterState) + n = IterativeParsingMemberValueState; + // Push current state. + *stack_.template Push(1) = n; + // Initialize and push the member/element count. + *stack_.template Push(1) = 0; + // Call handler + bool hr = (dst == IterativeParsingObjectInitialState) ? handler.StartObject() : handler.StartArray(); + // On handler short circuits the parsing. + if (!hr) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorTermination, is.Tell()); + return IterativeParsingErrorState; + } + else { + is.Take(); + return dst; + } + } + + case IterativeParsingMemberKeyState: + ParseString(is, handler, true); + if (HasParseError()) + return IterativeParsingErrorState; + else + return dst; + + case IterativeParsingKeyValueDelimiterState: + RAPIDJSON_ASSERT(token == ColonToken); + is.Take(); + return dst; + + case IterativeParsingMemberValueState: + // Must be non-compound value. Or it would be ObjectInitial or ArrayInitial state. + ParseValue(is, handler); + if (HasParseError()) { + return IterativeParsingErrorState; + } + return dst; + + case IterativeParsingElementState: + // Must be non-compound value. Or it would be ObjectInitial or ArrayInitial state. + ParseValue(is, handler); + if (HasParseError()) { + return IterativeParsingErrorState; + } + return dst; + + case IterativeParsingMemberDelimiterState: + case IterativeParsingElementDelimiterState: + is.Take(); + // Update member/element count. + *stack_.template Top() = *stack_.template Top() + 1; + return dst; + + case IterativeParsingObjectFinishState: + { + // Transit from delimiter is only allowed when trailing commas are enabled + if (!(parseFlags & kParseTrailingCommasFlag) && src == IterativeParsingMemberDelimiterState) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorObjectMissName, is.Tell()); + return IterativeParsingErrorState; + } + // Get member count. + SizeType c = *stack_.template Pop(1); + // If the object is not empty, count the last member. + if (src == IterativeParsingMemberValueState) + ++c; + // Restore the state. + IterativeParsingState n = static_cast(*stack_.template Pop(1)); + // Transit to Finish state if this is the topmost scope. + if (n == IterativeParsingStartState) + n = IterativeParsingFinishState; + // Call handler + bool hr = handler.EndObject(c); + // On handler short circuits the parsing. + if (!hr) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorTermination, is.Tell()); + return IterativeParsingErrorState; + } + else { + is.Take(); + return n; + } + } + + case IterativeParsingArrayFinishState: + { + // Transit from delimiter is only allowed when trailing commas are enabled + if (!(parseFlags & kParseTrailingCommasFlag) && src == IterativeParsingElementDelimiterState) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorValueInvalid, is.Tell()); + return IterativeParsingErrorState; + } + // Get element count. + SizeType c = *stack_.template Pop(1); + // If the array is not empty, count the last element. + if (src == IterativeParsingElementState) + ++c; + // Restore the state. + IterativeParsingState n = static_cast(*stack_.template Pop(1)); + // Transit to Finish state if this is the topmost scope. + if (n == IterativeParsingStartState) + n = IterativeParsingFinishState; + // Call handler + bool hr = handler.EndArray(c); + // On handler short circuits the parsing. + if (!hr) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorTermination, is.Tell()); + return IterativeParsingErrorState; + } + else { + is.Take(); + return n; + } + } + + default: + // This branch is for IterativeParsingValueState actually. + // Use `default:` rather than + // `case IterativeParsingValueState:` is for code coverage. + + // The IterativeParsingStartState is not enumerated in this switch-case. + // It is impossible for that case. And it can be caught by following assertion. + + // The IterativeParsingFinishState is not enumerated in this switch-case either. + // It is a "derivative" state which cannot triggered from Predict() directly. + // Therefore it cannot happen here. And it can be caught by following assertion. + RAPIDJSON_ASSERT(dst == IterativeParsingValueState); + + // Must be non-compound value. Or it would be ObjectInitial or ArrayInitial state. + ParseValue(is, handler); + if (HasParseError()) { + return IterativeParsingErrorState; + } + return IterativeParsingFinishState; + } + } + + template + void HandleError(IterativeParsingState src, InputStream& is) { + if (HasParseError()) { + // Error flag has been set. + return; + } + + switch (src) { + case IterativeParsingStartState: RAPIDJSON_PARSE_ERROR(kParseErrorDocumentEmpty, is.Tell()); return; + case IterativeParsingFinishState: RAPIDJSON_PARSE_ERROR(kParseErrorDocumentRootNotSingular, is.Tell()); return; + case IterativeParsingObjectInitialState: + case IterativeParsingMemberDelimiterState: RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissName, is.Tell()); return; + case IterativeParsingMemberKeyState: RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissColon, is.Tell()); return; + case IterativeParsingMemberValueState: RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, is.Tell()); return; + case IterativeParsingKeyValueDelimiterState: + case IterativeParsingArrayInitialState: + case IterativeParsingElementDelimiterState: RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell()); return; + default: RAPIDJSON_ASSERT(src == IterativeParsingElementState); RAPIDJSON_PARSE_ERROR(kParseErrorArrayMissCommaOrSquareBracket, is.Tell()); return; + } + } + + RAPIDJSON_FORCEINLINE bool IsIterativeParsingDelimiterState(IterativeParsingState s) const { + return s >= IterativeParsingElementDelimiterState; + } + + RAPIDJSON_FORCEINLINE bool IsIterativeParsingCompleteState(IterativeParsingState s) const { + return s <= IterativeParsingErrorState; + } + + template + ParseResult IterativeParse(InputStream& is, Handler& handler) { + parseResult_.Clear(); + ClearStackOnExit scope(*this); + IterativeParsingState state = IterativeParsingStartState; + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + while (is.Peek() != '\0') { + Token t = Tokenize(is.Peek()); + IterativeParsingState n = Predict(state, t); + IterativeParsingState d = Transit(state, t, n, is, handler); + + if (d == IterativeParsingErrorState) { + HandleError(state, is); + break; + } + + state = d; + + // Do not further consume streams if a root JSON has been parsed. + if ((parseFlags & kParseStopWhenDoneFlag) && state == IterativeParsingFinishState) + break; + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + } + + // Handle the end of file. + if (state != IterativeParsingFinishState) + HandleError(state, is); + + return parseResult_; + } + + static const size_t kDefaultStackCapacity = 256; //!< Default stack capacity in bytes for storing a single decoded string. + internal::Stack stack_; //!< A stack for storing decoded string temporarily during non-destructive parsing. + ParseResult parseResult_; + IterativeParsingState state_; +}; // class GenericReader + +//! Reader with UTF8 encoding and default allocator. +typedef GenericReader, UTF8<> > Reader; + +RAPIDJSON_NAMESPACE_END + +#if defined(__clang__) || defined(_MSC_VER) +RAPIDJSON_DIAG_POP +#endif + + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_READER_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/schema.h b/include/liblvgl/libs/thorvg/rapidjson/schema.h new file mode 100644 index 00000000..453e43e3 --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/schema.h @@ -0,0 +1,3262 @@ +// Tencent is pleased to support the open source community by making RapidJSON available-> +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip-> All rights reserved-> +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License-> You may obtain a copy of the License at +// +// http://opensource->org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied-> See the License for the +// specific language governing permissions and limitations under the License-> + +#ifndef RAPIDJSON_SCHEMA_H_ +#define RAPIDJSON_SCHEMA_H_ + +#include "document.h" +#include "pointer.h" +#include "stringbuffer.h" +#include "error/en.h" +#include "uri.h" +#include // abs, floor + +#if !defined(RAPIDJSON_SCHEMA_USE_INTERNALREGEX) +#define RAPIDJSON_SCHEMA_USE_INTERNALREGEX 1 +#else +#define RAPIDJSON_SCHEMA_USE_INTERNALREGEX 0 +#endif + +#if !RAPIDJSON_SCHEMA_USE_INTERNALREGEX && defined(RAPIDJSON_SCHEMA_USE_STDREGEX) && (__cplusplus >=201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)) +#define RAPIDJSON_SCHEMA_USE_STDREGEX 1 +#else +#define RAPIDJSON_SCHEMA_USE_STDREGEX 0 +#endif + +#if RAPIDJSON_SCHEMA_USE_INTERNALREGEX +#include "internal/regex.h" +#elif RAPIDJSON_SCHEMA_USE_STDREGEX +#include +#endif + +#if RAPIDJSON_SCHEMA_USE_INTERNALREGEX || RAPIDJSON_SCHEMA_USE_STDREGEX +#define RAPIDJSON_SCHEMA_HAS_REGEX 1 +#else +#define RAPIDJSON_SCHEMA_HAS_REGEX 0 +#endif + +#ifndef RAPIDJSON_SCHEMA_VERBOSE +#define RAPIDJSON_SCHEMA_VERBOSE 0 +#endif + +RAPIDJSON_DIAG_PUSH + +#if defined(__GNUC__) +RAPIDJSON_DIAG_OFF(effc++) +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_OFF(weak-vtables) +RAPIDJSON_DIAG_OFF(exit-time-destructors) +RAPIDJSON_DIAG_OFF(c++98-compat-pedantic) +RAPIDJSON_DIAG_OFF(variadic-macros) +#elif defined(_MSC_VER) +RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// Verbose Utilities + +#if RAPIDJSON_SCHEMA_VERBOSE + +namespace internal { + +inline void PrintInvalidKeywordData(const char* keyword) { + printf(" Fail keyword: '%s'\n", keyword); +} + +inline void PrintInvalidKeywordData(const wchar_t* keyword) { + wprintf(L" Fail keyword: '%ls'\n", keyword); +} + +inline void PrintInvalidDocumentData(const char* document) { + printf(" Fail document: '%s'\n", document); +} + +inline void PrintInvalidDocumentData(const wchar_t* document) { + wprintf(L" Fail document: '%ls'\n", document); +} + +inline void PrintValidatorPointersData(const char* s, const char* d, unsigned depth) { + printf(" Sch: %*s'%s'\n Doc: %*s'%s'\n", depth * 4, " ", s, depth * 4, " ", d); +} + +inline void PrintValidatorPointersData(const wchar_t* s, const wchar_t* d, unsigned depth) { + wprintf(L" Sch: %*ls'%ls'\n Doc: %*ls'%ls'\n", depth * 4, L" ", s, depth * 4, L" ", d); +} + +inline void PrintSchemaIdsData(const char* base, const char* local, const char* resolved) { + printf(" Resolving id: Base: '%s', Local: '%s', Resolved: '%s'\n", base, local, resolved); +} + +inline void PrintSchemaIdsData(const wchar_t* base, const wchar_t* local, const wchar_t* resolved) { + wprintf(L" Resolving id: Base: '%ls', Local: '%ls', Resolved: '%ls'\n", base, local, resolved); +} + +inline void PrintMethodData(const char* method) { + printf("%s\n", method); +} + +inline void PrintMethodData(const char* method, bool b) { + printf("%s, Data: '%s'\n", method, b ? "true" : "false"); +} + +inline void PrintMethodData(const char* method, int64_t i) { + printf("%s, Data: '%" PRId64 "'\n", method, i); +} + +inline void PrintMethodData(const char* method, uint64_t u) { + printf("%s, Data: '%" PRIu64 "'\n", method, u); +} + +inline void PrintMethodData(const char* method, double d) { + printf("%s, Data: '%lf'\n", method, d); +} + +inline void PrintMethodData(const char* method, const char* s) { + printf("%s, Data: '%s'\n", method, s); +} + +inline void PrintMethodData(const char* method, const wchar_t* s) { + wprintf(L"%hs, Data: '%ls'\n", method, s); +} + +inline void PrintMethodData(const char* method, const char* s1, const char* s2) { + printf("%s, Data: '%s', '%s'\n", method, s1, s2); +} + +inline void PrintMethodData(const char* method, const wchar_t* s1, const wchar_t* s2) { + wprintf(L"%hs, Data: '%ls', '%ls'\n", method, s1, s2); +} + +} // namespace internal + +#endif // RAPIDJSON_SCHEMA_VERBOSE + +#ifndef RAPIDJSON_SCHEMA_PRINT +#if RAPIDJSON_SCHEMA_VERBOSE +#define RAPIDJSON_SCHEMA_PRINT(name, ...) internal::Print##name##Data(__VA_ARGS__) +#else +#define RAPIDJSON_SCHEMA_PRINT(name, ...) +#endif +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_INVALID_KEYWORD_RETURN + +#define RAPIDJSON_INVALID_KEYWORD_RETURN(code)\ +RAPIDJSON_MULTILINEMACRO_BEGIN\ + context.invalidCode = code;\ + context.invalidKeyword = SchemaType::GetValidateErrorKeyword(code).GetString();\ + RAPIDJSON_SCHEMA_PRINT(InvalidKeyword, context.invalidKeyword);\ + return false;\ +RAPIDJSON_MULTILINEMACRO_END + +/////////////////////////////////////////////////////////////////////////////// +// ValidateFlag + +/*! \def RAPIDJSON_VALIDATE_DEFAULT_FLAGS + \ingroup RAPIDJSON_CONFIG + \brief User-defined kValidateDefaultFlags definition. + + User can define this as any \c ValidateFlag combinations. +*/ +#ifndef RAPIDJSON_VALIDATE_DEFAULT_FLAGS +#define RAPIDJSON_VALIDATE_DEFAULT_FLAGS kValidateNoFlags +#endif + +//! Combination of validate flags +/*! \see + */ +enum ValidateFlag { + kValidateNoFlags = 0, //!< No flags are set. + kValidateContinueOnErrorFlag = 1, //!< Don't stop after first validation error. + kValidateReadFlag = 2, //!< Validation is for a read semantic. + kValidateWriteFlag = 4, //!< Validation is for a write semantic. + kValidateDefaultFlags = RAPIDJSON_VALIDATE_DEFAULT_FLAGS //!< Default validate flags. Can be customized by defining RAPIDJSON_VALIDATE_DEFAULT_FLAGS +}; + +/////////////////////////////////////////////////////////////////////////////// +// Specification +enum SchemaDraft { + kDraftUnknown = -1, + kDraftNone = 0, + kDraft03 = 3, + kDraftMin = 4, //!< Current minimum supported draft + kDraft04 = 4, + kDraft05 = 5, + kDraftMax = 5, //!< Current maximum supported draft + kDraft06 = 6, + kDraft07 = 7, + kDraft2019_09 = 8, + kDraft2020_12 = 9 +}; + +enum OpenApiVersion { + kVersionUnknown = -1, + kVersionNone = 0, + kVersionMin = 2, //!< Current minimum supported version + kVersion20 = 2, + kVersion30 = 3, + kVersionMax = 3, //!< Current maximum supported version + kVersion31 = 4, +}; + +struct Specification { + Specification(SchemaDraft d) : draft(d), oapi(kVersionNone) {} + Specification(OpenApiVersion o) : oapi(o) { + if (oapi == kVersion20) draft = kDraft04; + else if (oapi == kVersion30) draft = kDraft05; + else if (oapi == kVersion31) draft = kDraft2020_12; + else draft = kDraft04; + } + ~Specification() {} + bool IsSupported() const { + return ((draft >= kDraftMin && draft <= kDraftMax) && ((oapi == kVersionNone) || (oapi >= kVersionMin && oapi <= kVersionMax))); + } + SchemaDraft draft; + OpenApiVersion oapi; +}; + +/////////////////////////////////////////////////////////////////////////////// +// Forward declarations + +template +class GenericSchemaDocument; + +namespace internal { + +template +class Schema; + +/////////////////////////////////////////////////////////////////////////////// +// ISchemaValidator + +class ISchemaValidator { +public: + virtual ~ISchemaValidator() {} + virtual bool IsValid() const = 0; + virtual void SetValidateFlags(unsigned flags) = 0; + virtual unsigned GetValidateFlags() const = 0; +}; + +/////////////////////////////////////////////////////////////////////////////// +// ISchemaStateFactory + +template +class ISchemaStateFactory { +public: + virtual ~ISchemaStateFactory() {} + virtual ISchemaValidator* CreateSchemaValidator(const SchemaType&, const bool inheritContinueOnErrors) = 0; + virtual void DestroySchemaValidator(ISchemaValidator* validator) = 0; + virtual void* CreateHasher() = 0; + virtual uint64_t GetHashCode(void* hasher) = 0; + virtual void DestroyHasher(void* hasher) = 0; + virtual void* MallocState(size_t size) = 0; + virtual void FreeState(void* p) = 0; +}; + +/////////////////////////////////////////////////////////////////////////////// +// IValidationErrorHandler + +template +class IValidationErrorHandler { +public: + typedef typename SchemaType::Ch Ch; + typedef typename SchemaType::SValue SValue; + + virtual ~IValidationErrorHandler() {} + + virtual void NotMultipleOf(int64_t actual, const SValue& expected) = 0; + virtual void NotMultipleOf(uint64_t actual, const SValue& expected) = 0; + virtual void NotMultipleOf(double actual, const SValue& expected) = 0; + virtual void AboveMaximum(int64_t actual, const SValue& expected, bool exclusive) = 0; + virtual void AboveMaximum(uint64_t actual, const SValue& expected, bool exclusive) = 0; + virtual void AboveMaximum(double actual, const SValue& expected, bool exclusive) = 0; + virtual void BelowMinimum(int64_t actual, const SValue& expected, bool exclusive) = 0; + virtual void BelowMinimum(uint64_t actual, const SValue& expected, bool exclusive) = 0; + virtual void BelowMinimum(double actual, const SValue& expected, bool exclusive) = 0; + + virtual void TooLong(const Ch* str, SizeType length, SizeType expected) = 0; + virtual void TooShort(const Ch* str, SizeType length, SizeType expected) = 0; + virtual void DoesNotMatch(const Ch* str, SizeType length) = 0; + + virtual void DisallowedItem(SizeType index) = 0; + virtual void TooFewItems(SizeType actualCount, SizeType expectedCount) = 0; + virtual void TooManyItems(SizeType actualCount, SizeType expectedCount) = 0; + virtual void DuplicateItems(SizeType index1, SizeType index2) = 0; + + virtual void TooManyProperties(SizeType actualCount, SizeType expectedCount) = 0; + virtual void TooFewProperties(SizeType actualCount, SizeType expectedCount) = 0; + virtual void StartMissingProperties() = 0; + virtual void AddMissingProperty(const SValue& name) = 0; + virtual bool EndMissingProperties() = 0; + virtual void PropertyViolations(ISchemaValidator** subvalidators, SizeType count) = 0; + virtual void DisallowedProperty(const Ch* name, SizeType length) = 0; + + virtual void StartDependencyErrors() = 0; + virtual void StartMissingDependentProperties() = 0; + virtual void AddMissingDependentProperty(const SValue& targetName) = 0; + virtual void EndMissingDependentProperties(const SValue& sourceName) = 0; + virtual void AddDependencySchemaError(const SValue& sourceName, ISchemaValidator* subvalidator) = 0; + virtual bool EndDependencyErrors() = 0; + + virtual void DisallowedValue(const ValidateErrorCode code) = 0; + virtual void StartDisallowedType() = 0; + virtual void AddExpectedType(const typename SchemaType::ValueType& expectedType) = 0; + virtual void EndDisallowedType(const typename SchemaType::ValueType& actualType) = 0; + virtual void NotAllOf(ISchemaValidator** subvalidators, SizeType count) = 0; + virtual void NoneOf(ISchemaValidator** subvalidators, SizeType count) = 0; + virtual void NotOneOf(ISchemaValidator** subvalidators, SizeType count) = 0; + virtual void MultipleOneOf(SizeType index1, SizeType index2) = 0; + virtual void Disallowed() = 0; + virtual void DisallowedWhenWriting() = 0; + virtual void DisallowedWhenReading() = 0; +}; + + +/////////////////////////////////////////////////////////////////////////////// +// Hasher + +// For comparison of compound value +template +class Hasher { +public: + typedef typename Encoding::Ch Ch; + + Hasher(Allocator* allocator = 0, size_t stackCapacity = kDefaultSize) : stack_(allocator, stackCapacity) {} + + bool Null() { return WriteType(kNullType); } + bool Bool(bool b) { return WriteType(b ? kTrueType : kFalseType); } + bool Int(int i) { Number n; n.u.i = i; n.d = static_cast(i); return WriteNumber(n); } + bool Uint(unsigned u) { Number n; n.u.u = u; n.d = static_cast(u); return WriteNumber(n); } + bool Int64(int64_t i) { Number n; n.u.i = i; n.d = static_cast(i); return WriteNumber(n); } + bool Uint64(uint64_t u) { Number n; n.u.u = u; n.d = static_cast(u); return WriteNumber(n); } + bool Double(double d) { + Number n; + if (d < 0) n.u.i = static_cast(d); + else n.u.u = static_cast(d); + n.d = d; + return WriteNumber(n); + } + + bool RawNumber(const Ch* str, SizeType len, bool) { + WriteBuffer(kNumberType, str, len * sizeof(Ch)); + return true; + } + + bool String(const Ch* str, SizeType len, bool) { + WriteBuffer(kStringType, str, len * sizeof(Ch)); + return true; + } + + bool StartObject() { return true; } + bool Key(const Ch* str, SizeType len, bool copy) { return String(str, len, copy); } + bool EndObject(SizeType memberCount) { + uint64_t h = Hash(0, kObjectType); + uint64_t* kv = stack_.template Pop(memberCount * 2); + for (SizeType i = 0; i < memberCount; i++) + h ^= Hash(kv[i * 2], kv[i * 2 + 1]); // Use xor to achieve member order insensitive + *stack_.template Push() = h; + return true; + } + + bool StartArray() { return true; } + bool EndArray(SizeType elementCount) { + uint64_t h = Hash(0, kArrayType); + uint64_t* e = stack_.template Pop(elementCount); + for (SizeType i = 0; i < elementCount; i++) + h = Hash(h, e[i]); // Use hash to achieve element order sensitive + *stack_.template Push() = h; + return true; + } + + bool IsValid() const { return stack_.GetSize() == sizeof(uint64_t); } + + uint64_t GetHashCode() const { + RAPIDJSON_ASSERT(IsValid()); + return *stack_.template Top(); + } + +private: + static const size_t kDefaultSize = 256; + struct Number { + union U { + uint64_t u; + int64_t i; + }u; + double d; + }; + + bool WriteType(Type type) { return WriteBuffer(type, 0, 0); } + + bool WriteNumber(const Number& n) { return WriteBuffer(kNumberType, &n, sizeof(n)); } + + bool WriteBuffer(Type type, const void* data, size_t len) { + // FNV-1a from http://isthe.com/chongo/tech/comp/fnv/ + uint64_t h = Hash(RAPIDJSON_UINT64_C2(0x84222325, 0xcbf29ce4), type); + const unsigned char* d = static_cast(data); + for (size_t i = 0; i < len; i++) + h = Hash(h, d[i]); + *stack_.template Push() = h; + return true; + } + + static uint64_t Hash(uint64_t h, uint64_t d) { + static const uint64_t kPrime = RAPIDJSON_UINT64_C2(0x00000100, 0x000001b3); + h ^= d; + h *= kPrime; + return h; + } + + Stack stack_; +}; + +/////////////////////////////////////////////////////////////////////////////// +// SchemaValidationContext + +template +struct SchemaValidationContext { + typedef Schema SchemaType; + typedef ISchemaStateFactory SchemaValidatorFactoryType; + typedef IValidationErrorHandler ErrorHandlerType; + typedef typename SchemaType::ValueType ValueType; + typedef typename ValueType::Ch Ch; + + enum PatternValidatorType { + kPatternValidatorOnly, + kPatternValidatorWithProperty, + kPatternValidatorWithAdditionalProperty + }; + + SchemaValidationContext(SchemaValidatorFactoryType& f, ErrorHandlerType& eh, const SchemaType* s, unsigned fl = 0) : + factory(f), + error_handler(eh), + schema(s), + flags(fl), + valueSchema(), + invalidKeyword(), + invalidCode(), + hasher(), + arrayElementHashCodes(), + validators(), + validatorCount(), + patternPropertiesValidators(), + patternPropertiesValidatorCount(), + patternPropertiesSchemas(), + patternPropertiesSchemaCount(), + valuePatternValidatorType(kPatternValidatorOnly), + propertyExist(), + inArray(false), + valueUniqueness(false), + arrayUniqueness(false) + { + } + + ~SchemaValidationContext() { + if (hasher) + factory.DestroyHasher(hasher); + if (validators) { + for (SizeType i = 0; i < validatorCount; i++) { + if (validators[i]) { + factory.DestroySchemaValidator(validators[i]); + } + } + factory.FreeState(validators); + } + if (patternPropertiesValidators) { + for (SizeType i = 0; i < patternPropertiesValidatorCount; i++) { + if (patternPropertiesValidators[i]) { + factory.DestroySchemaValidator(patternPropertiesValidators[i]); + } + } + factory.FreeState(patternPropertiesValidators); + } + if (patternPropertiesSchemas) + factory.FreeState(patternPropertiesSchemas); + if (propertyExist) + factory.FreeState(propertyExist); + } + + SchemaValidatorFactoryType& factory; + ErrorHandlerType& error_handler; + const SchemaType* schema; + unsigned flags; + const SchemaType* valueSchema; + const Ch* invalidKeyword; + ValidateErrorCode invalidCode; + void* hasher; // Only validator access + void* arrayElementHashCodes; // Only validator access this + ISchemaValidator** validators; + SizeType validatorCount; + ISchemaValidator** patternPropertiesValidators; + SizeType patternPropertiesValidatorCount; + const SchemaType** patternPropertiesSchemas; + SizeType patternPropertiesSchemaCount; + PatternValidatorType valuePatternValidatorType; + PatternValidatorType objectPatternValidatorType; + SizeType arrayElementIndex; + bool* propertyExist; + bool inArray; + bool valueUniqueness; + bool arrayUniqueness; +}; + +/////////////////////////////////////////////////////////////////////////////// +// Schema + +template +class Schema { +public: + typedef typename SchemaDocumentType::ValueType ValueType; + typedef typename SchemaDocumentType::AllocatorType AllocatorType; + typedef typename SchemaDocumentType::PointerType PointerType; + typedef typename ValueType::EncodingType EncodingType; + typedef typename EncodingType::Ch Ch; + typedef SchemaValidationContext Context; + typedef Schema SchemaType; + typedef GenericValue SValue; + typedef IValidationErrorHandler ErrorHandler; + typedef GenericUri UriType; + friend class GenericSchemaDocument; + + Schema(SchemaDocumentType* schemaDocument, const PointerType& p, const ValueType& value, const ValueType& document, AllocatorType* allocator, const UriType& id = UriType()) : + allocator_(allocator), + uri_(schemaDocument->GetURI(), *allocator), + id_(id, allocator), + spec_(schemaDocument->GetSpecification()), + pointer_(p, allocator), + typeless_(schemaDocument->GetTypeless()), + enum_(), + enumCount_(), + not_(), + type_((1 << kTotalSchemaType) - 1), // typeless + validatorCount_(), + notValidatorIndex_(), + properties_(), + additionalPropertiesSchema_(), + patternProperties_(), + patternPropertyCount_(), + propertyCount_(), + minProperties_(), + maxProperties_(SizeType(~0)), + additionalProperties_(true), + hasDependencies_(), + hasRequired_(), + hasSchemaDependencies_(), + additionalItemsSchema_(), + itemsList_(), + itemsTuple_(), + itemsTupleCount_(), + minItems_(), + maxItems_(SizeType(~0)), + additionalItems_(true), + uniqueItems_(false), + pattern_(), + minLength_(0), + maxLength_(~SizeType(0)), + exclusiveMinimum_(false), + exclusiveMaximum_(false), + defaultValueLength_(0), + readOnly_(false), + writeOnly_(false), + nullable_(false) + { + GenericStringBuffer sb; + p.StringifyUriFragment(sb); + RAPIDJSON_SCHEMA_PRINT(Method, "Schema::Schema", sb.GetString(), id.GetString()); + + typedef typename ValueType::ConstValueIterator ConstValueIterator; + typedef typename ValueType::ConstMemberIterator ConstMemberIterator; + + // PR #1393 + // Early add this Schema and its $ref(s) in schemaDocument's map to avoid infinite + // recursion (with recursive schemas), since schemaDocument->getSchema() is always + // checked before creating a new one. Don't cache typeless_, though. + if (this != typeless_) { + typedef typename SchemaDocumentType::SchemaEntry SchemaEntry; + SchemaEntry *entry = schemaDocument->schemaMap_.template Push(); + new (entry) SchemaEntry(pointer_, this, true, allocator_); + schemaDocument->AddSchemaRefs(this); + } + + if (!value.IsObject()) + return; + + // If we have an id property, resolve it with the in-scope id + // Not supported for open api 2.0 or 3.0 + if (spec_.oapi != kVersion20 && spec_.oapi != kVersion30) + if (const ValueType* v = GetMember(value, GetIdString())) { + if (v->IsString()) { + UriType local(*v, allocator); + id_ = local.Resolve(id_, allocator); + RAPIDJSON_SCHEMA_PRINT(SchemaIds, id.GetString(), v->GetString(), id_.GetString()); + } + } + + if (const ValueType* v = GetMember(value, GetTypeString())) { + type_ = 0; + if (v->IsString()) + AddType(*v); + else if (v->IsArray()) + for (ConstValueIterator itr = v->Begin(); itr != v->End(); ++itr) + AddType(*itr); + } + + if (const ValueType* v = GetMember(value, GetEnumString())) { + if (v->IsArray() && v->Size() > 0) { + enum_ = static_cast(allocator_->Malloc(sizeof(uint64_t) * v->Size())); + for (ConstValueIterator itr = v->Begin(); itr != v->End(); ++itr) { + typedef Hasher > EnumHasherType; + char buffer[256u + 24]; + MemoryPoolAllocator hasherAllocator(buffer, sizeof(buffer)); + EnumHasherType h(&hasherAllocator, 256); + itr->Accept(h); + enum_[enumCount_++] = h.GetHashCode(); + } + } + } + + if (schemaDocument) + AssignIfExist(allOf_, *schemaDocument, p, value, GetAllOfString(), document); + + // AnyOf, OneOf, Not not supported for open api 2.0 + if (schemaDocument && spec_.oapi != kVersion20) { + AssignIfExist(anyOf_, *schemaDocument, p, value, GetAnyOfString(), document); + AssignIfExist(oneOf_, *schemaDocument, p, value, GetOneOfString(), document); + + if (const ValueType* v = GetMember(value, GetNotString())) { + schemaDocument->CreateSchema(¬_, p.Append(GetNotString(), allocator_), *v, document, id_); + notValidatorIndex_ = validatorCount_; + validatorCount_++; + } + } + + // Object + + const ValueType* properties = GetMember(value, GetPropertiesString()); + const ValueType* required = GetMember(value, GetRequiredString()); + const ValueType* dependencies = GetMember(value, GetDependenciesString()); + { + // Gather properties from properties/required/dependencies + SValue allProperties(kArrayType); + + if (properties && properties->IsObject()) + for (ConstMemberIterator itr = properties->MemberBegin(); itr != properties->MemberEnd(); ++itr) + AddUniqueElement(allProperties, itr->name); + + if (required && required->IsArray()) + for (ConstValueIterator itr = required->Begin(); itr != required->End(); ++itr) + if (itr->IsString()) + AddUniqueElement(allProperties, *itr); + + // Dependencies not supported for open api 2.0 and 3.0 + if (spec_.oapi != kVersion20 && spec_.oapi != kVersion30) + if (dependencies && dependencies->IsObject()) + for (ConstMemberIterator itr = dependencies->MemberBegin(); itr != dependencies->MemberEnd(); ++itr) { + AddUniqueElement(allProperties, itr->name); + if (itr->value.IsArray()) + for (ConstValueIterator i = itr->value.Begin(); i != itr->value.End(); ++i) + if (i->IsString()) + AddUniqueElement(allProperties, *i); + } + + if (allProperties.Size() > 0) { + propertyCount_ = allProperties.Size(); + properties_ = static_cast(allocator_->Malloc(sizeof(Property) * propertyCount_)); + for (SizeType i = 0; i < propertyCount_; i++) { + new (&properties_[i]) Property(); + properties_[i].name = allProperties[i]; + properties_[i].schema = typeless_; + } + } + } + + if (properties && properties->IsObject()) { + PointerType q = p.Append(GetPropertiesString(), allocator_); + for (ConstMemberIterator itr = properties->MemberBegin(); itr != properties->MemberEnd(); ++itr) { + SizeType index; + if (FindPropertyIndex(itr->name, &index)) + schemaDocument->CreateSchema(&properties_[index].schema, q.Append(itr->name, allocator_), itr->value, document, id_); + } + } + + // PatternProperties not supported for open api 2.0 and 3.0 + if (spec_.oapi != kVersion20 && spec_.oapi != kVersion30) + if (const ValueType* v = GetMember(value, GetPatternPropertiesString())) { + PointerType q = p.Append(GetPatternPropertiesString(), allocator_); + patternProperties_ = static_cast(allocator_->Malloc(sizeof(PatternProperty) * v->MemberCount())); + patternPropertyCount_ = 0; + + for (ConstMemberIterator itr = v->MemberBegin(); itr != v->MemberEnd(); ++itr) { + new (&patternProperties_[patternPropertyCount_]) PatternProperty(); + PointerType r = q.Append(itr->name, allocator_); + patternProperties_[patternPropertyCount_].pattern = CreatePattern(itr->name, schemaDocument, r); + schemaDocument->CreateSchema(&patternProperties_[patternPropertyCount_].schema, r, itr->value, document, id_); + patternPropertyCount_++; + } + } + + if (required && required->IsArray()) + for (ConstValueIterator itr = required->Begin(); itr != required->End(); ++itr) + if (itr->IsString()) { + SizeType index; + if (FindPropertyIndex(*itr, &index)) { + properties_[index].required = true; + hasRequired_ = true; + } + } + + // Dependencies not supported for open api 2.0 and 3.0 + if (spec_.oapi != kVersion20 && spec_.oapi != kVersion30) + if (dependencies && dependencies->IsObject()) { + PointerType q = p.Append(GetDependenciesString(), allocator_); + hasDependencies_ = true; + for (ConstMemberIterator itr = dependencies->MemberBegin(); itr != dependencies->MemberEnd(); ++itr) { + SizeType sourceIndex; + if (FindPropertyIndex(itr->name, &sourceIndex)) { + if (itr->value.IsArray()) { + properties_[sourceIndex].dependencies = static_cast(allocator_->Malloc(sizeof(bool) * propertyCount_)); + std::memset(properties_[sourceIndex].dependencies, 0, sizeof(bool)* propertyCount_); + for (ConstValueIterator targetItr = itr->value.Begin(); targetItr != itr->value.End(); ++targetItr) { + SizeType targetIndex; + if (FindPropertyIndex(*targetItr, &targetIndex)) + properties_[sourceIndex].dependencies[targetIndex] = true; + } + } + else if (itr->value.IsObject()) { + hasSchemaDependencies_ = true; + schemaDocument->CreateSchema(&properties_[sourceIndex].dependenciesSchema, q.Append(itr->name, allocator_), itr->value, document, id_); + properties_[sourceIndex].dependenciesValidatorIndex = validatorCount_; + validatorCount_++; + } + } + } + } + + if (const ValueType* v = GetMember(value, GetAdditionalPropertiesString())) { + if (v->IsBool()) + additionalProperties_ = v->GetBool(); + else if (v->IsObject()) + schemaDocument->CreateSchema(&additionalPropertiesSchema_, p.Append(GetAdditionalPropertiesString(), allocator_), *v, document, id_); + } + + AssignIfExist(minProperties_, value, GetMinPropertiesString()); + AssignIfExist(maxProperties_, value, GetMaxPropertiesString()); + + // Array + if (const ValueType* v = GetMember(value, GetItemsString())) { + PointerType q = p.Append(GetItemsString(), allocator_); + if (v->IsObject()) // List validation + schemaDocument->CreateSchema(&itemsList_, q, *v, document, id_); + else if (v->IsArray()) { // Tuple validation + itemsTuple_ = static_cast(allocator_->Malloc(sizeof(const Schema*) * v->Size())); + SizeType index = 0; + for (ConstValueIterator itr = v->Begin(); itr != v->End(); ++itr, index++) + schemaDocument->CreateSchema(&itemsTuple_[itemsTupleCount_++], q.Append(index, allocator_), *itr, document, id_); + } + } + + AssignIfExist(minItems_, value, GetMinItemsString()); + AssignIfExist(maxItems_, value, GetMaxItemsString()); + + // AdditionalItems not supported for openapi 2.0 and 3.0 + if (spec_.oapi != kVersion20 && spec_.oapi != kVersion30) + if (const ValueType* v = GetMember(value, GetAdditionalItemsString())) { + if (v->IsBool()) + additionalItems_ = v->GetBool(); + else if (v->IsObject()) + schemaDocument->CreateSchema(&additionalItemsSchema_, p.Append(GetAdditionalItemsString(), allocator_), *v, document, id_); + } + + AssignIfExist(uniqueItems_, value, GetUniqueItemsString()); + + // String + AssignIfExist(minLength_, value, GetMinLengthString()); + AssignIfExist(maxLength_, value, GetMaxLengthString()); + + if (const ValueType* v = GetMember(value, GetPatternString())) + pattern_ = CreatePattern(*v, schemaDocument, p.Append(GetPatternString(), allocator_)); + + // Number + if (const ValueType* v = GetMember(value, GetMinimumString())) + if (v->IsNumber()) + minimum_.CopyFrom(*v, *allocator_); + + if (const ValueType* v = GetMember(value, GetMaximumString())) + if (v->IsNumber()) + maximum_.CopyFrom(*v, *allocator_); + + AssignIfExist(exclusiveMinimum_, value, GetExclusiveMinimumString()); + AssignIfExist(exclusiveMaximum_, value, GetExclusiveMaximumString()); + + if (const ValueType* v = GetMember(value, GetMultipleOfString())) + if (v->IsNumber() && v->GetDouble() > 0.0) + multipleOf_.CopyFrom(*v, *allocator_); + + // Default + if (const ValueType* v = GetMember(value, GetDefaultValueString())) + if (v->IsString()) + defaultValueLength_ = v->GetStringLength(); + + // ReadOnly - open api only (until draft 7 supported) + // WriteOnly - open api 3 only (until draft 7 supported) + // Both can't be true + if (spec_.oapi != kVersionNone) + AssignIfExist(readOnly_, value, GetReadOnlyString()); + if (spec_.oapi >= kVersion30) + AssignIfExist(writeOnly_, value, GetWriteOnlyString()); + if (readOnly_ && writeOnly_) + schemaDocument->SchemaError(kSchemaErrorReadOnlyAndWriteOnly, p); + + // Nullable - open api 3 only + // If true add 'null' as allowable type + if (spec_.oapi >= kVersion30) { + AssignIfExist(nullable_, value, GetNullableString()); + if (nullable_) + AddType(GetNullString()); + } + } + + ~Schema() { + AllocatorType::Free(enum_); + if (properties_) { + for (SizeType i = 0; i < propertyCount_; i++) + properties_[i].~Property(); + AllocatorType::Free(properties_); + } + if (patternProperties_) { + for (SizeType i = 0; i < patternPropertyCount_; i++) + patternProperties_[i].~PatternProperty(); + AllocatorType::Free(patternProperties_); + } + AllocatorType::Free(itemsTuple_); +#if RAPIDJSON_SCHEMA_HAS_REGEX + if (pattern_) { + pattern_->~RegexType(); + AllocatorType::Free(pattern_); + } +#endif + } + + const SValue& GetURI() const { + return uri_; + } + + const UriType& GetId() const { + return id_; + } + + const Specification& GetSpecification() const { + return spec_; + } + + const PointerType& GetPointer() const { + return pointer_; + } + + bool BeginValue(Context& context) const { + RAPIDJSON_SCHEMA_PRINT(Method, "Schema::BeginValue"); + if (context.inArray) { + if (uniqueItems_) + context.valueUniqueness = true; + + if (itemsList_) + context.valueSchema = itemsList_; + else if (itemsTuple_) { + if (context.arrayElementIndex < itemsTupleCount_) + context.valueSchema = itemsTuple_[context.arrayElementIndex]; + else if (additionalItemsSchema_) + context.valueSchema = additionalItemsSchema_; + else if (additionalItems_) + context.valueSchema = typeless_; + else { + context.error_handler.DisallowedItem(context.arrayElementIndex); + // Must set valueSchema for when kValidateContinueOnErrorFlag is set, else reports spurious type error + context.valueSchema = typeless_; + // Must bump arrayElementIndex for when kValidateContinueOnErrorFlag is set + context.arrayElementIndex++; + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorAdditionalItems); + } + } + else + context.valueSchema = typeless_; + + context.arrayElementIndex++; + } + return true; + } + + RAPIDJSON_FORCEINLINE bool EndValue(Context& context) const { + RAPIDJSON_SCHEMA_PRINT(Method, "Schema::EndValue"); + // Only check pattern properties if we have validators + if (context.patternPropertiesValidatorCount > 0) { + bool otherValid = false; + SizeType count = context.patternPropertiesValidatorCount; + if (context.objectPatternValidatorType != Context::kPatternValidatorOnly) + otherValid = context.patternPropertiesValidators[--count]->IsValid(); + + bool patternValid = true; + for (SizeType i = 0; i < count; i++) + if (!context.patternPropertiesValidators[i]->IsValid()) { + patternValid = false; + break; + } + + if (context.objectPatternValidatorType == Context::kPatternValidatorOnly) { + if (!patternValid) { + context.error_handler.PropertyViolations(context.patternPropertiesValidators, count); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorPatternProperties); + } + } + else if (context.objectPatternValidatorType == Context::kPatternValidatorWithProperty) { + if (!patternValid || !otherValid) { + context.error_handler.PropertyViolations(context.patternPropertiesValidators, count + 1); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorPatternProperties); + } + } + else if (!patternValid && !otherValid) { // kPatternValidatorWithAdditionalProperty) + context.error_handler.PropertyViolations(context.patternPropertiesValidators, count + 1); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorPatternProperties); + } + } + + // For enums only check if we have a hasher + if (enum_ && context.hasher) { + const uint64_t h = context.factory.GetHashCode(context.hasher); + for (SizeType i = 0; i < enumCount_; i++) + if (enum_[i] == h) + goto foundEnum; + context.error_handler.DisallowedValue(kValidateErrorEnum); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorEnum); + foundEnum:; + } + + // Only check allOf etc if we have validators + if (context.validatorCount > 0) { + if (allOf_.schemas) + for (SizeType i = allOf_.begin; i < allOf_.begin + allOf_.count; i++) + if (!context.validators[i]->IsValid()) { + context.error_handler.NotAllOf(&context.validators[allOf_.begin], allOf_.count); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorAllOf); + } + + if (anyOf_.schemas) { + for (SizeType i = anyOf_.begin; i < anyOf_.begin + anyOf_.count; i++) + if (context.validators[i]->IsValid()) + goto foundAny; + context.error_handler.NoneOf(&context.validators[anyOf_.begin], anyOf_.count); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorAnyOf); + foundAny:; + } + + if (oneOf_.schemas) { + bool oneValid = false; + SizeType firstMatch = 0; + for (SizeType i = oneOf_.begin; i < oneOf_.begin + oneOf_.count; i++) + if (context.validators[i]->IsValid()) { + if (oneValid) { + context.error_handler.MultipleOneOf(firstMatch, i - oneOf_.begin); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorOneOfMatch); + } else { + oneValid = true; + firstMatch = i - oneOf_.begin; + } + } + if (!oneValid) { + context.error_handler.NotOneOf(&context.validators[oneOf_.begin], oneOf_.count); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorOneOf); + } + } + + if (not_ && context.validators[notValidatorIndex_]->IsValid()) { + context.error_handler.Disallowed(); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorNot); + } + } + + return true; + } + + bool Null(Context& context) const { + RAPIDJSON_SCHEMA_PRINT(Method, "Schema::Null"); + if (!(type_ & (1 << kNullSchemaType))) { + DisallowedType(context, GetNullString()); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorType); + } + return CreateParallelValidator(context); + } + + bool Bool(Context& context, bool b) const { + RAPIDJSON_SCHEMA_PRINT(Method, "Schema::Bool", b); + if (!CheckBool(context, b)) + return false; + return CreateParallelValidator(context); + } + + bool Int(Context& context, int i) const { + RAPIDJSON_SCHEMA_PRINT(Method, "Schema::Int", (int64_t)i); + if (!CheckInt(context, i)) + return false; + return CreateParallelValidator(context); + } + + bool Uint(Context& context, unsigned u) const { + RAPIDJSON_SCHEMA_PRINT(Method, "Schema::Uint", (uint64_t)u); + if (!CheckUint(context, u)) + return false; + return CreateParallelValidator(context); + } + + bool Int64(Context& context, int64_t i) const { + RAPIDJSON_SCHEMA_PRINT(Method, "Schema::Int64", i); + if (!CheckInt(context, i)) + return false; + return CreateParallelValidator(context); + } + + bool Uint64(Context& context, uint64_t u) const { + RAPIDJSON_SCHEMA_PRINT(Method, "Schema::Uint64", u); + if (!CheckUint(context, u)) + return false; + return CreateParallelValidator(context); + } + + bool Double(Context& context, double d) const { + RAPIDJSON_SCHEMA_PRINT(Method, "Schema::Double", d); + if (!(type_ & (1 << kNumberSchemaType))) { + DisallowedType(context, GetNumberString()); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorType); + } + + if (!minimum_.IsNull() && !CheckDoubleMinimum(context, d)) + return false; + + if (!maximum_.IsNull() && !CheckDoubleMaximum(context, d)) + return false; + + if (!multipleOf_.IsNull() && !CheckDoubleMultipleOf(context, d)) + return false; + + return CreateParallelValidator(context); + } + + bool String(Context& context, const Ch* str, SizeType length, bool) const { + RAPIDJSON_SCHEMA_PRINT(Method, "Schema::String", str); + if (!(type_ & (1 << kStringSchemaType))) { + DisallowedType(context, GetStringString()); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorType); + } + + if (minLength_ != 0 || maxLength_ != SizeType(~0)) { + SizeType count; + if (internal::CountStringCodePoint(str, length, &count)) { + if (count < minLength_) { + context.error_handler.TooShort(str, length, minLength_); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorMinLength); + } + if (count > maxLength_) { + context.error_handler.TooLong(str, length, maxLength_); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorMaxLength); + } + } + } + + if (pattern_ && !IsPatternMatch(pattern_, str, length)) { + context.error_handler.DoesNotMatch(str, length); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorPattern); + } + + return CreateParallelValidator(context); + } + + bool StartObject(Context& context) const { + RAPIDJSON_SCHEMA_PRINT(Method, "Schema::StartObject"); + if (!(type_ & (1 << kObjectSchemaType))) { + DisallowedType(context, GetObjectString()); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorType); + } + + if (hasDependencies_ || hasRequired_) { + context.propertyExist = static_cast(context.factory.MallocState(sizeof(bool) * propertyCount_)); + std::memset(context.propertyExist, 0, sizeof(bool) * propertyCount_); + } + + if (patternProperties_) { // pre-allocate schema array + SizeType count = patternPropertyCount_ + 1; // extra for valuePatternValidatorType + context.patternPropertiesSchemas = static_cast(context.factory.MallocState(sizeof(const SchemaType*) * count)); + context.patternPropertiesSchemaCount = 0; + std::memset(context.patternPropertiesSchemas, 0, sizeof(SchemaType*) * count); + } + + return CreateParallelValidator(context); + } + + bool Key(Context& context, const Ch* str, SizeType len, bool) const { + RAPIDJSON_SCHEMA_PRINT(Method, "Schema::Key", str); + + if (patternProperties_) { + context.patternPropertiesSchemaCount = 0; + for (SizeType i = 0; i < patternPropertyCount_; i++) + if (patternProperties_[i].pattern && IsPatternMatch(patternProperties_[i].pattern, str, len)) { + context.patternPropertiesSchemas[context.patternPropertiesSchemaCount++] = patternProperties_[i].schema; + context.valueSchema = typeless_; + } + } + + SizeType index = 0; + if (FindPropertyIndex(ValueType(str, len).Move(), &index)) { + if (context.patternPropertiesSchemaCount > 0) { + context.patternPropertiesSchemas[context.patternPropertiesSchemaCount++] = properties_[index].schema; + context.valueSchema = typeless_; + context.valuePatternValidatorType = Context::kPatternValidatorWithProperty; + } + else + context.valueSchema = properties_[index].schema; + + if (context.propertyExist) + context.propertyExist[index] = true; + + return true; + } + + if (additionalPropertiesSchema_) { + if (context.patternPropertiesSchemaCount > 0) { + context.patternPropertiesSchemas[context.patternPropertiesSchemaCount++] = additionalPropertiesSchema_; + context.valueSchema = typeless_; + context.valuePatternValidatorType = Context::kPatternValidatorWithAdditionalProperty; + } + else + context.valueSchema = additionalPropertiesSchema_; + return true; + } + else if (additionalProperties_) { + context.valueSchema = typeless_; + return true; + } + + if (context.patternPropertiesSchemaCount == 0) { // patternProperties are not additional properties + // Must set valueSchema for when kValidateContinueOnErrorFlag is set, else reports spurious type error + context.valueSchema = typeless_; + context.error_handler.DisallowedProperty(str, len); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorAdditionalProperties); + } + + return true; + } + + bool EndObject(Context& context, SizeType memberCount) const { + RAPIDJSON_SCHEMA_PRINT(Method, "Schema::EndObject"); + if (hasRequired_) { + context.error_handler.StartMissingProperties(); + for (SizeType index = 0; index < propertyCount_; index++) + if (properties_[index].required && !context.propertyExist[index]) + if (properties_[index].schema->defaultValueLength_ == 0 ) + context.error_handler.AddMissingProperty(properties_[index].name); + if (context.error_handler.EndMissingProperties()) + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorRequired); + } + + if (memberCount < minProperties_) { + context.error_handler.TooFewProperties(memberCount, minProperties_); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorMinProperties); + } + + if (memberCount > maxProperties_) { + context.error_handler.TooManyProperties(memberCount, maxProperties_); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorMaxProperties); + } + + if (hasDependencies_) { + context.error_handler.StartDependencyErrors(); + for (SizeType sourceIndex = 0; sourceIndex < propertyCount_; sourceIndex++) { + const Property& source = properties_[sourceIndex]; + if (context.propertyExist[sourceIndex]) { + if (source.dependencies) { + context.error_handler.StartMissingDependentProperties(); + for (SizeType targetIndex = 0; targetIndex < propertyCount_; targetIndex++) + if (source.dependencies[targetIndex] && !context.propertyExist[targetIndex]) + context.error_handler.AddMissingDependentProperty(properties_[targetIndex].name); + context.error_handler.EndMissingDependentProperties(source.name); + } + else if (source.dependenciesSchema) { + ISchemaValidator* dependenciesValidator = context.validators[source.dependenciesValidatorIndex]; + if (!dependenciesValidator->IsValid()) + context.error_handler.AddDependencySchemaError(source.name, dependenciesValidator); + } + } + } + if (context.error_handler.EndDependencyErrors()) + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorDependencies); + } + + return true; + } + + bool StartArray(Context& context) const { + RAPIDJSON_SCHEMA_PRINT(Method, "Schema::StartArray"); + context.arrayElementIndex = 0; + context.inArray = true; // Ensure we note that we are in an array + + if (!(type_ & (1 << kArraySchemaType))) { + DisallowedType(context, GetArrayString()); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorType); + } + + return CreateParallelValidator(context); + } + + bool EndArray(Context& context, SizeType elementCount) const { + RAPIDJSON_SCHEMA_PRINT(Method, "Schema::EndArray"); + context.inArray = false; + + if (elementCount < minItems_) { + context.error_handler.TooFewItems(elementCount, minItems_); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorMinItems); + } + + if (elementCount > maxItems_) { + context.error_handler.TooManyItems(elementCount, maxItems_); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorMaxItems); + } + + return true; + } + + static const ValueType& GetValidateErrorKeyword(ValidateErrorCode validateErrorCode) { + switch (validateErrorCode) { + case kValidateErrorMultipleOf: return GetMultipleOfString(); + case kValidateErrorMaximum: return GetMaximumString(); + case kValidateErrorExclusiveMaximum: return GetMaximumString(); // Same + case kValidateErrorMinimum: return GetMinimumString(); + case kValidateErrorExclusiveMinimum: return GetMinimumString(); // Same + + case kValidateErrorMaxLength: return GetMaxLengthString(); + case kValidateErrorMinLength: return GetMinLengthString(); + case kValidateErrorPattern: return GetPatternString(); + + case kValidateErrorMaxItems: return GetMaxItemsString(); + case kValidateErrorMinItems: return GetMinItemsString(); + case kValidateErrorUniqueItems: return GetUniqueItemsString(); + case kValidateErrorAdditionalItems: return GetAdditionalItemsString(); + + case kValidateErrorMaxProperties: return GetMaxPropertiesString(); + case kValidateErrorMinProperties: return GetMinPropertiesString(); + case kValidateErrorRequired: return GetRequiredString(); + case kValidateErrorAdditionalProperties: return GetAdditionalPropertiesString(); + case kValidateErrorPatternProperties: return GetPatternPropertiesString(); + case kValidateErrorDependencies: return GetDependenciesString(); + + case kValidateErrorEnum: return GetEnumString(); + case kValidateErrorType: return GetTypeString(); + + case kValidateErrorOneOf: return GetOneOfString(); + case kValidateErrorOneOfMatch: return GetOneOfString(); // Same + case kValidateErrorAllOf: return GetAllOfString(); + case kValidateErrorAnyOf: return GetAnyOfString(); + case kValidateErrorNot: return GetNotString(); + + case kValidateErrorReadOnly: return GetReadOnlyString(); + case kValidateErrorWriteOnly: return GetWriteOnlyString(); + + default: return GetNullString(); + } + } + + + // Generate functions for string literal according to Ch +#define RAPIDJSON_STRING_(name, ...) \ + static const ValueType& Get##name##String() {\ + static const Ch s[] = { __VA_ARGS__, '\0' };\ + static const ValueType v(s, static_cast(sizeof(s) / sizeof(Ch) - 1));\ + return v;\ + } + + RAPIDJSON_STRING_(Null, 'n', 'u', 'l', 'l') + RAPIDJSON_STRING_(Boolean, 'b', 'o', 'o', 'l', 'e', 'a', 'n') + RAPIDJSON_STRING_(Object, 'o', 'b', 'j', 'e', 'c', 't') + RAPIDJSON_STRING_(Array, 'a', 'r', 'r', 'a', 'y') + RAPIDJSON_STRING_(String, 's', 't', 'r', 'i', 'n', 'g') + RAPIDJSON_STRING_(Number, 'n', 'u', 'm', 'b', 'e', 'r') + RAPIDJSON_STRING_(Integer, 'i', 'n', 't', 'e', 'g', 'e', 'r') + RAPIDJSON_STRING_(Type, 't', 'y', 'p', 'e') + RAPIDJSON_STRING_(Enum, 'e', 'n', 'u', 'm') + RAPIDJSON_STRING_(AllOf, 'a', 'l', 'l', 'O', 'f') + RAPIDJSON_STRING_(AnyOf, 'a', 'n', 'y', 'O', 'f') + RAPIDJSON_STRING_(OneOf, 'o', 'n', 'e', 'O', 'f') + RAPIDJSON_STRING_(Not, 'n', 'o', 't') + RAPIDJSON_STRING_(Properties, 'p', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's') + RAPIDJSON_STRING_(Required, 'r', 'e', 'q', 'u', 'i', 'r', 'e', 'd') + RAPIDJSON_STRING_(Dependencies, 'd', 'e', 'p', 'e', 'n', 'd', 'e', 'n', 'c', 'i', 'e', 's') + RAPIDJSON_STRING_(PatternProperties, 'p', 'a', 't', 't', 'e', 'r', 'n', 'P', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's') + RAPIDJSON_STRING_(AdditionalProperties, 'a', 'd', 'd', 'i', 't', 'i', 'o', 'n', 'a', 'l', 'P', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's') + RAPIDJSON_STRING_(MinProperties, 'm', 'i', 'n', 'P', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's') + RAPIDJSON_STRING_(MaxProperties, 'm', 'a', 'x', 'P', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's') + RAPIDJSON_STRING_(Items, 'i', 't', 'e', 'm', 's') + RAPIDJSON_STRING_(MinItems, 'm', 'i', 'n', 'I', 't', 'e', 'm', 's') + RAPIDJSON_STRING_(MaxItems, 'm', 'a', 'x', 'I', 't', 'e', 'm', 's') + RAPIDJSON_STRING_(AdditionalItems, 'a', 'd', 'd', 'i', 't', 'i', 'o', 'n', 'a', 'l', 'I', 't', 'e', 'm', 's') + RAPIDJSON_STRING_(UniqueItems, 'u', 'n', 'i', 'q', 'u', 'e', 'I', 't', 'e', 'm', 's') + RAPIDJSON_STRING_(MinLength, 'm', 'i', 'n', 'L', 'e', 'n', 'g', 't', 'h') + RAPIDJSON_STRING_(MaxLength, 'm', 'a', 'x', 'L', 'e', 'n', 'g', 't', 'h') + RAPIDJSON_STRING_(Pattern, 'p', 'a', 't', 't', 'e', 'r', 'n') + RAPIDJSON_STRING_(Minimum, 'm', 'i', 'n', 'i', 'm', 'u', 'm') + RAPIDJSON_STRING_(Maximum, 'm', 'a', 'x', 'i', 'm', 'u', 'm') + RAPIDJSON_STRING_(ExclusiveMinimum, 'e', 'x', 'c', 'l', 'u', 's', 'i', 'v', 'e', 'M', 'i', 'n', 'i', 'm', 'u', 'm') + RAPIDJSON_STRING_(ExclusiveMaximum, 'e', 'x', 'c', 'l', 'u', 's', 'i', 'v', 'e', 'M', 'a', 'x', 'i', 'm', 'u', 'm') + RAPIDJSON_STRING_(MultipleOf, 'm', 'u', 'l', 't', 'i', 'p', 'l', 'e', 'O', 'f') + RAPIDJSON_STRING_(DefaultValue, 'd', 'e', 'f', 'a', 'u', 'l', 't') + RAPIDJSON_STRING_(Schema, '$', 's', 'c', 'h', 'e', 'm', 'a') + RAPIDJSON_STRING_(Ref, '$', 'r', 'e', 'f') + RAPIDJSON_STRING_(Id, 'i', 'd') + RAPIDJSON_STRING_(Swagger, 's', 'w', 'a', 'g', 'g', 'e', 'r') + RAPIDJSON_STRING_(OpenApi, 'o', 'p', 'e', 'n', 'a', 'p', 'i') + RAPIDJSON_STRING_(ReadOnly, 'r', 'e', 'a', 'd', 'O', 'n', 'l', 'y') + RAPIDJSON_STRING_(WriteOnly, 'w', 'r', 'i', 't', 'e', 'O', 'n', 'l', 'y') + RAPIDJSON_STRING_(Nullable, 'n', 'u', 'l', 'l', 'a', 'b', 'l', 'e') + +#undef RAPIDJSON_STRING_ + +private: + enum SchemaValueType { + kNullSchemaType, + kBooleanSchemaType, + kObjectSchemaType, + kArraySchemaType, + kStringSchemaType, + kNumberSchemaType, + kIntegerSchemaType, + kTotalSchemaType + }; + +#if RAPIDJSON_SCHEMA_USE_INTERNALREGEX + typedef internal::GenericRegex RegexType; +#elif RAPIDJSON_SCHEMA_USE_STDREGEX + typedef std::basic_regex RegexType; +#else + typedef char RegexType; +#endif + + struct SchemaArray { + SchemaArray() : schemas(), count() {} + ~SchemaArray() { AllocatorType::Free(schemas); } + const SchemaType** schemas; + SizeType begin; // begin index of context.validators + SizeType count; + }; + + template + void AddUniqueElement(V1& a, const V2& v) { + for (typename V1::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr) + if (*itr == v) + return; + V1 c(v, *allocator_); + a.PushBack(c, *allocator_); + } + + static const ValueType* GetMember(const ValueType& value, const ValueType& name) { + typename ValueType::ConstMemberIterator itr = value.FindMember(name); + return itr != value.MemberEnd() ? &(itr->value) : 0; + } + + static void AssignIfExist(bool& out, const ValueType& value, const ValueType& name) { + if (const ValueType* v = GetMember(value, name)) + if (v->IsBool()) + out = v->GetBool(); + } + + static void AssignIfExist(SizeType& out, const ValueType& value, const ValueType& name) { + if (const ValueType* v = GetMember(value, name)) + if (v->IsUint64() && v->GetUint64() <= SizeType(~0)) + out = static_cast(v->GetUint64()); + } + + void AssignIfExist(SchemaArray& out, SchemaDocumentType& schemaDocument, const PointerType& p, const ValueType& value, const ValueType& name, const ValueType& document) { + if (const ValueType* v = GetMember(value, name)) { + if (v->IsArray() && v->Size() > 0) { + PointerType q = p.Append(name, allocator_); + out.count = v->Size(); + out.schemas = static_cast(allocator_->Malloc(out.count * sizeof(const Schema*))); + memset(out.schemas, 0, sizeof(Schema*)* out.count); + for (SizeType i = 0; i < out.count; i++) + schemaDocument.CreateSchema(&out.schemas[i], q.Append(i, allocator_), (*v)[i], document, id_); + out.begin = validatorCount_; + validatorCount_ += out.count; + } + } + } + +#if RAPIDJSON_SCHEMA_USE_INTERNALREGEX + template + RegexType* CreatePattern(const ValueType& value, SchemaDocumentType* sd, const PointerType& p) { + if (value.IsString()) { + RegexType* r = new (allocator_->Malloc(sizeof(RegexType))) RegexType(value.GetString(), allocator_); + if (!r->IsValid()) { + sd->SchemaErrorValue(kSchemaErrorRegexInvalid, p, value.GetString(), value.GetStringLength()); + r->~RegexType(); + AllocatorType::Free(r); + r = 0; + } + return r; + } + return 0; + } + + static bool IsPatternMatch(const RegexType* pattern, const Ch *str, SizeType) { + GenericRegexSearch rs(*pattern); + return rs.Search(str); + } +#elif RAPIDJSON_SCHEMA_USE_STDREGEX + template + RegexType* CreatePattern(const ValueType& value, SchemaDocumentType* sd, const PointerType& p) { + if (value.IsString()) { + RegexType *r = static_cast(allocator_->Malloc(sizeof(RegexType))); + try { + return new (r) RegexType(value.GetString(), std::size_t(value.GetStringLength()), std::regex_constants::ECMAScript); + } + catch (const std::regex_error& e) { + sd->SchemaErrorValue(kSchemaErrorRegexInvalid, p, value.GetString(), value.GetStringLength()); + AllocatorType::Free(r); + } + } + return 0; + } + + static bool IsPatternMatch(const RegexType* pattern, const Ch *str, SizeType length) { + std::match_results r; + return std::regex_search(str, str + length, r, *pattern); + } +#else + template + RegexType* CreatePattern(const ValueType&) { + return 0; + } + + static bool IsPatternMatch(const RegexType*, const Ch *, SizeType) { return true; } +#endif // RAPIDJSON_SCHEMA_USE_STDREGEX + + void AddType(const ValueType& type) { + if (type == GetNullString() ) type_ |= 1 << kNullSchemaType; + else if (type == GetBooleanString()) type_ |= 1 << kBooleanSchemaType; + else if (type == GetObjectString() ) type_ |= 1 << kObjectSchemaType; + else if (type == GetArrayString() ) type_ |= 1 << kArraySchemaType; + else if (type == GetStringString() ) type_ |= 1 << kStringSchemaType; + else if (type == GetIntegerString()) type_ |= 1 << kIntegerSchemaType; + else if (type == GetNumberString() ) type_ |= (1 << kNumberSchemaType) | (1 << kIntegerSchemaType); + } + + // Creates parallel validators for allOf, anyOf, oneOf, not and schema dependencies, if required. + // Also creates a hasher for enums and array uniqueness, if required. + // Also a useful place to add type-independent error checks. + bool CreateParallelValidator(Context& context) const { + if (enum_ || context.arrayUniqueness) + context.hasher = context.factory.CreateHasher(); + + if (validatorCount_) { + RAPIDJSON_ASSERT(context.validators == 0); + context.validators = static_cast(context.factory.MallocState(sizeof(ISchemaValidator*) * validatorCount_)); + std::memset(context.validators, 0, sizeof(ISchemaValidator*) * validatorCount_); + context.validatorCount = validatorCount_; + + // Always return after first failure for these sub-validators + if (allOf_.schemas) + CreateSchemaValidators(context, allOf_, false); + + if (anyOf_.schemas) + CreateSchemaValidators(context, anyOf_, false); + + if (oneOf_.schemas) + CreateSchemaValidators(context, oneOf_, false); + + if (not_) + context.validators[notValidatorIndex_] = context.factory.CreateSchemaValidator(*not_, false); + + if (hasSchemaDependencies_) { + for (SizeType i = 0; i < propertyCount_; i++) + if (properties_[i].dependenciesSchema) + context.validators[properties_[i].dependenciesValidatorIndex] = context.factory.CreateSchemaValidator(*properties_[i].dependenciesSchema, false); + } + } + + // Add any other type-independent checks here + if (readOnly_ && (context.flags & kValidateWriteFlag)) { + context.error_handler.DisallowedWhenWriting(); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorReadOnly); + } + if (writeOnly_ && (context.flags & kValidateReadFlag)) { + context.error_handler.DisallowedWhenReading(); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorWriteOnly); + } + + return true; + } + + void CreateSchemaValidators(Context& context, const SchemaArray& schemas, const bool inheritContinueOnErrors) const { + for (SizeType i = 0; i < schemas.count; i++) + context.validators[schemas.begin + i] = context.factory.CreateSchemaValidator(*schemas.schemas[i], inheritContinueOnErrors); + } + + // O(n) + bool FindPropertyIndex(const ValueType& name, SizeType* outIndex) const { + SizeType len = name.GetStringLength(); + const Ch* str = name.GetString(); + for (SizeType index = 0; index < propertyCount_; index++) + if (properties_[index].name.GetStringLength() == len && + (std::memcmp(properties_[index].name.GetString(), str, sizeof(Ch) * len) == 0)) + { + *outIndex = index; + return true; + } + return false; + } + + bool CheckBool(Context& context, bool) const { + if (!(type_ & (1 << kBooleanSchemaType))) { + DisallowedType(context, GetBooleanString()); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorType); + } + return true; + } + + bool CheckInt(Context& context, int64_t i) const { + if (!(type_ & ((1 << kIntegerSchemaType) | (1 << kNumberSchemaType)))) { + DisallowedType(context, GetIntegerString()); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorType); + } + + if (!minimum_.IsNull()) { + if (minimum_.IsInt64()) { + if (exclusiveMinimum_ ? i <= minimum_.GetInt64() : i < minimum_.GetInt64()) { + context.error_handler.BelowMinimum(i, minimum_, exclusiveMinimum_); + RAPIDJSON_INVALID_KEYWORD_RETURN(exclusiveMinimum_ ? kValidateErrorExclusiveMinimum : kValidateErrorMinimum); + } + } + else if (minimum_.IsUint64()) { + context.error_handler.BelowMinimum(i, minimum_, exclusiveMinimum_); + RAPIDJSON_INVALID_KEYWORD_RETURN(exclusiveMinimum_ ? kValidateErrorExclusiveMinimum : kValidateErrorMinimum); // i <= max(int64_t) < minimum.GetUint64() + } + else if (!CheckDoubleMinimum(context, static_cast(i))) + return false; + } + + if (!maximum_.IsNull()) { + if (maximum_.IsInt64()) { + if (exclusiveMaximum_ ? i >= maximum_.GetInt64() : i > maximum_.GetInt64()) { + context.error_handler.AboveMaximum(i, maximum_, exclusiveMaximum_); + RAPIDJSON_INVALID_KEYWORD_RETURN(exclusiveMaximum_ ? kValidateErrorExclusiveMaximum : kValidateErrorMaximum); + } + } + else if (maximum_.IsUint64()) { } + /* do nothing */ // i <= max(int64_t) < maximum_.GetUint64() + else if (!CheckDoubleMaximum(context, static_cast(i))) + return false; + } + + if (!multipleOf_.IsNull()) { + if (multipleOf_.IsUint64()) { + if (static_cast(i >= 0 ? i : -i) % multipleOf_.GetUint64() != 0) { + context.error_handler.NotMultipleOf(i, multipleOf_); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorMultipleOf); + } + } + else if (!CheckDoubleMultipleOf(context, static_cast(i))) + return false; + } + + return true; + } + + bool CheckUint(Context& context, uint64_t i) const { + if (!(type_ & ((1 << kIntegerSchemaType) | (1 << kNumberSchemaType)))) { + DisallowedType(context, GetIntegerString()); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorType); + } + + if (!minimum_.IsNull()) { + if (minimum_.IsUint64()) { + if (exclusiveMinimum_ ? i <= minimum_.GetUint64() : i < minimum_.GetUint64()) { + context.error_handler.BelowMinimum(i, minimum_, exclusiveMinimum_); + RAPIDJSON_INVALID_KEYWORD_RETURN(exclusiveMinimum_ ? kValidateErrorExclusiveMinimum : kValidateErrorMinimum); + } + } + else if (minimum_.IsInt64()) + /* do nothing */; // i >= 0 > minimum.Getint64() + else if (!CheckDoubleMinimum(context, static_cast(i))) + return false; + } + + if (!maximum_.IsNull()) { + if (maximum_.IsUint64()) { + if (exclusiveMaximum_ ? i >= maximum_.GetUint64() : i > maximum_.GetUint64()) { + context.error_handler.AboveMaximum(i, maximum_, exclusiveMaximum_); + RAPIDJSON_INVALID_KEYWORD_RETURN(exclusiveMaximum_ ? kValidateErrorExclusiveMaximum : kValidateErrorMaximum); + } + } + else if (maximum_.IsInt64()) { + context.error_handler.AboveMaximum(i, maximum_, exclusiveMaximum_); + RAPIDJSON_INVALID_KEYWORD_RETURN(exclusiveMaximum_ ? kValidateErrorExclusiveMaximum : kValidateErrorMaximum); // i >= 0 > maximum_ + } + else if (!CheckDoubleMaximum(context, static_cast(i))) + return false; + } + + if (!multipleOf_.IsNull()) { + if (multipleOf_.IsUint64()) { + if (i % multipleOf_.GetUint64() != 0) { + context.error_handler.NotMultipleOf(i, multipleOf_); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorMultipleOf); + } + } + else if (!CheckDoubleMultipleOf(context, static_cast(i))) + return false; + } + + return true; + } + + bool CheckDoubleMinimum(Context& context, double d) const { + if (exclusiveMinimum_ ? d <= minimum_.GetDouble() : d < minimum_.GetDouble()) { + context.error_handler.BelowMinimum(d, minimum_, exclusiveMinimum_); + RAPIDJSON_INVALID_KEYWORD_RETURN(exclusiveMinimum_ ? kValidateErrorExclusiveMinimum : kValidateErrorMinimum); + } + return true; + } + + bool CheckDoubleMaximum(Context& context, double d) const { + if (exclusiveMaximum_ ? d >= maximum_.GetDouble() : d > maximum_.GetDouble()) { + context.error_handler.AboveMaximum(d, maximum_, exclusiveMaximum_); + RAPIDJSON_INVALID_KEYWORD_RETURN(exclusiveMaximum_ ? kValidateErrorExclusiveMaximum : kValidateErrorMaximum); + } + return true; + } + + bool CheckDoubleMultipleOf(Context& context, double d) const { + double a = std::abs(d), b = std::abs(multipleOf_.GetDouble()); + double q = std::floor(a / b); + double r = a - q * b; + if (r > 0.0) { + context.error_handler.NotMultipleOf(d, multipleOf_); + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorMultipleOf); + } + return true; + } + + void DisallowedType(Context& context, const ValueType& actualType) const { + ErrorHandler& eh = context.error_handler; + eh.StartDisallowedType(); + + if (type_ & (1 << kNullSchemaType)) eh.AddExpectedType(GetNullString()); + if (type_ & (1 << kBooleanSchemaType)) eh.AddExpectedType(GetBooleanString()); + if (type_ & (1 << kObjectSchemaType)) eh.AddExpectedType(GetObjectString()); + if (type_ & (1 << kArraySchemaType)) eh.AddExpectedType(GetArrayString()); + if (type_ & (1 << kStringSchemaType)) eh.AddExpectedType(GetStringString()); + + if (type_ & (1 << kNumberSchemaType)) eh.AddExpectedType(GetNumberString()); + else if (type_ & (1 << kIntegerSchemaType)) eh.AddExpectedType(GetIntegerString()); + + eh.EndDisallowedType(actualType); + } + + struct Property { + Property() : schema(), dependenciesSchema(), dependenciesValidatorIndex(), dependencies(), required(false) {} + ~Property() { AllocatorType::Free(dependencies); } + SValue name; + const SchemaType* schema; + const SchemaType* dependenciesSchema; + SizeType dependenciesValidatorIndex; + bool* dependencies; + bool required; + }; + + struct PatternProperty { + PatternProperty() : schema(), pattern() {} + ~PatternProperty() { + if (pattern) { + pattern->~RegexType(); + AllocatorType::Free(pattern); + } + } + const SchemaType* schema; + RegexType* pattern; + }; + + AllocatorType* allocator_; + SValue uri_; + UriType id_; + Specification spec_; + PointerType pointer_; + const SchemaType* typeless_; + uint64_t* enum_; + SizeType enumCount_; + SchemaArray allOf_; + SchemaArray anyOf_; + SchemaArray oneOf_; + const SchemaType* not_; + unsigned type_; // bitmask of kSchemaType + SizeType validatorCount_; + SizeType notValidatorIndex_; + + Property* properties_; + const SchemaType* additionalPropertiesSchema_; + PatternProperty* patternProperties_; + SizeType patternPropertyCount_; + SizeType propertyCount_; + SizeType minProperties_; + SizeType maxProperties_; + bool additionalProperties_; + bool hasDependencies_; + bool hasRequired_; + bool hasSchemaDependencies_; + + const SchemaType* additionalItemsSchema_; + const SchemaType* itemsList_; + const SchemaType** itemsTuple_; + SizeType itemsTupleCount_; + SizeType minItems_; + SizeType maxItems_; + bool additionalItems_; + bool uniqueItems_; + + RegexType* pattern_; + SizeType minLength_; + SizeType maxLength_; + + SValue minimum_; + SValue maximum_; + SValue multipleOf_; + bool exclusiveMinimum_; + bool exclusiveMaximum_; + + SizeType defaultValueLength_; + + bool readOnly_; + bool writeOnly_; + bool nullable_; +}; + +template +struct TokenHelper { + RAPIDJSON_FORCEINLINE static void AppendIndexToken(Stack& documentStack, SizeType index) { + *documentStack.template Push() = '/'; + char buffer[21]; + size_t length = static_cast((sizeof(SizeType) == 4 ? u32toa(index, buffer) : u64toa(index, buffer)) - buffer); + for (size_t i = 0; i < length; i++) + *documentStack.template Push() = static_cast(buffer[i]); + } +}; + +// Partial specialized version for char to prevent buffer copying. +template +struct TokenHelper { + RAPIDJSON_FORCEINLINE static void AppendIndexToken(Stack& documentStack, SizeType index) { + if (sizeof(SizeType) == 4) { + char *buffer = documentStack.template Push(1 + 10); // '/' + uint + *buffer++ = '/'; + const char* end = internal::u32toa(index, buffer); + documentStack.template Pop(static_cast(10 - (end - buffer))); + } + else { + char *buffer = documentStack.template Push(1 + 20); // '/' + uint64 + *buffer++ = '/'; + const char* end = internal::u64toa(index, buffer); + documentStack.template Pop(static_cast(20 - (end - buffer))); + } + } +}; + +} // namespace internal + +/////////////////////////////////////////////////////////////////////////////// +// IGenericRemoteSchemaDocumentProvider + +template +class IGenericRemoteSchemaDocumentProvider { +public: + typedef typename SchemaDocumentType::Ch Ch; + typedef typename SchemaDocumentType::ValueType ValueType; + typedef typename SchemaDocumentType::AllocatorType AllocatorType; + + virtual ~IGenericRemoteSchemaDocumentProvider() {} + virtual const SchemaDocumentType* GetRemoteDocument(const Ch* uri, SizeType length) = 0; + virtual const SchemaDocumentType* GetRemoteDocument(const GenericUri uri, Specification& spec) { + // Default implementation just calls through for compatibility + // Following line suppresses unused parameter warning + (void)spec; + // printf("GetRemoteDocument: %d %d\n", spec.draft, spec.oapi); + return GetRemoteDocument(uri.GetBaseString(), uri.GetBaseStringLength()); + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// GenericSchemaDocument + +//! JSON schema document. +/*! + A JSON schema document is a compiled version of a JSON schema. + It is basically a tree of internal::Schema. + + \note This is an immutable class (i.e. its instance cannot be modified after construction). + \tparam ValueT Type of JSON value (e.g. \c Value ), which also determine the encoding. + \tparam Allocator Allocator type for allocating memory of this document. +*/ +template +class GenericSchemaDocument { +public: + typedef ValueT ValueType; + typedef IGenericRemoteSchemaDocumentProvider IRemoteSchemaDocumentProviderType; + typedef Allocator AllocatorType; + typedef typename ValueType::EncodingType EncodingType; + typedef typename EncodingType::Ch Ch; + typedef internal::Schema SchemaType; + typedef GenericPointer PointerType; + typedef GenericValue GValue; + typedef GenericUri UriType; + typedef GenericStringRef StringRefType; + friend class internal::Schema; + template + friend class GenericSchemaValidator; + + //! Constructor. + /*! + Compile a JSON document into schema document. + + \param document A JSON document as source. + \param uri The base URI of this schema document for purposes of violation reporting. + \param uriLength Length of \c name, in code points. + \param remoteProvider An optional remote schema document provider for resolving remote reference. Can be null. + \param allocator An optional allocator instance for allocating memory. Can be null. + \param pointer An optional JSON pointer to the start of the schema document + \param spec Optional schema draft or OpenAPI version. Used if no specification in document. Defaults to draft-04. + */ + explicit GenericSchemaDocument(const ValueType& document, const Ch* uri = 0, SizeType uriLength = 0, + IRemoteSchemaDocumentProviderType* remoteProvider = 0, Allocator* allocator = 0, + const PointerType& pointer = PointerType(), // PR #1393 + const Specification& spec = Specification(kDraft04)) : + remoteProvider_(remoteProvider), + allocator_(allocator), + ownAllocator_(), + root_(), + typeless_(), + schemaMap_(allocator, kInitialSchemaMapSize), + schemaRef_(allocator, kInitialSchemaRefSize), + spec_(spec), + error_(kObjectType), + currentError_() + { + RAPIDJSON_SCHEMA_PRINT(Method, "GenericSchemaDocument::GenericSchemaDocument"); + if (!allocator_) + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)(); + + Ch noUri[1] = {0}; + uri_.SetString(uri ? uri : noUri, uriLength, *allocator_); + docId_ = UriType(uri_, allocator_); + + typeless_ = static_cast(allocator_->Malloc(sizeof(SchemaType))); + new (typeless_) SchemaType(this, PointerType(), ValueType(kObjectType).Move(), ValueType(kObjectType).Move(), allocator_, docId_); + + // Establish the schema draft or open api version. + // We only ever look for '$schema' or 'swagger' or 'openapi' at the root of the document. + SetSchemaSpecification(document); + + // Generate root schema, it will call CreateSchema() to create sub-schemas, + // And call HandleRefSchema() if there are $ref. + // PR #1393 use input pointer if supplied + root_ = typeless_; + if (pointer.GetTokenCount() == 0) { + CreateSchemaRecursive(&root_, pointer, document, document, docId_); + } + else if (const ValueType* v = pointer.Get(document)) { + CreateSchema(&root_, pointer, *v, document, docId_); + } + else { + GenericStringBuffer sb; + pointer.StringifyUriFragment(sb); + SchemaErrorValue(kSchemaErrorStartUnknown, PointerType(), sb.GetString(), static_cast(sb.GetSize() / sizeof(Ch))); + } + + RAPIDJSON_ASSERT(root_ != 0); + + schemaRef_.ShrinkToFit(); // Deallocate all memory for ref + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move constructor in C++11 + GenericSchemaDocument(GenericSchemaDocument&& rhs) RAPIDJSON_NOEXCEPT : + remoteProvider_(rhs.remoteProvider_), + allocator_(rhs.allocator_), + ownAllocator_(rhs.ownAllocator_), + root_(rhs.root_), + typeless_(rhs.typeless_), + schemaMap_(std::move(rhs.schemaMap_)), + schemaRef_(std::move(rhs.schemaRef_)), + uri_(std::move(rhs.uri_)), + docId_(std::move(rhs.docId_)), + spec_(rhs.spec_), + error_(std::move(rhs.error_)), + currentError_(std::move(rhs.currentError_)) + { + rhs.remoteProvider_ = 0; + rhs.allocator_ = 0; + rhs.ownAllocator_ = 0; + rhs.typeless_ = 0; + } +#endif + + //! Destructor + ~GenericSchemaDocument() { + while (!schemaMap_.Empty()) + schemaMap_.template Pop(1)->~SchemaEntry(); + + if (typeless_) { + typeless_->~SchemaType(); + Allocator::Free(typeless_); + } + + // these may contain some allocator data so clear before deleting ownAllocator_ + uri_.SetNull(); + error_.SetNull(); + currentError_.SetNull(); + + RAPIDJSON_DELETE(ownAllocator_); + } + + const GValue& GetURI() const { return uri_; } + + const Specification& GetSpecification() const { return spec_; } + bool IsSupportedSpecification() const { return spec_.IsSupported(); } + + //! Static method to get the specification of any schema document + // Returns kDraftNone if document is silent + static const Specification GetSpecification(const ValueType& document) { + SchemaDraft draft = GetSchemaDraft(document); + if (draft != kDraftNone) + return Specification(draft); + else { + OpenApiVersion oapi = GetOpenApiVersion(document); + if (oapi != kVersionNone) + return Specification(oapi); + } + return Specification(kDraftNone); + } + + //! Get the root schema. + const SchemaType& GetRoot() const { return *root_; } + + //! Gets the error object. + GValue& GetError() { return error_; } + const GValue& GetError() const { return error_; } + + static const StringRefType& GetSchemaErrorKeyword(SchemaErrorCode schemaErrorCode) { + switch (schemaErrorCode) { + case kSchemaErrorStartUnknown: return GetStartUnknownString(); + case kSchemaErrorRefPlainName: return GetRefPlainNameString(); + case kSchemaErrorRefInvalid: return GetRefInvalidString(); + case kSchemaErrorRefPointerInvalid: return GetRefPointerInvalidString(); + case kSchemaErrorRefUnknown: return GetRefUnknownString(); + case kSchemaErrorRefCyclical: return GetRefCyclicalString(); + case kSchemaErrorRefNoRemoteProvider: return GetRefNoRemoteProviderString(); + case kSchemaErrorRefNoRemoteSchema: return GetRefNoRemoteSchemaString(); + case kSchemaErrorRegexInvalid: return GetRegexInvalidString(); + case kSchemaErrorSpecUnknown: return GetSpecUnknownString(); + case kSchemaErrorSpecUnsupported: return GetSpecUnsupportedString(); + case kSchemaErrorSpecIllegal: return GetSpecIllegalString(); + case kSchemaErrorReadOnlyAndWriteOnly: return GetReadOnlyAndWriteOnlyString(); + default: return GetNullString(); + } + } + + //! Default error method + void SchemaError(const SchemaErrorCode code, const PointerType& location) { + currentError_ = GValue(kObjectType); + AddCurrentError(code, location); + } + + //! Method for error with single string value insert + void SchemaErrorValue(const SchemaErrorCode code, const PointerType& location, const Ch* value, SizeType length) { + currentError_ = GValue(kObjectType); + currentError_.AddMember(GetValueString(), GValue(value, length, *allocator_).Move(), *allocator_); + AddCurrentError(code, location); + } + + //! Method for error with invalid pointer + void SchemaErrorPointer(const SchemaErrorCode code, const PointerType& location, const Ch* value, SizeType length, const PointerType& pointer) { + currentError_ = GValue(kObjectType); + currentError_.AddMember(GetValueString(), GValue(value, length, *allocator_).Move(), *allocator_); + currentError_.AddMember(GetOffsetString(), static_cast(pointer.GetParseErrorOffset() / sizeof(Ch)), *allocator_); + AddCurrentError(code, location); + } + + private: + //! Prohibit copying + GenericSchemaDocument(const GenericSchemaDocument&); + //! Prohibit assignment + GenericSchemaDocument& operator=(const GenericSchemaDocument&); + + typedef const PointerType* SchemaRefPtr; // PR #1393 + + struct SchemaEntry { + SchemaEntry(const PointerType& p, SchemaType* s, bool o, Allocator* allocator) : pointer(p, allocator), schema(s), owned(o) {} + ~SchemaEntry() { + if (owned) { + schema->~SchemaType(); + Allocator::Free(schema); + } + } + PointerType pointer; + SchemaType* schema; + bool owned; + }; + + void AddErrorInstanceLocation(GValue& result, const PointerType& location) { + GenericStringBuffer sb; + location.StringifyUriFragment(sb); + GValue instanceRef(sb.GetString(), static_cast(sb.GetSize() / sizeof(Ch)), *allocator_); + result.AddMember(GetInstanceRefString(), instanceRef, *allocator_); + } + + void AddError(GValue& keyword, GValue& error) { + typename GValue::MemberIterator member = error_.FindMember(keyword); + if (member == error_.MemberEnd()) + error_.AddMember(keyword, error, *allocator_); + else { + if (member->value.IsObject()) { + GValue errors(kArrayType); + errors.PushBack(member->value, *allocator_); + member->value = errors; + } + member->value.PushBack(error, *allocator_); + } + } + + void AddCurrentError(const SchemaErrorCode code, const PointerType& location) { + RAPIDJSON_SCHEMA_PRINT(InvalidKeyword, GetSchemaErrorKeyword(code)); + currentError_.AddMember(GetErrorCodeString(), code, *allocator_); + AddErrorInstanceLocation(currentError_, location); + AddError(GValue(GetSchemaErrorKeyword(code)).Move(), currentError_); + } + +#define RAPIDJSON_STRING_(name, ...) \ + static const StringRefType& Get##name##String() {\ + static const Ch s[] = { __VA_ARGS__, '\0' };\ + static const StringRefType v(s, static_cast(sizeof(s) / sizeof(Ch) - 1)); \ + return v;\ + } + + RAPIDJSON_STRING_(InstanceRef, 'i', 'n', 's', 't', 'a', 'n', 'c', 'e', 'R', 'e', 'f') + RAPIDJSON_STRING_(ErrorCode, 'e', 'r', 'r', 'o', 'r', 'C', 'o', 'd', 'e') + RAPIDJSON_STRING_(Value, 'v', 'a', 'l', 'u', 'e') + RAPIDJSON_STRING_(Offset, 'o', 'f', 'f', 's', 'e', 't') + + RAPIDJSON_STRING_(Null, 'n', 'u', 'l', 'l') + RAPIDJSON_STRING_(SpecUnknown, 'S', 'p', 'e', 'c', 'U', 'n', 'k', 'n', 'o', 'w', 'n') + RAPIDJSON_STRING_(SpecUnsupported, 'S', 'p', 'e', 'c', 'U', 'n', 's', 'u', 'p', 'p', 'o', 'r', 't', 'e', 'd') + RAPIDJSON_STRING_(SpecIllegal, 'S', 'p', 'e', 'c', 'I', 'l', 'l', 'e', 'g', 'a', 'l') + RAPIDJSON_STRING_(StartUnknown, 'S', 't', 'a', 'r', 't', 'U', 'n', 'k', 'n', 'o', 'w', 'n') + RAPIDJSON_STRING_(RefPlainName, 'R', 'e', 'f', 'P', 'l', 'a', 'i', 'n', 'N', 'a', 'm', 'e') + RAPIDJSON_STRING_(RefInvalid, 'R', 'e', 'f', 'I', 'n', 'v', 'a', 'l', 'i', 'd') + RAPIDJSON_STRING_(RefPointerInvalid, 'R', 'e', 'f', 'P', 'o', 'i', 'n', 't', 'e', 'r', 'I', 'n', 'v', 'a', 'l', 'i', 'd') + RAPIDJSON_STRING_(RefUnknown, 'R', 'e', 'f', 'U', 'n', 'k', 'n', 'o', 'w', 'n') + RAPIDJSON_STRING_(RefCyclical, 'R', 'e', 'f', 'C', 'y', 'c', 'l', 'i', 'c', 'a', 'l') + RAPIDJSON_STRING_(RefNoRemoteProvider, 'R', 'e', 'f', 'N', 'o', 'R', 'e', 'm', 'o', 't', 'e', 'P', 'r', 'o', 'v', 'i', 'd', 'e', 'r') + RAPIDJSON_STRING_(RefNoRemoteSchema, 'R', 'e', 'f', 'N', 'o', 'R', 'e', 'm', 'o', 't', 'e', 'S', 'c', 'h', 'e', 'm', 'a') + RAPIDJSON_STRING_(ReadOnlyAndWriteOnly, 'R', 'e', 'a', 'd', 'O', 'n', 'l', 'y', 'A', 'n', 'd', 'W', 'r', 'i', 't', 'e', 'O', 'n', 'l', 'y') + RAPIDJSON_STRING_(RegexInvalid, 'R', 'e', 'g', 'e', 'x', 'I', 'n', 'v', 'a', 'l', 'i', 'd') + +#undef RAPIDJSON_STRING_ + + // Static method to get schema draft of any schema document + static SchemaDraft GetSchemaDraft(const ValueType& document) { + static const Ch kDraft03String[] = { 'h', 't', 't', 'p', ':', '/', '/', 'j', 's', 'o', 'n', '-', 's', 'c', 'h', 'e', 'm', 'a', '.', 'o', 'r', 'g', '/', 'd', 'r', 'a', 'f', 't', '-', '0', '3', '/', 's', 'c', 'h', 'e', 'm', 'a', '#', '\0' }; + static const Ch kDraft04String[] = { 'h', 't', 't', 'p', ':', '/', '/', 'j', 's', 'o', 'n', '-', 's', 'c', 'h', 'e', 'm', 'a', '.', 'o', 'r', 'g', '/', 'd', 'r', 'a', 'f', 't', '-', '0', '4', '/', 's', 'c', 'h', 'e', 'm', 'a', '#', '\0' }; + static const Ch kDraft05String[] = { 'h', 't', 't', 'p', ':', '/', '/', 'j', 's', 'o', 'n', '-', 's', 'c', 'h', 'e', 'm', 'a', '.', 'o', 'r', 'g', '/', 'd', 'r', 'a', 'f', 't', '-', '0', '5', '/', 's', 'c', 'h', 'e', 'm', 'a', '#', '\0' }; + static const Ch kDraft06String[] = { 'h', 't', 't', 'p', ':', '/', '/', 'j', 's', 'o', 'n', '-', 's', 'c', 'h', 'e', 'm', 'a', '.', 'o', 'r', 'g', '/', 'd', 'r', 'a', 'f', 't', '-', '0', '6', '/', 's', 'c', 'h', 'e', 'm', 'a', '#', '\0' }; + static const Ch kDraft07String[] = { 'h', 't', 't', 'p', ':', '/', '/', 'j', 's', 'o', 'n', '-', 's', 'c', 'h', 'e', 'm', 'a', '.', 'o', 'r', 'g', '/', 'd', 'r', 'a', 'f', 't', '-', '0', '7', '/', 's', 'c', 'h', 'e', 'm', 'a', '#', '\0' }; + static const Ch kDraft2019_09String[] = { 'h', 't', 't', 'p', 's', ':', '/', '/', 'j', 's', 'o', 'n', '-', 's', 'c', 'h', 'e', 'm', 'a', '.', 'o', 'r', 'g', '/', 'd', 'r', 'a', 'f', 't', '/', '2', '0', '1', '9', '-', '0', '9', '/', 's', 'c', 'h', 'e', 'm', 'a', '\0' }; + static const Ch kDraft2020_12String[] = { 'h', 't', 't', 'p', 's', ':', '/', '/', 'j', 's', 'o', 'n', '-', 's', 'c', 'h', 'e', 'm', 'a', '.', 'o', 'r', 'g', '/', 'd', 'r', 'a', 'f', 't', '/', '2', '0', '2', '0', '-', '1', '2', '/', 's', 'c', 'h', 'e', 'm', 'a', '\0' }; + + if (!document.IsObject()) { + return kDraftNone; + } + + // Get the schema draft from the $schema keyword at the supplied location + typename ValueType::ConstMemberIterator itr = document.FindMember(SchemaType::GetSchemaString()); + if (itr != document.MemberEnd()) { + if (!itr->value.IsString()) return kDraftUnknown; + const UriType draftUri(itr->value); + // Check base uri for match + if (draftUri.Match(UriType(kDraft04String), false)) return kDraft04; + if (draftUri.Match(UriType(kDraft05String), false)) return kDraft05; + if (draftUri.Match(UriType(kDraft06String), false)) return kDraft06; + if (draftUri.Match(UriType(kDraft07String), false)) return kDraft07; + if (draftUri.Match(UriType(kDraft03String), false)) return kDraft03; + if (draftUri.Match(UriType(kDraft2019_09String), false)) return kDraft2019_09; + if (draftUri.Match(UriType(kDraft2020_12String), false)) return kDraft2020_12; + return kDraftUnknown; + } + // $schema not found + return kDraftNone; + } + + + // Get open api version of any schema document + static OpenApiVersion GetOpenApiVersion(const ValueType& document) { + static const Ch kVersion20String[] = { '2', '.', '0', '\0' }; + static const Ch kVersion30String[] = { '3', '.', '0', '.', '\0' }; // ignore patch level + static const Ch kVersion31String[] = { '3', '.', '1', '.', '\0' }; // ignore patch level + static SizeType len = internal::StrLen(kVersion30String); + + if (!document.IsObject()) { + return kVersionNone; + } + + // Get the open api version from the swagger / openapi keyword at the supplied location + typename ValueType::ConstMemberIterator itr = document.FindMember(SchemaType::GetSwaggerString()); + if (itr == document.MemberEnd()) itr = document.FindMember(SchemaType::GetOpenApiString()); + if (itr != document.MemberEnd()) { + if (!itr->value.IsString()) return kVersionUnknown; + const ValueType kVersion20Value(kVersion20String); + if (kVersion20Value == itr->value) return kVersion20; // must match 2.0 exactly + const ValueType kVersion30Value(kVersion30String); + if (itr->value.GetStringLength() > len && kVersion30Value == ValueType(itr->value.GetString(), len)) return kVersion30; // must match 3.0.x + const ValueType kVersion31Value(kVersion31String); + if (itr->value.GetStringLength() > len && kVersion31Value == ValueType(itr->value.GetString(), len)) return kVersion31; // must match 3.1.x + return kVersionUnknown; + } + // swagger or openapi not found + return kVersionNone; + } + + // Get the draft of the schema or the open api version (which implies the draft). + // Report an error if schema draft or open api version not supported or not recognized, or both in document, and carry on. + void SetSchemaSpecification(const ValueType& document) { + // Look for '$schema', 'swagger' or 'openapi' keyword at document root + SchemaDraft docDraft = GetSchemaDraft(document); + OpenApiVersion docOapi = GetOpenApiVersion(document); + // Error if both in document + if (docDraft != kDraftNone && docOapi != kVersionNone) + SchemaError(kSchemaErrorSpecIllegal, PointerType()); + // Use document draft or open api version if present or use spec from constructor + if (docDraft != kDraftNone) + spec_ = Specification(docDraft); + else if (docOapi != kVersionNone) + spec_ = Specification(docOapi); + // Error if draft or version unknown + if (spec_.draft == kDraftUnknown || spec_.oapi == kVersionUnknown) + SchemaError(kSchemaErrorSpecUnknown, PointerType()); + else if (!spec_.IsSupported()) + SchemaError(kSchemaErrorSpecUnsupported, PointerType()); + } + + // Changed by PR #1393 + void CreateSchemaRecursive(const SchemaType** schema, const PointerType& pointer, const ValueType& v, const ValueType& document, const UriType& id) { + if (v.GetType() == kObjectType) { + UriType newid = UriType(CreateSchema(schema, pointer, v, document, id), allocator_); + + for (typename ValueType::ConstMemberIterator itr = v.MemberBegin(); itr != v.MemberEnd(); ++itr) + CreateSchemaRecursive(0, pointer.Append(itr->name, allocator_), itr->value, document, newid); + } + else if (v.GetType() == kArrayType) + for (SizeType i = 0; i < v.Size(); i++) + CreateSchemaRecursive(0, pointer.Append(i, allocator_), v[i], document, id); + } + + // Changed by PR #1393 + const UriType& CreateSchema(const SchemaType** schema, const PointerType& pointer, const ValueType& v, const ValueType& document, const UriType& id) { + RAPIDJSON_ASSERT(pointer.IsValid()); + GenericStringBuffer sb; + pointer.StringifyUriFragment(sb); + RAPIDJSON_SCHEMA_PRINT(Method, "GenericSchemaDocument::CreateSchema", sb.GetString(), id.GetString()); + if (v.IsObject()) { + if (const SchemaType* sc = GetSchema(pointer)) { + if (schema) + *schema = sc; + AddSchemaRefs(const_cast(sc)); + } + else if (!HandleRefSchema(pointer, schema, v, document, id)) { + // The new schema constructor adds itself and its $ref(s) to schemaMap_ + SchemaType* s = new (allocator_->Malloc(sizeof(SchemaType))) SchemaType(this, pointer, v, document, allocator_, id); + if (schema) + *schema = s; + return s->GetId(); + } + } + else { + if (schema) + *schema = typeless_; + AddSchemaRefs(typeless_); + } + return id; + } + + // Changed by PR #1393 + // TODO should this return a UriType& ? + bool HandleRefSchema(const PointerType& source, const SchemaType** schema, const ValueType& v, const ValueType& document, const UriType& id) { + typename ValueType::ConstMemberIterator itr = v.FindMember(SchemaType::GetRefString()); + if (itr == v.MemberEnd()) + return false; + + GenericStringBuffer sb; + source.StringifyUriFragment(sb); + RAPIDJSON_SCHEMA_PRINT(Method, "GenericSchemaDocument::HandleRefSchema", sb.GetString(), id.GetString()); + // Resolve the source pointer to the $ref'ed schema (finally) + new (schemaRef_.template Push()) SchemaRefPtr(&source); + + if (itr->value.IsString()) { + SizeType len = itr->value.GetStringLength(); + if (len == 0) + SchemaError(kSchemaErrorRefInvalid, source); + else { + // First resolve $ref against the in-scope id + UriType scopeId = UriType(id, allocator_); + UriType ref = UriType(itr->value, allocator_).Resolve(scopeId, allocator_); + RAPIDJSON_SCHEMA_PRINT(SchemaIds, id.GetString(), itr->value.GetString(), ref.GetString()); + // See if the resolved $ref minus the fragment matches a resolved id in this document + // Search from the root. Returns the subschema in the document and its absolute JSON pointer. + PointerType basePointer = PointerType(); + const ValueType *base = FindId(document, ref, basePointer, docId_, false); + if (!base) { + // Remote reference - call the remote document provider + if (!remoteProvider_) + SchemaError(kSchemaErrorRefNoRemoteProvider, source); + else { + if (const GenericSchemaDocument* remoteDocument = remoteProvider_->GetRemoteDocument(ref, spec_)) { + const Ch* s = ref.GetFragString(); + len = ref.GetFragStringLength(); + if (len <= 1 || s[1] == '/') { + // JSON pointer fragment, absolute in the remote schema + const PointerType pointer(s, len, allocator_); + if (!pointer.IsValid()) + SchemaErrorPointer(kSchemaErrorRefPointerInvalid, source, s, len, pointer); + else { + // Get the subschema + if (const SchemaType *sc = remoteDocument->GetSchema(pointer)) { + if (schema) + *schema = sc; + AddSchemaRefs(const_cast(sc)); + return true; + } else + SchemaErrorValue(kSchemaErrorRefUnknown, source, ref.GetString(), ref.GetStringLength()); + } + } else + // Plain name fragment, not allowed in remote schema + SchemaErrorValue(kSchemaErrorRefPlainName, source, s, len); + } else + SchemaErrorValue(kSchemaErrorRefNoRemoteSchema, source, ref.GetString(), ref.GetStringLength()); + } + } + else { // Local reference + const Ch* s = ref.GetFragString(); + len = ref.GetFragStringLength(); + if (len <= 1 || s[1] == '/') { + // JSON pointer fragment, relative to the resolved URI + const PointerType relPointer(s, len, allocator_); + if (!relPointer.IsValid()) + SchemaErrorPointer(kSchemaErrorRefPointerInvalid, source, s, len, relPointer); + else { + // Get the subschema + if (const ValueType *pv = relPointer.Get(*base)) { + // Now get the absolute JSON pointer by adding relative to base + PointerType pointer(basePointer, allocator_); + for (SizeType i = 0; i < relPointer.GetTokenCount(); i++) + pointer = pointer.Append(relPointer.GetTokens()[i], allocator_); + if (IsCyclicRef(pointer)) + SchemaErrorValue(kSchemaErrorRefCyclical, source, ref.GetString(), ref.GetStringLength()); + else { + // Call CreateSchema recursively, but first compute the in-scope id for the $ref target as we have jumped there + // TODO: cache pointer <-> id mapping + size_t unresolvedTokenIndex; + scopeId = pointer.GetUri(document, docId_, &unresolvedTokenIndex, allocator_); + CreateSchema(schema, pointer, *pv, document, scopeId); + return true; + } + } else + SchemaErrorValue(kSchemaErrorRefUnknown, source, ref.GetString(), ref.GetStringLength()); + } + } else { + // Plain name fragment, relative to the resolved URI + // Not supported in open api 2.0 and 3.0 + PointerType pointer(allocator_); + if (spec_.oapi == kVersion20 || spec_.oapi == kVersion30) + SchemaErrorValue(kSchemaErrorRefPlainName, source, s, len); + // See if the fragment matches an id in this document. + // Search from the base we just established. Returns the subschema in the document and its absolute JSON pointer. + else if (const ValueType *pv = FindId(*base, ref, pointer, UriType(ref.GetBaseString(), ref.GetBaseStringLength(), allocator_), true, basePointer)) { + if (IsCyclicRef(pointer)) + SchemaErrorValue(kSchemaErrorRefCyclical, source, ref.GetString(), ref.GetStringLength()); + else { + // Call CreateSchema recursively, but first compute the in-scope id for the $ref target as we have jumped there + // TODO: cache pointer <-> id mapping + size_t unresolvedTokenIndex; + scopeId = pointer.GetUri(document, docId_, &unresolvedTokenIndex, allocator_); + CreateSchema(schema, pointer, *pv, document, scopeId); + return true; + } + } else + SchemaErrorValue(kSchemaErrorRefUnknown, source, ref.GetString(), ref.GetStringLength()); + } + } + } + } + + // Invalid/Unknown $ref + if (schema) + *schema = typeless_; + AddSchemaRefs(typeless_); + return true; + } + + //! Find the first subschema with a resolved 'id' that matches the specified URI. + // If full specified use all URI else ignore fragment. + // If found, return a pointer to the subschema and its JSON pointer. + // TODO cache pointer <-> id mapping + ValueType* FindId(const ValueType& doc, const UriType& finduri, PointerType& resptr, const UriType& baseuri, bool full, const PointerType& here = PointerType()) const { + SizeType i = 0; + ValueType* resval = 0; + UriType tempuri = UriType(finduri, allocator_); + UriType localuri = UriType(baseuri, allocator_); + if (doc.GetType() == kObjectType) { + // Establish the base URI of this object + typename ValueType::ConstMemberIterator m = doc.FindMember(SchemaType::GetIdString()); + if (m != doc.MemberEnd() && m->value.GetType() == kStringType) { + localuri = UriType(m->value, allocator_).Resolve(baseuri, allocator_); + } + // See if it matches + if (localuri.Match(finduri, full)) { + RAPIDJSON_SCHEMA_PRINT(Method, "GenericSchemaDocument::FindId (match)", full ? localuri.GetString() : localuri.GetBaseString()); + resval = const_cast(&doc); + resptr = here; + return resval; + } + // No match, continue looking + for (m = doc.MemberBegin(); m != doc.MemberEnd(); ++m) { + if (m->value.GetType() == kObjectType || m->value.GetType() == kArrayType) { + resval = FindId(m->value, finduri, resptr, localuri, full, here.Append(m->name.GetString(), m->name.GetStringLength(), allocator_)); + } + if (resval) break; + } + } else if (doc.GetType() == kArrayType) { + // Continue looking + for (typename ValueType::ConstValueIterator v = doc.Begin(); v != doc.End(); ++v) { + if (v->GetType() == kObjectType || v->GetType() == kArrayType) { + resval = FindId(*v, finduri, resptr, localuri, full, here.Append(i, allocator_)); + } + if (resval) break; + i++; + } + } + return resval; + } + + // Added by PR #1393 + void AddSchemaRefs(SchemaType* schema) { + RAPIDJSON_SCHEMA_PRINT(Method, "GenericSchemaDocument::AddSchemaRefs"); + while (!schemaRef_.Empty()) { + SchemaRefPtr *ref = schemaRef_.template Pop(1); + SchemaEntry *entry = schemaMap_.template Push(); + new (entry) SchemaEntry(**ref, schema, false, allocator_); + } + } + + // Added by PR #1393 + bool IsCyclicRef(const PointerType& pointer) const { + for (const SchemaRefPtr* ref = schemaRef_.template Bottom(); ref != schemaRef_.template End(); ++ref) + if (pointer == **ref) + return true; + return false; + } + + const SchemaType* GetSchema(const PointerType& pointer) const { + for (const SchemaEntry* target = schemaMap_.template Bottom(); target != schemaMap_.template End(); ++target) + if (pointer == target->pointer) + return target->schema; + return 0; + } + + PointerType GetPointer(const SchemaType* schema) const { + for (const SchemaEntry* target = schemaMap_.template Bottom(); target != schemaMap_.template End(); ++target) + if (schema == target->schema) + return target->pointer; + return PointerType(); + } + + const SchemaType* GetTypeless() const { return typeless_; } + + static const size_t kInitialSchemaMapSize = 64; + static const size_t kInitialSchemaRefSize = 64; + + IRemoteSchemaDocumentProviderType* remoteProvider_; + Allocator *allocator_; + Allocator *ownAllocator_; + const SchemaType* root_; //!< Root schema. + SchemaType* typeless_; + internal::Stack schemaMap_; // Stores created Pointer -> Schemas + internal::Stack schemaRef_; // Stores Pointer(s) from $ref(s) until resolved + GValue uri_; // Schema document URI + UriType docId_; + Specification spec_; + GValue error_; + GValue currentError_; +}; + +//! GenericSchemaDocument using Value type. +typedef GenericSchemaDocument SchemaDocument; +//! IGenericRemoteSchemaDocumentProvider using SchemaDocument. +typedef IGenericRemoteSchemaDocumentProvider IRemoteSchemaDocumentProvider; + +/////////////////////////////////////////////////////////////////////////////// +// GenericSchemaValidator + +//! JSON Schema Validator. +/*! + A SAX style JSON schema validator. + It uses a \c GenericSchemaDocument to validate SAX events. + It delegates the incoming SAX events to an output handler. + The default output handler does nothing. + It can be reused multiple times by calling \c Reset(). + + \tparam SchemaDocumentType Type of schema document. + \tparam OutputHandler Type of output handler. Default handler does nothing. + \tparam StateAllocator Allocator for storing the internal validation states. +*/ +template < + typename SchemaDocumentType, + typename OutputHandler = BaseReaderHandler, + typename StateAllocator = CrtAllocator> +class GenericSchemaValidator : + public internal::ISchemaStateFactory, + public internal::ISchemaValidator, + public internal::IValidationErrorHandler { +public: + typedef typename SchemaDocumentType::SchemaType SchemaType; + typedef typename SchemaDocumentType::PointerType PointerType; + typedef typename SchemaType::EncodingType EncodingType; + typedef typename SchemaType::SValue SValue; + typedef typename EncodingType::Ch Ch; + typedef GenericStringRef StringRefType; + typedef GenericValue ValueType; + + //! Constructor without output handler. + /*! + \param schemaDocument The schema document to conform to. + \param allocator Optional allocator for storing internal validation states. + \param schemaStackCapacity Optional initial capacity of schema path stack. + \param documentStackCapacity Optional initial capacity of document path stack. + */ + GenericSchemaValidator( + const SchemaDocumentType& schemaDocument, + StateAllocator* allocator = 0, + size_t schemaStackCapacity = kDefaultSchemaStackCapacity, + size_t documentStackCapacity = kDefaultDocumentStackCapacity) + : + schemaDocument_(&schemaDocument), + root_(schemaDocument.GetRoot()), + stateAllocator_(allocator), + ownStateAllocator_(0), + schemaStack_(allocator, schemaStackCapacity), + documentStack_(allocator, documentStackCapacity), + outputHandler_(0), + error_(kObjectType), + currentError_(), + missingDependents_(), + valid_(true), + flags_(kValidateDefaultFlags), + depth_(0) + { + RAPIDJSON_SCHEMA_PRINT(Method, "GenericSchemaValidator::GenericSchemaValidator"); + } + + //! Constructor with output handler. + /*! + \param schemaDocument The schema document to conform to. + \param allocator Optional allocator for storing internal validation states. + \param schemaStackCapacity Optional initial capacity of schema path stack. + \param documentStackCapacity Optional initial capacity of document path stack. + */ + GenericSchemaValidator( + const SchemaDocumentType& schemaDocument, + OutputHandler& outputHandler, + StateAllocator* allocator = 0, + size_t schemaStackCapacity = kDefaultSchemaStackCapacity, + size_t documentStackCapacity = kDefaultDocumentStackCapacity) + : + schemaDocument_(&schemaDocument), + root_(schemaDocument.GetRoot()), + stateAllocator_(allocator), + ownStateAllocator_(0), + schemaStack_(allocator, schemaStackCapacity), + documentStack_(allocator, documentStackCapacity), + outputHandler_(&outputHandler), + error_(kObjectType), + currentError_(), + missingDependents_(), + valid_(true), + flags_(kValidateDefaultFlags), + depth_(0) + { + RAPIDJSON_SCHEMA_PRINT(Method, "GenericSchemaValidator::GenericSchemaValidator (output handler)"); + } + + //! Destructor. + ~GenericSchemaValidator() { + Reset(); + RAPIDJSON_DELETE(ownStateAllocator_); + } + + //! Reset the internal states. + void Reset() { + while (!schemaStack_.Empty()) + PopSchema(); + documentStack_.Clear(); + ResetError(); + } + + //! Reset the error state. + void ResetError() { + error_.SetObject(); + currentError_.SetNull(); + missingDependents_.SetNull(); + valid_ = true; + } + + //! Implementation of ISchemaValidator + void SetValidateFlags(unsigned flags) { + flags_ = flags; + } + virtual unsigned GetValidateFlags() const { + return flags_; + } + + virtual bool IsValid() const { + if (!valid_) return false; + if (GetContinueOnErrors() && !error_.ObjectEmpty()) return false; + return true; + } + //! End of Implementation of ISchemaValidator + + //! Gets the error object. + ValueType& GetError() { return error_; } + const ValueType& GetError() const { return error_; } + + //! Gets the JSON pointer pointed to the invalid schema. + // If reporting all errors, the stack will be empty. + PointerType GetInvalidSchemaPointer() const { + return schemaStack_.Empty() ? PointerType() : CurrentSchema().GetPointer(); + } + + //! Gets the keyword of invalid schema. + // If reporting all errors, the stack will be empty, so return "errors". + const Ch* GetInvalidSchemaKeyword() const { + if (!schemaStack_.Empty()) return CurrentContext().invalidKeyword; + if (GetContinueOnErrors() && !error_.ObjectEmpty()) return (const Ch*)GetErrorsString(); + return 0; + } + + //! Gets the error code of invalid schema. + // If reporting all errors, the stack will be empty, so return kValidateErrors. + ValidateErrorCode GetInvalidSchemaCode() const { + if (!schemaStack_.Empty()) return CurrentContext().invalidCode; + if (GetContinueOnErrors() && !error_.ObjectEmpty()) return kValidateErrors; + return kValidateErrorNone; + } + + //! Gets the JSON pointer pointed to the invalid value. + // If reporting all errors, the stack will be empty. + PointerType GetInvalidDocumentPointer() const { + if (documentStack_.Empty()) { + return PointerType(); + } + else { + return PointerType(documentStack_.template Bottom(), documentStack_.GetSize() / sizeof(Ch)); + } + } + + void NotMultipleOf(int64_t actual, const SValue& expected) { + AddNumberError(kValidateErrorMultipleOf, ValueType(actual).Move(), expected); + } + void NotMultipleOf(uint64_t actual, const SValue& expected) { + AddNumberError(kValidateErrorMultipleOf, ValueType(actual).Move(), expected); + } + void NotMultipleOf(double actual, const SValue& expected) { + AddNumberError(kValidateErrorMultipleOf, ValueType(actual).Move(), expected); + } + void AboveMaximum(int64_t actual, const SValue& expected, bool exclusive) { + AddNumberError(exclusive ? kValidateErrorExclusiveMaximum : kValidateErrorMaximum, ValueType(actual).Move(), expected, + exclusive ? &SchemaType::GetExclusiveMaximumString : 0); + } + void AboveMaximum(uint64_t actual, const SValue& expected, bool exclusive) { + AddNumberError(exclusive ? kValidateErrorExclusiveMaximum : kValidateErrorMaximum, ValueType(actual).Move(), expected, + exclusive ? &SchemaType::GetExclusiveMaximumString : 0); + } + void AboveMaximum(double actual, const SValue& expected, bool exclusive) { + AddNumberError(exclusive ? kValidateErrorExclusiveMaximum : kValidateErrorMaximum, ValueType(actual).Move(), expected, + exclusive ? &SchemaType::GetExclusiveMaximumString : 0); + } + void BelowMinimum(int64_t actual, const SValue& expected, bool exclusive) { + AddNumberError(exclusive ? kValidateErrorExclusiveMinimum : kValidateErrorMinimum, ValueType(actual).Move(), expected, + exclusive ? &SchemaType::GetExclusiveMinimumString : 0); + } + void BelowMinimum(uint64_t actual, const SValue& expected, bool exclusive) { + AddNumberError(exclusive ? kValidateErrorExclusiveMinimum : kValidateErrorMinimum, ValueType(actual).Move(), expected, + exclusive ? &SchemaType::GetExclusiveMinimumString : 0); + } + void BelowMinimum(double actual, const SValue& expected, bool exclusive) { + AddNumberError(exclusive ? kValidateErrorExclusiveMinimum : kValidateErrorMinimum, ValueType(actual).Move(), expected, + exclusive ? &SchemaType::GetExclusiveMinimumString : 0); + } + + void TooLong(const Ch* str, SizeType length, SizeType expected) { + AddNumberError(kValidateErrorMaxLength, + ValueType(str, length, GetStateAllocator()).Move(), SValue(expected).Move()); + } + void TooShort(const Ch* str, SizeType length, SizeType expected) { + AddNumberError(kValidateErrorMinLength, + ValueType(str, length, GetStateAllocator()).Move(), SValue(expected).Move()); + } + void DoesNotMatch(const Ch* str, SizeType length) { + currentError_.SetObject(); + currentError_.AddMember(GetActualString(), ValueType(str, length, GetStateAllocator()).Move(), GetStateAllocator()); + AddCurrentError(kValidateErrorPattern); + } + + void DisallowedItem(SizeType index) { + currentError_.SetObject(); + currentError_.AddMember(GetDisallowedString(), ValueType(index).Move(), GetStateAllocator()); + AddCurrentError(kValidateErrorAdditionalItems, true); + } + void TooFewItems(SizeType actualCount, SizeType expectedCount) { + AddNumberError(kValidateErrorMinItems, + ValueType(actualCount).Move(), SValue(expectedCount).Move()); + } + void TooManyItems(SizeType actualCount, SizeType expectedCount) { + AddNumberError(kValidateErrorMaxItems, + ValueType(actualCount).Move(), SValue(expectedCount).Move()); + } + void DuplicateItems(SizeType index1, SizeType index2) { + ValueType duplicates(kArrayType); + duplicates.PushBack(index1, GetStateAllocator()); + duplicates.PushBack(index2, GetStateAllocator()); + currentError_.SetObject(); + currentError_.AddMember(GetDuplicatesString(), duplicates, GetStateAllocator()); + AddCurrentError(kValidateErrorUniqueItems, true); + } + + void TooManyProperties(SizeType actualCount, SizeType expectedCount) { + AddNumberError(kValidateErrorMaxProperties, + ValueType(actualCount).Move(), SValue(expectedCount).Move()); + } + void TooFewProperties(SizeType actualCount, SizeType expectedCount) { + AddNumberError(kValidateErrorMinProperties, + ValueType(actualCount).Move(), SValue(expectedCount).Move()); + } + void StartMissingProperties() { + currentError_.SetArray(); + } + void AddMissingProperty(const SValue& name) { + currentError_.PushBack(ValueType(name, GetStateAllocator()).Move(), GetStateAllocator()); + } + bool EndMissingProperties() { + if (currentError_.Empty()) + return false; + ValueType error(kObjectType); + error.AddMember(GetMissingString(), currentError_, GetStateAllocator()); + currentError_ = error; + AddCurrentError(kValidateErrorRequired); + return true; + } + void PropertyViolations(ISchemaValidator** subvalidators, SizeType count) { + for (SizeType i = 0; i < count; ++i) + MergeError(static_cast(subvalidators[i])->GetError()); + } + void DisallowedProperty(const Ch* name, SizeType length) { + currentError_.SetObject(); + currentError_.AddMember(GetDisallowedString(), ValueType(name, length, GetStateAllocator()).Move(), GetStateAllocator()); + AddCurrentError(kValidateErrorAdditionalProperties, true); + } + + void StartDependencyErrors() { + currentError_.SetObject(); + } + void StartMissingDependentProperties() { + missingDependents_.SetArray(); + } + void AddMissingDependentProperty(const SValue& targetName) { + missingDependents_.PushBack(ValueType(targetName, GetStateAllocator()).Move(), GetStateAllocator()); + } + void EndMissingDependentProperties(const SValue& sourceName) { + if (!missingDependents_.Empty()) { + // Create equivalent 'required' error + ValueType error(kObjectType); + ValidateErrorCode code = kValidateErrorRequired; + error.AddMember(GetMissingString(), missingDependents_.Move(), GetStateAllocator()); + AddErrorCode(error, code); + AddErrorInstanceLocation(error, false); + // When appending to a pointer ensure its allocator is used + PointerType schemaRef = GetInvalidSchemaPointer().Append(SchemaType::GetValidateErrorKeyword(kValidateErrorDependencies), &GetInvalidSchemaPointer().GetAllocator()); + AddErrorSchemaLocation(error, schemaRef.Append(sourceName.GetString(), sourceName.GetStringLength(), &GetInvalidSchemaPointer().GetAllocator())); + ValueType wrapper(kObjectType); + wrapper.AddMember(ValueType(SchemaType::GetValidateErrorKeyword(code), GetStateAllocator()).Move(), error, GetStateAllocator()); + currentError_.AddMember(ValueType(sourceName, GetStateAllocator()).Move(), wrapper, GetStateAllocator()); + } + } + void AddDependencySchemaError(const SValue& sourceName, ISchemaValidator* subvalidator) { + currentError_.AddMember(ValueType(sourceName, GetStateAllocator()).Move(), + static_cast(subvalidator)->GetError(), GetStateAllocator()); + } + bool EndDependencyErrors() { + if (currentError_.ObjectEmpty()) + return false; + ValueType error(kObjectType); + error.AddMember(GetErrorsString(), currentError_, GetStateAllocator()); + currentError_ = error; + AddCurrentError(kValidateErrorDependencies); + return true; + } + + void DisallowedValue(const ValidateErrorCode code = kValidateErrorEnum) { + currentError_.SetObject(); + AddCurrentError(code); + } + void StartDisallowedType() { + currentError_.SetArray(); + } + void AddExpectedType(const typename SchemaType::ValueType& expectedType) { + currentError_.PushBack(ValueType(expectedType, GetStateAllocator()).Move(), GetStateAllocator()); + } + void EndDisallowedType(const typename SchemaType::ValueType& actualType) { + ValueType error(kObjectType); + error.AddMember(GetExpectedString(), currentError_, GetStateAllocator()); + error.AddMember(GetActualString(), ValueType(actualType, GetStateAllocator()).Move(), GetStateAllocator()); + currentError_ = error; + AddCurrentError(kValidateErrorType); + } + void NotAllOf(ISchemaValidator** subvalidators, SizeType count) { + // Treat allOf like oneOf and anyOf to match https://rapidjson.org/md_doc_schema.html#allOf-anyOf-oneOf + AddErrorArray(kValidateErrorAllOf, subvalidators, count); + //for (SizeType i = 0; i < count; ++i) { + // MergeError(static_cast(subvalidators[i])->GetError()); + //} + } + void NoneOf(ISchemaValidator** subvalidators, SizeType count) { + AddErrorArray(kValidateErrorAnyOf, subvalidators, count); + } + void NotOneOf(ISchemaValidator** subvalidators, SizeType count) { + AddErrorArray(kValidateErrorOneOf, subvalidators, count); + } + void MultipleOneOf(SizeType index1, SizeType index2) { + ValueType matches(kArrayType); + matches.PushBack(index1, GetStateAllocator()); + matches.PushBack(index2, GetStateAllocator()); + currentError_.SetObject(); + currentError_.AddMember(GetMatchesString(), matches, GetStateAllocator()); + AddCurrentError(kValidateErrorOneOfMatch); + } + void Disallowed() { + currentError_.SetObject(); + AddCurrentError(kValidateErrorNot); + } + void DisallowedWhenWriting() { + currentError_.SetObject(); + AddCurrentError(kValidateErrorReadOnly); + } + void DisallowedWhenReading() { + currentError_.SetObject(); + AddCurrentError(kValidateErrorWriteOnly); + } + +#define RAPIDJSON_STRING_(name, ...) \ + static const StringRefType& Get##name##String() {\ + static const Ch s[] = { __VA_ARGS__, '\0' };\ + static const StringRefType v(s, static_cast(sizeof(s) / sizeof(Ch) - 1)); \ + return v;\ + } + + RAPIDJSON_STRING_(InstanceRef, 'i', 'n', 's', 't', 'a', 'n', 'c', 'e', 'R', 'e', 'f') + RAPIDJSON_STRING_(SchemaRef, 's', 'c', 'h', 'e', 'm', 'a', 'R', 'e', 'f') + RAPIDJSON_STRING_(Expected, 'e', 'x', 'p', 'e', 'c', 't', 'e', 'd') + RAPIDJSON_STRING_(Actual, 'a', 'c', 't', 'u', 'a', 'l') + RAPIDJSON_STRING_(Disallowed, 'd', 'i', 's', 'a', 'l', 'l', 'o', 'w', 'e', 'd') + RAPIDJSON_STRING_(Missing, 'm', 'i', 's', 's', 'i', 'n', 'g') + RAPIDJSON_STRING_(Errors, 'e', 'r', 'r', 'o', 'r', 's') + RAPIDJSON_STRING_(ErrorCode, 'e', 'r', 'r', 'o', 'r', 'C', 'o', 'd', 'e') + RAPIDJSON_STRING_(ErrorMessage, 'e', 'r', 'r', 'o', 'r', 'M', 'e', 's', 's', 'a', 'g', 'e') + RAPIDJSON_STRING_(Duplicates, 'd', 'u', 'p', 'l', 'i', 'c', 'a', 't', 'e', 's') + RAPIDJSON_STRING_(Matches, 'm', 'a', 't', 'c', 'h', 'e', 's') + +#undef RAPIDJSON_STRING_ + +#define RAPIDJSON_SCHEMA_HANDLE_BEGIN_(method, arg1)\ + if (!valid_) return false; \ + if ((!BeginValue() && !GetContinueOnErrors()) || (!CurrentSchema().method arg1 && !GetContinueOnErrors())) {\ + *documentStack_.template Push() = '\0';\ + documentStack_.template Pop(1);\ + RAPIDJSON_SCHEMA_PRINT(InvalidDocument, documentStack_.template Bottom());\ + valid_ = false;\ + return valid_;\ + } + +#define RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(method, arg2)\ + for (Context* context = schemaStack_.template Bottom(); context != schemaStack_.template End(); context++) {\ + if (context->hasher)\ + static_cast(context->hasher)->method arg2;\ + if (context->validators)\ + for (SizeType i_ = 0; i_ < context->validatorCount; i_++)\ + static_cast(context->validators[i_])->method arg2;\ + if (context->patternPropertiesValidators)\ + for (SizeType i_ = 0; i_ < context->patternPropertiesValidatorCount; i_++)\ + static_cast(context->patternPropertiesValidators[i_])->method arg2;\ + } + +#define RAPIDJSON_SCHEMA_HANDLE_END_(method, arg2)\ + valid_ = (EndValue() || GetContinueOnErrors()) && (!outputHandler_ || outputHandler_->method arg2);\ + return valid_; + +#define RAPIDJSON_SCHEMA_HANDLE_VALUE_(method, arg1, arg2) \ + RAPIDJSON_SCHEMA_HANDLE_BEGIN_ (method, arg1);\ + RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(method, arg2);\ + RAPIDJSON_SCHEMA_HANDLE_END_ (method, arg2) + + bool Null() { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Null, (CurrentContext()), ( )); } + bool Bool(bool b) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Bool, (CurrentContext(), b), (b)); } + bool Int(int i) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Int, (CurrentContext(), i), (i)); } + bool Uint(unsigned u) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Uint, (CurrentContext(), u), (u)); } + bool Int64(int64_t i) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Int64, (CurrentContext(), i), (i)); } + bool Uint64(uint64_t u) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Uint64, (CurrentContext(), u), (u)); } + bool Double(double d) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Double, (CurrentContext(), d), (d)); } + bool RawNumber(const Ch* str, SizeType length, bool copy) + { RAPIDJSON_SCHEMA_HANDLE_VALUE_(String, (CurrentContext(), str, length, copy), (str, length, copy)); } + bool String(const Ch* str, SizeType length, bool copy) + { RAPIDJSON_SCHEMA_HANDLE_VALUE_(String, (CurrentContext(), str, length, copy), (str, length, copy)); } + + bool StartObject() { + RAPIDJSON_SCHEMA_PRINT(Method, "GenericSchemaValidator::StartObject"); + RAPIDJSON_SCHEMA_HANDLE_BEGIN_(StartObject, (CurrentContext())); + RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(StartObject, ()); + valid_ = !outputHandler_ || outputHandler_->StartObject(); + return valid_; + } + + bool Key(const Ch* str, SizeType len, bool copy) { + RAPIDJSON_SCHEMA_PRINT(Method, "GenericSchemaValidator::Key", str); + if (!valid_) return false; + AppendToken(str, len); + if (!CurrentSchema().Key(CurrentContext(), str, len, copy) && !GetContinueOnErrors()) { + valid_ = false; + return valid_; + } + RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(Key, (str, len, copy)); + valid_ = !outputHandler_ || outputHandler_->Key(str, len, copy); + return valid_; + } + + bool EndObject(SizeType memberCount) { + RAPIDJSON_SCHEMA_PRINT(Method, "GenericSchemaValidator::EndObject"); + if (!valid_) return false; + RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(EndObject, (memberCount)); + if (!CurrentSchema().EndObject(CurrentContext(), memberCount) && !GetContinueOnErrors()) { + valid_ = false; + return valid_; + } + RAPIDJSON_SCHEMA_HANDLE_END_(EndObject, (memberCount)); + } + + bool StartArray() { + RAPIDJSON_SCHEMA_PRINT(Method, "GenericSchemaValidator::StartArray"); + RAPIDJSON_SCHEMA_HANDLE_BEGIN_(StartArray, (CurrentContext())); + RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(StartArray, ()); + valid_ = !outputHandler_ || outputHandler_->StartArray(); + return valid_; + } + + bool EndArray(SizeType elementCount) { + RAPIDJSON_SCHEMA_PRINT(Method, "GenericSchemaValidator::EndArray"); + if (!valid_) return false; + RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(EndArray, (elementCount)); + if (!CurrentSchema().EndArray(CurrentContext(), elementCount) && !GetContinueOnErrors()) { + valid_ = false; + return valid_; + } + RAPIDJSON_SCHEMA_HANDLE_END_(EndArray, (elementCount)); + } + +#undef RAPIDJSON_SCHEMA_HANDLE_BEGIN_ +#undef RAPIDJSON_SCHEMA_HANDLE_PARALLEL_ +#undef RAPIDJSON_SCHEMA_HANDLE_VALUE_ + + // Implementation of ISchemaStateFactory + virtual ISchemaValidator* CreateSchemaValidator(const SchemaType& root, const bool inheritContinueOnErrors) { + *documentStack_.template Push() = '\0'; + documentStack_.template Pop(1); + ISchemaValidator* sv = new (GetStateAllocator().Malloc(sizeof(GenericSchemaValidator))) GenericSchemaValidator(*schemaDocument_, root, documentStack_.template Bottom(), documentStack_.GetSize(), + depth_ + 1, + &GetStateAllocator()); + sv->SetValidateFlags(inheritContinueOnErrors ? GetValidateFlags() : GetValidateFlags() & ~(unsigned)kValidateContinueOnErrorFlag); + return sv; + } + + virtual void DestroySchemaValidator(ISchemaValidator* validator) { + GenericSchemaValidator* v = static_cast(validator); + v->~GenericSchemaValidator(); + StateAllocator::Free(v); + } + + virtual void* CreateHasher() { + return new (GetStateAllocator().Malloc(sizeof(HasherType))) HasherType(&GetStateAllocator()); + } + + virtual uint64_t GetHashCode(void* hasher) { + return static_cast(hasher)->GetHashCode(); + } + + virtual void DestroyHasher(void* hasher) { + HasherType* h = static_cast(hasher); + h->~HasherType(); + StateAllocator::Free(h); + } + + virtual void* MallocState(size_t size) { + return GetStateAllocator().Malloc(size); + } + + virtual void FreeState(void* p) { + StateAllocator::Free(p); + } + // End of implementation of ISchemaStateFactory + +private: + typedef typename SchemaType::Context Context; + typedef GenericValue, StateAllocator> HashCodeArray; + typedef internal::Hasher HasherType; + + GenericSchemaValidator( + const SchemaDocumentType& schemaDocument, + const SchemaType& root, + const char* basePath, size_t basePathSize, + unsigned depth, + StateAllocator* allocator = 0, + size_t schemaStackCapacity = kDefaultSchemaStackCapacity, + size_t documentStackCapacity = kDefaultDocumentStackCapacity) + : + schemaDocument_(&schemaDocument), + root_(root), + stateAllocator_(allocator), + ownStateAllocator_(0), + schemaStack_(allocator, schemaStackCapacity), + documentStack_(allocator, documentStackCapacity), + outputHandler_(0), + error_(kObjectType), + currentError_(), + missingDependents_(), + valid_(true), + flags_(kValidateDefaultFlags), + depth_(depth) + { + RAPIDJSON_SCHEMA_PRINT(Method, "GenericSchemaValidator::GenericSchemaValidator (internal)", basePath && basePathSize ? basePath : ""); + if (basePath && basePathSize) + memcpy(documentStack_.template Push(basePathSize), basePath, basePathSize); + } + + StateAllocator& GetStateAllocator() { + if (!stateAllocator_) + stateAllocator_ = ownStateAllocator_ = RAPIDJSON_NEW(StateAllocator)(); + return *stateAllocator_; + } + + bool GetContinueOnErrors() const { + return flags_ & kValidateContinueOnErrorFlag; + } + + bool BeginValue() { + RAPIDJSON_SCHEMA_PRINT(Method, "GenericSchemaValidator::BeginValue"); + if (schemaStack_.Empty()) + PushSchema(root_); + else { + if (CurrentContext().inArray) + internal::TokenHelper, Ch>::AppendIndexToken(documentStack_, CurrentContext().arrayElementIndex); + + if (!CurrentSchema().BeginValue(CurrentContext()) && !GetContinueOnErrors()) + return false; + + SizeType count = CurrentContext().patternPropertiesSchemaCount; + const SchemaType** sa = CurrentContext().patternPropertiesSchemas; + typename Context::PatternValidatorType patternValidatorType = CurrentContext().valuePatternValidatorType; + bool valueUniqueness = CurrentContext().valueUniqueness; + RAPIDJSON_ASSERT(CurrentContext().valueSchema); + PushSchema(*CurrentContext().valueSchema); + + if (count > 0) { + CurrentContext().objectPatternValidatorType = patternValidatorType; + ISchemaValidator**& va = CurrentContext().patternPropertiesValidators; + SizeType& validatorCount = CurrentContext().patternPropertiesValidatorCount; + va = static_cast(MallocState(sizeof(ISchemaValidator*) * count)); + std::memset(va, 0, sizeof(ISchemaValidator*) * count); + for (SizeType i = 0; i < count; i++) + va[validatorCount++] = CreateSchemaValidator(*sa[i], true); // Inherit continueOnError + } + + CurrentContext().arrayUniqueness = valueUniqueness; + } + return true; + } + + bool EndValue() { + RAPIDJSON_SCHEMA_PRINT(Method, "GenericSchemaValidator::EndValue"); + if (!CurrentSchema().EndValue(CurrentContext()) && !GetContinueOnErrors()) + return false; + + GenericStringBuffer sb; + schemaDocument_->GetPointer(&CurrentSchema()).StringifyUriFragment(sb); + *documentStack_.template Push() = '\0'; + documentStack_.template Pop(1); + RAPIDJSON_SCHEMA_PRINT(ValidatorPointers, sb.GetString(), documentStack_.template Bottom(), depth_); + void* hasher = CurrentContext().hasher; + uint64_t h = hasher && CurrentContext().arrayUniqueness ? static_cast(hasher)->GetHashCode() : 0; + + PopSchema(); + + if (!schemaStack_.Empty()) { + Context& context = CurrentContext(); + // Only check uniqueness if there is a hasher + if (hasher && context.valueUniqueness) { + HashCodeArray* a = static_cast(context.arrayElementHashCodes); + if (!a) + CurrentContext().arrayElementHashCodes = a = new (GetStateAllocator().Malloc(sizeof(HashCodeArray))) HashCodeArray(kArrayType); + for (typename HashCodeArray::ConstValueIterator itr = a->Begin(); itr != a->End(); ++itr) + if (itr->GetUint64() == h) { + DuplicateItems(static_cast(itr - a->Begin()), a->Size()); + // Cleanup before returning if continuing + if (GetContinueOnErrors()) { + a->PushBack(h, GetStateAllocator()); + while (!documentStack_.Empty() && *documentStack_.template Pop(1) != '/'); + } + RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorUniqueItems); + } + a->PushBack(h, GetStateAllocator()); + } + } + + // Remove the last token of document pointer + while (!documentStack_.Empty() && *documentStack_.template Pop(1) != '/') + ; + + return true; + } + + void AppendToken(const Ch* str, SizeType len) { + documentStack_.template Reserve(1 + len * 2); // worst case all characters are escaped as two characters + *documentStack_.template PushUnsafe() = '/'; + for (SizeType i = 0; i < len; i++) { + if (str[i] == '~') { + *documentStack_.template PushUnsafe() = '~'; + *documentStack_.template PushUnsafe() = '0'; + } + else if (str[i] == '/') { + *documentStack_.template PushUnsafe() = '~'; + *documentStack_.template PushUnsafe() = '1'; + } + else + *documentStack_.template PushUnsafe() = str[i]; + } + } + + RAPIDJSON_FORCEINLINE void PushSchema(const SchemaType& schema) { new (schemaStack_.template Push()) Context(*this, *this, &schema, flags_); } + + RAPIDJSON_FORCEINLINE void PopSchema() { + Context* c = schemaStack_.template Pop(1); + if (HashCodeArray* a = static_cast(c->arrayElementHashCodes)) { + a->~HashCodeArray(); + StateAllocator::Free(a); + } + c->~Context(); + } + + void AddErrorInstanceLocation(ValueType& result, bool parent) { + GenericStringBuffer sb; + PointerType instancePointer = GetInvalidDocumentPointer(); + ((parent && instancePointer.GetTokenCount() > 0) + ? PointerType(instancePointer.GetTokens(), instancePointer.GetTokenCount() - 1) + : instancePointer).StringifyUriFragment(sb); + ValueType instanceRef(sb.GetString(), static_cast(sb.GetSize() / sizeof(Ch)), + GetStateAllocator()); + result.AddMember(GetInstanceRefString(), instanceRef, GetStateAllocator()); + } + + void AddErrorSchemaLocation(ValueType& result, PointerType schema = PointerType()) { + GenericStringBuffer sb; + SizeType len = CurrentSchema().GetURI().GetStringLength(); + if (len) memcpy(sb.Push(len), CurrentSchema().GetURI().GetString(), len * sizeof(Ch)); + if (schema.GetTokenCount()) schema.StringifyUriFragment(sb); + else GetInvalidSchemaPointer().StringifyUriFragment(sb); + ValueType schemaRef(sb.GetString(), static_cast(sb.GetSize() / sizeof(Ch)), + GetStateAllocator()); + result.AddMember(GetSchemaRefString(), schemaRef, GetStateAllocator()); + } + + void AddErrorCode(ValueType& result, const ValidateErrorCode code) { + result.AddMember(GetErrorCodeString(), code, GetStateAllocator()); + } + + void AddError(ValueType& keyword, ValueType& error) { + typename ValueType::MemberIterator member = error_.FindMember(keyword); + if (member == error_.MemberEnd()) + error_.AddMember(keyword, error, GetStateAllocator()); + else { + if (member->value.IsObject()) { + ValueType errors(kArrayType); + errors.PushBack(member->value, GetStateAllocator()); + member->value = errors; + } + member->value.PushBack(error, GetStateAllocator()); + } + } + + void AddCurrentError(const ValidateErrorCode code, bool parent = false) { + AddErrorCode(currentError_, code); + AddErrorInstanceLocation(currentError_, parent); + AddErrorSchemaLocation(currentError_); + AddError(ValueType(SchemaType::GetValidateErrorKeyword(code), GetStateAllocator(), false).Move(), currentError_); + } + + void MergeError(ValueType& other) { + for (typename ValueType::MemberIterator it = other.MemberBegin(), end = other.MemberEnd(); it != end; ++it) { + AddError(it->name, it->value); + } + } + + void AddNumberError(const ValidateErrorCode code, ValueType& actual, const SValue& expected, + const typename SchemaType::ValueType& (*exclusive)() = 0) { + currentError_.SetObject(); + currentError_.AddMember(GetActualString(), actual, GetStateAllocator()); + currentError_.AddMember(GetExpectedString(), ValueType(expected, GetStateAllocator()).Move(), GetStateAllocator()); + if (exclusive) + currentError_.AddMember(ValueType(exclusive(), GetStateAllocator()).Move(), true, GetStateAllocator()); + AddCurrentError(code); + } + + void AddErrorArray(const ValidateErrorCode code, + ISchemaValidator** subvalidators, SizeType count) { + ValueType errors(kArrayType); + for (SizeType i = 0; i < count; ++i) + errors.PushBack(static_cast(subvalidators[i])->GetError(), GetStateAllocator()); + currentError_.SetObject(); + currentError_.AddMember(GetErrorsString(), errors, GetStateAllocator()); + AddCurrentError(code); + } + + const SchemaType& CurrentSchema() const { return *schemaStack_.template Top()->schema; } + Context& CurrentContext() { return *schemaStack_.template Top(); } + const Context& CurrentContext() const { return *schemaStack_.template Top(); } + + static const size_t kDefaultSchemaStackCapacity = 1024; + static const size_t kDefaultDocumentStackCapacity = 256; + const SchemaDocumentType* schemaDocument_; + const SchemaType& root_; + StateAllocator* stateAllocator_; + StateAllocator* ownStateAllocator_; + internal::Stack schemaStack_; //!< stack to store the current path of schema (BaseSchemaType *) + internal::Stack documentStack_; //!< stack to store the current path of validating document (Ch) + OutputHandler* outputHandler_; + ValueType error_; + ValueType currentError_; + ValueType missingDependents_; + bool valid_; + unsigned flags_; + unsigned depth_; +}; + +typedef GenericSchemaValidator SchemaValidator; + +/////////////////////////////////////////////////////////////////////////////// +// SchemaValidatingReader + +//! A helper class for parsing with validation. +/*! + This helper class is a functor, designed as a parameter of \ref GenericDocument::Populate(). + + \tparam parseFlags Combination of \ref ParseFlag. + \tparam InputStream Type of input stream, implementing Stream concept. + \tparam SourceEncoding Encoding of the input stream. + \tparam SchemaDocumentType Type of schema document. + \tparam StackAllocator Allocator type for stack. +*/ +template < + unsigned parseFlags, + typename InputStream, + typename SourceEncoding, + typename SchemaDocumentType = SchemaDocument, + typename StackAllocator = CrtAllocator> +class SchemaValidatingReader { +public: + typedef typename SchemaDocumentType::PointerType PointerType; + typedef typename InputStream::Ch Ch; + typedef GenericValue ValueType; + + //! Constructor + /*! + \param is Input stream. + \param sd Schema document. + */ + SchemaValidatingReader(InputStream& is, const SchemaDocumentType& sd) : is_(is), sd_(sd), invalidSchemaKeyword_(), invalidSchemaCode_(kValidateErrorNone), error_(kObjectType), isValid_(true) {} + + template + bool operator()(Handler& handler) { + GenericReader reader; + GenericSchemaValidator validator(sd_, handler); + parseResult_ = reader.template Parse(is_, validator); + + isValid_ = validator.IsValid(); + if (isValid_) { + invalidSchemaPointer_ = PointerType(); + invalidSchemaKeyword_ = 0; + invalidDocumentPointer_ = PointerType(); + error_.SetObject(); + } + else { + invalidSchemaPointer_ = validator.GetInvalidSchemaPointer(); + invalidSchemaKeyword_ = validator.GetInvalidSchemaKeyword(); + invalidSchemaCode_ = validator.GetInvalidSchemaCode(); + invalidDocumentPointer_ = validator.GetInvalidDocumentPointer(); + error_.CopyFrom(validator.GetError(), allocator_); + } + + return parseResult_; + } + + const ParseResult& GetParseResult() const { return parseResult_; } + bool IsValid() const { return isValid_; } + const PointerType& GetInvalidSchemaPointer() const { return invalidSchemaPointer_; } + const Ch* GetInvalidSchemaKeyword() const { return invalidSchemaKeyword_; } + const PointerType& GetInvalidDocumentPointer() const { return invalidDocumentPointer_; } + const ValueType& GetError() const { return error_; } + ValidateErrorCode GetInvalidSchemaCode() const { return invalidSchemaCode_; } + +private: + InputStream& is_; + const SchemaDocumentType& sd_; + + ParseResult parseResult_; + PointerType invalidSchemaPointer_; + const Ch* invalidSchemaKeyword_; + PointerType invalidDocumentPointer_; + ValidateErrorCode invalidSchemaCode_; + StackAllocator allocator_; + ValueType error_; + bool isValid_; +}; + +RAPIDJSON_NAMESPACE_END +RAPIDJSON_DIAG_POP + +#endif // RAPIDJSON_SCHEMA_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/stream.h b/include/liblvgl/libs/thorvg/rapidjson/stream.h new file mode 100644 index 00000000..1fd70915 --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/stream.h @@ -0,0 +1,223 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#include "rapidjson.h" + +#ifndef RAPIDJSON_STREAM_H_ +#define RAPIDJSON_STREAM_H_ + +#include "encodings.h" + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// Stream + +/*! \class rapidjson::Stream + \brief Concept for reading and writing characters. + + For read-only stream, no need to implement PutBegin(), Put(), Flush() and PutEnd(). + + For write-only stream, only need to implement Put() and Flush(). + +\code +concept Stream { + typename Ch; //!< Character type of the stream. + + //! Read the current character from stream without moving the read cursor. + Ch Peek() const; + + //! Read the current character from stream and moving the read cursor to next character. + Ch Take(); + + //! Get the current read cursor. + //! \return Number of characters read from start. + size_t Tell(); + + //! Begin writing operation at the current read pointer. + //! \return The begin writer pointer. + Ch* PutBegin(); + + //! Write a character. + void Put(Ch c); + + //! Flush the buffer. + void Flush(); + + //! End the writing operation. + //! \param begin The begin write pointer returned by PutBegin(). + //! \return Number of characters written. + size_t PutEnd(Ch* begin); +} +\endcode +*/ + +//! Provides additional information for stream. +/*! + By using traits pattern, this type provides a default configuration for stream. + For custom stream, this type can be specialized for other configuration. + See TEST(Reader, CustomStringStream) in readertest.cpp for example. +*/ +template +struct StreamTraits { + //! Whether to make local copy of stream for optimization during parsing. + /*! + By default, for safety, streams do not use local copy optimization. + Stream that can be copied fast should specialize this, like StreamTraits. + */ + enum { copyOptimization = 0 }; +}; + +//! Reserve n characters for writing to a stream. +template +inline void PutReserve(Stream& stream, size_t count) { + (void)stream; + (void)count; +} + +//! Write character to a stream, presuming buffer is reserved. +template +inline void PutUnsafe(Stream& stream, typename Stream::Ch c) { + stream.Put(c); +} + +//! Put N copies of a character to a stream. +template +inline void PutN(Stream& stream, Ch c, size_t n) { + PutReserve(stream, n); + for (size_t i = 0; i < n; i++) + PutUnsafe(stream, c); +} + +/////////////////////////////////////////////////////////////////////////////// +// GenericStreamWrapper + +//! A Stream Wrapper +/*! \tThis string stream is a wrapper for any stream by just forwarding any + \treceived message to the origin stream. + \note implements Stream concept +*/ + +#if defined(_MSC_VER) && _MSC_VER <= 1800 +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4702) // unreachable code +RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated +#endif + +template > +class GenericStreamWrapper { +public: + typedef typename Encoding::Ch Ch; + GenericStreamWrapper(InputStream& is): is_(is) {} + + Ch Peek() const { return is_.Peek(); } + Ch Take() { return is_.Take(); } + size_t Tell() { return is_.Tell(); } + Ch* PutBegin() { return is_.PutBegin(); } + void Put(Ch ch) { is_.Put(ch); } + void Flush() { is_.Flush(); } + size_t PutEnd(Ch* ch) { return is_.PutEnd(ch); } + + // wrapper for MemoryStream + const Ch* Peek4() const { return is_.Peek4(); } + + // wrapper for AutoUTFInputStream + UTFType GetType() const { return is_.GetType(); } + bool HasBOM() const { return is_.HasBOM(); } + +protected: + InputStream& is_; +}; + +#if defined(_MSC_VER) && _MSC_VER <= 1800 +RAPIDJSON_DIAG_POP +#endif + +/////////////////////////////////////////////////////////////////////////////// +// StringStream + +//! Read-only string stream. +/*! \note implements Stream concept +*/ +template +struct GenericStringStream { + typedef typename Encoding::Ch Ch; + + GenericStringStream(const Ch *src) : src_(src), head_(src) {} + + Ch Peek() const { return *src_; } + Ch Take() { return *src_++; } + size_t Tell() const { return static_cast(src_ - head_); } + + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + + const Ch* src_; //!< Current read position. + const Ch* head_; //!< Original head of the string. +}; + +template +struct StreamTraits > { + enum { copyOptimization = 1 }; +}; + +//! String stream with UTF8 encoding. +typedef GenericStringStream > StringStream; + +/////////////////////////////////////////////////////////////////////////////// +// InsituStringStream + +//! A read-write string stream. +/*! This string stream is particularly designed for in-situ parsing. + \note implements Stream concept +*/ +template +struct GenericInsituStringStream { + typedef typename Encoding::Ch Ch; + + GenericInsituStringStream(Ch *src) : src_(src), dst_(0), head_(src) {} + + // Read + Ch Peek() { return *src_; } + Ch Take() { return *src_++; } + size_t Tell() { return static_cast(src_ - head_); } + + // Write + void Put(Ch c) { RAPIDJSON_ASSERT(dst_ != 0); *dst_++ = c; } + + Ch* PutBegin() { return dst_ = src_; } + size_t PutEnd(Ch* begin) { return static_cast(dst_ - begin); } + void Flush() {} + + Ch* Push(size_t count) { Ch* begin = dst_; dst_ += count; return begin; } + void Pop(size_t count) { dst_ -= count; } + + Ch* src_; + Ch* dst_; + Ch* head_; +}; + +template +struct StreamTraits > { + enum { copyOptimization = 1 }; +}; + +//! Insitu string stream with UTF8 encoding. +typedef GenericInsituStringStream > InsituStringStream; + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_STREAM_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/stringbuffer.h b/include/liblvgl/libs/thorvg/rapidjson/stringbuffer.h new file mode 100644 index 00000000..17bfeac9 --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/stringbuffer.h @@ -0,0 +1,121 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_STRINGBUFFER_H_ +#define RAPIDJSON_STRINGBUFFER_H_ + +#include "stream.h" +#include "internal/stack.h" + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS +#include // std::move +#endif + +#include "internal/stack.h" + +#if defined(__clang__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(c++98-compat) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Represents an in-memory output stream. +/*! + \tparam Encoding Encoding of the stream. + \tparam Allocator type for allocating memory buffer. + \note implements Stream concept +*/ +template +class GenericStringBuffer { +public: + typedef typename Encoding::Ch Ch; + + GenericStringBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {} + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericStringBuffer(GenericStringBuffer&& rhs) : stack_(std::move(rhs.stack_)) {} + GenericStringBuffer& operator=(GenericStringBuffer&& rhs) { + if (&rhs != this) + stack_ = std::move(rhs.stack_); + return *this; + } +#endif + + void Put(Ch c) { *stack_.template Push() = c; } + void PutUnsafe(Ch c) { *stack_.template PushUnsafe() = c; } + void Flush() {} + + void Clear() { stack_.Clear(); } + void ShrinkToFit() { + // Push and pop a null terminator. This is safe. + *stack_.template Push() = '\0'; + stack_.ShrinkToFit(); + stack_.template Pop(1); + } + + void Reserve(size_t count) { stack_.template Reserve(count); } + Ch* Push(size_t count) { return stack_.template Push(count); } + Ch* PushUnsafe(size_t count) { return stack_.template PushUnsafe(count); } + void Pop(size_t count) { stack_.template Pop(count); } + + const Ch* GetString() const { + // Push and pop a null terminator. This is safe. + *stack_.template Push() = '\0'; + stack_.template Pop(1); + + return stack_.template Bottom(); + } + + //! Get the size of string in bytes in the string buffer. + size_t GetSize() const { return stack_.GetSize(); } + + //! Get the length of string in Ch in the string buffer. + size_t GetLength() const { return stack_.GetSize() / sizeof(Ch); } + + static const size_t kDefaultCapacity = 256; + mutable internal::Stack stack_; + +private: + // Prohibit copy constructor & assignment operator. + GenericStringBuffer(const GenericStringBuffer&); + GenericStringBuffer& operator=(const GenericStringBuffer&); +}; + +//! String buffer with UTF8 encoding +typedef GenericStringBuffer > StringBuffer; + +template +inline void PutReserve(GenericStringBuffer& stream, size_t count) { + stream.Reserve(count); +} + +template +inline void PutUnsafe(GenericStringBuffer& stream, typename Encoding::Ch c) { + stream.PutUnsafe(c); +} + +//! Implement specialized version of PutN() with memset() for better performance. +template<> +inline void PutN(GenericStringBuffer >& stream, char c, size_t n) { + std::memset(stream.stack_.Push(n), c, n * sizeof(c)); +} + +RAPIDJSON_NAMESPACE_END + +#if defined(__clang__) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_STRINGBUFFER_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/uri.h b/include/liblvgl/libs/thorvg/rapidjson/uri.h new file mode 100644 index 00000000..f93e508a --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/uri.h @@ -0,0 +1,481 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// (C) Copyright IBM Corporation 2021 +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_URI_H_ +#define RAPIDJSON_URI_H_ + +#include "internal/strfunc.h" + +#if defined(__clang__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(c++98-compat) +#elif defined(_MSC_VER) +RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// GenericUri + +template +class GenericUri { +public: + typedef typename ValueType::Ch Ch; +#if RAPIDJSON_HAS_STDSTRING + typedef std::basic_string String; +#endif + + //! Constructors + GenericUri(Allocator* allocator = 0) : uri_(), base_(), scheme_(), auth_(), path_(), query_(), frag_(), allocator_(allocator), ownAllocator_() { + } + + GenericUri(const Ch* uri, SizeType len, Allocator* allocator = 0) : uri_(), base_(), scheme_(), auth_(), path_(), query_(), frag_(), allocator_(allocator), ownAllocator_() { + Parse(uri, len); + } + + GenericUri(const Ch* uri, Allocator* allocator = 0) : uri_(), base_(), scheme_(), auth_(), path_(), query_(), frag_(), allocator_(allocator), ownAllocator_() { + Parse(uri, internal::StrLen(uri)); + } + + // Use with specializations of GenericValue + template GenericUri(const T& uri, Allocator* allocator = 0) : uri_(), base_(), scheme_(), auth_(), path_(), query_(), frag_(), allocator_(allocator), ownAllocator_() { + const Ch* u = uri.template Get(); // TypeHelper from document.h + Parse(u, internal::StrLen(u)); + } + +#if RAPIDJSON_HAS_STDSTRING + GenericUri(const String& uri, Allocator* allocator = 0) : uri_(), base_(), scheme_(), auth_(), path_(), query_(), frag_(), allocator_(allocator), ownAllocator_() { + Parse(uri.c_str(), internal::StrLen(uri.c_str())); + } +#endif + + //! Copy constructor + GenericUri(const GenericUri& rhs) : uri_(), base_(), scheme_(), auth_(), path_(), query_(), frag_(), allocator_(), ownAllocator_() { + *this = rhs; + } + + //! Copy constructor + GenericUri(const GenericUri& rhs, Allocator* allocator) : uri_(), base_(), scheme_(), auth_(), path_(), query_(), frag_(), allocator_(allocator), ownAllocator_() { + *this = rhs; + } + + //! Destructor. + ~GenericUri() { + Free(); + RAPIDJSON_DELETE(ownAllocator_); + } + + //! Assignment operator + GenericUri& operator=(const GenericUri& rhs) { + if (this != &rhs) { + // Do not delete ownAllocator + Free(); + Allocate(rhs.GetStringLength()); + auth_ = CopyPart(scheme_, rhs.scheme_, rhs.GetSchemeStringLength()); + path_ = CopyPart(auth_, rhs.auth_, rhs.GetAuthStringLength()); + query_ = CopyPart(path_, rhs.path_, rhs.GetPathStringLength()); + frag_ = CopyPart(query_, rhs.query_, rhs.GetQueryStringLength()); + base_ = CopyPart(frag_, rhs.frag_, rhs.GetFragStringLength()); + uri_ = CopyPart(base_, rhs.base_, rhs.GetBaseStringLength()); + CopyPart(uri_, rhs.uri_, rhs.GetStringLength()); + } + return *this; + } + + //! Getters + // Use with specializations of GenericValue + template void Get(T& uri, Allocator& allocator) { + uri.template Set(this->GetString(), allocator); // TypeHelper from document.h + } + + const Ch* GetString() const { return uri_; } + SizeType GetStringLength() const { return uri_ == 0 ? 0 : internal::StrLen(uri_); } + const Ch* GetBaseString() const { return base_; } + SizeType GetBaseStringLength() const { return base_ == 0 ? 0 : internal::StrLen(base_); } + const Ch* GetSchemeString() const { return scheme_; } + SizeType GetSchemeStringLength() const { return scheme_ == 0 ? 0 : internal::StrLen(scheme_); } + const Ch* GetAuthString() const { return auth_; } + SizeType GetAuthStringLength() const { return auth_ == 0 ? 0 : internal::StrLen(auth_); } + const Ch* GetPathString() const { return path_; } + SizeType GetPathStringLength() const { return path_ == 0 ? 0 : internal::StrLen(path_); } + const Ch* GetQueryString() const { return query_; } + SizeType GetQueryStringLength() const { return query_ == 0 ? 0 : internal::StrLen(query_); } + const Ch* GetFragString() const { return frag_; } + SizeType GetFragStringLength() const { return frag_ == 0 ? 0 : internal::StrLen(frag_); } + +#if RAPIDJSON_HAS_STDSTRING + static String Get(const GenericUri& uri) { return String(uri.GetString(), uri.GetStringLength()); } + static String GetBase(const GenericUri& uri) { return String(uri.GetBaseString(), uri.GetBaseStringLength()); } + static String GetScheme(const GenericUri& uri) { return String(uri.GetSchemeString(), uri.GetSchemeStringLength()); } + static String GetAuth(const GenericUri& uri) { return String(uri.GetAuthString(), uri.GetAuthStringLength()); } + static String GetPath(const GenericUri& uri) { return String(uri.GetPathString(), uri.GetPathStringLength()); } + static String GetQuery(const GenericUri& uri) { return String(uri.GetQueryString(), uri.GetQueryStringLength()); } + static String GetFrag(const GenericUri& uri) { return String(uri.GetFragString(), uri.GetFragStringLength()); } +#endif + + //! Equality operators + bool operator==(const GenericUri& rhs) const { + return Match(rhs, true); + } + + bool operator!=(const GenericUri& rhs) const { + return !Match(rhs, true); + } + + bool Match(const GenericUri& uri, bool full = true) const { + Ch* s1; + Ch* s2; + if (full) { + s1 = uri_; + s2 = uri.uri_; + } else { + s1 = base_; + s2 = uri.base_; + } + if (s1 == s2) return true; + if (s1 == 0 || s2 == 0) return false; + return internal::StrCmp(s1, s2) == 0; + } + + //! Resolve this URI against another (base) URI in accordance with URI resolution rules. + // See https://tools.ietf.org/html/rfc3986 + // Use for resolving an id or $ref with an in-scope id. + // Returns a new GenericUri for the resolved URI. + GenericUri Resolve(const GenericUri& baseuri, Allocator* allocator = 0) { + GenericUri resuri; + resuri.allocator_ = allocator; + // Ensure enough space for combining paths + resuri.Allocate(GetStringLength() + baseuri.GetStringLength() + 1); // + 1 for joining slash + + if (!(GetSchemeStringLength() == 0)) { + // Use all of this URI + resuri.auth_ = CopyPart(resuri.scheme_, scheme_, GetSchemeStringLength()); + resuri.path_ = CopyPart(resuri.auth_, auth_, GetAuthStringLength()); + resuri.query_ = CopyPart(resuri.path_, path_, GetPathStringLength()); + resuri.frag_ = CopyPart(resuri.query_, query_, GetQueryStringLength()); + resuri.RemoveDotSegments(); + } else { + // Use the base scheme + resuri.auth_ = CopyPart(resuri.scheme_, baseuri.scheme_, baseuri.GetSchemeStringLength()); + if (!(GetAuthStringLength() == 0)) { + // Use this auth, path, query + resuri.path_ = CopyPart(resuri.auth_, auth_, GetAuthStringLength()); + resuri.query_ = CopyPart(resuri.path_, path_, GetPathStringLength()); + resuri.frag_ = CopyPart(resuri.query_, query_, GetQueryStringLength()); + resuri.RemoveDotSegments(); + } else { + // Use the base auth + resuri.path_ = CopyPart(resuri.auth_, baseuri.auth_, baseuri.GetAuthStringLength()); + if (GetPathStringLength() == 0) { + // Use the base path + resuri.query_ = CopyPart(resuri.path_, baseuri.path_, baseuri.GetPathStringLength()); + if (GetQueryStringLength() == 0) { + // Use the base query + resuri.frag_ = CopyPart(resuri.query_, baseuri.query_, baseuri.GetQueryStringLength()); + } else { + // Use this query + resuri.frag_ = CopyPart(resuri.query_, query_, GetQueryStringLength()); + } + } else { + if (path_[0] == '/') { + // Absolute path - use all of this path + resuri.query_ = CopyPart(resuri.path_, path_, GetPathStringLength()); + resuri.RemoveDotSegments(); + } else { + // Relative path - append this path to base path after base path's last slash + size_t pos = 0; + if (!(baseuri.GetAuthStringLength() == 0) && baseuri.GetPathStringLength() == 0) { + resuri.path_[pos] = '/'; + pos++; + } + size_t lastslashpos = baseuri.GetPathStringLength(); + while (lastslashpos > 0) { + if (baseuri.path_[lastslashpos - 1] == '/') break; + lastslashpos--; + } + std::memcpy(&resuri.path_[pos], baseuri.path_, lastslashpos * sizeof(Ch)); + pos += lastslashpos; + resuri.query_ = CopyPart(&resuri.path_[pos], path_, GetPathStringLength()); + resuri.RemoveDotSegments(); + } + // Use this query + resuri.frag_ = CopyPart(resuri.query_, query_, GetQueryStringLength()); + } + } + } + // Always use this frag + resuri.base_ = CopyPart(resuri.frag_, frag_, GetFragStringLength()); + + // Re-constitute base_ and uri_ + resuri.SetBase(); + resuri.uri_ = resuri.base_ + resuri.GetBaseStringLength() + 1; + resuri.SetUri(); + return resuri; + } + + //! Get the allocator of this GenericUri. + Allocator& GetAllocator() { return *allocator_; } + +private: + // Allocate memory for a URI + // Returns total amount allocated + std::size_t Allocate(std::size_t len) { + // Create own allocator if user did not supply. + if (!allocator_) + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)(); + + // Allocate one block containing each part of the URI (5) plus base plus full URI, all null terminated. + // Order: scheme, auth, path, query, frag, base, uri + // Note need to set, increment, assign in 3 stages to avoid compiler warning bug. + size_t total = (3 * len + 7) * sizeof(Ch); + scheme_ = static_cast(allocator_->Malloc(total)); + *scheme_ = '\0'; + auth_ = scheme_; + auth_++; + *auth_ = '\0'; + path_ = auth_; + path_++; + *path_ = '\0'; + query_ = path_; + query_++; + *query_ = '\0'; + frag_ = query_; + frag_++; + *frag_ = '\0'; + base_ = frag_; + base_++; + *base_ = '\0'; + uri_ = base_; + uri_++; + *uri_ = '\0'; + return total; + } + + // Free memory for a URI + void Free() { + if (scheme_) { + Allocator::Free(scheme_); + scheme_ = 0; + } + } + + // Parse a URI into constituent scheme, authority, path, query, & fragment parts + // Supports URIs that match regex ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))? as per + // https://tools.ietf.org/html/rfc3986 + void Parse(const Ch* uri, std::size_t len) { + std::size_t start = 0, pos1 = 0, pos2 = 0; + Allocate(len); + + // Look for scheme ([^:/?#]+):)? + if (start < len) { + while (pos1 < len) { + if (uri[pos1] == ':') break; + pos1++; + } + if (pos1 != len) { + while (pos2 < len) { + if (uri[pos2] == '/') break; + if (uri[pos2] == '?') break; + if (uri[pos2] == '#') break; + pos2++; + } + if (pos1 < pos2) { + pos1++; + std::memcpy(scheme_, &uri[start], pos1 * sizeof(Ch)); + scheme_[pos1] = '\0'; + start = pos1; + } + } + } + // Look for auth (//([^/?#]*))? + // Note need to set, increment, assign in 3 stages to avoid compiler warning bug. + auth_ = scheme_ + GetSchemeStringLength(); + auth_++; + *auth_ = '\0'; + if (start < len - 1 && uri[start] == '/' && uri[start + 1] == '/') { + pos2 = start + 2; + while (pos2 < len) { + if (uri[pos2] == '/') break; + if (uri[pos2] == '?') break; + if (uri[pos2] == '#') break; + pos2++; + } + std::memcpy(auth_, &uri[start], (pos2 - start) * sizeof(Ch)); + auth_[pos2 - start] = '\0'; + start = pos2; + } + // Look for path ([^?#]*) + // Note need to set, increment, assign in 3 stages to avoid compiler warning bug. + path_ = auth_ + GetAuthStringLength(); + path_++; + *path_ = '\0'; + if (start < len) { + pos2 = start; + while (pos2 < len) { + if (uri[pos2] == '?') break; + if (uri[pos2] == '#') break; + pos2++; + } + if (start != pos2) { + std::memcpy(path_, &uri[start], (pos2 - start) * sizeof(Ch)); + path_[pos2 - start] = '\0'; + if (path_[0] == '/') + RemoveDotSegments(); // absolute path - normalize + start = pos2; + } + } + // Look for query (\?([^#]*))? + // Note need to set, increment, assign in 3 stages to avoid compiler warning bug. + query_ = path_ + GetPathStringLength(); + query_++; + *query_ = '\0'; + if (start < len && uri[start] == '?') { + pos2 = start + 1; + while (pos2 < len) { + if (uri[pos2] == '#') break; + pos2++; + } + if (start != pos2) { + std::memcpy(query_, &uri[start], (pos2 - start) * sizeof(Ch)); + query_[pos2 - start] = '\0'; + start = pos2; + } + } + // Look for fragment (#(.*))? + // Note need to set, increment, assign in 3 stages to avoid compiler warning bug. + frag_ = query_ + GetQueryStringLength(); + frag_++; + *frag_ = '\0'; + if (start < len && uri[start] == '#') { + std::memcpy(frag_, &uri[start], (len - start) * sizeof(Ch)); + frag_[len - start] = '\0'; + } + + // Re-constitute base_ and uri_ + base_ = frag_ + GetFragStringLength() + 1; + SetBase(); + uri_ = base_ + GetBaseStringLength() + 1; + SetUri(); + } + + // Reconstitute base + void SetBase() { + Ch* next = base_; + std::memcpy(next, scheme_, GetSchemeStringLength() * sizeof(Ch)); + next+= GetSchemeStringLength(); + std::memcpy(next, auth_, GetAuthStringLength() * sizeof(Ch)); + next+= GetAuthStringLength(); + std::memcpy(next, path_, GetPathStringLength() * sizeof(Ch)); + next+= GetPathStringLength(); + std::memcpy(next, query_, GetQueryStringLength() * sizeof(Ch)); + next+= GetQueryStringLength(); + *next = '\0'; + } + + // Reconstitute uri + void SetUri() { + Ch* next = uri_; + std::memcpy(next, base_, GetBaseStringLength() * sizeof(Ch)); + next+= GetBaseStringLength(); + std::memcpy(next, frag_, GetFragStringLength() * sizeof(Ch)); + next+= GetFragStringLength(); + *next = '\0'; + } + + // Copy a part from one GenericUri to another + // Return the pointer to the next part to be copied to + Ch* CopyPart(Ch* to, Ch* from, std::size_t len) { + RAPIDJSON_ASSERT(to != 0); + RAPIDJSON_ASSERT(from != 0); + std::memcpy(to, from, len * sizeof(Ch)); + to[len] = '\0'; + Ch* next = to + len + 1; + return next; + } + + // Remove . and .. segments from the path_ member. + // https://tools.ietf.org/html/rfc3986 + // This is done in place as we are only removing segments. + void RemoveDotSegments() { + std::size_t pathlen = GetPathStringLength(); + std::size_t pathpos = 0; // Position in path_ + std::size_t newpos = 0; // Position in new path_ + + // Loop through each segment in original path_ + while (pathpos < pathlen) { + // Get next segment, bounded by '/' or end + size_t slashpos = 0; + while ((pathpos + slashpos) < pathlen) { + if (path_[pathpos + slashpos] == '/') break; + slashpos++; + } + // Check for .. and . segments + if (slashpos == 2 && path_[pathpos] == '.' && path_[pathpos + 1] == '.') { + // Backup a .. segment in the new path_ + // We expect to find a previously added slash at the end or nothing + RAPIDJSON_ASSERT(newpos == 0 || path_[newpos - 1] == '/'); + size_t lastslashpos = newpos; + // Make sure we don't go beyond the start segment + if (lastslashpos > 1) { + // Find the next to last slash and back up to it + lastslashpos--; + while (lastslashpos > 0) { + if (path_[lastslashpos - 1] == '/') break; + lastslashpos--; + } + // Set the new path_ position + newpos = lastslashpos; + } + } else if (slashpos == 1 && path_[pathpos] == '.') { + // Discard . segment, leaves new path_ unchanged + } else { + // Move any other kind of segment to the new path_ + RAPIDJSON_ASSERT(newpos <= pathpos); + std::memmove(&path_[newpos], &path_[pathpos], slashpos * sizeof(Ch)); + newpos += slashpos; + // Add slash if not at end + if ((pathpos + slashpos) < pathlen) { + path_[newpos] = '/'; + newpos++; + } + } + // Move to next segment + pathpos += slashpos + 1; + } + path_[newpos] = '\0'; + } + + Ch* uri_; // Everything + Ch* base_; // Everything except fragment + Ch* scheme_; // Includes the : + Ch* auth_; // Includes the // + Ch* path_; // Absolute if starts with / + Ch* query_; // Includes the ? + Ch* frag_; // Includes the # + + Allocator* allocator_; //!< The current allocator. It is either user-supplied or equal to ownAllocator_. + Allocator* ownAllocator_; //!< Allocator owned by this Uri. +}; + +//! GenericUri for Value (UTF-8, default allocator). +typedef GenericUri Uri; + +RAPIDJSON_NAMESPACE_END + +#if defined(__clang__) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_URI_H_ diff --git a/include/liblvgl/libs/thorvg/rapidjson/writer.h b/include/liblvgl/libs/thorvg/rapidjson/writer.h new file mode 100644 index 00000000..81f34fc8 --- /dev/null +++ b/include/liblvgl/libs/thorvg/rapidjson/writer.h @@ -0,0 +1,710 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_WRITER_H_ +#define RAPIDJSON_WRITER_H_ + +#include "stream.h" +#include "internal/clzll.h" +#include "internal/meta.h" +#include "internal/stack.h" +#include "internal/strfunc.h" +#include "internal/dtoa.h" +#include "internal/itoa.h" +#include "stringbuffer.h" +#include // placement new + +#if defined(RAPIDJSON_SIMD) && defined(_MSC_VER) +#include +#pragma intrinsic(_BitScanForward) +#endif +#ifdef RAPIDJSON_SSE42 +#include +#elif defined(RAPIDJSON_SSE2) +#include +#elif defined(RAPIDJSON_NEON) +#include +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +RAPIDJSON_DIAG_OFF(unreachable-code) +RAPIDJSON_DIAG_OFF(c++98-compat) +#elif defined(_MSC_VER) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// WriteFlag + +/*! \def RAPIDJSON_WRITE_DEFAULT_FLAGS + \ingroup RAPIDJSON_CONFIG + \brief User-defined kWriteDefaultFlags definition. + + User can define this as any \c WriteFlag combinations. +*/ +#ifndef RAPIDJSON_WRITE_DEFAULT_FLAGS +#define RAPIDJSON_WRITE_DEFAULT_FLAGS kWriteNoFlags +#endif + +//! Combination of writeFlags +enum WriteFlag { + kWriteNoFlags = 0, //!< No flags are set. + kWriteValidateEncodingFlag = 1, //!< Validate encoding of JSON strings. + kWriteNanAndInfFlag = 2, //!< Allow writing of Infinity, -Infinity and NaN. + kWriteDefaultFlags = RAPIDJSON_WRITE_DEFAULT_FLAGS //!< Default write flags. Can be customized by defining RAPIDJSON_WRITE_DEFAULT_FLAGS +}; + +//! JSON writer +/*! Writer implements the concept Handler. + It generates JSON text by events to an output os. + + User may programmatically calls the functions of a writer to generate JSON text. + + On the other side, a writer can also be passed to objects that generates events, + + for example Reader::Parse() and Document::Accept(). + + \tparam OutputStream Type of output stream. + \tparam SourceEncoding Encoding of source string. + \tparam TargetEncoding Encoding of output stream. + \tparam StackAllocator Type of allocator for allocating memory of stack. + \note implements Handler concept +*/ +template, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator, unsigned writeFlags = kWriteDefaultFlags> +class Writer { +public: + typedef typename SourceEncoding::Ch Ch; + + static const int kDefaultMaxDecimalPlaces = 324; + + //! Constructor + /*! \param os Output stream. + \param stackAllocator User supplied allocator. If it is null, it will create a private one. + \param levelDepth Initial capacity of stack. + */ + explicit + Writer(OutputStream& os, StackAllocator* stackAllocator = 0, size_t levelDepth = kDefaultLevelDepth) : + os_(&os), level_stack_(stackAllocator, levelDepth * sizeof(Level)), maxDecimalPlaces_(kDefaultMaxDecimalPlaces), hasRoot_(false) {} + + explicit + Writer(StackAllocator* allocator = 0, size_t levelDepth = kDefaultLevelDepth) : + os_(0), level_stack_(allocator, levelDepth * sizeof(Level)), maxDecimalPlaces_(kDefaultMaxDecimalPlaces), hasRoot_(false) {} + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + Writer(Writer&& rhs) : + os_(rhs.os_), level_stack_(std::move(rhs.level_stack_)), maxDecimalPlaces_(rhs.maxDecimalPlaces_), hasRoot_(rhs.hasRoot_) { + rhs.os_ = 0; + } +#endif + + //! Reset the writer with a new stream. + /*! + This function reset the writer with a new stream and default settings, + in order to make a Writer object reusable for output multiple JSONs. + + \param os New output stream. + \code + Writer writer(os1); + writer.StartObject(); + // ... + writer.EndObject(); + + writer.Reset(os2); + writer.StartObject(); + // ... + writer.EndObject(); + \endcode + */ + void Reset(OutputStream& os) { + os_ = &os; + hasRoot_ = false; + level_stack_.Clear(); + } + + //! Checks whether the output is a complete JSON. + /*! + A complete JSON has a complete root object or array. + */ + bool IsComplete() const { + return hasRoot_ && level_stack_.Empty(); + } + + int GetMaxDecimalPlaces() const { + return maxDecimalPlaces_; + } + + //! Sets the maximum number of decimal places for double output. + /*! + This setting truncates the output with specified number of decimal places. + + For example, + + \code + writer.SetMaxDecimalPlaces(3); + writer.StartArray(); + writer.Double(0.12345); // "0.123" + writer.Double(0.0001); // "0.0" + writer.Double(1.234567890123456e30); // "1.234567890123456e30" (do not truncate significand for positive exponent) + writer.Double(1.23e-4); // "0.0" (do truncate significand for negative exponent) + writer.EndArray(); + \endcode + + The default setting does not truncate any decimal places. You can restore to this setting by calling + \code + writer.SetMaxDecimalPlaces(Writer::kDefaultMaxDecimalPlaces); + \endcode + */ + void SetMaxDecimalPlaces(int maxDecimalPlaces) { + maxDecimalPlaces_ = maxDecimalPlaces; + } + + /*!@name Implementation of Handler + \see Handler + */ + //@{ + + bool Null() { Prefix(kNullType); return EndValue(WriteNull()); } + bool Bool(bool b) { Prefix(b ? kTrueType : kFalseType); return EndValue(WriteBool(b)); } + bool Int(int i) { Prefix(kNumberType); return EndValue(WriteInt(i)); } + bool Uint(unsigned u) { Prefix(kNumberType); return EndValue(WriteUint(u)); } + bool Int64(int64_t i64) { Prefix(kNumberType); return EndValue(WriteInt64(i64)); } + bool Uint64(uint64_t u64) { Prefix(kNumberType); return EndValue(WriteUint64(u64)); } + + //! Writes the given \c double value to the stream + /*! + \param d The value to be written. + \return Whether it is succeed. + */ + bool Double(double d) { Prefix(kNumberType); return EndValue(WriteDouble(d)); } + + bool RawNumber(const Ch* str, SizeType length, bool copy = false) { + RAPIDJSON_ASSERT(str != 0); + (void)copy; + Prefix(kNumberType); + return EndValue(WriteString(str, length)); + } + + bool String(const Ch* str, SizeType length, bool copy = false) { + RAPIDJSON_ASSERT(str != 0); + (void)copy; + Prefix(kStringType); + return EndValue(WriteString(str, length)); + } + +#if RAPIDJSON_HAS_STDSTRING + bool String(const std::basic_string& str) { + return String(str.data(), SizeType(str.size())); + } +#endif + + bool StartObject() { + Prefix(kObjectType); + new (level_stack_.template Push()) Level(false); + return WriteStartObject(); + } + + bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); } + +#if RAPIDJSON_HAS_STDSTRING + bool Key(const std::basic_string& str) + { + return Key(str.data(), SizeType(str.size())); + } +#endif + + bool EndObject(SizeType memberCount = 0) { + (void)memberCount; + RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level)); // not inside an Object + RAPIDJSON_ASSERT(!level_stack_.template Top()->inArray); // currently inside an Array, not Object + RAPIDJSON_ASSERT(0 == level_stack_.template Top()->valueCount % 2); // Object has a Key without a Value + level_stack_.template Pop(1); + return EndValue(WriteEndObject()); + } + + bool StartArray() { + Prefix(kArrayType); + new (level_stack_.template Push()) Level(true); + return WriteStartArray(); + } + + bool EndArray(SizeType elementCount = 0) { + (void)elementCount; + RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level)); + RAPIDJSON_ASSERT(level_stack_.template Top()->inArray); + level_stack_.template Pop(1); + return EndValue(WriteEndArray()); + } + //@} + + /*! @name Convenience extensions */ + //@{ + + //! Simpler but slower overload. + bool String(const Ch* const& str) { return String(str, internal::StrLen(str)); } + bool Key(const Ch* const& str) { return Key(str, internal::StrLen(str)); } + + //@} + + //! Write a raw JSON value. + /*! + For user to write a stringified JSON as a value. + + \param json A well-formed JSON value. It should not contain null character within [0, length - 1] range. + \param length Length of the json. + \param type Type of the root of json. + */ + bool RawValue(const Ch* json, size_t length, Type type) { + RAPIDJSON_ASSERT(json != 0); + Prefix(type); + return EndValue(WriteRawValue(json, length)); + } + + //! Flush the output stream. + /*! + Allows the user to flush the output stream immediately. + */ + void Flush() { + os_->Flush(); + } + + static const size_t kDefaultLevelDepth = 32; + +protected: + //! Information for each nested level + struct Level { + Level(bool inArray_) : valueCount(0), inArray(inArray_) {} + size_t valueCount; //!< number of values in this level + bool inArray; //!< true if in array, otherwise in object + }; + + bool WriteNull() { + PutReserve(*os_, 4); + PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'u'); PutUnsafe(*os_, 'l'); PutUnsafe(*os_, 'l'); return true; + } + + bool WriteBool(bool b) { + if (b) { + PutReserve(*os_, 4); + PutUnsafe(*os_, 't'); PutUnsafe(*os_, 'r'); PutUnsafe(*os_, 'u'); PutUnsafe(*os_, 'e'); + } + else { + PutReserve(*os_, 5); + PutUnsafe(*os_, 'f'); PutUnsafe(*os_, 'a'); PutUnsafe(*os_, 'l'); PutUnsafe(*os_, 's'); PutUnsafe(*os_, 'e'); + } + return true; + } + + bool WriteInt(int i) { + char buffer[11]; + const char* end = internal::i32toa(i, buffer); + PutReserve(*os_, static_cast(end - buffer)); + for (const char* p = buffer; p != end; ++p) + PutUnsafe(*os_, static_cast(*p)); + return true; + } + + bool WriteUint(unsigned u) { + char buffer[10]; + const char* end = internal::u32toa(u, buffer); + PutReserve(*os_, static_cast(end - buffer)); + for (const char* p = buffer; p != end; ++p) + PutUnsafe(*os_, static_cast(*p)); + return true; + } + + bool WriteInt64(int64_t i64) { + char buffer[21]; + const char* end = internal::i64toa(i64, buffer); + PutReserve(*os_, static_cast(end - buffer)); + for (const char* p = buffer; p != end; ++p) + PutUnsafe(*os_, static_cast(*p)); + return true; + } + + bool WriteUint64(uint64_t u64) { + char buffer[20]; + char* end = internal::u64toa(u64, buffer); + PutReserve(*os_, static_cast(end - buffer)); + for (char* p = buffer; p != end; ++p) + PutUnsafe(*os_, static_cast(*p)); + return true; + } + + bool WriteDouble(double d) { + if (internal::Double(d).IsNanOrInf()) { + if (!(writeFlags & kWriteNanAndInfFlag)) + return false; + if (internal::Double(d).IsNan()) { + PutReserve(*os_, 3); + PutUnsafe(*os_, 'N'); PutUnsafe(*os_, 'a'); PutUnsafe(*os_, 'N'); + return true; + } + if (internal::Double(d).Sign()) { + PutReserve(*os_, 9); + PutUnsafe(*os_, '-'); + } + else + PutReserve(*os_, 8); + PutUnsafe(*os_, 'I'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'f'); + PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 't'); PutUnsafe(*os_, 'y'); + return true; + } + + char buffer[25]; + char* end = internal::dtoa(d, buffer, maxDecimalPlaces_); + PutReserve(*os_, static_cast(end - buffer)); + for (char* p = buffer; p != end; ++p) + PutUnsafe(*os_, static_cast(*p)); + return true; + } + + bool WriteString(const Ch* str, SizeType length) { + static const typename OutputStream::Ch hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + static const char escape[256] = { +#define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 + //0 1 2 3 4 5 6 7 8 9 A B C D E F + 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'b', 't', 'n', 'u', 'f', 'r', 'u', 'u', // 00 + 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', // 10 + 0, 0, '"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20 + Z16, Z16, // 30~4F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'\\', 0, 0, 0, // 50 + Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16 // 60~FF +#undef Z16 + }; + + if (TargetEncoding::supportUnicode) + PutReserve(*os_, 2 + length * 6); // "\uxxxx..." + else + PutReserve(*os_, 2 + length * 12); // "\uxxxx\uyyyy..." + + PutUnsafe(*os_, '\"'); + GenericStringStream is(str); + while (ScanWriteUnescapedString(is, length)) { + const Ch c = is.Peek(); + if (!TargetEncoding::supportUnicode && static_cast(c) >= 0x80) { + // Unicode escaping + unsigned codepoint; + if (RAPIDJSON_UNLIKELY(!SourceEncoding::Decode(is, &codepoint))) + return false; + PutUnsafe(*os_, '\\'); + PutUnsafe(*os_, 'u'); + if (codepoint <= 0xD7FF || (codepoint >= 0xE000 && codepoint <= 0xFFFF)) { + PutUnsafe(*os_, hexDigits[(codepoint >> 12) & 15]); + PutUnsafe(*os_, hexDigits[(codepoint >> 8) & 15]); + PutUnsafe(*os_, hexDigits[(codepoint >> 4) & 15]); + PutUnsafe(*os_, hexDigits[(codepoint ) & 15]); + } + else { + RAPIDJSON_ASSERT(codepoint >= 0x010000 && codepoint <= 0x10FFFF); + // Surrogate pair + unsigned s = codepoint - 0x010000; + unsigned lead = (s >> 10) + 0xD800; + unsigned trail = (s & 0x3FF) + 0xDC00; + PutUnsafe(*os_, hexDigits[(lead >> 12) & 15]); + PutUnsafe(*os_, hexDigits[(lead >> 8) & 15]); + PutUnsafe(*os_, hexDigits[(lead >> 4) & 15]); + PutUnsafe(*os_, hexDigits[(lead ) & 15]); + PutUnsafe(*os_, '\\'); + PutUnsafe(*os_, 'u'); + PutUnsafe(*os_, hexDigits[(trail >> 12) & 15]); + PutUnsafe(*os_, hexDigits[(trail >> 8) & 15]); + PutUnsafe(*os_, hexDigits[(trail >> 4) & 15]); + PutUnsafe(*os_, hexDigits[(trail ) & 15]); + } + } + else if ((sizeof(Ch) == 1 || static_cast(c) < 256) && RAPIDJSON_UNLIKELY(escape[static_cast(c)])) { + is.Take(); + PutUnsafe(*os_, '\\'); + PutUnsafe(*os_, static_cast(escape[static_cast(c)])); + if (escape[static_cast(c)] == 'u') { + PutUnsafe(*os_, '0'); + PutUnsafe(*os_, '0'); + PutUnsafe(*os_, hexDigits[static_cast(c) >> 4]); + PutUnsafe(*os_, hexDigits[static_cast(c) & 0xF]); + } + } + else if (RAPIDJSON_UNLIKELY(!(writeFlags & kWriteValidateEncodingFlag ? + Transcoder::Validate(is, *os_) : + Transcoder::TranscodeUnsafe(is, *os_)))) + return false; + } + PutUnsafe(*os_, '\"'); + return true; + } + + bool ScanWriteUnescapedString(GenericStringStream& is, size_t length) { + return RAPIDJSON_LIKELY(is.Tell() < length); + } + + bool WriteStartObject() { os_->Put('{'); return true; } + bool WriteEndObject() { os_->Put('}'); return true; } + bool WriteStartArray() { os_->Put('['); return true; } + bool WriteEndArray() { os_->Put(']'); return true; } + + bool WriteRawValue(const Ch* json, size_t length) { + PutReserve(*os_, length); + GenericStringStream is(json); + while (RAPIDJSON_LIKELY(is.Tell() < length)) { + RAPIDJSON_ASSERT(is.Peek() != '\0'); + if (RAPIDJSON_UNLIKELY(!(writeFlags & kWriteValidateEncodingFlag ? + Transcoder::Validate(is, *os_) : + Transcoder::TranscodeUnsafe(is, *os_)))) + return false; + } + return true; + } + + void Prefix(Type type) { + (void)type; + if (RAPIDJSON_LIKELY(level_stack_.GetSize() != 0)) { // this value is not at root + Level* level = level_stack_.template Top(); + if (level->valueCount > 0) { + if (level->inArray) + os_->Put(','); // add comma if it is not the first element in array + else // in object + os_->Put((level->valueCount % 2 == 0) ? ',' : ':'); + } + if (!level->inArray && level->valueCount % 2 == 0) + RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even number should be a name + level->valueCount++; + } + else { + RAPIDJSON_ASSERT(!hasRoot_); // Should only has one and only one root. + hasRoot_ = true; + } + } + + // Flush the value if it is the top level one. + bool EndValue(bool ret) { + if (RAPIDJSON_UNLIKELY(level_stack_.Empty())) // end of json text + Flush(); + return ret; + } + + OutputStream* os_; + internal::Stack level_stack_; + int maxDecimalPlaces_; + bool hasRoot_; + +private: + // Prohibit copy constructor & assignment operator. + Writer(const Writer&); + Writer& operator=(const Writer&); +}; + +// Full specialization for StringStream to prevent memory copying + +template<> +inline bool Writer::WriteInt(int i) { + char *buffer = os_->Push(11); + const char* end = internal::i32toa(i, buffer); + os_->Pop(static_cast(11 - (end - buffer))); + return true; +} + +template<> +inline bool Writer::WriteUint(unsigned u) { + char *buffer = os_->Push(10); + const char* end = internal::u32toa(u, buffer); + os_->Pop(static_cast(10 - (end - buffer))); + return true; +} + +template<> +inline bool Writer::WriteInt64(int64_t i64) { + char *buffer = os_->Push(21); + const char* end = internal::i64toa(i64, buffer); + os_->Pop(static_cast(21 - (end - buffer))); + return true; +} + +template<> +inline bool Writer::WriteUint64(uint64_t u) { + char *buffer = os_->Push(20); + const char* end = internal::u64toa(u, buffer); + os_->Pop(static_cast(20 - (end - buffer))); + return true; +} + +template<> +inline bool Writer::WriteDouble(double d) { + if (internal::Double(d).IsNanOrInf()) { + // Note: This code path can only be reached if (RAPIDJSON_WRITE_DEFAULT_FLAGS & kWriteNanAndInfFlag). + if (!(kWriteDefaultFlags & kWriteNanAndInfFlag)) + return false; + if (internal::Double(d).IsNan()) { + PutReserve(*os_, 3); + PutUnsafe(*os_, 'N'); PutUnsafe(*os_, 'a'); PutUnsafe(*os_, 'N'); + return true; + } + if (internal::Double(d).Sign()) { + PutReserve(*os_, 9); + PutUnsafe(*os_, '-'); + } + else + PutReserve(*os_, 8); + PutUnsafe(*os_, 'I'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'f'); + PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 't'); PutUnsafe(*os_, 'y'); + return true; + } + + char *buffer = os_->Push(25); + char* end = internal::dtoa(d, buffer, maxDecimalPlaces_); + os_->Pop(static_cast(25 - (end - buffer))); + return true; +} + +#if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) +template<> +inline bool Writer::ScanWriteUnescapedString(StringStream& is, size_t length) { + if (length < 16) + return RAPIDJSON_LIKELY(is.Tell() < length); + + if (!RAPIDJSON_LIKELY(is.Tell() < length)) + return false; + + const char* p = is.src_; + const char* end = is.head_ + length; + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + const char* endAligned = reinterpret_cast(reinterpret_cast(end) & static_cast(~15)); + if (nextAligned > end) + return true; + + while (p != nextAligned) + if (*p < 0x20 || *p == '\"' || *p == '\\') { + is.src_ = p; + return RAPIDJSON_LIKELY(is.Tell() < length); + } + else + os_->PutUnsafe(*p++); + + // The rest of string using SIMD + static const char dquote[16] = { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' }; + static const char bslash[16] = { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' }; + static const char space[16] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F }; + const __m128i dq = _mm_loadu_si128(reinterpret_cast(&dquote[0])); + const __m128i bs = _mm_loadu_si128(reinterpret_cast(&bslash[0])); + const __m128i sp = _mm_loadu_si128(reinterpret_cast(&space[0])); + + for (; p != endAligned; p += 16) { + const __m128i s = _mm_load_si128(reinterpret_cast(p)); + const __m128i t1 = _mm_cmpeq_epi8(s, dq); + const __m128i t2 = _mm_cmpeq_epi8(s, bs); + const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x1F) == 0x1F + const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3); + unsigned short r = static_cast(_mm_movemask_epi8(x)); + if (RAPIDJSON_UNLIKELY(r != 0)) { // some of characters is escaped + SizeType len; +#ifdef _MSC_VER // Find the index of first escaped + unsigned long offset; + _BitScanForward(&offset, r); + len = offset; +#else + len = static_cast(__builtin_ffs(r) - 1); +#endif + char* q = reinterpret_cast(os_->PushUnsafe(len)); + for (size_t i = 0; i < len; i++) + q[i] = p[i]; + + p += len; + break; + } + _mm_storeu_si128(reinterpret_cast<__m128i *>(os_->PushUnsafe(16)), s); + } + + is.src_ = p; + return RAPIDJSON_LIKELY(is.Tell() < length); +} +#elif defined(RAPIDJSON_NEON) +template<> +inline bool Writer::ScanWriteUnescapedString(StringStream& is, size_t length) { + if (length < 16) + return RAPIDJSON_LIKELY(is.Tell() < length); + + if (!RAPIDJSON_LIKELY(is.Tell() < length)) + return false; + + const char* p = is.src_; + const char* end = is.head_ + length; + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + const char* endAligned = reinterpret_cast(reinterpret_cast(end) & static_cast(~15)); + if (nextAligned > end) + return true; + + while (p != nextAligned) + if (*p < 0x20 || *p == '\"' || *p == '\\') { + is.src_ = p; + return RAPIDJSON_LIKELY(is.Tell() < length); + } + else + os_->PutUnsafe(*p++); + + // The rest of string using SIMD + const uint8x16_t s0 = vmovq_n_u8('"'); + const uint8x16_t s1 = vmovq_n_u8('\\'); + const uint8x16_t s2 = vmovq_n_u8('\b'); + const uint8x16_t s3 = vmovq_n_u8(32); + + for (; p != endAligned; p += 16) { + const uint8x16_t s = vld1q_u8(reinterpret_cast(p)); + uint8x16_t x = vceqq_u8(s, s0); + x = vorrq_u8(x, vceqq_u8(s, s1)); + x = vorrq_u8(x, vceqq_u8(s, s2)); + x = vorrq_u8(x, vcltq_u8(s, s3)); + + x = vrev64q_u8(x); // Rev in 64 + uint64_t low = vgetq_lane_u64(vreinterpretq_u64_u8(x), 0); // extract + uint64_t high = vgetq_lane_u64(vreinterpretq_u64_u8(x), 1); // extract + + SizeType len = 0; + bool escaped = false; + if (low == 0) { + if (high != 0) { + uint32_t lz = internal::clzll(high); + len = 8 + (lz >> 3); + escaped = true; + } + } else { + uint32_t lz = internal::clzll(low); + len = lz >> 3; + escaped = true; + } + if (RAPIDJSON_UNLIKELY(escaped)) { // some of characters is escaped + char* q = reinterpret_cast(os_->PushUnsafe(len)); + for (size_t i = 0; i < len; i++) + q[i] = p[i]; + + p += len; + break; + } + vst1q_u8(reinterpret_cast(os_->PushUnsafe(16)), s); + } + + is.src_ = p; + return RAPIDJSON_LIKELY(is.Tell() < length); +} +#endif // RAPIDJSON_NEON + +RAPIDJSON_NAMESPACE_END + +#if defined(_MSC_VER) || defined(__clang__) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_RAPIDJSON_H_ diff --git a/include/liblvgl/libs/thorvg/thorvg.h b/include/liblvgl/libs/thorvg/thorvg.h new file mode 100644 index 00000000..da3944c5 --- /dev/null +++ b/include/liblvgl/libs/thorvg/thorvg.h @@ -0,0 +1,2182 @@ +#ifndef _THORVG_H_ +#define _THORVG_H_ + +#include "../../lv_conf_internal.h" + +/*Testing of dependencies*/ +#if LV_USE_THORVG && LV_USE_VECTOR_GRAPHIC == 0 +#error "ThorVG: LV_USE_VECTOR_GRAPHIC is required. Enable it in lv_conf.h" +#endif + +#if LV_USE_THORVG_INTERNAL +#define TVG_BUILD 1 + + +#include +#include +#include +#include + +#ifdef TVG_API + #undef TVG_API +#endif + +#ifndef TVG_STATIC + #ifdef _WIN32 + #if TVG_BUILD + #define TVG_API __declspec(dllexport) + #else + #define TVG_API __declspec(dllimport) + #endif + #elif (defined(__SUNPRO_C) || defined(__SUNPRO_CC)) + #define TVG_API __global + #else + #if (defined(__GNUC__) && __GNUC__ >= 4) || defined(__INTEL_COMPILER) + #define TVG_API __attribute__ ((visibility("default"))) + #else + #define TVG_API + #endif + #endif +#else + #define TVG_API +#endif + +#ifdef TVG_DEPRECATED + #undef TVG_DEPRECATED +#endif + +#ifdef _WIN32 + #define TVG_DEPRECATED __declspec(deprecated) +#elif __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) + #define TVG_DEPRECATED __attribute__ ((__deprecated__)) +#else + #define TVG_DEPRECATED +#endif + +#define _TVG_DECLARE_PRIVATE(A) \ + struct Impl; \ + Impl* pImpl; \ +protected: \ + A(const A&) = delete; \ + const A& operator=(const A&) = delete; \ + A() + +#define _TVG_DISABLE_CTOR(A) \ + A() = delete; \ + ~A() = delete + +#define _TVG_DECLARE_ACCESSOR(A) \ + friend A + +namespace tvg +{ + +class RenderMethod; +class Animation; + +/** + * @defgroup ThorVG ThorVG + * @brief ThorVG classes and enumerations providing C++ APIs. + */ + +/**@{*/ + +/** + * @brief Enumeration specifying the result from the APIs. + */ +enum class Result +{ + Success = 0, ///< The value returned in case of a correct request execution. + InvalidArguments, ///< The value returned in the event of a problem with the arguments given to the API - e.g. empty paths or null pointers. + InsufficientCondition, ///< The value returned in case the request cannot be processed - e.g. asking for properties of an object, which does not exist. + FailedAllocation, ///< The value returned in case of unsuccessful memory allocation. + MemoryCorruption, ///< The value returned in the event of bad memory handling - e.g. failing in pointer releasing or casting + NonSupport, ///< The value returned in case of choosing unsupported options. + Unknown ///< The value returned in all other cases. +}; + + +/** + * @brief Enumeration specifying the values of the path commands accepted by TVG. + * + * Not to be confused with the path commands from the svg path element (like M, L, Q, H and many others). + * TVG interprets all of them and translates to the ones from the PathCommand values. + */ +enum class PathCommand +{ + Close = 0, ///< Ends the current sub-path and connects it with its initial point. This command doesn't expect any points. + MoveTo, ///< Sets a new initial point of the sub-path and a new current point. This command expects 1 point: the starting position. + LineTo, ///< Draws a line from the current point to the given point and sets a new value of the current point. This command expects 1 point: the end-position of the line. + CubicTo ///< Draws a cubic Bezier curve from the current point to the given point using two given control points and sets a new value of the current point. This command expects 3 points: the 1st control-point, the 2nd control-point, the end-point of the curve. +}; + + +/** + * @brief Enumeration determining the ending type of a stroke in the open sub-paths. + */ +enum class StrokeCap +{ + Square = 0, ///< The stroke is extended in both end-points of a sub-path by a rectangle, with the width equal to the stroke width and the length equal to the half of the stroke width. For zero length sub-paths the square is rendered with the size of the stroke width. + Round, ///< The stroke is extended in both end-points of a sub-path by a half circle, with a radius equal to the half of a stroke width. For zero length sub-paths a full circle is rendered. + Butt ///< The stroke ends exactly at each of the two end-points of a sub-path. For zero length sub-paths no stroke is rendered. +}; + + +/** + * @brief Enumeration determining the style used at the corners of joined stroked path segments. + */ +enum class StrokeJoin +{ + Bevel = 0, ///< The outer corner of the joined path segments is bevelled at the join point. The triangular region of the corner is enclosed by a straight line between the outer corners of each stroke. + Round, ///< The outer corner of the joined path segments is rounded. The circular region is centered at the join point. + Miter ///< The outer corner of the joined path segments is spiked. The spike is created by extension beyond the join point of the outer edges of the stroke until they intersect. In case the extension goes beyond the limit, the join style is converted to the Bevel style. +}; + + +/** + * @brief Enumeration specifying how to fill the area outside the gradient bounds. + */ +enum class FillSpread +{ + Pad = 0, ///< The remaining area is filled with the closest stop color. + Reflect, ///< The gradient pattern is reflected outside the gradient area until the expected region is filled. + Repeat ///< The gradient pattern is repeated continuously beyond the gradient area until the expected region is filled. +}; + + +/** + * @brief Enumeration specifying the algorithm used to establish which parts of the shape are treated as the inside of the shape. + */ +enum class FillRule +{ + Winding = 0, ///< A line from the point to a location outside the shape is drawn. The intersections of the line with the path segment of the shape are counted. Starting from zero, if the path segment of the shape crosses the line clockwise, one is added, otherwise one is subtracted. If the resulting sum is non zero, the point is inside the shape. + EvenOdd ///< A line from the point to a location outside the shape is drawn and its intersections with the path segments of the shape are counted. If the number of intersections is an odd number, the point is inside the shape. +}; + + +/** + * @brief Enumeration indicating the method used in the composition of two objects - the target and the source. + * + * Notation: S(Source), T(Target), SA(Source Alpha), TA(Target Alpha) + * + * @see Paint::composite() + */ +enum class CompositeMethod +{ + None = 0, ///< No composition is applied. + ClipPath, ///< The intersection of the source and the target is determined and only the resulting pixels from the source are rendered. + AlphaMask, ///< Alpha Masking using the compositing target's pixels as an alpha value. + InvAlphaMask, ///< Alpha Masking using the complement to the compositing target's pixels as an alpha value. + LumaMask, ///< Alpha Masking using the grayscale (0.2125R + 0.7154G + 0.0721*B) of the compositing target's pixels. @since 0.9 + InvLumaMask, ///< Alpha Masking using the grayscale (0.2125R + 0.7154G + 0.0721*B) of the complement to the compositing target's pixels. + AddMask, ///< Combines the target and source objects pixels using target alpha. (T * TA) + (S * (255 - TA)) (Experimental API) + SubtractMask, ///< Subtracts the source color from the target color while considering their respective target alpha. (T * TA) - (S * (255 - TA)) (Experimental API) + IntersectMask, ///< Computes the result by taking the minimum value between the target alpha and the source alpha and multiplies it with the target color. (T * min(TA, SA)) (Experimental API) + DifferenceMask ///< Calculates the absolute difference between the target color and the source color multiplied by the complement of the target alpha. abs(T - S * (255 - TA)) (Experimental API) +}; + + +/** + * @brief Enumeration indicates the method used for blending paint. Please refer to the respective formulas for each method. + * + * Notation: S(source paint as the top layer), D(destination as the bottom layer), Sa(source paint alpha), Da(destination alpha) + * + * @see Paint::blend() + * + * @note Experimental API + */ +enum class BlendMethod : uint8_t +{ + Normal = 0, ///< Perform the alpha blending(default). S if (Sa == 255), otherwise (Sa * S) + (255 - Sa) * D + Add, ///< Simply adds pixel values of one layer with the other. (S + D) + Screen, ///< The values of the pixels in the two layers are inverted, multiplied, and then inverted again. (S + D) - (S * D) + Multiply, ///< Takes the RGB channel values from 0 to 255 of each pixel in the top layer and multiples them with the values for the corresponding pixel from the bottom layer. (S * D) + Overlay, ///< Combines Multiply and Screen blend modes. (2 * S * D) if (2 * D < Da), otherwise (Sa * Da) - 2 * (Da - S) * (Sa - D) + Difference, ///< Subtracts the bottom layer from the top layer or the other way around, to always get a non-negative value. (S - D) if (S > D), otherwise (D - S) + Exclusion, ///< The result is twice the product of the top and bottom layers, subtracted from their sum. s + d - (2 * s * d) + SrcOver, ///< Replace the bottom layer with the top layer. + Darken, ///< Creates a pixel that retains the smallest components of the top and bottom layer pixels. min(S, D) + Lighten, ///< Only has the opposite action of Darken Only. max(S, D) + ColorDodge, ///< Divides the bottom layer by the inverted top layer. D / (255 - S) + ColorBurn, ///< Divides the inverted bottom layer by the top layer, and then inverts the result. 255 - (255 - D) / S + HardLight, ///< The same as Overlay but with the color roles reversed. (2 * S * D) if (S < Sa), otherwise (Sa * Da) - 2 * (Da - S) * (Sa - D) + SoftLight ///< The same as Overlay but with applying pure black or white does not result in pure black or white. (1 - 2 * S) * (D ^ 2) + (2 * S * D) +}; + + +/** + * @brief Enumeration specifying the engine type used for the graphics backend. For multiple backends bitwise operation is allowed. + */ +enum class CanvasEngine +{ + Sw = (1 << 1), ///< CPU rasterizer. + Gl = (1 << 2), ///< OpenGL rasterizer. + Wg = (1 << 3), ///< WebGPU rasterizer. (Experimental API) +}; + + +/** + * @brief A data structure representing a point in two-dimensional space. + */ +struct Point +{ + float x, y; +}; + + +/** + * @brief A data structure representing a three-dimensional matrix. + * + * The elements e11, e12, e21 and e22 represent the rotation matrix, including the scaling factor. + * The elements e13 and e23 determine the translation of the object along the x and y-axis, respectively. + * The elements e31 and e32 are set to 0, e33 is set to 1. + */ +struct Matrix +{ + float e11, e12, e13; + float e21, e22, e23; + float e31, e32, e33; +}; + + +/** + * @brief A data structure representing a texture mesh vertex + * + * @param pt The vertex coordinate + * @param uv The normalized texture coordinate in the range (0.0..1.0, 0.0..1.0) + * + * @note Experimental API + */ +struct Vertex +{ + Point pt; + Point uv; +}; + + +/** + * @brief A data structure representing a triangle in a texture mesh + * + * @param vertex The three vertices that make up the polygon + * + * @note Experimental API + */ +struct Polygon +{ + Vertex vertex[3]; +}; + + +/** + * @class Paint + * + * @brief An abstract class for managing graphical elements. + * + * A graphical element in TVG is any object composed into a Canvas. + * Paint represents such a graphical object and its behaviors such as duplication, transformation and composition. + * TVG recommends the user to regard a paint as a set of volatile commands. They can prepare a Paint and then request a Canvas to run them. + */ +class TVG_API Paint +{ +public: + virtual ~Paint(); + + /** + * @brief Sets the angle by which the object is rotated. + * + * The angle in measured clockwise from the horizontal axis. + * The rotational axis passes through the point on the object with zero coordinates. + * + * @param[in] degree The value of the angle in degrees. + * + * @retval Result::Success when succeed, Result::FailedAllocation otherwise. + */ + Result rotate(float degree) noexcept; + + /** + * @brief Sets the scale value of the object. + * + * @param[in] factor The value of the scaling factor. The default value is 1. + * + * @retval Result::Success when succeed, Result::FailedAllocation otherwise. + */ + Result scale(float factor) noexcept; + + /** + * @brief Sets the values by which the object is moved in a two-dimensional space. + * + * The origin of the coordinate system is in the upper left corner of the canvas. + * The horizontal and vertical axes point to the right and down, respectively. + * + * @param[in] x The value of the horizontal shift. + * @param[in] y The value of the vertical shift. + * + * @retval Result::Success when succeed, Result::FailedAllocation otherwise. + */ + Result translate(float x, float y) noexcept; + + /** + * @brief Sets the matrix of the affine transformation for the object. + * + * The augmented matrix of the transformation is expected to be given. + * + * @param[in] m The 3x3 augmented matrix. + * + * @retval Result::Success when succeed, Result::FailedAllocation otherwise. + */ + Result transform(const Matrix& m) noexcept; + + /** + * @brief Gets the matrix of the affine transformation of the object. + * + * The values of the matrix can be set by the transform() API, as well by the translate(), + * scale() and rotate(). In case no transformation was applied, the identity matrix is returned. + * + * @return The augmented transformation matrix. + * + * @since 0.4 + */ + Matrix transform() noexcept; + + /** + * @brief Sets the opacity of the object. + * + * @param[in] o The opacity value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque. + * + * @retval Result::Success when succeed. + * + * @note Setting the opacity with this API may require multiple render pass for composition. It is recommended to avoid changing the opacity if possible. + * @note ClipPath won't use the opacity value. (see: enum class CompositeMethod::ClipPath) + */ + Result opacity(uint8_t o) noexcept; + + /** + * @brief Sets the composition target object and the composition method. + * + * @param[in] target The paint of the target object. + * @param[in] method The method used to composite the source object with the target. + * + * @retval Result::Success when succeed, Result::InvalidArguments otherwise. + */ + Result composite(std::unique_ptr target, CompositeMethod method) noexcept; + + /** + * @brief Sets the blending method for the paint object. + * + * The blending feature allows you to combine colors to create visually appealing effects, including transparency, lighting, shading, and color mixing, among others. + * its process involves the combination of colors or images from the source paint object with the destination (the lower layer image) using blending operations. + * The blending operation is determined by the chosen @p BlendMethod, which specifies how the colors or images are combined. + * + * @param[in] method The blending method to be set. + * + * @retval Result::Success when the blending method is successfully set. + * + * @note Experimental API + */ + Result blend(BlendMethod method) const noexcept; + + /** + * @brief Gets the bounding box of the paint object before any transformation. + * + * @param[out] x The x coordinate of the upper left corner of the object. + * @param[out] y The y coordinate of the upper left corner of the object. + * @param[out] w The width of the object. + * @param[out] h The height of the object. + * + * @return Result::Success when succeed, Result::InsufficientCondition otherwise. + * + * @note The bounding box doesn't indicate the final rendered region. It's the smallest rectangle that encloses the object. + * @see Paint::bounds(float* x, float* y, float* w, float* h, bool transformed); + * @deprecated Use bounds(float* x, float* y, float* w, float* h, bool transformed) instead + */ + TVG_DEPRECATED Result bounds(float* x, float* y, float* w, float* h) const noexcept; + + /** + * @brief Gets the axis-aligned bounding box of the paint object. + * + * In case @p transform is @c true, all object's transformations are applied first, and then the bounding box is established. Otherwise, the bounding box is determined before any transformations. + * + * @param[out] x The x coordinate of the upper left corner of the object. + * @param[out] y The y coordinate of the upper left corner of the object. + * @param[out] w The width of the object. + * @param[out] h The height of the object. + * @param[in] transformed If @c true, the paint's transformations are taken into account, otherwise they aren't. + * + * @retval Result::Success when succeed, Result::InsufficientCondition otherwise. + * + * @note The bounding box doesn't indicate the actual drawing region. It's the smallest rectangle that encloses the object. + */ + Result bounds(float* x, float* y, float* w, float* h, bool transformed) const noexcept; + + /** + * @brief Duplicates the object. + * + * Creates a new object and sets its all properties as in the original object. + * + * @return The created object when succeed, @c nullptr otherwise. + */ + Paint* duplicate() const noexcept; + + /** + * @brief Gets the opacity value of the object. + * + * @return The opacity value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque. + */ + uint8_t opacity() const noexcept; + + /** + * @brief Gets the composition target object and the composition method. + * + * @param[out] target The paint of the target object. + * + * @return The method used to composite the source object with the target. + * + * @since 0.5 + */ + CompositeMethod composite(const Paint** target) const noexcept; + + /** + * @brief Gets the blending method of the object. + * + * @return The blending method + * + * @note Experimental API + */ + BlendMethod blend() const noexcept; + + /** + * @brief Return the unique id value of the paint instance. + * + * This method can be called for checking the current concrete instance type. + * + * @return The type id of the Paint instance. + */ + uint32_t identifier() const noexcept; + + _TVG_DECLARE_PRIVATE(Paint); +}; + + +/** + * @class Fill + * + * @brief An abstract class representing the gradient fill of the Shape object. + * + * It contains the information about the gradient colors and their arrangement + * inside the gradient bounds. The gradients bounds are defined in the LinearGradient + * or RadialGradient class, depending on the type of the gradient to be used. + * It specifies the gradient behavior in case the area defined by the gradient bounds + * is smaller than the area to be filled. + */ +class TVG_API Fill +{ +public: + /** + * @brief A data structure storing the information about the color and its relative position inside the gradient bounds. + */ + struct ColorStop + { + float offset; /**< The relative position of the color. */ + uint8_t r; /**< The red color channel value in the range [0 ~ 255]. */ + uint8_t g; /**< The green color channel value in the range [0 ~ 255]. */ + uint8_t b; /**< The blue color channel value in the range [0 ~ 255]. */ + uint8_t a; /**< The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque. */ + }; + + virtual ~Fill(); + + /** + * @brief Sets the parameters of the colors of the gradient and their position. + * + * @param[in] colorStops An array of ColorStop data structure. + * @param[in] cnt The count of the @p colorStops array equal to the colors number used in the gradient. + * + * @retval Result::Success when succeed. + */ + Result colorStops(const ColorStop* colorStops, uint32_t cnt) noexcept; + + /** + * @brief Sets the FillSpread value, which specifies how to fill the area outside the gradient bounds. + * + * @param[in] s The FillSpread value. + * + * @retval Result::Success when succeed. + */ + Result spread(FillSpread s) noexcept; + + /** + * @brief Sets the matrix of the affine transformation for the gradient fill. + * + * The augmented matrix of the transformation is expected to be given. + * + * @param[in] m The 3x3 augmented matrix. + * + * @retval Result::Success when succeed, Result::FailedAllocation otherwise. + */ + Result transform(const Matrix& m) noexcept; + + /** + * @brief Gets the parameters of the colors of the gradient, their position and number. + * + * @param[out] colorStops A pointer to the memory location, where the array of the gradient's ColorStop is stored. + * + * @return The number of colors used in the gradient. This value corresponds to the length of the @p colorStops array. + */ + uint32_t colorStops(const ColorStop** colorStops) const noexcept; + + /** + * @brief Gets the FillSpread value of the fill. + * + * @return The FillSpread value of this Fill. + */ + FillSpread spread() const noexcept; + + /** + * @brief Gets the matrix of the affine transformation of the gradient fill. + * + * In case no transformation was applied, the identity matrix is returned. + * + * @return The augmented transformation matrix. + */ + Matrix transform() const noexcept; + + /** + * @brief Creates a copy of the Fill object. + * + * Return a newly created Fill object with the properties copied from the original. + * + * @return A copied Fill object when succeed, @c nullptr otherwise. + */ + Fill* duplicate() const noexcept; + + /** + * @brief Return the unique id value of the Fill instance. + * + * This method can be called for checking the current concrete instance type. + * + * @return The type id of the Fill instance. + */ + uint32_t identifier() const noexcept; + + _TVG_DECLARE_PRIVATE(Fill); +}; + + +/** + * @class Canvas + * + * @brief An abstract class for drawing graphical elements. + * + * A canvas is an entity responsible for drawing the target. It sets up the drawing engine and the buffer, which can be drawn on the screen. It also manages given Paint objects. + * + * @note A Canvas behavior depends on the raster engine though the final content of the buffer is expected to be identical. + * @warning The Paint objects belonging to one Canvas can't be shared among multiple Canvases. + */ +class TVG_API Canvas +{ +public: + Canvas(RenderMethod*); + virtual ~Canvas(); + + /** + * @brief Sets the size of the container, where all the paints pushed into the Canvas are stored. + * + * If the number of objects pushed into the Canvas is known in advance, calling the function + * prevents multiple memory reallocation, thus improving the performance. + * + * @param[in] n The number of objects for which the memory is to be reserved. + * + * @return Result::Success when succeed. + */ + TVG_DEPRECATED Result reserve(uint32_t n) noexcept; + + /** + * @brief Returns the list of the paints that currently held by the Canvas. + * + * This function provides the list of paint nodes, allowing users a direct opportunity to modify the scene tree. + * + * @warning Please avoid accessing the paints during Canvas update/draw. You can access them after calling sync(). + * @see Canvas::sync() + * + * @note Experimental API + */ + std::list& paints() noexcept; + + /** + * @brief Passes drawing elements to the Canvas using Paint objects. + * + * Only pushed paints in the canvas will be drawing targets. + * They are retained by the canvas until you call Canvas::clear(). + * + * @param[in] paint A Paint object to be drawn. + * + * @retval Result::Success When succeed. + * @retval Result::MemoryCorruption In case a @c nullptr is passed as the argument. + * @retval Result::InsufficientCondition An internal error. + * + * @note The rendering order of the paints is the same as the order as they were pushed into the canvas. Consider sorting the paints before pushing them if you intend to use layering. + * @see Canvas::paints() + * @see Canvas::clear() + */ + virtual Result push(std::unique_ptr paint) noexcept; + + /** + * @brief Clear the internal canvas resources that used for the drawing. + * + * This API sets the total number of paints pushed into the canvas to zero. + * Depending on the value of the @p free argument, the paints are either freed or retained. + * So if you need to update paint properties while maintaining the existing scene structure, you can set @p free = false. + * + * @param[in] free If @c true, the memory occupied by paints is deallocated, otherwise it is not. + * + * @retval Result::Success when succeed, Result::InsufficientCondition otherwise. + * + * @see Canvas::push() + * @see Canvas::paints() + */ + virtual Result clear(bool free = true) noexcept; + + /** + * @brief Request the canvas to update the paint objects. + * + * If a @c nullptr is passed all paint objects retained by the Canvas are updated, + * otherwise only the paint to which the given @p paint points. + * + * @param[in] paint A pointer to the Paint object or @c nullptr. + * + * @retval Result::Success when succeed, Result::InsufficientCondition otherwise. + * + * @note The Update behavior can be asynchronous if the assigned thread number is greater than zero. + */ + virtual Result update(Paint* paint = nullptr) noexcept; + + /** + * @brief Requests the canvas to draw the Paint objects. + * + * @retval Result::Success when succeed, Result::InsufficientCondition otherwise. + * + * @note Drawing can be asynchronous if the assigned thread number is greater than zero. To guarantee the drawing is done, call sync() afterwards. + * @see Canvas::sync() + */ + virtual Result draw() noexcept; + + /** + * @brief Sets the drawing region in the canvas. + * + * This function defines the rectangular area of the canvas that will be used for drawing operations. + * The specified viewport is used to clip the rendering output to the boundaries of the rectangle. + * + * @param[in] x The x-coordinate of the upper-left corner of the rectangle. + * @param[in] y The y-coordinate of the upper-left corner of the rectangle. + * @param[in] w The width of the rectangle. + * @param[in] h The height of the rectangle. + * + * @retval Result::Success when succeed, Result::InsufficientCondition otherwise. + * + * @see SwCanvas::target() + * @see GlCanvas::target() + * @see WgCanvas::target() + * + * @warning It's not allowed to change the viewport during Canvas::push() - Canvas::sync() or Canvas::update() - Canvas::sync(). + * + * @note When resetting the target, the viewport will also be reset to the target size. + * @note Experimental API + */ + virtual Result viewport(int32_t x, int32_t y, int32_t w, int32_t h) noexcept; + + /** + * @brief Guarantees that drawing task is finished. + * + * The Canvas rendering can be performed asynchronously. To make sure that rendering is finished, + * the sync() must be called after the draw() regardless of threading. + * + * @retval Result::Success when succeed, Result::InsufficientCondition otherwise. + * @see Canvas::draw() + */ + virtual Result sync() noexcept; + + _TVG_DECLARE_PRIVATE(Canvas); +}; + + +/** + * @class LinearGradient + * + * @brief A class representing the linear gradient fill of the Shape object. + * + * Besides the APIs inherited from the Fill class, it enables setting and getting the linear gradient bounds. + * The behavior outside the gradient bounds depends on the value specified in the spread API. + */ +class TVG_API LinearGradient final : public Fill +{ +public: + ~LinearGradient(); + + /** + * @brief Sets the linear gradient bounds. + * + * The bounds of the linear gradient are defined as a surface constrained by two parallel lines crossing + * the given points (@p x1, @p y1) and (@p x2, @p y2), respectively. Both lines are perpendicular to the line linking + * (@p x1, @p y1) and (@p x2, @p y2). + * + * @param[in] x1 The horizontal coordinate of the first point used to determine the gradient bounds. + * @param[in] y1 The vertical coordinate of the first point used to determine the gradient bounds. + * @param[in] x2 The horizontal coordinate of the second point used to determine the gradient bounds. + * @param[in] y2 The vertical coordinate of the second point used to determine the gradient bounds. + * + * @retval Result::Success when succeed. + * + * @note In case the first and the second points are equal, an object filled with such a gradient fill is not rendered. + */ + Result linear(float x1, float y1, float x2, float y2) noexcept; + + /** + * @brief Gets the linear gradient bounds. + * + * The bounds of the linear gradient are defined as a surface constrained by two parallel lines crossing + * the given points (@p x1, @p y1) and (@p x2, @p y2), respectively. Both lines are perpendicular to the line linking + * (@p x1, @p y1) and (@p x2, @p y2). + * + * @param[out] x1 The horizontal coordinate of the first point used to determine the gradient bounds. + * @param[out] y1 The vertical coordinate of the first point used to determine the gradient bounds. + * @param[out] x2 The horizontal coordinate of the second point used to determine the gradient bounds. + * @param[out] y2 The vertical coordinate of the second point used to determine the gradient bounds. + * + * @retval Result::Success when succeed. + */ + Result linear(float* x1, float* y1, float* x2, float* y2) const noexcept; + + /** + * @brief Creates a new LinearGradient object. + * + * @return A new LinearGradient object. + */ + static std::unique_ptr gen() noexcept; + + /** + * @brief Return the unique id value of this class. + * + * This method can be referred for identifying the LinearGradient class type. + * + * @return The type id of the LinearGradient class. + */ + static uint32_t identifier() noexcept; + + _TVG_DECLARE_PRIVATE(LinearGradient); +}; + + +/** + * @class RadialGradient + * + * @brief A class representing the radial gradient fill of the Shape object. + * + */ +class TVG_API RadialGradient final : public Fill +{ +public: + ~RadialGradient(); + + /** + * @brief Sets the radial gradient bounds. + * + * The radial gradient bounds are defined as a circle centered in a given point (@p cx, @p cy) of a given radius. + * + * @param[in] cx The horizontal coordinate of the center of the bounding circle. + * @param[in] cy The vertical coordinate of the center of the bounding circle. + * @param[in] radius The radius of the bounding circle. + * + * @retval Result::Success when succeed, Result::InvalidArguments in case the @p radius value is zero or less. + */ + Result radial(float cx, float cy, float radius) noexcept; + + /** + * @brief Gets the radial gradient bounds. + * + * The radial gradient bounds are defined as a circle centered in a given point (@p cx, @p cy) of a given radius. + * + * @param[out] cx The horizontal coordinate of the center of the bounding circle. + * @param[out] cy The vertical coordinate of the center of the bounding circle. + * @param[out] radius The radius of the bounding circle. + * + * @retval Result::Success when succeed. + */ + Result radial(float* cx, float* cy, float* radius) const noexcept; + + /** + * @brief Creates a new RadialGradient object. + * + * @return A new RadialGradient object. + */ + static std::unique_ptr gen() noexcept; + + /** + * @brief Return the unique id value of this class. + * + * This method can be referred for identifying the RadialGradient class type. + * + * @return The type id of the RadialGradient class. + */ + static uint32_t identifier() noexcept; + + _TVG_DECLARE_PRIVATE(RadialGradient); +}; + + +/** + * @class Shape + * + * @brief A class representing two-dimensional figures and their properties. + * + * A shape has three major properties: shape outline, stroking, filling. The outline in the Shape is retained as the path. + * Path can be composed by accumulating primitive commands such as moveTo(), lineTo(), cubicTo(), or complete shape interfaces such as appendRect(), appendCircle(), etc. + * Path can consists of sub-paths. One sub-path is determined by a close command. + * + * The stroke of Shape is an optional property in case the Shape needs to be represented with/without the outline borders. + * It's efficient since the shape path and the stroking path can be shared with each other. It's also convenient when controlling both in one context. + */ +class TVG_API Shape final : public Paint +{ +public: + ~Shape(); + + /** + * @brief Resets the properties of the shape path. + * + * The transformation matrix, the color, the fill and the stroke properties are retained. + * + * @retval Result::Success when succeed. + * + * @note The memory, where the path data is stored, is not deallocated at this stage for caching effect. + */ + Result reset() noexcept; + + /** + * @brief Sets the initial point of the sub-path. + * + * The value of the current point is set to the given point. + * + * @param[in] x The horizontal coordinate of the initial point of the sub-path. + * @param[in] y The vertical coordinate of the initial point of the sub-path. + * + * @retval Result::Success when succeed. + */ + Result moveTo(float x, float y) noexcept; + + /** + * @brief Adds a new point to the sub-path, which results in drawing a line from the current point to the given end-point. + * + * The value of the current point is set to the given end-point. + * + * @param[in] x The horizontal coordinate of the end-point of the line. + * @param[in] y The vertical coordinate of the end-point of the line. + * + * @retval Result::Success when succeed. + * + * @note In case this is the first command in the path, it corresponds to the moveTo() call. + */ + Result lineTo(float x, float y) noexcept; + + /** + * @brief Adds new points to the sub-path, which results in drawing a cubic Bezier curve starting + * at the current point and ending at the given end-point (@p x, @p y) using the control points (@p cx1, @p cy1) and (@p cx2, @p cy2). + * + * The value of the current point is set to the given end-point. + * + * @param[in] cx1 The horizontal coordinate of the 1st control point. + * @param[in] cy1 The vertical coordinate of the 1st control point. + * @param[in] cx2 The horizontal coordinate of the 2nd control point. + * @param[in] cy2 The vertical coordinate of the 2nd control point. + * @param[in] x The horizontal coordinate of the end-point of the curve. + * @param[in] y The vertical coordinate of the end-point of the curve. + * + * @retval Result::Success when succeed. + * + * @note In case this is the first command in the path, no data from the path are rendered. + */ + Result cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) noexcept; + + /** + * @brief Closes the current sub-path by drawing a line from the current point to the initial point of the sub-path. + * + * The value of the current point is set to the initial point of the closed sub-path. + * + * @retval Result::Success when succeed. + * + * @note In case the sub-path does not contain any points, this function has no effect. + */ + Result close() noexcept; + + /** + * @brief Appends a rectangle to the path. + * + * The rectangle with rounded corners can be achieved by setting non-zero values to @p rx and @p ry arguments. + * The @p rx and @p ry values specify the radii of the ellipse defining the rounding of the corners. + * + * The position of the rectangle is specified by the coordinates of its upper left corner - @p x and @p y arguments. + * + * The rectangle is treated as a new sub-path - it is not connected with the previous sub-path. + * + * The value of the current point is set to (@p x + @p rx, @p y) - in case @p rx is greater + * than @p w/2 the current point is set to (@p x + @p w/2, @p y) + * + * @param[in] x The horizontal coordinate of the upper left corner of the rectangle. + * @param[in] y The vertical coordinate of the upper left corner of the rectangle. + * @param[in] w The width of the rectangle. + * @param[in] h The height of the rectangle. + * @param[in] rx The x-axis radius of the ellipse defining the rounded corners of the rectangle. + * @param[in] ry The y-axis radius of the ellipse defining the rounded corners of the rectangle. + * + * @retval Result::Success when succeed. + * + * @note For @p rx and @p ry greater than or equal to the half of @p w and the half of @p h, respectively, the shape become an ellipse. + */ + Result appendRect(float x, float y, float w, float h, float rx = 0, float ry = 0) noexcept; + + /** + * @brief Appends an ellipse to the path. + * + * The position of the ellipse is specified by the coordinates of its center - @p cx and @p cy arguments. + * + * The ellipse is treated as a new sub-path - it is not connected with the previous sub-path. + * + * The value of the current point is set to (@p cx, @p cy - @p ry). + * + * @param[in] cx The horizontal coordinate of the center of the ellipse. + * @param[in] cy The vertical coordinate of the center of the ellipse. + * @param[in] rx The x-axis radius of the ellipse. + * @param[in] ry The y-axis radius of the ellipse. + * + * @retval Result::Success when succeed. + */ + Result appendCircle(float cx, float cy, float rx, float ry) noexcept; + + /** + * @brief Appends a circular arc to the path. + * + * The arc is treated as a new sub-path - it is not connected with the previous sub-path. + * The current point value is set to the end-point of the arc in case @p pie is @c false, and to the center of the arc otherwise. + * + * @param[in] cx The horizontal coordinate of the center of the arc. + * @param[in] cy The vertical coordinate of the center of the arc. + * @param[in] radius The radius of the arc. + * @param[in] startAngle The start angle of the arc given in degrees, measured counter-clockwise from the horizontal line. + * @param[in] sweep The central angle of the arc given in degrees, measured counter-clockwise from @p startAngle. + * @param[in] pie Specifies whether to draw radii from the arc's center to both of its end-point - drawn if @c true. + * + * @retval Result::Success when succeed. + * + * @note Setting @p sweep value greater than 360 degrees, is equivalent to calling appendCircle(cx, cy, radius, radius). + */ + Result appendArc(float cx, float cy, float radius, float startAngle, float sweep, bool pie) noexcept; + + /** + * @brief Appends a given sub-path to the path. + * + * The current point value is set to the last point from the sub-path. + * For each command from the @p cmds array, an appropriate number of points in @p pts array should be specified. + * If the number of points in the @p pts array is different than the number required by the @p cmds array, the shape with this sub-path will not be displayed on the screen. + * + * @param[in] cmds The array of the commands in the sub-path. + * @param[in] cmdCnt The number of the sub-path's commands. + * @param[in] pts The array of the two-dimensional points. + * @param[in] ptsCnt The number of the points in the @p pts array. + * + * @retval Result::Success when succeed, Result::InvalidArguments otherwise. + * + * @note The interface is designed for optimal path setting if the caller has a completed path commands already. + */ + Result appendPath(const PathCommand* cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt) noexcept; + + /** + * @brief Sets the stroke width for all of the figures from the path. + * + * @param[in] width The width of the stroke. The default value is 0. + * + * @retval Result::Success when succeed, Result::FailedAllocation otherwise. + */ + Result stroke(float width) noexcept; + + /** + * @brief Sets the color of the stroke for all of the figures from the path. + * + * @param[in] r The red color channel value in the range [0 ~ 255]. The default value is 0. + * @param[in] g The green color channel value in the range [0 ~ 255]. The default value is 0. + * @param[in] b The blue color channel value in the range [0 ~ 255]. The default value is 0. + * @param[in] a The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque. The default value is 0. + * + * @retval Result::Success when succeed, Result::FailedAllocation otherwise. + */ + Result stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) noexcept; + + /** + * @brief Sets the gradient fill of the stroke for all of the figures from the path. + * + * @param[in] f The gradient fill. + * + * @retval Result::Success When succeed. + * @retval Result::FailedAllocation An internal error with a memory allocation for an object to be filled. + * @retval Result::MemoryCorruption In case a @c nullptr is passed as the argument. + */ + Result stroke(std::unique_ptr f) noexcept; + + /** + * @brief Sets the dash pattern of the stroke. + * + * @param[in] dashPattern The array of consecutive pair values of the dash length and the gap length. + * @param[in] cnt The length of the @p dashPattern array. + * + * @retval Result::Success When succeed. + * @retval Result::FailedAllocation An internal error with a memory allocation for an object to be dashed. + * @retval Result::InvalidArguments In case @p dashPattern is @c nullptr and @p cnt > 0, @p cnt is zero, any of the dash pattern values is zero or less. + * + * @note To reset the stroke dash pattern, pass @c nullptr to @p dashPattern and zero to @p cnt. + * @warning @p cnt must be greater than 1 if the dash pattern is valid. + */ + Result stroke(const float* dashPattern, uint32_t cnt) noexcept; + + /** + * @brief Sets the cap style of the stroke in the open sub-paths. + * + * @param[in] cap The cap style value. The default value is @c StrokeCap::Square. + * + * @retval Result::Success when succeed, Result::FailedAllocation otherwise. + */ + Result stroke(StrokeCap cap) noexcept; + + /** + * @brief Sets the join style for stroked path segments. + * + * The join style is used for joining the two line segment while stroking the path. + * + * @param[in] join The join style value. The default value is @c StrokeJoin::Bevel. + * + * @retval Result::Success when succeed, Result::FailedAllocation otherwise. + */ + Result stroke(StrokeJoin join) noexcept; + + + /** + * @brief Sets the stroke miterlimit. + * + * @param[in] miterlimit The miterlimit imposes a limit on the extent of the stroke join, when the @c StrokeJoin::Miter join style is set. The default value is 4. + * + * @retval Result::Success when succeed, Result::NonSupport unsupported value, Result::FailedAllocation otherwise. + * + * @since 0.11 + */ + Result strokeMiterlimit(float miterlimit) noexcept; + + /** + * @brief Sets the solid color for all of the figures from the path. + * + * The parts of the shape defined as inner are colored. + * + * @param[in] r The red color channel value in the range [0 ~ 255]. The default value is 0. + * @param[in] g The green color channel value in the range [0 ~ 255]. The default value is 0. + * @param[in] b The blue color channel value in the range [0 ~ 255]. The default value is 0. + * @param[in] a The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque. The default value is 0. + * + * @retval Result::Success when succeed. + * + * @note Either a solid color or a gradient fill is applied, depending on what was set as last. + * @note ClipPath won't use the fill values. (see: enum class CompositeMethod::ClipPath) + */ + Result fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) noexcept; + + /** + * @brief Sets the gradient fill for all of the figures from the path. + * + * The parts of the shape defined as inner are filled. + * + * @param[in] f The unique pointer to the gradient fill. + * + * @retval Result::Success when succeed, Result::MemoryCorruption otherwise. + * + * @note Either a solid color or a gradient fill is applied, depending on what was set as last. + */ + Result fill(std::unique_ptr f) noexcept; + + /** + * @brief Sets the fill rule for the Shape object. + * + * @param[in] r The fill rule value. The default value is @c FillRule::Winding. + * + * @retval Result::Success when succeed. + */ + Result fill(FillRule r) noexcept; + + + /** + * @brief Sets the rendering order of the stroke and the fill. + * + * @param[in] strokeFirst If @c true the stroke is rendered before the fill, otherwise the stroke is rendered as the second one (the default option). + * + * @retval Result::Success when succeed, Result::FailedAllocation otherwise. + * + * @since 0.10 + */ + Result order(bool strokeFirst) noexcept; + + + /** + * @brief Gets the commands data of the path. + * + * @param[out] cmds The pointer to the array of the commands from the path. + * + * @return The length of the @p cmds array when succeed, zero otherwise. + */ + uint32_t pathCommands(const PathCommand** cmds) const noexcept; + + /** + * @brief Gets the points values of the path. + * + * @param[out] pts The pointer to the array of the two-dimensional points from the path. + * + * @return The length of the @p pts array when succeed, zero otherwise. + */ + uint32_t pathCoords(const Point** pts) const noexcept; + + /** + * @brief Gets the pointer to the gradient fill of the shape. + * + * @return The pointer to the gradient fill of the stroke when succeed, @c nullptr in case no fill was set. + */ + const Fill* fill() const noexcept; + + /** + * @brief Gets the solid color of the shape. + * + * @param[out] r The red color channel value in the range [0 ~ 255]. + * @param[out] g The green color channel value in the range [0 ~ 255]. + * @param[out] b The blue color channel value in the range [0 ~ 255]. + * @param[out] a The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque. + * + * @return Result::Success when succeed. + */ + Result fillColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a = nullptr) const noexcept; + + /** + * @brief Gets the fill rule value. + * + * @return The fill rule value of the shape. + */ + FillRule fillRule() const noexcept; + + /** + * @brief Gets the stroke width. + * + * @return The stroke width value when succeed, zero if no stroke was set. + */ + float strokeWidth() const noexcept; + + /** + * @brief Gets the color of the shape's stroke. + * + * @param[out] r The red color channel value in the range [0 ~ 255]. + * @param[out] g The green color channel value in the range [0 ~ 255]. + * @param[out] b The blue color channel value in the range [0 ~ 255]. + * @param[out] a The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque. + * + * @retval Result::Success when succeed, Result::InsufficientCondition otherwise. + */ + Result strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a = nullptr) const noexcept; + + /** + * @brief Gets the pointer to the gradient fill of the stroke. + * + * @return The pointer to the gradient fill of the stroke when succeed, @c nullptr otherwise. + */ + const Fill* strokeFill() const noexcept; + + /** + * @brief Gets the dash pattern of the stroke. + * + * @param[out] dashPattern The pointer to the memory, where the dash pattern array is stored. + * + * @return The length of the @p dashPattern array. + */ + uint32_t strokeDash(const float** dashPattern) const noexcept; + + /** + * @brief Gets the cap style used for stroking the path. + * + * @return The cap style value of the stroke. + */ + StrokeCap strokeCap() const noexcept; + + /** + * @brief Gets the join style value used for stroking the path. + * + * @return The join style value of the stroke. + */ + StrokeJoin strokeJoin() const noexcept; + + /** + * @brief Gets the stroke miterlimit. + * + * @return The stroke miterlimit value when succeed, 4 if no stroke was set. + * + * @since 0.11 + */ + float strokeMiterlimit() const noexcept; + + /** + * @brief Creates a new Shape object. + * + * @return A new Shape object. + */ + static std::unique_ptr gen() noexcept; + + /** + * @brief Return the unique id value of this class. + * + * This method can be referred for identifying the Shape class type. + * + * @return The type id of the Shape class. + */ + static uint32_t identifier() noexcept; + + _TVG_DECLARE_PRIVATE(Shape); +}; + + +/** + * @class Picture + * + * @brief A class representing an image read in one of the supported formats: raw, svg, png, jpg, lottie(json) and etc. + * Besides the methods inherited from the Paint, it provides methods to load & draw images on the canvas. + * + * @note Supported formats are depended on the available TVG loaders. + * @note See Animation class if the picture data is animatable. + */ +class TVG_API Picture final : public Paint +{ +public: + ~Picture(); + + /** + * @brief Loads a picture data directly from a file. + * + * ThorVG efficiently caches the loaded data using the specified @p path as a key. + * This means that loading the same file again will not result in duplicate operations; + * instead, ThorVG will reuse the previously loaded picture data. + * + * @param[in] path A path to the picture file. + * + * @retval Result::Success When succeed. + * @retval Result::InvalidArguments In case the @p path is invalid. + * @retval Result::NonSupport When trying to load a file with an unknown extension. + * @retval Result::Unknown If an error occurs at a later stage. + * + * @note The Load behavior can be asynchronous if the assigned thread number is greater than zero. + * @see Initializer::init() + */ + Result load(const std::string& path) noexcept; + + /** + * @brief Loads a picture data from a memory block of a given size. + * + * ThorVG efficiently caches the loaded data using the specified @p data address as a key + * when the @p copy has @c false. This means that loading the same data again will not result in duplicate operations + * for the sharable @p data. Instead, ThorVG will reuse the previously loaded picture data. + * + * @param[in] data A pointer to a memory location where the content of the picture file is stored. + * @param[in] size The size in bytes of the memory occupied by the @p data. + * @param[in] copy Decides whether the data should be copied into the engine local buffer. + * + * @retval Result::Success When succeed. + * @retval Result::InvalidArguments In case no data are provided or the @p size is zero or less. + * @retval Result::NonSupport When trying to load a file with an unknown extension. + * @retval Result::Unknown If an error occurs at a later stage. + * + * @warning: you have responsibility to release the @p data memory if the @p copy is true + * @deprecated Use load(const char* data, uint32_t size, const std::string& mimeType, bool copy) instead. + * @see Result load(const char* data, uint32_t size, const std::string& mimeType, bool copy = false) noexcept + */ + TVG_DEPRECATED Result load(const char* data, uint32_t size, bool copy = false) noexcept; + + /** + * @brief Loads a picture data from a memory block of a given size. + * + * @param[in] data A pointer to a memory location where the content of the picture file is stored. + * @param[in] size The size in bytes of the memory occupied by the @p data. + * @param[in] mimeType Mimetype or extension of data such as "jpg", "jpeg", "lottie", "svg", "svg+xml", "png", etc. In case an empty string or an unknown type is provided, the loaders will be tried one by one. + * @param[in] copy If @c true the data are copied into the engine local buffer, otherwise they are not. + * + * @retval Result::Success When succeed. + * @retval Result::InvalidArguments In case no data are provided or the @p size is zero or less. + * @retval Result::NonSupport When trying to load a file with an unknown extension. + * @retval Result::Unknown If an error occurs at a later stage. + * + * @warning: It's the user responsibility to release the @p data memory if the @p copy is @c true. + * + * @note If you are unsure about the MIME type, you can provide an empty value like @c "", and thorvg will attempt to figure it out. + * @since 0.5 + */ + Result load(const char* data, uint32_t size, const std::string& mimeType, bool copy = false) noexcept; + + /** + * @brief Resizes the picture content to the given width and height. + * + * The picture content is resized while keeping the default size aspect ratio. + * The scaling factor is established for each of dimensions and the smaller value is applied to both of them. + * + * @param[in] w A new width of the image in pixels. + * @param[in] h A new height of the image in pixels. + * + * @retval Result::Success when succeed, Result::InsufficientCondition otherwise. + */ + Result size(float w, float h) noexcept; + + /** + * @brief Gets the size of the image. + * + * @param[out] w The width of the image in pixels. + * @param[out] h The height of the image in pixels. + * + * @retval Result::Success when succeed. + */ + Result size(float* w, float* h) const noexcept; + + /** + * @brief Loads a raw data from a memory block with a given size. + * + * ThorVG efficiently caches the loaded data using the specified @p data address as a key + * when the @p copy has @c false. This means that loading the same data again will not result in duplicate operations + * for the sharable @p data. Instead, ThorVG will reuse the previously loaded picture data. + * + * @param[in] paint A Tvg_Paint pointer to the picture object. + * @param[in] data A pointer to a memory location where the content of the picture raw data is stored. + * @param[in] w The width of the image @p data in pixels. + * @param[in] h The height of the image @p data in pixels. + * @param[in] premultiplied If @c true, the given image data is alpha-premultiplied. + * @param[in] copy If @c true the data are copied into the engine local buffer, otherwise they are not. + * + * @retval Result::Success When succeed, Result::InsufficientCondition otherwise. + * @retval Result::FailedAllocation An internal error possibly with memory allocation. + * + * @since 0.9 + */ + Result load(uint32_t* data, uint32_t w, uint32_t h, bool copy) noexcept; + + /** + * @brief Sets or removes the triangle mesh to deform the image. + * + * If a mesh is provided, the transform property of the Picture will apply to the triangle mesh, and the + * image data will be used as the texture. + * + * If @p triangles is @c nullptr, or @p triangleCnt is 0, the mesh will be removed. + * + * Only raster image types are supported at this time (png, jpg). Vector types like svg and tvg do not support. + * mesh deformation. However, if required you should be able to render a vector image to a raster image and then apply a mesh. + * + * @param[in] triangles An array of Polygons(triangles) that make up the mesh, or null to remove the mesh. + * @param[in] triangleCnt The number of Polygons(triangles) provided, or 0 to remove the mesh. + * + * @retval Result::Success When succeed. + * @retval Result::Unknown If fails + * + * @note The Polygons are copied internally, so modifying them after calling Mesh::mesh has no affect. + * @warning Please do not use it, this API is not official one. It could be modified in the next version. + * + * @note Experimental API + */ + Result mesh(const Polygon* triangles, uint32_t triangleCnt) noexcept; + + /** + * @brief Return the number of triangles in the mesh, and optionally get a pointer to the array of triangles in the mesh. + * + * @param[out] triangles Optional. A pointer to the array of Polygons used by this mesh. + * + * @return The number of polygons in the array. + * + * @note Modifying the triangles returned by this method will modify them directly within the mesh. + * @warning Please do not use it, this API is not official one. It could be modified in the next version. + * + * @note Experimental API + */ + uint32_t mesh(const Polygon** triangles) const noexcept; + + /** + * @brief Creates a new Picture object. + * + * @return A new Picture object. + */ + static std::unique_ptr gen() noexcept; + + /** + * @brief Return the unique id value of this class. + * + * This method can be referred for identifying the Picture class type. + * + * @return The type id of the Picture class. + */ + static uint32_t identifier() noexcept; + + _TVG_DECLARE_ACCESSOR(Animation); + _TVG_DECLARE_PRIVATE(Picture); +}; + + +/** + * @class Scene + * + * @brief A class to composite children paints. + * + * As the traditional graphics rendering method, TVG also enables scene-graph mechanism. + * This feature supports an array function for managing the multiple paints as one group paint. + * + * As a group, the scene can be transformed, made translucent and composited with other target paints, + * its children will be affected by the scene world. + */ +class TVG_API Scene final : public Paint +{ +public: + ~Scene(); + + /** + * @brief Passes drawing elements to the Scene using Paint objects. + * + * Only the paints pushed into the scene will be the drawn targets. + * The paints are retained by the scene until Scene::clear() is called. + * + * @param[in] paint A Paint object to be drawn. + * + * @retval Result::Success when succeed, Result::MemoryCorruption otherwise. + * + * @note The rendering order of the paints is the same as the order as they were pushed. Consider sorting the paints before pushing them if you intend to use layering. + * @see Scene::paints() + * @see Scene::clear() + */ + Result push(std::unique_ptr paint) noexcept; + + /** + * @brief Sets the size of the container, where all the paints pushed into the Scene are stored. + * + * If the number of objects pushed into the scene is known in advance, calling the function + * prevents multiple memory reallocation, thus improving the performance. + * + * @param[in] size The number of objects for which the memory is to be reserved. + * + * @return Result::Success when succeed, Result::FailedAllocation otherwise. + */ + TVG_DEPRECATED Result reserve(uint32_t size) noexcept; + + /** + * @brief Returns the list of the paints that currently held by the Scene. + * + * This function provides the list of paint nodes, allowing users a direct opportunity to modify the scene tree. + * + * @warning Please avoid accessing the paints during Scene update/draw. You can access them after calling Canvas::sync(). + * @see Canvas::sync() + * @see Scene::push() + * @see Scene::clear() + * + * @note Experimental API + */ + std::list& paints() noexcept; + + /** + * @brief Sets the total number of the paints pushed into the scene to be zero. + * Depending on the value of the @p free argument, the paints are freed or not. + * + * @param[in] free If @c true, the memory occupied by paints is deallocated, otherwise it is not. + * + * @retval Result::Success when succeed + * + * @warning If you don't free the paints they become dangled. They are supposed to be reused, otherwise you are responsible for their lives. Thus please use the @p free argument only when you know how it works, otherwise it's not recommended. + * + * @since 0.2 + */ + Result clear(bool free = true) noexcept; + + /** + * @brief Creates a new Scene object. + * + * @return A new Scene object. + */ + static std::unique_ptr gen() noexcept; + + /** + * @brief Return the unique id value of this class. + * + * This method can be referred for identifying the Scene class type. + * + * @return The type id of the Scene class. + */ + static uint32_t identifier() noexcept; + + _TVG_DECLARE_PRIVATE(Scene); +}; + + +/** + * @class Text + * + * @brief A class to represent text objects in a graphical context, allowing for rendering and manipulation of unicode text. + * + * @note Experimental API + */ +class TVG_API Text final : public Paint +{ +public: + ~Text(); + + /** + * @brief Sets the font properties for the text. + * + * This function allows you to define the font characteristics used for text rendering. + * It sets the font name, size and optionally the style. + * + * @param[in] name The name of the font. This should correspond to a font available in the canvas. + * @param[in] size The size of the font in points. This determines how large the text will appear. + * @param[in] style The style of the font. It can be used to set the font to 'italic'. + * If not specified, the default style is used. Only 'italic' style is supported currently. + * + * @retval Result::Success when the font properties are set successfully. + * @retval Result::InsufficientCondition when the specified @p name cannot be found. + * + * @note Experimental API + */ + Result font(const char* name, float size, const char* style = nullptr) noexcept; + + /** + * @brief Assigns the given unicode text to be rendered. + * + * This function sets the unicode string that will be displayed by the rendering system. + * The text is set according to the specified UTF encoding method, which defaults to UTF-8. + * + * @param[in] text The multi-byte text encoded with utf8 string to be rendered. + * + * @retval Result::Success when succeed. + * + * @note Experimental API + */ + Result text(const char* text) noexcept; + + /** + * @brief Sets the text color. + * + * @param[in] r The red color channel value in the range [0 ~ 255]. The default value is 0. + * @param[in] g The green color channel value in the range [0 ~ 255]. The default value is 0. + * @param[in] b The blue color channel value in the range [0 ~ 255]. The default value is 0. + * + * @retval Result::Success when succeed. + * @retval Result::InsufficientCondition when the font has not been set up prior to this operation. + * + * @see Text::font() + * + * @note Experimental API + */ + Result fill(uint8_t r, uint8_t g, uint8_t b) noexcept; + + /** + * @brief Sets the gradient fill for all of the figures from the text. + * + * The parts of the text defined as inner are filled. + * + * @param[in] f The unique pointer to the gradient fill. + * + * @retval Result::Success when succeed, Result::MemoryCorruption otherwise. + * @retval Result::InsufficientCondition when the font has not been set up prior to this operation. + * + * @note Either a solid color or a gradient fill is applied, depending on what was set as last. + * @note Experimental API + * + * @see Text::font() + */ + Result fill(std::unique_ptr f) noexcept; + + /** + * @brief Loads a scalable font data(ttf) from a file. + * + * ThorVG efficiently caches the loaded data using the specified @p path as a key. + * This means that loading the same file again will not result in duplicate operations; + * instead, ThorVG will reuse the previously loaded font data. + * + * @param[in] path The path to the font file. + * + * @retval Result::Success When succeed. + * @retval Result::InvalidArguments In case the @p path is invalid. + * @retval Result::NonSupport When trying to load a file with an unknown extension. + * @retval Result::Unknown If an error occurs at a later stage. + * + * @note Experimental API + * + * @see Text::unload(const std::string& path) + */ + static Result load(const std::string& path) noexcept; + + /** + * @brief Unloads the specified scalable font data (TTF) that was previously loaded. + * + * This function is used to release resources associated with a font file that has been loaded into memory. + * + * @param[in] path The file path of the loaded font. + * + * @retval Result::Success Successfully unloads the font data. + * @retval Result::InsufficientCondition Fails if the loader is not initialized. + * + * @note If the font data is currently in use, it will not be immediately unloaded. + * @note Experimental API + * + * @see Text::load(const std::string& path) + */ + static Result unload(const std::string& path) noexcept; + + /** + * @brief Creates a new Text object. + * + * @return A new Text object. + * + * @note Experimental API + */ + static std::unique_ptr gen() noexcept; + + /** + * @brief Return the unique id value of this class. + * + * This method can be referred for identifying the Text class type. + * + * @return The type id of the Text class. + */ + static uint32_t identifier() noexcept; + + _TVG_DECLARE_PRIVATE(Text); +}; + + +/** + * @class SwCanvas + * + * @brief A class for the rendering graphical elements with a software raster engine. + */ +class TVG_API SwCanvas final : public Canvas +{ +public: + ~SwCanvas(); + + /** + * @brief Enumeration specifying the methods of combining the 8-bit color channels into 32-bit color. + */ + enum Colorspace + { + ABGR8888 = 0, ///< The channels are joined in the order: alpha, blue, green, red. Colors are alpha-premultiplied. (a << 24 | b << 16 | g << 8 | r) + ARGB8888, ///< The channels are joined in the order: alpha, red, green, blue. Colors are alpha-premultiplied. (a << 24 | r << 16 | g << 8 | b) + ABGR8888S, ///< The channels are joined in the order: alpha, blue, green, red. Colors are un-alpha-premultiplied. @since 0.12 + ARGB8888S, ///< The channels are joined in the order: alpha, red, green, blue. Colors are un-alpha-premultiplied. @since 0.12 + }; + + /** + * @brief Enumeration specifying the methods of Memory Pool behavior policy. + * @since 0.4 + */ + enum MempoolPolicy + { + Default = 0, ///< Default behavior that ThorVG is designed to. + Shareable, ///< Memory Pool is shared among the SwCanvases. + Individual ///< Allocate designated memory pool that is only used by current instance. + }; + + /** + * @brief Sets the drawing target for the rasterization. + * + * The buffer of a desirable size should be allocated and owned by the caller. + * + * @param[in] buffer A pointer to a memory block of the size @p stride x @p h, where the raster data are stored. + * @param[in] stride The stride of the raster image - greater than or equal to @p w. + * @param[in] w The width of the raster image. + * @param[in] h The height of the raster image. + * @param[in] cs The value specifying the way the 32-bits colors should be read/written. + * + * @retval Result::Success When succeed. + * @retval Result::MemoryCorruption When casting in the internal function implementation failed. + * @retval Result::InvalidArguments In case no valid pointer is provided or the width, or the height or the stride is zero. + * @retval Result::NonSupport In case the software engine is not supported. + * + * @warning Do not access @p buffer during Canvas::push() - Canvas::sync(). It should not be accessed while the engine is writing on it. + * + * @see Canvas::viewport() + */ + Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h, Colorspace cs) noexcept; + + /** + * @brief Set sw engine memory pool behavior policy. + * + * Basically ThorVG draws a lot of shapes, it allocates/deallocates a few chunk of memory + * while processing rendering. It internally uses one shared memory pool + * which can be reused among the canvases in order to avoid memory overhead. + * + * Thus ThorVG suggests using a memory pool policy to satisfy user demands, + * if it needs to guarantee the thread-safety of the internal data access. + * + * @param[in] policy The method specifying the Memory Pool behavior. The default value is @c MempoolPolicy::Default. + * + * @retval Result::Success When succeed. + * @retval Result::InsufficientCondition If the canvas contains some paints already. + * @retval Result::NonSupport In case the software engine is not supported. + * + * @note When @c policy is set as @c MempoolPolicy::Individual, the current instance of canvas uses its own individual + * memory data, which is not shared with others. This is necessary when the canvas is accessed on a worker-thread. + * + * @warning It's not allowed after pushing any paints. + * + * @since 0.4 + */ + Result mempool(MempoolPolicy policy) noexcept; + + /** + * @brief Creates a new SwCanvas object. + * @return A new SwCanvas object. + */ + static std::unique_ptr gen() noexcept; + + _TVG_DECLARE_PRIVATE(SwCanvas); +}; + + +/** + * @class GlCanvas + * + * @brief A class for the rendering graphic elements with a GL raster engine. + * + * @warning Please do not use it. This class is not fully supported yet. + * + * @note Experimental API + */ +class TVG_API GlCanvas final : public Canvas +{ +public: + ~GlCanvas(); + + /** + * @brief Sets the drawing target for rasterization. + * + * This function specifies the drawing target where the rasterization will occur. It can target + * a specific framebuffer object (FBO) or the main surface. + * + * @param[in] id The GL target ID, usually indicating the FBO ID. A value of @c 0 specifies the main surface. + * @param[in] w The width (in pixels) of the raster image. + * @param[in] h The height (in pixels) of the raster image. + * + * @warning This API is experimental and not officially supported. It may be modified or removed in future versions. + * @warning Drawing on the main surface is currently not permitted. If the identifier (@p id) is set to @c 0, the operation will be aborted. + * + * @see Canvas::viewport() + * + * @note Currently, this only allows the GL_RGBA8 color space format. + * @note Experimental API + */ + Result target(int32_t id, uint32_t w, uint32_t h) noexcept; + + /** + * @brief Creates a new GlCanvas object. + * + * @return A new GlCanvas object. + * + * @note Experimental API + */ + static std::unique_ptr gen() noexcept; + + _TVG_DECLARE_PRIVATE(GlCanvas); +}; + + +/** + * @class WgCanvas + * + * @brief A class for the rendering graphic elements with a WebGPU raster engine. + * + * @warning Please do not use it. This class is not fully supported yet. + * + * @note Experimental API + */ +class TVG_API WgCanvas final : public Canvas +{ +public: + ~WgCanvas(); + + /** + * @brief Sets the target window for the rasterization. + * + * @warning Please do not use it, this API is not official one. It could be modified in the next version. + * + * @note Experimental API + * @see Canvas::viewport() + */ + Result target(void* window, uint32_t w, uint32_t h) noexcept; + + /** + * @brief Creates a new WgCanvas object. + * + * @return A new WgCanvas object. + * + * @note Experimental API + */ + static std::unique_ptr gen() noexcept; + + _TVG_DECLARE_PRIVATE(WgCanvas); +}; + + +/** + * @class Initializer + * + * @brief A class that enables initialization and termination of the TVG engines. + */ +class TVG_API Initializer final +{ +public: + /** + * @brief Initializes TVG engines. + * + * TVG requires the running-engine environment. + * TVG runs its own task-scheduler for parallelizing rendering tasks efficiently. + * You can indicate the number of threads, the count of which is designated @p threads. + * In the initialization step, TVG will generate/spawn the threads as set by @p threads count. + * + * @param[in] engine The engine types to initialize. This is relative to the Canvas types, in which it will be used. For multiple backends bitwise operation is allowed. + * @param[in] threads The number of additional threads. Zero indicates only the main thread is to be used. + * + * @retval Result::Success When succeed. + * @retval Result::FailedAllocation An internal error possibly with memory allocation. + * @retval Result::InvalidArguments If unknown engine type chosen. + * @retval Result::NonSupport In case the engine type is not supported on the system. + * @retval Result::Unknown Others. + * + * @note The Initializer keeps track of the number of times it was called. Threads count is fixed at the first init() call. + * @see Initializer::term() + */ + static Result init(CanvasEngine engine, uint32_t threads) noexcept; + + /** + * @brief Terminates TVG engines. + * + * @param[in] engine The engine types to terminate. This is relative to the Canvas types, in which it will be used. For multiple backends bitwise operation is allowed + * + * @retval Result::Success When succeed. + * @retval Result::InsufficientCondition In case there is nothing to be terminated. + * @retval Result::InvalidArguments If unknown engine type chosen. + * @retval Result::NonSupport In case the engine type is not supported on the system. + * @retval Result::Unknown Others. + * + * @note Initializer does own reference counting for multiple calls. + * @see Initializer::init() + */ + static Result term(CanvasEngine engine) noexcept; + + _TVG_DISABLE_CTOR(Initializer); +}; + + +/** + * @class Animation + * + * @brief The Animation class enables manipulation of animatable images. + * + * This class supports the display and control of animation frames. + * + * @since 0.13 + */ + +class TVG_API Animation +{ +public: + ~Animation(); + + /** + * @brief Specifies the current frame in the animation. + * + * @param[in] no The index of the animation frame to be displayed. The index should be less than the totalFrame(). + * + * @retval Result::Success Successfully set the frame. + * @retval Result::InsufficientCondition if the given @p no is the same as the current frame value. + * @retval Result::NonSupport The current Picture data does not support animations. + * + * @note For efficiency, ThorVG ignores updates to the new frame value if the difference from the current frame value + * is less than 0.001. In such cases, it returns @c Result::InsufficientCondition. + * Values less than 0.001 may be disregarded and may not be accurately retained by the Animation. + * + * @see totalFrame() + * + */ + Result frame(float no) noexcept; + + /** + * @brief Retrieves a picture instance associated with this animation instance. + * + * This function provides access to the picture instance that can be used to load animation formats, such as Lottie(json). + * After setting up the picture, it can be pushed to the designated canvas, enabling control over animation frames + * with this Animation instance. + * + * @return A picture instance that is tied to this animation. + * + * @warning The picture instance is owned by Animation. It should not be deleted manually. + * + */ + Picture* picture() const noexcept; + + /** + * @brief Retrieves the current frame number of the animation. + * + * @return The current frame number of the animation, between 0 and totalFrame() - 1. + * + * @note If the Picture is not properly configured, this function will return 0. + * + * @see Animation::frame(float no) + * @see Animation::totalFrame() + * + */ + float curFrame() const noexcept; + + /** + * @brief Retrieves the total number of frames in the animation. + * + * @return The total number of frames in the animation. + * + * @note Frame numbering starts from 0. + * @note If the Picture is not properly configured, this function will return 0. + * + */ + float totalFrame() const noexcept; + + /** + * @brief Retrieves the duration of the animation in seconds. + * + * @return The duration of the animation in seconds. + * + * @note If the Picture is not properly configured, this function will return 0. + * + */ + float duration() const noexcept; + + /** + * @brief Specifies the playback segment of the animation. + * + * The set segment is designated as the play area of the animation. + * This is useful for playing a specific segment within the entire animation. + * After setting, the number of animation frames and the playback time are calculated + * by mapping the playback segment as the entire range. + * + * @param[in] begin segment start. + * @param[in] end segment end. + * + * @retval Result::Success When succeed. + * @retval Result::InsufficientCondition In case the animation is not loaded. + * @retval Result::InvalidArguments When the given parameter is invalid. + * @retval Result::NonSupport When it's not animatable. + * + * @note Range from 0.0~1.0 + * @note If a marker has been specified, its range will be disregarded. + * @see LottieAnimation::segment(const char* marker) + * @note Experimental API + */ + Result segment(float begin, float end) noexcept; + + /** + * @brief Gets the current segment. + * + * @param[out] begin segment start. + * @param[out] end segment end. + * + * @retval Result::Success When succeed. + * @retval Result::InsufficientCondition In case the animation is not loaded. + * @retval Result::InvalidArguments When the given parameter is invalid. + * @retval Result::NonSupport When it's not animatable. + * + * @note Experimental API + */ + Result segment(float* begin, float* end = nullptr) noexcept; + + /** + * @brief Creates a new Animation object. + * + * @return A new Animation object. + * + */ + static std::unique_ptr gen() noexcept; + + _TVG_DECLARE_PRIVATE(Animation); +}; + + +/** + * @class Saver + * + * @brief A class for exporting a paint object into a specified file, from which to recover the paint data later. + * + * ThorVG provides a feature for exporting & importing paint data. The Saver role is to export the paint data to a file. + * It's useful when you need to save the composed scene or image from a paint object and recreate it later. + * + * The file format is decided by the extension name(i.e. "*.tvg") while the supported formats depend on the TVG packaging environment. + * If it doesn't support the file format, the save() method returns the @c Result::NonSupport result. + * + * Once you export a paint to the file successfully, you can recreate it using the Picture class. + * + * @see Picture::load() + * + * @since 0.5 + */ +class TVG_API Saver final +{ +public: + ~Saver(); + + /** + * @brief Sets the base background content for the saved image. + * + * @param[in] paint The paint to be drawn as the background image for the saving paint. + * + * @note Experimental API + */ + Result background(std::unique_ptr paint) noexcept; + + /** + * @brief Exports the given @p paint data to the given @p path + * + * If the saver module supports any compression mechanism, it will optimize the data size. + * This might affect the encoding/decoding time in some cases. You can turn off the compression + * if you wish to optimize for speed. + * + * @param[in] paint The paint to be saved with all its associated properties. + * @param[in] path A path to the file, in which the paint data is to be saved. + * @param[in] compress If @c true then compress data if possible. + * + * @retval Result::Success When succeed. + * @retval Result::InsufficientCondition If currently saving other resources. + * @retval Result::NonSupport When trying to save a file with an unknown extension or in an unsupported format. + * @retval Result::MemoryCorruption An internal error. + * @retval Result::Unknown In case an empty paint is to be saved. + * + * @note Saving can be asynchronous if the assigned thread number is greater than zero. To guarantee the saving is done, call sync() afterwards. + * @see Saver::sync() + * + * @since 0.5 + */ + Result save(std::unique_ptr paint, const std::string& path, bool compress = true) noexcept; + + /** + * @brief Export the provided animation data to the specified file path. + * + * This function exports the given animation data to the provided file path. You can also specify the desired frame rate in frames per second (FPS) by providing the fps parameter. + * + * @param[in] animation The animation to be saved, including all associated properties. + * @param[in] path The path to the file where the animation will be saved. + * @param[in] quality The encoded quality level. @c 0 is the minimum, @c 100 is the maximum value(recommended). + * @param[in] fps The desired frames per second (FPS). For example, to encode data at 60 FPS, pass 60. Pass 0 to keep the original frame data. + * + * @retval Result::Success if the export succeeds. + * @retval Result::InsufficientCondition if there are ongoing resource-saving operations. + * @retval Result::NonSupport if an attempt is made to save the file with an unknown extension or in an unsupported format. + * @retval Result::MemoryCorruption in case of an internal error. + * @retval Result::Unknown if attempting to save an empty paint. + * + * @note A higher frames per second (FPS) would result in a larger file size. It is recommended to use the default value. + * @note Saving can be asynchronous if the assigned thread number is greater than zero. To guarantee the saving is done, call sync() afterwards. + * + * @see Saver::sync() + * + * @note Experimental API + */ + Result save(std::unique_ptr animation, const std::string& path, uint32_t quality = 100, uint32_t fps = 0) noexcept; + + /** + * @brief Guarantees that the saving task is finished. + * + * The behavior of the Saver works on a sync/async basis, depending on the threading setting of the Initializer. + * Thus, if you wish to have a benefit of it, you must call sync() after the save() in the proper delayed time. + * Otherwise, you can call sync() immediately. + * + * @retval Result::Success when succeed. + * @retval Result::InsufficientCondition otherwise. + * + * @note The asynchronous tasking is dependent on the Saver module implementation. + * @see Saver::save() + * + * @since 0.5 + */ + Result sync() noexcept; + + /** + * @brief Creates a new Saver object. + * + * @return A new Saver object. + * + * @since 0.5 + */ + static std::unique_ptr gen() noexcept; + + _TVG_DECLARE_PRIVATE(Saver); +}; + + +/** + * @class Accessor + * + * @brief The Accessor is a utility class to debug the Scene structure by traversing the scene-tree. + * + * The Accessor helps you search specific nodes to read the property information, figure out the structure of the scene tree and its size. + * + * @warning We strongly warn you not to change the paints of a scene unless you really know the design-structure. + * + * @since 0.10 + */ +class TVG_API Accessor final +{ +public: + ~Accessor(); + + /** + * @brief Set the access function for traversing the Picture scene tree nodes. + * + * @param[in] picture The picture node to traverse the internal scene-tree. + * @param[in] func The callback function calling for every paint nodes of the Picture. + * + * @return Return the given @p picture instance. + * + * @note The bitmap based picture might not have the scene-tree. + */ + std::unique_ptr set(std::unique_ptr picture, std::function func) noexcept; + + /** + * @brief Creates a new Accessor object. + * + * @return A new Accessor object. + */ + static std::unique_ptr gen() noexcept; + + _TVG_DECLARE_PRIVATE(Accessor); +}; + + +/** + * @brief The cast() function is a utility function used to cast a 'Paint' to type 'T'. + * @since 0.11 + */ +template +std::unique_ptr cast(Paint* paint) +{ + return std::unique_ptr(static_cast(paint)); +} + + +/** + * @brief The cast() function is a utility function used to cast a 'Fill' to type 'T'. + * @since 0.11 + */ +template +std::unique_ptr cast(Fill* fill) +{ + return std::unique_ptr(static_cast(fill)); +} + + +/** @}*/ + +} //namespace + +#endif //_THORVG_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/thorvg_capi.h b/include/liblvgl/libs/thorvg/thorvg_capi.h new file mode 100644 index 00000000..98b709ea --- /dev/null +++ b/include/liblvgl/libs/thorvg/thorvg_capi.h @@ -0,0 +1,2508 @@ +/*! +* \file thorvg_capi.h +* +* \brief The module provides C bindings for the ThorVG library. +* Please refer to src/examples/Capi.cpp to find the thorvg_capi usage examples. +* +* The thorvg_capi module allows to implement the ThorVG client and provides +* the following functionalities: +* - drawing shapes: line, arc, curve, polygon, circle, user-defined, ... +* - filling: solid, linear and radial gradient +* - scene graph & affine transformation (translation, rotation, scale, ...) +* - stroking: width, join, cap, dash +* - composition: blending, masking, path clipping +* - pictures: SVG, PNG, JPG, bitmap +* +*/ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL +#define TVG_BUILD 1 + +#define TVG_BUILD 1 + +#ifndef __THORVG_CAPI_H__ +#define __THORVG_CAPI_H__ + +#include +#include + +#ifdef TVG_API + #undef TVG_API +#endif + +#ifndef TVG_STATIC + #ifdef _WIN32 + #if TVG_BUILD + #define TVG_API __declspec(dllexport) + #else + #define TVG_API __declspec(dllimport) + #endif + #elif (defined(__SUNPRO_C) || defined(__SUNPRO_CC)) + #define TVG_API __global + #else + #if (defined(__GNUC__) && __GNUC__ >= 4) || defined(__INTEL_COMPILER) + #define TVG_API __attribute__ ((visibility("default"))) + #else + #define TVG_API + #endif + #endif +#else + #define TVG_API +#endif + +#ifdef TVG_DEPRECATED + #undef TVG_DEPRECATED +#endif + +#ifdef _WIN32 + #define TVG_DEPRECATED __declspec(deprecated) +#elif __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) + #define TVG_DEPRECATED __attribute__ ((__deprecated__)) +#else + #define TVG_DEPRECATED +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/** +* \defgroup ThorVG_CAPI ThorVG_CAPI +* \brief ThorVG C language binding APIs. +* +* \{ +*/ + + +/** +* \brief A structure responsible for managing and drawing graphical elements. +* +* It sets up the target buffer, which can be drawn on the screen. It stores the Tvg_Paint objects (Shape, Scene, Picture). +*/ +typedef struct _Tvg_Canvas Tvg_Canvas; + + +/** +* \brief A structure representing a graphical element. +* +* \warning The TvgPaint objects cannot be shared between Canvases. +*/ +typedef struct _Tvg_Paint Tvg_Paint; + + +/** +* \brief A structure representing a gradient fill of a Tvg_Paint object. +*/ +typedef struct _Tvg_Gradient Tvg_Gradient; + + +/** +* \brief A structure representing an object that enables to save a Tvg_Paint object into a file. +*/ +typedef struct _Tvg_Saver Tvg_Saver; + +/** +* \brief A structure representing an animation controller object. +*/ +typedef struct _Tvg_Animation Tvg_Animation; + + +/** +* \brief Enumeration specifying the engine type used for the graphics backend. For multiple backends bitwise operation is allowed. +* +* \ingroup ThorVGCapi_Initializer +*/ +typedef enum { + TVG_ENGINE_SW = (1 << 1), ///< CPU rasterizer. + TVG_ENGINE_GL = (1 << 2) ///< OpenGL rasterizer. +} Tvg_Engine; + + +/** + * \brief Enumeration specifying the result from the APIs. + */ +typedef enum { + TVG_RESULT_SUCCESS = 0, ///< The value returned in case of a correct request execution. + TVG_RESULT_INVALID_ARGUMENT, ///< The value returned in the event of a problem with the arguments given to the API - e.g. empty paths or null pointers. + TVG_RESULT_INSUFFICIENT_CONDITION, ///< The value returned in case the request cannot be processed - e.g. asking for properties of an object, which does not exist. + TVG_RESULT_FAILED_ALLOCATION, ///< The value returned in case of unsuccessful memory allocation. + TVG_RESULT_MEMORY_CORRUPTION, ///< The value returned in the event of bad memory handling - e.g. failing in pointer releasing or casting + TVG_RESULT_NOT_SUPPORTED, ///< The value returned in case of choosing unsupported options. + TVG_RESULT_UNKNOWN ///< The value returned in all other cases. +} Tvg_Result; + + +/** + * \brief Enumeration indicating the method used in the composition of two objects - the target and the source. + * + * \ingroup ThorVGCapi_Paint + */ +typedef enum { + TVG_COMPOSITE_METHOD_NONE = 0, ///< No composition is applied. + TVG_COMPOSITE_METHOD_CLIP_PATH, ///< The intersection of the source and the target is determined and only the resulting pixels from the source are rendered. + TVG_COMPOSITE_METHOD_ALPHA_MASK, ///< The pixels of the source and the target are alpha blended. As a result, only the part of the source, which intersects with the target is visible. + TVG_COMPOSITE_METHOD_INVERSE_ALPHA_MASK, ///< The pixels of the source and the complement to the target's pixels are alpha blended. As a result, only the part of the source which is not covered by the target is visible. + TVG_COMPOSITE_METHOD_LUMA_MASK, ///< The source pixels are converted to grayscale (luma value) and alpha blended with the target. As a result, only the part of the source which intersects with the target is visible. \since 0.9 + TVG_COMPOSITE_METHOD_INVERSE_LUMA_MASK ///< The source pixels are converted to grayscale (luma value) and complement to the target's pixels are alpha blended. As a result, only the part of the source which is not covered by the target is visible. \Experimental API +} Tvg_Composite_Method; + +/** + * @brief Enumeration indicates the method used for blending paint. Please refer to the respective formulas for each method. + * + * \ingroup ThorVGCapi_Paint + * + * @note Experimental API + */ +typedef enum { + TVG_BLEND_METHOD_NORMAL = 0, ///< Perform the alpha blending(default). S if (Sa == 255), otherwise (Sa * S) + (255 - Sa) * D + TVG_BLEND_METHOD_ADD, ///< Simply adds pixel values of one layer with the other. (S + D) + TVG_BLEND_METHOD_SCREEN, ///< The values of the pixels in the two layers are inverted, multiplied, and then inverted again. (S + D) - (S * D) + TVG_BLEND_METHOD_MULTIPLY, ///< Takes the RGB channel values from 0 to 255 of each pixel in the top layer and multiples them with the values for the corresponding pixel from the bottom layer. (S * D) + TVG_BLEND_METHOD_OVERLAY, ///< Combines Multiply and Screen blend modes. (2 * S * D) if (2 * D < Da), otherwise (Sa * Da) - 2 * (Da - S) * (Sa - D) + TVG_BLEND_METHOD_DIFFERENCE, ///< Subtracts the bottom layer from the top layer or the other way around, to always get a non-negative value. (S - D) if (S > D), otherwise (D - S) + TVG_BLEND_METHOD_EXCLUSION, ///< The result is twice the product of the top and bottom layers, subtracted from their sum. s + d - (2 * s * d) + TVG_BLEND_METHOD_SRCOVER, ///< Replace the bottom layer with the top layer. + TVG_BLEND_METHOD_DARKEN, ///< Creates a pixel that retains the smallest components of the top and bottom layer pixels. min(S, D) + TVG_BLEND_METHOD_LIGHTEN, ///< Only has the opposite action of Darken Only. max(S, D) + TVG_BLEND_METHOD_COLORDODGE, ///< Divides the bottom layer by the inverted top layer. D / (255 - S) + TVG_BLEND_METHOD_COLORBURN, ///< Divides the inverted bottom layer by the top layer, and then inverts the result. 255 - (255 - D) / S + TVG_BLEND_METHOD_HARDLIGHT, ///< The same as Overlay but with the color roles reversed. (2 * S * D) if (S < Sa), otherwise (Sa * Da) - 2 * (Da - S) * (Sa - D) + TVG_BLEND_METHOD_SOFTLIGHT ///< The same as Overlay but with applying pure black or white does not result in pure black or white. (1 - 2 * S) * (D ^ 2) + (2 * S * D) +} Tvg_Blend_Method; + + +/** + * \brief Enumeration indicating the ThorVG class type. + * + * \ingroup ThorVGCapi_Paint + * + * \since 0.9 + */ +typedef enum { + TVG_IDENTIFIER_UNDEF = 0, ///< Undefined type. + TVG_IDENTIFIER_SHAPE, ///< A shape type paint. + TVG_IDENTIFIER_SCENE, ///< A scene type paint. + TVG_IDENTIFIER_PICTURE, ///< A picture type paint. + TVG_IDENTIFIER_LINEAR_GRAD, ///< A linear gradient type. + TVG_IDENTIFIER_RADIAL_GRAD ///< A radial gradient type. +} Tvg_Identifier; + + +/** + * \addtogroup ThorVGCapi_Shape + * \{ + */ + +/** + * \brief Enumeration specifying the values of the path commands accepted by TVG. + * + * Not to be confused with the path commands from the svg path element (like M, L, Q, H and many others). + * TVG interprets all of them and translates to the ones from the PathCommand values. + */ +typedef enum { + TVG_PATH_COMMAND_CLOSE = 0, ///< Ends the current sub-path and connects it with its initial point - corresponds to Z command in the svg path commands. + TVG_PATH_COMMAND_MOVE_TO, ///< Sets a new initial point of the sub-path and a new current point - corresponds to M command in the svg path commands. + TVG_PATH_COMMAND_LINE_TO, ///< Draws a line from the current point to the given point and sets a new value of the current point - corresponds to L command in the svg path commands. + TVG_PATH_COMMAND_CUBIC_TO ///< Draws a cubic Bezier curve from the current point to the given point using two given control points and sets a new value of the current point - corresponds to C command in the svg path commands. +} Tvg_Path_Command; + + +/** + * \brief Enumeration determining the ending type of a stroke in the open sub-paths. + */ +typedef enum { + TVG_STROKE_CAP_SQUARE = 0, ///< The stroke is extended in both endpoints of a sub-path by a rectangle, with the width equal to the stroke width and the length equal to the half of the stroke width. For zero length sub-paths the square is rendered with the size of the stroke width. + TVG_STROKE_CAP_ROUND, ///< The stroke is extended in both endpoints of a sub-path by a half circle, with a radius equal to the half of a stroke width. For zero length sub-paths a full circle is rendered. + TVG_STROKE_CAP_BUTT ///< The stroke ends exactly at each of the two endpoints of a sub-path. For zero length sub-paths no stroke is rendered. +} Tvg_Stroke_Cap; + + +/** + * \brief Enumeration specifying how to fill the area outside the gradient bounds. + */ +typedef enum { + TVG_STROKE_JOIN_BEVEL = 0, ///< The outer corner of the joined path segments is bevelled at the join point. The triangular region of the corner is enclosed by a straight line between the outer corners of each stroke. + TVG_STROKE_JOIN_ROUND, ///< The outer corner of the joined path segments is rounded. The circular region is centered at the join point. + TVG_STROKE_JOIN_MITER ///< The outer corner of the joined path segments is spiked. The spike is created by extension beyond the join point of the outer edges of the stroke until they intersect. In case the extension goes beyond the limit, the join style is converted to the Bevel style. +} Tvg_Stroke_Join; + + +/** + * \brief Enumeration specifying how to fill the area outside the gradient bounds. + */ +typedef enum { + TVG_STROKE_FILL_PAD = 0, ///< The remaining area is filled with the closest stop color. + TVG_STROKE_FILL_REFLECT, ///< The gradient pattern is reflected outside the gradient area until the expected region is filled. + TVG_STROKE_FILL_REPEAT ///< The gradient pattern is repeated continuously beyond the gradient area until the expected region is filled. +} Tvg_Stroke_Fill; + + +/** + * \brief Enumeration specifying the algorithm used to establish which parts of the shape are treated as the inside of the shape. + */ +typedef enum { + TVG_FILL_RULE_WINDING = 0, ///< A line from the point to a location outside the shape is drawn. The intersections of the line with the path segment of the shape are counted. Starting from zero, if the path segment of the shape crosses the line clockwise, one is added, otherwise one is subtracted. If the resulting sum is non zero, the point is inside the shape. + TVG_FILL_RULE_EVEN_ODD ///< A line from the point to a location outside the shape is drawn and its intersections with the path segments of the shape are counted. If the number of intersections is an odd number, the point is inside the shape. +} Tvg_Fill_Rule; + +/** \} */ // end addtogroup ThorVGCapi_Shape + + +/*! +* \addtogroup ThorVGCapi_Gradient +* \{ +*/ + +/*! +* \brief A data structure storing the information about the color and its relative position inside the gradient bounds. +*/ +typedef struct +{ + float offset; /**< The relative position of the color. */ + uint8_t r; /**< The red color channel value in the range [0 ~ 255]. */ + uint8_t g; /**< The green color channel value in the range [0 ~ 255]. */ + uint8_t b; /**< The blue color channel value in the range [0 ~ 255]. */ + uint8_t a; /**< The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque. */ +} Tvg_Color_Stop; + +/** \} */ // end addtogroup ThorVGCapi_Gradient + + +/** + * \brief A data structure representing a point in two-dimensional space. + */ +typedef struct +{ + float x, y; +} Tvg_Point; + + +/** + * \brief A data structure representing a three-dimensional matrix. + * + * The elements e11, e12, e21 and e22 represent the rotation matrix, including the scaling factor. + * The elements e13 and e23 determine the translation of the object along the x and y-axis, respectively. + * The elements e31 and e32 are set to 0, e33 is set to 1. + */ +typedef struct +{ + float e11, e12, e13; + float e21, e22, e23; + float e31, e32, e33; +} Tvg_Matrix; + + +/** +* \defgroup ThorVGCapi_Initializer Initializer +* \brief A module enabling initialization and termination of the TVG engines. +* +* \{ +*/ + +/************************************************************************/ +/* Engine API */ +/************************************************************************/ +/*! +* \brief Initializes TVG engines. +* +* TVG requires the running-engine environment. +* TVG runs its own task-scheduler for parallelizing rendering tasks efficiently. +* You can indicate the number of threads, the count of which is designated @p threads. +* In the initialization step, TVG will generate/spawn the threads as set by @p threads count. +* +* \code +* tvg_engine_init(TVG_ENGINE_SW, 0); //Initialize software renderer and use the main thread only +* \endcode +* +* \param[in] engine_method The engine types to initialize. This is relative to the Canvas types, in which it will be used. For multiple backends bitwise operation is allowed. +* - TVG_ENGINE_SW: CPU rasterizer +* - TVG_ENGINE_GL: OpenGL rasterizer (not supported yet) +* \param[in] threads The number of additional threads used to perform rendering. Zero indicates only the main thread is to be used. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_FAILED_ALLOCATION An internal error possibly with memory allocation. +* \retval TVG_RESULT_INVALID_ARGUMENT Unknown engine type. +* \retval TVG_RESULT_NOT_SUPPORTED Unsupported engine type. +* \retval TVG_RESULT_UNKNOWN Other error. +* +* \note The Initializer keeps track of the number of times it was called. Threads count is fixed at the first init() call. +* \see tvg_engine_term() +* \see Tvg_Engine +*/ +TVG_API Tvg_Result tvg_engine_init(Tvg_Engine engine_method, unsigned threads); + + +/*! +* \brief Terminates TVG engines. +* +* It should be called in case of termination of the TVG client with the same engine types as were passed when tvg_engine_init() was called. +* +* \code +* tvg_engine_init(TVG_ENGINE_SW, 0); +* //define canvas and shapes, update shapes, general rendering calls +* tvg_engine_term(TVG_ENGINE_SW); +* \endcode +* +* \param engine_method The engine types to terminate. This is relative to the Canvas types, in which it will be used. For multiple backends bitwise operation is allowed +* - TVG_ENGINE_SW: CPU rasterizer +* - TVG_ENGINE_GL: OpenGL rasterizer (not supported yet) +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INSUFFICIENT_CONDITION Nothing to be terminated. +* \retval TVG_RESULT_INVALID_ARGUMENT Unknown engine type. +* \retval TVG_RESULT_NOT_SUPPORTED Unsupported engine type. +* \retval TVG_RESULT_UNKNOWN An internal error. +* +* \see tvg_engine_init() +* \see Tvg_Engine +*/ +TVG_API Tvg_Result tvg_engine_term(Tvg_Engine engine_method); + + +/** \} */ // end defgroup ThorVGCapi_Initializer + + +/** +* \defgroup ThorVGCapi_Canvas Canvas +* \brief A module for managing and drawing graphical elements. +* +* A canvas is an entity responsible for drawing the target. It sets up the drawing engine and the buffer, which can be drawn on the screen. It also manages given Paint objects. +* +* \note A Canvas behavior depends on the raster engine though the final content of the buffer is expected to be identical. +* \warning The Paint objects belonging to one Canvas can't be shared among multiple Canvases. +\{ +*/ + + +/** +* \defgroup ThorVGCapi_SwCanvas SwCanvas +* \ingroup ThorVGCapi_Canvas +* +* \brief A module for rendering the graphical elements using the software engine. +* +* \{ +*/ + +/************************************************************************/ +/* SwCanvas API */ +/************************************************************************/ + +/** + * \brief Enumeration specifying the methods of Memory Pool behavior policy. + */ +typedef enum { + TVG_MEMPOOL_POLICY_DEFAULT = 0, ///< Default behavior that ThorVG is designed to. + TVG_MEMPOOL_POLICY_SHAREABLE, ///< Memory Pool is shared among canvases. + TVG_MEMPOOL_POLICY_INDIVIDUAL ///< Allocate designated memory pool that is used only by the current canvas instance. +} Tvg_Mempool_Policy; + + +/** + * \brief Enumeration specifying the methods of combining the 8-bit color channels into 32-bit color. + */ +typedef enum { + TVG_COLORSPACE_ABGR8888 = 0, ///< The channels are joined in the order: alpha, blue, green, red. Colors are alpha-premultiplied. (a << 24 | b << 16 | g << 8 | r) + TVG_COLORSPACE_ARGB8888, ///< The channels are joined in the order: alpha, red, green, blue. Colors are alpha-premultiplied. (a << 24 | r << 16 | g << 8 | b) + TVG_COLORSPACE_ABGR8888S, ///< The channels are joined in the order: alpha, blue, green, red. Colors are un-alpha-premultiplied. @since 0.13 + TVG_COLORSPACE_ARGB8888S ///< The channels are joined in the order: alpha, red, green, blue. Colors are un-alpha-premultiplied. @since 0.13 +} Tvg_Colorspace; + + +/*! +* \brief Creates a Canvas object. +* +* \code +* Tvg_Canvas *canvas = NULL; +* +* tvg_engine_init(TVG_ENGINE_SW, 4); +* canvas = tvg_swcanvas_create(); +* +* //set up the canvas buffer +* uint32_t *buffer = NULL; +* buffer = (uint32_t*) malloc(sizeof(uint32_t) * 100 * 100); +* if (!buffer) return; +* +* tvg_swcanvas_set_target(canvas, buffer, 100, 100, 100, TVG_COLORSPACE_ARGB8888); +* +* //set up paints and add them into the canvas before drawing it +* +* tvg_canvas_destroy(canvas); +* tvg_engine_term(TVG_ENGINE_SW); +* \endcode +* +* \return A new Tvg_Canvas object. +*/ +TVG_API Tvg_Canvas* tvg_swcanvas_create(void); + + +/*! +* \brief Sets the buffer used in the rasterization process and defines the used colorspace. +* +* For optimisation reasons TVG does not allocate memory for the output buffer on its own. +* The buffer of a desirable size should be allocated and owned by the caller. +* +* \param[in] canvas The Tvg_Canvas object managing the @p buffer. +* \param[in] buffer A pointer to the allocated memory block of the size @p stride x @p h. +* \param[in] stride The stride of the raster image - in most cases same value as @p w. +* \param[in] w The width of the raster image. +* \param[in] h The height of the raster image. +* \param[in] cs The colorspace value defining the way the 32-bits colors should be read/written. +* - TVG_COLORSPACE_ABGR8888 +* - TVG_COLORSPACE_ARGB8888 +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_MEMORY_CORRUPTION Casting in the internal function implementation failed. +* \retval TVG_RESULT_INVALID_ARGUMENTS An invalid canvas or buffer pointer passed or one of the @p stride, @p w or @p h being zero. +* \retval TVG_RESULT_NOT_SUPPORTED The software engine is not supported. +* +* \warning Do not access @p buffer during tvg_canvas_draw() - tvg_canvas_sync(). It should not be accessed while the engine is writing on it. +* +* \see Tvg_Colorspace +*/ +TVG_API Tvg_Result tvg_swcanvas_set_target(Tvg_Canvas* canvas, uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h, Tvg_Colorspace cs); + + +/*! +* \brief Sets the software engine memory pool behavior policy. +* +* ThorVG draws a lot of shapes, it allocates/deallocates a few chunk of memory +* while processing rendering. It internally uses one shared memory pool +* which can be reused among the canvases in order to avoid memory overhead. +* +* Thus ThorVG suggests using a memory pool policy to satisfy user demands, +* if it needs to guarantee the thread-safety of the internal data access. +* +* \param[in] canvas The Tvg_Canvas object of which the Memory Pool behavior is to be specified. +* \param[in] policy The method specifying the Memory Pool behavior. The default value is @c TVG_MEMPOOL_POLICY_DEFAULT. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENTS An invalid canvas pointer passed. +* \retval TVG_RESULT_INSUFFICIENT_CONDITION The canvas contains some paints already. +* \retval TVG_RESULT_NOT_SUPPORTED The software engine is not supported. +* +* \note When @c policy is set as @c TVG_MEMPOOL_POLICY_INDIVIDUAL, the current instance of canvas uses its own individual +* memory data, which is not shared with others. This is necessary when the canvas is accessed on a worker-thread. +* +* \warning It's not allowed after pushing any paints. +*/ +TVG_API Tvg_Result tvg_swcanvas_set_mempool(Tvg_Canvas* canvas, Tvg_Mempool_Policy policy); + +/** \} */ // end defgroup ThorVGCapi_SwCanvas + + +/************************************************************************/ +/* Common Canvas API */ +/************************************************************************/ +/*! +* \brief Clears the canvas internal data, releases all paints stored by the canvas and destroys the canvas object itself. +* +* \code +* static Tvg_Canvas *canvas = NULL; +* static uint32_t *buffer = NULL; +* +* static void _init() { +* canvas = tvg_swcanvas_create(); +* buffer = (uint32_t*) malloc(sizeof(uint32_t) * 100 * 100); +* tvg_swcanvas_set_target(canvas, buffer, 100, 100, 100, TVG_COLORSPACE_ARGB8888); +* } +* +* //a task called from main function in a loop +* static void _job(const int cmd) { +* //define a valid rectangle shape +* switch (cmd) { +* case CMD_EXIT: return 0; +* case CMD_ADD_RECT: +* tvg_canvas_push(canvas, rect); +* break; +* case CMD_DEL_RECT: +* tvg_paint_del(rect); +* //now to safely delete Tvg_Canvas, tvg_canvas_clear() API have to be used +* break; +* default: +* break; +* } +* } +* +* int main(int argc, char **argv) { +* int cmd = 0; +* int stop = 1; +* +* tvg_engine_init(TVG_ENGINE_SW, 4); +* +* while (stop) { +* //wait for a command e.g. from a console +* stop = _job(cmd); +* } +* tvg_canvas_clear(canvas, false); +* tvg_canvas_destroy(canvas); +* tvg_engine_term(TVG_ENGINE_SW); +* return 0; +* } +* +* tvg_canvas_destroy(canvas); +* tvg_engine_term(TVG_ENGINE_SW) +* \endcode +* +* \param[in] canvas The Tvg_Canvas object to be destroyed. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid pointer to the Tvg_Canvas object is passed. +* +* \note If the paints from the canvas should not be released, the tvg_canvas_clear() with a @c free argument value set to @c false should be called. +* Please be aware that in such a case TVG is not responsible for the paints release anymore and it has to be done manually in order to avoid memory leaks. +* +* \see tvg_paint_del(), tvg_canvas_clear() +*/ +TVG_API Tvg_Result tvg_canvas_destroy(Tvg_Canvas* canvas); + + +/*! +* \brief Inserts a drawing element into the canvas using a Tvg_Paint object. +* +* \param[in] canvas The Tvg_Canvas object managing the @p paint. +* \param[in] paint The Tvg_Paint object to be drawn. +* +* Only the paints pushed into the canvas will be drawing targets. +* They are retained by the canvas until you call tvg_canvas_clear(). +* If you know the number of the pushed objects in advance, please call tvg_canvas_reserve(). +* +* \return Tvg_Result return values: +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT In case a @c nullptr is passed as the argument. +* \retval TVG_RESULT_INSUFFICIENT_CONDITION An internal error. +* +* \note The rendering order of the paints is the same as the order as they were pushed. Consider sorting the paints before pushing them if you intend to use layering. +* \see tvg_canvas_clear() +*/ +TVG_API Tvg_Result tvg_canvas_push(Tvg_Canvas* canvas, Tvg_Paint* paint); + + +/*! +* \brief Reserves a memory block where the objects pushed into a canvas are stored. +* +* If the number of Tvg_Paints to be stored in a canvas is known in advance, calling this function reduces the multiple +* memory allocations thus improves the performance. +* +* \code +* Tvg_Canvas *canvas = NULL; +* +* tvg_engine_init(TVG_ENGINE_SW, 4); +* canvas = tvg_swcanvas_create(); +* +* uint32_t *buffer = NULL; +* buffer = (uint32_t*) malloc(sizeof(uint32_t) * 100 * 100); +* if (!buffer) return; +* +* tvg_swcanvas_set_target(canvas, buffer, 100, 100, 100, TVG_COLORSPACE_ARGB8888); +* +* tvg_canvas_destroy(canvas); +* tvg_engine_term(TVG_ENGINE_SW) +* \endcode +* +* \param[in] canvas The Tvg_Canvas object managing the reserved memory. +* \param[in] n The number of objects for which the memory is to be reserved. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Canvas pointer. +* \retval TVG_RESULT_FAILED_ALLOCATION An internal error with memory allocation. +*/ +TVG_DEPRECATED TVG_API Tvg_Result tvg_canvas_reserve(Tvg_Canvas* canvas, uint32_t n); + + +/*! +* \brief Sets the total number of the paints pushed into the canvas to be zero. +* Tvg_Paint objects stored in the canvas are released if @p free is set to @c true, otherwise the memory is not deallocated and +* all paints should be released manually in order to avoid memory leaks. +* +* \param[in] canvas The Tvg_Canvas object to be cleared. +* \param[in] free If @c true the memory occupied by paints is deallocated, otherwise it is not. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Canvas pointer. +* \retval TVG_RESULT_INSUFFICIENT_CONDITION An internal error. +* +* \warning Please use the @p free argument only when you know how it works, otherwise it's not recommended. +* +* \see tvg_canvas_destroy() +*/ +TVG_API Tvg_Result tvg_canvas_clear(Tvg_Canvas* canvas, bool free); + + +/*! +* \brief Updates all paints in a canvas. +* +* Should be called before drawing in order to prepare paints for the rendering. +* +* \code +* //A frame drawing example. Thread safety and events implementation is skipped to show only TVG code. +* +* static Tvg_Canvas *canvas = NULL; +* static Tvg_Paint *rect = NULL; +* +* int _frame_render(void) { +* tvg_canvas_update(canvas); +* tvg_canvas_draw(canvas); +* tvg_canvas_sync(canvas); +* } +* +* //event handler from your code or third party library +* void _event_handler(event *event_data) { +* if (!event_data) return NULL; +* switch(event_data.type) { +* case EVENT_RECT_ADD: +* if (!rect) { +* tvg_shape_append_rect(rect, 10, 10, 50, 50, 0, 0); +* tvg_shape_set_stroke_width(rect, 1.0f); +* tvg_shape_set_stroke_color(rect, 255, 0, 0, 255); +* tvg_canvas_push(canvas, rect); +* } +* break; +* case EVENT_RECT_MOVE: +* if (rect) tvg_paint_translate(rect, 10.0, 10.0); +* break; +* default: +* break; +* } +* } +* +* int main(int argc, char **argv) { +* //example handler from your code or third party lib +* event_handler_add(handler, _event_handler); +* +* //create frame rendering process which calls _frame_render() function. +* app_loop_begin(_frame_render); +* app_loop_finish(); +* cleanup(); +* } +* \endcode +* +* \param[in] canvas The Tvg_Canvas object to be updated. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Canvas pointer. +* \retval TVG_RESULT_INSUFFICIENT_CONDITION An internal error. +* +* \see tvg_canvas_update_paint() +*/ +TVG_API Tvg_Result tvg_canvas_update(Tvg_Canvas* canvas); + + +/*! +* \brief Updates the given Tvg_Paint object from the canvas before the rendering. +* +* If a client application using the TVG library does not update the entire canvas with tvg_canvas_update() in the frame +* rendering process, Tvg_Paint objects previously added to the canvas should be updated manually with this function. +* +* \param[in] canvas The Tvg_Canvas object to which the @p paint belongs. +* \param[in] paint The Tvg_Paint object to be updated. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT In case a @c nullptr is passed as the argument. +* +* \see tvg_canvas_update() +*/ +TVG_API Tvg_Result tvg_canvas_update_paint(Tvg_Canvas* canvas, Tvg_Paint* paint); + + +/*! +* \brief Requests the canvas to draw the Tvg_Paint objects. +* +* All paints from the given canvas will be rasterized to the buffer. +* +* \param[in] canvas The Tvg_Canvas object containing elements to be drawn. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Canvas pointer. +* \retval TVG_RESULT_INSUFFICIENT_CONDITION An internal error. +* +* \note Drawing can be asynchronous based on the assigned thread number. To guarantee the drawing is done, call tvg_canvas_sync() afterwards. +* \see tvg_canvas_sync() +*/ +TVG_API Tvg_Result tvg_canvas_draw(Tvg_Canvas* canvas); + + +/*! +* \brief Guarantees that the drawing process is finished. +* +* Since the canvas rendering can be performed asynchronously, it should be called after the tvg_canvas_draw(). +* +* \param[in] canvas The Tvg_Canvas object containing elements which were drawn. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Canvas pointer. +* \retval TVG_RESULT_INSUFFICIENT_CONDITION An internal error. +* +* \see tvg_canvas_draw() +*/ +TVG_API Tvg_Result tvg_canvas_sync(Tvg_Canvas* canvas); + + +/*! +* \brief Sets the drawing region in the canvas. +* +* This function defines the rectangular area of the canvas that will be used for drawing operations. +* The specified viewport is used to clip the rendering output to the boundaries of the rectangle. +* +* \param[in] canvas The Tvg_Canvas object containing elements which were drawn. +* \param[in] x The x-coordinate of the upper-left corner of the rectangle. +* \param[in] y The y-coordinate of the upper-left corner of the rectangle. +* \param[in] w The width of the rectangle. +* \param[in] h The height of the rectangle. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INSUFFICIENT_CONDITION An internal error. +* +* \warning It's not allowed to change the viewport during tvg_canvas_update() - tvg_canvas_sync() or tvg_canvas_push() - tvg_canvas_sync(). +* +* \note When resetting the target, the viewport will also be reset to the target size. +* \note Experimental API +* \see tvg_swcanvas_set_target() +*/ +TVG_API Tvg_Result tvg_canvas_set_viewport(Tvg_Canvas* canvas, int32_t x, int32_t y, int32_t w, int32_t h); + +/** \} */ // end defgroup ThorVGCapi_Canvas + + +/** +* \defgroup ThorVGCapi_Paint Paint +* \brief A module for managing graphical elements. It enables duplication, transformation and composition. +* +* \{ +*/ + +/************************************************************************/ +/* Paint API */ +/************************************************************************/ +/*! +* \brief Releases the given Tvg_Paint object. +* +* \code +* //example of cleanup function +* Tvg_Paint *rect = NULL; //rectangle shape added in other function +* +* //rectangle delete API +* int rectangle_delete(void) { +* if (rect) tvg_paint_del(rect); +* rect = NULL; +* } +* +* int cleanup(void) { +* tvg_canvas_clear(canvas, false); +* tvg_canvas_destroy(canvas); +* canvas = NULL; +* } +* \endcode +* +* \param[in] paint The Tvg_Paint object to be released. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +* +* \warning If this function is used, tvg_canvas_clear() with the @c free argument value set to @c false should be used in order to avoid unexpected behaviours. +* +* \see tvg_canvas_clear(), tvg_canvas_destroy() +*/ +TVG_API Tvg_Result tvg_paint_del(Tvg_Paint* paint); + + +/*! +* \brief Scales the given Tvg_Paint object by the given factor. +* +* \param[in] paint The Tvg_Paint object to be scaled. +* \param[in] factor The value of the scaling factor. The default value is 1. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +* \retval TVG_RESULT_FAILED_ALLOCATION An internal error with memory allocation. +*/ +TVG_API Tvg_Result tvg_paint_scale(Tvg_Paint* paint, float factor); + + +/*! +* \brief Rotates the given Tvg_Paint by the given angle. +* +* The angle in measured clockwise from the horizontal axis. +* The rotational axis passes through the point on the object with zero coordinates. +* +* \param[in] paint The Tvg_Paint object to be rotated. +* \param[in] degree The value of the rotation angle in degrees. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +* \retval TVG_RESULT_FAILED_ALLOCATION An internal error with memory allocation. +*/ +TVG_API Tvg_Result tvg_paint_rotate(Tvg_Paint* paint, float degree); + + +/*! +* \brief Moves the given Tvg_Paint in a two-dimensional space. +* +* The origin of the coordinate system is in the upper left corner of the canvas. +* The horizontal and vertical axes point to the right and down, respectively. +* +* \param[in] paint The Tvg_Paint object to be shifted. +* \param[in] x The value of the horizontal shift. +* \param[in] y The value of the vertical shift. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +* \retval TVG_RESULT_FAILED_ALLOCATION An internal error with memory allocation. +*/ +TVG_API Tvg_Result tvg_paint_translate(Tvg_Paint* paint, float x, float y); + + +/*! +* \brief Transforms the given Tvg_Paint using the augmented transformation matrix. +* +* The augmented matrix of the transformation is expected to be given. +* +* \param[in] paint The Tvg_Paint object to be transformed. +* \param[in] m The 3x3 augmented matrix. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT A @c nullptr is passed as the argument. +* \retval TVG_RESULT_FAILED_ALLOCATION An internal error with memory allocation. +*/ +TVG_API Tvg_Result tvg_paint_set_transform(Tvg_Paint* paint, const Tvg_Matrix* m); + + +/*! +* \brief Gets the matrix of the affine transformation of the given Tvg_Paint object. +* +* In case no transformation was applied, the identity matrix is returned. +* +* \param[in] paint The Tvg_Paint object of which to get the transformation matrix. +* \param[out] m The 3x3 augmented matrix. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT A @c nullptr is passed as the argument. +*/ +TVG_API Tvg_Result tvg_paint_get_transform(Tvg_Paint* paint, Tvg_Matrix* m); + + +/*! +* \brief Sets the opacity of the given Tvg_Paint. +* +* \param[in] paint The Tvg_Paint object of which the opacity value is to be set. +* \param[in] opacity The opacity value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +* +* \note Setting the opacity with this API may require multiple renderings using a composition. It is recommended to avoid changing the opacity if possible. +*/ +TVG_API Tvg_Result tvg_paint_set_opacity(Tvg_Paint* paint, uint8_t opacity); + + +/*! +* \brief Gets the opacity of the given Tvg_Paint. +* +* \param[in] paint The Tvg_Paint object of which to get the opacity value. +* \param[out] opacity The opacity value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT In case a @c nullptr is passed as the argument. +*/ +TVG_API Tvg_Result tvg_paint_get_opacity(const Tvg_Paint* paint, uint8_t* opacity); + + +/*! +* \brief Duplicates the given Tvg_Paint object. +* +* Creates a new object and sets its all properties as in the original object. +* +* \param[in] paint The Tvg_Paint object to be copied. +* +* \return A copied Tvg_Paint object if succeed, @c nullptr otherwise. +*/ +TVG_API Tvg_Paint* tvg_paint_duplicate(Tvg_Paint* paint); + + +/*! +* \brief Gets the axis-aligned bounding box of the Tvg_Paint object. +* +* \param[in] paint The Tvg_Paint object of which to get the bounds. +* \param[out] x The x coordinate of the upper left corner of the object. +* \param[out] y The y coordinate of the upper left corner of the object. +* \param[out] w The width of the object. +* \param[out] h The height of the object. +* \param[in] transformed If @c true, the transformation of the paint is taken into account, otherwise it isn't. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +* \retval TVG_RESULT_INSUFFICIENT_CONDITION Other errors. +* +* \note The bounding box doesn't indicate the actual drawing region. It's the smallest rectangle that encloses the object. +*/ +TVG_API Tvg_Result tvg_paint_get_bounds(const Tvg_Paint* paint, float* x, float* y, float* w, float* h, bool transformed); + + +/*! +* \brief Sets the composition target object and the composition method. +* +* \param[in] paint The source object of the composition. +* \param[in] target The target object of the composition. +* \param[in] method The method used to composite the source object with the target. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid @p paint or @p target object or the @p method equal to TVG_COMPOSITE_METHOD_NONE. +*/ +TVG_API Tvg_Result tvg_paint_set_composite_method(Tvg_Paint* paint, Tvg_Paint* target, Tvg_Composite_Method method); + + +/** +* \brief Gets the composition target object and the composition method. +* +* \param[in] paint The source object of the composition. +* \param[out] target The target object of the composition. +* \param[out] method The method used to composite the source object with the target. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT A @c nullptr is passed as the argument. +*/ +TVG_API Tvg_Result tvg_paint_get_composite_method(const Tvg_Paint* paint, const Tvg_Paint** target, Tvg_Composite_Method* method); + + +/** +* \brief Gets the unique id value of the paint instance indicating the instance type. +* +* \param[in] paint The Tvg_Paint object of which to get the identifier value. +* \param[out] identifier The unique identifier of the paint instance type. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT In case a @c nullptr is passed as the argument. +* +* \since 0.9 +*/ +TVG_API Tvg_Result tvg_paint_get_identifier(const Tvg_Paint* paint, Tvg_Identifier* identifier); + + +/** + * @brief Sets the blending method for the paint object. + * + * The blending feature allows you to combine colors to create visually appealing effects, including transparency, lighting, shading, and color mixing, among others. + * its process involves the combination of colors or images from the source paint object with the destination (the lower layer image) using blending operations. + * The blending operation is determined by the chosen @p BlendMethod, which specifies how the colors or images are combined. + * + * \param[in] paint The Tvg_Paint object of which to get the identifier value. + * \param[in] method The blending method to be set. + * + * \return Tvg_Result enumeration. + * \retval TVG_RESULT_INVALID_ARGUMENT In case a @c nullptr is passed as the argument. + * + * @note Experimental API + */ +TVG_API Tvg_Result tvg_paint_set_blend_method(const Tvg_Paint* paint, Tvg_Blend_Method method); + + +/** + * @brief Gets the blending method for the paint object. + * + * The blending feature allows you to combine colors to create visually appealing effects, including transparency, lighting, shading, and color mixing, among others. + * its process involves the combination of colors or images from the source paint object with the destination (the lower layer image) using blending operations. + * The blending operation is determined by the chosen @p BlendMethod, which specifies how the colors or images are combined. + * + * \param[in] paint The Tvg_Paint object of which to get the identifier value. + * \param[out] method The blending method of the paint. + * + * \return Tvg_Result enumeration. + * \retval TVG_RESULT_INVALID_ARGUMENT In case a @c nullptr is passed as the argument. + * + * @note Experimental API + */ +TVG_API Tvg_Result tvg_paint_get_blend_method(const Tvg_Paint* paint, Tvg_Blend_Method* method); + + +/** \} */ // end defgroup ThorVGCapi_Paint + +/** +* \defgroup ThorVGCapi_Shape Shape +* +* \brief A module for managing two-dimensional figures and their properties. +* +* A shape has three major properties: shape outline, stroking, filling. The outline in the shape is retained as the path. +* Path can be composed by accumulating primitive commands such as tvg_shape_move_to(), tvg_shape_line_to(), tvg_shape_cubic_to() or complete shape interfaces such as tvg_shape_append_rect(), tvg_shape_append_circle(), etc. +* Path can consists of sub-paths. One sub-path is determined by a close command. +* +* The stroke of a shape is an optional property in case the shape needs to be represented with/without the outline borders. +* It's efficient since the shape path and the stroking path can be shared with each other. It's also convenient when controlling both in one context. +* +* \{ +*/ + +/************************************************************************/ +/* Shape API */ +/************************************************************************/ +/*! +* \brief Creates a new shape object. +* +* \return A new shape object. +*/ +TVG_API Tvg_Paint* tvg_shape_new(void); + + +/*! +* \brief Resets the shape path properties. +* +* The color, the fill and the stroke properties are retained. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +* +* \note The memory, where the path data is stored, is not deallocated at this stage for caching effect. +*/ +TVG_API Tvg_Result tvg_shape_reset(Tvg_Paint* paint); + + +/*! +* \brief Sets the initial point of the sub-path. +* +* The value of the current point is set to the given point. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[in] x The horizontal coordinate of the initial point of the sub-path. +* \param[in] y The vertical coordinate of the initial point of the sub-path. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +*/ +TVG_API Tvg_Result tvg_shape_move_to(Tvg_Paint* paint, float x, float y); + + +/*! +* \brief Adds a new point to the sub-path, which results in drawing a line from the current point to the given end-point. +* +* The value of the current point is set to the given end-point. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[in] x The horizontal coordinate of the end-point of the line. +* \param[in] y The vertical coordinate of the end-point of the line. + +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +* +* \note In case this is the first command in the path, it corresponds to the tvg_shape_move_to() call. +*/ +TVG_API Tvg_Result tvg_shape_line_to(Tvg_Paint* paint, float x, float y); + + +/*! +* \brief Adds new points to the sub-path, which results in drawing a cubic Bezier curve. +* +* The Bezier curve starts at the current point and ends at the given end-point (@p x, @p y). Two control points (@p cx1, @p cy1) and (@p cx2, @p cy2) are used to determine the shape of the curve. +* The value of the current point is set to the given end-point. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[in] cx1 The horizontal coordinate of the 1st control point. +* \param[in] cy1 The vertical coordinate of the 1st control point. +* \param[in] cx2 The horizontal coordinate of the 2nd control point. +* \param[in] cy2 The vertical coordinate of the 2nd control point. +* \param[in] x The horizontal coordinate of the endpoint of the curve. +* \param[in] y The vertical coordinate of the endpoint of the curve. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +* +* \note In case this is the first command in the path, no data from the path are rendered. +*/ +TVG_API Tvg_Result tvg_shape_cubic_to(Tvg_Paint* paint, float cx1, float cy1, float cx2, float cy2, float x, float y); + + +/*! +* \brief Closes the current sub-path by drawing a line from the current point to the initial point of the sub-path. +* +* The value of the current point is set to the initial point of the closed sub-path. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +* +* \note In case the sub-path does not contain any points, this function has no effect. +*/ +TVG_API Tvg_Result tvg_shape_close(Tvg_Paint* paint); + + +/*! +* \brief Appends a rectangle to the path. +* +* The rectangle with rounded corners can be achieved by setting non-zero values to @p rx and @p ry arguments. +* The @p rx and @p ry values specify the radii of the ellipse defining the rounding of the corners. +* +* The position of the rectangle is specified by the coordinates of its upper left corner - @p x and @p y arguments. +* +* The rectangle is treated as a new sub-path - it is not connected with the previous sub-path. +* +* The value of the current point is set to (@p x + @p rx, @p y) - in case @p rx is greater +* than @p w/2 the current point is set to (@p x + @p w/2, @p y) +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[in] x The horizontal coordinate of the upper left corner of the rectangle. +* \param[in] y The vertical coordinate of the upper left corner of the rectangle. +* \param[in] w The width of the rectangle. +* \param[in] h The height of the rectangle. +* \param[in] rx The x-axis radius of the ellipse defining the rounded corners of the rectangle. +* \param[in] ry The y-axis radius of the ellipse defining the rounded corners of the rectangle. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +* +& \note For @p rx and @p ry greater than or equal to the half of @p w and the half of @p h, respectively, the shape become an ellipse. +*/ +TVG_API Tvg_Result tvg_shape_append_rect(Tvg_Paint* paint, float x, float y, float w, float h, float rx, float ry); + + +/*! +* \brief Appends an ellipse to the path. +* +* The position of the ellipse is specified by the coordinates of its center - @p cx and @p cy arguments. +* +* The ellipse is treated as a new sub-path - it is not connected with the previous sub-path. +* +* The value of the current point is set to (@p cx, @p cy - @p ry). +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[in] cx The horizontal coordinate of the center of the ellipse. +* \param[in] cy The vertical coordinate of the center of the ellipse. +* \param[in] rx The x-axis radius of the ellipse. +* \param[in] ry The y-axis radius of the ellipse. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +*/ +TVG_API Tvg_Result tvg_shape_append_circle(Tvg_Paint* paint, float cx, float cy, float rx, float ry); + + +/*! +* \brief Appends a circular arc to the path. +* +* The arc is treated as a new sub-path - it is not connected with the previous sub-path. +* The current point value is set to the end-point of the arc in case @p pie is @c false, and to the center of the arc otherwise. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[in] cx The horizontal coordinate of the center of the arc. +* \param[in] cy The vertical coordinate of the center of the arc. +* \param[in] radius The radius of the arc. +* \param[in] startAngle The start angle of the arc given in degrees, measured counter-clockwise from the horizontal line. +* \param[in] sweep The central angle of the arc given in degrees, measured counter-clockwise from @p startAngle. +* \param[in] pie Specifies whether to draw radii from the arc's center to both of its end-point - drawn if @c true. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +* +* \note Setting @p sweep value greater than 360 degrees, is equivalent to calling tvg_shape_append_circle(paint, cx, cy, radius, radius). +*/ +TVG_API Tvg_Result tvg_shape_append_arc(Tvg_Paint* paint, float cx, float cy, float radius, float startAngle, float sweep, uint8_t pie); + + +/*! +* \brief Appends a given sub-path to the path. +* +* The current point value is set to the last point from the sub-path. +* For each command from the @p cmds array, an appropriate number of points in @p pts array should be specified. +* If the number of points in the @p pts array is different than the number required by the @p cmds array, the shape with this sub-path will not be displayed on the screen. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[in] cmds The array of the commands in the sub-path. +* \param[in] cmdCnt The length of the @p cmds array. +* \param[in] pts The array of the two-dimensional points. +* \param[in] ptsCnt The length of the @p pts array. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT A @c nullptr passed as the argument or @p cmdCnt or @p ptsCnt equal to zero. +*/ +TVG_API Tvg_Result tvg_shape_append_path(Tvg_Paint* paint, const Tvg_Path_Command* cmds, uint32_t cmdCnt, const Tvg_Point* pts, uint32_t ptsCnt); + + +/*! +* \brief Gets the points values of the path. +* +* The function does not allocate any data, it operates on internal memory. There is no need to free the @p pts array. +* +* \code +* Tvg_Shape *shape = tvg_shape_new(); +* Tvg_Point *coords = NULL; +* uint32_t len = 0; +* +* tvg_shape_append_circle(shape, 10, 10, 50, 50); +* tvg_shape_get_path_coords(shape, (const Tvg_Point**)&coords, &len); +* //TVG approximates a circle by four Bezier curves. In the example above the coords array stores their coordinates. +* \endcode +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[out] pts The pointer to the array of the two-dimensional points from the path. +* \param[out] cnt The length of the @p pts array. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT A @c nullptr passed as the argument. +*/ +TVG_API Tvg_Result tvg_shape_get_path_coords(const Tvg_Paint* paint, const Tvg_Point** pts, uint32_t* cnt); + + +/*! +* \brief Gets the commands data of the path. +* +* The function does not allocate any data. There is no need to free the @p cmds array. +* +* \code +* Tvg_Shape *shape = tvg_shape_new(); +* Tvg_Path_Command *cmds = NULL; +* uint32_t len = 0; +* +* tvg_shape_append_circle(shape, 10, 10, 50, 50); +* tvg_shape_get_path_commands(shape, (const Tvg_Path_Command**)&cmds, &len); +* //TVG approximates a circle by four Bezier curves. In the example above the cmds array stores the commands of the path data. +* \endcode +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[out] cmds The pointer to the array of the commands from the path. +* \param[out] cnt The length of the @p cmds array. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT A @c nullptr passed as the argument. +*/ +TVG_API Tvg_Result tvg_shape_get_path_commands(const Tvg_Paint* paint, const Tvg_Path_Command** cmds, uint32_t* cnt); + + +/*! +* \brief Sets the stroke width for all of the figures from the @p paint. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[in] width The width of the stroke. The default value is 0. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +* \retval TVG_RESULT_FAILED_ALLOCATION An internal error with a memory allocation. +*/ +TVG_API Tvg_Result tvg_shape_set_stroke_width(Tvg_Paint* paint, float width); + + +/*! +* \brief Gets the shape's stroke width. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[out] width The stroke width. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid pointer passed as an argument. +*/ +TVG_API Tvg_Result tvg_shape_get_stroke_width(const Tvg_Paint* paint, float* width); + + +/*! +* \brief Sets the shape's stroke color. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[in] r The red color channel value in the range [0 ~ 255]. The default value is 0. +* \param[in] g The green color channel value in the range [0 ~ 255]. The default value is 0. +* \param[in] b The blue color channel value in the range [0 ~ 255]. The default value is 0. +* \param[in] a The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +* \retval TVG_RESULT_FAILED_ALLOCATION An internal error with a memory allocation. +* +* \note Either a solid color or a gradient fill is applied, depending on what was set as last. +*/ +TVG_API Tvg_Result tvg_shape_set_stroke_color(Tvg_Paint* paint, uint8_t r, uint8_t g, uint8_t b, uint8_t a); + + +/*! +* \brief Gets the shape's stroke color. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[out] r The red color channel value in the range [0 ~ 255]. The default value is 0. +* \param[out] g The green color channel value in the range [0 ~ 255]. The default value is 0. +* \param[out] b The blue color channel value in the range [0 ~ 255]. The default value is 0. +* \param[out] a The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +* \retval TVG_RESULT_INSUFFICIENT_CONDITION No stroke was set. +*/ +TVG_API Tvg_Result tvg_shape_get_stroke_color(const Tvg_Paint* paint, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a); + + +/*! +* \brief Sets the linear gradient fill of the stroke for all of the figures from the path. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[in] grad The linear gradient fill. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +* \retval TVG_RESULT_FAILED_ALLOCATION An internal error with a memory allocation. +* \retval TVG_RESULT_MEMORY_CORRUPTION An invalid Tvg_Gradient pointer. +* +* \note Either a solid color or a gradient fill is applied, depending on what was set as last. +*/ +TVG_API Tvg_Result tvg_shape_set_stroke_linear_gradient(Tvg_Paint* paint, Tvg_Gradient* grad); + + +/*! +* \brief Sets the radial gradient fill of the stroke for all of the figures from the path. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[in] grad The radial gradient fill. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +* \retval TVG_RESULT_FAILED_ALLOCATION An internal error with a memory allocation. +* \retval TVG_RESULT_MEMORY_CORRUPTION An invalid Tvg_Gradient pointer. +* +* \note Either a solid color or a gradient fill is applied, depending on what was set as last. +*/ +TVG_API Tvg_Result tvg_shape_set_stroke_radial_gradient(Tvg_Paint* paint, Tvg_Gradient* grad); + + +/*! +* \brief Gets the gradient fill of the shape's stroke. +* +* The function does not allocate any memory. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[out] grad The gradient fill. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid pointer passed as an argument. +*/ +TVG_API Tvg_Result tvg_shape_get_stroke_gradient(const Tvg_Paint* paint, Tvg_Gradient** grad); + + +/*! +* \brief Sets the shape's stroke dash pattern. +* +* \code +* //dash pattern examples +* float dashPattern[2] = {20, 10}; // -- -- -- +* float dashPattern[2] = {40, 20}; // ---- ---- ---- +* float dashPattern[4] = {10, 20, 30, 40} // - --- - --- +* \endcode +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[in] dashPattern The array of consecutive pair values of the dash length and the gap length. +* \param[in] cnt The size of the @p dashPattern array. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid pointer passed as an argument and @p cnt > 0, the given length of the array is less than two or any of the @p dashPattern values is zero or less. +* \retval TVG_RESULT_FAILED_ALLOCATION An internal error with a memory allocation. +* +* \note To reset the stroke dash pattern, pass @c nullptr to @p dashPattern and zero to @p cnt. +*/ +TVG_API Tvg_Result tvg_shape_set_stroke_dash(Tvg_Paint* paint, const float* dashPattern, uint32_t cnt); + + +/*! +* \brief Gets the dash pattern of the stroke. +* +* The function does not allocate any memory. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[out] dashPattern The array of consecutive pair values of the dash length and the gap length. +* \param[out] cnt The size of the @p dashPattern array. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid pointer passed as an argument. +*/ +TVG_API Tvg_Result tvg_shape_get_stroke_dash(const Tvg_Paint* paint, const float** dashPattern, uint32_t* cnt); + + +/*! +* \brief Sets the cap style used for stroking the path. +* +* The cap style specifies the shape to be used at the end of the open stroked sub-paths. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[in] cap The cap style value. The default value is @c TVG_STROKE_CAP_SQUARE. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +* \retval TVG_RESULT_FAILED_ALLOCATION An internal error with a memory allocation. +*/ +TVG_API Tvg_Result tvg_shape_set_stroke_cap(Tvg_Paint* paint, Tvg_Stroke_Cap cap); + + +/*! +* \brief Gets the stroke cap style used for stroking the path. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[out] cap The cap style value. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid pointer passed as an argument. +*/ +TVG_API Tvg_Result tvg_shape_get_stroke_cap(const Tvg_Paint* paint, Tvg_Stroke_Cap* cap); + + +/*! +* \brief Sets the join style for stroked path segments. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[in] join The join style value. The default value is @c TVG_STROKE_JOIN_BEVEL. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +* \retval TVG_RESULT_FAILED_ALLOCATION An internal error with a memory allocation. +*/ +TVG_API Tvg_Result tvg_shape_set_stroke_join(Tvg_Paint* paint, Tvg_Stroke_Join join); + + +/*! +* \brief The function gets the stroke join method +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[out] join The join style value. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid pointer passed as an argument. +*/ +TVG_API Tvg_Result tvg_shape_get_stroke_join(const Tvg_Paint* paint, Tvg_Stroke_Join* join); + + +/*! +* \brief Sets the stroke miterlimit. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[in] miterlimit The miterlimit imposes a limit on the extent of the stroke join when the @c TVG_STROKE_JOIN_MITER join style is set. The default value is 4. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +* \retval TVG_RESULT_NOT_SUPPORTED Unsupported value. +* \retval TVG_RESULT_FAILED_ALLOCATION An internal error with a memory allocation. +* +* \since 0.11 +*/ +TVG_API Tvg_Result tvg_shape_set_stroke_miterlimit(Tvg_Paint* paint, float miterlimit); + + +/*! +* \brief The function gets the stroke miterlimit. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[out] miterlimit The stroke miterlimit. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid pointer passed as an argument. +* +* \since 0.11 +*/ +TVG_API Tvg_Result tvg_shape_get_stroke_miterlimit(const Tvg_Paint* paint, float* miterlimit); + + +/*! +* \brief Sets the shape's solid color. +* +* The parts of the shape defined as inner are colored. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[in] r The red color channel value in the range [0 ~ 255]. The default value is 0. +* \param[in] g The green color channel value in the range [0 ~ 255]. The default value is 0. +* \param[in] b The blue color channel value in the range [0 ~ 255]. The default value is 0. +* \param[in] a The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque. The default value is 0. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +* +* \note Either a solid color or a gradient fill is applied, depending on what was set as last. +* \see tvg_shape_set_fill_rule() +*/ +TVG_API Tvg_Result tvg_shape_set_fill_color(Tvg_Paint* paint, uint8_t r, uint8_t g, uint8_t b, uint8_t a); + + +/*! +* \brief Gets the shape's solid color. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[out] r The red color channel value in the range [0 ~ 255]. The default value is 0. +* \param[out] g The green color channel value in the range [0 ~ 255]. The default value is 0. +* \param[out] b The blue color channel value in the range [0 ~ 255]. The default value is 0. +* \param[out] a The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque. The default value is 0. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +*/ +TVG_API Tvg_Result tvg_shape_get_fill_color(const Tvg_Paint* paint, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a); + + +/*! +* \brief Sets the shape's fill rule. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[in] rule The fill rule value. The default value is @c TVG_FILL_RULE_WINDING. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +*/ +TVG_API Tvg_Result tvg_shape_set_fill_rule(Tvg_Paint* paint, Tvg_Fill_Rule rule); + + +/*! +* \brief Gets the shape's fill rule. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[out] rule shape's fill rule +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid pointer passed as an argument. +*/ +TVG_API Tvg_Result tvg_shape_get_fill_rule(const Tvg_Paint* paint, Tvg_Fill_Rule* rule); + + +/*! +* \brief Sets the rendering order of the stroke and the fill. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[in] strokeFirst If @c true the stroke is rendered before the fill, otherwise the stroke is rendered as the second one (the default option). +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +* \retval TVG_RESULT_FAILED_ALLOCATION An internal error with a memory allocation. +* +* \since 0.10 +*/ +TVG_API Tvg_Result tvg_shape_set_paint_order(Tvg_Paint* paint, bool strokeFirst); + + +/*! +* \brief Sets the linear gradient fill for all of the figures from the path. +* +* The parts of the shape defined as inner are filled. +* +* \code +* Tvg_Gradient* grad = tvg_linear_gradient_new(); +* tvg_linear_gradient_set(grad, 700, 700, 800, 800); +* Tvg_Color_Stop color_stops[4] = +* { +* {0.0 , 0, 0, 0, 255}, +* {0.25, 255, 0, 0, 255}, +* {0.5 , 0, 255, 0, 255}, +* {1.0 , 0, 0, 255, 255} +* }; +* tvg_gradient_set_color_stops(grad, color_stops, 4); +* tvg_shape_set_linear_gradient(shape, grad); +* \endcode +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[in] grad The linear gradient fill. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +* \retval TVG_RESULT_MEMORY_CORRUPTION An invalid Tvg_Gradient pointer. +* +* \note Either a solid color or a gradient fill is applied, depending on what was set as last. +* \see tvg_shape_set_fill_rule() +*/ +TVG_API Tvg_Result tvg_shape_set_linear_gradient(Tvg_Paint* paint, Tvg_Gradient* grad); + + +/*! +* \brief Sets the radial gradient fill for all of the figures from the path. +* +* The parts of the shape defined as inner are filled. +* +* \code +* Tvg_Gradient* grad = tvg_radial_gradient_new(); +* tvg_radial_gradient_set(grad, 550, 550, 50); +* Tvg_Color_Stop color_stops[4] = +* { +* {0.0 , 0, 0, 0, 255}, +* {0.25, 255, 0, 0, 255}, +* {0.5 , 0, 255, 0, 255}, +* {1.0 , 0, 0, 255, 255} +* }; +* tvg_gradient_set_color_stops(grad, color_stops, 4); +* tvg_shape_set_radial_gradient(shape, grad); +* \endcode +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[in] grad The radial gradient fill. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +* \retval TVG_RESULT_MEMORY_CORRUPTION An invalid Tvg_Gradient pointer. +* +* \note Either a solid color or a gradient fill is applied, depending on what was set as last. +* \see tvg_shape_set_fill_rule() +*/ +TVG_API Tvg_Result tvg_shape_set_radial_gradient(Tvg_Paint* paint, Tvg_Gradient* grad); + + +/*! +* \brief Gets the gradient fill of the shape. +* +* The function does not allocate any data. +* +* \param[in] paint A Tvg_Paint pointer to the shape object. +* \param[out] grad The gradient fill. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid pointer passed as an argument. +*/ +TVG_API Tvg_Result tvg_shape_get_gradient(const Tvg_Paint* paint, Tvg_Gradient** grad); + + +/** \} */ // end defgroup ThorVGCapi_Shape + + +/** +* \defgroup ThorVGCapi_Gradient Gradient +* \brief A module managing the gradient fill of objects. +* +* The module enables to set and to get the gradient colors and their arrangement inside the gradient bounds, +* to specify the gradient bounds and the gradient behavior in case the area defined by the gradient bounds +* is smaller than the area to be filled. +* +* \{ +*/ + +/************************************************************************/ +/* Gradient API */ +/************************************************************************/ +/*! +* \brief Creates a new linear gradient object. +* +* \code +* Tvg_Paint* shape = tvg_shape_new(); +* tvg_shape_append_rect(shape, 700, 700, 100, 100, 20, 20); +* Tvg_Gradient* grad = tvg_linear_gradient_new(); +* tvg_linear_gradient_set(grad, 700, 700, 800, 800); +* Tvg_Color_Stop color_stops[2] = +* { +* {0.0, 0, 0, 0, 255}, +* {1.0, 0, 255, 0, 255}, +* }; +* tvg_gradient_set_color_stops(grad, color_stops, 2); +* tvg_shape_set_linear_gradient(shape, grad); +* \endcode +* +* \return A new linear gradient object. +*/ +TVG_API Tvg_Gradient* tvg_linear_gradient_new(void); + + +/*! +* \brief Creates a new radial gradient object. +* +* \code +* Tvg_Paint* shape = tvg_shape_new(); +* tvg_shape_append_rect(shape, 700, 700, 100, 100, 20, 20); +* Tvg_Gradient* grad = tvg_radial_gradient_new(); +* tvg_radial_gradient_set(grad, 550, 550, 50); +* Tvg_Color_Stop color_stops[2] = +* { +* {0.0, 0, 0, 0, 255}, +* {1.0, 0, 255, 0, 255}, +* }; +* tvg_gradient_set_color_stops(grad, color_stops, 2); +* tvg_shape_set_radial_gradient(shape, grad); +* \endcode +* +* \return A new radial gradient object. +*/ +TVG_API Tvg_Gradient* tvg_radial_gradient_new(void); + + +/*! +* \brief Sets the linear gradient bounds. +* +* The bounds of the linear gradient are defined as a surface constrained by two parallel lines crossing +* the given points (@p x1, @p y1) and (@p x2, @p y2), respectively. Both lines are perpendicular to the line linking +* (@p x1, @p y1) and (@p x2, @p y2). +* +* \param[in] grad The Tvg_Gradient object of which bounds are to be set. +* @param[in] x1 The horizontal coordinate of the first point used to determine the gradient bounds. +* @param[in] y1 The vertical coordinate of the first point used to determine the gradient bounds. +* @param[in] x2 The horizontal coordinate of the second point used to determine the gradient bounds. +* @param[in] y2 The vertical coordinate of the second point used to determine the gradient bounds. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Gradient pointer. +* +* \note In case the first and the second points are equal, an object filled with such a gradient fill is not rendered. +*/ +TVG_API Tvg_Result tvg_linear_gradient_set(Tvg_Gradient* grad, float x1, float y1, float x2, float y2); + + +/*! +* \brief Gets the linear gradient bounds. +* +* The bounds of the linear gradient are defined as a surface constrained by two parallel lines crossing +* the given points (@p x1, @p y1) and (@p x2, @p y2), respectively. Both lines are perpendicular to the line linking +* (@p x1, @p y1) and (@p x2, @p y2). +* +* \param[in] grad The Tvg_Gradient object of which to get the bounds. +* \param[out] x1 The horizontal coordinate of the first point used to determine the gradient bounds. +* \param[out] y1 The vertical coordinate of the first point used to determine the gradient bounds. +* \param[out] x2 The horizontal coordinate of the second point used to determine the gradient bounds. +* \param[out] y2 The vertical coordinate of the second point used to determine the gradient bounds. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Gradient pointer. +*/ +TVG_API Tvg_Result tvg_linear_gradient_get(Tvg_Gradient* grad, float* x1, float* y1, float* x2, float* y2); + + +/*! +* \brief Sets the radial gradient bounds. +* +* The radial gradient bounds are defined as a circle centered in a given point (@p cx, @p cy) of a given radius. +* +* \param[in] grad The Tvg_Gradient object of which bounds are to be set. +* \param[in] cx The horizontal coordinate of the center of the bounding circle. +* \param[in] cy The vertical coordinate of the center of the bounding circle. +* \param[in] radius The radius of the bounding circle. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Gradient pointer or the @p radius value less than zero. +*/ +TVG_API Tvg_Result tvg_radial_gradient_set(Tvg_Gradient* grad, float cx, float cy, float radius); + + +/*! +* \brief The function gets radial gradient center point ant radius +* +* \param[in] grad The Tvg_Gradient object of which bounds are to be set. +* \param[out] cx The horizontal coordinate of the center of the bounding circle. +* \param[out] cy The vertical coordinate of the center of the bounding circle. +* \param[out] radius The radius of the bounding circle. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Gradient pointer. +*/ +TVG_API Tvg_Result tvg_radial_gradient_get(Tvg_Gradient* grad, float* cx, float* cy, float* radius); + + +/*! +* \brief Sets the parameters of the colors of the gradient and their position. +* +* \param[in] grad The Tvg_Gradient object of which the color information is to be set. +* \param[in] color_stop An array of Tvg_Color_Stop data structure. +* \param[in] cnt The size of the @p color_stop array equal to the colors number used in the gradient. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Gradient pointer. +*/ +TVG_API Tvg_Result tvg_gradient_set_color_stops(Tvg_Gradient* grad, const Tvg_Color_Stop* color_stop, uint32_t cnt); + + +/*! +* \brief Gets the parameters of the colors of the gradient, their position and number +* +* The function does not allocate any memory. +* +* \param[in] grad The Tvg_Gradient object of which to get the color information. +* \param[out] color_stop An array of Tvg_Color_Stop data structure. +* \param[out] cnt The size of the @p color_stop array equal to the colors number used in the gradient. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT A @c nullptr passed as the argument. +*/ +TVG_API Tvg_Result tvg_gradient_get_color_stops(const Tvg_Gradient* grad, const Tvg_Color_Stop** color_stop, uint32_t* cnt); + + +/*! +* \brief Sets the Tvg_Stroke_Fill value, which specifies how to fill the area outside the gradient bounds. +* +* \param[in] grad The Tvg_Gradient object. +* \param[in] spread The FillSpread value. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Gradient pointer. +*/ +TVG_API Tvg_Result tvg_gradient_set_spread(Tvg_Gradient* grad, const Tvg_Stroke_Fill spread); + + +/*! +* \brief Gets the FillSpread value of the gradient object. +* +* \param[in] grad The Tvg_Gradient object. +* \param[out] spread The FillSpread value. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT A @c nullptr passed as the argument. +*/ +TVG_API Tvg_Result tvg_gradient_get_spread(const Tvg_Gradient* grad, Tvg_Stroke_Fill* spread); + + +/*! +* \brief Sets the matrix of the affine transformation for the gradient object. +* +* The augmented matrix of the transformation is expected to be given. +* +* \param[in] grad The Tvg_Gradient object to be transformed. +* \param[in] m The 3x3 augmented matrix. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT A @c nullptr is passed as the argument. +* \retval TVG_RESULT_FAILED_ALLOCATION An internal error with a memory allocation. +*/ +TVG_API Tvg_Result tvg_gradient_set_transform(Tvg_Gradient* grad, const Tvg_Matrix* m); + + +/*! +* \brief Gets the matrix of the affine transformation of the gradient object. +* +* In case no transformation was applied, the identity matrix is set. +* +* \param[in] grad The Tvg_Gradient object of which to get the transformation matrix. +* \param[out] m The 3x3 augmented matrix. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT A @c nullptr is passed as the argument. +*/ +TVG_API Tvg_Result tvg_gradient_get_transform(const Tvg_Gradient* grad, Tvg_Matrix* m); + +/** +* \brief Gets the unique id value of the gradient instance indicating the instance type. +* +* \param[in] grad The Tvg_Gradient object of which to get the identifier value. +* \param[out] identifier The unique identifier of the gradient instance type. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT In case a @c nullptr is passed as the argument. +* +* \since 0.9 +*/ +TVG_API Tvg_Result tvg_gradient_get_identifier(const Tvg_Gradient* grad, Tvg_Identifier* identifier); + + +/*! +* \brief Duplicates the given Tvg_Gradient object. +* +* Creates a new object and sets its all properties as in the original object. +* +* \param[in] grad The Tvg_Gradient object to be copied. +* +* \return A copied Tvg_Gradient object if succeed, @c nullptr otherwise. +*/ +TVG_API Tvg_Gradient* tvg_gradient_duplicate(Tvg_Gradient* grad); + + +/*! +* \brief Deletes the given gradient object. +* +* \param[in] grad The gradient object to be deleted. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Gradient pointer. +*/ +TVG_API Tvg_Result tvg_gradient_del(Tvg_Gradient* grad); + + +/** \} */ // end defgroup ThorVGCapi_Gradient + + +/** +* \defgroup ThorVGCapi_Picture Picture +* +* \brief A module enabling to create and to load an image in one of the supported formats: svg, png, jpg, lottie and raw. +* +* +* \{ +*/ + +/************************************************************************/ +/* Picture API */ +/************************************************************************/ +/*! +* \brief Creates a new picture object. +* +* \return A new picture object. +*/ +TVG_API Tvg_Paint* tvg_picture_new(void); + + +/*! +* \brief Loads a picture data directly from a file. +* +* ThorVG efficiently caches the loaded data using the specified @p path as a key. +* This means that loading the same file again will not result in duplicate operations; +* instead, ThorVG will reuse the previously loaded picture data. +* +* \param[in] paint A Tvg_Paint pointer to the picture object. +* \param[in] path The absolute path to the image file. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer or an empty @p path. +* \retval TVG_RESULT_NOT_SUPPORTED A file with an unknown extension. +* \retval TVG_RESULT_UNKNOWN An error at a later stage. +*/ +TVG_API Tvg_Result tvg_picture_load(Tvg_Paint* paint, const char* path); + + +/*! +* \brief Loads a picture data from a memory block of a given size. +* +* ThorVG efficiently caches the loaded data using the specified @p data address as a key +* when the @p copy has @c false. This means that loading the same data again will not result in duplicate operations +* for the sharable @p data. Instead, ThorVG will reuse the previously loaded picture data. +* +* \param[in] paint A Tvg_Paint pointer to the picture object. +* \param[in] data A pointer to a memory location where the content of the picture raw data is stored. +* \param[in] w The width of the image @p data in pixels. +* \param[in] h The height of the image @p data in pixels. +* \param[in] premultiplied If @c true, the given image data is alpha-premultiplied. +* \param[in] copy If @c true the data are copied into the engine local buffer, otherwise they are not. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer or no data are provided or the @p width or @p height value is zero or less. +* \retval TVG_RESULT_FAILED_ALLOCATION A problem with memory allocation occurs. +* \retval TVG_RESULT_INSUFFICIENT_CONDITION An error occurs at a later stage. +* +* \since 0.9 +*/ +TVG_API Tvg_Result tvg_picture_load_raw(Tvg_Paint* paint, uint32_t *data, uint32_t w, uint32_t h, bool copy); + + +/*! +* \brief Loads a picture data from a memory block of a given size. +* +* ThorVG efficiently caches the loaded data using the specified @p data address as a key +* when the @p copy has @c false. This means that loading the same data again will not result in duplicate operations +* for the sharable @p data. Instead, ThorVG will reuse the previously loaded picture data. +* +* \param[in] paint A Tvg_Paint pointer to the picture object. +* \param[in] data A pointer to a memory location where the content of the picture file is stored. +* \param[in] size The size in bytes of the memory occupied by the @p data. +* \param[in] mimetype Mimetype or extension of data such as "jpg", "jpeg", "svg", "svg+xml", "lottie", "png", etc. In case an empty string or an unknown type is provided, the loaders will be tried one by one. +* \param[in] copy If @c true the data are copied into the engine local buffer, otherwise they are not. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT In case a @c nullptr is passed as the argument or the @p size is zero or less. +* \retval TVG_RESULT_NOT_SUPPORTED A file with an unknown extension. +* \retval TVG_RESULT_UNKNOWN An error at a later stage. +* +* \warning: It's the user responsibility to release the @p data memory if the @p copy is @c true. +*/ +TVG_API Tvg_Result tvg_picture_load_data(Tvg_Paint* paint, const char *data, uint32_t size, const char *mimetype, bool copy); + + +/*! +* \brief Resizes the picture content to the given width and height. +* +* The picture content is resized while keeping the default size aspect ratio. +* The scaling factor is established for each of dimensions and the smaller value is applied to both of them. +* +* \param[in] paint A Tvg_Paint pointer to the picture object. +* \param[in] w A new width of the image in pixels. +* \param[in] h A new height of the image in pixels. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +* \retval TVG_RESULT_INSUFFICIENT_CONDITION An internal error. +*/ +TVG_API Tvg_Result tvg_picture_set_size(Tvg_Paint* paint, float w, float h); + + +/*! +* \brief Gets the size of the loaded picture. +* +* \param[in] paint A Tvg_Paint pointer to the picture object. +* \param[out] w A width of the image in pixels. +* \param[out] h A height of the image in pixels. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +*/ +TVG_API Tvg_Result tvg_picture_get_size(const Tvg_Paint* paint, float* w, float* h); + + +/** \} */ // end defgroup ThorVGCapi_Picture + + +/** +* \defgroup ThorVGCapi_Scene Scene +* \brief A module managing the multiple paints as one group paint. +* +* As a group, scene can be transformed, translucent, composited with other target paints, +* its children will be affected by the scene world. +* +* \{ +*/ + +/************************************************************************/ +/* Scene API */ +/************************************************************************/ +/*! +* \brief Creates a new scene object. +* +* A scene object is used to group many paints into one object, which can be manipulated using TVG APIs. +* +* \return A new scene object. +*/ +TVG_API Tvg_Paint* tvg_scene_new(void); + + +/*! +* \brief Sets the size of the container, where all the paints pushed into the scene are stored. +* +* If the number of objects pushed into the scene is known in advance, calling the function +* prevents multiple memory reallocation, thus improving the performance. +* +* \param[in] scene A Tvg_Paint pointer to the scene object. +* \param[in] size The number of objects for which the memory is to be reserved. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_FAILED_ALLOCATION An internal error with a memory allocation. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Paint pointer. +*/ +TVG_DEPRECATED TVG_API Tvg_Result tvg_scene_reserve(Tvg_Paint* scene, uint32_t size); + + +/*! +* \brief Passes drawing elements to the scene using Tvg_Paint objects. +* +* Only the paints pushed into the scene will be the drawn targets. +* The paints are retained by the scene until the tvg_scene_clear() is called. +* If you know the number of pushed objects in advance, please call tvg_scene_reserve(). +* +* \param[in] scene A Tvg_Paint pointer to the scene object. +* \param[in] paint A graphical object to be drawn. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT A @c nullptr passed as the argument. +* \retval TVG_RESULT_MEMORY_CORRUPTION An internal error. +* +* \note The rendering order of the paints is the same as the order as they were pushed. Consider sorting the paints before pushing them if you intend to use layering. +*/ +TVG_API Tvg_Result tvg_scene_push(Tvg_Paint* scene, Tvg_Paint* paint); + + +/*! +* \brief Clears a Tvg_Scene objects from pushed paints. +* +* Tvg_Paint objects stored in the scene are released if @p free is set to @c true, otherwise the memory is not deallocated and +* all paints should be released manually in order to avoid memory leaks. +* +* \param[in] scene The Tvg_Scene object to be cleared. +* \param[in] free If @c true the memory occupied by paints is deallocated, otherwise it is not. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Canvas pointer. +* +* \warning Please use the @p free argument only when you know how it works, otherwise it's not recommended. +*/ +TVG_API Tvg_Result tvg_scene_clear(Tvg_Paint* scene, bool free); + +/** \} */ // end defgroup ThorVGCapi_Scene + + +/** +* \defgroup ThorVGCapi_Saver Saver +* \brief A module for exporting a paint object into a specified file. +* +* The module enables to save the composed scene and/or image from a paint object. +* Once it's successfully exported to a file, it can be recreated using the Picture module. +* +* \{ +*/ + +/************************************************************************/ +/* Saver API */ +/************************************************************************/ +/*! +* \brief Creates a new Tvg_Saver object. +* +* \return A new Tvg_Saver object. +*/ +TVG_API Tvg_Saver* tvg_saver_new(void); + + +/*! +* \brief Exports the given @p paint data to the given @p path +* +* If the saver module supports any compression mechanism, it will optimize the data size. +* This might affect the encoding/decoding time in some cases. You can turn off the compression +* if you wish to optimize for speed. +* +* \param[in] saver The Tvg_Saver object connected with the saving task. +* \param[in] paint The paint to be saved with all its associated properties. +* \param[in] path A path to the file, in which the paint data is to be saved. +* \param[in] compress If @c true then compress data if possible. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT A @c nullptr passed as the argument. +* \retval TVG_RESULT_INSUFFICIENT_CONDITION Currently saving other resources. +* \retval TVG_RESULT_NOT_SUPPORTED Trying to save a file with an unknown extension or in an unsupported format. +* \retval TVG_RESULT_MEMORY_CORRUPTION An internal error. +* \retval TVG_RESULT_UNKNOWN An empty paint is to be saved. +* +* \note Saving can be asynchronous if the assigned thread number is greater than zero. To guarantee the saving is done, call tvg_saver_sync() afterwards. +* \see tvg_saver_sync() +*/ +TVG_API Tvg_Result tvg_saver_save(Tvg_Saver* saver, Tvg_Paint* paint, const char* path, bool compress); + + +/*! +* \brief Guarantees that the saving task is finished. +* +* The behavior of the Saver module works on a sync/async basis, depending on the threading setting of the Initializer. +* Thus, if you wish to have a benefit of it, you must call tvg_saver_sync() after the tvg_saver_save() in the proper delayed time. +* Otherwise, you can call tvg_saver_sync() immediately. +* +* \param[in] saver The Tvg_Saver object connected with the saving task. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT A @c nullptr passed as the argument. +* \retval TVG_RESULT_INSUFFICIENT_CONDITION No saving task is running. +* +* \note The asynchronous tasking is dependent on the Saver module implementation. +* \see tvg_saver_save() +*/ +TVG_API Tvg_Result tvg_saver_sync(Tvg_Saver* saver); + + +/*! +* \brief Deletes the given Tvg_Saver object. +* +* \param[in] saver The Tvg_Saver object to be deleted. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Saver pointer. +*/ +TVG_API Tvg_Result tvg_saver_del(Tvg_Saver* saver); + + +/** \} */ // end defgroup ThorVGCapi_Saver + + +/** +* \defgroup ThorVGCapi_Animation Animation +* \brief A module for manipulation of animatable images. +* +* The module supports the display and control of animation frames. +* +* \{ +*/ + +/************************************************************************/ +/* Animation API */ +/************************************************************************/ + +/*! +* \brief Creates a new Animation object. +* +* \return Tvg_Animation A new Tvg_Animation object. +* +* \since 0.13 +*/ +TVG_API Tvg_Animation* tvg_animation_new(void); + + +/*! +* \brief Specifies the current frame in the animation. +* +* \param[in] animation A Tvg_Animation pointer to the animation object. +* \param[in] no The index of the animation frame to be displayed. The index should be less than the tvg_animation_total_frame(). +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Animation pointer. +* \retval TVG_RESULT_INSUFFICIENT_CONDITION No animatable data loaded from the Picture. +* \retval TVG_RESULT_NOT_SUPPORTED The picture data does not support animations. +* +* \note For efficiency, ThorVG ignores updates to the new frame value if the difference from the current frame value +* is less than 0.001. In such cases, it returns @c Result::InsufficientCondition. +* Values less than 0.001 may be disregarded and may not be accurately retained by the Animation. +* \see tvg_animation_get_total_frame() +* +* \since 0.13 +*/ +TVG_API Tvg_Result tvg_animation_set_frame(Tvg_Animation* animation, float no); + + +/*! +* \brief Retrieves a picture instance associated with this animation instance. +* +* This function provides access to the picture instance that can be used to load animation formats, such as Lottie(json). +* After setting up the picture, it can be pushed to the designated canvas, enabling control over animation frames +* with this Animation instance. +* +* \param[in] animation A Tvg_Animation pointer to the animation object. +* +* \return A picture instance that is tied to this animation. +* +* \warning The picture instance is owned by Animation. It should not be deleted manually. +* +* \since 0.13 +*/ +TVG_API Tvg_Paint* tvg_animation_get_picture(Tvg_Animation* animation); + + +/*! +* \brief Retrieves the current frame number of the animation. +* +* \param[in] animation A Tvg_Animation pointer to the animation object. +* \param[in] no The current frame number of the animation, between 0 and totalFrame() - 1. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Animation pointer or @p no +* +* \see tvg_animation_get_total_frame() +* \see tvg_animation_set_frame() +* +* \since 0.13 +*/ +TVG_API Tvg_Result tvg_animation_get_frame(Tvg_Animation* animation, float* no); + + +/*! +* \brief Retrieves the total number of frames in the animation. +* +* \param[in] animation A Tvg_Animation pointer to the animation object. +* \param[in] cnt The total number of frames in the animation. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Animation pointer or @p cnt. +* +* \note Frame numbering starts from 0. +* \note If the Picture is not properly configured, this function will return 0. +* +* \since 0.13 +*/ +TVG_API Tvg_Result tvg_animation_get_total_frame(Tvg_Animation* animation, float* cnt); + + +/*! +* \brief Retrieves the duration of the animation in seconds. +* +* \param[in] animation A Tvg_Animation pointer to the animation object. +* \param[in] duration The duration of the animation in seconds. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Animation pointer or @p duration. +* +* \note If the Picture is not properly configured, this function will return 0. +* +* \since 0.13 +*/ +TVG_API Tvg_Result tvg_animation_get_duration(Tvg_Animation* animation, float* duration); + + +/*! +* \brief Specifies the playback segment of the animation. (Experimental API) +* +* \param[in] animation The Tvg_Animation pointer to the animation object. +* \param[in] begin segment begin. +* \param[in] end segment end. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INSUFFICIENT_CONDITION In case the animation is not loaded. +* \retval TVG_RESULT_INVALID_ARGUMENT When the given parameters are out of range. +* +* \since 0.13 +*/ +TVG_API Tvg_Result tvg_animation_set_segment(Tvg_Animation* animation, float begin, float end); + + +/*! +* \brief Gets the current segment. (Experimental API) +* +* \param[in] animation The Tvg_Animation pointer to the animation object. +* \param[out] begin segment begin. +* \param[out] end segment end. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INSUFFICIENT_CONDITION In case the animation is not loaded. +* \retval TVG_RESULT_INVALID_ARGUMENT When the given parameters are @c nullptr. +*/ +TVG_API Tvg_Result tvg_animation_get_segment(Tvg_Animation* animation, float* begin, float* end); + + +/*! +* \brief Deletes the given Tvg_Animation object. +* +* \param[in] animation The Tvg_Animation object to be deleted. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Animation pointer. +* +* \since 0.13 +*/ +TVG_API Tvg_Result tvg_animation_del(Tvg_Animation* animation); + + +/** \} */ // end defgroup ThorVGCapi_Animation + + +/** +* \defgroup ThorVGCapi_LottieAnimation LottieAnimation +* \brief A module for manipulation of lottie extension features. +* +* The module enables control of advanced Lottie features. +* \{ +*/ + +/************************************************************************/ +/* LottieAnimation Extension API */ +/************************************************************************/ + +/*! +* \brief Creates a new LottieAnimation object. (Experimental API) +* +* \return Tvg_Animation A new Tvg_LottieAnimation object. +*/ +TVG_API Tvg_Animation* tvg_lottie_animation_new(void); + + +/*! +* \brief Override the lottie properties through the slot data. (Experimental API) +* +* \param[in] animation The Tvg_Animation object to override the property with the slot. +* \param[in] slot The Lottie slot data in json, or @c nullptr to reset. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INSUFFICIENT_CONDITION In case the animation is not loaded. +* \retval TVG_RESULT_INVALID_ARGUMENT When the given @p slot is invalid +* \retval TVG_RESULT_NOT_SUPPORTED The Lottie Animation is not supported. +*/ +TVG_API Tvg_Result tvg_lottie_animation_override(Tvg_Animation* animation, const char* slot); + + +/*! +* \brief Specifies a segment by marker. (Experimental API) +* +* \param[in] animation The Tvg_Animation pointer to the Lottie animation object. +* \param[in] marker The name of the segment marker. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INSUFFICIENT_CONDITION In case the animation is not loaded. +* \retval TVG_RESULT_INVALID_ARGUMENT When the given @p marker is invalid. +* \retval TVG_RESULT_NOT_SUPPORTED The Lottie Animation is not supported. +*/ +TVG_API Tvg_Result tvg_lottie_animation_set_marker(Tvg_Animation* animation, const char* marker); + + +/*! +* \brief Gets the marker count of the animation. (Experimental API) +* +* \param[in] animation The Tvg_Animation pointer to the Lottie animation object. +* \param[out] cnt The count value of the markers. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT In case a @c nullptr is passed as the argument. +*/ +TVG_API Tvg_Result tvg_lottie_animation_get_markers_cnt(Tvg_Animation* animation, uint32_t* cnt); + + +/*! +* \brief Gets the marker name by a given index. (Experimental API) +* +* \param[in] animation The Tvg_Animation pointer to the Lottie animation object. +* \param[in] idx The index of the animation marker, starts from 0. +* \param[out] name The name of marker when succeed. +* +* \return Tvg_Result enumeration. +* \retval TVG_RESULT_SUCCESS Succeed. +* \retval TVG_RESULT_INVALID_ARGUMENT In case @c nullptr is passed as the argument or @c idx is out of range. +*/ +TVG_API Tvg_Result tvg_lottie_animation_get_marker(Tvg_Animation* animation, uint32_t idx, const char** name); + + +/** \} */ // end addtogroup ThorVGCapi_LottieAnimation + + +/** \} */ // end defgroup ThorVGCapi + + +#ifdef __cplusplus +} +#endif + +#endif //_THORVG_CAPI_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/thorvg_lottie.h b/include/liblvgl/libs/thorvg/thorvg_lottie.h new file mode 100644 index 00000000..b69410ae --- /dev/null +++ b/include/liblvgl/libs/thorvg/thorvg_lottie.h @@ -0,0 +1,100 @@ +#ifndef _THORVG_LOTTIE_H_ +#define _THORVG_LOTTIE_H_ + +#include "thorvg.h" + +namespace tvg +{ + +/** + * @class LottieAnimation + * + * @brief The LottieAnimation class enables control of advanced Lottie features. + * + * This class extends the Animation and has additional interfaces. + * + * @see Animation + * + * @note Experimental API + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL +class TVG_API LottieAnimation final : public Animation +{ +public: + ~LottieAnimation(); + + /** + * @brief Override Lottie properties using slot data. + * + * @param[in] slot The Lottie slot data in JSON format to override, or @c nullptr to reset. + * + * @retval Result::Success When succeed. + * @retval Result::InsufficientCondition In case the animation is not loaded. + * @retval Result::InvalidArguments When the given parameter is invalid. + * + * @note Experimental API + */ + Result override(const char* slot) noexcept; + + /** + * @brief Specifies a segment by marker. + * + * Markers are used to control animation playback by specifying start and end points, + * eliminating the need to know the exact frame numbers. + * Generally, markers are designated at the design level, + * meaning the callers must know the marker name in advance to use it. + * + * @param[in] marker The name of the segment marker. + * + * @retval Result::Success When successful. + * @retval Result::InsufficientCondition If the animation is not loaded. + * @retval Result::InvalidArguments When the given parameter is invalid. + * @retval Result::NonSupport When it's not animatable. + * + * @note If a @c marker is specified, the previously set segment will be disregarded. + * @note Set @c nullptr to reset the specified segment. + * @see Animation::segment(float begin, float end) + * @note Experimental API + */ + Result segment(const char* marker) noexcept; + + /** + * @brief Gets the marker count of the animation. + * + * @retval The count of the markers, zero if there is no marker. + * + * @see LottieAnimation::marker() + * @note Experimental API + */ + uint32_t markersCnt() noexcept; + + /** + * @brief Gets the marker name by a given index. + * + * @param[in] idx The index of the animation marker, starts from 0. + * + * @retval The name of marker when succeed, @c nullptr otherwise. + * + * @see LottieAnimation::markersCnt() + * @note Experimental API + */ + const char* marker(uint32_t idx) noexcept; + + /** + * @brief Creates a new LottieAnimation object. + * + * @return A new LottieAnimation object. + * + * @note Experimental API + */ + static std::unique_ptr gen() noexcept; +}; + +} //namespace + +#endif //_THORVG_LOTTIE_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgAccessor.cpp b/include/liblvgl/libs/thorvg/tvgAccessor.cpp new file mode 100644 index 00000000..e1f8608d --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgAccessor.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2021 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgIteratorAccessor.h" + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +static bool accessChildren(Iterator* it, function func) +{ + while (auto child = it->next()) { + //Access the child + if (!func(child)) return false; + + //Access the children of the child + if (auto it2 = IteratorAccessor::iterator(child)) { + if (!accessChildren(it2, func)) { + delete(it2); + return false; + } + delete(it2); + } + } + return true; +} + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +unique_ptr Accessor::set(unique_ptr picture, function func) noexcept +{ + auto p = picture.get(); + if (!p || !func) return picture; + + //Use the Preorder Tree-Search + + //Root + if (!func(p)) return picture; + + //Children + if (auto it = IteratorAccessor::iterator(p)) { + accessChildren(it, func); + delete(it); + } + return picture; +} + + +Accessor::~Accessor() +{ + +} + + +Accessor::Accessor() : pImpl(nullptr) +{ + +} + + +unique_ptr Accessor::gen() noexcept +{ + return unique_ptr(new Accessor); +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgAnimation.cpp b/include/liblvgl/libs/thorvg/tvgAnimation.cpp new file mode 100644 index 00000000..399ee458 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgAnimation.cpp @@ -0,0 +1,132 @@ +/* + * Copyright (c) 2023 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgFrameModule.h" +#include "tvgAnimation.h" + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +Animation::~Animation() +{ + delete(pImpl); +} + + +Animation::Animation() : pImpl(new Impl) +{ +} + + +Result Animation::frame(float no) noexcept +{ + auto loader = pImpl->picture->pImpl->loader; + + if (!loader) return Result::InsufficientCondition; + if (!loader->animatable()) return Result::NonSupport; + + if (static_cast(loader)->frame(no)) return Result::Success; + return Result::InsufficientCondition; +} + + +Picture* Animation::picture() const noexcept +{ + return pImpl->picture; +} + + +float Animation::curFrame() const noexcept +{ + auto loader = pImpl->picture->pImpl->loader; + + if (!loader) return 0; + if (!loader->animatable()) return 0; + + return static_cast(loader)->curFrame(); +} + + +float Animation::totalFrame() const noexcept +{ + auto loader = pImpl->picture->pImpl->loader; + + if (!loader) return 0; + if (!loader->animatable()) return 0; + + return static_cast(loader)->totalFrame(); +} + + +float Animation::duration() const noexcept +{ + auto loader = pImpl->picture->pImpl->loader; + + if (!loader) return 0; + if (!loader->animatable()) return 0; + + return static_cast(loader)->duration(); +} + + +Result Animation::segment(float begin, float end) noexcept +{ + if (begin < 0.0 || end > 1.0 || begin >= end) return Result::InvalidArguments; + + auto loader = pImpl->picture->pImpl->loader; + if (!loader) return Result::InsufficientCondition; + if (!loader->animatable()) return Result::NonSupport; + + static_cast(loader)->segment(begin, end); + + return Result::Success; +} + + +Result Animation::segment(float *begin, float *end) noexcept +{ + auto loader = pImpl->picture->pImpl->loader; + if (!loader) return Result::InsufficientCondition; + if (!loader->animatable()) return Result::NonSupport; + if (!begin && !end) return Result::InvalidArguments; + + static_cast(loader)->segment(begin, end); + + return Result::Success; +} + + +unique_ptr Animation::gen() noexcept +{ + return unique_ptr(new Animation); +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgAnimation.h b/include/liblvgl/libs/thorvg/tvgAnimation.h new file mode 100644 index 00000000..d0b45671 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgAnimation.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_ANIMATION_H_ +#define _TVG_ANIMATION_H_ + +#include "tvgCommon.h" +#include "tvgPaint.h" +#include "tvgPicture.h" + +struct Animation::Impl +{ + Picture* picture = nullptr; + + Impl() + { + picture = Picture::gen().release(); + PP(picture)->ref(); + } + + ~Impl() + { + if (PP(picture)->unref() == 0) { + delete(picture); + } + } +}; + +#endif //_TVG_ANIMATION_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgArray.h b/include/liblvgl/libs/thorvg/tvgArray.h new file mode 100644 index 00000000..33cd55ba --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgArray.h @@ -0,0 +1,209 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_ARRAY_H_ +#define _TVG_ARRAY_H_ + +#include +#include +#include + +namespace tvg +{ + +template +struct Array +{ + T* data = nullptr; + uint32_t count = 0; + uint32_t reserved = 0; + + Array(){} + + Array(int32_t size) + { + reserve(size); + } + + Array(const Array& rhs) + { + reset(); + *this = rhs; + } + + void push(T element) + { + if (count + 1 > reserved) { + reserved = count + (count + 2) / 2; + data = static_cast(realloc(data, sizeof(T) * reserved)); + } + data[count++] = element; + } + + void push(Array& rhs) + { + if (rhs.count == 0) return; + grow(rhs.count); + memcpy(data + count, rhs.data, rhs.count * sizeof(T)); + count += rhs.count; + } + + bool reserve(uint32_t size) + { + if (size > reserved) { + reserved = size; + data = static_cast(realloc(data, sizeof(T) * reserved)); + } + return true; + } + + bool grow(uint32_t size) + { + return reserve(count + size); + } + + const T& operator[](size_t idx) const + { + return data[idx]; + } + + T& operator[](size_t idx) + { + return data[idx]; + } + + const T* begin() const + { + return data; + } + + T* begin() + { + return data; + } + + T* end() + { + return data + count; + } + + const T* end() const + { + return data + count; + } + + const T& last() const + { + return data[count - 1]; + } + + const T& first() const + { + return data[0]; + } + + T& last() + { + return data[count - 1]; + } + + T& first() + { + return data[0]; + } + + void pop() + { + if (count > 0) --count; + } + + void reset() + { + free(data); + data = nullptr; + count = reserved = 0; + } + + void clear() + { + count = 0; + } + + bool empty() const + { + return count == 0; + } + + template + void sort() + { + qsort(data, 0, static_cast(count) - 1); + } + + void operator=(const Array& rhs) + { + reserve(rhs.count); + if (rhs.count > 0) memcpy(data, rhs.data, sizeof(T) * rhs.count); + count = rhs.count; + } + + ~Array() + { + free(data); + } + +private: + template + void qsort(T* arr, int32_t low, int32_t high) + { + if (low < high) { + int32_t i = low; + int32_t j = high; + T tmp = arr[low]; + while (i < j) { + while (i < j && !COMPARE{}(arr[j], tmp)) --j; + if (i < j) { + arr[i] = arr[j]; + ++i; + } + while (i < j && COMPARE{}(arr[i], tmp)) ++i; + if (i < j) { + arr[j] = arr[i]; + --j; + } + } + arr[i] = tmp; + qsort(arr, low, i - 1); + qsort(arr, i + 1, high); + } + } +}; + +} + +#endif //_TVG_ARRAY_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgBinaryDesc.h b/include/liblvgl/libs/thorvg/tvgBinaryDesc.h new file mode 100644 index 00000000..add8b081 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgBinaryDesc.h @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2021 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_BINARY_DESC_H_ +#define _TVG_BINARY_DESC_H_ + +/* TODO: Need to consider whether uin8_t is enough size for extension... + Rather than optimal data, we can use enough size and data compress? */ + +using TvgBinByte = uint8_t; +using TvgBinCounter = uint32_t; +using TvgBinTag = TvgBinByte; +using TvgBinFlag = TvgBinByte; + + +//Header +#define TVG_HEADER_SIZE 33 //TVG_HEADER_SIGNATURE_LENGTH + TVG_HEADER_VERSION_LENGTH + 2*SIZE(float) + TVG_HEADER_RESERVED_LENGTH + TVG_HEADER_COMPRESS_SIZE +#define TVG_HEADER_SIGNATURE "ThorVG" +#define TVG_HEADER_SIGNATURE_LENGTH 6 +#define TVG_HEADER_VERSION "001200" //Major 00, Minor 12, Micro 00 +#define TVG_HEADER_VERSION_LENGTH 6 +#define TVG_HEADER_RESERVED_LENGTH 1 //Storing flags for extensions +#define TVG_HEADER_COMPRESS_SIZE 12 //TVG_HEADER_UNCOMPRESSED_SIZE + TVG_HEADER_COMPRESSED_SIZE + TVG_HEADER_COMPRESSED_SIZE_BITS +//Compress Size +#define TVG_HEADER_UNCOMPRESSED_SIZE 4 //SIZE (TvgBinCounter) +#define TVG_HEADER_COMPRESSED_SIZE 4 //SIZE (TvgBinCounter) +#define TVG_HEADER_COMPRESSED_SIZE_BITS 4 //SIZE (TvgBinCounter) +//Reserved Flag +#define TVG_HEAD_FLAG_COMPRESSED 0x01 + +//Paint Type +#define TVG_TAG_CLASS_PICTURE (TvgBinTag)0xfc +#define TVG_TAG_CLASS_SHAPE (TvgBinTag)0xfd +#define TVG_TAG_CLASS_SCENE (TvgBinTag)0xfe + + +//Paint +#define TVG_TAG_PAINT_OPACITY (TvgBinTag)0x10 +#define TVG_TAG_PAINT_TRANSFORM (TvgBinTag)0x11 +#define TVG_TAG_PAINT_CMP_TARGET (TvgBinTag)0x01 +#define TVG_TAG_PAINT_CMP_METHOD (TvgBinTag)0x20 + + +//TODO: Keep this for the compatibility, Remove in TVG 1.0 release +//Scene + #define TVG_TAG_SCENE_RESERVEDCNT (TvgBinTag)0x30 + + +//Shape +#define TVG_TAG_SHAPE_PATH (TvgBinTag)0x40 +#define TVG_TAG_SHAPE_STROKE (TvgBinTag)0x41 +#define TVG_TAG_SHAPE_FILL (TvgBinTag)0x42 +#define TVG_TAG_SHAPE_COLOR (TvgBinTag)0x43 +#define TVG_TAG_SHAPE_FILLRULE (TvgBinTag)0x44 + + +//Stroke +#define TVG_TAG_SHAPE_STROKE_CAP (TvgBinTag)0x50 +#define TVG_TAG_SHAPE_STROKE_JOIN (TvgBinTag)0x51 +#define TVG_TAG_SHAPE_STROKE_WIDTH (TvgBinTag)0x52 +#define TVG_TAG_SHAPE_STROKE_COLOR (TvgBinTag)0x53 +#define TVG_TAG_SHAPE_STROKE_FILL (TvgBinTag)0x54 +#define TVG_TAG_SHAPE_STROKE_DASHPTRN (TvgBinTag)0x55 +#define TVG_TAG_SHAPE_STROKE_MITERLIMIT (TvgBinTag)0x56 +#define TVG_TAG_SHAPE_STROKE_ORDER (TvgBinTag)0x57 +#define TVG_TAG_SHAPE_STROKE_DASH_OFFSET (TvgBinTag)0x58 + + +//Fill +#define TVG_TAG_FILL_LINEAR_GRADIENT (TvgBinTag)0x60 +#define TVG_TAG_FILL_RADIAL_GRADIENT (TvgBinTag)0x61 +#define TVG_TAG_FILL_COLORSTOPS (TvgBinTag)0x62 +#define TVG_TAG_FILL_FILLSPREAD (TvgBinTag)0x63 +#define TVG_TAG_FILL_TRANSFORM (TvgBinTag)0x64 +#define TVG_TAG_FILL_RADIAL_GRADIENT_FOCAL (TvgBinTag)0x65 + +//Picture +#define TVG_TAG_PICTURE_RAW_IMAGE (TvgBinTag)0x70 +#define TVG_TAG_PICTURE_MESH (TvgBinTag)0x71 + +#endif //_TVG_BINARY_DESC_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgCanvas.cpp b/include/liblvgl/libs/thorvg/tvgCanvas.cpp new file mode 100644 index 00000000..22a97c20 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgCanvas.cpp @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgCanvas.h" + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +Canvas::Canvas(RenderMethod *pRenderer):pImpl(new Impl(pRenderer)) +{ +} + + +Canvas::~Canvas() +{ + delete(pImpl); +} + + +Result Canvas::reserve(TVG_UNUSED uint32_t n) noexcept +{ + return Result::NonSupport; +} + + +list& Canvas::paints() noexcept +{ + return pImpl->paints; +} + + +Result Canvas::push(unique_ptr paint) noexcept +{ + return pImpl->push(std::move(paint)); +} + + +Result Canvas::clear(bool free) noexcept +{ + return pImpl->clear(free); +} + + +Result Canvas::draw() noexcept +{ + TVGLOG("RENDERER", "Draw S. -------------------------------- Canvas(%p)", this); + auto ret = pImpl->draw(); + TVGLOG("RENDERER", "Draw E. -------------------------------- Canvas(%p)", this); + + return ret; +} + + +Result Canvas::update(Paint* paint) noexcept +{ + TVGLOG("RENDERER", "Update S. ------------------------------ Canvas(%p)", this); + auto ret = pImpl->update(paint, false); + TVGLOG("RENDERER", "Update E. ------------------------------ Canvas(%p)", this); + + return ret; +} + + +Result Canvas::viewport(int32_t x, int32_t y, int32_t w, int32_t h) noexcept +{ + return pImpl->viewport(x, y, w, h); +} + + +Result Canvas::sync() noexcept +{ + return pImpl->sync(); +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgCanvas.h b/include/liblvgl/libs/thorvg/tvgCanvas.h new file mode 100644 index 00000000..171f6aae --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgCanvas.h @@ -0,0 +1,165 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_CANVAS_H_ +#define _TVG_CANVAS_H_ + +#include "tvgPaint.h" + + +struct Canvas::Impl +{ + enum Status : uint8_t {Synced = 0, Updating, Drawing}; + + list paints; + RenderMethod* renderer; + RenderRegion vport = {0, 0, INT32_MAX, INT32_MAX}; + Status status = Status::Synced; + + bool refresh = false; //if all paints should be updated by force. + + Impl(RenderMethod* pRenderer) : renderer(pRenderer) + { + renderer->ref(); + } + + ~Impl() + { + //make it sure any deferred jobs + renderer->sync(); + renderer->clear(); + + clearPaints(); + + if (renderer->unref() == 0) delete(renderer); + } + + void clearPaints() + { + for (auto paint : paints) { + if (P(paint)->unref() == 0) delete(paint); + } + paints.clear(); + } + + Result push(unique_ptr paint) + { + //You cannot push paints during rendering. + if (status == Status::Drawing) return Result::InsufficientCondition; + + auto p = paint.release(); + if (!p) return Result::MemoryCorruption; + PP(p)->ref(); + paints.push_back(p); + + return update(p, true); + } + + Result clear(bool free) + { + //Clear render target before drawing + if (!renderer->clear()) return Result::InsufficientCondition; + + //Free paints + if (free) clearPaints(); + + status = Status::Synced; + + return Result::Success; + } + + void needRefresh() + { + refresh = true; + } + + Result update(Paint* paint, bool force) + { + if (paints.empty() || status == Status::Drawing) return Result::InsufficientCondition; + + Array clips; + auto flag = RenderUpdateFlag::None; + if (refresh || force) flag = RenderUpdateFlag::All; + + if (paint) { + paint->pImpl->update(renderer, nullptr, clips, 255, flag); + } else { + for (auto paint : paints) { + paint->pImpl->update(renderer, nullptr, clips, 255, flag); + } + refresh = false; + } + status = Status::Updating; + return Result::Success; + } + + Result draw() + { + if (status == Status::Drawing || paints.empty() || !renderer->preRender()) return Result::InsufficientCondition; + + bool rendered = false; + for (auto paint : paints) { + if (paint->pImpl->render(renderer)) rendered = true; + } + + if (!rendered || !renderer->postRender()) return Result::InsufficientCondition; + + status = Status::Drawing; + return Result::Success; + } + + Result sync() + { + if (status == Status::Synced) return Result::InsufficientCondition; + + if (renderer->sync()) { + status = Status::Synced; + return Result::Success; + } + + return Result::InsufficientCondition; + } + + Result viewport(int32_t x, int32_t y, int32_t w, int32_t h) + { + if (status != Status::Synced) return Result::InsufficientCondition; + RenderRegion val = {x, y, w, h}; + //intersect if the target buffer is already set. + auto surface = renderer->mainSurface(); + if (surface && surface->w > 0 && surface->h > 0) { + val.intersect({0, 0, (int32_t)surface->w, (int32_t)surface->h}); + } + if (vport == val) return Result::Success; + renderer->viewport(val); + vport = val; + needRefresh(); + return Result::Success; + } +}; + +#endif /* _TVG_CANVAS_H_ */ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgCapi.cpp b/include/liblvgl/libs/thorvg/tvgCapi.cpp new file mode 100644 index 00000000..1720d508 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgCapi.cpp @@ -0,0 +1,871 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "config.h" +#include +#include "thorvg.h" +#include "thorvg_capi.h" +#ifdef THORVG_LOTTIE_LOADER_SUPPORT +#include "thorvg_lottie.h" +#endif + +using namespace std; +using namespace tvg; + +#ifdef __cplusplus +extern "C" { +#endif + + +/************************************************************************/ +/* Engine API */ +/************************************************************************/ + +TVG_API Tvg_Result tvg_engine_init(Tvg_Engine engine_method, unsigned threads) +{ + return (Tvg_Result) Initializer::init(CanvasEngine(engine_method), threads); +} + + +TVG_API Tvg_Result tvg_engine_term(Tvg_Engine engine_method) +{ + return (Tvg_Result) Initializer::term(CanvasEngine(engine_method)); +} + + +/************************************************************************/ +/* Canvas API */ +/************************************************************************/ + +TVG_API Tvg_Canvas* tvg_swcanvas_create() +{ + return (Tvg_Canvas*) SwCanvas::gen().release(); +} + + +TVG_API Tvg_Result tvg_canvas_destroy(Tvg_Canvas* canvas) +{ + if (!canvas) return TVG_RESULT_INVALID_ARGUMENT; + delete(reinterpret_cast(canvas)); + return TVG_RESULT_SUCCESS; +} + + +TVG_API Tvg_Result tvg_swcanvas_set_mempool(Tvg_Canvas* canvas, Tvg_Mempool_Policy policy) +{ + if (!canvas) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(canvas)->mempool(static_cast(policy)); +} + + +TVG_API Tvg_Result tvg_swcanvas_set_target(Tvg_Canvas* canvas, uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h, Tvg_Colorspace cs) +{ + if (!canvas) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(canvas)->target(buffer, stride, w, h, static_cast(cs)); +} + + +TVG_API Tvg_Result tvg_canvas_push(Tvg_Canvas* canvas, Tvg_Paint* paint) +{ + if (!canvas || !paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(canvas)->push(unique_ptr((Paint*)paint)); +} + + +TVG_API Tvg_Result tvg_canvas_reserve(Tvg_Canvas* canvas, uint32_t n) +{ + return TVG_RESULT_NOT_SUPPORTED; +} + + +TVG_API Tvg_Result tvg_canvas_clear(Tvg_Canvas* canvas, bool free) +{ + if (!canvas) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(canvas)->clear(free); +} + + +TVG_API Tvg_Result tvg_canvas_update(Tvg_Canvas* canvas) +{ + if (!canvas) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(canvas)->update(nullptr); +} + + +TVG_API Tvg_Result tvg_canvas_est_viewport(Tvg_Canvas* canvas, int32_t x, int32_t y, int32_t w, int32_t h) +{ + if (!canvas) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(canvas)->viewport(x, y, w, h); +} + + +TVG_API Tvg_Result tvg_canvas_update_paint(Tvg_Canvas* canvas, Tvg_Paint* paint) +{ + if (!canvas || !paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(canvas)->update((Paint*) paint); +} + + +TVG_API Tvg_Result tvg_canvas_draw(Tvg_Canvas* canvas) +{ + if (!canvas) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(canvas)->draw(); +} + + +TVG_API Tvg_Result tvg_canvas_sync(Tvg_Canvas* canvas) +{ + if (!canvas) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(canvas)->sync(); +} + +TVG_API Tvg_Result tvg_canvas_set_viewport(Tvg_Canvas* canvas, int32_t x, int32_t y, int32_t w, int32_t h) +{ + if (!canvas) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(canvas)->viewport(x, y, w, h); +} + + +/************************************************************************/ +/* Paint API */ +/************************************************************************/ + +TVG_API Tvg_Result tvg_paint_del(Tvg_Paint* paint) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + delete(reinterpret_cast(paint)); + return TVG_RESULT_SUCCESS; +} + + +TVG_API Tvg_Result tvg_paint_scale(Tvg_Paint* paint, float factor) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->scale(factor); +} + + +TVG_API Tvg_Result tvg_paint_rotate(Tvg_Paint* paint, float degree) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->rotate(degree); +} + + +TVG_API Tvg_Result tvg_paint_translate(Tvg_Paint* paint, float x, float y) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->translate(x, y); +} + + +TVG_API Tvg_Result tvg_paint_set_transform(Tvg_Paint* paint, const Tvg_Matrix* m) +{ + if (!paint || !m) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->transform(*(reinterpret_cast(m))); +} + + +TVG_API Tvg_Result tvg_paint_get_transform(Tvg_Paint* paint, Tvg_Matrix* m) +{ + if (!paint || !m) return TVG_RESULT_INVALID_ARGUMENT; + *reinterpret_cast(m) = reinterpret_cast(paint)->transform(); + return TVG_RESULT_SUCCESS; +} + + +TVG_API Tvg_Paint* tvg_paint_duplicate(Tvg_Paint* paint) +{ + if (!paint) return nullptr; + return (Tvg_Paint*) reinterpret_cast(paint)->duplicate(); +} + + +TVG_API Tvg_Result tvg_paint_set_opacity(Tvg_Paint* paint, uint8_t opacity) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->opacity(opacity); +} + + +TVG_API Tvg_Result tvg_paint_get_opacity(const Tvg_Paint* paint, uint8_t* opacity) +{ + if (!paint || !opacity) return TVG_RESULT_INVALID_ARGUMENT; + *opacity = reinterpret_cast(paint)->opacity(); + return TVG_RESULT_SUCCESS; +} + + +TVG_API Tvg_Result tvg_paint_get_bounds(const Tvg_Paint* paint, float* x, float* y, float* w, float* h, bool transformed) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->bounds(x, y, w, h, transformed); +} + + +TVG_API Tvg_Result tvg_paint_set_composite_method(Tvg_Paint* paint, Tvg_Paint* target, Tvg_Composite_Method method) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->composite(unique_ptr((Paint*)(target)), (CompositeMethod)method); +} + + +TVG_API Tvg_Result tvg_paint_get_composite_method(const Tvg_Paint* paint, const Tvg_Paint** target, Tvg_Composite_Method* method) +{ + if (!paint || !target || !method) return TVG_RESULT_INVALID_ARGUMENT; + *reinterpret_cast(method) = reinterpret_cast(paint)->composite(reinterpret_cast(target)); + return TVG_RESULT_SUCCESS; +} + + +TVG_API Tvg_Result tvg_paint_set_blend_method(const Tvg_Paint* paint, Tvg_Blend_Method method) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->blend((BlendMethod)method); +} + + +TVG_API Tvg_Result tvg_paint_get_blend_method(const Tvg_Paint* paint, Tvg_Blend_Method* method) +{ + if (!paint || !method) return TVG_RESULT_INVALID_ARGUMENT; + *method = static_cast(reinterpret_cast(paint)->blend()); + return TVG_RESULT_SUCCESS; +} + + +TVG_API Tvg_Result tvg_paint_get_identifier(const Tvg_Paint* paint, Tvg_Identifier* identifier) +{ + if (!paint || !identifier) return TVG_RESULT_INVALID_ARGUMENT; + *identifier = static_cast(reinterpret_cast(paint)->identifier()); + return TVG_RESULT_SUCCESS; +} + +/************************************************************************/ +/* Shape API */ +/************************************************************************/ + +TVG_API Tvg_Paint* tvg_shape_new() +{ + return (Tvg_Paint*) Shape::gen().release(); +} + + +TVG_API Tvg_Result tvg_shape_reset(Tvg_Paint* paint) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->reset(); +} + + +TVG_API Tvg_Result tvg_shape_move_to(Tvg_Paint* paint, float x, float y) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->moveTo(x, y); +} + + +TVG_API Tvg_Result tvg_shape_line_to(Tvg_Paint* paint, float x, float y) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->lineTo(x, y); +} + + +TVG_API Tvg_Result tvg_shape_cubic_to(Tvg_Paint* paint, float cx1, float cy1, float cx2, float cy2, float x, float y) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->cubicTo(cx1, cy1, cx2, cy2, x, y); +} + + +TVG_API Tvg_Result tvg_shape_close(Tvg_Paint* paint) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->close(); +} + + +TVG_API Tvg_Result tvg_shape_append_rect(Tvg_Paint* paint, float x, float y, float w, float h, float rx, float ry) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->appendRect(x, y, w, h, rx, ry); +} + + +TVG_API Tvg_Result tvg_shape_append_arc(Tvg_Paint* paint, float cx, float cy, float radius, float startAngle, float sweep, uint8_t pie) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->appendArc(cx, cy, radius, startAngle, sweep, pie); +} + + +TVG_API Tvg_Result tvg_shape_append_circle(Tvg_Paint* paint, float cx, float cy, float rx, float ry) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->appendCircle(cx, cy, rx, ry); +} + + +TVG_API Tvg_Result tvg_shape_append_path(Tvg_Paint* paint, const Tvg_Path_Command* cmds, uint32_t cmdCnt, const Tvg_Point* pts, uint32_t ptsCnt) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->appendPath((const PathCommand*)cmds, cmdCnt, (const Point*)pts, ptsCnt); +} + + +TVG_API Tvg_Result tvg_shape_get_path_coords(const Tvg_Paint* paint, const Tvg_Point** pts, uint32_t* cnt) +{ + if (!paint || !pts || !cnt) return TVG_RESULT_INVALID_ARGUMENT; + *cnt = reinterpret_cast(paint)->pathCoords((const Point**)pts); + return TVG_RESULT_SUCCESS; +} + + +TVG_API Tvg_Result tvg_shape_get_path_commands(const Tvg_Paint* paint, const Tvg_Path_Command** cmds, uint32_t* cnt) +{ + if (!paint || !cmds || !cnt) return TVG_RESULT_INVALID_ARGUMENT; + *cnt = reinterpret_cast(paint)->pathCommands((const PathCommand**)cmds); + return TVG_RESULT_SUCCESS; +} + + +TVG_API Tvg_Result tvg_shape_set_stroke_width(Tvg_Paint* paint, float width) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->stroke(width); +} + + +TVG_API Tvg_Result tvg_shape_get_stroke_width(const Tvg_Paint* paint, float* width) +{ + if (!paint || !width) return TVG_RESULT_INVALID_ARGUMENT; + *width = reinterpret_cast(paint)->strokeWidth(); + return TVG_RESULT_SUCCESS; +} + + +TVG_API Tvg_Result tvg_shape_set_stroke_color(Tvg_Paint* paint, uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->stroke(r, g, b, a); +} + + +TVG_API Tvg_Result tvg_shape_get_stroke_color(const Tvg_Paint* paint, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->strokeColor(r, g, b, a); +} + + +TVG_API Tvg_Result tvg_shape_set_stroke_linear_gradient(Tvg_Paint* paint, Tvg_Gradient* gradient) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->stroke(unique_ptr((LinearGradient*)(gradient))); +} + + +TVG_API Tvg_Result tvg_shape_set_stroke_radial_gradient(Tvg_Paint* paint, Tvg_Gradient* gradient) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->stroke(unique_ptr((RadialGradient*)(gradient))); +} + + +TVG_API Tvg_Result tvg_shape_get_stroke_gradient(const Tvg_Paint* paint, Tvg_Gradient** gradient) +{ + if (!paint || !gradient) return TVG_RESULT_INVALID_ARGUMENT; + *gradient = (Tvg_Gradient*)(reinterpret_cast(paint)->strokeFill()); + return TVG_RESULT_SUCCESS; +} + + +TVG_API Tvg_Result tvg_shape_set_stroke_dash(Tvg_Paint* paint, const float* dashPattern, uint32_t cnt) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->stroke(dashPattern, cnt); +} + + +TVG_API Tvg_Result tvg_shape_get_stroke_dash(const Tvg_Paint* paint, const float** dashPattern, uint32_t* cnt) +{ + if (!paint || !cnt || !dashPattern) return TVG_RESULT_INVALID_ARGUMENT; + *cnt = reinterpret_cast(paint)->strokeDash(dashPattern); + return TVG_RESULT_SUCCESS; +} + + +TVG_API Tvg_Result tvg_shape_set_stroke_cap(Tvg_Paint* paint, Tvg_Stroke_Cap cap) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->stroke((StrokeCap)cap); +} + + +TVG_API Tvg_Result tvg_shape_get_stroke_cap(const Tvg_Paint* paint, Tvg_Stroke_Cap* cap) +{ + if (!paint || !cap) return TVG_RESULT_INVALID_ARGUMENT; + *cap = (Tvg_Stroke_Cap) reinterpret_cast(paint)->strokeCap(); + return TVG_RESULT_SUCCESS; +} + + +TVG_API Tvg_Result tvg_shape_set_stroke_join(Tvg_Paint* paint, Tvg_Stroke_Join join) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->stroke((StrokeJoin)join); +} + + +TVG_API Tvg_Result tvg_shape_get_stroke_join(const Tvg_Paint* paint, Tvg_Stroke_Join* join) +{ + if (!paint || !join) return TVG_RESULT_INVALID_ARGUMENT; + *join = (Tvg_Stroke_Join) reinterpret_cast(paint)->strokeJoin(); + return TVG_RESULT_SUCCESS; +} + + +TVG_API Tvg_Result tvg_shape_set_stroke_miterlimit(Tvg_Paint* paint, float ml) +{ + if (ml < 0.0f) return TVG_RESULT_NOT_SUPPORTED; + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->strokeMiterlimit(ml); +} + + +TVG_API Tvg_Result tvg_shape_get_stroke_miterlimit(const Tvg_Paint* paint, float* ml) +{ + if (!paint || !ml) return TVG_RESULT_INVALID_ARGUMENT; + *ml = reinterpret_cast(paint)->strokeMiterlimit(); + return TVG_RESULT_SUCCESS; +} + + +TVG_API Tvg_Result tvg_shape_set_fill_color(Tvg_Paint* paint, uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->fill(r, g, b, a); +} + + +TVG_API Tvg_Result tvg_shape_get_fill_color(const Tvg_Paint* paint, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->fillColor(r, g, b, a); +} + + +TVG_API Tvg_Result tvg_shape_set_fill_rule(Tvg_Paint* paint, Tvg_Fill_Rule rule) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->fill((FillRule)rule); +} + + +TVG_API Tvg_Result tvg_shape_get_fill_rule(const Tvg_Paint* paint, Tvg_Fill_Rule* rule) +{ + if (!paint || !rule) return TVG_RESULT_INVALID_ARGUMENT; + *rule = (Tvg_Fill_Rule) reinterpret_cast(paint)->fillRule(); + return TVG_RESULT_SUCCESS; +} + + +TVG_API Tvg_Result tvg_shape_set_paint_order(Tvg_Paint* paint, bool strokeFirst) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->order(strokeFirst); +} + + +TVG_API Tvg_Result tvg_shape_set_linear_gradient(Tvg_Paint* paint, Tvg_Gradient* gradient) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->fill(unique_ptr((LinearGradient*)(gradient))); +} + + +TVG_API Tvg_Result tvg_shape_set_radial_gradient(Tvg_Paint* paint, Tvg_Gradient* gradient) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->fill(unique_ptr((RadialGradient*)(gradient))); +} + + +TVG_API Tvg_Result tvg_shape_get_gradient(const Tvg_Paint* paint, Tvg_Gradient** gradient) +{ + if (!paint || !gradient) return TVG_RESULT_INVALID_ARGUMENT; + *gradient = (Tvg_Gradient*)(reinterpret_cast(paint)->fill()); + return TVG_RESULT_SUCCESS; +} + +/************************************************************************/ +/* Picture API */ +/************************************************************************/ + +TVG_API Tvg_Paint* tvg_picture_new() +{ + return (Tvg_Paint*) Picture::gen().release(); +} + + +TVG_API Tvg_Result tvg_picture_load(Tvg_Paint* paint, const char* path) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->load(path); +} + + +TVG_API Tvg_Result tvg_picture_load_raw(Tvg_Paint* paint, uint32_t *data, uint32_t w, uint32_t h, bool copy) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->load(data, w, h, copy); +} + + +TVG_API Tvg_Result tvg_picture_load_data(Tvg_Paint* paint, const char *data, uint32_t size, const char *mimetype, bool copy) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->load(data, size, mimetype ? mimetype : "", copy); +} + + +TVG_API Tvg_Result tvg_picture_set_size(Tvg_Paint* paint, float w, float h) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->size(w, h); +} + + +TVG_API Tvg_Result tvg_picture_get_size(const Tvg_Paint* paint, float* w, float* h) +{ + if (!paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(paint)->size(w, h); +} + + +/************************************************************************/ +/* Gradient API */ +/************************************************************************/ + +TVG_API Tvg_Gradient* tvg_linear_gradient_new() +{ + return (Tvg_Gradient*)LinearGradient::gen().release(); +} + + +TVG_API Tvg_Gradient* tvg_radial_gradient_new() +{ + return (Tvg_Gradient*)RadialGradient::gen().release(); +} + + +TVG_API Tvg_Gradient* tvg_gradient_duplicate(Tvg_Gradient* grad) +{ + if (!grad) return nullptr; + return (Tvg_Gradient*) reinterpret_cast(grad)->duplicate(); +} + + +TVG_API Tvg_Result tvg_gradient_del(Tvg_Gradient* grad) +{ + if (!grad) return TVG_RESULT_INVALID_ARGUMENT; + delete(reinterpret_cast(grad)); + return TVG_RESULT_SUCCESS; +} + + +TVG_API Tvg_Result tvg_linear_gradient_set(Tvg_Gradient* grad, float x1, float y1, float x2, float y2) +{ + if (!grad) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(grad)->linear(x1, y1, x2, y2); +} + + +TVG_API Tvg_Result tvg_linear_gradient_get(Tvg_Gradient* grad, float* x1, float* y1, float* x2, float* y2) +{ + if (!grad) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(grad)->linear(x1, y1, x2, y2); +} + + +TVG_API Tvg_Result tvg_radial_gradient_set(Tvg_Gradient* grad, float cx, float cy, float radius) +{ + if (!grad) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(grad)->radial(cx, cy, radius); +} + + +TVG_API Tvg_Result tvg_radial_gradient_get(Tvg_Gradient* grad, float* cx, float* cy, float* radius) +{ + if (!grad) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(grad)->radial(cx, cy, radius); +} + + +TVG_API Tvg_Result tvg_gradient_set_color_stops(Tvg_Gradient* grad, const Tvg_Color_Stop* color_stop, uint32_t cnt) +{ + if (!grad) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(grad)->colorStops(reinterpret_cast(color_stop), cnt); +} + + +TVG_API Tvg_Result tvg_gradient_get_color_stops(const Tvg_Gradient* grad, const Tvg_Color_Stop** color_stop, uint32_t* cnt) +{ + if (!grad || !color_stop || !cnt) return TVG_RESULT_INVALID_ARGUMENT; + *cnt = reinterpret_cast(grad)->colorStops(reinterpret_cast(color_stop)); + return TVG_RESULT_SUCCESS; +} + + +TVG_API Tvg_Result tvg_gradient_set_spread(Tvg_Gradient* grad, const Tvg_Stroke_Fill spread) +{ + if (!grad) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(grad)->spread((FillSpread)spread); +} + + +TVG_API Tvg_Result tvg_gradient_get_spread(const Tvg_Gradient* grad, Tvg_Stroke_Fill* spread) +{ + if (!grad || !spread) return TVG_RESULT_INVALID_ARGUMENT; + *spread = (Tvg_Stroke_Fill) reinterpret_cast(grad)->spread(); + return TVG_RESULT_SUCCESS; +} + + +TVG_API Tvg_Result tvg_gradient_set_transform(Tvg_Gradient* grad, const Tvg_Matrix* m) +{ + if (!grad || !m) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(grad)->transform(*(reinterpret_cast(m))); +} + + +TVG_API Tvg_Result tvg_gradient_get_transform(const Tvg_Gradient* grad, Tvg_Matrix* m) +{ + if (!grad || !m) return TVG_RESULT_INVALID_ARGUMENT; + *reinterpret_cast(m) = reinterpret_cast(const_cast(grad))->transform(); + return TVG_RESULT_SUCCESS; +} + + +TVG_API Tvg_Result tvg_gradient_get_identifier(const Tvg_Gradient* grad, Tvg_Identifier* identifier) +{ + if (!grad || !identifier) return TVG_RESULT_INVALID_ARGUMENT; + *identifier = static_cast(reinterpret_cast(grad)->identifier()); + return TVG_RESULT_SUCCESS; +} + +/************************************************************************/ +/* Scene API */ +/************************************************************************/ + +TVG_API Tvg_Paint* tvg_scene_new() +{ + return (Tvg_Paint*) Scene::gen().release(); +} + + +TVG_API Tvg_Result tvg_scene_reserve(Tvg_Paint* scene, uint32_t size) +{ + return TVG_RESULT_NOT_SUPPORTED; +} + + +TVG_API Tvg_Result tvg_scene_push(Tvg_Paint* scene, Tvg_Paint* paint) +{ + if (!scene || !paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(scene)->push(unique_ptr((Paint*)paint)); +} + + +TVG_API Tvg_Result tvg_scene_clear(Tvg_Paint* scene, bool free) +{ + if (!scene) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(scene)->clear(free); +} + + +/************************************************************************/ +/* Saver API */ +/************************************************************************/ + +TVG_API Tvg_Saver* tvg_saver_new() +{ + return (Tvg_Saver*) Saver::gen().release(); +} + + +TVG_API Tvg_Result tvg_saver_save(Tvg_Saver* saver, Tvg_Paint* paint, const char* path, bool compress) +{ + if (!saver || !paint || !path) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(saver)->save(unique_ptr((Paint*)paint), path, compress); +} + + +TVG_API Tvg_Result tvg_saver_sync(Tvg_Saver* saver) +{ + if (!saver) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(saver)->sync(); +} + + +TVG_API Tvg_Result tvg_saver_del(Tvg_Saver* saver) +{ + if (!saver) return TVG_RESULT_INVALID_ARGUMENT; + delete(reinterpret_cast(saver)); + return TVG_RESULT_SUCCESS; +} + + +/************************************************************************/ +/* Animation API */ +/************************************************************************/ + +TVG_API Tvg_Animation* tvg_animation_new() +{ + return (Tvg_Animation*) Animation::gen().release(); +} + + +TVG_API Tvg_Result tvg_animation_set_frame(Tvg_Animation* animation, float no) +{ + if (!animation) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(animation)->frame(no); +} + + +TVG_API Tvg_Result tvg_animation_get_frame(Tvg_Animation* animation, float* no) +{ + if (!animation || !no) return TVG_RESULT_INVALID_ARGUMENT; + *no = reinterpret_cast(animation)->curFrame(); + return TVG_RESULT_SUCCESS; +} + + +TVG_API Tvg_Result tvg_animation_get_total_frame(Tvg_Animation* animation, float* cnt) +{ + if (!animation || !cnt) return TVG_RESULT_INVALID_ARGUMENT; + *cnt = reinterpret_cast(animation)->totalFrame(); + return TVG_RESULT_SUCCESS; +} + + +TVG_API Tvg_Paint* tvg_animation_get_picture(Tvg_Animation* animation) +{ + if (!animation) return nullptr; + return (Tvg_Paint*) reinterpret_cast(animation)->picture(); +} + + +TVG_API Tvg_Result tvg_animation_get_duration(Tvg_Animation* animation, float* duration) +{ + if (!animation || !duration) return TVG_RESULT_INVALID_ARGUMENT; + *duration = reinterpret_cast(animation)->duration(); + return TVG_RESULT_SUCCESS; +} + + +TVG_API Tvg_Result tvg_animation_set_segment(Tvg_Animation* animation, float start, float end) +{ + if (!animation) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(animation)->segment(start, end); +} + + +TVG_API Tvg_Result tvg_animation_get_segment(Tvg_Animation* animation, float* start, float* end) +{ + if (!animation) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(animation)->segment(start, end); +} + + +TVG_API Tvg_Result tvg_animation_del(Tvg_Animation* animation) +{ + if (!animation) return TVG_RESULT_INVALID_ARGUMENT; + delete(reinterpret_cast(animation)); + return TVG_RESULT_SUCCESS; +} + + +/************************************************************************/ +/* Lottie Animation API */ +/************************************************************************/ + +TVG_API Tvg_Animation* tvg_lottie_animation_new() +{ +#ifdef THORVG_LOTTIE_LOADER_SUPPORT + return (Tvg_Animation*) LottieAnimation::gen().release(); +#endif + return nullptr; +} + + +TVG_API Tvg_Result tvg_lottie_animation_override(Tvg_Animation* animation, const char* slot) +{ +#ifdef THORVG_LOTTIE_LOADER_SUPPORT + if (!animation) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(animation)->override(slot); +#endif + return TVG_RESULT_NOT_SUPPORTED; +} + + +TVG_API Tvg_Result tvg_lottie_animation_set_marker(Tvg_Animation* animation, const char* marker) +{ +#ifdef THORVG_LOTTIE_LOADER_SUPPORT + if (!animation) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(animation)->segment(marker); +#endif + return TVG_RESULT_NOT_SUPPORTED; +} + + +TVG_API Tvg_Result tvg_lottie_animation_get_markers_cnt(Tvg_Animation* animation, uint32_t* cnt) +{ +#ifdef THORVG_LOTTIE_LOADER_SUPPORT + if (!animation || !cnt) return TVG_RESULT_INVALID_ARGUMENT; + *cnt = reinterpret_cast(animation)->markersCnt(); + return TVG_RESULT_SUCCESS; +#endif + return TVG_RESULT_NOT_SUPPORTED; +} + + +TVG_API Tvg_Result tvg_lottie_animation_get_marker(Tvg_Animation* animation, uint32_t idx, const char** name) +{ +#ifdef THORVG_LOTTIE_LOADER_SUPPORT + if (!animation || !name) return TVG_RESULT_INVALID_ARGUMENT; + *name = reinterpret_cast(animation)->marker(idx); + if (!(*name)) return TVG_RESULT_INVALID_ARGUMENT; + return TVG_RESULT_SUCCESS; +#endif + return TVG_RESULT_NOT_SUPPORTED; +} + +#ifdef __cplusplus +} +#endif + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgCommon.h b/include/liblvgl/libs/thorvg/tvgCommon.h new file mode 100644 index 00000000..d22c893f --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgCommon.h @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_COMMON_H_ +#define _TVG_COMMON_H_ + +#include "config.h" +#include "thorvg.h" + +using namespace std; +using namespace tvg; + +//for MSVC Compat +#ifdef _MSC_VER + #define TVG_UNUSED + #define strncasecmp _strnicmp + #define strcasecmp _stricmp +#else + #define TVG_UNUSED __attribute__ ((__unused__)) +#endif + +// Portable 'fallthrough' attribute +#if __has_cpp_attribute(fallthrough) + #ifdef _MSC_VER + #define TVG_FALLTHROUGH [[fallthrough]]; + #else + #define TVG_FALLTHROUGH __attribute__ ((fallthrough)); + #endif +#else + #define TVG_FALLTHROUGH +#endif + +#if defined(_MSC_VER) && defined(__clang__) + #define strncpy strncpy_s + #define strdup _strdup +#endif + +//TVG class identifier values +#define TVG_CLASS_ID_UNDEFINED 0 +#define TVG_CLASS_ID_SHAPE 1 +#define TVG_CLASS_ID_SCENE 2 +#define TVG_CLASS_ID_PICTURE 3 +#define TVG_CLASS_ID_LINEAR 4 +#define TVG_CLASS_ID_RADIAL 5 +#define TVG_CLASS_ID_TEXT 6 + +enum class FileType { Png = 0, Jpg, Webp, Tvg, Svg, Lottie, Ttf, Raw, Gif, Unknown }; + +using Size = Point; + +#ifdef THORVG_LOG_ENABLED + constexpr auto ErrorColor = "\033[31m"; //red + constexpr auto ErrorBgColor = "\033[41m";//bg red + constexpr auto LogColor = "\033[32m"; //green + constexpr auto LogBgColor = "\033[42m"; //bg green + constexpr auto GreyColor = "\033[90m"; //grey + constexpr auto ResetColors = "\033[0m"; //default + #define TVGERR(tag, fmt, ...) fprintf(stderr, "%s[E]%s %s" tag "%s (%s %d): %s" fmt "\n", ErrorBgColor, ResetColors, ErrorColor, GreyColor, __FILE__, __LINE__, ResetColors, ##__VA_ARGS__) + #define TVGLOG(tag, fmt, ...) fprintf(stdout, "%s[L]%s %s" tag "%s (%s %d): %s" fmt "\n", LogBgColor, ResetColors, LogColor, GreyColor, __FILE__, __LINE__, ResetColors, ##__VA_ARGS__) +#else + #define TVGERR(...) do {} while(0) + #define TVGLOG(...) do {} while(0) +#endif + +uint16_t THORVG_VERSION_NUMBER(); + + +#define P(A) ((A)->pImpl) //Access to pimpl. +#define PP(A) (((Paint*)(A))->pImpl) //Access to pimpl. + +#endif //_TVG_COMMON_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgCompressor.cpp b/include/liblvgl/libs/thorvg/tvgCompressor.cpp new file mode 100644 index 00000000..d0c7c4ec --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgCompressor.cpp @@ -0,0 +1,481 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +/* + * Lempel–Ziv–Welch (LZW) encoder/decoder by Guilherme R. Lampert(guilherme.ronaldo.lampert@gmail.com) + + * This is the compression scheme used by the GIF image format and the Unix 'compress' tool. + * Main differences from this implementation is that End Of Input (EOI) and Clear Codes (CC) + * are not stored in the output and the max code length in bits is 12, vs 16 in compress. + * + * EOI is simply detected by the end of the data stream, while CC happens if the + * dictionary gets filled. Data is written/read from bit streams, which handle + * byte-alignment for us in a transparent way. + + * The decoder relies on the hardcoded data layout produced by the encoder, since + * no additional reconstruction data is added to the output, so they must match. + * The nice thing about LZW is that we can reconstruct the dictionary directly from + * the stream of codes generated by the encoder, so this avoids storing additional + * headers in the bit stream. + + * The output code length is variable. It starts with the minimum number of bits + * required to store the base byte-sized dictionary and automatically increases + * as the dictionary gets larger (it starts at 9-bits and grows to 10-bits when + * code 512 is added, then 11-bits when 1024 is added, and so on). If the dictionary + * is filled (4096 items for a 12-bits dictionary), the whole thing is cleared and + * the process starts over. This is the main reason why the encoder and the decoder + * must match perfectly, since the lengths of the codes will not be specified with + * the data itself. + + * USEFUL LINKS: + * https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch + * http://rosettacode.org/wiki/LZW_compression + * http://www.cs.duke.edu/csed/curious/compression/lzw.html + * http://www.cs.cf.ac.uk/Dave/Multimedia/node214.html + * http://marknelson.us/1989/10/01/lzw-data-compression/ + */ +#include "config.h" + + + +#include +#include +#include "tvgCompressor.h" + +namespace tvg { + + +/************************************************************************/ +/* LZW Implementation */ +/************************************************************************/ + + +//LZW Dictionary helper: +constexpr int Nil = -1; +constexpr int MaxDictBits = 12; +constexpr int StartBits = 9; +constexpr int FirstCode = (1 << (StartBits - 1)); // 256 +constexpr int MaxDictEntries = (1 << MaxDictBits); // 4096 + + +//Round up to the next power-of-two number, e.g. 37 => 64 +static int nextPowerOfTwo(int num) +{ + --num; + for (size_t i = 1; i < sizeof(num) * 8; i <<= 1) { + num = num | num >> i; + } + return ++num; +} + + +struct BitStreamWriter +{ + uint8_t* stream; //Growable buffer to store our bits. Heap allocated & owned by the class instance. + int bytesAllocated; //Current size of heap-allocated stream buffer *in bytes*. + int granularity; //Amount bytesAllocated multiplies by when auto-resizing in appendBit(). + int currBytePos; //Current byte being written to, from 0 to bytesAllocated-1. + int nextBitPos; //Bit position within the current byte to access next. 0 to 7. + int numBitsWritten; //Number of bits in use from the stream buffer, not including byte-rounding padding. + + void internalInit() + { + stream = nullptr; + bytesAllocated = 0; + granularity = 2; + currBytePos = 0; + nextBitPos = 0; + numBitsWritten = 0; + } + + uint8_t* allocBytes(const int bytesWanted, uint8_t * oldPtr, const int oldSize) + { + auto newMemory = static_cast(malloc(bytesWanted)); + memset(newMemory, 0, bytesWanted); + + if (oldPtr) { + memcpy(newMemory, oldPtr, oldSize); + free(oldPtr); + } + return newMemory; + } + + BitStreamWriter() + { + /* 8192 bits for a start (1024 bytes). It will resize if needed. + Default granularity is 2. */ + internalInit(); + allocate(8192); + } + + BitStreamWriter(const int initialSizeInBits, const int growthGranularity = 2) + { + internalInit(); + setGranularity(growthGranularity); + allocate(initialSizeInBits); + } + + ~BitStreamWriter() + { + free(stream); + } + + void allocate(int bitsWanted) + { + //Require at least a byte. + if (bitsWanted <= 0) bitsWanted = 8; + + //Round upwards if needed: + if ((bitsWanted % 8) != 0) bitsWanted = nextPowerOfTwo(bitsWanted); + + //We might already have the required count. + const int sizeInBytes = bitsWanted / 8; + if (sizeInBytes <= bytesAllocated) return; + + stream = allocBytes(sizeInBytes, stream, bytesAllocated); + bytesAllocated = sizeInBytes; + } + + void appendBit(const int bit) + { + const uint32_t mask = uint32_t(1) << nextBitPos; + stream[currBytePos] = (stream[currBytePos] & ~mask) | (-bit & mask); + ++numBitsWritten; + + if (++nextBitPos == 8) { + nextBitPos = 0; + if (++currBytePos == bytesAllocated) allocate(bytesAllocated * granularity * 8); + } + } + + void appendBitsU64(const uint64_t num, const int bitCount) + { + for (int b = 0; b < bitCount; ++b) { + const uint64_t mask = uint64_t(1) << b; + const int bit = !!(num & mask); + appendBit(bit); + } + } + + uint8_t* release() + { + auto oldPtr = stream; + internalInit(); + return oldPtr; + } + + void setGranularity(const int growthGranularity) + { + granularity = (growthGranularity >= 2) ? growthGranularity : 2; + } + + int getByteCount() const + { + int usedBytes = numBitsWritten / 8; + int leftovers = numBitsWritten % 8; + if (leftovers != 0) ++usedBytes; + return usedBytes; + } +}; + + +struct BitStreamReader +{ + const uint8_t* stream; // Pointer to the external bit stream. Not owned by the reader. + const int sizeInBytes; // Size of the stream *in bytes*. Might include padding. + const int sizeInBits; // Size of the stream *in bits*, padding *not* include. + int currBytePos = 0; // Current byte being read in the stream. + int nextBitPos = 0; // Bit position within the current byte to access next. 0 to 7. + int numBitsRead = 0; // Total bits read from the stream so far. Never includes byte-rounding padding. + + BitStreamReader(const uint8_t* bitStream, const int byteCount, const int bitCount) : stream(bitStream), sizeInBytes(byteCount), sizeInBits(bitCount) + { + } + + bool readNextBit(int& bitOut) + { + if (numBitsRead >= sizeInBits) return false; //We are done. + + const uint32_t mask = uint32_t(1) << nextBitPos; + bitOut = !!(stream[currBytePos] & mask); + ++numBitsRead; + + if (++nextBitPos == 8) { + nextBitPos = 0; + ++currBytePos; + } + return true; + } + + uint64_t readBitsU64(const int bitCount) + { + uint64_t num = 0; + for (int b = 0; b < bitCount; ++b) { + int bit; + if (!readNextBit(bit)) break; + /* Based on a "Stanford bit-hack": + http://graphics.stanford.edu/~seander/bithacks.html#ConditionalSetOrClearBitsWithoutBranching */ + const uint64_t mask = uint64_t(1) << b; + num = (num & ~mask) | (-bit & mask); + } + return num; + } + + bool isEndOfStream() const + { + return numBitsRead >= sizeInBits; + } +}; + + +struct Dictionary +{ + struct Entry + { + int code; + int value; + }; + + //Dictionary entries 0-255 are always reserved to the byte/ASCII range. + int size; + Entry entries[MaxDictEntries]; + + Dictionary() + { + /* First 256 dictionary entries are reserved to the byte/ASCII range. + Additional entries follow for the character sequences found in the input. + Up to 4096 - 256 (MaxDictEntries - FirstCode). */ + size = FirstCode; + + for (int i = 0; i < size; ++i) { + entries[i].code = Nil; + entries[i].value = i; + } + } + + int findIndex(const int code, const int value) const + { + if (code == Nil) return value; + + //Linear search for now. + //TODO: Worth optimizing with a proper hash-table? + for (int i = 0; i < size; ++i) { + if (entries[i].code == code && entries[i].value == value) return i; + } + return Nil; + } + + bool add(const int code, const int value) + { + if (size == MaxDictEntries) return false; + entries[size].code = code; + entries[size].value = value; + ++size; + return true; + } + + bool flush(int & codeBitsWidth) + { + if (size == (1 << codeBitsWidth)) { + ++codeBitsWidth; + if (codeBitsWidth > MaxDictBits) { + //Clear the dictionary (except the first 256 byte entries). + codeBitsWidth = StartBits; + size = FirstCode; + return true; + } + } + return false; + } +}; + + +static bool outputByte(int code, uint8_t*& output, int outputSizeBytes, int& bytesDecodedSoFar) +{ + if (bytesDecodedSoFar >= outputSizeBytes) return false; + *output++ = static_cast(code); + ++bytesDecodedSoFar; + return true; +} + + +static bool outputSequence(const Dictionary& dict, int code, uint8_t*& output, int outputSizeBytes, int& bytesDecodedSoFar, int& firstByte) +{ + /* A sequence is stored backwards, so we have to write + it to a temp then output the buffer in reverse. */ + int i = 0; + uint8_t sequence[MaxDictEntries]; + + do { + sequence[i++] = dict.entries[code].value; + code = dict.entries[code].code; + } while (code >= 0); + + firstByte = sequence[--i]; + + for (; i >= 0; --i) { + if (!outputByte(sequence[i], output, outputSizeBytes, bytesDecodedSoFar)) return false; + } + return true; +} + + +uint8_t* lzwDecode(const uint8_t* compressed, uint32_t compressedSizeBytes, uint32_t compressedSizeBits, uint32_t uncompressedSizeBytes) +{ + int code = Nil; + int prevCode = Nil; + int firstByte = 0; + int bytesDecoded = 0; + int codeBitsWidth = StartBits; + auto uncompressed = (uint8_t*) malloc(sizeof(uint8_t) * uncompressedSizeBytes); + auto ptr = uncompressed; + + /* We'll reconstruct the dictionary based on the bit stream codes. + Unlike Huffman encoding, we don't store the dictionary as a prefix to the data. */ + Dictionary dictionary; + BitStreamReader bitStream(compressed, compressedSizeBytes, compressedSizeBits); + + /* We check to avoid an overflow of the user buffer. + If the buffer is smaller than the decompressed size, we break the loop and return the current decompression count. */ + while (!bitStream.isEndOfStream()) { + code = static_cast(bitStream.readBitsU64(codeBitsWidth)); + + if (prevCode == Nil) { + if (!outputByte(code, ptr, uncompressedSizeBytes, bytesDecoded)) break; + firstByte = code; + prevCode = code; + continue; + } + if (code >= dictionary.size) { + if (!outputSequence(dictionary, prevCode, ptr, uncompressedSizeBytes, bytesDecoded, firstByte)) break; + if (!outputByte(firstByte, ptr, uncompressedSizeBytes, bytesDecoded)) break; + } else if (!outputSequence(dictionary, code, ptr, uncompressedSizeBytes, bytesDecoded, firstByte)) break; + + dictionary.add(prevCode, firstByte); + if (dictionary.flush(codeBitsWidth)) prevCode = Nil; + else prevCode = code; + } + + return uncompressed; +} + + +uint8_t* lzwEncode(const uint8_t* uncompressed, uint32_t uncompressedSizeBytes, uint32_t* compressedSizeBytes, uint32_t* compressedSizeBits) +{ + //LZW encoding context: + int code = Nil; + int codeBitsWidth = StartBits; + Dictionary dictionary; + + //Output bit stream we write to. This will allocate memory as needed to accommodate the encoded data. + BitStreamWriter bitStream; + + for (; uncompressedSizeBytes > 0; --uncompressedSizeBytes, ++uncompressed) { + const int value = *uncompressed; + const int index = dictionary.findIndex(code, value); + + if (index != Nil) { + code = index; + continue; + } + + //Write the dictionary code using the minimum bit-with: + bitStream.appendBitsU64(code, codeBitsWidth); + + //Flush it when full so we can restart the sequences. + if (!dictionary.flush(codeBitsWidth)) { + //There's still space for this sequence. + dictionary.add(code, value); + } + code = value; + } + + //Residual code at the end: + if (code != Nil) bitStream.appendBitsU64(code, codeBitsWidth); + + //Pass ownership of the compressed data buffer to the user pointer: + *compressedSizeBytes = bitStream.getByteCount(); + *compressedSizeBits = bitStream.numBitsWritten; + + return bitStream.release(); +} + + +/************************************************************************/ +/* B64 Implementation */ +/************************************************************************/ + + +size_t b64Decode(const char* encoded, const size_t len, char** decoded) +{ + static constexpr const char B64_INDEX[256] = + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 62, 63, 62, 62, 63, 52, 53, 54, 55, 56, 57, + 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, + 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 0, 0, 0, 0, 63, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, + 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 + }; + + + if (!decoded || !encoded || len == 0) return 0; + + auto reserved = 3 * (1 + (len >> 2)) + 1; + auto output = static_cast(malloc(reserved * sizeof(char))); + if (!output) return 0; + output[reserved - 1] = '\0'; + + size_t idx = 0; + + while (*encoded && *(encoded + 1)) { + if (*encoded <= 0x20) { + ++encoded; + continue; + } + + auto value1 = B64_INDEX[(size_t)encoded[0]]; + auto value2 = B64_INDEX[(size_t)encoded[1]]; + output[idx++] = (value1 << 2) + ((value2 & 0x30) >> 4); + + if (!encoded[2] || encoded[3] < 0 || encoded[2] == '=' || encoded[2] == '.') break; + auto value3 = B64_INDEX[(size_t)encoded[2]]; + output[idx++] = ((value2 & 0x0f) << 4) + ((value3 & 0x3c) >> 2); + + if (!encoded[3] || encoded[3] < 0 || encoded[3] == '=' || encoded[3] == '.') break; + auto value4 = B64_INDEX[(size_t)encoded[3]]; + output[idx++] = ((value3 & 0x03) << 6) + value4; + encoded += 4; + } + *decoded = output; + return reserved; +} + + +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgCompressor.h b/include/liblvgl/libs/thorvg/tvgCompressor.h new file mode 100644 index 00000000..11e5fb39 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgCompressor.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_COMPRESSOR_H_ +#define _TVG_COMPRESSOR_H_ + +#include + +namespace tvg +{ + uint8_t* lzwEncode(const uint8_t* uncompressed, uint32_t uncompressedSizeBytes, uint32_t* compressedSizeBytes, uint32_t* compressedSizeBits); + uint8_t* lzwDecode(const uint8_t* compressed, uint32_t compressedSizeBytes, uint32_t compressedSizeBits, uint32_t uncompressedSizeBytes); + size_t b64Decode(const char* encoded, const size_t len, char** decoded); +} + +#endif //_TVG_COMPRESSOR_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgFill.cpp b/include/liblvgl/libs/thorvg/tvgFill.cpp new file mode 100644 index 00000000..76d678fe --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgFill.cpp @@ -0,0 +1,256 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgFill.h" + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +Fill* RadialGradient::Impl::duplicate() +{ + auto ret = RadialGradient::gen(); + if (!ret) return nullptr; + + ret->pImpl->cx = cx; + ret->pImpl->cy = cy; + ret->pImpl->r = r; + ret->pImpl->fx = fx; + ret->pImpl->fy = fy; + ret->pImpl->fr = fr; + + return ret.release(); +} + + +Result RadialGradient::Impl::radial(float cx, float cy, float r, float fx, float fy, float fr) +{ + if (r < 0 || fr < 0) return Result::InvalidArguments; + + this->cx = cx; + this->cy = cy; + this->r = r; + this->fx = fx; + this->fy = fy; + this->fr = fr; + + return Result::Success; +}; + + +Fill* LinearGradient::Impl::duplicate() +{ + auto ret = LinearGradient::gen(); + if (!ret) return nullptr; + + ret->pImpl->x1 = x1; + ret->pImpl->y1 = y1; + ret->pImpl->x2 = x2; + ret->pImpl->y2 = y2; + + return ret.release(); +}; + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +Fill::Fill():pImpl(new Impl()) +{ +} + + +Fill::~Fill() +{ + delete(pImpl); +} + + +Result Fill::colorStops(const ColorStop* colorStops, uint32_t cnt) noexcept +{ + if ((!colorStops && cnt > 0) || (colorStops && cnt == 0)) return Result::InvalidArguments; + + if (cnt == 0) { + if (pImpl->colorStops) { + free(pImpl->colorStops); + pImpl->colorStops = nullptr; + pImpl->cnt = 0; + } + return Result::Success; + } + + if (pImpl->cnt != cnt) { + pImpl->colorStops = static_cast(realloc(pImpl->colorStops, cnt * sizeof(ColorStop))); + } + + pImpl->cnt = cnt; + memcpy(pImpl->colorStops, colorStops, cnt * sizeof(ColorStop)); + + return Result::Success; +} + + +uint32_t Fill::colorStops(const ColorStop** colorStops) const noexcept +{ + if (colorStops) *colorStops = pImpl->colorStops; + + return pImpl->cnt; +} + + +Result Fill::spread(FillSpread s) noexcept +{ + pImpl->spread = s; + + return Result::Success; +} + + +FillSpread Fill::spread() const noexcept +{ + return pImpl->spread; +} + + +Result Fill::transform(const Matrix& m) noexcept +{ + if (!pImpl->transform) { + pImpl->transform = static_cast(malloc(sizeof(Matrix))); + } + *pImpl->transform = m; + return Result::Success; +} + + +Matrix Fill::transform() const noexcept +{ + if (pImpl->transform) return *pImpl->transform; + return {1, 0, 0, 0, 1, 0, 0, 0, 1}; +} + + +Fill* Fill::duplicate() const noexcept +{ + return pImpl->duplicate(); +} + + +uint32_t Fill::identifier() const noexcept +{ + return pImpl->id; +} + + +RadialGradient::RadialGradient():pImpl(new Impl()) +{ + Fill::pImpl->id = TVG_CLASS_ID_RADIAL; + Fill::pImpl->method(new FillDup(pImpl)); +} + + +RadialGradient::~RadialGradient() +{ + delete(pImpl); +} + + +Result RadialGradient::radial(float cx, float cy, float r) noexcept +{ + return pImpl->radial(cx, cy, r, cx, cy, 0.0f); +} + + +Result RadialGradient::radial(float* cx, float* cy, float* r) const noexcept +{ + if (cx) *cx = pImpl->cx; + if (cy) *cy = pImpl->cy; + if (r) *r = pImpl->r; + + return Result::Success; +} + + +unique_ptr RadialGradient::gen() noexcept +{ + return unique_ptr(new RadialGradient); +} + + +uint32_t RadialGradient::identifier() noexcept +{ + return TVG_CLASS_ID_RADIAL; +} + + +LinearGradient::LinearGradient():pImpl(new Impl()) +{ + Fill::pImpl->id = TVG_CLASS_ID_LINEAR; + Fill::pImpl->method(new FillDup(pImpl)); +} + + +LinearGradient::~LinearGradient() +{ + delete(pImpl); +} + + +Result LinearGradient::linear(float x1, float y1, float x2, float y2) noexcept +{ + pImpl->x1 = x1; + pImpl->y1 = y1; + pImpl->x2 = x2; + pImpl->y2 = y2; + + return Result::Success; +} + + +Result LinearGradient::linear(float* x1, float* y1, float* x2, float* y2) const noexcept +{ + if (x1) *x1 = pImpl->x1; + if (x2) *x2 = pImpl->x2; + if (y1) *y1 = pImpl->y1; + if (y2) *y2 = pImpl->y2; + + return Result::Success; +} + + +unique_ptr LinearGradient::gen() noexcept +{ + return unique_ptr(new LinearGradient); +} + + +uint32_t LinearGradient::identifier() noexcept +{ + return TVG_CLASS_ID_LINEAR; +} + + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgFill.h b/include/liblvgl/libs/thorvg/tvgFill.h new file mode 100644 index 00000000..e6d776f9 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgFill.h @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_FILL_H_ +#define _TVG_FILL_H_ + +#include +#include +#include "tvgCommon.h" + +template +struct DuplicateMethod +{ + virtual ~DuplicateMethod() {} + virtual T* duplicate() = 0; +}; + +template +struct FillDup : DuplicateMethod +{ + T* inst = nullptr; + + FillDup(T* _inst) : inst(_inst) {} + ~FillDup() {} + + Fill* duplicate() override + { + return inst->duplicate(); + } +}; + +struct Fill::Impl +{ + ColorStop* colorStops = nullptr; + Matrix* transform = nullptr; + uint32_t cnt = 0; + FillSpread spread; + DuplicateMethod* dup = nullptr; + uint8_t id; + + ~Impl() + { + delete(dup); + free(colorStops); + free(transform); + } + + void method(DuplicateMethod* dup) + { + this->dup = dup; + } + + Fill* duplicate() + { + auto ret = dup->duplicate(); + if (!ret) return nullptr; + + ret->pImpl->cnt = cnt; + ret->pImpl->spread = spread; + ret->pImpl->colorStops = static_cast(malloc(sizeof(ColorStop) * cnt)); + memcpy(ret->pImpl->colorStops, colorStops, sizeof(ColorStop) * cnt); + if (transform) { + ret->pImpl->transform = static_cast(malloc(sizeof(Matrix))); + *ret->pImpl->transform = *transform; + } + return ret; + } +}; + + +struct RadialGradient::Impl +{ + float cx = 0.0f, cy = 0.0f; + float fx = 0.0f, fy = 0.0f; + float r = 0.0f, fr = 0.0f; + + Fill* duplicate(); + Result radial(float cx, float cy, float r, float fx, float fy, float fr); +}; + + +struct LinearGradient::Impl +{ + float x1 = 0.0f; + float y1 = 0.0f; + float x2 = 0.0f; + float y2 = 0.0f; + + Fill* duplicate(); +}; + + +#endif //_TVG_FILL_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgFrameModule.h b/include/liblvgl/libs/thorvg/tvgFrameModule.h new file mode 100644 index 00000000..02e69a87 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgFrameModule.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2023 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_FRAME_MODULE_H_ +#define _TVG_FRAME_MODULE_H_ + +#include "tvgLoadModule.h" + +namespace tvg +{ + +class FrameModule: public ImageLoader +{ +public: + float segmentBegin = 0.0f; + float segmentEnd = 1.0f; + + FrameModule(FileType type) : ImageLoader(type) {} + virtual ~FrameModule() {} + + virtual bool frame(float no) = 0; //set the current frame number + virtual float totalFrame() = 0; //return the total frame count + virtual float curFrame() = 0; //return the current frame number + virtual float duration() = 0; //return the animation duration in seconds + + void segment(float* begin, float* end) + { + if (begin) *begin = segmentBegin; + if (end) *end = segmentEnd; + } + + void segment(float begin, float end) + { + segmentBegin = begin; + segmentEnd = end; + } + + virtual bool animatable() override { return true; } +}; + +} + +#endif //_TVG_FRAME_MODULE_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgGlCanvas.cpp b/include/liblvgl/libs/thorvg/tvgGlCanvas.cpp new file mode 100644 index 00000000..d246ea55 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgGlCanvas.cpp @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgCanvas.h" + +#ifdef THORVG_GL_RASTER_SUPPORT + #include "tvgGlRenderer.h" +#else + class GlRenderer : public RenderMethod + { + //Non Supported. Dummy Class */ + }; +#endif + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +struct GlCanvas::Impl +{ +}; + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +#ifdef THORVG_GL_RASTER_SUPPORT +GlCanvas::GlCanvas() : Canvas(GlRenderer::gen()), pImpl(new Impl) +#else +GlCanvas::GlCanvas() : Canvas(nullptr), pImpl(new Impl) +#endif +{ +} + + + +GlCanvas::~GlCanvas() +{ + delete(pImpl); +} + + +Result GlCanvas::target(int32_t id, uint32_t w, uint32_t h) noexcept +{ +#ifdef THORVG_GL_RASTER_SUPPORT + //We know renderer type, avoid dynamic_cast for performance. + auto renderer = static_cast(Canvas::pImpl->renderer); + if (!renderer) return Result::MemoryCorruption; + + if (!renderer->target(id, w, h)) return Result::Unknown; + Canvas::pImpl->vport = {0, 0, (int32_t)w, (int32_t)h}; + renderer->viewport(Canvas::pImpl->vport); + + //Paints must be updated again with this new target. + Canvas::pImpl->needRefresh(); + + return Result::Success; +#endif + return Result::NonSupport; +} + + +unique_ptr GlCanvas::gen() noexcept +{ +#ifdef THORVG_GL_RASTER_SUPPORT + if (GlRenderer::init() <= 0) return nullptr; + return unique_ptr(new GlCanvas); +#endif + return nullptr; +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgInitializer.cpp b/include/liblvgl/libs/thorvg/tvgInitializer.cpp new file mode 100644 index 00000000..2506fd2b --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgInitializer.cpp @@ -0,0 +1,185 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgCommon.h" +#include "tvgTaskScheduler.h" +#include "tvgLoader.h" + +#ifdef _WIN32 + #include +#endif + +#ifdef THORVG_SW_RASTER_SUPPORT + #include "tvgSwRenderer.h" +#endif + +#ifdef THORVG_GL_RASTER_SUPPORT + #include "tvgGlRenderer.h" +#endif + +#ifdef THORVG_WG_RASTER_SUPPORT + #include "tvgWgRenderer.h" +#endif + + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +static int _initCnt = 0; +static uint16_t _version = 0; + +//enum class operation helper +static constexpr bool operator &(CanvasEngine a, CanvasEngine b) +{ + return int(a) & int(b); +} + +static bool _buildVersionInfo() +{ + auto SRC = THORVG_VERSION_STRING; //ex) 0.3.99 + auto p = SRC; + const char* x; + + char major[3]; + x = strchr(p, '.'); + if (!x) return false; + memcpy(major, p, x - p); + major[x - p] = '\0'; + p = x + 1; + + char minor[3]; + x = strchr(p, '.'); + if (!x) return false; + memcpy(minor, p, x - p); + minor[x - p] = '\0'; + p = x + 1; + + char micro[3]; + x = SRC + strlen(THORVG_VERSION_STRING); + memcpy(micro, p, x - p); + micro[x - p] = '\0'; + + char sum[7]; + snprintf(sum, sizeof(sum), "%s%s%s", major, minor, micro); + + _version = atoi(sum); + + return true; +} + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +Result Initializer::init(CanvasEngine engine, uint32_t threads) noexcept +{ + auto nonSupport = true; + if (static_cast(engine) == 0) return Result::InvalidArguments; + + if (engine & CanvasEngine::Sw) { + #ifdef THORVG_SW_RASTER_SUPPORT + if (!SwRenderer::init(threads)) return Result::FailedAllocation; + nonSupport = false; + #endif + } + + if (engine & CanvasEngine::Gl) { + #ifdef THORVG_GL_RASTER_SUPPORT + if (!GlRenderer::init(threads)) return Result::FailedAllocation; + nonSupport = false; + #endif + } + + if (engine & CanvasEngine::Wg) { + #ifdef THORVG_WG_RASTER_SUPPORT + if (!WgRenderer::init(threads)) return Result::FailedAllocation; + nonSupport = false; + #endif + } + + if (nonSupport) return Result::NonSupport; + + if (_initCnt++ > 0) return Result::Success; + + if (!_buildVersionInfo()) return Result::Unknown; + + if (!LoaderMgr::init()) return Result::Unknown; + + TaskScheduler::init(threads); + + return Result::Success; +} + + +Result Initializer::term(CanvasEngine engine) noexcept +{ + if (_initCnt == 0) return Result::InsufficientCondition; + + auto nonSupport = true; + if (static_cast(engine) == 0) return Result::InvalidArguments; + + if (engine & CanvasEngine::Sw) { + #ifdef THORVG_SW_RASTER_SUPPORT + if (!SwRenderer::term()) return Result::InsufficientCondition; + nonSupport = false; + #endif + } + + if (engine & CanvasEngine::Gl) { + #ifdef THORVG_GL_RASTER_SUPPORT + if (!GlRenderer::term()) return Result::InsufficientCondition; + nonSupport = false; + #endif + } + + if (engine & CanvasEngine::Wg) { + #ifdef THORVG_WG_RASTER_SUPPORT + if (!WgRenderer::term()) return Result::InsufficientCondition; + nonSupport = false; + #endif + } + + if (nonSupport) return Result::NonSupport; + + if (--_initCnt > 0) return Result::Success; + + TaskScheduler::term(); + + if (!LoaderMgr::term()) return Result::Unknown; + + return Result::Success; +} + + +uint16_t THORVG_VERSION_NUMBER() +{ + return _version; +} + + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgInlist.h b/include/liblvgl/libs/thorvg/tvgInlist.h new file mode 100644 index 00000000..eaedecce --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgInlist.h @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2023 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_INLIST_H_ +#define _TVG_INLIST_H_ + +namespace tvg { + +//NOTE: declare this in your list item +#define INLIST_ITEM(T) \ + T* prev; \ + T* next + +template +struct Inlist +{ + T* head = nullptr; + T* tail = nullptr; + + void free() + { + while (head) { + auto t = head; + head = t->next; + delete(t); + } + head = tail = nullptr; + } + + void back(T* element) + { + if (tail) { + tail->next = element; + element->prev = tail; + element->next = nullptr; + tail = element; + } else { + head = tail = element; + element->prev = nullptr; + element->next = nullptr; + } + } + + void front(T* element) + { + if (head) { + head->prev = element; + element->prev = nullptr; + element->next = head; + head = element; + } else { + head = tail = element; + element->prev = nullptr; + element->next = nullptr; + } + } + + T* back() + { + if (!tail) return nullptr; + auto t = tail; + tail = t->prev; + if (!tail) head = nullptr; + return t; + } + + T* front() + { + if (!head) return nullptr; + auto t = head; + head = t->next; + if (!head) tail = nullptr; + return t; + } + + void remove(T* element) + { + if (element->prev) element->prev->next = element->next; + if (element->next) element->next->prev = element->prev; + if (element == head) head = element->next; + if (element == tail) tail = element->prev; + } + + bool empty() + { + return head ? false : true; + } +}; + +} + +#endif // _TVG_INLIST_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgIteratorAccessor.h b/include/liblvgl/libs/thorvg/tvgIteratorAccessor.h new file mode 100644 index 00000000..adc5f6ea --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgIteratorAccessor.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2021 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_ITERATOR_ACCESSOR_H_ +#define _TVG_ITERATOR_ACCESSOR_H_ + +#include "tvgPaint.h" + +namespace tvg +{ + +class IteratorAccessor +{ +public: + //Utility Method: Iterator Accessor + static Iterator* iterator(const Paint* paint) + { + return paint->pImpl->iterator(); + } +}; + +} + +#endif //_TVG_ITERATOR_ACCESSOR_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgLines.cpp b/include/liblvgl/libs/thorvg/tvgLines.cpp new file mode 100644 index 00000000..3bdab769 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgLines.cpp @@ -0,0 +1,249 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgMath.h" +#include "tvgLines.h" + +#define BEZIER_EPSILON 1e-2f + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +static float _lineLengthApprox(const Point& pt1, const Point& pt2) +{ + /* approximate sqrt(x*x + y*y) using alpha max plus beta min algorithm. + With alpha = 1, beta = 3/8, giving results with the largest error less + than 7% compared to the exact value. */ + Point diff = {pt2.x - pt1.x, pt2.y - pt1.y}; + if (diff.x < 0) diff.x = -diff.x; + if (diff.y < 0) diff.y = -diff.y; + return (diff.x > diff.y) ? (diff.x + diff.y * 0.375f) : (diff.y + diff.x * 0.375f); +} + + +static float _lineLength(const Point& pt1, const Point& pt2) +{ + Point diff = {pt2.x - pt1.x, pt2.y - pt1.y}; + return sqrtf(diff.x * diff.x + diff.y * diff.y); +} + + +template +float _bezLength(const Bezier& cur, LengthFunc lineLengthFunc) +{ + Bezier left, right; + auto len = lineLengthFunc(cur.start, cur.ctrl1) + lineLengthFunc(cur.ctrl1, cur.ctrl2) + lineLengthFunc(cur.ctrl2, cur.end); + auto chord = lineLengthFunc(cur.start, cur.end); + + if (fabsf(len - chord) > BEZIER_EPSILON) { + tvg::bezSplit(cur, left, right); + return _bezLength(left, lineLengthFunc) + _bezLength(right, lineLengthFunc); + } + return len; +} + + +template +float _bezAt(const Bezier& bz, float at, float length, LengthFunc lineLengthFunc) +{ + auto biggest = 1.0f; + auto smallest = 0.0f; + auto t = 0.5f; + + //just in case to prevent an infinite loop + if (at <= 0) return 0.0f; + if (at >= length) return 1.0f; + + while (true) { + auto right = bz; + Bezier left; + bezSplitLeft(right, t, left); + length = _bezLength(left, lineLengthFunc); + if (fabsf(length - at) < BEZIER_EPSILON || fabsf(smallest - biggest) < BEZIER_EPSILON) { + break; + } + if (length < at) { + smallest = t; + t = (t + biggest) * 0.5f; + } else { + biggest = t; + t = (smallest + t) * 0.5f; + } + } + return t; +} + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +namespace tvg +{ + +float lineLength(const Point& pt1, const Point& pt2) +{ + return _lineLength(pt1, pt2); +} + + +void lineSplitAt(const Line& cur, float at, Line& left, Line& right) +{ + auto len = lineLength(cur.pt1, cur.pt2); + auto dx = ((cur.pt2.x - cur.pt1.x) / len) * at; + auto dy = ((cur.pt2.y - cur.pt1.y) / len) * at; + left.pt1 = cur.pt1; + left.pt2.x = left.pt1.x + dx; + left.pt2.y = left.pt1.y + dy; + right.pt1 = left.pt2; + right.pt2 = cur.pt2; +} + + +void bezSplit(const Bezier& cur, Bezier& left, Bezier& right) +{ + auto c = (cur.ctrl1.x + cur.ctrl2.x) * 0.5f; + left.ctrl1.x = (cur.start.x + cur.ctrl1.x) * 0.5f; + right.ctrl2.x = (cur.ctrl2.x + cur.end.x) * 0.5f; + left.start.x = cur.start.x; + right.end.x = cur.end.x; + left.ctrl2.x = (left.ctrl1.x + c) * 0.5f; + right.ctrl1.x = (right.ctrl2.x + c) * 0.5f; + left.end.x = right.start.x = (left.ctrl2.x + right.ctrl1.x) * 0.5f; + + c = (cur.ctrl1.y + cur.ctrl2.y) * 0.5f; + left.ctrl1.y = (cur.start.y + cur.ctrl1.y) * 0.5f; + right.ctrl2.y = (cur.ctrl2.y + cur.end.y) * 0.5f; + left.start.y = cur.start.y; + right.end.y = cur.end.y; + left.ctrl2.y = (left.ctrl1.y + c) * 0.5f; + right.ctrl1.y = (right.ctrl2.y + c) * 0.5f; + left.end.y = right.start.y = (left.ctrl2.y + right.ctrl1.y) * 0.5f; +} + + +float bezLength(const Bezier& cur) +{ + return _bezLength(cur, _lineLength); +} + + +float bezLengthApprox(const Bezier& cur) +{ + return _bezLength(cur, _lineLengthApprox); +} + + +void bezSplitLeft(Bezier& cur, float at, Bezier& left) +{ + left.start = cur.start; + + left.ctrl1.x = cur.start.x + at * (cur.ctrl1.x - cur.start.x); + left.ctrl1.y = cur.start.y + at * (cur.ctrl1.y - cur.start.y); + + left.ctrl2.x = cur.ctrl1.x + at * (cur.ctrl2.x - cur.ctrl1.x); //temporary holding spot + left.ctrl2.y = cur.ctrl1.y + at * (cur.ctrl2.y - cur.ctrl1.y); //temporary holding spot + + cur.ctrl2.x = cur.ctrl2.x + at * (cur.end.x - cur.ctrl2.x); + cur.ctrl2.y = cur.ctrl2.y + at * (cur.end.y - cur.ctrl2.y); + + cur.ctrl1.x = left.ctrl2.x + at * (cur.ctrl2.x - left.ctrl2.x); + cur.ctrl1.y = left.ctrl2.y + at * (cur.ctrl2.y - left.ctrl2.y); + + left.ctrl2.x = left.ctrl1.x + at * (left.ctrl2.x - left.ctrl1.x); + left.ctrl2.y = left.ctrl1.y + at * (left.ctrl2.y - left.ctrl1.y); + + left.end.x = cur.start.x = left.ctrl2.x + at * (cur.ctrl1.x - left.ctrl2.x); + left.end.y = cur.start.y = left.ctrl2.y + at * (cur.ctrl1.y - left.ctrl2.y); +} + + +float bezAt(const Bezier& bz, float at, float length) +{ + return _bezAt(bz, at, length, _lineLength); +} + + +float bezAtApprox(const Bezier& bz, float at, float length) +{ + return _bezAt(bz, at, length, _lineLengthApprox); +} + + +void bezSplitAt(const Bezier& cur, float at, Bezier& left, Bezier& right) +{ + right = cur; + auto t = bezAt(right, at, bezLength(right)); + bezSplitLeft(right, t, left); +} + + +Point bezPointAt(const Bezier& bz, float t) +{ + Point cur; + auto it = 1.0f - t; + + auto ax = bz.start.x * it + bz.ctrl1.x * t; + auto bx = bz.ctrl1.x * it + bz.ctrl2.x * t; + auto cx = bz.ctrl2.x * it + bz.end.x * t; + ax = ax * it + bx * t; + bx = bx * it + cx * t; + cur.x = ax * it + bx * t; + + float ay = bz.start.y * it + bz.ctrl1.y * t; + float by = bz.ctrl1.y * it + bz.ctrl2.y * t; + float cy = bz.ctrl2.y * it + bz.end.y * t; + ay = ay * it + by * t; + by = by * it + cy * t; + cur.y = ay * it + by * t; + + return cur; +} + + +float bezAngleAt(const Bezier& bz, float t) +{ + if (t < 0 || t > 1) return 0; + + //derivate + // p'(t) = 3 * (-(1-2t+t^2) * p0 + (1 - 4 * t + 3 * t^2) * p1 + (2 * t - 3 * + // t^2) * p2 + t^2 * p3) + float mt = 1.0f - t; + float d = t * t; + float a = -mt * mt; + float b = 1 - 4 * t + 3 * d; + float c = 2 * t - 3 * d; + + Point pt ={a * bz.start.x + b * bz.ctrl1.x + c * bz.ctrl2.x + d * bz.end.x, a * bz.start.y + b * bz.ctrl1.y + c * bz.ctrl2.y + d * bz.end.y}; + pt.x *= 3; + pt.y *= 3; + + return mathRad2Deg(atan2(pt.x, pt.y)); +} + +} + +#endif /* LV_USE_THORVG_INTERNAL */ diff --git a/include/liblvgl/libs/thorvg/tvgLines.h b/include/liblvgl/libs/thorvg/tvgLines.h new file mode 100644 index 00000000..804ced13 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgLines.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_LINES_H_ +#define _TVG_LINES_H_ + +#include "tvgCommon.h" + +namespace tvg +{ + +struct Line +{ + Point pt1; + Point pt2; +}; + +float lineLength(const Point& pt1, const Point& pt2); +void lineSplitAt(const Line& cur, float at, Line& left, Line& right); + + +struct Bezier +{ + Point start; + Point ctrl1; + Point ctrl2; + Point end; +}; + +void bezSplit(const Bezier&cur, Bezier& left, Bezier& right); +float bezLength(const Bezier& cur); +void bezSplitLeft(Bezier& cur, float at, Bezier& left); +float bezAt(const Bezier& bz, float at, float length); +void bezSplitAt(const Bezier& cur, float at, Bezier& left, Bezier& right); +Point bezPointAt(const Bezier& bz, float t); +float bezAngleAt(const Bezier& bz, float t); + +float bezLengthApprox(const Bezier& cur); +float bezAtApprox(const Bezier& bz, float at, float length); +} + +#endif //_TVG_LINES_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ diff --git a/include/liblvgl/libs/thorvg/tvgLoadModule.h b/include/liblvgl/libs/thorvg/tvgLoadModule.h new file mode 100644 index 00000000..210995fc --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgLoadModule.h @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_LOAD_MODULE_H_ +#define _TVG_LOAD_MODULE_H_ + +#include "tvgRender.h" +#include "tvgInlist.h" + + +struct LoadModule +{ + INLIST_ITEM(LoadModule); + + //Use either hashkey(data) or hashpath(path) + union { + uintptr_t hashkey; + char* hashpath = nullptr; + }; + + FileType type; //current loader file type + uint16_t sharing = 0; //reference count + bool readied = false; //read done already. + bool pathcache = false; //cached by path + + LoadModule(FileType type) : type(type) {} + virtual ~LoadModule() + { + if (pathcache) free(hashpath); + } + + virtual bool open(const string& path) { return false; } + virtual bool open(const char* data, uint32_t size, bool copy) { return false; } + virtual bool resize(Paint* paint, float w, float h) { return false; } + virtual void sync() {}; //finish immediately if any async update jobs. + + virtual bool read() + { + if (readied) return false; + readied = true; + return true; + } + + bool cached() + { + if (hashkey) return true; + return false; + } + + virtual bool close() + { + if (sharing == 0) return true; + --sharing; + return false; + } +}; + + +struct ImageLoader : LoadModule +{ + static ColorSpace cs; //desired value + + float w = 0, h = 0; //default image size + Surface surface; + + ImageLoader(FileType type) : LoadModule(type) {} + + virtual bool animatable() { return false; } //true if this loader supports animation. + virtual Paint* paint() { return nullptr; } + + virtual Surface* bitmap() + { + if (surface.data) return &surface; + return nullptr; + } +}; + + +struct FontLoader : LoadModule +{ + float scale = 1.0f; + + FontLoader(FileType type) : LoadModule(type) {} + + virtual bool request(Shape* shape, char* text, bool italic = false) = 0; +}; + +#endif //_TVG_LOAD_MODULE_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgLoader.cpp b/include/liblvgl/libs/thorvg/tvgLoader.cpp new file mode 100644 index 00000000..8d1a3c58 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgLoader.cpp @@ -0,0 +1,441 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include + +#include "tvgInlist.h" +#include "tvgLoader.h" +#include "tvgLock.h" + +#ifdef THORVG_SVG_LOADER_SUPPORT + #include "tvgSvgLoader.h" +#endif + +#ifdef THORVG_PNG_LOADER_SUPPORT + #include "tvgPngLoader.h" +#endif + +#ifdef THORVG_TVG_LOADER_SUPPORT + #include "tvgTvgLoader.h" +#endif + +#ifdef THORVG_JPG_LOADER_SUPPORT + #include "tvgJpgLoader.h" +#endif + +#ifdef THORVG_WEBP_LOADER_SUPPORT + #include "tvgWebpLoader.h" +#endif + +#ifdef THORVG_TTF_LOADER_SUPPORT + #include "tvgTtfLoader.h" +#endif + +#ifdef THORVG_LOTTIE_LOADER_SUPPORT + #include "tvgLottieLoader.h" +#endif + +#include "tvgRawLoader.h" + + +uintptr_t HASH_KEY(const char* data) +{ + return reinterpret_cast(data); +} + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +ColorSpace ImageLoader::cs = ColorSpace::ARGB8888; + +static Key key; +static Inlist _activeLoaders; + + +static LoadModule* _find(FileType type) +{ + switch(type) { + case FileType::Png: { +#ifdef THORVG_PNG_LOADER_SUPPORT + return new PngLoader; +#endif + break; + } + case FileType::Jpg: { +#ifdef THORVG_JPG_LOADER_SUPPORT + return new JpgLoader; +#endif + break; + } + case FileType::Webp: { +#ifdef THORVG_WEBP_LOADER_SUPPORT + return new WebpLoader; +#endif + break; + } + case FileType::Tvg: { +#ifdef THORVG_TVG_LOADER_SUPPORT + return new TvgLoader; +#endif + break; + } + case FileType::Svg: { +#ifdef THORVG_SVG_LOADER_SUPPORT + return new SvgLoader; +#endif + break; + } + case FileType::Ttf: { +#ifdef THORVG_TTF_LOADER_SUPPORT + return new TtfLoader; +#endif + break; + } + case FileType::Lottie: { +#ifdef THORVG_LOTTIE_LOADER_SUPPORT + return new LottieLoader; +#endif + break; + } + case FileType::Raw: { + return new RawLoader; + break; + } + default: { + break; + } + } + +#ifdef THORVG_LOG_ENABLED + const char *format; + switch(type) { + case FileType::Tvg: { + format = "TVG"; + break; + } + case FileType::Svg: { + format = "SVG"; + break; + } + case FileType::Ttf: { + format = "TTF"; + break; + } + case FileType::Lottie: { + format = "lottie(json)"; + break; + } + case FileType::Raw: { + format = "RAW"; + break; + } + case FileType::Png: { + format = "PNG"; + break; + } + case FileType::Jpg: { + format = "JPG"; + break; + } + case FileType::Webp: { + format = "WEBP"; + break; + } + default: { + format = "???"; + break; + } + } + TVGLOG("RENDERER", "%s format is not supported", format); +#endif + return nullptr; +} + + +static LoadModule* _findByPath(const string& path) +{ + auto ext = path.substr(path.find_last_of(".") + 1); + if (!ext.compare("tvg")) return _find(FileType::Tvg); + if (!ext.compare("svg")) return _find(FileType::Svg); + if (!ext.compare("json")) return _find(FileType::Lottie); + if (!ext.compare("png")) return _find(FileType::Png); + if (!ext.compare("jpg")) return _find(FileType::Jpg); + if (!ext.compare("webp")) return _find(FileType::Webp); + if (!ext.compare("ttf") || !ext.compare("ttc")) return _find(FileType::Ttf); + if (!ext.compare("otf") || !ext.compare("otc")) return _find(FileType::Ttf); + return nullptr; +} + + +static FileType _convert(const string& mimeType) +{ + auto type = FileType::Unknown; + + if (mimeType == "tvg") type = FileType::Tvg; + else if (mimeType == "svg" || mimeType == "svg+xml") type = FileType::Svg; + else if (mimeType == "ttf" || mimeType == "otf") type = FileType::Ttf; + else if (mimeType == "lottie") type = FileType::Lottie; + else if (mimeType == "raw") type = FileType::Raw; + else if (mimeType == "png") type = FileType::Png; + else if (mimeType == "jpg" || mimeType == "jpeg") type = FileType::Jpg; + else if (mimeType == "webp") type = FileType::Webp; + else TVGLOG("RENDERER", "Given mimetype is unknown = \"%s\".", mimeType.c_str()); + + return type; +} + + +static LoadModule* _findByType(const string& mimeType) +{ + return _find(_convert(mimeType)); +} + + +static LoadModule* _findFromCache(const string& path) +{ + ScopedLock lock(key); + + auto loader = _activeLoaders.head; + + while (loader) { + if (loader->pathcache && !strcmp(loader->hashpath, path.c_str())) { + ++loader->sharing; + return loader; + } + loader = loader->next; + } + return nullptr; +} + + +static LoadModule* _findFromCache(const char* data, uint32_t size, const string& mimeType) +{ + auto type = _convert(mimeType); + if (type == FileType::Unknown) return nullptr; + + ScopedLock lock(key); + auto loader = _activeLoaders.head; + + auto key = HASH_KEY(data); + + while (loader) { + if (loader->type == type && loader->hashkey == key) { + ++loader->sharing; + return loader; + } + loader = loader->next; + } + return nullptr; +} + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + + +bool LoaderMgr::init() +{ + return true; +} + + +bool LoaderMgr::term() +{ + auto loader = _activeLoaders.head; + + //clean up the remained font loaders which is globally used. + while (loader && loader->type == FileType::Ttf) { + auto ret = loader->close(); + auto tmp = loader; + loader = loader->next; + _activeLoaders.remove(tmp); + if (ret) delete(tmp); + } + return true; +} + + +bool LoaderMgr::retrieve(LoadModule* loader) +{ + if (!loader) return false; + if (loader->close()) { + if (loader->cached()) { + ScopedLock lock(key); + _activeLoaders.remove(loader); + } + delete(loader); + } + return true; +} + + +LoadModule* LoaderMgr::loader(const string& path, bool* invalid) +{ + *invalid = false; + + //TODO: lottie is not sharable. + auto allowCache = true; + auto ext = path.substr(path.find_last_of(".") + 1); + if (!ext.compare("json")) allowCache = false; + + if (allowCache) { + if (auto loader = _findFromCache(path)) return loader; + } + + if (auto loader = _findByPath(path)) { + if (loader->open(path)) { + if (allowCache) { + loader->hashpath = strdup(path.c_str()); + loader->pathcache = true; + { + ScopedLock lock(key); + _activeLoaders.back(loader); + } + } + return loader; + } + delete(loader); + } + //Unknown MimeType. Try with the candidates in the order + for (int i = 0; i < static_cast(FileType::Raw); i++) { + if (auto loader = _find(static_cast(i))) { + if (loader->open(path)) { + if (allowCache) { + loader->hashpath = strdup(path.c_str()); + loader->pathcache = true; + { + ScopedLock lock(key); + _activeLoaders.back(loader); + } + } + return loader; + } + delete(loader); + } + } + *invalid = true; + return nullptr; +} + + +bool LoaderMgr::retrieve(const string& path) +{ + return retrieve(_findFromCache(path)); +} + + +LoadModule* LoaderMgr::loader(const char* key) +{ + auto loader = _activeLoaders.head; + + while (loader) { + if (loader->pathcache && strstr(loader->hashpath, key)) { + ++loader->sharing; + return loader; + } + loader = loader->next; + } + return nullptr; +} + + +LoadModule* LoaderMgr::loader(const char* data, uint32_t size, const string& mimeType, bool copy) +{ + //Note that users could use the same data pointer with the different content. + //Thus caching is only valid for shareable. + auto allowCache = !copy; + + //TODO: lottie is not sharable. + if (allowCache) { + auto type = _convert(mimeType); + if (type == FileType::Lottie) allowCache = false; + } + + if (allowCache) { + if (auto loader = _findFromCache(data, size, mimeType)) return loader; + } + + //Try with the given MimeType + if (!mimeType.empty()) { + if (auto loader = _findByType(mimeType)) { + if (loader->open(data, size, copy)) { + if (allowCache) { + loader->hashkey = HASH_KEY(data); + ScopedLock lock(key); + _activeLoaders.back(loader); + } + return loader; + } else { + TVGLOG("LOADER", "Given mimetype \"%s\" seems incorrect or not supported.", mimeType.c_str()); + delete(loader); + } + } + } + //Unknown MimeType. Try with the candidates in the order + for (int i = 0; i < static_cast(FileType::Raw); i++) { + auto loader = _find(static_cast(i)); + if (loader) { + if (loader->open(data, size, copy)) { + if (allowCache) { + loader->hashkey = HASH_KEY(data); + ScopedLock lock(key); + _activeLoaders.back(loader); + } + return loader; + } + delete(loader); + } + } + return nullptr; +} + + +LoadModule* LoaderMgr::loader(const uint32_t *data, uint32_t w, uint32_t h, bool copy) +{ + //Note that users could use the same data pointer with the different content. + //Thus caching is only valid for shareable. + if (!copy) { + //TODO: should we check premultiplied?? + if (auto loader = _findFromCache((const char*)(data), w * h, "raw")) return loader; + } + + //function is dedicated for raw images only + auto loader = new RawLoader; + if (loader->open(data, w, h, copy)) { + if (!copy) { + loader->hashkey = HASH_KEY((const char*)data); + ScopedLock lock(key); + _activeLoaders.back(loader); + } + return loader; + } + delete(loader); + return nullptr; +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgLoader.h b/include/liblvgl/libs/thorvg/tvgLoader.h new file mode 100644 index 00000000..50676990 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgLoader.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_LOADER_H_ +#define _TVG_LOADER_H_ + +#include "tvgLoadModule.h" + +struct LoaderMgr +{ + static bool init(); + static bool term(); + static LoadModule* loader(const string& path, bool* invalid); + static LoadModule* loader(const char* data, uint32_t size, const string& mimeType, bool copy); + static LoadModule* loader(const uint32_t* data, uint32_t w, uint32_t h, bool copy); + static LoadModule* loader(const char* key); + static bool retrieve(const string& path); + static bool retrieve(LoadModule* loader); +}; + +#endif //_TVG_LOADER_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgLock.h b/include/liblvgl/libs/thorvg/tvgLock.h new file mode 100644 index 00000000..709c120e --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgLock.h @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_LOCK_H_ +#define _TVG_LOCK_H_ + +#ifdef THORVG_THREAD_SUPPORT + +#include + +namespace tvg { + + struct Key + { + std::mutex mtx; + }; + + struct ScopedLock + { + Key* key = nullptr; + + ScopedLock(Key& k) + { + k.mtx.lock(); + key = &k; + } + + ~ScopedLock() + { + key->mtx.unlock(); + } + }; + +} + +#else //THORVG_THREAD_SUPPORT + +namespace tvg { + + struct Key {}; + + struct ScopedLock + { + ScopedLock(Key& key) {} + }; + +} + +#endif //THORVG_THREAD_SUPPORT + +#endif //_TVG_LOCK_H_ + + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgLottieAnimation.cpp b/include/liblvgl/libs/thorvg/tvgLottieAnimation.cpp new file mode 100644 index 00000000..69818b37 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgLottieAnimation.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgCommon.h" +#include "thorvg_lottie.h" +#include "tvgLottieLoader.h" +#include "tvgAnimation.h" + + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +LottieAnimation::~LottieAnimation() +{ +} + + +Result LottieAnimation::override(const char* slot) noexcept +{ + if (!pImpl->picture->pImpl->loader) return Result::InsufficientCondition; + + if (static_cast(pImpl->picture->pImpl->loader)->override(slot)) { + return Result::Success; + } + + return Result::InvalidArguments; +} + + +Result LottieAnimation::segment(const char* marker) noexcept +{ + auto loader = pImpl->picture->pImpl->loader; + if (!loader) return Result::InsufficientCondition; + if (!loader->animatable()) return Result::NonSupport; + + if (!marker) { + static_cast(loader)->segment(0.0f, 1.0f); + return Result::Success; + } + + float begin, end; + if (!static_cast(loader)->segment(marker, begin, end)) { + return Result::InvalidArguments; + } + return static_cast(this)->segment(begin, end); +} + + +uint32_t LottieAnimation::markersCnt() noexcept +{ + auto loader = pImpl->picture->pImpl->loader; + if (!loader || !loader->animatable()) return 0; + return static_cast(loader)->markersCnt(); +} + + +const char* LottieAnimation::marker(uint32_t idx) noexcept +{ + auto loader = pImpl->picture->pImpl->loader; + if (!loader || !loader->animatable()) return nullptr; + return static_cast(loader)->markers(idx); +} + + +unique_ptr LottieAnimation::gen() noexcept +{ + return unique_ptr(new LottieAnimation); +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgLottieBuilder.cpp b/include/liblvgl/libs/thorvg/tvgLottieBuilder.cpp new file mode 100644 index 00000000..82a43cfd --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgLottieBuilder.cpp @@ -0,0 +1,1378 @@ +/* + * Copyright (c) 2023 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include + +#include "tvgCommon.h" +#include "tvgMath.h" +#include "tvgPaint.h" +#include "tvgShape.h" +#include "tvgInlist.h" +#include "tvgTaskScheduler.h" +#include "tvgLottieModel.h" +#include "tvgLottieBuilder.h" +#include "tvgLottieExpressions.h" + + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +struct RenderRepeater +{ + int cnt; + float offset; + Point position; + Point anchor; + Point scale; + float rotation; + uint8_t startOpacity; + uint8_t endOpacity; + bool interpOpacity; + bool inorder; +}; + + +struct RenderContext +{ + INLIST_ITEM(RenderContext); + + Shape* propagator = nullptr; + Shape* merging = nullptr; //merging shapes if possible (if shapes have same properties) + LottieObject** begin = nullptr; //iteration entry point + RenderRepeater* repeater = nullptr; + Matrix* transform = nullptr; + float roundness = 0.0f; + bool fragmenting = false; //render context has been fragmented by filling + bool reqFragment = false; //requirement to fragment the render context + bool ownPropagator = true; //this rendering context shares the propagator + + RenderContext() + { + propagator = Shape::gen().release(); + } + + ~RenderContext() + { + if (ownPropagator) delete(propagator); + delete(repeater); + free(transform); + } + + RenderContext(const RenderContext& rhs, bool mergeable = false) + { + if (mergeable) { + this->ownPropagator = false; + propagator = rhs.propagator; + merging = rhs.merging; + } else { + propagator = static_cast(rhs.propagator->duplicate()); + } + + if (rhs.repeater) { + repeater = new RenderRepeater(); + *repeater = *rhs.repeater; + } + roundness = rhs.roundness; + } +}; + + +static void _updateChildren(LottieGroup* parent, float frameNo, Inlist& contexts, LottieExpressions* exps); +static void _updateLayer(LottieLayer* root, LottieLayer* layer, float frameNo, LottieExpressions* exps); +static bool _buildComposition(LottieComposition* comp, LottieGroup* parent); +static Shape* _draw(LottieGroup* parent, RenderContext* ctx); + +static void _rotateX(Matrix* m, float degree) +{ + if (degree == 0.0f) return; + auto radian = mathDeg2Rad(degree); + m->e22 *= cosf(radian); +} + + +static void _rotateY(Matrix* m, float degree) +{ + if (degree == 0.0f) return; + auto radian = mathDeg2Rad(degree); + m->e11 *= cosf(radian); +} + + +static void _rotationZ(Matrix* m, float degree) +{ + if (degree == 0.0f) return; + auto radian = mathDeg2Rad(degree); + m->e11 = cosf(radian); + m->e12 = -sinf(radian); + m->e21 = sinf(radian); + m->e22 = cosf(radian); +} + + +static void _skew(Matrix* m, float angleDeg, float axisDeg) +{ + auto angle = -mathDeg2Rad(angleDeg); + float tanVal = tanf(angle); + + axisDeg = fmod(axisDeg, 180.0f); + if (fabsf(axisDeg) < 0.01f || fabsf(axisDeg - 180.0f) < 0.01f || fabsf(axisDeg + 180.0f) < 0.01f) { + float cosVal = cosf(mathDeg2Rad(axisDeg)); + auto B = cosVal * cosVal * tanVal; + m->e12 += B * m->e11; + m->e22 += B * m->e21; + return; + } else if (fabsf(axisDeg - 90.0f) < 0.01f || fabsf(axisDeg + 90.0f) < 0.01f) { + float sinVal = -sinf(mathDeg2Rad(axisDeg)); + auto C = sinVal * sinVal * tanVal; + m->e11 -= C * m->e12; + m->e21 -= C * m->e22; + return; + } + + auto axis = -mathDeg2Rad(axisDeg); + float cosVal = cosf(axis); + float sinVal = sinf(axis); + auto A = sinVal * cosVal * tanVal; + auto B = cosVal * cosVal * tanVal; + auto C = sinVal * sinVal * tanVal; + + auto e11 = m->e11; + auto e21 = m->e21; + m->e11 = (1.0f - A) * e11 - C * m->e12; + m->e12 = B * e11 + (1.0f + A) * m->e12; + m->e21 = (1.0f - A) * e21 - C * m->e22; + m->e22 = B * e21 + (1.0f + A) * m->e22; +} + + +static bool _updateTransform(LottieTransform* transform, float frameNo, bool autoOrient, Matrix& matrix, uint8_t& opacity, LottieExpressions* exps) +{ + mathIdentity(&matrix); + + if (!transform) { + opacity = 255; + return false; + } + + if (transform->coords) { + mathTranslate(&matrix, transform->coords->x(frameNo), transform->coords->y(frameNo)); + } else { + auto position = transform->position(frameNo, exps); + mathTranslate(&matrix, position.x, position.y); + } + + auto angle = 0.0f; + if (autoOrient) angle = transform->position.angle(frameNo); + _rotationZ(&matrix, transform->rotation(frameNo, exps) + angle); + + if (transform->rotationEx) { + _rotateY(&matrix, transform->rotationEx->y(frameNo, exps)); + _rotateX(&matrix, transform->rotationEx->x(frameNo, exps)); + } + + auto skewAngle = transform->skewAngle(frameNo, exps); + if (skewAngle != 0.0f) { + // For angles where tangent explodes, the shape degenerates into an infinitely thin line. + // This is handled by zeroing out the matrix due to finite numerical precision. + skewAngle = fmod(skewAngle, 180.0f); + if (fabsf(skewAngle - 90.0f) < 0.01f || fabsf(skewAngle + 90.0f) < 0.01f) return false; + _skew(&matrix, skewAngle, transform->skewAxis(frameNo, exps)); + } + + auto scale = transform->scale(frameNo, exps); + mathScaleR(&matrix, scale.x * 0.01f, scale.y * 0.01f); + + //Lottie specific anchor transform. + auto anchor = transform->anchor(frameNo, exps); + mathTranslateR(&matrix, -anchor.x, -anchor.y); + + //invisible just in case. + if (scale.x == 0.0f || scale.y == 0.0f) opacity = 0; + else opacity = transform->opacity(frameNo, exps); + + return true; +} + + +static void _updateTransform(LottieLayer* layer, float frameNo, LottieExpressions* exps) +{ + if (!layer || mathEqual(layer->cache.frameNo, frameNo)) return; + + auto transform = layer->transform; + auto parent = layer->parent; + + if (parent) _updateTransform(parent, frameNo, exps); + + auto& matrix = layer->cache.matrix; + + _updateTransform(transform, frameNo, layer->autoOrient, matrix, layer->cache.opacity, exps); + + if (parent) { + if (!mathIdentity((const Matrix*) &parent->cache.matrix)) { + if (mathIdentity((const Matrix*) &matrix)) layer->cache.matrix = parent->cache.matrix; + else layer->cache.matrix = mathMultiply(&parent->cache.matrix, &matrix); + } + } + layer->cache.frameNo = frameNo; +} + + +static void _updateTransform(LottieGroup* parent, LottieObject** child, float frameNo, TVG_UNUSED Inlist& contexts, RenderContext* ctx, LottieExpressions* exps) +{ + auto transform = static_cast(*child); + if (!transform) return; + + uint8_t opacity; + + if (parent->mergeable()) { + if (!ctx->transform) ctx->transform = (Matrix*)malloc(sizeof(Matrix)); + _updateTransform(transform, frameNo, false, *ctx->transform, opacity, exps); + return; + } + + ctx->merging = nullptr; + + Matrix matrix; + if (!_updateTransform(transform, frameNo, false, matrix, opacity, exps)) return; + + auto pmatrix = PP(ctx->propagator)->transform(); + ctx->propagator->transform(pmatrix ? mathMultiply(pmatrix, &matrix) : matrix); + ctx->propagator->opacity(MULTIPLY(opacity, PP(ctx->propagator)->opacity)); + + //FIXME: preserve the stroke width. too workaround, need a better design. + if (P(ctx->propagator)->rs.strokeWidth() > 0.0f) { + auto denominator = sqrtf(matrix.e11 * matrix.e11 + matrix.e12 * matrix.e12); + if (denominator > 1.0f) ctx->propagator->stroke(ctx->propagator->strokeWidth() / denominator); + } +} + + +static void _updateGroup(LottieGroup* parent, LottieObject** child, float frameNo, TVG_UNUSED Inlist& pcontexts, RenderContext* ctx, LottieExpressions* exps) +{ + auto group = static_cast(*child); + + if (group->children.empty()) return; + + //Prepare render data + group->scene = parent->scene; + group->reqFragment |= ctx->reqFragment; + + //generate a merging shape to consolidate partial shapes into a single entity + if (group->mergeable()) _draw(parent, ctx); + + Inlist contexts; + contexts.back(new RenderContext(*ctx, group->mergeable())); + + _updateChildren(group, frameNo, contexts, exps); + + contexts.free(); +} + + +static void _updateStroke(LottieStroke* stroke, float frameNo, RenderContext* ctx, LottieExpressions* exps) +{ + ctx->propagator->stroke(stroke->width(frameNo, exps)); + ctx->propagator->stroke(stroke->cap); + ctx->propagator->stroke(stroke->join); + ctx->propagator->strokeMiterlimit(stroke->miterLimit); + + if (stroke->dashattr) { + float dashes[2]; + dashes[0] = stroke->dashSize(frameNo, exps); + dashes[1] = dashes[0] + stroke->dashGap(frameNo, exps); + P(ctx->propagator)->strokeDash(dashes, 2, stroke->dashOffset(frameNo, exps)); + } else { + ctx->propagator->stroke(nullptr, 0); + } +} + + +static bool _fragmented(LottieObject** child, Inlist& contexts, RenderContext* ctx) +{ + if (!ctx->reqFragment) return false; + if (ctx->fragmenting) return true; + + contexts.back(new RenderContext(*ctx)); + auto fragment = contexts.tail; + fragment->begin = child - 1; + ctx->fragmenting = true; + + return false; +} + + +static void _updateSolidStroke(TVG_UNUSED LottieGroup* parent, LottieObject** child, float frameNo, Inlist& contexts, RenderContext* ctx, LottieExpressions* exps) +{ + if (_fragmented(child, contexts, ctx)) return; + + auto stroke = static_cast(*child); + + ctx->merging = nullptr; + auto color = stroke->color(frameNo, exps); + ctx->propagator->stroke(color.rgb[0], color.rgb[1], color.rgb[2], stroke->opacity(frameNo, exps)); + _updateStroke(static_cast(stroke), frameNo, ctx, exps); +} + + +static void _updateGradientStroke(TVG_UNUSED LottieGroup* parent, LottieObject** child, float frameNo, Inlist& contexts, RenderContext* ctx, LottieExpressions* exps) +{ + if (_fragmented(child, contexts, ctx)) return; + + auto stroke = static_cast(*child); + + ctx->merging = nullptr; + ctx->propagator->stroke(unique_ptr(stroke->fill(frameNo, exps))); + _updateStroke(static_cast(stroke), frameNo, ctx, exps); +} + + +static void _updateSolidFill(TVG_UNUSED LottieGroup* parent, LottieObject** child, float frameNo, TVG_UNUSED Inlist& contexts, RenderContext* ctx, LottieExpressions* exps) +{ + if (_fragmented(child, contexts, ctx)) return; + + auto fill = static_cast(*child); + + ctx->merging = nullptr; + auto color = fill->color(frameNo); + ctx->propagator->fill(color.rgb[0], color.rgb[1], color.rgb[2], fill->opacity(frameNo, exps)); + ctx->propagator->fill(fill->rule); + + if (ctx->propagator->strokeWidth() > 0) ctx->propagator->order(true); +} + + +static void _updateGradientFill(TVG_UNUSED LottieGroup* parent, LottieObject** child, float frameNo, TVG_UNUSED Inlist& contexts, RenderContext* ctx, LottieExpressions* exps) +{ + if (_fragmented(child, contexts, ctx)) return; + + auto fill = static_cast(*child); + + ctx->merging = nullptr; + //TODO: reuse the fill instance? + ctx->propagator->fill(unique_ptr(fill->fill(frameNo, exps))); + ctx->propagator->fill(fill->rule); + ctx->propagator->opacity(MULTIPLY(fill->opacity(frameNo), PP(ctx->propagator)->opacity)); + + if (ctx->propagator->strokeWidth() > 0) ctx->propagator->order(true); +} + + +static Shape* _draw(LottieGroup* parent, RenderContext* ctx) +{ + if (ctx->merging) return ctx->merging; + + auto shape = cast(ctx->propagator->duplicate()); + ctx->merging = shape.get(); + parent->scene->push(std::move(shape)); + + return ctx->merging; +} + + +//OPTIMIZE: path? +static void _repeat(LottieGroup* parent, unique_ptr path, RenderContext* ctx) +{ + auto repeater = ctx->repeater; + + Array shapes(repeater->cnt); + + for (int i = 0; i < repeater->cnt; ++i) { + auto multiplier = repeater->offset + static_cast(i); + + auto shape = static_cast(ctx->propagator->duplicate()); + P(shape)->rs.path = P(path.get())->rs.path; + + auto opacity = repeater->interpOpacity ? mathLerp(repeater->startOpacity, repeater->endOpacity, static_cast(i + 1) / repeater->cnt) : repeater->startOpacity; + shape->opacity(opacity); + + Matrix m; + mathIdentity(&m); + mathTranslate(&m, repeater->position.x * multiplier + repeater->anchor.x, repeater->position.y * multiplier + repeater->anchor.y); + mathScale(&m, powf(repeater->scale.x * 0.01f, multiplier), powf(repeater->scale.y * 0.01f, multiplier)); + mathRotate(&m, repeater->rotation * multiplier); + mathTranslateR(&m, -repeater->anchor.x, -repeater->anchor.y); + + auto pm = PP(shape)->transform(); + shape->transform(pm ? mathMultiply(&m, pm) : m); + + if (ctx->roundness > 1.0f && P(shape)->rs.stroke) { + TVGERR("LOTTIE", "FIXME: Path roundness should be applied properly!"); + P(shape)->rs.stroke->join = StrokeJoin::Round; + } + + shapes.push(shape); + } + + //push repeat shapes in order. + if (repeater->inorder) { + for (auto shape = shapes.begin(); shape < shapes.end(); ++shape) { + parent->scene->push(cast(*shape)); + } + } else { + for (auto shape = shapes.end() - 1; shape >= shapes.begin(); --shape) { + parent->scene->push(cast(*shape)); + } + } +} + + +static void _appendRect(Shape* shape, float x, float y, float w, float h, float r, Matrix* transform) +{ + //sharp rect + if (mathZero(r)) { + PathCommand commands[] = { + PathCommand::MoveTo, PathCommand::LineTo, PathCommand::LineTo, + PathCommand::LineTo, PathCommand::Close + }; + + Point points[] = {{x + w, y}, {x + w, y + h}, {x, y + h}, {x, y}}; + if (transform) { + for (int i = 0; i < 4; i++) mathTransform(transform, &points[i]); + } + shape->appendPath(commands, 5, points, 4); + //round rect + } else { + PathCommand commands[] = { + PathCommand::MoveTo, PathCommand::LineTo, PathCommand::CubicTo, + PathCommand::LineTo, PathCommand::CubicTo, PathCommand::LineTo, + PathCommand::CubicTo, PathCommand::LineTo, PathCommand::CubicTo, + PathCommand::Close + }; + + auto halfW = w * 0.5f; + auto halfH = h * 0.5f; + auto rx = r > halfW ? halfW : r; + auto ry = r > halfH ? halfH : r; + auto hrx = rx * PATH_KAPPA; + auto hry = ry * PATH_KAPPA; + + constexpr int ptsCnt = 17; + Point points[ptsCnt] = { + {x + w, y + ry}, //moveTo + {x + w, y + h - ry}, //lineTo + {x + w, y + h - ry + hry}, {x + w - rx + hrx, y + h}, {x + w - rx, y + h}, //cubicTo + {x + rx, y + h}, //lineTo + {x + rx - hrx, y + h}, {x, y + h - ry + hry}, {x, y + h - ry}, //cubicTo + {x, y + ry}, //lineTo + {x, y + ry - hry}, {x + rx - hrx, y}, {x + rx, y}, //cubicTo + {x + w - rx, y}, //lineTo + {x + w - rx + hrx, y}, {x + w, y + ry - hry}, {x + w, y + ry} //cubicTo + }; + + if (transform) { + for (int i = 0; i < ptsCnt; i++) mathTransform(transform, &points[i]); + } + shape->appendPath(commands, 10, points, ptsCnt); + } +} + +static void _updateRect(LottieGroup* parent, LottieObject** child, float frameNo, TVG_UNUSED Inlist& contexts, RenderContext* ctx, LottieExpressions* exps) +{ + auto rect = static_cast(*child); + + auto position = rect->position(frameNo, exps); + auto size = rect->size(frameNo, exps); + auto roundness = rect->radius(frameNo, exps); + if (ctx->roundness > roundness) roundness = ctx->roundness; + + if (roundness > ROUNDNESS_EPSILON) { + if (roundness > size.x * 0.5f) roundness = size.x * 0.5f; + if (roundness > size.y * 0.5f) roundness = size.y * 0.5f; + } + + if (ctx->repeater) { + auto path = Shape::gen(); + _appendRect(path.get(), position.x - size.x * 0.5f, position.y - size.y * 0.5f, size.x, size.y, roundness, ctx->transform); + _repeat(parent, std::move(path), ctx); + } else { + auto merging = _draw(parent, ctx); + _appendRect(merging, position.x - size.x * 0.5f, position.y - size.y * 0.5f, size.x, size.y, roundness, ctx->transform); + } +} + + +static void _appendCircle(Shape* shape, float cx, float cy, float rx, float ry, Matrix* transform) +{ + auto rxKappa = rx * PATH_KAPPA; + auto ryKappa = ry * PATH_KAPPA; + + constexpr int cmdsCnt = 6; + PathCommand commands[cmdsCnt] = { + PathCommand::MoveTo, PathCommand::CubicTo, PathCommand::CubicTo, + PathCommand::CubicTo, PathCommand::CubicTo, PathCommand::Close + }; + + constexpr int ptsCnt = 13; + Point points[ptsCnt] = { + {cx, cy - ry}, //moveTo + {cx + rxKappa, cy - ry}, {cx + rx, cy - ryKappa}, {cx + rx, cy}, //cubicTo + {cx + rx, cy + ryKappa}, {cx + rxKappa, cy + ry}, {cx, cy + ry}, //cubicTo + {cx - rxKappa, cy + ry}, {cx - rx, cy + ryKappa}, {cx - rx, cy}, //cubicTo + {cx - rx, cy - ryKappa}, {cx - rxKappa, cy - ry}, {cx, cy - ry} //cubicTo + }; + + if (transform) { + for (int i = 0; i < ptsCnt; ++i) mathTransform(transform, &points[i]); + } + + shape->appendPath(commands, cmdsCnt, points, ptsCnt); +} + + +static void _updateEllipse(LottieGroup* parent, LottieObject** child, float frameNo, TVG_UNUSED Inlist& contexts, RenderContext* ctx, LottieExpressions* exps) +{ + auto ellipse = static_cast(*child); + + auto position = ellipse->position(frameNo, exps); + auto size = ellipse->size(frameNo, exps); + + if (ctx->repeater) { + auto path = Shape::gen(); + _appendCircle(path.get(), position.x, position.y, size.x * 0.5f, size.y * 0.5f, ctx->transform); + _repeat(parent, std::move(path), ctx); + } else { + auto merging = _draw(parent, ctx); + _appendCircle(merging, position.x, position.y, size.x * 0.5f, size.y * 0.5f, ctx->transform); + } +} + + +static void _updatePath(LottieGroup* parent, LottieObject** child, float frameNo, TVG_UNUSED Inlist& contexts, RenderContext* ctx, LottieExpressions* exps) +{ + auto path = static_cast(*child); + + if (ctx->repeater) { + auto p = Shape::gen(); + path->pathset(frameNo, P(p)->rs.path.cmds, P(p)->rs.path.pts, ctx->transform, ctx->roundness, exps); + _repeat(parent, std::move(p), ctx); + } else { + auto merging = _draw(parent, ctx); + if (path->pathset(frameNo, P(merging)->rs.path.cmds, P(merging)->rs.path.pts, ctx->transform, ctx->roundness, exps)) { + P(merging)->update(RenderUpdateFlag::Path); + } + } +} + + +static void _updateText(LottieGroup* parent, LottieObject** child, float frameNo, TVG_UNUSED Inlist& contexts, TVG_UNUSED RenderContext* ctx, LottieExpressions* exps) +{ + auto text = static_cast(*child); + auto& doc = text->doc(frameNo); + auto p = doc.text; + + if (!p || !text->font) return; + + auto scale = doc.size * 0.01f; + float spacing = text->spacing(frameNo) / scale; + Point cursor = {0.0f, 0.0f}; + auto scene = Scene::gen(); + int line = 0; + + //text string + while (true) { + //TODO: remove nested scenes. + //end of text, new line of the cursor position + if (*p == 13 || *p == 3 || *p == '\0') { + //text layout position + auto ascent = text->font->ascent * scale; + if (ascent > doc.bbox.size.y) ascent = doc.bbox.size.y; + Point layout = {doc.bbox.pos.x, doc.bbox.pos.y + ascent - doc.shift}; + + //adjust the layout + if (doc.justify == 1) layout.x += doc.bbox.size.x - (cursor.x * scale); //right aligned + else if (doc.justify == 2) layout.x += (doc.bbox.size.x * 0.5f) - (cursor.x * 0.5f * scale); //center aligned + + scene->translate(layout.x, layout.y); + scene->scale(scale); + + parent->scene->push(std::move(scene)); + + if (*p == '\0') break; + ++p; + + //new text group, single scene for each line + scene = Scene::gen(); + cursor.x = 0.0f; + cursor.y = ++line * (doc.height / scale); + } + + //find the glyph + for (auto g = text->font->chars.begin(); g < text->font->chars.end(); ++g) { + auto glyph = *g; + //draw matched glyphs + if (!strncmp(glyph->code, p, glyph->len)) { + //TODO: caching? + auto shape = Shape::gen(); + for (auto g = glyph->children.begin(); g < glyph->children.end(); ++g) { + auto group = static_cast(*g); + for (auto p = group->children.begin(); p < group->children.end(); ++p) { + if (static_cast(*p)->pathset(frameNo, P(shape)->rs.path.cmds, P(shape)->rs.path.pts, nullptr, 0.0f, exps)) { + P(shape)->update(RenderUpdateFlag::Path); + } + } + } + shape->fill(doc.color.rgb[0], doc.color.rgb[1], doc.color.rgb[2]); + shape->translate(cursor.x, cursor.y); + + if (doc.stroke.render) { + shape->stroke(StrokeJoin::Round); + shape->stroke(doc.stroke.width / scale); + shape->stroke(doc.stroke.color.rgb[0], doc.stroke.color.rgb[1], doc.stroke.color.rgb[2]); + } + + scene->push(std::move(shape)); + + p += glyph->len; + + //advance the cursor position horizontally + cursor.x += glyph->width + spacing + doc.tracking; + break; + } + } + } +} + + +static void _applyRoundedCorner(Shape* star, Shape* merging, float outerRoundness, float roundness, bool hasRoundness) +{ + static constexpr auto ROUNDED_POLYSTAR_MAGIC_NUMBER = 0.47829f; + + auto cmdCnt = star->pathCommands(nullptr); + const Point *pts = nullptr; + auto ptsCnt = star->pathCoords(&pts); + + auto len = mathLength(pts[1] - pts[2]); + auto r = len > 0.0f ? ROUNDED_POLYSTAR_MAGIC_NUMBER * mathMin(len * 0.5f, roundness) / len : 0.0f; + + if (hasRoundness) { + P(merging)->rs.path.cmds.grow((uint32_t)(1.5 * cmdCnt)); + P(merging)->rs.path.pts.grow((uint32_t)(4.5 * cmdCnt)); + + int start = 3 * mathZero(outerRoundness); + merging->moveTo(pts[start].x, pts[start].y); + + for (uint32_t i = 1 + start; i < ptsCnt; i += 6) { + auto& prev = pts[i]; + auto& curr = pts[i + 2]; + auto& next = (i < ptsCnt - start) ? pts[i + 4] : pts[2]; + auto& nextCtrl = (i < ptsCnt - start) ? pts[i + 5] : pts[3]; + auto dNext = r * (curr - next); + auto dPrev = r * (curr - prev); + + auto p0 = curr - 2.0f * dPrev; + auto p1 = curr - dPrev; + auto p2 = curr - dNext; + auto p3 = curr - 2.0f * dNext; + + merging->cubicTo(prev.x, prev.y, p0.x, p0.y, p0.x, p0.y); + merging->cubicTo(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y); + merging->cubicTo(p3.x, p3.y, next.x, next.y, nextCtrl.x, nextCtrl.y); + } + } else { + P(merging)->rs.path.cmds.grow(2 * cmdCnt); + P(merging)->rs.path.pts.grow(4 * cmdCnt); + + auto dPrev = r * (pts[1] - pts[0]); + auto p = pts[0] + 2.0f * dPrev; + merging->moveTo(p.x, p.y); + + for (uint32_t i = 1; i < ptsCnt; ++i) { + auto& curr = pts[i]; + auto& next = (i == ptsCnt - 1) ? pts[1] : pts[i + 1]; + auto dNext = r * (curr - next); + + auto p0 = curr - 2.0f * dPrev; + auto p1 = curr - dPrev; + auto p2 = curr - dNext; + auto p3 = curr - 2.0f * dNext; + + merging->lineTo(p0.x, p0.y); + merging->cubicTo(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y); + + dPrev = -1.0f * dNext; + } + } + merging->close(); +} + + +static void _updateStar(LottieGroup* parent, LottiePolyStar* star, Matrix* transform, float roundness, float frameNo, Shape* merging, LottieExpressions* exps) +{ + static constexpr auto POLYSTAR_MAGIC_NUMBER = 0.47829f / 0.28f; + + auto ptsCnt = star->ptsCnt(frameNo, exps); + auto innerRadius = star->innerRadius(frameNo, exps); + auto outerRadius = star->outerRadius(frameNo, exps); + auto innerRoundness = star->innerRoundness(frameNo, exps) * 0.01f; + auto outerRoundness = star->outerRoundness(frameNo, exps) * 0.01f; + + auto angle = mathDeg2Rad(-90.0f); + auto partialPointRadius = 0.0f; + auto anglePerPoint = (2.0f * MATH_PI / ptsCnt); + auto halfAnglePerPoint = anglePerPoint * 0.5f; + auto partialPointAmount = ptsCnt - floorf(ptsCnt); + auto longSegment = false; + auto numPoints = size_t(ceilf(ptsCnt) * 2); + auto direction = (star->direction == 0) ? 1.0f : -1.0f; + auto hasRoundness = false; + bool roundedCorner = (roundness > ROUNDNESS_EPSILON) && (mathZero(innerRoundness) || mathZero(outerRoundness)); + //TODO: we can use PathCommand / PathCoord directly. + auto shape = roundedCorner ? Shape::gen().release() : merging; + + float x, y; + + if (!mathZero(partialPointAmount)) { + angle += halfAnglePerPoint * (1.0f - partialPointAmount) * direction; + } + + if (!mathZero(partialPointAmount)) { + partialPointRadius = innerRadius + partialPointAmount * (outerRadius - innerRadius); + x = partialPointRadius * cosf(angle); + y = partialPointRadius * sinf(angle); + angle += anglePerPoint * partialPointAmount * 0.5f * direction; + } else { + x = outerRadius * cosf(angle); + y = outerRadius * sinf(angle); + angle += halfAnglePerPoint * direction; + } + + if (mathZero(innerRoundness) && mathZero(outerRoundness)) { + P(shape)->rs.path.pts.reserve(numPoints + 2); + P(shape)->rs.path.cmds.reserve(numPoints + 3); + } else { + P(shape)->rs.path.pts.reserve(numPoints * 3 + 2); + P(shape)->rs.path.cmds.reserve(numPoints + 3); + hasRoundness = true; + } + + Point in = {x, y}; + if (transform) mathTransform(transform, &in); + shape->moveTo(in.x, in.y); + + for (size_t i = 0; i < numPoints; i++) { + auto radius = longSegment ? outerRadius : innerRadius; + auto dTheta = halfAnglePerPoint; + if (!mathZero(partialPointRadius) && i == numPoints - 2) { + dTheta = anglePerPoint * partialPointAmount * 0.5f; + } + if (!mathZero(partialPointRadius) && i == numPoints - 1) { + radius = partialPointRadius; + } + auto previousX = x; + auto previousY = y; + x = radius * cosf(angle); + y = radius * sinf(angle); + + if (hasRoundness) { + auto cp1Theta = (atan2f(previousY, previousX) - MATH_PI2 * direction); + auto cp1Dx = cosf(cp1Theta); + auto cp1Dy = sinf(cp1Theta); + auto cp2Theta = (atan2f(y, x) - MATH_PI2 * direction); + auto cp2Dx = cosf(cp2Theta); + auto cp2Dy = sinf(cp2Theta); + + auto cp1Roundness = longSegment ? innerRoundness : outerRoundness; + auto cp2Roundness = longSegment ? outerRoundness : innerRoundness; + auto cp1Radius = longSegment ? innerRadius : outerRadius; + auto cp2Radius = longSegment ? outerRadius : innerRadius; + + auto cp1x = cp1Radius * cp1Roundness * POLYSTAR_MAGIC_NUMBER * cp1Dx / ptsCnt; + auto cp1y = cp1Radius * cp1Roundness * POLYSTAR_MAGIC_NUMBER * cp1Dy / ptsCnt; + auto cp2x = cp2Radius * cp2Roundness * POLYSTAR_MAGIC_NUMBER * cp2Dx / ptsCnt; + auto cp2y = cp2Radius * cp2Roundness * POLYSTAR_MAGIC_NUMBER * cp2Dy / ptsCnt; + + if (!mathZero(partialPointAmount) && ((i == 0) || (i == numPoints - 1))) { + cp1x *= partialPointAmount; + cp1y *= partialPointAmount; + cp2x *= partialPointAmount; + cp2y *= partialPointAmount; + } + Point in2 = {previousX - cp1x, previousY - cp1y}; + Point in3 = {x + cp2x, y + cp2y}; + Point in4 = {x, y}; + if (transform) { + mathTransform(transform, &in2); + mathTransform(transform, &in3); + mathTransform(transform, &in4); + } + shape->cubicTo(in2.x, in2.y, in3.x, in3.y, in4.x, in4.y); + } else { + Point in = {x, y}; + if (transform) mathTransform(transform, &in); + shape->lineTo(in.x, in.y); + } + angle += dTheta * direction; + longSegment = !longSegment; + } + shape->close(); + + if (roundedCorner) { + _applyRoundedCorner(shape, merging, outerRoundness, roundness, hasRoundness); + delete(shape); + } +} + + +static void _updatePolygon(LottieGroup* parent, LottiePolyStar* star, Matrix* transform, float frameNo, Shape* merging, LottieExpressions* exps) +{ + static constexpr auto POLYGON_MAGIC_NUMBER = 0.25f; + + auto ptsCnt = size_t(floor(star->ptsCnt(frameNo, exps))); + auto radius = star->outerRadius(frameNo, exps); + auto roundness = star->outerRoundness(frameNo, exps) * 0.01f; + + auto angle = mathDeg2Rad(-90.0f); + auto anglePerPoint = 2.0f * MATH_PI / float(ptsCnt); + auto direction = (star->direction == 0) ? 1.0f : -1.0f; + auto hasRoundness = false; + auto x = radius * cosf(angle); + auto y = radius * sinf(angle); + + angle += anglePerPoint * direction; + + if (mathZero(roundness)) { + P(merging)->rs.path.pts.reserve(ptsCnt + 2); + P(merging)->rs.path.cmds.reserve(ptsCnt + 3); + } else { + P(merging)->rs.path.pts.reserve(ptsCnt * 3 + 2); + P(merging)->rs.path.cmds.reserve(ptsCnt + 3); + hasRoundness = true; + } + + Point in = {x, y}; + if (transform) mathTransform(transform, &in); + merging->moveTo(in.x, in.y); + + for (size_t i = 0; i < ptsCnt; i++) { + auto previousX = x; + auto previousY = y; + x = (radius * cosf(angle)); + y = (radius * sinf(angle)); + + if (hasRoundness) { + auto cp1Theta = atan2f(previousY, previousX) - MATH_PI2 * direction; + auto cp1Dx = cosf(cp1Theta); + auto cp1Dy = sinf(cp1Theta); + auto cp2Theta = atan2f(y, x) - MATH_PI2 * direction; + auto cp2Dx = cosf(cp2Theta); + auto cp2Dy = sinf(cp2Theta); + + auto cp1x = radius * roundness * POLYGON_MAGIC_NUMBER * cp1Dx; + auto cp1y = radius * roundness * POLYGON_MAGIC_NUMBER * cp1Dy; + auto cp2x = radius * roundness * POLYGON_MAGIC_NUMBER * cp2Dx; + auto cp2y = radius * roundness * POLYGON_MAGIC_NUMBER * cp2Dy; + + Point in2 = {previousX - cp1x, previousY - cp1y}; + Point in3 = {x + cp2x, y + cp2y}; + Point in4 = {x, y}; + if (transform) { + mathTransform(transform, &in2); + mathTransform(transform, &in3); + mathTransform(transform, &in4); + } + merging->cubicTo(in2.x, in2.y, in3.x, in3.y, in4.x, in4.y); + } else { + Point in = {x, y}; + if (transform) mathTransform(transform, &in); + merging->lineTo(in.x, in.y); + } + angle += anglePerPoint * direction; + } + merging->close(); +} + + +static void _updatePolystar(LottieGroup* parent, LottieObject** child, float frameNo, TVG_UNUSED Inlist& contexts, RenderContext* ctx, LottieExpressions* exps) +{ + auto star= static_cast(*child); + + //Optimize: Can we skip the individual coords transform? + Matrix matrix; + mathIdentity(&matrix); + auto position = star->position(frameNo, exps); + mathTranslate(&matrix, position.x, position.y); + mathRotate(&matrix, star->rotation(frameNo, exps)); + + if (ctx->transform) matrix = mathMultiply(ctx->transform, &matrix); + + auto identity = mathIdentity((const Matrix*)&matrix); + + if (ctx->repeater) { + auto p = Shape::gen(); + if (star->type == LottiePolyStar::Star) _updateStar(parent, star, identity ? nullptr : &matrix, ctx->roundness, frameNo, p.get(), exps); + else _updatePolygon(parent, star, identity ? nullptr : &matrix, frameNo, p.get(), exps); + _repeat(parent, std::move(p), ctx); + } else { + auto merging = _draw(parent, ctx); + if (star->type == LottiePolyStar::Star) _updateStar(parent, star, identity ? nullptr : &matrix, ctx->roundness, frameNo, merging, exps); + else _updatePolygon(parent, star, identity ? nullptr : &matrix, frameNo, merging, exps); + P(merging)->update(RenderUpdateFlag::Path); + } +} + + +static void _updateImage(LottieGroup* parent, LottieObject** child, float frameNo, TVG_UNUSED Inlist& contexts, RenderContext* ctx) +{ + auto image = static_cast(*child); + auto picture = image->picture; + + if (!picture) { + picture = Picture::gen().release(); + + //force to load a picture on the same thread + TaskScheduler::async(false); + + if (image->size > 0) { + if (picture->load((const char*)image->b64Data, image->size, image->mimeType, false) != Result::Success) { + delete(picture); + return; + } + } else { + if (picture->load(image->path) != Result::Success) { + delete(picture); + return; + } + } + + TaskScheduler::async(true); + + image->picture = picture; + PP(picture)->ref(); + } + + if (ctx->propagator) { + if (auto matrix = PP(ctx->propagator)->transform()) { + picture->transform(*matrix); + } + picture->opacity(PP(ctx->propagator)->opacity); + } + parent->scene->push(cast(picture)); +} + + +static void _updateRoundedCorner(TVG_UNUSED LottieGroup* parent, LottieObject** child, float frameNo, TVG_UNUSED Inlist& contexts, RenderContext* ctx, LottieExpressions* exps) +{ + auto roundedCorner= static_cast(*child); + auto roundness = roundedCorner->radius(frameNo, exps); + if (ctx->roundness < roundness) ctx->roundness = roundness; +} + + +static void _updateRepeater(TVG_UNUSED LottieGroup* parent, LottieObject** child, float frameNo, TVG_UNUSED Inlist& contexts, RenderContext* ctx, LottieExpressions* exps) +{ + auto repeater= static_cast(*child); + + if (!ctx->repeater) ctx->repeater = new RenderRepeater(); + ctx->repeater->cnt = static_cast(repeater->copies(frameNo, exps)); + ctx->repeater->offset = repeater->offset(frameNo, exps); + ctx->repeater->position = repeater->position(frameNo, exps); + ctx->repeater->anchor = repeater->anchor(frameNo, exps); + ctx->repeater->scale = repeater->scale(frameNo, exps); + ctx->repeater->rotation = repeater->rotation(frameNo, exps); + ctx->repeater->startOpacity = repeater->startOpacity(frameNo, exps); + ctx->repeater->endOpacity = repeater->endOpacity(frameNo, exps); + ctx->repeater->inorder = repeater->inorder; + ctx->repeater->interpOpacity = (ctx->repeater->startOpacity == ctx->repeater->endOpacity) ? false : true; + + ctx->merging = nullptr; +} + + +static void _updateTrimpath(TVG_UNUSED LottieGroup* parent, LottieObject** child, float frameNo, TVG_UNUSED Inlist& contexts, RenderContext* ctx, LottieExpressions* exps) +{ + auto trimpath= static_cast(*child); + + float begin, end; + trimpath->segment(frameNo, begin, end, exps); + + if (P(ctx->propagator)->rs.stroke) { + auto pbegin = P(ctx->propagator)->rs.stroke->trim.begin; + auto pend = P(ctx->propagator)->rs.stroke->trim.end; + auto length = fabsf(pend - pbegin); + begin = (length * begin) + pbegin; + end = (length * end) + pbegin; + } + + P(ctx->propagator)->strokeTrim(begin, end, trimpath->type == LottieTrimpath::Type::Individual ? true : false); +} + + +static void _updateChildren(LottieGroup* parent, float frameNo, Inlist& contexts, LottieExpressions* exps) +{ + contexts.head->begin = parent->children.end() - 1; + + while (!contexts.empty()) { + auto ctx = contexts.front(); + ctx->reqFragment = parent->reqFragment; + for (auto child = ctx->begin; child >= parent->children.data; --child) { + //Here switch-case statements are more performant than virtual methods. + switch ((*child)->type) { + case LottieObject::Group: { + _updateGroup(parent, child, frameNo, contexts, ctx, exps); + break; + } + case LottieObject::Transform: { + _updateTransform(parent, child, frameNo, contexts, ctx, exps); + break; + } + case LottieObject::SolidFill: { + _updateSolidFill(parent, child, frameNo, contexts, ctx, exps); + break; + } + case LottieObject::SolidStroke: { + _updateSolidStroke(parent, child, frameNo, contexts, ctx, exps); + break; + } + case LottieObject::GradientFill: { + _updateGradientFill(parent, child, frameNo, contexts, ctx, exps); + break; + } + case LottieObject::GradientStroke: { + _updateGradientStroke(parent, child, frameNo, contexts, ctx, exps); + break; + } + case LottieObject::Rect: { + _updateRect(parent, child, frameNo, contexts, ctx, exps); + break; + } + case LottieObject::Ellipse: { + _updateEllipse(parent, child, frameNo, contexts, ctx, exps); + break; + } + case LottieObject::Path: { + _updatePath(parent, child, frameNo, contexts, ctx, exps); + break; + } + case LottieObject::Polystar: { + _updatePolystar(parent, child, frameNo, contexts, ctx, exps); + break; + } + case LottieObject::Image: { + _updateImage(parent, child, frameNo, contexts, ctx); + break; + } + case LottieObject::Trimpath: { + _updateTrimpath(parent, child, frameNo, contexts, ctx, exps); + break; + } + case LottieObject::Text: { + _updateText(parent, child, frameNo, contexts, ctx, exps); + break; + } + case LottieObject::Repeater: { + _updateRepeater(parent, child, frameNo, contexts, ctx, exps); + break; + } + case LottieObject::RoundedCorner: { + _updateRoundedCorner(parent, child, frameNo, contexts, ctx, exps); + break; + } + default: break; + } + } + delete(ctx); + } +} + + +static void _updatePrecomp(LottieLayer* precomp, float frameNo, LottieExpressions* exps) +{ + if (precomp->children.empty()) return; + + frameNo = precomp->remap(frameNo, exps); + + for (auto child = precomp->children.end() - 1; child >= precomp->children.begin(); --child) { + _updateLayer(precomp, static_cast(*child), frameNo, exps); + } + + //clip the layer viewport + if (precomp->w > 0 && precomp->h > 0) { + auto clipper = Shape::gen().release(); + clipper->appendRect(0, 0, static_cast(precomp->w), static_cast(precomp->h)); + clipper->transform(precomp->cache.matrix); + + //TODO: remove the intermediate scene.... + auto cscene = Scene::gen(); + cscene->composite(cast(clipper), CompositeMethod::ClipPath); + cscene->push(cast(precomp->scene)); + precomp->scene = cscene.release(); + } +} + + +static void _updateSolid(LottieLayer* layer) +{ + auto shape = Shape::gen(); + shape->appendRect(0, 0, static_cast(layer->w), static_cast(layer->h)); + shape->fill(layer->color.rgb[0], layer->color.rgb[1], layer->color.rgb[2], layer->cache.opacity); + layer->scene->push(std::move(shape)); +} + + +static void _updateMaskings(LottieLayer* layer, float frameNo, LottieExpressions* exps) +{ + if (layer->masks.count == 0) return; + + //maskings + Shape* pmask = nullptr; + auto pmethod = CompositeMethod::AlphaMask; + + for (auto m = layer->masks.begin(); m < layer->masks.end(); ++m) { + auto mask = static_cast(*m); + auto shape = Shape::gen().release(); + shape->fill(255, 255, 255, mask->opacity(frameNo)); + shape->transform(layer->cache.matrix); + if (mask->pathset(frameNo, P(shape)->rs.path.cmds, P(shape)->rs.path.pts, nullptr, 0.0f, exps)) { + P(shape)->update(RenderUpdateFlag::Path); + } + auto method = mask->method; + if (pmask) { + //false of false is true. invert. + if (method == CompositeMethod::SubtractMask && pmethod == method) { + method = CompositeMethod::AddMask; + } else if (pmethod == CompositeMethod::DifferenceMask && pmethod == method) { + method = CompositeMethod::IntersectMask; + } + pmask->composite(cast(shape), method); + } else { + if (method == CompositeMethod::SubtractMask) method = CompositeMethod::InvAlphaMask; + else if (method == CompositeMethod::AddMask) method = CompositeMethod::AlphaMask; + else if (method == CompositeMethod::IntersectMask) method = CompositeMethod::AlphaMask; + else if (method == CompositeMethod::DifferenceMask) method = CompositeMethod::AlphaMask; //does this correct? + layer->scene->composite(cast(shape), method); + } + pmethod = mask->method; + pmask = shape; + } +} + + +static bool _updateMatte(LottieLayer* root, LottieLayer* layer, float frameNo, LottieExpressions* exps) +{ + auto target = layer->matte.target; + if (!target) return true; + + _updateLayer(root, target, frameNo, exps); + + if (target->scene) { + layer->scene->composite(cast(target->scene), layer->matte.type); + } else if (layer->matte.type == CompositeMethod::AlphaMask || layer->matte.type == CompositeMethod::LumaMask) { + //matte target is not exist. alpha blending definitely bring an invisible result + delete(layer->scene); + layer->scene = nullptr; + return false; + } + return true; +} + + +static void _updateLayer(LottieLayer* root, LottieLayer* layer, float frameNo, LottieExpressions* exps) +{ + layer->scene = nullptr; + + //visibility + if (frameNo < layer->inFrame || frameNo >= layer->outFrame) return; + + _updateTransform(layer, frameNo, exps); + + //full transparent scene. no need to perform + if (layer->type != LottieLayer::Null && layer->cache.opacity == 0) return; + + //Prepare render data + layer->scene = Scene::gen().release(); + + //ignore opacity when Null layer? + if (layer->type != LottieLayer::Null) layer->scene->opacity(layer->cache.opacity); + + layer->scene->transform(layer->cache.matrix); + + if (layer->matte.target && layer->masks.count > 0) TVGERR("LOTTIE", "FIXME: Matte + Masking??"); + + if (!_updateMatte(root, layer, frameNo, exps)) return; + + _updateMaskings(layer, frameNo, exps); + + switch (layer->type) { + case LottieLayer::Precomp: { + _updatePrecomp(layer, frameNo, exps); + break; + } + case LottieLayer::Solid: { + _updateSolid(layer); + break; + } + default: { + if (!layer->children.empty()) { + Inlist contexts; + contexts.back(new RenderContext); + _updateChildren(layer, frameNo, contexts, exps); + contexts.free(); + } + break; + } + } + + layer->scene->blend(layer->blendMethod); + + //the given matte source was composited by the target earlier. + if (!layer->matteSrc) root->scene->push(cast(layer->scene)); +} + + +static void _buildReference(LottieComposition* comp, LottieLayer* layer) +{ + for (auto asset = comp->assets.begin(); asset < comp->assets.end(); ++asset) { + if (strcmp(layer->refId, (*asset)->name)) continue; + if (layer->type == LottieLayer::Precomp) { + auto assetLayer = static_cast(*asset); + if (_buildComposition(comp, assetLayer)) { + layer->children = assetLayer->children; + layer->reqFragment = assetLayer->reqFragment; + } + } else if (layer->type == LottieLayer::Image) { + layer->children.push(*asset); + } + break; + } +} + + +static void _buildHierarchy(LottieGroup* parent, LottieLayer* child) +{ + if (child->pid == -1) return; + + if (child->matte.target && child->pid == child->matte.target->id) { + child->parent = child->matte.target; + return; + } + + for (auto p = parent->children.begin(); p < parent->children.end(); ++p) { + auto parent = static_cast(*p); + if (child == parent) continue; + if (child->pid == parent->id) { + child->parent = parent; + break; + } + if (parent->matte.target && parent->matte.target->id == child->pid) { + child->parent = parent->matte.target; + break; + } + } +} + + +static void _attachFont(LottieComposition* comp, LottieLayer* parent) +{ + //TODO: Consider to migrate this attachment to the frame update time. + for (auto c = parent->children.begin(); c < parent->children.end(); ++c) { + auto text = static_cast(*c); + auto& doc = text->doc(0); + if (!doc.name) continue; + auto len = strlen(doc.name); + for (uint32_t i = 0; i < comp->fonts.count; ++i) { + auto font = comp->fonts[i]; + auto len2 = strlen(font->name); + if (!strncmp(font->name, doc.name, len < len2 ? len : len2)) { + text->font = font; + break; + } + } + } +} + + +static bool _buildComposition(LottieComposition* comp, LottieGroup* parent) +{ + if (parent->children.count == 0) return false; + if (parent->buildDone) return true; + parent->buildDone = true; + + for (auto c = parent->children.begin(); c < parent->children.end(); ++c) { + auto child = static_cast(*c); + + //attach the precomp layer. + if (child->refId) _buildReference(comp, child); + + if (child->matte.target) { + //parenting + _buildHierarchy(parent, child->matte.target); + //precomp referencing + if (child->matte.target->refId) _buildReference(comp, child->matte.target); + } + _buildHierarchy(parent, child); + + //attach the necessary font data + if (child->type == LottieLayer::Text) _attachFont(comp, child); + } + return true; +} + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +bool LottieBuilder::update(LottieComposition* comp, float frameNo) +{ + if (comp->root->children.empty()) return false; + + frameNo += comp->startFrame; + if (frameNo < comp->startFrame) frameNo = comp->startFrame; + if (frameNo >= comp->endFrame) frameNo = (comp->endFrame - 1); + + //update children layers + auto root = comp->root; + root->scene->clear(); + + if (exps && comp->expressions) exps->update(comp->timeAtFrame(frameNo)); + + for (auto child = root->children.end() - 1; child >= root->children.begin(); --child) { + _updateLayer(root, static_cast(*child), frameNo, exps); + } + + return true; +} + + +void LottieBuilder::build(LottieComposition* comp) +{ + if (!comp) return; + + comp->root->scene = Scene::gen().release(); + if (!comp->root->scene) return; + + _buildComposition(comp, comp->root); + + if (!update(comp, 0)) return; + + //viewport clip + auto clip = Shape::gen(); + clip->appendRect(0, 0, static_cast(comp->w), static_cast(comp->h)); + comp->root->scene->composite(std::move(clip), CompositeMethod::ClipPath); +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgLottieBuilder.h b/include/liblvgl/libs/thorvg/tvgLottieBuilder.h new file mode 100644 index 00000000..690fa85a --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgLottieBuilder.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL +#ifndef _TVG_LOTTIE_BUILDER_H_ +#define _TVG_LOTTIE_BUILDER_H_ + +#include "tvgCommon.h" +#include "tvgLottieExpressions.h" + +struct LottieComposition; + +struct LottieBuilder +{ + LottieExpressions* exps = nullptr; + + LottieBuilder() + { + exps = LottieExpressions::instance(); + } + + ~LottieBuilder() + { + LottieExpressions::retrieve(exps); + } + + bool update(LottieComposition* comp, float progress); + void build(LottieComposition* comp); +}; + +#endif //_TVG_LOTTIE_BUILDER_H + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgLottieExpressions.cpp b/include/liblvgl/libs/thorvg/tvgLottieExpressions.cpp new file mode 100644 index 00000000..aa6c9b13 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgLottieExpressions.cpp @@ -0,0 +1,1298 @@ +/* + * Copyright (c) 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgMath.h" +#include "tvgLottieModel.h" +#include "tvgLottieExpressions.h" + +#ifdef THORVG_LOTTIE_EXPRESSIONS_SUPPORT + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +struct ExpContent +{ + LottieObject* obj; + float frameNo; +}; + + +//reserved expressions specifiers +static const char* EXP_NAME = "name"; +static const char* EXP_CONTENT = "content"; +static const char* EXP_WIDTH = "width"; +static const char* EXP_HEIGHT = "height"; +static const char* EXP_CYCLE = "cycle"; +static const char* EXP_PINGPONG = "pingpong"; +static const char* EXP_OFFSET = "offset"; +static const char* EXP_CONTINUE = "continue"; +static const char* EXP_TIME = "time"; +static const char* EXP_VALUE = "value"; +static const char* EXP_INDEX = "index"; +static const char* EXP_EFFECT= "effect"; + +static LottieExpressions* exps = nullptr; //singleton instance engine + + +static void contentFree(void *native_p, struct jerry_object_native_info_t *info_p) +{ + free(native_p); +} + +static jerry_object_native_info_t freeCb {contentFree, 0, 0}; +static uint32_t engineRefCnt = 0; //Expressions Engine reference count + + +static char* _name(jerry_value_t args) +{ + auto arg0 = jerry_value_to_string(args); + auto len = jerry_string_length(arg0); + auto name = (jerry_char_t*)malloc(len * sizeof(jerry_char_t) + 1); + jerry_string_to_buffer(arg0, JERRY_ENCODING_UTF8, name, len); + name[len] = '\0'; + jerry_value_free(arg0); + return (char*) name; +} + + +static jerry_value_t _toComp(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + TVGERR("LOTTIE", "toComp is not supported in expressions!"); + + return jerry_undefined(); +} + + +static void _buildTransform(jerry_value_t context, LottieTransform* transform) +{ + if (!transform) return; + + auto obj = jerry_object(); + jerry_object_set_sz(context, "transform", obj); + + auto anchorPoint = jerry_object(); + jerry_object_set_native_ptr(anchorPoint, nullptr, &transform->anchor); + jerry_object_set_sz(obj, "anchorPoint", anchorPoint); + jerry_value_free(anchorPoint); + + auto position = jerry_object(); + jerry_object_set_native_ptr(position, nullptr, &transform->position); + jerry_object_set_sz(obj, "position", position); + jerry_value_free(position); + + auto scale = jerry_object(); + jerry_object_set_native_ptr(scale, nullptr, &transform->scale); + jerry_object_set_sz(obj, "scale", scale); + jerry_value_free(scale); + + auto rotation = jerry_object(); + jerry_object_set_native_ptr(rotation, nullptr, &transform->rotation); + jerry_object_set_sz(obj, "rotation", rotation); + jerry_value_free(rotation); + + auto opacity = jerry_object(); + jerry_object_set_native_ptr(opacity, nullptr, &transform->opacity); + jerry_object_set_sz(obj, "opacity", opacity); + jerry_value_free(opacity); + + jerry_value_free(obj); +} + + +static void _buildLayer(jerry_value_t context, LottieLayer* layer, LottieComposition* comp) +{ + auto width = jerry_number(layer->w); + jerry_object_set_sz(context, EXP_WIDTH, width); + jerry_value_free(width); + + auto height = jerry_number(layer->h); + jerry_object_set_sz(context, EXP_HEIGHT, height); + jerry_value_free(height); + + auto index = jerry_number(layer->id); + jerry_object_set_sz(context, EXP_INDEX, index); + jerry_value_free(index); + + auto parent = jerry_object(); + jerry_object_set_native_ptr(parent, nullptr, layer->parent); + jerry_object_set_sz(context, "parent", parent); + jerry_value_free(parent); + + auto hasParent = jerry_boolean(layer->parent ? true : false); + jerry_object_set_sz(context, "hasParent", hasParent); + jerry_value_free(hasParent); + + auto inPoint = jerry_number(layer->inFrame); + jerry_object_set_sz(context, "inPoint", inPoint); + jerry_value_free(inPoint); + + auto outPoint = jerry_number(layer->outFrame); + jerry_object_set_sz(context, "outPoint", outPoint); + jerry_value_free(outPoint); + + auto startTime = jerry_number(comp->timeAtFrame(layer->startFrame)); + jerry_object_set_sz(context, "startTime", startTime); + jerry_value_free(startTime); + + auto hasVideo = jerry_boolean(false); + jerry_object_set_sz(context, "hasVideo", hasVideo); + jerry_value_free(hasVideo); + + auto hasAudio = jerry_boolean(false); + jerry_object_set_sz(context, "hasAudio", hasAudio); + jerry_value_free(hasAudio); + + //active, #current in the animation range? + + auto enabled = jerry_boolean(!layer->hidden); + jerry_object_set_sz(context, "enabled", enabled); + jerry_value_free(enabled); + + auto audioActive = jerry_boolean(false); + jerry_object_set_sz(context, "audioActive", audioActive); + jerry_value_free(audioActive); + + //sampleImage(point, radius = [.5, .5], postEffect=true, t=time) + + _buildTransform(context, layer->transform); + + //audioLevels, #the value of the Audio Levels property of the layer in decibels + + auto timeRemap = jerry_object(); + jerry_object_set_native_ptr(timeRemap, nullptr, &layer->timeRemap); + jerry_object_set_sz(context, "timeRemap", timeRemap); + jerry_value_free(timeRemap); + + //marker.key(index) + //marker.key(name) + //marker.nearestKey(t) + //marker.numKeys + + auto name = jerry_string_sz(layer->name); + jerry_object_set_sz(context, EXP_NAME, name); + jerry_value_free(name); + + auto toComp = jerry_function_external(_toComp); + jerry_object_set_sz(context, "toComp", toComp); + jerry_object_set_native_ptr(toComp, nullptr, comp); + jerry_value_free(toComp); +} + + +static jerry_value_t _value(float frameNo, LottieExpression* exp) +{ + switch (exp->type) { + case LottieProperty::Type::Point: { + auto value = jerry_object(); + auto pos = (*static_cast(exp->property))(frameNo); + auto val1 = jerry_number(pos.x); + auto val2 = jerry_number(pos.y); + jerry_object_set_index(value, 0, val1); + jerry_object_set_index(value, 1, val2); + jerry_value_free(val1); + jerry_value_free(val2); + return value; + } + case LottieProperty::Type::Float: { + return jerry_number((*static_cast(exp->property))(frameNo)); + } + case LottieProperty::Type::Opacity: { + return jerry_number((*static_cast(exp->property))(frameNo)); + } + case LottieProperty::Type::PathSet: { + auto value = jerry_object(); + jerry_object_set_native_ptr(value, nullptr, exp->property); + return value; + } + case LottieProperty::Type::Position: { + auto value = jerry_object(); + auto pos = (*static_cast(exp->property))(frameNo); + auto val1 = jerry_number(pos.x); + auto val2 = jerry_number(pos.y); + jerry_object_set_index(value, 0, val1); + jerry_object_set_index(value, 1, val2); + jerry_value_free(val1); + jerry_value_free(val2); + return value; + } + default: { + TVGERR("LOTTIE", "Non supported type for value? = %d", (int) exp->type); + } + } + return jerry_undefined(); +} + + +static jerry_value_t _addsub(const jerry_value_t args[], float addsub) +{ + //1d + if (jerry_value_is_number(args[0])) return jerry_number(jerry_value_as_number(args[0]) + addsub * jerry_value_as_number(args[1])); + + //2d + auto val1 = jerry_object_get_index(args[0], 0); + auto val2 = jerry_object_get_index(args[0], 1); + auto val3 = jerry_object_get_index(args[1], 0); + auto val4 = jerry_object_get_index(args[1], 1); + auto x = jerry_value_as_number(val1) + addsub * jerry_value_as_number(val3); + auto y = jerry_value_as_number(val2) + addsub * jerry_value_as_number(val4); + + jerry_value_free(val1); + jerry_value_free(val2); + jerry_value_free(val3); + jerry_value_free(val4); + + auto obj = jerry_object(); + val1 = jerry_number(x); + val2 = jerry_number(y); + jerry_object_set_index(obj, 0, val1); + jerry_object_set_index(obj, 1, val2); + jerry_value_free(val1); + jerry_value_free(val2); + + return obj; +} + + +static jerry_value_t _muldiv(const jerry_value_t arg1, float arg2) +{ + //1d + if (jerry_value_is_number(arg1)) return jerry_number(jerry_value_as_number(arg1) * arg2); + + //2d + auto val1 = jerry_object_get_index(arg1, 0); + auto val2 = jerry_object_get_index(arg1, 1); + auto x = jerry_value_as_number(val1) * arg2; + auto y = jerry_value_as_number(val2) * arg2; + + jerry_value_free(val1); + jerry_value_free(val2); + + auto obj = jerry_object(); + val1 = jerry_number(x); + val2 = jerry_number(y); + jerry_object_set_index(obj, 0, val1); + jerry_object_set_index(obj, 1, val2); + jerry_value_free(val1); + jerry_value_free(val2); + + return obj; +} + + +static jerry_value_t _add(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + return _addsub(args, 1.0f); +} + + +static jerry_value_t _sub(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + return _addsub(args, -1.0f); +} + + +static jerry_value_t _mul(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + return _muldiv(args[0], jerry_value_as_number(args[1])); +} + + +static jerry_value_t _div(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + return _muldiv(args[0], 1.0f / jerry_value_as_number(args[1])); +} + + +static jerry_value_t _interp(float t, const jerry_value_t args[], int argsCnt) +{ + auto tMin = 0.0f; + auto tMax = 1.0f; + int idx = 0; + + if (argsCnt > 3) { + tMin = jerry_value_as_number(args[1]); + tMax = jerry_value_as_number(args[2]); + idx += 2; + } + + //2d + if (jerry_value_is_object(args[idx + 1]) && jerry_value_is_object(args[idx + 2])) { + auto val1 = jerry_object_get_index(args[0], 0); + auto val2 = jerry_object_get_index(args[0], 1); + auto val3 = jerry_object_get_index(args[1], 0); + auto val4 = jerry_object_get_index(args[1], 1); + + Point pt1 = {(float)jerry_value_as_number(val1), (float)jerry_value_as_number(val2)}; + Point pt2 = {(float)jerry_value_as_number(val3), (float)jerry_value_as_number(val4)}; + Point ret; + if (t <= tMin) ret = pt1; + else if (t >= tMax) ret = pt2; + else ret = mathLerp(pt1, pt2, t); + + jerry_value_free(val1); + jerry_value_free(val2); + jerry_value_free(val3); + jerry_value_free(val4); + + auto obj = jerry_object(); + val1 = jerry_number(ret.x); + val2 = jerry_number(ret.y); + jerry_object_set_index(obj, 0, val1); + jerry_object_set_index(obj, 1, val2); + jerry_value_free(val1); + jerry_value_free(val2); + + return obj; + } + + //1d + auto val1 = (float) jerry_value_as_number(args[idx + 1]); + if (t <= tMin) jerry_number(val1); + auto val2 = (float) jerry_value_as_number(args[idx + 2]); + if (t >= tMax) jerry_number(val2); + return jerry_number(mathLerp(val1, val2, t)); +} + + +static jerry_value_t _linear(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + auto t = (float) jerry_value_as_number(args[0]); + return _interp(t, args, jerry_value_as_uint32(argsCnt)); +} + + +static jerry_value_t _ease(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + auto t = (float) jerry_value_as_number(args[0]); + t = (t < 0.5) ? (4 * t * t * t) : (1.0f - pow(-2.0f * t + 2.0f, 3) * 0.5f); + return _interp(t, args, jerry_value_as_uint32(argsCnt)); +} + + + +static jerry_value_t _easeIn(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + auto t = (float) jerry_value_as_number(args[0]); + t = t * t * t; + return _interp(t, args, jerry_value_as_uint32(argsCnt)); +} + + +static jerry_value_t _easeOut(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + auto t = (float) jerry_value_as_number(args[0]); + t = 1.0f - pow(1.0f - t, 3); + return _interp(t, args, jerry_value_as_uint32(argsCnt)); +} + + +static jerry_value_t _clamp(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + auto num = jerry_value_as_number(args[0]); + auto limit1 = jerry_value_as_number(args[1]); + auto limit2 = jerry_value_as_number(args[2]); + + //clamping + if (num < limit1) num = limit1; + if (num > limit2) num = limit2; + + return jerry_number(num); +} + + +static jerry_value_t _dot(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + auto val1 = jerry_object_get_index(args[0], 0); + auto val2 = jerry_object_get_index(args[0], 1); + auto val3 = jerry_object_get_index(args[1], 0); + auto val4 = jerry_object_get_index(args[1], 1); + + auto x = jerry_value_as_number(val1) * jerry_value_as_number(val3); + auto y = jerry_value_as_number(val2) * jerry_value_as_number(val4); + + jerry_value_free(val1); + jerry_value_free(val2); + jerry_value_free(val3); + jerry_value_free(val4); + + return jerry_number(x + y); +} + + +static jerry_value_t _cross(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + auto val1 = jerry_object_get_index(args[0], 0); + auto val2 = jerry_object_get_index(args[0], 1); + auto val3 = jerry_object_get_index(args[1], 0); + auto val4 = jerry_object_get_index(args[1], 1); + + auto x = jerry_value_as_number(val1) * jerry_value_as_number(val4); + auto y = jerry_value_as_number(val2) * jerry_value_as_number(val3); + + jerry_value_free(val1); + jerry_value_free(val2); + jerry_value_free(val3); + jerry_value_free(val4); + + return jerry_number(x - y); +} + + +static jerry_value_t _normalize(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + auto val1 = jerry_object_get_index(args[0], 0); + auto val2 = jerry_object_get_index(args[0], 1); + auto x = jerry_value_as_number(val1); + auto y = jerry_value_as_number(val2); + + jerry_value_free(val1); + jerry_value_free(val2); + + auto length = sqrtf(x * x + y * y); + + x /= length; + y /= length; + + auto obj = jerry_object(); + val1 = jerry_number(x); + val2 = jerry_number(y); + jerry_object_set_index(obj, 0, val1); + jerry_object_set_index(obj, 0, val2); + jerry_value_free(val1); + jerry_value_free(val2); + + return obj; +} + + +static jerry_value_t _length(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + auto val1 = jerry_object_get_index(args[0], 0); + auto val2 = jerry_object_get_index(args[0], 1); + auto x = jerry_value_as_number(val1); + auto y = jerry_value_as_number(val2); + + jerry_value_free(val1); + jerry_value_free(val2); + + return jerry_number(sqrtf(x * x + y * y)); +} + + +static jerry_value_t _random(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + auto val = (float)(rand() % 10000001); + return jerry_number(val * 0.0000001f); +} + + +static jerry_value_t _deg2rad(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + return jerry_number(mathDeg2Rad((float)jerry_value_as_number(args[0]))); +} + + +static jerry_value_t _rad2deg(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + return jerry_number(mathRad2Deg((float)jerry_value_as_number(args[0]))); +} + + +static jerry_value_t _effect(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + TVGERR("LOTTIE", "effect is not supported in expressions!"); + + return jerry_undefined(); +} + + +static jerry_value_t _fromCompToSurface(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + TVGERR("LOTTIE", "fromCompToSurface is not supported in expressions!"); + + return jerry_undefined(); +} + + +static jerry_value_t _content(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + auto name = _name(args[0]); + auto data = static_cast(jerry_object_get_native_ptr(info->function, &freeCb)); + auto group = static_cast(data->obj); + auto target = group->content((char*)name); + free(name); + if (!target) return jerry_undefined(); + + //find the a path property(sh) in the group layer? + switch (target->type) { + case LottieObject::Group: { + auto group = static_cast(target); + auto obj = jerry_function_external(_content); + + //attach a transform + for (auto c = group->children.begin(); c < group->children.end(); ++c) { + if ((*c)->type == LottieObject::Type::Transform) { + _buildTransform(obj, static_cast(*c)); + break; + } + } + auto data2 = (ExpContent*)malloc(sizeof(ExpContent)); + data2->obj = group; + data2->frameNo = data->frameNo; + jerry_object_set_native_ptr(obj, &freeCb, data2); + jerry_object_set_sz(obj, EXP_CONTENT, obj); + return obj; + } + case LottieObject::Path: { + jerry_value_t obj = jerry_object(); + jerry_object_set_native_ptr(obj, nullptr, &static_cast(target)->pathset); + jerry_object_set_sz(obj, "path", obj); + return obj; + } + case LottieObject::Trimpath: { + auto trimpath = static_cast(target); + jerry_value_t obj = jerry_object(); + auto start = jerry_number(trimpath->start(data->frameNo)); + jerry_object_set_sz(obj, "start", start); + jerry_value_free(start); + auto end = jerry_number(trimpath->end(data->frameNo)); + jerry_object_set_sz(obj, "end", end); + jerry_value_free(end); + auto offset = jerry_number(trimpath->offset(data->frameNo)); + jerry_object_set_sz(obj, "offset", end); + jerry_value_free(offset); + return obj; + } + default: break; + } + return jerry_undefined(); +} + + +static jerry_value_t _layer(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + auto comp = static_cast(jerry_object_get_native_ptr(info->function, nullptr)); + LottieLayer* layer; + + //layer index + if (jerry_value_is_number(args[0])) { + auto idx = (uint16_t)jerry_value_as_int32(args[0]); + layer = comp->layer(idx); + jerry_value_free(idx); + //layer name + } else { + auto name = _name(args[0]); + layer = comp->layer((char*)name); + free(name); + } + + if (!layer) return jerry_undefined(); + + auto obj = jerry_object(); + jerry_object_set_native_ptr(obj, nullptr, layer); + _buildLayer(obj, layer, comp); + + return obj; +} + + +static jerry_value_t _nearestKey(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + auto exp = static_cast(jerry_object_get_native_ptr(info->function, nullptr)); + auto time = jerry_value_as_number(args[0]); + auto frameNo = exp->comp->frameAtTime(time); + auto index = jerry_number(exp->property->nearest(frameNo)); + + auto obj = jerry_object(); + jerry_object_set_sz(obj, EXP_INDEX, index); + jerry_value_free(index); + + return obj; +} + + +static jerry_value_t _valueAtTime(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + auto exp = static_cast(jerry_object_get_native_ptr(info->function, nullptr)); + auto time = jerry_value_as_number(args[0]); + auto frameNo = exp->comp->frameAtTime(time); + return _value(frameNo, exp); +} + + +static jerry_value_t _velocityAtTime(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + auto exp = static_cast(jerry_object_get_native_ptr(info->function, nullptr)); + auto time = jerry_value_as_number(args[0]); + auto frameNo = exp->comp->frameAtTime(time); + auto key = exp->property->nearest(frameNo); + auto pframe = exp->property->frameNo(key - 1); + auto cframe = exp->property->frameNo(key); + auto elapsed = (cframe - pframe) / (exp->comp->frameRate); + + Point cur, prv; + + //compute the velocity + switch (exp->type) { + case LottieProperty::Type::Point: { + prv = (*static_cast(exp->property))(pframe); + cur = (*static_cast(exp->property))(cframe); + break; + } + case LottieProperty::Type::Position: { + prv = (*static_cast(exp->property))(pframe); + cur = (*static_cast(exp->property))(cframe); + break; + } + default: { + TVGERR("LOTTIE", "Non supported type for velocityAtTime?"); + return jerry_undefined(); + } + } + + float velocity[] = {(cur.x - prv.x) / elapsed, (cur.y - prv.y) / elapsed}; + + auto obj = jerry_object(); + auto val1 = jerry_number(velocity[0]); + auto val2 = jerry_number(velocity[1]); + jerry_object_set_index(obj, 0, val1); + jerry_object_set_index(obj, 1, val2); + jerry_value_free(val1); + jerry_value_free(val2); + + return obj; +} + + +static jerry_value_t _speedAtTime(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + auto exp = static_cast(jerry_object_get_native_ptr(info->function, nullptr)); + auto time = jerry_value_as_number(args[0]); + auto frameNo = exp->comp->frameAtTime(time); + auto key = exp->property->nearest(frameNo); + auto pframe = exp->property->frameNo(key - 1); + auto cframe = exp->property->frameNo(key); + auto elapsed = (cframe - pframe) / (exp->comp->frameRate); + + Point cur, prv; + + //compute the velocity + switch (exp->type) { + case LottieProperty::Type::Point: { + prv = (*static_cast(exp->property))(pframe); + cur = (*static_cast(exp->property))(cframe); + break; + } + case LottieProperty::Type::Position: { + prv = (*static_cast(exp->property))(pframe); + cur = (*static_cast(exp->property))(cframe); + break; + } + default: { + TVGERR("LOTTIE", "Non supported type for speedAtTime?"); + return jerry_undefined(); + } + } + + auto speed = sqrtf(pow(cur.x - prv.x, 2) + pow(cur.y - prv.y, 2)) / elapsed; + auto obj = jerry_number(speed); + return obj; +} + + +static bool _loopOutCommon(LottieExpression* exp, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + exp->loop.mode = LottieExpression::LoopMode::OutCycle; + + if (argsCnt > 0) { + auto name = _name(args[0]); + if (!strcmp(name, EXP_CYCLE)) exp->loop.mode = LottieExpression::LoopMode::OutCycle; + else if (!strcmp(name, EXP_PINGPONG)) exp->loop.mode = LottieExpression::LoopMode::OutPingPong; + else if (!strcmp(name, EXP_OFFSET)) exp->loop.mode = LottieExpression::LoopMode::OutOffset; + else if (!strcmp(name, EXP_CONTINUE)) exp->loop.mode = LottieExpression::LoopMode::OutContinue; + free(name); + } + + if (exp->loop.mode != LottieExpression::LoopMode::OutCycle) { + TVGERR("hermet", "Not supported loopOut type = %d", exp->loop.mode); + return false; + } + + return true; +} + + +static jerry_value_t _loopOut(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + auto exp = static_cast(jerry_object_get_native_ptr(info->function, nullptr)); + + if (!_loopOutCommon(exp, args, argsCnt)) return jerry_undefined(); + + if (argsCnt > 1) exp->loop.key = jerry_value_as_int32(args[1]); + + auto obj = jerry_object(); + jerry_object_set_native_ptr(obj, nullptr, exp->property); + return obj; +} + + +static jerry_value_t _loopOutDuration(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + auto exp = static_cast(jerry_object_get_native_ptr(info->function, nullptr)); + + if (!_loopOutCommon(exp, args, argsCnt)) return jerry_undefined(); + + if (argsCnt > 1) { + exp->loop.in = exp->comp->frameAtTime((float)jerry_value_as_int32(args[1])); + } + + auto obj = jerry_object(); + jerry_object_set_native_ptr(obj, nullptr, exp->property); + return obj; +} + + +static bool _loopInCommon(LottieExpression* exp, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + exp->loop.mode = LottieExpression::LoopMode::InCycle; + + if (argsCnt > 0) { + auto name = _name(args[0]); + if (!strcmp(name, EXP_CYCLE)) exp->loop.mode = LottieExpression::LoopMode::InCycle; + else if (!strcmp(name, EXP_PINGPONG)) exp->loop.mode = LottieExpression::LoopMode::InPingPong; + else if (!strcmp(name, EXP_OFFSET)) exp->loop.mode = LottieExpression::LoopMode::InOffset; + else if (!strcmp(name, EXP_CONTINUE)) exp->loop.mode = LottieExpression::LoopMode::InContinue; + free(name); + } + + if (exp->loop.mode != LottieExpression::LoopMode::InCycle) { + TVGERR("hermet", "Not supported loopOut type = %d", exp->loop.mode); + return false; + } + + return true; +} + +static jerry_value_t _loopIn(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + auto exp = static_cast(jerry_object_get_native_ptr(info->function, nullptr)); + + if (!_loopInCommon(exp, args, argsCnt)) return jerry_undefined(); + + if (argsCnt > 1) { + exp->loop.in = exp->comp->frameAtTime((float)jerry_value_as_int32(args[1])); + } + + auto obj = jerry_object(); + jerry_object_set_native_ptr(obj, nullptr, exp->property); + return obj; +} + + +static jerry_value_t _loopInDuration(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + auto exp = static_cast(jerry_object_get_native_ptr(info->function, nullptr)); + + if (argsCnt > 1) { + exp->loop.in = exp->comp->frameAtTime((float)jerry_value_as_int32(args[1])); + } + + if (!_loopInCommon(exp, args, argsCnt)) return jerry_undefined(); + + auto obj = jerry_object(); + jerry_object_set_native_ptr(obj, nullptr, exp->property); + return obj; +} + + +static jerry_value_t _key(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + auto exp = static_cast(jerry_object_get_native_ptr(info->function, nullptr)); + auto key = jerry_value_as_int32(args[0]); + auto frameNo = exp->property->frameNo(key); + auto time = jerry_number(exp->comp->timeAtFrame(frameNo)); + auto value = _value(frameNo, exp); + + auto obj = jerry_object(); + jerry_object_set_sz(obj, EXP_TIME, time); + jerry_object_set_sz(obj, EXP_INDEX, args[0]); + jerry_object_set_sz(obj, EXP_VALUE, value); + + jerry_value_free(time); + jerry_value_free(value); + + return obj; +} + + + +static jerry_value_t _createPath(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + //TODO: arg1: points, arg2: inTangents, arg3: outTangents, arg4: isClosed + auto arg1 = jerry_value_to_object(args[0]); + auto pathset = jerry_object_get_native_ptr(arg1, nullptr); + if (!pathset) { + TVGERR("LOTTIE", "failed createPath()"); + return jerry_undefined(); + } + + jerry_value_free(arg1); + + auto obj = jerry_object(); + jerry_object_set_native_ptr(obj, nullptr, pathset); + return obj; +} + + +static jerry_value_t _uniformPath(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + auto pathset = static_cast(jerry_object_get_native_ptr(info->function, nullptr)); + + /* TODO: ThorVG prebuilds the path data for performance. + It actually need to constructs the Array for points, inTangents, outTangents and then return here... */ + auto obj = jerry_object(); + jerry_object_set_native_ptr(obj, nullptr, pathset); + return obj; +} + + +static jerry_value_t _isClosed(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + //TODO: Not used + return jerry_boolean(true); +} + + +static void _buildPath(jerry_value_t context, LottieExpression* exp) +{ + //Trick for fast building path. + auto points = jerry_function_external(_uniformPath); + jerry_object_set_native_ptr(points, nullptr, exp->property); + jerry_object_set_sz(context, "points", points); + jerry_value_free(points); + + auto inTangents = jerry_function_external(_uniformPath); + jerry_object_set_native_ptr(inTangents, nullptr, exp->property); + jerry_object_set_sz(context, "inTangents", inTangents); + jerry_value_free(inTangents); + + auto outTangents = jerry_function_external(_uniformPath); + jerry_object_set_native_ptr(outTangents, nullptr, exp->property); + jerry_object_set_sz(context, "outTangents", outTangents); + jerry_value_free(outTangents); + + auto isClosed = jerry_function_external(_isClosed); + jerry_object_set_native_ptr(isClosed, nullptr, exp->property); + jerry_object_set_sz(context, "isClosed", isClosed); + jerry_value_free(isClosed); + +} + + +static void _buildProperty(float frameNo, jerry_value_t context, LottieExpression* exp) +{ + auto value = _value(frameNo, exp); + jerry_object_set_sz(context, EXP_VALUE, value); + jerry_value_free(value); + + auto valueAtTime = jerry_function_external(_valueAtTime); + jerry_object_set_sz(context, "valueAtTime", valueAtTime); + jerry_object_set_native_ptr(valueAtTime, nullptr, exp); + jerry_value_free(valueAtTime); + + auto velocity = jerry_number(0.0f); + jerry_object_set_sz(context, "velocity", velocity); + jerry_value_free(velocity); + + auto velocityAtTime = jerry_function_external(_velocityAtTime); + jerry_object_set_sz(context, "velocityAtTime", velocityAtTime); + jerry_object_set_native_ptr(velocityAtTime, nullptr, exp); + jerry_value_free(velocityAtTime); + + auto speed = jerry_number(0.0f); + jerry_object_set_sz(context, "speed", speed); + jerry_value_free(speed); + + auto speedAtTime = jerry_function_external(_speedAtTime); + jerry_object_set_sz(context, "speedAtTime", speedAtTime); + jerry_object_set_native_ptr(speedAtTime, nullptr, exp); + jerry_value_free(speedAtTime); + + //wiggle(freq, amp, octaves=1, amp_mult=.5, t=time) + //temporalWiggle(freq, amp, octaves=1, amp_mult=.5, t=time) + //smooth(width=.2, samples=5, t=time) + + auto loopIn = jerry_function_external(_loopIn); + jerry_object_set_sz(context, "loopIn", loopIn); + jerry_object_set_native_ptr(loopIn, nullptr, exp); + jerry_value_free(loopIn); + + auto loopOut = jerry_function_external(_loopOut); + jerry_object_set_sz(context, "loopOut", loopOut); + jerry_object_set_native_ptr(loopOut, nullptr, exp); + jerry_value_free(loopOut); + + auto loopInDuration = jerry_function_external(_loopInDuration); + jerry_object_set_sz(context, "loopInDuration", loopInDuration); + jerry_object_set_native_ptr(loopInDuration, nullptr, exp); + jerry_value_free(loopInDuration); + + auto loopOutDuration = jerry_function_external(_loopOutDuration); + jerry_object_set_sz(context, "loopOutDuration", loopOutDuration); + jerry_object_set_native_ptr(loopOutDuration, nullptr, exp); + jerry_value_free(loopOutDuration); + + auto key = jerry_function_external(_key); + jerry_object_set_sz(context, "key", key); + jerry_object_set_native_ptr(key, nullptr, exp); + jerry_value_free(key); + + //key(markerName) + + auto nearestKey = jerry_function_external(_nearestKey); + jerry_object_set_native_ptr(nearestKey, nullptr, exp); + jerry_object_set_sz(context, "nearestKey", nearestKey); + jerry_value_free(nearestKey); + + auto numKeys = jerry_number(exp->property->frameCnt()); + jerry_object_set_sz(context, "numKeys", numKeys); + jerry_value_free(numKeys); + + //propertyGroup(countUp = 1) + //propertyIndex + //name + + //content("name"), #look for the named property from a layer + auto data = (ExpContent*)malloc(sizeof(ExpContent)); + data->obj = exp->layer; + data->frameNo = frameNo; + + auto content = jerry_function_external(_content); + jerry_object_set_sz(context, EXP_CONTENT, content); + jerry_object_set_native_ptr(content, &freeCb, data); + jerry_value_free(content); +} + + +static jerry_value_t _comp(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt) +{ + auto comp = static_cast(jerry_object_get_native_ptr(info->function, nullptr)); + LottieLayer* layer; + + auto arg0 = jerry_value_to_string(args[0]); + auto len = jerry_string_length(arg0); + auto name = (jerry_char_t*)alloca(len * sizeof(jerry_char_t) + 1); + jerry_string_to_buffer(arg0, JERRY_ENCODING_UTF8, name, len); + name[len] = '\0'; + + jerry_value_free(arg0); + + layer = comp->asset((char*)name); + + if (!layer) return jerry_undefined(); + + auto obj = jerry_object(); + jerry_object_set_native_ptr(obj, nullptr, layer); + _buildLayer(obj, layer, comp); + + return obj; +} + + +static void _buildMath(jerry_value_t context) +{ + auto bm_mul = jerry_function_external(_mul); + jerry_object_set_sz(context, "$bm_mul", bm_mul); + jerry_value_free(bm_mul); + + auto bm_sum = jerry_function_external(_add); + jerry_object_set_sz(context, "$bm_sum", bm_sum); + jerry_value_free(bm_sum); + + auto bm_add = jerry_function_external(_add); + jerry_object_set_sz(context, "$bm_add", bm_add); + jerry_value_free(bm_add); + + auto bm_sub = jerry_function_external(_sub); + jerry_object_set_sz(context, "$bm_sub", bm_sub); + jerry_value_free(bm_sub); + + auto bm_div = jerry_function_external(_div); + jerry_object_set_sz(context, "$bm_div", bm_div); + jerry_value_free(bm_div); + + auto mul = jerry_function_external(_mul); + jerry_object_set_sz(context, "mul", mul); + jerry_value_free(mul); + + auto sum = jerry_function_external(_add); + jerry_object_set_sz(context, "sum", sum); + jerry_value_free(sum); + + auto add = jerry_function_external(_add); + jerry_object_set_sz(context, "add", add); + jerry_value_free(add); + + auto sub = jerry_function_external(_sub); + jerry_object_set_sz(context, "sub", sub); + jerry_value_free(sub); + + auto div = jerry_function_external(_div); + jerry_object_set_sz(context, "div", div); + jerry_value_free(div); + + auto clamp = jerry_function_external(_clamp); + jerry_object_set_sz(context, "clamp", clamp); + jerry_value_free(clamp); + + auto dot = jerry_function_external(_dot); + jerry_object_set_sz(context, "dot", dot); + jerry_value_free(dot); + + auto cross = jerry_function_external(_cross); + jerry_object_set_sz(context, "cross", cross); + jerry_value_free(cross); + + auto normalize = jerry_function_external(_normalize); + jerry_object_set_sz(context, "normalize", normalize); + jerry_value_free(normalize); + + auto length = jerry_function_external(_length); + jerry_object_set_sz(context, "length", length); + jerry_value_free(length); + + auto random = jerry_function_external(_random); + jerry_object_set_sz(context, "random", random); + jerry_value_free(random); + + auto deg2rad = jerry_function_external(_deg2rad); + jerry_object_set_sz(context, "degreesToRadians", deg2rad); + jerry_value_free(deg2rad); + + auto rad2deg = jerry_function_external(_rad2deg); + jerry_object_set_sz(context, "radiansToDegrees", rad2deg); + jerry_value_free(rad2deg); + + auto linear = jerry_function_external(_linear); + jerry_object_set_sz(context, "linear", linear); + jerry_value_free(linear); + + auto ease = jerry_function_external(_ease); + jerry_object_set_sz(context, "ease", ease); + jerry_value_free(ease); + + auto easeIn = jerry_function_external(_easeIn); + jerry_object_set_sz(context, "easeIn", easeIn); + jerry_value_free(easeIn); + + auto easeOut = jerry_function_external(_easeOut); + jerry_object_set_sz(context, "easeOut", easeOut); + jerry_value_free(easeOut); + + //lookAt +} + + +void LottieExpressions::buildComp(LottieComposition* comp) +{ + jerry_object_set_native_ptr(this->comp, nullptr, comp); + jerry_object_set_native_ptr(thisComp, nullptr, comp); + jerry_object_set_native_ptr(layer, nullptr, comp); + + //marker + //marker.key(index) + //marker.key(name) + //marker.nearestKey(t) + //marker.numKeys + + auto numLayers = jerry_number(comp->root->children.count); + jerry_object_set_sz(thisComp, "numLayers", numLayers); + jerry_value_free(numLayers); + + //activeCamera + + auto width = jerry_number(comp->w); + jerry_object_set_sz(thisComp, EXP_WIDTH, width); + jerry_value_free(width); + + auto height = jerry_number(comp->h); + jerry_object_set_sz(thisComp, EXP_HEIGHT, height); + jerry_value_free(height); + + auto duration = jerry_number(comp->duration()); + jerry_object_set_sz(thisComp, "duration", duration); + jerry_value_free(duration); + + //ntscDropFrame + //displayStartTime + + auto frameDuration = jerry_number(1.0f / comp->frameRate); + jerry_object_set_sz(thisComp, "frameDuration", frameDuration); + jerry_value_free(frameDuration); + + //shutterAngle + //shutterPhase + //bgColor + //pixelAspect + + auto name = jerry_string((jerry_char_t*)comp->name, strlen(comp->name), JERRY_ENCODING_UTF8); + jerry_object_set_sz(thisComp, EXP_NAME, name); + jerry_value_free(name); +} + + +jerry_value_t LottieExpressions::buildGlobal() +{ + global = jerry_current_realm(); + + //comp(name) + comp = jerry_function_external(_comp); + jerry_object_set_sz(global, "comp", comp); + + //footage(name) + + thisComp = jerry_object(); + jerry_object_set_sz(global, "thisComp", thisComp); + + //layer(index) / layer(name) / layer(otherLayer, reIndex) + layer = jerry_function_external(_layer); + jerry_object_set_sz(thisComp, "layer", layer); + + thisLayer = jerry_object(); + jerry_object_set_sz(global, "thisLayer", thisLayer); + + thisProperty = jerry_object(); + jerry_object_set_sz(global, "thisProperty", thisProperty); + + auto effect = jerry_function_external(_effect); + jerry_object_set_sz(global, EXP_EFFECT, effect); + jerry_value_free(effect); + + auto fromCompToSurface = jerry_function_external(_fromCompToSurface); + jerry_object_set_sz(global, "fromCompToSurface", fromCompToSurface); + jerry_value_free(fromCompToSurface); + + auto createPath = jerry_function_external(_createPath); + jerry_object_set_sz(global, "createPath", createPath); + jerry_value_free(createPath); + + //posterizeTime(framesPerSecond) + //value + + return global; +} + + +jerry_value_t LottieExpressions::evaluate(float frameNo, LottieExpression* exp) +{ + buildComp(exp->comp); + + //update global context values + jerry_object_set_native_ptr(thisLayer, nullptr, exp->layer); + _buildLayer(thisLayer, exp->layer, exp->comp); + + jerry_object_set_native_ptr(thisProperty, nullptr, exp->property); + _buildProperty(frameNo, global, exp); + + if (exp->type == LottieProperty::Type::PathSet) _buildPath(thisProperty, exp); + if (exp->object->type == LottieObject::Transform) _buildTransform(global, static_cast(exp->object)); + + //evaluate the code + auto eval = jerry_eval((jerry_char_t *) exp->code, strlen(exp->code), JERRY_PARSE_NO_OPTS); + + if (jerry_value_is_exception(eval) || jerry_value_is_undefined(eval)) { + exp->enabled = false; // The feature is experimental, it will be forcefully turned off if it's incompatible. + return jerry_undefined(); + } + + jerry_value_free(eval); + + return jerry_object_get_sz(global, "$bm_rt"); +} + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +LottieExpressions::~LottieExpressions() +{ + jerry_value_free(thisProperty); + jerry_value_free(thisLayer); + jerry_value_free(layer); + jerry_value_free(thisComp); + jerry_value_free(comp); + jerry_value_free(global); + jerry_cleanup(); +} + + +LottieExpressions::LottieExpressions() +{ + jerry_init(JERRY_INIT_EMPTY); + _buildMath(buildGlobal()); +} + + +void LottieExpressions::update(float curTime) +{ + //time, #current time in seconds + auto time = jerry_number(curTime); + jerry_object_set_sz(global, EXP_TIME, time); + jerry_value_free(time); +} + + +//FIXME: Threads support +#include "tvgTaskScheduler.h" + +LottieExpressions* LottieExpressions::instance() +{ + //FIXME: Threads support + if (TaskScheduler::threads() > 1) { + TVGLOG("LOTTIE", "Lottie Expressions are not supported with tvg threads"); + return nullptr; + } + + if (!exps) exps = new LottieExpressions; + ++engineRefCnt; + return exps; +} + + +void LottieExpressions::retrieve(LottieExpressions* instance) +{ + if (--engineRefCnt == 0) { + delete(instance); + exps = nullptr; + } +} + + +#endif //THORVG_LOTTIE_EXPRESSIONS_SUPPORT + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgLottieExpressions.h b/include/liblvgl/libs/thorvg/tvgLottieExpressions.h new file mode 100644 index 00000000..14819a8f --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgLottieExpressions.h @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_LOTTIE_EXPRESSIONS_H_ +#define _TVG_LOTTIE_EXPRESSIONS_H_ + +#include "tvgCommon.h" + +struct LottieExpression; +struct LottieComposition; +struct RGB24; + +#ifdef THORVG_LOTTIE_EXPRESSIONS_SUPPORT + +#include "jerryscript.h" + + +struct LottieExpressions +{ +public: + template + bool result(float frameNo, NumType& out, LottieExpression* exp) + { + auto bm_rt = evaluate(frameNo, exp); + + if (auto prop = static_cast(jerry_object_get_native_ptr(bm_rt, nullptr))) { + out = (*prop)(frameNo); + } else if (jerry_value_is_number(bm_rt)) { + out = (NumType) jerry_value_as_number(bm_rt); + } else { + TVGERR("LOTTIE", "Failed dispatching a Value!"); + return false; + } + jerry_value_free(bm_rt); + return true; + } + + template + bool result(float frameNo, Point& out, LottieExpression* exp) + { + auto bm_rt = evaluate(frameNo, exp); + + if (jerry_value_is_object(bm_rt)) { + if (auto prop = static_cast(jerry_object_get_native_ptr(bm_rt, nullptr))) { + out = (*prop)(frameNo); + } else { + auto x = jerry_object_get_index(bm_rt, 0); + auto y = jerry_object_get_index(bm_rt, 1); + out.x = jerry_value_as_number(x); + out.y = jerry_value_as_number(y); + jerry_value_free(x); + jerry_value_free(y); + } + } else { + TVGERR("LOTTIE", "Failed dispatching Point!"); + return false; + } + jerry_value_free(bm_rt); + return true; + } + + template + bool result(float frameNo, RGB24& out, LottieExpression* exp) + { + auto bm_rt = evaluate(frameNo, exp); + + if (auto color = static_cast(jerry_object_get_native_ptr(bm_rt, nullptr))) { + out = (*color)(frameNo); + } else { + TVGERR("LOTTIE", "Failed dispatching Color!"); + return false; + } + jerry_value_free(bm_rt); + return true; + } + + template + bool result(float frameNo, Fill* fill, LottieExpression* exp) + { + auto bm_rt = evaluate(frameNo, exp); + + if (auto colorStop = static_cast(jerry_object_get_native_ptr(bm_rt, nullptr))) { + (*colorStop)(frameNo, fill, this); + } else { + TVGERR("LOTTIE", "Failed dispatching ColorStop!"); + return false; + } + jerry_value_free(bm_rt); + return true; + } + + template + bool result(float frameNo, Array& cmds, Array& pts, Matrix* transform, float roundness, LottieExpression* exp) + { + auto bm_rt = evaluate(frameNo, exp); + + if (auto pathset = static_cast(jerry_object_get_native_ptr(bm_rt, nullptr))) { + (*pathset)(frameNo, cmds, pts, transform, roundness); + } else { + TVGERR("LOTTIE", "Failed dispatching PathSet!"); + return false; + } + jerry_value_free(bm_rt); + return true; + } + + void update(float curTime); + + //singleton (no thread safety) + static LottieExpressions* instance(); + static void retrieve(LottieExpressions* instance); + +private: + LottieExpressions(); + ~LottieExpressions(); + + jerry_value_t evaluate(float frameNo, LottieExpression* exp); + jerry_value_t buildGlobal(); + void buildComp(LottieComposition* comp); + + //global object, attributes, methods + jerry_value_t global; + jerry_value_t comp; + jerry_value_t layer; + jerry_value_t thisComp; + jerry_value_t thisLayer; + jerry_value_t thisProperty; +}; + +#else + +struct LottieExpressions +{ + template bool result(TVG_UNUSED float, TVG_UNUSED NumType&, TVG_UNUSED LottieExpression*) { return false; } + template bool result(TVG_UNUSED float, TVG_UNUSED Point&, LottieExpression*) { return false; } + template bool result(TVG_UNUSED float, TVG_UNUSED RGB24&, TVG_UNUSED LottieExpression*) { return false; } + template bool result(TVG_UNUSED float, TVG_UNUSED Fill*, TVG_UNUSED LottieExpression*) { return false; } + template bool result(TVG_UNUSED float, TVG_UNUSED Array&, TVG_UNUSED Array&, TVG_UNUSED Matrix* transform, TVG_UNUSED float, TVG_UNUSED LottieExpression*) { return false; } + void update(TVG_UNUSED float) {} + static LottieExpressions* instance() { return nullptr; } + static void retrieve(TVG_UNUSED LottieExpressions* instance) {} +}; + +#endif //THORVG_LOTTIE_EXPRESSIONS_SUPPORT + +#endif //_TVG_LOTTIE_EXPRESSIONS_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgLottieInterpolator.cpp b/include/liblvgl/libs/thorvg/tvgLottieInterpolator.cpp new file mode 100644 index 00000000..c92bfb6e --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgLottieInterpolator.cpp @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2023 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include +#include "tvgCommon.h" +#include "tvgMath.h" +#include "tvgLottieInterpolator.h" + + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +#define NEWTON_MIN_SLOPE 0.02f +#define NEWTON_ITERATIONS 4 +#define SUBDIVISION_PRECISION 0.0000001f +#define SUBDIVISION_MAX_ITERATIONS 10 + + +static inline float _constA(float aA1, float aA2) { return 1.0f - 3.0f * aA2 + 3.0f * aA1; } +static inline float _constB(float aA1, float aA2) { return 3.0f * aA2 - 6.0f * aA1; } +static inline float _constC(float aA1) { return 3.0f * aA1; } + + +static inline float _getSlope(float t, float aA1, float aA2) +{ + return 3.0f * _constA(aA1, aA2) * t * t + 2.0f * _constB(aA1, aA2) * t + _constC(aA1); +} + + +static inline float _calcBezier(float t, float aA1, float aA2) +{ + return ((_constA(aA1, aA2) * t + _constB(aA1, aA2)) * t + _constC(aA1)) * t; +} + + +float LottieInterpolator::getTForX(float aX) +{ + //Find interval where t lies + auto intervalStart = 0.0f; + auto currentSample = &samples[1]; + auto lastSample = &samples[SPLINE_TABLE_SIZE - 1]; + + for (; currentSample != lastSample && *currentSample <= aX; ++currentSample) { + intervalStart += SAMPLE_STEP_SIZE; + } + + --currentSample; // t now lies between *currentSample and *currentSample+1 + + // Interpolate to provide an initial guess for t + auto dist = (aX - *currentSample) / (*(currentSample + 1) - *currentSample); + auto guessForT = intervalStart + dist * SAMPLE_STEP_SIZE; + + // Check the slope to see what strategy to use. If the slope is too small + // Newton-Raphson iteration won't converge on a root so we use bisection + // instead. + auto initialSlope = _getSlope(guessForT, outTangent.x, inTangent.x); + if (initialSlope >= NEWTON_MIN_SLOPE) return NewtonRaphsonIterate(aX, guessForT); + else if (initialSlope == 0.0) return guessForT; + else return binarySubdivide(aX, intervalStart, intervalStart + SAMPLE_STEP_SIZE); +} + + +float LottieInterpolator::binarySubdivide(float aX, float aA, float aB) +{ + float x, t; + int i = 0; + + do { + t = aA + (aB - aA) / 2.0f; + x = _calcBezier(t, outTangent.x, inTangent.x) - aX; + if (x > 0.0f) aB = t; + else aA = t; + } while (fabsf(x) > SUBDIVISION_PRECISION && ++i < SUBDIVISION_MAX_ITERATIONS); + return t; +} + + +float LottieInterpolator::NewtonRaphsonIterate(float aX, float aGuessT) +{ + // Refine guess with Newton-Raphson iteration + for (int i = 0; i < NEWTON_ITERATIONS; ++i) { + // We're trying to find where f(t) = aX, + // so we're actually looking for a root for: CalcBezier(t) - aX + auto currentX = _calcBezier(aGuessT, outTangent.x, inTangent.x) - aX; + auto currentSlope = _getSlope(aGuessT, outTangent.x, inTangent.x); + if (currentSlope == 0.0f) return aGuessT; + aGuessT -= currentX / currentSlope; + } + return aGuessT; +} + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +float LottieInterpolator::progress(float t) +{ + if (outTangent.x == outTangent.y && inTangent.x == inTangent.y) return t; + return _calcBezier(getTForX(t), outTangent.y, inTangent.y); +} + + +void LottieInterpolator::set(const char* key, Point& inTangent, Point& outTangent) +{ + this->key = strdup(key); + this->inTangent = inTangent; + this->outTangent = outTangent; + + if (outTangent.x == outTangent.y && inTangent.x == inTangent.y) return; + + //calculates sample values + for (int i = 0; i < SPLINE_TABLE_SIZE; ++i) { + samples[i] = _calcBezier(float(i) * SAMPLE_STEP_SIZE, outTangent.x, inTangent.x); + } +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgLottieInterpolator.h b/include/liblvgl/libs/thorvg/tvgLottieInterpolator.h new file mode 100644 index 00000000..c7d07d47 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgLottieInterpolator.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_LOTTIE_INTERPOLATOR_H_ +#define _TVG_LOTTIE_INTERPOLATOR_H_ + +#define SPLINE_TABLE_SIZE 11 + +struct LottieInterpolator +{ + char* key; + Point outTangent, inTangent; + + float progress(float t); + void set(const char* key, Point& inTangent, Point& outTangent); + +private: + static constexpr float SAMPLE_STEP_SIZE = 1.0f / float(SPLINE_TABLE_SIZE - 1); + float samples[SPLINE_TABLE_SIZE]; + + float getTForX(float aX); + float binarySubdivide(float aX, float aA, float aB); + float NewtonRaphsonIterate(float aX, float aGuessT); +}; + +#endif //_TVG_LOTTIE_INTERPOLATOR_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgLottieLoader.cpp b/include/liblvgl/libs/thorvg/tvgLottieLoader.cpp new file mode 100644 index 00000000..8230211a --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgLottieLoader.cpp @@ -0,0 +1,407 @@ +/* + * Copyright (c) 2023 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgLottieLoader.h" +#include "tvgLottieModel.h" +#include "tvgLottieParser.h" +#include "tvgLottieBuilder.h" +#include "tvgStr.h" + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +void LottieLoader::run(unsigned tid) +{ + //update frame + if (comp) { + builder->update(comp, frameNo); + //initial loading + } else { + LottieParser parser(content, dirName); + if (!parser.parse()) return; + comp = parser.comp; + builder->build(comp); + } + rebuild = false; +} + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +LottieLoader::LottieLoader() : FrameModule(FileType::Lottie), builder(new LottieBuilder) +{ + +} + + +LottieLoader::~LottieLoader() +{ + this->done(); + + if (copy) free((char*)content); + free(dirName); + + //TODO: correct position? + delete(comp); + delete(builder); +} + + +bool LottieLoader::header() +{ + //A single thread doesn't need to perform intensive tasks. + if (TaskScheduler::threads() == 0) { + run(0); + if (comp) { + w = static_cast(comp->w); + h = static_cast(comp->h); + frameDuration = comp->duration(); + frameCnt = comp->frameCnt(); + return true; + } else { + return false; + } + } + + //Quickly validate the given Lottie file without parsing in order to get the animation info. + auto startFrame = 0.0f; + auto endFrame = 0.0f; + auto frameRate = 0.0f; + uint32_t depth = 0; + + auto p = content; + + while (*p != '\0') { + if (*p == '{') { + ++depth; + ++p; + continue; + } + if (*p == '}') { + --depth; + ++p; + continue; + } + if (depth != 1) { + ++p; + continue; + } + //version. + if (!strncmp(p, "\"v\":", 4)) { + p += 4; + continue; + } + + //framerate + if (!strncmp(p, "\"fr\":", 5)) { + p += 5; + auto e = strstr(p, ","); + if (!e) e = strstr(p, "}"); + frameRate = strToFloat(p, nullptr); + p = e; + continue; + } + + //start frame + if (!strncmp(p, "\"ip\":", 5)) { + p += 5; + auto e = strstr(p, ","); + if (!e) e = strstr(p, "}"); + startFrame = strToFloat(p, nullptr); + p = e; + continue; + } + + //end frame + if (!strncmp(p, "\"op\":", 5)) { + p += 5; + auto e = strstr(p, ","); + if (!e) e = strstr(p, "}"); + endFrame = strToFloat(p, nullptr); + p = e; + continue; + } + + //width + if (!strncmp(p, "\"w\":", 4)) { + p += 4; + auto e = strstr(p, ","); + if (!e) e = strstr(p, "}"); + w = strToFloat(p, nullptr); + p = e; + continue; + } + //height + if (!strncmp(p, "\"h\":", 4)) { + p += 4; + auto e = strstr(p, ","); + if (!e) e = strstr(p, "}"); + h = strToFloat(p, nullptr); + p = e; + continue; + } + ++p; + } + + if (frameRate < FLOAT_EPSILON) { + TVGLOG("LOTTIE", "Not a Lottie file? Frame rate is 0!"); + return false; + } + + frameCnt = (endFrame - startFrame); + frameDuration = frameCnt / frameRate; + + TVGLOG("LOTTIE", "info: frame rate = %f, duration = %f size = %f x %f", frameRate, frameDuration, w, h); + + return true; +} + + +bool LottieLoader::open(const char* data, uint32_t size, bool copy) +{ + if (copy) { + content = (char*)malloc(size); + if (!content) return false; + memcpy((char*)content, data, size); + } else content = data; + + this->size = size; + this->copy = copy; + + return header(); +} + + +bool LottieLoader::open(const string& path) +{ + auto f = fopen(path.c_str(), "r"); + if (!f) return false; + + fseek(f, 0, SEEK_END); + + size = ftell(f); + if (size == 0) { + fclose(f); + return false; + } + + auto content = (char*)(malloc(sizeof(char) * size + 1)); + fseek(f, 0, SEEK_SET); + auto ret = fread(content, sizeof(char), size, f); + if (ret < size) { + fclose(f); + return false; + } + content[size] = '\0'; + + fclose(f); + + this->dirName = strDirname(path.c_str()); + this->content = content; + this->copy = true; + + return header(); +} + + +bool LottieLoader::resize(Paint* paint, float w, float h) +{ + if (!paint) return false; + + auto sx = w / this->w; + auto sy = h / this->h; + Matrix m = {sx, 0, 0, 0, sy, 0, 0, 0, 1}; + paint->transform(m); + + //apply the scale to the base clipper + const Paint* clipper; + paint->composite(&clipper); + if (clipper) const_cast(clipper)->transform(m); + + return true; +} + + +bool LottieLoader::read() +{ + if (!content || size == 0) return false; + + //the loading has been already completed + if (comp || !LoadModule::read()) return true; + + TaskScheduler::request(this); + + return true; +} + + +Paint* LottieLoader::paint() +{ + done(); + + if (!comp) return nullptr; + comp->initiated = true; + return comp->root->scene; +} + + +bool LottieLoader::override(const char* slot) +{ + if (!comp) done(); + + if (!comp || comp->slots.count == 0) return false; + + auto success = true; + + //override slots + if (slot) { + //Copy the input data because the JSON parser will encode the data immediately. + auto temp = strdup(slot); + + //parsing slot json + LottieParser parser(temp, dirName); + + auto idx = 0; + while (auto sid = parser.sid(idx == 0)) { + for (auto s = comp->slots.begin(); s < comp->slots.end(); ++s) { + if (strcmp((*s)->sid, sid)) continue; + if (!parser.apply(*s)) success = false; + break; + } + ++idx; + } + + if (idx < 1) success = false; + free(temp); + rebuild = overridden = success; + //reset slots + } else if (overridden) { + for (auto s = comp->slots.begin(); s < comp->slots.end(); ++s) { + (*s)->reset(); + } + overridden = false; + rebuild = true; + } + return success; +} + + +bool LottieLoader::frame(float no) +{ + auto frameNo = no + startFrame(); + + //This ensures that the target frame number is reached. + frameNo *= 10000.0f; + frameNo = roundf(frameNo); + frameNo *= 0.0001f; + + //Skip update if frame diff is too small. + if (fabsf(this->frameNo - frameNo) <= 0.0009f) return false; + + this->done(); + + this->frameNo = frameNo; + + TaskScheduler::request(this); + + return true; +} + + +float LottieLoader::startFrame() +{ + return frameCnt * segmentBegin; +} + + +float LottieLoader::totalFrame() +{ + return (segmentEnd - segmentBegin) * frameCnt; +} + + +float LottieLoader::curFrame() +{ + return frameNo - startFrame(); +} + + +float LottieLoader::duration() +{ + if (segmentBegin == 0.0f && segmentEnd == 1.0f) return frameDuration; + + if (!comp) done(); + if (!comp) return 0.0f; + + return frameCnt * (segmentEnd - segmentBegin) / comp->frameRate; +} + + +void LottieLoader::sync() +{ + this->done(); + + if (rebuild) run(0); +} + + +uint32_t LottieLoader::markersCnt() +{ + if (!comp) done(); + if (!comp) return 0; + return comp->markers.count; +} + + +const char* LottieLoader::markers(uint32_t index) +{ + if (!comp) done(); + if (!comp || index >= comp->markers.count) return nullptr; + auto marker = comp->markers.begin() + index; + return (*marker)->name; +} + + +bool LottieLoader::segment(const char* marker, float& begin, float& end) +{ + if (!comp) done(); + if (!comp) return false; + + for (auto m = comp->markers.begin(); m < comp->markers.end(); ++m) { + if (!strcmp(marker, (*m)->name)) { + begin = (*m)->time / frameCnt; + end = ((*m)->time + (*m)->duration) / frameCnt; + return true; + } + } + return false; +} + + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgLottieLoader.h b/include/liblvgl/libs/thorvg/tvgLottieLoader.h new file mode 100644 index 00000000..57ad9b7f --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgLottieLoader.h @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2023 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_LOTTIE_LOADER_H_ +#define _TVG_LOTTIE_LOADER_H_ + +#include "tvgCommon.h" +#include "tvgFrameModule.h" +#include "tvgTaskScheduler.h" + +struct LottieComposition; +struct LottieBuilder; + +class LottieLoader : public FrameModule, public Task +{ +public: + const char* content = nullptr; //lottie file data + uint32_t size = 0; //lottie data size + float frameNo = 0.0f; //current frame number + float frameCnt = 0.0f; + float frameDuration = 0.0f; + + LottieBuilder* builder; + LottieComposition* comp = nullptr; + + char* dirName = nullptr; //base resource directory + bool copy = false; //"content" is owned by this loader + bool overridden = false; //overridden properties with slots + bool rebuild = false; //require building the lottie scene + + LottieLoader(); + ~LottieLoader(); + + bool open(const string& path) override; + bool open(const char* data, uint32_t size, bool copy) override; + bool resize(Paint* paint, float w, float h) override; + bool read() override; + Paint* paint() override; + bool override(const char* slot); + + //Frame Controls + bool frame(float no) override; + float totalFrame() override; + float curFrame() override; + float duration() override; + void sync() override; + + //Marker Supports + uint32_t markersCnt(); + const char* markers(uint32_t index); + bool segment(const char* marker, float& begin, float& end); + +private: + bool header(); + void clear(); + float startFrame(); + void run(unsigned tid) override; +}; + + +#endif //_TVG_LOTTIELOADER_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgLottieModel.cpp b/include/liblvgl/libs/thorvg/tvgLottieModel.cpp new file mode 100644 index 00000000..5230adb4 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgLottieModel.cpp @@ -0,0 +1,424 @@ +/* + * Copyright (c) 2023 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgPaint.h" +#include "tvgFill.h" +#include "tvgLottieModel.h" + + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +void LottieSlot::reset() +{ + if (!overridden) return; + + for (auto pair = pairs.begin(); pair < pairs.end(); ++pair) { + switch (type) { + case LottieProperty::Type::ColorStop: { + static_cast(pair->obj)->colorStops.release(); + static_cast(pair->obj)->colorStops = *static_cast(pair->prop); + static_cast(pair->prop)->frames = nullptr; + break; + } + case LottieProperty::Type::Color: { + static_cast(pair->obj)->color.release(); + static_cast(pair->obj)->color = *static_cast(pair->prop); + static_cast(pair->prop)->frames = nullptr; + break; + } + case LottieProperty::Type::TextDoc: { + static_cast(pair->obj)->doc.release(); + static_cast(pair->obj)->doc = *static_cast(pair->prop); + static_cast(pair->prop)->frames = nullptr; + break; + } + default: break; + } + delete(pair->prop); + pair->prop = nullptr; + } + overridden = false; +} + + +void LottieSlot::assign(LottieObject* target) +{ + //apply slot object to all targets + for (auto pair = pairs.begin(); pair < pairs.end(); ++pair) { + //backup the original properties before overwriting + switch (type) { + case LottieProperty::Type::ColorStop: { + if (!overridden) { + pair->prop = new LottieColorStop; + *static_cast(pair->prop) = static_cast(pair->obj)->colorStops; + } + + pair->obj->override(&static_cast(target)->colorStops); + break; + } + case LottieProperty::Type::Color: { + if (!overridden) { + pair->prop = new LottieColor; + *static_cast(pair->prop) = static_cast(pair->obj)->color; + } + + pair->obj->override(&static_cast(target)->color); + break; + } + case LottieProperty::Type::TextDoc: { + if (!overridden) { + pair->prop = new LottieTextDoc; + *static_cast(pair->prop) = static_cast(pair->obj)->doc; + } + + pair->obj->override(&static_cast(target)->doc); + break; + } + default: break; + } + } + overridden = true; +} + + +LottieImage::~LottieImage() +{ + free(b64Data); + free(mimeType); + + if (picture && PP(picture)->unref() == 0) { + delete(picture); + } +} + + +void LottieTrimpath::segment(float frameNo, float& start, float& end, LottieExpressions* exps) +{ + auto s = this->start(frameNo, exps) * 0.01f; + auto e = this->end(frameNo, exps) * 0.01f; + auto o = fmodf(this->offset(frameNo, exps), 360.0f) / 360.0f; //0 ~ 1 + + auto diff = fabs(s - e); + if (mathZero(diff)) { + start = 0.0f; + end = 0.0f; + return; + } + if (mathEqual(diff, 1.0f) || mathEqual(diff, 2.0f)) { + start = 0.0f; + end = 1.0f; + return; + } + + s += o; + e += o; + + auto loop = true; + + //no loop + if (s > 1.0f && e > 1.0f) loop = false; + if (s < 0.0f && e < 0.0f) loop = false; + if (s >= 0.0f && s <= 1.0f && e >= 0.0f && e <= 1.0f) loop = false; + + if (s > 1.0f) s -= 1.0f; + if (s < 0.0f) s += 1.0f; + if (e > 1.0f) e -= 1.0f; + if (e < 0.0f) e += 1.0f; + + if (loop) { + start = s > e ? s : e; + end = s < e ? s : e; + } else { + start = s < e ? s : e; + end = s > e ? s : e; + } +} + + +uint32_t LottieGradient::populate(ColorStop& color) +{ + colorStops.populated = true; + if (!color.input) return 0; + + uint32_t alphaCnt = (color.input->count - (colorStops.count * 4)) / 2; + Array output(colorStops.count + alphaCnt); + uint32_t cidx = 0; //color count + uint32_t clast = colorStops.count * 4; + if (clast > color.input->count) clast = color.input->count; + uint32_t aidx = clast; //alpha count + Fill::ColorStop cs; + + //merge color stops. + for (uint32_t i = 0; i < color.input->count; ++i) { + if (cidx == clast || aidx == color.input->count) break; + if ((*color.input)[cidx] == (*color.input)[aidx]) { + cs.offset = (*color.input)[cidx]; + cs.r = lroundf((*color.input)[cidx + 1] * 255.0f); + cs.g = lroundf((*color.input)[cidx + 2] * 255.0f); + cs.b = lroundf((*color.input)[cidx + 3] * 255.0f); + cs.a = lroundf((*color.input)[aidx + 1] * 255.0f); + cidx += 4; + aidx += 2; + } else if ((*color.input)[cidx] < (*color.input)[aidx]) { + cs.offset = (*color.input)[cidx]; + cs.r = lroundf((*color.input)[cidx + 1] * 255.0f); + cs.g = lroundf((*color.input)[cidx + 2] * 255.0f); + cs.b = lroundf((*color.input)[cidx + 3] * 255.0f); + //generate alpha value + if (output.count > 0) { + auto p = ((*color.input)[cidx] - output.last().offset) / ((*color.input)[aidx] - output.last().offset); + cs.a = mathLerp(output.last().a, lroundf((*color.input)[aidx + 1] * 255.0f), p); + } else cs.a = 255; + cidx += 4; + } else { + cs.offset = (*color.input)[aidx]; + cs.a = lroundf((*color.input)[aidx + 1] * 255.0f); + //generate color value + if (output.count > 0) { + auto p = ((*color.input)[aidx] - output.last().offset) / ((*color.input)[cidx] - output.last().offset); + cs.r = mathLerp(output.last().r, lroundf((*color.input)[cidx + 1] * 255.0f), p); + cs.g = mathLerp(output.last().g, lroundf((*color.input)[cidx + 2] * 255.0f), p); + cs.b = mathLerp(output.last().b, lroundf((*color.input)[cidx + 3] * 255.0f), p); + } else cs.r = cs.g = cs.b = 255; + aidx += 2; + } + output.push(cs); + } + + //color remains + while (cidx + 3 < clast) { + cs.offset = (*color.input)[cidx]; + cs.r = lroundf((*color.input)[cidx + 1] * 255.0f); + cs.g = lroundf((*color.input)[cidx + 2] * 255.0f); + cs.b = lroundf((*color.input)[cidx + 3] * 255.0f); + cs.a = (output.count > 0) ? output.last().a : 255; + output.push(cs); + cidx += 4; + } + + //alpha remains + while (aidx < color.input->count) { + cs.offset = (*color.input)[aidx]; + cs.a = lroundf((*color.input)[aidx + 1] * 255.0f); + if (output.count > 0) { + cs.r = output.last().r; + cs.g = output.last().g; + cs.b = output.last().b; + } else cs.r = cs.g = cs.b = 255; + output.push(cs); + aidx += 2; + } + + color.data = output.data; + output.data = nullptr; + + color.input->reset(); + delete(color.input); + + return output.count; +} + + +Fill* LottieGradient::fill(float frameNo, LottieExpressions* exps) +{ + Fill* fill = nullptr; + auto s = start(frameNo, exps); + auto e = end(frameNo, exps); + + //Linear Gradient + if (id == 1) { + fill = LinearGradient::gen().release(); + static_cast(fill)->linear(s.x, s.y, e.x, e.y); + } + //Radial Gradient + if (id == 2) { + fill = RadialGradient::gen().release(); + + auto w = fabsf(e.x - s.x); + auto h = fabsf(e.y - s.y); + auto r = (w > h) ? (w + 0.375f * h) : (h + 0.375f * w); + auto progress = this->height(frameNo, exps) * 0.01f; + + if (mathZero(progress)) { + P(static_cast(fill))->radial(s.x, s.y, r, s.x, s.y, 0.0f); + } else { + if (mathEqual(progress, 1.0f)) progress = 0.99f; + auto startAngle = mathRad2Deg(atan2(e.y - s.y, e.x - s.x)); + auto angle = mathDeg2Rad((startAngle + this->angle(frameNo, exps))); + auto fx = s.x + cos(angle) * progress * r; + auto fy = s.y + sin(angle) * progress * r; + // Lottie doesn't have any focal radius concept + P(static_cast(fill))->radial(s.x, s.y, r, fx, fy, 0.0f); + } + } + + if (!fill) return nullptr; + + colorStops(frameNo, fill, exps); + + return fill; +} + + +void LottieGroup::prepare(LottieObject::Type type) +{ + LottieObject::type = type; + + if (children.count == 0) return; + + size_t strokeCnt = 0; + size_t fillCnt = 0; + + for (auto c = children.end() - 1; c >= children.begin(); --c) { + auto child = static_cast(*c); + + if (child->type == LottieObject::Type::Trimpath) trimpath = true; + + /* Figure out if this group is a simple path drawing. + In that case, the rendering context can be sharable with the parent's. */ + if (allowMerge && (child->type == LottieObject::Group || !child->mergeable())) allowMerge = false; + + if (reqFragment) continue; + + /* Figure out if the rendering context should be fragmented. + Multiple stroking or grouping with a stroking would occur this. + This fragment resolves the overlapped stroke outlines. */ + if (child->type == LottieObject::Group && !child->mergeable()) { + if (strokeCnt > 0 || fillCnt > 0) reqFragment = true; + } else if (child->type == LottieObject::SolidStroke || child->type == LottieObject::GradientStroke) { + if (strokeCnt > 0) reqFragment = true; + else ++strokeCnt; + } else if (child->type == LottieObject::SolidFill || child->type == LottieObject::GradientFill) { + if (fillCnt > 0) reqFragment = true; + else ++fillCnt; + } + } + + //Reverse the drawing order if this group has a trimpath. + if (!trimpath) return; + + for (uint32_t i = 0; i < children.count - 1; ) { + auto child2 = children[i + 1]; + if (!child2->mergeable() || child2->type == LottieObject::Transform) { + i += 2; + continue; + } + auto child = children[i]; + if (!child->mergeable() || child->type == LottieObject::Transform) { + i++; + continue; + } + children[i] = child2; + children[i + 1] = child; + i++; + } +} + + +LottieLayer::~LottieLayer() +{ + if (refId) { + //No need to free assets children because the Composition owns them. + children.clear(); + free(refId); + } + + for (auto m = masks.begin(); m < masks.end(); ++m) { + delete(*m); + } + + delete(matte.target); + delete(transform); +} + +void LottieLayer::prepare() +{ + /* if layer is hidden, only useful data is its transform matrix. + so force it to be a Null Layer and release all resource. */ + if (hidden) { + type = LottieLayer::Null; + for (auto p = children.begin(); p < children.end(); ++p) delete(*p); + children.reset(); + return; + } + LottieGroup::prepare(LottieObject::Layer); +} + + +float LottieLayer::remap(float frameNo, LottieExpressions* exp) +{ + if (timeRemap.frames || timeRemap.value) { + frameNo = comp->frameAtTime(timeRemap(frameNo, exp)); + } else { + frameNo -= startFrame; + } + return (frameNo / timeStretch); +} + + +LottieComposition::~LottieComposition() +{ + if (!initiated && root) delete(root->scene); + + delete(root); + free(version); + free(name); + + //delete interpolators + for (auto i = interpolators.begin(); i < interpolators.end(); ++i) { + free((*i)->key); + free(*i); + } + + //delete assets + for (auto a = assets.begin(); a < assets.end(); ++a) { + delete(*a); + } + + //delete fonts + for (auto f = fonts.begin(); f < fonts.end(); ++f) { + delete(*f); + } + + //delete slots + for (auto s = slots.begin(); s < slots.end(); ++s) { + delete(*s); + } + + for (auto m = markers.begin(); m < markers.end(); ++m) { + delete(*m); + } +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgLottieModel.h b/include/liblvgl/libs/thorvg/tvgLottieModel.h new file mode 100644 index 00000000..6ba92de1 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgLottieModel.h @@ -0,0 +1,685 @@ +/* + * Copyright (c) 2023 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_LOTTIE_MODEL_H_ +#define _TVG_LOTTIE_MODEL_H_ + +#include + +#include "tvgCommon.h" +#include "tvgRender.h" +#include "tvgLottieProperty.h" + + +struct LottieComposition; + +struct LottieStroke +{ + struct DashAttr + { + //0: offset, 1: dash, 2: gap + LottieFloat value[3] = {0.0f, 0.0f, 0.0f}; + }; + + virtual ~LottieStroke() + { + delete(dashattr); + } + + LottieFloat& dash(int no) + { + if (!dashattr) dashattr = new DashAttr; + return dashattr->value[no]; + } + + float dashOffset(float frameNo, LottieExpressions* exps) + { + return dash(0)(frameNo, exps); + } + + float dashGap(float frameNo, LottieExpressions* exps) + { + return dash(2)(frameNo, exps); + } + + float dashSize(float frameNo, LottieExpressions* exps) + { + auto d = dash(1)(frameNo, exps); + if (d == 0.0f) return 0.1f; + else return d; + } + + LottieFloat width = 0.0f; + DashAttr* dashattr = nullptr; + float miterLimit = 0; + StrokeCap cap = StrokeCap::Round; + StrokeJoin join = StrokeJoin::Round; +}; + + +struct LottieMask +{ + LottiePathSet pathset; + LottieOpacity opacity = 255; + CompositeMethod method; + bool inverse = false; +}; + + +struct LottieObject +{ + enum Type : uint8_t + { + Composition = 0, + Layer, + Group, + Transform, + SolidFill, + SolidStroke, + GradientFill, + GradientStroke, + Rect, + Ellipse, + Path, + Polystar, + Image, + Trimpath, + Text, + Repeater, + RoundedCorner + }; + + virtual ~LottieObject() + { + free(name); + } + + virtual void override(LottieProperty* prop) + { + TVGERR("LOTTIE", "Unsupported slot type"); + } + + virtual bool mergeable() { return false; } + + char* name = nullptr; + Type type; + bool hidden = false; //remove? +}; + + +struct LottieGlyph +{ + Array children; //glyph shapes. + float width; + char* code; + char* family = nullptr; + char* style = nullptr; + uint16_t size; + uint8_t len; + + void prepare() + { + len = strlen(code); + } + + ~LottieGlyph() + { + for (auto p = children.begin(); p < children.end(); ++p) delete(*p); + free(code); + } +}; + + +struct LottieFont +{ + enum Origin : uint8_t { Local = 0, CssURL, ScriptURL, FontURL, Embedded }; + + ~LottieFont() + { + for (auto c = chars.begin(); c < chars.end(); ++c) delete(*c); + free(style); + free(family); + free(name); + } + + Array chars; + char* name = nullptr; + char* family = nullptr; + char* style = nullptr; + float ascent = 0.0f; + Origin origin = Embedded; +}; + +struct LottieMarker +{ + char* name = nullptr; + float time = 0.0f; + float duration = 0.0f; + + ~LottieMarker() + { + free(name); + } +}; + +struct LottieText : LottieObject +{ + void prepare() + { + LottieObject::type = LottieObject::Text; + } + + void override(LottieProperty* prop) override + { + this->doc = *static_cast(prop); + this->prepare(); + } + + LottieTextDoc doc; + LottieFont* font; + LottieFloat spacing = 0.0f; //letter spacing +}; + + +struct LottieTrimpath : LottieObject +{ + enum Type : uint8_t { Simultaneous = 1, Individual = 2 }; + + void prepare() + { + LottieObject::type = LottieObject::Trimpath; + } + + bool mergeable() override + { + if (!start.frames && start.value == 0.0f && !end.frames && end.value == 100.0f && !offset.frames && offset.value == 0.0f) return true; + return false; + } + + void segment(float frameNo, float& start, float& end, LottieExpressions* exps); + + LottieFloat start = 0.0f; + LottieFloat end = 100.0f; + LottieFloat offset = 0.0f; + Type type = Simultaneous; +}; + + +struct LottieShape : LottieObject +{ + virtual ~LottieShape() {} + uint8_t direction = 0; //0: clockwise, 2: counter-clockwise, 3: xor(?) + + bool mergeable() override + { + return true; + } +}; + + +struct LottieRoundedCorner : LottieObject +{ + void prepare() + { + LottieObject::type = LottieObject::RoundedCorner; + } + LottieFloat radius = 0.0f; +}; + + +struct LottiePath : LottieShape +{ + void prepare() + { + LottieObject::type = LottieObject::Path; + } + + LottiePathSet pathset; +}; + + +struct LottieRect : LottieShape +{ + void prepare() + { + LottieObject::type = LottieObject::Rect; + } + + LottiePosition position = Point{0.0f, 0.0f}; + LottiePoint size = Point{0.0f, 0.0f}; + LottieFloat radius = 0.0f; //rounded corner radius +}; + + +struct LottiePolyStar : LottieShape +{ + enum Type : uint8_t {Star = 1, Polygon}; + + void prepare() + { + LottieObject::type = LottieObject::Polystar; + } + + LottiePosition position = Point{0.0f, 0.0f}; + LottieFloat innerRadius = 0.0f; + LottieFloat outerRadius = 0.0f; + LottieFloat innerRoundness = 0.0f; + LottieFloat outerRoundness = 0.0f; + LottieFloat rotation = 0.0f; + LottieFloat ptsCnt = 0.0f; + Type type = Polygon; +}; + + +struct LottieEllipse : LottieShape +{ + void prepare() + { + LottieObject::type = LottieObject::Ellipse; + } + + LottiePosition position = Point{0.0f, 0.0f}; + LottiePoint size = Point{0.0f, 0.0f}; +}; + + +struct LottieTransform : LottieObject +{ + struct SeparateCoord + { + LottieFloat x = 0.0f; + LottieFloat y = 0.0f; + }; + + struct RotationEx + { + LottieFloat x = 0.0f; + LottieFloat y = 0.0f; + }; + + ~LottieTransform() + { + delete(coords); + delete(rotationEx); + } + + void prepare() + { + LottieObject::type = LottieObject::Transform; + } + + bool mergeable() override + { + if (!opacity.frames && opacity.value == 255) return true; + return false; + } + + LottiePosition position = Point{0.0f, 0.0f}; + LottieFloat rotation = 0.0f; //z rotation + LottiePoint scale = Point{100.0f, 100.0f}; + LottiePoint anchor = Point{0.0f, 0.0f}; + LottieOpacity opacity = 255; + LottieFloat skewAngle = 0.0f; + LottieFloat skewAxis = 0.0f; + + SeparateCoord* coords = nullptr; //either a position or separate coordinates + RotationEx* rotationEx = nullptr; //extension for 3d rotation +}; + + +struct LottieSolid : LottieObject +{ + LottieColor color = RGB24{255, 255, 255}; + LottieOpacity opacity = 255; +}; + + +struct LottieSolidStroke : LottieSolid, LottieStroke +{ + void prepare() + { + LottieObject::type = LottieObject::SolidStroke; + } + + void override(LottieProperty* prop) override + { + this->color = *static_cast(prop); + this->prepare(); + } +}; + + +struct LottieSolidFill : LottieSolid +{ + void prepare() + { + LottieObject::type = LottieObject::SolidFill; + } + + void override(LottieProperty* prop) override + { + this->color = *static_cast(prop); + this->prepare(); + } + + FillRule rule = FillRule::Winding; +}; + + +struct LottieGradient : LottieObject +{ + bool prepare() + { + if (!colorStops.populated) { + if (colorStops.frames) { + for (auto v = colorStops.frames->begin(); v < colorStops.frames->end(); ++v) { + colorStops.count = populate(v->value); + } + } else { + colorStops.count = populate(colorStops.value); + } + } + if (start.frames || end.frames || height.frames || angle.frames || opacity.frames || colorStops.frames) return true; + return false; + } + + uint32_t populate(ColorStop& color); + Fill* fill(float frameNo, LottieExpressions* exps); + + LottiePoint start = Point{0.0f, 0.0f}; + LottiePoint end = Point{0.0f, 0.0f}; + LottieFloat height = 0.0f; + LottieFloat angle = 0.0f; + LottieOpacity opacity = 255; + LottieColorStop colorStops; + uint8_t id = 0; //1: linear, 2: radial +}; + + +struct LottieGradientFill : LottieGradient +{ + void prepare() + { + LottieObject::type = LottieObject::GradientFill; + LottieGradient::prepare(); + } + + void override(LottieProperty* prop) override + { + this->colorStops = *static_cast(prop); + this->prepare(); + } + + FillRule rule = FillRule::Winding; +}; + + +struct LottieGradientStroke : LottieGradient, LottieStroke +{ + void prepare() + { + LottieObject::type = LottieObject::GradientStroke; + LottieGradient::prepare(); + } + + void override(LottieProperty* prop) override + { + this->colorStops = *static_cast(prop); + this->prepare(); + } +}; + + +struct LottieImage : LottieObject +{ + union { + char* b64Data = nullptr; + char* path; + }; + char* mimeType = nullptr; + uint32_t size = 0; + + Picture* picture = nullptr; //tvg render data + + ~LottieImage(); + + void prepare() + { + LottieObject::type = LottieObject::Image; + } +}; + + +struct LottieRepeater : LottieObject +{ + void prepare() + { + LottieObject::type = LottieObject::Repeater; + } + + LottieFloat copies = 0.0f; + LottieFloat offset = 0.0f; + + //Transform + LottiePosition position = Point{0.0f, 0.0f}; + LottieFloat rotation = 0.0f; + LottiePoint scale = Point{100.0f, 100.0f}; + LottiePoint anchor = Point{0.0f, 0.0f}; + LottieOpacity startOpacity = 255; + LottieOpacity endOpacity = 255; + bool inorder = true; //true: higher, false: lower +}; + + +struct LottieGroup : LottieObject +{ + virtual ~LottieGroup() + { + for (auto p = children.begin(); p < children.end(); ++p) delete(*p); + } + + void prepare(LottieObject::Type type = LottieObject::Group); + bool mergeable() override { return allowMerge; } + + LottieObject* content(const char* id) + { + if (name && !strcmp(name, id)) return this; + + //source has children, find recursively. + for (auto c = children.begin(); c < children.end(); ++c) { + auto child = *c; + if (child->type == LottieObject::Type::Group || child->type == LottieObject::Type::Layer) { + if (auto ret = static_cast(child)->content(id)) return ret; + } else if (child->name && !strcmp(child->name, id)) return child; + } + return nullptr; + } + + Scene* scene = nullptr; //tvg render data + Array children; + + bool reqFragment = false; //requirement to fragment the render context + bool buildDone = false; //completed in building the composition. + bool allowMerge = true; //if this group is consisted of simple (transformed) shapes. + bool trimpath = false; //this group has a trimpath. +}; + + +struct LottieLayer : LottieGroup +{ + enum Type : uint8_t {Precomp = 0, Solid, Image, Null, Shape, Text}; + + ~LottieLayer(); + + uint8_t opacity(float frameNo) + { + //return zero if the visibility is false. + if (type == Null) return 255; + return transform->opacity(frameNo); + } + + bool mergeable() override { return false; } + + void prepare(); + float remap(float frameNo, LottieExpressions* exp); + + struct { + CompositeMethod type = CompositeMethod::None; + LottieLayer* target = nullptr; + } matte; + + BlendMethod blendMethod = BlendMethod::Normal; + LottieLayer* parent = nullptr; + LottieFloat timeRemap = 0.0f; + LottieComposition* comp = nullptr; + LottieTransform* transform = nullptr; + Array masks; + RGB24 color; //used by Solid layer + + float timeStretch = 1.0f; + float w = 0.0f, h = 0.0f; + float inFrame = 0.0f; + float outFrame = 0.0f; + float startFrame = 0.0f; + char* refId = nullptr; //pre-composition reference. + int16_t pid = -1; //id of the parent layer. + int16_t id = -1; //id of the current layer. + + //cached data + struct { + float frameNo = -1.0f; + Matrix matrix; + uint8_t opacity; + } cache; + + Type type = Null; + bool autoOrient = false; + bool matteSrc = false; +}; + + +struct LottieSlot +{ + struct Pair { + LottieObject* obj; + LottieProperty* prop; + }; + + void assign(LottieObject* target); + void reset(); + + LottieSlot(char* sid, LottieObject* obj, LottieProperty::Type type) : sid(sid), type(type) + { + pairs.push({obj, 0}); + } + + ~LottieSlot() + { + free(sid); + if (!overridden) return; + for (auto pair = pairs.begin(); pair < pairs.end(); ++pair) { + delete(pair->prop); + } + } + + char* sid; + Array pairs; + LottieProperty::Type type; + bool overridden = false; +}; + + +struct LottieComposition +{ + ~LottieComposition(); + + float duration() const + { + return frameCnt() / frameRate; // in second + } + + float frameAtTime(float timeInSec) const + { + auto p = timeInSec / duration(); + if (p < 0.0f) p = 0.0f; + return p * frameCnt(); + } + + float timeAtFrame(float frameNo) + { + return (frameNo - startFrame) / frameRate; + } + + float frameCnt() const + { + return endFrame - startFrame; + } + + LottieLayer* layer(const char* name) + { + for (auto child = root->children.begin(); child < root->children.end(); ++child) { + auto layer = static_cast(*child); + if (layer->name && !strcmp(layer->name, name)) return layer; + } + return nullptr; + } + + LottieLayer* layer(int16_t id) + { + for (auto child = root->children.begin(); child < root->children.end(); ++child) { + auto layer = static_cast(*child); + if (layer->id == id) return layer; + } + return nullptr; + } + + LottieLayer* asset(const char* name) + { + for (auto asset = assets.begin(); asset < assets.end(); ++asset) { + auto layer = static_cast(*asset); + if (layer->name && !strcmp(layer->name, name)) return layer; + } + return nullptr; + } + + LottieLayer* root = nullptr; + char* version = nullptr; + char* name = nullptr; + float w, h; + float startFrame, endFrame; + float frameRate; + Array assets; + Array interpolators; + Array fonts; + Array slots; + Array markers; + bool expressions = false; + bool initiated = false; +}; + +#endif //_TVG_LOTTIE_MODEL_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgLottieParser.cpp b/include/liblvgl/libs/thorvg/tvgLottieParser.cpp new file mode 100644 index 00000000..17e43b09 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgLottieParser.cpp @@ -0,0 +1,1405 @@ +/* + * Copyright (c) 2023 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgStr.h" +#include "tvgCompressor.h" +#include "tvgLottieModel.h" +#include "tvgLottieParser.h" +#include "tvgLottieExpressions.h" + + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +#define KEY_AS(name) !strcmp(key, name) + + +static LottieExpression* _expression(char* code, LottieComposition* comp, LottieLayer* layer, LottieObject* object, LottieProperty* property, LottieProperty::Type type) +{ + if (!comp->expressions) comp->expressions = true; + + auto inst = new LottieExpression; + inst->code = code; + inst->comp = comp; + inst->layer = layer; + inst->object = object; + inst->property = property; + inst->type = type; + inst->enabled = true; + + return inst; +} + + +static char* _int2str(int num) +{ + char str[20]; + snprintf(str, 20, "%d", num); + return strdup(str); +} + + +CompositeMethod LottieParser::getMaskMethod(bool inversed) +{ + auto mode = getString(); + if (!mode) return CompositeMethod::None; + + switch (mode[0]) { + case 'a': { + if (inversed) return CompositeMethod::InvAlphaMask; + else return CompositeMethod::AddMask; + } + case 's': return CompositeMethod::SubtractMask; + case 'i': return CompositeMethod::IntersectMask; + case 'f': return CompositeMethod::DifferenceMask; + default: return CompositeMethod::None; + } +} + + +BlendMethod LottieParser::getBlendMethod() +{ + switch (getInt()) { + case 0: return BlendMethod::Normal; + case 1: return BlendMethod::Multiply; + case 2: return BlendMethod::Screen; + case 3: return BlendMethod::Overlay; + case 4: return BlendMethod::Darken; + case 5: return BlendMethod::Lighten; + case 6: return BlendMethod::ColorDodge; + case 7: return BlendMethod::ColorBurn; + case 8: return BlendMethod::HardLight; + case 9: return BlendMethod::SoftLight; + case 10: return BlendMethod::Difference; + case 11: return BlendMethod::Exclusion; + //case 12: return BlendMethod::Hue: + //case 13: return BlendMethod::Saturation: + //case 14: return BlendMethod::Color: + //case 15: return BlendMethod::Luminosity: + case 16: return BlendMethod::Add; + //case 17: return BlendMethod::HardMix: + default: { + TVGERR("LOTTIE", "Non-Supported Blend Mode"); + return BlendMethod::Normal; + } + } +} + + +RGB24 LottieParser::getColor(const char *str) +{ + RGB24 color = {0, 0, 0}; + + if (!str) return color; + + auto len = strlen(str); + + // some resource has empty color string, return a default color for those cases. + if (len != 7 || str[0] != '#') return color; + + char tmp[3] = {'\0', '\0', '\0'}; + tmp[0] = str[1]; + tmp[1] = str[2]; + color.rgb[0] = uint8_t(strtol(tmp, nullptr, 16)); + + tmp[0] = str[3]; + tmp[1] = str[4]; + color.rgb[1] = uint8_t(strtol(tmp, nullptr, 16)); + + tmp[0] = str[5]; + tmp[1] = str[6]; + color.rgb[2] = uint8_t(strtol(tmp, nullptr, 16)); + + return color; +} + + +FillRule LottieParser::getFillRule() +{ + switch (getInt()) { + case 1: return FillRule::Winding; + default: return FillRule::EvenOdd; + } +} + + +CompositeMethod LottieParser::getMatteType() +{ + switch (getInt()) { + case 1: return CompositeMethod::AlphaMask; + case 2: return CompositeMethod::InvAlphaMask; + case 3: return CompositeMethod::LumaMask; + case 4: return CompositeMethod::InvLumaMask; + default: return CompositeMethod::None; + } +} + + +StrokeCap LottieParser::getStrokeCap() +{ + switch (getInt()) { + case 1: return StrokeCap::Butt; + case 2: return StrokeCap::Round; + default: return StrokeCap::Square; + } +} + + +StrokeJoin LottieParser::getStrokeJoin() +{ + switch (getInt()) { + case 1: return StrokeJoin::Miter; + case 2: return StrokeJoin::Round; + default: return StrokeJoin::Bevel; + } +} + + +void LottieParser::getValue(TextDocument& doc) +{ + enterObject(); + while (auto key = nextObjectKey()) { + if (KEY_AS("s")) doc.size = getFloat(); + else if (KEY_AS("f")) doc.name = getStringCopy(); + else if (KEY_AS("t")) doc.text = getStringCopy(); + else if (KEY_AS("j")) doc.justify = getInt(); + else if (KEY_AS("tr")) doc.tracking = getFloat() * 0.1f; + else if (KEY_AS("lh")) doc.height = getFloat(); + else if (KEY_AS("ls")) doc.shift = getFloat(); + else if (KEY_AS("fc")) getValue(doc.color); + else if (KEY_AS("ps")) getValue(doc.bbox.pos); + else if (KEY_AS("sz")) getValue(doc.bbox.size); + else if (KEY_AS("sc")) getValue(doc.stroke.color); + else if (KEY_AS("sw")) doc.stroke.width = getFloat(); + else if (KEY_AS("of")) doc.stroke.render = getBool(); + else skip(key); + } +} + + +void LottieParser::getValue(PathSet& path) +{ + Array outs, ins, pts; + bool closed = false; + + /* The shape object could be wrapped by a array + if its part of the keyframe object */ + auto arrayWrapper = (peekType() == kArrayType) ? true : false; + if (arrayWrapper) enterArray(); + + enterObject(); + while (auto key = nextObjectKey()) { + if (KEY_AS("i")) getValue(ins); + else if (KEY_AS("o")) getValue(outs); + else if (KEY_AS("v")) getValue(pts); + else if (KEY_AS("c")) closed = getBool(); + else skip(key); + } + + //exit properly from the array + if (arrayWrapper) nextArrayValue(); + + //valid path data? + if (ins.empty() || outs.empty() || pts.empty()) return; + if (ins.count != outs.count || outs.count != pts.count) return; + + //convert path + auto out = outs.begin(); + auto in = ins.begin(); + auto pt = pts.begin(); + + //Store manipulated results + Array outPts; + Array outCmds; + + //Reuse the buffers + outPts.data = path.pts; + outPts.reserved = path.ptsCnt; + outCmds.data = path.cmds; + outCmds.reserved = path.cmdsCnt; + + size_t extra = closed ? 3 : 0; + outPts.reserve(pts.count * 3 + 1 + extra); + outCmds.reserve(pts.count + 2); + + outCmds.push(PathCommand::MoveTo); + outPts.push(*pt); + + for (++pt, ++out, ++in; pt < pts.end(); ++pt, ++out, ++in) { + outCmds.push(PathCommand::CubicTo); + outPts.push(*(pt - 1) + *(out - 1)); + outPts.push(*pt + *in); + outPts.push(*pt); + } + + if (closed) { + outPts.push(pts.last() + outs.last()); + outPts.push(pts.first() + ins.first()); + outPts.push(pts.first()); + outCmds.push(PathCommand::CubicTo); + outCmds.push(PathCommand::Close); + } + + path.pts = outPts.data; + path.cmds = outCmds.data; + path.ptsCnt = outPts.count; + path.cmdsCnt = outCmds.count; + + outPts.data = nullptr; + outCmds.data = nullptr; +} + + +void LottieParser::getValue(ColorStop& color) +{ + if (peekType() == kArrayType) enterArray(); + + color.input = new Array(static_cast(context.parent)->colorStops.count); + + while (nextArrayValue()) color.input->push(getFloat()); +} + + +void LottieParser::getValue(Array& pts) +{ + enterArray(); + while (nextArrayValue()) { + enterArray(); + Point pt; + getValue(pt); + pts.push(pt); + } +} + + +void LottieParser::getValue(uint8_t& val) +{ + if (peekType() == kArrayType) { + enterArray(); + if (nextArrayValue()) val = (uint8_t)(getFloat() * 2.55f); + //discard rest + while (nextArrayValue()) getFloat(); + } else { + val = (uint8_t)(getFloat() * 2.55f); + } +} + + +void LottieParser::getValue(float& val) +{ + if (peekType() == kArrayType) { + enterArray(); + if (nextArrayValue()) val = getFloat(); + //discard rest + while (nextArrayValue()) getFloat(); + } else { + val = getFloat(); + } +} + + +void LottieParser::getValue(Point& pt) +{ + int i = 0; + auto ptr = (float*)(&pt); + + if (peekType() == kArrayType) enterArray(); + + while (nextArrayValue()) { + auto val = getFloat(); + if (i < 2) ptr[i++] = val; + } +} + + +void LottieParser::getValue(RGB24& color) +{ + int i = 0; + + if (peekType() == kArrayType) enterArray(); + + while (nextArrayValue()) { + auto val = getFloat(); + if (i < 3) color.rgb[i++] = int32_t(lroundf(val * 255.0f)); + } + + //TODO: color filter? +} + + +void LottieParser::getInterpolatorPoint(Point& pt) +{ + enterObject(); + while (auto key = nextObjectKey()) { + if (KEY_AS("x")) getValue(pt.x); + else if (KEY_AS("y")) getValue(pt.y); + } +} + + +template +void LottieParser::parseSlotProperty(T& prop) +{ + while (auto key = nextObjectKey()) { + if (KEY_AS("p")) parseProperty(prop); + else skip(key); + } +} + + +template +bool LottieParser::parseTangent(const char *key, LottieVectorFrame& value) +{ + if (KEY_AS("ti")) { + value.hasTangent = true; + getValue(value.inTangent); + } else if (KEY_AS("to")) { + value.hasTangent = true; + getValue(value.outTangent); + } else return false; + + return true; +} + + +template +bool LottieParser::parseTangent(const char *key, LottieScalarFrame& value) +{ + return false; +} + + +LottieInterpolator* LottieParser::getInterpolator(const char* key, Point& in, Point& out) +{ + char buf[20]; + + if (!key) { + snprintf(buf, sizeof(buf), "%.2f_%.2f_%.2f_%.2f", in.x, in.y, out.x, out.y); + key = buf; + } + + LottieInterpolator* interpolator = nullptr; + + //get a cached interpolator if it has any. + for (auto i = comp->interpolators.begin(); i < comp->interpolators.end(); ++i) { + if (!strncmp((*i)->key, key, sizeof(buf))) interpolator = *i; + } + + //new interpolator + if (!interpolator) { + interpolator = static_cast(malloc(sizeof(LottieInterpolator))); + interpolator->set(key, in, out); + comp->interpolators.push(interpolator); + } + + return interpolator; +} + + +template +void LottieParser::parseKeyFrame(T& prop) +{ + Point inTangent, outTangent; + const char* interpolatorKey = nullptr; + auto& frame = prop.newFrame(); + auto interpolator = false; + + enterObject(); + + while (auto key = nextObjectKey()) { + if (KEY_AS("i")) { + interpolator = true; + getInterpolatorPoint(inTangent); + } else if (KEY_AS("o")) { + getInterpolatorPoint(outTangent); + } else if (KEY_AS("n")) { + if (peekType() == kStringType) { + interpolatorKey = getString(); + } else { + enterArray(); + while (nextArrayValue()) { + if (!interpolatorKey) interpolatorKey = getString(); + else skip(nullptr); + } + } + } else if (KEY_AS("t")) { + frame.no = getFloat(); + } else if (KEY_AS("s")) { + getValue(frame.value); + } else if (KEY_AS("e")) { + //current end frame and the next start frame is duplicated, + //We propagate the end value to the next frame to avoid having duplicated values. + auto& frame2 = prop.nextFrame(); + getValue(frame2.value); + } else if (parseTangent(key, frame)) { + continue; + } else if (KEY_AS("h")) { + frame.hold = getInt(); + } else skip(key); + } + + if (interpolator) { + frame.interpolator = getInterpolator(interpolatorKey, inTangent, outTangent); + } +} + +template +void LottieParser::parsePropertyInternal(T& prop) +{ + //single value property + if (peekType() == kNumberType) { + getValue(prop.value); + //multi value property + } else { + //TODO: Here might be a single frame. + //Can we figure out the frame number in advance? + enterArray(); + while (nextArrayValue()) { + //keyframes value + if (peekType() == kObjectType) { + parseKeyFrame(prop); + //multi value property with no keyframes + } else { + getValue(prop.value); + break; + } + } + prop.prepare(); + } +} + + +template +void LottieParser::parseProperty(T& prop, LottieObject* obj) +{ + enterObject(); + while (auto key = nextObjectKey()) { + if (KEY_AS("k")) parsePropertyInternal(prop); + else if (obj && KEY_AS("sid")) { + auto sid = getStringCopy(); + //append object if the slot already exists. + for (auto slot = comp->slots.begin(); slot < comp->slots.end(); ++slot) { + if (strcmp((*slot)->sid, sid)) continue; + (*slot)->pairs.push({obj, 0}); + return; + } + comp->slots.push(new LottieSlot(sid, obj, type)); + } else if (!strcmp(key, "x")) { + prop.exp = _expression(getStringCopy(), comp, context.layer, context.parent, &prop, type); + } + else skip(key); + } +} + + +LottieRect* LottieParser::parseRect() +{ + auto rect = new LottieRect; + if (!rect) return nullptr; + + context.parent = rect; + + while (auto key = nextObjectKey()) { + if (KEY_AS("s")) parseProperty(rect->size); + else if (KEY_AS("p"))parseProperty(rect->position); + else if (KEY_AS("r")) parseProperty(rect->radius); + else if (KEY_AS("nm")) rect->name = getStringCopy(); + else if (KEY_AS("hd")) rect->hidden = getBool(); + else skip(key); + } + rect->prepare(); + return rect; +} + + +LottieEllipse* LottieParser::parseEllipse() +{ + auto ellipse = new LottieEllipse; + if (!ellipse) return nullptr; + + context.parent = ellipse; + + while (auto key = nextObjectKey()) { + if (KEY_AS("nm")) ellipse->name = getStringCopy(); + else if (KEY_AS("p")) parseProperty(ellipse->position); + else if (KEY_AS("s")) parseProperty(ellipse->size); + else if (KEY_AS("hd")) ellipse->hidden = getBool(); + else skip(key); + } + ellipse->prepare(); + return ellipse; +} + + +LottieTransform* LottieParser::parseTransform(bool ddd) +{ + auto transform = new LottieTransform; + if (!transform) return nullptr; + + context.parent = transform; + + if (ddd) { + transform->rotationEx = new LottieTransform::RotationEx; + TVGLOG("LOTTIE", "3D transform(ddd) is not totally compatible."); + } + + while (auto key = nextObjectKey()) { + if (KEY_AS("p")) + { + enterObject(); + while (auto key = nextObjectKey()) { + if (KEY_AS("k")) parsePropertyInternal(transform->position); + else if (KEY_AS("s") && getBool()) transform->coords = new LottieTransform::SeparateCoord; + //check separateCoord to figure out whether "x(expression)" / "x(coord)" + else if (transform->coords && KEY_AS("x")) parseProperty(transform->coords->x); + else if (transform->coords && KEY_AS("y")) parseProperty(transform->coords->y); + else if (KEY_AS("x")) transform->position.exp = _expression(getStringCopy(), comp, context.layer, context.parent, &transform->position, LottieProperty::Type::Position); + else skip(key); + } + } + else if (KEY_AS("a")) parseProperty(transform->anchor); + else if (KEY_AS("s")) parseProperty(transform->scale); + else if (KEY_AS("r")) parseProperty(transform->rotation); + else if (KEY_AS("o")) parseProperty(transform->opacity); + else if (transform->rotationEx && KEY_AS("rx")) parseProperty(transform->rotationEx->x); + else if (transform->rotationEx && KEY_AS("ry")) parseProperty(transform->rotationEx->y); + else if (transform->rotationEx && KEY_AS("rz")) parseProperty(transform->rotation); + else if (KEY_AS("nm")) transform->name = getStringCopy(); + else if (KEY_AS("sk")) parseProperty(transform->skewAngle); + else if (KEY_AS("sa")) parseProperty(transform->skewAxis); + else skip(key); + } + transform->prepare(); + return transform; +} + + +LottieSolidFill* LottieParser::parseSolidFill() +{ + auto fill = new LottieSolidFill; + if (!fill) return nullptr; + + context.parent = fill; + + while (auto key = nextObjectKey()) { + if (KEY_AS("nm")) fill->name = getStringCopy(); + else if (KEY_AS("c")) parseProperty(fill->color, fill); + else if (KEY_AS("o")) parseProperty(fill->opacity, fill); + else if (KEY_AS("fillEnabled")) fill->hidden |= !getBool(); + else if (KEY_AS("r")) fill->rule = getFillRule(); + else if (KEY_AS("hd")) fill->hidden = getBool(); + else skip(key); + } + fill->prepare(); + return fill; +} + + +void LottieParser::parseStrokeDash(LottieStroke* stroke) +{ + enterArray(); + while (nextArrayValue()) { + enterObject(); + int idx = 0; + while (auto key = nextObjectKey()) { + if (KEY_AS("n")) { + auto style = getString(); + if (!strcmp("o", style)) idx = 0; //offset + else if (!strcmp("d", style)) idx = 1; //dash + else if (!strcmp("g", style)) idx = 2; //gap + } else if (KEY_AS("v")) { + parseProperty(stroke->dash(idx)); + } else skip(key); + } + } +} + + +LottieSolidStroke* LottieParser::parseSolidStroke() +{ + auto stroke = new LottieSolidStroke; + if (!stroke) return nullptr; + + context.parent = stroke; + + while (auto key = nextObjectKey()) { + if (KEY_AS("c")) parseProperty(stroke->color, stroke); + else if (KEY_AS("o")) parseProperty(stroke->opacity, stroke); + else if (KEY_AS("w")) parseProperty(stroke->width, stroke); + else if (KEY_AS("lc")) stroke->cap = getStrokeCap(); + else if (KEY_AS("lj")) stroke->join = getStrokeJoin(); + else if (KEY_AS("ml")) stroke->miterLimit = getFloat(); + else if (KEY_AS("nm")) stroke->name = getStringCopy(); + else if (KEY_AS("hd")) stroke->hidden = getBool(); + else if (KEY_AS("fillEnabled")) stroke->hidden |= !getBool(); + else if (KEY_AS("d")) parseStrokeDash(stroke); + else skip(key); + } + stroke->prepare(); + return stroke; +} + + +void LottieParser::getPathSet(LottiePathSet& path) +{ + enterObject(); + while (auto key = nextObjectKey()) { + if (KEY_AS("k")) { + if (peekType() == kArrayType) { + enterArray(); + while (nextArrayValue()) parseKeyFrame(path); + } else { + getValue(path.value); + } + } else if (!strcmp(key, "x")) { + path.exp = _expression(getStringCopy(), comp, context.layer, context.parent, &path, LottieProperty::Type::PathSet); + } else skip(key); + } +} + + +LottiePath* LottieParser::parsePath() +{ + auto path = new LottiePath; + if (!path) return nullptr; + + while (auto key = nextObjectKey()) { + if (KEY_AS("nm")) path->name = getStringCopy(); + else if (KEY_AS("ks")) getPathSet(path->pathset); + else if (KEY_AS("hd")) path->hidden = getBool(); + else skip(key); + } + path->prepare(); + return path; +} + + +LottiePolyStar* LottieParser::parsePolyStar() +{ + auto star = new LottiePolyStar; + if (!star) return nullptr; + + context.parent = star; + + while (auto key = nextObjectKey()) { + if (KEY_AS("nm")) star->name = getStringCopy(); + else if (KEY_AS("p")) parseProperty(star->position); + else if (KEY_AS("pt")) parseProperty(star->ptsCnt); + else if (KEY_AS("ir")) parseProperty(star->innerRadius); + else if (KEY_AS("is")) parseProperty(star->innerRoundness); + else if (KEY_AS("or")) parseProperty(star->outerRadius); + else if (KEY_AS("os")) parseProperty(star->outerRoundness); + else if (KEY_AS("r")) parseProperty(star->rotation); + else if (KEY_AS("sy")) star->type = (LottiePolyStar::Type) getInt(); + else if (KEY_AS("hd")) star->hidden = getBool(); + else skip(key); + } + star->prepare(); + return star; +} + + +LottieRoundedCorner* LottieParser::parseRoundedCorner() +{ + auto corner = new LottieRoundedCorner; + if (!corner) return nullptr; + + context.parent = corner; + + while (auto key = nextObjectKey()) { + if (KEY_AS("nm")) corner->name = getStringCopy(); + else if (KEY_AS("r")) parseProperty(corner->radius); + else if (KEY_AS("hd")) corner->hidden = getBool(); + else skip(key); + } + corner->prepare(); + return corner; +} + + +void LottieParser::parseGradient(LottieGradient* gradient, const char* key) +{ + if (KEY_AS("t")) gradient->id = getInt(); + else if (KEY_AS("o")) parseProperty(gradient->opacity, gradient); + else if (KEY_AS("g")) + { + enterObject(); + while (auto key = nextObjectKey()) { + if (KEY_AS("p")) gradient->colorStops.count = getInt(); + else if (KEY_AS("k")) parseProperty(gradient->colorStops, gradient); + else skip(key); + } + } + else if (KEY_AS("s")) parseProperty(gradient->start, gradient); + else if (KEY_AS("e")) parseProperty(gradient->end, gradient); + else if (KEY_AS("h")) parseProperty(gradient->height, gradient); + else if (KEY_AS("a")) parseProperty(gradient->angle, gradient); + else skip(key); +} + + +LottieGradientFill* LottieParser::parseGradientFill() +{ + auto fill = new LottieGradientFill; + if (!fill) return nullptr; + + context.parent = fill; + + while (auto key = nextObjectKey()) { + if (KEY_AS("nm")) fill->name = getStringCopy(); + else if (KEY_AS("r")) fill->rule = getFillRule(); + else if (KEY_AS("hd")) fill->hidden = getBool(); + else parseGradient(fill, key); + } + + fill->prepare(); + + return fill; +} + + +LottieGradientStroke* LottieParser::parseGradientStroke() +{ + auto stroke = new LottieGradientStroke; + if (!stroke) return nullptr; + + context.parent = stroke; + + while (auto key = nextObjectKey()) { + if (KEY_AS("nm")) stroke->name = getStringCopy(); + else if (KEY_AS("lc")) stroke->cap = getStrokeCap(); + else if (KEY_AS("lj")) stroke->join = getStrokeJoin(); + else if (KEY_AS("ml")) stroke->miterLimit = getFloat(); + else if (KEY_AS("hd")) stroke->hidden = getBool(); + else if (KEY_AS("w")) parseProperty(stroke->width); + else if (KEY_AS("d")) parseStrokeDash(stroke); + else parseGradient(stroke, key); + } + stroke->prepare(); + + return stroke; +} + + +LottieTrimpath* LottieParser::parseTrimpath() +{ + auto trim = new LottieTrimpath; + if (!trim) return nullptr; + + context.parent = trim; + + while (auto key = nextObjectKey()) { + if (KEY_AS("nm")) trim->name = getStringCopy(); + else if (KEY_AS("s")) parseProperty(trim->start); + else if (KEY_AS("e")) parseProperty(trim->end); + else if (KEY_AS("o")) parseProperty(trim->offset); + else if (KEY_AS("m")) trim->type = static_cast(getInt()); + else if (KEY_AS("hd")) trim->hidden = getBool(); + else skip(key); + } + trim->prepare(); + + return trim; +} + + +LottieRepeater* LottieParser::parseRepeater() +{ + auto repeater = new LottieRepeater; + if (!repeater) return nullptr; + + context.parent = repeater; + + while (auto key = nextObjectKey()) { + if (KEY_AS("nm")) repeater->name = getStringCopy(); + else if (KEY_AS("c")) parseProperty(repeater->copies); + else if (KEY_AS("o")) parseProperty(repeater->offset); + else if (KEY_AS("m")) repeater->inorder = getInt(); + else if (KEY_AS("tr")) + { + enterObject(); + while (auto key = nextObjectKey()) { + if (KEY_AS("a")) parseProperty(repeater->anchor); + else if (KEY_AS("p")) parseProperty(repeater->position); + else if (KEY_AS("r")) parseProperty(repeater->rotation); + else if (KEY_AS("s")) parseProperty(repeater->scale); + else if (KEY_AS("so")) parseProperty(repeater->startOpacity); + else if (KEY_AS("eo")) parseProperty(repeater->endOpacity); + else skip(key); + } + } + else if (KEY_AS("hd")) repeater->hidden = getBool(); + else skip(key); + } + repeater->prepare(); + + return repeater; +} + + +LottieObject* LottieParser::parseObject() +{ + auto type = getString(); + if (!type) return nullptr; + + if (!strcmp(type, "gr")) return parseGroup(); + else if (!strcmp(type, "rc")) return parseRect(); + else if (!strcmp(type, "el")) return parseEllipse(); + else if (!strcmp(type, "tr")) return parseTransform(); + else if (!strcmp(type, "fl")) return parseSolidFill(); + else if (!strcmp(type, "st")) return parseSolidStroke(); + else if (!strcmp(type, "sh")) return parsePath(); + else if (!strcmp(type, "sr")) return parsePolyStar(); + else if (!strcmp(type, "rd")) return parseRoundedCorner(); + else if (!strcmp(type, "gf")) return parseGradientFill(); + else if (!strcmp(type, "gs")) return parseGradientStroke(); + else if (!strcmp(type, "tm")) return parseTrimpath(); + else if (!strcmp(type, "rp")) return parseRepeater(); + else if (!strcmp(type, "mm")) TVGERR("LOTTIE", "MergePath(mm) is not supported yet"); + else if (!strcmp(type, "pb")) TVGERR("LOTTIE", "Puker/Bloat(pb) is not supported yet"); + else if (!strcmp(type, "tw")) TVGERR("LOTTIE", "Twist(tw) is not supported yet"); + else if (!strcmp(type, "op")) TVGERR("LOTTIE", "Offset Path(op) is not supported yet"); + else if (!strcmp(type, "zz")) TVGERR("LOTTIE", "Zig Zag(zz) is not supported yet"); + return nullptr; +} + + +void LottieParser::parseObject(Array& parent) +{ + enterObject(); + while (auto key = nextObjectKey()) { + if (KEY_AS("ty")) { + if (auto child = parseObject()) { + if (child->hidden) delete(child); + else parent.push(child); + } + } else skip(key); + } +} + + +LottieImage* LottieParser::parseImage(const char* data, const char* subPath, bool embedded) +{ + //Used for Image Asset + auto image = new LottieImage; + + //embedded image resource. should start with "data:" + //header look like "data:image/png;base64," so need to skip till ','. + if (embedded && !strncmp(data, "data:", 5)) { + //figure out the mimetype + auto mimeType = data + 11; + auto needle = strstr(mimeType, ";"); + image->mimeType = strDuplicate(mimeType, needle - mimeType); + //b64 data + auto b64Data = strstr(data, ",") + 1; + size_t length = strlen(data) - (b64Data - data); + image->size = b64Decode(b64Data, length, &image->b64Data); + //external image resource + } else { + auto len = strlen(dirName) + strlen(subPath) + strlen(data) + 1; + image->path = static_cast(malloc(len)); + snprintf(image->path, len, "%s%s%s", dirName, subPath, data); + } + + image->prepare(); + + return image; +} + + +LottieObject* LottieParser::parseAsset() +{ + enterObject(); + + LottieObject* obj = nullptr; + char *id = nullptr; + + //Used for Image Asset + const char* data = nullptr; + const char* subPath = nullptr; + auto embedded = false; + + while (auto key = nextObjectKey()) { + if (KEY_AS("id")) + { + if (peekType() == kStringType) { + id = getStringCopy(); + } else { + id = _int2str(getInt()); + } + } + else if (KEY_AS("layers")) obj = parseLayers(); + else if (KEY_AS("u")) subPath = getString(); + else if (KEY_AS("p")) data = getString(); + else if (KEY_AS("e")) embedded = getInt(); + else skip(key); + } + if (data) obj = parseImage(data, subPath, embedded); + if (obj) obj->name = id; + else free(id); + return obj; +} + + +LottieFont* LottieParser::parseFont() +{ + enterObject(); + + auto font = new LottieFont; + + while (auto key = nextObjectKey()) { + if (KEY_AS("fName")) font->name = getStringCopy(); + else if (KEY_AS("fFamily")) font->family = getStringCopy(); + else if (KEY_AS("fStyle")) font->style = getStringCopy(); + else if (KEY_AS("ascent")) font->ascent = getFloat(); + else if (KEY_AS("origin")) font->origin = (LottieFont::Origin) getInt(); + else skip(key); + } + return font; +} + + +void LottieParser::parseAssets() +{ + enterArray(); + while (nextArrayValue()) { + auto asset = parseAsset(); + if (asset) comp->assets.push(asset); + else TVGERR("LOTTIE", "Invalid Asset!"); + } +} + +LottieMarker* LottieParser::parseMarker() +{ + enterObject(); + + auto marker = new LottieMarker; + + while (auto key = nextObjectKey()) { + if (KEY_AS("cm")) marker->name = getStringCopy(); + else if (KEY_AS("tm")) marker->time = getFloat(); + else if (KEY_AS("dr")) marker->duration = getFloat(); + else skip(key); + } + + return marker; +} + +void LottieParser::parseMarkers() +{ + enterArray(); + while (nextArrayValue()) { + comp->markers.push(parseMarker()); + } +} + +void LottieParser::parseChars(Array& glyphs) +{ + enterArray(); + while (nextArrayValue()) { + enterObject(); + //a new glyph + auto glyph = new LottieGlyph; + while (auto key = nextObjectKey()) { + if (KEY_AS("ch")) glyph->code = getStringCopy(); + else if (KEY_AS("size")) glyph->size = static_cast(getFloat()); + else if (KEY_AS("style")) glyph->style = getStringCopy(); + else if (KEY_AS("w")) glyph->width = getFloat(); + else if (KEY_AS("fFamily")) glyph->family = getStringCopy(); + else if (KEY_AS("data")) + { //glyph shapes + enterObject(); + while (auto key = nextObjectKey()) { + if (KEY_AS("shapes")) parseShapes(glyph->children); + } + } else skip(key); + } + glyph->prepare(); + glyphs.push(glyph); + } +} + +void LottieParser::parseFonts() +{ + enterObject(); + while (auto key = nextObjectKey()) { + if (KEY_AS("list")) { + enterArray(); + while (nextArrayValue()) { + comp->fonts.push(parseFont()); + } + } else skip(key); + } +} + + +LottieObject* LottieParser::parseGroup() +{ + auto group = new LottieGroup; + if (!group) return nullptr; + + while (auto key = nextObjectKey()) { + if (KEY_AS("nm")) { + group->name = getStringCopy(); + } else if (KEY_AS("it")) { + enterArray(); + while (nextArrayValue()) parseObject(group->children); + } else skip(key); + } + if (group->children.empty()) { + delete(group); + return nullptr; + } + group->prepare(); + + return group; +} + + +void LottieParser::parseTimeRemap(LottieLayer* layer) +{ + parseProperty(layer->timeRemap); +} + + +uint8_t LottieParser::getDirection() +{ + auto v = getInt(); + if (v == 1) return 0; + if (v == 2) return 3; + if (v == 3) return 2; + return 0; +} + +void LottieParser::parseShapes(Array& parent) +{ + uint8_t direction; + + enterArray(); + while (nextArrayValue()) { + direction = 0; + enterObject(); + while (auto key = nextObjectKey()) { + if (KEY_AS("it")) { + enterArray(); + while (nextArrayValue()) parseObject(parent); + } else if (KEY_AS("d")) { + direction = getDirection(); + } else if (KEY_AS("ty")) { + if (auto child = parseObject()) { + if (child->hidden) delete(child); + else parent.push(child); + if (direction > 0) static_cast(child)->direction = direction; + } + } else skip(key); + } + } +} + + +void LottieParser::parseTextRange(LottieText* text) +{ + enterArray(); + while (nextArrayValue()) { + enterObject(); + while (auto key = nextObjectKey()) { + if (KEY_AS("a")) { //text style + enterObject(); + while (auto key = nextObjectKey()) { + if (KEY_AS("t")) parseProperty(text->spacing); + else skip(key); + } + } else skip(key); + } + } +} + + +void LottieParser::parseText(Array& parent) +{ + enterObject(); + + auto text = new LottieText; + + while (auto key = nextObjectKey()) { + if (KEY_AS("d")) parseProperty(text->doc, text); + else if (KEY_AS("a")) parseTextRange(text); + //else if (KEY_AS("p")) TVGLOG("LOTTIE", "Text Follow Path (p) is not supported"); + //else if (KEY_AS("m")) TVGLOG("LOTTIE", "Text Alignment Option (m) is not supported"); + else skip(key); + } + + text->prepare(); + parent.push(text); +} + + +void LottieParser::getLayerSize(float& val) +{ + if (val == 0.0f) { + val = getFloat(); + } else { + //layer might have both w(width) & sw(solid color width) + //override one if the a new size is smaller. + auto w = getFloat(); + if (w < val) val = w; + } +} + +LottieMask* LottieParser::parseMask() +{ + auto mask = new LottieMask; + if (!mask) return nullptr; + + enterObject(); + while (auto key = nextObjectKey()) { + if (KEY_AS("inv")) mask->inverse = getBool(); + else if (KEY_AS("mode")) mask->method = getMaskMethod(mask->inverse); + else if (KEY_AS("pt")) getPathSet(mask->pathset); + else if (KEY_AS("o")) parseProperty(mask->opacity); + else skip(key); + } + + return mask; +} + + +void LottieParser::parseMasks(LottieLayer* layer) +{ + enterArray(); + while (nextArrayValue()) { + auto mask = parseMask(); + layer->masks.push(mask); + } +} + + +LottieLayer* LottieParser::parseLayer() +{ + auto layer = new LottieLayer; + if (!layer) return nullptr; + + layer->comp = comp; + context.layer = layer; + + auto ddd = false; + + enterObject(); + + while (auto key = nextObjectKey()) { + if (KEY_AS("ddd")) ddd = getInt(); //3d layer + else if (KEY_AS("ind")) layer->id = getInt(); + else if (KEY_AS("ty")) layer->type = (LottieLayer::Type) getInt(); + else if (KEY_AS("nm")) layer->name = getStringCopy(); + else if (KEY_AS("sr")) layer->timeStretch = getFloat(); + else if (KEY_AS("ks")) + { + enterObject(); + layer->transform = parseTransform(ddd); + } + else if (KEY_AS("ao")) layer->autoOrient = getInt(); + else if (KEY_AS("shapes")) parseShapes(layer->children); + else if (KEY_AS("ip")) layer->inFrame = getFloat(); + else if (KEY_AS("op")) layer->outFrame = getFloat(); + else if (KEY_AS("st")) layer->startFrame = getFloat(); + else if (KEY_AS("bm")) layer->blendMethod = getBlendMethod(); + else if (KEY_AS("parent")) layer->pid = getInt(); + else if (KEY_AS("tm")) parseTimeRemap(layer); + else if (KEY_AS("w") || KEY_AS("sw")) getLayerSize(layer->w); + else if (KEY_AS("h") || KEY_AS("sh")) getLayerSize(layer->h); + else if (KEY_AS("sc")) layer->color = getColor(getString()); + else if (KEY_AS("tt")) layer->matte.type = getMatteType(); + else if (KEY_AS("masksProperties")) parseMasks(layer); + else if (KEY_AS("hd")) layer->hidden = getBool(); + else if (KEY_AS("refId")) layer->refId = getStringCopy(); + else if (KEY_AS("td")) layer->matteSrc = getInt(); //used for matte layer + else if (KEY_AS("t")) parseText(layer->children); + else if (KEY_AS("ef")) + { + TVGERR("LOTTIE", "layer effect(ef) is not supported!"); + skip(key); + } + else skip(key); + } + + //Not a valid layer + if (!layer->transform) { + delete(layer); + return nullptr; + } + + layer->prepare(); + + return layer; +} + + +LottieLayer* LottieParser::parseLayers() +{ + auto root = new LottieLayer; + if (!root) return nullptr; + + root->type = LottieLayer::Precomp; + root->comp = comp; + + enterArray(); + while (nextArrayValue()) { + if (auto layer = parseLayer()) { + if (layer->matte.type == CompositeMethod::None) { + root->children.push(layer); + } else { + //matte source must be located in the right previous. + auto matte = static_cast(root->children.last()); + if (matte->matteSrc) { + layer->matte.target = matte; + } else { + TVGLOG("LOTTIE", "Matte Source(%s) is not designated?", matte->name); + } + root->children.last() = layer; + } + } + } + root->prepare(); + return root; +} + + +void LottieParser::postProcess(Array& glyphs) +{ + //aggregate font characters + for (uint32_t g = 0; g < glyphs.count; ++g) { + auto glyph = glyphs[g]; + for (uint32_t i = 0; i < comp->fonts.count; ++i) { + auto& font = comp->fonts[i]; + if (!strcmp(font->family, glyph->family) && !strcmp(font->style, glyph->style)) { + font->chars.push(glyph); + free(glyph->family); + free(glyph->style); + break; + } + } + } +} + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +const char* LottieParser::sid(bool first) +{ + if (first) { + //verify json + if (!parseNext()) return nullptr; + enterObject(); + } + return nextObjectKey(); +} + + +bool LottieParser::apply(LottieSlot* slot) +{ + enterObject(); + + //OPTIMIZE: we can create the property directly, without object + LottieObject* obj = nullptr; //slot object + + switch (slot->type) { + case LottieProperty::Type::ColorStop: { + obj = new LottieGradient; + context.parent = obj; + parseSlotProperty(static_cast(obj)->colorStops); + break; + } + case LottieProperty::Type::Color: { + obj = new LottieSolid; + context.parent = obj; + parseSlotProperty(static_cast(obj)->color); + break; + } + case LottieProperty::Type::TextDoc: { + obj = new LottieText; + context.parent = obj; + parseSlotProperty(static_cast(obj)->doc); + break; + } + default: break; + } + + if (!obj || Invalid()) return false; + + slot->assign(obj); + + delete(obj); + + return true; +} + + +bool LottieParser::parse() +{ + //verify json. + if (!parseNext()) return false; + + enterObject(); + + if (comp) delete(comp); + comp = new LottieComposition; + if (!comp) return false; + + Array glyphs; + + while (auto key = nextObjectKey()) { + if (KEY_AS("v")) comp->version = getStringCopy(); + else if (KEY_AS("fr")) comp->frameRate = getFloat(); + else if (KEY_AS("ip")) comp->startFrame = getFloat(); + else if (KEY_AS("op")) comp->endFrame = getFloat(); + else if (KEY_AS("w")) comp->w = getFloat(); + else if (KEY_AS("h")) comp->h = getFloat(); + else if (KEY_AS("nm")) comp->name = getStringCopy(); + else if (KEY_AS("assets")) parseAssets(); + else if (KEY_AS("layers")) comp->root = parseLayers(); + else if (KEY_AS("fonts")) parseFonts(); + else if (KEY_AS("chars")) parseChars(glyphs); + else if (KEY_AS("markers")) parseMarkers(); + else skip(key); + } + + if (Invalid() || !comp->root) { + delete(comp); + return false; + } + + comp->root->inFrame = comp->startFrame; + comp->root->outFrame = comp->endFrame; + + postProcess(glyphs); + + return true; +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgLottieParser.h b/include/liblvgl/libs/thorvg/tvgLottieParser.h new file mode 100644 index 00000000..a8b6b0d9 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgLottieParser.h @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2023 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_LOTTIE_PARSER_H_ +#define _TVG_LOTTIE_PARSER_H_ + +#include "tvgCommon.h" +#include "tvgLottieParserHandler.h" +#include "tvgLottieProperty.h" + +struct LottieParser : LookaheadParserHandler +{ +public: + LottieParser(const char *str, const char* dirName) : LookaheadParserHandler(str) + { + this->dirName = dirName; + } + + bool parse(); + bool apply(LottieSlot* slot); + const char* sid(bool first = false); + + LottieComposition* comp = nullptr; + const char* dirName = nullptr; //base resource directory + +private: + BlendMethod getBlendMethod(); + RGB24 getColor(const char *str); + CompositeMethod getMatteType(); + FillRule getFillRule(); + StrokeCap getStrokeCap(); + StrokeJoin getStrokeJoin(); + CompositeMethod getMaskMethod(bool inversed); + LottieInterpolator* getInterpolator(const char* key, Point& in, Point& out); + uint8_t getDirection(); + + void getInterpolatorPoint(Point& pt); + void getPathSet(LottiePathSet& path); + void getLayerSize(float& val); + void getValue(TextDocument& doc); + void getValue(PathSet& path); + void getValue(Array& pts); + void getValue(ColorStop& color); + void getValue(float& val); + void getValue(uint8_t& val); + void getValue(Point& pt); + void getValue(RGB24& color); + + template bool parseTangent(const char *key, LottieVectorFrame& value); + template bool parseTangent(const char *key, LottieScalarFrame& value); + template void parseKeyFrame(T& prop); + template void parsePropertyInternal(T& prop); + template void parseProperty(T& prop, LottieObject* obj = nullptr); + template void parseSlotProperty(T& prop); + + LottieObject* parseObject(); + LottieObject* parseAsset(); + LottieImage* parseImage(const char* data, const char* subPath, bool embedded); + LottieLayer* parseLayer(); + LottieObject* parseGroup(); + LottieRect* parseRect(); + LottieEllipse* parseEllipse(); + LottieSolidFill* parseSolidFill(); + LottieTransform* parseTransform(bool ddd = false); + LottieSolidStroke* parseSolidStroke(); + LottieGradientStroke* parseGradientStroke(); + LottiePath* parsePath(); + LottiePolyStar* parsePolyStar(); + LottieRoundedCorner* parseRoundedCorner(); + LottieGradientFill* parseGradientFill(); + LottieLayer* parseLayers(); + LottieMask* parseMask(); + LottieTrimpath* parseTrimpath(); + LottieRepeater* parseRepeater(); + LottieFont* parseFont(); + LottieMarker* parseMarker(); + + void parseObject(Array& parent); + void parseShapes(Array& parent); + void parseText(Array& parent); + void parseMasks(LottieLayer* layer); + void parseTimeRemap(LottieLayer* layer); + void parseStrokeDash(LottieStroke* stroke); + void parseGradient(LottieGradient* gradient, const char* key); + void parseTextRange(LottieText* text); + void parseAssets(); + void parseFonts(); + void parseChars(Array& glyphs); + void parseMarkers(); + void postProcess(Array& glyphs); + + //Current parsing context + struct Context { + LottieLayer* layer = nullptr; + LottieObject* parent = nullptr; + } context; +}; + +#endif //_TVG_LOTTIE_PARSER_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgLottieParserHandler.cpp b/include/liblvgl/libs/thorvg/tvgLottieParserHandler.cpp new file mode 100644 index 00000000..4d00441b --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgLottieParserHandler.cpp @@ -0,0 +1,241 @@ +/* + * Copyright (c) 2023 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "tvgLottieParserHandler.h" + + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +static const int PARSE_FLAGS = kParseDefaultFlags | kParseInsituFlag; + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + + +bool LookaheadParserHandler::enterArray() +{ + if (state != kEnteringArray) { + Error(); + return false; + } + parseNext(); + return true; +} + + +bool LookaheadParserHandler::nextArrayValue() +{ + if (state == kExitingArray) { + parseNext(); + return false; + } + //SPECIAL CASE: same as nextObjectKey() + if (state == kExitingObject) return false; + if (state == kError || state == kHasKey) { + Error(); + return false; + } + return true; +} + + +int LookaheadParserHandler::getInt() +{ + if (state != kHasNumber || !val.IsInt()) { + Error(); + return 0; + } + auto result = val.GetInt(); + parseNext(); + return result; +} + + +float LookaheadParserHandler::getFloat() +{ + if (state != kHasNumber) { + Error(); + return 0; + } + auto result = val.GetFloat(); + parseNext(); + return result; +} + + +const char* LookaheadParserHandler::getString() +{ + if (state != kHasString) { + Error(); + return nullptr; + } + auto result = val.GetString(); + parseNext(); + return result; +} + + +char* LookaheadParserHandler::getStringCopy() +{ + auto str = getString(); + if (str) return strdup(str); + return nullptr; +} + + +bool LookaheadParserHandler::getBool() +{ + if (state != kHasBool) { + Error(); + return false; + } + auto result = val.GetBool(); + parseNext(); + return result; +} + + +void LookaheadParserHandler::getNull() +{ + if (state != kHasNull) { + Error(); + return; + } + parseNext(); +} + + +bool LookaheadParserHandler::parseNext() +{ + if (reader.HasParseError()) { + Error(); + return false; + } + if (!reader.IterativeParseNext(iss, *this)) { + Error(); + return false; + } + return true; +} + + +bool LookaheadParserHandler::enterObject() +{ + if (state != kEnteringObject) { + Error(); + return false; + } + parseNext(); + return true; +} + + +int LookaheadParserHandler::peekType() +{ + if (state >= kHasNull && state <= kHasKey) return val.GetType(); + if (state == kEnteringArray) return kArrayType; + if (state == kEnteringObject) return kObjectType; + return -1; +} + + +void LookaheadParserHandler::skipOut(int depth) +{ + do { + if (state == kEnteringArray || state == kEnteringObject) ++depth; + else if (state == kExitingArray || state == kExitingObject) --depth; + else if (state == kError) return; + parseNext(); + } while (depth > 0); +} + + +const char* LookaheadParserHandler::nextObjectKey() +{ + if (state == kHasKey) { + auto result = val.GetString(); + parseNext(); + return result; + } + + /* SPECIAL CASE: The parser works with a predefined rule that it will be only + while (nextObjectKey()) for each object but in case of our nested group + object we can call multiple time nextObjectKey() while exiting the object + so ignore those and don't put parser in the error state. */ + if (state == kExitingArray || state == kEnteringObject) return nullptr; + + if (state != kExitingObject) { + Error(); + return nullptr; + } + + parseNext(); + return nullptr; +} + + +void LookaheadParserHandler::skip(const char* key) +{ + //if (key) TVGLOG("LOTTIE", "Skipped parsing value = %s", key); + + if (peekType() == kArrayType) { + enterArray(); + skipOut(1); + } else if (peekType() == kObjectType) { + enterObject(); + skipOut(1); + } else { + skipOut(0); + } +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgLottieParserHandler.h b/include/liblvgl/libs/thorvg/tvgLottieParserHandler.h new file mode 100644 index 00000000..65b4545a --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgLottieParserHandler.h @@ -0,0 +1,206 @@ +/* + * Copyright (c) 2023 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef _TVG_LOTTIE_PARSER_HANDLER_H_ +#define _TVG_LOTTIE_PARSER_HANDLER_H_ + +#include "rapidjson/document.h" +#include "tvgCommon.h" + + +using namespace rapidjson; + + +struct LookaheadParserHandler +{ + enum LookaheadParsingState { + kInit = 0, + kError, + kHasNull, + kHasBool, + kHasNumber, + kHasString, + kHasKey, + kEnteringObject, + kExitingObject, + kEnteringArray, + kExitingArray + }; + + Value val; + LookaheadParsingState state = kInit; + Reader reader; + InsituStringStream iss; + + LookaheadParserHandler(const char *str) : iss((char*)str) + { + reader.IterativeParseInit(); + } + + bool Null() + { + state = kHasNull; + val.SetNull(); + return true; + } + + bool Bool(bool b) + { + state = kHasBool; + val.SetBool(b); + return true; + } + + bool Int(int i) + { + state = kHasNumber; + val.SetInt(i); + return true; + } + + bool Uint(unsigned u) + { + state = kHasNumber; + val.SetUint(u); + return true; + } + + bool Int64(int64_t i) + { + state = kHasNumber; + val.SetInt64(i); + return true; + } + + bool Uint64(int64_t u) + { + state = kHasNumber; + val.SetUint64(u); + return true; + } + + bool Double(double d) + { + state = kHasNumber; + val.SetDouble(d); + return true; + } + + bool RawNumber(const char *, SizeType, TVG_UNUSED bool) + { + return false; + } + + bool String(const char *str, SizeType length, TVG_UNUSED bool) + { + state = kHasString; + val.SetString(str, length); + return true; + } + + bool StartObject() + { + state = kEnteringObject; + return true; + } + + bool Key(const char *str, SizeType length, TVG_UNUSED bool) + { + state = kHasKey; + val.SetString(str, length); + return true; + } + + bool EndObject(SizeType) + { + state = kExitingObject; + return true; + } + + bool StartArray() + { + state = kEnteringArray; + return true; + } + + bool EndArray(SizeType) + { + state = kExitingArray; + return true; + } + + void Error() + { + TVGERR("LOTTIE", "Parsing Error!"); + state = kError; + } + + bool Invalid() + { + return state == kError; + } + + bool enterObject(); + bool enterArray(); + bool nextArrayValue(); + int getInt(); + float getFloat(); + const char* getString(); + char* getStringCopy(); + bool getBool(); + void getNull(); + bool parseNext(); + const char* nextObjectKey(); + void skip(const char* key); + void skipOut(int depth); + int peekType(); +}; + +#endif //_TVG_LOTTIE_PARSER_HANDLER_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgLottieProperty.h b/include/liblvgl/libs/thorvg/tvgLottieProperty.h new file mode 100644 index 00000000..19df8952 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgLottieProperty.h @@ -0,0 +1,935 @@ +/* + * Copyright (c) 2023 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_LOTTIE_PROPERTY_H_ +#define _TVG_LOTTIE_PROPERTY_H_ + +#include "tvgCommon.h" +#include "tvgArray.h" +#include "tvgMath.h" +#include "tvgLines.h" +#include "tvgLottieInterpolator.h" +#include "tvgLottieExpressions.h" + +#define ROUNDNESS_EPSILON 1.0f + +struct LottieFont; +struct LottieLayer; +struct LottieObject; + + +struct PathSet +{ + Point* pts = nullptr; + PathCommand* cmds = nullptr; + uint16_t ptsCnt = 0; + uint16_t cmdsCnt = 0; +}; + + +struct RGB24 +{ + int32_t rgb[3]; +}; + + +struct ColorStop +{ + Fill::ColorStop* data = nullptr; + Array* input = nullptr; +}; + + +struct TextDocument +{ + char* text = nullptr; + float height; + float shift; + RGB24 color; + struct { + Point pos; + Point size; + } bbox; + struct { + RGB24 color; + float width; + bool render = false; + } stroke; + char* name = nullptr; + float size; + float tracking = 0.0f; + uint8_t justify; +}; + + +static inline RGB24 operator-(const RGB24& lhs, const RGB24& rhs) +{ + return {lhs.rgb[0] - rhs.rgb[0], lhs.rgb[1] - rhs.rgb[1], lhs.rgb[2] - rhs.rgb[2]}; +} + + +static inline RGB24 operator+(const RGB24& lhs, const RGB24& rhs) +{ + return {lhs.rgb[0] + rhs.rgb[0], lhs.rgb[1] + rhs.rgb[1], lhs.rgb[2] + rhs.rgb[2]}; +} + + +static inline RGB24 operator*(const RGB24& lhs, float rhs) +{ + return {(int32_t)lroundf(lhs.rgb[0] * rhs), (int32_t)lroundf(lhs.rgb[1] * rhs), (int32_t)lroundf(lhs.rgb[2] * rhs)}; +} + + +template +struct LottieScalarFrame +{ + T value; //keyframe value + float no; //frame number + LottieInterpolator* interpolator; + bool hold = false; //do not interpolate. + + T interpolate(LottieScalarFrame* next, float frameNo) + { + auto t = (frameNo - no) / (next->no - no); + if (interpolator) t = interpolator->progress(t); + + if (hold) { + if (t < 1.0f) return value; + else return next->value; + } + return mathLerp(value, next->value, t); + } +}; + + +template +struct LottieVectorFrame +{ + T value; //keyframe value + float no; //frame number + LottieInterpolator* interpolator; + T outTangent, inTangent; + float length; + bool hasTangent = false; + bool hold = false; + + T interpolate(LottieVectorFrame* next, float frameNo) + { + auto t = (frameNo - no) / (next->no - no); + if (interpolator) t = interpolator->progress(t); + + if (hold) { + if (t < 1.0f) return value; + else return next->value; + } + + if (hasTangent) { + Bezier bz = {value, value + outTangent, next->value + inTangent, next->value}; + t = bezAtApprox(bz, t * length, length); + return bezPointAt(bz, t); + } else { + return mathLerp(value, next->value, t); + } + } + + float angle(LottieVectorFrame* next, float frameNo) + { + if (!hasTangent) return 0; + auto t = (frameNo - no) / (next->no - no); + if (interpolator) t = interpolator->progress(t); + Bezier bz = {value, value + outTangent, next->value + inTangent, next->value}; + t = bezAtApprox(bz, t * length, length); + return -bezAngleAt(bz, t); + } + + void prepare(LottieVectorFrame* next) + { + Bezier bz = {value, value + outTangent, next->value + inTangent, next->value}; + length = bezLengthApprox(bz); + } +}; + + +//Property would have an either keyframes or single value. +struct LottieProperty +{ + enum class Type : uint8_t { Point = 0, Float, Opacity, Color, PathSet, ColorStop, Position, TextDoc, Invalid }; + virtual ~LottieProperty() {} + + LottieExpression* exp = nullptr; + + //TODO: Apply common bodies? + virtual uint32_t frameCnt() = 0; + virtual uint32_t nearest(float time) = 0; + virtual float frameNo(int32_t key) = 0; +}; + + +struct LottieExpression +{ + enum LoopMode : uint8_t { None = 0, InCycle = 1, InPingPong, InOffset, InContinue, OutCycle, OutPingPong, OutOffset, OutContinue }; + + char* code; + LottieComposition* comp; + LottieLayer* layer; + LottieObject* object; + LottieProperty* property; + LottieProperty::Type type; + + bool enabled; + + struct { + uint32_t key = 0; //the keyframe number repeating to + float in = FLT_MAX; //looping duration in frame number + LoopMode mode = None; + } loop; +; + ~LottieExpression() + { + free(code); + } +}; + + +static void _copy(PathSet* pathset, Array& outPts, Matrix* transform) +{ + Array inPts; + + if (transform) { + for (int i = 0; i < pathset->ptsCnt; ++i) { + Point pt = pathset->pts[i]; + mathMultiply(&pt, transform); + outPts.push(pt); + } + } else { + inPts.data = pathset->pts; + inPts.count = pathset->ptsCnt; + outPts.push(inPts); + inPts.data = nullptr; + } +} + + +static void _copy(PathSet* pathset, Array& outCmds) +{ + Array inCmds; + inCmds.data = pathset->cmds; + inCmds.count = pathset->cmdsCnt; + outCmds.push(inCmds); + inCmds.data = nullptr; +} + + +static void _roundCorner(Array& cmds, Array& pts, const Point& prev, const Point& curr, const Point& next, float roundness) +{ + auto lenPrev = mathLength(prev - curr); + auto rPrev = lenPrev > 0.0f ? 0.5f * mathMin(lenPrev * 0.5f, roundness) / lenPrev : 0.0f; + auto lenNext = mathLength(next - curr); + auto rNext = lenNext > 0.0f ? 0.5f * mathMin(lenNext * 0.5f, roundness) / lenNext : 0.0f; + + auto dPrev = rPrev * (curr - prev); + auto dNext = rNext * (curr - next); + + pts.push(curr - 2.0f * dPrev); + pts.push(curr - dPrev); + pts.push(curr - dNext); + pts.push(curr - 2.0f * dNext); + cmds.push(PathCommand::LineTo); + cmds.push(PathCommand::CubicTo); +} + + +static bool _modifier(Point* inputPts, uint32_t inputPtsCnt, PathCommand* inputCmds, uint32_t inputCmdsCnt, Array& cmds, Array& pts, Matrix* transform, float roundness) +{ + cmds.reserve(inputCmdsCnt * 2); + pts.reserve((uint16_t)(inputPtsCnt * 1.5)); + auto ptsCnt = pts.count; + + auto startIndex = 0; + for (uint32_t iCmds = 0, iPts = 0; iCmds < inputCmdsCnt; ++iCmds) { + switch (inputCmds[iCmds]) { + case PathCommand::MoveTo: { + startIndex = pts.count; + cmds.push(PathCommand::MoveTo); + pts.push(inputPts[iPts++]); + break; + } + case PathCommand::CubicTo: { + auto& prev = inputPts[iPts - 1]; + auto& curr = inputPts[iPts + 2]; + if (iCmds < inputCmdsCnt - 1 && + mathZero(inputPts[iPts - 1] - inputPts[iPts]) && + mathZero(inputPts[iPts + 1] - inputPts[iPts + 2])) { + if (inputCmds[iCmds + 1] == PathCommand::CubicTo && + mathZero(inputPts[iPts + 2] - inputPts[iPts + 3]) && + mathZero(inputPts[iPts + 4] - inputPts[iPts + 5])) { + _roundCorner(cmds, pts, prev, curr, inputPts[iPts + 5], roundness); + iPts += 3; + break; + } else if (inputCmds[iCmds + 1] == PathCommand::Close) { + _roundCorner(cmds, pts, prev, curr, inputPts[2], roundness); + pts[startIndex] = pts.last(); + iPts += 3; + break; + } + } + cmds.push(PathCommand::CubicTo); + pts.push(inputPts[iPts++]); + pts.push(inputPts[iPts++]); + pts.push(inputPts[iPts++]); + break; + } + case PathCommand::Close: { + cmds.push(PathCommand::Close); + break; + } + default: break; + } + } + if (transform) { + for (auto i = ptsCnt; i < pts.count; ++i) + mathTransform(transform, &pts[i]); + } + return true; +} + + +template +uint32_t _bsearch(T* frames, float frameNo) +{ + int32_t low = 0; + int32_t high = int32_t(frames->count) - 1; + + while (low <= high) { + auto mid = low + (high - low) / 2; + auto frame = frames->data + mid; + if (frameNo < frame->no) high = mid - 1; + else low = mid + 1; + } + if (high < low) low = high; + if (low < 0) low = 0; + return low; +} + + +template +uint32_t _nearest(T* frames, float frameNo) +{ + if (frames) { + auto key = _bsearch(frames, frameNo); + if (key == frames->count - 1) return key; + return (fabsf(frames->data[key].no - frameNo) < fabsf(frames->data[key + 1].no - frameNo)) ? key : (key + 1); + } + return 0; +} + + +template +float _frameNo(T* frames, int32_t key) +{ + if (!frames) return 0.0f; + if (key < 0) key = 0; + if (key >= (int32_t) frames->count) key = (int32_t)(frames->count - 1); + return (*frames)[key].no; +} + + +template +float _loop(T* frames, float frameNo, LottieExpression* exp) +{ + if (frameNo >= exp->loop.in || frameNo < frames->first().no ||frameNo < frames->last().no) return frameNo; + + switch (exp->loop.mode) { + case LottieExpression::LoopMode::InCycle: { + frameNo -= frames->first().no; + return fmodf(frameNo, frames->last().no - frames->first().no) + (*frames)[exp->loop.key].no; + } + case LottieExpression::LoopMode::OutCycle: { + frameNo -= frames->first().no; + return fmodf(frameNo, (*frames)[frames->count - 1 - exp->loop.key].no - frames->first().no) + frames->first().no; + } + default: break; + } + return frameNo; +} + + +template +struct LottieGenericProperty : LottieProperty +{ + //Property has an either keyframes or single value. + Array>* frames = nullptr; + T value; + + LottieGenericProperty(T v) : value(v) {} + LottieGenericProperty() {} + + ~LottieGenericProperty() + { + release(); + } + + void release() + { + delete(frames); + frames = nullptr; + if (exp) { + delete(exp); + exp = nullptr; + } + } + + uint32_t nearest(float frameNo) override + { + return _nearest(frames, frameNo); + } + + uint32_t frameCnt() override + { + return frames ? frames->count : 1; + } + + float frameNo(int32_t key) override + { + return _frameNo(frames, key); + } + + LottieScalarFrame& newFrame() + { + if (!frames) frames = new Array>; + if (frames->count + 1 >= frames->reserved) { + auto old = frames->reserved; + frames->grow(frames->count + 2); + memset((void*)(frames->data + old), 0x00, sizeof(LottieScalarFrame) * (frames->reserved - old)); + } + ++frames->count; + return frames->last(); + } + + LottieScalarFrame& nextFrame() + { + return (*frames)[frames->count]; + } + + T operator()(float frameNo) + { + if (!frames) return value; + if (frames->count == 1 || frameNo <= frames->first().no) return frames->first().value; + if (frameNo >= frames->last().no) return frames->last().value; + + auto frame = frames->data + _bsearch(frames, frameNo); + if (mathEqual(frame->no, frameNo)) return frame->value; + return frame->interpolate(frame + 1, frameNo); + } + + T operator()(float frameNo, LottieExpressions* exps) + { + if (exps && (exp && exp->enabled)) { + T out{}; + if (exp->loop.mode != LottieExpression::LoopMode::None) frameNo = _loop(frames, frameNo, exp); + if (exps->result>(frameNo, out, exp)) return out; + } + return operator()(frameNo); + } + + T& operator=(const T& other) + { + //shallow copy, used for slot overriding + if (other.frames) { + frames = other.frames; + const_cast(other).frames = nullptr; + } else value = other.value; + return *this; + } + + float angle(float frameNo) { return 0; } + void prepare() {} +}; + + +struct LottiePathSet : LottieProperty +{ + Array>* frames = nullptr; + PathSet value; + + ~LottiePathSet() + { + release(); + } + + void release() + { + if (exp) { + delete(exp); + exp = nullptr; + } + + free(value.cmds); + free(value.pts); + + if (!frames) return; + + for (auto p = frames->begin(); p < frames->end(); ++p) { + free((*p).value.cmds); + free((*p).value.pts); + } + free(frames->data); + free(frames); + } + + uint32_t nearest(float frameNo) override + { + return _nearest(frames, frameNo); + } + + uint32_t frameCnt() override + { + return frames ? frames->count : 1; + } + + float frameNo(int32_t key) override + { + return _frameNo(frames, key); + } + + LottieScalarFrame& newFrame() + { + if (!frames) { + frames = static_cast>*>(calloc(1, sizeof(Array>))); + } + if (frames->count + 1 >= frames->reserved) { + auto old = frames->reserved; + frames->grow(frames->count + 2); + memset((void*)(frames->data + old), 0x00, sizeof(LottieScalarFrame) * (frames->reserved - old)); + } + ++frames->count; + return frames->last(); + } + + LottieScalarFrame& nextFrame() + { + return (*frames)[frames->count]; + } + + bool operator()(float frameNo, Array& cmds, Array& pts, Matrix* transform, float roundness) + { + PathSet* path = nullptr; + LottieScalarFrame* frame = nullptr; + float t; + bool interpolate = false; + + if (!frames) path = &value; + else if (frames->count == 1 || frameNo <= frames->first().no) path = &frames->first().value; + else if (frameNo >= frames->last().no) path = &frames->last().value; + else { + frame = frames->data + _bsearch(frames, frameNo); + if (mathEqual(frame->no, frameNo)) path = &frame->value; + else { + t = (frameNo - frame->no) / ((frame + 1)->no - frame->no); + if (frame->interpolator) t = frame->interpolator->progress(t); + if (frame->hold) path = &(frame + ((t < 1.0f) ? 0 : 1))->value; + else interpolate = true; + } + } + + if (!interpolate) { + if (roundness > ROUNDNESS_EPSILON) return _modifier(path->pts, path->ptsCnt, path->cmds, path->cmdsCnt, cmds, pts, transform, roundness); + _copy(path, cmds); + _copy(path, pts, transform); + return true; + } + + auto s = frame->value.pts; + auto e = (frame + 1)->value.pts; + + if (roundness > ROUNDNESS_EPSILON) { + auto interpPts = (Point*)malloc(frame->value.ptsCnt * sizeof(Point)); + auto p = interpPts; + for (auto i = 0; i < frame->value.ptsCnt; ++i, ++s, ++e, ++p) { + *p = mathLerp(*s, *e, t); + if (transform) mathMultiply(p, transform); + } + _modifier(interpPts, frame->value.ptsCnt, frame->value.cmds, frame->value.cmdsCnt, cmds, pts, nullptr, roundness); + free(interpPts); + return true; + } else { + for (auto i = 0; i < frame->value.ptsCnt; ++i, ++s, ++e) { + auto pt = mathLerp(*s, *e, t); + if (transform) mathMultiply(&pt, transform); + pts.push(pt); + } + _copy(&frame->value, cmds); + } + return true; + } + + + bool operator()(float frameNo, Array& cmds, Array& pts, Matrix* transform, float roundness, LottieExpressions* exps) + { + if (exps && (exp && exp->enabled)) { + if (exp->loop.mode != LottieExpression::LoopMode::None) frameNo = _loop(frames, frameNo, exp); + if (exps->result(frameNo, cmds, pts, transform, roundness, exp)) return true; + } + return operator()(frameNo, cmds, pts, transform, roundness); + } + + void prepare() {} +}; + + +struct LottieColorStop : LottieProperty +{ + Array>* frames = nullptr; + ColorStop value; + uint16_t count = 0; //colorstop count + bool populated = false; + + ~LottieColorStop() + { + release(); + } + + void release() + { + if (exp) { + delete(exp); + exp = nullptr; + } + + if (value.data) { + free(value.data); + value.data = nullptr; + } + + if (!frames) return; + + for (auto p = frames->begin(); p < frames->end(); ++p) { + free((*p).value.data); + } + free(frames->data); + free(frames); + frames = nullptr; + } + + uint32_t nearest(float frameNo) override + { + return _nearest(frames, frameNo); + } + + uint32_t frameCnt() override + { + return frames ? frames->count : 1; + } + + float frameNo(int32_t key) override + { + return _frameNo(frames, key); + } + + LottieScalarFrame& newFrame() + { + if (!frames) { + frames = static_cast>*>(calloc(1, sizeof(Array>))); + } + if (frames->count + 1 >= frames->reserved) { + auto old = frames->reserved; + frames->grow(frames->count + 2); + memset((void*)(frames->data + old), 0x00, sizeof(LottieScalarFrame) * (frames->reserved - old)); + } + ++frames->count; + return frames->last(); + } + + LottieScalarFrame& nextFrame() + { + return (*frames)[frames->count]; + } + + Result operator()(float frameNo, Fill* fill, LottieExpressions* exps) + { + if (exps && (exp && exp->enabled)) { + if (exp->loop.mode != LottieExpression::LoopMode::None) frameNo = _loop(frames, frameNo, exp); + if (exps->result(frameNo, fill, exp)) return Result::Success; + } + + if (!frames) return fill->colorStops(value.data, count); + + if (frames->count == 1 || frameNo <= frames->first().no) { + return fill->colorStops(frames->first().value.data, count); + } + + if (frameNo >= frames->last().no) return fill->colorStops(frames->last().value.data, count); + + auto frame = frames->data + _bsearch(frames, frameNo); + if (mathEqual(frame->no, frameNo)) return fill->colorStops(frame->value.data, count); + + //interpolate + auto t = (frameNo - frame->no) / ((frame + 1)->no - frame->no); + if (frame->interpolator) t = frame->interpolator->progress(t); + + if (frame->hold) { + if (t < 1.0f) fill->colorStops(frame->value.data, count); + else fill->colorStops((frame + 1)->value.data, count); + } + + auto s = frame->value.data; + auto e = (frame + 1)->value.data; + + Array result; + + for (auto i = 0; i < count; ++i, ++s, ++e) { + auto offset = mathLerp(s->offset, e->offset, t); + auto r = mathLerp(s->r, e->r, t); + auto g = mathLerp(s->g, e->g, t); + auto b = mathLerp(s->b, e->b, t); + auto a = mathLerp(s->a, e->a, t); + result.push({offset, r, g, b, a}); + } + return fill->colorStops(result.data, count); + } + + LottieColorStop& operator=(const LottieColorStop& other) + { + //shallow copy, used for slot overriding + if (other.frames) { + frames = other.frames; + const_cast(other).frames = nullptr; + } else { + value = other.value; + const_cast(other).value = {nullptr, nullptr}; + } + populated = other.populated; + count = other.count; + + return *this; + } + + void prepare() {} +}; + + +struct LottiePosition : LottieProperty +{ + Array>* frames = nullptr; + Point value; + + LottiePosition(Point v) : value(v) + { + } + + ~LottiePosition() + { + release(); + } + + void release() + { + delete(frames); + frames = nullptr; + + if (exp) { + delete(exp); + exp = nullptr; + } + } + + uint32_t nearest(float frameNo) override + { + return _nearest(frames, frameNo); + } + + uint32_t frameCnt() override + { + return frames ? frames->count : 1; + } + + float frameNo(int32_t key) override + { + return _frameNo(frames, key); + } + + LottieVectorFrame& newFrame() + { + if (!frames) frames = new Array>; + if (frames->count + 1 >= frames->reserved) { + auto old = frames->reserved; + frames->grow(frames->count + 2); + memset((void*)(frames->data + old), 0x00, sizeof(LottieVectorFrame) * (frames->reserved - old)); + } + ++frames->count; + return frames->last(); + } + + LottieVectorFrame& nextFrame() + { + return (*frames)[frames->count]; + } + + Point operator()(float frameNo) + { + if (!frames) return value; + if (frames->count == 1 || frameNo <= frames->first().no) return frames->first().value; + if (frameNo >= frames->last().no) return frames->last().value; + + auto frame = frames->data + _bsearch(frames, frameNo); + if (mathEqual(frame->no, frameNo)) return frame->value; + return frame->interpolate(frame + 1, frameNo); + } + + Point operator()(float frameNo, LottieExpressions* exps) + { + Point out{}; + if (exps && (exp && exp->enabled)) { + if (exp->loop.mode != LottieExpression::LoopMode::None) frameNo = _loop(frames, frameNo, exp); + if (exps->result(frameNo, out, exp)) return out; + } + return operator()(frameNo); + } + + float angle(float frameNo) + { + if (!frames) return 0; + if (frames->count == 1 || frameNo <= frames->first().no) return 0; + if (frameNo >= frames->last().no) return 0; + + auto frame = frames->data + _bsearch(frames, frameNo); + return frame->angle(frame + 1, frameNo); + } + + void prepare() + { + if (!frames || frames->count < 2) return; + for (auto frame = frames->begin() + 1; frame < frames->end(); ++frame) { + (frame - 1)->prepare(frame); + } + } +}; + + +struct LottieTextDoc : LottieProperty +{ + Array>* frames = nullptr; + TextDocument value; + + ~LottieTextDoc() + { + release(); + } + + void release() + { + if (exp) { + delete(exp); + exp = nullptr; + } + + if (value.text) { + free(value.text); + value.text = nullptr; + } + if (value.name) { + free(value.name); + value.name = nullptr; + } + + if (!frames) return; + + for (auto p = frames->begin(); p < frames->end(); ++p) { + free((*p).value.text); + free((*p).value.name); + } + delete(frames); + frames = nullptr; + } + + uint32_t nearest(float frameNo) override + { + return _nearest(frames, frameNo); + } + + uint32_t frameCnt() override + { + return frames ? frames->count : 1; + } + + float frameNo(int32_t key) override + { + return _frameNo(frames, key); + } + + LottieScalarFrame& newFrame() + { + if (!frames) frames = new Array>; + if (frames->count + 1 >= frames->reserved) { + auto old = frames->reserved; + frames->grow(frames->count + 2); + memset((void*)(frames->data + old), 0x00, sizeof(LottieScalarFrame) * (frames->reserved - old)); + } + ++frames->count; + return frames->last(); + } + + LottieScalarFrame& nextFrame() + { + return (*frames)[frames->count]; + } + + TextDocument& operator()(float frameNo) + { + if (!frames) return value; + if (frames->count == 1 || frameNo <= frames->first().no) return frames->first().value; + if (frameNo >= frames->last().no) return frames->last().value; + + auto frame = frames->data + _bsearch(frames, frameNo); + return frame->value; + } + + LottieTextDoc& operator=(const LottieTextDoc& other) + { + //shallow copy, used for slot overriding + if (other.frames) { + frames = other.frames; + const_cast(other).frames = nullptr; + } else { + value = other.value; + const_cast(other).value.text = nullptr; + const_cast(other).value.name = nullptr; + } + return *this; + } + + void prepare() {} +}; + + +using LottiePoint = LottieGenericProperty; +using LottieFloat = LottieGenericProperty; +using LottieOpacity = LottieGenericProperty; +using LottieColor = LottieGenericProperty; + +#endif //_TVG_LOTTIE_PROPERTY_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgMath.cpp b/include/liblvgl/libs/thorvg/tvgMath.cpp new file mode 100644 index 00000000..509a459d --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgMath.cpp @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2021 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgMath.h" + + +bool mathInverse(const Matrix* m, Matrix* out) +{ + auto det = m->e11 * (m->e22 * m->e33 - m->e32 * m->e23) - + m->e12 * (m->e21 * m->e33 - m->e23 * m->e31) + + m->e13 * (m->e21 * m->e32 - m->e22 * m->e31); + + if (mathZero(det)) return false; + + auto invDet = 1 / det; + + out->e11 = (m->e22 * m->e33 - m->e32 * m->e23) * invDet; + out->e12 = (m->e13 * m->e32 - m->e12 * m->e33) * invDet; + out->e13 = (m->e12 * m->e23 - m->e13 * m->e22) * invDet; + out->e21 = (m->e23 * m->e31 - m->e21 * m->e33) * invDet; + out->e22 = (m->e11 * m->e33 - m->e13 * m->e31) * invDet; + out->e23 = (m->e21 * m->e13 - m->e11 * m->e23) * invDet; + out->e31 = (m->e21 * m->e32 - m->e31 * m->e22) * invDet; + out->e32 = (m->e31 * m->e12 - m->e11 * m->e32) * invDet; + out->e33 = (m->e11 * m->e22 - m->e21 * m->e12) * invDet; + + return true; +} + + +Matrix mathMultiply(const Matrix* lhs, const Matrix* rhs) +{ + Matrix m; + + m.e11 = lhs->e11 * rhs->e11 + lhs->e12 * rhs->e21 + lhs->e13 * rhs->e31; + m.e12 = lhs->e11 * rhs->e12 + lhs->e12 * rhs->e22 + lhs->e13 * rhs->e32; + m.e13 = lhs->e11 * rhs->e13 + lhs->e12 * rhs->e23 + lhs->e13 * rhs->e33; + + m.e21 = lhs->e21 * rhs->e11 + lhs->e22 * rhs->e21 + lhs->e23 * rhs->e31; + m.e22 = lhs->e21 * rhs->e12 + lhs->e22 * rhs->e22 + lhs->e23 * rhs->e32; + m.e23 = lhs->e21 * rhs->e13 + lhs->e22 * rhs->e23 + lhs->e23 * rhs->e33; + + m.e31 = lhs->e31 * rhs->e11 + lhs->e32 * rhs->e21 + lhs->e33 * rhs->e31; + m.e32 = lhs->e31 * rhs->e12 + lhs->e32 * rhs->e22 + lhs->e33 * rhs->e32; + m.e33 = lhs->e31 * rhs->e13 + lhs->e32 * rhs->e23 + lhs->e33 * rhs->e33; + + return m; +} + + +void mathRotate(Matrix* m, float degree) +{ + if (degree == 0.0f) return; + + auto radian = degree / 180.0f * MATH_PI; + auto cosVal = cosf(radian); + auto sinVal = sinf(radian); + + m->e12 = m->e11 * -sinVal; + m->e11 *= cosVal; + m->e21 = m->e22 * sinVal; + m->e22 *= cosVal; +} + + +bool mathIdentity(const Matrix* m) +{ + if (m->e11 != 1.0f || m->e12 != 0.0f || m->e13 != 0.0f || + m->e21 != 0.0f || m->e22 != 1.0f || m->e23 != 0.0f || + m->e31 != 0.0f || m->e32 != 0.0f || m->e33 != 1.0f) { + return false; + } + return true; +} + + +void mathMultiply(Point* pt, const Matrix* transform) +{ + auto tx = pt->x * transform->e11 + pt->y * transform->e12 + transform->e13; + auto ty = pt->x * transform->e21 + pt->y * transform->e22 + transform->e23; + pt->x = tx; + pt->y = ty; +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgMath.h b/include/liblvgl/libs/thorvg/tvgMath.h new file mode 100644 index 00000000..49bbe186 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgMath.h @@ -0,0 +1,228 @@ +/* + * Copyright (c) 2021 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_MATH_H_ +#define _TVG_MATH_H_ + + #define _USE_MATH_DEFINES + +#include +#include +#include "tvgCommon.h" + +#define MATH_PI 3.14159265358979323846f +#define MATH_PI2 1.57079632679489661923f +#define FLOAT_EPSILON 1.0e-06f //1.192092896e-07f +#define PATH_KAPPA 0.552284f + +#define mathMin(x, y) (((x) < (y)) ? (x) : (y)) +#define mathMax(x, y) (((x) > (y)) ? (x) : (y)) + + +bool mathInverse(const Matrix* m, Matrix* out); +Matrix mathMultiply(const Matrix* lhs, const Matrix* rhs); +void mathRotate(Matrix* m, float degree); +bool mathIdentity(const Matrix* m); +void mathMultiply(Point* pt, const Matrix* transform); + + +static inline float mathDeg2Rad(float degree) +{ + return degree * (MATH_PI / 180.0f); +} + + +static inline float mathRad2Deg(float radian) +{ + return radian * (180.0f / MATH_PI); +} + + +static inline bool mathZero(float a) +{ + return (fabsf(a) <= FLOAT_EPSILON) ? true : false; +} + + +static inline bool mathZero(const Point& p) +{ + return mathZero(p.x) && mathZero(p.y); +} + + +static inline bool mathEqual(float a, float b) +{ + return mathZero(a - b); +} + + +static inline bool mathEqual(const Matrix& a, const Matrix& b) +{ + if (!mathEqual(a.e11, b.e11) || !mathEqual(a.e12, b.e12) || !mathEqual(a.e13, b.e13) || + !mathEqual(a.e21, b.e21) || !mathEqual(a.e22, b.e22) || !mathEqual(a.e23, b.e23) || + !mathEqual(a.e31, b.e31) || !mathEqual(a.e32, b.e32) || !mathEqual(a.e33, b.e33)) { + return false; + } + return true; +} + + +static inline bool mathRightAngle(const Matrix* m) +{ + auto radian = fabsf(atan2f(m->e21, m->e11)); + if (radian < FLOAT_EPSILON || mathEqual(radian, MATH_PI2) || mathEqual(radian, MATH_PI)) return true; + return false; +} + + +static inline bool mathSkewed(const Matrix* m) +{ + return !mathZero(m->e21 + m->e12); +} + + +static inline void mathIdentity(Matrix* m) +{ + m->e11 = 1.0f; + m->e12 = 0.0f; + m->e13 = 0.0f; + m->e21 = 0.0f; + m->e22 = 1.0f; + m->e23 = 0.0f; + m->e31 = 0.0f; + m->e32 = 0.0f; + m->e33 = 1.0f; +} + + +static inline void mathTransform(Matrix* transform, Point* coord) +{ + auto x = coord->x; + auto y = coord->y; + coord->x = x * transform->e11 + y * transform->e12 + transform->e13; + coord->y = x * transform->e21 + y * transform->e22 + transform->e23; +} + + +static inline void mathScale(Matrix* m, float sx, float sy) +{ + m->e11 *= sx; + m->e22 *= sy; +} + + +static inline void mathScaleR(Matrix* m, float x, float y) +{ + if (x != 1.0f) { + m->e11 *= x; + m->e21 *= x; + } + if (y != 1.0f) { + m->e22 *= y; + m->e12 *= y; + } +} + + +static inline void mathTranslate(Matrix* m, float x, float y) +{ + m->e13 += x; + m->e23 += y; +} + + +static inline void mathTranslateR(Matrix* m, float x, float y) +{ + if (x == 0.0f && y == 0.0f) return; + m->e13 += (x * m->e11 + y * m->e12); + m->e23 += (x * m->e21 + y * m->e22); +} + + +static inline void mathLog(Matrix* m) +{ + TVGLOG("MATH", "Matrix: [%f %f %f] [%f %f %f] [%f %f %f]", m->e11, m->e12, m->e13, m->e21, m->e22, m->e23, m->e31, m->e32, m->e33); +} + + +static inline float mathLength(const Point* a, const Point* b) +{ + auto x = b->x - a->x; + auto y = b->y - a->y; + + if (x < 0) x = -x; + if (y < 0) y = -y; + + return (x > y) ? (x + 0.375f * y) : (y + 0.375f * x); +} + + +static inline float mathLength(const Point& a) +{ + return sqrtf(a.x * a.x + a.y * a.y); +} + + +static inline Point operator-(const Point& lhs, const Point& rhs) +{ + return {lhs.x - rhs.x, lhs.y - rhs.y}; +} + + +static inline Point operator+(const Point& lhs, const Point& rhs) +{ + return {lhs.x + rhs.x, lhs.y + rhs.y}; +} + + +static inline Point operator*(const Point& lhs, float rhs) +{ + return {lhs.x * rhs, lhs.y * rhs}; +} + + +static inline Point operator*(const float& lhs, const Point& rhs) +{ + return {lhs * rhs.x, lhs * rhs.y}; +} + + +static inline Point operator/(const Point& lhs, const float rhs) +{ + return {lhs.x / rhs, lhs.y / rhs}; +} + + +template +static inline T mathLerp(const T &start, const T &end, float t) +{ + return static_cast(start + (end - start) * t); +} + + +#endif //_TVG_MATH_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgPaint.cpp b/include/liblvgl/libs/thorvg/tvgPaint.cpp new file mode 100644 index 00000000..a2f4c628 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgPaint.cpp @@ -0,0 +1,482 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgMath.h" +#include "tvgPaint.h" +#include "tvgShape.h" +#include "tvgPicture.h" +#include "tvgScene.h" +#include "tvgText.h" + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +#define PAINT_METHOD(ret, METHOD) \ + switch (id) { \ + case TVG_CLASS_ID_SHAPE: ret = P((Shape*)paint)->METHOD; break; \ + case TVG_CLASS_ID_SCENE: ret = P((Scene*)paint)->METHOD; break; \ + case TVG_CLASS_ID_PICTURE: ret = P((Picture*)paint)->METHOD; break; \ + case TVG_CLASS_ID_TEXT: ret = P((Text*)paint)->METHOD; break; \ + default: ret = {}; \ + } + + + +static Result _compFastTrack(Paint* cmpTarget, const RenderTransform* pTransform, RenderTransform* rTransform, RenderRegion& viewport) +{ + /* Access Shape class by Paint is bad... but it's ok still it's an internal usage. */ + auto shape = static_cast(cmpTarget); + + //Rectangle Candidates? + const Point* pts; + auto ptsCnt = shape->pathCoords(&pts); + + //nothing to clip + if (ptsCnt == 0) return Result::InvalidArguments; + + if (ptsCnt != 4) return Result::InsufficientCondition; + + if (rTransform) rTransform->update(); + + //No rotation and no skewing + if (pTransform && (!mathRightAngle(&pTransform->m) || mathSkewed(&pTransform->m))) return Result::InsufficientCondition; + if (rTransform && (!mathRightAngle(&rTransform->m) || mathSkewed(&rTransform->m))) return Result::InsufficientCondition; + + //Perpendicular Rectangle? + auto pt1 = pts + 0; + auto pt2 = pts + 1; + auto pt3 = pts + 2; + auto pt4 = pts + 3; + + if ((mathEqual(pt1->x, pt2->x) && mathEqual(pt2->y, pt3->y) && mathEqual(pt3->x, pt4->x) && mathEqual(pt1->y, pt4->y)) || + (mathEqual(pt2->x, pt3->x) && mathEqual(pt1->y, pt2->y) && mathEqual(pt1->x, pt4->x) && mathEqual(pt3->y, pt4->y))) { + + auto v1 = *pt1; + auto v2 = *pt3; + + if (rTransform) { + mathMultiply(&v1, &rTransform->m); + mathMultiply(&v2, &rTransform->m); + } + + if (pTransform) { + mathMultiply(&v1, &pTransform->m); + mathMultiply(&v2, &pTransform->m); + } + + //sorting + if (v1.x > v2.x) { + auto tmp = v2.x; + v2.x = v1.x; + v1.x = tmp; + } + + if (v1.y > v2.y) { + auto tmp = v2.y; + v2.y = v1.y; + v1.y = tmp; + } + + viewport.x = static_cast(v1.x); + viewport.y = static_cast(v1.y); + viewport.w = static_cast(ceil(v2.x - viewport.x)); + viewport.h = static_cast(ceil(v2.y - viewport.y)); + + if (viewport.w < 0) viewport.w = 0; + if (viewport.h < 0) viewport.h = 0; + + return Result::Success; + } + return Result::InsufficientCondition; +} + + +RenderRegion Paint::Impl::bounds(RenderMethod* renderer) const +{ + RenderRegion ret; + PAINT_METHOD(ret, bounds(renderer)); + return ret; +} + + +Iterator* Paint::Impl::iterator() +{ + Iterator* ret; + PAINT_METHOD(ret, iterator()); + return ret; +} + + +Paint* Paint::Impl::duplicate() +{ + Paint* ret; + PAINT_METHOD(ret, duplicate()); + + //duplicate Transform + if (rTransform) { + ret->pImpl->rTransform = new RenderTransform(); + *ret->pImpl->rTransform = *rTransform; + ret->pImpl->renderFlag |= RenderUpdateFlag::Transform; + } + + ret->pImpl->opacity = opacity; + + if (compData) ret->pImpl->composite(ret, compData->target->duplicate(), compData->method); + + return ret; +} + + +bool Paint::Impl::rotate(float degree) +{ + if (rTransform) { + if (mathEqual(degree, rTransform->degree)) return true; + } else { + if (mathZero(degree)) return true; + rTransform = new RenderTransform(); + } + rTransform->degree = degree; + if (!rTransform->overriding) renderFlag |= RenderUpdateFlag::Transform; + + return true; +} + + +bool Paint::Impl::scale(float factor) +{ + if (rTransform) { + if (mathEqual(factor, rTransform->scale)) return true; + } else { + if (mathEqual(factor, 1.0f)) return true; + rTransform = new RenderTransform(); + } + rTransform->scale = factor; + if (!rTransform->overriding) renderFlag |= RenderUpdateFlag::Transform; + + return true; +} + + +bool Paint::Impl::translate(float x, float y) +{ + if (rTransform) { + if (mathEqual(x, rTransform->x) && mathEqual(y, rTransform->y)) return true; + } else { + if (mathZero(x) && mathZero(y)) return true; + rTransform = new RenderTransform(); + } + rTransform->x = x; + rTransform->y = y; + if (!rTransform->overriding) renderFlag |= RenderUpdateFlag::Transform; + + return true; +} + + +bool Paint::Impl::render(RenderMethod* renderer) +{ + Compositor* cmp = nullptr; + + /* Note: only ClipPath is processed in update() step. + Create a composition image. */ + if (compData && compData->method != CompositeMethod::ClipPath && !(compData->target->pImpl->ctxFlag & ContextFlag::FastTrack)) { + RenderRegion region; + PAINT_METHOD(region, bounds(renderer)); + + if (MASK_REGION_MERGING(compData->method)) region.add(P(compData->target)->bounds(renderer)); + if (region.w == 0 || region.h == 0) return true; + cmp = renderer->target(region, COMPOSITE_TO_COLORSPACE(renderer, compData->method)); + if (renderer->beginComposite(cmp, CompositeMethod::None, 255)) { + compData->target->pImpl->render(renderer); + } + } + + if (cmp) renderer->beginComposite(cmp, compData->method, compData->target->pImpl->opacity); + + renderer->blend(blendMethod); + + bool ret; + PAINT_METHOD(ret, render(renderer)); + + if (cmp) renderer->endComposite(cmp); + + return ret; +} + + +RenderData Paint::Impl::update(RenderMethod* renderer, const RenderTransform* pTransform, Array& clips, uint8_t opacity, RenderUpdateFlag pFlag, bool clipper) +{ + if (this->renderer != renderer) { + if (this->renderer) TVGERR("RENDERER", "paint's renderer has been changed!"); + renderer->ref(); + this->renderer = renderer; + } + + if (renderFlag & RenderUpdateFlag::Transform) { + if (!rTransform) return nullptr; + rTransform->update(); + } + + /* 1. Composition Pre Processing */ + RenderData trd = nullptr; //composite target render data + RenderRegion viewport; + Result compFastTrack = Result::InsufficientCondition; + bool childClipper = false; + + if (compData) { + auto target = compData->target; + auto method = compData->method; + target->pImpl->ctxFlag &= ~ContextFlag::FastTrack; //reset + + /* If the transformation has no rotational factors and the ClipPath/Alpha(InvAlpha)Masking involves a simple rectangle, + we can optimize by using the viewport instead of the regular ClipPath/AlphaMasking sequence for improved performance. */ + auto tryFastTrack = false; + if (target->identifier() == TVG_CLASS_ID_SHAPE) { + if (method == CompositeMethod::ClipPath) tryFastTrack = true; + else { + auto shape = static_cast(target); + uint8_t a; + shape->fillColor(nullptr, nullptr, nullptr, &a); + //no gradient fill & no compositions of the composition target. + if (!shape->fill() && !(PP(shape)->compData)) { + if (method == CompositeMethod::AlphaMask && a == 255 && PP(shape)->opacity == 255) tryFastTrack = true; + else if (method == CompositeMethod::InvAlphaMask && (a == 0 || PP(shape)->opacity == 0)) tryFastTrack = true; + } + } + if (tryFastTrack) { + RenderRegion viewport2; + if (target->composite(nullptr) != CompositeMethod::ClipPath && + (compFastTrack = _compFastTrack(target, pTransform, target->pImpl->rTransform, viewport2)) == Result::Success) { + viewport = renderer->viewport(); + viewport2.intersect(viewport); + renderer->viewport(viewport2); + target->pImpl->ctxFlag |= ContextFlag::FastTrack; + } + } + } + if (compFastTrack == Result::InsufficientCondition) { + childClipper = compData->method == CompositeMethod::ClipPath ? true : false; + trd = target->pImpl->update(renderer, pTransform, clips, 255, pFlag, childClipper); + if (childClipper) clips.push(trd); + } + } + + /* 2. Main Update */ + auto newFlag = static_cast(pFlag | renderFlag); + renderFlag = RenderUpdateFlag::None; + opacity = MULTIPLY(opacity, this->opacity); + + RenderData rd = nullptr; + RenderTransform outTransform(pTransform, rTransform); + PAINT_METHOD(rd, update(renderer, &outTransform, clips, opacity, newFlag, clipper)); + + /* 3. Composition Post Processing */ + if (compFastTrack == Result::Success) renderer->viewport(viewport); + else if (childClipper) clips.pop(); + + return rd; +} + + +bool Paint::Impl::bounds(float* x, float* y, float* w, float* h, bool transformed, bool stroking) +{ + Matrix* m = nullptr; + bool ret; + + //Case: No transformed, quick return! + if (!transformed || !(m = this->transform())) { + PAINT_METHOD(ret, bounds(x, y, w, h, stroking)); + return ret; + } + + //Case: Transformed + auto tx = 0.0f; + auto ty = 0.0f; + auto tw = 0.0f; + auto th = 0.0f; + + PAINT_METHOD(ret, bounds(&tx, &ty, &tw, &th, stroking)); + + //Get vertices + Point pt[4] = {{tx, ty}, {tx + tw, ty}, {tx + tw, ty + th}, {tx, ty + th}}; + + //New bounding box + auto x1 = FLT_MAX; + auto y1 = FLT_MAX; + auto x2 = -FLT_MAX; + auto y2 = -FLT_MAX; + + //Compute the AABB after transformation + for (int i = 0; i < 4; i++) { + mathMultiply(&pt[i], m); + + if (pt[i].x < x1) x1 = pt[i].x; + if (pt[i].x > x2) x2 = pt[i].x; + if (pt[i].y < y1) y1 = pt[i].y; + if (pt[i].y > y2) y2 = pt[i].y; + } + + if (x) *x = x1; + if (y) *y = y1; + if (w) *w = x2 - x1; + if (h) *h = y2 - y1; + + return ret; +} + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +Paint :: Paint() : pImpl(new Impl(this)) +{ +} + + +Paint :: ~Paint() +{ + delete(pImpl); +} + + +Result Paint::rotate(float degree) noexcept +{ + if (pImpl->rotate(degree)) return Result::Success; + return Result::FailedAllocation; +} + + +Result Paint::scale(float factor) noexcept +{ + if (pImpl->scale(factor)) return Result::Success; + return Result::FailedAllocation; +} + + +Result Paint::translate(float x, float y) noexcept +{ + if (pImpl->translate(x, y)) return Result::Success; + return Result::FailedAllocation; +} + + +Result Paint::transform(const Matrix& m) noexcept +{ + if (pImpl->transform(m)) return Result::Success; + return Result::FailedAllocation; +} + + +Matrix Paint::transform() noexcept +{ + auto pTransform = pImpl->transform(); + if (pTransform) return *pTransform; + return {1, 0, 0, 0, 1, 0, 0, 0, 1}; +} + + +TVG_DEPRECATED Result Paint::bounds(float* x, float* y, float* w, float* h) const noexcept +{ + return this->bounds(x, y, w, h, false); +} + + +Result Paint::bounds(float* x, float* y, float* w, float* h, bool transform) const noexcept +{ + if (pImpl->bounds(x, y, w, h, transform, true)) return Result::Success; + return Result::InsufficientCondition; +} + + +Paint* Paint::duplicate() const noexcept +{ + return pImpl->duplicate(); +} + + +Result Paint::composite(std::unique_ptr target, CompositeMethod method) noexcept +{ + auto p = target.release(); + if (pImpl->composite(this, p, method)) return Result::Success; + delete(p); + return Result::InvalidArguments; +} + + +CompositeMethod Paint::composite(const Paint** target) const noexcept +{ + if (pImpl->compData) { + if (target) *target = pImpl->compData->target; + return pImpl->compData->method; + } else { + if (target) *target = nullptr; + return CompositeMethod::None; + } +} + + +Result Paint::opacity(uint8_t o) noexcept +{ + if (pImpl->opacity == o) return Result::Success; + + pImpl->opacity = o; + pImpl->renderFlag |= RenderUpdateFlag::Color; + + return Result::Success; +} + + +uint8_t Paint::opacity() const noexcept +{ + return pImpl->opacity; +} + + +uint32_t Paint::identifier() const noexcept +{ + return pImpl->id; +} + + +Result Paint::blend(BlendMethod method) const noexcept +{ + if (pImpl->blendMethod != method) { + pImpl->blendMethod = method; + pImpl->renderFlag |= RenderUpdateFlag::Blend; + } + + return Result::Success; +} + + +BlendMethod Paint::blend() const noexcept +{ + return pImpl->blendMethod; +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgPaint.h b/include/liblvgl/libs/thorvg/tvgPaint.h new file mode 100644 index 00000000..a3fbc074 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgPaint.h @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_PAINT_H_ +#define _TVG_PAINT_H_ + +#include "tvgRender.h" +#include "tvgMath.h" + +namespace tvg +{ + enum ContextFlag : uint8_t {Invalid = 0, FastTrack = 1}; + + struct Iterator + { + virtual ~Iterator() {} + virtual const Paint* next() = 0; + virtual uint32_t count() = 0; + virtual void begin() = 0; + }; + + struct Composite + { + Paint* target; + Paint* source; + CompositeMethod method; + }; + + struct Paint::Impl + { + Paint* paint = nullptr; + RenderTransform* rTransform = nullptr; + Composite* compData = nullptr; + RenderMethod* renderer = nullptr; + BlendMethod blendMethod = BlendMethod::Normal; //uint8_t + uint8_t renderFlag = RenderUpdateFlag::None; + uint8_t ctxFlag = ContextFlag::Invalid; + uint8_t id; + uint8_t opacity = 255; + uint8_t refCnt = 0; //reference count + + Impl(Paint* pnt) : paint(pnt) {} + + ~Impl() + { + if (compData) { + if (P(compData->target)->unref() == 0) delete(compData->target); + free(compData); + } + delete(rTransform); + if (renderer && (renderer->unref() == 0)) delete(renderer); + } + + uint8_t ref() + { + if (refCnt == 255) TVGERR("RENDERER", "Corrupted reference count!"); + return ++refCnt; + } + + uint8_t unref() + { + if (refCnt == 0) TVGERR("RENDERER", "Corrupted reference count!"); + return --refCnt; + } + + bool transform(const Matrix& m) + { + if (!rTransform) { + if (mathIdentity(&m)) return true; + rTransform = new RenderTransform(); + if (!rTransform) return false; + } + rTransform->override(m); + renderFlag |= RenderUpdateFlag::Transform; + + return true; + } + + Matrix* transform() + { + if (rTransform) { + rTransform->update(); + return &rTransform->m; + } + return nullptr; + } + + bool composite(Paint* source, Paint* target, CompositeMethod method) + { + //Invalid case + if ((!target && method != CompositeMethod::None) || (target && method == CompositeMethod::None)) return false; + + if (compData) { + P(compData->target)->unref(); + if ((compData->target != target) && P(compData->target)->refCnt == 0) { + delete(compData->target); + } + //Reset scenario + if (!target && method == CompositeMethod::None) { + free(compData); + compData = nullptr; + return true; + } + } else { + if (!target && method == CompositeMethod::None) return true; + compData = static_cast(calloc(1, sizeof(Composite))); + } + P(target)->ref(); + compData->target = target; + compData->source = source; + compData->method = method; + return true; + } + + RenderRegion bounds(RenderMethod* renderer) const; + Iterator* iterator(); + bool rotate(float degree); + bool scale(float factor); + bool translate(float x, float y); + bool bounds(float* x, float* y, float* w, float* h, bool transformed, bool stroking); + RenderData update(RenderMethod* renderer, const RenderTransform* pTransform, Array& clips, uint8_t opacity, RenderUpdateFlag pFlag, bool clipper = false); + bool render(RenderMethod* renderer); + Paint* duplicate(); + }; +} + +#endif //_TVG_PAINT_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgPicture.cpp b/include/liblvgl/libs/thorvg/tvgPicture.cpp new file mode 100644 index 00000000..3d7859d2 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgPicture.cpp @@ -0,0 +1,238 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgPicture.h" + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +RenderUpdateFlag Picture::Impl::load() +{ + if (loader) { + if (paint) { + loader->sync(); + } else { + paint = loader->paint(); + if (paint) { + if (w != loader->w || h != loader->h) { + if (!resizing) { + w = loader->w; + h = loader->h; + } + loader->resize(paint, w, h); + resizing = false; + } + return RenderUpdateFlag::None; + } + } + if (!surface) { + if ((surface = loader->bitmap())) { + return RenderUpdateFlag::Image; + } + } + } + return RenderUpdateFlag::None; +} + + +bool Picture::Impl::needComposition(uint8_t opacity) +{ + //In this case, paint(scene) would try composition itself. + if (opacity < 255) return false; + + //Composition test + const Paint* target; + auto method = picture->composite(&target); + if (!target || method == tvg::CompositeMethod::ClipPath) return false; + if (target->pImpl->opacity == 255 || target->pImpl->opacity == 0) return false; + + return true; +} + + +bool Picture::Impl::render(RenderMethod* renderer) +{ + bool ret = false; + if (surface) return renderer->renderImage(rd); + else if (paint) { + Compositor* cmp = nullptr; + if (needComp) { + cmp = renderer->target(bounds(renderer), renderer->colorSpace()); + renderer->beginComposite(cmp, CompositeMethod::None, 255); + } + ret = paint->pImpl->render(renderer); + if (cmp) renderer->endComposite(cmp); + } + return ret; +} + + +bool Picture::Impl::size(float w, float h) +{ + this->w = w; + this->h = h; + resizing = true; + return true; +} + + +RenderRegion Picture::Impl::bounds(RenderMethod* renderer) +{ + if (rd) return renderer->region(rd); + if (paint) return paint->pImpl->bounds(renderer); + return {0, 0, 0, 0}; +} + + +RenderTransform Picture::Impl::resizeTransform(const RenderTransform* pTransform) +{ + //Overriding Transformation by the desired image size + auto sx = w / loader->w; + auto sy = h / loader->h; + auto scale = sx < sy ? sx : sy; + + RenderTransform tmp; + tmp.m = {scale, 0, 0, 0, scale, 0, 0, 0, 1}; + + if (!pTransform) return tmp; + else return RenderTransform(pTransform, &tmp); +} + + +Result Picture::Impl::load(ImageLoader* loader) +{ + //Same resource has been loaded. + if (this->loader == loader) { + this->loader->sharing--; //make it sure the reference counting. + return Result::Success; + } else if (this->loader) { + LoaderMgr::retrieve(this->loader); + } + + this->loader = loader; + + if (!loader->read()) return Result::Unknown; + + this->w = loader->w; + this->h = loader->h; + + return Result::Success; +} + + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +Picture::Picture() : pImpl(new Impl(this)) +{ + Paint::pImpl->id = TVG_CLASS_ID_PICTURE; +} + + +Picture::~Picture() +{ + delete(pImpl); +} + + +unique_ptr Picture::gen() noexcept +{ + return unique_ptr(new Picture); +} + + +uint32_t Picture::identifier() noexcept +{ + return TVG_CLASS_ID_PICTURE; +} + + +Result Picture::load(const std::string& path) noexcept +{ + if (path.empty()) return Result::InvalidArguments; + + return pImpl->load(path); +} + + +Result Picture::load(const char* data, uint32_t size, const string& mimeType, bool copy) noexcept +{ + if (!data || size <= 0) return Result::InvalidArguments; + + return pImpl->load(data, size, mimeType, copy); +} + + +TVG_DEPRECATED Result Picture::load(const char* data, uint32_t size, bool copy) noexcept +{ + return load(data, size, "", copy); +} + + +Result Picture::load(uint32_t* data, uint32_t w, uint32_t h, bool copy) noexcept +{ + if (!data || w <= 0 || h <= 0) return Result::InvalidArguments; + + return pImpl->load(data, w, h, copy); +} + + +Result Picture::size(float w, float h) noexcept +{ + if (pImpl->size(w, h)) return Result::Success; + return Result::InsufficientCondition; +} + + +Result Picture::size(float* w, float* h) const noexcept +{ + if (!pImpl->loader) return Result::InsufficientCondition; + if (w) *w = pImpl->w; + if (h) *h = pImpl->h; + return Result::Success; +} + + +Result Picture::mesh(const Polygon* triangles, uint32_t triangleCnt) noexcept +{ + if (!triangles && triangleCnt > 0) return Result::InvalidArguments; + if (triangles && triangleCnt == 0) return Result::InvalidArguments; + + pImpl->mesh(triangles, triangleCnt); + return Result::Success; +} + + +uint32_t Picture::mesh(const Polygon** triangles) const noexcept +{ + if (triangles) *triangles = pImpl->rm.triangles; + return pImpl->rm.triangleCnt; +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgPicture.h b/include/liblvgl/libs/thorvg/tvgPicture.h new file mode 100644 index 00000000..7c3ec540 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgPicture.h @@ -0,0 +1,250 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_PICTURE_H_ +#define _TVG_PICTURE_H_ + +#include +#include "tvgPaint.h" +#include "tvgLoader.h" + + +struct PictureIterator : Iterator +{ + Paint* paint = nullptr; + Paint* ptr = nullptr; + + PictureIterator(Paint* p) : paint(p) {} + + const Paint* next() override + { + if (!ptr) ptr = paint; + else ptr = nullptr; + return ptr; + } + + uint32_t count() override + { + if (paint) return 1; + else return 0; + } + + void begin() override + { + ptr = nullptr; + } +}; + + +struct Picture::Impl +{ + ImageLoader* loader = nullptr; + + Paint* paint = nullptr; //vector picture uses + Surface* surface = nullptr; //bitmap picture uses + RenderData rd = nullptr; //engine data + float w = 0, h = 0; + RenderMesh rm; //mesh data + Picture* picture = nullptr; + bool resizing = false; + bool needComp = false; //need composition + + RenderTransform resizeTransform(const RenderTransform* pTransform); + bool needComposition(uint8_t opacity); + bool render(RenderMethod* renderer); + bool size(float w, float h); + RenderRegion bounds(RenderMethod* renderer); + Result load(ImageLoader* ploader); + + Impl(Picture* p) : picture(p) + { + } + + ~Impl() + { + LoaderMgr::retrieve(loader); + if (surface) { + if (auto renderer = PP(picture)->renderer) { + renderer->dispose(rd); + } + } + delete(paint); + } + + RenderData update(RenderMethod* renderer, const RenderTransform* pTransform, Array& clips, uint8_t opacity, RenderUpdateFlag pFlag, bool clipper) + { + auto flag = load(); + + if (surface) { + auto transform = resizeTransform(pTransform); + rd = renderer->prepare(surface, &rm, rd, &transform, clips, opacity, static_cast(pFlag | flag)); + } else if (paint) { + if (resizing) { + loader->resize(paint, w, h); + resizing = false; + } + needComp = needComposition(opacity) ? true : false; + rd = paint->pImpl->update(renderer, pTransform, clips, opacity, static_cast(pFlag | flag), clipper); + } + return rd; + } + + bool bounds(float* x, float* y, float* w, float* h, bool stroking) + { + if (rm.triangleCnt > 0) { + auto triangles = rm.triangles; + auto min = triangles[0].vertex[0].pt; + auto max = triangles[0].vertex[0].pt; + + for (uint32_t i = 0; i < rm.triangleCnt; ++i) { + if (triangles[i].vertex[0].pt.x < min.x) min.x = triangles[i].vertex[0].pt.x; + else if (triangles[i].vertex[0].pt.x > max.x) max.x = triangles[i].vertex[0].pt.x; + if (triangles[i].vertex[0].pt.y < min.y) min.y = triangles[i].vertex[0].pt.y; + else if (triangles[i].vertex[0].pt.y > max.y) max.y = triangles[i].vertex[0].pt.y; + + if (triangles[i].vertex[1].pt.x < min.x) min.x = triangles[i].vertex[1].pt.x; + else if (triangles[i].vertex[1].pt.x > max.x) max.x = triangles[i].vertex[1].pt.x; + if (triangles[i].vertex[1].pt.y < min.y) min.y = triangles[i].vertex[1].pt.y; + else if (triangles[i].vertex[1].pt.y > max.y) max.y = triangles[i].vertex[1].pt.y; + + if (triangles[i].vertex[2].pt.x < min.x) min.x = triangles[i].vertex[2].pt.x; + else if (triangles[i].vertex[2].pt.x > max.x) max.x = triangles[i].vertex[2].pt.x; + if (triangles[i].vertex[2].pt.y < min.y) min.y = triangles[i].vertex[2].pt.y; + else if (triangles[i].vertex[2].pt.y > max.y) max.y = triangles[i].vertex[2].pt.y; + } + if (x) *x = min.x; + if (y) *y = min.y; + if (w) *w = max.x - min.x; + if (h) *h = max.y - min.y; + } else { + if (x) *x = 0; + if (y) *y = 0; + if (w) *w = this->w; + if (h) *h = this->h; + } + return true; + } + + Result load(const string& path) + { + if (paint || surface) return Result::InsufficientCondition; + + bool invalid; //Invalid Path + auto loader = static_cast(LoaderMgr::loader(path, &invalid)); + if (!loader) { + if (invalid) return Result::InvalidArguments; + return Result::NonSupport; + } + return load(loader); + } + + Result load(const char* data, uint32_t size, const string& mimeType, bool copy) + { + if (paint || surface) return Result::InsufficientCondition; + auto loader = static_cast(LoaderMgr::loader(data, size, mimeType, copy)); + if (!loader) return Result::NonSupport; + return load(loader); + } + + Result load(uint32_t* data, uint32_t w, uint32_t h, bool copy) + { + if (paint || surface) return Result::InsufficientCondition; + + auto loader = static_cast(LoaderMgr::loader(data, w, h, copy)); + if (!loader) return Result::FailedAllocation; + + return load(loader); + } + + void mesh(const Polygon* triangles, const uint32_t triangleCnt) + { + if (triangles && triangleCnt > 0) { + this->rm.triangleCnt = triangleCnt; + this->rm.triangles = (Polygon*)malloc(sizeof(Polygon) * triangleCnt); + memcpy(this->rm.triangles, triangles, sizeof(Polygon) * triangleCnt); + } else { + free(this->rm.triangles); + this->rm.triangles = nullptr; + this->rm.triangleCnt = 0; + } + } + + Paint* duplicate() + { + load(); + + auto ret = Picture::gen().release(); + auto dup = ret->pImpl; + + if (paint) dup->paint = paint->duplicate(); + + if (loader) { + dup->loader = loader; + ++dup->loader->sharing; + } + + dup->surface = surface; + dup->w = w; + dup->h = h; + dup->resizing = resizing; + + if (rm.triangleCnt > 0) { + dup->rm.triangleCnt = rm.triangleCnt; + dup->rm.triangles = (Polygon*)malloc(sizeof(Polygon) * rm.triangleCnt); + memcpy(dup->rm.triangles, rm.triangles, sizeof(Polygon) * rm.triangleCnt); + } + + return ret; + } + + Iterator* iterator() + { + load(); + return new PictureIterator(paint); + } + + uint32_t* data(uint32_t* w, uint32_t* h) + { + //Try it, If not loaded yet. + load(); + + if (loader) { + if (w) *w = static_cast(loader->w); + if (h) *h = static_cast(loader->h); + } else { + if (w) *w = 0; + if (h) *h = 0; + } + if (surface) return surface->buf32; + else return nullptr; + } + + RenderUpdateFlag load(); +}; + +#endif //_TVG_PICTURE_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgRawLoader.cpp b/include/liblvgl/libs/thorvg/tvgRawLoader.cpp new file mode 100644 index 00000000..22741412 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgRawLoader.cpp @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include +#include +#include "tvgLoader.h" +#include "tvgRawLoader.h" + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +RawLoader::RawLoader() : ImageLoader(FileType::Raw) +{ +} + + +RawLoader::~RawLoader() +{ + if (copy) free(surface.buf32); +} + + +bool RawLoader::open(const uint32_t* data, uint32_t w, uint32_t h, bool copy) +{ + if (!LoadModule::read()) return true; + + if (!data || w == 0 || h == 0) return false; + + this->w = (float)w; + this->h = (float)h; + this->copy = copy; + + if (copy) { + surface.buf32 = (uint32_t*)malloc(sizeof(uint32_t) * w * h); + if (!surface.buf32) return false; + memcpy((void*)surface.buf32, data, sizeof(uint32_t) * w * h); + } + else surface.buf32 = const_cast(data); + + //setup the surface + surface.stride = w; + surface.w = w; + surface.h = h; + surface.cs = ColorSpace::ARGB8888; + surface.channelSize = sizeof(uint32_t); + surface.premultiplied = true; + + return true; +} + + +bool RawLoader::read() +{ + LoadModule::read(); + + return true; +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgRawLoader.h b/include/liblvgl/libs/thorvg/tvgRawLoader.h new file mode 100644 index 00000000..492fd534 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgRawLoader.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_RAW_LOADER_H_ +#define _TVG_RAW_LOADER_H_ + +class RawLoader : public ImageLoader +{ +public: + bool copy = false; + + RawLoader(); + ~RawLoader(); + + using LoadModule::open; + bool open(const uint32_t* data, uint32_t w, uint32_t h, bool copy); + bool read() override; +}; + + +#endif //_TVG_RAW_LOADER_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgRender.cpp b/include/liblvgl/libs/thorvg/tvgRender.cpp new file mode 100644 index 00000000..dad20e8b --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgRender.cpp @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgMath.h" +#include "tvgRender.h" + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +void RenderTransform::override(const Matrix& m) +{ + this->m = m; + overriding = true; +} + + +void RenderTransform::update() +{ + if (overriding) return; + + mathIdentity(&m); + + mathScale(&m, scale, scale); + + if (!mathZero(degree)) mathRotate(&m, degree); + + mathTranslate(&m, x, y); +} + + +RenderTransform::RenderTransform(const RenderTransform* lhs, const RenderTransform* rhs) +{ + if (lhs && rhs) m = mathMultiply(&lhs->m, &rhs->m); + else if (lhs) m = lhs->m; + else if (rhs) m = rhs->m; + else mathIdentity(&m); +} + + +void RenderRegion::intersect(const RenderRegion& rhs) +{ + auto x1 = x + w; + auto y1 = y + h; + auto x2 = rhs.x + rhs.w; + auto y2 = rhs.y + rhs.h; + + x = (x > rhs.x) ? x : rhs.x; + y = (y > rhs.y) ? y : rhs.y; + w = ((x1 < x2) ? x1 : x2) - x; + h = ((y1 < y2) ? y1 : y2) - y; + + if (w < 0) w = 0; + if (h < 0) h = 0; +} + + +void RenderRegion::add(const RenderRegion& rhs) +{ + if (rhs.x < x) { + w += (x - rhs.x); + x = rhs.x; + } + if (rhs.y < y) { + h += (y - rhs.y); + y = rhs.y; + } + if (rhs.x + rhs.w > x + w) w = (rhs.x + rhs.w) - x; + if (rhs.y + rhs.h > y + h) h = (rhs.y + rhs.h) - y; +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgRender.h b/include/liblvgl/libs/thorvg/tvgRender.h new file mode 100644 index 00000000..8467a910 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgRender.h @@ -0,0 +1,356 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_RENDER_H_ +#define _TVG_RENDER_H_ + +#include "tvgCommon.h" +#include "tvgArray.h" +#include "tvgLock.h" + +namespace tvg +{ + +using RenderData = void*; +using pixel_t = uint32_t; + +enum RenderUpdateFlag : uint8_t {None = 0, Path = 1, Color = 2, Gradient = 4, Stroke = 8, Transform = 16, Image = 32, GradientStroke = 64, Blend = 128, All = 255}; + +struct Surface; + +enum ColorSpace +{ + ABGR8888 = 0, //The channels are joined in the order: alpha, blue, green, red. Colors are alpha-premultiplied. + ARGB8888, //The channels are joined in the order: alpha, red, green, blue. Colors are alpha-premultiplied. + ABGR8888S, //The channels are joined in the order: alpha, blue, green, red. Colors are un-alpha-premultiplied. + ARGB8888S, //The channels are joined in the order: alpha, red, green, blue. Colors are un-alpha-premultiplied. + Grayscale8, //One single channel data. + Unsupported //TODO: Change to the default, At the moment, we put it in the last to align with SwCanvas::Colorspace. +}; + +struct Surface +{ + union { + pixel_t* data = nullptr; //system based data pointer + uint32_t* buf32; //for explicit 32bits channels + uint8_t* buf8; //for explicit 8bits grayscale + }; + Key key; //a reserved lock for the thread safety + uint32_t stride = 0; + uint32_t w = 0, h = 0; + ColorSpace cs = ColorSpace::Unsupported; + uint8_t channelSize = 0; + bool premultiplied = false; //Alpha-premultiplied + + Surface() + { + } + + Surface(const Surface* rhs) + { + data = rhs->data; + stride = rhs->stride; + w = rhs->w; + h = rhs->h; + cs = rhs->cs; + channelSize = rhs->channelSize; + premultiplied = rhs->premultiplied; + } + + +}; + +struct Compositor +{ + CompositeMethod method; + uint8_t opacity; +}; + +struct RenderMesh +{ + Polygon* triangles = nullptr; + uint32_t triangleCnt = 0; + + ~RenderMesh() + { + free(triangles); + } +}; + +struct RenderRegion +{ + int32_t x, y, w, h; + + void intersect(const RenderRegion& rhs); + void add(const RenderRegion& rhs); + + bool operator==(const RenderRegion& rhs) + { + if (x == rhs.x && y == rhs.y && w == rhs.w && h == rhs.h) return true; + return false; + } +}; + +struct RenderTransform +{ + Matrix m; //3x3 Matrix Elements + float x = 0.0f; + float y = 0.0f; + float degree = 0.0f; //rotation degree + float scale = 1.0f; //scale factor + bool overriding = false; //user transform? + + void update(); + void override(const Matrix& m); + + RenderTransform() {} + RenderTransform(const RenderTransform* lhs, const RenderTransform* rhs); +}; + +struct RenderStroke +{ + float width = 0.0f; + uint8_t color[4] = {0, 0, 0, 0}; + Fill *fill = nullptr; + float* dashPattern = nullptr; + uint32_t dashCnt = 0; + float dashOffset = 0.0f; + StrokeCap cap = StrokeCap::Square; + StrokeJoin join = StrokeJoin::Bevel; + float miterlimit = 4.0f; + bool strokeFirst = false; + + struct { + float begin = 0.0f; + float end = 1.0f; + bool individual = false; + } trim; + + ~RenderStroke() + { + free(dashPattern); + delete(fill); + } +}; + +struct RenderShape +{ + struct + { + Array cmds; + Array pts; + } path; + + Fill *fill = nullptr; + RenderStroke *stroke = nullptr; + uint8_t color[4] = {0, 0, 0, 0}; //r, g, b, a + FillRule rule = FillRule::Winding; + + ~RenderShape() + { + delete(fill); + delete(stroke); + } + + void fillColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const + { + if (r) *r = color[0]; + if (g) *g = color[1]; + if (b) *b = color[2]; + if (a) *a = color[3]; + } + + float strokeWidth() const + { + if (!stroke) return 0; + return stroke->width; + } + + bool strokeTrim() const + { + if (!stroke) return false; + if (stroke->trim.begin == 0.0f && stroke->trim.end == 1.0f) return false; + if (stroke->trim.begin == 1.0f && stroke->trim.end == 0.0f) return false; + return true; + } + + bool strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const + { + if (!stroke) return false; + + if (r) *r = stroke->color[0]; + if (g) *g = stroke->color[1]; + if (b) *b = stroke->color[2]; + if (a) *a = stroke->color[3]; + + return true; + } + + const Fill* strokeFill() const + { + if (!stroke) return nullptr; + return stroke->fill; + } + + uint32_t strokeDash(const float** dashPattern, float* offset) const + { + if (!stroke) return 0; + if (dashPattern) *dashPattern = stroke->dashPattern; + if (offset) *offset = stroke->dashOffset; + return stroke->dashCnt; + } + + StrokeCap strokeCap() const + { + if (!stroke) return StrokeCap::Square; + return stroke->cap; + } + + StrokeJoin strokeJoin() const + { + if (!stroke) return StrokeJoin::Bevel; + return stroke->join; + } + + float strokeMiterlimit() const + { + if (!stroke) return 4.0f; + + return stroke->miterlimit;; + } +}; + +class RenderMethod +{ +private: + uint32_t refCnt = 0; //reference count + Key key; + +public: + uint32_t ref() + { + ScopedLock lock(key); + return (++refCnt); + } + + uint32_t unref() + { + ScopedLock lock(key); + return (--refCnt); + } + + virtual ~RenderMethod() {} + virtual RenderData prepare(const RenderShape& rshape, RenderData data, const RenderTransform* transform, Array& clips, uint8_t opacity, RenderUpdateFlag flags, bool clipper) = 0; + virtual RenderData prepare(const Array& scene, RenderData data, const RenderTransform* transform, Array& clips, uint8_t opacity, RenderUpdateFlag flags) = 0; + virtual RenderData prepare(Surface* surface, const RenderMesh* mesh, RenderData data, const RenderTransform* transform, Array& clips, uint8_t opacity, RenderUpdateFlag flags) = 0; + virtual bool preRender() = 0; + virtual bool renderShape(RenderData data) = 0; + virtual bool renderImage(RenderData data) = 0; + virtual bool postRender() = 0; + virtual void dispose(RenderData data) = 0; + virtual RenderRegion region(RenderData data) = 0; + virtual RenderRegion viewport() = 0; + virtual bool viewport(const RenderRegion& vp) = 0; + virtual bool blend(BlendMethod method) = 0; + virtual ColorSpace colorSpace() = 0; + virtual const Surface* mainSurface() = 0; + + virtual bool clear() = 0; + virtual bool sync() = 0; + + virtual Compositor* target(const RenderRegion& region, ColorSpace cs) = 0; + virtual bool beginComposite(Compositor* cmp, CompositeMethod method, uint8_t opacity) = 0; + virtual bool endComposite(Compositor* cmp) = 0; +}; + +static inline bool MASK_REGION_MERGING(CompositeMethod method) +{ + switch(method) { + case CompositeMethod::AlphaMask: + case CompositeMethod::InvAlphaMask: + case CompositeMethod::LumaMask: + case CompositeMethod::InvLumaMask: + case CompositeMethod::SubtractMask: + case CompositeMethod::IntersectMask: + return false; + //these might expand the rendering region + case CompositeMethod::AddMask: + case CompositeMethod::DifferenceMask: + return true; + default: + TVGERR("RENDERER", "Unsupported Composite Method! = %d", (int)method); + return false; + } +} + +static inline uint8_t CHANNEL_SIZE(ColorSpace cs) +{ + switch(cs) { + case ColorSpace::ABGR8888: + case ColorSpace::ABGR8888S: + case ColorSpace::ARGB8888: + case ColorSpace::ARGB8888S: + return sizeof(uint32_t); + case ColorSpace::Grayscale8: + return sizeof(uint8_t); + case ColorSpace::Unsupported: + default: + TVGERR("RENDERER", "Unsupported Channel Size! = %d", (int)cs); + return 0; + } +} + +static inline ColorSpace COMPOSITE_TO_COLORSPACE(RenderMethod* renderer, CompositeMethod method) +{ + switch(method) { + case CompositeMethod::AlphaMask: + case CompositeMethod::InvAlphaMask: + case CompositeMethod::AddMask: + case CompositeMethod::DifferenceMask: + case CompositeMethod::SubtractMask: + case CompositeMethod::IntersectMask: + return ColorSpace::Grayscale8; + //TODO: Optimize Luma/InvLuma colorspace to Grayscale8 + case CompositeMethod::LumaMask: + case CompositeMethod::InvLumaMask: + return renderer->colorSpace(); + default: + TVGERR("RENDERER", "Unsupported Composite Size! = %d", (int)method); + return ColorSpace::Unsupported; + } +} + +static inline uint8_t MULTIPLY(uint8_t c, uint8_t a) +{ + return (((c) * (a) + 0xff) >> 8); +} + + +} + +#endif //_TVG_RENDER_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSaveModule.h b/include/liblvgl/libs/thorvg/tvgSaveModule.h new file mode 100644 index 00000000..abfd5381 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSaveModule.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2021 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_SAVE_MODULE_H_ +#define _TVG_SAVE_MODULE_H_ + +#include "tvgIteratorAccessor.h" + +namespace tvg +{ + +class SaveModule +{ +public: + virtual ~SaveModule() {} + + virtual bool save(Paint* paint, const string& path, bool compress) = 0; + virtual bool save(Animation* animation, Paint* bg, const string& path, uint32_t quality, uint32_t fps) = 0; + virtual bool close() = 0; +}; + +} + +#endif //_TVG_SAVE_MODULE_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSaver.cpp b/include/liblvgl/libs/thorvg/tvgSaver.cpp new file mode 100644 index 00000000..f2a22ec6 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSaver.cpp @@ -0,0 +1,209 @@ +/* + * Copyright (c) 2021 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgCommon.h" +#include "tvgSaveModule.h" +#include "tvgPaint.h" + +#ifdef THORVG_TVG_SAVER_SUPPORT + #include "tvgTvgSaver.h" +#endif +#ifdef THORVG_GIF_SAVER_SUPPORT + #include "tvgGifSaver.h" +#endif + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +struct Saver::Impl +{ + SaveModule* saveModule = nullptr; + Paint* bg = nullptr; + + ~Impl() + { + delete(saveModule); + delete(bg); + } +}; + + +static SaveModule* _find(FileType type) +{ + switch(type) { + case FileType::Tvg: { +#ifdef THORVG_TVG_SAVER_SUPPORT + return new TvgSaver; +#endif + break; + } + case FileType::Gif: { +#ifdef THORVG_GIF_SAVER_SUPPORT + return new GifSaver; +#endif + break; + } + default: { + break; + } + } + +#ifdef THORVG_LOG_ENABLED + const char *format; + switch(type) { + case FileType::Tvg: { + format = "TVG"; + break; + } + case FileType::Gif: { + format = "GIF"; + break; + } + default: { + format = "???"; + break; + } + } + TVGLOG("RENDERER", "%s format is not supported", format); +#endif + return nullptr; +} + + +static SaveModule* _find(const string& path) +{ + auto ext = path.substr(path.find_last_of(".") + 1); + if (!ext.compare("tvg")) { + return _find(FileType::Tvg); + } else if (!ext.compare("gif")) { + return _find(FileType::Gif); + } + return nullptr; +} + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +Saver::Saver() : pImpl(new Impl()) +{ +} + + +Saver::~Saver() +{ + delete(pImpl); +} + + +Result Saver::save(std::unique_ptr paint, const string& path, bool compress) noexcept +{ + auto p = paint.release(); + if (!p) return Result::MemoryCorruption; + + //Already on saving another resource. + if (pImpl->saveModule) { + if (P(p)->refCnt == 0) delete(p); + return Result::InsufficientCondition; + } + + if (auto saveModule = _find(path)) { + if (saveModule->save(p, path, compress)) { + pImpl->saveModule = saveModule; + return Result::Success; + } else { + if (P(p)->refCnt == 0) delete(p); + delete(saveModule); + return Result::Unknown; + } + } + if (P(p)->refCnt == 0) delete(p); + return Result::NonSupport; +} + + +Result Saver::background(unique_ptr paint) noexcept +{ + delete(pImpl->bg); + pImpl->bg = paint.release(); + + return Result::Success; +} + + +Result Saver::save(unique_ptr animation, const string& path, uint32_t quality, uint32_t fps) noexcept +{ + auto a = animation.release(); + if (!a) return Result::MemoryCorruption; + + //animation holds the picture, it must be 1 at the bottom. + auto remove = PP(a->picture())->refCnt <= 1 ? true : false; + + if (mathZero(a->totalFrame())) { + if (remove) delete(a); + return Result::InsufficientCondition; + } + + //Already on saving another resource. + if (pImpl->saveModule) { + if (remove) delete(a); + return Result::InsufficientCondition; + } + + if (auto saveModule = _find(path)) { + if (saveModule->save(a, pImpl->bg, path, quality, fps)) { + pImpl->saveModule = saveModule; + return Result::Success; + } else { + if (remove) delete(a); + delete(saveModule); + return Result::Unknown; + } + } + if (remove) delete(a); + return Result::NonSupport; +} + + +Result Saver::sync() noexcept +{ + if (!pImpl->saveModule) return Result::InsufficientCondition; + pImpl->saveModule->close(); + delete(pImpl->saveModule); + pImpl->saveModule = nullptr; + + return Result::Success; +} + + +unique_ptr Saver::gen() noexcept +{ + return unique_ptr(new Saver); +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgScene.cpp b/include/liblvgl/libs/thorvg/tvgScene.cpp new file mode 100644 index 00000000..cd911f71 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgScene.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgScene.h" + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +Scene::Scene() : pImpl(new Impl(this)) +{ + Paint::pImpl->id = TVG_CLASS_ID_SCENE; +} + + +Scene::~Scene() +{ + delete(pImpl); +} + + +unique_ptr Scene::gen() noexcept +{ + return unique_ptr(new Scene); +} + + +uint32_t Scene::identifier() noexcept +{ + return TVG_CLASS_ID_SCENE; +} + + +Result Scene::push(unique_ptr paint) noexcept +{ + auto p = paint.release(); + if (!p) return Result::MemoryCorruption; + PP(p)->ref(); + pImpl->paints.push_back(p); + + return Result::Success; +} + + +Result Scene::reserve(TVG_UNUSED uint32_t size) noexcept +{ + return Result::NonSupport; +} + + +Result Scene::clear(bool free) noexcept +{ + pImpl->clear(free); + + return Result::Success; +} + + +list& Scene::paints() noexcept +{ + return pImpl->paints; +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgScene.h b/include/liblvgl/libs/thorvg/tvgScene.h new file mode 100644 index 00000000..8347d924 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgScene.h @@ -0,0 +1,235 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_SCENE_H_ +#define _TVG_SCENE_H_ + +#include +#include "tvgPaint.h" + + +struct SceneIterator : Iterator +{ + list* paints; + list::iterator itr; + + SceneIterator(list* p) : paints(p) + { + begin(); + } + + const Paint* next() override + { + if (itr == paints->end()) return nullptr; + auto paint = *itr; + ++itr; + return paint; + } + + uint32_t count() override + { + return paints->size(); + } + + void begin() override + { + itr = paints->begin(); + } +}; + +struct Scene::Impl +{ + list paints; + RenderData rd = nullptr; + Scene* scene = nullptr; + uint8_t opacity; //for composition + bool needComp = false; //composite or not + + Impl(Scene* s) : scene(s) + { + } + + ~Impl() + { + for (auto paint : paints) { + if (P(paint)->unref() == 0) delete(paint); + } + + if (auto renderer = PP(scene)->renderer) { + renderer->dispose(rd); + } + } + + bool needComposition(uint8_t opacity) + { + if (opacity == 0 || paints.empty()) return false; + + //Masking may require composition (even if opacity == 255) + auto compMethod = scene->composite(nullptr); + if (compMethod != CompositeMethod::None && compMethod != CompositeMethod::ClipPath) return true; + + //Blending may require composition (even if opacity == 255) + if (scene->blend() != BlendMethod::Normal) return true; + + //Half translucent requires intermediate composition. + if (opacity == 255) return false; + + //If scene has several children or only scene, it may require composition. + //OPTIMIZE: the bitmap type of the picture would not need the composition. + //OPTIMIZE: a single paint of a scene would not need the composition. + if (paints.size() == 1 && paints.front()->identifier() == TVG_CLASS_ID_SHAPE) return false; + + return true; + } + + RenderData update(RenderMethod* renderer, const RenderTransform* transform, Array& clips, uint8_t opacity, RenderUpdateFlag flag, bool clipper) + { + if ((needComp = needComposition(opacity))) { + /* Overriding opacity value. If this scene is half-translucent, + It must do intermediate composition with that opacity value. */ + this->opacity = opacity; + opacity = 255; + } + + if (clipper) { + Array rds(paints.size()); + for (auto paint : paints) { + rds.push(paint->pImpl->update(renderer, transform, clips, opacity, flag, true)); + } + rd = renderer->prepare(rds, rd, transform, clips, opacity, flag); + return rd; + } else { + for (auto paint : paints) { + paint->pImpl->update(renderer, transform, clips, opacity, flag, false); + } + return nullptr; + } + } + + bool render(RenderMethod* renderer) + { + Compositor* cmp = nullptr; + auto ret = true; + + if (needComp) { + cmp = renderer->target(bounds(renderer), renderer->colorSpace()); + renderer->beginComposite(cmp, CompositeMethod::None, opacity); + } + + for (auto paint : paints) { + ret &= paint->pImpl->render(renderer); + } + + if (cmp) renderer->endComposite(cmp); + + return ret; + } + + RenderRegion bounds(RenderMethod* renderer) const + { + if (paints.empty()) return {0, 0, 0, 0}; + + int32_t x1 = INT32_MAX; + int32_t y1 = INT32_MAX; + int32_t x2 = 0; + int32_t y2 = 0; + + for (auto paint : paints) { + auto region = paint->pImpl->bounds(renderer); + + //Merge regions + if (region.x < x1) x1 = region.x; + if (x2 < region.x + region.w) x2 = (region.x + region.w); + if (region.y < y1) y1 = region.y; + if (y2 < region.y + region.h) y2 = (region.y + region.h); + } + + return {x1, y1, (x2 - x1), (y2 - y1)}; + } + + bool bounds(float* px, float* py, float* pw, float* ph, bool stroking) + { + if (paints.empty()) return false; + + auto x1 = FLT_MAX; + auto y1 = FLT_MAX; + auto x2 = -FLT_MAX; + auto y2 = -FLT_MAX; + + for (auto paint : paints) { + auto x = FLT_MAX; + auto y = FLT_MAX; + auto w = 0.0f; + auto h = 0.0f; + + if (!P(paint)->bounds(&x, &y, &w, &h, true, stroking)) continue; + + //Merge regions + if (x < x1) x1 = x; + if (x2 < x + w) x2 = (x + w); + if (y < y1) y1 = y; + if (y2 < y + h) y2 = (y + h); + } + + if (px) *px = x1; + if (py) *py = y1; + if (pw) *pw = (x2 - x1); + if (ph) *ph = (y2 - y1); + + return true; + } + + Paint* duplicate() + { + auto ret = Scene::gen().release(); + auto dup = ret->pImpl; + + for (auto paint : paints) { + auto cdup = paint->duplicate(); + P(cdup)->ref(); + dup->paints.push_back(cdup); + } + + return ret; + } + + void clear(bool free) + { + for (auto paint : paints) { + if (P(paint)->unref() == 0 && free) delete(paint); + } + paints.clear(); + } + + Iterator* iterator() + { + return new SceneIterator(&paints); + } +}; + +#endif //_TVG_SCENE_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgShape.cpp b/include/liblvgl/libs/thorvg/tvgShape.cpp new file mode 100644 index 00000000..fcf77e24 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgShape.cpp @@ -0,0 +1,411 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgMath.h" +#include "tvgShape.h" + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +Shape :: Shape() : pImpl(new Impl(this)) +{ + Paint::pImpl->id = TVG_CLASS_ID_SHAPE; +} + + +Shape :: ~Shape() +{ + delete(pImpl); +} + + +unique_ptr Shape::gen() noexcept +{ + return unique_ptr(new Shape); +} + + +uint32_t Shape::identifier() noexcept +{ + return TVG_CLASS_ID_SHAPE; +} + + +Result Shape::reset() noexcept +{ + pImpl->rs.path.cmds.clear(); + pImpl->rs.path.pts.clear(); + + pImpl->flag |= RenderUpdateFlag::Path; + + return Result::Success; +} + + +uint32_t Shape::pathCommands(const PathCommand** cmds) const noexcept +{ + if (cmds) *cmds = pImpl->rs.path.cmds.data; + return pImpl->rs.path.cmds.count; +} + + +uint32_t Shape::pathCoords(const Point** pts) const noexcept +{ + if (pts) *pts = pImpl->rs.path.pts.data; + return pImpl->rs.path.pts.count; +} + + +Result Shape::appendPath(const PathCommand *cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt) noexcept +{ + if (cmdCnt == 0 || ptsCnt == 0 || !cmds || !pts) return Result::InvalidArguments; + + pImpl->grow(cmdCnt, ptsCnt); + pImpl->append(cmds, cmdCnt, pts, ptsCnt); + + return Result::Success; +} + + +Result Shape::moveTo(float x, float y) noexcept +{ + pImpl->moveTo(x, y); + + return Result::Success; +} + + +Result Shape::lineTo(float x, float y) noexcept +{ + pImpl->lineTo(x, y); + + return Result::Success; +} + + +Result Shape::cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) noexcept +{ + pImpl->cubicTo(cx1, cy1, cx2, cy2, x, y); + + return Result::Success; +} + + +Result Shape::close() noexcept +{ + pImpl->close(); + + return Result::Success; +} + + +Result Shape::appendCircle(float cx, float cy, float rx, float ry) noexcept +{ + auto rxKappa = rx * PATH_KAPPA; + auto ryKappa = ry * PATH_KAPPA; + + pImpl->grow(6, 13); + pImpl->moveTo(cx + rx, cy); + pImpl->cubicTo(cx + rx, cy + ryKappa, cx + rxKappa, cy + ry, cx, cy + ry); + pImpl->cubicTo(cx - rxKappa, cy + ry, cx - rx, cy + ryKappa, cx - rx, cy); + pImpl->cubicTo(cx - rx, cy - ryKappa, cx - rxKappa, cy - ry, cx, cy - ry); + pImpl->cubicTo(cx + rxKappa, cy - ry, cx + rx, cy - ryKappa, cx + rx, cy); + pImpl->close(); + + return Result::Success; +} + +Result Shape::appendArc(float cx, float cy, float radius, float startAngle, float sweep, bool pie) noexcept +{ + //just circle + if (sweep >= 360.0f || sweep <= -360.0f) return appendCircle(cx, cy, radius, radius); + + const float arcPrecision = 1e-5f; + startAngle = mathDeg2Rad(startAngle); + sweep = mathDeg2Rad(sweep); + + auto nCurves = static_cast(fabsf(sweep / MATH_PI2)); + if (fabsf(sweep / MATH_PI2) - nCurves > arcPrecision) ++nCurves; + auto sweepSign = (sweep < 0 ? -1 : 1); + auto fract = fmodf(sweep, MATH_PI2); + fract = (fabsf(fract) < arcPrecision) ? MATH_PI2 * sweepSign : fract; + + //Start from here + Point start = {radius * cosf(startAngle), radius * sinf(startAngle)}; + + if (pie) { + pImpl->moveTo(cx, cy); + pImpl->lineTo(start.x + cx, start.y + cy); + } else { + pImpl->moveTo(start.x + cx, start.y + cy); + } + + for (int i = 0; i < nCurves; ++i) { + auto endAngle = startAngle + ((i != nCurves - 1) ? MATH_PI2 * sweepSign : fract); + Point end = {radius * cosf(endAngle), radius * sinf(endAngle)}; + + //variables needed to calculate bezier control points + + //get bezier control points using article: + //(http://itc.ktu.lt/index.php/ITC/article/view/11812/6479) + auto ax = start.x; + auto ay = start.y; + auto bx = end.x; + auto by = end.y; + auto q1 = ax * ax + ay * ay; + auto q2 = ax * bx + ay * by + q1; + auto k2 = (4.0f/3.0f) * ((sqrtf(2 * q1 * q2) - q2) / (ax * by - ay * bx)); + + start = end; //Next start point is the current end point + + end.x += cx; + end.y += cy; + + Point ctrl1 = {ax - k2 * ay + cx, ay + k2 * ax + cy}; + Point ctrl2 = {bx + k2 * by + cx, by - k2 * bx + cy}; + + pImpl->cubicTo(ctrl1.x, ctrl1.y, ctrl2.x, ctrl2.y, end.x, end.y); + + startAngle = endAngle; + } + + if (pie) pImpl->close(); + + return Result::Success; +} + + +Result Shape::appendRect(float x, float y, float w, float h, float rx, float ry) noexcept +{ + auto halfW = w * 0.5f; + auto halfH = h * 0.5f; + + //clamping cornerRadius by minimum size + if (rx > halfW) rx = halfW; + if (ry > halfH) ry = halfH; + + //rectangle + if (rx == 0 && ry == 0) { + pImpl->grow(5, 4); + pImpl->moveTo(x, y); + pImpl->lineTo(x + w, y); + pImpl->lineTo(x + w, y + h); + pImpl->lineTo(x, y + h); + pImpl->close(); + //rounded rectangle or circle + } else { + auto hrx = rx * PATH_KAPPA; + auto hry = ry * PATH_KAPPA; + pImpl->grow(10, 17); + pImpl->moveTo(x + rx, y); + pImpl->lineTo(x + w - rx, y); + pImpl->cubicTo(x + w - rx + hrx, y, x + w, y + ry - hry, x + w, y + ry); + pImpl->lineTo(x + w, y + h - ry); + pImpl->cubicTo(x + w, y + h - ry + hry, x + w - rx + hrx, y + h, x + w - rx, y + h); + pImpl->lineTo(x + rx, y + h); + pImpl->cubicTo(x + rx - hrx, y + h, x, y + h - ry + hry, x, y + h - ry); + pImpl->lineTo(x, y + ry); + pImpl->cubicTo(x, y + ry - hry, x + rx - hrx, y, x + rx, y); + pImpl->close(); + } + + return Result::Success; +} + + +Result Shape::fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept +{ + if (pImpl->rs.fill) { + delete(pImpl->rs.fill); + pImpl->rs.fill = nullptr; + pImpl->flag |= RenderUpdateFlag::Gradient; + } + + if (r == pImpl->rs.color[0] && g == pImpl->rs.color[1] && b == pImpl->rs.color[2] && a == pImpl->rs.color[3]) return Result::Success; + + pImpl->rs.color[0] = r; + pImpl->rs.color[1] = g; + pImpl->rs.color[2] = b; + pImpl->rs.color[3] = a; + pImpl->flag |= RenderUpdateFlag::Color; + + return Result::Success; +} + + +Result Shape::fill(unique_ptr f) noexcept +{ + auto p = f.release(); + if (!p) return Result::MemoryCorruption; + + if (pImpl->rs.fill && pImpl->rs.fill != p) delete(pImpl->rs.fill); + pImpl->rs.fill = p; + pImpl->flag |= RenderUpdateFlag::Gradient; + + return Result::Success; +} + + +Result Shape::fillColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept +{ + pImpl->rs.fillColor(r, g, b, a); + + return Result::Success; +} + + +const Fill* Shape::fill() const noexcept +{ + return pImpl->rs.fill; +} + + +Result Shape::order(bool strokeFirst) noexcept +{ + if (!pImpl->strokeFirst(strokeFirst)) return Result::FailedAllocation; + + return Result::Success; +} + + +Result Shape::stroke(float width) noexcept +{ + if (!pImpl->strokeWidth(width)) return Result::FailedAllocation; + + return Result::Success; +} + + +float Shape::strokeWidth() const noexcept +{ + return pImpl->rs.strokeWidth(); +} + + +Result Shape::stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept +{ + if (!pImpl->strokeColor(r, g, b, a)) return Result::FailedAllocation; + + return Result::Success; +} + + +Result Shape::strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept +{ + if (!pImpl->rs.strokeColor(r, g, b, a)) return Result::InsufficientCondition; + + return Result::Success; +} + + +Result Shape::stroke(unique_ptr f) noexcept +{ + return pImpl->strokeFill(std::move(f)); +} + + +const Fill* Shape::strokeFill() const noexcept +{ + return pImpl->rs.strokeFill(); +} + + +Result Shape::stroke(const float* dashPattern, uint32_t cnt) noexcept +{ + return pImpl->strokeDash(dashPattern, cnt, 0); +} + + +uint32_t Shape::strokeDash(const float** dashPattern) const noexcept +{ + return pImpl->rs.strokeDash(dashPattern, nullptr); +} + + +Result Shape::stroke(StrokeCap cap) noexcept +{ + if (!pImpl->strokeCap(cap)) return Result::FailedAllocation; + + return Result::Success; +} + + +Result Shape::stroke(StrokeJoin join) noexcept +{ + if (!pImpl->strokeJoin(join)) return Result::FailedAllocation; + + return Result::Success; +} + +Result Shape::strokeMiterlimit(float miterlimit) noexcept +{ + // https://www.w3.org/TR/SVG2/painting.html#LineJoin + // - A negative value for stroke-miterlimit must be treated as an illegal value. + if (miterlimit < 0.0f) return Result::NonSupport; + // TODO Find out a reasonable max value. + if (!pImpl->strokeMiterlimit(miterlimit)) return Result::FailedAllocation; + + return Result::Success; +} + + +StrokeCap Shape::strokeCap() const noexcept +{ + return pImpl->rs.strokeCap(); +} + + +StrokeJoin Shape::strokeJoin() const noexcept +{ + return pImpl->rs.strokeJoin(); +} + +float Shape::strokeMiterlimit() const noexcept +{ + return pImpl->rs.strokeMiterlimit(); +} + + +Result Shape::fill(FillRule r) noexcept +{ + pImpl->rs.rule = r; + + return Result::Success; +} + + +FillRule Shape::fillRule() const noexcept +{ + return pImpl->rs.rule; +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgShape.h b/include/liblvgl/libs/thorvg/tvgShape.h new file mode 100644 index 00000000..bb5e6f37 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgShape.h @@ -0,0 +1,407 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_SHAPE_H_ +#define _TVG_SHAPE_H_ + +#include +#include "tvgMath.h" +#include "tvgPaint.h" + + +struct Shape::Impl +{ + RenderShape rs; //shape data + RenderData rd = nullptr; //engine data + Shape* shape; + uint8_t flag = RenderUpdateFlag::None; + uint8_t opacity; //for composition + bool needComp = false; //composite or not + + Impl(Shape* s) : shape(s) + { + } + + ~Impl() + { + if (auto renderer = PP(shape)->renderer) { + renderer->dispose(rd); + } + } + + bool render(RenderMethod* renderer) + { + Compositor* cmp = nullptr; + bool ret; + + if (needComp) { + cmp = renderer->target(bounds(renderer), renderer->colorSpace()); + renderer->beginComposite(cmp, CompositeMethod::None, opacity); + } + ret = renderer->renderShape(rd); + if (cmp) renderer->endComposite(cmp); + return ret; + } + + bool needComposition(uint8_t opacity) + { + if (opacity == 0) return false; + + //Shape composition is only necessary when stroking & fill are valid. + if (!rs.stroke || rs.stroke->width < FLOAT_EPSILON || (!rs.stroke->fill && rs.stroke->color[3] == 0)) return false; + if (!rs.fill && rs.color[3] == 0) return false; + + //translucent fill & stroke + if (opacity < 255) return true; + + //Composition test + const Paint* target; + auto method = shape->composite(&target); + if (!target || method == CompositeMethod::ClipPath) return false; + if (target->pImpl->opacity == 255 || target->pImpl->opacity == 0) { + if (target->identifier() == TVG_CLASS_ID_SHAPE) { + auto shape = static_cast(target); + if (!shape->fill()) { + uint8_t r, g, b, a; + shape->fillColor(&r, &g, &b, &a); + if (a == 0 || a == 255) { + if (method == CompositeMethod::LumaMask || method == CompositeMethod::InvLumaMask) { + if ((r == 255 && g == 255 && b == 255) || (r == 0 && g == 0 && b == 0)) return false; + } else return false; + } + } + } + } + + return true; + } + + RenderData update(RenderMethod* renderer, const RenderTransform* transform, Array& clips, uint8_t opacity, RenderUpdateFlag pFlag, bool clipper) + { + if ((needComp = needComposition(opacity))) { + /* Overriding opacity value. If this scene is half-translucent, + It must do intermediate composition with that opacity value. */ + this->opacity = opacity; + opacity = 255; + } + + rd = renderer->prepare(rs, rd, transform, clips, opacity, static_cast(pFlag | flag), clipper); + flag = RenderUpdateFlag::None; + return rd; + } + + RenderRegion bounds(RenderMethod* renderer) + { + return renderer->region(rd); + } + + bool bounds(float* x, float* y, float* w, float* h, bool stroking) + { + //Path bounding size + if (rs.path.pts.count > 0 ) { + auto pts = rs.path.pts.begin(); + Point min = { pts->x, pts->y }; + Point max = { pts->x, pts->y }; + + for (auto pts2 = pts + 1; pts2 < rs.path.pts.end(); ++pts2) { + if (pts2->x < min.x) min.x = pts2->x; + if (pts2->y < min.y) min.y = pts2->y; + if (pts2->x > max.x) max.x = pts2->x; + if (pts2->y > max.y) max.y = pts2->y; + } + + if (x) *x = min.x; + if (y) *y = min.y; + if (w) *w = max.x - min.x; + if (h) *h = max.y - min.y; + } + + //Stroke feathering + if (stroking && rs.stroke) { + if (x) *x -= rs.stroke->width * 0.5f; + if (y) *y -= rs.stroke->width * 0.5f; + if (w) *w += rs.stroke->width; + if (h) *h += rs.stroke->width; + } + return rs.path.pts.count > 0 ? true : false; + } + + void reserveCmd(uint32_t cmdCnt) + { + rs.path.cmds.reserve(cmdCnt); + } + + void reservePts(uint32_t ptsCnt) + { + rs.path.pts.reserve(ptsCnt); + } + + void grow(uint32_t cmdCnt, uint32_t ptsCnt) + { + rs.path.cmds.grow(cmdCnt); + rs.path.pts.grow(ptsCnt); + } + + void append(const PathCommand* cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt) + { + memcpy(rs.path.cmds.end(), cmds, sizeof(PathCommand) * cmdCnt); + memcpy(rs.path.pts.end(), pts, sizeof(Point) * ptsCnt); + rs.path.cmds.count += cmdCnt; + rs.path.pts.count += ptsCnt; + + flag |= RenderUpdateFlag::Path; + } + + void moveTo(float x, float y) + { + rs.path.cmds.push(PathCommand::MoveTo); + rs.path.pts.push({x, y}); + + flag |= RenderUpdateFlag::Path; + } + + void lineTo(float x, float y) + { + rs.path.cmds.push(PathCommand::LineTo); + rs.path.pts.push({x, y}); + + flag |= RenderUpdateFlag::Path; + } + + void cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) + { + rs.path.cmds.push(PathCommand::CubicTo); + rs.path.pts.push({cx1, cy1}); + rs.path.pts.push({cx2, cy2}); + rs.path.pts.push({x, y}); + + flag |= RenderUpdateFlag::Path; + } + + void close() + { + //Don't close multiple times. + if (rs.path.cmds.count > 0 && rs.path.cmds.last() == PathCommand::Close) return; + + rs.path.cmds.push(PathCommand::Close); + + flag |= RenderUpdateFlag::Path; + } + + bool strokeWidth(float width) + { + if (!rs.stroke) rs.stroke = new RenderStroke(); + rs.stroke->width = width; + flag |= RenderUpdateFlag::Stroke; + + return true; + } + + bool strokeTrim(float begin, float end, bool individual) + { + if (!rs.stroke) { + if (begin == 0.0f && end == 1.0f) return true; + rs.stroke = new RenderStroke(); + } + + if (mathEqual(rs.stroke->trim.begin, begin) && mathEqual(rs.stroke->trim.end, end)) return true; + + rs.stroke->trim.begin = begin; + rs.stroke->trim.end = end; + rs.stroke->trim.individual = individual; + flag |= RenderUpdateFlag::Stroke; + + return true; + } + + bool strokeCap(StrokeCap cap) + { + if (!rs.stroke) rs.stroke = new RenderStroke(); + rs.stroke->cap = cap; + flag |= RenderUpdateFlag::Stroke; + + return true; + } + + bool strokeJoin(StrokeJoin join) + { + if (!rs.stroke) rs.stroke = new RenderStroke(); + rs.stroke->join = join; + flag |= RenderUpdateFlag::Stroke; + + return true; + } + + bool strokeMiterlimit(float miterlimit) + { + if (!rs.stroke) rs.stroke = new RenderStroke(); + rs.stroke->miterlimit = miterlimit; + flag |= RenderUpdateFlag::Stroke; + + return true; + } + + bool strokeColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a) + { + if (!rs.stroke) rs.stroke = new RenderStroke(); + if (rs.stroke->fill) { + delete(rs.stroke->fill); + rs.stroke->fill = nullptr; + flag |= RenderUpdateFlag::GradientStroke; + } + + rs.stroke->color[0] = r; + rs.stroke->color[1] = g; + rs.stroke->color[2] = b; + rs.stroke->color[3] = a; + + flag |= RenderUpdateFlag::Stroke; + + return true; + } + + Result strokeFill(unique_ptr f) + { + auto p = f.release(); + if (!p) return Result::MemoryCorruption; + + if (!rs.stroke) rs.stroke = new RenderStroke(); + if (rs.stroke->fill && rs.stroke->fill != p) delete(rs.stroke->fill); + rs.stroke->fill = p; + + flag |= RenderUpdateFlag::Stroke; + flag |= RenderUpdateFlag::GradientStroke; + + return Result::Success; + } + + Result strokeDash(const float* pattern, uint32_t cnt, float offset) + { + if ((cnt == 1) || (!pattern && cnt > 0) || (pattern && cnt == 0)) { + return Result::InvalidArguments; + } + + for (uint32_t i = 0; i < cnt; i++) { + if (pattern[i] < FLOAT_EPSILON) return Result::InvalidArguments; + } + + //Reset dash + if (!pattern && cnt == 0) { + free(rs.stroke->dashPattern); + rs.stroke->dashPattern = nullptr; + } else { + if (!rs.stroke) rs.stroke = new RenderStroke(); + if (rs.stroke->dashCnt != cnt) { + free(rs.stroke->dashPattern); + rs.stroke->dashPattern = nullptr; + } + if (!rs.stroke->dashPattern) { + rs.stroke->dashPattern = static_cast(malloc(sizeof(float) * cnt)); + if (!rs.stroke->dashPattern) return Result::FailedAllocation; + } + for (uint32_t i = 0; i < cnt; ++i) { + rs.stroke->dashPattern[i] = pattern[i]; + } + } + rs.stroke->dashCnt = cnt; + rs.stroke->dashOffset = offset; + flag |= RenderUpdateFlag::Stroke; + + return Result::Success; + } + + bool strokeFirst() + { + if (!rs.stroke) return true; + return rs.stroke->strokeFirst; + } + + bool strokeFirst(bool strokeFirst) + { + if (!rs.stroke) rs.stroke = new RenderStroke(); + rs.stroke->strokeFirst = strokeFirst; + flag |= RenderUpdateFlag::Stroke; + + return true; + } + + void update(RenderUpdateFlag flag) + { + this->flag |= flag; + } + + Paint* duplicate() + { + auto ret = Shape::gen().release(); + auto dup = ret->pImpl; + + dup->rs.rule = rs.rule; + + //Color + memcpy(dup->rs.color, rs.color, sizeof(rs.color)); + dup->flag = RenderUpdateFlag::Color; + + //Path + if (rs.path.cmds.count > 0 && rs.path.pts.count > 0) { + dup->rs.path.cmds = rs.path.cmds; + dup->rs.path.pts = rs.path.pts; + dup->flag |= RenderUpdateFlag::Path; + } + + //Stroke + if (rs.stroke) { + dup->rs.stroke = new RenderStroke(); + *dup->rs.stroke = *rs.stroke; + memcpy(dup->rs.stroke->color, rs.stroke->color, sizeof(rs.stroke->color)); + if (rs.stroke->dashCnt > 0) { + dup->rs.stroke->dashPattern = static_cast(malloc(sizeof(float) * rs.stroke->dashCnt)); + memcpy(dup->rs.stroke->dashPattern, rs.stroke->dashPattern, sizeof(float) * rs.stroke->dashCnt); + } + if (rs.stroke->fill) { + dup->rs.stroke->fill = rs.stroke->fill->duplicate(); + dup->flag |= RenderUpdateFlag::GradientStroke; + } + dup->flag |= RenderUpdateFlag::Stroke; + } + + //Fill + if (rs.fill) { + dup->rs.fill = rs.fill->duplicate(); + dup->flag |= RenderUpdateFlag::Gradient; + } + + return ret; + } + + Iterator* iterator() + { + return nullptr; + } +}; + +#endif //_TVG_SHAPE_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgStr.cpp b/include/liblvgl/libs/thorvg/tvgStr.cpp new file mode 100644 index 00000000..1838e756 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgStr.cpp @@ -0,0 +1,248 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "config.h" +#include +#include +#include +#include "tvgMath.h" +#include "tvgStr.h" + + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +static inline bool _floatExact(float a, float b) +{ + return memcmp(&a, &b, sizeof(float)) == 0; +} + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +namespace tvg { + +/* + * https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/strtof-strtof-l-wcstof-wcstof-l?view=msvc-160 + * + * src should be one of the following form : + * + * [whitespace] [sign] {digits [radix digits] | radix digits} [{e | E} [sign] digits] + * [whitespace] [sign] {INF | INFINITY} + * [whitespace] [sign] NAN [sequence] + * + * No hexadecimal form supported + * no sequence supported after NAN + */ +float strToFloat(const char *nPtr, char **endPtr) +{ + if (endPtr) *endPtr = (char *) (nPtr); + if (!nPtr) return 0.0f; + + auto a = nPtr; + auto iter = nPtr; + auto val = 0.0f; + unsigned long long integerPart = 0; + int minus = 1; + + //ignore leading whitespaces + while (isspace(*iter)) iter++; + + //signed or not + if (*iter == '-') { + minus = -1; + iter++; + } else if (*iter == '+') { + iter++; + } + + if (tolower(*iter) == 'i') { + if ((tolower(*(iter + 1)) == 'n') && (tolower(*(iter + 2)) == 'f')) iter += 3; + else goto error; + + if (tolower(*(iter)) == 'i') { + if ((tolower(*(iter + 1)) == 'n') && (tolower(*(iter + 2)) == 'i') && (tolower(*(iter + 3)) == 't') && + (tolower(*(iter + 4)) == 'y')) + iter += 5; + else goto error; + } + if (endPtr) *endPtr = (char *) (iter); + return (minus == -1) ? -INFINITY : INFINITY; + } + + if (tolower(*iter) == 'n') { + if ((tolower(*(iter + 1)) == 'a') && (tolower(*(iter + 2)) == 'n')) iter += 3; + else goto error; + + if (endPtr) *endPtr = (char *) (iter); + return (minus == -1) ? -NAN : NAN; + } + + //Optional: integer part before dot + if (isdigit(*iter)) { + for (; isdigit(*iter); iter++) { + integerPart = integerPart * 10ULL + (unsigned long long) (*iter - '0'); + } + a = iter; + } else if (*iter != '.') { + goto success; + } + + val = static_cast(integerPart); + + //Optional: decimal part after dot + if (*iter == '.') { + unsigned long long decimalPart = 0; + unsigned long long pow10 = 1; + int count = 0; + + iter++; + + if (isdigit(*iter)) { + for (; isdigit(*iter); iter++, count++) { + if (count < 19) { + decimalPart = decimalPart * 10ULL + +static_cast(*iter - '0'); + pow10 *= 10ULL; + } + } + } else if (isspace(*iter)) { //skip if there is a space after the dot. + a = iter; + goto success; + } + + val += static_cast(decimalPart) / static_cast(pow10); + a = iter; + } + + //Optional: exponent + if (*iter == 'e' || *iter == 'E') { + ++iter; + + //Exception: svg may have 'em' unit for fonts. ex) 5em, 10.5em + if ((*iter == 'm') || (*iter == 'M')) { + //TODO: We don't support font em unit now, but has to multiply val * font size later... + a = iter + 1; + goto success; + } + + //signed or not + int minus_e = 1; + + if (*iter == '-') { + minus_e = -1; + ++iter; + } else if (*iter == '+') { + iter++; + } + + unsigned int exponentPart = 0; + + if (isdigit(*iter)) { + while (*iter == '0') iter++; + for (; isdigit(*iter); iter++) { + exponentPart = exponentPart * 10U + static_cast(*iter - '0'); + } + } else if (!isdigit(*(a - 1))) { + a = nPtr; + goto success; + } else if (*iter == 0) { + goto success; + } + + //if ((_floatExact(val, 2.2250738585072011f)) && ((minus_e * static_cast(exponentPart)) <= -308)) { + if ((_floatExact(val, 1.175494351f)) && ((minus_e * static_cast(exponentPart)) <= -38)) { + //val *= 1.0e-308f; + val *= 1.0e-38f; + a = iter; + goto success; + } + + a = iter; + auto scale = 1.0f; + + while (exponentPart >= 8U) { + scale *= 1E8f; + exponentPart -= 8U; + } + while (exponentPart > 0U) { + scale *= 10.0f; + exponentPart--; + } + val = (minus_e == -1) ? (val / scale) : (val * scale); + } else if ((iter > nPtr) && !isdigit(*(iter - 1))) { + a = nPtr; + goto success; + } + +success: + if (endPtr) *endPtr = (char *)(a); + if (!std::isfinite(val)) return 0.0f; + + return minus * val; + +error: + if (endPtr) *endPtr = (char *)(nPtr); + return 0.0f; +} + + +int str2int(const char* str, size_t n) +{ + int ret = 0; + for(size_t i = 0; i < n; ++i) { + ret = ret * 10 + (str[i] - '0'); + } + return ret; +} + +char* strDuplicate(const char *str, size_t n) +{ + auto len = strlen(str); + if (len < n) n = len; + + auto ret = (char *) malloc(n + 1); + if (!ret) return nullptr; + ret[n] = '\0'; + + return (char *) memcpy(ret, str, n); +} + +char* strDirname(const char* path) +{ + const char *ptr = strrchr(path, '/'); +#ifdef _WIN32 + if (ptr) ptr = strrchr(ptr + 1, '\\'); +#endif + int len = int(ptr + 1 - path); // +1 to include '/' + return strDuplicate(path, len); +} + +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgStr.h b/include/liblvgl/libs/thorvg/tvgStr.h new file mode 100644 index 00000000..154a1995 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgStr.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_STR_H_ +#define _TVG_STR_H_ + +#include + +namespace tvg +{ + +float strToFloat(const char *nPtr, char **endPtr); //convert to float +int str2int(const char* str, size_t n); //convert to integer +char* strDuplicate(const char *str, size_t n); //copy the string +char* strDirname(const char* path); //return the full directory name + +} +#endif //_TVG_STR_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSvgCssStyle.cpp b/include/liblvgl/libs/thorvg/tvgSvgCssStyle.cpp new file mode 100644 index 00000000..11274e33 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSvgCssStyle.cpp @@ -0,0 +1,271 @@ +/* + * Copyright (c) 2022 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgSvgCssStyle.h" + +#include + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +static bool _isImportanceApplicable(SvgStyleFlags &toFlagsImportance, SvgStyleFlags fromFlagsImportance, SvgStyleFlags flag) +{ + if (!(toFlagsImportance & flag) && (fromFlagsImportance & flag)) { + return true; + } + return false; +} + +static void _copyStyle(SvgStyleProperty* to, const SvgStyleProperty* from) +{ + if (from == nullptr) return; + //Copy the properties of 'from' only if they were explicitly set (not the default ones). + if ((from->curColorSet && !(to->flags & SvgStyleFlags::Color)) || + _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::Color)) { + to->color = from->color; + to->curColorSet = true; + to->flags = (to->flags | SvgStyleFlags::Color); + if (from->flagsImportance & SvgStyleFlags::Color) { + to->flagsImportance = (to->flagsImportance | SvgStyleFlags::Color); + } + } + if (((from->flags & SvgStyleFlags::PaintOrder) && !(to->flags & SvgStyleFlags::PaintOrder)) || + _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::PaintOrder)) { + to->paintOrder = from->paintOrder; + to->flags = (to->flags | SvgStyleFlags::PaintOrder); + if (from->flagsImportance & SvgStyleFlags::PaintOrder) { + to->flagsImportance = (to->flagsImportance | SvgStyleFlags::PaintOrder); + } + } + if (((from->flags & SvgStyleFlags::Display) && !(to->flags & SvgStyleFlags::Display)) || + _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::Display)) { + to->display = from->display; + to->flags = (to->flags | SvgStyleFlags::Display); + if (from->flagsImportance & SvgStyleFlags::Display) { + to->flagsImportance = (to->flagsImportance | SvgStyleFlags::Display); + } + } + //Fill + if (((from->fill.flags & SvgFillFlags::Paint) && !(to->flags & SvgStyleFlags::Fill)) || + _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::Fill)) { + to->fill.paint.color = from->fill.paint.color; + to->fill.paint.none = from->fill.paint.none; + to->fill.paint.curColor = from->fill.paint.curColor; + if (from->fill.paint.url) { + if (to->fill.paint.url) free(to->fill.paint.url); + to->fill.paint.url = strdup(from->fill.paint.url); + } + to->fill.flags = (to->fill.flags | SvgFillFlags::Paint); + to->flags = (to->flags | SvgStyleFlags::Fill); + if (from->flagsImportance & SvgStyleFlags::Fill) { + to->flagsImportance = (to->flagsImportance | SvgStyleFlags::Fill); + } + } + if (((from->fill.flags & SvgFillFlags::Opacity) && !(to->flags & SvgStyleFlags::FillOpacity)) || + _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::FillOpacity)) { + to->fill.opacity = from->fill.opacity; + to->fill.flags = (to->fill.flags | SvgFillFlags::Opacity); + to->flags = (to->flags | SvgStyleFlags::FillOpacity); + if (from->flagsImportance & SvgStyleFlags::FillOpacity) { + to->flagsImportance = (to->flagsImportance | SvgStyleFlags::FillOpacity); + } + } + if (((from->fill.flags & SvgFillFlags::FillRule) && !(to->flags & SvgStyleFlags::FillRule)) || + _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::FillRule)) { + to->fill.fillRule = from->fill.fillRule; + to->fill.flags = (to->fill.flags | SvgFillFlags::FillRule); + to->flags = (to->flags | SvgStyleFlags::FillRule); + if (from->flagsImportance & SvgStyleFlags::FillRule) { + to->flagsImportance = (to->flagsImportance | SvgStyleFlags::FillRule); + } + } + //Stroke + if (((from->stroke.flags & SvgStrokeFlags::Paint) && !(to->flags & SvgStyleFlags::Stroke)) || + _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::Stroke)) { + to->stroke.paint.color = from->stroke.paint.color; + to->stroke.paint.none = from->stroke.paint.none; + to->stroke.paint.curColor = from->stroke.paint.curColor; + if (from->stroke.paint.url) { + if (to->stroke.paint.url) free(to->stroke.paint.url); + to->stroke.paint.url = strdup(from->stroke.paint.url); + } + to->stroke.flags = (to->stroke.flags | SvgStrokeFlags::Paint); + to->flags = (to->flags | SvgStyleFlags::Stroke); + if (from->flagsImportance & SvgStyleFlags::Stroke) { + to->flagsImportance = (to->flagsImportance | SvgStyleFlags::Stroke); + } + } + if (((from->stroke.flags & SvgStrokeFlags::Opacity) && !(to->flags & SvgStyleFlags::StrokeOpacity)) || + _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::StrokeOpacity)) { + to->stroke.opacity = from->stroke.opacity; + to->stroke.flags = (to->stroke.flags | SvgStrokeFlags::Opacity); + to->flags = (to->flags | SvgStyleFlags::StrokeOpacity); + if (from->flagsImportance & SvgStyleFlags::StrokeOpacity) { + to->flagsImportance = (to->flagsImportance | SvgStyleFlags::StrokeOpacity); + } + } + if (((from->stroke.flags & SvgStrokeFlags::Width) && !(to->flags & SvgStyleFlags::StrokeWidth)) || + _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::StrokeWidth)) { + to->stroke.width = from->stroke.width; + to->stroke.flags = (to->stroke.flags | SvgStrokeFlags::Width); + to->flags = (to->flags | SvgStyleFlags::StrokeWidth); + if (from->flagsImportance & SvgStyleFlags::StrokeWidth) { + to->flagsImportance = (to->flagsImportance | SvgStyleFlags::StrokeWidth); + } + } + if (((from->stroke.flags & SvgStrokeFlags::Dash) && !(to->flags & SvgStyleFlags::StrokeDashArray)) || + _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::StrokeDashArray)) { + if (from->stroke.dash.array.count > 0) { + to->stroke.dash.array.clear(); + to->stroke.dash.array.reserve(from->stroke.dash.array.count); + for (uint32_t i = 0; i < from->stroke.dash.array.count; ++i) { + to->stroke.dash.array.push(from->stroke.dash.array[i]); + } + to->stroke.flags = (to->stroke.flags | SvgStrokeFlags::Dash); + to->flags = (to->flags | SvgStyleFlags::StrokeDashArray); + if (from->flagsImportance & SvgStyleFlags::StrokeDashArray) { + to->flagsImportance = (to->flagsImportance | SvgStyleFlags::StrokeDashArray); + } + } + } + if (((from->stroke.flags & SvgStrokeFlags::Cap) && !(to->flags & SvgStyleFlags::StrokeLineCap)) || + _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::StrokeLineCap)) { + to->stroke.cap = from->stroke.cap; + to->stroke.flags = (to->stroke.flags | SvgStrokeFlags::Cap); + to->flags = (to->flags | SvgStyleFlags::StrokeLineCap); + if (from->flagsImportance & SvgStyleFlags::StrokeLineCap) { + to->flagsImportance = (to->flagsImportance | SvgStyleFlags::StrokeLineCap); + } + } + if (((from->stroke.flags & SvgStrokeFlags::Join) && !(to->flags & SvgStyleFlags::StrokeLineJoin)) || + _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::StrokeLineJoin)) { + to->stroke.join = from->stroke.join; + to->stroke.flags = (to->stroke.flags | SvgStrokeFlags::Join); + to->flags = (to->flags | SvgStyleFlags::StrokeLineJoin); + if (from->flagsImportance & SvgStyleFlags::StrokeLineJoin) { + to->flagsImportance = (to->flagsImportance | SvgStyleFlags::StrokeLineJoin); + } + } + //Opacity + //TODO: it can be set to be 255 and shouldn't be changed by attribute 'opacity' + if ((from->opacity < 255 && !(to->flags & SvgStyleFlags::Opacity)) || + _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::Opacity)) { + to->opacity = from->opacity; + to->flags = (to->flags | SvgStyleFlags::Opacity); + if (from->flagsImportance & SvgStyleFlags::Opacity) { + to->flagsImportance = (to->flagsImportance | SvgStyleFlags::Opacity); + } + } +} + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +void cssCopyStyleAttr(SvgNode* to, const SvgNode* from) +{ + //Copy matrix attribute + if (from->transform && !(to->style->flags & SvgStyleFlags::Transform)) { + to->transform = (Matrix*)malloc(sizeof(Matrix)); + if (to->transform) { + *to->transform = *from->transform; + to->style->flags = (to->style->flags | SvgStyleFlags::Transform); + } + } + //Copy style attribute + _copyStyle(to->style, from->style); + + if (from->style->clipPath.url) { + if (to->style->clipPath.url) free(to->style->clipPath.url); + to->style->clipPath.url = strdup(from->style->clipPath.url); + } + if (from->style->mask.url) { + if (to->style->mask.url) free(to->style->mask.url); + to->style->mask.url = strdup(from->style->mask.url); + } +} + + +SvgNode* cssFindStyleNode(const SvgNode* style, const char* title, SvgNodeType type) +{ + if (!style) return nullptr; + + auto child = style->child.data; + for (uint32_t i = 0; i < style->child.count; ++i, ++child) { + if ((*child)->type == type) { + if ((!title && !(*child)->id) || (title && (*child)->id && !strcmp((*child)->id, title))) return (*child); + } + } + return nullptr; +} + + +SvgNode* cssFindStyleNode(const SvgNode* style, const char* title) +{ + if (!style || !title) return nullptr; + + auto child = style->child.data; + for (uint32_t i = 0; i < style->child.count; ++i, ++child) { + if ((*child)->type == SvgNodeType::CssStyle) { + if ((*child)->id && !strcmp((*child)->id, title)) return (*child); + } + } + return nullptr; +} + + +void cssUpdateStyle(SvgNode* doc, SvgNode* style) +{ + if (doc->child.count > 0) { + auto child = doc->child.data; + for (uint32_t i = 0; i < doc->child.count; ++i, ++child) { + if (auto cssNode = cssFindStyleNode(style, nullptr, (*child)->type)) { + cssCopyStyleAttr(*child, cssNode); + } + cssUpdateStyle(*child, style); + } + } +} + + +void cssApplyStyleToPostponeds(Array& postponeds, SvgNode* style) +{ + for (uint32_t i = 0; i < postponeds.count; ++i) { + auto nodeIdPair = postponeds[i]; + + //css styling: tag.name has higher priority than .name + if (auto cssNode = cssFindStyleNode(style, nodeIdPair.id, nodeIdPair.node->type)) { + cssCopyStyleAttr(nodeIdPair.node, cssNode); + } + if (auto cssNode = cssFindStyleNode(style, nodeIdPair.id)) { + cssCopyStyleAttr(nodeIdPair.node, cssNode); + } + } +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSvgCssStyle.h b/include/liblvgl/libs/thorvg/tvgSvgCssStyle.h new file mode 100644 index 00000000..24812b3e --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSvgCssStyle.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_SVG_CSS_STYLE_H_ +#define _TVG_SVG_CSS_STYLE_H_ + +#include "tvgSvgLoaderCommon.h" + +void cssCopyStyleAttr(SvgNode* to, const SvgNode* from); +SvgNode* cssFindStyleNode(const SvgNode* style, const char* title, SvgNodeType type); +SvgNode* cssFindStyleNode(const SvgNode* style, const char* title); +void cssUpdateStyle(SvgNode* doc, SvgNode* style); +void cssApplyStyleToPostponeds(Array& postponeds, SvgNode* style); + +#endif //_TVG_SVG_CSS_STYLE_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSvgLoader.cpp b/include/liblvgl/libs/thorvg/tvgSvgLoader.cpp new file mode 100644 index 00000000..34ca2c5b --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSvgLoader.cpp @@ -0,0 +1,3917 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +/* + * Copyright notice for the EFL: + + * Copyright (C) EFL developers (see AUTHORS) + + * All rights reserved. + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + + * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include +#include +#include +#include "tvgLoader.h" +#include "tvgXmlParser.h" +#include "tvgSvgLoader.h" +#include "tvgSvgSceneBuilder.h" +#include "tvgStr.h" +#include "tvgSvgCssStyle.h" +#include "tvgMath.h" + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +/* + * According to: https://www.w3.org/TR/SVG2/coords.html#Units + * and: https://www.w3.org/TR/css-values-4/#absolute-lengths + */ +#define PX_PER_IN 96 //1 in = 96 px +#define PX_PER_PC 16 //1 pc = 1/6 in -> PX_PER_IN/6 +#define PX_PER_PT 1.333333f //1 pt = 1/72 in -> PX_PER_IN/72 +#define PX_PER_MM 3.779528f //1 in = 25.4 mm -> PX_PER_IN/25.4 +#define PX_PER_CM 37.79528f //1 in = 2.54 cm -> PX_PER_IN/2.54 + +typedef bool (*parseAttributes)(const char* buf, unsigned bufLength, simpleXMLAttributeCb func, const void* data); +typedef SvgNode* (*FactoryMethod)(SvgLoaderData* loader, SvgNode* parent, const char* buf, unsigned bufLength, parseAttributes func); +typedef SvgStyleGradient* (*GradientFactoryMethod)(SvgLoaderData* loader, const char* buf, unsigned bufLength); + +static char* _skipSpace(const char* str, const char* end) +{ + while (((end && str < end) || (!end && *str != '\0')) && isspace(*str)) { + ++str; + } + return (char*) str; +} + + +static char* _copyId(const char* str) +{ + if (!str) return nullptr; + if (strlen(str) == 0) return nullptr; + + return strdup(str); +} + + +static const char* _skipComma(const char* content) +{ + content = _skipSpace(content, nullptr); + if (*content == ',') return content + 1; + return content; +} + + +static bool _parseNumber(const char** content, const char** end, float* number) +{ + const char* _end = end ? *end : nullptr; + + *number = strToFloat(*content, (char**)&_end); + //If the start of string is not number + if ((*content) == _end) { + if (end) *end = _end; + return false; + } + //Skip comma if any + *content = _skipComma(_end); + if (end) *end = _end; + + return true; +} + + +static constexpr struct +{ + AspectRatioAlign align; + const char* tag; +} alignTags[] = { + { AspectRatioAlign::XMinYMin, "xMinYMin" }, + { AspectRatioAlign::XMidYMin, "xMidYMin" }, + { AspectRatioAlign::XMaxYMin, "xMaxYMin" }, + { AspectRatioAlign::XMinYMid, "xMinYMid" }, + { AspectRatioAlign::XMidYMid, "xMidYMid" }, + { AspectRatioAlign::XMaxYMid, "xMaxYMid" }, + { AspectRatioAlign::XMinYMax, "xMinYMax" }, + { AspectRatioAlign::XMidYMax, "xMidYMax" }, + { AspectRatioAlign::XMaxYMax, "xMaxYMax" }, +}; + + +static void _parseAspectRatio(const char** content, AspectRatioAlign* align, AspectRatioMeetOrSlice* meetOrSlice) +{ + if (!strcmp(*content, "none")) { + *align = AspectRatioAlign::None; + return; + } + + for (unsigned int i = 0; i < sizeof(alignTags) / sizeof(alignTags[0]); i++) { + if (!strncmp(*content, alignTags[i].tag, 8)) { + *align = alignTags[i].align; + *content += 8; + *content = _skipSpace(*content, nullptr); + break; + } + } + + if (!strcmp(*content, "meet")) { + *meetOrSlice = AspectRatioMeetOrSlice::Meet; + } else if (!strcmp(*content, "slice")) { + *meetOrSlice = AspectRatioMeetOrSlice::Slice; + } +} + + +/** + * According to https://www.w3.org/TR/SVG/coords.html#Units + */ +static float _toFloat(const SvgParser* svgParse, const char* str, SvgParserLengthType type) +{ + float parsedValue = strToFloat(str, nullptr); + + if (strstr(str, "cm")) parsedValue *= PX_PER_CM; + else if (strstr(str, "mm")) parsedValue *= PX_PER_MM; + else if (strstr(str, "pt")) parsedValue *= PX_PER_PT; + else if (strstr(str, "pc")) parsedValue *= PX_PER_PC; + else if (strstr(str, "in")) parsedValue *= PX_PER_IN; + else if (strstr(str, "%")) { + if (type == SvgParserLengthType::Vertical) parsedValue = (parsedValue / 100.0f) * svgParse->global.h; + else if (type == SvgParserLengthType::Horizontal) parsedValue = (parsedValue / 100.0f) * svgParse->global.w; + else //if other then it's radius + { + float max = svgParse->global.w; + if (max < svgParse->global.h) + max = svgParse->global.h; + parsedValue = (parsedValue / 100.0f) * max; + } + } + //TODO: Implement 'em', 'ex' attributes + + return parsedValue; +} + + +static float _gradientToFloat(const SvgParser* svgParse, const char* str, bool& isPercentage) +{ + char* end = nullptr; + + float parsedValue = strToFloat(str, &end); + isPercentage = false; + + if (strstr(str, "%")) { + parsedValue = parsedValue / 100.0f; + isPercentage = true; + } + else if (strstr(str, "cm")) parsedValue *= PX_PER_CM; + else if (strstr(str, "mm")) parsedValue *= PX_PER_MM; + else if (strstr(str, "pt")) parsedValue *= PX_PER_PT; + else if (strstr(str, "pc")) parsedValue *= PX_PER_PC; + else if (strstr(str, "in")) parsedValue *= PX_PER_IN; + //TODO: Implement 'em', 'ex' attributes + + return parsedValue; +} + + +static float _toOffset(const char* str) +{ + char* end = nullptr; + auto strEnd = str + strlen(str); + + float parsedValue = strToFloat(str, &end); + + end = _skipSpace(end, nullptr); + auto ptr = strstr(str, "%"); + + if (ptr) { + parsedValue = parsedValue / 100.0f; + if (end != ptr || (end + 1) != strEnd) return 0; + } else if (end != strEnd) return 0; + + return parsedValue; +} + + +static int _toOpacity(const char* str) +{ + char* end = nullptr; + float opacity = strToFloat(str, &end); + + if (end) { + if (end[0] == '%' && end[1] == '\0') return lrint(opacity * 2.55f); + else if (*end == '\0') return lrint(opacity * 255); + } + return 255; +} + + +static SvgMaskType _toMaskType(const char* str) +{ + if (!strcmp(str, "Alpha")) return SvgMaskType::Alpha; + + return SvgMaskType::Luminance; +} + + +//The default rendering order: fill, stroke, markers +//If any is omitted, will be rendered in its default order after the specified ones. +static bool _toPaintOrder(const char* str) +{ + uint8_t position = 1; + uint8_t strokePosition = 0; + uint8_t fillPosition = 0; + + while (*str != '\0') { + str = _skipSpace(str, nullptr); + if (!strncmp(str, "fill", 4)) { + fillPosition = position++; + str += 4; + } else if (!strncmp(str, "stroke", 6)) { + strokePosition = position++; + str += 6; + } else if (!strncmp(str, "markers", 7)) { + str += 7; + } else { + return _toPaintOrder("fill stroke"); + } + } + + if (fillPosition == 0) fillPosition = position++; + if (strokePosition == 0) strokePosition = position++; + + return fillPosition < strokePosition; +} + + +#define _PARSE_TAG(Type, Name, Name1, Tags_Array, Default) \ + static Type _to##Name1(const char* str) \ + { \ + unsigned int i; \ + \ + for (i = 0; i < sizeof(Tags_Array) / sizeof(Tags_Array[0]); i++) { \ + if (!strcmp(str, Tags_Array[i].tag)) return Tags_Array[i].Name; \ + } \ + return Default; \ + } + + +/* parse the line cap used during stroking a path. + * Value: butt | round | square | inherit + * Initial: butt + * https://www.w3.org/TR/SVG/painting.html + */ +static constexpr struct +{ + StrokeCap lineCap; + const char* tag; +} lineCapTags[] = { + { StrokeCap::Butt, "butt" }, + { StrokeCap::Round, "round" }, + { StrokeCap::Square, "square" } +}; + + +_PARSE_TAG(StrokeCap, lineCap, LineCap, lineCapTags, StrokeCap::Butt) + + +/* parse the line join used during stroking a path. + * Value: miter | round | bevel | inherit + * Initial: miter + * https://www.w3.org/TR/SVG/painting.html + */ +static constexpr struct +{ + StrokeJoin lineJoin; + const char* tag; +} lineJoinTags[] = { + { StrokeJoin::Miter, "miter" }, + { StrokeJoin::Round, "round" }, + { StrokeJoin::Bevel, "bevel" } +}; + + +_PARSE_TAG(StrokeJoin, lineJoin, LineJoin, lineJoinTags, StrokeJoin::Miter) + + +/* parse the fill rule used during filling a path. + * Value: nonzero | evenodd | inherit + * Initial: nonzero + * https://www.w3.org/TR/SVG/painting.html + */ +static constexpr struct +{ + FillRule fillRule; + const char* tag; +} fillRuleTags[] = { + { FillRule::EvenOdd, "evenodd" } +}; + + +_PARSE_TAG(FillRule, fillRule, FillRule, fillRuleTags, FillRule::Winding) + + +/* parse the dash pattern used during stroking a path. + * Value: none | | inherit + * Initial: none + * https://www.w3.org/TR/SVG/painting.html + */ +static void _parseDashArray(SvgLoaderData* loader, const char *str, SvgDash* dash) +{ + if (!strncmp(str, "none", 4)) return; + + char *end = nullptr; + + while (*str) { + str = _skipComma(str); + float parsedValue = strToFloat(str, &end); + if (str == end) break; + if (parsedValue <= 0.0f) break; + if (*end == '%') { + ++end; + //Refers to the diagonal length of the viewport. + //https://www.w3.org/TR/SVG2/coords.html#Units + parsedValue = (sqrtf(powf(loader->svgParse->global.w, 2) + powf(loader->svgParse->global.h, 2)) / sqrtf(2.0f)) * (parsedValue / 100.0f); + } + (*dash).array.push(parsedValue); + str = end; + } + //If dash array size is 1, it means that dash and gap size are the same. + if ((*dash).array.count == 1) (*dash).array.push((*dash).array[0]); +} + + +static char* _idFromUrl(const char* url) +{ + auto open = strchr(url, '('); + auto close = strchr(url, ')'); + if (!open || !close || open >= close) return nullptr; + + open = strchr(url, '#'); + if (!open || open >= close) return nullptr; + + ++open; + --close; + + //trim the rest of the spaces if any + while (open < close && *close == ' ') --close; + + //quick verification + for (auto id = open; id < close; id++) { + if (*id == ' ' || *id == '\'') return nullptr; + } + + return strDuplicate(open, (close - open + 1)); +} + + +static unsigned char _parseColor(const char* value, char** end) +{ + float r; + + r = strToFloat(value, end); + *end = _skipSpace(*end, nullptr); + if (**end == '%') { + r = 255 * r / 100; + (*end)++; + } + *end = _skipSpace(*end, nullptr); + + if (r < 0 || r > 255) { + *end = nullptr; + return 0; + } + + return lrint(r); +} + + +static constexpr struct +{ + const char* name; + unsigned int value; +} colors[] = { + { "aliceblue", 0xfff0f8ff }, + { "antiquewhite", 0xfffaebd7 }, + { "aqua", 0xff00ffff }, + { "aquamarine", 0xff7fffd4 }, + { "azure", 0xfff0ffff }, + { "beige", 0xfff5f5dc }, + { "bisque", 0xffffe4c4 }, + { "black", 0xff000000 }, + { "blanchedalmond", 0xffffebcd }, + { "blue", 0xff0000ff }, + { "blueviolet", 0xff8a2be2 }, + { "brown", 0xffa52a2a }, + { "burlywood", 0xffdeb887 }, + { "cadetblue", 0xff5f9ea0 }, + { "chartreuse", 0xff7fff00 }, + { "chocolate", 0xffd2691e }, + { "coral", 0xffff7f50 }, + { "cornflowerblue", 0xff6495ed }, + { "cornsilk", 0xfffff8dc }, + { "crimson", 0xffdc143c }, + { "cyan", 0xff00ffff }, + { "darkblue", 0xff00008b }, + { "darkcyan", 0xff008b8b }, + { "darkgoldenrod", 0xffb8860b }, + { "darkgray", 0xffa9a9a9 }, + { "darkgrey", 0xffa9a9a9 }, + { "darkgreen", 0xff006400 }, + { "darkkhaki", 0xffbdb76b }, + { "darkmagenta", 0xff8b008b }, + { "darkolivegreen", 0xff556b2f }, + { "darkorange", 0xffff8c00 }, + { "darkorchid", 0xff9932cc }, + { "darkred", 0xff8b0000 }, + { "darksalmon", 0xffe9967a }, + { "darkseagreen", 0xff8fbc8f }, + { "darkslateblue", 0xff483d8b }, + { "darkslategray", 0xff2f4f4f }, + { "darkslategrey", 0xff2f4f4f }, + { "darkturquoise", 0xff00ced1 }, + { "darkviolet", 0xff9400d3 }, + { "deeppink", 0xffff1493 }, + { "deepskyblue", 0xff00bfff }, + { "dimgray", 0xff696969 }, + { "dimgrey", 0xff696969 }, + { "dodgerblue", 0xff1e90ff }, + { "firebrick", 0xffb22222 }, + { "floralwhite", 0xfffffaf0 }, + { "forestgreen", 0xff228b22 }, + { "fuchsia", 0xffff00ff }, + { "gainsboro", 0xffdcdcdc }, + { "ghostwhite", 0xfff8f8ff }, + { "gold", 0xffffd700 }, + { "goldenrod", 0xffdaa520 }, + { "gray", 0xff808080 }, + { "grey", 0xff808080 }, + { "green", 0xff008000 }, + { "greenyellow", 0xffadff2f }, + { "honeydew", 0xfff0fff0 }, + { "hotpink", 0xffff69b4 }, + { "indianred", 0xffcd5c5c }, + { "indigo", 0xff4b0082 }, + { "ivory", 0xfffffff0 }, + { "khaki", 0xfff0e68c }, + { "lavender", 0xffe6e6fa }, + { "lavenderblush", 0xfffff0f5 }, + { "lawngreen", 0xff7cfc00 }, + { "lemonchiffon", 0xfffffacd }, + { "lightblue", 0xffadd8e6 }, + { "lightcoral", 0xfff08080 }, + { "lightcyan", 0xffe0ffff }, + { "lightgoldenrodyellow", 0xfffafad2 }, + { "lightgray", 0xffd3d3d3 }, + { "lightgrey", 0xffd3d3d3 }, + { "lightgreen", 0xff90ee90 }, + { "lightpink", 0xffffb6c1 }, + { "lightsalmon", 0xffffa07a }, + { "lightseagreen", 0xff20b2aa }, + { "lightskyblue", 0xff87cefa }, + { "lightslategray", 0xff778899 }, + { "lightslategrey", 0xff778899 }, + { "lightsteelblue", 0xffb0c4de }, + { "lightyellow", 0xffffffe0 }, + { "lime", 0xff00ff00 }, + { "limegreen", 0xff32cd32 }, + { "linen", 0xfffaf0e6 }, + { "magenta", 0xffff00ff }, + { "maroon", 0xff800000 }, + { "mediumaquamarine", 0xff66cdaa }, + { "mediumblue", 0xff0000cd }, + { "mediumorchid", 0xffba55d3 }, + { "mediumpurple", 0xff9370d8 }, + { "mediumseagreen", 0xff3cb371 }, + { "mediumslateblue", 0xff7b68ee }, + { "mediumspringgreen", 0xff00fa9a }, + { "mediumturquoise", 0xff48d1cc }, + { "mediumvioletred", 0xffc71585 }, + { "midnightblue", 0xff191970 }, + { "mintcream", 0xfff5fffa }, + { "mistyrose", 0xffffe4e1 }, + { "moccasin", 0xffffe4b5 }, + { "navajowhite", 0xffffdead }, + { "navy", 0xff000080 }, + { "oldlace", 0xfffdf5e6 }, + { "olive", 0xff808000 }, + { "olivedrab", 0xff6b8e23 }, + { "orange", 0xffffa500 }, + { "orangered", 0xffff4500 }, + { "orchid", 0xffda70d6 }, + { "palegoldenrod", 0xffeee8aa }, + { "palegreen", 0xff98fb98 }, + { "paleturquoise", 0xffafeeee }, + { "palevioletred", 0xffd87093 }, + { "papayawhip", 0xffffefd5 }, + { "peachpuff", 0xffffdab9 }, + { "peru", 0xffcd853f }, + { "pink", 0xffffc0cb }, + { "plum", 0xffdda0dd }, + { "powderblue", 0xffb0e0e6 }, + { "purple", 0xff800080 }, + { "red", 0xffff0000 }, + { "rosybrown", 0xffbc8f8f }, + { "royalblue", 0xff4169e1 }, + { "saddlebrown", 0xff8b4513 }, + { "salmon", 0xfffa8072 }, + { "sandybrown", 0xfff4a460 }, + { "seagreen", 0xff2e8b57 }, + { "seashell", 0xfffff5ee }, + { "sienna", 0xffa0522d }, + { "silver", 0xffc0c0c0 }, + { "skyblue", 0xff87ceeb }, + { "slateblue", 0xff6a5acd }, + { "slategray", 0xff708090 }, + { "slategrey", 0xff708090 }, + { "snow", 0xfffffafa }, + { "springgreen", 0xff00ff7f }, + { "steelblue", 0xff4682b4 }, + { "tan", 0xffd2b48c }, + { "teal", 0xff008080 }, + { "thistle", 0xffd8bfd8 }, + { "tomato", 0xffff6347 }, + { "turquoise", 0xff40e0d0 }, + { "violet", 0xffee82ee }, + { "wheat", 0xfff5deb3 }, + { "white", 0xffffffff }, + { "whitesmoke", 0xfff5f5f5 }, + { "yellow", 0xffffff00 }, + { "yellowgreen", 0xff9acd32 } +}; + + +static bool _hslToRgb(float hue, float saturation, float brightness, uint8_t* red, uint8_t* green, uint8_t* blue) +{ + if (!red || !green || !blue) return false; + + float sv, vsf, f, p, q, t, v; + float _red = 0, _green = 0, _blue = 0; + uint32_t i = 0; + + if (mathZero(saturation)) _red = _green = _blue = brightness; + else { + if (mathEqual(hue, 360.0)) hue = 0.0f; + hue /= 60.0f; + + v = (brightness <= 0.5f) ? (brightness * (1.0f + saturation)) : (brightness + saturation - (brightness * saturation)); + p = brightness + brightness - v; + + if (!mathZero(v)) sv = (v - p) / v; + else sv = 0; + + i = static_cast(hue); + f = hue - i; + + vsf = v * sv * f; + + t = p + vsf; + q = v - vsf; + + switch (i) { + case 0: { + _red = v; + _green = t; + _blue = p; + break; + } + case 1: { + _red = q; + _green = v; + _blue = p; + break; + } + case 2: { + _red = p; + _green = v; + _blue = t; + break; + } + case 3: { + _red = p; + _green = q; + _blue = v; + break; + } + case 4: { + _red = t; + _green = p; + _blue = v; + break; + } + case 5: { + _red = v; + _green = p; + _blue = q; + break; + } + } + } + + *red = static_cast(roundf(_red * 255.0f)); + *green = static_cast(roundf(_green * 255.0f)); + *blue = static_cast(roundf(_blue * 255.0f)); + + return true; +} + + +static bool _toColor(const char* str, uint8_t* r, uint8_t* g, uint8_t* b, char** ref) +{ + unsigned int len = strlen(str); + char *red, *green, *blue; + unsigned char tr, tg, tb; + + if (len == 4 && str[0] == '#') { + //Case for "#456" should be interpreted as "#445566" + if (isxdigit(str[1]) && isxdigit(str[2]) && isxdigit(str[3])) { + char tmp[3] = { '\0', '\0', '\0' }; + tmp[0] = str[1]; + tmp[1] = str[1]; + *r = strtol(tmp, nullptr, 16); + tmp[0] = str[2]; + tmp[1] = str[2]; + *g = strtol(tmp, nullptr, 16); + tmp[0] = str[3]; + tmp[1] = str[3]; + *b = strtol(tmp, nullptr, 16); + } + return true; + } else if (len == 7 && str[0] == '#') { + if (isxdigit(str[1]) && isxdigit(str[2]) && isxdigit(str[3]) && isxdigit(str[4]) && isxdigit(str[5]) && isxdigit(str[6])) { + char tmp[3] = { '\0', '\0', '\0' }; + tmp[0] = str[1]; + tmp[1] = str[2]; + *r = strtol(tmp, nullptr, 16); + tmp[0] = str[3]; + tmp[1] = str[4]; + *g = strtol(tmp, nullptr, 16); + tmp[0] = str[5]; + tmp[1] = str[6]; + *b = strtol(tmp, nullptr, 16); + } + return true; + } else if (len >= 10 && (str[0] == 'r' || str[0] == 'R') && (str[1] == 'g' || str[1] == 'G') && (str[2] == 'b' || str[2] == 'B') && str[3] == '(' && str[len - 1] == ')') { + tr = _parseColor(str + 4, &red); + if (red && *red == ',') { + tg = _parseColor(red + 1, &green); + if (green && *green == ',') { + tb = _parseColor(green + 1, &blue); + if (blue && blue[0] == ')' && blue[1] == '\0') { + *r = tr; + *g = tg; + *b = tb; + } + } + } + return true; + } else if (ref && len >= 3 && !strncmp(str, "url", 3)) { + if (*ref) free(*ref); + *ref = _idFromUrl((const char*)(str + 3)); + return true; + } else if (len >= 10 && (str[0] == 'h' || str[0] == 'H') && (str[1] == 's' || str[1] == 'S') && (str[2] == 'l' || str[2] == 'L') && str[3] == '(' && str[len - 1] == ')') { + float th, ts, tb; + const char *content, *hue, *saturation, *brightness; + content = str + 4; + content = _skipSpace(content, nullptr); + if (_parseNumber(&content, &hue, &th) && hue) { + th = float(uint32_t(th) % 360); + hue = _skipSpace(hue, nullptr); + hue = (char*)_skipComma(hue); + hue = _skipSpace(hue, nullptr); + if (_parseNumber(&hue, &saturation, &ts) && saturation && *saturation == '%') { + ts /= 100.0f; + saturation = _skipSpace(saturation + 1, nullptr); + saturation = (char*)_skipComma(saturation); + saturation = _skipSpace(saturation, nullptr); + if (_parseNumber(&saturation, &brightness, &tb) && brightness && *brightness == '%') { + tb /= 100.0f; + brightness = _skipSpace(brightness + 1, nullptr); + if (brightness && brightness[0] == ')' && brightness[1] == '\0') { + return _hslToRgb(th, ts, tb, r, g, b); + } + } + } + } + } else { + //Handle named color + for (unsigned int i = 0; i < (sizeof(colors) / sizeof(colors[0])); i++) { + if (!strcasecmp(colors[i].name, str)) { + *r = (((uint8_t*)(&(colors[i].value)))[2]); + *g = (((uint8_t*)(&(colors[i].value)))[1]); + *b = (((uint8_t*)(&(colors[i].value)))[0]); + return true; + } + } + } + return false; +} + + +static char* _parseNumbersArray(char* str, float* points, int* ptCount, int len) +{ + int count = 0; + char* end = nullptr; + + str = _skipSpace(str, nullptr); + while ((count < len) && (isdigit(*str) || *str == '-' || *str == '+' || *str == '.')) { + points[count++] = strToFloat(str, &end); + str = end; + str = _skipSpace(str, nullptr); + if (*str == ',') ++str; + //Eat the rest of space + str = _skipSpace(str, nullptr); + } + *ptCount = count; + return str; +} + + +enum class MatrixState { + Unknown, + Matrix, + Translate, + Rotate, + Scale, + SkewX, + SkewY +}; + + +#define MATRIX_DEF(Name, Value) \ + { \ +#Name, sizeof(#Name), Value \ + } + + +static constexpr struct +{ + const char* tag; + int sz; + MatrixState state; +} matrixTags[] = { + MATRIX_DEF(matrix, MatrixState::Matrix), + MATRIX_DEF(translate, MatrixState::Translate), + MATRIX_DEF(rotate, MatrixState::Rotate), + MATRIX_DEF(scale, MatrixState::Scale), + MATRIX_DEF(skewX, MatrixState::SkewX), + MATRIX_DEF(skewY, MatrixState::SkewY) +}; + + +/* parse transform attribute + * https://www.w3.org/TR/SVG/coords.html#TransformAttribute + */ +static Matrix* _parseTransformationMatrix(const char* value) +{ + const int POINT_CNT = 8; + + auto matrix = (Matrix*)malloc(sizeof(Matrix)); + if (!matrix) return nullptr; + *matrix = {1, 0, 0, 0, 1, 0, 0, 0, 1}; + + float points[POINT_CNT]; + int ptCount = 0; + char* str = (char*)value; + char* end = str + strlen(str); + + while (str < end) { + auto state = MatrixState::Unknown; + + if (isspace(*str) || (*str == ',')) { + ++str; + continue; + } + for (unsigned int i = 0; i < sizeof(matrixTags) / sizeof(matrixTags[0]); i++) { + if (!strncmp(matrixTags[i].tag, str, matrixTags[i].sz - 1)) { + state = matrixTags[i].state; + str += (matrixTags[i].sz - 1); + break; + } + } + if (state == MatrixState::Unknown) goto error; + + str = _skipSpace(str, end); + if (*str != '(') goto error; + ++str; + str = _parseNumbersArray(str, points, &ptCount, POINT_CNT); + if (*str != ')') goto error; + ++str; + + if (state == MatrixState::Matrix) { + if (ptCount != 6) goto error; + Matrix tmp = {points[0], points[2], points[4], points[1], points[3], points[5], 0, 0, 1}; + *matrix = mathMultiply(matrix, &tmp); + } else if (state == MatrixState::Translate) { + if (ptCount == 1) { + Matrix tmp = {1, 0, points[0], 0, 1, 0, 0, 0, 1}; + *matrix = mathMultiply(matrix, &tmp); + } else if (ptCount == 2) { + Matrix tmp = {1, 0, points[0], 0, 1, points[1], 0, 0, 1}; + *matrix = mathMultiply(matrix, &tmp); + } else goto error; + } else if (state == MatrixState::Rotate) { + //Transform to signed. + points[0] = fmodf(points[0], 360.0f); + if (points[0] < 0) points[0] += 360.0f; + auto c = cosf(mathDeg2Rad(points[0])); + auto s = sinf(mathDeg2Rad(points[0])); + if (ptCount == 1) { + Matrix tmp = { c, -s, 0, s, c, 0, 0, 0, 1 }; + *matrix = mathMultiply(matrix, &tmp); + } else if (ptCount == 3) { + Matrix tmp = { 1, 0, points[1], 0, 1, points[2], 0, 0, 1 }; + *matrix = mathMultiply(matrix, &tmp); + tmp = { c, -s, 0, s, c, 0, 0, 0, 1 }; + *matrix = mathMultiply(matrix, &tmp); + tmp = { 1, 0, -points[1], 0, 1, -points[2], 0, 0, 1 }; + *matrix = mathMultiply(matrix, &tmp); + } else { + goto error; + } + } else if (state == MatrixState::Scale) { + if (ptCount < 1 || ptCount > 2) goto error; + auto sx = points[0]; + auto sy = sx; + if (ptCount == 2) sy = points[1]; + Matrix tmp = { sx, 0, 0, 0, sy, 0, 0, 0, 1 }; + *matrix = mathMultiply(matrix, &tmp); + } else if (state == MatrixState::SkewX) { + if (ptCount != 1) goto error; + auto deg = tanf(mathDeg2Rad(points[0])); + Matrix tmp = { 1, deg, 0, 0, 1, 0, 0, 0, 1 }; + *matrix = mathMultiply(matrix, &tmp); + } else if (state == MatrixState::SkewY) { + if (ptCount != 1) goto error; + auto deg = tanf(mathDeg2Rad(points[0])); + Matrix tmp = { 1, 0, 0, deg, 1, 0, 0, 0, 1 }; + *matrix = mathMultiply(matrix, &tmp); + } + } + return matrix; +error: + if (matrix) free(matrix); + return nullptr; +} + + +#define LENGTH_DEF(Name, Value) \ + { \ +#Name, sizeof(#Name), Value \ + } + + +static void _postpone(Array& nodes, SvgNode *node, char* id) +{ + nodes.push({node, id}); +} + + +/* +// TODO - remove? +static constexpr struct +{ + const char* tag; + int sz; + SvgLengthType type; +} lengthTags[] = { + LENGTH_DEF(%, SvgLengthType::Percent), + LENGTH_DEF(px, SvgLengthType::Px), + LENGTH_DEF(pc, SvgLengthType::Pc), + LENGTH_DEF(pt, SvgLengthType::Pt), + LENGTH_DEF(mm, SvgLengthType::Mm), + LENGTH_DEF(cm, SvgLengthType::Cm), + LENGTH_DEF(in, SvgLengthType::In) +}; + +static float _parseLength(const char* str, SvgLengthType* type) +{ + float value; + int sz = strlen(str); + + *type = SvgLengthType::Px; + for (unsigned int i = 0; i < sizeof(lengthTags) / sizeof(lengthTags[0]); i++) { + if (lengthTags[i].sz - 1 == sz && !strncmp(lengthTags[i].tag, str, sz)) *type = lengthTags[i].type; + } + value = svgUtilStrtof(str, nullptr); + return value; +} +*/ + +static bool _parseStyleAttr(void* data, const char* key, const char* value); +static bool _parseStyleAttr(void* data, const char* key, const char* value, bool style); + + +static bool _attrParseSvgNode(void* data, const char* key, const char* value) +{ + SvgLoaderData* loader = (SvgLoaderData*)data; + SvgNode* node = loader->svgParse->node; + SvgDocNode* doc = &(node->node.doc); + + if (!strcmp(key, "width")) { + doc->w = _toFloat(loader->svgParse, value, SvgParserLengthType::Horizontal); + if (strstr(value, "%") && !(doc->viewFlag & SvgViewFlag::Viewbox)) { + doc->viewFlag = (doc->viewFlag | SvgViewFlag::WidthInPercent); + } else { + doc->viewFlag = (doc->viewFlag | SvgViewFlag::Width); + } + } else if (!strcmp(key, "height")) { + doc->h = _toFloat(loader->svgParse, value, SvgParserLengthType::Vertical); + if (strstr(value, "%") && !(doc->viewFlag & SvgViewFlag::Viewbox)) { + doc->viewFlag = (doc->viewFlag | SvgViewFlag::HeightInPercent); + } else { + doc->viewFlag = (doc->viewFlag | SvgViewFlag::Height); + } + } else if (!strcmp(key, "viewBox")) { + if (_parseNumber(&value, nullptr, &doc->vx)) { + if (_parseNumber(&value, nullptr, &doc->vy)) { + if (_parseNumber(&value, nullptr, &doc->vw)) { + if (_parseNumber(&value, nullptr, &doc->vh)) { + doc->viewFlag = (doc->viewFlag | SvgViewFlag::Viewbox); + loader->svgParse->global.h = doc->vh; + } + loader->svgParse->global.w = doc->vw; + } + loader->svgParse->global.y = doc->vy; + } + loader->svgParse->global.x = doc->vx; + } + if ((doc->viewFlag & SvgViewFlag::Viewbox) && (doc->vw < 0.0f || doc->vh < 0.0f)) { + doc->viewFlag = (SvgViewFlag)((uint32_t)doc->viewFlag & ~(uint32_t)SvgViewFlag::Viewbox); + TVGLOG("SVG", "Negative values of the width and/or height - the attribute invalidated."); + } + if (!(doc->viewFlag & SvgViewFlag::Viewbox)) { + loader->svgParse->global.x = loader->svgParse->global.y = 0.0f; + loader->svgParse->global.w = loader->svgParse->global.h = 1.0f; + } + } else if (!strcmp(key, "preserveAspectRatio")) { + _parseAspectRatio(&value, &doc->align, &doc->meetOrSlice); + } else if (!strcmp(key, "style")) { + return simpleXmlParseW3CAttribute(value, strlen(value), _parseStyleAttr, loader); +#ifdef THORVG_LOG_ENABLED + } else if ((!strcmp(key, "x") || !strcmp(key, "y")) && fabsf(strToFloat(value, nullptr)) > FLOAT_EPSILON) { + TVGLOG("SVG", "Unsupported attributes used [Elements type: Svg][Attribute: %s][Value: %s]", key, value); +#endif + } else { + return _parseStyleAttr(loader, key, value, false); + } + return true; +} + + +//https://www.w3.org/TR/SVGTiny12/painting.html#SpecifyingPaint +static void _handlePaintAttr(SvgPaint* paint, const char* value) +{ + if (!strcmp(value, "none")) { + //No paint property + paint->none = true; + return; + } + if (!strcmp(value, "currentColor")) { + paint->curColor = true; + paint->none = false; + return; + } + if (_toColor(value, &paint->color.r, &paint->color.g, &paint->color.b, &paint->url)) paint->none = false; +} + + +static void _handleColorAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, const char* value) +{ + SvgStyleProperty* style = node->style; + if (_toColor(value, &style->color.r, &style->color.g, &style->color.b, nullptr)) { + style->curColorSet = true; + } +} + + +static void _handleFillAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, const char* value) +{ + SvgStyleProperty* style = node->style; + style->fill.flags = (style->fill.flags | SvgFillFlags::Paint); + _handlePaintAttr(&style->fill.paint, value); +} + + +static void _handleStrokeAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, const char* value) +{ + SvgStyleProperty* style = node->style; + style->stroke.flags = (style->stroke.flags | SvgStrokeFlags::Paint); + _handlePaintAttr(&style->stroke.paint, value); +} + + +static void _handleStrokeOpacityAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, const char* value) +{ + node->style->stroke.flags = (node->style->stroke.flags | SvgStrokeFlags::Opacity); + node->style->stroke.opacity = _toOpacity(value); +} + +static void _handleStrokeDashArrayAttr(SvgLoaderData* loader, SvgNode* node, const char* value) +{ + node->style->stroke.flags = (node->style->stroke.flags | SvgStrokeFlags::Dash); + _parseDashArray(loader, value, &node->style->stroke.dash); +} + +static void _handleStrokeDashOffsetAttr(SvgLoaderData* loader, SvgNode* node, const char* value) +{ + node->style->stroke.flags = (node->style->stroke.flags | SvgStrokeFlags::DashOffset); + node->style->stroke.dash.offset = _toFloat(loader->svgParse, value, SvgParserLengthType::Horizontal); +} + +static void _handleStrokeWidthAttr(SvgLoaderData* loader, SvgNode* node, const char* value) +{ + node->style->stroke.flags = (node->style->stroke.flags | SvgStrokeFlags::Width); + node->style->stroke.width = _toFloat(loader->svgParse, value, SvgParserLengthType::Horizontal); +} + + +static void _handleStrokeLineCapAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, const char* value) +{ + node->style->stroke.flags = (node->style->stroke.flags | SvgStrokeFlags::Cap); + node->style->stroke.cap = _toLineCap(value); +} + + +static void _handleStrokeLineJoinAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, const char* value) +{ + node->style->stroke.flags = (node->style->stroke.flags | SvgStrokeFlags::Join); + node->style->stroke.join = _toLineJoin(value); +} + +static void _handleStrokeMiterlimitAttr(SvgLoaderData* loader, SvgNode* node, const char* value) +{ + char* end = nullptr; + const float miterlimit = strToFloat(value, &end); + + // https://www.w3.org/TR/SVG2/painting.html#LineJoin + // - A negative value for stroke-miterlimit must be treated as an illegal value. + if (miterlimit < 0.0f) { + TVGERR("SVG", "A stroke-miterlimit change (%f <- %f) with a negative value is omitted.", + node->style->stroke.miterlimit, miterlimit); + return; + } + + node->style->stroke.flags = (node->style->stroke.flags | SvgStrokeFlags::Miterlimit); + node->style->stroke.miterlimit = miterlimit; +} + +static void _handleFillRuleAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, const char* value) +{ + node->style->fill.flags = (node->style->fill.flags | SvgFillFlags::FillRule); + node->style->fill.fillRule = _toFillRule(value); +} + + +static void _handleOpacityAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, const char* value) +{ + node->style->flags = (node->style->flags | SvgStyleFlags::Opacity); + node->style->opacity = _toOpacity(value); +} + + +static void _handleFillOpacityAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, const char* value) +{ + node->style->fill.flags = (node->style->fill.flags | SvgFillFlags::Opacity); + node->style->fill.opacity = _toOpacity(value); +} + + +static void _handleTransformAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, const char* value) +{ + node->transform = _parseTransformationMatrix(value); +} + + +static void _handleClipPathAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, const char* value) +{ + SvgStyleProperty* style = node->style; + int len = strlen(value); + if (len >= 3 && !strncmp(value, "url", 3)) { + if (style->clipPath.url) free(style->clipPath.url); + style->clipPath.url = _idFromUrl((const char*)(value + 3)); + } +} + + +static void _handleMaskAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, const char* value) +{ + SvgStyleProperty* style = node->style; + int len = strlen(value); + if (len >= 3 && !strncmp(value, "url", 3)) { + if (style->mask.url) free(style->mask.url); + style->mask.url = _idFromUrl((const char*)(value + 3)); + } +} + + +static void _handleMaskTypeAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, const char* value) +{ + node->node.mask.type = _toMaskType(value); +} + + +static void _handleDisplayAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, const char* value) +{ + //TODO : The display attribute can have various values as well as "none". + // The default is "inline" which means visible and "none" means invisible. + // Depending on the type of node, additional functionality may be required. + // refer to https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/display + node->style->flags = (node->style->flags | SvgStyleFlags::Display); + if (!strcmp(value, "none")) node->style->display = false; + else node->style->display = true; +} + + +static void _handlePaintOrderAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, const char* value) +{ + node->style->flags = (node->style->flags | SvgStyleFlags::PaintOrder); + node->style->paintOrder = _toPaintOrder(value); +} + + +static void _handleCssClassAttr(SvgLoaderData* loader, SvgNode* node, const char* value) +{ + auto cssClass = &node->style->cssClass; + + if (*cssClass && value) free(*cssClass); + *cssClass = _copyId(value); + + bool cssClassFound = false; + + //css styling: tag.name has higher priority than .name + if (auto cssNode = cssFindStyleNode(loader->cssStyle, *cssClass, node->type)) { + cssClassFound = true; + cssCopyStyleAttr(node, cssNode); + } + if (auto cssNode = cssFindStyleNode(loader->cssStyle, *cssClass)) { + cssClassFound = true; + cssCopyStyleAttr(node, cssNode); + } + + if (!cssClassFound) _postpone(loader->nodesToStyle, node, *cssClass); +} + + +typedef void (*styleMethod)(SvgLoaderData* loader, SvgNode* node, const char* value); + +#define STYLE_DEF(Name, Name1, Flag) { #Name, sizeof(#Name), _handle##Name1##Attr, Flag } + + +static constexpr struct +{ + const char* tag; + int sz; + styleMethod tagHandler; + SvgStyleFlags flag; +} styleTags[] = { + STYLE_DEF(color, Color, SvgStyleFlags::Color), + STYLE_DEF(fill, Fill, SvgStyleFlags::Fill), + STYLE_DEF(fill-rule, FillRule, SvgStyleFlags::FillRule), + STYLE_DEF(fill-opacity, FillOpacity, SvgStyleFlags::FillOpacity), + STYLE_DEF(opacity, Opacity, SvgStyleFlags::Opacity), + STYLE_DEF(stroke, Stroke, SvgStyleFlags::Stroke), + STYLE_DEF(stroke-width, StrokeWidth, SvgStyleFlags::StrokeWidth), + STYLE_DEF(stroke-linejoin, StrokeLineJoin, SvgStyleFlags::StrokeLineJoin), + STYLE_DEF(stroke-miterlimit, StrokeMiterlimit, SvgStyleFlags::StrokeMiterlimit), + STYLE_DEF(stroke-linecap, StrokeLineCap, SvgStyleFlags::StrokeLineCap), + STYLE_DEF(stroke-opacity, StrokeOpacity, SvgStyleFlags::StrokeOpacity), + STYLE_DEF(stroke-dasharray, StrokeDashArray, SvgStyleFlags::StrokeDashArray), + STYLE_DEF(stroke-dashoffset, StrokeDashOffset, SvgStyleFlags::StrokeDashOffset), + STYLE_DEF(transform, Transform, SvgStyleFlags::Transform), + STYLE_DEF(clip-path, ClipPath, SvgStyleFlags::ClipPath), + STYLE_DEF(mask, Mask, SvgStyleFlags::Mask), + STYLE_DEF(mask-type, MaskType, SvgStyleFlags::MaskType), + STYLE_DEF(display, Display, SvgStyleFlags::Display), + STYLE_DEF(paint-order, PaintOrder, SvgStyleFlags::PaintOrder) +}; + + +static bool _parseStyleAttr(void* data, const char* key, const char* value, bool style) +{ + SvgLoaderData* loader = (SvgLoaderData*)data; + SvgNode* node = loader->svgParse->node; + int sz; + if (!key || !value) return false; + + //Trim the white space + key = _skipSpace(key, nullptr); + value = _skipSpace(value, nullptr); + + sz = strlen(key); + for (unsigned int i = 0; i < sizeof(styleTags) / sizeof(styleTags[0]); i++) { + if (styleTags[i].sz - 1 == sz && !strncmp(styleTags[i].tag, key, sz)) { + bool importance = false; + if (auto ptr = strstr(value, "!important")) { + size_t size = ptr - value; + while (size > 0 && isspace(value[size - 1])) { + size--; + } + value = strDuplicate(value, size); + importance = true; + } + if (style) { + if (importance || !(node->style->flagsImportance & styleTags[i].flag)) { + styleTags[i].tagHandler(loader, node, value); + node->style->flags = (node->style->flags | styleTags[i].flag); + } + } else if (!(node->style->flags & styleTags[i].flag)) { + styleTags[i].tagHandler(loader, node, value); + } + if (importance) { + node->style->flagsImportance = (node->style->flags | styleTags[i].flag); + free(const_cast(value)); + } + return true; + } + } + + return false; +} + + +static bool _parseStyleAttr(void* data, const char* key, const char* value) +{ + return _parseStyleAttr(data, key, value, true); +} + + +/* parse g node + * https://www.w3.org/TR/SVG/struct.html#Groups + */ +static bool _attrParseGNode(void* data, const char* key, const char* value) +{ + SvgLoaderData* loader = (SvgLoaderData*)data; + SvgNode* node = loader->svgParse->node; + + if (!strcmp(key, "style")) { + return simpleXmlParseW3CAttribute(value, strlen(value), _parseStyleAttr, loader); + } else if (!strcmp(key, "transform")) { + node->transform = _parseTransformationMatrix(value); + } else if (!strcmp(key, "id")) { + if (node->id && value) free(node->id); + node->id = _copyId(value); + } else if (!strcmp(key, "class")) { + _handleCssClassAttr(loader, node, value); + } else if (!strcmp(key, "clip-path")) { + _handleClipPathAttr(loader, node, value); + } else if (!strcmp(key, "mask")) { + _handleMaskAttr(loader, node, value); + } else { + return _parseStyleAttr(loader, key, value, false); + } + return true; +} + + +/* parse clipPath node + * https://www.w3.org/TR/SVG/struct.html#Groups + */ +static bool _attrParseClipPathNode(void* data, const char* key, const char* value) +{ + SvgLoaderData* loader = (SvgLoaderData*)data; + SvgNode* node = loader->svgParse->node; + SvgClipNode* clip = &(node->node.clip); + + if (!strcmp(key, "style")) { + return simpleXmlParseW3CAttribute(value, strlen(value), _parseStyleAttr, loader); + } else if (!strcmp(key, "transform")) { + node->transform = _parseTransformationMatrix(value); + } else if (!strcmp(key, "id")) { + if (node->id && value) free(node->id); + node->id = _copyId(value); + } else if (!strcmp(key, "class")) { + _handleCssClassAttr(loader, node, value); + } else if (!strcmp(key, "clipPathUnits")) { + if (!strcmp(value, "objectBoundingBox")) clip->userSpace = false; + } else { + return _parseStyleAttr(loader, key, value, false); + } + return true; +} + + +static bool _attrParseMaskNode(void* data, const char* key, const char* value) +{ + SvgLoaderData* loader = (SvgLoaderData*)data; + SvgNode* node = loader->svgParse->node; + SvgMaskNode* mask = &(node->node.mask); + + if (!strcmp(key, "style")) { + return simpleXmlParseW3CAttribute(value, strlen(value), _parseStyleAttr, loader); + } else if (!strcmp(key, "transform")) { + node->transform = _parseTransformationMatrix(value); + } else if (!strcmp(key, "id")) { + if (node->id && value) free(node->id); + node->id = _copyId(value); + } else if (!strcmp(key, "class")) { + _handleCssClassAttr(loader, node, value); + } else if (!strcmp(key, "maskContentUnits")) { + if (!strcmp(value, "objectBoundingBox")) mask->userSpace = false; + } else if (!strcmp(key, "mask-type")) { + mask->type = _toMaskType(value); + } else { + return _parseStyleAttr(loader, key, value, false); + } + return true; +} + + +static bool _attrParseCssStyleNode(void* data, const char* key, const char* value) +{ + SvgLoaderData* loader = (SvgLoaderData*)data; + SvgNode* node = loader->svgParse->node; + + if (!strcmp(key, "id")) { + if (node->id && value) free(node->id); + node->id = _copyId(value); + } else { + return _parseStyleAttr(loader, key, value, false); + } + return true; +} + + +static bool _attrParseSymbolNode(void* data, const char* key, const char* value) +{ + SvgLoaderData* loader = (SvgLoaderData*)data; + SvgNode* node = loader->svgParse->node; + SvgSymbolNode* symbol = &(node->node.symbol); + + if (!strcmp(key, "viewBox")) { + if (!_parseNumber(&value, nullptr, &symbol->vx) || !_parseNumber(&value, nullptr, &symbol->vy)) return false; + if (!_parseNumber(&value, nullptr, &symbol->vw) || !_parseNumber(&value, nullptr, &symbol->vh)) return false; + symbol->hasViewBox = true; + } else if (!strcmp(key, "width")) { + symbol->w = _toFloat(loader->svgParse, value, SvgParserLengthType::Horizontal); + symbol->hasWidth = true; + } else if (!strcmp(key, "height")) { + symbol->h = _toFloat(loader->svgParse, value, SvgParserLengthType::Vertical); + symbol->hasHeight = true; + } else if (!strcmp(key, "preserveAspectRatio")) { + _parseAspectRatio(&value, &symbol->align, &symbol->meetOrSlice); + } else if (!strcmp(key, "overflow")) { + if (!strcmp(value, "visible")) symbol->overflowVisible = true; + } else { + return _attrParseGNode(data, key, value); + } + + return true; +} + + +static SvgNode* _createNode(SvgNode* parent, SvgNodeType type) +{ + SvgNode* node = (SvgNode*)calloc(1, sizeof(SvgNode)); + + if (!node) return nullptr; + + //Default fill property + node->style = (SvgStyleProperty*)calloc(1, sizeof(SvgStyleProperty)); + + if (!node->style) { + free(node); + return nullptr; + } + + //Update the default value of stroke and fill + //https://www.w3.org/TR/SVGTiny12/painting.html#SpecifyingPaint + node->style->fill.paint.none = false; + //Default fill opacity is 1 + node->style->fill.opacity = 255; + node->style->opacity = 255; + //Default current color is not set + node->style->fill.paint.curColor = false; + node->style->curColorSet = false; + //Default fill rule is nonzero + node->style->fill.fillRule = FillRule::Winding; + + //Default stroke is none + node->style->stroke.paint.none = true; + //Default stroke opacity is 1 + node->style->stroke.opacity = 255; + //Default stroke current color is not set + node->style->stroke.paint.curColor = false; + //Default stroke width is 1 + node->style->stroke.width = 1; + //Default line cap is butt + node->style->stroke.cap = StrokeCap::Butt; + //Default line join is miter + node->style->stroke.join = StrokeJoin::Miter; + node->style->stroke.miterlimit = 4.0f; + node->style->stroke.scale = 1.0; + + node->style->paintOrder = _toPaintOrder("fill stroke"); + + //Default display is true("inline"). + node->style->display = true; + + node->parent = parent; + node->type = type; + + if (parent) parent->child.push(node); + return node; +} + + +static SvgNode* _createDefsNode(TVG_UNUSED SvgLoaderData* loader, TVG_UNUSED SvgNode* parent, const char* buf, unsigned bufLength, TVG_UNUSED parseAttributes func) +{ + if (loader->def && loader->doc->node.doc.defs) return loader->def; + SvgNode* node = _createNode(nullptr, SvgNodeType::Defs); + + loader->def = node; + loader->doc->node.doc.defs = node; + return node; +} + + +static SvgNode* _createGNode(TVG_UNUSED SvgLoaderData* loader, SvgNode* parent, const char* buf, unsigned bufLength, parseAttributes func) +{ + loader->svgParse->node = _createNode(parent, SvgNodeType::G); + if (!loader->svgParse->node) return nullptr; + + func(buf, bufLength, _attrParseGNode, loader); + return loader->svgParse->node; +} + + +static SvgNode* _createSvgNode(SvgLoaderData* loader, SvgNode* parent, const char* buf, unsigned bufLength, parseAttributes func) +{ + loader->svgParse->node = _createNode(parent, SvgNodeType::Doc); + if (!loader->svgParse->node) return nullptr; + SvgDocNode* doc = &(loader->svgParse->node->node.doc); + + loader->svgParse->global.w = 1.0f; + loader->svgParse->global.h = 1.0f; + + doc->align = AspectRatioAlign::XMidYMid; + doc->meetOrSlice = AspectRatioMeetOrSlice::Meet; + doc->viewFlag = SvgViewFlag::None; + func(buf, bufLength, _attrParseSvgNode, loader); + + if (!(doc->viewFlag & SvgViewFlag::Viewbox)) { + if (doc->viewFlag & SvgViewFlag::Width) { + loader->svgParse->global.w = doc->w; + } + if (doc->viewFlag & SvgViewFlag::Height) { + loader->svgParse->global.h = doc->h; + } + } + return loader->svgParse->node; +} + + +static SvgNode* _createMaskNode(SvgLoaderData* loader, SvgNode* parent, TVG_UNUSED const char* buf, TVG_UNUSED unsigned bufLength, parseAttributes func) +{ + loader->svgParse->node = _createNode(parent, SvgNodeType::Mask); + if (!loader->svgParse->node) return nullptr; + + loader->svgParse->node->node.mask.userSpace = true; + loader->svgParse->node->node.mask.type = SvgMaskType::Luminance; + + func(buf, bufLength, _attrParseMaskNode, loader); + + return loader->svgParse->node; +} + + +static SvgNode* _createClipPathNode(SvgLoaderData* loader, SvgNode* parent, const char* buf, unsigned bufLength, parseAttributes func) +{ + loader->svgParse->node = _createNode(parent, SvgNodeType::ClipPath); + if (!loader->svgParse->node) return nullptr; + + loader->svgParse->node->style->display = false; + loader->svgParse->node->node.clip.userSpace = true; + + func(buf, bufLength, _attrParseClipPathNode, loader); + + return loader->svgParse->node; +} + + +static SvgNode* _createCssStyleNode(SvgLoaderData* loader, SvgNode* parent, const char* buf, unsigned bufLength, parseAttributes func) +{ + loader->svgParse->node = _createNode(parent, SvgNodeType::CssStyle); + if (!loader->svgParse->node) return nullptr; + + func(buf, bufLength, _attrParseCssStyleNode, loader); + + return loader->svgParse->node; +} + + +static SvgNode* _createSymbolNode(SvgLoaderData* loader, SvgNode* parent, const char* buf, unsigned bufLength, parseAttributes func) +{ + loader->svgParse->node = _createNode(parent, SvgNodeType::Symbol); + if (!loader->svgParse->node) return nullptr; + + loader->svgParse->node->node.symbol.align = AspectRatioAlign::XMidYMid; + loader->svgParse->node->node.symbol.meetOrSlice = AspectRatioMeetOrSlice::Meet; + loader->svgParse->node->node.symbol.overflowVisible = false; + + loader->svgParse->node->node.symbol.hasViewBox = false; + loader->svgParse->node->node.symbol.hasWidth = false; + loader->svgParse->node->node.symbol.hasHeight = false; + loader->svgParse->node->node.symbol.vx = 0.0f; + loader->svgParse->node->node.symbol.vy = 0.0f; + + func(buf, bufLength, _attrParseSymbolNode, loader); + + return loader->svgParse->node; +} + + +static bool _attrParsePathNode(void* data, const char* key, const char* value) +{ + SvgLoaderData* loader = (SvgLoaderData*)data; + SvgNode* node = loader->svgParse->node; + SvgPathNode* path = &(node->node.path); + + if (!strcmp(key, "d")) { + if (path->path) free(path->path); + //Temporary: need to copy + path->path = _copyId(value); + } else if (!strcmp(key, "style")) { + return simpleXmlParseW3CAttribute(value, strlen(value), _parseStyleAttr, loader); + } else if (!strcmp(key, "clip-path")) { + _handleClipPathAttr(loader, node, value); + } else if (!strcmp(key, "mask")) { + _handleMaskAttr(loader, node, value); + } else if (!strcmp(key, "id")) { + if (node->id && value) free(node->id); + node->id = _copyId(value); + } else if (!strcmp(key, "class")) { + _handleCssClassAttr(loader, node, value); + } else { + return _parseStyleAttr(loader, key, value, false); + } + return true; +} + + +static SvgNode* _createPathNode(SvgLoaderData* loader, SvgNode* parent, const char* buf, unsigned bufLength, parseAttributes func) +{ + loader->svgParse->node = _createNode(parent, SvgNodeType::Path); + + if (!loader->svgParse->node) return nullptr; + + func(buf, bufLength, _attrParsePathNode, loader); + + return loader->svgParse->node; +} + + +static constexpr struct +{ + const char* tag; + SvgParserLengthType type; + int sz; + size_t offset; +} circleTags[] = { + {"cx", SvgParserLengthType::Horizontal, sizeof("cx"), offsetof(SvgCircleNode, cx)}, + {"cy", SvgParserLengthType::Vertical, sizeof("cy"), offsetof(SvgCircleNode, cy)}, + {"r", SvgParserLengthType::Other, sizeof("r"), offsetof(SvgCircleNode, r)} +}; + + +/* parse the attributes for a circle element. + * https://www.w3.org/TR/SVG/shapes.html#CircleElement + */ +static bool _attrParseCircleNode(void* data, const char* key, const char* value) +{ + SvgLoaderData* loader = (SvgLoaderData*)data; + SvgNode* node = loader->svgParse->node; + SvgCircleNode* circle = &(node->node.circle); + unsigned char* array; + int sz = strlen(key); + + array = (unsigned char*)circle; + for (unsigned int i = 0; i < sizeof(circleTags) / sizeof(circleTags[0]); i++) { + if (circleTags[i].sz - 1 == sz && !strncmp(circleTags[i].tag, key, sz)) { + *((float*)(array + circleTags[i].offset)) = _toFloat(loader->svgParse, value, circleTags[i].type); + return true; + } + } + + if (!strcmp(key, "style")) { + return simpleXmlParseW3CAttribute(value, strlen(value), _parseStyleAttr, loader); + } else if (!strcmp(key, "clip-path")) { + _handleClipPathAttr(loader, node, value); + } else if (!strcmp(key, "mask")) { + _handleMaskAttr(loader, node, value); + } else if (!strcmp(key, "id")) { + if (node->id && value) free(node->id); + node->id = _copyId(value); + } else if (!strcmp(key, "class")) { + _handleCssClassAttr(loader, node, value); + } else { + return _parseStyleAttr(loader, key, value, false); + } + return true; +} + + +static SvgNode* _createCircleNode(SvgLoaderData* loader, SvgNode* parent, const char* buf, unsigned bufLength, parseAttributes func) +{ + loader->svgParse->node = _createNode(parent, SvgNodeType::Circle); + + if (!loader->svgParse->node) return nullptr; + + func(buf, bufLength, _attrParseCircleNode, loader); + return loader->svgParse->node; +} + + +static constexpr struct +{ + const char* tag; + SvgParserLengthType type; + int sz; + size_t offset; +} ellipseTags[] = { + {"cx", SvgParserLengthType::Horizontal, sizeof("cx"), offsetof(SvgEllipseNode, cx)}, + {"cy", SvgParserLengthType::Vertical, sizeof("cy"), offsetof(SvgEllipseNode, cy)}, + {"rx", SvgParserLengthType::Horizontal, sizeof("rx"), offsetof(SvgEllipseNode, rx)}, + {"ry", SvgParserLengthType::Vertical, sizeof("ry"), offsetof(SvgEllipseNode, ry)} +}; + + +/* parse the attributes for an ellipse element. + * https://www.w3.org/TR/SVG/shapes.html#EllipseElement + */ +static bool _attrParseEllipseNode(void* data, const char* key, const char* value) +{ + SvgLoaderData* loader = (SvgLoaderData*)data; + SvgNode* node = loader->svgParse->node; + SvgEllipseNode* ellipse = &(node->node.ellipse); + unsigned char* array; + int sz = strlen(key); + + array = (unsigned char*)ellipse; + for (unsigned int i = 0; i < sizeof(ellipseTags) / sizeof(ellipseTags[0]); i++) { + if (ellipseTags[i].sz - 1 == sz && !strncmp(ellipseTags[i].tag, key, sz)) { + *((float*)(array + ellipseTags[i].offset)) = _toFloat(loader->svgParse, value, ellipseTags[i].type); + return true; + } + } + + if (!strcmp(key, "id")) { + if (node->id && value) free(node->id); + node->id = _copyId(value); + } else if (!strcmp(key, "class")) { + _handleCssClassAttr(loader, node, value); + } else if (!strcmp(key, "style")) { + return simpleXmlParseW3CAttribute(value, strlen(value), _parseStyleAttr, loader); + } else if (!strcmp(key, "clip-path")) { + _handleClipPathAttr(loader, node, value); + } else if (!strcmp(key, "mask")) { + _handleMaskAttr(loader, node, value); + } else { + return _parseStyleAttr(loader, key, value, false); + } + return true; +} + + +static SvgNode* _createEllipseNode(SvgLoaderData* loader, SvgNode* parent, const char* buf, unsigned bufLength, parseAttributes func) +{ + loader->svgParse->node = _createNode(parent, SvgNodeType::Ellipse); + + if (!loader->svgParse->node) return nullptr; + + func(buf, bufLength, _attrParseEllipseNode, loader); + return loader->svgParse->node; +} + + +static bool _attrParsePolygonPoints(const char* str, SvgPolygonNode* polygon) +{ + float num_x, num_y; + while (_parseNumber(&str, nullptr, &num_x) && _parseNumber(&str, nullptr, &num_y)) { + polygon->pts.push(num_x); + polygon->pts.push(num_y); + } + return true; +} + + +/* parse the attributes for a polygon element. + * https://www.w3.org/TR/SVG/shapes.html#PolylineElement + */ +static bool _attrParsePolygonNode(void* data, const char* key, const char* value) +{ + SvgLoaderData* loader = (SvgLoaderData*)data; + SvgNode* node = loader->svgParse->node; + SvgPolygonNode* polygon = nullptr; + + if (node->type == SvgNodeType::Polygon) polygon = &(node->node.polygon); + else polygon = &(node->node.polyline); + + if (!strcmp(key, "points")) { + return _attrParsePolygonPoints(value, polygon); + } else if (!strcmp(key, "style")) { + return simpleXmlParseW3CAttribute(value, strlen(value), _parseStyleAttr, loader); + } else if (!strcmp(key, "clip-path")) { + _handleClipPathAttr(loader, node, value); + } else if (!strcmp(key, "mask")) { + _handleMaskAttr(loader, node, value); + } else if (!strcmp(key, "id")) { + if (node->id && value) free(node->id); + node->id = _copyId(value); + } else if (!strcmp(key, "class")) { + _handleCssClassAttr(loader, node, value); + } else { + return _parseStyleAttr(loader, key, value, false); + } + return true; +} + + +static SvgNode* _createPolygonNode(SvgLoaderData* loader, SvgNode* parent, const char* buf, unsigned bufLength, parseAttributes func) +{ + loader->svgParse->node = _createNode(parent, SvgNodeType::Polygon); + + if (!loader->svgParse->node) return nullptr; + + func(buf, bufLength, _attrParsePolygonNode, loader); + return loader->svgParse->node; +} + + +static SvgNode* _createPolylineNode(SvgLoaderData* loader, SvgNode* parent, const char* buf, unsigned bufLength, parseAttributes func) +{ + loader->svgParse->node = _createNode(parent, SvgNodeType::Polyline); + + if (!loader->svgParse->node) return nullptr; + + func(buf, bufLength, _attrParsePolygonNode, loader); + return loader->svgParse->node; +} + +static constexpr struct +{ + const char* tag; + SvgParserLengthType type; + int sz; + size_t offset; +} rectTags[] = { + {"x", SvgParserLengthType::Horizontal, sizeof("x"), offsetof(SvgRectNode, x)}, + {"y", SvgParserLengthType::Vertical, sizeof("y"), offsetof(SvgRectNode, y)}, + {"width", SvgParserLengthType::Horizontal, sizeof("width"), offsetof(SvgRectNode, w)}, + {"height", SvgParserLengthType::Vertical, sizeof("height"), offsetof(SvgRectNode, h)}, + {"rx", SvgParserLengthType::Horizontal, sizeof("rx"), offsetof(SvgRectNode, rx)}, + {"ry", SvgParserLengthType::Vertical, sizeof("ry"), offsetof(SvgRectNode, ry)} +}; + + +/* parse the attributes for a rect element. + * https://www.w3.org/TR/SVG/shapes.html#RectElement + */ +static bool _attrParseRectNode(void* data, const char* key, const char* value) +{ + SvgLoaderData* loader = (SvgLoaderData*)data; + SvgNode* node = loader->svgParse->node; + SvgRectNode* rect = &(node->node.rect); + unsigned char* array; + bool ret = true; + int sz = strlen(key); + + array = (unsigned char*)rect; + for (unsigned int i = 0; i < sizeof(rectTags) / sizeof(rectTags[0]); i++) { + if (rectTags[i].sz - 1 == sz && !strncmp(rectTags[i].tag, key, sz)) { + *((float*)(array + rectTags[i].offset)) = _toFloat(loader->svgParse, value, rectTags[i].type); + + //Case if only rx or ry is declared + if (!strncmp(rectTags[i].tag, "rx", sz)) rect->hasRx = true; + if (!strncmp(rectTags[i].tag, "ry", sz)) rect->hasRy = true; + + if ((rect->rx >= FLOAT_EPSILON) && (rect->ry < FLOAT_EPSILON) && rect->hasRx && !rect->hasRy) rect->ry = rect->rx; + if ((rect->ry >= FLOAT_EPSILON) && (rect->rx < FLOAT_EPSILON) && !rect->hasRx && rect->hasRy) rect->rx = rect->ry; + return ret; + } + } + + if (!strcmp(key, "id")) { + if (node->id && value) free(node->id); + node->id = _copyId(value); + } else if (!strcmp(key, "class")) { + _handleCssClassAttr(loader, node, value); + } else if (!strcmp(key, "style")) { + ret = simpleXmlParseW3CAttribute(value, strlen(value), _parseStyleAttr, loader); + } else if (!strcmp(key, "clip-path")) { + _handleClipPathAttr(loader, node, value); + } else if (!strcmp(key, "mask")) { + _handleMaskAttr(loader, node, value); + } else { + ret = _parseStyleAttr(loader, key, value, false); + } + + return ret; +} + + +static SvgNode* _createRectNode(SvgLoaderData* loader, SvgNode* parent, const char* buf, unsigned bufLength, parseAttributes func) +{ + loader->svgParse->node = _createNode(parent, SvgNodeType::Rect); + + if (!loader->svgParse->node) return nullptr; + + loader->svgParse->node->node.rect.hasRx = loader->svgParse->node->node.rect.hasRy = false; + + func(buf, bufLength, _attrParseRectNode, loader); + return loader->svgParse->node; +} + + +static constexpr struct +{ + const char* tag; + SvgParserLengthType type; + int sz; + size_t offset; +} lineTags[] = { + {"x1", SvgParserLengthType::Horizontal, sizeof("x1"), offsetof(SvgLineNode, x1)}, + {"y1", SvgParserLengthType::Vertical, sizeof("y1"), offsetof(SvgLineNode, y1)}, + {"x2", SvgParserLengthType::Horizontal, sizeof("x2"), offsetof(SvgLineNode, x2)}, + {"y2", SvgParserLengthType::Vertical, sizeof("y2"), offsetof(SvgLineNode, y2)} +}; + + +/* parse the attributes for a line element. + * https://www.w3.org/TR/SVG/shapes.html#LineElement + */ +static bool _attrParseLineNode(void* data, const char* key, const char* value) +{ + SvgLoaderData* loader = (SvgLoaderData*)data; + SvgNode* node = loader->svgParse->node; + SvgLineNode* line = &(node->node.line); + unsigned char* array; + int sz = strlen(key); + + array = (unsigned char*)line; + for (unsigned int i = 0; i < sizeof(lineTags) / sizeof(lineTags[0]); i++) { + if (lineTags[i].sz - 1 == sz && !strncmp(lineTags[i].tag, key, sz)) { + *((float*)(array + lineTags[i].offset)) = _toFloat(loader->svgParse, value, lineTags[i].type); + return true; + } + } + + if (!strcmp(key, "id")) { + if (node->id && value) free(node->id); + node->id = _copyId(value); + } else if (!strcmp(key, "class")) { + _handleCssClassAttr(loader, node, value); + } else if (!strcmp(key, "style")) { + return simpleXmlParseW3CAttribute(value, strlen(value), _parseStyleAttr, loader); + } else if (!strcmp(key, "clip-path")) { + _handleClipPathAttr(loader, node, value); + } else if (!strcmp(key, "mask")) { + _handleMaskAttr(loader, node, value); + } else { + return _parseStyleAttr(loader, key, value, false); + } + return true; +} + + +static SvgNode* _createLineNode(SvgLoaderData* loader, SvgNode* parent, const char* buf, unsigned bufLength, parseAttributes func) +{ + loader->svgParse->node = _createNode(parent, SvgNodeType::Line); + + if (!loader->svgParse->node) return nullptr; + + func(buf, bufLength, _attrParseLineNode, loader); + return loader->svgParse->node; +} + + +static char* _idFromHref(const char* href) +{ + href = _skipSpace(href, nullptr); + if ((*href) == '#') href++; + return strdup(href); +} + + +static constexpr struct +{ + const char* tag; + SvgParserLengthType type; + int sz; + size_t offset; +} imageTags[] = { + {"x", SvgParserLengthType::Horizontal, sizeof("x"), offsetof(SvgRectNode, x)}, + {"y", SvgParserLengthType::Vertical, sizeof("y"), offsetof(SvgRectNode, y)}, + {"width", SvgParserLengthType::Horizontal, sizeof("width"), offsetof(SvgRectNode, w)}, + {"height", SvgParserLengthType::Vertical, sizeof("height"), offsetof(SvgRectNode, h)}, +}; + + +/* parse the attributes for a image element. + * https://www.w3.org/TR/SVG/embedded.html#ImageElement + */ +static bool _attrParseImageNode(void* data, const char* key, const char* value) +{ + SvgLoaderData* loader = (SvgLoaderData*)data; + SvgNode* node = loader->svgParse->node; + SvgImageNode* image = &(node->node.image); + unsigned char* array; + int sz = strlen(key); + + array = (unsigned char*)image; + for (unsigned int i = 0; i < sizeof(imageTags) / sizeof(imageTags[0]); i++) { + if (imageTags[i].sz - 1 == sz && !strncmp(imageTags[i].tag, key, sz)) { + *((float*)(array + imageTags[i].offset)) = _toFloat(loader->svgParse, value, imageTags[i].type); + return true; + } + } + + if (!strcmp(key, "href") || !strcmp(key, "xlink:href")) { + if (image->href && value) free(image->href); + image->href = _idFromHref(value); + } else if (!strcmp(key, "id")) { + if (node->id && value) free(node->id); + node->id = _copyId(value); + } else if (!strcmp(key, "class")) { + _handleCssClassAttr(loader, node, value); + } else if (!strcmp(key, "style")) { + return simpleXmlParseW3CAttribute(value, strlen(value), _parseStyleAttr, loader); + } else if (!strcmp(key, "clip-path")) { + _handleClipPathAttr(loader, node, value); + } else if (!strcmp(key, "mask")) { + _handleMaskAttr(loader, node, value); + } else if (!strcmp(key, "transform")) { + node->transform = _parseTransformationMatrix(value); + } else { + return _parseStyleAttr(loader, key, value); + } + return true; +} + + +static SvgNode* _createImageNode(SvgLoaderData* loader, SvgNode* parent, const char* buf, unsigned bufLength, parseAttributes func) +{ + loader->svgParse->node = _createNode(parent, SvgNodeType::Image); + + if (!loader->svgParse->node) return nullptr; + + func(buf, bufLength, _attrParseImageNode, loader); + return loader->svgParse->node; +} + + +static SvgNode* _getDefsNode(SvgNode* node) +{ + if (!node) return nullptr; + + while (node->parent != nullptr) { + node = node->parent; + } + + if (node->type == SvgNodeType::Doc) return node->node.doc.defs; + if (node->type == SvgNodeType::Defs) return node; + + return nullptr; +} + + +static SvgNode* _findNodeById(SvgNode *node, const char* id) +{ + if (!node) return nullptr; + + SvgNode* result = nullptr; + if (node->id && !strcmp(node->id, id)) return node; + + if (node->child.count > 0) { + auto child = node->child.data; + for (uint32_t i = 0; i < node->child.count; ++i, ++child) { + result = _findNodeById(*child, id); + if (result) break; + } + } + return result; +} + + +static SvgNode* _findParentById(SvgNode* node, char* id, SvgNode* doc) +{ + SvgNode *parent = node->parent; + while (parent != nullptr && parent != doc) { + if (parent->id && !strcmp(parent->id, id)) { + return parent; + } + parent = parent->parent; + } + return nullptr; +} + + +static constexpr struct +{ + const char* tag; + SvgParserLengthType type; + int sz; + size_t offset; +} useTags[] = { + {"x", SvgParserLengthType::Horizontal, sizeof("x"), offsetof(SvgUseNode, x)}, + {"y", SvgParserLengthType::Vertical, sizeof("y"), offsetof(SvgUseNode, y)}, + {"width", SvgParserLengthType::Horizontal, sizeof("width"), offsetof(SvgUseNode, w)}, + {"height", SvgParserLengthType::Vertical, sizeof("height"), offsetof(SvgUseNode, h)} +}; + + +static void _cloneNode(SvgNode* from, SvgNode* parent, int depth); +static bool _attrParseUseNode(void* data, const char* key, const char* value) +{ + SvgLoaderData* loader = (SvgLoaderData*)data; + SvgNode *defs, *nodeFrom, *node = loader->svgParse->node; + char* id; + + SvgUseNode* use = &(node->node.use); + int sz = strlen(key); + unsigned char* array = (unsigned char*)use; + for (unsigned int i = 0; i < sizeof(useTags) / sizeof(useTags[0]); i++) { + if (useTags[i].sz - 1 == sz && !strncmp(useTags[i].tag, key, sz)) { + *((float*)(array + useTags[i].offset)) = _toFloat(loader->svgParse, value, useTags[i].type); + + if (useTags[i].offset == offsetof(SvgUseNode, w)) use->isWidthSet = true; + else if (useTags[i].offset == offsetof(SvgUseNode, h)) use->isHeightSet = true; + + return true; + } + } + + if (!strcmp(key, "href") || !strcmp(key, "xlink:href")) { + id = _idFromHref(value); + defs = _getDefsNode(node); + nodeFrom = _findNodeById(defs, id); + if (nodeFrom) { + if (!_findParentById(node, id, loader->doc)) { + _cloneNode(nodeFrom, node, 0); + if (nodeFrom->type == SvgNodeType::Symbol) use->symbol = nodeFrom; + } else { + TVGLOG("SVG", "%s is ancestor element. This reference is invalid.", id); + } + free(id); + } else { + //some svg export software include element at the end of the file + //if so the 'from' element won't be found now and we have to repeat finding + //after the whole file is parsed + _postpone(loader->cloneNodes, node, id); + } + } else { + return _attrParseGNode(data, key, value); + } + return true; +} + + +static SvgNode* _createUseNode(SvgLoaderData* loader, SvgNode* parent, const char* buf, unsigned bufLength, parseAttributes func) +{ + loader->svgParse->node = _createNode(parent, SvgNodeType::Use); + + if (!loader->svgParse->node) return nullptr; + + loader->svgParse->node->node.use.isWidthSet = false; + loader->svgParse->node->node.use.isHeightSet = false; + + func(buf, bufLength, _attrParseUseNode, loader); + return loader->svgParse->node; +} + + +//TODO: Implement 'text' primitive +static constexpr struct +{ + const char* tag; + int sz; + FactoryMethod tagHandler; +} graphicsTags[] = { + {"use", sizeof("use"), _createUseNode}, + {"circle", sizeof("circle"), _createCircleNode}, + {"ellipse", sizeof("ellipse"), _createEllipseNode}, + {"path", sizeof("path"), _createPathNode}, + {"polygon", sizeof("polygon"), _createPolygonNode}, + {"rect", sizeof("rect"), _createRectNode}, + {"polyline", sizeof("polyline"), _createPolylineNode}, + {"line", sizeof("line"), _createLineNode}, + {"image", sizeof("image"), _createImageNode} +}; + + +static constexpr struct +{ + const char* tag; + int sz; + FactoryMethod tagHandler; +} groupTags[] = { + {"defs", sizeof("defs"), _createDefsNode}, + {"g", sizeof("g"), _createGNode}, + {"svg", sizeof("svg"), _createSvgNode}, + {"mask", sizeof("mask"), _createMaskNode}, + {"clipPath", sizeof("clipPath"), _createClipPathNode}, + {"style", sizeof("style"), _createCssStyleNode}, + {"symbol", sizeof("symbol"), _createSymbolNode} +}; + + +#define FIND_FACTORY(Short_Name, Tags_Array) \ + static FactoryMethod \ + _find##Short_Name##Factory(const char* name) \ + { \ + unsigned int i; \ + int sz = strlen(name); \ + \ + for (i = 0; i < sizeof(Tags_Array) / sizeof(Tags_Array[0]); i++) { \ + if (Tags_Array[i].sz - 1 == sz && !strncmp(Tags_Array[i].tag, name, sz)) { \ + return Tags_Array[i].tagHandler; \ + } \ + } \ + return nullptr; \ + } + +FIND_FACTORY(Group, groupTags) +FIND_FACTORY(Graphics, graphicsTags) + + +FillSpread _parseSpreadValue(const char* value) +{ + auto spread = FillSpread::Pad; + + if (!strcmp(value, "reflect")) { + spread = FillSpread::Reflect; + } else if (!strcmp(value, "repeat")) { + spread = FillSpread::Repeat; + } + + return spread; +} + + +static void _handleRadialCxAttr(SvgLoaderData* loader, SvgRadialGradient* radial, const char* value) +{ + radial->cx = _gradientToFloat(loader->svgParse, value, radial->isCxPercentage); + if (!loader->svgParse->gradient.parsedFx) { + radial->fx = radial->cx; + radial->isFxPercentage = radial->isCxPercentage; + } +} + + +static void _handleRadialCyAttr(SvgLoaderData* loader, SvgRadialGradient* radial, const char* value) +{ + radial->cy = _gradientToFloat(loader->svgParse, value, radial->isCyPercentage); + if (!loader->svgParse->gradient.parsedFy) { + radial->fy = radial->cy; + radial->isFyPercentage = radial->isCyPercentage; + } +} + + +static void _handleRadialFxAttr(SvgLoaderData* loader, SvgRadialGradient* radial, const char* value) +{ + radial->fx = _gradientToFloat(loader->svgParse, value, radial->isFxPercentage); + loader->svgParse->gradient.parsedFx = true; +} + + +static void _handleRadialFyAttr(SvgLoaderData* loader, SvgRadialGradient* radial, const char* value) +{ + radial->fy = _gradientToFloat(loader->svgParse, value, radial->isFyPercentage); + loader->svgParse->gradient.parsedFy = true; +} + + +static void _handleRadialFrAttr(SvgLoaderData* loader, SvgRadialGradient* radial, const char* value) +{ + radial->fr = _gradientToFloat(loader->svgParse, value, radial->isFrPercentage); +} + + +static void _handleRadialRAttr(SvgLoaderData* loader, SvgRadialGradient* radial, const char* value) +{ + radial->r = _gradientToFloat(loader->svgParse, value, radial->isRPercentage); +} + + +static void _recalcRadialCxAttr(SvgLoaderData* loader, SvgRadialGradient* radial, bool userSpace) +{ + if (userSpace && !radial->isCxPercentage) radial->cx = radial->cx / loader->svgParse->global.w; +} + + +static void _recalcRadialCyAttr(SvgLoaderData* loader, SvgRadialGradient* radial, bool userSpace) +{ + if (userSpace && !radial->isCyPercentage) radial->cy = radial->cy / loader->svgParse->global.h; +} + + +static void _recalcRadialFxAttr(SvgLoaderData* loader, SvgRadialGradient* radial, bool userSpace) +{ + if (userSpace && !radial->isFxPercentage) radial->fx = radial->fx / loader->svgParse->global.w; +} + + +static void _recalcRadialFyAttr(SvgLoaderData* loader, SvgRadialGradient* radial, bool userSpace) +{ + if (userSpace && !radial->isFyPercentage) radial->fy = radial->fy / loader->svgParse->global.h; +} + + +static void _recalcRadialFrAttr(SvgLoaderData* loader, SvgRadialGradient* radial, bool userSpace) +{ + // scaling factor based on the Units paragraph from : https://www.w3.org/TR/2015/WD-SVG2-20150915/coords.html + if (userSpace && !radial->isFrPercentage) radial->fr = radial->fr / (sqrtf(powf(loader->svgParse->global.h, 2) + powf(loader->svgParse->global.w, 2)) / sqrtf(2.0)); +} + + +static void _recalcRadialRAttr(SvgLoaderData* loader, SvgRadialGradient* radial, bool userSpace) +{ + // scaling factor based on the Units paragraph from : https://www.w3.org/TR/2015/WD-SVG2-20150915/coords.html + if (userSpace && !radial->isRPercentage) radial->r = radial->r / (sqrtf(powf(loader->svgParse->global.h, 2) + powf(loader->svgParse->global.w, 2)) / sqrtf(2.0)); +} + + +static void _recalcInheritedRadialCxAttr(SvgLoaderData* loader, SvgRadialGradient* radial, bool userSpace) +{ + if (!radial->isCxPercentage) { + if (userSpace) radial->cx /= loader->svgParse->global.w; + else radial->cx *= loader->svgParse->global.w; + } +} + + +static void _recalcInheritedRadialCyAttr(SvgLoaderData* loader, SvgRadialGradient* radial, bool userSpace) +{ + if (!radial->isCyPercentage) { + if (userSpace) radial->cy /= loader->svgParse->global.h; + else radial->cy *= loader->svgParse->global.h; + } +} + + +static void _recalcInheritedRadialFxAttr(SvgLoaderData* loader, SvgRadialGradient* radial, bool userSpace) +{ + if (!radial->isFxPercentage) { + if (userSpace) radial->fx /= loader->svgParse->global.w; + else radial->fx *= loader->svgParse->global.w; + } +} + + +static void _recalcInheritedRadialFyAttr(SvgLoaderData* loader, SvgRadialGradient* radial, bool userSpace) +{ + if (!radial->isFyPercentage) { + if (userSpace) radial->fy /= loader->svgParse->global.h; + else radial->fy *= loader->svgParse->global.h; + } +} + + +static void _recalcInheritedRadialFrAttr(SvgLoaderData* loader, SvgRadialGradient* radial, bool userSpace) +{ + if (!radial->isFrPercentage) { + if (userSpace) radial->fr /= sqrtf(powf(loader->svgParse->global.h, 2) + powf(loader->svgParse->global.w, 2)) / sqrtf(2.0); + else radial->fr *= sqrtf(powf(loader->svgParse->global.h, 2) + powf(loader->svgParse->global.w, 2)) / sqrtf(2.0); + } +} + + +static void _recalcInheritedRadialRAttr(SvgLoaderData* loader, SvgRadialGradient* radial, bool userSpace) +{ + if (!radial->isRPercentage) { + if (userSpace) radial->r /= sqrtf(powf(loader->svgParse->global.h, 2) + powf(loader->svgParse->global.w, 2)) / sqrtf(2.0); + else radial->r *= sqrtf(powf(loader->svgParse->global.h, 2) + powf(loader->svgParse->global.w, 2)) / sqrtf(2.0); + } +} + + +static void _inheritRadialCxAttr(SvgStyleGradient* to, SvgStyleGradient* from) +{ + to->radial->cx = from->radial->cx; + to->radial->isCxPercentage = from->radial->isCxPercentage; + to->flags = (to->flags | SvgGradientFlags::Cx); +} + + +static void _inheritRadialCyAttr(SvgStyleGradient* to, SvgStyleGradient* from) +{ + to->radial->cy = from->radial->cy; + to->radial->isCyPercentage = from->radial->isCyPercentage; + to->flags = (to->flags | SvgGradientFlags::Cy); +} + + +static void _inheritRadialFxAttr(SvgStyleGradient* to, SvgStyleGradient* from) +{ + to->radial->fx = from->radial->fx; + to->radial->isFxPercentage = from->radial->isFxPercentage; + to->flags = (to->flags | SvgGradientFlags::Fx); +} + + +static void _inheritRadialFyAttr(SvgStyleGradient* to, SvgStyleGradient* from) +{ + to->radial->fy = from->radial->fy; + to->radial->isFyPercentage = from->radial->isFyPercentage; + to->flags = (to->flags | SvgGradientFlags::Fy); +} + + +static void _inheritRadialFrAttr(SvgStyleGradient* to, SvgStyleGradient* from) +{ + to->radial->fr = from->radial->fr; + to->radial->isFrPercentage = from->radial->isFrPercentage; + to->flags = (to->flags | SvgGradientFlags::Fr); +} + + +static void _inheritRadialRAttr(SvgStyleGradient* to, SvgStyleGradient* from) +{ + to->radial->r = from->radial->r; + to->radial->isRPercentage = from->radial->isRPercentage; + to->flags = (to->flags | SvgGradientFlags::R); +} + + +typedef void (*radialMethod)(SvgLoaderData* loader, SvgRadialGradient* radial, const char* value); +typedef void (*radialInheritMethod)(SvgStyleGradient* to, SvgStyleGradient* from); +typedef void (*radialMethodRecalc)(SvgLoaderData* loader, SvgRadialGradient* radial, bool userSpace); + + +#define RADIAL_DEF(Name, Name1, Flag) \ + { \ +#Name, sizeof(#Name), _handleRadial##Name1##Attr, _inheritRadial##Name1##Attr, _recalcRadial##Name1##Attr, _recalcInheritedRadial##Name1##Attr, Flag \ + } + + +static constexpr struct +{ + const char* tag; + int sz; + radialMethod tagHandler; + radialInheritMethod tagInheritHandler; + radialMethodRecalc tagRecalc; + radialMethodRecalc tagInheritedRecalc; + SvgGradientFlags flag; +} radialTags[] = { + RADIAL_DEF(cx, Cx, SvgGradientFlags::Cx), + RADIAL_DEF(cy, Cy, SvgGradientFlags::Cy), + RADIAL_DEF(fx, Fx, SvgGradientFlags::Fx), + RADIAL_DEF(fy, Fy, SvgGradientFlags::Fy), + RADIAL_DEF(r, R, SvgGradientFlags::R), + RADIAL_DEF(fr, Fr, SvgGradientFlags::Fr) +}; + + +static bool _attrParseRadialGradientNode(void* data, const char* key, const char* value) +{ + SvgLoaderData* loader = (SvgLoaderData*)data; + SvgStyleGradient* grad = loader->svgParse->styleGrad; + SvgRadialGradient* radial = grad->radial; + int sz = strlen(key); + + for (unsigned int i = 0; i < sizeof(radialTags) / sizeof(radialTags[0]); i++) { + if (radialTags[i].sz - 1 == sz && !strncmp(radialTags[i].tag, key, sz)) { + radialTags[i].tagHandler(loader, radial, value); + grad->flags = (grad->flags | radialTags[i].flag); + return true; + } + } + + if (!strcmp(key, "id")) { + if (grad->id && value) free(grad->id); + grad->id = _copyId(value); + } else if (!strcmp(key, "spreadMethod")) { + grad->spread = _parseSpreadValue(value); + grad->flags = (grad->flags | SvgGradientFlags::SpreadMethod); + } else if (!strcmp(key, "href") || !strcmp(key, "xlink:href")) { + if (grad->ref && value) free(grad->ref); + grad->ref = _idFromHref(value); + } else if (!strcmp(key, "gradientUnits")) { + if (!strcmp(value, "userSpaceOnUse")) grad->userSpace = true; + grad->flags = (grad->flags | SvgGradientFlags::GradientUnits); + } else if (!strcmp(key, "gradientTransform")) { + grad->transform = _parseTransformationMatrix(value); + } else { + return false; + } + + return true; +} + + +static SvgStyleGradient* _createRadialGradient(SvgLoaderData* loader, const char* buf, unsigned bufLength) +{ + auto grad = (SvgStyleGradient*)(calloc(1, sizeof(SvgStyleGradient))); + loader->svgParse->styleGrad = grad; + + grad->flags = SvgGradientFlags::None; + grad->type = SvgGradientType::Radial; + grad->userSpace = false; + grad->radial = (SvgRadialGradient*)calloc(1, sizeof(SvgRadialGradient)); + if (!grad->radial) { + grad->clear(); + free(grad); + return nullptr; + } + /** + * Default values of gradient transformed into global percentage + */ + grad->radial->cx = 0.5f; + grad->radial->cy = 0.5f; + grad->radial->fx = 0.5f; + grad->radial->fy = 0.5f; + grad->radial->r = 0.5f; + grad->radial->isCxPercentage = true; + grad->radial->isCyPercentage = true; + grad->radial->isFxPercentage = true; + grad->radial->isFyPercentage = true; + grad->radial->isRPercentage = true; + grad->radial->isFrPercentage = true; + + loader->svgParse->gradient.parsedFx = false; + loader->svgParse->gradient.parsedFy = false; + simpleXmlParseAttributes(buf, bufLength, + _attrParseRadialGradientNode, loader); + + for (unsigned int i = 0; i < sizeof(radialTags) / sizeof(radialTags[0]); i++) { + radialTags[i].tagRecalc(loader, grad->radial, grad->userSpace); + } + + return loader->svgParse->styleGrad; +} + + +static bool _attrParseStopsStyle(void* data, const char* key, const char* value) +{ + SvgLoaderData* loader = (SvgLoaderData*)data; + auto stop = &loader->svgParse->gradStop; + + if (!strcmp(key, "stop-opacity")) { + stop->a = _toOpacity(value); + loader->svgParse->flags = (loader->svgParse->flags | SvgStopStyleFlags::StopOpacity); + } else if (!strcmp(key, "stop-color")) { + if (_toColor(value, &stop->r, &stop->g, &stop->b, nullptr)) { + loader->svgParse->flags = (loader->svgParse->flags | SvgStopStyleFlags::StopColor); + } + } else { + return false; + } + + return true; +} + + +static bool _attrParseStops(void* data, const char* key, const char* value) +{ + SvgLoaderData* loader = (SvgLoaderData*)data; + auto stop = &loader->svgParse->gradStop; + + if (!strcmp(key, "offset")) { + stop->offset = _toOffset(value); + } else if (!strcmp(key, "stop-opacity")) { + if (!(loader->svgParse->flags & SvgStopStyleFlags::StopOpacity)) { + stop->a = _toOpacity(value); + } + } else if (!strcmp(key, "stop-color")) { + if (!(loader->svgParse->flags & SvgStopStyleFlags::StopColor)) { + _toColor(value, &stop->r, &stop->g, &stop->b, nullptr); + } + } else if (!strcmp(key, "style")) { + simpleXmlParseW3CAttribute(value, strlen(value), _attrParseStopsStyle, data); + } else { + return false; + } + + return true; +} + + +static void _handleLinearX1Attr(SvgLoaderData* loader, SvgLinearGradient* linear, const char* value) +{ + linear->x1 = _gradientToFloat(loader->svgParse, value, linear->isX1Percentage); +} + + +static void _handleLinearY1Attr(SvgLoaderData* loader, SvgLinearGradient* linear, const char* value) +{ + linear->y1 = _gradientToFloat(loader->svgParse, value, linear->isY1Percentage); +} + + +static void _handleLinearX2Attr(SvgLoaderData* loader, SvgLinearGradient* linear, const char* value) +{ + linear->x2 = _gradientToFloat(loader->svgParse, value, linear->isX2Percentage); +} + + +static void _handleLinearY2Attr(SvgLoaderData* loader, SvgLinearGradient* linear, const char* value) +{ + linear->y2 = _gradientToFloat(loader->svgParse, value, linear->isY2Percentage); +} + + +static void _recalcLinearX1Attr(SvgLoaderData* loader, SvgLinearGradient* linear, bool userSpace) +{ + if (userSpace && !linear->isX1Percentage) linear->x1 = linear->x1 / loader->svgParse->global.w; +} + + +static void _recalcLinearY1Attr(SvgLoaderData* loader, SvgLinearGradient* linear, bool userSpace) +{ + if (userSpace && !linear->isY1Percentage) linear->y1 = linear->y1 / loader->svgParse->global.h; +} + + +static void _recalcLinearX2Attr(SvgLoaderData* loader, SvgLinearGradient* linear, bool userSpace) +{ + if (userSpace && !linear->isX2Percentage) linear->x2 = linear->x2 / loader->svgParse->global.w; +} + + +static void _recalcLinearY2Attr(SvgLoaderData* loader, SvgLinearGradient* linear, bool userSpace) +{ + if (userSpace && !linear->isY2Percentage) linear->y2 = linear->y2 / loader->svgParse->global.h; +} + + +static void _recalcInheritedLinearX1Attr(SvgLoaderData* loader, SvgLinearGradient* linear, bool userSpace) +{ + if (!linear->isX1Percentage) { + if (userSpace) linear->x1 /= loader->svgParse->global.w; + else linear->x1 *= loader->svgParse->global.w; + } +} + + +static void _recalcInheritedLinearX2Attr(SvgLoaderData* loader, SvgLinearGradient* linear, bool userSpace) +{ + if (!linear->isX2Percentage) { + if (userSpace) linear->x2 /= loader->svgParse->global.w; + else linear->x2 *= loader->svgParse->global.w; + } +} + + +static void _recalcInheritedLinearY1Attr(SvgLoaderData* loader, SvgLinearGradient* linear, bool userSpace) +{ + if (!linear->isY1Percentage) { + if (userSpace) linear->y1 /= loader->svgParse->global.h; + else linear->y1 *= loader->svgParse->global.h; + } +} + + +static void _recalcInheritedLinearY2Attr(SvgLoaderData* loader, SvgLinearGradient* linear, bool userSpace) +{ + if (!linear->isY2Percentage) { + if (userSpace) linear->y2 /= loader->svgParse->global.h; + else linear->y2 *= loader->svgParse->global.h; + } +} + + +static void _inheritLinearX1Attr(SvgStyleGradient* to, SvgStyleGradient* from) +{ + to->linear->x1 = from->linear->x1; + to->linear->isX1Percentage = from->linear->isX1Percentage; + to->flags = (to->flags | SvgGradientFlags::X1); +} + + +static void _inheritLinearX2Attr(SvgStyleGradient* to, SvgStyleGradient* from) +{ + to->linear->x2 = from->linear->x2; + to->linear->isX2Percentage = from->linear->isX2Percentage; + to->flags = (to->flags | SvgGradientFlags::X2); +} + + +static void _inheritLinearY1Attr(SvgStyleGradient* to, SvgStyleGradient* from) +{ + to->linear->y1 = from->linear->y1; + to->linear->isY1Percentage = from->linear->isY1Percentage; + to->flags = (to->flags | SvgGradientFlags::Y1); +} + + +static void _inheritLinearY2Attr(SvgStyleGradient* to, SvgStyleGradient* from) +{ + to->linear->y2 = from->linear->y2; + to->linear->isY2Percentage = from->linear->isY2Percentage; + to->flags = (to->flags | SvgGradientFlags::Y2); +} + + +typedef void (*Linear_Method)(SvgLoaderData* loader, SvgLinearGradient* linear, const char* value); +typedef void (*Linear_Inherit_Method)(SvgStyleGradient* to, SvgStyleGradient* from); +typedef void (*Linear_Method_Recalc)(SvgLoaderData* loader, SvgLinearGradient* linear, bool userSpace); + + +#define LINEAR_DEF(Name, Name1, Flag) \ + { \ +#Name, sizeof(#Name), _handleLinear##Name1##Attr, _inheritLinear##Name1##Attr, _recalcLinear##Name1##Attr, _recalcInheritedLinear##Name1##Attr, Flag \ + } + + +static constexpr struct +{ + const char* tag; + int sz; + Linear_Method tagHandler; + Linear_Inherit_Method tagInheritHandler; + Linear_Method_Recalc tagRecalc; + Linear_Method_Recalc tagInheritedRecalc; + SvgGradientFlags flag; +} linear_tags[] = { + LINEAR_DEF(x1, X1, SvgGradientFlags::X1), + LINEAR_DEF(y1, Y1, SvgGradientFlags::Y1), + LINEAR_DEF(x2, X2, SvgGradientFlags::X2), + LINEAR_DEF(y2, Y2, SvgGradientFlags::Y2) +}; + + +static bool _attrParseLinearGradientNode(void* data, const char* key, const char* value) +{ + SvgLoaderData* loader = (SvgLoaderData*)data; + SvgStyleGradient* grad = loader->svgParse->styleGrad; + SvgLinearGradient* linear = grad->linear; + int sz = strlen(key); + + for (unsigned int i = 0; i < sizeof(linear_tags) / sizeof(linear_tags[0]); i++) { + if (linear_tags[i].sz - 1 == sz && !strncmp(linear_tags[i].tag, key, sz)) { + linear_tags[i].tagHandler(loader, linear, value); + grad->flags = (grad->flags | linear_tags[i].flag); + return true; + } + } + + if (!strcmp(key, "id")) { + if (grad->id && value) free(grad->id); + grad->id = _copyId(value); + } else if (!strcmp(key, "spreadMethod")) { + grad->spread = _parseSpreadValue(value); + grad->flags = (grad->flags | SvgGradientFlags::SpreadMethod); + } else if (!strcmp(key, "href") || !strcmp(key, "xlink:href")) { + if (grad->ref && value) free(grad->ref); + grad->ref = _idFromHref(value); + } else if (!strcmp(key, "gradientUnits")) { + if (!strcmp(value, "userSpaceOnUse")) grad->userSpace = true; + grad->flags = (grad->flags | SvgGradientFlags::GradientUnits); + } else if (!strcmp(key, "gradientTransform")) { + grad->transform = _parseTransformationMatrix(value); + } else { + return false; + } + + return true; +} + + +static SvgStyleGradient* _createLinearGradient(SvgLoaderData* loader, const char* buf, unsigned bufLength) +{ + auto grad = (SvgStyleGradient*)(calloc(1, sizeof(SvgStyleGradient))); + loader->svgParse->styleGrad = grad; + + grad->flags = SvgGradientFlags::None; + grad->type = SvgGradientType::Linear; + grad->userSpace = false; + grad->linear = (SvgLinearGradient*)calloc(1, sizeof(SvgLinearGradient)); + if (!grad->linear) { + grad->clear(); + free(grad); + return nullptr; + } + /** + * Default value of x2 is 100% - transformed to the global percentage + */ + grad->linear->x2 = 1.0f; + grad->linear->isX2Percentage = true; + + simpleXmlParseAttributes(buf, bufLength, _attrParseLinearGradientNode, loader); + + for (unsigned int i = 0; i < sizeof(linear_tags) / sizeof(linear_tags[0]); i++) { + linear_tags[i].tagRecalc(loader, grad->linear, grad->userSpace); + } + + return loader->svgParse->styleGrad; +} + + +#define GRADIENT_DEF(Name, Name1) \ + { \ +#Name, sizeof(#Name), _create##Name1 \ + } + + +/** + * In the case when the gradients lengths are given as numbers (not percentages) + * in the current user coordinate system, they are recalculated into percentages + * related to the canvas width and height. + */ +static constexpr struct +{ + const char* tag; + int sz; + GradientFactoryMethod tagHandler; +} gradientTags[] = { + GRADIENT_DEF(linearGradient, LinearGradient), + GRADIENT_DEF(radialGradient, RadialGradient) +}; + + +static GradientFactoryMethod _findGradientFactory(const char* name) +{ + int sz = strlen(name); + + for (unsigned int i = 0; i < sizeof(gradientTags) / sizeof(gradientTags[0]); i++) { + if (gradientTags[i].sz - 1 == sz && !strncmp(gradientTags[i].tag, name, sz)) { + return gradientTags[i].tagHandler; + } + } + return nullptr; +} + + +static void _cloneGradStops(Array& dst, const Array& src) +{ + for (uint32_t i = 0; i < src.count; ++i) { + dst.push(src[i]); + } +} + + +static void _inheritGradient(SvgLoaderData* loader, SvgStyleGradient* to, SvgStyleGradient* from) +{ + if (!to || !from) return; + + if (!(to->flags & SvgGradientFlags::SpreadMethod) && (from->flags & SvgGradientFlags::SpreadMethod)) { + to->spread = from->spread; + to->flags = (to->flags | SvgGradientFlags::SpreadMethod); + } + bool gradUnitSet = (to->flags & SvgGradientFlags::GradientUnits); + if (!(to->flags & SvgGradientFlags::GradientUnits) && (from->flags & SvgGradientFlags::GradientUnits)) { + to->userSpace = from->userSpace; + to->flags = (to->flags | SvgGradientFlags::GradientUnits); + } + + if (!to->transform && from->transform) { + to->transform = (Matrix*)malloc(sizeof(Matrix)); + if (to->transform) memcpy(to->transform, from->transform, sizeof(Matrix)); + } + + if (to->type == SvgGradientType::Linear) { + for (unsigned int i = 0; i < sizeof(linear_tags) / sizeof(linear_tags[0]); i++) { + bool coordSet = to->flags & linear_tags[i].flag; + if (!(to->flags & linear_tags[i].flag) && (from->flags & linear_tags[i].flag)) { + linear_tags[i].tagInheritHandler(to, from); + } + + //GradUnits not set directly, coord set + if (!gradUnitSet && coordSet) { + linear_tags[i].tagRecalc(loader, to->linear, to->userSpace); + } + //GradUnits set, coord not set directly + if (to->userSpace == from->userSpace) continue; + if (gradUnitSet && !coordSet) { + linear_tags[i].tagInheritedRecalc(loader, to->linear, to->userSpace); + } + } + } else if (to->type == SvgGradientType::Radial) { + for (unsigned int i = 0; i < sizeof(radialTags) / sizeof(radialTags[0]); i++) { + bool coordSet = (to->flags & radialTags[i].flag); + if (!(to->flags & radialTags[i].flag) && (from->flags & radialTags[i].flag)) { + radialTags[i].tagInheritHandler(to, from); + } + + //GradUnits not set directly, coord set + if (!gradUnitSet && coordSet) { + radialTags[i].tagRecalc(loader, to->radial, to->userSpace); + //If fx and fy are not set, set cx and cy. + if (!strcmp(radialTags[i].tag, "cx") && !(to->flags & SvgGradientFlags::Fx)) to->radial->fx = to->radial->cx; + if (!strcmp(radialTags[i].tag, "cy") && !(to->flags & SvgGradientFlags::Fy)) to->radial->fy = to->radial->cy; + } + //GradUnits set, coord not set directly + if (to->userSpace == from->userSpace) continue; + if (gradUnitSet && !coordSet) { + //If fx and fx are not set, do not call recalc. + if (!strcmp(radialTags[i].tag, "fx") && !(to->flags & SvgGradientFlags::Fx)) continue; + if (!strcmp(radialTags[i].tag, "fy") && !(to->flags & SvgGradientFlags::Fy)) continue; + radialTags[i].tagInheritedRecalc(loader, to->radial, to->userSpace); + } + } + } + + if (to->stops.empty()) _cloneGradStops(to->stops, from->stops); +} + + +static SvgStyleGradient* _cloneGradient(SvgStyleGradient* from) +{ + if (!from) return nullptr; + + auto grad = (SvgStyleGradient*)(calloc(1, sizeof(SvgStyleGradient))); + if (!grad) return nullptr; + + grad->type = from->type; + grad->id = from->id ? _copyId(from->id) : nullptr; + grad->ref = from->ref ? _copyId(from->ref) : nullptr; + grad->spread = from->spread; + grad->userSpace = from->userSpace; + grad->flags = from->flags; + + if (from->transform) { + grad->transform = (Matrix*)calloc(1, sizeof(Matrix)); + if (grad->transform) memcpy(grad->transform, from->transform, sizeof(Matrix)); + } + + if (grad->type == SvgGradientType::Linear) { + grad->linear = (SvgLinearGradient*)calloc(1, sizeof(SvgLinearGradient)); + if (!grad->linear) goto error_grad_alloc; + memcpy(grad->linear, from->linear, sizeof(SvgLinearGradient)); + } else if (grad->type == SvgGradientType::Radial) { + grad->radial = (SvgRadialGradient*)calloc(1, sizeof(SvgRadialGradient)); + if (!grad->radial) goto error_grad_alloc; + memcpy(grad->radial, from->radial, sizeof(SvgRadialGradient)); + } + + _cloneGradStops(grad->stops, from->stops); + + return grad; + + error_grad_alloc: + if (grad) { + grad->clear(); + free(grad); + } + return nullptr; +} + + +static void _styleInherit(SvgStyleProperty* child, const SvgStyleProperty* parent) +{ + if (parent == nullptr) return; + //Inherit the property of parent if not present in child. + if (!child->curColorSet) { + child->color = parent->color; + child->curColorSet = parent->curColorSet; + } + if (!(child->flags & SvgStyleFlags::PaintOrder)) { + child->paintOrder = parent->paintOrder; + } + //Fill + if (!(child->fill.flags & SvgFillFlags::Paint)) { + child->fill.paint.color = parent->fill.paint.color; + child->fill.paint.none = parent->fill.paint.none; + child->fill.paint.curColor = parent->fill.paint.curColor; + if (parent->fill.paint.url) { + if (child->fill.paint.url) free(child->fill.paint.url); + child->fill.paint.url = _copyId(parent->fill.paint.url); + } + } + if (!(child->fill.flags & SvgFillFlags::Opacity)) { + child->fill.opacity = parent->fill.opacity; + } + if (!(child->fill.flags & SvgFillFlags::FillRule)) { + child->fill.fillRule = parent->fill.fillRule; + } + //Stroke + if (!(child->stroke.flags & SvgStrokeFlags::Paint)) { + child->stroke.paint.color = parent->stroke.paint.color; + child->stroke.paint.none = parent->stroke.paint.none; + child->stroke.paint.curColor = parent->stroke.paint.curColor; + if (parent->stroke.paint.url) { + if (child->stroke.paint.url) free(child->stroke.paint.url); + child->stroke.paint.url = _copyId(parent->stroke.paint.url); + } + } + if (!(child->stroke.flags & SvgStrokeFlags::Opacity)) { + child->stroke.opacity = parent->stroke.opacity; + } + if (!(child->stroke.flags & SvgStrokeFlags::Width)) { + child->stroke.width = parent->stroke.width; + } + if (!(child->stroke.flags & SvgStrokeFlags::Dash)) { + if (parent->stroke.dash.array.count > 0) { + child->stroke.dash.array.clear(); + child->stroke.dash.array.reserve(parent->stroke.dash.array.count); + for (uint32_t i = 0; i < parent->stroke.dash.array.count; ++i) { + child->stroke.dash.array.push(parent->stroke.dash.array[i]); + } + } + } + if (!(child->stroke.flags & SvgStrokeFlags::DashOffset)) { + child->stroke.dash.offset = parent->stroke.dash.offset; + } + if (!(child->stroke.flags & SvgStrokeFlags::Cap)) { + child->stroke.cap = parent->stroke.cap; + } + if (!(child->stroke.flags & SvgStrokeFlags::Join)) { + child->stroke.join = parent->stroke.join; + } + if (!(child->stroke.flags & SvgStrokeFlags::Miterlimit)) { + child->stroke.miterlimit = parent->stroke.miterlimit; + } +} + + +static void _styleCopy(SvgStyleProperty* to, const SvgStyleProperty* from) +{ + if (from == nullptr) return; + //Copy the properties of 'from' only if they were explicitly set (not the default ones). + if (from->curColorSet) { + to->color = from->color; + to->curColorSet = true; + } + if (from->flags & SvgStyleFlags::Opacity) { + to->opacity = from->opacity; + } + if (from->flags & SvgStyleFlags::PaintOrder) { + to->paintOrder = from->paintOrder; + } + if (from->flags & SvgStyleFlags::Display) { + to->display = from->display; + } + //Fill + to->fill.flags = (to->fill.flags | from->fill.flags); + if (from->fill.flags & SvgFillFlags::Paint) { + to->fill.paint.color = from->fill.paint.color; + to->fill.paint.none = from->fill.paint.none; + to->fill.paint.curColor = from->fill.paint.curColor; + if (from->fill.paint.url) { + if (to->fill.paint.url) free(to->fill.paint.url); + to->fill.paint.url = _copyId(from->fill.paint.url); + } + } + if (from->fill.flags & SvgFillFlags::Opacity) { + to->fill.opacity = from->fill.opacity; + } + if (from->fill.flags & SvgFillFlags::FillRule) { + to->fill.fillRule = from->fill.fillRule; + } + //Stroke + to->stroke.flags = (to->stroke.flags | from->stroke.flags); + if (from->stroke.flags & SvgStrokeFlags::Paint) { + to->stroke.paint.color = from->stroke.paint.color; + to->stroke.paint.none = from->stroke.paint.none; + to->stroke.paint.curColor = from->stroke.paint.curColor; + if (from->stroke.paint.url) { + if (to->stroke.paint.url) free(to->stroke.paint.url); + to->stroke.paint.url = _copyId(from->stroke.paint.url); + } + } + if (from->stroke.flags & SvgStrokeFlags::Opacity) { + to->stroke.opacity = from->stroke.opacity; + } + if (from->stroke.flags & SvgStrokeFlags::Width) { + to->stroke.width = from->stroke.width; + } + if (from->stroke.flags & SvgStrokeFlags::Dash) { + if (from->stroke.dash.array.count > 0) { + to->stroke.dash.array.clear(); + to->stroke.dash.array.reserve(from->stroke.dash.array.count); + for (uint32_t i = 0; i < from->stroke.dash.array.count; ++i) { + to->stroke.dash.array.push(from->stroke.dash.array[i]); + } + } + } + if (from->stroke.flags & SvgStrokeFlags::DashOffset) { + to->stroke.dash.offset = from->stroke.dash.offset; + } + if (from->stroke.flags & SvgStrokeFlags::Cap) { + to->stroke.cap = from->stroke.cap; + } + if (from->stroke.flags & SvgStrokeFlags::Join) { + to->stroke.join = from->stroke.join; + } + if (from->stroke.flags & SvgStrokeFlags::Miterlimit) { + to->stroke.miterlimit = from->stroke.miterlimit; + } +} + + +static void _copyAttr(SvgNode* to, const SvgNode* from) +{ + //Copy matrix attribute + if (from->transform) { + to->transform = (Matrix*)malloc(sizeof(Matrix)); + if (to->transform) *to->transform = *from->transform; + } + //Copy style attribute + _styleCopy(to->style, from->style); + to->style->flags = (to->style->flags | from->style->flags); + if (from->style->clipPath.url) { + if (to->style->clipPath.url) free(to->style->clipPath.url); + to->style->clipPath.url = strdup(from->style->clipPath.url); + } + if (from->style->mask.url) { + if (to->style->mask.url) free(to->style->mask.url); + to->style->mask.url = strdup(from->style->mask.url); + } + + //Copy node attribute + switch (from->type) { + case SvgNodeType::Circle: { + to->node.circle.cx = from->node.circle.cx; + to->node.circle.cy = from->node.circle.cy; + to->node.circle.r = from->node.circle.r; + break; + } + case SvgNodeType::Ellipse: { + to->node.ellipse.cx = from->node.ellipse.cx; + to->node.ellipse.cy = from->node.ellipse.cy; + to->node.ellipse.rx = from->node.ellipse.rx; + to->node.ellipse.ry = from->node.ellipse.ry; + break; + } + case SvgNodeType::Rect: { + to->node.rect.x = from->node.rect.x; + to->node.rect.y = from->node.rect.y; + to->node.rect.w = from->node.rect.w; + to->node.rect.h = from->node.rect.h; + to->node.rect.rx = from->node.rect.rx; + to->node.rect.ry = from->node.rect.ry; + to->node.rect.hasRx = from->node.rect.hasRx; + to->node.rect.hasRy = from->node.rect.hasRy; + break; + } + case SvgNodeType::Line: { + to->node.line.x1 = from->node.line.x1; + to->node.line.y1 = from->node.line.y1; + to->node.line.x2 = from->node.line.x2; + to->node.line.y2 = from->node.line.y2; + break; + } + case SvgNodeType::Path: { + if (from->node.path.path) { + if (to->node.path.path) free(to->node.path.path); + to->node.path.path = strdup(from->node.path.path); + } + break; + } + case SvgNodeType::Polygon: { + if ((to->node.polygon.pts.count = from->node.polygon.pts.count)) { + to->node.polygon.pts = from->node.polygon.pts; + } + break; + } + case SvgNodeType::Polyline: { + if ((to->node.polyline.pts.count = from->node.polyline.pts.count)) { + to->node.polyline.pts = from->node.polyline.pts; + } + break; + } + case SvgNodeType::Image: { + to->node.image.x = from->node.image.x; + to->node.image.y = from->node.image.y; + to->node.image.w = from->node.image.w; + to->node.image.h = from->node.image.h; + if (from->node.image.href) { + if (to->node.image.href) free(to->node.image.href); + to->node.image.href = strdup(from->node.image.href); + } + break; + } + case SvgNodeType::Use: { + to->node.use.x = from->node.use.x; + to->node.use.y = from->node.use.y; + to->node.use.w = from->node.use.w; + to->node.use.h = from->node.use.h; + to->node.use.isWidthSet = from->node.use.isWidthSet; + to->node.use.isHeightSet = from->node.use.isHeightSet; + to->node.use.symbol = from->node.use.symbol; + break; + } + default: { + break; + } + } +} + + +static void _cloneNode(SvgNode* from, SvgNode* parent, int depth) +{ + /* Exception handling: Prevent invalid SVG data input. + The size is the arbitrary value, we need an experimental size. */ + if (depth == 8192) { + TVGERR("SVG", "Infinite recursive call - stopped after %d calls! Svg file may be incorrectly formatted.", depth); + return; + } + + SvgNode* newNode; + if (!from || !parent || from == parent) return; + + newNode = _createNode(parent, from->type); + if (!newNode) return; + + _styleInherit(newNode->style, parent->style); + _copyAttr(newNode, from); + + auto child = from->child.data; + for (uint32_t i = 0; i < from->child.count; ++i, ++child) { + _cloneNode(*child, newNode, depth + 1); + } +} + + +static void _clonePostponedNodes(Array* cloneNodes, SvgNode* doc) +{ + for (uint32_t i = 0; i < cloneNodes->count; ++i) { + auto nodeIdPair = (*cloneNodes)[i]; + auto defs = _getDefsNode(nodeIdPair.node); + auto nodeFrom = _findNodeById(defs, nodeIdPair.id); + if (!nodeFrom) nodeFrom = _findNodeById(doc, nodeIdPair.id); + if (!_findParentById(nodeIdPair.node, nodeIdPair.id, doc)) { + _cloneNode(nodeFrom, nodeIdPair.node, 0); + if (nodeFrom && nodeFrom->type == SvgNodeType::Symbol && nodeIdPair.node->type == SvgNodeType::Use) { + nodeIdPair.node->node.use.symbol = nodeFrom; + } + } else { + TVGLOG("SVG", "%s is ancestor element. This reference is invalid.", nodeIdPair.id); + } + free(nodeIdPair.id); + } +} + + +static constexpr struct +{ + const char* tag; + size_t sz; +} popArray[] = { + {"g", sizeof("g")}, + {"svg", sizeof("svg")}, + {"defs", sizeof("defs")}, + {"mask", sizeof("mask")}, + {"clipPath", sizeof("clipPath")}, + {"style", sizeof("style")}, + {"symbol", sizeof("symbol")} +}; + + +static void _svgLoaderParserXmlClose(SvgLoaderData* loader, const char* content) +{ + content = _skipSpace(content, nullptr); + + for (unsigned int i = 0; i < sizeof(popArray) / sizeof(popArray[0]); i++) { + if (!strncmp(content, popArray[i].tag, popArray[i].sz - 1)) { + loader->stack.pop(); + break; + } + } + + for (unsigned int i = 0; i < sizeof(graphicsTags) / sizeof(graphicsTags[0]); i++) { + if (!strncmp(content, graphicsTags[i].tag, graphicsTags[i].sz - 1)) { + loader->currentGraphicsNode = nullptr; + loader->stack.pop(); + break; + } + } + + loader->level--; +} + + +static void _svgLoaderParserXmlOpen(SvgLoaderData* loader, const char* content, unsigned int length, bool empty) +{ + const char* attrs = nullptr; + int attrsLength = 0; + int sz = length; + char tagName[20] = ""; + FactoryMethod method; + GradientFactoryMethod gradientMethod; + SvgNode *node = nullptr, *parent = nullptr; + loader->level++; + attrs = simpleXmlFindAttributesTag(content, length); + + if (!attrs) { + //Parse the empty tag + attrs = content; + while ((attrs != nullptr) && *attrs != '>') attrs++; + if (empty) attrs--; + } + + if (attrs) { + //Find out the tag name starting from content till sz length + sz = attrs - content; + while ((sz > 0) && (isspace(content[sz - 1]))) sz--; + if ((unsigned)sz >= sizeof(tagName)) return; + strncpy(tagName, content, sz); + tagName[sz] = '\0'; + attrsLength = length - sz; + } + + if ((method = _findGroupFactory(tagName))) { + //Group + if (empty) return; + if (!loader->doc) { + if (strcmp(tagName, "svg")) return; //Not a valid svg document + node = method(loader, nullptr, attrs, attrsLength, simpleXmlParseAttributes); + loader->doc = node; + } else { + if (!strcmp(tagName, "svg")) return; //Already loaded (SvgNodeType::Doc) tag + if (loader->stack.count > 0) parent = loader->stack.last(); + else parent = loader->doc; + if (!strcmp(tagName, "style")) { + // TODO: For now only the first style node is saved. After the css id selector + // is introduced this if condition shouldn't be necessary any more + if (!loader->cssStyle) { + node = method(loader, nullptr, attrs, attrsLength, simpleXmlParseAttributes); + loader->cssStyle = node; + loader->doc->node.doc.style = node; + loader->style = true; + } + } else { + node = method(loader, parent, attrs, attrsLength, simpleXmlParseAttributes); + } + } + + if (!node) return; + if (node->type != SvgNodeType::Defs || !empty) { + loader->stack.push(node); + } + } else if ((method = _findGraphicsFactory(tagName))) { + if (loader->stack.count > 0) parent = loader->stack.last(); + else parent = loader->doc; + node = method(loader, parent, attrs, attrsLength, simpleXmlParseAttributes); + if (node && !empty) { + auto defs = _createDefsNode(loader, nullptr, nullptr, 0, nullptr); + loader->stack.push(defs); + loader->currentGraphicsNode = node; + } + } else if ((gradientMethod = _findGradientFactory(tagName))) { + SvgStyleGradient* gradient; + gradient = gradientMethod(loader, attrs, attrsLength); + //FIXME: The current parsing structure does not distinguish end tags. + // There is no way to know if the currently parsed gradient is in defs. + // If a gradient is declared outside of defs after defs is set, it is included in the gradients of defs. + // But finally, the loader has a gradient style list regardless of defs. + // This is only to support this when multiple gradients are declared, even if no defs are declared. + // refer to: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/defs + if (loader->def && loader->doc->node.doc.defs) { + loader->def->node.defs.gradients.push(gradient); + } else { + loader->gradients.push(gradient); + } + loader->latestGradient = gradient; + } else if (!strcmp(tagName, "stop")) { + if (!loader->latestGradient) { + TVGLOG("SVG", "Stop element is used outside of the Gradient element"); + return; + } + /* default value for opacity */ + loader->svgParse->gradStop = {0.0f, 0, 0, 0, 255}; + loader->svgParse->flags = SvgStopStyleFlags::StopDefault; + simpleXmlParseAttributes(attrs, attrsLength, _attrParseStops, loader); + loader->latestGradient->stops.push(loader->svgParse->gradStop); + } else if (!isIgnoreUnsupportedLogElements(tagName)) { + TVGLOG("SVG", "Unsupported elements used [Elements: %s]", tagName); + } +} + + +static void _svgLoaderParserXmlCssStyle(SvgLoaderData* loader, const char* content, unsigned int length) +{ + char* tag; + char* name; + const char* attrs = nullptr; + unsigned int attrsLength = 0; + + FactoryMethod method; + GradientFactoryMethod gradientMethod; + SvgNode *node = nullptr; + + while (auto next = simpleXmlParseCSSAttribute(content, length, &tag, &name, &attrs, &attrsLength)) { + if ((method = _findGroupFactory(tag))) { + if ((node = method(loader, loader->cssStyle, attrs, attrsLength, simpleXmlParseW3CAttribute))) node->id = _copyId(name); + } else if ((method = _findGraphicsFactory(tag))) { + if ((node = method(loader, loader->cssStyle, attrs, attrsLength, simpleXmlParseW3CAttribute))) node->id = _copyId(name); + } else if ((gradientMethod = _findGradientFactory(tag))) { + TVGLOG("SVG", "Unsupported elements used in the internal CSS style sheets [Elements: %s]", tag); + } else if (!strcmp(tag, "stop")) { + TVGLOG("SVG", "Unsupported elements used in the internal CSS style sheets [Elements: %s]", tag); + } else if (!strcmp(tag, "all")) { + if ((node = _createCssStyleNode(loader, loader->cssStyle, attrs, attrsLength, simpleXmlParseW3CAttribute))) node->id = _copyId(name); + } else if (!isIgnoreUnsupportedLogElements(tag)) { + TVGLOG("SVG", "Unsupported elements used in the internal CSS style sheets [Elements: %s]", tag); + } + + length -= next - content; + content = next; + + free(tag); + free(name); + } + loader->style = false; +} + + +static bool _svgLoaderParser(void* data, SimpleXMLType type, const char* content, unsigned int length) +{ + SvgLoaderData* loader = (SvgLoaderData*)data; + + switch (type) { + case SimpleXMLType::Open: { + _svgLoaderParserXmlOpen(loader, content, length, false); + break; + } + case SimpleXMLType::OpenEmpty: { + _svgLoaderParserXmlOpen(loader, content, length, true); + break; + } + case SimpleXMLType::Close: { + _svgLoaderParserXmlClose(loader, content); + break; + } + case SimpleXMLType::Data: + case SimpleXMLType::CData: { + if (loader->style) _svgLoaderParserXmlCssStyle(loader, content, length); + break; + } + case SimpleXMLType::DoctypeChild: { + break; + } + case SimpleXMLType::Ignored: + case SimpleXMLType::Comment: + case SimpleXMLType::Doctype: { + break; + } + default: { + break; + } + } + + return true; +} + + +static void _inefficientNodeCheck(TVG_UNUSED SvgNode* node) +{ +#ifdef THORVG_LOG_ENABLED + auto type = simpleXmlNodeTypeToString(node->type); + + if (!node->style->display && node->type != SvgNodeType::ClipPath) TVGLOG("SVG", "Inefficient elements used [Display is none][Node Type : %s]", type); + if (node->style->opacity == 0) TVGLOG("SVG", "Inefficient elements used [Opacity is zero][Node Type : %s]", type); + if (node->style->fill.opacity == 0 && node->style->stroke.opacity == 0) TVGLOG("SVG", "Inefficient elements used [Fill opacity and stroke opacity are zero][Node Type : %s]", type); + + switch (node->type) { + case SvgNodeType::Path: { + if (!node->node.path.path) TVGLOG("SVG", "Inefficient elements used [Empty path][Node Type : %s]", type); + break; + } + case SvgNodeType::Ellipse: { + if (node->node.ellipse.rx == 0 && node->node.ellipse.ry == 0) TVGLOG("SVG", "Inefficient elements used [Size is zero][Node Type : %s]", type); + break; + } + case SvgNodeType::Polygon: + case SvgNodeType::Polyline: { + if (node->node.polygon.pts.count < 2) TVGLOG("SVG", "Inefficient elements used [Invalid Polygon][Node Type : %s]", type); + break; + } + case SvgNodeType::Circle: { + if (node->node.circle.r == 0) TVGLOG("SVG", "Inefficient elements used [Size is zero][Node Type : %s]", type); + break; + } + case SvgNodeType::Rect: { + if (node->node.rect.w == 0 && node->node.rect.h) TVGLOG("SVG", "Inefficient elements used [Size is zero][Node Type : %s]", type); + break; + } + case SvgNodeType::Line: { + if (node->node.line.x1 == node->node.line.x2 && node->node.line.y1 == node->node.line.y2) TVGLOG("SVG", "Inefficient elements used [Size is zero][Node Type : %s]", type); + break; + } + default: break; + } +#endif +} + + +static void _updateStyle(SvgNode* node, SvgStyleProperty* parentStyle) +{ + _styleInherit(node->style, parentStyle); + _inefficientNodeCheck(node); + + auto child = node->child.data; + for (uint32_t i = 0; i < node->child.count; ++i, ++child) { + _updateStyle(*child, node->style); + } +} + + +static SvgStyleGradient* _gradientDup(SvgLoaderData* loader, Array* gradients, const char* id) +{ + SvgStyleGradient* result = nullptr; + + auto gradList = gradients->data; + + for (uint32_t i = 0; i < gradients->count; ++i) { + if ((*gradList)->id && !strcmp((*gradList)->id, id)) { + result = _cloneGradient(*gradList); + break; + } + ++gradList; + } + + if (result && result->ref) { + gradList = gradients->data; + for (uint32_t i = 0; i < gradients->count; ++i) { + if ((*gradList)->id && !strcmp((*gradList)->id, result->ref)) { + _inheritGradient(loader, result, *gradList); + break; + } + ++gradList; + } + } + + return result; +} + + +static void _updateGradient(SvgLoaderData* loader, SvgNode* node, Array* gradients) +{ + if (node->child.count > 0) { + auto child = node->child.data; + for (uint32_t i = 0; i < node->child.count; ++i, ++child) { + _updateGradient(loader, *child, gradients); + } + } else { + if (node->style->fill.paint.url) { + auto newGrad = _gradientDup(loader, gradients, node->style->fill.paint.url); + if (newGrad) { + if (node->style->fill.paint.gradient) { + node->style->fill.paint.gradient->clear(); + free(node->style->fill.paint.gradient); + } + node->style->fill.paint.gradient = newGrad; + } + } + if (node->style->stroke.paint.url) { + auto newGrad = _gradientDup(loader, gradients, node->style->stroke.paint.url); + if (newGrad) { + if (node->style->stroke.paint.gradient) { + node->style->stroke.paint.gradient->clear(); + free(node->style->stroke.paint.gradient); + } + node->style->stroke.paint.gradient = newGrad; + } + } + } +} + + +static void _updateComposite(SvgNode* node, SvgNode* root) +{ + if (node->style->clipPath.url && !node->style->clipPath.node) { + SvgNode* findResult = _findNodeById(root, node->style->clipPath.url); + if (findResult) node->style->clipPath.node = findResult; + } + if (node->style->mask.url && !node->style->mask.node) { + SvgNode* findResult = _findNodeById(root, node->style->mask.url); + if (findResult) node->style->mask.node = findResult; + } + if (node->child.count > 0) { + auto child = node->child.data; + for (uint32_t i = 0; i < node->child.count; ++i, ++child) { + _updateComposite(*child, root); + } + } +} + + +static void _freeNodeStyle(SvgStyleProperty* style) +{ + if (!style) return; + + //style->clipPath.node and style->mask.node has only the addresses of node. Therefore, node is released from _freeNode. + free(style->clipPath.url); + free(style->mask.url); + free(style->cssClass); + + if (style->fill.paint.gradient) { + style->fill.paint.gradient->clear(); + free(style->fill.paint.gradient); + } + if (style->stroke.paint.gradient) { + style->stroke.paint.gradient->clear(); + free(style->stroke.paint.gradient); + } + free(style->fill.paint.url); + free(style->stroke.paint.url); + style->stroke.dash.array.reset(); + free(style); +} + + +static void _freeNode(SvgNode* node) +{ + if (!node) return; + + auto child = node->child.data; + for (uint32_t i = 0; i < node->child.count; ++i, ++child) { + _freeNode(*child); + } + node->child.reset(); + + free(node->id); + free(node->transform); + _freeNodeStyle(node->style); + switch (node->type) { + case SvgNodeType::Path: { + free(node->node.path.path); + break; + } + case SvgNodeType::Polygon: { + free(node->node.polygon.pts.data); + break; + } + case SvgNodeType::Polyline: { + free(node->node.polyline.pts.data); + break; + } + case SvgNodeType::Doc: { + _freeNode(node->node.doc.defs); + _freeNode(node->node.doc.style); + break; + } + case SvgNodeType::Defs: { + auto gradients = node->node.defs.gradients.data; + for (size_t i = 0; i < node->node.defs.gradients.count; ++i) { + (*gradients)->clear(); + free(*gradients); + ++gradients; + } + node->node.defs.gradients.reset(); + break; + } + case SvgNodeType::Image: { + free(node->node.image.href); + break; + } + default: { + break; + } + } + free(node); +} + + +static bool _svgLoaderParserForValidCheckXmlOpen(SvgLoaderData* loader, const char* content, unsigned int length) +{ + const char* attrs = nullptr; + int sz = length; + char tagName[20] = ""; + FactoryMethod method; + SvgNode *node = nullptr; + int attrsLength = 0; + loader->level++; + attrs = simpleXmlFindAttributesTag(content, length); + + if (!attrs) { + //Parse the empty tag + attrs = content; + while ((attrs != nullptr) && *attrs != '>') attrs++; + } + + if (attrs) { + sz = attrs - content; + while ((sz > 0) && (isspace(content[sz - 1]))) sz--; + if ((unsigned)sz >= sizeof(tagName)) return false; + strncpy(tagName, content, sz); + tagName[sz] = '\0'; + attrsLength = length - sz; + } + + if ((method = _findGroupFactory(tagName))) { + if (!loader->doc) { + if (strcmp(tagName, "svg")) return true; //Not a valid svg document + node = method(loader, nullptr, attrs, attrsLength, simpleXmlParseAttributes); + loader->doc = node; + loader->stack.push(node); + return false; + } + } + return true; +} + + +static bool _svgLoaderParserForValidCheck(void* data, SimpleXMLType type, const char* content, unsigned int length) +{ + SvgLoaderData* loader = (SvgLoaderData*)data; + bool res = true;; + + switch (type) { + case SimpleXMLType::Open: + case SimpleXMLType::OpenEmpty: { + //If 'res' is false, it means tag is found. + res = _svgLoaderParserForValidCheckXmlOpen(loader, content, length); + break; + } + default: { + break; + } + } + + return res; +} + + +void SvgLoader::clear(bool all) +{ + //flush out the intermediate data + free(loaderData.svgParse); + loaderData.svgParse = nullptr; + + for (auto gradient = loaderData.gradients.begin(); gradient < loaderData.gradients.end(); ++gradient) { + (*gradient)->clear(); + free(*gradient); + } + loaderData.gradients.reset(); + + _freeNode(loaderData.doc); + loaderData.doc = nullptr; + loaderData.stack.reset(); + + if (!all) return; + + for (auto p = loaderData.images.begin(); p < loaderData.images.end(); ++p) { + free(*p); + } + loaderData.images.reset(); + + if (copy) free((char*)content); + + delete(root); + root = nullptr; + + size = 0; + content = nullptr; + copy = false; +} + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +SvgLoader::SvgLoader() : ImageLoader(FileType::Svg) +{ +} + + +SvgLoader::~SvgLoader() +{ + this->done(); + clear(); +} + + +void SvgLoader::run(unsigned tid) +{ + //According to the SVG standard the value of the width/height of the viewbox set to 0 disables rendering + if ((viewFlag & SvgViewFlag::Viewbox) && (fabsf(vw) <= FLOAT_EPSILON || fabsf(vh) <= FLOAT_EPSILON)) { + TVGLOG("SVG", "The width and/or height set to 0 - rendering disabled."); + root = Scene::gen().release(); + return; + } + + if (!simpleXmlParse(content, size, true, _svgLoaderParser, &(loaderData))) return; + + if (loaderData.doc) { + auto defs = loaderData.doc->node.doc.defs; + + if (loaderData.nodesToStyle.count > 0) cssApplyStyleToPostponeds(loaderData.nodesToStyle, loaderData.cssStyle); + if (loaderData.cssStyle) cssUpdateStyle(loaderData.doc, loaderData.cssStyle); + + if (loaderData.cloneNodes.count > 0) _clonePostponedNodes(&loaderData.cloneNodes, loaderData.doc); + + _updateComposite(loaderData.doc, loaderData.doc); + if (defs) _updateComposite(loaderData.doc, defs); + + _updateStyle(loaderData.doc, nullptr); + if (defs) _updateStyle(defs, nullptr); + + if (loaderData.gradients.count > 0) _updateGradient(&loaderData, loaderData.doc, &loaderData.gradients); + if (defs) _updateGradient(&loaderData, loaderData.doc, &defs->node.defs.gradients); + } + root = svgSceneBuild(loaderData, {vx, vy, vw, vh}, w, h, align, meetOrSlice, svgPath, viewFlag); + + //In case no viewbox and width/height data is provided the completion of loading + //has to be forced, in order to establish this data based on the whole picture. + if (!(viewFlag & SvgViewFlag::Viewbox)) { + //Override viewbox & size again after svg loading. + vx = loaderData.doc->node.doc.vx; + vy = loaderData.doc->node.doc.vy; + vw = loaderData.doc->node.doc.vw; + vh = loaderData.doc->node.doc.vh; + w = loaderData.doc->node.doc.w; + h = loaderData.doc->node.doc.h; + } + + clear(false); +} + + +bool SvgLoader::header() +{ + //For valid check, only tag is parsed first. + //If the tag is found, the loaded file is valid and stores viewbox information. + //After that, the remaining content data is parsed in order with async. + loaderData.svgParse = (SvgParser*)malloc(sizeof(SvgParser)); + if (!loaderData.svgParse) return false; + + loaderData.svgParse->flags = SvgStopStyleFlags::StopDefault; + viewFlag = SvgViewFlag::None; + + simpleXmlParse(content, size, true, _svgLoaderParserForValidCheck, &(loaderData)); + + if (loaderData.doc && loaderData.doc->type == SvgNodeType::Doc) { + viewFlag = loaderData.doc->node.doc.viewFlag; + align = loaderData.doc->node.doc.align; + meetOrSlice = loaderData.doc->node.doc.meetOrSlice; + + if (viewFlag & SvgViewFlag::Viewbox) { + vx = loaderData.doc->node.doc.vx; + vy = loaderData.doc->node.doc.vy; + vw = loaderData.doc->node.doc.vw; + vh = loaderData.doc->node.doc.vh; + + if (viewFlag & SvgViewFlag::Width) w = loaderData.doc->node.doc.w; + else { + w = loaderData.doc->node.doc.vw; + if (viewFlag & SvgViewFlag::WidthInPercent) { + w *= loaderData.doc->node.doc.w; + viewFlag = (viewFlag ^ SvgViewFlag::WidthInPercent); + } + viewFlag = (viewFlag | SvgViewFlag::Width); + } + if (viewFlag & SvgViewFlag::Height) h = loaderData.doc->node.doc.h; + else { + h = loaderData.doc->node.doc.vh; + if (viewFlag & SvgViewFlag::HeightInPercent) { + h *= loaderData.doc->node.doc.h; + viewFlag = (viewFlag ^ SvgViewFlag::HeightInPercent); + } + viewFlag = (viewFlag | SvgViewFlag::Height); + } + //In case no viewbox and width/height data is provided the completion of loading + //has to be forced, in order to establish this data based on the whole picture. + } else { + //Before loading, set default viewbox & size if they are empty + vx = vy = 0.0f; + if (viewFlag & SvgViewFlag::Width) { + vw = w = loaderData.doc->node.doc.w; + } else { + vw = 1.0f; + if (viewFlag & SvgViewFlag::WidthInPercent) { + w = loaderData.doc->node.doc.w; + } else w = 1.0f; + } + + if (viewFlag & SvgViewFlag::Height) { + vh = h = loaderData.doc->node.doc.h; + } else { + vh = 1.0f; + if (viewFlag & SvgViewFlag::HeightInPercent) { + h = loaderData.doc->node.doc.h; + } else h = 1.0f; + } + + run(0); + } + + return true; + } + + TVGLOG("SVG", "No SVG File. There is no "); + return false; +} + + +bool SvgLoader::open(const char* data, uint32_t size, bool copy) +{ + clear(); + + if (copy) { + content = (char*)malloc(size + 1); + if (!content) return false; + memcpy((char*)content, data, size); + content[size] = '\0'; + } else content = (char*)data; + + this->size = size; + this->copy = copy; + + return header(); +} + + +bool SvgLoader::open(const string& path) +{ + clear(); + + ifstream f; + f.open(path); + + if (!f.is_open()) return false; + + svgPath = path; + getline(f, filePath, '\0'); + f.close(); + + if (filePath.empty()) return false; + + content = (char*)filePath.c_str(); + size = filePath.size(); + + return header(); +} + + +bool SvgLoader::resize(Paint* paint, float w, float h) +{ + if (!paint) return false; + + auto sx = w / this->w; + auto sy = h / this->h; + Matrix m = {sx, 0, 0, 0, sy, 0, 0, 0, 1}; + paint->transform(m); + + return true; +} + + +bool SvgLoader::read() +{ + if (!content || size == 0) return false; + + //the loading has been already completed in header() + if (root || !LoadModule::read()) return true; + + TaskScheduler::request(this); + + return true; +} + + +bool SvgLoader::close() +{ + if (!LoadModule::close()) return false; + this->done(); + clear(); + return true; +} + + +Paint* SvgLoader::paint() +{ + this->done(); + auto ret = root; + root = nullptr; + return ret; +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSvgLoader.h b/include/liblvgl/libs/thorvg/tvgSvgLoader.h new file mode 100644 index 00000000..d5cd7bb5 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSvgLoader.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_SVG_LOADER_H_ +#define _TVG_SVG_LOADER_H_ + +#include "tvgTaskScheduler.h" +#include "tvgSvgLoaderCommon.h" + +class SvgLoader : public ImageLoader, public Task +{ +public: + string filePath; + string svgPath = ""; + char* content = nullptr; + uint32_t size = 0; + + SvgLoaderData loaderData; + Scene* root = nullptr; + + bool copy = false; + + SvgLoader(); + ~SvgLoader(); + + bool open(const string& path) override; + bool open(const char* data, uint32_t size, bool copy) override; + bool resize(Paint* paint, float w, float h) override; + bool read() override; + bool close() override; + + Paint* paint() override; + +private: + SvgViewFlag viewFlag = SvgViewFlag::None; + AspectRatioAlign align = AspectRatioAlign::XMidYMid; + AspectRatioMeetOrSlice meetOrSlice = AspectRatioMeetOrSlice::Meet; + float vx = 0; + float vy = 0; + float vw = 0; + float vh = 0; + + bool header(); + void clear(bool all = true); + void run(unsigned tid) override; +}; + + +#endif //_TVG_SVG_LOADER_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSvgLoaderCommon.h b/include/liblvgl/libs/thorvg/tvgSvgLoaderCommon.h new file mode 100644 index 00000000..b0373908 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSvgLoaderCommon.h @@ -0,0 +1,577 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_SVG_LOADER_COMMON_H_ +#define _TVG_SVG_LOADER_COMMON_H_ + +#include "tvgCommon.h" +#include "tvgArray.h" + +struct SvgNode; +struct SvgStyleGradient; + +//NOTE: Please update simpleXmlNodeTypeToString() as well. +enum class SvgNodeType +{ + Doc, + G, + Defs, + Animation, + Arc, + Circle, + Ellipse, + Image, + Line, + Path, + Polygon, + Polyline, + Rect, + Text, + TextArea, + Tspan, + Use, + Video, + ClipPath, + Mask, + CssStyle, + Symbol, + Unknown +}; + +/* +// TODO - remove? +enum class SvgLengthType +{ + Percent, + Px, + Pc, + Pt, + Mm, + Cm, + In, +}; +*/ + +enum class SvgFillFlags +{ + Paint = 0x01, + Opacity = 0x02, + Gradient = 0x04, + FillRule = 0x08, + ClipPath = 0x16 +}; + +constexpr bool operator &(SvgFillFlags a, SvgFillFlags b) +{ + return int(a) & int(b); +} + +constexpr SvgFillFlags operator |(SvgFillFlags a, SvgFillFlags b) +{ + return SvgFillFlags(int(a) | int(b)); +} + +enum class SvgStrokeFlags +{ + Paint = 0x1, + Opacity = 0x2, + Gradient = 0x4, + Scale = 0x8, + Width = 0x10, + Cap = 0x20, + Join = 0x40, + Dash = 0x80, + Miterlimit = 0x100, + DashOffset = 0x200 +}; + +constexpr bool operator &(SvgStrokeFlags a, SvgStrokeFlags b) +{ + return int(a) & int(b); +} + +constexpr SvgStrokeFlags operator |(SvgStrokeFlags a, SvgStrokeFlags b) +{ + return SvgStrokeFlags(int(a) | int(b)); +} + + +enum class SvgGradientType +{ + Linear, + Radial +}; + +enum class SvgStyleFlags +{ + Color = 0x01, + Fill = 0x02, + FillRule = 0x04, + FillOpacity = 0x08, + Opacity = 0x010, + Stroke = 0x20, + StrokeWidth = 0x40, + StrokeLineJoin = 0x80, + StrokeLineCap = 0x100, + StrokeOpacity = 0x200, + StrokeDashArray = 0x400, + Transform = 0x800, + ClipPath = 0x1000, + Mask = 0x2000, + MaskType = 0x4000, + Display = 0x8000, + PaintOrder = 0x10000, + StrokeMiterlimit = 0x20000, + StrokeDashOffset = 0x40000, +}; + +constexpr bool operator &(SvgStyleFlags a, SvgStyleFlags b) +{ + return int(a) & int(b); +} + +constexpr SvgStyleFlags operator |(SvgStyleFlags a, SvgStyleFlags b) +{ + return SvgStyleFlags(int(a) | int(b)); +} + +enum class SvgStopStyleFlags +{ + StopDefault = 0x0, + StopOpacity = 0x01, + StopColor = 0x02 +}; + +constexpr bool operator &(SvgStopStyleFlags a, SvgStopStyleFlags b) +{ + return int(a) & int(b); +} + +constexpr SvgStopStyleFlags operator |(SvgStopStyleFlags a, SvgStopStyleFlags b) +{ + return SvgStopStyleFlags(int(a) | int(b)); +} + +enum class SvgGradientFlags +{ + None = 0x0, + GradientUnits = 0x1, + SpreadMethod = 0x2, + X1 = 0x4, + X2 = 0x8, + Y1 = 0x10, + Y2 = 0x20, + Cx = 0x40, + Cy = 0x80, + R = 0x100, + Fx = 0x200, + Fy = 0x400, + Fr = 0x800 +}; + +constexpr bool operator &(SvgGradientFlags a, SvgGradientFlags b) +{ + return int(a) & int(b); +} + +constexpr SvgGradientFlags operator |(SvgGradientFlags a, SvgGradientFlags b) +{ + return SvgGradientFlags(int(a) | int(b)); +} + +enum class SvgFillRule +{ + Winding = 0, + OddEven = 1 +}; + +enum class SvgMaskType +{ + Luminance = 0, + Alpha +}; + +//Length type to recalculate %, pt, pc, mm, cm etc +enum class SvgParserLengthType +{ + Vertical, + Horizontal, + //In case of, for example, radius of radial gradient + Other +}; + +enum class SvgViewFlag +{ + None = 0x0, + Width = 0x01, //viewPort width + Height = 0x02, //viewPort height + Viewbox = 0x04, //viewBox x,y,w,h - used only if all 4 are correctly set + WidthInPercent = 0x08, + HeightInPercent = 0x10 +}; + +constexpr bool operator &(SvgViewFlag a, SvgViewFlag b) +{ + return static_cast(a) & static_cast(b); +} + +constexpr SvgViewFlag operator |(SvgViewFlag a, SvgViewFlag b) +{ + return SvgViewFlag(int(a) | int(b)); +} + +constexpr SvgViewFlag operator ^(SvgViewFlag a, SvgViewFlag b) +{ + return SvgViewFlag(int(a) ^ int(b)); +} + +enum class AspectRatioAlign +{ + None, + XMinYMin, + XMidYMin, + XMaxYMin, + XMinYMid, + XMidYMid, + XMaxYMid, + XMinYMax, + XMidYMax, + XMaxYMax +}; + +enum class AspectRatioMeetOrSlice +{ + Meet, + Slice +}; + +struct SvgDocNode +{ + float w; //unit: point or in percentage see: SvgViewFlag + float h; //unit: point or in percentage see: SvgViewFlag + float vx; + float vy; + float vw; + float vh; + SvgViewFlag viewFlag; + SvgNode* defs; + SvgNode* style; + AspectRatioAlign align; + AspectRatioMeetOrSlice meetOrSlice; +}; + +struct SvgGNode +{ +}; + +struct SvgDefsNode +{ + Array gradients; +}; + +struct SvgSymbolNode +{ + float w, h; + float vx, vy, vw, vh; + AspectRatioAlign align; + AspectRatioMeetOrSlice meetOrSlice; + bool overflowVisible; + bool hasViewBox; + bool hasWidth; + bool hasHeight; +}; + +struct SvgUseNode +{ + float x, y, w, h; + bool isWidthSet; + bool isHeightSet; + SvgNode* symbol; +}; + +struct SvgEllipseNode +{ + float cx; + float cy; + float rx; + float ry; +}; + +struct SvgCircleNode +{ + float cx; + float cy; + float r; +}; + +struct SvgRectNode +{ + float x; + float y; + float w; + float h; + float rx; + float ry; + bool hasRx; + bool hasRy; +}; + +struct SvgLineNode +{ + float x1; + float y1; + float x2; + float y2; +}; + +struct SvgImageNode +{ + float x, y, w, h; + char* href; +}; + +struct SvgPathNode +{ + char* path; +}; + +struct SvgPolygonNode +{ + Array pts; +}; + +struct SvgClipNode +{ + bool userSpace; +}; + +struct SvgMaskNode +{ + SvgMaskType type; + bool userSpace; +}; + +struct SvgCssStyleNode +{ +}; + +struct SvgLinearGradient +{ + float x1; + float y1; + float x2; + float y2; + bool isX1Percentage; + bool isY1Percentage; + bool isX2Percentage; + bool isY2Percentage; +}; + +struct SvgRadialGradient +{ + float cx; + float cy; + float fx; + float fy; + float r; + float fr; + bool isCxPercentage; + bool isCyPercentage; + bool isFxPercentage; + bool isFyPercentage; + bool isRPercentage; + bool isFrPercentage; +}; + +struct SvgComposite +{ + char *url; + SvgNode* node; + bool applying; //flag for checking circular dependency. +}; + +struct SvgColor +{ + uint8_t r; + uint8_t g; + uint8_t b; +}; + +struct SvgPaint +{ + SvgStyleGradient* gradient; + char *url; + SvgColor color; + bool none; + bool curColor; +}; + +struct SvgDash +{ + Array array; + float offset; +}; + +struct SvgStyleGradient +{ + SvgGradientType type; + char* id; + char* ref; + FillSpread spread; + SvgRadialGradient* radial; + SvgLinearGradient* linear; + Matrix* transform; + Array stops; + SvgGradientFlags flags; + bool userSpace; + + void clear() + { + stops.reset(); + free(transform); + free(radial); + free(linear); + free(ref); + free(id); + } +}; + +struct SvgStyleFill +{ + SvgFillFlags flags; + SvgPaint paint; + int opacity; + FillRule fillRule; +}; + +struct SvgStyleStroke +{ + SvgStrokeFlags flags; + SvgPaint paint; + int opacity; + float scale; + float width; + float centered; + StrokeCap cap; + StrokeJoin join; + float miterlimit; + SvgDash dash; +}; + +struct SvgStyleProperty +{ + SvgStyleFill fill; + SvgStyleStroke stroke; + SvgComposite clipPath; + SvgComposite mask; + int opacity; + SvgColor color; + char* cssClass; + SvgStyleFlags flags; + SvgStyleFlags flagsImportance; //indicates the importance of the flag - if set, higher priority is applied (https://drafts.csswg.org/css-cascade-4/#importance) + bool curColorSet; + bool paintOrder; //true if default (fill, stroke), false otherwise + bool display; +}; + +struct SvgNode +{ + SvgNodeType type; + SvgNode* parent; + Array child; + char *id; + SvgStyleProperty *style; + Matrix* transform; + union { + SvgGNode g; + SvgDocNode doc; + SvgDefsNode defs; + SvgUseNode use; + SvgCircleNode circle; + SvgEllipseNode ellipse; + SvgPolygonNode polygon; + SvgPolygonNode polyline; + SvgRectNode rect; + SvgPathNode path; + SvgLineNode line; + SvgImageNode image; + SvgMaskNode mask; + SvgClipNode clip; + SvgCssStyleNode cssStyle; + SvgSymbolNode symbol; + } node; + ~SvgNode(); +}; + +struct SvgParser +{ + SvgNode* node; + SvgStyleGradient* styleGrad; + Fill::ColorStop gradStop; + SvgStopStyleFlags flags; + struct + { + float x, y, w, h; + } global; + struct + { + bool parsedFx; + bool parsedFy; + } gradient; +}; + +struct SvgNodeIdPair +{ + SvgNode* node; + char *id; +}; + +struct SvgLoaderData +{ + Array stack; + SvgNode* doc = nullptr; + SvgNode* def = nullptr; + SvgNode* cssStyle = nullptr; + Array gradients; + SvgStyleGradient* latestGradient = nullptr; //For stops + SvgParser* svgParse = nullptr; + Array cloneNodes; + Array nodesToStyle; + Array images; //embedded images + int level = 0; + bool result = false; + bool style = false; + SvgNode* currentGraphicsNode = nullptr; +}; + +struct Box +{ + float x, y, w, h; +}; + +#endif + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSvgPath.cpp b/include/liblvgl/libs/thorvg/tvgSvgPath.cpp new file mode 100644 index 00000000..42312c37 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSvgPath.cpp @@ -0,0 +1,577 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +/* + * Copyright notice for the EFL: + + * Copyright (C) EFL developers (see AUTHORS) + + * All rights reserved. + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + + * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#define _USE_MATH_DEFINES //Math Constants are not defined in Standard C/C++. + +#include +#include +#include "tvgMath.h" +#include "tvgShape.h" +#include "tvgSvgLoaderCommon.h" +#include "tvgSvgPath.h" +#include "tvgStr.h" + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +static char* _skipComma(const char* content) +{ + while (*content && isspace(*content)) { + content++; + } + if (*content == ',') return (char*)content + 1; + return (char*)content; +} + + +static bool _parseNumber(char** content, float* number) +{ + char* end = NULL; + *number = strToFloat(*content, &end); + //If the start of string is not number + if ((*content) == end) return false; + //Skip comma if any + *content = _skipComma(end); + return true; +} + + +static bool _parseFlag(char** content, int* number) +{ + char* end = NULL; + if (*(*content) != '0' && *(*content) != '1') return false; + *number = *(*content) - '0'; + *content += 1; + end = *content; + *content = _skipComma(end); + + return true; +} + + +void _pathAppendArcTo(Array* cmds, Array* pts, Point* cur, Point* curCtl, float x, float y, float rx, float ry, float angle, bool largeArc, bool sweep) +{ + float cxp, cyp, cx, cy; + float sx, sy; + float cosPhi, sinPhi; + float dx2, dy2; + float x1p, y1p; + float x1p2, y1p2; + float rx2, ry2; + float lambda; + float c; + float at; + float theta1, deltaTheta; + float nat; + float delta, bcp; + float cosPhiRx, cosPhiRy; + float sinPhiRx, sinPhiRy; + float cosTheta1, sinTheta1; + int segments; + + //Some helpful stuff is available here: + //http://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes + sx = cur->x; + sy = cur->y; + + //Correction of out-of-range radii, see F6.6.1 (step 2) + rx = fabsf(rx); + ry = fabsf(ry); + + angle = mathDeg2Rad(angle); + cosPhi = cosf(angle); + sinPhi = sinf(angle); + dx2 = (sx - x) / 2.0f; + dy2 = (sy - y) / 2.0f; + x1p = cosPhi * dx2 + sinPhi * dy2; + y1p = cosPhi * dy2 - sinPhi * dx2; + x1p2 = x1p * x1p; + y1p2 = y1p * y1p; + rx2 = rx * rx; + ry2 = ry * ry; + lambda = (x1p2 / rx2) + (y1p2 / ry2); + + //Correction of out-of-range radii, see F6.6.2 (step 4) + if (lambda > 1.0f) { + //See F6.6.3 + float lambdaRoot = sqrtf(lambda); + + rx *= lambdaRoot; + ry *= lambdaRoot; + //Update rx2 and ry2 + rx2 = rx * rx; + ry2 = ry * ry; + } + + c = (rx2 * ry2) - (rx2 * y1p2) - (ry2 * x1p2); + + //Check if there is no possible solution + //(i.e. we can't do a square root of a negative value) + if (c < 0.0f) { + //Scale uniformly until we have a single solution + //(see F6.2) i.e. when c == 0.0 + float scale = sqrtf(1.0f - c / (rx2 * ry2)); + rx *= scale; + ry *= scale; + //Update rx2 and ry2 + rx2 = rx * rx; + ry2 = ry * ry; + + //Step 2 (F6.5.2) - simplified since c == 0.0 + cxp = 0.0f; + cyp = 0.0f; + //Step 3 (F6.5.3 first part) - simplified since cxp and cyp == 0.0 + cx = 0.0f; + cy = 0.0f; + } else { + //Complete c calculation + c = sqrtf(c / ((rx2 * y1p2) + (ry2 * x1p2))); + //Inverse sign if Fa == Fs + if (largeArc == sweep) c = -c; + + //Step 2 (F6.5.2) + cxp = c * (rx * y1p / ry); + cyp = c * (-ry * x1p / rx); + + //Step 3 (F6.5.3 first part) + cx = cosPhi * cxp - sinPhi * cyp; + cy = sinPhi * cxp + cosPhi * cyp; + } + + //Step 3 (F6.5.3 second part) we now have the center point of the ellipse + cx += (sx + x) / 2.0f; + cy += (sy + y) / 2.0f; + + //Step 4 (F6.5.4) + //We dont' use arccos (as per w3c doc), see + //http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/index.htm + //Note: atan2 (0.0, 1.0) == 0.0 + at = atan2(((y1p - cyp) / ry), ((x1p - cxp) / rx)); + theta1 = (at < 0.0f) ? 2.0f * MATH_PI + at : at; + + nat = atan2(((-y1p - cyp) / ry), ((-x1p - cxp) / rx)); + deltaTheta = (nat < at) ? 2.0f * MATH_PI - at + nat : nat - at; + + if (sweep) { + //Ensure delta theta < 0 or else add 360 degrees + if (deltaTheta < 0.0f) deltaTheta += 2.0f * MATH_PI; + } else { + //Ensure delta theta > 0 or else substract 360 degrees + if (deltaTheta > 0.0f) deltaTheta -= 2.0f * MATH_PI; + } + + //Add several cubic bezier to approximate the arc + //(smaller than 90 degrees) + //We add one extra segment because we want something + //Smaller than 90deg (i.e. not 90 itself) + segments = static_cast(fabsf(deltaTheta / MATH_PI2) + 1.0f); + delta = deltaTheta / segments; + + //http://www.stillhq.com/ctpfaq/2001/comp.text.pdf-faq-2001-04.txt (section 2.13) + bcp = 4.0f / 3.0f * (1.0f - cosf(delta / 2.0f)) / sinf(delta / 2.0f); + + cosPhiRx = cosPhi * rx; + cosPhiRy = cosPhi * ry; + sinPhiRx = sinPhi * rx; + sinPhiRy = sinPhi * ry; + + cosTheta1 = cosf(theta1); + sinTheta1 = sinf(theta1); + + for (int i = 0; i < segments; ++i) { + //End angle (for this segment) = current + delta + float c1x, c1y, ex, ey, c2x, c2y; + float theta2 = theta1 + delta; + float cosTheta2 = cosf(theta2); + float sinTheta2 = sinf(theta2); + Point p[3]; + + //First control point (based on start point sx,sy) + c1x = sx - bcp * (cosPhiRx * sinTheta1 + sinPhiRy * cosTheta1); + c1y = sy + bcp * (cosPhiRy * cosTheta1 - sinPhiRx * sinTheta1); + + //End point (for this segment) + ex = cx + (cosPhiRx * cosTheta2 - sinPhiRy * sinTheta2); + ey = cy + (sinPhiRx * cosTheta2 + cosPhiRy * sinTheta2); + + //Second control point (based on end point ex,ey) + c2x = ex + bcp * (cosPhiRx * sinTheta2 + sinPhiRy * cosTheta2); + c2y = ey + bcp * (sinPhiRx * sinTheta2 - cosPhiRy * cosTheta2); + cmds->push(PathCommand::CubicTo); + p[0] = {c1x, c1y}; + p[1] = {c2x, c2y}; + p[2] = {ex, ey}; + pts->push(p[0]); + pts->push(p[1]); + pts->push(p[2]); + *curCtl = p[1]; + *cur = p[2]; + + //Next start point is the current end point (same for angle) + sx = ex; + sy = ey; + theta1 = theta2; + //Avoid recomputations + cosTheta1 = cosTheta2; + sinTheta1 = sinTheta2; + } +} + +static int _numberCount(char cmd) +{ + int count = 0; + switch (cmd) { + case 'M': + case 'm': + case 'L': + case 'l': + case 'T': + case 't': { + count = 2; + break; + } + case 'C': + case 'c': + case 'E': + case 'e': { + count = 6; + break; + } + case 'H': + case 'h': + case 'V': + case 'v': { + count = 1; + break; + } + case 'S': + case 's': + case 'Q': + case 'q': { + count = 4; + break; + } + case 'A': + case 'a': { + count = 7; + break; + } + default: + break; + } + return count; +} + + +static bool _processCommand(Array* cmds, Array* pts, char cmd, float* arr, int count, Point* cur, Point* curCtl, Point* startPoint, bool *isQuadratic, bool* closed) +{ + switch (cmd) { + case 'm': + case 'l': + case 'c': + case 's': + case 'q': + case 't': { + for (int i = 0; i < count - 1; i += 2) { + arr[i] = arr[i] + cur->x; + arr[i + 1] = arr[i + 1] + cur->y; + } + break; + } + case 'h': { + arr[0] = arr[0] + cur->x; + break; + } + case 'v': { + arr[0] = arr[0] + cur->y; + break; + } + case 'a': { + arr[5] = arr[5] + cur->x; + arr[6] = arr[6] + cur->y; + break; + } + default: { + break; + } + } + + switch (cmd) { + case 'm': + case 'M': { + Point p = {arr[0], arr[1]}; + cmds->push(PathCommand::MoveTo); + pts->push(p); + *cur = {arr[0], arr[1]}; + *startPoint = {arr[0], arr[1]}; + break; + } + case 'l': + case 'L': { + Point p = {arr[0], arr[1]}; + cmds->push(PathCommand::LineTo); + pts->push(p); + *cur = {arr[0], arr[1]}; + break; + } + case 'c': + case 'C': { + Point p[3]; + cmds->push(PathCommand::CubicTo); + p[0] = {arr[0], arr[1]}; + p[1] = {arr[2], arr[3]}; + p[2] = {arr[4], arr[5]}; + pts->push(p[0]); + pts->push(p[1]); + pts->push(p[2]); + *curCtl = p[1]; + *cur = p[2]; + *isQuadratic = false; + break; + } + case 's': + case 'S': { + Point p[3], ctrl; + if ((cmds->count > 1) && (cmds->last() == PathCommand::CubicTo) && + !(*isQuadratic)) { + ctrl.x = 2 * cur->x - curCtl->x; + ctrl.y = 2 * cur->y - curCtl->y; + } else { + ctrl = *cur; + } + cmds->push(PathCommand::CubicTo); + p[0] = ctrl; + p[1] = {arr[0], arr[1]}; + p[2] = {arr[2], arr[3]}; + pts->push(p[0]); + pts->push(p[1]); + pts->push(p[2]); + *curCtl = p[1]; + *cur = p[2]; + *isQuadratic = false; + break; + } + case 'q': + case 'Q': { + Point p[3]; + float ctrl_x0 = (cur->x + 2 * arr[0]) * (1.0f / 3.0f); + float ctrl_y0 = (cur->y + 2 * arr[1]) * (1.0f / 3.0f); + float ctrl_x1 = (arr[2] + 2 * arr[0]) * (1.0f / 3.0f); + float ctrl_y1 = (arr[3] + 2 * arr[1]) * (1.0f / 3.0f); + cmds->push(PathCommand::CubicTo); + p[0] = {ctrl_x0, ctrl_y0}; + p[1] = {ctrl_x1, ctrl_y1}; + p[2] = {arr[2], arr[3]}; + pts->push(p[0]); + pts->push(p[1]); + pts->push(p[2]); + *curCtl = {arr[0], arr[1]}; + *cur = p[2]; + *isQuadratic = true; + break; + } + case 't': + case 'T': { + Point p[3], ctrl; + if ((cmds->count > 1) && (cmds->last() == PathCommand::CubicTo) && + *isQuadratic) { + ctrl.x = 2 * cur->x - curCtl->x; + ctrl.y = 2 * cur->y - curCtl->y; + } else { + ctrl = *cur; + } + float ctrl_x0 = (cur->x + 2 * ctrl.x) * (1.0f / 3.0f); + float ctrl_y0 = (cur->y + 2 * ctrl.y) * (1.0f / 3.0f); + float ctrl_x1 = (arr[0] + 2 * ctrl.x) * (1.0f / 3.0f); + float ctrl_y1 = (arr[1] + 2 * ctrl.y) * (1.0f / 3.0f); + cmds->push(PathCommand::CubicTo); + p[0] = {ctrl_x0, ctrl_y0}; + p[1] = {ctrl_x1, ctrl_y1}; + p[2] = {arr[0], arr[1]}; + pts->push(p[0]); + pts->push(p[1]); + pts->push(p[2]); + *curCtl = {ctrl.x, ctrl.y}; + *cur = p[2]; + *isQuadratic = true; + break; + } + case 'h': + case 'H': { + Point p = {arr[0], cur->y}; + cmds->push(PathCommand::LineTo); + pts->push(p); + cur->x = arr[0]; + break; + } + case 'v': + case 'V': { + Point p = {cur->x, arr[0]}; + cmds->push(PathCommand::LineTo); + pts->push(p); + cur->y = arr[0]; + break; + } + case 'z': + case 'Z': { + cmds->push(PathCommand::Close); + *cur = *startPoint; + *closed = true; + break; + } + case 'a': + case 'A': { + if (mathZero(arr[0]) || mathZero(arr[1])) { + Point p = {arr[5], arr[6]}; + cmds->push(PathCommand::LineTo); + pts->push(p); + *cur = {arr[5], arr[6]}; + } else if (!mathEqual(cur->x, arr[5]) || !mathEqual(cur->y, arr[6])) { + _pathAppendArcTo(cmds, pts, cur, curCtl, arr[5], arr[6], fabsf(arr[0]), fabsf(arr[1]), arr[2], arr[3], arr[4]); + *cur = *curCtl = {arr[5], arr[6]}; + *isQuadratic = false; + } + break; + } + default: { + return false; + } + } + return true; +} + + +static char* _nextCommand(char* path, char* cmd, float* arr, int* count, bool* closed) +{ + int large, sweep; + + path = _skipComma(path); + if (isalpha(*path)) { + *cmd = *path; + path++; + *count = _numberCount(*cmd); + } else { + if (*cmd == 'm') *cmd = 'l'; + else if (*cmd == 'M') *cmd = 'L'; + else { + if (*closed) return nullptr; + } + } + if (*count == 7) { + //Special case for arc command + if (_parseNumber(&path, &arr[0])) { + if (_parseNumber(&path, &arr[1])) { + if (_parseNumber(&path, &arr[2])) { + if (_parseFlag(&path, &large)) { + if (_parseFlag(&path, &sweep)) { + if (_parseNumber(&path, &arr[5])) { + if (_parseNumber(&path, &arr[6])) { + arr[3] = (float)large; + arr[4] = (float)sweep; + return path; + } + } + } + } + } + } + } + *count = 0; + return NULL; + } + for (int i = 0; i < *count; i++) { + if (!_parseNumber(&path, &arr[i])) { + *count = 0; + return NULL; + } + path = _skipComma(path); + } + return path; +} + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + + +bool svgPathToShape(const char* svgPath, Shape* shape) +{ + float numberArray[7]; + int numberCount = 0; + Point cur = { 0, 0 }; + Point curCtl = { 0, 0 }; + Point startPoint = { 0, 0 }; + char cmd = 0; + bool isQuadratic = false; + bool closed = false; + char* path = (char*)svgPath; + + auto& pts = P(shape)->rs.path.pts; + auto& cmds = P(shape)->rs.path.cmds; + auto lastCmds = cmds.count; + + while ((path[0] != '\0')) { + path = _nextCommand(path, &cmd, numberArray, &numberCount, &closed); + if (!path) break; + closed = false; + if (!_processCommand(&cmds, &pts, cmd, numberArray, numberCount, &cur, &curCtl, &startPoint, &isQuadratic, &closed)) break; + } + + if (cmds.count > lastCmds && cmds[lastCmds] != PathCommand::MoveTo) return false; + return true; +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSvgPath.h b/include/liblvgl/libs/thorvg/tvgSvgPath.h new file mode 100644 index 00000000..673fa0dd --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSvgPath.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_SVG_PATH_H_ +#define _TVG_SVG_PATH_H_ + +#include "tvgCommon.h" + +bool svgPathToShape(const char* svgPath, Shape* shape); + +#endif //_TVG_SVG_PATH_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSvgSceneBuilder.cpp b/include/liblvgl/libs/thorvg/tvgSvgSceneBuilder.cpp new file mode 100644 index 00000000..585e487a --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSvgSceneBuilder.cpp @@ -0,0 +1,888 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgMath.h" /* to include math.h before cstring */ +#include +#include +#include "tvgShape.h" +#include "tvgCompressor.h" +#include "tvgPaint.h" +#include "tvgFill.h" +#include "tvgStr.h" +#include "tvgSvgLoaderCommon.h" +#include "tvgSvgSceneBuilder.h" +#include "tvgSvgPath.h" +#include "tvgSvgUtil.h" + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +static bool _appendShape(SvgLoaderData& loaderData, SvgNode* node, Shape* shape, const Box& vBox, const string& svgPath); +static bool _appendClipShape(SvgLoaderData& loaderData, SvgNode* node, Shape* shape, const Box& vBox, const string& svgPath, const Matrix* transform); +static unique_ptr _sceneBuildHelper(SvgLoaderData& loaderData, const SvgNode* node, const Box& vBox, const string& svgPath, bool mask, int depth, bool* isMaskWhite = nullptr); + + +static inline bool _isGroupType(SvgNodeType type) +{ + if (type == SvgNodeType::Doc || type == SvgNodeType::G || type == SvgNodeType::Use || type == SvgNodeType::ClipPath || type == SvgNodeType::Symbol) return true; + return false; +} + + +//According to: https://www.w3.org/TR/SVG11/coords.html#ObjectBoundingBoxUnits (the last paragraph) +//a stroke width should be ignored for bounding box calculations +static Box _boundingBox(const Shape* shape) +{ + float x, y, w, h; + shape->bounds(&x, &y, &w, &h, false); + + if (auto strokeW = shape->strokeWidth()) { + x += 0.5f * strokeW; + y += 0.5f * strokeW; + w -= strokeW; + h -= strokeW; + } + + return {x, y, w, h}; +} + + +static void _transformMultiply(const Matrix* mBBox, Matrix* gradTransf) +{ + gradTransf->e13 = gradTransf->e13 * mBBox->e11 + mBBox->e13; + gradTransf->e12 *= mBBox->e11; + gradTransf->e11 *= mBBox->e11; + + gradTransf->e23 = gradTransf->e23 * mBBox->e22 + mBBox->e23; + gradTransf->e22 *= mBBox->e22; + gradTransf->e21 *= mBBox->e22; +} + + +static unique_ptr _applyLinearGradientProperty(SvgStyleGradient* g, const Shape* vg, const Box& vBox, int opacity) +{ + Fill::ColorStop* stops; + int stopCount = 0; + auto fillGrad = LinearGradient::gen(); + + bool isTransform = (g->transform ? true : false); + Matrix finalTransform = {1, 0, 0, 0, 1, 0, 0, 0, 1}; + if (isTransform) finalTransform = *g->transform; + + if (g->userSpace) { + g->linear->x1 = g->linear->x1 * vBox.w; + g->linear->y1 = g->linear->y1 * vBox.h; + g->linear->x2 = g->linear->x2 * vBox.w; + g->linear->y2 = g->linear->y2 * vBox.h; + } else { + Matrix m = {vBox.w, 0, vBox.x, 0, vBox.h, vBox.y, 0, 0, 1}; + if (isTransform) _transformMultiply(&m, &finalTransform); + else { + finalTransform = m; + isTransform = true; + } + } + + if (isTransform) fillGrad->transform(finalTransform); + + fillGrad->linear(g->linear->x1, g->linear->y1, g->linear->x2, g->linear->y2); + fillGrad->spread(g->spread); + + //Update the stops + stopCount = g->stops.count; + if (stopCount > 0) { + stops = (Fill::ColorStop*)calloc(stopCount, sizeof(Fill::ColorStop)); + if (!stops) return fillGrad; + auto prevOffset = 0.0f; + for (uint32_t i = 0; i < g->stops.count; ++i) { + auto colorStop = &g->stops[i]; + //Use premultiplied color + stops[i].r = colorStop->r; + stops[i].g = colorStop->g; + stops[i].b = colorStop->b; + stops[i].a = static_cast((colorStop->a * opacity) / 255); + stops[i].offset = colorStop->offset; + //check the offset corner cases - refer to: https://svgwg.org/svg2-draft/pservers.html#StopNotes + if (colorStop->offset < prevOffset) stops[i].offset = prevOffset; + else if (colorStop->offset > 1) stops[i].offset = 1; + prevOffset = stops[i].offset; + } + fillGrad->colorStops(stops, stopCount); + free(stops); + } + return fillGrad; +} + + +static unique_ptr _applyRadialGradientProperty(SvgStyleGradient* g, const Shape* vg, const Box& vBox, int opacity) +{ + Fill::ColorStop *stops; + int stopCount = 0; + auto fillGrad = RadialGradient::gen(); + + bool isTransform = (g->transform ? true : false); + Matrix finalTransform = {1, 0, 0, 0, 1, 0, 0, 0, 1}; + if (isTransform) finalTransform = *g->transform; + + if (g->userSpace) { + //The radius scaling is done according to the Units section: + //https://www.w3.org/TR/2015/WD-SVG2-20150915/coords.html + g->radial->cx = g->radial->cx * vBox.w; + g->radial->cy = g->radial->cy * vBox.h; + g->radial->r = g->radial->r * sqrtf(powf(vBox.w, 2.0f) + powf(vBox.h, 2.0f)) / sqrtf(2.0f); + g->radial->fx = g->radial->fx * vBox.w; + g->radial->fy = g->radial->fy * vBox.h; + g->radial->fr = g->radial->fr * sqrtf(powf(vBox.w, 2.0f) + powf(vBox.h, 2.0f)) / sqrtf(2.0f); + } else { + Matrix m = {vBox.w, 0, vBox.x, 0, vBox.h, vBox.y, 0, 0, 1}; + if (isTransform) _transformMultiply(&m, &finalTransform); + else { + finalTransform = m; + isTransform = true; + } + } + + if (isTransform) fillGrad->transform(finalTransform); + + P(fillGrad)->radial(g->radial->cx, g->radial->cy, g->radial->r, g->radial->fx, g->radial->fy, g->radial->fr); + fillGrad->spread(g->spread); + + //Update the stops + stopCount = g->stops.count; + if (stopCount > 0) { + stops = (Fill::ColorStop*)calloc(stopCount, sizeof(Fill::ColorStop)); + if (!stops) return fillGrad; + auto prevOffset = 0.0f; + for (uint32_t i = 0; i < g->stops.count; ++i) { + auto colorStop = &g->stops[i]; + //Use premultiplied color + stops[i].r = colorStop->r; + stops[i].g = colorStop->g; + stops[i].b = colorStop->b; + stops[i].a = static_cast((colorStop->a * opacity) / 255); + stops[i].offset = colorStop->offset; + //check the offset corner cases - refer to: https://svgwg.org/svg2-draft/pservers.html#StopNotes + if (colorStop->offset < prevOffset) stops[i].offset = prevOffset; + else if (colorStop->offset > 1) stops[i].offset = 1; + prevOffset = stops[i].offset; + } + fillGrad->colorStops(stops, stopCount); + free(stops); + } + return fillGrad; +} + + +//The SVG standard allows only for 'use' nodes that point directly to a basic shape. +static bool _appendClipUseNode(SvgLoaderData& loaderData, SvgNode* node, Shape* shape, const Box& vBox, const string& svgPath) +{ + if (node->child.count != 1) return false; + auto child = *(node->child.data); + + Matrix finalTransform = {1, 0, 0, 0, 1, 0, 0, 0, 1}; + if (node->transform) finalTransform = *node->transform; + if (node->node.use.x != 0.0f || node->node.use.y != 0.0f) { + Matrix m = {1, 0, node->node.use.x, 0, 1, node->node.use.y, 0, 0, 1}; + finalTransform = mathMultiply(&finalTransform, &m); + } + if (child->transform) finalTransform = mathMultiply(child->transform, &finalTransform); + + return _appendClipShape(loaderData, child, shape, vBox, svgPath, mathIdentity((const Matrix*)(&finalTransform)) ? nullptr : &finalTransform); +} + + +static bool _appendClipChild(SvgLoaderData& loaderData, SvgNode* node, Shape* shape, const Box& vBox, const string& svgPath, bool clip) +{ + if (node->type == SvgNodeType::Use) { + return _appendClipUseNode(loaderData, node, shape, vBox, svgPath); + } + return _appendClipShape(loaderData, node, shape, vBox, svgPath, nullptr); +} + + +static Matrix _compositionTransform(Paint* paint, const SvgNode* node, const SvgNode* compNode, SvgNodeType type) +{ + Matrix m = {1, 0, 0, 0, 1, 0, 0, 0, 1}; + //The initial mask transformation ignored according to the SVG standard. + if (node->transform && type != SvgNodeType::Mask) { + m = *node->transform; + } + if (compNode->transform) { + m = mathMultiply(&m, compNode->transform); + } + if (!compNode->node.clip.userSpace) { + float x, y, w, h; + P(paint)->bounds(&x, &y, &w, &h, false, false); + Matrix mBBox = {w, 0, x, 0, h, y, 0, 0, 1}; + m = mathMultiply(&m, &mBBox); + } + return m; +} + + +static void _applyComposition(SvgLoaderData& loaderData, Paint* paint, const SvgNode* node, const Box& vBox, const string& svgPath) +{ + /* ClipPath */ + /* Do not drop in Circular Dependency for ClipPath. + Composition can be applied recursively if its children nodes have composition target to this one. */ + if (node->style->clipPath.applying) { + TVGLOG("SVG", "Multiple Composition Tried! Check out Circular dependency?"); + } else { + auto compNode = node->style->clipPath.node; + if (compNode && compNode->child.count > 0) { + node->style->clipPath.applying = true; + + auto comp = Shape::gen(); + + auto child = compNode->child.data; + auto valid = false; //Composite only when valid shapes exist + + for (uint32_t i = 0; i < compNode->child.count; ++i, ++child) { + if (_appendClipChild(loaderData, *child, comp.get(), vBox, svgPath, compNode->child.count > 1)) valid = true; + } + + if (valid) { + Matrix finalTransform = _compositionTransform(paint, node, compNode, SvgNodeType::ClipPath); + comp->transform(finalTransform); + + paint->composite(std::move(comp), CompositeMethod::ClipPath); + } + + node->style->clipPath.applying = false; + } + } + + /* Mask */ + /* Do not drop in Circular Dependency for Mask. + Composition can be applied recursively if its children nodes have composition target to this one. */ + if (node->style->mask.applying) { + TVGLOG("SVG", "Multiple Composition Tried! Check out Circular dependency?"); + } else { + auto compNode = node->style->mask.node; + if (compNode && compNode->child.count > 0) { + node->style->mask.applying = true; + + bool isMaskWhite = true; + if (auto comp = _sceneBuildHelper(loaderData, compNode, vBox, svgPath, true, 0, &isMaskWhite)) { + if (!compNode->node.mask.userSpace) { + Matrix finalTransform = _compositionTransform(paint, node, compNode, SvgNodeType::Mask); + comp->transform(finalTransform); + } else { + if (node->transform) comp->transform(*node->transform); + } + + if (compNode->node.mask.type == SvgMaskType::Luminance && !isMaskWhite) { + paint->composite(std::move(comp), CompositeMethod::LumaMask); + } else { + paint->composite(std::move(comp), CompositeMethod::AlphaMask); + } + } + + node->style->mask.applying = false; + } + } +} + + +static void _applyProperty(SvgLoaderData& loaderData, SvgNode* node, Shape* vg, const Box& vBox, const string& svgPath, bool clip) +{ + SvgStyleProperty* style = node->style; + + //Clip transformation is applied directly to the path in the _appendClipShape function + if (node->transform && !clip) vg->transform(*node->transform); + if (node->type == SvgNodeType::Doc || !node->style->display) return; + + //If fill property is nullptr then do nothing + if (style->fill.paint.none) { + //Do nothing + } else if (style->fill.paint.gradient) { + Box bBox = vBox; + if (!style->fill.paint.gradient->userSpace) bBox = _boundingBox(vg); + + if (style->fill.paint.gradient->type == SvgGradientType::Linear) { + auto linear = _applyLinearGradientProperty(style->fill.paint.gradient, vg, bBox, style->fill.opacity); + vg->fill(std::move(linear)); + } else if (style->fill.paint.gradient->type == SvgGradientType::Radial) { + auto radial = _applyRadialGradientProperty(style->fill.paint.gradient, vg, bBox, style->fill.opacity); + vg->fill(std::move(radial)); + } + } else if (style->fill.paint.url) { + //TODO: Apply the color pointed by url + } else if (style->fill.paint.curColor) { + //Apply the current style color + vg->fill(style->color.r, style->color.g, style->color.b, style->fill.opacity); + } else { + //Apply the fill color + vg->fill(style->fill.paint.color.r, style->fill.paint.color.g, style->fill.paint.color.b, style->fill.opacity); + } + + //Apply the fill rule + vg->fill((tvg::FillRule)style->fill.fillRule); + //Rendering order + vg->order(!style->paintOrder); + + //Apply node opacity + if (style->opacity < 255) vg->opacity(style->opacity); + + if (node->type == SvgNodeType::G || node->type == SvgNodeType::Use) return; + + //Apply the stroke style property + vg->stroke(style->stroke.width); + vg->stroke(style->stroke.cap); + vg->stroke(style->stroke.join); + vg->strokeMiterlimit(style->stroke.miterlimit); + if (style->stroke.dash.array.count > 0) { + P(vg)->strokeDash(style->stroke.dash.array.data, style->stroke.dash.array.count, style->stroke.dash.offset); + } + + //If stroke property is nullptr then do nothing + if (style->stroke.paint.none) { + vg->stroke(0.0f); + } else if (style->stroke.paint.gradient) { + Box bBox = vBox; + if (!style->stroke.paint.gradient->userSpace) bBox = _boundingBox(vg); + + if (style->stroke.paint.gradient->type == SvgGradientType::Linear) { + auto linear = _applyLinearGradientProperty(style->stroke.paint.gradient, vg, bBox, style->stroke.opacity); + vg->stroke(std::move(linear)); + } else if (style->stroke.paint.gradient->type == SvgGradientType::Radial) { + auto radial = _applyRadialGradientProperty(style->stroke.paint.gradient, vg, bBox, style->stroke.opacity); + vg->stroke(std::move(radial)); + } + } else if (style->stroke.paint.url) { + //TODO: Apply the color pointed by url + } else if (style->stroke.paint.curColor) { + //Apply the current style color + vg->stroke(style->color.r, style->color.g, style->color.b, style->stroke.opacity); + } else { + //Apply the stroke color + vg->stroke(style->stroke.paint.color.r, style->stroke.paint.color.g, style->stroke.paint.color.b, style->stroke.opacity); + } + + _applyComposition(loaderData, vg, node, vBox, svgPath); +} + + +static unique_ptr _shapeBuildHelper(SvgLoaderData& loaderData, SvgNode* node, const Box& vBox, const string& svgPath) +{ + auto shape = Shape::gen(); + if (_appendShape(loaderData, node, shape.get(), vBox, svgPath)) return shape; + else return nullptr; +} + + +static bool _recognizeShape(SvgNode* node, Shape* shape) +{ + switch (node->type) { + case SvgNodeType::Path: { + if (node->node.path.path) { + if (!svgPathToShape(node->node.path.path, shape)) { + TVGERR("SVG", "Invalid path information."); + return false; + } + } + break; + } + case SvgNodeType::Ellipse: { + shape->appendCircle(node->node.ellipse.cx, node->node.ellipse.cy, node->node.ellipse.rx, node->node.ellipse.ry); + break; + } + case SvgNodeType::Polygon: { + if (node->node.polygon.pts.count < 2) break; + auto pts = node->node.polygon.pts.begin(); + shape->moveTo(pts[0], pts[1]); + for (pts += 2; pts < node->node.polygon.pts.end(); pts += 2) { + shape->lineTo(pts[0], pts[1]); + } + shape->close(); + break; + } + case SvgNodeType::Polyline: { + if (node->node.polyline.pts.count < 2) break; + auto pts = node->node.polyline.pts.begin(); + shape->moveTo(pts[0], pts[1]); + for (pts += 2; pts < node->node.polyline.pts.end(); pts += 2) { + shape->lineTo(pts[0], pts[1]); + } + break; + } + case SvgNodeType::Circle: { + shape->appendCircle(node->node.circle.cx, node->node.circle.cy, node->node.circle.r, node->node.circle.r); + break; + } + case SvgNodeType::Rect: { + shape->appendRect(node->node.rect.x, node->node.rect.y, node->node.rect.w, node->node.rect.h, node->node.rect.rx, node->node.rect.ry); + break; + } + case SvgNodeType::Line: { + shape->moveTo(node->node.line.x1, node->node.line.y1); + shape->lineTo(node->node.line.x2, node->node.line.y2); + break; + } + default: { + return false; + } + } + return true; +} + + +static bool _appendShape(SvgLoaderData& loaderData, SvgNode* node, Shape* shape, const Box& vBox, const string& svgPath) +{ + if (!_recognizeShape(node, shape)) return false; + + _applyProperty(loaderData, node, shape, vBox, svgPath, false); + return true; +} + + +static bool _appendClipShape(SvgLoaderData& loaderData, SvgNode* node, Shape* shape, const Box& vBox, const string& svgPath, const Matrix* transform) +{ + //The 'transform' matrix has higher priority than the node->transform, since it already contains it + auto m = transform ? transform : (node->transform ? node->transform : nullptr); + + uint32_t currentPtsCnt = 0; + if (m) { + const Point *tmp = nullptr; + currentPtsCnt = shape->pathCoords(&tmp); + } + + if (!_recognizeShape(node, shape)) return false; + + if (m) { + const Point *pts = nullptr; + auto ptsCnt = shape->pathCoords(&pts); + + auto p = const_cast(pts) + currentPtsCnt; + while (currentPtsCnt++ < ptsCnt) mathMultiply(p++, m); + } + + _applyProperty(loaderData, node, shape, vBox, svgPath, true); + return true; +} + + +enum class imageMimeTypeEncoding +{ + base64 = 0x1, + utf8 = 0x2 +}; + +constexpr imageMimeTypeEncoding operator|(imageMimeTypeEncoding a, imageMimeTypeEncoding b) { + return static_cast(static_cast(a) | static_cast(b)); +} + +constexpr bool operator&(imageMimeTypeEncoding a, imageMimeTypeEncoding b) { + return (static_cast(a) & static_cast(b)); +} + + +static constexpr struct +{ + const char* name; + int sz; + imageMimeTypeEncoding encoding; +} imageMimeTypes[] = { + {"jpeg", sizeof("jpeg"), imageMimeTypeEncoding::base64}, + {"png", sizeof("png"), imageMimeTypeEncoding::base64}, + {"svg+xml", sizeof("svg+xml"), imageMimeTypeEncoding::base64 | imageMimeTypeEncoding::utf8}, +}; + + +static bool _isValidImageMimeTypeAndEncoding(const char** href, const char** mimetype, imageMimeTypeEncoding* encoding) { + if (strncmp(*href, "image/", sizeof("image/") - 1)) return false; //not allowed mime type + *href += sizeof("image/") - 1; + + //RFC2397 data:[][;base64], + //mediatype := [ type "/" subtype ] *( ";" parameter ) + //parameter := attribute "=" value + for (unsigned int i = 0; i < sizeof(imageMimeTypes) / sizeof(imageMimeTypes[0]); i++) { + if (!strncmp(*href, imageMimeTypes[i].name, imageMimeTypes[i].sz - 1)) { + *href += imageMimeTypes[i].sz - 1; + *mimetype = imageMimeTypes[i].name; + + while (**href && **href != ',') { + while (**href && **href != ';') ++(*href); + if (!**href) return false; + ++(*href); + + if (imageMimeTypes[i].encoding & imageMimeTypeEncoding::base64) { + if (!strncmp(*href, "base64,", sizeof("base64,") - 1)) { + *href += sizeof("base64,") - 1; + *encoding = imageMimeTypeEncoding::base64; + return true; //valid base64 + } + } + if (imageMimeTypes[i].encoding & imageMimeTypeEncoding::utf8) { + if (!strncmp(*href, "utf8,", sizeof("utf8,") - 1)) { + *href += sizeof("utf8,") - 1; + *encoding = imageMimeTypeEncoding::utf8; + return true; //valid utf8 + } + } + } + //no encoding defined + if (**href == ',' && (imageMimeTypes[i].encoding & imageMimeTypeEncoding::utf8)) { + ++(*href); + *encoding = imageMimeTypeEncoding::utf8; + return true; //allow no encoding defined if utf8 expected + } + return false; + } + } + return false; +} + +#include "tvgTaskScheduler.h" + +static unique_ptr _imageBuildHelper(SvgLoaderData& loaderData, SvgNode* node, const Box& vBox, const string& svgPath) +{ + if (!node->node.image.href || !strlen(node->node.image.href)) return nullptr; + auto picture = Picture::gen(); + + TaskScheduler::async(false); //force to load a picture on the same thread + + const char* href = node->node.image.href; + if (!strncmp(href, "data:", sizeof("data:") - 1)) { + href += sizeof("data:") - 1; + const char* mimetype; + imageMimeTypeEncoding encoding; + if (!_isValidImageMimeTypeAndEncoding(&href, &mimetype, &encoding)) return nullptr; //not allowed mime type or encoding + char *decoded = nullptr; + if (encoding == imageMimeTypeEncoding::base64) { + auto size = b64Decode(href, strlen(href), &decoded); + if (picture->load(decoded, size, mimetype, false) != Result::Success) { + free(decoded); + TaskScheduler::async(true); + return nullptr; + } + } else { + auto size = svgUtilURLDecode(href, &decoded); + if (picture->load(decoded, size, mimetype, false) != Result::Success) { + free(decoded); + TaskScheduler::async(true); + return nullptr; + } + } + loaderData.images.push(decoded); + } else { + if (!strncmp(href, "file://", sizeof("file://") - 1)) href += sizeof("file://") - 1; + //TODO: protect against recursive svg image loading + //Temporarily disable embedded svg: + const char *dot = strrchr(href, '.'); + if (dot && !strcmp(dot, ".svg")) { + TVGLOG("SVG", "Embedded svg file is disabled."); + TaskScheduler::async(true); + return nullptr; + } + string imagePath = href; + if (strncmp(href, "/", 1)) { + auto last = svgPath.find_last_of("/"); + imagePath = svgPath.substr(0, (last == string::npos ? 0 : last + 1)) + imagePath; + } + if (picture->load(imagePath) != Result::Success) { + TaskScheduler::async(true); + return nullptr; + } + } + + TaskScheduler::async(true); + + float w, h; + Matrix m = {1, 0, 0, 0, 1, 0, 0, 0, 1}; + if (picture->size(&w, &h) == Result::Success && w > 0 && h > 0) { + auto sx = node->node.image.w / w; + auto sy = node->node.image.h / h; + m = {sx, 0, node->node.image.x, 0, sy, node->node.image.y, 0, 0, 1}; + } + if (node->transform) m = mathMultiply(node->transform, &m); + picture->transform(m); + + _applyComposition(loaderData, picture.get(), node, vBox, svgPath); + + return picture; +} + + +static Matrix _calculateAspectRatioMatrix(AspectRatioAlign align, AspectRatioMeetOrSlice meetOrSlice, float width, float height, const Box& box) +{ + auto sx = width / box.w; + auto sy = height / box.h; + auto tvx = box.x * sx; + auto tvy = box.y * sy; + + if (align == AspectRatioAlign::None) + return {sx, 0, -tvx, 0, sy, -tvy, 0, 0, 1}; + + //Scale + if (meetOrSlice == AspectRatioMeetOrSlice::Meet) { + if (sx < sy) sy = sx; + else sx = sy; + } else { + if (sx < sy) sx = sy; + else sy = sx; + } + + //Align + tvx = box.x * sx; + tvy = box.y * sy; + auto tvw = box.w * sx; + auto tvh = box.h * sy; + + switch (align) { + case AspectRatioAlign::XMinYMin: { + break; + } + case AspectRatioAlign::XMidYMin: { + tvx -= (width - tvw) * 0.5f; + break; + } + case AspectRatioAlign::XMaxYMin: { + tvx -= width - tvw; + break; + } + case AspectRatioAlign::XMinYMid: { + tvy -= (height - tvh) * 0.5f; + break; + } + case AspectRatioAlign::XMidYMid: { + tvx -= (width - tvw) * 0.5f; + tvy -= (height - tvh) * 0.5f; + break; + } + case AspectRatioAlign::XMaxYMid: { + tvx -= width - tvw; + tvy -= (height - tvh) * 0.5f; + break; + } + case AspectRatioAlign::XMinYMax: { + tvy -= height - tvh; + break; + } + case AspectRatioAlign::XMidYMax: { + tvx -= (width - tvw) * 0.5f; + tvy -= height - tvh; + break; + } + case AspectRatioAlign::XMaxYMax: { + tvx -= width - tvw; + tvy -= height - tvh; + break; + } + default: { + break; + } + } + + return {sx, 0, -tvx, 0, sy, -tvy, 0, 0, 1}; +} + + +static unique_ptr _useBuildHelper(SvgLoaderData& loaderData, const SvgNode* node, const Box& vBox, const string& svgPath, int depth, bool* isMaskWhite) +{ + unique_ptr finalScene; + auto scene = _sceneBuildHelper(loaderData, node, vBox, svgPath, false, depth + 1, isMaskWhite); + + // mUseTransform = mUseTransform * mTranslate + Matrix mUseTransform = {1, 0, 0, 0, 1, 0, 0, 0, 1}; + if (node->transform) mUseTransform = *node->transform; + if (node->node.use.x != 0.0f || node->node.use.y != 0.0f) { + Matrix mTranslate = {1, 0, node->node.use.x, 0, 1, node->node.use.y, 0, 0, 1}; + mUseTransform = mathMultiply(&mUseTransform, &mTranslate); + } + + if (node->node.use.symbol) { + auto symbol = node->node.use.symbol->node.symbol; + + auto width = (symbol.hasWidth ? symbol.w : vBox.w); + if (node->node.use.isWidthSet) width = node->node.use.w; + auto height = (symbol.hasHeight ? symbol.h : vBox.h);; + if (node->node.use.isHeightSet) height = node->node.use.h; + auto vw = (symbol.hasViewBox ? symbol.vw : width); + auto vh = (symbol.hasViewBox ? symbol.vh : height); + + Matrix mViewBox = {1, 0, 0, 0, 1, 0, 0, 0, 1}; + if ((!mathEqual(width, vw) || !mathEqual(height, vh)) && vw > 0 && vh > 0) { + Box box = {symbol.vx, symbol.vy, vw, vh}; + mViewBox = _calculateAspectRatioMatrix(symbol.align, symbol.meetOrSlice, width, height, box); + } else if (!mathZero(symbol.vx) || !mathZero(symbol.vy)) { + mViewBox = {1, 0, -symbol.vx, 0, 1, -symbol.vy, 0, 0, 1}; + } + + // mSceneTransform = mUseTransform * mSymbolTransform * mViewBox + Matrix mSceneTransform = mViewBox; + if (node->node.use.symbol->transform) { + mSceneTransform = mathMultiply(node->node.use.symbol->transform, &mViewBox); + } + mSceneTransform = mathMultiply(&mUseTransform, &mSceneTransform); + scene->transform(mSceneTransform); + + if (node->node.use.symbol->node.symbol.overflowVisible) { + finalScene = std::move(scene); + } else { + auto viewBoxClip = Shape::gen(); + viewBoxClip->appendRect(0, 0, width, height, 0, 0); + + // mClipTransform = mUseTransform * mSymbolTransform + Matrix mClipTransform = mUseTransform; + if (node->node.use.symbol->transform) { + mClipTransform = mathMultiply(&mUseTransform, node->node.use.symbol->transform); + } + viewBoxClip->transform(mClipTransform); + + auto compositeLayer = Scene::gen(); + compositeLayer->composite(std::move(viewBoxClip), CompositeMethod::ClipPath); + compositeLayer->push(std::move(scene)); + + auto root = Scene::gen(); + root->push(std::move(compositeLayer)); + + finalScene = std::move(root); + } + } else { + scene->transform(mUseTransform); + finalScene = std::move(scene); + } + + return finalScene; +} + + +static unique_ptr _sceneBuildHelper(SvgLoaderData& loaderData, const SvgNode* node, const Box& vBox, const string& svgPath, bool mask, int depth, bool* isMaskWhite) +{ + /* Exception handling: Prevent invalid SVG data input. + The size is the arbitrary value, we need an experimental size. */ + if (depth > 2192) { + TVGERR("SVG", "Infinite recursive call - stopped after %d calls! Svg file may be incorrectly formatted.", depth); + return nullptr; + } + + if (_isGroupType(node->type) || mask) { + auto scene = Scene::gen(); + // For a Symbol node, the viewBox transformation has to be applied first - see _useBuildHelper() + if (!mask && node->transform && node->type != SvgNodeType::Symbol) scene->transform(*node->transform); + + if (node->style->display && node->style->opacity != 0) { + auto child = node->child.data; + for (uint32_t i = 0; i < node->child.count; ++i, ++child) { + if (_isGroupType((*child)->type)) { + if ((*child)->type == SvgNodeType::Use) + scene->push(_useBuildHelper(loaderData, *child, vBox, svgPath, depth + 1, isMaskWhite)); + else if (!((*child)->type == SvgNodeType::Symbol && node->type != SvgNodeType::Use)) + scene->push(_sceneBuildHelper(loaderData, *child, vBox, svgPath, false, depth + 1, isMaskWhite)); + } else if ((*child)->type == SvgNodeType::Image) { + auto image = _imageBuildHelper(loaderData, *child, vBox, svgPath); + if (image) { + scene->push(std::move(image)); + if (isMaskWhite) *isMaskWhite = false; + } + } else if ((*child)->type != SvgNodeType::Mask) { + auto shape = _shapeBuildHelper(loaderData, *child, vBox, svgPath); + if (shape) { + if (isMaskWhite) { + uint8_t r, g, b; + shape->fillColor(&r, &g, &b); + if (shape->fill() || r < 255 || g < 255 || b < 255 || shape->strokeFill() || + (shape->strokeColor(&r, &g, &b) == Result::Success && (r < 255 || g < 255 || b < 255))) { + *isMaskWhite = false; + } + } + scene->push(std::move(shape)); + } + } + } + _applyComposition(loaderData, scene.get(), node, vBox, svgPath); + scene->opacity(node->style->opacity); + } + return scene; + } + return nullptr; +} + + +static void _updateInvalidViewSize(const Scene* scene, Box& vBox, float& w, float& h, SvgViewFlag viewFlag) +{ + bool validWidth = (viewFlag & SvgViewFlag::Width); + bool validHeight = (viewFlag & SvgViewFlag::Height); + + float x, y; + scene->bounds(&x, &y, &vBox.w, &vBox.h, false); + if (!validWidth && !validHeight) { + vBox.x = x; + vBox.y = y; + } else { + if (validWidth) vBox.w = w; + if (validHeight) vBox.h = h; + } + + //the size would have 1x1 or percentage values. + if (!validWidth) w *= vBox.w; + if (!validHeight) h *= vBox.h; +} + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +Scene* svgSceneBuild(SvgLoaderData& loaderData, Box vBox, float w, float h, AspectRatioAlign align, AspectRatioMeetOrSlice meetOrSlice, const string& svgPath, SvgViewFlag viewFlag) +{ + //TODO: aspect ratio is valid only if viewBox was set + + if (!loaderData.doc || (loaderData.doc->type != SvgNodeType::Doc)) return nullptr; + + auto docNode = _sceneBuildHelper(loaderData, loaderData.doc, vBox, svgPath, false, 0); + + if (!(viewFlag & SvgViewFlag::Viewbox)) _updateInvalidViewSize(docNode.get(), vBox, w, h, viewFlag); + + if (!mathEqual(w, vBox.w) || !mathEqual(h, vBox.h)) { + Matrix m = _calculateAspectRatioMatrix(align, meetOrSlice, w, h, vBox); + docNode->transform(m); + } else if (!mathZero(vBox.x) || !mathZero(vBox.y)) { + docNode->translate(-vBox.x, -vBox.y); + } + + auto viewBoxClip = Shape::gen(); + viewBoxClip->appendRect(0, 0, w, h); + + auto compositeLayer = Scene::gen(); + compositeLayer->composite(std::move(viewBoxClip), CompositeMethod::ClipPath); + compositeLayer->push(std::move(docNode)); + + auto root = Scene::gen(); + root->push(std::move(compositeLayer)); + + loaderData.doc->node.doc.vx = vBox.x; + loaderData.doc->node.doc.vy = vBox.y; + loaderData.doc->node.doc.vw = vBox.w; + loaderData.doc->node.doc.vh = vBox.h; + loaderData.doc->node.doc.w = w; + loaderData.doc->node.doc.h = h; + + return root.release(); +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSvgSceneBuilder.h b/include/liblvgl/libs/thorvg/tvgSvgSceneBuilder.h new file mode 100644 index 00000000..8ab1b672 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSvgSceneBuilder.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_SVG_SCENE_BUILDER_H_ +#define _TVG_SVG_SCENE_BUILDER_H_ + +#include "tvgCommon.h" + +Scene* svgSceneBuild(SvgLoaderData& loaderData, Box vBox, float w, float h, AspectRatioAlign align, AspectRatioMeetOrSlice meetOrSlice, const string& svgPath, SvgViewFlag viewFlag); + +#endif //_TVG_SVG_SCENE_BUILDER_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSvgUtil.cpp b/include/liblvgl/libs/thorvg/tvgSvgUtil.cpp new file mode 100644 index 00000000..e77e2a0f --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSvgUtil.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include +#include "tvgSvgUtil.h" + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +static uint8_t _hexCharToDec(const char c) +{ + if (c >= 'a') return c - 'a' + 10; + else if (c >= 'A') return c - 'A' + 10; + else return c - '0'; +} + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +size_t svgUtilURLDecode(const char *src, char** dst) +{ + if (!src) return 0; + + auto length = strlen(src); + if (length == 0) return 0; + + char* decoded = (char*)malloc(sizeof(char) * length + 1); + + char a, b; + int idx =0; + while (*src) { + if (*src == '%' && + ((a = src[1]) && (b = src[2])) && + (isxdigit(a) && isxdigit(b))) { + decoded[idx++] = (_hexCharToDec(a) << 4) + _hexCharToDec(b); + src+=3; + } else if (*src == '+') { + decoded[idx++] = ' '; + src++; + } else { + decoded[idx++] = *src++; + } + } + decoded[idx] = '\0'; + + *dst = decoded; + return idx + 1; +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSvgUtil.h b/include/liblvgl/libs/thorvg/tvgSvgUtil.h new file mode 100644 index 00000000..cc4b54c0 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSvgUtil.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_SVG_UTIL_H_ +#define _TVG_SVG_UTIL_H_ + +#include "tvgCommon.h" + +size_t svgUtilURLDecode(const char *src, char** dst); + +#endif //_TVG_SVG_UTIL_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSwCanvas.cpp b/include/liblvgl/libs/thorvg/tvgSwCanvas.cpp new file mode 100644 index 00000000..27bd7e3f --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSwCanvas.cpp @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgCanvas.h" +#include "tvgLoadModule.h" + +#ifdef THORVG_SW_RASTER_SUPPORT + #include "tvgSwRenderer.h" +#else + class SwRenderer : public RenderMethod + { + //Non Supported. Dummy Class */ + }; +#endif + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +struct SwCanvas::Impl +{ +}; + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +#ifdef THORVG_SW_RASTER_SUPPORT +SwCanvas::SwCanvas() : Canvas(SwRenderer::gen()), pImpl(new Impl) +#else +SwCanvas::SwCanvas() : Canvas(nullptr), pImpl(new Impl) +#endif +{ +} + + +SwCanvas::~SwCanvas() +{ + delete(pImpl); +} + + +Result SwCanvas::mempool(MempoolPolicy policy) noexcept +{ +#ifdef THORVG_SW_RASTER_SUPPORT + //We know renderer type, avoid dynamic_cast for performance. + auto renderer = static_cast(Canvas::pImpl->renderer); + if (!renderer) return Result::MemoryCorruption; + + //It can't change the policy during the running. + if (!Canvas::pImpl->paints.empty()) return Result::InsufficientCondition; + + if (policy == MempoolPolicy::Individual) renderer->mempool(false); + else renderer->mempool(true); + + return Result::Success; +#endif + return Result::NonSupport; +} + + +Result SwCanvas::target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h, Colorspace cs) noexcept +{ +#ifdef THORVG_SW_RASTER_SUPPORT + //We know renderer type, avoid dynamic_cast for performance. + auto renderer = static_cast(Canvas::pImpl->renderer); + if (!renderer) return Result::MemoryCorruption; + + if (!renderer->target(buffer, stride, w, h, static_cast(cs))) return Result::InvalidArguments; + Canvas::pImpl->vport = {0, 0, (int32_t)w, (int32_t)h}; + renderer->viewport(Canvas::pImpl->vport); + + //Paints must be updated again with this new target. + Canvas::pImpl->needRefresh(); + + //FIXME: The value must be associated with an individual canvas instance. + ImageLoader::cs = static_cast(cs); + + return Result::Success; +#endif + return Result::NonSupport; +} + + +unique_ptr SwCanvas::gen() noexcept +{ +#ifdef THORVG_SW_RASTER_SUPPORT + if (SwRenderer::init() <= 0) return nullptr; + return unique_ptr(new SwCanvas); +#endif + return nullptr; +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSwCommon.h b/include/liblvgl/libs/thorvg/tvgSwCommon.h new file mode 100644 index 00000000..182cf8fa --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSwCommon.h @@ -0,0 +1,593 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_SW_COMMON_H_ +#define _TVG_SW_COMMON_H_ + +#include "tvgCommon.h" +#include "tvgRender.h" + +#include + +#if 0 +#include +static double timeStamp() +{ + struct timeval tv; + gettimeofday(&tv, NULL); + return (tv.tv_sec + tv.tv_usec / 1000000.0); +} +#endif + +#define SW_CURVE_TYPE_POINT 0 +#define SW_CURVE_TYPE_CUBIC 1 +#define SW_ANGLE_PI (180L << 16) +#define SW_ANGLE_2PI (SW_ANGLE_PI << 1) +#define SW_ANGLE_PI2 (SW_ANGLE_PI >> 1) + +using SwCoord = signed long; +using SwFixed = signed long long; + + +static inline float TO_FLOAT(SwCoord val) +{ + return static_cast(val) / 64.0f; +} + +struct SwPoint +{ + SwCoord x, y; + + SwPoint& operator+=(const SwPoint& rhs) + { + x += rhs.x; + y += rhs.y; + return *this; + } + + SwPoint operator+(const SwPoint& rhs) const + { + return {x + rhs.x, y + rhs.y}; + } + + SwPoint operator-(const SwPoint& rhs) const + { + return {x - rhs.x, y - rhs.y}; + } + + bool operator==(const SwPoint& rhs) const + { + return (x == rhs.x && y == rhs.y); + } + + bool operator!=(const SwPoint& rhs) const + { + return (x != rhs.x || y != rhs.y); + } + + bool zero() const + { + if (x == 0 && y == 0) return true; + else return false; + } + + bool small() const + { + //2 is epsilon... + if (abs(x) < 2 && abs(y) < 2) return true; + else return false; + } + + Point toPoint() const + { + return {TO_FLOAT(x), TO_FLOAT(y)}; + } +}; + +struct SwSize +{ + SwCoord w, h; +}; + +struct SwOutline +{ + Array pts; //the outline's points + Array cntrs; //the contour end points + Array types; //curve type + Array closed; //opened or closed path? + FillRule fillRule; +}; + +struct SwSpan +{ + uint16_t x, y; + uint16_t len; + uint8_t coverage; +}; + +struct SwRleData +{ + SwSpan *spans; + uint32_t alloc; + uint32_t size; +}; + +struct SwBBox +{ + SwPoint min, max; + + void reset() + { + min.x = min.y = max.x = max.y = 0; + } +}; + +struct SwFill +{ + struct SwLinear { + float dx, dy; + float len; + float offset; + }; + + struct SwRadial { + float a11, a12, a13; + float a21, a22, a23; + float fx, fy, fr; + float dx, dy, dr; + float invA, a; + }; + + union { + SwLinear linear; + SwRadial radial; + }; + + uint32_t* ctable; + FillSpread spread; + + bool translucent; +}; + +struct SwStrokeBorder +{ + uint32_t ptsCnt; + uint32_t maxPts; + SwPoint* pts; + uint8_t* tags; + int32_t start; //index of current sub-path start point + bool movable; //true: for ends of lineto borders +}; + +struct SwStroke +{ + SwFixed angleIn; + SwFixed angleOut; + SwPoint center; + SwFixed lineLength; + SwFixed subPathAngle; + SwPoint ptStartSubPath; + SwFixed subPathLineLength; + SwFixed width; + SwFixed miterlimit; + + StrokeCap cap; + StrokeJoin join; + StrokeJoin joinSaved; + SwFill* fill = nullptr; + + SwStrokeBorder borders[2]; + + float sx, sy; + + bool firstPt; + bool closedSubPath; + bool handleWideStrokes; +}; + +struct SwDashStroke +{ + SwOutline* outline = nullptr; + float curLen = 0; + int32_t curIdx = 0; + Point ptStart = {0, 0}; + Point ptCur = {0, 0}; + float* pattern = nullptr; + uint32_t cnt = 0; + bool curOpGap = false; + bool move = true; +}; + +struct SwShape +{ + SwOutline* outline = nullptr; + SwStroke* stroke = nullptr; + SwFill* fill = nullptr; + SwRleData* rle = nullptr; + SwRleData* strokeRle = nullptr; + SwBBox bbox; //Keep it boundary without stroke region. Using for optimal filling. + + bool fastTrack = false; //Fast Track: axis-aligned rectangle without any clips? +}; + +struct SwImage +{ + SwOutline* outline = nullptr; + SwRleData* rle = nullptr; + union { + pixel_t* data; //system based data pointer + uint32_t* buf32; //for explicit 32bits channels + uint8_t* buf8; //for explicit 8bits grayscale + }; + uint32_t w, h, stride; + int32_t ox = 0; //offset x + int32_t oy = 0; //offset y + float scale; + uint8_t channelSize; + + bool direct = false; //draw image directly (with offset) + bool scaled = false; //draw scaled image +}; + +typedef uint8_t(*SwMask)(uint8_t s, uint8_t d, uint8_t a); //src, dst, alpha +typedef uint32_t(*SwBlender)(uint32_t s, uint32_t d, uint8_t a); //src, dst, alpha +typedef uint32_t(*SwJoin)(uint8_t r, uint8_t g, uint8_t b, uint8_t a); //color channel join +typedef uint8_t(*SwAlpha)(uint8_t*); //blending alpha + +struct SwCompositor; + +struct SwSurface : Surface +{ + SwJoin join; + SwAlpha alphas[4]; //Alpha:2, InvAlpha:3, Luma:4, InvLuma:5 + SwBlender blender = nullptr; //blender (optional) + SwCompositor* compositor = nullptr; //compositor (optional) + BlendMethod blendMethod; //blending method (uint8_t) + + SwAlpha alpha(CompositeMethod method) + { + auto idx = (int)(method) - 2; //0: None, 1: ClipPath + return alphas[idx > 3 ? 0 : idx]; //CompositeMethod has only four Matting methods. + } + + SwSurface() + { + } + + SwSurface(const SwSurface* rhs) : Surface(rhs) + { + join = rhs->join; + memcpy(alphas, rhs->alphas, sizeof(alphas)); + blender = rhs->blender; + compositor = rhs->compositor; + blendMethod = rhs->blendMethod; + } +}; + +struct SwCompositor : Compositor +{ + SwSurface* recoverSfc; //Recover surface when composition is started + SwCompositor* recoverCmp; //Recover compositor when composition is done + SwImage image; + SwBBox bbox; + bool valid; +}; + +struct SwMpool +{ + SwOutline* outline; + SwOutline* strokeOutline; + SwOutline* dashOutline; + unsigned allocSize; +}; + +static inline SwCoord TO_SWCOORD(float val) +{ + return SwCoord(val * 64.0f); +} + +static inline uint32_t JOIN(uint8_t c0, uint8_t c1, uint8_t c2, uint8_t c3) +{ + return (c0 << 24 | c1 << 16 | c2 << 8 | c3); +} + +static inline uint32_t ALPHA_BLEND(uint32_t c, uint32_t a) +{ + return (((((c >> 8) & 0x00ff00ff) * a + 0x00ff00ff) & 0xff00ff00) + + ((((c & 0x00ff00ff) * a + 0x00ff00ff) >> 8) & 0x00ff00ff)); +} + +static inline uint32_t INTERPOLATE(uint32_t s, uint32_t d, uint8_t a) +{ + return (((((((s >> 8) & 0xff00ff) - ((d >> 8) & 0xff00ff)) * a) + (d & 0xff00ff00)) & 0xff00ff00) + ((((((s & 0xff00ff) - (d & 0xff00ff)) * a) >> 8) + (d & 0xff00ff)) & 0xff00ff)); +} + +static inline uint8_t INTERPOLATE8(uint8_t s, uint8_t d, uint8_t a) +{ + return (((s) * (a) + 0xff) >> 8) + (((d) * ~(a) + 0xff) >> 8); +} + +static inline SwCoord HALF_STROKE(float width) +{ + return TO_SWCOORD(width * 0.5f); +} + +static inline uint8_t A(uint32_t c) +{ + return ((c) >> 24); +} + +static inline uint8_t IA(uint32_t c) +{ + return (~(c) >> 24); +} + +static inline uint8_t C1(uint32_t c) +{ + return ((c) >> 16); +} + +static inline uint8_t C2(uint32_t c) +{ + return ((c) >> 8); +} + +static inline uint8_t C3(uint32_t c) +{ + return (c); +} + +static inline uint32_t opBlendInterp(uint32_t s, uint32_t d, uint8_t a) +{ + return INTERPOLATE(s, d, a); +} + +static inline uint32_t opBlendNormal(uint32_t s, uint32_t d, uint8_t a) +{ + auto t = ALPHA_BLEND(s, a); + return t + ALPHA_BLEND(d, IA(t)); +} + +static inline uint32_t opBlendPreNormal(uint32_t s, uint32_t d, TVG_UNUSED uint8_t a) +{ + return s + ALPHA_BLEND(d, IA(s)); +} + +static inline uint32_t opBlendSrcOver(uint32_t s, TVG_UNUSED uint32_t d, TVG_UNUSED uint8_t a) +{ + return s; +} + +//TODO: BlendMethod could remove the alpha parameter. +static inline uint32_t opBlendDifference(uint32_t s, uint32_t d, TVG_UNUSED uint8_t a) +{ + //if (s > d) => s - d + //else => d - s + auto c1 = (C1(s) > C1(d)) ? (C1(s) - C1(d)) : (C1(d) - C1(s)); + auto c2 = (C2(s) > C2(d)) ? (C2(s) - C2(d)) : (C2(d) - C2(s)); + auto c3 = (C3(s) > C3(d)) ? (C3(s) - C3(d)) : (C3(d) - C3(s)); + return JOIN(255, c1, c2, c3); +} + +static inline uint32_t opBlendExclusion(uint32_t s, uint32_t d, TVG_UNUSED uint8_t a) +{ + //A + B - 2AB + auto c1 = std::min(255, C1(s) + C1(d) - std::min(255, (C1(s) * C1(d)) << 1)); + auto c2 = std::min(255, C2(s) + C2(d) - std::min(255, (C2(s) * C2(d)) << 1)); + auto c3 = std::min(255, C3(s) + C3(d) - std::min(255, (C3(s) * C3(d)) << 1)); + return JOIN(255, c1, c2, c3); +} + +static inline uint32_t opBlendAdd(uint32_t s, uint32_t d, TVG_UNUSED uint8_t a) +{ + // s + d + auto c1 = std::min(C1(s) + C1(d), 255); + auto c2 = std::min(C2(s) + C2(d), 255); + auto c3 = std::min(C3(s) + C3(d), 255); + return JOIN(255, c1, c2, c3); +} + +static inline uint32_t opBlendScreen(uint32_t s, uint32_t d, TVG_UNUSED uint8_t a) +{ + // s + d - s * d + auto c1 = C1(s) + C1(d) - MULTIPLY(C1(s), C1(d)); + auto c2 = C2(s) + C2(d) - MULTIPLY(C2(s), C2(d)); + auto c3 = C3(s) + C3(d) - MULTIPLY(C3(s), C3(d)); + return JOIN(255, c1, c2, c3); +} + + +static inline uint32_t opBlendMultiply(uint32_t s, uint32_t d, TVG_UNUSED uint8_t a) +{ + // s * d + auto c1 = MULTIPLY(C1(s), C1(d)); + auto c2 = MULTIPLY(C2(s), C2(d)); + auto c3 = MULTIPLY(C3(s), C3(d)); + return JOIN(255, c1, c2, c3); +} + + +static inline uint32_t opBlendOverlay(uint32_t s, uint32_t d, TVG_UNUSED uint8_t a) +{ + // if (2 * d < da) => 2 * s * d, + // else => 1 - 2 * (1 - s) * (1 - d) + auto c1 = (C1(d) < 128) ? std::min(255, 2 * MULTIPLY(C1(s), C1(d))) : (255 - std::min(255, 2 * MULTIPLY(255 - C1(s), 255 - C1(d)))); + auto c2 = (C2(d) < 128) ? std::min(255, 2 * MULTIPLY(C2(s), C2(d))) : (255 - std::min(255, 2 * MULTIPLY(255 - C2(s), 255 - C2(d)))); + auto c3 = (C3(d) < 128) ? std::min(255, 2 * MULTIPLY(C3(s), C3(d))) : (255 - std::min(255, 2 * MULTIPLY(255 - C3(s), 255 - C3(d)))); + return JOIN(255, c1, c2, c3); +} + +static inline uint32_t opBlendDarken(uint32_t s, uint32_t d, TVG_UNUSED uint8_t a) +{ + // min(s, d) + auto c1 = std::min(C1(s), C1(d)); + auto c2 = std::min(C2(s), C2(d)); + auto c3 = std::min(C3(s), C3(d)); + return JOIN(255, c1, c2, c3); +} + +static inline uint32_t opBlendLighten(uint32_t s, uint32_t d, TVG_UNUSED uint8_t a) +{ + // max(s, d) + auto c1 = std::max(C1(s), C1(d)); + auto c2 = std::max(C2(s), C2(d)); + auto c3 = std::max(C3(s), C3(d)); + return JOIN(255, c1, c2, c3); +} + +static inline uint32_t opBlendColorDodge(uint32_t s, uint32_t d, TVG_UNUSED uint8_t a) +{ + // d / (1 - s) + auto is = 0xffffffff - s; + auto c1 = (C1(is) > 0) ? (C1(d) / C1(is)) : C1(d); + auto c2 = (C2(is) > 0) ? (C2(d) / C2(is)) : C2(d); + auto c3 = (C3(is) > 0) ? (C3(d) / C3(is)) : C3(d); + return JOIN(255, c1, c2, c3); +} + +static inline uint32_t opBlendColorBurn(uint32_t s, uint32_t d, TVG_UNUSED uint8_t a) +{ + // 1 - (1 - d) / s + auto id = 0xffffffff - d; + auto c1 = 255 - ((C1(s) > 0) ? (C1(id) / C1(s)) : C1(id)); + auto c2 = 255 - ((C2(s) > 0) ? (C2(id) / C2(s)) : C2(id)); + auto c3 = 255 - ((C3(s) > 0) ? (C3(id) / C3(s)) : C3(id)); + return JOIN(255, c1, c2, c3); +} + +static inline uint32_t opBlendHardLight(uint32_t s, uint32_t d, TVG_UNUSED uint8_t a) +{ + auto c1 = (C1(s) < 128) ? std::min(255, 2 * MULTIPLY(C1(s), C1(d))) : (255 - std::min(255, 2 * MULTIPLY(255 - C1(s), 255 - C1(d)))); + auto c2 = (C2(s) < 128) ? std::min(255, 2 * MULTIPLY(C2(s), C2(d))) : (255 - std::min(255, 2 * MULTIPLY(255 - C2(s), 255 - C2(d)))); + auto c3 = (C3(s) < 128) ? std::min(255, 2 * MULTIPLY(C3(s), C3(d))) : (255 - std::min(255, 2 * MULTIPLY(255 - C3(s), 255 - C3(d)))); + return JOIN(255, c1, c2, c3); +} + +static inline uint32_t opBlendSoftLight(uint32_t s, uint32_t d, TVG_UNUSED uint8_t a) +{ + //(255 - 2 * s) * (d * d) + (2 * s * b) + auto c1 = std::min(255, MULTIPLY(255 - std::min(255, 2 * C1(s)), MULTIPLY(C1(d), C1(d))) + 2 * MULTIPLY(C1(s), C1(d))); + auto c2 = std::min(255, MULTIPLY(255 - std::min(255, 2 * C2(s)), MULTIPLY(C2(d), C2(d))) + 2 * MULTIPLY(C2(s), C2(d))); + auto c3 = std::min(255, MULTIPLY(255 - std::min(255, 2 * C3(s)), MULTIPLY(C3(d), C3(d))) + 2 * MULTIPLY(C3(s), C3(d))); + return JOIN(255, c1, c2, c3); +} + + +int64_t mathMultiply(int64_t a, int64_t b); +int64_t mathDivide(int64_t a, int64_t b); +int64_t mathMulDiv(int64_t a, int64_t b, int64_t c); +void mathRotate(SwPoint& pt, SwFixed angle); +SwFixed mathTan(SwFixed angle); +SwFixed mathAtan(const SwPoint& pt); +SwFixed mathCos(SwFixed angle); +SwFixed mathSin(SwFixed angle); +void mathSplitCubic(SwPoint* base); +SwFixed mathDiff(SwFixed angle1, SwFixed angle2); +SwFixed mathLength(const SwPoint& pt); +bool mathSmallCubic(const SwPoint* base, SwFixed& angleIn, SwFixed& angleMid, SwFixed& angleOut); +SwFixed mathMean(SwFixed angle1, SwFixed angle2); +SwPoint mathTransform(const Point* to, const Matrix* transform); +bool mathUpdateOutlineBBox(const SwOutline* outline, const SwBBox& clipRegion, SwBBox& renderRegion, bool fastTrack); +bool mathClipBBox(const SwBBox& clipper, SwBBox& clipee); + +void shapeReset(SwShape* shape); +bool shapePrepare(SwShape* shape, const RenderShape* rshape, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid, bool hasComposite); +bool shapePrepared(const SwShape* shape); +bool shapeGenRle(SwShape* shape, const RenderShape* rshape, bool antiAlias); +void shapeDelOutline(SwShape* shape, SwMpool* mpool, uint32_t tid); +void shapeResetStroke(SwShape* shape, const RenderShape* rshape, const Matrix* transform); +bool shapeGenStrokeRle(SwShape* shape, const RenderShape* rshape, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid); +void shapeFree(SwShape* shape); +void shapeDelStroke(SwShape* shape); +bool shapeGenFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint8_t opacity, bool ctable); +bool shapeGenStrokeFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint8_t opacity, bool ctable); +void shapeResetFill(SwShape* shape); +void shapeResetStrokeFill(SwShape* shape); +void shapeDelFill(SwShape* shape); +void shapeDelStrokeFill(SwShape* shape); + +void strokeReset(SwStroke* stroke, const RenderShape* shape, const Matrix* transform); +bool strokeParseOutline(SwStroke* stroke, const SwOutline& outline); +SwOutline* strokeExportOutline(SwStroke* stroke, SwMpool* mpool, unsigned tid); +void strokeFree(SwStroke* stroke); + +bool imagePrepare(SwImage* image, const RenderMesh* mesh, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid); +bool imageGenRle(SwImage* image, const SwBBox& renderRegion, bool antiAlias); +void imageDelOutline(SwImage* image, SwMpool* mpool, uint32_t tid); +void imageReset(SwImage* image); +void imageFree(SwImage* image); + +bool fillGenColorTable(SwFill* fill, const Fill* fdata, const Matrix* transform, SwSurface* surface, uint8_t opacity, bool ctable); +void fillReset(SwFill* fill); +void fillFree(SwFill* fill); + +//OPTIMIZE_ME: Skip the function pointer access +void fillLinear(const SwFill* fill, uint8_t* dst, uint32_t y, uint32_t x, uint32_t len, SwMask maskOp, uint8_t opacity); //composite masking ver. +void fillLinear(const SwFill* fill, uint8_t* dst, uint32_t y, uint32_t x, uint32_t len, uint8_t* cmp, SwMask maskOp, uint8_t opacity); //direct masking ver. +void fillLinear(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, SwBlender op, uint8_t a); //blending ver. +void fillLinear(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, SwBlender op, SwBlender op2, uint8_t a); //blending + BlendingMethod(op2) ver. +void fillLinear(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, uint8_t* cmp, SwAlpha alpha, uint8_t csize, uint8_t opacity); //matting ver. + +void fillRadial(const SwFill* fill, uint8_t* dst, uint32_t y, uint32_t x, uint32_t len, SwMask op, uint8_t a); //composite masking ver. +void fillRadial(const SwFill* fill, uint8_t* dst, uint32_t y, uint32_t x, uint32_t len, uint8_t* cmp, SwMask op, uint8_t a) ; //direct masking ver. +void fillRadial(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, SwBlender op, uint8_t a); //blending ver. +void fillRadial(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, SwBlender op, SwBlender op2, uint8_t a); //blending + BlendingMethod(op2) ver. +void fillRadial(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, uint8_t* cmp, SwAlpha alpha, uint8_t csize, uint8_t opacity); //matting ver. + +SwRleData* rleRender(SwRleData* rle, const SwOutline* outline, const SwBBox& renderRegion, bool antiAlias); +SwRleData* rleRender(const SwBBox* bbox); +void rleFree(SwRleData* rle); +void rleReset(SwRleData* rle); +void rleMerge(SwRleData* rle, SwRleData* clip1, SwRleData* clip2); +void rleClipPath(SwRleData* rle, const SwRleData* clip); +void rleClipRect(SwRleData* rle, const SwBBox* clip); + +SwMpool* mpoolInit(uint32_t threads); +bool mpoolTerm(SwMpool* mpool); +bool mpoolClear(SwMpool* mpool); +SwOutline* mpoolReqOutline(SwMpool* mpool, unsigned idx); +void mpoolRetOutline(SwMpool* mpool, unsigned idx); +SwOutline* mpoolReqStrokeOutline(SwMpool* mpool, unsigned idx); +void mpoolRetStrokeOutline(SwMpool* mpool, unsigned idx); +SwOutline* mpoolReqDashOutline(SwMpool* mpool, unsigned idx); +void mpoolRetDashOutline(SwMpool* mpool, unsigned idx); + +bool rasterCompositor(SwSurface* surface); +bool rasterGradientShape(SwSurface* surface, SwShape* shape, unsigned id); +bool rasterShape(SwSurface* surface, SwShape* shape, uint8_t r, uint8_t g, uint8_t b, uint8_t a); +bool rasterImage(SwSurface* surface, SwImage* image, const RenderMesh* mesh, const Matrix* transform, const SwBBox& bbox, uint8_t opacity); +bool rasterStroke(SwSurface* surface, SwShape* shape, uint8_t r, uint8_t g, uint8_t b, uint8_t a); +bool rasterGradientStroke(SwSurface* surface, SwShape* shape, unsigned id); +bool rasterClear(SwSurface* surface, uint32_t x, uint32_t y, uint32_t w, uint32_t h); +void rasterPixel32(uint32_t *dst, uint32_t val, uint32_t offset, int32_t len); +void rasterGrayscale8(uint8_t *dst, uint8_t val, uint32_t offset, int32_t len); +void rasterUnpremultiply(Surface* surface); +void rasterPremultiply(Surface* surface); +bool rasterConvertCS(Surface* surface, ColorSpace to); + +#endif /* _TVG_SW_COMMON_H_ */ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSwFill.cpp b/include/liblvgl/libs/thorvg/tvgSwFill.cpp new file mode 100644 index 00000000..f939fc8b --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSwFill.cpp @@ -0,0 +1,790 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgMath.h" +#include "tvgSwCommon.h" +#include "tvgFill.h" + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +#define RADIAL_A_THRESHOLD 0.0005f +#define GRADIENT_STOP_SIZE 1024 +#define FIXPT_BITS 8 +#define FIXPT_SIZE (1<radial.a + * B = 2 * (dr * fr + rx * dx + ry * dy) + * C = fr^2 - rx^2 - ry^2 + * Derivatives are computed with respect to dx. + * This procedure aims to optimize and eliminate the need to calculate all values from the beginning + * for consecutive x values with a constant y. The Taylor series expansions are computed as long as + * its terms are non-zero. + */ +static void _calculateCoefficients(const SwFill* fill, uint32_t x, uint32_t y, float& b, float& deltaB, float& det, float& deltaDet, float& deltaDeltaDet) +{ + auto radial = &fill->radial; + + auto rx = (x + 0.5f) * radial->a11 + (y + 0.5f) * radial->a12 + radial->a13 - radial->fx; + auto ry = (x + 0.5f) * radial->a21 + (y + 0.5f) * radial->a22 + radial->a23 - radial->fy; + + b = (radial->dr * radial->fr + rx * radial->dx + ry * radial->dy) * radial->invA; + deltaB = (radial->a11 * radial->dx + radial->a21 * radial->dy) * radial->invA; + + auto rr = rx * rx + ry * ry; + auto deltaRr = 2.0f * (rx * radial->a11 + ry * radial->a21) * radial->invA; + auto deltaDeltaRr = 2.0f * (radial->a11 * radial->a11 + radial->a21 * radial->a21) * radial->invA; + + det = b * b + (rr - radial->fr * radial->fr) * radial->invA; + deltaDet = 2.0f * b * deltaB + deltaB * deltaB + deltaRr + deltaDeltaRr; + deltaDeltaDet = 2.0f * deltaB * deltaB + deltaDeltaRr; +} + + +static bool _updateColorTable(SwFill* fill, const Fill* fdata, const SwSurface* surface, uint8_t opacity) +{ + if (!fill->ctable) { + fill->ctable = static_cast(malloc(GRADIENT_STOP_SIZE * sizeof(uint32_t))); + if (!fill->ctable) return false; + } + + const Fill::ColorStop* colors; + auto cnt = fdata->colorStops(&colors); + if (cnt == 0 || !colors) return false; + + auto pColors = colors; + + auto a = MULTIPLY(pColors->a, opacity); + if (a < 255) fill->translucent = true; + + auto r = pColors->r; + auto g = pColors->g; + auto b = pColors->b; + auto rgba = surface->join(r, g, b, a); + + auto inc = 1.0f / static_cast(GRADIENT_STOP_SIZE); + auto pos = 1.5f * inc; + uint32_t i = 0; + + fill->ctable[i++] = ALPHA_BLEND(rgba | 0xff000000, a); + + while (pos <= pColors->offset) { + fill->ctable[i] = fill->ctable[i - 1]; + ++i; + pos += inc; + } + + for (uint32_t j = 0; j < cnt - 1; ++j) { + auto curr = colors + j; + auto next = curr + 1; + auto delta = 1.0f / (next->offset - curr->offset); + auto a2 = MULTIPLY(next->a, opacity); + if (!fill->translucent && a2 < 255) fill->translucent = true; + + auto rgba2 = surface->join(next->r, next->g, next->b, a2); + + while (pos < next->offset && i < GRADIENT_STOP_SIZE) { + auto t = (pos - curr->offset) * delta; + auto dist = static_cast(255 * t); + auto dist2 = 255 - dist; + + auto color = INTERPOLATE(rgba, rgba2, dist2); + fill->ctable[i] = ALPHA_BLEND((color | 0xff000000), (color >> 24)); + + ++i; + pos += inc; + } + rgba = rgba2; + a = a2; + } + rgba = ALPHA_BLEND((rgba | 0xff000000), a); + + for (; i < GRADIENT_STOP_SIZE; ++i) + fill->ctable[i] = rgba; + + //Make sure the last color stop is represented at the end of the table + fill->ctable[GRADIENT_STOP_SIZE - 1] = rgba; + + return true; +} + + +bool _prepareLinear(SwFill* fill, const LinearGradient* linear, const Matrix* transform) +{ + float x1, x2, y1, y2; + if (linear->linear(&x1, &y1, &x2, &y2) != Result::Success) return false; + + fill->linear.dx = x2 - x1; + fill->linear.dy = y2 - y1; + fill->linear.len = fill->linear.dx * fill->linear.dx + fill->linear.dy * fill->linear.dy; + + if (fill->linear.len < FLOAT_EPSILON) return true; + + fill->linear.dx /= fill->linear.len; + fill->linear.dy /= fill->linear.len; + fill->linear.offset = -fill->linear.dx * x1 - fill->linear.dy * y1; + + auto gradTransform = linear->transform(); + bool isTransformation = !mathIdentity((const Matrix*)(&gradTransform)); + + if (isTransformation) { + if (transform) gradTransform = mathMultiply(transform, &gradTransform); + } else if (transform) { + gradTransform = *transform; + isTransformation = true; + } + + if (isTransformation) { + Matrix invTransform; + if (!mathInverse(&gradTransform, &invTransform)) return false; + + fill->linear.offset += fill->linear.dx * invTransform.e13 + fill->linear.dy * invTransform.e23; + + auto dx = fill->linear.dx; + fill->linear.dx = dx * invTransform.e11 + fill->linear.dy * invTransform.e21; + fill->linear.dy = dx * invTransform.e12 + fill->linear.dy * invTransform.e22; + + fill->linear.len = fill->linear.dx * fill->linear.dx + fill->linear.dy * fill->linear.dy; + } + + return true; +} + + +bool _prepareRadial(SwFill* fill, const RadialGradient* radial, const Matrix* transform) +{ + auto cx = P(radial)->cx; + auto cy = P(radial)->cy; + auto r = P(radial)->r; + auto fx = P(radial)->fx; + auto fy = P(radial)->fy; + auto fr = P(radial)->fr; + + if (r < FLOAT_EPSILON) return true; + + fill->radial.dr = r - fr; + fill->radial.dx = cx - fx; + fill->radial.dy = cy - fy; + fill->radial.fr = fr; + fill->radial.fx = fx; + fill->radial.fy = fy; + fill->radial.a = fill->radial.dr * fill->radial.dr - fill->radial.dx * fill->radial.dx - fill->radial.dy * fill->radial.dy; + + //This condition fulfills the SVG 1.1 std: + //the focal point, if outside the end circle, is moved to be on the end circle + //See: the SVG 2 std requirements: https://www.w3.org/TR/SVG2/pservers.html#RadialGradientNotes + if (fill->radial.a < 0) { + auto dist = sqrtf(fill->radial.dx * fill->radial.dx + fill->radial.dy * fill->radial.dy); + fill->radial.fx = cx + r * (fx - cx) / dist; + fill->radial.fy = cy + r * (fy - cy) / dist; + fill->radial.dx = cx - fill->radial.fx; + fill->radial.dy = cy - fill->radial.fy; + // Prevent loss of precision on Apple Silicon when dr=dy and dx=0 due to FMA + // https://github.com/thorvg/thorvg/issues/2014 + auto dr2 = fill->radial.dr * fill->radial.dr; + auto dx2 = fill->radial.dx * fill->radial.dx; + auto dy2 = fill->radial.dy * fill->radial.dy; + + fill->radial.a = dr2 - dx2 - dy2; + } + + if (fill->radial.a > 0) fill->radial.invA = 1.0f / fill->radial.a; + + auto gradTransform = radial->transform(); + bool isTransformation = !mathIdentity((const Matrix*)(&gradTransform)); + + if (transform) { + if (isTransformation) gradTransform = mathMultiply(transform, &gradTransform); + else { + gradTransform = *transform; + isTransformation = true; + } + } + + if (isTransformation) { + Matrix invTransform; + if (!mathInverse(&gradTransform, &invTransform)) return false; + fill->radial.a11 = invTransform.e11; + fill->radial.a12 = invTransform.e12; + fill->radial.a13 = invTransform.e13; + fill->radial.a21 = invTransform.e21; + fill->radial.a22 = invTransform.e22; + fill->radial.a23 = invTransform.e23; + } else { + fill->radial.a11 = fill->radial.a22 = 1.0f; + fill->radial.a12 = fill->radial.a13 = 0.0f; + fill->radial.a21 = fill->radial.a23 = 0.0f; + } + return true; +} + + +static inline uint32_t _clamp(const SwFill* fill, int32_t pos) +{ + switch (fill->spread) { + case FillSpread::Pad: { + if (pos >= GRADIENT_STOP_SIZE) pos = GRADIENT_STOP_SIZE - 1; + else if (pos < 0) pos = 0; + break; + } + case FillSpread::Repeat: { + pos = pos % GRADIENT_STOP_SIZE; + if (pos < 0) pos = GRADIENT_STOP_SIZE + pos; + break; + } + case FillSpread::Reflect: { + auto limit = GRADIENT_STOP_SIZE * 2; + pos = pos % limit; + if (pos < 0) pos = limit + pos; + if (pos >= GRADIENT_STOP_SIZE) pos = (limit - pos - 1); + break; + } + } + return pos; +} + + +static inline uint32_t _fixedPixel(const SwFill* fill, int32_t pos) +{ + int32_t i = (pos + (FIXPT_SIZE / 2)) >> FIXPT_BITS; + return fill->ctable[_clamp(fill, i)]; +} + + +static inline uint32_t _pixel(const SwFill* fill, float pos) +{ + auto i = static_cast(pos * (GRADIENT_STOP_SIZE - 1) + 0.5f); + return fill->ctable[_clamp(fill, i)]; +} + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + + +void fillRadial(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, uint8_t* cmp, SwAlpha alpha, uint8_t csize, uint8_t opacity) +{ + //edge case + if (fill->radial.a < RADIAL_A_THRESHOLD) { + auto radial = &fill->radial; + auto rx = (x + 0.5f) * radial->a11 + (y + 0.5f) * radial->a12 + radial->a13 - radial->fx; + auto ry = (x + 0.5f) * radial->a21 + (y + 0.5f) * radial->a22 + radial->a23 - radial->fy; + + if (opacity == 255) { + for (uint32_t i = 0 ; i < len ; ++i, ++dst, cmp += csize) { + auto x0 = 0.5f * (rx * rx + ry * ry - radial->fr * radial->fr) / (radial->dr * radial->fr + rx * radial->dx + ry * radial->dy); + *dst = opBlendNormal(_pixel(fill, x0), *dst, alpha(cmp)); + rx += radial->a11; + ry += radial->a21; + } + } else { + for (uint32_t i = 0 ; i < len ; ++i, ++dst, cmp += csize) { + auto x0 = 0.5f * (rx * rx + ry * ry - radial->fr * radial->fr) / (radial->dr * radial->fr + rx * radial->dx + ry * radial->dy); + *dst = opBlendNormal(_pixel(fill, x0), *dst, MULTIPLY(opacity, alpha(cmp))); + rx += radial->a11; + ry += radial->a21; + } + } + } else { + float b, deltaB, det, deltaDet, deltaDeltaDet; + _calculateCoefficients(fill, x, y, b, deltaB, det, deltaDet, deltaDeltaDet); + + if (opacity == 255) { + for (uint32_t i = 0 ; i < len ; ++i, ++dst, cmp += csize) { + *dst = opBlendNormal(_pixel(fill, sqrtf(det) - b), *dst, alpha(cmp)); + det += deltaDet; + deltaDet += deltaDeltaDet; + b += deltaB; + } + } else { + for (uint32_t i = 0 ; i < len ; ++i, ++dst, cmp += csize) { + *dst = opBlendNormal(_pixel(fill, sqrtf(det) - b), *dst, MULTIPLY(opacity, alpha(cmp))); + det += deltaDet; + deltaDet += deltaDeltaDet; + b += deltaB; + } + } + } +} + + +void fillRadial(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, SwBlender op, uint8_t a) +{ + if (fill->radial.a < RADIAL_A_THRESHOLD) { + auto radial = &fill->radial; + auto rx = (x + 0.5f) * radial->a11 + (y + 0.5f) * radial->a12 + radial->a13 - radial->fx; + auto ry = (x + 0.5f) * radial->a21 + (y + 0.5f) * radial->a22 + radial->a23 - radial->fy; + for (uint32_t i = 0; i < len; ++i, ++dst) { + auto x0 = 0.5f * (rx * rx + ry * ry - radial->fr * radial->fr) / (radial->dr * radial->fr + rx * radial->dx + ry * radial->dy); + *dst = op(_pixel(fill, x0), *dst, a); + rx += radial->a11; + ry += radial->a21; + } + } else { + float b, deltaB, det, deltaDet, deltaDeltaDet; + _calculateCoefficients(fill, x, y, b, deltaB, det, deltaDet, deltaDeltaDet); + + for (uint32_t i = 0; i < len; ++i, ++dst) { + *dst = op(_pixel(fill, sqrtf(det) - b), *dst, a); + det += deltaDet; + deltaDet += deltaDeltaDet; + b += deltaB; + } + } +} + + +void fillRadial(const SwFill* fill, uint8_t* dst, uint32_t y, uint32_t x, uint32_t len, SwMask maskOp, uint8_t a) +{ + if (fill->radial.a < RADIAL_A_THRESHOLD) { + auto radial = &fill->radial; + auto rx = (x + 0.5f) * radial->a11 + (y + 0.5f) * radial->a12 + radial->a13 - radial->fx; + auto ry = (x + 0.5f) * radial->a21 + (y + 0.5f) * radial->a22 + radial->a23 - radial->fy; + for (uint32_t i = 0 ; i < len ; ++i, ++dst) { + auto x0 = 0.5f * (rx * rx + ry * ry - radial->fr * radial->fr) / (radial->dr * radial->fr + rx * radial->dx + ry * radial->dy); + auto src = MULTIPLY(a, A(_pixel(fill, x0))); + *dst = maskOp(src, *dst, ~src); + rx += radial->a11; + ry += radial->a21; + } + } else { + float b, deltaB, det, deltaDet, deltaDeltaDet; + _calculateCoefficients(fill, x, y, b, deltaB, det, deltaDet, deltaDeltaDet); + + for (uint32_t i = 0 ; i < len ; ++i, ++dst) { + auto src = MULTIPLY(a, A(_pixel(fill, sqrtf(det) - b))); + *dst = maskOp(src, *dst, ~src); + det += deltaDet; + deltaDet += deltaDeltaDet; + b += deltaB; + } + } +} + + +void fillRadial(const SwFill* fill, uint8_t* dst, uint32_t y, uint32_t x, uint32_t len, uint8_t* cmp, SwMask maskOp, uint8_t a) +{ + if (fill->radial.a < RADIAL_A_THRESHOLD) { + auto radial = &fill->radial; + auto rx = (x + 0.5f) * radial->a11 + (y + 0.5f) * radial->a12 + radial->a13 - radial->fx; + auto ry = (x + 0.5f) * radial->a21 + (y + 0.5f) * radial->a22 + radial->a23 - radial->fy; + for (uint32_t i = 0 ; i < len ; ++i, ++dst, ++cmp) { + auto x0 = 0.5f * (rx * rx + ry * ry - radial->fr * radial->fr) / (radial->dr * radial->fr + rx * radial->dx + ry * radial->dy); + auto src = MULTIPLY(A(A(_pixel(fill, x0))), a); + auto tmp = maskOp(src, *cmp, 0); + *dst = tmp + MULTIPLY(*dst, ~tmp); + rx += radial->a11; + ry += radial->a21; + } + } else { + float b, deltaB, det, deltaDet, deltaDeltaDet; + _calculateCoefficients(fill, x, y, b, deltaB, det, deltaDet, deltaDeltaDet); + + for (uint32_t i = 0 ; i < len ; ++i, ++dst, ++cmp) { + auto src = MULTIPLY(A(_pixel(fill, sqrtf(det))), a); + auto tmp = maskOp(src, *cmp, 0); + *dst = tmp + MULTIPLY(*dst, ~tmp); + deltaDet += deltaDeltaDet; + b += deltaB; + } + } +} + + +void fillRadial(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, SwBlender op, SwBlender op2, uint8_t a) +{ + if (fill->radial.a < RADIAL_A_THRESHOLD) { + auto radial = &fill->radial; + auto rx = (x + 0.5f) * radial->a11 + (y + 0.5f) * radial->a12 + radial->a13 - radial->fx; + auto ry = (x + 0.5f) * radial->a21 + (y + 0.5f) * radial->a22 + radial->a23 - radial->fy; + + if (a == 255) { + for (uint32_t i = 0; i < len; ++i, ++dst) { + auto x0 = 0.5f * (rx * rx + ry * ry - radial->fr * radial->fr) / (radial->dr * radial->fr + rx * radial->dx + ry * radial->dy); + auto tmp = op(_pixel(fill, x0), *dst, 255); + *dst = op2(tmp, *dst, 255); + rx += radial->a11; + ry += radial->a21; + } + } else { + for (uint32_t i = 0; i < len; ++i, ++dst) { + auto x0 = 0.5f * (rx * rx + ry * ry - radial->fr * radial->fr) / (radial->dr * radial->fr + rx * radial->dx + ry * radial->dy); + auto tmp = op(_pixel(fill, x0), *dst, 255); + auto tmp2 = op2(tmp, *dst, 255); + *dst = INTERPOLATE(tmp2, *dst, a); + rx += radial->a11; + ry += radial->a21; + } + } + } else { + float b, deltaB, det, deltaDet, deltaDeltaDet; + _calculateCoefficients(fill, x, y, b, deltaB, det, deltaDet, deltaDeltaDet); + if (a == 255) { + for (uint32_t i = 0 ; i < len ; ++i, ++dst) { + auto tmp = op(_pixel(fill, sqrtf(det) - b), *dst, 255); + *dst = op2(tmp, *dst, 255); + det += deltaDet; + deltaDet += deltaDeltaDet; + b += deltaB; + } + } else { + for (uint32_t i = 0 ; i < len ; ++i, ++dst) { + auto tmp = op(_pixel(fill, sqrtf(det) - b), *dst, 255); + auto tmp2 = op2(tmp, *dst, 255); + *dst = INTERPOLATE(tmp2, *dst, a); + det += deltaDet; + deltaDet += deltaDeltaDet; + b += deltaB; + } + } + } +} + + +void fillLinear(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, uint8_t* cmp, SwAlpha alpha, uint8_t csize, uint8_t opacity) +{ + //Rotation + float rx = x + 0.5f; + float ry = y + 0.5f; + float t = (fill->linear.dx * rx + fill->linear.dy * ry + fill->linear.offset) * (GRADIENT_STOP_SIZE - 1); + float inc = (fill->linear.dx) * (GRADIENT_STOP_SIZE - 1); + + if (opacity == 255) { + if (mathZero(inc)) { + auto color = _fixedPixel(fill, static_cast(t * FIXPT_SIZE)); + for (uint32_t i = 0; i < len; ++i, ++dst, cmp += csize) { + *dst = opBlendNormal(color, *dst, alpha(cmp)); + } + return; + } + + auto vMax = static_cast(INT32_MAX >> (FIXPT_BITS + 1)); + auto vMin = -vMax; + auto v = t + (inc * len); + + //we can use fixed point math + if (v < vMax && v > vMin) { + auto t2 = static_cast(t * FIXPT_SIZE); + auto inc2 = static_cast(inc * FIXPT_SIZE); + for (uint32_t j = 0; j < len; ++j, ++dst, cmp += csize) { + *dst = opBlendNormal(_fixedPixel(fill, t2), *dst, alpha(cmp)); + t2 += inc2; + } + //we have to fallback to float math + } else { + uint32_t counter = 0; + while (counter++ < len) { + *dst = opBlendNormal(_pixel(fill, t / GRADIENT_STOP_SIZE), *dst, alpha(cmp)); + ++dst; + t += inc; + cmp += csize; + } + } + } else { + if (mathZero(inc)) { + auto color = _fixedPixel(fill, static_cast(t * FIXPT_SIZE)); + for (uint32_t i = 0; i < len; ++i, ++dst, cmp += csize) { + *dst = opBlendNormal(color, *dst, MULTIPLY(alpha(cmp), opacity)); + } + return; + } + + auto vMax = static_cast(INT32_MAX >> (FIXPT_BITS + 1)); + auto vMin = -vMax; + auto v = t + (inc * len); + + //we can use fixed point math + if (v < vMax && v > vMin) { + auto t2 = static_cast(t * FIXPT_SIZE); + auto inc2 = static_cast(inc * FIXPT_SIZE); + for (uint32_t j = 0; j < len; ++j, ++dst, cmp += csize) { + *dst = opBlendNormal(_fixedPixel(fill, t2), *dst, MULTIPLY(alpha(cmp), opacity)); + t2 += inc2; + } + //we have to fallback to float math + } else { + uint32_t counter = 0; + while (counter++ < len) { + *dst = opBlendNormal(_pixel(fill, t / GRADIENT_STOP_SIZE), *dst, MULTIPLY(opacity, alpha(cmp))); + ++dst; + t += inc; + cmp += csize; + } + } + } +} + + +void fillLinear(const SwFill* fill, uint8_t* dst, uint32_t y, uint32_t x, uint32_t len, SwMask maskOp, uint8_t a) +{ + //Rotation + float rx = x + 0.5f; + float ry = y + 0.5f; + float t = (fill->linear.dx * rx + fill->linear.dy * ry + fill->linear.offset) * (GRADIENT_STOP_SIZE - 1); + float inc = (fill->linear.dx) * (GRADIENT_STOP_SIZE - 1); + + if (mathZero(inc)) { + auto src = MULTIPLY(a, A(_fixedPixel(fill, static_cast(t * FIXPT_SIZE)))); + for (uint32_t i = 0; i < len; ++i, ++dst) { + *dst = maskOp(src, *dst, ~src); + } + return; + } + + auto vMax = static_cast(INT32_MAX >> (FIXPT_BITS + 1)); + auto vMin = -vMax; + auto v = t + (inc * len); + + //we can use fixed point math + if (v < vMax && v > vMin) { + auto t2 = static_cast(t * FIXPT_SIZE); + auto inc2 = static_cast(inc * FIXPT_SIZE); + for (uint32_t j = 0; j < len; ++j, ++dst) { + auto src = MULTIPLY(_fixedPixel(fill, t2), a); + *dst = maskOp(src, *dst, ~src); + t2 += inc2; + } + //we have to fallback to float math + } else { + uint32_t counter = 0; + while (counter++ < len) { + auto src = MULTIPLY(_pixel(fill, t / GRADIENT_STOP_SIZE), a); + *dst = maskOp(src, *dst, ~src); + ++dst; + t += inc; + } + } +} + + +void fillLinear(const SwFill* fill, uint8_t* dst, uint32_t y, uint32_t x, uint32_t len, uint8_t* cmp, SwMask maskOp, uint8_t a) +{ + //Rotation + float rx = x + 0.5f; + float ry = y + 0.5f; + float t = (fill->linear.dx * rx + fill->linear.dy * ry + fill->linear.offset) * (GRADIENT_STOP_SIZE - 1); + float inc = (fill->linear.dx) * (GRADIENT_STOP_SIZE - 1); + + if (mathZero(inc)) { + auto src = A(_fixedPixel(fill, static_cast(t * FIXPT_SIZE))); + src = MULTIPLY(src, a); + for (uint32_t i = 0; i < len; ++i, ++dst, ++cmp) { + auto tmp = maskOp(src, *cmp, 0); + *dst = tmp + MULTIPLY(*dst, ~tmp); + } + return; + } + + auto vMax = static_cast(INT32_MAX >> (FIXPT_BITS + 1)); + auto vMin = -vMax; + auto v = t + (inc * len); + + //we can use fixed point math + if (v < vMax && v > vMin) { + auto t2 = static_cast(t * FIXPT_SIZE); + auto inc2 = static_cast(inc * FIXPT_SIZE); + for (uint32_t j = 0; j < len; ++j, ++dst, ++cmp) { + auto src = MULTIPLY(a, A(_fixedPixel(fill, t2))); + auto tmp = maskOp(src, *cmp, 0); + *dst = tmp + MULTIPLY(*dst, ~tmp); + t2 += inc2; + } + //we have to fallback to float math + } else { + uint32_t counter = 0; + while (counter++ < len) { + auto src = MULTIPLY(A(_pixel(fill, t / GRADIENT_STOP_SIZE)), a); + auto tmp = maskOp(src, *cmp, 0); + *dst = tmp + MULTIPLY(*dst, ~tmp); + ++dst; + ++cmp; + t += inc; + } + } +} + + +void fillLinear(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, SwBlender op, uint8_t a) +{ + //Rotation + float rx = x + 0.5f; + float ry = y + 0.5f; + float t = (fill->linear.dx * rx + fill->linear.dy * ry + fill->linear.offset) * (GRADIENT_STOP_SIZE - 1); + float inc = (fill->linear.dx) * (GRADIENT_STOP_SIZE - 1); + + if (mathZero(inc)) { + auto color = _fixedPixel(fill, static_cast(t * FIXPT_SIZE)); + for (uint32_t i = 0; i < len; ++i, ++dst) { + *dst = op(color, *dst, a); + } + return; + } + + auto vMax = static_cast(INT32_MAX >> (FIXPT_BITS + 1)); + auto vMin = -vMax; + auto v = t + (inc * len); + + //we can use fixed point math + if (v < vMax && v > vMin) { + auto t2 = static_cast(t * FIXPT_SIZE); + auto inc2 = static_cast(inc * FIXPT_SIZE); + for (uint32_t j = 0; j < len; ++j, ++dst) { + *dst = op(_fixedPixel(fill, t2), *dst, a); + t2 += inc2; + } + //we have to fallback to float math + } else { + uint32_t counter = 0; + while (counter++ < len) { + *dst = op(_pixel(fill, t / GRADIENT_STOP_SIZE), *dst, a); + ++dst; + t += inc; + } + } +} + + +void fillLinear(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, SwBlender op, SwBlender op2, uint8_t a) +{ + //Rotation + float rx = x + 0.5f; + float ry = y + 0.5f; + float t = (fill->linear.dx * rx + fill->linear.dy * ry + fill->linear.offset) * (GRADIENT_STOP_SIZE - 1); + float inc = (fill->linear.dx) * (GRADIENT_STOP_SIZE - 1); + + if (mathZero(inc)) { + auto color = _fixedPixel(fill, static_cast(t * FIXPT_SIZE)); + if (a == 255) { + for (uint32_t i = 0; i < len; ++i, ++dst) { + auto tmp = op(color, *dst, a); + *dst = op2(tmp, *dst, 255); + } + } else { + for (uint32_t i = 0; i < len; ++i, ++dst) { + auto tmp = op(color, *dst, a); + auto tmp2 = op2(tmp, *dst, 255); + *dst = INTERPOLATE(tmp2, *dst, a); + } + } + return; + } + + auto vMax = static_cast(INT32_MAX >> (FIXPT_BITS + 1)); + auto vMin = -vMax; + auto v = t + (inc * len); + + if (a == 255) { + //we can use fixed point math + if (v < vMax && v > vMin) { + auto t2 = static_cast(t * FIXPT_SIZE); + auto inc2 = static_cast(inc * FIXPT_SIZE); + for (uint32_t j = 0; j < len; ++j, ++dst) { + auto tmp = op(_fixedPixel(fill, t2), *dst, 255); + *dst = op2(tmp, *dst, 255); + t2 += inc2; + } + //we have to fallback to float math + } else { + uint32_t counter = 0; + while (counter++ < len) { + auto tmp = op(_pixel(fill, t / GRADIENT_STOP_SIZE), *dst, 255); + *dst = op2(tmp, *dst, 255); + ++dst; + t += inc; + } + } + } else { + //we can use fixed point math + if (v < vMax && v > vMin) { + auto t2 = static_cast(t * FIXPT_SIZE); + auto inc2 = static_cast(inc * FIXPT_SIZE); + for (uint32_t j = 0; j < len; ++j, ++dst) { + auto tmp = op(_fixedPixel(fill, t2), *dst, 255); + auto tmp2 = op2(tmp, *dst, 255); + *dst = INTERPOLATE(tmp2, *dst, a); + t2 += inc2; + } + //we have to fallback to float math + } else { + uint32_t counter = 0; + while (counter++ < len) { + auto tmp = op(_pixel(fill, t / GRADIENT_STOP_SIZE), *dst, 255); + auto tmp2 = op2(tmp, *dst, 255); + *dst = INTERPOLATE(tmp2, *dst, a); + ++dst; + t += inc; + } + } + } +} + + +bool fillGenColorTable(SwFill* fill, const Fill* fdata, const Matrix* transform, SwSurface* surface, uint8_t opacity, bool ctable) +{ + if (!fill) return false; + + fill->spread = fdata->spread(); + + if (ctable) { + if (!_updateColorTable(fill, fdata, surface, opacity)) return false; + } + + if (fdata->identifier() == TVG_CLASS_ID_LINEAR) { + return _prepareLinear(fill, static_cast(fdata), transform); + } else if (fdata->identifier() == TVG_CLASS_ID_RADIAL) { + return _prepareRadial(fill, static_cast(fdata), transform); + } + + //LOG: What type of gradient?! + + return false; +} + + +void fillReset(SwFill* fill) +{ + if (fill->ctable) { + free(fill->ctable); + fill->ctable = nullptr; + } + fill->translucent = false; +} + + +void fillFree(SwFill* fill) +{ + if (!fill) return; + + if (fill->ctable) free(fill->ctable); + + free(fill); +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSwImage.cpp b/include/liblvgl/libs/thorvg/tvgSwImage.cpp new file mode 100644 index 00000000..22d1cf79 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSwImage.cpp @@ -0,0 +1,164 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgMath.h" +#include "tvgSwCommon.h" + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +static inline bool _onlyShifted(const Matrix* m) +{ + if (mathEqual(m->e11, 1.0f) && mathEqual(m->e22, 1.0f) && mathZero(m->e12) && mathZero(m->e21)) return true; + return false; +} + + +static bool _genOutline(SwImage* image, const RenderMesh* mesh, const Matrix* transform, SwMpool* mpool, unsigned tid) +{ + image->outline = mpoolReqOutline(mpool, tid); + auto outline = image->outline; + + outline->pts.reserve(5); + outline->types.reserve(5); + outline->cntrs.reserve(1); + outline->closed.reserve(1); + + Point to[4]; + if (mesh->triangleCnt > 0) { + // TODO: Optimise me. We appear to calculate this exact min/max bounding area in multiple + // places. We should be able to re-use one we have already done? Also see: + // tvgPicture.h --> bounds + // tvgSwRasterTexmap.h --> _rasterTexmapPolygonMesh + // + // TODO: Should we calculate the exact path(s) of the triangle mesh instead? + // i.e. copy tvgSwShape.capp -> _genOutline? + // + // TODO: Cntrs? + auto triangles = mesh->triangles; + auto min = triangles[0].vertex[0].pt; + auto max = triangles[0].vertex[0].pt; + + for (uint32_t i = 0; i < mesh->triangleCnt; ++i) { + if (triangles[i].vertex[0].pt.x < min.x) min.x = triangles[i].vertex[0].pt.x; + else if (triangles[i].vertex[0].pt.x > max.x) max.x = triangles[i].vertex[0].pt.x; + if (triangles[i].vertex[0].pt.y < min.y) min.y = triangles[i].vertex[0].pt.y; + else if (triangles[i].vertex[0].pt.y > max.y) max.y = triangles[i].vertex[0].pt.y; + + if (triangles[i].vertex[1].pt.x < min.x) min.x = triangles[i].vertex[1].pt.x; + else if (triangles[i].vertex[1].pt.x > max.x) max.x = triangles[i].vertex[1].pt.x; + if (triangles[i].vertex[1].pt.y < min.y) min.y = triangles[i].vertex[1].pt.y; + else if (triangles[i].vertex[1].pt.y > max.y) max.y = triangles[i].vertex[1].pt.y; + + if (triangles[i].vertex[2].pt.x < min.x) min.x = triangles[i].vertex[2].pt.x; + else if (triangles[i].vertex[2].pt.x > max.x) max.x = triangles[i].vertex[2].pt.x; + if (triangles[i].vertex[2].pt.y < min.y) min.y = triangles[i].vertex[2].pt.y; + else if (triangles[i].vertex[2].pt.y > max.y) max.y = triangles[i].vertex[2].pt.y; + } + to[0] = {min.x, min.y}; + to[1] = {max.x, min.y}; + to[2] = {max.x, max.y}; + to[3] = {min.x, max.y}; + } else { + auto w = static_cast(image->w); + auto h = static_cast(image->h); + to[0] = {0, 0}; + to[1] = {w, 0}; + to[2] = {w, h}; + to[3] = {0, h}; + } + + for (int i = 0; i < 4; i++) { + outline->pts.push(mathTransform(&to[i], transform)); + outline->types.push(SW_CURVE_TYPE_POINT); + } + + outline->pts.push(outline->pts[0]); + outline->types.push(SW_CURVE_TYPE_POINT); + outline->cntrs.push(outline->pts.count - 1); + outline->closed.push(true); + + image->outline = outline; + + return true; +} + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +bool imagePrepare(SwImage* image, const RenderMesh* mesh, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid) +{ + image->direct = _onlyShifted(transform); + + //Fast track: Non-transformed image but just shifted. + if (image->direct) { + image->ox = -static_cast(round(transform->e13)); + image->oy = -static_cast(round(transform->e23)); + //Figure out the scale factor by transform matrix + } else { + auto scaleX = sqrtf((transform->e11 * transform->e11) + (transform->e21 * transform->e21)); + auto scaleY = sqrtf((transform->e22 * transform->e22) + (transform->e12 * transform->e12)); + image->scale = (fabsf(scaleX - scaleY) > 0.01f) ? 1.0f : scaleX; + + if (mathZero(transform->e12) && mathZero(transform->e21)) image->scaled = true; + else image->scaled = false; + } + + if (!_genOutline(image, mesh, transform, mpool, tid)) return false; + return mathUpdateOutlineBBox(image->outline, clipRegion, renderRegion, image->direct); +} + + +bool imageGenRle(SwImage* image, const SwBBox& renderRegion, bool antiAlias) +{ + if ((image->rle = rleRender(image->rle, image->outline, renderRegion, antiAlias))) return true; + + return false; +} + + +void imageDelOutline(SwImage* image, SwMpool* mpool, uint32_t tid) +{ + mpoolRetOutline(mpool, tid); + image->outline = nullptr; +} + + +void imageReset(SwImage* image) +{ + rleReset(image->rle); +} + + +void imageFree(SwImage* image) +{ + rleFree(image->rle); +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSwMath.cpp b/include/liblvgl/libs/thorvg/tvgSwMath.cpp new file mode 100644 index 00000000..8b8d8a4b --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSwMath.cpp @@ -0,0 +1,329 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgMath.h" +#include "tvgSwCommon.h" + + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +static float TO_RADIAN(SwFixed angle) +{ + return (float(angle) / 65536.0f) * (MATH_PI / 180.0f); +} + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +SwFixed mathMean(SwFixed angle1, SwFixed angle2) +{ + return angle1 + mathDiff(angle1, angle2) / 2; +} + + +bool mathSmallCubic(const SwPoint* base, SwFixed& angleIn, SwFixed& angleMid, SwFixed& angleOut) +{ + auto d1 = base[2] - base[3]; + auto d2 = base[1] - base[2]; + auto d3 = base[0] - base[1]; + + if (d1.small()) { + if (d2.small()) { + if (d3.small()) { + angleIn = angleMid = angleOut = 0; + return true; + } else { + angleIn = angleMid = angleOut = mathAtan(d3); + } + } else { + if (d3.small()) { + angleIn = angleMid = angleOut = mathAtan(d2); + } else { + angleIn = angleMid = mathAtan(d2); + angleOut = mathAtan(d3); + } + } + } else { + if (d2.small()) { + if (d3.small()) { + angleIn = angleMid = angleOut = mathAtan(d1); + } else { + angleIn = mathAtan(d1); + angleOut = mathAtan(d3); + angleMid = mathMean(angleIn, angleOut); + } + } else { + if (d3.small()) { + angleIn = mathAtan(d1); + angleMid = angleOut = mathAtan(d2); + } else { + angleIn = mathAtan(d1); + angleMid = mathAtan(d2); + angleOut = mathAtan(d3); + } + } + } + + auto theta1 = abs(mathDiff(angleIn, angleMid)); + auto theta2 = abs(mathDiff(angleMid, angleOut)); + + if ((theta1 < (SW_ANGLE_PI / 8)) && (theta2 < (SW_ANGLE_PI / 8))) return true; + return false; +} + + +int64_t mathMultiply(int64_t a, int64_t b) +{ + int32_t s = 1; + + //move sign + if (a < 0) { + a = -a; + s = -s; + } + if (b < 0) { + b = -b; + s = -s; + } + int64_t c = (a * b + 0x8000L) >> 16; + return (s > 0) ? c : -c; +} + + +int64_t mathDivide(int64_t a, int64_t b) +{ + int32_t s = 1; + + //move sign + if (a < 0) { + a = -a; + s = -s; + } + if (b < 0) { + b = -b; + s = -s; + } + int64_t q = b > 0 ? ((a << 16) + (b >> 1)) / b : 0x7FFFFFFFL; + return (s < 0 ? -q : q); +} + + +int64_t mathMulDiv(int64_t a, int64_t b, int64_t c) +{ + int32_t s = 1; + + //move sign + if (a < 0) { + a = -a; + s = -s; + } + if (b < 0) { + b = -b; + s = -s; + } + if (c < 0) { + c = -c; + s = -s; + } + int64_t d = c > 0 ? (a * b + (c >> 1)) / c : 0x7FFFFFFFL; + + return (s > 0 ? d : -d); +} + + +void mathRotate(SwPoint& pt, SwFixed angle) +{ + if (angle == 0 || pt.zero()) return; + + Point v = pt.toPoint(); + + auto radian = TO_RADIAN(angle); + auto cosv = cosf(radian); + auto sinv = sinf(radian); + + pt.x = SwCoord(roundf((v.x * cosv - v.y * sinv) * 64.0f)); + pt.y = SwCoord(roundf((v.x * sinv + v.y * cosv) * 64.0f)); +} + + +SwFixed mathTan(SwFixed angle) +{ + if (angle == 0) return 0; + return SwFixed(tanf(TO_RADIAN(angle)) * 65536.0f); +} + + +SwFixed mathAtan(const SwPoint& pt) +{ + if (pt.zero()) return 0; + return SwFixed(atan2f(TO_FLOAT(pt.y), TO_FLOAT(pt.x)) * (180.0f / MATH_PI) * 65536.0f); +} + + +SwFixed mathSin(SwFixed angle) +{ + if (angle == 0) return 0; + return mathCos(SW_ANGLE_PI2 - angle); +} + + +SwFixed mathCos(SwFixed angle) +{ + return SwFixed(cosf(TO_RADIAN(angle)) * 65536.0f); +} + + +SwFixed mathLength(const SwPoint& pt) +{ + if (pt.zero()) return 0; + + //trivial case + if (pt.x == 0) return abs(pt.y); + if (pt.y == 0) return abs(pt.x); + + auto v = pt.toPoint(); + //return static_cast(sqrtf(v.x * v.x + v.y * v.y) * 65536.0f); + + /* approximate sqrt(x*x + y*y) using alpha max plus beta min algorithm. + With alpha = 1, beta = 3/8, giving results with the largest error less + than 7% compared to the exact value. */ + if (v.x < 0) v.x = -v.x; + if (v.y < 0) v.y = -v.y; + return static_cast((v.x > v.y) ? (v.x + v.y * 0.375f) : (v.y + v.x * 0.375f)); +} + + +void mathSplitCubic(SwPoint* base) +{ + SwCoord a, b, c, d; + + base[6].x = base[3].x; + c = base[1].x; + d = base[2].x; + base[1].x = a = (base[0].x + c) >> 1; + base[5].x = b = (base[3].x + d) >> 1; + c = (c + d) >> 1; + base[2].x = a = (a + c) >> 1; + base[4].x = b = (b + c) >> 1; + base[3].x = (a + b) >> 1; + + base[6].y = base[3].y; + c = base[1].y; + d = base[2].y; + base[1].y = a = (base[0].y + c) >> 1; + base[5].y = b = (base[3].y + d) >> 1; + c = (c + d) >> 1; + base[2].y = a = (a + c) >> 1; + base[4].y = b = (b + c) >> 1; + base[3].y = (a + b) >> 1; +} + + +SwFixed mathDiff(SwFixed angle1, SwFixed angle2) +{ + auto delta = angle2 - angle1; + + delta %= SW_ANGLE_2PI; + if (delta < 0) delta += SW_ANGLE_2PI; + if (delta > SW_ANGLE_PI) delta -= SW_ANGLE_2PI; + + return delta; +} + + +SwPoint mathTransform(const Point* to, const Matrix* transform) +{ + if (!transform) return {TO_SWCOORD(to->x), TO_SWCOORD(to->y)}; + + auto tx = to->x * transform->e11 + to->y * transform->e12 + transform->e13; + auto ty = to->x * transform->e21 + to->y * transform->e22 + transform->e23; + + return {TO_SWCOORD(tx), TO_SWCOORD(ty)}; +} + + +bool mathClipBBox(const SwBBox& clipper, SwBBox& clipee) +{ + clipee.max.x = (clipee.max.x < clipper.max.x) ? clipee.max.x : clipper.max.x; + clipee.max.y = (clipee.max.y < clipper.max.y) ? clipee.max.y : clipper.max.y; + clipee.min.x = (clipee.min.x > clipper.min.x) ? clipee.min.x : clipper.min.x; + clipee.min.y = (clipee.min.y > clipper.min.y) ? clipee.min.y : clipper.min.y; + + //Check valid region + if (clipee.max.x - clipee.min.x < 1 && clipee.max.y - clipee.min.y < 1) return false; + + //Check boundary + if (clipee.min.x >= clipper.max.x || clipee.min.y >= clipper.max.y || + clipee.max.x <= clipper.min.x || clipee.max.y <= clipper.min.y) return false; + + return true; +} + + +bool mathUpdateOutlineBBox(const SwOutline* outline, const SwBBox& clipRegion, SwBBox& renderRegion, bool fastTrack) +{ + if (!outline) return false; + + if (outline->pts.empty() || outline->cntrs.empty()) { + renderRegion.reset(); + return false; + } + + auto pt = outline->pts.begin(); + + auto xMin = pt->x; + auto xMax = pt->x; + auto yMin = pt->y; + auto yMax = pt->y; + + for (++pt; pt < outline->pts.end(); ++pt) { + if (xMin > pt->x) xMin = pt->x; + if (xMax < pt->x) xMax = pt->x; + if (yMin > pt->y) yMin = pt->y; + if (yMax < pt->y) yMax = pt->y; + } + //Since no antialiasing is applied in the Fast Track case, + //the rasterization region has to be rearranged. + //https://github.com/Samsung/thorvg/issues/916 + if (fastTrack) { + renderRegion.min.x = static_cast(round(xMin / 64.0f)); + renderRegion.max.x = static_cast(round(xMax / 64.0f)); + renderRegion.min.y = static_cast(round(yMin / 64.0f)); + renderRegion.max.y = static_cast(round(yMax / 64.0f)); + } else { + renderRegion.min.x = xMin >> 6; + renderRegion.max.x = (xMax + 63) >> 6; + renderRegion.min.y = yMin >> 6; + renderRegion.max.y = (yMax + 63) >> 6; + } + return mathClipBBox(clipRegion, renderRegion); +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSwMemPool.cpp b/include/liblvgl/libs/thorvg/tvgSwMemPool.cpp new file mode 100644 index 00000000..4a9d7f52 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSwMemPool.cpp @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgSwCommon.h" + + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +SwOutline* mpoolReqOutline(SwMpool* mpool, unsigned idx) +{ + return &mpool->outline[idx]; +} + + +void mpoolRetOutline(SwMpool* mpool, unsigned idx) +{ + mpool->outline[idx].pts.clear(); + mpool->outline[idx].cntrs.clear(); + mpool->outline[idx].types.clear(); + mpool->outline[idx].closed.clear(); +} + + +SwOutline* mpoolReqStrokeOutline(SwMpool* mpool, unsigned idx) +{ + return &mpool->strokeOutline[idx]; +} + + +void mpoolRetStrokeOutline(SwMpool* mpool, unsigned idx) +{ + mpool->strokeOutline[idx].pts.clear(); + mpool->strokeOutline[idx].cntrs.clear(); + mpool->strokeOutline[idx].types.clear(); + mpool->strokeOutline[idx].closed.clear(); +} + + +SwOutline* mpoolReqDashOutline(SwMpool* mpool, unsigned idx) +{ + return &mpool->dashOutline[idx]; +} + + +void mpoolRetDashOutline(SwMpool* mpool, unsigned idx) +{ + mpool->dashOutline[idx].pts.clear(); + mpool->dashOutline[idx].cntrs.clear(); + mpool->dashOutline[idx].types.clear(); + mpool->dashOutline[idx].closed.clear(); +} + + +SwMpool* mpoolInit(uint32_t threads) +{ + auto allocSize = threads + 1; + + auto mpool = static_cast(calloc(sizeof(SwMpool), 1)); + mpool->outline = static_cast(calloc(1, sizeof(SwOutline) * allocSize)); + mpool->strokeOutline = static_cast(calloc(1, sizeof(SwOutline) * allocSize)); + mpool->dashOutline = static_cast(calloc(1, sizeof(SwOutline) * allocSize)); + mpool->allocSize = allocSize; + + return mpool; +} + + +bool mpoolClear(SwMpool* mpool) +{ + for (unsigned i = 0; i < mpool->allocSize; ++i) { + mpool->outline[i].pts.reset(); + mpool->outline[i].cntrs.reset(); + mpool->outline[i].types.reset(); + mpool->outline[i].closed.reset(); + + mpool->strokeOutline[i].pts.reset(); + mpool->strokeOutline[i].cntrs.reset(); + mpool->strokeOutline[i].types.reset(); + mpool->strokeOutline[i].closed.reset(); + + mpool->dashOutline[i].pts.reset(); + mpool->dashOutline[i].cntrs.reset(); + mpool->dashOutline[i].types.reset(); + mpool->dashOutline[i].closed.reset(); + } + + return true; +} + + +bool mpoolTerm(SwMpool* mpool) +{ + if (!mpool) return false; + + mpoolClear(mpool); + + free(mpool->outline); + free(mpool->strokeOutline); + free(mpool->dashOutline); + free(mpool); + + return true; +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSwRaster.cpp b/include/liblvgl/libs/thorvg/tvgSwRaster.cpp new file mode 100644 index 00000000..237e30de --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSwRaster.cpp @@ -0,0 +1,1967 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifdef _WIN32 + #include +#elif defined(__linux__) + #include +#else + #include +#endif + +#include "tvgMath.h" +#include "tvgRender.h" +#include "tvgSwCommon.h" + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ +constexpr auto DOWN_SCALE_TOLERANCE = 0.5f; + +struct FillLinear +{ + void operator()(const SwFill* fill, uint8_t* dst, uint32_t y, uint32_t x, uint32_t len, SwMask op, uint8_t a) + { + fillLinear(fill, dst, y, x, len, op, a); + } + + void operator()(const SwFill* fill, uint8_t* dst, uint32_t y, uint32_t x, uint32_t len, uint8_t* cmp, SwMask op, uint8_t a) + { + fillLinear(fill, dst, y, x, len, cmp, op, a); + } + + void operator()(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, SwBlender op, uint8_t a) + { + fillLinear(fill, dst, y, x, len, op, a); + } + + void operator()(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, uint8_t* cmp, SwAlpha alpha, uint8_t csize, uint8_t opacity) + { + fillLinear(fill, dst, y, x, len, cmp, alpha, csize, opacity); + } + + void operator()(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, SwBlender op, SwBlender op2, uint8_t a) + { + fillLinear(fill, dst, y, x, len, op, op2, a); + } + +}; + +struct FillRadial +{ + void operator()(const SwFill* fill, uint8_t* dst, uint32_t y, uint32_t x, uint32_t len, SwMask op, uint8_t a) + { + fillRadial(fill, dst, y, x, len, op, a); + } + + void operator()(const SwFill* fill, uint8_t* dst, uint32_t y, uint32_t x, uint32_t len, uint8_t* cmp, SwMask op, uint8_t a) + { + fillRadial(fill, dst, y, x, len, cmp, op, a); + } + + void operator()(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, SwBlender op, uint8_t a) + { + fillRadial(fill, dst, y, x, len, op, a); + } + + void operator()(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, uint8_t* cmp, SwAlpha alpha, uint8_t csize, uint8_t opacity) + { + fillRadial(fill, dst, y, x, len, cmp, alpha, csize, opacity); + } + + void operator()(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, SwBlender op, SwBlender op2, uint8_t a) + { + fillRadial(fill, dst, y, x, len, op, op2, a); + } +}; + + +static inline uint8_t _alpha(uint8_t* a) +{ + return *a; +} + + +static inline uint8_t _ialpha(uint8_t* a) +{ + return ~(*a); +} + + +static inline uint8_t _abgrLuma(uint8_t* c) +{ + auto v = *(uint32_t*)c; + return ((((v&0xff)*54) + (((v>>8)&0xff)*183) + (((v>>16)&0xff)*19))) >> 8; //0.2125*R + 0.7154*G + 0.0721*B +} + + +static inline uint8_t _argbLuma(uint8_t* c) +{ + auto v = *(uint32_t*)c; + return ((((v&0xff)*19) + (((v>>8)&0xff)*183) + (((v>>16)&0xff)*54))) >> 8; //0.0721*B + 0.7154*G + 0.2125*R +} + + +static inline uint8_t _abgrInvLuma(uint8_t* c) +{ + return ~_abgrLuma(c); +} + + +static inline uint8_t _argbInvLuma(uint8_t* c) +{ + return ~_argbLuma(c); +} + + +static inline uint32_t _abgrJoin(uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ + return (a << 24 | b << 16 | g << 8 | r); +} + + +static inline uint32_t _argbJoin(uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ + return (a << 24 | r << 16 | g << 8 | b); +} + +static inline bool _blending(const SwSurface* surface) +{ + return (surface->blender) ? true : false; +} + + +/* OPTIMIZE_ME: Probably, we can separate masking(8bits) / composition(32bits) + This would help to enhance the performance by avoiding the unnecessary matting from the composition */ +static inline bool _compositing(const SwSurface* surface) +{ + if (!surface->compositor || (int)surface->compositor->method <= (int)CompositeMethod::ClipPath) return false; + return true; +} + + +static inline bool _matting(const SwSurface* surface) +{ + if ((int)surface->compositor->method < (int)CompositeMethod::AddMask) return true; + else return false; +} + +static inline uint8_t _opMaskNone(uint8_t s, TVG_UNUSED uint8_t d, TVG_UNUSED uint8_t a) +{ + return s; +} + +static inline uint8_t _opMaskAdd(uint8_t s, uint8_t d, uint8_t a) +{ + return s + MULTIPLY(d, a); +} + + +static inline uint8_t _opMaskSubtract(uint8_t s, uint8_t d, TVG_UNUSED uint8_t a) +{ + return MULTIPLY(s, 255 - d); +} + + +static inline uint8_t _opMaskIntersect(uint8_t s, uint8_t d, TVG_UNUSED uint8_t a) +{ + return MULTIPLY(s, d); +} + + +static inline uint8_t _opMaskDifference(uint8_t s, uint8_t d, uint8_t a) +{ + return MULTIPLY(s, 255 - d) + MULTIPLY(d, a); +} + + +static inline bool _direct(CompositeMethod method) +{ + //subtract & Intersect allows the direct composition + if (method == CompositeMethod::SubtractMask || method == CompositeMethod::IntersectMask) return true; + return false; +} + + +static inline SwMask _getMaskOp(CompositeMethod method) +{ + switch (method) { + case CompositeMethod::AddMask: return _opMaskAdd; + case CompositeMethod::SubtractMask: return _opMaskSubtract; + case CompositeMethod::DifferenceMask: return _opMaskDifference; + case CompositeMethod::IntersectMask: return _opMaskIntersect; + default: return nullptr; + } +} + + +static bool _compositeMaskImage(SwSurface* surface, const SwImage* image, const SwBBox& region) +{ + auto dbuffer = &surface->buf8[region.min.y * surface->stride + region.min.x]; + auto sbuffer = image->buf8 + (region.min.y + image->oy) * image->stride + (region.min.x + image->ox); + + for (auto y = region.min.y; y < region.max.y; ++y) { + auto dst = dbuffer; + auto src = sbuffer; + for (auto x = region.min.x; x < region.max.x; x++, dst++, src++) { + *dst = *src + MULTIPLY(*dst, ~*src); + } + dbuffer += surface->stride; + sbuffer += image->stride; + } + return true; +} + + +#include "tvgSwRasterTexmap.h" +#include "tvgSwRasterC.h" +#include "tvgSwRasterAvx.h" +#include "tvgSwRasterNeon.h" + + +static inline uint32_t _sampleSize(float scale) +{ + auto sampleSize = static_cast(0.5f / scale); + if (sampleSize == 0) sampleSize = 1; + return sampleSize; +} + + +//Bilinear Interpolation +//OPTIMIZE_ME: Skip the function pointer access +static uint32_t _interpUpScaler(const uint32_t *img, TVG_UNUSED uint32_t stride, uint32_t w, uint32_t h, float sx, float sy, TVG_UNUSED int32_t miny, TVG_UNUSED int32_t maxy, TVG_UNUSED int32_t n) +{ + auto rx = (size_t)(sx); + auto ry = (size_t)(sy); + auto rx2 = rx + 1; + if (rx2 >= w) rx2 = w - 1; + auto ry2 = ry + 1; + if (ry2 >= h) ry2 = h - 1; + + auto dx = (sx > 0.0f) ? static_cast((sx - rx) * 255.0f) : 0; + auto dy = (sy > 0.0f) ? static_cast((sy - ry) * 255.0f) : 0; + + auto c1 = img[rx + ry * w]; + auto c2 = img[rx2 + ry * w]; + auto c3 = img[rx + ry2 * w]; + auto c4 = img[rx2 + ry2 * w]; + + return INTERPOLATE(INTERPOLATE(c4, c3, dx), INTERPOLATE(c2, c1, dx), dy); +} + + +//2n x 2n Mean Kernel +//OPTIMIZE_ME: Skip the function pointer access +static uint32_t _interpDownScaler(const uint32_t *img, uint32_t stride, uint32_t w, uint32_t h, float sx, TVG_UNUSED float sy, int32_t miny, int32_t maxy, int32_t n) +{ + size_t c[4] = {0, 0, 0, 0}; + + int32_t minx = (int32_t)sx - n; + if (minx < 0) minx = 0; + + int32_t maxx = (int32_t)sx + n; + if (maxx >= (int32_t)w) maxx = w; + + int32_t inc = (n / 2) + 1; + n = 0; + + auto src = img + minx + miny * stride; + + for (auto y = miny; y < maxy; y += inc) { + auto p = src; + for (auto x = minx; x < maxx; x += inc, p += inc) { + c[0] += A(*p); + c[1] += C1(*p); + c[2] += C2(*p); + c[3] += C3(*p); + ++n; + } + src += (stride * inc); + } + + c[0] /= n; + c[1] /= n; + c[2] /= n; + c[3] /= n; + + return (c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3]; +} + + +/************************************************************************/ +/* Rect */ +/************************************************************************/ + +static bool _rasterCompositeMaskedRect(SwSurface* surface, const SwBBox& region, SwMask maskOp, uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ + auto w = static_cast(region.max.x - region.min.x); + auto h = static_cast(region.max.y - region.min.y); + auto cstride = surface->compositor->image.stride; + auto cbuffer = surface->compositor->image.buf8 + (region.min.y * cstride + region.min.x); //compositor buffer + auto ialpha = 255 - a; + + for (uint32_t y = 0; y < h; ++y) { + auto cmp = cbuffer; + for (uint32_t x = 0; x < w; ++x, ++cmp) { + *cmp = maskOp(a, *cmp, ialpha); + } + cbuffer += cstride; + } + return _compositeMaskImage(surface, &surface->compositor->image, surface->compositor->bbox); +} + + +static bool _rasterDirectMaskedRect(SwSurface* surface, const SwBBox& region, SwMask maskOp, uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ + auto w = static_cast(region.max.x - region.min.x); + auto h = static_cast(region.max.y - region.min.y); + auto cbuffer = surface->compositor->image.buf8 + (region.min.y * surface->compositor->image.stride + region.min.x); //compositor buffer + auto dbuffer = surface->buf8 + (region.min.y * surface->stride + region.min.x); //destination buffer + + for (uint32_t y = 0; y < h; ++y) { + auto cmp = cbuffer; + auto dst = dbuffer; + for (uint32_t x = 0; x < w; ++x, ++cmp, ++dst) { + auto tmp = maskOp(a, *cmp, 0); //not use alpha. + *dst = tmp + MULTIPLY(*dst, ~tmp); + } + cbuffer += surface->compositor->image.stride; + dbuffer += surface->stride; + } + return true; +} + + +static bool _rasterMaskedRect(SwSurface* surface, const SwBBox& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ + //8bit masking channels composition + if (surface->channelSize != sizeof(uint8_t)) return false; + + TVGLOG("SW_ENGINE", "Masked(%d) Rect [Region: %lu %lu %lu %lu]", (int)surface->compositor->method, region.min.x, region.min.y, region.max.x - region.min.x, region.max.y - region.min.y); + + auto maskOp = _getMaskOp(surface->compositor->method); + if (_direct(surface->compositor->method)) return _rasterDirectMaskedRect(surface, region, maskOp, r, g, b, a); + else return _rasterCompositeMaskedRect(surface, region, maskOp, r, g, b, a); + return false; +} + + +static bool _rasterMattedRect(SwSurface* surface, const SwBBox& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ + auto w = static_cast(region.max.x - region.min.x); + auto h = static_cast(region.max.y - region.min.y); + auto csize = surface->compositor->image.channelSize; + auto cbuffer = surface->compositor->image.buf8 + ((region.min.y * surface->compositor->image.stride + region.min.x) * csize); //compositor buffer + auto alpha = surface->alpha(surface->compositor->method); + + TVGLOG("SW_ENGINE", "Matted(%d) Rect [Region: %lu %lu %u %u]", (int)surface->compositor->method, region.min.x, region.min.y, w, h); + + //32bits channels + if (surface->channelSize == sizeof(uint32_t)) { + auto color = surface->join(r, g, b, a); + auto buffer = surface->buf32 + (region.min.y * surface->stride) + region.min.x; + for (uint32_t y = 0; y < h; ++y) { + auto dst = &buffer[y * surface->stride]; + auto cmp = &cbuffer[y * surface->compositor->image.stride * csize]; + for (uint32_t x = 0; x < w; ++x, ++dst, cmp += csize) { + *dst = INTERPOLATE(color, *dst, alpha(cmp)); + } + } + //8bits grayscale + } else if (surface->channelSize == sizeof(uint8_t)) { + auto buffer = surface->buf8 + (region.min.y * surface->stride) + region.min.x; + for (uint32_t y = 0; y < h; ++y) { + auto dst = &buffer[y * surface->stride]; + auto cmp = &cbuffer[y * surface->compositor->image.stride * csize]; + for (uint32_t x = 0; x < w; ++x, ++dst, cmp += csize) { + *dst = INTERPOLATE8(a, *dst, alpha(cmp)); + } + } + } + return true; +} + + +static bool _rasterBlendingRect(SwSurface* surface, const SwBBox& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ + if (surface->channelSize != sizeof(uint32_t)) return false; + + auto w = static_cast(region.max.x - region.min.x); + auto h = static_cast(region.max.y - region.min.y); + auto color = surface->join(r, g, b, a); + auto buffer = surface->buf32 + (region.min.y * surface->stride) + region.min.x; + auto ialpha = 255 - a; + + for (uint32_t y = 0; y < h; ++y) { + auto dst = &buffer[y * surface->stride]; + for (uint32_t x = 0; x < w; ++x, ++dst) { + *dst = surface->blender(color, *dst, ialpha); + } + } + return true; +} + + +static bool _rasterTranslucentRect(SwSurface* surface, const SwBBox& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ +#if defined(THORVG_AVX_VECTOR_SUPPORT) + return avxRasterTranslucentRect(surface, region, r, g, b, a); +#elif defined(THORVG_NEON_VECTOR_SUPPORT) + return neonRasterTranslucentRect(surface, region, r, g, b, a); +#else + return cRasterTranslucentRect(surface, region, r, g, b, a); +#endif +} + + +static bool _rasterSolidRect(SwSurface* surface, const SwBBox& region, uint8_t r, uint8_t g, uint8_t b) +{ + auto w = static_cast(region.max.x - region.min.x); + auto h = static_cast(region.max.y - region.min.y); + + //32bits channels + if (surface->channelSize == sizeof(uint32_t)) { + auto color = surface->join(r, g, b, 255); + auto buffer = surface->buf32 + (region.min.y * surface->stride); + for (uint32_t y = 0; y < h; ++y) { + rasterPixel32(buffer + y * surface->stride, color, region.min.x, w); + } + return true; + } + //8bits grayscale + if (surface->channelSize == sizeof(uint8_t)) { + for (uint32_t y = 0; y < h; ++y) { + rasterGrayscale8(surface->buf8, 255, (y + region.min.y) * surface->stride + region.min.x, w); + } + return true; + } + return false; +} + + +static bool _rasterRect(SwSurface* surface, const SwBBox& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ + if (_compositing(surface)) { + if (_matting(surface)) return _rasterMattedRect(surface, region, r, g, b, a); + else return _rasterMaskedRect(surface, region, r, g, b, a); + } else if (_blending(surface)) { + return _rasterBlendingRect(surface, region, r, g, b, a); + } else { + if (a == 255) return _rasterSolidRect(surface, region, r, g, b); + else return _rasterTranslucentRect(surface, region, r, g, b, a); + } + return false; +} + + +/************************************************************************/ +/* Rle */ +/************************************************************************/ + +static bool _rasterCompositeMaskedRle(SwSurface* surface, SwRleData* rle, SwMask maskOp, uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ + auto span = rle->spans; + auto cbuffer = surface->compositor->image.buf8; + auto cstride = surface->compositor->image.stride; + uint8_t src; + + for (uint32_t i = 0; i < rle->size; ++i, ++span) { + auto cmp = &cbuffer[span->y * cstride + span->x]; + if (span->coverage == 255) src = a; + else src = MULTIPLY(a, span->coverage); + auto ialpha = 255 - src; + for (auto x = 0; x < span->len; ++x, ++cmp) { + *cmp = maskOp(src, *cmp, ialpha); + } + } + return _compositeMaskImage(surface, &surface->compositor->image, surface->compositor->bbox); +} + + +static bool _rasterDirectMaskedRle(SwSurface* surface, SwRleData* rle, SwMask maskOp, uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ + auto span = rle->spans; + auto cbuffer = surface->compositor->image.buf8; + auto cstride = surface->compositor->image.stride; + uint8_t src; + + for (uint32_t i = 0; i < rle->size; ++i, ++span) { + auto cmp = &cbuffer[span->y * cstride + span->x]; + auto dst = &surface->buf8[span->y * surface->stride + span->x]; + if (span->coverage == 255) src = a; + else src = MULTIPLY(a, span->coverage); + for (auto x = 0; x < span->len; ++x, ++cmp, ++dst) { + auto tmp = maskOp(src, *cmp, 0); //not use alpha + *dst = tmp + MULTIPLY(*dst, ~tmp); + } + } + return true; +} + + +static bool _rasterMaskedRle(SwSurface* surface, SwRleData* rle, uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ + TVGLOG("SW_ENGINE", "Masked(%d) Rle", (int)surface->compositor->method); + + //8bit masking channels composition + if (surface->channelSize != sizeof(uint8_t)) return false; + + auto maskOp = _getMaskOp(surface->compositor->method); + if (_direct(surface->compositor->method)) return _rasterDirectMaskedRle(surface, rle, maskOp, r, g, b, a); + else return _rasterCompositeMaskedRle(surface, rle, maskOp, r, g, b, a); + return false; +} + + +static bool _rasterMattedRle(SwSurface* surface, SwRleData* rle, uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ + TVGLOG("SW_ENGINE", "Matted(%d) Rle", (int)surface->compositor->method); + + auto span = rle->spans; + auto cbuffer = surface->compositor->image.buf8; + auto csize = surface->compositor->image.channelSize; + auto alpha = surface->alpha(surface->compositor->method); + + //32bit channels + if (surface->channelSize == sizeof(uint32_t)) { + uint32_t src; + auto color = surface->join(r, g, b, a); + for (uint32_t i = 0; i < rle->size; ++i, ++span) { + auto dst = &surface->buf32[span->y * surface->stride + span->x]; + auto cmp = &cbuffer[(span->y * surface->compositor->image.stride + span->x) * csize]; + if (span->coverage == 255) src = color; + else src = ALPHA_BLEND(color, span->coverage); + for (uint32_t x = 0; x < span->len; ++x, ++dst, cmp += csize) { + auto tmp = ALPHA_BLEND(src, alpha(cmp)); + *dst = tmp + ALPHA_BLEND(*dst, IA(tmp)); + } + } + return true; + } + //8bit grayscale + if (surface->channelSize == sizeof(uint8_t)) { + uint8_t src; + for (uint32_t i = 0; i < rle->size; ++i, ++span) { + auto dst = &surface->buf8[span->y * surface->stride + span->x]; + auto cmp = &cbuffer[(span->y * surface->compositor->image.stride + span->x) * csize]; + if (span->coverage == 255) src = a; + else src = MULTIPLY(a, span->coverage); + for (uint32_t x = 0; x < span->len; ++x, ++dst, cmp += csize) { + *dst = INTERPOLATE8(src, *dst, alpha(cmp)); + } + } + return true; + } + return false; +} + + +static bool _rasterBlendingRle(SwSurface* surface, const SwRleData* rle, uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ + if (surface->channelSize != sizeof(uint32_t)) return false; + + auto span = rle->spans; + auto color = surface->join(r, g, b, a); + auto ialpha = 255 - a; + + for (uint32_t i = 0; i < rle->size; ++i, ++span) { + auto dst = &surface->buf32[span->y * surface->stride + span->x]; + if (span->coverage == 255) { + for (uint32_t x = 0; x < span->len; ++x, ++dst) { + *dst = surface->blender(color, *dst, ialpha); + } + } else { + for (uint32_t x = 0; x < span->len; ++x, ++dst) { + auto tmp = surface->blender(color, *dst, ialpha); + *dst = INTERPOLATE(tmp, *dst, span->coverage); + } + } + } + return true; +} + + +static bool _rasterTranslucentRle(SwSurface* surface, const SwRleData* rle, uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ +#if defined(THORVG_AVX_VECTOR_SUPPORT) + return avxRasterTranslucentRle(surface, rle, r, g, b, a); +#elif defined(THORVG_NEON_VECTOR_SUPPORT) + return neonRasterTranslucentRle(surface, rle, r, g, b, a); +#else + return cRasterTranslucentRle(surface, rle, r, g, b, a); +#endif +} + + +static bool _rasterSolidRle(SwSurface* surface, const SwRleData* rle, uint8_t r, uint8_t g, uint8_t b) +{ + auto span = rle->spans; + + //32bit channels + if (surface->channelSize == sizeof(uint32_t)) { + auto color = surface->join(r, g, b, 255); + for (uint32_t i = 0; i < rle->size; ++i, ++span) { + if (span->coverage == 255) { + rasterPixel32(surface->buf32 + span->y * surface->stride, color, span->x, span->len); + } else { + auto dst = &surface->buf32[span->y * surface->stride + span->x]; + auto src = ALPHA_BLEND(color, span->coverage); + auto ialpha = 255 - span->coverage; + for (uint32_t x = 0; x < span->len; ++x, ++dst) { + *dst = src + ALPHA_BLEND(*dst, ialpha); + } + } + } + //8bit grayscale + } else if (surface->channelSize == sizeof(uint8_t)) { + for (uint32_t i = 0; i < rle->size; ++i, ++span) { + if (span->coverage == 255) { + rasterGrayscale8(surface->buf8, span->coverage, span->y * surface->stride + span->x, span->len); + } else { + auto dst = &surface->buf8[span->y * surface->stride + span->x]; + auto ialpha = 255 - span->coverage; + for (uint32_t x = 0; x < span->len; ++x, ++dst) { + *dst = span->coverage + MULTIPLY(*dst, ialpha); + } + } + } + } + return true; +} + + +static bool _rasterRle(SwSurface* surface, SwRleData* rle, uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ + if (!rle) return false; + + if (_compositing(surface)) { + if (_matting(surface)) return _rasterMattedRle(surface, rle, r, g, b, a); + else return _rasterMaskedRle(surface, rle, r, g, b, a); + } else if (_blending(surface)) { + return _rasterBlendingRle(surface, rle, r, g, b, a); + } else { + if (a == 255) return _rasterSolidRle(surface, rle, r, g, b); + else return _rasterTranslucentRle(surface, rle, r, g, b, a); + } + return false; +} + + +/************************************************************************/ +/* RLE Scaled Image */ +/************************************************************************/ + +#define SCALED_IMAGE_RANGE_Y(y) \ + auto sy = (y) * itransform->e22 + itransform->e23 - 0.49f; \ + if (sy <= -0.5f || (uint32_t)(sy + 0.5f) >= image->h) continue; \ + if (scaleMethod == _interpDownScaler) { \ + auto my = (int32_t)round(sy); \ + miny = my - (int32_t)sampleSize; \ + if (miny < 0) miny = 0; \ + maxy = my + (int32_t)sampleSize; \ + if (maxy >= (int32_t)image->h) maxy = (int32_t)image->h; \ + } + +#define SCALED_IMAGE_RANGE_X \ + auto sx = (x) * itransform->e11 + itransform->e13 - 0.49f; \ + if (sx <= -0.5f || (uint32_t)(sx + 0.5f) >= image->w) continue; \ + + +#if 0 //Enable it when GRAYSCALE image is supported +static bool _rasterCompositeScaledMaskedRleImage(SwSurface* surface, const SwImage* image, const Matrix* itransform, const SwBBox& region, SwMask maskOp, uint8_t opacity) +{ + auto scaleMethod = image->scale < DOWN_SCALE_TOLERANCE ? _interpDownScaler : _interpUpScaler; + auto sampleSize = _sampleSize(image->scale); + auto span = image->rle->spans; + int32_t miny = 0, maxy = 0; + + for (uint32_t i = 0; i < image->rle->size; ++i, ++span) { + SCALED_IMAGE_RANGE_Y(span->y) + auto cmp = &surface->compositor->image.buf8[span->y * surface->compositor->image.stride + span->x]; + auto a = MULTIPLY(span->coverage, opacity); + for (uint32_t x = static_cast(span->x); x < static_cast(span->x) + span->len; ++x, ++cmp) { + SCALED_IMAGE_RANGE_X + auto src = scaleMethod(image->buf8, image->stride, image->w, image->h, sx, sy, miny, maxy, sampleSize); + if (a < 255) src = MULTIPLY(src, a); + *cmp = maskOp(src, *cmp, ~src); + } + } + return true; +} + + +static bool _rasterDirectScaledMaskedRleImage(SwSurface* surface, const SwImage* image, const Matrix* itransform, const SwBBox& region, SwMask maskOp, uint8_t opacity) +{ + auto scaleMethod = image->scale < DOWN_SCALE_TOLERANCE ? _interpDownScaler : _interpUpScaler; + auto sampleSize = _sampleSize(image->scale); + auto span = image->rle->spans; + int32_t miny = 0, maxy = 0; + + for (uint32_t i = 0; i < image->rle->size; ++i, ++span) { + SCALED_IMAGE_RANGE_Y(span->y) + auto cmp = &surface->compositor->image.buf8[span->y * surface->compositor->image.stride + span->x]; + auto dst = &surface->buf8[span->y * surface->stride + span->x]; + auto a = MULTIPLY(span->coverage, opacity); + for (uint32_t x = static_cast(span->x); x < static_cast(span->x) + span->len; ++x, ++cmp, ++dst) { + SCALED_IMAGE_RANGE_X + auto src = scaleMethod(image->buf8, image->stride, image->w, image->h, sx, sy, miny, maxy, sampleSize); + if (a < 255) src = MULTIPLY(src, a); + src = maskOp(src, *cmp, 0); //not use alpha + *dst = src + MULTIPLY(*dst, ~src); + } + } + return _compositeMaskImage(surface, &surface->compositor->image, surface->compositor->bbox); +} +#endif + +static bool _rasterScaledMaskedRleImage(SwSurface* surface, const SwImage* image, const Matrix* itransform, const SwBBox& region, uint8_t opacity) +{ +#if 0 //Enable it when GRAYSCALE image is supported + TVGLOG("SW_ENGINE", "Scaled Masked(%d) Rle Image", (int)surface->compositor->method); + + //8bit masking channels composition + if (surface->channelSize != sizeof(uint8_t)) return false; + + auto maskOp = _getMaskOp(surface->compositor->method); + if (_direct(surface->compositor->method)) return _rasterDirectScaledMaskedRleImage(surface, image, itransform, region, maskOp, opacity); + else return _rasterCompositeScaledMaskedRleImage(surface, image, itransform, region, maskOp, opacity); +#endif + return false; +} + + +static bool _rasterScaledMattedRleImage(SwSurface* surface, const SwImage* image, const Matrix* itransform, const SwBBox& region, uint8_t opacity) +{ + TVGLOG("SW_ENGINE", "Scaled Matted(%d) Rle Image", (int)surface->compositor->method); + + auto span = image->rle->spans; + auto csize = surface->compositor->image.channelSize; + auto alpha = surface->alpha(surface->compositor->method); + auto scaleMethod = image->scale < DOWN_SCALE_TOLERANCE ? _interpDownScaler : _interpUpScaler; + auto sampleSize = _sampleSize(image->scale); + int32_t miny = 0, maxy = 0; + + for (uint32_t i = 0; i < image->rle->size; ++i, ++span) { + SCALED_IMAGE_RANGE_Y(span->y) + auto dst = &surface->buf32[span->y * surface->stride + span->x]; + auto cmp = &surface->compositor->image.buf8[(span->y * surface->compositor->image.stride + span->x) * csize]; + auto a = MULTIPLY(span->coverage, opacity); + for (uint32_t x = static_cast(span->x); x < static_cast(span->x) + span->len; ++x, ++dst, cmp += csize) { + SCALED_IMAGE_RANGE_X + auto src = scaleMethod(image->buf32, image->stride, image->w, image->h, sx, sy, miny, maxy, sampleSize); + src = ALPHA_BLEND(src, (a == 255) ? alpha(cmp) : MULTIPLY(alpha(cmp), a)); + *dst = src + ALPHA_BLEND(*dst, IA(src)); + } + } + + return true; +} + + +static bool _rasterScaledBlendingRleImage(SwSurface* surface, const SwImage* image, const Matrix* itransform, const SwBBox& region, uint8_t opacity) +{ + auto span = image->rle->spans; + auto scaleMethod = image->scale < DOWN_SCALE_TOLERANCE ? _interpDownScaler : _interpUpScaler; + auto sampleSize = _sampleSize(image->scale); + int32_t miny = 0, maxy = 0; + + for (uint32_t i = 0; i < image->rle->size; ++i, ++span) { + SCALED_IMAGE_RANGE_Y(span->y) + auto dst = &surface->buf32[span->y * surface->stride + span->x]; + auto alpha = MULTIPLY(span->coverage, opacity); + if (alpha == 255) { + for (uint32_t x = static_cast(span->x); x < static_cast(span->x) + span->len; ++x, ++dst) { + SCALED_IMAGE_RANGE_X + auto src = scaleMethod(image->buf32, image->stride, image->w, image->h, sx, sy, miny, maxy, sampleSize); + auto tmp = surface->blender(src, *dst, 255); + *dst = INTERPOLATE(tmp, *dst, A(src)); + } + } else { + for (uint32_t x = static_cast(span->x); x < static_cast(span->x) + span->len; ++x, ++dst) { + SCALED_IMAGE_RANGE_X + auto src = scaleMethod(image->buf32, image->stride, image->w, image->h, sx, sy, miny, maxy, sampleSize); + if (opacity < 255) src = ALPHA_BLEND(src, opacity); + auto tmp = surface->blender(src, *dst, 255); + *dst = INTERPOLATE(tmp, *dst, MULTIPLY(span->coverage, A(src))); + } + } + } + return true; +} + + +static bool _rasterScaledRleImage(SwSurface* surface, const SwImage* image, const Matrix* itransform, const SwBBox& region, uint8_t opacity) +{ + auto span = image->rle->spans; + auto scaleMethod = image->scale < DOWN_SCALE_TOLERANCE ? _interpDownScaler : _interpUpScaler; + auto sampleSize = _sampleSize(image->scale); + int32_t miny = 0, maxy = 0; + + for (uint32_t i = 0; i < image->rle->size; ++i, ++span) { + SCALED_IMAGE_RANGE_Y(span->y) + auto dst = &surface->buf32[span->y * surface->stride + span->x]; + auto alpha = MULTIPLY(span->coverage, opacity); + for (uint32_t x = static_cast(span->x); x < static_cast(span->x) + span->len; ++x, ++dst) { + SCALED_IMAGE_RANGE_X + auto src = scaleMethod(image->buf32, image->stride, image->w, image->h, sx, sy, miny, maxy, sampleSize); + if (alpha < 255) src = ALPHA_BLEND(src, alpha); + *dst = src + ALPHA_BLEND(*dst, IA(src)); + } + } + return true; +} + + +static bool _scaledRleImage(SwSurface* surface, const SwImage* image, const Matrix* transform, const SwBBox& region, uint8_t opacity) +{ + if (surface->channelSize == sizeof(uint8_t)) { + TVGERR("SW_ENGINE", "Not supported scaled rle image!"); + return false; + } + + Matrix itransform; + + if (transform) { + if (!mathInverse(transform, &itransform)) return false; + } else mathIdentity(&itransform); + + if (_compositing(surface)) { + if (_matting(surface)) return _rasterScaledMattedRleImage(surface, image, &itransform, region, opacity); + else return _rasterScaledMaskedRleImage(surface, image, &itransform, region, opacity); + } else if (_blending(surface)) { + return _rasterScaledBlendingRleImage(surface, image, &itransform, region, opacity); + } else { + return _rasterScaledRleImage(surface, image, &itransform, region, opacity); + } + return false; +} + + +/************************************************************************/ +/* RLE Direct Image */ +/************************************************************************/ + +#if 0 //Enable it when GRAYSCALE image is supported +static bool _rasterCompositeDirectMaskedRleImage(SwSurface* surface, const SwImage* image, SwMask maskOp, uint8_t opacity) +{ + auto span = image->rle->spans; + auto cbuffer = surface->compositor->image.buf8; + auto cstride = surface->compositor->image.stride; + + for (uint32_t i = 0; i < image->rle->size; ++i, ++span) { + auto src = image->buf8 + (span->y + image->oy) * image->stride + (span->x + image->ox); + auto cmp = &cbuffer[span->y * cstride + span->x]; + auto alpha = MULTIPLY(span->coverage, opacity); + if (alpha == 255) { + for (uint32_t x = 0; x < span->len; ++x, ++src, ++cmp) { + *cmp = maskOp(*src, *cmp, ~*src); + } + } else { + for (uint32_t x = 0; x < span->len; ++x, ++src, ++cmp) { + auto tmp = MULTIPLY(*src, alpha); + *cmp = maskOp(*src, *cmp, ~tmp); + } + } + } + return _compositeMaskImage(surface, &surface->compositor->image, surface->compositor->bbox); +} + + +static bool _rasterDirectDirectMaskedRleImage(SwSurface* surface, const SwImage* image, SwMask maskOp, uint8_t opacity) +{ + auto span = image->rle->spans; + auto cbuffer = surface->compositor->image.buf8; + auto cstride = surface->compositor->image.stride; + + for (uint32_t i = 0; i < image->rle->size; ++i, ++span) { + auto src = image->buf8 + (span->y + image->oy) * image->stride + (span->x + image->ox); + auto cmp = &cbuffer[span->y * cstride + span->x]; + auto dst = &surface->buf8[span->y * surface->stride + span->x]; + auto alpha = MULTIPLY(span->coverage, opacity); + if (alpha == 255) { + for (uint32_t x = 0; x < span->len; ++x, ++src, ++cmp, ++dst) { + auto tmp = maskOp(*src, *cmp, 0); //not use alpha + *dst = INTERPOLATE8(tmp, *dst, (255 - tmp)); + } + } else { + for (uint32_t x = 0; x < span->len; ++x, ++src, ++cmp, ++dst) { + auto tmp = maskOp(MULTIPLY(*src, alpha), *cmp, 0); //not use alpha + *dst = INTERPOLATE8(tmp, *dst, (255 - tmp)); + } + } + } + return true; +} +#endif + +static bool _rasterDirectMaskedRleImage(SwSurface* surface, const SwImage* image, uint8_t opacity) +{ +#if 0 //Enable it when GRAYSCALE image is supported + TVGLOG("SW_ENGINE", "Direct Masked(%d) Rle Image", (int)surface->compositor->method); + + //8bit masking channels composition + if (surface->channelSize != sizeof(uint8_t)) return false; + + auto maskOp = _getMaskOp(surface->compositor->method); + if (_direct(surface->compositor->method)) _rasterDirectDirectMaskedRleImage(surface, image, maskOp, opacity); + else return _rasterCompositeDirectMaskedRleImage(surface, image, maskOp, opacity); +#endif + return false; +} + + +static bool _rasterDirectMattedRleImage(SwSurface* surface, const SwImage* image, uint8_t opacity) +{ + TVGLOG("SW_ENGINE", "Direct Matted(%d) Rle Image", (int)surface->compositor->method); + + auto span = image->rle->spans; + auto csize = surface->compositor->image.channelSize; + auto cbuffer = surface->compositor->image.buf8; + auto alpha = surface->alpha(surface->compositor->method); + + for (uint32_t i = 0; i < image->rle->size; ++i, ++span) { + auto dst = &surface->buf32[span->y * surface->stride + span->x]; + auto cmp = &cbuffer[(span->y * surface->compositor->image.stride + span->x) * csize]; + auto img = image->buf32 + (span->y + image->oy) * image->stride + (span->x + image->ox); + auto a = MULTIPLY(span->coverage, opacity); + if (a == 255) { + for (uint32_t x = 0; x < span->len; ++x, ++dst, ++img, cmp += csize) { + auto tmp = ALPHA_BLEND(*img, alpha(cmp)); + *dst = tmp + ALPHA_BLEND(*dst, IA(tmp)); + } + } else { + for (uint32_t x = 0; x < span->len; ++x, ++dst, ++img, cmp += csize) { + auto tmp = ALPHA_BLEND(*img, MULTIPLY(a, alpha(cmp))); + *dst = tmp + ALPHA_BLEND(*dst, IA(tmp)); + } + } + } + return true; +} + + +static bool _rasterDirectBlendingRleImage(SwSurface* surface, const SwImage* image, uint8_t opacity) +{ + auto span = image->rle->spans; + + for (uint32_t i = 0; i < image->rle->size; ++i, ++span) { + auto dst = &surface->buf32[span->y * surface->stride + span->x]; + auto img = image->buf32 + (span->y + image->oy) * image->stride + (span->x + image->ox); + auto alpha = MULTIPLY(span->coverage, opacity); + if (alpha == 255) { + for (uint32_t x = 0; x < span->len; ++x, ++dst, ++img) { + *dst = surface->blender(*img, *dst, IA(*img)); + } + } else if (opacity == 255) { + for (uint32_t x = 0; x < span->len; ++x, ++dst, ++img) { + auto tmp = surface->blender(*img, *dst, 255); + *dst = INTERPOLATE(tmp, *dst, MULTIPLY(span->coverage, A(*img))); + } + } else { + for (uint32_t x = 0; x < span->len; ++x, ++dst, ++img) { + auto src = ALPHA_BLEND(*img, opacity); + auto tmp = surface->blender(src, *dst, IA(src)); + *dst = INTERPOLATE(tmp, *dst, MULTIPLY(span->coverage, A(src))); + } + } + } + return true; +} + + +static bool _rasterDirectRleImage(SwSurface* surface, const SwImage* image, uint8_t opacity) +{ + auto span = image->rle->spans; + + for (uint32_t i = 0; i < image->rle->size; ++i, ++span) { + auto dst = &surface->buf32[span->y * surface->stride + span->x]; + auto img = image->buf32 + (span->y + image->oy) * image->stride + (span->x + image->ox); + auto alpha = MULTIPLY(span->coverage, opacity); + if (alpha == 255) { + for (uint32_t x = 0; x < span->len; ++x, ++dst, ++img) { + *dst = *img + ALPHA_BLEND(*dst, IA(*img)); + } + } else { + for (uint32_t x = 0; x < span->len; ++x, ++dst, ++img) { + auto src = ALPHA_BLEND(*img, alpha); + *dst = src + ALPHA_BLEND(*dst, IA(src)); + } + } + } + return true; +} + + +static bool _directRleImage(SwSurface* surface, const SwImage* image, uint8_t opacity) +{ + if (surface->channelSize == sizeof(uint8_t)) { + TVGERR("SW_ENGINE", "Not supported grayscale rle image!"); + return false; + } + + if (_compositing(surface)) { + if (_matting(surface)) return _rasterDirectMattedRleImage(surface, image, opacity); + else return _rasterDirectMaskedRleImage(surface, image, opacity); + } else if (_blending(surface)) { + return _rasterDirectBlendingRleImage(surface, image, opacity); + } else { + return _rasterDirectRleImage(surface, image, opacity); + } + return false; +} + + +/************************************************************************/ +/*Scaled Image */ +/************************************************************************/ + +#if 0 //Enable it when GRAYSCALE image is supported +static bool _rasterCompositeScaledMaskedImage(SwSurface* surface, const SwImage* image, const Matrix* itransform, const SwBBox& region, SwMask maskOp, uint8_t opacity) +{ + auto scaleMethod = image->scale < DOWN_SCALE_TOLERANCE ? _interpDownScaler : _interpUpScaler; + auto sampleSize = _sampleSize(image->scale); + auto cstride = surface->compositor->image.stride; + auto cbuffer = surface->compositor->image.buf8 + (region.min.y * cstride + region.min.x); + int32_t miny = 0, maxy = 0; + + for (auto y = region.min.y; y < region.max.y; ++y) { + SCALED_IMAGE_RANGE_Y(y) + auto cmp = cbuffer; + for (auto x = region.min.x; x < region.max.x; ++x, ++cmp) { + SCALED_IMAGE_RANGE_X + auto src = scaleMethod(image->buf8, image->stride, image->w, image->h, sx, sy, miny, maxy, sampleSize); + if (opacity < 255) src = MULTIPLY(src, opacity); + *cmp = maskOp(src, *cmp, ~src); + } + cbuffer += cstride; + } + return _compositeMaskImage(surface, &surface->compositor->image, surface->compositor->bbox); +} + + +static bool _rasterDirectScaledMaskedImage(SwSurface* surface, const SwImage* image, const Matrix* itransform, const SwBBox& region, SwMask maskOp, uint8_t opacity) +{ + auto scaleMethod = image->scale < DOWN_SCALE_TOLERANCE ? _interpDownScaler : _interpUpScaler; + auto sampleSize = _sampleSize(image->scale); + auto cstride = surface->compositor->image.stride; + auto cbuffer = surface->compositor->image.buf8 + (region.min.y * cstride + region.min.x); + auto dbuffer = surface->buf8 + (region.min.y * surface->stride + region.min.x); + int32_t miny = 0, maxy = 0; + + for (auto y = region.min.y; y < region.max.y; ++y) { + SCALED_IMAGE_RANGE_Y(y) + auto cmp = cbuffer; + auto dst = dbuffer; + for (auto x = region.min.x; x < region.max.x; ++x, ++cmp, ++dst) { + SCALED_IMAGE_RANGE_X + auto src = scaleMethod(image->buf8, image->stride, image->w, image->h, sx, sy, miny, maxy, sampleSize); + if (opacity < 255) src = MULTIPLY(src, opacity); + src = maskOp(src, *cmp, 0); //not use alpha + *dst = src + MULTIPLY(*dst, ~src); + } + cbuffer += cstride; + dbuffer += surface->stride; + } + return true; +} +#endif + +static bool _rasterScaledMaskedImage(SwSurface* surface, const SwImage* image, const Matrix* itransform, const SwBBox& region, uint8_t opacity) +{ +#if 0 //Enable it when GRAYSCALE image is supported + TVGLOG("SW_ENGINE", "Scaled Masked(%d) Image [Region: %lu %lu %lu %lu]", (int)surface->compositor->method, region.min.x, region.min.y, region.max.x - region.min.x, region.max.y - region.min.y); + + auto maskOp = _getMaskOp(surface->compositor->method); + if (_direct(surface->compositor->method)) return _rasterDirectScaledMaskedImage(surface, image, itransform, region, maskOp, opacity); + else return _rasterCompositeScaledMaskedImage(surface, image, itransform, region, maskOp, opacity); +#endif + return false; +} + + +static bool _rasterScaledMattedImage(SwSurface* surface, const SwImage* image, const Matrix* itransform, const SwBBox& region, uint8_t opacity) +{ + auto dbuffer = surface->buf32 + (region.min.y * surface->stride + region.min.x); + auto csize = surface->compositor->image.channelSize; + auto cbuffer = surface->compositor->image.buf8 + (region.min.y * surface->compositor->image.stride + region.min.x) * csize; + auto alpha = surface->alpha(surface->compositor->method); + + TVGLOG("SW_ENGINE", "Scaled Matted(%d) Image [Region: %lu %lu %lu %lu]", (int)surface->compositor->method, region.min.x, region.min.y, region.max.x - region.min.x, region.max.y - region.min.y); + + auto scaleMethod = image->scale < DOWN_SCALE_TOLERANCE ? _interpDownScaler : _interpUpScaler; + auto sampleSize = _sampleSize(image->scale); + int32_t miny = 0, maxy = 0; + + for (auto y = region.min.y; y < region.max.y; ++y) { + SCALED_IMAGE_RANGE_Y(y) + auto dst = dbuffer; + auto cmp = cbuffer; + for (auto x = region.min.x; x < region.max.x; ++x, ++dst, cmp += csize) { + SCALED_IMAGE_RANGE_X + auto src = scaleMethod(image->buf32, image->stride, image->w, image->h, sx, sy, miny, maxy, sampleSize); + auto tmp = ALPHA_BLEND(src, opacity == 255 ? alpha(cmp) : MULTIPLY(opacity, alpha(cmp))); + *dst = tmp + ALPHA_BLEND(*dst, IA(tmp)); + } + dbuffer += surface->stride; + cbuffer += surface->compositor->image.stride * csize; + } + return true; +} + + +static bool _rasterScaledBlendingImage(SwSurface* surface, const SwImage* image, const Matrix* itransform, const SwBBox& region, uint8_t opacity) +{ + auto dbuffer = surface->buf32 + (region.min.y * surface->stride + region.min.x); + auto scaleMethod = image->scale < DOWN_SCALE_TOLERANCE ? _interpDownScaler : _interpUpScaler; + auto sampleSize = _sampleSize(image->scale); + int32_t miny = 0, maxy = 0; + + for (auto y = region.min.y; y < region.max.y; ++y, dbuffer += surface->stride) { + SCALED_IMAGE_RANGE_Y(y) + auto dst = dbuffer; + for (auto x = region.min.x; x < region.max.x; ++x, ++dst) { + SCALED_IMAGE_RANGE_X + auto src = scaleMethod(image->buf32, image->stride, image->w, image->h, sx, sy, miny, maxy, sampleSize); + if (opacity < 255) ALPHA_BLEND(src, opacity); + auto tmp = surface->blender(src, *dst, 255); + *dst = INTERPOLATE(tmp, *dst, A(src)); + } + } + return true; +} + + +static bool _rasterScaledImage(SwSurface* surface, const SwImage* image, const Matrix* itransform, const SwBBox& region, uint8_t opacity) +{ + auto dbuffer = surface->buf32 + (region.min.y * surface->stride + region.min.x); + auto scaleMethod = image->scale < DOWN_SCALE_TOLERANCE ? _interpDownScaler : _interpUpScaler; + auto sampleSize = _sampleSize(image->scale); + int32_t miny = 0, maxy = 0; + + for (auto y = region.min.y; y < region.max.y; ++y, dbuffer += surface->stride) { + SCALED_IMAGE_RANGE_Y(y) + auto dst = dbuffer; + for (auto x = region.min.x; x < region.max.x; ++x, ++dst) { + SCALED_IMAGE_RANGE_X + auto src = scaleMethod(image->buf32, image->stride, image->w, image->h, sx, sy, miny, maxy, sampleSize); + if (opacity < 255) src = ALPHA_BLEND(src, opacity); + *dst = src + ALPHA_BLEND(*dst, IA(src)); + } + } + return true; +} + + +static bool _scaledImage(SwSurface* surface, const SwImage* image, const Matrix* transform, const SwBBox& region, uint8_t opacity) +{ + if (surface->channelSize == sizeof(uint8_t)) { + TVGERR("SW_ENGINE", "Not supported grayscale Textmap polygon mesh!"); + return false; + } + + Matrix itransform; + + if (transform) { + if (!mathInverse(transform, &itransform)) return false; + } else mathIdentity(&itransform); + + if (_compositing(surface)) { + if (_matting(surface)) return _rasterScaledMattedImage(surface, image, &itransform, region, opacity); + else return _rasterScaledMaskedImage(surface, image, &itransform, region, opacity); + } else if (_blending(surface)) { + return _rasterScaledBlendingImage(surface, image, &itransform, region, opacity); + } else { + return _rasterScaledImage(surface, image, &itransform, region, opacity); + } + return false; +} + + +/************************************************************************/ +/* Direct Image */ +/************************************************************************/ + +#if 0 //Enable it when GRAYSCALE image is supported +static bool _rasterCompositeDirectMaskedImage(SwSurface* surface, const SwImage* image, const SwBBox& region, SwMask maskOp, uint8_t opacity) +{ + auto h = static_cast(region.max.y - region.min.y); + auto w = static_cast(region.max.x - region.min.x); + auto cstride = surface->compositor->image.stride; + + auto cbuffer = surface->compositor->image.buf8 + (region.min.y * cstride + region.min.x); //compositor buffer + auto sbuffer = image->buf8 + (region.min.y + image->oy) * image->stride + (region.min.x + image->ox); + + for (uint32_t y = 0; y < h; ++y) { + auto cmp = cbuffer; + auto src = sbuffer; + if (opacity == 255) { + for (uint32_t x = 0; x < w; ++x, ++src, ++cmp) { + *cmp = maskOp(*src, *cmp, ~*src); + } + } else { + for (uint32_t x = 0; x < w; ++x, ++src, ++cmp) { + auto tmp = MULTIPLY(*src, opacity); + *cmp = maskOp(tmp, *cmp, ~tmp); + } + } + cbuffer += cstride; + sbuffer += image->stride; + } + return _compositeMaskImage(surface, &surface->compositor->image, surface->compositor->bbox); +} + + +static bool _rasterDirectDirectMaskedImage(SwSurface* surface, const SwImage* image, const SwBBox& region, SwMask maskOp, uint8_t opacity) +{ + auto h = static_cast(region.max.y - region.min.y); + auto w = static_cast(region.max.x - region.min.x); + auto cstride = surface->compositor->image.stride; + + auto cbuffer = surface->compositor->image.buf32 + (region.min.y * cstride + region.min.x); //compositor buffer + auto dbuffer = surface->buf8 + (region.min.y * surface->stride + region.min.x); //destination buffer + auto sbuffer = image->buf8 + (region.min.y + image->oy) * image->stride + (region.min.x + image->ox); + + for (uint32_t y = 0; y < h; ++y) { + auto cmp = cbuffer; + auto dst = dbuffer; + auto src = sbuffer; + if (opacity == 255) { + for (uint32_t x = 0; x < w; ++x, ++src, ++cmp, ++dst) { + auto tmp = maskOp(*src, *cmp, 0); //not use alpha + *dst = tmp + MULTIPLY(*dst, ~tmp); + } + } else { + for (uint32_t x = 0; x < w; ++x, ++src, ++cmp, ++dst) { + auto tmp = maskOp(MULTIPLY(*src, opacity), *cmp, 0); //not use alpha + *dst = tmp + MULTIPLY(*dst, ~tmp); + } + } + cbuffer += cstride; + dbuffer += surface->stride; + sbuffer += image->stride; + } + return true; +} +#endif + +static bool _rasterDirectMaskedImage(SwSurface* surface, const SwImage* image, const SwBBox& region, uint8_t opacity) +{ + TVGERR("SW_ENGINE", "Not Supported: Direct Masked(%d) Image [Region: %lu %lu %lu %lu]", (int)surface->compositor->method, region.min.x, region.min.y, region.max.x - region.min.x, region.max.y - region.min.y); + +#if 0 //Enable it when GRAYSCALE image is supported + auto maskOp = _getMaskOp(surface->compositor->method); + if (_direct(surface->compositor->method)) return _rasterDirectDirectMaskedImage(surface, image, region, maskOp, opacity); + else return _rasterCompositeDirectMaskedImage(surface, image, region, maskOp, opacity); +#endif + return false; +} + + +static bool _rasterDirectMattedImage(SwSurface* surface, const SwImage* image, const SwBBox& region, uint8_t opacity) +{ + auto h = static_cast(region.max.y - region.min.y); + auto w = static_cast(region.max.x - region.min.x); + auto csize = surface->compositor->image.channelSize; + auto alpha = surface->alpha(surface->compositor->method); + auto sbuffer = image->buf32 + (region.min.y + image->oy) * image->stride + (region.min.x + image->ox); + auto cbuffer = surface->compositor->image.buf8 + (region.min.y * surface->compositor->image.stride + region.min.x) * csize; //compositor buffer + + TVGLOG("SW_ENGINE", "Direct Matted(%d) Image [Region: %lu %lu %u %u]", (int)surface->compositor->method, region.min.x, region.min.y, w, h); + + //32 bits + if (surface->channelSize == sizeof(uint32_t)) { + auto buffer = surface->buf32 + (region.min.y * surface->stride) + region.min.x; + for (uint32_t y = 0; y < h; ++y) { + auto dst = buffer; + auto cmp = cbuffer; + auto src = sbuffer; + if (opacity == 255) { + for (uint32_t x = 0; x < w; ++x, ++dst, ++src, cmp += csize) { + auto tmp = ALPHA_BLEND(*src, alpha(cmp)); + *dst = tmp + ALPHA_BLEND(*dst, IA(tmp)); + } + } else { + for (uint32_t x = 0; x < w; ++x, ++dst, ++src, cmp += csize) { + auto tmp = ALPHA_BLEND(*src, MULTIPLY(opacity, alpha(cmp))); + *dst = tmp + ALPHA_BLEND(*dst, IA(tmp)); + } + } + buffer += surface->stride; + cbuffer += surface->compositor->image.stride * csize; + sbuffer += image->stride; + } + //8 bits + } else if (surface->channelSize == sizeof(uint8_t)) { + auto buffer = surface->buf8 + (region.min.y * surface->stride) + region.min.x; + for (uint32_t y = 0; y < h; ++y) { + auto dst = buffer; + auto cmp = cbuffer; + auto src = sbuffer; + if (opacity == 255) { + for (uint32_t x = 0; x < w; ++x, ++dst, ++src, cmp += csize) { + *dst = MULTIPLY(A(*src), alpha(cmp)); + } + } else { + for (uint32_t x = 0; x < w; ++x, ++dst, ++src, cmp += csize) { + *dst = MULTIPLY(A(*src), MULTIPLY(opacity, alpha(cmp))); + } + } + buffer += surface->stride; + cbuffer += surface->compositor->image.stride * csize; + sbuffer += image->stride; + } + } + return true; +} + + +static bool _rasterDirectBlendingImage(SwSurface* surface, const SwImage* image, const SwBBox& region, uint8_t opacity) +{ + if (surface->channelSize == sizeof(uint8_t)) { + TVGERR("SW_ENGINE", "Not supported grayscale image!"); + return false; + } + + auto dbuffer = &surface->buf32[region.min.y * surface->stride + region.min.x]; + auto sbuffer = image->buf32 + (region.min.y + image->oy) * image->stride + (region.min.x + image->ox); + + for (auto y = region.min.y; y < region.max.y; ++y) { + auto dst = dbuffer; + auto src = sbuffer; + if (opacity == 255) { + for (auto x = region.min.x; x < region.max.x; x++, dst++, src++) { + auto tmp = surface->blender(*src, *dst, 255); + *dst = INTERPOLATE(tmp, *dst, A(*src)); + } + } else { + for (auto x = region.min.x; x < region.max.x; ++x, ++dst, ++src) { + auto tmp = ALPHA_BLEND(*src, opacity); + auto tmp2 = surface->blender(tmp, *dst, 255); + *dst = INTERPOLATE(tmp2, *dst, A(tmp)); + } + } + dbuffer += surface->stride; + sbuffer += image->stride; + } + return true; +} + + +static bool _rasterDirectImage(SwSurface* surface, const SwImage* image, const SwBBox& region, uint8_t opacity) +{ + if (surface->channelSize == sizeof(uint8_t)) { + TVGERR("SW_ENGINE", "Not supported grayscale image!"); + return false; + } + + auto dbuffer = &surface->buf32[region.min.y * surface->stride + region.min.x]; + auto sbuffer = image->buf32 + (region.min.y + image->oy) * image->stride + (region.min.x + image->ox); + + for (auto y = region.min.y; y < region.max.y; ++y) { + auto dst = dbuffer; + auto src = sbuffer; + if (opacity == 255) { + for (auto x = region.min.x; x < region.max.x; x++, dst++, src++) { + *dst = *src + ALPHA_BLEND(*dst, IA(*src)); + } + } else { + for (auto x = region.min.x; x < region.max.x; ++x, ++dst, ++src) { + auto tmp = ALPHA_BLEND(*src, opacity); + *dst = tmp + ALPHA_BLEND(*dst, IA(tmp)); + } + } + dbuffer += surface->stride; + sbuffer += image->stride; + } + return true; +} + + +//Blenders for the following scenarios: [Composition / Non-Composition] * [Opaque / Translucent] +static bool _directImage(SwSurface* surface, const SwImage* image, const SwBBox& region, uint8_t opacity) +{ + if (_compositing(surface)) { + if (_matting(surface)) return _rasterDirectMattedImage(surface, image, region, opacity); + else return _rasterDirectMaskedImage(surface, image, region, opacity); + } else if (_blending(surface)) { + return _rasterDirectBlendingImage(surface, image, region, opacity); + } else { + return _rasterDirectImage(surface, image, region, opacity); + } + return false; +} + + +//Blenders for the following scenarios: [RLE / Whole] * [Direct / Scaled / Transformed] +static bool _rasterImage(SwSurface* surface, SwImage* image, const Matrix* transform, const SwBBox& region, uint8_t opacity) +{ + //RLE Image + if (image->rle) { + if (image->direct) return _directRleImage(surface, image, opacity); + else if (image->scaled) return _scaledRleImage(surface, image, transform, region, opacity); + else return _rasterTexmapPolygon(surface, image, transform, nullptr, opacity); + //Whole Image + } else { + if (image->direct) return _directImage(surface, image, region, opacity); + else if (image->scaled) return _scaledImage(surface, image, transform, region, opacity); + else return _rasterTexmapPolygon(surface, image, transform, ®ion, opacity); + } +} + + +/************************************************************************/ +/* Rect Gradient */ +/************************************************************************/ + +template +static bool _rasterCompositeGradientMaskedRect(SwSurface* surface, const SwBBox& region, const SwFill* fill, SwMask maskOp) +{ + auto h = static_cast(region.max.y - region.min.y); + auto w = static_cast(region.max.x - region.min.x); + auto cstride = surface->compositor->image.stride; + auto cbuffer = surface->compositor->image.buf8 + (region.min.y * cstride + region.min.x); + + for (uint32_t y = 0; y < h; ++y) { + fillMethod()(fill, cbuffer, region.min.y + y, region.min.x, w, maskOp, 255); + cbuffer += surface->stride; + } + return _compositeMaskImage(surface, &surface->compositor->image, surface->compositor->bbox); +} + + +template +static bool _rasterDirectGradientMaskedRect(SwSurface* surface, const SwBBox& region, const SwFill* fill, SwMask maskOp) +{ + auto h = static_cast(region.max.y - region.min.y); + auto w = static_cast(region.max.x - region.min.x); + auto cstride = surface->compositor->image.stride; + auto cbuffer = surface->compositor->image.buf8 + (region.min.y * cstride + region.min.x); + auto dbuffer = surface->buf8 + (region.min.y * surface->stride + region.min.x); + + for (uint32_t y = 0; y < h; ++y) { + fillMethod()(fill, dbuffer, region.min.y + y, region.min.x, w, cbuffer, maskOp, 255); + cbuffer += cstride; + dbuffer += surface->stride; + } + return true; +} + + +template +static bool _rasterGradientMaskedRect(SwSurface* surface, const SwBBox& region, const SwFill* fill) +{ + auto method = surface->compositor->method; + + TVGLOG("SW_ENGINE", "Masked(%d) Gradient [Region: %lu %lu %lu %lu]", (int)method, region.min.x, region.min.y, region.max.x - region.min.x, region.max.y - region.min.y); + + auto maskOp = _getMaskOp(method); + + if (_direct(method)) return _rasterDirectGradientMaskedRect(surface, region, fill, maskOp); + else return _rasterCompositeGradientMaskedRect(surface, region, fill, maskOp); + + return false; +} + + +template +static bool _rasterGradientMattedRect(SwSurface* surface, const SwBBox& region, const SwFill* fill) +{ + auto buffer = surface->buf32 + (region.min.y * surface->stride) + region.min.x; + auto h = static_cast(region.max.y - region.min.y); + auto w = static_cast(region.max.x - region.min.x); + auto csize = surface->compositor->image.channelSize; + auto cbuffer = surface->compositor->image.buf8 + (region.min.y * surface->compositor->image.stride + region.min.x) * csize; + auto alpha = surface->alpha(surface->compositor->method); + + TVGLOG("SW_ENGINE", "Matted(%d) Gradient [Region: %lu %lu %u %u]", (int)surface->compositor->method, region.min.x, region.min.y, w, h); + + for (uint32_t y = 0; y < h; ++y) { + fillMethod()(fill, buffer, region.min.y + y, region.min.x, w, cbuffer, alpha, csize, 255); + buffer += surface->stride; + cbuffer += surface->stride * csize; + } + return true; +} + + +template +static bool _rasterBlendingGradientRect(SwSurface* surface, const SwBBox& region, const SwFill* fill) +{ + auto buffer = surface->buf32 + (region.min.y * surface->stride) + region.min.x; + auto w = static_cast(region.max.x - region.min.x); + auto h = static_cast(region.max.y - region.min.y); + + if (fill->translucent) { + for (uint32_t y = 0; y < h; ++y) { + fillMethod()(fill, buffer + y * surface->stride, region.min.y + y, region.min.x, w, opBlendPreNormal, surface->blender, 255); + } + } else { + for (uint32_t y = 0; y < h; ++y) { + fillMethod()(fill, buffer + y * surface->stride, region.min.y + y, region.min.x, w, opBlendSrcOver, surface->blender, 255); + } + } + return true; +} + +template +static bool _rasterTranslucentGradientRect(SwSurface* surface, const SwBBox& region, const SwFill* fill) +{ + auto buffer = surface->buf32 + (region.min.y * surface->stride) + region.min.x; + auto h = static_cast(region.max.y - region.min.y); + auto w = static_cast(region.max.x - region.min.x); + + for (uint32_t y = 0; y < h; ++y) { + fillMethod()(fill, buffer, region.min.y + y, region.min.x, w, opBlendPreNormal, 255); + buffer += surface->stride; + } + return true; +} + + +template +static bool _rasterSolidGradientRect(SwSurface* surface, const SwBBox& region, const SwFill* fill) +{ + auto buffer = surface->buf32 + (region.min.y * surface->stride) + region.min.x; + auto w = static_cast(region.max.x - region.min.x); + auto h = static_cast(region.max.y - region.min.y); + + for (uint32_t y = 0; y < h; ++y) { + fillMethod()(fill, buffer + y * surface->stride, region.min.y + y, region.min.x, w, opBlendSrcOver, 255); + } + return true; +} + + +static bool _rasterLinearGradientRect(SwSurface* surface, const SwBBox& region, const SwFill* fill) +{ + if (fill->linear.len < FLOAT_EPSILON) return false; + + if (_compositing(surface)) { + if (_matting(surface)) return _rasterGradientMattedRect(surface, region, fill); + else return _rasterGradientMaskedRect(surface, region, fill); + } else if (_blending(surface)) { + return _rasterBlendingGradientRect(surface, region, fill); + } else { + if (fill->translucent) return _rasterTranslucentGradientRect(surface, region, fill); + else _rasterSolidGradientRect(surface, region, fill); + } + return false; +} + + +static bool _rasterRadialGradientRect(SwSurface* surface, const SwBBox& region, const SwFill* fill) +{ + if (_compositing(surface)) { + if (_matting(surface)) return _rasterGradientMattedRect(surface, region, fill); + else return _rasterGradientMaskedRect(surface, region, fill); + } else if (_blending(surface)) { + return _rasterBlendingGradientRect(surface, region, fill); + } else { + if (fill->translucent) return _rasterTranslucentGradientRect(surface, region, fill); + else _rasterSolidGradientRect(surface, region, fill); + } + return false; +} + + +/************************************************************************/ +/* Rle Gradient */ +/************************************************************************/ + +template +static bool _rasterCompositeGradientMaskedRle(SwSurface* surface, const SwRleData* rle, const SwFill* fill, SwMask maskOp) +{ + auto span = rle->spans; + auto cstride = surface->compositor->image.stride; + auto cbuffer = surface->compositor->image.buf8; + + for (uint32_t i = 0; i < rle->size; ++i, ++span) { + auto cmp = &cbuffer[span->y * cstride + span->x]; + fillMethod()(fill, cmp, span->y, span->x, span->len, maskOp, span->coverage); + } + return _compositeMaskImage(surface, &surface->compositor->image, surface->compositor->bbox); +} + + +template +static bool _rasterDirectGradientMaskedRle(SwSurface* surface, const SwRleData* rle, const SwFill* fill, SwMask maskOp) +{ + auto span = rle->spans; + auto cstride = surface->compositor->image.stride; + auto cbuffer = surface->compositor->image.buf8; + auto dbuffer = surface->buf8; + + for (uint32_t i = 0; i < rle->size; ++i, ++span) { + auto cmp = &cbuffer[span->y * cstride + span->x]; + auto dst = &dbuffer[span->y * surface->stride + span->x]; + fillMethod()(fill, dst, span->y, span->x, span->len, cmp, maskOp, span->coverage); + } + return true; +} + + +template +static bool _rasterGradientMaskedRle(SwSurface* surface, const SwRleData* rle, const SwFill* fill) +{ + auto method = surface->compositor->method; + + TVGLOG("SW_ENGINE", "Masked(%d) Rle Linear Gradient", (int)method); + + auto maskOp = _getMaskOp(method); + + if (_direct(method)) return _rasterDirectGradientMaskedRle(surface, rle, fill, maskOp); + else return _rasterCompositeGradientMaskedRle(surface, rle, fill, maskOp); + return false; +} + + +template +static bool _rasterGradientMattedRle(SwSurface* surface, const SwRleData* rle, const SwFill* fill) +{ + TVGLOG("SW_ENGINE", "Matted(%d) Rle Linear Gradient", (int)surface->compositor->method); + + auto span = rle->spans; + auto csize = surface->compositor->image.channelSize; + auto cbuffer = surface->compositor->image.buf8; + auto alpha = surface->alpha(surface->compositor->method); + + for (uint32_t i = 0; i < rle->size; ++i, ++span) { + auto dst = &surface->buf32[span->y * surface->stride + span->x]; + auto cmp = &cbuffer[(span->y * surface->compositor->image.stride + span->x) * csize]; + fillMethod()(fill, dst, span->y, span->x, span->len, cmp, alpha, csize, span->coverage); + } + return true; +} + + +template +static bool _rasterBlendingGradientRle(SwSurface* surface, const SwRleData* rle, const SwFill* fill) +{ + auto span = rle->spans; + + for (uint32_t i = 0; i < rle->size; ++i, ++span) { + auto dst = &surface->buf32[span->y * surface->stride + span->x]; + fillMethod()(fill, dst, span->y, span->x, span->len, opBlendPreNormal, surface->blender, span->coverage); + } + return true; +} + + +template +static bool _rasterTranslucentGradientRle(SwSurface* surface, const SwRleData* rle, const SwFill* fill) +{ + auto span = rle->spans; + + //32 bits + if (surface->channelSize == sizeof(uint32_t)) { + for (uint32_t i = 0; i < rle->size; ++i, ++span) { + auto dst = &surface->buf32[span->y * surface->stride + span->x]; + if (span->coverage == 255) fillMethod()(fill, dst, span->y, span->x, span->len, opBlendPreNormal, 255); + else fillMethod()(fill, dst, span->y, span->x, span->len, opBlendNormal, span->coverage); + } + //8 bits + } else if (surface->channelSize == sizeof(uint8_t)) { + for (uint32_t i = 0; i < rle->size; ++i, ++span) { + auto dst = &surface->buf8[span->y * surface->stride + span->x]; + fillMethod()(fill, dst, span->y, span->x, span->len, _opMaskAdd, 255); + } + } + return true; +} + + +template +static bool _rasterSolidGradientRle(SwSurface* surface, const SwRleData* rle, const SwFill* fill) +{ + auto span = rle->spans; + + //32 bits + if (surface->channelSize == sizeof(uint32_t)) { + for (uint32_t i = 0; i < rle->size; ++i, ++span) { + auto dst = &surface->buf32[span->y * surface->stride + span->x]; + if (span->coverage == 255) fillMethod()(fill, dst, span->y, span->x, span->len, opBlendSrcOver, 255); + else fillMethod()(fill, dst, span->y, span->x, span->len, opBlendInterp, span->coverage); + } + //8 bits + } else if (surface->channelSize == sizeof(uint8_t)) { + for (uint32_t i = 0; i < rle->size; ++i, ++span) { + auto dst = &surface->buf8[span->y * surface->stride + span->x]; + if (span->coverage == 255) fillMethod()(fill, dst, span->y, span->x, span->len, _opMaskNone, 255); + else fillMethod()(fill, dst, span->y, span->x, span->len, _opMaskAdd, span->coverage); + } + } + + return true; +} + + +static bool _rasterLinearGradientRle(SwSurface* surface, const SwRleData* rle, const SwFill* fill) +{ + if (!rle) return false; + + if (_compositing(surface)) { + if (_matting(surface)) return _rasterGradientMattedRle(surface, rle, fill); + else return _rasterGradientMaskedRle(surface, rle, fill); + } else if (_blending(surface)) { + return _rasterBlendingGradientRle(surface, rle, fill); + } else { + if (fill->translucent) return _rasterTranslucentGradientRle(surface, rle, fill); + else return _rasterSolidGradientRle(surface, rle, fill); + } + return false; +} + + +static bool _rasterRadialGradientRle(SwSurface* surface, const SwRleData* rle, const SwFill* fill) +{ + if (!rle) return false; + + if (_compositing(surface)) { + if (_matting(surface)) return _rasterGradientMattedRle(surface, rle, fill); + else return _rasterGradientMaskedRle(surface, rle, fill); + } else if (_blending(surface)) { + _rasterBlendingGradientRle(surface, rle, fill); + } else { + if (fill->translucent) _rasterTranslucentGradientRle(surface, rle, fill); + else return _rasterSolidGradientRle(surface, rle, fill); + } + return false; +} + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + + +void rasterGrayscale8(uint8_t *dst, uint8_t val, uint32_t offset, int32_t len) +{ +#if defined(THORVG_AVX_VECTOR_SUPPORT) + avxRasterGrayscale8(dst, val, offset, len); +#elif defined(THORVG_NEON_VECTOR_SUPPORT) + neonRasterGrayscale8(dst, val, offset, len); +#else + cRasterPixels(dst, val, offset, len); +#endif +} + + +void rasterPixel32(uint32_t *dst, uint32_t val, uint32_t offset, int32_t len) +{ +#if defined(THORVG_AVX_VECTOR_SUPPORT) + avxRasterPixel32(dst, val, offset, len); +#elif defined(THORVG_NEON_VECTOR_SUPPORT) + neonRasterPixel32(dst, val, offset, len); +#else + cRasterPixels(dst, val, offset, len); +#endif +} + + +bool rasterCompositor(SwSurface* surface) +{ + //See CompositeMethod, Alpha:3, InvAlpha:4, Luma:5, InvLuma:6 + surface->alphas[0] = _alpha; + surface->alphas[1] = _ialpha; + + if (surface->cs == ColorSpace::ABGR8888 || surface->cs == ColorSpace::ABGR8888S) { + surface->join = _abgrJoin; + surface->alphas[2] = _abgrLuma; + surface->alphas[3] = _abgrInvLuma; + } else if (surface->cs == ColorSpace::ARGB8888 || surface->cs == ColorSpace::ARGB8888S) { + surface->join = _argbJoin; + surface->alphas[2] = _argbLuma; + surface->alphas[3] = _argbInvLuma; + } else { + TVGERR("SW_ENGINE", "Unsupported Colorspace(%d) is expected!", surface->cs); + return false; + } + return true; +} + + +bool rasterClear(SwSurface* surface, uint32_t x, uint32_t y, uint32_t w, uint32_t h) +{ + if (!surface || !surface->buf32 || surface->stride == 0 || surface->w == 0 || surface->h == 0) return false; + + //32 bits + if (surface->channelSize == sizeof(uint32_t)) { + //full clear + if (w == surface->stride) { + rasterPixel32(surface->buf32, 0x00000000, surface->stride * y, w * h); + //partial clear + } else { + for (uint32_t i = 0; i < h; i++) { + rasterPixel32(surface->buf32, 0x00000000, (surface->stride * y + x) + (surface->stride * i), w); + } + } + //8 bits + } else if (surface->channelSize == sizeof(uint8_t)) { + //full clear + if (w == surface->stride) { + rasterGrayscale8(surface->buf8, 0x00, surface->stride * y, w * h); + //partial clear + } else { + for (uint32_t i = 0; i < h; i++) { + rasterGrayscale8(surface->buf8, 0x00, (surface->stride * y + x) + (surface->stride * i), w); + } + } + } + return true; +} + + +void rasterUnpremultiply(Surface* surface) +{ + if (surface->channelSize != sizeof(uint32_t)) return; + + TVGLOG("SW_ENGINE", "Unpremultiply [Size: %d x %d]", surface->w, surface->h); + + //OPTIMIZE_ME: +SIMD + for (uint32_t y = 0; y < surface->h; y++) { + auto buffer = surface->buf32 + surface->stride * y; + for (uint32_t x = 0; x < surface->w; ++x) { + uint8_t a = buffer[x] >> 24; + if (a == 255) { + continue; + } else if (a == 0) { + buffer[x] = 0x00ffffff; + } else { + uint16_t r = ((buffer[x] >> 8) & 0xff00) / a; + uint16_t g = ((buffer[x]) & 0xff00) / a; + uint16_t b = ((buffer[x] << 8) & 0xff00) / a; + if (r > 0xff) r = 0xff; + if (g > 0xff) g = 0xff; + if (b > 0xff) b = 0xff; + buffer[x] = (a << 24) | (r << 16) | (g << 8) | (b); + } + } + } + surface->premultiplied = false; +} + + +void rasterPremultiply(Surface* surface) +{ + ScopedLock lock(surface->key); + if (surface->premultiplied || (surface->channelSize != sizeof(uint32_t))) return; + surface->premultiplied = true; + + TVGLOG("SW_ENGINE", "Premultiply [Size: %d x %d]", surface->w, surface->h); + + //OPTIMIZE_ME: +SIMD + auto buffer = surface->buf32; + for (uint32_t y = 0; y < surface->h; ++y, buffer += surface->stride) { + auto dst = buffer; + for (uint32_t x = 0; x < surface->w; ++x, ++dst) { + auto c = *dst; + auto a = (c >> 24); + *dst = (c & 0xff000000) + ((((c >> 8) & 0xff) * a) & 0xff00) + ((((c & 0x00ff00ff) * a) >> 8) & 0x00ff00ff); + } + } +} + + +bool rasterGradientShape(SwSurface* surface, SwShape* shape, unsigned id) +{ + if (!shape->fill) return false; + + if (shape->fastTrack) { + if (id == TVG_CLASS_ID_LINEAR) return _rasterLinearGradientRect(surface, shape->bbox, shape->fill); + else if (id == TVG_CLASS_ID_RADIAL)return _rasterRadialGradientRect(surface, shape->bbox, shape->fill); + } else { + if (id == TVG_CLASS_ID_LINEAR) return _rasterLinearGradientRle(surface, shape->rle, shape->fill); + else if (id == TVG_CLASS_ID_RADIAL) return _rasterRadialGradientRle(surface, shape->rle, shape->fill); + } + return false; +} + + +bool rasterGradientStroke(SwSurface* surface, SwShape* shape, unsigned id) +{ + if (!shape->stroke || !shape->stroke->fill || !shape->strokeRle) return false; + + if (id == TVG_CLASS_ID_LINEAR) return _rasterLinearGradientRle(surface, shape->strokeRle, shape->stroke->fill); + else if (id == TVG_CLASS_ID_RADIAL) return _rasterRadialGradientRle(surface, shape->strokeRle, shape->stroke->fill); + + return false; +} + + +bool rasterShape(SwSurface* surface, SwShape* shape, uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ + if (a < 255) { + r = MULTIPLY(r, a); + g = MULTIPLY(g, a); + b = MULTIPLY(b, a); + } + if (shape->fastTrack) return _rasterRect(surface, shape->bbox, r, g, b, a); + else return _rasterRle(surface, shape->rle, r, g, b, a); +} + + +bool rasterStroke(SwSurface* surface, SwShape* shape, uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ + if (a < 255) { + r = MULTIPLY(r, a); + g = MULTIPLY(g, a); + b = MULTIPLY(b, a); + } + + return _rasterRle(surface, shape->strokeRle, r, g, b, a); +} + + +bool rasterImage(SwSurface* surface, SwImage* image, const RenderMesh* mesh, const Matrix* transform, const SwBBox& bbox, uint8_t opacity) +{ + //Outside of the viewport, skip the rendering + if (bbox.max.x < 0 || bbox.max.y < 0 || bbox.min.x >= static_cast(surface->w) || bbox.min.y >= static_cast(surface->h)) return true; + + if (mesh && mesh->triangleCnt > 0) return _rasterTexmapPolygonMesh(surface, image, mesh, transform, &bbox, opacity); + else return _rasterImage(surface, image, transform, bbox, opacity); +} + + +bool rasterConvertCS(Surface* surface, ColorSpace to) +{ + ScopedLock lock(surface->key); + if (surface->cs == to) return true; + + //TODO: Support SIMD accelerations + auto from = surface->cs; + + if (((from == ColorSpace::ABGR8888) || (from == ColorSpace::ABGR8888S)) && ((to == ColorSpace::ARGB8888) || (to == ColorSpace::ARGB8888S))) { + surface->cs = to; + return cRasterABGRtoARGB(surface); + } + if (((from == ColorSpace::ARGB8888) || (from == ColorSpace::ARGB8888S)) && ((to == ColorSpace::ABGR8888) || (to == ColorSpace::ABGR8888S))) { + surface->cs = to; + return cRasterARGBtoABGR(surface); + } + return false; +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSwRasterAvx.h b/include/liblvgl/libs/thorvg/tvgSwRasterAvx.h new file mode 100644 index 00000000..cc170de9 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSwRasterAvx.h @@ -0,0 +1,214 @@ +/* + * Copyright (c) 2021 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifdef THORVG_AVX_VECTOR_SUPPORT + +#include + +#define N_32BITS_IN_128REG 4 +#define N_32BITS_IN_256REG 8 + +static inline __m128i ALPHA_BLEND(__m128i c, __m128i a) +{ + //1. set the masks for the A/G and R/B channels + auto AG = _mm_set1_epi32(0xff00ff00); + auto RB = _mm_set1_epi32(0x00ff00ff); + + //2. mask the alpha vector - originally quartet [a, a, a, a] + auto aAG = _mm_and_si128(a, AG); + auto aRB = _mm_and_si128(a, RB); + + //3. calculate the alpha blending of the 2nd and 4th channel + //- mask the color vector + //- multiply it by the masked alpha vector + //- add the correction to compensate bit shifting used instead of dividing by 255 + //- shift bits - corresponding to division by 256 + auto even = _mm_and_si128(c, RB); + even = _mm_mullo_epi16(even, aRB); + even =_mm_add_epi16(even, RB); + even = _mm_srli_epi16(even, 8); + + //4. calculate the alpha blending of the 1st and 3rd channel: + //- mask the color vector + //- multiply it by the corresponding masked alpha vector and store the high bits of the result + //- add the correction to compensate division by 256 instead of by 255 (next step) + //- remove the low 8 bits to mimic the division by 256 + auto odd = _mm_and_si128(c, AG); + odd = _mm_mulhi_epu16(odd, aAG); + odd = _mm_add_epi16(odd, RB); + odd = _mm_and_si128(odd, AG); + + //5. the final result + return _mm_or_si128(odd, even); +} + + +static void avxRasterGrayscale8(uint8_t* dst, uint8_t val, uint32_t offset, int32_t len) +{ + dst += offset; + + __m256i vecVal = _mm256_set1_epi8(val); + + int32_t i = 0; + for (; i <= len - 32; i += 32) { + _mm256_storeu_si256((__m256i*)(dst + i), vecVal); + } + + for (; i < len; ++i) { + dst[i] = val; + } +} + + +static void avxRasterPixel32(uint32_t *dst, uint32_t val, uint32_t offset, int32_t len) +{ + //1. calculate how many iterations we need to cover the length + uint32_t iterations = len / N_32BITS_IN_256REG; + uint32_t avxFilled = iterations * N_32BITS_IN_256REG; + + //2. set the beginning of the array + dst += offset; + + //3. fill the octets + for (uint32_t i = 0; i < iterations; ++i, dst += N_32BITS_IN_256REG) { + _mm256_storeu_si256((__m256i*)dst, _mm256_set1_epi32(val)); + } + + //4. fill leftovers (in the first step we have to set the pointer to the place where the avx job is done) + int32_t leftovers = len - avxFilled; + while (leftovers--) *dst++ = val; +} + + +static bool avxRasterTranslucentRect(SwSurface* surface, const SwBBox& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ + if (surface->channelSize != sizeof(uint32_t)) { + TVGERR("SW_ENGINE", "Unsupported Channel Size = %d", surface->channelSize); + return false; + } + + auto color = surface->join(r, g, b, a); + auto buffer = surface->buf32 + (region.min.y * surface->stride) + region.min.x; + auto h = static_cast(region.max.y - region.min.y); + auto w = static_cast(region.max.x - region.min.x); + + uint32_t ialpha = 255 - a; + + auto avxColor = _mm_set1_epi32(color); + auto avxIalpha = _mm_set1_epi8(ialpha); + + for (uint32_t y = 0; y < h; ++y) { + auto dst = &buffer[y * surface->stride]; + + //1. fill the not aligned memory (for 128-bit registers a 16-bytes alignment is required) + auto notAligned = ((uintptr_t)dst & 0xf) / 4; + if (notAligned) { + notAligned = (N_32BITS_IN_128REG - notAligned > w ? w : N_32BITS_IN_128REG - notAligned); + for (uint32_t x = 0; x < notAligned; ++x, ++dst) { + *dst = color + ALPHA_BLEND(*dst, ialpha); + } + } + + //2. fill the aligned memory - N_32BITS_IN_128REG pixels processed at once + uint32_t iterations = (w - notAligned) / N_32BITS_IN_128REG; + uint32_t avxFilled = iterations * N_32BITS_IN_128REG; + auto avxDst = (__m128i*)dst; + for (uint32_t x = 0; x < iterations; ++x, ++avxDst) { + *avxDst = _mm_add_epi32(avxColor, ALPHA_BLEND(*avxDst, avxIalpha)); + } + + //3. fill the remaining pixels + int32_t leftovers = w - notAligned - avxFilled; + dst += avxFilled; + while (leftovers--) { + *dst = color + ALPHA_BLEND(*dst, ialpha); + dst++; + } + } + return true; +} + + +static bool avxRasterTranslucentRle(SwSurface* surface, const SwRleData* rle, uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ + if (surface->channelSize != sizeof(uint32_t)) { + TVGERR("SW_ENGINE", "Unsupported Channel Size = %d", surface->channelSize); + return false; + } + + auto color = surface->join(r, g, b, a); + auto span = rle->spans; + uint32_t src; + + for (uint32_t i = 0; i < rle->size; ++i) { + auto dst = &surface->buf32[span->y * surface->stride + span->x]; + + if (span->coverage < 255) src = ALPHA_BLEND(color, span->coverage); + else src = color; + + auto ialpha = IA(src); + + //1. fill the not aligned memory (for 128-bit registers a 16-bytes alignment is required) + auto notAligned = ((uintptr_t)dst & 0xf) / 4; + if (notAligned) { + notAligned = (N_32BITS_IN_128REG - notAligned > span->len ? span->len : N_32BITS_IN_128REG - notAligned); + for (uint32_t x = 0; x < notAligned; ++x, ++dst) { + *dst = src + ALPHA_BLEND(*dst, ialpha); + } + } + + //2. fill the aligned memory using avx - N_32BITS_IN_128REG pixels processed at once + //In order to avoid unnecessary avx variables declarations a check is made whether there are any iterations at all + uint32_t iterations = (span->len - notAligned) / N_32BITS_IN_128REG; + uint32_t avxFilled = 0; + if (iterations > 0) { + auto avxSrc = _mm_set1_epi32(src); + auto avxIalpha = _mm_set1_epi8(ialpha); + + avxFilled = iterations * N_32BITS_IN_128REG; + auto avxDst = (__m128i*)dst; + for (uint32_t x = 0; x < iterations; ++x, ++avxDst) { + *avxDst = _mm_add_epi32(avxSrc, ALPHA_BLEND(*avxDst, avxIalpha)); + } + } + + //3. fill the remaining pixels + int32_t leftovers = span->len - notAligned - avxFilled; + dst += avxFilled; + while (leftovers--) { + *dst = src + ALPHA_BLEND(*dst, ialpha); + dst++; + } + + ++span; + } + return true; +} + + +#endif + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSwRasterC.h b/include/liblvgl/libs/thorvg/tvgSwRasterC.h new file mode 100644 index 00000000..bc5211e5 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSwRasterC.h @@ -0,0 +1,169 @@ +/* + * Copyright (c) 2021 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +template +static void inline cRasterPixels(PIXEL_T* dst, PIXEL_T val, uint32_t offset, int32_t len) +{ + dst += offset; + + //fix the misaligned memory + auto alignOffset = (long long) dst % 8; + if (alignOffset > 0) { + if (sizeof(PIXEL_T) == 4) alignOffset /= 4; + else if (sizeof(PIXEL_T) == 1) alignOffset = 8 - alignOffset; + while (alignOffset > 0 && len > 0) { + *dst++ = val; + --len; + --alignOffset; + } + } + + //64bits faster clear + if ((sizeof(PIXEL_T) == 4)) { + auto val64 = (uint64_t(val) << 32) | uint64_t(val); + while (len > 1) { + *reinterpret_cast(dst) = val64; + len -= 2; + dst += 2; + } + } else if (sizeof(PIXEL_T) == 1) { + auto val32 = (uint32_t(val) << 24) | (uint32_t(val) << 16) | (uint32_t(val) << 8) | uint32_t(val); + auto val64 = (uint64_t(val32) << 32) | val32; + while (len > 7) { + *reinterpret_cast(dst) = val64; + len -= 8; + dst += 8; + } + } + + //leftovers + while (len--) *dst++ = val; +} + + +static bool inline cRasterTranslucentRle(SwSurface* surface, const SwRleData* rle, uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ + auto span = rle->spans; + + //32bit channels + if (surface->channelSize == sizeof(uint32_t)) { + auto color = surface->join(r, g, b, a); + uint32_t src; + for (uint32_t i = 0; i < rle->size; ++i, ++span) { + auto dst = &surface->buf32[span->y * surface->stride + span->x]; + if (span->coverage < 255) src = ALPHA_BLEND(color, span->coverage); + else src = color; + auto ialpha = IA(src); + for (uint32_t x = 0; x < span->len; ++x, ++dst) { + *dst = src + ALPHA_BLEND(*dst, ialpha); + } + } + //8bit grayscale + } else if (surface->channelSize == sizeof(uint8_t)) { + uint8_t src; + for (uint32_t i = 0; i < rle->size; ++i, ++span) { + auto dst = &surface->buf8[span->y * surface->stride + span->x]; + if (span->coverage < 255) src = MULTIPLY(span->coverage, a); + else src = a; + auto ialpha = ~a; + for (uint32_t x = 0; x < span->len; ++x, ++dst) { + *dst = src + MULTIPLY(*dst, ialpha); + } + } + } + return true; +} + + +static bool inline cRasterTranslucentRect(SwSurface* surface, const SwBBox& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ + auto h = static_cast(region.max.y - region.min.y); + auto w = static_cast(region.max.x - region.min.x); + + //32bits channels + if (surface->channelSize == sizeof(uint32_t)) { + auto color = surface->join(r, g, b, a); + auto buffer = surface->buf32 + (region.min.y * surface->stride) + region.min.x; + auto ialpha = 255 - a; + for (uint32_t y = 0; y < h; ++y) { + auto dst = &buffer[y * surface->stride]; + for (uint32_t x = 0; x < w; ++x, ++dst) { + *dst = color + ALPHA_BLEND(*dst, ialpha); + } + } + //8bit grayscale + } else if (surface->channelSize == sizeof(uint8_t)) { + auto buffer = surface->buf8 + (region.min.y * surface->stride) + region.min.x; + auto ialpha = ~a; + for (uint32_t y = 0; y < h; ++y) { + auto dst = &buffer[y * surface->stride]; + for (uint32_t x = 0; x < w; ++x, ++dst) { + *dst = a + MULTIPLY(*dst, ialpha); + } + } + } + return true; +} + + +static bool inline cRasterABGRtoARGB(Surface* surface) +{ + TVGLOG("SW_ENGINE", "Convert ColorSpace ABGR - ARGB [Size: %d x %d]", surface->w, surface->h); + + //64bits faster converting + if (surface->w % 2 == 0) { + auto buffer = reinterpret_cast(surface->buf32); + for (uint32_t y = 0; y < surface->h; ++y, buffer += surface->stride / 2) { + auto dst = buffer; + for (uint32_t x = 0; x < surface->w / 2; ++x, ++dst) { + auto c = *dst; + //flip Blue, Red channels + *dst = (c & 0xff000000ff000000) + ((c & 0x00ff000000ff0000) >> 16) + (c & 0x0000ff000000ff00) + ((c & 0x000000ff000000ff) << 16); + } + } + //default converting + } else { + auto buffer = surface->buf32; + for (uint32_t y = 0; y < surface->h; ++y, buffer += surface->stride) { + auto dst = buffer; + for (uint32_t x = 0; x < surface->w; ++x, ++dst) { + auto c = *dst; + //flip Blue, Red channels + *dst = (c & 0xff000000) + ((c & 0x00ff0000) >> 16) + (c & 0x0000ff00) + ((c & 0x000000ff) << 16); + } + } + } + return true; +} + + +static bool inline cRasterARGBtoABGR(Surface* surface) +{ + //exactly same with ABGRtoARGB + return cRasterABGRtoARGB(surface); +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSwRasterNeon.h b/include/liblvgl/libs/thorvg/tvgSwRasterNeon.h new file mode 100644 index 00000000..a84bfe87 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSwRasterNeon.h @@ -0,0 +1,184 @@ +/* + * Copyright (c) 2021 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifdef THORVG_NEON_VECTOR_SUPPORT + +#include + +//TODO : need to support windows ARM + +#if defined(__ARM_64BIT_STATE) || defined(_M_ARM64) +#define TVG_AARCH64 1 +#else +#define TVG_AARCH64 0 +#endif + + +static inline uint8x8_t ALPHA_BLEND(uint8x8_t c, uint8x8_t a) +{ + uint16x8_t t = vmull_u8(c, a); + return vshrn_n_u16(t, 8); +} + + +static void neonRasterGrayscale8(uint8_t* dst, uint8_t val, uint32_t offset, int32_t len) +{ + dst += offset; + + int32_t i = 0; + const uint8x16_t valVec = vdupq_n_u8(val); +#if TVG_AARCH64 + uint8x16x4_t valQuad = {valVec, valVec, valVec, valVec}; + for (; i <= len - 16 * 4; i += 16 * 4) { + vst1q_u8_x4(dst + i, valQuad); + } +#else + for (; i <= len - 16; i += 16) { + vst1q_u8(dst + i, valVec); + } +#endif + for (; i < len; i++) { + dst[i] = val; + } +} + + +static void neonRasterPixel32(uint32_t *dst, uint32_t val, uint32_t offset, int32_t len) +{ + dst += offset; + + uint32x4_t vectorVal = vdupq_n_u32(val); + +#if TVG_AARCH64 + uint32_t iterations = len / 16; + uint32_t neonFilled = iterations * 16; + uint32x4x4_t valQuad = {vectorVal, vectorVal, vectorVal, vectorVal}; + for (uint32_t i = 0; i < iterations; ++i) { + vst4q_u32(dst, valQuad); + dst += 16; + } +#else + uint32_t iterations = len / 4; + uint32_t neonFilled = iterations * 4; + for (uint32_t i = 0; i < iterations; ++i) { + vst1q_u32(dst, vectorVal); + dst += 4; + } +#endif + int32_t leftovers = len - neonFilled; + while (leftovers--) *dst++ = val; +} + + +static bool neonRasterTranslucentRle(SwSurface* surface, const SwRleData* rle, uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ + if (surface->channelSize != sizeof(uint32_t)) { + TVGERR("SW_ENGINE", "Unsupported Channel Size = %d", surface->channelSize); + return false; + } + + auto color = surface->join(r, g, b, a); + auto span = rle->spans; + uint32_t src; + uint8x8_t *vDst = nullptr; + uint16_t align; + + for (uint32_t i = 0; i < rle->size; ++i) { + if (span->coverage < 255) src = ALPHA_BLEND(color, span->coverage); + else src = color; + + auto dst = &surface->buf32[span->y * surface->stride + span->x]; + auto ialpha = IA(src); + + if ((((uintptr_t) dst) & 0x7) != 0) { + //fill not aligned byte + *dst = src + ALPHA_BLEND(*dst, ialpha); + vDst = (uint8x8_t*)(dst + 1); + align = 1; + } else { + vDst = (uint8x8_t*) dst; + align = 0; + } + + uint8x8_t vSrc = (uint8x8_t) vdup_n_u32(src); + uint8x8_t vIalpha = vdup_n_u8((uint8_t) ialpha); + + for (uint32_t x = 0; x < (span->len - align) / 2; ++x) + vDst[x] = vadd_u8(vSrc, ALPHA_BLEND(vDst[x], vIalpha)); + + auto leftovers = (span->len - align) % 2; + if (leftovers > 0) dst[span->len - 1] = src + ALPHA_BLEND(dst[span->len - 1], ialpha); + + ++span; + } + return true; +} + + +static bool neonRasterTranslucentRect(SwSurface* surface, const SwBBox& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ + if (surface->channelSize != sizeof(uint32_t)) { + TVGERR("SW_ENGINE", "Unsupported Channel Size = %d", surface->channelSize); + return false; + } + + auto color = surface->join(r, g, b, a); + auto buffer = surface->buf32 + (region.min.y * surface->stride) + region.min.x; + auto h = static_cast(region.max.y - region.min.y); + auto w = static_cast(region.max.x - region.min.x); + auto ialpha = 255 - a; + + auto vColor = vdup_n_u32(color); + auto vIalpha = vdup_n_u8((uint8_t) ialpha); + + uint8x8_t* vDst = nullptr; + uint32_t align; + + for (uint32_t y = 0; y < h; ++y) { + auto dst = &buffer[y * surface->stride]; + + if ((((uintptr_t) dst) & 0x7) != 0) { + //fill not aligned byte + *dst = color + ALPHA_BLEND(*dst, ialpha); + vDst = (uint8x8_t*) (dst + 1); + align = 1; + } else { + vDst = (uint8x8_t*) dst; + align = 0; + } + + for (uint32_t x = 0; x < (w - align) / 2; ++x) + vDst[x] = vadd_u8((uint8x8_t)vColor, ALPHA_BLEND(vDst[x], vIalpha)); + + auto leftovers = (w - align) % 2; + if (leftovers > 0) dst[w - 1] = color + ALPHA_BLEND(dst[w - 1], ialpha); + } + return true; +} + +#endif + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSwRasterTexmap.h b/include/liblvgl/libs/thorvg/tvgSwRasterTexmap.h new file mode 100644 index 00000000..6ef2e320 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSwRasterTexmap.h @@ -0,0 +1,1214 @@ +/* + * Copyright (c) 2021 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +struct AALine +{ + int32_t x[2]; + int32_t coverage[2]; + int32_t length[2]; +}; + +struct AASpans +{ + AALine *lines; + int32_t yStart; + int32_t yEnd; +}; + +static inline void _swap(float& a, float& b, float& tmp) +{ + tmp = a; + a = b; + b = tmp; +} + + +//Careful! Shared resource, No support threading +static float dudx, dvdx; +static float dxdya, dxdyb, dudya, dvdya; +static float xa, xb, ua, va; + + +//Y Range exception handling +static bool _arrange(const SwImage* image, const SwBBox* region, int& yStart, int& yEnd) +{ + int32_t regionTop, regionBottom; + + if (region) { + regionTop = region->min.y; + regionBottom = region->max.y; + } else { + regionTop = image->rle->spans->y; + regionBottom = image->rle->spans[image->rle->size - 1].y; + } + + if (yStart >= regionBottom) return false; + + if (yStart < regionTop) yStart = regionTop; + if (yEnd > regionBottom) yEnd = regionBottom; + + return true; +} + + +static bool _rasterMaskedPolygonImageSegment(SwSurface* surface, const SwImage* image, const SwBBox* region, int yStart, int yEnd, AASpans* aaSpans, uint8_t opacity, uint8_t dirFlag = 0) +{ + return false; + +#if 0 //Enable it when GRAYSCALE image is supported + auto maskOp = _getMaskOp(surface->compositor->method); + auto direct = _direct(surface->compositor->method); + float _dudx = dudx, _dvdx = dvdx; + float _dxdya = dxdya, _dxdyb = dxdyb, _dudya = dudya, _dvdya = dvdya; + float _xa = xa, _xb = xb, _ua = ua, _va = va; + auto sbuf = image->buf8; + int32_t sw = static_cast(image->stride); + int32_t sh = image->h; + int32_t x1, x2, x, y, ar, ab, iru, irv, px, ay; + int32_t vv = 0, uu = 0; + int32_t minx = INT32_MAX, maxx = 0; + float dx, u, v, iptr; + SwSpan* span = nullptr; //used only when rle based. + + if (!_arrange(image, region, yStart, yEnd)) return false; + + //Loop through all lines in the segment + uint32_t spanIdx = 0; + + if (region) { + minx = region->min.x; + maxx = region->max.x; + } else { + span = image->rle->spans; + while (span->y < yStart) { + ++span; + ++spanIdx; + } + } + + y = yStart; + + while (y < yEnd) { + x1 = (int32_t)_xa; + x2 = (int32_t)_xb; + + if (!region) { + minx = INT32_MAX; + maxx = 0; + //one single row, could be consisted of multiple spans. + while (span->y == y && spanIdx < image->rle->size) { + if (minx > span->x) minx = span->x; + if (maxx < span->x + span->len) maxx = span->x + span->len; + ++span; + ++spanIdx; + } + } + if (x1 < minx) x1 = minx; + if (x2 > maxx) x2 = maxx; + + //Anti-Aliasing frames + ay = y - aaSpans->yStart; + if (aaSpans->lines[ay].x[0] > x1) aaSpans->lines[ay].x[0] = x1; + if (aaSpans->lines[ay].x[1] < x2) aaSpans->lines[ay].x[1] = x2; + + //Range allowed + if ((x2 - x1) >= 1 && (x1 < maxx) && (x2 > minx)) { + + //Perform subtexel pre-stepping on UV + dx = 1 - (_xa - x1); + u = _ua + dx * _dudx; + v = _va + dx * _dvdx; + + x = x1; + + auto cmp = &surface->compositor->image.buf8[y * surface->compositor->image.stride + x1]; + auto dst = &surface->buf8[y * surface->stride + x1]; + + if (opacity == 255) { + //Draw horizontal line + while (x++ < x2) { + uu = (int) u; + if (uu >= sw) continue; + vv = (int) v; + if (vv >= sh) continue; + + ar = (int)(255 * (1 - modff(u, &iptr))); + ab = (int)(255 * (1 - modff(v, &iptr))); + iru = uu + 1; + irv = vv + 1; + + px = *(sbuf + (vv * sw) + uu); + + /* horizontal interpolate */ + if (iru < sw) { + /* right pixel */ + int px2 = *(sbuf + (vv * sw) + iru); + px = INTERPOLATE(px, px2, ar); + } + /* vertical interpolate */ + if (irv < sh) { + /* bottom pixel */ + int px2 = *(sbuf + (irv * sw) + uu); + + /* horizontal interpolate */ + if (iru < sw) { + /* bottom right pixel */ + int px3 = *(sbuf + (irv * sw) + iru); + px2 = INTERPOLATE(px2, px3, ar); + } + px = INTERPOLATE(px, px2, ab); + } + if (direct) { + auto tmp = maskOp(px, *cmp, 0); //not use alpha + *dst = tmp + MULTIPLY(*dst, ~tmp); + ++dst; + } else { + *cmp = maskOp(px, *cmp, ~px); + } + ++cmp; + + //Step UV horizontally + u += _dudx; + v += _dvdx; + //range over? + if ((uint32_t)v >= image->h) break; + } + } else { + //Draw horizontal line + while (x++ < x2) { + uu = (int) u; + if (uu >= sw) continue; + vv = (int) v; + if (vv >= sh) continue; + + ar = (int)(255 * (1 - modff(u, &iptr))); + ab = (int)(255 * (1 - modff(v, &iptr))); + iru = uu + 1; + irv = vv + 1; + + px = *(sbuf + (vv * sw) + uu); + + /* horizontal interpolate */ + if (iru < sw) { + /* right pixel */ + int px2 = *(sbuf + (vv * sw) + iru); + px = INTERPOLATE(px, px2, ar); + } + /* vertical interpolate */ + if (irv < sh) { + /* bottom pixel */ + int px2 = *(sbuf + (irv * sw) + uu); + + /* horizontal interpolate */ + if (iru < sw) { + /* bottom right pixel */ + int px3 = *(sbuf + (irv * sw) + iru); + px2 = INTERPOLATE(px2, px3, ar); + } + px = INTERPOLATE(px, px2, ab); + } + + if (direct) { + auto tmp = maskOp(MULTIPLY(px, opacity), *cmp, 0); + *dst = tmp + MULTIPLY(*dst, ~tmp); + ++dst; + } else { + auto tmp = MULTIPLY(px, opacity); + *cmp = maskOp(tmp, *cmp, ~px); + } + ++cmp; + + //Step UV horizontally + u += _dudx; + v += _dvdx; + //range over? + if ((uint32_t)v >= image->h) break; + } + } + } + + //Step along both edges + _xa += _dxdya; + _xb += _dxdyb; + _ua += _dudya; + _va += _dvdya; + + if (!region && spanIdx >= image->rle->size) break; + + ++y; + } + xa = _xa; + xb = _xb; + ua = _ua; + va = _va; + + return true; +#endif +} + + +static void _rasterBlendingPolygonImageSegment(SwSurface* surface, const SwImage* image, const SwBBox* region, int yStart, int yEnd, AASpans* aaSpans, uint8_t opacity) +{ + float _dudx = dudx, _dvdx = dvdx; + float _dxdya = dxdya, _dxdyb = dxdyb, _dudya = dudya, _dvdya = dvdya; + float _xa = xa, _xb = xb, _ua = ua, _va = va; + auto sbuf = image->buf32; + auto dbuf = surface->buf32; + int32_t sw = static_cast(image->stride); + int32_t sh = image->h; + int32_t dw = surface->stride; + int32_t x1, x2, x, y, ar, ab, iru, irv, px, ay; + int32_t vv = 0, uu = 0; + int32_t minx = INT32_MAX, maxx = 0; + float dx, u, v, iptr; + uint32_t* buf; + SwSpan* span = nullptr; //used only when rle based. + + if (!_arrange(image, region, yStart, yEnd)) return; + + //Loop through all lines in the segment + uint32_t spanIdx = 0; + + if (region) { + minx = region->min.x; + maxx = region->max.x; + } else { + span = image->rle->spans; + while (span->y < yStart) { + ++span; + ++spanIdx; + } + } + + y = yStart; + + while (y < yEnd) { + x1 = (int32_t)_xa; + x2 = (int32_t)_xb; + + if (!region) { + minx = INT32_MAX; + maxx = 0; + //one single row, could be consisted of multiple spans. + while (span->y == y && spanIdx < image->rle->size) { + if (minx > span->x) minx = span->x; + if (maxx < span->x + span->len) maxx = span->x + span->len; + ++span; + ++spanIdx; + } + } + if (x1 < minx) x1 = minx; + if (x2 > maxx) x2 = maxx; + + //Anti-Aliasing frames + ay = y - aaSpans->yStart; + if (aaSpans->lines[ay].x[0] > x1) aaSpans->lines[ay].x[0] = x1; + if (aaSpans->lines[ay].x[1] < x2) aaSpans->lines[ay].x[1] = x2; + + //Range allowed + if ((x2 - x1) >= 1 && (x1 < maxx) && (x2 > minx)) { + + //Perform subtexel pre-stepping on UV + dx = 1 - (_xa - x1); + u = _ua + dx * _dudx; + v = _va + dx * _dvdx; + + buf = dbuf + ((y * dw) + x1); + + x = x1; + + if (opacity == 255) { + //Draw horizontal line + while (x++ < x2) { + uu = (int) u; + if (uu >= sw) continue; + vv = (int) v; + if (vv >= sh) continue; + + ar = (int)(255 * (1 - modff(u, &iptr))); + ab = (int)(255 * (1 - modff(v, &iptr))); + iru = uu + 1; + irv = vv + 1; + + px = *(sbuf + (vv * sw) + uu); + + /* horizontal interpolate */ + if (iru < sw) { + /* right pixel */ + int px2 = *(sbuf + (vv * sw) + iru); + px = INTERPOLATE(px, px2, ar); + } + /* vertical interpolate */ + if (irv < sh) { + /* bottom pixel */ + int px2 = *(sbuf + (irv * sw) + uu); + + /* horizontal interpolate */ + if (iru < sw) { + /* bottom right pixel */ + int px3 = *(sbuf + (irv * sw) + iru); + px2 = INTERPOLATE(px2, px3, ar); + } + px = INTERPOLATE(px, px2, ab); + } + *buf = surface->blender(px, *buf, IA(px)); + ++buf; + + //Step UV horizontally + u += _dudx; + v += _dvdx; + //range over? + if ((uint32_t)v >= image->h) break; + } + } else { + //Draw horizontal line + while (x++ < x2) { + uu = (int) u; + if (uu >= sw) continue; + vv = (int) v; + if (vv >= sh) continue; + + ar = (int)(255 * (1 - modff(u, &iptr))); + ab = (int)(255 * (1 - modff(v, &iptr))); + iru = uu + 1; + irv = vv + 1; + + px = *(sbuf + (vv * sw) + uu); + + /* horizontal interpolate */ + if (iru < sw) { + /* right pixel */ + int px2 = *(sbuf + (vv * sw) + iru); + px = INTERPOLATE(px, px2, ar); + } + /* vertical interpolate */ + if (irv < sh) { + /* bottom pixel */ + int px2 = *(sbuf + (irv * sw) + uu); + + /* horizontal interpolate */ + if (iru < sw) { + /* bottom right pixel */ + int px3 = *(sbuf + (irv * sw) + iru); + px2 = INTERPOLATE(px2, px3, ar); + } + px = INTERPOLATE(px, px2, ab); + } + auto src = ALPHA_BLEND(px, opacity); + *buf = surface->blender(src, *buf, IA(src)); + ++buf; + + //Step UV horizontally + u += _dudx; + v += _dvdx; + //range over? + if ((uint32_t)v >= image->h) break; + } + } + } + + //Step along both edges + _xa += _dxdya; + _xb += _dxdyb; + _ua += _dudya; + _va += _dvdya; + + if (!region && spanIdx >= image->rle->size) break; + + ++y; + } + xa = _xa; + xb = _xb; + ua = _ua; + va = _va; +} + + +static void _rasterPolygonImageSegment(SwSurface* surface, const SwImage* image, const SwBBox* region, int yStart, int yEnd, AASpans* aaSpans, uint8_t opacity, bool matting) +{ + float _dudx = dudx, _dvdx = dvdx; + float _dxdya = dxdya, _dxdyb = dxdyb, _dudya = dudya, _dvdya = dvdya; + float _xa = xa, _xb = xb, _ua = ua, _va = va; + auto sbuf = image->buf32; + auto dbuf = surface->buf32; + int32_t sw = static_cast(image->stride); + int32_t sh = image->h; + int32_t dw = surface->stride; + int32_t x1, x2, x, y, ar, ab, iru, irv, px, ay; + int32_t vv = 0, uu = 0; + int32_t minx = INT32_MAX, maxx = 0; + float dx, u, v, iptr; + uint32_t* buf; + SwSpan* span = nullptr; //used only when rle based. + + //for matting(composition) + auto csize = matting ? surface->compositor->image.channelSize: 0; + auto alpha = matting ? surface->alpha(surface->compositor->method) : nullptr; + uint8_t* cmp = nullptr; + + if (!_arrange(image, region, yStart, yEnd)) return; + + //Loop through all lines in the segment + uint32_t spanIdx = 0; + + if (region) { + minx = region->min.x; + maxx = region->max.x; + } else { + span = image->rle->spans; + while (span->y < yStart) { + ++span; + ++spanIdx; + } + } + + y = yStart; + + while (y < yEnd) { + x1 = (int32_t)_xa; + x2 = (int32_t)_xb; + + if (!region) { + minx = INT32_MAX; + maxx = 0; + //one single row, could be consisted of multiple spans. + while (span->y == y && spanIdx < image->rle->size) { + if (minx > span->x) minx = span->x; + if (maxx < span->x + span->len) maxx = span->x + span->len; + ++span; + ++spanIdx; + } + } + if (x1 < minx) x1 = minx; + if (x2 > maxx) x2 = maxx; + + //Anti-Aliasing frames + ay = y - aaSpans->yStart; + if (aaSpans->lines[ay].x[0] > x1) aaSpans->lines[ay].x[0] = x1; + if (aaSpans->lines[ay].x[1] < x2) aaSpans->lines[ay].x[1] = x2; + + //Range allowed + if ((x2 - x1) >= 1 && (x1 < maxx) && (x2 > minx)) { + + //Perform subtexel pre-stepping on UV + dx = 1 - (_xa - x1); + u = _ua + dx * _dudx; + v = _va + dx * _dvdx; + + buf = dbuf + ((y * dw) + x1); + + x = x1; + + if (matting) cmp = &surface->compositor->image.buf8[(y * surface->compositor->image.stride + x1) * csize]; + + if (opacity == 255) { + //Draw horizontal line + while (x++ < x2) { + uu = (int) u; + if (uu >= sw) continue; + vv = (int) v; + if (vv >= sh) continue; + + ar = (int)(255.0f * (1.0f - modff(u, &iptr))); + ab = (int)(255.0f * (1.0f - modff(v, &iptr))); + iru = uu + 1; + irv = vv + 1; + + px = *(sbuf + (vv * sw) + uu); + + /* horizontal interpolate */ + if (iru < sw) { + /* right pixel */ + int px2 = *(sbuf + (vv * sw) + iru); + px = INTERPOLATE(px, px2, ar); + } + /* vertical interpolate */ + if (irv < sh) { + /* bottom pixel */ + int px2 = *(sbuf + (irv * sw) + uu); + + /* horizontal interpolate */ + if (iru < sw) { + /* bottom right pixel */ + int px3 = *(sbuf + (irv * sw) + iru); + px2 = INTERPOLATE(px2, px3, ar); + } + px = INTERPOLATE(px, px2, ab); + } + uint32_t src; + if (matting) { + src = ALPHA_BLEND(px, alpha(cmp)); + cmp += csize; + } else { + src = px; + } + *buf = src + ALPHA_BLEND(*buf, IA(src)); + ++buf; + + //Step UV horizontally + u += _dudx; + v += _dvdx; + //range over? + if ((uint32_t)v >= image->h) break; + } + } else { + //Draw horizontal line + while (x++ < x2) { + uu = (int) u; + vv = (int) v; + + ar = (int)(255.0f * (1.0f - modff(u, &iptr))); + ab = (int)(255.0f * (1.0f - modff(v, &iptr))); + iru = uu + 1; + irv = vv + 1; + + if (vv >= sh) continue; + + px = *(sbuf + (vv * sw) + uu); + + /* horizontal interpolate */ + if (iru < sw) { + /* right pixel */ + int px2 = *(sbuf + (vv * sw) + iru); + px = INTERPOLATE(px, px2, ar); + } + /* vertical interpolate */ + if (irv < sh) { + /* bottom pixel */ + int px2 = *(sbuf + (irv * sw) + uu); + + /* horizontal interpolate */ + if (iru < sw) { + /* bottom right pixel */ + int px3 = *(sbuf + (irv * sw) + iru); + px2 = INTERPOLATE(px2, px3, ar); + } + px = INTERPOLATE(px, px2, ab); + } + uint32_t src; + if (matting) { + src = ALPHA_BLEND(px, MULTIPLY(opacity, alpha(cmp))); + cmp += csize; + } else { + src = ALPHA_BLEND(px, opacity); + } + *buf = src + ALPHA_BLEND(*buf, IA(src)); + ++buf; + + //Step UV horizontally + u += _dudx; + v += _dvdx; + //range over? + if ((uint32_t)v >= image->h) break; + } + } + } + + //Step along both edges + _xa += _dxdya; + _xb += _dxdyb; + _ua += _dudya; + _va += _dvdya; + + if (!region && spanIdx >= image->rle->size) break; + + ++y; + } + xa = _xa; + xb = _xb; + ua = _ua; + va = _va; +} + + +/* This mapping algorithm is based on Mikael Kalms's. */ +static void _rasterPolygonImage(SwSurface* surface, const SwImage* image, const SwBBox* region, Polygon& polygon, AASpans* aaSpans, uint8_t opacity) +{ + float x[3] = {polygon.vertex[0].pt.x, polygon.vertex[1].pt.x, polygon.vertex[2].pt.x}; + float y[3] = {polygon.vertex[0].pt.y, polygon.vertex[1].pt.y, polygon.vertex[2].pt.y}; + float u[3] = {polygon.vertex[0].uv.x, polygon.vertex[1].uv.x, polygon.vertex[2].uv.x}; + float v[3] = {polygon.vertex[0].uv.y, polygon.vertex[1].uv.y, polygon.vertex[2].uv.y}; + + float off_y; + float dxdy[3] = {0.0f, 0.0f, 0.0f}; + float tmp; + + auto upper = false; + + //Sort the vertices in ascending Y order + if (y[0] > y[1]) { + _swap(x[0], x[1], tmp); + _swap(y[0], y[1], tmp); + _swap(u[0], u[1], tmp); + _swap(v[0], v[1], tmp); + } + if (y[0] > y[2]) { + _swap(x[0], x[2], tmp); + _swap(y[0], y[2], tmp); + _swap(u[0], u[2], tmp); + _swap(v[0], v[2], tmp); + } + if (y[1] > y[2]) { + _swap(x[1], x[2], tmp); + _swap(y[1], y[2], tmp); + _swap(u[1], u[2], tmp); + _swap(v[1], v[2], tmp); + } + + //Y indexes + int yi[3] = {(int)y[0], (int)y[1], (int)y[2]}; + + //Skip drawing if it's too thin to cover any pixels at all. + if ((yi[0] == yi[1] && yi[0] == yi[2]) || ((int) x[0] == (int) x[1] && (int) x[0] == (int) x[2])) return; + + //Calculate horizontal and vertical increments for UV axes (these calcs are certainly not optimal, although they're stable (handles any dy being 0) + auto denom = ((x[2] - x[0]) * (y[1] - y[0]) - (x[1] - x[0]) * (y[2] - y[0])); + + //Skip poly if it's an infinitely thin line + if (mathZero(denom)) return; + + denom = 1 / denom; //Reciprocal for speeding up + dudx = ((u[2] - u[0]) * (y[1] - y[0]) - (u[1] - u[0]) * (y[2] - y[0])) * denom; + dvdx = ((v[2] - v[0]) * (y[1] - y[0]) - (v[1] - v[0]) * (y[2] - y[0])) * denom; + auto dudy = ((u[1] - u[0]) * (x[2] - x[0]) - (u[2] - u[0]) * (x[1] - x[0])) * denom; + auto dvdy = ((v[1] - v[0]) * (x[2] - x[0]) - (v[2] - v[0]) * (x[1] - x[0])) * denom; + + //Calculate X-slopes along the edges + if (y[1] > y[0]) dxdy[0] = (x[1] - x[0]) / (y[1] - y[0]); + if (y[2] > y[0]) dxdy[1] = (x[2] - x[0]) / (y[2] - y[0]); + if (y[2] > y[1]) dxdy[2] = (x[2] - x[1]) / (y[2] - y[1]); + + //Determine which side of the polygon the longer edge is on + auto side = (dxdy[1] > dxdy[0]) ? true : false; + + if (mathEqual(y[0], y[1])) side = x[0] > x[1]; + if (mathEqual(y[1], y[2])) side = x[2] > x[1]; + + auto regionTop = region ? region->min.y : image->rle->spans->y; //Normal Image or Rle Image? + auto compositing = _compositing(surface); //Composition required + auto blending = _blending(surface); //Blending required + + //Longer edge is on the left side + if (!side) { + //Calculate slopes along left edge + dxdya = dxdy[1]; + dudya = dxdya * dudx + dudy; + dvdya = dxdya * dvdx + dvdy; + + //Perform subpixel pre-stepping along left edge + auto dy = 1.0f - (y[0] - yi[0]); + xa = x[0] + dy * dxdya; + ua = u[0] + dy * dudya; + va = v[0] + dy * dvdya; + + //Draw upper segment if possibly visible + if (yi[0] < yi[1]) { + off_y = y[0] < regionTop ? (regionTop - y[0]) : 0; + xa += (off_y * dxdya); + ua += (off_y * dudya); + va += (off_y * dvdya); + + // Set right edge X-slope and perform subpixel pre-stepping + dxdyb = dxdy[0]; + xb = x[0] + dy * dxdyb + (off_y * dxdyb); + + if (compositing) { + if (_matting(surface)) _rasterPolygonImageSegment(surface, image, region, yi[0], yi[1], aaSpans, opacity, true); + else _rasterMaskedPolygonImageSegment(surface, image, region, yi[0], yi[1], aaSpans, opacity, 1); + } else if (blending) { + _rasterBlendingPolygonImageSegment(surface, image, region, yi[0], yi[1], aaSpans, opacity); + } else { + _rasterPolygonImageSegment(surface, image, region, yi[0], yi[1], aaSpans, opacity, false); + } + upper = true; + } + //Draw lower segment if possibly visible + if (yi[1] < yi[2]) { + off_y = y[1] < regionTop ? (regionTop - y[1]) : 0; + if (!upper) { + xa += (off_y * dxdya); + ua += (off_y * dudya); + va += (off_y * dvdya); + } + // Set right edge X-slope and perform subpixel pre-stepping + dxdyb = dxdy[2]; + xb = x[1] + (1 - (y[1] - yi[1])) * dxdyb + (off_y * dxdyb); + if (compositing) { + if (_matting(surface)) _rasterPolygonImageSegment(surface, image, region, yi[1], yi[2], aaSpans, opacity, true); + else _rasterMaskedPolygonImageSegment(surface, image, region, yi[1], yi[2], aaSpans, opacity, 2); + } else if (blending) { + _rasterBlendingPolygonImageSegment(surface, image, region, yi[1], yi[2], aaSpans, opacity); + } else { + _rasterPolygonImageSegment(surface, image, region, yi[1], yi[2], aaSpans, opacity, false); + } + } + //Longer edge is on the right side + } else { + //Set right edge X-slope and perform subpixel pre-stepping + dxdyb = dxdy[1]; + auto dy = 1.0f - (y[0] - yi[0]); + xb = x[0] + dy * dxdyb; + + //Draw upper segment if possibly visible + if (yi[0] < yi[1]) { + off_y = y[0] < regionTop ? (regionTop - y[0]) : 0; + xb += (off_y *dxdyb); + + // Set slopes along left edge and perform subpixel pre-stepping + dxdya = dxdy[0]; + dudya = dxdya * dudx + dudy; + dvdya = dxdya * dvdx + dvdy; + + xa = x[0] + dy * dxdya + (off_y * dxdya); + ua = u[0] + dy * dudya + (off_y * dudya); + va = v[0] + dy * dvdya + (off_y * dvdya); + + if (compositing) { + if (_matting(surface)) _rasterPolygonImageSegment(surface, image, region, yi[0], yi[1], aaSpans, opacity, true); + else _rasterMaskedPolygonImageSegment(surface, image, region, yi[0], yi[1], aaSpans, opacity, 3); + } else if (blending) { + _rasterBlendingPolygonImageSegment(surface, image, region, yi[0], yi[1], aaSpans, opacity); + } else { + _rasterPolygonImageSegment(surface, image, region, yi[0], yi[1], aaSpans, opacity, false); + } + upper = true; + } + //Draw lower segment if possibly visible + if (yi[1] < yi[2]) { + off_y = y[1] < regionTop ? (regionTop - y[1]) : 0; + if (!upper) xb += (off_y *dxdyb); + + // Set slopes along left edge and perform subpixel pre-stepping + dxdya = dxdy[2]; + dudya = dxdya * dudx + dudy; + dvdya = dxdya * dvdx + dvdy; + dy = 1 - (y[1] - yi[1]); + xa = x[1] + dy * dxdya + (off_y * dxdya); + ua = u[1] + dy * dudya + (off_y * dudya); + va = v[1] + dy * dvdya + (off_y * dvdya); + + if (compositing) { + if (_matting(surface)) _rasterPolygonImageSegment(surface, image, region, yi[1], yi[2], aaSpans, opacity, true); + else _rasterMaskedPolygonImageSegment(surface, image, region, yi[1], yi[2], aaSpans, opacity, 4); + } else if (blending) { + _rasterBlendingPolygonImageSegment(surface, image, region, yi[1], yi[2], aaSpans, opacity); + } else { + _rasterPolygonImageSegment(surface, image, region, yi[1], yi[2], aaSpans, opacity, false); + } + } + } +} + + +static AASpans* _AASpans(float ymin, float ymax, const SwImage* image, const SwBBox* region) +{ + auto yStart = static_cast(ymin); + auto yEnd = static_cast(ymax); + + if (!_arrange(image, region, yStart, yEnd)) return nullptr; + + auto aaSpans = static_cast(malloc(sizeof(AASpans))); + aaSpans->yStart = yStart; + aaSpans->yEnd = yEnd; + + //Initialize X range + auto height = yEnd - yStart; + + aaSpans->lines = static_cast(malloc(height * sizeof(AALine))); + + for (int32_t i = 0; i < height; i++) { + aaSpans->lines[i].x[0] = INT32_MAX; + aaSpans->lines[i].x[1] = 0; + aaSpans->lines[i].length[0] = 0; + aaSpans->lines[i].length[1] = 0; + } + return aaSpans; +} + + +static void _calcIrregularCoverage(AALine* lines, int32_t eidx, int32_t y, int32_t diagonal, int32_t edgeDist, bool reverse) +{ + if (eidx == 1) reverse = !reverse; + int32_t coverage = (255 / (diagonal + 2)); + int32_t tmp; + for (int32_t ry = 0; ry < (diagonal + 2); ry++) { + tmp = y - ry - edgeDist; + if (tmp < 0) return; + lines[tmp].length[eidx] = 1; + if (reverse) lines[tmp].coverage[eidx] = 255 - (coverage * ry); + else lines[tmp].coverage[eidx] = (coverage * ry); + } +} + + +static void _calcVertCoverage(AALine *lines, int32_t eidx, int32_t y, int32_t rewind, bool reverse) +{ + if (eidx == 1) reverse = !reverse; + int32_t coverage = (255 / (rewind + 1)); + int32_t tmp; + for (int ry = 1; ry < (rewind + 1); ry++) { + tmp = y - ry; + if (tmp < 0) return; + lines[tmp].length[eidx] = 1; + if (reverse) lines[tmp].coverage[eidx] = (255 - (coverage * ry)); + else lines[tmp].coverage[eidx] = (coverage * ry); + } +} + + +static void _calcHorizCoverage(AALine *lines, int32_t eidx, int32_t y, int32_t x, int32_t x2) +{ + if (lines[y].length[eidx] < abs(x - x2)) { + lines[y].length[eidx] = abs(x - x2); + lines[y].coverage[eidx] = (255 / (lines[y].length[eidx] + 1)); + } +} + + +/* + * This Anti-Aliasing mechanism is originated from Hermet Park's idea. + * To understand this AA logic, you can refer this page: + * www.hermet.pe.kr/122 (hermetpark@gmail.com) +*/ +static void _calcAAEdge(AASpans *aaSpans, int32_t eidx) +{ +//Previous edge direction: +#define DirOutHor 0x0011 +#define DirOutVer 0x0001 +#define DirInHor 0x0010 +#define DirInVer 0x0000 +#define DirNone 0x1000 + +#define PUSH_VERTEX() \ + do { \ + pEdge.x = lines[y].x[eidx]; \ + pEdge.y = y; \ + ptx[0] = tx[0]; \ + ptx[1] = tx[1]; \ + } while (0) + + int32_t y = 0; + SwPoint pEdge = {-1, -1}; //previous edge point + SwPoint edgeDiff = {0, 0}; //temporary used for point distance + + /* store bigger to tx[0] between prev and current edge's x positions. */ + int32_t tx[2] = {0, 0}; + /* back up prev tx values */ + int32_t ptx[2] = {0, 0}; + int32_t diagonal = 0; //straight diagonal pixels count + + auto yStart = aaSpans->yStart; + auto yEnd = aaSpans->yEnd; + auto lines = aaSpans->lines; + + int32_t prevDir = DirNone; + int32_t curDir = DirNone; + + yEnd -= yStart; + + //Start Edge + if (y < yEnd) { + pEdge.x = lines[y].x[eidx]; + pEdge.y = y; + } + + //Calculates AA Edges + for (y++; y < yEnd; y++) { + //Ready tx + if (eidx == 0) { + tx[0] = pEdge.x; + tx[1] = lines[y].x[0]; + } else { + tx[0] = lines[y].x[1]; + tx[1] = pEdge.x; + } + edgeDiff.x = (tx[0] - tx[1]); + edgeDiff.y = (y - pEdge.y); + + //Confirm current edge direction + if (edgeDiff.x > 0) { + if (edgeDiff.y == 1) curDir = DirOutHor; + else curDir = DirOutVer; + } else if (edgeDiff.x < 0) { + if (edgeDiff.y == 1) curDir = DirInHor; + else curDir = DirInVer; + } else curDir = DirNone; + + //straight diagonal increase + if ((curDir == prevDir) && (y < yEnd)) { + if ((abs(edgeDiff.x) == 1) && (edgeDiff.y == 1)) { + ++diagonal; + PUSH_VERTEX(); + continue; + } + } + + switch (curDir) { + case DirOutHor: { + _calcHorizCoverage(lines, eidx, y, tx[0], tx[1]); + if (diagonal > 0) { + _calcIrregularCoverage(lines, eidx, y, diagonal, 0, true); + diagonal = 0; + } + /* Increment direction is changed: Outside Vertical -> Outside Horizontal */ + if (prevDir == DirOutVer) _calcHorizCoverage(lines, eidx, pEdge.y, ptx[0], ptx[1]); + + //Trick, but fine-tunning! + if (y == 1) _calcHorizCoverage(lines, eidx, pEdge.y, tx[0], tx[1]); + PUSH_VERTEX(); + } + break; + case DirOutVer: { + _calcVertCoverage(lines, eidx, y, edgeDiff.y, true); + if (diagonal > 0) { + _calcIrregularCoverage(lines, eidx, y, diagonal, edgeDiff.y, false); + diagonal = 0; + } + /* Increment direction is changed: Outside Horizontal -> Outside Vertical */ + if (prevDir == DirOutHor) _calcHorizCoverage(lines, eidx, pEdge.y, ptx[0], ptx[1]); + PUSH_VERTEX(); + } + break; + case DirInHor: { + _calcHorizCoverage(lines, eidx, (y - 1), tx[0], tx[1]); + if (diagonal > 0) { + _calcIrregularCoverage(lines, eidx, y, diagonal, 0, false); + diagonal = 0; + } + /* Increment direction is changed: Outside Horizontal -> Inside Horizontal */ + if (prevDir == DirOutHor) _calcHorizCoverage(lines, eidx, pEdge.y, ptx[0], ptx[1]); + PUSH_VERTEX(); + } + break; + case DirInVer: { + _calcVertCoverage(lines, eidx, y, edgeDiff.y, false); + if (prevDir == DirOutHor) edgeDiff.y -= 1; //Weird, fine tuning????????????????????? + if (diagonal > 0) { + _calcIrregularCoverage(lines, eidx, y, diagonal, edgeDiff.y, true); + diagonal = 0; + } + /* Increment direction is changed: Outside Horizontal -> Inside Vertical */ + if (prevDir == DirOutHor) _calcHorizCoverage(lines, eidx, pEdge.y, ptx[0], ptx[1]); + PUSH_VERTEX(); + } + break; + } + if (curDir != DirNone) prevDir = curDir; + } + + //leftovers...? + if ((edgeDiff.y == 1) && (edgeDiff.x != 0)) { + if (y >= yEnd) y = (yEnd - 1); + _calcHorizCoverage(lines, eidx, y - 1, ptx[0], ptx[1]); + _calcHorizCoverage(lines, eidx, y, tx[0], tx[1]); + } else { + ++y; + if (y > yEnd) y = yEnd; + _calcVertCoverage(lines, eidx, y, (edgeDiff.y + 1), (prevDir & 0x00000001)); + } +} + + +static bool _apply(SwSurface* surface, AASpans* aaSpans) +{ + auto y = aaSpans->yStart; + uint32_t pixel; + uint32_t* dst; + int32_t pos; + + //left side + _calcAAEdge(aaSpans, 0); + //right side + _calcAAEdge(aaSpans, 1); + + while (y < aaSpans->yEnd) { + auto line = &aaSpans->lines[y - aaSpans->yStart]; + auto width = line->x[1] - line->x[0]; + if (width > 0) { + auto offset = y * surface->stride; + + //Left edge + dst = surface->buf32 + (offset + line->x[0]); + if (line->x[0] > 1) pixel = *(dst - 1); + else pixel = *dst; + + pos = 1; + while (pos <= line->length[0]) { + *dst = INTERPOLATE(*dst, pixel, line->coverage[0] * pos); + ++dst; + ++pos; + } + + //Right edge + dst = surface->buf32 + (offset + line->x[1] - 1); + if (line->x[1] < (int32_t)(surface->w - 1)) pixel = *(dst + 1); + else pixel = *dst; + + pos = width; + while ((int32_t)(width - line->length[1]) < pos) { + *dst = INTERPOLATE(*dst, pixel, 255 - (line->coverage[1] * (line->length[1] - (width - pos)))); + --dst; + --pos; + } + } + y++; + } + + free(aaSpans->lines); + free(aaSpans); + + return true; +} + + +/* + 2 triangles constructs 1 mesh. + below figure illustrates vert[4] index info. + If you need better quality, please divide a mesh by more number of triangles. + + 0 -- 1 + | / | + | / | + 3 -- 2 +*/ +static bool _rasterTexmapPolygon(SwSurface* surface, const SwImage* image, const Matrix* transform, const SwBBox* region, uint8_t opacity) +{ + if (surface->channelSize == sizeof(uint8_t)) { + TVGERR("SW_ENGINE", "Not supported grayscale Textmap polygon!"); + return false; + } + + //Exceptions: No dedicated drawing area? + if ((!image->rle && !region) || (image->rle && image->rle->size == 0)) return false; + + /* Prepare vertices. + shift XY coordinates to match the sub-pixeling technique. */ + Vertex vertices[4]; + vertices[0] = {{0.0f, 0.0f}, {0.0f, 0.0f}}; + vertices[1] = {{float(image->w), 0.0f}, {float(image->w), 0.0f}}; + vertices[2] = {{float(image->w), float(image->h)}, {float(image->w), float(image->h)}}; + vertices[3] = {{0.0f, float(image->h)}, {0.0f, float(image->h)}}; + + float ys = FLT_MAX, ye = -1.0f; + for (int i = 0; i < 4; i++) { + if (transform) mathMultiply(&vertices[i].pt, transform); + if (vertices[i].pt.y < ys) ys = vertices[i].pt.y; + if (vertices[i].pt.y > ye) ye = vertices[i].pt.y; + } + + auto aaSpans = _AASpans(ys, ye, image, region); + if (!aaSpans) return true; + + Polygon polygon; + + //Draw the first polygon + polygon.vertex[0] = vertices[0]; + polygon.vertex[1] = vertices[1]; + polygon.vertex[2] = vertices[3]; + + _rasterPolygonImage(surface, image, region, polygon, aaSpans, opacity); + + //Draw the second polygon + polygon.vertex[0] = vertices[1]; + polygon.vertex[1] = vertices[2]; + polygon.vertex[2] = vertices[3]; + + _rasterPolygonImage(surface, image, region, polygon, aaSpans, opacity); + +#if 0 + if (_compositing(surface) && _masking(surface) && !_direct(surface->compositor->method)) { + _compositeMaskImage(surface, &surface->compositor->image, surface->compositor->bbox); + } +#endif + return _apply(surface, aaSpans); +} + + +/* + Provide any number of triangles to draw a mesh using the supplied image. + Indexes are not used, so each triangle (Polygon) vertex has to be defined, even if they copy the previous one. + Example: + + 0 -- 1 0 -- 1 0 + | / | --> | / / | + | / | | / / | + 2 -- 3 2 1 -- 2 + + Should provide two Polygons, one for each triangle. + // TODO: region? +*/ +static bool _rasterTexmapPolygonMesh(SwSurface* surface, const SwImage* image, const RenderMesh* mesh, const Matrix* transform, const SwBBox* region, uint8_t opacity) +{ + if (surface->channelSize == sizeof(uint8_t)) { + TVGERR("SW_ENGINE", "Not supported grayscale Textmap polygon mesh!"); + return false; + } + + //Exceptions: No dedicated drawing area? + if ((!image->rle && !region) || (image->rle && image->rle->size == 0)) return false; + + // Step polygons once to transform + auto transformedTris = (Polygon*)malloc(sizeof(Polygon) * mesh->triangleCnt); + float ys = FLT_MAX, ye = -1.0f; + for (uint32_t i = 0; i < mesh->triangleCnt; i++) { + transformedTris[i] = mesh->triangles[i]; + mathMultiply(&transformedTris[i].vertex[0].pt, transform); + mathMultiply(&transformedTris[i].vertex[1].pt, transform); + mathMultiply(&transformedTris[i].vertex[2].pt, transform); + + if (transformedTris[i].vertex[0].pt.y < ys) ys = transformedTris[i].vertex[0].pt.y; + else if (transformedTris[i].vertex[0].pt.y > ye) ye = transformedTris[i].vertex[0].pt.y; + if (transformedTris[i].vertex[1].pt.y < ys) ys = transformedTris[i].vertex[1].pt.y; + else if (transformedTris[i].vertex[1].pt.y > ye) ye = transformedTris[i].vertex[1].pt.y; + if (transformedTris[i].vertex[2].pt.y < ys) ys = transformedTris[i].vertex[2].pt.y; + else if (transformedTris[i].vertex[2].pt.y > ye) ye = transformedTris[i].vertex[2].pt.y; + + // Convert normalized UV coordinates to image coordinates + transformedTris[i].vertex[0].uv.x *= (float)image->w; + transformedTris[i].vertex[0].uv.y *= (float)image->h; + transformedTris[i].vertex[1].uv.x *= (float)image->w; + transformedTris[i].vertex[1].uv.y *= (float)image->h; + transformedTris[i].vertex[2].uv.x *= (float)image->w; + transformedTris[i].vertex[2].uv.y *= (float)image->h; + } + + // Get AA spans and step polygons again to draw + if (auto aaSpans = _AASpans(ys, ye, image, region)) { + for (uint32_t i = 0; i < mesh->triangleCnt; i++) { + _rasterPolygonImage(surface, image, region, transformedTris[i], aaSpans, opacity); + } +#if 0 + if (_compositing(surface) && _masking(surface) && !_direct(surface->compositor->method)) { + _compositeMaskImage(surface, &surface->compositor->image, surface->compositor->bbox); + } +#endif + _apply(surface, aaSpans); + } + free(transformedTris); + return true; +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSwRenderer.cpp b/include/liblvgl/libs/thorvg/tvgSwRenderer.cpp new file mode 100644 index 00000000..9a096542 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSwRenderer.cpp @@ -0,0 +1,859 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgMath.h" +#include "tvgSwCommon.h" +#include "tvgTaskScheduler.h" +#include "tvgSwRenderer.h" + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ +static int32_t initEngineCnt = false; +static int32_t rendererCnt = 0; +static SwMpool* globalMpool = nullptr; +static uint32_t threadsCnt = 0; + +struct SwTask : Task +{ + SwSurface* surface = nullptr; + SwMpool* mpool = nullptr; + SwBBox bbox = {{0, 0}, {0, 0}}; //Whole Rendering Region + Matrix* transform = nullptr; + Array clips; + RenderUpdateFlag flags = RenderUpdateFlag::None; + uint8_t opacity; + bool pushed = false; //Pushed into task list? + bool disposed = false; //Disposed task? + + RenderRegion bounds() + { + //Can we skip the synchronization? + done(); + + RenderRegion region; + + //Range over? + region.x = bbox.min.x > 0 ? bbox.min.x : 0; + region.y = bbox.min.y > 0 ? bbox.min.y : 0; + region.w = bbox.max.x - region.x; + region.h = bbox.max.y - region.y; + if (region.w < 0) region.w = 0; + if (region.h < 0) region.h = 0; + + return region; + } + + virtual void dispose() = 0; + virtual bool clip(SwRleData* target) = 0; + virtual SwRleData* rle() = 0; + + virtual ~SwTask() + { + free(transform); + } +}; + + +struct SwShapeTask : SwTask +{ + SwShape shape; + const RenderShape* rshape = nullptr; + bool cmpStroking = false; + bool clipper = false; + + /* We assume that if the stroke width is greater than 2, + the shape's outline beneath the stroke could be adequately covered by the stroke drawing. + Therefore, antialiasing is disabled under this condition. + Additionally, the stroke style should not be dashed. */ + bool antialiasing(float strokeWidth) + { + return strokeWidth < 2.0f || rshape->stroke->dashCnt > 0 || rshape->stroke->strokeFirst; + } + + float validStrokeWidth() + { + if (!rshape->stroke) return 0.0f; + + auto width = rshape->stroke->width; + if (mathZero(width)) return 0.0f; + + if (!rshape->stroke->fill && (MULTIPLY(rshape->stroke->color[3], opacity) == 0)) return 0.0f; + if (mathZero(rshape->stroke->trim.begin - rshape->stroke->trim.end)) return 0.0f; + + if (transform) return (width * sqrt(transform->e11 * transform->e11 + transform->e12 * transform->e12)); + else return width; + } + + + bool clip(SwRleData* target) override + { + if (shape.fastTrack) rleClipRect(target, &bbox); + else if (shape.rle) rleClipPath(target, shape.rle); + else return false; + + return true; + } + + SwRleData* rle() override + { + if (!shape.rle && shape.fastTrack) { + shape.rle = rleRender(&shape.bbox); + } + return shape.rle; + } + + void run(unsigned tid) override + { + if (opacity == 0 && !clipper) return; //Invisible + + auto strokeWidth = validStrokeWidth(); + bool visibleFill = false; + auto clipRegion = bbox; + + //This checks also for the case, if the invisible shape turned to visible by alpha. + auto prepareShape = false; + if (!shapePrepared(&shape) && (flags & RenderUpdateFlag::Color)) prepareShape = true; + + //Shape + if (flags & (RenderUpdateFlag::Path | RenderUpdateFlag::Transform) || prepareShape) { + uint8_t alpha = 0; + rshape->fillColor(nullptr, nullptr, nullptr, &alpha); + alpha = MULTIPLY(alpha, opacity); + visibleFill = (alpha > 0 || rshape->fill); + if (visibleFill || clipper) { + shapeReset(&shape); + if (!shapePrepare(&shape, rshape, transform, clipRegion, bbox, mpool, tid, clips.count > 0 ? true : false)) { + visibleFill = false; + } + } + } + //Fill + if (flags & (RenderUpdateFlag::Gradient | RenderUpdateFlag::Transform | RenderUpdateFlag::Color)) { + if (visibleFill || clipper) { + if (!shapeGenRle(&shape, rshape, antialiasing(strokeWidth))) goto err; + } + if (auto fill = rshape->fill) { + auto ctable = (flags & RenderUpdateFlag::Gradient) ? true : false; + if (ctable) shapeResetFill(&shape); + if (!shapeGenFillColors(&shape, fill, transform, surface, opacity, ctable)) goto err; + } else { + shapeDelFill(&shape); + } + } + //Stroke + if (flags & (RenderUpdateFlag::Stroke | RenderUpdateFlag::Transform)) { + if (strokeWidth > 0.0f) { + shapeResetStroke(&shape, rshape, transform); + if (!shapeGenStrokeRle(&shape, rshape, transform, clipRegion, bbox, mpool, tid)) goto err; + + if (auto fill = rshape->strokeFill()) { + auto ctable = (flags & RenderUpdateFlag::GradientStroke) ? true : false; + if (ctable) shapeResetStrokeFill(&shape); + if (!shapeGenStrokeFillColors(&shape, fill, transform, surface, opacity, ctable)) goto err; + } else { + shapeDelStrokeFill(&shape); + } + } else { + shapeDelStroke(&shape); + } + } + + //Clear current task memorypool here if the clippers would use the same memory pool + shapeDelOutline(&shape, mpool, tid); + + //Clip Path + for (auto clip = clips.begin(); clip < clips.end(); ++clip) { + auto clipper = static_cast(*clip); + //Clip shape rle + if (shape.rle && !clipper->clip(shape.rle)) goto err; + //Clip stroke rle + if (shape.strokeRle && !clipper->clip(shape.strokeRle)) goto err; + } + return; + + err: + shapeReset(&shape); + shapeDelOutline(&shape, mpool, tid); + } + + void dispose() override + { + shapeFree(&shape); + } +}; + + +struct SwSceneTask : SwTask +{ + Array scene; //list of paints render data (SwTask) + SwRleData* sceneRle = nullptr; + + bool clip(SwRleData* target) override + { + //Only one shape + if (scene.count == 1) { + return static_cast(*scene.data)->clip(target); + } + + //More than one shapes + if (sceneRle) rleClipPath(target, sceneRle); + else TVGLOG("SW_ENGINE", "No clippers in a scene?"); + + return true; + } + + SwRleData* rle() override + { + return sceneRle; + } + + void run(unsigned tid) override + { + //TODO: Skip the run if the scene hasn't changed. + if (!sceneRle) sceneRle = static_cast(calloc(1, sizeof(SwRleData))); + else rleReset(sceneRle); + + //Merge shapes if it has more than one shapes + if (scene.count > 1) { + //Merge first two clippers + auto clipper1 = static_cast(*scene.data); + auto clipper2 = static_cast(*(scene.data + 1)); + + rleMerge(sceneRle, clipper1->rle(), clipper2->rle()); + + //Unify the remained clippers + for (auto rd = scene.begin() + 2; rd < scene.end(); ++rd) { + auto clipper = static_cast(*rd); + rleMerge(sceneRle, sceneRle, clipper->rle()); + } + } + } + + void dispose() override + { + rleFree(sceneRle); + } +}; + + +struct SwImageTask : SwTask +{ + SwImage image; + Surface* source; //Image source + const RenderMesh* mesh = nullptr; //Should be valid ptr in action + + bool clip(SwRleData* target) override + { + TVGERR("SW_ENGINE", "Image is used as ClipPath?"); + return true; + } + + SwRleData* rle() override + { + TVGERR("SW_ENGINE", "Image is used as Scene ClipPath?"); + return nullptr; + } + + void run(unsigned tid) override + { + auto clipRegion = bbox; + + //Convert colorspace if it's not aligned. + rasterConvertCS(source, surface->cs); + rasterPremultiply(source); + + image.data = source->data; + image.w = source->w; + image.h = source->h; + image.stride = source->stride; + image.channelSize = source->channelSize; + + //Invisible shape turned to visible by alpha. + if ((flags & (RenderUpdateFlag::Image | RenderUpdateFlag::Transform | RenderUpdateFlag::Color)) && (opacity > 0)) { + imageReset(&image); + if (!image.data || image.w == 0 || image.h == 0) goto end; + + if (!imagePrepare(&image, mesh, transform, clipRegion, bbox, mpool, tid)) goto end; + + // TODO: How do we clip the triangle mesh? Only clip non-meshed images for now + if (mesh->triangleCnt == 0 && clips.count > 0) { + if (!imageGenRle(&image, bbox, false)) goto end; + if (image.rle) { + //Clear current task memorypool here if the clippers would use the same memory pool + imageDelOutline(&image, mpool, tid); + for (auto clip = clips.begin(); clip < clips.end(); ++clip) { + auto clipper = static_cast(*clip); + if (!clipper->clip(image.rle)) goto err; + } + return; + } + } + } + goto end; + err: + rleReset(image.rle); + end: + imageDelOutline(&image, mpool, tid); + } + + void dispose() override + { + imageFree(&image); + } +}; + + +static void _termEngine() +{ + if (rendererCnt > 0) return; + + mpoolTerm(globalMpool); + globalMpool = nullptr; +} + + +static void _renderFill(SwShapeTask* task, SwSurface* surface, uint8_t opacity) +{ + uint8_t r, g, b, a; + if (auto fill = task->rshape->fill) { + rasterGradientShape(surface, &task->shape, fill->identifier()); + } else { + task->rshape->fillColor(&r, &g, &b, &a); + a = MULTIPLY(opacity, a); + if (a > 0) rasterShape(surface, &task->shape, r, g, b, a); + } +} + +static void _renderStroke(SwShapeTask* task, SwSurface* surface, uint8_t opacity) +{ + uint8_t r, g, b, a; + if (auto strokeFill = task->rshape->strokeFill()) { + rasterGradientStroke(surface, &task->shape, strokeFill->identifier()); + } else { + if (task->rshape->strokeColor(&r, &g, &b, &a)) { + a = MULTIPLY(opacity, a); + if (a > 0) rasterStroke(surface, &task->shape, r, g, b, a); + } + } +} + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +SwRenderer::~SwRenderer() +{ + clearCompositors(); + + delete(surface); + + if (!sharedMpool) mpoolTerm(mpool); + + --rendererCnt; + + if (rendererCnt == 0 && initEngineCnt == 0) _termEngine(); +} + + +bool SwRenderer::clear() +{ + for (auto task = tasks.begin(); task < tasks.end(); ++task) { + if ((*task)->disposed) { + delete(*task); + } else { + (*task)->done(); + (*task)->pushed = false; + } + } + tasks.clear(); + + if (!sharedMpool) mpoolClear(mpool); + + if (surface) { + vport.x = vport.y = 0; + vport.w = surface->w; + vport.h = surface->h; + } + + return true; +} + + +bool SwRenderer::sync() +{ + return true; +} + + +RenderRegion SwRenderer::viewport() +{ + return vport; +} + + +bool SwRenderer::viewport(const RenderRegion& vp) +{ + vport = vp; + return true; +} + + +bool SwRenderer::target(pixel_t* data, uint32_t stride, uint32_t w, uint32_t h, ColorSpace cs) +{ + if (!data || stride == 0 || w == 0 || h == 0 || w > stride) return false; + + clearCompositors(); + + if (!surface) surface = new SwSurface; + + surface->data = data; + surface->stride = stride; + surface->w = w; + surface->h = h; + surface->cs = cs; + surface->channelSize = CHANNEL_SIZE(cs); + surface->premultiplied = true; + + return rasterCompositor(surface); +} + + +bool SwRenderer::preRender() +{ + return true; +} + + +void SwRenderer::clearCompositors() +{ + //Free Composite Caches + for (auto comp = compositors.begin(); comp < compositors.end(); ++comp) { + free((*comp)->compositor->image.data); + delete((*comp)->compositor); + delete(*comp); + } + compositors.reset(); +} + + +bool SwRenderer::postRender() +{ + //Unmultiply alpha if needed + if (surface->cs == ColorSpace::ABGR8888S || surface->cs == ColorSpace::ARGB8888S) { + rasterUnpremultiply(surface); + } + + for (auto task = tasks.begin(); task < tasks.end(); ++task) { + if ((*task)->disposed) delete(*task); + else (*task)->pushed = false; + } + tasks.clear(); + + return true; +} + + +bool SwRenderer::renderImage(RenderData data) +{ + auto task = static_cast(data); + task->done(); + + if (task->opacity == 0) return true; + + return rasterImage(surface, &task->image, task->mesh, task->transform, task->bbox, task->opacity); +} + + +bool SwRenderer::renderShape(RenderData data) +{ + auto task = static_cast(data); + if (!task) return false; + + task->done(); + + if (task->opacity == 0) return true; + + //Main raster stage + if (task->rshape->stroke && task->rshape->stroke->strokeFirst) { + _renderStroke(task, surface, task->opacity); + _renderFill(task, surface, task->opacity); + } else { + _renderFill(task, surface, task->opacity); + _renderStroke(task, surface, task->opacity); + } + + return true; +} + + +bool SwRenderer::blend(BlendMethod method) +{ + if (surface->blendMethod == method) return true; + surface->blendMethod = method; + + switch (method) { + case BlendMethod::Add: + surface->blender = opBlendAdd; + break; + case BlendMethod::Screen: + surface->blender = opBlendScreen; + break; + case BlendMethod::Multiply: + surface->blender = opBlendMultiply; + break; + case BlendMethod::Overlay: + surface->blender = opBlendOverlay; + break; + case BlendMethod::Difference: + surface->blender = opBlendDifference; + break; + case BlendMethod::Exclusion: + surface->blender = opBlendExclusion; + break; + case BlendMethod::SrcOver: + surface->blender = opBlendSrcOver; + break; + case BlendMethod::Darken: + surface->blender = opBlendDarken; + break; + case BlendMethod::Lighten: + surface->blender = opBlendLighten; + break; + case BlendMethod::ColorDodge: + surface->blender = opBlendColorDodge; + break; + case BlendMethod::ColorBurn: + surface->blender = opBlendColorBurn; + break; + case BlendMethod::HardLight: + surface->blender = opBlendHardLight; + break; + case BlendMethod::SoftLight: + surface->blender = opBlendSoftLight; + break; + default: + surface->blender = nullptr; + break; + } + return false; +} + + +RenderRegion SwRenderer::region(RenderData data) +{ + return static_cast(data)->bounds(); +} + + +bool SwRenderer::beginComposite(Compositor* cmp, CompositeMethod method, uint8_t opacity) +{ + if (!cmp) return false; + auto p = static_cast(cmp); + + p->method = method; + p->opacity = opacity; + + //Current Context? + if (p->method != CompositeMethod::None) { + surface = p->recoverSfc; + surface->compositor = p; + } + + return true; +} + + +bool SwRenderer::mempool(bool shared) +{ + if (shared == sharedMpool) return true; + + if (shared) { + if (!sharedMpool) { + if (!mpoolTerm(mpool)) return false; + mpool = globalMpool; + } + } else { + if (sharedMpool) mpool = mpoolInit(threadsCnt); + } + + sharedMpool = shared; + + if (mpool) return true; + return false; +} + + +const Surface* SwRenderer::mainSurface() +{ + return surface; +} + + +Compositor* SwRenderer::target(const RenderRegion& region, ColorSpace cs) +{ + auto x = region.x; + auto y = region.y; + auto w = region.w; + auto h = region.h; + auto sw = static_cast(surface->w); + auto sh = static_cast(surface->h); + + //Out of boundary + if (x >= sw || y >= sh || x + w < 0 || y + h < 0) return nullptr; + + SwSurface* cmp = nullptr; + + auto reqChannelSize = CHANNEL_SIZE(cs); + + //Use cached data + for (auto p = compositors.begin(); p < compositors.end(); ++p) { + if ((*p)->compositor->valid && (*p)->compositor->image.channelSize == reqChannelSize) { + cmp = *p; + break; + } + } + + //New Composition + if (!cmp) { + //Inherits attributes from main surface + cmp = new SwSurface(surface); + cmp->compositor = new SwCompositor; + + //TODO: We can optimize compositor surface size from (surface->stride x surface->h) to Parameter(w x h) + cmp->compositor->image.data = (pixel_t*)malloc(reqChannelSize * surface->stride * surface->h); + cmp->channelSize = cmp->compositor->image.channelSize = reqChannelSize; + + compositors.push(cmp); + } + + //Boundary Check + if (x + w > sw) w = (sw - x); + if (y + h > sh) h = (sh - y); + + cmp->compositor->recoverSfc = surface; + cmp->compositor->recoverCmp = surface->compositor; + cmp->compositor->valid = false; + cmp->compositor->bbox.min.x = x; + cmp->compositor->bbox.min.y = y; + cmp->compositor->bbox.max.x = x + w; + cmp->compositor->bbox.max.y = y + h; + cmp->compositor->image.stride = surface->stride; + cmp->compositor->image.w = surface->w; + cmp->compositor->image.h = surface->h; + cmp->compositor->image.direct = true; + + cmp->data = cmp->compositor->image.data; + cmp->w = cmp->compositor->image.w; + cmp->h = cmp->compositor->image.h; + + rasterClear(cmp, x, y, w, h); + + //Switch render target + surface = cmp; + + return cmp->compositor; +} + + +bool SwRenderer::endComposite(Compositor* cmp) +{ + if (!cmp) return false; + + auto p = static_cast(cmp); + p->valid = true; + + //Recover Context + surface = p->recoverSfc; + surface->compositor = p->recoverCmp; + + //Default is alpha blending + if (p->method == CompositeMethod::None) { + return rasterImage(surface, &p->image, nullptr, nullptr, p->bbox, p->opacity); + } + + return true; +} + + +ColorSpace SwRenderer::colorSpace() +{ + if (surface) return surface->cs; + else return ColorSpace::Unsupported; +} + + +void SwRenderer::dispose(RenderData data) +{ + auto task = static_cast(data); + if (!task) return; + task->done(); + task->dispose(); + + if (task->pushed) task->disposed = true; + else delete(task); +} + + +void* SwRenderer::prepareCommon(SwTask* task, const RenderTransform* transform, const Array& clips, uint8_t opacity, RenderUpdateFlag flags) +{ + if (!surface) return task; + if (flags == RenderUpdateFlag::None) return task; + + //Finish previous task if it has duplicated request. + task->done(); + + //TODO: Failed threading them. It would be better if it's possible. + //See: https://github.com/thorvg/thorvg/issues/1409 + //Guarantee composition targets get ready. + for (auto clip = clips.begin(); clip < clips.end(); ++clip) { + static_cast(*clip)->done(); + } + + task->clips = clips; + + if (transform) { + if (!task->transform) task->transform = static_cast(malloc(sizeof(Matrix))); + *task->transform = transform->m; + } else { + if (task->transform) free(task->transform); + task->transform = nullptr; + } + + //zero size? + if (task->transform) { + if (task->transform->e11 == 0.0f && task->transform->e12 == 0.0f) return task; //zero width + if (task->transform->e21 == 0.0f && task->transform->e22 == 0.0f) return task; //zero height + } + + task->opacity = opacity; + task->surface = surface; + task->mpool = mpool; + task->flags = flags; + task->bbox.min.x = mathMax(static_cast(0), static_cast(vport.x)); + task->bbox.min.y = mathMax(static_cast(0), static_cast(vport.y)); + task->bbox.max.x = mathMin(static_cast(surface->w), static_cast(vport.x + vport.w)); + task->bbox.max.y = mathMin(static_cast(surface->h), static_cast(vport.y + vport.h)); + + if (!task->pushed) { + task->pushed = true; + tasks.push(task); + } + + TaskScheduler::request(task); + + return task; +} + + +RenderData SwRenderer::prepare(Surface* surface, const RenderMesh* mesh, RenderData data, const RenderTransform* transform, Array& clips, uint8_t opacity, RenderUpdateFlag flags) +{ + //prepare task + auto task = static_cast(data); + if (!task) task = new SwImageTask; + task->source = surface; + task->mesh = mesh; + return prepareCommon(task, transform, clips, opacity, flags); +} + + +RenderData SwRenderer::prepare(const Array& scene, RenderData data, const RenderTransform* transform, Array& clips, uint8_t opacity, RenderUpdateFlag flags) +{ + //prepare task + auto task = static_cast(data); + if (!task) task = new SwSceneTask; + task->scene = scene; + + //TODO: Failed threading them. It would be better if it's possible. + //See: https://github.com/thorvg/thorvg/issues/1409 + //Guarantee composition targets get ready. + for (auto task = scene.begin(); task < scene.end(); ++task) { + static_cast(*task)->done(); + } + return prepareCommon(task, transform, clips, opacity, flags); +} + + +RenderData SwRenderer::prepare(const RenderShape& rshape, RenderData data, const RenderTransform* transform, Array& clips, uint8_t opacity, RenderUpdateFlag flags, bool clipper) +{ + //prepare task + auto task = static_cast(data); + if (!task) { + task = new SwShapeTask; + task->rshape = &rshape; + } + task->clipper = clipper; + + return prepareCommon(task, transform, clips, opacity, flags); +} + + +SwRenderer::SwRenderer():mpool(globalMpool) +{ +} + + +bool SwRenderer::init(uint32_t threads) +{ + if ((initEngineCnt++) > 0) return true; + + threadsCnt = threads; + + //Share the memory pool among the renderer + globalMpool = mpoolInit(threads); + if (!globalMpool) { + --initEngineCnt; + return false; + } + + return true; +} + + +int32_t SwRenderer::init() +{ + return initEngineCnt; +} + + +bool SwRenderer::term() +{ + if ((--initEngineCnt) > 0) return true; + + initEngineCnt = 0; + + _termEngine(); + + return true; +} + +SwRenderer* SwRenderer::gen() +{ + ++rendererCnt; + return new SwRenderer(); +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSwRenderer.h b/include/liblvgl/libs/thorvg/tvgSwRenderer.h new file mode 100644 index 00000000..593ba52e --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSwRenderer.h @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_SW_RENDERER_H_ +#define _TVG_SW_RENDERER_H_ + +#include "tvgRender.h" + +struct SwSurface; +struct SwTask; +struct SwCompositor; +struct SwMpool; + +namespace tvg +{ + +class SwRenderer : public RenderMethod +{ +public: + RenderData prepare(const RenderShape& rshape, RenderData data, const RenderTransform* transform, Array& clips, uint8_t opacity, RenderUpdateFlag flags, bool clipper) override; + RenderData prepare(const Array& scene, RenderData data, const RenderTransform* transform, Array& clips, uint8_t opacity, RenderUpdateFlag flags) override; + RenderData prepare(Surface* surface, const RenderMesh* mesh, RenderData data, const RenderTransform* transform, Array& clips, uint8_t opacity, RenderUpdateFlag flags) override; + bool preRender() override; + bool renderShape(RenderData data) override; + bool renderImage(RenderData data) override; + bool postRender() override; + void dispose(RenderData data) override; + RenderRegion region(RenderData data) override; + RenderRegion viewport() override; + bool viewport(const RenderRegion& vp) override; + bool blend(BlendMethod method) override; + ColorSpace colorSpace() override; + const Surface* mainSurface() override; + + bool clear() override; + bool sync() override; + bool target(pixel_t* data, uint32_t stride, uint32_t w, uint32_t h, ColorSpace cs); + bool mempool(bool shared); + + Compositor* target(const RenderRegion& region, ColorSpace cs) override; + bool beginComposite(Compositor* cmp, CompositeMethod method, uint8_t opacity) override; + bool endComposite(Compositor* cmp) override; + void clearCompositors(); + + static SwRenderer* gen(); + static bool init(uint32_t threads); + static int32_t init(); + static bool term(); + +private: + SwSurface* surface = nullptr; //active surface + Array tasks; //async task list + Array compositors; //render targets cache list + SwMpool* mpool; //private memory pool + RenderRegion vport; //viewport + bool sharedMpool = true; //memory-pool behavior policy + + SwRenderer(); + ~SwRenderer(); + + RenderData prepareCommon(SwTask* task, const RenderTransform* transform, const Array& clips, uint8_t opacity, RenderUpdateFlag flags); +}; + +} + +#endif /* _TVG_SW_RENDERER_H_ */ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSwRle.cpp b/include/liblvgl/libs/thorvg/tvgSwRle.cpp new file mode 100644 index 00000000..689f2fe1 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSwRle.cpp @@ -0,0 +1,1134 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +/* + * The FreeType Project LICENSE + * ---------------------------- + + * 2006-Jan-27 + + * Copyright 1996-2002, 2006 by + * David Turner, Robert Wilhelm, and Werner Lemberg + + + + * Introduction + * ============ + + * The FreeType Project is distributed in several archive packages; + * some of them may contain, in addition to the FreeType font engine, + * various tools and contributions which rely on, or relate to, the + * FreeType Project. + + * This license applies to all files found in such packages, and + * which do not fall under their own explicit license. The license + * affects thus the FreeType font engine, the test programs, + * documentation and makefiles, at the very least. + + * This license was inspired by the BSD, Artistic, and IJG + * (Independent JPEG Group) licenses, which all encourage inclusion + * and use of free software in commercial and freeware products + * alike. As a consequence, its main points are that: + + * o We don't promise that this software works. However, we will be + * interested in any kind of bug reports. (`as is' distribution) + + * o You can use this software for whatever you want, in parts or + * full form, without having to pay us. (`royalty-free' usage) + + * o You may not pretend that you wrote this software. If you use + * it, or only parts of it, in a program, you must acknowledge + * somewhere in your documentation that you have used the + * FreeType code. (`credits') + + * We specifically permit and encourage the inclusion of this + * software, with or without modifications, in commercial products. + * We disclaim all warranties covering The FreeType Project and + * assume no liability related to The FreeType Project. + + + * Finally, many people asked us for a preferred form for a + * credit/disclaimer to use in compliance with this license. We thus + * encourage you to use the following text: + + * """ + * Portions of this software are copyright � The FreeType + * Project (www.freetype.org). All rights reserved. + * """ + + * Please replace with the value from the FreeType version you + * actually use. + +* Legal Terms +* =========== + +* 0. Definitions +* -------------- + +* Throughout this license, the terms `package', `FreeType Project', +* and `FreeType archive' refer to the set of files originally +* distributed by the authors (David Turner, Robert Wilhelm, and +* Werner Lemberg) as the `FreeType Project', be they named as alpha, +* beta or final release. + +* `You' refers to the licensee, or person using the project, where +* `using' is a generic term including compiling the project's source +* code as well as linking it to form a `program' or `executable'. +* This program is referred to as `a program using the FreeType +* engine'. + +* This license applies to all files distributed in the original +* FreeType Project, including all source code, binaries and +* documentation, unless otherwise stated in the file in its +* original, unmodified form as distributed in the original archive. +* If you are unsure whether or not a particular file is covered by +* this license, you must contact us to verify this. + +* The FreeType Project is copyright (C) 1996-2000 by David Turner, +* Robert Wilhelm, and Werner Lemberg. All rights reserved except as +* specified below. + +* 1. No Warranty +* -------------- + +* THE FREETYPE PROJECT IS PROVIDED `AS IS' WITHOUT WARRANTY OF ANY +* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +* PURPOSE. IN NO EVENT WILL ANY OF THE AUTHORS OR COPYRIGHT HOLDERS +* BE LIABLE FOR ANY DAMAGES CAUSED BY THE USE OR THE INABILITY TO +* USE, OF THE FREETYPE PROJECT. + +* 2. Redistribution +* ----------------- + +* This license grants a worldwide, royalty-free, perpetual and +* irrevocable right and license to use, execute, perform, compile, +* display, copy, create derivative works of, distribute and +* sublicense the FreeType Project (in both source and object code +* forms) and derivative works thereof for any purpose; and to +* authorize others to exercise some or all of the rights granted +* herein, subject to the following conditions: + +* o Redistribution of source code must retain this license file +* (`FTL.TXT') unaltered; any additions, deletions or changes to +* the original files must be clearly indicated in accompanying +* documentation. The copyright notices of the unaltered, +* original files must be preserved in all copies of source +* files. + +* o Redistribution in binary form must provide a disclaimer that +* states that the software is based in part of the work of the +* FreeType Team, in the distribution documentation. We also +* encourage you to put an URL to the FreeType web page in your +* documentation, though this isn't mandatory. + +* These conditions apply to any software derived from or based on +* the FreeType Project, not just the unmodified files. If you use +* our work, you must acknowledge us. However, no fee need be paid +* to us. + +* 3. Advertising +* -------------- + +* Neither the FreeType authors and contributors nor you shall use +* the name of the other for commercial, advertising, or promotional +* purposes without specific prior written permission. + +* We suggest, but do not require, that you use one or more of the +* following phrases to refer to this software in your documentation +* or advertising materials: `FreeType Project', `FreeType Engine', +* `FreeType library', or `FreeType Distribution'. + +* As you have not signed this license, you are not required to +* accept it. However, as the FreeType Project is copyrighted +* material, only this license, or another one contracted with the +* authors, grants you the right to use, distribute, and modify it. +* Therefore, by using, distributing, or modifying the FreeType +* Project, you indicate that you understand and accept all the terms +* of this license. + +* 4. Contacts +* ----------- + +* There are two mailing lists related to FreeType: + +* o freetype@nongnu.org + +* Discusses general use and applications of FreeType, as well as +* future and wanted additions to the library and distribution. +* If you are looking for support, start in this list if you +* haven't found anything to help you in the documentation. + +* o freetype-devel@nongnu.org + +* Discusses bugs, as well as engine internals, design issues, +* specific licenses, porting, etc. + +* Our home page can be found at + +* http://www.freetype.org +*/ + +#include +#include +#include +#include "tvgSwCommon.h" + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +constexpr auto MAX_SPANS = 256; +constexpr auto PIXEL_BITS = 8; //must be at least 6 bits! +constexpr auto ONE_PIXEL = (1L << PIXEL_BITS); + +using Area = long; + +struct Band +{ + SwCoord min, max; +}; + +struct Cell +{ + SwCoord x; + SwCoord cover; + Area area; + Cell *next; +}; + +struct RleWorker +{ + SwRleData* rle; + + SwPoint cellPos; + SwPoint cellMin; + SwPoint cellMax; + SwCoord cellXCnt; + SwCoord cellYCnt; + + Area area; + SwCoord cover; + + Cell* cells; + ptrdiff_t maxCells; + ptrdiff_t cellsCnt; + + SwPoint pos; + + SwPoint bezStack[32 * 3 + 1]; + int levStack[32]; + + SwOutline* outline; + + SwSpan spans[MAX_SPANS]; + int spansCnt; + int ySpan; + + int bandSize; + int bandShoot; + + jmp_buf jmpBuf; + + void* buffer; + long bufferSize; + + Cell** yCells; + SwCoord yCnt; + + bool invalid; + bool antiAlias; +}; + + +static inline SwPoint UPSCALE(const SwPoint& pt) +{ + return {SwCoord(((unsigned long) pt.x) << (PIXEL_BITS - 6)), SwCoord(((unsigned long) pt.y) << (PIXEL_BITS - 6))}; +} + + +static inline SwPoint TRUNC(const SwPoint& pt) +{ + return {pt.x >> PIXEL_BITS, pt.y >> PIXEL_BITS}; +} + + +static inline SwCoord TRUNC(const SwCoord x) +{ + return x >> PIXEL_BITS; +} + + +static inline SwPoint SUBPIXELS(const SwPoint& pt) +{ + return {SwCoord(((unsigned long) pt.x) << PIXEL_BITS), SwCoord(((unsigned long) pt.y) << PIXEL_BITS)}; +} + + +static inline SwCoord SUBPIXELS(const SwCoord x) +{ + return SwCoord(((unsigned long) x) << PIXEL_BITS); +} + +/* + * Approximate sqrt(x*x+y*y) using the `alpha max plus beta min' + * algorithm. We use alpha = 1, beta = 3/8, giving us results with a + * largest error less than 7% compared to the exact value. + */ +static inline SwCoord HYPOT(SwPoint pt) +{ + if (pt.x < 0) pt.x = -pt.x; + if (pt.y < 0) pt.y = -pt.y; + return ((pt.x > pt.y) ? (pt.x + (3 * pt.y >> 3)) : (pt.y + (3 * pt.x >> 3))); +} + +static void _genSpan(SwRleData* rle, const SwSpan* spans, uint32_t count) +{ + auto newSize = rle->size + count; + + /* allocate enough memory for new spans */ + /* alloc is required to prevent free and reallocation */ + /* when the rle needs to be regenerated because of attribute change. */ + if (rle->alloc < newSize) { + rle->alloc = (newSize * 2); + //OPTIMIZE: use mempool! + rle->spans = static_cast(realloc(rle->spans, rle->alloc * sizeof(SwSpan))); + } + + //copy the new spans to the allocated memory + SwSpan* lastSpan = rle->spans + rle->size; + memcpy(lastSpan, spans, count * sizeof(SwSpan)); + + rle->size = newSize; +} + + +static void _horizLine(RleWorker& rw, SwCoord x, SwCoord y, SwCoord area, SwCoord aCount) +{ + x += rw.cellMin.x; + y += rw.cellMin.y; + + //Clip Y range + if (y < rw.cellMin.y || y >= rw.cellMax.y) return; + + /* compute the coverage line's coverage, depending on the outline fill rule */ + /* the coverage percentage is area/(PIXEL_BITS*PIXEL_BITS*2) */ + auto coverage = static_cast(area >> (PIXEL_BITS * 2 + 1 - 8)); //range 0 - 255 + + if (coverage < 0) coverage = -coverage; + + if (rw.outline->fillRule == FillRule::EvenOdd) { + coverage &= 511; + if (coverage > 255) coverage = 511 - coverage; + } else { + //normal non-zero winding rule + if (coverage > 255) coverage = 255; + } + + //span has ushort coordinates. check limit overflow + if (x >= SHRT_MAX) { + TVGERR("SW_ENGINE", "X-coordinate overflow!"); + x = SHRT_MAX; + } + if (y >= SHRT_MAX) { + TVGERR("SW_ENGINE", "Y Coordinate overflow!"); + y = SHRT_MAX; + } + + if (coverage > 0) { + if (!rw.antiAlias) coverage = 255; + auto count = rw.spansCnt; + auto span = rw.spans + count - 1; + + //see whether we can add this span to the current list + if ((count > 0) && (rw.ySpan == y) && + (span->x + span->len == x) && (span->coverage == coverage)) { + + //Clip x range + SwCoord xOver = 0; + if (x + aCount >= rw.cellMax.x) xOver -= (x + aCount - rw.cellMax.x); + if (x < rw.cellMin.x) xOver -= (rw.cellMin.x - x); + + //span->len += (aCount + xOver) - 1; + span->len += (aCount + xOver); + return; + } + + if (count >= MAX_SPANS) { + _genSpan(rw.rle, rw.spans, count); + rw.spansCnt = 0; + rw.ySpan = 0; + span = rw.spans; + } else { + ++span; + } + + //Clip x range + SwCoord xOver = 0; + if (x + aCount >= rw.cellMax.x) xOver -= (x + aCount - rw.cellMax.x); + if (x < rw.cellMin.x) { + xOver -= (rw.cellMin.x - x); + x = rw.cellMin.x; + } + + //Nothing to draw + if (aCount + xOver <= 0) return; + + //add a span to the current list + span->x = x; + span->y = y; + span->len = (aCount + xOver); + span->coverage = coverage; + ++rw.spansCnt; + rw.ySpan = y; + } +} + + +static void _sweep(RleWorker& rw) +{ + if (rw.cellsCnt == 0) return; + + rw.spansCnt = 0; + rw.ySpan = 0; + + for (int y = 0; y < rw.yCnt; ++y) { + auto cover = 0; + auto x = 0; + auto cell = rw.yCells[y]; + + while (cell) { + if (cell->x > x && cover != 0) _horizLine(rw, x, y, cover * (ONE_PIXEL * 2), cell->x - x); + cover += cell->cover; + auto area = cover * (ONE_PIXEL * 2) - cell->area; + if (area != 0 && cell->x >= 0) _horizLine(rw, cell->x, y, area, 1); + x = cell->x + 1; + cell = cell->next; + } + + if (cover != 0) _horizLine(rw, x, y, cover * (ONE_PIXEL * 2), rw.cellXCnt - x); + } + + if (rw.spansCnt > 0) _genSpan(rw.rle, rw.spans, rw.spansCnt); +} + + +static Cell* _findCell(RleWorker& rw) +{ + auto x = rw.cellPos.x; + if (x > rw.cellXCnt) x = rw.cellXCnt; + + auto pcell = &rw.yCells[rw.cellPos.y]; + + while(true) { + Cell* cell = *pcell; + if (!cell || cell->x > x) break; + if (cell->x == x) return cell; + pcell = &cell->next; + } + + if (rw.cellsCnt >= rw.maxCells) longjmp(rw.jmpBuf, 1); + + auto cell = rw.cells + rw.cellsCnt++; + cell->x = x; + cell->area = 0; + cell->cover = 0; + cell->next = *pcell; + *pcell = cell; + + return cell; +} + + +static void _recordCell(RleWorker& rw) +{ + if (rw.area | rw.cover) { + auto cell = _findCell(rw); + cell->area += rw.area; + cell->cover += rw.cover; + } +} + + +static void _setCell(RleWorker& rw, SwPoint pos) +{ + /* Move the cell pointer to a new position. We set the `invalid' */ + /* flag to indicate that the cell isn't part of those we're interested */ + /* in during the render phase. This means that: */ + /* */ + /* . the new vertical position must be within min_ey..max_ey-1. */ + /* . the new horizontal position must be strictly less than max_ex */ + /* */ + /* Note that if a cell is to the left of the clipping region, it is */ + /* actually set to the (min_ex-1) horizontal position. */ + + /* All cells that are on the left of the clipping region go to the + min_ex - 1 horizontal position. */ + pos.x -= rw.cellMin.x; + pos.y -= rw.cellMin.y; + + if (pos.x > rw.cellMax.x) pos.x = rw.cellMax.x; + + //Are we moving to a different cell? + if (pos != rw.cellPos) { + //Record the current one if it is valid + if (!rw.invalid) _recordCell(rw); + } + + rw.area = 0; + rw.cover = 0; + rw.cellPos = pos; + rw.invalid = ((unsigned)pos.y >= (unsigned)rw.cellYCnt || pos.x >= rw.cellXCnt); +} + + +static void _startCell(RleWorker& rw, SwPoint pos) +{ + if (pos.x > rw.cellMax.x) pos.x = rw.cellMax.x; + if (pos.x < rw.cellMin.x) pos.x = rw.cellMin.x; + + rw.area = 0; + rw.cover = 0; + rw.cellPos = pos - rw.cellMin; + rw.invalid = false; + + _setCell(rw, pos); +} + + +static void _moveTo(RleWorker& rw, const SwPoint& to) +{ + //record current cell, if any */ + if (!rw.invalid) _recordCell(rw); + + //start to a new position + _startCell(rw, TRUNC(to)); + + rw.pos = to; +} + + +static void _lineTo(RleWorker& rw, const SwPoint& to) +{ +#define SW_UDIV(a, b) \ + static_cast(((unsigned long)(a) * (unsigned long)(b)) >> \ + (sizeof(long) * CHAR_BIT - PIXEL_BITS)) + + auto e1 = TRUNC(rw.pos); + auto e2 = TRUNC(to); + + //vertical clipping + if ((e1.y >= rw.cellMax.y && e2.y >= rw.cellMax.y) || (e1.y < rw.cellMin.y && e2.y < rw.cellMin.y)) { + rw.pos = to; + return; + } + + auto diff = to - rw.pos; + auto f1 = rw.pos - SUBPIXELS(e1); + SwPoint f2; + + //inside one cell + if (e1 == e2) { + ; + //any horizontal line + } else if (diff.y == 0) { + e1.x = e2.x; + _setCell(rw, e1); + } else if (diff.x == 0) { + //vertical line up + if (diff.y > 0) { + do { + f2.y = ONE_PIXEL; + rw.cover += (f2.y - f1.y); + rw.area += (f2.y - f1.y) * f1.x * 2; + f1.y = 0; + ++e1.y; + _setCell(rw, e1); + } while(e1.y != e2.y); + //vertical line down + } else { + do { + f2.y = 0; + rw.cover += (f2.y - f1.y); + rw.area += (f2.y - f1.y) * f1.x * 2; + f1.y = ONE_PIXEL; + --e1.y; + _setCell(rw, e1); + } while(e1.y != e2.y); + } + //any other line + } else { + Area prod = diff.x * f1.y - diff.y * f1.x; + + /* These macros speed up repetitive divisions by replacing them + with multiplications and right shifts. */ + auto dx_r = static_cast(ULONG_MAX >> PIXEL_BITS) / (diff.x); + auto dy_r = static_cast(ULONG_MAX >> PIXEL_BITS) / (diff.y); + + /* The fundamental value `prod' determines which side and the */ + /* exact coordinate where the line exits current cell. It is */ + /* also easily updated when moving from one cell to the next. */ + do { + auto px = diff.x * ONE_PIXEL; + auto py = diff.y * ONE_PIXEL; + + //left + if (prod <= 0 && prod - px > 0) { + f2 = {0, SW_UDIV(-prod, -dx_r)}; + prod -= py; + rw.cover += (f2.y - f1.y); + rw.area += (f2.y - f1.y) * (f1.x + f2.x); + f1 = {ONE_PIXEL, f2.y}; + --e1.x; + //up + } else if (prod - px <= 0 && prod - px + py > 0) { + prod -= px; + f2 = {SW_UDIV(-prod, dy_r), ONE_PIXEL}; + rw.cover += (f2.y - f1.y); + rw.area += (f2.y - f1.y) * (f1.x + f2.x); + f1 = {f2.x, 0}; + ++e1.y; + //right + } else if (prod - px + py <= 0 && prod + py >= 0) { + prod += py; + f2 = {ONE_PIXEL, SW_UDIV(prod, dx_r)}; + rw.cover += (f2.y - f1.y); + rw.area += (f2.y - f1.y) * (f1.x + f2.x); + f1 = {0, f2.y}; + ++e1.x; + //down + } else { + f2 = {SW_UDIV(prod, -dy_r), 0}; + prod += px; + rw.cover += (f2.y - f1.y); + rw.area += (f2.y - f1.y) * (f1.x + f2.x); + f1 = {f2.x, ONE_PIXEL}; + --e1.y; + } + + _setCell(rw, e1); + + } while(e1 != e2); + } + + f2 = {to.x - SUBPIXELS(e2.x), to.y - SUBPIXELS(e2.y)}; + rw.cover += (f2.y - f1.y); + rw.area += (f2.y - f1.y) * (f1.x + f2.x); + rw.pos = to; +} + + +static void _cubicTo(RleWorker& rw, const SwPoint& ctrl1, const SwPoint& ctrl2, const SwPoint& to) +{ + auto arc = rw.bezStack; + arc[0] = to; + arc[1] = ctrl2; + arc[2] = ctrl1; + arc[3] = rw.pos; + + //Short-cut the arc that crosses the current band + auto min = arc[0].y; + auto max = arc[0].y; + + SwCoord y; + for (auto i = 1; i < 4; ++i) { + y = arc[i].y; + if (y < min) min = y; + if (y > max) max = y; + } + + if (TRUNC(min) >= rw.cellMax.y || TRUNC(max) < rw.cellMin.y) goto draw; + + /* Decide whether to split or draw. See `Rapid Termination */ + /* Evaluation for Recursive Subdivision of Bezier Curves' by Thomas */ + /* F. Hain, at */ + /* http://www.cis.southalabama.edu/~hain/general/Publications/Bezier/Camera-ready%20CISST02%202.pdf */ + while (true) { + { + //diff is the P0 - P3 chord vector + auto diff = arc[3] - arc[0]; + auto L = HYPOT(diff); + + //avoid possible arithmetic overflow below by splitting + if (L > SHRT_MAX) goto split; + + //max deviation may be as much as (s/L) * 3/4 (if Hain's v = 1) + auto sLimit = L * (ONE_PIXEL / 6); + + auto diff1 = arc[1] - arc[0]; + auto s = diff.y * diff1.x - diff.x * diff1.y; + if (s < 0) s = -s; + if (s > sLimit) goto split; + + //s is L * the perpendicular distance from P2 to the line P0 - P3 + auto diff2 = arc[2] - arc[0]; + s = diff.y * diff2.x - diff.x * diff2.y; + if (s < 0) s = -s; + if (s > sLimit) goto split; + + /* Split super curvy segments where the off points are so far + from the chord that the angles P0-P1-P3 or P0-P2-P3 become + acute as detected by appropriate dot products */ + if (diff1.x * (diff1.x - diff.x) + diff1.y * (diff1.y - diff.y) > 0 || + diff2.x * (diff2.x - diff.x) + diff2.y * (diff2.y - diff.y) > 0) + goto split; + + //no reason to split + goto draw; + } + split: + mathSplitCubic(arc); + arc += 3; + continue; + + draw: + _lineTo(rw, arc[0]); + if (arc == rw.bezStack) return; + arc -= 3; + } +} + + +static void _decomposeOutline(RleWorker& rw) +{ + auto outline = rw.outline; + auto first = 0; //index of first point in contour + + for (auto cntr = outline->cntrs.begin(); cntr < outline->cntrs.end(); ++cntr) { + auto last = *cntr; + auto limit = outline->pts.data + last; + auto start = UPSCALE(outline->pts[first]); + auto pt = outline->pts.data + first; + auto types = outline->types.data + first; + + _moveTo(rw, UPSCALE(outline->pts[first])); + + while (pt < limit) { + ++pt; + ++types; + + //emit a single line_to + if (types[0] == SW_CURVE_TYPE_POINT) { + _lineTo(rw, UPSCALE(*pt)); + //types cubic + } else { + pt += 2; + types += 2; + + if (pt <= limit) { + _cubicTo(rw, UPSCALE(pt[-2]), UPSCALE(pt[-1]), UPSCALE(pt[0])); + continue; + } + _cubicTo(rw, UPSCALE(pt[-2]), UPSCALE(pt[-1]), start); + goto close; + } + } + _lineTo(rw, start); + close: + first = last + 1; + } +} + + +static int _genRle(RleWorker& rw) +{ + if (setjmp(rw.jmpBuf) == 0) { + _decomposeOutline(rw); + if (!rw.invalid) _recordCell(rw); + return 0; + } + return -1; //lack of cell memory +} + + +static SwSpan* _intersectSpansRegion(const SwRleData *clip, const SwRleData *target, SwSpan *outSpans, uint32_t outSpansCnt) +{ + auto out = outSpans; + auto spans = target->spans; + auto end = target->spans + target->size; + auto clipSpans = clip->spans; + auto clipEnd = clip->spans + clip->size; + + while (spans < end && clipSpans < clipEnd) { + //align y coordinates. + if (clipSpans->y > spans->y) { + ++spans; + continue; + } + if (spans->y > clipSpans->y) { + ++clipSpans; + continue; + } + + //Try clipping with all clip spans which have a same y coordinate. + auto temp = clipSpans; + while(temp < clipEnd && outSpansCnt > 0 && temp->y == clipSpans->y) { + auto sx1 = spans->x; + auto sx2 = sx1 + spans->len; + auto cx1 = temp->x; + auto cx2 = cx1 + temp->len; + + //The span must be left(x1) to right(x2) direction. Not intersected. + if (cx2 < sx1 || sx2 < cx1) { + ++temp; + continue; + } + + //clip span region. + auto x = sx1 > cx1 ? sx1 : cx1; + auto len = (sx2 < cx2 ? sx2 : cx2) - x; + if (len > 0) { + out->x = x; + out->y = temp->y; + out->len = len; + out->coverage = (uint8_t)(((spans->coverage * temp->coverage) + 0xff) >> 8); + ++out; + --outSpansCnt; + } + ++temp; + } + ++spans; + } + return out; +} + + +static SwSpan* _intersectSpansRect(const SwBBox *bbox, const SwRleData *targetRle, SwSpan *outSpans, uint32_t outSpansCnt) +{ + auto out = outSpans; + auto spans = targetRle->spans; + auto end = targetRle->spans + targetRle->size; + auto minx = static_cast(bbox->min.x); + auto miny = static_cast(bbox->min.y); + auto maxx = minx + static_cast(bbox->max.x - bbox->min.x) - 1; + auto maxy = miny + static_cast(bbox->max.y - bbox->min.y) - 1; + + while (outSpansCnt > 0 && spans < end) { + if (spans->y > maxy) { + spans = end; + break; + } + if (spans->y < miny || spans->x > maxx || spans->x + spans->len <= minx) { + ++spans; + continue; + } + if (spans->x < minx) { + out->len = (spans->len - (minx - spans->x)) < (maxx - minx + 1) ? (spans->len - (minx - spans->x)) : (maxx - minx + 1); + out->x = minx; + } + else { + out->x = spans->x; + out->len = spans->len < (maxx - spans->x + 1) ? spans->len : (maxx - spans->x + 1); + } + if (out->len > 0) { + out->y = spans->y; + out->coverage = spans->coverage; + ++out; + --outSpansCnt; + } + ++spans; + } + return out; +} + + +static SwSpan* _mergeSpansRegion(const SwRleData *clip1, const SwRleData *clip2, SwSpan *outSpans) +{ + auto out = outSpans; + auto spans1 = clip1->spans; + auto end1 = clip1->spans + clip1->size; + auto spans2 = clip2->spans; + auto end2 = clip2->spans + clip2->size; + + //list two spans up in y order + //TODO: Remove duplicated regions? + while (spans1 < end1 && spans2 < end2) { + while (spans1 < end1 && spans1->y <= spans2->y) { + *out = *spans1; + ++spans1; + ++out; + } + if (spans1 >= end1) break; + while (spans2 < end2 && spans2->y <= spans1->y) { + *out = *spans2; + ++spans2; + ++out; + } + } + + //Leftovers + while (spans1 < end1) { + *out = *spans1; + ++spans1; + ++out; + } + while (spans2 < end2) { + *out = *spans2; + ++spans2; + ++out; + } + + return out; +} + + +void _replaceClipSpan(SwRleData *rle, SwSpan* clippedSpans, uint32_t size) +{ + free(rle->spans); + rle->spans = clippedSpans; + rle->size = rle->alloc = size; +} + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +SwRleData* rleRender(SwRleData* rle, const SwOutline* outline, const SwBBox& renderRegion, bool antiAlias) +{ + constexpr auto RENDER_POOL_SIZE = 16384L; + constexpr auto BAND_SIZE = 40; + + //TODO: We can preserve several static workers in advance + RleWorker rw; + Cell buffer[RENDER_POOL_SIZE / sizeof(Cell)]; + + //Init Cells + rw.buffer = buffer; + rw.bufferSize = sizeof(buffer); + rw.yCells = reinterpret_cast(buffer); + rw.cells = nullptr; + rw.maxCells = 0; + rw.cellsCnt = 0; + rw.area = 0; + rw.cover = 0; + rw.invalid = true; + rw.cellMin = renderRegion.min; + rw.cellMax = renderRegion.max; + rw.cellXCnt = rw.cellMax.x - rw.cellMin.x; + rw.cellYCnt = rw.cellMax.y - rw.cellMin.y; + rw.ySpan = 0; + rw.outline = const_cast(outline); + rw.bandSize = rw.bufferSize / (sizeof(Cell) * 8); //bandSize: 64 + rw.bandShoot = 0; + rw.antiAlias = antiAlias; + + if (!rle) rw.rle = reinterpret_cast(calloc(1, sizeof(SwRleData))); + else rw.rle = rle; + + //Generate RLE + Band bands[BAND_SIZE]; + Band* band; + + /* set up vertical bands */ + auto bandCnt = static_cast((rw.cellMax.y - rw.cellMin.y) / rw.bandSize); + if (bandCnt == 0) bandCnt = 1; + else if (bandCnt >= BAND_SIZE) bandCnt = (BAND_SIZE - 1); + + auto min = rw.cellMin.y; + auto yMax = rw.cellMax.y; + SwCoord max; + int ret; + + for (int n = 0; n < bandCnt; ++n, min = max) { + max = min + rw.bandSize; + if (n == bandCnt -1 || max > yMax) max = yMax; + + bands[0].min = min; + bands[0].max = max; + band = bands; + + while (band >= bands) { + rw.yCells = static_cast(rw.buffer); + rw.yCnt = band->max - band->min; + + int cellStart = sizeof(Cell*) * (int)rw.yCnt; + int cellMod = cellStart % sizeof(Cell); + + if (cellMod > 0) cellStart += sizeof(Cell) - cellMod; + + auto cellEnd = rw.bufferSize; + cellEnd -= cellEnd % sizeof(Cell); + + auto cellsMax = reinterpret_cast((char*)rw.buffer + cellEnd); + rw.cells = reinterpret_cast((char*)rw.buffer + cellStart); + + if (rw.cells >= cellsMax) goto reduce_bands; + + rw.maxCells = cellsMax - rw.cells; + if (rw.maxCells < 2) goto reduce_bands; + + for (int y = 0; y < rw.yCnt; ++y) + rw.yCells[y] = nullptr; + + rw.cellsCnt = 0; + rw.invalid = true; + rw.cellMin.y = band->min; + rw.cellMax.y = band->max; + rw.cellYCnt = band->max - band->min; + + ret = _genRle(rw); + if (ret == 0) { + _sweep(rw); + --band; + continue; + } else if (ret == 1) { + goto error; + } + + reduce_bands: + /* render pool overflow: we will reduce the render band by half */ + auto bottom = band->min; + auto top = band->max; + auto middle = bottom + ((top - bottom) >> 1); + + /* This is too complex for a single scanline; there must + be some problems */ + if (middle == bottom) goto error; + + if (bottom - top >= rw.bandSize) ++rw.bandShoot; + + band[1].min = bottom; + band[1].max = middle; + band[0].min = middle; + band[0].max = top; + ++band; + } + } + + if (rw.bandShoot > 8 && rw.bandSize > 16) + rw.bandSize = (rw.bandSize >> 1); + + return rw.rle; + +error: + free(rw.rle); + rw.rle = nullptr; + return nullptr; +} + + +SwRleData* rleRender(const SwBBox* bbox) +{ + auto width = static_cast(bbox->max.x - bbox->min.x); + auto height = static_cast(bbox->max.y - bbox->min.y); + + auto rle = static_cast(malloc(sizeof(SwRleData))); + rle->spans = static_cast(malloc(sizeof(SwSpan) * height)); + rle->size = height; + rle->alloc = height; + + auto span = rle->spans; + for (uint16_t i = 0; i < height; ++i, ++span) { + span->x = bbox->min.x; + span->y = bbox->min.y + i; + span->len = width; + span->coverage = 255; + } + + return rle; +} + + +void rleReset(SwRleData* rle) +{ + if (!rle) return; + rle->size = 0; +} + + +void rleFree(SwRleData* rle) +{ + if (!rle) return; + if (rle->spans) free(rle->spans); + free(rle); +} + + +void rleMerge(SwRleData* rle, SwRleData* clip1, SwRleData* clip2) +{ + if (!rle || (!clip1 && !clip2)) return; + if (clip1 && clip1->size == 0 && clip2 && clip2->size == 0) return; + + TVGLOG("SW_ENGINE", "Unifying Rle!"); + + //clip1 is empty, just copy clip2 + if (!clip1 || clip1->size == 0) { + if (clip2) { + auto spans = static_cast(malloc(sizeof(SwSpan) * (clip2->size))); + memcpy(spans, clip2->spans, clip2->size); + _replaceClipSpan(rle, spans, clip2->size); + } else { + _replaceClipSpan(rle, nullptr, 0); + } + return; + } + + //clip2 is empty, just copy clip1 + if (!clip2 || clip2->size == 0) { + if (clip1) { + auto spans = static_cast(malloc(sizeof(SwSpan) * (clip1->size))); + memcpy(spans, clip1->spans, clip1->size); + _replaceClipSpan(rle, spans, clip1->size); + } else { + _replaceClipSpan(rle, nullptr, 0); + } + return; + } + + auto spanCnt = clip1->size + clip2->size; + auto spans = static_cast(malloc(sizeof(SwSpan) * spanCnt)); + auto spansEnd = _mergeSpansRegion(clip1, clip2, spans); + + _replaceClipSpan(rle, spans, spansEnd - spans); +} + + +void rleClipPath(SwRleData *rle, const SwRleData *clip) +{ + if (rle->size == 0 || clip->size == 0) return; + auto spanCnt = rle->size > clip->size ? rle->size : clip->size; + auto spans = static_cast(malloc(sizeof(SwSpan) * (spanCnt))); + auto spansEnd = _intersectSpansRegion(clip, rle, spans, spanCnt); + + _replaceClipSpan(rle, spans, spansEnd - spans); + + TVGLOG("SW_ENGINE", "Using ClipPath!"); +} + + +void rleClipRect(SwRleData *rle, const SwBBox* clip) +{ + if (rle->size == 0) return; + auto spans = static_cast(malloc(sizeof(SwSpan) * (rle->size))); + auto spansEnd = _intersectSpansRect(clip, rle, spans, rle->size); + + _replaceClipSpan(rle, spans, spansEnd - spans); + + TVGLOG("SW_ENGINE", "Using ClipRect!"); +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSwShape.cpp b/include/liblvgl/libs/thorvg/tvgSwShape.cpp new file mode 100644 index 00000000..e2652c1e --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSwShape.cpp @@ -0,0 +1,675 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgSwCommon.h" +#include "tvgMath.h" +#include "tvgLines.h" + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +static bool _outlineBegin(SwOutline& outline) +{ + //Make a contour if lineTo/curveTo without calling close or moveTo beforehand. + if (outline.pts.empty()) return false; + outline.cntrs.push(outline.pts.count - 1); + outline.closed.push(false); + outline.pts.push(outline.pts[outline.cntrs.last()]); + outline.types.push(SW_CURVE_TYPE_POINT); + return false; +} + + +static bool _outlineEnd(SwOutline& outline) +{ + if (outline.pts.empty()) return false; + outline.cntrs.push(outline.pts.count - 1); + outline.closed.push(false); + return false; +} + + +static bool _outlineMoveTo(SwOutline& outline, const Point* to, const Matrix* transform, bool closed = false) +{ + //make it a contour, if the last contour is not closed yet. + if (!closed) _outlineEnd(outline); + + outline.pts.push(mathTransform(to, transform)); + outline.types.push(SW_CURVE_TYPE_POINT); + return false; +} + + +static void _outlineLineTo(SwOutline& outline, const Point* to, const Matrix* transform) +{ + outline.pts.push(mathTransform(to, transform)); + outline.types.push(SW_CURVE_TYPE_POINT); +} + + +static void _outlineCubicTo(SwOutline& outline, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform) +{ + outline.pts.push(mathTransform(ctrl1, transform)); + outline.types.push(SW_CURVE_TYPE_CUBIC); + + outline.pts.push(mathTransform(ctrl2, transform)); + outline.types.push(SW_CURVE_TYPE_CUBIC); + + outline.pts.push(mathTransform(to, transform)); + outline.types.push(SW_CURVE_TYPE_POINT); +} + + +static bool _outlineClose(SwOutline& outline) +{ + uint32_t i; + if (outline.cntrs.count > 0) i = outline.cntrs.last() + 1; + else i = 0; + + //Make sure there is at least one point in the current path + if (outline.pts.count == i) return false; + + //Close the path + outline.pts.push(outline.pts[i]); + outline.cntrs.push(outline.pts.count - 1); + outline.types.push(SW_CURVE_TYPE_POINT); + outline.closed.push(true); + + return true; +} + + +static void _dashLineTo(SwDashStroke& dash, const Point* to, const Matrix* transform) +{ + Line cur = {dash.ptCur, *to}; + auto len = lineLength(cur.pt1, cur.pt2); + + if (mathZero(len)) { + _outlineMoveTo(*dash.outline, &dash.ptCur, transform); + //draw the current line fully + } else if (len < dash.curLen) { + dash.curLen -= len; + if (!dash.curOpGap) { + if (dash.move) { + _outlineMoveTo(*dash.outline, &dash.ptCur, transform); + dash.move = false; + } + _outlineLineTo(*dash.outline, to, transform); + } + //draw the current line partially + } else { + while (len - dash.curLen > 0.0001f) { + Line left, right; + if (dash.curLen > 0) { + len -= dash.curLen; + lineSplitAt(cur, dash.curLen, left, right); + if (!dash.curOpGap) { + if (dash.move || dash.pattern[dash.curIdx] - dash.curLen < FLOAT_EPSILON) { + _outlineMoveTo(*dash.outline, &left.pt1, transform); + dash.move = false; + } + _outlineLineTo(*dash.outline, &left.pt2, transform); + } + } else { + right = cur; + } + dash.curIdx = (dash.curIdx + 1) % dash.cnt; + dash.curLen = dash.pattern[dash.curIdx]; + dash.curOpGap = !dash.curOpGap; + cur = right; + dash.ptCur = cur.pt1; + dash.move = true; + } + //leftovers + dash.curLen -= len; + if (!dash.curOpGap) { + if (dash.move) { + _outlineMoveTo(*dash.outline, &cur.pt1, transform); + dash.move = false; + } + _outlineLineTo(*dash.outline, &cur.pt2, transform); + } + if (dash.curLen < 1 && TO_SWCOORD(len) > 1) { + //move to next dash + dash.curIdx = (dash.curIdx + 1) % dash.cnt; + dash.curLen = dash.pattern[dash.curIdx]; + dash.curOpGap = !dash.curOpGap; + } + } + dash.ptCur = *to; +} + + +static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform) +{ + Bezier cur = {dash.ptCur, *ctrl1, *ctrl2, *to}; + auto len = bezLength(cur); + + //draw the current line fully + if (mathZero(len)) { + _outlineMoveTo(*dash.outline, &dash.ptCur, transform); + } else if (len < dash.curLen) { + dash.curLen -= len; + if (!dash.curOpGap) { + if (dash.move) { + _outlineMoveTo(*dash.outline, &dash.ptCur, transform); + dash.move = false; + } + _outlineCubicTo(*dash.outline, ctrl1, ctrl2, to, transform); + } + //draw the current line partially + } else { + while ((len - dash.curLen) > 0.0001f) { + Bezier left, right; + if (dash.curLen > 0) { + len -= dash.curLen; + bezSplitAt(cur, dash.curLen, left, right); + if (!dash.curOpGap) { + if (dash.move || dash.pattern[dash.curIdx] - dash.curLen < FLOAT_EPSILON) { + _outlineMoveTo(*dash.outline, &left.start, transform); + dash.move = false; + } + _outlineCubicTo(*dash.outline, &left.ctrl1, &left.ctrl2, &left.end, transform); + } + } else { + right = cur; + } + dash.curIdx = (dash.curIdx + 1) % dash.cnt; + dash.curLen = dash.pattern[dash.curIdx]; + dash.curOpGap = !dash.curOpGap; + cur = right; + dash.ptCur = right.start; + dash.move = true; + } + //leftovers + dash.curLen -= len; + if (!dash.curOpGap) { + if (dash.move) { + _outlineMoveTo(*dash.outline, &cur.start, transform); + dash.move = false; + } + _outlineCubicTo(*dash.outline, &cur.ctrl1, &cur.ctrl2, &cur.end, transform); + } + if (dash.curLen < 1 && TO_SWCOORD(len) > 1) { + //move to next dash + dash.curIdx = (dash.curIdx + 1) % dash.cnt; + dash.curLen = dash.pattern[dash.curIdx]; + dash.curOpGap = !dash.curOpGap; + } + } + dash.ptCur = *to; +} + + +static void _dashClose(SwDashStroke& dash, const Matrix* transform) +{ + _dashLineTo(dash, &dash.ptStart, transform); +} + + +static void _dashMoveTo(SwDashStroke& dash, const Point* pts) +{ + dash.ptCur = *pts; + dash.ptStart = *pts; + dash.move = true; +} + + +static void _dashMoveTo(SwDashStroke& dash, uint32_t offIdx, float offset, const Point* pts) +{ + dash.curIdx = offIdx % dash.cnt; + dash.curLen = dash.pattern[dash.curIdx] - offset; + dash.curOpGap = offIdx % 2; + dash.ptStart = dash.ptCur = *pts; + dash.move = true; +} + + +static SwOutline* _genDashOutline(const RenderShape* rshape, const Matrix* transform, float length, SwMpool* mpool, unsigned tid) +{ + const PathCommand* cmds = rshape->path.cmds.data; + auto cmdCnt = rshape->path.cmds.count; + const Point* pts = rshape->path.pts.data; + auto ptsCnt = rshape->path.pts.count; + + //No actual shape data + if (cmdCnt == 0 || ptsCnt == 0) return nullptr; + + SwDashStroke dash; + auto offset = 0.0f; + auto trimmed = false; + + dash.cnt = rshape->strokeDash((const float**)&dash.pattern, &offset); + + //dash by trimming. + if (length > 0.0f && dash.cnt == 0) { + auto begin = length * rshape->stroke->trim.begin; + auto end = length * rshape->stroke->trim.end; + + //TODO: mix trimming + dash style + + //default + if (end > begin) { + if (begin > 0.0f) dash.cnt += 4; + else dash.cnt += 2; + //looping + } else dash.cnt += 3; + + dash.pattern = (float*)malloc(sizeof(float) * dash.cnt); + + if (dash.cnt == 2) { + dash.pattern[0] = end - begin; + dash.pattern[1] = length - (end - begin); + } else if (dash.cnt == 3) { + dash.pattern[0] = end; + dash.pattern[1] = (begin - end); + dash.pattern[2] = length - begin; + } else { + dash.pattern[0] = 0; //zero dash to start with a space. + dash.pattern[1] = begin; + dash.pattern[2] = end - begin; + dash.pattern[3] = length - end; + } + + trimmed = true; + //just a dash style. + } else { + if (dash.cnt == 0) return nullptr; + } + + //offset? + auto patternLength = 0.0f; + uint32_t offIdx = 0; + if (!mathZero(offset)) { + for (size_t i = 0; i < dash.cnt; ++i) patternLength += dash.pattern[i]; + bool isOdd = dash.cnt % 2; + if (isOdd) patternLength *= 2; + + offset = fmodf(offset, patternLength); + if (offset < 0) offset += patternLength; + + for (size_t i = 0; i < dash.cnt * (1 + (size_t)isOdd); ++i, ++offIdx) { + auto curPattern = dash.pattern[i % dash.cnt]; + if (offset < curPattern) break; + offset -= curPattern; + } + } + + dash.outline = mpoolReqDashOutline(mpool, tid); + + //must begin with moveTo + if (cmds[0] == PathCommand::MoveTo) { + _dashMoveTo(dash, offIdx, offset, pts); + cmds++; + pts++; + } + + while (--cmdCnt > 0) { + switch (*cmds) { + case PathCommand::Close: { + _dashClose(dash, transform); + break; + } + case PathCommand::MoveTo: { + if (rshape->stroke->trim.individual) _dashMoveTo(dash, pts); + else _dashMoveTo(dash, offIdx, offset, pts); + ++pts; + break; + } + case PathCommand::LineTo: { + _dashLineTo(dash, pts, transform); + ++pts; + break; + } + case PathCommand::CubicTo: { + _dashCubicTo(dash, pts, pts + 1, pts + 2, transform); + pts += 3; + break; + } + } + ++cmds; + } + + _outlineEnd(*dash.outline); + + if (trimmed) free(dash.pattern); + + return dash.outline; +} + + +static float _outlineLength(const RenderShape* rshape) +{ + const PathCommand* cmds = rshape->path.cmds.data; + auto cmdCnt = rshape->path.cmds.count; + const Point* pts = rshape->path.pts.data; + auto ptsCnt = rshape->path.pts.count; + + //No actual shape data + if (cmdCnt == 0 || ptsCnt == 0) return 0.0f; + + const Point* close = nullptr; + auto length = 0.0f; + auto slength = -1.0f; + auto simultaneous = !rshape->stroke->trim.individual; + + //Compute the whole length + while (cmdCnt-- > 0) { + switch (*cmds) { + case PathCommand::Close: { + length += mathLength(pts - 1, close); + //retrieve the max length of the shape if the simultaneous mode. + if (simultaneous) { + if (slength < length) slength = length; + length = 0.0f; + } + break; + } + case PathCommand::MoveTo: { + close = pts; + ++pts; + break; + } + case PathCommand::LineTo: { + length += mathLength(pts - 1, pts); + ++pts; + break; + } + case PathCommand::CubicTo: { + length += bezLength({*(pts - 1), *pts, *(pts + 1), *(pts + 2)}); + pts += 3; + break; + } + } + ++cmds; + } + if (simultaneous && slength > length) return slength; + else return length; +} + + +static bool _axisAlignedRect(const SwOutline* outline) +{ + //Fast Track: axis-aligned rectangle? + if (outline->pts.count != 5) return false; + + auto pt1 = outline->pts.data + 0; + auto pt2 = outline->pts.data + 1; + auto pt3 = outline->pts.data + 2; + auto pt4 = outline->pts.data + 3; + + auto a = SwPoint{pt1->x, pt3->y}; + auto b = SwPoint{pt3->x, pt1->y}; + + if ((*pt2 == a && *pt4 == b) || (*pt2 == b && *pt4 == a)) return true; + + return false; +} + + +static bool _genOutline(SwShape* shape, const RenderShape* rshape, const Matrix* transform, SwMpool* mpool, unsigned tid, bool hasComposite) +{ + const PathCommand* cmds = rshape->path.cmds.data; + auto cmdCnt = rshape->path.cmds.count; + const Point* pts = rshape->path.pts.data; + auto ptsCnt = rshape->path.pts.count; + + //No actual shape data + if (cmdCnt == 0 || ptsCnt == 0) return false; + + shape->outline = mpoolReqOutline(mpool, tid); + auto outline = shape->outline; + auto closed = false; + + //Generate Outlines + while (cmdCnt-- > 0) { + switch (*cmds) { + case PathCommand::Close: { + if (!closed) closed = _outlineClose(*outline); + break; + } + case PathCommand::MoveTo: { + closed = _outlineMoveTo(*outline, pts, transform, closed); + ++pts; + break; + } + case PathCommand::LineTo: { + if (closed) closed = _outlineBegin(*outline); + _outlineLineTo(*outline, pts, transform); + ++pts; + break; + } + case PathCommand::CubicTo: { + if (closed) closed = _outlineBegin(*outline); + _outlineCubicTo(*outline, pts, pts + 1, pts + 2, transform); + pts += 3; + break; + } + } + ++cmds; + } + + if (!closed) _outlineEnd(*outline); + + outline->fillRule = rshape->rule; + shape->outline = outline; + + shape->fastTrack = (!hasComposite && _axisAlignedRect(shape->outline)); + return true; +} + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +bool shapePrepare(SwShape* shape, const RenderShape* rshape, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid, bool hasComposite) +{ + if (!_genOutline(shape, rshape, transform, mpool, tid, hasComposite)) return false; + if (!mathUpdateOutlineBBox(shape->outline, clipRegion, renderRegion, shape->fastTrack)) return false; + + //Keep it for Rasterization Region + shape->bbox = renderRegion; + + //Check valid region + if (renderRegion.max.x - renderRegion.min.x < 1 && renderRegion.max.y - renderRegion.min.y < 1) return false; + + //Check boundary + if (renderRegion.min.x >= clipRegion.max.x || renderRegion.min.y >= clipRegion.max.y || + renderRegion.max.x <= clipRegion.min.x || renderRegion.max.y <= clipRegion.min.y) return false; + + return true; +} + + +bool shapePrepared(const SwShape* shape) +{ + return shape->rle ? true : false; +} + + +bool shapeGenRle(SwShape* shape, TVG_UNUSED const RenderShape* rshape, bool antiAlias) +{ + //FIXME: Should we draw it? + //Case: Stroke Line + //if (shape.outline->opened) return true; + + //Case A: Fast Track Rectangle Drawing + if (shape->fastTrack) return true; + + //Case B: Normal Shape RLE Drawing + if ((shape->rle = rleRender(shape->rle, shape->outline, shape->bbox, antiAlias))) return true; + + return false; +} + + +void shapeDelOutline(SwShape* shape, SwMpool* mpool, uint32_t tid) +{ + mpoolRetOutline(mpool, tid); + shape->outline = nullptr; +} + + +void shapeReset(SwShape* shape) +{ + rleReset(shape->rle); + rleReset(shape->strokeRle); + shape->fastTrack = false; + shape->bbox.reset(); +} + + +void shapeFree(SwShape* shape) +{ + rleFree(shape->rle); + shape->rle = nullptr; + + shapeDelFill(shape); + + if (shape->stroke) { + rleFree(shape->strokeRle); + shape->strokeRle = nullptr; + strokeFree(shape->stroke); + shape->stroke = nullptr; + } +} + + +void shapeDelStroke(SwShape* shape) +{ + if (!shape->stroke) return; + rleFree(shape->strokeRle); + shape->strokeRle = nullptr; + strokeFree(shape->stroke); + shape->stroke = nullptr; +} + + +void shapeResetStroke(SwShape* shape, const RenderShape* rshape, const Matrix* transform) +{ + if (!shape->stroke) shape->stroke = static_cast(calloc(1, sizeof(SwStroke))); + auto stroke = shape->stroke; + if (!stroke) return; + + strokeReset(stroke, rshape, transform); + rleReset(shape->strokeRle); +} + + +bool shapeGenStrokeRle(SwShape* shape, const RenderShape* rshape, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid) +{ + SwOutline* shapeOutline = nullptr; + SwOutline* strokeOutline = nullptr; + auto dashStroking = false; + auto ret = true; + + auto length = rshape->strokeTrim() ? _outlineLength(rshape) : 0.0f; + + //Dash style (+trimming) + if (rshape->stroke->dashCnt > 0 || length > 0) { + shapeOutline = _genDashOutline(rshape, transform, length, mpool, tid); + if (!shapeOutline) return false; + dashStroking = true; + //Normal style + } else { + if (!shape->outline) { + if (!_genOutline(shape, rshape, transform, mpool, tid, false)) return false; + } + shapeOutline = shape->outline; + } + + if (!strokeParseOutline(shape->stroke, *shapeOutline)) { + ret = false; + goto clear; + } + + strokeOutline = strokeExportOutline(shape->stroke, mpool, tid); + + if (!mathUpdateOutlineBBox(strokeOutline, clipRegion, renderRegion, false)) { + ret = false; + goto clear; + } + + shape->strokeRle = rleRender(shape->strokeRle, strokeOutline, renderRegion, true); + +clear: + if (dashStroking) mpoolRetDashOutline(mpool, tid); + mpoolRetStrokeOutline(mpool, tid); + + return ret; +} + + +bool shapeGenFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint8_t opacity, bool ctable) +{ + return fillGenColorTable(shape->fill, fill, transform, surface, opacity, ctable); +} + + +bool shapeGenStrokeFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint8_t opacity, bool ctable) +{ + return fillGenColorTable(shape->stroke->fill, fill, transform, surface, opacity, ctable); +} + + +void shapeResetFill(SwShape* shape) +{ + if (!shape->fill) { + shape->fill = static_cast(calloc(1, sizeof(SwFill))); + if (!shape->fill) return; + } + fillReset(shape->fill); +} + + +void shapeResetStrokeFill(SwShape* shape) +{ + if (!shape->stroke->fill) { + shape->stroke->fill = static_cast(calloc(1, sizeof(SwFill))); + if (!shape->stroke->fill) return; + } + fillReset(shape->stroke->fill); +} + + +void shapeDelFill(SwShape* shape) +{ + if (!shape->fill) return; + fillFree(shape->fill); + shape->fill = nullptr; +} + + +void shapeDelStrokeFill(SwShape* shape) +{ + if (!shape->stroke->fill) return; + fillFree(shape->stroke->fill); + shape->stroke->fill = nullptr; +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgSwStroke.cpp b/include/liblvgl/libs/thorvg/tvgSwStroke.cpp new file mode 100644 index 00000000..954250c8 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgSwStroke.cpp @@ -0,0 +1,913 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include +#include +#include "tvgSwCommon.h" + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +static constexpr auto SW_STROKE_TAG_POINT = 1; +static constexpr auto SW_STROKE_TAG_CUBIC = 2; +static constexpr auto SW_STROKE_TAG_BEGIN = 4; +static constexpr auto SW_STROKE_TAG_END = 8; + +static inline SwFixed SIDE_TO_ROTATE(const int32_t s) +{ + return (SW_ANGLE_PI2 - static_cast(s) * SW_ANGLE_PI); +} + + +static inline void SCALE(const SwStroke& stroke, SwPoint& pt) +{ + pt.x = static_cast(pt.x * stroke.sx); + pt.y = static_cast(pt.y * stroke.sy); +} + + +static void _growBorder(SwStrokeBorder* border, uint32_t newPts) +{ + auto maxOld = border->maxPts; + auto maxNew = border->ptsCnt + newPts; + + if (maxNew <= maxOld) return; + + auto maxCur = maxOld; + + while (maxCur < maxNew) + maxCur += (maxCur >> 1) + 16; + //OPTIMIZE: use mempool! + border->pts = static_cast(realloc(border->pts, maxCur * sizeof(SwPoint))); + border->tags = static_cast(realloc(border->tags, maxCur * sizeof(uint8_t))); + border->maxPts = maxCur; +} + + +static void _borderClose(SwStrokeBorder* border, bool reverse) +{ + auto start = border->start; + auto count = border->ptsCnt; + + //Don't record empty paths! + if (count <= start + 1U) { + border->ptsCnt = start; + } else { + /* Copy the last point to the start of this sub-path, + since it contains the adjusted starting coordinates */ + border->ptsCnt = --count; + border->pts[start] = border->pts[count]; + + if (reverse) { + //reverse the points + auto pt1 = border->pts + start + 1; + auto pt2 = border->pts + count - 1; + + while (pt1 < pt2) { + auto tmp = *pt1; + *pt1 = *pt2; + *pt2 = tmp; + ++pt1; + --pt2; + } + + //reverse the tags + auto tag1 = border->tags + start + 1; + auto tag2 = border->tags + count - 1; + + while (tag1 < tag2) { + auto tmp = *tag1; + *tag1 = *tag2; + *tag2 = tmp; + ++tag1; + --tag2; + } + } + + border->tags[start] |= SW_STROKE_TAG_BEGIN; + border->tags[count - 1] |= SW_STROKE_TAG_END; + } + + border->start = -1; + border->movable = false; +} + + +static void _borderCubicTo(SwStrokeBorder* border, const SwPoint& ctrl1, const SwPoint& ctrl2, const SwPoint& to) +{ + _growBorder(border, 3); + + auto pt = border->pts + border->ptsCnt; + auto tag = border->tags + border->ptsCnt; + + pt[0] = ctrl1; + pt[1] = ctrl2; + pt[2] = to; + + tag[0] = SW_STROKE_TAG_CUBIC; + tag[1] = SW_STROKE_TAG_CUBIC; + tag[2] = SW_STROKE_TAG_POINT; + + border->ptsCnt += 3; + border->movable = false; +} + + +static void _borderArcTo(SwStrokeBorder* border, const SwPoint& center, SwFixed radius, SwFixed angleStart, SwFixed angleDiff, SwStroke& stroke) +{ + constexpr SwFixed ARC_CUBIC_ANGLE = SW_ANGLE_PI / 2; + SwPoint a = {static_cast(radius), 0}; + mathRotate(a, angleStart); + SCALE(stroke, a); + a += center; + + auto total = angleDiff; + auto angle = angleStart; + auto rotate = (angleDiff >= 0) ? SW_ANGLE_PI2 : -SW_ANGLE_PI2; + + while (total != 0) { + auto step = total; + if (step > ARC_CUBIC_ANGLE) step = ARC_CUBIC_ANGLE; + else if (step < -ARC_CUBIC_ANGLE) step = -ARC_CUBIC_ANGLE; + + auto next = angle + step; + auto theta = step; + if (theta < 0) theta = -theta; + + theta >>= 1; + + //compute end point + SwPoint b = {static_cast(radius), 0}; + mathRotate(b, next); + SCALE(stroke, b); + b += center; + + //compute first and second control points + auto length = mathMulDiv(radius, mathSin(theta) * 4, (0x10000L + mathCos(theta)) * 3); + + SwPoint a2 = {static_cast(length), 0}; + mathRotate(a2, angle + rotate); + SCALE(stroke, a2); + a2 += a; + + SwPoint b2 = {static_cast(length), 0}; + mathRotate(b2, next - rotate); + SCALE(stroke, b2); + b2 += b; + + //add cubic arc + _borderCubicTo(border, a2, b2, b); + + //process the rest of the arc? + a = b; + total -= step; + angle = next; + } +} + + +static void _borderLineTo(SwStrokeBorder* border, const SwPoint& to, bool movable) +{ + if (border->movable) { + //move last point + border->pts[border->ptsCnt - 1] = to; + } else { + //don't add zero-length line_to + if (border->ptsCnt > 0 && (border->pts[border->ptsCnt - 1] - to).small()) return; + + _growBorder(border, 1); + border->pts[border->ptsCnt] = to; + border->tags[border->ptsCnt] = SW_STROKE_TAG_POINT; + border->ptsCnt += 1; + } + + border->movable = movable; +} + + +static void _borderMoveTo(SwStrokeBorder* border, SwPoint& to) +{ + //close current open path if any? + if (border->start >= 0) _borderClose(border, false); + + border->start = border->ptsCnt; + border->movable = false; + + _borderLineTo(border, to, false); +} + + +static void _arcTo(SwStroke& stroke, int32_t side) +{ + auto border = stroke.borders + side; + auto rotate = SIDE_TO_ROTATE(side); + auto total = mathDiff(stroke.angleIn, stroke.angleOut); + if (total == SW_ANGLE_PI) total = -rotate * 2; + + _borderArcTo(border, stroke.center, stroke.width, stroke.angleIn + rotate, total, stroke); + border->movable = false; +} + + +static void _outside(SwStroke& stroke, int32_t side, SwFixed lineLength) +{ + auto border = stroke.borders + side; + + if (stroke.join == StrokeJoin::Round) { + _arcTo(stroke, side); + } else { + //this is a mitered (pointed) or beveled (truncated) corner + auto rotate = SIDE_TO_ROTATE(side); + auto bevel = (stroke.join == StrokeJoin::Bevel) ? true : false; + SwFixed phi = 0; + SwFixed thcos = 0; + + if (!bevel) { + auto theta = mathDiff(stroke.angleIn, stroke.angleOut); + if (theta == SW_ANGLE_PI) { + theta = rotate; + phi = stroke.angleIn; + } else { + theta /= 2; + phi = stroke.angleIn + theta + rotate; + } + + thcos = mathCos(theta); + auto sigma = mathMultiply(stroke.miterlimit, thcos); + + //is miter limit exceeded? + if (sigma < 0x10000L) bevel = true; + } + + //this is a bevel (broken angle) + if (bevel) { + SwPoint delta = {static_cast(stroke.width), 0}; + mathRotate(delta, stroke.angleOut + rotate); + SCALE(stroke, delta); + delta += stroke.center; + border->movable = false; + _borderLineTo(border, delta, false); + //this is a miter (intersection) + } else { + auto length = mathDivide(stroke.width, thcos); + SwPoint delta = {static_cast(length), 0}; + mathRotate(delta, phi); + SCALE(stroke, delta); + delta += stroke.center; + _borderLineTo(border, delta, false); + + /* Now add and end point + Only needed if not lineto (lineLength is zero for curves) */ + if (lineLength == 0) { + delta = {static_cast(stroke.width), 0}; + mathRotate(delta, stroke.angleOut + rotate); + SCALE(stroke, delta); + delta += stroke.center; + _borderLineTo(border, delta, false); + } + } + } +} + + +static void _inside(SwStroke& stroke, int32_t side, SwFixed lineLength) +{ + auto border = stroke.borders + side; + auto theta = mathDiff(stroke.angleIn, stroke.angleOut) / 2; + SwPoint delta; + bool intersect = false; + + /* Only intersect borders if between two line_to's and both + lines are long enough (line length is zero for curves). */ + if (border->movable && lineLength > 0) { + //compute minimum required length of lines + SwFixed minLength = abs(mathMultiply(stroke.width, mathTan(theta))); + if (stroke.lineLength >= minLength && lineLength >= minLength) intersect = true; + } + + auto rotate = SIDE_TO_ROTATE(side); + + if (!intersect) { + delta = {static_cast(stroke.width), 0}; + mathRotate(delta, stroke.angleOut + rotate); + SCALE(stroke, delta); + delta += stroke.center; + border->movable = false; + } else { + //compute median angle + auto phi = stroke.angleIn + theta; + auto thcos = mathCos(theta); + delta = {static_cast(mathDivide(stroke.width, thcos)), 0}; + mathRotate(delta, phi + rotate); + SCALE(stroke, delta); + delta += stroke.center; + } + + _borderLineTo(border, delta, false); +} + + +void _processCorner(SwStroke& stroke, SwFixed lineLength) +{ + auto turn = mathDiff(stroke.angleIn, stroke.angleOut); + + //no specific corner processing is required if the turn is 0 + if (turn == 0) return; + + //when we turn to the right, the inside side is 0 + int32_t inside = 0; + + //otherwise, the inside is 1 + if (turn < 0) inside = 1; + + //process the inside + _inside(stroke, inside, lineLength); + + //process the outside + _outside(stroke, 1 - inside, lineLength); +} + + +void _firstSubPath(SwStroke& stroke, SwFixed startAngle, SwFixed lineLength) +{ + SwPoint delta = {static_cast(stroke.width), 0}; + mathRotate(delta, startAngle + SW_ANGLE_PI2); + SCALE(stroke, delta); + + auto pt = stroke.center + delta; + auto border = stroke.borders; + _borderMoveTo(border, pt); + + pt = stroke.center - delta; + ++border; + _borderMoveTo(border, pt); + + /* Save angle, position and line length for last join + lineLength is zero for curves */ + stroke.subPathAngle = startAngle; + stroke.firstPt = false; + stroke.subPathLineLength = lineLength; +} + + +static void _lineTo(SwStroke& stroke, const SwPoint& to) +{ + auto delta = to - stroke.center; + + //a zero-length lineto is a no-op; avoid creating a spurious corner + if (delta.zero()) return; + + /* The lineLength is used to determine the intersection of strokes outlines. + The scale needs to be reverted since the stroke width has not been scaled. + An alternative option is to scale the width of the stroke properly by + calculating the mixture of the sx/sy rating on the stroke direction. */ + delta.x = static_cast(delta.x / stroke.sx); + delta.y = static_cast(delta.y / stroke.sy); + auto lineLength = mathLength(delta); + auto angle = mathAtan(delta); + + delta = {static_cast(stroke.width), 0}; + mathRotate(delta, angle + SW_ANGLE_PI2); + SCALE(stroke, delta); + + //process corner if necessary + if (stroke.firstPt) { + /* This is the first segment of a subpath. We need to add a point to each border + at their respective starting point locations. */ + _firstSubPath(stroke, angle, lineLength); + } else { + //process the current corner + stroke.angleOut = angle; + _processCorner(stroke, lineLength); + } + + //now add a line segment to both the inside and outside paths + auto border = stroke.borders; + auto side = 1; + + while (side >= 0) { + auto pt = to + delta; + + //the ends of lineto borders are movable + _borderLineTo(border, pt, true); + + delta.x = -delta.x; + delta.y = -delta.y; + + --side; + ++border; + } + + stroke.angleIn = angle; + stroke.center = to; + stroke.lineLength = lineLength; +} + + +static void _cubicTo(SwStroke& stroke, const SwPoint& ctrl1, const SwPoint& ctrl2, const SwPoint& to) +{ + SwPoint bezStack[37]; //TODO: static? + auto limit = bezStack + 32; + auto arc = bezStack; + auto firstArc = true; + arc[0] = to; + arc[1] = ctrl2; + arc[2] = ctrl1; + arc[3] = stroke.center; + + while (arc >= bezStack) { + SwFixed angleIn, angleOut, angleMid; + + //initialize with current direction + angleIn = angleOut = angleMid = stroke.angleIn; + + if (arc < limit && !mathSmallCubic(arc, angleIn, angleMid, angleOut)) { + if (stroke.firstPt) stroke.angleIn = angleIn; + mathSplitCubic(arc); + arc += 3; + continue; + } + + if (firstArc) { + firstArc = false; + //process corner if necessary + if (stroke.firstPt) { + _firstSubPath(stroke, angleIn, 0); + } else { + stroke.angleOut = angleIn; + _processCorner(stroke, 0); + } + } else if (abs(mathDiff(stroke.angleIn, angleIn)) > (SW_ANGLE_PI / 8) / 4) { + //if the deviation from one arc to the next is too great add a round corner + stroke.center = arc[3]; + stroke.angleOut = angleIn; + stroke.join = StrokeJoin::Round; + + _processCorner(stroke, 0); + + //reinstate line join style + stroke.join = stroke.joinSaved; + } + + //the arc's angle is small enough; we can add it directly to each border + auto theta1 = mathDiff(angleIn, angleMid) / 2; + auto theta2 = mathDiff(angleMid, angleOut) / 2; + auto phi1 = mathMean(angleIn, angleMid); + auto phi2 = mathMean(angleMid, angleOut); + auto length1 = mathDivide(stroke.width, mathCos(theta1)); + auto length2 = mathDivide(stroke.width, mathCos(theta2)); + SwFixed alpha0 = 0; + + //compute direction of original arc + if (stroke.handleWideStrokes) { + alpha0 = mathAtan(arc[0] - arc[3]); + } + + auto border = stroke.borders; + int32_t side = 0; + + while (side < 2) { + auto rotate = SIDE_TO_ROTATE(side); + + //compute control points + SwPoint _ctrl1 = {static_cast(length1), 0}; + mathRotate(_ctrl1, phi1 + rotate); + SCALE(stroke, _ctrl1); + _ctrl1 += arc[2]; + + SwPoint _ctrl2 = {static_cast(length2), 0}; + mathRotate(_ctrl2, phi2 + rotate); + SCALE(stroke, _ctrl2); + _ctrl2 += arc[1]; + + //compute end point + SwPoint _end = {static_cast(stroke.width), 0}; + mathRotate(_end, angleOut + rotate); + SCALE(stroke, _end); + _end += arc[0]; + + if (stroke.handleWideStrokes) { + /* determine whether the border radius is greater than the radius of + curvature of the original arc */ + auto _start = border->pts[border->ptsCnt - 1]; + auto alpha1 = mathAtan(_end - _start); + + //is the direction of the border arc opposite to that of the original arc? + if (abs(mathDiff(alpha0, alpha1)) > SW_ANGLE_PI / 2) { + + //use the sine rule to find the intersection point + auto beta = mathAtan(arc[3] - _start); + auto gamma = mathAtan(arc[0] - _end); + auto bvec = _end - _start; + auto blen = mathLength(bvec); + auto sinA = abs(mathSin(alpha1 - gamma)); + auto sinB = abs(mathSin(beta - gamma)); + auto alen = mathMulDiv(blen, sinA, sinB); + + SwPoint delta = {static_cast(alen), 0}; + mathRotate(delta, beta); + delta += _start; + + //circumnavigate the negative sector backwards + border->movable = false; + _borderLineTo(border, delta, false); + _borderLineTo(border, _end, false); + _borderCubicTo(border, _ctrl2, _ctrl1, _start); + + //and then move to the endpoint + _borderLineTo(border, _end, false); + + ++side; + ++border; + continue; + } + } + _borderCubicTo(border, _ctrl1, _ctrl2, _end); + ++side; + ++border; + } + arc -= 3; + stroke.angleIn = angleOut; + } + stroke.center = to; +} + + +static void _addCap(SwStroke& stroke, SwFixed angle, int32_t side) +{ + if (stroke.cap == StrokeCap::Square) { + auto rotate = SIDE_TO_ROTATE(side); + auto border = stroke.borders + side; + + SwPoint delta = {static_cast(stroke.width), 0}; + mathRotate(delta, angle); + SCALE(stroke, delta); + + SwPoint delta2 = {static_cast(stroke.width), 0}; + mathRotate(delta2, angle + rotate); + SCALE(stroke, delta2); + delta += stroke.center + delta2; + + _borderLineTo(border, delta, false); + + delta = {static_cast(stroke.width), 0}; + mathRotate(delta, angle); + SCALE(stroke, delta); + + delta2 = {static_cast(stroke.width), 0}; + mathRotate(delta2, angle - rotate); + SCALE(stroke, delta2); + delta += delta2 + stroke.center; + + _borderLineTo(border, delta, false); + + } else if (stroke.cap == StrokeCap::Round) { + + stroke.angleIn = angle; + stroke.angleOut = angle + SW_ANGLE_PI; + _arcTo(stroke, side); + return; + + } else { //Butt + auto rotate = SIDE_TO_ROTATE(side); + auto border = stroke.borders + side; + + SwPoint delta = {static_cast(stroke.width), 0}; + mathRotate(delta, angle + rotate); + SCALE(stroke, delta); + delta += stroke.center; + + _borderLineTo(border, delta, false); + + delta = {static_cast(stroke.width), 0}; + mathRotate(delta, angle - rotate); + SCALE(stroke, delta); + delta += stroke.center; + + _borderLineTo(border, delta, false); + } +} + + +static void _addReverseLeft(SwStroke& stroke, bool opened) +{ + auto right = stroke.borders + 0; + auto left = stroke.borders + 1; + auto newPts = left->ptsCnt - left->start; + + if (newPts <= 0) return; + + _growBorder(right, newPts); + + auto dstPt = right->pts + right->ptsCnt; + auto dstTag = right->tags + right->ptsCnt; + auto srcPt = left->pts + left->ptsCnt - 1; + auto srcTag = left->tags + left->ptsCnt - 1; + + while (srcPt >= left->pts + left->start) { + *dstPt = *srcPt; + *dstTag = *srcTag; + + if (opened) { + dstTag[0] &= ~(SW_STROKE_TAG_BEGIN | SW_STROKE_TAG_END); + } else { + //switch begin/end tags if necessary + auto ttag = dstTag[0] & (SW_STROKE_TAG_BEGIN | SW_STROKE_TAG_END); + if (ttag == SW_STROKE_TAG_BEGIN || ttag == SW_STROKE_TAG_END) + dstTag[0] ^= (SW_STROKE_TAG_BEGIN | SW_STROKE_TAG_END); + } + --srcPt; + --srcTag; + ++dstPt; + ++dstTag; + } + + left->ptsCnt = left->start; + right->ptsCnt += newPts; + right->movable = false; + left->movable = false; +} + + +static void _beginSubPath(SwStroke& stroke, const SwPoint& to, bool closed) +{ + /* We cannot process the first point because there is not enough + information regarding its corner/cap. Later, it will be processed + in the _endSubPath() */ + + stroke.firstPt = true; + stroke.center = to; + stroke.closedSubPath = closed; + + /* Determine if we need to check whether the border radius is greater + than the radius of curvature of a curve, to handle this case specially. + This is only required if bevel joins or butt caps may be created because + round & miter joins and round & square caps cover the negative sector + created with wide strokes. */ + if ((stroke.join != StrokeJoin::Round) || (!stroke.closedSubPath && stroke.cap == StrokeCap::Butt)) + stroke.handleWideStrokes = true; + else + stroke.handleWideStrokes = false; + + stroke.ptStartSubPath = to; + stroke.angleIn = 0; +} + + +static void _endSubPath(SwStroke& stroke) +{ + if (stroke.closedSubPath) { + //close the path if needed + if (stroke.center != stroke.ptStartSubPath) + _lineTo(stroke, stroke.ptStartSubPath); + + //process the corner + stroke.angleOut = stroke.subPathAngle; + auto turn = mathDiff(stroke.angleIn, stroke.angleOut); + + //No specific corner processing is required if the turn is 0 + if (turn != 0) { + //when we turn to the right, the inside is 0 + int32_t inside = 0; + + //otherwise, the inside is 1 + if (turn < 0) inside = 1; + + _inside(stroke, inside, stroke.subPathLineLength); //inside + _outside(stroke, 1 - inside, stroke.subPathLineLength); //outside + } + + _borderClose(stroke.borders + 0, false); + _borderClose(stroke.borders + 1, true); + } else { + auto right = stroke.borders; + + /* all right, this is an opened path, we need to add a cap between + right & left, add the reverse of left, then add a final cap + between left & right */ + _addCap(stroke, stroke.angleIn, 0); + + //add reversed points from 'left' to 'right' + _addReverseLeft(stroke, true); + + //now add the final cap + stroke.center = stroke.ptStartSubPath; + _addCap(stroke, stroke.subPathAngle + SW_ANGLE_PI, 0); + + /* now end the right subpath accordingly. The left one is rewind + and doesn't need further processing */ + _borderClose(right, false); + } +} + + +static void _getCounts(SwStrokeBorder* border, uint32_t& ptsCnt, uint32_t& cntrsCnt) +{ + auto count = border->ptsCnt; + auto tags = border->tags; + uint32_t _ptsCnt = 0; + uint32_t _cntrsCnt = 0; + bool inCntr = false; + + while (count > 0) { + if (tags[0] & SW_STROKE_TAG_BEGIN) { + if (inCntr) goto fail; + inCntr = true; + } else if (!inCntr) goto fail; + + if (tags[0] & SW_STROKE_TAG_END) { + inCntr = false; + ++_cntrsCnt; + } + --count; + ++_ptsCnt; + ++tags; + } + + if (inCntr) goto fail; + + ptsCnt = _ptsCnt; + cntrsCnt = _cntrsCnt; + + return; + +fail: + ptsCnt = 0; + cntrsCnt = 0; +} + + +static void _exportBorderOutline(const SwStroke& stroke, SwOutline* outline, uint32_t side) +{ + auto border = stroke.borders + side; + if (border->ptsCnt == 0) return; + + memcpy(outline->pts.data + outline->pts.count, border->pts, border->ptsCnt * sizeof(SwPoint)); + + auto cnt = border->ptsCnt; + auto src = border->tags; + auto tags = outline->types.data + outline->types.count; + auto idx = outline->pts.count; + + while (cnt > 0) { + if (*src & SW_STROKE_TAG_POINT) *tags = SW_CURVE_TYPE_POINT; + else if (*src & SW_STROKE_TAG_CUBIC) *tags = SW_CURVE_TYPE_CUBIC; + else TVGERR("SW_ENGINE", "Invalid stroke tag was given! = %d", *src); + if (*src & SW_STROKE_TAG_END) outline->cntrs.push(idx); + ++src; + ++tags; + ++idx; + --cnt; + } + outline->pts.count += border->ptsCnt; + outline->types.count += border->ptsCnt; +} + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +void strokeFree(SwStroke* stroke) +{ + if (!stroke) return; + + //free borders + if (stroke->borders[0].pts) free(stroke->borders[0].pts); + if (stroke->borders[0].tags) free(stroke->borders[0].tags); + if (stroke->borders[1].pts) free(stroke->borders[1].pts); + if (stroke->borders[1].tags) free(stroke->borders[1].tags); + + fillFree(stroke->fill); + stroke->fill = nullptr; + + free(stroke); +} + + +void strokeReset(SwStroke* stroke, const RenderShape* rshape, const Matrix* transform) +{ + if (transform) { + stroke->sx = sqrtf(powf(transform->e11, 2.0f) + powf(transform->e21, 2.0f)); + stroke->sy = sqrtf(powf(transform->e12, 2.0f) + powf(transform->e22, 2.0f)); + } else { + stroke->sx = stroke->sy = 1.0f; + } + + stroke->width = HALF_STROKE(rshape->strokeWidth()); + stroke->cap = rshape->strokeCap(); + stroke->miterlimit = static_cast(rshape->strokeMiterlimit()) << 16; + + //Save line join: it can be temporarily changed when stroking curves... + stroke->joinSaved = stroke->join = rshape->strokeJoin(); + + stroke->borders[0].ptsCnt = 0; + stroke->borders[0].start = -1; + stroke->borders[1].ptsCnt = 0; + stroke->borders[1].start = -1; +} + + +bool strokeParseOutline(SwStroke* stroke, const SwOutline& outline) +{ + uint32_t first = 0; + uint32_t i = 0; + + for (auto cntr = outline.cntrs.begin(); cntr < outline.cntrs.end(); ++cntr, ++i) { + auto last = *cntr; //index of last point in contour + auto limit = outline.pts.data + last; + + //Skip empty points + if (last <= first) { + first = last + 1; + continue; + } + + auto start = outline.pts[first]; + auto pt = outline.pts.data + first; + auto types = outline.types.data + first; + auto type = types[0]; + + //A contour cannot start with a cubic control point + if (type == SW_CURVE_TYPE_CUBIC) return false; + + auto closed = outline.closed.data ? outline.closed.data[i]: false; + + _beginSubPath(*stroke, start, closed); + + while (pt < limit) { + ++pt; + ++types; + + //emit a single line_to + if (types[0] == SW_CURVE_TYPE_POINT) { + _lineTo(*stroke, *pt); + //types cubic + } else { + if (pt + 1 > limit || types[1] != SW_CURVE_TYPE_CUBIC) return false; + + pt += 2; + types += 2; + + if (pt <= limit) { + _cubicTo(*stroke, pt[-2], pt[-1], pt[0]); + continue; + } + _cubicTo(*stroke, pt[-2], pt[-1], start); + goto close; + } + } + close: + if (!stroke->firstPt) _endSubPath(*stroke); + first = last + 1; + } + return true; +} + + +SwOutline* strokeExportOutline(SwStroke* stroke, SwMpool* mpool, unsigned tid) +{ + uint32_t count1, count2, count3, count4; + + _getCounts(stroke->borders + 0, count1, count2); + _getCounts(stroke->borders + 1, count3, count4); + + auto ptsCnt = count1 + count3; + auto cntrsCnt = count2 + count4; + + auto outline = mpoolReqStrokeOutline(mpool, tid); + outline->pts.reserve(ptsCnt); + outline->types.reserve(ptsCnt); + outline->cntrs.reserve(cntrsCnt); + + _exportBorderOutline(*stroke, outline, 0); //left + _exportBorderOutline(*stroke, outline, 1); //right + + return outline; +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgTaskScheduler.cpp b/include/liblvgl/libs/thorvg/tvgTaskScheduler.cpp new file mode 100644 index 00000000..b5c7c2ed --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgTaskScheduler.cpp @@ -0,0 +1,235 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgArray.h" +#include "tvgInlist.h" +#include "tvgTaskScheduler.h" + +#ifdef THORVG_THREAD_SUPPORT + #include + #include +#endif + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +namespace tvg { + +struct TaskSchedulerImpl; +static TaskSchedulerImpl* inst = nullptr; + +#ifdef THORVG_THREAD_SUPPORT + +static thread_local bool _async = true; + +struct TaskQueue { + Inlist taskDeque; + mutex mtx; + condition_variable ready; + bool done = false; + + bool tryPop(Task** task) + { + unique_lock lock{mtx, try_to_lock}; + if (!lock || taskDeque.empty()) return false; + *task = taskDeque.front(); + return true; + } + + bool tryPush(Task* task) + { + { + unique_lock lock{mtx, try_to_lock}; + if (!lock) return false; + taskDeque.back(task); + } + ready.notify_one(); + return true; + } + + void complete() + { + { + lock_guard lock{mtx}; + done = true; + } + ready.notify_all(); + } + + bool pop(Task** task) + { + unique_lock lock{mtx}; + + while (taskDeque.empty() && !done) { + ready.wait(lock); + } + + if (taskDeque.empty()) return false; + + *task = taskDeque.front(); + return true; + } + + void push(Task* task) + { + { + lock_guard lock{mtx}; + taskDeque.back(task); + } + ready.notify_one(); + } +}; + + +struct TaskSchedulerImpl +{ + Array threads; + Array taskQueues; + atomic idx{0}; + + TaskSchedulerImpl(uint32_t threadCnt) + { + threads.reserve(threadCnt); + taskQueues.reserve(threadCnt); + + for (uint32_t i = 0; i < threadCnt; ++i) { + taskQueues.push(new TaskQueue); + threads.push(new thread); + } + for (uint32_t i = 0; i < threadCnt; ++i) { + *threads.data[i] = thread([&, i] { run(i); }); + } + } + + ~TaskSchedulerImpl() + { + for (auto tq = taskQueues.begin(); tq < taskQueues.end(); ++tq) { + (*tq)->complete(); + } + for (auto thread = threads.begin(); thread < threads.end(); ++thread) { + (*thread)->join(); + delete(*thread); + } + for (auto tq = taskQueues.begin(); tq < taskQueues.end(); ++tq) { + delete(*tq); + } + } + + void run(unsigned i) + { + Task* task; + + //Thread Loop + while (true) { + auto success = false; + for (uint32_t x = 0; x < threads.count * 2; ++x) { + if (taskQueues[(i + x) % threads.count]->tryPop(&task)) { + success = true; + break; + } + } + + if (!success && !taskQueues[i]->pop(&task)) break; + (*task)(i + 1); + } + } + + void request(Task* task) + { + //Async + if (threads.count > 0 && _async) { + task->prepare(); + auto i = idx++; + for (uint32_t n = 0; n < threads.count; ++n) { + if (taskQueues[(i + n) % threads.count]->tryPush(task)) return; + } + taskQueues[i % threads.count]->push(task); + //Sync + } else { + task->run(0); + } + } + + uint32_t threadCnt() + { + return threads.count; + } +}; + +#else //THORVG_THREAD_SUPPORT + +static bool _async = true; + +struct TaskSchedulerImpl +{ + TaskSchedulerImpl(TVG_UNUSED uint32_t threadCnt) {} + void request(Task* task) { task->run(0); } + uint32_t threadCnt() { return 0; } +}; + +#endif //THORVG_THREAD_SUPPORT + +} //namespace + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +void TaskScheduler::init(uint32_t threads) +{ + if (inst) return; + inst = new TaskSchedulerImpl(threads); +} + + +void TaskScheduler::term() +{ + delete(inst); + inst = nullptr; +} + + +void TaskScheduler::request(Task* task) +{ + if (inst) inst->request(task); +} + + +uint32_t TaskScheduler::threads() +{ + if (inst) return inst->threadCnt(); + return 0; +} + + +void TaskScheduler::async(bool on) +{ + //toggle async tasking for each thread on/off + _async = on; +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgTaskScheduler.h b/include/liblvgl/libs/thorvg/tvgTaskScheduler.h new file mode 100644 index 00000000..af1334b7 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgTaskScheduler.h @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_TASK_SCHEDULER_H_ +#define _TVG_TASK_SCHEDULER_H_ + +#include +#include + +#include "tvgCommon.h" +#include "tvgInlist.h" + +namespace tvg { + +#ifdef THORVG_THREAD_SUPPORT + +struct Task +{ +private: + mutex mtx; + condition_variable cv; + bool ready = true; + bool pending = false; + +public: + INLIST_ITEM(Task); + + virtual ~Task() = default; + + void done() + { + if (!pending) return; + + unique_lock lock(mtx); + while (!ready) cv.wait(lock); + pending = false; + } + +protected: + virtual void run(unsigned tid) = 0; + +private: + void operator()(unsigned tid) + { + run(tid); + + lock_guard lock(mtx); + ready = true; + cv.notify_one(); + } + + void prepare() + { + ready = false; + pending = true; + } + + friend struct TaskSchedulerImpl; +}; + +#else //THORVG_THREAD_SUPPORT + +struct Task +{ +public: + INLIST_ITEM(Task); + + virtual ~Task() = default; + void done() {} + +protected: + virtual void run(unsigned tid) = 0; + +private: + friend struct TaskSchedulerImpl; +}; + +#endif //THORVG_THREAD_SUPPORT + + +struct TaskScheduler +{ + static uint32_t threads(); + static void init(uint32_t threads); + static void term(); + static void request(Task* task); + static void async(bool on); +}; + +} //namespace + +#endif //_TVG_TASK_SCHEDULER_H_ + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgText.cpp b/include/liblvgl/libs/thorvg/tvgText.cpp new file mode 100644 index 00000000..6cb2b4f7 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgText.cpp @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2023 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + + +#include "tvgText.h" + + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + + +Text::Text() : pImpl(new Impl) +{ + Paint::pImpl->id = TVG_CLASS_ID_TEXT; +} + + +Text::~Text() +{ + delete(pImpl); +} + + +Result Text::text(const char* text) noexcept +{ + return pImpl->text(text); +} + + +Result Text::font(const char* name, float size, const char* style) noexcept +{ + return pImpl->font(name, size, style); +} + + +Result Text::load(const std::string& path) noexcept +{ + bool invalid; //invalid path + if (!LoaderMgr::loader(path, &invalid)) { + if (invalid) return Result::InvalidArguments; + else return Result::NonSupport; + } + + return Result::Success; +} + + +Result Text::unload(const std::string& path) noexcept +{ + if (LoaderMgr::retrieve(path)) return Result::Success; + return Result::InsufficientCondition; +} + + +Result Text::fill(uint8_t r, uint8_t g, uint8_t b) noexcept +{ + if (!pImpl->paint) return Result::InsufficientCondition; + + return pImpl->fill(r, g, b); +} + + +Result Text::fill(unique_ptr f) noexcept +{ + if (!pImpl->paint) return Result::InsufficientCondition; + + auto p = f.release(); + if (!p) return Result::MemoryCorruption; + + return pImpl->fill(p); +} + + +unique_ptr Text::gen() noexcept +{ + return unique_ptr(new Text); +} + + +uint32_t Text::identifier() noexcept +{ + return TVG_CLASS_ID_TEXT; +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgText.h b/include/liblvgl/libs/thorvg/tvgText.h new file mode 100644 index 00000000..89cef187 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgText.h @@ -0,0 +1,189 @@ +/* + * Copyright (c) 2023 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_TEXT_H +#define _TVG_TEXT_H + +#include +#include "tvgShape.h" +#include "tvgFill.h" + +#ifdef THORVG_TTF_LOADER_SUPPORT + #include "tvgTtfLoader.h" +#else + #include "tvgLoader.h" +#endif + +struct Text::Impl +{ + FontLoader* loader = nullptr; + Shape* paint = nullptr; + char* utf8 = nullptr; + float fontSize; + bool italic = false; + bool changed = false; + + ~Impl() + { + free(utf8); + LoaderMgr::retrieve(loader); + delete(paint); + } + + Result fill(uint8_t r, uint8_t g, uint8_t b) + { + return paint->fill(r, g, b); + } + + Result fill(Fill* f) + { + return paint->fill(cast(f)); + } + + Result text(const char* utf8) + { + free(this->utf8); + if (utf8) this->utf8 = strdup(utf8); + else this->utf8 = nullptr; + changed = true; + + return Result::Success; + } + + Result font(const char* name, float size, const char* style) + { + auto loader = LoaderMgr::loader(name); + if (!loader) return Result::InsufficientCondition; + + //Same resource has been loaded. + if (this->loader == loader) { + this->loader->sharing--; //make it sure the reference counting. + return Result::Success; + } else if (this->loader) { + LoaderMgr::retrieve(this->loader); + } + this->loader = static_cast(loader); + + if (!paint) paint = Shape::gen().release(); + + fontSize = size; + if (style && strstr(style, "italic")) italic = true; + changed = true; + return Result::Success; + } + + RenderRegion bounds(RenderMethod* renderer) + { + if (paint) return P(paint)->bounds(renderer); + else return {0, 0, 0, 0}; + } + + bool render(RenderMethod* renderer) + { + if (paint) return PP(paint)->render(renderer); + return false; + } + + bool load() + { + if (!loader) return false; + + //reload + if (changed) { + loader->request(paint, utf8, italic); + loader->read(); + changed = false; + } + if (paint) { + loader->resize(paint, fontSize, fontSize); + return true; + } + return false; + } + + RenderData update(RenderMethod* renderer, const RenderTransform* transform, Array& clips, uint8_t opacity, RenderUpdateFlag pFlag, bool clipper) + { + if (!load()) return nullptr; + + //transform the gradient coordinates based on the final scaled font. + if (P(paint)->flag & RenderUpdateFlag::Gradient) { + auto fill = P(paint)->rs.fill; + auto scale = 1.0f / loader->scale; + if (fill->identifier() == TVG_CLASS_ID_LINEAR) { + P(static_cast(fill))->x1 *= scale; + P(static_cast(fill))->y1 *= scale; + P(static_cast(fill))->x2 *= scale; + P(static_cast(fill))->y2 *= scale; + } else { + P(static_cast(fill))->cx *= scale; + P(static_cast(fill))->cy *= scale; + P(static_cast(fill))->r *= scale; + P(static_cast(fill))->fx *= scale; + P(static_cast(fill))->fy *= scale; + P(static_cast(fill))->fr *= scale; + } + } + return PP(paint)->update(renderer, transform, clips, opacity, pFlag, clipper); + } + + bool bounds(float* x, float* y, float* w, float* h, TVG_UNUSED bool stroking) + { + if (!load() || !paint) return false; + paint->bounds(x, y, w, h, true); + return true; + } + + Paint* duplicate() + { + load(); + + auto ret = Text::gen().release(); + auto dup = ret->pImpl; + if (paint) dup->paint = static_cast(paint->duplicate()); + + if (loader) { + dup->loader = loader; + ++dup->loader->sharing; + } + + dup->utf8 = strdup(utf8); + dup->italic = italic; + dup->fontSize = fontSize; + + return ret; + } + + Iterator* iterator() + { + return nullptr; + } +}; + + + +#endif //_TVG_TEXT_H + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgWgCanvas.cpp b/include/liblvgl/libs/thorvg/tvgWgCanvas.cpp new file mode 100644 index 00000000..3bdaec9e --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgWgCanvas.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2023 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include "tvgCanvas.h" + +#ifdef THORVG_WG_RASTER_SUPPORT + #include "tvgWgRenderer.h" +#endif + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +struct WgCanvas::Impl +{ +}; + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +#ifdef THORVG_WG_RASTER_SUPPORT +WgCanvas::WgCanvas() : Canvas(WgRenderer::gen()), pImpl(new Impl) +#else +WgCanvas::WgCanvas() : Canvas(nullptr), pImpl(nullptr) +#endif +{ +} + +WgCanvas::~WgCanvas() +{ + delete pImpl; +} + +Result WgCanvas::target(void* window, uint32_t w, uint32_t h) noexcept +{ +#ifdef THORVG_WG_RASTER_SUPPORT + if (!window) return Result::InvalidArguments; + if ((w == 0) || (h == 0)) return Result::InvalidArguments; + + //We know renderer type, avoid dynamic_cast for performance. + auto renderer = static_cast(Canvas::pImpl->renderer); + if (!renderer) return Result::MemoryCorruption; + + if (!renderer->target(window, w, h)) return Result::Unknown; + Canvas::pImpl->vport = {0, 0, (int32_t)w, (int32_t)h}; + renderer->viewport(Canvas::pImpl->vport); + + //Paints must be updated again with this new target. + Canvas::pImpl->needRefresh(); + + return Result::Success; +#endif + return Result::NonSupport; +} + +unique_ptr WgCanvas::gen() noexcept +{ +#ifdef THORVG_WG_RASTER_SUPPORT + return unique_ptr(new WgCanvas); +#endif + return nullptr; +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgXmlParser.cpp b/include/liblvgl/libs/thorvg/tvgXmlParser.cpp new file mode 100644 index 00000000..ffb9b5df --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgXmlParser.cpp @@ -0,0 +1,595 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#include +#include +#include + +#ifdef _WIN32 + #include +#elif defined(__linux__) + #include +#else + #include +#endif + +#include "tvgXmlParser.h" +#include "tvgStr.h" + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +bool _isIgnoreUnsupportedLogAttributes(TVG_UNUSED const char* tagAttribute, TVG_UNUSED const char* tagValue) +{ +#ifdef THORVG_LOG_ENABLED + const auto attributesNum = 6; + const struct + { + const char* tag; + bool tagWildcard; //If true, it is assumed that a wildcard is used after the tag. (ex: tagName*) + const char* value; + } attributes[] = { + {"id", false, nullptr}, + {"data-name", false, nullptr}, + {"overflow", false, "visible"}, + {"version", false, nullptr}, + {"xmlns", true, nullptr}, + {"xml:space", false, nullptr}, + }; + + for (unsigned int i = 0; i < attributesNum; ++i) { + if (!strncmp(tagAttribute, attributes[i].tag, attributes[i].tagWildcard ? strlen(attributes[i].tag) : strlen(tagAttribute))) { + if (attributes[i].value && tagValue) { + if (!strncmp(tagValue, attributes[i].value, strlen(tagValue))) { + return true; + } else continue; + } + return true; + } + } + return false; +#endif + return true; +} + + +static const char* _simpleXmlFindWhiteSpace(const char* itr, const char* itrEnd) +{ + for (; itr < itrEnd; itr++) { + if (isspace((unsigned char)*itr)) break; + } + return itr; +} + + +static const char* _simpleXmlSkipWhiteSpace(const char* itr, const char* itrEnd) +{ + for (; itr < itrEnd; itr++) { + if (!isspace((unsigned char)*itr)) break; + } + return itr; +} + + +static const char* _simpleXmlUnskipWhiteSpace(const char* itr, const char* itrStart) +{ + for (itr--; itr > itrStart; itr--) { + if (!isspace((unsigned char)*itr)) break; + } + return itr + 1; +} + + +static const char* _simpleXmlSkipXmlEntities(const char* itr, const char* itrEnd) +{ + auto p = itr; + while (itr < itrEnd && *itr == '&') { + for (int i = 0; i < NUMBER_OF_XML_ENTITIES; ++i) { + if (strncmp(itr, xmlEntity[i], xmlEntityLength[i]) == 0) { + itr += xmlEntityLength[i]; + break; + } + } + if (itr == p) break; + p = itr; + } + return itr; +} + + +static const char* _simpleXmlUnskipXmlEntities(const char* itr, const char* itrStart) +{ + auto p = itr; + while (itr > itrStart && *(itr - 1) == ';') { + for (int i = 0; i < NUMBER_OF_XML_ENTITIES; ++i) { + if (itr - xmlEntityLength[i] > itrStart && + strncmp(itr - xmlEntityLength[i], xmlEntity[i], xmlEntityLength[i]) == 0) { + itr -= xmlEntityLength[i]; + break; + } + } + if (itr == p) break; + p = itr; + } + return itr; +} + + +static const char* _skipWhiteSpacesAndXmlEntities(const char* itr, const char* itrEnd) +{ + itr = _simpleXmlSkipWhiteSpace(itr, itrEnd); + auto p = itr; + while (true) { + if (p != (itr = _simpleXmlSkipXmlEntities(itr, itrEnd))) p = itr; + else break; + if (p != (itr = _simpleXmlSkipWhiteSpace(itr, itrEnd))) p = itr; + else break; + } + return itr; +} + + +static const char* _unskipWhiteSpacesAndXmlEntities(const char* itr, const char* itrStart) +{ + itr = _simpleXmlUnskipWhiteSpace(itr, itrStart); + auto p = itr; + while (true) { + if (p != (itr = _simpleXmlUnskipXmlEntities(itr, itrStart))) p = itr; + else break; + if (p != (itr = _simpleXmlUnskipWhiteSpace(itr, itrStart))) p = itr; + else break; + } + return itr; +} + + +static const char* _simpleXmlFindStartTag(const char* itr, const char* itrEnd) +{ + return (const char*)memchr(itr, '<', itrEnd - itr); +} + + +static const char* _simpleXmlFindEndTag(const char* itr, const char* itrEnd) +{ + bool insideQuote[2] = {false, false}; // 0: ", 1: ' + for (; itr < itrEnd; itr++) { + if (*itr == '"' && !insideQuote[1]) insideQuote[0] = !insideQuote[0]; + if (*itr == '\'' && !insideQuote[0]) insideQuote[1] = !insideQuote[1]; + if (!insideQuote[0] && !insideQuote[1]) { + if ((*itr == '>') || (*itr == '<')) + return itr; + } + } + return nullptr; +} + + +static const char* _simpleXmlFindEndCommentTag(const char* itr, const char* itrEnd) +{ + for (; itr < itrEnd; itr++) { + if ((*itr == '-') && ((itr + 1 < itrEnd) && (*(itr + 1) == '-')) && ((itr + 2 < itrEnd) && (*(itr + 2) == '>'))) return itr + 2; + } + return nullptr; +} + + +static const char* _simpleXmlFindEndCdataTag(const char* itr, const char* itrEnd) +{ + for (; itr < itrEnd; itr++) { + if ((*itr == ']') && ((itr + 1 < itrEnd) && (*(itr + 1) == ']')) && ((itr + 2 < itrEnd) && (*(itr + 2) == '>'))) return itr + 2; + } + return nullptr; +} + + +static const char* _simpleXmlFindDoctypeChildEndTag(const char* itr, const char* itrEnd) +{ + for (; itr < itrEnd; itr++) { + if (*itr == '>') return itr; + } + return nullptr; +} + + +static SimpleXMLType _getXMLType(const char* itr, const char* itrEnd, size_t &toff) +{ + toff = 0; + if (itr[1] == '/') { + toff = 1; + return SimpleXMLType::Close; + } else if (itr[1] == '?') { + toff = 1; + return SimpleXMLType::Processing; + } else if (itr[1] == '!') { + if ((itr + sizeof("") - 1 < itrEnd) && (!memcmp(itr + 2, "DOCTYPE", sizeof("DOCTYPE") - 1)) && ((itr[2 + sizeof("DOCTYPE") - 1] == '>') || (isspace((unsigned char)itr[2 + sizeof("DOCTYPE") - 1])))) { + toff = sizeof("!DOCTYPE") - 1; + return SimpleXMLType::Doctype; + } else if ((itr + sizeof("") - 1 < itrEnd) && (!memcmp(itr + 2, "[CDATA[", sizeof("[CDATA[") - 1))) { + toff = sizeof("![CDATA[") - 1; + return SimpleXMLType::CData; + } else if ((itr + sizeof("") - 1 < itrEnd) && (!memcmp(itr + 2, "--", sizeof("--") - 1))) { + toff = sizeof("!--") - 1; + return SimpleXMLType::Comment; + } else if (itr + sizeof("") - 1 < itrEnd) { + toff = sizeof("!") - 1; + return SimpleXMLType::DoctypeChild; + } + return SimpleXMLType::Open; + } + return SimpleXMLType::Open; +} + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +const char* simpleXmlNodeTypeToString(TVG_UNUSED SvgNodeType type) +{ +#ifdef THORVG_LOG_ENABLED + static const char* TYPE_NAMES[] = { + "Svg", + "G", + "Defs", + "Animation", + "Arc", + "Circle", + "Ellipse", + "Image", + "Line", + "Path", + "Polygon", + "Polyline", + "Rect", + "Text", + "TextArea", + "Tspan", + "Use", + "Video", + "ClipPath", + "Mask", + "Symbol", + "Unknown", + }; + return TYPE_NAMES[(int) type]; +#endif + return nullptr; +} + + +bool isIgnoreUnsupportedLogElements(TVG_UNUSED const char* tagName) +{ +#ifdef THORVG_LOG_ENABLED + const auto elementsNum = 1; + const char* const elements[] = { "title" }; + + for (unsigned int i = 0; i < elementsNum; ++i) { + if (!strncmp(tagName, elements[i], strlen(tagName))) { + return true; + } + } + return false; +#else + return true; +#endif +} + + +bool simpleXmlParseAttributes(const char* buf, unsigned bufLength, simpleXMLAttributeCb func, const void* data) +{ + const char *itr = buf, *itrEnd = buf + bufLength; + char* tmpBuf = (char*)malloc(bufLength + 1); + + if (!buf || !func || !tmpBuf) goto error; + + while (itr < itrEnd) { + const char* p = _skipWhiteSpacesAndXmlEntities(itr, itrEnd); + const char *key, *keyEnd, *value, *valueEnd; + char* tval; + + if (p == itrEnd) goto success; + + key = p; + for (keyEnd = key; keyEnd < itrEnd; keyEnd++) { + if ((*keyEnd == '=') || (isspace((unsigned char)*keyEnd))) break; + } + if (keyEnd == itrEnd) goto error; + if (keyEnd == key) { // There is no key. This case is invalid, but explores the following syntax. + itr = keyEnd + 1; + continue; + } + + if (*keyEnd == '=') value = keyEnd + 1; + else { + value = (const char*)memchr(keyEnd, '=', itrEnd - keyEnd); + if (!value) goto error; + value++; + } + keyEnd = _simpleXmlUnskipXmlEntities(keyEnd, key); + + value = _skipWhiteSpacesAndXmlEntities(value, itrEnd); + if (value == itrEnd) goto error; + + if ((*value == '"') || (*value == '\'')) { + valueEnd = (const char*)memchr(value + 1, *value, itrEnd - value); + if (!valueEnd) goto error; + value++; + } else { + valueEnd = _simpleXmlFindWhiteSpace(value, itrEnd); + } + + itr = valueEnd + 1; + + value = _skipWhiteSpacesAndXmlEntities(value, itrEnd); + valueEnd = _unskipWhiteSpacesAndXmlEntities(valueEnd, value); + + memcpy(tmpBuf, key, keyEnd - key); + tmpBuf[keyEnd - key] = '\0'; + + tval = tmpBuf + (keyEnd - key) + 1; + int i = 0; + while (value < valueEnd) { + value = _simpleXmlSkipXmlEntities(value, valueEnd); + tval[i++] = *value; + value++; + } + tval[i] = '\0'; + + if (!func((void*)data, tmpBuf, tval)) { + if (!_isIgnoreUnsupportedLogAttributes(tmpBuf, tval)) { + TVGLOG("SVG", "Unsupported attributes used [Elements type: %s][Id : %s][Attribute: %s][Value: %s]", simpleXmlNodeTypeToString(((SvgLoaderData*)data)->svgParse->node->type), ((SvgLoaderData*)data)->svgParse->node->id ? ((SvgLoaderData*)data)->svgParse->node->id : "NO_ID", tmpBuf, tval ? tval : "NONE"); + } + } + } + +success: + free(tmpBuf); + return true; + +error: + free(tmpBuf); + return false; +} + + +bool simpleXmlParse(const char* buf, unsigned bufLength, bool strip, simpleXMLCb func, const void* data) +{ + const char *itr = buf, *itrEnd = buf + bufLength; + + if (!buf || !func) return false; + + while (itr < itrEnd) { + if (itr[0] == '<') { + //Invalid case + if (itr + 1 >= itrEnd) return false; + + size_t toff = 0; + SimpleXMLType type = _getXMLType(itr, itrEnd, toff); + + const char* p; + if (type == SimpleXMLType::CData) p = _simpleXmlFindEndCdataTag(itr + 1 + toff, itrEnd); + else if (type == SimpleXMLType::DoctypeChild) p = _simpleXmlFindDoctypeChildEndTag(itr + 1 + toff, itrEnd); + else if (type == SimpleXMLType::Comment) p = _simpleXmlFindEndCommentTag(itr + 1 + toff, itrEnd); + else p = _simpleXmlFindEndTag(itr + 1 + toff, itrEnd); + + if (p) { + //Invalid case: '<' nested + if (*p == '<' && type != SimpleXMLType::Doctype) return false; + const char *start, *end; + + start = itr + 1 + toff; + end = p; + + switch (type) { + case SimpleXMLType::Open: { + if (p[-1] == '/') { + type = SimpleXMLType::OpenEmpty; + end--; + } + break; + } + case SimpleXMLType::CData: { + if (!memcmp(p - 2, "]]", 2)) end -= 2; + break; + } + case SimpleXMLType::Processing: { + if (p[-1] == '?') end--; + break; + } + case SimpleXMLType::Comment: { + if (!memcmp(p - 2, "--", 2)) end -= 2; + break; + } + default: { + break; + } + } + + if (strip && (type != SimpleXMLType::CData)) { + start = _skipWhiteSpacesAndXmlEntities(start, end); + end = _unskipWhiteSpacesAndXmlEntities(end, start); + } + + if (!func((void*)data, type, start, (unsigned int)(end - start))) return false; + + itr = p + 1; + } else { + return false; + } + } else { + const char *p, *end; + + if (strip) { + p = itr; + p = _skipWhiteSpacesAndXmlEntities(p, itrEnd); + if (p) { + if (!func((void*)data, SimpleXMLType::Ignored, itr, (unsigned int)(p - itr))) return false; + itr = p; + } + } + + p = _simpleXmlFindStartTag(itr, itrEnd); + if (!p) p = itrEnd; + + end = p; + if (strip) end = _unskipWhiteSpacesAndXmlEntities(end, itr); + + if (itr != end && !func((void*)data, SimpleXMLType::Data, itr, (unsigned int)(end - itr))) return false; + + if (strip && (end < p) && !func((void*)data, SimpleXMLType::Ignored, end, (unsigned int)(p - end))) return false; + + itr = p; + } + } + return true; +} + + +bool simpleXmlParseW3CAttribute(const char* buf, unsigned bufLength, simpleXMLAttributeCb func, const void* data) +{ + const char* end; + char* key; + char* val; + char* next; + + if (!buf) return false; + + end = buf + bufLength; + key = (char*)alloca(end - buf + 1); + val = (char*)alloca(end - buf + 1); + + if (buf == end) return true; + + do { + char* sep = (char*)strchr(buf, ':'); + next = (char*)strchr(buf, ';'); + if (sep >= end) { + next = nullptr; + sep = nullptr; + } + if (next >= end) next = nullptr; + + key[0] = '\0'; + val[0] = '\0'; + + if (next == nullptr && sep != nullptr) { + memcpy(key, buf, sep - buf); + key[sep - buf] = '\0'; + + memcpy(val, sep + 1, end - sep - 1); + val[end - sep - 1] = '\0'; + } else if (sep < next && sep != nullptr) { + memcpy(key, buf, sep - buf); + key[sep - buf] = '\0'; + + memcpy(val, sep + 1, next - sep - 1); + val[next - sep - 1] = '\0'; + } else if (next) { + memcpy(key, buf, next - buf); + key[next - buf] = '\0'; + } + + if (key[0]) { + key = const_cast(_simpleXmlSkipWhiteSpace(key, key + strlen(key))); + key[_simpleXmlUnskipWhiteSpace(key + strlen(key) , key) - key] = '\0'; + val = const_cast(_simpleXmlSkipWhiteSpace(val, val + strlen(val))); + val[_simpleXmlUnskipWhiteSpace(val + strlen(val) , val) - val] = '\0'; + + if (!func((void*)data, key, val)) { + if (!_isIgnoreUnsupportedLogAttributes(key, val)) { + TVGLOG("SVG", "Unsupported attributes used [Elements type: %s][Id : %s][Attribute: %s][Value: %s]", simpleXmlNodeTypeToString(((SvgLoaderData*)data)->svgParse->node->type), ((SvgLoaderData*)data)->svgParse->node->id ? ((SvgLoaderData*)data)->svgParse->node->id : "NO_ID", key, val ? val : "NONE"); + } + } + } + + buf = next + 1; + } while (next != nullptr); + + return true; +} + + +/* + * Supported formats: + * tag {}, .name {}, tag.name{} + */ +const char* simpleXmlParseCSSAttribute(const char* buf, unsigned bufLength, char** tag, char** name, const char** attrs, unsigned* attrsLength) +{ + if (!buf) return nullptr; + + *tag = *name = nullptr; + *attrsLength = 0; + + auto itr = _simpleXmlSkipWhiteSpace(buf, buf + bufLength); + auto itrEnd = (const char*)memchr(buf, '{', bufLength); + + if (!itrEnd || itr == itrEnd) return nullptr; + + auto nextElement = (const char*)memchr(itrEnd, '}', bufLength - (itrEnd - buf)); + if (!nextElement) return nullptr; + + *attrs = itrEnd + 1; + *attrsLength = nextElement - *attrs; + + const char *p; + + itrEnd = _simpleXmlUnskipWhiteSpace(itrEnd, itr); + if (*(itrEnd - 1) == '.') return nullptr; + + for (p = itr; p < itrEnd; p++) { + if (*p == '.') break; + } + + if (p == itr) *tag = strdup("all"); + else *tag = strDuplicate(itr, p - itr); + + if (p == itrEnd) *name = nullptr; + else *name = strDuplicate(p + 1, itrEnd - p - 1); + + return (nextElement ? nextElement + 1 : nullptr); +} + + +const char* simpleXmlFindAttributesTag(const char* buf, unsigned bufLength) +{ + const char *itr = buf, *itrEnd = buf + bufLength; + + for (; itr < itrEnd; itr++) { + if (!isspace((unsigned char)*itr)) { + //User skip tagname and already gave it the attributes. + if (*itr == '=') return buf; + } else { + itr = _simpleXmlUnskipXmlEntities(itr, buf); + if (itr == itrEnd) return nullptr; + return itr; + } + } + + return nullptr; +} + +#endif /* LV_USE_THORVG_INTERNAL */ + diff --git a/include/liblvgl/libs/thorvg/tvgXmlParser.h b/include/liblvgl/libs/thorvg/tvgXmlParser.h new file mode 100644 index 00000000..be080686 --- /dev/null +++ b/include/liblvgl/libs/thorvg/tvgXmlParser.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "../../lv_conf_internal.h" +#if LV_USE_THORVG_INTERNAL + +#ifndef _TVG_SIMPLE_XML_PARSER_H_ +#define _TVG_SIMPLE_XML_PARSER_H_ + +#include "tvgSvgLoaderCommon.h" + +#define NUMBER_OF_XML_ENTITIES 9 +const char* const xmlEntity[] = {" ", """, " ", "'", "&", "<", ">", "#", "'"}; +const int xmlEntityLength[] = {5, 6, 6, 6, 5, 4, 4, 6, 6}; + +enum class SimpleXMLType +{ + Open = 0, //!< \ + OpenEmpty, //!< \ + Close, //!< \ + Data, //!< tag text data + CData, //!< \ + Error, //!< error contents + Processing, //!< \ \ + Doctype, //!< \ + Ignored, //!< whatever is ignored by parser, like whitespace + DoctypeChild //!< \= 'width' +// or 2. call stbrp_allow_out_of_mem() defined below with 'allow_out_of_mem = 1' +// +// If you don't do either of the above things, widths will be quantized to multiples +// of small integers to guarantee the algorithm doesn't run out of temporary storage. +// +// If you do #2, then the non-quantized algorithm will be used, but the algorithm +// may run out of temporary storage and be unable to pack some rectangles. + +STBRP_DEF void stbrp_setup_allow_out_of_mem(stbrp_context * context, int allow_out_of_mem); +// Optionally call this function after init but before doing any packing to +// change the handling of the out-of-temp-memory scenario, described above. +// If you call init again, this will be reset to the default (false). + +STBRP_DEF void stbrp_setup_heuristic(stbrp_context * context, int heuristic); +// Optionally select which packing heuristic the library should use. Different +// heuristics will produce better/worse results for different data sets. +// If you call init again, this will be reset to the default. + +enum { + STBRP_HEURISTIC_Skyline_default = 0, + STBRP_HEURISTIC_Skyline_BL_sortHeight = STBRP_HEURISTIC_Skyline_default, + STBRP_HEURISTIC_Skyline_BF_sortHeight +}; + +////////////////////////////////////////////////////////////////////////////// +// +// the details of the following structures don't matter to you, but they must +// be visible so you can handle the memory allocations for them + +struct stbrp_node { + stbrp_coord x, y; + stbrp_node * next; +}; + +struct stbrp_context { + int width; + int height; + int align; + int init_mode; + int heuristic; + int num_nodes; + stbrp_node * active_head; + stbrp_node * free_head; + stbrp_node extra[2]; // we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2' +}; + +#ifdef __cplusplus +} +#endif + +#endif + +////////////////////////////////////////////////////////////////////////////// +// +// IMPLEMENTATION SECTION +// + +#ifdef STB_RECT_PACK_IMPLEMENTATION +#ifndef STBRP_SORT + #include + #define STBRP_SORT qsort +#endif + +#ifndef STBRP_ASSERT + #include + #define STBRP_ASSERT assert +#endif + +#ifdef _MSC_VER + #define STBRP__NOTUSED(v) (void)(v) + #define STBRP__CDECL __cdecl +#else + #define STBRP__NOTUSED(v) (void)sizeof(v) + #define STBRP__CDECL +#endif + +enum { + STBRP__INIT_skyline = 1 +}; + +STBRP_DEF void stbrp_setup_heuristic(stbrp_context * context, int heuristic) +{ + switch(context->init_mode) { + case STBRP__INIT_skyline: + STBRP_ASSERT(heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight || heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight); + context->heuristic = heuristic; + break; + default: + STBRP_ASSERT(0); + } +} + +STBRP_DEF void stbrp_setup_allow_out_of_mem(stbrp_context * context, int allow_out_of_mem) +{ + if(allow_out_of_mem) + // if it's ok to run out of memory, then don't bother aligning them; + // this gives better packing, but may fail due to OOM (even though + // the rectangles easily fit). @TODO a smarter approach would be to only + // quantize once we've hit OOM, then we could get rid of this parameter. + context->align = 1; + else { + // if it's not ok to run out of memory, then quantize the widths + // so that num_nodes is always enough nodes. + // + // I.e. num_nodes * align >= width + // align >= width / num_nodes + // align = ceil(width/num_nodes) + + context->align = (context->width + context->num_nodes - 1) / context->num_nodes; + } +} + +STBRP_DEF void stbrp_init_target(stbrp_context * context, int width, int height, stbrp_node * nodes, int num_nodes) +{ + int i; + + for(i = 0; i < num_nodes - 1; ++i) + nodes[i].next = &nodes[i + 1]; + nodes[i].next = NULL; + context->init_mode = STBRP__INIT_skyline; + context->heuristic = STBRP_HEURISTIC_Skyline_default; + context->free_head = &nodes[0]; + context->active_head = &context->extra[0]; + context->width = width; + context->height = height; + context->num_nodes = num_nodes; + stbrp_setup_allow_out_of_mem(context, 0); + + // node 0 is the full width, node 1 is the sentinel (lets us not store width explicitly) + context->extra[0].x = 0; + context->extra[0].y = 0; + context->extra[0].next = &context->extra[1]; + context->extra[1].x = (stbrp_coord) width; + context->extra[1].y = (1 << 30); + context->extra[1].next = NULL; +} + +// find minimum y position if it starts at x1 +static int stbrp__skyline_find_min_y(stbrp_context * c, stbrp_node * first, int x0, int width, int * pwaste) +{ + stbrp_node * node = first; + int x1 = x0 + width; + int min_y, visited_width, waste_area; + + STBRP__NOTUSED(c); + + STBRP_ASSERT(first->x <= x0); + +#if 0 + // skip in case we're past the node + while(node->next->x <= x0) + ++node; +#else + STBRP_ASSERT(node->next->x > x0); // we ended up handling this in the caller for efficiency +#endif + + STBRP_ASSERT(node->x <= x0); + + min_y = 0; + waste_area = 0; + visited_width = 0; + while(node->x < x1) { + if(node->y > min_y) { + // raise min_y higher. + // we've accounted for all waste up to min_y, + // but we'll now add more waste for everything we've visited + waste_area += visited_width * (node->y - min_y); + min_y = node->y; + // the first time through, visited_width might be reduced + if(node->x < x0) + visited_width += node->next->x - x0; + else + visited_width += node->next->x - node->x; + } + else { + // add waste area + int under_width = node->next->x - node->x; + if(under_width + visited_width > width) + under_width = width - visited_width; + waste_area += under_width * (min_y - node->y); + visited_width += under_width; + } + node = node->next; + } + + *pwaste = waste_area; + return min_y; +} + +typedef struct { + int x, y; + stbrp_node ** prev_link; +} stbrp__findresult; + +static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context * c, int width, int height) +{ + int best_waste = (1 << 30), best_x, best_y = (1 << 30); + stbrp__findresult fr; + stbrp_node ** prev, * node, * tail, ** best = NULL; + + // align to multiple of c->align + width = (width + c->align - 1); + width -= width % c->align; + STBRP_ASSERT(width % c->align == 0); + + // if it can't possibly fit, bail immediately + if(width > c->width || height > c->height) { + fr.prev_link = NULL; + fr.x = fr.y = 0; + return fr; + } + + node = c->active_head; + prev = &c->active_head; + while(node->x + width <= c->width) { + int y, waste; + y = stbrp__skyline_find_min_y(c, node, node->x, width, &waste); + if(c->heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight) { // actually just want to test BL + // bottom left + if(y < best_y) { + best_y = y; + best = prev; + } + } + else { + // best-fit + if(y + height <= c->height) { + // can only use it if it first vertically + if(y < best_y || (y == best_y && waste < best_waste)) { + best_y = y; + best_waste = waste; + best = prev; + } + } + } + prev = &node->next; + node = node->next; + } + + best_x = (best == NULL) ? 0 : (*best)->x; + + // if doing best-fit (BF), we also have to try aligning right edge to each node position + // + // e.g, if fitting + // + // ____________________ + // |____________________| + // + // into + // + // | | + // | ____________| + // |____________| + // + // then right-aligned reduces waste, but bottom-left BL is always chooses left-aligned + // + // This makes BF take about 2x the time + + if(c->heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight) { + tail = c->active_head; + node = c->active_head; + prev = &c->active_head; + // find first node that's admissible + while(tail->x < width) + tail = tail->next; + while(tail) { + int xpos = tail->x - width; + int y, waste; + STBRP_ASSERT(xpos >= 0); + // find the left position that matches this + while(node->next->x <= xpos) { + prev = &node->next; + node = node->next; + } + STBRP_ASSERT(node->next->x > xpos && node->x <= xpos); + y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste); + if(y + height <= c->height) { + if(y <= best_y) { + if(y < best_y || waste < best_waste || (waste == best_waste && xpos < best_x)) { + best_x = xpos; + STBRP_ASSERT(y <= best_y); + best_y = y; + best_waste = waste; + best = prev; + } + } + } + tail = tail->next; + } + } + + fr.prev_link = best; + fr.x = best_x; + fr.y = best_y; + return fr; +} + +static stbrp__findresult stbrp__skyline_pack_rectangle(stbrp_context * context, int width, int height) +{ + // find best position according to heuristic + stbrp__findresult res = stbrp__skyline_find_best_pos(context, width, height); + stbrp_node * node, * cur; + + // bail if: + // 1. it failed + // 2. the best node doesn't fit (we don't always check this) + // 3. we're out of memory + if(res.prev_link == NULL || res.y + height > context->height || context->free_head == NULL) { + res.prev_link = NULL; + return res; + } + + // on success, create new node + node = context->free_head; + node->x = (stbrp_coord) res.x; + node->y = (stbrp_coord)(res.y + height); + + context->free_head = node->next; + + // insert the new node into the right starting point, and + // let 'cur' point to the remaining nodes needing to be + // stitched back in + + cur = *res.prev_link; + if(cur->x < res.x) { + // preserve the existing one, so start testing with the next one + stbrp_node * next = cur->next; + cur->next = node; + cur = next; + } + else { + *res.prev_link = node; + } + + // from here, traverse cur and free the nodes, until we get to one + // that shouldn't be freed + while(cur->next && cur->next->x <= res.x + width) { + stbrp_node * next = cur->next; + // move the current node to the free list + cur->next = context->free_head; + context->free_head = cur; + cur = next; + } + + // stitch the list back in + node->next = cur; + + if(cur->x < res.x + width) + cur->x = (stbrp_coord)(res.x + width); + +#ifdef _DEBUG + cur = context->active_head; + while(cur->x < context->width) { + STBRP_ASSERT(cur->x < cur->next->x); + cur = cur->next; + } + STBRP_ASSERT(cur->next == NULL); + + { + int count = 0; + cur = context->active_head; + while(cur) { + cur = cur->next; + ++count; + } + cur = context->free_head; + while(cur) { + cur = cur->next; + ++count; + } + STBRP_ASSERT(count == context->num_nodes + 2); + } +#endif + + return res; +} + +static int STBRP__CDECL rect_height_compare(const void * a, const void * b) +{ + const stbrp_rect * p = (const stbrp_rect *) a; + const stbrp_rect * q = (const stbrp_rect *) b; + if(p->h > q->h) + return -1; + if(p->h < q->h) + return 1; + return (p->w > q->w) ? -1 : (p->w < q->w); +} + +static int STBRP__CDECL rect_original_order(const void * a, const void * b) +{ + const stbrp_rect * p = (const stbrp_rect *) a; + const stbrp_rect * q = (const stbrp_rect *) b; + return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed); +} + +STBRP_DEF int stbrp_pack_rects(stbrp_context * context, stbrp_rect * rects, int num_rects) +{ + int i, all_rects_packed = 1; + + // we use the 'was_packed' field internally to allow sorting/unsorting + for(i = 0; i < num_rects; ++i) { + rects[i].was_packed = i; + } + + // sort according to heuristic + STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_height_compare); + + for(i = 0; i < num_rects; ++i) { + if(rects[i].w == 0 || rects[i].h == 0) { + rects[i].x = rects[i].y = 0; // empty rect needs no space + } + else { + stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[i].w, rects[i].h); + if(fr.prev_link) { + rects[i].x = (stbrp_coord) fr.x; + rects[i].y = (stbrp_coord) fr.y; + } + else { + rects[i].x = rects[i].y = STBRP__MAXVAL; + } + } + } + + // unsort + STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_original_order); + + // set was_packed flags and all_rects_packed status + for(i = 0; i < num_rects; ++i) { + rects[i].was_packed = !(rects[i].x == STBRP__MAXVAL && rects[i].y == STBRP__MAXVAL); + if(!rects[i].was_packed) + all_rects_packed = 0; + } + + // return the all_rects_packed status + return all_rects_packed; +} +#endif + +#if defined(__GNUC__) || defined(__clang__) + #pragma GCC diagnostic pop +#endif + +/* +------------------------------------------------------------------------------ +This software is available under 2 licenses -- choose whichever you prefer. +------------------------------------------------------------------------------ +ALTERNATIVE A - MIT License +Copyright (c) 2017 Sean Barrett +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +------------------------------------------------------------------------------ +ALTERNATIVE B - Public Domain (www.unlicense.org) +This is free and unencumbered software released into the public domain. +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, +commercial or non-commercial, and by any means. +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and successors. We intend this dedication to be an +overt act of relinquishment in perpetuity of all present and future rights to +this software under copyright law. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +------------------------------------------------------------------------------ +*/ diff --git a/include/liblvgl/libs/tiny_ttf/stb_truetype_htcw.h b/include/liblvgl/libs/tiny_ttf/stb_truetype_htcw.h new file mode 100644 index 00000000..1cd3e2ab --- /dev/null +++ b/include/liblvgl/libs/tiny_ttf/stb_truetype_htcw.h @@ -0,0 +1,5598 @@ +// stb_truetype.h - v1.26htcw (fork to enable streaming and low memory environments) +// stb_truetype.h - v1.26 - public domain +// authored from 2009-2021 by Sean Barrett / RAD Game Tools +// +// ======================================================================= +// +// NO SECURITY GUARANTEE -- DO NOT USE THIS ON UNTRUSTED FONT FILES +// +// This library does no range checking of the offsets found in the file, +// meaning an attacker can use it to read arbitrary memory. +// +// ======================================================================= +// +// This library processes TrueType files: +// parse files +// extract glyph metrics +// extract glyph shapes +// render glyphs to one-channel bitmaps with antialiasing (box filter) +// render glyphs to one-channel SDF bitmaps (signed-distance field/function) +// +// Todo: +// non-MS cmaps +// crashproof on bad data +// hinting? (no longer patented) +// cleartype-style AA? +// optimize: use simple memory allocator for intermediates +// optimize: build edge-list directly from curves +// optimize: rasterize directly from curves? +// +// ADDITIONAL CONTRIBUTORS +// +// Mikko Mononen: compound shape support, more cmap formats +// Tor Andersson: kerning, subpixel rendering +// Dougall Johnson: OpenType / Type 2 font handling +// Daniel Ribeiro Maciel: basic GPOS-based kerning +// +// Misc other: +// Ryan Gordon +// Simon Glass +// github:IntellectualKitty +// Imanol Celaya +// Daniel Ribeiro Maciel +// +// Bug/warning reports/fixes: +// "Zer" on mollyrocket Fabian "ryg" Giesen github:NiLuJe +// Cass Everitt Martins Mozeiko github:aloucks +// stoiko (Haemimont Games) Cap Petschulat github:oyvindjam +// Brian Hook Omar Cornut github:vassvik +// Walter van Niftrik Ryan Griege +// David Gow Peter LaValle +// David Given Sergey Popov +// Ivan-Assen Ivanov Giumo X. Clanjor +// Anthony Pesch Higor Euripedes +// Johan Duparc Thomas Fields +// Hou Qiming Derek Vinyard +// Rob Loach Cort Stratton +// Kenney Phillis Jr. Brian Costabile +// Ken Voskuil (kaesve) +// +// VERSION HISTORY +// +// 1.26 (2021-08-28) fix broken rasterizer +// 1.25 (2021-07-11) many fixes +// 1.24 (2020-02-05) fix warning +// 1.23 (2020-02-02) query SVG data for glyphs; query whole kerning table (but only kern not GPOS) +// 1.22 (2019-08-11) minimize missing-glyph duplication; fix kerning if both 'GPOS' and 'kern' are defined +// 1.21 (2019-02-25) fix warning +// 1.20 (2019-02-07) PackFontRange skips missing codepoints; GetScaleFontVMetrics() +// 1.19 (2018-02-11) GPOS kerning, STBTT_fmod +// 1.18 (2018-01-29) add missing function +// 1.17 (2017-07-23) make more arguments const; doc fix +// 1.16 (2017-07-12) SDF support +// 1.15 (2017-03-03) make more arguments const +// 1.14 (2017-01-16) num-fonts-in-TTC function +// 1.13 (2017-01-02) support OpenType fonts, certain Apple fonts +// 1.12 (2016-10-25) suppress warnings about casting away const with -Wcast-qual +// 1.11 (2016-04-02) fix unused-variable warning +// 1.10 (2016-04-02) user-defined fabs(); rare memory leak; remove duplicate typedef +// 1.09 (2016-01-16) warning fix; avoid crash on outofmem; use allocation userdata properly +// 1.08 (2015-09-13) document stbtt_Rasterize(); fixes for vertical & horizontal edges +// 1.07 (2015-08-01) allow PackFontRanges to accept arrays of sparse codepoints; +// variant PackFontRanges to pack and render in separate phases; +// fix stbtt_GetFontOFfsetForIndex (never worked for non-0 input?); +// fixed an assert() bug in the new rasterizer +// replace assert() with STBTT_assert() in new rasterizer +// +// Full history can be found at the end of this file. +// +// LICENSE +// +// See end of file for license information. +// +// USAGE +// +// Include this file in whatever places need to refer to it. In ONE C/C++ +// file, write: +// #define STB_TRUETYPE_IMPLEMENTATION +// before the #include of this file. This expands out the actual +// implementation into that C/C++ file. +// +// To make the implementation private to the file that generates the implementation, +// #define STBTT_STATIC +// +// Simple 3D API (don't ship this, but it's fine for tools and quick start) +// stbtt_BakeFontBitmap() -- bake a font to a bitmap for use as texture +// stbtt_GetBakedQuad() -- compute quad to draw for a given char +// +// Improved 3D API (more shippable): +// #include "stb_rect_pack.h" -- optional, but you really want it +// stbtt_PackBegin() +// stbtt_PackSetOversampling() -- for improved quality on small fonts +// stbtt_PackFontRanges() -- pack and renders +// stbtt_PackEnd() +// stbtt_GetPackedQuad() +// +// "Load" a font file from a memory buffer (you have to keep the buffer loaded) +// stbtt_InitFont() +// stbtt_GetFontOffsetForIndex() -- indexing for TTC font collections +// stbtt_GetNumberOfFonts() -- number of fonts for TTC font collections +// +// Render a unicode codepoint to a bitmap +// stbtt_GetCodepointBitmap() -- allocates and returns a bitmap +// stbtt_MakeCodepointBitmap() -- renders into bitmap you provide +// stbtt_GetCodepointBitmapBox() -- how big the bitmap must be +// +// Character advance/positioning +// stbtt_GetCodepointHMetrics() +// stbtt_GetFontVMetrics() +// stbtt_GetFontVMetricsOS2() +// stbtt_GetCodepointKernAdvance() +// +// Starting with version 1.06, the rasterizer was replaced with a new, +// faster and generally-more-precise rasterizer. The new rasterizer more +// accurately measures pixel coverage for anti-aliasing, except in the case +// where multiple shapes overlap, in which case it overestimates the AA pixel +// coverage. Thus, anti-aliasing of intersecting shapes may look wrong. If +// this turns out to be a problem, you can re-enable the old rasterizer with +// #define STBTT_RASTERIZER_VERSION 1 +// which will incur about a 15% speed hit. +// +// ADDITIONAL DOCUMENTATION +// +// Immediately after this block comment are a series of sample programs. +// +// After the sample programs is the "header file" section. This section +// includes documentation for each API function. +// +// Some important concepts to understand to use this library: +// +// Codepoint +// Characters are defined by unicode codepoints, e.g. 65 is +// uppercase A, 231 is lowercase c with a cedilla, 0x7e30 is +// the hiragana for "ma". +// +// Glyph +// A visual character shape (every codepoint is rendered as +// some glyph) +// +// Glyph index +// A font-specific integer ID representing a glyph +// +// Baseline +// Glyph shapes are defined relative to a baseline, which is the +// bottom of uppercase characters. Characters extend both above +// and below the baseline. +// +// Current Point +// As you draw text to the screen, you keep track of a "current point" +// which is the origin of each character. The current point's vertical +// position is the baseline. Even "baked fonts" use this model. +// +// Vertical Font Metrics +// The vertical qualities of the font, used to vertically position +// and space the characters. See docs for stbtt_GetFontVMetrics. +// +// Font Size in Pixels or Points +// The preferred interface for specifying font sizes in stb_truetype +// is to specify how tall the font's vertical extent should be in pixels. +// If that sounds good enough, skip the next paragraph. +// +// Most font APIs instead use "points", which are a common typographic +// measurement for describing font size, defined as 72 points per inch. +// stb_truetype provides a point API for compatibility. However, true +// "per inch" conventions don't make much sense on computer displays +// since different monitors have different number of pixels per +// inch. For example, Windows traditionally uses a convention that +// there are 96 pixels per inch, thus making 'inch' measurements have +// nothing to do with inches, and thus effectively defining a point to +// be 1.333 pixels. Additionally, the TrueType font data provides +// an explicit scale factor to scale a given font's glyphs to points, +// but the author has observed that this scale factor is often wrong +// for non-commercial fonts, thus making fonts scaled in points +// according to the TrueType spec incoherently sized in practice. +// +// DETAILED USAGE: +// +// Scale: +// Select how high you want the font to be, in points or pixels. +// Call ScaleForPixelHeight or ScaleForMappingEmToPixels to compute +// a scale factor SF that will be used by all other functions. +// +// Baseline: +// You need to select a y-coordinate that is the baseline of where +// your text will appear. Call GetFontBoundingBox to get the baseline-relative +// bounding box for all characters. SF*-y0 will be the distance in pixels +// that the worst-case character could extend above the baseline, so if +// you want the top edge of characters to appear at the top of the +// screen where y=0, then you would set the baseline to SF*-y0. +// +// Current point: +// Set the current point where the first character will appear. The +// first character could extend left of the current point; this is font +// dependent. You can either choose a current point that is the leftmost +// point and hope, or add some padding, or check the bounding box or +// left-side-bearing of the first character to be displayed and set +// the current point based on that. +// +// Displaying a character: +// Compute the bounding box of the character. It will contain signed values +// relative to . I.e. if it returns x0,y0,x1,y1, +// then the character should be displayed in the rectangle from +// to = 32 && *text < 128) { + stbtt_aligned_quad q; + stbtt_GetBakedQuad(cdata, 512, 512, *text - 32, &x, &y, &q, 1);//1=opengl & d3d10+,0=d3d9 + glTexCoord2f(q.s0, q.t0); + glVertex2f(q.x0, q.y0); + glTexCoord2f(q.s1, q.t0); + glVertex2f(q.x1, q.y0); + glTexCoord2f(q.s1, q.t1); + glVertex2f(q.x1, q.y1); + glTexCoord2f(q.s0, q.t1); + glVertex2f(q.x0, q.y1); + } + ++text; + } + glEnd(); +} +#endif +// +// +////////////////////////////////////////////////////////////////////////////// +// +// Complete program (this compiles): get a single bitmap, print as ASCII art +// +#if 0 +#include +#define STB_TRUETYPE_IMPLEMENTATION // force following include to generate implementation +#include "stb_truetype.h" + +char ttf_buffer[1 << 25]; + +int main(int argc, char ** argv) +{ + stbtt_fontinfo font; + unsigned char * bitmap; + int w, h, i, j, c = (argc > 1 ? atoi(argv[1]) : 'a'), s = (argc > 2 ? atoi(argv[2]) : 20); + + fread(ttf_buffer, 1, 1 << 25, fopen(argc > 3 ? argv[3] : "c:/windows/fonts/arialbd.ttf", "rb")); + + stbtt_InitFont(&font, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer, 0)); + bitmap = stbtt_GetCodepointBitmap(&font, 0, stbtt_ScaleForPixelHeight(&font, s), c, &w, &h, 0, 0); + + for(j = 0; j < h; ++j) { + for(i = 0; i < w; ++i) + putchar(" .:ioVM@"[bitmap[j * w + i] >> 5]); + putchar('\n'); + } + return 0; +} +#endif +// +// Output: +// +// .ii. +// @@@@@@. +// V@Mio@@o +// :i. V@V +// :oM@@M +// :@@@MM@M +// @@o o@M +// :@@. M@M +// @@@o@@@@ +// :M@@V:@@. +// +////////////////////////////////////////////////////////////////////////////// +// +// Complete program: print "Hello World!" banner, with bugs +// +#if 0 +char buffer[24 << 20]; +unsigned char screen[20][79]; + +int main(int arg, char ** argv) +{ + stbtt_fontinfo font; + int i, j, ascent, baseline, ch = 0; + float scale, xpos = 2; // leave a little padding in case the character extends left + char * text = "Heljo World!"; // intentionally misspelled to show 'lj' brokenness + + fread(buffer, 1, 1000000, fopen("c:/windows/fonts/arialbd.ttf", "rb")); + stbtt_InitFont(&font, buffer, 0); + + scale = stbtt_ScaleForPixelHeight(&font, 15); + stbtt_GetFontVMetrics(&font, &ascent, 0, 0); + baseline = (int)(ascent * scale); + + while(text[ch]) { + int advance, lsb, x0, y0, x1, y1; + float x_shift = xpos - (float)floor(xpos); + stbtt_GetCodepointHMetrics(&font, text[ch], &advance, &lsb); + stbtt_GetCodepointBitmapBoxSubpixel(&font, text[ch], scale, scale, x_shift, 0, &x0, &y0, &x1, &y1); + stbtt_MakeCodepointBitmapSubpixel(&font, &screen[baseline + y0][(int)xpos + x0], x1 - x0, y1 - y0, 79, scale, scale, + x_shift, 0, text[ch]); + // note that this stomps the old data, so where character boxes overlap (e.g. 'lj') it's wrong + // because this API is really for baking character bitmaps into textures. if you want to render + // a sequence of characters, you really need to render each bitmap to a temp buffer, then + // "alpha blend" that into the working buffer + xpos += (advance * scale); + if(text[ch + 1]) + xpos += scale * stbtt_GetCodepointKernAdvance(&font, text[ch], text[ch + 1]); + ++ch; + } + + for(j = 0; j < 20; ++j) { + for(i = 0; i < 78; ++i) + putchar(" .:ioVM@"[screen[j][i] >> 5]); + putchar('\n'); + } + + return 0; +} +#endif + +////////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////////// +//// +//// INTEGRATION WITH YOUR CODEBASE +//// +//// The following sections allow you to supply alternate definitions +//// of C library functions used by stb_truetype, e.g. if you don't +//// link with the C runtime library. + +#ifdef STB_TRUETYPE_IMPLEMENTATION +// #define your own (u)stbtt_int8/16/32 before including to override this +#ifndef stbtt_uint8 + typedef unsigned char stbtt_uint8; + typedef signed char stbtt_int8; + typedef unsigned short stbtt_uint16; + typedef signed short stbtt_int16; + typedef unsigned int stbtt_uint32; + typedef signed int stbtt_int32; +#endif + +typedef char stbtt__check_size32[sizeof(stbtt_int32) == 4 ? 1 : -1]; +typedef char stbtt__check_size16[sizeof(stbtt_int16) == 2 ? 1 : -1]; + +// define STBTT_STDIO_STREAM to stream from a FILE object +// instead of from memory. Or define STBTT_STREAM_TYPE, +// STBTT_STREAM_READ and STBTT_STREAM_SEEK to implement +// another streaming source +#ifdef STBTT_STDIO_STREAM + #include + #define STBTT_STREAM_TYPE FILE* + #define STBTT_STREAM_READ(s,x,y) fread(x,1,y,s); + #define STBTT_STREAM_SEEK(s,x) fseek(s,x,SEEK_SET); +#endif + +// heap factor sizes for various counts of objects +// adjust for your platform. Below is suitable for +// modern PC class machines. +#ifndef STBTT_HEAP_FACTOR_SIZE_32 + #define STBTT_HEAP_FACTOR_SIZE_32 2000 +#endif + +#ifndef STBTT_HEAP_FACTOR_SIZE_128 + #define STBTT_HEAP_FACTOR_SIZE_128 800 +#endif + +#ifndef STBTT_HEAP_FACTOR_SIZE_DEFAULT + #define STBTT_HEAP_FACTOR_SIZE_DEFAULT 100 +#endif + +// e.g. #define your own STBTT_ifloor/STBTT_iceil() to avoid math.h +#ifndef STBTT_ifloor + #include + #define STBTT_ifloor(x) ((int) floor(x)) + #define STBTT_iceil(x) ((int) ceil(x)) +#endif + +#ifndef STBTT_sqrt + #include + #define STBTT_sqrt(x) (float)sqrt(x) + #define STBTT_pow(x,y) pow(x,y) +#endif + +#ifndef STBTT_fmod + #include + #define STBTT_fmod(x,y) fmod(x,y) +#endif + +#ifndef STBTT_cos + #include + #define STBTT_cos(x) cos(x) + #define STBTT_acos(x) acos(x) +#endif + +#ifndef STBTT_fabs + #include + #define STBTT_fabs(x) (float)fabs(x) +#endif + +// #define your own functions "STBTT_malloc" / "STBTT_free" to avoid malloc.h +#ifndef STBTT_malloc + #include + #define STBTT_malloc(x,u) ((void)(u),malloc(x)) + #define STBTT_free(x,u) ((void)(u),free(x)) +#endif + +#ifndef STBTT_assert + #include + #define STBTT_assert(x) assert(x) +#endif + +#ifndef STBTT_strlen + #include + #define STBTT_strlen(x) strlen(x) +#endif + +#ifndef STBTT_memcpy + #include + #define STBTT_memcpy memcpy + #define STBTT_memset memset +#endif +#endif + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// +//// +//// INTERFACE +//// +//// + +#ifndef __STB_INCLUDE_STB_TRUETYPE_H__ +#define __STB_INCLUDE_STB_TRUETYPE_H__ + +#ifdef STBTT_STATIC + #define STBTT_DEF static +#else + #define STBTT_DEF extern +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +// private structure +typedef struct { +#ifdef STBTT_STREAM_TYPE + STBTT_STREAM_TYPE data; + stbtt_uint32 offset; +#else + unsigned char * data; +#endif + int cursor; + int size; +} stbtt__buf; + +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-function" +#endif + +////////////////////////////////////////////////////////////////////////////// +// +// TEXTURE BAKING API +// +// If you use this API, you only have to call two functions ever. +// + +typedef struct { + unsigned short x0, y0, x1, y1; // coordinates of bbox in bitmap + float xoff, yoff, xadvance; +} stbtt_bakedchar; + +typedef struct { + float x0, y0, s0, t0; // top-left + float x1, y1, s1, t1; // bottom-right +} stbtt_aligned_quad; + +STBTT_DEF void stbtt_GetBakedQuad(const stbtt_bakedchar * chardata, int pw, int ph, // same data as above + int char_index, // character to display + float * xpos, float * ypos, // pointers to current position in screen pixel space + stbtt_aligned_quad * q, // output: quad to draw + int opengl_fillrule); // true if opengl fill rule; false if DX9 or earlier +// Call GetBakedQuad with char_index = 'character - first_char', and it +// creates the quad you need to draw and advances the current position. +// +// The coordinate system used assumes y increases downwards. +// +// Characters will extend both above and below the current position; +// see discussion of "BASELINE" above. +// +// It's inefficient; you might want to c&p it and optimize it. + +////////////////////////////////////////////////////////////////////////////// +// +// NEW TEXTURE BAKING API +// +// This provides options for packing multiple fonts into one atlas, not +// perfectly but better than nothing. + +typedef struct { + unsigned short x0, y0, x1, y1; // coordinates of bbox in bitmap + float xoff, yoff, xadvance; + float xoff2, yoff2; +} stbtt_packedchar; + +/// @cond +/** + * Tells Doxygen to ignore a duplicate declaration + */ +typedef struct stbtt_pack_context stbtt_pack_context; +typedef struct stbtt_fontinfo stbtt_fontinfo; +/// @endcond + +#ifndef STB_RECT_PACK_VERSION +/// @cond +/** + * Tells Doxygen to ignore a duplicate declaration + */ +typedef struct stbrp_rect stbrp_rect; +/// @endcond + +#endif +STBTT_DEF int stbtt_PackBegin(stbtt_pack_context * spc, unsigned char * pixels, int width, int height, + int stride_in_bytes, int padding, void * alloc_context); + +// Initializes a packing context stored in the passed-in stbtt_pack_context. +// Future calls using this context will pack characters into the bitmap passed +// in here: a 1-channel bitmap that is width * height. stride_in_bytes is +// the distance from one row to the next (or 0 to mean they are packed tightly +// together). "padding" is the amount of padding to leave between each +// character (normally you want '1' for bitmaps you'll use as textures with +// bilinear filtering). +// +// Returns 0 on failure, 1 on success. + +STBTT_DEF void stbtt_PackEnd(stbtt_pack_context * spc); +// Cleans up the packing context and frees all memory. + +#define STBTT_POINT_SIZE(x) (-(x)) + +typedef struct { + float font_size; + int first_unicode_codepoint_in_range; // if non-zero, then the chars are continuous, and this is the first codepoint + int * array_of_unicode_codepoints; // if non-zero, then this is an array of unicode codepoints + int num_chars; + stbtt_packedchar * chardata_for_range; // output + unsigned char h_oversample, v_oversample; // don't set these, they're used internally +} stbtt_pack_range; + +STBTT_DEF void stbtt_PackSetOversampling(stbtt_pack_context * spc, unsigned int h_oversample, + unsigned int v_oversample); +// Oversampling a font increases the quality by allowing higher-quality subpixel +// positioning, and is especially valuable at smaller text sizes. +// +// This function sets the amount of oversampling for all following calls to +// stbtt_PackFontRange(s) or stbtt_PackFontRangesGatherRects for a given +// pack context. The default (no oversampling) is achieved by h_oversample=1 +// and v_oversample=1. The total number of pixels required is +// h_oversample*v_oversample larger than the default; for example, 2x2 +// oversampling requires 4x the storage of 1x1. For best results, render +// oversampled textures with bilinear filtering. Look at the readme in +// stb/tests/oversample for information about oversampled fonts +// +// To use with PackFontRangesGather etc., you must set it before calls +// call to PackFontRangesGatherRects. + +STBTT_DEF void stbtt_PackSetSkipMissingCodepoints(stbtt_pack_context * spc, int skip); +// If skip != 0, this tells stb_truetype to skip any codepoints for which +// there is no corresponding glyph. If skip=0, which is the default, then +// codepoints without a glyph received the font's "missing character" glyph, +// typically an empty box by convention. + +STBTT_DEF void stbtt_GetPackedQuad(const stbtt_packedchar * chardata, int pw, int ph, // same data as above + int char_index, // character to display + float * xpos, float * ypos, // pointers to current position in screen pixel space + stbtt_aligned_quad * q, // output: quad to draw + int align_to_integer); + +STBTT_DEF int stbtt_PackFontRangesGatherRects(stbtt_pack_context * spc, const stbtt_fontinfo * info, + stbtt_pack_range * ranges, int num_ranges, stbrp_rect * rects); +STBTT_DEF void stbtt_PackFontRangesPackRects(stbtt_pack_context * spc, stbrp_rect * rects, int num_rects); +STBTT_DEF int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context * spc, const stbtt_fontinfo * info, + stbtt_pack_range * ranges, int num_ranges, stbrp_rect * rects); +// Calling these functions in sequence is roughly equivalent to calling +// stbtt_PackFontRanges(). If you more control over the packing of multiple +// fonts, or if you want to pack custom data into a font texture, take a look +// at the source to of stbtt_PackFontRanges() and create a custom version +// using these functions, e.g. call GatherRects multiple times, +// building up a single array of rects, then call PackRects once, +// then call RenderIntoRects repeatedly. This may result in a +// better packing than calling PackFontRanges multiple times +// (or it may not). + +// this is an opaque structure that you shouldn't mess with which holds +// all the context needed from PackBegin to PackEnd. +struct stbtt_pack_context { + void * user_allocator_context; + void * pack_info; + int width; + int height; + int stride_in_bytes; + int padding; + int skip_missing; + unsigned int h_oversample, v_oversample; + unsigned char * pixels; + void * nodes; +}; + +////////////////////////////////////////////////////////////////////////////// +// +// FONT LOADING +// +// +#ifdef STBTT_STREAM_TYPE +STBTT_DEF int stbtt_GetNumberOfFonts(STBTT_STREAM_TYPE data); +#else +STBTT_DEF int stbtt_GetNumberOfFonts(const unsigned char * data); +#endif +// This function will determine the number of fonts in a font file. TrueType +// collection (.ttc) files may contain multiple fonts, while TrueType font +// (.ttf) files only contain one font. The number of fonts can be used for +// indexing with the previous function where the index is between zero and one +// less than the total fonts. If an error occurs, -1 is returned. +#ifdef STBTT_STREAM_TYPE +STBTT_DEF int stbtt_GetFontOffsetForIndex(STBTT_STREAM_TYPE, int index); +#else +STBTT_DEF int stbtt_GetFontOffsetForIndex(const unsigned char * data, int index); +#endif + +// Each .ttf/.ttc file may have more than one font. Each font has a sequential +// index number starting from 0. Call this function to get the font offset for +// a given index; it returns -1 if the index is out of range. A regular .ttf +// file will only define one font and it always be at offset 0, so it will +// return '0' for index 0, and -1 for all other indices. + +// The following structure is defined publicly so you can declare one on +// the stack or as a global or etc, but you should treat it as opaque. +struct stbtt_fontinfo { + void * userdata; +#ifdef STBTT_STREAM_TYPE + STBTT_STREAM_TYPE data; +#else + unsigned char * data; // pointer to .ttf file +#endif + int fontstart; // offset of start of font + + int numGlyphs; // number of glyphs, needed for range checking + + int loca, head, glyf, hhea, hmtx, kern, gpos, svg; // table locations as offset from start of .ttf + int index_map; // a cmap mapping for our chosen character encoding + int indexToLocFormat; // format needed to map from glyph index to glyph + + stbtt__buf cff; // cff font data + stbtt__buf charstrings; // the charstring index + stbtt__buf gsubrs; // global charstring subroutines index + stbtt__buf subrs; // private charstring subroutines index + stbtt__buf fontdicts; // array of font dicts + stbtt__buf fdselect; // map from glyph to fontdict +}; +#ifdef STBTT_STREAM_TYPE +STBTT_DEF int stbtt_InitFont(stbtt_fontinfo * info, STBTT_STREAM_TYPE data, int offset); +#else +STBTT_DEF int stbtt_InitFont(stbtt_fontinfo * info, const unsigned char * data, int offset); +#endif +// Given an offset into the file that defines a font, this function builds +// the necessary cached info for the rest of the system. You must allocate +// the stbtt_fontinfo yourself, and stbtt_InitFont will fill it out. You don't +// need to do anything special to free it, because the contents are pure +// value data with no additional data structures. Returns 0 on failure. + +////////////////////////////////////////////////////////////////////////////// +// +// CHARACTER TO GLYPH-INDEX CONVERSIOn + +STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo * info, int unicode_codepoint); +// If you're going to perform multiple operations on the same character +// and you want a speed-up, call this function with the character you're +// going to process, then use glyph-based functions instead of the +// codepoint-based functions. +// Returns 0 if the character codepoint is not defined in the font. + +////////////////////////////////////////////////////////////////////////////// +// +// CHARACTER PROPERTIES +// + +STBTT_DEF float stbtt_ScaleForPixelHeight(const stbtt_fontinfo * info, float pixels); +// computes a scale factor to produce a font whose "height" is 'pixels' tall. +// Height is measured as the distance from the highest ascender to the lowest +// descender; in other words, it's equivalent to calling stbtt_GetFontVMetrics +// and computing: +// scale = pixels / (ascent - descent) +// so if you prefer to measure height by the ascent only, use a similar calculation. + +STBTT_DEF float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo * info, float pixels); +// computes a scale factor to produce a font whose EM size is mapped to +// 'pixels' tall. This is probably what traditional APIs compute, but +// I'm not positive. + +STBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo * info, int * ascent, int * descent, int * lineGap); +// ascent is the coordinate above the baseline the font extends; descent +// is the coordinate below the baseline the font extends (i.e. it is typically negative) +// lineGap is the spacing between one row's descent and the next row's ascent... +// so you should advance the vertical position by "*ascent - *descent + *lineGap" +// these are expressed in unscaled coordinates, so you must multiply by +// the scale factor for a given size + +STBTT_DEF int stbtt_GetFontVMetricsOS2(const stbtt_fontinfo * info, int * typoAscent, int * typoDescent, + int * typoLineGap); +// analogous to GetFontVMetrics, but returns the "typographic" values from the OS/2 +// table (specific to MS/Windows TTF files). +// +// Returns 1 on success (table present), 0 on failure. + +STBTT_DEF void stbtt_GetFontBoundingBox(const stbtt_fontinfo * info, int * x0, int * y0, int * x1, int * y1); +// the bounding box around all possible characters + +STBTT_DEF void stbtt_GetCodepointHMetrics(const stbtt_fontinfo * info, int codepoint, int * advanceWidth, + int * leftSideBearing); +// leftSideBearing is the offset from the current horizontal position to the left edge of the character +// advanceWidth is the offset from the current horizontal position to the next horizontal position +// these are expressed in unscaled coordinates + +STBTT_DEF int stbtt_GetCodepointKernAdvance(const stbtt_fontinfo * info, int ch1, int ch2); +// an additional amount to add to the 'advance' value between ch1 and ch2 + +STBTT_DEF int stbtt_GetCodepointBox(const stbtt_fontinfo * info, int codepoint, int * x0, int * y0, int * x1, int * y1); +// Gets the bounding box of the visible part of the glyph, in unscaled coordinates + +STBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo * info, int glyph_index, int * advanceWidth, + int * leftSideBearing); +STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo * info, int glyph1, int glyph2); +STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo * info, int glyph_index, int * x0, int * y0, int * x1, int * y1); +// as above, but takes one or more glyph indices for greater efficiency + +typedef struct _stbtt_kerningentry { + int glyph1; // use stbtt_FindGlyphIndex + int glyph2; + int advance; +} stbtt_kerningentry; + +STBTT_DEF int stbtt_GetKerningTableLength(const stbtt_fontinfo * info); +STBTT_DEF int stbtt_GetKerningTable(const stbtt_fontinfo * info, stbtt_kerningentry * table, int table_length); +// Retrieves a complete list of all of the kerning pairs provided by the font +// stbtt_GetKerningTable never writes more than table_length entries and returns how many entries it did write. +// The table will be sorted by (a.glyph1 == b.glyph1)?(a.glyph2 < b.glyph2):(a.glyph1 < b.glyph1) + +////////////////////////////////////////////////////////////////////////////// +// +// GLYPH SHAPES (you probably don't need these, but they have to go before +// the bitmaps for C declaration-order reasons) +// + +#ifndef STBTT_vmove // you can predefine these to use different values (but why?) +enum { + STBTT_vmove = 1, + STBTT_vline, + STBTT_vcurve, + STBTT_vcubic +}; +#endif + +#ifndef stbtt_vertex // you can predefine this to use different values +// (we share this with other code at RAD) +#define stbtt_vertex_type short // can't use stbtt_int16 because that's not visible in the header file +typedef struct { + stbtt_vertex_type x, y, cx, cy, cx1, cy1; + unsigned char type, padding; +} stbtt_vertex; +#endif + +STBTT_DEF int stbtt_IsGlyphEmpty(const stbtt_fontinfo * info, int glyph_index); +// returns non-zero if nothing is drawn for this glyph + +STBTT_DEF int stbtt_GetCodepointShape(const stbtt_fontinfo * info, int unicode_codepoint, stbtt_vertex ** vertices); +STBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo * info, int glyph_index, stbtt_vertex ** vertices); +// returns # of vertices and fills *vertices with the pointer to them +// these are expressed in "unscaled" coordinates +// +// The shape is a series of contours. Each one starts with +// a STBTT_moveto, then consists of a series of mixed +// STBTT_lineto and STBTT_curveto segments. A lineto +// draws a line from previous endpoint to its x,y; a curveto +// draws a quadratic bezier from previous endpoint to +// its x,y, using cx,cy as the bezier control point. + +STBTT_DEF void stbtt_FreeShape(const stbtt_fontinfo * info, stbtt_vertex * vertices); +// frees the data allocated above + +STBTT_DEF stbtt_uint32 stbtt_FindSVGDoc(const stbtt_fontinfo * info, int gl); +STBTT_DEF int stbtt_GetCodepointSVG(const stbtt_fontinfo * info, int unicode_codepoint, stbtt_uint32 * svgOfs); +STBTT_DEF int stbtt_GetGlyphSVG(const stbtt_fontinfo * info, int gl, stbtt_uint32 * svgOfs); +// fills svg with the character's SVG data. +// returns data size or 0 if SVG not found. + +////////////////////////////////////////////////////////////////////////////// +// +// BITMAP RENDERING +// + +STBTT_DEF void stbtt_FreeBitmap(unsigned char * bitmap, void * userdata); +// frees the bitmap allocated below + +STBTT_DEF unsigned char * stbtt_GetCodepointBitmap(const stbtt_fontinfo * info, float scale_x, float scale_y, + int codepoint, int * width, int * height, int * xoff, int * yoff); +// allocates a large-enough single-channel 8bpp bitmap and renders the +// specified character/glyph at the specified scale into it, with +// antialiasing. 0 is no coverage (transparent), 255 is fully covered (opaque). +// *width & *height are filled out with the width & height of the bitmap, +// which is stored left-to-right, top-to-bottom. +// +// xoff/yoff are the offset it pixel space from the glyph origin to the top-left of the bitmap + +STBTT_DEF unsigned char * stbtt_GetCodepointBitmapSubpixel(const stbtt_fontinfo * info, float scale_x, float scale_y, + float shift_x, float shift_y, int codepoint, int * width, int * height, int * xoff, int * yoff); +// the same as stbtt_GetCodepointBitmap, but you can specify a subpixel +// shift for the character + +STBTT_DEF void stbtt_MakeCodepointBitmap(const stbtt_fontinfo * info, unsigned char * output, int out_w, int out_h, + int out_stride, float scale_x, float scale_y, int codepoint); +// the same as stbtt_GetCodepointBitmap, but you pass in storage for the bitmap +// in the form of 'output', with row spacing of 'out_stride' bytes. the bitmap +// is clipped to out_w/out_h bytes. Call stbtt_GetCodepointBitmapBox to get the +// width and height and positioning info for it first. + +STBTT_DEF void stbtt_MakeCodepointBitmapSubpixel(const stbtt_fontinfo * info, unsigned char * output, int out_w, + int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint); +// same as stbtt_MakeCodepointBitmap, but you can specify a subpixel +// shift for the character + +STBTT_DEF void stbtt_MakeCodepointBitmapSubpixelPrefilter(const stbtt_fontinfo * info, unsigned char * output, + int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int oversample_x, + int oversample_y, float * sub_x, float * sub_y, int codepoint); +// same as stbtt_MakeCodepointBitmapSubpixel, but prefiltering +// is performed (see stbtt_PackSetOversampling) + +STBTT_DEF void stbtt_GetCodepointBitmapBox(const stbtt_fontinfo * font, int codepoint, float scale_x, float scale_y, + int * ix0, int * iy0, int * ix1, int * iy1); +// get the bbox of the bitmap centered around the glyph origin; so the +// bitmap width is ix1-ix0, height is iy1-iy0, and location to place +// the bitmap top left is (leftSideBearing*scale,iy0). +// (Note that the bitmap uses y-increases-down, but the shape uses +// y-increases-up, so CodepointBitmapBox and CodepointBox are inverted.) + +STBTT_DEF void stbtt_GetCodepointBitmapBoxSubpixel(const stbtt_fontinfo * font, int codepoint, float scale_x, + float scale_y, float shift_x, float shift_y, int * ix0, int * iy0, int * ix1, int * iy1); +// same as stbtt_GetCodepointBitmapBox, but you can specify a subpixel +// shift for the character + +// the following functions are equivalent to the above functions, but operate +// on glyph indices instead of Unicode codepoints (for efficiency) +STBTT_DEF unsigned char * stbtt_GetGlyphBitmap(const stbtt_fontinfo * info, float scale_x, float scale_y, int glyph, + int * width, int * height, int * xoff, int * yoff); +STBTT_DEF unsigned char * stbtt_GetGlyphBitmapSubpixel(const stbtt_fontinfo * info, float scale_x, float scale_y, + float shift_x, float shift_y, int glyph, int * width, int * height, int * xoff, int * yoff); +STBTT_DEF void stbtt_MakeGlyphBitmap(const stbtt_fontinfo * info, unsigned char * output, int out_w, int out_h, + int out_stride, float scale_x, float scale_y, int glyph); +STBTT_DEF void stbtt_MakeGlyphBitmapSubpixel(const stbtt_fontinfo * info, unsigned char * output, int out_w, int out_h, + int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int glyph); +STBTT_DEF void stbtt_MakeGlyphBitmapSubpixelPrefilter(const stbtt_fontinfo * info, unsigned char * output, int out_w, + int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int oversample_x, + int oversample_y, float * sub_x, float * sub_y, int glyph); +STBTT_DEF void stbtt_GetGlyphBitmapBox(const stbtt_fontinfo * font, int glyph, float scale_x, float scale_y, int * ix0, + int * iy0, int * ix1, int * iy1); +STBTT_DEF void stbtt_GetGlyphBitmapBoxSubpixel(const stbtt_fontinfo * font, int glyph, float scale_x, float scale_y, + float shift_x, float shift_y, int * ix0, int * iy0, int * ix1, int * iy1); + +// @TODO: don't expose this structure +typedef struct { + int w, h, stride; + unsigned char * pixels; +} stbtt__bitmap; + +// rasterize a shape with quadratic beziers into a bitmap +STBTT_DEF void stbtt_Rasterize(stbtt__bitmap * result, // 1-channel bitmap to draw into + float flatness_in_pixels, // allowable error of curve in pixels + stbtt_vertex * vertices, // array of vertices defining shape + int num_verts, // number of vertices in above array + float scale_x, float scale_y, // scale applied to input vertices + float shift_x, float shift_y, // translation applied to input vertices + int x_off, int y_off, // another translation applied to input + int invert, // if non-zero, vertically flip shape + void * userdata); // context for to STBTT_MALLOC + +////////////////////////////////////////////////////////////////////////////// +// +// Signed Distance Function (or Field) rendering + +STBTT_DEF void stbtt_FreeSDF(unsigned char * bitmap, void * userdata); +// frees the SDF bitmap allocated below + +STBTT_DEF unsigned char * stbtt_GetGlyphSDF(const stbtt_fontinfo * info, float scale, int glyph, int padding, + unsigned char onedge_value, float pixel_dist_scale, int * width, int * height, int * xoff, int * yoff); +STBTT_DEF unsigned char * stbtt_GetCodepointSDF(const stbtt_fontinfo * info, float scale, int codepoint, int padding, + unsigned char onedge_value, float pixel_dist_scale, int * width, int * height, int * xoff, int * yoff); +// These functions compute a discretized SDF field for a single character, suitable for storing +// in a single-channel texture, sampling with bilinear filtering, and testing against +// larger than some threshold to produce scalable fonts. +// info -- the font +// scale -- controls the size of the resulting SDF bitmap, same as it would be creating a regular bitmap +// glyph/codepoint -- the character to generate the SDF for +// padding -- extra "pixels" around the character which are filled with the distance to the character (not 0), +// which allows effects like bit outlines +// onedge_value -- value 0-255 to test the SDF against to reconstruct the character (i.e. the isocontour of the character) +// pixel_dist_scale -- what value the SDF should increase by when moving one SDF "pixel" away from the edge (on the 0..255 scale) +// if positive, > onedge_value is inside; if negative, < onedge_value is inside +// width,height -- output height & width of the SDF bitmap (including padding) +// xoff,yoff -- output origin of the character +// return value -- a 2D array of bytes 0..255, width*height in size +// +// pixel_dist_scale & onedge_value are a scale & bias that allows you to make +// optimal use of the limited 0..255 for your application, trading off precision +// and special effects. SDF values outside the range 0..255 are clamped to 0..255. +// +// Example: +// scale = stbtt_ScaleForPixelHeight(22) +// padding = 5 +// onedge_value = 180 +// pixel_dist_scale = 180/5.0 = 36.0 +// +// This will create an SDF bitmap in which the character is about 22 pixels +// high but the whole bitmap is about 22+5+5=32 pixels high. To produce a filled +// shape, sample the SDF at each pixel and fill the pixel if the SDF value +// is greater than or equal to 180/255. (You'll actually want to antialias, +// which is beyond the scope of this example.) Additionally, you can compute +// offset outlines (e.g. to stroke the character border inside & outside, +// or only outside). For example, to fill outside the character up to 3 SDF +// pixels, you would compare against (180-36.0*3)/255 = 72/255. The above +// choice of variables maps a range from 5 pixels outside the shape to +// 2 pixels inside the shape to 0..255; this is intended primarily for apply +// outside effects only (the interior range is needed to allow proper +// antialiasing of the font at *smaller* sizes) +// +// The function computes the SDF analytically at each SDF pixel, not by e.g. +// building a higher-res bitmap and approximating it. In theory the quality +// should be as high as possible for an SDF of this size & representation, but +// unclear if this is true in practice (perhaps building a higher-res bitmap +// and computing from that can allow drop-out prevention). +// +// The algorithm has not been optimized at all, so expect it to be slow +// if computing lots of characters or very large sizes. + +////////////////////////////////////////////////////////////////////////////// +// +// Finding the right font... +// +// You should really just solve this offline, keep your own tables +// of what font is what, and don't try to get it out of the .ttf file. +// That's because getting it out of the .ttf file is really hard, because +// the names in the file can appear in many possible encodings, in many +// possible languages, and e.g. if you need a case-insensitive comparison, +// the details of that depend on the encoding & language in a complex way +// (actually underspecified in truetype, but also gigantic). +// +// But you can use the provided functions in two possible ways: +// stbtt_FindMatchingFont() will use *case-sensitive* comparisons on +// unicode-encoded names to try to find the font you want; +// you can run this before calling stbtt_InitFont() +// +// stbtt_GetFontNameString() lets you get any of the various strings +// from the file yourself and do your own comparisons on them. +// You have to have called stbtt_InitFont() first. + +#ifdef STBTT_STREAM_TYPE +STBTT_DEF int stbtt_FindMatchingFont(STBTT_STREAM_TYPE fontdata, const char * name, int flags); +#else +STBTT_DEF int stbtt_FindMatchingFont(const unsigned char * fontdata, const char * name, int flags); +#endif +// returns the offset (not index) of the font that matches, or -1 if none +// if you use STBTT_MACSTYLE_DONTCARE, use a font name like "Arial Bold". +// if you use any other flag, use a font name like "Arial"; this checks +// the 'macStyle' header field; i don't know if fonts set this consistently +#define STBTT_MACSTYLE_DONTCARE 0 +#define STBTT_MACSTYLE_BOLD 1 +#define STBTT_MACSTYLE_ITALIC 2 +#define STBTT_MACSTYLE_UNDERSCORE 4 +#define STBTT_MACSTYLE_NONE 8 // <= not same as 0, this makes us check the bitfield is 0 + +#ifdef STBTT_STREAM_TYPE +STBTT_DEF int stbtt_CompareUTF8toUTF16_bigendian(const char * s1, int len1, STBTT_STREAM_TYPE s2, stbtt_uint32 s2offs, + int len2); +#else +STBTT_DEF int stbtt_CompareUTF8toUTF16_bigendian(const char * s1, int len1, const char * s2, stbtt_uint32 s2offs, + int len2); +#endif +// returns 1/0 whether the first string interpreted as utf8 is identical to +// the second string interpreted as big-endian utf16... useful for strings from next func + +STBTT_DEF stbtt_uint32 stbtt_GetFontNameString(const stbtt_fontinfo * font, int * length, int platformID, + int encodingID, int languageID, int nameID); + +// returns the string (which may be big-endian double byte, e.g. for unicode) +// and puts the length in bytes in *length. +// +// some of the values for the IDs are below; for more see the truetype spec: +// http://developer.apple.com/textfonts/TTRefMan/RM06/Chap6name.html +// http://www.microsoft.com/typography/otspec/name.htm + +enum { // platformID + STBTT_PLATFORM_ID_UNICODE = 0, + STBTT_PLATFORM_ID_MAC = 1, + STBTT_PLATFORM_ID_ISO = 2, + STBTT_PLATFORM_ID_MICROSOFT = 3 +}; + +enum { // encodingID for STBTT_PLATFORM_ID_UNICODE + STBTT_UNICODE_EID_UNICODE_1_0 = 0, + STBTT_UNICODE_EID_UNICODE_1_1 = 1, + STBTT_UNICODE_EID_ISO_10646 = 2, + STBTT_UNICODE_EID_UNICODE_2_0_BMP = 3, + STBTT_UNICODE_EID_UNICODE_2_0_FULL = 4 +}; + +enum { // encodingID for STBTT_PLATFORM_ID_MICROSOFT + STBTT_MS_EID_SYMBOL = 0, + STBTT_MS_EID_UNICODE_BMP = 1, + STBTT_MS_EID_SHIFTJIS = 2, + STBTT_MS_EID_UNICODE_FULL = 10 +}; + +enum { // encodingID for STBTT_PLATFORM_ID_MAC; same as Script Manager codes + STBTT_MAC_EID_ROMAN = 0, STBTT_MAC_EID_ARABIC = 4, + STBTT_MAC_EID_JAPANESE = 1, STBTT_MAC_EID_HEBREW = 5, + STBTT_MAC_EID_CHINESE_TRAD = 2, STBTT_MAC_EID_GREEK = 6, + STBTT_MAC_EID_KOREAN = 3, STBTT_MAC_EID_RUSSIAN = 7 +}; + +enum { // languageID for STBTT_PLATFORM_ID_MICROSOFT; same as LCID... + // problematic because there are e.g. 16 english LCIDs and 16 arabic LCIDs + STBTT_MS_LANG_ENGLISH = 0x0409, STBTT_MS_LANG_ITALIAN = 0x0410, + STBTT_MS_LANG_CHINESE = 0x0804, STBTT_MS_LANG_JAPANESE = 0x0411, + STBTT_MS_LANG_DUTCH = 0x0413, STBTT_MS_LANG_KOREAN = 0x0412, + STBTT_MS_LANG_FRENCH = 0x040c, STBTT_MS_LANG_RUSSIAN = 0x0419, + STBTT_MS_LANG_GERMAN = 0x0407, STBTT_MS_LANG_SPANISH = 0x0409, + STBTT_MS_LANG_HEBREW = 0x040d, STBTT_MS_LANG_SWEDISH = 0x041D +}; + +enum { // languageID for STBTT_PLATFORM_ID_MAC + STBTT_MAC_LANG_ENGLISH = 0, STBTT_MAC_LANG_JAPANESE = 11, + STBTT_MAC_LANG_ARABIC = 12, STBTT_MAC_LANG_KOREAN = 23, + STBTT_MAC_LANG_DUTCH = 4, STBTT_MAC_LANG_RUSSIAN = 32, + STBTT_MAC_LANG_FRENCH = 1, STBTT_MAC_LANG_SPANISH = 6, + STBTT_MAC_LANG_GERMAN = 2, STBTT_MAC_LANG_SWEDISH = 5, + STBTT_MAC_LANG_HEBREW = 10, STBTT_MAC_LANG_CHINESE_SIMPLIFIED = 33, + STBTT_MAC_LANG_ITALIAN = 3, STBTT_MAC_LANG_CHINESE_TRAD = 19 +}; + +#ifdef __cplusplus +} +#endif + +#endif // __STB_INCLUDE_STB_TRUETYPE_H__ + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// +//// +//// IMPLEMENTATION +//// +//// + +#ifdef STB_TRUETYPE_IMPLEMENTATION + +#ifndef STBTT_MAX_OVERSAMPLE + #define STBTT_MAX_OVERSAMPLE 8 +#endif + +#if STBTT_MAX_OVERSAMPLE > 255 + #error "STBTT_MAX_OVERSAMPLE cannot be > 255" +#endif + +typedef int stbtt__test_oversample_pow2[(STBTT_MAX_OVERSAMPLE & (STBTT_MAX_OVERSAMPLE - 1)) == 0 ? 1 : -1]; + +#ifndef STBTT_RASTERIZER_VERSION + #define STBTT_RASTERIZER_VERSION 2 +#endif + +#ifdef _MSC_VER + #define STBTT__NOTUSED(v) (void)(v) +#else + #define STBTT__NOTUSED(v) (void)sizeof(v) +#endif + +////////////////////////////////////////////////////////////////////////// +// +// stbtt__buf helpers to parse data from file +// + +static stbtt_uint8 stbtt__buf_get8(stbtt__buf * b) +{ + if(b->cursor >= b->size) + return 0; +#ifdef STBTT_STREAM_TYPE + long pos = (long)(b->cursor + b->offset); + STBTT_STREAM_SEEK(b->data, pos); + stbtt_uint8 result; + STBTT_STREAM_READ(b->data, &result, 1); + ++b->cursor; + return result; +#else + return b->data[b->cursor++]; +#endif + +} + +static stbtt_uint8 stbtt__buf_peek8(stbtt__buf * b) +{ + if(b->cursor >= b->size) + return 0; +#ifdef STBTT_STREAM_TYPE + long pos = (long)(b->cursor + b->offset); + STBTT_STREAM_SEEK(b->data, pos); + stbtt_uint8 result; + STBTT_STREAM_READ(b->data, &result, 1); + return result; +#else + return b->data[b->cursor]; +#endif + +} + +static void stbtt__buf_seek(stbtt__buf * b, int o) +{ + STBTT_assert(!(o > b->size || o < 0)); + b->cursor = (o > b->size || o < 0) ? b->size : o; +} + +static void stbtt__buf_skip(stbtt__buf * b, int o) +{ + stbtt__buf_seek(b, b->cursor + o); +} + +static stbtt_uint32 stbtt__buf_get(stbtt__buf * b, int n) +{ + stbtt_uint32 v = 0; + int i; + STBTT_assert(n >= 1 && n <= 4); + for(i = 0; i < n; i++) + v = (v << 8) | stbtt__buf_get8(b); + return v; +} +#ifdef STBTT_STREAM_TYPE + static stbtt__buf stbtt__new_buf(STBTT_STREAM_TYPE s, size_t size) +#else + static stbtt__buf stbtt__new_buf(const void * p, size_t size) +#endif +{ + stbtt__buf r; + STBTT_assert(size < 0x40000000); +#ifdef STBTT_STREAM_TYPE + r.data = s; + r.offset = 0; +#else + r.data = (stbtt_uint8 *)p; +#endif + r.size = (int)size; + r.cursor = 0; + return r; +} + +#define stbtt__buf_get16(b) stbtt__buf_get((b), 2) +#define stbtt__buf_get32(b) stbtt__buf_get((b), 4) + +static stbtt__buf stbtt__buf_range(const stbtt__buf * b, int o, int s) +{ + stbtt__buf r = stbtt__new_buf(NULL, 0); + if(o < 0 || s < 0 || o > b->size || s > b->size - o) return r; +#ifdef STBTT_STREAM_TYPE + r.data = b->data; + r.offset = b->offset + o; +#else + r.data = b->data + o; +#endif + r.size = s; + return r; +} + +static stbtt__buf stbtt__cff_get_index(stbtt__buf * b) +{ + int count, start, offsize; + start = b->cursor; + count = stbtt__buf_get16(b); + if(count) { + offsize = stbtt__buf_get8(b); + STBTT_assert(offsize >= 1 && offsize <= 4); + stbtt__buf_skip(b, offsize * count); + stbtt__buf_skip(b, stbtt__buf_get(b, offsize) - 1); + } + return stbtt__buf_range(b, start, b->cursor - start); +} + +static stbtt_uint32 stbtt__cff_int(stbtt__buf * b) +{ + int b0 = stbtt__buf_get8(b); + if(b0 >= 32 && b0 <= 246) return b0 - 139; + else if(b0 >= 247 && b0 <= 250) return (b0 - 247) * 256 + stbtt__buf_get8(b) + 108; + else if(b0 >= 251 && b0 <= 254) return -(b0 - 251) * 256 - stbtt__buf_get8(b) - 108; + else if(b0 == 28) return stbtt__buf_get16(b); + else if(b0 == 29) return stbtt__buf_get32(b); + STBTT_assert(0); + return 0; +} + +static void stbtt__cff_skip_operand(stbtt__buf * b) +{ + int v, b0 = stbtt__buf_peek8(b); + STBTT_assert(b0 >= 28); + if(b0 == 30) { + stbtt__buf_skip(b, 1); + while(b->cursor < b->size) { + v = stbtt__buf_get8(b); + if((v & 0xF) == 0xF || (v >> 4) == 0xF) + break; + } + } + else { + stbtt__cff_int(b); + } +} + +static stbtt__buf stbtt__dict_get(stbtt__buf * b, int key) +{ + stbtt__buf_seek(b, 0); + while(b->cursor < b->size) { + int start = b->cursor, end, op; + while(stbtt__buf_peek8(b) >= 28) + stbtt__cff_skip_operand(b); + end = b->cursor; + op = stbtt__buf_get8(b); + if(op == 12) op = stbtt__buf_get8(b) | 0x100; + if(op == key) return stbtt__buf_range(b, start, end - start); + } + return stbtt__buf_range(b, 0, 0); +} + +static void stbtt__dict_get_ints(stbtt__buf * b, int key, int outcount, stbtt_uint32 * out) +{ + int i; + stbtt__buf operands = stbtt__dict_get(b, key); + for(i = 0; i < outcount && operands.cursor < operands.size; i++) + out[i] = stbtt__cff_int(&operands); +} + +static int stbtt__cff_index_count(stbtt__buf * b) +{ + stbtt__buf_seek(b, 0); + return stbtt__buf_get16(b); +} + +static stbtt__buf stbtt__cff_index_get(stbtt__buf b, int i) +{ + int count, offsize, start, end; + stbtt__buf_seek(&b, 0); + count = stbtt__buf_get16(&b); + offsize = stbtt__buf_get8(&b); + STBTT_assert(i >= 0 && i < count); + STBTT_assert(offsize >= 1 && offsize <= 4); + stbtt__buf_skip(&b, i * offsize); + start = stbtt__buf_get(&b, offsize); + end = stbtt__buf_get(&b, offsize); + return stbtt__buf_range(&b, 2 + (count + 1) * offsize + start, end - start); +} + +////////////////////////////////////////////////////////////////////////// +// +// accessors to parse data from file +// + +// on platforms that don't allow misaligned reads, if we want to allow +// truetype fonts that aren't padded to alignment, define ALLOW_UNALIGNED_TRUETYPE + +#ifdef STBTT_STREAM_TYPE +static stbtt_uint8 ttBYTE(STBTT_STREAM_TYPE s, stbtt_uint32 offset) +{ + STBTT_STREAM_SEEK(s, offset); + stbtt_uint8 r; + STBTT_STREAM_READ(s, &r, 1); + return r; +} +#define ttCHAR(s, offset) ((stbtt_int8)ttBYTE(s,offset)) +static stbtt_uint16 ttUSHORT(STBTT_STREAM_TYPE s, stbtt_uint32 offset) +{ + STBTT_STREAM_SEEK(s, offset); + stbtt_uint8 r[2]; + STBTT_STREAM_READ(s, &r, 2); + return r[0] * 256 + r[1]; +} +static stbtt_int16 ttSHORT(STBTT_STREAM_TYPE s, stbtt_uint32 offset) +{ + STBTT_STREAM_SEEK(s, offset); + stbtt_uint8 r[2]; + STBTT_STREAM_READ(s, &r, 2); + return r[0] * 256 + r[1]; +} +static stbtt_uint32 ttULONG(STBTT_STREAM_TYPE s, stbtt_uint32 offset) +{ + STBTT_STREAM_SEEK(s, offset); + stbtt_uint8 r[4]; + STBTT_STREAM_READ(s, &r, 4); + return (r[0] << 24) + (r[1] << 16) + (r[2] << 8) + r[3]; +} +static stbtt_int32 ttLONG(STBTT_STREAM_TYPE s, stbtt_uint32 offset) +{ + STBTT_STREAM_SEEK(s, offset); + stbtt_uint8 r[4]; + STBTT_STREAM_READ(s, &r, 4); + return (r[0] << 24) + (r[1] << 16) + (r[2] << 8) + r[3]; +} +#else +#define ttBYTE(p, offset) (* (stbtt_uint8 *) (p+offset)) +#define ttCHAR(p, offset) (* (stbtt_int8 *) (p+offset)) +static stbtt_uint16 ttUSHORT(const stbtt_uint8 * p, stbtt_uint32 offset) +{ + return p[offset + 0] * 256 + p[offset + 1]; +} +static stbtt_int16 ttSHORT(const stbtt_uint8 * p, stbtt_uint32 offset) +{ + return p[offset + 0] * 256 + p[offset + 1]; +} +static stbtt_uint32 ttULONG(const stbtt_uint8 * p, stbtt_uint32 offset) +{ + return (p[offset + 0] << 24) + (p[offset + 1] << 16) + (p[offset + 2] << 8) + p[offset + 3]; +} +static stbtt_int32 ttLONG(const stbtt_uint8 * p, stbtt_uint32 offset) +{ + return (p[offset + 0] << 24) + (p[offset + 1] << 16) + (p[offset + 2] << 8) + p[offset + 3]; +} +#endif +#define ttFixed(p, offset) ttLONG(p, offset) + +#define stbtt_tag4(p,c0,c1,c2,c3) ((p)[0] == (c0) && (p)[1] == (c1) && (p)[2] == (c2) && (p)[3] == (c3)) +#define stbtt_tag(p,str) stbtt_tag4(p,str[0],str[1],str[2],str[3]) +#ifdef STBTT_STREAM_TYPE + static int stbtt__isfont(STBTT_STREAM_TYPE stream, stbtt_uint32 offs) +#else + static int stbtt__isfont(stbtt_uint8 * font, stbtt_uint32 offs) +#endif +{ +#ifdef STBTT_STREAM_TYPE + stbtt_uint8 font[4]; + STBTT_STREAM_SEEK(stream, offs); + STBTT_STREAM_READ(stream, font, 4); +#else + font += offs; +#endif + // check the version number + if(stbtt_tag4(font, '1', 0, 0, 0)) return 1; // TrueType 1 + if(stbtt_tag(font, "typ1")) return 1; // TrueType with type 1 font -- we don't support this! + if(stbtt_tag(font, "OTTO")) return 1; // OpenType with CFF + if(stbtt_tag4(font, 0, 1, 0, 0)) return 1; // OpenType 1.0 + if(stbtt_tag(font, "true")) return 1; // Apple specification for TrueType fonts + return 0; +} + +// @OPTIMIZE: binary search +#ifdef STBTT_STREAM_TYPE + static stbtt_uint32 stbtt__find_table(STBTT_STREAM_TYPE data, stbtt_uint32 fontstart, const char * tag) +#else + static stbtt_uint32 stbtt__find_table(stbtt_uint8 * data, stbtt_uint32 fontstart, const char * tag) +#endif +{ + stbtt_int32 num_tables = ttUSHORT(data, fontstart + 4); + stbtt_uint32 tabledir = fontstart + 12; + stbtt_int32 i; + for(i = 0; i < num_tables; ++i) { + stbtt_uint32 loc = tabledir + 16 * i; +#ifdef STBTT_STREAM_TYPE + stbtt_uint8 buf[4]; + STBTT_STREAM_SEEK(data, loc + 0); + STBTT_STREAM_READ(data, buf, 4); + if(stbtt_tag(buf, tag)) + return ttULONG(data, loc + 8); +#else + if(stbtt_tag(data + loc + 0, tag)) + return ttULONG(data, loc + 8); +#endif + } + return 0; +} +#ifdef STBTT_STREAM_TYPE + static int stbtt_GetFontOffsetForIndex_internal(STBTT_STREAM_TYPE font_collection, int index) +#else + static int stbtt_GetFontOffsetForIndex_internal(unsigned char * font_collection, int index) +#endif +{ + // if it's just a font, there's only one valid index + if(stbtt__isfont(font_collection, 0)) + return index == 0 ? 0 : -1; + + // check if it's a TTC +#ifdef STBTT_STREAM_TYPE + stbtt_uint8 buf[4]; + STBTT_STREAM_SEEK(font_collection, 0); + STBTT_STREAM_READ(font_collection, buf, 4); + if(stbtt_tag(buf, "ttcf")) { +#else + if(stbtt_tag(font_collection, "ttcf")) { +#endif + // version 1? + if(ttULONG(font_collection, 4) == 0x00010000 || ttULONG(font_collection, 4) == 0x00020000) { + stbtt_int32 n = ttLONG(font_collection, 8); + if(index >= n) + return -1; + return ttULONG(font_collection, 12 + index * 4); + } + } + return -1; +} +#ifdef STBTT_STREAM_TYPE + static int stbtt_GetNumberOfFonts_internal(STBTT_STREAM_TYPE font_collection) +#else + static int stbtt_GetNumberOfFonts_internal(unsigned char * font_collection) +#endif +{ + // if it's just a font, there's only one valid font + if(stbtt__isfont(font_collection, 0)) + return 1; + + // check if it's a TTC +#ifdef STBTT_STREAM_TYPE + stbtt_uint8 buf[4]; + STBTT_STREAM_SEEK(font_collection, 0); + STBTT_STREAM_READ(font_collection, buf, 4); + if(stbtt_tag(buf, "ttcf")) { +#else + if(stbtt_tag(font_collection, "ttcf")) { +#endif + // version 1? + if(ttULONG(font_collection, 4) == 0x00010000 || ttULONG(font_collection, 4) == 0x00020000) { + return ttLONG(font_collection, 8); + } + } + return 0; +} + +static stbtt__buf stbtt__get_subrs(stbtt__buf cff, stbtt__buf fontdict) +{ + stbtt_uint32 subrsoff = 0, private_loc[2] = { 0, 0 }; + stbtt__buf pdict; + stbtt__dict_get_ints(&fontdict, 18, 2, private_loc); + if(!private_loc[1] || !private_loc[0]) return stbtt__new_buf(NULL, 0); + pdict = stbtt__buf_range(&cff, private_loc[1], private_loc[0]); + stbtt__dict_get_ints(&pdict, 19, 1, &subrsoff); + if(!subrsoff) return stbtt__new_buf(NULL, 0); + stbtt__buf_seek(&cff, private_loc[1] + subrsoff); + return stbtt__cff_get_index(&cff); +} + +// since most people won't use this, find this table the first time it's needed +static int stbtt__get_svg(stbtt_fontinfo * info) +{ + stbtt_uint32 t; + if(info->svg < 0) { + t = stbtt__find_table(info->data, info->fontstart, "SVG "); + if(t) { + stbtt_uint32 offset = ttULONG(info->data, t + 2); + info->svg = t + offset; + } + else { + info->svg = 0; + } + } + return info->svg; +} +#ifdef STBTT_STREAM_TYPE + static int stbtt_InitFont_internal(stbtt_fontinfo * info, STBTT_STREAM_TYPE data, int fontstart) +#else + static int stbtt_InitFont_internal(stbtt_fontinfo * info, unsigned char * data, int fontstart) +#endif +{ + stbtt_uint32 cmap, t; + stbtt_int32 i, numTables; + + info->data = data; + info->fontstart = fontstart; + info->cff = stbtt__new_buf(NULL, 0); + + cmap = stbtt__find_table(data, fontstart, "cmap"); // required + info->loca = stbtt__find_table(data, fontstart, "loca"); // required + info->head = stbtt__find_table(data, fontstart, "head"); // required + info->glyf = stbtt__find_table(data, fontstart, "glyf"); // required + info->hhea = stbtt__find_table(data, fontstart, "hhea"); // required + info->hmtx = stbtt__find_table(data, fontstart, "hmtx"); // required + info->kern = stbtt__find_table(data, fontstart, "kern"); // not required + info->gpos = stbtt__find_table(data, fontstart, "GPOS"); // not required + + if(!cmap || !info->head || !info->hhea || !info->hmtx) + return 0; + if(info->glyf) { + // required for truetype + if(!info->loca) return 0; + } + else { + // initialization for CFF / Type2 fonts (OTF) + stbtt__buf b, topdict, topdictidx; + stbtt_uint32 cstype = 2, charstrings = 0, fdarrayoff = 0, fdselectoff = 0; + stbtt_uint32 cff; + + cff = stbtt__find_table(data, fontstart, "CFF "); + if(!cff) return 0; + + info->fontdicts = stbtt__new_buf(NULL, 0); + info->fdselect = stbtt__new_buf(NULL, 0); + + // @TODO this should use size from table (not 512MB) + +#ifdef STBTT_STREAM_TYPE + info->cff = stbtt__new_buf(info->data, 512 * 1024 * 1024); + info->cff.offset = cff; +#else + info->cff = stbtt__new_buf(info->data + cff, 512 * 1024 * 1024); +#endif + b = info->cff; + + // read the header + stbtt__buf_skip(&b, 2); + stbtt__buf_seek(&b, stbtt__buf_get8(&b)); // hdrsize + + // @TODO the name INDEX could list multiple fonts, + // but we just use the first one. + stbtt__cff_get_index(&b); // name INDEX + topdictidx = stbtt__cff_get_index(&b); + topdict = stbtt__cff_index_get(topdictidx, 0); + stbtt__cff_get_index(&b); // string INDEX + info->gsubrs = stbtt__cff_get_index(&b); + + stbtt__dict_get_ints(&topdict, 17, 1, &charstrings); + stbtt__dict_get_ints(&topdict, 0x100 | 6, 1, &cstype); + stbtt__dict_get_ints(&topdict, 0x100 | 36, 1, &fdarrayoff); + stbtt__dict_get_ints(&topdict, 0x100 | 37, 1, &fdselectoff); + info->subrs = stbtt__get_subrs(b, topdict); + + // we only support Type 2 charstrings + if(cstype != 2) return 0; + if(charstrings == 0) return 0; + + if(fdarrayoff) { + // looks like a CID font + if(!fdselectoff) return 0; + stbtt__buf_seek(&b, fdarrayoff); + info->fontdicts = stbtt__cff_get_index(&b); + info->fdselect = stbtt__buf_range(&b, fdselectoff, b.size - fdselectoff); + } + + stbtt__buf_seek(&b, charstrings); + info->charstrings = stbtt__cff_get_index(&b); + } + + t = stbtt__find_table(data, fontstart, "maxp"); + if(t) + info->numGlyphs = ttUSHORT(data, t + 4); + else + info->numGlyphs = 0xffff; + + info->svg = -1; + + // find a cmap encoding table we understand *now* to avoid searching + // later. (todo: could make this installable) + // the same regardless of glyph. + numTables = ttUSHORT(data, cmap + 2); + info->index_map = 0; + for(i = 0; i < numTables; ++i) { + stbtt_uint32 encoding_record = cmap + 4 + 8 * i; + // find an encoding we understand: + switch(ttUSHORT(data, encoding_record)) { + case STBTT_PLATFORM_ID_MICROSOFT: + switch(ttUSHORT(data, encoding_record + 2)) { + case STBTT_MS_EID_UNICODE_BMP: + case STBTT_MS_EID_UNICODE_FULL: + // MS/Unicode + info->index_map = cmap + ttULONG(data, encoding_record + 4); + break; + } + break; + case STBTT_PLATFORM_ID_UNICODE: + // Mac/iOS has these + // all the encodingIDs are unicode, so we don't bother to check it + info->index_map = cmap + ttULONG(data, encoding_record + 4); + break; + } + } + if(info->index_map == 0) + return 0; + + info->indexToLocFormat = ttUSHORT(data, info->head + 50); + return 1; +} + +STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo * info, int unicode_codepoint) +{ +#ifdef STBTT_STREAM_TYPE + STBTT_STREAM_TYPE data = info->data; +#else + stbtt_uint8 * data = info->data; +#endif + stbtt_uint32 index_map = info->index_map; + + stbtt_uint16 format = ttUSHORT(data, index_map + 0); + if(format == 0) { // apple byte encoding + stbtt_int32 bytes = ttUSHORT(data, index_map + 2); + if(unicode_codepoint < bytes - 6) + return ttBYTE(data, index_map + 6 + unicode_codepoint); + return 0; + } + else if(format == 6) { + stbtt_uint32 first = ttUSHORT(data, index_map + 6); + stbtt_uint32 count = ttUSHORT(data, index_map + 8); + if((stbtt_uint32)unicode_codepoint >= first && (stbtt_uint32)unicode_codepoint < first + count) + return ttUSHORT(data, index_map + 10 + (unicode_codepoint - first) * 2); + return 0; + } + else if(format == 2) { + STBTT_assert(0); // @TODO: high-byte mapping for japanese/chinese/korean + return 0; + } + else if(format == 4) { // standard mapping for windows fonts: binary search collection of ranges + stbtt_uint16 segcount = ttUSHORT(data, index_map + 6) >> 1; + stbtt_uint16 searchRange = ttUSHORT(data, index_map + 8) >> 1; + stbtt_uint16 entrySelector = ttUSHORT(data, index_map + 10); + stbtt_uint16 rangeShift = ttUSHORT(data, index_map + 12) >> 1; + + // do a binary search of the segments + stbtt_uint32 endCount = index_map + 14; + stbtt_uint32 search = endCount; + + if(unicode_codepoint > 0xffff) + return 0; + + // they lie from endCount .. endCount + segCount + // but searchRange is the nearest power of two, so... + if(unicode_codepoint >= ttUSHORT(data, search + rangeShift * 2)) + search += rangeShift * 2; + + // now decrement to bias correctly to find smallest + search -= 2; + while(entrySelector) { + stbtt_uint16 end; + searchRange >>= 1; + end = ttUSHORT(data, search + searchRange * 2); + if(unicode_codepoint > end) + search += searchRange * 2; + --entrySelector; + } + search += 2; + + { + stbtt_uint16 offset, start, last; + stbtt_uint16 item = (stbtt_uint16)((search - endCount) >> 1); + + start = ttUSHORT(data, index_map + 14 + segcount * 2 + 2 + 2 * item); + last = ttUSHORT(data, endCount + 2 * item); + if(unicode_codepoint < start || unicode_codepoint > last) + return 0; + + offset = ttUSHORT(data, index_map + 14 + segcount * 6 + 2 + 2 * item); + if(offset == 0) + return (stbtt_uint16)(unicode_codepoint + ttSHORT(data, index_map + 14 + segcount * 4 + 2 + 2 * item)); + + return ttUSHORT(data, offset + (unicode_codepoint - start) * 2 + index_map + 14 + segcount * 6 + 2 + 2 * item); + } + } + else if(format == 12 || format == 13) { + stbtt_uint32 ngroups = ttULONG(data, index_map + 12); + stbtt_int32 low, high; + low = 0; + high = (stbtt_int32)ngroups; + // Binary search the right group. + while(low < high) { + stbtt_int32 mid = low + ((high - low) >> 1); // rounds down, so low <= mid < high + stbtt_uint32 start_char = ttULONG(data, index_map + 16 + mid * 12); + stbtt_uint32 end_char = ttULONG(data, index_map + 16 + mid * 12 + 4); + if((stbtt_uint32)unicode_codepoint < start_char) + high = mid; + else if((stbtt_uint32)unicode_codepoint > end_char) + low = mid + 1; + else { + stbtt_uint32 start_glyph = ttULONG(data, index_map + 16 + mid * 12 + 8); + if(format == 12) + return start_glyph + unicode_codepoint - start_char; + else // format == 13 + return start_glyph; + } + } + return 0; // not found + } + // @TODO + STBTT_assert(0); + return 0; +} + +STBTT_DEF int stbtt_GetCodepointShape(const stbtt_fontinfo * info, int unicode_codepoint, stbtt_vertex * *vertices) +{ + return stbtt_GetGlyphShape(info, stbtt_FindGlyphIndex(info, unicode_codepoint), vertices); +} + +static void stbtt_setvertex(stbtt_vertex * v, stbtt_uint8 type, stbtt_int32 x, stbtt_int32 y, stbtt_int32 cx, + stbtt_int32 cy) +{ + v->type = type; + v->x = (stbtt_int16)x; + v->y = (stbtt_int16)y; + v->cx = (stbtt_int16)cx; + v->cy = (stbtt_int16)cy; +} + +static int stbtt__GetGlyfOffset(const stbtt_fontinfo * info, int glyph_index) +{ + int g1, g2; + + STBTT_assert(!info->cff.size); + + if(glyph_index >= info->numGlyphs) return -1; // glyph index out of range + if(info->indexToLocFormat >= 2) return -1; // unknown index->glyph map format + + if(info->indexToLocFormat == 0) { + g1 = info->glyf + ttUSHORT(info->data, info->loca + glyph_index * 2) * 2; + g2 = info->glyf + ttUSHORT(info->data, info->loca + glyph_index * 2 + 2) * 2; + } + else { + g1 = info->glyf + ttULONG(info->data, info->loca + glyph_index * 4); + g2 = info->glyf + ttULONG(info->data, info->loca + glyph_index * 4 + 4); + } + + return g1 == g2 ? -1 : g1; // if length is 0, return -1 +} + +static int stbtt__GetGlyphInfoT2(const stbtt_fontinfo * info, int glyph_index, int * x0, int * y0, int * x1, int * y1); + +STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo * info, int glyph_index, int * x0, int * y0, int * x1, int * y1) +{ + if(info->cff.size) { + stbtt__GetGlyphInfoT2(info, glyph_index, x0, y0, x1, y1); + } + else { + int g = stbtt__GetGlyfOffset(info, glyph_index); + if(g < 0) return 0; + + if(x0) *x0 = ttSHORT(info->data, g + 2); + if(y0) *y0 = ttSHORT(info->data, g + 4); + if(x1) *x1 = ttSHORT(info->data, g + 6); + if(y1) *y1 = ttSHORT(info->data, g + 8); + } + return 1; +} + +STBTT_DEF int stbtt_GetCodepointBox(const stbtt_fontinfo * info, int codepoint, int * x0, int * y0, int * x1, int * y1) +{ + return stbtt_GetGlyphBox(info, stbtt_FindGlyphIndex(info, codepoint), x0, y0, x1, y1); +} + +STBTT_DEF int stbtt_IsGlyphEmpty(const stbtt_fontinfo * info, int glyph_index) +{ + stbtt_int16 numberOfContours; + int g; + if(info->cff.size) + return stbtt__GetGlyphInfoT2(info, glyph_index, NULL, NULL, NULL, NULL) == 0; + g = stbtt__GetGlyfOffset(info, glyph_index); + if(g < 0) return 1; + numberOfContours = ttSHORT(info->data, g); + return numberOfContours == 0; +} + +static int stbtt__close_shape(stbtt_vertex * vertices, int num_vertices, int was_off, int start_off, + stbtt_int32 sx, stbtt_int32 sy, stbtt_int32 scx, stbtt_int32 scy, stbtt_int32 cx, stbtt_int32 cy) +{ + if(start_off) { + if(was_off) + stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx + scx) >> 1, (cy + scy) >> 1, cx, cy); + stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, sx, sy, scx, scy); + } + else { + if(was_off) + stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, sx, sy, cx, cy); + else + stbtt_setvertex(&vertices[num_vertices++], STBTT_vline, sx, sy, 0, 0); + } + return num_vertices; +} + +static int stbtt__GetGlyphShapeTT(const stbtt_fontinfo * info, int glyph_index, stbtt_vertex * *pvertices) +{ + stbtt_int16 numberOfContours; + stbtt_uint32 endPtsOfContours; +#ifdef STBTT_STREAM_TYPE + STBTT_STREAM_TYPE data = info->data; +#else + stbtt_uint8 * data = info->data; +#endif + stbtt_vertex * vertices = 0; + int num_vertices = 0; + int g = stbtt__GetGlyfOffset(info, glyph_index); + + *pvertices = NULL; + + if(g < 0) return 0; + + numberOfContours = ttSHORT(data, g); + + if(numberOfContours > 0) { + stbtt_uint8 flags = 0, flagcount; + stbtt_int32 ins, i, j = 0, m, n, next_move, was_off = 0, off, start_off = 0; + stbtt_int32 x, y, cx, cy, sx, sy, scx, scy; + stbtt_uint32 points; + endPtsOfContours = (g + 10); + ins = ttUSHORT(data, g + 10 + numberOfContours * 2); + points = g + 10 + numberOfContours * 2 + 2 + ins; + + n = 1 + ttUSHORT(data, endPtsOfContours + numberOfContours * 2 - 2); + + m = n + 2 * numberOfContours; // a loose bound on how many vertices we might need + vertices = (stbtt_vertex *)STBTT_malloc(m * sizeof(vertices[0]), info->userdata); + if(vertices == 0) + return 0; + + next_move = 0; + flagcount = 0; + + // in first pass, we load uninterpreted data into the allocated array + // above, shifted to the end of the array so we won't overwrite it when + // we create our final data starting from the front + + off = m - n; // starting offset for uninterpreted data, regardless of how m ends up being calculated + + // first load flags + + for(i = 0; i < n; ++i) { + if(flagcount == 0) { + flags = ttBYTE(data, points++); + if(flags & 8) + flagcount = ttBYTE(data, points++); + } + else + --flagcount; + vertices[off + i].type = flags; + } + + // now load x coordinates + x = 0; + for(i = 0; i < n; ++i) { + flags = vertices[off + i].type; + if(flags & 2) { + stbtt_int16 dx = ttBYTE(data, points++); + x += (flags & 16) ? dx : -dx; // ??? + } + else { + if(!(flags & 16)) { + x = x + (stbtt_int16)(ttBYTE(data, points) * 256 + ttBYTE(data, points + 1)); + points += 2; + } + } + vertices[off + i].x = (stbtt_int16)x; + } + + // now load y coordinates + y = 0; + for(i = 0; i < n; ++i) { + flags = vertices[off + i].type; + if(flags & 4) { + stbtt_int16 dy = ttBYTE(data, points++); + y += (flags & 32) ? dy : -dy; // ??? + } + else { + if(!(flags & 32)) { + y = y + (stbtt_int16)(ttBYTE(data, points) * 256 + ttBYTE(data, points + 1)); + points += 2; + } + } + vertices[off + i].y = (stbtt_int16)y; + } + + // now convert them to our format + num_vertices = 0; + sx = sy = cx = cy = scx = scy = 0; + for(i = 0; i < n; ++i) { + flags = vertices[off + i].type; + x = (stbtt_int16)vertices[off + i].x; + y = (stbtt_int16)vertices[off + i].y; + + if(next_move == i) { + if(i != 0) + num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx, sy, scx, scy, cx, cy); + + // now start the new one + start_off = !(flags & 1); + if(start_off) { + // if we start off with an off-curve point, then when we need to find a point on the curve + // where we can start, and we need to save some state for when we wraparound. + scx = x; + scy = y; + if(!(vertices[off + i + 1].type & 1)) { + // next point is also a curve point, so interpolate an on-point curve + sx = (x + (stbtt_int32)vertices[off + i + 1].x) >> 1; + sy = (y + (stbtt_int32)vertices[off + i + 1].y) >> 1; + } + else { + // otherwise just use the next point as our start point + sx = (stbtt_int32)vertices[off + i + 1].x; + sy = (stbtt_int32)vertices[off + i + 1].y; + ++i; // we're using point i+1 as the starting point, so skip it + } + } + else { + sx = x; + sy = y; + } + stbtt_setvertex(&vertices[num_vertices++], STBTT_vmove, sx, sy, 0, 0); + was_off = 0; + next_move = 1 + ttUSHORT(data, endPtsOfContours + j * 2); + ++j; + } + else { + if(!(flags & 1)) { // if it's a curve + if(was_off) // two off-curve control points in a row means interpolate an on-curve midpoint + stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx + x) >> 1, (cy + y) >> 1, cx, cy); + cx = x; + cy = y; + was_off = 1; + } + else { + if(was_off) + stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, x, y, cx, cy); + else + stbtt_setvertex(&vertices[num_vertices++], STBTT_vline, x, y, 0, 0); + was_off = 0; + } + } + } + num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx, sy, scx, scy, cx, cy); + } + else if(numberOfContours < 0) { + // Compound shapes. + int more = 1; + stbtt_uint32 comp = g + 10; + num_vertices = 0; + vertices = 0; + while(more) { + stbtt_uint16 flags, gidx; + int comp_num_verts = 0, i; + stbtt_vertex * comp_verts = 0, * tmp = 0; + float mtx[6] = { 1, 0, 0, 1, 0, 0 }, m, n; + + flags = ttSHORT(data, comp); + comp += 2; + gidx = ttSHORT(data, comp); + comp += 2; + + if(flags & 2) { // XY values + if(flags & 1) { // shorts + mtx[4] = ttSHORT(data, comp); + comp += 2; + mtx[5] = ttSHORT(data, comp); + comp += 2; + } + else { + mtx[4] = ttCHAR(data, comp); + comp += 1; + mtx[5] = ttCHAR(data, comp); + comp += 1; + } + } + else { + // @TODO handle matching point + STBTT_assert(0); + } + if(flags & (1 << 3)) { // WE_HAVE_A_SCALE + mtx[0] = mtx[3] = ttSHORT(data, comp) / 16384.0f; + comp += 2; + mtx[1] = mtx[2] = 0; + } + else if(flags & (1 << 6)) { // WE_HAVE_AN_X_AND_YSCALE + mtx[0] = ttSHORT(data, comp) / 16384.0f; + comp += 2; + mtx[1] = mtx[2] = 0; + mtx[3] = ttSHORT(data, comp) / 16384.0f; + comp += 2; + } + else if(flags & (1 << 7)) { // WE_HAVE_A_TWO_BY_TWO + mtx[0] = ttSHORT(data, comp) / 16384.0f; + comp += 2; + mtx[1] = ttSHORT(data, comp) / 16384.0f; + comp += 2; + mtx[2] = ttSHORT(data, comp) / 16384.0f; + comp += 2; + mtx[3] = ttSHORT(data, comp) / 16384.0f; + comp += 2; + } + + // Find transformation scales. + m = (float)STBTT_sqrt(mtx[0] * mtx[0] + mtx[1] * mtx[1]); + n = (float)STBTT_sqrt(mtx[2] * mtx[2] + mtx[3] * mtx[3]); + + // Get indexed glyph. + comp_num_verts = stbtt_GetGlyphShape(info, gidx, &comp_verts); + if(comp_num_verts > 0) { + // Transform vertices. + for(i = 0; i < comp_num_verts; ++i) { + stbtt_vertex * v = &comp_verts[i]; + stbtt_vertex_type x, y; + x = v->x; + y = v->y; + v->x = (stbtt_vertex_type)(m * (mtx[0] * x + mtx[2] * y + mtx[4])); + v->y = (stbtt_vertex_type)(n * (mtx[1] * x + mtx[3] * y + mtx[5])); + x = v->cx; + y = v->cy; + v->cx = (stbtt_vertex_type)(m * (mtx[0] * x + mtx[2] * y + mtx[4])); + v->cy = (stbtt_vertex_type)(n * (mtx[1] * x + mtx[3] * y + mtx[5])); + } + // Append vertices. + tmp = (stbtt_vertex *)STBTT_malloc((num_vertices + comp_num_verts) * sizeof(stbtt_vertex), info->userdata); + if(!tmp) { + if(vertices) STBTT_free(vertices, info->userdata); + if(comp_verts) STBTT_free(comp_verts, info->userdata); + return 0; + } + if(num_vertices > 0 && vertices) STBTT_memcpy(tmp, vertices, num_vertices * sizeof(stbtt_vertex)); + STBTT_memcpy(tmp + num_vertices, comp_verts, comp_num_verts * sizeof(stbtt_vertex)); + if(vertices) STBTT_free(vertices, info->userdata); + vertices = tmp; + STBTT_free(comp_verts, info->userdata); + num_vertices += comp_num_verts; + } + // More components ? + more = flags & (1 << 5); + } + } + else { + // numberOfCounters == 0, do nothing + } + + *pvertices = vertices; + return num_vertices; +} + +typedef struct { + int bounds; + int started; + float first_x, first_y; + float x, y; + stbtt_int32 min_x, max_x, min_y, max_y; + + stbtt_vertex * pvertices; + int num_vertices; +} stbtt__csctx; + +#define STBTT__CSCTX_INIT(bounds) {bounds,0, 0,0, 0,0, 0,0,0,0, NULL, 0} + +static void stbtt__track_vertex(stbtt__csctx * c, stbtt_int32 x, stbtt_int32 y) +{ + if(x > c->max_x || !c->started) c->max_x = x; + if(y > c->max_y || !c->started) c->max_y = y; + if(x < c->min_x || !c->started) c->min_x = x; + if(y < c->min_y || !c->started) c->min_y = y; + c->started = 1; +} + +static void stbtt__csctx_v(stbtt__csctx * c, stbtt_uint8 type, stbtt_int32 x, stbtt_int32 y, stbtt_int32 cx, + stbtt_int32 cy, stbtt_int32 cx1, stbtt_int32 cy1) +{ + if(c->bounds) { + stbtt__track_vertex(c, x, y); + if(type == STBTT_vcubic) { + stbtt__track_vertex(c, cx, cy); + stbtt__track_vertex(c, cx1, cy1); + } + } + else { + stbtt_setvertex(&c->pvertices[c->num_vertices], type, x, y, cx, cy); + c->pvertices[c->num_vertices].cx1 = (stbtt_int16)cx1; + c->pvertices[c->num_vertices].cy1 = (stbtt_int16)cy1; + } + c->num_vertices++; +} + +static void stbtt__csctx_close_shape(stbtt__csctx * ctx) +{ + if(ctx->first_x != ctx->x || ctx->first_y != ctx->y) + stbtt__csctx_v(ctx, STBTT_vline, (int)ctx->first_x, (int)ctx->first_y, 0, 0, 0, 0); +} + +static void stbtt__csctx_rmove_to(stbtt__csctx * ctx, float dx, float dy) +{ + stbtt__csctx_close_shape(ctx); + ctx->first_x = ctx->x = ctx->x + dx; + ctx->first_y = ctx->y = ctx->y + dy; + stbtt__csctx_v(ctx, STBTT_vmove, (int)ctx->x, (int)ctx->y, 0, 0, 0, 0); +} + +static void stbtt__csctx_rline_to(stbtt__csctx * ctx, float dx, float dy) +{ + ctx->x += dx; + ctx->y += dy; + stbtt__csctx_v(ctx, STBTT_vline, (int)ctx->x, (int)ctx->y, 0, 0, 0, 0); +} + +static void stbtt__csctx_rccurve_to(stbtt__csctx * ctx, float dx1, float dy1, float dx2, float dy2, float dx3, + float dy3) +{ + float cx1 = ctx->x + dx1; + float cy1 = ctx->y + dy1; + float cx2 = cx1 + dx2; + float cy2 = cy1 + dy2; + ctx->x = cx2 + dx3; + ctx->y = cy2 + dy3; + stbtt__csctx_v(ctx, STBTT_vcubic, (int)ctx->x, (int)ctx->y, (int)cx1, (int)cy1, (int)cx2, (int)cy2); +} + +static stbtt__buf stbtt__get_subr(stbtt__buf idx, int n) +{ + int count = stbtt__cff_index_count(&idx); + int bias = 107; + if(count >= 33900) + bias = 32768; + else if(count >= 1240) + bias = 1131; + n += bias; + if(n < 0 || n >= count) + return stbtt__new_buf(NULL, 0); + return stbtt__cff_index_get(idx, n); +} + +static stbtt__buf stbtt__cid_get_glyph_subrs(const stbtt_fontinfo * info, int glyph_index) +{ + stbtt__buf fdselect = info->fdselect; + int nranges, start, end, v, fmt, fdselector = -1, i; + + stbtt__buf_seek(&fdselect, 0); + fmt = stbtt__buf_get8(&fdselect); + if(fmt == 0) { + // untested + stbtt__buf_skip(&fdselect, glyph_index); + fdselector = stbtt__buf_get8(&fdselect); + } + else if(fmt == 3) { + nranges = stbtt__buf_get16(&fdselect); + start = stbtt__buf_get16(&fdselect); + for(i = 0; i < nranges; i++) { + v = stbtt__buf_get8(&fdselect); + end = stbtt__buf_get16(&fdselect); + if(glyph_index >= start && glyph_index < end) { + fdselector = v; + break; + } + start = end; + } + } + if(fdselector == -1) stbtt__new_buf(NULL, 0); + return stbtt__get_subrs(info->cff, stbtt__cff_index_get(info->fontdicts, fdselector)); +} + +static int stbtt__run_charstring(const stbtt_fontinfo * info, int glyph_index, stbtt__csctx * c) +{ + int in_header = 1, maskbits = 0, subr_stack_height = 0, sp = 0, v, i, b0; + int has_subrs = 0, clear_stack; + float s[48]; + stbtt__buf subr_stack[10], subrs = info->subrs, b; + float f; + +#define STBTT__CSERR(s) (0) + + // this currently ignores the initial width value, which isn't needed if we have hmtx + b = stbtt__cff_index_get(info->charstrings, glyph_index); + while(b.cursor < b.size) { + i = 0; + clear_stack = 1; + b0 = stbtt__buf_get8(&b); + switch(b0) { + // @TODO implement hinting + case 0x13: // hintmask + case 0x14: // cntrmask + if(in_header) + maskbits += (sp / 2); // implicit "vstem" + in_header = 0; + stbtt__buf_skip(&b, (maskbits + 7) / 8); + break; + + case 0x01: // hstem + case 0x03: // vstem + case 0x12: // hstemhm + case 0x17: // vstemhm + maskbits += (sp / 2); + break; + + case 0x15: // rmoveto + in_header = 0; + if(sp < 2) return STBTT__CSERR("rmoveto stack"); + stbtt__csctx_rmove_to(c, s[sp - 2], s[sp - 1]); + break; + case 0x04: // vmoveto + in_header = 0; + if(sp < 1) return STBTT__CSERR("vmoveto stack"); + stbtt__csctx_rmove_to(c, 0, s[sp - 1]); + break; + case 0x16: // hmoveto + in_header = 0; + if(sp < 1) return STBTT__CSERR("hmoveto stack"); + stbtt__csctx_rmove_to(c, s[sp - 1], 0); + break; + + case 0x05: // rlineto + if(sp < 2) return STBTT__CSERR("rlineto stack"); + for(; i + 1 < sp; i += 2) + stbtt__csctx_rline_to(c, s[i], s[i + 1]); + break; + + // hlineto/vlineto and vhcurveto/hvcurveto alternate horizontal and vertical + // starting from a different place. + + case 0x07: // vlineto + if(sp < 1) return STBTT__CSERR("vlineto stack"); + goto vlineto; + case 0x06: // hlineto + if(sp < 1) return STBTT__CSERR("hlineto stack"); + for(;;) { + if(i >= sp) break; + stbtt__csctx_rline_to(c, s[i], 0); + i++; +vlineto: + if(i >= sp) break; + stbtt__csctx_rline_to(c, 0, s[i]); + i++; + } + break; + + case 0x1F: // hvcurveto + if(sp < 4) return STBTT__CSERR("hvcurveto stack"); + goto hvcurveto; + case 0x1E: // vhcurveto + if(sp < 4) return STBTT__CSERR("vhcurveto stack"); + for(;;) { + if(i + 3 >= sp) break; + stbtt__csctx_rccurve_to(c, 0, s[i], s[i + 1], s[i + 2], s[i + 3], (sp - i == 5) ? s[i + 4] : 0.0f); + i += 4; +hvcurveto: + if(i + 3 >= sp) break; + stbtt__csctx_rccurve_to(c, s[i], 0, s[i + 1], s[i + 2], (sp - i == 5) ? s[i + 4] : 0.0f, s[i + 3]); + i += 4; + } + break; + + case 0x08: // rrcurveto + if(sp < 6) return STBTT__CSERR("rcurveline stack"); + for(; i + 5 < sp; i += 6) + stbtt__csctx_rccurve_to(c, s[i], s[i + 1], s[i + 2], s[i + 3], s[i + 4], s[i + 5]); + break; + + case 0x18: // rcurveline + if(sp < 8) return STBTT__CSERR("rcurveline stack"); + for(; i + 5 < sp - 2; i += 6) + stbtt__csctx_rccurve_to(c, s[i], s[i + 1], s[i + 2], s[i + 3], s[i + 4], s[i + 5]); + if(i + 1 >= sp) return STBTT__CSERR("rcurveline stack"); + stbtt__csctx_rline_to(c, s[i], s[i + 1]); + break; + + case 0x19: // rlinecurve + if(sp < 8) return STBTT__CSERR("rlinecurve stack"); + for(; i + 1 < sp - 6; i += 2) + stbtt__csctx_rline_to(c, s[i], s[i + 1]); + if(i + 5 >= sp) return STBTT__CSERR("rlinecurve stack"); + stbtt__csctx_rccurve_to(c, s[i], s[i + 1], s[i + 2], s[i + 3], s[i + 4], s[i + 5]); + break; + + case 0x1A: // vvcurveto + case 0x1B: // hhcurveto + if(sp < 4) return STBTT__CSERR("(vv|hh)curveto stack"); + f = 0.0; + if(sp & 1) { + f = s[i]; + i++; + } + for(; i + 3 < sp; i += 4) { + if(b0 == 0x1B) + stbtt__csctx_rccurve_to(c, s[i], f, s[i + 1], s[i + 2], s[i + 3], 0.0); + else + stbtt__csctx_rccurve_to(c, f, s[i], s[i + 1], s[i + 2], 0.0, s[i + 3]); + f = 0.0; + } + break; + + case 0x0A: // callsubr + if(!has_subrs) { + if(info->fdselect.size) + subrs = stbtt__cid_get_glyph_subrs(info, glyph_index); + has_subrs = 1; + } + // FALLTHROUGH + case 0x1D: // callgsubr + if(sp < 1) return STBTT__CSERR("call(g|)subr stack"); + v = (int)s[--sp]; + if(subr_stack_height >= 10) return STBTT__CSERR("recursion limit"); + subr_stack[subr_stack_height++] = b; + b = stbtt__get_subr(b0 == 0x0A ? subrs : info->gsubrs, v); + if(b.size == 0) return STBTT__CSERR("subr not found"); + b.cursor = 0; + clear_stack = 0; + break; + + case 0x0B: // return + if(subr_stack_height <= 0) return STBTT__CSERR("return outside subr"); + b = subr_stack[--subr_stack_height]; + clear_stack = 0; + break; + + case 0x0E: // endchar + stbtt__csctx_close_shape(c); + return 1; + + case 0x0C: { // two-byte escape + float dx1, dx2, dx3, dx4, dx5, dx6, dy1, dy2, dy3, dy4, dy5, dy6; + float dx, dy; + int b1 = stbtt__buf_get8(&b); + switch(b1) { + // @TODO These "flex" implementations ignore the flex-depth and resolution, + // and always draw beziers. + case 0x22: // hflex + if(sp < 7) return STBTT__CSERR("hflex stack"); + dx1 = s[0]; + dx2 = s[1]; + dy2 = s[2]; + dx3 = s[3]; + dx4 = s[4]; + dx5 = s[5]; + dx6 = s[6]; + stbtt__csctx_rccurve_to(c, dx1, 0, dx2, dy2, dx3, 0); + stbtt__csctx_rccurve_to(c, dx4, 0, dx5, -dy2, dx6, 0); + break; + + case 0x23: // flex + if(sp < 13) return STBTT__CSERR("flex stack"); + dx1 = s[0]; + dy1 = s[1]; + dx2 = s[2]; + dy2 = s[3]; + dx3 = s[4]; + dy3 = s[5]; + dx4 = s[6]; + dy4 = s[7]; + dx5 = s[8]; + dy5 = s[9]; + dx6 = s[10]; + dy6 = s[11]; + //fd is s[12] + stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, dy3); + stbtt__csctx_rccurve_to(c, dx4, dy4, dx5, dy5, dx6, dy6); + break; + + case 0x24: // hflex1 + if(sp < 9) return STBTT__CSERR("hflex1 stack"); + dx1 = s[0]; + dy1 = s[1]; + dx2 = s[2]; + dy2 = s[3]; + dx3 = s[4]; + dx4 = s[5]; + dx5 = s[6]; + dy5 = s[7]; + dx6 = s[8]; + stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, 0); + stbtt__csctx_rccurve_to(c, dx4, 0, dx5, dy5, dx6, -(dy1 + dy2 + dy5)); + break; + + case 0x25: // flex1 + if(sp < 11) return STBTT__CSERR("flex1 stack"); + dx1 = s[0]; + dy1 = s[1]; + dx2 = s[2]; + dy2 = s[3]; + dx3 = s[4]; + dy3 = s[5]; + dx4 = s[6]; + dy4 = s[7]; + dx5 = s[8]; + dy5 = s[9]; + dx6 = dy6 = s[10]; + dx = dx1 + dx2 + dx3 + dx4 + dx5; + dy = dy1 + dy2 + dy3 + dy4 + dy5; + if(STBTT_fabs(dx) > STBTT_fabs(dy)) + dy6 = -dy; + else + dx6 = -dx; + stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, dy3); + stbtt__csctx_rccurve_to(c, dx4, dy4, dx5, dy5, dx6, dy6); + break; + + default: + return STBTT__CSERR("unimplemented"); + } + } + break; + + default: + if(b0 != 255 && b0 != 28 && b0 < 32) + return STBTT__CSERR("reserved operator"); + + // push immediate + if(b0 == 255) { + f = (float)(stbtt_int32)stbtt__buf_get32(&b) / 0x10000; + } + else { + stbtt__buf_skip(&b, -1); + f = (float)(stbtt_int16)stbtt__cff_int(&b); + } + if(sp >= 48) return STBTT__CSERR("push stack overflow"); + s[sp++] = f; + clear_stack = 0; + break; + } + if(clear_stack) sp = 0; + } + return STBTT__CSERR("no endchar"); + +#undef STBTT__CSERR +} + +static int stbtt__GetGlyphShapeT2(const stbtt_fontinfo * info, int glyph_index, stbtt_vertex * *pvertices) +{ + // runs the charstring twice, once to count and once to output (to avoid realloc) + stbtt__csctx count_ctx = STBTT__CSCTX_INIT(1); + stbtt__csctx output_ctx = STBTT__CSCTX_INIT(0); + if(stbtt__run_charstring(info, glyph_index, &count_ctx)) { + *pvertices = (stbtt_vertex *)STBTT_malloc(count_ctx.num_vertices * sizeof(stbtt_vertex), info->userdata); + output_ctx.pvertices = *pvertices; + if(stbtt__run_charstring(info, glyph_index, &output_ctx)) { + STBTT_assert(output_ctx.num_vertices == count_ctx.num_vertices); + return output_ctx.num_vertices; + } + } + *pvertices = NULL; + return 0; +} + +static int stbtt__GetGlyphInfoT2(const stbtt_fontinfo * info, int glyph_index, int * x0, int * y0, int * x1, int * y1) +{ + stbtt__csctx c = STBTT__CSCTX_INIT(1); + int r = stbtt__run_charstring(info, glyph_index, &c); + if(x0) *x0 = r ? c.min_x : 0; + if(y0) *y0 = r ? c.min_y : 0; + if(x1) *x1 = r ? c.max_x : 0; + if(y1) *y1 = r ? c.max_y : 0; + return r ? c.num_vertices : 0; +} + +STBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo * info, int glyph_index, stbtt_vertex * *pvertices) +{ + if(!info->cff.size) + return stbtt__GetGlyphShapeTT(info, glyph_index, pvertices); + else + return stbtt__GetGlyphShapeT2(info, glyph_index, pvertices); +} + +STBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo * info, int glyph_index, int * advanceWidth, + int * leftSideBearing) +{ + stbtt_uint16 numOfLongHorMetrics = ttUSHORT(info->data, info->hhea + 34); + if(glyph_index < numOfLongHorMetrics) { + if(advanceWidth) *advanceWidth = ttSHORT(info->data, info->hmtx + 4 * glyph_index); + if(leftSideBearing) *leftSideBearing = ttSHORT(info->data, info->hmtx + 4 * glyph_index + 2); + } + else { + if(advanceWidth) *advanceWidth = ttSHORT(info->data, info->hmtx + 4 * (numOfLongHorMetrics - 1)); + if(leftSideBearing) *leftSideBearing = ttSHORT(info->data, + info->hmtx + 4 * numOfLongHorMetrics + 2 * (glyph_index - numOfLongHorMetrics)); + } +} + +STBTT_DEF int stbtt_GetKerningTableLength(const stbtt_fontinfo * info) +{ + // we only look at the first table. it must be 'horizontal' and format 0. + if(!info->kern) + return 0; + if(ttUSHORT(info->data, 2 + info->kern) < 1) // number of tables, need at least 1 + return 0; + if(ttUSHORT(info->data, 8 + info->kern) != 1) // horizontal flag must be set in format + return 0; + + return ttUSHORT(info->data, 10 + info->kern); +} + +STBTT_DEF int stbtt_GetKerningTable(const stbtt_fontinfo * info, stbtt_kerningentry * table, int table_length) +{ + int k, length; + + // we only look at the first table. it must be 'horizontal' and format 0. + if(!info->kern) + return 0; + if(ttUSHORT(info->data, 2 + info->kern) < 1) // number of tables, need at least 1 + return 0; + if(ttUSHORT(info->data, 8 + info->kern) != 1) // horizontal flag must be set in format + return 0; + + length = ttUSHORT(info->data, 10 + info->kern); + if(table_length < length) + length = table_length; + + for(k = 0; k < length; k++) { + table[k].glyph1 = ttUSHORT(info->data, 18 + (k * 6) + info->kern); + table[k].glyph2 = ttUSHORT(info->data, 20 + (k * 6) + info->kern); + table[k].advance = ttSHORT(info->data, 22 + (k * 6) + info->kern); + } + + return length; +} + +static int stbtt__GetGlyphKernInfoAdvance(const stbtt_fontinfo * info, int glyph1, int glyph2) +{ + stbtt_uint32 needle, straw; + int l, r, m; + + // we only look at the first table. it must be 'horizontal' and format 0. + if(!info->kern) + return 0; + if(ttUSHORT(info->data, info->kern + 2) < 1) // number of tables, need at least 1 + return 0; + if(ttUSHORT(info->data, info->kern + 8) != 1) // horizontal flag must be set in format + return 0; + + l = 0; + r = ttUSHORT(info->data, info->kern + 10) - 1; + needle = glyph1 << 16 | glyph2; + while(l <= r) { + m = (l + r) >> 1; + straw = ttULONG(info->data, info->kern + 18 + (m * 6)); // note: unaligned read + if(needle < straw) + r = m - 1; + else if(needle > straw) + l = m + 1; + else + return ttSHORT(info->data, info->kern + 22 + (m * 6)); + } + return 0; +} +#ifdef STBTT_STREAM_TYPE + static stbtt_int32 stbtt__GetCoverageIndex(STBTT_STREAM_TYPE data, stbtt_uint32 coverageTable, int glyph) +#else + static stbtt_int32 stbtt__GetCoverageIndex(const stbtt_uint8 * data, stbtt_uint32 coverageTable, int glyph) +#endif +{ + stbtt_uint16 coverageFormat = ttUSHORT(data, coverageTable); + switch(coverageFormat) { + case 1: { + stbtt_uint16 glyphCount = ttUSHORT(data, coverageTable + 2); + + // Binary search. + stbtt_int32 l = 0, r = glyphCount - 1, m; + int straw, needle = glyph; + while(l <= r) { + stbtt_uint32 glyphArray = coverageTable + 4; + stbtt_uint16 glyphID; + m = (l + r) >> 1; + glyphID = ttUSHORT(data, glyphArray + 2 * m); + straw = glyphID; + if(needle < straw) + r = m - 1; + else if(needle > straw) + l = m + 1; + else { + return m; + } + } + break; + } + + case 2: { + stbtt_uint16 rangeCount = ttUSHORT(data, coverageTable + 2); + stbtt_uint32 rangeArray = coverageTable + 4; + + // Binary search. + stbtt_int32 l = 0, r = rangeCount - 1, m; + int strawStart, strawEnd, needle = glyph; + while(l <= r) { + stbtt_uint32 rangeRecord; + m = (l + r) >> 1; + rangeRecord = rangeArray + 6 * m; + strawStart = ttUSHORT(data, rangeRecord); + strawEnd = ttUSHORT(data, rangeRecord + 2); + if(needle < strawStart) + r = m - 1; + else if(needle > strawEnd) + l = m + 1; + else { + stbtt_uint16 startCoverageIndex = ttUSHORT(data, rangeRecord + 4); + return startCoverageIndex + glyph - strawStart; + } + } + break; + } + + default: + return -1; // unsupported + } + + return -1; +} +#ifdef STBTT_STREAM_TYPE + static stbtt_int32 stbtt__GetGlyphClass(STBTT_STREAM_TYPE data, stbtt_uint32 classDefTable, int glyph) +#else + static stbtt_int32 stbtt__GetGlyphClass(const stbtt_uint8 * data, stbtt_uint32 classDefTable, int glyph) +#endif +{ + stbtt_uint16 classDefFormat = ttUSHORT(data, classDefTable); + switch(classDefFormat) { + case 1: { + stbtt_uint16 startGlyphID = ttUSHORT(data, classDefTable + 2); + stbtt_uint16 glyphCount = ttUSHORT(data, classDefTable + 4); + stbtt_uint32 classDef1ValueArray = classDefTable + 6; + + if(glyph >= startGlyphID && glyph < startGlyphID + glyphCount) + return (stbtt_int32)ttUSHORT(data, classDef1ValueArray + 2 * (glyph - startGlyphID)); + break; + } + + case 2: { + stbtt_uint16 classRangeCount = ttUSHORT(data, classDefTable + 2); + stbtt_uint32 classRangeRecords = classDefTable + 4; + + // Binary search. + stbtt_int32 l = 0, r = classRangeCount - 1, m; + int strawStart, strawEnd, needle = glyph; + while(l <= r) { + stbtt_uint32 classRangeRecord; + m = (l + r) >> 1; + classRangeRecord = classRangeRecords + 6 * m; + strawStart = ttUSHORT(data, classRangeRecord); + strawEnd = ttUSHORT(data, classRangeRecord + 2); + if(needle < strawStart) + r = m - 1; + else if(needle > strawEnd) + l = m + 1; + else + return (stbtt_int32)ttUSHORT(data, classRangeRecord + 4); + } + break; + } + + default: + return -1; // Unsupported definition type, return an error. + } + + // "All glyphs not assigned to a class fall into class 0". (OpenType spec) + return 0; +} + +// Define to STBTT_assert(x) if you want to break on unimplemented formats. +#define STBTT_GPOS_TODO_assert(x) + +static stbtt_int32 stbtt__GetGlyphGPOSInfoAdvance(const stbtt_fontinfo * info, int glyph1, int glyph2) +{ + stbtt_uint16 lookupListOffset; + stbtt_uint32 lookupList; + stbtt_uint16 lookupCount; +#ifdef STBTT_STREAM_TYPE + STBTT_STREAM_TYPE data = info->data; +#else + const stbtt_uint8 * data = info->data; +#endif + stbtt_int32 i, sti; + + if(!info->gpos) return 0; + + if(ttUSHORT(data, 0 + info->gpos) != 1) return 0; // Major version 1 + if(ttUSHORT(data, 2 + info->gpos) != 0) return 0; // Minor version 0 + + lookupListOffset = ttUSHORT(data, 8 + info->gpos); + lookupList = lookupListOffset; + lookupCount = ttUSHORT(data, lookupList); + + for(i = 0; i < lookupCount; ++i) { + stbtt_uint16 lookupOffset = ttUSHORT(data, lookupList + 2 + 2 * i); + stbtt_uint32 lookupTable = lookupList + lookupOffset; + + stbtt_uint16 lookupType = ttUSHORT(data, lookupTable); + stbtt_uint16 subTableCount = ttUSHORT(data, lookupTable + 4); + stbtt_uint32 subTableOffsets = lookupTable + 6; + if(lookupType != 2) // Pair Adjustment Positioning Subtable + continue; + + for(sti = 0; sti < subTableCount; sti++) { + stbtt_uint16 subtableOffset = ttUSHORT(data, subTableOffsets + 2 * sti); + stbtt_uint32 table = lookupTable + subtableOffset; + stbtt_uint16 posFormat = ttUSHORT(data, table); + stbtt_uint16 coverageOffset = ttUSHORT(data, table + 2); + stbtt_int32 coverageIndex = stbtt__GetCoverageIndex(data, table + coverageOffset, glyph1); + if(coverageIndex == -1) continue; + + switch(posFormat) { + case 1: { + stbtt_int32 l, r, m; + int straw, needle; + stbtt_uint16 valueFormat1 = ttUSHORT(data, table + 4); + stbtt_uint16 valueFormat2 = ttUSHORT(data, table + 6); + if(valueFormat1 == 4 && valueFormat2 == 0) { // Support more formats? + stbtt_int32 valueRecordPairSizeInBytes = 2; + stbtt_uint16 pairSetCount = ttUSHORT(data, table + 8); + stbtt_uint16 pairPosOffset = ttUSHORT(data, table + 10 + 2 * coverageIndex); + stbtt_uint32 pairValueTable = table + pairPosOffset; + stbtt_uint16 pairValueCount = ttUSHORT(data, pairValueTable); + stbtt_uint32 pairValueArray = pairValueTable + 2; + + if(coverageIndex >= pairSetCount) return 0; + + needle = glyph2; + r = pairValueCount - 1; + l = 0; + + // Binary search. + while(l <= r) { + stbtt_uint16 secondGlyph; + stbtt_uint32 pairValue; + m = (l + r) >> 1; + pairValue = pairValueArray + (2 + valueRecordPairSizeInBytes) * m; + secondGlyph = ttUSHORT(data, pairValue); + straw = secondGlyph; + if(needle < straw) + r = m - 1; + else if(needle > straw) + l = m + 1; + else { + stbtt_int16 xAdvance = ttSHORT(data, pairValue + 2); + return xAdvance; + } + } + } + else + return 0; + break; + } + + case 2: { + stbtt_uint16 valueFormat1 = ttUSHORT(data, table + 4); + stbtt_uint16 valueFormat2 = ttUSHORT(data, table + 6); + if(valueFormat1 == 4 && valueFormat2 == 0) { // Support more formats? + stbtt_uint16 classDef1Offset = ttUSHORT(data, table + 8); + stbtt_uint16 classDef2Offset = ttUSHORT(data, table + 10); + int glyph1class = stbtt__GetGlyphClass(data, table + classDef1Offset, glyph1); + int glyph2class = stbtt__GetGlyphClass(data, table + classDef2Offset, glyph2); + + stbtt_uint16 class1Count = ttUSHORT(data, table + 12); + stbtt_uint16 class2Count = ttUSHORT(data, table + 14); + stbtt_uint32 class1Records, class2Records; + stbtt_int16 xAdvance; + + if(glyph1class < 0 || glyph1class >= class1Count) return 0; // malformed + if(glyph2class < 0 || glyph2class >= class2Count) return 0; // malformed + + class1Records = table + 16; + class2Records = class1Records + 2 * (glyph1class * class2Count); + xAdvance = ttSHORT(data, class2Records + 2 * glyph2class); + return xAdvance; + } + else + return 0; + break; + } + + default: + return 0; // Unsupported position format + } + } + } + return 0; +} + +STBTT_DEF int stbtt_KernTableCheck(const stbtt_fontinfo * info) +{ + if(info->gpos) { + stbtt_uint16 lookupListOffset; + stbtt_uint32 lookupList; + stbtt_uint16 lookupCount; +#ifdef STBTT_STREAM_TYPE + STBTT_STREAM_TYPE data = info->data; +#else + const stbtt_uint8 * data = info->data; +#endif + stbtt_int32 i; + + if(!info->gpos) return 0; + + if(ttUSHORT(data, 0 + info->gpos) != 1) return 0; // Major version 1 + if(ttUSHORT(data, 2 + info->gpos) != 0) return 0; // Minor version 0 + + lookupListOffset = ttUSHORT(data, 8 + info->gpos); + lookupList = lookupListOffset; + lookupCount = ttUSHORT(data, lookupList); + + for(i = 0; i < lookupCount; ++i) { + stbtt_uint16 lookupOffset = ttUSHORT(data, lookupList + 2 + 2 * i); + stbtt_uint32 lookupTable = lookupList + lookupOffset; + + stbtt_uint16 lookupType = ttUSHORT(data, lookupTable); + + if(lookupType != 2) // Pair Adjustment Positioning Subtable + continue; + + return 1; // we have a usable lookup table. + } + return 0; + } + else if(info->kern) { + return 1; + } + return 0; +} + +STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo * info, int g1, int g2) +{ + int xAdvance = 0; + + if(info->gpos) + xAdvance += stbtt__GetGlyphGPOSInfoAdvance(info, g1, g2); + else if(info->kern) + xAdvance += stbtt__GetGlyphKernInfoAdvance(info, g1, g2); + + return xAdvance; +} + +STBTT_DEF int stbtt_GetCodepointKernAdvance(const stbtt_fontinfo * info, int ch1, int ch2) +{ + if(!info->kern && !info->gpos) // if no kerning table, don't waste time looking up both codepoint->glyphs + return 0; + return stbtt_GetGlyphKernAdvance(info, stbtt_FindGlyphIndex(info, ch1), stbtt_FindGlyphIndex(info, ch2)); +} + +STBTT_DEF void stbtt_GetCodepointHMetrics(const stbtt_fontinfo * info, int codepoint, int * advanceWidth, + int * leftSideBearing) +{ + stbtt_GetGlyphHMetrics(info, stbtt_FindGlyphIndex(info, codepoint), advanceWidth, leftSideBearing); +} + +STBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo * info, int * ascent, int * descent, int * lineGap) +{ + if(ascent) *ascent = ttSHORT(info->data, info->hhea + 4); + if(descent) *descent = ttSHORT(info->data, info->hhea + 6); + if(lineGap) *lineGap = ttSHORT(info->data, info->hhea + 8); +} + +STBTT_DEF int stbtt_GetFontVMetricsOS2(const stbtt_fontinfo * info, int * typoAscent, int * typoDescent, + int * typoLineGap) +{ + int tab = stbtt__find_table(info->data, info->fontstart, "OS/2"); + if(!tab) + return 0; + if(typoAscent) *typoAscent = ttSHORT(info->data, tab + 68); + if(typoDescent) *typoDescent = ttSHORT(info->data, tab + 70); + if(typoLineGap) *typoLineGap = ttSHORT(info->data, tab + 72); + return 1; +} + +STBTT_DEF void stbtt_GetFontBoundingBox(const stbtt_fontinfo * info, int * x0, int * y0, int * x1, int * y1) +{ + *x0 = ttSHORT(info->data, info->head + 36); + *y0 = ttSHORT(info->data, info->head + 38); + *x1 = ttSHORT(info->data, info->head + 40); + *y1 = ttSHORT(info->data, info->head + 42); +} + +STBTT_DEF float stbtt_ScaleForPixelHeight(const stbtt_fontinfo * info, float height) +{ + int fheight = ttSHORT(info->data, info->hhea + 4) - ttSHORT(info->data, info->hhea + 6); + return (float)height / fheight; +} + +STBTT_DEF float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo * info, float pixels) +{ + int unitsPerEm = ttUSHORT(info->data, info->head + 18); + return pixels / unitsPerEm; +} + +STBTT_DEF void stbtt_FreeShape(const stbtt_fontinfo * info, stbtt_vertex * v) +{ + STBTT_free(v, info->userdata); +} + +STBTT_DEF stbtt_uint32 stbtt_FindSVGDoc(const stbtt_fontinfo * info, int gl) +{ + int i; + stbtt_uint32 svg_doc_list = stbtt__get_svg((stbtt_fontinfo *)info); + + int numEntries = ttUSHORT(info->data, svg_doc_list); + stbtt_uint32 svg_docs = svg_doc_list + 2; + + for(i = 0; i < numEntries; i++) { + stbtt_uint32 svg_doc = svg_docs + (12 * i); + if((gl >= ttUSHORT(info->data, svg_doc)) && (gl <= ttUSHORT(info->data, svg_doc + 2))) + return svg_doc; + } + return 0; +} + +STBTT_DEF int stbtt_GetGlyphSVG(const stbtt_fontinfo * info, int gl, stbtt_uint32 * svgOfs) +{ + stbtt_uint32 svg_doc; + + if(info->svg == 0) + return 0; + + svg_doc = stbtt_FindSVGDoc(info, gl); + if(svg_doc != 0) { + *svgOfs = info->svg + ttULONG(info->data, svg_doc + 4); + return ttULONG(info->data, svg_doc + 8); + } + else { + return 0; + } +} + +STBTT_DEF int stbtt_GetCodepointSVG(const stbtt_fontinfo * info, int unicode_codepoint, stbtt_uint32 * svgOfs) +{ + return stbtt_GetGlyphSVG(info, stbtt_FindGlyphIndex(info, unicode_codepoint), svgOfs); +} + +////////////////////////////////////////////////////////////////////////////// +// +// antialiasing software rasterizer +// + +STBTT_DEF void stbtt_GetGlyphBitmapBoxSubpixel(const stbtt_fontinfo * font, int glyph, float scale_x, float scale_y, + float shift_x, float shift_y, int * ix0, int * iy0, int * ix1, int * iy1) +{ + int x0 = 0, y0 = 0, x1, y1; // =0 suppresses compiler warning + if(!stbtt_GetGlyphBox(font, glyph, &x0, &y0, &x1, &y1)) { + // e.g. space character + if(ix0) *ix0 = 0; + if(iy0) *iy0 = 0; + if(ix1) *ix1 = 0; + if(iy1) *iy1 = 0; + } + else { + // move to integral bboxes (treating pixels as little squares, what pixels get touched)? + if(ix0) *ix0 = STBTT_ifloor(x0 * scale_x + shift_x); + if(iy0) *iy0 = STBTT_ifloor(-y1 * scale_y + shift_y); + if(ix1) *ix1 = STBTT_iceil(x1 * scale_x + shift_x); + if(iy1) *iy1 = STBTT_iceil(-y0 * scale_y + shift_y); + } +} + +STBTT_DEF void stbtt_GetGlyphBitmapBox(const stbtt_fontinfo * font, int glyph, float scale_x, float scale_y, int * ix0, + int * iy0, int * ix1, int * iy1) +{ + stbtt_GetGlyphBitmapBoxSubpixel(font, glyph, scale_x, scale_y, 0.0f, 0.0f, ix0, iy0, ix1, iy1); +} + +STBTT_DEF void stbtt_GetCodepointBitmapBoxSubpixel(const stbtt_fontinfo * font, int codepoint, float scale_x, + float scale_y, float shift_x, float shift_y, int * ix0, int * iy0, int * ix1, int * iy1) +{ + stbtt_GetGlyphBitmapBoxSubpixel(font, stbtt_FindGlyphIndex(font, codepoint), scale_x, scale_y, shift_x, shift_y, ix0, + iy0, ix1, iy1); +} + +STBTT_DEF void stbtt_GetCodepointBitmapBox(const stbtt_fontinfo * font, int codepoint, float scale_x, float scale_y, + int * ix0, int * iy0, int * ix1, int * iy1) +{ + stbtt_GetCodepointBitmapBoxSubpixel(font, codepoint, scale_x, scale_y, 0.0f, 0.0f, ix0, iy0, ix1, iy1); +} + +////////////////////////////////////////////////////////////////////////////// +// +// Rasterizer + +typedef struct stbtt__hheap_chunk { + struct stbtt__hheap_chunk * next; +} stbtt__hheap_chunk; + +typedef struct stbtt__hheap { + struct stbtt__hheap_chunk * head; + void * first_free; + int num_remaining_in_head_chunk; +} stbtt__hheap; + +static void * stbtt__hheap_alloc(stbtt__hheap * hh, size_t size, void * userdata) +{ + if(hh->first_free) { + void * p = hh->first_free; + hh->first_free = *(void **)p; + return p; + } + else { + if(hh->num_remaining_in_head_chunk == 0) { + int count = (size < 32 ? STBTT_HEAP_FACTOR_SIZE_32 : size < 128 ? STBTT_HEAP_FACTOR_SIZE_128 : + STBTT_HEAP_FACTOR_SIZE_DEFAULT); + stbtt__hheap_chunk * c = (stbtt__hheap_chunk *)STBTT_malloc(sizeof(stbtt__hheap_chunk) + size * count, userdata); + if(c == NULL) + return NULL; + c->next = hh->head; + hh->head = c; + hh->num_remaining_in_head_chunk = count; + } + --hh->num_remaining_in_head_chunk; + return (char *)(hh->head) + sizeof(stbtt__hheap_chunk) + size * hh->num_remaining_in_head_chunk; + } +} + +static void stbtt__hheap_free(stbtt__hheap * hh, void * p) +{ + *(void **)p = hh->first_free; + hh->first_free = p; +} + +static void stbtt__hheap_cleanup(stbtt__hheap * hh, void * userdata) +{ + stbtt__hheap_chunk * c = hh->head; + while(c) { + stbtt__hheap_chunk * n = c->next; + STBTT_free(c, userdata); + c = n; + } +} + +typedef struct stbtt__edge { + float x0, y0, x1, y1; + int invert; +} stbtt__edge; + +typedef struct stbtt__active_edge { + struct stbtt__active_edge * next; +#if STBTT_RASTERIZER_VERSION==1 + int x, dx; + float ey; + int direction; +#elif STBTT_RASTERIZER_VERSION==2 + float fx, fdx, fdy; + float direction; + float sy; + float ey; +#else +#error "Unrecognized value of STBTT_RASTERIZER_VERSION" +#endif +} stbtt__active_edge; + +#if STBTT_RASTERIZER_VERSION == 1 +#define STBTT_FIXSHIFT 10 +#define STBTT_FIX (1 << STBTT_FIXSHIFT) +#define STBTT_FIXMASK (STBTT_FIX-1) + +static stbtt__active_edge * stbtt__new_active(stbtt__hheap * hh, stbtt__edge * e, int off_x, float start_point, + void * userdata) +{ + stbtt__active_edge * z = (stbtt__active_edge *)stbtt__hheap_alloc(hh, sizeof(*z), userdata); + float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0); + STBTT_assert(z != NULL); + if(!z) return z; + + // round dx down to avoid overshooting + if(dxdy < 0) + z->dx = -STBTT_ifloor(STBTT_FIX * -dxdy); + else + z->dx = STBTT_ifloor(STBTT_FIX * dxdy); + + z->x = STBTT_ifloor(STBTT_FIX * e->x0 + z->dx * (start_point - + e->y0)); // use z->dx so when we offset later it's by the same amount + z->x -= off_x * STBTT_FIX; + + z->ey = e->y1; + z->next = 0; + z->direction = e->invert ? 1 : -1; + return z; +} +#elif STBTT_RASTERIZER_VERSION == 2 +static stbtt__active_edge * stbtt__new_active(stbtt__hheap * hh, stbtt__edge * e, int off_x, float start_point, + void * userdata) +{ + stbtt__active_edge * z = (stbtt__active_edge *)stbtt__hheap_alloc(hh, sizeof(*z), userdata); + float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0); + STBTT_assert(z != NULL); + //STBTT_assert(e->y0 <= start_point); + if(!z) return z; + z->fdx = dxdy; + z->fdy = dxdy != 0.0f ? (1.0f / dxdy) : 0.0f; + z->fx = e->x0 + dxdy * (start_point - e->y0); + z->fx -= off_x; + z->direction = e->invert ? 1.0f : -1.0f; + z->sy = e->y0; + z->ey = e->y1; + z->next = 0; + return z; +} +#else +#error "Unrecognized value of STBTT_RASTERIZER_VERSION" +#endif + +#if STBTT_RASTERIZER_VERSION == 1 +// note: this routine clips fills that extend off the edges... ideally this +// wouldn't happen, but it could happen if the truetype glyph bounding boxes +// are wrong, or if the user supplies a too-small bitmap +static void stbtt__fill_active_edges(unsigned char * scanline, int len, stbtt__active_edge * e, int max_weight) +{ + // non-zero winding fill + int x0 = 0, w = 0; + + while(e) { + if(w == 0) { + // if we're currently at zero, we need to record the edge start point + x0 = e->x; + w += e->direction; + } + else { + int x1 = e->x; + w += e->direction; + // if we went to zero, we need to draw + if(w == 0) { + int i = x0 >> STBTT_FIXSHIFT; + int j = x1 >> STBTT_FIXSHIFT; + + if(i < len && j >= 0) { + if(i == j) { + // x0,x1 are the same pixel, so compute combined coverage + scanline[i] = scanline[i] + (stbtt_uint8)((x1 - x0) * max_weight >> STBTT_FIXSHIFT); + } + else { + if(i >= 0) // add antialiasing for x0 + scanline[i] = scanline[i] + (stbtt_uint8)(((STBTT_FIX - (x0 & STBTT_FIXMASK)) * max_weight) >> STBTT_FIXSHIFT); + else + i = -1; // clip + + if(j < len) // add antialiasing for x1 + scanline[j] = scanline[j] + (stbtt_uint8)(((x1 & STBTT_FIXMASK) * max_weight) >> STBTT_FIXSHIFT); + else + j = len; // clip + + for(++i; i < j; ++i) // fill pixels between x0 and x1 + scanline[i] = scanline[i] + (stbtt_uint8)max_weight; + } + } + } + } + + e = e->next; + } +} + +static void stbtt__rasterize_sorted_edges(stbtt__bitmap * result, stbtt__edge * e, int n, int vsubsample, int off_x, + int off_y, void * userdata) +{ + stbtt__hheap hh = { 0, 0, 0 }; + stbtt__active_edge * active = NULL; + int y, j = 0; + int max_weight = (255 / vsubsample); // weight per vertical scanline + int s; // vertical subsample index + unsigned char scanline_data[512], * scanline; + + if(result->w > 512) + scanline = (unsigned char *)STBTT_malloc(result->w, userdata); + else + scanline = scanline_data; + + y = off_y * vsubsample; + e[n].y0 = (off_y + result->h) * (float)vsubsample + 1; + + while(j < result->h) { + STBTT_memset(scanline, 0, result->w); + for(s = 0; s < vsubsample; ++s) { + // find center of pixel for this scanline + float scan_y = y + 0.5f; + stbtt__active_edge ** step = &active; + + // update all active edges; + // remove all active edges that terminate before the center of this scanline + while(*step) { + stbtt__active_edge * z = *step; + if(z->ey <= scan_y) { + *step = z->next; // delete from list + STBTT_assert(z->direction); + z->direction = 0; + stbtt__hheap_free(&hh, z); + } + else { + z->x += z->dx; // advance to position for current scanline + step = &((*step)->next); // advance through list + } + } + + // resort the list if needed + for(;;) { + int changed = 0; + step = &active; + while(*step && (*step)->next) { + if((*step)->x > (*step)->next->x) { + stbtt__active_edge * t = *step; + stbtt__active_edge * q = t->next; + + t->next = q->next; + q->next = t; + *step = q; + changed = 1; + } + step = &(*step)->next; + } + if(!changed) break; + } + + // insert all edges that start before the center of this scanline -- omit ones that also end on this scanline + while(e->y0 <= scan_y) { + if(e->y1 > scan_y) { + stbtt__active_edge * z = stbtt__new_active(&hh, e, off_x, scan_y, userdata); + if(z != NULL) { + // find insertion point + if(active == NULL) + active = z; + else if(z->x < active->x) { + // insert at front + z->next = active; + active = z; + } + else { + // find thing to insert AFTER + stbtt__active_edge * p = active; + while(p->next && p->next->x < z->x) + p = p->next; + // at this point, p->next->x is NOT < z->x + z->next = p->next; + p->next = z; + } + } + } + ++e; + } + + // now process all active edges in XOR fashion + if(active) + stbtt__fill_active_edges(scanline, result->w, active, max_weight); + + ++y; + } + STBTT_memcpy(result->pixels + j * result->stride, scanline, result->w); + ++j; + } + + stbtt__hheap_cleanup(&hh, userdata); + + if(scanline != scanline_data) + STBTT_free(scanline, userdata); +} + +#elif STBTT_RASTERIZER_VERSION == 2 + +// the edge passed in here does not cross the vertical line at x or the vertical line at x+1 +// (i.e. it has already been clipped to those) +static void stbtt__handle_clipped_edge(float * scanline, int x, stbtt__active_edge * e, float x0, float y0, float x1, + float y1) +{ + if(y0 == y1) return; + STBTT_assert(y0 < y1); + STBTT_assert(e->sy <= e->ey); + if(y0 > e->ey) return; + if(y1 < e->sy) return; + if(y0 < e->sy) { + x0 += (x1 - x0) * (e->sy - y0) / (y1 - y0); + y0 = e->sy; + } + if(y1 > e->ey) { + x1 += (x1 - x0) * (e->ey - y1) / (y1 - y0); + y1 = e->ey; + } + + if(x0 == x) + STBTT_assert(x1 <= x + 1); + else if(x0 == x + 1) + STBTT_assert(x1 >= x); + else if(x0 <= x) + STBTT_assert(x1 <= x); + else if(x0 >= x + 1) + STBTT_assert(x1 >= x + 1); + else + STBTT_assert(x1 >= x && x1 <= x + 1); + + if(x0 <= x && x1 <= x) + scanline[x] += e->direction * (y1 - y0); + else if(x0 >= x + 1 && x1 >= x + 1) { + /*Nothing to do*/; + } + else { + STBTT_assert(x0 >= x && x0 <= x + 1 && x1 >= x && x1 <= x + 1); + scanline[x] += e->direction * (y1 - y0) * (1 - ((x0 - x) + (x1 - x)) / 2); // coverage = 1 - average x position + } +} + +static float stbtt__sized_trapezoid_area(float height, float top_width, float bottom_width) +{ + STBTT_assert(top_width >= 0); + STBTT_assert(bottom_width >= 0); + return (top_width + bottom_width) / 2.0f * height; +} + +static float stbtt__position_trapezoid_area(float height, float tx0, float tx1, float bx0, float bx1) +{ + return stbtt__sized_trapezoid_area(height, tx1 - tx0, bx1 - bx0); +} + +static float stbtt__sized_triangle_area(float height, float width) +{ + return height * width / 2; +} + +static void stbtt__fill_active_edges_new(float * scanline, float * scanline_fill, int len, stbtt__active_edge * e, + float y_top) +{ + float y_bottom = y_top + 1; + + while(e) { + // brute force every pixel + + // compute intersection points with top & bottom + STBTT_assert(e->ey >= y_top); + + if(e->fdx == 0) { + float x0 = e->fx; + if(x0 < len) { + if(x0 >= 0) { + stbtt__handle_clipped_edge(scanline, (int)x0, e, x0, y_top, x0, y_bottom); + stbtt__handle_clipped_edge(scanline_fill - 1, (int)x0 + 1, e, x0, y_top, x0, y_bottom); + } + else { + stbtt__handle_clipped_edge(scanline_fill - 1, 0, e, x0, y_top, x0, y_bottom); + } + } + } + else { + float x0 = e->fx; + float dx = e->fdx; + float xb = x0 + dx; + float x_top, x_bottom; + float sy0, sy1; + float dy = e->fdy; + STBTT_assert(e->sy <= y_bottom && e->ey >= y_top); + + // compute endpoints of line segment clipped to this scanline (if the + // line segment starts on this scanline. x0 is the intersection of the + // line with y_top, but that may be off the line segment. + if(e->sy > y_top) { + x_top = x0 + dx * (e->sy - y_top); + sy0 = e->sy; + } + else { + x_top = x0; + sy0 = y_top; + } + if(e->ey < y_bottom) { + x_bottom = x0 + dx * (e->ey - y_top); + sy1 = e->ey; + } + else { + x_bottom = xb; + sy1 = y_bottom; + } + + if(x_top >= 0 && x_bottom >= 0 && x_top < len && x_bottom < len) { + // from here on, we don't have to range check x values + + if((int)x_top == (int)x_bottom) { + float height; + // simple case, only spans one pixel + int x = (int)x_top; + height = (sy1 - sy0) * e->direction; + STBTT_assert(x >= 0 && x < len); + scanline[x] += stbtt__position_trapezoid_area(height, x_top, x + 1.0f, x_bottom, x + 1.0f); + scanline_fill[x] += height; // everything right of this pixel is filled + } + else { + int x, x1, x2; + float y_crossing, y_final, step, sign, area; + // covers 2+ pixels + if(x_top > x_bottom) { + // flip scanline vertically; signed area is the same + float t; + sy0 = y_bottom - (sy0 - y_top); + sy1 = y_bottom - (sy1 - y_top); + t = sy0, sy0 = sy1, sy1 = t; + t = x_bottom, x_bottom = x_top, x_top = t; + dx = -dx; + dy = -dy; + t = x0, x0 = xb, xb = t; + } + STBTT_assert(dy >= 0); + STBTT_assert(dx >= 0); + + x1 = (int)x_top; + x2 = (int)x_bottom; + // compute intersection with y axis at x1+1 + y_crossing = y_top + dy * (x1 + 1 - x0); + + // compute intersection with y axis at x2 + y_final = y_top + dy * (x2 - x0); + + // x1 x_top x2 x_bottom + // y_top +------|-----+------------+------------+--------|---+------------+ + // | | | | | | + // | | | | | | + // sy0 | Txxxxx|............|............|............|............| + // y_crossing | *xxxxx.......|............|............|............| + // | | xxxxx..|............|............|............| + // | | /- xx*xxxx........|............|............| + // | | dy < | xxxxxx..|............|............| + // y_final | | \- | xx*xxx.........|............| + // sy1 | | | | xxxxxB...|............| + // | | | | | | + // | | | | | | + // y_bottom +------------+------------+------------+------------+------------+ + // + // goal is to measure the area covered by '.' in each pixel + + // if x2 is right at the right edge of x1, y_crossing can blow up, github #1057 + // @TODO: maybe test against sy1 rather than y_bottom? + if(y_crossing > y_bottom) + y_crossing = y_bottom; + + sign = e->direction; + + // area of the rectangle covered from sy0..y_crossing + area = sign * (y_crossing - sy0); + + // area of the triangle (x_top,sy0), (x1+1,sy0), (x1+1,y_crossing) + scanline[x1] += stbtt__sized_triangle_area(area, x1 + 1 - x_top); + + // check if final y_crossing is blown up; no test case for this + if(y_final > y_bottom) { + y_final = y_bottom; + dy = (y_final - y_crossing) / (x2 - (x1 + 1)); // if denom=0, y_final = y_crossing, so y_final <= y_bottom + } + + // in second pixel, area covered by line segment found in first pixel + // is always a rectangle 1 wide * the height of that line segment; this + // is exactly what the variable 'area' stores. it also gets a contribution + // from the line segment within it. the THIRD pixel will get the first + // pixel's rectangle contribution, the second pixel's rectangle contribution, + // and its own contribution. the 'own contribution' is the same in every pixel except + // the leftmost and rightmost, a trapezoid that slides down in each pixel. + // the second pixel's contribution to the third pixel will be the + // rectangle 1 wide times the height change in the second pixel, which is dy. + + step = sign * dy * 1; // dy is dy/dx, change in y for every 1 change in x, + // which multiplied by 1-pixel-width is how much pixel area changes for each step in x + // so the area advances by 'step' every time + + for(x = x1 + 1; x < x2; ++x) { + scanline[x] += area + step / 2; // area of trapezoid is 1*step/2 + area += step; + } + STBTT_assert(STBTT_fabs(area) <= 1.01f); // accumulated error from area += step unless we round step down + STBTT_assert(sy1 > y_final - 0.01f); + + // area covered in the last pixel is the rectangle from all the pixels to the left, + // plus the trapezoid filled by the line segment in this pixel all the way to the right edge + scanline[x2] += area + sign * stbtt__position_trapezoid_area(sy1 - y_final, (float)x2, x2 + 1.0f, x_bottom, x2 + 1.0f); + + // the rest of the line is filled based on the total height of the line segment in this pixel + scanline_fill[x2] += sign * (sy1 - sy0); + } + } + else { + // if edge goes outside of box we're drawing, we require + // clipping logic. since this does not match the intended use + // of this library, we use a different, very slow brute + // force implementation + // note though that this does happen some of the time because + // x_top and x_bottom can be extrapolated at the top & bottom of + // the shape and actually lie outside the bounding box + int x; + for(x = 0; x < len; ++x) { + // cases: + // + // there can be up to two intersections with the pixel. any intersection + // with left or right edges can be handled by splitting into two (or three) + // regions. intersections with top & bottom do not necessitate case-wise logic. + // + // the old way of doing this found the intersections with the left & right edges, + // then used some simple logic to produce up to three segments in sorted order + // from top-to-bottom. however, this had a problem: if an x edge was epsilon + // across the x border, then the corresponding y position might not be distinct + // from the other y segment, and it might ignored as an empty segment. to avoid + // that, we need to explicitly produce segments based on x positions. + + // rename variables to clearly-defined pairs + float y0 = y_top; + float x1 = (float)(x); + float x2 = (float)(x + 1); + float x3 = xb; + float y3 = y_bottom; + + // x = e->x + e->dx * (y-y_top) + // (y-y_top) = (x - e->x) / e->dx + // y = (x - e->x) / e->dx + y_top + float y1 = (x - x0) / dx + y_top; + float y2 = (x + 1 - x0) / dx + y_top; + + if(x0 < x1 && x3 > x2) { // three segments descending down-right + stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x1, y1); + stbtt__handle_clipped_edge(scanline, x, e, x1, y1, x2, y2); + stbtt__handle_clipped_edge(scanline, x, e, x2, y2, x3, y3); + } + else if(x3 < x1 && x0 > x2) { // three segments descending down-left + stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x2, y2); + stbtt__handle_clipped_edge(scanline, x, e, x2, y2, x1, y1); + stbtt__handle_clipped_edge(scanline, x, e, x1, y1, x3, y3); + } + else if(x0 < x1 && x3 > x1) { // two segments across x, down-right + stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x1, y1); + stbtt__handle_clipped_edge(scanline, x, e, x1, y1, x3, y3); + } + else if(x3 < x1 && x0 > x1) { // two segments across x, down-left + stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x1, y1); + stbtt__handle_clipped_edge(scanline, x, e, x1, y1, x3, y3); + } + else if(x0 < x2 && x3 > x2) { // two segments across x+1, down-right + stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x2, y2); + stbtt__handle_clipped_edge(scanline, x, e, x2, y2, x3, y3); + } + else if(x3 < x2 && x0 > x2) { // two segments across x+1, down-left + stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x2, y2); + stbtt__handle_clipped_edge(scanline, x, e, x2, y2, x3, y3); + } + else { // one segment + stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x3, y3); + } + } + } + } + e = e->next; + } +} + +// directly AA rasterize edges w/o supersampling +static void stbtt__rasterize_sorted_edges(stbtt__bitmap * result, stbtt__edge * e, int n, int vsubsample, int off_x, + int off_y, void * userdata) +{ + stbtt__hheap hh = { 0, 0, 0 }; + stbtt__active_edge * active = NULL; + int y, j = 0, i; + float scanline_data[129], * scanline, * scanline2; + + STBTT__NOTUSED(vsubsample); + + if(result->w > 64) + scanline = (float *)STBTT_malloc((result->w * 2 + 1) * sizeof(float), userdata); + else + scanline = scanline_data; + + scanline2 = scanline + result->w; + + y = off_y; + e[n].y0 = (float)(off_y + result->h) + 1; + + while(j < result->h) { + // find center of pixel for this scanline + float scan_y_top = y + 0.0f; + float scan_y_bottom = y + 1.0f; + stbtt__active_edge ** step = &active; + + STBTT_memset(scanline, 0, result->w * sizeof(scanline[0])); + STBTT_memset(scanline2, 0, (result->w + 1) * sizeof(scanline[0])); + + // update all active edges; + // remove all active edges that terminate before the top of this scanline + while(*step) { + stbtt__active_edge * z = *step; + if(z->ey <= scan_y_top) { + *step = z->next; // delete from list + STBTT_assert(z->direction); + z->direction = 0; + stbtt__hheap_free(&hh, z); + } + else { + step = &((*step)->next); // advance through list + } + } + + // insert all edges that start before the bottom of this scanline + while(e->y0 <= scan_y_bottom) { + if(e->y0 != e->y1) { + stbtt__active_edge * z = stbtt__new_active(&hh, e, off_x, scan_y_top, userdata); + if(z != NULL) { + if(j == 0 && off_y != 0) { + if(z->ey < scan_y_top) { + // this can happen due to subpixel positioning and some kind of fp rounding error i think + z->ey = scan_y_top; + } + } + STBTT_assert(z->ey >= scan_y_top); // if we get really unlucky a tiny bit of an edge can be out of bounds + // insert at front + z->next = active; + active = z; + } + } + ++e; + } + + // now process all active edges + if(active) + stbtt__fill_active_edges_new(scanline, scanline2 + 1, result->w, active, scan_y_top); + + { + float sum = 0; + for(i = 0; i < result->w; ++i) { + float k; + int m; + sum += scanline2[i]; + k = scanline[i] + sum; + k = (float)STBTT_fabs(k) * 255 + 0.5f; + m = (int)k; + if(m > 255) m = 255; + result->pixels[j * result->stride + i] = (unsigned char)m; + } + } + // advance all the edges + step = &active; + while(*step) { + stbtt__active_edge * z = *step; + z->fx += z->fdx; // advance to position for current scanline + step = &((*step)->next); // advance through list + } + + ++y; + ++j; + } + + stbtt__hheap_cleanup(&hh, userdata); + + if(scanline != scanline_data) + STBTT_free(scanline, userdata); +} +#else +#error "Unrecognized value of STBTT_RASTERIZER_VERSION" +#endif + +#define STBTT__COMPARE(a,b) ((a)->y0 < (b)->y0) + +static void stbtt__sort_edges_ins_sort(stbtt__edge * p, int n) +{ + int i, j; + for(i = 1; i < n; ++i) { + stbtt__edge t = p[i], * a = &t; + j = i; + while(j > 0) { + stbtt__edge * b = &p[j - 1]; + int c = STBTT__COMPARE(a, b); + if(!c) break; + p[j] = p[j - 1]; + --j; + } + if(i != j) + p[j] = t; + } +} + +static void stbtt__sort_edges_quicksort(stbtt__edge * p, int n) +{ + /* threshold for transitioning to insertion sort */ + while(n > 12) { + stbtt__edge t; + int c01, c12, c, m, i, j; + + /* compute median of three */ + m = n >> 1; + c01 = STBTT__COMPARE(&p[0], &p[m]); + c12 = STBTT__COMPARE(&p[m], &p[n - 1]); + /* if 0 >= mid >= end, or 0 < mid < end, then use mid */ + if(c01 != c12) { + /* otherwise, we'll need to swap something else to middle */ + int z; + c = STBTT__COMPARE(&p[0], &p[n - 1]); + /* 0>mid && midn => n; 0 0 */ + /* 0n: 0>n => 0; 0 n */ + z = (c == c12) ? 0 : n - 1; + t = p[z]; + p[z] = p[m]; + p[m] = t; + } + /* now p[m] is the median-of-three */ + /* swap it to the beginning so it won't move around */ + t = p[0]; + p[0] = p[m]; + p[m] = t; + + /* partition loop */ + i = 1; + j = n - 1; + for(;;) { + /* handling of equality is crucial here */ + /* for sentinels & efficiency with duplicates */ + for(;; ++i) { + if(!STBTT__COMPARE(&p[i], &p[0])) break; + } + for(;; --j) { + if(!STBTT__COMPARE(&p[0], &p[j])) break; + } + /* make sure we haven't crossed */ + if(i >= j) break; + t = p[i]; + p[i] = p[j]; + p[j] = t; + + ++i; + --j; + } + /* recurse on smaller side, iterate on larger */ + if(j < (n - i)) { + stbtt__sort_edges_quicksort(p, j); + p = p + i; + n = n - i; + } + else { + stbtt__sort_edges_quicksort(p + i, n - i); + n = j; + } + } +} + +static void stbtt__sort_edges(stbtt__edge * p, int n) +{ + stbtt__sort_edges_quicksort(p, n); + stbtt__sort_edges_ins_sort(p, n); +} + +typedef struct { + float x, y; +} stbtt__point; + +static void stbtt__rasterize(stbtt__bitmap * result, stbtt__point * pts, int * wcount, int windings, float scale_x, + float scale_y, float shift_x, float shift_y, int off_x, int off_y, int invert, void * userdata) +{ + float y_scale_inv = invert ? -scale_y : scale_y; + stbtt__edge * e; + int n, i, j, k, m; +#if STBTT_RASTERIZER_VERSION == 1 + int vsubsample = result->h < 8 ? 15 : 5; +#elif STBTT_RASTERIZER_VERSION == 2 + int vsubsample = 1; +#else +#error "Unrecognized value of STBTT_RASTERIZER_VERSION" +#endif + // vsubsample should divide 255 evenly; otherwise we won't reach full opacity + + // now we have to blow out the windings into explicit edge lists + n = 0; + for(i = 0; i < windings; ++i) + n += wcount[i]; + + e = (stbtt__edge *)STBTT_malloc(sizeof(*e) * (n + 1), userdata); // add an extra one as a sentinel + if(e == 0) return; + n = 0; + + m = 0; + for(i = 0; i < windings; ++i) { + stbtt__point * p = pts + m; + m += wcount[i]; + j = wcount[i] - 1; + for(k = 0; k < wcount[i]; j = k++) { + int a = k, b = j; + // skip the edge if horizontal + if(p[j].y == p[k].y) + continue; + // add edge from j to k to the list + e[n].invert = 0; + if(invert ? p[j].y > p[k].y : p[j].y < p[k].y) { + e[n].invert = 1; + a = j, b = k; + } + e[n].x0 = p[a].x * scale_x + shift_x; + e[n].y0 = (p[a].y * y_scale_inv + shift_y) * vsubsample; + e[n].x1 = p[b].x * scale_x + shift_x; + e[n].y1 = (p[b].y * y_scale_inv + shift_y) * vsubsample; + ++n; + } + } + + // now sort the edges by their highest point (should snap to integer, and then by x) + //STBTT_sort(e, n, sizeof(e[0]), stbtt__edge_compare); + stbtt__sort_edges(e, n); + + // now, traverse the scanlines and find the intersections on each scanline, use xor winding rule + stbtt__rasterize_sorted_edges(result, e, n, vsubsample, off_x, off_y, userdata); + + STBTT_free(e, userdata); +} + +static void stbtt__add_point(stbtt__point * points, int n, float x, float y) +{ + if(!points) return; // during first pass, it's unallocated + points[n].x = x; + points[n].y = y; +} + +// tessellate until threshold p is happy... @TODO warped to compensate for non-linear stretching +static int stbtt__tesselate_curve(stbtt__point * points, int * num_points, float x0, float y0, float x1, float y1, + float x2, float y2, float objspace_flatness_squared, int n) +{ + // midpoint + float mx = (x0 + 2 * x1 + x2) / 4; + float my = (y0 + 2 * y1 + y2) / 4; + // versus directly drawn line + float dx = (x0 + x2) / 2 - mx; + float dy = (y0 + y2) / 2 - my; + if(n > 16) // 65536 segments on one curve better be enough! + return 1; + if(dx * dx + dy * dy > objspace_flatness_squared) { // half-pixel error allowed... need to be smaller if AA + stbtt__tesselate_curve(points, num_points, x0, y0, (x0 + x1) / 2.0f, (y0 + y1) / 2.0f, mx, my, + objspace_flatness_squared, n + 1); + stbtt__tesselate_curve(points, num_points, mx, my, (x1 + x2) / 2.0f, (y1 + y2) / 2.0f, x2, y2, + objspace_flatness_squared, n + 1); + } + else { + stbtt__add_point(points, *num_points, x2, y2); + *num_points = *num_points + 1; + } + return 1; +} + +static void stbtt__tesselate_cubic(stbtt__point * points, int * num_points, float x0, float y0, float x1, float y1, + float x2, float y2, float x3, float y3, float objspace_flatness_squared, int n) +{ + // @TODO this "flatness" calculation is just made-up nonsense that seems to work well enough + float dx0 = x1 - x0; + float dy0 = y1 - y0; + float dx1 = x2 - x1; + float dy1 = y2 - y1; + float dx2 = x3 - x2; + float dy2 = y3 - y2; + float dx = x3 - x0; + float dy = y3 - y0; + float longlen = (float)(STBTT_sqrt(dx0 * dx0 + dy0 * dy0) + STBTT_sqrt(dx1 * dx1 + dy1 * dy1) + STBTT_sqrt( + dx2 * dx2 + dy2 * dy2)); + float shortlen = (float)STBTT_sqrt(dx * dx + dy * dy); + float flatness_squared = longlen * longlen - shortlen * shortlen; + + if(n > 16) // 65536 segments on one curve better be enough! + return; + + if(flatness_squared > objspace_flatness_squared) { + float x01 = (x0 + x1) / 2; + float y01 = (y0 + y1) / 2; + float x12 = (x1 + x2) / 2; + float y12 = (y1 + y2) / 2; + float x23 = (x2 + x3) / 2; + float y23 = (y2 + y3) / 2; + + float xa = (x01 + x12) / 2; + float ya = (y01 + y12) / 2; + float xb = (x12 + x23) / 2; + float yb = (y12 + y23) / 2; + + float mx = (xa + xb) / 2; + float my = (ya + yb) / 2; + + stbtt__tesselate_cubic(points, num_points, x0, y0, x01, y01, xa, ya, mx, my, objspace_flatness_squared, n + 1); + stbtt__tesselate_cubic(points, num_points, mx, my, xb, yb, x23, y23, x3, y3, objspace_flatness_squared, n + 1); + } + else { + stbtt__add_point(points, *num_points, x3, y3); + *num_points = *num_points + 1; + } +} + +// returns number of contours +static stbtt__point * stbtt_FlattenCurves(stbtt_vertex * vertices, int num_verts, float objspace_flatness, + int ** contour_lengths, int * num_contours, void * userdata) +{ + stbtt__point * points = 0; + int num_points = 0; + + float objspace_flatness_squared = objspace_flatness * objspace_flatness; + int i, n = 0, start = 0, pass; + + // count how many "moves" there are to get the contour count + for(i = 0; i < num_verts; ++i) + if(vertices[i].type == STBTT_vmove) + ++n; + + *num_contours = n; + if(n == 0) return 0; + + *contour_lengths = (int *)STBTT_malloc(sizeof(**contour_lengths) * n, userdata); + + if(*contour_lengths == 0) { + *num_contours = 0; + return 0; + } + + // make two passes through the points so we don't need to realloc + for(pass = 0; pass < 2; ++pass) { + float x = 0, y = 0; + if(pass == 1) { + points = (stbtt__point *)STBTT_malloc(num_points * sizeof(points[0]), userdata); + if(points == NULL) goto error; + } + num_points = 0; + n = -1; + for(i = 0; i < num_verts; ++i) { + switch(vertices[i].type) { + case STBTT_vmove: + // start the next contour + if(n >= 0) + (*contour_lengths)[n] = num_points - start; + ++n; + start = num_points; + + x = vertices[i].x, y = vertices[i].y; + stbtt__add_point(points, num_points++, x, y); + break; + case STBTT_vline: + x = vertices[i].x, y = vertices[i].y; + stbtt__add_point(points, num_points++, x, y); + break; + case STBTT_vcurve: + stbtt__tesselate_curve(points, &num_points, x, y, + vertices[i].cx, vertices[i].cy, + vertices[i].x, vertices[i].y, + objspace_flatness_squared, 0); + x = vertices[i].x, y = vertices[i].y; + break; + case STBTT_vcubic: + stbtt__tesselate_cubic(points, &num_points, x, y, + vertices[i].cx, vertices[i].cy, + vertices[i].cx1, vertices[i].cy1, + vertices[i].x, vertices[i].y, + objspace_flatness_squared, 0); + x = vertices[i].x, y = vertices[i].y; + break; + } + } + (*contour_lengths)[n] = num_points - start; + } + + return points; +error: + STBTT_free(points, userdata); + STBTT_free(*contour_lengths, userdata); + *contour_lengths = 0; + *num_contours = 0; + return NULL; +} + +STBTT_DEF void stbtt_Rasterize(stbtt__bitmap * result, float flatness_in_pixels, stbtt_vertex * vertices, int num_verts, + float scale_x, float scale_y, float shift_x, float shift_y, int x_off, int y_off, int invert, void * userdata) +{ + float scale = scale_x > scale_y ? scale_y : scale_x; + int winding_count = 0; + int * winding_lengths = NULL; + stbtt__point * windings = stbtt_FlattenCurves(vertices, num_verts, flatness_in_pixels / scale, &winding_lengths, + &winding_count, userdata); + if(windings) { + stbtt__rasterize(result, windings, winding_lengths, winding_count, scale_x, scale_y, shift_x, shift_y, x_off, y_off, + invert, userdata); + STBTT_free(winding_lengths, userdata); + STBTT_free(windings, userdata); + } +} + +STBTT_DEF void stbtt_FreeBitmap(unsigned char * bitmap, void * userdata) +{ + STBTT_free(bitmap, userdata); +} + +STBTT_DEF unsigned char * stbtt_GetGlyphBitmapSubpixel(const stbtt_fontinfo * info, float scale_x, float scale_y, + float shift_x, float shift_y, int glyph, int * width, int * height, int * xoff, int * yoff) +{ + int ix0, iy0, ix1, iy1; + stbtt__bitmap gbm; + stbtt_vertex * vertices; + int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices); + + if(scale_x == 0) scale_x = scale_y; + if(scale_y == 0) { + if(scale_x == 0) { + STBTT_free(vertices, info->userdata); + return NULL; + } + scale_y = scale_x; + } + + stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0, &iy0, &ix1, &iy1); + + // now we get the size + gbm.w = (ix1 - ix0); + gbm.h = (iy1 - iy0); + gbm.pixels = NULL; // in case we error + + if(width) *width = gbm.w; + if(height) *height = gbm.h; + if(xoff) *xoff = ix0; + if(yoff) *yoff = iy0; + + if(gbm.w && gbm.h) { + gbm.pixels = (unsigned char *)STBTT_malloc(gbm.w * gbm.h, info->userdata); + if(gbm.pixels) { + gbm.stride = gbm.w; + + stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0, iy0, 1, info->userdata); + } + } + STBTT_free(vertices, info->userdata); + return gbm.pixels; +} + +STBTT_DEF unsigned char * stbtt_GetGlyphBitmap(const stbtt_fontinfo * info, float scale_x, float scale_y, int glyph, + int * width, int * height, int * xoff, int * yoff) +{ + return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y, 0.0f, 0.0f, glyph, width, height, xoff, yoff); +} + +STBTT_DEF void stbtt_MakeGlyphBitmapSubpixel(const stbtt_fontinfo * info, unsigned char * output, int out_w, int out_h, + int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int glyph) +{ + int ix0, iy0; + stbtt_vertex * vertices; + int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices); + stbtt__bitmap gbm; + + stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0, &iy0, 0, 0); + gbm.pixels = output; + gbm.w = out_w; + gbm.h = out_h; + gbm.stride = out_stride; + + if(gbm.w && gbm.h) + stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0, iy0, 1, info->userdata); + + STBTT_free(vertices, info->userdata); +} + +STBTT_DEF void stbtt_MakeGlyphBitmap(const stbtt_fontinfo * info, unsigned char * output, int out_w, int out_h, + int out_stride, float scale_x, float scale_y, int glyph) +{ + stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f, 0.0f, glyph); +} + +STBTT_DEF unsigned char * stbtt_GetCodepointBitmapSubpixel(const stbtt_fontinfo * info, float scale_x, float scale_y, + float shift_x, float shift_y, int codepoint, int * width, int * height, int * xoff, int * yoff) +{ + return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y, shift_x, shift_y, stbtt_FindGlyphIndex(info, codepoint), + width, height, xoff, yoff); +} + +STBTT_DEF void stbtt_MakeCodepointBitmapSubpixelPrefilter(const stbtt_fontinfo * info, unsigned char * output, + int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int oversample_x, + int oversample_y, float * sub_x, float * sub_y, int codepoint) +{ + stbtt_MakeGlyphBitmapSubpixelPrefilter(info, output, out_w, out_h, out_stride, scale_x, scale_y, shift_x, shift_y, + oversample_x, oversample_y, sub_x, sub_y, stbtt_FindGlyphIndex(info, codepoint)); +} + +STBTT_DEF void stbtt_MakeCodepointBitmapSubpixel(const stbtt_fontinfo * info, unsigned char * output, int out_w, + int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint) +{ + stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, shift_x, shift_y, + stbtt_FindGlyphIndex(info, codepoint)); +} + +STBTT_DEF unsigned char * stbtt_GetCodepointBitmap(const stbtt_fontinfo * info, float scale_x, float scale_y, + int codepoint, int * width, int * height, int * xoff, int * yoff) +{ + return stbtt_GetCodepointBitmapSubpixel(info, scale_x, scale_y, 0.0f, 0.0f, codepoint, width, height, xoff, yoff); +} + +STBTT_DEF void stbtt_MakeCodepointBitmap(const stbtt_fontinfo * info, unsigned char * output, int out_w, int out_h, + int out_stride, float scale_x, float scale_y, int codepoint) +{ + stbtt_MakeCodepointBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f, 0.0f, codepoint); +} + +////////////////////////////////////////////////////////////////////////////// +// +// bitmap baking +// +// This is SUPER-CRAPPY packing to keep source code small +#ifdef STBTT_STREAM_TYPE +static int stbtt_BakeFontBitmap_internal(STBTT_STREAM_TYPE data, + int offset, // font location (use offset=0 for plain .ttf) + float pixel_height, // height of font in pixels + unsigned char * pixels, int pw, int ph, // bitmap to be filled in + int first_char, int num_chars, // characters to bake + stbtt_bakedchar * chardata) +#else +static int stbtt_BakeFontBitmap_internal(unsigned char * data, + int offset, // font location (use offset=0 for plain .ttf) + float pixel_height, // height of font in pixels + unsigned char * pixels, int pw, int ph, // bitmap to be filled in + int first_char, int num_chars, // characters to bake + stbtt_bakedchar * chardata) +#endif +{ + float scale; + int x, y, bottom_y, i; + stbtt_fontinfo f; + f.userdata = NULL; + if(!stbtt_InitFont(&f, data, offset)) + return -1; + STBTT_memset(pixels, 0, pw * ph); // background of 0 around pixels + x = y = 1; + bottom_y = 1; + + scale = stbtt_ScaleForPixelHeight(&f, pixel_height); + + for(i = 0; i < num_chars; ++i) { + int advance, lsb, x0, y0, x1, y1, gw, gh; + int g = stbtt_FindGlyphIndex(&f, first_char + i); + stbtt_GetGlyphHMetrics(&f, g, &advance, &lsb); + stbtt_GetGlyphBitmapBox(&f, g, scale, scale, &x0, &y0, &x1, &y1); + gw = x1 - x0; + gh = y1 - y0; + if(x + gw + 1 >= pw) + y = bottom_y, x = 1; // advance to next row + if(y + gh + 1 >= ph) // check if it fits vertically AFTER potentially moving to next row + return -i; + STBTT_assert(x + gw < pw); + STBTT_assert(y + gh < ph); + stbtt_MakeGlyphBitmap(&f, pixels + x + y * pw, gw, gh, pw, scale, scale, g); + chardata[i].x0 = (stbtt_int16)x; + chardata[i].y0 = (stbtt_int16)y; + chardata[i].x1 = (stbtt_int16)(x + gw); + chardata[i].y1 = (stbtt_int16)(y + gh); + chardata[i].xadvance = scale * advance; + chardata[i].xoff = (float)x0; + chardata[i].yoff = (float)y0; + x = x + gw + 1; + if(y + gh + 1 > bottom_y) + bottom_y = y + gh + 1; + } + return bottom_y; +} + +STBTT_DEF void stbtt_GetBakedQuad(const stbtt_bakedchar * chardata, int pw, int ph, int char_index, float * xpos, + float * ypos, stbtt_aligned_quad * q, int opengl_fillrule) +{ + float d3d_bias = opengl_fillrule ? 0 : -0.5f; + float ipw = 1.0f / pw, iph = 1.0f / ph; + const stbtt_bakedchar * b = chardata + char_index; + int round_x = STBTT_ifloor((*xpos + b->xoff) + 0.5f); + int round_y = STBTT_ifloor((*ypos + b->yoff) + 0.5f); + + q->x0 = round_x + d3d_bias; + q->y0 = round_y + d3d_bias; + q->x1 = round_x + b->x1 - b->x0 + d3d_bias; + q->y1 = round_y + b->y1 - b->y0 + d3d_bias; + + q->s0 = b->x0 * ipw; + q->t0 = b->y0 * iph; + q->s1 = b->x1 * ipw; + q->t1 = b->y1 * iph; + + *xpos += b->xadvance; +} + +////////////////////////////////////////////////////////////////////////////// +// +// rectangle packing replacement routines if you don't have stb_rect_pack.h +// + +#ifndef STB_RECT_PACK_VERSION + +typedef int stbrp_coord; + +//////////////////////////////////////////////////////////////////////////////////// +// // +// // +// COMPILER WARNING ?!?!? // +// // +// // +// if you get a compile warning due to these symbols being defined more than // +// once, move #include "stb_rect_pack.h" before #include "stb_truetype.h" // +// // +//////////////////////////////////////////////////////////////////////////////////// + +typedef struct { + int width, height; + int x, y, bottom_y; +} stbrp_context; + +typedef struct { + unsigned char x; +} stbrp_node; + +struct stbrp_rect { + stbrp_coord x, y; + int id, w, h, was_packed; +}; + +static void stbrp_init_target(stbrp_context * con, int pw, int ph, stbrp_node * nodes, int num_nodes) +{ + con->width = pw; + con->height = ph; + con->x = 0; + con->y = 0; + con->bottom_y = 0; + STBTT__NOTUSED(nodes); + STBTT__NOTUSED(num_nodes); +} + +static void stbrp_pack_rects(stbrp_context * con, stbrp_rect * rects, int num_rects) +{ + int i; + for(i = 0; i < num_rects; ++i) { + if(con->x + rects[i].w > con->width) { + con->x = 0; + con->y = con->bottom_y; + } + if(con->y + rects[i].h > con->height) + break; + rects[i].x = con->x; + rects[i].y = con->y; + rects[i].was_packed = 1; + con->x += rects[i].w; + if(con->y + rects[i].h > con->bottom_y) + con->bottom_y = con->y + rects[i].h; + } + for(; i < num_rects; ++i) + rects[i].was_packed = 0; +} +#endif + +////////////////////////////////////////////////////////////////////////////// +// +// bitmap baking +// +// This is SUPER-AWESOME (tm Ryan Gordon) packing using stb_rect_pack.h. If +// stb_rect_pack.h isn't available, it uses the BakeFontBitmap strategy. + +STBTT_DEF int stbtt_PackBegin(stbtt_pack_context * spc, unsigned char * pixels, int pw, int ph, int stride_in_bytes, + int padding, void * alloc_context) +{ + stbrp_context * context = (stbrp_context *)STBTT_malloc(sizeof(*context), alloc_context); + int num_nodes = pw - padding; + stbrp_node * nodes = (stbrp_node *)STBTT_malloc(sizeof(*nodes) * num_nodes, alloc_context); + + if(context == NULL || nodes == NULL) { + if(context != NULL) STBTT_free(context, alloc_context); + if(nodes != NULL) STBTT_free(nodes, alloc_context); + return 0; + } + + spc->user_allocator_context = alloc_context; + spc->width = pw; + spc->height = ph; + spc->pixels = pixels; + spc->pack_info = context; + spc->nodes = nodes; + spc->padding = padding; + spc->stride_in_bytes = stride_in_bytes != 0 ? stride_in_bytes : pw; + spc->h_oversample = 1; + spc->v_oversample = 1; + spc->skip_missing = 0; + + stbrp_init_target(context, pw - padding, ph - padding, nodes, num_nodes); + + if(pixels) + STBTT_memset(pixels, 0, pw * ph); // background of 0 around pixels + + return 1; +} + +STBTT_DEF void stbtt_PackEnd(stbtt_pack_context * spc) +{ + STBTT_free(spc->nodes, spc->user_allocator_context); + STBTT_free(spc->pack_info, spc->user_allocator_context); +} + +STBTT_DEF void stbtt_PackSetOversampling(stbtt_pack_context * spc, unsigned int h_oversample, unsigned int v_oversample) +{ + STBTT_assert(h_oversample <= STBTT_MAX_OVERSAMPLE); + STBTT_assert(v_oversample <= STBTT_MAX_OVERSAMPLE); + if(h_oversample <= STBTT_MAX_OVERSAMPLE) + spc->h_oversample = h_oversample; + if(v_oversample <= STBTT_MAX_OVERSAMPLE) + spc->v_oversample = v_oversample; +} + +STBTT_DEF void stbtt_PackSetSkipMissingCodepoints(stbtt_pack_context * spc, int skip) +{ + spc->skip_missing = skip; +} + +#define STBTT__OVER_MASK (STBTT_MAX_OVERSAMPLE-1) + +static void stbtt__h_prefilter(unsigned char * pixels, int w, int h, int stride_in_bytes, unsigned int kernel_width) +{ + unsigned char buffer[STBTT_MAX_OVERSAMPLE]; + int safe_w = w - kernel_width; + int j; + STBTT_memset(buffer, 0, STBTT_MAX_OVERSAMPLE); // suppress bogus warning from VS2013 -analyze + for(j = 0; j < h; ++j) { + int i; + unsigned int total; + STBTT_memset(buffer, 0, kernel_width); + + total = 0; + + // make kernel_width a constant in common cases so compiler can optimize out the divide + switch(kernel_width) { + case 2: + for(i = 0; i <= safe_w; ++i) { + total += pixels[i] - buffer[i & STBTT__OVER_MASK]; + buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i]; + pixels[i] = (unsigned char)(total / 2); + } + break; + case 3: + for(i = 0; i <= safe_w; ++i) { + total += pixels[i] - buffer[i & STBTT__OVER_MASK]; + buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i]; + pixels[i] = (unsigned char)(total / 3); + } + break; + case 4: + for(i = 0; i <= safe_w; ++i) { + total += pixels[i] - buffer[i & STBTT__OVER_MASK]; + buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i]; + pixels[i] = (unsigned char)(total / 4); + } + break; + case 5: + for(i = 0; i <= safe_w; ++i) { + total += pixels[i] - buffer[i & STBTT__OVER_MASK]; + buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i]; + pixels[i] = (unsigned char)(total / 5); + } + break; + default: + for(i = 0; i <= safe_w; ++i) { + total += pixels[i] - buffer[i & STBTT__OVER_MASK]; + buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i]; + pixels[i] = (unsigned char)(total / kernel_width); + } + break; + } + + for(; i < w; ++i) { + STBTT_assert(pixels[i] == 0); + total -= buffer[i & STBTT__OVER_MASK]; + pixels[i] = (unsigned char)(total / kernel_width); + } + + pixels += stride_in_bytes; + } +} + +static void stbtt__v_prefilter(unsigned char * pixels, int w, int h, int stride_in_bytes, unsigned int kernel_width) +{ + unsigned char buffer[STBTT_MAX_OVERSAMPLE]; + int safe_h = h - kernel_width; + int j; + STBTT_memset(buffer, 0, STBTT_MAX_OVERSAMPLE); // suppress bogus warning from VS2013 -analyze + for(j = 0; j < w; ++j) { + int i; + unsigned int total; + STBTT_memset(buffer, 0, kernel_width); + + total = 0; + + // make kernel_width a constant in common cases so compiler can optimize out the divide + switch(kernel_width) { + case 2: + for(i = 0; i <= safe_h; ++i) { + total += pixels[i * stride_in_bytes] - buffer[i & STBTT__OVER_MASK]; + buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i * stride_in_bytes]; + pixels[i * stride_in_bytes] = (unsigned char)(total / 2); + } + break; + case 3: + for(i = 0; i <= safe_h; ++i) { + total += pixels[i * stride_in_bytes] - buffer[i & STBTT__OVER_MASK]; + buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i * stride_in_bytes]; + pixels[i * stride_in_bytes] = (unsigned char)(total / 3); + } + break; + case 4: + for(i = 0; i <= safe_h; ++i) { + total += pixels[i * stride_in_bytes] - buffer[i & STBTT__OVER_MASK]; + buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i * stride_in_bytes]; + pixels[i * stride_in_bytes] = (unsigned char)(total / 4); + } + break; + case 5: + for(i = 0; i <= safe_h; ++i) { + total += pixels[i * stride_in_bytes] - buffer[i & STBTT__OVER_MASK]; + buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i * stride_in_bytes]; + pixels[i * stride_in_bytes] = (unsigned char)(total / 5); + } + break; + default: + for(i = 0; i <= safe_h; ++i) { + total += pixels[i * stride_in_bytes] - buffer[i & STBTT__OVER_MASK]; + buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i * stride_in_bytes]; + pixels[i * stride_in_bytes] = (unsigned char)(total / kernel_width); + } + break; + } + + for(; i < h; ++i) { + STBTT_assert(pixels[i * stride_in_bytes] == 0); + total -= buffer[i & STBTT__OVER_MASK]; + pixels[i * stride_in_bytes] = (unsigned char)(total / kernel_width); + } + + pixels += 1; + } +} + +static float stbtt__oversample_shift(int oversample) +{ + if(!oversample) + return 0.0f; + + // The prefilter is a box filter of width "oversample", + // which shifts phase by (oversample - 1)/2 pixels in + // oversampled space. We want to shift in the opposite + // direction to counter this. + return (float) - (oversample - 1) / (2.0f * (float)oversample); +} + +// rects array must be big enough to accommodate all characters in the given ranges +STBTT_DEF int stbtt_PackFontRangesGatherRects(stbtt_pack_context * spc, const stbtt_fontinfo * info, + stbtt_pack_range * ranges, int num_ranges, stbrp_rect * rects) +{ + int i, j, k; + int missing_glyph_added = 0; + + k = 0; + for(i = 0; i < num_ranges; ++i) { + float fh = ranges[i].font_size; + float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh); + ranges[i].h_oversample = (unsigned char)spc->h_oversample; + ranges[i].v_oversample = (unsigned char)spc->v_oversample; + for(j = 0; j < ranges[i].num_chars; ++j) { + int x0, y0, x1, y1; + int codepoint = ranges[i].array_of_unicode_codepoints == NULL ? ranges[i].first_unicode_codepoint_in_range + j : + ranges[i].array_of_unicode_codepoints[j]; + int glyph = stbtt_FindGlyphIndex(info, codepoint); + if(glyph == 0 && (spc->skip_missing || missing_glyph_added)) { + rects[k].w = rects[k].h = 0; + } + else { + stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, + scale * spc->h_oversample, + scale * spc->v_oversample, + 0, 0, + &x0, &y0, &x1, &y1); + rects[k].w = (stbrp_coord)(x1 - x0 + spc->padding + spc->h_oversample - 1); + rects[k].h = (stbrp_coord)(y1 - y0 + spc->padding + spc->v_oversample - 1); + if(glyph == 0) + missing_glyph_added = 1; + } + ++k; + } + } + + return k; +} + +STBTT_DEF void stbtt_MakeGlyphBitmapSubpixelPrefilter(const stbtt_fontinfo * info, unsigned char * output, int out_w, + int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int prefilter_x, int prefilter_y, + float * sub_x, float * sub_y, int glyph) +{ + stbtt_MakeGlyphBitmapSubpixel(info, + output, + out_w - (prefilter_x - 1), + out_h - (prefilter_y - 1), + out_stride, + scale_x, + scale_y, + shift_x, + shift_y, + glyph); + + if(prefilter_x > 1) + stbtt__h_prefilter(output, out_w, out_h, out_stride, prefilter_x); + + if(prefilter_y > 1) + stbtt__v_prefilter(output, out_w, out_h, out_stride, prefilter_y); + + *sub_x = stbtt__oversample_shift(prefilter_x); + *sub_y = stbtt__oversample_shift(prefilter_y); +} + +// rects array must be big enough to accommodate all characters in the given ranges +STBTT_DEF int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context * spc, const stbtt_fontinfo * info, + stbtt_pack_range * ranges, int num_ranges, stbrp_rect * rects) +{ + int i, j, k, missing_glyph = -1, return_value = 1; + + // save current values + int old_h_over = spc->h_oversample; + int old_v_over = spc->v_oversample; + + k = 0; + for(i = 0; i < num_ranges; ++i) { + float fh = ranges[i].font_size; + float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh); + float recip_h, recip_v, sub_x, sub_y; + spc->h_oversample = ranges[i].h_oversample; + spc->v_oversample = ranges[i].v_oversample; + recip_h = 1.0f / spc->h_oversample; + recip_v = 1.0f / spc->v_oversample; + sub_x = stbtt__oversample_shift(spc->h_oversample); + sub_y = stbtt__oversample_shift(spc->v_oversample); + for(j = 0; j < ranges[i].num_chars; ++j) { + stbrp_rect * r = &rects[k]; + if(r->was_packed && r->w != 0 && r->h != 0) { + stbtt_packedchar * bc = &ranges[i].chardata_for_range[j]; + int advance, lsb, x0, y0, x1, y1; + int codepoint = ranges[i].array_of_unicode_codepoints == NULL ? ranges[i].first_unicode_codepoint_in_range + j : + ranges[i].array_of_unicode_codepoints[j]; + int glyph = stbtt_FindGlyphIndex(info, codepoint); + stbrp_coord pad = (stbrp_coord)spc->padding; + + // pad on left and top + r->x += pad; + r->y += pad; + r->w -= pad; + r->h -= pad; + stbtt_GetGlyphHMetrics(info, glyph, &advance, &lsb); + stbtt_GetGlyphBitmapBox(info, glyph, + scale * spc->h_oversample, + scale * spc->v_oversample, + &x0, &y0, &x1, &y1); + stbtt_MakeGlyphBitmapSubpixel(info, + spc->pixels + r->x + r->y * spc->stride_in_bytes, + r->w - spc->h_oversample + 1, + r->h - spc->v_oversample + 1, + spc->stride_in_bytes, + scale * spc->h_oversample, + scale * spc->v_oversample, + 0, 0, + glyph); + + if(spc->h_oversample > 1) + stbtt__h_prefilter(spc->pixels + r->x + r->y * spc->stride_in_bytes, + r->w, r->h, spc->stride_in_bytes, + spc->h_oversample); + + if(spc->v_oversample > 1) + stbtt__v_prefilter(spc->pixels + r->x + r->y * spc->stride_in_bytes, + r->w, r->h, spc->stride_in_bytes, + spc->v_oversample); + + bc->x0 = (stbtt_int16)r->x; + bc->y0 = (stbtt_int16)r->y; + bc->x1 = (stbtt_int16)(r->x + r->w); + bc->y1 = (stbtt_int16)(r->y + r->h); + bc->xadvance = scale * advance; + bc->xoff = (float)x0 * recip_h + sub_x; + bc->yoff = (float)y0 * recip_v + sub_y; + bc->xoff2 = (x0 + r->w) * recip_h + sub_x; + bc->yoff2 = (y0 + r->h) * recip_v + sub_y; + + if(glyph == 0) + missing_glyph = j; + } + else if(spc->skip_missing) { + return_value = 0; + } + else if(r->was_packed && r->w == 0 && r->h == 0 && missing_glyph >= 0) { + ranges[i].chardata_for_range[j] = ranges[i].chardata_for_range[missing_glyph]; + } + else { + return_value = 0; // if any fail, report failure + } + + ++k; + } + } + + // restore original values + spc->h_oversample = old_h_over; + spc->v_oversample = old_v_over; + + return return_value; +} + +#ifdef STBTT_STREAM_TYPE +STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context * spc, STBTT_STREAM_TYPE fontdata, int font_index, + stbtt_pack_range * ranges, int num_ranges); +#else +STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context * spc, const unsigned char * fontdata, int font_index, + stbtt_pack_range * ranges, int num_ranges); +#endif + +STBTT_DEF void stbtt_PackFontRangesPackRects(stbtt_pack_context * spc, stbrp_rect * rects, int num_rects) +{ + stbrp_pack_rects((stbrp_context *)spc->pack_info, rects, num_rects); +} +#ifdef STBTT_STREAM_TYPE +STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context * spc, STBTT_STREAM_TYPE fontdata, int font_index, + stbtt_pack_range * ranges, int num_ranges) +#else +STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context * spc, const unsigned char * fontdata, int font_index, + stbtt_pack_range * ranges, int num_ranges) +#endif +{ + stbtt_fontinfo info; + int i, j, n, return_value = 1; + //stbrp_context *context = (stbrp_context *) spc->pack_info; + stbrp_rect * rects; + + // flag all characters as NOT packed + for(i = 0; i < num_ranges; ++i) + for(j = 0; j < ranges[i].num_chars; ++j) + ranges[i].chardata_for_range[j].x0 = + ranges[i].chardata_for_range[j].y0 = + ranges[i].chardata_for_range[j].x1 = + ranges[i].chardata_for_range[j].y1 = 0; + + n = 0; + for(i = 0; i < num_ranges; ++i) + n += ranges[i].num_chars; + + rects = (stbrp_rect *)STBTT_malloc(sizeof(*rects) * n, spc->user_allocator_context); + if(rects == NULL) + return 0; + + info.userdata = spc->user_allocator_context; + stbtt_InitFont(&info, fontdata, stbtt_GetFontOffsetForIndex(fontdata, font_index)); + + n = stbtt_PackFontRangesGatherRects(spc, &info, ranges, num_ranges, rects); + + stbtt_PackFontRangesPackRects(spc, rects, n); + + return_value = stbtt_PackFontRangesRenderIntoRects(spc, &info, ranges, num_ranges, rects); + + STBTT_free(rects, spc->user_allocator_context); + return return_value; +} + +#ifdef STBTT_STREAM_TYPE +STBTT_DEF int stbtt_PackFontRange(stbtt_pack_context * spc, STBTT_STREAM_TYPE fontdata, int font_index, float font_size, + int first_unicode_codepoint_in_range, int num_chars_in_range, stbtt_packedchar * chardata_for_range); +#else +STBTT_DEF int stbtt_PackFontRange(stbtt_pack_context * spc, const unsigned char * fontdata, int font_index, + float font_size, + int first_unicode_codepoint_in_range, int num_chars_in_range, stbtt_packedchar * chardata_for_range); +#endif + +#ifdef STBTT_STREAM_TYPE +STBTT_DEF int stbtt_PackFontRange(stbtt_pack_context * spc, STBTT_STREAM_TYPE fontdata, int font_index, float font_size, + int first_unicode_codepoint_in_range, int num_chars_in_range, stbtt_packedchar * chardata_for_range) +#else +STBTT_DEF int stbtt_PackFontRange(stbtt_pack_context * spc, const unsigned char * fontdata, int font_index, + float font_size, + int first_unicode_codepoint_in_range, int num_chars_in_range, stbtt_packedchar * chardata_for_range) +#endif +{ + stbtt_pack_range range; + range.first_unicode_codepoint_in_range = first_unicode_codepoint_in_range; + range.array_of_unicode_codepoints = NULL; + range.num_chars = num_chars_in_range; + range.chardata_for_range = chardata_for_range; + range.font_size = font_size; + return stbtt_PackFontRanges(spc, fontdata, font_index, &range, 1); +} + +#ifdef STBTT_STREAM_TYPE +STBTT_DEF void stbtt_GetScaledFontVMetrics(STBTT_STREAM_TYPE fontdata, int index, float size, float * ascent, + float * descent, float * lineGap); +#else +STBTT_DEF void stbtt_GetScaledFontVMetrics(const unsigned char * fontdata, int index, float size, float * ascent, + float * descent, float * lineGap); +#endif + +#ifdef STBTT_STREAM_TYPE +STBTT_DEF void stbtt_GetScaledFontVMetrics(STBTT_STREAM_TYPE fontdata, int index, float size, float * ascent, + float * descent, float * lineGap) +#else +STBTT_DEF void stbtt_GetScaledFontVMetrics(const unsigned char * fontdata, int index, float size, float * ascent, + float * descent, float * lineGap) +#endif +{ + int i_ascent, i_descent, i_lineGap; + float scale; + stbtt_fontinfo info; + stbtt_InitFont(&info, fontdata, stbtt_GetFontOffsetForIndex(fontdata, index)); + scale = size > 0 ? stbtt_ScaleForPixelHeight(&info, size) : stbtt_ScaleForMappingEmToPixels(&info, -size); + stbtt_GetFontVMetrics(&info, &i_ascent, &i_descent, &i_lineGap); + *ascent = (float)i_ascent * scale; + *descent = (float)i_descent * scale; + *lineGap = (float)i_lineGap * scale; +} + +STBTT_DEF void stbtt_GetPackedQuad(const stbtt_packedchar * chardata, int pw, int ph, int char_index, float * xpos, + float * ypos, stbtt_aligned_quad * q, int align_to_integer) +{ + float ipw = 1.0f / pw, iph = 1.0f / ph; + const stbtt_packedchar * b = chardata + char_index; + + if(align_to_integer) { + float x = (float)STBTT_ifloor((*xpos + b->xoff) + 0.5f); + float y = (float)STBTT_ifloor((*ypos + b->yoff) + 0.5f); + q->x0 = x; + q->y0 = y; + q->x1 = x + b->xoff2 - b->xoff; + q->y1 = y + b->yoff2 - b->yoff; + } + else { + q->x0 = *xpos + b->xoff; + q->y0 = *ypos + b->yoff; + q->x1 = *xpos + b->xoff2; + q->y1 = *ypos + b->yoff2; + } + + q->s0 = b->x0 * ipw; + q->t0 = b->y0 * iph; + q->s1 = b->x1 * ipw; + q->t1 = b->y1 * iph; + + *xpos += b->xadvance; +} + +////////////////////////////////////////////////////////////////////////////// +// +// sdf computation +// + +#define STBTT_min(a,b) ((a) < (b) ? (a) : (b)) +#define STBTT_max(a,b) ((a) < (b) ? (b) : (a)) + +static int stbtt__ray_intersect_bezier(float orig[2], float ray[2], float q0[2], float q1[2], float q2[2], + float hits[2][2]) +{ + float q0perp = q0[1] * ray[0] - q0[0] * ray[1]; + float q1perp = q1[1] * ray[0] - q1[0] * ray[1]; + float q2perp = q2[1] * ray[0] - q2[0] * ray[1]; + float roperp = orig[1] * ray[0] - orig[0] * ray[1]; + + float a = q0perp - 2 * q1perp + q2perp; + float b = q1perp - q0perp; + float c = q0perp - roperp; + + float s0 = 0., s1 = 0.; + int num_s = 0; + + if(a != 0.0f) { + float discr = b * b - a * c; + if(discr > 0.0f) { + float rcpna = -1 / a; + float d = (float)STBTT_sqrt(discr); + s0 = (b + d) * rcpna; + s1 = (b - d) * rcpna; + if(s0 >= 0.0f && s0 <= 1.0f) + num_s = 1; + if(d > 0.0f && s1 >= 0.0f && s1 <= 1.0f) { + if(num_s == 0) s0 = s1; + ++num_s; + } + } + } + else { + // 2*b*s + c = 0 + // s = -c / (2*b) + s0 = c / (-2 * b); + if(s0 >= 0.0f && s0 <= 1.0f) + num_s = 1; + } + + if(num_s == 0) + return 0; + else { + float rcp_len2 = 1 / (ray[0] * ray[0] + ray[1] * ray[1]); + float rayn_x = ray[0] * rcp_len2, rayn_y = ray[1] * rcp_len2; + + float q0d = q0[0] * rayn_x + q0[1] * rayn_y; + float q1d = q1[0] * rayn_x + q1[1] * rayn_y; + float q2d = q2[0] * rayn_x + q2[1] * rayn_y; + float rod = orig[0] * rayn_x + orig[1] * rayn_y; + + float q10d = q1d - q0d; + float q20d = q2d - q0d; + float q0rd = q0d - rod; + + hits[0][0] = q0rd + s0 * (2.0f - 2.0f * s0) * q10d + s0 * s0 * q20d; + hits[0][1] = a * s0 + b; + + if(num_s > 1) { + hits[1][0] = q0rd + s1 * (2.0f - 2.0f * s1) * q10d + s1 * s1 * q20d; + hits[1][1] = a * s1 + b; + return 2; + } + else { + return 1; + } + } +} + +static int equal(float * a, float * b) +{ + return (a[0] == b[0] && a[1] == b[1]); +} + +static int stbtt__compute_crossings_x(float x, float y, int nverts, stbtt_vertex * verts) +{ + int i; + float orig[2], ray[2] = { 1, 0 }; + float y_frac; + int winding = 0; + + // make sure y never passes through a vertex of the shape + y_frac = (float)STBTT_fmod(y, 1.0f); + if(y_frac < 0.01f) + y += 0.01f; + else if(y_frac > 0.99f) + y -= 0.01f; + + orig[0] = x; + orig[1] = y; + + // test a ray from (-infinity,y) to (x,y) + for(i = 0; i < nverts; ++i) { + if(verts[i].type == STBTT_vline) { + int x0 = (int)verts[i - 1].x, y0 = (int)verts[i - 1].y; + int x1 = (int)verts[i].x, y1 = (int)verts[i].y; + if(y > STBTT_min(y0, y1) && y < STBTT_max(y0, y1) && x > STBTT_min(x0, x1)) { + float x_inter = (y - y0) / (y1 - y0) * (x1 - x0) + x0; + if(x_inter < x) + winding += (y0 < y1) ? 1 : -1; + } + } + if(verts[i].type == STBTT_vcurve) { + int x0 = (int)verts[i - 1].x, y0 = (int)verts[i - 1].y; + int x1 = (int)verts[i].cx, y1 = (int)verts[i].cy; + int x2 = (int)verts[i].x, y2 = (int)verts[i].y; + int ax = STBTT_min(x0, STBTT_min(x1, x2)), ay = STBTT_min(y0, STBTT_min(y1, y2)); + int by = STBTT_max(y0, STBTT_max(y1, y2)); + if(y > ay && y < by && x > ax) { + float q0[2], q1[2], q2[2]; + float hits[2][2]; + q0[0] = (float)x0; + q0[1] = (float)y0; + q1[0] = (float)x1; + q1[1] = (float)y1; + q2[0] = (float)x2; + q2[1] = (float)y2; + if(equal(q0, q1) || equal(q1, q2)) { + x0 = (int)verts[i - 1].x; + y0 = (int)verts[i - 1].y; + x1 = (int)verts[i].x; + y1 = (int)verts[i].y; + if(y > STBTT_min(y0, y1) && y < STBTT_max(y0, y1) && x > STBTT_min(x0, x1)) { + float x_inter = (y - y0) / (y1 - y0) * (x1 - x0) + x0; + if(x_inter < x) + winding += (y0 < y1) ? 1 : -1; + } + } + else { + int num_hits = stbtt__ray_intersect_bezier(orig, ray, q0, q1, q2, hits); + if(num_hits >= 1) + if(hits[0][0] < 0) + winding += (hits[0][1] < 0 ? -1 : 1); + if(num_hits >= 2) + if(hits[1][0] < 0) + winding += (hits[1][1] < 0 ? -1 : 1); + } + } + } + } + return winding; +} + +static float stbtt__cuberoot(float x) +{ + if(x < 0) + return -(float)STBTT_pow(-x, 1.0f / 3.0f); + else + return (float)STBTT_pow(x, 1.0f / 3.0f); +} + +// x^3 + a*x^2 + b*x + c = 0 +static int stbtt__solve_cubic(float a, float b, float c, float * r) +{ + float s = -a / 3; + float p = b - a * a / 3; + float q = a * (2 * a * a - 9 * b) / 27 + c; + float p3 = p * p * p; + float d = q * q + 4 * p3 / 27; + if(d >= 0) { + float z = (float)STBTT_sqrt(d); + float u = (-q + z) / 2; + float v = (-q - z) / 2; + u = stbtt__cuberoot(u); + v = stbtt__cuberoot(v); + r[0] = s + u + v; + return 1; + } + else { + float u = (float)STBTT_sqrt(-p / 3); + float v = (float)STBTT_acos(-STBTT_sqrt(-27 / p3) * q / 2) / 3; // p3 must be negative, since d is negative + float m = (float)STBTT_cos(v); + float n = (float)STBTT_cos(v - 3.141592f / 2) * 1.732050808f; + r[0] = s + u * 2 * m; + r[1] = s - u * (m + n); + r[2] = s - u * (m - n); + + //STBTT_assert( STBTT_fabs(((r[0]+a)*r[0]+b)*r[0]+c) < 0.05f); // these asserts may not be safe at all scales, though they're in bezier t parameter units so maybe? + //STBTT_assert( STBTT_fabs(((r[1]+a)*r[1]+b)*r[1]+c) < 0.05f); + //STBTT_assert( STBTT_fabs(((r[2]+a)*r[2]+b)*r[2]+c) < 0.05f); + return 3; + } +} + +STBTT_DEF unsigned char * stbtt_GetGlyphSDF(const stbtt_fontinfo * info, float scale, int glyph, int padding, + unsigned char onedge_value, float pixel_dist_scale, int * width, int * height, int * xoff, int * yoff) +{ + float scale_x = scale, scale_y = scale; + int ix0, iy0, ix1, iy1; + int w, h; + unsigned char * data; + + if(scale == 0) return NULL; + + stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale, scale, 0.0f, 0.0f, &ix0, &iy0, &ix1, &iy1); + + // if empty, return NULL + if(ix0 == ix1 || iy0 == iy1) + return NULL; + + ix0 -= padding; + iy0 -= padding; + ix1 += padding; + iy1 += padding; + + w = (ix1 - ix0); + h = (iy1 - iy0); + + if(width) *width = w; + if(height) *height = h; + if(xoff) *xoff = ix0; + if(yoff) *yoff = iy0; + + // invert for y-downwards bitmaps + scale_y = -scale_y; + + { + int x, y, i, j; + float * precompute; + stbtt_vertex * verts; + int num_verts = stbtt_GetGlyphShape(info, glyph, &verts); + data = (unsigned char *)STBTT_malloc(w * h, info->userdata); + precompute = (float *)STBTT_malloc(num_verts * sizeof(float), info->userdata); + + for(i = 0, j = num_verts - 1; i < num_verts; j = i++) { + if(verts[i].type == STBTT_vline) { + float x0 = verts[i].x * scale_x, y0 = verts[i].y * scale_y; + float x1 = verts[j].x * scale_x, y1 = verts[j].y * scale_y; + float dist = (float)STBTT_sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0)); + precompute[i] = (dist == 0) ? 0.0f : 1.0f / dist; + } + else if(verts[i].type == STBTT_vcurve) { + float x2 = verts[j].x * scale_x, y2 = verts[j].y * scale_y; + float x1 = verts[i].cx * scale_x, y1 = verts[i].cy * scale_y; + float x0 = verts[i].x * scale_x, y0 = verts[i].y * scale_y; + float bx = x0 - 2 * x1 + x2, by = y0 - 2 * y1 + y2; + float len2 = bx * bx + by * by; + if(len2 != 0.0f) + precompute[i] = 1.0f / (bx * bx + by * by); + else + precompute[i] = 0.0f; + } + else + precompute[i] = 0.0f; + } + + for(y = iy0; y < iy1; ++y) { + for(x = ix0; x < ix1; ++x) { + float val; + float min_dist = 999999.0f; + float sx = (float)x + 0.5f; + float sy = (float)y + 0.5f; + float x_gspace = (sx / scale_x); + float y_gspace = (sy / scale_y); + + int winding = stbtt__compute_crossings_x(x_gspace, y_gspace, num_verts, + verts); // @OPTIMIZE: this could just be a rasterization, but needs to be line vs. non-tesselated curves so a new path + + for(i = 0; i < num_verts; ++i) { + float x0 = verts[i].x * scale_x, y0 = verts[i].y * scale_y; + + if(verts[i].type == STBTT_vline && precompute[i] != 0.0f) { + float x1 = verts[i - 1].x * scale_x, y1 = verts[i - 1].y * scale_y; + + float dist, dist2 = (x0 - sx) * (x0 - sx) + (y0 - sy) * (y0 - sy); + if(dist2 < min_dist * min_dist) + min_dist = (float)STBTT_sqrt(dist2); + + // coarse culling against bbox + //if (sx > STBTT_min(x0,x1)-min_dist && sx < STBTT_max(x0,x1)+min_dist && + // sy > STBTT_min(y0,y1)-min_dist && sy < STBTT_max(y0,y1)+min_dist) + dist = (float)STBTT_fabs((x1 - x0) * (y0 - sy) - (y1 - y0) * (x0 - sx)) * precompute[i]; + STBTT_assert(i != 0); + if(dist < min_dist) { + // check position along line + // x' = x0 + t*(x1-x0), y' = y0 + t*(y1-y0) + // minimize (x'-sx)*(x'-sx)+(y'-sy)*(y'-sy) + float dx = x1 - x0, dy = y1 - y0; + float px = x0 - sx, py = y0 - sy; + // minimize (px+t*dx)^2 + (py+t*dy)^2 = px*px + 2*px*dx*t + t^2*dx*dx + py*py + 2*py*dy*t + t^2*dy*dy + // derivative: 2*px*dx + 2*py*dy + (2*dx*dx+2*dy*dy)*t, set to 0 and solve + float t = -(px * dx + py * dy) / (dx * dx + dy * dy); + if(t >= 0.0f && t <= 1.0f) + min_dist = dist; + } + } + else if(verts[i].type == STBTT_vcurve) { + float x2 = verts[i - 1].x * scale_x, y2 = verts[i - 1].y * scale_y; + float x1 = verts[i].cx * scale_x, y1 = verts[i].cy * scale_y; + float box_x0 = STBTT_min(STBTT_min(x0, x1), x2); + float box_y0 = STBTT_min(STBTT_min(y0, y1), y2); + float box_x1 = STBTT_max(STBTT_max(x0, x1), x2); + float box_y1 = STBTT_max(STBTT_max(y0, y1), y2); + // coarse culling against bbox to avoid computing cubic unnecessarily + if(sx > box_x0 - min_dist && sx < box_x1 + min_dist && sy > box_y0 - min_dist && sy < box_y1 + min_dist) { + int num = 0; + float ax = x1 - x0, ay = y1 - y0; + float bx = x0 - 2 * x1 + x2, by = y0 - 2 * y1 + y2; + float mx = x0 - sx, my = y0 - sy; + float res[3] = { 0.f, 0.f, 0.f }; + float px, py, t, it, dist2; + float a_inv = precompute[i]; + if(a_inv == 0.0f) { // if a_inv is 0, it's 2nd degree so use quadratic formula + float a = 3 * (ax * bx + ay * by); + float b = 2 * (ax * ax + ay * ay) + (mx * bx + my * by); + float c = mx * ax + my * ay; + if(a == 0.0f) { // if a is 0, it's linear + if(b != 0.0f) { + res[num++] = -c / b; + } + } + else { + float discriminant = b * b - 4 * a * c; + if(discriminant < 0) + num = 0; + else { + float root = (float)STBTT_sqrt(discriminant); + res[0] = (-b - root) / (2 * a); + res[1] = (-b + root) / (2 * a); + num = 2; // don't bother distinguishing 1-solution case, as code below will still work + } + } + } + else { + float b = 3 * (ax * bx + ay * by) * a_inv; // could precompute this as it doesn't depend on sample point + float c = (2 * (ax * ax + ay * ay) + (mx * bx + my * by)) * a_inv; + float d = (mx * ax + my * ay) * a_inv; + num = stbtt__solve_cubic(b, c, d, res); + } + dist2 = (x0 - sx) * (x0 - sx) + (y0 - sy) * (y0 - sy); + if(dist2 < min_dist * min_dist) + min_dist = (float)STBTT_sqrt(dist2); + + if(num >= 1 && res[0] >= 0.0f && res[0] <= 1.0f) { + t = res[0], it = 1.0f - t; + px = it * it * x0 + 2 * t * it * x1 + t * t * x2; + py = it * it * y0 + 2 * t * it * y1 + t * t * y2; + dist2 = (px - sx) * (px - sx) + (py - sy) * (py - sy); + if(dist2 < min_dist * min_dist) + min_dist = (float)STBTT_sqrt(dist2); + } + if(num >= 2 && res[1] >= 0.0f && res[1] <= 1.0f) { + t = res[1], it = 1.0f - t; + px = it * it * x0 + 2 * t * it * x1 + t * t * x2; + py = it * it * y0 + 2 * t * it * y1 + t * t * y2; + dist2 = (px - sx) * (px - sx) + (py - sy) * (py - sy); + if(dist2 < min_dist * min_dist) + min_dist = (float)STBTT_sqrt(dist2); + } + if(num >= 3 && res[2] >= 0.0f && res[2] <= 1.0f) { + t = res[2], it = 1.0f - t; + px = it * it * x0 + 2 * t * it * x1 + t * t * x2; + py = it * it * y0 + 2 * t * it * y1 + t * t * y2; + dist2 = (px - sx) * (px - sx) + (py - sy) * (py - sy); + if(dist2 < min_dist * min_dist) + min_dist = (float)STBTT_sqrt(dist2); + } + } + } + } + if(winding == 0) + min_dist = -min_dist; // if outside the shape, value is negative + val = onedge_value + pixel_dist_scale * min_dist; + if(val < 0) + val = 0; + else if(val > 255) + val = 255; + data[(y - iy0) * w + (x - ix0)] = (unsigned char)val; + } + } + STBTT_free(precompute, info->userdata); + STBTT_free(verts, info->userdata); + } + return data; +} + +STBTT_DEF unsigned char * stbtt_GetCodepointSDF(const stbtt_fontinfo * info, float scale, int codepoint, int padding, + unsigned char onedge_value, float pixel_dist_scale, int * width, int * height, int * xoff, int * yoff) +{ + return stbtt_GetGlyphSDF(info, scale, stbtt_FindGlyphIndex(info, codepoint), padding, onedge_value, pixel_dist_scale, + width, height, xoff, yoff); +} + +STBTT_DEF void stbtt_FreeSDF(unsigned char * bitmap, void * userdata) +{ + STBTT_free(bitmap, userdata); +} + +////////////////////////////////////////////////////////////////////////////// +// +// font name matching -- recommended not to use this +// + +// check if a utf8 string contains a prefix which is the utf16 string; if so return length of matching utf8 string +#ifdef STBTT_STREAM_TYPE +static stbtt_int32 stbtt__CompareUTF8toUTF16_bigendian_prefix(stbtt_uint8 * s1, stbtt_int32 len1, STBTT_STREAM_TYPE s2, + stbtt_uint32 s2offs, stbtt_int32 len2) +#else +static stbtt_int32 stbtt__CompareUTF8toUTF16_bigendian_prefix(stbtt_uint8 * s1, stbtt_int32 len1, stbtt_uint8 * s2, + stbtt_uint32 s2offs, stbtt_int32 len2) +#endif +{ + stbtt_int32 i = 0; + + // convert utf16 to utf8 and compare the results while converting + while(len2) { + stbtt_uint16 ch = ttUSHORT(s2, s2offs); + if(ch < 0x80) { + if(i >= len1) return -1; + if(s1[i++] != ch) return -1; + } + else if(ch < 0x800) { + if(i + 1 >= len1) return -1; + if(s1[i++] != 0xc0 + (ch >> 6)) return -1; + if(s1[i++] != 0x80 + (ch & 0x3f)) return -1; + } + else if(ch >= 0xd800 && ch < 0xdc00) { + stbtt_uint32 c; + stbtt_uint16 ch2 = ttUSHORT(s2, s2offs + 2); + if(i + 3 >= len1) return -1; + c = ((ch - 0xd800) << 10) + (ch2 - 0xdc00) + 0x10000; + if(s1[i++] != 0xf0 + (c >> 18)) return -1; + if(s1[i++] != 0x80 + ((c >> 12) & 0x3f)) return -1; + if(s1[i++] != 0x80 + ((c >> 6) & 0x3f)) return -1; + if(s1[i++] != 0x80 + ((c) & 0x3f)) return -1; + s2offs += 2; // plus another 2 below + len2 -= 2; + } + else if(ch >= 0xdc00 && ch < 0xe000) { + return -1; + } + else { + if(i + 2 >= len1) return -1; + if(s1[i++] != 0xe0 + (ch >> 12)) return -1; + if(s1[i++] != 0x80 + ((ch >> 6) & 0x3f)) return -1; + if(s1[i++] != 0x80 + ((ch) & 0x3f)) return -1; + } + s2offs += 2; + len2 -= 2; + } + return i; +} +#ifdef STBTT_STREAM_TYPE +static int stbtt_CompareUTF8toUTF16_bigendian_internal(char * s1, int len1, STBTT_STREAM_TYPE s2, stbtt_uint32 s2offs, + int len2) +{ + return len1 == stbtt__CompareUTF8toUTF16_bigendian_prefix((stbtt_uint8 *)s1, len1, s2, s2offs, len2); +} +#else +static int stbtt_CompareUTF8toUTF16_bigendian_internal(char * s1, int len1, char * s2, stbtt_uint32 s2offs, int len2) +{ + return len1 == stbtt__CompareUTF8toUTF16_bigendian_prefix((stbtt_uint8 *)s1, len1, (stbtt_uint8 *)s2, s2offs, len2); +} +#endif +// returns results in whatever encoding you request... but note that 2-byte encodings +// will be BIG-ENDIAN... use stbtt_CompareUTF8toUTF16_bigendian() to compare +STBTT_DEF stbtt_uint32 stbtt_GetFontNameString(const stbtt_fontinfo * font, int * length, int platformID, + int encodingID, int languageID, int nameID) +{ + stbtt_int32 i, count, stringOffset; + stbtt_uint32 offset = font->fontstart; + stbtt_uint32 nm = stbtt__find_table(font->data, offset, "name"); + if(!nm) return 0; + + count = ttUSHORT(font->data, nm + 2); + stringOffset = nm + ttUSHORT(font->data, nm + 4); + for(i = 0; i < count; ++i) { + stbtt_uint32 loc = nm + 6 + 12 * i; + if(platformID == ttUSHORT(font->data, loc + 0) && encodingID == ttUSHORT(font->data, loc + 2) + && languageID == ttUSHORT(font->data, loc + 4) && nameID == ttUSHORT(font->data, loc + 6)) { + *length = ttUSHORT(font->data, loc + 8); + return stringOffset + ttUSHORT(font->data, loc + 10); + } + } + return 0; +} +#ifdef STBTT_STREAM_TYPE +static int stbtt__matchpair(STBTT_STREAM_TYPE fc, stbtt_uint32 nm, stbtt_uint8 * name, stbtt_int32 nlen, + stbtt_int32 target_id, stbtt_int32 next_id) +#else +static int stbtt__matchpair(stbtt_uint8 * fc, stbtt_uint32 nm, stbtt_uint8 * name, stbtt_int32 nlen, + stbtt_int32 target_id, stbtt_int32 next_id) +#endif +{ + stbtt_int32 i; + stbtt_int32 count = ttUSHORT(fc, nm + 2); + stbtt_int32 stringOffset = nm + ttUSHORT(fc, nm + 4); + + for(i = 0; i < count; ++i) { + stbtt_uint32 loc = nm + 6 + 12 * i; + stbtt_int32 id = ttUSHORT(fc, loc + 6); + if(id == target_id) { + // find the encoding + stbtt_int32 platform = ttUSHORT(fc, loc + 0), encoding = ttUSHORT(fc, loc + 2), language = ttUSHORT(fc, loc + 4); + + // is this a Unicode encoding? + if(platform == 0 || (platform == 3 && encoding == 1) || (platform == 3 && encoding == 10)) { + stbtt_int32 slen = ttUSHORT(fc, loc + 8); + stbtt_int32 off = ttUSHORT(fc, loc + 10); + + // check if there's a prefix match + stbtt_int32 matchlen = stbtt__CompareUTF8toUTF16_bigendian_prefix(name, nlen, fc, stringOffset + off, slen); + if(matchlen >= 0) { + // check for target_id+1 immediately following, with same encoding & language + if(i + 1 < count && ttUSHORT(fc, loc + 12 + 6) == next_id && ttUSHORT(fc, loc + 12) == platform && + ttUSHORT(fc, loc + 12 + 2) == encoding && ttUSHORT(fc, loc + 12 + 4) == language) { + slen = ttUSHORT(fc, loc + 12 + 8); + off = ttUSHORT(fc, loc + 12 + 10); + if(slen == 0) { + if(matchlen == nlen) + return 1; + } + else if(matchlen < nlen && name[matchlen] == ' ') { + ++matchlen; +#ifdef STBTT_STREAM_TYPE + if(stbtt_CompareUTF8toUTF16_bigendian_internal((char *)(name + matchlen), nlen - matchlen, fc, stringOffset + off, + slen)) +#else + if(stbtt_CompareUTF8toUTF16_bigendian_internal((char *)(name + matchlen), nlen - matchlen, (char *)fc, + stringOffset + off, slen)) +#endif + return 1; + } + } + else { + // if nothing immediately following + if(matchlen == nlen) + return 1; + } + } + } + + // @TODO handle other encodings + } + } + return 0; +} +#ifdef STBTT_STREAM_TYPE + static int stbtt__matches(STBTT_STREAM_TYPE fc, stbtt_uint32 offset, stbtt_uint8 * name, stbtt_int32 flags) +#else + static int stbtt__matches(stbtt_uint8 * fc, stbtt_uint32 offset, stbtt_uint8 * name, stbtt_int32 flags) +#endif + +{ + stbtt_int32 nlen = (stbtt_int32)STBTT_strlen((char *)name); + stbtt_uint32 nm, hd; + if(!stbtt__isfont(fc, offset)) return 0; + + // check italics/bold/underline flags in macStyle... + if(flags) { + hd = stbtt__find_table(fc, offset, "head"); + if((ttUSHORT(fc, hd + 44) & 7) != (flags & 7)) return 0; + } + + nm = stbtt__find_table(fc, offset, "name"); + if(!nm) return 0; + + if(flags) { + if(name == NULL) return 1; + // if we checked the macStyle flags, then just check the family and ignore the subfamily + if(stbtt__matchpair(fc, nm, name, nlen, 16, -1)) return 1; + if(stbtt__matchpair(fc, nm, name, nlen, 1, -1)) return 1; + if(stbtt__matchpair(fc, nm, name, nlen, 3, -1)) return 1; + } + else { + if(name == NULL) return 1; + if(stbtt__matchpair(fc, nm, name, nlen, 16, 17)) return 1; + if(stbtt__matchpair(fc, nm, name, nlen, 1, 2)) return 1; + if(stbtt__matchpair(fc, nm, name, nlen, 3, -1)) return 1; + } + + return 0; +} +#ifdef STBTT_STREAM_TYPE + static int stbtt_FindMatchingFont_internal(STBTT_STREAM_TYPE font_collection, char * name_utf8, stbtt_int32 flags) +#else + static int stbtt_FindMatchingFont_internal(unsigned char * font_collection, char * name_utf8, stbtt_int32 flags) +#endif +{ + stbtt_int32 i; + for(i = 0;; ++i) { + stbtt_int32 off = stbtt_GetFontOffsetForIndex(font_collection, i); + if(off < 0) return off; +#ifdef STBTT_STREAM_TYPE + if(stbtt__matches(font_collection, off, (stbtt_uint8 *)name_utf8, flags)) +#else + if(stbtt__matches((stbtt_uint8 *)font_collection, off, (stbtt_uint8 *)name_utf8, flags)) +#endif + return off; + } +} + +#if defined(__GNUC__) || defined(__clang__) + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wcast-qual" +#endif + +#ifdef STBTT_STREAM_TYPE +STBTT_DEF int stbtt_BakeFontBitmap(STBTT_STREAM_TYPE data, int offset, + float pixel_height, unsigned char * pixels, int pw, int ph, + int first_char, int num_chars, stbtt_bakedchar * chardata); +#else +STBTT_DEF int stbtt_BakeFontBitmap(const unsigned char * data, int offset, + float pixel_height, unsigned char * pixels, int pw, int ph, + int first_char, int num_chars, stbtt_bakedchar * chardata); +#endif + +#ifdef STBTT_STREAM_TYPE +STBTT_DEF int stbtt_BakeFontBitmap(STBTT_STREAM_TYPE data, int offset, + float pixel_height, unsigned char * pixels, int pw, int ph, + int first_char, int num_chars, stbtt_bakedchar * chardata) +#else +STBTT_DEF int stbtt_BakeFontBitmap(const unsigned char * data, int offset, + float pixel_height, unsigned char * pixels, int pw, int ph, + int first_char, int num_chars, stbtt_bakedchar * chardata) +#endif +{ +#ifdef STBTT_STREAM_TYPE + return stbtt_BakeFontBitmap_internal(data, offset, pixel_height, pixels, pw, ph, first_char, num_chars, chardata); +#else + return stbtt_BakeFontBitmap_internal((unsigned char *)data, offset, pixel_height, pixels, pw, ph, first_char, num_chars, + chardata); +#endif +} +#ifdef STBTT_STREAM_TYPE +STBTT_DEF int stbtt_GetFontOffsetForIndex(STBTT_STREAM_TYPE data, int index) +{ + return stbtt_GetFontOffsetForIndex_internal(data, index); +} +#else +STBTT_DEF int stbtt_GetFontOffsetForIndex(const unsigned char * data, int index) +{ + return stbtt_GetFontOffsetForIndex_internal((unsigned char *)data, index); +} +#endif +#ifdef STBTT_STREAM_TYPE +STBTT_DEF int stbtt_GetNumberOfFonts(STBTT_STREAM_TYPE data) +{ + return stbtt_GetNumberOfFonts_internal(data); +} +#else +STBTT_DEF int stbtt_GetNumberOfFonts(const unsigned char * data) +{ + return stbtt_GetNumberOfFonts_internal((unsigned char *)data); +} +#endif +#ifdef STBTT_STREAM_TYPE +STBTT_DEF int stbtt_InitFont(stbtt_fontinfo * info, STBTT_STREAM_TYPE data, int offset) +{ + return stbtt_InitFont_internal(info, data, offset); +} +#else +STBTT_DEF int stbtt_InitFont(stbtt_fontinfo * info, const unsigned char * data, int offset) +{ + return stbtt_InitFont_internal(info, (unsigned char *)data, offset); +} +#endif +#ifdef STBTT_STREAM_TYPE +STBTT_DEF int stbtt_FindMatchingFont(STBTT_STREAM_TYPE fontdata, const char * name, int flags) +{ + return stbtt_FindMatchingFont_internal(fontdata, (char *)name, flags); +} +#else +STBTT_DEF int stbtt_FindMatchingFont(const unsigned char * fontdata, const char * name, int flags) +{ + return stbtt_FindMatchingFont_internal((unsigned char *)fontdata, (char *)name, flags); +} +#endif +#ifdef STBTT_STREAM_TYPE +STBTT_DEF int stbtt_CompareUTF8toUTF16_bigendian(const char * s1, int len1, STBTT_STREAM_TYPE s2, stbtt_uint32 s2offs, + int len2) +{ + return stbtt_CompareUTF8toUTF16_bigendian_internal((char *)s1, len1, s2, s2offs, len2); +} +#else +STBTT_DEF int stbtt_CompareUTF8toUTF16_bigendian(const char * s1, int len1, const char * s2, stbtt_uint32 s2offs, + int len2) +{ + return stbtt_CompareUTF8toUTF16_bigendian_internal((char *)s1, len1, (char *)s2, s2offs, len2); +} +#endif + +#if defined(__GNUC__) || defined(__clang__) + #pragma GCC diagnostic pop + #pragma GCC diagnostic pop +#endif + +#endif // STB_TRUETYPE_IMPLEMENTATION + +// FULL VERSION HISTORY +// +// 1.25 (2021-07-11) many fixes +// 1.24 (2020-02-05) fix warning +// 1.23 (2020-02-02) query SVG data for glyphs; query whole kerning table (but only kern not GPOS) +// 1.22 (2019-08-11) minimize missing-glyph duplication; fix kerning if both 'GPOS' and 'kern' are defined +// 1.21 (2019-02-25) fix warning +// 1.20 (2019-02-07) PackFontRange skips missing codepoints; GetScaleFontVMetrics() +// 1.19 (2018-02-11) OpenType GPOS kerning (horizontal only), STBTT_fmod +// 1.18 (2018-01-29) add missing function +// 1.17 (2017-07-23) make more arguments const; doc fix +// 1.16 (2017-07-12) SDF support +// 1.15 (2017-03-03) make more arguments const +// 1.14 (2017-01-16) num-fonts-in-TTC function +// 1.13 (2017-01-02) support OpenType fonts, certain Apple fonts +// 1.12 (2016-10-25) suppress warnings about casting away const with -Wcast-qual +// 1.11 (2016-04-02) fix unused-variable warning +// 1.10 (2016-04-02) allow user-defined fabs() replacement +// fix memory leak if fontsize=0.0 +// fix warning from duplicate typedef +// 1.09 (2016-01-16) warning fix; avoid crash on outofmem; use alloc userdata for PackFontRanges +// 1.08 (2015-09-13) document stbtt_Rasterize(); fixes for vertical & horizontal edges +// 1.07 (2015-08-01) allow PackFontRanges to accept arrays of sparse codepoints; +// allow PackFontRanges to pack and render in separate phases; +// fix stbtt_GetFontOFfsetForIndex (never worked for non-0 input?); +// fixed an assert() bug in the new rasterizer +// replace assert() with STBTT_assert() in new rasterizer +// 1.06 (2015-07-14) performance improvements (~35% faster on x86 and x64 on test machine) +// also more precise AA rasterizer, except if shapes overlap +// remove need for STBTT_sort +// 1.05 (2015-04-15) fix misplaced definitions for STBTT_STATIC +// 1.04 (2015-04-15) typo in example +// 1.03 (2015-04-12) STBTT_STATIC, fix memory leak in new packing, various fixes +// 1.02 (2014-12-10) fix various warnings & compile issues w/ stb_rect_pack, C++ +// 1.01 (2014-12-08) fix subpixel position when oversampling to exactly match +// non-oversampled; STBTT_POINT_SIZE for packed case only +// 1.00 (2014-12-06) add new PackBegin etc. API, w/ support for oversampling +// 0.99 (2014-09-18) fix multiple bugs with subpixel rendering (ryg) +// 0.9 (2014-08-07) support certain mac/iOS fonts without an MS platformID +// 0.8b (2014-07-07) fix a warning +// 0.8 (2014-05-25) fix a few more warnings +// 0.7 (2013-09-25) bugfix: subpixel glyph bug fixed in 0.5 had come back +// 0.6c (2012-07-24) improve documentation +// 0.6b (2012-07-20) fix a few more warnings +// 0.6 (2012-07-17) fix warnings; added stbtt_ScaleForMappingEmToPixels, +// stbtt_GetFontBoundingBox, stbtt_IsGlyphEmpty +// 0.5 (2011-12-09) bugfixes: +// subpixel glyph renderer computed wrong bounding box +// first vertex of shape can be off-curve (FreeSans) +// 0.4b (2011-12-03) fixed an error in the font baking example +// 0.4 (2011-12-01) kerning, subpixel rendering (tor) +// bugfixes for: +// codepoint-to-glyph conversion using table fmt=12 +// codepoint-to-glyph conversion using table fmt=4 +// stbtt_GetBakedQuad with non-square texture (Zer) +// updated Hello World! sample to use kerning and subpixel +// fixed some warnings +// 0.3 (2009-06-24) cmap fmt=12, compound shapes (MM) +// userdata, malloc-from-userdata, non-zero fill (stb) +// 0.2 (2009-03-11) Fix unsigned/signed char warnings +// 0.1 (2009-03-09) First public release +// + +/* +------------------------------------------------------------------------------ +This software is available under 2 licenses -- choose whichever you prefer. +------------------------------------------------------------------------------ +ALTERNATIVE A - MIT License +Copyright (c) 2017 Sean Barrett +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +------------------------------------------------------------------------------ +ALTERNATIVE B - Public Domain (www.unlicense.org) +This is free and unencumbered software released into the public domain. +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, +commercial or non-commercial, and by any means. +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and successors. We intend this dedication to be an +overt act of relinquishment in perpetuity of all present and future rights to +this software under copyright law. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +------------------------------------------------------------------------------ +*/ diff --git a/include/liblvgl/libs/tjpgd/lv_tjpgd.h b/include/liblvgl/libs/tjpgd/lv_tjpgd.h new file mode 100644 index 00000000..ec35800b --- /dev/null +++ b/include/liblvgl/libs/tjpgd/lv_tjpgd.h @@ -0,0 +1,45 @@ +/** + * @file lv_tjpgd.h + * + */ + +#ifndef LV_TJPGD_H +#define LV_TJPGD_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#if LV_USE_TJPGD + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void lv_tjpgd_init(void); + +void lv_tjpgd_deinit(void); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_TJPGD*/ + +#ifdef __cplusplus +} +#endif + +#endif /* LV_TJPGD_H */ diff --git a/include/liblvgl/libs/tjpgd/tjpgd.h b/include/liblvgl/libs/tjpgd/tjpgd.h new file mode 100644 index 00000000..887038a2 --- /dev/null +++ b/include/liblvgl/libs/tjpgd/tjpgd.h @@ -0,0 +1,108 @@ +/*----------------------------------------------------------------------------/ +/ TJpgDec - Tiny JPEG Decompressor R0.03 include file (C)ChaN, 2021 +/----------------------------------------------------------------------------*/ +#ifndef DEF_TJPGDEC +#define DEF_TJPGDEC + +#ifdef __cplusplus +extern "C" { +#endif + +#include "../../lv_conf_internal.h" +#include "tjpgdcnf.h" + +#if LV_USE_TJPGD + +#include +#include + +#if JD_FASTDECODE >= 1 +typedef int16_t jd_yuv_t; +#else +typedef uint8_t jd_yuv_t; +#endif + + +/* Error code */ +typedef enum { + JDR_OK = 0, /* 0: Succeeded */ + JDR_INTR, /* 1: Interrupted by output function */ + JDR_INP, /* 2: Device error or wrong termination of input stream */ + JDR_MEM1, /* 3: Insufficient memory pool for the image */ + JDR_MEM2, /* 4: Insufficient stream input buffer */ + JDR_PAR, /* 5: Parameter error */ + JDR_FMT1, /* 6: Data format error (may be broken data) */ + JDR_FMT2, /* 7: Right format but not supported */ + JDR_FMT3 /* 8: Not supported JPEG standard */ +} JRESULT; + + + +/* Rectangular region in the output image */ +typedef struct { + uint16_t left; /* Left end */ + uint16_t right; /* Right end */ + uint16_t top; /* Top end */ + uint16_t bottom; /* Bottom end */ +} JRECT; + + + +/* Decompressor object structure */ +typedef struct JDEC JDEC; +struct JDEC { + size_t dctr; /* Number of bytes available in the input buffer */ + uint8_t * dptr; /* Current data read ptr */ + uint8_t * inbuf; /* Bit stream input buffer */ + uint8_t dbit; /* Number of bits available in wreg or reading bit mask */ + uint8_t scale; /* Output scaling ratio */ + uint8_t msx, msy; /* MCU size in unit of block (width, height) */ + uint8_t qtid[3]; /* Quantization table ID of each component, Y, Cb, Cr */ + uint8_t ncomp; /* Number of color components 1:grayscale, 3:color */ + int16_t dcv[3]; /* Previous DC element of each component */ + uint16_t nrst; /* Restart interval */ + uint16_t rst; /* Restart count*/ + uint16_t rsc; /* Expected restart sequence ID*/ + uint16_t width, height; /* Size of the input image (pixel) */ + uint8_t * huffbits[2][2]; /* Huffman bit distribution tables [id][dcac] */ + uint16_t * huffcode[2][2]; /* Huffman code word tables [id][dcac] */ + uint8_t * huffdata[2][2]; /* Huffman decoded data tables [id][dcac] */ + int32_t * qttbl[4]; /* Dequantizer tables [id] */ +#if JD_FASTDECODE >= 1 + uint32_t wreg; /* Working shift register */ + uint8_t marker; /* Detected marker (0:None) */ +#if JD_FASTDECODE == 2 + uint8_t longofs[2][2]; /* Table offset of long code [id][dcac] */ + uint16_t * hufflut_ac[2]; /* Fast huffman decode tables for AC short code [id] */ + uint8_t * hufflut_dc[2]; /* Fast huffman decode tables for DC short code [id] */ +#endif +#endif + void * workbuf; /* Working buffer for IDCT and RGB output */ + jd_yuv_t * mcubuf; /* Working buffer for the MCU */ + void * pool; /* Pointer to available memory pool */ + void * pool_original; /* Pointer to original pool */ + size_t sz_pool; /* Size of memory pool (bytes available) */ + size_t (*infunc)(JDEC *, uint8_t *, size_t); /* Pointer to jpeg stream input function */ + void * device; /* Pointer to I/O device identifier for the session */ +}; + + + +/* TJpgDec API functions */ +JRESULT jd_prepare(JDEC * jd, size_t (*infunc)(JDEC *, uint8_t *, size_t), void * pool, size_t sz_pool, void * dev); + +JRESULT jd_decomp(JDEC * jd, int (*outfunc)(JDEC *, void *, JRECT *), uint8_t scale); + +JRESULT jd_mcu_load(JDEC * jd); + +JRESULT jd_mcu_output(JDEC * jd, int (*outfunc)(JDEC *, void *, JRECT *), unsigned int x, unsigned int y); + +JRESULT jd_restart(JDEC * jd, uint16_t rstn); + +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* _TJPGDEC */ diff --git a/include/liblvgl/extra/libs/sjpg/tjpgdcnf.h b/include/liblvgl/libs/tjpgd/tjpgdcnf.h similarity index 84% rename from include/liblvgl/extra/libs/sjpg/tjpgdcnf.h rename to include/liblvgl/libs/tjpgd/tjpgdcnf.h index 6d425e6f..dc27d6a9 100644 --- a/include/liblvgl/extra/libs/sjpg/tjpgdcnf.h +++ b/include/liblvgl/libs/tjpgd/tjpgdcnf.h @@ -2,29 +2,29 @@ /* TJpgDec System Configurations R0.03 */ /*----------------------------------------------*/ -#define JD_SZBUF 512 +#define JD_SZBUF 512 /* Specifies size of stream input buffer */ -#define JD_FORMAT 0 +#define JD_FORMAT 0 /* Specifies output pixel format. / 0: RGB888 (24-bit/pix) / 1: RGB565 (16-bit/pix) / 2: Grayscale (8-bit/pix) */ -#define JD_USE_SCALE 1 +#define JD_USE_SCALE 0 /* Switches output descaling feature. / 0: Disable / 1: Enable */ -#define JD_TBLCLIP 1 +#define JD_TBLCLIP 1 /* Use table conversion for saturation arithmetic. A bit faster, but increases 1 KB of code size. / 0: Disable / 1: Enable */ -#define JD_FASTDECODE 0 +#define JD_FASTDECODE 1 /* Optimization level / 0: Basic optimization. Suitable for 8/16-bit MCUs. / 1: + 32-bit barrel shifter. Suitable for 32-bit MCUs. diff --git a/include/liblvgl/llemu.h b/include/liblvgl/llemu.h index c3c4e6dc..ce9b00b2 100644 --- a/include/liblvgl/llemu.h +++ b/include/liblvgl/llemu.h @@ -435,4 +435,4 @@ void lcd_set_text_align(text_align_e_t alignment); } // namespace pros } // extern "C" #endif -#endif // _LIBLVGL_LLEMU_H_ +#endif // _LIBLVGL_LLEMU_H_ \ No newline at end of file diff --git a/include/liblvgl/llemu.hpp b/include/liblvgl/llemu.hpp index 78d31934..6e6d733d 100644 --- a/include/liblvgl/llemu.hpp +++ b/include/liblvgl/llemu.hpp @@ -370,4 +370,4 @@ std::uint8_t read_buttons(void); } // namespace pros -#endif // _LIBLVGL_LLEMU_HPP_ +#endif // _LIBLVGL_LLEMU_HPP_ \ No newline at end of file diff --git a/include/liblvgl/lv_api_map.h b/include/liblvgl/lv_api_map.h deleted file mode 100644 index c1b9e8d8..00000000 --- a/include/liblvgl/lv_api_map.h +++ /dev/null @@ -1,88 +0,0 @@ -/** - * @file lv_api_map.h - * - */ - -#ifndef LV_API_MAP_H -#define LV_API_MAP_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lvgl.h" - -/********************* - * DEFINES - *********************/ - -#define LV_NO_TASK_READY LV_NO_TIMER_READY -#define LV_INDEV_STATE_REL LV_INDEV_STATE_RELEASED -#define LV_INDEV_STATE_PR LV_INDEV_STATE_PRESSED -#define LV_OBJ_FLAG_SNAPABLE LV_OBJ_FLAG_SNAPPABLE /*Fixed typo*/ - -/********************** - * TYPEDEFS - **********************/ - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -static inline LV_ATTRIBUTE_TIMER_HANDLER uint32_t lv_task_handler(void) -{ - return lv_timer_handler(); -} - -/********************** - * MACROS - **********************/ - - -/********************** - * INLINE FUNCTIONS - **********************/ - -/** - * Move the object to the foreground. - * It will look like if it was created as the last child of its parent. - * It also means it can cover any of the siblings. - * @param obj pointer to an object - */ -static inline void lv_obj_move_foreground(lv_obj_t * obj) -{ - lv_obj_t * parent = lv_obj_get_parent(obj); - lv_obj_move_to_index(obj, lv_obj_get_child_cnt(parent) - 1); -} - -/** - * Move the object to the background. - * It will look like if it was created as the first child of its parent. - * It also means any of the siblings can cover the object. - * @param obj pointer to an object - */ -static inline void lv_obj_move_background(lv_obj_t * obj) -{ - lv_obj_move_to_index(obj, 0); -} - - - -/********************** - * DEPRECATED FUNCTIONS - **********************/ - -static inline uint32_t lv_obj_get_child_id(const struct _lv_obj_t * obj) -{ - LV_LOG_WARN("lv_obj_get_child_id(obj) is deprecated, please use lv_obj_get_index(obj)."); - return lv_obj_get_index(obj); -} - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_API_MAP_H*/ diff --git a/include/liblvgl/lv_api_map_v8.h b/include/liblvgl/lv_api_map_v8.h new file mode 100644 index 00000000..822450f5 --- /dev/null +++ b/include/liblvgl/lv_api_map_v8.h @@ -0,0 +1,328 @@ +/** + * @file lv_api_map_v8.h + * + */ + +#ifndef LV_API_MAP_V8_H +#define LV_API_MAP_V8_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "misc/lv_types.h" + +/********************* + * DEFINES + *********************/ + +#define LV_DISP_ROTATION_0 LV_DISPLAY_ROTATION_0 +#define LV_DISP_ROTATION_90 LV_DISPLAY_ROTATION_90 +#define LV_DISP_ROTATION_180 LV_DISPLAY_ROTATION_180 +#define LV_DISP_ROTATION_270 LV_DISPLAY_ROTATION_270 + +#define LV_DISP_RENDER_MODE_PARTIAL LV_DISPLAY_RENDER_MODE_PARTIAL +#define LV_DISP_RENDER_MODE_DIRECT LV_DISPLAY_RENDER_MODE_DIRECT +#define LV_DISP_RENDER_MODE_FULL LV_DISPLAY_RENDER_MODE_FULL + +#if LV_USE_BUTTONMATRIX +#define LV_BTNMATRIX_BTN_NONE LV_BUTTONMATRIX_BUTTON_NONE + +#define LV_BTNMATRIX_CTRL_HIDDEN LV_BUTTONMATRIX_CTRL_HIDDEN +#define LV_BTNMATRIX_CTRL_NO_REPEAT LV_BUTTONMATRIX_CTRL_NO_REPEAT +#define LV_BTNMATRIX_CTRL_DISABLED LV_BUTTONMATRIX_CTRL_DISABLED +#define LV_BTNMATRIX_CTRL_CHECKABLE LV_BUTTONMATRIX_CTRL_CHECKABLE +#define LV_BTNMATRIX_CTRL_CHECKED LV_BUTTONMATRIX_CTRL_CHECKED +#define LV_BTNMATRIX_CTRL_CLICK_TRIG LV_BUTTONMATRIX_CTRL_CLICK_TRIG +#define LV_BTNMATRIX_CTRL_POPOVER LV_BUTTONMATRIX_CTRL_POPOVER +#define LV_BTNMATRIX_CTRL_CUSTOM_1 LV_BUTTONMATRIX_CTRL_CUSTOM_1 +#define LV_BTNMATRIX_CTRL_CUSTOM_2 LV_BUTTONMATRIX_CTRL_CUSTOM_2 +#endif /* LV_USE_BUTTONMATRIX */ + +/********************** + * TYPEDEFS + **********************/ +typedef int32_t lv_coord_t; +typedef lv_result_t lv_res_t; +typedef lv_image_dsc_t lv_img_dsc_t; +typedef lv_display_t lv_disp_t; +typedef lv_display_rotation_t lv_disp_rotation_t; +typedef lv_display_render_mode_t lv_disp_render_t; +typedef lv_anim_completed_cb_t lv_anim_ready_cb_t; +typedef lv_screen_load_anim_t lv_scr_load_anim_t; + +#if LV_USE_BUTTONMATRIX +typedef lv_buttonmatrix_ctrl_t lv_btnmatrix_ctrl_t; +#endif /* LV_USE_BUTTONMATRIX */ + +#if LV_USE_IMAGEBUTTON +#define LV_IMGBTN_STATE_RELEASED LV_IMAGEBUTTON_STATE_RELEASED +#define LV_IMGBTN_STATE_PRESSED LV_IMAGEBUTTON_STATE_PRESSED +#define LV_IMGBTN_STATE_DISABLED LV_IMAGEBUTTON_STATE_DISABLED +#define LV_IMGBTN_STATE_CHECKED_RELEASED LV_IMAGEBUTTON_STATE_CHECKED_RELEASED +#define LV_IMGBTN_STATE_CHECKED_PRESSED LV_IMAGEBUTTON_STATE_CHECKED_PRESSED +#define LV_IMGBTN_STATE_CHECKED_DISABLED LV_IMAGEBUTTON_STATE_CHECKED_DISABLED +#endif /* LV_USE_IMAGEBUTTON */ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +static inline LV_ATTRIBUTE_TIMER_HANDLER uint32_t lv_task_handler(void) +{ + return lv_timer_handler(); +} + +/** + * Move the object to the foreground. + * It will look like if it was created as the last child of its parent. + * It also means it can cover any of the siblings. + * @param obj pointer to an object + */ +static inline void lv_obj_move_foreground(lv_obj_t * obj) +{ + lv_obj_t * parent = lv_obj_get_parent(obj); + if(!parent) { + LV_LOG_WARN("parent is NULL"); + return; + } + + lv_obj_move_to_index(obj, lv_obj_get_child_count(parent) - 1); +} + +/** + * Move the object to the background. + * It will look like if it was created as the first child of its parent. + * It also means any of the siblings can cover the object. + * @param obj pointer to an object + */ +static inline void lv_obj_move_background(lv_obj_t * obj) +{ + lv_obj_move_to_index(obj, 0); +} + +/********************** + * MACROS + **********************/ +#define LV_RES_OK LV_RESULT_OK +#define LV_RES_INV LV_RESULT_INVALID + +#define LV_INDEV_STATE_PR LV_INDEV_STATE_PRESSED +#define LV_INDEV_STATE_REL LV_INDEV_STATE_RELEASED + +#define lv_obj_del lv_obj_delete +#define lv_obj_del_async lv_obj_delete_async +#define lv_obj_clear_flag lv_obj_remove_flag +#define lv_obj_clear_state lv_obj_remove_state + +#define lv_indev_set_disp lv_indev_set_display +#define lv_indev_get_act lv_indev_active +#define lv_scr_act lv_screen_active +#define lv_disp_remove lv_display_delete +#define lv_disp_set_default lv_display_set_default +#define lv_disp_get_default lv_display_get_default +#define lv_disp_get_next lv_display_get_next +#define lv_disp_set_rotation lv_display_set_rotation +#define lv_disp_get_hor_res lv_display_get_horizontal_resolution +#define lv_disp_get_ver_res lv_display_get_vertical_resolution +#define lv_disp_get_physical_hor_res lv_display_get_physical_horizontal_resolution +#define lv_disp_get_physical_ver_res lv_display_get_physical_vertical_resolution +#define lv_disp_get_offset_x lv_display_get_offset_x +#define lv_disp_get_offset_y lv_display_get_offset_y +#define lv_disp_get_rotation lv_display_get_rotation +#define lv_disp_get_dpi lv_display_get_dpi +#define lv_disp_get_antialiasing lv_display_get_antialiasing +#define lv_disp_flush_ready lv_display_flush_ready +#define lv_disp_flush_is_last lv_display_flush_is_last +#define lv_disp_get_scr_act lv_display_get_screen_active +#define lv_disp_get_scr_prev lv_display_get_screen_prev +#define lv_disp_load_scr lv_screen_load +#define lv_scr_load lv_screen_load +#define lv_scr_load_anim lv_screen_load_anim +#define lv_disp_get_layer_top lv_display_get_layer_top +#define lv_disp_get_layer_sys lv_display_get_layer_sys +#define lv_disp_send_event lv_display_send_event +#define lv_disp_set_theme lv_display_set_theme +#define lv_disp_get_theme lv_display_get_theme +#define lv_disp_get_inactive_time lv_display_get_inactive_time +#define lv_disp_trig_activity lv_display_trigger_activity +#define lv_disp_enable_invalidation lv_display_enable_invalidation +#define lv_disp_is_invalidation_enabled lv_display_is_invalidation_enabled +#define lv_disp_refr_timer lv_display_refr_timer +#define lv_disp_get_refr_timer lv_display_get_refr_timer + +#define lv_timer_del lv_timer_delete + +#define lv_anim_del lv_anim_delete +#define lv_anim_del_all lv_anim_delete_all +#define lv_anim_set_ready_cb lv_anim_set_completed_cb + +#define lv_group_del lv_group_delete + +#if LV_USE_TEXTAREA +#define lv_txt_get_size lv_text_get_size +#define lv_txt_get_width lv_text_get_width +#endif /* LV_USE_TEXTAREA */ + +#if LV_USE_IMAGE +#define lv_img_create lv_image_create +#define lv_img_set_src lv_image_set_src +#define lv_img_set_offset_x lv_image_set_offset_x +#define lv_img_set_offset_y lv_image_set_offset_y +#define lv_img_set_angle lv_image_set_rotation +#define lv_img_set_pivot lv_image_set_pivot +#define lv_img_set_zoom lv_image_set_scale +#define lv_img_set_antialias lv_image_set_antialias +#define lv_img_get_src lv_image_get_src +#define lv_img_get_offset_x lv_image_get_offset_x +#define lv_img_get_offset_y lv_image_get_offset_y +#define lv_img_get_angle lv_image_get_rotation +#define lv_img_get_pivot lv_image_get_pivot +#define lv_img_get_zoom lv_image_get_scale +#define lv_img_get_antialias lv_image_get_antialias +#endif /* LV_USE_IMAGE */ + +#if LV_USE_IMAGEBUTTON +#define lv_imgbtn_create lv_imagebutton_create +#define lv_imgbtn_set_src lv_imagebutton_set_src +#define lv_imgbtn_set_state lv_imagebutton_set_state +#define lv_imgbtn_get_src_left lv_imagebutton_get_src_left +#define lv_imgbtn_get_src_middle lv_imagebutton_get_src_middle +#define lv_imgbtn_get_src_right lv_imagebutton_get_src_right +#endif /* LV_USE_IMAGEBUTTON */ + +#if LV_USE_LIST +#define lv_list_set_btn_text lv_list_set_button_text +#define lv_list_get_btn_text lv_list_get_button_text +#define lv_list_add_btn lv_list_add_button +#endif /* LV_USE_LIST */ + +#if LV_USE_BUTTON +#define lv_btn_create lv_button_create +#endif /* LV_USE_BUTTON */ + +#if LV_USE_BUTTONMATRIX +#define lv_btnmatrix_create lv_buttonmatrix_create +#define lv_btnmatrix_set_map lv_buttonmatrix_set_map +#define lv_btnmatrix_set_ctrl_map lv_buttonmatrix_set_ctrl_map +#define lv_btnmatrix_set_selected_btn lv_buttonmatrix_set_selected_button +#define lv_btnmatrix_set_btn_ctrl lv_buttonmatrix_set_button_ctrl +#define lv_btnmatrix_clear_btn_ctrl lv_buttonmatrix_clear_button_ctrl +#define lv_btnmatrix_set_btn_ctrl_all lv_buttonmatrix_set_button_ctrl_all +#define lv_btnmatrix_clear_btn_ctrl_all lv_buttonmatrix_clear_button_ctrl_all +#define lv_btnmatrix_set_btn_width lv_buttonmatrix_set_button_width +#define lv_btnmatrix_set_one_checked lv_buttonmatrix_set_one_checked +#define lv_btnmatrix_get_map lv_buttonmatrix_get_map +#define lv_btnmatrix_get_selected_btn lv_buttonmatrix_get_selected_button +#define lv_btnmatrix_get_btn_text lv_buttonmatrix_get_button_text +#define lv_btnmatrix_has_button_ctrl lv_buttonmatrix_has_button_ctrl +#define lv_btnmatrix_get_one_checked lv_buttonmatrix_get_one_checked +#endif /* LV_USE_BUTTONMATRIX */ + +#if LV_USE_TABVIEW +#define lv_tabview_get_tab_btns lv_tabview_get_tab_bar +#define lv_tabview_get_tab_act lv_tabview_get_tab_active +#define lv_tabview_set_act lv_tabview_set_active +#endif /* LV_USE_TABVIEW */ + +#if LV_USE_TILEVIEW +#define lv_tileview_get_tile_act lv_tileview_get_tile_active +#define lv_obj_set_tile_id lv_tileview_set_tile_by_index +#define lv_obj_set_tile lv_tileview_set_tile +#endif /* LV_USE_TILEVIEW */ + +#if LV_USE_ROLLER +#define lv_roller_set_visible_row_cnt lv_roller_set_visible_row_count +#define lv_roller_get_option_cnt lv_roller_get_option_count +#endif /* LV_USE_ROLLER */ + +#if LV_USE_TABLE +#define lv_table_set_col_cnt lv_table_set_column_count +#define lv_table_set_row_cnt lv_table_set_row_count +#define lv_table_get_col_cnt lv_table_get_column_count +#define lv_table_get_row_cnt lv_table_get_row_count +#define lv_table_set_col_width lv_table_set_column_width +#define lv_table_get_col_width lv_table_get_column_width +#endif /* LV_USE_TABLE */ + +#if LV_USE_DROPDOWN +#define lv_dropdown_get_option_cnt lv_dropdown_get_option_count +#endif /* LV_USE_DROPDOWN */ + +#define lv_obj_get_child_cnt lv_obj_get_child_count +#define lv_obj_get_disp lv_obj_get_display +#define lv_obj_delete_anim_ready_cb lv_obj_delete_anim_completed_cb + +#define LV_STYLE_ANIM_TIME LV_STYLE_ANIM_DURATION +#define LV_STYLE_IMG_OPA LV_STYLE_IMAGE_OPA +#define LV_STYLE_IMG_RECOLOR LV_STYLE_IMAGE_RECOLOR +#define LV_STYLE_IMG_RECOLOR_OPA LV_STYLE_IMAGE_RECOLOR_OPA +#define LV_STYLE_SHADOW_OFS_X LV_STYLE_SHADOW_OFFSET_X +#define LV_STYLE_SHADOW_OFS_Y LV_STYLE_SHADOW_OFFSET_Y +#define LV_STYLE_TRANSFORM_ANGLE LV_STYLE_TRANSFORM_ROTATION + +#define lv_obj_get_style_anim_time lv_obj_get_style_anim_duration +#define lv_obj_get_style_img_opa lv_obj_get_style_image_opa +#define lv_obj_get_style_img_recolor lv_obj_get_style_image_recolor +#define lv_obj_get_style_img_recolor_filtered lv_obj_get_style_image_recolor_filtered +#define lv_obj_get_style_img_recolor_opa lv_obj_get_style_image_recolor_opa +#define lv_obj_get_style_shadow_ofs_x lv_obj_get_style_shadow_offset_x +#define lv_obj_get_style_shadow_ofs_y lv_obj_get_style_shadow_offset_y +#define lv_obj_get_style_transform_angle lv_obj_get_style_transform_rotation +#define lv_obj_get_style_bg_img_src lv_obj_get_style_bg_image_src +#define lv_obj_get_style_bg_img_recolor lv_obj_get_style_bg_image_recolor +#define lv_obj_get_style_bg_img_recolor_opa lv_obj_get_style_bg_image_recolor_opa + +#define lv_obj_set_style_anim_time lv_obj_set_style_anim_duration +#define lv_obj_set_style_img_opa lv_obj_set_style_image_opa +#define lv_obj_set_style_img_recolor lv_obj_set_style_image_recolor +#define lv_obj_set_style_img_recolor_opa lv_obj_set_style_image_recolor_opa +#define lv_obj_set_style_shadow_ofs_x lv_obj_set_style_shadow_offset_x +#define lv_obj_set_style_shadow_ofs_y lv_obj_set_style_shadow_offset_y +#define lv_obj_set_style_transform_zoom lv_obj_set_style_transform_scale +#define lv_obj_set_style_transform_angle lv_obj_set_style_transform_rotation +#define lv_obj_set_style_bg_img_src lv_obj_set_style_bg_image_src +#define lv_obj_set_style_bg_img_recolor lv_obj_set_style_bg_image_recolor +#define lv_obj_set_style_bg_img_recolor_opa lv_obj_set_style_bg_image_recolor_opa + +#define lv_style_set_anim_time lv_style_set_anim_duration +#define lv_style_set_img_opa lv_style_set_image_opa +#define lv_style_set_img_recolor lv_style_set_image_recolor +#define lv_style_set_img_recolor_opa lv_style_set_image_recolor_opa +#define lv_style_set_shadow_ofs_x lv_style_set_shadow_offset_x +#define lv_style_set_shadow_ofs_y lv_style_set_shadow_offset_y +#define lv_style_set_transform_angle lv_style_set_transform_rotation +#define lv_style_set_transform_zoom lv_style_set_transform_scale +#define lv_style_set_bg_img_src lv_style_set_bg_image_src +#define lv_style_set_bg_img_recolor lv_style_set_bg_image_recolor +#define lv_style_set_bg_img_recolor_opa lv_style_set_bg_image_recolor_opa + +#if LV_USE_KEYBOARD +#define lv_keyboard_get_selected_btn lv_keyboard_get_selected_button +#define lv_keyboard_get_btn_text lv_keyboard_get_button_text +#endif /* LV_USE_KEYBOARD */ + +#define LV_ZOOM_NONE LV_SCALE_NONE + +#define lv_image_decoder_built_in_open lv_bin_decoder_open +#define lv_image_decoder_built_in_close lv_bin_decoder_close + +/********************** + * MACROS + **********************/ +/** Use this macro to declare an image in a C file*/ +#define LV_IMG_DECLARE(var_name) extern const lv_image_dsc_t var_name; + +/********************** + * DEPRECATED FUNCTIONS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_API_MAP_V8_H*/ diff --git a/include/liblvgl/lv_api_map_v9_0.h b/include/liblvgl/lv_api_map_v9_0.h new file mode 100644 index 00000000..b0d22cdf --- /dev/null +++ b/include/liblvgl/lv_api_map_v9_0.h @@ -0,0 +1,66 @@ +/** + * @file lv_api_map_v9_0.h + * + */ + +#ifndef LV_API_MAP_V9_0_H +#define LV_API_MAP_V9_0_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "misc/lv_types.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ +#define lv_image_set_align lv_image_set_inner_align +#define lv_image_get_align lv_image_get_inner_align + +#ifndef LV_DRAW_LAYER_SIMPLE_BUF_SIZE +#define LV_DRAW_LAYER_SIMPLE_BUF_SIZE LV_DRAW_SW_LAYER_SIMPLE_BUF_SIZE +#endif + +#define lv_button_bind_checked lv_obj_bind_checked + +#define LV_DRAW_BUF_DEFINE LV_DRAW_BUF_DEFINE_STATIC + +#define _lv_utils_bsearch lv_utils_bsearch +#define lv_draw_buf_align_user lv_draw_buf_align_ex +#define lv_draw_buf_create_user lv_draw_buf_create_ex +#define lv_draw_buf_width_to_stride_user lv_draw_buf_width_to_stride_ex +#define lv_draw_buf_dup_user lv_draw_buf_dup_ex + +#define lv_draw_buf_invalidate_cache_user(handlers, drawbuf, area) lv_draw_buf_invalidate_cache(drawbuf, area) +#define lv_draw_buf_flush_cache_user(handlers, drawbuf, area) lv_draw_buf_flush_cache(drawbuf, area) +#define lv_draw_buf_destroy_user(handlers, drawbuf) lv_draw_buf_destroy(drawbuf) + +/********************** + * MACROS + **********************/ + +/********************** + * DEPRECATED FUNCTIONS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_API_MAP_V9_0_H*/ diff --git a/include/liblvgl/lv_api_map_v9_1.h b/include/liblvgl/lv_api_map_v9_1.h new file mode 100644 index 00000000..a142b406 --- /dev/null +++ b/include/liblvgl/lv_api_map_v9_1.h @@ -0,0 +1,90 @@ +/** + * @file lv_api_map_v9_1.h + * + */ + +#ifndef LV_API_MAP_V9_1_H +#define LV_API_MAP_V9_1_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "misc/lv_types.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +/********************** + * MACROS + **********************/ + +#define _LV_EVENT_LAST LV_EVENT_LAST +#define _lv_event_code_t lv_event_code_t +#define _lv_event_mark_deleted lv_event_mark_deleted +#define lv_obj_add_event lv_obj_add_event_cb + +#define _LV_STYLE_LAST_BUILT_IN_PROP LV_STYLE_LAST_BUILT_IN_PROP +#define _LV_FLEX_REVERSE LV_FLEX_REVERSE +#define _LV_FLEX_WRAP LV_FLEX_WRAP +#define _LV_FLEX_COLUMN LV_FLEX_COLUMN + +#define _lv_area_is_equal lv_area_is_equal +#define _lv_area_is_in lv_area_is_in +#define _lv_area_intersect lv_area_intersect +#define _lv_area_is_point_on lv_area_is_point_on +#define _lv_area_join lv_area_join +#define _lv_image_buf_get_transformed_area lv_image_buf_get_transformed_area + +#define _lv_ll_init lv_ll_init +#define _lv_ll_ins_head lv_ll_ins_head +#define _lv_ll_ins_prev lv_ll_ins_prev +#define _lv_ll_ins_tail lv_ll_ins_tail +#define _lv_ll_get_head lv_ll_get_head +#define _lv_ll_get_tail lv_ll_get_tail +#define _lv_ll_get_next lv_ll_get_next +#define _lv_ll_get_prev lv_ll_get_prev +#define _lv_ll_get_len lv_ll_get_len +#define _lv_ll_move_before lv_ll_move_before +#define _lv_ll_is_empty lv_ll_is_empty +#define _lv_ll_clear lv_ll_clear +#define _lv_ll_remove lv_ll_remove +#define _lv_ll_chg_list lv_ll_chg_list +#define _LV_LL_READ LV_LL_READ +#define _LV_LL_READ_BACK LV_LL_READ_BACK + +#define _lv_obj_scroll_by_raw lv_obj_scroll_by_raw +#define _lv_obj_get_ext_draw_size lv_obj_get_ext_draw_size +#define _lv_indev_scroll_handler lv_indev_scroll_handler + +#define _lv_display_refr_timer lv_disp_refr_timer +#define _lv_disp_refr_timer lv_disp_refr_timer +#define _lv_disp_get_refr_timer lv_disp_get_refr_timer + +#define _lv_inv_area lv_inv_area + +/********************** + * DEPRECATED FUNCTIONS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_API_MAP_V9_0_H*/ diff --git a/include/liblvgl/lv_conf.h b/include/liblvgl/lv_conf.h index a7b8bf08..e4369823 100644 --- a/include/liblvgl/lv_conf.h +++ b/include/liblvgl/lv_conf.h @@ -83,7 +83,7 @@ typedef int16_t lv_coord_t; #define LV_MEM_CUSTOM 0 #if LV_MEM_CUSTOM == 0 /* Size of the memory used by `lv_mem_alloc` in bytes (>= 2kB)*/ -# define LV_MEM_SIZE (32U * 1024U) +# define LV_MEM_SIZE (10U * 1024U * 1024U) /* Compiler prefix for a big array declaration */ # define LV_MEM_ATTR @@ -217,6 +217,15 @@ e.g. "stm32f769xx.h" or "stm32f429xx.h" */ typedef void * lv_fs_drv_user_data_t; #endif +#define LV_USE_FS_STDIO 1 +#define LV_FS_STDIO_LETTER 'S' +#define LV_FS_STDIO_PATH "/usd/" +#define LV_FS_STDIO_CACHE_SIZE 0 + +#define LV_USE_LODEPNG 1 +#define LV_USE_GIF 1 +#define LV_USE_TJPGD 1 + /*1: Add a `user_data` to drivers and objects*/ #define LV_USE_USER_DATA 1 @@ -243,7 +252,7 @@ typedef void * lv_fs_drv_user_data_t; * With complex image decoders (e.g. PNG or JPG) caching can save the continuous open/decode of images. * However the opened images might consume additional RAM. * Set it to 0 to disable caching */ -#define LV_IMG_CACHE_DEF_SIZE 1 +#define LV_IMG_CACHE_DEF_SIZE (8U * 1024U) /*Declare the type of the user data of image decoder (can be e.g. `void *`, `int`, `struct`)*/ typedef void * lv_img_decoder_user_data_t; @@ -314,7 +323,7 @@ typedef void * lv_indev_drv_user_data_t; /*Type of user data in the i *===============*/ /*1: Enable the log module*/ -#define LV_USE_LOG 0 +#define LV_USE_LOG 1 #if LV_USE_LOG /* How important log should be added: * LV_LOG_LEVEL_TRACE A lot of logs to give detailed information @@ -327,7 +336,7 @@ typedef void * lv_indev_drv_user_data_t; /*Type of user data in the i /* 1: Print the log with 'printf'; * 0: user need to register a callback with `lv_log_register_print_cb`*/ -# define LV_LOG_PRINTF 0 +# define LV_LOG_PRINTF 1 #endif /*LV_USE_LOG*/ /*================= @@ -431,6 +440,7 @@ typedef void * lv_indev_drv_user_data_t; /*Type of user data in the i * glyphs cannot be processed by the library and won't be rendered. */ #define LV_USE_FONT_COMPRESSED 1 +#define LV_USE_QRCODE 0 /* Enable subpixel rendering */ #define LV_USE_FONT_SUBPX 1 @@ -580,6 +590,9 @@ typedef void * lv_obj_user_data_t; */ #define LV_USE_EXT_CLICK_AREA LV_EXT_CLICK_AREA_TINY +#define LV_USE_FLEX 1 +#define LV_USE_GRID 1 + /*================== * LV OBJ X USAGE *================*/ diff --git a/include/liblvgl/lv_conf_checker.h b/include/liblvgl/lv_conf_checker.h deleted file mode 100644 index 3a8ead51..00000000 --- a/include/liblvgl/lv_conf_checker.h +++ /dev/null @@ -1,664 +0,0 @@ -/** - * GENERATED FILE, DO NOT EDIT IT! - * @file lv_conf_checker.h - * Make sure all the defines of lv_conf.h have a default value -**/ - -#ifndef LV_CONF_CHECKER_H -#define LV_CONF_CHECKER_H -/*=================== - Dynamic memory - *===================*/ - -/* Memory size which will be used by the library - * to store the graphical objects and other data */ -#ifndef LV_MEM_CUSTOM -#define LV_MEM_CUSTOM 0 /*1: use custom malloc/free, 0: use the built-in lv_mem_alloc/lv_mem_free*/ -#endif -#if LV_MEM_CUSTOM == 0 -#ifndef LV_MEM_SIZE -# define LV_MEM_SIZE (64U * 1024U) /*Size memory used by `lv_mem_alloc` in bytes (>= 2kB)*/ -#endif -#ifndef LV_MEM_ATTR -# define LV_MEM_ATTR /*Complier prefix for big array declaration*/ -#endif -#ifndef LV_MEM_ADR -# define LV_MEM_ADR 0 /*Set an address for memory pool instead of allocation it as an array. Can be in external SRAM too.*/ -#endif -#ifndef LV_MEM_AUTO_DEFRAG -# define LV_MEM_AUTO_DEFRAG 1 /*Automatically defrag on free*/ -#endif -#else /*LV_MEM_CUSTOM*/ -#ifndef LV_MEM_CUSTOM_INCLUDE -# define LV_MEM_CUSTOM_INCLUDE /*Header for the dynamic memory function*/ -#endif -#ifndef LV_MEM_CUSTOM_ALLOC -# define LV_MEM_CUSTOM_ALLOC malloc /*Wrapper to malloc*/ -#endif -#ifndef LV_MEM_CUSTOM_FREE -# define LV_MEM_CUSTOM_FREE free /*Wrapper to free*/ -#endif -#endif /*LV_MEM_CUSTOM*/ - -/* Garbage Collector settings - * Used if lvgl is binded to higher language and the memory is managed by that language */ -#ifndef LV_ENABLE_GC -#define LV_ENABLE_GC 0 -#endif -#if LV_ENABLE_GC != 0 -#ifndef LV_MEM_CUSTOM_REALLOC -# define LV_MEM_CUSTOM_REALLOC your_realloc /*Wrapper to realloc*/ -#endif -#ifndef LV_MEM_CUSTOM_GET_SIZE -# define LV_MEM_CUSTOM_GET_SIZE your_mem_get_size /*Wrapper to lv_mem_get_size*/ -#endif -#ifndef LV_GC_INCLUDE -# define LV_GC_INCLUDE "gc.h" /*Include Garbage Collector related things*/ -#endif -#endif /* LV_ENABLE_GC */ - -/*=================== - Graphical settings - *===================*/ - -/* Horizontal and vertical resolution of the library.*/ -#ifndef LV_HOR_RES -#define LV_HOR_RES (480) -#endif -#ifndef LV_VER_RES -#define LV_VER_RES (320) -#endif - -/* Dot Per Inch: used to initialize default sizes. E.g. a button with width = LV_DPI / 2 -> half inch wide - * (Not so important, you can adjust it to modify default sizes and spaces)*/ -#ifndef LV_DPI -#define LV_DPI 100 -#endif - -/* Enable anti-aliasing (lines, and radiuses will be smoothed) */ -#ifndef LV_ANTIALIAS -#define LV_ANTIALIAS 1 /*1: Enable anti-aliasing*/ -#endif - -/*Screen refresh period in milliseconds*/ -#ifndef LV_REFR_PERIOD -#define LV_REFR_PERIOD 30 -#endif - -/*----------------- - * VDB settings - *----------------*/ - -/* VDB (Virtual Display Buffer) is an internal graphics buffer. - * The GUI will be drawn into this buffer first and then - * the buffer will be passed to your `disp_drv.disp_flush` function to - * copy it to your frame buffer. - * VDB is required for: buffered drawing, opacity, anti-aliasing and shadows - * Learn more: https://docs.littlevgl.com/#Drawing*/ - -/* Size of the VDB in pixels. Typical size: ~1/10 screen. Must be >= LV_HOR_RES - * Setting it to 0 will disable VDB and `disp_drv.disp_fill` and `disp_drv.disp_map` functions - * will be called to draw to the frame buffer directly*/ -#ifndef LV_VDB_SIZE -#define LV_VDB_SIZE ((LV_VER_RES * LV_HOR_RES) / 10) -#endif - - /* Bit-per-pixel of VDB. Useful for monochrome or non-standard color format displays. - * Special formats are handled with `disp_drv.vdb_wr`)*/ -#ifndef LV_VDB_PX_BPP -#define LV_VDB_PX_BPP LV_COLOR_SIZE /*LV_COLOR_SIZE comes from LV_COLOR_DEPTH below to set 8, 16 or 32 bit pixel size automatically */ -#endif - - /* Place VDB to a specific address (e.g. in external RAM) - * 0: allocate automatically into RAM - * LV_VDB_ADR_INV: to replace it later with `lv_vdb_set_adr()`*/ -#ifndef LV_VDB_ADR -#define LV_VDB_ADR 0 -#endif - -/* Use two Virtual Display buffers (VDB) to parallelize rendering and flushing - * The flushing should use DMA to write the frame buffer in the background */ -#ifndef LV_VDB_DOUBLE -#define LV_VDB_DOUBLE 0 -#endif - -/* Place VDB2 to a specific address (e.g. in external RAM) - * 0: allocate automatically into RAM - * LV_VDB_ADR_INV: to replace it later with `lv_vdb_set_adr()`*/ -#ifndef LV_VDB2_ADR -#define LV_VDB2_ADR 0 -#endif - -/* Using true double buffering in `disp_drv.disp_flush` you will always get the image of the whole screen. - * Your only task is to set the rendered image (`color_p` parameter) as frame buffer address or send it to your display. - * The best if you do in the blank period of you display to avoid tearing effect. - * Requires: - * - LV_VDB_SIZE = LV_HOR_RES * LV_VER_RES - * - LV_VDB_DOUBLE = 1 - */ -#ifndef LV_VDB_TRUE_DOUBLE_BUFFERED -#define LV_VDB_TRUE_DOUBLE_BUFFERED 0 -#endif - -/*================= - Misc. setting - *=================*/ - -/*Input device settings*/ -#ifndef LV_INDEV_READ_PERIOD -#define LV_INDEV_READ_PERIOD 50 /*Input device read period in milliseconds*/ -#endif -#ifndef LV_INDEV_POINT_MARKER -#define LV_INDEV_POINT_MARKER 0 /*Mark the pressed points (required: USE_LV_REAL_DRAW = 1)*/ -#endif -#ifndef LV_INDEV_DRAG_LIMIT -#define LV_INDEV_DRAG_LIMIT 10 /*Drag threshold in pixels */ -#endif -#ifndef LV_INDEV_DRAG_THROW -#define LV_INDEV_DRAG_THROW 20 /*Drag throw slow-down in [%] (must be > 0). Greater value means faster slow-down */ -#endif -#ifndef LV_INDEV_LONG_PRESS_TIME -#define LV_INDEV_LONG_PRESS_TIME 400 /*Long press time in milliseconds*/ -#endif -#ifndef LV_INDEV_LONG_PRESS_REP_TIME -#define LV_INDEV_LONG_PRESS_REP_TIME 100 /*Repeated trigger period in long press [ms] */ -#endif - -/*Color settings*/ -#ifndef LV_COLOR_DEPTH -#define LV_COLOR_DEPTH 16 /*Color depth: 1/8/16/32*/ -#endif -#ifndef LV_COLOR_16_SWAP -#define LV_COLOR_16_SWAP 0 /*Swap the 2 bytes of RGB565 color. Useful if the display has a 8 bit interface (e.g. SPI)*/ -#endif -#ifndef LV_COLOR_SCREEN_TRANSP -#define LV_COLOR_SCREEN_TRANSP 0 /*1: Enable screen transparency. Useful for OSD or other overlapping GUIs. Requires ARGB8888 colors*/ -#endif -#ifndef LV_COLOR_TRANSP -#define LV_COLOR_TRANSP LV_COLOR_LIME /*Images pixels with this color will not be drawn (with chroma keying)*/ -#endif - -/*Text settings*/ -#ifndef LV_TXT_UTF8 -#define LV_TXT_UTF8 1 /*Enable UTF-8 coded Unicode character usage */ -#endif -#ifndef LV_TXT_BREAK_CHARS -#define LV_TXT_BREAK_CHARS " ,.;:-_" /*Can break texts on these chars*/ -#endif -#ifndef LV_TXT_LINE_BREAK_LONG_LEN -#define LV_TXT_LINE_BREAK_LONG_LEN 12 /* If a character is at least this long, will break wherever "prettiest" */ -#endif -#ifndef LV_TXT_LINE_BREAK_LONG_PRE_MIN_LEN -#define LV_TXT_LINE_BREAK_LONG_PRE_MIN_LEN 3 /* Minimum number of characters of a word to put on a line before a break */ -#endif -#ifndef LV_TXT_LINE_BREAK_LONG_POST_MIN_LEN -#define LV_TXT_LINE_BREAK_LONG_POST_MIN_LEN 1 /* Minimum number of characters of a word to put on a line after a break */ -#endif - -/*Feature usage*/ -#ifndef USE_LV_ANIMATION -#define USE_LV_ANIMATION 1 /*1: Enable all animations*/ -#endif -#ifndef USE_LV_SHADOW -#define USE_LV_SHADOW 1 /*1: Enable shadows*/ -#endif -#ifndef USE_LV_GROUP -#define USE_LV_GROUP 1 /*1: Enable object groups (for keyboards)*/ -#endif -#ifndef USE_LV_GPU -#define USE_LV_GPU 1 /*1: Enable GPU interface*/ -#endif -#ifndef USE_LV_REAL_DRAW -#define USE_LV_REAL_DRAW 1 /*1: Enable function which draw directly to the frame buffer instead of VDB (required if LV_VDB_SIZE = 0)*/ -#endif -#ifndef USE_LV_FILESYSTEM -#define USE_LV_FILESYSTEM 1 /*1: Enable file system (might be required for images*/ -#endif -#ifndef USE_LV_MULTI_LANG -#define USE_LV_MULTI_LANG 0 /* Number of languages for labels to store (0: to disable this feature)*/ -#endif - -/*Compiler settings*/ -#ifndef LV_ATTRIBUTE_TICK_INC -#define LV_ATTRIBUTE_TICK_INC /* Define a custom attribute to `lv_tick_inc` function */ -#endif -#ifndef LV_ATTRIBUTE_TASK_HANDLER -#define LV_ATTRIBUTE_TASK_HANDLER /* Define a custom attribute to `lv_task_handler` function */ -#endif -#ifndef LV_ATTRIBUTE_MEM_ALIGN -#define LV_ATTRIBUTE_MEM_ALIGN /* With size optimization (-Os) the compiler might not align data to 4 or 8 byte boundary. This alignment will be explicitly applied where needed.*/ -#endif -#ifndef LV_COMPILER_VLA_SUPPORTED -#define LV_COMPILER_VLA_SUPPORTED 1 /* 1: Variable length array is supported*/ -#endif -#ifndef LV_COMPILER_NON_CONST_INIT_SUPPORTED -#define LV_COMPILER_NON_CONST_INIT_SUPPORTED 1 /* 1: Initialization with non constant values are supported */ -#endif - -/*HAL settings*/ -#ifndef LV_TICK_CUSTOM -#define LV_TICK_CUSTOM 0 /*1: use a custom tick source (removing the need to manually update the tick with `lv_tick_inc`) */ -#endif -#if LV_TICK_CUSTOM == 1 -#ifndef LV_TICK_CUSTOM_INCLUDE -#define LV_TICK_CUSTOM_INCLUDE "something.h" /*Header for the sys time function*/ -#endif -#ifndef LV_TICK_CUSTOM_SYS_TIME_EXPR -#define LV_TICK_CUSTOM_SYS_TIME_EXPR (millis()) /*Expression evaluating to current systime in ms*/ -#endif -#endif /*LV_TICK_CUSTOM*/ - - -/*Log settings*/ -#ifndef USE_LV_LOG -#define USE_LV_LOG 1 /*Enable/disable the log module*/ -#endif -#if USE_LV_LOG -/* How important log should be added: - * LV_LOG_LEVEL_TRACE A lot of logs to give detailed information - * LV_LOG_LEVEL_INFO Log important events - * LV_LOG_LEVEL_WARN Log if something unwanted happened but didn't caused problem - * LV_LOG_LEVEL_ERROR Only critical issue, when the system may fail - */ -#ifndef LV_LOG_LEVEL -# define LV_LOG_LEVEL LV_LOG_LEVEL_WARN -#endif -/* 1: Print the log with 'printf'; 0: user need to register a callback*/ - -#ifndef LV_LOG_PRINTF -# define LV_LOG_PRINTF 0 -#endif -#endif /*USE_LV_LOG*/ - -/*================ - * THEME USAGE - *================*/ -#ifndef LV_THEME_LIVE_UPDATE -#define LV_THEME_LIVE_UPDATE 1 /*1: Allow theme switching at run time. Uses 8..10 kB of RAM*/ -#endif - -#ifndef USE_LV_THEME_TEMPL -#define USE_LV_THEME_TEMPL 0 /*Just for test*/ -#endif -#ifndef USE_LV_THEME_DEFAULT -#define USE_LV_THEME_DEFAULT 1 /*Built mainly from the built-in styles. Consumes very few RAM*/ -#endif -#ifndef USE_LV_THEME_ALIEN -#define USE_LV_THEME_ALIEN 1 /*Dark futuristic theme*/ -#endif -#ifndef USE_LV_THEME_NIGHT -#define USE_LV_THEME_NIGHT 1 /*Dark elegant theme*/ -#endif -#ifndef USE_LV_THEME_MONO -#define USE_LV_THEME_MONO 1 /*Mono color theme for monochrome displays*/ -#endif -#ifndef USE_LV_THEME_MATERIAL -#define USE_LV_THEME_MATERIAL 1 /*Flat theme with bold colors and light shadows*/ -#endif -#ifndef USE_LV_THEME_ZEN -#define USE_LV_THEME_ZEN 1 /*Peaceful, mainly light theme */ -#endif -#ifndef USE_LV_THEME_NEMO -#define USE_LV_THEME_NEMO 1 /*Water-like theme based on the movie "Finding Nemo"*/ -#endif - -/*================== - * FONT USAGE - *===================*/ - -/* More info about fonts: https://docs.littlevgl.com/#Fonts - * To enable a built-in font use 1,2,4 or 8 values - * which will determine the bit-per-pixel. Higher value means smoother fonts */ -#ifndef USE_LV_FONT_DEJAVU_10 -#define USE_LV_FONT_DEJAVU_10 4 -#endif -#ifndef USE_LV_FONT_DEJAVU_10_LATIN_SUP -#define USE_LV_FONT_DEJAVU_10_LATIN_SUP 4 -#endif -#ifndef USE_LV_FONT_DEJAVU_10_CYRILLIC -#define USE_LV_FONT_DEJAVU_10_CYRILLIC 4 -#endif -#ifndef USE_LV_FONT_SYMBOL_10 -#define USE_LV_FONT_SYMBOL_10 4 -#endif - -#ifndef USE_LV_FONT_DEJAVU_20 -#define USE_LV_FONT_DEJAVU_20 4 -#endif -#ifndef USE_LV_FONT_DEJAVU_20_LATIN_SUP -#define USE_LV_FONT_DEJAVU_20_LATIN_SUP 4 -#endif -#ifndef USE_LV_FONT_DEJAVU_20_CYRILLIC -#define USE_LV_FONT_DEJAVU_20_CYRILLIC 4 -#endif -#ifndef USE_LV_FONT_SYMBOL_20 -#define USE_LV_FONT_SYMBOL_20 4 -#endif - -#ifndef USE_LV_FONT_DEJAVU_30 -#define USE_LV_FONT_DEJAVU_30 4 -#endif -#ifndef USE_LV_FONT_DEJAVU_30_LATIN_SUP -#define USE_LV_FONT_DEJAVU_30_LATIN_SUP 4 -#endif -#ifndef USE_LV_FONT_DEJAVU_30_CYRILLIC -#define USE_LV_FONT_DEJAVU_30_CYRILLIC 4 -#endif -#ifndef USE_LV_FONT_SYMBOL_30 -#define USE_LV_FONT_SYMBOL_30 4 -#endif - -#ifndef USE_LV_FONT_DEJAVU_40 -#define USE_LV_FONT_DEJAVU_40 4 -#endif -#ifndef USE_LV_FONT_DEJAVU_40_LATIN_SUP -#define USE_LV_FONT_DEJAVU_40_LATIN_SUP 4 -#endif -#ifndef USE_LV_FONT_DEJAVU_40_CYRILLIC -#define USE_LV_FONT_DEJAVU_40_CYRILLIC 4 -#endif -#ifndef USE_LV_FONT_SYMBOL_40 -#define USE_LV_FONT_SYMBOL_40 4 -#endif - -#ifndef USE_LV_FONT_MONOSPACE_8 -#define USE_LV_FONT_MONOSPACE_8 1 -#endif - -/* Optionally declare your custom fonts here. - * You can use these fonts as default font too - * and they will be available globally. E.g. - * #define LV_FONT_CUSTOM_DECLARE LV_FONT_DECLARE(my_font_1) \ - * LV_FONT_DECLARE(my_font_2) \ - */ -#ifndef LV_FONT_CUSTOM_DECLARE -#define LV_FONT_CUSTOM_DECLARE -#endif - - -#ifndef LV_FONT_DEFAULT -#define LV_FONT_DEFAULT &lv_font_dejavu_20 /*Always set a default font from the built-in fonts*/ -#endif - -/*=================== - * LV_OBJ SETTINGS - *==================*/ -#ifndef LV_OBJ_FREE_NUM_TYPE -#define LV_OBJ_FREE_NUM_TYPE uint32_t /*Type of free number attribute (comment out disable free number)*/ -#endif -#ifndef LV_OBJ_FREE_PTR -#define LV_OBJ_FREE_PTR 1 /*Enable the free pointer attribute*/ -#endif -#ifndef LV_OBJ_REALIGN -#define LV_OBJ_REALIGN 1 /*Enable `lv_obj_realaign()` based on `lv_obj_align()` parameters*/ -#endif - -/*================== - * LV OBJ X USAGE - *================*/ -/* - * Documentation of the object types: https://docs.littlevgl.com/#Object-types - */ - -/***************** - * Simple object - *****************/ - -/*Label (dependencies: -*/ -#ifndef USE_LV_LABEL -#define USE_LV_LABEL 1 -#endif -#if USE_LV_LABEL != 0 -#ifndef LV_LABEL_SCROLL_SPEED -# define LV_LABEL_SCROLL_SPEED 25 /*Hor, or ver. scroll speed [px/sec] in 'LV_LABEL_LONG_SCROLL/ROLL' mode*/ -#endif -#endif - -/*Image (dependencies: lv_label*/ -#ifndef USE_LV_IMG -#define USE_LV_IMG 1 -#endif -#if USE_LV_IMG != 0 -#ifndef LV_IMG_CF_INDEXED -# define LV_IMG_CF_INDEXED 1 /*Enable indexed (palette) images*/ -#endif -#ifndef LV_IMG_CF_ALPHA -# define LV_IMG_CF_ALPHA 1 /*Enable alpha indexed images*/ -#endif -#endif - -/*Line (dependencies: -*/ -#ifndef USE_LV_LINE -#define USE_LV_LINE 1 -#endif - -/*Arc (dependencies: -)*/ -#ifndef USE_LV_ARC -#define USE_LV_ARC 1 -#endif - -/******************* - * Container objects - *******************/ - -/*Container (dependencies: -*/ -#ifndef USE_LV_CONT -#define USE_LV_CONT 1 -#endif - -/*Page (dependencies: lv_cont)*/ -#ifndef USE_LV_PAGE -#define USE_LV_PAGE 1 -#endif - -/*Window (dependencies: lv_cont, lv_btn, lv_label, lv_img, lv_page)*/ -#ifndef USE_LV_WIN -#define USE_LV_WIN 1 -#endif - -/*Tab (dependencies: lv_page, lv_btnm)*/ -#ifndef USE_LV_TABVIEW -#define USE_LV_TABVIEW 1 -#endif -# if USE_LV_TABVIEW != 0 -#ifndef LV_TABVIEW_ANIM_TIME -# define LV_TABVIEW_ANIM_TIME 300 /*Time of slide animation [ms] (0: no animation)*/ -#endif -#endif - -/*Tileview (dependencies: lv_page) */ -#ifndef USE_LV_TILEVIEW -#define USE_LV_TILEVIEW 1 -#endif -#if USE_LV_TILEVIEW -#ifndef LV_TILEVIEW_ANIM_TIME -# define LV_TILEVIEW_ANIM_TIME 300 /*Time of slide animation [ms] (0: no animation)*/ -#endif -#endif - -/************************* - * Data visualizer objects - *************************/ - -/*Bar (dependencies: -)*/ -#ifndef USE_LV_BAR -#define USE_LV_BAR 1 -#endif - -/*Line meter (dependencies: *;)*/ -#ifndef USE_LV_LMETER -#define USE_LV_LMETER 1 -#endif - -/*Gauge (dependencies:lv_bar, lv_lmeter)*/ -#ifndef USE_LV_GAUGE -#define USE_LV_GAUGE 1 -#endif - -/*Chart (dependencies: -)*/ -#ifndef USE_LV_CHART -#define USE_LV_CHART 1 -#endif - -/*Table (dependencies: lv_label)*/ -#ifndef USE_LV_TABLE -#define USE_LV_TABLE 1 -#endif -#if USE_LV_TABLE -#ifndef LV_TABLE_COL_MAX -# define LV_TABLE_COL_MAX 12 -#endif -#endif - -/*LED (dependencies: -)*/ -#ifndef USE_LV_LED -#define USE_LV_LED 1 -#endif - -/*Message box (dependencies: lv_rect, lv_btnm, lv_label)*/ -#ifndef USE_LV_MBOX -#define USE_LV_MBOX 1 -#endif - -/*Text area (dependencies: lv_label, lv_page)*/ -#ifndef USE_LV_TA -#define USE_LV_TA 1 -#endif -#if USE_LV_TA != 0 -#ifndef LV_TA_CURSOR_BLINK_TIME -# define LV_TA_CURSOR_BLINK_TIME 400 /*ms*/ -#endif -#ifndef LV_TA_PWD_SHOW_TIME -# define LV_TA_PWD_SHOW_TIME 1500 /*ms*/ -#endif -#endif - -/*Spinbox (dependencies: lv_ta)*/ -#ifndef USE_LV_SPINBOX -#define USE_LV_SPINBOX 1 -#endif - -/*Calendar (dependencies: -)*/ -#ifndef USE_LV_CALENDAR -#define USE_LV_CALENDAR 1 -#endif - -/*Preload (dependencies: lv_arc)*/ -#ifndef USE_LV_PRELOAD -#define USE_LV_PRELOAD 1 -#endif -#if USE_LV_PRELOAD != 0 -#ifndef LV_PRELOAD_DEF_ARC_LENGTH -# define LV_PRELOAD_DEF_ARC_LENGTH 60 /*[deg]*/ -#endif -#ifndef LV_PRELOAD_DEF_SPIN_TIME -# define LV_PRELOAD_DEF_SPIN_TIME 1000 /*[ms]*/ -#endif -#ifndef LV_PRELOAD_DEF_ANIM -# define LV_PRELOAD_DEF_ANIM LV_PRELOAD_TYPE_SPINNING_ARC -#endif -#endif - -/*Canvas (dependencies: lv_img)*/ -#ifndef USE_LV_CANVAS -#define USE_LV_CANVAS 1 -#endif -/************************* - * User input objects - *************************/ - -/*Button (dependencies: lv_cont*/ -#ifndef USE_LV_BTN -#define USE_LV_BTN 1 -#endif -#if USE_LV_BTN != 0 -#ifndef LV_BTN_INK_EFFECT -# define LV_BTN_INK_EFFECT 1 /*Enable button-state animations - draw a circle on click (dependencies: USE_LV_ANIMATION)*/ -#endif -#endif - -/*Image Button (dependencies: lv_btn*/ -#ifndef USE_LV_IMGBTN -#define USE_LV_IMGBTN 1 -#endif -#if USE_LV_IMGBTN -#ifndef LV_IMGBTN_TILED -# define LV_IMGBTN_TILED 0 /*1: The imgbtn requires left, mid and right parts and the width can be set freely*/ -#endif -#endif - -/*Button matrix (dependencies: -)*/ -#ifndef USE_LV_BTNM -#define USE_LV_BTNM 1 -#endif - -/*Keyboard (dependencies: lv_btnm)*/ -#ifndef USE_LV_KB -#define USE_LV_KB 1 -#endif - -/*Check box (dependencies: lv_btn, lv_label)*/ -#ifndef USE_LV_CB -#define USE_LV_CB 1 -#endif - -/*List (dependencies: lv_page, lv_btn, lv_label, (lv_img optionally for icons ))*/ -#ifndef USE_LV_LIST -#define USE_LV_LIST 1 -#endif -#if USE_LV_LIST != 0 -#ifndef LV_LIST_FOCUS_TIME -# define LV_LIST_FOCUS_TIME 100 /*Default animation time of focusing to a list element [ms] (0: no animation) */ -#endif -#endif - -/*Drop down list (dependencies: lv_page, lv_label, lv_symbol_def.h)*/ -#ifndef USE_LV_DDLIST -#define USE_LV_DDLIST 1 -#endif -#if USE_LV_DDLIST != 0 -#ifndef LV_DDLIST_ANIM_TIME -# define LV_DDLIST_ANIM_TIME 200 /*Open and close default animation time [ms] (0: no animation)*/ -#endif -#endif - -/*Roller (dependencies: lv_ddlist)*/ -#ifndef USE_LV_ROLLER -#define USE_LV_ROLLER 1 -#endif -#if USE_LV_ROLLER != 0 -#ifndef LV_ROLLER_ANIM_TIME -# define LV_ROLLER_ANIM_TIME 200 /*Focus animation time [ms] (0: no animation)*/ -#endif -#endif - -/*Slider (dependencies: lv_bar)*/ -#ifndef USE_LV_SLIDER -#define USE_LV_SLIDER 1 -#endif - -/*Switch (dependencies: lv_slider)*/ -#ifndef USE_LV_SW -#define USE_LV_SW 1 -#endif - -/************************* - * Non-user section - *************************/ - -#if LV_INDEV_DRAG_THROW <= 0 -#warning "LV_INDEV_DRAG_THROW must be greater than 0" -#undef LV_INDEV_DRAG_THROW -#ifndef LV_INDEV_DRAG_THROW -#define LV_INDEV_DRAG_THROW 1 -#endif -#endif - -#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) /* Disable warnings for Visual Studio*/ -#ifndef _CRT_SECURE_NO_WARNINGS -# define _CRT_SECURE_NO_WARNINGS -#endif -#endif - - -#endif /*LV_CONF_CHECKER_H*/ diff --git a/include/liblvgl/lv_conf_internal.h b/include/liblvgl/lv_conf_internal.h index 97807fe3..a8e28b15 100644 --- a/include/liblvgl/lv_conf_internal.h +++ b/include/liblvgl/lv_conf_internal.h @@ -1,24 +1,43 @@ /** * GENERATED FILE, DO NOT EDIT IT! * @file lv_conf_internal.h - * Make sure all the defines of lv_conf.h have a default value -**/ + * This file ensures all defines of lv_conf.h have a default value. + */ #ifndef LV_CONF_INTERNAL_H #define LV_CONF_INTERNAL_H /* clang-format off */ -#include - -/* Handle special Kconfig options */ +/* Config options */ +#define LV_OS_NONE 0 +#define LV_OS_PTHREAD 1 +#define LV_OS_FREERTOS 2 +#define LV_OS_CMSIS_RTOS2 3 +#define LV_OS_RTTHREAD 4 +#define LV_OS_WINDOWS 5 +#define LV_OS_MQX 6 +#define LV_OS_CUSTOM 255 + +#define LV_STDLIB_BUILTIN 0 +#define LV_STDLIB_CLIB 1 +#define LV_STDLIB_MICROPYTHON 2 +#define LV_STDLIB_RTTHREAD 3 +#define LV_STDLIB_CUSTOM 255 + +#define LV_DRAW_SW_ASM_NONE 0 +#define LV_DRAW_SW_ASM_NEON 1 +#define LV_DRAW_SW_ASM_HELIUM 2 +#define LV_DRAW_SW_ASM_CUSTOM 255 + +/** Handle special Kconfig options. */ #ifndef LV_KCONFIG_IGNORE #include "lv_conf_kconfig.h" - #ifdef CONFIG_LV_CONF_SKIP + #if defined(CONFIG_LV_CONF_SKIP) && !defined(LV_CONF_SKIP) #define LV_CONF_SKIP #endif #endif -/*If "lv_conf.h" is available from here try to use it later.*/ +/* If "lv_conf.h" is available from here try to use it later. */ #ifdef __has_include #if __has_include("lv_conf.h") #ifndef LV_CONF_INCLUDE_SIMPLE @@ -27,18 +46,18 @@ #endif #endif -/*If lv_conf.h is not skipped include it*/ -#ifndef LV_CONF_SKIP - #ifdef LV_CONF_PATH /*If there is a path defined for lv_conf.h use it*/ +/* If lv_conf.h is not skipped, include it. */ +#if !defined(LV_CONF_SKIP) || defined(LV_CONF_PATH) + #ifdef LV_CONF_PATH /* If there is a path defined for lv_conf.h, use it */ #define __LV_TO_STR_AUX(x) #x #define __LV_TO_STR(x) __LV_TO_STR_AUX(x) #include __LV_TO_STR(LV_CONF_PATH) #undef __LV_TO_STR_AUX #undef __LV_TO_STR - #elif defined(LV_CONF_INCLUDE_SIMPLE) /*Or simply include lv_conf.h is enabled*/ + #elif defined(LV_CONF_INCLUDE_SIMPLE) /* Or simply include lv_conf.h is enabled. */ #include "lv_conf.h" #else - #include "../../lv_conf.h" /*Else assume lv_conf.h is next to the lvgl folder*/ + #include "../../lv_conf.h" /* Else assume lv_conf.h is next to the lvgl folder. */ #endif #if !defined(LV_CONF_H) && !defined(LV_CONF_SUPPRESS_DEFINE_CHECK) /* #include will sometimes silently fail when __has_include is used */ @@ -48,20 +67,23 @@ #endif #ifdef CONFIG_LV_COLOR_DEPTH - #define _LV_KCONFIG_PRESENT + #define LV_KCONFIG_PRESENT #endif /*---------------------------------- * Start parsing lv_conf_template.h -----------------------------------*/ -#include +/* If you need to include anything here, do it inside the `__ASSEMBLY__` guard */ +#if 0 && defined(__ASSEMBLY__) +#include "my_include.h" +#endif /*==================== COLOR SETTINGS *====================*/ -/*Color depth: 1 (1 byte per pixel), 8 (RGB332), 16 (RGB565), 32 (ARGB8888)*/ +/** Color depth: 1 (I1), 8 (L8), 16 (RGB565), 24 (RGB888), 32 (XRGB8888) */ #ifndef LV_COLOR_DEPTH #ifdef CONFIG_LV_COLOR_DEPTH #define LV_COLOR_DEPTH CONFIG_LV_COLOR_DEPTH @@ -70,76 +92,126 @@ #endif #endif -/*Swap the 2 bytes of RGB565 color. Useful if the display has an 8-bit interface (e.g. SPI)*/ -#ifndef LV_COLOR_16_SWAP - #ifdef CONFIG_LV_COLOR_16_SWAP - #define LV_COLOR_16_SWAP CONFIG_LV_COLOR_16_SWAP +/*========================= + STDLIB WRAPPER SETTINGS + *=========================*/ + +/** Possible values + * - LV_STDLIB_BUILTIN: LVGL's built in implementation + * - LV_STDLIB_CLIB: Standard C functions, like malloc, strlen, etc + * - LV_STDLIB_MICROPYTHON: MicroPython implementation + * - LV_STDLIB_RTTHREAD: RT-Thread implementation + * - LV_STDLIB_CUSTOM: Implement the functions externally + */ +#ifndef LV_USE_STDLIB_MALLOC + #ifdef CONFIG_LV_USE_STDLIB_MALLOC + #define LV_USE_STDLIB_MALLOC CONFIG_LV_USE_STDLIB_MALLOC #else - #define LV_COLOR_16_SWAP 0 + #define LV_USE_STDLIB_MALLOC LV_STDLIB_BUILTIN #endif #endif -/*Enable features to draw on transparent background. - *It's required if opa, and transform_* style properties are used. - *Can be also used if the UI is above another layer, e.g. an OSD menu or video player.*/ -#ifndef LV_COLOR_SCREEN_TRANSP - #ifdef CONFIG_LV_COLOR_SCREEN_TRANSP - #define LV_COLOR_SCREEN_TRANSP CONFIG_LV_COLOR_SCREEN_TRANSP +/** Possible values + * - LV_STDLIB_BUILTIN: LVGL's built in implementation + * - LV_STDLIB_CLIB: Standard C functions, like malloc, strlen, etc + * - LV_STDLIB_MICROPYTHON: MicroPython implementation + * - LV_STDLIB_RTTHREAD: RT-Thread implementation + * - LV_STDLIB_CUSTOM: Implement the functions externally + */ +#ifndef LV_USE_STDLIB_STRING + #ifdef CONFIG_LV_USE_STDLIB_STRING + #define LV_USE_STDLIB_STRING CONFIG_LV_USE_STDLIB_STRING #else - #define LV_COLOR_SCREEN_TRANSP 0 + #define LV_USE_STDLIB_STRING LV_STDLIB_BUILTIN #endif #endif -/* Adjust color mix functions rounding. GPUs might calculate color mix (blending) differently. - * 0: round down, 64: round up from x.75, 128: round up from half, 192: round up from x.25, 254: round up */ -#ifndef LV_COLOR_MIX_ROUND_OFS - #ifdef CONFIG_LV_COLOR_MIX_ROUND_OFS - #define LV_COLOR_MIX_ROUND_OFS CONFIG_LV_COLOR_MIX_ROUND_OFS +/** Possible values + * - LV_STDLIB_BUILTIN: LVGL's built in implementation + * - LV_STDLIB_CLIB: Standard C functions, like malloc, strlen, etc + * - LV_STDLIB_MICROPYTHON: MicroPython implementation + * - LV_STDLIB_RTTHREAD: RT-Thread implementation + * - LV_STDLIB_CUSTOM: Implement the functions externally + */ +#ifndef LV_USE_STDLIB_SPRINTF + #ifdef CONFIG_LV_USE_STDLIB_SPRINTF + #define LV_USE_STDLIB_SPRINTF CONFIG_LV_USE_STDLIB_SPRINTF #else - #define LV_COLOR_MIX_ROUND_OFS 0 + #define LV_USE_STDLIB_SPRINTF LV_STDLIB_BUILTIN #endif #endif -/*Images pixels with this color will not be drawn if they are chroma keyed)*/ -#ifndef LV_COLOR_CHROMA_KEY - #ifdef CONFIG_LV_COLOR_CHROMA_KEY - #define LV_COLOR_CHROMA_KEY CONFIG_LV_COLOR_CHROMA_KEY +#ifndef LV_STDINT_INCLUDE + #ifdef CONFIG_LV_STDINT_INCLUDE + #define LV_STDINT_INCLUDE CONFIG_LV_STDINT_INCLUDE #else - #define LV_COLOR_CHROMA_KEY lv_color_hex(0x00ff00) /*pure green*/ + #define LV_STDINT_INCLUDE #endif #endif - -/*========================= - MEMORY SETTINGS - *=========================*/ - -/*1: use custom malloc/free, 0: use the built-in `lv_mem_alloc()` and `lv_mem_free()`*/ -#ifndef LV_MEM_CUSTOM - #ifdef CONFIG_LV_MEM_CUSTOM - #define LV_MEM_CUSTOM CONFIG_LV_MEM_CUSTOM +#ifndef LV_STDDEF_INCLUDE + #ifdef CONFIG_LV_STDDEF_INCLUDE + #define LV_STDDEF_INCLUDE CONFIG_LV_STDDEF_INCLUDE #else - #define LV_MEM_CUSTOM 0 + #define LV_STDDEF_INCLUDE #endif #endif -#if LV_MEM_CUSTOM == 0 - /*Size of the memory available for `lv_mem_alloc()` in bytes (>= 2kB)*/ +#ifndef LV_STDBOOL_INCLUDE + #ifdef CONFIG_LV_STDBOOL_INCLUDE + #define LV_STDBOOL_INCLUDE CONFIG_LV_STDBOOL_INCLUDE + #else + #define LV_STDBOOL_INCLUDE + #endif +#endif +#ifndef LV_INTTYPES_INCLUDE + #ifdef CONFIG_LV_INTTYPES_INCLUDE + #define LV_INTTYPES_INCLUDE CONFIG_LV_INTTYPES_INCLUDE + #else + #define LV_INTTYPES_INCLUDE + #endif +#endif +#ifndef LV_LIMITS_INCLUDE + #ifdef CONFIG_LV_LIMITS_INCLUDE + #define LV_LIMITS_INCLUDE CONFIG_LV_LIMITS_INCLUDE + #else + #define LV_LIMITS_INCLUDE + #endif +#endif +#ifndef LV_STDARG_INCLUDE + #ifdef CONFIG_LV_STDARG_INCLUDE + #define LV_STDARG_INCLUDE CONFIG_LV_STDARG_INCLUDE + #else + #define LV_STDARG_INCLUDE + #endif +#endif + +#if LV_USE_STDLIB_MALLOC == LV_STDLIB_BUILTIN + /** Size of memory available for `lv_malloc()` in bytes (>= 2kB) */ #ifndef LV_MEM_SIZE #ifdef CONFIG_LV_MEM_SIZE #define LV_MEM_SIZE CONFIG_LV_MEM_SIZE #else - #define LV_MEM_SIZE (48U * 1024U) /*[bytes]*/ + #define LV_MEM_SIZE (64 * 1024U) /**< [bytes] */ + #endif + #endif + + /** Size of the memory expand for `lv_malloc()` in bytes */ + #ifndef LV_MEM_POOL_EXPAND_SIZE + #ifdef CONFIG_LV_MEM_POOL_EXPAND_SIZE + #define LV_MEM_POOL_EXPAND_SIZE CONFIG_LV_MEM_POOL_EXPAND_SIZE + #else + #define LV_MEM_POOL_EXPAND_SIZE 0 #endif #endif - /*Set an address for the memory pool instead of allocating it as a normal array. Can be in external SRAM too.*/ + /** Set an address for the memory pool instead of allocating it as a normal array. Can be in external SRAM too. */ #ifndef LV_MEM_ADR #ifdef CONFIG_LV_MEM_ADR #define LV_MEM_ADR CONFIG_LV_MEM_ADR #else - #define LV_MEM_ADR 0 /*0: unused*/ + #define LV_MEM_ADR 0 /**< 0: unused*/ #endif #endif - /*Instead of an address give a memory allocator that will be called to get a memory pool for LVGL. E.g. my_malloc*/ + /* Instead of an address give a memory allocator that will be called to get a memory pool for LVGL. E.g. my_malloc */ #if LV_MEM_ADR == 0 #ifndef LV_MEM_POOL_INCLUDE #ifdef CONFIG_LV_MEM_POOL_INCLUDE @@ -156,381 +228,657 @@ #endif #endif #endif +#endif /*LV_USE_STDLIB_MALLOC == LV_STDLIB_BUILTIN*/ -#else /*LV_MEM_CUSTOM*/ - #ifndef LV_MEM_CUSTOM_INCLUDE - #ifdef CONFIG_LV_MEM_CUSTOM_INCLUDE - #define LV_MEM_CUSTOM_INCLUDE CONFIG_LV_MEM_CUSTOM_INCLUDE - #else - #define LV_MEM_CUSTOM_INCLUDE /*Header for the dynamic memory function*/ - #endif +/*==================== + HAL SETTINGS + *====================*/ + +/** Default display refresh, input device read and animation step period. */ +#ifndef LV_DEF_REFR_PERIOD + #ifdef CONFIG_LV_DEF_REFR_PERIOD + #define LV_DEF_REFR_PERIOD CONFIG_LV_DEF_REFR_PERIOD + #else + #define LV_DEF_REFR_PERIOD 33 /**< [ms] */ #endif - #ifndef LV_MEM_CUSTOM_ALLOC - #ifdef CONFIG_LV_MEM_CUSTOM_ALLOC - #define LV_MEM_CUSTOM_ALLOC CONFIG_LV_MEM_CUSTOM_ALLOC - #else - #define LV_MEM_CUSTOM_ALLOC malloc - #endif +#endif + +/** Default Dots Per Inch. Used to initialize default sizes such as widgets sized, style paddings. + * (Not so important, you can adjust it to modify default sizes and spaces.) */ +#ifndef LV_DPI_DEF + #ifdef CONFIG_LV_DPI_DEF + #define LV_DPI_DEF CONFIG_LV_DPI_DEF + #else + #define LV_DPI_DEF 130 /**< [px/inch] */ #endif - #ifndef LV_MEM_CUSTOM_FREE - #ifdef CONFIG_LV_MEM_CUSTOM_FREE - #define LV_MEM_CUSTOM_FREE CONFIG_LV_MEM_CUSTOM_FREE +#endif + +/*================= + * OPERATING SYSTEM + *=================*/ +/** Select operating system to use. Possible options: + * - LV_OS_NONE + * - LV_OS_PTHREAD + * - LV_OS_FREERTOS + * - LV_OS_CMSIS_RTOS2 + * - LV_OS_RTTHREAD + * - LV_OS_WINDOWS + * - LV_OS_MQX + * - LV_OS_CUSTOM */ +#ifndef LV_USE_OS + #ifdef CONFIG_LV_USE_OS + #define LV_USE_OS CONFIG_LV_USE_OS + #else + #define LV_USE_OS LV_OS_NONE + #endif +#endif + +#if LV_USE_OS == LV_OS_CUSTOM + #ifndef LV_OS_CUSTOM_INCLUDE + #ifdef CONFIG_LV_OS_CUSTOM_INCLUDE + #define LV_OS_CUSTOM_INCLUDE CONFIG_LV_OS_CUSTOM_INCLUDE #else - #define LV_MEM_CUSTOM_FREE free + #define LV_OS_CUSTOM_INCLUDE #endif #endif - #ifndef LV_MEM_CUSTOM_REALLOC - #ifdef CONFIG_LV_MEM_CUSTOM_REALLOC - #define LV_MEM_CUSTOM_REALLOC CONFIG_LV_MEM_CUSTOM_REALLOC +#endif +#if LV_USE_OS == LV_OS_FREERTOS + /* + * Unblocking an RTOS task with a direct notification is 45% faster and uses less RAM + * than unblocking a task using an intermediary object such as a binary semaphore. + * RTOS task notifications can only be used when there is only one task that can be the recipient of the event. + */ + #ifndef LV_USE_FREERTOS_TASK_NOTIFY + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_FREERTOS_TASK_NOTIFY + #define LV_USE_FREERTOS_TASK_NOTIFY CONFIG_LV_USE_FREERTOS_TASK_NOTIFY + #else + #define LV_USE_FREERTOS_TASK_NOTIFY 0 + #endif + #else + #define LV_USE_FREERTOS_TASK_NOTIFY 1 + #endif + #endif +#endif + +/*======================== + * RENDERING CONFIGURATION + *========================*/ + +/** Align stride of all layers and images to this bytes */ +#ifndef LV_DRAW_BUF_STRIDE_ALIGN + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_DRAW_BUF_STRIDE_ALIGN + #define LV_DRAW_BUF_STRIDE_ALIGN CONFIG_LV_DRAW_BUF_STRIDE_ALIGN #else - #define LV_MEM_CUSTOM_REALLOC realloc + #define LV_DRAW_BUF_STRIDE_ALIGN 0 #endif + #else + #define LV_DRAW_BUF_STRIDE_ALIGN 1 #endif -#endif /*LV_MEM_CUSTOM*/ +#endif -/*Number of the intermediate memory buffer used during rendering and other internal processing mechanisms. - *You will see an error log message if there wasn't enough buffers. */ -#ifndef LV_MEM_BUF_MAX_NUM - #ifdef CONFIG_LV_MEM_BUF_MAX_NUM - #define LV_MEM_BUF_MAX_NUM CONFIG_LV_MEM_BUF_MAX_NUM +/** Align start address of draw_buf addresses to this bytes*/ +#ifndef LV_DRAW_BUF_ALIGN + #ifdef CONFIG_LV_DRAW_BUF_ALIGN + #define LV_DRAW_BUF_ALIGN CONFIG_LV_DRAW_BUF_ALIGN #else - #define LV_MEM_BUF_MAX_NUM 16 + #define LV_DRAW_BUF_ALIGN 4 #endif #endif -/*Use the standard `memcpy` and `memset` instead of LVGL's own functions. (Might or might not be faster).*/ -#ifndef LV_MEMCPY_MEMSET_STD - #ifdef CONFIG_LV_MEMCPY_MEMSET_STD - #define LV_MEMCPY_MEMSET_STD CONFIG_LV_MEMCPY_MEMSET_STD +/** Using matrix for transformations. + * Requirements: + * - `LV_USE_MATRIX = 1`. + * - Rendering engine needs to support 3x3 matrix transformations. */ +#ifndef LV_DRAW_TRANSFORM_USE_MATRIX + #ifdef CONFIG_LV_DRAW_TRANSFORM_USE_MATRIX + #define LV_DRAW_TRANSFORM_USE_MATRIX CONFIG_LV_DRAW_TRANSFORM_USE_MATRIX #else - #define LV_MEMCPY_MEMSET_STD 0 + #define LV_DRAW_TRANSFORM_USE_MATRIX 0 #endif #endif -/*==================== - HAL SETTINGS - *====================*/ +/* If a widget has `style_opa < 255` (not `bg_opa`, `text_opa` etc) or not NORMAL blend mode + * it is buffered into a "simple" layer before rendering. The widget can be buffered in smaller chunks. + * "Transformed layers" (if `transform_angle/zoom` are set) use larger buffers + * and can't be drawn in chunks. */ -/*Default display refresh period. LVG will redraw changed areas with this period time*/ -#ifndef LV_DISP_DEF_REFR_PERIOD - #ifdef CONFIG_LV_DISP_DEF_REFR_PERIOD - #define LV_DISP_DEF_REFR_PERIOD CONFIG_LV_DISP_DEF_REFR_PERIOD +/** The target buffer size for simple layer chunks. */ +#ifndef LV_DRAW_LAYER_SIMPLE_BUF_SIZE + #ifdef CONFIG_LV_DRAW_LAYER_SIMPLE_BUF_SIZE + #define LV_DRAW_LAYER_SIMPLE_BUF_SIZE CONFIG_LV_DRAW_LAYER_SIMPLE_BUF_SIZE #else - #define LV_DISP_DEF_REFR_PERIOD 30 /*[ms]*/ + #define LV_DRAW_LAYER_SIMPLE_BUF_SIZE (24 * 1024) /**< [bytes]*/ #endif #endif -/*Input device read period in milliseconds*/ -#ifndef LV_INDEV_DEF_READ_PERIOD - #ifdef CONFIG_LV_INDEV_DEF_READ_PERIOD - #define LV_INDEV_DEF_READ_PERIOD CONFIG_LV_INDEV_DEF_READ_PERIOD +/** Stack size of drawing thread. + * NOTE: If FreeType or ThorVG is enabled, it is recommended to set it to 32KB or more. + */ +#ifndef LV_DRAW_THREAD_STACK_SIZE + #ifdef CONFIG_LV_DRAW_THREAD_STACK_SIZE + #define LV_DRAW_THREAD_STACK_SIZE CONFIG_LV_DRAW_THREAD_STACK_SIZE #else - #define LV_INDEV_DEF_READ_PERIOD 30 /*[ms]*/ + #define LV_DRAW_THREAD_STACK_SIZE (8 * 1024) /**< [bytes]*/ #endif #endif -/*Use a custom tick source that tells the elapsed time in milliseconds. - *It removes the need to manually update the tick with `lv_tick_inc()`)*/ -#ifndef LV_TICK_CUSTOM - #ifdef CONFIG_LV_TICK_CUSTOM - #define LV_TICK_CUSTOM CONFIG_LV_TICK_CUSTOM +#ifndef LV_USE_DRAW_SW + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_DRAW_SW + #define LV_USE_DRAW_SW CONFIG_LV_USE_DRAW_SW + #else + #define LV_USE_DRAW_SW 0 + #endif #else - #define LV_TICK_CUSTOM 0 + #define LV_USE_DRAW_SW 1 #endif #endif -#if LV_TICK_CUSTOM - #ifndef LV_TICK_CUSTOM_INCLUDE - #ifdef CONFIG_LV_TICK_CUSTOM_INCLUDE - #define LV_TICK_CUSTOM_INCLUDE CONFIG_LV_TICK_CUSTOM_INCLUDE +#if LV_USE_DRAW_SW == 1 + /* + * Selectively disable color format support in order to reduce code size. + * NOTE: some features use certain color formats internally, e.g. + * - gradients use RGB888 + * - bitmaps with transparency may use ARGB8888 + */ + #ifndef LV_DRAW_SW_SUPPORT_RGB565 + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_DRAW_SW_SUPPORT_RGB565 + #define LV_DRAW_SW_SUPPORT_RGB565 CONFIG_LV_DRAW_SW_SUPPORT_RGB565 + #else + #define LV_DRAW_SW_SUPPORT_RGB565 0 + #endif #else - #define LV_TICK_CUSTOM_INCLUDE "Arduino.h" /*Header for the system time function*/ + #define LV_DRAW_SW_SUPPORT_RGB565 1 #endif #endif - #ifndef LV_TICK_CUSTOM_SYS_TIME_EXPR - #ifdef CONFIG_LV_TICK_CUSTOM_SYS_TIME_EXPR - #define LV_TICK_CUSTOM_SYS_TIME_EXPR CONFIG_LV_TICK_CUSTOM_SYS_TIME_EXPR + #ifndef LV_DRAW_SW_SUPPORT_RGB565A8 + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_DRAW_SW_SUPPORT_RGB565A8 + #define LV_DRAW_SW_SUPPORT_RGB565A8 CONFIG_LV_DRAW_SW_SUPPORT_RGB565A8 + #else + #define LV_DRAW_SW_SUPPORT_RGB565A8 0 + #endif #else - #define LV_TICK_CUSTOM_SYS_TIME_EXPR (millis()) /*Expression evaluating to current system time in ms*/ + #define LV_DRAW_SW_SUPPORT_RGB565A8 1 #endif #endif -#endif /*LV_TICK_CUSTOM*/ - -/*Default Dot Per Inch. Used to initialize default sizes such as widgets sized, style paddings. - *(Not so important, you can adjust it to modify default sizes and spaces)*/ -#ifndef LV_DPI_DEF - #ifdef CONFIG_LV_DPI_DEF - #define LV_DPI_DEF CONFIG_LV_DPI_DEF - #else - #define LV_DPI_DEF 130 /*[px/inch]*/ + #ifndef LV_DRAW_SW_SUPPORT_RGB888 + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_DRAW_SW_SUPPORT_RGB888 + #define LV_DRAW_SW_SUPPORT_RGB888 CONFIG_LV_DRAW_SW_SUPPORT_RGB888 + #else + #define LV_DRAW_SW_SUPPORT_RGB888 0 + #endif + #else + #define LV_DRAW_SW_SUPPORT_RGB888 1 + #endif + #endif + #ifndef LV_DRAW_SW_SUPPORT_XRGB8888 + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_DRAW_SW_SUPPORT_XRGB8888 + #define LV_DRAW_SW_SUPPORT_XRGB8888 CONFIG_LV_DRAW_SW_SUPPORT_XRGB8888 + #else + #define LV_DRAW_SW_SUPPORT_XRGB8888 0 + #endif + #else + #define LV_DRAW_SW_SUPPORT_XRGB8888 1 + #endif + #endif + #ifndef LV_DRAW_SW_SUPPORT_ARGB8888 + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_DRAW_SW_SUPPORT_ARGB8888 + #define LV_DRAW_SW_SUPPORT_ARGB8888 CONFIG_LV_DRAW_SW_SUPPORT_ARGB8888 + #else + #define LV_DRAW_SW_SUPPORT_ARGB8888 0 + #endif + #else + #define LV_DRAW_SW_SUPPORT_ARGB8888 1 + #endif + #endif + #ifndef LV_DRAW_SW_SUPPORT_L8 + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_DRAW_SW_SUPPORT_L8 + #define LV_DRAW_SW_SUPPORT_L8 CONFIG_LV_DRAW_SW_SUPPORT_L8 + #else + #define LV_DRAW_SW_SUPPORT_L8 0 + #endif + #else + #define LV_DRAW_SW_SUPPORT_L8 1 + #endif + #endif + #ifndef LV_DRAW_SW_SUPPORT_AL88 + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_DRAW_SW_SUPPORT_AL88 + #define LV_DRAW_SW_SUPPORT_AL88 CONFIG_LV_DRAW_SW_SUPPORT_AL88 + #else + #define LV_DRAW_SW_SUPPORT_AL88 0 + #endif + #else + #define LV_DRAW_SW_SUPPORT_AL88 1 + #endif + #endif + #ifndef LV_DRAW_SW_SUPPORT_A8 + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_DRAW_SW_SUPPORT_A8 + #define LV_DRAW_SW_SUPPORT_A8 CONFIG_LV_DRAW_SW_SUPPORT_A8 + #else + #define LV_DRAW_SW_SUPPORT_A8 0 + #endif + #else + #define LV_DRAW_SW_SUPPORT_A8 1 + #endif + #endif + #ifndef LV_DRAW_SW_SUPPORT_I1 + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_DRAW_SW_SUPPORT_I1 + #define LV_DRAW_SW_SUPPORT_I1 CONFIG_LV_DRAW_SW_SUPPORT_I1 + #else + #define LV_DRAW_SW_SUPPORT_I1 0 + #endif + #else + #define LV_DRAW_SW_SUPPORT_I1 1 + #endif #endif -#endif -/*======================= - * FEATURE CONFIGURATION - *=======================*/ + /** Set number of draw units. + * - > 1 requires operating system to be enabled in `LV_USE_OS`. + * - > 1 means multiple threads will render the screen in parallel. */ + #ifndef LV_DRAW_SW_DRAW_UNIT_CNT + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_DRAW_SW_DRAW_UNIT_CNT + #define LV_DRAW_SW_DRAW_UNIT_CNT CONFIG_LV_DRAW_SW_DRAW_UNIT_CNT + #else + #define LV_DRAW_SW_DRAW_UNIT_CNT 0 + #endif + #else + #define LV_DRAW_SW_DRAW_UNIT_CNT 1 + #endif + #endif -/*------------- - * Drawing - *-----------*/ + /** Use Arm-2D to accelerate software (sw) rendering. */ + #ifndef LV_USE_DRAW_ARM2D_SYNC + #ifdef CONFIG_LV_USE_DRAW_ARM2D_SYNC + #define LV_USE_DRAW_ARM2D_SYNC CONFIG_LV_USE_DRAW_ARM2D_SYNC + #else + #define LV_USE_DRAW_ARM2D_SYNC 0 + #endif + #endif -/*Enable complex draw engine. - *Required to draw shadow, gradient, rounded corners, circles, arc, skew lines, image transformations or any masks*/ -#ifndef LV_DRAW_COMPLEX - #ifdef _LV_KCONFIG_PRESENT - #ifdef CONFIG_LV_DRAW_COMPLEX - #define LV_DRAW_COMPLEX CONFIG_LV_DRAW_COMPLEX + /** Enable native helium assembly to be compiled. */ + #ifndef LV_USE_NATIVE_HELIUM_ASM + #ifdef CONFIG_LV_USE_NATIVE_HELIUM_ASM + #define LV_USE_NATIVE_HELIUM_ASM CONFIG_LV_USE_NATIVE_HELIUM_ASM #else - #define LV_DRAW_COMPLEX 0 + #define LV_USE_NATIVE_HELIUM_ASM 0 #endif - #else - #define LV_DRAW_COMPLEX 1 #endif -#endif -#if LV_DRAW_COMPLEX != 0 - /*Allow buffering some shadow calculation. - *LV_SHADOW_CACHE_SIZE is the max. shadow size to buffer, where shadow size is `shadow_width + radius` - *Caching has LV_SHADOW_CACHE_SIZE^2 RAM cost*/ - #ifndef LV_SHADOW_CACHE_SIZE - #ifdef CONFIG_LV_SHADOW_CACHE_SIZE - #define LV_SHADOW_CACHE_SIZE CONFIG_LV_SHADOW_CACHE_SIZE + /** + * - 0: Use a simple renderer capable of drawing only simple rectangles with gradient, images, text, and straight lines only. + * - 1: Use a complex renderer capable of drawing rounded corners, shadow, skew lines, and arcs too. */ + #ifndef LV_DRAW_SW_COMPLEX + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_DRAW_SW_COMPLEX + #define LV_DRAW_SW_COMPLEX CONFIG_LV_DRAW_SW_COMPLEX + #else + #define LV_DRAW_SW_COMPLEX 0 + #endif #else - #define LV_SHADOW_CACHE_SIZE 0 + #define LV_DRAW_SW_COMPLEX 1 + #endif + #endif + + #if LV_DRAW_SW_COMPLEX == 1 + /** Allow buffering some shadow calculation. + * LV_DRAW_SW_SHADOW_CACHE_SIZE is the maximum shadow size to buffer, where shadow size is + * `shadow_width + radius`. Caching has LV_DRAW_SW_SHADOW_CACHE_SIZE^2 RAM cost. */ + #ifndef LV_DRAW_SW_SHADOW_CACHE_SIZE + #ifdef CONFIG_LV_DRAW_SW_SHADOW_CACHE_SIZE + #define LV_DRAW_SW_SHADOW_CACHE_SIZE CONFIG_LV_DRAW_SW_SHADOW_CACHE_SIZE + #else + #define LV_DRAW_SW_SHADOW_CACHE_SIZE 0 + #endif + #endif + + /** Set number of maximally-cached circle data. + * The circumference of 1/4 circle are saved for anti-aliasing. + * `radius * 4` bytes are used per circle (the most often used radiuses are saved). + * - 0: disables caching */ + #ifndef LV_DRAW_SW_CIRCLE_CACHE_SIZE + #ifdef CONFIG_LV_DRAW_SW_CIRCLE_CACHE_SIZE + #define LV_DRAW_SW_CIRCLE_CACHE_SIZE CONFIG_LV_DRAW_SW_CIRCLE_CACHE_SIZE + #else + #define LV_DRAW_SW_CIRCLE_CACHE_SIZE 4 + #endif #endif #endif - /* Set number of maximally cached circle data. - * The circumference of 1/4 circle are saved for anti-aliasing - * radius * 4 bytes are used per circle (the most often used radiuses are saved) - * 0: to disable caching */ - #ifndef LV_CIRCLE_CACHE_SIZE - #ifdef CONFIG_LV_CIRCLE_CACHE_SIZE - #define LV_CIRCLE_CACHE_SIZE CONFIG_LV_CIRCLE_CACHE_SIZE + #ifndef LV_USE_DRAW_SW_ASM + #ifdef CONFIG_LV_USE_DRAW_SW_ASM + #define LV_USE_DRAW_SW_ASM CONFIG_LV_USE_DRAW_SW_ASM #else - #define LV_CIRCLE_CACHE_SIZE 4 + #define LV_USE_DRAW_SW_ASM LV_DRAW_SW_ASM_NONE #endif #endif -#endif /*LV_DRAW_COMPLEX*/ -/** - * "Simple layers" are used when a widget has `style_opa < 255` to buffer the widget into a layer - * and blend it as an image with the given opacity. - * Note that `bg_opa`, `text_opa` etc don't require buffering into layer) - * The widget can be buffered in smaller chunks to avoid using large buffers. - * - * - LV_LAYER_SIMPLE_BUF_SIZE: [bytes] the optimal target buffer size. LVGL will try to allocate it - * - LV_LAYER_SIMPLE_FALLBACK_BUF_SIZE: [bytes] used if `LV_LAYER_SIMPLE_BUF_SIZE` couldn't be allocated. - * - * Both buffer sizes are in bytes. - * "Transformed layers" (where transform_angle/zoom properties are used) use larger buffers - * and can't be drawn in chunks. So these settings affects only widgets with opacity. - */ -#ifndef LV_LAYER_SIMPLE_BUF_SIZE - #ifdef CONFIG_LV_LAYER_SIMPLE_BUF_SIZE - #define LV_LAYER_SIMPLE_BUF_SIZE CONFIG_LV_LAYER_SIMPLE_BUF_SIZE - #else - #define LV_LAYER_SIMPLE_BUF_SIZE (24 * 1024) + #if LV_USE_DRAW_SW_ASM == LV_DRAW_SW_ASM_CUSTOM + #ifndef LV_DRAW_SW_ASM_CUSTOM_INCLUDE + #ifdef CONFIG_LV_DRAW_SW_ASM_CUSTOM_INCLUDE + #define LV_DRAW_SW_ASM_CUSTOM_INCLUDE CONFIG_LV_DRAW_SW_ASM_CUSTOM_INCLUDE + #else + #define LV_DRAW_SW_ASM_CUSTOM_INCLUDE "" + #endif + #endif + #endif + + /** Enable drawing complex gradients in software: linear at an angle, radial or conical */ + #ifndef LV_USE_DRAW_SW_COMPLEX_GRADIENTS + #ifdef CONFIG_LV_USE_DRAW_SW_COMPLEX_GRADIENTS + #define LV_USE_DRAW_SW_COMPLEX_GRADIENTS CONFIG_LV_USE_DRAW_SW_COMPLEX_GRADIENTS + #else + #define LV_USE_DRAW_SW_COMPLEX_GRADIENTS 0 + #endif #endif #endif -#ifndef LV_LAYER_SIMPLE_FALLBACK_BUF_SIZE - #ifdef CONFIG_LV_LAYER_SIMPLE_FALLBACK_BUF_SIZE - #define LV_LAYER_SIMPLE_FALLBACK_BUF_SIZE CONFIG_LV_LAYER_SIMPLE_FALLBACK_BUF_SIZE + +/*Use TSi's aka (Think Silicon) NemaGFX */ +#ifndef LV_USE_NEMA_GFX + #ifdef CONFIG_LV_USE_NEMA_GFX + #define LV_USE_NEMA_GFX CONFIG_LV_USE_NEMA_GFX #else - #define LV_LAYER_SIMPLE_FALLBACK_BUF_SIZE (3 * 1024) + #define LV_USE_NEMA_GFX 0 #endif #endif -/*Default image cache size. Image caching keeps the images opened. - *If only the built-in image formats are used there is no real advantage of caching. (I.e. if no new image decoder is added) - *With complex image decoders (e.g. PNG or JPG) caching can save the continuous open/decode of images. - *However the opened images might consume additional RAM. - *0: to disable caching*/ -#ifndef LV_IMG_CACHE_DEF_SIZE - #ifdef CONFIG_LV_IMG_CACHE_DEF_SIZE - #define LV_IMG_CACHE_DEF_SIZE CONFIG_LV_IMG_CACHE_DEF_SIZE - #else - #define LV_IMG_CACHE_DEF_SIZE 0 +#if LV_USE_NEMA_GFX + #ifndef LV_NEMA_GFX_HAL_INCLUDE + #ifdef CONFIG_LV_NEMA_GFX_HAL_INCLUDE + #define LV_NEMA_GFX_HAL_INCLUDE CONFIG_LV_NEMA_GFX_HAL_INCLUDE + #else + #define LV_NEMA_GFX_HAL_INCLUDE + #endif + #endif + + /*Enable Vector Graphics Operations. Available only if NemaVG library is present*/ + #ifndef LV_USE_NEMA_VG + #ifdef CONFIG_LV_USE_NEMA_VG + #define LV_USE_NEMA_VG CONFIG_LV_USE_NEMA_VG + #else + #define LV_USE_NEMA_VG 0 + #endif + #endif + + #if LV_USE_NEMA_VG + /*Define application's resolution used for VG related buffer allocation */ + #ifndef LV_NEMA_GFX_MAX_RESX + #ifdef CONFIG_LV_NEMA_GFX_MAX_RESX + #define LV_NEMA_GFX_MAX_RESX CONFIG_LV_NEMA_GFX_MAX_RESX + #else + #define LV_NEMA_GFX_MAX_RESX 800 + #endif + #endif + #ifndef LV_NEMA_GFX_MAX_RESY + #ifdef CONFIG_LV_NEMA_GFX_MAX_RESY + #define LV_NEMA_GFX_MAX_RESY CONFIG_LV_NEMA_GFX_MAX_RESY + #else + #define LV_NEMA_GFX_MAX_RESY 600 + #endif + #endif #endif #endif -/*Number of stops allowed per gradient. Increase this to allow more stops. - *This adds (sizeof(lv_color_t) + 1) bytes per additional stop*/ -#ifndef LV_GRADIENT_MAX_STOPS - #ifdef CONFIG_LV_GRADIENT_MAX_STOPS - #define LV_GRADIENT_MAX_STOPS CONFIG_LV_GRADIENT_MAX_STOPS +/** Use NXP's VG-Lite GPU on iMX RTxxx platforms. */ +#ifndef LV_USE_DRAW_VGLITE + #ifdef CONFIG_LV_USE_DRAW_VGLITE + #define LV_USE_DRAW_VGLITE CONFIG_LV_USE_DRAW_VGLITE #else - #define LV_GRADIENT_MAX_STOPS 2 + #define LV_USE_DRAW_VGLITE 0 #endif #endif -/*Default gradient buffer size. - *When LVGL calculates the gradient "maps" it can save them into a cache to avoid calculating them again. - *LV_GRAD_CACHE_DEF_SIZE sets the size of this cache in bytes. - *If the cache is too small the map will be allocated only while it's required for the drawing. - *0 mean no caching.*/ -#ifndef LV_GRAD_CACHE_DEF_SIZE - #ifdef CONFIG_LV_GRAD_CACHE_DEF_SIZE - #define LV_GRAD_CACHE_DEF_SIZE CONFIG_LV_GRAD_CACHE_DEF_SIZE - #else - #define LV_GRAD_CACHE_DEF_SIZE 0 +#if LV_USE_DRAW_VGLITE + /** Enable blit quality degradation workaround recommended for screen's dimension > 352 pixels. */ + #ifndef LV_USE_VGLITE_BLIT_SPLIT + #ifdef CONFIG_LV_USE_VGLITE_BLIT_SPLIT + #define LV_USE_VGLITE_BLIT_SPLIT CONFIG_LV_USE_VGLITE_BLIT_SPLIT + #else + #define LV_USE_VGLITE_BLIT_SPLIT 0 + #endif + #endif + + #if LV_USE_OS + /** Use additional draw thread for VG-Lite processing. */ + #ifndef LV_USE_VGLITE_DRAW_THREAD + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_VGLITE_DRAW_THREAD + #define LV_USE_VGLITE_DRAW_THREAD CONFIG_LV_USE_VGLITE_DRAW_THREAD + #else + #define LV_USE_VGLITE_DRAW_THREAD 0 + #endif + #else + #define LV_USE_VGLITE_DRAW_THREAD 1 + #endif + #endif + + #if LV_USE_VGLITE_DRAW_THREAD + /** Enable VGLite draw async. Queue multiple tasks and flash them once to the GPU. */ + #ifndef LV_USE_VGLITE_DRAW_ASYNC + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_VGLITE_DRAW_ASYNC + #define LV_USE_VGLITE_DRAW_ASYNC CONFIG_LV_USE_VGLITE_DRAW_ASYNC + #else + #define LV_USE_VGLITE_DRAW_ASYNC 0 + #endif + #else + #define LV_USE_VGLITE_DRAW_ASYNC 1 + #endif + #endif + #endif + #endif + + /** Enable VGLite asserts. */ + #ifndef LV_USE_VGLITE_ASSERT + #ifdef CONFIG_LV_USE_VGLITE_ASSERT + #define LV_USE_VGLITE_ASSERT CONFIG_LV_USE_VGLITE_ASSERT + #else + #define LV_USE_VGLITE_ASSERT 0 + #endif #endif #endif -/*Allow dithering the gradients (to achieve visual smooth color gradients on limited color depth display) - *LV_DITHER_GRADIENT implies allocating one or two more lines of the object's rendering surface - *The increase in memory consumption is (32 bits * object width) plus 24 bits * object width if using error diffusion */ -#ifndef LV_DITHER_GRADIENT - #ifdef CONFIG_LV_DITHER_GRADIENT - #define LV_DITHER_GRADIENT CONFIG_LV_DITHER_GRADIENT +/** Use NXP's PXP on iMX RTxxx platforms. */ +#ifndef LV_USE_PXP + #ifdef CONFIG_LV_USE_PXP + #define LV_USE_PXP CONFIG_LV_USE_PXP #else - #define LV_DITHER_GRADIENT 0 + #define LV_USE_PXP 0 #endif #endif -#if LV_DITHER_GRADIENT - /*Add support for error diffusion dithering. - *Error diffusion dithering gets a much better visual result, but implies more CPU consumption and memory when drawing. - *The increase in memory consumption is (24 bits * object's width)*/ - #ifndef LV_DITHER_ERROR_DIFFUSION - #ifdef CONFIG_LV_DITHER_ERROR_DIFFUSION - #define LV_DITHER_ERROR_DIFFUSION CONFIG_LV_DITHER_ERROR_DIFFUSION + +#if LV_USE_PXP + /** Use PXP for drawing.*/ + #ifndef LV_USE_DRAW_PXP + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_DRAW_PXP + #define LV_USE_DRAW_PXP CONFIG_LV_USE_DRAW_PXP + #else + #define LV_USE_DRAW_PXP 0 + #endif + #else + #define LV_USE_DRAW_PXP 1 + #endif + #endif + + /** Use PXP to rotate display.*/ + #ifndef LV_USE_ROTATE_PXP + #ifdef CONFIG_LV_USE_ROTATE_PXP + #define LV_USE_ROTATE_PXP CONFIG_LV_USE_ROTATE_PXP + #else + #define LV_USE_ROTATE_PXP 0 + #endif + #endif + + #if LV_USE_DRAW_PXP && LV_USE_OS + /** Use additional draw thread for PXP processing.*/ + #ifndef LV_USE_PXP_DRAW_THREAD + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_PXP_DRAW_THREAD + #define LV_USE_PXP_DRAW_THREAD CONFIG_LV_USE_PXP_DRAW_THREAD + #else + #define LV_USE_PXP_DRAW_THREAD 0 + #endif + #else + #define LV_USE_PXP_DRAW_THREAD 1 + #endif + #endif + #endif + + /** Enable PXP asserts. */ + #ifndef LV_USE_PXP_ASSERT + #ifdef CONFIG_LV_USE_PXP_ASSERT + #define LV_USE_PXP_ASSERT CONFIG_LV_USE_PXP_ASSERT #else - #define LV_DITHER_ERROR_DIFFUSION 0 + #define LV_USE_PXP_ASSERT 0 #endif #endif #endif -/*Maximum buffer size to allocate for rotation. - *Only used if software rotation is enabled in the display driver.*/ -#ifndef LV_DISP_ROT_MAX_BUF - #ifdef CONFIG_LV_DISP_ROT_MAX_BUF - #define LV_DISP_ROT_MAX_BUF CONFIG_LV_DISP_ROT_MAX_BUF +/** Use Renesas Dave2D on RA platforms. */ +#ifndef LV_USE_DRAW_DAVE2D + #ifdef CONFIG_LV_USE_DRAW_DAVE2D + #define LV_USE_DRAW_DAVE2D CONFIG_LV_USE_DRAW_DAVE2D #else - #define LV_DISP_ROT_MAX_BUF (10*1024) + #define LV_USE_DRAW_DAVE2D 0 #endif #endif -/*------------- - * GPU - *-----------*/ - -/*Use Arm's 2D acceleration library Arm-2D */ -#ifndef LV_USE_GPU_ARM2D - #ifdef CONFIG_LV_USE_GPU_ARM2D - #define LV_USE_GPU_ARM2D CONFIG_LV_USE_GPU_ARM2D +/** Draw using cached SDL textures*/ +#ifndef LV_USE_DRAW_SDL + #ifdef CONFIG_LV_USE_DRAW_SDL + #define LV_USE_DRAW_SDL CONFIG_LV_USE_DRAW_SDL #else - #define LV_USE_GPU_ARM2D 0 + #define LV_USE_DRAW_SDL 0 #endif #endif -/*Use STM32's DMA2D (aka Chrom Art) GPU*/ -#ifndef LV_USE_GPU_STM32_DMA2D - #ifdef CONFIG_LV_USE_GPU_STM32_DMA2D - #define LV_USE_GPU_STM32_DMA2D CONFIG_LV_USE_GPU_STM32_DMA2D +/** Use VG-Lite GPU. */ +#ifndef LV_USE_DRAW_VG_LITE + #ifdef CONFIG_LV_USE_DRAW_VG_LITE + #define LV_USE_DRAW_VG_LITE CONFIG_LV_USE_DRAW_VG_LITE #else - #define LV_USE_GPU_STM32_DMA2D 0 + #define LV_USE_DRAW_VG_LITE 0 #endif #endif -#if LV_USE_GPU_STM32_DMA2D - /*Must be defined to include path of CMSIS header of target processor - e.g. "stm32f769xx.h" or "stm32f429xx.h"*/ - #ifndef LV_GPU_DMA2D_CMSIS_INCLUDE - #ifdef CONFIG_LV_GPU_DMA2D_CMSIS_INCLUDE - #define LV_GPU_DMA2D_CMSIS_INCLUDE CONFIG_LV_GPU_DMA2D_CMSIS_INCLUDE + +#if LV_USE_DRAW_VG_LITE + /** Enable VG-Lite custom external 'gpu_init()' function */ + #ifndef LV_VG_LITE_USE_GPU_INIT + #ifdef CONFIG_LV_VG_LITE_USE_GPU_INIT + #define LV_VG_LITE_USE_GPU_INIT CONFIG_LV_VG_LITE_USE_GPU_INIT #else - #define LV_GPU_DMA2D_CMSIS_INCLUDE + #define LV_VG_LITE_USE_GPU_INIT 0 #endif #endif -#endif -/*Use SWM341's DMA2D GPU*/ -#ifndef LV_USE_GPU_SWM341_DMA2D - #ifdef CONFIG_LV_USE_GPU_SWM341_DMA2D - #define LV_USE_GPU_SWM341_DMA2D CONFIG_LV_USE_GPU_SWM341_DMA2D - #else - #define LV_USE_GPU_SWM341_DMA2D 0 + /** Enable VG-Lite assert. */ + #ifndef LV_VG_LITE_USE_ASSERT + #ifdef CONFIG_LV_VG_LITE_USE_ASSERT + #define LV_VG_LITE_USE_ASSERT CONFIG_LV_VG_LITE_USE_ASSERT + #else + #define LV_VG_LITE_USE_ASSERT 0 + #endif #endif -#endif -#if LV_USE_GPU_SWM341_DMA2D - #ifndef LV_GPU_SWM341_DMA2D_INCLUDE - #ifdef CONFIG_LV_GPU_SWM341_DMA2D_INCLUDE - #define LV_GPU_SWM341_DMA2D_INCLUDE CONFIG_LV_GPU_SWM341_DMA2D_INCLUDE + + /** VG-Lite flush commit trigger threshold. GPU will try to batch these many draw tasks. */ + #ifndef LV_VG_LITE_FLUSH_MAX_COUNT + #ifdef CONFIG_LV_VG_LITE_FLUSH_MAX_COUNT + #define LV_VG_LITE_FLUSH_MAX_COUNT CONFIG_LV_VG_LITE_FLUSH_MAX_COUNT #else - #define LV_GPU_SWM341_DMA2D_INCLUDE "SWM341.h" + #define LV_VG_LITE_FLUSH_MAX_COUNT 8 #endif #endif -#endif -/*Use NXP's PXP GPU iMX RTxxx platforms*/ -#ifndef LV_USE_GPU_NXP_PXP - #ifdef CONFIG_LV_USE_GPU_NXP_PXP - #define LV_USE_GPU_NXP_PXP CONFIG_LV_USE_GPU_NXP_PXP - #else - #define LV_USE_GPU_NXP_PXP 0 + /** Enable border to simulate shadow. + * NOTE: which usually improves performance, + * but does not guarantee the same rendering quality as the software. */ + #ifndef LV_VG_LITE_USE_BOX_SHADOW + #ifdef CONFIG_LV_VG_LITE_USE_BOX_SHADOW + #define LV_VG_LITE_USE_BOX_SHADOW CONFIG_LV_VG_LITE_USE_BOX_SHADOW + #else + #define LV_VG_LITE_USE_BOX_SHADOW 0 + #endif #endif -#endif -#if LV_USE_GPU_NXP_PXP - /*1: Add default bare metal and FreeRTOS interrupt handling routines for PXP (lv_gpu_nxp_pxp_osa.c) - * and call lv_gpu_nxp_pxp_init() automatically during lv_init(). Note that symbol SDK_OS_FREE_RTOS - * has to be defined in order to use FreeRTOS OSA, otherwise bare-metal implementation is selected. - *0: lv_gpu_nxp_pxp_init() has to be called manually before lv_init() - */ - #ifndef LV_USE_GPU_NXP_PXP_AUTO_INIT - #ifdef CONFIG_LV_USE_GPU_NXP_PXP_AUTO_INIT - #define LV_USE_GPU_NXP_PXP_AUTO_INIT CONFIG_LV_USE_GPU_NXP_PXP_AUTO_INIT + + /** VG-Lite gradient maximum cache number. + * @note The memory usage of a single gradient image is 4K bytes. */ + #ifndef LV_VG_LITE_GRAD_CACHE_CNT + #ifdef CONFIG_LV_VG_LITE_GRAD_CACHE_CNT + #define LV_VG_LITE_GRAD_CACHE_CNT CONFIG_LV_VG_LITE_GRAD_CACHE_CNT #else - #define LV_USE_GPU_NXP_PXP_AUTO_INIT 0 + #define LV_VG_LITE_GRAD_CACHE_CNT 32 #endif #endif -#endif -/*Use NXP's VG-Lite GPU iMX RTxxx platforms*/ -#ifndef LV_USE_GPU_NXP_VG_LITE - #ifdef CONFIG_LV_USE_GPU_NXP_VG_LITE - #define LV_USE_GPU_NXP_VG_LITE CONFIG_LV_USE_GPU_NXP_VG_LITE - #else - #define LV_USE_GPU_NXP_VG_LITE 0 + /** VG-Lite stroke maximum cache number. */ + #ifndef LV_VG_LITE_STROKE_CACHE_CNT + #ifdef CONFIG_LV_VG_LITE_STROKE_CACHE_CNT + #define LV_VG_LITE_STROKE_CACHE_CNT CONFIG_LV_VG_LITE_STROKE_CACHE_CNT + #else + #define LV_VG_LITE_STROKE_CACHE_CNT 32 + #endif #endif #endif -/*Use SDL renderer API*/ -#ifndef LV_USE_GPU_SDL - #ifdef CONFIG_LV_USE_GPU_SDL - #define LV_USE_GPU_SDL CONFIG_LV_USE_GPU_SDL +/** Accelerate blends, fills, etc. with STM32 DMA2D */ +#ifndef LV_USE_DRAW_DMA2D + #ifdef CONFIG_LV_USE_DRAW_DMA2D + #define LV_USE_DRAW_DMA2D CONFIG_LV_USE_DRAW_DMA2D #else - #define LV_USE_GPU_SDL 0 + #define LV_USE_DRAW_DMA2D 0 #endif #endif -#if LV_USE_GPU_SDL - #ifndef LV_GPU_SDL_INCLUDE_PATH - #ifdef CONFIG_LV_GPU_SDL_INCLUDE_PATH - #define LV_GPU_SDL_INCLUDE_PATH CONFIG_LV_GPU_SDL_INCLUDE_PATH + +#if LV_USE_DRAW_DMA2D + #ifndef LV_DRAW_DMA2D_HAL_INCLUDE + #ifdef CONFIG_LV_DRAW_DMA2D_HAL_INCLUDE + #define LV_DRAW_DMA2D_HAL_INCLUDE CONFIG_LV_DRAW_DMA2D_HAL_INCLUDE #else - #define LV_GPU_SDL_INCLUDE_PATH + #define LV_DRAW_DMA2D_HAL_INCLUDE "stm32h7xx_hal.h" #endif #endif - /*Texture cache size, 8MB by default*/ - #ifndef LV_GPU_SDL_LRU_SIZE - #ifdef CONFIG_LV_GPU_SDL_LRU_SIZE - #define LV_GPU_SDL_LRU_SIZE CONFIG_LV_GPU_SDL_LRU_SIZE + + /* if enabled, the user is required to call `lv_draw_dma2d_transfer_complete_interrupt_handler` + * upon receiving the DMA2D global interrupt + */ + #ifndef LV_USE_DRAW_DMA2D_INTERRUPT + #ifdef CONFIG_LV_USE_DRAW_DMA2D_INTERRUPT + #define LV_USE_DRAW_DMA2D_INTERRUPT CONFIG_LV_USE_DRAW_DMA2D_INTERRUPT #else - #define LV_GPU_SDL_LRU_SIZE (1024 * 1024 * 8) + #define LV_USE_DRAW_DMA2D_INTERRUPT 0 #endif #endif - /*Custom blend mode for mask drawing, disable if you need to link with older SDL2 lib*/ - #ifndef LV_GPU_SDL_CUSTOM_BLEND_MODE - #ifdef CONFIG_LV_GPU_SDL_CUSTOM_BLEND_MODE - #define LV_GPU_SDL_CUSTOM_BLEND_MODE CONFIG_LV_GPU_SDL_CUSTOM_BLEND_MODE - #else - #define LV_GPU_SDL_CUSTOM_BLEND_MODE (SDL_VERSION_ATLEAST(2, 0, 6)) - #endif +#endif + +/** Draw using cached OpenGLES textures */ +#ifndef LV_USE_DRAW_OPENGLES + #ifdef CONFIG_LV_USE_DRAW_OPENGLES + #define LV_USE_DRAW_OPENGLES CONFIG_LV_USE_DRAW_OPENGLES + #else + #define LV_USE_DRAW_OPENGLES 0 #endif #endif +/*======================= + * FEATURE CONFIGURATION + *=======================*/ + /*------------- * Logging *-----------*/ -/*Enable the log module*/ +/** Enable log module */ #ifndef LV_USE_LOG #ifdef CONFIG_LV_USE_LOG #define LV_USE_LOG CONFIG_LV_USE_LOG @@ -539,14 +887,13 @@ #endif #endif #if LV_USE_LOG - - /*How important log should be added: - *LV_LOG_LEVEL_TRACE A lot of logs to give detailed information - *LV_LOG_LEVEL_INFO Log important events - *LV_LOG_LEVEL_WARN Log if something unwanted happened but didn't cause a problem - *LV_LOG_LEVEL_ERROR Only critical issue, when the system may fail - *LV_LOG_LEVEL_USER Only logs added by the user - *LV_LOG_LEVEL_NONE Do not log anything*/ + /** Set value to one of the following levels of logging detail: + * - LV_LOG_LEVEL_TRACE Log detailed information. + * - LV_LOG_LEVEL_INFO Log important events. + * - LV_LOG_LEVEL_WARN Log if something unwanted happened but didn't cause a problem. + * - LV_LOG_LEVEL_ERROR Log only critical issues, when system may fail. + * - LV_LOG_LEVEL_USER Log only custom log messages added by the user. + * - LV_LOG_LEVEL_NONE Do not log anything. */ #ifndef LV_LOG_LEVEL #ifdef CONFIG_LV_LOG_LEVEL #define LV_LOG_LEVEL CONFIG_LV_LOG_LEVEL @@ -555,8 +902,8 @@ #endif #endif - /*1: Print the log with 'printf'; - *0: User need to register a callback with `lv_log_register_print_cb()`*/ + /** - 1: Print log with 'printf'; + * - 0: User needs to register a callback with `lv_log_register_print_cb()`. */ #ifndef LV_LOG_PRINTF #ifdef CONFIG_LV_LOG_PRINTF #define LV_LOG_PRINTF CONFIG_LV_LOG_PRINTF @@ -565,149 +912,192 @@ #endif #endif - /*Enable/disable LV_LOG_TRACE in modules that produces a huge number of logs*/ + /** Set callback to print logs. + * E.g `my_print`. The prototype should be `void my_print(lv_log_level_t level, const char * buf)`. + * Can be overwritten by `lv_log_register_print_cb`. */ + //#define LV_LOG_PRINT_CB + + /** - 1: Enable printing timestamp; + * - 0: Disable printing timestamp. */ + #ifndef LV_LOG_USE_TIMESTAMP + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_LOG_USE_TIMESTAMP + #define LV_LOG_USE_TIMESTAMP CONFIG_LV_LOG_USE_TIMESTAMP + #else + #define LV_LOG_USE_TIMESTAMP 0 + #endif + #else + #define LV_LOG_USE_TIMESTAMP 1 + #endif + #endif + + /** - 1: Print file and line number of the log; + * - 0: Do not print file and line number of the log. */ + #ifndef LV_LOG_USE_FILE_LINE + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_LOG_USE_FILE_LINE + #define LV_LOG_USE_FILE_LINE CONFIG_LV_LOG_USE_FILE_LINE + #else + #define LV_LOG_USE_FILE_LINE 0 + #endif + #else + #define LV_LOG_USE_FILE_LINE 1 + #endif + #endif + + /* Enable/disable LV_LOG_TRACE in modules that produces a huge number of logs. */ #ifndef LV_LOG_TRACE_MEM - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_LOG_TRACE_MEM #define LV_LOG_TRACE_MEM CONFIG_LV_LOG_TRACE_MEM #else #define LV_LOG_TRACE_MEM 0 #endif #else - #define LV_LOG_TRACE_MEM 1 + #define LV_LOG_TRACE_MEM 1 /**< Enable/disable trace logs in memory operations. */ #endif #endif #ifndef LV_LOG_TRACE_TIMER - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_LOG_TRACE_TIMER #define LV_LOG_TRACE_TIMER CONFIG_LV_LOG_TRACE_TIMER #else #define LV_LOG_TRACE_TIMER 0 #endif #else - #define LV_LOG_TRACE_TIMER 1 + #define LV_LOG_TRACE_TIMER 1 /**< Enable/disable trace logs in timer operations. */ #endif #endif #ifndef LV_LOG_TRACE_INDEV - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_LOG_TRACE_INDEV #define LV_LOG_TRACE_INDEV CONFIG_LV_LOG_TRACE_INDEV #else #define LV_LOG_TRACE_INDEV 0 #endif #else - #define LV_LOG_TRACE_INDEV 1 + #define LV_LOG_TRACE_INDEV 1 /**< Enable/disable trace logs in input device operations. */ #endif #endif #ifndef LV_LOG_TRACE_DISP_REFR - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_LOG_TRACE_DISP_REFR #define LV_LOG_TRACE_DISP_REFR CONFIG_LV_LOG_TRACE_DISP_REFR #else #define LV_LOG_TRACE_DISP_REFR 0 #endif #else - #define LV_LOG_TRACE_DISP_REFR 1 + #define LV_LOG_TRACE_DISP_REFR 1 /**< Enable/disable trace logs in display re-draw operations. */ #endif #endif #ifndef LV_LOG_TRACE_EVENT - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_LOG_TRACE_EVENT #define LV_LOG_TRACE_EVENT CONFIG_LV_LOG_TRACE_EVENT #else #define LV_LOG_TRACE_EVENT 0 #endif #else - #define LV_LOG_TRACE_EVENT 1 + #define LV_LOG_TRACE_EVENT 1 /**< Enable/disable trace logs in event dispatch logic. */ #endif #endif #ifndef LV_LOG_TRACE_OBJ_CREATE - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_LOG_TRACE_OBJ_CREATE #define LV_LOG_TRACE_OBJ_CREATE CONFIG_LV_LOG_TRACE_OBJ_CREATE #else #define LV_LOG_TRACE_OBJ_CREATE 0 #endif #else - #define LV_LOG_TRACE_OBJ_CREATE 1 + #define LV_LOG_TRACE_OBJ_CREATE 1 /**< Enable/disable trace logs in object creation (core `obj` creation plus every widget). */ #endif #endif #ifndef LV_LOG_TRACE_LAYOUT - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_LOG_TRACE_LAYOUT #define LV_LOG_TRACE_LAYOUT CONFIG_LV_LOG_TRACE_LAYOUT #else #define LV_LOG_TRACE_LAYOUT 0 #endif #else - #define LV_LOG_TRACE_LAYOUT 1 + #define LV_LOG_TRACE_LAYOUT 1 /**< Enable/disable trace logs in flex- and grid-layout operations. */ #endif #endif #ifndef LV_LOG_TRACE_ANIM - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_LOG_TRACE_ANIM #define LV_LOG_TRACE_ANIM CONFIG_LV_LOG_TRACE_ANIM #else #define LV_LOG_TRACE_ANIM 0 #endif #else - #define LV_LOG_TRACE_ANIM 1 + #define LV_LOG_TRACE_ANIM 1 /**< Enable/disable trace logs in animation logic. */ + #endif + #endif + #ifndef LV_LOG_TRACE_CACHE + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_LOG_TRACE_CACHE + #define LV_LOG_TRACE_CACHE CONFIG_LV_LOG_TRACE_CACHE + #else + #define LV_LOG_TRACE_CACHE 0 + #endif + #else + #define LV_LOG_TRACE_CACHE 1 /**< Enable/disable trace logs in cache operations. */ #endif #endif - #endif /*LV_USE_LOG*/ /*------------- * Asserts *-----------*/ -/*Enable asserts if an operation is failed or an invalid data is found. - *If LV_USE_LOG is enabled an error message will be printed on failure*/ +/* Enable assertion failures if an operation fails or invalid data is found. + * If LV_USE_LOG is enabled, an error message will be printed on failure. */ #ifndef LV_USE_ASSERT_NULL - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_USE_ASSERT_NULL #define LV_USE_ASSERT_NULL CONFIG_LV_USE_ASSERT_NULL #else #define LV_USE_ASSERT_NULL 0 #endif #else - #define LV_USE_ASSERT_NULL 1 /*Check if the parameter is NULL. (Very fast, recommended)*/ + #define LV_USE_ASSERT_NULL 1 /**< Check if the parameter is NULL. (Very fast, recommended) */ #endif #endif #ifndef LV_USE_ASSERT_MALLOC - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_USE_ASSERT_MALLOC #define LV_USE_ASSERT_MALLOC CONFIG_LV_USE_ASSERT_MALLOC #else #define LV_USE_ASSERT_MALLOC 0 #endif #else - #define LV_USE_ASSERT_MALLOC 1 /*Checks is the memory is successfully allocated or no. (Very fast, recommended)*/ + #define LV_USE_ASSERT_MALLOC 1 /**< Checks is the memory is successfully allocated or no. (Very fast, recommended) */ #endif #endif #ifndef LV_USE_ASSERT_STYLE #ifdef CONFIG_LV_USE_ASSERT_STYLE #define LV_USE_ASSERT_STYLE CONFIG_LV_USE_ASSERT_STYLE #else - #define LV_USE_ASSERT_STYLE 0 /*Check if the styles are properly initialized. (Very fast, recommended)*/ + #define LV_USE_ASSERT_STYLE 0 /**< Check if the styles are properly initialized. (Very fast, recommended) */ #endif #endif #ifndef LV_USE_ASSERT_MEM_INTEGRITY #ifdef CONFIG_LV_USE_ASSERT_MEM_INTEGRITY #define LV_USE_ASSERT_MEM_INTEGRITY CONFIG_LV_USE_ASSERT_MEM_INTEGRITY #else - #define LV_USE_ASSERT_MEM_INTEGRITY 0 /*Check the integrity of `lv_mem` after critical operations. (Slow)*/ + #define LV_USE_ASSERT_MEM_INTEGRITY 0 /**< Check the integrity of `lv_mem` after critical operations. (Slow) */ #endif #endif #ifndef LV_USE_ASSERT_OBJ #ifdef CONFIG_LV_USE_ASSERT_OBJ #define LV_USE_ASSERT_OBJ CONFIG_LV_USE_ASSERT_OBJ #else - #define LV_USE_ASSERT_OBJ 0 /*Check the object's type and existence (e.g. not deleted). (Slow)*/ + #define LV_USE_ASSERT_OBJ 0 /**< Check the object's type and existence (e.g. not deleted). (Slow) */ #endif #endif -/*Add a custom handler when assert happens e.g. to restart the MCU*/ +/** Add a custom handler when assert happens e.g. to restart MCU. */ #ifndef LV_ASSERT_HANDLER_INCLUDE #ifdef CONFIG_LV_ASSERT_HANDLER_INCLUDE #define LV_ASSERT_HANDLER_INCLUDE CONFIG_LV_ASSERT_HANDLER_INCLUDE @@ -719,136 +1109,255 @@ #ifdef CONFIG_LV_ASSERT_HANDLER #define LV_ASSERT_HANDLER CONFIG_LV_ASSERT_HANDLER #else - #define LV_ASSERT_HANDLER while(1); /*Halt by default*/ + #define LV_ASSERT_HANDLER while(1); /**< Halt by default */ #endif #endif /*------------- - * Others + * Debug *-----------*/ -/*1: Show CPU usage and FPS count*/ -#ifndef LV_USE_PERF_MONITOR - #ifdef CONFIG_LV_USE_PERF_MONITOR - #define LV_USE_PERF_MONITOR CONFIG_LV_USE_PERF_MONITOR +/** 1: Draw random colored rectangles over the redrawn areas. */ +#ifndef LV_USE_REFR_DEBUG + #ifdef CONFIG_LV_USE_REFR_DEBUG + #define LV_USE_REFR_DEBUG CONFIG_LV_USE_REFR_DEBUG #else - #define LV_USE_PERF_MONITOR 0 + #define LV_USE_REFR_DEBUG 0 #endif #endif -#if LV_USE_PERF_MONITOR - #ifndef LV_USE_PERF_MONITOR_POS - #ifdef CONFIG_LV_USE_PERF_MONITOR_POS - #define LV_USE_PERF_MONITOR_POS CONFIG_LV_USE_PERF_MONITOR_POS + +/** 1: Draw a red overlay for ARGB layers and a green overlay for RGB layers*/ +#ifndef LV_USE_LAYER_DEBUG + #ifdef CONFIG_LV_USE_LAYER_DEBUG + #define LV_USE_LAYER_DEBUG CONFIG_LV_USE_LAYER_DEBUG + #else + #define LV_USE_LAYER_DEBUG 0 + #endif +#endif + +/** 1: Adds the following behaviors for debugging: + * - Draw overlays with different colors for each draw_unit's tasks. + * - Draw index number of draw unit on white background. + * - For layers, draws index number of draw unit on black background. */ +#ifndef LV_USE_PARALLEL_DRAW_DEBUG + #ifdef CONFIG_LV_USE_PARALLEL_DRAW_DEBUG + #define LV_USE_PARALLEL_DRAW_DEBUG CONFIG_LV_USE_PARALLEL_DRAW_DEBUG + #else + #define LV_USE_PARALLEL_DRAW_DEBUG 0 + #endif +#endif + +/*------------- + * Others + *-----------*/ + +#ifndef LV_ENABLE_GLOBAL_CUSTOM + #ifdef CONFIG_LV_ENABLE_GLOBAL_CUSTOM + #define LV_ENABLE_GLOBAL_CUSTOM CONFIG_LV_ENABLE_GLOBAL_CUSTOM + #else + #define LV_ENABLE_GLOBAL_CUSTOM 0 + #endif +#endif +#if LV_ENABLE_GLOBAL_CUSTOM + /** Header to include for custom 'lv_global' function" */ + #ifndef LV_GLOBAL_CUSTOM_INCLUDE + #ifdef CONFIG_LV_GLOBAL_CUSTOM_INCLUDE + #define LV_GLOBAL_CUSTOM_INCLUDE CONFIG_LV_GLOBAL_CUSTOM_INCLUDE #else - #define LV_USE_PERF_MONITOR_POS LV_ALIGN_BOTTOM_RIGHT + #define LV_GLOBAL_CUSTOM_INCLUDE #endif #endif #endif -/*1: Show the used memory and the memory fragmentation - * Requires LV_MEM_CUSTOM = 0*/ -#ifndef LV_USE_MEM_MONITOR - #ifdef CONFIG_LV_USE_MEM_MONITOR - #define LV_USE_MEM_MONITOR CONFIG_LV_USE_MEM_MONITOR +/** Default cache size in bytes. + * Used by image decoders such as `lv_lodepng` to keep the decoded image in memory. + * If size is not set to 0, the decoder will fail to decode when the cache is full. + * If size is 0, the cache function is not enabled and the decoded memory will be + * released immediately after use. */ +#ifndef LV_CACHE_DEF_SIZE + #ifdef CONFIG_LV_CACHE_DEF_SIZE + #define LV_CACHE_DEF_SIZE CONFIG_LV_CACHE_DEF_SIZE + #else + #define LV_CACHE_DEF_SIZE 0 + #endif +#endif + +/** Default number of image header cache entries. The cache is used to store the headers of images + * The main logic is like `LV_CACHE_DEF_SIZE` but for image headers. */ +#ifndef LV_IMAGE_HEADER_CACHE_DEF_CNT + #ifdef CONFIG_LV_IMAGE_HEADER_CACHE_DEF_CNT + #define LV_IMAGE_HEADER_CACHE_DEF_CNT CONFIG_LV_IMAGE_HEADER_CACHE_DEF_CNT + #else + #define LV_IMAGE_HEADER_CACHE_DEF_CNT 0 + #endif +#endif + +/** Number of stops allowed per gradient. Increase this to allow more stops. + * This adds (sizeof(lv_color_t) + 1) bytes per additional stop. */ +#ifndef LV_GRADIENT_MAX_STOPS + #ifdef CONFIG_LV_GRADIENT_MAX_STOPS + #define LV_GRADIENT_MAX_STOPS CONFIG_LV_GRADIENT_MAX_STOPS + #else + #define LV_GRADIENT_MAX_STOPS 2 + #endif +#endif + +/** Adjust color mix functions rounding. GPUs might calculate color mix (blending) differently. + * - 0: round down, + * - 64: round up from x.75, + * - 128: round up from half, + * - 192: round up from x.25, + * - 254: round up */ +#ifndef LV_COLOR_MIX_ROUND_OFS + #ifdef CONFIG_LV_COLOR_MIX_ROUND_OFS + #define LV_COLOR_MIX_ROUND_OFS CONFIG_LV_COLOR_MIX_ROUND_OFS + #else + #define LV_COLOR_MIX_ROUND_OFS 0 + #endif +#endif + +/** Add 2 x 32-bit variables to each `lv_obj_t` to speed up getting style properties */ +#ifndef LV_OBJ_STYLE_CACHE + #ifdef CONFIG_LV_OBJ_STYLE_CACHE + #define LV_OBJ_STYLE_CACHE CONFIG_LV_OBJ_STYLE_CACHE + #else + #define LV_OBJ_STYLE_CACHE 0 + #endif +#endif + +/** Add `id` field to `lv_obj_t` */ +#ifndef LV_USE_OBJ_ID + #ifdef CONFIG_LV_USE_OBJ_ID + #define LV_USE_OBJ_ID CONFIG_LV_USE_OBJ_ID + #else + #define LV_USE_OBJ_ID 0 + #endif +#endif + +/** Automatically assign an ID when obj is created */ +#ifndef LV_OBJ_ID_AUTO_ASSIGN + #ifdef CONFIG_LV_OBJ_ID_AUTO_ASSIGN + #define LV_OBJ_ID_AUTO_ASSIGN CONFIG_LV_OBJ_ID_AUTO_ASSIGN #else - #define LV_USE_MEM_MONITOR 0 + #define LV_OBJ_ID_AUTO_ASSIGN LV_USE_OBJ_ID #endif #endif -#if LV_USE_MEM_MONITOR - #ifndef LV_USE_MEM_MONITOR_POS - #ifdef CONFIG_LV_USE_MEM_MONITOR_POS - #define LV_USE_MEM_MONITOR_POS CONFIG_LV_USE_MEM_MONITOR_POS + +/** Use builtin obj ID handler functions: +* - lv_obj_assign_id: Called when a widget is created. Use a separate counter for each widget class as an ID. +* - lv_obj_id_compare: Compare the ID to decide if it matches with a requested value. +* - lv_obj_stringify_id: Return string-ified identifier, e.g. "button3". +* - lv_obj_free_id: Does nothing, as there is no memory allocation for the ID. +* When disabled these functions needs to be implemented by the user.*/ +#ifndef LV_USE_OBJ_ID_BUILTIN + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_OBJ_ID_BUILTIN + #define LV_USE_OBJ_ID_BUILTIN CONFIG_LV_USE_OBJ_ID_BUILTIN #else - #define LV_USE_MEM_MONITOR_POS LV_ALIGN_BOTTOM_LEFT + #define LV_USE_OBJ_ID_BUILTIN 0 #endif + #else + #define LV_USE_OBJ_ID_BUILTIN 1 #endif #endif -/*1: Draw random colored rectangles over the redrawn areas*/ -#ifndef LV_USE_REFR_DEBUG - #ifdef CONFIG_LV_USE_REFR_DEBUG - #define LV_USE_REFR_DEBUG CONFIG_LV_USE_REFR_DEBUG +/** Use obj property set/get API. */ +#ifndef LV_USE_OBJ_PROPERTY + #ifdef CONFIG_LV_USE_OBJ_PROPERTY + #define LV_USE_OBJ_PROPERTY CONFIG_LV_USE_OBJ_PROPERTY #else - #define LV_USE_REFR_DEBUG 0 + #define LV_USE_OBJ_PROPERTY 0 + #endif +#endif + +/** Enable property name support. */ +#ifndef LV_USE_OBJ_PROPERTY_NAME + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_OBJ_PROPERTY_NAME + #define LV_USE_OBJ_PROPERTY_NAME CONFIG_LV_USE_OBJ_PROPERTY_NAME + #else + #define LV_USE_OBJ_PROPERTY_NAME 0 + #endif + #else + #define LV_USE_OBJ_PROPERTY_NAME 1 #endif #endif -/*Change the built in (v)snprintf functions*/ -#ifndef LV_SPRINTF_CUSTOM - #ifdef CONFIG_LV_SPRINTF_CUSTOM - #define LV_SPRINTF_CUSTOM CONFIG_LV_SPRINTF_CUSTOM +/* Use VG-Lite Simulator. + * - Requires: LV_USE_THORVG_INTERNAL or LV_USE_THORVG_EXTERNAL */ +#ifndef LV_USE_VG_LITE_THORVG + #ifdef CONFIG_LV_USE_VG_LITE_THORVG + #define LV_USE_VG_LITE_THORVG CONFIG_LV_USE_VG_LITE_THORVG #else - #define LV_SPRINTF_CUSTOM 0 + #define LV_USE_VG_LITE_THORVG 0 #endif #endif -#if LV_SPRINTF_CUSTOM - #ifndef LV_SPRINTF_INCLUDE - #ifdef CONFIG_LV_SPRINTF_INCLUDE - #define LV_SPRINTF_INCLUDE CONFIG_LV_SPRINTF_INCLUDE + +#if LV_USE_VG_LITE_THORVG + /** Enable LVGL's blend mode support */ + #ifndef LV_VG_LITE_THORVG_LVGL_BLEND_SUPPORT + #ifdef CONFIG_LV_VG_LITE_THORVG_LVGL_BLEND_SUPPORT + #define LV_VG_LITE_THORVG_LVGL_BLEND_SUPPORT CONFIG_LV_VG_LITE_THORVG_LVGL_BLEND_SUPPORT #else - #define LV_SPRINTF_INCLUDE + #define LV_VG_LITE_THORVG_LVGL_BLEND_SUPPORT 0 #endif #endif - #ifndef lv_snprintf - #ifdef CONFIG_LV_SNPRINTF - #define lv_snprintf CONFIG_LV_SNPRINTF + + /** Enable YUV color format support */ + #ifndef LV_VG_LITE_THORVG_YUV_SUPPORT + #ifdef CONFIG_LV_VG_LITE_THORVG_YUV_SUPPORT + #define LV_VG_LITE_THORVG_YUV_SUPPORT CONFIG_LV_VG_LITE_THORVG_YUV_SUPPORT #else - #define lv_snprintf snprintf + #define LV_VG_LITE_THORVG_YUV_SUPPORT 0 #endif #endif - #ifndef lv_vsnprintf - #ifdef CONFIG_LV_VSNPRINTF - #define lv_vsnprintf CONFIG_LV_VSNPRINTF + + /** Enable Linear gradient extension support */ + #ifndef LV_VG_LITE_THORVG_LINEAR_GRADIENT_EXT_SUPPORT + #ifdef CONFIG_LV_VG_LITE_THORVG_LINEAR_GRADIENT_EXT_SUPPORT + #define LV_VG_LITE_THORVG_LINEAR_GRADIENT_EXT_SUPPORT CONFIG_LV_VG_LITE_THORVG_LINEAR_GRADIENT_EXT_SUPPORT #else - #define lv_vsnprintf vsnprintf + #define LV_VG_LITE_THORVG_LINEAR_GRADIENT_EXT_SUPPORT 0 #endif #endif -#else /*LV_SPRINTF_CUSTOM*/ - #ifndef LV_SPRINTF_USE_FLOAT - #ifdef CONFIG_LV_SPRINTF_USE_FLOAT - #define LV_SPRINTF_USE_FLOAT CONFIG_LV_SPRINTF_USE_FLOAT + + /** Enable alignment on 16 pixels */ + #ifndef LV_VG_LITE_THORVG_16PIXELS_ALIGN + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_VG_LITE_THORVG_16PIXELS_ALIGN + #define LV_VG_LITE_THORVG_16PIXELS_ALIGN CONFIG_LV_VG_LITE_THORVG_16PIXELS_ALIGN + #else + #define LV_VG_LITE_THORVG_16PIXELS_ALIGN 0 + #endif #else - #define LV_SPRINTF_USE_FLOAT 0 + #define LV_VG_LITE_THORVG_16PIXELS_ALIGN 1 #endif #endif -#endif /*LV_SPRINTF_CUSTOM*/ -#ifndef LV_USE_USER_DATA - #ifdef _LV_KCONFIG_PRESENT - #ifdef CONFIG_LV_USE_USER_DATA - #define LV_USE_USER_DATA CONFIG_LV_USE_USER_DATA + /** Buffer address alignment */ + #ifndef LV_VG_LITE_THORVG_BUF_ADDR_ALIGN + #ifdef CONFIG_LV_VG_LITE_THORVG_BUF_ADDR_ALIGN + #define LV_VG_LITE_THORVG_BUF_ADDR_ALIGN CONFIG_LV_VG_LITE_THORVG_BUF_ADDR_ALIGN #else - #define LV_USE_USER_DATA 0 + #define LV_VG_LITE_THORVG_BUF_ADDR_ALIGN 64 #endif - #else - #define LV_USE_USER_DATA 1 #endif -#endif -/*Garbage Collector settings - *Used if lvgl is bound to higher level language and the memory is managed by that language*/ -#ifndef LV_ENABLE_GC - #ifdef CONFIG_LV_ENABLE_GC - #define LV_ENABLE_GC CONFIG_LV_ENABLE_GC - #else - #define LV_ENABLE_GC 0 - #endif -#endif -#if LV_ENABLE_GC != 0 - #ifndef LV_GC_INCLUDE - #ifdef CONFIG_LV_GC_INCLUDE - #define LV_GC_INCLUDE CONFIG_LV_GC_INCLUDE + /** Enable multi-thread render */ + #ifndef LV_VG_LITE_THORVG_THREAD_RENDER + #ifdef CONFIG_LV_VG_LITE_THORVG_THREAD_RENDER + #define LV_VG_LITE_THORVG_THREAD_RENDER CONFIG_LV_VG_LITE_THORVG_THREAD_RENDER #else - #define LV_GC_INCLUDE "gc.h" /*Include Garbage Collector related things*/ + #define LV_VG_LITE_THORVG_THREAD_RENDER 0 #endif #endif -#endif /*LV_ENABLE_GC*/ +#endif /*===================== * COMPILER SETTINGS *====================*/ -/*For big endian systems set to 1*/ +/** For big endian systems set to 1 */ #ifndef LV_BIG_ENDIAN_SYSTEM #ifdef CONFIG_LV_BIG_ENDIAN_SYSTEM #define LV_BIG_ENDIAN_SYSTEM CONFIG_LV_BIG_ENDIAN_SYSTEM @@ -857,7 +1366,7 @@ #endif #endif -/*Define a custom attribute to `lv_tick_inc` function*/ +/** Define a custom attribute for `lv_tick_inc` function */ #ifndef LV_ATTRIBUTE_TICK_INC #ifdef CONFIG_LV_ATTRIBUTE_TICK_INC #define LV_ATTRIBUTE_TICK_INC CONFIG_LV_ATTRIBUTE_TICK_INC @@ -866,7 +1375,7 @@ #endif #endif -/*Define a custom attribute to `lv_timer_handler` function*/ +/** Define a custom attribute for `lv_timer_handler` function */ #ifndef LV_ATTRIBUTE_TIMER_HANDLER #ifdef CONFIG_LV_ATTRIBUTE_TIMER_HANDLER #define LV_ATTRIBUTE_TIMER_HANDLER CONFIG_LV_ATTRIBUTE_TIMER_HANDLER @@ -875,7 +1384,7 @@ #endif #endif -/*Define a custom attribute to `lv_disp_flush_ready` function*/ +/** Define a custom attribute for `lv_display_flush_ready` function */ #ifndef LV_ATTRIBUTE_FLUSH_READY #ifdef CONFIG_LV_ATTRIBUTE_FLUSH_READY #define LV_ATTRIBUTE_FLUSH_READY CONFIG_LV_ATTRIBUTE_FLUSH_READY @@ -884,9 +1393,10 @@ #endif #endif -/*Required alignment size for buffers*/ +/** Align VG_LITE buffers on this number of bytes. + * @note vglite_src_buf_aligned() uses this value to validate alignment of passed buffer pointers. */ #ifndef LV_ATTRIBUTE_MEM_ALIGN_SIZE - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_ATTRIBUTE_MEM_ALIGN_SIZE #define LV_ATTRIBUTE_MEM_ALIGN_SIZE CONFIG_LV_ATTRIBUTE_MEM_ALIGN_SIZE #else @@ -897,8 +1407,8 @@ #endif #endif -/*Will be added where memories needs to be aligned (with -Os data might not be aligned to boundary by default). - * E.g. __attribute__((aligned(4)))*/ +/** Will be added where memory needs to be aligned (with -Os data might not be aligned to boundary by default). + * E.g. __attribute__((aligned(4)))*/ #ifndef LV_ATTRIBUTE_MEM_ALIGN #ifdef CONFIG_LV_ATTRIBUTE_MEM_ALIGN #define LV_ATTRIBUTE_MEM_ALIGN CONFIG_LV_ATTRIBUTE_MEM_ALIGN @@ -907,7 +1417,7 @@ #endif #endif -/*Attribute to mark large constant arrays for example font's bitmaps*/ +/** Attribute to mark large constant arrays, for example for font bitmaps */ #ifndef LV_ATTRIBUTE_LARGE_CONST #ifdef CONFIG_LV_ATTRIBUTE_LARGE_CONST #define LV_ATTRIBUTE_LARGE_CONST CONFIG_LV_ATTRIBUTE_LARGE_CONST @@ -916,7 +1426,7 @@ #endif #endif -/*Compiler prefix for a big array declaration in RAM*/ +/** Compiler prefix for a large array declaration in RAM */ #ifndef LV_ATTRIBUTE_LARGE_RAM_ARRAY #ifdef CONFIG_LV_ATTRIBUTE_LARGE_RAM_ARRAY #define LV_ATTRIBUTE_LARGE_RAM_ARRAY CONFIG_LV_ATTRIBUTE_LARGE_RAM_ARRAY @@ -925,7 +1435,7 @@ #endif #endif -/*Place performance critical functions into a faster memory (e.g RAM)*/ +/** Place performance critical functions into a faster memory (e.g RAM) */ #ifndef LV_ATTRIBUTE_FAST_MEM #ifdef CONFIG_LV_ATTRIBUTE_FAST_MEM #define LV_ATTRIBUTE_FAST_MEM CONFIG_LV_ATTRIBUTE_FAST_MEM @@ -934,31 +1444,50 @@ #endif #endif -/*Prefix variables that are used in GPU accelerated operations, often these need to be placed in RAM sections that are DMA accessible*/ -#ifndef LV_ATTRIBUTE_DMA - #ifdef CONFIG_LV_ATTRIBUTE_DMA - #define LV_ATTRIBUTE_DMA CONFIG_LV_ATTRIBUTE_DMA +/** Export integer constant to binding. This macro is used with constants in the form of LV_ that + * should also appear on LVGL binding API such as MicroPython. */ +#ifndef LV_EXPORT_CONST_INT + #ifdef CONFIG_LV_EXPORT_CONST_INT + #define LV_EXPORT_CONST_INT CONFIG_LV_EXPORT_CONST_INT + #else + #define LV_EXPORT_CONST_INT(int_value) struct _silence_gcc_warning /**< The default value just prevents GCC warning */ + #endif +#endif + +/** Prefix all global extern data with this */ +#ifndef LV_ATTRIBUTE_EXTERN_DATA + #ifdef CONFIG_LV_ATTRIBUTE_EXTERN_DATA + #define LV_ATTRIBUTE_EXTERN_DATA CONFIG_LV_ATTRIBUTE_EXTERN_DATA #else - #define LV_ATTRIBUTE_DMA + #define LV_ATTRIBUTE_EXTERN_DATA #endif #endif -/*Export integer constant to binding. This macro is used with constants in the form of LV_ that - *should also appear on LVGL binding API such as Micropython.*/ -#ifndef LV_EXPORT_CONST_INT - #ifdef CONFIG_LV_EXPORT_CONST_INT - #define LV_EXPORT_CONST_INT CONFIG_LV_EXPORT_CONST_INT +/** Use `float` as `lv_value_precise_t` */ +#ifndef LV_USE_FLOAT + #ifdef CONFIG_LV_USE_FLOAT + #define LV_USE_FLOAT CONFIG_LV_USE_FLOAT #else - #define LV_EXPORT_CONST_INT(int_value) struct _silence_gcc_warning /*The default value just prevents GCC warning*/ + #define LV_USE_FLOAT 0 #endif #endif -/*Extend the default -32k..32k coordinate range to -4M..4M by using int32_t for coordinates instead of int16_t*/ -#ifndef LV_USE_LARGE_COORD - #ifdef CONFIG_LV_USE_LARGE_COORD - #define LV_USE_LARGE_COORD CONFIG_LV_USE_LARGE_COORD +/** Enable matrix support + * - Requires `LV_USE_FLOAT = 1` */ +#ifndef LV_USE_MATRIX + #ifdef CONFIG_LV_USE_MATRIX + #define LV_USE_MATRIX CONFIG_LV_USE_MATRIX #else - #define LV_USE_LARGE_COORD 0 + #define LV_USE_MATRIX 0 + #endif +#endif + +/** Include `lvgl_private.h` in `lvgl.h` to access internal data and functions by default */ +#ifndef LV_USE_PRIVATE_API + #ifdef CONFIG_LV_USE_PRIVATE_API + #define LV_USE_PRIVATE_API CONFIG_LV_USE_PRIVATE_API + #else + #define LV_USE_PRIVATE_API 0 #endif #endif @@ -966,8 +1495,8 @@ * FONT USAGE *===================*/ -/*Montserrat fonts with ASCII range and some symbols using bpp = 4 - *https://fonts.google.com/specimen/Montserrat*/ +/* Montserrat fonts with ASCII range and some symbols using bpp = 4 + * https://fonts.google.com/specimen/Montserrat */ #ifndef LV_FONT_MONTSERRAT_8 #ifdef CONFIG_LV_FONT_MONTSERRAT_8 #define LV_FONT_MONTSERRAT_8 CONFIG_LV_FONT_MONTSERRAT_8 @@ -990,7 +1519,7 @@ #endif #endif #ifndef LV_FONT_MONTSERRAT_14 - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_FONT_MONTSERRAT_14 #define LV_FONT_MONTSERRAT_14 CONFIG_LV_FONT_MONTSERRAT_14 #else @@ -1120,37 +1649,37 @@ #endif #endif -/*Demonstrate special features*/ -#ifndef LV_FONT_MONTSERRAT_12_SUBPX - #ifdef CONFIG_LV_FONT_MONTSERRAT_12_SUBPX - #define LV_FONT_MONTSERRAT_12_SUBPX CONFIG_LV_FONT_MONTSERRAT_12_SUBPX - #else - #define LV_FONT_MONTSERRAT_12_SUBPX 0 - #endif -#endif +/* Demonstrate special features */ #ifndef LV_FONT_MONTSERRAT_28_COMPRESSED #ifdef CONFIG_LV_FONT_MONTSERRAT_28_COMPRESSED #define LV_FONT_MONTSERRAT_28_COMPRESSED CONFIG_LV_FONT_MONTSERRAT_28_COMPRESSED #else - #define LV_FONT_MONTSERRAT_28_COMPRESSED 0 /*bpp = 3*/ + #define LV_FONT_MONTSERRAT_28_COMPRESSED 0 /**< bpp = 3 */ #endif #endif #ifndef LV_FONT_DEJAVU_16_PERSIAN_HEBREW #ifdef CONFIG_LV_FONT_DEJAVU_16_PERSIAN_HEBREW #define LV_FONT_DEJAVU_16_PERSIAN_HEBREW CONFIG_LV_FONT_DEJAVU_16_PERSIAN_HEBREW #else - #define LV_FONT_DEJAVU_16_PERSIAN_HEBREW 0 /*Hebrew, Arabic, Persian letters and all their forms*/ + #define LV_FONT_DEJAVU_16_PERSIAN_HEBREW 0 /**< Hebrew, Arabic, Persian letters and all their forms */ + #endif +#endif +#ifndef LV_FONT_SIMSUN_14_CJK + #ifdef CONFIG_LV_FONT_SIMSUN_14_CJK + #define LV_FONT_SIMSUN_14_CJK CONFIG_LV_FONT_SIMSUN_14_CJK + #else + #define LV_FONT_SIMSUN_14_CJK 0 /**< 1000 most common CJK radicals */ #endif #endif #ifndef LV_FONT_SIMSUN_16_CJK #ifdef CONFIG_LV_FONT_SIMSUN_16_CJK #define LV_FONT_SIMSUN_16_CJK CONFIG_LV_FONT_SIMSUN_16_CJK #else - #define LV_FONT_SIMSUN_16_CJK 0 /*1000 most common CJK radicals*/ + #define LV_FONT_SIMSUN_16_CJK 0 /**< 1000 most common CJK radicals */ #endif #endif -/*Pixel perfect monospace fonts*/ +/** Pixel perfect monospaced fonts */ #ifndef LV_FONT_UNSCII_8 #ifdef CONFIG_LV_FONT_UNSCII_8 #define LV_FONT_UNSCII_8 CONFIG_LV_FONT_UNSCII_8 @@ -1166,9 +1695,15 @@ #endif #endif -/*Optionally declare custom fonts here. - *You can use these fonts as default font too and they will be available globally. - *E.g. #define LV_FONT_CUSTOM_DECLARE LV_FONT_DECLARE(my_font_1) LV_FONT_DECLARE(my_font_2)*/ +/** Optionally declare custom fonts here. + * + * You can use any of these fonts as the default font too and they will be available + * globally. Example: + * + * @code + * #define LV_FONT_CUSTOM_DECLARE LV_FONT_DECLARE(my_font_1) LV_FONT_DECLARE(my_font_2) + * @endcode + */ #ifndef LV_FONT_CUSTOM_DECLARE #ifdef CONFIG_LV_FONT_CUSTOM_DECLARE #define LV_FONT_CUSTOM_DECLARE CONFIG_LV_FONT_CUSTOM_DECLARE @@ -1177,7 +1712,7 @@ #endif #endif -/*Always set a default font*/ +/** Always set a default font */ #ifndef LV_FONT_DEFAULT #ifdef CONFIG_LV_FONT_DEFAULT #define LV_FONT_DEFAULT CONFIG_LV_FONT_DEFAULT @@ -1186,9 +1721,9 @@ #endif #endif -/*Enable handling large font and/or fonts with a lot of characters. - *The limit depends on the font size, font face and bpp. - *Compiler error will be triggered if a font needs it.*/ +/** Enable handling large font and/or fonts with a lot of characters. + * The limit depends on the font size, font face and bpp. + * A compiler error will be triggered if a font needs it. */ #ifndef LV_FONT_FMT_TXT_LARGE #ifdef CONFIG_LV_FONT_FMT_TXT_LARGE #define LV_FONT_FMT_TXT_LARGE CONFIG_LV_FONT_FMT_TXT_LARGE @@ -1197,7 +1732,7 @@ #endif #endif -/*Enables/disables support for compressed fonts.*/ +/** Enables/disables support for compressed fonts. */ #ifndef LV_USE_FONT_COMPRESSED #ifdef CONFIG_LV_USE_FONT_COMPRESSED #define LV_USE_FONT_COMPRESSED CONFIG_LV_USE_FONT_COMPRESSED @@ -1206,28 +1741,9 @@ #endif #endif -/*Enable subpixel rendering*/ -#ifndef LV_USE_FONT_SUBPX - #ifdef CONFIG_LV_USE_FONT_SUBPX - #define LV_USE_FONT_SUBPX CONFIG_LV_USE_FONT_SUBPX - #else - #define LV_USE_FONT_SUBPX 0 - #endif -#endif -#if LV_USE_FONT_SUBPX - /*Set the pixel order of the display. Physical order of RGB channels. Doesn't matter with "normal" fonts.*/ - #ifndef LV_FONT_SUBPX_BGR - #ifdef CONFIG_LV_FONT_SUBPX_BGR - #define LV_FONT_SUBPX_BGR CONFIG_LV_FONT_SUBPX_BGR - #else - #define LV_FONT_SUBPX_BGR 0 /*0: RGB; 1:BGR order*/ - #endif - #endif -#endif - -/*Enable drawing placeholders when glyph dsc is not found*/ +/** Enable drawing placeholders when glyph dsc is not found. */ #ifndef LV_USE_FONT_PLACEHOLDER - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_USE_FONT_PLACEHOLDER #define LV_USE_FONT_PLACEHOLDER CONFIG_LV_USE_FONT_PLACEHOLDER #else @@ -1244,7 +1760,7 @@ /** * Select a character encoding for strings. - * Your IDE or editor should have the same character encoding + * Your IDE or editor should have the same character encoding. * - LV_TXT_ENC_UTF8 * - LV_TXT_ENC_ASCII */ @@ -1256,17 +1772,17 @@ #endif #endif -/*Can break (wrap) texts on these chars*/ +/** While rendering text strings, break (wrap) text on these chars. */ #ifndef LV_TXT_BREAK_CHARS #ifdef CONFIG_LV_TXT_BREAK_CHARS #define LV_TXT_BREAK_CHARS CONFIG_LV_TXT_BREAK_CHARS #else - #define LV_TXT_BREAK_CHARS " ,.;:-_" + #define LV_TXT_BREAK_CHARS " ,.;:-_)]}" #endif #endif -/*If a word is at least this long, will break wherever "prettiest" - *To disable, set to a value <= 0*/ +/** If a word is at least this long, will break wherever "prettiest". + * To disable, set to a value <= 0. */ #ifndef LV_TXT_LINE_BREAK_LONG_LEN #ifdef CONFIG_LV_TXT_LINE_BREAK_LONG_LEN #define LV_TXT_LINE_BREAK_LONG_LEN CONFIG_LV_TXT_LINE_BREAK_LONG_LEN @@ -1275,8 +1791,8 @@ #endif #endif -/*Minimum number of characters in a long word to put on a line before a break. - *Depends on LV_TXT_LINE_BREAK_LONG_LEN.*/ +/** Minimum number of characters in a long word to put on a line before a break. + * Depends on LV_TXT_LINE_BREAK_LONG_LEN. */ #ifndef LV_TXT_LINE_BREAK_LONG_PRE_MIN_LEN #ifdef CONFIG_LV_TXT_LINE_BREAK_LONG_PRE_MIN_LEN #define LV_TXT_LINE_BREAK_LONG_PRE_MIN_LEN CONFIG_LV_TXT_LINE_BREAK_LONG_PRE_MIN_LEN @@ -1285,8 +1801,8 @@ #endif #endif -/*Minimum number of characters in a long word to put on a line after a break. - *Depends on LV_TXT_LINE_BREAK_LONG_LEN.*/ +/** Minimum number of characters in a long word to put on a line after a break. + * Depends on LV_TXT_LINE_BREAK_LONG_LEN. */ #ifndef LV_TXT_LINE_BREAK_LONG_POST_MIN_LEN #ifdef CONFIG_LV_TXT_LINE_BREAK_LONG_POST_MIN_LEN #define LV_TXT_LINE_BREAK_LONG_POST_MIN_LEN CONFIG_LV_TXT_LINE_BREAK_LONG_POST_MIN_LEN @@ -1295,18 +1811,9 @@ #endif #endif -/*The control character to use for signalling text recoloring.*/ -#ifndef LV_TXT_COLOR_CMD - #ifdef CONFIG_LV_TXT_COLOR_CMD - #define LV_TXT_COLOR_CMD CONFIG_LV_TXT_COLOR_CMD - #else - #define LV_TXT_COLOR_CMD "#" - #endif -#endif - -/*Support bidirectional texts. Allows mixing Left-to-Right and Right-to-Left texts. - *The direction will be processed according to the Unicode Bidirectional Algorithm: - *https://www.w3.org/International/articles/inline-bidi-markup/uba-basics*/ +/** Support bidirectional text. Allows mixing Left-to-Right and Right-to-Left text. + * The direction will be processed according to the Unicode Bidirectional Algorithm: + * https://www.w3.org/International/articles/inline-bidi-markup/uba-basics */ #ifndef LV_USE_BIDI #ifdef CONFIG_LV_USE_BIDI #define LV_USE_BIDI CONFIG_LV_USE_BIDI @@ -1318,7 +1825,7 @@ /*Set the default direction. Supported values: *`LV_BASE_DIR_LTR` Left-to-Right *`LV_BASE_DIR_RTL` Right-to-Left - *`LV_BASE_DIR_AUTO` detect texts base direction*/ + *`LV_BASE_DIR_AUTO` detect text base direction*/ #ifndef LV_BIDI_BASE_DIR_DEF #ifdef CONFIG_LV_BIDI_BASE_DIR_DEF #define LV_BIDI_BASE_DIR_DEF CONFIG_LV_BIDI_BASE_DIR_DEF @@ -1328,8 +1835,8 @@ #endif #endif -/*Enable Arabic/Persian processing - *In these languages characters should be replaced with an other form based on their position in the text*/ +/** Enable Arabic/Persian processing + * In these languages characters should be replaced with another form based on their position in the text */ #ifndef LV_USE_ARABIC_PERSIAN_CHARS #ifdef CONFIG_LV_USE_ARABIC_PERSIAN_CHARS #define LV_USE_ARABIC_PERSIAN_CHARS CONFIG_LV_USE_ARABIC_PERSIAN_CHARS @@ -1338,14 +1845,53 @@ #endif #endif +/*The control character to use for signaling text recoloring*/ +#ifndef LV_TXT_COLOR_CMD + #ifdef CONFIG_LV_TXT_COLOR_CMD + #define LV_TXT_COLOR_CMD CONFIG_LV_TXT_COLOR_CMD + #else + #define LV_TXT_COLOR_CMD "#" + #endif +#endif + /*================== - * WIDGET USAGE + * WIDGETS *================*/ +/* Documentation for widgets can be found here: https://docs.lvgl.io/latest/en/html/widgets/index.html . */ + +/** 1: Causes these widgets to be given default values at creation time. + * - lv_buttonmatrix_t: Get default maps: {"Btn1", "Btn2", "Btn3", "\n", "Btn4", "Btn5", ""}, else map not set. + * - lv_checkbox_t : String label set to "Check box", else set to empty string. + * - lv_dropdown_t : Options set to "Option 1", "Option 2", "Option 3", else no values are set. + * - lv_roller_t : Options set to "Option 1", "Option 2", "Option 3", "Option 4", "Option 5", else no values are set. + * - lv_label_t : Text set to "Text", else empty string. + * */ +#ifndef LV_WIDGETS_HAS_DEFAULT_VALUE + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_WIDGETS_HAS_DEFAULT_VALUE + #define LV_WIDGETS_HAS_DEFAULT_VALUE CONFIG_LV_WIDGETS_HAS_DEFAULT_VALUE + #else + #define LV_WIDGETS_HAS_DEFAULT_VALUE 0 + #endif + #else + #define LV_WIDGETS_HAS_DEFAULT_VALUE 1 + #endif +#endif -/*Documentation of the widgets: https://docs.lvgl.io/latest/en/html/widgets/index.html*/ +#ifndef LV_USE_ANIMIMG + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_ANIMIMG + #define LV_USE_ANIMIMG CONFIG_LV_USE_ANIMIMG + #else + #define LV_USE_ANIMIMG 0 + #endif + #else + #define LV_USE_ANIMIMG 1 + #endif +#endif #ifndef LV_USE_ARC - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_USE_ARC #define LV_USE_ARC CONFIG_LV_USE_ARC #else @@ -1357,7 +1903,7 @@ #endif #ifndef LV_USE_BAR - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_USE_BAR #define LV_USE_BAR CONFIG_LV_USE_BAR #else @@ -1368,32 +1914,107 @@ #endif #endif -#ifndef LV_USE_BTN - #ifdef _LV_KCONFIG_PRESENT - #ifdef CONFIG_LV_USE_BTN - #define LV_USE_BTN CONFIG_LV_USE_BTN +#ifndef LV_USE_BUTTON + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_BUTTON + #define LV_USE_BUTTON CONFIG_LV_USE_BUTTON + #else + #define LV_USE_BUTTON 0 + #endif + #else + #define LV_USE_BUTTON 1 + #endif +#endif + +#ifndef LV_USE_BUTTONMATRIX + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_BUTTONMATRIX + #define LV_USE_BUTTONMATRIX CONFIG_LV_USE_BUTTONMATRIX #else - #define LV_USE_BTN 0 + #define LV_USE_BUTTONMATRIX 0 #endif #else - #define LV_USE_BTN 1 + #define LV_USE_BUTTONMATRIX 1 #endif #endif -#ifndef LV_USE_BTNMATRIX - #ifdef _LV_KCONFIG_PRESENT - #ifdef CONFIG_LV_USE_BTNMATRIX - #define LV_USE_BTNMATRIX CONFIG_LV_USE_BTNMATRIX +#ifndef LV_USE_CALENDAR + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_CALENDAR + #define LV_USE_CALENDAR CONFIG_LV_USE_CALENDAR #else - #define LV_USE_BTNMATRIX 0 + #define LV_USE_CALENDAR 0 #endif #else - #define LV_USE_BTNMATRIX 1 + #define LV_USE_CALENDAR 1 #endif #endif +#if LV_USE_CALENDAR + #ifndef LV_CALENDAR_WEEK_STARTS_MONDAY + #ifdef CONFIG_LV_CALENDAR_WEEK_STARTS_MONDAY + #define LV_CALENDAR_WEEK_STARTS_MONDAY CONFIG_LV_CALENDAR_WEEK_STARTS_MONDAY + #else + #define LV_CALENDAR_WEEK_STARTS_MONDAY 0 + #endif + #endif + #if LV_CALENDAR_WEEK_STARTS_MONDAY + #ifndef LV_CALENDAR_DEFAULT_DAY_NAMES + #ifdef CONFIG_LV_CALENDAR_DEFAULT_DAY_NAMES + #define LV_CALENDAR_DEFAULT_DAY_NAMES CONFIG_LV_CALENDAR_DEFAULT_DAY_NAMES + #else + #define LV_CALENDAR_DEFAULT_DAY_NAMES {"Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"} + #endif + #endif + #else + #ifndef LV_CALENDAR_DEFAULT_DAY_NAMES + #ifdef CONFIG_LV_CALENDAR_DEFAULT_DAY_NAMES + #define LV_CALENDAR_DEFAULT_DAY_NAMES CONFIG_LV_CALENDAR_DEFAULT_DAY_NAMES + #else + #define LV_CALENDAR_DEFAULT_DAY_NAMES {"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"} + #endif + #endif + #endif + + #ifndef LV_CALENDAR_DEFAULT_MONTH_NAMES + #ifdef CONFIG_LV_CALENDAR_DEFAULT_MONTH_NAMES + #define LV_CALENDAR_DEFAULT_MONTH_NAMES CONFIG_LV_CALENDAR_DEFAULT_MONTH_NAMES + #else + #define LV_CALENDAR_DEFAULT_MONTH_NAMES {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"} + #endif + #endif + #ifndef LV_USE_CALENDAR_HEADER_ARROW + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_CALENDAR_HEADER_ARROW + #define LV_USE_CALENDAR_HEADER_ARROW CONFIG_LV_USE_CALENDAR_HEADER_ARROW + #else + #define LV_USE_CALENDAR_HEADER_ARROW 0 + #endif + #else + #define LV_USE_CALENDAR_HEADER_ARROW 1 + #endif + #endif + #ifndef LV_USE_CALENDAR_HEADER_DROPDOWN + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_CALENDAR_HEADER_DROPDOWN + #define LV_USE_CALENDAR_HEADER_DROPDOWN CONFIG_LV_USE_CALENDAR_HEADER_DROPDOWN + #else + #define LV_USE_CALENDAR_HEADER_DROPDOWN 0 + #endif + #else + #define LV_USE_CALENDAR_HEADER_DROPDOWN 1 + #endif + #endif + #ifndef LV_USE_CALENDAR_CHINESE + #ifdef CONFIG_LV_USE_CALENDAR_CHINESE + #define LV_USE_CALENDAR_CHINESE CONFIG_LV_USE_CALENDAR_CHINESE + #else + #define LV_USE_CALENDAR_CHINESE 0 + #endif + #endif +#endif /*LV_USE_CALENDAR*/ #ifndef LV_USE_CANVAS - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_USE_CANVAS #define LV_USE_CANVAS CONFIG_LV_USE_CANVAS #else @@ -1404,8 +2025,20 @@ #endif #endif +#ifndef LV_USE_CHART + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_CHART + #define LV_USE_CHART CONFIG_LV_USE_CHART + #else + #define LV_USE_CHART 0 + #endif + #else + #define LV_USE_CHART 1 + #endif +#endif + #ifndef LV_USE_CHECKBOX - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_USE_CHECKBOX #define LV_USE_CHECKBOX CONFIG_LV_USE_CHECKBOX #else @@ -1417,31 +2050,55 @@ #endif #ifndef LV_USE_DROPDOWN - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_USE_DROPDOWN #define LV_USE_DROPDOWN CONFIG_LV_USE_DROPDOWN #else #define LV_USE_DROPDOWN 0 #endif #else - #define LV_USE_DROPDOWN 1 /*Requires: lv_label*/ + #define LV_USE_DROPDOWN 1 /**< Requires: lv_label */ + #endif +#endif + +#ifndef LV_USE_IMAGE + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_IMAGE + #define LV_USE_IMAGE CONFIG_LV_USE_IMAGE + #else + #define LV_USE_IMAGE 0 + #endif + #else + #define LV_USE_IMAGE 1 /**< Requires: lv_label */ + #endif +#endif + +#ifndef LV_USE_IMAGEBUTTON + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_IMAGEBUTTON + #define LV_USE_IMAGEBUTTON CONFIG_LV_USE_IMAGEBUTTON + #else + #define LV_USE_IMAGEBUTTON 0 + #endif + #else + #define LV_USE_IMAGEBUTTON 1 #endif #endif -#ifndef LV_USE_IMG - #ifdef _LV_KCONFIG_PRESENT - #ifdef CONFIG_LV_USE_IMG - #define LV_USE_IMG CONFIG_LV_USE_IMG +#ifndef LV_USE_KEYBOARD + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_KEYBOARD + #define LV_USE_KEYBOARD CONFIG_LV_USE_KEYBOARD #else - #define LV_USE_IMG 0 + #define LV_USE_KEYBOARD 0 #endif #else - #define LV_USE_IMG 1 /*Requires: lv_label*/ + #define LV_USE_KEYBOARD 1 #endif #endif #ifndef LV_USE_LABEL - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_USE_LABEL #define LV_USE_LABEL CONFIG_LV_USE_LABEL #else @@ -1453,31 +2110,50 @@ #endif #if LV_USE_LABEL #ifndef LV_LABEL_TEXT_SELECTION - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_LABEL_TEXT_SELECTION #define LV_LABEL_TEXT_SELECTION CONFIG_LV_LABEL_TEXT_SELECTION #else #define LV_LABEL_TEXT_SELECTION 0 #endif #else - #define LV_LABEL_TEXT_SELECTION 1 /*Enable selecting text of the label*/ + #define LV_LABEL_TEXT_SELECTION 1 /**< Enable selecting text of the label */ #endif #endif #ifndef LV_LABEL_LONG_TXT_HINT - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_LABEL_LONG_TXT_HINT #define LV_LABEL_LONG_TXT_HINT CONFIG_LV_LABEL_LONG_TXT_HINT #else #define LV_LABEL_LONG_TXT_HINT 0 #endif #else - #define LV_LABEL_LONG_TXT_HINT 1 /*Store some extra info in labels to speed up drawing of very long texts*/ + #define LV_LABEL_LONG_TXT_HINT 1 /**< Store some extra info in labels to speed up drawing of very long text */ + #endif + #endif + #ifndef LV_LABEL_WAIT_CHAR_COUNT + #ifdef CONFIG_LV_LABEL_WAIT_CHAR_COUNT + #define LV_LABEL_WAIT_CHAR_COUNT CONFIG_LV_LABEL_WAIT_CHAR_COUNT + #else + #define LV_LABEL_WAIT_CHAR_COUNT 3 /**< The count of wait chart */ + #endif + #endif +#endif + +#ifndef LV_USE_LED + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_LED + #define LV_USE_LED CONFIG_LV_USE_LED + #else + #define LV_USE_LED 0 #endif + #else + #define LV_USE_LED 1 #endif #endif #ifndef LV_USE_LINE - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_USE_LINE #define LV_USE_LINE CONFIG_LV_USE_LINE #else @@ -1488,281 +2164,88 @@ #endif #endif -#ifndef LV_USE_ROLLER - #ifdef _LV_KCONFIG_PRESENT - #ifdef CONFIG_LV_USE_ROLLER - #define LV_USE_ROLLER CONFIG_LV_USE_ROLLER +#ifndef LV_USE_LIST + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_LIST + #define LV_USE_LIST CONFIG_LV_USE_LIST #else - #define LV_USE_ROLLER 0 + #define LV_USE_LIST 0 #endif #else - #define LV_USE_ROLLER 1 /*Requires: lv_label*/ + #define LV_USE_LIST 1 #endif #endif -#if LV_USE_ROLLER - #ifndef LV_ROLLER_INF_PAGES - #ifdef CONFIG_LV_ROLLER_INF_PAGES - #define LV_ROLLER_INF_PAGES CONFIG_LV_ROLLER_INF_PAGES - #else - #define LV_ROLLER_INF_PAGES 7 /*Number of extra "pages" when the roller is infinite*/ - #endif + +#ifndef LV_USE_LOTTIE + #ifdef CONFIG_LV_USE_LOTTIE + #define LV_USE_LOTTIE CONFIG_LV_USE_LOTTIE + #else + #define LV_USE_LOTTIE 0 /**< Requires: lv_canvas, thorvg */ #endif #endif -#ifndef LV_USE_SLIDER - #ifdef _LV_KCONFIG_PRESENT - #ifdef CONFIG_LV_USE_SLIDER - #define LV_USE_SLIDER CONFIG_LV_USE_SLIDER - #else - #define LV_USE_SLIDER 0 - #endif - #else - #define LV_USE_SLIDER 1 /*Requires: lv_bar*/ - #endif -#endif - -#ifndef LV_USE_SWITCH - #ifdef _LV_KCONFIG_PRESENT - #ifdef CONFIG_LV_USE_SWITCH - #define LV_USE_SWITCH CONFIG_LV_USE_SWITCH - #else - #define LV_USE_SWITCH 0 - #endif - #else - #define LV_USE_SWITCH 1 - #endif -#endif - -#ifndef LV_USE_TEXTAREA - #ifdef _LV_KCONFIG_PRESENT - #ifdef CONFIG_LV_USE_TEXTAREA - #define LV_USE_TEXTAREA CONFIG_LV_USE_TEXTAREA - #else - #define LV_USE_TEXTAREA 0 - #endif - #else - #define LV_USE_TEXTAREA 1 /*Requires: lv_label*/ - #endif -#endif -#if LV_USE_TEXTAREA != 0 - #ifndef LV_TEXTAREA_DEF_PWD_SHOW_TIME - #ifdef CONFIG_LV_TEXTAREA_DEF_PWD_SHOW_TIME - #define LV_TEXTAREA_DEF_PWD_SHOW_TIME CONFIG_LV_TEXTAREA_DEF_PWD_SHOW_TIME - #else - #define LV_TEXTAREA_DEF_PWD_SHOW_TIME 1500 /*ms*/ - #endif - #endif -#endif - -#ifndef LV_USE_TABLE - #ifdef _LV_KCONFIG_PRESENT - #ifdef CONFIG_LV_USE_TABLE - #define LV_USE_TABLE CONFIG_LV_USE_TABLE - #else - #define LV_USE_TABLE 0 - #endif - #else - #define LV_USE_TABLE 1 - #endif -#endif - -/*================== - * EXTRA COMPONENTS - *==================*/ - -/*----------- - * Widgets - *----------*/ -#ifndef LV_USE_ANIMIMG - #ifdef _LV_KCONFIG_PRESENT - #ifdef CONFIG_LV_USE_ANIMIMG - #define LV_USE_ANIMIMG CONFIG_LV_USE_ANIMIMG - #else - #define LV_USE_ANIMIMG 0 - #endif - #else - #define LV_USE_ANIMIMG 1 - #endif -#endif - -#ifndef LV_USE_CALENDAR - #ifdef _LV_KCONFIG_PRESENT - #ifdef CONFIG_LV_USE_CALENDAR - #define LV_USE_CALENDAR CONFIG_LV_USE_CALENDAR - #else - #define LV_USE_CALENDAR 0 - #endif - #else - #define LV_USE_CALENDAR 1 - #endif -#endif -#if LV_USE_CALENDAR - #ifndef LV_CALENDAR_WEEK_STARTS_MONDAY - #ifdef CONFIG_LV_CALENDAR_WEEK_STARTS_MONDAY - #define LV_CALENDAR_WEEK_STARTS_MONDAY CONFIG_LV_CALENDAR_WEEK_STARTS_MONDAY - #else - #define LV_CALENDAR_WEEK_STARTS_MONDAY 0 - #endif - #endif - #if LV_CALENDAR_WEEK_STARTS_MONDAY - #ifndef LV_CALENDAR_DEFAULT_DAY_NAMES - #ifdef CONFIG_LV_CALENDAR_DEFAULT_DAY_NAMES - #define LV_CALENDAR_DEFAULT_DAY_NAMES CONFIG_LV_CALENDAR_DEFAULT_DAY_NAMES - #else - #define LV_CALENDAR_DEFAULT_DAY_NAMES {"Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"} - #endif - #endif - #else - #ifndef LV_CALENDAR_DEFAULT_DAY_NAMES - #ifdef CONFIG_LV_CALENDAR_DEFAULT_DAY_NAMES - #define LV_CALENDAR_DEFAULT_DAY_NAMES CONFIG_LV_CALENDAR_DEFAULT_DAY_NAMES - #else - #define LV_CALENDAR_DEFAULT_DAY_NAMES {"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"} - #endif - #endif - #endif - - #ifndef LV_CALENDAR_DEFAULT_MONTH_NAMES - #ifdef CONFIG_LV_CALENDAR_DEFAULT_MONTH_NAMES - #define LV_CALENDAR_DEFAULT_MONTH_NAMES CONFIG_LV_CALENDAR_DEFAULT_MONTH_NAMES - #else - #define LV_CALENDAR_DEFAULT_MONTH_NAMES {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"} - #endif - #endif - #ifndef LV_USE_CALENDAR_HEADER_ARROW - #ifdef _LV_KCONFIG_PRESENT - #ifdef CONFIG_LV_USE_CALENDAR_HEADER_ARROW - #define LV_USE_CALENDAR_HEADER_ARROW CONFIG_LV_USE_CALENDAR_HEADER_ARROW - #else - #define LV_USE_CALENDAR_HEADER_ARROW 0 - #endif - #else - #define LV_USE_CALENDAR_HEADER_ARROW 1 - #endif - #endif - #ifndef LV_USE_CALENDAR_HEADER_DROPDOWN - #ifdef _LV_KCONFIG_PRESENT - #ifdef CONFIG_LV_USE_CALENDAR_HEADER_DROPDOWN - #define LV_USE_CALENDAR_HEADER_DROPDOWN CONFIG_LV_USE_CALENDAR_HEADER_DROPDOWN - #else - #define LV_USE_CALENDAR_HEADER_DROPDOWN 0 - #endif - #else - #define LV_USE_CALENDAR_HEADER_DROPDOWN 1 - #endif - #endif -#endif /*LV_USE_CALENDAR*/ - -#ifndef LV_USE_CHART - #ifdef _LV_KCONFIG_PRESENT - #ifdef CONFIG_LV_USE_CHART - #define LV_USE_CHART CONFIG_LV_USE_CHART - #else - #define LV_USE_CHART 0 - #endif - #else - #define LV_USE_CHART 1 - #endif -#endif - -#ifndef LV_USE_COLORWHEEL - #ifdef _LV_KCONFIG_PRESENT - #ifdef CONFIG_LV_USE_COLORWHEEL - #define LV_USE_COLORWHEEL CONFIG_LV_USE_COLORWHEEL - #else - #define LV_USE_COLORWHEEL 0 - #endif - #else - #define LV_USE_COLORWHEEL 1 - #endif -#endif - -#ifndef LV_USE_IMGBTN - #ifdef _LV_KCONFIG_PRESENT - #ifdef CONFIG_LV_USE_IMGBTN - #define LV_USE_IMGBTN CONFIG_LV_USE_IMGBTN - #else - #define LV_USE_IMGBTN 0 - #endif - #else - #define LV_USE_IMGBTN 1 - #endif -#endif - -#ifndef LV_USE_KEYBOARD - #ifdef _LV_KCONFIG_PRESENT - #ifdef CONFIG_LV_USE_KEYBOARD - #define LV_USE_KEYBOARD CONFIG_LV_USE_KEYBOARD - #else - #define LV_USE_KEYBOARD 0 - #endif - #else - #define LV_USE_KEYBOARD 1 - #endif -#endif - -#ifndef LV_USE_LED - #ifdef _LV_KCONFIG_PRESENT - #ifdef CONFIG_LV_USE_LED - #define LV_USE_LED CONFIG_LV_USE_LED +#ifndef LV_USE_MENU + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_MENU + #define LV_USE_MENU CONFIG_LV_USE_MENU #else - #define LV_USE_LED 0 + #define LV_USE_MENU 0 #endif #else - #define LV_USE_LED 1 + #define LV_USE_MENU 1 #endif #endif -#ifndef LV_USE_LIST - #ifdef _LV_KCONFIG_PRESENT - #ifdef CONFIG_LV_USE_LIST - #define LV_USE_LIST CONFIG_LV_USE_LIST +#ifndef LV_USE_MSGBOX + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_MSGBOX + #define LV_USE_MSGBOX CONFIG_LV_USE_MSGBOX #else - #define LV_USE_LIST 0 + #define LV_USE_MSGBOX 0 #endif #else - #define LV_USE_LIST 1 + #define LV_USE_MSGBOX 1 #endif #endif -#ifndef LV_USE_MENU - #ifdef _LV_KCONFIG_PRESENT - #ifdef CONFIG_LV_USE_MENU - #define LV_USE_MENU CONFIG_LV_USE_MENU +#ifndef LV_USE_ROLLER + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_ROLLER + #define LV_USE_ROLLER CONFIG_LV_USE_ROLLER #else - #define LV_USE_MENU 0 + #define LV_USE_ROLLER 0 #endif #else - #define LV_USE_MENU 1 + #define LV_USE_ROLLER 1 /**< Requires: lv_label */ #endif #endif -#ifndef LV_USE_METER - #ifdef _LV_KCONFIG_PRESENT - #ifdef CONFIG_LV_USE_METER - #define LV_USE_METER CONFIG_LV_USE_METER +#ifndef LV_USE_SCALE + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_SCALE + #define LV_USE_SCALE CONFIG_LV_USE_SCALE #else - #define LV_USE_METER 0 + #define LV_USE_SCALE 0 #endif #else - #define LV_USE_METER 1 + #define LV_USE_SCALE 1 #endif #endif -#ifndef LV_USE_MSGBOX - #ifdef _LV_KCONFIG_PRESENT - #ifdef CONFIG_LV_USE_MSGBOX - #define LV_USE_MSGBOX CONFIG_LV_USE_MSGBOX +#ifndef LV_USE_SLIDER + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_SLIDER + #define LV_USE_SLIDER CONFIG_LV_USE_SLIDER #else - #define LV_USE_MSGBOX 0 + #define LV_USE_SLIDER 0 #endif #else - #define LV_USE_MSGBOX 1 + #define LV_USE_SLIDER 1 /**< Requires: lv_bar */ #endif #endif #ifndef LV_USE_SPAN - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_USE_SPAN #define LV_USE_SPAN CONFIG_LV_USE_SPAN #else @@ -1773,7 +2256,7 @@ #endif #endif #if LV_USE_SPAN - /*A line text can contain maximum num of span descriptor */ + /** A line of text can contain this maximum number of span descriptors. */ #ifndef LV_SPAN_SNIPPET_STACK_SIZE #ifdef CONFIG_LV_SPAN_SNIPPET_STACK_SIZE #define LV_SPAN_SNIPPET_STACK_SIZE CONFIG_LV_SPAN_SNIPPET_STACK_SIZE @@ -1784,7 +2267,7 @@ #endif #ifndef LV_USE_SPINBOX - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_USE_SPINBOX #define LV_USE_SPINBOX CONFIG_LV_USE_SPINBOX #else @@ -1796,7 +2279,7 @@ #endif #ifndef LV_USE_SPINNER - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_USE_SPINNER #define LV_USE_SPINNER CONFIG_LV_USE_SPINNER #else @@ -1807,8 +2290,32 @@ #endif #endif +#ifndef LV_USE_SWITCH + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_SWITCH + #define LV_USE_SWITCH CONFIG_LV_USE_SWITCH + #else + #define LV_USE_SWITCH 0 + #endif + #else + #define LV_USE_SWITCH 1 + #endif +#endif + +#ifndef LV_USE_TABLE + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_TABLE + #define LV_USE_TABLE CONFIG_LV_USE_TABLE + #else + #define LV_USE_TABLE 0 + #endif + #else + #define LV_USE_TABLE 1 + #endif +#endif + #ifndef LV_USE_TABVIEW - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_USE_TABVIEW #define LV_USE_TABVIEW CONFIG_LV_USE_TABVIEW #else @@ -1819,8 +2326,29 @@ #endif #endif +#ifndef LV_USE_TEXTAREA + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_TEXTAREA + #define LV_USE_TEXTAREA CONFIG_LV_USE_TEXTAREA + #else + #define LV_USE_TEXTAREA 0 + #endif + #else + #define LV_USE_TEXTAREA 1 /**< Requires: lv_label */ + #endif +#endif +#if LV_USE_TEXTAREA != 0 + #ifndef LV_TEXTAREA_DEF_PWD_SHOW_TIME + #ifdef CONFIG_LV_TEXTAREA_DEF_PWD_SHOW_TIME + #define LV_TEXTAREA_DEF_PWD_SHOW_TIME CONFIG_LV_TEXTAREA_DEF_PWD_SHOW_TIME + #else + #define LV_TEXTAREA_DEF_PWD_SHOW_TIME 1500 /**< [ms] */ + #endif + #endif +#endif + #ifndef LV_USE_TILEVIEW - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_USE_TILEVIEW #define LV_USE_TILEVIEW CONFIG_LV_USE_TILEVIEW #else @@ -1832,7 +2360,7 @@ #endif #ifndef LV_USE_WIN - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_USE_WIN #define LV_USE_WIN CONFIG_LV_USE_WIN #else @@ -1843,13 +2371,14 @@ #endif #endif -/*----------- - * Themes - *----------*/ +/*================== + * THEMES + *==================*/ +/* Documentation for themes can be found here: https://docs.lvgl.io/master/overview/style.html#themes . */ -/*A simple, impressive and very complete theme*/ +/** A simple, impressive and very complete theme */ #ifndef LV_USE_THEME_DEFAULT - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_USE_THEME_DEFAULT #define LV_USE_THEME_DEFAULT CONFIG_LV_USE_THEME_DEFAULT #else @@ -1860,8 +2389,7 @@ #endif #endif #if LV_USE_THEME_DEFAULT - - /*0: Light mode; 1: Dark mode*/ + /** 0: Light mode; 1: Dark mode */ #ifndef LV_THEME_DEFAULT_DARK #ifdef CONFIG_LV_THEME_DEFAULT_DARK #define LV_THEME_DEFAULT_DARK CONFIG_LV_THEME_DEFAULT_DARK @@ -1870,9 +2398,9 @@ #endif #endif - /*1: Enable grow on press*/ + /** 1: Enable grow on press */ #ifndef LV_THEME_DEFAULT_GROW - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_THEME_DEFAULT_GROW #define LV_THEME_DEFAULT_GROW CONFIG_LV_THEME_DEFAULT_GROW #else @@ -1883,7 +2411,7 @@ #endif #endif - /*Default transition time in [ms]*/ + /** Default transition time in ms. */ #ifndef LV_THEME_DEFAULT_TRANSITION_TIME #ifdef CONFIG_LV_THEME_DEFAULT_TRANSITION_TIME #define LV_THEME_DEFAULT_TRANSITION_TIME CONFIG_LV_THEME_DEFAULT_TRANSITION_TIME @@ -1893,22 +2421,22 @@ #endif #endif /*LV_USE_THEME_DEFAULT*/ -/*A very simple theme that is a good starting point for a custom theme*/ -#ifndef LV_USE_THEME_BASIC - #ifdef _LV_KCONFIG_PRESENT - #ifdef CONFIG_LV_USE_THEME_BASIC - #define LV_USE_THEME_BASIC CONFIG_LV_USE_THEME_BASIC +/** A very simple theme that is a good starting point for a custom theme */ +#ifndef LV_USE_THEME_SIMPLE + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_THEME_SIMPLE + #define LV_USE_THEME_SIMPLE CONFIG_LV_USE_THEME_SIMPLE #else - #define LV_USE_THEME_BASIC 0 + #define LV_USE_THEME_SIMPLE 0 #endif #else - #define LV_USE_THEME_BASIC 1 + #define LV_USE_THEME_SIMPLE 1 #endif #endif -/*A theme designed for monochrome displays*/ +/** A theme designed for monochrome displays */ #ifndef LV_USE_THEME_MONO - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_USE_THEME_MONO #define LV_USE_THEME_MONO CONFIG_LV_USE_THEME_MONO #else @@ -1919,13 +2447,14 @@ #endif #endif -/*----------- - * Layouts - *----------*/ +/*================== + * LAYOUTS + *==================*/ +/* Documentation for layouts can be found here: https://docs.lvgl.io/master/layouts/index.html . */ -/*A layout similar to Flexbox in CSS.*/ +/** A layout similar to Flexbox in CSS. */ #ifndef LV_USE_FLEX - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_USE_FLEX #define LV_USE_FLEX CONFIG_LV_USE_FLEX #else @@ -1936,9 +2465,9 @@ #endif #endif -/*A layout similar to Grid in CSS.*/ +/** A layout similar to Grid in CSS. */ #ifndef LV_USE_GRID - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_USE_GRID #define LV_USE_GRID CONFIG_LV_USE_GRID #else @@ -1949,13 +2478,23 @@ #endif #endif -/*--------------------- - * 3rd party libraries - *--------------------*/ +/*==================== + * 3RD PARTS LIBRARIES + *====================*/ +/* Documentation for libraries can be found here: https://docs.lvgl.io/master/libs/index.html . */ + +/* File system interfaces for common APIs */ -/*File system interfaces for common APIs */ +/** Setting a default driver letter allows skipping the driver prefix in filepaths. */ +#ifndef LV_FS_DEFAULT_DRIVE_LETTER + #ifdef CONFIG_LV_FS_DEFAULT_DRIVE_LETTER + #define LV_FS_DEFAULT_DRIVE_LETTER CONFIG_LV_FS_DEFAULT_DRIVE_LETTER + #else + #define LV_FS_DEFAULT_DRIVE_LETTER '\0' + #endif +#endif -/*API for fopen, fread, etc*/ +/** API for fopen, fread, etc. */ #ifndef LV_USE_FS_STDIO #ifdef CONFIG_LV_USE_FS_STDIO #define LV_USE_FS_STDIO CONFIG_LV_USE_FS_STDIO @@ -1968,26 +2507,26 @@ #ifdef CONFIG_LV_FS_STDIO_LETTER #define LV_FS_STDIO_LETTER CONFIG_LV_FS_STDIO_LETTER #else - #define LV_FS_STDIO_LETTER '\0' /*Set an upper cased letter on which the drive will accessible (e.g. 'A')*/ + #define LV_FS_STDIO_LETTER '\0' /**< Set an upper cased letter on which the drive will accessible (e.g. 'A') */ #endif #endif #ifndef LV_FS_STDIO_PATH #ifdef CONFIG_LV_FS_STDIO_PATH #define LV_FS_STDIO_PATH CONFIG_LV_FS_STDIO_PATH #else - #define LV_FS_STDIO_PATH "" /*Set the working directory. File/directory paths will be appended to it.*/ + #define LV_FS_STDIO_PATH "" /**< Set the working directory. File/directory paths will be appended to it. */ #endif #endif #ifndef LV_FS_STDIO_CACHE_SIZE #ifdef CONFIG_LV_FS_STDIO_CACHE_SIZE #define LV_FS_STDIO_CACHE_SIZE CONFIG_LV_FS_STDIO_CACHE_SIZE #else - #define LV_FS_STDIO_CACHE_SIZE 0 /*>0 to cache this number of bytes in lv_fs_read()*/ + #define LV_FS_STDIO_CACHE_SIZE 0 /**< >0 to cache this number of bytes in lv_fs_read() */ #endif #endif #endif -/*API for open, read, etc*/ +/** API for open, read, etc. */ #ifndef LV_USE_FS_POSIX #ifdef CONFIG_LV_USE_FS_POSIX #define LV_USE_FS_POSIX CONFIG_LV_USE_FS_POSIX @@ -2000,26 +2539,26 @@ #ifdef CONFIG_LV_FS_POSIX_LETTER #define LV_FS_POSIX_LETTER CONFIG_LV_FS_POSIX_LETTER #else - #define LV_FS_POSIX_LETTER '\0' /*Set an upper cased letter on which the drive will accessible (e.g. 'A')*/ + #define LV_FS_POSIX_LETTER '\0' /**< Set an upper cased letter on which the drive will accessible (e.g. 'A') */ #endif #endif #ifndef LV_FS_POSIX_PATH #ifdef CONFIG_LV_FS_POSIX_PATH #define LV_FS_POSIX_PATH CONFIG_LV_FS_POSIX_PATH #else - #define LV_FS_POSIX_PATH "" /*Set the working directory. File/directory paths will be appended to it.*/ + #define LV_FS_POSIX_PATH "" /**< Set the working directory. File/directory paths will be appended to it. */ #endif #endif #ifndef LV_FS_POSIX_CACHE_SIZE #ifdef CONFIG_LV_FS_POSIX_CACHE_SIZE #define LV_FS_POSIX_CACHE_SIZE CONFIG_LV_FS_POSIX_CACHE_SIZE #else - #define LV_FS_POSIX_CACHE_SIZE 0 /*>0 to cache this number of bytes in lv_fs_read()*/ + #define LV_FS_POSIX_CACHE_SIZE 0 /**< >0 to cache this number of bytes in lv_fs_read() */ #endif #endif #endif -/*API for CreateFile, ReadFile, etc*/ +/** API for CreateFile, ReadFile, etc. */ #ifndef LV_USE_FS_WIN32 #ifdef CONFIG_LV_USE_FS_WIN32 #define LV_USE_FS_WIN32 CONFIG_LV_USE_FS_WIN32 @@ -2032,26 +2571,26 @@ #ifdef CONFIG_LV_FS_WIN32_LETTER #define LV_FS_WIN32_LETTER CONFIG_LV_FS_WIN32_LETTER #else - #define LV_FS_WIN32_LETTER '\0' /*Set an upper cased letter on which the drive will accessible (e.g. 'A')*/ + #define LV_FS_WIN32_LETTER '\0' /**< Set an upper cased letter on which the drive will accessible (e.g. 'A') */ #endif #endif #ifndef LV_FS_WIN32_PATH #ifdef CONFIG_LV_FS_WIN32_PATH #define LV_FS_WIN32_PATH CONFIG_LV_FS_WIN32_PATH #else - #define LV_FS_WIN32_PATH "" /*Set the working directory. File/directory paths will be appended to it.*/ + #define LV_FS_WIN32_PATH "" /**< Set the working directory. File/directory paths will be appended to it. */ #endif #endif #ifndef LV_FS_WIN32_CACHE_SIZE #ifdef CONFIG_LV_FS_WIN32_CACHE_SIZE #define LV_FS_WIN32_CACHE_SIZE CONFIG_LV_FS_WIN32_CACHE_SIZE #else - #define LV_FS_WIN32_CACHE_SIZE 0 /*>0 to cache this number of bytes in lv_fs_read()*/ + #define LV_FS_WIN32_CACHE_SIZE 0 /**< >0 to cache this number of bytes in lv_fs_read() */ #endif #endif #endif -/*API for FATFS (needs to be added separately). Uses f_open, f_read, etc*/ +/** API for FATFS (needs to be added separately). Uses f_open, f_read, etc. */ #ifndef LV_USE_FS_FATFS #ifdef CONFIG_LV_USE_FS_FATFS #define LV_USE_FS_FATFS CONFIG_LV_USE_FS_FATFS @@ -2064,28 +2603,109 @@ #ifdef CONFIG_LV_FS_FATFS_LETTER #define LV_FS_FATFS_LETTER CONFIG_LV_FS_FATFS_LETTER #else - #define LV_FS_FATFS_LETTER '\0' /*Set an upper cased letter on which the drive will accessible (e.g. 'A')*/ + #define LV_FS_FATFS_LETTER '\0' /**< Set an upper cased letter on which the drive will accessible (e.g. 'A') */ #endif #endif #ifndef LV_FS_FATFS_CACHE_SIZE #ifdef CONFIG_LV_FS_FATFS_CACHE_SIZE #define LV_FS_FATFS_CACHE_SIZE CONFIG_LV_FS_FATFS_CACHE_SIZE #else - #define LV_FS_FATFS_CACHE_SIZE 0 /*>0 to cache this number of bytes in lv_fs_read()*/ + #define LV_FS_FATFS_CACHE_SIZE 0 /**< >0 to cache this number of bytes in lv_fs_read() */ #endif #endif #endif -/*PNG decoder library*/ -#ifndef LV_USE_PNG - #ifdef CONFIG_LV_USE_PNG - #define LV_USE_PNG CONFIG_LV_USE_PNG +/** API for memory-mapped file access. */ +#ifndef LV_USE_FS_MEMFS + #ifdef CONFIG_LV_USE_FS_MEMFS + #define LV_USE_FS_MEMFS CONFIG_LV_USE_FS_MEMFS #else - #define LV_USE_PNG 0 + #define LV_USE_FS_MEMFS 0 + #endif +#endif +#if LV_USE_FS_MEMFS + #ifndef LV_FS_MEMFS_LETTER + #ifdef CONFIG_LV_FS_MEMFS_LETTER + #define LV_FS_MEMFS_LETTER CONFIG_LV_FS_MEMFS_LETTER + #else + #define LV_FS_MEMFS_LETTER '\0' /**< Set an upper cased letter on which the drive will accessible (e.g. 'A') */ + #endif #endif #endif -/*BMP decoder library*/ +/** API for LittleFs. */ +#ifndef LV_USE_FS_LITTLEFS + #ifdef CONFIG_LV_USE_FS_LITTLEFS + #define LV_USE_FS_LITTLEFS CONFIG_LV_USE_FS_LITTLEFS + #else + #define LV_USE_FS_LITTLEFS 0 + #endif +#endif +#if LV_USE_FS_LITTLEFS + #ifndef LV_FS_LITTLEFS_LETTER + #ifdef CONFIG_LV_FS_LITTLEFS_LETTER + #define LV_FS_LITTLEFS_LETTER CONFIG_LV_FS_LITTLEFS_LETTER + #else + #define LV_FS_LITTLEFS_LETTER '\0' /**< Set an upper cased letter on which the drive will accessible (e.g. 'A') */ + #endif + #endif +#endif + +/** API for Arduino LittleFs. */ +#ifndef LV_USE_FS_ARDUINO_ESP_LITTLEFS + #ifdef CONFIG_LV_USE_FS_ARDUINO_ESP_LITTLEFS + #define LV_USE_FS_ARDUINO_ESP_LITTLEFS CONFIG_LV_USE_FS_ARDUINO_ESP_LITTLEFS + #else + #define LV_USE_FS_ARDUINO_ESP_LITTLEFS 0 + #endif +#endif +#if LV_USE_FS_ARDUINO_ESP_LITTLEFS + #ifndef LV_FS_ARDUINO_ESP_LITTLEFS_LETTER + #ifdef CONFIG_LV_FS_ARDUINO_ESP_LITTLEFS_LETTER + #define LV_FS_ARDUINO_ESP_LITTLEFS_LETTER CONFIG_LV_FS_ARDUINO_ESP_LITTLEFS_LETTER + #else + #define LV_FS_ARDUINO_ESP_LITTLEFS_LETTER '\0' /**< Set an upper cased letter on which the drive will accessible (e.g. 'A') */ + #endif + #endif +#endif + +/** API for Arduino Sd. */ +#ifndef LV_USE_FS_ARDUINO_SD + #ifdef CONFIG_LV_USE_FS_ARDUINO_SD + #define LV_USE_FS_ARDUINO_SD CONFIG_LV_USE_FS_ARDUINO_SD + #else + #define LV_USE_FS_ARDUINO_SD 0 + #endif +#endif +#if LV_USE_FS_ARDUINO_SD + #ifndef LV_FS_ARDUINO_SD_LETTER + #ifdef CONFIG_LV_FS_ARDUINO_SD_LETTER + #define LV_FS_ARDUINO_SD_LETTER CONFIG_LV_FS_ARDUINO_SD_LETTER + #else + #define LV_FS_ARDUINO_SD_LETTER '\0' /**< Set an upper cased letter on which the drive will accessible (e.g. 'A') */ + #endif + #endif +#endif + +/** LODEPNG decoder library */ +#ifndef LV_USE_LODEPNG + #ifdef CONFIG_LV_USE_LODEPNG + #define LV_USE_LODEPNG CONFIG_LV_USE_LODEPNG + #else + #define LV_USE_LODEPNG 0 + #endif +#endif + +/** PNG decoder(libpng) library */ +#ifndef LV_USE_LIBPNG + #ifdef CONFIG_LV_USE_LIBPNG + #define LV_USE_LIBPNG CONFIG_LV_USE_LIBPNG + #else + #define LV_USE_LIBPNG 0 + #endif +#endif + +/** BMP decoder library */ #ifndef LV_USE_BMP #ifdef CONFIG_LV_USE_BMP #define LV_USE_BMP CONFIG_LV_USE_BMP @@ -2094,17 +2714,27 @@ #endif #endif -/* JPG + split JPG decoder library. - * Split JPG is a custom format optimized for embedded systems. */ -#ifndef LV_USE_SJPG - #ifdef CONFIG_LV_USE_SJPG - #define LV_USE_SJPG CONFIG_LV_USE_SJPG +/** JPG + split JPG decoder library. + * Split JPG is a custom format optimized for embedded systems. */ +#ifndef LV_USE_TJPGD + #ifdef CONFIG_LV_USE_TJPGD + #define LV_USE_TJPGD CONFIG_LV_USE_TJPGD + #else + #define LV_USE_TJPGD 0 + #endif +#endif + +/** libjpeg-turbo decoder library. + * - Supports complete JPEG specifications and high-performance JPEG decoding. */ +#ifndef LV_USE_LIBJPEG_TURBO + #ifdef CONFIG_LV_USE_LIBJPEG_TURBO + #define LV_USE_LIBJPEG_TURBO CONFIG_LV_USE_LIBJPEG_TURBO #else - #define LV_USE_SJPG 0 + #define LV_USE_LIBJPEG_TURBO 0 #endif #endif -/*GIF decoder library*/ +/** GIF decoder library */ #ifndef LV_USE_GIF #ifdef CONFIG_LV_USE_GIF #define LV_USE_GIF CONFIG_LV_USE_GIF @@ -2112,8 +2742,37 @@ #define LV_USE_GIF 0 #endif #endif + #if LV_USE_GIF + /** GIF decoder accelerate */ + #ifndef LV_GIF_CACHE_DECODE_DATA + #ifdef CONFIG_LV_GIF_CACHE_DECODE_DATA + #define LV_GIF_CACHE_DECODE_DATA CONFIG_LV_GIF_CACHE_DECODE_DATA + #else + #define LV_GIF_CACHE_DECODE_DATA 0 + #endif + #endif +#endif + + +/** Decode bin images to RAM */ +#ifndef LV_BIN_DECODER_RAM_LOAD + #ifdef CONFIG_LV_BIN_DECODER_RAM_LOAD + #define LV_BIN_DECODER_RAM_LOAD CONFIG_LV_BIN_DECODER_RAM_LOAD + #else + #define LV_BIN_DECODER_RAM_LOAD 0 + #endif +#endif -/*QR code library*/ +/** RLE decompress library */ +#ifndef LV_USE_RLE + #ifdef CONFIG_LV_USE_RLE + #define LV_USE_RLE CONFIG_LV_USE_RLE + #else + #define LV_USE_RLE 0 + #endif +#endif + +/** QR code library */ #ifndef LV_USE_QRCODE #ifdef CONFIG_LV_USE_QRCODE #define LV_USE_QRCODE CONFIG_LV_USE_QRCODE @@ -2122,7 +2781,16 @@ #endif #endif -/*FreeType library*/ +/** Barcode code library */ +#ifndef LV_USE_BARCODE + #ifdef CONFIG_LV_USE_BARCODE + #define LV_USE_BARCODE CONFIG_LV_USE_BARCODE + #else + #define LV_USE_BARCODE 0 + #endif +#endif + +/** FreeType library */ #ifndef LV_USE_FREETYPE #ifdef CONFIG_LV_USE_FREETYPE #define LV_USE_FREETYPE CONFIG_LV_USE_FREETYPE @@ -2131,45 +2799,53 @@ #endif #endif #if LV_USE_FREETYPE - /*Memory used by FreeType to cache characters [bytes] (-1: no caching)*/ - #ifndef LV_FREETYPE_CACHE_SIZE - #ifdef CONFIG_LV_FREETYPE_CACHE_SIZE - #define LV_FREETYPE_CACHE_SIZE CONFIG_LV_FREETYPE_CACHE_SIZE + /** Let FreeType use LVGL memory and file porting */ + #ifndef LV_FREETYPE_USE_LVGL_PORT + #ifdef CONFIG_LV_FREETYPE_USE_LVGL_PORT + #define LV_FREETYPE_USE_LVGL_PORT CONFIG_LV_FREETYPE_USE_LVGL_PORT #else - #define LV_FREETYPE_CACHE_SIZE (16 * 1024) + #define LV_FREETYPE_USE_LVGL_PORT 0 #endif #endif - #if LV_FREETYPE_CACHE_SIZE >= 0 - /* 1: bitmap cache use the sbit cache, 0:bitmap cache use the image cache. */ - /* sbit cache:it is much more memory efficient for small bitmaps(font size < 256) */ - /* if font size >= 256, must be configured as image cache */ - #ifndef LV_FREETYPE_SBIT_CACHE - #ifdef CONFIG_LV_FREETYPE_SBIT_CACHE - #define LV_FREETYPE_SBIT_CACHE CONFIG_LV_FREETYPE_SBIT_CACHE - #else - #define LV_FREETYPE_SBIT_CACHE 0 - #endif + + /** Cache count of glyphs in FreeType, i.e. number of glyphs that can be cached. + * The higher the value, the more memory will be used. */ + #ifndef LV_FREETYPE_CACHE_FT_GLYPH_CNT + #ifdef CONFIG_LV_FREETYPE_CACHE_FT_GLYPH_CNT + #define LV_FREETYPE_CACHE_FT_GLYPH_CNT CONFIG_LV_FREETYPE_CACHE_FT_GLYPH_CNT + #else + #define LV_FREETYPE_CACHE_FT_GLYPH_CNT 256 #endif - /* Maximum number of opened FT_Face/FT_Size objects managed by this cache instance. */ - /* (0:use system defaults) */ - #ifndef LV_FREETYPE_CACHE_FT_FACES - #ifdef CONFIG_LV_FREETYPE_CACHE_FT_FACES - #define LV_FREETYPE_CACHE_FT_FACES CONFIG_LV_FREETYPE_CACHE_FT_FACES - #else - #define LV_FREETYPE_CACHE_FT_FACES 0 - #endif + #endif +#endif + +/** Built-in TTF decoder */ +#ifndef LV_USE_TINY_TTF + #ifdef CONFIG_LV_USE_TINY_TTF + #define LV_USE_TINY_TTF CONFIG_LV_USE_TINY_TTF + #else + #define LV_USE_TINY_TTF 0 + #endif +#endif +#if LV_USE_TINY_TTF + /* Enable loading TTF data from files */ + #ifndef LV_TINY_TTF_FILE_SUPPORT + #ifdef CONFIG_LV_TINY_TTF_FILE_SUPPORT + #define LV_TINY_TTF_FILE_SUPPORT CONFIG_LV_TINY_TTF_FILE_SUPPORT + #else + #define LV_TINY_TTF_FILE_SUPPORT 0 #endif - #ifndef LV_FREETYPE_CACHE_FT_SIZES - #ifdef CONFIG_LV_FREETYPE_CACHE_FT_SIZES - #define LV_FREETYPE_CACHE_FT_SIZES CONFIG_LV_FREETYPE_CACHE_FT_SIZES - #else - #define LV_FREETYPE_CACHE_FT_SIZES 0 - #endif + #endif + #ifndef LV_TINY_TTF_CACHE_GLYPH_CNT + #ifdef CONFIG_LV_TINY_TTF_CACHE_GLYPH_CNT + #define LV_TINY_TTF_CACHE_GLYPH_CNT CONFIG_LV_TINY_TTF_CACHE_GLYPH_CNT + #else + #define LV_TINY_TTF_CACHE_GLYPH_CNT 256 #endif #endif #endif -/*Rlottie library*/ +/** Rlottie library */ #ifndef LV_USE_RLOTTIE #ifdef CONFIG_LV_USE_RLOTTIE #define LV_USE_RLOTTIE CONFIG_LV_USE_RLOTTIE @@ -2178,8 +2854,77 @@ #endif #endif -/*FFmpeg library for image decoding and playing videos - *Supports all major image formats so do not enable other image decoder with it*/ +/** Enable Vector Graphic APIs + * - Requires `LV_USE_MATRIX = 1` */ +#ifndef LV_USE_VECTOR_GRAPHIC + #ifdef CONFIG_LV_USE_VECTOR_GRAPHIC + #define LV_USE_VECTOR_GRAPHIC CONFIG_LV_USE_VECTOR_GRAPHIC + #else + #define LV_USE_VECTOR_GRAPHIC 0 + #endif +#endif + +/** Enable ThorVG (vector graphics library) from the src/libs folder */ +#ifndef LV_USE_THORVG_INTERNAL + #ifdef CONFIG_LV_USE_THORVG_INTERNAL + #define LV_USE_THORVG_INTERNAL CONFIG_LV_USE_THORVG_INTERNAL + #else + #define LV_USE_THORVG_INTERNAL 0 + #endif +#endif + +/** Enable ThorVG by assuming that its installed and linked to the project */ +#ifndef LV_USE_THORVG_EXTERNAL + #ifdef CONFIG_LV_USE_THORVG_EXTERNAL + #define LV_USE_THORVG_EXTERNAL CONFIG_LV_USE_THORVG_EXTERNAL + #else + #define LV_USE_THORVG_EXTERNAL 0 + #endif +#endif + +/** Use lvgl built-in LZ4 lib */ +#ifndef LV_USE_LZ4_INTERNAL + #ifdef CONFIG_LV_USE_LZ4_INTERNAL + #define LV_USE_LZ4_INTERNAL CONFIG_LV_USE_LZ4_INTERNAL + #else + #define LV_USE_LZ4_INTERNAL 0 + #endif +#endif + +/** Use external LZ4 library */ +#ifndef LV_USE_LZ4_EXTERNAL + #ifdef CONFIG_LV_USE_LZ4_EXTERNAL + #define LV_USE_LZ4_EXTERNAL CONFIG_LV_USE_LZ4_EXTERNAL + #else + #define LV_USE_LZ4_EXTERNAL 0 + #endif +#endif + +/*SVG library*/ +#ifndef LV_USE_SVG + #ifdef CONFIG_LV_USE_SVG + #define LV_USE_SVG CONFIG_LV_USE_SVG + #else + #define LV_USE_SVG 0 + #endif +#endif +#ifndef LV_USE_SVG_ANIMATION + #ifdef CONFIG_LV_USE_SVG_ANIMATION + #define LV_USE_SVG_ANIMATION CONFIG_LV_USE_SVG_ANIMATION + #else + #define LV_USE_SVG_ANIMATION 0 + #endif +#endif +#ifndef LV_USE_SVG_DEBUG + #ifdef CONFIG_LV_USE_SVG_DEBUG + #define LV_USE_SVG_DEBUG CONFIG_LV_USE_SVG_DEBUG + #else + #define LV_USE_SVG_DEBUG 0 + #endif +#endif + +/** FFmpeg library for image decoding and playing videos. + * Supports all major image formats so do not enable other image decoder with it. */ #ifndef LV_USE_FFMPEG #ifdef CONFIG_LV_USE_FFMPEG #define LV_USE_FFMPEG CONFIG_LV_USE_FFMPEG @@ -2188,7 +2933,7 @@ #endif #endif #if LV_USE_FFMPEG - /*Dump input information to stderr*/ + /** Dump input information to stderr */ #ifndef LV_FFMPEG_DUMP_FORMAT #ifdef CONFIG_LV_FFMPEG_DUMP_FORMAT #define LV_FFMPEG_DUMP_FORMAT CONFIG_LV_FFMPEG_DUMP_FORMAT @@ -2198,11 +2943,12 @@ #endif #endif -/*----------- - * Others - *----------*/ +/*================== + * OTHERS + *==================*/ +/* Documentation for several of the below items can be found here: https://docs.lvgl.io/master/others/index.html . */ -/*1: Enable API to take snapshot for object*/ +/** 1: Enable API to take snapshot for object */ #ifndef LV_USE_SNAPSHOT #ifdef CONFIG_LV_USE_SNAPSHOT #define LV_USE_SNAPSHOT CONFIG_LV_USE_SNAPSHOT @@ -2211,7 +2957,278 @@ #endif #endif -/*1: Enable Monkey test*/ +/** 1: Enable system monitor component */ +#ifndef LV_USE_SYSMON + #ifdef CONFIG_LV_USE_SYSMON + #define LV_USE_SYSMON CONFIG_LV_USE_SYSMON + #else + #define LV_USE_SYSMON 0 + #endif +#endif +#if LV_USE_SYSMON + /** Get the idle percentage. E.g. uint32_t my_get_idle(void); */ + #ifndef LV_SYSMON_GET_IDLE + #ifdef CONFIG_LV_SYSMON_GET_IDLE + #define LV_SYSMON_GET_IDLE CONFIG_LV_SYSMON_GET_IDLE + #else + #define LV_SYSMON_GET_IDLE lv_timer_get_idle + #endif + #endif + + /** 1: Show CPU usage and FPS count. + * - Requires `LV_USE_SYSMON = 1` */ + #ifndef LV_USE_PERF_MONITOR + #ifdef CONFIG_LV_USE_PERF_MONITOR + #define LV_USE_PERF_MONITOR CONFIG_LV_USE_PERF_MONITOR + #else + #define LV_USE_PERF_MONITOR 0 + #endif + #endif + #if LV_USE_PERF_MONITOR + #ifndef LV_USE_PERF_MONITOR_POS + #ifdef CONFIG_LV_USE_PERF_MONITOR_POS + #define LV_USE_PERF_MONITOR_POS CONFIG_LV_USE_PERF_MONITOR_POS + #else + #define LV_USE_PERF_MONITOR_POS LV_ALIGN_BOTTOM_RIGHT + #endif + #endif + + /** 0: Displays performance data on the screen; 1: Prints performance data using log. */ + #ifndef LV_USE_PERF_MONITOR_LOG_MODE + #ifdef CONFIG_LV_USE_PERF_MONITOR_LOG_MODE + #define LV_USE_PERF_MONITOR_LOG_MODE CONFIG_LV_USE_PERF_MONITOR_LOG_MODE + #else + #define LV_USE_PERF_MONITOR_LOG_MODE 0 + #endif + #endif + #endif + + /** 1: Show used memory and memory fragmentation. + * - Requires `LV_USE_STDLIB_MALLOC = LV_STDLIB_BUILTIN` + * - Requires `LV_USE_SYSMON = 1`*/ + #ifndef LV_USE_MEM_MONITOR + #ifdef CONFIG_LV_USE_MEM_MONITOR + #define LV_USE_MEM_MONITOR CONFIG_LV_USE_MEM_MONITOR + #else + #define LV_USE_MEM_MONITOR 0 + #endif + #endif + #if LV_USE_MEM_MONITOR + #ifndef LV_USE_MEM_MONITOR_POS + #ifdef CONFIG_LV_USE_MEM_MONITOR_POS + #define LV_USE_MEM_MONITOR_POS CONFIG_LV_USE_MEM_MONITOR_POS + #else + #define LV_USE_MEM_MONITOR_POS LV_ALIGN_BOTTOM_LEFT + #endif + #endif + #endif +#endif /*LV_USE_SYSMON*/ + +/** 1: Enable runtime performance profiler */ +#ifndef LV_USE_PROFILER + #ifdef CONFIG_LV_USE_PROFILER + #define LV_USE_PROFILER CONFIG_LV_USE_PROFILER + #else + #define LV_USE_PROFILER 0 + #endif +#endif +#if LV_USE_PROFILER + /** 1: Enable the built-in profiler */ + #ifndef LV_USE_PROFILER_BUILTIN + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_PROFILER_BUILTIN + #define LV_USE_PROFILER_BUILTIN CONFIG_LV_USE_PROFILER_BUILTIN + #else + #define LV_USE_PROFILER_BUILTIN 0 + #endif + #else + #define LV_USE_PROFILER_BUILTIN 1 + #endif + #endif + #if LV_USE_PROFILER_BUILTIN + /** Default profiler trace buffer size */ + #ifndef LV_PROFILER_BUILTIN_BUF_SIZE + #ifdef CONFIG_LV_PROFILER_BUILTIN_BUF_SIZE + #define LV_PROFILER_BUILTIN_BUF_SIZE CONFIG_LV_PROFILER_BUILTIN_BUF_SIZE + #else + #define LV_PROFILER_BUILTIN_BUF_SIZE (16 * 1024) /**< [bytes] */ + #endif + #endif + #endif + + /** Header to include for profiler */ + #ifndef LV_PROFILER_INCLUDE + #ifdef CONFIG_LV_PROFILER_INCLUDE + #define LV_PROFILER_INCLUDE CONFIG_LV_PROFILER_INCLUDE + #else + #define LV_PROFILER_INCLUDE "lvgl/src/misc/lv_profiler_builtin.h" + #endif + #endif + + /** Profiler start point function */ + #ifndef LV_PROFILER_BEGIN + #ifdef CONFIG_LV_PROFILER_BEGIN + #define LV_PROFILER_BEGIN CONFIG_LV_PROFILER_BEGIN + #else + #define LV_PROFILER_BEGIN LV_PROFILER_BUILTIN_BEGIN + #endif + #endif + + /** Profiler end point function */ + #ifndef LV_PROFILER_END + #ifdef CONFIG_LV_PROFILER_END + #define LV_PROFILER_END CONFIG_LV_PROFILER_END + #else + #define LV_PROFILER_END LV_PROFILER_BUILTIN_END + #endif + #endif + + /** Profiler start point function with custom tag */ + #ifndef LV_PROFILER_BEGIN_TAG + #ifdef CONFIG_LV_PROFILER_BEGIN_TAG + #define LV_PROFILER_BEGIN_TAG CONFIG_LV_PROFILER_BEGIN_TAG + #else + #define LV_PROFILER_BEGIN_TAG LV_PROFILER_BUILTIN_BEGIN_TAG + #endif + #endif + + /** Profiler end point function with custom tag */ + #ifndef LV_PROFILER_END_TAG + #ifdef CONFIG_LV_PROFILER_END_TAG + #define LV_PROFILER_END_TAG CONFIG_LV_PROFILER_END_TAG + #else + #define LV_PROFILER_END_TAG LV_PROFILER_BUILTIN_END_TAG + #endif + #endif + + /*Enable layout profiler*/ + #ifndef LV_PROFILER_LAYOUT + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_PROFILER_LAYOUT + #define LV_PROFILER_LAYOUT CONFIG_LV_PROFILER_LAYOUT + #else + #define LV_PROFILER_LAYOUT 0 + #endif + #else + #define LV_PROFILER_LAYOUT 1 + #endif + #endif + + /*Enable disp refr profiler*/ + #ifndef LV_PROFILER_REFR + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_PROFILER_REFR + #define LV_PROFILER_REFR CONFIG_LV_PROFILER_REFR + #else + #define LV_PROFILER_REFR 0 + #endif + #else + #define LV_PROFILER_REFR 1 + #endif + #endif + + /*Enable draw profiler*/ + #ifndef LV_PROFILER_DRAW + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_PROFILER_DRAW + #define LV_PROFILER_DRAW CONFIG_LV_PROFILER_DRAW + #else + #define LV_PROFILER_DRAW 0 + #endif + #else + #define LV_PROFILER_DRAW 1 + #endif + #endif + + /*Enable indev profiler*/ + #ifndef LV_PROFILER_INDEV + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_PROFILER_INDEV + #define LV_PROFILER_INDEV CONFIG_LV_PROFILER_INDEV + #else + #define LV_PROFILER_INDEV 0 + #endif + #else + #define LV_PROFILER_INDEV 1 + #endif + #endif + + /*Enable decoder profiler*/ + #ifndef LV_PROFILER_DECODER + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_PROFILER_DECODER + #define LV_PROFILER_DECODER CONFIG_LV_PROFILER_DECODER + #else + #define LV_PROFILER_DECODER 0 + #endif + #else + #define LV_PROFILER_DECODER 1 + #endif + #endif + + /*Enable font profiler*/ + #ifndef LV_PROFILER_FONT + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_PROFILER_FONT + #define LV_PROFILER_FONT CONFIG_LV_PROFILER_FONT + #else + #define LV_PROFILER_FONT 0 + #endif + #else + #define LV_PROFILER_FONT 1 + #endif + #endif + + /*Enable fs profiler*/ + #ifndef LV_PROFILER_FS + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_PROFILER_FS + #define LV_PROFILER_FS CONFIG_LV_PROFILER_FS + #else + #define LV_PROFILER_FS 0 + #endif + #else + #define LV_PROFILER_FS 1 + #endif + #endif + + /*Enable style profiler*/ + #ifndef LV_PROFILER_STYLE + #ifdef CONFIG_LV_PROFILER_STYLE + #define LV_PROFILER_STYLE CONFIG_LV_PROFILER_STYLE + #else + #define LV_PROFILER_STYLE 0 + #endif + #endif + + /*Enable timer profiler*/ + #ifndef LV_PROFILER_TIMER + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_PROFILER_TIMER + #define LV_PROFILER_TIMER CONFIG_LV_PROFILER_TIMER + #else + #define LV_PROFILER_TIMER 0 + #endif + #else + #define LV_PROFILER_TIMER 1 + #endif + #endif + + /*Enable cache profiler*/ + #ifndef LV_PROFILER_CACHE + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_PROFILER_CACHE + #define LV_PROFILER_CACHE CONFIG_LV_PROFILER_CACHE + #else + #define LV_PROFILER_CACHE 0 + #endif + #else + #define LV_PROFILER_CACHE 1 + #endif + #endif +#endif + +/** 1: Enable Monkey test */ #ifndef LV_USE_MONKEY #ifdef CONFIG_LV_USE_MONKEY #define LV_USE_MONKEY CONFIG_LV_USE_MONKEY @@ -2220,7 +3237,7 @@ #endif #endif -/*1: Enable grid navigation*/ +/** 1: Enable grid navigation */ #ifndef LV_USE_GRIDNAV #ifdef CONFIG_LV_USE_GRIDNAV #define LV_USE_GRIDNAV CONFIG_LV_USE_GRIDNAV @@ -2229,7 +3246,7 @@ #endif #endif -/*1: Enable lv_obj fragment*/ +/** 1: Enable `lv_obj` fragment logic */ #ifndef LV_USE_FRAGMENT #ifdef CONFIG_LV_USE_FRAGMENT #define LV_USE_FRAGMENT CONFIG_LV_USE_FRAGMENT @@ -2238,7 +3255,7 @@ #endif #endif -/*1: Support using images as font in label or span widgets */ +/** 1: Support using images as font in label or span widgets */ #ifndef LV_USE_IMGFONT #ifdef CONFIG_LV_USE_IMGFONT #define LV_USE_IMGFONT CONFIG_LV_USE_IMGFONT @@ -2247,17 +3264,21 @@ #endif #endif -/*1: Enable a published subscriber based messaging system */ -#ifndef LV_USE_MSG - #ifdef CONFIG_LV_USE_MSG - #define LV_USE_MSG CONFIG_LV_USE_MSG +/** 1: Enable an observer pattern implementation */ +#ifndef LV_USE_OBSERVER + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_OBSERVER + #define LV_USE_OBSERVER CONFIG_LV_USE_OBSERVER + #else + #define LV_USE_OBSERVER 0 + #endif #else - #define LV_USE_MSG 0 + #define LV_USE_OBSERVER 1 #endif #endif -/*1: Enable Pinyin input method*/ -/*Requires: lv_keyboard*/ +/** 1: Enable Pinyin input method + * - Requires: lv_keyboard */ #ifndef LV_USE_IME_PINYIN #ifdef CONFIG_LV_USE_IME_PINYIN #define LV_USE_IME_PINYIN CONFIG_LV_USE_IME_PINYIN @@ -2266,10 +3287,10 @@ #endif #endif #if LV_USE_IME_PINYIN - /*1: Use default thesaurus*/ - /*If you do not use the default thesaurus, be sure to use `lv_ime_pinyin` after setting the thesauruss*/ + /** 1: Use default thesaurus. + * @note If you do not use the default thesaurus, be sure to use `lv_ime_pinyin` after setting the thesaurus. */ #ifndef LV_IME_PINYIN_USE_DEFAULT_DICT - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_IME_PINYIN_USE_DEFAULT_DICT #define LV_IME_PINYIN_USE_DEFAULT_DICT CONFIG_LV_IME_PINYIN_USE_DEFAULT_DICT #else @@ -2279,8 +3300,8 @@ #define LV_IME_PINYIN_USE_DEFAULT_DICT 1 #endif #endif - /*Set the maximum number of candidate panels that can be displayed*/ - /*This needs to be adjusted according to the size of the screen*/ + /** Set maximum number of candidate panels that can be displayed. + * @note This needs to be adjusted according to size of screen. */ #ifndef LV_IME_PINYIN_CAND_TEXT_NUM #ifdef CONFIG_LV_IME_PINYIN_CAND_TEXT_NUM #define LV_IME_PINYIN_CAND_TEXT_NUM CONFIG_LV_IME_PINYIN_CAND_TEXT_NUM @@ -2289,9 +3310,9 @@ #endif #endif - /*Use 9 key input(k9)*/ + /** Use 9-key input (k9). */ #ifndef LV_IME_PINYIN_USE_K9_MODE - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_IME_PINYIN_USE_K9_MODE #define LV_IME_PINYIN_USE_K9_MODE CONFIG_LV_IME_PINYIN_USE_K9_MODE #else @@ -2309,16 +3330,524 @@ #define LV_IME_PINYIN_K9_CAND_TEXT_NUM 3 #endif #endif - #endif // LV_IME_PINYIN_USE_K9_MODE + #endif /*LV_IME_PINYIN_USE_K9_MODE*/ +#endif + +/** 1: Enable file explorer. + * - Requires: lv_table */ +#ifndef LV_USE_FILE_EXPLORER + #ifdef CONFIG_LV_USE_FILE_EXPLORER + #define LV_USE_FILE_EXPLORER CONFIG_LV_USE_FILE_EXPLORER + #else + #define LV_USE_FILE_EXPLORER 0 + #endif +#endif +#if LV_USE_FILE_EXPLORER + /** Maximum length of path */ + #ifndef LV_FILE_EXPLORER_PATH_MAX_LEN + #ifdef CONFIG_LV_FILE_EXPLORER_PATH_MAX_LEN + #define LV_FILE_EXPLORER_PATH_MAX_LEN CONFIG_LV_FILE_EXPLORER_PATH_MAX_LEN + #else + #define LV_FILE_EXPLORER_PATH_MAX_LEN (128) + #endif + #endif + /** Quick access bar, 1:use, 0:do not use. + * - Requires: lv_list */ + #ifndef LV_FILE_EXPLORER_QUICK_ACCESS + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_FILE_EXPLORER_QUICK_ACCESS + #define LV_FILE_EXPLORER_QUICK_ACCESS CONFIG_LV_FILE_EXPLORER_QUICK_ACCESS + #else + #define LV_FILE_EXPLORER_QUICK_ACCESS 0 + #endif + #else + #define LV_FILE_EXPLORER_QUICK_ACCESS 1 + #endif + #endif +#endif + +/** 1: Enable freetype font manager + * - Requires: LV_USE_FREETYPE */ +#ifndef LV_USE_FONT_MANAGER + #ifdef CONFIG_LV_USE_FONT_MANAGER + #define LV_USE_FONT_MANAGER CONFIG_LV_USE_FONT_MANAGER + #else + #define LV_USE_FONT_MANAGER 0 + #endif +#endif +#if LV_USE_FONT_MANAGER + +/*Font manager name max length*/ +#ifndef LV_FONT_MANAGER_NAME_MAX_LEN + #ifdef CONFIG_LV_FONT_MANAGER_NAME_MAX_LEN + #define LV_FONT_MANAGER_NAME_MAX_LEN CONFIG_LV_FONT_MANAGER_NAME_MAX_LEN + #else + #define LV_FONT_MANAGER_NAME_MAX_LEN 32 + #endif +#endif + +#endif + +/*================== + * DEVICES + *==================*/ + +/** Use SDL to open window on PC and handle mouse and keyboard. */ +#ifndef LV_USE_SDL + #ifdef CONFIG_LV_USE_SDL + #define LV_USE_SDL CONFIG_LV_USE_SDL + #else + #define LV_USE_SDL 0 + #endif +#endif +#if LV_USE_SDL + #ifndef LV_SDL_INCLUDE_PATH + #ifdef CONFIG_LV_SDL_INCLUDE_PATH + #define LV_SDL_INCLUDE_PATH CONFIG_LV_SDL_INCLUDE_PATH + #else + #define LV_SDL_INCLUDE_PATH + #endif + #endif + #ifndef LV_SDL_RENDER_MODE + #ifdef CONFIG_LV_SDL_RENDER_MODE + #define LV_SDL_RENDER_MODE CONFIG_LV_SDL_RENDER_MODE + #else + #define LV_SDL_RENDER_MODE LV_DISPLAY_RENDER_MODE_DIRECT /**< LV_DISPLAY_RENDER_MODE_DIRECT is recommended for best performance */ + #endif + #endif + #ifndef LV_SDL_BUF_COUNT + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_SDL_BUF_COUNT + #define LV_SDL_BUF_COUNT CONFIG_LV_SDL_BUF_COUNT + #else + #define LV_SDL_BUF_COUNT 0 + #endif + #else + #define LV_SDL_BUF_COUNT 1 /**< 1 or 2 */ + #endif + #endif + #ifndef LV_SDL_ACCELERATED + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_SDL_ACCELERATED + #define LV_SDL_ACCELERATED CONFIG_LV_SDL_ACCELERATED + #else + #define LV_SDL_ACCELERATED 0 + #endif + #else + #define LV_SDL_ACCELERATED 1 /**< 1: Use hardware acceleration*/ + #endif + #endif + #ifndef LV_SDL_FULLSCREEN + #ifdef CONFIG_LV_SDL_FULLSCREEN + #define LV_SDL_FULLSCREEN CONFIG_LV_SDL_FULLSCREEN + #else + #define LV_SDL_FULLSCREEN 0 /**< 1: Make the window full screen by default */ + #endif + #endif + #ifndef LV_SDL_DIRECT_EXIT + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_SDL_DIRECT_EXIT + #define LV_SDL_DIRECT_EXIT CONFIG_LV_SDL_DIRECT_EXIT + #else + #define LV_SDL_DIRECT_EXIT 0 + #endif + #else + #define LV_SDL_DIRECT_EXIT 1 /**< 1: Exit the application when all SDL windows are closed */ + #endif + #endif + #ifndef LV_SDL_MOUSEWHEEL_MODE + #ifdef CONFIG_LV_SDL_MOUSEWHEEL_MODE + #define LV_SDL_MOUSEWHEEL_MODE CONFIG_LV_SDL_MOUSEWHEEL_MODE + #else + #define LV_SDL_MOUSEWHEEL_MODE LV_SDL_MOUSEWHEEL_MODE_ENCODER /*LV_SDL_MOUSEWHEEL_MODE_ENCODER/CROWN*/ + #endif + #endif +#endif + +/** Use X11 to open window on Linux desktop and handle mouse and keyboard */ +#ifndef LV_USE_X11 + #ifdef CONFIG_LV_USE_X11 + #define LV_USE_X11 CONFIG_LV_USE_X11 + #else + #define LV_USE_X11 0 + #endif +#endif +#if LV_USE_X11 + #ifndef LV_X11_DIRECT_EXIT + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_X11_DIRECT_EXIT + #define LV_X11_DIRECT_EXIT CONFIG_LV_X11_DIRECT_EXIT + #else + #define LV_X11_DIRECT_EXIT 0 + #endif + #else + #define LV_X11_DIRECT_EXIT 1 /**< Exit application when all X11 windows have been closed */ + #endif + #endif + #ifndef LV_X11_DOUBLE_BUFFER + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_X11_DOUBLE_BUFFER + #define LV_X11_DOUBLE_BUFFER CONFIG_LV_X11_DOUBLE_BUFFER + #else + #define LV_X11_DOUBLE_BUFFER 0 + #endif + #else + #define LV_X11_DOUBLE_BUFFER 1 /**< Use double buffers for rendering */ + #endif + #endif + /* Select only 1 of the following render modes (LV_X11_RENDER_MODE_PARTIAL preferred!). */ + #ifndef LV_X11_RENDER_MODE_PARTIAL + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_X11_RENDER_MODE_PARTIAL + #define LV_X11_RENDER_MODE_PARTIAL CONFIG_LV_X11_RENDER_MODE_PARTIAL + #else + #define LV_X11_RENDER_MODE_PARTIAL 0 + #endif + #else + #define LV_X11_RENDER_MODE_PARTIAL 1 /**< Partial render mode (preferred) */ + #endif + #endif + #ifndef LV_X11_RENDER_MODE_DIRECT + #ifdef CONFIG_LV_X11_RENDER_MODE_DIRECT + #define LV_X11_RENDER_MODE_DIRECT CONFIG_LV_X11_RENDER_MODE_DIRECT + #else + #define LV_X11_RENDER_MODE_DIRECT 0 /**< Direct render mode */ + #endif + #endif + #ifndef LV_X11_RENDER_MODE_FULL + #ifdef CONFIG_LV_X11_RENDER_MODE_FULL + #define LV_X11_RENDER_MODE_FULL CONFIG_LV_X11_RENDER_MODE_FULL + #else + #define LV_X11_RENDER_MODE_FULL 0 /**< Full render mode */ + #endif + #endif +#endif + +/** Use Wayland to open a window and handle input on Linux or BSD desktops */ +#ifndef LV_USE_WAYLAND + #ifdef CONFIG_LV_USE_WAYLAND + #define LV_USE_WAYLAND CONFIG_LV_USE_WAYLAND + #else + #define LV_USE_WAYLAND 0 + #endif +#endif +#if LV_USE_WAYLAND + #ifndef LV_WAYLAND_WINDOW_DECORATIONS + #ifdef CONFIG_LV_WAYLAND_WINDOW_DECORATIONS + #define LV_WAYLAND_WINDOW_DECORATIONS CONFIG_LV_WAYLAND_WINDOW_DECORATIONS + #else + #define LV_WAYLAND_WINDOW_DECORATIONS 0 /**< Draw client side window decorations only necessary on Mutter/GNOME */ + #endif + #endif + #ifndef LV_WAYLAND_WL_SHELL + #ifdef CONFIG_LV_WAYLAND_WL_SHELL + #define LV_WAYLAND_WL_SHELL CONFIG_LV_WAYLAND_WL_SHELL + #else + #define LV_WAYLAND_WL_SHELL 0 /**< Use the legacy wl_shell protocol instead of the default XDG shell */ + #endif + #endif +#endif + +/** Driver for /dev/fb */ +#ifndef LV_USE_LINUX_FBDEV + #ifdef CONFIG_LV_USE_LINUX_FBDEV + #define LV_USE_LINUX_FBDEV CONFIG_LV_USE_LINUX_FBDEV + #else + #define LV_USE_LINUX_FBDEV 0 + #endif +#endif +#if LV_USE_LINUX_FBDEV + #ifndef LV_LINUX_FBDEV_BSD + #ifdef CONFIG_LV_LINUX_FBDEV_BSD + #define LV_LINUX_FBDEV_BSD CONFIG_LV_LINUX_FBDEV_BSD + #else + #define LV_LINUX_FBDEV_BSD 0 + #endif + #endif + #ifndef LV_LINUX_FBDEV_RENDER_MODE + #ifdef CONFIG_LV_LINUX_FBDEV_RENDER_MODE + #define LV_LINUX_FBDEV_RENDER_MODE CONFIG_LV_LINUX_FBDEV_RENDER_MODE + #else + #define LV_LINUX_FBDEV_RENDER_MODE LV_DISPLAY_RENDER_MODE_PARTIAL + #endif + #endif + #ifndef LV_LINUX_FBDEV_BUFFER_COUNT + #ifdef CONFIG_LV_LINUX_FBDEV_BUFFER_COUNT + #define LV_LINUX_FBDEV_BUFFER_COUNT CONFIG_LV_LINUX_FBDEV_BUFFER_COUNT + #else + #define LV_LINUX_FBDEV_BUFFER_COUNT 0 + #endif + #endif + #ifndef LV_LINUX_FBDEV_BUFFER_SIZE + #ifdef CONFIG_LV_LINUX_FBDEV_BUFFER_SIZE + #define LV_LINUX_FBDEV_BUFFER_SIZE CONFIG_LV_LINUX_FBDEV_BUFFER_SIZE + #else + #define LV_LINUX_FBDEV_BUFFER_SIZE 60 + #endif + #endif +#endif + +/** Use Nuttx to open window and handle touchscreen */ +#ifndef LV_USE_NUTTX + #ifdef CONFIG_LV_USE_NUTTX + #define LV_USE_NUTTX CONFIG_LV_USE_NUTTX + #else + #define LV_USE_NUTTX 0 + #endif +#endif + +#if LV_USE_NUTTX + #ifndef LV_USE_NUTTX_INDEPENDENT_IMAGE_HEAP + #ifdef CONFIG_LV_USE_NUTTX_INDEPENDENT_IMAGE_HEAP + #define LV_USE_NUTTX_INDEPENDENT_IMAGE_HEAP CONFIG_LV_USE_NUTTX_INDEPENDENT_IMAGE_HEAP + #else + #define LV_USE_NUTTX_INDEPENDENT_IMAGE_HEAP 0 + #endif + #endif + + #ifndef LV_USE_NUTTX_LIBUV + #ifdef CONFIG_LV_USE_NUTTX_LIBUV + #define LV_USE_NUTTX_LIBUV CONFIG_LV_USE_NUTTX_LIBUV + #else + #define LV_USE_NUTTX_LIBUV 0 + #endif + #endif + + /** Use Nuttx custom init API to open window and handle touchscreen */ + #ifndef LV_USE_NUTTX_CUSTOM_INIT + #ifdef CONFIG_LV_USE_NUTTX_CUSTOM_INIT + #define LV_USE_NUTTX_CUSTOM_INIT CONFIG_LV_USE_NUTTX_CUSTOM_INIT + #else + #define LV_USE_NUTTX_CUSTOM_INIT 0 + #endif + #endif + + /** Driver for /dev/lcd */ + #ifndef LV_USE_NUTTX_LCD + #ifdef CONFIG_LV_USE_NUTTX_LCD + #define LV_USE_NUTTX_LCD CONFIG_LV_USE_NUTTX_LCD + #else + #define LV_USE_NUTTX_LCD 0 + #endif + #endif + #if LV_USE_NUTTX_LCD + #ifndef LV_NUTTX_LCD_BUFFER_COUNT + #ifdef CONFIG_LV_NUTTX_LCD_BUFFER_COUNT + #define LV_NUTTX_LCD_BUFFER_COUNT CONFIG_LV_NUTTX_LCD_BUFFER_COUNT + #else + #define LV_NUTTX_LCD_BUFFER_COUNT 0 + #endif + #endif + #ifndef LV_NUTTX_LCD_BUFFER_SIZE + #ifdef CONFIG_LV_NUTTX_LCD_BUFFER_SIZE + #define LV_NUTTX_LCD_BUFFER_SIZE CONFIG_LV_NUTTX_LCD_BUFFER_SIZE + #else + #define LV_NUTTX_LCD_BUFFER_SIZE 60 + #endif + #endif + #endif + + /** Driver for /dev/input */ + #ifndef LV_USE_NUTTX_TOUCHSCREEN + #ifdef CONFIG_LV_USE_NUTTX_TOUCHSCREEN + #define LV_USE_NUTTX_TOUCHSCREEN CONFIG_LV_USE_NUTTX_TOUCHSCREEN + #else + #define LV_USE_NUTTX_TOUCHSCREEN 0 + #endif + #endif +#endif + +/** Driver for /dev/dri/card */ +#ifndef LV_USE_LINUX_DRM + #ifdef CONFIG_LV_USE_LINUX_DRM + #define LV_USE_LINUX_DRM CONFIG_LV_USE_LINUX_DRM + #else + #define LV_USE_LINUX_DRM 0 + #endif +#endif + +/** Interface for TFT_eSPI */ +#ifndef LV_USE_TFT_ESPI + #ifdef CONFIG_LV_USE_TFT_ESPI + #define LV_USE_TFT_ESPI CONFIG_LV_USE_TFT_ESPI + #else + #define LV_USE_TFT_ESPI 0 + #endif +#endif + +/** Driver for evdev input devices */ +#ifndef LV_USE_EVDEV + #ifdef CONFIG_LV_USE_EVDEV + #define LV_USE_EVDEV CONFIG_LV_USE_EVDEV + #else + #define LV_USE_EVDEV 0 + #endif +#endif + +/** Driver for libinput input devices */ +#ifndef LV_USE_LIBINPUT + #ifdef CONFIG_LV_USE_LIBINPUT + #define LV_USE_LIBINPUT CONFIG_LV_USE_LIBINPUT + #else + #define LV_USE_LIBINPUT 0 + #endif +#endif + +#if LV_USE_LIBINPUT + #ifndef LV_LIBINPUT_BSD + #ifdef CONFIG_LV_LIBINPUT_BSD + #define LV_LIBINPUT_BSD CONFIG_LV_LIBINPUT_BSD + #else + #define LV_LIBINPUT_BSD 0 + #endif + #endif + + /** Full keyboard support */ + #ifndef LV_LIBINPUT_XKB + #ifdef CONFIG_LV_LIBINPUT_XKB + #define LV_LIBINPUT_XKB CONFIG_LV_LIBINPUT_XKB + #else + #define LV_LIBINPUT_XKB 0 + #endif + #endif + #if LV_LIBINPUT_XKB + /** "setxkbmap -query" can help find the right values for your keyboard */ + #ifndef LV_LIBINPUT_XKB_KEY_MAP + #ifdef CONFIG_LV_LIBINPUT_XKB_KEY_MAP + #define LV_LIBINPUT_XKB_KEY_MAP CONFIG_LV_LIBINPUT_XKB_KEY_MAP + #else + #define LV_LIBINPUT_XKB_KEY_MAP { .rules = NULL, .model = "pc101", .layout = "us", .variant = NULL, .options = NULL } + #endif + #endif + #endif +#endif + +/* Drivers for LCD devices connected via SPI/parallel port */ +#ifndef LV_USE_ST7735 + #ifdef CONFIG_LV_USE_ST7735 + #define LV_USE_ST7735 CONFIG_LV_USE_ST7735 + #else + #define LV_USE_ST7735 0 + #endif +#endif +#ifndef LV_USE_ST7789 + #ifdef CONFIG_LV_USE_ST7789 + #define LV_USE_ST7789 CONFIG_LV_USE_ST7789 + #else + #define LV_USE_ST7789 0 + #endif +#endif +#ifndef LV_USE_ST7796 + #ifdef CONFIG_LV_USE_ST7796 + #define LV_USE_ST7796 CONFIG_LV_USE_ST7796 + #else + #define LV_USE_ST7796 0 + #endif +#endif +#ifndef LV_USE_ILI9341 + #ifdef CONFIG_LV_USE_ILI9341 + #define LV_USE_ILI9341 CONFIG_LV_USE_ILI9341 + #else + #define LV_USE_ILI9341 0 + #endif +#endif + +#ifndef LV_USE_GENERIC_MIPI + #ifdef CONFIG_LV_USE_GENERIC_MIPI + #define LV_USE_GENERIC_MIPI CONFIG_LV_USE_GENERIC_MIPI + #else + #define LV_USE_GENERIC_MIPI (LV_USE_ST7735 | LV_USE_ST7789 | LV_USE_ST7796 | LV_USE_ILI9341) + #endif +#endif + +/** Driver for Renesas GLCD */ +#ifndef LV_USE_RENESAS_GLCDC + #ifdef CONFIG_LV_USE_RENESAS_GLCDC + #define LV_USE_RENESAS_GLCDC CONFIG_LV_USE_RENESAS_GLCDC + #else + #define LV_USE_RENESAS_GLCDC 0 + #endif +#endif + +/** Driver for ST LTDC */ +#ifndef LV_USE_ST_LTDC + #ifdef CONFIG_LV_USE_ST_LTDC + #define LV_USE_ST_LTDC CONFIG_LV_USE_ST_LTDC + #else + #define LV_USE_ST_LTDC 0 + #endif +#endif +#if LV_USE_ST_LTDC + /* Only used for partial. */ + #ifndef LV_ST_LTDC_USE_DMA2D_FLUSH + #ifdef CONFIG_LV_ST_LTDC_USE_DMA2D_FLUSH + #define LV_ST_LTDC_USE_DMA2D_FLUSH CONFIG_LV_ST_LTDC_USE_DMA2D_FLUSH + #else + #define LV_ST_LTDC_USE_DMA2D_FLUSH 0 + #endif + #endif +#endif + +/** LVGL Windows backend */ +#ifndef LV_USE_WINDOWS + #ifdef CONFIG_LV_USE_WINDOWS + #define LV_USE_WINDOWS CONFIG_LV_USE_WINDOWS + #else + #define LV_USE_WINDOWS 0 + #endif +#endif + +/** Use OpenGL to open window on PC and handle mouse and keyboard */ +#ifndef LV_USE_OPENGLES + #ifdef CONFIG_LV_USE_OPENGLES + #define LV_USE_OPENGLES CONFIG_LV_USE_OPENGLES + #else + #define LV_USE_OPENGLES 0 + #endif +#endif +#if LV_USE_OPENGLES + #ifndef LV_USE_OPENGLES_DEBUG + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_USE_OPENGLES_DEBUG + #define LV_USE_OPENGLES_DEBUG CONFIG_LV_USE_OPENGLES_DEBUG + #else + #define LV_USE_OPENGLES_DEBUG 0 + #endif + #else + #define LV_USE_OPENGLES_DEBUG 1 /**< Enable or disable debug for opengles */ + #endif + #endif +#endif + +/** QNX Screen display and input drivers */ +#ifndef LV_USE_QNX + #ifdef CONFIG_LV_USE_QNX + #define LV_USE_QNX CONFIG_LV_USE_QNX + #else + #define LV_USE_QNX 0 + #endif +#endif +#if LV_USE_QNX + #ifndef LV_QNX_BUF_COUNT + #ifdef LV_KCONFIG_PRESENT + #ifdef CONFIG_LV_QNX_BUF_COUNT + #define LV_QNX_BUF_COUNT CONFIG_LV_QNX_BUF_COUNT + #else + #define LV_QNX_BUF_COUNT 0 + #endif + #else + #define LV_QNX_BUF_COUNT 1 /**< 1 or 2 */ + #endif + #endif #endif /*================== * EXAMPLES *==================*/ -/*Enable the examples to be built with the library*/ +/** Enable examples to be built with the library. */ #ifndef LV_BUILD_EXAMPLES - #ifdef _LV_KCONFIG_PRESENT + #ifdef LV_KCONFIG_PRESENT #ifdef CONFIG_LV_BUILD_EXAMPLES #define LV_BUILD_EXAMPLES CONFIG_LV_BUILD_EXAMPLES #else @@ -2333,7 +3862,7 @@ * DEMO USAGE ====================*/ -/*Show some widget. It might be required to increase `LV_MEM_SIZE` */ +/** Show some widgets. This might be required to increase `LV_MEM_SIZE`. */ #ifndef LV_USE_DEMO_WIDGETS #ifdef CONFIG_LV_USE_DEMO_WIDGETS #define LV_USE_DEMO_WIDGETS CONFIG_LV_USE_DEMO_WIDGETS @@ -2341,17 +3870,8 @@ #define LV_USE_DEMO_WIDGETS 0 #endif #endif -#if LV_USE_DEMO_WIDGETS -#ifndef LV_DEMO_WIDGETS_SLIDESHOW - #ifdef CONFIG_LV_DEMO_WIDGETS_SLIDESHOW - #define LV_DEMO_WIDGETS_SLIDESHOW CONFIG_LV_DEMO_WIDGETS_SLIDESHOW - #else - #define LV_DEMO_WIDGETS_SLIDESHOW 0 - #endif -#endif -#endif -/*Demonstrate the usage of encoder and keyboard*/ +/** Demonstrate usage of encoder and keyboard. */ #ifndef LV_USE_DEMO_KEYPAD_AND_ENCODER #ifdef CONFIG_LV_USE_DEMO_KEYPAD_AND_ENCODER #define LV_USE_DEMO_KEYPAD_AND_ENCODER CONFIG_LV_USE_DEMO_KEYPAD_AND_ENCODER @@ -2360,7 +3880,7 @@ #endif #endif -/*Benchmark your system*/ +/** Benchmark your system */ #ifndef LV_USE_DEMO_BENCHMARK #ifdef CONFIG_LV_USE_DEMO_BENCHMARK #define LV_USE_DEMO_BENCHMARK CONFIG_LV_USE_DEMO_BENCHMARK @@ -2368,18 +3888,18 @@ #define LV_USE_DEMO_BENCHMARK 0 #endif #endif -#if LV_USE_DEMO_BENCHMARK -/*Use RGB565A8 images with 16 bit color depth instead of ARGB8565*/ -#ifndef LV_DEMO_BENCHMARK_RGB565A8 - #ifdef CONFIG_LV_DEMO_BENCHMARK_RGB565A8 - #define LV_DEMO_BENCHMARK_RGB565A8 CONFIG_LV_DEMO_BENCHMARK_RGB565A8 + +/** Render test for each primitive. + * - Requires at least 480x272 display. */ +#ifndef LV_USE_DEMO_RENDER + #ifdef CONFIG_LV_USE_DEMO_RENDER + #define LV_USE_DEMO_RENDER CONFIG_LV_USE_DEMO_RENDER #else - #define LV_DEMO_BENCHMARK_RGB565A8 0 + #define LV_USE_DEMO_RENDER 0 #endif #endif -#endif -/*Stress test for LVGL*/ +/** Stress test for LVGL */ #ifndef LV_USE_DEMO_STRESS #ifdef CONFIG_LV_USE_DEMO_STRESS #define LV_USE_DEMO_STRESS CONFIG_LV_USE_DEMO_STRESS @@ -2388,7 +3908,7 @@ #endif #endif -/*Music player demo*/ +/** Music player demo */ #ifndef LV_USE_DEMO_MUSIC #ifdef CONFIG_LV_USE_DEMO_MUSIC #define LV_USE_DEMO_MUSIC CONFIG_LV_USE_DEMO_MUSIC @@ -2434,18 +3954,84 @@ #endif #endif +/** Flex layout demo */ +#ifndef LV_USE_DEMO_FLEX_LAYOUT + #ifdef CONFIG_LV_USE_DEMO_FLEX_LAYOUT + #define LV_USE_DEMO_FLEX_LAYOUT CONFIG_LV_USE_DEMO_FLEX_LAYOUT + #else + #define LV_USE_DEMO_FLEX_LAYOUT 0 + #endif +#endif + +/** Smart-phone like multi-language demo */ +#ifndef LV_USE_DEMO_MULTILANG + #ifdef CONFIG_LV_USE_DEMO_MULTILANG + #define LV_USE_DEMO_MULTILANG CONFIG_LV_USE_DEMO_MULTILANG + #else + #define LV_USE_DEMO_MULTILANG 0 + #endif +#endif + +/** Widget transformation demo */ +#ifndef LV_USE_DEMO_TRANSFORM + #ifdef CONFIG_LV_USE_DEMO_TRANSFORM + #define LV_USE_DEMO_TRANSFORM CONFIG_LV_USE_DEMO_TRANSFORM + #else + #define LV_USE_DEMO_TRANSFORM 0 + #endif +#endif + +/** Demonstrate scroll settings */ +#ifndef LV_USE_DEMO_SCROLL + #ifdef CONFIG_LV_USE_DEMO_SCROLL + #define LV_USE_DEMO_SCROLL CONFIG_LV_USE_DEMO_SCROLL + #else + #define LV_USE_DEMO_SCROLL 0 + #endif +#endif + +/** Vector graphic demo */ +#ifndef LV_USE_DEMO_VECTOR_GRAPHIC + #ifdef CONFIG_LV_USE_DEMO_VECTOR_GRAPHIC + #define LV_USE_DEMO_VECTOR_GRAPHIC CONFIG_LV_USE_DEMO_VECTOR_GRAPHIC + #else + #define LV_USE_DEMO_VECTOR_GRAPHIC 0 + #endif +#endif + +/*E-bike demo with Lottie animations (if LV_USE_LOTTIE is enabled)*/ +#ifndef LV_USE_DEMO_EBIKE + #ifdef CONFIG_LV_USE_DEMO_EBIKE + #define LV_USE_DEMO_EBIKE CONFIG_LV_USE_DEMO_EBIKE + #else + #define LV_USE_DEMO_EBIKE 0 + #endif +#endif +#if LV_USE_DEMO_EBIKE + #ifndef LV_DEMO_EBIKE_PORTRAIT + #ifdef CONFIG_LV_DEMO_EBIKE_PORTRAIT + #define LV_DEMO_EBIKE_PORTRAIT CONFIG_LV_DEMO_EBIKE_PORTRAIT + #else + #define LV_DEMO_EBIKE_PORTRAIT 0 /*0: for 480x270..480x320, 1: for 480x800..720x1280*/ + #endif + #endif +#endif + /*---------------------------------- * End of parsing lv_conf_template.h -----------------------------------*/ +#ifndef __ASSEMBLY__ LV_EXPORT_CONST_INT(LV_DPI_DEF); +LV_EXPORT_CONST_INT(LV_DRAW_BUF_STRIDE_ALIGN); +LV_EXPORT_CONST_INT(LV_DRAW_BUF_ALIGN); +#endif -#undef _LV_KCONFIG_PRESENT - +#undef LV_KCONFIG_PRESENT -/*Set some defines if a dependency is disabled*/ +/* Set some defines if a dependency is disabled. */ #if LV_USE_LOG == 0 #define LV_LOG_LEVEL LV_LOG_LEVEL_NONE #define LV_LOG_TRACE_MEM 0 @@ -2458,8 +4044,31 @@ LV_EXPORT_CONST_INT(LV_DPI_DEF); #define LV_LOG_TRACE_ANIM 0 #endif /*LV_USE_LOG*/ +#if LV_USE_SYSMON == 0 + #define LV_USE_PERF_MONITOR 0 + #define LV_USE_MEM_MONITOR 0 +#endif /*LV_USE_SYSMON*/ + +#ifndef LV_USE_LZ4 + #define LV_USE_LZ4 (LV_USE_LZ4_INTERNAL || LV_USE_LZ4_EXTERNAL) +#endif + +#ifndef LV_USE_THORVG + #define LV_USE_THORVG (LV_USE_THORVG_INTERNAL || LV_USE_THORVG_EXTERNAL) +#endif + +#if LV_USE_OS + #if (LV_USE_FREETYPE || LV_USE_THORVG) && LV_DRAW_THREAD_STACK_SIZE < (32 * 1024) + #warning "Increase LV_DRAW_THREAD_STACK_SIZE to at least 32KB for FreeType or ThorVG." + #endif + + #if defined(LV_DRAW_THREAD_STACKSIZE) && !defined(LV_DRAW_THREAD_STACK_SIZE) + #warning "LV_DRAW_THREAD_STACKSIZE was renamed to LV_DRAW_THREAD_STACK_SIZE. Please update lv_conf.h or run menuconfig again." + #define LV_DRAW_THREAD_STACK_SIZE LV_DRAW_THREAD_STACKSIZE + #endif +#endif -/*If running without lv_conf.h add typedefs with default value*/ +/* If running without lv_conf.h, add typedefs with default value. */ #ifdef LV_CONF_SKIP #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) /*Disable warnings for Visual Studio*/ #define _CRT_SECURE_NO_WARNINGS diff --git a/include/liblvgl/lv_conf_kconfig.h b/include/liblvgl/lv_conf_kconfig.h index 7742fe77..44e76959 100644 --- a/include/liblvgl/lv_conf_kconfig.h +++ b/include/liblvgl/lv_conf_kconfig.h @@ -18,6 +18,16 @@ extern "C" { # ifdef __NuttX__ # include +/* + * Make sure version number in Kconfig file is correctly set. + * Mismatch can happen when user manually copy lvgl/Kconfig file to their project, like what NuttX does. + */ +# include "../lv_version.h" + +# if CONFIG_LVGL_VERSION_MAJOR != LVGL_VERSION_MAJOR || CONFIG_LVGL_VERSION_MINOR != LVGL_VERSION_MINOR \ + || CONFIG_LVGL_VERSION_PATCH != LVGL_VERSION_PATCH +# warning "Version mismatch between Kconfig and lvgl/lv_version.h" +# endif # elif defined(__RTTHREAD__) # define LV_CONF_INCLUDE_SIMPLE # include @@ -26,11 +36,51 @@ extern "C" { #endif /*LV_CONF_KCONFIG_EXTERNAL_INCLUDE*/ /******************* - * LV COLOR CHROMA KEY + * LV_USE_STDLIB_MALLOC + *******************/ + +#ifdef CONFIG_LV_USE_BUILTIN_MALLOC +# define CONFIG_LV_USE_STDLIB_MALLOC LV_STDLIB_BUILTIN +#elif defined(CONFIG_LV_USE_CLIB_MALLOC) +# define CONFIG_LV_USE_STDLIB_MALLOC LV_STDLIB_CLIB +#elif defined(CONFIG_LV_USE_MICROPYTHON_MALLOC) +# define CONFIG_LV_USE_STDLIB_MALLOC LV_STDLIB_MICROPYTHON +#elif defined(CONFIG_LV_USE_RTTHREAD_MALLOC) +# define CONFIG_LV_USE_STDLIB_MALLOC LV_STDLIB_RTTHREAD +#elif defined (CONFIG_LV_USE_CUSTOM_MALLOC) +# define CONFIG_LV_USE_STDLIB_MALLOC LV_STDLIB_CUSTOM +#endif + +/******************* + * LV_USE_STDLIB_STRING *******************/ -#ifdef CONFIG_LV_COLOR_CHROMA_KEY_HEX -# define CONFIG_LV_COLOR_CHROMA_KEY lv_color_hex(CONFIG_LV_COLOR_CHROMA_KEY_HEX) +#ifdef CONFIG_LV_USE_BUILTIN_STRING +# define CONFIG_LV_USE_STDLIB_STRING LV_STDLIB_BUILTIN +#elif defined(CONFIG_LV_USE_CLIB_STRING) +# define CONFIG_LV_USE_STDLIB_STRING LV_STDLIB_CLIB +#elif defined(CONFIG_LV_USE_MICROPYTHON_STRING) +# define CONFIG_LV_USE_STDLIB_STRING LV_STDLIB_MICROPYTHON +#elif defined(CONFIG_LV_USE_RTTHREAD_STRING) +# define CONFIG_LV_USE_STDLIB_STRING LV_STDLIB_RTTHREAD +#elif defined (CONFIG_LV_USE_CUSTOM_STRING) +# define CONFIG_LV_USE_STDLIB_STRING LV_STDLIB_CUSTOM +#endif + +/******************* + * LV_USE_STDLIB_SPRINTF + *******************/ + +#ifdef CONFIG_LV_USE_BUILTIN_SPRINTF +# define CONFIG_LV_USE_STDLIB_SPRINTF LV_STDLIB_BUILTIN +#elif defined(CONFIG_LV_USE_CLIB_SPRINTF) +# define CONFIG_LV_USE_STDLIB_SPRINTF LV_STDLIB_CLIB +#elif defined(CONFIG_LV_USE_MICROPYTHON_SPRINTF) +# define CONFIG_LV_USE_STDLIB_SPRINTF LV_STDLIB_MICROPYTHON +#elif defined(CONFIG_LV_USE_RTTHREAD_SPRINTF) +# define CONFIG_LV_USE_STDLIB_SPRINTF LV_STDLIB_RTTHREAD +#elif defined (CONFIG_LV_USE_CUSTOM_SPRINTF) +# define CONFIG_LV_USE_STDLIB_SPRINTF LV_STDLIB_CUSTOM #endif /******************* @@ -38,9 +88,17 @@ extern "C" { *******************/ #ifdef CONFIG_LV_MEM_SIZE_KILOBYTES +# if(CONFIG_LV_MEM_SIZE_KILOBYTES < 2) +# error "LV_MEM_SIZE >= 2kB is required" +# endif + # define CONFIG_LV_MEM_SIZE (CONFIG_LV_MEM_SIZE_KILOBYTES * 1024U) #endif +#ifdef CONFIG_LV_MEM_POOL_EXPAND_SIZE_KILOBYTES +# define CONFIG_LV_MEM_POOL_EXPAND_SIZE (CONFIG_LV_MEM_POOL_EXPAND_SIZE_KILOBYTES * 1024U) +#endif + /*------------------ * MONITOR POSITION *-----------------*/ @@ -146,6 +204,8 @@ extern "C" { # define CONFIG_LV_FONT_DEFAULT &lv_font_montserrat_28_compressed #elif defined(CONFIG_LV_FONT_DEFAULT_DEJAVU_16_PERSIAN_HEBREW) # define CONFIG_LV_FONT_DEFAULT &lv_font_dejavu_16_persian_hebrew +#elif defined(CONFIG_LV_FONT_DEFAULT_SIMSUN_14_CJK) +# define CONFIG_LV_FONT_DEFAULT &lv_font_simsun_14_cjk #elif defined(CONFIG_LV_FONT_DEFAULT_SIMSUN_16_CJK) # define CONFIG_LV_FONT_DEFAULT &lv_font_simsun_16_cjk #elif defined(CONFIG_LV_FONT_DEFAULT_UNSCII_8) @@ -175,8 +235,32 @@ extern "C" { # define CONFIG_LV_BIDI_BASE_DIR_DEF LV_BASE_DIR_AUTO #endif +/*------------------ + * SDL + *-----------------*/ + +#ifdef CONFIG_LV_SDL_RENDER_MODE_PARTIAL +# define CONFIG_LV_SDL_RENDER_MODE LV_DISPLAY_RENDER_MODE_PARTIAL +#elif defined(CONFIG_LV_SDL_RENDER_MODE_DIRECT) +# define CONFIG_LV_SDL_RENDER_MODE LV_DISPLAY_RENDER_MODE_DIRECT +#elif defined(CONFIG_LV_SDL_RENDER_MODE_FULL) +# define CONFIG_LV_SDL_RENDER_MODE LV_DISPLAY_RENDER_MODE_FULL +#endif + +/*------------------ + * LINUX FBDEV + *-----------------*/ + +#ifdef CONFIG_LV_LINUX_FBDEV_RENDER_MODE_PARTIAL +# define CONFIG_LV_LINUX_FBDEV_RENDER_MODE LV_DISPLAY_RENDER_MODE_PARTIAL +#elif defined(CONFIG_LV_LINUX_FBDEV_RENDER_MODE_DIRECT) +# define CONFIG_LV_LINUX_FBDEV_RENDER_MODE LV_DISPLAY_RENDER_MODE_DIRECT +#elif defined(CONFIG_LV_LINUX_FBDEV_RENDER_MODE_FULL) +# define CONFIG_LV_LINUX_FBDEV_RENDER_MODE LV_DISPLAY_RENDER_MODE_FULL +#endif + #ifdef __cplusplus } /*extern "C"*/ #endif -#endif /*LV_CONF_KCONFIG_H*/ +#endif /*LV_CONF_KCONFIG_H*/ \ No newline at end of file diff --git a/include/liblvgl/lv_init.h b/include/liblvgl/lv_init.h new file mode 100644 index 00000000..0815142c --- /dev/null +++ b/include/liblvgl/lv_init.h @@ -0,0 +1,55 @@ +/** + * @file lv_init.h + * + */ + +#ifndef LV_INIT_H +#define LV_INIT_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "lv_conf_internal.h" +#include "misc/lv_types.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Initialize LVGL library. + * Should be called before any other LVGL related function. + */ +void lv_init(void); + +/** + * Deinit the 'lv' library + */ +void lv_deinit(void); + +/** + * Returns whether the 'lv' library is currently initialized + */ +bool lv_is_initialized(void); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_INIT_H*/ diff --git a/include/liblvgl/lv_version.h b/include/liblvgl/lv_version.h new file mode 100644 index 00000000..b3a4ac7d --- /dev/null +++ b/include/liblvgl/lv_version.h @@ -0,0 +1,66 @@ +/** + * @file lv_version.h + * + */ + +#ifndef LV_VERSION_H +#define LV_VERSION_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +/*Current version of LittlevGL*/ +#define LVGL_VERSION_MAJOR 9 +#define LVGL_VERSION_MINOR 2 +#define LVGL_VERSION_PATCH 0 +#define LVGL_VERSION_INFO "" + + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ +/* Gives 1 if the x.y.z version is supported in the current version + * Usage: + * + * - Require v6 + * #if LV_VERSION_CHECK(6,0,0) + * new_func_in_v6(); + * #endif + * + * + * - Require at least v5.3 + * #if LV_VERSION_CHECK(5,3,0) + * new_feature_from_v5_3(); + * #endif + * + * + * - Require v5.3.2 bugfixes + * #if LV_VERSION_CHECK(5,3,2) + * bugfix_in_v5_3_2(); + * #endif + * + * */ +#define LV_VERSION_CHECK(x,y,z) (x == LVGL_VERSION_MAJOR && (y < LVGL_VERSION_MINOR || (y == LVGL_VERSION_MINOR && z <= LVGL_VERSION_PATCH))) + + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /*LV_VERSION_H*/ diff --git a/include/liblvgl/lvgl.h b/include/liblvgl/lvgl.h index 60d29f5d..9a6653b9 100644 --- a/include/liblvgl/lvgl.h +++ b/include/liblvgl/lvgl.h @@ -13,60 +13,117 @@ extern "C" { /*************************** * CURRENT VERSION OF LVGL ***************************/ -#define LVGL_VERSION_MAJOR 8 -#define LVGL_VERSION_MINOR 3 -#define LVGL_VERSION_PATCH 4 -#define LVGL_VERSION_INFO "dev" +#include "lv_version.h" /********************* * INCLUDES *********************/ +#include "lvgl_private.h" +#include "liblvgl/lv_init.h" + +#include "liblvgl/stdlib/lv_mem.h" +#include "liblvgl/stdlib/lv_string.h" +#include "liblvgl/stdlib/lv_sprintf.h" #include "liblvgl/misc/lv_log.h" #include "liblvgl/misc/lv_timer.h" #include "liblvgl/misc/lv_math.h" -#include "liblvgl/misc/lv_mem.h" +#include "liblvgl/misc/lv_array.h" #include "liblvgl/misc/lv_async.h" #include "liblvgl/misc/lv_anim_timeline.h" -#include "liblvgl/misc/lv_printf.h" +#include "liblvgl/misc/lv_profiler_builtin.h" +#include "liblvgl/misc/lv_rb.h" +#include "liblvgl/misc/lv_utils.h" -#include "liblvgl/hal/lv_hal.h" +#include "liblvgl/tick/lv_tick.h" #include "liblvgl/core/lv_obj.h" #include "liblvgl/core/lv_group.h" -#include "liblvgl/core/lv_indev.h" +#include "liblvgl/indev/lv_indev.h" #include "liblvgl/core/lv_refr.h" -#include "liblvgl/core/lv_disp.h" -#include "liblvgl/core/lv_theme.h" +#include "liblvgl/display/lv_display.h" #include "liblvgl/font/lv_font.h" -#include "liblvgl/font/lv_font_loader.h" +#include "liblvgl/font/lv_binfont_loader.h" #include "liblvgl/font/lv_font_fmt_txt.h" -#include "liblvgl/widgets/lv_arc.h" -#include "liblvgl/widgets/lv_btn.h" -#include "liblvgl/widgets/lv_img.h" -#include "liblvgl/widgets/lv_label.h" -#include "liblvgl/widgets/lv_line.h" -#include "liblvgl/widgets/lv_table.h" -#include "liblvgl/widgets/lv_checkbox.h" -#include "liblvgl/widgets/lv_bar.h" -#include "liblvgl/widgets/lv_slider.h" -#include "liblvgl/widgets/lv_btnmatrix.h" -#include "liblvgl/widgets/lv_dropdown.h" -#include "liblvgl/widgets/lv_roller.h" -#include "liblvgl/widgets/lv_textarea.h" -#include "liblvgl/widgets/lv_canvas.h" -#include "liblvgl/widgets/lv_switch.h" +#include "liblvgl/widgets/animimage/lv_animimage.h" +#include "liblvgl/widgets/arc/lv_arc.h" +#include "liblvgl/widgets/bar/lv_bar.h" +#include "liblvgl/widgets/button/lv_button.h" +#include "liblvgl/widgets/buttonmatrix/lv_buttonmatrix.h" +#include "liblvgl/widgets/calendar/lv_calendar.h" +#include "liblvgl/widgets/canvas/lv_canvas.h" +#include "liblvgl/widgets/chart/lv_chart.h" +#include "liblvgl/widgets/checkbox/lv_checkbox.h" +#include "liblvgl/widgets/dropdown/lv_dropdown.h" +#include "liblvgl/widgets/image/lv_image.h" +#include "liblvgl/widgets/imagebutton/lv_imagebutton.h" +#include "liblvgl/widgets/keyboard/lv_keyboard.h" +#include "liblvgl/widgets/label/lv_label.h" +#include "liblvgl/widgets/led/lv_led.h" +#include "liblvgl/widgets/line/lv_line.h" +#include "liblvgl/widgets/list/lv_list.h" +#include "liblvgl/widgets/lottie/lv_lottie.h" +#include "liblvgl/widgets/menu/lv_menu.h" +#include "liblvgl/widgets/msgbox/lv_msgbox.h" +#include "liblvgl/widgets/roller/lv_roller.h" +#include "liblvgl/widgets/scale/lv_scale.h" +#include "liblvgl/widgets/slider/lv_slider.h" +#include "liblvgl/widgets/span/lv_span.h" +#include "liblvgl/widgets/spinbox/lv_spinbox.h" +#include "liblvgl/widgets/spinner/lv_spinner.h" +#include "liblvgl/widgets/switch/lv_switch.h" +#include "liblvgl/widgets/table/lv_table.h" +#include "liblvgl/widgets/tabview/lv_tabview.h" +#include "liblvgl/widgets/textarea/lv_textarea.h" +#include "liblvgl/widgets/tileview/lv_tileview.h" +#include "liblvgl/widgets/win/lv_win.h" + +#include "liblvgl/others/snapshot/lv_snapshot.h" +#include "liblvgl/others/sysmon/lv_sysmon.h" +#include "liblvgl/others/monkey/lv_monkey.h" +#include "liblvgl/others/gridnav/lv_gridnav.h" +#include "liblvgl/others/fragment/lv_fragment.h" +#include "liblvgl/others/imgfont/lv_imgfont.h" +#include "liblvgl/others/observer/lv_observer.h" +#include "liblvgl/others/ime/lv_ime_pinyin.h" +#include "liblvgl/others/file_explorer/lv_file_explorer.h" + +#include "liblvgl/libs/barcode/lv_barcode.h" +#include "liblvgl/libs/bin_decoder/lv_bin_decoder.h" +#include "liblvgl/libs/bmp/lv_bmp.h" +#include "liblvgl/libs/rle/lv_rle.h" +#include "liblvgl/libs/fsdrv/lv_fsdrv.h" +#include "liblvgl/libs/lodepng/lv_lodepng.h" +#include "liblvgl/libs/libpng/lv_libpng.h" +#include "liblvgl/libs/gif/lv_gif.h" +#include "liblvgl/libs/qrcode/lv_qrcode.h" +#include "liblvgl/libs/tjpgd/lv_tjpgd.h" +#include "liblvgl/libs/libjpeg_turbo/lv_libjpeg_turbo.h" +#include "liblvgl/libs/freetype/lv_freetype.h" +#include "liblvgl/libs/rlottie/lv_rlottie.h" +#include "liblvgl/libs/ffmpeg/lv_ffmpeg.h" +#include "liblvgl/libs/tiny_ttf/lv_tiny_ttf.h" + +#include "liblvgl/layouts/lv_layout.h" #include "liblvgl/draw/lv_draw.h" +#include "liblvgl/draw/lv_draw_buf.h" +#include "liblvgl/draw/lv_draw_vector.h" +#include "liblvgl/draw/sw/lv_draw_sw.h" + +#include "liblvgl/themes/lv_theme.h" -#include "liblvgl/lv_api_map.h" +#include "liblvgl/drivers/lv_drivers.h" -/*----------------- - * EXTRAS - *----------------*/ -#include "liblvgl/extra/lv_extra.h" +// #include "liblvgl/lv_api_map_v8.h" +#include "liblvgl/lv_api_map_v9_0.h" +#include "liblvgl/lv_api_map_v9_1.h" + +#if LV_USE_PRIVATE_API +#include "liblvgl/lvgl_private.h" +#endif /********************* * DEFINES @@ -126,7 +183,7 @@ static inline int lv_version_patch(void) return LVGL_VERSION_PATCH; } -static inline const char *lv_version_info(void) +static inline const char * lv_version_info(void) { return LVGL_VERSION_INFO; } @@ -135,4 +192,4 @@ static inline const char *lv_version_info(void) } /*extern "C"*/ #endif -#endif /*LVGL_H*/ +#endif /*LVGL_H*/ \ No newline at end of file diff --git a/include/liblvgl/lvgl_private.h b/include/liblvgl/lvgl_private.h new file mode 100644 index 00000000..8ab64e97 --- /dev/null +++ b/include/liblvgl/lvgl_private.h @@ -0,0 +1,125 @@ +/** + * @file lvgl_private.h + * + */ + +#ifndef LVGL_PRIVATE_H +#define LVGL_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "core/lv_global.h" + +#include "display/lv_display_private.h" +#include "indev/lv_indev_private.h" +#include "misc/lv_text_private.h" +#include "misc/cache/lv_cache_entry_private.h" +#include "misc/cache/lv_cache_private.h" +#include "layouts/lv_layout_private.h" +#include "stdlib/lv_mem_private.h" +#include "others/file_explorer/lv_file_explorer_private.h" +#include "others/sysmon/lv_sysmon_private.h" +#include "others/monkey/lv_monkey_private.h" +#include "others/ime/lv_ime_pinyin_private.h" +#include "others/fragment/lv_fragment_private.h" +#include "others/observer/lv_observer_private.h" +#include "libs/qrcode/lv_qrcode_private.h" +#include "libs/barcode/lv_barcode_private.h" +#include "libs/gif/lv_gif_private.h" +#include "draw/lv_draw_triangle_private.h" +#include "draw/lv_draw_private.h" +#include "draw/lv_draw_rect_private.h" +#include "draw/lv_draw_image_private.h" +#include "draw/lv_image_decoder_private.h" +#include "draw/lv_draw_label_private.h" +#include "draw/lv_draw_vector_private.h" +#include "draw/lv_draw_buf_private.h" +#include "draw/lv_draw_mask_private.h" +#include "draw/sw/lv_draw_sw_gradient_private.h" +#include "draw/sw/lv_draw_sw_private.h" +#include "draw/sw/lv_draw_sw_mask_private.h" +#include "draw/sw/blend/lv_draw_sw_blend_private.h" +#include "drivers/libinput/lv_xkb_private.h" +#include "drivers/libinput/lv_libinput_private.h" +#include "font/lv_font_fmt_txt_private.h" +#include "themes/lv_theme_private.h" +#include "core/lv_refr_private.h" +#include "core/lv_obj_style_private.h" +#include "core/lv_obj_private.h" +#include "core/lv_obj_scroll_private.h" +#include "core/lv_obj_draw_private.h" +#include "core/lv_obj_class_private.h" +#include "core/lv_group_private.h" +#include "core/lv_obj_event_private.h" +#include "misc/lv_timer_private.h" +#include "misc/lv_area_private.h" +#include "misc/lv_fs_private.h" +#include "misc/lv_profiler_builtin_private.h" +#include "misc/lv_event_private.h" +#include "misc/lv_bidi_private.h" +#include "misc/lv_rb_private.h" +#include "misc/lv_style_private.h" +#include "misc/lv_color_op_private.h" +#include "misc/lv_anim_private.h" +#include "widgets/msgbox/lv_msgbox_private.h" +#include "widgets/buttonmatrix/lv_buttonmatrix_private.h" +#include "widgets/slider/lv_slider_private.h" +#include "widgets/switch/lv_switch_private.h" +#include "widgets/calendar/lv_calendar_private.h" +#include "widgets/imagebutton/lv_imagebutton_private.h" +#include "widgets/bar/lv_bar_private.h" +#include "widgets/image/lv_image_private.h" +#include "widgets/textarea/lv_textarea_private.h" +#include "widgets/table/lv_table_private.h" +#include "widgets/checkbox/lv_checkbox_private.h" +#include "widgets/roller/lv_roller_private.h" +#include "widgets/win/lv_win_private.h" +#include "widgets/keyboard/lv_keyboard_private.h" +#include "widgets/line/lv_line_private.h" +#include "widgets/animimage/lv_animimage_private.h" +#include "widgets/dropdown/lv_dropdown_private.h" +#include "widgets/menu/lv_menu_private.h" +#include "widgets/chart/lv_chart_private.h" +#include "widgets/button/lv_button_private.h" +#include "widgets/scale/lv_scale_private.h" +#include "widgets/led/lv_led_private.h" +#include "widgets/arc/lv_arc_private.h" +#include "widgets/tileview/lv_tileview_private.h" +#include "widgets/spinbox/lv_spinbox_private.h" +#include "widgets/span/lv_span_private.h" +#include "widgets/label/lv_label_private.h" +#include "widgets/canvas/lv_canvas_private.h" +#include "widgets/tabview/lv_tabview_private.h" +#include "tick/lv_tick_private.h" +#include "stdlib/builtin/lv_tlsf_private.h" +#include "libs/rlottie/lv_rlottie_private.h" +#include "libs/ffmpeg/lv_ffmpeg_private.h" +#include "widgets/lottie/lv_lottie_private.h" +#include "osal/lv_os_private.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LVGL_PRIVATE_H*/ diff --git a/include/liblvgl/misc/cache/lv_cache.h b/include/liblvgl/misc/cache/lv_cache.h new file mode 100644 index 00000000..0271c87f --- /dev/null +++ b/include/liblvgl/misc/cache/lv_cache.h @@ -0,0 +1,237 @@ +/** + * @file lv_cache.h + * + */ + +#ifndef LV_CACHE_H +#define LV_CACHE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "lv_cache_entry.h" +#include "lv_cache_private.h" +#include "../lv_types.h" + +#include "lv_cache_lru_rb.h" + +#include "lv_image_cache.h" +#include "lv_image_header_cache.h" +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create a cache object with the given parameters. + * @param cache_class The class of the cache. Currently only support one two builtin classes: + * - lv_cache_class_lru_rb_count for LRU-based cache with count-based eviction policy. + * - lv_cache_class_lru_rb_size for LRU-based cache with size-based eviction policy. + * @param node_size The node size is the size of the data stored in the cache.. + * @param max_size The max size is the maximum amount of memory or count that the cache can hold. + * - lv_cache_class_lru_rb_count: max_size is the maximum count of nodes in the cache. + * - lv_cache_class_lru_rb_size: max_size is the maximum size of the cache in bytes. + * @param ops A set of operations that can be performed on the cache. See lv_cache_ops_t for details. + * @return Returns a pointer to the created cache object on success, `NULL` on error. + */ +lv_cache_t * lv_cache_create(const lv_cache_class_t * cache_class, + size_t node_size, size_t max_size, + lv_cache_ops_t ops); + +/** + * Destroy a cache object. + * @param cache The cache object pointer to destroy. + * @param user_data A user data pointer that will be passed to the free callback. + */ +void lv_cache_destroy(lv_cache_t * cache, void * user_data); + +/** + * Acquire a cache entry with the given key. If entry not in cache, it will return `NULL` (not found). + * If the entry is found, it's priority will be changed by the cache's policy. And the `lv_cache_entry_t::ref_cnt` will be incremented. + * @param cache The cache object pointer to acquire the entry. + * @param key The key of the entry to acquire. + * @param user_data A user data pointer that will be passed to the create callback. + * @return Returns a pointer to the acquired cache entry on success with `lv_cache_entry_t::ref_cnt` incremented, `NULL` on error. + */ +lv_cache_entry_t * lv_cache_acquire(lv_cache_t * cache, const void * key, void * user_data); + +/** + * Acquire a cache entry with the given key. If the entry is not in the cache, it will create a new entry with the given key. + * If the entry is found, it's priority will be changed by the cache's policy. And the `lv_cache_entry_t::ref_cnt` will be incremented. + * If you want to use this API to simplify the code, you should provide a `lv_cache_ops_t::create_cb` that creates a new entry with the given key. + * This API is a combination of lv_cache_acquire() and lv_cache_add(). The effect is the same as calling lv_cache_acquire() and lv_cache_add() separately. + * And the internal impact on cache is also consistent with these two APIs. + * @param cache The cache object pointer to acquire the entry. + * @param key The key of the entry to acquire or create. + * @param user_data A user data pointer that will be passed to the create callback. + * @return Returns a pointer to the acquired or created cache entry on success with `lv_cache_entry_t::ref_cnt` incremented, `NULL` on error. + */ +lv_cache_entry_t * lv_cache_acquire_or_create(lv_cache_t * cache, const void * key, void * user_data); + +/** + * Add a new cache entry with the given key and data. If the cache is full, the cache's policy will be used to evict an entry. + * @param cache The cache object pointer to add the entry. + * @param key The key of the entry to add. + * @param user_data A user data pointer that will be passed to the create callback. + * @return Returns a pointer to the added cache entry on success with `lv_cache_entry_t::ref_cnt` incremented, `NULL` on error. + */ +lv_cache_entry_t * lv_cache_add(lv_cache_t * cache, const void * key, void * user_data); + +/** + * Release a cache entry. The `lv_cache_entry_t::ref_cnt` will be decremented. If the `lv_cache_entry_t::ref_cnt` is zero, it will issue an error. + * If the entry passed to this function is the last reference to the data and the entry is marked as invalid, the cache's policy will be used to evict the entry. + * @param cache The cache object pointer to release the entry. + * @param entry The cache entry pointer to release. + * @param user_data A user data pointer that will be passed to the free callback. + */ +void lv_cache_release(lv_cache_t * cache, lv_cache_entry_t * entry, void * user_data); + +/** + * Reserve a certain amount of memory/count in the cache. This function is useful when you want to reserve a certain amount of memory/count in advance, + * for example, when you know that you will need it later. + * When the current cache size is max than the reserved size, the function will evict entries until the reserved size is reached. + * @param cache The cache object pointer to reserve. + * @param reserved_size The amount of memory/count to reserve. + * @param user_data A user data pointer that will be passed to the free callback. + */ +void lv_cache_reserve(lv_cache_t * cache, uint32_t reserved_size, void * user_data); + +/** + * Drop a cache entry with the given key. If the entry is not in the cache, nothing will happen to it. + * If the entry is found, it will be removed from the cache and its data will be freed when the last reference to it is released. + * @note The data will not be freed immediately but when the last reference to it is released. But this entry will not be found by lv_cache_acquire(). + * If you want cache a same key again, you should use lv_cache_add() or lv_cache_acquire_or_create(). + * @param cache The cache object pointer to drop the entry. + * @param key The key of the entry to drop. + * @param user_data A user data pointer that will be passed to the free callback. + */ +void lv_cache_drop(lv_cache_t * cache, const void * key, void * user_data); + +/** + * Drop all cache entries. All entries will be removed from the cache and their data will be freed when the last reference to them is released. + * @note If some entries are still referenced by other objects, it will issue an error. And this case shouldn't happen in normal cases.. + * @param cache The cache object pointer to drop all entries. + * @param user_data A user data pointer that will be passed to the free callback. + */ +void lv_cache_drop_all(lv_cache_t * cache, void * user_data); + +/** + * Evict one entry from the cache. The eviction policy will be used to select the entry to evict. + * @param cache The cache object pointer to evict an entry. + * @param user_data A user data pointer that will be passed to the free callback. + * @return Returns true if an entry is evicted, false if no entry is evicted. + */ +bool lv_cache_evict_one(lv_cache_t * cache, void * user_data); + +/** + * Set the maximum size of the cache. + * If the current cache size is greater than the new maximum size, the cache's policy will be used to evict entries until the new maximum size is reached. + * If set to 0, the cache will be disabled. + * @note But this behavior will happen only new entries are added to the cache. + * @param cache The cache object pointer to set the maximum size. + * @param max_size The new maximum size of the cache. + * @param user_data A user data pointer that will be passed to the free callback. + */ +void lv_cache_set_max_size(lv_cache_t * cache, size_t max_size, void * user_data); + +/** + * Get the maximum size of the cache. + * @param cache The cache object pointer to get the maximum size. + * @param user_data A user data pointer that will be passed to the free callback. + * @return Returns the maximum size of the cache. + */ +size_t lv_cache_get_max_size(lv_cache_t * cache, void * user_data); + +/** + * Get the current size of the cache. + * @param cache The cache object pointer to get the current size. + * @param user_data A user data pointer that will be passed to the free callback. + * @return Returns the current size of the cache. + */ +size_t lv_cache_get_size(lv_cache_t * cache, void * user_data); + +/** + * Get the free size of the cache. + * @param cache The cache object pointer to get the free size. + * @param user_data A user data pointer that will be passed to the free callback. + * @return Returns the free size of the cache. + */ +size_t lv_cache_get_free_size(lv_cache_t * cache, void * user_data); + +/** + * Return true if the cache is enabled. + * Disabled cache means that when the max_size of the cache is 0. In this case, all cache operations will be no-op. + * @param cache The cache object pointer to check if it's disabled. + * @return Returns true if the cache is enabled, false otherwise. + */ +bool lv_cache_is_enabled(lv_cache_t * cache); + +/** + * Set the compare callback of the cache. + * @param cache The cache object pointer to set the compare callback. + * @param compare_cb The compare callback to set. + * @param user_data A user data pointer. + */ +void lv_cache_set_compare_cb(lv_cache_t * cache, lv_cache_compare_cb_t compare_cb, void * user_data); + +/** + * Set the create callback of the cache. + * @param cache The cache object pointer to set the create callback. + * @param alloc_cb The create callback to set. + * @param user_data A user data pointer. + */ +void lv_cache_set_create_cb(lv_cache_t * cache, lv_cache_create_cb_t alloc_cb, void * user_data); + +/** + * Set the free callback of the cache. + * @param cache The cache object pointer to set the free callback. + * @param free_cb The free callback to set. + * @param user_data A user data pointer. + */ +void lv_cache_set_free_cb(lv_cache_t * cache, lv_cache_free_cb_t free_cb, void * user_data); + +/** + * Give a name for a cache object. Only the pointer of the string is saved. + * @param cache The cache object pointer to set the name. + * @param name The name of the cache. + */ +void lv_cache_set_name(lv_cache_t * cache, const char * name); + +/** + * Get the name of a cache object. + * @param cache The cache object pointer to get the name. + * @return Returns the name of the cache. + */ +const char * lv_cache_get_name(lv_cache_t * cache); + +/** + * Create an iterator for the cache object. The iterator is used to iterate over all cache entries. + * @param cache The cache object pointer to create the iterator. + * @return Returns a pointer to the created iterator on success, `NULL` on error. + */ +lv_iter_t * lv_cache_iter_create(lv_cache_t * cache); + +/************************* + * GLOBAL VARIABLES + *************************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /* LV_CACHE_H */ diff --git a/include/liblvgl/misc/cache/lv_cache_entry.h b/include/liblvgl/misc/cache/lv_cache_entry.h new file mode 100644 index 00000000..947f71d7 --- /dev/null +++ b/include/liblvgl/misc/cache/lv_cache_entry.h @@ -0,0 +1,114 @@ +/** +* @file lv_cache_entry.h +* + */ + +#ifndef LV_CACHE_ENTRY_H +#define LV_CACHE_ENTRY_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../osal/lv_os.h" +#include "../lv_types.h" +#include "lv_cache_private.h" +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Get the size of a cache entry. + * @param node_size The size of the node in the cache. + * @return The size of the cache entry. + */ +uint32_t lv_cache_entry_get_size(const uint32_t node_size); + +/** + * Get the reference count of a cache entry. + * @param entry The cache entry to get the reference count of. + * @return The reference count of the cache entry. + */ +int32_t lv_cache_entry_get_ref(lv_cache_entry_t * entry); + +/** + * Get the node size of a cache entry. Which is the same size with lv_cache_entry_get_size()'s node_size parameter. + * @param entry The cache entry to get the node size of. + * @return The node size of the cache entry. + */ +uint32_t lv_cache_entry_get_node_size(lv_cache_entry_t * entry); + +/** + * Check if a cache entry is invalid. + * @param entry The cache entry to check. + * @return True: the cache entry is invalid. False: the cache entry is valid. + */ +bool lv_cache_entry_is_invalid(lv_cache_entry_t * entry); + +/** + * Get the data of a cache entry. + * @param entry The cache entry to get the data of. + * @return The pointer to the data of the cache entry. + */ +void * lv_cache_entry_get_data(lv_cache_entry_t * entry); + +/** + * Get the cache instance of a cache entry. + * @param entry The cache entry to get the cache instance of. + * @return The pointer to the cache instance of the cache entry. + */ +const lv_cache_t * lv_cache_entry_get_cache(const lv_cache_entry_t * entry); + +/** + * Get the cache entry of a data. The data should be allocated by the cache instance. + * @param data The data to get the cache entry of. + * @param node_size The size of the node in the cache. + * @return The pointer to the cache entry of the data. + */ +lv_cache_entry_t * lv_cache_entry_get_entry(void * data, const uint32_t node_size); + +/** + * Allocate a cache entry. + * @param node_size The size of the node in the cache. + * @param cache The cache instance to allocate the cache entry from. + * @return The pointer to the allocated cache entry. + */ +lv_cache_entry_t * lv_cache_entry_alloc(const uint32_t node_size, const lv_cache_t * cache); + +/** + * Initialize a cache entry. + * @param entry The cache entry to initialize. + * @param cache The cache instance to allocate the cache entry from. + * @param node_size The size of the node in the cache. + */ +void lv_cache_entry_init(lv_cache_entry_t * entry, const lv_cache_t * cache, const uint32_t node_size); + +/** + * Deallocate a cache entry. And the data of the cache entry will be freed. + * @param entry The cache entry to deallocate. + */ +void lv_cache_entry_delete(lv_cache_entry_t * entry); +/************************* + * GLOBAL VARIABLES + *************************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_CACHE_ENTRY_H*/ diff --git a/include/liblvgl/misc/cache/lv_cache_entry_private.h b/include/liblvgl/misc/cache/lv_cache_entry_private.h new file mode 100644 index 00000000..f8d41bb9 --- /dev/null +++ b/include/liblvgl/misc/cache/lv_cache_entry_private.h @@ -0,0 +1,50 @@ +/** + * @file lv_cache_entry_private.h + * + */ + +#ifndef LV_CACHE_ENTRY_PRIVATE_H +#define LV_CACHE_ENTRY_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../lv_types.h" +#include "../../osal/lv_os.h" +#include "../lv_profiler.h" +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ +void lv_cache_entry_reset_ref(lv_cache_entry_t * entry); +void lv_cache_entry_inc_ref(lv_cache_entry_t * entry); +void lv_cache_entry_dec_ref(lv_cache_entry_t * entry); +void lv_cache_entry_set_node_size(lv_cache_entry_t * entry, uint32_t node_size); +void lv_cache_entry_set_invalid(lv_cache_entry_t * entry, bool is_invalid); +void lv_cache_entry_set_cache(lv_cache_entry_t * entry, const lv_cache_t * cache); +void * lv_cache_entry_acquire_data(lv_cache_entry_t * entry); +void lv_cache_entry_release_data(lv_cache_entry_t * entry, void * user_data); +/************************* + * GLOBAL VARIABLES + *************************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /* LV_CACHE_ENTRY_PRIVATE_H */ diff --git a/include/liblvgl/misc/cache/lv_cache_lru_rb.h b/include/liblvgl/misc/cache/lv_cache_lru_rb.h new file mode 100644 index 00000000..c0f44f39 --- /dev/null +++ b/include/liblvgl/misc/cache/lv_cache_lru_rb.h @@ -0,0 +1,44 @@ +/** +* @file lv_cache_lru_rb.h +* +*/ + +#ifndef LV_CACHE_LRU_RB_H +#define LV_CACHE_LRU_RB_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "lv_cache_entry.h" +#include "lv_cache_private.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/************************* + * GLOBAL VARIABLES + *************************/ +LV_ATTRIBUTE_EXTERN_DATA extern const lv_cache_class_t lv_cache_class_lru_rb_count; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_cache_class_t lv_cache_class_lru_rb_size; +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_CACHE_LRU_RB_H*/ diff --git a/include/liblvgl/misc/cache/lv_cache_private.h b/include/liblvgl/misc/cache/lv_cache_private.h new file mode 100644 index 00000000..d197fbf6 --- /dev/null +++ b/include/liblvgl/misc/cache/lv_cache_private.h @@ -0,0 +1,201 @@ +/** +* @file lv_cache_private.h +* +*/ + +#ifndef LV_CACHE_PRIVATE_H +#define LV_CACHE_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../lv_types.h" +#include "../../osal/lv_os.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** + * The result of the cache reserve condition callback + */ +typedef enum { + LV_CACHE_RESERVE_COND_OK, /**< The condition is met and no entries need to be evicted */ + LV_CACHE_RESERVE_COND_TOO_LARGE, /**< The condition is not met and the reserve size is too large */ + LV_CACHE_RESERVE_COND_NEED_VICTIM, /**< The condition is not met and a victim is needed to be evicted */ + LV_CACHE_RESERVE_COND_ERROR /**< An error occurred while checking the condition */ +} lv_cache_reserve_cond_res_t; + +struct _lv_cache_ops_t; +struct _lv_cache_t; +struct _lv_cache_class_t; +struct _lv_cache_entry_t; + +typedef struct _lv_cache_ops_t lv_cache_ops_t; +typedef struct _lv_cache_t lv_cache_t; +typedef struct _lv_cache_class_t lv_cache_class_t; +typedef struct _lv_cache_entry_t lv_cache_entry_t; + +typedef int8_t lv_cache_compare_res_t; +typedef bool (*lv_cache_create_cb_t)(void * node, void * user_data); +typedef void (*lv_cache_free_cb_t)(void * node, void * user_data); +typedef lv_cache_compare_res_t (*lv_cache_compare_cb_t)(const void * a, const void * b); + +/** + * The cache instance allocation function, used by the cache class to allocate memory for cache instances. + * @return It should return a pointer to the allocated instance. + */ +typedef void * (*lv_cache_alloc_cb_t)(void); + +/** + * The cache instance initialization function, used by the cache class to initialize the cache instance. + * @return It should return true if the initialization is successful, false otherwise. + */ +typedef bool (*lv_cache_init_cb_t)(lv_cache_t * cache); + +/** + * The cache instance destruction function, used by the cache class to destroy the cache instance. + */ +typedef void (*lv_cache_destroy_cb_t)(lv_cache_t * cache, void * user_data); + +/** + * The cache get function, used by the cache class to get a cache entry by its key. + * @return `NULL` if the key is not found. + */ +typedef lv_cache_entry_t * (*lv_cache_get_cb_t)(lv_cache_t * cache, const void * key, void * user_data); + +/** + * The cache add function, used by the cache class to add a cache entry with a given key. + * This function only cares about how to add the entry, it doesn't check if the entry already exists and doesn't care about is it a victim or not. + * @return the added cache entry, or NULL if the entry is not added. + */ +typedef lv_cache_entry_t * (*lv_cache_add_cb_t)(lv_cache_t * cache, const void * key, void * user_data); + +/** + * The cache remove function, used by the cache class to remove a cache entry from the cache but doesn't free the memory.. + * This function only cares about how to remove the entry, it doesn't care about is it a victim or not. + */ +typedef void (*lv_cache_remove_cb_t)(lv_cache_t * cache, lv_cache_entry_t * entry, void * user_data); + +/** + * The cache drop function, used by the cache class to remove a cache entry from the cache and free the memory. + */ +typedef void (*lv_cache_drop_cb_t)(lv_cache_t * cache, const void * key, void * user_data); + +/** + * The cache drop all function, used by the cache class to remove all cache entries from the cache and free the memory. + */ +typedef void (*lv_cache_drop_all_cb_t)(lv_cache_t * cache, void * user_data); + +/** + * The cache get victim function, used by the cache class to get a victim entry to be evicted. + */ +typedef lv_cache_entry_t * (*lv_cache_get_victim_cb)(lv_cache_t * cache, void * user_data); + +/** + * The cache reserve condition function, used by the cache class to check if a new entry can be added to the cache without exceeding its maximum size. + * See lv_cache_reserve_cond_res_t for the possible results. + */ +typedef lv_cache_reserve_cond_res_t (*lv_cache_reserve_cond_cb)(lv_cache_t * cache, const void * key, size_t size, + void * user_data); + +/** + * The cache iterator creation function, used by the cache class to create an iterator for the cache. + * @return A pointer to the created iterator, or NULL if the iterator cannot be created. + */ +typedef lv_iter_t * (*lv_cache_iter_create_cb)(lv_cache_t * cache); + +/** + * The cache operations struct + */ +struct _lv_cache_ops_t { + lv_cache_compare_cb_t compare_cb; /**< Compare function for keys */ + lv_cache_create_cb_t create_cb; /**< Create function for nodes */ + lv_cache_free_cb_t free_cb; /**< Free function for nodes */ +}; + +/** + * The cache entry struct + */ +struct _lv_cache_t { + const lv_cache_class_t * clz; /**< Cache class. There are two built-in classes: + * - lv_cache_class_lru_rb_count for LRU-based cache with count-based eviction policy. + * - lv_cache_class_lru_rb_size for LRU-based cache with size-based eviction policy. */ + + uint32_t node_size; /**< Size of a node */ + + uint32_t max_size; /**< Maximum size of the cache */ + uint32_t size; /**< Current size of the cache */ + + lv_cache_ops_t ops; /**< Cache operations struct _lv_cache_ops_t */ + + lv_mutex_t lock; /**< Cache lock used to protect the cache in multithreading environments */ + + const char * name; /**< Name of the cache */ +}; + +/** + * Cache class struct for building custom cache classes + * + * Examples: + * - lv_cache_class_lru_rb_count for LRU-based cache with count-based eviction policy. + * - lv_cache_class_lru_rb_size for LRU-based cache with size-based eviction policy. + */ +struct _lv_cache_class_t { + lv_cache_alloc_cb_t alloc_cb; /**< The allocation function for cache entries */ + lv_cache_init_cb_t init_cb; /**< The initialization function for cache entries */ + lv_cache_destroy_cb_t destroy_cb; /**< The destruction function for cache entries */ + + lv_cache_get_cb_t get_cb; /**< The get function for cache entries */ + lv_cache_add_cb_t add_cb; /**< The add function for cache entries */ + lv_cache_remove_cb_t remove_cb; /**< The remove function for cache entries */ + lv_cache_drop_cb_t drop_cb; /**< The drop function for cache entries */ + lv_cache_drop_all_cb_t drop_all_cb; /**< The drop all function for cache entries */ + lv_cache_get_victim_cb get_victim_cb; /**< The get victim function for cache entries */ + lv_cache_reserve_cond_cb reserve_cond_cb; /**< The reserve condition function for cache entries */ + + lv_cache_iter_create_cb iter_create_cb; /**< The iterator creation function for cache entries */ +}; + +/*----------------- + * Cache entry slot + *----------------*/ + +struct _lv_cache_slot_size_t; + +typedef struct _lv_cache_slot_size_t lv_cache_slot_size_t; + +/** + * Cache entry slot struct + * + * To add new fields to the cache entry, add them to a new struct and add it to the first + * field of the cache data struct. And this one is a size slot for the cache entry. + */ +struct _lv_cache_slot_size_t { + size_t size; +}; +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/************************* + * GLOBAL VARIABLES + *************************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_CACHE_PRIVATE_H*/ diff --git a/include/liblvgl/misc/cache/lv_image_cache.h b/include/liblvgl/misc/cache/lv_image_cache.h new file mode 100644 index 00000000..8ae20fe6 --- /dev/null +++ b/include/liblvgl/misc/cache/lv_image_cache.h @@ -0,0 +1,82 @@ +/** +* @file lv_image_cache.h +* + */ + +#ifndef LV_IMAGE_CACHE_H +#define LV_IMAGE_CACHE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../lv_conf_internal.h" +#include "../lv_types.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Initialize image cache. + * @param size size of the cache in bytes. + * @return LV_RESULT_OK: initialization succeeded, LV_RESULT_INVALID: failed. + */ +lv_result_t lv_image_cache_init(uint32_t size); + +/** + * Resize image cache. + * If set to 0, the cache will be disabled. + * @param new_size new size of the cache in bytes. + * @param evict_now true: evict the images should be removed by the eviction policy, false: wait for the next cache cleanup. + */ +void lv_image_cache_resize(uint32_t new_size, bool evict_now); + +/** + * Invalidate image cache. Use NULL to invalidate all images. + * @param src pointer to an image source. + */ +void lv_image_cache_drop(const void * src); + +/** + * Return true if the image cache is enabled. + * @return true: enabled, false: disabled. + */ +bool lv_image_cache_is_enabled(void); + +/** + * Create an iterator to iterate over the image cache. + * @return an iterator to iterate over the image cache. + */ +lv_iter_t * lv_image_cache_iter_create(void); + +/** + * Dump the content of the image cache in a human-readable format with cache order. + */ +void lv_image_cache_dump(void); + +/************************* + * GLOBAL VARIABLES + *************************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_IMAGE_CACHE_H*/ diff --git a/include/liblvgl/misc/cache/lv_image_header_cache.h b/include/liblvgl/misc/cache/lv_image_header_cache.h new file mode 100644 index 00000000..1886be38 --- /dev/null +++ b/include/liblvgl/misc/cache/lv_image_header_cache.h @@ -0,0 +1,83 @@ +/** +* @file lv_image_header_cache.h +* + */ + +#ifndef LV_IMAGE_HEADER_CACHE_H +#define LV_IMAGE_HEADER_CACHE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../lv_conf_internal.h" +#include "../lv_types.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Initialize image header cache. + * @param count initial size of the cache in count of image headers. + * @return LV_RESULT_OK: initialization succeeded, LV_RESULT_INVALID: failed. + */ +lv_result_t lv_image_header_cache_init(uint32_t count); + +/** + * Resize image header cache. + * If set to 0, the cache is disabled. + * @param count new max count of cached image headers. + * @param evict_now true: evict the image headers should be removed by the eviction policy, false: wait for the next cache cleanup. + */ +void lv_image_header_cache_resize(uint32_t count, bool evict_now); + +/** + * Invalidate image header cache. Use NULL to invalidate all image headers. + * It's also automatically called when an image is invalidated. + * @param src pointer to an image source. + */ +void lv_image_header_cache_drop(const void * src); + +/** + * Return true if the image header cache is enabled. + * @return true: enabled, false: disabled. + */ +bool lv_image_header_cache_is_enabled(void); + +/** + * Create an iterator to iterate over the image header cache. + * @return an iterator to iterate over the image header cache. + */ +lv_iter_t * lv_image_header_cache_iter_create(void); + +/** + * Dump the content of the image header cache in a human-readable format with cache order. + */ +void lv_image_header_cache_dump(void); + +/************************* + * GLOBAL VARIABLES + *************************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_IMAGE_HEADER_CACHE_H*/ diff --git a/include/liblvgl/misc/lv_anim.h b/include/liblvgl/misc/lv_anim.h index cb460ab8..84602e41 100644 --- a/include/liblvgl/misc/lv_anim.h +++ b/include/liblvgl/misc/lv_anim.h @@ -13,19 +13,65 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" - -#include -#include -#include +#include "../lv_conf_internal.h" +#include "lv_types.h" +#include "lv_math.h" +#include "lv_timer.h" +#include "lv_ll.h" /********************* * DEFINES *********************/ -#define LV_ANIM_REPEAT_INFINITE 0xFFFF +#define LV_ANIM_REPEAT_INFINITE 0xFFFFFFFF #define LV_ANIM_PLAYTIME_INFINITE 0xFFFFFFFF +/* + * Macros used to set cubic-bezier anim parameter. + * Parameters come from https://easings.net/ + * + * Usage: + * + * lv_anim_t a; + * lv_anim_init(&a); + * ... + * lv_anim_set_path_cb(&a, lv_anim_path_custom_bezier3); + * LV_ANIM_SET_EASE_IN_SINE(&a); //Set cubic-bezier anim parameter to easeInSine + * ... + * lv_anim_start(&a); + */ + +#define _PARA(a, x1, y1, x2, y2) ((a)->parameter.bezier3 = \ +(lv_anim_bezier3_para_t) { \ + LV_BEZIER_VAL_FLOAT(x1), LV_BEZIER_VAL_FLOAT(y1), \ + LV_BEZIER_VAL_FLOAT(x2), LV_BEZIER_VAL_FLOAT(y2) } \ + ) + +#define LV_ANIM_SET_EASE_IN_SINE(a) _PARA(a, 0.12, 0, 0.39, 0) +#define LV_ANIM_SET_EASE_OUT_SINE(a) _PARA(a, 0.61, 1, 0.88, 1) +#define LV_ANIM_SET_EASE_IN_OUT_SINE(a) _PARA(a, 0.37, 0, 0.63, 1) +#define LV_ANIM_SET_EASE_IN_QUAD(a) _PARA(a, 0.11, 0, 0.5, 0) +#define LV_ANIM_SET_EASE_OUT_QUAD(a) _PARA(a, 0.5, 1, 0.89, 1) +#define LV_ANIM_SET_EASE_IN_OUT_QUAD(a) _PARA(a, 0.45, 0, 0.55, 1) +#define LV_ANIM_SET_EASE_IN_CUBIC(a) _PARA(a, 0.32, 0, 0.67, 0) +#define LV_ANIM_SET_EASE_OUT_CUBIC(a) _PARA(a, 0.33, 1, 0.68, 1) +#define LV_ANIM_SET_EASE_IN_OUT_CUBIC(a) _PARA(a, 0.65, 0, 0.35, 1) +#define LV_ANIM_SET_EASE_IN_QUART(a) _PARA(a, 0.5, 0, 0.75, 0) +#define LV_ANIM_SET_EASE_OUT_QUART(a) _PARA(a, 0.25, 1, 0.5, 1) +#define LV_ANIM_SET_EASE_IN_OUT_QUART(a) _PARA(a, 0.76, 0, 0.24, 1) +#define LV_ANIM_SET_EASE_IN_QUINT(a) _PARA(a, 0.64, 0, 0.78, 0) +#define LV_ANIM_SET_EASE_OUT_QUINT(a) _PARA(a, 0.22, 1, 0.36, 1) +#define LV_ANIM_SET_EASE_IN_OUT_QUINT(a) _PARA(a, 0.83, 0, 0.17, 1) +#define LV_ANIM_SET_EASE_IN_EXPO(a) _PARA(a, 0.7, 0, 0.84, 0) +#define LV_ANIM_SET_EASE_OUT_EXPO(a) _PARA(a, 0.16, 1, 0.3, 1) +#define LV_ANIM_SET_EASE_IN_OUT_EXPO(a) _PARA(a, 0.87, 0, 0.13, 1) +#define LV_ANIM_SET_EASE_IN_CIRC(a) _PARA(a, 0.55, 0, 1, 0.45) +#define LV_ANIM_SET_EASE_OUT_CIRC(a) _PARA(a, 0, 0.55, 0.45, 1) +#define LV_ANIM_SET_EASE_IN_OUT_CIRC(a) _PARA(a, 0.85, 0, 0.15, 1) +#define LV_ANIM_SET_EASE_IN_BACK(a) _PARA(a, 0.36, 0, 0.66, -0.56) +#define LV_ANIM_SET_EASE_OUT_BACK(a) _PARA(a, 0.34, 1.56, 0.64, 1) +#define LV_ANIM_SET_EASE_IN_OUT_BACK(a) _PARA(a, 0.68, -0.6, 0.32, 1.6) + LV_EXPORT_CONST_INT(LV_ANIM_REPEAT_INFINITE); LV_EXPORT_CONST_INT(LV_ANIM_PLAYTIME_INFINITE); @@ -39,11 +85,8 @@ typedef enum { LV_ANIM_ON, } lv_anim_enable_t; -struct _lv_anim_t; -struct _lv_timer_t; - /** Get the current value during an animation*/ -typedef int32_t (*lv_anim_path_cb_t)(const struct _lv_anim_t *); +typedef int32_t (*lv_anim_path_cb_t)(const lv_anim_t *); /** Generic prototype of "animator" functions. * First parameter is the variable to animate. @@ -55,58 +98,65 @@ typedef void (*lv_anim_exec_xcb_t)(void *, int32_t); /** Same as `lv_anim_exec_xcb_t` but receives `lv_anim_t *` as the first parameter. * It's more consistent but less convenient. Might be used by binding generator functions.*/ -typedef void (*lv_anim_custom_exec_cb_t)(struct _lv_anim_t *, int32_t); +typedef void (*lv_anim_custom_exec_cb_t)(lv_anim_t *, int32_t); /** Callback to call when the animation is ready*/ -typedef void (*lv_anim_ready_cb_t)(struct _lv_anim_t *); +typedef void (*lv_anim_completed_cb_t)(lv_anim_t *); /** Callback to call when the animation really stars (considering `delay`)*/ -typedef void (*lv_anim_start_cb_t)(struct _lv_anim_t *); +typedef void (*lv_anim_start_cb_t)(lv_anim_t *); /** Callback used when the animation values are relative to get the current value*/ -typedef int32_t (*lv_anim_get_value_cb_t)(struct _lv_anim_t *); +typedef int32_t (*lv_anim_get_value_cb_t)(lv_anim_t *); /** Callback used when the animation is deleted*/ -typedef void (*lv_anim_deleted_cb_t)(struct _lv_anim_t *); +typedef void (*lv_anim_deleted_cb_t)(lv_anim_t *); + +/** Parameter used when path is custom_bezier */ +typedef struct { + int16_t x1; + int16_t y1; + int16_t x2; + int16_t y2; +} lv_anim_bezier3_para_t; /** Describes an animation*/ -typedef struct _lv_anim_t { - void * var; /**var = var; -} +void lv_anim_set_var(lv_anim_t * a, void * var); /** * Set a function to animate `var` @@ -135,30 +182,26 @@ static inline void lv_anim_set_var(lv_anim_t * a, void * var) * LVGL's built-in functions can be used. * E.g. lv_obj_set_x */ -static inline void lv_anim_set_exec_cb(lv_anim_t * a, lv_anim_exec_xcb_t exec_cb) -{ - a->exec_cb = exec_cb; -} +void lv_anim_set_exec_cb(lv_anim_t * a, lv_anim_exec_xcb_t exec_cb); /** * Set the duration of an animation * @param a pointer to an initialized `lv_anim_t` variable * @param duration duration of the animation in milliseconds */ -static inline void lv_anim_set_time(lv_anim_t * a, uint32_t duration) -{ - a->time = duration; -} +void lv_anim_set_duration(lv_anim_t * a, uint32_t duration); + +/** + * Legacy `lv_anim_set_time` API will be removed soon, use `lv_anim_set_duration` instead. + */ +void lv_anim_set_time(lv_anim_t * a, uint32_t duration); /** * Set a delay before starting the animation * @param a pointer to an initialized `lv_anim_t` variable * @param delay delay before the animation in milliseconds */ -static inline void lv_anim_set_delay(lv_anim_t * a, uint32_t delay) -{ - a->act_time = -(int32_t)(delay); -} +void lv_anim_set_delay(lv_anim_t * a, uint32_t delay); /** * Set the start and end values of an animation @@ -166,47 +209,31 @@ static inline void lv_anim_set_delay(lv_anim_t * a, uint32_t delay) * @param start the start value * @param end the end value */ -static inline void lv_anim_set_values(lv_anim_t * a, int32_t start, int32_t end) -{ - a->start_value = start; - a->current_value = start; - a->end_value = end; -} +void lv_anim_set_values(lv_anim_t * a, int32_t start, int32_t end); /** * Similar to `lv_anim_set_exec_cb` but `lv_anim_custom_exec_cb_t` receives * `lv_anim_t * ` as its first parameter instead of `void *`. * This function might be used when LVGL is bound to other languages because * it's more consistent to have `lv_anim_t *` as first parameter. - * The variable to animate can be stored in the animation's `user_data` * @param a pointer to an initialized `lv_anim_t` variable * @param exec_cb a function to execute. */ -static inline void lv_anim_set_custom_exec_cb(lv_anim_t * a, lv_anim_custom_exec_cb_t exec_cb) -{ - a->var = a; - a->exec_cb = (lv_anim_exec_xcb_t)exec_cb; -} +void lv_anim_set_custom_exec_cb(lv_anim_t * a, lv_anim_custom_exec_cb_t exec_cb); /** * Set the path (curve) of the animation. * @param a pointer to an initialized `lv_anim_t` variable * @param path_cb a function to set the current value of the animation. */ -static inline void lv_anim_set_path_cb(lv_anim_t * a, lv_anim_path_cb_t path_cb) -{ - a->path_cb = path_cb; -} +void lv_anim_set_path_cb(lv_anim_t * a, lv_anim_path_cb_t path_cb); /** * Set a function call when the animation really starts (considering `delay`) * @param a pointer to an initialized `lv_anim_t` variable * @param start_cb a function call when the animation starts */ -static inline void lv_anim_set_start_cb(lv_anim_t * a, lv_anim_start_cb_t start_cb) -{ - a->start_cb = start_cb; -} +void lv_anim_set_start_cb(lv_anim_t * a, lv_anim_start_cb_t start_cb); /** * Set a function to use the current value of the variable and make start and end value @@ -214,70 +241,54 @@ static inline void lv_anim_set_start_cb(lv_anim_t * a, lv_anim_start_cb_t start_ * @param a pointer to an initialized `lv_anim_t` variable * @param get_value_cb a function call when the animation starts */ -static inline void lv_anim_set_get_value_cb(lv_anim_t * a, lv_anim_get_value_cb_t get_value_cb) -{ - a->get_value_cb = get_value_cb; -} +void lv_anim_set_get_value_cb(lv_anim_t * a, lv_anim_get_value_cb_t get_value_cb); /** - * Set a function call when the animation is ready - * @param a pointer to an initialized `lv_anim_t` variable - * @param ready_cb a function call when the animation is ready + * Set a function call when the animation is completed + * @param a pointer to an initialized `lv_anim_t` variable + * @param completed_cb a function call when the animation is fully completed */ -static inline void lv_anim_set_ready_cb(lv_anim_t * a, lv_anim_ready_cb_t ready_cb) -{ - a->ready_cb = ready_cb; -} +void lv_anim_set_completed_cb(lv_anim_t * a, lv_anim_completed_cb_t completed_cb); /** * Set a function call when the animation is deleted. * @param a pointer to an initialized `lv_anim_t` variable * @param deleted_cb a function call when the animation is deleted */ -static inline void lv_anim_set_deleted_cb(lv_anim_t * a, lv_anim_deleted_cb_t deleted_cb) -{ - a->deleted_cb = deleted_cb; -} +void lv_anim_set_deleted_cb(lv_anim_t * a, lv_anim_deleted_cb_t deleted_cb); /** * Make the animation to play back to when the forward direction is ready * @param a pointer to an initialized `lv_anim_t` variable - * @param time the duration of the playback animation in milliseconds. 0: disable playback + * @param duration duration of playback animation in milliseconds. 0: disable playback + */ +void lv_anim_set_playback_duration(lv_anim_t * a, uint32_t duration); + +/** + * Legacy `lv_anim_set_playback_time` API will be removed soon, use `lv_anim_set_playback_duration` instead. */ -static inline void lv_anim_set_playback_time(lv_anim_t * a, uint32_t time) -{ - a->playback_time = time; -} +void lv_anim_set_playback_time(lv_anim_t * a, uint32_t duration); /** * Make the animation to play back to when the forward direction is ready * @param a pointer to an initialized `lv_anim_t` variable * @param delay delay in milliseconds before starting the playback animation. */ -static inline void lv_anim_set_playback_delay(lv_anim_t * a, uint32_t delay) -{ - a->playback_delay = delay; -} +void lv_anim_set_playback_delay(lv_anim_t * a, uint32_t delay); /** * Make the animation repeat itself. * @param a pointer to an initialized `lv_anim_t` variable * @param cnt repeat count or `LV_ANIM_REPEAT_INFINITE` for infinite repetition. 0: to disable repetition. */ -static inline void lv_anim_set_repeat_count(lv_anim_t * a, uint16_t cnt) -{ - a->repeat_cnt = cnt; -} +void lv_anim_set_repeat_count(lv_anim_t * a, uint32_t cnt); /** * Set a delay before repeating the animation. * @param a pointer to an initialized `lv_anim_t` variable * @param delay delay in milliseconds before repeating the animation. */ -static inline void lv_anim_set_repeat_delay(lv_anim_t * a, uint32_t delay) -{ - a->repeat_delay = delay; -} +void lv_anim_set_repeat_delay(lv_anim_t * a, uint32_t delay); /** * Set a whether the animation's should be applied immediately or only when the delay expired. @@ -285,22 +296,24 @@ static inline void lv_anim_set_repeat_delay(lv_anim_t * a, uint32_t delay) * @param en true: apply the start value immediately in `lv_anim_start`; * false: apply the start value only when `delay` ms is elapsed and the animations really starts */ -static inline void lv_anim_set_early_apply(lv_anim_t * a, bool en) -{ - a->early_apply = en; -} +void lv_anim_set_early_apply(lv_anim_t * a, bool en); /** * Set the custom user data field of the animation. * @param a pointer to an initialized `lv_anim_t` variable * @param user_data pointer to the new user_data. */ -#if LV_USE_USER_DATA -static inline void lv_anim_set_user_data(lv_anim_t * a, void * user_data) -{ - a->user_data = user_data; -} -#endif +void lv_anim_set_user_data(lv_anim_t * a, void * user_data); + +/** + * Set parameter for cubic bezier path + * @param a pointer to an initialized `lv_anim_t` variable + * @param x1 first control point X + * @param y1 first control point Y + * @param x2 second control point X + * @param y2 second control point Y + */ +void lv_anim_set_bezier3_param(lv_anim_t * a, int16_t x1, int16_t y1, int16_t x2, int16_t y2); /** * Create an animation @@ -314,43 +327,49 @@ lv_anim_t * lv_anim_start(const lv_anim_t * a); * @param a pointer to an initialized `lv_anim_t` variable * @return delay before the animation in milliseconds */ -static inline uint32_t lv_anim_get_delay(lv_anim_t * a) -{ - return -a->act_time; -} +uint32_t lv_anim_get_delay(const lv_anim_t * a); /** * Get the time used to play the animation. * @param a pointer to an animation. * @return the play time in milliseconds. */ -uint32_t lv_anim_get_playtime(lv_anim_t * a); +uint32_t lv_anim_get_playtime(const lv_anim_t * a); + +/** + * Get the duration of an animation + * @param a pointer to an initialized `lv_anim_t` variable + * @return the duration of the animation in milliseconds + */ +uint32_t lv_anim_get_time(const lv_anim_t * a); + +/** + * Get the repeat count of the animation. + * @param a pointer to an initialized `lv_anim_t` variable + * @return the repeat count or `LV_ANIM_REPEAT_INFINITE` for infinite repetition. 0: disabled repetition. + */ +uint32_t lv_anim_get_repeat_count(const lv_anim_t * a); /** * Get the user_data field of the animation * @param a pointer to an initialized `lv_anim_t` variable * @return the pointer to the custom user_data of the animation */ -#if LV_USE_USER_DATA -static inline void * lv_anim_get_user_data(lv_anim_t * a) -{ - return a->user_data; -} -#endif +void * lv_anim_get_user_data(const lv_anim_t * a); /** - * Delete an animation of a variable with a given animator function + * Delete animation(s) of a variable with a given animator function * @param var pointer to variable * @param exec_cb a function pointer which is animating 'var', * or NULL to ignore it and delete all the animations of 'var * @return true: at least 1 animation is deleted, false: no animation is deleted */ -bool lv_anim_del(void * var, lv_anim_exec_xcb_t exec_cb); +bool lv_anim_delete(void * var, lv_anim_exec_xcb_t exec_cb); /** * Delete all the animations */ -void lv_anim_del_all(void); +void lv_anim_delete_all(void); /** * Get the animation of a variable and its `exec_cb`. @@ -364,7 +383,7 @@ lv_anim_t * lv_anim_get(void * var, lv_anim_exec_xcb_t exec_cb); * Get global animation refresher timer. * @return pointer to the animation refresher timer. */ -struct _lv_timer_t * lv_anim_get_timer(void); +lv_timer_t * lv_anim_get_timer(void); /** * Delete an animation by getting the animated variable from `a`. @@ -377,10 +396,7 @@ struct _lv_timer_t * lv_anim_get_timer(void); * or NULL to ignore it and delete all the animations of 'var * @return true: at least 1 animation is deleted, false: no animation is deleted */ -static inline bool lv_anim_custom_del(lv_anim_t * a, lv_anim_custom_exec_cb_t exec_cb) -{ - return lv_anim_del(a ? a->var : NULL, (lv_anim_exec_xcb_t)exec_cb); -} +bool lv_anim_custom_delete(lv_anim_t * a, lv_anim_custom_exec_cb_t exec_cb); /** * Get the animation of a variable and its `exec_cb`. @@ -391,10 +407,7 @@ static inline bool lv_anim_custom_del(lv_anim_t * a, lv_anim_custom_exec_cb_t ex * @param exec_cb a function pointer which is animating 'var', or NULL to return first matching 'var' * @return pointer to the animation. */ -static inline lv_anim_t * lv_anim_custom_get(lv_anim_t * a, lv_anim_custom_exec_cb_t exec_cb) -{ - return lv_anim_get(a ? a->var : NULL, (lv_anim_exec_xcb_t)exec_cb); -} +lv_anim_t * lv_anim_custom_get(lv_anim_t * a, lv_anim_custom_exec_cb_t exec_cb); /** * Get the number of currently running animations @@ -403,11 +416,32 @@ static inline lv_anim_t * lv_anim_custom_get(lv_anim_t * a, lv_anim_custom_exec_ uint16_t lv_anim_count_running(void); /** - * Calculate the time of an animation with a given speed and the start and end values - * @param speed speed of animation in unit/sec - * @param start start value of the animation - * @param end end value of the animation - * @return the required time [ms] for the animation with the given parameters + * Store the speed as a special value which can be used as time in animations. + * It will be converted to time internally based on the start and end values + * @param speed the speed of the animation in with unit / sec resolution in 0..10k range + * @return a special value which can be used as an animation time + */ +uint32_t lv_anim_speed(uint32_t speed); + +/** + * Store the speed as a special value which can be used as time in animations. + * It will be converted to time internally based on the start and end values + * @param speed the speed of the animation in as unit / sec resolution in 0..10k range + * @param min_time the minimum time in 0..10k range + * @param max_time the maximum time in 0..10k range + * @return a special value in where all three values are stored and can be used as an animation time + * @note internally speed is stored as 10 unit/sec + * @note internally min/max_time are stored with 10 ms unit + * + */ +uint32_t lv_anim_speed_clamped(uint32_t speed, uint32_t min_time, uint32_t max_time); + +/** + * Calculate the time of an animation based on its speed, start and end values. + * @param speed the speed of the animation + * @param start the start value + * @param end the end value + * @return the time of the animation in milliseconds */ uint32_t lv_anim_speed_to_time(uint32_t speed, int32_t start, int32_t end); @@ -469,6 +503,13 @@ int32_t lv_anim_path_bounce(const lv_anim_t * a); */ int32_t lv_anim_path_step(const lv_anim_t * a); +/** + * A custom cubic bezier animation path, need to specify cubic-parameters in a->parameter.bezier3 + * @param a pointer to an animation + * @return the current value to set + */ +int32_t lv_anim_path_custom_bezier3(const lv_anim_t * a); + /********************** * GLOBAL VARIABLES **********************/ diff --git a/include/liblvgl/misc/lv_anim_private.h b/include/liblvgl/misc/lv_anim_private.h new file mode 100644 index 00000000..5c258501 --- /dev/null +++ b/include/liblvgl/misc/lv_anim_private.h @@ -0,0 +1,56 @@ +/** + * @file lv_anim_private.h + * + */ + +#ifndef LV_ANIM_PRIVATE_H +#define LV_ANIM_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_anim.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef struct { + bool anim_list_changed; + bool anim_run_round; + lv_timer_t * timer; + lv_ll_t anim_ll; +} lv_anim_state_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Init the animation module + */ +void lv_anim_core_init(void); + +/** + * Deinit the animation module + */ +void lv_anim_core_deinit(void); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_ANIM_PRIVATE_H*/ diff --git a/include/liblvgl/misc/lv_anim_timeline.h b/include/liblvgl/misc/lv_anim_timeline.h index d4dd0fcf..0e9651d5 100644 --- a/include/liblvgl/misc/lv_anim_timeline.h +++ b/include/liblvgl/misc/lv_anim_timeline.h @@ -19,12 +19,12 @@ extern "C" { * DEFINES *********************/ +#define LV_ANIM_TIMELINE_PROGRESS_MAX 0xFFFF + /********************** * TYPEDEFS **********************/ -struct _lv_anim_timeline_t; - typedef struct _lv_anim_timeline_t lv_anim_timeline_t; /********************** @@ -41,7 +41,7 @@ lv_anim_timeline_t * lv_anim_timeline_create(void); * Delete animation timeline. * @param at pointer to the animation timeline. */ -void lv_anim_timeline_del(lv_anim_timeline_t * at); +void lv_anim_timeline_delete(lv_anim_timeline_t * at); /** * Add animation to the animation timeline. @@ -49,7 +49,7 @@ void lv_anim_timeline_del(lv_anim_timeline_t * at); * @param start_time the time the animation started on the timeline, note that start_time will override the value of delay. * @param a pointer to an animation. */ -void lv_anim_timeline_add(lv_anim_timeline_t * at, uint32_t start_time, lv_anim_t * a); +void lv_anim_timeline_add(lv_anim_timeline_t * at, uint32_t start_time, const lv_anim_t * a); /** * Start the animation timeline. @@ -59,10 +59,10 @@ void lv_anim_timeline_add(lv_anim_timeline_t * at, uint32_t start_time, lv_anim_ uint32_t lv_anim_timeline_start(lv_anim_timeline_t * at); /** - * Stop the animation timeline. + * Pause the animation timeline. * @param at pointer to the animation timeline. */ -void lv_anim_timeline_stop(lv_anim_timeline_t * at); +void lv_anim_timeline_pause(lv_anim_timeline_t * at); /** * Set the playback direction of the animation timeline. @@ -71,6 +71,20 @@ void lv_anim_timeline_stop(lv_anim_timeline_t * at); */ void lv_anim_timeline_set_reverse(lv_anim_timeline_t * at, bool reverse); +/** + * Make the animation timeline repeat itself. + * @param at pointer to the animation timeline. + * @param cnt repeat count or `LV_ANIM_REPEAT_INFINITE` for infinite repetition. 0: to disable repetition. + */ +void lv_anim_timeline_set_repeat_count(lv_anim_timeline_t * at, uint32_t cnt); + +/** + * Set a delay before repeating the animation timeline. + * @param at pointer to the animation timeline. + * @param delay delay in milliseconds before repeating the animation timeline. + */ +void lv_anim_timeline_set_repeat_delay(lv_anim_timeline_t * at, uint32_t delay); + /** * Set the progress of the animation timeline. * @param at pointer to the animation timeline. @@ -92,6 +106,25 @@ uint32_t lv_anim_timeline_get_playtime(lv_anim_timeline_t * at); */ bool lv_anim_timeline_get_reverse(lv_anim_timeline_t * at); +/** + * Get the progress of the animation timeline. + * @param at pointer to the animation timeline. + * @return return value 0~65535 to map 0~100% animation progress. + */ +uint16_t lv_anim_timeline_get_progress(lv_anim_timeline_t * at); + +/** + * Get repeat count of the animation timeline. + * @param at pointer to the animation timeline. + */ +uint32_t lv_anim_timeline_get_repeat_count(lv_anim_timeline_t * at); + +/** + * Get repeat delay of the animation timeline. + * @param at pointer to the animation timeline. + */ +uint32_t lv_anim_timeline_get_repeat_delay(lv_anim_timeline_t * at); + /********************** * MACROS **********************/ diff --git a/include/liblvgl/misc/lv_area.h b/include/liblvgl/misc/lv_area.h index 036767b8..0798c74f 100644 --- a/include/liblvgl/misc/lv_area.h +++ b/include/liblvgl/misc/lv_area.h @@ -13,20 +13,14 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" -#include -#include +#include "../lv_conf_internal.h" +#include "lv_types.h" +#include "lv_math.h" /********************* * DEFINES *********************/ -#if LV_USE_LARGE_COORD -typedef int32_t lv_coord_t; -#else -typedef int16_t lv_coord_t; -#endif - /********************** * TYPEDEFS **********************/ @@ -35,20 +29,26 @@ typedef int16_t lv_coord_t; * Represents a point on the screen. */ typedef struct { - lv_coord_t x; - lv_coord_t y; + int32_t x; + int32_t y; } lv_point_t; +typedef struct { + lv_value_precise_t x; + lv_value_precise_t y; +} lv_point_precise_t; + /** Represents an area of the screen.*/ typedef struct { - lv_coord_t x1; - lv_coord_t y1; - lv_coord_t x2; - lv_coord_t y2; + int32_t x1; + int32_t y1; + int32_t x2; + int32_t y2; } lv_area_t; /** Alignments*/ -enum { + +typedef enum { LV_ALIGN_DEFAULT = 0, LV_ALIGN_TOP_LEFT, LV_ALIGN_TOP_MID, @@ -72,10 +72,9 @@ enum { LV_ALIGN_OUT_RIGHT_TOP, LV_ALIGN_OUT_RIGHT_MID, LV_ALIGN_OUT_RIGHT_BOTTOM, -}; -typedef uint8_t lv_align_t; +} lv_align_t; -enum { +typedef enum { LV_DIR_NONE = 0x00, LV_DIR_LEFT = (1 << 0), LV_DIR_RIGHT = (1 << 1), @@ -84,9 +83,7 @@ enum { LV_DIR_HOR = LV_DIR_LEFT | LV_DIR_RIGHT, LV_DIR_VER = LV_DIR_TOP | LV_DIR_BOTTOM, LV_DIR_ALL = LV_DIR_HOR | LV_DIR_VER, -}; - -typedef uint8_t lv_dir_t; +} lv_dir_t; /********************** * GLOBAL PROTOTYPES @@ -100,7 +97,7 @@ typedef uint8_t lv_dir_t; * @param x2 right coordinate of the area * @param y2 bottom coordinate of the area */ -void lv_area_set(lv_area_t * area_p, lv_coord_t x1, lv_coord_t y1, lv_coord_t x2, lv_coord_t y2); +void lv_area_set(lv_area_t * area_p, int32_t x1, int32_t y1, int32_t x2, int32_t y2); /** * Copy an area @@ -120,42 +117,28 @@ inline static void lv_area_copy(lv_area_t * dest, const lv_area_t * src) * @param area_p pointer to an area * @return the width of the area (if x1 == x2 -> width = 1) */ -static inline lv_coord_t lv_area_get_width(const lv_area_t * area_p) -{ - return (lv_coord_t)(area_p->x2 - area_p->x1 + 1); -} +int32_t lv_area_get_width(const lv_area_t * area_p); /** * Get the height of an area * @param area_p pointer to an area * @return the height of the area (if y1 == y2 -> height = 1) */ -static inline lv_coord_t lv_area_get_height(const lv_area_t * area_p) -{ - return (lv_coord_t)(area_p->y2 - area_p->y1 + 1); -} +int32_t lv_area_get_height(const lv_area_t * area_p); /** * Set the width of an area * @param area_p pointer to an area * @param w the new width of the area (w == 1 makes x1 == x2) */ -void lv_area_set_width(lv_area_t * area_p, lv_coord_t w); +void lv_area_set_width(lv_area_t * area_p, int32_t w); /** * Set the height of an area * @param area_p pointer to an area * @param h the new height of the area (h == 1 makes y1 == y2) */ -void lv_area_set_height(lv_area_t * area_p, lv_coord_t h); - -/** - * Set the position of an area (width and height will be kept) - * @param area_p pointer to an area - * @param x the new x coordinate of the area - * @param y the new y coordinate of the area - */ -void _lv_area_set_pos(lv_area_t * area_p, lv_coord_t x, lv_coord_t y); +void lv_area_set_height(lv_area_t * area_p, int32_t h); /** * Return with area of an area (x * y) @@ -164,129 +147,105 @@ void _lv_area_set_pos(lv_area_t * area_p, lv_coord_t x, lv_coord_t y); */ uint32_t lv_area_get_size(const lv_area_t * area_p); -void lv_area_increase(lv_area_t * area, lv_coord_t w_extra, lv_coord_t h_extra); +void lv_area_increase(lv_area_t * area, int32_t w_extra, int32_t h_extra); -void lv_area_move(lv_area_t * area, lv_coord_t x_ofs, lv_coord_t y_ofs); +void lv_area_move(lv_area_t * area, int32_t x_ofs, int32_t y_ofs); /** - * Get the common parts of two areas - * @param res_p pointer to an area, the result will be stored her - * @param a1_p pointer to the first area - * @param a2_p pointer to the second area - * @return false: the two area has NO common parts, res_p is invalid + * Align an area to another + * @param base an area where the other will be aligned + * @param to_align the area to align + * @param align `LV_ALIGN_...` + * @param ofs_x X offset + * @param ofs_y Y offset */ -bool _lv_area_intersect(lv_area_t * res_p, const lv_area_t * a1_p, const lv_area_t * a2_p); +void lv_area_align(const lv_area_t * base, lv_area_t * to_align, lv_align_t align, int32_t ofs_x, int32_t ofs_y); /** - * Join two areas into a third which involves the other two - * @param res_p pointer to an area, the result will be stored here - * @param a1_p pointer to the first area - * @param a2_p pointer to the second area + * Transform a point + * @param point pointer to a point + * @param angle angle with 0.1 resolutions (123 means 12.3°) + * @param scale_x horizontal zoom, 256 means 100% + * @param scale_y vertical zoom, 256 means 100% + * @param pivot pointer to the pivot point of the transformation + * @param zoom_first true: zoom first and rotate after that; else: opposite order */ -void _lv_area_join(lv_area_t * a_res_p, const lv_area_t * a1_p, const lv_area_t * a2_p); +void lv_point_transform(lv_point_t * point, int32_t angle, int32_t scale_x, int32_t scale_y, const lv_point_t * pivot, + bool zoom_first); /** - * Check if a point is on an area - * @param a_p pointer to an area - * @param p_p pointer to a point - * @param radius radius of area (e.g. for rounded rectangle) - * @return false:the point is out of the area + * Transform an array of points + * @param points pointer to an array of points + * @param count number of points in the array + * @param angle angle with 0.1 resolutions (123 means 12.3°) + * @param scale_x horizontal zoom, 256 means 100% + * @param scale_y vertical zoom, 256 means 100% + * @param pivot pointer to the pivot point of the transformation + * @param zoom_first true: zoom first and rotate after that; else: opposite order */ -bool _lv_area_is_point_on(const lv_area_t * a_p, const lv_point_t * p_p, lv_coord_t radius); +void lv_point_array_transform(lv_point_t * points, size_t count, int32_t angle, int32_t scale_x, int32_t scale_y, + const lv_point_t * pivot, + bool zoom_first); -/** - * Check if two area has common parts - * @param a1_p pointer to an area. - * @param a2_p pointer to an other area - * @return false: a1_p and a2_p has no common parts - */ -bool _lv_area_is_on(const lv_area_t * a1_p, const lv_area_t * a2_p); +lv_point_t lv_point_from_precise(const lv_point_precise_t * p); -/** - * Check if an area is fully on an other - * @param ain_p pointer to an area which could be in 'aholder_p' - * @param aholder_p pointer to an area which could involve 'ain_p' - * @param radius radius of `aholder_p` (e.g. for rounded rectangle) - * @return true: `ain_p` is fully inside `aholder_p` - */ -bool _lv_area_is_in(const lv_area_t * ain_p, const lv_area_t * aholder_p, lv_coord_t radius); +lv_point_precise_t lv_point_to_precise(const lv_point_t * p); +void lv_point_set(lv_point_t * p, int32_t x, int32_t y); -/** - * Check if an area is fully out of an other - * @param aout_p pointer to an area which could be in 'aholder_p' - * @param aholder_p pointer to an area which could involve 'ain_p' - * @param radius radius of `aholder_p` (e.g. for rounded rectangle) - * @return true: `aout_p` is fully outside `aholder_p` - */ -bool _lv_area_is_out(const lv_area_t * aout_p, const lv_area_t * aholder_p, lv_coord_t radius); +void lv_point_precise_set(lv_point_precise_t * p, lv_value_precise_t x, lv_value_precise_t y); -/** - * Check if 2 area is the same - * @param a pointer to an area - * @param b pointer to another area - */ -bool _lv_area_is_equal(const lv_area_t * a, const lv_area_t * b); - -/** - * Align an area to an other - * @param base an are where the other will be aligned - * @param to_align the area to align - * @param align `LV_ALIGN_...` - */ -void lv_area_align(const lv_area_t * base, lv_area_t * to_align, lv_align_t align, lv_coord_t ofs_x, lv_coord_t ofs_y); +void lv_point_swap(lv_point_t * p1, lv_point_t * p2); -void lv_point_transform(lv_point_t * p, int32_t angle, int32_t zoom, const lv_point_t * pivot); +void lv_point_precise_swap(lv_point_precise_t * p1, lv_point_precise_t * p2); /********************** * MACROS **********************/ -#if LV_USE_LARGE_COORD -#define _LV_COORD_TYPE_SHIFT (29U) -#else -#define _LV_COORD_TYPE_SHIFT (13U) -#endif - -#define _LV_COORD_TYPE_MASK (3 << _LV_COORD_TYPE_SHIFT) -#define _LV_COORD_TYPE(x) ((x) & _LV_COORD_TYPE_MASK) /*Extract type specifiers*/ -#define _LV_COORD_PLAIN(x) ((x) & ~_LV_COORD_TYPE_MASK) /*Remove type specifiers*/ +#define LV_COORD_TYPE_SHIFT (29U) -#define _LV_COORD_TYPE_PX (0 << _LV_COORD_TYPE_SHIFT) -#define _LV_COORD_TYPE_SPEC (1 << _LV_COORD_TYPE_SHIFT) -#define _LV_COORD_TYPE_PX_NEG (3 << _LV_COORD_TYPE_SHIFT) +#define LV_COORD_TYPE_MASK (3 << LV_COORD_TYPE_SHIFT) +#define LV_COORD_TYPE(x) ((x) & LV_COORD_TYPE_MASK) /*Extract type specifiers*/ +#define LV_COORD_PLAIN(x) ((x) & ~LV_COORD_TYPE_MASK) /*Remove type specifiers*/ -#define LV_COORD_IS_PX(x) (_LV_COORD_TYPE(x) == _LV_COORD_TYPE_PX || \ - _LV_COORD_TYPE(x) == _LV_COORD_TYPE_PX_NEG ? true : false) -#define LV_COORD_IS_SPEC(x) (_LV_COORD_TYPE(x) == _LV_COORD_TYPE_SPEC ? true : false) +#define LV_COORD_TYPE_PX (0 << LV_COORD_TYPE_SHIFT) +#define LV_COORD_TYPE_SPEC (1 << LV_COORD_TYPE_SHIFT) +#define LV_COORD_TYPE_PX_NEG (3 << LV_COORD_TYPE_SHIFT) -#define LV_COORD_SET_SPEC(x) ((x) | _LV_COORD_TYPE_SPEC) +#define LV_COORD_IS_PX(x) (LV_COORD_TYPE(x) == LV_COORD_TYPE_PX || LV_COORD_TYPE(x) == LV_COORD_TYPE_PX_NEG) +#define LV_COORD_IS_SPEC(x) (LV_COORD_TYPE(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) ? true : false) -#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); +#define LV_COORD_SET_SPEC(x) ((x) | LV_COORD_TYPE_SPEC) -/*Max coordinate value*/ -#define LV_COORD_MAX ((1 << _LV_COORD_TYPE_SHIFT) - 1) +/** Max coordinate value */ +#define LV_COORD_MAX ((1 << LV_COORD_TYPE_SHIFT) - 1) #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_MIN); +LV_EXPORT_CONST_INT(LV_SIZE_CONTENT); /** - * Convert a percentage value to `lv_coord_t`. + * Convert a percentage value to `int32_t`. * Percentage values are stored in special range * @param x the percentage (0..1000) * @return a coordinate that stores the percentage */ -static inline lv_coord_t lv_pct(lv_coord_t x) -{ - return LV_PCT(x); -} +int32_t lv_pct(int32_t x); + +int32_t lv_pct_to_px(int32_t v, int32_t base); #ifdef __cplusplus } /*extern "C"*/ diff --git a/include/liblvgl/misc/lv_area_private.h b/include/liblvgl/misc/lv_area_private.h new file mode 100644 index 00000000..bebca594 --- /dev/null +++ b/include/liblvgl/misc/lv_area_private.h @@ -0,0 +1,115 @@ +/** + * @file lv_area_private.h + * + */ + +#ifndef LV_AREA_PRIVATE_H +#define LV_AREA_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_area.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Set the position of an area (width and height will be kept) + * @param area_p pointer to an area + * @param x the new x coordinate of the area + * @param y the new y coordinate of the area + */ +void lv_area_set_pos(lv_area_t * area_p, int32_t x, int32_t y); + +/** + * Get the common parts of two areas + * @param res_p pointer to an area, the result will be stored her + * @param a1_p pointer to the first area + * @param a2_p pointer to the second area + * @return false: the two area has NO common parts, res_p is invalid + */ +bool lv_area_intersect(lv_area_t * res_p, const lv_area_t * a1_p, const lv_area_t * a2_p); + +/** + * Get resulting sub areas after removing the common parts of two areas from the first area + * @param res_p pointer to an array of areas with a count of 4, the resulting areas will be stored here + * @param a1_p pointer to the first area + * @param a2_p pointer to the second area + * @return number of results (max 4) or -1 if no intersect + */ +int8_t lv_area_diff(lv_area_t res_p[], const lv_area_t * a1_p, const lv_area_t * a2_p); + +/** + * Join two areas into a third which involves the other two + * @param a_res_p pointer to an area, the result will be stored here + * @param a1_p pointer to the first area + * @param a2_p pointer to the second area + */ +void lv_area_join(lv_area_t * a_res_p, const lv_area_t * a1_p, const lv_area_t * a2_p); + +/** + * Check if a point is on an area + * @param a_p pointer to an area + * @param p_p pointer to a point + * @param radius radius of area (e.g. for rounded rectangle) + * @return false:the point is out of the area + */ +bool lv_area_is_point_on(const lv_area_t * a_p, const lv_point_t * p_p, int32_t radius); + +/** + * Check if two area has common parts + * @param a1_p pointer to an area. + * @param a2_p pointer to another area + * @return false: a1_p and a2_p has no common parts + */ +bool lv_area_is_on(const lv_area_t * a1_p, const lv_area_t * a2_p); + +/** + * Check if an area is fully on another + * @param ain_p pointer to an area which could be in 'aholder_p' + * @param aholder_p pointer to an area which could involve 'ain_p' + * @param radius radius of `aholder_p` (e.g. for rounded rectangle) + * @return true: `ain_p` is fully inside `aholder_p` + */ +bool lv_area_is_in(const lv_area_t * ain_p, const lv_area_t * aholder_p, int32_t radius); + +/** + * Check if an area is fully out of another + * @param aout_p pointer to an area which could be in 'aholder_p' + * @param aholder_p pointer to an area which could involve 'ain_p' + * @param radius radius of `aholder_p` (e.g. for rounded rectangle) + * @return true: `aout_p` is fully outside `aholder_p` + */ +bool lv_area_is_out(const lv_area_t * aout_p, const lv_area_t * aholder_p, int32_t radius); + +/** + * Check if 2 area is the same + * @param a pointer to an area + * @param b pointer to another area + */ +bool lv_area_is_equal(const lv_area_t * a, const lv_area_t * b); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_AREA_PRIVATE_H*/ diff --git a/include/liblvgl/misc/lv_array.h b/include/liblvgl/misc/lv_array.h new file mode 100644 index 00000000..78f7ac3d --- /dev/null +++ b/include/liblvgl/misc/lv_array.h @@ -0,0 +1,205 @@ +/** + * @file lv_array.h + * Array. The elements are dynamically allocated by the 'lv_mem' module. + */ + +#ifndef LV_ARRAY_H +#define LV_ARRAY_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "lv_types.h" + +/********************* + * DEFINES + *********************/ + +#ifndef LV_ARRAY_DEFAULT_CAPACITY +#define LV_ARRAY_DEFAULT_CAPACITY 4 +#endif + +#ifndef LV_ARRAY_DEFAULT_SHRINK_RATIO +#define LV_ARRAY_DEFAULT_SHRINK_RATIO 2 +#endif + +/********************** + * TYPEDEFS + **********************/ + +/** Description of a array*/ +struct _lv_array_t { + uint8_t * data; + uint32_t size; + uint32_t capacity; + uint32_t element_size; + + bool inner_alloc; /* true: data is allocated by the array; false: data is allocated by the user */ +}; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Init an array. + * @param array pointer to an `lv_array_t` variable to initialize + * @param capacity the initial capacity of the array + * @param element_size the size of an element in bytes + */ +void lv_array_init(lv_array_t * array, uint32_t capacity, uint32_t element_size); + +/** + * Init an array from a buffer. + * @note The buffer must be large enough to store `capacity` elements. The array will not release the buffer and reallocate it. + * The user must ensure that the buffer is valid during the lifetime of the array. And release the buffer when the array is no longer needed. + * @param array pointer to an `lv_array_t` variable to initialize + * @param buf pointer to a buffer to use as the array's data + * @param capacity the initial capacity of the array + * @param element_size the size of an element in bytes + */ +void lv_array_init_from_buf(lv_array_t * array, void * buf, uint32_t capacity, uint32_t element_size); + +/** + * Resize the array to the given capacity. + * @note if the new capacity is smaller than the current size, the array will be truncated. + * @param array pointer to an `lv_array_t` variable + * @param new_capacity the new capacity of the array + */ +bool lv_array_resize(lv_array_t * array, uint32_t new_capacity); + +/** + * Deinit the array, and free the allocated memory + * @param array pointer to an `lv_array_t` variable to deinitialize + */ +void lv_array_deinit(lv_array_t * array); + +/** + * Return how many elements are stored in the array. + * @param array pointer to an `lv_array_t` variable + * @return the number of elements stored in the array + */ +uint32_t lv_array_size(const lv_array_t * array); + +/** + * Return the capacity of the array, i.e. how many elements can be stored. + * @param array pointer to an `lv_array_t` variable + * @return the capacity of the array + */ +uint32_t lv_array_capacity(const lv_array_t * array); + +/** + * Return if the array is empty + * @param array pointer to an `lv_array_t` variable + * @return true: array is empty; false: array is not empty + */ +bool lv_array_is_empty(const lv_array_t * array); + +/** + * Return if the array is full + * @param array pointer to an `lv_array_t` variable + * @return true: array is full; false: array is not full + */ +bool lv_array_is_full(const lv_array_t * array); + +/** + * Copy an array to another. + * @note this will create a new array with the same capacity and size as the source array. + * @param target pointer to an `lv_array_t` variable to copy to + * @param source pointer to an `lv_array_t` variable to copy from + */ +void lv_array_copy(lv_array_t * target, const lv_array_t * source); + +/** + * Remove all elements in array. + * @param array pointer to an `lv_array_t` variable + */ +void lv_array_clear(lv_array_t * array); + +/** + * Shrink the memory capacity of array if necessary. + * @param array pointer to an `lv_array_t` variable + */ +void lv_array_shrink(lv_array_t * array); + +/** + * Remove the element at the specified position in the array. + * @param array pointer to an `lv_array_t` variable + * @param index the index of the element to remove + * @return LV_RESULT_OK: success, otherwise: error + */ +lv_result_t lv_array_remove(lv_array_t * array, uint32_t index); + +/** + * Remove from the array either a single element or a range of elements ([start, end)). + * @note This effectively reduces the container size by the number of elements removed. + * @note When start equals to end, the function has no effect. + * @param array pointer to an `lv_array_t` variable + * @param start the index of the first element to be removed + * @param end the index of the first element that is not to be removed + * @return LV_RESULT_OK: success, otherwise: error + */ +lv_result_t lv_array_erase(lv_array_t * array, uint32_t start, uint32_t end); + +/** + * Concatenate two arrays. Adds new elements to the end of the array. + * @note The destination array is automatically expanded as necessary. + * @param array pointer to an `lv_array_t` variable + * @param other pointer to the array to concatenate + * @return LV_RESULT_OK: success, otherwise: error + */ +lv_result_t lv_array_concat(lv_array_t * array, const lv_array_t * other); + +/** + * Push back element. Adds a new element to the end of the array. + * If the array capacity is not enough for the new element, the array will be resized automatically. + * @note If the element is NULL, it will be added as an empty element. + * @param array pointer to an `lv_array_t` variable + * @param element pointer to the element to add. NULL to push an empty element. + * @return LV_RESULT_OK: success, otherwise: error + */ +lv_result_t lv_array_push_back(lv_array_t * array, const void * element); + +/** + * Assigns one content to the array, replacing its current content. + * @param array pointer to an `lv_array_t` variable + * @param index the index of the element to replace + * @param value pointer to the elements to add + * @return true: success; false: error + */ +lv_result_t lv_array_assign(lv_array_t * array, uint32_t index, const void * value); + +/** + * Returns a pointer to the element at position n in the array. + * @param array pointer to an `lv_array_t` variable + * @param index the index of the element to return + * @return a pointer to the requested element, NULL if `index` is out of range + */ +void * lv_array_at(const lv_array_t * array, uint32_t index); + +/** + * Returns a pointer to the first element in the array. + * @param array pointer to an `lv_array_t` variable + * @return a pointer to the first element in the array + */ +void * lv_array_front(const lv_array_t * array); + +/** + * Returns a pointer to the last element in the array. + * @param array pointer to an `lv_array_t` variable + */ +void * lv_array_back(const lv_array_t * array); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif diff --git a/include/liblvgl/misc/lv_assert.h b/include/liblvgl/misc/lv_assert.h index 4625297d..c9dc849c 100644 --- a/include/liblvgl/misc/lv_assert.h +++ b/include/liblvgl/misc/lv_assert.h @@ -13,9 +13,9 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" +#include "../lv_conf_internal.h" #include "lv_log.h" -#include "lv_mem.h" +#include "../stdlib/lv_mem.h" #include LV_ASSERT_HANDLER_INCLUDE /********************* @@ -50,6 +50,14 @@ extern "C" { } \ } while(0) +#define LV_ASSERT_FORMAT_MSG(expr, format, ...) \ + do { \ + if(!(expr)) { \ + LV_LOG_ERROR("Asserted at expression: %s " format , #expr, __VA_ARGS__); \ + LV_ASSERT_HANDLER \ + } \ + } while(0) + /*----------------- * ASSERTS *-----------------*/ @@ -67,7 +75,7 @@ extern "C" { #endif #if LV_USE_ASSERT_MEM_INTEGRITY -# define LV_ASSERT_MEM_INTEGRITY() LV_ASSERT_MSG(lv_mem_test() == LV_RES_OK, "Memory integrity error"); +# define LV_ASSERT_MEM_INTEGRITY() LV_ASSERT_MSG(lv_mem_test() == LV_RESULT_OK, "Memory integrity error"); #else # define LV_ASSERT_MEM_INTEGRITY() #endif diff --git a/include/liblvgl/misc/lv_async.h b/include/liblvgl/misc/lv_async.h index 4ad5756d..362a0ffa 100644 --- a/include/liblvgl/misc/lv_async.h +++ b/include/liblvgl/misc/lv_async.h @@ -41,14 +41,14 @@ typedef void (*lv_async_cb_t)(void *); * the `func_name(object, callback, ...)` convention) * @param user_data custom parameter */ -lv_res_t lv_async_call(lv_async_cb_t async_xcb, void * user_data); +lv_result_t lv_async_call(lv_async_cb_t async_xcb, void * user_data); /** * Cancel an asynchronous function call * @param async_xcb a callback which is the task itself. * @param user_data custom parameter */ -lv_res_t lv_async_call_cancel(lv_async_cb_t async_xcb, void * user_data); +lv_result_t lv_async_call_cancel(lv_async_cb_t async_xcb, void * user_data); /********************** * MACROS diff --git a/include/liblvgl/misc/lv_bidi.h b/include/liblvgl/misc/lv_bidi.h index fafb4a28..77575efb 100644 --- a/include/liblvgl/misc/lv_bidi.h +++ b/include/liblvgl/misc/lv_bidi.h @@ -13,98 +13,35 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" - -#include -#include -#include "lv_txt.h" +#include "../lv_conf_internal.h" +#include "lv_types.h" +#include "lv_text.h" /********************* * DEFINES *********************/ -/*Special non printable strong characters. - *They can be inserted to texts to affect the run's direction*/ +/** Special non printable strong characters. + * They can be inserted to texts to affect the run's direction */ #define LV_BIDI_LRO "\xE2\x80\xAD" /*U+202D*/ #define LV_BIDI_RLO "\xE2\x80\xAE" /*U+202E*/ /********************** * TYPEDEFS **********************/ -enum { +typedef enum { LV_BASE_DIR_LTR = 0x00, LV_BASE_DIR_RTL = 0x01, LV_BASE_DIR_AUTO = 0x02, LV_BASE_DIR_NEUTRAL = 0x20, LV_BASE_DIR_WEAK = 0x21, -}; - -typedef uint8_t lv_base_dir_t; +} lv_base_dir_t; /********************** * GLOBAL PROTOTYPES **********************/ #if LV_USE_BIDI -/** - * Convert a text to get the characters in the correct visual order according to - * Unicode Bidirectional Algorithm - * @param str_in the text to process - * @param str_out store the result here. Has the be `strlen(str_in)` length - * @param base_dir `LV_BASE_DIR_LTR` or `LV_BASE_DIR_RTL` - */ -void _lv_bidi_process(const char * str_in, char * str_out, lv_base_dir_t base_dir); - -/** - * Auto-detect the direction of a text based on the first strong character - * @param txt the text to process - * @return `LV_BASE_DIR_LTR` or `LV_BASE_DIR_RTL` - */ -lv_base_dir_t _lv_bidi_detect_base_dir(const char * txt); - -/** - * Get the logical position of a character in a line - * @param str_in the input string. Can be only one line. - * @param bidi_txt internally the text is bidi processed which buffer can be get here. - * If not required anymore has to freed with `lv_mem_free()` - * Can be `NULL` is unused - * @param len length of the line in character count - * @param base_dir base direction of the text: `LV_BASE_DIR_LTR` or `LV_BASE_DIR_RTL` - * @param visual_pos the visual character position which logical position should be get - * @param is_rtl tell the char at `visual_pos` is RTL or LTR context - * @return the logical character position - */ -uint16_t _lv_bidi_get_logical_pos(const char * str_in, char ** bidi_txt, uint32_t len, lv_base_dir_t base_dir, - uint32_t visual_pos, bool * is_rtl); - -/** - * Get the visual position of a character in a line - * @param str_in the input string. Can be only one line. - * @param bidi_txt internally the text is bidi processed which buffer can be get here. - * If not required anymore has to freed with `lv_mem_free()` - * Can be `NULL` is unused - * @param len length of the line in character count - * @param base_dir base direction of the text: `LV_BASE_DIR_LTR` or `LV_BASE_DIR_RTL` - * @param logical_pos the logical character position which visual position should be get - * @param is_rtl tell the char at `logical_pos` is RTL or LTR context - * @return the visual character position - */ -uint16_t _lv_bidi_get_visual_pos(const char * str_in, char ** bidi_txt, uint16_t len, lv_base_dir_t base_dir, - uint32_t logical_pos, bool * is_rtl); - -/** - * Bidi process a paragraph of text - * @param str_in the string to process - * @param str_out store the result here - * @param len length of the text - * @param base_dir base dir of the text - * @param pos_conv_out an `uint16_t` array to store the related logical position of the character. - * Can be `NULL` is unused - * @param pos_conv_len length of `pos_conv_out` in element count - */ -void _lv_bidi_process_paragraph(const char * str_in, char * str_out, uint32_t len, lv_base_dir_t base_dir, - uint16_t * pos_conv_out, uint16_t pos_conv_len); - /** * Get the real text alignment from the a text alignment, base direction and a text. * @param align LV_TEXT_ALIGN_..., write back the calculated align here (LV_TEXT_ALIGN_LEFT/RIGHT/CENTER) @@ -113,6 +50,11 @@ void _lv_bidi_process_paragraph(const char * str_in, char * str_out, uint32_t le */ void lv_bidi_calculate_align(lv_text_align_t * align, lv_base_dir_t * base_dir, const char * txt); +/** + * Set custom neutrals string + * @param neutrals default " \t\n\r.,:;'\"`!?%/\\-=()[]{}<>@#&$|" + */ +void lv_bidi_set_custom_neutrals_static(const char * neutrals); /********************** * MACROS diff --git a/include/liblvgl/misc/lv_bidi_private.h b/include/liblvgl/misc/lv_bidi_private.h new file mode 100644 index 00000000..0b684dae --- /dev/null +++ b/include/liblvgl/misc/lv_bidi_private.h @@ -0,0 +1,101 @@ +/** + * @file lv_bidi_private.h + * + */ + +#ifndef LV_BIDI_PRIVATE_H +#define LV_BIDI_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_bidi.h" +#if LV_USE_BIDI + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Convert a text to get the characters in the correct visual order according to + * Unicode Bidirectional Algorithm + * @param str_in the text to process + * @param str_out store the result here. Has the be `strlen(str_in)` length + * @param base_dir `LV_BASE_DIR_LTR` or `LV_BASE_DIR_RTL` + */ +void lv_bidi_process(const char * str_in, char * str_out, lv_base_dir_t base_dir); + +/** + * Auto-detect the direction of a text based on the first strong character + * @param txt the text to process + * @return `LV_BASE_DIR_LTR` or `LV_BASE_DIR_RTL` + */ +lv_base_dir_t lv_bidi_detect_base_dir(const char * txt); + +/** + * Get the logical position of a character in a line + * @param str_in the input string. Can be only one line. + * @param bidi_txt internally the text is bidi processed which buffer can be get here. + * If not required anymore has to freed with `lv_free()` + * Can be `NULL` is unused + * @param len length of the line in character count + * @param base_dir base direction of the text: `LV_BASE_DIR_LTR` or `LV_BASE_DIR_RTL` + * @param visual_pos the visual character position which logical position should be get + * @param is_rtl tell the char at `visual_pos` is RTL or LTR context + * @return the logical character position + */ +uint16_t lv_bidi_get_logical_pos(const char * str_in, char ** bidi_txt, uint32_t len, lv_base_dir_t base_dir, + uint32_t visual_pos, bool * is_rtl); + +/** + * Get the visual position of a character in a line + * @param str_in the input string. Can be only one line. + * @param bidi_txt internally the text is bidi processed which buffer can be get here. + * If not required anymore has to freed with `lv_free()` + * Can be `NULL` is unused + * @param len length of the line in character count + * @param base_dir base direction of the text: `LV_BASE_DIR_LTR` or `LV_BASE_DIR_RTL` + * @param logical_pos the logical character position which visual position should be get + * @param is_rtl tell the char at `logical_pos` is RTL or LTR context + * @return the visual character position + */ +uint16_t lv_bidi_get_visual_pos(const char * str_in, char ** bidi_txt, uint16_t len, lv_base_dir_t base_dir, + uint32_t logical_pos, bool * is_rtl); + +/** + * Bidi process a paragraph of text + * @param str_in the string to process + * @param str_out store the result here + * @param len length of the text + * @param base_dir base dir of the text + * @param pos_conv_out an `uint16_t` array to store the related logical position of the character. + * Can be `NULL` is unused + * @param pos_conv_len length of `pos_conv_out` in element count + */ +void lv_bidi_process_paragraph(const char * str_in, char * str_out, uint32_t len, lv_base_dir_t base_dir, + uint16_t * pos_conv_out, uint16_t pos_conv_len); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_BIDI*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_BIDI_PRIVATE_H*/ diff --git a/include/liblvgl/misc/lv_circle_buf.h b/include/liblvgl/misc/lv_circle_buf.h new file mode 100644 index 00000000..61490008 --- /dev/null +++ b/include/liblvgl/misc/lv_circle_buf.h @@ -0,0 +1,191 @@ +/** +* @file lv_circle_buf.h +* + */ + + +#ifndef LV_CIRCLE_BUF_H +#define LV_CIRCLE_BUF_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_types.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef bool (*lv_circle_buf_fill_cb_t)(void * buf, uint32_t buff_len, int32_t index, void * user_data); + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create a circle buffer + * @param capacity the maximum number of elements in the buffer + * @param element_size the size of an element in bytes + * @return pointer to the created buffer + */ +lv_circle_buf_t * lv_circle_buf_create(uint32_t capacity, uint32_t element_size); + +/** + * Create a circle buffer from an existing buffer + * @param buf pointer to a buffer + * @param capacity the maximum number of elements in the buffer + * @param element_size the size of an element in bytes + * @return pointer to the created buffer + */ +lv_circle_buf_t * lv_circle_buf_create_from_buf(void * buf, uint32_t capacity, uint32_t element_size); + +/** + * Create a circle buffer from an existing array + * @param array pointer to an array + * @return pointer to the created buffer + */ +lv_circle_buf_t * lv_circle_buf_create_from_array(const lv_array_t * array); + +/** + * Resize the buffer + * @param circle_buf pointer to a buffer + * @param capacity the new capacity of the buffer + * @return LV_RESULT_OK: the buffer is resized; LV_RESULT_INVALID: the buffer is not resized + */ +lv_result_t lv_circle_buf_resize(lv_circle_buf_t * circle_buf, uint32_t capacity); + +/** + * Destroy a circle buffer + * @param circle_buf pointer to buffer + */ +void lv_circle_buf_destroy(lv_circle_buf_t * circle_buf); + +/** + * Get the size of the buffer + * @param circle_buf pointer to buffer + * @return the number of elements in the buffer + */ +uint32_t lv_circle_buf_size(const lv_circle_buf_t * circle_buf); + +/** + * Get the capacity of the buffer + * @param circle_buf pointer to buffer + * @return the maximum number of elements in the buffer + */ +uint32_t lv_circle_buf_capacity(const lv_circle_buf_t * circle_buf); + +/** + * Get the remaining space in the buffer + * @param circle_buf pointer to buffer + * @return the number of elements that can be written to the buffer + */ +uint32_t lv_circle_buf_remain(const lv_circle_buf_t * circle_buf); + +/** + * Check if the buffer is empty + * @param circle_buf pointer to buffer + * @return true: the buffer is empty; false: the buffer is not empty + */ +bool lv_circle_buf_is_empty(const lv_circle_buf_t * circle_buf); + +/** + * Check if the buffer is full + * @param circle_buf pointer to buffer + * @return true: the buffer is full; false: the buffer is not full + */ +bool lv_circle_buf_is_full(const lv_circle_buf_t * circle_buf); + +/** + * Reset the buffer + * @param circle_buf pointer to buffer + * @return LV_RESULT_OK: the buffer is reset; LV_RESULT_INVALID: the buffer is not reset + */ +void lv_circle_buf_reset(lv_circle_buf_t * circle_buf); + +/** + * Get the head of the buffer + * @param circle_buf pointer to buffer + * @return pointer to the head of the buffer + */ +void * lv_circle_buf_head(const lv_circle_buf_t * circle_buf); + +/** + * Get the tail of the buffer + * @param circle_buf pointer to buffer + * @return pointer to the tail of the buffer + */ +void * lv_circle_buf_tail(const lv_circle_buf_t * circle_buf); + +/** + * Read a value + * @param circle_buf pointer to buffer + * @param data pointer to a variable to store the read value + * @return LV_RESULT_OK: the value is read; LV_RESULT_INVALID: the value is not read + */ +lv_result_t lv_circle_buf_read(lv_circle_buf_t * circle_buf, void * data); + +/** + * Write a value + * @param circle_buf pointer to buffer + * @param data pointer to the value to write + * @return LV_RESULT_OK: the value is written; LV_RESULT_INVALID: the value is not written + */ +lv_result_t lv_circle_buf_write(lv_circle_buf_t * circle_buf, const void * data); + +/** + * Fill the buffer with values + * @param circle_buf pointer to buffer + * @param count the number of values to fill + * @param fill_cb the callback function to fill the buffer + * @param user_data + * @return the number of values filled + */ +uint32_t lv_circle_buf_fill(lv_circle_buf_t * circle_buf, uint32_t count, lv_circle_buf_fill_cb_t fill_cb, + void * user_data); + +/** + * Skip a value + * @param circle_buf pointer to buffer + * @return LV_RESULT_OK: the value is skipped; LV_RESULT_INVALID: the value is not skipped + */ +lv_result_t lv_circle_buf_skip(lv_circle_buf_t * circle_buf); + +/** + * Peek a value + * @param circle_buf pointer to buffer + * @param data pointer to a variable to store the peeked value + * @return LV_RESULT_OK: the value is peeked; LV_RESULT_INVALID: the value is not peeked + */ +lv_result_t lv_circle_buf_peek(const lv_circle_buf_t * circle_buf, void * data); + +/** + * Peek a value at an index + * @param circle_buf pointer to buffer + * @param index the index of the value to peek, if the index is greater than the size of the buffer, it will return looply. + * @param data pointer to a variable to store the peeked value + * @return LV_RESULT_OK: the value is peeked; LV_RESULT_INVALID: the value is not peeked + */ +lv_result_t lv_circle_buf_peek_at(const lv_circle_buf_t * circle_buf, uint32_t index, void * data); + +/************************* + * GLOBAL VARIABLES + *************************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_CIRCLE_BUF_H*/ diff --git a/include/liblvgl/misc/lv_color.h b/include/liblvgl/misc/lv_color.h index 8e80a616..d5222400 100644 --- a/include/liblvgl/misc/lv_color.h +++ b/include/liblvgl/misc/lv_color.h @@ -13,31 +13,30 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" +#include "../lv_conf_internal.h" #include "lv_assert.h" #include "lv_math.h" #include "lv_types.h" -/*Error checking*/ -#if LV_COLOR_DEPTH == 24 -#error "LV_COLOR_DEPTH 24 is deprecated. Use LV_COLOR_DEPTH 32 instead (lv_conf.h)" -#endif - -#if LV_COLOR_DEPTH != 16 && LV_COLOR_16_SWAP != 0 -#error "LV_COLOR_16_SWAP requires LV_COLOR_DEPTH == 16. Set it in lv_conf.h" -#endif - -#include - /********************* * DEFINES *********************/ LV_EXPORT_CONST_INT(LV_COLOR_DEPTH); -LV_EXPORT_CONST_INT(LV_COLOR_16_SWAP); + +#if LV_COLOR_DEPTH == 8 +#define LV_COLOR_NATIVE_WITH_ALPHA_SIZE 2 +#elif LV_COLOR_DEPTH == 16 +#define LV_COLOR_NATIVE_WITH_ALPHA_SIZE 3 +#elif LV_COLOR_DEPTH == 24 +#define LV_COLOR_NATIVE_WITH_ALPHA_SIZE 4 +#elif LV_COLOR_DEPTH == 32 +#define LV_COLOR_NATIVE_WITH_ALPHA_SIZE 4 +#endif /** * Opacity percentages. */ + enum { LV_OPA_TRANSP = 0, LV_OPA_0 = 0, @@ -54,598 +53,295 @@ enum { LV_OPA_COVER = 255, }; -#define LV_OPA_MIN 2 /*Opacities below this will be transparent*/ -#define LV_OPA_MAX 253 /*Opacities above this will fully cover*/ +#define LV_OPA_MIN 2 /**< Opacities below this will be transparent */ +#define LV_OPA_MAX 253 /**< Opacities above this will fully cover */ -#if LV_COLOR_DEPTH == 1 -#define LV_COLOR_SIZE 8 -#elif LV_COLOR_DEPTH == 8 -#define LV_COLOR_SIZE 8 -#elif LV_COLOR_DEPTH == 16 -#define LV_COLOR_SIZE 16 -#elif LV_COLOR_DEPTH == 32 -#define LV_COLOR_SIZE 32 -#else -#error "Invalid LV_COLOR_DEPTH in lv_conf.h! Set it to 1, 8, 16 or 32!" -#endif - -#if defined(__cplusplus) && !defined(_LV_COLOR_HAS_MODERN_CPP) /** -* MSVC compiler's definition of the __cplusplus indicating 199711L regardless to C++ standard version -* see https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-cplusplus -* so we use _MSC_VER macro instead of __cplusplus -*/ -#ifdef _MSC_VER -#if _MSC_VER >= 1900 /*Visual Studio 2015*/ -#define _LV_COLOR_HAS_MODERN_CPP 1 -#endif -#else -#if __cplusplus >= 201103L -#define _LV_COLOR_HAS_MODERN_CPP 1 -#endif -#endif -#endif /*__cplusplus*/ - -#ifndef _LV_COLOR_HAS_MODERN_CPP -#define _LV_COLOR_HAS_MODERN_CPP 0 -#endif - -#if _LV_COLOR_HAS_MODERN_CPP -/*Fix msvc compiler error C4576 inside C++ code*/ -#define _LV_COLOR_MAKE_TYPE_HELPER lv_color_t -#else -#define _LV_COLOR_MAKE_TYPE_HELPER (lv_color_t) -#endif - -/*--------------------------------------- - * Macros for all existing color depths - * to set/get values of the color channels - *------------------------------------------*/ -# define LV_COLOR_SET_R1(c, v) (c).ch.red = (uint8_t)((v) & 0x1) -# define LV_COLOR_SET_G1(c, v) (c).ch.green = (uint8_t)((v) & 0x1) -# define LV_COLOR_SET_B1(c, v) (c).ch.blue = (uint8_t)((v) & 0x1) -# define LV_COLOR_SET_A1(c, v) do {} while(0) - -# define LV_COLOR_GET_R1(c) (c).ch.red -# define LV_COLOR_GET_G1(c) (c).ch.green -# define LV_COLOR_GET_B1(c) (c).ch.blue -# define LV_COLOR_GET_A1(c) 0xFF - -# define _LV_COLOR_ZERO_INITIALIZER1 {0x00} -# define LV_COLOR_MAKE1(r8, g8, b8) {(uint8_t)((b8 >> 7) | (g8 >> 7) | (r8 >> 7))} - -# define LV_COLOR_SET_R8(c, v) (c).ch.red = (uint8_t)((v) & 0x7U) -# define LV_COLOR_SET_G8(c, v) (c).ch.green = (uint8_t)((v) & 0x7U) -# define LV_COLOR_SET_B8(c, v) (c).ch.blue = (uint8_t)((v) & 0x3U) -# define LV_COLOR_SET_A8(c, v) do {} while(0) - -# define LV_COLOR_GET_R8(c) (c).ch.red -# define LV_COLOR_GET_G8(c) (c).ch.green -# define LV_COLOR_GET_B8(c) (c).ch.blue -# define LV_COLOR_GET_A8(c) 0xFF - -# define _LV_COLOR_ZERO_INITIALIZER8 {{0x00, 0x00, 0x00}} -# define LV_COLOR_MAKE8(r8, g8, b8) {{(uint8_t)((b8 >> 6) & 0x3U), (uint8_t)((g8 >> 5) & 0x7U), (uint8_t)((r8 >> 5) & 0x7U)}} - -# define LV_COLOR_SET_R16(c, v) (c).ch.red = (uint8_t)((v) & 0x1FU) -#if LV_COLOR_16_SWAP == 0 -# define LV_COLOR_SET_G16(c, v) (c).ch.green = (uint8_t)((v) & 0x3FU) -#else -# define LV_COLOR_SET_G16(c, v) {(c).ch.green_h = (uint8_t)(((v) >> 3) & 0x7); (c).ch.green_l = (uint8_t)((v) & 0x7);} -#endif -# define LV_COLOR_SET_B16(c, v) (c).ch.blue = (uint8_t)((v) & 0x1FU) -# define LV_COLOR_SET_A16(c, v) do {} while(0) - -# define LV_COLOR_GET_R16(c) (c).ch.red -#if LV_COLOR_16_SWAP == 0 -# define LV_COLOR_GET_G16(c) (c).ch.green -#else -# define LV_COLOR_GET_G16(c) (((c).ch.green_h << 3) + (c).ch.green_l) -#endif -# define LV_COLOR_GET_B16(c) (c).ch.blue -# define LV_COLOR_GET_A16(c) 0xFF - -#if LV_COLOR_16_SWAP == 0 -# define _LV_COLOR_ZERO_INITIALIZER16 {{0x00, 0x00, 0x00}} -# define LV_COLOR_MAKE16(r8, g8, b8) {{(uint8_t)((b8 >> 3) & 0x1FU), (uint8_t)((g8 >> 2) & 0x3FU), (uint8_t)((r8 >> 3) & 0x1FU)}} -#else -# define _LV_COLOR_ZERO_INITIALIZER16 {{0x00, 0x00, 0x00, 0x00}} -# define LV_COLOR_MAKE16(r8, g8, b8) {{(uint8_t)((g8 >> 5) & 0x7U), (uint8_t)((r8 >> 3) & 0x1FU), (uint8_t)((b8 >> 3) & 0x1FU), (uint8_t)((g8 >> 2) & 0x7U)}} -#endif - -# define LV_COLOR_SET_R32(c, v) (c).ch.red = (uint8_t)((v) & 0xFF) -# define LV_COLOR_SET_G32(c, v) (c).ch.green = (uint8_t)((v) & 0xFF) -# define LV_COLOR_SET_B32(c, v) (c).ch.blue = (uint8_t)((v) & 0xFF) -# define LV_COLOR_SET_A32(c, v) (c).ch.alpha = (uint8_t)((v) & 0xFF) - -# define LV_COLOR_GET_R32(c) (c).ch.red -# define LV_COLOR_GET_G32(c) (c).ch.green -# define LV_COLOR_GET_B32(c) (c).ch.blue -# define LV_COLOR_GET_A32(c) (c).ch.alpha - -# define _LV_COLOR_ZERO_INITIALIZER32 {{0x00, 0x00, 0x00, 0x00}} -# define LV_COLOR_MAKE32(r8, g8, b8) {{b8, g8, r8, 0xff}} /*Fix 0xff alpha*/ - -/*--------------------------------------- - * Macros for the current color depth - * to set/get values of the color channels - *------------------------------------------*/ -#define LV_COLOR_SET_R(c, v) LV_CONCAT(LV_COLOR_SET_R, LV_COLOR_DEPTH)(c, v) -#define LV_COLOR_SET_G(c, v) LV_CONCAT(LV_COLOR_SET_G, LV_COLOR_DEPTH)(c, v) -#define LV_COLOR_SET_B(c, v) LV_CONCAT(LV_COLOR_SET_B, LV_COLOR_DEPTH)(c, v) -#define LV_COLOR_SET_A(c, v) LV_CONCAT(LV_COLOR_SET_A, LV_COLOR_DEPTH)(c, v) - -#define LV_COLOR_GET_R(c) LV_CONCAT(LV_COLOR_GET_R, LV_COLOR_DEPTH)(c) -#define LV_COLOR_GET_G(c) LV_CONCAT(LV_COLOR_GET_G, LV_COLOR_DEPTH)(c) -#define LV_COLOR_GET_B(c) LV_CONCAT(LV_COLOR_GET_B, LV_COLOR_DEPTH)(c) -#define LV_COLOR_GET_A(c) LV_CONCAT(LV_COLOR_GET_A, LV_COLOR_DEPTH)(c) + * Get the pixel size of a color format in bits, bpp + * @param cf a color format (`LV_COLOR_FORMAT_...`) + * @return the pixel size in bits + * @sa lv_color_format_get_bpp + */ +#define LV_COLOR_FORMAT_GET_BPP(cf) ( \ + (cf) == LV_COLOR_FORMAT_I1 ? 1 : \ + (cf) == LV_COLOR_FORMAT_A1 ? 1 : \ + (cf) == LV_COLOR_FORMAT_I2 ? 2 : \ + (cf) == LV_COLOR_FORMAT_A2 ? 2 : \ + (cf) == LV_COLOR_FORMAT_I4 ? 4 : \ + (cf) == LV_COLOR_FORMAT_A4 ? 4 : \ + (cf) == LV_COLOR_FORMAT_L8 ? 8 : \ + (cf) == LV_COLOR_FORMAT_A8 ? 8 : \ + (cf) == LV_COLOR_FORMAT_I8 ? 8 : \ + (cf) == LV_COLOR_FORMAT_AL88 ? 16 : \ + (cf) == LV_COLOR_FORMAT_RGB565 ? 16 : \ + (cf) == LV_COLOR_FORMAT_RGB565A8 ? 16 : \ + (cf) == LV_COLOR_FORMAT_YUY2 ? 16 : \ + (cf) == LV_COLOR_FORMAT_ARGB8565 ? 24 : \ + (cf) == LV_COLOR_FORMAT_RGB888 ? 24 : \ + (cf) == LV_COLOR_FORMAT_ARGB8888 ? 32 : \ + (cf) == LV_COLOR_FORMAT_XRGB8888 ? 32 : \ + 0 \ + ) -#define _LV_COLOR_ZERO_INITIALIZER LV_CONCAT(_LV_COLOR_ZERO_INITIALIZER, LV_COLOR_DEPTH) -#define LV_COLOR_MAKE(r8, g8, b8) LV_CONCAT(LV_COLOR_MAKE, LV_COLOR_DEPTH)(r8, g8, b8) +/** + * Get the pixel size of a color format in bytes + * @param cf a color format (`LV_COLOR_FORMAT_...`) + * @return the pixel size in bytes + * @sa lv_color_format_get_size + */ +#define LV_COLOR_FORMAT_GET_SIZE(cf) ((LV_COLOR_FORMAT_GET_BPP(cf) + 7) >> 3) /********************** * TYPEDEFS **********************/ -typedef union { - uint8_t full; /*must be declared first to set all bits of byte via initializer list*/ - union { - uint8_t blue : 1; - uint8_t green : 1; - uint8_t red : 1; - } ch; -} lv_color1_t; - -typedef union { - struct { - uint8_t blue : 2; - uint8_t green : 3; - uint8_t red : 3; - } ch; - uint8_t full; -} lv_color8_t; - -typedef union { - struct { -#if LV_COLOR_16_SWAP == 0 - uint16_t blue : 5; - uint16_t green : 6; - uint16_t red : 5; -#else - uint16_t green_h : 3; - uint16_t red : 5; - uint16_t blue : 5; - uint16_t green_l : 3; -#endif - } ch; - uint16_t full; +typedef struct { + uint8_t blue; + uint8_t green; + uint8_t red; +} lv_color_t; + +typedef struct { + uint16_t blue : 5; + uint16_t green : 6; + uint16_t red : 5; } lv_color16_t; -typedef union { - struct { - uint8_t blue; - uint8_t green; - uint8_t red; - uint8_t alpha; - } ch; - uint32_t full; +typedef struct { + uint8_t blue; + uint8_t green; + uint8_t red; + uint8_t alpha; } lv_color32_t; -typedef LV_CONCAT3(uint, LV_COLOR_SIZE, _t) lv_color_int_t; -typedef LV_CONCAT3(lv_color, LV_COLOR_DEPTH, _t) lv_color_t; - typedef struct { uint16_t h; uint8_t s; uint8_t v; } lv_color_hsv_t; -//! @cond Doxygen_Suppress -/*No idea where the guard is required but else throws warnings in the docs*/ -typedef uint8_t lv_opa_t; -//! @endcond +typedef struct { + uint8_t lumi; + uint8_t alpha; +} lv_color16a_t; + +typedef enum { + LV_COLOR_FORMAT_UNKNOWN = 0, + + LV_COLOR_FORMAT_RAW = 0x01, + LV_COLOR_FORMAT_RAW_ALPHA = 0x02, + + /*<=1 byte (+alpha) formats*/ + LV_COLOR_FORMAT_L8 = 0x06, + LV_COLOR_FORMAT_I1 = 0x07, + LV_COLOR_FORMAT_I2 = 0x08, + LV_COLOR_FORMAT_I4 = 0x09, + LV_COLOR_FORMAT_I8 = 0x0A, + LV_COLOR_FORMAT_A8 = 0x0E, + + /*2 byte (+alpha) formats*/ + LV_COLOR_FORMAT_RGB565 = 0x12, + LV_COLOR_FORMAT_ARGB8565 = 0x13, /**< Not supported by sw renderer yet. */ + LV_COLOR_FORMAT_RGB565A8 = 0x14, /**< Color array followed by Alpha array*/ + LV_COLOR_FORMAT_AL88 = 0x15, /**< L8 with alpha >*/ + + /*3 byte (+alpha) formats*/ + LV_COLOR_FORMAT_RGB888 = 0x0F, + LV_COLOR_FORMAT_ARGB8888 = 0x10, + LV_COLOR_FORMAT_XRGB8888 = 0x11, + + /*Formats not supported by software renderer but kept here so GPU can use it*/ + LV_COLOR_FORMAT_A1 = 0x0B, + LV_COLOR_FORMAT_A2 = 0x0C, + LV_COLOR_FORMAT_A4 = 0x0D, + + /* reference to https://wiki.videolan.org/YUV/ */ + /*YUV planar formats*/ + LV_COLOR_FORMAT_YUV_START = 0x20, + LV_COLOR_FORMAT_I420 = LV_COLOR_FORMAT_YUV_START, /*YUV420 planar(3 plane)*/ + LV_COLOR_FORMAT_I422 = 0x21, /*YUV422 planar(3 plane)*/ + LV_COLOR_FORMAT_I444 = 0x22, /*YUV444 planar(3 plane)*/ + LV_COLOR_FORMAT_I400 = 0x23, /*YUV400 no chroma channel*/ + LV_COLOR_FORMAT_NV21 = 0x24, /*YUV420 planar(2 plane), UV plane in 'V, U, V, U'*/ + LV_COLOR_FORMAT_NV12 = 0x25, /*YUV420 planar(2 plane), UV plane in 'U, V, U, V'*/ + + /*YUV packed formats*/ + LV_COLOR_FORMAT_YUY2 = 0x26, /*YUV422 packed like 'Y U Y V'*/ + LV_COLOR_FORMAT_UYVY = 0x27, /*YUV422 packed like 'U Y V Y'*/ + + LV_COLOR_FORMAT_YUV_END = LV_COLOR_FORMAT_UYVY, + + /*Color formats in which LVGL can render*/ +#if LV_COLOR_DEPTH == 1 + LV_COLOR_FORMAT_NATIVE = LV_COLOR_FORMAT_I1, + LV_COLOR_FORMAT_NATIVE_WITH_ALPHA = LV_COLOR_FORMAT_I1, +#elif LV_COLOR_DEPTH == 8 + LV_COLOR_FORMAT_NATIVE = LV_COLOR_FORMAT_L8, + LV_COLOR_FORMAT_NATIVE_WITH_ALPHA = LV_COLOR_FORMAT_AL88, +#elif LV_COLOR_DEPTH == 16 + LV_COLOR_FORMAT_NATIVE = LV_COLOR_FORMAT_RGB565, + LV_COLOR_FORMAT_NATIVE_WITH_ALPHA = LV_COLOR_FORMAT_RGB565A8, +#elif LV_COLOR_DEPTH == 24 + LV_COLOR_FORMAT_NATIVE = LV_COLOR_FORMAT_RGB888, + LV_COLOR_FORMAT_NATIVE_WITH_ALPHA = LV_COLOR_FORMAT_ARGB8888, +#elif LV_COLOR_DEPTH == 32 + LV_COLOR_FORMAT_NATIVE = LV_COLOR_FORMAT_XRGB8888, + LV_COLOR_FORMAT_NATIVE_WITH_ALPHA = LV_COLOR_FORMAT_ARGB8888, +#else +#error "LV_COLOR_DEPTH should be 1, 8, 16, 24 or 32" +#endif -struct _lv_color_filter_dsc_t; +} lv_color_format_t; -typedef lv_color_t (*lv_color_filter_cb_t)(const struct _lv_color_filter_dsc_t *, lv_color_t, lv_opa_t); +#define LV_COLOR_FORMAT_IS_ALPHA_ONLY(cf) ((cf) >= LV_COLOR_FORMAT_A1 && (cf) <= LV_COLOR_FORMAT_A8) +#define LV_COLOR_FORMAT_IS_INDEXED(cf) ((cf) >= LV_COLOR_FORMAT_I1 && (cf) <= LV_COLOR_FORMAT_I8) +#define LV_COLOR_FORMAT_IS_YUV(cf) ((cf) >= LV_COLOR_FORMAT_YUV_START && (cf) <= LV_COLOR_FORMAT_YUV_END) +#define LV_COLOR_INDEXED_PALETTE_SIZE(cf) ((cf) == LV_COLOR_FORMAT_I1 ? 2 :\ + (cf) == LV_COLOR_FORMAT_I2 ? 4 :\ + (cf) == LV_COLOR_FORMAT_I4 ? 16 :\ + (cf) == LV_COLOR_FORMAT_I8 ? 256 : 0) -typedef struct _lv_color_filter_dsc_t { - lv_color_filter_cb_t filter_cb; - void * user_data; -} lv_color_filter_dsc_t; +/********************** + * MACROS + **********************/ +#define LV_COLOR_MAKE(r8, g8, b8) {b8, g8, r8} -typedef enum { - LV_PALETTE_RED, - LV_PALETTE_PINK, - LV_PALETTE_PURPLE, - LV_PALETTE_DEEP_PURPLE, - LV_PALETTE_INDIGO, - LV_PALETTE_BLUE, - LV_PALETTE_LIGHT_BLUE, - LV_PALETTE_CYAN, - LV_PALETTE_TEAL, - LV_PALETTE_GREEN, - LV_PALETTE_LIGHT_GREEN, - LV_PALETTE_LIME, - LV_PALETTE_YELLOW, - LV_PALETTE_AMBER, - LV_PALETTE_ORANGE, - LV_PALETTE_DEEP_ORANGE, - LV_PALETTE_BROWN, - LV_PALETTE_BLUE_GREY, - LV_PALETTE_GREY, - _LV_PALETTE_LAST, - LV_PALETTE_NONE = 0xff, -} lv_palette_t; +#define LV_OPA_MIX2(a1, a2) (((int32_t)(a1) * (a2)) >> 8) +#define LV_OPA_MIX3(a1, a2, a3) (((int32_t)(a1) * (a2) * (a3)) >> 16) /********************** * GLOBAL PROTOTYPES **********************/ -/*In color conversations: - * - When converting to bigger color type the LSB weight of 1 LSB is calculated - * E.g. 16 bit Red has 5 bits - * 8 bit Red has 3 bits - * ---------------------- - * 8 bit red LSB = (2^5 - 1) / (2^3 - 1) = 31 / 7 = 4 - * - * - When calculating to smaller color type simply shift out the LSBs - * E.g. 8 bit Red has 3 bits - * 16 bit Red has 5 bits - * ---------------------- - * Shift right with 5 - 3 = 2 +/** + * Get the pixel size of a color format in bits, bpp + * @param cf a color format (`LV_COLOR_FORMAT_...`) + * @return the pixel size in bits + * @sa LV_COLOR_FORMAT_GET_BPP */ -static inline uint8_t lv_color_to1(lv_color_t color) -{ -#if LV_COLOR_DEPTH == 1 - return color.full; -#elif LV_COLOR_DEPTH == 8 - if((LV_COLOR_GET_R(color) & 0x4) || (LV_COLOR_GET_G(color) & 0x4) || (LV_COLOR_GET_B(color) & 0x2)) { - return 1; - } - else { - return 0; - } -#elif LV_COLOR_DEPTH == 16 - if((LV_COLOR_GET_R(color) & 0x10) || (LV_COLOR_GET_G(color) & 0x20) || (LV_COLOR_GET_B(color) & 0x10)) { - return 1; - } - else { - return 0; - } -#elif LV_COLOR_DEPTH == 32 - if((LV_COLOR_GET_R(color) & 0x80) || (LV_COLOR_GET_G(color) & 0x80) || (LV_COLOR_GET_B(color) & 0x80)) { - return 1; - } - else { - return 0; - } -#endif -} - -static inline uint8_t lv_color_to8(lv_color_t color) -{ -#if LV_COLOR_DEPTH == 1 - if(color.full == 0) - return 0; - else - return 0xFF; -#elif LV_COLOR_DEPTH == 8 - return color.full; -#elif LV_COLOR_DEPTH == 16 - lv_color8_t ret; - LV_COLOR_SET_R8(ret, LV_COLOR_GET_R(color) >> 2); /*5 - 3 = 2*/ - LV_COLOR_SET_G8(ret, LV_COLOR_GET_G(color) >> 3); /*6 - 3 = 3*/ - LV_COLOR_SET_B8(ret, LV_COLOR_GET_B(color) >> 3); /*5 - 2 = 3*/ - return ret.full; -#elif LV_COLOR_DEPTH == 32 - lv_color8_t ret; - LV_COLOR_SET_R8(ret, LV_COLOR_GET_R(color) >> 5); /*8 - 3 = 5*/ - LV_COLOR_SET_G8(ret, LV_COLOR_GET_G(color) >> 5); /*8 - 3 = 5*/ - LV_COLOR_SET_B8(ret, LV_COLOR_GET_B(color) >> 6); /*8 - 2 = 6*/ - return ret.full; -#endif -} - -static inline uint16_t lv_color_to16(lv_color_t color) -{ -#if LV_COLOR_DEPTH == 1 - if(color.full == 0) - return 0; - else - return 0xFFFF; -#elif LV_COLOR_DEPTH == 8 - lv_color16_t ret; - LV_COLOR_SET_R16(ret, LV_COLOR_GET_R(color) * 4); /*(2^5 - 1)/(2^3 - 1) = 31/7 = 4*/ - LV_COLOR_SET_G16(ret, LV_COLOR_GET_G(color) * 9); /*(2^6 - 1)/(2^3 - 1) = 63/7 = 9*/ - LV_COLOR_SET_B16(ret, LV_COLOR_GET_B(color) * 10); /*(2^5 - 1)/(2^2 - 1) = 31/3 = 10*/ - return ret.full; -#elif LV_COLOR_DEPTH == 16 - return color.full; -#elif LV_COLOR_DEPTH == 32 - lv_color16_t ret; - LV_COLOR_SET_R16(ret, LV_COLOR_GET_R(color) >> 3); /*8 - 5 = 3*/ - LV_COLOR_SET_G16(ret, LV_COLOR_GET_G(color) >> 2); /*8 - 6 = 2*/ - LV_COLOR_SET_B16(ret, LV_COLOR_GET_B(color) >> 3); /*8 - 5 = 3*/ - return ret.full; -#endif -} +uint8_t lv_color_format_get_bpp(lv_color_format_t cf); -static inline uint32_t lv_color_to32(lv_color_t color) -{ -#if LV_COLOR_DEPTH == 1 - if(color.full == 0) - return 0xFF000000; - else - return 0xFFFFFFFF; -#elif LV_COLOR_DEPTH == 8 - lv_color32_t ret; - LV_COLOR_SET_R32(ret, LV_COLOR_GET_R(color) * 36); /*(2^8 - 1)/(2^3 - 1) = 255/7 = 36*/ - LV_COLOR_SET_G32(ret, LV_COLOR_GET_G(color) * 36); /*(2^8 - 1)/(2^3 - 1) = 255/7 = 36*/ - LV_COLOR_SET_B32(ret, LV_COLOR_GET_B(color) * 85); /*(2^8 - 1)/(2^2 - 1) = 255/3 = 85*/ - LV_COLOR_SET_A32(ret, 0xFF); - return ret.full; -#elif LV_COLOR_DEPTH == 16 - /** - * The floating point math for conversion is: - * valueto = valuefrom * ( (2^bitsto - 1) / (float)(2^bitsfrom - 1) ) - * The faster integer math for conversion is: - * valueto = ( valuefrom * multiplier + adder ) >> divisor - * multiplier = FLOOR( ( (2^bitsto - 1) << divisor ) / (float)(2^bitsfrom - 1) ) - * - * Find the first divisor where ( adder >> divisor ) <= 0 - * - * 5-bit to 8-bit: ( 31 * multiplier + adder ) >> divisor = 255 - * divisor multiplier adder min (0) max (31) - * 0 8 7 7 255 - * 1 16 14 7 255 - * 2 32 28 7 255 - * 3 65 25 3 255 - * 4 131 19 1 255 - * 5 263 7 0 255 - * - * 6-bit to 8-bit: 255 = ( 63 * multiplier + adder ) >> divisor - * divisor multiplier adder min (0) max (63) - * 0 4 3 3 255 - * 1 8 6 3 255 - * 2 16 12 3 255 - * 3 32 24 3 255 - * 4 64 48 3 255 - * 5 129 33 1 255 - * 6 259 3 0 255 - */ - - lv_color32_t ret; - LV_COLOR_SET_R32(ret, (LV_COLOR_GET_R(color) * 263 + 7) >> 5); - LV_COLOR_SET_G32(ret, (LV_COLOR_GET_G(color) * 259 + 3) >> 6); - LV_COLOR_SET_B32(ret, (LV_COLOR_GET_B(color) * 263 + 7) >> 5); - LV_COLOR_SET_A32(ret, 0xFF); - return ret.full; -#elif LV_COLOR_DEPTH == 32 - return color.full; -#endif -} +/** + * Get the pixel size of a color format in bytes + * @param cf a color format (`LV_COLOR_FORMAT_...`) + * @return the pixel size in bytes + * @sa LV_COLOR_FORMAT_GET_SIZE + */ +uint8_t lv_color_format_get_size(lv_color_format_t cf); -//! @cond Doxygen_Suppress +/** + * Check if a color format has alpha channel or not + * @param src_cf a color format (`LV_COLOR_FORMAT_...`) + * @return true: has alpha channel; false: doesn't have alpha channel + */ +bool lv_color_format_has_alpha(lv_color_format_t src_cf); /** - * Mix two colors with a given ratio. - * @param c1 the first color to mix (usually the foreground) - * @param c2 the second color to mix (usually the background) - * @param mix The ratio of the colors. 0: full `c2`, 255: full `c1`, 127: half `c1` and half`c2` - * @return the mixed color + * Create an ARGB8888 color from RGB888 + alpha + * @param color an RGB888 color + * @param opa the alpha value + * @return the ARGB8888 color */ -LV_ATTRIBUTE_FAST_MEM static inline lv_color_t lv_color_mix(lv_color_t c1, lv_color_t c2, uint8_t mix) -{ - lv_color_t ret; - -#if LV_COLOR_DEPTH == 16 && LV_COLOR_16_SWAP == 0 && LV_COLOR_MIX_ROUND_OFS == 0 - /*Source: https://stackoverflow.com/a/50012418/1999969*/ - mix = (uint32_t)((uint32_t)mix + 4) >> 3; - uint32_t bg = (uint32_t)((uint32_t)c2.full | ((uint32_t)c2.full << 16)) & - 0x7E0F81F; /*0b00000111111000001111100000011111*/ - uint32_t fg = (uint32_t)((uint32_t)c1.full | ((uint32_t)c1.full << 16)) & 0x7E0F81F; - uint32_t result = ((((fg - bg) * mix) >> 5) + bg) & 0x7E0F81F; - ret.full = (uint16_t)((result >> 16) | result); -#elif LV_COLOR_DEPTH != 1 - /*LV_COLOR_DEPTH == 8, 16 or 32*/ - LV_COLOR_SET_R(ret, LV_UDIV255((uint16_t)LV_COLOR_GET_R(c1) * mix + LV_COLOR_GET_R(c2) * - (255 - mix) + LV_COLOR_MIX_ROUND_OFS)); - LV_COLOR_SET_G(ret, LV_UDIV255((uint16_t)LV_COLOR_GET_G(c1) * mix + LV_COLOR_GET_G(c2) * - (255 - mix) + LV_COLOR_MIX_ROUND_OFS)); - LV_COLOR_SET_B(ret, LV_UDIV255((uint16_t)LV_COLOR_GET_B(c1) * mix + LV_COLOR_GET_B(c2) * - (255 - mix) + LV_COLOR_MIX_ROUND_OFS)); - LV_COLOR_SET_A(ret, 0xFF); -#else - /*LV_COLOR_DEPTH == 1*/ - ret.full = mix > LV_OPA_50 ? c1.full : c2.full; -#endif +lv_color32_t lv_color_to_32(lv_color_t color, lv_opa_t opa); - return ret; -} +/** + * Convert an RGB888 color to an integer + * @param c an RGB888 color + * @return `c` as an integer + */ +uint32_t lv_color_to_int(lv_color_t c); -LV_ATTRIBUTE_FAST_MEM static inline void lv_color_premult(lv_color_t c, uint8_t mix, uint16_t * out) -{ -#if LV_COLOR_DEPTH != 1 - out[0] = (uint16_t)LV_COLOR_GET_R(c) * mix; - out[1] = (uint16_t)LV_COLOR_GET_G(c) * mix; - out[2] = (uint16_t)LV_COLOR_GET_B(c) * mix; -#else - (void) mix; - /*Pre-multiplication can't be used with 1 bpp*/ - out[0] = LV_COLOR_GET_R(c); - out[1] = LV_COLOR_GET_G(c); - out[2] = LV_COLOR_GET_B(c); -#endif +/** + * Check if two RGB888 color are equal + * @param c1 the first color + * @param c2 the second color + * @return true: equal + */ +bool lv_color_eq(lv_color_t c1, lv_color_t c2); -} +/** + * Check if two ARGB8888 color are equal + * @param c1 the first color + * @param c2 the second color + * @return true: equal + */ +bool lv_color32_eq(lv_color32_t c1, lv_color32_t c2); /** - * Mix two colors with a given ratio. It runs faster then `lv_color_mix` but requires some pre computation. - * @param premult_c1 The first color. Should be preprocessed with `lv_color_premult(c1)` - * @param c2 The second color. As it is no pre computation required on it - * @param mix The ratio of the colors. 0: full `c1`, 255: full `c2`, 127: half `c1` and half `c2`. - * Should be modified like mix = `255 - mix` - * @return the mixed color - * @note 255 won't give clearly `c1`. + * Create a color from 0x000000..0xffffff input + * @param c the hex input + * @return the color */ -LV_ATTRIBUTE_FAST_MEM static inline lv_color_t lv_color_mix_premult(uint16_t * premult_c1, lv_color_t c2, uint8_t mix) -{ - lv_color_t ret; -#if LV_COLOR_DEPTH != 1 - /*LV_COLOR_DEPTH == 8 or 32*/ - LV_COLOR_SET_R(ret, LV_UDIV255(premult_c1[0] + LV_COLOR_GET_R(c2) * mix + LV_COLOR_MIX_ROUND_OFS)); - LV_COLOR_SET_G(ret, LV_UDIV255(premult_c1[1] + LV_COLOR_GET_G(c2) * mix + LV_COLOR_MIX_ROUND_OFS)); - LV_COLOR_SET_B(ret, LV_UDIV255(premult_c1[2] + LV_COLOR_GET_B(c2) * mix + LV_COLOR_MIX_ROUND_OFS)); - LV_COLOR_SET_A(ret, 0xFF); -#else - /*LV_COLOR_DEPTH == 1*/ - /*Restore color1*/ - lv_color_t c1; - LV_COLOR_SET_R(c1, premult_c1[0]); - LV_COLOR_SET_G(c1, premult_c1[1]); - LV_COLOR_SET_B(c1, premult_c1[2]); - ret.full = mix > LV_OPA_50 ? c2.full : c1.full; -#endif +lv_color_t lv_color_hex(uint32_t c); - return ret; -} +/** + * Create an RGB888 color + * @param r the red channel (0..255) + * @param g the green channel (0..255) + * @param b the blue channel (0..255) + * @return the color + */ +lv_color_t lv_color_make(uint8_t r, uint8_t g, uint8_t b); /** - * Mix two colors. Both color can have alpha value. - * @param bg_color background color - * @param bg_opa alpha of the background color - * @param fg_color foreground color - * @param fg_opa alpha of the foreground color - * @param res_color the result color - * @param res_opa the result opacity + * Create an ARGB8888 color + * @param r the red channel (0..255) + * @param g the green channel (0..255) + * @param b the blue channel (0..255) + * @param a the alpha channel (0..255) + * @return the color */ -LV_ATTRIBUTE_FAST_MEM static inline void lv_color_mix_with_alpha(lv_color_t bg_color, lv_opa_t bg_opa, - lv_color_t fg_color, lv_opa_t fg_opa, - lv_color_t * res_color, lv_opa_t * res_opa) -{ - /*Pick the foreground if it's fully opaque or the Background is fully transparent*/ - if(fg_opa >= LV_OPA_MAX || bg_opa <= LV_OPA_MIN) { - res_color->full = fg_color.full; - *res_opa = fg_opa; - } - /*Transparent foreground: use the Background*/ - else if(fg_opa <= LV_OPA_MIN) { - res_color->full = bg_color.full; - *res_opa = bg_opa; - } - /*Opaque background: use simple mix*/ - else if(bg_opa >= LV_OPA_MAX) { - *res_color = lv_color_mix(fg_color, bg_color, fg_opa); - *res_opa = LV_OPA_COVER; - } - /*Both colors have alpha. Expensive calculation need to be applied*/ - else { - /*Save the parameters and the result. If they will be asked again don't compute again*/ - static lv_opa_t fg_opa_save = 0; - static lv_opa_t bg_opa_save = 0; - static lv_color_t fg_color_save = _LV_COLOR_ZERO_INITIALIZER; - static lv_color_t bg_color_save = _LV_COLOR_ZERO_INITIALIZER; - static lv_color_t res_color_saved = _LV_COLOR_ZERO_INITIALIZER; - static lv_opa_t res_opa_saved = 0; - - if(fg_opa != fg_opa_save || bg_opa != bg_opa_save || fg_color.full != fg_color_save.full || - bg_color.full != bg_color_save.full) { - fg_opa_save = fg_opa; - bg_opa_save = bg_opa; - fg_color_save.full = fg_color.full; - bg_color_save.full = bg_color.full; - /*Info: - * https://en.wikipedia.org/wiki/Alpha_compositing#Analytical_derivation_of_the_over_operator*/ - res_opa_saved = 255 - ((uint16_t)((uint16_t)(255 - fg_opa) * (255 - bg_opa)) >> 8); - LV_ASSERT(res_opa_saved != 0); - lv_opa_t ratio = (uint16_t)((uint16_t)fg_opa * 255) / res_opa_saved; - res_color_saved = lv_color_mix(fg_color, bg_color, ratio); - - } - - res_color->full = res_color_saved.full; - *res_opa = res_opa_saved; - } -} - -//! @endcond +lv_color32_t lv_color32_make(uint8_t r, uint8_t g, uint8_t b, uint8_t a); /** - * Get the brightness of a color - * @param color a color - * @return the brightness [0..255] + * Create a color from 0x000..0xfff input + * @param c the hex input (e.g. 0x123 will be 0x112233) + * @return the color */ -static inline uint8_t lv_color_brightness(lv_color_t color) -{ - lv_color32_t c32; - c32.full = lv_color_to32(color); - uint16_t bright = (uint16_t)(3u * LV_COLOR_GET_R32(c32) + LV_COLOR_GET_B32(c32) + 4u * LV_COLOR_GET_G32(c32)); - return (uint8_t)(bright >> 3); -} - -static inline lv_color_t lv_color_make(uint8_t r, uint8_t g, uint8_t b) -{ - return _LV_COLOR_MAKE_TYPE_HELPER LV_COLOR_MAKE(r, g, b); -} - -static inline lv_color_t lv_color_hex(uint32_t c) -{ -#if LV_COLOR_DEPTH == 16 - lv_color_t r; -#if LV_COLOR_16_SWAP == 0 - /* Convert a 4 bytes per pixel in format ARGB32 to R5G6B5 format - naive way (by calling lv_color_make with components): - r = ((c & 0xFF0000) >> 19) - g = ((c & 0xFF00) >> 10) - b = ((c & 0xFF) >> 3) - rgb565 = (r << 11) | (g << 5) | b - That's 3 mask, 5 bitshift and 2 or operations - - A bit better: - r = ((c & 0xF80000) >> 8) - g = ((c & 0xFC00) >> 5) - b = ((c & 0xFF) >> 3) - rgb565 = r | g | b - That's 3 mask, 3 bitshifts and 2 or operations */ - r.full = (uint16_t)(((c & 0xF80000) >> 8) | ((c & 0xFC00) >> 5) | ((c & 0xFF) >> 3)); -#else - /* We want: rrrr rrrr GGGg gggg bbbb bbbb => gggb bbbb rrrr rGGG */ - r.full = (uint16_t)(((c & 0xF80000) >> 16) | ((c & 0xFC00) >> 13) | ((c & 0x1C00) << 3) | ((c & 0xF8) << 5)); -#endif - return r; -#elif LV_COLOR_DEPTH == 32 - lv_color_t r; - r.full = c | 0xFF000000; - return r; -#else /*LV_COLOR_DEPTH == 8*/ - return lv_color_make((uint8_t)((c >> 16) & 0xFF), (uint8_t)((c >> 8) & 0xFF), (uint8_t)(c & 0xFF)); -#endif -} +lv_color_t lv_color_hex3(uint32_t c); -static inline lv_color_t lv_color_hex3(uint32_t c) -{ - return lv_color_make((uint8_t)(((c >> 4) & 0xF0) | ((c >> 8) & 0xF)), (uint8_t)((c & 0xF0) | ((c & 0xF0) >> 4)), - (uint8_t)((c & 0xF) | ((c & 0xF) << 4))); -} +/** + * Convert am RGB888 color to RGB565 stored in `uint16_t` + * @param color and RGB888 color + * @return `color` as RGB565 on `uin16_t` + */ +uint16_t lv_color_to_u16(lv_color_t color); -static inline void lv_color_filter_dsc_init(lv_color_filter_dsc_t * dsc, lv_color_filter_cb_t cb) -{ - dsc->filter_cb = cb; -} +/** + * Convert am RGB888 color to XRGB8888 stored in `uint32_t` + * @param color and RGB888 color + * @return `color` as XRGB8888 on `uin32_t` (the alpha channel is always set to 0xFF) + */ +uint32_t lv_color_to_u32(lv_color_t color); -//! @cond Doxygen_Suppress -//! -LV_ATTRIBUTE_FAST_MEM void lv_color_fill(lv_color_t * buf, lv_color_t color, uint32_t px_num); +/** + * Mix two RGB565 colors + * @param c1 the first color (typically the foreground color) + * @param c2 the second color (typically the background color) + * @param mix 0..255, or LV_OPA_0/10/20... + * @return mix == 0: c2 + * mix == 255: c1 + * mix == 128: 0.5 x c1 + 0.5 x c2 + */ +uint16_t LV_ATTRIBUTE_FAST_MEM lv_color_16_16_mix(uint16_t c1, uint16_t c2, uint8_t mix); -//! @endcond +/** + * Mix white to a color + * @param c the base color + * @param lvl the intensity of white (0: no change, 255: fully white) + * @return the mixed color + */ lv_color_t lv_color_lighten(lv_color_t c, lv_opa_t lvl); +/** + * Mix black to a color + * @param c the base color + * @param lvl the intensity of black (0: no change, 255: fully black) + * @return the mixed color + */ lv_color_t lv_color_darken(lv_color_t c, lv_opa_t lvl); -lv_color_t lv_color_change_lightness(lv_color_t c, lv_opa_t lvl); - /** * Convert a HSV color to RGB * @param h hue [0..359] @@ -671,36 +367,61 @@ lv_color_hsv_t lv_color_rgb_to_hsv(uint8_t r8, uint8_t g8, uint8_t b8); */ lv_color_hsv_t lv_color_to_hsv(lv_color_t color); +/*Source: https://vuetifyjs.com/en/styles/colors/#material-colors*/ + /** - * Just a wrapper around LV_COLOR_CHROMA_KEY because it might be more convenient to use a function in some cases - * @return LV_COLOR_CHROMA_KEY + * A helper for white color + * @return a white color */ -static inline lv_color_t lv_color_chroma_key(void) -{ - return LV_COLOR_CHROMA_KEY; -} +lv_color_t lv_color_white(void); -/********************** - * PREDEFINED COLORS - **********************/ -/*Source: https://vuetifyjs.com/en/styles/colors/#material-colors*/ +/** + * A helper for black color + * @return a black color + */ +lv_color_t lv_color_black(void); + +void lv_color_premultiply(lv_color32_t * c); -lv_color_t lv_palette_main(lv_palette_t p); -static inline lv_color_t lv_color_white(void) -{ - return lv_color_make(0xff, 0xff, 0xff); -} -static inline lv_color_t lv_color_black(void) -{ - return lv_color_make(0x00, 0x0, 0x00); -} -lv_color_t lv_palette_lighten(lv_palette_t p, uint8_t lvl); -lv_color_t lv_palette_darken(lv_palette_t p, uint8_t lvl); +void lv_color16_premultiply(lv_color16_t * c, lv_opa_t a); + +/** + * Get the luminance of a color: luminance = 0.3 R + 0.59 G + 0.11 B + * @param c a color + * @return the brightness [0..255] + */ +uint8_t lv_color_luminance(lv_color_t c); + +/** + * Get the luminance of a color16: luminance = 0.3 R + 0.59 G + 0.11 B + * @param c a color + * @return the brightness [0..255] + */ +uint8_t lv_color16_luminance(const lv_color16_t c); + +/** + * Get the luminance of a color24: luminance = 0.3 R + 0.59 G + 0.11 B + * @param c a color + * @return the brightness [0..255] + */ +uint8_t lv_color24_luminance(const uint8_t * c); + +/** + * Get the luminance of a color32: luminance = 0.3 R + 0.59 G + 0.11 B + * @param c a color + * @return the brightness [0..255] + */ +uint8_t lv_color32_luminance(lv_color32_t c); /********************** * MACROS **********************/ +#include "lv_palette.h" +#include "lv_color_op.h" + +LV_ATTRIBUTE_EXTERN_DATA extern const lv_color_filter_dsc_t lv_color_filter_shade; + #ifdef __cplusplus } /*extern "C"*/ #endif diff --git a/include/liblvgl/misc/lv_color_op.h b/include/liblvgl/misc/lv_color_op.h new file mode 100644 index 00000000..72a0e696 --- /dev/null +++ b/include/liblvgl/misc/lv_color_op.h @@ -0,0 +1,82 @@ +/** + * @file lv_color_op.h + * + */ + +#ifndef LV_COLOR_OP_H +#define LV_COLOR_OP_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "lv_assert.h" +#include "lv_math.h" +#include "lv_color.h" +#include "lv_types.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_color_filter_dsc_t; + +typedef lv_color_t (*lv_color_filter_cb_t)(const struct _lv_color_filter_dsc_t *, lv_color_t, lv_opa_t); + +struct _lv_color_filter_dsc_t { + lv_color_filter_cb_t filter_cb; + void * user_data; +}; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Mix two colors with a given ratio. + * @param c1 the first color to mix (usually the foreground) + * @param c2 the second color to mix (usually the background) + * @param mix The ratio of the colors. 0: full `c2`, 255: full `c1`, 127: half `c1` and half`c2` + * @return the mixed color + */ +lv_color_t LV_ATTRIBUTE_FAST_MEM lv_color_mix(lv_color_t c1, lv_color_t c2, uint8_t mix); + +/** + * + * @param fg + * @param bg + * @return + * @note Use bg.alpha in the return value + * @note Use fg.alpha as mix ratio + */ +lv_color32_t lv_color_mix32(lv_color32_t fg, lv_color32_t bg); + +/** + * Get the brightness of a color + * @param c a color + * @return brightness in range [0..255] + */ +uint8_t lv_color_brightness(lv_color_t c); + +void lv_color_filter_dsc_init(lv_color_filter_dsc_t * dsc, lv_color_filter_cb_t cb); + +/********************** + * PREDEFINED COLORS + **********************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_COLOR_H*/ diff --git a/include/liblvgl/misc/lv_color_op_private.h b/include/liblvgl/misc/lv_color_op_private.h new file mode 100644 index 00000000..d6516ebd --- /dev/null +++ b/include/liblvgl/misc/lv_color_op_private.h @@ -0,0 +1,39 @@ +/** + * @file lv_color_op_private.h + * + */ + +#ifndef LV_COLOR_OP_PRIVATE_H +#define LV_COLOR_OP_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_color_op.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_COLOR_OP_PRIVATE_H*/ diff --git a/include/liblvgl/misc/lv_event.h b/include/liblvgl/misc/lv_event.h new file mode 100644 index 00000000..89aee8ab --- /dev/null +++ b/include/liblvgl/misc/lv_event.h @@ -0,0 +1,224 @@ +/** + * @file lv_event.h + * + */ + +#ifndef LV_EVENT_H +#define LV_EVENT_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "lv_types.h" +#include "../lv_conf_internal.h" + +#include "lv_array.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef void (*lv_event_cb_t)(lv_event_t * e); + +/** + * Type of event being sent to the object. + */ +typedef enum { + LV_EVENT_ALL = 0, + + /** Input device events*/ + LV_EVENT_PRESSED, /**< The object has been pressed*/ + LV_EVENT_PRESSING, /**< The object is being pressed (called continuously while pressing)*/ + LV_EVENT_PRESS_LOST, /**< The object is still being pressed but slid cursor/finger off of the object */ + LV_EVENT_SHORT_CLICKED, /**< The object was pressed for a short period of time, then released it. Not called if scrolled.*/ + LV_EVENT_SINGLE_CLICKED, /**< Called for the first short click within a small distance and short time*/ + LV_EVENT_DOUBLE_CLICKED, /**< Called for the second short click within small distance and short time*/ + LV_EVENT_TRIPLE_CLICKED, /**< Called for the third short click within small distance and short time*/ + LV_EVENT_LONG_PRESSED, /**< Object has been pressed for at least `long_press_time`. Not called if scrolled.*/ + LV_EVENT_LONG_PRESSED_REPEAT, /**< Called after `long_press_time` in every `long_press_repeat_time` ms. Not called if scrolled.*/ + LV_EVENT_CLICKED, /**< Called on release if not scrolled (regardless to long press)*/ + LV_EVENT_RELEASED, /**< Called in every cases when the object has been released*/ + LV_EVENT_SCROLL_BEGIN, /**< Scrolling begins. The event parameter is a pointer to the animation of the scroll. Can be modified*/ + LV_EVENT_SCROLL_THROW_BEGIN, + LV_EVENT_SCROLL_END, /**< Scrolling ends*/ + LV_EVENT_SCROLL, /**< Scrolling*/ + LV_EVENT_GESTURE, /**< A gesture is detected. Get the gesture with `lv_indev_get_gesture_dir(lv_indev_active());` */ + LV_EVENT_KEY, /**< A key is sent to the object. Get the key with `lv_indev_get_key(lv_indev_active());`*/ + LV_EVENT_ROTARY, /**< An encoder or wheel was rotated. Get the rotation count with `lv_event_get_rotary_diff(e);`*/ + LV_EVENT_FOCUSED, /**< The object is focused*/ + LV_EVENT_DEFOCUSED, /**< The object is defocused*/ + LV_EVENT_LEAVE, /**< The object is defocused but still selected*/ + LV_EVENT_HIT_TEST, /**< Perform advanced hit-testing*/ + LV_EVENT_INDEV_RESET, /**< Indev has been reset*/ + LV_EVENT_HOVER_OVER, /**< Indev hover over object*/ + LV_EVENT_HOVER_LEAVE, /**< Indev hover leave object*/ + + /** Drawing events*/ + LV_EVENT_COVER_CHECK, /**< Check if the object fully covers an area. The event parameter is `lv_cover_check_info_t *`.*/ + LV_EVENT_REFR_EXT_DRAW_SIZE, /**< Get the required extra draw area around the object (e.g. for shadow). The event parameter is `int32_t *` to store the size.*/ + LV_EVENT_DRAW_MAIN_BEGIN, /**< Starting the main drawing phase*/ + LV_EVENT_DRAW_MAIN, /**< Perform the main drawing*/ + LV_EVENT_DRAW_MAIN_END, /**< Finishing the main drawing phase*/ + LV_EVENT_DRAW_POST_BEGIN, /**< Starting the post draw phase (when all children are drawn)*/ + LV_EVENT_DRAW_POST, /**< Perform the post draw phase (when all children are drawn)*/ + LV_EVENT_DRAW_POST_END, /**< Finishing the post draw phase (when all children are drawn)*/ + LV_EVENT_DRAW_TASK_ADDED, /**< Adding a draw task */ + + /** Special events*/ + LV_EVENT_VALUE_CHANGED, /**< The object's value has changed (i.e. slider moved)*/ + LV_EVENT_INSERT, /**< A text is inserted to the object. The event data is `char *` being inserted.*/ + LV_EVENT_REFRESH, /**< Notify the object to refresh something on it (for the user)*/ + LV_EVENT_READY, /**< A process has finished*/ + LV_EVENT_CANCEL, /**< A process has been cancelled */ + + /** Other events*/ + LV_EVENT_CREATE, /**< Object is being created*/ + LV_EVENT_DELETE, /**< Object is being deleted*/ + LV_EVENT_CHILD_CHANGED, /**< Child was removed, added, or its size, position were changed */ + LV_EVENT_CHILD_CREATED, /**< Child was created, always bubbles up to all parents*/ + LV_EVENT_CHILD_DELETED, /**< Child was deleted, always bubbles up to all parents*/ + LV_EVENT_SCREEN_UNLOAD_START, /**< A screen unload started, fired immediately when scr_load is called*/ + LV_EVENT_SCREEN_LOAD_START, /**< A screen load started, fired when the screen change delay is expired*/ + LV_EVENT_SCREEN_LOADED, /**< A screen was loaded*/ + LV_EVENT_SCREEN_UNLOADED, /**< A screen was unloaded*/ + LV_EVENT_SIZE_CHANGED, /**< Object coordinates/size have changed*/ + LV_EVENT_STYLE_CHANGED, /**< Object's style has changed*/ + LV_EVENT_LAYOUT_CHANGED, /**< The children position has changed due to a layout recalculation*/ + LV_EVENT_GET_SELF_SIZE, /**< Get the internal size of a widget*/ + + /** Events of optional LVGL components*/ + LV_EVENT_INVALIDATE_AREA, + LV_EVENT_RESOLUTION_CHANGED, + LV_EVENT_COLOR_FORMAT_CHANGED, + LV_EVENT_REFR_REQUEST, + LV_EVENT_REFR_START, + LV_EVENT_REFR_READY, + LV_EVENT_RENDER_START, + LV_EVENT_RENDER_READY, + LV_EVENT_FLUSH_START, + LV_EVENT_FLUSH_FINISH, + LV_EVENT_FLUSH_WAIT_START, + LV_EVENT_FLUSH_WAIT_FINISH, + + LV_EVENT_VSYNC, + + LV_EVENT_LAST, /** Number of default events*/ + + LV_EVENT_PREPROCESS = 0x8000, /** This is a flag that can be set with an event so it's processed + before the class default event processing */ + LV_EVENT_MARKED_DELETING = 0x10000, +} lv_event_code_t; + +typedef struct { + lv_array_t array; + uint8_t is_traversing: 1; /**< True: the list is being nested traversed */ + uint8_t has_marked_deleting: 1; /**< True: the list has marked deleting objects + when some of events are marked as deleting */ +} lv_event_list_t; + +/** + * @brief Event callback. + * Events are used to notify the user of some action being taken on the object. + * For details, see ::lv_event_t. + */ + +lv_result_t lv_event_send(lv_event_list_t * list, lv_event_t * e, bool preprocess); + +lv_event_dsc_t * lv_event_add(lv_event_list_t * list, lv_event_cb_t cb, lv_event_code_t filter, void * user_data); +bool lv_event_remove_dsc(lv_event_list_t * list, lv_event_dsc_t * dsc); + +uint32_t lv_event_get_count(lv_event_list_t * list); + +lv_event_dsc_t * lv_event_get_dsc(lv_event_list_t * list, uint32_t index); + +lv_event_cb_t lv_event_dsc_get_cb(lv_event_dsc_t * dsc); + +void * lv_event_dsc_get_user_data(lv_event_dsc_t * dsc); + +bool lv_event_remove(lv_event_list_t * list, uint32_t index); + +void lv_event_remove_all(lv_event_list_t * list); + +/** + * Get the object originally targeted by the event. It's the same even if the event is bubbled. + * @param e pointer to the event descriptor + * @return the target of the event_code + */ +void * lv_event_get_target(lv_event_t * e); + +/** + * Get the current target of the event. It's the object which event handler being called. + * If the event is not bubbled it's the same as "normal" target. + * @param e pointer to the event descriptor + * @return pointer to the current target of the event_code + */ +void * lv_event_get_current_target(lv_event_t * e); + +/** + * Get the event code of an event + * @param e pointer to the event descriptor + * @return the event code. (E.g. `LV_EVENT_CLICKED`, `LV_EVENT_FOCUSED`, etc) + */ +lv_event_code_t lv_event_get_code(lv_event_t * e); + +/** + * Get the parameter passed when the event was sent + * @param e pointer to the event descriptor + * @return pointer to the parameter + */ +void * lv_event_get_param(lv_event_t * e); + +/** + * Get the user_data passed when the event was registered on the object + * @param e pointer to the event descriptor + * @return pointer to the user_data + */ +void * lv_event_get_user_data(lv_event_t * e); + +/** + * Stop the event from bubbling. + * This is only valid when called in the middle of an event processing chain. + * @param e pointer to the event descriptor + */ +void lv_event_stop_bubbling(lv_event_t * e); + +/** + * Stop processing this event. + * This is only valid when called in the middle of an event processing chain. + * @param e pointer to the event descriptor + */ +void lv_event_stop_processing(lv_event_t * e); + +/** + * Register a new, custom event ID. + * It can be used the same way as e.g. `LV_EVENT_CLICKED` to send custom events + * @return the new event id + * + * Example: + * @code + * uint32_t LV_EVENT_MINE = 0; + * ... + * e = lv_event_register_id(); + * ... + * lv_obj_send_event(obj, LV_EVENT_MINE, &some_data); + * @endcode + */ +uint32_t lv_event_register_id(void); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_EVENT_H*/ diff --git a/include/liblvgl/misc/lv_event_private.h b/include/liblvgl/misc/lv_event_private.h new file mode 100644 index 00000000..5ba0e5a5 --- /dev/null +++ b/include/liblvgl/misc/lv_event_private.h @@ -0,0 +1,73 @@ +/** + * @file lv_event_private.h + * + */ + +#ifndef LV_EVENT_PRIVATE_H +#define LV_EVENT_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_event.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_event_dsc_t { + lv_event_cb_t cb; + void * user_data; + uint32_t filter; +}; + +struct _lv_event_t { + void * current_target; + void * original_target; + lv_event_code_t code; + void * user_data; + void * param; + lv_event_t * prev; + uint8_t deleted : 1; + uint8_t stop_processing : 1; + uint8_t stop_bubbling : 1; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void lv_event_push(lv_event_t * e); + +void lv_event_pop(lv_event_t * e); + +/** + * Nested events can be called and one of them might belong to an object that is being deleted. + * Mark this object's `event_temp_data` deleted to know that its `lv_obj_send_event` should return `LV_RESULT_INVALID` + * @param target pointer to an event target which was deleted + */ +void lv_event_mark_deleted(void * target); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_EVENT_PRIVATE_H*/ diff --git a/include/liblvgl/misc/lv_fs.h b/include/liblvgl/misc/lv_fs.h index 13b2eeb5..075171b4 100644 --- a/include/liblvgl/misc/lv_fs.h +++ b/include/liblvgl/misc/lv_fs.h @@ -13,10 +13,8 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" - -#include -#include +#include "../lv_conf_internal.h" +#include "lv_types.h" /********************* * DEFINES @@ -24,6 +22,8 @@ extern "C" { #define LV_FS_MAX_FN_LENGTH 64 #define LV_FS_MAX_PATH_LENGTH 256 +#define LV_FS_CACHE_FROM_BUFFER UINT32_MAX + /********************** * TYPEDEFS **********************/ @@ -31,7 +31,7 @@ extern "C" { /** * Errors in the file system module. */ -enum { +typedef enum { LV_FS_RES_OK = 0, LV_FS_RES_HW_ERR, /*Low level hardware error*/ LV_FS_RES_FS_ERR, /*Error in the file system structure*/ @@ -45,18 +45,15 @@ enum { LV_FS_RES_OUT_OF_MEM, /*Not enough memory for an internal operation*/ LV_FS_RES_INV_PARAM, /*Invalid parameter among arguments*/ LV_FS_RES_UNKNOWN, /*Other unknown error*/ -}; -typedef uint8_t lv_fs_res_t; +} lv_fs_res_t; /** * File open mode. */ -enum { +typedef enum { LV_FS_MODE_WR = 0x01, LV_FS_MODE_RD = 0x02, -}; -typedef uint8_t lv_fs_mode_t; - +} lv_fs_mode_t; /** * Seek modes. @@ -67,33 +64,26 @@ typedef enum { LV_FS_SEEK_END = 0x02, /**< Set the position from the end of the file*/ } lv_fs_whence_t; -typedef struct _lv_fs_drv_t { +struct _lv_fs_drv_t; +typedef struct _lv_fs_drv_t lv_fs_drv_t; +struct _lv_fs_drv_t { char letter; - uint16_t cache_size; - bool (*ready_cb)(struct _lv_fs_drv_t * drv); + uint32_t cache_size; + bool (*ready_cb)(lv_fs_drv_t * drv); - void * (*open_cb)(struct _lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode); - lv_fs_res_t (*close_cb)(struct _lv_fs_drv_t * drv, void * file_p); - lv_fs_res_t (*read_cb)(struct _lv_fs_drv_t * drv, void * file_p, void * buf, uint32_t btr, uint32_t * br); - lv_fs_res_t (*write_cb)(struct _lv_fs_drv_t * drv, void * file_p, const void * buf, uint32_t btw, uint32_t * bw); - lv_fs_res_t (*seek_cb)(struct _lv_fs_drv_t * drv, void * file_p, uint32_t pos, lv_fs_whence_t whence); - lv_fs_res_t (*tell_cb)(struct _lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p); + void * (*open_cb)(lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode); + lv_fs_res_t (*close_cb)(lv_fs_drv_t * drv, void * file_p); + lv_fs_res_t (*read_cb)(lv_fs_drv_t * drv, void * file_p, void * buf, uint32_t btr, uint32_t * br); + lv_fs_res_t (*write_cb)(lv_fs_drv_t * drv, void * file_p, const void * buf, uint32_t btw, uint32_t * bw); + lv_fs_res_t (*seek_cb)(lv_fs_drv_t * drv, void * file_p, uint32_t pos, lv_fs_whence_t whence); + lv_fs_res_t (*tell_cb)(lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p); - void * (*dir_open_cb)(struct _lv_fs_drv_t * drv, const char * path); - lv_fs_res_t (*dir_read_cb)(struct _lv_fs_drv_t * drv, void * rddir_p, char * fn); - lv_fs_res_t (*dir_close_cb)(struct _lv_fs_drv_t * drv, void * rddir_p); + void * (*dir_open_cb)(lv_fs_drv_t * drv, const char * path); + lv_fs_res_t (*dir_read_cb)(lv_fs_drv_t * drv, void * rddir_p, char * fn, uint32_t fn_len); + lv_fs_res_t (*dir_close_cb)(lv_fs_drv_t * drv, void * rddir_p); -#if LV_USE_USER_DATA void * user_data; /**< Custom file user data*/ -#endif -} lv_fs_drv_t; - -typedef struct { - uint32_t start; - uint32_t end; - uint32_t file_position; - void * buffer; -} lv_fs_file_cache_t; +}; typedef struct { void * file_d; @@ -101,6 +91,7 @@ typedef struct { lv_fs_file_cache_t * cache; } lv_fs_file_t; + typedef struct { void * dir_d; lv_fs_drv_t * drv; @@ -110,14 +101,9 @@ typedef struct { * GLOBAL PROTOTYPES **********************/ -/** - * Initialize the File system interface - */ -void _lv_fs_init(void); - /** * Initialize a file system driver with default values. - * It is used to surly have known values in the fields ant not memory junk. + * It is used to ensure all fields have known values and not memory junk. * After it you can set the fields. * @param drv pointer to driver variable to initialize */ @@ -155,6 +141,15 @@ bool lv_fs_is_ready(char letter); */ lv_fs_res_t lv_fs_open(lv_fs_file_t * file_p, const char * path, lv_fs_mode_t mode); +/** + * Make a path object for the memory-mapped file compatible with the file system interface + * @param path path to a lv_fs_path_ex object + * @param letter the letter of the driver. E.g. `LV_FS_MEMFS_LETTER` + * @param buf address of the memory buffer + * @param size size of the memory buffer in bytes + */ +void lv_fs_make_path_from_buffer(lv_fs_path_ex_t * path, char letter, const void * buf, uint32_t size); + /** * Close an already opened file * @param file_p pointer to a lv_fs_file_t variable @@ -186,7 +181,7 @@ lv_fs_res_t lv_fs_write(lv_fs_file_t * file_p, const void * buf, uint32_t btw, u * Set the position of the 'cursor' (read write pointer) in a file * @param file_p pointer to a lv_fs_file_t variable * @param pos the new position expressed in bytes index (0: start of file) - * @param whence tells from where set the position. See @lv_fs_whence_t + * @param whence tells from where to set position. See lv_fs_whence_t * @return LV_FS_RES_OK or any error from lv_fs_res_t enum */ lv_fs_res_t lv_fs_seek(lv_fs_file_t * file_p, uint32_t pos, lv_fs_whence_t whence); @@ -194,7 +189,7 @@ lv_fs_res_t lv_fs_seek(lv_fs_file_t * file_p, uint32_t pos, lv_fs_whence_t whenc /** * Give the position of the read write pointer * @param file_p pointer to a lv_fs_file_t variable - * @param pos_p pointer to store the position of the read write pointer + * @param pos pointer to store the position of the read write pointer * @return LV_FS_RES_OK or any error from 'fs_res_t' */ lv_fs_res_t lv_fs_tell(lv_fs_file_t * file_p, uint32_t * pos); @@ -212,9 +207,10 @@ lv_fs_res_t lv_fs_dir_open(lv_fs_dir_t * rddir_p, const char * path); * The name of the directories will begin with '/' * @param rddir_p pointer to an initialized 'fs_dir_t' variable * @param fn pointer to a buffer to store the filename + * @param fn_len length of the buffer to store the filename * @return LV_FS_RES_OK or any error from lv_fs_res_t enum */ -lv_fs_res_t lv_fs_dir_read(lv_fs_dir_t * rddir_p, char * fn); +lv_fs_res_t lv_fs_dir_read(lv_fs_dir_t * rddir_p, char * fn, uint32_t fn_len); /** * Close the directory reading diff --git a/include/liblvgl/misc/lv_fs_private.h b/include/liblvgl/misc/lv_fs_private.h new file mode 100644 index 00000000..c6b21374 --- /dev/null +++ b/include/liblvgl/misc/lv_fs_private.h @@ -0,0 +1,63 @@ +/** + * @file lv_fs_private.h + * + */ + +#ifndef LV_FS_PRIVATE_H +#define LV_FS_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_fs.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_fs_file_cache_t { + uint32_t start; + uint32_t end; + uint32_t file_position; + void * buffer; +}; + +/** Extended path object to specify buffer for memory-mapped files */ +struct _lv_fs_path_ex_t { + char path[4]; /**< This is needed to make it compatible with a normal path */ + const void * buffer; + uint32_t size; +}; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Initialize the File system interface + */ +void lv_fs_init(void); + +/** + * Deinitialize the File system interface + */ +void lv_fs_deinit(void); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_FS_PRIVATE_H*/ diff --git a/include/liblvgl/misc/lv_gc.h b/include/liblvgl/misc/lv_gc.h deleted file mode 100644 index 7a98d107..00000000 --- a/include/liblvgl/misc/lv_gc.h +++ /dev/null @@ -1,97 +0,0 @@ -/** - * @file lv_gc.h - * - */ - -#ifndef LV_GC_H -#define LV_GC_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lv_conf_internal.h" -#include -#include "lv_mem.h" -#include "lv_ll.h" -#include "lv_timer.h" -#include "lv_types.h" -#include "liblvgl/draw/lv_img_cache.h" -#include "liblvgl/draw/lv_draw_mask.h" -#include "liblvgl/core/lv_obj_pos.h" - -/********************* - * DEFINES - *********************/ -#if LV_IMG_CACHE_DEF_SIZE -# define LV_IMG_CACHE_DEF 1 -#else -# define LV_IMG_CACHE_DEF 0 -#endif - -#define LV_DISPATCH(f, t, n) f(t, n) -#define LV_DISPATCH_COND(f, t, n, m, v) LV_CONCAT3(LV_DISPATCH, m, v)(f, t, n) - -#define LV_DISPATCH00(f, t, n) LV_DISPATCH(f, t, n) -#define LV_DISPATCH01(f, t, n) -#define LV_DISPATCH10(f, t, n) -#define LV_DISPATCH11(f, t, n) LV_DISPATCH(f, t, n) - -#define LV_ITERATE_ROOTS(f) \ - LV_DISPATCH(f, lv_ll_t, _lv_timer_ll) /*Linked list to store the lv_timers*/ \ - LV_DISPATCH(f, lv_ll_t, _lv_disp_ll) /*Linked list of display device*/ \ - LV_DISPATCH(f, lv_ll_t, _lv_indev_ll) /*Linked list of input device*/ \ - LV_DISPATCH(f, lv_ll_t, _lv_fsdrv_ll) \ - LV_DISPATCH(f, lv_ll_t, _lv_anim_ll) \ - LV_DISPATCH(f, lv_ll_t, _lv_group_ll) \ - LV_DISPATCH(f, lv_ll_t, _lv_img_decoder_ll) \ - LV_DISPATCH(f, lv_ll_t, _lv_obj_style_trans_ll) \ - LV_DISPATCH(f, lv_layout_dsc_t *, _lv_layout_list) \ - LV_DISPATCH_COND(f, _lv_img_cache_entry_t*, _lv_img_cache_array, LV_IMG_CACHE_DEF, 1) \ - LV_DISPATCH_COND(f, _lv_img_cache_entry_t, _lv_img_cache_single, LV_IMG_CACHE_DEF, 0) \ - LV_DISPATCH(f, lv_timer_t*, _lv_timer_act) \ - LV_DISPATCH(f, lv_mem_buf_arr_t , lv_mem_buf) \ - LV_DISPATCH_COND(f, _lv_draw_mask_radius_circle_dsc_arr_t , _lv_circle_cache, LV_DRAW_COMPLEX, 1) \ - LV_DISPATCH_COND(f, _lv_draw_mask_saved_arr_t , _lv_draw_mask_list, LV_DRAW_COMPLEX, 1) \ - LV_DISPATCH(f, void * , _lv_theme_default_styles) \ - LV_DISPATCH(f, void * , _lv_theme_basic_styles) \ - LV_DISPATCH_COND(f, uint8_t *, _lv_font_decompr_buf, LV_USE_FONT_COMPRESSED, 1) \ - LV_DISPATCH(f, uint8_t * , _lv_grad_cache_mem) \ - LV_DISPATCH(f, uint8_t * , _lv_style_custom_prop_flag_lookup_table) - -#define LV_DEFINE_ROOT(root_type, root_name) root_type root_name; -#define LV_ROOTS LV_ITERATE_ROOTS(LV_DEFINE_ROOT) - -#if LV_ENABLE_GC == 1 -#if LV_MEM_CUSTOM != 1 -#error "GC requires CUSTOM_MEM" -#endif /*LV_MEM_CUSTOM*/ -#include LV_GC_INCLUDE -#else /*LV_ENABLE_GC*/ -#define LV_GC_ROOT(x) x -#define LV_EXTERN_ROOT(root_type, root_name) extern root_type root_name; -LV_ITERATE_ROOTS(LV_EXTERN_ROOT) -#endif /*LV_ENABLE_GC*/ - -/********************** - * TYPEDEFS - **********************/ - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -void _lv_gc_clear_roots(void); - -/********************** - * MACROS - **********************/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_GC_H*/ diff --git a/include/liblvgl/misc/lv_iter.h b/include/liblvgl/misc/lv_iter.h new file mode 100644 index 00000000..469d441e --- /dev/null +++ b/include/liblvgl/misc/lv_iter.h @@ -0,0 +1,120 @@ +/** +* @file lv_iter.h +* + */ + + +#ifndef LV_ITER_H +#define LV_ITER_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_types.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef lv_result_t (*lv_iter_next_cb)(void * instance, void * context, void * elem); +typedef void (*lv_iter_inspect_cb)(void * elem); + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create an iterator based on an instance, and then the next element of the iterator can be obtained through lv_iter_next, + * In order to obtain the next operation in a unified and abstract way. + * @param instance The instance to be iterated + * @param elem_size The size of the element to be iterated in bytes + * @param context_size The size of the context to be passed to the next_cb in bytes + * @param next_cb The callback function to get the next element + * @return The iterator object + */ +lv_iter_t * lv_iter_create(void * instance, uint32_t elem_size, uint32_t context_size, lv_iter_next_cb next_cb); + +/** + * Get the context of the iterator. You can use it to store some temporary variables associated with current iterator.. + * @param iter `lv_iter_t` object create before + * @return the iter context + */ +void * lv_iter_get_context(const lv_iter_t * iter); + +/** + * Destroy the iterator object, and release the context. Other resources allocated by the user are not released. + * The user needs to release it by itself. + * @param iter `lv_iter_t` object create before + */ +void lv_iter_destroy(lv_iter_t * iter); + +/** + * Get the next element of the iterator. + * @param iter `lv_iter_t` object create before + * @param elem The pointer to store the next element + * @return LV_RESULT_OK: Get the next element successfully + * LV_RESULT_INVALID: The next element is invalid + */ +lv_result_t lv_iter_next(lv_iter_t * iter, void * elem); + +/** + * Make the iterator peekable, which means that the user can peek the next element without advancing the iterator. + * @param iter `lv_iter_t` object create before + * @param capacity The capacity of the peek buffer + */ +void lv_iter_make_peekable(lv_iter_t * iter, uint32_t capacity); + +/** + * Peek the next element of the iterator without advancing the iterator. + * @param iter `lv_iter_t` object create before + * @param elem The pointer to store the next element + * @return LV_RESULT_OK: Peek the next element successfully + * LV_RESULT_INVALID: The next element is invalid + */ +lv_result_t lv_iter_peek(lv_iter_t * iter, void * elem); + +/** + * Only advance the iterator without getting the next element. + * @param iter `lv_iter_t` object create before + * @return LV_RESULT_OK: Peek the next element successfully + * LV_RESULT_INVALID: The next element is invalid + */ +lv_result_t lv_iter_peek_advance(lv_iter_t * iter); + +/** + * Reset the peek cursor to the `next` cursor. + * @param iter `lv_iter_t` object create before + * @return LV_RESULT_OK: Reset the peek buffer successfully + * LV_RESULT_INVALID: The peek buffer is invalid + */ +lv_result_t lv_iter_peek_reset(lv_iter_t * iter); + +/** + * Inspect the element of the iterator. The callback function will be called for each element of the iterator. + * @param iter `lv_iter_t` object create before + * @param inspect_cb The callback function to inspect the element + */ +void lv_iter_inspect(lv_iter_t * iter, lv_iter_inspect_cb inspect_cb); + +/************************* + * GLOBAL VARIABLES + *************************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_ITER_H*/ diff --git a/include/liblvgl/misc/lv_ll.h b/include/liblvgl/misc/lv_ll.h index d38f6928..ee0836ef 100644 --- a/include/liblvgl/misc/lv_ll.h +++ b/include/liblvgl/misc/lv_ll.h @@ -13,9 +13,8 @@ extern "C" { /********************* * INCLUDES *********************/ -#include -#include -#include +#include "../lv_conf_internal.h" +#include "lv_types.h" /********************* * DEFINES @@ -44,14 +43,14 @@ typedef struct { * @param ll_p pointer to lv_ll_t variable * @param node_size the size of 1 node in bytes */ -void _lv_ll_init(lv_ll_t * ll_p, uint32_t node_size); +void lv_ll_init(lv_ll_t * ll_p, uint32_t node_size); /** * Add a new head to a linked list * @param ll_p pointer to linked list * @return pointer to the new head */ -void * _lv_ll_ins_head(lv_ll_t * ll_p); +void * lv_ll_ins_head(lv_ll_t * ll_p); /** * Insert a new node in front of the n_act node @@ -59,14 +58,14 @@ void * _lv_ll_ins_head(lv_ll_t * ll_p); * @param n_act pointer a node * @return pointer to the new node */ -void * _lv_ll_ins_prev(lv_ll_t * ll_p, void * n_act); +void * lv_ll_ins_prev(lv_ll_t * ll_p, void * n_act); /** * Add a new tail to a linked list * @param ll_p pointer to linked list * @return pointer to the new tail */ -void * _lv_ll_ins_tail(lv_ll_t * ll_p); +void * lv_ll_ins_tail(lv_ll_t * ll_p); /** * Remove the node 'node_p' from 'll_p' linked list. @@ -74,13 +73,15 @@ void * _lv_ll_ins_tail(lv_ll_t * ll_p); * @param ll_p pointer to the linked list of 'node_p' * @param node_p pointer to node in 'll_p' linked list */ -void _lv_ll_remove(lv_ll_t * ll_p, void * node_p); +void lv_ll_remove(lv_ll_t * ll_p, void * node_p); + +void lv_ll_clear_custom(lv_ll_t * ll_p, void(*cleanup)(void *)); /** * Remove and free all elements from a linked list. The list remain valid but become empty. * @param ll_p pointer to linked list */ -void _lv_ll_clear(lv_ll_t * ll_p); +void lv_ll_clear(lv_ll_t * ll_p); /** * Move a node to a new linked list @@ -90,21 +91,21 @@ void _lv_ll_clear(lv_ll_t * ll_p); * @param head true: be the head in the new list * false be the tail in the new list */ -void _lv_ll_chg_list(lv_ll_t * ll_ori_p, lv_ll_t * ll_new_p, void * node, bool head); +void lv_ll_chg_list(lv_ll_t * ll_ori_p, lv_ll_t * ll_new_p, void * node, bool head); /** * Return with head node of the linked list * @param ll_p pointer to linked list * @return pointer to the head of 'll_p' */ -void * _lv_ll_get_head(const lv_ll_t * ll_p); +void * lv_ll_get_head(const lv_ll_t * ll_p); /** * Return with tail node of the linked list * @param ll_p pointer to linked list * @return pointer to the tail of 'll_p' */ -void * _lv_ll_get_tail(const lv_ll_t * ll_p); +void * lv_ll_get_tail(const lv_ll_t * ll_p); /** * Return with the pointer of the next node after 'n_act' @@ -112,7 +113,7 @@ void * _lv_ll_get_tail(const lv_ll_t * ll_p); * @param n_act pointer a node * @return pointer to the next node */ -void * _lv_ll_get_next(const lv_ll_t * ll_p, const void * n_act); +void * lv_ll_get_next(const lv_ll_t * ll_p, const void * n_act); /** * Return with the pointer of the previous node after 'n_act' @@ -120,16 +121,16 @@ void * _lv_ll_get_next(const lv_ll_t * ll_p, const void * n_act); * @param n_act pointer a node * @return pointer to the previous node */ -void * _lv_ll_get_prev(const lv_ll_t * ll_p, const void * n_act); +void * lv_ll_get_prev(const lv_ll_t * ll_p, const void * n_act); /** * Return the length of the linked list. * @param ll_p pointer to linked list * @return length of the linked list */ -uint32_t _lv_ll_get_len(const lv_ll_t * ll_p); +uint32_t lv_ll_get_len(const lv_ll_t * ll_p); -/** +/* * TODO * @param ll_p * @param n1_p @@ -138,27 +139,28 @@ void lv_ll_swap(lv_ll_t * ll_p, void * n1_p, void * n2_p); */ /** - * Move a node before an other node in the same linked list + * Move a node before another node in the same linked list + * * @param ll_p pointer to a linked list * @param n_act pointer to node to move * @param n_after pointer to a node which should be after `n_act` */ -void _lv_ll_move_before(lv_ll_t * ll_p, void * n_act, void * n_after); +void lv_ll_move_before(lv_ll_t * ll_p, void * n_act, void * n_after); /** * Check if a linked list is empty * @param ll_p pointer to a linked list * @return true: the linked list is empty; false: not empty */ -bool _lv_ll_is_empty(lv_ll_t * ll_p); +bool lv_ll_is_empty(lv_ll_t * ll_p); /********************** * MACROS **********************/ -#define _LV_LL_READ(list, i) for(i = _lv_ll_get_head(list); i != NULL; i = _lv_ll_get_next(list, i)) +#define LV_LL_READ(list, i) for(i = lv_ll_get_head(list); i != NULL; i = lv_ll_get_next(list, i)) -#define _LV_LL_READ_BACK(list, i) for(i = _lv_ll_get_tail(list); i != NULL; i = _lv_ll_get_prev(list, i)) +#define LV_LL_READ_BACK(list, i) for(i = lv_ll_get_tail(list); i != NULL; i = lv_ll_get_prev(list, i)) #ifdef __cplusplus } /*extern "C"*/ diff --git a/include/liblvgl/misc/lv_log.h b/include/liblvgl/misc/lv_log.h index cd61905d..7774ba6f 100644 --- a/include/liblvgl/misc/lv_log.h +++ b/include/liblvgl/misc/lv_log.h @@ -13,8 +13,7 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" -#include +#include "../lv_conf_internal.h" #include "lv_types.h" @@ -24,13 +23,13 @@ extern "C" { /*Possible log level. For compatibility declare it independently from `LV_USE_LOG`*/ -#define LV_LOG_LEVEL_TRACE 0 /**< A lot of logs to give detailed information*/ -#define LV_LOG_LEVEL_INFO 1 /**< Log important events*/ -#define LV_LOG_LEVEL_WARN 2 /**< Log if something unwanted happened but didn't caused problem*/ -#define LV_LOG_LEVEL_ERROR 3 /**< Only critical issue, when the system may fail*/ -#define LV_LOG_LEVEL_USER 4 /**< Custom logs from the user*/ -#define LV_LOG_LEVEL_NONE 5 /**< Do not log anything*/ -#define _LV_LOG_LEVEL_NUM 6 /**< Number of log levels*/ +#define LV_LOG_LEVEL_TRACE 0 /**< Log detailed information. */ +#define LV_LOG_LEVEL_INFO 1 /**< Log important events. */ +#define LV_LOG_LEVEL_WARN 2 /**< Log if something unwanted happened but didn't caused problem. */ +#define LV_LOG_LEVEL_ERROR 3 /**< Log only critical issues, when system may fail. */ +#define LV_LOG_LEVEL_USER 4 /**< Log only custom log messages added by the user. */ +#define LV_LOG_LEVEL_NONE 5 /**< Do not log anything. */ +#define LV_LOG_LEVEL_NUM 6 /**< Number of log levels */ LV_EXPORT_CONST_INT(LV_LOG_LEVEL_TRACE); LV_EXPORT_CONST_INT(LV_LOG_LEVEL_INFO); @@ -42,6 +41,15 @@ LV_EXPORT_CONST_INT(LV_LOG_LEVEL_NONE); typedef int8_t lv_log_level_t; #if LV_USE_LOG + +#if LV_LOG_USE_FILE_LINE +#define LV_LOG_FILE __FILE__ +#define LV_LOG_LINE __LINE__ +#else +#define LV_LOG_FILE NULL +#define LV_LOG_LINE 0 +#endif + /********************** * TYPEDEFS **********************/ @@ -49,7 +57,7 @@ typedef int8_t lv_log_level_t; /** * Log print function. Receives a string buffer to print". */ -typedef void (*lv_log_print_g_cb_t)(const char * buf); +typedef void (*lv_log_print_g_cb_t)(lv_log_level_t level, const char * buf); /********************** * GLOBAL PROTOTYPES @@ -80,15 +88,15 @@ void lv_log(const char * format, ...) LV_FORMAT_ATTRIBUTE(1, 2); * @param format printf-like format string * @param ... parameters for `format` */ -void _lv_log_add(lv_log_level_t level, const char * file, int line, - const char * func, const char * format, ...) LV_FORMAT_ATTRIBUTE(5, 6); +void lv_log_add(lv_log_level_t level, const char * file, int line, + const char * func, const char * format, ...) LV_FORMAT_ATTRIBUTE(5, 6); /********************** * MACROS **********************/ #ifndef LV_LOG_TRACE # if LV_LOG_LEVEL <= LV_LOG_LEVEL_TRACE -# define LV_LOG_TRACE(...) _lv_log_add(LV_LOG_LEVEL_TRACE, __FILE__, __LINE__, __func__, __VA_ARGS__) +# define LV_LOG_TRACE(...) lv_log_add(LV_LOG_LEVEL_TRACE, LV_LOG_FILE, LV_LOG_LINE, __func__, __VA_ARGS__) # else # define LV_LOG_TRACE(...) do {}while(0) # endif @@ -96,7 +104,7 @@ void _lv_log_add(lv_log_level_t level, const char * file, int line, #ifndef LV_LOG_INFO # if LV_LOG_LEVEL <= LV_LOG_LEVEL_INFO -# define LV_LOG_INFO(...) _lv_log_add(LV_LOG_LEVEL_INFO, __FILE__, __LINE__, __func__, __VA_ARGS__) +# define LV_LOG_INFO(...) lv_log_add(LV_LOG_LEVEL_INFO, LV_LOG_FILE, LV_LOG_LINE, __func__, __VA_ARGS__) # else # define LV_LOG_INFO(...) do {}while(0) # endif @@ -104,7 +112,7 @@ void _lv_log_add(lv_log_level_t level, const char * file, int line, #ifndef LV_LOG_WARN # if LV_LOG_LEVEL <= LV_LOG_LEVEL_WARN -# define LV_LOG_WARN(...) _lv_log_add(LV_LOG_LEVEL_WARN, __FILE__, __LINE__, __func__, __VA_ARGS__) +# define LV_LOG_WARN(...) lv_log_add(LV_LOG_LEVEL_WARN, LV_LOG_FILE, LV_LOG_LINE, __func__, __VA_ARGS__) # else # define LV_LOG_WARN(...) do {}while(0) # endif @@ -112,7 +120,7 @@ void _lv_log_add(lv_log_level_t level, const char * file, int line, #ifndef LV_LOG_ERROR # if LV_LOG_LEVEL <= LV_LOG_LEVEL_ERROR -# define LV_LOG_ERROR(...) _lv_log_add(LV_LOG_LEVEL_ERROR, __FILE__, __LINE__, __func__, __VA_ARGS__) +# define LV_LOG_ERROR(...) lv_log_add(LV_LOG_LEVEL_ERROR, LV_LOG_FILE, LV_LOG_LINE, __func__, __VA_ARGS__) # else # define LV_LOG_ERROR(...) do {}while(0) # endif @@ -120,7 +128,7 @@ void _lv_log_add(lv_log_level_t level, const char * file, int line, #ifndef LV_LOG_USER # if LV_LOG_LEVEL <= LV_LOG_LEVEL_USER -# define LV_LOG_USER(...) _lv_log_add(LV_LOG_LEVEL_USER, __FILE__, __LINE__, __func__, __VA_ARGS__) +# define LV_LOG_USER(...) lv_log_add(LV_LOG_LEVEL_USER, LV_LOG_FILE, LV_LOG_LINE, __func__, __VA_ARGS__) # else # define LV_LOG_USER(...) do {}while(0) # endif @@ -137,7 +145,7 @@ void _lv_log_add(lv_log_level_t level, const char * file, int line, #else /*LV_USE_LOG*/ /*Do nothing if `LV_USE_LOG 0`*/ -#define _lv_log_add(level, file, line, ...) +#define lv_log_add(level, file, line, ...) #define LV_LOG_TRACE(...) do {}while(0) #define LV_LOG_INFO(...) do {}while(0) #define LV_LOG_WARN(...) do {}while(0) diff --git a/include/liblvgl/misc/lv_lru.h b/include/liblvgl/misc/lv_lru.h index 3e9729e0..fe41322d 100644 --- a/include/liblvgl/misc/lv_lru.h +++ b/include/liblvgl/misc/lv_lru.h @@ -14,14 +14,10 @@ extern "C" { * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" +#include "../lv_conf_internal.h" #include "lv_types.h" -#include -#include - - /********************* * DEFINES *********************/ @@ -39,10 +35,11 @@ typedef enum { LV_LRU_VALUE_TOO_LARGE } lv_lru_res_t; -typedef void (lv_lru_free_t)(void * v); +typedef void (*lv_lru_free_cb_t)(void * v); + typedef struct _lv_lru_item_t lv_lru_item_t; -typedef struct lv_lru_t { +typedef struct _lv_lru_t { lv_lru_item_t ** items; uint64_t access_count; size_t free_memory; @@ -50,20 +47,19 @@ typedef struct lv_lru_t { size_t average_item_length; size_t hash_table_size; uint32_t seed; - lv_lru_free_t * value_free; - lv_lru_free_t * key_free; + lv_lru_free_cb_t value_free; + lv_lru_free_cb_t key_free; lv_lru_item_t * free_items; } lv_lru_t; - /********************** * GLOBAL PROTOTYPES **********************/ -lv_lru_t * lv_lru_create(size_t cache_size, size_t average_length, lv_lru_free_t * value_free, - lv_lru_free_t * key_free); +lv_lru_t * lv_lru_create(size_t cache_size, size_t average_length, lv_lru_free_cb_t value_free, + lv_lru_free_cb_t key_free); -void lv_lru_del(lv_lru_t * cache); +void lv_lru_delete(lv_lru_t * cache); lv_lru_res_t lv_lru_set(lv_lru_t * cache, const void * key, size_t key_length, void * value, size_t value_length); diff --git a/include/liblvgl/misc/lv_math.h b/include/liblvgl/misc/lv_math.h index 24f9ea6c..ef226f4d 100644 --- a/include/liblvgl/misc/lv_math.h +++ b/include/liblvgl/misc/lv_math.h @@ -13,17 +13,24 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" -#include +#include "../lv_conf_internal.h" +#include "lv_types.h" /********************* * DEFINES *********************/ -#define LV_TRIGO_SIN_MAX 32767 +#define LV_TRIGO_SIN_MAX 32768 #define LV_TRIGO_SHIFT 15 /**< >> LV_TRIGO_SHIFT to normalize*/ -#define LV_BEZIER_VAL_MAX 1024 /**< Max time in Bezier functions (not [0..1] to use integers)*/ #define LV_BEZIER_VAL_SHIFT 10 /**< log2(LV_BEZIER_VAL_MAX): used to normalize up scaled values*/ +#define LV_BEZIER_VAL_MAX (1L << LV_BEZIER_VAL_SHIFT) /**< Max time in Bezier functions (not [0..1] to use integers)*/ +#define LV_BEZIER_VAL_FLOAT(f) ((int32_t)((f) * LV_BEZIER_VAL_MAX)) /**< Convert const float number cubic-bezier values to fix-point value*/ + +/** Align up value x to align, align must be a power of two */ +#define LV_ALIGN_UP(x, align) (((x) + ((align) - 1)) & ~((align) - 1)) + +/** Round up value x to round, round can be any integer number */ +#define LV_ROUND_UP(x, round) ((((x) + ((round) - 1)) / (round)) * (round)) /********************** * TYPEDEFS @@ -44,25 +51,34 @@ typedef struct { * @param angle * @return sinus of 'angle'. sin(-90) = -32767, sin(90) = 32767 */ -LV_ATTRIBUTE_FAST_MEM int16_t lv_trigo_sin(int16_t angle); +int32_t /* LV_ATTRIBUTE_FAST_MEM */ lv_trigo_sin(int16_t angle); -static inline LV_ATTRIBUTE_FAST_MEM int16_t lv_trigo_cos(int16_t angle) -{ - return lv_trigo_sin(angle + 90); -} +int32_t LV_ATTRIBUTE_FAST_MEM lv_trigo_cos(int16_t angle); //! @endcond +/** + * Calculate the y value of cubic-bezier(x1, y1, x2, y2) function as specified x. + * @param x time in range of [0..LV_BEZIER_VAL_MAX] + * @param x1 x of control point 1 in range of [0..LV_BEZIER_VAL_MAX] + * @param y1 y of control point 1 in range of [0..LV_BEZIER_VAL_MAX] + * @param x2 x of control point 2 in range of [0..LV_BEZIER_VAL_MAX] + * @param y2 y of control point 2 in range of [0..LV_BEZIER_VAL_MAX] + * @return the value calculated + */ +int32_t lv_cubic_bezier(int32_t x, int32_t x1, int32_t y1, int32_t x2, int32_t y2); + /** * Calculate a value of a Cubic Bezier function. * @param t time in range of [0..LV_BEZIER_VAL_MAX] - * @param u0 start values in range of [0..LV_BEZIER_VAL_MAX] + * @param u0 must be 0 * @param u1 control value 1 values in range of [0..LV_BEZIER_VAL_MAX] * @param u2 control value 2 in range of [0..LV_BEZIER_VAL_MAX] - * @param u3 end values in range of [0..LV_BEZIER_VAL_MAX] + * @param u3 must be LV_BEZIER_VAL_MAX * @return the value calculated from the given parameters in range of [0..LV_BEZIER_VAL_MAX] */ -uint32_t lv_bezier3(uint32_t t, uint32_t u0, uint32_t u1, uint32_t u2, uint32_t u3); +int32_t lv_bezier3(int32_t t, int32_t u0, uint32_t u1, int32_t u2, int32_t u3); + /** * Calculate the atan2 of a vector. @@ -84,14 +100,30 @@ uint16_t lv_atan2(int x, int y); * If root < 256: mask = 0x800 * Else: mask = 0x8000 */ -LV_ATTRIBUTE_FAST_MEM void lv_sqrt(uint32_t x, lv_sqrt_res_t * q, uint32_t mask); +void /* LV_ATTRIBUTE_FAST_MEM */ lv_sqrt(uint32_t x, lv_sqrt_res_t * q, uint32_t mask); //! @endcond +/** + * Alternative (fast, approximate) implementation for getting the square root of an integer. + * @param x integer which square root should be calculated + */ +int32_t /* LV_ATTRIBUTE_FAST_MEM */ lv_sqrt32(uint32_t x); + +/** + * Calculate the square of an integer (input range is 0..32767). + * @param x input + * @return square + */ +static inline int32_t lv_sqr(int32_t x) +{ + return x * x; +} + /** * Calculate the integer exponents. * @param base - * @param power + * @param exp * @return base raised to the power exponent */ int64_t lv_pow(int64_t base, int8_t exp); @@ -107,6 +139,12 @@ int64_t lv_pow(int64_t base, int8_t exp); */ int32_t lv_map(int32_t x, int32_t min_in, int32_t max_in, int32_t min_out, int32_t max_out); +/** + * Set the seed of the pseudo random number generator + * @param seed a number to initialize the random generator + */ +void lv_rand_set_seed(uint32_t seed); + /** * Get a pseudo random number in the given range * @param min the minimum value diff --git a/include/liblvgl/misc/lv_matrix.h b/include/liblvgl/misc/lv_matrix.h new file mode 100644 index 00000000..7b8bb7f4 --- /dev/null +++ b/include/liblvgl/misc/lv_matrix.h @@ -0,0 +1,129 @@ +/** + * @file lv_matrix.h + * + */ + +#ifndef LV_MATRIX_H +#define LV_MATRIX_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../lv_conf_internal.h" + +#if LV_USE_MATRIX + +#include "lv_types.h" +#include "lv_area.h" + +/********************* + * DEFINES + *********************/ + +#if !LV_USE_FLOAT +#error "LV_USE_FLOAT is required for lv_matrix" +#endif + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_matrix_t { + float m[3][3]; +}; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Set matrix to identity matrix + * @param matrix pointer to a matrix + */ +void lv_matrix_identity(lv_matrix_t * matrix); + +/** + * Translate the matrix to new position + * @param matrix pointer to a matrix + * @param tx the amount of translate in x direction + * @param tx the amount of translate in y direction + */ +void lv_matrix_translate(lv_matrix_t * matrix, float tx, float ty); + +/** + * Change the scale factor of the matrix + * @param matrix pointer to a matrix + * @param scale_x the scale factor for the X direction + * @param scale_y the scale factor for the Y direction + */ +void lv_matrix_scale(lv_matrix_t * matrix, float scale_x, float scale_y); + +/** + * Rotate the matrix with origin + * @param matrix pointer to a matrix + * @param degree angle to rotate + */ +void lv_matrix_rotate(lv_matrix_t * matrix, float degree); + +/** + * Change the skew factor of the matrix + * @param matrix pointer to a matrix + * @param skew_x the skew factor for x direction + * @param skew_y the skew factor for y direction + */ +void lv_matrix_skew(lv_matrix_t * matrix, float skew_x, float skew_y); + +/** + * Multiply two matrix and store the result to the first one + * @param matrix pointer to a matrix + * @param matrix2 pointer to another matrix + */ +void lv_matrix_multiply(lv_matrix_t * matrix, const lv_matrix_t * mul); + +/** + * Invert the matrix + * @param matrix pointer to a matrix + * @param m pointer to another matrix (optional) + * @return true: the matrix is invertible, false: the matrix is singular and cannot be inverted + */ +bool lv_matrix_inverse(lv_matrix_t * matrix, const lv_matrix_t * m); + +/** + * Transform a point by a matrix + * @param matrix pointer to a matrix + * @param point pointer to a point + * @return the transformed point + */ +lv_point_precise_t lv_matrix_transform_precise_point(const lv_matrix_t * matrix, const lv_point_precise_t * point); + +/** + * Transform an area by a matrix + * @param matrix pointer to a matrix + * @param area pointer to an area + * @return the transformed area + */ +lv_area_t lv_matrix_transform_area(const lv_matrix_t * matrix, const lv_area_t * area); + +/** + * Check if the matrix is identity or translation matrix + * @param matrix pointer to a matrix + * @return true: the matrix is identity or translation matrix, false: the matrix is not identity or translation matrix + */ +bool lv_matrix_is_identity_or_translation(const lv_matrix_t * matrix); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_MATRIX*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_MATRIX_H*/ diff --git a/include/liblvgl/misc/lv_mem.h b/include/liblvgl/misc/lv_mem.h deleted file mode 100644 index fd202aba..00000000 --- a/include/liblvgl/misc/lv_mem.h +++ /dev/null @@ -1,243 +0,0 @@ -/** - * @file lv_mem.h - * - */ - -#ifndef LV_MEM_H -#define LV_MEM_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lv_conf_internal.h" - -#include -#include -#include - -#include "lv_types.h" - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -/** - * Heap information structure. - */ -typedef struct { - uint32_t total_size; /**< Total heap size*/ - uint32_t free_cnt; - uint32_t free_size; /**< Size of available memory*/ - uint32_t free_biggest_size; - uint32_t used_cnt; - uint32_t max_used; /**< Max size of Heap memory used*/ - uint8_t used_pct; /**< Percentage used*/ - uint8_t frag_pct; /**< Amount of fragmentation*/ -} lv_mem_monitor_t; - -typedef struct { - void * p; - uint16_t size; - uint8_t used : 1; -} lv_mem_buf_t; - -typedef lv_mem_buf_t lv_mem_buf_arr_t[LV_MEM_BUF_MAX_NUM]; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Initialize the dyn_mem module (work memory and other variables) - */ -void lv_mem_init(void); - -/** - * Clean up the memory buffer which frees all the allocated memories. - * @note It work only if `LV_MEM_CUSTOM == 0` - */ -void lv_mem_deinit(void); - -/** - * Allocate a memory dynamically - * @param size size of the memory to allocate in bytes - * @return pointer to the allocated memory - */ -void * lv_mem_alloc(size_t size); - -/** - * Free an allocated data - * @param data pointer to an allocated memory - */ -void lv_mem_free(void * data); - -/** - * Reallocate a memory with a new size. The old content will be kept. - * @param data pointer to an allocated memory. - * Its content will be copied to the new memory block and freed - * @param new_size the desired new size in byte - * @return pointer to the new memory, NULL on failure - */ -void * lv_mem_realloc(void * data_p, size_t new_size); - -/** - * - * @return - */ -lv_res_t lv_mem_test(void); - -/** - * Give information about the work memory of dynamic allocation - * @param mon_p pointer to a lv_mem_monitor_t variable, - * the result of the analysis will be stored here - */ -void lv_mem_monitor(lv_mem_monitor_t * mon_p); - - -/** - * Get a temporal buffer with the given size. - * @param size the required size - */ -void * lv_mem_buf_get(uint32_t size); - -/** - * Release a memory buffer - * @param p buffer to release - */ -void lv_mem_buf_release(void * p); - -/** - * Free all memory buffers - */ -void lv_mem_buf_free_all(void); - -//! @cond Doxygen_Suppress - -#if LV_MEMCPY_MEMSET_STD - -/** - * Wrapper for the standard memcpy - * @param dst pointer to the destination buffer - * @param src pointer to the source buffer - * @param len number of byte to copy - */ -static inline void * lv_memcpy(void * dst, const void * src, size_t len) -{ - return memcpy(dst, src, len); -} - -/** - * Wrapper for the standard memcpy - * @param dst pointer to the destination buffer - * @param src pointer to the source buffer - * @param len number of byte to copy - */ -static inline void * lv_memcpy_small(void * dst, const void * src, size_t len) -{ - return memcpy(dst, src, len); -} - -/** - * Wrapper for the standard memset - * @param dst pointer to the destination buffer - * @param v value to set [0..255] - * @param len number of byte to set - */ -static inline void lv_memset(void * dst, uint8_t v, size_t len) -{ - memset(dst, v, len); -} - -/** - * Wrapper for the standard memset with fixed 0x00 value - * @param dst pointer to the destination buffer - * @param len number of byte to set - */ -static inline void lv_memset_00(void * dst, size_t len) -{ - memset(dst, 0x00, len); -} - -/** - * Wrapper for the standard memset with fixed 0xFF value - * @param dst pointer to the destination buffer - * @param len number of byte to set - */ -static inline void lv_memset_ff(void * dst, size_t len) -{ - memset(dst, 0xFF, len); -} - -#else -/** - * Same as `memcpy` but optimized for 4 byte operation. - * @param dst pointer to the destination buffer - * @param src pointer to the source buffer - * @param len number of byte to copy - */ -LV_ATTRIBUTE_FAST_MEM void * lv_memcpy(void * dst, const void * src, size_t len); - -/** - * Same as `memcpy` but optimized to copy only a few bytes. - * @param dst pointer to the destination buffer - * @param src pointer to the source buffer - * @param len number of byte to copy - */ -LV_ATTRIBUTE_FAST_MEM static inline void * lv_memcpy_small(void * dst, const void * src, size_t len) -{ - uint8_t * d8 = (uint8_t *)dst; - const uint8_t * s8 = (const uint8_t *)src; - - while(len) { - *d8 = *s8; - d8++; - s8++; - len--; - } - - return dst; -} - -/** - * Same as `memset` but optimized for 4 byte operation. - * @param dst pointer to the destination buffer - * @param v value to set [0..255] - * @param len number of byte to set - */ -LV_ATTRIBUTE_FAST_MEM void lv_memset(void * dst, uint8_t v, size_t len); - -/** - * Same as `memset(dst, 0x00, len)` but optimized for 4 byte operation. - * @param dst pointer to the destination buffer - * @param len number of byte to set - */ -LV_ATTRIBUTE_FAST_MEM void lv_memset_00(void * dst, size_t len); - -/** - * Same as `memset(dst, 0xFF, len)` but optimized for 4 byte operation. - * @param dst pointer to the destination buffer - * @param len number of byte to set - */ -LV_ATTRIBUTE_FAST_MEM void lv_memset_ff(void * dst, size_t len); - -//! @endcond - -#endif - -/********************** - * MACROS - **********************/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_MEM_H*/ diff --git a/include/liblvgl/misc/lv_palette.h b/include/liblvgl/misc/lv_palette.h new file mode 100644 index 00000000..d2f408ca --- /dev/null +++ b/include/liblvgl/misc/lv_palette.h @@ -0,0 +1,68 @@ +/** + * @file lv_palette.h + * + */ + +#ifndef LV_PALETTE_H +#define LV_PALETTE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "lv_color.h" +#include "lv_types.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ +typedef enum { + LV_PALETTE_RED, + LV_PALETTE_PINK, + LV_PALETTE_PURPLE, + LV_PALETTE_DEEP_PURPLE, + LV_PALETTE_INDIGO, + LV_PALETTE_BLUE, + LV_PALETTE_LIGHT_BLUE, + LV_PALETTE_CYAN, + LV_PALETTE_TEAL, + LV_PALETTE_GREEN, + LV_PALETTE_LIGHT_GREEN, + LV_PALETTE_LIME, + LV_PALETTE_YELLOW, + LV_PALETTE_AMBER, + LV_PALETTE_ORANGE, + LV_PALETTE_DEEP_ORANGE, + LV_PALETTE_BROWN, + LV_PALETTE_BLUE_GREY, + LV_PALETTE_GREY, + LV_PALETTE_LAST, + LV_PALETTE_NONE = 0xff, +} lv_palette_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/*Source: https://vuetifyjs.com/en/styles/colors/#material-colors*/ + +lv_color_t lv_palette_main(lv_palette_t p); +lv_color_t lv_palette_lighten(lv_palette_t p, uint8_t lvl); +lv_color_t lv_palette_darken(lv_palette_t p, uint8_t lvl); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_PALETTE_H*/ diff --git a/include/liblvgl/misc/lv_printf.h b/include/liblvgl/misc/lv_printf.h deleted file mode 100644 index 4cbbd84c..00000000 --- a/include/liblvgl/misc/lv_printf.h +++ /dev/null @@ -1,92 +0,0 @@ -/////////////////////////////////////////////////////////////////////////////// -// \author (c) Marco Paland (info@paland.com) -// 2014-2019, PALANDesign Hannover, Germany -// -// \license The MIT License (MIT) -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// -// \brief Tiny printf, sprintf and snprintf implementation, optimized for speed on -// embedded systems with a very limited resources. -// Use this instead of bloated standard/newlib printf. -// These routines are thread safe and reentrant. -// -/////////////////////////////////////////////////////////////////////////////// - -/*Original repository: https://github.com/mpaland/printf*/ - -#ifndef _LV_PRINTF_H_ -#define _LV_PRINTF_H_ - -#if defined(__has_include) - #if __has_include() - #include - /* platform-specific printf format for int32_t, usually "d" or "ld" */ - #define LV_PRId32 PRId32 - #define LV_PRIu32 PRIu32 - #else - #define LV_PRId32 "d" - #define LV_PRIu32 "u" - #endif -#else - /* hope this is correct for ports without __has_include or without inttypes.h */ - #define LV_PRId32 "d" - #define LV_PRIu32 "u" -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -#include "../lv_conf_internal.h" - -#if LV_SPRINTF_CUSTOM == 0 - -#include -#include - -#include "lv_types.h" - -typedef struct { - const char * fmt; - va_list * va; -} lv_vaformat_t; - -/** - * Tiny snprintf/vsnprintf implementation - * \param buffer A pointer to the buffer where to store the formatted string - * \param count The maximum number of characters to store in the buffer, including a terminating null character - * \param format A string that specifies the format of the output - * \param va A value identifying a variable arguments list - * \return The number of characters that COULD have been written into the buffer, not counting the terminating - * null character. A value equal or larger than count indicates truncation. Only when the returned value - * is non-negative and less than count, the string has been completely written. - */ -int lv_snprintf(char * buffer, size_t count, const char * format, ...) LV_FORMAT_ATTRIBUTE(3, 4); -int lv_vsnprintf(char * buffer, size_t count, const char * format, va_list va) LV_FORMAT_ATTRIBUTE(3, 0); - -#else -#include LV_SPRINTF_INCLUDE -#endif - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif // _LV_PRINTF_H_ diff --git a/include/liblvgl/misc/lv_profiler.h b/include/liblvgl/misc/lv_profiler.h new file mode 100644 index 00000000..84be4e1f --- /dev/null +++ b/include/liblvgl/misc/lv_profiler.h @@ -0,0 +1,172 @@ +/** + * @file lv_profiler.h + * + */ + +#ifndef LV_PROFILER_H +#define LV_PROFILER_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../lv_conf_internal.h" + +#if LV_USE_PROFILER + +#include LV_PROFILER_INCLUDE + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#else + +#define LV_PROFILER_BEGIN +#define LV_PROFILER_END +#define LV_PROFILER_BEGIN_TAG(tag) LV_UNUSED(tag) +#define LV_PROFILER_END_TAG(tag) LV_UNUSED(tag) + +#endif /*LV_USE_PROFILER*/ + +#if LV_USE_PROFILER && LV_PROFILER_LAYOUT +#define LV_PROFILER_LAYOUT_BEGIN LV_PROFILER_BEGIN +#define LV_PROFILER_LAYOUT_END LV_PROFILER_END +#define LV_PROFILER_LAYOUT_BEGIN_TAG(tag) LV_PROFILER_BEGIN_TAG(tag) +#define LV_PROFILER_LAYOUT_END_TAG(tag) LV_PROFILER_END_TAG(tag) +#else +#define LV_PROFILER_LAYOUT_BEGIN +#define LV_PROFILER_LAYOUT_END +#define LV_PROFILER_LAYOUT_BEGIN_TAG(tag) +#define LV_PROFILER_LAYOUT_END_TAG(tag) +#endif + +#if LV_USE_PROFILER && LV_PROFILER_STYLE +#define LV_PROFILER_STYLE_BEGIN LV_PROFILER_BEGIN +#define LV_PROFILER_STYLE_END LV_PROFILER_END +#define LV_PROFILER_STYLE_BEGIN_TAG(tag) LV_PROFILER_BEGIN_TAG(tag) +#define LV_PROFILER_STYLE_END_TAG(tag) LV_PROFILER_END_TAG(tag) +#else +#define LV_PROFILER_STYLE_BEGIN +#define LV_PROFILER_STYLE_END +#define LV_PROFILER_STYLE_BEGIN_TAG(tag) +#define LV_PROFILER_STYLE_END_TAG(tag) +#endif + +#if LV_USE_PROFILER && LV_PROFILER_DRAW +#define LV_PROFILER_DRAW_BEGIN LV_PROFILER_BEGIN +#define LV_PROFILER_DRAW_END LV_PROFILER_END +#define LV_PROFILER_DRAW_BEGIN_TAG(tag) LV_PROFILER_BEGIN_TAG(tag) +#define LV_PROFILER_DRAW_END_TAG(tag) LV_PROFILER_END_TAG(tag) +#else +#define LV_PROFILER_DRAW_BEGIN +#define LV_PROFILER_DRAW_END +#define LV_PROFILER_DRAW_BEGIN_TAG(tag) +#define LV_PROFILER_DRAW_END_TAG(tag) +#endif + +#if LV_USE_PROFILER && LV_PROFILER_DECODER +#define LV_PROFILER_DECODER_BEGIN LV_PROFILER_BEGIN +#define LV_PROFILER_DECODER_END LV_PROFILER_END +#define LV_PROFILER_DECODER_BEGIN_TAG(tag) LV_PROFILER_BEGIN_TAG(tag) +#define LV_PROFILER_DECODER_END_TAG(tag) LV_PROFILER_END_TAG(tag) +#else +#define LV_PROFILER_DECODER_BEGIN +#define LV_PROFILER_DECODER_END +#define LV_PROFILER_DECODER_BEGIN_TAG(tag) +#define LV_PROFILER_DECODER_END_TAG(tag) +#endif + +#if LV_USE_PROFILER && LV_PROFILER_REFR +#define LV_PROFILER_REFR_BEGIN LV_PROFILER_BEGIN +#define LV_PROFILER_REFR_END LV_PROFILER_END +#define LV_PROFILER_REFR_BEGIN_TAG(tag) LV_PROFILER_BEGIN_TAG(tag) +#define LV_PROFILER_REFR_END_TAG(tag) LV_PROFILER_END_TAG(tag) +#else +#define LV_PROFILER_REFR_BEGIN +#define LV_PROFILER_REFR_END +#define LV_PROFILER_REFR_BEGIN_TAG(tag) +#define LV_PROFILER_REFR_END_TAG(tag) +#endif + +#if LV_USE_PROFILER && LV_PROFILER_INDEV +#define LV_PROFILER_INDEV_BEGIN LV_PROFILER_BEGIN +#define LV_PROFILER_INDEV_END LV_PROFILER_END +#define LV_PROFILER_INDEV_BEGIN_TAG(tag) LV_PROFILER_BEGIN_TAG(tag) +#define LV_PROFILER_INDEV_END_TAG(tag) LV_PROFILER_END_TAG(tag) +#else +#define LV_PROFILER_INDEV_BEGIN +#define LV_PROFILER_INDEV_END +#define LV_PROFILER_INDEV_BEGIN_TAG(tag) +#define LV_PROFILER_INDEV_END_TAG(tag) +#endif + +#if LV_USE_PROFILER && LV_PROFILER_FONT +#define LV_PROFILER_FONT_BEGIN LV_PROFILER_BEGIN +#define LV_PROFILER_FONT_END LV_PROFILER_END +#define LV_PROFILER_FONT_BEGIN_TAG(tag) LV_PROFILER_BEGIN_TAG(tag) +#define LV_PROFILER_FONT_END_TAG(tag) LV_PROFILER_END_TAG(tag) +#else +#define LV_PROFILER_FONT_BEGIN +#define LV_PROFILER_FONT_END +#define LV_PROFILER_FONT_BEGIN_TAG(tag) +#define LV_PROFILER_FONT_END_TAG(tag) +#endif + +#if LV_USE_PROFILER && LV_PROFILER_CACHE +#define LV_PROFILER_CACHE_BEGIN LV_PROFILER_BEGIN +#define LV_PROFILER_CACHE_END LV_PROFILER_END +#define LV_PROFILER_CACHE_BEGIN_TAG(tag) LV_PROFILER_BEGIN_TAG(tag) +#define LV_PROFILER_CACHE_END_TAG(tag) LV_PROFILER_END_TAG(tag) +#else +#define LV_PROFILER_CACHE_BEGIN +#define LV_PROFILER_CACHE_END +#define LV_PROFILER_CACHE_BEGIN_TAG(tag) +#define LV_PROFILER_CACHE_END_TAG(tag) +#endif + +#if LV_USE_PROFILER && LV_PROFILER_FS +#define LV_PROFILER_FS_BEGIN LV_PROFILER_BEGIN +#define LV_PROFILER_FS_END LV_PROFILER_END +#define LV_PROFILER_FS_BEGIN_TAG(tag) LV_PROFILER_BEGIN_TAG(tag) +#define LV_PROFILER_FS_END_TAG(tag) LV_PROFILER_END_TAG(tag) +#else +#define LV_PROFILER_FS_BEGIN +#define LV_PROFILER_FS_END +#define LV_PROFILER_FS_BEGIN_TAG(tag) +#define LV_PROFILER_FS_END_TAG(tag) +#endif + +#if LV_USE_PROFILER && LV_PROFILER_TIMER +#define LV_PROFILER_TIMER_BEGIN LV_PROFILER_BEGIN +#define LV_PROFILER_TIMER_END LV_PROFILER_END +#define LV_PROFILER_TIMER_BEGIN_TAG(tag) LV_PROFILER_BEGIN_TAG(tag) +#define LV_PROFILER_TIMER_END_TAG(tag) LV_PROFILER_END_TAG(tag) +#else +#define LV_PROFILER_TIMER_BEGIN +#define LV_PROFILER_TIMER_END +#define LV_PROFILER_TIMER_BEGIN_TAG(tag) +#define LV_PROFILER_TIMER_END_TAG(tag) +#endif + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_PROFILER_H*/ diff --git a/include/liblvgl/misc/lv_profiler_builtin.h b/include/liblvgl/misc/lv_profiler_builtin.h new file mode 100644 index 00000000..aa0220b2 --- /dev/null +++ b/include/liblvgl/misc/lv_profiler_builtin.h @@ -0,0 +1,85 @@ +/** + * @file lv_profiler_builtin.h + * + */ + +#ifndef LV_PROFILER_BUILTIN_H +#define LV_PROFILER_BUILTIN_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../lv_conf_internal.h" + +#if LV_USE_PROFILER && LV_USE_PROFILER_BUILTIN + +#include "lv_types.h" + +/********************* + * DEFINES + *********************/ + +#define LV_PROFILER_BUILTIN_BEGIN_TAG(tag) lv_profiler_builtin_write((tag), 'B') +#define LV_PROFILER_BUILTIN_END_TAG(tag) lv_profiler_builtin_write((tag), 'E') +#define LV_PROFILER_BUILTIN_BEGIN LV_PROFILER_BUILTIN_BEGIN_TAG(__func__) +#define LV_PROFILER_BUILTIN_END LV_PROFILER_BUILTIN_END_TAG(__func__) + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * @brief Initialize the configuration of the built-in profiler + * @param config Pointer to the configuration structure of the built-in profiler + */ +void lv_profiler_builtin_config_init(lv_profiler_builtin_config_t * config); + +/** + * @brief Initialize the built-in profiler with the given configuration + * @param config Pointer to the configuration structure of the built-in profiler + */ +void lv_profiler_builtin_init(const lv_profiler_builtin_config_t * config); + +/** + * @brief Uninitialize the built-in profiler + */ +void lv_profiler_builtin_uninit(void); + +/** + * @brief Enable or disable the built-in profiler + * @param enable true to enable the built-in profiler, false to disable + */ +void lv_profiler_builtin_set_enable(bool enable); + +/** + * @brief Flush the profiling data to the console + */ +void lv_profiler_builtin_flush(void); + +/** + * @brief Write the profiling data for a function with the given tag + * @param func Name of the function being profiled + * @param tag Tag to associate with the profiling data for the function + */ +void lv_profiler_builtin_write(const char * func, char tag); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_PROFILER_BUILTIN*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_PROFILER_BUILTIN_H*/ diff --git a/include/liblvgl/misc/lv_profiler_builtin_private.h b/include/liblvgl/misc/lv_profiler_builtin_private.h new file mode 100644 index 00000000..a134620d --- /dev/null +++ b/include/liblvgl/misc/lv_profiler_builtin_private.h @@ -0,0 +1,56 @@ +/** + * @file lv_profiler_builtin_private.h + * + */ + +#ifndef LV_PROFILER_BUILTIN_PRIVATE_H +#define LV_PROFILER_BUILTIN_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_profiler_builtin.h" + +#if LV_USE_PROFILER && LV_USE_PROFILER_BUILTIN + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** + * @brief LVGL profiler built-in configuration structure + */ +struct _lv_profiler_builtin_config_t { + size_t buf_size; /**< The size of the buffer used for profiling data */ + uint32_t tick_per_sec; /**< The number of ticks per second */ + uint32_t (*tick_get_cb)(void); /**< Callback function to get the current tick count */ + void (*flush_cb)(const char * buf); /**< Callback function to flush the profiling data */ + int (*tid_get_cb)(void); /**< Callback function to get the current thread ID */ + int (*cpu_get_cb)(void); /**< Callback function to get the current CPU */ +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_PROFILER && LV_USE_PROFILER_BUILTIN */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_PROFILER_BUILTIN_PRIVATE_H*/ diff --git a/include/liblvgl/misc/lv_rb.h b/include/liblvgl/misc/lv_rb.h new file mode 100644 index 00000000..bec0a788 --- /dev/null +++ b/include/liblvgl/misc/lv_rb.h @@ -0,0 +1,66 @@ +/** + * @file lv_rb.h + * + */ + +#ifndef LV_RB_H +#define LV_RB_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "lv_types.h" +#include "lv_assert.h" +#include "lv_types.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef enum { + LV_RB_COLOR_RED, + LV_RB_COLOR_BLACK +} lv_rb_color_t; + +typedef int8_t lv_rb_compare_res_t; + +typedef lv_rb_compare_res_t (*lv_rb_compare_t)(const void * a, const void * b); + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +bool lv_rb_init(lv_rb_t * tree, lv_rb_compare_t compare, size_t node_size); +lv_rb_node_t * lv_rb_insert(lv_rb_t * tree, void * key); +lv_rb_node_t * lv_rb_find(lv_rb_t * tree, const void * key); +void * lv_rb_remove_node(lv_rb_t * tree, lv_rb_node_t * node); +void * lv_rb_remove(lv_rb_t * tree, const void * key); +bool lv_rb_drop_node(lv_rb_t * tree, lv_rb_node_t * node); +bool lv_rb_drop(lv_rb_t * tree, const void * key); +lv_rb_node_t * lv_rb_minimum(lv_rb_t * node); +lv_rb_node_t * lv_rb_maximum(lv_rb_t * node); +lv_rb_node_t * lv_rb_minimum_from(lv_rb_node_t * node); +lv_rb_node_t * lv_rb_maximum_from(lv_rb_node_t * node); +void lv_rb_destroy(lv_rb_t * tree); + +/************************* + * GLOBAL VARIABLES + *************************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_RB_H*/ diff --git a/include/liblvgl/misc/lv_rb_private.h b/include/liblvgl/misc/lv_rb_private.h new file mode 100644 index 00000000..492ca2e2 --- /dev/null +++ b/include/liblvgl/misc/lv_rb_private.h @@ -0,0 +1,54 @@ +/** + * @file lv_rb_private.h + * + */ + +#ifndef LV_RB_PRIVATE_H +#define LV_RB_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_rb.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_rb_node_t { + struct _lv_rb_node_t * parent; + struct _lv_rb_node_t * left; + struct _lv_rb_node_t * right; + lv_rb_color_t color; + void * data; +}; + +struct _lv_rb_t { + lv_rb_node_t * root; + lv_rb_compare_t compare; + size_t size; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_RB_PRIVATE_H*/ diff --git a/include/liblvgl/misc/lv_style.h b/include/liblvgl/misc/lv_style.h index 44a542dc..19909dbe 100644 --- a/include/liblvgl/misc/lv_style.h +++ b/include/liblvgl/misc/lv_style.h @@ -13,16 +13,15 @@ extern "C" { /********************* * INCLUDES *********************/ -#include -#include -#include "liblvgl/font/lv_font.h" +#include "../font/lv_font.h" #include "lv_color.h" #include "lv_area.h" #include "lv_anim.h" -#include "lv_txt.h" +#include "lv_text.h" #include "lv_types.h" #include "lv_assert.h" #include "lv_bidi.h" +#include "../layouts/lv_layout.h" /********************* * DEFINES @@ -30,86 +29,74 @@ extern "C" { #define LV_STYLE_SENTINEL_VALUE 0xAABBCCDD -/** +/* * Flags for style behavior - * - * The rest of the flags will have _FLAG added to their name in v9. */ -#define LV_STYLE_PROP_FLAG_NONE (0) -#define LV_STYLE_PROP_INHERIT (1 << 0) /*Inherited*/ -#define LV_STYLE_PROP_EXT_DRAW (1 << 1) /*Requires ext. draw size update when changed*/ -#define LV_STYLE_PROP_LAYOUT_REFR (1 << 2) /*Requires layout update when changed*/ -#define LV_STYLE_PROP_PARENT_LAYOUT_REFR (1 << 3) /*Requires layout update on parent when changed*/ -#define LV_STYLE_PROP_LAYER_REFR (1 << 4) /*Affects layer handling*/ -#define LV_STYLE_PROP_ALL (0x1F) /*Indicating all flags*/ - -/** +#define LV_STYLE_PROP_FLAG_NONE (0) /**< No special behavior */ +#define LV_STYLE_PROP_FLAG_INHERITABLE (1 << 0) /**< Inherited */ +#define LV_STYLE_PROP_FLAG_EXT_DRAW_UPDATE (1 << 1) /**< Requires ext. draw size update when changed */ +#define LV_STYLE_PROP_FLAG_LAYOUT_UPDATE (1 << 2) /**< Requires layout update when changed */ +#define LV_STYLE_PROP_FLAG_PARENT_LAYOUT_UPDATE (1 << 3) /**< Requires layout update on parent when changed */ +#define LV_STYLE_PROP_FLAG_LAYER_UPDATE (1 << 4) /**< Affects layer handling */ +#define LV_STYLE_PROP_FLAG_TRANSFORM (1 << 5) /**< Affects the object's transformation */ +#define LV_STYLE_PROP_FLAG_ALL (0x3F) /**< Indicating all flags */ + +/* * Other constants */ -#define LV_IMG_ZOOM_NONE 256 /*Value for not zooming the image*/ -LV_EXPORT_CONST_INT(LV_IMG_ZOOM_NONE); +#define LV_SCALE_NONE 256 /**< Value for not zooming the image */ +LV_EXPORT_CONST_INT(LV_SCALE_NONE); // *INDENT-OFF* #if LV_USE_ASSERT_STYLE #define LV_STYLE_CONST_INIT(var_name, prop_array) \ const lv_style_t var_name = { \ .sentinel = LV_STYLE_SENTINEL_VALUE, \ - .v_p = { .const_props = prop_array }, \ - .has_group = 0xFF, \ - .prop1 = LV_STYLE_PROP_ANY, \ - .prop_cnt = (sizeof(prop_array) / sizeof((prop_array)[0])), \ + .values_and_props = (void*)prop_array, \ + .has_group = 0xFFFFFFFF, \ + .prop_cnt = 255 \ } #else #define LV_STYLE_CONST_INIT(var_name, prop_array) \ const lv_style_t var_name = { \ - .v_p = { .const_props = prop_array }, \ - .has_group = 0xFF, \ - .prop1 = LV_STYLE_PROP_ANY, \ - .prop_cnt = (sizeof(prop_array) / sizeof((prop_array)[0])), \ + .values_and_props = prop_array, \ + .has_group = 0xFFFFFFFF, \ + .prop_cnt = 255, \ } #endif // *INDENT-ON* -#define LV_STYLE_PROP_META_INHERIT 0x8000 -#define LV_STYLE_PROP_META_INITIAL 0x4000 -#define LV_STYLE_PROP_META_MASK (LV_STYLE_PROP_META_INHERIT | LV_STYLE_PROP_META_INITIAL) - -#define LV_STYLE_PROP_ID_MASK(prop) ((lv_style_prop_t)((prop) & ~LV_STYLE_PROP_META_MASK)) +#define LV_STYLE_CONST_PROPS_END { .prop = LV_STYLE_PROP_INV, .value = { .num = 0 } } /********************** * TYPEDEFS **********************/ /** - * Possible options how to blend opaque drawings + * Possible options for blending opaque drawings */ -enum { +typedef enum { LV_BLEND_MODE_NORMAL, /**< Simply mix according to the opacity value*/ LV_BLEND_MODE_ADDITIVE, /**< Add the respective color channels*/ LV_BLEND_MODE_SUBTRACTIVE,/**< Subtract the foreground from the background*/ LV_BLEND_MODE_MULTIPLY, /**< Multiply the foreground and background*/ - LV_BLEND_MODE_REPLACE, /**< Replace background with foreground in the area*/ -}; - -typedef uint8_t lv_blend_mode_t; +} lv_blend_mode_t; /** * Some options to apply decorations on texts. * 'OR'ed values can be used. */ -enum { +typedef enum { LV_TEXT_DECOR_NONE = 0x00, LV_TEXT_DECOR_UNDERLINE = 0x01, LV_TEXT_DECOR_STRIKETHROUGH = 0x02, -}; - -typedef uint8_t lv_text_decor_t; +} lv_text_decor_t; /** * Selects on which sides border should be drawn * 'OR'ed values can be used. */ -enum { +typedef enum { LV_BORDER_SIDE_NONE = 0x00, LV_BORDER_SIDE_BOTTOM = 0x01, LV_BORDER_SIDE_TOP = 0x02, @@ -117,48 +104,71 @@ enum { LV_BORDER_SIDE_RIGHT = 0x08, LV_BORDER_SIDE_FULL = 0x0F, LV_BORDER_SIDE_INTERNAL = 0x10, /**< FOR matrix-like objects (e.g. Button matrix)*/ -}; -typedef uint8_t lv_border_side_t; +} lv_border_side_t; /** * The direction of the gradient. */ -enum { - LV_GRAD_DIR_NONE, /**< No gradient (the `grad_color` property is ignored)*/ - LV_GRAD_DIR_VER, /**< Vertical (top to bottom) gradient*/ - LV_GRAD_DIR_HOR, /**< Horizontal (left to right) gradient*/ -}; - -typedef uint8_t lv_grad_dir_t; +typedef enum { + LV_GRAD_DIR_NONE, /**< No gradient (the `grad_color` property is ignored)*/ + LV_GRAD_DIR_VER, /**< Simple vertical (top to bottom) gradient*/ + LV_GRAD_DIR_HOR, /**< Simple horizontal (left to right) gradient*/ + LV_GRAD_DIR_LINEAR, /**< Linear gradient defined by start and end points. Can be at any angle.*/ + LV_GRAD_DIR_RADIAL, /**< Radial gradient defined by start and end circles*/ + LV_GRAD_DIR_CONICAL, /**< Conical gradient defined by center point, start and end angles*/ +} lv_grad_dir_t; /** - * The dithering algorithm for the gradient - * Depends on LV_DITHER_GRADIENT - */ -enum { - LV_DITHER_NONE, /**< No dithering, colors are just quantized to the output resolution*/ - LV_DITHER_ORDERED, /**< Ordered dithering. Faster to compute and use less memory but lower quality*/ - LV_DITHER_ERR_DIFF, /**< Error diffusion mode. Slower to compute and use more memory but give highest dither quality*/ -}; - -typedef uint8_t lv_dither_mode_t; + * Gradient behavior outside the defined range. +*/ +typedef enum { + LV_GRAD_EXTEND_PAD, /**< Repeat the same color*/ + LV_GRAD_EXTEND_REPEAT, /**< Repeat the pattern*/ + LV_GRAD_EXTEND_REFLECT, /**< Repeat the pattern mirrored*/ +} lv_grad_extend_t; /** A gradient stop definition. * This matches a color and a position in a virtual 0-255 scale. */ typedef struct { lv_color_t color; /**< The stop color */ + lv_opa_t opa; /**< The opacity of the color*/ uint8_t frac; /**< The stop position in 1/255 unit */ } lv_gradient_stop_t; /** A descriptor of a gradient. */ typedef struct { - lv_gradient_stop_t stops[LV_GRADIENT_MAX_STOPS]; /**< A gradient stop array */ - uint8_t stops_count; /**< The number of used stops in the array */ - lv_grad_dir_t dir : 3; /**< The gradient direction. - * Any of LV_GRAD_DIR_HOR, LV_GRAD_DIR_VER, LV_GRAD_DIR_NONE */ - lv_dither_mode_t dither : 3; /**< Whether to dither the gradient or not. - * Any of LV_DITHER_NONE, LV_DITHER_ORDERED, LV_DITHER_ERR_DIFF */ + lv_gradient_stop_t stops[LV_GRADIENT_MAX_STOPS]; /**< A gradient stop array */ + uint8_t stops_count; /**< The number of used stops in the array */ + lv_grad_dir_t dir : 3; /**< The gradient direction. + * Any of LV_GRAD_DIR_NONE, LV_GRAD_DIR_VER, LV_GRAD_DIR_HOR, + * LV_GRAD_TYPE_LINEAR, LV_GRAD_TYPE_RADIAL, LV_GRAD_TYPE_CONICAL */ + lv_grad_extend_t extend : 2; /**< Behaviour outside the defined range. + * LV_GRAD_EXTEND_NONE, LV_GRAD_EXTEND_PAD, LV_GRAD_EXTEND_REPEAT, LV_GRAD_EXTEND_REFLECT */ +#if LV_USE_DRAW_SW_COMPLEX_GRADIENTS + union { + /*Linear gradient parameters*/ + struct { + lv_point_t start; /**< Linear gradient vector start point */ + lv_point_t end; /**< Linear gradient vector end point */ + } linear; + /*Radial gradient parameters*/ + struct { + lv_point_t focal; /**< Center of the focal (starting) circle in local coordinates */ + /* (can be the same as the ending circle to create concentric circles) */ + lv_point_t focal_extent; /**< Point on the circle (can be the same as the center) */ + lv_point_t end; /**< Center of the ending circle in local coordinates */ + lv_point_t end_extent; /**< Point on the circle determining the radius of the gradient */ + } radial; + /*Conical gradient parameters*/ + struct { + lv_point_t center; /**< Conical gradient center point */ + int16_t start_angle; /**< Start angle 0..3600 */ + int16_t end_angle; /**< End angle 0..3600 */ + } conical; + } params; + void * state; +#endif } lv_grad_dsc_t; /** @@ -175,131 +185,170 @@ typedef union { * * Props are split into groups of 16. When adding a new prop to a group, ensure it does not overflow into the next one. */ -typedef enum { +enum { LV_STYLE_PROP_INV = 0, /*Group 0*/ LV_STYLE_WIDTH = 1, - LV_STYLE_MIN_WIDTH = 2, - LV_STYLE_MAX_WIDTH = 3, - LV_STYLE_HEIGHT = 4, - LV_STYLE_MIN_HEIGHT = 5, - LV_STYLE_MAX_HEIGHT = 6, - LV_STYLE_X = 7, - LV_STYLE_Y = 8, - LV_STYLE_ALIGN = 9, - LV_STYLE_LAYOUT = 10, - LV_STYLE_RADIUS = 11, + LV_STYLE_HEIGHT = 2, + LV_STYLE_LENGTH = 3, + + LV_STYLE_MIN_WIDTH = 4, + LV_STYLE_MAX_WIDTH = 5, + LV_STYLE_MIN_HEIGHT = 6, + LV_STYLE_MAX_HEIGHT = 7, + + LV_STYLE_X = 8, + LV_STYLE_Y = 9, + LV_STYLE_ALIGN = 10, + + LV_STYLE_RADIUS = 12, /*Group 1*/ LV_STYLE_PAD_TOP = 16, LV_STYLE_PAD_BOTTOM = 17, LV_STYLE_PAD_LEFT = 18, LV_STYLE_PAD_RIGHT = 19, + LV_STYLE_PAD_ROW = 20, LV_STYLE_PAD_COLUMN = 21, - LV_STYLE_BASE_DIR = 22, - LV_STYLE_CLIP_CORNER = 23, + LV_STYLE_LAYOUT = 22, + + LV_STYLE_MARGIN_TOP = 24, + LV_STYLE_MARGIN_BOTTOM = 25, + LV_STYLE_MARGIN_LEFT = 26, + LV_STYLE_MARGIN_RIGHT = 27, /*Group 2*/ - LV_STYLE_BG_COLOR = 32, - LV_STYLE_BG_OPA = 33, - LV_STYLE_BG_GRAD_COLOR = 34, - LV_STYLE_BG_GRAD_DIR = 35, - LV_STYLE_BG_MAIN_STOP = 36, - LV_STYLE_BG_GRAD_STOP = 37, + LV_STYLE_BG_COLOR = 28, + LV_STYLE_BG_OPA = 29, + + LV_STYLE_BG_GRAD_DIR = 32, + LV_STYLE_BG_MAIN_STOP = 33, + LV_STYLE_BG_GRAD_STOP = 34, + LV_STYLE_BG_GRAD_COLOR = 35, + + LV_STYLE_BG_MAIN_OPA = 36, + LV_STYLE_BG_GRAD_OPA = 37, LV_STYLE_BG_GRAD = 38, - LV_STYLE_BG_DITHER_MODE = 39, - LV_STYLE_BG_IMG_SRC = 40, - LV_STYLE_BG_IMG_OPA = 41, - LV_STYLE_BG_IMG_RECOLOR = 42, - LV_STYLE_BG_IMG_RECOLOR_OPA = 43, - LV_STYLE_BG_IMG_TILED = 44, + LV_STYLE_BASE_DIR = 39, + + LV_STYLE_BG_IMAGE_SRC = 40, + LV_STYLE_BG_IMAGE_OPA = 41, + LV_STYLE_BG_IMAGE_RECOLOR = 42, + LV_STYLE_BG_IMAGE_RECOLOR_OPA = 43, + + LV_STYLE_BG_IMAGE_TILED = 44, + LV_STYLE_CLIP_CORNER = 45, /*Group 3*/ - LV_STYLE_BORDER_COLOR = 48, - LV_STYLE_BORDER_OPA = 49, - LV_STYLE_BORDER_WIDTH = 50, - LV_STYLE_BORDER_SIDE = 51, - LV_STYLE_BORDER_POST = 52, - LV_STYLE_OUTLINE_WIDTH = 53, - LV_STYLE_OUTLINE_COLOR = 54, - LV_STYLE_OUTLINE_OPA = 55, - LV_STYLE_OUTLINE_PAD = 56, + LV_STYLE_BORDER_WIDTH = 48, + LV_STYLE_BORDER_COLOR = 49, + LV_STYLE_BORDER_OPA = 50, + + LV_STYLE_BORDER_SIDE = 52, + LV_STYLE_BORDER_POST = 53, + + LV_STYLE_OUTLINE_WIDTH = 56, + LV_STYLE_OUTLINE_COLOR = 57, + LV_STYLE_OUTLINE_OPA = 58, + LV_STYLE_OUTLINE_PAD = 59, /*Group 4*/ - LV_STYLE_SHADOW_WIDTH = 64, - LV_STYLE_SHADOW_OFS_X = 65, - LV_STYLE_SHADOW_OFS_Y = 66, - LV_STYLE_SHADOW_SPREAD = 67, - LV_STYLE_SHADOW_COLOR = 68, - LV_STYLE_SHADOW_OPA = 69, - LV_STYLE_IMG_OPA = 70, - LV_STYLE_IMG_RECOLOR = 71, - LV_STYLE_IMG_RECOLOR_OPA = 72, - LV_STYLE_LINE_WIDTH = 73, - LV_STYLE_LINE_DASH_WIDTH = 74, - LV_STYLE_LINE_DASH_GAP = 75, - LV_STYLE_LINE_ROUNDED = 76, - LV_STYLE_LINE_COLOR = 77, - LV_STYLE_LINE_OPA = 78, + LV_STYLE_SHADOW_WIDTH = 60, + LV_STYLE_SHADOW_COLOR = 61, + LV_STYLE_SHADOW_OPA = 62, + + LV_STYLE_SHADOW_OFFSET_X = 64, + LV_STYLE_SHADOW_OFFSET_Y = 65, + LV_STYLE_SHADOW_SPREAD = 66, + + LV_STYLE_IMAGE_OPA = 68, + LV_STYLE_IMAGE_RECOLOR = 69, + LV_STYLE_IMAGE_RECOLOR_OPA = 70, + + LV_STYLE_LINE_WIDTH = 72, + LV_STYLE_LINE_DASH_WIDTH = 73, + LV_STYLE_LINE_DASH_GAP = 74, + LV_STYLE_LINE_ROUNDED = 75, + LV_STYLE_LINE_COLOR = 76, + LV_STYLE_LINE_OPA = 77, /*Group 5*/ LV_STYLE_ARC_WIDTH = 80, LV_STYLE_ARC_ROUNDED = 81, LV_STYLE_ARC_COLOR = 82, LV_STYLE_ARC_OPA = 83, - LV_STYLE_ARC_IMG_SRC = 84, - LV_STYLE_TEXT_COLOR = 85, - LV_STYLE_TEXT_OPA = 86, - LV_STYLE_TEXT_FONT = 87, - LV_STYLE_TEXT_LETTER_SPACE = 88, - LV_STYLE_TEXT_LINE_SPACE = 89, - LV_STYLE_TEXT_DECOR = 90, - LV_STYLE_TEXT_ALIGN = 91, - - /*Group 6*/ - LV_STYLE_OPA = 96, + LV_STYLE_ARC_IMAGE_SRC = 84, + + LV_STYLE_TEXT_COLOR = 88, + LV_STYLE_TEXT_OPA = 89, + LV_STYLE_TEXT_FONT = 90, + + LV_STYLE_TEXT_LETTER_SPACE = 91, + LV_STYLE_TEXT_LINE_SPACE = 92, + LV_STYLE_TEXT_DECOR = 93, + LV_STYLE_TEXT_ALIGN = 94, + + LV_STYLE_OPA = 95, + LV_STYLE_OPA_LAYERED = 96, LV_STYLE_COLOR_FILTER_DSC = 97, LV_STYLE_COLOR_FILTER_OPA = 98, LV_STYLE_ANIM = 99, - LV_STYLE_ANIM_TIME = 100, - LV_STYLE_ANIM_SPEED = 101, + LV_STYLE_ANIM_DURATION = 100, LV_STYLE_TRANSITION = 102, LV_STYLE_BLEND_MODE = 103, LV_STYLE_TRANSFORM_WIDTH = 104, LV_STYLE_TRANSFORM_HEIGHT = 105, LV_STYLE_TRANSLATE_X = 106, LV_STYLE_TRANSLATE_Y = 107, - LV_STYLE_TRANSFORM_ZOOM = 108, - LV_STYLE_TRANSFORM_ANGLE = 109, - LV_STYLE_TRANSFORM_PIVOT_X = 110, - LV_STYLE_TRANSFORM_PIVOT_Y = 111, - - _LV_STYLE_LAST_BUILT_IN_PROP = 111, - _LV_STYLE_NUM_BUILT_IN_PROPS = _LV_STYLE_LAST_BUILT_IN_PROP + 1, - - LV_STYLE_PROP_ANY = 0xFFFF, - _LV_STYLE_PROP_CONST = 0xFFFF /* magic value for const styles */ -} lv_style_prop_t; + LV_STYLE_TRANSFORM_SCALE_X = 108, + LV_STYLE_TRANSFORM_SCALE_Y = 109, + LV_STYLE_TRANSFORM_ROTATION = 110, + LV_STYLE_TRANSFORM_PIVOT_X = 111, + LV_STYLE_TRANSFORM_PIVOT_Y = 112, + LV_STYLE_TRANSFORM_SKEW_X = 113, + LV_STYLE_TRANSFORM_SKEW_Y = 114, + LV_STYLE_BITMAP_MASK_SRC = 115, + LV_STYLE_ROTARY_SENSITIVITY = 116, + + LV_STYLE_FLEX_FLOW = 125, + LV_STYLE_FLEX_MAIN_PLACE = 126, + LV_STYLE_FLEX_CROSS_PLACE = 127, + LV_STYLE_FLEX_TRACK_PLACE = 128, + LV_STYLE_FLEX_GROW = 129, + + LV_STYLE_GRID_COLUMN_ALIGN = 130, + LV_STYLE_GRID_ROW_ALIGN = 131, + LV_STYLE_GRID_ROW_DSC_ARRAY = 132, + LV_STYLE_GRID_COLUMN_DSC_ARRAY = 133, + LV_STYLE_GRID_CELL_COLUMN_POS = 134, + LV_STYLE_GRID_CELL_COLUMN_SPAN = 135, + LV_STYLE_GRID_CELL_X_ALIGN = 136, + LV_STYLE_GRID_CELL_ROW_POS = 137, + LV_STYLE_GRID_CELL_ROW_SPAN = 138, + LV_STYLE_GRID_CELL_Y_ALIGN = 139, + + LV_STYLE_LAST_BUILT_IN_PROP = 140, + + LV_STYLE_NUM_BUILT_IN_PROPS = LV_STYLE_LAST_BUILT_IN_PROP + 1, + + LV_STYLE_PROP_ANY = 0xFF, + LV_STYLE_PROP_CONST = 0xFF /* magic value for const styles */ +}; -enum { +typedef enum { LV_STYLE_RES_NOT_FOUND, LV_STYLE_RES_FOUND, - LV_STYLE_RES_INHERIT -}; - -typedef uint8_t lv_style_res_t; +} lv_style_res_t; /** * Descriptor for style transitions */ typedef struct { const lv_style_prop_t * props; /**< An array with the properties to animate.*/ -#if LV_USE_USER_DATA void * user_data; /**< A custom user data that will be passed to the animation's user_data */ -#endif - lv_anim_path_cb_t path_xcb; /**< A path for the animation.*/ + lv_anim_path_cb_t path_xcb; /**< A path for the animation.*/ uint32_t time; /**< Duration of the transition in [ms]*/ uint32_t delay; /**< Delay before the transition in [ms]*/ } lv_style_transition_dsc_t; @@ -321,24 +370,16 @@ typedef struct { uint32_t sentinel; #endif - /*If there is only one property store it directly. - *For more properties allocate an array*/ - union { - lv_style_value_t value1; - uint8_t * values_and_props; - const lv_style_const_prop_t * const_props; - } v_p; - - uint16_t prop1; - uint8_t has_group; - uint8_t prop_cnt; + void * values_and_props; + + uint32_t has_group; + uint8_t prop_cnt; /**< 255 means it's a constant style*/ } lv_style_t; /********************** * GLOBAL PROTOTYPES **********************/ - /** * Initialize a style * @param style pointer to a style to initialize @@ -354,10 +395,23 @@ void lv_style_init(lv_style_t * style); */ void lv_style_reset(lv_style_t * style); +/** + * Check if a style is constant + * @param style pointer to a style + * @return true: the style is constant + */ +static inline bool lv_style_is_const(const lv_style_t * style) +{ + if(style->prop_cnt == 255) return true; + return false; +} + /** * Register a new style property for custom usage * @return a new property ID, or LV_STYLE_PROP_INV if there are no more available. - * @example + * + * Example: + * @code * lv_style_prop_t MY_PROP; * static inline void lv_style_set_my_prop(lv_style_t * style, lv_color_t value) { * lv_style_value_t v = {.color = value}; lv_style_set_prop(style, MY_PROP, v); } @@ -366,6 +420,7 @@ void lv_style_reset(lv_style_t * style); * MY_PROP = lv_style_register_prop(); * ... * lv_style_set_my_prop(&style1, lv_palette_main(LV_PALETTE_RED)); + * @endcode */ lv_style_prop_t lv_style_register_prop(uint8_t flag); @@ -392,22 +447,13 @@ bool lv_style_remove_prop(lv_style_t * style, lv_style_prop_t prop); */ void lv_style_set_prop(lv_style_t * style, lv_style_prop_t prop, lv_style_value_t value); -/** - * Set a special meta state for a property in a style. - * This function shouldn't be used directly by the user. - * @param style pointer to style - * @param prop the ID of a property (e.g. `LV_STYLE_BG_COLOR`) - * @param meta the meta value to attach to the property in the style - */ -void lv_style_set_prop_meta(lv_style_t * style, lv_style_prop_t prop, uint16_t meta); - /** * Get the value of a property * @param style pointer to a style * @param prop the ID of a property * @param value pointer to a `lv_style_value_t` variable to store the value - * @return LV_RES_INV: the property wasn't found in the style (`value` is unchanged) - * LV_RES_OK: the property was fond, and `value` is set accordingly + * @return LV_RESULT_INVALID: the property wasn't found in the style (`value` is unchanged) + * LV_RESULT_OK: the property was fond, and `value` is set accordingly * @note For performance reasons there are no sanity check on `style` */ lv_style_res_t lv_style_get_prop(const lv_style_t * style, lv_style_prop_t prop, lv_style_value_t * value); @@ -420,10 +466,13 @@ lv_style_res_t lv_style_get_prop(const lv_style_t * style, lv_style_prop_t prop, * @param time duration of the transition in [ms] * @param delay delay before the transition in [ms] * @param user_data any custom data that will be saved in the transition animation and will be available when `path_cb` is called - * @example + * + * Example: + * @code * const static lv_style_prop_t trans_props[] = { LV_STYLE_BG_OPA, LV_STYLE_BG_COLOR, 0 }; - * static lv_style_transition_dsc_t trans1; - * lv_style_transition_dsc_init(&trans1, trans_props, NULL, 300, 0, NULL); + * static lv_style_transition_dsc_t trans1; + * lv_style_transition_dsc_init(&trans1, trans_props, NULL, 300, 0, NULL); + * @endcode */ void lv_style_transition_dsc_init(lv_style_transition_dsc_t * tr, const lv_style_prop_t props[], lv_anim_path_cb_t path_cb, uint32_t time, uint32_t delay, void * user_data); @@ -440,58 +489,35 @@ lv_style_value_t lv_style_prop_get_default(lv_style_prop_t prop); * @param style pointer to a style * @param prop the ID of a property * @param value pointer to a `lv_style_value_t` variable to store the value - * @return LV_RES_INV: the property wasn't found in the style (`value` is unchanged) - * LV_RES_OK: the property was fond, and `value` is set accordingly + * @return LV_RESULT_INVALID: the property wasn't found in the style (`value` is unchanged) + * LV_RESULT_OK: the property was fond, and `value` is set accordingly * @note For performance reasons there are no sanity check on `style` * @note This function is the same as ::lv_style_get_prop but inlined. Use it only on performance critical places */ static inline lv_style_res_t lv_style_get_prop_inlined(const lv_style_t * style, lv_style_prop_t prop, lv_style_value_t * value) { - if(style->prop1 == LV_STYLE_PROP_ANY) { - const lv_style_const_prop_t * const_prop; + if(lv_style_is_const(style)) { + lv_style_const_prop_t * props = (lv_style_const_prop_t *)style->values_and_props; uint32_t i; - for(i = 0; i < style->prop_cnt; i++) { - const_prop = style->v_p.const_props + i; - lv_style_prop_t prop_id = LV_STYLE_PROP_ID_MASK(const_prop->prop); - if(prop_id == prop) { - if(const_prop->prop & LV_STYLE_PROP_META_INHERIT) - return LV_STYLE_RES_INHERIT; - *value = (const_prop->prop & LV_STYLE_PROP_META_INITIAL) ? lv_style_prop_get_default(prop_id) : const_prop->value; + for(i = 0; props[i].prop != LV_STYLE_PROP_INV; i++) { + if(props[i].prop == prop) { + *value = props[i].value; return LV_STYLE_RES_FOUND; } } - return LV_STYLE_RES_NOT_FOUND; } - - if(style->prop_cnt == 0) return LV_STYLE_RES_NOT_FOUND; - - if(style->prop_cnt > 1) { - uint8_t * tmp = style->v_p.values_and_props + style->prop_cnt * sizeof(lv_style_value_t); - uint16_t * props = (uint16_t *)tmp; + else { + lv_style_prop_t * props = (lv_style_prop_t *)style->values_and_props + style->prop_cnt * sizeof(lv_style_value_t); uint32_t i; for(i = 0; i < style->prop_cnt; i++) { - lv_style_prop_t prop_id = LV_STYLE_PROP_ID_MASK(props[i]); - if(prop_id == prop) { - if(props[i] & LV_STYLE_PROP_META_INHERIT) - return LV_STYLE_RES_INHERIT; - if(props[i] & LV_STYLE_PROP_META_INITIAL) - *value = lv_style_prop_get_default(prop_id); - else { - lv_style_value_t * values = (lv_style_value_t *)style->v_p.values_and_props; - *value = values[i]; - } + if(props[i] == prop) { + lv_style_value_t * values = (lv_style_value_t *)style->values_and_props; + *value = values[i]; return LV_STYLE_RES_FOUND; } } } - else if(LV_STYLE_PROP_ID_MASK(style->prop1) == prop) { - if(style->prop1 & LV_STYLE_PROP_META_INHERIT) - return LV_STYLE_RES_INHERIT; - *value = (style->prop1 & LV_STYLE_PROP_META_INITIAL) ? lv_style_prop_get_default(LV_STYLE_PROP_ID_MASK( - style->prop1)) : style->v_p.value1; - return LV_STYLE_RES_FOUND; - } return LV_STYLE_RES_NOT_FOUND; } @@ -506,9 +532,15 @@ bool lv_style_is_empty(const lv_style_t * style); * Tell the group of a property. If the a property from a group is set in a style the (1 << group) bit of style->has_group is set. * It allows early skipping the style if the property is not exists in the style at all. * @param prop a style property - * @return the group [0..7] 7 means all the custom properties with index > 112 + * @return the group [0..30] 30 means all the custom properties with index > 120 */ -uint8_t _lv_style_get_prop_group(lv_style_prop_t prop); +static inline uint32_t lv_style_get_prop_group(lv_style_prop_t prop) +{ + uint32_t group = prop >> 2; + if(group > 30) group = 31; /*The MSB marks all the custom properties*/ + return group; + +} /** * Get the flags of a built-in or custom property. @@ -516,17 +548,17 @@ uint8_t _lv_style_get_prop_group(lv_style_prop_t prop); * @param prop a style property * @return the flags of the property */ -uint8_t _lv_style_prop_lookup_flags(lv_style_prop_t prop); +uint8_t lv_style_prop_lookup_flags(lv_style_prop_t prop); #include "lv_style_gen.h" -static inline void lv_style_set_size(lv_style_t * style, lv_coord_t value) +static inline void lv_style_set_size(lv_style_t * style, int32_t width, int32_t height) { - lv_style_set_width(style, value); - lv_style_set_height(style, value); + lv_style_set_width(style, width); + lv_style_set_height(style, height); } -static inline void lv_style_set_pad_all(lv_style_t * style, lv_coord_t value) +static inline void lv_style_set_pad_all(lv_style_t * style, int32_t value) { lv_style_set_pad_left(style, value); lv_style_set_pad_right(style, value); @@ -534,24 +566,38 @@ static inline void lv_style_set_pad_all(lv_style_t * style, lv_coord_t value) lv_style_set_pad_bottom(style, value); } -static inline void lv_style_set_pad_hor(lv_style_t * style, lv_coord_t value) +static inline void lv_style_set_pad_hor(lv_style_t * style, int32_t value) { lv_style_set_pad_left(style, value); lv_style_set_pad_right(style, value); } -static inline void lv_style_set_pad_ver(lv_style_t * style, lv_coord_t value) +static inline void lv_style_set_pad_ver(lv_style_t * style, int32_t value) { lv_style_set_pad_top(style, value); lv_style_set_pad_bottom(style, value); } -static inline void lv_style_set_pad_gap(lv_style_t * style, lv_coord_t value) +static inline void lv_style_set_pad_gap(lv_style_t * style, int32_t value) { lv_style_set_pad_row(style, value); lv_style_set_pad_column(style, value); } +static inline void lv_style_set_margin_all(lv_style_t * style, int32_t value) +{ + lv_style_set_margin_left(style, value); + lv_style_set_margin_right(style, value); + lv_style_set_margin_top(style, value); + lv_style_set_margin_bottom(style, value); +} + +static inline void lv_style_set_transform_scale(lv_style_t * style, int32_t value) +{ + lv_style_set_transform_scale_x(style, value); + lv_style_set_transform_scale_y(style, value); +} + /** * @brief Check if the style property has a specified behavioral flag. * @@ -564,13 +610,15 @@ static inline void lv_style_set_pad_gap(lv_style_t * style, lv_coord_t value) */ static inline bool lv_style_prop_has_flag(lv_style_prop_t prop, uint8_t flag) { - return _lv_style_prop_lookup_flags(prop) & flag; + return lv_style_prop_lookup_flags(prop) & flag; } /************************* * GLOBAL VARIABLES *************************/ +LV_ATTRIBUTE_EXTERN_DATA extern const lv_style_prop_t lv_style_const_prop_id_inv; + /********************** * MACROS **********************/ diff --git a/include/liblvgl/misc/lv_style_gen.h b/include/liblvgl/misc/lv_style_gen.h index 8bf3b68f..5714e74d 100644 --- a/include/liblvgl/misc/lv_style_gen.h +++ b/include/liblvgl/misc/lv_style_gen.h @@ -1,87 +1,135 @@ -void lv_style_set_width(lv_style_t * style, lv_coord_t value); -void lv_style_set_min_width(lv_style_t * style, lv_coord_t value); -void lv_style_set_max_width(lv_style_t * style, lv_coord_t value); -void lv_style_set_height(lv_style_t * style, lv_coord_t value); -void lv_style_set_min_height(lv_style_t * style, lv_coord_t value); -void lv_style_set_max_height(lv_style_t * style, lv_coord_t value); -void lv_style_set_x(lv_style_t * style, lv_coord_t value); -void lv_style_set_y(lv_style_t * style, lv_coord_t value); + +/* + ********************************************************************** + * DO NOT EDIT + * This file is automatically generated by "style_api_gen.py" + ********************************************************************** + */ + + +#ifndef LV_STYLE_GEN_H +#define LV_STYLE_GEN_H + +#ifdef __cplusplus +extern "C" { +#endif + +void lv_style_set_width(lv_style_t * style, int32_t value); +void lv_style_set_min_width(lv_style_t * style, int32_t value); +void lv_style_set_max_width(lv_style_t * style, int32_t value); +void lv_style_set_height(lv_style_t * style, int32_t value); +void lv_style_set_min_height(lv_style_t * style, int32_t value); +void lv_style_set_max_height(lv_style_t * style, int32_t value); +void lv_style_set_length(lv_style_t * style, int32_t value); +void lv_style_set_x(lv_style_t * style, int32_t value); +void lv_style_set_y(lv_style_t * style, int32_t value); void lv_style_set_align(lv_style_t * style, lv_align_t value); -void lv_style_set_transform_width(lv_style_t * style, lv_coord_t value); -void lv_style_set_transform_height(lv_style_t * style, lv_coord_t value); -void lv_style_set_translate_x(lv_style_t * style, lv_coord_t value); -void lv_style_set_translate_y(lv_style_t * style, lv_coord_t value); -void lv_style_set_transform_zoom(lv_style_t * style, lv_coord_t value); -void lv_style_set_transform_angle(lv_style_t * style, lv_coord_t value); -void lv_style_set_transform_pivot_x(lv_style_t * style, lv_coord_t value); -void lv_style_set_transform_pivot_y(lv_style_t * style, lv_coord_t value); -void lv_style_set_pad_top(lv_style_t * style, lv_coord_t value); -void lv_style_set_pad_bottom(lv_style_t * style, lv_coord_t value); -void lv_style_set_pad_left(lv_style_t * style, lv_coord_t value); -void lv_style_set_pad_right(lv_style_t * style, lv_coord_t value); -void lv_style_set_pad_row(lv_style_t * style, lv_coord_t value); -void lv_style_set_pad_column(lv_style_t * style, lv_coord_t value); +void lv_style_set_transform_width(lv_style_t * style, int32_t value); +void lv_style_set_transform_height(lv_style_t * style, int32_t value); +void lv_style_set_translate_x(lv_style_t * style, int32_t value); +void lv_style_set_translate_y(lv_style_t * style, int32_t value); +void lv_style_set_transform_scale_x(lv_style_t * style, int32_t value); +void lv_style_set_transform_scale_y(lv_style_t * style, int32_t value); +void lv_style_set_transform_rotation(lv_style_t * style, int32_t value); +void lv_style_set_transform_pivot_x(lv_style_t * style, int32_t value); +void lv_style_set_transform_pivot_y(lv_style_t * style, int32_t value); +void lv_style_set_transform_skew_x(lv_style_t * style, int32_t value); +void lv_style_set_transform_skew_y(lv_style_t * style, int32_t value); +void lv_style_set_pad_top(lv_style_t * style, int32_t value); +void lv_style_set_pad_bottom(lv_style_t * style, int32_t value); +void lv_style_set_pad_left(lv_style_t * style, int32_t value); +void lv_style_set_pad_right(lv_style_t * style, int32_t value); +void lv_style_set_pad_row(lv_style_t * style, int32_t value); +void lv_style_set_pad_column(lv_style_t * style, int32_t value); +void lv_style_set_margin_top(lv_style_t * style, int32_t value); +void lv_style_set_margin_bottom(lv_style_t * style, int32_t value); +void lv_style_set_margin_left(lv_style_t * style, int32_t value); +void lv_style_set_margin_right(lv_style_t * style, int32_t value); void lv_style_set_bg_color(lv_style_t * style, lv_color_t value); void lv_style_set_bg_opa(lv_style_t * style, lv_opa_t value); void lv_style_set_bg_grad_color(lv_style_t * style, lv_color_t value); void lv_style_set_bg_grad_dir(lv_style_t * style, lv_grad_dir_t value); -void lv_style_set_bg_main_stop(lv_style_t * style, lv_coord_t value); -void lv_style_set_bg_grad_stop(lv_style_t * style, lv_coord_t value); +void lv_style_set_bg_main_stop(lv_style_t * style, int32_t value); +void lv_style_set_bg_grad_stop(lv_style_t * style, int32_t value); +void lv_style_set_bg_main_opa(lv_style_t * style, lv_opa_t value); +void lv_style_set_bg_grad_opa(lv_style_t * style, lv_opa_t value); void lv_style_set_bg_grad(lv_style_t * style, const lv_grad_dsc_t * value); -void lv_style_set_bg_dither_mode(lv_style_t * style, lv_dither_mode_t value); -void lv_style_set_bg_img_src(lv_style_t * style, const void * value); -void lv_style_set_bg_img_opa(lv_style_t * style, lv_opa_t value); -void lv_style_set_bg_img_recolor(lv_style_t * style, lv_color_t value); -void lv_style_set_bg_img_recolor_opa(lv_style_t * style, lv_opa_t value); -void lv_style_set_bg_img_tiled(lv_style_t * style, bool value); +void lv_style_set_bg_image_src(lv_style_t * style, const void * value); +void lv_style_set_bg_image_opa(lv_style_t * style, lv_opa_t value); +void lv_style_set_bg_image_recolor(lv_style_t * style, lv_color_t value); +void lv_style_set_bg_image_recolor_opa(lv_style_t * style, lv_opa_t value); +void lv_style_set_bg_image_tiled(lv_style_t * style, bool value); void lv_style_set_border_color(lv_style_t * style, lv_color_t value); void lv_style_set_border_opa(lv_style_t * style, lv_opa_t value); -void lv_style_set_border_width(lv_style_t * style, lv_coord_t value); +void lv_style_set_border_width(lv_style_t * style, int32_t value); void lv_style_set_border_side(lv_style_t * style, lv_border_side_t value); void lv_style_set_border_post(lv_style_t * style, bool value); -void lv_style_set_outline_width(lv_style_t * style, lv_coord_t value); +void lv_style_set_outline_width(lv_style_t * style, int32_t value); void lv_style_set_outline_color(lv_style_t * style, lv_color_t value); void lv_style_set_outline_opa(lv_style_t * style, lv_opa_t value); -void lv_style_set_outline_pad(lv_style_t * style, lv_coord_t value); -void lv_style_set_shadow_width(lv_style_t * style, lv_coord_t value); -void lv_style_set_shadow_ofs_x(lv_style_t * style, lv_coord_t value); -void lv_style_set_shadow_ofs_y(lv_style_t * style, lv_coord_t value); -void lv_style_set_shadow_spread(lv_style_t * style, lv_coord_t value); +void lv_style_set_outline_pad(lv_style_t * style, int32_t value); +void lv_style_set_shadow_width(lv_style_t * style, int32_t value); +void lv_style_set_shadow_offset_x(lv_style_t * style, int32_t value); +void lv_style_set_shadow_offset_y(lv_style_t * style, int32_t value); +void lv_style_set_shadow_spread(lv_style_t * style, int32_t value); void lv_style_set_shadow_color(lv_style_t * style, lv_color_t value); void lv_style_set_shadow_opa(lv_style_t * style, lv_opa_t value); -void lv_style_set_img_opa(lv_style_t * style, lv_opa_t value); -void lv_style_set_img_recolor(lv_style_t * style, lv_color_t value); -void lv_style_set_img_recolor_opa(lv_style_t * style, lv_opa_t value); -void lv_style_set_line_width(lv_style_t * style, lv_coord_t value); -void lv_style_set_line_dash_width(lv_style_t * style, lv_coord_t value); -void lv_style_set_line_dash_gap(lv_style_t * style, lv_coord_t value); +void lv_style_set_image_opa(lv_style_t * style, lv_opa_t value); +void lv_style_set_image_recolor(lv_style_t * style, lv_color_t value); +void lv_style_set_image_recolor_opa(lv_style_t * style, lv_opa_t value); +void lv_style_set_line_width(lv_style_t * style, int32_t value); +void lv_style_set_line_dash_width(lv_style_t * style, int32_t value); +void lv_style_set_line_dash_gap(lv_style_t * style, int32_t value); void lv_style_set_line_rounded(lv_style_t * style, bool value); void lv_style_set_line_color(lv_style_t * style, lv_color_t value); void lv_style_set_line_opa(lv_style_t * style, lv_opa_t value); -void lv_style_set_arc_width(lv_style_t * style, lv_coord_t value); +void lv_style_set_arc_width(lv_style_t * style, int32_t value); void lv_style_set_arc_rounded(lv_style_t * style, bool value); void lv_style_set_arc_color(lv_style_t * style, lv_color_t value); void lv_style_set_arc_opa(lv_style_t * style, lv_opa_t value); -void lv_style_set_arc_img_src(lv_style_t * style, const void * value); +void lv_style_set_arc_image_src(lv_style_t * style, const void * value); void lv_style_set_text_color(lv_style_t * style, lv_color_t value); void lv_style_set_text_opa(lv_style_t * style, lv_opa_t value); void lv_style_set_text_font(lv_style_t * style, const lv_font_t * value); -void lv_style_set_text_letter_space(lv_style_t * style, lv_coord_t value); -void lv_style_set_text_line_space(lv_style_t * style, lv_coord_t value); +void lv_style_set_text_letter_space(lv_style_t * style, int32_t value); +void lv_style_set_text_line_space(lv_style_t * style, int32_t value); void lv_style_set_text_decor(lv_style_t * style, lv_text_decor_t value); void lv_style_set_text_align(lv_style_t * style, lv_text_align_t value); -void lv_style_set_radius(lv_style_t * style, lv_coord_t value); +void lv_style_set_radius(lv_style_t * style, int32_t value); void lv_style_set_clip_corner(lv_style_t * style, bool value); void lv_style_set_opa(lv_style_t * style, lv_opa_t value); +void lv_style_set_opa_layered(lv_style_t * style, lv_opa_t value); void lv_style_set_color_filter_dsc(lv_style_t * style, const lv_color_filter_dsc_t * value); void lv_style_set_color_filter_opa(lv_style_t * style, lv_opa_t value); void lv_style_set_anim(lv_style_t * style, const lv_anim_t * value); -void lv_style_set_anim_time(lv_style_t * style, uint32_t value); -void lv_style_set_anim_speed(lv_style_t * style, uint32_t value); +void lv_style_set_anim_duration(lv_style_t * style, uint32_t value); void lv_style_set_transition(lv_style_t * style, const lv_style_transition_dsc_t * value); void lv_style_set_blend_mode(lv_style_t * style, lv_blend_mode_t value); void lv_style_set_layout(lv_style_t * style, uint16_t value); void lv_style_set_base_dir(lv_style_t * style, lv_base_dir_t value); +void lv_style_set_bitmap_mask_src(lv_style_t * style, const void * value); +void lv_style_set_rotary_sensitivity(lv_style_t * style, uint32_t value); +#if LV_USE_FLEX +void lv_style_set_flex_flow(lv_style_t * style, lv_flex_flow_t value); +void lv_style_set_flex_main_place(lv_style_t * style, lv_flex_align_t value); +void lv_style_set_flex_cross_place(lv_style_t * style, lv_flex_align_t value); +void lv_style_set_flex_track_place(lv_style_t * style, lv_flex_align_t value); +void lv_style_set_flex_grow(lv_style_t * style, uint8_t value); +#endif /*LV_USE_FLEX*/ + +#if LV_USE_GRID +void lv_style_set_grid_column_dsc_array(lv_style_t * style, const int32_t * value); +void lv_style_set_grid_column_align(lv_style_t * style, lv_grid_align_t value); +void lv_style_set_grid_row_dsc_array(lv_style_t * style, const int32_t * value); +void lv_style_set_grid_row_align(lv_style_t * style, lv_grid_align_t value); +void lv_style_set_grid_cell_column_pos(lv_style_t * style, int32_t value); +void lv_style_set_grid_cell_x_align(lv_style_t * style, lv_grid_align_t value); +void lv_style_set_grid_cell_column_span(lv_style_t * style, int32_t value); +void lv_style_set_grid_cell_row_pos(lv_style_t * style, int32_t value); +void lv_style_set_grid_cell_y_align(lv_style_t * style, lv_grid_align_t value); +void lv_style_set_grid_cell_row_span(lv_style_t * style, int32_t value); +#endif /*LV_USE_GRID*/ + #define LV_STYLE_CONST_WIDTH(val) \ { \ @@ -113,6 +161,11 @@ void lv_style_set_base_dir(lv_style_t * style, lv_base_dir_t value); .prop = LV_STYLE_MAX_HEIGHT, .value = { .num = (int32_t)val } \ } +#define LV_STYLE_CONST_LENGTH(val) \ + { \ + .prop = LV_STYLE_LENGTH, .value = { .num = (int32_t)val } \ + } + #define LV_STYLE_CONST_X(val) \ { \ .prop = LV_STYLE_X, .value = { .num = (int32_t)val } \ @@ -148,14 +201,19 @@ void lv_style_set_base_dir(lv_style_t * style, lv_base_dir_t value); .prop = LV_STYLE_TRANSLATE_Y, .value = { .num = (int32_t)val } \ } -#define LV_STYLE_CONST_TRANSFORM_ZOOM(val) \ +#define LV_STYLE_CONST_TRANSFORM_SCALE_X(val) \ + { \ + .prop = LV_STYLE_TRANSFORM_SCALE_X, .value = { .num = (int32_t)val } \ + } + +#define LV_STYLE_CONST_TRANSFORM_SCALE_Y(val) \ { \ - .prop = LV_STYLE_TRANSFORM_ZOOM, .value = { .num = (int32_t)val } \ + .prop = LV_STYLE_TRANSFORM_SCALE_Y, .value = { .num = (int32_t)val } \ } -#define LV_STYLE_CONST_TRANSFORM_ANGLE(val) \ +#define LV_STYLE_CONST_TRANSFORM_ROTATION(val) \ { \ - .prop = LV_STYLE_TRANSFORM_ANGLE, .value = { .num = (int32_t)val } \ + .prop = LV_STYLE_TRANSFORM_ROTATION, .value = { .num = (int32_t)val } \ } #define LV_STYLE_CONST_TRANSFORM_PIVOT_X(val) \ @@ -168,6 +226,16 @@ void lv_style_set_base_dir(lv_style_t * style, lv_base_dir_t value); .prop = LV_STYLE_TRANSFORM_PIVOT_Y, .value = { .num = (int32_t)val } \ } +#define LV_STYLE_CONST_TRANSFORM_SKEW_X(val) \ + { \ + .prop = LV_STYLE_TRANSFORM_SKEW_X, .value = { .num = (int32_t)val } \ + } + +#define LV_STYLE_CONST_TRANSFORM_SKEW_Y(val) \ + { \ + .prop = LV_STYLE_TRANSFORM_SKEW_Y, .value = { .num = (int32_t)val } \ + } + #define LV_STYLE_CONST_PAD_TOP(val) \ { \ .prop = LV_STYLE_PAD_TOP, .value = { .num = (int32_t)val } \ @@ -198,6 +266,26 @@ void lv_style_set_base_dir(lv_style_t * style, lv_base_dir_t value); .prop = LV_STYLE_PAD_COLUMN, .value = { .num = (int32_t)val } \ } +#define LV_STYLE_CONST_MARGIN_TOP(val) \ + { \ + .prop = LV_STYLE_MARGIN_TOP, .value = { .num = (int32_t)val } \ + } + +#define LV_STYLE_CONST_MARGIN_BOTTOM(val) \ + { \ + .prop = LV_STYLE_MARGIN_BOTTOM, .value = { .num = (int32_t)val } \ + } + +#define LV_STYLE_CONST_MARGIN_LEFT(val) \ + { \ + .prop = LV_STYLE_MARGIN_LEFT, .value = { .num = (int32_t)val } \ + } + +#define LV_STYLE_CONST_MARGIN_RIGHT(val) \ + { \ + .prop = LV_STYLE_MARGIN_RIGHT, .value = { .num = (int32_t)val } \ + } + #define LV_STYLE_CONST_BG_COLOR(val) \ { \ .prop = LV_STYLE_BG_COLOR, .value = { .color = val } \ @@ -228,39 +316,44 @@ void lv_style_set_base_dir(lv_style_t * style, lv_base_dir_t value); .prop = LV_STYLE_BG_GRAD_STOP, .value = { .num = (int32_t)val } \ } -#define LV_STYLE_CONST_BG_GRAD(val) \ +#define LV_STYLE_CONST_BG_MAIN_OPA(val) \ { \ - .prop = LV_STYLE_BG_GRAD, .value = { .ptr = val } \ + .prop = LV_STYLE_BG_MAIN_OPA, .value = { .num = (int32_t)val } \ + } + +#define LV_STYLE_CONST_BG_GRAD_OPA(val) \ + { \ + .prop = LV_STYLE_BG_GRAD_OPA, .value = { .num = (int32_t)val } \ } -#define LV_STYLE_CONST_BG_DITHER_MODE(val) \ +#define LV_STYLE_CONST_BG_GRAD(val) \ { \ - .prop = LV_STYLE_BG_DITHER_MODE, .value = { .num = (int32_t)val } \ + .prop = LV_STYLE_BG_GRAD, .value = { .ptr = val } \ } -#define LV_STYLE_CONST_BG_IMG_SRC(val) \ +#define LV_STYLE_CONST_BG_IMAGE_SRC(val) \ { \ - .prop = LV_STYLE_BG_IMG_SRC, .value = { .ptr = val } \ + .prop = LV_STYLE_BG_IMAGE_SRC, .value = { .ptr = val } \ } -#define LV_STYLE_CONST_BG_IMG_OPA(val) \ +#define LV_STYLE_CONST_BG_IMAGE_OPA(val) \ { \ - .prop = LV_STYLE_BG_IMG_OPA, .value = { .num = (int32_t)val } \ + .prop = LV_STYLE_BG_IMAGE_OPA, .value = { .num = (int32_t)val } \ } -#define LV_STYLE_CONST_BG_IMG_RECOLOR(val) \ +#define LV_STYLE_CONST_BG_IMAGE_RECOLOR(val) \ { \ - .prop = LV_STYLE_BG_IMG_RECOLOR, .value = { .color = val } \ + .prop = LV_STYLE_BG_IMAGE_RECOLOR, .value = { .color = val } \ } -#define LV_STYLE_CONST_BG_IMG_RECOLOR_OPA(val) \ +#define LV_STYLE_CONST_BG_IMAGE_RECOLOR_OPA(val) \ { \ - .prop = LV_STYLE_BG_IMG_RECOLOR_OPA, .value = { .num = (int32_t)val } \ + .prop = LV_STYLE_BG_IMAGE_RECOLOR_OPA, .value = { .num = (int32_t)val } \ } -#define LV_STYLE_CONST_BG_IMG_TILED(val) \ +#define LV_STYLE_CONST_BG_IMAGE_TILED(val) \ { \ - .prop = LV_STYLE_BG_IMG_TILED, .value = { .num = (int32_t)val } \ + .prop = LV_STYLE_BG_IMAGE_TILED, .value = { .num = (int32_t)val } \ } #define LV_STYLE_CONST_BORDER_COLOR(val) \ @@ -313,14 +406,14 @@ void lv_style_set_base_dir(lv_style_t * style, lv_base_dir_t value); .prop = LV_STYLE_SHADOW_WIDTH, .value = { .num = (int32_t)val } \ } -#define LV_STYLE_CONST_SHADOW_OFS_X(val) \ +#define LV_STYLE_CONST_SHADOW_OFFSET_X(val) \ { \ - .prop = LV_STYLE_SHADOW_OFS_X, .value = { .num = (int32_t)val } \ + .prop = LV_STYLE_SHADOW_OFFSET_X, .value = { .num = (int32_t)val } \ } -#define LV_STYLE_CONST_SHADOW_OFS_Y(val) \ +#define LV_STYLE_CONST_SHADOW_OFFSET_Y(val) \ { \ - .prop = LV_STYLE_SHADOW_OFS_Y, .value = { .num = (int32_t)val } \ + .prop = LV_STYLE_SHADOW_OFFSET_Y, .value = { .num = (int32_t)val } \ } #define LV_STYLE_CONST_SHADOW_SPREAD(val) \ @@ -338,19 +431,19 @@ void lv_style_set_base_dir(lv_style_t * style, lv_base_dir_t value); .prop = LV_STYLE_SHADOW_OPA, .value = { .num = (int32_t)val } \ } -#define LV_STYLE_CONST_IMG_OPA(val) \ +#define LV_STYLE_CONST_IMAGE_OPA(val) \ { \ - .prop = LV_STYLE_IMG_OPA, .value = { .num = (int32_t)val } \ + .prop = LV_STYLE_IMAGE_OPA, .value = { .num = (int32_t)val } \ } -#define LV_STYLE_CONST_IMG_RECOLOR(val) \ +#define LV_STYLE_CONST_IMAGE_RECOLOR(val) \ { \ - .prop = LV_STYLE_IMG_RECOLOR, .value = { .color = val } \ + .prop = LV_STYLE_IMAGE_RECOLOR, .value = { .color = val } \ } -#define LV_STYLE_CONST_IMG_RECOLOR_OPA(val) \ +#define LV_STYLE_CONST_IMAGE_RECOLOR_OPA(val) \ { \ - .prop = LV_STYLE_IMG_RECOLOR_OPA, .value = { .num = (int32_t)val } \ + .prop = LV_STYLE_IMAGE_RECOLOR_OPA, .value = { .num = (int32_t)val } \ } #define LV_STYLE_CONST_LINE_WIDTH(val) \ @@ -403,9 +496,9 @@ void lv_style_set_base_dir(lv_style_t * style, lv_base_dir_t value); .prop = LV_STYLE_ARC_OPA, .value = { .num = (int32_t)val } \ } -#define LV_STYLE_CONST_ARC_IMG_SRC(val) \ +#define LV_STYLE_CONST_ARC_IMAGE_SRC(val) \ { \ - .prop = LV_STYLE_ARC_IMG_SRC, .value = { .ptr = val } \ + .prop = LV_STYLE_ARC_IMAGE_SRC, .value = { .ptr = val } \ } #define LV_STYLE_CONST_TEXT_COLOR(val) \ @@ -458,6 +551,11 @@ void lv_style_set_base_dir(lv_style_t * style, lv_base_dir_t value); .prop = LV_STYLE_OPA, .value = { .num = (int32_t)val } \ } +#define LV_STYLE_CONST_OPA_LAYERED(val) \ + { \ + .prop = LV_STYLE_OPA_LAYERED, .value = { .num = (int32_t)val } \ + } + #define LV_STYLE_CONST_COLOR_FILTER_DSC(val) \ { \ .prop = LV_STYLE_COLOR_FILTER_DSC, .value = { .ptr = val } \ @@ -473,14 +571,9 @@ void lv_style_set_base_dir(lv_style_t * style, lv_base_dir_t value); .prop = LV_STYLE_ANIM, .value = { .ptr = val } \ } -#define LV_STYLE_CONST_ANIM_TIME(val) \ +#define LV_STYLE_CONST_ANIM_DURATION(val) \ { \ - .prop = LV_STYLE_ANIM_TIME, .value = { .num = (int32_t)val } \ - } - -#define LV_STYLE_CONST_ANIM_SPEED(val) \ - { \ - .prop = LV_STYLE_ANIM_SPEED, .value = { .num = (int32_t)val } \ + .prop = LV_STYLE_ANIM_DURATION, .value = { .num = (int32_t)val } \ } #define LV_STYLE_CONST_TRANSITION(val) \ @@ -502,3 +595,100 @@ void lv_style_set_base_dir(lv_style_t * style, lv_base_dir_t value); { \ .prop = LV_STYLE_BASE_DIR, .value = { .num = (int32_t)val } \ } + +#define LV_STYLE_CONST_BITMAP_MASK_SRC(val) \ + { \ + .prop = LV_STYLE_BITMAP_MASK_SRC, .value = { .ptr = val } \ + } + +#define LV_STYLE_CONST_ROTARY_SENSITIVITY(val) \ + { \ + .prop = LV_STYLE_ROTARY_SENSITIVITY, .value = { .num = (int32_t)val } \ + } +#if LV_USE_FLEX + +#define LV_STYLE_CONST_FLEX_FLOW(val) \ + { \ + .prop = LV_STYLE_FLEX_FLOW, .value = { .num = (int32_t)val } \ + } + +#define LV_STYLE_CONST_FLEX_MAIN_PLACE(val) \ + { \ + .prop = LV_STYLE_FLEX_MAIN_PLACE, .value = { .num = (int32_t)val } \ + } + +#define LV_STYLE_CONST_FLEX_CROSS_PLACE(val) \ + { \ + .prop = LV_STYLE_FLEX_CROSS_PLACE, .value = { .num = (int32_t)val } \ + } + +#define LV_STYLE_CONST_FLEX_TRACK_PLACE(val) \ + { \ + .prop = LV_STYLE_FLEX_TRACK_PLACE, .value = { .num = (int32_t)val } \ + } + +#define LV_STYLE_CONST_FLEX_GROW(val) \ + { \ + .prop = LV_STYLE_FLEX_GROW, .value = { .num = (int32_t)val } \ + } +#endif /*LV_USE_FLEX*/ + +#if LV_USE_GRID + +#define LV_STYLE_CONST_GRID_COLUMN_DSC_ARRAY(val) \ + { \ + .prop = LV_STYLE_GRID_COLUMN_DSC_ARRAY, .value = { .ptr = val } \ + } + +#define LV_STYLE_CONST_GRID_COLUMN_ALIGN(val) \ + { \ + .prop = LV_STYLE_GRID_COLUMN_ALIGN, .value = { .num = (int32_t)val } \ + } + +#define LV_STYLE_CONST_GRID_ROW_DSC_ARRAY(val) \ + { \ + .prop = LV_STYLE_GRID_ROW_DSC_ARRAY, .value = { .ptr = val } \ + } + +#define LV_STYLE_CONST_GRID_ROW_ALIGN(val) \ + { \ + .prop = LV_STYLE_GRID_ROW_ALIGN, .value = { .num = (int32_t)val } \ + } + +#define LV_STYLE_CONST_GRID_CELL_COLUMN_POS(val) \ + { \ + .prop = LV_STYLE_GRID_CELL_COLUMN_POS, .value = { .num = (int32_t)val } \ + } + +#define LV_STYLE_CONST_GRID_CELL_X_ALIGN(val) \ + { \ + .prop = LV_STYLE_GRID_CELL_X_ALIGN, .value = { .num = (int32_t)val } \ + } + +#define LV_STYLE_CONST_GRID_CELL_COLUMN_SPAN(val) \ + { \ + .prop = LV_STYLE_GRID_CELL_COLUMN_SPAN, .value = { .num = (int32_t)val } \ + } + +#define LV_STYLE_CONST_GRID_CELL_ROW_POS(val) \ + { \ + .prop = LV_STYLE_GRID_CELL_ROW_POS, .value = { .num = (int32_t)val } \ + } + +#define LV_STYLE_CONST_GRID_CELL_Y_ALIGN(val) \ + { \ + .prop = LV_STYLE_GRID_CELL_Y_ALIGN, .value = { .num = (int32_t)val } \ + } + +#define LV_STYLE_CONST_GRID_CELL_ROW_SPAN(val) \ + { \ + .prop = LV_STYLE_GRID_CELL_ROW_SPAN, .value = { .num = (int32_t)val } \ + } +#endif /*LV_USE_GRID*/ + + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LV_STYLE_GEN_H */ diff --git a/include/liblvgl/extra/libs/sjpg/lv_sjpg.h b/include/liblvgl/misc/lv_style_private.h similarity index 72% rename from include/liblvgl/extra/libs/sjpg/lv_sjpg.h rename to include/liblvgl/misc/lv_style_private.h index d06e80de..8a88185d 100644 --- a/include/liblvgl/extra/libs/sjpg/lv_sjpg.h +++ b/include/liblvgl/misc/lv_style_private.h @@ -1,10 +1,10 @@ /** - * @file lv_sjpg.h + * @file lv_style_private.h * */ -#ifndef LV_SJPEG_H -#define LV_SJPEG_H +#ifndef LV_STYLE_PRIVATE_H +#define LV_STYLE_PRIVATE_H #ifdef __cplusplus extern "C" { @@ -14,7 +14,7 @@ extern "C" { * INCLUDES *********************/ -#if LV_USE_SJPG +#include "lv_style.h" /********************* * DEFINES @@ -28,16 +28,12 @@ extern "C" { * GLOBAL PROTOTYPES **********************/ -void lv_split_jpeg_init(void); - /********************** * MACROS **********************/ -#endif /*LV_USE_SJPG*/ - #ifdef __cplusplus -} +} /*extern "C"*/ #endif -#endif /* LV_SJPEG_H */ +#endif /*LV_STYLE_PRIVATE_H*/ diff --git a/include/liblvgl/misc/lv_text.h b/include/liblvgl/misc/lv_text.h new file mode 100644 index 00000000..1381c36b --- /dev/null +++ b/include/liblvgl/misc/lv_text.h @@ -0,0 +1,125 @@ +/** + * @file lv_text.h + * + */ + +#ifndef LV_TEXT_H +#define LV_TEXT_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../lv_conf_internal.h" + +#include "lv_types.h" +#include "lv_area.h" +#include "../font/lv_font.h" +#include "../stdlib/lv_sprintf.h" + +/********************* + * DEFINES + *********************/ +#ifndef LV_TXT_COLOR_CMD +#define LV_TXT_COLOR_CMD "#" +#endif + +#define LV_TXT_ENC_UTF8 1 +#define LV_TXT_ENC_ASCII 2 + +/********************** + * TYPEDEFS + **********************/ + +/** + * Options for text rendering. + */ + +typedef enum { + LV_TEXT_FLAG_NONE = 0x00, + LV_TEXT_FLAG_EXPAND = 0x01, /**< Ignore max-width to avoid automatic word wrapping*/ + LV_TEXT_FLAG_FIT = 0x02, /**< Max-width is already equal to the longest line. (Used to skip some calculation)*/ + LV_TEXT_FLAG_BREAK_ALL = 0x04, /**< To prevent overflow, insert breaks between any two characters. + Otherwise breaks are inserted at word boundaries, as configured via LV_TXT_BREAK_CHARS + or according to LV_TXT_LINE_BREAK_LONG_LEN, LV_TXT_LINE_BREAK_LONG_PRE_MIN_LEN, + and LV_TXT_LINE_BREAK_LONG_POST_MIN_LEN.*/ + LV_TEXT_FLAG_RECOLOR = 0x08, /**< Enable parsing of recolor command*/ +} lv_text_flag_t; + +/** Label align policy*/ +typedef enum { + LV_TEXT_ALIGN_AUTO, /**< Align text auto*/ + LV_TEXT_ALIGN_LEFT, /**< Align text to left*/ + LV_TEXT_ALIGN_CENTER, /**< Align text to center*/ + LV_TEXT_ALIGN_RIGHT, /**< Align text to right*/ +} lv_text_align_t; + +/** State machine for text renderer. */ +typedef enum { + LV_TEXT_CMD_STATE_WAIT, /**< Waiting for command*/ + LV_TEXT_CMD_STATE_PAR, /**< Processing the parameter*/ + LV_TEXT_CMD_STATE_IN, /**< Processing the command*/ +} lv_text_cmd_state_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Get size of a text + * @param size_res pointer to a 'point_t' variable to store the result + * @param text pointer to a text + * @param font pointer to font of the text + * @param letter_space letter space of the text + * @param line_space line space of the text + * @param max_width max width of the text (break the lines to fit this size). Set COORD_MAX to avoid + * @param flag settings for the text from ::lv_text_flag_t + + * line breaks + */ +void lv_text_get_size(lv_point_t * size_res, const char * text, const lv_font_t * font, int32_t letter_space, + int32_t line_space, int32_t max_width, lv_text_flag_t flag); + +/** + * Give the length of a text with a given font + * @param txt a '\0' terminate string + * @param length length of 'txt' in byte count and not characters (Á is 1 character but 2 bytes in + * UTF-8) + * @param font pointer to a font + * @param letter_space letter space + * @return length of a char_num long text + */ +int32_t lv_text_get_width(const char * txt, uint32_t length, const lv_font_t * font, int32_t letter_space); + +/** + * Give the length of a text with a given font with text flags + * @param txt a '\0' terminate string + * @param length length of 'txt' in byte count and not characters (Á is 1 character but 2 bytes in + * UTF-8) + * @param font pointer to a font + * @param letter_space letter space + * @param flags settings for the text from ::lv_text_flag_t + * @return length of a char_num long text + */ +int32_t lv_text_get_width_with_flags(const char * txt, uint32_t length, const lv_font_t * font, int32_t letter_space, + lv_text_flag_t flags); + +/** + * Check if c is command state + * @param state + * @param c + * @return True if c is state + */ +bool lv_text_is_cmd(lv_text_cmd_state_t * state, uint32_t c); +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_TEXT_H*/ diff --git a/include/liblvgl/misc/lv_txt_ap.h b/include/liblvgl/misc/lv_text_ap.h similarity index 71% rename from include/liblvgl/misc/lv_txt_ap.h rename to include/liblvgl/misc/lv_text_ap.h index 3519bb60..cac7c712 100644 --- a/include/liblvgl/misc/lv_txt_ap.h +++ b/include/liblvgl/misc/lv_text_ap.h @@ -1,10 +1,10 @@ /** - * @file lv_txt_ap.h + * @file lv_text_ap.h * */ -#ifndef LV_TXT_AP_H -#define LV_TXT_AP_H +#ifndef LV_TEXT_AP_H +#define LV_TEXT_AP_H #ifdef __cplusplus extern "C" { @@ -13,9 +13,9 @@ extern "C" { /********************* * INCLUDES *********************/ -#include -#include "lv_txt.h" -#include "liblvgl/draw/lv_draw.h" +#include "lv_text.h" +#include "lv_types.h" +#include "../draw/lv_draw.h" #if LV_USE_ARABIC_PERSIAN_CHARS == 1 @@ -33,8 +33,8 @@ extern "C" { /********************** * GLOBAL PROTOTYPES **********************/ -uint32_t _lv_txt_ap_calc_bytes_cnt(const char * txt); -void _lv_txt_ap_proc(const char * txt, char * txt_out); +uint32_t lv_text_ap_calc_bytes_count(const char * txt); +void lv_text_ap_proc(const char * txt, char * txt_out); /********************** * MACROS @@ -46,4 +46,4 @@ void _lv_txt_ap_proc(const char * txt, char * txt_out); } /*extern "C"*/ #endif -#endif /*LV_TXT_AP_H*/ +#endif /*LV_TEXT_AP_H*/ diff --git a/include/liblvgl/misc/lv_text_private.h b/include/liblvgl/misc/lv_text_private.h new file mode 100644 index 00000000..858f5938 --- /dev/null +++ b/include/liblvgl/misc/lv_text_private.h @@ -0,0 +1,273 @@ +/** + * @file lv_text_private.h + * + */ + +#ifndef LV_TEXT_PRIVATE_H +#define LV_TEXT_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_text.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Get the next line of text. Check line length and break chars too. + * @param txt a '\0' terminated string + * @param font pointer to a font + * @param letter_space letter space + * @param max_width max width of the text (break the lines to fit this size). Set COORD_MAX to avoid + * line breaks + * @param used_width When used_width != NULL, save the width of this line if + * flag == LV_TEXT_FLAG_NONE, otherwise save -1. + * @param flag settings for the text from 'txt_flag_type' enum + * @return the index of the first char of the new line (in byte index not letter index. With UTF-8 + * they are different) + */ +uint32_t lv_text_get_next_line(const char * txt, const lv_font_t * font, int32_t letter_space, + int32_t max_width, int32_t * used_width, lv_text_flag_t flag); + +/** + * Insert a string into another + * @param txt_buf the original text (must be big enough for the result text and NULL terminated) + * @param pos position to insert (0: before the original text, 1: after the first char etc.) + * @param ins_txt text to insert, must be '\0' terminated + */ +void lv_text_ins(char * txt_buf, uint32_t pos, const char * ins_txt); + +/** + * Delete a part of a string + * @param txt string to modify, must be '\0' terminated and should point to a heap or stack frame, not read-only memory. + * @param pos position where to start the deleting (0: before the first char, 1: after the first + * char etc.) + * @param len number of characters to delete + */ +void lv_text_cut(char * txt, uint32_t pos, uint32_t len); + +/** + * return a new formatted text. Memory will be allocated to store the text. + * @param fmt `printf`-like format + * @param ap items to print + + * @return pointer to the allocated text string. + */ +char * lv_text_set_text_vfmt(const char * fmt, va_list ap) LV_FORMAT_ATTRIBUTE(1, 0); + +/** + * Decode two encoded character from a string. + * @param txt pointer to '\0' terminated string + * @param letter the first decoded Unicode character or 0 on invalid data code + * @param letter_next the second decoded Unicode character or 0 on invalid data code + * @param ofs start index in 'txt' where to start. + * After the call it will point to the next encoded char in 'txt'. + * NULL to use txt[0] as index + */ +void lv_text_encoded_letter_next_2(const char * txt, uint32_t * letter, uint32_t * letter_next, uint32_t * ofs); + +/** + * Test if char is break char or not (a text can broken here or not) + * @param letter a letter + * @return false: 'letter' is not break char + */ +static inline bool lv_text_is_break_char(uint32_t letter) +{ + uint8_t i; + bool ret = false; + + /*Compare the letter to TXT_BREAK_CHARS*/ + for(i = 0; LV_TXT_BREAK_CHARS[i] != '\0'; i++) { + if(letter == (uint32_t)LV_TXT_BREAK_CHARS[i]) { + ret = true; /*If match then it is break char*/ + break; + } + } + + return ret; +} + +/** + * Test if char is break char or not (a text can broken here or not) + * @param letter a letter + * @return false: 'letter' is not break char + */ +static inline bool lv_text_is_a_word(uint32_t letter) +{ + /*Cheap check on invalid letter*/ + if(letter == 0) return false; + + /*CJK Unified Ideographs*/ + if(letter >= 0x4E00 && letter <= 0x9FFF) { + return true; + } + + /*Fullwidth ASCII variants*/ + if(letter >= 0xFF01 && letter <= 0xFF5E) { + return true; + } + + /*CJK symbols and punctuation*/ + if(letter >= 0x3000 && letter <= 0x303F) { + return true; + } + + /*CJK Radicals Supplement*/ + if(letter >= 0x2E80 && letter <= 0x2EFF) { + return true; + } + + /*CJK Strokes*/ + if(letter >= 0x31C0 && letter <= 0x31EF) { + return true; + } + + /*Hiragana and Katakana*/ + if(letter >= 0x3040 && letter <= 0x30FF) { + return true; + } + + /*Chinese Vertical Forms*/ + if(letter >= 0xFE10 && letter <= 0xFE1F) { + return true; + } + + /*CJK Compatibility Forms*/ + if(letter >= 0xFE30 && letter <= 0xFE4F) { + return true; + } + + return false; +} + +/** + * Test if character can be treated as marker, and don't need to be rendered. + * Note, this is not a full list. Add your findings to the list. + * + * @param letter a letter + * @return true if so + */ +static inline bool lv_text_is_marker(uint32_t letter) +{ + if(letter < 0x20) return true; + + /*U+061C ARABIC LETTER MARK, see https://www.compart.com/en/unicode/block/U+0600*/ + if(letter == 0x061C) return true; + + /*U+115F HANGUL CHOSEONG FILLER, See https://www.compart.com/en/unicode/block/U+1100*/ + if(letter == 0x115F) return true; + /*U+1160 HANGUL JUNGSEONG FILLER*/ + if(letter == 0x1160) return true; + + /*See https://www.compart.com/en/unicode/block/U+1800*/ + if(letter >= 0x180B && letter <= 0x180E) return true; + + /*See https://www.compart.com/en/unicode/block/U+2000*/ + if(letter >= 0x200B && letter <= 0x200F) return true; + if(letter >= 0x2028 && letter <= 0x202F) return true; + if(letter >= 0x205F && letter <= 0x206F) return true; + + /*U+FEFF ZERO WIDTH NO-BREAK SPACE, see https://www.compart.com/en/unicode/block/U+FE70*/ + if(letter == 0xFEFF) return true; + + if(letter == 0xF8FF) return true; /*LV_SYMBOL_DUMMY*/ + + return false; +} + +/*************************************************************** + * GLOBAL FUNCTION POINTERS FOR CHARACTER ENCODING INTERFACE + ***************************************************************/ + +/** + * Give the size of an encoded character + * @param txt pointer to a character in a string + * @return length of the encoded character (1,2,3 ...). O in invalid + */ +extern uint8_t (*const lv_text_encoded_size)(const char * txt); + +/** + * Convert a Unicode letter to encoded + * @param letter_uni a Unicode letter + * @return Encoded character in Little Endian to be compatible with C chars (e.g. 'Á', 'Ü') + */ +extern uint32_t (*const lv_text_unicode_to_encoded)(uint32_t letter_uni); + +/** + * Convert a wide character, e.g. 'Á' little endian to be compatible with the encoded format. + * @param c a wide character + * @return `c` in the encoded format + */ +extern uint32_t (*const lv_text_encoded_conv_wc)(uint32_t c); + +/** + * Decode the next encoded character from a string. + * @param txt pointer to '\0' terminated string + * @param i_start start index in 'txt' where to start. + * After the call it will point to the next encoded char in 'txt'. + * NULL to use txt[0] as index + * @return the decoded Unicode character or 0 on invalid data code + */ +extern uint32_t (*const lv_text_encoded_next)(const char * txt, uint32_t * i_start); + +/** + * Get the previous encoded character form a string. + * + * @param txt pointer to '\0' terminated string + * @param i_start index in 'txt' where to start. After the call it will point to the previous + * encoded char in 'txt'. + * + * @return the decoded Unicode character or 0 on invalid data + */ +extern uint32_t (*const lv_text_encoded_prev)(const char * txt, uint32_t * i_start); + +/** + * Convert a letter index (in the encoded text) to byte index. + * E.g. in UTF-8 "AÁRT" index of 'R' is 2 but start at byte 3 because 'Á' is 2 bytes long + * @param txt a '\0' terminated UTF-8 string + * @param utf8_id character index + * @return byte index of the 'enc_id'th letter + */ +extern uint32_t (*const lv_text_encoded_get_byte_id)(const char * txt, uint32_t utf8_id); + +/** + * Convert a byte index (in an encoded text) to character index. + * E.g. in UTF-8 "AÁRT" index of 'R' is 2 but start at byte 3 because 'Á' is 2 bytes long + * @param txt a '\0' terminated UTF-8 string + * @param byte_id byte index + * @return character index of the letter at 'byte_id'th position + */ +extern uint32_t (*const lv_text_encoded_get_char_id)(const char * txt, uint32_t byte_id); + +/** + * Get the number of characters (and NOT bytes) in a string. + * E.g. in UTF-8 "ÁBC" is 3 characters (but 4 bytes) + * @param txt a '\0' terminated char string + * @return number of characters + */ +extern uint32_t (*const lv_text_get_encoded_length)(const char * txt); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_TEXT_PRIVATE_H*/ diff --git a/include/liblvgl/misc/lv_timer.h b/include/liblvgl/misc/lv_timer.h index 16572d3d..be250033 100644 --- a/include/liblvgl/misc/lv_timer.h +++ b/include/liblvgl/misc/lv_timer.h @@ -12,11 +12,10 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" -#include "liblvgl/hal/lv_hal_tick.h" - -#include -#include +#include "../lv_conf_internal.h" +#include "../tick/lv_tick.h" +#include "lv_types.h" +#include "lv_ll.h" /********************* * DEFINES @@ -31,34 +30,20 @@ extern "C" { * TYPEDEFS **********************/ -struct _lv_timer_t; - /** * Timers execute this type of functions. */ -typedef void (*lv_timer_cb_t)(struct _lv_timer_t *); +typedef void (*lv_timer_cb_t)(lv_timer_t *); /** - * Descriptor of a lv_timer + * Timer handler resume this type of function. */ -typedef struct _lv_timer_t { - uint32_t period; /**< How often the timer should run*/ - uint32_t last_run; /**< Last time the timer ran*/ - lv_timer_cb_t timer_cb; /**< Timer function*/ - void * user_data; /**< Custom user data*/ - int32_t repeat_count; /**< 1: One time; -1 : infinity; n>0: residual times*/ - uint32_t paused : 1; -} lv_timer_t; +typedef void (*lv_timer_handler_resume_cb_t)(void * data); /********************** * GLOBAL PROTOTYPES **********************/ -/** - * Init the lv_timer module - */ -void _lv_timer_core_init(void); - //! @cond Doxygen_Suppress /** @@ -73,22 +58,26 @@ LV_ATTRIBUTE_TIMER_HANDLER uint32_t lv_timer_handler(void); * Call it in the super-loop of main() or threads. It will run lv_timer_handler() * with a given period in ms. You can use it with sleep or delay in OS environment. * This function is used to simplify the porting. - * @param __ms the period for running lv_timer_handler() + * @param period the period for running lv_timer_handler() + * @return the time after which it must be called again */ -static inline LV_ATTRIBUTE_TIMER_HANDLER uint32_t lv_timer_handler_run_in_period(uint32_t ms) -{ - static uint32_t last_tick = 0; - uint32_t curr_tick = lv_tick_get(); +LV_ATTRIBUTE_TIMER_HANDLER uint32_t lv_timer_handler_run_in_period(uint32_t period); - if((curr_tick - last_tick) >= (ms)) { - last_tick = curr_tick; - return lv_timer_handler(); - } - return 1; -} +/** + * Call it in the super-loop of main() or threads. It will automatically call lv_timer_handler() at the right time. + * This function is used to simplify the porting. + */ +LV_ATTRIBUTE_TIMER_HANDLER void lv_timer_periodic_handler(void); /** - * Create an "empty" timer. It needs to initialized with at least + * Set the resume callback to the timer handler + * @param cb the function to call when timer handler is resumed + * @param data pointer to a resume data + */ +void lv_timer_handler_set_resume_cb(lv_timer_handler_resume_cb_t cb, void * data); + +/** + * Create an "empty" timer. It needs to be initialized with at least * `lv_timer_set_cb` and `lv_timer_set_period` * @return pointer to the created timer */ @@ -109,18 +98,22 @@ lv_timer_t * lv_timer_create(lv_timer_cb_t timer_xcb, uint32_t period, void * us * Delete a lv_timer * @param timer pointer to an lv_timer */ -void lv_timer_del(lv_timer_t * timer); +void lv_timer_delete(lv_timer_t * timer); /** - * Pause/resume a timer. + * Pause a timer. * @param timer pointer to an lv_timer */ void lv_timer_pause(lv_timer_t * timer); +/** + * Resume a timer. + * @param timer pointer to an lv_timer + */ void lv_timer_resume(lv_timer_t * timer); /** - * Set the callback the timer (the function to call periodically) + * Set the callback to the timer (the function to call periodically) * @param timer pointer to a timer * @param timer_cb the function to call periodically */ @@ -146,6 +139,20 @@ void lv_timer_ready(lv_timer_t * timer); */ void lv_timer_set_repeat_count(lv_timer_t * timer, int32_t repeat_count); +/** + * Set whether a lv_timer will be deleted automatically when it is called `repeat_count` times. + * @param timer pointer to a lv_timer. + * @param auto_delete true: auto delete; false: timer will be paused when it is called `repeat_count` times. + */ +void lv_timer_set_auto_delete(lv_timer_t * timer, bool auto_delete); + +/** + * Set custom parameter to the lv_timer. + * @param timer pointer to a lv_timer. + * @param user_data custom parameter + */ +void lv_timer_set_user_data(lv_timer_t * timer, void * user_data); + /** * Reset a lv_timer. * It will be called the previously set period milliseconds later. @@ -163,7 +170,13 @@ void lv_timer_enable(bool en); * Get idle percentage * @return the lv_timer idle in percentage */ -uint8_t lv_timer_get_idle(void); +uint32_t lv_timer_get_idle(void); + +/** + * Get the time remaining until the next timer will run + * @return the time remaining in ms + */ +uint32_t lv_timer_get_time_until_next(void); /** * Iterate through the timers @@ -172,6 +185,20 @@ uint8_t lv_timer_get_idle(void); */ lv_timer_t * lv_timer_get_next(lv_timer_t * timer); +/** + * Get the user_data passed when the timer was created + * @param timer pointer to the lv_timer + * @return pointer to the user_data + */ +void * lv_timer_get_user_data(lv_timer_t * timer); + +/** + * Get the pause state of a timer + * @param timer pointer to a lv_timer + * @return true: timer is paused; false: timer is running + */ +bool lv_timer_get_paused(lv_timer_t * timer); + /********************** * MACROS **********************/ diff --git a/include/liblvgl/misc/lv_timer_private.h b/include/liblvgl/misc/lv_timer_private.h new file mode 100644 index 00000000..9f7269f5 --- /dev/null +++ b/include/liblvgl/misc/lv_timer_private.h @@ -0,0 +1,81 @@ +/** + * @file lv_timer_private.h + * + */ + +#ifndef LV_TIMER_PRIVATE_H +#define LV_TIMER_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_timer.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** + * Descriptor of a lv_timer + */ +struct _lv_timer_t { + uint32_t period; /**< How often the timer should run */ + uint32_t last_run; /**< Last time the timer ran */ + lv_timer_cb_t timer_cb; /**< Timer function */ + void * user_data; /**< Custom user data */ + int32_t repeat_count; /**< 1: One time; -1 : infinity; n>0: residual times */ + uint32_t paused : 1; + uint32_t auto_delete : 1; +}; + +typedef struct { + lv_ll_t timer_ll; /**< Linked list to store the lv_timers */ + + bool lv_timer_run; + uint8_t idle_last; + bool timer_deleted; + bool timer_created; + uint32_t timer_time_until_next; + + bool already_running; + uint32_t periodic_last_tick; + uint32_t busy_time; + uint32_t idle_period_start; + uint32_t run_cnt; + + lv_timer_handler_resume_cb_t resume_cb; + void * resume_data; +} lv_timer_state_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Init the lv_timer module + */ +void lv_timer_core_init(void); + +/** + * Deinit the lv_timer module + */ +void lv_timer_core_deinit(void); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_TIMER_PRIVATE_H*/ diff --git a/include/liblvgl/misc/lv_tree.h b/include/liblvgl/misc/lv_tree.h new file mode 100644 index 00000000..e058a03c --- /dev/null +++ b/include/liblvgl/misc/lv_tree.h @@ -0,0 +1,105 @@ +/** + * @file lv_tree.h + * Tree. The tree nodes are dynamically allocated by the 'lv_mem' module. + */ + +#ifndef LV_TREE_H +#define LV_TREE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "lv_types.h" + +/********************* + * DEFINES + *********************/ + +#define LV_TREE_NODE(n) ((lv_tree_node_t*)(n)) + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_tree_node_t; + +/** + * Describe the common methods of every object. + * Similar to a C++ class. + */ +typedef struct _lv_tree_class_t { + const struct _lv_tree_class_t * base_class; + uint32_t instance_size; + void (*constructor_cb)(const struct _lv_tree_class_t * class_p, struct _lv_tree_node_t * node); + void (*destructor_cb)(const struct _lv_tree_class_t * class_p, struct _lv_tree_node_t * node); +} lv_tree_class_t; + +/** Description of a tree node*/ +typedef struct _lv_tree_node_t { + struct _lv_tree_node_t * parent; + struct _lv_tree_node_t ** children; + uint32_t child_cnt; + uint32_t child_cap; + const struct _lv_tree_class_t * class_p; +} lv_tree_node_t; + +enum { + LV_TREE_WALK_PRE_ORDER = 0, + LV_TREE_WALK_POST_ORDER, +}; +typedef uint8_t lv_tree_walk_mode_t; + +typedef bool (*lv_tree_traverse_cb_t)(const lv_tree_node_t * node, void * user_data); +typedef bool (*lv_tree_before_cb_t)(const lv_tree_node_t * node, void * user_data); +typedef void (*lv_tree_after_cb_t)(const lv_tree_node_t * node, void * user_data); + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +extern const lv_tree_class_t lv_tree_node_class; + +/** + * @brief Create a tree node + * @param class_p pointer to a class of the node + * @param parent pointer to the parent node (or NULL if it's the root node) + * @return pointer to the new node + */ +lv_tree_node_t * lv_tree_node_create(const lv_tree_class_t * class_p, lv_tree_node_t * parent); + +/** + * @brief Delete a tree node and all its children recursively + * @param node pointer to the node to delete + */ +void lv_tree_node_delete(lv_tree_node_t * node); + +/** + * @brief Walk the tree recursively and call a callback function on each node + * @param node pointer to the root node of the tree + * @param mode LV_TREE_WALK_PRE_ORDER or LV_TREE_WALK_POST_ORDER + * @param cb callback function to call on each node + * @param bcb callback function to call before visiting a node + * @param acb callback function to call after visiting a node + * @param user_data user data to pass to the callback functions + * @return true: traversal is finished; false: traversal breaked + */ +bool lv_tree_walk(const lv_tree_node_t * node, + lv_tree_walk_mode_t mode, + lv_tree_traverse_cb_t cb, + lv_tree_before_cb_t bcb, + lv_tree_after_cb_t acb, + void * user_data); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif diff --git a/include/liblvgl/misc/lv_txt.h b/include/liblvgl/misc/lv_txt.h deleted file mode 100644 index ae60e8b8..00000000 --- a/include/liblvgl/misc/lv_txt.h +++ /dev/null @@ -1,264 +0,0 @@ -/** - * @file lv_txt.h - * - */ - -#ifndef LV_TXT_H -#define LV_TXT_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lv_conf_internal.h" - -#include -#include -#include "lv_area.h" -#include "liblvgl/font/lv_font.h" -#include "lv_printf.h" -#include "lv_types.h" - -/********************* - * DEFINES - *********************/ -#ifndef LV_TXT_COLOR_CMD -#define LV_TXT_COLOR_CMD "#" -#endif - -#define LV_TXT_ENC_UTF8 1 -#define LV_TXT_ENC_ASCII 2 - -/********************** - * TYPEDEFS - **********************/ - -/** - * Options for text rendering. - */ -enum { - LV_TEXT_FLAG_NONE = 0x00, - LV_TEXT_FLAG_RECOLOR = 0x01, /**< Enable parsing of recolor command*/ - LV_TEXT_FLAG_EXPAND = 0x02, /**< Ignore max-width to avoid automatic word wrapping*/ - LV_TEXT_FLAG_FIT = 0x04, /**< Max-width is already equal to the longest line. (Used to skip some calculation)*/ -}; -typedef uint8_t lv_text_flag_t; - -/** - * State machine for text renderer.*/ -enum { - LV_TEXT_CMD_STATE_WAIT, /**< Waiting for command*/ - LV_TEXT_CMD_STATE_PAR, /**< Processing the parameter*/ - LV_TEXT_CMD_STATE_IN, /**< Processing the command*/ -}; -typedef uint8_t lv_text_cmd_state_t; - -/** Label align policy*/ -enum { - LV_TEXT_ALIGN_AUTO, /**< Align text auto*/ - LV_TEXT_ALIGN_LEFT, /**< Align text to left*/ - LV_TEXT_ALIGN_CENTER, /**< Align text to center*/ - LV_TEXT_ALIGN_RIGHT, /**< Align text to right*/ -}; -typedef uint8_t lv_text_align_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Get size of a text - * @param size_res pointer to a 'point_t' variable to store the result - * @param text pointer to a text - * @param font pointer to font of the text - * @param letter_space letter space of the text - * @param line_space line space of the text - * @param flags settings for the text from ::lv_text_flag_t - * @param max_width max width of the text (break the lines to fit this size). Set COORD_MAX to avoid - * line breaks - */ -void lv_txt_get_size(lv_point_t * size_res, const char * text, const lv_font_t * font, lv_coord_t letter_space, - lv_coord_t line_space, lv_coord_t max_width, lv_text_flag_t flag); - -/** - * Get the next line of text. Check line length and break chars too. - * @param txt a '\0' terminated string - * @param font pointer to a font - * @param letter_space letter space - * @param max_width max width of the text (break the lines to fit this size). Set COORD_MAX to avoid - * line breaks - * @param used_width When used_width != NULL, save the width of this line if - * flag == LV_TEXT_FLAG_NONE, otherwise save -1. - * @param flags settings for the text from 'txt_flag_type' enum - * @return the index of the first char of the new line (in byte index not letter index. With UTF-8 - * they are different) - */ -uint32_t _lv_txt_get_next_line(const char * txt, const lv_font_t * font, lv_coord_t letter_space, - lv_coord_t max_width, lv_coord_t * used_width, lv_text_flag_t flag); - -/** - * Give the length of a text with a given font - * @param txt a '\0' terminate string - * @param length length of 'txt' in byte count and not characters (Á is 1 character but 2 bytes in - * UTF-8) - * @param font pointer to a font - * @param letter_space letter space - * @param flags settings for the text from 'txt_flag_t' enum - * @return length of a char_num long text - */ -lv_coord_t lv_txt_get_width(const char * txt, uint32_t length, const lv_font_t * font, lv_coord_t letter_space, - lv_text_flag_t flag); - -/** - * Check next character in a string and decide if the character is part of the command or not - * @param state pointer to a txt_cmd_state_t variable which stores the current state of command - * processing - * @param c the current character - * @return true: the character is part of a command and should not be written, - * false: the character should be written - */ -bool _lv_txt_is_cmd(lv_text_cmd_state_t * state, uint32_t c); - -/** - * Insert a string into an other - * @param txt_buf the original text (must be big enough for the result text and NULL terminated) - * @param pos position to insert (0: before the original text, 1: after the first char etc.) - * @param ins_txt text to insert, must be '\0' terminated - */ -void _lv_txt_ins(char * txt_buf, uint32_t pos, const char * ins_txt); - -/** - * Delete a part of a string - * @param txt string to modify, must be '\0' terminated and should point to a heap or stack frame, not read-only memory. - * @param pos position where to start the deleting (0: before the first char, 1: after the first - * char etc.) - * @param len number of characters to delete - */ -void _lv_txt_cut(char * txt, uint32_t pos, uint32_t len); - -/** - * return a new formatted text. Memory will be allocated to store the text. - * @param fmt `printf`-like format - * @return pointer to the allocated text string. - */ -char * _lv_txt_set_text_vfmt(const char * fmt, va_list ap) LV_FORMAT_ATTRIBUTE(1, 0); - -/** - * Decode two encoded character from a string. - * @param txt pointer to '\0' terminated string - * @param letter the first decoded Unicode character or 0 on invalid data code - * @param letter_next the second decoded Unicode character or 0 on invalid data code - * @param ofs start index in 'txt' where to start. - * After the call it will point to the next encoded char in 'txt'. - * NULL to use txt[0] as index - */ -void _lv_txt_encoded_letter_next_2(const char * txt, uint32_t * letter, uint32_t * letter_next, uint32_t * ofs); - -/** - * Test if char is break char or not (a text can broken here or not) - * @param letter a letter - * @return false: 'letter' is not break char - */ -static inline bool _lv_txt_is_break_char(uint32_t letter) -{ - uint8_t i; - bool ret = false; - - /* each chinese character can be break */ - if(letter >= 0x4E00 && letter <= 0x9FA5) { - return true; - } - - /*Compare the letter to TXT_BREAK_CHARS*/ - for(i = 0; LV_TXT_BREAK_CHARS[i] != '\0'; i++) { - if(letter == (uint32_t)LV_TXT_BREAK_CHARS[i]) { - ret = true; /*If match then it is break char*/ - break; - } - } - - return ret; -} - -/*************************************************************** - * GLOBAL FUNCTION POINTERS FOR CHARACTER ENCODING INTERFACE - ***************************************************************/ - -/** - * Give the size of an encoded character - * @param str pointer to a character in a string - * @return length of the encoded character (1,2,3 ...). O in invalid - */ -extern uint8_t (*_lv_txt_encoded_size)(const char *); - -/** - * Convert a Unicode letter to encoded - * @param letter_uni a Unicode letter - * @return Encoded character in Little Endian to be compatible with C chars (e.g. 'Á', 'Ü') - */ -extern uint32_t (*_lv_txt_unicode_to_encoded)(uint32_t); - -/** - * Convert a wide character, e.g. 'Á' little endian to be compatible with the encoded format. - * @param c a wide character - * @return `c` in the encoded format - */ -extern uint32_t (*_lv_txt_encoded_conv_wc)(uint32_t c); - -/** - * Decode the next encoded character from a string. - * @param txt pointer to '\0' terminated string - * @param i start index in 'txt' where to start. - * After the call it will point to the next encoded char in 'txt'. - * NULL to use txt[0] as index - * @return the decoded Unicode character or 0 on invalid data code - */ -extern uint32_t (*_lv_txt_encoded_next)(const char *, uint32_t *); - -/** - * Get the previous encoded character form a string. - * @param txt pointer to '\0' terminated string - * @param i_start index in 'txt' where to start. After the call it will point to the previous - * encoded char in 'txt'. - * @return the decoded Unicode character or 0 on invalid data - */ -extern uint32_t (*_lv_txt_encoded_prev)(const char *, uint32_t *); - -/** - * Convert a letter index (in the encoded text) to byte index. - * E.g. in UTF-8 "AÁRT" index of 'R' is 2 but start at byte 3 because 'Á' is 2 bytes long - * @param txt a '\0' terminated UTF-8 string - * @param enc_id letter index - * @return byte index of the 'enc_id'th letter - */ -extern uint32_t (*_lv_txt_encoded_get_byte_id)(const char *, uint32_t); - -/** - * Convert a byte index (in an encoded text) to character index. - * E.g. in UTF-8 "AÁRT" index of 'R' is 2 but start at byte 3 because 'Á' is 2 bytes long - * @param txt a '\0' terminated UTF-8 string - * @param byte_id byte index - * @return character index of the letter at 'byte_id'th position - */ -extern uint32_t (*_lv_txt_encoded_get_char_id)(const char *, uint32_t); - -/** - * Get the number of characters (and NOT bytes) in a string. - * E.g. in UTF-8 "ÁBC" is 3 characters (but 4 bytes) - * @param txt a '\0' terminated char string - * @return number of characters - */ -extern uint32_t (*_lv_txt_get_encoded_length)(const char *); - -/********************** - * MACROS - **********************/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_TXT_H*/ diff --git a/include/liblvgl/misc/lv_types.h b/include/liblvgl/misc/lv_types.h index 84aee103..d86284a7 100644 --- a/include/liblvgl/misc/lv_types.h +++ b/include/liblvgl/misc/lv_types.h @@ -13,20 +13,29 @@ extern "C" { /********************* * INCLUDES *********************/ -#include +#include "../lv_conf_internal.h" + +#ifndef __ASSEMBLY__ +#include LV_STDINT_INCLUDE +#include LV_STDDEF_INCLUDE +#include LV_STDBOOL_INCLUDE +#include LV_INTTYPES_INCLUDE +#include LV_LIMITS_INCLUDE +#include LV_STDARG_INCLUDE +#endif /********************* * DEFINES *********************/ -// If __UINTPTR_MAX__ or UINTPTR_MAX are available, use them to determine arch size +/*If __UINTPTR_MAX__ or UINTPTR_MAX are available, use them to determine arch size*/ #if defined(__UINTPTR_MAX__) && __UINTPTR_MAX__ > 0xFFFFFFFF #define LV_ARCH_64 #elif defined(UINTPTR_MAX) && UINTPTR_MAX > 0xFFFFFFFF #define LV_ARCH_64 -// Otherwise use compiler-dependent means to determine arch size +/*Otherwise use compiler-dependent means to determine arch size*/ #elif defined(_WIN64) || defined(__x86_64__) || defined(__ppc64__) || defined (__aarch64__) #define LV_ARCH_64 @@ -36,31 +45,307 @@ extern "C" { * TYPEDEFS **********************/ +/* Exclude C enum and struct definitions when included by assembly code */ +#ifndef __ASSEMBLY__ + /** * LVGL error codes. */ -enum { - LV_RES_INV = 0, /*Typically indicates that the object is deleted (become invalid) in the action +typedef enum { + LV_RESULT_INVALID = 0, /*Typically indicates that the object is deleted (become invalid) in the action function or an operation was failed*/ - LV_RES_OK, /*The object is valid (no deleted) after the action*/ -}; -typedef uint8_t lv_res_t; + LV_RESULT_OK, /*The object is valid (no deleted) after the action*/ +} lv_result_t; #if defined(__cplusplus) || __STDC_VERSION__ >= 199901L -// If c99 or newer, use the definition of uintptr_t directly from +/*If c99 or newer, use the definition of uintptr_t directly from */ typedef uintptr_t lv_uintptr_t; +typedef intptr_t lv_intptr_t; #else -// Otherwise, use the arch size determination +/*Otherwise, use the arch size determination*/ #ifdef LV_ARCH_64 typedef uint64_t lv_uintptr_t; +typedef int64_t lv_intptr_t; #else typedef uint32_t lv_uintptr_t; +typedef int32_t lv_intptr_t; #endif #endif +#if LV_USE_FLOAT +typedef float lv_value_precise_t; +#else +typedef int32_t lv_value_precise_t; +#endif + +/** + * Typedefs from various lvgl modules. + * They are defined here to avoid circular dependencies. + */ + +typedef struct _lv_obj_t lv_obj_t; + +typedef uint16_t lv_state_t; +typedef uint32_t lv_part_t; + +typedef uint8_t lv_opa_t; + +typedef uint8_t lv_style_prop_t; + +typedef struct _lv_obj_class_t lv_obj_class_t; + +typedef struct _lv_group_t lv_group_t; + +typedef struct _lv_display_t lv_display_t; + +typedef struct _lv_layer_t lv_layer_t; +typedef struct _lv_draw_unit_t lv_draw_unit_t; +typedef struct _lv_draw_task_t lv_draw_task_t; + +typedef struct _lv_indev_t lv_indev_t; + +typedef struct _lv_event_t lv_event_t; + +typedef struct _lv_timer_t lv_timer_t; + +typedef struct _lv_theme_t lv_theme_t; + +typedef struct _lv_anim_t lv_anim_t; + +typedef struct _lv_font_t lv_font_t; + +typedef struct _lv_image_decoder_t lv_image_decoder_t; + +typedef struct _lv_image_decoder_dsc_t lv_image_decoder_dsc_t; + +typedef struct _lv_fragment_t lv_fragment_t; +typedef struct _lv_fragment_class_t lv_fragment_class_t; +typedef struct _lv_fragment_managed_states_t lv_fragment_managed_states_t; + +typedef struct _lv_profiler_builtin_config_t lv_profiler_builtin_config_t; + +typedef struct _lv_rb_node_t lv_rb_node_t; + +typedef struct _lv_rb_t lv_rb_t; + +typedef struct _lv_color_filter_dsc_t lv_color_filter_dsc_t; + +typedef struct _lv_event_dsc_t lv_event_dsc_t; + +typedef struct _lv_fs_file_cache_t lv_fs_file_cache_t; + +typedef struct _lv_fs_path_ex_t lv_fs_path_ex_t; + +typedef struct _lv_image_decoder_args_t lv_image_decoder_args_t; + +typedef struct _lv_image_cache_data_t lv_image_cache_data_t; + +typedef struct _lv_image_header_cache_data_t lv_image_header_cache_data_t; + +typedef struct _lv_draw_mask_t lv_draw_mask_t; + +typedef struct _lv_grad_t lv_grad_t; + +typedef struct _lv_draw_label_hint_t lv_draw_label_hint_t; + +typedef struct _lv_draw_glyph_dsc_t lv_draw_glyph_dsc_t; + +typedef struct _lv_draw_image_sup_t lv_draw_image_sup_t; + +typedef struct _lv_draw_mask_rect_dsc_t lv_draw_mask_rect_dsc_t; + +typedef struct _lv_obj_style_t lv_obj_style_t; + +typedef struct _lv_obj_style_transition_dsc_t lv_obj_style_transition_dsc_t; + +typedef struct _lv_hit_test_info_t lv_hit_test_info_t; + +typedef struct _lv_cover_check_info_t lv_cover_check_info_t; + +typedef struct _lv_obj_spec_attr_t lv_obj_spec_attr_t; + +typedef struct _lv_image_t lv_image_t; + +typedef struct _lv_animimg_t lv_animimg_t; + +typedef struct _lv_arc_t lv_arc_t; + +typedef struct _lv_label_t lv_label_t; + +typedef struct _lv_bar_anim_t lv_bar_anim_t; + +typedef struct _lv_bar_t lv_bar_t; + +typedef struct _lv_button_t lv_button_t; + +typedef struct _lv_buttonmatrix_t lv_buttonmatrix_t; + +typedef struct _lv_calendar_t lv_calendar_t; + +typedef struct _lv_canvas_t lv_canvas_t; + +typedef struct _lv_chart_series_t lv_chart_series_t; + +typedef struct _lv_chart_cursor_t lv_chart_cursor_t; + +typedef struct _lv_chart_t lv_chart_t; + +typedef struct _lv_checkbox_t lv_checkbox_t; + +typedef struct _lv_dropdown_t lv_dropdown_t; + +typedef struct _lv_dropdown_list_t lv_dropdown_list_t; + +typedef struct _lv_imagebutton_src_info_t lv_imagebutton_src_info_t; + +typedef struct _lv_imagebutton_t lv_imagebutton_t; + +typedef struct _lv_keyboard_t lv_keyboard_t; + +typedef struct _lv_led_t lv_led_t; + +typedef struct _lv_line_t lv_line_t; + +typedef struct _lv_menu_load_page_event_data_t lv_menu_load_page_event_data_t; + +typedef struct _lv_menu_history_t lv_menu_history_t; + +typedef struct _lv_menu_t lv_menu_t; + +typedef struct _lv_menu_page_t lv_menu_page_t; + +typedef struct _lv_msgbox_t lv_msgbox_t; + +typedef struct _lv_roller_t lv_roller_t; + +typedef struct _lv_scale_section_t lv_scale_section_t; + +typedef struct _lv_scale_t lv_scale_t; + +typedef struct _lv_slider_t lv_slider_t; + +typedef struct _lv_span_t lv_span_t; + +typedef struct _lv_spangroup_t lv_spangroup_t; + +typedef struct _lv_textarea_t lv_textarea_t; + +typedef struct _lv_spinbox_t lv_spinbox_t; + +typedef struct _lv_switch_t lv_switch_t; + +typedef struct _lv_table_cell_t lv_table_cell_t; + +typedef struct _lv_table_t lv_table_t; + +typedef struct _lv_tabview_t lv_tabview_t; + +typedef struct _lv_tileview_t lv_tileview_t; + +typedef struct _lv_tileview_tile_t lv_tileview_tile_t; + +typedef struct _lv_win_t lv_win_t; + +typedef struct _lv_observer_t lv_observer_t; + +typedef struct _lv_monkey_config_t lv_monkey_config_t; + +typedef struct _lv_ime_pinyin_t lv_ime_pinyin_t; + +typedef struct _lv_file_explorer_t lv_file_explorer_t; + +typedef struct _lv_barcode_t lv_barcode_t; + +typedef struct _lv_gif_t lv_gif_t; + +typedef struct _lv_qrcode_t lv_qrcode_t; + +typedef struct _lv_freetype_outline_vector_t lv_freetype_outline_vector_t; + +typedef struct _lv_freetype_outline_event_param_t lv_freetype_outline_event_param_t; + +typedef struct _lv_fpoint_t lv_fpoint_t; + +typedef struct _lv_matrix_t lv_matrix_t; + +typedef struct _lv_vector_path_t lv_vector_path_t; + +typedef struct _lv_vector_gradient_t lv_vector_gradient_t; + +typedef struct _lv_vector_fill_dsc_t lv_vector_fill_dsc_t; + +typedef struct _lv_vector_stroke_dsc_t lv_vector_stroke_dsc_t; + +typedef struct _lv_vector_draw_dsc_t lv_vector_draw_dsc_t; + +typedef struct _lv_draw_vector_task_dsc_t lv_draw_vector_task_dsc_t; + +typedef struct _lv_vector_dsc_t lv_vector_dsc_t; + +typedef struct _lv_xkb_t lv_xkb_t; + +typedef struct _lv_libinput_event_t lv_libinput_event_t; + +typedef struct _lv_libinput_t lv_libinput_t; + +typedef struct _lv_draw_sw_unit_t lv_draw_sw_unit_t; + +typedef struct _lv_draw_sw_mask_common_dsc_t lv_draw_sw_mask_common_dsc_t; + +typedef struct _lv_draw_sw_mask_line_param_t lv_draw_sw_mask_line_param_t; + +typedef struct _lv_draw_sw_mask_angle_param_t lv_draw_sw_mask_angle_param_t; + +typedef struct _lv_draw_sw_mask_radius_param_t lv_draw_sw_mask_radius_param_t; + +typedef struct _lv_draw_sw_mask_fade_param_t lv_draw_sw_mask_fade_param_t; + +typedef struct _lv_draw_sw_mask_map_param_t lv_draw_sw_mask_map_param_t; + +typedef struct _lv_draw_sw_blend_dsc_t lv_draw_sw_blend_dsc_t; + +typedef struct _lv_draw_sw_blend_fill_dsc_t lv_draw_sw_blend_fill_dsc_t; + +typedef struct _lv_draw_sw_blend_image_dsc_t lv_draw_sw_blend_image_dsc_t; + +typedef struct _lv_draw_buf_handlers_t lv_draw_buf_handlers_t; + +typedef struct _lv_rlottie_t lv_rlottie_t; + +typedef struct _lv_ffmpeg_player_t lv_ffmpeg_player_t; + +typedef struct _lv_glfw_window_t lv_glfw_window_t; +typedef struct _lv_glfw_texture_t lv_glfw_texture_t; + +typedef uint32_t lv_prop_id_t; + +typedef struct _lv_array_t lv_array_t; + +typedef struct _lv_iter_t lv_iter_t; + +typedef struct _lv_circle_buf_t lv_circle_buf_t; + +typedef struct _lv_draw_buf_t lv_draw_buf_t; + +#if LV_USE_OBJ_PROPERTY +typedef struct _lv_property_name_t lv_property_name_t; +#endif + +#if LV_USE_SYSMON + +typedef struct _lv_sysmon_backend_data_t lv_sysmon_backend_data_t; + +#if LV_USE_PERF_MONITOR +typedef struct _lv_sysmon_perf_info_t lv_sysmon_perf_info_t; +#endif /*LV_USE_PERF_MONITOR*/ + +#endif /*LV_USE_SYSMON*/ + +#endif /*__ASSEMBLY__*/ + /********************** * GLOBAL PROTOTYPES **********************/ @@ -73,15 +358,17 @@ typedef uint32_t lv_uintptr_t; #define _LV_CONCAT(x, y) x ## y #define LV_CONCAT(x, y) _LV_CONCAT(x, y) +#undef _LV_CONCAT #define _LV_CONCAT3(x, y, z) x ## y ## z #define LV_CONCAT3(x, y, z) _LV_CONCAT3(x, y, z) +#undef _LV_CONCAT3 #if defined(PYCPARSER) || defined(__CC_ARM) #define LV_FORMAT_ATTRIBUTE(fmtstr, vararg) #elif defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 4) || __GNUC__ > 4) #define LV_FORMAT_ATTRIBUTE(fmtstr, vararg) __attribute__((format(gnu_printf, fmtstr, vararg))) -#elif (defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)) +#elif (defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) || defined(__IAR_SYSTEMS_ICC__)) #define LV_FORMAT_ATTRIBUTE(fmtstr, vararg) __attribute__((format(printf, fmtstr, vararg))) #else #define LV_FORMAT_ATTRIBUTE(fmtstr, vararg) diff --git a/include/liblvgl/misc/lv_utils.h b/include/liblvgl/misc/lv_utils.h index 84d2bb95..7a8d6101 100644 --- a/include/liblvgl/misc/lv_utils.h +++ b/include/liblvgl/misc/lv_utils.h @@ -13,7 +13,9 @@ extern "C" { /********************* * INCLUDES *********************/ -#include + +#include "lv_types.h" +#include "../draw/lv_draw_buf.h" /********************* * DEFINES @@ -33,19 +35,27 @@ extern "C" { * argument (the search key) is less that it's second (a table entry), * zero if equal, and positive if greater. * - * @note Items in the array must be in ascending order. + * @note Items in the array must be in ascending order. * * @param key Pointer to item being searched for * @param base Pointer to first element to search * @param n Number of elements * @param size Size of each element - * @param cmp Pointer to comparison function (see #unicode_list_compare as a comparison function - * example) + * @param cmp Pointer to comparison function (see unicode_list_compare() + * as a comparison function example) * * @return a pointer to a matching item, or NULL if none exists. */ -void * _lv_utils_bsearch(const void * key, const void * base, uint32_t n, uint32_t size, - int32_t (*cmp)(const void * pRef, const void * pElement)); +void * lv_utils_bsearch(const void * key, const void * base, size_t n, size_t size, + int (*cmp)(const void * pRef, const void * pElement)); + +/** + * Save a draw buf to a file + * @param draw_buf pointer to a draw buffer + * @param path path to the file to save + * @return LV_RESULT_OK: success; LV_RESULT_INVALID: error + */ +lv_result_t lv_draw_buf_save_to_file(const lv_draw_buf_t * draw_buf, const char * path); /********************** * MACROS diff --git a/include/liblvgl/osal/lv_cmsis_rtos2.h b/include/liblvgl/osal/lv_cmsis_rtos2.h new file mode 100644 index 00000000..62481e7e --- /dev/null +++ b/include/liblvgl/osal/lv_cmsis_rtos2.h @@ -0,0 +1,53 @@ +/** + * @file lv_cmsis_rtos2.h + * + */ + +/* + * Copyright (C) 2023 Arm Limited or its affiliates. All rights reserved. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef LV_CMSIS_RTOS2_H +#define LV_CMSIS_RTOS2_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#if LV_USE_OS == LV_OS_CMSIS_RTOS2 + +#include "cmsis_os2.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ +typedef osThreadId_t lv_thread_t; + +typedef osMutexId_t lv_mutex_t; + +typedef osEventFlagsId_t lv_thread_sync_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_OS == LV_OS_CMSIS_RTOS2*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_OS_CMSIS_RTOS2*/ diff --git a/include/liblvgl/osal/lv_freertos.h b/include/liblvgl/osal/lv_freertos.h new file mode 100644 index 00000000..67a5a1e7 --- /dev/null +++ b/include/liblvgl/osal/lv_freertos.h @@ -0,0 +1,105 @@ +/** + * @file lv_freertos.h + * + */ + +/** + * Copyright 2023 NXP + * + * SPDX-License-Identifier: MIT + */ + +#ifndef LV_FREERTOS_H +#define LV_FREERTOS_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "lv_os.h" + +#if LV_USE_OS == LV_OS_FREERTOS + +#if (ESP_PLATFORM) +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/semphr.h" +#else +#include "FreeRTOS.h" +#include "task.h" +#include "semphr.h" +#endif + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef struct { + void (*pvStartRoutine)(void *); /**< Application thread function. */ + void * pTaskArg; /**< Arguments for application thread function. */ + TaskHandle_t xTaskHandle; /**< FreeRTOS task handle. */ +} lv_thread_t; + +typedef struct { + BaseType_t xIsInitialized; /**< Set to pdTRUE if this mutex is initialized, pdFALSE otherwise. */ + SemaphoreHandle_t xMutex; /**< FreeRTOS mutex. */ +} lv_mutex_t; + +typedef struct { + BaseType_t + xIsInitialized; /**< Set to pdTRUE if this condition variable is initialized, pdFALSE otherwise. */ + BaseType_t xSyncSignal; /**< Set to pdTRUE if the thread is signaled, pdFALSE otherwise. */ +#if LV_USE_FREERTOS_TASK_NOTIFY + TaskHandle_t xTaskToNotify; /**< The task waiting to be signalled. NULL if nothing is waiting. */ +#else + SemaphoreHandle_t xCondWaitSemaphore; /**< Threads block on this semaphore in lv_thread_sync_wait. */ + uint32_t ulWaitingThreads; /**< The number of threads currently waiting on this condition variable. */ + SemaphoreHandle_t xSyncMutex; /**< Threads take this mutex before accessing the condition variable. */ +#endif +} lv_thread_sync_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Set it for `traceTASK_SWITCHED_IN()` as + * `lv_freertos_task_switch_in(pxCurrentTCB->pcTaskName)` + * to save the start time stamp of a task + * @param name the name of the which is switched in + */ +void lv_freertos_task_switch_in(const char * name); + +/** + * Set it for `traceTASK_SWITCHED_OUT()` as + * `lv_freertos_task_switch_out()` + * to save finish time stamp of a task + */ +void lv_freertos_task_switch_out(void); + +/** + * Set it for `LV_SYSMON_GET_IDLE` to show the CPU usage + * as reported based the usage of FreeRTOS's idle task + * If it's important when a GPU is used. + * @return the idle percentage since the last call + */ +uint32_t lv_os_get_idle_percent(void); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_OS == LV_OS_FREERTOS*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_FREERTOS_H*/ diff --git a/include/liblvgl/osal/lv_mqx.h b/include/liblvgl/osal/lv_mqx.h new file mode 100644 index 00000000..601f596a --- /dev/null +++ b/include/liblvgl/osal/lv_mqx.h @@ -0,0 +1,51 @@ +/** + * @file lv_mqx.h + * + */ + +#ifndef LV_MQX_H +#define LV_MQX_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "lv_os.h" + +#if LV_USE_OS == LV_OS_MQX + +#include "mqx.h" +#include "mutex.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef _task_id lv_thread_t; + +typedef MUTEX_STRUCT lv_mutex_t; + +typedef LWSEM_STRUCT lv_thread_sync_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_OS == LV_OS_MQX*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_MQX_H*/ diff --git a/include/liblvgl/osal/lv_os.h b/include/liblvgl/osal/lv_os.h new file mode 100644 index 00000000..d0a4e02f --- /dev/null +++ b/include/liblvgl/osal/lv_os.h @@ -0,0 +1,186 @@ +/** + * @file lv_os.h + * + */ + +#ifndef LV_OS_H +#define LV_OS_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * OS OPTIONS + *********************/ + +/********************* + * INCLUDES + *********************/ +#include "../lv_conf_internal.h" + +#include "../misc/lv_types.h" + +#if LV_USE_OS == LV_OS_NONE +#include "lv_os_none.h" +#elif LV_USE_OS == LV_OS_PTHREAD +#include "lv_pthread.h" +#elif LV_USE_OS == LV_OS_FREERTOS +#include "lv_freertos.h" +#elif LV_USE_OS == LV_OS_CMSIS_RTOS2 +#include "lv_cmsis_rtos2.h" +#elif LV_USE_OS == LV_OS_RTTHREAD +#include "lv_rtthread.h" +#elif LV_USE_OS == LV_OS_WINDOWS +#include "lv_windows.h" +#elif LV_USE_OS == LV_OS_MQX +#include "lv_mqx.h" +#elif LV_USE_OS == LV_OS_CUSTOM +#include LV_OS_CUSTOM_INCLUDE +#endif + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ +typedef enum { + LV_THREAD_PRIO_LOWEST, + LV_THREAD_PRIO_LOW, + LV_THREAD_PRIO_MID, + LV_THREAD_PRIO_HIGH, + LV_THREAD_PRIO_HIGHEST, +} lv_thread_prio_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/*---------------------------------------- + * These functions needs to be implemented + * for specific operating systems + *---------------------------------------*/ + +/** + * Create a new thread + * @param thread a variable in which the thread will be stored + * @param prio priority of the thread + * @param callback function of the thread + * @param stack_size stack size in bytes + * @param user_data arbitrary data, will be available in the callback + * @return LV_RESULT_OK: success; LV_RESULT_INVALID: failure + */ +lv_result_t lv_thread_init(lv_thread_t * thread, lv_thread_prio_t prio, void (*callback)(void *), size_t stack_size, + void * user_data); + +/** + * Delete a thread + * @param thread the thread to delete + * @return LV_RESULT_OK: success; LV_RESULT_INVALID: failure + */ +lv_result_t lv_thread_delete(lv_thread_t * thread); + +/** + * Create a mutex + * @param mutex a variable in which the thread will be stored + * @return LV_RESULT_OK: success; LV_RESULT_INVALID: failure + */ +lv_result_t lv_mutex_init(lv_mutex_t * mutex); + +/** + * Lock a mutex + * @param mutex the mutex to lock + * @return LV_RESULT_OK: success; LV_RESULT_INVALID: failure + */ +lv_result_t lv_mutex_lock(lv_mutex_t * mutex); + +/** + * Lock a mutex from interrupt + * @param mutex the mutex to lock + * @return LV_RESULT_OK: success; LV_RESULT_INVALID: failure + */ +lv_result_t lv_mutex_lock_isr(lv_mutex_t * mutex); + +/** + * Unlock a mutex + * @param mutex the mutex to unlock + * @return LV_RESULT_OK: success; LV_RESULT_INVALID: failure + */ +lv_result_t lv_mutex_unlock(lv_mutex_t * mutex); + +/** + * Delete a mutex + * @param mutex the mutex to delete + * @return LV_RESULT_OK: success; LV_RESULT_INVALID: failure + */ +lv_result_t lv_mutex_delete(lv_mutex_t * mutex); + +/** + * Create a thread synchronization object + * @param sync a variable in which the sync will be stored + * @return LV_RESULT_OK: success; LV_RESULT_INVALID: failure + */ +lv_result_t lv_thread_sync_init(lv_thread_sync_t * sync); + +/** + * Wait for a "signal" on a sync object + * @param sync a sync object + * @return LV_RESULT_OK: success; LV_RESULT_INVALID: failure + */ +lv_result_t lv_thread_sync_wait(lv_thread_sync_t * sync); + +/** + * Send a wake-up signal to a sync object + * @param sync a sync object + * @return LV_RESULT_OK: success; LV_RESULT_INVALID: failure + */ +lv_result_t lv_thread_sync_signal(lv_thread_sync_t * sync); + +/** + * Send a wake-up signal to a sync object from interrupt + * @param sync a sync object + * @return LV_RESULT_OK: success; LV_RESULT_INVALID: failure + */ +lv_result_t lv_thread_sync_signal_isr(lv_thread_sync_t * sync); + +/** + * Delete a sync object + * @param sync a sync object to delete + * @return LV_RESULT_OK: success; LV_RESULT_INVALID: failure + */ +lv_result_t lv_thread_sync_delete(lv_thread_sync_t * sync); + +/** + * Lock LVGL's general mutex. + * LVGL is not thread safe, so a mutex is used to avoid executing multiple LVGL functions at the same time + * from different threads. It shall be called when calling LVGL functions from threads + * different than lv_timer_handler's thread. It doesn't need to be called in LVGL events because + * they are called from lv_timer_handler(). + * It is called internally in lv_timer_handler(). + */ +void lv_lock(void); + +/** + * Same as `lv_lock()` but can be called from an interrupt. + * @return LV_RESULT_OK: success; LV_RESULT_INVALID: failure + */ +lv_result_t lv_lock_isr(void); + +/** + * The pair of `lv_lock()` and `lv_lock_isr()`. + * It unlocks LVGL general mutex. + * It is called internally in lv_timer_handler(). + */ +void lv_unlock(void); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_OS_H*/ diff --git a/include/liblvgl/osal/lv_os_none.h b/include/liblvgl/osal/lv_os_none.h new file mode 100644 index 00000000..e08a0dc4 --- /dev/null +++ b/include/liblvgl/osal/lv_os_none.h @@ -0,0 +1,43 @@ +/** + * @file lv_os_none.h + * + */ + +#ifndef LV_OS_NONE_H +#define LV_OS_NONE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#if LV_USE_OS == LV_OS_NONE + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ +typedef int lv_mutex_t; +typedef int lv_thread_t; +typedef int lv_thread_sync_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_OS == LV_OS_NONE*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_OS_NONE_H*/ diff --git a/include/liblvgl/osal/lv_os_private.h b/include/liblvgl/osal/lv_os_private.h new file mode 100644 index 00000000..4fe08cd2 --- /dev/null +++ b/include/liblvgl/osal/lv_os_private.h @@ -0,0 +1,47 @@ +/** + * @file lv_os_private.h + * + */ + +#ifndef LV_OS_PRIVATE_H +#define LV_OS_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * OS OPTIONS + *********************/ + +/********************* + * INCLUDES + *********************/ + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Initialize the OS layer + */ +void lv_os_init(void); + + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_OS_PRIVATE_H*/ diff --git a/include/liblvgl/osal/lv_pthread.h b/include/liblvgl/osal/lv_pthread.h new file mode 100644 index 00000000..e7abc90c --- /dev/null +++ b/include/liblvgl/osal/lv_pthread.h @@ -0,0 +1,57 @@ +/** + * @file lv_pthread.h + * + */ + +#ifndef LV_PTHREAD_H +#define LV_PTHREAD_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#if LV_USE_OS == LV_OS_PTHREAD + +#include +#include +#include + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ +typedef struct { + pthread_t thread; + void (*callback)(void *); + void * user_data; +} lv_thread_t; + +typedef pthread_mutex_t lv_mutex_t; + +typedef struct { + pthread_mutex_t mutex; + pthread_cond_t cond; + bool v; +} lv_thread_sync_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_OS == LV_OS_PTHREAD*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_PTHREAD_H*/ diff --git a/include/liblvgl/osal/lv_rtthread.h b/include/liblvgl/osal/lv_rtthread.h new file mode 100644 index 00000000..255a47af --- /dev/null +++ b/include/liblvgl/osal/lv_rtthread.h @@ -0,0 +1,54 @@ +/** + * @file lv_rtthread.h + * + */ + +#ifndef LV_RTTHREAD_H +#define LV_RTTHREAD_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#if LV_USE_OS == LV_OS_RTTHREAD + +#include +#include + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ +typedef struct { + rt_thread_t thread; +} lv_thread_t; + +typedef struct { + rt_mutex_t mutex; +} lv_mutex_t; + +typedef struct { + rt_sem_t sem; +} lv_thread_sync_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_OS == LV_OS_RTTHREAD*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_RTTHREAD_H*/ diff --git a/include/liblvgl/osal/lv_windows.h b/include/liblvgl/osal/lv_windows.h new file mode 100644 index 00000000..9d864535 --- /dev/null +++ b/include/liblvgl/osal/lv_windows.h @@ -0,0 +1,54 @@ +/** + * @file lv_windows.h + * + */ + +#ifndef LV_WINDOWS_H +#define LV_WINDOWS_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#if LV_USE_OS == LV_OS_WINDOWS + +#include +#include + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef HANDLE lv_thread_t; + +typedef CRITICAL_SECTION lv_mutex_t; + +typedef struct { + CRITICAL_SECTION cs; + CONDITION_VARIABLE cv; + bool v; +} lv_thread_sync_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_OS == LV_OS_WINDOWS*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_WINDOWS_H*/ diff --git a/include/liblvgl/others/file_explorer/lv_file_explorer.h b/include/liblvgl/others/file_explorer/lv_file_explorer.h new file mode 100644 index 00000000..8dd7762f --- /dev/null +++ b/include/liblvgl/others/file_explorer/lv_file_explorer.h @@ -0,0 +1,168 @@ +/** + * @file lv_file_explorer.h + * + */ + +#ifndef LV_FILE_EXPLORER_H +#define LV_FILE_EXPLORER_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../lv_conf_internal.h" +#include "../../core/lv_obj.h" + +#if LV_USE_FILE_EXPLORER != 0 + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef enum { + LV_EXPLORER_SORT_NONE, + LV_EXPLORER_SORT_KIND, +} lv_file_explorer_sort_t; + +#if LV_FILE_EXPLORER_QUICK_ACCESS +typedef enum { + LV_EXPLORER_HOME_DIR, + LV_EXPLORER_MUSIC_DIR, + LV_EXPLORER_PICTURES_DIR, + LV_EXPLORER_VIDEO_DIR, + LV_EXPLORER_DOCS_DIR, + LV_EXPLORER_FS_DIR, +} lv_file_explorer_dir_t; +#endif + +extern const lv_obj_class_t lv_file_explorer_class; + +/*********************** + * GLOBAL VARIABLES + ***********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ +lv_obj_t * lv_file_explorer_create(lv_obj_t * parent); + +/*===================== + * Setter functions + *====================*/ + +#if LV_FILE_EXPLORER_QUICK_ACCESS +/** + * Set file_explorer + * @param obj pointer to a label object + * @param dir the dir from 'lv_file_explorer_dir_t' enum. + * @param path path + + */ +void lv_file_explorer_set_quick_access_path(lv_obj_t * obj, lv_file_explorer_dir_t dir, const char * path); +#endif + +/** + * Set file_explorer sort + * @param obj pointer to a file explorer object + * @param sort the sort from 'lv_file_explorer_sort_t' enum. + */ +void lv_file_explorer_set_sort(lv_obj_t * obj, lv_file_explorer_sort_t sort); + +/*===================== + * Getter functions + *====================*/ + +/** + * Get file explorer Selected file + * @param obj pointer to a file explorer object + * @return pointer to the file explorer selected file name + */ +const char * lv_file_explorer_get_selected_file_name(const lv_obj_t * obj); + +/** + * Get file explorer cur path + * @param obj pointer to a file explorer object + * @return pointer to the file explorer cur path + */ +const char * lv_file_explorer_get_current_path(const lv_obj_t * obj); + +/** + * Get file explorer head area obj + * @param obj pointer to a file explorer object + * @return pointer to the file explorer head area obj(lv_obj) + */ +lv_obj_t * lv_file_explorer_get_header(lv_obj_t * obj); + +/** + * Get file explorer head area obj + * @param obj pointer to a file explorer object + * @return pointer to the file explorer quick access area obj(lv_obj) + */ +lv_obj_t * lv_file_explorer_get_quick_access_area(lv_obj_t * obj); + +/** + * Get file explorer path obj(label) + * @param obj pointer to a file explorer object + * @return pointer to the file explorer path obj(lv_label) + */ +lv_obj_t * lv_file_explorer_get_path_label(lv_obj_t * obj); + +#if LV_FILE_EXPLORER_QUICK_ACCESS +/** + * Get file explorer places list obj(lv_list) + * @param obj pointer to a file explorer object + * @return pointer to the file explorer places list obj(lv_list) + */ +lv_obj_t * lv_file_explorer_get_places_list(lv_obj_t * obj); + +/** + * Get file explorer device list obj(lv_list) + * @param obj pointer to a file explorer object + * @return pointer to the file explorer device list obj(lv_list) + */ +lv_obj_t * lv_file_explorer_get_device_list(lv_obj_t * obj); +#endif + +/** + * Get file explorer file list obj(lv_table) + * @param obj pointer to a file explorer object + * @return pointer to the file explorer file table obj(lv_table) + */ +lv_obj_t * lv_file_explorer_get_file_table(lv_obj_t * obj); + +/** + * Set file_explorer sort + * @param obj pointer to a file explorer object + * @return the current mode from 'lv_file_explorer_sort_t' + */ +lv_file_explorer_sort_t lv_file_explorer_get_sort(const lv_obj_t * obj); + +/*===================== + * Other functions + *====================*/ + +/** + * Open a specified path + * @param obj pointer to a file explorer object + * @param dir pointer to the path + */ +void lv_file_explorer_open_dir(lv_obj_t * obj, const char * dir); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_FILE_EXPLORER*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_FILE_EXPLORER_H*/ diff --git a/include/liblvgl/others/file_explorer/lv_file_explorer_private.h b/include/liblvgl/others/file_explorer/lv_file_explorer_private.h new file mode 100644 index 00000000..430debb5 --- /dev/null +++ b/include/liblvgl/others/file_explorer/lv_file_explorer_private.h @@ -0,0 +1,69 @@ +/** + * @file lv_file_explorer_private.h + * + */ + +#ifndef LV_FILE_EXPLORER_PRIVATE_H +#define LV_FILE_EXPLORER_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../core/lv_obj_private.h" +#include "lv_file_explorer.h" + +#if LV_USE_FILE_EXPLORER != 0 + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/*Data of canvas*/ +struct _lv_file_explorer_t { + lv_obj_t obj; + lv_obj_t * cont; + lv_obj_t * head_area; + lv_obj_t * browser_area; + lv_obj_t * file_table; + lv_obj_t * path_label; +#if LV_FILE_EXPLORER_QUICK_ACCESS + lv_obj_t * quick_access_area; + lv_obj_t * list_device; + lv_obj_t * list_places; + char * home_dir; + char * music_dir; + char * pictures_dir; + char * video_dir; + char * docs_dir; + char * fs_dir; +#endif + const char * sel_fn; + char current_path[LV_FILE_EXPLORER_PATH_MAX_LEN]; + lv_file_explorer_sort_t sort; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_FILE_EXPLORER != 0 */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_FILE_EXPLORER_PRIVATE_H*/ diff --git a/include/liblvgl/others/font_manager/lv_font_manager.h b/include/liblvgl/others/font_manager/lv_font_manager.h new file mode 100644 index 00000000..b5e33b9f --- /dev/null +++ b/include/liblvgl/others/font_manager/lv_font_manager.h @@ -0,0 +1,106 @@ +/** + * @file lv_font_manager.h + * + */ +#ifndef LV_FONT_MANAGER_H +#define LV_FONT_MANAGER_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../misc/lv_types.h" + +#if LV_USE_FONT_MANAGER + +#if !LV_USE_FREETYPE +#error "LV_USE_FONT_MANAGER requires LV_USE_FREETYPE" +#endif + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef struct _lv_font_manager_t lv_font_manager_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create main font manager. + * @param recycle_cache_size number of fonts that were recently deleted from the cache. + * @return pointer to main font manager. + */ +lv_font_manager_t * lv_font_manager_create(uint32_t recycle_cache_size); + +/** + * Delete main font manager. + * @param manager pointer to main font manager. + * @return return true if the deletion was successful. + */ +bool lv_font_manager_delete(lv_font_manager_t * manager); + +/** + * Add the font file path. + * @param manager pointer to main font manager. + * @param name font name. + * @param path font file path. + */ +void lv_font_manager_add_path(lv_font_manager_t * manager, const char * name, const char * path); + +/** + * Add the font file path with static memory. + * @param manager pointer to main font manager. + * @param name font name. + * @param path font file path. + */ +void lv_font_manager_add_path_static(lv_font_manager_t * manager, const char * name, const char * path); + +/** + * Remove the font file path. + * @param manager pointer to main font manager. + * @param name font name. + * @return return true if the remove was successful. + */ +bool lv_font_manager_remove_path(lv_font_manager_t * manager, const char * name); + +/** + * Create font. + * @param manager pointer to main font manager. + * @param font_family font family name. + * @param render_mode font render mode, see lv_freetype_font_render_mode_t. + * @param size font size. + * @param style font style, see lv_freetype_font_style_t. + * @return point to the created font + */ +lv_font_t * lv_font_manager_create_font(lv_font_manager_t * manager, const char * font_family, uint16_t render_mode, + uint32_t size, uint16_t style); + +/** + * Delete font. + * @param manager pointer to main font manager. + * @param font point to the font. + * @return return true if the deletion was successful. + */ +void lv_font_manager_delete_font(lv_font_manager_t * manager, lv_font_t * font); + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_FONT_MANAGER */ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* FONT_MANAGER_MANAGER_H */ diff --git a/include/liblvgl/others/font_manager/lv_font_manager_recycle.h b/include/liblvgl/others/font_manager/lv_font_manager_recycle.h new file mode 100644 index 00000000..57d41ec4 --- /dev/null +++ b/include/liblvgl/others/font_manager/lv_font_manager_recycle.h @@ -0,0 +1,78 @@ +/** + * @file lv_font_manager_recycle.h + * + */ + +#ifndef LV_FONT_MANAGER_RECYCLE_H +#define LV_FONT_MANAGER_RECYCLE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_font_manager_utils.h" + +#if LV_USE_FONT_MANAGER + +/********************* + * INCLUDES + *********************/ + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef struct _lv_font_manager_recycle_t lv_font_manager_recycle_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create font recycle manager. + * @param max_size recycle size. + * @return pointer to font recycle manager. + */ +lv_font_manager_recycle_t * lv_font_manager_recycle_create(uint32_t max_size); + +/** + * Delete font recycle manager. + * @param manager pointer to font recycle manager. + */ +void lv_font_manager_recycle_delete(lv_font_manager_recycle_t * manager); + +/** + * Get a reusable font. + * @param manager pointer to font recycle manager. + * @param ft_info font info. + * @return returns true on success. + */ +lv_font_t * lv_font_manager_recycle_get_reuse(lv_font_manager_recycle_t * manager, const lv_freetype_info_t * ft_info); + +/** + * Set fonts to be reused. + * @param manager pointer to font recycle manager. + * @param ft_info font info. + */ +void lv_font_manager_recycle_set_reuse(lv_font_manager_recycle_t * manager, lv_font_t * font, + const lv_freetype_info_t * ft_info); + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_FONT_MANAGER */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /* LV_FONT_MANAGER_RECYCLE_H */ diff --git a/include/liblvgl/others/font_manager/lv_font_manager_utils.h b/include/liblvgl/others/font_manager/lv_font_manager_utils.h new file mode 100644 index 00000000..a04b287e --- /dev/null +++ b/include/liblvgl/others/font_manager/lv_font_manager_utils.h @@ -0,0 +1,59 @@ +/** + * @file lv_font_manager_utils.h + * + */ +#ifndef LV_FONT_MANAGER_UTILS_H +#define LV_FONT_MANAGER_UTILS_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../misc/lv_types.h" + +#if LV_USE_FONT_MANAGER + +#include "../../libs/freetype/lv_freetype.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef struct { + const char * name; + lv_freetype_font_render_mode_t render_mode; + lv_freetype_font_style_t style; + uint32_t size; +} lv_freetype_info_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Compare font information. + * @param ft_info_1 font information 1. + * @param ft_info_2 font information 2. + * @return return true if the fonts are equal. + */ +bool lv_freetype_info_is_equal(const lv_freetype_info_t * ft_info_1, const lv_freetype_info_t * ft_info_2); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_FONT_MANAGER*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /* LV_FONT_MANAGER_UTILS_H */ diff --git a/include/liblvgl/others/fragment/README.md b/include/liblvgl/others/fragment/README.md new file mode 100644 index 00000000..e69de29b diff --git a/include/liblvgl/extra/others/fragment/lv_fragment.h b/include/liblvgl/others/fragment/lv_fragment.h similarity index 86% rename from include/liblvgl/extra/others/fragment/lv_fragment.h rename to include/liblvgl/others/fragment/lv_fragment.h index 9b7312f8..1e7d29fc 100644 --- a/include/liblvgl/extra/others/fragment/lv_fragment.h +++ b/include/liblvgl/others/fragment/lv_fragment.h @@ -13,12 +13,10 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" +#include "../../core/lv_obj.h" #if LV_USE_FRAGMENT -#include "liblvgl/core/lv_obj.h" - /********************* * DEFINES *********************/ @@ -29,10 +27,6 @@ extern "C" { typedef struct _lv_fragment_manager_t lv_fragment_manager_t; -typedef struct _lv_fragment_t lv_fragment_t; -typedef struct _lv_fragment_class_t lv_fragment_class_t; -typedef struct _lv_fragment_managed_states_t lv_fragment_managed_states_t; - struct _lv_fragment_t { /** * Class of this fragment @@ -126,40 +120,6 @@ struct _lv_fragment_class_t { size_t instance_size; }; -/** - * Fragment states - */ -typedef struct _lv_fragment_managed_states_t { - /** - * Class of the fragment - */ - const lv_fragment_class_t * cls; - /** - * Manager the fragment attached to - */ - lv_fragment_manager_t * manager; - /** - * Container object the fragment adding view to - */ - lv_obj_t * const * container; - /** - * Fragment instance - */ - lv_fragment_t * instance; - /** - * true between `create_obj_cb` and `obj_deleted_cb` - */ - bool obj_created; - /** - * true before `lv_fragment_del_obj` is called. Don't touch any object if this is true - */ - bool destroying_obj; - /** - * true if this fragment is in navigation stack that can be popped - */ - bool in_stack; -} lv_fragment_managed_states_t; - /********************** * GLOBAL PROTOTYPES **********************/ @@ -175,7 +135,7 @@ lv_fragment_manager_t * lv_fragment_manager_create(lv_fragment_t * parent); * Destroy fragment manager instance * @param manager Fragment manager instance */ -void lv_fragment_manager_del(lv_fragment_manager_t * manager); +void lv_fragment_manager_delete(lv_fragment_manager_t * manager); /** * Create object of all fragments managed by this manager. @@ -187,7 +147,7 @@ void lv_fragment_manager_create_obj(lv_fragment_manager_t * manager); * Delete object created by all fragments managed by this manager. Instance of fragments will not be deleted. * @param manager Fragment manager instance */ -void lv_fragment_manager_del_obj(lv_fragment_manager_t * manager); +void lv_fragment_manager_delete_obj(lv_fragment_manager_t * manager); /** * Attach fragment to manager, and add to container. @@ -266,7 +226,6 @@ lv_fragment_t * lv_fragment_manager_find_by_container(lv_fragment_manager_t * ma */ lv_fragment_t * lv_fragment_manager_get_parent_fragment(lv_fragment_manager_t * manager); - /** * Create a fragment instance. * @@ -280,7 +239,7 @@ lv_fragment_t * lv_fragment_create(const lv_fragment_class_t * cls, void * args) * Destroy a fragment. * @param fragment Fragment instance. */ -void lv_fragment_del(lv_fragment_t * fragment); +void lv_fragment_delete(lv_fragment_t * fragment); /** * Get associated manager of this fragment @@ -317,7 +276,7 @@ lv_obj_t * lv_fragment_create_obj(lv_fragment_t * fragment, lv_obj_t * container * * @param fragment Fragment instance. */ -void lv_fragment_del_obj(lv_fragment_t * fragment); +void lv_fragment_delete_obj(lv_fragment_t * fragment); /** * Destroy obj in fragment, and recreate them. @@ -325,7 +284,6 @@ void lv_fragment_del_obj(lv_fragment_t * fragment); */ void lv_fragment_recreate_obj(lv_fragment_t * fragment); - /********************** * MACROS **********************/ diff --git a/include/liblvgl/others/fragment/lv_fragment_private.h b/include/liblvgl/others/fragment/lv_fragment_private.h new file mode 100644 index 00000000..bf0d23f4 --- /dev/null +++ b/include/liblvgl/others/fragment/lv_fragment_private.h @@ -0,0 +1,77 @@ +/** + * @file lv_fragment_private.h + * + */ + +#ifndef LV_FRAGMENT_PRIVATE_H +#define LV_FRAGMENT_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_fragment.h" + +#if LV_USE_FRAGMENT + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** + * Fragment states + */ +struct _lv_fragment_managed_states_t { + /** + * Class of the fragment + */ + const lv_fragment_class_t * cls; + /** + * Manager the fragment attached to + */ + lv_fragment_manager_t * manager; + /** + * Container object the fragment adding view to + */ + lv_obj_t * const * container; + /** + * Fragment instance + */ + lv_fragment_t * instance; + /** + * true between `create_obj_cb` and `obj_deleted_cb` + */ + bool obj_created; + /** + * true before `lv_fragment_delete_obj` is called. Don't touch any object if this is true + */ + bool destroying_obj; + /** + * true if this fragment is in navigation stack that can be popped + */ + bool in_stack; +}; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_FRAGMENT */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_FRAGMENT_PRIVATE_H*/ diff --git a/include/liblvgl/extra/others/gridnav/lv_gridnav.h b/include/liblvgl/others/gridnav/lv_gridnav.h similarity index 72% rename from include/liblvgl/extra/others/gridnav/lv_gridnav.h rename to include/liblvgl/others/gridnav/lv_gridnav.h index 89391208..65bd1e09 100644 --- a/include/liblvgl/extra/others/gridnav/lv_gridnav.h +++ b/include/liblvgl/others/gridnav/lv_gridnav.h @@ -1,50 +1,10 @@ -/** - * @file lv_templ.c - * - */ - -/********************* - * INCLUDES - *********************/ - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -/*This typedef exists purely to keep -Wpedantic happy when the file is empty.*/ -/*It can be removed.*/ -typedef int _keep_pedantic_happy; - -/********************** - * STATIC PROTOTYPES - **********************/ - -/********************** - * STATIC VARIABLES - **********************/ - -/********************** - * MACROS - **********************/ - -/********************** - * GLOBAL FUNCTIONS - **********************/ - -/********************** - * STATIC FUNCTIONS - **********************/ /** * @file lv_gridnav.h * */ -#ifndef LV_GRIDFOCUS_H -#define LV_GRIDFOCUS_H +#ifndef LV_GRIDNAV_H +#define LV_GRIDNAV_H #ifdef __cplusplus extern "C" { @@ -53,7 +13,7 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/core/lv_obj.h" +#include "../../core/lv_obj.h" #if LV_USE_GRIDNAV @@ -80,6 +40,18 @@ typedef enum { * If there is no more room for scrolling the next/previous object will be focused normally */ LV_GRIDNAV_CTRL_SCROLL_FIRST = 0x2, + /** + * Only use left/right keys for grid navigation. Up/down key events will be sent to the + * focused object. + */ + LV_GRIDNAV_CTRL_HORIZONTAL_MOVE_ONLY = 0x4, + + /** + * Only use up/down keys for grid navigation. Left/right key events will be sent to the + * focused object. + */ + LV_GRIDNAV_CTRL_VERTICAL_MOVE_ONLY = 0x8 + } lv_gridnav_ctrl_t; /********************** @@ -120,4 +92,4 @@ void lv_gridnav_set_focused(lv_obj_t * cont, lv_obj_t * to_focus, lv_anim_enable } /*extern "C"*/ #endif -#endif /*LV_GRIDFOCUS_H*/ +#endif /* LV_GRIDNAV_H */ diff --git a/include/liblvgl/extra/others/ime/lv_ime_pinyin.h b/include/liblvgl/others/ime/lv_ime_pinyin.h similarity index 67% rename from include/liblvgl/extra/others/ime/lv_ime_pinyin.h rename to include/liblvgl/others/ime/lv_ime_pinyin.h index 2d70d293..ff85eee8 100644 --- a/include/liblvgl/extra/others/ime/lv_ime_pinyin.h +++ b/include/liblvgl/others/ime/lv_ime_pinyin.h @@ -12,7 +12,8 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lvgl.h" +#include "../../lv_conf_internal.h" +#include "../../core/lv_obj.h" #if LV_USE_IME_PINYIN != 0 @@ -28,6 +29,7 @@ extern "C" { typedef enum { LV_IME_PINYIN_MODE_K26, LV_IME_PINYIN_MODE_K9, + LV_IME_PINYIN_MODE_K9_NUMBER, } lv_ime_pinyin_mode_t; /*Data of pinyin_dict*/ @@ -41,33 +43,12 @@ typedef struct { char py_str[7]; } ime_pinyin_k9_py_str_t; -/*Data of lv_ime_pinyin*/ -typedef struct { - lv_obj_t obj; - lv_obj_t * kb; - lv_obj_t * cand_panel; - lv_pinyin_dict_t * dict; - lv_ll_t k9_legal_py_ll; - char * cand_str; /* Candidate string */ - char input_char[16]; /* Input box character */ -#if LV_IME_PINYIN_USE_K9_MODE - char k9_input_str[LV_IME_PINYIN_K9_MAX_INPUT]; /* 9-key input(k9) mode input string */ - uint16_t k9_py_ll_pos; /* Current pinyin map pages(k9) */ - uint16_t k9_legal_py_count; /* Count of legal Pinyin numbers(k9) */ - uint16_t k9_input_str_len; /* 9-key input(k9) mode input string max len */ -#endif - uint16_t ta_count; /* The number of characters entered in the text box this time */ - uint16_t cand_num; /* Number of candidates */ - uint16_t py_page; /* Current pinyin map pages(k26) */ - uint16_t py_num[26]; /* Number and length of Pinyin */ - uint16_t py_pos[26]; /* Pinyin position */ - uint8_t mode : 1; /* Set mode, 1: 26-key input(k26), 0: 9-key input(k9). Default: 1. */ -} lv_ime_pinyin_t; - /*********************** * GLOBAL VARIABLES ***********************/ +extern const lv_obj_class_t lv_ime_pinyin_class; + /********************** * GLOBAL PROTOTYPES **********************/ @@ -80,7 +61,7 @@ lv_obj_t * lv_ime_pinyin_create(lv_obj_t * parent); /** * Set the keyboard of Pinyin input method. * @param obj pointer to a Pinyin input method object - * @param dict pointer to a Pinyin input method keyboard + * @param kb pointer to a Pinyin input method keyboard */ void lv_ime_pinyin_set_keyboard(lv_obj_t * obj, lv_obj_t * kb); @@ -98,7 +79,6 @@ void lv_ime_pinyin_set_dict(lv_obj_t * obj, lv_pinyin_dict_t * dict); */ void lv_ime_pinyin_set_mode(lv_obj_t * obj, lv_ime_pinyin_mode_t mode); - /*===================== * Getter functions *====================*/ @@ -110,7 +90,6 @@ void lv_ime_pinyin_set_mode(lv_obj_t * obj, lv_ime_pinyin_mode_t mode); */ lv_obj_t * lv_ime_pinyin_get_kb(lv_obj_t * obj); - /** * Set the dictionary of Pinyin input method. * @param obj pointer to a Pinyin input method object @@ -118,13 +97,12 @@ lv_obj_t * lv_ime_pinyin_get_kb(lv_obj_t * obj); */ lv_obj_t * lv_ime_pinyin_get_cand_panel(lv_obj_t * obj); - /** * Set the dictionary of Pinyin input method. * @param obj pointer to a Pinyin input method object * @return pointer to the Pinyin input method dictionary */ -lv_pinyin_dict_t * lv_ime_pinyin_get_dict(lv_obj_t * obj); +const lv_pinyin_dict_t * lv_ime_pinyin_get_dict(lv_obj_t * obj); /*===================== * Other functions @@ -141,5 +119,3 @@ lv_pinyin_dict_t * lv_ime_pinyin_get_dict(lv_obj_t * obj); #endif #endif /*LV_USE_IME_PINYIN*/ - - diff --git a/include/liblvgl/others/ime/lv_ime_pinyin_private.h b/include/liblvgl/others/ime/lv_ime_pinyin_private.h new file mode 100644 index 00000000..f422438e --- /dev/null +++ b/include/liblvgl/others/ime/lv_ime_pinyin_private.h @@ -0,0 +1,68 @@ +/** + * @file lv_ime_pinyin_private.h + * + */ + +#ifndef LV_IME_PINYIN_PRIVATE_H +#define LV_IME_PINYIN_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../core/lv_obj_private.h" +#include "lv_ime_pinyin.h" + +#if LV_USE_IME_PINYIN != 0 + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/*Data of lv_ime_pinyin*/ +struct _lv_ime_pinyin_t { + lv_obj_t obj; + lv_obj_t * kb; + lv_obj_t * cand_panel; + const lv_pinyin_dict_t * dict; + lv_ll_t k9_legal_py_ll; + char * cand_str; /* Candidate string */ + char input_char[16]; /* Input box character */ +#if LV_IME_PINYIN_USE_K9_MODE + char k9_input_str[LV_IME_PINYIN_K9_MAX_INPUT + 1]; /* 9-key input(k9) mode input string */ + uint16_t k9_py_ll_pos; /* Current pinyin map pages(k9) */ + uint16_t k9_legal_py_count; /* Count of legal Pinyin numbers(k9) */ + uint16_t k9_input_str_len; /* 9-key input(k9) mode input string max len */ +#endif + uint16_t ta_count; /* The number of characters entered in the text box this time */ + uint16_t cand_num; /* Number of candidates */ + uint16_t py_page; /* Current pinyin map pages(k26) */ + uint16_t py_num[26]; /* Number and length of Pinyin */ + uint16_t py_pos[26]; /* Pinyin position */ + lv_ime_pinyin_mode_t mode; /* Set mode, 1: 26-key input(k26), 0: 9-key input(k9). Default: 1. */ +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_IME_PINYIN != 0 */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_IME_PINYIN_PRIVATE_H*/ diff --git a/include/liblvgl/extra/others/imgfont/lv_imgfont.h b/include/liblvgl/others/imgfont/lv_imgfont.h similarity index 68% rename from include/liblvgl/extra/others/imgfont/lv_imgfont.h rename to include/liblvgl/others/imgfont/lv_imgfont.h index 4fb6f78b..68c317a6 100644 --- a/include/liblvgl/extra/others/imgfont/lv_imgfont.h +++ b/include/liblvgl/others/imgfont/lv_imgfont.h @@ -13,7 +13,8 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lvgl.h" +#include "../../lv_conf_internal.h" +#include "../../font/lv_font.h" #if LV_USE_IMGFONT @@ -26,8 +27,9 @@ extern "C" { **********************/ /* gets the image path name of this character */ -typedef bool (*lv_get_imgfont_path_cb_t)(const lv_font_t * font, void * img_src, - uint16_t len, uint32_t unicode, uint32_t unicode_next); +typedef const void * (*lv_imgfont_get_path_cb_t)(const lv_font_t * font, + uint32_t unicode, uint32_t unicode_next, + int32_t * offset_y, void * user_data); /********************** * GLOBAL PROTOTYPES @@ -37,9 +39,10 @@ typedef bool (*lv_get_imgfont_path_cb_t)(const lv_font_t * font, void * img_src, * Creates a image font with info parameter specified. * @param height font size * @param path_cb a function to get the image path name of character. + * @param user_data pointer to user data * @return pointer to the new imgfont or NULL if create error. */ -lv_font_t * lv_imgfont_create(uint16_t height, lv_get_imgfont_path_cb_t path_cb); +lv_font_t * lv_imgfont_create(uint16_t height, lv_imgfont_get_path_cb_t path_cb, void * user_data); /** * Destroy a image font that has been created. diff --git a/include/liblvgl/extra/others/monkey/lv_monkey.h b/include/liblvgl/others/monkey/lv_monkey.h similarity index 89% rename from include/liblvgl/extra/others/monkey/lv_monkey.h rename to include/liblvgl/others/monkey/lv_monkey.h index 589daf1d..97fc6e37 100644 --- a/include/liblvgl/extra/others/monkey/lv_monkey.h +++ b/include/liblvgl/others/monkey/lv_monkey.h @@ -12,7 +12,8 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lvgl.h" +#include "../../lv_conf_internal.h" +#include "../../indev/lv_indev.h" #if LV_USE_MONKEY != 0 @@ -23,17 +24,19 @@ extern "C" { /********************** * TYPEDEFS **********************/ -struct _lv_monkey; -typedef struct _lv_monkey lv_monkey_t; -typedef struct { +typedef struct _lv_monkey_t lv_monkey_t; + +struct _lv_monkey_config_t { /**< Input device type*/ lv_indev_type_t type; /**< Monkey execution period*/ struct { + //! @cond Doxygen_Suppress uint32_t min; uint32_t max; + //! @endcond } period_range; /**< The range of input value*/ @@ -41,7 +44,7 @@ typedef struct { int32_t min; int32_t max; } input_range; -} lv_monkey_config_t; +}; /********************** * GLOBAL PROTOTYPES @@ -81,8 +84,6 @@ void lv_monkey_set_enable(lv_monkey_t * monkey, bool en); */ bool lv_monkey_get_enable(lv_monkey_t * monkey); -#if LV_USE_USER_DATA - /** * Set the user_data field of the monkey * @param monkey pointer to a monkey @@ -97,13 +98,11 @@ void lv_monkey_set_user_data(lv_monkey_t * monkey, void * user_data); */ void * lv_monkey_get_user_data(lv_monkey_t * monkey); -#endif/*LV_USE_USER_DATA*/ - /** * Delete monkey * @param monkey pointer to monkey */ -void lv_monkey_del(lv_monkey_t * monkey); +void lv_monkey_delete(lv_monkey_t * monkey); /********************** * MACROS diff --git a/include/liblvgl/others/monkey/lv_monkey_private.h b/include/liblvgl/others/monkey/lv_monkey_private.h new file mode 100644 index 00000000..a08ea073 --- /dev/null +++ b/include/liblvgl/others/monkey/lv_monkey_private.h @@ -0,0 +1,43 @@ +/** + * @file lv_monkey_private.h + * + */ + +#ifndef LV_MONKEY_PRIVATE_H +#define LV_MONKEY_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_monkey.h" + +#if LV_USE_MONKEY != 0 + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_MONKEY != 0 */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_MONKEY_PRIVATE_H*/ diff --git a/include/liblvgl/others/observer/lv_observer.h b/include/liblvgl/others/observer/lv_observer.h new file mode 100644 index 00000000..cec77fee --- /dev/null +++ b/include/liblvgl/others/observer/lv_observer.h @@ -0,0 +1,406 @@ +/** + * @file lv_observer.h + * + */ + +#ifndef LV_OBSERVER_H +#define LV_OBSERVER_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../core/lv_obj.h" +#if LV_USE_OBSERVER + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** + * Values for lv_submect_t's `type` field. + */ +typedef enum { + LV_SUBJECT_TYPE_INVALID = 0, /**< indicates subject not initialized yet*/ + LV_SUBJECT_TYPE_NONE = 1, /**< a null value like None or NILt*/ + LV_SUBJECT_TYPE_INT = 2, /**< an int32_t*/ + LV_SUBJECT_TYPE_POINTER = 3, /**< a void pointer*/ + LV_SUBJECT_TYPE_COLOR = 4, /**< an lv_color_t*/ + LV_SUBJECT_TYPE_GROUP = 5, /**< an array of subjects*/ + LV_SUBJECT_TYPE_STRING = 6, /**< a char pointer*/ +} lv_subject_type_t; + +/** + * A common type to handle all the various observable types in the same way + */ +typedef union { + int32_t num; /**< Integer number (opacity, enums, booleans or "normal" numbers)*/ + const void * pointer; /**< Constant pointer (string buffer, format string, font, cone text, etc)*/ + lv_color_t color; /**< Color */ +} lv_subject_value_t; + +/** + * The subject (an observable value) + */ +typedef struct { + lv_ll_t subs_ll; /**< Subscribers*/ + uint32_t type : 4; + uint32_t size : 28; /**< Might be used to store a size related to `type`*/ + lv_subject_value_t value; /**< Actual value*/ + lv_subject_value_t prev_value; /**< Previous value*/ + uint32_t notify_restart_query : 1; /**< If an observer deleted start notifying from the beginning. */ + void * user_data; /**< Additional parameter, can be used freely by the user*/ +} lv_subject_t; + +/** + * Callback called when the observed value changes + * @param observer pointer to the observer of the callback + * @param subject pointer to the subject of the observer + */ +typedef void (*lv_observer_cb_t)(lv_observer_t * observer, lv_subject_t * subject); + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Initialize an integer type subject + * @param subject pointer to the subject + * @param value initial value + */ +void lv_subject_init_int(lv_subject_t * subject, int32_t value); + +/** + * Set the value of an integer subject. It will notify all the observers as well. + * @param subject pointer to the subject + * @param value the new value + */ +void lv_subject_set_int(lv_subject_t * subject, int32_t value); + +/** + * Get the current value of an integer subject + * @param subject pointer to the subject + * @return the current value + */ +int32_t lv_subject_get_int(lv_subject_t * subject); + +/** + * Get the previous value of an integer subject + * @param subject pointer to the subject + * @return the current value + */ +int32_t lv_subject_get_previous_int(lv_subject_t * subject); + +/** + * Initialize a string type subject + * @param subject pointer to the subject + * @param buf pointer to a buffer to store the string + * @param prev_buf pointer to a buffer to store the previous string, can be NULL if not used + * @param size size of the buffer + * @param value initial value as a string, e.g. "hello" + * @note the string subject stores the whole string, not only a pointer + */ +void lv_subject_init_string(lv_subject_t * subject, char * buf, char * prev_buf, size_t size, const char * value); + +/** + * Copy a string to a subject. It will notify all the observers as well. + * @param subject pointer to the subject + * @param buf the new string + */ +void lv_subject_copy_string(lv_subject_t * subject, const char * buf); + +/** + * Get the current value of an string subject + * @param subject pointer to the subject + * @return pointer to the buffer containing the current value + */ +const char * lv_subject_get_string(lv_subject_t * subject); + +/** + * Get the previous value of an string subject + * @param subject pointer to the subject + * @return pointer to the buffer containing the current value + * @note NULL will be returned if NULL was passed in `lv_subject_init_string()` + * as `prev_buf` + */ +const char * lv_subject_get_previous_string(lv_subject_t * subject); + +/** + * Initialize an pointer type subject + * @param subject pointer to the subject + * @param value initial value + */ +void lv_subject_init_pointer(lv_subject_t * subject, void * value); + +/** + * Set the value of a pointer subject. It will notify all the observers as well. + * @param subject pointer to the subject + * @param ptr new value + */ +void lv_subject_set_pointer(lv_subject_t * subject, void * ptr); + +/** + * Get the current value of a pointer subject + * @param subject pointer to the subject + * @return current value + */ +const void * lv_subject_get_pointer(lv_subject_t * subject); + +/** + * Get the previous value of a pointer subject + * @param subject pointer to the subject + * @return current value + */ +const void * lv_subject_get_previous_pointer(lv_subject_t * subject); + +/** + * Initialize an color type subject + * @param subject pointer to the subject + * @param color initial value + */ +void lv_subject_init_color(lv_subject_t * subject, lv_color_t color); + +/** + * Set the value of a color subject. It will notify all the observers as well. + * @param subject pointer to the subject + * @param color new value + */ +void lv_subject_set_color(lv_subject_t * subject, lv_color_t color); + +/** + * Get the current value of a color subject + * @param subject pointer to the subject + * @return current value + */ +lv_color_t lv_subject_get_color(lv_subject_t * subject); + +/** + * Get the previous value of a color subject + * @param subject pointer to the subject + * @return current value + */ +lv_color_t lv_subject_get_previous_color(lv_subject_t * subject); + +/** + * Initialize a subject group + * @param subject pointer to the subject + * @param list list of other subject addresses, any of these changes `subject` will be notified + * @param list_len number of elements in `list` + */ +void lv_subject_init_group(lv_subject_t * subject, lv_subject_t * list[], uint32_t list_len); + +/** + * Remove all the observers from a subject and free all allocated memories in it + * @param subject pointer to the subject + * @note objects added with `lv_subject_add_observer_obj` should be already deleted or + * removed manually. + */ +void lv_subject_deinit(lv_subject_t * subject); + +/** + * Get an element from the subject group's list + * @param subject pointer to the subject + * @param index index of the element to get + * @return pointer a subject from the list, or NULL if the index is out of bounds + */ +lv_subject_t * lv_subject_get_group_element(lv_subject_t * subject, int32_t index); + +/** + * Add an observer to a subject. When the subject changes `observer_cb` will be called. + * @param subject pointer to the subject + * @param observer_cb callback to call + * @param user_data optional user data + * @return pointer to the created observer + */ +lv_observer_t * lv_subject_add_observer(lv_subject_t * subject, lv_observer_cb_t observer_cb, void * user_data); + +/** + * Add an observer to a subject for an object. + * When the object is deleted, it will be removed from the subject automatically. + * @param subject pointer to the subject + * @param observer_cb callback to call + * @param obj pointer to an object + * @param user_data optional user data + * @return pointer to the created observer + */ +lv_observer_t * lv_subject_add_observer_obj(lv_subject_t * subject, lv_observer_cb_t observer_cb, lv_obj_t * obj, + void * user_data); + +/** + * Add an observer to a subject and also save a target. + * @param subject pointer to the subject + * @param observer_cb callback to call + * @param target pointer to any data + * @param user_data optional user data + * @return pointer to the created observer + */ +lv_observer_t * lv_subject_add_observer_with_target(lv_subject_t * subject, lv_observer_cb_t observer_cb, + void * target, void * user_data); + +/** + * Remove an observer from its subject + * @param observer pointer to an observer + */ +void lv_observer_remove(lv_observer_t * observer); + +/** + * Remove the observers of an object from a subject or all subjects + * @param obj the object whose observers should be removed + * @param subject the subject to remove the object from, or `NULL` to remove from all subjects + * @note This function can be used e.g. when an object's subject(s) needs to be replaced by other subject(s) + */ +void lv_obj_remove_from_subject(lv_obj_t * obj, lv_subject_t * subject); + +/** + * Get the target of an observer + * @param observer pointer to an observer + * @return pointer to the saved target + */ +void * lv_observer_get_target(lv_observer_t * observer); + +/** + * Get the target object of the observer. + * It's the same as `lv_observer_get_target` and added only + * for semantic reasons + * @param observer pointer to an observer + * @return pointer to the saved object target + */ +lv_obj_t * lv_observer_get_target_obj(lv_observer_t * observer); + +/** + * Get the user data of the observer. + * @param observer pointer to an observer + * @return void pointer to the saved user data +*/ +void * lv_observer_get_user_data(const lv_observer_t * observer); + +/** + * Notify all observers of subject + * @param subject pointer to a subject + */ +void lv_subject_notify(lv_subject_t * subject); + +/** + * Set an object flag if an integer subject's value is equal to a reference value, clear the flag otherwise + * @param obj pointer to an object + * @param subject pointer to a subject + * @param flag flag to set or clear (e.g. `LV_OBJ_FLAG_HIDDEN`) + * @param ref_value reference value to compare the subject's value with + * @return pointer to the created observer + */ +lv_observer_t * lv_obj_bind_flag_if_eq(lv_obj_t * obj, lv_subject_t * subject, lv_obj_flag_t flag, int32_t ref_value); + +/** + * Set an object flag if an integer subject's value is not equal to a reference value, clear the flag otherwise + * @param obj pointer to an object + * @param subject pointer to a subject + * @param flag flag to set or clear (e.g. `LV_OBJ_FLAG_HIDDEN`) + * @param ref_value reference value to compare the subject's value with + * @return pointer to the created observer + */ +lv_observer_t * lv_obj_bind_flag_if_not_eq(lv_obj_t * obj, lv_subject_t * subject, lv_obj_flag_t flag, + int32_t ref_value); + +/** + * Set an object state if an integer subject's value is equal to a reference value, clear the flag otherwise + * @param obj pointer to an object + * @param subject pointer to a subject + * @param state state to set or clear (e.g. `LV_STATE_CHECKED`) + * @param ref_value reference value to compare the subject's value with + * @return pointer to the created observer + */ +lv_observer_t * lv_obj_bind_state_if_eq(lv_obj_t * obj, lv_subject_t * subject, lv_state_t state, int32_t ref_value); + +/** + * Set an object state if an integer subject's value is not equal to a reference value, clear the flag otherwise + * @param obj pointer to an object + * @param subject pointer to a subject + * @param state state to set or clear (e.g. `LV_STATE_CHECKED`) + * @param ref_value reference value to compare the subject's value with + * @return pointer to the created observer + */ +lv_observer_t * lv_obj_bind_state_if_not_eq(lv_obj_t * obj, lv_subject_t * subject, lv_state_t state, + int32_t ref_value); + +/** + * Set an integer subject to 1 when an object is checked and set it 0 when unchecked. + * @param obj pointer to an object + * @param subject pointer to a subject + * @return pointer to the created observer + * @note Ensure the object's `LV_OBJ_FLAG_CHECKABLE` flag is set + */ +lv_observer_t * lv_obj_bind_checked(lv_obj_t * obj, lv_subject_t * subject); + +#if LV_USE_LABEL +/** + * Bind an integer, string, or pointer subject to a label. + * @param obj pointer to a label + * @param subject pointer to a subject + * @param fmt optional format string with 1 format specifier (e.g. "%d °C") + * or NULL to bind the value directly. + * @return pointer to the created observer + * @note fmt == NULL can be used only with string and pointer subjects. + * @note if the subject is a pointer must point to a `\0` terminated string. + */ +lv_observer_t * lv_label_bind_text(lv_obj_t * obj, lv_subject_t * subject, const char * fmt); +#endif + +#if LV_USE_ARC +/** + * Bind an integer subject to an arc's value + * @param obj pointer to an arc + * @param subject pointer to a subject + * @return pointer to the created observer + */ +lv_observer_t * lv_arc_bind_value(lv_obj_t * obj, lv_subject_t * subject); +#endif + +#if LV_USE_SLIDER +/** + * Bind an integer subject to a slider's value + * @param obj pointer to a slider + * @param subject pointer to a subject + * @return pointer to the created observer + */ +lv_observer_t * lv_slider_bind_value(lv_obj_t * obj, lv_subject_t * subject); +#endif + +#if LV_USE_ROLLER +/** + * Bind an integer subject to a roller's value + * @param obj pointer to a roller + * @param subject pointer to a subject + * @return pointer to the created observer + */ +lv_observer_t * lv_roller_bind_value(lv_obj_t * obj, lv_subject_t * subject); +#endif + +#if LV_USE_DROPDOWN +/** + * Bind an integer subject to a dropdown's value + * @param obj pointer to a drop down + * @param subject pointer to a subject + * @return pointer to the created observer + */ +lv_observer_t * lv_dropdown_bind_value(lv_obj_t * obj, lv_subject_t * subject); +#endif + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_OBSERVER*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_OBSERVER_H*/ diff --git a/include/liblvgl/others/observer/lv_observer_private.h b/include/liblvgl/others/observer/lv_observer_private.h new file mode 100644 index 00000000..40a22dac --- /dev/null +++ b/include/liblvgl/others/observer/lv_observer_private.h @@ -0,0 +1,57 @@ +/** + * @file lv_observer_private.h + * + */ + +#ifndef LV_OBSERVER_PRIVATE_H +#define LV_OBSERVER_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_observer.h" + +#if LV_USE_OBSERVER + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** + * The observer object: a descriptor returned when subscribing LVGL widgets to subjects + */ +struct _lv_observer_t { + lv_subject_t * subject; /**< The observed value */ + lv_observer_cb_t cb; /**< Callback that should be called when the value changes*/ + void * target; /**< A target for the observer, e.g. a widget or style*/ + void * user_data; /**< Additional parameter supplied when subscribing*/ + uint32_t auto_free_user_data : 1; /**< Automatically free user data when the observer is removed */ + uint32_t notified : 1; /**< Mark if this observer was already notified*/ + uint32_t for_obj : 1; /**< `target` is an `lv_obj_t *`*/ +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_OBSERVER */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_OBSERVER_PRIVATE_H*/ diff --git a/include/liblvgl/others/snapshot/lv_snapshot.h b/include/liblvgl/others/snapshot/lv_snapshot.h new file mode 100644 index 00000000..90de106c --- /dev/null +++ b/include/liblvgl/others/snapshot/lv_snapshot.h @@ -0,0 +1,101 @@ +/** + * @file lv_snapshot.h + * + */ + +#ifndef LV_SNAPSHOT_H +#define LV_SNAPSHOT_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../core/lv_obj.h" + +#if LV_USE_SNAPSHOT + +#include +#include + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Take snapshot for object with its children, create the draw buffer as needed. + * @param obj the object to generate snapshot. + * @param cf color format for generated image. + * @return a pointer to an draw buffer containing snapshot image, or NULL if failed. + */ +lv_draw_buf_t * lv_snapshot_take(lv_obj_t * obj, lv_color_format_t cf); + +/** + * Create a draw buffer to store the snapshot image for object. + * @param obj the object to generate snapshot. + * @param cf color format for generated image. + * @return a pointer to an draw buffer ready for taking snapshot, or NULL if failed. + */ +lv_draw_buf_t * lv_snapshot_create_draw_buf(lv_obj_t * obj, lv_color_format_t cf); + +/** + * Reshape the draw buffer to prepare for taking snapshot for obj. + * This is usually used to check if the existing draw buffer is enough for + * obj snapshot. If return LV_RESULT_INVALID, you should create a new one. + * @param draw_buf the draw buffer to reshape. + * @param obj the object to generate snapshot. + */ +lv_result_t lv_snapshot_reshape_draw_buf(lv_obj_t * obj, lv_draw_buf_t * draw_buf); + +/** + * Take snapshot for object with its children, save image info to provided buffer. + * @param obj the object to generate snapshot. + * @param cf color format for new snapshot image. + * It could differ with cf of `draw_buf` as long as the new cf will fit in. + * @param draw_buf the draw buffer to store the image result. It's reshaped automatically. + * @return LV_RESULT_OK on success, LV_RESULT_INVALID on error. + */ +lv_result_t lv_snapshot_take_to_draw_buf(lv_obj_t * obj, lv_color_format_t cf, lv_draw_buf_t * draw_buf); + +/** + * @deprecated Use `lv_draw_buf_destroy` instead. + * + * Free the snapshot image returned by @ref lv_snapshot_take + * @param dsc the image descriptor generated by lv_snapshot_take. + */ +void lv_snapshot_free(lv_image_dsc_t * dsc); + +/** + * Take snapshot for object with its children, save image info to provided buffer. + * @param obj the object to generate snapshot. + * @param cf color format for generated image. + * @param dsc image descriptor to store the image result. + * @param buf the buffer to store image data. It must meet align requirement. + * @param buf_size provided buffer size in bytes. + * @return LV_RESULT_OK on success, LV_RESULT_INVALID on error. + * @deprecated Use lv_snapshot_take_to_draw_buf instead. + */ +lv_result_t lv_snapshot_take_to_buf(lv_obj_t * obj, lv_color_format_t cf, lv_image_dsc_t * dsc, + void * buf, + uint32_t buf_size); + +/********************** + * MACROS + **********************/ +#endif /*LV_USE_SNAPSHOT*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif diff --git a/include/liblvgl/others/sysmon/lv_sysmon.h b/include/liblvgl/others/sysmon/lv_sysmon.h new file mode 100644 index 00000000..0859c064 --- /dev/null +++ b/include/liblvgl/others/sysmon/lv_sysmon.h @@ -0,0 +1,91 @@ +/** + * @file lv_sysmon.h + * + */ + +#ifndef LV_SYSMON_H +#define LV_SYSMON_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../misc/lv_timer.h" +#include "../../others/observer/lv_observer.h" + +#if LV_USE_SYSMON + +#if LV_USE_LABEL == 0 +#error "lv_sysmon: lv_label is required. Enable it in lv_conf.h (LV_USE_LABEL 1) " +#endif + +#if LV_USE_OBSERVER == 0 +#error "lv_observer: lv_observer is required. Enable it in lv_conf.h (LV_USE_OBSERVER 1) " +#endif + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create a new system monitor label + * @param disp create the sys. mon. on this display's system layer + * @return the create label + */ +lv_obj_t * lv_sysmon_create(lv_display_t * disp); + +#if LV_USE_PERF_MONITOR + +/** + * Show system performance monitor: CPU usage and FPS count + * @param disp target display, NULL: use the default displays + */ +void lv_sysmon_show_performance(lv_display_t * disp); + +/** + * Hide system performance monitor + * @param disp target display, NULL: use the default + */ +void lv_sysmon_hide_performance(lv_display_t * disp); + +#endif /*LV_USE_PERF_MONITOR*/ + +#if LV_USE_MEM_MONITOR + +/** + * Show system memory monitor: used memory and the memory fragmentation + * @param disp target display, NULL: use the default displays + */ +void lv_sysmon_show_memory(lv_display_t * disp); + +/** + * Hide system memory monitor + * @param disp target display, NULL: use the default displays + */ +void lv_sysmon_hide_memory(lv_display_t * disp); + +#endif /*LV_USE_MEM_MONITOR*/ + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_SYSMON*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_SYSMON_H*/ diff --git a/include/liblvgl/others/sysmon/lv_sysmon_private.h b/include/liblvgl/others/sysmon/lv_sysmon_private.h new file mode 100644 index 00000000..b4fa3877 --- /dev/null +++ b/include/liblvgl/others/sysmon/lv_sysmon_private.h @@ -0,0 +1,91 @@ +/** + * @file lv_sysmon_private.h + * + */ + +#ifndef LV_SYSMON_PRIVATE_H +#define LV_SYSMON_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_sysmon.h" + +#if LV_USE_SYSMON + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_sysmon_backend_data_t { + lv_subject_t subject; + lv_timer_t * timer; +}; + +#if LV_USE_PERF_MONITOR +struct _lv_sysmon_perf_info_t { + struct { + bool inited; + uint32_t refr_start; + uint32_t refr_interval_sum; + uint32_t refr_elaps_sum; + uint32_t refr_cnt; + uint32_t render_start; + uint32_t render_elaps_sum; /*Contains the flush time too*/ + uint32_t render_cnt; + uint32_t flush_in_render_start; + uint32_t flush_in_render_elaps_sum; + uint32_t flush_not_in_render_start; + uint32_t flush_not_in_render_elaps_sum; + uint32_t last_report_timestamp; + uint32_t render_in_progress : 1; + } measured; + + struct { + uint32_t fps; + uint32_t cpu; + uint32_t refr_avg_time; + uint32_t render_avg_time; /**< Pure rendering time without flush time*/ + uint32_t flush_avg_time; /**< Pure flushing time without rendering time*/ + uint32_t cpu_avg_total; + uint32_t fps_avg_total; + uint32_t run_cnt; + } calculated; + +}; +#endif + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Initialize built-in system monitor, such as performance and memory monitor. + */ +void lv_sysmon_builtin_init(void); + +/** + * DeInitialize built-in system monitor, such as performance and memory monitor. + */ +void lv_sysmon_builtin_deinit(void); + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_SYSMON */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_SYSMON_PRIVATE_H*/ diff --git a/include/liblvgl/others/vg_lite_tvg/vg_lite.h b/include/liblvgl/others/vg_lite_tvg/vg_lite.h new file mode 100644 index 00000000..abbd6e86 --- /dev/null +++ b/include/liblvgl/others/vg_lite_tvg/vg_lite.h @@ -0,0 +1,1387 @@ +/**************************************************************************** +* +* Copyright 2012 - 2023 Vivante Corporation, Santa Clara, California. +* All Rights Reserved. +* +* Permission is hereby granted, free of charge, to any person obtaining +* a copy of this software and associated documentation files (the +* 'Software'), to deal in the Software without restriction, including +* without limitation the rights to use, copy, modify, merge, publish, +* distribute, sub license, and/or sell copies of the Software, and to +* permit persons to whom the Software is furnished to do so, subject +* to the following conditions: +* +* The above copyright notice and this permission notice (including the +* next paragraph) shall be included in all copies or substantial +* portions of the Software. +* +* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. +* IN NO EVENT SHALL VIVANTE AND/OR ITS SUPPLIERS BE LIABLE FOR ANY +* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +* +*****************************************************************************/ + +#ifndef _vg_lite_h_ +#define _vg_lite_h_ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * causes MSVC error C1189. + * Not needed because "The __inline keyword is equivalent to inline." + * See: https://learn.microsoft.com/en-us/cpp/cpp/inline-functions-cpp?view=msvc-170 +*/ +/* +#if defined(_MSC_VER) +#define inline __inline +#endif +*/ + +#include +#include + + +/* VGLite API Constants *******************************************************************************************************************/ + +#define VGLITE_HEADER_VERSION 7 + +#ifndef VGLITE_VERSION_3_0 +#define VGLITE_VERSION_3_0 1 + +#define VGLITE_MAKE_VERSION(major, minor, patch) (((major) << 16) | ((minor) << 8) | (patch)) +#define VGLITE_VERSION_MAJOR(version) (((uint32_t)(version) >> 16) & 0xff) +#define VGLITE_VERSION_MINOR(version) (((uint32_t)(version) >> 8) & 0xff) +#define VGLITE_VERSION_PATCH(version) ((uint32_t)(version) & 0xff) + +#define VGLITE_API_VERSION_3_0 VGLITE_MAKE_VERSION(3, 0, 0) + +#define VGLITE_RELEASE_VERSION VGLITE_MAKE_VERSION(4,0,47) + +#define VGL_FALSE 0 +#define VGL_TRUE 1 + +/* Path command (op code). */ +#define VLC_OP_END 0x00 +#define VLC_OP_CLOSE 0x01 +#define VLC_OP_MOVE 0x02 +#define VLC_OP_MOVE_REL 0x03 +#define VLC_OP_LINE 0x04 +#define VLC_OP_LINE_REL 0x05 +#define VLC_OP_QUAD 0x06 +#define VLC_OP_QUAD_REL 0x07 +#define VLC_OP_CUBIC 0x08 +#define VLC_OP_CUBIC_REL 0x09 +#define VLC_OP_BREAK 0x0A +#define VLC_OP_HLINE 0x0B +#define VLC_OP_HLINE_REL 0x0C +#define VLC_OP_VLINE 0x0D +#define VLC_OP_VLINE_REL 0x0E +#define VLC_OP_SQUAD 0x0F +#define VLC_OP_SQUAD_REL 0x10 +#define VLC_OP_SCUBIC 0x11 +#define VLC_OP_SCUBIC_REL 0x12 +#define VLC_OP_SCCWARC 0x13 +#define VLC_OP_SCCWARC_REL 0x14 +#define VLC_OP_SCWARC 0x15 +#define VLC_OP_SCWARC_REL 0x16 +#define VLC_OP_LCCWARC 0x17 +#define VLC_OP_LCCWARC_REL 0x18 +#define VLC_OP_LCWARC 0x19 +#define VLC_OP_LCWARC_REL 0x1A + +/* Macros for path manipulating: See path definitions. */ +#define VLM_PATH_ENABLE_UPLOAD(path) (path).uploaded.property |= 1 +#define VLM_PATH_DISABLE_UPLOAD(path) (path).uploaded.property &= (~1) +#define VLM_PATH_GET_UPLOAD_BIT(path) ((path).uploaded.property & 1) + +/* Gradient constants. */ +#define VLC_MAX_COLOR_RAMP_STOPS 256 /*! The max number of radial gradient stops. */ +#define VLC_MAX_GRADIENT_STOPS 16 /*! The max number of gradient stops. */ +#define VLC_GRADIENT_BUFFER_WIDTH 1024 /*! The internal gradient buffer width.*/ + + +/* API name defines for backward compatibility to VGLite 2.0 APIs */ +#define vg_lite_buffer_upload vg_lite_upload_buffer +#define vg_lite_path_append vg_lite_append_path +#define vg_lite_path_calc_length vg_lite_get_path_length +#define vg_lite_set_ts_buffer vg_lite_set_tess_buffer +#define vg_lite_set_draw_path_type vg_lite_set_path_type +#define vg_lite_create_mask_layer vg_lite_create_masklayer +#define vg_lite_fill_mask_layer vg_lite_fill_masklayer +#define vg_lite_blend_mask_layer vg_lite_blend_masklayer +#define vg_lite_generate_mask_layer_by_path vg_lite_render_masklayer +#define vg_lite_set_mask_layer vg_lite_set_masklayer +#define vg_lite_destroy_mask_layer vg_lite_destroy_masklayer +#define vg_lite_enable_mask vg_lite_enable_masklayer +#define vg_lite_enable_color_transformation vg_lite_enable_color_transform +#define vg_lite_set_color_transformation vg_lite_set_color_transform +#define vg_lite_set_image_global_alpha vg_lite_source_global_alpha +#define vg_lite_set_dest_global_alpha vg_lite_dest_global_alpha +#define vg_lite_clear_rad_grad vg_lite_clear_radial_grad +#define vg_lite_update_rad_grad vg_lite_update_radial_grad +#define vg_lite_get_rad_grad_matrix vg_lite_get_radial_grad_matrix +#define vg_lite_set_rad_grad vg_lite_set_radial_grad +#define vg_lite_draw_linear_gradient vg_lite_draw_linear_grad +#define vg_lite_draw_radial_gradient vg_lite_draw_radial_grad +#define vg_lite_draw_gradient vg_lite_draw_grad +#define vg_lite_mem_avail vg_lite_get_mem_size +#define vg_lite_set_update_stroke vg_lite_update_stroke + +#define vg_lite_buffer_image_mode_t vg_lite_image_mode_t +#define vg_lite_draw_path_type_t vg_lite_path_type_t +#define vg_lite_linear_gradient_ext_t vg_lite_ext_linear_gradient_t +#define vg_lite_buffer_transparency_mode_t vg_lite_transparency_t + + +/* VGLite API Types ***********************************************************************************************************************/ + +typedef unsigned char vg_lite_uint8_t; +typedef char vg_lite_int8_t; +typedef short vg_lite_int16_t; +typedef unsigned short vg_lite_uint16_t; +typedef int vg_lite_int32_t; +typedef unsigned int vg_lite_uint32_t; +typedef unsigned long long vg_lite_uint64_t; +typedef float vg_lite_float_t; +typedef double vg_lite_double_t; +typedef char vg_lite_char; +typedef char* vg_lite_string; +typedef void* vg_lite_pointer; +typedef void vg_lite_void; +typedef unsigned int vg_lite_color_t; + + +/* VGLite API Enumerations ****************************************************************************************************************/ + +#ifndef VG_LITE_ERROR +#define VG_LITE_ERROR 1 + + /* Error codes that the vg_lite functions can return. */ + typedef enum vg_lite_error + { + VG_LITE_SUCCESS = 0, /*! Success. */ + VG_LITE_INVALID_ARGUMENT, /*! An invalid argument was specified. */ + VG_LITE_OUT_OF_MEMORY, /*! Out of memory. */ + VG_LITE_NO_CONTEXT, /*! No context or an unintialized context specified. */ + VG_LITE_TIMEOUT, /*! A timeout has occurred during a wait. */ + VG_LITE_OUT_OF_RESOURCES, /*! Out of system resources. */ + VG_LITE_GENERIC_IO, /*! Cannot communicate with the kernel driver. */ + VG_LITE_NOT_SUPPORT, /*! Function call not supported. */ + VG_LITE_ALREADY_EXISTS, /*! Object already exists */ + VG_LITE_NOT_ALIGNED, /*! Data alignment error */ + VG_LITE_FLEXA_TIME_OUT, /*! VG timeout requesting for segment buffer */ + VG_LITE_FLEXA_HANDSHAKE_FAIL, /*! VG and SBI synchronizer handshake failed */ + } vg_lite_error_t; +#endif + + /* Chip features bit */ + typedef enum vg_lite_feature + { + gcFEATURE_BIT_VG_IM_INDEX_FORMAT, + gcFEATURE_BIT_VG_SCISSOR, + gcFEATURE_BIT_VG_BORDER_CULLING, + gcFEATURE_BIT_VG_RGBA2_FORMAT, + gcFEATURE_BIT_VG_QUALITY_8X, + gcFEATURE_BIT_VG_IM_FASTCLAER, + gcFEATURE_BIT_VG_RADIAL_GRADIENT, + gcFEATURE_BIT_VG_GLOBAL_ALPHA, + gcFEATURE_BIT_VG_RGBA8_ETC2_EAC, + gcFEATURE_BIT_VG_COLOR_KEY, + gcFEATURE_BIT_VG_DOUBLE_IMAGE, + gcFEATURE_BIT_VG_YUV_OUTPUT, + gcFEATURE_BIT_VG_FLEXA, + gcFEATURE_BIT_VG_24BIT, + gcFEATURE_BIT_VG_DITHER, + gcFEATURE_BIT_VG_USE_DST, + gcFEATURE_BIT_VG_PE_CLEAR, + gcFEATURE_BIT_VG_IM_INPUT, + gcFEATURE_BIT_VG_DEC_COMPRESS, + gcFEATURE_BIT_VG_LINEAR_GRADIENT_EXT, + gcFEATURE_BIT_VG_MASK, + gcFEATURE_BIT_VG_MIRROR, + gcFEATURE_BIT_VG_GAMMA, + gcFEATURE_BIT_VG_NEW_BLEND_MODE, + gcFEATURE_BIT_VG_STENCIL, + gcFEATURE_BIT_VG_SRC_PREMULTIPLIED, /*! Valid only if gcFEATURE_BIT_VG_HW_PREMULTIPLY is 0 */ + gcFEATURE_BIT_VG_HW_PREMULTIPLY, /*! HW multiplier can accept either premultiplied or not */ + gcFEATURE_BIT_VG_COLOR_TRANSFORMATION, + gcFEATURE_BIT_VG_LVGL_SUPPORT, + gcFEATURE_BIT_VG_INDEX_ENDIAN, + gcFEATURE_BIT_VG_24BIT_PLANAR, + gcFEATURE_BIT_VG_PIXEL_MATRIX, + gcFEATURE_BIT_VG_NEW_IMAGE_INDEX, + gcFEATURE_BIT_VG_PARALLEL_PATHS, + gcFEATURE_BIT_VG_STRIPE_MODE, + gcFEATURE_BIT_VG_IM_DEC_INPUT, + gcFEATURE_BIT_VG_GAUSSIAN_BLUR, + gcFEATURE_BIT_VG_RECTANGLE_TILED_OUT, + gcFEATURE_BIT_VG_TESSELLATION_TILED_OUT, + gcFEATURE_BIT_VG_IM_REPEAT_REFLECT, + gcFEATURE_BIT_VG_YUY2_INPUT, + gcFEATURE_BIT_VG_YUV_INPUT, + gcFEATURE_BIT_VG_YUV_TILED_INPUT, + gcFEATURE_BIT_VG_AYUV_INPUT, + gcFEATURE_BIT_VG_16PIXELS_ALIGN, + gcFEATURE_BIT_VG_DEC_COMPRESS_2_0, + gcFEATURE_COUNT + } vg_lite_feature_t; + + /* Rendering quality enums. */ + typedef enum vg_lite_quality + { + VG_LITE_HIGH, /*! High quality 16x anti-aliasing path. */ + VG_LITE_UPPER, /*! Upper quality 8x anti-aliasing path. */ + VG_LITE_MEDIUM, /*! Medium quality 4x anti-aliasing path. */ + VG_LITE_LOW, /*! Low quality path without any anti-aliasing. */ + } vg_lite_quality_t; + + /* Format of path coordinates. */ + typedef enum vg_lite_format + { + VG_LITE_S8, /*! Signed 8-bit coordinates. */ + VG_LITE_S16, /*! Signed 16-bit coordinates. */ + VG_LITE_S32, /*! Signed 32-bit coordinates. */ + VG_LITE_FP32, /*! 32-bit floating point coordinates. */ + } vg_lite_format_t; + + /* Format of pixel buffer. */ + typedef enum vg_lite_buffer_format + { + /* OpenVG VGImageFormat enums: + * Note: The bits for each color channel are stored within a machine word + * from MSB to LSB in the order indicated by the pixel format name. + * This is opposite of VG_LITE_* formats (from LSB to MSB). + */ + + /* RGB{A,X} channel ordering */ + VG_sRGBX_8888 = 0, + VG_sRGBA_8888 = 1, + VG_sRGBA_8888_PRE = 2, + VG_sRGB_565 = 3, + VG_sRGBA_5551 = 4, + VG_sRGBA_4444 = 5, + VG_sL_8 = 6, + VG_lRGBX_8888 = 7, + VG_lRGBA_8888 = 8, + VG_lRGBA_8888_PRE = 9, + VG_lL_8 = 10, + VG_A_8 = 11, + VG_BW_1 = 12, + VG_A_1 = 13, + VG_A_4 = 14, + + VG_sRGBX_8888_PRE = 15, + VG_sRGB_565_PRE = 16, + VG_sRGBA_5551_PRE = 17, + VG_sRGBA_4444_PRE = 18, + VG_lRGBX_8888_PRE = 19, + VG_lRGB_565 = 20, + VG_lRGB_565_PRE = 21, + VG_lRGBA_5551 = 22, + VG_lRGBA_5551_PRE = 23, + VG_lRGBA_4444 = 24, + VG_lRGBA_4444_PRE = 25, + + /* {A,X}RGB channel ordering */ + VG_sXRGB_8888 = 0 | (1 << 6), + VG_sARGB_8888 = 1 | (1 << 6), + VG_sARGB_8888_PRE = 2 | (1 << 6), + VG_sARGB_1555 = 4 | (1 << 6), + VG_sARGB_4444 = 5 | (1 << 6), + VG_lXRGB_8888 = 7 | (1 << 6), + VG_lARGB_8888 = 8 | (1 << 6), + VG_lARGB_8888_PRE = 9 | (1 << 6), + + /* BGR{A,X} channel ordering */ + VG_sBGRX_8888 = 0 | (1 << 7), + VG_sBGRA_8888 = 1 | (1 << 7), + VG_sBGRA_8888_PRE = 2 | (1 << 7), + VG_sBGR_565 = 3 | (1 << 7), + VG_sBGRA_5551 = 4 | (1 << 7), + VG_sBGRA_4444 = 5 | (1 << 7), + VG_lBGRX_8888 = 7 | (1 << 7), + VG_lBGRA_8888 = 8 | (1 << 7), + VG_lBGRA_8888_PRE = 9 | (1 << 7), + + /* {A,X}BGR channel ordering */ + VG_sXBGR_8888 = 0 | (1 << 6) | (1 << 7), + VG_sABGR_8888 = 1 | (1 << 6) | (1 << 7), + VG_sABGR_8888_PRE = 2 | (1 << 6) | (1 << 7), + VG_sABGR_1555 = 4 | (1 << 6) | (1 << 7), + VG_sABGR_4444 = 5 | (1 << 6) | (1 << 7), + VG_lXBGR_8888 = 7 | (1 << 6) | (1 << 7), + VG_lABGR_8888 = 8 | (1 << 6) | (1 << 7), + VG_lABGR_8888_PRE = 9 | (1 << 6) | (1 << 7), + + /* Original VGLite API image format enums: + * Note: The bits for each color channel are stored within a machine word + * from LSB to MSB in the order indicated by the pixel format name. + * This is opposite of OPENVG VG_* formats (from MSB to LSB). + */ + VG_LITE_RGBA8888 = 0 | (1 << 10), + VG_LITE_BGRA8888 = 1 | (1 << 10), + VG_LITE_RGBX8888 = 2 | (1 << 10), + VG_LITE_BGRX8888 = 3 | (1 << 10), + VG_LITE_RGB565 = 4 | (1 << 10), + VG_LITE_BGR565 = 5 | (1 << 10), + VG_LITE_RGBA4444 = 6 | (1 << 10), + VG_LITE_BGRA4444 = 7 | (1 << 10), + VG_LITE_BGRA5551 = 8 | (1 << 10), + VG_LITE_A4 = 9 | (1 << 10), + VG_LITE_A8 = 10 | (1 << 10), + VG_LITE_L8 = 11 | (1 << 10), + VG_LITE_YUYV = 12 | (1 << 10), + VG_LITE_YUY2 = 13 | (1 << 10), + VG_LITE_ANV12 = 14 | (1 << 10), + VG_LITE_AYUY2 = 15 | (1 << 10), + VG_LITE_NV12 = 16 | (1 << 10), + VG_LITE_YV12 = 17 | (1 << 10), + VG_LITE_YV24 = 18 | (1 << 10), + VG_LITE_YV16 = 19 | (1 << 10), + VG_LITE_NV16 = 20 | (1 << 10), + VG_LITE_YUY2_TILED = 21 | (1 << 10), + VG_LITE_NV12_TILED = 22 | (1 << 10), + VG_LITE_ANV12_TILED = 23 | (1 << 10), + VG_LITE_AYUY2_TILED = 24 | (1 << 10), + VG_LITE_RGBA2222 = 25 | (1 << 10), + VG_LITE_BGRA2222 = 26 | (1 << 10), + VG_LITE_ABGR2222 = 27 | (1 << 10), + VG_LITE_ARGB2222 = 28 | (1 << 10), + VG_LITE_ABGR4444 = 29 | (1 << 10), + VG_LITE_ARGB4444 = 30 | (1 << 10), + VG_LITE_ABGR8888 = 31 | (1 << 10), + VG_LITE_ARGB8888 = 32 | (1 << 10), + VG_LITE_ABGR1555 = 33 | (1 << 10), + VG_LITE_RGBA5551 = 34 | (1 << 10), + VG_LITE_ARGB1555 = 35 | (1 << 10), + VG_LITE_XBGR8888 = 36 | (1 << 10), + VG_LITE_XRGB8888 = 37 | (1 << 10), + VG_LITE_RGBA8888_ETC2_EAC = 38 | (1 << 10), + VG_LITE_RGB888 = 39 | (1 << 10), + VG_LITE_BGR888 = 40 | (1 << 10), + VG_LITE_ABGR8565 = 41 | (1 << 10), + VG_LITE_BGRA5658 = 42 | (1 << 10), + VG_LITE_ARGB8565 = 43 | (1 << 10), + VG_LITE_RGBA5658 = 44 | (1 << 10), + VG_LITE_ABGR8565_PLANAR = 45 | (1 << 10), + VG_LITE_BGRA5658_PLANAR = 46 | (1 << 10), + VG_LITE_ARGB8565_PLANAR = 47 | (1 << 10), + VG_LITE_RGBA5658_PLANAR = 48 | (1 << 10), + + VG_LITE_INDEX_1 = 0 | (1 << 11), /*! Indexed format. */ + VG_LITE_INDEX_2 = 1 | (1 << 11), + VG_LITE_INDEX_4 = 2 | (1 << 11), + VG_LITE_INDEX_8 = 3 | (1 << 11), + + } vg_lite_buffer_format_t; + + /* Swizzle of packed YUV format UV channels. */ + typedef enum vg_lite_swizzle + { + VG_LITE_SWIZZLE_UV, + VG_LITE_SWIZZLE_VU, + } vg_lite_swizzle_t; + + /* The YUV<->RGB conversion rule. */ + typedef enum vg_lite_yuv2rgb + { + VG_LITE_YUV601, + VG_LITE_YUV709, + } vg_lite_yuv2rgb_t; + + /* The pixel layout in a buffer. */ + typedef enum vg_lite_buffer_layout + { + VG_LITE_LINEAR, + VG_LITE_TILED, + } vg_lite_buffer_layout_t; + + /* The image (buffer) rendering mode. Match OpenVG enum VGImageMode */ + typedef enum vg_lite_image_mode + { + /* For enum value backward compatibility */ + VG_LITE_ZERO = 0, + VG_LITE_NORMAL_IMAGE_MODE = 0x1F00, + VG_LITE_MULTIPLY_IMAGE_MODE = 0x1F01, + VG_LITE_STENCIL_MODE = 0x1F02, + VG_LITE_NONE_IMAGE_MODE = 0x1F03, + VG_LITE_RECOLOR_MODE = 0x1F04, + } vg_lite_image_mode_t; + + /* The image (buffer) transparency mode. */ + typedef enum vg_lite_transparency + { + VG_LITE_IMAGE_OPAQUE, + VG_LITE_IMAGE_TRANSPARENT + } vg_lite_transparency_t; + + /* Blending modes. VG_BLEND_* match OpenVG enum VGBlendMode. + * S and D represent source and destination color channels. + * Sa and Da represent the source and destination alpha channels. + */ + typedef enum vg_lite_blend + { + VG_LITE_BLEND_NONE = 0, /*! S, No blend, Non-premultiplied */ + VG_LITE_BLEND_SRC_OVER = 1, /*! S + (1 - Sa) * D , Non-premultiplied */ + VG_LITE_BLEND_DST_OVER = 2, /*! (1 - Da) * S + D , Non-premultiplied */ + VG_LITE_BLEND_SRC_IN = 3, /*! Da * S , Non-premultiplied */ + VG_LITE_BLEND_DST_IN = 4, /*! Sa * D , Non-premultiplied */ + VG_LITE_BLEND_MULTIPLY = 5, /*! S * (1 - Da) + D * (1 - Sa) + S * D , Non-premultiplied */ + VG_LITE_BLEND_SCREEN = 6, /*! S + D - S * D , Non-premultiplied */ + VG_LITE_BLEND_DARKEN = 7, /*! min(SrcOver, DstOver) , Non-premultiplied */ + VG_LITE_BLEND_LIGHTEN = 8, /*! max(SrcOver, DstOver) , Non-premultiplied */ + VG_LITE_BLEND_ADDITIVE = 9, /*! S + D , Non-premultiplied */ + VG_LITE_BLEND_SUBTRACT = 10, /*! D * (1 - Sa) , Non-premultiplied */ + VG_LITE_BLEND_SUBTRACT_LVGL = 11, /*! D - S , Non-premultiplied */ + VG_LITE_BLEND_NORMAL_LVGL = 12, /*! S * Sa + (1 - Sa) * D , Non-premultiplied */ + VG_LITE_BLEND_ADDITIVE_LVGL = 13, /*! (S + D) * Sa + D * (1 - Sa) , Non-premultiplied */ + VG_LITE_BLEND_MULTIPLY_LVGL = 14, /*! (S * D) * Sa + D * (1 - Sa) , Non-premultiplied */ + VG_LITE_BLEND_PREMULTIPLY_SRC_OVER = 15, /*! S * Sa + (1 - Sa) * D , Non-premultiplied */ + + OPENVG_BLEND_SRC = 0x2000, /*! Copy SRC, no blend, Premultiplied */ + OPENVG_BLEND_SRC_OVER = 0x2001, /*! Porter-Duff SRC_OVER blend, Premultiplied */ + OPENVG_BLEND_DST_OVER = 0x2002, /*! Porter-Duff DST_OVER blend, Premultiplied */ + OPENVG_BLEND_SRC_IN = 0x2003, /*! Porter-Duff SRC_IN blend, Premultiplied */ + OPENVG_BLEND_DST_IN = 0x2004, /*! Porter-Duff DST_IN blend, Premultiplied */ + OPENVG_BLEND_MULTIPLY = 0x2005, /*! Porter-Duff MULTIPLY blend, Premultiplied */ + OPENVG_BLEND_SCREEN = 0x2006, /*! Porter-Duff SCREEN blend, Premultiplied */ + OPENVG_BLEND_DARKEN = 0x2007, /*! Porter-Duff DARKEN blend, Premultiplied */ + OPENVG_BLEND_LIGHTEN = 0x2008, /*! Porter-Duff LIGHTEN blend, Premultiplied */ + OPENVG_BLEND_ADDITIVE = 0x2009, /*! Porter-Duff ADDITIVE blend, Premultiplied */ + } vg_lite_blend_t; + + /* Fill rules. Match OpenVG enum VGFillRule */ + typedef enum vg_lite_fill + { + VG_LITE_FILL_EVEN_ODD = 0x1900, /*! A pixel is drawn it it crosses an odd number of path pixels. */ + VG_LITE_FILL_NON_ZERO = 0x1901, /*! A pixel is drawn if it crosses at least one path pixel. */ + } vg_lite_fill_t; + + /* Global alpha modes. */ + typedef enum vg_lite_global_alpha + { + VG_LITE_NORMAL = 0, /*! Use original src/dst alpha value. */ + VG_LITE_GLOBAL, /*! Use global src/dst alpha value to replace original src/dst alpha value. */ + VG_LITE_SCALED, /*! Multiply global src/dst alpha value and original src/dst alpha value. */ + } vg_lite_global_alpha_t; + + /* Filter modes. */ + typedef enum vg_lite_filter + { + VG_LITE_FILTER_POINT = 0, /*! Fetch the nearest image pixel. */ + VG_LITE_FILTER_LINEAR = 0x1000, /*! Used for linear paint. */ + VG_LITE_FILTER_BI_LINEAR = 0x2000, /*! Use a 2x2 box around the image pixel and perform an interpolation. */ + VG_LITE_FILTER_GAUSSIAN = 0x3000, /*! Perform 3x3 gaussian blur with the convolution for image pixel. */ + } vg_lite_filter_t; + + /* Pattern padding mode. Match OpenVG enum VGTilingMode. */ + typedef enum vg_lite_pattern_mode + { + VG_LITE_PATTERN_COLOR = 0x1D00, /*! Pixel outside the bounds of sourceimage should be taken as the color */ + VG_LITE_PATTERN_PAD = 0x1D01, /*! Pixel outside the bounds of sourceimage should be taken as having the same color as the closest edge pixel */ + VG_LITE_PATTERN_REPEAT = 0x1D02, /*! Pixel outside the bounds of sourceimage should be repeated indefinitely in all directions */ + VG_LITE_PATTERN_REFLECT = 0x1D03, /*! Pixel outside the bounds of sourceimage should be reflected indefinitely in all directions */ + } vg_lite_pattern_mode_t; + + /* Paint type. Match OpenVG enum VGPaintType. */ + typedef enum vg_lite_paint_type + { + /* For enum value backward compatibility */ + VG_LITE_PAINT_ZERO = 0, + VG_LITE_PAINT_COLOR = 0x1B00, + VG_LITE_PAINT_LINEAR_GRADIENT = 0x1B01, + VG_LITE_PAINT_RADIAL_GRADIENT = 0x1B02, + VG_LITE_PAINT_PATTERN = 0x1B03, + } vg_lite_paint_type_t; + + /* Radial gradient padding mode. Match OpenVG enum VGColorRampSpreadMode */ + typedef enum + { + VG_LITE_GRADIENT_SPREAD_FILL = 0, + VG_LITE_GRADIENT_SPREAD_PAD = 0x1C00, + VG_LITE_GRADIENT_SPREAD_REPEAT = 0x1C01, + VG_LITE_GRADIENT_SPREAD_REFLECT = 0x1C02, + } vg_lite_gradient_spreadmode_t; + + /* Decnano Compress mode. */ + typedef enum vg_lite_compress_mode + { + VG_LITE_DEC_DISABLE = 0, /*! disable compress */ + VG_LITE_DEC_NON_SAMPLE, /*! compress ratio is 1.6 if use ARGB8888, compress ratio is 2 if use XRGB8888 */ + VG_LITE_DEC_HSAMPLE, /*! compress ratio is 2 if use ARGB8888, compress ratio is 2.6 if use XRGB8888 */ + VG_LITE_DEC_HV_SAMPLE, /*! compress ratio is 2.6 if use ARGB8888, compress ratio is 4 if use XRGB8888 */ + } vg_lite_compress_mode_t; + + /* Draw path type. Match OpenVG enum VGPaintMode */ + typedef enum vg_lite_path_type + { + /* For enum value backward compatibility */ + VG_LITE_DRAW_ZERO = 0, + VG_LITE_DRAW_STROKE_PATH = (1<<0), + VG_LITE_DRAW_FILL_PATH = (1<<1), + VG_LITE_DRAW_FILL_STROKE_PATH = (1<<1 | 1<<0), + } vg_lite_path_type_t; + + /* End cap style. Match OpenVG enum VGCapStyle */ + typedef enum vg_lite_cap_style + { + VG_LITE_CAP_BUTT = 0x1700, + VG_LITE_CAP_ROUND = 0x1701, + VG_LITE_CAP_SQUARE = 0x1702, + } vg_lite_cap_style_t; + + /* Line join styles. Match OpenVG enum VGJoinStyle */ + typedef enum vg_lite_join_style + { + VG_LITE_JOIN_MITER = 0x1800, + VG_LITE_JOIN_ROUND = 0x1801, + VG_LITE_JOIN_BEVEL = 0x1802, + } vg_lite_join_style_t; + + /* Mask operation mode. Match OpenVG enum VGMaskOperation */ + typedef enum vg_lite_mask_operation + { + VG_LITE_CLEAR_MASK = 0x1500, /*! Set all dest mask values to 0 */ + VG_LITE_FILL_MASK = 0x1501, /*! Set all dest mask values to 1 */ + VG_LITE_SET_MASK = 0x1502, /*! Copy from src masklayer to dest masklayer. */ + VG_LITE_UNION_MASK = 0x1503, /*! Replace dest masklayer by its union with src masklayer. */ + VG_LITE_INTERSECT_MASK = 0x1504, /*! Replace dest masklayer by its intersection with src masklayer. */ + VG_LITE_SUBTRACT_MASK = 0x1505, /*! Subtract src mask in dest masklayer */ + } vg_lite_mask_operation_t; + + /* Mirror orientation mode. */ + typedef enum vg_lite_orientation + { + VG_LITE_ORIENTATION_TOP_BOTTOM, + VG_LITE_ORIENTATION_BOTTOM_TOP, + } vg_lite_orientation_t; + + /* Gamma conversion mode. */ + typedef enum vg_lite_gamma_conversion + { + VG_LITE_GAMMA_NO_CONVERSION, /*! Leave color as is. */ + VG_LITE_GAMMA_LINEAR, /*! Convert from sRGB to linear space. */ + VG_LITE_GAMMA_NON_LINEAR /*! Convert from linear to sRGB space. */ + } vg_lite_gamma_conversion_t; + + /* Index endian */ + typedef enum vg_lite_index_endian + { + VG_LITE_INDEX_LITTLE_ENDIAN, /*! Parse the index pixel from low to high, + *! when using index1, the parsing order is bit0~bit7. + *! when using index2, the parsing order is bit0:1,bit2:3,bit4:5.bit6:7. + *! when using index4, the parsing order is bit0:3,bit4:7. + */ + VG_LITE_INDEX_BIG_ENDIAN, /*! Parse the index pixel from low to high, + *! when using index1, the parsing order is bit7~bit0. + *! when using index2, the parsing order is bit7:6,bit5:4,bit3:2.bit1:0. + *! when using index4, the parsing order is bit4:7,bit0:3. + */ + } vg_lite_index_endian_t; + + /* Map flag*/ + typedef enum vg_lite_map_flag + { + VG_LITE_MAP_USER_MEMORY = 0, + VG_LITE_MAP_DMABUF = 0x01, + } vg_lite_map_flag_t; + + /*VGLite parameters variable*/ + typedef enum vg_lite_param_type + { + VG_LITE_SCISSOR_RECT, /*! count must be 4n for x, y, right, bottom */ + VG_LITE_GPU_IDLE_STATE, /*! 0: busy, 1: idle */ + } vg_lite_param_type_t; + +/* VGLite API Structures ******************************************************************************************************************/ + + /* VGLite driver information */ + typedef struct vg_lite_info { + vg_lite_uint32_t api_version; + vg_lite_uint32_t header_version; + vg_lite_uint32_t release_version; + vg_lite_uint32_t reserved; + } vg_lite_info_t; + + /* A 2D Point definition. */ + typedef struct vg_lite_point { + vg_lite_int32_t x; + vg_lite_int32_t y; + } vg_lite_point_t; + + /* Four 2D Point that form a polygon */ + typedef vg_lite_point_t vg_lite_point4_t[4]; + + /* A rectangle.*/ + typedef struct vg_lite_rectangle { + vg_lite_int32_t x; /*! Left coordinate of rectangle. */ + vg_lite_int32_t y; /*! Top coordinate of rectangle. */ + vg_lite_int32_t width; /*! Width of rectangle. */ + vg_lite_int32_t height; /*! Height of rectangle. */ + } vg_lite_rectangle_t; + + typedef struct vg_lite_matrix { + vg_lite_float_t m[3][3]; /*! The 3x3 matrix is in [row][column] order. */ + vg_lite_float_t scaleX; + vg_lite_float_t scaleY; + vg_lite_float_t angle; + } vg_lite_matrix_t; + + typedef struct vg_lite_yuvinfo + { + vg_lite_swizzle_t swizzle; /*! UV swizzle. */ + vg_lite_yuv2rgb_t yuv2rgb; /*! 601 or 709 conversion standard. */ + vg_lite_uint32_t uv_planar; /*! UV(U) planar address. */ + vg_lite_uint32_t v_planar; /*! V planar address. */ + vg_lite_uint32_t alpha_planar; /*! Alpha planar address. */ + vg_lite_uint32_t uv_stride; /*! UV(U) stride. */ + vg_lite_uint32_t v_stride; /*! V stride. */ + vg_lite_uint32_t alpha_stride; /*! Alpha stride. */ + vg_lite_uint32_t uv_height; /*! UV(U) height. */ + vg_lite_uint32_t v_height; /*! V height. */ + vg_lite_pointer uv_memory; /*! The logical pointer to the UV(U) planar memory. */ + vg_lite_pointer v_memory; /*! The logical pointer to the V planar memory. */ + vg_lite_pointer uv_handle; /*! The memory handle of the UV(U) planar. */ + vg_lite_pointer v_handle; /*! The memory handle of the V planar. */ + } vg_lite_yuvinfo_t; + + typedef struct vg_lite_path_point* vg_lite_path_point_ptr; + typedef struct vg_lite_path_point + { + /* X coordinate. */ + vg_lite_float_t x; + + /* Y coordinate. */ + vg_lite_float_t y; + + /* Flatten flag for flattened path. */ + vg_lite_uint8_t flatten_flag; + + /* Curve type for stroke path. */ + vg_lite_uint8_t curve_type; + + /* X tangent. */ + vg_lite_float_t tangentX; + + /* Y tangent. */ + vg_lite_float_t tangentY; + + /* Length of the line. */ + vg_lite_float_t length; + + /* Pointer to next point node. */ + vg_lite_path_point_ptr next; + + /* Pointer to previous point node. */ + vg_lite_path_point_ptr prev; + + } vg_lite_path_point_t; + + typedef struct vg_lite_sub_path* vg_lite_sub_path_ptr; + typedef struct vg_lite_sub_path + { + /* Pointer to next sub path. */ + vg_lite_sub_path_ptr next; + + /* Number of points. */ + vg_lite_uint32_t point_count; + + /* Point list. */ + vg_lite_path_point_ptr point_list; + + /* Last point. */ + vg_lite_path_point_ptr end_point; + + /* Whether is path is closed. */ + vg_lite_uint8_t closed; + + /* Sub path length. */ + vg_lite_float_t length; + + } vg_lite_sub_path_t; + + /* Save divided path data according to MOVE/MOVE_REL. */ + typedef struct vg_lite_path_list* vg_lite_path_list_ptr; + typedef struct vg_lite_path_list + { + vg_lite_path_point_ptr path_points; + vg_lite_path_point_ptr path_end; + vg_lite_uint32_t point_count; + vg_lite_path_list_ptr next; + vg_lite_uint8_t closed; + + } vg_lite_path_list_t; + + typedef struct vg_lite_stroke + { + /* Stroke parameters */ + vg_lite_cap_style_t cap_style; + vg_lite_join_style_t join_style; + vg_lite_float_t line_width; + vg_lite_float_t miter_limit; + vg_lite_float_t *dash_pattern; + vg_lite_uint32_t pattern_count; + vg_lite_float_t dash_phase; + vg_lite_float_t dash_length; + vg_lite_uint32_t dash_index; + vg_lite_float_t half_width; + + /* Total length of stroke dash patterns. */ + vg_lite_float_t pattern_length; + + /* For fast checking. */ + vg_lite_float_t miter_square; + + /* Temp storage of stroke subPath. */ + vg_lite_path_point_ptr path_points; + vg_lite_path_point_ptr path_end; + vg_lite_uint32_t point_count; + vg_lite_path_point_ptr left_point; + vg_lite_path_point_ptr right_point; + vg_lite_path_point_ptr stroke_points; + vg_lite_path_point_ptr stroke_end; + vg_lite_uint32_t stroke_count; + + /* Divide stroke path according to move or move_rel for avoiding implicit closure. */ + vg_lite_path_list_ptr path_list_divide; + + /* pointer to current divided path data. */ + vg_lite_path_list_ptr cur_list; + + /* Flag that add end_path in driver. */ + vg_lite_uint8_t add_end; + vg_lite_uint8_t dash_reset; + + /* Sub path list. */ + vg_lite_sub_path_ptr stroke_paths; + + /* Last sub path. */ + vg_lite_sub_path_ptr last_stroke; + + /* Swing area handling. */ + vg_lite_uint32_t swing_handling; + vg_lite_float_t swing_deltax; + vg_lite_float_t swing_deltay; + vg_lite_path_point_ptr swing_start; + vg_lite_path_point_ptr swing_stroke; + vg_lite_float_t swing_length; + vg_lite_float_t swing_centlen; + vg_lite_uint32_t swing_count; + vg_lite_uint8_t need_swing; + vg_lite_uint8_t swing_ccw; + + vg_lite_float_t stroke_length; + vg_lite_uint32_t stroke_size; + + /* The stroke line is fat line. */ + vg_lite_uint8_t fattened; + vg_lite_uint8_t closed; + + } vg_lite_stroke_t; + + /* Fast clear buffer. */ + typedef struct vg_lite_fc_buffer + { + vg_lite_int32_t width; /*! Width of the buffer in pixels. */ + vg_lite_int32_t height; /*! height of the buffer in pixels. */ + vg_lite_int32_t stride; /*! The number of bytes to move from one line in the buffer to the next line. */ + vg_lite_pointer handle; /*! The memory handle of the buffer's memory as allocated by the VGLite kernel. */ + vg_lite_pointer memory; /*! The logical pointer to the buffer's memory for the CPU. */ + vg_lite_uint32_t address; /*! The address to the buffer's memory for the hardware. */ + vg_lite_uint32_t color; /*! The fastclear color value. */ + } vg_lite_fc_buffer_t; + + /* Structure for any image or render target. */ + typedef struct vg_lite_buffer + { + vg_lite_int32_t width; /*! Width of the buffer in pixels. */ + vg_lite_int32_t height; /*! Height of the buffer in pixels. */ + vg_lite_int32_t stride; /*! The number of bytes to move from one line in the buffer to the next line. */ + vg_lite_buffer_layout_t tiled; /*! Indicating the buffer memory layout is linear or tiled. */ + vg_lite_buffer_format_t format; /*! The pixel format of the buffer. */ + vg_lite_pointer handle; /*! The memory handle of the buffer's memory as allocated by the VGLite kernel. */ + vg_lite_pointer memory; /*! The logical pointer to the buffer's memory for the CPU. */ + vg_lite_uint32_t address; /*! The address to the buffer's memory for the hardware. */ + vg_lite_yuvinfo_t yuv; /*! The yuv format details. */ + vg_lite_image_mode_t image_mode; /*! The blit image mode. */ + vg_lite_transparency_t transparency_mode; /*! image transparency mode. */ + vg_lite_fc_buffer_t fc_buffer[3]; /*! 3 fastclear buffers,reserved YUV format. */ + vg_lite_compress_mode_t compress_mode; /*! Refer to the definition by vg_lite_compress_mode_t. */ + vg_lite_index_endian_t index_endian; /*! Refer to the definition by vg_lite_index_endian_t. */ + vg_lite_paint_type_t paintType; /*! Get paintcolor from different paint types. */ + vg_lite_uint8_t fc_enable; /*! enable im fastclear. */ + vg_lite_uint8_t scissor_layer; /*! The buffer is scissor buffer. */ + vg_lite_uint8_t premultiplied; /*! The RGB pixel values are alpha-premultiplied */ + } vg_lite_buffer_t; + + /* Memory allocation info by kernel. */ + typedef struct vg_lite_hw_memory + { + vg_lite_pointer handle; /*! gpu memory object handle. */ + vg_lite_pointer memory; /*! logical memory address. */ + vg_lite_uint32_t address; /*! GPU memory address. */ + vg_lite_uint32_t bytes; /*! Size of memory. */ + vg_lite_uint32_t property; /*! Currently bit0 is used for path upload state: + *! 1 : enable auto path data uploading. + *! 0 : disable path data uploading. path data is embedded in command buffer. */ + } vg_lite_hw_memory_t; + + /* Path info for drawing command. */ + typedef struct vg_lite_path + { + vg_lite_float_t bounding_box[4]; /*! Bounding box specified as left, top, right, and bottom. */ + vg_lite_quality_t quality; /*! Quality hint for the path. */ + vg_lite_format_t format; /*! Coordinate format. */ + vg_lite_hw_memory_t uploaded; /*! Path data that has been upload into GPU addressable memory. */ + vg_lite_uint32_t path_length; /*! Number of bytes in the path data. */ + vg_lite_pointer path; /*! Pointer to the physical description of the path. */ + vg_lite_int8_t path_changed; /*! Indicate whether path data is synced with command buffer (uploaded) or not. */ + vg_lite_int8_t pdata_internal; /*! Indicate whether path data memory is allocated by driver. */ + vg_lite_path_type_t path_type; /*! Refer to the definition by vg_lite_path_type_t. */ + vg_lite_stroke_t *stroke; /*! Pointer to a vg_lite_stroke_t structure.*/ + vg_lite_pointer stroke_path; /*! Pointer to the physical description of the stroke path. */ + vg_lite_uint32_t stroke_size; /*! Number of bytes in the stroke path data. */ + vg_lite_color_t stroke_color; /*! The stroke path fill color. */ + vg_lite_int8_t add_end; /*! Flag that add end_path in driver. */ + } vg_lite_path_t; + + /* Color ramp definition. */ + typedef struct vg_lite_color_ramp + { + vg_lite_float_t stop; /*! Value for the color stop. */ + vg_lite_float_t red; /*! Red color channel value for the color stop. */ + vg_lite_float_t green; /*! Green color channel value for the color stop. */ + vg_lite_float_t blue; /*! Blue color channel value for the color stop. */ + vg_lite_float_t alpha; /*! Alpha color channel value for the color stop. */ + } vg_lite_color_ramp_t; + + /* Linear gradient parameter */ + typedef struct vg_lite_linear_gradient_parameter + { + vg_lite_float_t X0; + vg_lite_float_t Y0; + vg_lite_float_t X1; + vg_lite_float_t Y1; + } vg_lite_linear_gradient_parameter_t; + + typedef struct vg_lite_radial_gradient_parameter + { + vg_lite_float_t cx; /*! x coordinate of the center point. */ + vg_lite_float_t cy; /*! y coordinate of the center point. */ + vg_lite_float_t r; /*! radius. */ + vg_lite_float_t fx; /*! x coordinate of the focal point. */ + vg_lite_float_t fy; /*! y coordinate of the focal point. */ + } vg_lite_radial_gradient_parameter_t; + + /* Linear gradient definition. */ + typedef struct vg_lite_linear_gradient { + vg_lite_uint32_t colors[VLC_MAX_GRADIENT_STOPS]; /*! Colors for stops. */ + vg_lite_uint32_t count; /*! Count of colors, up to 16. */ + vg_lite_uint32_t stops[VLC_MAX_GRADIENT_STOPS]; /*! Color stops, value from 0 to 255. */ + vg_lite_matrix_t matrix; /*! The matrix to transform the gradient. */ + vg_lite_buffer_t image; /*! The image for rendering as gradient pattern. */ + } vg_lite_linear_gradient_t; + + /* Extended linear gradient definition. */ + typedef struct vg_lite_ext_linear_gradient { + vg_lite_uint32_t count; /*! Count of colors, up to 256. */ + vg_lite_matrix_t matrix; /*! The matrix to transform the gradient. */ + vg_lite_buffer_t image; /*! The image for rendering as gradient pattern. */ + vg_lite_linear_gradient_parameter_t linear_grad; /*! Include center point,focal point and radius.*/ + + vg_lite_uint32_t ramp_length; /*! Color ramp for gradient paints provided to driver. */ + vg_lite_color_ramp_t color_ramp[VLC_MAX_COLOR_RAMP_STOPS]; + + vg_lite_uint32_t converted_length; /*! Converted internal color ramp. */ + vg_lite_color_ramp_t converted_ramp[VLC_MAX_COLOR_RAMP_STOPS + 2]; + + vg_lite_uint8_t pre_multiplied; /*! If color values of color_ramp[] are multiply by alpha value of color_ramp[]. */ + vg_lite_gradient_spreadmode_t spread_mode; /*! The spread mode that applied to the pixels out of the image after transformed. */ + } vg_lite_ext_linear_gradient_t; + + /* Radial gradient definition. */ + typedef struct vg_lite_radial_gradient + { + vg_lite_uint32_t count; /*! Count of colors, up to 256. */ + vg_lite_matrix_t matrix; /*! The matrix to transform the gradient. */ + vg_lite_buffer_t image; /*! The image for rendering as gradient pattern. */ + vg_lite_radial_gradient_parameter_t radial_grad; /*! Include center point,focal point and radius.*/ + + vg_lite_uint32_t ramp_length; /*! Color ramp for gradient paints provided to the driver. */ + vg_lite_color_ramp_t color_ramp[VLC_MAX_COLOR_RAMP_STOPS]; + + vg_lite_uint32_t converted_length; /*! Converted internal color ramp. */ + vg_lite_color_ramp_t converted_ramp[VLC_MAX_COLOR_RAMP_STOPS + 2]; + + vg_lite_uint8_t pre_multiplied; /*! If color values of color_ramp[] are multiply by alpha value of color_ramp[]. */ + vg_lite_gradient_spreadmode_t spread_mode; /*! The spread mode that applied to the pixels out of the image after transformed. */ + } vg_lite_radial_gradient_t; + + /* Colorkey definition */ + typedef struct vg_lite_color_key + { + vg_lite_uint8_t enable; /*! The color key is effective only when "enable" is true, */ + vg_lite_uint8_t low_r; /*! The R channel of low_rgb. */ + vg_lite_uint8_t low_g; /*! The G channel of low_rgb. */ + vg_lite_uint8_t low_b; /*! The B channel of low_rgb. */ + vg_lite_uint8_t alpha; /*! The alpha channel to replace destination pixel alpha channel.*/ + vg_lite_uint8_t hign_r; /*! The R channel of hign_rgb. */ + vg_lite_uint8_t hign_g; /*! The G channel of hign_rgb. */ + vg_lite_uint8_t hign_b; /*! The B channel of hign_rgb. */ + } vg_lite_color_key_t; + + /* Four colorkey definition. + * rgb_hi_0, rgb_lo_0, alpha_0, enable_0; + * rgb_hi_1, rgb_lo_1, alpha_1, enable_1; + * rgb_hi_2, rgb_lo_2, alpha_2, enable_2; + * rgb_hi_3, rgb_lo_3, alpha_3, enable_3; + * Priority order: color_key_0 > color_key_1 > color_key_2 > color_key_3. + */ + typedef vg_lite_color_key_t vg_lite_color_key4_t[4]; + + /* Pixel matrix values */ + typedef vg_lite_float_t vg_lite_pixel_matrix_t[20]; + + /* HW pixel channel enable flags */ + typedef struct vg_lite_pixel_channel_enable + { + vg_lite_uint8_t enable_a; /*! Enable A channel.*/ + vg_lite_uint8_t enable_b; /*! Enable B channel. */ + vg_lite_uint8_t enable_g; /*! Enable G channel. */ + vg_lite_uint8_t enable_r; /*! Enable R channel. */ + } vg_lite_pixel_channel_enable_t; + + /* Pixel color transform */ + typedef struct vg_lite_color_transform + { + vg_lite_float_t a_scale; + vg_lite_float_t a_bias; + vg_lite_float_t r_scale; + vg_lite_float_t r_bias; + vg_lite_float_t g_scale; + vg_lite_float_t g_bias; + vg_lite_float_t b_scale; + vg_lite_float_t b_bias; + } vg_lite_color_transform_t; + +/* VGLite API Functions *******************************************************************************************************************/ + + /* Initialize a vglite context. */ + vg_lite_error_t vg_lite_init(vg_lite_int32_t tess_width, vg_lite_int32_t tess_height); + + /* Destroy a vglite context. */ + vg_lite_error_t vg_lite_close(void); + + /* Get the VGLite driver information. */ + vg_lite_error_t vg_lite_get_info(vg_lite_info_t* info); + + /* Get the GPU chip information. */ + vg_lite_uint32_t vg_lite_get_product_info(vg_lite_char *name, vg_lite_uint32_t *chip_id, vg_lite_uint32_t *chip_rev); + + /* Query if a specific feature is supported. */ + vg_lite_uint32_t vg_lite_query_feature(vg_lite_feature_t feature); + + /* Flush command buffer and wait for GPU to complete. */ + vg_lite_error_t vg_lite_finish(void); + + /* Flush the command buffer without waiting for GPU to complete. */ + vg_lite_error_t vg_lite_flush(void); + + /* Get the value of register from register's address. */ + vg_lite_error_t vg_lite_get_register(vg_lite_uint32_t address, vg_lite_uint32_t* result); + + /* Generate a 3x3 homogenous matrix to transform 4 source coordinates to 4 target coordinates. */ + vg_lite_error_t vg_lite_get_transform_matrix(vg_lite_point4_t src, vg_lite_point4_t dst, vg_lite_matrix_t *mat); + + /* Allocate a buffer from GPU hardware accessible memory. */ + vg_lite_error_t vg_lite_allocate(vg_lite_buffer_t *buffer); + + /* Free a buffer allocated by vg_lite_allocate() */ + vg_lite_error_t vg_lite_free(vg_lite_buffer_t *buffer); + + /* Upload RGB or YUV pixel data to an allocated buffer. */ + vg_lite_error_t vg_lite_upload_buffer(vg_lite_buffer_t *buffer, vg_lite_uint8_t *data[3], vg_lite_uint32_t stride[3]); + + /* Map a buffer into hardware accessible address space. */ + vg_lite_error_t vg_lite_map(vg_lite_buffer_t *buffer, vg_lite_map_flag_t flag, int32_t fd); + + /* Unmap a buffer that is mapped */ + vg_lite_error_t vg_lite_unmap(vg_lite_buffer_t *buffer); + + /* flush cache */ + vg_lite_error_t vg_lite_flush_mapped_buffer(vg_lite_buffer_t * buffer); + + /* Fill a buffer rectangle area with a specified color. */ + vg_lite_error_t vg_lite_clear(vg_lite_buffer_t *target, vg_lite_rectangle_t *rect, vg_lite_color_t color); + + /* Copy a source image to target buffer with transformation, blending, color mixing, and filtering. */ + vg_lite_error_t vg_lite_blit(vg_lite_buffer_t *target, + vg_lite_buffer_t *source, + vg_lite_matrix_t *matrix, + vg_lite_blend_t blend, + vg_lite_color_t color, + vg_lite_filter_t filter); + + /* Copy a rectangle area of source image to target buffer with transformation, blending, color mixing, and filtering. */ + vg_lite_error_t vg_lite_blit_rect(vg_lite_buffer_t *target, + vg_lite_buffer_t *source, + vg_lite_rectangle_t *rect, + vg_lite_matrix_t *matrix, + vg_lite_blend_t blend, + vg_lite_color_t color, + vg_lite_filter_t filter); + + /* Copy two source images to the target buffer with transformation, blending, and filtering. */ + vg_lite_error_t vg_lite_blit2(vg_lite_buffer_t *target, + vg_lite_buffer_t *source0, + vg_lite_buffer_t *source1, + vg_lite_matrix_t *matrix0, + vg_lite_matrix_t *matrix1, + vg_lite_blend_t blend, + vg_lite_filter_t filter); + + /* Copy a rectangle area of source image to target buffer without transformation, blending, color mixing, and filtering. */ + vg_lite_error_t vg_lite_copy_image(vg_lite_buffer_t *target, + vg_lite_buffer_t *source, + vg_lite_int32_t sx, + vg_lite_int32_t sy, + vg_lite_int32_t dx, + vg_lite_int32_t dy, + vg_lite_int32_t width, + vg_lite_int32_t height); + + /* Draw a path to a target buffer with transformation, color, and blending */ + vg_lite_error_t vg_lite_draw(vg_lite_buffer_t *target, + vg_lite_path_t *path, + vg_lite_fill_t fill_rule, + vg_lite_matrix_t *matrix, + vg_lite_blend_t blend, + vg_lite_color_t color); + + /* Set stroke path attributes. */ + vg_lite_error_t vg_lite_set_stroke(vg_lite_path_t *path, + vg_lite_cap_style_t cap_style, + vg_lite_join_style_t join_style, + vg_lite_float_t line_width, + vg_lite_float_t miter_limit, + vg_lite_float_t *dash_pattern, + vg_lite_uint32_t pattern_count, + vg_lite_float_t dash_phase, + vg_lite_color_t color); + + /* Update stroke path. */ + vg_lite_error_t vg_lite_update_stroke(vg_lite_path_t *path); + + /* Set path type. */ + vg_lite_error_t vg_lite_set_path_type(vg_lite_path_t *path, vg_lite_path_type_t path_type); + + /* Clears all attributes of a path. */ + vg_lite_error_t vg_lite_clear_path(vg_lite_path_t *path); + + /* Upload a path to GPU memory so GPU can access it directly. */ + vg_lite_error_t vg_lite_upload_path(vg_lite_path_t *path); + + /* Initialize a path object with attributes. */ + vg_lite_error_t vg_lite_init_path(vg_lite_path_t *path, + vg_lite_format_t format, + vg_lite_quality_t quality, + vg_lite_uint32_t length, + vg_lite_pointer data, + vg_lite_float_t min_x, + vg_lite_float_t min_y, + vg_lite_float_t max_x, + vg_lite_float_t max_y); + + /* Initializes a arc path with attributes. */ + vg_lite_error_t vg_lite_init_arc_path(vg_lite_path_t *path, + vg_lite_format_t format, + vg_lite_quality_t quality, + vg_lite_uint32_t length, + vg_lite_pointer data, + vg_lite_float_t min_x, + vg_lite_float_t min_y, + vg_lite_float_t max_x, + vg_lite_float_t max_y); + + /* Return the size (in bytes) of command buffer for a path opcode array. */ + vg_lite_uint32_t vg_lite_get_path_length(vg_lite_uint8_t *opcode, + vg_lite_uint32_t count, + vg_lite_format_t format); + + /* Generate command buffer for the (path) based on input opcodes (opcode) and coordinates (data). */ + vg_lite_error_t vg_lite_append_path(vg_lite_path_t *path, + vg_lite_uint8_t *opcode, + vg_lite_pointer data, + vg_lite_uint32_t seg_count); + + /* Set CLUT (Color Look Up Table) for index image. The (colors) is in ARGB format. */ + vg_lite_error_t vg_lite_set_CLUT(vg_lite_uint32_t count, vg_lite_uint32_t *colors); + + /* Draw a path that is filled by a transformed image pattern. */ + vg_lite_error_t vg_lite_draw_pattern(vg_lite_buffer_t *target, + vg_lite_path_t *path, + vg_lite_fill_t fill_rule, + vg_lite_matrix_t *path_matrix, + vg_lite_buffer_t *pattern_image, + vg_lite_matrix_t *pattern_matrix, + vg_lite_blend_t blend, + vg_lite_pattern_mode_t pattern_mode, + vg_lite_color_t pattern_color, + vg_lite_color_t color, + vg_lite_filter_t filter); + + /* Initialize a linear gradient object with default attributes. */ + vg_lite_error_t vg_lite_init_grad(vg_lite_linear_gradient_t *grad); + + /* Reset a linear gradient object attributes. */ + vg_lite_error_t vg_lite_clear_grad(vg_lite_linear_gradient_t *grad); + + /* Update a linear gradient object. */ + vg_lite_error_t vg_lite_update_grad(vg_lite_linear_gradient_t *grad); + + /* Return pointer to a linear gradient object's matrix. */ + vg_lite_matrix_t* vg_lite_get_grad_matrix(vg_lite_linear_gradient_t *grad); + + /* Set attributes for a linear gradient object. */ + vg_lite_error_t vg_lite_set_grad(vg_lite_linear_gradient_t *grad, + vg_lite_uint32_t count, + vg_lite_uint32_t *colors, + vg_lite_uint32_t *stops); + + /* Draw a path with a linear gradient object pattern. */ + vg_lite_error_t vg_lite_draw_grad(vg_lite_buffer_t *target, + vg_lite_path_t *path, + vg_lite_fill_t fill_rule, + vg_lite_matrix_t *matrix, + vg_lite_linear_gradient_t *grad, + vg_lite_blend_t blend); + + /* Reset an extended linear gradient object attributes and free image buffer. */ + vg_lite_error_t vg_lite_clear_linear_grad(vg_lite_ext_linear_gradient_t *grad); + + /* Update an extended linear gradient object. */ + vg_lite_error_t vg_lite_update_linear_grad(vg_lite_ext_linear_gradient_t *grad); + + /* Return pointer to an extended linear gradient object's matrix. */ + vg_lite_matrix_t* vg_lite_get_linear_grad_matrix(vg_lite_ext_linear_gradient_t *grad); + + /* Set attributes for an extended linear gradient object. */ + vg_lite_error_t vg_lite_set_linear_grad(vg_lite_ext_linear_gradient_t *grad, + vg_lite_uint32_t count, + vg_lite_color_ramp_t *color_ramp, + vg_lite_linear_gradient_parameter_t grad_param, + vg_lite_gradient_spreadmode_t spread_mode, + vg_lite_uint8_t pre_mult); + + /* Draw a path with an extended linear gradient object. */ + vg_lite_error_t vg_lite_draw_linear_grad(vg_lite_buffer_t *target, + vg_lite_path_t *path, + vg_lite_fill_t fill_rule, + vg_lite_matrix_t *path_matrix, + vg_lite_ext_linear_gradient_t *grad, + vg_lite_color_t paint_color, + vg_lite_blend_t blend, + vg_lite_filter_t filter); + + /* Reset a radial gradient object attributes and free image buffer. */ + vg_lite_error_t vg_lite_clear_radial_grad(vg_lite_radial_gradient_t *grad); + + /* Update a radial gradient object. */ + vg_lite_error_t vg_lite_update_radial_grad(vg_lite_radial_gradient_t *grad); + + /* Return pointer to a radial gradient object's matrix. */ + vg_lite_matrix_t* vg_lite_get_radial_grad_matrix(vg_lite_radial_gradient_t *grad); + + /* Set attributes for a radial gradient object. */ + vg_lite_error_t vg_lite_set_radial_grad(vg_lite_radial_gradient_t *grad, + vg_lite_uint32_t count, + vg_lite_color_ramp_t *color_ramp, + vg_lite_radial_gradient_parameter_t grad_param, + vg_lite_gradient_spreadmode_t spread_mode, + vg_lite_uint8_t pre_mult); + + /* Draw a path with a radial gradient object pattern. */ + vg_lite_error_t vg_lite_draw_radial_grad(vg_lite_buffer_t *target, + vg_lite_path_t *path, + vg_lite_fill_t fill_rule, + vg_lite_matrix_t *path_matrix, + vg_lite_radial_gradient_t *grad, + vg_lite_color_t paint_color, + vg_lite_blend_t blend, + vg_lite_filter_t filter); + + /* Load an identity matrix. */ + vg_lite_error_t vg_lite_identity(vg_lite_matrix_t *matrix); + + /* Translate a matrix. */ + vg_lite_error_t vg_lite_translate(vg_lite_float_t x, vg_lite_float_t y, vg_lite_matrix_t *matrix); + + /* Scale a matrix. */ + vg_lite_error_t vg_lite_scale(vg_lite_float_t scale_x, vg_lite_float_t scale_y, vg_lite_matrix_t *matrix); + + /* Rotate a matrix. */ + vg_lite_error_t vg_lite_rotate(vg_lite_float_t degrees, vg_lite_matrix_t *matrix); + + /* Set and enable a scissor rectangle for render target. */ + vg_lite_error_t vg_lite_set_scissor(vg_lite_int32_t x, vg_lite_int32_t y, vg_lite_int32_t right, vg_lite_int32_t bottom); + + /* Set scissor rectangles on mask layer. Scissor rects are enabled/disabled by following APIs. */ + vg_lite_error_t vg_lite_scissor_rects(vg_lite_uint32_t nums, vg_lite_rectangle_t rect[]); + + /* Enable scissor rects defined on mask layer. */ + vg_lite_error_t vg_lite_enable_scissor(void); + + /* Disable scissor rects defined on mask layer. */ + vg_lite_error_t vg_lite_disable_scissor(void); + + /* Query size of available contiguous video memory. */ + vg_lite_error_t vg_lite_get_mem_size(vg_lite_uint32_t *size); + + /* Set global alpha value for source image */ + vg_lite_error_t vg_lite_source_global_alpha(vg_lite_global_alpha_t alpha_mode, vg_lite_uint8_t alpha_value); + + /* Set global alpha value for destination image. */ + vg_lite_error_t vg_lite_dest_global_alpha(vg_lite_global_alpha_t alpha_mode, vg_lite_uint8_t alpha_value); + + /* Set colorkey. */ + vg_lite_error_t vg_lite_set_color_key(vg_lite_color_key4_t colorkey); + + /* Enable dither function. Dither is OFF by default. */ + vg_lite_error_t vg_lite_enable_dither(void); + + /* Disable dither function. Dither is OFF by default. */ + vg_lite_error_t vg_lite_disable_dither(void); + + /* Set a 64-byte aligned memory buffer (physical) as VGLite tessellation buffer. */ + vg_lite_error_t vg_lite_set_tess_buffer(vg_lite_uint32_t physical, vg_lite_uint32_t size); + + /* Can be called before vg_lite_init() to overwrite the default VG_LITE_COMMAND_BUFFER_SIZE */ + vg_lite_error_t vg_lite_set_command_buffer_size(vg_lite_uint32_t size); + + /* Set a user-defined external memory buffer (physical, 64-byte aligned) as VGLite command buffer. */ + vg_lite_error_t vg_lite_set_command_buffer(vg_lite_uint32_t physical, vg_lite_uint32_t size); + + /* Setup a pixel transform matrix m[20] which transforms each pixel as following: + * + * |a'| |m0 m1 m2 m3 m4 | |a| + * |r'| |m5 m6 m7 m8 m9 | |r| + * |g'| = |m10 m11 m12 m13 m14|.|g| + * |b'| |m15 m16 m17 m18 m19| |b| + * |1 | |0 0 0 0 1 | |1| + * + * The pixel transform for A, R, G, B channel can be enabled/disabled individually with (channel) parameter. + */ + vg_lite_error_t vg_lite_set_pixel_matrix(vg_lite_pixel_matrix_t matrix, vg_lite_pixel_channel_enable_t *channel); + + /* Setup 3x3 gaussian blur weight values to filter image pixels. + * + * Parameters w0, w1, w2 define a 3x3 gaussian blur weight matrix as below + * + * | w2 w1 w2 | + * | w1 w0 w1 | + * | w2 w1 w2 | + * + * The sum of 9 kernel weights must be 1.0 to avoid convolution overflow ( w0 + 4*w1 + 4*w2 = 1.0 ). + * The 3x3 weight matrix applies to a 3x3 pixel block + * + * | pixel[i-1][j-1] pixel[i][j-1] pixel[i+1][j-1]| + * | pixel[i-1][j] pixel[i][j] pixel[i+1][j] | + * | pixel[i-1][j+1] pixel[i][j+1] pixel[i+1][j+1]| + * + * With the following dot product equation: + * + * color[i][j] = w2*pixel[i-1][j-1] + w1*pixel[i][j-1] + w2*pixel[i+1][j-1] + * + w1*pixel[i-1][j] + w0*pixel[i][j] + w1*pixel[i+1][j] + * + w2*pixel[i-1][j+1] + w1*pixel[i][j+1] + w2*pixel[i+1][j+1]; + */ + vg_lite_error_t vg_lite_gaussian_filter(vg_lite_float_t w0, vg_lite_float_t w1, vg_lite_float_t w2); + + /* Enable masklayer function. Masklayer is OFF by default. */ + vg_lite_error_t vg_lite_enable_masklayer(void); + + /* Disable masklayer function. Masklayer is OFF by default. */ + vg_lite_error_t vg_lite_disable_masklayer(void); + + /* Setup a masklayer. */ + vg_lite_error_t vg_lite_set_masklayer(vg_lite_buffer_t *masklayer); + + /* Free a masklayer and disable mask operation. */ + vg_lite_error_t vg_lite_destroy_masklayer(vg_lite_buffer_t *masklayer); + + /* Create a masklayer with default format A8 and default pixel value 255. */ + vg_lite_error_t vg_lite_create_masklayer(vg_lite_buffer_t *masklayer, + vg_lite_uint32_t width, + vg_lite_uint32_t height); + + /* Set pixel values for a rectangle area in a masklayer */ + vg_lite_error_t vg_lite_fill_masklayer(vg_lite_buffer_t *masklayer, + vg_lite_rectangle_t *rect, + vg_lite_uint8_t value); + + /* Blend a rectangle area of src masklayer with dst masklayer according to (operation). */ + vg_lite_error_t vg_lite_blend_masklayer(vg_lite_buffer_t *dst, + vg_lite_buffer_t *src, + vg_lite_mask_operation_t operation, + vg_lite_rectangle_t *rect); + + /* Render a (path) with (fill_rule), (color), (matrix) to the masklayer. */ + vg_lite_error_t vg_lite_render_masklayer(vg_lite_buffer_t *masklayer, + vg_lite_mask_operation_t operation, + vg_lite_path_t *path, + vg_lite_fill_t fill_rule, + vg_lite_color_t color, + vg_lite_matrix_t *matrix); + + /* Set mirror orientation. */ + vg_lite_error_t vg_lite_set_mirror(vg_lite_orientation_t orientation); + + /* Set gamma value. */ + vg_lite_error_t vg_lite_set_gamma(vg_lite_gamma_conversion_t gamma_value); + + /* Enable color transformation, which is OFF by default. */ + vg_lite_error_t vg_lite_enable_color_transform(void); + + /* Disable color transformation, which is OFF by default. */ + vg_lite_error_t vg_lite_disable_color_transform(void); + + /* Set pixel color transformation scale and bias values for each pixel channel. */ + vg_lite_error_t vg_lite_set_color_transform(vg_lite_color_transform_t *values); + + /* Set flexa stream id. */ + vg_lite_error_t vg_lite_flexa_set_stream(vg_lite_uint8_t stream_id); + + /* set flexa background buffer.*/ + vg_lite_error_t vg_lite_flexa_bg_buffer(vg_lite_uint8_t stream_id, + vg_lite_buffer_t *buffer, + vg_lite_uint32_t seg_count, + vg_lite_uint32_t seg_size); + + /* Enable flexa. */ + vg_lite_error_t vg_lite_flexa_enable(void); + + /* Disable flexa.*/ + vg_lite_error_t vg_lite_flexa_disable(void); + + /* Set flexa stop flag after the last frame. */ + vg_lite_error_t vg_lite_flexa_stop_frame(void); + + /* Dump command buffer */ + vg_lite_error_t vg_lite_dump_command_buffer(void); + + /* Return VGLite parameters in params[] array */ + vg_lite_error_t vg_lite_get_parameter(vg_lite_param_type_t type, + vg_lite_int32_t count, + vg_lite_float_t* params); + +#endif /* VGLITE_VERSION_3_0 */ + +#ifdef __cplusplus +} +#endif +#endif /* _vg_lite_h_ */ diff --git a/include/liblvgl/others/vg_lite_tvg/vg_lite_tvg.cpp b/include/liblvgl/others/vg_lite_tvg/vg_lite_tvg.cpp new file mode 100644 index 00000000..47d1930c --- /dev/null +++ b/include/liblvgl/others/vg_lite_tvg/vg_lite_tvg.cpp @@ -0,0 +1,2854 @@ +/** + * @file vg_lite_tvg.cpp + * + */ + +/********************* + * INCLUDES + *********************/ +#include "../../lv_conf_internal.h" +#if LV_USE_DRAW_VG_LITE && LV_USE_VG_LITE_THORVG + +#include "vg_lite.h" +#include "../../lvgl.h" +#include "../../libs/thorvg/thorvg.h" +#include +#include +#include +#include +#include +#include + +#if LV_VG_LITE_THORVG_YUV_SUPPORT + #include +#endif + +/********************* + * DEFINES + *********************/ + +#define TVG_CANVAS_ENGINE CanvasEngine::Sw +#define TVG_COLOR(COLOR) B(COLOR), G(COLOR), R(COLOR), A(COLOR) +#define TVG_IS_VG_FMT_SUPPORT(fmt) ((fmt) == VG_LITE_BGRA8888 || (fmt) == VG_LITE_BGRX8888) + +#define TVG_CHECK_RETURN_VG_ERROR(FUNC) \ + do { \ + Result res = FUNC; \ + if (res != Result::Success) { \ + LV_LOG_ERROR("Executed '" #FUNC "' error: %d", (int)res); \ + return vg_lite_error_conv(res); \ + } \ + } while (0) +#define TVG_CHECK_RETURN_RESULT(FUNC) \ + do { \ + Result res = FUNC; \ + if (res != Result::Success) { \ + LV_LOG_ERROR("Executed '" #FUNC "' error: %d", (int)res);\ + return res; \ + } \ + } while (0) + +/* clang-format off */ + +#define IS_INDEX_FMT(fmt) \ + ((fmt) == VG_LITE_INDEX_1 \ + || (fmt) == VG_LITE_INDEX_2 \ + || (fmt) == VG_LITE_INDEX_4 \ + || (fmt) == VG_LITE_INDEX_8) + +#define VLC_GET_ARG(CUR, INDEX) vlc_get_arg((cur + (INDEX) * fmt_len), path->format); +#define VLC_GET_OP_CODE(ptr) (*((uint8_t*)ptr)) +#define VLC_OP_ARG_LEN(OP, LEN) \ + case VLC_OP_##OP: \ + return (LEN) + +#define A(color) ((color) >> 24) +#define R(color) (((color) & 0x00ff0000) >> 16) +#define G(color) (((color) & 0x0000ff00) >> 8) +#define B(color) ((color) & 0xff) +#define ARGB(a, r, g, b) ((a) << 24) | ((r) << 16) | ((g) << 8) | (b) +#define MIN(a, b) (a) > (b) ? (b) : (a) +#define MAX(a, b) (a) > (b) ? (a) : (b) +#define UDIV255(x) (((x) * 0x8081U) >> 0x17) +#define LERP(v1, v2, w) ((v1) * (w) + (v2) * (1.0f - (w))) +#define CLAMP(x, min, max) (((x) < (min)) ? (min) : ((x) > (max)) ? (max) : (x)) +#define COLOR_FROM_RAMP(ColorRamp) (((vg_lite_float_t*)ColorRamp) + 1) + +#define VG_LITE_RETURN_ERROR(func) \ + if ((error = func) != VG_LITE_SUCCESS) \ + return error + +#define VG_LITE_ALIGN(number, align_bytes) \ + (((number) + ((align_bytes)-1)) & ~((align_bytes)-1)) + +#define VG_LITE_IS_ALIGNED(num, align) (((uintptr_t)(num) & ((align)-1)) == 0) + +#define VG_LITE_IS_ALPHA_FORMAT(format) \ + ((format) == VG_LITE_A8 || (format) == VG_LITE_A4) + +/* clang-format on */ + +/********************** + * TYPEDEFS + **********************/ + +using namespace tvg; + +#pragma pack(1) +typedef struct { + uint8_t blue; + uint8_t green; + uint8_t red; +} vg_color24_t; + +typedef struct { + uint16_t blue : 5; + uint16_t green : 6; + uint16_t red : 5; +} vg_color16_t; + +typedef struct { + vg_color16_t c; + uint8_t alpha; +} vg_color16_alpha_t; + +typedef struct { + uint8_t blue; + uint8_t green; + uint8_t red; + uint8_t alpha; +} vg_color32_t; + +typedef struct { + vg_lite_float_t x; + vg_lite_float_t y; +} vg_lite_fpoint_t; + +#pragma pack() + +class vg_lite_ctx +{ + public: + std::unique_ptr canvas; + void * target_buffer; + void * tvg_target_buffer; + vg_lite_uint32_t target_px_size; + vg_lite_buffer_format_t target_format; + vg_lite_rectangle_t scissor_rect; + bool scissor_is_set; + + public: + vg_lite_ctx() + : target_buffer { nullptr } + , tvg_target_buffer { nullptr } + , target_px_size { 0 } + , target_format { VG_LITE_BGRA8888 } + , scissor_rect { 0, 0, 0, 0 } + , scissor_is_set { false } + , clut_2colors { 0 } + , clut_4colors { 0 } + , clut_16colors { 0 } + , clut_256colors { 0 } + { + canvas = SwCanvas::gen(); + } + + vg_lite_uint32_t * get_image_buffer(vg_lite_uint32_t w, vg_lite_uint32_t h) + { + src_buffer.resize(w * h); + return src_buffer.data(); + } + + vg_lite_uint32_t * get_temp_target_buffer(vg_lite_uint32_t w, vg_lite_uint32_t h) + { + vg_lite_uint32_t px_size = w * h; + if(px_size > dest_buffer.size()) { + dest_buffer.resize(w * h); + } + return dest_buffer.data(); + } + + vg_lite_uint32_t * get_temp_target_buffer() + { + return dest_buffer.data(); + } + + void set_CLUT(vg_lite_uint32_t count, const vg_lite_uint32_t * colors) + { + switch(count) { + case 2: + memcpy(clut_2colors, colors, sizeof(clut_2colors)); + break; + case 4: + memcpy(clut_4colors, colors, sizeof(clut_4colors)); + break; + case 16: + memcpy(clut_16colors, colors, sizeof(clut_16colors)); + break; + case 256: + memcpy(clut_256colors, colors, sizeof(clut_256colors)); + break; + default: + LV_ASSERT(false); + break; + } + } + + const vg_lite_uint32_t * get_CLUT(vg_lite_buffer_format_t format) + { + switch(format) { + case VG_LITE_INDEX_1: + return clut_2colors; + + case VG_LITE_INDEX_2: + return clut_2colors; + + case VG_LITE_INDEX_4: + return clut_4colors; + + case VG_LITE_INDEX_8: + return clut_256colors; + + default: + break; + } + + LV_ASSERT(false); + return nullptr; + } + + static vg_lite_ctx * get_instance() + { + static vg_lite_ctx instance; + return &instance; + } + + private: + /* */ + std::vector src_buffer; + std::vector dest_buffer; + + vg_lite_uint32_t clut_2colors[2]; + vg_lite_uint32_t clut_4colors[4]; + vg_lite_uint32_t clut_16colors[16]; + vg_lite_uint32_t clut_256colors[256]; +}; + +template +class vg_lite_converter +{ + public: + typedef void (*converter_cb_t)(DEST_TYPE * dest, const SRC_TYPE * src, vg_lite_uint32_t px_size, + vg_lite_uint32_t color); + + public: + vg_lite_converter(converter_cb_t converter) + : _converter_cb(converter) + { + } + + void convert(vg_lite_buffer_t * dest_buf, const vg_lite_buffer_t * src_buf, vg_lite_uint32_t color = 0) + { + LV_ASSERT(_converter_cb); + uint8_t * dest = (uint8_t *)dest_buf->memory; + const uint8_t * src = (const uint8_t *)src_buf->memory; + vg_lite_uint32_t h = src_buf->height; + + while(h--) { + _converter_cb((DEST_TYPE *)dest, (const SRC_TYPE *)src, src_buf->width, color); + dest += dest_buf->stride; + src += src_buf->stride; + } + } + + private: + converter_cb_t _converter_cb; +}; + +typedef vg_lite_float_t FLOATVECTOR4[4]; + +/********************** + * STATIC PROTOTYPES + **********************/ + +static vg_lite_error_t vg_lite_error_conv(Result result); +static Matrix matrix_conv(const vg_lite_matrix_t * matrix); +static FillRule fill_rule_conv(vg_lite_fill_t fill); +static BlendMethod blend_method_conv(vg_lite_blend_t blend); +static StrokeCap stroke_cap_conv(vg_lite_cap_style_t cap); +static StrokeJoin stroke_join_conv(vg_lite_join_style_t join); +static FillSpread fill_spread_conv(vg_lite_gradient_spreadmode_t spread); +static Result shape_append_path(std::unique_ptr & shape, vg_lite_path_t * path, vg_lite_matrix_t * matrix); +static Result shape_append_rect(std::unique_ptr & shape, const vg_lite_buffer_t * target, + const vg_lite_rectangle_t * rect); +static Result canvas_set_target(vg_lite_ctx * ctx, vg_lite_buffer_t * target); +static Result picture_load(vg_lite_ctx * ctx, std::unique_ptr & picture, const vg_lite_buffer_t * source, + vg_lite_color_t color = 0); + +static inline bool math_zero(float a) +{ + return (fabs(a) < FLT_EPSILON); +} + +static inline bool math_equal(float a, float b) +{ + return math_zero(a - b); +} + +static void ClampColor(FLOATVECTOR4 Source, FLOATVECTOR4 Target, uint8_t Premultiplied); +static uint8_t PackColorComponent(vg_lite_float_t value); +static void get_format_bytes(vg_lite_buffer_format_t format, + vg_lite_uint32_t * mul, + vg_lite_uint32_t * div, + vg_lite_uint32_t * bytes_align); + +static vg_lite_fpoint_t matrix_transform_point(const vg_lite_matrix_t * matrix, const vg_lite_fpoint_t * point); +static bool vg_lite_matrix_inverse(vg_lite_matrix_t * result, const vg_lite_matrix_t * matrix); +static void vg_lite_matrix_multiply(vg_lite_matrix_t * matrix, const vg_lite_matrix_t * mult); + +/********************** + * STATIC VARIABLES + **********************/ + +/* color converters */ + +static vg_lite_converter conv_bgra8888_to_bgr565( + [](vg_color16_t * dest, const vg_color32_t * src, vg_lite_uint32_t px_size, vg_lite_uint32_t /* color */) +{ + while(px_size--) { + dest->red = src->red * 0x1F / 0xFF; + dest->green = src->green * 0x3F / 0xFF; + dest->blue = src->blue * 0x1F / 0xFF; + src++; + dest++; + } +}); + +static vg_lite_converter conv_bgra8888_to_bgra5658( + [](vg_color16_alpha_t * dest, const vg_color32_t * src, vg_lite_uint32_t px_size, vg_lite_uint32_t /* color */) +{ + while(px_size--) { + dest->c.red = src->red * 0x1F / 0xFF; + dest->c.green = src->green * 0x3F / 0xFF; + dest->c.blue = src->blue * 0x1F / 0xFF; + dest->alpha = src->alpha; + src++; + dest++; + } +}); + +static vg_lite_converter conv_bgr565_to_bgra8888( + [](vg_color32_t * dest, const vg_color16_t * src, vg_lite_uint32_t px_size, vg_lite_uint32_t /* color */) +{ + while(px_size--) { + dest->red = src->red * 0xFF / 0x1F; + dest->green = src->green * 0xFF / 0x3F; + dest->blue = src->blue * 0xFF / 0x1F; + dest->alpha = 0xFF; + src++; + dest++; + } +}); + +static vg_lite_converter conv_bgra5658_to_bgra8888( + [](vg_color32_t * dest, const vg_color16_alpha_t * src, vg_lite_uint32_t px_size, vg_lite_uint32_t /* color */) +{ + while(px_size--) { + dest->red = src->c.red * 0xFF / 0x1F; + dest->green = src->c.green * 0xFF / 0x3F; + dest->blue = src->c.blue * 0xFF / 0x1F; + dest->alpha = src->alpha; + src++; + dest++; + } +}); + +static vg_lite_converter conv_bgrx8888_to_bgra8888( + [](vg_color32_t * dest, const vg_color32_t * src, vg_lite_uint32_t px_size, vg_lite_uint32_t /* color */) +{ + while(px_size--) { + *dest = *src; + dest->alpha = 0xFF; + dest++; + src++; + } +}); + +static vg_lite_converter conv_bgr888_to_bgra8888( + [](vg_color32_t * dest, const vg_color24_t * src, vg_lite_uint32_t px_size, vg_lite_uint32_t /* color */) +{ + while(px_size--) { + dest->red = src->red; + dest->green = src->green; + dest->blue = src->blue; + dest->alpha = 0xFF; + src++; + dest++; + } +}); + +static vg_lite_converter conv_alpha8_to_bgra8888( + [](vg_color32_t * dest, const uint8_t * src, vg_lite_uint32_t px_size, vg_lite_uint32_t color) +{ + while(px_size--) { + uint8_t alpha = *src; + dest->alpha = alpha; + dest->red = UDIV255(B(color) * alpha); + dest->green = UDIV255(G(color) * alpha); + dest->blue = UDIV255(R(color) * alpha); + dest++; + src++; + } +}); + +static vg_lite_converter conv_alpha4_to_bgra8888( + [](vg_color32_t * dest, const uint8_t * src, vg_lite_uint32_t px_size, vg_lite_uint32_t color) +{ + /* 1 byte -> 2 px */ + px_size /= 2; + + while(px_size--) { + /* high 4bit */ + uint8_t alpha = (*src & 0xF0); + dest->alpha = alpha; + dest->red = UDIV255(B(color) * alpha); + dest->green = UDIV255(G(color) * alpha); + dest->blue = UDIV255(R(color) * alpha); + dest++; + + /* low 4bit */ + alpha = (*src & 0x0F) << 4; + dest->alpha = alpha; + dest->red = UDIV255(B(color) * alpha); + dest->green = UDIV255(G(color) * alpha); + dest->blue = UDIV255(R(color) * alpha); + + dest++; + src++; + } +}); + +static vg_lite_converter conv_l8_to_bgra8888( + [](vg_color32_t * dest, const uint8_t * src, vg_lite_uint32_t px_size, vg_lite_uint32_t /* color */) +{ + while(px_size--) { + dest->alpha = 0xFF; + dest->red = *src; + dest->green = *src; + dest->blue = *src; + dest++; + src++; + } +}); + +/********************** + * MACROS + **********************/ + +/********************** + * GLOBAL FUNCTIONS + **********************/ + +extern "C" { + + void gpu_init(void) + { + vg_lite_init(0, 0); + } + + vg_lite_error_t vg_lite_allocate(vg_lite_buffer_t * buffer) + { + if(buffer->format == VG_LITE_RGBA8888_ETC2_EAC && (buffer->width % 16 || buffer->height % 4)) { + return VG_LITE_INVALID_ARGUMENT; + } + + /* Reset planar. */ + buffer->yuv.uv_planar = buffer->yuv.v_planar = buffer->yuv.alpha_planar = 0; + + /* Align height in case format is tiled. */ + if(buffer->format >= VG_LITE_YUY2 && buffer->format <= VG_LITE_NV16) { + buffer->height = VG_LITE_ALIGN(buffer->height, 4); + buffer->yuv.swizzle = VG_LITE_SWIZZLE_UV; + } + + if(buffer->format >= VG_LITE_YUY2_TILED && buffer->format <= VG_LITE_AYUY2_TILED) { + buffer->height = VG_LITE_ALIGN(buffer->height, 4); + buffer->tiled = VG_LITE_TILED; + buffer->yuv.swizzle = VG_LITE_SWIZZLE_UV; + } + + vg_lite_uint32_t mul, div, align; + get_format_bytes(buffer->format, &mul, &div, &align); + vg_lite_uint32_t stride = VG_LITE_ALIGN((buffer->width * mul / div), align); + + buffer->stride = stride; + + /* Size must be multiple of align, See: https://en.cppreference.com/w/c/memory/aligned_alloc */ + size_t size = VG_LITE_ALIGN(buffer->height * stride, LV_VG_LITE_THORVG_BUF_ADDR_ALIGN); +#ifndef _WIN32 + buffer->memory = aligned_alloc(LV_VG_LITE_THORVG_BUF_ADDR_ALIGN, size); +#else + buffer->memory = _aligned_malloc(size, LV_VG_LITE_THORVG_BUF_ADDR_ALIGN); +#endif + LV_ASSERT(buffer->memory); + buffer->address = (vg_lite_uint32_t)(uintptr_t)buffer->memory; + buffer->handle = buffer->memory; + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_free(vg_lite_buffer_t * buffer) + { + LV_ASSERT(buffer->memory); +#ifndef _WIN32 + free(buffer->memory); +#else + _aligned_free(buffer->memory); +#endif + memset(buffer, 0, sizeof(vg_lite_buffer_t)); + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_upload_buffer(vg_lite_buffer_t * buffer, vg_lite_uint8_t * data[3], vg_lite_uint32_t stride[3]) + { + LV_UNUSED(buffer); + LV_UNUSED(data); + LV_UNUSED(stride); + return VG_LITE_NOT_SUPPORT; + } + + vg_lite_error_t vg_lite_map(vg_lite_buffer_t * buffer, vg_lite_map_flag_t flag, int32_t fd) + { + LV_UNUSED(buffer); + LV_UNUSED(flag); + LV_UNUSED(fd); + return VG_LITE_NOT_SUPPORT; + } + + vg_lite_error_t vg_lite_unmap(vg_lite_buffer_t * buffer) + { + LV_UNUSED(buffer); + return VG_LITE_NOT_SUPPORT; + } + + vg_lite_error_t vg_lite_clear(vg_lite_buffer_t * target, vg_lite_rectangle_t * rectangle, vg_lite_color_t color) + { + auto ctx = vg_lite_ctx::get_instance(); + TVG_CHECK_RETURN_VG_ERROR(canvas_set_target(ctx, target)); + + auto shape = Shape::gen(); + TVG_CHECK_RETURN_VG_ERROR(shape_append_rect(shape, target, rectangle)); + TVG_CHECK_RETURN_VG_ERROR(shape->blend(BlendMethod::SrcOver)); + TVG_CHECK_RETURN_VG_ERROR(shape->fill(TVG_COLOR(color))); + TVG_CHECK_RETURN_VG_ERROR(ctx->canvas->push(std::move(shape))); + + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_blit(vg_lite_buffer_t * target, + vg_lite_buffer_t * source, + vg_lite_matrix_t * matrix, + vg_lite_blend_t blend, + vg_lite_color_t color, + vg_lite_filter_t filter) + { + LV_UNUSED(filter); + auto ctx = vg_lite_ctx::get_instance(); + canvas_set_target(ctx, target); + + auto picture = Picture::gen(); + + TVG_CHECK_RETURN_VG_ERROR(picture_load(ctx, picture, source, color)); + TVG_CHECK_RETURN_VG_ERROR(picture->transform(matrix_conv(matrix))); + TVG_CHECK_RETURN_VG_ERROR(picture->blend(blend_method_conv(blend))); + TVG_CHECK_RETURN_VG_ERROR(ctx->canvas->push(std::move(picture))); + + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_blit2(vg_lite_buffer_t * target, + vg_lite_buffer_t * source0, + vg_lite_buffer_t * source1, + vg_lite_matrix_t * matrix0, + vg_lite_matrix_t * matrix1, + vg_lite_blend_t blend, + vg_lite_filter_t filter) + { + if(!vg_lite_query_feature(gcFEATURE_BIT_VG_DOUBLE_IMAGE)) { + return VG_LITE_NOT_SUPPORT; + } + + vg_lite_error_t error; + + VG_LITE_RETURN_ERROR(vg_lite_blit(target, source0, matrix0, blend, 0, filter)); + VG_LITE_RETURN_ERROR(vg_lite_blit(target, source1, matrix1, blend, 0, filter)); + + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_blit_rect(vg_lite_buffer_t * target, + vg_lite_buffer_t * source, + vg_lite_rectangle_t * rect, + vg_lite_matrix_t * matrix, + vg_lite_blend_t blend, + vg_lite_color_t color, + vg_lite_filter_t filter) + { + LV_UNUSED(filter); + auto ctx = vg_lite_ctx::get_instance(); + TVG_CHECK_RETURN_VG_ERROR(canvas_set_target(ctx, target)); + + auto shape = Shape::gen(); + TVG_CHECK_RETURN_VG_ERROR(shape_append_rect(shape, target, rect)); + TVG_CHECK_RETURN_VG_ERROR(shape->transform(matrix_conv(matrix))); + + auto picture = tvg::Picture::gen(); + TVG_CHECK_RETURN_VG_ERROR(picture_load(ctx, picture, source, color)); + TVG_CHECK_RETURN_VG_ERROR(picture->transform(matrix_conv(matrix))); + TVG_CHECK_RETURN_VG_ERROR(picture->blend(blend_method_conv(blend))); + TVG_CHECK_RETURN_VG_ERROR(picture->composite(std::move(shape), CompositeMethod::ClipPath)); + TVG_CHECK_RETURN_VG_ERROR(ctx->canvas->push(std::move(picture))); + + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_init(int32_t tessellation_width, int32_t tessellation_height) + { + LV_UNUSED(tessellation_width); + LV_UNUSED(tessellation_height); +#if LV_VG_LITE_THORVG_THREAD_RENDER + /* Threads Count */ + auto threads = std::thread::hardware_concurrency(); + if(threads > 0) { + --threads; /* Allow the designated main thread capacity */ + } +#endif + + /* Initialize ThorVG Engine */ + TVG_CHECK_RETURN_VG_ERROR(Initializer::init(TVG_CANVAS_ENGINE, 0)); + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_close(void) + { + TVG_CHECK_RETURN_VG_ERROR(Initializer::term(TVG_CANVAS_ENGINE)); + return VG_LITE_SUCCESS; + } + + static void picture_bgra8888_to_bgr565(vg_color16_t * dest, const vg_color32_t * src, vg_lite_uint32_t px_size) + { + while(px_size--) { + dest->red = src->red * 0x1F / 0xFF; + dest->green = src->green * 0x3F / 0xFF; + dest->blue = src->blue * 0x1F / 0xFF; + src++; + dest++; + } + } + + static void picture_bgra8888_to_bgra5658(vg_color16_alpha_t * dest, const vg_color32_t * src, vg_lite_uint32_t px_size) + { + while(px_size--) { + dest->c.red = src->red * 0x1F / 0xFF; + dest->c.green = src->green * 0x3F / 0xFF; + dest->c.blue = src->blue * 0x1F / 0xFF; + dest->alpha = src->alpha; + src++; + dest++; + } + } + + static void picture_bgra8888_to_bgr888(vg_color24_t * dest, const vg_color32_t * src, vg_lite_uint32_t px_size) + { + while(px_size--) { + dest->red = src->red; + dest->green = src->green; + dest->blue = src->blue; + src++; + dest++; + } + } + + static void picture_bgra8888_to_l8(uint8_t * dest, const vg_color32_t * src, vg_lite_uint32_t px_size) + { + while(px_size--) { + *dest = (src->red * 19595 + src->green * 38469 + src->blue * 7472) >> 16; + src++; + dest++; + } + } + + static void picture_bgra8888_to_alpha8(uint8_t * dest, const vg_color32_t * src, vg_lite_uint32_t px_size) + { + while(px_size--) { + *dest = src->alpha; + src++; + dest++; + } + } + + vg_lite_error_t vg_lite_finish(void) + { + vg_lite_ctx * ctx = vg_lite_ctx::get_instance(); + + if(ctx->canvas->draw() == Result::InsufficientCondition) { + return VG_LITE_SUCCESS; + } + + TVG_CHECK_RETURN_VG_ERROR(ctx->canvas->sync()); + TVG_CHECK_RETURN_VG_ERROR(ctx->canvas->clear(true)); + + /* make sure target buffer is valid */ + LV_ASSERT_NULL(ctx->target_buffer); + + /* If target_buffer is not in a format supported by thorvg, software conversion is required. */ + switch(ctx->target_format) { + case VG_LITE_BGR565: + picture_bgra8888_to_bgr565( + (vg_color16_t *)ctx->target_buffer, + (const vg_color32_t *)ctx->get_temp_target_buffer(), + ctx->target_px_size); + break; + case VG_LITE_BGRA5658: + picture_bgra8888_to_bgra5658( + (vg_color16_alpha_t *)ctx->target_buffer, + (const vg_color32_t *)ctx->get_temp_target_buffer(), + ctx->target_px_size); + break; + case VG_LITE_BGR888: + picture_bgra8888_to_bgr888( + (vg_color24_t *)ctx->target_buffer, + (const vg_color32_t *)ctx->get_temp_target_buffer(), + ctx->target_px_size); + break; + case VG_LITE_L8: + picture_bgra8888_to_l8( + (uint8_t *)ctx->target_buffer, + (const vg_color32_t *)ctx->get_temp_target_buffer(), + ctx->target_px_size); + break; + case VG_LITE_A8: + picture_bgra8888_to_alpha8( + (uint8_t *)ctx->target_buffer, + (const vg_color32_t *)ctx->get_temp_target_buffer(), + ctx->target_px_size); + break; + case VG_LITE_BGRA8888: + case VG_LITE_BGRX8888: + /* No conversion required. */ + break; + default: + LV_LOG_ERROR("unsupported format: %d", ctx->target_format); + LV_ASSERT(false); + break; + } + + /* finish convert, clean target buffer info */ + ctx->target_buffer = nullptr; + ctx->tvg_target_buffer = nullptr; + ctx->target_px_size = 0; + + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_flush(void) + { + return vg_lite_finish(); + } + + vg_lite_error_t vg_lite_draw(vg_lite_buffer_t * target, + vg_lite_path_t * path, + vg_lite_fill_t fill_rule, + vg_lite_matrix_t * matrix, + vg_lite_blend_t blend, + vg_lite_color_t color) + { + auto ctx = vg_lite_ctx::get_instance(); + TVG_CHECK_RETURN_VG_ERROR(canvas_set_target(ctx, target)); + + auto shape = Shape::gen(); + TVG_CHECK_RETURN_VG_ERROR(shape_append_path(shape, path, matrix)); + TVG_CHECK_RETURN_VG_ERROR(shape->transform(matrix_conv(matrix))); + TVG_CHECK_RETURN_VG_ERROR(shape->fill(fill_rule_conv(fill_rule));); + TVG_CHECK_RETURN_VG_ERROR(shape->blend(blend_method_conv(blend))); + TVG_CHECK_RETURN_VG_ERROR(shape->fill(TVG_COLOR(color))); + TVG_CHECK_RETURN_VG_ERROR(ctx->canvas->push(std::move(shape))); + + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_set_stroke(vg_lite_path_t * path, + vg_lite_cap_style_t cap_style, + vg_lite_join_style_t join_style, + vg_lite_float_t line_width, + vg_lite_float_t miter_limit, + vg_lite_float_t * dash_pattern, + vg_lite_uint32_t pattern_count, + vg_lite_float_t dash_phase, + vg_lite_color_t color) + { + if(!path || line_width <= 0) { + return VG_LITE_INVALID_ARGUMENT; + } + + if(miter_limit < 1.0f) { + miter_limit = 1.0f; + } + + if(!path->stroke) { + path->stroke = (vg_lite_stroke_t *)lv_malloc_zeroed(sizeof(vg_lite_stroke_t)); + + if(!path->stroke) { + return VG_LITE_OUT_OF_RESOURCES; + } + } + + path->stroke->cap_style = cap_style; + path->stroke->join_style = join_style; + path->stroke->line_width = line_width; + path->stroke->miter_limit = miter_limit; + path->stroke->half_width = line_width / 2.0f; + path->stroke->miter_square = path->stroke->miter_limit * path->stroke->miter_limit; + path->stroke->dash_pattern = dash_pattern; + path->stroke->pattern_count = pattern_count; + path->stroke->dash_phase = dash_phase; + path->stroke_color = color; + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_update_stroke(vg_lite_path_t * path) + { + LV_UNUSED(path); + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_set_path_type(vg_lite_path_t * path, vg_lite_path_type_t path_type) + { + if(!path || + (path_type != VG_LITE_DRAW_FILL_PATH && + path_type != VG_LITE_DRAW_STROKE_PATH && + path_type != VG_LITE_DRAW_FILL_STROKE_PATH) + ) + return VG_LITE_INVALID_ARGUMENT; + + path->path_type = path_type; + + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_get_register(vg_lite_uint32_t address, vg_lite_uint32_t * result) + { + LV_UNUSED(address); + LV_UNUSED(result); + return VG_LITE_NOT_SUPPORT; + } + + vg_lite_error_t vg_lite_get_info(vg_lite_info_t * info) + { + info->api_version = VGLITE_API_VERSION_3_0; + info->header_version = VGLITE_HEADER_VERSION; + info->release_version = VGLITE_RELEASE_VERSION; + info->reserved = 0; + return VG_LITE_SUCCESS; + } + + vg_lite_uint32_t vg_lite_get_product_info(char * name, vg_lite_uint32_t * chip_id, vg_lite_uint32_t * chip_rev) + { + strcpy(name, "GCNanoLiteV"); + *chip_id = 0x265; + *chip_rev = 0x2000; + return 1; + } + + vg_lite_uint32_t vg_lite_query_feature(vg_lite_feature_t feature) + { + switch(feature) { + case gcFEATURE_BIT_VG_IM_INDEX_FORMAT: + case gcFEATURE_BIT_VG_BORDER_CULLING: + case gcFEATURE_BIT_VG_RGBA2_FORMAT: + case gcFEATURE_BIT_VG_IM_FASTCLAER: + case gcFEATURE_BIT_VG_GLOBAL_ALPHA: + case gcFEATURE_BIT_VG_COLOR_KEY: + case gcFEATURE_BIT_VG_24BIT: + case gcFEATURE_BIT_VG_DITHER: + case gcFEATURE_BIT_VG_USE_DST: + case gcFEATURE_BIT_VG_RADIAL_GRADIENT: + case gcFEATURE_BIT_VG_IM_REPEAT_REFLECT: + case gcFEATURE_BIT_VG_SCISSOR: + +#if LV_VG_LITE_THORVG_LVGL_BLEND_SUPPORT + case gcFEATURE_BIT_VG_LVGL_SUPPORT: +#endif + +#if LV_VG_LITE_THORVG_YUV_SUPPORT + case gcFEATURE_BIT_VG_YUV_INPUT: +#endif + +#if LV_VG_LITE_THORVG_LINEAR_GRADIENT_EXT_SUPPORT + case gcFEATURE_BIT_VG_LINEAR_GRADIENT_EXT: +#endif + +#if LV_VG_LITE_THORVG_16PIXELS_ALIGN + case gcFEATURE_BIT_VG_16PIXELS_ALIGN: +#endif + return 1; + default: + break; + } + return 0; + } + + vg_lite_error_t vg_lite_init_path(vg_lite_path_t * path, + vg_lite_format_t data_format, + vg_lite_quality_t quality, + vg_lite_uint32_t path_length, + void * path_data, + vg_lite_float_t min_x, vg_lite_float_t min_y, + vg_lite_float_t max_x, vg_lite_float_t max_y) + { + if(!path) { + return VG_LITE_INVALID_ARGUMENT; + } + + lv_memzero(path, sizeof(vg_lite_path_t)); + + path->format = data_format; + path->quality = quality; + path->bounding_box[0] = min_x; + path->bounding_box[1] = min_y; + path->bounding_box[2] = max_x; + path->bounding_box[3] = max_y; + + path->path_length = path_length; + path->path = path_data; + + path->path_changed = 1; + path->uploaded.address = 0; + path->uploaded.bytes = 0; + path->uploaded.handle = NULL; + path->uploaded.memory = NULL; + path->pdata_internal = 0; + + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_init_arc_path(vg_lite_path_t * path, + vg_lite_format_t data_format, + vg_lite_quality_t quality, + vg_lite_uint32_t path_length, + void * path_data, + vg_lite_float_t min_x, vg_lite_float_t min_y, + vg_lite_float_t max_x, vg_lite_float_t max_y) + { + LV_UNUSED(path); + LV_UNUSED(data_format); + LV_UNUSED(quality); + LV_UNUSED(path_length); + LV_UNUSED(path_data); + LV_UNUSED(min_x); + LV_UNUSED(min_y); + LV_UNUSED(max_x); + LV_UNUSED(max_y); + return VG_LITE_NOT_SUPPORT; + } + + vg_lite_error_t vg_lite_clear_path(vg_lite_path_t * path) + { + LV_ASSERT_NULL(path); + + if(path->stroke) { + lv_free(path->stroke); + path->stroke = NULL; + } + + return VG_LITE_SUCCESS; + } + + vg_lite_uint32_t vg_lite_get_path_length(vg_lite_uint8_t * opcode, + vg_lite_uint32_t count, + vg_lite_format_t format) + { + LV_UNUSED(opcode); + LV_UNUSED(count); + LV_UNUSED(format); + return 0; + } + + vg_lite_error_t vg_lite_append_path(vg_lite_path_t * path, + uint8_t * cmd, + void * data, + vg_lite_uint32_t seg_count) + { + LV_UNUSED(path); + LV_UNUSED(cmd); + LV_UNUSED(data); + LV_UNUSED(seg_count); + return VG_LITE_NOT_SUPPORT; + } + + vg_lite_error_t vg_lite_upload_path(vg_lite_path_t * path) + { + LV_UNUSED(path); + return VG_LITE_NOT_SUPPORT; + } + + vg_lite_error_t vg_lite_set_CLUT(vg_lite_uint32_t count, + vg_lite_uint32_t * colors) + { + if(!vg_lite_query_feature(gcFEATURE_BIT_VG_IM_INDEX_FORMAT)) { + return VG_LITE_NOT_SUPPORT; + } + LV_ASSERT(colors); + + auto ctx = vg_lite_ctx::get_instance(); + ctx->set_CLUT(count, colors); + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_draw_pattern(vg_lite_buffer_t * target, + vg_lite_path_t * path, + vg_lite_fill_t fill_rule, + vg_lite_matrix_t * path_matrix, + vg_lite_buffer_t * pattern_image, + vg_lite_matrix_t * pattern_matrix, + vg_lite_blend_t blend, + vg_lite_pattern_mode_t pattern_mode, + vg_lite_color_t pattern_color, + vg_lite_color_t color, + vg_lite_filter_t filter) + { + LV_UNUSED(pattern_mode); + LV_UNUSED(pattern_color); + LV_UNUSED(filter); + + auto ctx = vg_lite_ctx::get_instance(); + TVG_CHECK_RETURN_VG_ERROR(canvas_set_target(ctx, target)); + + auto shape = Shape::gen(); + TVG_CHECK_RETURN_VG_ERROR(shape_append_path(shape, path, path_matrix)); + TVG_CHECK_RETURN_VG_ERROR(shape->fill(fill_rule_conv(fill_rule))); + TVG_CHECK_RETURN_VG_ERROR(shape->transform(matrix_conv(path_matrix))); + + auto picture = tvg::Picture::gen(); + TVG_CHECK_RETURN_VG_ERROR(picture_load(ctx, picture, pattern_image, color)); + TVG_CHECK_RETURN_VG_ERROR(picture->transform(matrix_conv(pattern_matrix))); + TVG_CHECK_RETURN_VG_ERROR(picture->blend(blend_method_conv(blend))); + TVG_CHECK_RETURN_VG_ERROR(picture->composite(std::move(shape), CompositeMethod::ClipPath)); + TVG_CHECK_RETURN_VG_ERROR(ctx->canvas->push(std::move(picture))); + + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_init_grad(vg_lite_linear_gradient_t * grad) + { + vg_lite_error_t error = VG_LITE_SUCCESS; + + /* Set the member values according to driver defaults. */ + grad->image.width = VLC_GRADIENT_BUFFER_WIDTH; + grad->image.height = 1; + grad->image.stride = 0; + grad->image.format = VG_LITE_BGRA8888; + + /* Allocate the image for gradient. */ + error = vg_lite_allocate(&grad->image); + + grad->count = 0; + + return error; + } + + vg_lite_error_t vg_lite_set_linear_grad(vg_lite_ext_linear_gradient_t * grad, + vg_lite_uint32_t count, + vg_lite_color_ramp_t * color_ramp, + vg_lite_linear_gradient_parameter_t linear_gradient, + vg_lite_gradient_spreadmode_t spread_mode, + vg_lite_uint8_t pre_multiplied) + { + static vg_lite_color_ramp_t default_ramp[] = { + { + 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f + }, + { + 1.0f, + 1.0f, 1.0f, 1.0f, 1.0f + } + }; + + vg_lite_uint32_t i, trg_count; + vg_lite_float_t prev_stop; + vg_lite_color_ramp_t * src_ramp; + vg_lite_color_ramp_t * src_ramp_last; + vg_lite_color_ramp_t * trg_ramp; + + /* Reset the count. */ + trg_count = 0; + + if((linear_gradient.X0 == linear_gradient.X1) && (linear_gradient.Y0 == linear_gradient.Y1)) + return VG_LITE_INVALID_ARGUMENT; + + grad->linear_grad = linear_gradient; + grad->pre_multiplied = pre_multiplied; + grad->spread_mode = spread_mode; + + if(!count || count > VLC_MAX_COLOR_RAMP_STOPS || color_ramp == NULL) + goto Empty_sequence_handler; + + for(i = 0; i < count; i++) + grad->color_ramp[i] = color_ramp[i]; + grad->ramp_length = count; + + /* Determine the last source ramp. */ + src_ramp_last + = grad->color_ramp + + grad->ramp_length; + + /* Set the initial previous stop. */ + prev_stop = -1; + + /* Reset the count. */ + trg_count = 0; + + /* Walk through the source ramp. */ + for( + src_ramp = grad->color_ramp, trg_ramp = grad->converted_ramp; + (src_ramp < src_ramp_last) && (trg_count < VLC_MAX_COLOR_RAMP_STOPS + 2); + src_ramp += 1) { + /* Must be in increasing order. */ + if(src_ramp->stop < prev_stop) { + /* Ignore the entire sequence. */ + trg_count = 0; + break; + } + + /* Update the previous stop value. */ + prev_stop = src_ramp->stop; + + /* Must be within [0..1] range. */ + if((src_ramp->stop < 0.0f) || (src_ramp->stop > 1.0f)) { + /* Ignore. */ + continue; + } + + /* Clamp color. */ + ClampColor(COLOR_FROM_RAMP(src_ramp), COLOR_FROM_RAMP(trg_ramp), 0); + + /* First stop greater than zero? */ + if((trg_count == 0) && (src_ramp->stop > 0.0f)) { + /* Force the first stop to 0.0f. */ + trg_ramp->stop = 0.0f; + + /* Replicate the entry. */ + trg_ramp[1] = *trg_ramp; + trg_ramp[1].stop = src_ramp->stop; + + /* Advance. */ + trg_ramp += 2; + trg_count += 2; + } + else { + /* Set the stop value. */ + trg_ramp->stop = src_ramp->stop; + + /* Advance. */ + trg_ramp += 1; + trg_count += 1; + } + } + + /* Empty sequence? */ + if(trg_count == 0) { + memcpy(grad->converted_ramp, default_ramp, sizeof(default_ramp)); + grad->converted_length = sizeof(default_ramp) / 5; + } + else { + /* The last stop must be at 1.0. */ + if(trg_ramp[-1].stop != 1.0f) { + /* Replicate the last entry. */ + *trg_ramp = trg_ramp[-1]; + + /* Force the last stop to 1.0f. */ + trg_ramp->stop = 1.0f; + + /* Update the final entry count. */ + trg_count += 1; + } + + /* Set new length. */ + grad->converted_length = trg_count; + } + return VG_LITE_SUCCESS; + +Empty_sequence_handler: + memcpy(grad->converted_ramp, default_ramp, sizeof(default_ramp)); + grad->converted_length = sizeof(default_ramp) / 5; + + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_update_linear_grad(vg_lite_ext_linear_gradient_t * grad) + { + vg_lite_uint32_t ramp_length; + vg_lite_color_ramp_t * color_ramp; + vg_lite_uint32_t common, stop; + vg_lite_uint32_t i, width; + uint8_t * bits; + vg_lite_float_t x0, y0, x1, y1, length; + vg_lite_error_t error = VG_LITE_SUCCESS; + + /* Get shortcuts to the color ramp. */ + ramp_length = grad->converted_length; + color_ramp = grad->converted_ramp; + + x0 = grad->linear_grad.X0; + y0 = grad->linear_grad.Y0; + x1 = grad->linear_grad.X1; + y1 = grad->linear_grad.Y1; + length = (vg_lite_float_t)sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0)); + + if(length <= 0) + return VG_LITE_INVALID_ARGUMENT; + /* Find the common denominator of the color ramp stops. */ + if(length < 1) { + common = 1; + } + else { + common = (vg_lite_uint32_t)length; + } + + for(i = 0; i < ramp_length; ++i) { + if(color_ramp[i].stop != 0.0f) { + vg_lite_float_t mul = common * color_ramp[i].stop; + vg_lite_float_t frac = mul - (vg_lite_float_t)floor(mul); + if(frac > 0.00013f) { /* Suppose error for zero is 0.00013 */ + common = MAX(common, (vg_lite_uint32_t)(1.0f / frac + 0.5f)); + } + } + } + + /* Compute the width of the required color array. */ + width = common + 1; + + /* Allocate the color ramp surface. */ + memset(&grad->image, 0, sizeof(grad->image)); + grad->image.width = width; + grad->image.height = 1; + grad->image.stride = 0; + grad->image.image_mode = VG_LITE_NONE_IMAGE_MODE; + grad->image.format = VG_LITE_ABGR8888; + + /* Allocate the image for gradient. */ + VG_LITE_RETURN_ERROR(vg_lite_allocate(&grad->image)); + memset(grad->image.memory, 0, grad->image.stride * grad->image.height); + width = common + 1; + /* Set pointer to color array. */ + bits = (uint8_t *)grad->image.memory; + + /* Start filling the color array. */ + stop = 0; + for(i = 0; i < width; ++i) { + vg_lite_float_t gradient; + vg_lite_float_t color[4]; + vg_lite_float_t color1[4]; + vg_lite_float_t color2[4]; + vg_lite_float_t weight; + + if(i == 241) + i = 241; + /* Compute gradient for current color array entry. */ + gradient = (vg_lite_float_t)i / (vg_lite_float_t)(width - 1); + + /* Find the entry in the color ramp that matches or exceeds this + ** gradient. */ + while(gradient > color_ramp[stop].stop) { + ++stop; + } + + if(gradient == color_ramp[stop].stop) { + /* Perfect match weight 1.0. */ + weight = 1.0f; + + /* Use color ramp color. */ + color1[3] = color_ramp[stop].alpha; + color1[2] = color_ramp[stop].blue; + color1[1] = color_ramp[stop].green; + color1[0] = color_ramp[stop].red; + + color2[3] = color2[2] = color2[1] = color2[0] = 0.0f; + } + else { + if(stop == 0) { + return VG_LITE_INVALID_ARGUMENT; + } + /* Compute weight. */ + weight = (color_ramp[stop].stop - gradient) + / (color_ramp[stop].stop - color_ramp[stop - 1].stop); + + /* Grab color ramp color of previous stop. */ + color1[3] = color_ramp[stop - 1].alpha; + color1[2] = color_ramp[stop - 1].blue; + color1[1] = color_ramp[stop - 1].green; + color1[0] = color_ramp[stop - 1].red; + + /* Grab color ramp color of current stop. */ + color2[3] = color_ramp[stop].alpha; + color2[2] = color_ramp[stop].blue; + color2[1] = color_ramp[stop].green; + color2[0] = color_ramp[stop].red; + } + + if(grad->pre_multiplied) { + /* Pre-multiply the first color. */ + color1[2] *= color1[3]; + color1[1] *= color1[3]; + color1[0] *= color1[3]; + + /* Pre-multiply the second color. */ + color2[2] *= color2[3]; + color2[1] *= color2[3]; + color2[0] *= color2[3]; + } + + /* Filter the colors per channel. */ + color[3] = LERP(color1[3], color2[3], weight); + color[2] = LERP(color1[2], color2[2], weight); + color[1] = LERP(color1[1], color2[1], weight); + color[0] = LERP(color1[0], color2[0], weight); + + /* Pack the final color. */ + *bits++ = PackColorComponent(color[3]); + *bits++ = PackColorComponent(color[2]); + *bits++ = PackColorComponent(color[1]); + *bits++ = PackColorComponent(color[0]); + } + + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_draw_linear_grad(vg_lite_buffer_t * target, + vg_lite_path_t * path, + vg_lite_fill_t fill_rule, + vg_lite_matrix_t * path_matrix, + vg_lite_ext_linear_gradient_t * grad, + vg_lite_color_t paint_color, + vg_lite_blend_t blend, + vg_lite_filter_t filter) + { + LV_UNUSED(paint_color); + LV_UNUSED(filter); + + auto ctx = vg_lite_ctx::get_instance(); + TVG_CHECK_RETURN_VG_ERROR(canvas_set_target(ctx, target)); + + auto shape = Shape::gen(); + TVG_CHECK_RETURN_VG_ERROR(shape_append_path(shape, path, path_matrix)); + TVG_CHECK_RETURN_VG_ERROR(shape->transform(matrix_conv(path_matrix))); + TVG_CHECK_RETURN_VG_ERROR(shape->fill(fill_rule_conv(fill_rule));); + TVG_CHECK_RETURN_VG_ERROR(shape->blend(blend_method_conv(blend))); + + auto linearGrad = LinearGradient::gen(); + TVG_CHECK_RETURN_VG_ERROR(linearGrad->linear(grad->linear_grad.X0, grad->linear_grad.Y0, grad->linear_grad.X1, + grad->linear_grad.Y1)); + TVG_CHECK_RETURN_VG_ERROR(linearGrad->transform(matrix_conv(&grad->matrix))); + TVG_CHECK_RETURN_VG_ERROR(linearGrad->spread(fill_spread_conv(grad->spread_mode))); + + tvg::Fill::ColorStop colorStops[VLC_MAX_COLOR_RAMP_STOPS]; + for(vg_lite_uint32_t i = 0; i < grad->ramp_length; i++) { + colorStops[i].offset = grad->color_ramp[i].stop; + colorStops[i].r = grad->color_ramp[i].red * 255.0f; + colorStops[i].g = grad->color_ramp[i].green * 255.0f; + colorStops[i].b = grad->color_ramp[i].blue * 255.0f; + colorStops[i].a = grad->color_ramp[i].alpha * 255.0f; + } + TVG_CHECK_RETURN_VG_ERROR(linearGrad->colorStops(colorStops, grad->ramp_length)); + + TVG_CHECK_RETURN_VG_ERROR(shape->fill(std::move(linearGrad))); + TVG_CHECK_RETURN_VG_ERROR(ctx->canvas->push(std::move(shape))); + + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_set_radial_grad(vg_lite_radial_gradient_t * grad, + vg_lite_uint32_t count, + vg_lite_color_ramp_t * color_ramp, + vg_lite_radial_gradient_parameter_t radial_grad, + vg_lite_gradient_spreadmode_t spread_mode, + vg_lite_uint8_t pre_multiplied) + { + static vg_lite_color_ramp_t defaultRamp[] = { + { + 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f + }, + { + 1.0f, + 1.0f, 1.0f, 1.0f, 1.0f + } + }; + + vg_lite_uint32_t i, trgCount; + vg_lite_float_t prevStop; + vg_lite_color_ramp_t * srcRamp; + vg_lite_color_ramp_t * srcRampLast; + vg_lite_color_ramp_t * trgRamp; + + /* Reset the count. */ + trgCount = 0; + + if(radial_grad.r <= 0) + return VG_LITE_INVALID_ARGUMENT; + + grad->radial_grad = radial_grad; + grad->pre_multiplied = pre_multiplied; + grad->spread_mode = spread_mode; + + if(!count || count > VLC_MAX_COLOR_RAMP_STOPS || color_ramp == NULL) + goto Empty_sequence_handler; + + for(i = 0; i < count; i++) + grad->color_ramp[i] = color_ramp[i]; + grad->ramp_length = count; + + /* Determine the last source ramp. */ + srcRampLast + = grad->color_ramp + + grad->ramp_length; + + /* Set the initial previous stop. */ + prevStop = -1; + + /* Reset the count. */ + trgCount = 0; + + /* Walk through the source ramp. */ + for( + srcRamp = grad->color_ramp, trgRamp = grad->converted_ramp; + (srcRamp < srcRampLast) && (trgCount < VLC_MAX_COLOR_RAMP_STOPS + 2); + srcRamp += 1) { + /* Must be in increasing order. */ + if(srcRamp->stop < prevStop) { + /* Ignore the entire sequence. */ + trgCount = 0; + break; + } + + /* Update the previous stop value. */ + prevStop = srcRamp->stop; + + /* Must be within [0..1] range. */ + if((srcRamp->stop < 0.0f) || (srcRamp->stop > 1.0f)) { + /* Ignore. */ + continue; + } + + /* Clamp color. */ + ClampColor(COLOR_FROM_RAMP(srcRamp), COLOR_FROM_RAMP(trgRamp), 0); + + /* First stop greater than zero? */ + if((trgCount == 0) && (srcRamp->stop > 0.0f)) { + /* Force the first stop to 0.0f. */ + trgRamp->stop = 0.0f; + + /* Replicate the entry. */ + trgRamp[1] = *trgRamp; + trgRamp[1].stop = srcRamp->stop; + + /* Advance. */ + trgRamp += 2; + trgCount += 2; + } + else { + /* Set the stop value. */ + trgRamp->stop = srcRamp->stop; + + /* Advance. */ + trgRamp += 1; + trgCount += 1; + } + } + + /* Empty sequence? */ + if(trgCount == 0) { + memcpy(grad->converted_ramp, defaultRamp, sizeof(defaultRamp)); + grad->converted_length = sizeof(defaultRamp) / 5; + } + else { + /* The last stop must be at 1.0. */ + if(trgRamp[-1].stop != 1.0f) { + /* Replicate the last entry. */ + *trgRamp = trgRamp[-1]; + + /* Force the last stop to 1.0f. */ + trgRamp->stop = 1.0f; + + /* Update the final entry count. */ + trgCount += 1; + } + + /* Set new length. */ + grad->converted_length = trgCount; + } + return VG_LITE_SUCCESS; + +Empty_sequence_handler: + memcpy(grad->converted_ramp, defaultRamp, sizeof(defaultRamp)); + grad->converted_length = sizeof(defaultRamp) / 5; + + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_update_radial_grad(vg_lite_radial_gradient_t * grad) + { + vg_lite_uint32_t ramp_length; + vg_lite_color_ramp_t * colorRamp; + vg_lite_uint32_t common, stop; + vg_lite_uint32_t i, width; + uint8_t * bits; + vg_lite_error_t error = VG_LITE_SUCCESS; + vg_lite_uint32_t align, mul, div; + + /* Get shortcuts to the color ramp. */ + ramp_length = grad->converted_length; + colorRamp = grad->converted_ramp; + + if(grad->radial_grad.r <= 0) + return VG_LITE_INVALID_ARGUMENT; + + /* Find the common denominator of the color ramp stops. */ + if(grad->radial_grad.r < 1) { + common = 1; + } + else { + common = (vg_lite_uint32_t)grad->radial_grad.r; + } + + for(i = 0; i < ramp_length; ++i) { + if(colorRamp[i].stop != 0.0f) { + vg_lite_float_t m = common * colorRamp[i].stop; + vg_lite_float_t frac = m - (vg_lite_float_t)floor(m); + if(frac > 0.00013f) { /* Suppose error for zero is 0.00013 */ + common = MAX(common, (vg_lite_uint32_t)(1.0f / frac + 0.5f)); + } + } + } + + /* Compute the width of the required color array. */ + width = common + 1; + width = (width + 15) & (~0xf); + + /* Allocate the color ramp surface. */ + memset(&grad->image, 0, sizeof(grad->image)); + grad->image.width = width; + grad->image.height = 1; + grad->image.stride = 0; + grad->image.image_mode = VG_LITE_NONE_IMAGE_MODE; + grad->image.format = VG_LITE_ABGR8888; + + /* Allocate the image for gradient. */ + VG_LITE_RETURN_ERROR(vg_lite_allocate(&grad->image)); + + get_format_bytes(VG_LITE_ABGR8888, &mul, &div, &align); + width = grad->image.stride * div / mul; + + /* Set pointer to color array. */ + bits = (uint8_t *)grad->image.memory; + + /* Start filling the color array. */ + stop = 0; + for(i = 0; i < width; ++i) { + vg_lite_float_t gradient; + vg_lite_float_t color[4]; + vg_lite_float_t color1[4]; + vg_lite_float_t color2[4]; + vg_lite_float_t weight; + + /* Compute gradient for current color array entry. */ + gradient = (vg_lite_float_t)i / (vg_lite_float_t)(width - 1); + + /* Find the entry in the color ramp that matches or exceeds this + ** gradient. */ + while(gradient > colorRamp[stop].stop) { + ++stop; + } + + if(gradient == colorRamp[stop].stop) { + /* Perfect match weight 1.0. */ + weight = 1.0f; + + /* Use color ramp color. */ + color1[3] = colorRamp[stop].alpha; + color1[2] = colorRamp[stop].blue; + color1[1] = colorRamp[stop].green; + color1[0] = colorRamp[stop].red; + + color2[3] = color2[2] = color2[1] = color2[0] = 0.0f; + } + else { + /* Compute weight. */ + weight = (colorRamp[stop].stop - gradient) + / (colorRamp[stop].stop - colorRamp[stop - 1].stop); + + /* Grab color ramp color of previous stop. */ + color1[3] = colorRamp[stop - 1].alpha; + color1[2] = colorRamp[stop - 1].blue; + color1[1] = colorRamp[stop - 1].green; + color1[0] = colorRamp[stop - 1].red; + + /* Grab color ramp color of current stop. */ + color2[3] = colorRamp[stop].alpha; + color2[2] = colorRamp[stop].blue; + color2[1] = colorRamp[stop].green; + color2[0] = colorRamp[stop].red; + } + + if(grad->pre_multiplied) { + /* Pre-multiply the first color. */ + color1[2] *= color1[3]; + color1[1] *= color1[3]; + color1[0] *= color1[3]; + + /* Pre-multiply the second color. */ + color2[2] *= color2[3]; + color2[1] *= color2[3]; + color2[0] *= color2[3]; + } + + /* Filter the colors per channel. */ + color[3] = LERP(color1[3], color2[3], weight); + color[2] = LERP(color1[2], color2[2], weight); + color[1] = LERP(color1[1], color2[1], weight); + color[0] = LERP(color1[0], color2[0], weight); + + /* Pack the final color. */ + *bits++ = PackColorComponent(color[3]); + *bits++ = PackColorComponent(color[2]); + *bits++ = PackColorComponent(color[1]); + *bits++ = PackColorComponent(color[0]); + } + + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_set_grad(vg_lite_linear_gradient_t * grad, + vg_lite_uint32_t count, + vg_lite_uint32_t * colors, + vg_lite_uint32_t * stops) + { + vg_lite_uint32_t i; + + grad->count = 0; /* Opaque B&W gradient */ + if(!count || count > VLC_MAX_GRADIENT_STOPS || colors == NULL || stops == NULL) + return VG_LITE_SUCCESS; + + /* Check stops validity */ + for(i = 0; i < count; i++) + if(stops[i] < VLC_GRADIENT_BUFFER_WIDTH) { + if(!grad->count || stops[i] > grad->stops[grad->count - 1]) { + grad->stops[grad->count] = stops[i]; + grad->colors[grad->count] = colors[i]; + grad->count++; + } + else if(stops[i] == grad->stops[grad->count - 1]) { + /* Equal stops : use the color corresponding to the last stop + in the sequence */ + grad->colors[grad->count - 1] = colors[i]; + } + } + + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_update_grad(vg_lite_linear_gradient_t * grad) + { + vg_lite_error_t error = VG_LITE_SUCCESS; + int32_t r0, g0, b0, a0; + int32_t r1, g1, b1, a1; + int32_t lr, lg, lb, la; + vg_lite_uint32_t i; + int32_t j; + int32_t ds, dr, dg, db, da; + vg_lite_uint32_t * buffer = (vg_lite_uint32_t *)grad->image.memory; + + if(grad->count == 0) { + /* If no valid stops have been specified (e.g., due to an empty input + * array, out-of-range, or out-of-order stops), a stop at 0 with color + * 0xFF000000 (opaque black) and a stop at 255 with color 0xFFFFFFFF + * (opaque white) are implicitly defined. */ + grad->stops[0] = 0; + grad->colors[0] = 0xFF000000; /* Opaque black */ + grad->stops[1] = 255; + grad->colors[1] = 0xFFFFFFFF; /* Opaque white */ + grad->count = 2; + } + else if(grad->count && grad->stops[0] != 0) { + /* If at least one valid stop has been specified, but none has been + * defined with an offset of 0, an implicit stop is added with an + * offset of 0 and the same color as the first user-defined stop. */ + for(i = 0; i < grad->stops[0]; i++) + buffer[i] = grad->colors[0]; + } + a0 = A(grad->colors[0]); + r0 = R(grad->colors[0]); + g0 = G(grad->colors[0]); + b0 = B(grad->colors[0]); + + /* Calculate the colors for each pixel of the image. */ + for(i = 0; i < grad->count - 1; i++) { + buffer[grad->stops[i]] = grad->colors[i]; + ds = grad->stops[i + 1] - grad->stops[i]; + a1 = A(grad->colors[i + 1]); + r1 = R(grad->colors[i + 1]); + g1 = G(grad->colors[i + 1]); + b1 = B(grad->colors[i + 1]); + + da = a1 - a0; + dr = r1 - r0; + dg = g1 - g0; + db = b1 - b0; + + for(j = 1; j < ds; j++) { + la = a0 + da * j / ds; + lr = r0 + dr * j / ds; + lg = g0 + dg * j / ds; + lb = b0 + db * j / ds; + + buffer[grad->stops[i] + j] = ARGB(la, lr, lg, lb); + } + + a0 = a1; + r0 = r1; + g0 = g1; + b0 = b1; + } + + /* If at least one valid stop has been specified, but none has been defined + * with an offset of 255, an implicit stop is added with an offset of 255 + * and the same color as the last user-defined stop. */ + for(i = grad->stops[grad->count - 1]; i < VLC_GRADIENT_BUFFER_WIDTH; i++) + buffer[i] = grad->colors[grad->count - 1]; + + return error; + } + + vg_lite_error_t vg_lite_clear_linear_grad(vg_lite_ext_linear_gradient_t * grad) + { + vg_lite_error_t error = VG_LITE_SUCCESS; + + grad->count = 0; + /* Release the image resource. */ + if(grad->image.handle != NULL) { + error = vg_lite_free(&grad->image); + } + + return error; + } + + vg_lite_error_t vg_lite_clear_grad(vg_lite_linear_gradient_t * grad) + { + vg_lite_error_t error = VG_LITE_SUCCESS; + + grad->count = 0; + /* Release the image resource. */ + if(grad->image.handle != NULL) { + error = vg_lite_free(&grad->image); + } + + return error; + } + + vg_lite_error_t vg_lite_clear_radial_grad(vg_lite_radial_gradient_t * grad) + { + vg_lite_error_t error = VG_LITE_SUCCESS; + + grad->count = 0; + /* Release the image resource. */ + if(grad->image.handle != NULL) { + error = vg_lite_free(&grad->image); + } + + return error; + } + + vg_lite_matrix_t * vg_lite_get_linear_grad_matrix(vg_lite_ext_linear_gradient_t * grad) + { + return &grad->matrix; + } + + vg_lite_matrix_t * vg_lite_get_grad_matrix(vg_lite_linear_gradient_t * grad) + { + return &grad->matrix; + } + + vg_lite_matrix_t * vg_lite_get_radial_grad_matrix(vg_lite_radial_gradient_t * grad) + { + return &grad->matrix; + } + + vg_lite_error_t vg_lite_draw_grad(vg_lite_buffer_t * target, + vg_lite_path_t * path, + vg_lite_fill_t fill_rule, + vg_lite_matrix_t * matrix, + vg_lite_linear_gradient_t * grad, + vg_lite_blend_t blend) + { + auto ctx = vg_lite_ctx::get_instance(); + TVG_CHECK_RETURN_VG_ERROR(canvas_set_target(ctx, target)); + + auto shape = Shape::gen(); + TVG_CHECK_RETURN_VG_ERROR(shape_append_path(shape, path, matrix)); + TVG_CHECK_RETURN_VG_ERROR(shape->transform(matrix_conv(matrix))); + TVG_CHECK_RETURN_VG_ERROR(shape->fill(fill_rule_conv(fill_rule));); + TVG_CHECK_RETURN_VG_ERROR(shape->blend(blend_method_conv(blend))); + + vg_lite_matrix_t grad_matrix; + vg_lite_identity(&grad_matrix); + vg_lite_matrix_inverse(&grad_matrix, matrix); + vg_lite_matrix_multiply(&grad_matrix, &grad->matrix); + + vg_lite_fpoint_t p1 = {0.0f, 0.0f}; + vg_lite_fpoint_t p2 = {1.0f, 0}; + + vg_lite_fpoint_t p1_trans = p1; + vg_lite_fpoint_t p2_trans = p2; + + p1_trans = matrix_transform_point(&grad_matrix, &p1); + p2_trans = matrix_transform_point(&grad_matrix, &p2); + float dx = (p2_trans.x - p1_trans.x); + float dy = (p2_trans.y - p1_trans.y); + float scale = sqrtf(dx * dx + dy * dy); + float angle = (float)(atan2f(dy, dx)); + float dlen = 256 * scale; + float x_min = grad_matrix.m[0][2]; + float y_min = grad_matrix.m[1][2]; + float x_max = x_min + dlen * cosf(angle); + float y_max = y_min + dlen * sinf(angle); + LV_LOG_TRACE("linear gradient {%.2f, %.2f} ~ {%.2f, %.2f}", x_min, y_min, x_max, y_max); + auto linearGrad = LinearGradient::gen(); + TVG_CHECK_RETURN_VG_ERROR(linearGrad->linear(x_min, y_min, x_max, y_max)); + TVG_CHECK_RETURN_VG_ERROR(linearGrad->spread(FillSpread::Pad)); + + tvg::Fill::ColorStop colorStops[VLC_MAX_GRADIENT_STOPS]; + for(vg_lite_uint32_t i = 0; i < grad->count; i++) { + colorStops[i].offset = grad->stops[i] / 255.0f; + colorStops[i].r = R(grad->colors[i]); + colorStops[i].g = G(grad->colors[i]); + colorStops[i].b = B(grad->colors[i]); + colorStops[i].a = A(grad->colors[i]); + } + TVG_CHECK_RETURN_VG_ERROR(linearGrad->colorStops(colorStops, grad->count)); + + TVG_CHECK_RETURN_VG_ERROR(shape->fill(std::move(linearGrad))); + TVG_CHECK_RETURN_VG_ERROR(ctx->canvas->push(std::move(shape))); + + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_draw_radial_grad(vg_lite_buffer_t * target, + vg_lite_path_t * path, + vg_lite_fill_t fill_rule, + vg_lite_matrix_t * path_matrix, + vg_lite_radial_gradient_t * grad, + vg_lite_color_t paint_color, + vg_lite_blend_t blend, + vg_lite_filter_t filter) + { + LV_UNUSED(paint_color); + LV_UNUSED(filter); + + auto ctx = vg_lite_ctx::get_instance(); + TVG_CHECK_RETURN_VG_ERROR(canvas_set_target(ctx, target)); + + auto shape = Shape::gen(); + TVG_CHECK_RETURN_VG_ERROR(shape_append_path(shape, path, path_matrix)); + TVG_CHECK_RETURN_VG_ERROR(shape->transform(matrix_conv(path_matrix))); + TVG_CHECK_RETURN_VG_ERROR(shape->fill(fill_rule_conv(fill_rule));); + TVG_CHECK_RETURN_VG_ERROR(shape->blend(blend_method_conv(blend))); + + auto radialGrad = RadialGradient::gen(); + TVG_CHECK_RETURN_VG_ERROR(radialGrad->transform(matrix_conv(&grad->matrix))); + TVG_CHECK_RETURN_VG_ERROR(radialGrad->radial(grad->radial_grad.cx, grad->radial_grad.cy, grad->radial_grad.r)); + TVG_CHECK_RETURN_VG_ERROR(radialGrad->spread(fill_spread_conv(grad->spread_mode))); + + tvg::Fill::ColorStop colorStops[VLC_MAX_COLOR_RAMP_STOPS]; + for(vg_lite_uint32_t i = 0; i < grad->ramp_length; i++) { + colorStops[i].offset = grad->color_ramp[i].stop; + colorStops[i].r = grad->color_ramp[i].red * 255.0f; + colorStops[i].g = grad->color_ramp[i].green * 255.0f; + colorStops[i].b = grad->color_ramp[i].blue * 255.0f; + colorStops[i].a = grad->color_ramp[i].alpha * 255.0f; + } + TVG_CHECK_RETURN_VG_ERROR(radialGrad->colorStops(colorStops, grad->ramp_length)); + + TVG_CHECK_RETURN_VG_ERROR(shape->fill(std::move(radialGrad))); + TVG_CHECK_RETURN_VG_ERROR(ctx->canvas->push(std::move(shape))); + + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_set_command_buffer_size(vg_lite_uint32_t size) + { + LV_UNUSED(size); + return VG_LITE_NOT_SUPPORT; + } + + vg_lite_error_t vg_lite_set_scissor(vg_lite_int32_t x, vg_lite_int32_t y, vg_lite_int32_t right, vg_lite_int32_t bottom) + { + auto ctx = vg_lite_ctx::get_instance(); + vg_lite_int32_t width = right - x; + vg_lite_int32_t height = bottom - y; + + if(width <= 0 || height <= 0) { + return VG_LITE_INVALID_ARGUMENT; + } + + if(ctx->scissor_rect.x == x && ctx->scissor_rect.y == y && + ctx->scissor_rect.width == width && ctx->scissor_rect.height == height) { + return VG_LITE_SUCCESS; + } + + /*Finish the previous rendering before setting the new scissor*/ + vg_lite_error_t error; + VG_LITE_RETURN_ERROR(vg_lite_finish()); + + ctx->scissor_rect.x = x; + ctx->scissor_rect.y = y; + ctx->scissor_rect.width = width; + ctx->scissor_rect.height = height; + ctx->scissor_is_set = true; + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_enable_scissor(void) + { + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_disable_scissor(void) + { + return VG_LITE_SUCCESS; + } + + vg_lite_error_t vg_lite_get_mem_size(vg_lite_uint32_t * size) + { + *size = 0; + return VG_LITE_NOT_SUPPORT; + } + + vg_lite_error_t vg_lite_source_global_alpha(vg_lite_global_alpha_t alpha_mode, uint8_t alpha_value) + { + LV_UNUSED(alpha_mode); + LV_UNUSED(alpha_value); + return VG_LITE_NOT_SUPPORT; + } + + vg_lite_error_t vg_lite_dest_global_alpha(vg_lite_global_alpha_t alpha_mode, uint8_t alpha_value) + { + LV_UNUSED(alpha_mode); + LV_UNUSED(alpha_value); + return VG_LITE_NOT_SUPPORT; + } + + vg_lite_error_t vg_lite_set_color_key(vg_lite_color_key4_t colorkey) + { + LV_UNUSED(colorkey); + return VG_LITE_NOT_SUPPORT; + } + + vg_lite_error_t vg_lite_set_flexa_stream_id(uint8_t stream_id) + { + LV_UNUSED(stream_id); + return VG_LITE_NOT_SUPPORT; + } + + vg_lite_error_t vg_lite_set_flexa_current_background_buffer(uint8_t stream_id, + vg_lite_buffer_t * buffer, + vg_lite_uint32_t background_segment_count, + vg_lite_uint32_t background_segment_size) + { + LV_UNUSED(stream_id); + LV_UNUSED(buffer); + LV_UNUSED(background_segment_count); + LV_UNUSED(background_segment_size); + return VG_LITE_NOT_SUPPORT; + } + + vg_lite_error_t vg_lite_enable_flexa(void) + { + return VG_LITE_NOT_SUPPORT; + } + + vg_lite_error_t vg_lite_disable_flexa(void) + { + return VG_LITE_NOT_SUPPORT; + } + + vg_lite_error_t vg_lite_set_flexa_stop_frame(void) + { + return VG_LITE_NOT_SUPPORT; + } + + vg_lite_error_t vg_lite_enable_dither(void) + { + return VG_LITE_NOT_SUPPORT; + } + + vg_lite_error_t vg_lite_disable_dither(void) + { + return VG_LITE_NOT_SUPPORT; + } + + vg_lite_error_t vg_lite_set_tess_buffer(vg_lite_uint32_t physical, vg_lite_uint32_t size) + { + LV_UNUSED(physical); + LV_UNUSED(size); + return VG_LITE_NOT_SUPPORT; + } + + vg_lite_error_t vg_lite_set_command_buffer(vg_lite_uint32_t physical, vg_lite_uint32_t size) + { + LV_UNUSED(physical); + LV_UNUSED(size); + return VG_LITE_NOT_SUPPORT; + } + + vg_lite_error_t vg_lite_get_parameter(vg_lite_param_type_t type, + vg_lite_int32_t count, + vg_lite_float_t * params) + { + switch(type) { + case VG_LITE_GPU_IDLE_STATE: + if(count != 1 || params == NULL) { + return VG_LITE_INVALID_ARGUMENT; + } + + *(vg_lite_uint32_t *)params = 1; + return VG_LITE_SUCCESS; + + default: + break; + } + + return VG_LITE_NOT_SUPPORT; + } +} /* extern "C" */ + +/********************** + * STATIC FUNCTIONS + **********************/ + +static vg_lite_error_t vg_lite_error_conv(Result result) +{ + switch(result) { + case Result::Success: + return VG_LITE_SUCCESS; + + case Result::InvalidArguments: + return VG_LITE_INVALID_ARGUMENT; + + case Result::InsufficientCondition: + return VG_LITE_OUT_OF_RESOURCES; + + case Result::FailedAllocation: + return VG_LITE_OUT_OF_MEMORY; + + case Result::NonSupport: + return VG_LITE_NOT_SUPPORT; + + default: + break; + } + + return VG_LITE_TIMEOUT; +} + +static Matrix matrix_conv(const vg_lite_matrix_t * matrix) +{ + return *(Matrix *)matrix; +} + +static FillRule fill_rule_conv(vg_lite_fill_t fill) +{ + if(fill == VG_LITE_FILL_EVEN_ODD) { + return FillRule::EvenOdd; + } + + return FillRule::Winding; +} + +static BlendMethod blend_method_conv(vg_lite_blend_t blend) +{ + switch(blend) { + case VG_LITE_BLEND_NONE: + return BlendMethod::SrcOver; + + case VG_LITE_BLEND_NORMAL_LVGL: + return BlendMethod::Normal; + + case VG_LITE_BLEND_SRC_OVER: + return BlendMethod::Normal; + + case VG_LITE_BLEND_SCREEN: + return BlendMethod::Screen; + + case VG_LITE_BLEND_ADDITIVE: + return BlendMethod::Add; + + case VG_LITE_BLEND_MULTIPLY: + return BlendMethod::Multiply; + + default: + break; + } + + return BlendMethod::Normal; +} + +static StrokeCap stroke_cap_conv(vg_lite_cap_style_t cap) +{ + switch(cap) { + case VG_LITE_CAP_SQUARE: + return StrokeCap::Square; + case VG_LITE_CAP_ROUND: + return StrokeCap::Round; + case VG_LITE_CAP_BUTT: + return StrokeCap::Butt; + default: + break; + } + + return StrokeCap::Square; +} + +static StrokeJoin stroke_join_conv(vg_lite_join_style_t join) +{ + switch(join) { + case VG_LITE_JOIN_BEVEL: + return StrokeJoin::Bevel; + case VG_LITE_JOIN_ROUND: + return StrokeJoin::Round; + case VG_LITE_JOIN_MITER: + return StrokeJoin::Miter; + default: + break; + } + + return StrokeJoin::Bevel; +} + +static FillSpread fill_spread_conv(vg_lite_gradient_spreadmode_t spread) +{ + switch(spread) { + case VG_LITE_GRADIENT_SPREAD_PAD: + return FillSpread::Pad; + case VG_LITE_GRADIENT_SPREAD_REPEAT: + return FillSpread::Repeat; + case VG_LITE_GRADIENT_SPREAD_REFLECT: + return FillSpread::Reflect; + default: + return FillSpread::Pad; + } +} + +static float vlc_get_arg(const void * data, vg_lite_format_t format) +{ + switch(format) { + case VG_LITE_S8: + return *((int8_t *)data); + + case VG_LITE_S16: + return *((int16_t *)data); + + case VG_LITE_S32: + return *((int32_t *)data); + + case VG_LITE_FP32: + return *((float *)data); + + default: + LV_LOG_ERROR("UNKNOW_FORMAT: %d", format); + break; + } + + return 0; +} + +static uint8_t vlc_format_len(vg_lite_format_t format) +{ + switch(format) { + case VG_LITE_S8: + return 1; + case VG_LITE_S16: + return 2; + case VG_LITE_S32: + return 4; + case VG_LITE_FP32: + return 4; + default: + LV_LOG_ERROR("UNKNOW_FORMAT: %d", format); + LV_ASSERT(false); + break; + } + + return 0; +} + +static uint8_t vlc_op_arg_len(uint8_t vlc_op) +{ + switch(vlc_op) { + VLC_OP_ARG_LEN(END, 0); + VLC_OP_ARG_LEN(CLOSE, 0); + VLC_OP_ARG_LEN(MOVE, 2); + VLC_OP_ARG_LEN(MOVE_REL, 2); + VLC_OP_ARG_LEN(LINE, 2); + VLC_OP_ARG_LEN(LINE_REL, 2); + VLC_OP_ARG_LEN(QUAD, 4); + VLC_OP_ARG_LEN(QUAD_REL, 4); + VLC_OP_ARG_LEN(CUBIC, 6); + VLC_OP_ARG_LEN(CUBIC_REL, 6); + VLC_OP_ARG_LEN(SCCWARC, 5); + VLC_OP_ARG_LEN(SCCWARC_REL, 5); + VLC_OP_ARG_LEN(SCWARC, 5); + VLC_OP_ARG_LEN(SCWARC_REL, 5); + VLC_OP_ARG_LEN(LCCWARC, 5); + VLC_OP_ARG_LEN(LCCWARC_REL, 5); + VLC_OP_ARG_LEN(LCWARC, 5); + VLC_OP_ARG_LEN(LCWARC_REL, 5); + default: + LV_LOG_ERROR("UNKNOW_VLC_OP: 0x%x", vlc_op); + LV_ASSERT(false); + break; + } + + return 0; +} + +static Result shape_set_stroke(std::unique_ptr & shape, const vg_lite_path_t * path) +{ + switch(path->path_type) { + case VG_LITE_DRAW_ZERO: + case VG_LITE_DRAW_FILL_PATH: + /* if path is not a stroke, return */ + return Result::Success; + + case VG_LITE_DRAW_STROKE_PATH: + case VG_LITE_DRAW_FILL_STROKE_PATH: + break; + + default: + LV_LOG_ERROR("unknown path type: %d", path->path_type); + return Result::InvalidArguments; + } + + LV_ASSERT_NULL(path->stroke); + TVG_CHECK_RETURN_RESULT(shape->stroke(path->stroke->line_width)); + TVG_CHECK_RETURN_RESULT(shape->strokeMiterlimit(path->stroke->miter_limit)); + TVG_CHECK_RETURN_RESULT(shape->stroke(stroke_cap_conv(path->stroke->cap_style))); + TVG_CHECK_RETURN_RESULT(shape->stroke(stroke_join_conv(path->stroke->join_style))); + TVG_CHECK_RETURN_RESULT(shape->stroke(TVG_COLOR(path->stroke_color))); + + if(path->stroke->pattern_count) { + LV_ASSERT_NULL(path->stroke->dash_pattern); + TVG_CHECK_RETURN_RESULT(shape->stroke(path->stroke->dash_pattern, path->stroke->pattern_count)); + } + + return Result::Success; +} + +static Result shape_append_path(std::unique_ptr & shape, vg_lite_path_t * path, vg_lite_matrix_t * matrix) +{ + uint8_t fmt_len = vlc_format_len(path->format); + uint8_t * cur = (uint8_t *)path->path; + uint8_t * end = cur + path->path_length; + + while(cur < end) { + /* get op code */ + uint8_t op_code = VLC_GET_OP_CODE(cur); + + /* get arguments length */ + uint8_t arg_len = vlc_op_arg_len(op_code); + + /* skip op code */ + cur += fmt_len; + + switch(op_code) { + case VLC_OP_MOVE: { + float x = VLC_GET_ARG(cur, 0); + float y = VLC_GET_ARG(cur, 1); + TVG_CHECK_RETURN_RESULT(shape->moveTo(x, y)); + } + break; + + case VLC_OP_LINE: { + float x = VLC_GET_ARG(cur, 0); + float y = VLC_GET_ARG(cur, 1); + TVG_CHECK_RETURN_RESULT(shape->lineTo(x, y)); + } + break; + + case VLC_OP_QUAD: { + /* hack pre point */ + float qcx0 = VLC_GET_ARG(cur, -3); + float qcy0 = VLC_GET_ARG(cur, -2); + float qcx1 = VLC_GET_ARG(cur, 0); + float qcy1 = VLC_GET_ARG(cur, 1); + float x = VLC_GET_ARG(cur, 2); + float y = VLC_GET_ARG(cur, 3); + + qcx0 += (qcx1 - qcx0) * 2 / 3; + qcy0 += (qcy1 - qcy0) * 2 / 3; + qcx1 = x + (qcx1 - x) * 2 / 3; + qcy1 = y + (qcy1 - y) * 2 / 3; + + TVG_CHECK_RETURN_RESULT(shape->cubicTo(qcx0, qcy0, qcx1, qcy1, x, y)); + } + break; + + case VLC_OP_CUBIC: { + float cx1 = VLC_GET_ARG(cur, 0); + float cy1 = VLC_GET_ARG(cur, 1); + float cx2 = VLC_GET_ARG(cur, 2); + float cy2 = VLC_GET_ARG(cur, 3); + float x = VLC_GET_ARG(cur, 4); + float y = VLC_GET_ARG(cur, 5); + TVG_CHECK_RETURN_RESULT(shape->cubicTo(cx1, cy1, cx2, cy2, x, y)); + } + break; + + case VLC_OP_CLOSE: + TVG_CHECK_RETURN_RESULT(shape->close()); + break; + + default: + break; + } + + cur += arg_len * fmt_len; + } + + TVG_CHECK_RETURN_RESULT(shape_set_stroke(shape, path)); + + float x_min = path->bounding_box[0]; + float y_min = path->bounding_box[1]; + float x_max = path->bounding_box[2]; + float y_max = path->bounding_box[3]; + + if(math_equal(x_min, FLT_MIN) && math_equal(y_min, FLT_MIN) + && math_equal(x_max, FLT_MAX) && math_equal(y_max, FLT_MAX)) { + return Result::Success; + } + + auto cilp = Shape::gen(); + TVG_CHECK_RETURN_RESULT(cilp->appendRect(x_min, y_min, x_max - x_min, y_max - y_min, 0, 0)); + TVG_CHECK_RETURN_RESULT(cilp->transform(matrix_conv(matrix))); + TVG_CHECK_RETURN_RESULT(shape->composite(std::move(cilp), CompositeMethod::ClipPath)); + + return Result::Success; +} + +static Result shape_append_rect(std::unique_ptr & shape, const vg_lite_buffer_t * target, + const vg_lite_rectangle_t * rect) +{ + if(rect) { + TVG_CHECK_RETURN_RESULT(shape->appendRect(rect->x, rect->y, rect->width, rect->height, 0, 0)); + } + else if(target) { + TVG_CHECK_RETURN_RESULT(shape->appendRect(0, 0, target->width, target->height, 0, 0)); + } + else { + return Result::InvalidArguments; + } + + return Result::Success; +} + +static Result canvas_set_target(vg_lite_ctx * ctx, vg_lite_buffer_t * target) +{ + /* if target_buffer needs to be changed, finish current drawing */ + if(ctx->target_buffer && ctx->target_buffer != target->memory) { + vg_lite_finish(); + } + + ctx->target_buffer = target->memory; + ctx->target_format = target->format; + ctx->target_px_size = target->width * target->height; + + void * canvas_target_buffer; + if(TVG_IS_VG_FMT_SUPPORT(target->format)) { + /* if target format is supported by VG, use target buffer directly */ + canvas_target_buffer = target->memory; + } + else { + /* if target format is not supported by VG, use internal buffer */ + canvas_target_buffer = ctx->get_temp_target_buffer(target->width, target->height); + } + + /* Prevent repeated target setting */ + if(ctx->tvg_target_buffer == canvas_target_buffer) { + return Result::Success; + } + + ctx->tvg_target_buffer = canvas_target_buffer; + + TVG_CHECK_RETURN_RESULT(ctx->canvas->target( + (uint32_t *)ctx->tvg_target_buffer, + target->width, + target->width, + target->height, + SwCanvas::ARGB8888)); + + if(ctx->scissor_is_set) { + TVG_CHECK_RETURN_RESULT( + ctx->canvas->viewport( + ctx->scissor_rect.x, ctx->scissor_rect.y, + ctx->scissor_rect.width, ctx->scissor_rect.height)); + } + + return Result::Success; +} + +static vg_lite_uint32_t width_to_stride(vg_lite_uint32_t w, vg_lite_buffer_format_t color_format) +{ + if(vg_lite_query_feature(gcFEATURE_BIT_VG_16PIXELS_ALIGN)) { + w = VG_LITE_ALIGN(w, 16); + } + + vg_lite_uint32_t mul, div, align; + get_format_bytes(color_format, &mul, &div, &align); + return VG_LITE_ALIGN((w * mul / div), align); +} + +static bool decode_indexed_line( + vg_lite_buffer_format_t color_format, + const vg_lite_uint32_t * palette, + int32_t x, int32_t y, + int32_t w_px, const uint8_t * in, vg_lite_uint32_t * out) +{ + uint8_t px_size; + uint16_t mask; + + vg_lite_uint32_t w_byte = width_to_stride(w_px, color_format); + + in += w_byte * y; /*First pixel*/ + out += w_px * y; + + int8_t shift = 0; + switch(color_format) { + case VG_LITE_INDEX_1: + px_size = 1; + in += x / 8; /*8pixel per byte*/ + shift = 7 - (x & 0x7); + break; + case VG_LITE_INDEX_2: + px_size = 2; + in += x / 4; /*4pixel per byte*/ + shift = 6 - 2 * (x & 0x3); + break; + case VG_LITE_INDEX_4: + px_size = 4; + in += x / 2; /*2pixel per byte*/ + shift = 4 - 4 * (x & 0x1); + break; + case VG_LITE_INDEX_8: + px_size = 8; + in += x; + shift = 0; + break; + default: + LV_ASSERT(false); + return false; + } + + mask = (1 << px_size) - 1; /*E.g. px_size = 2; mask = 0x03*/ + + int32_t i; + for(i = 0; i < w_px; i++) { + uint8_t val_act = (*in >> shift) & mask; + out[i] = palette[val_act]; + + shift -= px_size; + if(shift < 0) { + shift = 8 - px_size; + in++; + } + } + return true; +} + +static Result picture_load(vg_lite_ctx * ctx, std::unique_ptr & picture, const vg_lite_buffer_t * source, + vg_lite_color_t color) +{ + vg_lite_uint32_t * image_buffer; + LV_ASSERT(VG_LITE_IS_ALIGNED(source->memory, LV_VG_LITE_THORVG_BUF_ADDR_ALIGN)); + +#if LV_VG_LITE_THORVG_16PIXELS_ALIGN + LV_ASSERT(VG_LITE_IS_ALIGNED(source->width, 16)); +#endif + + if(source->format == VG_LITE_BGRA8888 && source->image_mode == VG_LITE_NORMAL_IMAGE_MODE) { + image_buffer = (vg_lite_uint32_t *)source->memory; + } + else { + vg_lite_uint32_t width = source->width; + vg_lite_uint32_t height = source->height; + vg_lite_uint32_t px_size = width * height; + image_buffer = ctx->get_image_buffer(width, height); + + vg_lite_buffer_t target; + memset(&target, 0, sizeof(target)); + target.memory = image_buffer; + target.format = VG_LITE_BGRA8888; + target.width = width; + target.height = height; + target.stride = width_to_stride(width, target.format); + + switch(source->format) { + case VG_LITE_INDEX_1: + case VG_LITE_INDEX_2: + case VG_LITE_INDEX_4: + case VG_LITE_INDEX_8: { + const vg_lite_uint32_t * clut_colors = ctx->get_CLUT(source->format); + for(vg_lite_uint32_t y = 0; y < height; y++) { + decode_indexed_line(source->format, clut_colors, 0, y, width, (uint8_t *)source->memory, image_buffer); + } + } + break; + + case VG_LITE_A4: { + conv_alpha4_to_bgra8888.convert(&target, source, color); + } + break; + + case VG_LITE_A8: { + conv_alpha8_to_bgra8888.convert(&target, source, color); + } + break; + + case VG_LITE_L8: { + conv_l8_to_bgra8888.convert(&target, source); + } + break; + + case VG_LITE_BGRX8888: { + conv_bgrx8888_to_bgra8888.convert(&target, source); + } + break; + + case VG_LITE_BGR888: { + conv_bgr888_to_bgra8888.convert(&target, source); + } + break; + + case VG_LITE_BGRA5658: { + conv_bgra5658_to_bgra8888.convert(&target, source); + } + break; + + case VG_LITE_BGR565: { + conv_bgr565_to_bgra8888.convert(&target, source); + } + break; + +#if LV_VG_LITE_THORVG_YUV_SUPPORT + case VG_LITE_NV12: { + libyuv::NV12ToARGB((const uint8_t *)source->memory, source->stride, (const uint8_t *)source->yuv.uv_memory, + source->yuv.uv_stride, + (uint8_t *)image_buffer, source->width * sizeof(vg_lite_uint32_t), width, height); + } + break; +#endif + + case VG_LITE_BGRA8888: { + memcpy(image_buffer, source->memory, px_size * sizeof(vg_color32_t)); + } + break; + + default: + LV_LOG_ERROR("unsupported format: %d", source->format); + LV_ASSERT(false); + break; + } + + /* multiply color */ + if(source->image_mode == VG_LITE_MULTIPLY_IMAGE_MODE && !VG_LITE_IS_ALPHA_FORMAT(source->format)) { + vg_color32_t * dest = (vg_color32_t *)image_buffer; + while(px_size--) { + dest->alpha = UDIV255(dest->alpha * A(color)); + dest->red = UDIV255(dest->red * B(color)); + dest->green = UDIV255(dest->green * G(color)); + dest->blue = UDIV255(dest->blue * R(color)); + dest++; + } + } + } + + TVG_CHECK_RETURN_RESULT(picture->load((uint32_t *)image_buffer, source->width, source->height, true)); + + return Result::Success; +} + +static void ClampColor(FLOATVECTOR4 Source, FLOATVECTOR4 Target, uint8_t Premultiplied) +{ + vg_lite_float_t colorMax; + /* Clamp the alpha channel. */ + Target[3] = CLAMP(Source[3], 0.0f, 1.0f); + + /* Determine the maximum value for the color channels. */ + colorMax = Premultiplied ? Target[3] : 1.0f; + + /* Clamp the color channels. */ + Target[0] = CLAMP(Source[0], 0.0f, colorMax); + Target[1] = CLAMP(Source[1], 0.0f, colorMax); + Target[2] = CLAMP(Source[2], 0.0f, colorMax); +} + +static uint8_t PackColorComponent(vg_lite_float_t value) +{ + /* Compute the rounded normalized value. */ + vg_lite_float_t rounded = value * 255.0f + 0.5f; + + /* Get the integer part. */ + int32_t roundedInt = (int32_t)rounded; + + /* Clamp to 0..1 range. */ + uint8_t clamped = (uint8_t)CLAMP(roundedInt, 0, 255); + + /* Return result. */ + return clamped; +} + +/* Get the bpp information of a color format. */ +static void get_format_bytes(vg_lite_buffer_format_t format, + vg_lite_uint32_t * mul, + vg_lite_uint32_t * div, + vg_lite_uint32_t * bytes_align) +{ + *mul = *div = 1; + *bytes_align = 4; + switch(format) { + case VG_LITE_L8: + case VG_LITE_A8: + case VG_LITE_RGBA8888_ETC2_EAC: + break; + + case VG_LITE_A4: + *div = 2; + break; + + case VG_LITE_ABGR1555: + case VG_LITE_ARGB1555: + case VG_LITE_BGRA5551: + case VG_LITE_RGBA5551: + case VG_LITE_RGBA4444: + case VG_LITE_BGRA4444: + case VG_LITE_ABGR4444: + case VG_LITE_ARGB4444: + case VG_LITE_RGB565: + case VG_LITE_BGR565: + case VG_LITE_YUYV: + case VG_LITE_YUY2: + case VG_LITE_YUY2_TILED: + /* AYUY2 buffer memory = YUY2 + alpha. */ + case VG_LITE_AYUY2: + case VG_LITE_AYUY2_TILED: + /* ABGR8565_PLANAR buffer memory = RGB565 + alpha. */ + case VG_LITE_ABGR8565_PLANAR: + case VG_LITE_ARGB8565_PLANAR: + case VG_LITE_RGBA5658_PLANAR: + case VG_LITE_BGRA5658_PLANAR: + *mul = 2; + break; + + case VG_LITE_RGBA8888: + case VG_LITE_BGRA8888: + case VG_LITE_ABGR8888: + case VG_LITE_ARGB8888: + case VG_LITE_RGBX8888: + case VG_LITE_BGRX8888: + case VG_LITE_XBGR8888: + case VG_LITE_XRGB8888: + *mul = 4; + break; + + case VG_LITE_NV12: + case VG_LITE_NV12_TILED: + *mul = 3; + break; + + case VG_LITE_ANV12: + case VG_LITE_ANV12_TILED: + *mul = 4; + break; + + case VG_LITE_INDEX_1: + *div = 8; + *bytes_align = 8; + break; + + case VG_LITE_INDEX_2: + *div = 4; + *bytes_align = 8; + break; + + case VG_LITE_INDEX_4: + *div = 2; + *bytes_align = 8; + break; + + case VG_LITE_INDEX_8: + *bytes_align = 1; + break; + + case VG_LITE_RGBA2222: + case VG_LITE_BGRA2222: + case VG_LITE_ABGR2222: + case VG_LITE_ARGB2222: + *mul = 1; + break; + + case VG_LITE_RGB888: + case VG_LITE_BGR888: + case VG_LITE_ABGR8565: + case VG_LITE_BGRA5658: + case VG_LITE_ARGB8565: + case VG_LITE_RGBA5658: + *mul = 3; + break; + + /* OpenVG format*/ + case VG_sRGBX_8888: + case VG_sRGBA_8888: + case VG_sRGBA_8888_PRE: + case VG_lRGBX_8888: + case VG_lRGBA_8888: + case VG_lRGBA_8888_PRE: + case VG_sXRGB_8888: + case VG_sARGB_8888: + case VG_sARGB_8888_PRE: + case VG_lXRGB_8888: + case VG_lARGB_8888: + case VG_lARGB_8888_PRE: + case VG_sBGRX_8888: + case VG_sBGRA_8888: + case VG_sBGRA_8888_PRE: + case VG_lBGRX_8888: + case VG_lBGRA_8888: + case VG_sXBGR_8888: + case VG_sABGR_8888: + case VG_lBGRA_8888_PRE: + case VG_sABGR_8888_PRE: + case VG_lXBGR_8888: + case VG_lABGR_8888: + case VG_lABGR_8888_PRE: + *mul = 4; + break; + + case VG_sRGBA_5551: + case VG_sRGBA_4444: + case VG_sARGB_1555: + case VG_sARGB_4444: + case VG_sBGRA_5551: + case VG_sBGRA_4444: + case VG_sABGR_1555: + case VG_sABGR_4444: + case VG_sRGB_565: + case VG_sBGR_565: + *mul = 2; + break; + + case VG_sL_8: + case VG_lL_8: + case VG_A_8: + break; + + case VG_BW_1: + case VG_A_4: + case VG_A_1: + *div = 2; + break; + + default: + break; + } +} + +static vg_lite_fpoint_t matrix_transform_point(const vg_lite_matrix_t * matrix, const vg_lite_fpoint_t * point) +{ + vg_lite_fpoint_t p; + p.x = (vg_lite_float_t)(point->x * matrix->m[0][0] + point->y * matrix->m[0][1] + matrix->m[0][2]); + p.y = (vg_lite_float_t)(point->x * matrix->m[1][0] + point->y * matrix->m[1][1] + matrix->m[1][2]); + return p; +} + +static bool vg_lite_matrix_inverse(vg_lite_matrix_t * result, const vg_lite_matrix_t * matrix) +{ + vg_lite_float_t det00, det01, det02; + vg_lite_float_t d; + bool is_affine; + + /* Test for identity matrix. */ + if(matrix == NULL) { + result->m[0][0] = 1.0f; + result->m[0][1] = 0.0f; + result->m[0][2] = 0.0f; + result->m[1][0] = 0.0f; + result->m[1][1] = 1.0f; + result->m[1][2] = 0.0f; + result->m[2][0] = 0.0f; + result->m[2][1] = 0.0f; + result->m[2][2] = 1.0f; + + /* Success. */ + return true; + } + + det00 = (matrix->m[1][1] * matrix->m[2][2]) - (matrix->m[2][1] * matrix->m[1][2]); + det01 = (matrix->m[2][0] * matrix->m[1][2]) - (matrix->m[1][0] * matrix->m[2][2]); + det02 = (matrix->m[1][0] * matrix->m[2][1]) - (matrix->m[2][0] * matrix->m[1][1]); + + /* Compute determinant. */ + d = (matrix->m[0][0] * det00) + (matrix->m[0][1] * det01) + (matrix->m[0][2] * det02); + + /* Return 0 if there is no inverse matrix. */ + if(d == 0.0f) + return false; + + /* Compute reciprocal. */ + d = 1.0f / d; + + /* Determine if the matrix is affine. */ + is_affine = (matrix->m[2][0] == 0.0f) && (matrix->m[2][1] == 0.0f) && (matrix->m[2][2] == 1.0f); + + result->m[0][0] = d * det00; + result->m[0][1] = d * ((matrix->m[2][1] * matrix->m[0][2]) - (matrix->m[0][1] * matrix->m[2][2])); + result->m[0][2] = d * ((matrix->m[0][1] * matrix->m[1][2]) - (matrix->m[1][1] * matrix->m[0][2])); + result->m[1][0] = d * det01; + result->m[1][1] = d * ((matrix->m[0][0] * matrix->m[2][2]) - (matrix->m[2][0] * matrix->m[0][2])); + result->m[1][2] = d * ((matrix->m[1][0] * matrix->m[0][2]) - (matrix->m[0][0] * matrix->m[1][2])); + result->m[2][0] = is_affine ? 0.0f : d * det02; + result->m[2][1] = is_affine ? 0.0f : d * ((matrix->m[2][0] * matrix->m[0][1]) - (matrix->m[0][0] * matrix->m[2][1])); + result->m[2][2] = is_affine ? 1.0f : d * ((matrix->m[0][0] * matrix->m[1][1]) - (matrix->m[1][0] * matrix->m[0][1])); + + /* Success. */ + return true; +} + +static void vg_lite_matrix_multiply(vg_lite_matrix_t * matrix, const vg_lite_matrix_t * mult) +{ + vg_lite_matrix_t temp; + int row, column; + + /* Process all rows. */ + for(row = 0; row < 3; row++) { + /* Process all columns. */ + for(column = 0; column < 3; column++) { + /* Compute matrix entry. */ + temp.m[row][column] = (matrix->m[row][0] * mult->m[0][column]) + + (matrix->m[row][1] * mult->m[1][column]) + + (matrix->m[row][2] * mult->m[2][column]); + } + } + + /* Copy temporary matrix into result. */ + lv_memcpy(matrix->m, &temp.m, sizeof(temp.m)); +} + +#endif diff --git a/include/liblvgl/misc/lv_tlsf.h b/include/liblvgl/stdlib/builtin/lv_tlsf.h similarity index 94% rename from include/liblvgl/misc/lv_tlsf.h rename to include/liblvgl/stdlib/builtin/lv_tlsf.h index f12590b6..bfaf0843 100644 --- a/include/liblvgl/misc/lv_tlsf.h +++ b/include/liblvgl/stdlib/builtin/lv_tlsf.h @@ -1,5 +1,5 @@ -#include "../lv_conf_internal.h" -#if LV_MEM_CUSTOM == 0 +#include "../../lv_conf_internal.h" +#if LV_USE_STDLIB_MALLOC == LV_STDLIB_BUILTIN #ifndef LV_TLSF_H #define LV_TLSF_H @@ -41,7 +41,9 @@ ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include +#include "../../osal/lv_os.h" +#include "../../misc/lv_ll.h" +#include "../../misc/lv_types.h" #if defined(__cplusplus) extern "C" { @@ -92,4 +94,4 @@ int lv_tlsf_check_pool(lv_pool_t pool); #endif /*LV_TLSF_H*/ -#endif /* LV_MEM_CUSTOM == 0 */ +#endif /*LV_STDLIB_BUILTIN*/ diff --git a/include/liblvgl/stdlib/builtin/lv_tlsf_private.h b/include/liblvgl/stdlib/builtin/lv_tlsf_private.h new file mode 100644 index 00000000..036e9d73 --- /dev/null +++ b/include/liblvgl/stdlib/builtin/lv_tlsf_private.h @@ -0,0 +1,53 @@ +/** + * @file lv_tlsf_private.h + * + */ + +#ifndef LV_TLSF_PRIVATE_H +#define LV_TLSF_PRIVATE_H + +#if LV_USE_STDLIB_MALLOC == LV_STDLIB_BUILTIN + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_tlsf.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef struct { +#if LV_USE_OS + lv_mutex_t mutex; +#endif + lv_tlsf_t tlsf; + size_t cur_used; + size_t max_used; + lv_ll_t pool_ll; +} lv_tlsf_state_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_USE_STDLIB_MALLOC == LV_STDLIB_BUILTIN*/ + +#endif /*LV_TLSF_PRIVATE_H*/ diff --git a/include/liblvgl/stdlib/lv_mem.h b/include/liblvgl/stdlib/lv_mem.h new file mode 100644 index 00000000..e136aad6 --- /dev/null +++ b/include/liblvgl/stdlib/lv_mem.h @@ -0,0 +1,156 @@ +/** + * @file lv_mem.h + * + */ + +#ifndef LV_MEM_H +#define LV_MEM_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../lv_conf_internal.h" + +#include "lv_string.h" + +#include "../misc/lv_types.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef void * lv_mem_pool_t; + +/** + * Heap information structure. + */ +typedef struct { + size_t total_size; /**< Total heap size */ + size_t free_cnt; + size_t free_size; /**< Size of available memory */ + size_t free_biggest_size; + size_t used_cnt; + size_t max_used; /**< Max size of Heap memory used */ + uint8_t used_pct; /**< Percentage used */ + uint8_t frag_pct; /**< Amount of fragmentation */ +} lv_mem_monitor_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Initialize to use malloc/free/realloc etc + */ +void lv_mem_init(void); + +/** + * Drop all dynamically allocated memory and reset the memory pools' state + */ +void lv_mem_deinit(void); + +lv_mem_pool_t lv_mem_add_pool(void * mem, size_t bytes); + +void lv_mem_remove_pool(lv_mem_pool_t pool); + +/** + * Allocate memory dynamically + * @param size requested size in bytes + * @return pointer to allocated uninitialized memory, or NULL on failure + */ +void * lv_malloc(size_t size); + +/** + * Allocate a block of zeroed memory dynamically + * @param num requested number of element to be allocated. + * @param size requested size of each element in bytes. + * @return pointer to allocated zeroed memory, or NULL on failure + */ +void * lv_calloc(size_t num, size_t size); + +/** + * Allocate zeroed memory dynamically + * @param size requested size in bytes + * @return pointer to allocated zeroed memory, or NULL on failure + */ +void * lv_zalloc(size_t size); + +/** + * Allocate zeroed memory dynamically + * @param size requested size in bytes + * @return pointer to allocated zeroed memory, or NULL on failure + */ +void * lv_malloc_zeroed(size_t size); + +/** + * Free an allocated data + * @param data pointer to an allocated memory + */ +void lv_free(void * data); + +/** + * Reallocate a memory with a new size. The old content will be kept. + * @param data_p pointer to an allocated memory. + * Its content will be copied to the new memory block and freed + * @param new_size the desired new size in byte + * @return pointer to the new memory, NULL on failure + */ +void * lv_realloc(void * data_p, size_t new_size); + +/** + * Used internally to execute a plain `malloc` operation + * @param size size in bytes to `malloc` + */ +void * lv_malloc_core(size_t size); + +/** + * Used internally to execute a plain `free` operation + * @param p memory address to free + */ +void lv_free_core(void * p); + +/** + * Used internally to execute a plain realloc operation + * @param p memory address to realloc + * @param new_size size in bytes to realloc + */ +void * lv_realloc_core(void * p, size_t new_size); + +/** + * Used internally by lv_mem_monitor() to gather LVGL heap state information. + * @param mon_p pointer to lv_mem_monitor_t object to be populated. + */ +void lv_mem_monitor_core(lv_mem_monitor_t * mon_p); + +lv_result_t lv_mem_test_core(void); + +/** + * @brief Tests the memory allocation system by allocating and freeing a block of memory. + * @return LV_RESULT_OK if the memory allocation system is working properly, or LV_RESULT_INVALID if there is an error. + */ +lv_result_t lv_mem_test(void); + +/** + * Give information about the work memory of dynamic allocation + * @param mon_p pointer to a lv_mem_monitor_t variable, + * the result of the analysis will be stored here + */ +void lv_mem_monitor(lv_mem_monitor_t * mon_p); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_MEM_H*/ diff --git a/include/liblvgl/stdlib/lv_mem_private.h b/include/liblvgl/stdlib/lv_mem_private.h new file mode 100644 index 00000000..9e70ae1a --- /dev/null +++ b/include/liblvgl/stdlib/lv_mem_private.h @@ -0,0 +1,39 @@ +/** + * @file lv_mem_private.h + * + */ + +#ifndef LV_MEM_PRIVATE_H +#define LV_MEM_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_mem.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_MEM_PRIVATE_H*/ diff --git a/include/liblvgl/stdlib/lv_sprintf.h b/include/liblvgl/stdlib/lv_sprintf.h new file mode 100644 index 00000000..a7f2188d --- /dev/null +++ b/include/liblvgl/stdlib/lv_sprintf.h @@ -0,0 +1,45 @@ +/** + * @file lv_sprintf.h + * + */ + +#ifndef LV_SPRINTF_H +#define LV_SPRINTF_H + +#if defined(__has_include) + #if __has_include(LV_INTTYPES_INCLUDE) + #include LV_INTTYPES_INCLUDE + /* platform-specific printf format for int32_t, usually "d" or "ld" */ + #define LV_PRId32 PRId32 + #define LV_PRIu32 PRIu32 + #define LV_PRIx32 PRIx32 + #define LV_PRIX32 PRIX32 + #else + #define LV_PRId32 "d" + #define LV_PRIu32 "u" + #define LV_PRIx32 "x" + #define LV_PRIX32 "X" + #endif +#else + /* hope this is correct for ports without __has_include or without inttypes.h */ + #define LV_PRId32 "d" + #define LV_PRIu32 "u" + #define LV_PRIx32 "x" + #define LV_PRIX32 "X" +#endif + +#include "../misc/lv_types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int lv_snprintf(char * buffer, size_t count, const char * format, ...); + +int lv_vsnprintf(char * buffer, size_t count, const char * format, va_list va); + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /* LV_SPRINTF_H */ diff --git a/include/liblvgl/stdlib/lv_string.h b/include/liblvgl/stdlib/lv_string.h new file mode 100644 index 00000000..ce73502e --- /dev/null +++ b/include/liblvgl/stdlib/lv_string.h @@ -0,0 +1,166 @@ +/** + * @file lv_string.h + * + */ + +#ifndef LV_STRING_H +#define LV_STRING_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../lv_conf_internal.h" +#include "../misc/lv_types.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * @brief Copies a block of memory from a source address to a destination address. + * @param dst Pointer to the destination array where the content is to be copied. + * @param src Pointer to the source of data to be copied. + * @param len Number of bytes to copy. + * @return Pointer to the destination array. + * @note The function does not check for any overlapping of the source and destination memory blocks. + */ +void * lv_memcpy(void * dst, const void * src, size_t len); + +/** + * @brief Fills a block of memory with a specified value. + * @param dst Pointer to the destination array to fill with the specified value. + * @param v Value to be set. The value is passed as an int, but the function fills + * the block of memory using the unsigned char conversion of this value. + * @param len Number of bytes to be set to the value. + */ +void lv_memset(void * dst, uint8_t v, size_t len); + +/** + * @brief Move a block of memory from source to destination + * @param dst Pointer to the destination array where the content is to be copied. + * @param src Pointer to the source of data to be copied. + * @param len Number of bytes to copy + * @return Pointer to the destination array. + */ +void * lv_memmove(void * dst, const void * src, size_t len); + +/** + * @brief This function will compare two memory blocks + * @param p1 Pointer to the first memory block + * @param p2 Pointer to the second memory block + * @param len Number of bytes to compare + * @return The difference between the value of the first unmatching byte. + */ +int lv_memcmp(const void * p1, const void * p2, size_t len); + +/** + * Same as `memset(dst, 0x00, len)`. + * @param dst pointer to the destination buffer + * @param len number of byte to set + */ +static inline void lv_memzero(void * dst, size_t len) +{ + lv_memset(dst, 0x00, len); +} + +/** + * @brief Computes the length of the string str up to, but not including the terminating null character. + * @param str Pointer to the null-terminated byte string to be examined. + * @return The length of the string in bytes. + */ +size_t lv_strlen(const char * str); + +/** + * @brief Copies up to dst_size-1 (non-null) characters from src to dst. A null terminator is always added. + * @param dst Pointer to the destination array where the content is to be copied. + * @param src Pointer to the source of data to be copied. + * @param dst_size Maximum number of characters to be copied to dst, including the null character. + * @return The length of src. The return value is equivalent to the value returned by lv_strlen(src) + */ +size_t lv_strlcpy(char * dst, const char * src, size_t dst_size); + +/** + * @brief Copies up to dest_size characters from the string pointed to by src to the character array pointed to by dst + * and fills the remaining length with null bytes. + * @param dst Pointer to the destination array where the content is to be copied. + * @param src Pointer to the source of data to be copied. + * @param dest_size Maximum number of characters to be copied to dst. + * @return A pointer to the destination array, which is dst. + * @note dst will not be null terminated if dest_size bytes were copied from src before the end of src was reached. + */ +char * lv_strncpy(char * dst, const char * src, size_t dest_size); + +/** + * @brief Copies the string pointed to by src, including the terminating null character, + * to the character array pointed to by dst. + * @param dst Pointer to the destination array where the content is to be copied. + * @param src Pointer to the source of data to be copied. + * @return A pointer to the destination array, which is dst. + */ +char * lv_strcpy(char * dst, const char * src); + +/** + * @brief This function will compare two strings without specified length. + * @param s1 pointer to the first string + * @param s2 pointer to the second string + * @return the difference between the value of the first unmatching character. + */ +int lv_strcmp(const char * s1, const char * s2); + +/** + * @brief Duplicate a string by allocating a new one and copying the content. + * @param src Pointer to the source of data to be copied. + * @return A pointer to the new allocated string. NULL if failed. + */ +char * lv_strdup(const char * src); + +/** + * @brief Copies the string pointed to by src, including the terminating null character, + * to the end of the string pointed to by dst. + * @param dst Pointer to the destination string where the content is to be appended. + * @param src Pointer to the source of data to be copied. + * @return A pointer to the destination string, which is dst. + */ +char * lv_strcat(char * dst, const char * src); + +/** + * @brief Copies up to src_len characters from the string pointed to by src + * to the end of the string pointed to by dst. + * A terminating null character is appended to dst even if no null character + * was encountered in src after src_len characters were copied. + * @param dst Pointer to the destination string where the content is to be appended. + * @param src Pointer to the source of data to be copied. + * @param src_len Maximum number of characters from src to be copied to the end of dst. + * @return A pointer to the destination string, which is dst. + */ +char * lv_strncat(char * dst, const char * src, size_t src_len); + +/** + * @brief Searches for the first occurrence of character c in the string str. + * @param str Pointer to the null-terminated byte string to be searched. + * @param c The character to be searched for. + * @return A pointer to the first occurrence of character c in the string str, or a null pointer if c is not found. + */ +char * lv_strchr(const char * str, int c); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_STRING_H*/ diff --git a/include/liblvgl/extra/themes/default/lv_theme_default.h b/include/liblvgl/themes/default/lv_theme_default.h similarity index 81% rename from include/liblvgl/extra/themes/default/lv_theme_default.h rename to include/liblvgl/themes/default/lv_theme_default.h index 7fab5be4..72d9acbf 100644 --- a/include/liblvgl/extra/themes/default/lv_theme_default.h +++ b/include/liblvgl/themes/default/lv_theme_default.h @@ -13,7 +13,7 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/core/lv_obj.h" +#include "../lv_theme.h" #if LV_USE_THEME_DEFAULT @@ -31,12 +31,14 @@ extern "C" { /** * Initialize the theme + * @param disp pointer to display * @param color_primary the primary color of the theme * @param color_secondary the secondary color for the theme + * @param dark * @param font pointer to a font to use. * @return a pointer to reference this theme later */ -lv_theme_t * lv_theme_default_init(lv_disp_t * disp, lv_color_t color_primary, lv_color_t color_secondary, bool dark, +lv_theme_t * lv_theme_default_init(lv_display_t * disp, lv_color_t color_primary, lv_color_t color_secondary, bool dark, const lv_font_t * font); /** @@ -51,6 +53,11 @@ lv_theme_t * lv_theme_default_get(void); */ bool lv_theme_default_is_inited(void); +/** + * Deinitialize the default theme + */ +void lv_theme_default_deinit(void); + /********************** * MACROS **********************/ diff --git a/include/liblvgl/core/lv_theme.h b/include/liblvgl/themes/lv_theme.h similarity index 79% rename from include/liblvgl/core/lv_theme.h rename to include/liblvgl/themes/lv_theme.h index 80da522c..21cb09b1 100644 --- a/include/liblvgl/core/lv_theme.h +++ b/include/liblvgl/themes/lv_theme.h @@ -13,7 +13,7 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/core/lv_obj.h" +#include "../core/lv_obj.h" /********************* * DEFINES @@ -23,23 +23,7 @@ extern "C" { * TYPEDEFS **********************/ -struct _lv_theme_t; -struct _lv_disp_t; - -typedef void (*lv_theme_apply_cb_t)(struct _lv_theme_t *, lv_obj_t *); - -typedef struct _lv_theme_t { - lv_theme_apply_cb_t apply_cb; - struct _lv_theme_t * parent; /**< Apply the current theme's style on top of this theme.*/ - void * user_data; - struct _lv_disp_t * disp; - lv_color_t color_primary; - lv_color_t color_secondary; - const lv_font_t * font_small; - const lv_font_t * font_normal; - const lv_font_t * font_large; - uint32_t flags; /*Any custom flag used by the theme*/ -} lv_theme_t; +typedef void (*lv_theme_apply_cb_t)(lv_theme_t *, lv_obj_t *); /********************** * GLOBAL PROTOTYPES @@ -113,6 +97,10 @@ lv_color_t lv_theme_get_color_secondary(lv_obj_t * obj); * MACROS **********************/ +#include "default/lv_theme_default.h" +#include "mono/lv_theme_mono.h" +#include "simple/lv_theme_simple.h" + #ifdef __cplusplus } /*extern "C"*/ #endif diff --git a/include/liblvgl/themes/lv_theme_private.h b/include/liblvgl/themes/lv_theme_private.h new file mode 100644 index 00000000..f7bb24cd --- /dev/null +++ b/include/liblvgl/themes/lv_theme_private.h @@ -0,0 +1,53 @@ +/** + * @file lv_theme_private.h + * + */ + +#ifndef LV_THEME_PRIVATE_H +#define LV_THEME_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_theme.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_theme_t { + lv_theme_apply_cb_t apply_cb; + lv_theme_t * parent; /**< Apply the current theme's style on top of this theme. */ + void * user_data; + lv_display_t * disp; + lv_color_t color_primary; + lv_color_t color_secondary; + const lv_font_t * font_small; + const lv_font_t * font_normal; + const lv_font_t * font_large; + uint32_t flags; /**< Any custom flag used by the theme */ +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_THEME_PRIVATE_H*/ diff --git a/include/liblvgl/extra/themes/mono/lv_theme_mono.h b/include/liblvgl/themes/mono/lv_theme_mono.h similarity index 69% rename from include/liblvgl/extra/themes/mono/lv_theme_mono.h rename to include/liblvgl/themes/mono/lv_theme_mono.h index 3d907fff..30ac4c48 100644 --- a/include/liblvgl/extra/themes/mono/lv_theme_mono.h +++ b/include/liblvgl/themes/mono/lv_theme_mono.h @@ -3,8 +3,8 @@ * */ -#ifndef LV_USE_THEME_MONO_H -#define LV_USE_THEME_MONO_H +#ifndef LV_THEME_MONO_H +#define LV_THEME_MONO_H #ifdef __cplusplus extern "C" { @@ -13,7 +13,7 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/core/lv_obj.h" +#include "../lv_theme.h" #if LV_USE_THEME_MONO @@ -31,12 +31,12 @@ extern "C" { /** * Initialize the theme - * @param color_primary the primary color of the theme - * @param color_secondary the secondary color for the theme + * @param disp pointer to display + * @param dark_bg * @param font pointer to a font to use. * @return a pointer to reference this theme later */ -lv_theme_t * lv_theme_mono_init(lv_disp_t * disp, bool dark_bg, const lv_font_t * font); +lv_theme_t * lv_theme_mono_init(lv_display_t * disp, bool dark_bg, const lv_font_t * font); /** * Check if the theme is initialized @@ -44,6 +44,11 @@ lv_theme_t * lv_theme_mono_init(lv_disp_t * disp, bool dark_bg, const lv_font_t */ bool lv_theme_mono_is_inited(void); +/** + * Deinitialize the mono theme + */ +void lv_theme_mono_deinit(void); + /********************** * MACROS **********************/ @@ -54,4 +59,4 @@ bool lv_theme_mono_is_inited(void); } /*extern "C"*/ #endif -#endif /*LV_USE_THEME_MONO_H*/ +#endif /* LV_THEME_MONO_H */ diff --git a/include/liblvgl/extra/themes/basic/lv_theme_basic.h b/include/liblvgl/themes/simple/lv_theme_simple.h similarity index 57% rename from include/liblvgl/extra/themes/basic/lv_theme_basic.h rename to include/liblvgl/themes/simple/lv_theme_simple.h index 06d6828b..67173665 100644 --- a/include/liblvgl/extra/themes/basic/lv_theme_basic.h +++ b/include/liblvgl/themes/simple/lv_theme_simple.h @@ -1,10 +1,10 @@ /** - * @file lv_theme_basic.h + * @file lv_theme_simple.h * */ -#ifndef LV_THEME_BASIC_H -#define LV_THEME_BASIC_H +#ifndef LV_THEME_SIMPLE_H +#define LV_THEME_SIMPLE_H #ifdef __cplusplus extern "C" { @@ -13,9 +13,10 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/core/lv_obj.h" +#include "../lv_theme.h" +#include "../../display/lv_display.h" -#if LV_USE_THEME_BASIC +#if LV_USE_THEME_SIMPLE /********************* * DEFINES @@ -34,13 +35,24 @@ extern "C" { * @param disp pointer to display to attach the theme * @return a pointer to reference this theme later */ -lv_theme_t * lv_theme_basic_init(lv_disp_t * disp); +lv_theme_t * lv_theme_simple_init(lv_display_t * disp); /** * Check if the theme is initialized * @return true if default theme is initialized, false otherwise */ -bool lv_theme_basic_is_inited(void); +bool lv_theme_simple_is_inited(void); + +/** + * Get simple theme + * @return a pointer to simple theme, or NULL if this is not initialized + */ +lv_theme_t * lv_theme_simple_get(void); + +/** + * Deinitialize the simple theme + */ +void lv_theme_simple_deinit(void); /********************** * MACROS @@ -52,4 +64,4 @@ bool lv_theme_basic_is_inited(void); } /*extern "C"*/ #endif -#endif /*LV_THEME_BASIC_H*/ +#endif /*LV_THEME_SIMPLE_H*/ diff --git a/include/liblvgl/tick/lv_tick.h b/include/liblvgl/tick/lv_tick.h new file mode 100644 index 00000000..048b93a3 --- /dev/null +++ b/include/liblvgl/tick/lv_tick.h @@ -0,0 +1,85 @@ +/** + * @file lv_tick.h + * Provide access to the system tick with 1 millisecond resolution + */ + +#ifndef LV_TICK_H +#define LV_TICK_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../lv_conf_internal.h" + +#include "../misc/lv_types.h" + +/********************* + * DEFINES + *********************/ +#ifndef LV_ATTRIBUTE_TICK_INC +#define LV_ATTRIBUTE_TICK_INC +#endif + +/********************** + * TYPEDEFS + **********************/ +typedef uint32_t (*lv_tick_get_cb_t)(void); + +typedef void (*lv_delay_cb_t)(uint32_t ms); + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * You have to call this function periodically + * @param tick_period the call period of this function in milliseconds + */ +LV_ATTRIBUTE_TICK_INC void lv_tick_inc(uint32_t tick_period); + +/** + * Get the elapsed milliseconds since start up + * @return the elapsed milliseconds + */ +uint32_t lv_tick_get(void); + +/** + * Get the elapsed milliseconds since a previous time stamp + * @param prev_tick a previous time stamp (return value of lv_tick_get() ) + * @return the elapsed milliseconds since 'prev_tick' + */ +uint32_t lv_tick_elaps(uint32_t prev_tick); + +/** + * Delay for the given milliseconds. + * By default it's a blocking delay, but with `lv_delay_set_cb()` + * a custom delay function can be set too + * @param ms the number of milliseconds to delay + */ +void lv_delay_ms(uint32_t ms); + +/** + * Set the custom callback for 'lv_tick_get' + * @param cb call this callback on 'lv_tick_get' + */ +void lv_tick_set_cb(lv_tick_get_cb_t cb); + +/** + * Set a custom callback for 'lv_delay_ms' + * @param cb call this callback in 'lv_delay_ms' + */ +void lv_delay_set_cb(lv_delay_cb_t cb); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_TICK_H*/ diff --git a/include/liblvgl/tick/lv_tick_private.h b/include/liblvgl/tick/lv_tick_private.h new file mode 100644 index 00000000..cf8ae9b0 --- /dev/null +++ b/include/liblvgl/tick/lv_tick_private.h @@ -0,0 +1,46 @@ +/** + * @file lv_tick_private.h + * + */ + +#ifndef LV_TICK_PRIVATE_H +#define LV_TICK_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_tick.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef struct { + uint32_t sys_time; + volatile uint8_t sys_irq_flag; + lv_tick_get_cb_t tick_get_cb; + lv_delay_cb_t delay_cb; +} lv_tick_state_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_TICK_PRIVATE_H*/ diff --git a/include/liblvgl/widgets/animimage/lv_animimage.h b/include/liblvgl/widgets/animimage/lv_animimage.h new file mode 100644 index 00000000..a2f1926c --- /dev/null +++ b/include/liblvgl/widgets/animimage/lv_animimage.h @@ -0,0 +1,129 @@ +/** + * @file lv_animimage.h + * + */ + +#ifndef LV_ANIMIMAGE_H +#define LV_ANIMIMAGE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../image/lv_image.h" +#include "../../misc/lv_types.h" + +#if LV_USE_ANIMIMG != 0 + +/*Testing of dependencies*/ +#if LV_USE_IMAGE == 0 +#error "lv_animimg: lv_img is required. Enable it in lv_conf.h (LV_USE_IMAGE 1)" +#endif + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_animimg_class; + +/** Image parts */ +typedef enum { + LV_ANIM_IMAGE_PART_MAIN, +} lv_animimg_part_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create an animation image objects + * @param parent pointer to an object, it will be the parent of the new button + * @return pointer to the created animation image object + */ +lv_obj_t * lv_animimg_create(lv_obj_t * parent); + +/*===================== + * Setter functions + *====================*/ + +/** + * Set the image animation images source. + * @param img pointer to an animation image object + * @param dsc pointer to a series images + * @param num images' number + */ +void lv_animimg_set_src(lv_obj_t * img, const void * dsc[], size_t num); + +/** + * Startup the image animation. + * @param obj pointer to an animation image object + */ +void lv_animimg_start(lv_obj_t * obj); + +/** + * Set the image animation duration time. unit:ms + * @param img pointer to an animation image object + * @param duration the duration in milliseconds + */ +void lv_animimg_set_duration(lv_obj_t * img, uint32_t duration); + +/** + * Set the image animation repeatedly play times. + * @param img pointer to an animation image object + * @param count the number of times to repeat the animation + */ +void lv_animimg_set_repeat_count(lv_obj_t * img, uint32_t count); + +/*===================== + * Getter functions + *====================*/ + +/** + * Get the image animation images source. + * @param img pointer to an animation image object + * @return a pointer that will point to a series images + */ +const void ** lv_animimg_get_src(lv_obj_t * img); + +/** + * Get the image animation images source. + * @param img pointer to an animation image object + * @return the number of source images + */ +uint8_t lv_animimg_get_src_count(lv_obj_t * img); + +/** + * Get the image animation duration time. unit:ms + * @param img pointer to an animation image object + * @return the animation duration time + */ +uint32_t lv_animimg_get_duration(lv_obj_t * img); + +/** + * Get the image animation repeat play times. + * @param img pointer to an animation image object + * @return the repeat count + */ +uint32_t lv_animimg_get_repeat_count(lv_obj_t * img); + +/** + * Get the image animation underlying animation. + * @param img pointer to an animation image object + * @return the animation reference + */ +lv_anim_t * lv_animimg_get_anim(lv_obj_t * img); + +#endif /*LV_USE_ANIMIMG*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /*LV_ANIMIMAGE_H*/ diff --git a/include/liblvgl/widgets/animimage/lv_animimage_private.h b/include/liblvgl/widgets/animimage/lv_animimage_private.h new file mode 100644 index 00000000..a5ef5ebb --- /dev/null +++ b/include/liblvgl/widgets/animimage/lv_animimage_private.h @@ -0,0 +1,55 @@ +/** + * @file lv_animimage_private.h + * + */ + +#ifndef LV_ANIMIMAGE_PRIVATE_H +#define LV_ANIMIMAGE_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../image/lv_image_private.h" +#include "../../misc/lv_anim_private.h" +#include "lv_animimage.h" + +#if LV_USE_ANIMIMG != 0 + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** Data of the animimage */ +struct _lv_animimg_t { + lv_image_t img; + lv_anim_t anim; + /* picture sequence */ + const void ** dsc; + int8_t pic_count; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_ANIMIMG != 0 */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_ANIMIMAGE_PRIVATE_H*/ diff --git a/include/liblvgl/widgets/arc/lv_arc.h b/include/liblvgl/widgets/arc/lv_arc.h new file mode 100644 index 00000000..f241745f --- /dev/null +++ b/include/liblvgl/widgets/arc/lv_arc.h @@ -0,0 +1,247 @@ +/** + * @file lv_arc.h + * + */ + +#ifndef LV_ARC_H +#define LV_ARC_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../lv_conf_internal.h" + +#if LV_USE_ARC != 0 + +#include "../../core/lv_obj.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ +typedef enum { + LV_ARC_MODE_NORMAL, + LV_ARC_MODE_SYMMETRICAL, + LV_ARC_MODE_REVERSE +} lv_arc_mode_t; + +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_arc_class; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create an arc object + * @param parent pointer to an object, it will be the parent of the new arc + * @return pointer to the created arc + */ +lv_obj_t * lv_arc_create(lv_obj_t * parent); + +/*====================== + * Add/remove functions + *=====================*/ + +/*===================== + * Setter functions + *====================*/ + +/** + * Set the start angle of an arc. 0 deg: right, 90 bottom, etc. + * @param obj pointer to an arc object + * @param start the start angle. (if `LV_USE_FLOAT` is enabled it can be fractional too.) + */ +void lv_arc_set_start_angle(lv_obj_t * obj, lv_value_precise_t start); + +/** + * Set the end angle of an arc. 0 deg: right, 90 bottom, etc. + * @param obj pointer to an arc object + * @param end the end angle (if `LV_USE_FLOAT` is enabled it can be fractional too.) + */ +void lv_arc_set_end_angle(lv_obj_t * obj, lv_value_precise_t end); + +/** + * Set the start and end angles + * @param obj pointer to an arc object + * @param start the start angle (if `LV_USE_FLOAT` is enabled it can be fractional too.) + * @param end the end angle (if `LV_USE_FLOAT` is enabled it can be fractional too.) + */ +void lv_arc_set_angles(lv_obj_t * obj, lv_value_precise_t start, lv_value_precise_t end); + +/** + * Set the start angle of an arc background. 0 deg: right, 90 bottom, etc. + * @param obj pointer to an arc object + * @param start the start angle (if `LV_USE_FLOAT` is enabled it can be fractional too.) + */ +void lv_arc_set_bg_start_angle(lv_obj_t * obj, lv_value_precise_t start); + +/** + * Set the start angle of an arc background. 0 deg: right, 90 bottom etc. + * @param obj pointer to an arc object + * @param end the end angle (if `LV_USE_FLOAT` is enabled it can be fractional too.) + */ +void lv_arc_set_bg_end_angle(lv_obj_t * obj, lv_value_precise_t end); + +/** + * Set the start and end angles of the arc background + * @param obj pointer to an arc object + * @param start the start angle (if `LV_USE_FLOAT` is enabled it can be fractional too.) + * @param end the end angle (if `LV_USE_FLOAT` is enabled it can be fractional too.) + */ +void lv_arc_set_bg_angles(lv_obj_t * obj, lv_value_precise_t start, lv_value_precise_t end); + +/** + * Set the rotation for the whole arc + * @param obj pointer to an arc object + * @param rotation rotation angle + */ +void lv_arc_set_rotation(lv_obj_t * obj, int32_t rotation); + +/** + * Set the type of arc. + * @param obj pointer to arc object + * @param type arc's mode + */ +void lv_arc_set_mode(lv_obj_t * obj, lv_arc_mode_t type); + +/** + * Set a new value on the arc + * @param obj pointer to an arc object + * @param value new value + */ +void lv_arc_set_value(lv_obj_t * obj, int32_t value); + +/** + * Set minimum and the maximum values of an arc + * @param obj pointer to the arc object + * @param min minimum value + * @param max maximum value + */ +void lv_arc_set_range(lv_obj_t * obj, int32_t min, int32_t max); + +/** + * Set a change rate to limit the speed how fast the arc should reach the pressed point. + * @param obj pointer to an arc object + * @param rate the change rate + */ +void lv_arc_set_change_rate(lv_obj_t * obj, uint32_t rate); + +/** + * Set an offset angle for the knob + * @param obj pointer to an arc object + * @param offset knob offset from main arc in degrees + */ +void lv_arc_set_knob_offset(lv_obj_t * obj, int32_t offset); + +/*===================== + * Getter functions + *====================*/ + +/** + * Get the start angle of an arc. + * @param obj pointer to an arc object + * @return the start angle [0..360] (if `LV_USE_FLOAT` is enabled it can be fractional too.) + */ +lv_value_precise_t lv_arc_get_angle_start(lv_obj_t * obj); + +/** + * Get the end angle of an arc. + * @param obj pointer to an arc object + * @return the end angle [0..360] (if `LV_USE_FLOAT` is enabled it can be fractional too.) + */ +lv_value_precise_t lv_arc_get_angle_end(lv_obj_t * obj); + +/** + * Get the start angle of an arc background. + * @param obj pointer to an arc object + * @return the start angle [0..360] (if `LV_USE_FLOAT` is enabled it can be fractional too.) + */ +lv_value_precise_t lv_arc_get_bg_angle_start(lv_obj_t * obj); + +/** + * Get the end angle of an arc background. + * @param obj pointer to an arc object + * @return the end angle [0..360] (if `LV_USE_FLOAT` is enabled it can be fractional too.) + */ +lv_value_precise_t lv_arc_get_bg_angle_end(lv_obj_t * obj); + +/** + * Get the value of an arc + * @param obj pointer to an arc object + * @return the value of the arc + */ +int32_t lv_arc_get_value(const lv_obj_t * obj); + +/** + * Get the minimum value of an arc + * @param obj pointer to an arc object + * @return the minimum value of the arc + */ +int32_t lv_arc_get_min_value(const lv_obj_t * obj); + +/** + * Get the maximum value of an arc + * @param obj pointer to an arc object + * @return the maximum value of the arc + */ +int32_t lv_arc_get_max_value(const lv_obj_t * obj); + +/** + * Get whether the arc is type or not. + * @param obj pointer to an arc object + * @return arc's mode + */ +lv_arc_mode_t lv_arc_get_mode(const lv_obj_t * obj); + +/** + * Get the rotation for the whole arc + * @param obj pointer to an arc object + * @return arc's current rotation + */ +int32_t lv_arc_get_rotation(const lv_obj_t * obj); + +/** + * Get the current knob angle offset + * @param obj pointer to an arc object + * @return arc's current knob offset + */ +int32_t lv_arc_get_knob_offset(const lv_obj_t * obj); + +/*===================== + * Other functions + *====================*/ + +/** + * Align an object to the current position of the arc (knob) + * @param obj pointer to an arc object + * @param obj_to_align pointer to an object to align + * @param r_offset consider the radius larger with this value (< 0: for smaller radius) + */ +void lv_arc_align_obj_to_angle(const lv_obj_t * obj, lv_obj_t * obj_to_align, int32_t r_offset); + +/** + * Rotate an object to the current position of the arc (knob) + * @param obj pointer to an arc object + * @param obj_to_rotate pointer to an object to rotate + * @param r_offset consider the radius larger with this value (< 0: for smaller radius) + */ +void lv_arc_rotate_obj_to_angle(const lv_obj_t * obj, lv_obj_t * obj_to_rotate, int32_t r_offset); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_ARC*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_ARC_H*/ diff --git a/include/liblvgl/widgets/arc/lv_arc_private.h b/include/liblvgl/widgets/arc/lv_arc_private.h new file mode 100644 index 00000000..71f576a4 --- /dev/null +++ b/include/liblvgl/widgets/arc/lv_arc_private.h @@ -0,0 +1,64 @@ +/** + * @file lv_arc_private.h + * + */ + +#ifndef LV_ARC_PRIVATE_H +#define LV_ARC_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../core/lv_obj_private.h" +#include "lv_arc.h" + +#if LV_USE_ARC != 0 + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_arc_t { + lv_obj_t obj; + int32_t rotation; + lv_value_precise_t indic_angle_start; + lv_value_precise_t indic_angle_end; + lv_value_precise_t bg_angle_start; + lv_value_precise_t bg_angle_end; + int32_t value; /**< Current value of the arc */ + int32_t min_value; /**< Minimum value of the arc */ + int32_t max_value; /**< Maximum value of the arc */ + uint32_t dragging : 1; + uint32_t type : 2; + uint32_t min_close : 1; /**< 1: the last pressed angle was closer to minimum end */ + uint32_t in_out : 1; /**< 1: The click was within the background arc angles. 0: Click outside */ + uint32_t chg_rate; /**< Drag angle rate of change of the arc (degrees/sec) */ + uint32_t last_tick; /**< Last dragging event timestamp of the arc */ + lv_value_precise_t last_angle; /**< Last dragging angle of the arc */ + int16_t knob_offset; /**< knob offset from the main arc */ +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_ARC != 0 */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_ARC_PRIVATE_H*/ diff --git a/include/liblvgl/widgets/lv_bar.h b/include/liblvgl/widgets/bar/lv_bar.h similarity index 60% rename from include/liblvgl/widgets/lv_bar.h rename to include/liblvgl/widgets/bar/lv_bar.h index 55d17119..93c88786 100644 --- a/include/liblvgl/widgets/lv_bar.h +++ b/include/liblvgl/widgets/bar/lv_bar.h @@ -13,14 +13,13 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" +#include "../../lv_conf_internal.h" #if LV_USE_BAR != 0 -#include "liblvgl/core/lv_obj.h" -#include "liblvgl/misc/lv_anim.h" -#include "lv_btn.h" -#include "lv_label.h" +#include "../../core/lv_obj.h" +#include "../../misc/lv_anim.h" +#include "../label/lv_label.h" /********************* * DEFINES @@ -29,42 +28,19 @@ extern "C" { /********************** * TYPEDEFS **********************/ - -enum { +typedef enum { LV_BAR_MODE_NORMAL, LV_BAR_MODE_SYMMETRICAL, LV_BAR_MODE_RANGE -}; -typedef uint8_t lv_bar_mode_t; - -typedef struct { - lv_obj_t * bar; - int32_t anim_start; - int32_t anim_end; - int32_t anim_state; -} _lv_bar_anim_t; - -typedef struct { - lv_obj_t obj; - int32_t cur_value; /**< Current value of the bar*/ - int32_t min_value; /**< Minimum value of the bar*/ - int32_t max_value; /**< Maximum value of the bar*/ - int32_t start_value; /**< Start value of the bar*/ - lv_area_t indic_area; /**< Save the indicator area. Might be used by derived types*/ - _lv_bar_anim_t cur_value_anim; - _lv_bar_anim_t start_value_anim; - lv_bar_mode_t mode : 2; /**< Type of bar*/ -} lv_bar_t; - -extern const lv_obj_class_t lv_bar_class; +} lv_bar_mode_t; -/** - * `type` field in `lv_obj_draw_part_dsc_t` if `class_p = lv_bar_class` - * Used in `LV_EVENT_DRAW_PART_BEGIN` and `LV_EVENT_DRAW_PART_END` - */ typedef enum { - LV_BAR_DRAW_PART_INDICATOR, /**< The indicator*/ -} lv_bar_draw_part_type_t; + LV_BAR_ORIENTATION_AUTO, + LV_BAR_ORIENTATION_HORIZONTAL, + LV_BAR_ORIENTATION_VERTICAL +} lv_bar_orientation_t; + +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_bar_class; /********************** * GLOBAL PROTOTYPES @@ -72,8 +48,8 @@ typedef enum { /** * Create a bar object - * @param parent pointer to an object, it will be the parent of the new bar - * @return pointer to the created bar + * @param parent pointer to an object, it will be the parent of the new bar + * @return pointer to the created bar */ lv_obj_t * lv_bar_create(lv_obj_t * parent); @@ -83,17 +59,17 @@ lv_obj_t * lv_bar_create(lv_obj_t * parent); /** * Set a new value on the bar - * @param bar pointer to a bar object - * @param value new value - * @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately + * @param obj pointer to a bar object + * @param value new value + * @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately */ void lv_bar_set_value(lv_obj_t * obj, int32_t value, lv_anim_enable_t anim); /** * Set a new start value on the bar - * @param obj pointer to a bar object - * @param value new start value - * @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately + * @param obj pointer to a bar object + * @param start_value new start value + * @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately */ void lv_bar_set_start_value(lv_obj_t * obj, int32_t start_value, lv_anim_enable_t anim); @@ -102,6 +78,7 @@ void lv_bar_set_start_value(lv_obj_t * obj, int32_t start_value, lv_anim_enable_ * @param obj pointer to the bar object * @param min minimum value * @param max maximum value + * @note If min is greater than max, the drawing direction becomes to the opposite direction. */ void lv_bar_set_range(lv_obj_t * obj, int32_t min, int32_t max); @@ -112,6 +89,13 @@ void lv_bar_set_range(lv_obj_t * obj, int32_t min, int32_t max); */ void lv_bar_set_mode(lv_obj_t * obj, lv_bar_mode_t mode); +/** + * Set the orientation of bar. + * @param obj pointer to bar object + * @param orientation bar orientation from `lv_bar_orientation_t` + */ +void lv_bar_set_orientation(lv_obj_t * obj, lv_bar_orientation_t orientation); + /*===================== * Getter functions *====================*/ @@ -151,6 +135,20 @@ int32_t lv_bar_get_max_value(const lv_obj_t * obj); */ lv_bar_mode_t lv_bar_get_mode(lv_obj_t * obj); +/** + * Get the orientation of bar. + * @param obj pointer to bar object + * @return bar orientation from ::lv_bar_orientation_t + */ +lv_bar_orientation_t lv_bar_get_orientation(lv_obj_t * obj); + +/** + * Give the bar is in symmetrical mode or not + * @param obj pointer to bar object + * @return true: in symmetrical mode false : not in +*/ +bool lv_bar_is_symmetrical(lv_obj_t * obj); + /********************** * MACROS **********************/ diff --git a/include/liblvgl/widgets/bar/lv_bar_private.h b/include/liblvgl/widgets/bar/lv_bar_private.h new file mode 100644 index 00000000..c403805f --- /dev/null +++ b/include/liblvgl/widgets/bar/lv_bar_private.h @@ -0,0 +1,66 @@ +/** + * @file lv_bar_private.h + * + */ + +#ifndef LV_BAR_PRIVATE_H +#define LV_BAR_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../core/lv_obj_private.h" +#include "lv_bar.h" + +#if LV_USE_BAR != 0 + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_bar_anim_t { + lv_obj_t * bar; + int32_t anim_start; + int32_t anim_end; + int32_t anim_state; +}; + +struct _lv_bar_t { + lv_obj_t obj; + int32_t cur_value; /**< Current value of the bar*/ + int32_t min_value; /**< Minimum value of the bar*/ + int32_t max_value; /**< Maximum value of the bar*/ + int32_t start_value; /**< Start value of the bar*/ + lv_area_t indic_area; /**< Save the indicator area. Might be used by derived types*/ + bool val_reversed; /**< Whether value been reversed */ + lv_bar_anim_t cur_value_anim; + lv_bar_anim_t start_value_anim; + lv_bar_mode_t mode : 3; /**< Type of bar*/ + lv_bar_orientation_t orientation : 3; /**< Orientation of bar*/ +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_BAR != 0 */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_BAR_PRIVATE_H*/ diff --git a/include/liblvgl/widgets/lv_btn.h b/include/liblvgl/widgets/button/lv_button.h similarity index 57% rename from include/liblvgl/widgets/lv_btn.h rename to include/liblvgl/widgets/button/lv_button.h index 893e6a05..eb7a0a57 100644 --- a/include/liblvgl/widgets/lv_btn.h +++ b/include/liblvgl/widgets/button/lv_button.h @@ -1,10 +1,10 @@ /** - * @file lv_btn.h + * @file lv_button.h * */ -#ifndef LV_BTN_H -#define LV_BTN_H +#ifndef LV_BUTTON_H +#define LV_BUTTON_H #ifdef __cplusplus extern "C" { @@ -13,24 +13,16 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" +#include "../../lv_conf_internal.h" -#if LV_USE_BTN != 0 -#include "liblvgl/core/lv_obj.h" +#if LV_USE_BUTTON != 0 +#include "../../core/lv_obj.h" /********************* * DEFINES *********************/ -/********************** - * TYPEDEFS - **********************/ - -typedef struct { - lv_obj_t obj; -} lv_btn_t; - -extern const lv_obj_class_t lv_btn_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_button_class; /********************** * GLOBAL PROTOTYPES @@ -41,16 +33,16 @@ extern const lv_obj_class_t lv_btn_class; * @param parent pointer to an object, it will be the parent of the new button * @return pointer to the created button */ -lv_obj_t * lv_btn_create(lv_obj_t * parent); +lv_obj_t * lv_button_create(lv_obj_t * parent); /********************** * MACROS **********************/ -#endif /*LV_USE_BTN*/ +#endif /*LV_USE_BUTTON*/ #ifdef __cplusplus } /*extern "C"*/ #endif -#endif /*LV_BTN_H*/ +#endif /*LV_BUTTON_H*/ diff --git a/include/liblvgl/extra/libs/lv_libs.h b/include/liblvgl/widgets/button/lv_button_private.h similarity index 55% rename from include/liblvgl/extra/libs/lv_libs.h rename to include/liblvgl/widgets/button/lv_button_private.h index 6782b1d0..c89c93aa 100644 --- a/include/liblvgl/extra/libs/lv_libs.h +++ b/include/liblvgl/widgets/button/lv_button_private.h @@ -1,10 +1,10 @@ /** - * @file lv_libs.h + * @file lv_button_private.h * */ -#ifndef LV_LIBS_H -#define LV_LIBS_H +#ifndef LV_BUTTON_PRIVATE_H +#define LV_BUTTON_PRIVATE_H #ifdef __cplusplus extern "C" { @@ -13,15 +13,11 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "bmp/lv_bmp.h" -#include "fsdrv/lv_fsdrv.h" -#include "png/lv_png.h" -#include "gif/lv_gif.h" -#include "qrcode/lv_qrcode.h" -#include "sjpg/lv_sjpg.h" -#include "freetype/lv_freetype.h" -#include "rlottie/lv_rlottie.h" -#include "ffmpeg/lv_ffmpeg.h" + +#include "../../core/lv_obj_private.h" +#include "lv_button.h" + +#if LV_USE_BUTTON != 0 /********************* * DEFINES @@ -31,6 +27,15 @@ extern "C" { * TYPEDEFS **********************/ +/********************** + * TYPEDEFS + **********************/ + +struct _lv_button_t { + lv_obj_t obj; +}; + + /********************** * GLOBAL PROTOTYPES **********************/ @@ -39,8 +44,10 @@ extern "C" { * MACROS **********************/ +#endif /* LV_USE_BUTTON != 0 */ + #ifdef __cplusplus } /*extern "C"*/ #endif -#endif /*LV_LIBS_H*/ +#endif /*LV_BUTTON_PRIVATE_H*/ diff --git a/include/liblvgl/widgets/buttonmatrix/lv_buttonmatrix.h b/include/liblvgl/widgets/buttonmatrix/lv_buttonmatrix.h new file mode 100644 index 00000000..8333b564 --- /dev/null +++ b/include/liblvgl/widgets/buttonmatrix/lv_buttonmatrix.h @@ -0,0 +1,202 @@ +/** + * @file lv_buttonmatrix.h + * + */ + +#ifndef LV_BUTTONMATRIX_H +#define LV_BUTTONMATRIX_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../lv_conf_internal.h" + +#if LV_USE_BUTTONMATRIX != 0 + +#include "../../core/lv_obj.h" + +/********************* + * DEFINES + *********************/ +#define LV_BUTTONMATRIX_BUTTON_NONE 0xFFFF +LV_EXPORT_CONST_INT(LV_BUTTONMATRIX_BUTTON_NONE); + +/********************** + * TYPEDEFS + **********************/ + +/** Type to store button control bits (disabled, hidden etc.) + * The first 3 bits are used to store the width*/ +typedef enum { + LV_BUTTONMATRIX_CTRL_HIDDEN = 0x0010, /**< Button hidden*/ + LV_BUTTONMATRIX_CTRL_NO_REPEAT = 0x0020, /**< Do not repeat press this button.*/ + LV_BUTTONMATRIX_CTRL_DISABLED = 0x0040, /**< Disable this button.*/ + LV_BUTTONMATRIX_CTRL_CHECKABLE = 0x0080, /**< The button can be toggled.*/ + LV_BUTTONMATRIX_CTRL_CHECKED = 0x0100, /**< Button is currently toggled (e.g. checked).*/ + LV_BUTTONMATRIX_CTRL_CLICK_TRIG = 0x0200, /**< 1: Send LV_EVENT_VALUE_CHANGE on CLICK, 0: Send LV_EVENT_VALUE_CHANGE on PRESS*/ + LV_BUTTONMATRIX_CTRL_POPOVER = 0x0400, /**< Show a popover when pressing this key*/ + LV_BUTTONMATRIX_CTRL_RECOLOR = 0x0800, /**< Enable text recoloring with `#color`*/ + LV_BUTTONMATRIX_CTRL_RESERVED_2 = 0x1000, /**< Reserved for later use*/ + LV_BUTTONMATRIX_CTRL_RESERVED_3 = 0x2000, /**< Reserved for later use*/ + LV_BUTTONMATRIX_CTRL_CUSTOM_1 = 0x4000, /**< Custom free to use flag*/ + LV_BUTTONMATRIX_CTRL_CUSTOM_2 = 0x8000, /**< Custom free to use flag*/ +} lv_buttonmatrix_ctrl_t; + +typedef bool (*lv_buttonmatrix_button_draw_cb_t)(lv_obj_t * btnm, uint32_t btn_id, const lv_area_t * draw_area, + const lv_area_t * clip_area); + +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_buttonmatrix_class; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create a button matrix object + * @param parent pointer to an object, it will be the parent of the new button matrix + * @return pointer to the created button matrix + */ +lv_obj_t * lv_buttonmatrix_create(lv_obj_t * parent); + +/*===================== + * Setter functions + *====================*/ + +/** + * Set a new map. Buttons will be created/deleted according to the map. The + * button matrix keeps a reference to the map and so the string array must not + * be deallocated during the life of the matrix. + * @param obj pointer to a button matrix object + * @param map pointer a string array. The last string has to be: "". Use "\n" to make a line break. + */ +void lv_buttonmatrix_set_map(lv_obj_t * obj, const char * const map[]); + +/** + * Set the button control map (hidden, disabled etc.) for a button matrix. + * The control map array will be copied and so may be deallocated after this + * function returns. + * @param obj pointer to a button matrix object + * @param ctrl_map pointer to an array of `lv_button_ctrl_t` control bytes. The + * length of the array and position of the elements must match + * the number and order of the individual buttons (i.e. excludes + * newline entries). + * An element of the map should look like e.g.: + * `ctrl_map[0] = width | LV_BUTTONMATRIX_CTRL_NO_REPEAT | LV_BUTTONMATRIX_CTRL_TGL_ENABLE` + */ +void lv_buttonmatrix_set_ctrl_map(lv_obj_t * obj, const lv_buttonmatrix_ctrl_t ctrl_map[]); + +/** + * Set the selected buttons + * @param obj pointer to button matrix object + * @param btn_id 0 based index of the button to modify. (Not counting new lines) + */ +void lv_buttonmatrix_set_selected_button(lv_obj_t * obj, uint32_t btn_id); + +/** + * Set the attributes of a button of the button matrix + * @param obj pointer to button matrix object + * @param btn_id 0 based index of the button to modify. (Not counting new lines) + * @param ctrl OR-ed attributes. E.g. `LV_BUTTONMATRIX_CTRL_NO_REPEAT | LV_BUTTONMATRIX_CTRL_CHECKABLE` + */ +void lv_buttonmatrix_set_button_ctrl(lv_obj_t * obj, uint32_t btn_id, lv_buttonmatrix_ctrl_t ctrl); + +/** + * Clear the attributes of a button of the button matrix + * @param obj pointer to button matrix object + * @param btn_id 0 based index of the button to modify. (Not counting new lines) + * @param ctrl OR-ed attributes. E.g. `LV_BUTTONMATRIX_CTRL_NO_REPEAT | LV_BUTTONMATRIX_CTRL_CHECKABLE` + */ +void lv_buttonmatrix_clear_button_ctrl(lv_obj_t * obj, uint32_t btn_id, lv_buttonmatrix_ctrl_t ctrl); + +/** + * Set attributes of all buttons of a button matrix + * @param obj pointer to a button matrix object + * @param ctrl attribute(s) to set from `lv_buttonmatrix_ctrl_t`. Values can be ORed. + */ +void lv_buttonmatrix_set_button_ctrl_all(lv_obj_t * obj, lv_buttonmatrix_ctrl_t ctrl); + +/** + * Clear the attributes of all buttons of a button matrix + * @param obj pointer to a button matrix object + * @param ctrl attribute(s) to set from `lv_buttonmatrix_ctrl_t`. Values can be ORed. + */ +void lv_buttonmatrix_clear_button_ctrl_all(lv_obj_t * obj, lv_buttonmatrix_ctrl_t ctrl); + +/** + * Set a single button's relative width. + * This method will cause the matrix be regenerated and is a relatively + * expensive operation. It is recommended that initial width be specified using + * `lv_buttonmatrix_set_ctrl_map` and this method only be used for dynamic changes. + * @param obj pointer to button matrix object + * @param btn_id 0 based index of the button to modify. + * @param width relative width compared to the buttons in the same row. [1..15] + */ +void lv_buttonmatrix_set_button_width(lv_obj_t * obj, uint32_t btn_id, uint32_t width); + +/** + * Make the button matrix like a selector widget (only one button may be checked at a time). + * `LV_BUTTONMATRIX_CTRL_CHECKABLE` must be enabled on the buttons to be selected using + * `lv_buttonmatrix_set_ctrl()` or `lv_buttonmatrix_set_button_ctrl_all()`. + * @param obj pointer to a button matrix object + * @param en whether "one check" mode is enabled + */ +void lv_buttonmatrix_set_one_checked(lv_obj_t * obj, bool en); + +/*===================== + * Getter functions + *====================*/ + +/** + * Get the current map of a button matrix + * @param obj pointer to a button matrix object + * @return the current map + */ +const char * const * lv_buttonmatrix_get_map(const lv_obj_t * obj); + +/** + * Get the index of the lastly "activated" button by the user (pressed, released, focused etc) + * Useful in the `event_cb` to get the text of the button, check if hidden etc. + * @param obj pointer to button matrix object + * @return index of the last released button (LV_BUTTONMATRIX_BUTTON_NONE: if unset) + */ +uint32_t lv_buttonmatrix_get_selected_button(const lv_obj_t * obj); + +/** + * Get the button's text + * @param obj pointer to button matrix object + * @param btn_id the index a button not counting new line characters. + * @return text of btn_index` button + */ +const char * lv_buttonmatrix_get_button_text(const lv_obj_t * obj, uint32_t btn_id); + +/** + * Get the whether a control value is enabled or disabled for button of a button matrix + * @param obj pointer to a button matrix object + * @param btn_id the index of a button not counting new line characters. + * @param ctrl control values to check (ORed value can be used) + * @return true: the control attribute is enabled false: disabled + */ +bool lv_buttonmatrix_has_button_ctrl(lv_obj_t * obj, uint32_t btn_id, lv_buttonmatrix_ctrl_t ctrl); + +/** + * Tell whether "one check" mode is enabled or not. + * @param obj Button matrix object + * @return true: "one check" mode is enabled; false: disabled + */ +bool lv_buttonmatrix_get_one_checked(const lv_obj_t * obj); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_BUTTONMATRIX*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_BUTTONMATRIX_H*/ diff --git a/include/liblvgl/widgets/buttonmatrix/lv_buttonmatrix_private.h b/include/liblvgl/widgets/buttonmatrix/lv_buttonmatrix_private.h new file mode 100644 index 00000000..824bdcb0 --- /dev/null +++ b/include/liblvgl/widgets/buttonmatrix/lv_buttonmatrix_private.h @@ -0,0 +1,57 @@ +/** + * @file lv_buttonmatrix_private.h + * + */ + +#ifndef LV_BUTTONMATRIX_PRIVATE_H +#define LV_BUTTONMATRIX_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../core/lv_obj_private.h" +#include "lv_buttonmatrix.h" + +#if LV_USE_BUTTONMATRIX != 0 + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** Data of button matrix */ +struct _lv_buttonmatrix_t { + lv_obj_t obj; + const char * const * map_p; /**< Pointer to the current map */ + lv_area_t * button_areas; /**< Array of areas of buttons */ + lv_buttonmatrix_ctrl_t * ctrl_bits; /**< Array of control bytes */ + uint32_t btn_cnt; /**< Number of button in 'map_p'(Handled by the library) */ + uint32_t row_cnt; /**< Number of rows in 'map_p'(Handled by the library) */ + uint32_t btn_id_sel; /**< Index of the active button (being pressed/released etc) or LV_BUTTONMATRIX_BUTTON_NONE */ + uint32_t one_check : 1; /**< Single button toggled at once */ +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_BUTTONMATRIX != 0 */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_BUTTONMATRIX_PRIVATE_H*/ diff --git a/include/liblvgl/extra/widgets/calendar/lv_calendar.h b/include/liblvgl/widgets/calendar/lv_calendar.h similarity index 64% rename from include/liblvgl/extra/widgets/calendar/lv_calendar.h rename to include/liblvgl/widgets/calendar/lv_calendar.h index ad157bf6..38c5d644 100644 --- a/include/liblvgl/extra/widgets/calendar/lv_calendar.h +++ b/include/liblvgl/widgets/calendar/lv_calendar.h @@ -13,7 +13,7 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/widgets/lv_btnmatrix.h" +#include "../buttonmatrix/lv_buttonmatrix.h" #if LV_USE_CALENDAR @@ -30,30 +30,21 @@ extern "C" { */ typedef struct { uint16_t year; - int8_t month; /** 1..12*/ - int8_t day; /** 1..31*/ + int8_t month; /**< 1..12 */ + int8_t day; /**< 1..31 */ } lv_calendar_date_t; -/*Data of calendar*/ -typedef struct { - lv_obj_t obj; - lv_obj_t * btnm; - /*New data for this type*/ - lv_calendar_date_t today; /*Date of today*/ - lv_calendar_date_t showed_date; /*Currently visible month (day is ignored)*/ - lv_calendar_date_t * - highlighted_dates; /*Apply different style on these days (pointer to an array defined by the user)*/ - uint16_t highlighted_dates_num; /*Number of elements in `highlighted_days`*/ - const char * map[8 * 7]; - char nums [7 * 6][4]; -} lv_calendar_t; - -extern const lv_obj_class_t lv_calendar_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_calendar_class; /********************** * GLOBAL PROTOTYPES **********************/ +/** + * Create a calendar widget + * @param parent pointer to an object, it will be the parent of the new calendar + * @return pointer the created calendar + */ lv_obj_t * lv_calendar_create(lv_obj_t * parent); /*====================== @@ -88,7 +79,7 @@ void lv_calendar_set_showed_date(lv_obj_t * obj, uint32_t year, uint32_t month); * Only the pointer will be saved so this variable can't be local which will be destroyed later. * @param date_num number of dates in the array */ -void lv_calendar_set_highlighted_dates(lv_obj_t * obj, lv_calendar_date_t highlighted[], uint16_t date_num); +void lv_calendar_set_highlighted_dates(lv_obj_t * obj, lv_calendar_date_t highlighted[], size_t date_num); /** * Set the name of the days @@ -106,46 +97,47 @@ void lv_calendar_set_day_names(lv_obj_t * obj, const char ** day_names); /** * Get the button matrix object of the calendar. * It shows the dates and day names. - * @param obj pointer to a calendar object - * @return pointer to a the button matrix + * @param obj pointer to a calendar object + * @return pointer to a the button matrix */ lv_obj_t * lv_calendar_get_btnmatrix(const lv_obj_t * obj); /** * Get the today's date - * @param calendar pointer to a calendar object - * @return return pointer to an `lv_calendar_date_t` variable containing the date of today. + * @param calendar pointer to a calendar object + * @return return pointer to an `lv_calendar_date_t` variable containing the date of today. */ const lv_calendar_date_t * lv_calendar_get_today_date(const lv_obj_t * calendar); /** * Get the currently showed - * @param calendar pointer to a calendar object - * @return pointer to an `lv_calendar_date_t` variable containing the date is being shown. + * @param calendar pointer to a calendar object + * @return pointer to an `lv_calendar_date_t` variable containing the date is being shown. */ const lv_calendar_date_t * lv_calendar_get_showed_date(const lv_obj_t * calendar); /** * Get the highlighted dates - * @param calendar pointer to a calendar object - * @return pointer to an `lv_calendar_date_t` array containing the dates. + * @param calendar pointer to a calendar object + * @return pointer to an `lv_calendar_date_t` array containing the dates. */ lv_calendar_date_t * lv_calendar_get_highlighted_dates(const lv_obj_t * calendar); /** * Get the number of the highlighted dates - * @param calendar pointer to a calendar object - * @return number of highlighted days + * @param calendar pointer to a calendar object + * @return number of highlighted days */ -uint16_t lv_calendar_get_highlighted_dates_num(const lv_obj_t * calendar); +size_t lv_calendar_get_highlighted_dates_num(const lv_obj_t * calendar); /** * Get the currently pressed day - * @param calendar pointer to a calendar object - * @param date store the pressed date here - * @return LV_RES_OK: there is a valid pressed date; LV_RES_INV: there is no pressed data + * @param calendar pointer to a calendar object + * @param date store the pressed date here + * @return LV_RESULT_OK: there is a valid pressed date + * LV_RESULT_INVALID: there is no pressed data */ -lv_res_t lv_calendar_get_pressed_date(const lv_obj_t * calendar, lv_calendar_date_t * date); +lv_result_t lv_calendar_get_pressed_date(const lv_obj_t * calendar, lv_calendar_date_t * date); /*===================== * Other functions @@ -155,6 +147,10 @@ lv_res_t lv_calendar_get_pressed_date(const lv_obj_t * calendar, lv_calendar_dat * MACROS **********************/ +#include "lv_calendar_header_arrow.h" +#include "lv_calendar_header_dropdown.h" +#include "lv_calendar_chinese.h" + #endif /*LV_USE_CALENDAR*/ #ifdef __cplusplus diff --git a/include/liblvgl/widgets/calendar/lv_calendar_chinese.h b/include/liblvgl/widgets/calendar/lv_calendar_chinese.h new file mode 100644 index 00000000..fb7a1bc0 --- /dev/null +++ b/include/liblvgl/widgets/calendar/lv_calendar_chinese.h @@ -0,0 +1,68 @@ +/** + * @file lv_calendar_chinese.h + * + */ + +#ifndef LV_CALENDAR_CHINESE_H +#define LV_CALENDAR_CHINESE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../core/lv_obj.h" +#include "lv_calendar.h" +#if LV_USE_CALENDAR && LV_USE_CALENDAR_CHINESE + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +typedef struct { + lv_calendar_date_t today; + bool leep_month; +} lv_calendar_chinese_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Enable the chinese calendar. + * @param obj pointer to a calendar object. + * @param en true: enable chinese calendar; false: disable + */ +void lv_calendar_set_chinese_mode(lv_obj_t * obj, bool en); + +/** + * Get the name of the day + * @param gregorian to obtain the gregorian time for the name + * @return return the name of the day + */ +const char * lv_calendar_get_day_name(lv_calendar_date_t * gregorian); + +/** + * Get the chinese time of the gregorian time (reference: https://www.cnblogs.com/liyang31tg/p/4123171.html) + * @param gregorian_time need to convert to chinese time in gregorian time + * @param chinese_time the chinese time convert from gregorian time + */ +void lv_calendar_gregorian_to_chinese(lv_calendar_date_t * gregorian_time, lv_calendar_chinese_t * chinese_time); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_CALENDAR_CHINESE*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_CALENDAR_CHINESE_H*/ diff --git a/include/liblvgl/widgets/calendar/lv_calendar_chinese_private.h b/include/liblvgl/widgets/calendar/lv_calendar_chinese_private.h new file mode 100644 index 00000000..5572259e --- /dev/null +++ b/include/liblvgl/widgets/calendar/lv_calendar_chinese_private.h @@ -0,0 +1,53 @@ +/** + * @file lv_calendar_chinese_private.h + * + */ + +#ifndef LV_CALENDAR_CHINESE_PRIVATE_H +#define LV_CALENDAR_CHINESE_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_calendar_chinese.h" + +#if LV_USE_CALENDAR && LV_USE_CALENDAR_CHINESE + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct lv_calendar_chinese_t { + lv_calendar_date_t today; + bool leep_month; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_CALENDAR && LV_USE_CALENDAR_CHINESE */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_CALENDAR_CHINESE_PRIVATE_H*/ diff --git a/include/liblvgl/extra/widgets/calendar/lv_calendar_header_arrow.h b/include/liblvgl/widgets/calendar/lv_calendar_header_arrow.h similarity index 83% rename from include/liblvgl/extra/widgets/calendar/lv_calendar_header_arrow.h rename to include/liblvgl/widgets/calendar/lv_calendar_header_arrow.h index 30639e85..a04b355c 100644 --- a/include/liblvgl/extra/widgets/calendar/lv_calendar_header_arrow.h +++ b/include/liblvgl/widgets/calendar/lv_calendar_header_arrow.h @@ -13,8 +13,8 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/core/lv_obj.h" -#if LV_USE_CALENDAR_HEADER_ARROW +#include "../../core/lv_obj.h" +#if LV_USE_CALENDAR && LV_USE_CALENDAR_HEADER_ARROW /********************* * DEFINES @@ -23,7 +23,7 @@ extern "C" { /********************** * TYPEDEFS **********************/ -extern const lv_obj_class_t lv_calendar_header_arrow_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_calendar_header_arrow_class; /********************** * GLOBAL PROTOTYPES diff --git a/include/liblvgl/extra/widgets/calendar/lv_calendar_header_dropdown.h b/include/liblvgl/widgets/calendar/lv_calendar_header_dropdown.h similarity index 52% rename from include/liblvgl/extra/widgets/calendar/lv_calendar_header_dropdown.h rename to include/liblvgl/widgets/calendar/lv_calendar_header_dropdown.h index ba926a0b..1ca87762 100644 --- a/include/liblvgl/extra/widgets/calendar/lv_calendar_header_dropdown.h +++ b/include/liblvgl/widgets/calendar/lv_calendar_header_dropdown.h @@ -13,8 +13,12 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/core/lv_obj.h" -#if LV_USE_CALENDAR_HEADER_DROPDOWN +#include "../../core/lv_obj.h" +#if LV_USE_CALENDAR && LV_USE_CALENDAR_HEADER_DROPDOWN + +#if LV_USE_DROPDOWN == 0 +#error "LV_USE_DROPDOWN needs to be enabled" +#endif /********************* * DEFINES @@ -23,7 +27,7 @@ extern "C" { /********************** * TYPEDEFS **********************/ -extern const lv_obj_class_t lv_calendar_header_dropdown_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_calendar_header_dropdown_class; /********************** * GLOBAL PROTOTYPES @@ -36,6 +40,15 @@ extern const lv_obj_class_t lv_calendar_header_dropdown_class; */ lv_obj_t * lv_calendar_header_dropdown_create(lv_obj_t * parent); +/** + * Sets a custom calendar year list + * @param parent pointer to a calendar object + * @param years_list pointer to an const char array with the years list, see lv_dropdown set_options for more information. + * E.g. `const char * years = "2023\n2022\n2021\n2020\n2019" + * Only the pointer will be saved so this variable can't be local which will be destroyed later. + */ +void lv_calendar_header_dropdown_set_year_list(lv_obj_t * parent, const char * years_list); + /********************** * MACROS **********************/ diff --git a/include/liblvgl/widgets/calendar/lv_calendar_private.h b/include/liblvgl/widgets/calendar/lv_calendar_private.h new file mode 100644 index 00000000..0a5b8a2e --- /dev/null +++ b/include/liblvgl/widgets/calendar/lv_calendar_private.h @@ -0,0 +1,70 @@ +/** + * @file lv_calendar_private.h + * + */ + +#ifndef LV_CALENDAR_PRIVATE_H +#define LV_CALENDAR_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../core/lv_obj_private.h" +#include "lv_calendar.h" + +#if LV_USE_CALENDAR + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** Data of calendar */ +struct _lv_calendar_t { + lv_obj_t obj; + /* New data for this type */ + lv_obj_t * btnm; + lv_calendar_date_t today; /**< Date of today */ + lv_calendar_date_t showed_date; /**< Currently visible month (day is ignored) */ + lv_calendar_date_t * highlighted_dates; /**< Apply different style on these days (pointer to user-defined array) */ + size_t highlighted_dates_num; /**< Number of elements in `highlighted_days` */ + const char * map[8 * 7]; +#ifdef LV_USE_CALENDAR_CHINESE + bool use_chinese_calendar; + + /** 7 * 6: A week has 7 days, and the calendar displays 6 weeks in total. + * 20: Including the number of dates, line breaks, names for each day, + * and reserving several spaces for addresses. */ + char nums [7 * 6][20]; +#else + /** 7 * 6: A week has 7 days, and the calendar displays 6 weeks in total. + * 6: Including the number of dates, and reserving several spaces for + * addresses.*/ + char nums [7 * 6][4]; +#endif +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_CALENDAR */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_CALENDAR_PRIVATE_H*/ diff --git a/include/liblvgl/widgets/canvas/lv_canvas.h b/include/liblvgl/widgets/canvas/lv_canvas.h new file mode 100644 index 00000000..d669b177 --- /dev/null +++ b/include/liblvgl/widgets/canvas/lv_canvas.h @@ -0,0 +1,185 @@ +/** + * @file lv_canvas.h + * + */ + +#ifndef LV_CANVAS_H +#define LV_CANVAS_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../lv_conf_internal.h" + +#if LV_USE_CANVAS != 0 + +#include "../image/lv_image.h" +#include "../../draw/lv_draw_image.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_canvas_class; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create a canvas object + * @param parent pointer to an object, it will be the parent of the new canvas + * @return pointer to the created canvas + */ +lv_obj_t * lv_canvas_create(lv_obj_t * parent); + +/*===================== + * Setter functions + *====================*/ + +/** + * Set a buffer for the canvas. + * + * Use lv_canvas_set_draw_buf() instead if you need to set a buffer with alignment requirement. + * + * @param obj pointer to a canvas object + * @param buf buffer where content of canvas will be. + * The required size is (lv_image_color_format_get_px_size(cf) * w) / 8 * h) + * It can be allocated with `lv_malloc()` or + * it can be statically allocated array (e.g. static lv_color_t buf[100*50]) or + * it can be an address in RAM or external SRAM + * @param w width of canvas + * @param h height of canvas + * @param cf color format. `LV_COLOR_FORMAT...` + */ +void lv_canvas_set_buffer(lv_obj_t * obj, void * buf, int32_t w, int32_t h, lv_color_format_t cf); + +/** + * Set a draw buffer for the canvas. A draw buffer either can be allocated by `lv_draw_buf_create()` + * or defined statically by `LV_DRAW_BUF_DEFINE_STATIC`. When buffer start address and stride has alignment + * requirement, it's recommended to use `lv_draw_buf_create`. + * @param obj pointer to a canvas object + * @param draw_buf pointer to a draw buffer + */ +void lv_canvas_set_draw_buf(lv_obj_t * obj, lv_draw_buf_t * draw_buf); + +/** + * Set a pixel's color and opacity + * @param obj pointer to a canvas + * @param x X coordinate of the pixel + * @param y Y coordinate of the pixel + * @param color the color + * @param opa the opacity + * @note The following color formats are supported + * LV_COLOR_FORMAT_I1/2/4/8, LV_COLOR_FORMAT_A8, + * LV_COLOR_FORMAT_RGB565, LV_COLOR_FORMAT_RGB888, + * LV_COLOR_FORMAT_XRGB8888, LV_COLOR_FORMAT_ARGB8888 + */ +void lv_canvas_set_px(lv_obj_t * obj, int32_t x, int32_t y, lv_color_t color, lv_opa_t opa); + +/** + * Set the palette color of a canvas for index format. Valid only for `LV_COLOR_FORMAT_I1/2/4/8` + * @param obj pointer to canvas object + * @param index the palette color to set: + * - for `LV_COLOR_FORMAT_I1`: 0..1 + * - for `LV_COLOR_FORMAT_I2`: 0..3 + * - for `LV_COLOR_FORMAT_I4`: 0..15 + * - for `LV_COLOR_FORMAT_I8`: 0..255 + * @param color the color to set + */ +void lv_canvas_set_palette(lv_obj_t * obj, uint8_t index, lv_color32_t color); + +/*===================== + * Getter functions + *====================*/ + +lv_draw_buf_t * lv_canvas_get_draw_buf(lv_obj_t * obj); + +/** + * Get a pixel's color and opacity + * @param obj pointer to a canvas + * @param x X coordinate of the pixel + * @param y Y coordinate of the pixel + * @return ARGB8888 color of the pixel + */ +lv_color32_t lv_canvas_get_px(lv_obj_t * obj, int32_t x, int32_t y); + +/** + * Get the image of the canvas as a pointer to an `lv_image_dsc_t` variable. + * @param canvas pointer to a canvas object + * @return pointer to the image descriptor. + */ +lv_image_dsc_t * lv_canvas_get_image(lv_obj_t * canvas); + +/** + * Return the pointer for the buffer. + * It's recommended to use this function instead of the buffer form the + * return value of lv_canvas_get_image() as is can be aligned + * @param canvas pointer to a canvas object + * @return pointer to the buffer + */ +const void * lv_canvas_get_buf(lv_obj_t * canvas); + +/*===================== + * Other functions + *====================*/ + +/** + * Copy a buffer to the canvas + * @param obj pointer to a canvas object + * @param canvas_area the area of the canvas to copy + * @param dest_buf pointer to a buffer to store the copied data + * @param dest_area the area of the destination buffer to copy to. If omitted NULL, copy to the whole `dest_buf` + */ +void lv_canvas_copy_buf(lv_obj_t * obj, const lv_area_t * canvas_area, lv_draw_buf_t * dest_buf, + const lv_area_t * dest_area); + +/** + * Fill the canvas with color + * @param obj pointer to a canvas + * @param color the background color + * @param opa the desired opacity + */ +void lv_canvas_fill_bg(lv_obj_t * obj, lv_color_t color, lv_opa_t opa); + +/** + * Initialize a layer to use LVGL's generic draw functions (lv_draw_rect/label/...) on the canvas. + * Needs to be usd in pair with `lv_canvas_finish_layer`. + * @param canvas pointer to a canvas + * @param layer pointer to a layer variable to initialize + */ +void lv_canvas_init_layer(lv_obj_t * canvas, lv_layer_t * layer); + +/** + * Wait until all the drawings are finished on layer. + * Needs to be usd in pair with `lv_canvas_init_layer`. + * @param canvas pointer to a canvas + * @param layer pointer to a layer to finalize + */ +void lv_canvas_finish_layer(lv_obj_t * canvas, lv_layer_t * layer); + +/********************** + * MACROS + **********************/ + +#define LV_CANVAS_BUF_SIZE(w, h, bpp, stride) (((((w * bpp + 7) >> 3) + stride - 1) & ~(stride - 1)) * h + LV_DRAW_BUF_ALIGN) + +/** + * Just a wrapper to `LV_CANVAS_BUF_SIZE` for bindings. + */ +uint32_t lv_canvas_buf_size(int32_t w, int32_t h, uint8_t bpp, uint8_t stride); + +#endif /*LV_USE_CANVAS*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_CANVAS_H*/ diff --git a/include/liblvgl/widgets/canvas/lv_canvas_private.h b/include/liblvgl/widgets/canvas/lv_canvas_private.h new file mode 100644 index 00000000..55b30547 --- /dev/null +++ b/include/liblvgl/widgets/canvas/lv_canvas_private.h @@ -0,0 +1,52 @@ +/** + * @file lv_canvas_private.h + * + */ + +#ifndef LV_CANVAS_PRIVATE_H +#define LV_CANVAS_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../image/lv_image_private.h" +#include "lv_canvas.h" + +#if LV_USE_CANVAS != 0 + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** Canvas data */ +struct _lv_canvas_t { + lv_image_t img; + lv_draw_buf_t * draw_buf; + lv_draw_buf_t static_buf; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_CANVAS != 0 */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_CANVAS_PRIVATE_H*/ diff --git a/include/liblvgl/extra/widgets/chart/lv_chart.h b/include/liblvgl/widgets/chart/lv_chart.h similarity index 65% rename from include/liblvgl/extra/widgets/chart/lv_chart.h rename to include/liblvgl/widgets/chart/lv_chart.h index 14d70b9a..de258314 100644 --- a/include/liblvgl/extra/widgets/chart/lv_chart.h +++ b/include/liblvgl/widgets/chart/lv_chart.h @@ -13,7 +13,8 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lvgl.h" +#include "../../lv_conf_internal.h" +#include "../../core/lv_obj.h" #if LV_USE_CHART != 0 @@ -22,11 +23,7 @@ extern "C" { *********************/ /**Default value of points. Can be used to not draw a point*/ -#if LV_USE_LARGE_COORD -#define LV_CHART_POINT_NONE (INT32_MAX) -#else -#define LV_CHART_POINT_NONE (INT16_MAX) -#endif +#define LV_CHART_POINT_NONE (INT32_MAX) LV_EXPORT_CONST_INT(LV_CHART_POINT_NONE); /********************** @@ -36,103 +33,33 @@ LV_EXPORT_CONST_INT(LV_CHART_POINT_NONE); /** * Chart types */ -enum { +typedef enum { LV_CHART_TYPE_NONE, /**< Don't draw the series*/ LV_CHART_TYPE_LINE, /**< Connect the points with lines*/ LV_CHART_TYPE_BAR, /**< Draw columns*/ LV_CHART_TYPE_SCATTER, /**< Draw points and lines in 2D (x,y coordinates)*/ -}; -typedef uint8_t lv_chart_type_t; +} lv_chart_type_t; /** * Chart update mode for `lv_chart_set_next` */ -enum { +typedef enum { LV_CHART_UPDATE_MODE_SHIFT, /**< Shift old data to the left and add the new one the right*/ LV_CHART_UPDATE_MODE_CIRCULAR, /**< Add the new data in a circular way*/ -}; -typedef uint8_t lv_chart_update_mode_t; +} lv_chart_update_mode_t; /** * Enumeration of the axis' */ -enum { +typedef enum { LV_CHART_AXIS_PRIMARY_Y = 0x00, LV_CHART_AXIS_SECONDARY_Y = 0x01, LV_CHART_AXIS_PRIMARY_X = 0x02, LV_CHART_AXIS_SECONDARY_X = 0x04, - _LV_CHART_AXIS_LAST -}; -typedef uint8_t lv_chart_axis_t; - -/** - * Descriptor a chart series - */ -typedef struct { - lv_coord_t * x_points; - lv_coord_t * y_points; - lv_color_t color; - uint16_t start_point; - uint8_t hidden : 1; - uint8_t x_ext_buf_assigned : 1; - uint8_t y_ext_buf_assigned : 1; - uint8_t x_axis_sec : 1; - uint8_t y_axis_sec : 1; -} lv_chart_series_t; - -typedef struct { - lv_point_t pos; - lv_coord_t point_id; - lv_color_t color; - lv_chart_series_t * ser; - lv_dir_t dir; - uint8_t pos_set: 1; /*1: pos is set; 0: point_id is set*/ -} lv_chart_cursor_t; - -typedef struct { - lv_coord_t major_len; - lv_coord_t minor_len; - lv_coord_t draw_size; - uint32_t minor_cnt : 15; - uint32_t major_cnt : 15; - uint32_t label_en : 1; -} lv_chart_tick_dsc_t; - - -typedef struct { - lv_obj_t obj; - lv_ll_t series_ll; /**< Linked list for the series (stores lv_chart_series_t)*/ - lv_ll_t cursor_ll; /**< Linked list for the cursors (stores lv_chart_cursor_t)*/ - lv_chart_tick_dsc_t tick[4]; - lv_coord_t ymin[2]; - lv_coord_t ymax[2]; - lv_coord_t xmin[2]; - lv_coord_t xmax[2]; - lv_coord_t pressed_point_id; - uint16_t hdiv_cnt; /**< Number of horizontal division lines*/ - uint16_t vdiv_cnt; /**< Number of vertical division lines*/ - uint16_t point_cnt; /**< Point number in a data line*/ - uint16_t zoom_x; - uint16_t zoom_y; - lv_chart_type_t type : 3; /**< Line or column chart*/ - lv_chart_update_mode_t update_mode : 1; -} lv_chart_t; - -extern const lv_obj_class_t lv_chart_class; - -/** - * `type` field in `lv_obj_draw_part_dsc_t` if `class_p = lv_chart_class` - * Used in `LV_EVENT_DRAW_PART_BEGIN` and `LV_EVENT_DRAW_PART_END` - */ -typedef enum { - LV_CHART_DRAW_PART_DIV_LINE_INIT, /**< Used before/after drawn the div lines*/ - LV_CHART_DRAW_PART_DIV_LINE_HOR, /**< Used for each horizontal division lines*/ - LV_CHART_DRAW_PART_DIV_LINE_VER, /**< Used for each vertical division lines*/ - LV_CHART_DRAW_PART_LINE_AND_POINT, /**< Used on line and scatter charts for lines and points*/ - LV_CHART_DRAW_PART_BAR, /**< Used on bar charts for the rectangles*/ - LV_CHART_DRAW_PART_CURSOR, /**< Used on cursor lines and points*/ - LV_CHART_DRAW_PART_TICK_LABEL, /**< Used on tick lines and labels*/ -} lv_chart_draw_part_type_t; + LV_CHART_AXIS_LAST +} lv_chart_axis_t; + +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_chart_class; /********************** * GLOBAL PROTOTYPES @@ -156,7 +83,7 @@ void lv_chart_set_type(lv_obj_t * obj, lv_chart_type_t type); * @param obj pointer to a chart object * @param cnt new number of points on the data lines */ -void lv_chart_set_point_count(lv_obj_t * obj, uint16_t cnt); +void lv_chart_set_point_count(lv_obj_t * obj, uint32_t cnt); /** * Set the minimal and maximal y values on an axis @@ -165,12 +92,12 @@ void lv_chart_set_point_count(lv_obj_t * obj, uint16_t cnt); * @param min minimum value of the y axis * @param max maximum value of the y axis */ -void lv_chart_set_range(lv_obj_t * obj, lv_chart_axis_t axis, lv_coord_t min, lv_coord_t max); +void lv_chart_set_range(lv_obj_t * obj, lv_chart_axis_t axis, int32_t min, int32_t max); /** * Set update mode of the chart object. Affects - * @param obj pointer to a chart object - * @param mode the update mode + * @param obj pointer to a chart object + * @param update_mode the update mode */ void lv_chart_set_update_mode(lv_obj_t * obj, lv_chart_update_mode_t update_mode); @@ -182,49 +109,6 @@ void lv_chart_set_update_mode(lv_obj_t * obj, lv_chart_update_mode_t update_mode */ void lv_chart_set_div_line_count(lv_obj_t * obj, uint8_t hdiv, uint8_t vdiv); -/** - * Zoom into the chart in X direction - * @param obj pointer to a chart object - * @param zoom_x zoom in x direction. LV_ZOOM_NONE or 256 for no zoom, 512 double zoom - */ -void lv_chart_set_zoom_x(lv_obj_t * obj, uint16_t zoom_x); - -/** - * Zoom into the chart in Y direction - * @param obj pointer to a chart object - * @param zoom_y zoom in y direction. LV_ZOOM_NONE or 256 for no zoom, 512 double zoom - */ -void lv_chart_set_zoom_y(lv_obj_t * obj, uint16_t zoom_y); - -/** - * Get X zoom of a chart - * @param obj pointer to a chart object - * @return the X zoom value - */ -uint16_t lv_chart_get_zoom_x(const lv_obj_t * obj); - -/** - * Get Y zoom of a chart - * @param obj pointer to a chart object - * @return the Y zoom value - */ -uint16_t lv_chart_get_zoom_y(const lv_obj_t * obj); - -/** - * Set the number of tick lines on an axis - * @param obj pointer to a chart object - * @param axis an axis which ticks count should be set - * @param major_len length of major ticks - * @param minor_len length of minor ticks - * @param major_cnt number of major ticks on the axis - * @param minor_cnt number of minor ticks between two major ticks - * @param label_en true: enable label drawing on major ticks - * @param draw_size extra size required to draw the tick and labels - * (start with 20 px and increase if the ticks/labels are clipped) - */ -void lv_chart_set_axis_tick(lv_obj_t * obj, lv_chart_axis_t axis, lv_coord_t major_len, lv_coord_t minor_len, - lv_coord_t major_cnt, lv_coord_t minor_cnt, bool label_en, lv_coord_t draw_size); - /** * Get the type of a chart * @param obj pointer to chart object @@ -234,31 +118,31 @@ lv_chart_type_t lv_chart_get_type(const lv_obj_t * obj); /** * Get the data point number per data line on chart - * @param chart pointer to chart object + * @param obj pointer to chart object * @return point number on each data line */ -uint16_t lv_chart_get_point_count(const lv_obj_t * obj); +uint32_t lv_chart_get_point_count(const lv_obj_t * obj); /** * Get the current index of the x-axis start point in the data array - * @param chart pointer to a chart object + * @param obj pointer to a chart object * @param ser pointer to a data series on 'chart' * @return the index of the current x start point in the data array */ -uint16_t lv_chart_get_x_start_point(const lv_obj_t * obj, lv_chart_series_t * ser); +uint32_t lv_chart_get_x_start_point(const lv_obj_t * obj, lv_chart_series_t * ser); /** * Get the position of a point to the chart. - * @param chart pointer to a chart object + * @param obj pointer to a chart object * @param ser pointer to series * @param id the index. * @param p_out store the result position here */ -void lv_chart_get_point_pos_by_id(lv_obj_t * obj, lv_chart_series_t * ser, uint16_t id, lv_point_t * p_out); +void lv_chart_get_point_pos_by_id(lv_obj_t * obj, lv_chart_series_t * ser, uint32_t id, lv_point_t * p_out); /** * Refresh a chart if its data line has changed - * @param chart pointer to chart object + * @param obj pointer to chart object */ void lv_chart_refresh(lv_obj_t * obj); @@ -271,20 +155,20 @@ void lv_chart_refresh(lv_obj_t * obj); * @param obj pointer to a chart object * @param color color of the data series * @param axis the y axis to which the series should be attached (::LV_CHART_AXIS_PRIMARY_Y or ::LV_CHART_AXIS_SECONDARY_Y) - * @return pointer to the allocated data series + * @return pointer to the allocated data series or NULL on failure */ lv_chart_series_t * lv_chart_add_series(lv_obj_t * obj, lv_color_t color, lv_chart_axis_t axis); /** * Deallocate and remove a data series from a chart - * @param chart pointer to a chart object + * @param obj pointer to a chart object * @param series pointer to a data series on 'chart' */ void lv_chart_remove_series(lv_obj_t * obj, lv_chart_series_t * series); /** * Hide/Unhide a single series of a chart. - * @param obj pointer to a chart object. + * @param chart pointer to a chart object. * @param series pointer to a series object * @param hide true: hide the series */ @@ -292,12 +176,20 @@ void lv_chart_hide_series(lv_obj_t * chart, lv_chart_series_t * series, bool hid /** * Change the color of a series - * @param obj pointer to a chart object. + * @param chart pointer to a chart object. * @param series pointer to a series object * @param color the new color of the series */ void lv_chart_set_series_color(lv_obj_t * chart, lv_chart_series_t * series, lv_color_t color); +/** + * Get the color of a series + * @param chart pointer to a chart object. + * @param series pointer to a series object + * @return the color of the series + */ +lv_color_t lv_chart_get_series_color(lv_obj_t * chart, const lv_chart_series_t * series); + /** * Set the index of the x-axis start point in the data array. * This point will be considers the first (left) point and the other points will be drawn after it. @@ -305,7 +197,7 @@ void lv_chart_set_series_color(lv_obj_t * chart, lv_chart_series_t * series, lv_ * @param ser pointer to a data series on 'chart' * @param id the index of the x point in the data array */ -void lv_chart_set_x_start_point(lv_obj_t * obj, lv_chart_series_t * ser, uint16_t id); +void lv_chart_set_x_start_point(lv_obj_t * obj, lv_chart_series_t * ser, uint32_t id); /** * Get the next series. @@ -315,8 +207,6 @@ void lv_chart_set_x_start_point(lv_obj_t * obj, lv_chart_series_t * ser, uint16_ */ lv_chart_series_t * lv_chart_get_series_next(const lv_obj_t * chart, const lv_chart_series_t * ser); - - /*===================== * Cursor *====================*/ @@ -332,7 +222,7 @@ lv_chart_cursor_t * lv_chart_add_cursor(lv_obj_t * obj, lv_color_t color, lv_di /** * Set the coordinate of the cursor with respect to the paddings - * @param obj pointer to a chart object + * @param chart pointer to a chart object * @param cursor pointer to the cursor * @param pos the new coordinate of cursor relative to the chart */ @@ -340,17 +230,17 @@ void lv_chart_set_cursor_pos(lv_obj_t * chart, lv_chart_cursor_t * cursor, lv_po /** * Stick the cursor to a point - * @param obj pointer to a chart object + * @param chart pointer to a chart object * @param cursor pointer to the cursor * @param ser pointer to a series * @param point_id the point's index or `LV_CHART_POINT_NONE` to not assign to any points. */ void lv_chart_set_cursor_point(lv_obj_t * chart, lv_chart_cursor_t * cursor, lv_chart_series_t * ser, - uint16_t point_id); + uint32_t point_id); /** * Get the coordinate of the cursor with respect to the paddings - * @param obj pointer to a chart object + * @param chart pointer to a chart object * @param cursor pointer to cursor * @return coordinate of the cursor as lv_point_t */ @@ -366,7 +256,7 @@ lv_point_t lv_chart_get_cursor_point(lv_obj_t * chart, lv_chart_cursor_t * curso * @param ser pointer to a data series on 'chart' * @param value the new value for all points. `LV_CHART_POINT_NONE` can be used to hide the points. */ -void lv_chart_set_all_value(lv_obj_t * obj, lv_chart_series_t * ser, lv_coord_t value); +void lv_chart_set_all_value(lv_obj_t * obj, lv_chart_series_t * ser, int32_t value); /** * Set the next point's Y value according to the update mode policy. @@ -374,7 +264,7 @@ void lv_chart_set_all_value(lv_obj_t * obj, lv_chart_series_t * ser, lv_coord_t * @param ser pointer to a data series on 'chart' * @param value the new value of the next data */ -void lv_chart_set_next_value(lv_obj_t * obj, lv_chart_series_t * ser, lv_coord_t value); +void lv_chart_set_next_value(lv_obj_t * obj, lv_chart_series_t * ser, int32_t value); /** * Set the next point's X and Y value according to the update mode policy. @@ -383,7 +273,7 @@ void lv_chart_set_next_value(lv_obj_t * obj, lv_chart_series_t * ser, lv_coord_t * @param x_value the new X value of the next data * @param y_value the new Y value of the next data */ -void lv_chart_set_next_value2(lv_obj_t * obj, lv_chart_series_t * ser, lv_coord_t x_value, lv_coord_t y_value); +void lv_chart_set_next_value2(lv_obj_t * obj, lv_chart_series_t * ser, int32_t x_value, int32_t y_value); /** * Set an individual point's y value of a chart's series directly based on its index @@ -392,7 +282,7 @@ void lv_chart_set_next_value2(lv_obj_t * obj, lv_chart_series_t * ser, lv_coord_ * @param id the index of the x point in the array * @param value value to assign to array point */ -void lv_chart_set_value_by_id(lv_obj_t * obj, lv_chart_series_t * ser, uint16_t id, lv_coord_t value); +void lv_chart_set_value_by_id(lv_obj_t * obj, lv_chart_series_t * ser, uint32_t id, int32_t value); /** * Set an individual point's x and y value of a chart's series directly based on its index @@ -403,8 +293,8 @@ void lv_chart_set_value_by_id(lv_obj_t * obj, lv_chart_series_t * ser, uint16_t * @param x_value the new X value of the next data * @param y_value the new Y value of the next data */ -void lv_chart_set_value_by_id2(lv_obj_t * obj, lv_chart_series_t * ser, uint16_t id, lv_coord_t x_value, - lv_coord_t y_value); +void lv_chart_set_value_by_id2(lv_obj_t * obj, lv_chart_series_t * ser, uint32_t id, int32_t x_value, + int32_t y_value); /** * Set an external array for the y data points to use for the chart @@ -413,7 +303,7 @@ void lv_chart_set_value_by_id2(lv_obj_t * obj, lv_chart_series_t * ser, uint16_t * @param ser pointer to a data series on 'chart' * @param array external array of points for chart */ -void lv_chart_set_ext_y_array(lv_obj_t * obj, lv_chart_series_t * ser, lv_coord_t array[]); +void lv_chart_set_ext_y_array(lv_obj_t * obj, lv_chart_series_t * ser, int32_t array[]); /** * Set an external array for the x data points to use for the chart @@ -422,7 +312,7 @@ void lv_chart_set_ext_y_array(lv_obj_t * obj, lv_chart_series_t * ser, lv_coord_ * @param ser pointer to a data series on 'chart' * @param array external array of points for chart */ -void lv_chart_set_ext_x_array(lv_obj_t * obj, lv_chart_series_t * ser, lv_coord_t array[]); +void lv_chart_set_ext_x_array(lv_obj_t * obj, lv_chart_series_t * ser, int32_t array[]); /** * Get the array of y values of a series @@ -430,7 +320,7 @@ void lv_chart_set_ext_x_array(lv_obj_t * obj, lv_chart_series_t * ser, lv_coord_ * @param ser pointer to a data series on 'chart' * @return the array of values with 'point_count' elements */ -lv_coord_t * lv_chart_get_y_array(const lv_obj_t * obj, lv_chart_series_t * ser); +int32_t * lv_chart_get_y_array(const lv_obj_t * obj, lv_chart_series_t * ser); /** * Get the array of x values of a series @@ -438,7 +328,7 @@ lv_coord_t * lv_chart_get_y_array(const lv_obj_t * obj, lv_chart_series_t * ser) * @param ser pointer to a data series on 'chart' * @return the array of values with 'point_count' elements */ -lv_coord_t * lv_chart_get_x_array(const lv_obj_t * obj, lv_chart_series_t * ser); +int32_t * lv_chart_get_x_array(const lv_obj_t * obj, lv_chart_series_t * ser); /** * Get the index of the currently pressed point. It's the same for every series. @@ -447,6 +337,14 @@ lv_coord_t * lv_chart_get_x_array(const lv_obj_t * obj, lv_chart_series_t * ser) */ uint32_t lv_chart_get_pressed_point(const lv_obj_t * obj); +/** + * Get the overall offset from the chart's side to the center of the first point. + * In case of a bar chart it will be the center of the first column group + * @param obj pointer to a chart object + * @return the offset of the center + */ +int32_t lv_chart_get_first_point_center_offset(lv_obj_t * obj); + /********************** * MACROS **********************/ diff --git a/include/liblvgl/widgets/chart/lv_chart_private.h b/include/liblvgl/widgets/chart/lv_chart_private.h new file mode 100644 index 00000000..4380db21 --- /dev/null +++ b/include/liblvgl/widgets/chart/lv_chart_private.h @@ -0,0 +1,85 @@ +/** + * @file lv_chart_private.h + * + */ + +#ifndef LV_CHART_PRIVATE_H +#define LV_CHART_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../core/lv_obj_private.h" +#include "lv_chart.h" + +#if LV_USE_CHART != 0 + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** + * Descriptor a chart series + */ +struct _lv_chart_series_t { + int32_t * x_points; + int32_t * y_points; + lv_color_t color; + uint32_t start_point; + uint32_t hidden : 1; + uint32_t x_ext_buf_assigned : 1; + uint32_t y_ext_buf_assigned : 1; + uint32_t x_axis_sec : 1; + uint32_t y_axis_sec : 1; +}; + +struct _lv_chart_cursor_t { + lv_point_t pos; + int32_t point_id; + lv_color_t color; + lv_chart_series_t * ser; + lv_dir_t dir; + uint32_t pos_set: 1; /**< 1: pos is set; 0: point_id is set*/ +}; + +struct _lv_chart_t { + lv_obj_t obj; + lv_ll_t series_ll; /**< Linked list for the series (stores lv_chart_series_t)*/ + lv_ll_t cursor_ll; /**< Linked list for the cursors (stores lv_chart_cursor_t)*/ + int32_t ymin[2]; + int32_t ymax[2]; + int32_t xmin[2]; + int32_t xmax[2]; + int32_t pressed_point_id; + uint32_t hdiv_cnt; /**< Number of horizontal division lines*/ + uint32_t vdiv_cnt; /**< Number of vertical division lines*/ + uint32_t point_cnt; /**< Point number in a data line*/ + lv_chart_type_t type : 3; /**< Line or column chart*/ + lv_chart_update_mode_t update_mode : 1; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_CHART != 0 */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_CHART_PRIVATE_H*/ diff --git a/include/liblvgl/widgets/lv_checkbox.h b/include/liblvgl/widgets/checkbox/lv_checkbox.h similarity index 68% rename from include/liblvgl/widgets/lv_checkbox.h rename to include/liblvgl/widgets/checkbox/lv_checkbox.h index 6b2bdc39..804ff600 100644 --- a/include/liblvgl/widgets/lv_checkbox.h +++ b/include/liblvgl/widgets/checkbox/lv_checkbox.h @@ -1,5 +1,5 @@ /** - * @file lv_cb.h + * @file lv_checkbox.h * */ @@ -13,8 +13,8 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" -#include "liblvgl/core/lv_obj.h" +#include "../../lv_conf_internal.h" +#include "../../core/lv_obj.h" #if LV_USE_CHECKBOX != 0 @@ -22,25 +22,7 @@ extern "C" { * DEFINES *********************/ -/********************** - * TYPEDEFS - **********************/ - -typedef struct { - lv_obj_t obj; - char * txt; - uint32_t static_txt : 1; -} lv_checkbox_t; - -extern const lv_obj_class_t lv_checkbox_class; - -/** - * `type` field in `lv_obj_draw_part_dsc_t` if `class_p = lv_checkbox_class` - * Used in `LV_EVENT_DRAW_PART_BEGIN` and `LV_EVENT_DRAW_PART_END` - */ -typedef enum { - LV_CHECKBOX_DRAW_PART_BOX, /**< The tick box*/ -} lv_checkbox_draw_part_type_t; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_checkbox_class; /********************** * GLOBAL PROTOTYPES @@ -60,7 +42,7 @@ lv_obj_t * lv_checkbox_create(lv_obj_t * parent); /** * Set the text of a check box. `txt` will be copied and may be deallocated * after this function returns. - * @param cb pointer to a check box + * @param obj pointer to a check box * @param txt the text of the check box. NULL to refresh with the current text. */ void lv_checkbox_set_text(lv_obj_t * obj, const char * txt); @@ -68,7 +50,7 @@ void lv_checkbox_set_text(lv_obj_t * obj, const char * txt); /** * Set the text of a check box. `txt` must not be deallocated during the life * of this checkbox. - * @param cb pointer to a check box + * @param obj pointer to a check box * @param txt the text of the check box. */ void lv_checkbox_set_text_static(lv_obj_t * obj, const char * txt); @@ -79,7 +61,7 @@ void lv_checkbox_set_text_static(lv_obj_t * obj, const char * txt); /** * Get the text of a check box - * @param cb pointer to check box object + * @param obj pointer to check box object * @return pointer to the text of the check box */ const char * lv_checkbox_get_text(const lv_obj_t * obj); diff --git a/include/liblvgl/widgets/checkbox/lv_checkbox_private.h b/include/liblvgl/widgets/checkbox/lv_checkbox_private.h new file mode 100644 index 00000000..a7167c9d --- /dev/null +++ b/include/liblvgl/widgets/checkbox/lv_checkbox_private.h @@ -0,0 +1,55 @@ +/** + * @file lv_checkbox_private.h + * + */ + +#ifndef LV_CHECKBOX_PRIVATE_H +#define LV_CHECKBOX_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_checkbox.h" + +#if LV_USE_CHECKBOX != 0 +#include "../../core/lv_obj_private.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_checkbox_t { + lv_obj_t obj; + char * txt; + uint32_t static_txt : 1; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_CHECKBOX != 0 */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_CHECKBOX_PRIVATE_H*/ diff --git a/include/liblvgl/widgets/lv_dropdown.h b/include/liblvgl/widgets/dropdown/lv_dropdown.h similarity index 82% rename from include/liblvgl/widgets/lv_dropdown.h rename to include/liblvgl/widgets/dropdown/lv_dropdown.h index 9848b145..c9f55be1 100644 --- a/include/liblvgl/widgets/lv_dropdown.h +++ b/include/liblvgl/widgets/dropdown/lv_dropdown.h @@ -13,7 +13,7 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" +#include "../../lv_conf_internal.h" #if LV_USE_DROPDOWN != 0 @@ -23,7 +23,7 @@ extern "C" { #error "lv_dropdown: lv_label is required. Enable it in lv_conf.h (LV_USE_LABEL 1)" #endif -#include "../widgets/lv_label.h" +#include "../label/lv_label.h" /********************* * DEFINES @@ -31,32 +31,24 @@ extern "C" { #define LV_DROPDOWN_POS_LAST 0xFFFF LV_EXPORT_CONST_INT(LV_DROPDOWN_POS_LAST); -/********************** - * TYPEDEFS - **********************/ +#if LV_USE_OBJ_PROPERTY +enum { + LV_PROPERTY_ID(DROPDOWN, TEXT, LV_PROPERTY_TYPE_TEXT, 0), + LV_PROPERTY_ID(DROPDOWN, OPTIONS, LV_PROPERTY_TYPE_TEXT, 1), + LV_PROPERTY_ID(DROPDOWN, OPTION_COUNT, LV_PROPERTY_TYPE_INT, 2), + LV_PROPERTY_ID(DROPDOWN, SELECTED, LV_PROPERTY_TYPE_INT, 3), + // LV_PROPERTY_ID(DROPDOWN, SELECTED_STR, LV_PROPERTY_TYPE_TEXT, 4), + LV_PROPERTY_ID(DROPDOWN, DIR, LV_PROPERTY_TYPE_INT, 5), + LV_PROPERTY_ID(DROPDOWN, SYMBOL, LV_PROPERTY_TYPE_TEXT, 6), + LV_PROPERTY_ID(DROPDOWN, SELECTED_HIGHLIGHT, LV_PROPERTY_TYPE_INT, 7), + LV_PROPERTY_ID(DROPDOWN, LIST, LV_PROPERTY_TYPE_OBJ, 8), + LV_PROPERTY_ID(DROPDOWN, IS_OPEN, LV_PROPERTY_TYPE_BOOL, 9), + LV_PROPERTY_DROPDOWN_END, +}; +#endif -typedef struct { - lv_obj_t obj; - lv_obj_t * list; /**< The dropped down list*/ - const char * text; /**< Text to display on the dropdown's button*/ - const void * symbol; /**< Arrow or other icon when the drop-down list is closed*/ - char * options; /**< Options in a '\n' separated list*/ - uint16_t option_cnt; /**< Number of options*/ - uint16_t sel_opt_id; /**< Index of the currently selected option*/ - uint16_t sel_opt_id_orig; /**< Store the original index on focus*/ - uint16_t pr_opt_id; /**< Index of the currently pressed option*/ - lv_dir_t dir : 4; /**< Direction in which the list should open*/ - uint8_t static_txt : 1; /**< 1: Only a pointer is saved in `options`*/ - uint8_t selected_highlight: 1; /**< 1: Make the selected option highlighted in the list*/ -} lv_dropdown_t; - -typedef struct { - lv_obj_t obj; - lv_obj_t * dropdown; -} lv_dropdown_list_t; - -extern const lv_obj_class_t lv_dropdown_class; -extern const lv_obj_class_t lv_dropdownlist_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_dropdown_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_dropdownlist_class; /********************** * GLOBAL PROTOTYPES @@ -117,7 +109,7 @@ void lv_dropdown_clear_options(lv_obj_t * obj); * @param obj pointer to drop-down list object * @param sel_opt id of the selected option (0 ... number of option - 1); */ -void lv_dropdown_set_selected(lv_obj_t * obj, uint16_t sel_opt); +void lv_dropdown_set_selected(lv_obj_t * obj, uint32_t sel_opt); /** * Set the direction of the a drop-down list @@ -172,14 +164,14 @@ const char * lv_dropdown_get_options(const lv_obj_t * obj); * @param obj pointer to drop-down list object * @return index of the selected option (0 ... number of option - 1); */ -uint16_t lv_dropdown_get_selected(const lv_obj_t * obj); +uint32_t lv_dropdown_get_selected(const lv_obj_t * obj); /** * Get the total number of options * @param obj pointer to drop-down list object * @return the total number of options in the list */ -uint16_t lv_dropdown_get_option_cnt(const lv_obj_t * obj); +uint32_t lv_dropdown_get_option_count(const lv_obj_t * obj); /** * Get the current selected option as a string @@ -224,7 +216,7 @@ lv_dir_t lv_dropdown_get_dir(const lv_obj_t * obj); /** * Open the drop.down list - * @param obj pointer to drop-down list object + * @param dropdown_obj pointer to drop-down list object */ void lv_dropdown_open(lv_obj_t * dropdown_obj); diff --git a/include/liblvgl/widgets/dropdown/lv_dropdown_private.h b/include/liblvgl/widgets/dropdown/lv_dropdown_private.h new file mode 100644 index 00000000..8229758b --- /dev/null +++ b/include/liblvgl/widgets/dropdown/lv_dropdown_private.h @@ -0,0 +1,69 @@ +/** + * @file lv_dropdown_private.h + * + */ + +#ifndef LV_DROPDOWN_PRIVATE_H +#define LV_DROPDOWN_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../core/lv_obj_private.h" +#include "lv_dropdown.h" + +#if LV_USE_DROPDOWN != 0 + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_dropdown_t { + lv_obj_t obj; + lv_obj_t * list; /**< The dropped down list*/ + const char * text; /**< Text to display on the dropdown's button*/ + const void * symbol; /**< Arrow or other icon when the drop-down list is closed*/ + char * options; /**< Options in a '\n' separated list*/ + uint32_t option_cnt; /**< Number of options*/ + uint32_t sel_opt_id; /**< Index of the currently selected option*/ + uint32_t sel_opt_id_orig; /**< Store the original index on focus*/ + uint32_t pr_opt_id; /**< Index of the currently pressed option*/ + uint8_t dir : 4; /**< Direction in which the list should open*/ + uint8_t static_txt : 1; /**< 1: Only a pointer is saved in `options`*/ + uint8_t selected_highlight: 1; /**< 1: Make the selected option highlighted in the list*/ +}; + +struct _lv_dropdown_list_t { + lv_obj_t obj; + lv_obj_t * dropdown; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_DROPDOWN != 0 */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_DROPDOWN_PRIVATE_H*/ diff --git a/include/liblvgl/widgets/image/lv_image.h b/include/liblvgl/widgets/image/lv_image.h new file mode 100644 index 00000000..abc52177 --- /dev/null +++ b/include/liblvgl/widgets/image/lv_image.h @@ -0,0 +1,310 @@ +/** + * @file lv_image.h + * + */ + +#ifndef LV_IMAGE_H +#define LV_IMAGE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../lv_conf_internal.h" + +#if LV_USE_IMAGE != 0 + +/*Testing of dependencies*/ +#if LV_USE_LABEL == 0 +#error "lv_img: lv_label is required. Enable it in lv_conf.h (LV_USE_LABEL 1)" +#endif + +#include "../../core/lv_obj.h" +#include "../../misc/lv_fs.h" +#include "../../draw/lv_draw.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_image_class; + +/** + * Image size mode, when image size and object size is different + */ +typedef enum { + LV_IMAGE_ALIGN_DEFAULT = 0, + LV_IMAGE_ALIGN_TOP_LEFT, + LV_IMAGE_ALIGN_TOP_MID, + LV_IMAGE_ALIGN_TOP_RIGHT, + LV_IMAGE_ALIGN_BOTTOM_LEFT, + LV_IMAGE_ALIGN_BOTTOM_MID, + LV_IMAGE_ALIGN_BOTTOM_RIGHT, + LV_IMAGE_ALIGN_LEFT_MID, + LV_IMAGE_ALIGN_RIGHT_MID, + LV_IMAGE_ALIGN_CENTER, + LV_IMAGE_ALIGN_AUTO_TRANSFORM, + LV_IMAGE_ALIGN_STRETCH, + LV_IMAGE_ALIGN_TILE, +} lv_image_align_t; + +#if LV_USE_OBJ_PROPERTY +enum { + LV_PROPERTY_ID(IMAGE, SRC, LV_PROPERTY_TYPE_IMGSRC, 0), + LV_PROPERTY_ID(IMAGE, OFFSET_X, LV_PROPERTY_TYPE_INT, 1), + LV_PROPERTY_ID(IMAGE, OFFSET_Y, LV_PROPERTY_TYPE_INT, 2), + LV_PROPERTY_ID(IMAGE, ROTATION, LV_PROPERTY_TYPE_INT, 3), + LV_PROPERTY_ID(IMAGE, PIVOT, LV_PROPERTY_TYPE_POINT, 4), + LV_PROPERTY_ID(IMAGE, SCALE, LV_PROPERTY_TYPE_INT, 5), + LV_PROPERTY_ID(IMAGE, SCALE_X, LV_PROPERTY_TYPE_INT, 6), + LV_PROPERTY_ID(IMAGE, SCALE_Y, LV_PROPERTY_TYPE_INT, 7), + LV_PROPERTY_ID(IMAGE, BLEND_MODE, LV_PROPERTY_TYPE_INT, 8), + LV_PROPERTY_ID(IMAGE, ANTIALIAS, LV_PROPERTY_TYPE_INT, 9), + LV_PROPERTY_ID(IMAGE, INNER_ALIGN, LV_PROPERTY_TYPE_INT, 10), + LV_PROPERTY_IMAGE_END, +}; +#endif + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create an image object + * @param parent pointer to an object, it will be the parent of the new image + * @return pointer to the created image + */ +lv_obj_t * lv_image_create(lv_obj_t * parent); + +/*===================== + * Setter functions + *====================*/ + +/** + * Set the image data to display on the object + * @param obj pointer to an image object + * @param src 1) pointer to an ::lv_image_dsc_t descriptor (converted by LVGL's image converter) (e.g. &my_img) or + * 2) path to an image file (e.g. "S:/dir/img.bin")or + * 3) a SYMBOL (e.g. LV_SYMBOL_OK) + */ +void lv_image_set_src(lv_obj_t * obj, const void * src); + +/** + * Set an offset for the source of an image so the image will be displayed from the new origin. + * @param obj pointer to an image + * @param x the new offset along x axis. + */ +void lv_image_set_offset_x(lv_obj_t * obj, int32_t x); + +/** + * Set an offset for the source of an image. + * so the image will be displayed from the new origin. + * @param obj pointer to an image + * @param y the new offset along y axis. + */ +void lv_image_set_offset_y(lv_obj_t * obj, int32_t y); + +/** + * Set the rotation angle of the image. + * The image will be rotated around the set pivot set by `lv_image_set_pivot()` + * Note that indexed and alpha only images can't be transformed. + * @param obj pointer to an image object + * @param angle rotation in degree with 0.1 degree resolution (0..3600: clock wise) + * @note if image_align is `LV_IMAGE_ALIGN_STRETCH` or `LV_IMAGE_ALIGN_FIT` + * rotation will be set to 0 automatically. + * + */ +void lv_image_set_rotation(lv_obj_t * obj, int32_t angle); + +/** + * Set the rotation center of the image. + * The image will be rotated around this point. + * x, y can be set with value of LV_PCT, lv_image_get_pivot will return the true pixel coordinate of pivot in this case. + * @param obj pointer to an image object + * @param x rotation center x of the image + * @param y rotation center y of the image + */ +void lv_image_set_pivot(lv_obj_t * obj, int32_t x, int32_t y); + +/** + * Set the zoom factor of the image. + * Note that indexed and alpha only images can't be transformed. + * @param obj pointer to an image object + * @param zoom the zoom factor. Example values: + * - 256 or LV_ZOOM_IMAGE_NONE: no zoom + * - <256: scale down + * - >256: scale up + * - 128: half size + * - 512: double size + */ +void lv_image_set_scale(lv_obj_t * obj, uint32_t zoom); + +/** + * Set the horizontal zoom factor of the image. + * Note that indexed and alpha only images can't be transformed. + * @param obj pointer to an image object + * @param zoom the zoom factor. Example values: + * - 256 or LV_ZOOM_IMAGE_NONE: no zoom + * - <256: scale down + * - >256: scale up + * - 128: half size + * - 512: double size + */ +void lv_image_set_scale_x(lv_obj_t * obj, uint32_t zoom); + +/** + * Set the vertical zoom factor of the image. + * Note that indexed and alpha only images can't be transformed. + * @param obj pointer to an image object + * @param zoom the zoom factor. Example values: + * - 256 or LV_ZOOM_IMAGE_NONE: no zoom + * - <256: scale down + * - >256: scale up + * - 128: half size + * - 512: double size + */ +void lv_image_set_scale_y(lv_obj_t * obj, uint32_t zoom); + +/** + * Set the blend mode of an image. + * @param obj pointer to an image object + * @param blend_mode the new blend mode + */ +void lv_image_set_blend_mode(lv_obj_t * obj, lv_blend_mode_t blend_mode); + +/** + * Enable/disable anti-aliasing for the transformations (rotate, zoom) or not. + * The quality is better with anti-aliasing looks better but slower. + * @param obj pointer to an image object + * @param antialias true: anti-aliased; false: not anti-aliased + */ +void lv_image_set_antialias(lv_obj_t * obj, bool antialias); + +/** + * Set the image object size mode. + * @param obj pointer to an image object + * @param align the new align mode. + * @note if image_align is `LV_IMAGE_ALIGN_STRETCH` or `LV_IMAGE_ALIGN_FIT` + * rotation, scale and pivot will be overwritten and controlled internally. + */ +void lv_image_set_inner_align(lv_obj_t * obj, lv_image_align_t align); + +/** + * Set an A8 bitmap mask for the image. + * @param obj pointer to an image object + * @param src an lv_image_dsc_t bitmap mask source. + */ +void lv_image_set_bitmap_map_src(lv_obj_t * obj, const lv_image_dsc_t * src); + +/*===================== + * Getter functions + *====================*/ + +/** + * Get the source of the image + * @param obj pointer to an image object + * @return the image source (symbol, file name or ::lv-img_dsc_t for C arrays) + */ +const void * lv_image_get_src(lv_obj_t * obj); + +/** + * Get the offset's x attribute of the image object. + * @param obj pointer to an image + * @return offset X value. + */ +int32_t lv_image_get_offset_x(lv_obj_t * obj); + +/** + * Get the offset's y attribute of the image object. + * @param obj pointer to an image + * @return offset Y value. + */ +int32_t lv_image_get_offset_y(lv_obj_t * obj); + +/** + * Get the rotation of the image. + * @param obj pointer to an image object + * @return rotation in 0.1 degrees (0..3600) + * @note if image_align is `LV_IMAGE_ALIGN_STRETCH` or `LV_IMAGE_ALIGN_FIT` + * rotation will be set to 0 automatically. + */ +int32_t lv_image_get_rotation(lv_obj_t * obj); + +/** + * Get the pivot (rotation center) of the image. + * If pivot is set with LV_PCT, convert it to px before return. + * @param obj pointer to an image object + * @param pivot store the rotation center here + */ +void lv_image_get_pivot(lv_obj_t * obj, lv_point_t * pivot); + +/** + * Get the zoom factor of the image. + * @param obj pointer to an image object + * @return zoom factor (256: no zoom) + */ +int32_t lv_image_get_scale(lv_obj_t * obj); + +/** + * Get the horizontal zoom factor of the image. + * @param obj pointer to an image object + * @return zoom factor (256: no zoom) + */ +int32_t lv_image_get_scale_x(lv_obj_t * obj); + +/** + * Get the vertical zoom factor of the image. + * @param obj pointer to an image object + * @return zoom factor (256: no zoom) + */ +int32_t lv_image_get_scale_y(lv_obj_t * obj); + +/** + * Get the current blend mode of the image + * @param obj pointer to an image object + * @return the current blend mode + */ +lv_blend_mode_t lv_image_get_blend_mode(lv_obj_t * obj); + +/** + * Get whether the transformations (rotate, zoom) are anti-aliased or not + * @param obj pointer to an image object + * @return true: anti-aliased; false: not anti-aliased + */ +bool lv_image_get_antialias(lv_obj_t * obj); + +/** + * Get the size mode of the image + * @param obj pointer to an image object + * @return element of `lv_image_align_t` + */ +lv_image_align_t lv_image_get_inner_align(lv_obj_t * obj); + +/** + * Get the bitmap mask source. + * @param obj pointer to an image object + * @return an lv_image_dsc_t bitmap mask source. + */ +const lv_image_dsc_t * lv_image_get_bitmap_map_src(lv_obj_t * obj); + +/********************** + * MACROS + **********************/ + +/** Use this macro to declare an image in a C file*/ +#define LV_IMAGE_DECLARE(var_name) extern const lv_image_dsc_t var_name + +#endif /*LV_USE_IMAGE*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_IMAGE_H*/ diff --git a/include/liblvgl/widgets/image/lv_image_private.h b/include/liblvgl/widgets/image/lv_image_private.h new file mode 100644 index 00000000..c13acf4e --- /dev/null +++ b/include/liblvgl/widgets/image/lv_image_private.h @@ -0,0 +1,66 @@ +/** + * @file lv_image_private.h + * + */ + +#ifndef LV_IMAGE_PRIVATE_H +#define LV_IMAGE_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../core/lv_obj_private.h" +#include "lv_image.h" + +#if LV_USE_IMAGE != 0 + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** + * Data of image + */ +struct _lv_image_t { + lv_obj_t obj; + const void * src; /**< Image source: Pointer to an array or a file or a symbol*/ + const lv_image_dsc_t * bitmap_mask_src; /**< Pointer to an A8 bitmap mask */ + lv_point_t offset; + int32_t w; /**< Width of the image (Handled by the library)*/ + int32_t h; /**< Height of the image (Handled by the library)*/ + uint32_t rotation; /**< Rotation angle of the image*/ + uint32_t scale_x; /**< 256 means no zoom, 512 double size, 128 half size*/ + uint32_t scale_y; /**< 256 means no zoom, 512 double size, 128 half size*/ + lv_point_t pivot; /**< Rotation center of the image*/ + uint32_t src_type : 2; /**< See: lv_image_src_t*/ + uint32_t cf : 5; /**< Color format from `lv_color_format_t`*/ + uint32_t antialias : 1; /**< Apply anti-aliasing in transformations (rotate, zoom)*/ + uint32_t align: 4; /**< Image size mode when image size and object size is different. See lv_image_align_t*/ + uint32_t blend_mode: 4; /**< Element of `lv_blend_mode_t`*/ +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_IMAGE != 0 */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_IMAGE_PRIVATE_H*/ diff --git a/include/liblvgl/widgets/imagebutton/lv_imagebutton.h b/include/liblvgl/widgets/imagebutton/lv_imagebutton.h new file mode 100644 index 00000000..ed6dc3a5 --- /dev/null +++ b/include/liblvgl/widgets/imagebutton/lv_imagebutton.h @@ -0,0 +1,121 @@ +/** + * @file lv_imagebutton.h + * + */ + +#ifndef LV_IMAGEBUTTON_H +#define LV_IMAGEBUTTON_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../core/lv_obj.h" + +#if LV_USE_IMAGEBUTTON != 0 + +/********************* + * DEFINES + *********************/ +typedef enum { + LV_IMAGEBUTTON_STATE_RELEASED, + LV_IMAGEBUTTON_STATE_PRESSED, + LV_IMAGEBUTTON_STATE_DISABLED, + LV_IMAGEBUTTON_STATE_CHECKED_RELEASED, + LV_IMAGEBUTTON_STATE_CHECKED_PRESSED, + LV_IMAGEBUTTON_STATE_CHECKED_DISABLED, + LV_IMAGEBUTTON_STATE_NUM, +} lv_imagebutton_state_t; + +/********************** + * TYPEDEFS + **********************/ +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_imagebutton_class; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create an image button object + * @param parent pointer to an object, it will be the parent of the new image button + * @return pointer to the created image button + */ +lv_obj_t * lv_imagebutton_create(lv_obj_t * parent); + +/*====================== + * Add/remove functions + *=====================*/ + +/*===================== + * Setter functions + *====================*/ + +/** + * Set images for a state of the image button + * @param imagebutton pointer to an image button object + * @param state for which state set the new image + * @param src_left pointer to an image source for the left side of the button (a C array or path to + * a file) + * @param src_mid pointer to an image source for the middle of the button (ideally 1px wide) (a C + * array or path to a file) + * @param src_right pointer to an image source for the right side of the button (a C array or path + * to a file) + */ +void lv_imagebutton_set_src(lv_obj_t * imagebutton, lv_imagebutton_state_t state, const void * src_left, + const void * src_mid, + const void * src_right); + +/** + * Use this function instead of `lv_obj_add/remove_state` to set a state manually + * @param imagebutton pointer to an image button object + * @param state the new state + */ +void lv_imagebutton_set_state(lv_obj_t * imagebutton, lv_imagebutton_state_t state); + +/*===================== + * Getter functions + *====================*/ + +/** + * Get the left image in a given state + * @param imagebutton pointer to an image button object + * @param state the state where to get the image (from `lv_button_state_t`) ` + * @return pointer to the left image source (a C array or path to a file) + */ +const void * lv_imagebutton_get_src_left(lv_obj_t * imagebutton, lv_imagebutton_state_t state); + +/** + * Get the middle image in a given state + * @param imagebutton pointer to an image button object + * @param state the state where to get the image (from `lv_button_state_t`) ` + * @return pointer to the middle image source (a C array or path to a file) + */ +const void * lv_imagebutton_get_src_middle(lv_obj_t * imagebutton, lv_imagebutton_state_t state); + +/** + * Get the right image in a given state + * @param imagebutton pointer to an image button object + * @param state the state where to get the image (from `lv_button_state_t`) ` + * @return pointer to the left image source (a C array or path to a file) + */ +const void * lv_imagebutton_get_src_right(lv_obj_t * imagebutton, lv_imagebutton_state_t state); + +/*===================== + * Other functions + *====================*/ + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_IMAGEBUTTON*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_IMAGEBUTTON_H*/ diff --git a/include/liblvgl/widgets/imagebutton/lv_imagebutton_private.h b/include/liblvgl/widgets/imagebutton/lv_imagebutton_private.h new file mode 100644 index 00000000..107f49df --- /dev/null +++ b/include/liblvgl/widgets/imagebutton/lv_imagebutton_private.h @@ -0,0 +1,58 @@ +/** + * @file lv_imagebutton_private.h + * + */ + +#ifndef LV_IMAGEBUTTON_PRIVATE_H +#define LV_IMAGEBUTTON_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_imagebutton.h" + +#if LV_USE_IMAGEBUTTON != 0 +#include "../../core/lv_obj_private.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_imagebutton_src_info_t { + const void * img_src; + lv_image_header_t header; +}; + +/** Data of image button */ +struct _lv_imagebutton_t { + lv_obj_t obj; + lv_imagebutton_src_info_t src_mid[LV_IMAGEBUTTON_STATE_NUM]; /**< Store center images to each state */ + lv_imagebutton_src_info_t src_left[LV_IMAGEBUTTON_STATE_NUM]; /**< Store left side images to each state */ + lv_imagebutton_src_info_t src_right[LV_IMAGEBUTTON_STATE_NUM]; /**< Store right side images to each state */ +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_IMAGEBUTTON != 0 */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_IMAGEBUTTON_PRIVATE_H*/ diff --git a/include/liblvgl/extra/widgets/keyboard/lv_keyboard.h b/include/liblvgl/widgets/keyboard/lv_keyboard.h similarity index 51% rename from include/liblvgl/extra/widgets/keyboard/lv_keyboard.h rename to include/liblvgl/widgets/keyboard/lv_keyboard.h index 64356a33..f177c184 100644 --- a/include/liblvgl/extra/widgets/keyboard/lv_keyboard.h +++ b/include/liblvgl/widgets/keyboard/lv_keyboard.h @@ -13,30 +13,30 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/widgets/lv_btnmatrix.h" +#include "../buttonmatrix/lv_buttonmatrix.h" #if LV_USE_KEYBOARD /*Testing of dependencies*/ -#if LV_USE_BTNMATRIX == 0 -#error "lv_kb: lv_btnm is required. Enable it in lv_conf.h (LV_USE_BTNMATRIX 1) " +#if LV_USE_BUTTONMATRIX == 0 +#error "lv_buttonmatrix is required. Enable it in lv_conf.h (LV_USE_BUTTONMATRIX 1) " #endif #if LV_USE_TEXTAREA == 0 -#error "lv_kb: lv_ta is required. Enable it in lv_conf.h (LV_USE_TEXTAREA 1) " +#error "lv_textarea is required. Enable it in lv_conf.h (LV_USE_TEXTAREA 1) " #endif /********************* * DEFINES *********************/ -#define LV_KEYBOARD_CTRL_BTN_FLAGS (LV_BTNMATRIX_CTRL_NO_REPEAT | LV_BTNMATRIX_CTRL_CLICK_TRIG | LV_BTNMATRIX_CTRL_CHECKED) +#define LV_KEYBOARD_CTRL_BUTTON_FLAGS (LV_BUTTONMATRIX_CTRL_NO_REPEAT | LV_BUTTONMATRIX_CTRL_CLICK_TRIG | LV_BUTTONMATRIX_CTRL_CHECKED) /********************** * TYPEDEFS **********************/ /** Current keyboard mode.*/ -enum { +typedef enum { LV_KEYBOARD_MODE_TEXT_LOWER, LV_KEYBOARD_MODE_TEXT_UPPER, LV_KEYBOARD_MODE_SPECIAL, @@ -45,18 +45,22 @@ enum { LV_KEYBOARD_MODE_USER_2, LV_KEYBOARD_MODE_USER_3, LV_KEYBOARD_MODE_USER_4, -}; -typedef uint8_t lv_keyboard_mode_t; +#if LV_USE_ARABIC_PERSIAN_CHARS == 1 + LV_KEYBOARD_MODE_TEXT_ARABIC +#endif +} lv_keyboard_mode_t; -/*Data of keyboard*/ -typedef struct { - lv_btnmatrix_t btnm; - lv_obj_t * ta; /*Pointer to the assigned text area*/ - lv_keyboard_mode_t mode; /*Key map type*/ - uint8_t popovers : 1; /*Show button titles in popovers on press*/ -} lv_keyboard_t; +#if LV_USE_OBJ_PROPERTY +enum { + LV_PROPERTY_ID(KEYBOARD, TEXTAREA, LV_PROPERTY_TYPE_OBJ, 0), + LV_PROPERTY_ID(KEYBOARD, MODE, LV_PROPERTY_TYPE_INT, 1), + LV_PROPERTY_ID(KEYBOARD, POPOVERS, LV_PROPERTY_TYPE_INT, 2), + LV_PROPERTY_ID(KEYBOARD, SELECTED_BUTTON, LV_PROPERTY_TYPE_INT, 3), + LV_PROPERTY_KEYBOARD_END, +}; +#endif -extern const lv_obj_class_t lv_keyboard_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_keyboard_class; /********************** * GLOBAL PROTOTYPES @@ -64,8 +68,8 @@ extern const lv_obj_class_t lv_keyboard_class; /** * Create a Keyboard object - * @param parent pointer to an object, it will be the parent of the new keyboard - * @return pointer to the created keyboard + * @param parent pointer to an object, it will be the parent of the new keyboard + * @return pointer to the created keyboard */ lv_obj_t * lv_keyboard_create(lv_obj_t * parent); @@ -75,34 +79,36 @@ lv_obj_t * lv_keyboard_create(lv_obj_t * parent); /** * Assign a Text Area to the Keyboard. The pressed characters will be put there. - * @param kb pointer to a Keyboard object - * @param ta pointer to a Text Area object to write there + * @param kb pointer to a Keyboard object + * @param ta pointer to a Text Area object to write there */ void lv_keyboard_set_textarea(lv_obj_t * kb, lv_obj_t * ta); /** * Set a new a mode (text or number map) - * @param kb pointer to a Keyboard object - * @param mode the mode from 'lv_keyboard_mode_t' + * @param kb pointer to a Keyboard object + * @param mode the mode from 'lv_keyboard_mode_t' */ void lv_keyboard_set_mode(lv_obj_t * kb, lv_keyboard_mode_t mode); /** * Show the button title in a popover when pressed. - * @param kb pointer to a Keyboard object - * @param en whether "popovers" mode is enabled + * @param kb pointer to a Keyboard object + * @param en whether "popovers" mode is enabled */ void lv_keyboard_set_popovers(lv_obj_t * kb, bool en); /** * Set a new map for the keyboard - * @param kb pointer to a Keyboard object - * @param mode keyboard map to alter 'lv_keyboard_mode_t' - * @param map pointer to a string array to describe the map. - * See 'lv_btnmatrix_set_map()' for more info. + * @param kb pointer to a Keyboard object + * @param mode keyboard map to alter 'lv_keyboard_mode_t' + * @param map pointer to a string array to describe the map. + * See 'lv_buttonmatrix_set_map()' for more info. + * @param ctrl_map See 'lv_buttonmatrix_set_ctrl_map()' for more info. + */ -void lv_keyboard_set_map(lv_obj_t * kb, lv_keyboard_mode_t mode, const char * map[], - const lv_btnmatrix_ctrl_t ctrl_map[]); +void lv_keyboard_set_map(lv_obj_t * kb, lv_keyboard_mode_t mode, const char * const map[], + const lv_buttonmatrix_ctrl_t ctrl_map[]); /*===================== * Getter functions @@ -110,45 +116,39 @@ void lv_keyboard_set_map(lv_obj_t * kb, lv_keyboard_mode_t mode, const char * ma /** * Assign a Text Area to the Keyboard. The pressed characters will be put there. - * @param kb pointer to a Keyboard object - * @return pointer to the assigned Text Area object + * @param kb pointer to a Keyboard object + * @return pointer to the assigned Text Area object */ lv_obj_t * lv_keyboard_get_textarea(const lv_obj_t * kb); /** * Set a new a mode (text or number map) - * @param kb pointer to a Keyboard object - * @return the current mode from 'lv_keyboard_mode_t' + * @param kb pointer to a Keyboard object + * @return the current mode from 'lv_keyboard_mode_t' */ lv_keyboard_mode_t lv_keyboard_get_mode(const lv_obj_t * kb); /** * Tell whether "popovers" mode is enabled or not. - * @param kb pointer to a Keyboard object - * @return true: "popovers" mode is enabled; false: disabled + * @param obj pointer to a Keyboard object + * @return true: "popovers" mode is enabled; false: disabled */ -bool lv_btnmatrix_get_popovers(const lv_obj_t * obj); +bool lv_keyboard_get_popovers(const lv_obj_t * obj); /** * Get the current map of a keyboard - * @param kb pointer to a keyboard object - * @return the current map + * @param kb pointer to a keyboard object + * @return the current map */ -static inline const char ** lv_keyboard_get_map_array(const lv_obj_t * kb) -{ - return lv_btnmatrix_get_map(kb); -} +const char * const * lv_keyboard_get_map_array(const lv_obj_t * kb); /** * Get the index of the lastly "activated" button by the user (pressed, released, focused etc) * Useful in the `event_cb` to get the text of the button, check if hidden etc. * @param obj pointer to button matrix object - * @return index of the last released button (LV_BTNMATRIX_BTN_NONE: if unset) + * @return index of the last released button (LV_BUTTONMATRIX_BUTTON_NONE: if unset) */ -static inline uint16_t lv_keyboard_get_selected_btn(const lv_obj_t * obj) -{ - return lv_btnmatrix_get_selected_btn(obj); -} +uint32_t lv_keyboard_get_selected_button(const lv_obj_t * obj); /** * Get the button's text @@ -156,10 +156,7 @@ static inline uint16_t lv_keyboard_get_selected_btn(const lv_obj_t * obj) * @param btn_id the index a button not counting new line characters. * @return text of btn_index` button */ -static inline const char * lv_keyboard_get_btn_text(const lv_obj_t * obj, uint16_t btn_id) -{ - return lv_btnmatrix_get_btn_text(obj, btn_id); -} +const char * lv_keyboard_get_button_text(const lv_obj_t * obj, uint32_t btn_id); /*===================== * Other functions @@ -169,8 +166,7 @@ static inline const char * lv_keyboard_get_btn_text(const lv_obj_t * obj, uint16 * Default keyboard event to add characters to the Text area and change the map. * If a custom `event_cb` is added to the keyboard this function can be called from it to handle the * button clicks - * @param kb pointer to a keyboard - * @param event the triggering event + * @param e the triggering event */ void lv_keyboard_def_event_cb(lv_event_t * e); diff --git a/include/liblvgl/widgets/keyboard/lv_keyboard_private.h b/include/liblvgl/widgets/keyboard/lv_keyboard_private.h new file mode 100644 index 00000000..3de02734 --- /dev/null +++ b/include/liblvgl/widgets/keyboard/lv_keyboard_private.h @@ -0,0 +1,53 @@ +/** + * @file lv_keyboard_private.h + * + */ + +#ifndef LV_KEYBOARD_PRIVATE_H +#define LV_KEYBOARD_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../buttonmatrix/lv_buttonmatrix_private.h" +#include "lv_keyboard.h" + +#if LV_USE_KEYBOARD + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** Data of keyboard */ +struct _lv_keyboard_t { + lv_buttonmatrix_t btnm; + lv_obj_t * ta; /**< Pointer to the assigned text area */ + lv_keyboard_mode_t mode; /**< Key map type */ + uint8_t popovers : 1; /**< Show button titles in popovers on press */ +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_KEYBOARD */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_KEYBOARD_PRIVATE_H*/ diff --git a/include/liblvgl/widgets/lv_label.h b/include/liblvgl/widgets/label/lv_label.h similarity index 74% rename from include/liblvgl/widgets/lv_label.h rename to include/liblvgl/widgets/label/lv_label.h index 39a68db0..d9fddd81 100644 --- a/include/liblvgl/widgets/lv_label.h +++ b/include/liblvgl/widgets/label/lv_label.h @@ -13,24 +13,28 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "../lv_conf_internal.h" +#include "../../lv_conf_internal.h" #if LV_USE_LABEL != 0 -#include -#include "liblvgl/core/lv_obj.h" -#include "liblvgl/font/lv_font.h" -#include "liblvgl/font/lv_symbol_def.h" -#include "liblvgl/misc/lv_txt.h" -#include "liblvgl/draw/lv_draw.h" +#include "../../misc/lv_types.h" +#include "../../core/lv_obj.h" +#include "../../font/lv_font.h" +#include "../../font/lv_symbol_def.h" +#include "../../misc/lv_text.h" +#include "../../draw/lv_draw.h" /********************* * DEFINES *********************/ -#define LV_LABEL_WAIT_CHAR_COUNT 3 #define LV_LABEL_DOT_NUM 3 #define LV_LABEL_POS_LAST 0xFFFF #define LV_LABEL_TEXT_SELECTION_OFF LV_DRAW_LABEL_NO_TXT_SEL +#if LV_WIDGETS_HAS_DEFAULT_VALUE +#define LV_LABEL_DEFAULT_TEXT "Text" +#else +#define LV_LABEL_DEFAULT_TEXT "" +#endif LV_EXPORT_CONST_INT(LV_LABEL_DOT_NUM); LV_EXPORT_CONST_INT(LV_LABEL_POS_LAST); @@ -41,42 +45,25 @@ LV_EXPORT_CONST_INT(LV_LABEL_TEXT_SELECTION_OFF); **********************/ /** Long mode behaviors. Used in 'lv_label_ext_t'*/ -enum { - LV_LABEL_LONG_WRAP, /**< Keep the object width, wrap the too long lines and expand the object height*/ +typedef enum { + LV_LABEL_LONG_WRAP, /**< Keep the object width, wrap lines longer than object width and expand the object height*/ LV_LABEL_LONG_DOT, /**< Keep the size and write dots at the end if the text is too long*/ LV_LABEL_LONG_SCROLL, /**< Keep the size and roll the text back and forth*/ LV_LABEL_LONG_SCROLL_CIRCULAR, /**< Keep the size and roll the text circularly*/ LV_LABEL_LONG_CLIP, /**< Keep the size and clip the text out of it*/ -}; -typedef uint8_t lv_label_long_mode_t; - -typedef struct { - lv_obj_t obj; - char * text; - union { - char * tmp_ptr; /*Pointer to the allocated memory containing the character replaced by dots*/ - char tmp[LV_LABEL_DOT_NUM + 1]; /*Directly store the characters if <=4 characters*/ - } dot; - uint32_t dot_end; /*The real text length, used in dot mode*/ - -#if LV_LABEL_LONG_TXT_HINT - lv_draw_label_hint_t hint; -#endif +} lv_label_long_mode_t; -#if LV_LABEL_TEXT_SELECTION - uint32_t sel_start; - uint32_t sel_end; +#if LV_USE_OBJ_PROPERTY +enum { + LV_PROPERTY_ID(LABEL, TEXT, LV_PROPERTY_TYPE_TEXT, 0), + LV_PROPERTY_ID(LABEL, LONG_MODE, LV_PROPERTY_TYPE_INT, 1), + LV_PROPERTY_ID(LABEL, TEXT_SELECTION_START, LV_PROPERTY_TYPE_INT, 2), + LV_PROPERTY_ID(LABEL, TEXT_SELECTION_END, LV_PROPERTY_TYPE_INT, 3), + LV_PROPERTY_LABEL_END, +}; #endif - lv_point_t offset; /*Text draw position offset*/ - lv_label_long_mode_t long_mode : 3; /*Determine what to do with the long texts*/ - uint8_t static_txt : 1; /*Flag to indicate the text is static*/ - uint8_t recolor : 1; /*Enable in-line letter re-coloring*/ - uint8_t expand : 1; /*Ignore real width (used by the library with LV_LABEL_LONG_SCROLL)*/ - uint8_t dot_tmp_alloc : 1; /*1: dot is allocated, 0: dot directly holds up to 4 chars*/ -} lv_label_t; - -extern const lv_obj_class_t lv_label_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_label_class; /********************** * GLOBAL PROTOTYPES @@ -104,7 +91,11 @@ void lv_label_set_text(lv_obj_t * obj, const char * text); * Set a new formatted text for a label. Memory will be allocated to store the text by the label. * @param obj pointer to a label object * @param fmt `printf`-like format - * @example lv_label_set_text_fmt(label1, "%d user", user_num); + * + * Example: + * @code + * lv_label_set_text_fmt(label1, "%d user", user_num); + * @endcode */ void lv_label_set_text_fmt(lv_obj_t * obj, const char * fmt, ...) LV_FORMAT_ATTRIBUTE(2, 3); @@ -117,34 +108,34 @@ void lv_label_set_text_fmt(lv_obj_t * obj, const char * fmt, ...) LV_FORMAT_ATTR void lv_label_set_text_static(lv_obj_t * obj, const char * text); /** - * Set the behavior of the label with longer text then the object size + * Set the behavior of the label with text longer than the object size * @param obj pointer to a label object * @param long_mode the new mode from 'lv_label_long_mode' enum. * In LV_LONG_WRAP/DOT/SCROLL/SCROLL_CIRC the size of the label should be set AFTER this function */ void lv_label_set_long_mode(lv_obj_t * obj, lv_label_long_mode_t long_mode); -/** - * Enable the recoloring by in-line commands - * @param obj pointer to a label object - * @param en true: enable recoloring, false: disable - * @example "This is a #ff0000 red# word" - */ -void lv_label_set_recolor(lv_obj_t * obj, bool en); - /** * Set where text selection should start * @param obj pointer to a label object * @param index character index from where selection should start. `LV_LABEL_TEXT_SELECTION_OFF` for no selection */ -void lv_label_set_text_sel_start(lv_obj_t * obj, uint32_t index); +void lv_label_set_text_selection_start(lv_obj_t * obj, uint32_t index); /** * Set where text selection should end * @param obj pointer to a label object * @param index character index where selection should end. `LV_LABEL_TEXT_SELECTION_OFF` for no selection */ -void lv_label_set_text_sel_end(lv_obj_t * obj, uint32_t index); +void lv_label_set_text_selection_end(lv_obj_t * obj, uint32_t index); + +/** + * Enable the recoloring by in-line commands + * @param obj pointer to a label object + * @param en true: enable recoloring, false: disable + * @example "This is a #ff0000 red# word" + */ +void lv_label_set_recolor(lv_obj_t * obj, bool en); /*===================== * Getter functions @@ -164,17 +155,10 @@ char * lv_label_get_text(const lv_obj_t * obj); */ lv_label_long_mode_t lv_label_get_long_mode(const lv_obj_t * obj); -/** - * Get the recoloring attribute - * @param obj pointer to a label object - * @return true: recoloring is enabled, false: disable - */ -bool lv_label_get_recolor(const lv_obj_t * obj); - /** * Get the relative x and y coordinates of a letter * @param obj pointer to a label object - * @param index index of the character [0 ... text length - 1]. + * @param char_id index of the character [0 ... text length - 1]. * Expressed in character index, not byte index (different in UTF-8) * @param pos store the result here (E.g. index = 0 gives 0;0 coordinates if the text if aligned to the left) */ @@ -183,11 +167,12 @@ void lv_label_get_letter_pos(const lv_obj_t * obj, uint32_t char_id, lv_point_t /** * Get the index of letter on a relative point of a label. * @param obj pointer to label object - * @param pos pointer to point with coordinates on a the label + * @param pos_in pointer to point with coordinates on a the label + * @param bidi whether to use bidi processed * @return The index of the letter on the 'pos_p' point (E.g. on 0;0 is the 0. letter if aligned to the left) * Expressed in character index and not byte index (different in UTF-8) */ -uint32_t lv_label_get_letter_on(const lv_obj_t * obj, lv_point_t * pos_in); +uint32_t lv_label_get_letter_on(const lv_obj_t * obj, lv_point_t * pos_in, bool bidi); /** * Check if a character is drawn under a point. @@ -211,12 +196,19 @@ uint32_t lv_label_get_text_selection_start(const lv_obj_t * obj); */ uint32_t lv_label_get_text_selection_end(const lv_obj_t * obj); +/** + * @brief Get the recoloring attribute + * @param obj pointer to a label object. + * @return true: recoloring is enabled, false: recoloring is disabled + */ +bool lv_label_get_recolor(const lv_obj_t * obj); + /*===================== * Other functions *====================*/ /** - * Insert a text to a label. The label text can not be static. + * Insert a text to a label. The label text cannot be static. * @param obj pointer to a label object * @param pos character index to insert. Expressed in character index and not byte index. * 0: before first char. LV_LABEL_POS_LAST: after last char. @@ -225,10 +217,10 @@ uint32_t lv_label_get_text_selection_end(const lv_obj_t * obj); void lv_label_ins_text(lv_obj_t * obj, uint32_t pos, const char * txt); /** - * Delete characters from a label. The label text can not be static. + * Delete characters from a label. The label text cannot be static. * @param obj pointer to a label object * @param pos character index from where to cut. Expressed in character index and not byte index. - * 0: start in from of the first character + * 0: start in front of the first character * @param cnt number of characters to cut */ void lv_label_cut_text(lv_obj_t * obj, uint32_t pos, uint32_t cnt); diff --git a/include/liblvgl/widgets/label/lv_label_private.h b/include/liblvgl/widgets/label/lv_label_private.h new file mode 100644 index 00000000..d3c3cc88 --- /dev/null +++ b/include/liblvgl/widgets/label/lv_label_private.h @@ -0,0 +1,74 @@ +/** + * @file lv_label_private.h + * + */ + +#ifndef LV_LABEL_PRIVATE_H +#define LV_LABEL_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../draw/lv_draw_label_private.h" +#include "../../core/lv_obj_private.h" +#include "lv_label.h" + +#if LV_USE_LABEL != 0 + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_label_t { + lv_obj_t obj; + char * text; + union { + char * tmp_ptr; /**< Pointer to the allocated memory containing the character replaced by dots */ + char tmp[LV_LABEL_DOT_NUM + 1]; /**< Directly store the characters if <=4 characters */ + } dot; + uint32_t dot_end; /**< The real text length, used in dot mode */ + +#if LV_LABEL_LONG_TXT_HINT + lv_draw_label_hint_t hint; +#endif + +#if LV_LABEL_TEXT_SELECTION + uint32_t sel_start; + uint32_t sel_end; +#endif + + lv_point_t size_cache; /**< Text size cache */ + lv_point_t offset; /**< Text draw position offset */ + lv_label_long_mode_t long_mode : 3; /**< Determine what to do with the long texts */ + uint8_t static_txt : 1; /**< Flag to indicate the text is static */ + uint8_t recolor : 1; /**< Enable in-line letter re-coloring*/ + uint8_t expand : 1; /**< Ignore real width (used by the library with LV_LABEL_LONG_SCROLL) */ + uint8_t dot_tmp_alloc : 1; /**< 1: dot is allocated, 0: dot directly holds up to 4 chars */ + uint8_t invalid_size_cache : 1; /**< 1: Recalculate size and update cache */ +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_LABEL != 0 */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_LABEL_PRIVATE_H*/ diff --git a/include/liblvgl/extra/widgets/led/lv_led.h b/include/liblvgl/widgets/led/lv_led.h similarity index 58% rename from include/liblvgl/extra/widgets/led/lv_led.h rename to include/liblvgl/widgets/led/lv_led.h index f188b8e9..c085a97f 100644 --- a/include/liblvgl/extra/widgets/led/lv_led.h +++ b/include/liblvgl/widgets/led/lv_led.h @@ -13,11 +13,10 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lvgl.h" +#include "../../core/lv_obj.h" #if LV_USE_LED - /********************* * DEFINES *********************/ @@ -35,22 +34,7 @@ extern "C" { * TYPEDEFS **********************/ -/*Data of led*/ -typedef struct { - lv_obj_t obj; - lv_color_t color; - uint8_t bright; /**< Current brightness of the LED (0..255)*/ -} lv_led_t; - -extern const lv_obj_class_t lv_led_class; - -/** - * `type` field in `lv_obj_draw_part_dsc_t` if `class_p = lv_led_class` - * Used in `LV_EVENT_DRAW_PART_BEGIN` and `LV_EVENT_DRAW_PART_END` - */ -typedef enum { - LV_LED_DRAW_PART_RECTANGLE, /**< The main rectangle*/ -} lv_led_draw_part_type_t; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_led_class; /********************** * GLOBAL PROTOTYPES @@ -58,8 +42,8 @@ typedef enum { /** * Create a led object - * @param parent pointer to an object, it will be the parent of the new led - * @return pointer to the created led + * @param parent pointer to an object, it will be the parent of the new led + * @return pointer to the created led */ lv_obj_t * lv_led_create(lv_obj_t * parent); @@ -72,33 +56,33 @@ void lv_led_set_color(lv_obj_t * led, lv_color_t color); /** * Set the brightness of a LED object - * @param led pointer to a LED object - * @param bright LV_LED_BRIGHT_MIN (max. dark) ... LV_LED_BRIGHT_MAX (max. light) + * @param led pointer to a LED object + * @param bright LV_LED_BRIGHT_MIN (max. dark) ... LV_LED_BRIGHT_MAX (max. light) */ void lv_led_set_brightness(lv_obj_t * led, uint8_t bright); /** * Light on a LED - * @param led pointer to a LED object + * @param led pointer to a LED object */ void lv_led_on(lv_obj_t * led); /** * Light off a LED - * @param led pointer to a LED object + * @param led pointer to a LED object */ void lv_led_off(lv_obj_t * led); /** * Toggle the state of a LED - * @param led pointer to a LED object + * @param led pointer to a LED object */ void lv_led_toggle(lv_obj_t * led); /** - * Get the brightness of a LEd object - * @param led pointer to LED object - * @return bright 0 (max. dark) ... 255 (max. light) + * Get the brightness of a LED object + * @param obj pointer to LED object + * @return bright 0 (max. dark) ... 255 (max. light) */ uint8_t lv_led_get_brightness(const lv_obj_t * obj); @@ -112,5 +96,4 @@ uint8_t lv_led_get_brightness(const lv_obj_t * obj); } /*extern "C"*/ #endif - #endif /*LV_LED_H*/ diff --git a/include/liblvgl/widgets/led/lv_led_private.h b/include/liblvgl/widgets/led/lv_led_private.h new file mode 100644 index 00000000..77b5490f --- /dev/null +++ b/include/liblvgl/widgets/led/lv_led_private.h @@ -0,0 +1,52 @@ +/** + * @file lv_led_private.h + * + */ + +#ifndef LV_LED_PRIVATE_H +#define LV_LED_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_led.h" + +#if LV_USE_LED +#include "../../core/lv_obj_private.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** Data of led */ +struct _lv_led_t { + lv_obj_t obj; + lv_color_t color; + uint8_t bright; /**< Current brightness of the LED (0..255)*/ +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_LED */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_LED_PRIVATE_H*/ diff --git a/include/liblvgl/widgets/lv_line.h b/include/liblvgl/widgets/line/lv_line.h similarity index 51% rename from include/liblvgl/widgets/lv_line.h rename to include/liblvgl/widgets/line/lv_line.h index 622ef810..d6a1d603 100644 --- a/include/liblvgl/widgets/lv_line.h +++ b/include/liblvgl/widgets/line/lv_line.h @@ -13,12 +13,9 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" - +#include "../../core/lv_obj.h" #if LV_USE_LINE != 0 -#include "liblvgl/core/lv_obj.h" - /********************* * DEFINES *********************/ @@ -27,15 +24,7 @@ extern "C" { * TYPEDEFS **********************/ -/*Data of line*/ -typedef struct { - lv_obj_t obj; - const lv_point_t * point_array; /**< Pointer to an array with the points of the line*/ - uint16_t point_num; /**< Number of points in 'point_array'*/ - uint8_t y_inv : 1; /**< 1: y == 0 will be on the bottom*/ -} lv_line_t; - -extern const lv_obj_class_t lv_line_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_line_class; /********************** * GLOBAL PROTOTYPES @@ -58,7 +47,15 @@ lv_obj_t * lv_line_create(lv_obj_t * parent); * @param points an array of points. Only the address is saved, so the array needs to be alive while the line exists * @param point_num number of points in 'point_a' */ -void lv_line_set_points(lv_obj_t * obj, const lv_point_t points[], uint16_t point_num); +void lv_line_set_points(lv_obj_t * obj, const lv_point_precise_t points[], uint32_t point_num); + +/** + * Set a non-const array of points. Identical to `lv_line_set_points` except the array may be retrieved by `lv_line_get_points_mutable`. + * @param obj pointer to a line object + * @param points a non-const array of points. Only the address is saved, so the array needs to be alive while the line exists. + * @param point_num number of points in 'point_a' + */ +void lv_line_set_points_mutable(lv_obj_t * obj, lv_point_precise_t points[], uint32_t point_num); /** * Enable (or disable) the y coordinate inversion. @@ -73,6 +70,34 @@ void lv_line_set_y_invert(lv_obj_t * obj, bool en); * Getter functions *====================*/ +/** + * Get the pointer to the array of points. + * @param obj pointer to a line object + * @return const pointer to the array of points + */ +const lv_point_precise_t * lv_line_get_points(lv_obj_t * obj); + +/** + * Get the number of points in the array of points. + * @param obj pointer to a line object + * @return number of points in array of points + */ +uint32_t lv_line_get_point_count(lv_obj_t * obj); + +/** + * Check the mutability of the stored point array pointer. + * @param obj pointer to a line object + * @return true: the point array pointer is mutable, false: constant + */ +bool lv_line_is_point_array_mutable(lv_obj_t * obj); + +/** + * Get a pointer to the mutable array of points or NULL if it is not mutable + * @param obj pointer to a line object + * @return pointer to the array of points. NULL if not mutable. + */ +lv_point_precise_t * lv_line_get_points_mutable(lv_obj_t * obj); + /** * Get the y inversion attribute * @param obj pointer to a line object diff --git a/include/liblvgl/widgets/line/lv_line_private.h b/include/liblvgl/widgets/line/lv_line_private.h new file mode 100644 index 00000000..4899392b --- /dev/null +++ b/include/liblvgl/widgets/line/lv_line_private.h @@ -0,0 +1,57 @@ +/** + * @file lv_line_private.h + * + */ + +#ifndef LV_LINE_PRIVATE_H +#define LV_LINE_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../core/lv_obj_private.h" +#include "lv_line.h" + +#if LV_USE_LINE != 0 + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** Data of line */ +struct _lv_line_t { + lv_obj_t obj; + union { + const lv_point_precise_t * constant; + lv_point_precise_t * mut; + } point_array; /**< Pointer to an array with the points of the line*/ + uint32_t point_num; /**< Number of points in 'point_array'*/ + uint32_t y_inv : 1; /**< 1: y == 0 will be on the bottom*/ + uint32_t point_array_is_mutable : 1; /**< whether the point array is const or mutable*/ +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_LINE != 0 */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_LINE_PRIVATE_H*/ diff --git a/include/liblvgl/widgets/list/lv_list.h b/include/liblvgl/widgets/list/lv_list.h new file mode 100644 index 00000000..0c3894d0 --- /dev/null +++ b/include/liblvgl/widgets/list/lv_list.h @@ -0,0 +1,85 @@ +/** + * @file lv_list.h + * + */ + +#ifndef LV_LIST_H +#define LV_LIST_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../core/lv_obj.h" + +#if LV_USE_LIST + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_list_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_list_text_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_list_button_class; +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create a list object + * @param parent pointer to an object, it will be the parent of the new list + * @return pointer to the created list + */ +lv_obj_t * lv_list_create(lv_obj_t * parent); + +/** + * Add text to a list + * @param list pointer to a list, it will be the parent of the new label + * @param txt text of the new label + * @return pointer to the created label + */ +lv_obj_t * lv_list_add_text(lv_obj_t * list, const char * txt); + +/** + * Add button to a list + * @param list pointer to a list, it will be the parent of the new button + * @param icon icon for the button, when NULL it will have no icon + * @param txt text of the new button, when NULL no text will be added + * @return pointer to the created button + */ +lv_obj_t * lv_list_add_button(lv_obj_t * list, const void * icon, const char * txt); + +/** + * Get text of a given list button + * @param list pointer to a list + * @param btn pointer to the button + * @return text of btn, if btn doesn't have text "" will be returned + */ +const char * lv_list_get_button_text(lv_obj_t * list, lv_obj_t * btn); + +/** + * Set text of a given list button + * @param list pointer to a list + * @param btn pointer to the button + * @param txt pointer to the text + */ +void lv_list_set_button_text(lv_obj_t * list, lv_obj_t * btn, const char * txt); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_LIST*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_LIST_H*/ diff --git a/include/liblvgl/widgets/lottie/lv_lottie.h b/include/liblvgl/widgets/lottie/lv_lottie.h new file mode 100644 index 00000000..1ccfe164 --- /dev/null +++ b/include/liblvgl/widgets/lottie/lv_lottie.h @@ -0,0 +1,102 @@ +/** + * @file lv_lottie.h + * + */ + +#ifndef LV_LOTTIE_H +#define LV_LOTTIE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../misc/lv_types.h" +#if LV_USE_LOTTIE + +/*Testing of dependencies*/ +#if LV_USE_CANVAS == 0 +#error "lv_lottie: lv_canvas is required. Enable it in lv_conf.h (LV_USE_CANVAS 1)" +#endif + +#if LV_USE_THORVG == 0 +#error "lv_lottie: ThorVG is required. Enable it in lv_conf.h (LV_USE_THORVG_INTERNAL/EXTERNAL 1)" +#endif + +#include "../../draw/lv_draw_buf.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create a lottie animation + * @param parent pointer to the parent widget + * @return pointer to the created Lottie animation widget + */ +lv_obj_t * lv_lottie_create(lv_obj_t * parent); + +/** + * Set a buffer for the animation. It also defines the size of the animation + * @param obj pointer to a lottie widget + * @param w width of the animation and buffer + * @param h height of the animation and buffer + * @param buf a static buffer with `width x height x 4` byte size + */ +void lv_lottie_set_buffer(lv_obj_t * obj, int32_t w, int32_t h, void * buf); + +/** + * Set a draw buffer for the animation. It also defines the size of the animation + * @param obj pointer to a lottie widget + * @param draw_buf an initialized draw buffer with ARGB8888 color format + */ +void lv_lottie_set_draw_buf(lv_obj_t * obj, lv_draw_buf_t * draw_buf); + +/** + * Set the source for the animation as an array + * @param obj pointer to a lottie widget + * @param src the lottie animation converted to an nul terminated array + * @param src_size size of the source array in bytes + */ +void lv_lottie_set_src_data(lv_obj_t * obj, const void * src, size_t src_size); + +/** + * Set the source for the animation as a path. + * Lottie doesn't use LVGL's File System API. + * @param obj pointer to a lottie widget + * @param src path to a json file, e.g. "path/to/file.json" + */ +void lv_lottie_set_src_file(lv_obj_t * obj, const char * src); + +/** + * Get the LVGL animation which controls the lottie animation + * @param obj pointer to a lottie widget + * @return the LVGL animation + */ +lv_anim_t * lv_lottie_get_anim(lv_obj_t * obj); + +/********************** + * GLOBAL VARIABLES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_LOTTIE*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_LOTTIE_H*/ diff --git a/include/liblvgl/widgets/lottie/lv_lottie_private.h b/include/liblvgl/widgets/lottie/lv_lottie_private.h new file mode 100644 index 00000000..6362afb8 --- /dev/null +++ b/include/liblvgl/widgets/lottie/lv_lottie_private.h @@ -0,0 +1,60 @@ +/** + * @file lv_lottie_private.h + * + */ + +#ifndef LV_LOTTIE_PRIVATE_H +#define LV_LOTTIE_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../lv_conf_internal.h" +#if LV_USE_LOTTIE + +#include "lv_lottie.h" +#include "../canvas/lv_canvas_private.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +#if LV_USE_THORVG_EXTERNAL +#include +#else +#include "../../libs/thorvg/thorvg_capi.h" +#endif + +typedef struct { + lv_canvas_t canvas; + Tvg_Paint * tvg_paint; + Tvg_Canvas * tvg_canvas; + Tvg_Animation * tvg_anim; + lv_anim_t * anim; + int32_t last_rendered_time; +} lv_lottie_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /*LV_LOTTIE_H*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_LOTTIE_PRIVATE_H*/ diff --git a/include/liblvgl/widgets/lv_arc.h b/include/liblvgl/widgets/lv_arc.h deleted file mode 100644 index 96f4629b..00000000 --- a/include/liblvgl/widgets/lv_arc.h +++ /dev/null @@ -1,256 +0,0 @@ -/** - * @file lv_arc.h - * - */ - -#ifndef LV_ARC_H -#define LV_ARC_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lv_conf_internal.h" - -#if LV_USE_ARC != 0 - -#include "liblvgl/core/lv_obj.h" - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -enum { - LV_ARC_MODE_NORMAL, - LV_ARC_MODE_SYMMETRICAL, - LV_ARC_MODE_REVERSE -}; -typedef uint8_t lv_arc_mode_t; - -typedef struct { - lv_obj_t obj; - uint16_t rotation; - uint16_t indic_angle_start; - uint16_t indic_angle_end; - uint16_t bg_angle_start; - uint16_t bg_angle_end; - int16_t value; /*Current value of the arc*/ - int16_t min_value; /*Minimum value of the arc*/ - int16_t max_value; /*Maximum value of the arc*/ - uint16_t dragging : 1; - uint16_t type : 2; - uint16_t min_close : 1; /*1: the last pressed angle was closer to minimum end*/ - uint16_t chg_rate; /*Drag angle rate of change of the arc (degrees/sec)*/ - uint32_t last_tick; /*Last dragging event timestamp of the arc*/ - int16_t last_angle; /*Last dragging angle of the arc*/ -} lv_arc_t; - -extern const lv_obj_class_t lv_arc_class; - -/** - * `type` field in `lv_obj_draw_part_dsc_t` if `class_p = lv_arc_class` - * Used in `LV_EVENT_DRAW_PART_BEGIN` and `LV_EVENT_DRAW_PART_END` - */ -typedef enum { - LV_ARC_DRAW_PART_BACKGROUND, /**< The background arc*/ - LV_ARC_DRAW_PART_FOREGROUND, /**< The foreground arc*/ - LV_ARC_DRAW_PART_KNOB, /**< The knob*/ -} lv_arc_draw_part_type_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Create an arc object - * @param parent pointer to an object, it will be the parent of the new arc - * @return pointer to the created arc - */ -lv_obj_t * lv_arc_create(lv_obj_t * parent); - -/*====================== - * Add/remove functions - *=====================*/ - -/*===================== - * Setter functions - *====================*/ - -/** - * Set the start angle of an arc. 0 deg: right, 90 bottom, etc. - * @param obj pointer to an arc object - * @param start the start angle - */ -void lv_arc_set_start_angle(lv_obj_t * obj, uint16_t start); - -/** - * Set the end angle of an arc. 0 deg: right, 90 bottom, etc. - * @param obj pointer to an arc object - * @param end the end angle - */ -void lv_arc_set_end_angle(lv_obj_t * obj, uint16_t end); - -/** - * Set the start and end angles - * @param obj pointer to an arc object - * @param start the start angle - * @param end the end angle - */ -void lv_arc_set_angles(lv_obj_t * obj, uint16_t start, uint16_t end); - -/** - * Set the start angle of an arc background. 0 deg: right, 90 bottom, etc. - * @param obj pointer to an arc object - * @param start the start angle - */ -void lv_arc_set_bg_start_angle(lv_obj_t * obj, uint16_t start); - -/** - * Set the start angle of an arc background. 0 deg: right, 90 bottom etc. - * @param obj pointer to an arc object - * @param end the end angle - */ -void lv_arc_set_bg_end_angle(lv_obj_t * obj, uint16_t end); - -/** - * Set the start and end angles of the arc background - * @param obj pointer to an arc object - * @param start the start angle - * @param end the end angle - */ -void lv_arc_set_bg_angles(lv_obj_t * obj, uint16_t start, uint16_t end); - -/** - * Set the rotation for the whole arc - * @param obj pointer to an arc object - * @param rotation rotation angle - */ -void lv_arc_set_rotation(lv_obj_t * obj, uint16_t rotation); - -/** - * Set the type of arc. - * @param obj pointer to arc object - * @param mode arc's mode - */ -void lv_arc_set_mode(lv_obj_t * obj, lv_arc_mode_t type); - -/** - * Set a new value on the arc - * @param obj pointer to an arc object - * @param value new value - */ -void lv_arc_set_value(lv_obj_t * obj, int16_t value); - -/** - * Set minimum and the maximum values of an arc - * @param obj pointer to the arc object - * @param min minimum value - * @param max maximum value - */ -void lv_arc_set_range(lv_obj_t * obj, int16_t min, int16_t max); - -/** - * Set a change rate to limit the speed how fast the arc should reach the pressed point. - * @param obj pointer to an arc object - * @param rate the change rate - */ -void lv_arc_set_change_rate(lv_obj_t * obj, uint16_t rate); - -/*===================== - * Getter functions - *====================*/ - -/** - * Get the start angle of an arc. - * @param obj pointer to an arc object - * @return the start angle [0..360] - */ -uint16_t lv_arc_get_angle_start(lv_obj_t * obj); - -/** - * Get the end angle of an arc. - * @param obj pointer to an arc object - * @return the end angle [0..360] - */ -uint16_t lv_arc_get_angle_end(lv_obj_t * obj); - -/** - * Get the start angle of an arc background. - * @param obj pointer to an arc object - * @return the start angle [0..360] - */ -uint16_t lv_arc_get_bg_angle_start(lv_obj_t * obj); - -/** - * Get the end angle of an arc background. - * @param obj pointer to an arc object - * @return the end angle [0..360] - */ -uint16_t lv_arc_get_bg_angle_end(lv_obj_t * obj); - -/** - * Get the value of an arc - * @param obj pointer to an arc object - * @return the value of the arc - */ -int16_t lv_arc_get_value(const lv_obj_t * obj); - -/** - * Get the minimum value of an arc - * @param obj pointer to an arc object - * @return the minimum value of the arc - */ -int16_t lv_arc_get_min_value(const lv_obj_t * obj); - -/** - * Get the maximum value of an arc - * @param obj pointer to an arc object - * @return the maximum value of the arc - */ -int16_t lv_arc_get_max_value(const lv_obj_t * obj); - -/** - * Get whether the arc is type or not. - * @param obj pointer to an arc object - * @return arc's mode - */ -lv_arc_mode_t lv_arc_get_mode(const lv_obj_t * obj); - -/*===================== - * Other functions - *====================*/ - -/** - * Align an object to the current position of the arc (knob) - * @param obj pointer to an arc object - * @param obj_to_align pointer to an object to align - * @param r_offset consider the radius larger with this value (< 0: for smaller radius) - */ -void lv_arc_align_obj_to_angle(const lv_obj_t * obj, lv_obj_t * obj_to_align, lv_coord_t r_offset); - -/** - * Rotate an object to the current position of the arc (knob) - * @param obj pointer to an arc object - * @param obj_to_align pointer to an object to rotate - * @param r_offset consider the radius larger with this value (< 0: for smaller radius) - */ -void lv_arc_rotate_obj_to_angle(const lv_obj_t * obj, lv_obj_t * obj_to_rotate, lv_coord_t r_offset); - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_ARC*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_ARC_H*/ diff --git a/include/liblvgl/widgets/lv_btnmatrix.h b/include/liblvgl/widgets/lv_btnmatrix.h deleted file mode 100644 index 31d62bb3..00000000 --- a/include/liblvgl/widgets/lv_btnmatrix.h +++ /dev/null @@ -1,225 +0,0 @@ -/** - * @file lv_btnmatrix.h - * - */ - -#ifndef LV_BTNMATRIX_H -#define LV_BTNMATRIX_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lv_conf_internal.h" - -#if LV_USE_BTNMATRIX != 0 - -#include "liblvgl/core/lv_obj.h" - -/********************* - * DEFINES - *********************/ -#define LV_BTNMATRIX_BTN_NONE 0xFFFF -LV_EXPORT_CONST_INT(LV_BTNMATRIX_BTN_NONE); - -/********************** - * TYPEDEFS - **********************/ - -/** Type to store button control bits (disabled, hidden etc.) - * The first 3 bits are used to store the width*/ -enum { - _LV_BTNMATRIX_WIDTH = 0x0007, /**< Reserved to stire the size units*/ - LV_BTNMATRIX_CTRL_HIDDEN = 0x0008, /**< Button hidden*/ - LV_BTNMATRIX_CTRL_NO_REPEAT = 0x0010, /**< Do not repeat press this button.*/ - LV_BTNMATRIX_CTRL_DISABLED = 0x0020, /**< Disable this button.*/ - LV_BTNMATRIX_CTRL_CHECKABLE = 0x0040, /**< The button can be toggled.*/ - LV_BTNMATRIX_CTRL_CHECKED = 0x0080, /**< Button is currently toggled (e.g. checked).*/ - LV_BTNMATRIX_CTRL_CLICK_TRIG = 0x0100, /**< 1: Send LV_EVENT_VALUE_CHANGE on CLICK, 0: Send LV_EVENT_VALUE_CHANGE on PRESS*/ - LV_BTNMATRIX_CTRL_POPOVER = 0x0200, /**< Show a popover when pressing this key*/ - LV_BTNMATRIX_CTRL_RECOLOR = 0x1000, /**< Enable text recoloring with `#color`*/ - _LV_BTNMATRIX_CTRL_RESERVED = 0x2000, /**< Reserved for later use*/ - LV_BTNMATRIX_CTRL_CUSTOM_1 = 0x4000, /**< Custom free to use flag*/ - LV_BTNMATRIX_CTRL_CUSTOM_2 = 0x8000, /**< Custom free to use flag*/ -}; - -typedef uint16_t lv_btnmatrix_ctrl_t; - -typedef bool (*lv_btnmatrix_btn_draw_cb_t)(lv_obj_t * btnm, uint32_t btn_id, const lv_area_t * draw_area, - const lv_area_t * clip_area); - -/*Data of button matrix*/ -typedef struct { - lv_obj_t obj; - const char ** map_p; /*Pointer to the current map*/ - lv_area_t * button_areas; /*Array of areas of buttons*/ - lv_btnmatrix_ctrl_t * ctrl_bits; /*Array of control bytes*/ - uint16_t btn_cnt; /*Number of button in 'map_p'(Handled by the library)*/ - uint16_t row_cnt; /*Number of rows in 'map_p'(Handled by the library)*/ - uint16_t btn_id_sel; /*Index of the active button (being pressed/released etc) or LV_BTNMATRIX_BTN_NONE*/ - uint8_t one_check : 1; /*Single button toggled at once*/ -} lv_btnmatrix_t; - -extern const lv_obj_class_t lv_btnmatrix_class; - -/** - * `type` field in `lv_obj_draw_part_dsc_t` if `class_p = lv_btnmatrix_class` - * Used in `LV_EVENT_DRAW_PART_BEGIN` and `LV_EVENT_DRAW_PART_END` - */ -typedef enum { - LV_BTNMATRIX_DRAW_PART_BTN, /**< The rectangle and label of buttons*/ -} lv_btnmatrix_draw_part_type_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Create a button matrix object - * @param parent pointer to an object, it will be the parent of the new button matrix - * @return pointer to the created button matrix - */ -lv_obj_t * lv_btnmatrix_create(lv_obj_t * parent); - -/*===================== - * Setter functions - *====================*/ - -/** - * Set a new map. Buttons will be created/deleted according to the map. The - * button matrix keeps a reference to the map and so the string array must not - * be deallocated during the life of the matrix. - * @param obj pointer to a button matrix object - * @param map pointer a string array. The last string has to be: "". Use "\n" to make a line break. - */ -void lv_btnmatrix_set_map(lv_obj_t * obj, const char * map[]); - -/** - * Set the button control map (hidden, disabled etc.) for a button matrix. - * The control map array will be copied and so may be deallocated after this - * function returns. - * @param obj pointer to a button matrix object - * @param ctrl_map pointer to an array of `lv_btn_ctrl_t` control bytes. The - * length of the array and position of the elements must match - * the number and order of the individual buttons (i.e. excludes - * newline entries). - * An element of the map should look like e.g.: - * `ctrl_map[0] = width | LV_BTNMATRIX_CTRL_NO_REPEAT | LV_BTNMATRIX_CTRL_TGL_ENABLE` - */ -void lv_btnmatrix_set_ctrl_map(lv_obj_t * obj, const lv_btnmatrix_ctrl_t ctrl_map[]); - -/** - * Set the selected buttons - * @param obj pointer to button matrix object - * @param btn_id 0 based index of the button to modify. (Not counting new lines) - */ -void lv_btnmatrix_set_selected_btn(lv_obj_t * obj, uint16_t btn_id); - -/** - * Set the attributes of a button of the button matrix - * @param obj pointer to button matrix object - * @param btn_id 0 based index of the button to modify. (Not counting new lines) - * @param ctrl OR-ed attributs. E.g. `LV_BTNMATRIX_CTRL_NO_REPEAT | LV_BTNMATRIX_CTRL_CHECKABLE` - */ -void lv_btnmatrix_set_btn_ctrl(lv_obj_t * obj, uint16_t btn_id, lv_btnmatrix_ctrl_t ctrl); - -/** - * Clear the attributes of a button of the button matrix - * @param obj pointer to button matrix object - * @param btn_id 0 based index of the button to modify. (Not counting new lines) - * @param ctrl OR-ed attributs. E.g. `LV_BTNMATRIX_CTRL_NO_REPEAT | LV_BTNMATRIX_CTRL_CHECKABLE` - */ -void lv_btnmatrix_clear_btn_ctrl(lv_obj_t * obj, uint16_t btn_id, lv_btnmatrix_ctrl_t ctrl); - -/** - * Set attributes of all buttons of a button matrix - * @param obj pointer to a button matrix object - * @param ctrl attribute(s) to set from `lv_btnmatrix_ctrl_t`. Values can be ORed. - */ -void lv_btnmatrix_set_btn_ctrl_all(lv_obj_t * obj, lv_btnmatrix_ctrl_t ctrl); - -/** - * Clear the attributes of all buttons of a button matrix - * @param obj pointer to a button matrix object - * @param ctrl attribute(s) to set from `lv_btnmatrix_ctrl_t`. Values can be ORed. - * @param en true: set the attributes; false: clear the attributes - */ -void lv_btnmatrix_clear_btn_ctrl_all(lv_obj_t * obj, lv_btnmatrix_ctrl_t ctrl); - -/** - * Set a single button's relative width. - * This method will cause the matrix be regenerated and is a relatively - * expensive operation. It is recommended that initial width be specified using - * `lv_btnmatrix_set_ctrl_map` and this method only be used for dynamic changes. - * @param obj pointer to button matrix object - * @param btn_id 0 based index of the button to modify. - * @param width relative width compared to the buttons in the same row. [1..7] - */ -void lv_btnmatrix_set_btn_width(lv_obj_t * obj, uint16_t btn_id, uint8_t width); - -/** - * Make the button matrix like a selector widget (only one button may be checked at a time). - * `LV_BTNMATRIX_CTRL_CHECKABLE` must be enabled on the buttons to be selected using - * `lv_btnmatrix_set_ctrl()` or `lv_btnmatrix_set_btn_ctrl_all()`. - * @param obj pointer to a button matrix object - * @param en whether "one check" mode is enabled - */ -void lv_btnmatrix_set_one_checked(lv_obj_t * obj, bool en); - -/*===================== - * Getter functions - *====================*/ - -/** - * Get the current map of a button matrix - * @param obj pointer to a button matrix object - * @return the current map - */ -const char ** lv_btnmatrix_get_map(const lv_obj_t * obj); - -/** - * Get the index of the lastly "activated" button by the user (pressed, released, focused etc) - * Useful in the `event_cb` to get the text of the button, check if hidden etc. - * @param obj pointer to button matrix object - * @return index of the last released button (LV_BTNMATRIX_BTN_NONE: if unset) - */ -uint16_t lv_btnmatrix_get_selected_btn(const lv_obj_t * obj); - -/** - * Get the button's text - * @param obj pointer to button matrix object - * @param btn_id the index a button not counting new line characters. - * @return text of btn_index` button - */ -const char * lv_btnmatrix_get_btn_text(const lv_obj_t * obj, uint16_t btn_id); - -/** - * Get the whether a control value is enabled or disabled for button of a button matrix - * @param obj pointer to a button matrix object - * @param btn_id the index of a button not counting new line characters. - * @param ctrl control values to check (ORed value can be used) - * @return true: the control attribute is enabled false: disabled - */ -bool lv_btnmatrix_has_btn_ctrl(lv_obj_t * obj, uint16_t btn_id, lv_btnmatrix_ctrl_t ctrl); - -/** - * Tell whether "one check" mode is enabled or not. - * @param obj Button matrix object - * @return true: "one check" mode is enabled; false: disabled - */ -bool lv_btnmatrix_get_one_checked(const lv_obj_t * obj); - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_BTNMATRIX*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_BTNMATRIX_H*/ diff --git a/include/liblvgl/widgets/lv_canvas.h b/include/liblvgl/widgets/lv_canvas.h deleted file mode 100644 index f92a1c5c..00000000 --- a/include/liblvgl/widgets/lv_canvas.h +++ /dev/null @@ -1,280 +0,0 @@ -/** - * @file lv_canvas.h - * - */ - -#ifndef LV_CANVAS_H -#define LV_CANVAS_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lv_conf_internal.h" - -#if LV_USE_CANVAS != 0 - -#include "liblvgl/core/lv_obj.h" -#include "liblvgl/widgets/lv_img.h" -#include "liblvgl/draw/lv_draw_img.h" - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ -extern const lv_obj_class_t lv_canvas_class; - -/*Data of canvas*/ -typedef struct { - lv_img_t img; - lv_img_dsc_t dsc; -} lv_canvas_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Create a canvas object - * @param parent pointer to an object, it will be the parent of the new canvas - * @return pointer to the created canvas - */ -lv_obj_t * lv_canvas_create(lv_obj_t * parent); - -/*===================== - * Setter functions - *====================*/ - -/** - * Set a buffer for the canvas. - * @param buf a buffer where the content of the canvas will be. - * The required size is (lv_img_color_format_get_px_size(cf) * w) / 8 * h) - * It can be allocated with `lv_mem_alloc()` or - * it can be statically allocated array (e.g. static lv_color_t buf[100*50]) or - * it can be an address in RAM or external SRAM - * @param canvas pointer to a canvas object - * @param w width of the canvas - * @param h height of the canvas - * @param cf color format. `LV_IMG_CF_...` - */ -void lv_canvas_set_buffer(lv_obj_t * canvas, void * buf, lv_coord_t w, lv_coord_t h, lv_img_cf_t cf); - -/** - * Set the color of a pixel on the canvas - * @param canvas - * @param x x coordinate of the point to set - * @param y x coordinate of the point to set - * @param c color of the pixel - */ -void lv_canvas_set_px_color(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y, lv_color_t c); - -/** - * DEPRECATED: added only for backward compatibility - */ -static inline void lv_canvas_set_px(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y, lv_color_t c) -{ - lv_canvas_set_px_color(canvas, x, y, c); -} - -/** - * Set the opacity of a pixel on the canvas - * @param canvas - * @param x x coordinate of the point to set - * @param y x coordinate of the point to set - * @param opa opacity of the pixel (0..255) - */ -void lv_canvas_set_px_opa(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y, lv_opa_t opa); - - -/** - * Set the palette color of a canvas with index format. Valid only for `LV_IMG_CF_INDEXED1/2/4/8` - * @param canvas pointer to canvas object - * @param id the palette color to set: - * - for `LV_IMG_CF_INDEXED1`: 0..1 - * - for `LV_IMG_CF_INDEXED2`: 0..3 - * - for `LV_IMG_CF_INDEXED4`: 0..15 - * - for `LV_IMG_CF_INDEXED8`: 0..255 - * @param c the color to set - */ -void lv_canvas_set_palette(lv_obj_t * canvas, uint8_t id, lv_color_t c); - -/*===================== - * Getter functions - *====================*/ - -/** - * Get the color of a pixel on the canvas - * @param canvas - * @param x x coordinate of the point to set - * @param y x coordinate of the point to set - * @return color of the point - */ -lv_color_t lv_canvas_get_px(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y); - -/** - * Get the image of the canvas as a pointer to an `lv_img_dsc_t` variable. - * @param canvas pointer to a canvas object - * @return pointer to the image descriptor. - */ -lv_img_dsc_t * lv_canvas_get_img(lv_obj_t * canvas); - -/*===================== - * Other functions - *====================*/ - -/** - * Copy a buffer to the canvas - * @param canvas pointer to a canvas object - * @param to_copy buffer to copy. The color format has to match with the canvas's buffer color - * format - * @param x left side of the destination position - * @param y top side of the destination position - * @param w width of the buffer to copy - * @param h height of the buffer to copy - */ -void lv_canvas_copy_buf(lv_obj_t * canvas, const void * to_copy, lv_coord_t x, lv_coord_t y, lv_coord_t w, - lv_coord_t h); - -/** - * Transform and image and store the result on a canvas. - * @param canvas pointer to a canvas object to store the result of the transformation. - * @param img pointer to an image descriptor to transform. - * Can be the image descriptor of an other canvas too (`lv_canvas_get_img()`). - * @param angle the angle of rotation (0..3600), 0.1 deg resolution - * @param zoom zoom factor (256 no zoom); - * @param offset_x offset X to tell where to put the result data on destination canvas - * @param offset_y offset X to tell where to put the result data on destination canvas - * @param pivot_x pivot X of rotation. Relative to the source canvas - * Set to `source width / 2` to rotate around the center - * @param pivot_y pivot Y of rotation. Relative to the source canvas - * Set to `source height / 2` to rotate around the center - * @param antialias apply anti-aliasing during the transformation. Looks better but slower. - */ -void lv_canvas_transform(lv_obj_t * canvas, lv_img_dsc_t * img, int16_t angle, uint16_t zoom, lv_coord_t offset_x, - lv_coord_t offset_y, - int32_t pivot_x, int32_t pivot_y, bool antialias); - -/** - * Apply horizontal blur on the canvas - * @param canvas pointer to a canvas object - * @param area the area to blur. If `NULL` the whole canvas will be blurred. - * @param r radius of the blur - */ -void lv_canvas_blur_hor(lv_obj_t * canvas, const lv_area_t * area, uint16_t r); - -/** - * Apply vertical blur on the canvas - * @param canvas pointer to a canvas object - * @param area the area to blur. If `NULL` the whole canvas will be blurred. - * @param r radius of the blur - */ -void lv_canvas_blur_ver(lv_obj_t * canvas, const lv_area_t * area, uint16_t r); - -/** - * Fill the canvas with color - * @param canvas pointer to a canvas - * @param color the background color - * @param opa the desired opacity - */ -void lv_canvas_fill_bg(lv_obj_t * canvas, lv_color_t color, lv_opa_t opa); - -/** - * Draw a rectangle on the canvas - * @param canvas pointer to a canvas object - * @param x left coordinate of the rectangle - * @param y top coordinate of the rectangle - * @param w width of the rectangle - * @param h height of the rectangle - * @param draw_dsc descriptor of the rectangle - */ -void lv_canvas_draw_rect(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y, lv_coord_t w, lv_coord_t h, - const lv_draw_rect_dsc_t * draw_dsc); - -/** - * Draw a text on the canvas. - * @param canvas pointer to a canvas object - * @param x left coordinate of the text - * @param y top coordinate of the text - * @param max_w max width of the text. The text will be wrapped to fit into this size - * @param draw_dsc pointer to a valid label descriptor `lv_draw_label_dsc_t` - * @param txt text to display - */ -void lv_canvas_draw_text(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y, lv_coord_t max_w, - lv_draw_label_dsc_t * draw_dsc, const char * txt); - -/** - * Draw an image on the canvas - * @param canvas pointer to a canvas object - * @param x left coordinate of the image - * @param y top coordinate of the image - * @param src image source. Can be a pointer an `lv_img_dsc_t` variable or a path an image. - * @param draw_dsc pointer to a valid label descriptor `lv_draw_img_dsc_t` - */ -void lv_canvas_draw_img(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y, const void * src, - const lv_draw_img_dsc_t * draw_dsc); - -/** - * Draw a line on the canvas - * @param canvas pointer to a canvas object - * @param points point of the line - * @param point_cnt number of points - * @param draw_dsc pointer to an initialized `lv_draw_line_dsc_t` variable - */ -void lv_canvas_draw_line(lv_obj_t * canvas, const lv_point_t points[], uint32_t point_cnt, - const lv_draw_line_dsc_t * draw_dsc); - -/** - * Draw a polygon on the canvas - * @param canvas pointer to a canvas object - * @param points point of the polygon - * @param point_cnt number of points - * @param draw_dsc pointer to an initialized `lv_draw_rect_dsc_t` variable - */ -void lv_canvas_draw_polygon(lv_obj_t * canvas, const lv_point_t points[], uint32_t point_cnt, - const lv_draw_rect_dsc_t * draw_dsc); - -/** - * Draw an arc on the canvas - * @param canvas pointer to a canvas object - * @param x origo x of the arc - * @param y origo y of the arc - * @param r radius of the arc - * @param start_angle start angle in degrees - * @param end_angle end angle in degrees - * @param draw_dsc pointer to an initialized `lv_draw_line_dsc_t` variable - */ -void lv_canvas_draw_arc(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y, lv_coord_t r, int32_t start_angle, - int32_t end_angle, const lv_draw_arc_dsc_t * draw_dsc); - -/********************** - * MACROS - **********************/ -#define LV_CANVAS_BUF_SIZE_TRUE_COLOR(w, h) LV_IMG_BUF_SIZE_TRUE_COLOR(w, h) -#define LV_CANVAS_BUF_SIZE_TRUE_COLOR_CHROMA_KEYED(w, h) LV_IMG_BUF_SIZE_TRUE_COLOR_CHROMA_KEYED(w, h) -#define LV_CANVAS_BUF_SIZE_TRUE_COLOR_ALPHA(w, h) LV_IMG_BUF_SIZE_TRUE_COLOR_ALPHA(w, h) - -/*+ 1: to be sure no fractional row*/ -#define LV_CANVAS_BUF_SIZE_ALPHA_1BIT(w, h) LV_IMG_BUF_SIZE_ALPHA_1BIT(w, h) -#define LV_CANVAS_BUF_SIZE_ALPHA_2BIT(w, h) LV_IMG_BUF_SIZE_ALPHA_2BIT(w, h) -#define LV_CANVAS_BUF_SIZE_ALPHA_4BIT(w, h) LV_IMG_BUF_SIZE_ALPHA_4BIT(w, h) -#define LV_CANVAS_BUF_SIZE_ALPHA_8BIT(w, h) LV_IMG_BUF_SIZE_ALPHA_8BIT(w, h) - -/*4 * X: for palette*/ -#define LV_CANVAS_BUF_SIZE_INDEXED_1BIT(w, h) LV_IMG_BUF_SIZE_INDEXED_1BIT(w, h) -#define LV_CANVAS_BUF_SIZE_INDEXED_2BIT(w, h) LV_IMG_BUF_SIZE_INDEXED_2BIT(w, h) -#define LV_CANVAS_BUF_SIZE_INDEXED_4BIT(w, h) LV_IMG_BUF_SIZE_INDEXED_4BIT(w, h) -#define LV_CANVAS_BUF_SIZE_INDEXED_8BIT(w, h) LV_IMG_BUF_SIZE_INDEXED_8BIT(w, h) - -#endif /*LV_USE_CANVAS*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_CANVAS_H*/ diff --git a/include/liblvgl/widgets/lv_img.h b/include/liblvgl/widgets/lv_img.h deleted file mode 100644 index 5513a26c..00000000 --- a/include/liblvgl/widgets/lv_img.h +++ /dev/null @@ -1,234 +0,0 @@ -/** - * @file lv_img.h - * - */ - -#ifndef LV_IMG_H -#define LV_IMG_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lv_conf_internal.h" - -#if LV_USE_IMG != 0 - -/*Testing of dependencies*/ -#if LV_USE_LABEL == 0 -#error "lv_img: lv_label is required. Enable it in lv_conf.h (LV_USE_LABEL 1)" -#endif - -#include "liblvgl/core/lv_obj.h" -#include "liblvgl/misc/lv_fs.h" -#include "liblvgl/draw/lv_draw.h" - -/********************* - * DEFINES - *********************/ - -/********************** - * TYPEDEFS - **********************/ - -/** - * Data of image - */ -typedef struct { - lv_obj_t obj; - const void * src; /*Image source: Pointer to an array or a file or a symbol*/ - lv_point_t offset; - lv_coord_t w; /*Width of the image (Handled by the library)*/ - lv_coord_t h; /*Height of the image (Handled by the library)*/ - uint16_t angle; /*rotation angle of the image*/ - lv_point_t pivot; /*rotation center of the image*/ - uint16_t zoom; /*256 means no zoom, 512 double size, 128 half size*/ - uint8_t src_type : 2; /*See: lv_img_src_t*/ - uint8_t cf : 5; /*Color format from `lv_img_color_format_t`*/ - uint8_t antialias : 1; /*Apply anti-aliasing in transformations (rotate, zoom)*/ - uint8_t obj_size_mode: 2; /*Image size mode when image size and object size is different.*/ -} lv_img_t; - -extern const lv_obj_class_t lv_img_class; - -/** - * Image size mode, when image size and object size is different - */ -enum { - /** Zoom doesn't affect the coordinates of the object, - * however if zoomed in the image is drawn out of the its coordinates. - * The layout's won't change on zoom */ - LV_IMG_SIZE_MODE_VIRTUAL = 0, - - /** If the object size is set to SIZE_CONTENT, then object size equals zoomed image size. - * It causes layout recalculation. - * If the object size is set explicitly, the image will be cropped when zoomed in.*/ - LV_IMG_SIZE_MODE_REAL, -}; - -typedef uint8_t lv_img_size_mode_t; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Create an image object - * @param parent pointer to an object, it will be the parent of the new image - * @return pointer to the created image - */ -lv_obj_t * lv_img_create(lv_obj_t * parent); - -/*===================== - * Setter functions - *====================*/ - -/** - * Set the image data to display on the object - * @param obj pointer to an image object - * @param src_img 1) pointer to an ::lv_img_dsc_t descriptor (converted by LVGL's image converter) (e.g. &my_img) or - * 2) path to an image file (e.g. "S:/dir/img.bin")or - * 3) a SYMBOL (e.g. LV_SYMBOL_OK) - */ -void lv_img_set_src(lv_obj_t * obj, const void * src); - -/** - * Set an offset for the source of an image so the image will be displayed from the new origin. - * @param obj pointer to an image - * @param x the new offset along x axis. - */ -void lv_img_set_offset_x(lv_obj_t * obj, lv_coord_t x); - -/** - * Set an offset for the source of an image. - * so the image will be displayed from the new origin. - * @param obj pointer to an image - * @param y the new offset along y axis. - */ -void lv_img_set_offset_y(lv_obj_t * obj, lv_coord_t y); - - -/** - * Set the rotation angle of the image. - * The image will be rotated around the set pivot set by `lv_img_set_pivot()` - * Note that indexed and alpha only images can't be transformed. - * @param obj pointer to an image object - * @param angle rotation angle in degree with 0.1 degree resolution (0..3600: clock wise) - */ -void lv_img_set_angle(lv_obj_t * obj, int16_t angle); - -/** - * Set the rotation center of the image. - * The image will be rotated around this point. - * @param obj pointer to an image object - * @param x rotation center x of the image - * @param y rotation center y of the image - */ -void lv_img_set_pivot(lv_obj_t * obj, lv_coord_t x, lv_coord_t y); - - -/** - * Set the zoom factor of the image. - * Note that indexed and alpha only images can't be transformed. - * @param img pointer to an image object - * @param zoom the zoom factor. - * @example 256 or LV_ZOOM_IMG_NONE for no zoom - * @example <256: scale down - * @example >256 scale up - * @example 128 half size - * @example 512 double size - */ -void lv_img_set_zoom(lv_obj_t * obj, uint16_t zoom); - -/** - * Enable/disable anti-aliasing for the transformations (rotate, zoom) or not. - * The quality is better with anti-aliasing looks better but slower. - * @param obj pointer to an image object - * @param antialias true: anti-aliased; false: not anti-aliased - */ -void lv_img_set_antialias(lv_obj_t * obj, bool antialias); - -/** - * Set the image object size mode. - * - * @param obj pointer to an image object - * @param mode the new size mode. - */ -void lv_img_set_size_mode(lv_obj_t * obj, lv_img_size_mode_t mode); -/*===================== - * Getter functions - *====================*/ - -/** - * Get the source of the image - * @param obj pointer to an image object - * @return the image source (symbol, file name or ::lv-img_dsc_t for C arrays) - */ -const void * lv_img_get_src(lv_obj_t * obj); - -/** - * Get the offset's x attribute of the image object. - * @param img pointer to an image - * @return offset X value. - */ -lv_coord_t lv_img_get_offset_x(lv_obj_t * obj); - -/** - * Get the offset's y attribute of the image object. - * @param obj pointer to an image - * @return offset Y value. - */ -lv_coord_t lv_img_get_offset_y(lv_obj_t * obj); - -/** - * Get the rotation angle of the image. - * @param obj pointer to an image object - * @return rotation angle in 0.1 degrees (0..3600) - */ -uint16_t lv_img_get_angle(lv_obj_t * obj); - -/** - * Get the pivot (rotation center) of the image. - * @param img pointer to an image object - * @param pivot store the rotation center here - */ -void lv_img_get_pivot(lv_obj_t * obj, lv_point_t * pivot); - -/** - * Get the zoom factor of the image. - * @param obj pointer to an image object - * @return zoom factor (256: no zoom) - */ -uint16_t lv_img_get_zoom(lv_obj_t * obj); - -/** - * Get whether the transformations (rotate, zoom) are anti-aliased or not - * @param obj pointer to an image object - * @return true: anti-aliased; false: not anti-aliased - */ -bool lv_img_get_antialias(lv_obj_t * obj); - -/** - * Get the size mode of the image - * @param obj pointer to an image object - * @return element of @ref lv_img_size_mode_t - */ -lv_img_size_mode_t lv_img_get_size_mode(lv_obj_t * obj); - -/********************** - * MACROS - **********************/ - -/** Use this macro to declare an image in a C file*/ -#define LV_IMG_DECLARE(var_name) extern const lv_img_dsc_t var_name; - -#endif /*LV_USE_IMG*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_IMG_H*/ diff --git a/include/liblvgl/widgets/lv_switch.h b/include/liblvgl/widgets/lv_switch.h deleted file mode 100644 index 63ec4276..00000000 --- a/include/liblvgl/widgets/lv_switch.h +++ /dev/null @@ -1,61 +0,0 @@ -/** - * @file lv_switch.h - * - */ - -#ifndef LV_SWITCH_H -#define LV_SWITCH_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#include "liblvgl/lv_conf_internal.h" - -#if LV_USE_SWITCH != 0 - -#include "liblvgl/core/lv_obj.h" - -/********************* - * DEFINES - *********************/ - -/** Switch knob extra area correction factor */ -#define _LV_SWITCH_KNOB_EXT_AREA_CORRECTION 2 - -/********************** - * TYPEDEFS - **********************/ - -typedef struct { - lv_obj_t obj; - int32_t anim_state; -} lv_switch_t; - -extern const lv_obj_class_t lv_switch_class; - -/********************** - * GLOBAL PROTOTYPES - **********************/ - -/** - * Create a switch object - * @param parent pointer to an object, it will be the parent of the new switch - * @return pointer to the created switch - */ -lv_obj_t * lv_switch_create(lv_obj_t * parent); - -/********************** - * MACROS - **********************/ - -#endif /*LV_USE_SWITCH*/ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /*LV_SWITCH_H*/ diff --git a/include/liblvgl/widgets/menu/lv_menu.h b/include/liblvgl/widgets/menu/lv_menu.h new file mode 100644 index 00000000..3d75f8d9 --- /dev/null +++ b/include/liblvgl/widgets/menu/lv_menu.h @@ -0,0 +1,213 @@ +/** + * @file lv_menu.h + * + */ + +#ifndef LV_MENU_H +#define LV_MENU_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../core/lv_obj.h" + +#if LV_USE_MENU + +#if LV_USE_FLEX == 0 +#error "LV_USE_FLEX needs to be enabled" +#endif + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ +typedef enum { + LV_MENU_HEADER_TOP_FIXED, /**< Header is positioned at the top */ + LV_MENU_HEADER_TOP_UNFIXED, /**< Header is positioned at the top and can be scrolled out of view*/ + LV_MENU_HEADER_BOTTOM_FIXED /**< Header is positioned at the bottom */ +} lv_menu_mode_header_t; + +typedef enum { + LV_MENU_ROOT_BACK_BUTTON_DISABLED, + LV_MENU_ROOT_BACK_BUTTON_ENABLED +} lv_menu_mode_root_back_button_t; + +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_menu_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_menu_page_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_menu_cont_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_menu_section_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_menu_separator_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_menu_sidebar_cont_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_menu_main_cont_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_menu_sidebar_header_cont_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_menu_main_header_cont_class; +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create a menu object + * @param parent pointer to an object, it will be the parent of the new menu + * @return pointer to the created menu + */ +lv_obj_t * lv_menu_create(lv_obj_t * parent); + +/** + * Create a menu page object + * @param parent pointer to menu object + * @param title pointer to text for title in header (NULL to not display title) + * @return pointer to the created menu page + */ +lv_obj_t * lv_menu_page_create(lv_obj_t * parent, char const * const title); + +/** + * Create a menu cont object + * @param parent pointer to an object, it will be the parent of the new menu cont object + * @return pointer to the created menu cont + */ +lv_obj_t * lv_menu_cont_create(lv_obj_t * parent); + +/** + * Create a menu section object + * @param parent pointer to an object, it will be the parent of the new menu section object + * @return pointer to the created menu section + */ +lv_obj_t * lv_menu_section_create(lv_obj_t * parent); + +/** + * Create a menu separator object + * @param parent pointer to an object, it will be the parent of the new menu separator object + * @return pointer to the created menu separator + */ +lv_obj_t * lv_menu_separator_create(lv_obj_t * parent); +/*===================== + * Setter functions + *====================*/ +/** + * Set menu page to display in main + * @param obj pointer to the menu + * @param page pointer to the menu page to set (NULL to clear main and clear menu history) + */ +void lv_menu_set_page(lv_obj_t * obj, lv_obj_t * page); + +/** + * Set menu page title + * @param page pointer to the menu page + * @param title pointer to text for title in header (NULL to not display title) + */ +void lv_menu_set_page_title(lv_obj_t * page, char const * const title); + +/** + * Set menu page title with a static text. It will not be saved by the label so the 'text' variable + * has to be 'alive' while the page exists. + * @param page pointer to the menu page + * @param title pointer to text for title in header (NULL to not display title) + */ +void lv_menu_set_page_title_static(lv_obj_t * page, char const * const title); + +/** + * Set menu page to display in sidebar + * @param obj pointer to the menu + * @param page pointer to the menu page to set (NULL to clear sidebar) + */ +void lv_menu_set_sidebar_page(lv_obj_t * obj, lv_obj_t * page); + +/** + * Set the how the header should behave and its position + * @param obj pointer to a menu + * @param mode LV_MENU_HEADER_TOP_FIXED/TOP_UNFIXED/BOTTOM_FIXED + */ +void lv_menu_set_mode_header(lv_obj_t * obj, lv_menu_mode_header_t mode); + +/** + * Set whether back button should appear at root + * @param obj pointer to a menu + * @param mode LV_MENU_ROOT_BACK_BUTTON_DISABLED/ENABLED + */ +void lv_menu_set_mode_root_back_button(lv_obj_t * obj, lv_menu_mode_root_back_button_t mode); + +/** + * Add menu to the menu item + * @param menu pointer to the menu + * @param obj pointer to the obj + * @param page pointer to the page to load when obj is clicked + */ +void lv_menu_set_load_page_event(lv_obj_t * menu, lv_obj_t * obj, lv_obj_t * page); + +/*===================== + * Getter functions + *====================*/ +/** +* Get a pointer to menu page that is currently displayed in main +* @param obj pointer to the menu +* @return pointer to current page +*/ +lv_obj_t * lv_menu_get_cur_main_page(lv_obj_t * obj); + +/** +* Get a pointer to menu page that is currently displayed in sidebar +* @param obj pointer to the menu +* @return pointer to current page +*/ +lv_obj_t * lv_menu_get_cur_sidebar_page(lv_obj_t * obj); + +/** +* Get a pointer to main header obj +* @param obj pointer to the menu +* @return pointer to main header obj +*/ +lv_obj_t * lv_menu_get_main_header(lv_obj_t * obj); + +/** +* Get a pointer to main header back btn obj +* @param obj pointer to the menu +* @return pointer to main header back btn obj +*/ +lv_obj_t * lv_menu_get_main_header_back_button(lv_obj_t * obj); + +/** +* Get a pointer to sidebar header obj +* @param obj pointer to the menu +* @return pointer to sidebar header obj +*/ +lv_obj_t * lv_menu_get_sidebar_header(lv_obj_t * obj); + +/** +* Get a pointer to sidebar header obj +* @param obj pointer to the menu +* @return pointer to sidebar header back btn obj +*/ +lv_obj_t * lv_menu_get_sidebar_header_back_button(lv_obj_t * obj); + +/** + * Check if an obj is a root back btn + * @param menu pointer to the menu + * @param obj pointer to the back button + * @return true if it is a root back btn + */ +bool lv_menu_back_button_is_root(lv_obj_t * menu, lv_obj_t * obj); + +/** + * Clear menu history + * @param obj pointer to the menu + */ +void lv_menu_clear_history(lv_obj_t * obj); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_MENU*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_MENU_H*/ diff --git a/include/liblvgl/widgets/menu/lv_menu_private.h b/include/liblvgl/widgets/menu/lv_menu_private.h new file mode 100644 index 00000000..51229371 --- /dev/null +++ b/include/liblvgl/widgets/menu/lv_menu_private.h @@ -0,0 +1,84 @@ +/** + * @file lv_menu_private.h + * + */ + +#ifndef LV_MENU_PRIVATE_H +#define LV_MENU_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../core/lv_obj_private.h" +#include "lv_menu.h" + +#if LV_USE_MENU + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_menu_load_page_event_data_t { + lv_obj_t * menu; + lv_obj_t * page; +}; + +struct _lv_menu_history_t { + lv_obj_t * page; +}; + +struct _lv_menu_t { + lv_obj_t obj; + lv_obj_t * storage; /**< a pointer to obj that is the parent of all pages not displayed */ + lv_obj_t * main; + lv_obj_t * main_page; + lv_obj_t * main_header; + lv_obj_t * + main_header_back_btn; /**< a pointer to obj that on click triggers back btn event handler, can be same as 'main_header' */ + lv_obj_t * main_header_title; + lv_obj_t * sidebar; + lv_obj_t * sidebar_page; + lv_obj_t * sidebar_header; + lv_obj_t * + sidebar_header_back_btn; /**< a pointer to obj that on click triggers back btn event handler, can be same as 'sidebar_header' */ + lv_obj_t * sidebar_header_title; + lv_obj_t * selected_tab; + lv_ll_t history_ll; + uint8_t cur_depth; + uint8_t prev_depth; + uint8_t sidebar_generated : 1; + lv_menu_mode_header_t mode_header : 2; + lv_menu_mode_root_back_button_t mode_root_back_btn : 1; +}; + +struct _lv_menu_page_t { + lv_obj_t obj; + char * title; + bool static_title; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_MENU */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_MENU_PRIVATE_H*/ diff --git a/include/liblvgl/widgets/msgbox/lv_msgbox.h b/include/liblvgl/widgets/msgbox/lv_msgbox.h new file mode 100644 index 00000000..5605b802 --- /dev/null +++ b/include/liblvgl/widgets/msgbox/lv_msgbox.h @@ -0,0 +1,141 @@ +/** + * @file lv_msgbox.h + * + */ + +#ifndef LV_MSGBOX_H +#define LV_MSGBOX_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../core/lv_obj.h" + +#if LV_USE_MSGBOX + +/*Testing of dependencies*/ +#if LV_USE_BUTTONMATRIX == 0 +#error "lv_mbox: lv_buttonmatrix is required. Enable it in lv_conf.h (LV_USE_BUTTONMATRIX 1) " +#endif + +#if LV_USE_LABEL == 0 +#error "lv_mbox: lv_label is required. Enable it in lv_conf.h (LV_USE_LABEL 1) " +#endif + +/********************* + * DEFINES + *********************/ + +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_msgbox_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_msgbox_header_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_msgbox_content_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_msgbox_footer_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_msgbox_header_button_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_msgbox_footer_button_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_msgbox_backdrop_class; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create an empty message box + * @param parent the parent or NULL to create a modal msgbox + * @return the created message box + */ +lv_obj_t * lv_msgbox_create(lv_obj_t * parent); + +/** + * Add title to the message box. It also creates a header for the title. + * @param obj pointer to a message box + * @param title the text of the tile + * @return the created title label + */ +lv_obj_t * lv_msgbox_add_title(lv_obj_t * obj, const char * title); + +/** + * Add a button to the header of to the message box. It also creates a header. + * @param obj pointer to a message box + * @param icon the icon of the button + * @return the created button + */ +lv_obj_t * lv_msgbox_add_header_button(lv_obj_t * obj, const void * icon); + +/** + * Add a text to the content area of message box. Multiple texts will be created below each other. + * @param obj pointer to a message box + * @param text text to add + * @return the created button + */ +lv_obj_t * lv_msgbox_add_text(lv_obj_t * obj, const char * text); + +/** + * Add a button to the footer of to the message box. It also creates a footer. + * @param obj pointer to a message box + * @param text the text of the button + * @return the created button + */ +lv_obj_t * lv_msgbox_add_footer_button(lv_obj_t * obj, const char * text); + +/** + * Add a close button to the message box. It also creates a header. + * @param obj pointer to a message box + * @return the created close button + */ +lv_obj_t * lv_msgbox_add_close_button(lv_obj_t * obj); + +/** + * Get the header widget + * @param obj pointer to a message box + * @return the header, or NULL if not exists + */ +lv_obj_t * lv_msgbox_get_header(lv_obj_t * obj); + +/** + * Get the footer widget + * @param obj pointer to a message box + * @return the footer, or NULL if not exists + */ +lv_obj_t * lv_msgbox_get_footer(lv_obj_t * obj); + +/** + * Get the content widget + * @param obj pointer to a message box + * @return the content + */ +lv_obj_t * lv_msgbox_get_content(lv_obj_t * obj); + +/** + * Get the title label + * @param obj pointer to a message box + * @return the title, or NULL if it does not exist + */ +lv_obj_t * lv_msgbox_get_title(lv_obj_t * obj); + +/** + * Close a message box + * @param mbox pointer to a message box + */ +void lv_msgbox_close(lv_obj_t * mbox); + +/** + * Close a message box in the next call of the message box + * @param mbox pointer to a message box + */ +void lv_msgbox_close_async(lv_obj_t * mbox); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_MSGBOX*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_MSGBOX_H*/ diff --git a/include/liblvgl/widgets/msgbox/lv_msgbox_private.h b/include/liblvgl/widgets/msgbox/lv_msgbox_private.h new file mode 100644 index 00000000..4799ea3c --- /dev/null +++ b/include/liblvgl/widgets/msgbox/lv_msgbox_private.h @@ -0,0 +1,57 @@ +/** + * @file lv_msgbox_private.h + * + */ + +#ifndef LV_MSGBOX_PRIVATE_H +#define LV_MSGBOX_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_msgbox.h" + +#if LV_USE_MSGBOX +#include "../../core/lv_obj_private.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_msgbox_t { + lv_obj_t obj; + lv_obj_t * header; + lv_obj_t * content; + lv_obj_t * footer; + lv_obj_t * title; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_MSGBOX */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_MSGBOX_PRIVATE_H*/ diff --git a/include/liblvgl/widgets/lv_objx_templ.h b/include/liblvgl/widgets/objx_templ/lv_objx_templ.h similarity index 91% rename from include/liblvgl/widgets/lv_objx_templ.h rename to include/liblvgl/widgets/objx_templ/lv_objx_templ.h index f363df4e..ccf1e1c3 100644 --- a/include/liblvgl/widgets/lv_objx_templ.h +++ b/include/liblvgl/widgets/objx_templ/lv_objx_templ.h @@ -20,11 +20,11 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" +#include "../lv_conf_internal.h" #if LV_USE_TEMPL != 0 -#include "liblvgl/core/lv_obj.h" +#include "../core/lv_obj.h" /********************* * DEFINES @@ -39,7 +39,7 @@ typedef struct { /*New data for this type*/ } lv_templ_t; -extern const lv_obj_class_t lv_templ_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_templ_class; /********************** * GLOBAL PROTOTYPES diff --git a/include/liblvgl/widgets/property/lv_obj_property_names.h b/include/liblvgl/widgets/property/lv_obj_property_names.h new file mode 100644 index 00000000..4b4ff67a --- /dev/null +++ b/include/liblvgl/widgets/property/lv_obj_property_names.h @@ -0,0 +1,22 @@ + +/** + * @file lv_obj_property_names.h + * GENERATED FILE, DO NOT EDIT IT! + */ +#ifndef LV_OBJ_PROPERTY_NAMES_H +#define LV_OBJ_PROPERTY_NAMES_H + +#include "../../misc/lv_types.h" + +#if LV_USE_OBJ_PROPERTY && LV_USE_OBJ_PROPERTY_NAME + + extern const lv_property_name_t lv_dropdown_property_names[9]; + extern const lv_property_name_t lv_image_property_names[11]; + extern const lv_property_name_t lv_keyboard_property_names[4]; + extern const lv_property_name_t lv_label_property_names[4]; + extern const lv_property_name_t lv_obj_property_names[73]; + extern const lv_property_name_t lv_roller_property_names[3]; + extern const lv_property_name_t lv_style_property_names[112]; + extern const lv_property_name_t lv_textarea_property_names[15]; +#endif +#endif diff --git a/include/liblvgl/widgets/property/lv_style_properties.h b/include/liblvgl/widgets/property/lv_style_properties.h new file mode 100644 index 00000000..146f35ad --- /dev/null +++ b/include/liblvgl/widgets/property/lv_style_properties.h @@ -0,0 +1,130 @@ + +/** + * GENERATED FILE, DO NOT EDIT IT! + * @file lv_style_properties.h + */ +#ifndef LV_STYLE_PROPERTIES_H +#define LV_STYLE_PROPERTIES_H + +#include "../../core/lv_obj_property.h" +#if LV_USE_OBJ_PROPERTY + + +/* *INDENT-OFF* */ +enum { + LV_PROPERTY_ID(STYLE, ALIGN, LV_PROPERTY_TYPE_INT, LV_STYLE_ALIGN), + LV_PROPERTY_ID(STYLE, ANIM, LV_PROPERTY_TYPE_INT, LV_STYLE_ANIM), + LV_PROPERTY_ID(STYLE, ANIM_DURATION, LV_PROPERTY_TYPE_INT, LV_STYLE_ANIM_DURATION), + LV_PROPERTY_ID(STYLE, ARC_COLOR, LV_PROPERTY_TYPE_INT, LV_STYLE_ARC_COLOR), + LV_PROPERTY_ID(STYLE, ARC_IMAGE_SRC, LV_PROPERTY_TYPE_INT, LV_STYLE_ARC_IMAGE_SRC), + LV_PROPERTY_ID(STYLE, ARC_OPA, LV_PROPERTY_TYPE_INT, LV_STYLE_ARC_OPA), + LV_PROPERTY_ID(STYLE, ARC_ROUNDED, LV_PROPERTY_TYPE_INT, LV_STYLE_ARC_ROUNDED), + LV_PROPERTY_ID(STYLE, ARC_WIDTH, LV_PROPERTY_TYPE_INT, LV_STYLE_ARC_WIDTH), + LV_PROPERTY_ID(STYLE, BASE_DIR, LV_PROPERTY_TYPE_INT, LV_STYLE_BASE_DIR), + LV_PROPERTY_ID(STYLE, BG_COLOR, LV_PROPERTY_TYPE_COLOR, LV_STYLE_BG_COLOR), + LV_PROPERTY_ID(STYLE, BG_GRAD, LV_PROPERTY_TYPE_INT, LV_STYLE_BG_GRAD), + LV_PROPERTY_ID(STYLE, BG_GRAD_COLOR, LV_PROPERTY_TYPE_COLOR, LV_STYLE_BG_GRAD_COLOR), + LV_PROPERTY_ID(STYLE, BG_GRAD_DIR, LV_PROPERTY_TYPE_INT, LV_STYLE_BG_GRAD_DIR), + LV_PROPERTY_ID(STYLE, BG_GRAD_OPA, LV_PROPERTY_TYPE_INT, LV_STYLE_BG_GRAD_OPA), + LV_PROPERTY_ID(STYLE, BG_GRAD_STOP, LV_PROPERTY_TYPE_INT, LV_STYLE_BG_GRAD_STOP), + LV_PROPERTY_ID(STYLE, BG_IMAGE_OPA, LV_PROPERTY_TYPE_INT, LV_STYLE_BG_IMAGE_OPA), + LV_PROPERTY_ID(STYLE, BG_IMAGE_RECOLOR, LV_PROPERTY_TYPE_COLOR, LV_STYLE_BG_IMAGE_RECOLOR), + LV_PROPERTY_ID(STYLE, BG_IMAGE_RECOLOR_OPA, LV_PROPERTY_TYPE_INT, LV_STYLE_BG_IMAGE_RECOLOR_OPA), + LV_PROPERTY_ID(STYLE, BG_IMAGE_SRC, LV_PROPERTY_TYPE_IMGSRC, LV_STYLE_BG_IMAGE_SRC), + LV_PROPERTY_ID(STYLE, BG_IMAGE_TILED, LV_PROPERTY_TYPE_INT, LV_STYLE_BG_IMAGE_TILED), + LV_PROPERTY_ID(STYLE, BG_MAIN_OPA, LV_PROPERTY_TYPE_INT, LV_STYLE_BG_MAIN_OPA), + LV_PROPERTY_ID(STYLE, BG_MAIN_STOP, LV_PROPERTY_TYPE_INT, LV_STYLE_BG_MAIN_STOP), + LV_PROPERTY_ID(STYLE, BG_OPA, LV_PROPERTY_TYPE_INT, LV_STYLE_BG_OPA), + LV_PROPERTY_ID(STYLE, BITMAP_MASK_SRC, LV_PROPERTY_TYPE_INT, LV_STYLE_BITMAP_MASK_SRC), + LV_PROPERTY_ID(STYLE, BLEND_MODE, LV_PROPERTY_TYPE_INT, LV_STYLE_BLEND_MODE), + LV_PROPERTY_ID(STYLE, BORDER_COLOR, LV_PROPERTY_TYPE_COLOR, LV_STYLE_BORDER_COLOR), + LV_PROPERTY_ID(STYLE, BORDER_OPA, LV_PROPERTY_TYPE_INT, LV_STYLE_BORDER_OPA), + LV_PROPERTY_ID(STYLE, BORDER_POST, LV_PROPERTY_TYPE_INT, LV_STYLE_BORDER_POST), + LV_PROPERTY_ID(STYLE, BORDER_SIDE, LV_PROPERTY_TYPE_INT, LV_STYLE_BORDER_SIDE), + LV_PROPERTY_ID(STYLE, BORDER_WIDTH, LV_PROPERTY_TYPE_INT, LV_STYLE_BORDER_WIDTH), + LV_PROPERTY_ID(STYLE, CLIP_CORNER, LV_PROPERTY_TYPE_INT, LV_STYLE_CLIP_CORNER), + LV_PROPERTY_ID(STYLE, COLOR_FILTER_DSC, LV_PROPERTY_TYPE_INT, LV_STYLE_COLOR_FILTER_DSC), + LV_PROPERTY_ID(STYLE, COLOR_FILTER_OPA, LV_PROPERTY_TYPE_INT, LV_STYLE_COLOR_FILTER_OPA), + LV_PROPERTY_ID(STYLE, FLEX_CROSS_PLACE, LV_PROPERTY_TYPE_INT, LV_STYLE_FLEX_CROSS_PLACE), + LV_PROPERTY_ID(STYLE, FLEX_FLOW, LV_PROPERTY_TYPE_INT, LV_STYLE_FLEX_FLOW), + LV_PROPERTY_ID(STYLE, FLEX_GROW, LV_PROPERTY_TYPE_INT, LV_STYLE_FLEX_GROW), + LV_PROPERTY_ID(STYLE, FLEX_MAIN_PLACE, LV_PROPERTY_TYPE_INT, LV_STYLE_FLEX_MAIN_PLACE), + LV_PROPERTY_ID(STYLE, FLEX_TRACK_PLACE, LV_PROPERTY_TYPE_INT, LV_STYLE_FLEX_TRACK_PLACE), + LV_PROPERTY_ID(STYLE, GRID_CELL_COLUMN_POS, LV_PROPERTY_TYPE_INT, LV_STYLE_GRID_CELL_COLUMN_POS), + LV_PROPERTY_ID(STYLE, GRID_CELL_COLUMN_SPAN, LV_PROPERTY_TYPE_INT, LV_STYLE_GRID_CELL_COLUMN_SPAN), + LV_PROPERTY_ID(STYLE, GRID_CELL_ROW_POS, LV_PROPERTY_TYPE_INT, LV_STYLE_GRID_CELL_ROW_POS), + LV_PROPERTY_ID(STYLE, GRID_CELL_ROW_SPAN, LV_PROPERTY_TYPE_INT, LV_STYLE_GRID_CELL_ROW_SPAN), + LV_PROPERTY_ID(STYLE, GRID_CELL_X_ALIGN, LV_PROPERTY_TYPE_INT, LV_STYLE_GRID_CELL_X_ALIGN), + LV_PROPERTY_ID(STYLE, GRID_CELL_Y_ALIGN, LV_PROPERTY_TYPE_INT, LV_STYLE_GRID_CELL_Y_ALIGN), + LV_PROPERTY_ID(STYLE, GRID_COLUMN_ALIGN, LV_PROPERTY_TYPE_INT, LV_STYLE_GRID_COLUMN_ALIGN), + LV_PROPERTY_ID(STYLE, GRID_COLUMN_DSC_ARRAY, LV_PROPERTY_TYPE_INT, LV_STYLE_GRID_COLUMN_DSC_ARRAY), + LV_PROPERTY_ID(STYLE, GRID_ROW_ALIGN, LV_PROPERTY_TYPE_INT, LV_STYLE_GRID_ROW_ALIGN), + LV_PROPERTY_ID(STYLE, GRID_ROW_DSC_ARRAY, LV_PROPERTY_TYPE_INT, LV_STYLE_GRID_ROW_DSC_ARRAY), + LV_PROPERTY_ID(STYLE, HEIGHT, LV_PROPERTY_TYPE_INT, LV_STYLE_HEIGHT), + LV_PROPERTY_ID(STYLE, IMAGE_OPA, LV_PROPERTY_TYPE_INT, LV_STYLE_IMAGE_OPA), + LV_PROPERTY_ID(STYLE, IMAGE_RECOLOR, LV_PROPERTY_TYPE_COLOR, LV_STYLE_IMAGE_RECOLOR), + LV_PROPERTY_ID(STYLE, IMAGE_RECOLOR_OPA, LV_PROPERTY_TYPE_INT, LV_STYLE_IMAGE_RECOLOR_OPA), + LV_PROPERTY_ID(STYLE, LAST_BUILT_IN_PROP, LV_PROPERTY_TYPE_INT, LV_STYLE_LAST_BUILT_IN_PROP), + LV_PROPERTY_ID(STYLE, LAYOUT, LV_PROPERTY_TYPE_INT, LV_STYLE_LAYOUT), + LV_PROPERTY_ID(STYLE, LENGTH, LV_PROPERTY_TYPE_INT, LV_STYLE_LENGTH), + LV_PROPERTY_ID(STYLE, LINE_COLOR, LV_PROPERTY_TYPE_COLOR, LV_STYLE_LINE_COLOR), + LV_PROPERTY_ID(STYLE, LINE_DASH_GAP, LV_PROPERTY_TYPE_INT, LV_STYLE_LINE_DASH_GAP), + LV_PROPERTY_ID(STYLE, LINE_DASH_WIDTH, LV_PROPERTY_TYPE_INT, LV_STYLE_LINE_DASH_WIDTH), + LV_PROPERTY_ID(STYLE, LINE_OPA, LV_PROPERTY_TYPE_INT, LV_STYLE_LINE_OPA), + LV_PROPERTY_ID(STYLE, LINE_ROUNDED, LV_PROPERTY_TYPE_INT, LV_STYLE_LINE_ROUNDED), + LV_PROPERTY_ID(STYLE, LINE_WIDTH, LV_PROPERTY_TYPE_INT, LV_STYLE_LINE_WIDTH), + LV_PROPERTY_ID(STYLE, MARGIN_BOTTOM, LV_PROPERTY_TYPE_INT, LV_STYLE_MARGIN_BOTTOM), + LV_PROPERTY_ID(STYLE, MARGIN_LEFT, LV_PROPERTY_TYPE_INT, LV_STYLE_MARGIN_LEFT), + LV_PROPERTY_ID(STYLE, MARGIN_RIGHT, LV_PROPERTY_TYPE_INT, LV_STYLE_MARGIN_RIGHT), + LV_PROPERTY_ID(STYLE, MARGIN_TOP, LV_PROPERTY_TYPE_INT, LV_STYLE_MARGIN_TOP), + LV_PROPERTY_ID(STYLE, MAX_HEIGHT, LV_PROPERTY_TYPE_INT, LV_STYLE_MAX_HEIGHT), + LV_PROPERTY_ID(STYLE, MAX_WIDTH, LV_PROPERTY_TYPE_INT, LV_STYLE_MAX_WIDTH), + LV_PROPERTY_ID(STYLE, MIN_HEIGHT, LV_PROPERTY_TYPE_INT, LV_STYLE_MIN_HEIGHT), + LV_PROPERTY_ID(STYLE, MIN_WIDTH, LV_PROPERTY_TYPE_INT, LV_STYLE_MIN_WIDTH), + LV_PROPERTY_ID(STYLE, OPA, LV_PROPERTY_TYPE_INT, LV_STYLE_OPA), + LV_PROPERTY_ID(STYLE, OPA_LAYERED, LV_PROPERTY_TYPE_INT, LV_STYLE_OPA_LAYERED), + LV_PROPERTY_ID(STYLE, OUTLINE_COLOR, LV_PROPERTY_TYPE_COLOR, LV_STYLE_OUTLINE_COLOR), + LV_PROPERTY_ID(STYLE, OUTLINE_OPA, LV_PROPERTY_TYPE_INT, LV_STYLE_OUTLINE_OPA), + LV_PROPERTY_ID(STYLE, OUTLINE_PAD, LV_PROPERTY_TYPE_INT, LV_STYLE_OUTLINE_PAD), + LV_PROPERTY_ID(STYLE, OUTLINE_WIDTH, LV_PROPERTY_TYPE_INT, LV_STYLE_OUTLINE_WIDTH), + LV_PROPERTY_ID(STYLE, PAD_BOTTOM, LV_PROPERTY_TYPE_INT, LV_STYLE_PAD_BOTTOM), + LV_PROPERTY_ID(STYLE, PAD_COLUMN, LV_PROPERTY_TYPE_INT, LV_STYLE_PAD_COLUMN), + LV_PROPERTY_ID(STYLE, PAD_LEFT, LV_PROPERTY_TYPE_INT, LV_STYLE_PAD_LEFT), + LV_PROPERTY_ID(STYLE, PAD_RIGHT, LV_PROPERTY_TYPE_INT, LV_STYLE_PAD_RIGHT), + LV_PROPERTY_ID(STYLE, PAD_ROW, LV_PROPERTY_TYPE_INT, LV_STYLE_PAD_ROW), + LV_PROPERTY_ID(STYLE, PAD_TOP, LV_PROPERTY_TYPE_INT, LV_STYLE_PAD_TOP), + LV_PROPERTY_ID(STYLE, PROP_INV, LV_PROPERTY_TYPE_INT, LV_STYLE_PROP_INV), + LV_PROPERTY_ID(STYLE, RADIUS, LV_PROPERTY_TYPE_INT, LV_STYLE_RADIUS), + LV_PROPERTY_ID(STYLE, ROTARY_SENSITIVITY, LV_PROPERTY_TYPE_INT, LV_STYLE_ROTARY_SENSITIVITY), + LV_PROPERTY_ID(STYLE, SHADOW_COLOR, LV_PROPERTY_TYPE_COLOR, LV_STYLE_SHADOW_COLOR), + LV_PROPERTY_ID(STYLE, SHADOW_OFFSET_X, LV_PROPERTY_TYPE_INT, LV_STYLE_SHADOW_OFFSET_X), + LV_PROPERTY_ID(STYLE, SHADOW_OFFSET_Y, LV_PROPERTY_TYPE_INT, LV_STYLE_SHADOW_OFFSET_Y), + LV_PROPERTY_ID(STYLE, SHADOW_OPA, LV_PROPERTY_TYPE_INT, LV_STYLE_SHADOW_OPA), + LV_PROPERTY_ID(STYLE, SHADOW_SPREAD, LV_PROPERTY_TYPE_INT, LV_STYLE_SHADOW_SPREAD), + LV_PROPERTY_ID(STYLE, SHADOW_WIDTH, LV_PROPERTY_TYPE_INT, LV_STYLE_SHADOW_WIDTH), + LV_PROPERTY_ID(STYLE, TEXT_ALIGN, LV_PROPERTY_TYPE_INT, LV_STYLE_TEXT_ALIGN), + LV_PROPERTY_ID(STYLE, TEXT_COLOR, LV_PROPERTY_TYPE_COLOR, LV_STYLE_TEXT_COLOR), + LV_PROPERTY_ID(STYLE, TEXT_DECOR, LV_PROPERTY_TYPE_INT, LV_STYLE_TEXT_DECOR), + LV_PROPERTY_ID(STYLE, TEXT_FONT, LV_PROPERTY_TYPE_FONT, LV_STYLE_TEXT_FONT), + LV_PROPERTY_ID(STYLE, TEXT_LETTER_SPACE, LV_PROPERTY_TYPE_INT, LV_STYLE_TEXT_LETTER_SPACE), + LV_PROPERTY_ID(STYLE, TEXT_LINE_SPACE, LV_PROPERTY_TYPE_INT, LV_STYLE_TEXT_LINE_SPACE), + LV_PROPERTY_ID(STYLE, TEXT_OPA, LV_PROPERTY_TYPE_INT, LV_STYLE_TEXT_OPA), + LV_PROPERTY_ID(STYLE, TRANSFORM_HEIGHT, LV_PROPERTY_TYPE_INT, LV_STYLE_TRANSFORM_HEIGHT), + LV_PROPERTY_ID(STYLE, TRANSFORM_PIVOT_X, LV_PROPERTY_TYPE_INT, LV_STYLE_TRANSFORM_PIVOT_X), + LV_PROPERTY_ID(STYLE, TRANSFORM_PIVOT_Y, LV_PROPERTY_TYPE_INT, LV_STYLE_TRANSFORM_PIVOT_Y), + LV_PROPERTY_ID(STYLE, TRANSFORM_ROTATION, LV_PROPERTY_TYPE_INT, LV_STYLE_TRANSFORM_ROTATION), + LV_PROPERTY_ID(STYLE, TRANSFORM_SCALE_X, LV_PROPERTY_TYPE_INT, LV_STYLE_TRANSFORM_SCALE_X), + LV_PROPERTY_ID(STYLE, TRANSFORM_SCALE_Y, LV_PROPERTY_TYPE_INT, LV_STYLE_TRANSFORM_SCALE_Y), + LV_PROPERTY_ID(STYLE, TRANSFORM_SKEW_X, LV_PROPERTY_TYPE_INT, LV_STYLE_TRANSFORM_SKEW_X), + LV_PROPERTY_ID(STYLE, TRANSFORM_SKEW_Y, LV_PROPERTY_TYPE_INT, LV_STYLE_TRANSFORM_SKEW_Y), + LV_PROPERTY_ID(STYLE, TRANSFORM_WIDTH, LV_PROPERTY_TYPE_INT, LV_STYLE_TRANSFORM_WIDTH), + LV_PROPERTY_ID(STYLE, TRANSITION, LV_PROPERTY_TYPE_INT, LV_STYLE_TRANSITION), + LV_PROPERTY_ID(STYLE, TRANSLATE_X, LV_PROPERTY_TYPE_INT, LV_STYLE_TRANSLATE_X), + LV_PROPERTY_ID(STYLE, TRANSLATE_Y, LV_PROPERTY_TYPE_INT, LV_STYLE_TRANSLATE_Y), + LV_PROPERTY_ID(STYLE, WIDTH, LV_PROPERTY_TYPE_INT, LV_STYLE_WIDTH), + LV_PROPERTY_ID(STYLE, X, LV_PROPERTY_TYPE_INT, LV_STYLE_X), + LV_PROPERTY_ID(STYLE, Y, LV_PROPERTY_TYPE_INT, LV_STYLE_Y), +}; + +#endif +#endif diff --git a/include/liblvgl/widgets/lv_roller.h b/include/liblvgl/widgets/roller/lv_roller.h similarity index 72% rename from include/liblvgl/widgets/lv_roller.h rename to include/liblvgl/widgets/roller/lv_roller.h index 4984c262..e6a94b29 100644 --- a/include/liblvgl/widgets/lv_roller.h +++ b/include/liblvgl/widgets/roller/lv_roller.h @@ -13,7 +13,7 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" +#include "../../core/lv_obj.h" #if LV_USE_ROLLER != 0 @@ -22,8 +22,7 @@ extern "C" { #error "lv_roller: lv_label is required. Enable it in lv_conf.h (LV_USE_ROLLER 1)" #endif -#include "liblvgl/core/lv_obj.h" -#include "lv_label.h" +#include "../label/lv_label.h" /********************* * DEFINES @@ -33,25 +32,22 @@ extern "C" { * TYPEDEFS **********************/ -/** Roller mode.*/ +/** Roller mode. */ +typedef enum { + LV_ROLLER_MODE_NORMAL, /**< Normal mode (roller ends at the end of the options). */ + LV_ROLLER_MODE_INFINITE, /**< Infinite mode (roller can be scrolled forever). */ +} lv_roller_mode_t; + +#if LV_USE_OBJ_PROPERTY enum { - LV_ROLLER_MODE_NORMAL, /**< Normal mode (roller ends at the end of the options).*/ - LV_ROLLER_MODE_INFINITE, /**< Infinite mode (roller can be scrolled forever).*/ + LV_PROPERTY_ID(ROLLER, OPTIONS, LV_PROPERTY_TYPE_TEXT, 0), + LV_PROPERTY_ID(ROLLER, SELECTED, LV_PROPERTY_TYPE_INT, 1), + LV_PROPERTY_ID(ROLLER, VISIBLE_ROW_COUNT, LV_PROPERTY_TYPE_INT, 2), + LV_PROPERTY_ROLLER_END, }; +#endif -typedef uint8_t lv_roller_mode_t; - -typedef struct { - lv_obj_t obj; - uint16_t option_cnt; /**< Number of options*/ - uint16_t sel_opt_id; /**< Index of the current option*/ - uint16_t sel_opt_id_ori; /**< Store the original index on focus*/ - lv_roller_mode_t mode : 1; - uint32_t moved : 1; -} lv_roller_t; - -extern const lv_obj_class_t lv_roller_class; - +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_roller_class; /********************** * GLOBAL PROTOTYPES @@ -80,16 +76,16 @@ void lv_roller_set_options(lv_obj_t * obj, const char * options, lv_roller_mode_ * Set the selected option * @param obj pointer to a roller object * @param sel_opt index of the selected option (0 ... number of option - 1); - * @param anim_en LV_ANIM_ON: set with animation; LV_ANOM_OFF set immediately + * @param anim LV_ANIM_ON: set with animation; LV_ANOM_OFF set immediately */ -void lv_roller_set_selected(lv_obj_t * obj, uint16_t sel_opt, lv_anim_enable_t anim); +void lv_roller_set_selected(lv_obj_t * obj, uint32_t sel_opt, lv_anim_enable_t anim); /** * Set the height to show the given number of rows (options) * @param obj pointer to a roller object * @param row_cnt number of desired visible rows */ -void lv_roller_set_visible_row_count(lv_obj_t * obj, uint8_t row_cnt); +void lv_roller_set_visible_row_count(lv_obj_t * obj, uint32_t row_cnt); /*===================== * Getter functions @@ -100,17 +96,16 @@ void lv_roller_set_visible_row_count(lv_obj_t * obj, uint8_t row_cnt); * @param obj pointer to a roller object * @return index of the selected option (0 ... number of option - 1); */ -uint16_t lv_roller_get_selected(const lv_obj_t * obj); +uint32_t lv_roller_get_selected(const lv_obj_t * obj); /** * Get the current selected option as a string. - * @param obj pointer to ddlist object + * @param obj pointer to roller object * @param buf pointer to an array to store the string * @param buf_size size of `buf` in bytes. 0: to ignore it. */ void lv_roller_get_selected_str(const lv_obj_t * obj, char * buf, uint32_t buf_size); - /** * Get the options of a roller * @param obj pointer to roller object @@ -123,7 +118,7 @@ const char * lv_roller_get_options(const lv_obj_t * obj); * @param obj pointer to a roller object * @return the total number of options */ -uint16_t lv_roller_get_option_cnt(const lv_obj_t * obj); +uint32_t lv_roller_get_option_count(const lv_obj_t * obj); /********************** * MACROS diff --git a/include/liblvgl/widgets/roller/lv_roller_private.h b/include/liblvgl/widgets/roller/lv_roller_private.h new file mode 100644 index 00000000..686c0d50 --- /dev/null +++ b/include/liblvgl/widgets/roller/lv_roller_private.h @@ -0,0 +1,55 @@ +/** + * @file lv_roller_private.h + * + */ + +#ifndef LV_ROLLER_PRIVATE_H +#define LV_ROLLER_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_roller.h" + +#if LV_USE_ROLLER != 0 +#include "../../core/lv_obj_private.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_roller_t { + lv_obj_t obj; + uint32_t option_cnt; /**< Number of options*/ + uint32_t sel_opt_id; /**< Index of the current option*/ + uint32_t sel_opt_id_ori; /**< Store the original index on focus*/ + uint32_t inf_page_cnt; /**< Number of extra pages added to make the roller look infinite */ + lv_roller_mode_t mode : 2; + uint32_t moved : 1; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_ROLLER != 0 */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_ROLLER_PRIVATE_H*/ diff --git a/include/liblvgl/widgets/scale/lv_scale.h b/include/liblvgl/widgets/scale/lv_scale.h new file mode 100644 index 00000000..ad1bd2b4 --- /dev/null +++ b/include/liblvgl/widgets/scale/lv_scale.h @@ -0,0 +1,258 @@ +/** + * @file lv_scale.h + * + */ + +#ifndef LV_SCALE_H +#define LV_SCALE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../lv_conf_internal.h" + +#if LV_USE_SCALE != 0 + +#include "../../core/lv_obj.h" +#include "../line/lv_line.h" +#include "../image/lv_image.h" + +/********************* + * DEFINES + *********************/ + +/**Default value of total minor ticks. */ +#define LV_SCALE_TOTAL_TICK_COUNT_DEFAULT (11U) +LV_EXPORT_CONST_INT(LV_SCALE_TOTAL_TICK_COUNT_DEFAULT); + +/**Default value of major tick every nth ticks. */ +#define LV_SCALE_MAJOR_TICK_EVERY_DEFAULT (5U) +LV_EXPORT_CONST_INT(LV_SCALE_MAJOR_TICK_EVERY_DEFAULT); + +/**Default value of scale label enabled. */ +#define LV_SCALE_LABEL_ENABLED_DEFAULT (1U) +LV_EXPORT_CONST_INT(LV_SCALE_LABEL_ENABLED_DEFAULT); + +/********************** + * TYPEDEFS + **********************/ + +/** + * Scale mode + */ +typedef enum { + LV_SCALE_MODE_HORIZONTAL_TOP = 0x00U, + LV_SCALE_MODE_HORIZONTAL_BOTTOM = 0x01U, + LV_SCALE_MODE_VERTICAL_LEFT = 0x02U, + LV_SCALE_MODE_VERTICAL_RIGHT = 0x04U, + LV_SCALE_MODE_ROUND_INNER = 0x08U, + LV_SCALE_MODE_ROUND_OUTER = 0x10U, + LV_SCALE_MODE_LAST +} lv_scale_mode_t; + +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_scale_class; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create an scale object + * @param parent pointer to an object, it will be the parent of the new scale + * @return pointer to the created scale + */ +lv_obj_t * lv_scale_create(lv_obj_t * parent); + +/*====================== + * Add/remove functions + *=====================*/ + +/*===================== + * Setter functions + *====================*/ + +/** + * Set scale mode. See lv_scale_mode_t + * @param obj pointer the scale object + * @param mode the new scale mode + */ +void lv_scale_set_mode(lv_obj_t * obj, lv_scale_mode_t mode); + +/** + * Set scale total tick count (including minor and major ticks) + * @param obj pointer the scale object + * @param total_tick_count New total tick count + */ +void lv_scale_set_total_tick_count(lv_obj_t * obj, uint32_t total_tick_count); + +/** + * Sets how often the major tick will be drawn + * @param obj pointer the scale object + * @param major_tick_every the new count for major tick drawing + */ +void lv_scale_set_major_tick_every(lv_obj_t * obj, uint32_t major_tick_every); + +/** + * Sets label visibility + * @param obj pointer the scale object + * @param show_label true/false to enable tick label + */ +void lv_scale_set_label_show(lv_obj_t * obj, bool show_label); + +/** + * Set the minimal and maximal values on a scale + * @param obj pointer to a scale object + * @param min minimum value of the scale + * @param max maximum value of the scale + */ +void lv_scale_set_range(lv_obj_t * obj, int32_t min, int32_t max); + +/** + * Set properties specific to round scale + * @param obj pointer to a scale object + * @param angle_range the angular range of the scale + */ +void lv_scale_set_angle_range(lv_obj_t * obj, uint32_t angle_range); + +/** + * Set properties specific to round scale + * @param obj pointer to a scale object + * @param rotation the angular offset from the 3 o'clock position (clock-wise) + */ +void lv_scale_set_rotation(lv_obj_t * obj, int32_t rotation); + +/** + * Point the needle to the corresponding value through the line + * @param obj pointer to a scale object + * @param needle_line needle_line of the scale. The line points will be allocated and + * managed by the scale unless the line point array was previously set + * using `lv_line_set_points_mutable`. + * @param needle_length length of the needle + * needle_length>0 needle_length=needle_length; + * needle_length<0 needle_length=radius-|needle_length|; + * @param value needle to point to the corresponding value + */ +void lv_scale_set_line_needle_value(lv_obj_t * obj, lv_obj_t * needle_line, int32_t needle_length, + int32_t value); + +/** + * Point the needle to the corresponding value through the image, + image must point to the right. E.g. -O------> + * @param obj pointer to a scale object + * @param needle_img needle_img of the scale + * @param value needle to point to the corresponding value + */ +void lv_scale_set_image_needle_value(lv_obj_t * obj, lv_obj_t * needle_img, int32_t value); + +/** + * Set custom text source for major ticks labels + * @param obj pointer to a scale object + * @param txt_src pointer to an array of strings which will be display at major ticks + */ +void lv_scale_set_text_src(lv_obj_t * obj, const char * txt_src[]); + +/** + * Draw the scale after all the children are drawn + * @param obj pointer to a scale object + * @param en true: enable post draw + */ +void lv_scale_set_post_draw(lv_obj_t * obj, bool en); + +/** + * Draw the scale ticks on top of all parts + * @param obj pointer to a scale object + * @param en true: enable draw ticks on top of all parts + */ +void lv_scale_set_draw_ticks_on_top(lv_obj_t * obj, bool en); + +/** + * Add a section to the given scale + * @param obj pointer to a scale object + * @return pointer to the new section + */ +lv_scale_section_t * lv_scale_add_section(lv_obj_t * obj); + +/** + * Set the range for the given scale section + * @param section pointer to a scale section object + * @param minor_range section new minor range + * @param major_range section new major range + */ +void lv_scale_section_set_range(lv_scale_section_t * section, int32_t minor_range, int32_t major_range); + +/** + * Set the style of the part for the given scale section + * @param section pointer to a scale section object + * @param part the part for the section, e.g. LV_PART_INDICATOR + * @param section_part_style Pointer to the section part style + */ +void lv_scale_section_set_style(lv_scale_section_t * section, lv_part_t part, lv_style_t * section_part_style); + +/*===================== + * Getter functions + *====================*/ + +/** + * Get scale mode. See lv_scale_mode_t + * @param obj pointer the scale object + * @return Scale mode + */ +lv_scale_mode_t lv_scale_get_mode(lv_obj_t * obj); + +/** + * Get scale total tick count (including minor and major ticks) + * @param obj pointer the scale object + * @return Scale total tick count + */ +int32_t lv_scale_get_total_tick_count(lv_obj_t * obj); + +/** + * Gets how often the major tick will be drawn + * @param obj pointer the scale object + * @return Scale major tick every count + */ +int32_t lv_scale_get_major_tick_every(lv_obj_t * obj); + +/** + * Gets label visibility + * @param obj pointer the scale object + * @return true if tick label is enabled, false otherwise + */ +bool lv_scale_get_label_show(lv_obj_t * obj); + +/** + * Get angle range of a round scale + * @param obj pointer to a scale object + * @return Scale angle_range + */ +uint32_t lv_scale_get_angle_range(lv_obj_t * obj); + +/** + * Get the min range for the given scale section + * @param obj pointer to a scale section object + * @return section minor range + */ +int32_t lv_scale_get_range_min_value(lv_obj_t * obj); + +/** + * Get the max range for the given scale section + * @param obj pointer to a scale section object + * @return section max range + */ +int32_t lv_scale_get_range_max_value(lv_obj_t * obj); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_SCALE*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_SCALE_H*/ diff --git a/include/liblvgl/widgets/scale/lv_scale_private.h b/include/liblvgl/widgets/scale/lv_scale_private.h new file mode 100644 index 00000000..c60cf196 --- /dev/null +++ b/include/liblvgl/widgets/scale/lv_scale_private.h @@ -0,0 +1,82 @@ +/** + * @file lv_scale_private.h + * + */ + +#ifndef LV_SCALE_PRIVATE_H +#define LV_SCALE_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_scale.h" + +#if LV_USE_SCALE != 0 +#include "../../core/lv_obj_private.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_scale_section_t { + lv_style_t * main_style; + lv_style_t * indicator_style; + lv_style_t * items_style; + int32_t minor_range; + int32_t major_range; + uint32_t first_tick_idx_in_section; + uint32_t last_tick_idx_in_section; + uint32_t first_tick_idx_is_major; + uint32_t last_tick_idx_is_major; + int32_t first_tick_in_section_width; + int32_t last_tick_in_section_width; + lv_point_t first_tick_in_section; + lv_point_t last_tick_in_section; +}; + +struct _lv_scale_t { + lv_obj_t obj; + lv_ll_t section_ll; /**< Linked list for the sections (stores lv_scale_section_t)*/ + const char ** txt_src; + lv_scale_mode_t mode; + int32_t range_min; + int32_t range_max; + uint32_t total_tick_count : 15; + uint32_t major_tick_every : 15; + uint32_t label_enabled : 1; + uint32_t post_draw : 1; + uint32_t draw_ticks_on_top : 1; + /* Round scale */ + uint32_t angle_range; + int32_t rotation; + /* Private properties */ + int32_t custom_label_cnt; + int32_t last_tick_width; + int32_t first_tick_width; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_SCALE != 0 */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_SCALE_PRIVATE_H*/ diff --git a/include/liblvgl/widgets/lv_slider.h b/include/liblvgl/widgets/slider/lv_slider.h similarity index 57% rename from include/liblvgl/widgets/lv_slider.h rename to include/liblvgl/widgets/slider/lv_slider.h index 20c940f8..5f08a5c5 100644 --- a/include/liblvgl/widgets/lv_slider.h +++ b/include/liblvgl/widgets/slider/lv_slider.h @@ -13,7 +13,7 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" +#include "../bar/lv_bar.h" #if LV_USE_SLIDER != 0 @@ -22,9 +22,6 @@ extern "C" { #error "lv_slider: lv_bar is required. Enable it in lv_conf.h (LV_USE_BAR 1)" #endif -#include "liblvgl/core/lv_obj.h" -#include "lv_bar.h" - /********************* * DEFINES *********************/ @@ -32,33 +29,13 @@ extern "C" { /********************** * TYPEDEFS **********************/ - -enum { +typedef enum { LV_SLIDER_MODE_NORMAL = LV_BAR_MODE_NORMAL, LV_SLIDER_MODE_SYMMETRICAL = LV_BAR_MODE_SYMMETRICAL, LV_SLIDER_MODE_RANGE = LV_BAR_MODE_RANGE -}; -typedef uint8_t lv_slider_mode_t; - -typedef struct { - lv_bar_t bar; /*Add the ancestor's type first*/ - lv_area_t left_knob_area; - lv_area_t right_knob_area; - int32_t * value_to_set; /*Which bar value to set*/ - uint8_t dragging : 1; /*1: the slider is being dragged*/ - uint8_t left_knob_focus : 1; /*1: with encoder now the right knob can be adjusted*/ -} lv_slider_t; - -extern const lv_obj_class_t lv_slider_class; +} lv_slider_mode_t; -/** - * `type` field in `lv_obj_draw_part_dsc_t` if `class_p = lv_slider_class` - * Used in `LV_EVENT_DRAW_PART_BEGIN` and `LV_EVENT_DRAW_PART_END` - */ -typedef enum { - LV_SLIDER_DRAW_PART_KNOB, /**< The main (right) knob's rectangle*/ - LV_SLIDER_DRAW_PART_KNOB_LEFT, /**< The left knob's rectangle*/ -} lv_slider_draw_part_type_t; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_slider_class; /********************** * GLOBAL PROTOTYPES @@ -81,10 +58,7 @@ lv_obj_t * lv_slider_create(lv_obj_t * parent); * @param value the new value * @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately */ -static inline void lv_slider_set_value(lv_obj_t * obj, int32_t value, lv_anim_enable_t anim) -{ - lv_bar_set_value(obj, value, anim); -} +void lv_slider_set_value(lv_obj_t * obj, int32_t value, lv_anim_enable_t anim); /** * Set a new value for the left knob of a slider @@ -92,10 +66,7 @@ static inline void lv_slider_set_value(lv_obj_t * obj, int32_t value, lv_anim_en * @param value new value * @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately */ -static inline void lv_slider_set_left_value(lv_obj_t * obj, int32_t value, lv_anim_enable_t anim) -{ - lv_bar_set_start_value(obj, value, anim); -} +void lv_slider_set_left_value(lv_obj_t * obj, int32_t value, lv_anim_enable_t anim); /** * Set minimum and the maximum values of a bar @@ -103,20 +74,14 @@ static inline void lv_slider_set_left_value(lv_obj_t * obj, int32_t value, lv_an * @param min minimum value * @param max maximum value */ -static inline void lv_slider_set_range(lv_obj_t * obj, int32_t min, int32_t max) -{ - lv_bar_set_range(obj, min, max); -} +void lv_slider_set_range(lv_obj_t * obj, int32_t min, int32_t max); /** * Set the mode of slider. * @param obj pointer to a slider object * @param mode the mode of the slider. See ::lv_slider_mode_t */ -static inline void lv_slider_set_mode(lv_obj_t * obj, lv_slider_mode_t mode) -{ - lv_bar_set_mode(obj, (lv_bar_mode_t)mode); -} +void lv_slider_set_mode(lv_obj_t * obj, lv_slider_mode_t mode); /*===================== * Getter functions @@ -127,40 +92,28 @@ static inline void lv_slider_set_mode(lv_obj_t * obj, lv_slider_mode_t mode) * @param obj pointer to a slider object * @return the value of the main knob of the slider */ -static inline int32_t lv_slider_get_value(const lv_obj_t * obj) -{ - return lv_bar_get_value(obj); -} +int32_t lv_slider_get_value(const lv_obj_t * obj); /** * Get the value of the left knob of a slider * @param obj pointer to a slider object * @return the value of the left knob of the slider */ -static inline int32_t lv_slider_get_left_value(const lv_obj_t * obj) -{ - return lv_bar_get_start_value(obj); -} +int32_t lv_slider_get_left_value(const lv_obj_t * obj); /** * Get the minimum value of a slider * @param obj pointer to a slider object * @return the minimum value of the slider */ -static inline int32_t lv_slider_get_min_value(const lv_obj_t * obj) -{ - return lv_bar_get_min_value(obj); -} +int32_t lv_slider_get_min_value(const lv_obj_t * obj); /** * Get the maximum value of a slider * @param obj pointer to a slider object * @return the maximum value of the slider */ -static inline int32_t lv_slider_get_max_value(const lv_obj_t * obj) -{ - return lv_bar_get_max_value(obj); -} +int32_t lv_slider_get_max_value(const lv_obj_t * obj); /** * Give the slider is being dragged or not @@ -171,16 +124,17 @@ bool lv_slider_is_dragged(const lv_obj_t * obj); /** * Get the mode of the slider. - * @param obj pointer to a bar object + * @param slider pointer to a slider object * @return see ::lv_slider_mode_t */ -static inline lv_slider_mode_t lv_slider_get_mode(lv_obj_t * slider) -{ - lv_bar_mode_t mode = lv_bar_get_mode(slider); - if(mode == LV_BAR_MODE_SYMMETRICAL) return LV_SLIDER_MODE_SYMMETRICAL; - else if(mode == LV_BAR_MODE_RANGE) return LV_SLIDER_MODE_RANGE; - else return LV_SLIDER_MODE_NORMAL; -} +lv_slider_mode_t lv_slider_get_mode(lv_obj_t * slider); + +/** + * Give the slider is in symmetrical mode or not + * @param obj pointer to slider object + * @return true: in symmetrical mode false : not in +*/ +bool lv_slider_is_symmetrical(lv_obj_t * obj); /********************** * MACROS diff --git a/include/liblvgl/widgets/slider/lv_slider_private.h b/include/liblvgl/widgets/slider/lv_slider_private.h new file mode 100644 index 00000000..0b4464db --- /dev/null +++ b/include/liblvgl/widgets/slider/lv_slider_private.h @@ -0,0 +1,55 @@ +/** + * @file lv_slider_private.h + * + */ + +#ifndef LV_SLIDER_PRIVATE_H +#define LV_SLIDER_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../bar/lv_bar_private.h" +#include "lv_slider.h" + +#if LV_USE_SLIDER != 0 + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_slider_t { + lv_bar_t bar; /**< Add the ancestor's type first */ + lv_area_t left_knob_area; + lv_area_t right_knob_area; + lv_point_t pressed_point; + int32_t * value_to_set; /**< Which bar value to set */ + uint8_t dragging : 1; /**< 1: the slider is being dragged */ + uint8_t left_knob_focus : 1; /**< 1: with encoder now the right knob can be adjusted */ +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_SLIDER != 0 */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_SLIDER_PRIVATE_H*/ diff --git a/include/liblvgl/widgets/span/lv_span.h b/include/liblvgl/widgets/span/lv_span.h new file mode 100644 index 00000000..17bcf51e --- /dev/null +++ b/include/liblvgl/widgets/span/lv_span.h @@ -0,0 +1,284 @@ +/** + * @file lv_span.h + * + */ + +#ifndef LV_SPAN_H +#define LV_SPAN_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../lv_conf_internal.h" +#include "../../core/lv_obj.h" + +#if LV_USE_SPAN != 0 + +/********************* + * DEFINES + *********************/ +#ifndef LV_SPAN_SNIPPET_STACK_SIZE +#define LV_SPAN_SNIPPET_STACK_SIZE 64 +#endif + +/********************** + * TYPEDEFS + **********************/ +typedef enum { + LV_SPAN_OVERFLOW_CLIP, + LV_SPAN_OVERFLOW_ELLIPSIS, + LV_SPAN_OVERFLOW_LAST, /**< Fence member*/ +} lv_span_overflow_t; + +typedef enum { + LV_SPAN_MODE_FIXED, /**< fixed the obj size */ + LV_SPAN_MODE_EXPAND, /**< Expand the object size to the text size */ + LV_SPAN_MODE_BREAK, /**< Keep width, break the too long lines and expand height */ + LV_SPAN_MODE_LAST /**< Fence member */ +} lv_span_mode_t; + +/** Coords of a span */ +typedef struct _lv_span_coords_t { + lv_area_t heading; + lv_area_t middle; + lv_area_t trailing; +} lv_span_coords_t; + +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_spangroup_class; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +void lv_span_stack_init(void); +void lv_span_stack_deinit(void); + +/** + * Create a spangroup object + * @param parent pointer to an object, it will be the parent of the new spangroup + * @return pointer to the created spangroup + */ +lv_obj_t * lv_spangroup_create(lv_obj_t * parent); + +/** + * Create a span string descriptor and add to spangroup. + * @param obj pointer to a spangroup object. + * @return pointer to the created span. + */ +lv_span_t * lv_spangroup_new_span(lv_obj_t * obj); + +/** + * Remove the span from the spangroup and free memory. + * @param obj pointer to a spangroup object. + * @param span pointer to a span. + */ +void lv_spangroup_delete_span(lv_obj_t * obj, lv_span_t * span); + +/*===================== + * Setter functions + *====================*/ + +/** + * Set a new text for a span. Memory will be allocated to store the text by the span. + * @param span pointer to a span. + * @param text pointer to a text. + */ +void lv_span_set_text(lv_span_t * span, const char * text); + +/** + * Set a static text. It will not be saved by the span so the 'text' variable + * has to be 'alive' while the span exist. + * @param span pointer to a span. + * @param text pointer to a text. + */ +void lv_span_set_text_static(lv_span_t * span, const char * text); + +/** + * Set the align of the spangroup. + * @param obj pointer to a spangroup object. + * @param align see lv_text_align_t for details. + */ +void lv_spangroup_set_align(lv_obj_t * obj, lv_text_align_t align); + +/** + * Set the overflow of the spangroup. + * @param obj pointer to a spangroup object. + * @param overflow see lv_span_overflow_t for details. + */ +void lv_spangroup_set_overflow(lv_obj_t * obj, lv_span_overflow_t overflow); + +/** + * Set the indent of the spangroup. + * @param obj pointer to a spangroup object. + * @param indent the first line indentation + */ +void lv_spangroup_set_indent(lv_obj_t * obj, int32_t indent); + +/** + * Set the mode of the spangroup. + * @param obj pointer to a spangroup object. + * @param mode see lv_span_mode_t for details. + */ +void lv_spangroup_set_mode(lv_obj_t * obj, lv_span_mode_t mode); + +/** + * Set maximum lines of the spangroup. + * @param obj pointer to a spangroup object. + * @param lines max lines that can be displayed in LV_SPAN_MODE_BREAK mode. < 0 means no limit. + */ +void lv_spangroup_set_max_lines(lv_obj_t * obj, int32_t lines); + +/*===================== + * Getter functions + *====================*/ + +/** + * Get a pointer to the style of a span + * @param span pointer to the span + * @return pointer to the style. valid as long as the span is valid +*/ +lv_style_t * lv_span_get_style(lv_span_t * span); + +/** + * Get a pointer to the text of a span + * @param span pointer to the span + * @return pointer to the text +*/ +const char * lv_span_get_text(lv_span_t * span); + +/** + * Get a spangroup child by its index. + * + * @param obj The spangroup object + * @param id the index of the child. + * 0: the oldest (firstly created) child + * 1: the second oldest + * child count-1: the youngest + * -1: the youngest + * -2: the second youngest + * @return The child span at index `id`, or NULL if the ID does not exist + */ +lv_span_t * lv_spangroup_get_child(const lv_obj_t * obj, int32_t id); + +/** + * Get number of spans + * @param obj the spangroup object to get the child count of. + * @return the span count of the spangroup. + */ +uint32_t lv_spangroup_get_span_count(const lv_obj_t * obj); + +/** + * Get the align of the spangroup. + * @param obj pointer to a spangroup object. + * @return the align value. + */ +lv_text_align_t lv_spangroup_get_align(lv_obj_t * obj); + +/** + * Get the overflow of the spangroup. + * @param obj pointer to a spangroup object. + * @return the overflow value. + */ +lv_span_overflow_t lv_spangroup_get_overflow(lv_obj_t * obj); + +/** + * Get the indent of the spangroup. + * @param obj pointer to a spangroup object. + * @return the indent value. + */ +int32_t lv_spangroup_get_indent(lv_obj_t * obj); + +/** + * Get the mode of the spangroup. + * @param obj pointer to a spangroup object. + */ +lv_span_mode_t lv_spangroup_get_mode(lv_obj_t * obj); + +/** + * Get maximum lines of the spangroup. + * @param obj pointer to a spangroup object. + * @return the max lines value. + */ +int32_t lv_spangroup_get_max_lines(lv_obj_t * obj); + +/** + * Get max line height of all span in the spangroup. + * @param obj pointer to a spangroup object. + */ +int32_t lv_spangroup_get_max_line_height(lv_obj_t * obj); + +/** + * Get the text content width when all span of spangroup on a line. + * @param obj pointer to a spangroup object. + * @param max_width if text content width >= max_width, return max_width + * to reduce computation, if max_width == 0, returns the text content width. + * @return text content width or max_width. + */ +uint32_t lv_spangroup_get_expand_width(lv_obj_t * obj, uint32_t max_width); + +/** + * Get the text content height with width fixed. + * @param obj pointer to a spangroup object. + * @param width the width of the span group. + + */ +int32_t lv_spangroup_get_expand_height(lv_obj_t * obj, int32_t width); + +/** + * Get the span's coords in the spangroup. + * @note Before calling this function, please make sure that the layout of span group has been updated. + * Like calling lv_obj_update_layout() like function. + * + * +--------+ + * |Heading +--->------------------+ + * | Pos | | Heading | + * +--------+---+------------------+ + * | | + * | | + * | | + * | Middle +--------+| + * | |Trailing|| + * | +-| Pos || + * | | +--------+| + * +-------------------v-----------+ + * | Trailing | + * +-------------------+ + * @param obj pointer to a spangroup object. + * @param span pointer to a span. + * @return the span's coords in the spangroup. + */ +lv_span_coords_t lv_spangroup_get_span_coords(lv_obj_t * obj, const lv_span_t * span); + +/** + * Get the span object by point. + * @param obj pointer to a spangroup object. + * @param point pointer to point containing absolute coordinates + * @return pointer to the span under the point or `NULL` if not found. + */ +lv_span_t * lv_spangroup_get_span_by_point(lv_obj_t * obj, const lv_point_t * point); + +/*===================== + * Other functions + *====================*/ + +/** + * Update the mode of the spangroup. + * @param obj pointer to a spangroup object. + */ +void lv_spangroup_refr_mode(lv_obj_t * obj); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_SPAN*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /*LV_SPAN_H*/ diff --git a/include/liblvgl/widgets/span/lv_span_private.h b/include/liblvgl/widgets/span/lv_span_private.h new file mode 100644 index 00000000..6e80405e --- /dev/null +++ b/include/liblvgl/widgets/span/lv_span_private.h @@ -0,0 +1,68 @@ +/** + * @file lv_span_private.h + * + */ + +#ifndef LV_SPAN_PRIVATE_H +#define LV_SPAN_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../core/lv_obj_private.h" +#include "lv_span.h" + +#if LV_USE_SPAN != 0 + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_span_t { + char * txt; /**< a pointer to display text */ + lv_obj_t * spangroup; /**< a pointer to spangroup */ + lv_style_t style; /**< display text style */ + uint32_t static_flag : 1; /**< the text is static flag */ + + lv_point_t trailing_pos; + int32_t trailing_height; +}; + +/** Data of label*/ +struct _lv_spangroup_t { + lv_obj_t obj; + int32_t lines; + int32_t indent; /**< first line indent */ + int32_t cache_w; /**< the cache automatically calculates the width */ + int32_t cache_h; /**< similar cache_w */ + lv_ll_t child_ll; + uint32_t mode : 2; /**< details see lv_span_mode_t */ + uint32_t overflow : 1; /**< details see lv_span_overflow_t */ + uint32_t refresh : 1; /**< the spangroup need refresh cache_w and cache_h */ +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_SPAN != 0 */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_SPAN_PRIVATE_H*/ diff --git a/include/liblvgl/extra/widgets/spinbox/lv_spinbox.h b/include/liblvgl/widgets/spinbox/lv_spinbox.h similarity index 53% rename from include/liblvgl/extra/widgets/spinbox/lv_spinbox.h rename to include/liblvgl/widgets/spinbox/lv_spinbox.h index 830178fa..ae3fdb0c 100644 --- a/include/liblvgl/extra/widgets/spinbox/lv_spinbox.h +++ b/include/liblvgl/widgets/spinbox/lv_spinbox.h @@ -13,7 +13,7 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lvgl.h" +#include "../textarea/lv_textarea.h" #if LV_USE_SPINBOX @@ -31,30 +31,16 @@ extern "C" { * TYPEDEFS **********************/ -/*Data of spinbox*/ -typedef struct { - lv_textarea_t ta; /*Ext. of ancestor*/ - /*New data for this type*/ - int32_t value; - int32_t range_max; - int32_t range_min; - int32_t step; - uint16_t digit_count : 4; - uint16_t dec_point_pos : 4; /*if 0, there is no separator and the number is an integer*/ - uint16_t rollover : 1; // Set to true for rollover functionality - uint16_t digit_step_dir : 2; // the direction the digit will step on encoder button press when editing -} lv_spinbox_t; - -extern const lv_obj_class_t lv_spinbox_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_spinbox_class; /********************** * GLOBAL PROTOTYPES **********************/ /** - * Create a Spinbox object - * @param parent pointer to an object, it will be the parent of the new spinbox - * @return pointer to the created spinbox + * Create a spinbox object + * @param parent pointer to an object, it will be the parent of the new spinbox + * @return pointer to the created spinbox */ lv_obj_t * lv_spinbox_create(lv_obj_t * parent); @@ -64,37 +50,37 @@ lv_obj_t * lv_spinbox_create(lv_obj_t * parent); /** * Set spinbox value - * @param obj pointer to spinbox - * @param i value to be set + * @param obj pointer to spinbox + * @param v value to be set */ -void lv_spinbox_set_value(lv_obj_t * obj, int32_t i); +void lv_spinbox_set_value(lv_obj_t * obj, int32_t v); /** * Set spinbox rollover function - * @param obj pointer to spinbox - * @param b true or false to enable or disable (default) + * @param obj pointer to spinbox + * @param rollover true or false to enable or disable (default) */ -void lv_spinbox_set_rollover(lv_obj_t * obj, bool b); +void lv_spinbox_set_rollover(lv_obj_t * obj, bool rollover); /** * Set spinbox digit format (digit count and decimal format) - * @param obj pointer to spinbox - * @param digit_count number of digit excluding the decimal separator and the sign - * @param separator_position number of digit before the decimal point. If 0, decimal point is not + * @param obj pointer to spinbox + * @param digit_count number of digit excluding the decimal separator and the sign + * @param sep_pos number of digit before the decimal point. If 0, decimal point is not * shown */ -void lv_spinbox_set_digit_format(lv_obj_t * obj, uint8_t digit_count, uint8_t separator_position); +void lv_spinbox_set_digit_format(lv_obj_t * obj, uint32_t digit_count, uint32_t sep_pos); /** * Set spinbox step - * @param obj pointer to spinbox - * @param step steps on increment/decrement. Can be 1, 10, 100, 1000, etc the digit that will change. + * @param obj pointer to spinbox + * @param step steps on increment/decrement. Can be 1, 10, 100, 1000, etc the digit that will change. */ void lv_spinbox_set_step(lv_obj_t * obj, uint32_t step); /** * Set spinbox value range - * @param obj pointer to spinbox + * @param obj pointer to spinbox * @param range_min maximum value, inclusive * @param range_max minimum value, inclusive */ @@ -102,15 +88,15 @@ void lv_spinbox_set_range(lv_obj_t * obj, int32_t range_min, int32_t range_max); /** * Set cursor position to a specific digit for edition - * @param obj pointer to spinbox - * @param pos selected position in spinbox + * @param obj pointer to spinbox + * @param pos selected position in spinbox */ -void lv_spinbox_set_cursor_pos(lv_obj_t * obj, uint8_t pos); +void lv_spinbox_set_cursor_pos(lv_obj_t * obj, uint32_t pos); /** * Set direction of digit step when clicking an encoder button while in editing mode - * @param obj pointer to spinbox - * @param direction the direction (LV_DIR_RIGHT or LV_DIR_LEFT) + * @param obj pointer to spinbox + * @param direction the direction (LV_DIR_RIGHT or LV_DIR_LEFT) */ void lv_spinbox_set_digit_step_direction(lv_obj_t * obj, lv_dir_t direction); @@ -120,21 +106,21 @@ void lv_spinbox_set_digit_step_direction(lv_obj_t * obj, lv_dir_t direction); /** * Get spinbox rollover function status - * @param obj pointer to spinbox + * @param obj pointer to spinbox */ bool lv_spinbox_get_rollover(lv_obj_t * obj); /** * Get the spinbox numeral value (user has to convert to float according to its digit format) - * @param obj pointer to spinbox - * @return value integer value of the spinbox + * @param obj pointer to spinbox + * @return value integer value of the spinbox */ int32_t lv_spinbox_get_value(lv_obj_t * obj); /** * Get the spinbox step value (user has to convert to float according to its digit format) - * @param obj pointer to spinbox - * @return value integer step value of the spinbox + * @param obj pointer to spinbox + * @return value integer step value of the spinbox */ int32_t lv_spinbox_get_step(lv_obj_t * obj); @@ -144,25 +130,25 @@ int32_t lv_spinbox_get_step(lv_obj_t * obj); /** * Select next lower digit for edition by dividing the step by 10 - * @param obj pointer to spinbox + * @param obj pointer to spinbox */ void lv_spinbox_step_next(lv_obj_t * obj); /** * Select next higher digit for edition by multiplying the step by 10 - * @param obj pointer to spinbox + * @param obj pointer to spinbox */ void lv_spinbox_step_prev(lv_obj_t * obj); /** * Increment spinbox value by one step - * @param obj pointer to spinbox + * @param obj pointer to spinbox */ void lv_spinbox_increment(lv_obj_t * obj); /** * Decrement spinbox value by one step - * @param obj pointer to spinbox + * @param obj pointer to spinbox */ void lv_spinbox_decrement(lv_obj_t * obj); @@ -170,10 +156,6 @@ void lv_spinbox_decrement(lv_obj_t * obj); * MACROS **********************/ -/* It was ambiguous in MicroPython. See https://github.com/lvgl/lvgl/issues/3301 - * TODO remove in v9*/ -#define lv_spinbox_set_pos lv_spinbox_set_cursor_pos - #endif /*LV_USE_SPINBOX*/ #ifdef __cplusplus diff --git a/include/liblvgl/widgets/spinbox/lv_spinbox_private.h b/include/liblvgl/widgets/spinbox/lv_spinbox_private.h new file mode 100644 index 00000000..333bd506 --- /dev/null +++ b/include/liblvgl/widgets/spinbox/lv_spinbox_private.h @@ -0,0 +1,59 @@ +/** + * @file lv_spinbox_private.h + * + */ + +#ifndef LV_SPINBOX_PRIVATE_H +#define LV_SPINBOX_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../textarea/lv_textarea_private.h" +#include "lv_spinbox.h" + +#if LV_USE_SPINBOX + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** Data of spinbox */ +struct _lv_spinbox_t { + lv_textarea_t ta; /**< Ext. of ancestor */ + /*New data for this type*/ + int32_t value; + int32_t range_max; + int32_t range_min; + int32_t step; + uint32_t digit_count : 4; + uint32_t dec_point_pos : 4; /**< if 0, there is no separator and the number is an integer */ + uint32_t rollover : 1; /**< Set to true for rollover functionality */ + uint32_t digit_step_dir : 2; /**< the direction the digit will step on encoder button press when editing */ +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_SPINBOX */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_SPINBOX_PRIVATE_H*/ diff --git a/include/liblvgl/extra/widgets/spinner/lv_spinner.h b/include/liblvgl/widgets/spinner/lv_spinner.h similarity index 53% rename from include/liblvgl/extra/widgets/spinner/lv_spinner.h rename to include/liblvgl/widgets/spinner/lv_spinner.h index 794f7363..92445631 100644 --- a/include/liblvgl/extra/widgets/spinner/lv_spinner.h +++ b/include/liblvgl/widgets/spinner/lv_spinner.h @@ -13,7 +13,7 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lvgl.h" +#include "../../lv_conf_internal.h" #if LV_USE_SPINNER @@ -29,13 +29,26 @@ extern "C" { /********************** * TYPEDEFS **********************/ -extern const lv_obj_class_t lv_spinner_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_spinner_class; /********************** * GLOBAL PROTOTYPES **********************/ -lv_obj_t * lv_spinner_create(lv_obj_t * parent, uint32_t time, uint32_t arc_length); +/** + * Create a spinner widget + * @param parent pointer to an object, it will be the parent of the new spinner. + * @return the created spinner + */ +lv_obj_t * lv_spinner_create(lv_obj_t * parent); + +/** + * Set the animation time and arc length of the spinner + * @param obj pointer to a spinner + * @param t the animation time in milliseconds + * @param angle the angle of the arc in degrees + */ +void lv_spinner_set_anim_params(lv_obj_t * obj, uint32_t t, uint32_t angle); /********************** * MACROS diff --git a/include/liblvgl/widgets/switch/lv_switch.h b/include/liblvgl/widgets/switch/lv_switch.h new file mode 100644 index 00000000..3cc0d284 --- /dev/null +++ b/include/liblvgl/widgets/switch/lv_switch.h @@ -0,0 +1,84 @@ +/** + * @file lv_switch.h + * + */ + +#ifndef LV_SWITCH_H +#define LV_SWITCH_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../lv_conf_internal.h" + +#if LV_USE_SWITCH != 0 + +#include "../../core/lv_obj.h" + +/********************* + * DEFINES + *********************/ + +/** Switch knob extra area correction factor */ +#define LV_SWITCH_KNOB_EXT_AREA_CORRECTION 2 + +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_switch_class; + +/********************** + * TYPEDEFS + **********************/ + +typedef enum { + LV_SWITCH_ORIENTATION_AUTO, + LV_SWITCH_ORIENTATION_HORIZONTAL, + LV_SWITCH_ORIENTATION_VERTICAL +} lv_switch_orientation_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create a switch object + * @param parent pointer to an object, it will be the parent of the new switch + * @return pointer to the created switch + */ +lv_obj_t * lv_switch_create(lv_obj_t * parent); + +/*===================== + * Setter functions + *====================*/ + +/** + * Set the orientation of switch. + * @param obj pointer to switch object + * @param orientation switch orientation from `lv_switch_orientation_t` + */ +void lv_switch_set_orientation(lv_obj_t * obj, lv_switch_orientation_t orientation); + +/*===================== + * Getter functions + *====================*/ + +/** + * Get the orientation of switch. + * @param obj pointer to switch object + * @return switch orientation from ::lv_switch_orientation_t + */ +lv_switch_orientation_t lv_switch_get_orientation(lv_obj_t * obj); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_SWITCH*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_SWITCH_H*/ diff --git a/include/liblvgl/widgets/switch/lv_switch_private.h b/include/liblvgl/widgets/switch/lv_switch_private.h new file mode 100644 index 00000000..66e90d32 --- /dev/null +++ b/include/liblvgl/widgets/switch/lv_switch_private.h @@ -0,0 +1,55 @@ +/** + * @file lv_switch_private.h + * + */ + +#ifndef LV_SWITCH_PRIVATE_H +#define LV_SWITCH_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_switch.h" + +#if LV_USE_SWITCH != 0 +#include "../../core/lv_obj_private.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_switch_t { + lv_obj_t obj; + int32_t anim_state; + lv_switch_orientation_t orientation : 3; /**< Orientation of switch*/ +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_SWITCH != 0 */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_SWITCH_PRIVATE_H*/ diff --git a/include/liblvgl/widgets/lv_table.h b/include/liblvgl/widgets/table/lv_table.h similarity index 67% rename from include/liblvgl/widgets/lv_table.h rename to include/liblvgl/widgets/table/lv_table.h index 311d9149..0d615a1b 100644 --- a/include/liblvgl/widgets/lv_table.h +++ b/include/liblvgl/widgets/table/lv_table.h @@ -13,7 +13,8 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" + +#include "../label/lv_label.h" #if LV_USE_TABLE != 0 @@ -22,9 +23,6 @@ extern "C" { #error "lv_table: lv_label is required. Enable it in lv_conf.h (LV_USE_LABEL 1)" #endif -#include "liblvgl/core/lv_obj.h" -#include "lv_label.h" - /********************* * DEFINES *********************/ @@ -35,38 +33,16 @@ LV_EXPORT_CONST_INT(LV_TABLE_CELL_NONE); * TYPEDEFS **********************/ -enum { +typedef enum { LV_TABLE_CELL_CTRL_MERGE_RIGHT = 1 << 0, LV_TABLE_CELL_CTRL_TEXT_CROP = 1 << 1, LV_TABLE_CELL_CTRL_CUSTOM_1 = 1 << 4, LV_TABLE_CELL_CTRL_CUSTOM_2 = 1 << 5, LV_TABLE_CELL_CTRL_CUSTOM_3 = 1 << 6, LV_TABLE_CELL_CTRL_CUSTOM_4 = 1 << 7, -}; - -typedef uint8_t lv_table_cell_ctrl_t; +} lv_table_cell_ctrl_t; -/*Data of table*/ -typedef struct { - lv_obj_t obj; - uint16_t col_cnt; - uint16_t row_cnt; - char ** cell_data; - lv_coord_t * row_h; - lv_coord_t * col_w; - uint16_t col_act; - uint16_t row_act; -} lv_table_t; - -extern const lv_obj_class_t lv_table_class; - -/** - * `type` field in `lv_obj_draw_part_dsc_t` if `class_p = lv_table_class` - * Used in `LV_EVENT_DRAW_PART_BEGIN` and `LV_EVENT_DRAW_PART_END` - */ -typedef enum { - LV_TABLE_DRAW_PART_CELL, /**< A cell*/ -} lv_table_draw_part_type_t; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_table_class; /********************** * GLOBAL PROTOTYPES @@ -91,7 +67,7 @@ lv_obj_t * lv_table_create(lv_obj_t * parent); * @param txt text to display in the cell. It will be copied and saved so this variable is not required after this function call. * @note New roes/columns are added automatically if required */ -void lv_table_set_cell_value(lv_obj_t * obj, uint16_t row, uint16_t col, const char * txt); +void lv_table_set_cell_value(lv_obj_t * obj, uint32_t row, uint32_t col, const char * txt); /** * Set the value of a cell. Memory will be allocated to store the text by the table. @@ -101,21 +77,22 @@ void lv_table_set_cell_value(lv_obj_t * obj, uint16_t row, uint16_t col, const c * @param fmt `printf`-like format * @note New roes/columns are added automatically if required */ -void lv_table_set_cell_value_fmt(lv_obj_t * obj, uint16_t row, uint16_t col, const char * fmt, ...); +void lv_table_set_cell_value_fmt(lv_obj_t * obj, uint32_t row, uint32_t col, const char * fmt, + ...) LV_FORMAT_ATTRIBUTE(4, 5); /** * Set the number of rows * @param obj table pointer to a Table object * @param row_cnt number of rows */ -void lv_table_set_row_cnt(lv_obj_t * obj, uint16_t row_cnt); +void lv_table_set_row_count(lv_obj_t * obj, uint32_t row_cnt); /** * Set the number of columns * @param obj table pointer to a Table object * @param col_cnt number of columns. */ -void lv_table_set_col_cnt(lv_obj_t * obj, uint16_t col_cnt); +void lv_table_set_column_count(lv_obj_t * obj, uint32_t col_cnt); /** * Set the width of a column @@ -123,7 +100,7 @@ void lv_table_set_col_cnt(lv_obj_t * obj, uint16_t col_cnt); * @param col_id id of the column [0 .. LV_TABLE_COL_MAX -1] * @param w width of the column */ -void lv_table_set_col_width(lv_obj_t * obj, uint16_t col_id, lv_coord_t w); +void lv_table_set_column_width(lv_obj_t * obj, uint32_t col_id, int32_t w); /** * Add control bits to the cell. @@ -132,8 +109,7 @@ void lv_table_set_col_width(lv_obj_t * obj, uint16_t col_id, lv_coord_t w); * @param col id of the column [0 .. col_cnt -1] * @param ctrl OR-ed values from ::lv_table_cell_ctrl_t */ -void lv_table_add_cell_ctrl(lv_obj_t * obj, uint16_t row, uint16_t col, lv_table_cell_ctrl_t ctrl); - +void lv_table_add_cell_ctrl(lv_obj_t * obj, uint32_t row, uint32_t col, lv_table_cell_ctrl_t ctrl); /** * Clear control bits of the cell. @@ -142,7 +118,27 @@ void lv_table_add_cell_ctrl(lv_obj_t * obj, uint16_t row, uint16_t col, lv_table * @param col id of the column [0 .. col_cnt -1] * @param ctrl OR-ed values from ::lv_table_cell_ctrl_t */ -void lv_table_clear_cell_ctrl(lv_obj_t * obj, uint16_t row, uint16_t col, lv_table_cell_ctrl_t ctrl); +void lv_table_clear_cell_ctrl(lv_obj_t * obj, uint32_t row, uint32_t col, lv_table_cell_ctrl_t ctrl); + +/** + * Add custom user data to the cell. + * @param obj pointer to a Table object + * @param row id of the row [0 .. row_cnt -1] + * @param col id of the column [0 .. col_cnt -1] + * @param user_data pointer to the new user_data. + * Should be allocated by `lv_malloc`, + * and it will be freed automatically when the table is deleted or + * when the cell is dropped due to lower row or column count. + */ +void lv_table_set_cell_user_data(lv_obj_t * obj, uint16_t row, uint16_t col, void * user_data); + +/** + * Set the selected cell + * @param obj pointer to a table object + * @param row id of the cell row to select + * @param col id of the cell column to select + */ +void lv_table_set_selected_cell(lv_obj_t * obj, uint16_t row, uint16_t col); /*===================== * Getter functions @@ -155,21 +151,21 @@ void lv_table_clear_cell_ctrl(lv_obj_t * obj, uint16_t row, uint16_t col, lv_tab * @param col id of the column [0 .. col_cnt -1] * @return text in the cell */ -const char * lv_table_get_cell_value(lv_obj_t * obj, uint16_t row, uint16_t col); +const char * lv_table_get_cell_value(lv_obj_t * obj, uint32_t row, uint32_t col); /** * Get the number of rows. * @param obj table pointer to a Table object * @return number of rows. */ -uint16_t lv_table_get_row_cnt(lv_obj_t * obj); +uint32_t lv_table_get_row_count(lv_obj_t * obj); /** * Get the number of columns. * @param obj table pointer to a Table object * @return number of columns. */ -uint16_t lv_table_get_col_cnt(lv_obj_t * obj); +uint32_t lv_table_get_column_count(lv_obj_t * obj); /** * Get the width of a column @@ -177,7 +173,7 @@ uint16_t lv_table_get_col_cnt(lv_obj_t * obj); * @param col id of the column [0 .. LV_TABLE_COL_MAX -1] * @return width of the column */ -lv_coord_t lv_table_get_col_width(lv_obj_t * obj, uint16_t col); +int32_t lv_table_get_column_width(lv_obj_t * obj, uint32_t col); /** * Get whether a cell has the control bits @@ -187,7 +183,7 @@ lv_coord_t lv_table_get_col_width(lv_obj_t * obj, uint16_t col); * @param ctrl OR-ed values from ::lv_table_cell_ctrl_t * @return true: all control bits are set; false: not all control bits are set */ -bool lv_table_has_cell_ctrl(lv_obj_t * obj, uint16_t row, uint16_t col, lv_table_cell_ctrl_t ctrl); +bool lv_table_has_cell_ctrl(lv_obj_t * obj, uint32_t row, uint32_t col, lv_table_cell_ctrl_t ctrl); /** * Get the selected cell (pressed and or focused) @@ -195,7 +191,15 @@ bool lv_table_has_cell_ctrl(lv_obj_t * obj, uint16_t row, uint16_t col, lv_table * @param row pointer to variable to store the selected row (LV_TABLE_CELL_NONE: if no cell selected) * @param col pointer to variable to store the selected column (LV_TABLE_CELL_NONE: if no cell selected) */ -void lv_table_get_selected_cell(lv_obj_t * obj, uint16_t * row, uint16_t * col); +void lv_table_get_selected_cell(lv_obj_t * obj, uint32_t * row, uint32_t * col); + +/** + * Get custom user data to the cell. + * @param obj pointer to a Table object + * @param row id of the row [0 .. row_cnt -1] + * @param col id of the column [0 .. col_cnt -1] + */ +void * lv_table_get_cell_user_data(lv_obj_t * obj, uint16_t row, uint16_t col); /********************** * MACROS diff --git a/include/liblvgl/widgets/table/lv_table_private.h b/include/liblvgl/widgets/table/lv_table_private.h new file mode 100644 index 00000000..816de1e7 --- /dev/null +++ b/include/liblvgl/widgets/table/lv_table_private.h @@ -0,0 +1,64 @@ +/** + * @file lv_table_private.h + * + */ + +#ifndef LV_TABLE_PRIVATE_H +#define LV_TABLE_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "lv_table.h" + +#if LV_USE_TABLE != 0 +#include "../../core/lv_obj_private.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** Cell data */ +struct _lv_table_cell_t { + lv_table_cell_ctrl_t ctrl; + void * user_data; /**< Custom user data */ + char txt[1]; /**< Variable length array */ +}; + +/** Table data */ +struct _lv_table_t { + lv_obj_t obj; + uint32_t col_cnt; + uint32_t row_cnt; + lv_table_cell_t ** cell_data; + int32_t * row_h; + int32_t * col_w; + uint32_t col_act; + uint32_t row_act; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_TABLE != 0 */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_TABLE_PRIVATE_H*/ diff --git a/include/liblvgl/widgets/tabview/lv_tabview.h b/include/liblvgl/widgets/tabview/lv_tabview.h new file mode 100644 index 00000000..f9bfdcc1 --- /dev/null +++ b/include/liblvgl/widgets/tabview/lv_tabview.h @@ -0,0 +1,115 @@ +/** + * @file lv_tabview.h + * + */ + +#ifndef LV_TABVIEW_H +#define LV_TABVIEW_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../lv_conf_internal.h" +#include "../../core/lv_obj.h" + +#if LV_USE_TABVIEW + +/********************* + * DEFINES + *********************/ + +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_tabview_class; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create a tabview widget + * @param parent pointer to a parent widget + * @return the created tabview + */ +lv_obj_t * lv_tabview_create(lv_obj_t * parent); + +/** + * Add a tab to the tabview + * @param obj pointer to a tabview widget + * @param name the name of the tab, it will be displayed on the tab bar + * @return the widget where the content of the tab can be created + */ +lv_obj_t * lv_tabview_add_tab(lv_obj_t * obj, const char * name); + +/** + * Change the name of the tab + * @param obj pointer to a tabview widget + * @param idx the index of the tab to rename + * @param new_name the new name as a string + */ +void lv_tabview_rename_tab(lv_obj_t * obj, uint32_t idx, const char * new_name); + +/** + * Show a tab + * @param obj pointer to a tabview widget + * @param idx the index of the tab to show + * @param anim_en LV_ANIM_ON/OFF + */ +void lv_tabview_set_active(lv_obj_t * obj, uint32_t idx, lv_anim_enable_t anim_en); + +/** + * Set the position of the tab bar + * @param obj pointer to a tabview widget + * @param dir LV_DIR_TOP/BOTTOM/LEFT/RIGHT + */ +void lv_tabview_set_tab_bar_position(lv_obj_t * obj, lv_dir_t dir); + +/** + * Set the width or height of the tab bar + * @param obj pointer to tabview widget + * @param size size of the tab bar in pixels or percentage. + * will be used as width or height based on the position of the tab bar) + */ +void lv_tabview_set_tab_bar_size(lv_obj_t * obj, int32_t size); + +/** + * Get the number of tabs + * @param obj pointer to a tabview widget + * @return the number of tabs + */ +uint32_t lv_tabview_get_tab_count(lv_obj_t * obj); + +/** + * Get the current tab's index + * @param obj pointer to a tabview widget + * @return the zero based index of the current tab + */ +uint32_t lv_tabview_get_tab_active(lv_obj_t * obj); + +/** + * Get the widget where the container of each tab is created + * @param obj pointer to a tabview widget + * @return the main container widget + */ +lv_obj_t * lv_tabview_get_content(lv_obj_t * obj); + +/** + * Get the tab bar where the buttons are created + * @param obj pointer to a tabview widget + * @return the tab bar + */ +lv_obj_t * lv_tabview_get_tab_bar(lv_obj_t * obj); + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_TABVIEW*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_TABVIEW_H*/ diff --git a/include/liblvgl/widgets/tabview/lv_tabview_private.h b/include/liblvgl/widgets/tabview/lv_tabview_private.h new file mode 100644 index 00000000..0e00351c --- /dev/null +++ b/include/liblvgl/widgets/tabview/lv_tabview_private.h @@ -0,0 +1,55 @@ +/** + * @file lv_tabview_private.h + * + */ + +#ifndef LV_TABVIEW_PRIVATE_H +#define LV_TABVIEW_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../core/lv_obj_private.h" +#include "lv_tabview.h" + +#if LV_USE_TABVIEW + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * TYPEDEFS + **********************/ + +struct _lv_tabview_t { + lv_obj_t obj; + uint32_t tab_cur; + lv_dir_t tab_pos; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_TABVIEW */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_TABVIEW_PRIVATE_H*/ diff --git a/include/liblvgl/widgets/lv_textarea.h b/include/liblvgl/widgets/textarea/lv_textarea.h similarity index 79% rename from include/liblvgl/widgets/lv_textarea.h rename to include/liblvgl/widgets/textarea/lv_textarea.h index cfa47e47..0804029d 100644 --- a/include/liblvgl/widgets/lv_textarea.h +++ b/include/liblvgl/widgets/textarea/lv_textarea.h @@ -13,18 +13,15 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "liblvgl/lv_conf_internal.h" +#include "../label/lv_label.h" #if LV_USE_TEXTAREA != 0 /*Testing of dependencies*/ #if LV_USE_LABEL == 0 -#error "lv_ta: lv_label is required. Enable it in lv_conf.h (LV_USE_LABEL 1)" +#error "lv_textarea: lv_label is required. Enable it in lv_conf.h (LV_USE_LABEL 1)" #endif -#include "liblvgl/core/lv_obj.h" -#include "lv_label.h" - /********************* * DEFINES *********************/ @@ -36,37 +33,28 @@ LV_EXPORT_CONST_INT(LV_TEXTAREA_CURSOR_LAST); * TYPEDEFS **********************/ -/*Data of text area*/ -typedef struct { - lv_obj_t obj; - lv_obj_t * label; /*Label of the text area*/ - char * placeholder_txt; /*Place holder label. only visible if text is an empty string*/ - char * pwd_tmp; /*Used to store the original text in password mode*/ - char * pwd_bullet; /*Replacement characters displayed in password mode*/ - const char * accepted_chars; /*Only these characters will be accepted. NULL: accept all*/ - uint32_t max_length; /*The max. number of characters. 0: no limit*/ - uint16_t pwd_show_time; /*Time to show characters in password mode before change them to '*'*/ - struct { - lv_coord_t valid_x; /*Used when stepping up/down to a shorter line. - *(Used by the library)*/ - uint32_t pos; /*The current cursor position - *(0: before 1st letter; 1: before 2nd letter ...)*/ - lv_area_t area; /*Cursor area relative to the Text Area*/ - uint32_t txt_byte_pos; /*Byte index of the letter after (on) the cursor*/ - uint8_t show : 1; /*Cursor is visible now or not (Handled by the library)*/ - uint8_t click_pos : 1; /*1: Enable positioning the cursor by clicking the text area*/ - } cursor; -#if LV_LABEL_TEXT_SELECTION - uint32_t sel_start; /*Temporary values for text selection*/ - uint32_t sel_end; - uint8_t text_sel_in_prog : 1; /*User is in process of selecting*/ - uint8_t text_sel_en : 1; /*Text can be selected on this text area*/ +#if LV_USE_OBJ_PROPERTY +enum { + LV_PROPERTY_ID(TEXTAREA, TEXT, LV_PROPERTY_TYPE_TEXT, 0), + LV_PROPERTY_ID(TEXTAREA, PLACEHOLDER_TEXT, LV_PROPERTY_TYPE_TEXT, 1), + LV_PROPERTY_ID(TEXTAREA, CURSOR_POS, LV_PROPERTY_TYPE_INT, 2), + LV_PROPERTY_ID(TEXTAREA, CURSOR_CLICK_POS, LV_PROPERTY_TYPE_INT, 3), + LV_PROPERTY_ID(TEXTAREA, PASSWORD_MODE, LV_PROPERTY_TYPE_INT, 4), + LV_PROPERTY_ID(TEXTAREA, PASSWORD_BULLET, LV_PROPERTY_TYPE_TEXT, 5), + LV_PROPERTY_ID(TEXTAREA, ONE_LINE, LV_PROPERTY_TYPE_BOOL, 6), + LV_PROPERTY_ID(TEXTAREA, ACCEPTED_CHARS, LV_PROPERTY_TYPE_TEXT, 7), + LV_PROPERTY_ID(TEXTAREA, MAX_LENGTH, LV_PROPERTY_TYPE_INT, 8), + LV_PROPERTY_ID(TEXTAREA, INSERT_REPLACE, LV_PROPERTY_TYPE_TEXT, 9), + LV_PROPERTY_ID(TEXTAREA, TEXT_SELECTION, LV_PROPERTY_TYPE_BOOL, 10), + LV_PROPERTY_ID(TEXTAREA, PASSWORD_SHOW_TIME, LV_PROPERTY_TYPE_INT, 11), + LV_PROPERTY_ID(TEXTAREA, LABEL, LV_PROPERTY_TYPE_OBJ, 12), + LV_PROPERTY_ID(TEXTAREA, TEXT_IS_SELECTED, LV_PROPERTY_TYPE_INT, 13), + LV_PROPERTY_ID(TEXTAREA, CURRENT_CHAR, LV_PROPERTY_TYPE_INT, 14), + LV_PROPERTY_TEXTAREA_END, +}; #endif - uint8_t pwd_mode : 1; /*Replace characters with '*'*/ - uint8_t one_line : 1; /*One line mode (ignore line breaks)*/ -} lv_textarea_t; -extern const lv_obj_class_t lv_textarea_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_textarea_class; enum { LV_PART_TEXTAREA_PLACEHOLDER = LV_PART_CUSTOM_FIRST, @@ -89,7 +77,7 @@ lv_obj_t * lv_textarea_create(lv_obj_t * parent); /** * Insert a character to the current cursor position. - * To add a wide char, e.g. 'Á' use `_lv_txt_encoded_conv_wc('Á')` + * To add a wide char, e.g. 'Á' use `lv_text_encoded_conv_wc('Á')` * @param obj pointer to a text area object * @param c a character (e.g. 'a') */ @@ -106,13 +94,13 @@ void lv_textarea_add_text(lv_obj_t * obj, const char * txt); * Delete a the left character from the current cursor position * @param obj pointer to a text area object */ -void lv_textarea_del_char(lv_obj_t * obj); +void lv_textarea_delete_char(lv_obj_t * obj); /** * Delete the right character from the current cursor position * @param obj pointer to a text area object */ -void lv_textarea_del_char_forward(lv_obj_t * obj); +void lv_textarea_delete_char_forward(lv_obj_t * obj); /*===================== * Setter functions @@ -184,7 +172,7 @@ void lv_textarea_set_accepted_chars(lv_obj_t * obj, const char * list); void lv_textarea_set_max_length(lv_obj_t * obj, uint32_t num); /** - * In `LV_EVENT_INSERT` the text which planned to be inserted can be replaced by an other text. + * In `LV_EVENT_INSERT` the text which planned to be inserted can be replaced by another text. * It can be used to add automatic formatting to the text area. * @param obj pointer to a text area object * @param txt pointer to a new string to insert. If `""` no text will be added. @@ -204,10 +192,10 @@ void lv_textarea_set_text_selection(lv_obj_t * obj, bool en); * @param obj pointer to a text area object * @param time show time in milliseconds. 0: hide immediately. */ -void lv_textarea_set_password_show_time(lv_obj_t * obj, uint16_t time); +void lv_textarea_set_password_show_time(lv_obj_t * obj, uint32_t time); /** - * Deprecated: use the normal text_align style property instead + * @deprecated Use the normal text_align style property instead * Set the label's alignment. * It sets where the label is aligned (in one line mode it can be smaller than the text area) * and how the lines of the area align in case of multiline text area @@ -309,7 +297,14 @@ bool lv_textarea_get_text_selection(lv_obj_t * obj); * @param obj pointer to a text area object * @return show time in milliseconds. 0: hide immediately. */ -uint16_t lv_textarea_get_password_show_time(lv_obj_t * obj); +uint32_t lv_textarea_get_password_show_time(lv_obj_t * obj); + +/** + * Get a the character from the current cursor position + * @param obj pointer to a text area object + * @return a the character or 0 + */ +uint32_t lv_textarea_get_current_char(lv_obj_t * obj); /*===================== * Other functions diff --git a/include/liblvgl/widgets/textarea/lv_textarea_private.h b/include/liblvgl/widgets/textarea/lv_textarea_private.h new file mode 100644 index 00000000..68102e8c --- /dev/null +++ b/include/liblvgl/widgets/textarea/lv_textarea_private.h @@ -0,0 +1,75 @@ +/** + * @file lv_textarea_private.h + * + */ + +#ifndef LV_TEXTAREA_PRIVATE_H +#define LV_TEXTAREA_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../core/lv_obj_private.h" +#include "lv_textarea.h" + +#if LV_USE_TEXTAREA != 0 + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/** Data of text area */ +struct _lv_textarea_t { + lv_obj_t obj; + lv_obj_t * label; /**< Label of the text area */ + char * placeholder_txt; /**< Place holder label. only visible if text is an empty string */ + char * pwd_tmp; /**< Used to store the original text in password mode */ + char * pwd_bullet; /**< Replacement characters displayed in password mode */ + const char * accepted_chars; /**< Only these characters will be accepted. NULL: accept all */ + uint32_t max_length; /**< The max. number of characters. 0: no limit */ + uint32_t pwd_show_time; /**< Time to show characters in password mode before change them to '*' */ + struct { + int32_t valid_x; /**< Used when stepping up/down to a shorter line. + *(Used by the library) */ + uint32_t pos; /**< The current cursor position + *(0: before 1st letter; 1: before 2nd letter ...) */ + lv_area_t area; /**< Cursor area relative to the Text Area */ + uint32_t txt_byte_pos; /**< Byte index of the letter after (on) the cursor */ + uint8_t show : 1; /**< Cursor is visible now or not (Handled by the library) */ + uint8_t click_pos : 1; /**< 1: Enable positioning the cursor by clicking the text area */ + } cursor; +#if LV_LABEL_TEXT_SELECTION + uint32_t sel_start; /**< Temporary values for text selection */ + uint32_t sel_end; + uint8_t text_sel_in_prog : 1; /**< User is in process of selecting */ + uint8_t text_sel_en : 1; /**< Text can be selected on this text area */ +#endif + uint8_t pwd_mode : 1; /**< Replace characters with '*' */ + uint8_t one_line : 1; /**< One line mode (ignore line breaks) */ +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_TEXTAREA != 0 */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_TEXTAREA_PRIVATE_H*/ diff --git a/include/liblvgl/widgets/tileview/lv_tileview.h b/include/liblvgl/widgets/tileview/lv_tileview.h new file mode 100644 index 00000000..4058bb65 --- /dev/null +++ b/include/liblvgl/widgets/tileview/lv_tileview.h @@ -0,0 +1,86 @@ +/** + * @file lv_tileview.h + * + */ + +#ifndef LV_TILEVIEW_H +#define LV_TILEVIEW_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../core/lv_obj.h" + +#if LV_USE_TILEVIEW + +/********************* + * DEFINES + *********************/ + +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_tileview_class; +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_tileview_tile_class; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create a tileview object + * @param parent pointer to an object, it will be the parent of the new tileview + * @return pointer to the created tileview + */ +lv_obj_t * lv_tileview_create(lv_obj_t * parent); + +/** + * Add a tile to the tileview + * @param tv pointer to the tileview object + * @param col_id column id of the tile + * @param row_id row id of the tile + * @param dir direction to move to the next tile + * @return pointer to the added tile object + */ +lv_obj_t * lv_tileview_add_tile(lv_obj_t * tv, uint8_t col_id, uint8_t row_id, lv_dir_t dir); + +/** + * Set the active tile in the tileview. + * @param parent pointer to the tileview object + * @param tile_obj pointer to the tile object to be set as active + * @param anim_en animation enable flag (LV_ANIM_ON or LV_ANIM_OFF) + */ +void lv_tileview_set_tile(lv_obj_t * tv, lv_obj_t * tile_obj, lv_anim_enable_t anim_en); + +/** + * Set the active tile by index in the tileview + * @param tv pointer to the tileview object + * @param col_id column id of the tile to be set as active + * @param row_id row id of the tile to be set as active + * @param anim_en animation enable flag (LV_ANIM_ON or LV_ANIM_OFF) + */ +void lv_tileview_set_tile_by_index(lv_obj_t * tv, uint32_t col_id, uint32_t row_id, lv_anim_enable_t anim_en); + +/** + * Get the currently active tile in the tileview + * @param obj pointer to the tileview object + * @return pointer to the currently active tile object + */ +lv_obj_t * lv_tileview_get_tile_active(lv_obj_t * obj); + +/*===================== + * Other functions + *====================*/ + +/********************** + * MACROS + **********************/ + +#endif /*LV_USE_TILEVIEW*/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_TILEVIEW_H*/ diff --git a/include/liblvgl/widgets/tileview/lv_tileview_private.h b/include/liblvgl/widgets/tileview/lv_tileview_private.h new file mode 100644 index 00000000..e343db0c --- /dev/null +++ b/include/liblvgl/widgets/tileview/lv_tileview_private.h @@ -0,0 +1,58 @@ +/** + * @file lv_tileview_private.h + * + */ + +#ifndef LV_TILEVIEW_PRIVATE_H +#define LV_TILEVIEW_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ + +#include "../../core/lv_obj_private.h" +#include "lv_tileview.h" + +#if LV_USE_TILEVIEW + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * TYPEDEFS + **********************/ +struct _lv_tileview_t { + lv_obj_t obj; + lv_obj_t * tile_act; +}; + +struct _lv_tileview_tile_t { + lv_obj_t obj; + lv_dir_t dir; +}; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#endif /* LV_USE_TILEVIEW */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_TILEVIEW_PRIVATE_H*/ diff --git a/include/liblvgl/widgets/win/lv_win.h b/include/liblvgl/widgets/win/lv_win.h new file mode 100644 index 00000000..366a8b1b --- /dev/null +++ b/include/liblvgl/widgets/win/lv_win.h @@ -0,0 +1,74 @@ +/** + * @file lv_win.h + * + */ + +#ifndef LV_WIN_H +#define LV_WIN_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../lv_conf_internal.h" +#include "../../core/lv_obj.h" +#if LV_USE_WIN +/********************* + * DEFINES + *********************/ + +LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_win_class; + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create a window widget + * @param parent pointer to a parent widget + * @return the created window + */ +lv_obj_t * lv_win_create(lv_obj_t * parent); + +/** + * Add a title to the window + * @param obj pointer to a window widget + * @param txt the text of the title + * @return the widget where the content of the title can be created + */ +lv_obj_t * lv_win_add_title(lv_obj_t * win, const char * txt); + +/** + * Add a button to the window + * @param obj pointer to a window widget + * @param icon an icon to be displayed on the button + * @param btn_w width of the button + * @return the widget where the content of the button can be created + */ +lv_obj_t * lv_win_add_button(lv_obj_t * win, const void * icon, int32_t btn_w); + +/** + * Get the header of the window + * @param win pointer to a window widget + * @return the header of the window + */ +lv_obj_t * lv_win_get_header(lv_obj_t * win); + +/** + * Get the content of the window + * @param win pointer to a window widget + * @return the content of the window + */ +lv_obj_t * lv_win_get_content(lv_obj_t * win); +/********************** + * MACROS + **********************/ +#endif /*LV_USE_WIN*/ +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_WIN_H*/ diff --git a/include/liblvgl/extra/others/lv_others.h b/include/liblvgl/widgets/win/lv_win_private.h similarity index 57% rename from include/liblvgl/extra/others/lv_others.h rename to include/liblvgl/widgets/win/lv_win_private.h index 106d85e4..25e40c59 100644 --- a/include/liblvgl/extra/others/lv_others.h +++ b/include/liblvgl/widgets/win/lv_win_private.h @@ -1,10 +1,10 @@ /** - * @file lv_others.h + * @file lv_win_private.h * */ -#ifndef LV_OTHERS_H -#define LV_OTHERS_H +#ifndef LV_WIN_PRIVATE_H +#define LV_WIN_PRIVATE_H #ifdef __cplusplus extern "C" { @@ -13,13 +13,11 @@ extern "C" { /********************* * INCLUDES *********************/ -#include "snapshot/lv_snapshot.h" -#include "monkey/lv_monkey.h" -#include "gridnav/lv_gridnav.h" -#include "fragment/lv_fragment.h" -#include "imgfont/lv_imgfont.h" -#include "msg/lv_msg.h" -#include "ime/lv_ime_pinyin.h" + +#include "../../core/lv_obj_private.h" +#include "lv_win.h" + +#if LV_USE_WIN /********************* * DEFINES @@ -29,6 +27,14 @@ extern "C" { * TYPEDEFS **********************/ +/********************** + * TYPEDEFS + **********************/ +struct _lv_win_t { + lv_obj_t obj; +}; + + /********************** * GLOBAL PROTOTYPES **********************/ @@ -37,8 +43,10 @@ extern "C" { * MACROS **********************/ +#endif /* LV_USE_WIN */ + #ifdef __cplusplus } /*extern "C"*/ #endif -#endif /*LV_OTHERS_H*/ +#endif /*LV_WIN_PRIVATE_H*/ diff --git a/include/pros/abstract_motor.hpp b/include/pros/abstract_motor.hpp index d2a3e31f..fd8e8034 100644 --- a/include/pros/abstract_motor.hpp +++ b/include/pros/abstract_motor.hpp @@ -9,7 +9,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright Copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright Copyright (c) 2017-2024, Purdue University ACM SIGBots. * All rights reserved. * * This Source Code Form is subject to the terms of the Mozilla Public @@ -69,6 +69,16 @@ enum class MotorGears { invalid = INT32_MAX ///< Error return code }; +/** + * \enum MotorType + * Indicates the type of a motor + */ +enum class MotorType { + v5 = 0, ///< 11w motor + exp = 1, ///< 5.5w motor + invalid = INT32_MAX ///< Error return code +}; + // Provide Aliases for MotorGears using MotorGearset = MotorGears; @@ -957,6 +967,34 @@ class AbstractMotor { */ virtual std::vector is_reversed_all(void) const = 0; + /** + * Gets the type of the motor + * + * This function uses the following values of errno when an error state is + * reached: + * ENODEV - The port cannot be configured as a motor + * + * \param index Optional parameter. + * The index of the motor to get the target position of. + * By default index is 0, and will return an error for an out of bounds index + * + * \return One of MotorType according to the type of the motor, + * or pros::MotorType::invalid if the operation failed + */ + virtual MotorType get_type(const std::uint8_t index = 0) const = 0; + + /** + * Gets a vector of the type(s) of the motor(s). + * + * This function uses the following values of errno when an error state is + * reached: + * ENODEV - The port cannot be configured as a motor + * + * \return A vector of MotorType according to the type(s) of the motor(s), + * or pros::MotorType::invalid if the operation failed. + */ + virtual std::vector get_type_all(void) const = 0; + /** * Sets one of MotorBrake to the motor. Works with the C enum * and the C++ enum class. diff --git a/include/pros/adi.h b/include/pros/adi.h index f1bbe373..0b45e57d 100644 --- a/include/pros/adi.h +++ b/include/pros/adi.h @@ -7,7 +7,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this diff --git a/include/pros/adi.hpp b/include/pros/adi.hpp index 75429c98..f01615d7 100644 --- a/include/pros/adi.hpp +++ b/include/pros/adi.hpp @@ -7,12 +7,12 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * + * * \defgroup cpp-adi ADI (TriPort) C++ API * \note The external ADI API can be found [here.](@ref ext-adi) * \note Additional example code for this module can be found in its [Tutorial.](@ref adi) @@ -22,25 +22,24 @@ #define _PROS_ADI_HPP_ #include -#include +#include #include #include -#include +#include #include "pros/adi.h" -#define LEGACY_TYPEDEF(old_name, new_name) \ - using old_name [[deprecated("use " #new_name " instead")]] = new_name +#define LEGACY_TYPEDEF(old_name, new_name) using old_name [[deprecated("use " #new_name " instead")]] = new_name namespace pros { namespace adi { - + /** type definition for the pair of smart port and adi port for the basic adi devices */ using ext_adi_port_pair_t = std::pair; /** type definition for the triplet of smart port and two adi ports for the two wire adi devices*/ using ext_adi_port_tuple_t = std::tuple; - + /** * \ingroup cpp-adi */ @@ -63,12 +62,12 @@ class Port { * The ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure * \param type * The configuration type for the port - * + * * \b Example * \code * #define POTENTIOMETER_PORT 1 * #define POTENTIOMETER_TYPE pros::E_ADI_POT_EDR - * + * * void opcontrol() { * pros::ADIPotentiometer potentiometer (POTENTIOMETER_PORT, POTENTIOMETER_TYPE); * while (true) { @@ -94,14 +93,14 @@ class Port { * (from 1-8, 'a'-'h', 'A'-'H') to configure * \param type * The configuration type for the port - * + * * \b Example * \code * #define ANALOG_SENSOR_PORT 'a' * #define EXT_ADI_SMART_PORT 1 - * + * * void initialize() { - * pros::adi::Port sensor ({{ EXT_ADI_SMART_PORT , ANALOG_SENSOR_PORT }}, E_ADI_ANALOG_IN); + * pros::adi::Port sensor ({EXT_ADI_SMART_PORT, ANALOG_SENSOR_PORT}, E_ADI_ANALOG_IN); * // Displays the value of E_ADI_ANALOG_IN * std::cout << "Port Type: " << sensor.get_config(); * } @@ -113,7 +112,7 @@ class Port { * Gets the configuration for the given ADI port. * * \return The ADI configuration for the given port - * + * * \b Example * \code * #define ANALOG_SENSOR_PORT 1 @@ -130,11 +129,11 @@ class Port { * Gets the value for the given ADI port. * * \return The value stored for the given port - * + * * \b Example * \code * #define ANALOG_SENSOR_PORT 1 - * + * * void opcontrol() { * pros::adi::Port sensor (ANALOG_SENSOR_PORT, E_ADI_ANALOG_IN); * std::cout << "Port Value: " << sensor.get_value(); @@ -151,11 +150,11 @@ class Port { * * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. - * + * * \b Example * \code * #define ANALOG_SENSOR_PORT 1 - * + * * void initialize() { * pros::adi::Port sensor (ANALOG_SENSOR_PORT, E_ADI_DIGITAL_IN); * // Do things as a digital sensor @@ -177,11 +176,11 @@ class Port { * * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. - * + * * \b Example * \code * #define DIGITAL_SENSOR_PORT 1 - * + * * void initialize() { * pros::adi::Port sensor (DIGITAL_SENSOR_PORT, E_ADI_DIGITAL_OUT); * sensor.set_value(DIGITAL_SENSOR_PORT, HIGH); @@ -194,24 +193,24 @@ class Port { * Gets the port of the sensor. * * \return returns a tuple of integer ports. - * - * \note The parts of the tuple are {smart port, adi port, second adi port (when applicable)}. - * - * + * + * \note The parts of the tuple are {smart port, adi port, second adi port (when applicable)}. + * + * * \b Example * \code * #define DIGITAL_SENSOR_PORT 1 // 'A' - * + * * void initialize() { * pros::adi::AnalogIn sensor (DIGITAL_SENSOR_PORT); - * - * // Getting values from the tuple using std::get + * + * // Getting values from the tuple using std::get * int sensorSmartPort = std::get<0>(sensor.get_port()); // First value * int sensorAdiPort = std::get<1>(sensor.get_port()); // Second value - * + * * // Prints the first and second value from the port tuple (The Adi Port. The first value is the Smart Port) * printf("Sensor Smart Port: %d\n", sensorSmartPort); - * printf("Sensor Adi Port: %d\n", sensorAdiPort); + * printf("Sensor Adi Port: %d\n", sensorAdiPort); * } * \endcode */ @@ -242,11 +241,11 @@ class AnalogIn : protected Port { * * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. - * + * * \b Example * \code * #define ANALOG_SENSOR_PORT 1 - * + * * void opcontrol() { * pros::ADIAnalogIn sensor (ANALOG_SENSOR_PORT); * while (true) { @@ -270,14 +269,14 @@ class AnalogIn : protected Port { * * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. - * + * * \b Example * \code * #define EXT_ADI_SENSOR_PORT 1 * #define ADI_PORT 'a' - * + * * void opcontrol() { - * pros::ADIAnalogIn sensor ({{EXT_ADI_SMART_PORT, ADI_PORT}}); + * pros::ADIAnalogIn sensor ({EXT_ADI_SMART_PORT, ADI_PORT}); * while (true) { * // Use the sensor * } @@ -301,20 +300,20 @@ class AnalogIn : protected Port { * * Do not use this function when the sensor value might be unstable (gyro * rotation, accelerometer movement). - * + * * \note The ADI currently returns data at 10ms intervals, in contrast to the - * calibrate function’s 1ms sample rate. + * calibrate function’s 1ms sample rate. * * This function uses the following values of errno when an error state is * reached: * ENODEV - The port is not configured as an analog input * * \return The average sensor value computed by this function - * + * * \b Example * \code * #define ANALOG_SENSOR_PORT 1 - * + * * void initialize() { * pros::adi::AnalogIn sensor (ANALOG_SENSOR_PORT); * sensor.calibrate(ANALOG_SENSOR_PORT); @@ -339,11 +338,11 @@ class AnalogIn : protected Port { * * \return The difference of the sensor value from its calibrated default from * -4095 to 4095 - * + * * \b Example * \code * #define ANALOG_SENSOR_PORT 1 - * + * * void initialize() { * pros::adi::AnalogIn sensor (ANALOG_SENSOR_PORT); * sensor.calibrate(ANALOG_SENSOR_PORT); @@ -373,11 +372,11 @@ class AnalogIn : protected Port { * * \return The difference of the sensor value from its calibrated default from * -16384 to 16384 - * + * * \b Example * \code * #define ANALOG_SENSOR_PORT 1 - * + * * void initialize() { * pros::adi::AnalogIn sensor (ANALOG_SENSOR_PORT); * sensor.calibrate(ANALOG_SENSOR_PORT); @@ -390,22 +389,23 @@ class AnalogIn : protected Port { /** * Reads an analog input channel and returns the 12-bit value. - * - * The value returned is undefined if the analog pin has been switched to a different mode. The meaning of the returned value varies depending on the sensor attached. - * + * + * The value returned is undefined if the analog pin has been switched to a different mode. The meaning of the + * returned value varies depending on the sensor attached. + * * Inherited from ADIPort::get_value. - * + * * This function uses the following values of errno when an error state is reached: * EADDRINUSE - The port is not configured as an analog input (e.g. the port has been reconfigured) - * + * * \return The analog sensor value, where a value of 0 reflects an input * voltage of nearly 0 V and a value of 4095 reflects an input voltage of * nearly 5 V - * + * * \b Example * \code * #define ANALOG_SENSOR_PORT 1 - * + * * void initialize() { * pros::adi::AnalogIn sensor (ANALOG_SENSOR_PORT); * std::cout << "Sensor Reading:" << sensor.get_value(); @@ -415,11 +415,11 @@ class AnalogIn : protected Port { using Port::get_value; /** - * This is the overload for the << operator for printing to streams - * - * Prints in format(this below is all in one line with no new line): + * This is the overload for the << operator for printing to streams + * + * Prints in format(this below is all in one line with no new line): * AnalogIn [smart_port: analog_in._smart_port, adi_port: analog_in._adi_port, - * value calibrated: (12 bit calibrated value), + * value calibrated: (12 bit calibrated value), * value calibrated HR: (16 bit calibrated value), value: (12 bit value)] */ friend std::ostream& operator<<(std::ostream& os, pros::adi::AnalogIn& analog_in); @@ -450,11 +450,11 @@ class AnalogOut : private Port { * * \param adi_port * The ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure - * + * * \b Example * \code * #define ANALOG_SENSOR_PORT 1 - * + * * void opcontrol() { * pros::AnalogOut sensor (ANALOG_SENSOR_PORT); * // Use the sensor @@ -474,14 +474,14 @@ class AnalogOut : private Port { * \param port_pair * The pair of the smart port number (from 1-22) and the * ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure - * + * * \b Example * \code * #define EXT_ADI_SMART_PORT 1 * #define ADI_PORT 'a' - * + * * void opcontrol() { - * pros::AnalogOut sensor ({{EXT_ADI_SMART_PORT, ADI_PORT}}); + * pros::AnalogOut sensor ({EXT_ADI_SMART_PORT, ADI_PORT}); * // Use the sensor * } * \endcode @@ -492,7 +492,7 @@ class AnalogOut : private Port { * Sets the output for the Analog Output from 0 (0V) to 4095 (5V). * * Inherited from ADIPort::set_value. - * + * * This function uses the following values of errno when an error state is reached: * EACCES - Another resource is currently trying to access the ADI. * @@ -501,11 +501,11 @@ class AnalogOut : private Port { * * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. - * + * * \b Example * \code * #define ANALOG_SENSOR_PORT 1 - * + * * void opcontrol() { * pros::AnalogOut sensor (ANALOG_SENSOR_PORT); * sensor.set_value(4095); // Set the port to 5V @@ -513,13 +513,13 @@ class AnalogOut : private Port { * \endcode */ using Port::set_value; - + using Port::get_port; /** * This is the overload for the << operator for printing to streams - * - * Prints in format(this below is all in one line with no new line): + * + * Prints in format(this below is all in one line with no new line): * AnalogOut [smart_port: analog_out._smart_port, adi_port: analog_out._adi_port, * value: (value)] */ @@ -545,11 +545,11 @@ class DigitalOut : private Port { * The ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure * \param init_state * The initial state for the port - * + * * \b Example * \code * #define DIGITAL_SENSOR_PORT 1 - * + * * void opcontrol() { * bool state = LOW; * pros::adi::DigitalOut sensor (DIGITAL_SENSOR_PORT, state); @@ -576,15 +576,15 @@ class DigitalOut : private Port { * ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure * \param init_state * The initial state for the port - * + * * \b Example * \code * #define EXT_ADI_SMART_PORT 1 * #define ADI_PORT 'a' - * + * * void opcontrol() { * bool state = LOW; - * pros::adi::DigitalOut sensor ( {{ EXT_ADI_SMART_PORT , ADI_PORT }}); + * pros::adi::DigitalOut sensor ({EXT_ADI_SMART_PORT , ADI_PORT}); * while (true) { * state != state; * sensor.set_value(state); @@ -599,7 +599,7 @@ class DigitalOut : private Port { * Sets the digital value (1 or 0) of a pin. * * Inherited from ADIPort::set_value. - * + * * This function uses the following values of errno when an error state is * reached: * EADDRINUSE - The port is not configured as a digital output (e.g. the port has been reconfigured) @@ -608,11 +608,11 @@ class DigitalOut : private Port { * The value to set the ADI port to * * \return if the operation was successful or PROS_ERR if the operation failed, setting errno. - * + * * \b Example * \code * #define DIGITAL_SENSOR_PORT 1 - * + * * void opcontrol() { * bool state = LOW; * pros::adi::DigitalOut sensor (DIGITAL_SENSOR_PORT); @@ -634,7 +634,7 @@ class DigitalOut : private Port { * Prints in format(this below is all in one line with no new line): * DigitalOut [smart_port: digital_out._smart_port, adi_port: digital_out._adi_port, * value: (value)] - */ + */ friend std::ostream& operator<<(std::ostream& os, pros::adi::DigitalOut& digital_out); }; ///@} @@ -643,7 +643,7 @@ class DigitalIn : private Port { /** * \addtogroup cpp-adi * @{ - */ + */ public: /** * Configures an ADI port to act as a Digital Input. @@ -655,11 +655,11 @@ class DigitalIn : private Port { * * \param adi_port * The ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure - * + * * \b Example * \code * #define DIGITAL_SENSOR_PORT 1 - * + * * void opcontrol() { * pros::adi::DigitalIn sensor (ANALOG_SENSOR_PORT); * // Use the sensor @@ -679,14 +679,14 @@ class DigitalIn : private Port { * \param port_pair * The pair of the smart port number (from 1-22) and the * ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure - * + * * \b Example * \code * #define EXT_ADI_SMART_PORT 1 * #define ADI_PORT 'a' - * + * * void opcontrol() { - * pros::adi::DigitalIn sensor ({{EXT_ADI_SMART_PORT, ADI_PORT}}); + * pros::adi::DigitalIn sensor ({EXT_ADI_SMART_PORT, ADI_PORT}); * // Use the sensor * } * \endcode @@ -710,11 +710,11 @@ class DigitalIn : private Port { * * \return 1 if the button is pressed and had not been pressed the last time * this function was called, 0 otherwise. - * + * * \b Example * \code * #define DIGITAL_SENSOR_PORT 1 - * + * * void opcontrol() { * pros::adi::DigitalIn sensor (DIGITAL_SENSOR_PORT); * while (true) { @@ -730,21 +730,21 @@ class DigitalIn : private Port { /** * Gets the digital value (1 or 0) of a pin. - * + * * Inherited from ADIPort::get_value. - * + * * This function uses the following values of errno when an error state is reached: - * + * * EADDRINUSE - The port is not configured as a digital input (e.g. the port has been reconfigured) - * + * * Analogous to adi_digital_read. * * \return The value stored for the given port - * + * * \b Example * \code * #define DIGITAL_SENSOR_PORT 1 - * + * * void opcontrol() { * pros::adi::DigitalIn sensor (DIGITAL_SENSOR_PORT); * while (true) { @@ -770,14 +770,14 @@ class DigitalIn : private Port { ///@} -//Derived Class(es) from DigitalIn +// Derived Class(es) from DigitalIn using Button = DigitalIn; class Motor : private Port { /** * \addtogroup cpp-adi * @{ - */ + */ public: /** * Configures an ADI port to act as a Motor. @@ -789,11 +789,11 @@ class Motor : private Port { * * \param adi_port * The ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure - * + * * \b Example * \code * #define MOTOR_PORT 1 - * + * * void opcontrol() { * pros::adi::Motor motor (MOTOR_PORT); * motor.set_value(127); // Go full speed forward @@ -816,14 +816,14 @@ class Motor : private Port { * \param port_pair * The pair of the smart port number (from 1-22) and the * ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure - * + * * \b Example * \code * #define EXT_ADI_SMART_PORT 1 * #define ADI_MOTOR_PORT 'a' - * + * * void opcontrol() { - * pros::adi::Motor motor ( {{ EXT_ADI_SMART_PORT , ADI_MOTOR_PORT}} ); + * pros::adi::Motor motor ({EXT_ADI_SMART_PORT, ADI_MOTOR_PORT}); * motor.set_value(127); // Go full speed forward * std::cout << "Commanded Motor Power: " << motor.get_value(); // Will display 127 * delay(1000); @@ -842,11 +842,11 @@ class Motor : private Port { * * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. - * + * * \b Example * \code * #define MOTOR_PORT 1 - * + * * void opcontrol() { * pros::adi::Motor motor (MOTOR_PORT); * motor.set_value(127); // Go full speed forward @@ -871,11 +871,11 @@ class Motor : private Port { * * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. - * + * * \b Example * \code * #define MOTOR_PORT 1 - * + * * void opcontrol() { * pros::adi::Motor motor (MOTOR_PORT); * motor.set_value(127); // Go full speed forward @@ -894,13 +894,13 @@ class Motor : private Port { * reached: * ENODEV - The port is not configured as a motor * - * \return The last set speed of the motor on the given - * + * \return The last set speed of the motor on the given + * * \b Example * \code * #define MOTOR_PORT 1 - * - * void opcontrol() { + * + * void opcontrol() { * pros::adi::Motor motor (MOTOR_PORT); * motor.set_value(127); // Go full speed forward * std::cout << "Commanded Motor Power: " << motor.get_value(); // Will display 127 @@ -920,7 +920,7 @@ class Encoder : private Port { /** * \addtogroup cpp-adi * @{ - */ + */ public: /** * Configures a set of ADI ports to act as an Encoder. @@ -936,12 +936,12 @@ class Encoder : private Port { * The "bottom" wire from the encoder sensor * \param reverse * If "true", the sensor will count in the opposite direction - * + * * \b Example * \code * #define PORT_TOP 1 * #define PORT_BOTTOM 2 - * + * * void opcontrol() { * pros::adi::Encoder sensor (PORT_TOP, PORT_BOTTOM, false); * // Use the sensor @@ -964,13 +964,13 @@ class Encoder : private Port { * the encoder sensor * \param reverse * If "true", the sensor will count in theopposite direction - * + * * \b Example * \code * #define PORT_TOP 'A' * #define PORT_BOTTOM 'B' * #define SMART_PORT 1 - * + * * void opcontrol() { * pros::adi::Encoder sensor ({ SMART_PORT, PORT_TOP, PORT_BOTTOM }, false); * // Use the sensor @@ -991,12 +991,12 @@ class Encoder : private Port { * * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. - * + * * \b Example * \code * #define PORT_TOP 1 * #define PORT_BOTTOM 2 - * + * * void opcontrol() { * pros::adi::Encoder sensor (PORT_TOP, PORT_BOTTOM, false); * delay(1000); // Move the encoder around in this time @@ -1016,12 +1016,12 @@ class Encoder : private Port { * ENODEV - The port is not configured as a motor * * \return The signed and cumulative number of counts since the last start or - * + * * \b Example * \code * #define PORT_TOP 1 * #define PORT_BOTTOM 2 - * + * * void opcontrol() { * pros::adi::Encoder sensor (PORT_TOP, PORT_BOTTOM, false); * while (true) { @@ -1039,13 +1039,12 @@ class Encoder : private Port { * Prints in format(this below is all in one line with no new line): * Encoder [smart_port: encoder._smart_port, adi_port: encoder._adi_port, * value: (value)] - */ + */ friend std::ostream& operator<<(std::ostream& os, pros::adi::Encoder& encoder); ext_adi_port_tuple_t get_port() const override; private: ext_adi_port_pair_t _port_pair; - }; ///@} @@ -1054,7 +1053,7 @@ class Ultrasonic : private Port { /** * \addtogroup cpp-adi * @{ - */ + */ public: /** * Configures a set of ADI ports to act as an Ultrasonic sensor. @@ -1070,12 +1069,12 @@ class Ultrasonic : private Port { * \param port_echo * The port connected to the yellow INPUT cable. This should be in the * next highest port following port_ping. - * + * * \b Example * \code * #define PORT_PING 1 * #define PORT_ECHO 2 - * + * * void opcontrol() { * pros::adi::Ultrasonic sensor (PORT_PING, PORT_ECHO); * while (true) { @@ -1101,13 +1100,13 @@ class Ultrasonic : private Port { * OUTPUT cable (1, 3, 5, 7 or 'A', 'C', 'E', 'G'), and the port * connected to the yellow INPUT cable (the next) highest port * following port_ping). - * + * * \b Example * \code * #define PORT_PING 'A' * #define PORT_ECHO 'B' * #define SMART_PORT 1 - * + * * void opcontrol() { * pros::adi::Ultrasonic sensor ( {{ SMART_PORT, PORT_PING, PORT_ECHO }} ); * while (true) { @@ -1133,12 +1132,12 @@ class Ultrasonic : private Port { * * \return The distance to the nearest object in m^-4 (10000 indicates 1 * meter), measured from the sensor's mounting points. - * + * * \b Example * \code * #define PORT_PING 1 * #define PORT_ECHO 2 - * + * * void opcontrol() { * pros::adi::Ultrasonic sensor (PORT_PING, PORT_ECHO); * while (true) { @@ -1160,7 +1159,7 @@ class Gyro : private Port { /** * \addtogroup cpp-adi * @{ - */ + */ public: /** * Initializes a gyroscope on the given port. If the given port has not @@ -1182,12 +1181,12 @@ class Gyro : private Port { * The ADI port to initialize as a gyro (from 1-8, 'a'-'h', 'A'-'H') * \param multiplier * A scalar value that will be multiplied by the gyro heading value - * supplied by the - * + * supplied by the + * * \b Example * \code * #define GYRO_PORT 1 - * + * * void opcontrol() { * pros::adi::Gyro gyro (GYRO_PORT); * while (true) { @@ -1221,15 +1220,15 @@ class Gyro : private Port { * ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure * \param multiplier * A scalar value that will be multiplied by the gyro heading value - * supplied by the - * + * supplied by the + * * \b Example * \code * #define ADI_GYRO_PORT 'a' * #define SMART_PORT 1 - * + * * void opcontrol() { - * pros::adi::Gyro gyro ({{ SMART_PORT , ADI_GYRO_PORT }}); + * pros::adi::Gyro gyro ({SMART_PORT ,ADI_GYRO_PORT}); * while (true) { * // Get the gyro heading * std::cout << "Distance: " << gyro.get_value(); @@ -1239,7 +1238,7 @@ class Gyro : private Port { * \endcode */ explicit Gyro(ext_adi_port_pair_t port_pair, double multiplier = 1); - + /** * Gets the current gyro angle in tenths of a degree. Unless a multiplier is * applied to the gyro, the return value will be a whole number representing @@ -1253,11 +1252,11 @@ class Gyro : private Port { * ENODEV - The port is not configured as a gyro * * \return The gyro angle in degrees. - * + * * \b Example * \code * #define GYRO_PORT 1 - * + * * void opcontrol() { * pros::adi::Gyro gyro (GYRO_PORT); * while (true) { @@ -1279,24 +1278,24 @@ class Gyro : private Port { * * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. - * + * * \b Example * \code * #define GYRO_PORT 1 - * + * * void opcontrol() { * pros::adi::Gyro gyro (GYRO_PORT); * std::uint32_t now = pros::millis(); * while (true) { * // Get the gyro heading * std::cout << "Distance: " << gyro.get_value(); - * + * * if (pros::millis() - now > 2000) { * // Reset the gyro every 2 seconds * gyro.reset(); * now = pros::millis(); * } - * + * * pros::delay(10); * } * } @@ -1313,7 +1312,7 @@ class Potentiometer : public AnalogIn { /** * \addtogroup cpp-adi * @{ - */ + */ public: /** * Configures an ADI port to act as a Potentiometer. @@ -1326,22 +1325,22 @@ class Potentiometer : public AnalogIn { * \param adi_port * The ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure * \param potentiometer_type - * An adi_potentiometer_type_e_t enum value specifying the potentiometer version type - * - * \b Example - * \code - * #define POTENTIOMETER_PORT 1 - * #define POTENTIOMETER_TYPE pros::E_ADI_POT_EDR - * - * void opcontrol() { - * pros::adi::Potentiometer potentiometer (POTENTIOMETER_PORT, POTENTIOMETER_TYPE); - * while (true) { - * // Get the potentiometer angle - * std::cout << "Angle: " << potentiometer.get_angle(); - * pros::delay(10); - * } - * } - * \endcode + * An adi_potentiometer_type_e_t enum value specifying the potentiometer version type + * + * \b Example + * \code + * #define POTENTIOMETER_PORT 1 + * #define POTENTIOMETER_TYPE pros::E_ADI_POT_EDR + * + * void opcontrol() { + * pros::adi::Potentiometer potentiometer (POTENTIOMETER_PORT, POTENTIOMETER_TYPE); + * while (true) { + * // Get the potentiometer angle + * std::cout << "Angle: " << potentiometer.get_angle(); + * pros::delay(10); + * } + * } + * \endcode */ explicit Potentiometer(std::uint8_t adi_port, adi_potentiometer_type_e_t potentiometer_type = E_ADI_POT_EDR); @@ -1357,15 +1356,15 @@ class Potentiometer : public AnalogIn { * The pair of the smart port number (from 1-22) and the * ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure * \param potentiometer_type - * An adi_potentiometer_type_e_t enum value specifying the potentiometer version type - * + * An adi_potentiometer_type_e_t enum value specifying the potentiometer version type + * * \b Example * \code * #define ADI_POTENTIOMETER_PORT 'a' * #define SMART_PORT 1 - * + * * void opcontrol() { - * pros::adi::Potentiometer potentiometer ({{ SMART_PORT , ADI_POTENTIOMETER_PORT }}); + * pros::adi::Potentiometer potentiometer ({SMART_PORT, ADI_POTENTIOMETER_PORT}); * while (true) { * // Get the potentiometer angle * std::cout << "Angle: " << potentiometer.get_angle(); @@ -1386,14 +1385,14 @@ class Potentiometer : public AnalogIn { * reached: * ENXIO - The given value is not within the range of ADI Ports * EADDRINUSE - The port is not configured as a potentiometer - * + * * \return The potentiometer angle in degrees. - * + * * \b Example * \code * #define ADI_POTENTIOMETER_PORT 'a' * #define SMART_PORT 1 - * + * * void opcontrol() { * pros::adi::Potentiometer potentiometer ({{ SMART_PORT , ADI_POTENTIOMETER_PORT }}); * while (true) { @@ -1460,14 +1459,13 @@ class Potentiometer : public AnalogIn { /** * This is the overload for the << operator for printing to streams - * Potentiometer [value: (value), value calibrated: (calibrated value), + * Potentiometer [value: (value), value calibrated: (calibrated value), * angle: (angle)] * Prints in format(this below is all in one line with no new line): - */ + */ friend std::ostream& operator<<(std::ostream& os, pros::adi::Potentiometer& potentiometer); using Port::get_port; - }; ///@} @@ -1476,11 +1474,11 @@ class Led : protected Port { /** * \addtogroup cpp-adi * @{ - */ + */ public: /** * @brief Configures an ADI port to act as a LED. - * + * * This function uses the following values of errno when an error state is * reached: * ENXIO - Either the ADI port value or the smart port value is not within its @@ -1490,12 +1488,12 @@ class Led : protected Port { * The ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure * \param length * The number of LEDs in the chain - * - * \b Example: + * + * \b Example: * \code * #define LED_PORT 'a' * #define LED_LENGTH 3 - * + * * void opcontrol() { * pros::Led led (LED_PORT, LED_LENGTH); * while (true) { @@ -1504,14 +1502,14 @@ class Led : protected Port { * pros::delay(20); * } * } - * \endcode - * + * \endcode + * */ explicit Led(std::uint8_t adi_port, std::uint32_t length); /** * @brief Configures an ADI port on a adi_expander to act as a LED. - * + * * This function uses the following values of errno when an error state is * reached: * ENXIO - Either the ADI port value or the smart port value is not within its @@ -1522,15 +1520,15 @@ class Led : protected Port { * ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure * \param length * The number of LEDs in the chain - * - * \b Example: + * + * \b Example: * \code * #define LED_PORT 'a' * #define SMART_PORT 1 * #define LED_LENGTH 3 - * + * * void opcontrol() { - * pros::Led led ({{ SMART_PORT , LED_PORT }}, LED_LENGTH); + * pros::Led led ({SMART_PORT, LED_PORT}, LED_LENGTH); * while (true) { * // Set entire LED strip to red * led.set_all(0xFF0000); @@ -1542,17 +1540,17 @@ class Led : protected Port { explicit Led(ext_adi_port_pair_t port_pair, std::uint32_t length); /** - * @brief Operator overload to access the buffer in the ADILed class, it is + * @brief Operator overload to access the buffer in the ADILed class, it is * recommended that you call .update(); after doing any operations with this. - * + * * @param i 0 indexed pixel of the lED * @return uint32_t& the address of the buffer at i to modify - * - * \b Example: + * + * \b Example: * \code * #define LED_PORT 'a' * #define LED_LENGTH 3 - * + * * void opcontrol() { * pros::Led led (LED_PORT, LED_LENGTH); * while (true) { @@ -1561,7 +1559,7 @@ class Led : protected Port { * led.set_pixel(0x00FF00, 1); * led.set_pixel(0x0000FF, 2); * pros::delay(20); - * + * * // Use the [] operator to set the first pixel to black * led.operator[](0) = 0x000000; * led.update(); @@ -1569,194 +1567,194 @@ class Led : protected Port { * } * } */ - std::uint32_t& operator[] (size_t i); - - /** - * @brief Clear the entire led strip of color - * - * This function uses the following values of errno when an error state is - * reached: - * ENXIO - The given value is not within the range of ADI Ports - * EINVAL - A parameter is out of bounds/incorrect - * EADDRINUSE - The port is not configured for ADI output - * - * @return PROS_SUCCESS if successful, PROS_ERR if not - * - * \b Example: - * \code - * #define LED_PORT 'a' - * #define LED_LENGTH 3 - * - * void opcontrol() { - * pros::Led led (LED_PORT, LED_LENGTH); - * while (true) { - * // Set the first 3 pixels to red, green, and blue - * led.set_pixel(0xFF0000, 0); - * led.set_pixel(0x00FF00, 1); - * led.set_pixel(0x0000FF, 2); - * pros::delay(20); - * - * // Clear the led strip of color - * led.clear(); - * pros::delay(20); - * } - * } - * \endcode - */ + std::uint32_t& operator[](size_t i); + + /** + * @brief Clear the entire led strip of color + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of ADI Ports + * EINVAL - A parameter is out of bounds/incorrect + * EADDRINUSE - The port is not configured for ADI output + * + * @return PROS_SUCCESS if successful, PROS_ERR if not + * + * \b Example: + * \code + * #define LED_PORT 'a' + * #define LED_LENGTH 3 + * + * void opcontrol() { + * pros::Led led (LED_PORT, LED_LENGTH); + * while (true) { + * // Set the first 3 pixels to red, green, and blue + * led.set_pixel(0xFF0000, 0); + * led.set_pixel(0x00FF00, 1); + * led.set_pixel(0x0000FF, 2); + * pros::delay(20); + * + * // Clear the led strip of color + * led.clear(); + * pros::delay(20); + * } + * } + * \endcode + */ std::int32_t clear_all(); std::int32_t clear(); - - /** - * @brief Force the LED strip to update with the current buffered values, this - * should be called after any changes to the buffer using the [] operator. - * - * This function uses the following values of errno when an error state is - * reached: - * EINVAL - A parameter is out of bounds/incorrect - * EADDRINUSE - The port is not configured for ADI output - * - * @return PROS_SUCCESS if successful, PROS_ERR if not - * - * \b Example: - * \code - * #define LED_PORT 'a' - * #define LED_LENGTH 3 - * - * void opcontrol() { - * pros::Led led (LED_PORT, LED_LENGTH); - * while (true) { - * // Set the first 3 pixels to red, green, and blue - * led.set_pixel(0xFF0000, 0); - * led.set_pixel(0x00FF00, 1); - * led.set_pixel(0x0000FF, 2); - * pros::delay(20); - * - * // Use the [] operator to set the first pixel to black - * led.operator[](0) = 0x000000; - * // Update the led strip with the new values - * led.update(); - * pros::delay(20); - * } - * } - * \endcode - */ + + /** + * @brief Force the LED strip to update with the current buffered values, this + * should be called after any changes to the buffer using the [] operator. + * + * This function uses the following values of errno when an error state is + * reached: + * EINVAL - A parameter is out of bounds/incorrect + * EADDRINUSE - The port is not configured for ADI output + * + * @return PROS_SUCCESS if successful, PROS_ERR if not + * + * \b Example: + * \code + * #define LED_PORT 'a' + * #define LED_LENGTH 3 + * + * void opcontrol() { + * pros::Led led (LED_PORT, LED_LENGTH); + * while (true) { + * // Set the first 3 pixels to red, green, and blue + * led.set_pixel(0xFF0000, 0); + * led.set_pixel(0x00FF00, 1); + * led.set_pixel(0x0000FF, 2); + * pros::delay(20); + * + * // Use the [] operator to set the first pixel to black + * led.operator[](0) = 0x000000; + * // Update the led strip with the new values + * led.update(); + * pros::delay(20); + * } + * } + * \endcode + */ std::int32_t update() const; /** - * @brief Set the entire led strip to one color - * - * This function uses the following values of errno when an error state is - * reached: - * EINVAL - A parameter is out of bounds/incorrect - * EADDRINUSE - The port is not configured for ADI output - * - * @param color color to set all the led strip value to - * @return PROS_SUCCESS if successful, PROS_ERR if not - * - * \b Example: - * \code - * #define LED_PORT 'a' - * #define LED_LENGTH 3 - * - * void opcontrol() { - * pros::Led led (LED_PORT, LED_LENGTH); - * while (true) { - * // Set the entire led strip to blue - * led.set_all(0x0000FF); - * pros::delay(20); - * } - * } - * \endcode - */ + * @brief Set the entire led strip to one color + * + * This function uses the following values of errno when an error state is + * reached: + * EINVAL - A parameter is out of bounds/incorrect + * EADDRINUSE - The port is not configured for ADI output + * + * @param color color to set all the led strip value to + * @return PROS_SUCCESS if successful, PROS_ERR if not + * + * \b Example: + * \code + * #define LED_PORT 'a' + * #define LED_LENGTH 3 + * + * void opcontrol() { + * pros::Led led (LED_PORT, LED_LENGTH); + * while (true) { + * // Set the entire led strip to blue + * led.set_all(0x0000FF); + * pros::delay(20); + * } + * } + * \endcode + */ std::int32_t set_all(uint32_t color); /** - * @brief Set one pixel on the led strip - * - * This function uses the following values of errno when an error state is - * reached: - * EINVAL - A parameter is out of bounds/incorrect - * EADDRINUSE - The port is not configured for ADI output - * - * @param color color to clear all the led strip to - * @param pixel_position position of the pixel to clear - * @return PROS_SUCCESS if successful, PROS_ERR if not - * - * \b Example: - * \code - * #define LED_PORT 'a' - * #define LED_LENGTH 3 - * - * void opcontrol() { - * pros::Led led (LED_PORT, LED_LENGTH); - * while (true) { - * // Set the first pixel to blue - * led.set_pixel(0x0000FF, 0); - * pros::delay(20); - * } - * } - * \endcode - */ + * @brief Set one pixel on the led strip + * + * This function uses the following values of errno when an error state is + * reached: + * EINVAL - A parameter is out of bounds/incorrect + * EADDRINUSE - The port is not configured for ADI output + * + * @param color color to clear all the led strip to + * @param pixel_position position of the pixel to clear + * @return PROS_SUCCESS if successful, PROS_ERR if not + * + * \b Example: + * \code + * #define LED_PORT 'a' + * #define LED_LENGTH 3 + * + * void opcontrol() { + * pros::Led led (LED_PORT, LED_LENGTH); + * while (true) { + * // Set the first pixel to blue + * led.set_pixel(0x0000FF, 0); + * pros::delay(20); + * } + * } + * \endcode + */ std::int32_t set_pixel(uint32_t color, uint32_t pixel_position); /** - * @brief Clear one pixel on the led strip - * - * This function uses the following values of errno when an error state is - * reached: - * EINVAL - A parameter is out of bounds/incorrect - * EADDRINUSE - The port is not configured for ADI output - * - * @param pixel_position position of the pixel to clear - * @return PROS_SUCCESS if successful, PROS_ERR if not - * - * \b Example: - * \code - * #define LED_PORT 'a' - * #define LED_LENGTH 3 - * - * void opcontrol() { - * pros::Led led (LED_PORT, LED_LENGTH); - * while (true) { - * // Set the first pixel to blue - * led.set_pixel(0x0000FF, 0); - * pros::delay(20); - * - * // Clear the first pixel - * led.clear_pixel(0); - * pros::delay(20); - * } - * } - * \endcode - */ + * @brief Clear one pixel on the led strip + * + * This function uses the following values of errno when an error state is + * reached: + * EINVAL - A parameter is out of bounds/incorrect + * EADDRINUSE - The port is not configured for ADI output + * + * @param pixel_position position of the pixel to clear + * @return PROS_SUCCESS if successful, PROS_ERR if not + * + * \b Example: + * \code + * #define LED_PORT 'a' + * #define LED_LENGTH 3 + * + * void opcontrol() { + * pros::Led led (LED_PORT, LED_LENGTH); + * while (true) { + * // Set the first pixel to blue + * led.set_pixel(0x0000FF, 0); + * pros::delay(20); + * + * // Clear the first pixel + * led.clear_pixel(0); + * pros::delay(20); + * } + * } + * \endcode + */ std::int32_t clear_pixel(uint32_t pixel_position); /** - * @brief Get the length of the led strip - * - * This function uses the following values of errno when an error state is - * reached: - * EINVAL - A parameter is out of bounds/incorrect - * EADDRINUSE - The port is not configured for ADI output - * - * @return The length (in pixels) of the LED strip - * - * \b Example: - * \code - * #define LED_PORT 'a' - * #define LED_LENGTH 3 - * - * void opcontrol() { - * pros::Led led (LED_PORT, LED_LENGTH); - * while (true) { - * // Get the length of the led strip - * int length = led.length(); - * pros::lcd::print(1, "Length: %d", length); - * pros::delay(20); - * } - * } - * \endcode - */ + * @brief Get the length of the led strip + * + * This function uses the following values of errno when an error state is + * reached: + * EINVAL - A parameter is out of bounds/incorrect + * EADDRINUSE - The port is not configured for ADI output + * + * @return The length (in pixels) of the LED strip + * + * \b Example: + * \code + * #define LED_PORT 'a' + * #define LED_LENGTH 3 + * + * void opcontrol() { + * pros::Led led (LED_PORT, LED_LENGTH); + * while (true) { + * // Get the length of the led strip + * int length = led.length(); + * pros::lcd::print(1, "Length: %d", length); + * pros::delay(20); + * } + * } + * \endcode + */ std::int32_t length(); using Port::get_port; @@ -1773,35 +1771,34 @@ class Pneumatics : public DigitalOut { /** * \addtogroup cpp-adi * @{ - */ + */ public: - /** * Creates a Pneumatics object for the given port. - * + * * This function uses the following values of errno when an error state is * reached: * ENXIO - The given value is not within the range of ADI Ports - * + * * \param adi_port * The ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure * \param start_extended * If true, the pneumatic will start extended when the program starts. * By default, the piston starts retracted when the program starts. * \param extended_is_low - * A flag to set whether the the pneumatic is extended when the ADI + * A flag to set whether the the pneumatic is extended when the ADI * it receives a high or a low value. When true, the extended state - * corresponds to a output low on the ADI port. This allows the user + * corresponds to a output low on the ADI port. This allows the user * to reverse the behavior of the pneumatics if needed. - * + * * /b Example: * \code * void opcontrol() { * pros::adi::Pneumatics left_piston('a', false); // Starts retracted, extends when the ADI port is high - * pros::adi::Pneumatics right_piston('b', false, true); // Starts retracted, extends when the ADI port is low - * + * pros::adi::Pneumatics right_piston('b', false, true); // Starts retracted, extends when the ADI port is low + * * pros::Controller master(pros::E_CONTROLLER_MASTER); - * + * * while (true) { * if(master.get_digital(pros::E_CONTROLLER_DIGITAL_L1)) { * left_piston.extend(); @@ -1809,31 +1806,28 @@ class Pneumatics : public DigitalOut { * if(master.get_digital(pros::E_CONTROLLER_DIGITAL_L2)) { * left_piston.retract(); * } - * + * * if(master.get_digital(pros::E_CONTROLLER_DIGITAL_R1)) { * left_piston.extend(); * } * if(master.get_digital(pros::E_CONTROLLER_DIGITAL_2)) { * left_piston.retract(); * } - * + * * pros::delay(10); * } - * + * * \endcode */ - explicit Pneumatics(std::uint8_t adi_port, - bool start_extended, - bool extended_is_low = false - ); + explicit Pneumatics(std::uint8_t adi_port, bool start_extended, bool extended_is_low = false); /** * Creates a Pneumatics object for the given port pair. - * + * * This function uses the following values of errno when an error state is * reached: * ENXIO - The given value is not within the range of ADI Ports - * + * * \param port_pair * The pair of the smart port number (from 1-22) and the * ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure @@ -1841,19 +1835,20 @@ class Pneumatics : public DigitalOut { * If true, the pneumatic will start extended when the program starts. * By default, the piston starts retracted when the program starts. * \param extended_is_low - * A flag to set whether the the pneumatic is extended when the ADI + * A flag to set whether the the pneumatic is extended when the ADI * it receives a high or a low value. When true, the extended state - * corresponds to a output low on the ADI port. This allows the user + * corresponds to a output low on the ADI port. This allows the user * to reverse the behavior of the pneumatics if needed. - * + * * /b Example: * \code * void opcontrol() { * pros::adi::Pneumatics left_piston({1, 'a'}, false); // Starts retracted, extends when the ADI port is high - * pros::adi::Pneumatics right_piston({1, 'b'}, false, true); // Starts retracted, extends when the ADI port is low - * + * pros::adi::Pneumatics right_piston({1, 'b'}, false, true); // Starts retracted, extends when the ADI port is + *low + * * pros::Controller master(pros::E_CONTROLLER_MASTER); - * + * * while (true) { * if(master.get_digital(pros::E_CONTROLLER_DIGITAL_L1)) { * left_piston.extend(); @@ -1861,37 +1856,34 @@ class Pneumatics : public DigitalOut { * if(master.get_digital(pros::E_CONTROLLER_DIGITAL_L2)) { * left_piston.retract(); * } - * + * * if(master.get_digital(pros::E_CONTROLLER_DIGITAL_R1)) { * left_piston.extend(); * } * if(master.get_digital(pros::E_CONTROLLER_DIGITAL_R2)) { * left_piston.retract(); * } - * + * * pros::delay(10); * } * } * \endcode */ - explicit Pneumatics(ext_adi_port_pair_t port_pair, - bool start_extended, - bool extended_is_low = false - ); + explicit Pneumatics(ext_adi_port_pair_t port_pair, bool start_extended, bool extended_is_low = false); /** * Extends the piston, if not already extended. - * + * * \return 1 if the piston newly extended, 0 if the piston was already - * extended, or PROS_ERR is the operation failed, setting errno. - * + * extended, or PROS_ERR is the operation failed, setting errno. + * * \b Example: * \code * void opcontrol() { * pros::adi::Pneumatics piston({1, 'a'}, false); // Starts retracted, extends when the ADI port is high - * + * * pros::Controller master(pros::E_CONTROLLER_MASTER); - * + * * while (true) { * if(master.get_digital(pros::E_CONTROLLER_DIGITAL_X)) { * left_piston.extend(); @@ -1902,7 +1894,7 @@ class Pneumatics : public DigitalOut { * if(mastetr.get_digital(pros::E_CONTROLLER_DIGITAL_A)) { * left_piston.toggle(); * } - * + * * pros::delay(10); * } * } @@ -1914,15 +1906,15 @@ class Pneumatics : public DigitalOut { * Retracts the piston, if not already retracted. * * \return 1 if the piston newly retracted, 0 if the piston was already - * retracted, or PROS_ERR is the operation failed, setting errno. + * retracted, or PROS_ERR is the operation failed, setting errno. * * \b Example: * \code * void opcontrol() { * pros::adi::Pneumatics piston({1, 'a'}, false); // Starts retracted, extends when the ADI port is high - * + * * pros::Controller master(pros::E_CONTROLLER_MASTER); - * + * * while (true) { * if(master.get_digital(pros::E_CONTROLLER_DIGITAL_X)) { * left_piston.extend(); @@ -1933,7 +1925,7 @@ class Pneumatics : public DigitalOut { * if(mastetr.get_digital(pros::E_CONTROLLER_DIGITAL_A)) { * left_piston.toggle(); * } - * + * * pros::delay(10); * } * } @@ -1947,17 +1939,17 @@ class Pneumatics : public DigitalOut { * * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. - * - * \return 1 if the piston successfully toggled, or PROS_ERR if the + * + * \return 1 if the piston successfully toggled, or PROS_ERR if the * operation failed, setting errno. * *\b Example: * \code * void opcontrol() { * pros::adi::Pneumatics piston({1, 'a'}, false); // Starts retracted, extends when the ADI port is high - * + * * pros::Controller master(pros::E_CONTROLLER_MASTER); - * + * * while (true) { * if(master.get_digital(pros::E_CONTROLLER_DIGITAL_X)) { * left_piston.extend(); @@ -1968,7 +1960,7 @@ class Pneumatics : public DigitalOut { * if(mastetr.get_digital(pros::E_CONTROLLER_DIGITAL_A)) { * left_piston.toggle(); * } - * + * * pros::delay(10); * } * } @@ -1977,10 +1969,10 @@ class Pneumatics : public DigitalOut { std::int32_t toggle(); /** - * Returns whether the piston is extended or not. - * + * Returns whether the piston is extended or not. + * * \return true if the piston is extended, false if it is retracted. - * + * * \b Example * \code * #define ADI_PNEUMATICS_PORT 'a' @@ -1995,20 +1987,18 @@ class Pneumatics : public DigitalOut { * else { * printf("The pneumatic is not extended\n"); * } - * + * * pros::delay(10); * } * } * \endcode */ - bool is_extended() const; + bool is_extended() const; -private: - bool state; // Holds the physical state of the ADI port - bool extended_is_low; // A flag that sets whether extended corresponds to - // a low signal - - + private: + bool state; // Holds the physical state of the ADI port + bool extended_is_low; // A flag that sets whether extended corresponds to + // a low signal }; ///@} @@ -2017,7 +2007,7 @@ class Pneumatics : public DigitalOut { /* Pros4 upgrade backwards compatibility for ADI api. -Prints a deprecated warning when user uses old pros::ADIDevice style API. +Prints a deprecated warning when user uses old pros::ADIDevice style API. Remove when and if fully removing old API. */ LEGACY_TYPEDEF(ADIPort, pros::adi::Port); @@ -2032,15 +2022,15 @@ LEGACY_TYPEDEF(ADIUltrasonic, pros::adi::Ultrasonic); LEGACY_TYPEDEF(LED, pros::adi::Led); // Backwards Compatibility for Derived Classes -LEGACY_TYPEDEF(ADIPotentiometer,pros::adi::Potentiometer); -LEGACY_TYPEDEF(ADILineSensor,pros::adi::LineSensor); -LEGACY_TYPEDEF(ADILightSensor,pros::adi::LightSensor); -LEGACY_TYPEDEF(ADIAccelerometer,pros::adi::Accelerometer); -LEGACY_TYPEDEF(ADIButton,pros::adi::Button); -LEGACY_TYPEDEF(ADIPneumatics,pros::adi::Pneumatics); +LEGACY_TYPEDEF(ADIPotentiometer, pros::adi::Potentiometer); +LEGACY_TYPEDEF(ADILineSensor, pros::adi::LineSensor); +LEGACY_TYPEDEF(ADILightSensor, pros::adi::LightSensor); +LEGACY_TYPEDEF(ADIAccelerometer, pros::adi::Accelerometer); +LEGACY_TYPEDEF(ADIButton, pros::adi::Button); +LEGACY_TYPEDEF(ADIPneumatics, pros::adi::Pneumatics); LEGACY_TYPEDEF(ADILED, pros::adi::Led); LEGACY_TYPEDEF(ADILed, pros::adi::Led); } // namespace pros -#endif // _PROS_ADI_HPP_ +#endif // _PROS_ADI_HPP_ \ No newline at end of file diff --git a/include/pros/ai_vision.h b/include/pros/ai_vision.h new file mode 100644 index 00000000..5deea418 --- /dev/null +++ b/include/pros/ai_vision.h @@ -0,0 +1,502 @@ +/** + * \file pros/aivision.h + * \ingroup c-aivision + * + * Contains prototypes for the VEX AI Vision Sensor-related functions. + * + * This file should not be modified by users, since it gets replaced whenever + * a kernel upgrade occurs. + * + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. + * All rights reserved. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * \defgroup c-aivision AI Vision Sensor C API + * \note Additional example code for this module can be found in its [Tutorial.](@ref aivision) + */ + +#ifndef _PROS_AIVISION_H_ +#define _PROS_AIVISION_H_ + +/** + * \addtogroup c-aivision + * @{ + */ + +/// \name Macros +/// Parameters given by VEX +///@{ + +#define AIVISION_MAX_OBJECT_COUNT 24 +#define AIVISION_MAX_CLASSNAME_COUNT 20 +#define AIVISION_MODE_TAG_SET_BIT (1 << 29) + +///@} + +///@} + +#include + +#ifdef __cplusplus +extern "C" { +namespace pros { +#endif + +/** + * \addtogroup c-aivision + * @{ + */ + +/** + * \enum aivision_detected_type_e_t + * This enumeration defines what kind of object is stored inside the union in aivision_object_s + */ +typedef enum aivision_detected_type { + E_AIVISION_DETECTED_COLOR = (1 << 0), + E_AIVISION_DETECTED_CODE = (1 << 1), + E_AIVISION_DETECTED_OBJECT = (1 << 2), + E_AIVISION_DETECTED_TAG = (1 << 3) +} aivision_detected_type_e_t; + +/** + * \enum aivision_mode_type_e_t + * This enumeration defines what kinds of objects the ai vision sensor will scan for: + * tags (april tags), colors (user defined colors), and objects (game elements), and all (all objects) + */ + +typedef enum aivision_mode_type { + E_AIVISION_MODE_TAGS = (1 << 0), + E_AIVISION_MODE_COLORS = (1 << 1), + E_AIVISION_MODE_OBJECTS = (1 << 2), + E_AIVISION_MODE_COLOR_MERGE = (1 << 4), + E_AIVISION_MODE_ALL = (1 << 0) | (1 << 1) | (1 << 2), +} aivision_mode_type_e_t; + +/** + * \struct aivision_color_s_t + * This structure contains the parameters used by the AI Vision Sensor to define a color. hue_range and saturation_range + * are ranges for hue and saturation that are acceptable. + * For example, if a large hue range is specified for a blue color, colors that are more magenta or teal may be detected + * as "blue". + */ +typedef struct aivision_color_s { + uint8_t id; /**< id of color descriptor, can range from 1-7 */ + uint8_t red; /**< red value of color */ + uint8_t green; /**< green value of color */ + uint8_t blue; /**< blue value of color */ + float hue_range; /**< range by which detected color's hue can vary from the base color, can range from 1-40 */ + float saturation_range; /**< range by which detected color's saturation can vary from base color, can range from 0.1-1 */ +} aivision_color_s_t; + +/** + * \struct aivision_code_s_t + * This structure contains the parameters used by the AI Vision sensor to define a code. + * Codes are a combination of color descriptors, and tells the AI Vision sensor to merge objects + * close to each other that belong to the given color descriptors into a single object that matches + * the code descriptor. + * Codes must use at least 2, and no greater than 5, color descriptors. + */ +typedef struct aivision_code_s { + uint8_t id; /**< id of code descriptor, can range from 1-5 */ + uint8_t length; /**< number of color descriptors used by this code. */ + int16_t c1; /**< id of first color descriptor */ + int16_t c2; /**< id of second color descriptor */ + int16_t c3; /**< id of third color descriptor */ + int16_t c4; /**< id of fourth color descriptor */ + int16_t c5; /**< id of fifth color descriptor */ +} aivision_code_s_t; + +/** + * \enum aivision_tag_family_e_t + * This enumeration corresponds to a family of AprilTags. + * \see https://april.eecs.umich.edu/software/apriltag + */ +typedef enum aivision_tag_family_e { + TAG_CIRCLE_21H7 = 0, + TAG_16H5 = 1, + TAG_25H9 = 2, + TAG_61H11 = 3 +} aivision_tag_family_e_t; + +/** + * \struct aivision_object_color_s_t + * This structure contains a detected color. + */ +typedef struct __attribute__((packed)) aivision_object_color_s { + uint16_t xoffset; // left edge (from camera's view) + uint16_t yoffset; // top edge + uint16_t width; + uint16_t height; + uint16_t angle; // angle, in tenths of a degree +} aivision_object_color_s_t; + +/** + * \struct aivision_object_tag_s_t + * This structure contains a detected tag. + */ +typedef struct __attribute__((packed)) aivision_object_tag_s { + int16_t x0; + int16_t y0; + int16_t x1; + int16_t y1; + int16_t x2; + int16_t y2; + int16_t x3; + int16_t y3; +} aivision_object_tag_s_t; + +typedef struct __attribute__((packed)) aivision_object_element_s { + uint16_t xoffset; // left + uint16_t yoffset; // top + uint16_t width; + uint16_t height; + uint16_t score; // confidence that this struct is +} aivision_object_element_s_t; +/** + * \struct aivision_object_s_t + * This structure contains one of aivision_detected_type_e_t, stored in type + * + * If the object is a color, id stores the color's id + * If the object is an April Tag, id stores the tag's id + * If the object is an AI model element, id stores the element id as per + * https://api.vex.com/v5/home/cpp/AiVision/AiObjdesc.html + */ +typedef struct __attribute__((packed)) aivision_object_s { + uint8_t id; // object id + uint8_t type; // object type + union { + aivision_object_color_s_t color; + aivision_object_tag_s_t tag; + aivision_object_element_s_t element; + } object; +} aivision_object_s_t; +/// @} + +#ifdef __cplusplus +namespace c { +#endif + +/** + * \addtogroup c-aivision + * @{ + */ +/// \name Functions + +/** + * Resets the AI Vision sensor to the initial state. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \param port The V5 port number from 1-21 + * + * \return PROS_SUCCESS if the operation was successful or PROS_ERR if the operation + * failed, setting errno. + * + * \b Example + * \code + * #define AIVISION_PORT 1 + * void initialize() { + * aivision_reset(AIVISION_PORT); + * } + * \endcode + */ +int32_t aivision_reset(uint8_t port); + +/** + * Returns a bitfield of the types of objects the AI vision sensor is currently searching for, + * as per aivision_mode_type_e_t. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \param port The V5 port number from 1-21 + * \return the bitfield if the operation was successful or PROS_ERR if the operation + * failed, setting errno. + * + * \b Example + * \code + * #define AIVISION_PORT 1 + * void initialize() { + * aivision_get_enabled_detection_types(AIVISION_PORT); + * } + * \endcode + */ +int32_t aivision_get_enabled_detection_types(uint8_t port); + +/** + * Modifies the types of objects the AI vision sensor is currently searching for, as per aivision_mode_type_e_t. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \b Example + * aivision_set_enabled_detection_types(1, 0b010, 0b101) would disable the detection of tags and objects, + * and leave the setting of colors alone. + * + * \param port The V5 port number from 1-21 + * \param bits the bits to set + * \param bitmask the bitmask to apply + * \return PROS_SUCCESS if the operation was successful or PROS_ERR if the operation + * failed, setting errno. + */ +int32_t aivision_set_enabled_detection_types(uint8_t port, uint8_t bits, uint8_t bitmask); + +/** + * Enable detecting these types of objects, a bitmask as per aivision_mode_type_e_t. + * Enabling any given type of object will not disable the detection of other objects. + * This must be done explicitly. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \b Example + * \code + * #define AIVISION_PORT 1 + * void initialize() { + * // start or continue looking for AI model objects + * aivision_enable_detection_types(AIVISION_PORT, aivision_mode_type_e_t::E_AIVISION_MODE_OBJECTS); + * } + * \endcode + * + * \param port The V5 port number from 1-21 + * \param types_mask The types to enable + * \return PROS_SUCCESS if the operation was successful or PROS_ERR if the operation + * failed, setting errno. + */ +int32_t aivision_enable_detection_types(uint8_t port, uint8_t types_mask); + +/** + * Disable detecting these types of objects, a bitmask as per aivision_mode_type_e_t. + * Disabling any given type of object will not affect the detection of other objects. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \b Example + * \code + * #define AIVISION_PORT 1 + * void initialize() { + * // stop looking for AI model objects (competition elements, for example) + * aivision_disable_detection_types(AIVISION_PORT, aivision_mode_type_e_t::E_AIVISION_MODE_OBJECTS); + * } + * \endcode + * + * \param port The V5 port number from 1-21 + * \param types_mask The types to enable + * \return PROS_SUCCESS if the operation was successful or PROS_ERR if the operation + * failed, setting errno. + */ +int32_t aivision_disable_detection_types(uint8_t port, uint8_t types_mask); + +/** + * Sets the april tag family to detect. Use this function will override the enabled apriltag + * detection family. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \param port The V5 port number from 1-21 + * \param family the tag family to configure the AI Vision sensor to detect + * \return PROS_SUCCESS if the operation was successful or PROS_ERR if the operation + * failed, setting errno. + */ +int32_t aivision_set_tag_family_override(uint8_t port, aivision_tag_family_e_t family); + +/** + * Sets the april tag family to detect. Use this function will allow multiple apriltags + * to be detected. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \param port The V5 port number from 1-21 + * \param family the tag family to configure the AI Vision sensor to detect + * \return PROS_SUCCESS if the operation was successful or PROS_ERR if the operation + * failed, setting errno. + */ +int32_t aivision_set_tag_family(uint8_t port, aivision_tag_family_e_t family); + +/** + * Set a color configuration that the AI vision sensor will detect. + * The color detection type must be separately enabled. + * If a color with the same ID already is stored in the sensor, it will be overwritten. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \param port The V5 port number from 1-21 + * \param color the color to configure the AI Vision sensor to detect + * \return PROS_SUCCESS if the operation was successful or PROS_ERR if the operation + * failed, setting errno + */ +int32_t aivision_set_color(uint8_t port, const aivision_color_s_t* color); + +/** + * Get a color configuration that the AI vision sensor has stored. + * If you attempt to get a color configuration that has not been previously used, the + * behavior is not defined. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \param port The V5 port number from 1-21 + * \param id the id of color from 1-7 + * \return PROS_SUCCESS if the operation was successful or PROS_ERR if the operation + * failed, setting errno + */ +aivision_color_s_t aivision_get_color(uint8_t port, uint32_t id); + +/** + * Get a class name that the AI vision sensor has stored. + * The AI Vision sensor may not correctly report classnames for the first several hundred milliseconds + * of being plugged in. + * By passing in -1 for the id, the function will return the number of class names the AI vision sensor reports. + * For other values of id, the function return value is undefined + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \param port The V5 port number from 1-21 + * \param id the id of the class name from 0-(AIVISION_MAX_CLASSNAME_COUNT - 1) + * \param class_name a string of length >=20 to store the classname. + * \return PROS_SUCCESS if the operation was successful or PROS_ERR if the operation + * failed, setting errno + */ +int32_t aivision_get_class_name(uint8_t port, int32_t id, uint8_t* class_name); + +/** + * Enable or disable the bounding box overlay the AI Vision sensor outputs on the USB port. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \param port The V5 port number from 1-21 + * \param enabled if the overlay is enabled or disabled + * \return PROS_SUCCESS if the operation was successful or PROS_ERR if the operation + * failed, setting errno + */ +int32_t aivision_set_usb_bounding_box_overlay(uint8_t port, bool enabled); + +/** + * Runs auto white balance to adjust to different lighting conditions. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \param port The V5 port number from 1-21 + * \return PROS_SUCCESS if the operation was successful or PROS_ERR if the operation + * failed, setting errno + */ +int32_t aivision_start_awb(uint8_t port); + +/** + * Get a code that the AI vision sensor has stored. + * + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \param port The V5 port number from 1-21 + * \param id The id from 1-5 + * \return the code, or a struct with an invalid ID if the operation failed, setting errno + */ +aivision_code_s_t aivision_get_code(uint8_t port, uint32_t id); + +/** + * Set a code that the AI vision sensor will detect for. + * The id of the code is stored in the aivision_code_s_t struct. If there is already a code + * stored in the AI vision sensor with the id, this function will overwrite. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \param port The V5 port number from 1-21 + * \param code The code to set + * \return PROS_SUCCESS if the operation was successful or PROS_ERR if the operation + * failed, setting errno + */ +int32_t aivision_set_code(uint8_t port, const aivision_code_s_t* wcode); + +/** + * Get the current number of objects detected by the AI vision sensor. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \param port The V5 port number from 1-21 + * \return the number of objects if the operation was successful or PROS_ERR if the operation failed, setting errno + */ +int32_t aivision_get_object_count(uint8_t port); + +/** + * Get the detected object at a given object index; there are aivision_get_object_count objects and the index starts + * from 0. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * @param port The V5 port number from 1-21 + * @param object_index the object index + * @return the detected object if the operation was successful or an invalid object type if the operation failed, + * setting errno + */ +aivision_object_s_t aivision_get_object(uint8_t port, uint32_t object_index); + +/** + * Get the current reported temperature of the AI Vision sensor in degrees Celsius. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \param port The V5 port number from 1-21 + * \return the temperature if the operation was successful or PROS_ERR_F if the operation failed, setting errno + */ +double aivision_get_temperature(uint8_t port); + +///@} + +#ifdef __cplusplus +} // namespace c +} // namespace pros +} +#endif + +#endif // _PROS_VISION_H_ \ No newline at end of file diff --git a/include/pros/ai_vision.hpp b/include/pros/ai_vision.hpp new file mode 100644 index 00000000..30863d55 --- /dev/null +++ b/include/pros/ai_vision.hpp @@ -0,0 +1,600 @@ +/** + * \file pros/aivision.hpp + * \ingroup cpp-aivision + * + * Contains prototypes for the VEX AI Vision Sensor-related functions in C++. + * + * This file should not be modified by users, since it gets replaced whenever + * a kernel upgrade occurs. + * + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. + * All rights reserved. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * \defgroup cpp-aivision AI Vision Sensor C++ API + * \note Additional example code for this module can be found in its [Tutorial.](@ref aivision) + */ + +#ifndef _PROS_AIVISION_HPP_ +#define _PROS_AIVISION_HPP_ + +#include +#include +#include + +#include "pros/ai_vision.h" +#include "pros/device.hpp" + +namespace pros { +inline namespace v5 { + +/** + * \enum AivisionDetectType + * \ingroup cpp-aivision + * Enum class for describing detection type of objects detected by the AI Vision Sensor. + */ +enum class AivisionDetectType : uint8_t { + color = (1 << 0), /**< object was detected based on color descriptor */ + code = (1 << 1), /**< object was detected based on code descriptor */ + object = (1 << 2), /**< object was detected using AI model */ + tag = (1 << 3) /**< object was detected as an AprilTag */ +}; + +/** + * \enum AivisionModeType + * \ingroup cpp-aivision + * Enum class for enabling/disabling detection types of AI Vision Sensor. + */ +enum class AivisionModeType : uint8_t { + tags = (1 << 0), /**< AprilTag detection */ + colors = (1 << 1), /**< color and code detection */ + objects = (1 << 2), /**< AI model object detection */ + color_merge = (1 << 4), /**< merge adjacent color detections */ + all = (1 << 0) | (1 << 1) | (1 << 2), +}; + +/** + * \enum AivisionTagFamily + * \ingroup cpp-aivision + * Enum class for describing family of apriltags to detect. + */ +enum class AivisionTagFamily { tag_21H7 = 0, tag_16H5 = 1, tag_25H9 = 2, tag_61H11 = 3 }; + +/** + * \ingroup cpp-aivision + */ +class AIVision : public Device { + /** + * \addtogroup cpp-aivision + * @{ + */ + public: + + using Color = aivision_color_s_t; + using Code = aivision_code_s_t; + using Object = aivision_object_s_t; + + /** + * Create a AI Vision Sensor object on the given port. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as an AI vision sensor + * + * \param port + * The V5 port number from 1-21 + * + * \b Example + * \code + * void opcontrol() { + * pros::AIVision ai_sensor(2); // Creates a vision sensor on port two + * } + * \endcode + */ + explicit AIVision(const std::uint8_t port); + + AIVision(const Device& device) : AIVision(device.get_port()){}; + + /** + * Gets all vision sensors. + * + * \return A vector of AIVision sensor objects. + * + * \b Example + * \code + * void opcontrol() { + * std::vector aivision_all = pros::AIVision::get_all_devices(); // All AI vision sensors that are + * connected + * } + * \endcode + */ + static std::vector get_all_devices(); + + /** + * Check if the dected type is the same as the given type. + * + * \return true if the type is the same, false otherwise + * + * \b Example + * \code + * void opcontrol() { + * pros::AIVision aivision(1); + * pros::AIVision::Object object = aivision.get_object(0); + * if (AIVision::is_type(AivisionDetectType::color, object)) { + * printf("is color\n"); + * } else if (AIVision::is_type(AivisionDetectType::object, object)) { + * printf("is object\n"); + * } else if (AIVision::is_type(AivisionDetectType::code, object)) { + * printf("is code\n"); + * } else if (AIVision::is_type(AivisionDetectType::tag, object)) { + * printf("is tag\n"); + * } else { + * printf("unknown\n"); + * } + * } + * \endcode + */ + static bool is_type(const Object& object, AivisionDetectType type); + + /** + * Resets the AI Vision sensor to the initial state. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + + * + * \return PROS_SUCCESS if the operation was successful or PROS_ERR if the operation + * failed, setting errno. + * + * \b Example + * \code + * #define AIVISION_PORT 1 + * void initialize() { + * pros::AIVision aivision(AIVISION_PORT); + * aivision.reset(); + * } + * \endcode + */ + int32_t reset(); + + /** + * Returns a bitfield of the types of objects the AI vision sensor is currently searching for, + * as per AivisionModeType. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + + * \return the bitfield if the operation was successful or PROS_ERR if the operation + * failed, setting errno. + * + * \b Example + * \code + * #define AIVISION_PORT 1 + * void initialize() { + * pros::AIVision aivision(AIVISION_PORT); + * int32_t enabled_types = aivision.get_enabled_detection_types(); + * printf("is tag: %d\n", enabled_types | AivisionModeType::tags); + * } + * \endcode + */ + int32_t get_enabled_detection_types(); + + /** + * Enable detecting these types of objects, a bitmask as per aivision_mode_type_e_t. + * Enabling any given type of object will not disable the detection of other objects. + * This must be done explicitly. + * + * For this function you must use bitwise or to combine the types you want to enable. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \b Example + * \code + * #define AIVISION_PORT 1 + * void initialize() { + * pros::AIVision aivision(AIVISION_PORT); + * // start or continue looking for AI model objects + * // enable aivision to look for tags and objects + * aivision.enable_detection_types(AivisionModeType::tags | AivisionModeType::objects); + * } + * \endcode + * + * \param types_mask The types to enable + * \return PROS_SUCCESS if the operation was successful or PROS_ERR if the operation + * failed, setting errno. + */ + int32_t enable_detection_types(AivisionModeType types_mask); + + /** + * Enable detecting these types of objects, a bitmask as per aivision_mode_type_e_t. + * Enabling any given type of object will not disable the detection of other objects. + * This must be done explicitly. + * + * For this function you can use comma separated values to combine the types you want to enable. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \b Example + * \code + * #define AIVISION_PORT 1 + * void initialize() { + * pros::AIVision aivision(AIVISION_PORT); + * // start or continue looking for AI model objects + * // enable aivision to look for tags and objects + * aivision.enable_detection_types(AivisionModeType::tags, AivisionModeType::objects); + * } + * \endcode + * + * \param types_mask The types to enable + * \return PROS_SUCCESS if the operation was successful or PROS_ERR if the operation + * failed, setting errno. + */ + template + requires((std::conjunction_v...>)) + int32_t enable_detection_types(Flags... flags) { + auto types_mask = (static_cast(flags) | ...); + return c::aivision_enable_detection_types(this->_port, static_cast(types_mask)); + } + + /** + * Disable detecting these types of objects, a bitmask as per aivision_mode_type_e_t. + * Disabling any given type of object will not affect the detection of other objects. + * + * For this function you must use bitwise or to combine the types you want to disable. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \b Example + * \code + * #define AIVISION_PORT 1 + * void initialize() { + * pros::AIVision aivision(AIVISION_PORT); + * // stop looking for AI model objects (competition elements, for example) + * // disable aivision to look for tags and objects + * aivision.disable_detection_types(AivisionModeType::tags | AivisionModeType::objects); + * } + * \endcode + * + * \param types_mask The types to enable + * \return PROS_SUCCESS if the operation was successful or PROS_ERR if the operation + * failed, setting errno. + */ + int32_t disable_detection_types(AivisionModeType types_mask); + + /** + * Disable detecting these types of objects, a bitmask as per aivision_mode_type_e_t. + * Disabling any given type of object will not affect the detection of other objects. + * + * For this function you can use comma separated values to combine the types you want to disable. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \b Example + * \code + * #define AIVISION_PORT 1 + * void initialize() { + * pros::AIVision aivision(AIVISION_PORT); + * // stop looking for AI model objects (competition elements, for example) + * // disable aivision to look for tags and objects + * aivision.disable_detection_types(AivisionModeType::tags | AivisionModeType::objects); + * } + * \endcode + * + * \param types_mask The types to enable + * \return PROS_SUCCESS if the operation was successful or PROS_ERR if the operation + * failed, setting errno. + */ + template + requires((std::conjunction_v...>)) + int32_t disable_detection_types(Flags... flags) { + auto types_mask = (static_cast(flags) | ...); + return c::aivision_disable_detection_types(this->_port, static_cast(types_mask)); + } + + /** + * Sets the april tag family to detect. + * If override is true, the AI vision sensor will only look for the given family. + * Otherwise, it will add the given tag to the list of enabled tags. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \code + * #define AIVISION_PORT 1 + * void initialize() { + * pros::AIVision aivision(AIVISION_PORT); + * // set the only tag family to look for to 21H7 + * aivision.set_tag_family(AivisionTagFamily::tag_21H7); + * // add 16H5 to the list of enabled tag families + * aivision.set_tag_family(AivisionTagFamily::tag_16H5); + * // set the only tag family to look for to 25H9 + * aivision.set_tag_family(AivisionTagFamily::tag_25H9, true); + * } + * \endcode + * + * \param family the tag family to configure the AI Vision sensor to detect + * \param override if true, the given family will be set as the only enabled tag family. + * \return PROS_SUCCESS if the operation was successful or PROS_ERR if the operation + * failed, setting errno. + */ + int32_t set_tag_family(AivisionTagFamily family, bool override = false); + + /** + * Set a color configuration that the AI vision sensor will detect. + * The color detection type must be separately enabled. + * If a color with the same ID already is stored in the sensor, it will be overwritten. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \code + * #define AIVISION_PORT 1 + * void opcontrol() { + * pros::AIVision aivision(AIVISION_PORT); + * AIVision::Color color = {1, 207, 19, 25, 10.00, 0.20}; + * aivision.set_color(color); + * } + * \endcode + * + * \param color the color to configure the AI Vision sensor to detect + * \return PROS_SUCCESS if the operation was successful or PROS_ERR if the operation + * failed, setting errno + */ + int32_t set_color(const Color& color); + + /** + * Get a color configuration that the AI vision sensor has stored. + * If you attempt to get a color configuration that has not been previously used, the + * behavior is not defined. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * + * #define AIVISION_PORT 1 + * void opcontrol() { + * pros::AIVision aivision(AIVISION_PORT); + * AIVision::Code color = aivision.get_color(0); + * printf("id: %d, red: %d, green: %d, blue: %d, hue_range: %f, saturation_range: %f\n", + * color.id, color.red, color.green, color.blue, color.hue_range, color.saturation_range); + * } + * + * \param id the id of color from 1-7 + * \return PROS_SUCCESS if the operation was successful or PROS_ERR if the operation + * failed, setting errno + */ + AIVision::Color get_color(uint32_t id); + + /** + * Set a code that the AI vision sensor will detect for. + * The id of the code is stored in the aivision_code_s_t struct. If there is already a code + * stored in the AI vision sensor with the id, this function will overwrite. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \code + * + * #define AIVISION_PORT 1 + * void opcontrol() { + * pros::AIVision aivision(AIVISION_PORT); + * AIVision::Code code = {1, 207, 19, 25, 10.00, 0.20}; + * aivision.set_code(code); + * } + * + * \endcode + * + * \param code The code to set + * \return PROS_SUCCESS if the operation was successful or PROS_ERR if the operation + * failed, setting errno + */ + uint32_t set_code(const Code& code); + + /** + * Get a code that the AI vision sensor has stored. + * + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \code + * + * #define AIVISION_PORT 1 + * void opcontrol() { + * pros::AIVision aivision(AIVISION_PORT); + * AIVision::Code code = aivision.get_code(0); + * printf("id: %d, length: %d, c1: %d, c2: %d, c3: %d, c4: %d, c5: %d\n", + * code.id, code.length, code.c1, code.c2, code.c3, code.c4, code.c5); + * ) + * } + * + * \endcode + * + * \param id The id from 1-5 + * \return the code, or a struct with an invalid ID if the operation failed, setting errno + */ + AIVision::Code get_code(uint32_t id); + + /** + * Runs auto white balance to adjust to different lighting conditions. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \return PROS_SUCCESS if the operation was successful or PROS_ERR if the operation + * failed, setting errno + */ + int32_t start_awb(); + + /** + * Get a class name that the AI vision sensor has stored. + * The AI Vision sensor may not correctly report classnames for the first several hundred milliseconds + * of being plugged in. + * By passing in -1 for the id, the function will return the number of class names the AI vision sensor reports. + * For other values of id, the function return value is undefined + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * \code + * #define AIVISION_PORT 1 + * void opcontrol() { + * pros::AIVision aivision(AIVISION_PORT); + * char* class_name = new char[21]; + * aivision.get_class_name(0, class_name); + * printf("%s\n", class_name); + * delete[] class_name; + * } + * + * \endcode + * + * \param id the id of the class name from 0-(AIVISION_MAX_CLASSNAME_COUNT - 1) + * \param class_name a string of length >=20 to store the classname. + * \return PROS_SUCCESS if the operation was successful or PROS_ERR if the operation + * failed, setting errno + */ + int32_t get_class_name(int32_t id, char* class_name); + + /** + * Get a class name that the AI vision sensor has stored. + * The AI Vision sensor may not correctly report classnames for the first several hundred milliseconds + * of being plugged in. + * By passing in -1 for the id, the function will return the number of class names the AI vision sensor reports. + * For other values of id, the function return value is undefined + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * \code + * #define AIVISION_PORT 1 + * void opcontrol() { + * pros::AIVision aivision(AIVISION_PORT); + * auto name = aivision.get_class_name(1); + * + * if(name.has_value()) { + * printf("Class name: %s\n", name.value().c_str()); + * } else { + * printf("Error: %ld\n", errno); + * } + * } + * + * \endcode + * + * \param id the id of the class name from 0-(AIVISION_MAX_CLASSNAME_COUNT - 1) + * \return the class name string in std::optional if the operation was successful + * or an empty optional if the operation failed, setting errno + */ + std::optional get_class_name(int32_t id); + + /** + * Get the current number of objects detected by the AI vision sensor. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \code + * #define AIVISION_PORT 1 + * void opcontrol() { + * pros::AIVision aivision(AIVISION_PORT); + * int32_t object_count = aivision.get_object_count(); + * printf("%d\n", object_count); + * } + * \endcode + * + * \return the number of objects if the operation was successful or PROS_ERR if the operation failed, setting errno + */ + int32_t get_object_count(); + + /** + * Get the detected object at a given object index; there are aivision_get_object_count objects and the index starts + * from 0. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \code + * #define AIVISION_PORT 1 + * void opcontrol() { + * pros::AIVision aivision(AIVISION_PORT); + * int32_t object_count = aivision.get_object_count(); + * for (int i = 0; i < object_count; i++) { + * pros::AIVision::Object object = aivision.get_object(i); + * printf("Object %d: %d\n", i, object.type); + * } + * } + * + * \endcode + * + * @param object_index the object index + * @return the detected object if the operation was successful or an invalid object type if the operation failed, + * setting errno + */ + Object get_object(uint32_t object_index); + + /** + * Get all detected objects in a vector. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as a vision sensor + * + * \code + * #define AIVISION_PORT 1 + * void opcontrol() { + * pros::AIVision aivision(AIVISION_PORT); + * auto objects = aivision.get_all_objects(); + * for (const auto& object : objects) { + * printf("Object %d: %d\n", object.id, object.type); + * } + * } + * \endcode + * + * @return a vector of all detected objects + */ + std::vector get_all_objects(); + /// @} +}; +} // namespace v5 +} // namespace pros +#endif // _PROS_VISION_HPP_ diff --git a/include/pros/apix.h b/include/pros/apix.h index 165c0541..dc6c5b00 100644 --- a/include/pros/apix.h +++ b/include/pros/apix.h @@ -11,7 +11,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * All rights reserved. * * This Source Code Form is subject to the terms of the Mozilla Public @@ -101,86 +101,6 @@ bool task_abort_delay(task_t task); void task_notify_when_deleting(task_t target_task, task_t task_to_notify, uint32_t value, notify_action_e_t notify_action); -/** - * Creates a recursive mutex which can be locked recursively by the owner. - * - * \return A newly created recursive mutex. - * - * \b Example: - * \code - * mutex_t mutex = mutex_recursive_create(); - * - * void task_fn(void* param) { - * while(1) { - * mutex_recursive_take(mutex, 1000); - * // critical section - * mutex_recursive_give(mutex); - * task_delay(1000); - * } - * } - * task_create(task_fn, (void*)"PROS", TASK_PRIORITY_DEFAULT, - * TASK_STACK_DEPTH_DEFAULT, "task_fn"); - * - * \endcode - */ -mutex_t mutex_recursive_create(void); - -/** - * Takes a recursive mutex. - * - * \param mutex - * A mutex handle created by mutex_recursive_create - * \param wait_time - * Amount of time to wait before timing out - * - * \return 1 if the mutex was obtained, 0 otherwise - * - * \b Example: - * \code - * mutex_t mutex = mutex_recursive_create(); - * - * void task_fn(void* param) { - * while(1) { - * mutex_recursive_take(mutex, 1000); - * // critical section - * mutex_recursive_give(mutex); - * task_delay(1000); - * } - * } - * task_create(task_fn, (void*)"PROS", TASK_PRIORITY_DEFAULT, - * TASK_STACK_DEPTH_DEFAULT, "task_fn"); - * - * \endcode - */ -bool mutex_recursive_take(mutex_t mutex, uint32_t timeout); - -/** - * Gives a recursive mutex. - * - * \param mutex - * A mutex handle created by mutex_recursive_create - * - * \return 1 if the mutex was obtained, 0 otherwise - * - * \b Example: - * \code - * mutex_t mutex = mutex_recursive_create(); - * - * void task_fn(void* param) { - * while(1) { - * mutex_recursive_take(mutex, 1000); - * // critical section - * mutex_recursive_give(mutex); - * task_delay(1000); - * } - * } - * task_create(task_fn, (void*)"PROS", TASK_PRIORITY_DEFAULT, - * TASK_STACK_DEPTH_DEFAULT, "task_fn"); - * - * \endcode - */ -bool mutex_recursive_give(mutex_t mutex); - /** * Returns a handle to the current owner of a mutex. * @@ -741,6 +661,48 @@ v5_device_e_t registry_get_plugged_type(uint8_t port); ///@} +/// \name Startup options +///@{ + +/** + * Enable/disable the PROS banner printed to the serial stream. + * + * \warning This function must be called BEFORE the PROS daemon starts. + * The easiest way to acheive this is to NOT call this function directly, + * and instead use the BANNER_ENABLE macro. + * + * \param enable + * Whether the banner should be enabled or disabled. + */ +void enable_banner(bool enabled); + +/** + * This priority value, when used with __attribute__((constructor( ))), is + * guaranteed to run before PROS initializes. + */ +#define PRE_PROS_INIT_PRIORITY 101 + +/** + * Enable/disable the PROS banner printed to the serial stream. + * + * \warning This macro must be used in global scope, outside of any function. + * + * \param enable + * Whether the banner should be enabled or disabled. + */ +#ifdef __cplusplus +#define ENABLE_BANNER(enabled) static_assert(!__builtin_strcmp(__FUNCTION__, "top level"), \ + "Cannot use ENABLE_BANNER inside a function!"); \ + __attribute__((constructor(PRE_PROS_INIT_PRIORITY))) static void _enable_banner_impl() \ + { pros::c::enable_banner(enabled); } +#else +#define ENABLE_BANNER(enabled) static_assert(!__builtin_strcmp(__FUNCTION__, "top level"), \ + "Cannot use ENABLE_BANNER inside a function!"); \ + __attribute__((constructor(PRE_PROS_INIT_PRIORITY))) static void _enable_banner_impl() \ + { enable_banner(enabled); } +#endif +///@} + /// \name Filesystem ///@{ diff --git a/include/pros/colors.h b/include/pros/colors.h index b70a8f99..431d9d85 100644 --- a/include/pros/colors.h +++ b/include/pros/colors.h @@ -6,7 +6,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * Copyright (c) 2017-2020 Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License v. 2.0. If a copy of the MPL was not distributed with this diff --git a/include/pros/colors.hpp b/include/pros/colors.hpp index 970c2cfa..c19ec6e6 100644 --- a/include/pros/colors.hpp +++ b/include/pros/colors.hpp @@ -6,7 +6,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * Copyright (c) 2017-2022 Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License v. 2.0. If a copy of the MPL was not distributed with this diff --git a/include/pros/device.h b/include/pros/device.h index 889e0085..c6fb5bfb 100644 --- a/include/pros/device.h +++ b/include/pros/device.h @@ -8,7 +8,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2021, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this @@ -56,6 +56,7 @@ typedef enum v5_device_e { E_DEVICE_ADI = 12, ///< This port is an ADI expander E_DEVICE_OPTICAL = 16, ///< An optical sensor is plugged into the port E_DEVICE_GPS = 20, ///< A GPS sensor is plugged into the port + E_DEVICE_AIVISION = 29, ///< An AI Vision sensor is plugged into the port E_DEVICE_SERIAL = 129, ///< A serial device is plugged into the port E_DEVICE_GENERIC __attribute__((deprecated("use E_DEVICE_SERIAL instead"))) = E_DEVICE_SERIAL, E_DEVICE_UNDEFINED = 255 ///< The device type is not defined, or is not a valid device diff --git a/include/pros/device.hpp b/include/pros/device.hpp index 6c21e0c1..560e03fd 100644 --- a/include/pros/device.hpp +++ b/include/pros/device.hpp @@ -6,7 +6,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2021, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this @@ -51,6 +51,7 @@ enum class DeviceType { adi = 12, ///< This port is an ADI expander optical = 16, ///< An optical sensor is plugged into the port gps = 20, ///< A GPS sensor is plugged into the port + aivision = 29, ///< An AI vision sensor is plugged into the port serial = 129, ///< A serial device is plugged into the port undefined = 255 ///< The device type is not defined, or is not a valid device }; @@ -137,8 +138,48 @@ class Device { */ pros::DeviceType get_plugged_type() const; + + /** + * Gets the type of device on a given port. + * + * This function uses the following values of errno when an error state is + * reached: + * EACCES - Mutex of port cannot be taken (access denied). + * + * \param port The V5 port number from 1-21 + * + * \return The device type as an enum. + * + * \b Example + * \code + * #define DEVICE_PORT 1 + * + * void opcontrol() { + * while (true) { + * DeviceType dt = pros::Device::get_plugged_type(DEVICE_PORT); + * printf("device plugged type: {plugged type: %d}\n", dt); + * delay(20); + * } + * } + * \endcode + */ static pros::DeviceType get_plugged_type(std::uint8_t port); + /** + * Gets all devices of a given device type. + * + * \param device_type The pros::DeviceType enum that matches the type of device desired. + * + * \return A vector of Device objects for the given device type. + * + * \b Example + * \code + * void opcontrol() { + * std::vector motor_devices = pros::Device::get_all_devices(pros::DeviceType::motor); // All Device objects are motors + * } + * \endcode + */ + static std::vector get_all_devices(pros::DeviceType device_type = pros::DeviceType::undefined); protected: diff --git a/include/pros/distance.h b/include/pros/distance.h index 63bcd417..e64f0d90 100644 --- a/include/pros/distance.h +++ b/include/pros/distance.h @@ -7,7 +7,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this diff --git a/include/pros/distance.hpp b/include/pros/distance.hpp index f1b866e6..17df3d1e 100644 --- a/include/pros/distance.hpp +++ b/include/pros/distance.hpp @@ -7,12 +7,12 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * + * * \defgroup cpp-distance VEX Distance Sensor C++ API */ @@ -34,7 +34,7 @@ class Distance : public Device { /** * \addtogroup cpp-distance * @{ - */ + */ public: /** * Creates a Distance Sensor object for the given port. @@ -58,9 +58,7 @@ class Distance : public Device { */ Distance(const std::uint8_t port); - Distance(const Device& device) - : Distance(device.get_port()) {}; - + Distance(const Device& device) : Distance(device.get_port()){}; /** * Get the currently measured distance from the sensor in mm * @@ -75,11 +73,11 @@ class Distance : public Device { * \b Example * \code * #define DISTANCE_PORT 1 - * + * * void opcontrol() { - Distance distance(DISTANCE_PORT); + Distance distance(DISTANCE_PORT); * while (true) { - * printf("Distance confidence: %d\n", distance.get()); + * printf("Distance: %d\n", distance.get()); * delay(20); * } * } @@ -87,8 +85,6 @@ class Distance : public Device { */ virtual std::int32_t get(); - static std::vector get_all_devices(); - /** * Get the currently measured distance from the sensor in mm. * \note This function is identical to get(). @@ -104,11 +100,11 @@ class Distance : public Device { * \b Example * \code * #define DISTANCE_PORT 1 - * + * * void opcontrol() { - Distance distance(DISTANCE_PORT); + Distance distance(DISTANCE_PORT); * while (true) { - * printf("Distance confidence: %d\n", distance.get_distance()); + * printf("Distance: %d\n", distance.get_distance()); * delay(20); * } * } @@ -116,6 +112,21 @@ class Distance : public Device { */ virtual std::int32_t get_distance(); + /** + * Gets all distance sensors. + * + * \return A vector of Distance sensor objects. + * + * \b Example + * \code + * void opcontrol() { + * std::vector distance_all = pros::Distance::get_all_devices(); // All distance sensors that are + * connected + * } + * \endcode + */ + static std::vector get_all_devices(); + /** * Get the confidence in the distance reading * @@ -134,9 +145,9 @@ class Distance : public Device { * \b Example * \code * #define DISTANCE_PORT 1 - * + * * void opcontrol() { - Distance distance(DISTANCE_PORT); + Distance distance(DISTANCE_PORT); * while (true) { * printf("Distance confidence: %d\n", distance.get_confidence()); * delay(20); @@ -164,11 +175,11 @@ class Distance : public Device { * \b Example * \code * #define DISTANCE_PORT 1 - * + * * void opcontrol() { - Distance distance(DISTANCE_PORT); + Distance distance(DISTANCE_PORT); * while (true) { - * printf("Distance confidence: %d\n", distance.get_object_size()); + * printf("Distance object size: %d\n", distance.get_object_size()); * delay(20); * } * } @@ -186,14 +197,14 @@ class Distance : public Device { * * \return The velocity value or PROS_ERR if the operation failed, setting * errno. - * + * * \b Example * \code - * + * * void opcontrol() { * Distance distance(DISTANCE_PORT); * while (true) { - * printf("Distance Object velocity: %f\n", distance.get_object_velocity()); + * printf("Distance object velocity: %f\n", distance.get_object_velocity()); * delay(20); * } * } @@ -201,23 +212,36 @@ class Distance : public Device { */ virtual double get_object_velocity(); - /** - * This is the overload for the << operator for printing to streams - * - * Prints in format(this below is all in one line with no new line): - * Distance [port: (port number), distance: (distance), confidence: (confidence), - * object size: (object size), object velocity: (object velocity)] - */ + /** + * This is the overload for the << operator for printing to streams + * + * Prints in format(this below is all in one line with no new line): + * Distance [port: (port number), distance: (distance), confidence: (confidence), + * object size: (object size), object velocity: (object velocity)] + */ friend std::ostream& operator<<(std::ostream& os, pros::Distance& distance); - + private: ///@} }; namespace literals { +/** + * Constructs a Distance sensor object from a literal ending in _dist via calling the constructor + * + * \return a pros::Distance for the corresponding port + * + * \b Example + * \code + * using namespace pros::literals; + * void opcontrol() { + * pros::Distance dist = 2_dist; //Makes an dist object on port 2 + * } + * \endcode + */ const pros::Distance operator"" _dist(const unsigned long long int d); } // namespace literals -} +} // namespace v5 } // namespace pros #endif diff --git a/include/pros/error.h b/include/pros/error.h index 17dd6d3d..e1d03b6b 100644 --- a/include/pros/error.h +++ b/include/pros/error.h @@ -6,7 +6,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright Copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright Copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this diff --git a/include/pros/ext_adi.h b/include/pros/ext_adi.h index 421b1938..18fde003 100644 --- a/include/pros/ext_adi.h +++ b/include/pros/ext_adi.h @@ -7,7 +7,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this diff --git a/include/pros/gps.h b/include/pros/gps.h index c4d4fdfe..5cc5eef3 100644 --- a/include/pros/gps.h +++ b/include/pros/gps.h @@ -7,7 +7,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this diff --git a/include/pros/gps.hpp b/include/pros/gps.hpp index 1d5a0015..b9fc7b5a 100644 --- a/include/pros/gps.hpp +++ b/include/pros/gps.hpp @@ -7,7 +7,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this @@ -223,6 +223,19 @@ class Gps : public Device { */ virtual std::int32_t set_offset(double xOffset, double yOffset) const; + /** + + * Gets all GPS sensors. + * + * \return A vector of Gps sensor objects. + * + * \b Example + * \code + * void opcontrol() { + * std::vector gps_all = pros::Gps::get_all_devices(); // All GPS sensors that are connected + * } + * \endcode + */ static std::vector get_all_devices(); /** diff --git a/include/pros/imu.h b/include/pros/imu.h index 63cc7384..00ed1647 100644 --- a/include/pros/imu.h +++ b/include/pros/imu.h @@ -7,7 +7,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this diff --git a/include/pros/imu.hpp b/include/pros/imu.hpp index 0cee565c..4163d857 100644 --- a/include/pros/imu.hpp +++ b/include/pros/imu.hpp @@ -7,7 +7,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this @@ -55,7 +55,7 @@ inline namespace v5 { class Imu : public Device { /** * \addtogroup cpp-imu - * ///@{ + * @{ */ public: @@ -83,8 +83,8 @@ class Imu : public Device { * \endcode */ - Imu(const std::uint8_t port) : Device(port, DeviceType::imu) {}; + Imu(const std::uint8_t port) : Device(port, DeviceType::imu){}; Imu(const Device& device) : Imu(device.get_port()){}; @@ -136,7 +136,7 @@ class Imu : public Device { * * void opcontrol() { * pros::Imu imu(IMU_PORT); - * imu.calibrate(); + * * // Block until calibration is complete * imu.reset(true); * } @@ -190,6 +190,20 @@ class Imu : public Device { */ virtual std::int32_t set_data_rate(std::uint32_t rate) const; + + /** + * Gets all IMU sensors. + * + * \return A vector of Imu sensor objects. + * + * \b Example + * \code + * void opcontrol() { + * std::vector imu_all = pros::Imu::get_all_devices(); // All IMU sensors that are connected + * } + * \endcode + */ + static std::vector get_all_devices(); /** @@ -997,7 +1011,7 @@ class Imu : public Device { * * while (true) { * // Calibrate the sensor - * imu.calibrate(); + * imu.reset(); * delay(20); * * // Check if the sensor is calibrating @@ -1040,6 +1054,19 @@ class Imu : public Device { }; namespace literals { +/** + * Constructs a Imu from a literal ending in _imu via calling the constructor + * + * \return a pros::Imu for the corresponding port + * + * \b Example + * \code + * using namespace pros::literals; + * void opcontrol() { + * pros::Imu imu = 2_imu; //Makes an IMU object on port 2 + * } + * \endcode + */ const pros::Imu operator"" _imu(const unsigned long long int i); } // namespace literals diff --git a/include/pros/link.h b/include/pros/link.h index 9cd6eead..e4a1472c 100644 --- a/include/pros/link.h +++ b/include/pros/link.h @@ -7,7 +7,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this diff --git a/include/pros/link.hpp b/include/pros/link.hpp index 68bb5e51..995d433b 100644 --- a/include/pros/link.hpp +++ b/include/pros/link.hpp @@ -7,7 +7,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * Copyright (c) 2017-2021, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this diff --git a/include/pros/llemu.h b/include/pros/llemu.h index 868cd736..1f3ee4a3 100644 --- a/include/pros/llemu.h +++ b/include/pros/llemu.h @@ -3,6 +3,7 @@ // TODO:? Should there be weak symbols for the C api in here as well? +#include "stdbool.h" #include "stdint.h" /******************************************************************************/ @@ -41,7 +42,7 @@ namespace c { * \return True if the operation was successful, or false otherwise, setting * errno values as specified above. */ -bool __attribute__((weak)) lcd_print(int16_t line, const char* fmt, ...) { +bool __attribute__((weak)) lcd_print(__attribute__((unused)) int16_t line, __attribute__((unused)) const char* fmt, ...) { return false; } diff --git a/include/pros/llemu.hpp b/include/pros/llemu.hpp index 4f75d6c7..6e860176 100644 --- a/include/pros/llemu.hpp +++ b/include/pros/llemu.hpp @@ -11,7 +11,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this @@ -47,7 +47,11 @@ namespace pros { /** * \ingroup cpp-llemu */ +#if defined(_PROS_KERNEL_SUPPRESS_LLEMU_WARNING) || defined(_PROS_INCLUDE_LIBLVGL_LLEMU_HPP) namespace lcd { +#else +namespace [[deprecated("Without liblvgl, LLEMU functions will not display anything. To install liblvgl run \"pros c install liblvgl\" in the PROS terminal.")]] lcd { +#endif #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-function" namespace { @@ -70,14 +74,19 @@ namespace lcd { * For documentation on these functions, please see the doxygen comments for * these functions in the libvgl llemu headers. */ - + extern __attribute__((weak)) bool is_initialized(void); + extern __attribute__((weak)) bool initialize(void); + extern __attribute__((weak)) bool shutdown(void); extern __attribute__((weak)) bool set_text(std::int16_t line, std::string text); + extern __attribute__((weak)) bool clear(void); extern __attribute__((weak)) bool clear_line(std::int16_t line); - extern __attribute__((weak)) bool initialize(void); - extern __attribute__((weak)) std::uint8_t read_buttons(void); + // TODO: Text_Align is defined in liblvgl so this ain't going to compile for now. + // extern __attribute__((weak)) void set_text_align(Text_Align text_align); + extern __attribute__((weak)) void register_btn0_cb(lcd_btn_cb_fn_t cb); extern __attribute__((weak)) void register_btn1_cb(lcd_btn_cb_fn_t cb); - extern __attribute__((weak)) bool is_initialized(void); - + extern __attribute__((weak)) void register_btn2_cb(lcd_btn_cb_fn_t cb); + extern __attribute__((weak)) std::uint8_t read_buttons(void); + /** * \addtogroup cpp-llemu * @{ diff --git a/include/pros/misc.h b/include/pros/misc.h index 677dad81..8123fbfd 100644 --- a/include/pros/misc.h +++ b/include/pros/misc.h @@ -8,7 +8,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * All rights reservered. * * This Source Code Form is subject to the terms of the Mozilla Public @@ -223,7 +223,9 @@ typedef enum { /// The ‘Y’ button on the right button pad of the controller. E_CONTROLLER_DIGITAL_Y, /// The ‘A’ button on the right button pad of the controller. - E_CONTROLLER_DIGITAL_A + E_CONTROLLER_DIGITAL_A, + /// The power button on the front of the controller. + E_CONTROLLER_DIGITAL_POWER } controller_digital_e_t; #ifdef PROS_USE_SIMPLE_NAMES @@ -246,6 +248,7 @@ typedef enum { #define DIGITAL_B pros::E_CONTROLLER_DIGITAL_B #define DIGITAL_Y pros::E_CONTROLLER_DIGITAL_Y #define DIGITAL_A pros::E_CONTROLLER_DIGITAL_A +#define DIGITAL_POWER pros::E_CONTROLLER_DIGITAL_POWER #else #define CONTROLLER_MASTER E_CONTROLLER_MASTER #define CONTROLLER_PARTNER E_CONTROLLER_PARTNER @@ -481,6 +484,45 @@ int32_t controller_get_digital(controller_id_e_t id, controller_digital_e_t butt */ int32_t controller_get_digital_new_press(controller_id_e_t id, controller_digital_e_t button); +/** + * Returns a falling-edge case for a controller button press. + * + * This function is not thread-safe. + * Multiple tasks polling a single button may return different results under + * the same circumstances, so only one task should call this function for any + * given button. E.g., Task A calls this function for buttons 1 and 2. + * Task B may call this function for button 3, but should not for buttons + * 1 or 2. A typical use-case for this function is to call inside opcontrol + * to detect new button releases, and not in any other tasks. + * + * This function uses the following values of errno when an error state is + * reached: + * EACCES - Another resource is currently trying to access the controller + * port. + * + * \param button + * The button to read. Must be one of + * DIGITAL_{RIGHT,DOWN,LEFT,UP,A,B,Y,X,R1,R2,L1,L2} + * + * \return 1 if the button on the controller is not pressed and had been + * pressed the last time this function was called, 0 otherwise. + * + * \b Example + * \code + * void opcontrol() { + * pros::Controller master(pros::E_CONTROLLER_MASTER); + * while (true) { + * if (master.get_digital_new_release(pros::E_CONTROLLER_DIGITAL_A)) { + * // Toggle pneumatics or other similar actions + * } + * + * delay(2); + * } + * } + * \endcode + */ +int32_t controller_get_digital_new_release(controller_id_e_t id, controller_digital_e_t button); + /** * Sets text to the controller LCD screen. * diff --git a/include/pros/misc.hpp b/include/pros/misc.hpp index 25ad31a1..b0a115e4 100644 --- a/include/pros/misc.hpp +++ b/include/pros/misc.hpp @@ -8,13 +8,13 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * All rights reservered. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * + * * \defgroup cpp-misc Miscellaneous C++ API * \note Additional example code for this module can be found in its [Tutorial.](@ref controller) */ @@ -22,11 +22,11 @@ #ifndef _PROS_MISC_HPP_ #define _PROS_MISC_HPP_ -#include "pros/misc.h" - #include #include +#include "pros/misc.h" + namespace pros { inline namespace v5 { /** @@ -56,13 +56,13 @@ class Controller { * port. * * \return 1 if the controller is connected, 0 otherwise - * + * * \b Example * \code * void status_display_controller(){ * pros::Controller master(pros::E_CONTROLLER_MASTER); * if(!master.is_connected()) { - * pros::lcd::print(0, "Main controller is not connected!"); + * pros::lcd::print(0, "Main controller is not connected!"); * } * } * \endcode @@ -84,7 +84,7 @@ class Controller { * * \return The current reading of the analog channel: [-127, 127]. * If the controller was not connected, then 0 is returned - * + * * \b Example * \code * void opcontrol() { @@ -107,7 +107,7 @@ class Controller { * port. * * \return The controller's battery capacity - * + * * \b Example * \code * void initialize() { @@ -127,7 +127,7 @@ class Controller { * port. * * \return The controller's battery level - * + * * \b Example * \code * void initialize() { @@ -153,7 +153,7 @@ class Controller { * * \return 1 if the button on the controller is pressed. * If the controller was not connected, then 0 is returned - * + * * \b Example * \code * void opcontrol() { @@ -194,7 +194,7 @@ class Controller { * * \return 1 if the button on the controller is pressed and had not been * pressed the last time this function was called, 0 otherwise. - * + * * \b Example * \code * void opcontrol() { @@ -203,7 +203,7 @@ class Controller { * if (master.get_digital_new_press(pros::E_CONTROLLER_DIGITAL_A)) { * // Toggle pneumatics or other similar actions * } - * + * * delay(2); * } * } @@ -211,6 +211,45 @@ class Controller { */ std::int32_t get_digital_new_press(controller_digital_e_t button); + /** + * Returns a falling-edge case for a controller button press. + * + * This function is not thread-safe. + * Multiple tasks polling a single button may return different results under + * the same circumstances, so only one task should call this function for any + * given button. E.g., Task A calls this function for buttons 1 and 2. + * Task B may call this function for button 3, but should not for buttons + * 1 or 2. A typical use-case for this function is to call inside opcontrol + * to detect new button releases, and not in any other tasks. + * + * This function uses the following values of errno when an error state is + * reached: + * EACCES - Another resource is currently trying to access the controller + * port. + * + * \param button + * The button to read. Must be one of + * DIGITAL_{RIGHT,DOWN,LEFT,UP,A,B,Y,X,R1,R2,L1,L2} + * + * \return 1 if the button on the controller is not pressed and had been + * pressed the last time this function was called, 0 otherwise. + * + * \b Example + * \code + * void opcontrol() { + * pros::Controller master(pros::E_CONTROLLER_MASTER); + * while (true) { + * if (master.get_digital_new_release(pros::E_CONTROLLER_DIGITAL_A)) { + * // Toggle pneumatics or other similar actions + * } + * + * delay(2); + * } + * } + * \endcode + */ + std::int32_t get_digital_new_release(controller_digital_e_t button); + #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-function" template @@ -244,7 +283,7 @@ class Controller { * * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. - * + * * \b Example * \code * void opcontrol() { @@ -286,22 +325,22 @@ class Controller { * * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. - * + * * \b Example - * \code - * void opcontrol() { - * int count = 0; + * \code + * void opcontrol() { + * int count = 0; * pros::Controller master(pros::E_CONTROLLER_MASTER); - * while (true) { - * if (!(count % 25)) { - * // Only print every 50ms, the controller text update rate is slow - * master.set_text(0, 0, "Example text"); - * } - * count++; - * delay(2); - * } - * } - * \endcode + * while (true) { + * if (!(count % 25)) { + * // Only print every 50ms, the controller text update rate is slow + * master.set_text(0, 0, "Example text"); + * } + * count++; + * delay(2); + * } + * } + * \endcode */ std::int32_t set_text(std::uint8_t line, std::uint8_t col, const char* str); std::int32_t set_text(std::uint8_t line, std::uint8_t col, const std::string& str); @@ -322,16 +361,16 @@ class Controller { * * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. - * + * * \b Example - * \code - * void opcontrol() { - * pros::Controller master(pros::E_CONTROLLER_MASTER); - * master.set_text(0, 0, "Example"); - * delay(100); - * master.clear_line(0); - * } - * \endcode + * \code + * void opcontrol() { + * pros::Controller master(pros::E_CONTROLLER_MASTER); + * master.set_text(0, 0, "Example"); + * delay(100); + * master.clear_line(0); + * } + * \endcode */ std::int32_t clear_line(std::uint8_t line); @@ -353,7 +392,7 @@ class Controller { * * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. - * + * * \b Example * \code * void opcontrol() { @@ -386,7 +425,7 @@ class Controller { * * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. - * + * * \b Example * \code * void opcontrol() { @@ -403,7 +442,7 @@ class Controller { controller_id_e_t _id; ///@} }; -} // namespace v5 +} // namespace v5 namespace battery { /** @@ -418,7 +457,7 @@ namespace battery { * EACCES - Another resource is currently trying to access the battery port. * * \return The current voltage of the battery - * + * * \b Example * \code * void initialize() { @@ -436,7 +475,7 @@ double get_capacity(void); * EACCES - Another resource is currently trying to access the battery port. * * \return The current current of the battery - * + * * \b Example * \code * void initialize() { @@ -454,7 +493,7 @@ int32_t get_current(void); * EACCES - Another resource is currently trying to access the battery port. * * \return The current temperature of the battery - * + * * \b Example * \code * void initialize() { @@ -472,7 +511,7 @@ double get_temperature(void); * EACCES - Another resource is currently trying to access the battery port. * * \return The current capacity of the battery - * + * * \b Example * \code * void initialize() { @@ -490,7 +529,7 @@ namespace competition { * * \return The competition control status as a mask of bits with * COMPETITION_{ENABLED,AUTONOMOUS,CONNECTED}. - * + * * \b Example * \code * void status_display_task(){ @@ -518,7 +557,7 @@ namespace usd { * Checks if the SD card is installed. * * \return 1 if the SD card is installed, 0 otherwise - * + * * \b Example * \code * void opcontrol() { @@ -528,7 +567,7 @@ namespace usd { */ std::int32_t is_installed(void); /** -Lists the files in a directory specified by the path + * Lists the files in a directory specified by the path * Puts the list of file names (NOT DIRECTORIES) into the buffer seperated by newlines * * This function uses the following values of errno when an error state is @@ -541,18 +580,18 @@ Lists the files in a directory specified by the path * EINVAL - the path name format is invalid * EACCES - Access denied or directory full * EEXIST - Access denied - * EROFS - SD card is write protected + * EROFS - SD card is write protected * ENXIO - drive number is invalid or not a FAT32 drive * ENOBUFS - drive has no work area * ENFILE - too many open files - * - * - * + * + * + * * \note use a path of "\" to list the files in the main directory NOT "/usd/" * DO NOT PREPEND YOUR PATHS WITH "/usd/" - * + * * \return 1 on success or PROS_ERR on failure setting errno - * + * * \b Example * \code * void opcontrol() { @@ -567,7 +606,8 @@ Lists the files in a directory specified by the path * pros::delay(100); * } * \endcode -*/ + */ + std::int32_t list_files(const char* path, char* buffer, std::int32_t len); } // namespace usd diff --git a/include/pros/motor_group.hpp b/include/pros/motor_group.hpp index 4ff73e46..c9a4a1a0 100644 --- a/include/pros/motor_group.hpp +++ b/include/pros/motor_group.hpp @@ -10,7 +10,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this @@ -54,7 +54,7 @@ class MotorGroup : public virtual AbstractMotor { * A reversed motor will reverse the input or output movement functions and movement related * telemetry in order to produce consistant behavior with non-reversed motors * - * \param gearset = pros::v5::MotorGears::invalid + * \param gearset = pros::v5::MotorGears::invalid * Optional parameter for the gearset for the motor. * Does not explicitly set the motor gearset if it is invalid or not specified * @@ -65,7 +65,7 @@ class MotorGroup : public virtual AbstractMotor { * \b Example * \code * void opcontrol() { - * MotorGroup first_mg({1, -2}); //Creates a motor on port 1 and a reversed motor on port 2 + * MotorGroup first_mg({1, -2}); //Creates a motor on port 1 and a reversed motor on port 2 * MotorGroup rotations_mg({4, 5}, pros::v5::MotorGears::blue, pros::v5::MotorUnits::rotations); * //Creates a motor group on ports 4 and 5 with blue motors using rotaions as the encoder units * } @@ -91,7 +91,7 @@ class MotorGroup : public virtual AbstractMotor { * A reversed motor will reverse the input or output movement functions and movement related * telemetry in order to produce consistant behavior with non-reversed motors * - * \param gearset = pros::v5::MotorGears::invalid + * \param gearset = pros::v5::MotorGears::invalid * \param gearset = pros::v5::MotorGears::green * Optional parameter for the gearset for the motor. * Does not explicitly set the motor gearset if it is invalid or not specified @@ -103,7 +103,7 @@ class MotorGroup : public virtual AbstractMotor { * \b Example * \code * void opcontrol() { - * MotorGroup first_mg({1, -2}); //Creates a motor on port 1 and a reversed motor on port 2 with + * MotorGroup first_mg({1, -2}); //Creates a motor on port 1 and a reversed motor on port 2 with * with both motors using the green gearset and degrees as the encoder units * MotorGroup rotations_mg({4, 5}, pros::v5::MotorGears::blue, pros::v5::MotorUnits::rotations); * //Creates a motor group on ports 4 and 5 with blue motors using rotaions as the encoder units @@ -126,14 +126,14 @@ class MotorGroup : public virtual AbstractMotor { * EDOM - The motor group is empty * * \param abstract_motor - * THe abstract motor to turn into a motor group + * The abstract motor to turn into a motor group * Uses abstract_motor.get_port_all() to get the vector of ports * * * \b Example * \code * void opcontrol() { - * MotorGroup first_mg({1, -2}); //Creates a motor on port 1 and a reversed motor on port 2 with + * MotorGroup first_mg({1, -2}); //Creates a motor on port 1 and a reversed motor on port 2 with * with both motors using the green gearset and degrees as the encoder units * AbstractMotor abs_mtr_group = first_mg; * MotorGroup new_mg = (MotorGroup) abs_mtr_group; @@ -141,7 +141,8 @@ class MotorGroup : public virtual AbstractMotor { * \endcode */ - MotorGroup(MotorGroup& motor_group); + MotorGroup(AbstractMotor& motor_group); + /// \name Motor movement functions /// These functions allow programmers to make motors move ///@{ @@ -917,7 +918,7 @@ class MotorGroup : public virtual AbstractMotor { std::vector get_position_all(void) const; /** - * Gets the power drawn by a motor in the motor group in Watts. + * Gets the power drawn by a motor in the motor group in Watts. * * This function uses the following values of errno when an error state is * reached: @@ -1067,7 +1068,7 @@ class MotorGroup : public virtual AbstractMotor { */ double get_temperature(const std::uint8_t index = 0) const; /** - * Gets the temperature of each motor in the motor group in degrees Celsius. + * Gets the temperature of each motor in the motor group in degrees Celsius. * * This function uses the following values of errno when an error state is * reached: @@ -1431,7 +1432,7 @@ class MotorGroup : public virtual AbstractMotor { * * \param index Optional parameter, 0 by default. * The zero indexed index of the motor in the motor group - * + * * \return One of MotorUnits according to what is set for the * motor or E_MOTOR_ENCODER_INVALID if the operation failed. * @@ -1477,7 +1478,7 @@ class MotorGroup : public virtual AbstractMotor { * *\param index Optional parameter, 0 by default. * The zero indexed index of the motor in the motor group - * + * * \return One of MotorGears according to what is set for the motor, * or pros::MotorGears::invalid if the operation failed. * @@ -1498,7 +1499,7 @@ class MotorGroup : public virtual AbstractMotor { * ENODEV - The port cannot be configured as a motor * EDOM - The motor group is empty * - * + * * \return A vector with one of MotorGears according to what is set for the motor, * or pros::MotorGears::invalid if the operation failed for each motor. * @@ -1618,6 +1619,51 @@ class MotorGroup : public virtual AbstractMotor { */ std::vector is_reversed_all(void) const; + /** + * Gets the type of a motor in the motor group. + * + * This function uses the following values of errno when an error state is + * reached: + * ENODEV - The port cannot be configured as a motor + * EDOM - The motor group is empty + * EOVERFLOW - The index is greater than or equal to MotorGroup::size() + * + *\param index Optional parameter, 0 by default. + * The zero indexed index of the motor in the motor group + * + * \return One of MotorType according to the type of the motor, + * or pros::MotorType::invalid if the operation failed. + * + * \b Example + * \code + * void initialize() { + * pros::MotorGroup mg ({1,3}, E_MOTOR_GEARSET_06, false, E_MOTOR_ENCODER_COUNTS); + * std::cout << "Motor Type: " << mg.get_type(); + * } + * \endcode + */ + MotorType get_type(const std::uint8_t index = 0) const; + /** + * Gets a vector of the type of each motor. + * + * This function uses the following values of errno when an error state is + * reached: + * ENODEV - The port cannot be configured as a motor + * EDOM - The motor group is empty + * + * \return A vector with one of MotorType according to the type of the motor, + * or pros::MotorType::invalid if the operation failed for each motor. + * + * \b Example + * \code + * void initialize() { + * pros::MotorGroup mg ({1,3}, E_MOTOR_GEARSET_06, false, E_MOTOR_ENCODER_COUNTS); + * std::cout << "Motor Type: " << mg.get_type_all()[0]; + * } + * \endcode + */ + std::vector get_type_all(void) const; + /** * Sets one of MotorBrake to a motor in the motor group. Works with the C enum * and the C++ enum class. @@ -1630,7 +1676,7 @@ class MotorGroup : public virtual AbstractMotor { * * \param mode * The MotorBrake to set for the motor - * + * * \param index Optional parameter, 0 by default. * The zero indexed index of the motor in the motor group * @@ -1660,7 +1706,7 @@ class MotorGroup : public virtual AbstractMotor { * * \param mode * The MotorBrake to set for the motor - * + * * \param index Optional parameter, 0 by default. * The zero indexed index of the motor in the motor group * @@ -1689,7 +1735,7 @@ class MotorGroup : public virtual AbstractMotor { * * \param mode * The MotorBrake to set for the motor - * + * * * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. @@ -1998,6 +2044,7 @@ class MotorGroup : public virtual AbstractMotor { * \endcode */ std::int32_t set_gearing(std::vector gearsets) const; + /** * Sets one of the gear cartridge (red, green, blue) for one motor in the motor group. Usable with * the C++ enum class and the C enum. @@ -2229,7 +2276,7 @@ class MotorGroup : public virtual AbstractMotor { * mg.move_absolute(100, 100); // This does not cause a movement * mg.set_zero_position(80); * mg.set_zero_position(80, 1); - * mg.move_absolute(100, 100); // Moves 80 units forward + * mg.move_absolute(100, 100); // Moves 20 units forward * } * \endcode * @@ -2260,7 +2307,7 @@ class MotorGroup : public virtual AbstractMotor { * mg.move_absolute(100, 100); // This does not cause a movement * * mg.set_zero_position_all(80); - * mg.move_absolute(100, 100); // Moves 80 units forward + * mg.move_absolute(100, 100); // Moves 20 units forward * } * \endcode * @@ -2329,9 +2376,9 @@ class MotorGroup : public virtual AbstractMotor { std::int8_t size(void) const; /** - * Gets the port of a motor in the motor group + * Gets the port of a motor in the motor group via index * - * * \param index Optional parameter, 0 by default. + * \param index Optional parameter, 0 by default. * The zero indexed index of the motor in the motor group * * \return The port of the motor at the specified index. @@ -2345,7 +2392,7 @@ class MotorGroup : public virtual AbstractMotor { * Maintains the order of the other motor group * */ - void operator+=(MotorGroup&); + void operator+=(AbstractMotor&); /** * Appends all the motors in the other motor group reference to this motor group @@ -2353,7 +2400,7 @@ class MotorGroup : public virtual AbstractMotor { * Maintains the order of the other motor group * */ - void append(MotorGroup&); + void append(AbstractMotor&); /** * Removes the all motors on the port (regardless of reversal) from the motor group diff --git a/include/pros/motors.h b/include/pros/motors.h index acf61e7a..3c99f03b 100644 --- a/include/pros/motors.h +++ b/include/pros/motors.h @@ -7,7 +7,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this @@ -900,6 +900,16 @@ typedef enum motor_gearset_e { E_MOTOR_GEARSET_INVALID = INT32_MAX, // Error: Invalid Gearset } motor_gearset_e_t; +/** + * \enum motor_type_e_t + * Indicates the type of a motor + */ +typedef enum motor_type_e { + E_MOTOR_TYPE_V5 = 0, // 11 watt V5 motor + E_MOTOR_TYPE_EXP = 1, // 5.5 watt EXP motor + E_MOTOR_TYPE_INVALID = INT32_MAX, // Error: invalid type +} motor_type_e_t; + #ifdef PROS_USE_SIMPLE_NAMES #ifdef __cplusplus #define MOTOR_BRAKE_COAST pros::E_MOTOR_BRAKE_COAST @@ -921,6 +931,9 @@ typedef enum motor_gearset_e { #define MOTOR_GEAR_BLUE pros::E_MOTOR_GEAR_BLUE #define MOTOR_GEAR_600 pros::E_MOTOR_GEAR_600 #define MOTOR_GEARSET_INVALID pros::E_MOTOR_GEARSET_INVALID +#define MOTOR_TYPE_V5 pros::E_MOTOR_TYPE_V5 +#define MOTOR_TYPE_EXP pros::E_MOTOR_TYPE_EXP +#define MOTOR_TYPE_INVALID pros::E_MOTOR_TYPE_INVALID #else #define MOTOR_BRAKE_COAST E_MOTOR_BRAKE_COAST #define MOTOR_BRAKE_BRAKE E_MOTOR_BRAKE_BRAKE @@ -941,6 +954,9 @@ typedef enum motor_gearset_e { #define MOTOR_GEAR_BLUE E_MOTOR_GEAR_BLUE #define MOTOR_GEAR_600 E_MOTOR_GEAR_600 #define MOTOR_GEARSET_INVALID E_MOTOR_GEARSET_INVALID +#define MOTOR_TYPE_V5 E_MOTOR_TYPE_V5 +#define MOTOR_TYPE_EXP E_MOTOR_TYPE_EXP +#define MOTOR_TYPE_INVALID E_MOTOR_TYPE_INVALID #endif #endif @@ -1027,7 +1043,7 @@ namespace c { * } * * motor_set_zero_position(1, 80); - * motor_move_absolute(1, 100, 100); // Moves 80 units forward + * motor_move_absolute(1, 100, 100); // Moves 20 units forward * while (!((motor_get_position(1) - 100 < 105) && (motor_get_position(1) - 100 > 95))) { * delay(2); * } @@ -1328,6 +1344,30 @@ motor_gearset_e_t motor_get_gearing(int8_t port); */ int32_t motor_get_voltage_limit(int8_t port); +/** + * Get the type of the motor + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports |1-21| + * ENODEV - The port cannot be configured as a motor + * + * \param port + * The V5 port number from 1 to 21, or from -21 to -1 for reversed motors + * + * \return One of motor_type_e_t according to the type of the motor, or + * E_MOTOR_TYPE_INVALID if the operation failed. + * + * \b Example + * \code + * void initialize() { + * printf("Motor Type: %d\n", motor_get_type(1)); + * // Prints the type of the motor + * } + * \endcode + */ +motor_type_e_t motor_get_type(int8_t port); + ///@} ///@} diff --git a/include/pros/motors.hpp b/include/pros/motors.hpp index 26beb484..6556aada 100644 --- a/include/pros/motors.hpp +++ b/include/pros/motors.hpp @@ -7,7 +7,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this @@ -40,42 +40,45 @@ class Motor : public AbstractMotor, public Device { /** * Constructs a new Motor object. - * + * * This function uses the following values of errno when an error state is - * reached: - * ENXIO - The given value is not within the range of V5 ports |1-21|. - * ENODEV - The port cannot be configured as a motor - * + * reached: + * ENXIO - The given value is not within the range of V5 ports |1-21|. + * ENODEV - The port cannot be configured as a motor + * * \param port - * The V5 port number from 1 to 21, or from -21 to -1 for reversed motors. + * The V5 port number from 1 to 21, or from -21 to -1 for reversed motors. * A reversed motor will reverse the input or output movement functions and movement related * telemetry in order to produce consistant behavior with non-reversed motors - * + * * \param gearset = pros::v5::MotorGears::green * Optional parameter for the gearset for the motor. * Does not explicitly set the gearset if not specified or if the gearset is invalid - * + * * \param encoder_units = pros::v5::MotorUnits::degrees * Optional parameter for the encoder units of the motor * Does not explicitly set the gearset if not specified or if the gearset is invalid - * + * * \b Example - * \code - * void opcontrol() { + * \code + * void opcontrol() { * Motor first_motor(1); //Creates a motor on port 1 without altering gearset or encoder units * Motor reversed_motor(-2); //Creates a reversed motor on port 1 port 1 without altering gearset or encoder units * Motor blue_motor(3, pros::v5::MotorGears::blue); //Creates a motor on port 3 with blue gear set - * Motor rotations_motor(4, pros::v5::MotorGears::green, pros::v5::MotorUnits::rotations); port 4 w/ rotations - * - * } - * \endcode - * + * Motor rotations_motor(4, pros::v5::MotorGears::green, pros::v5::MotorUnits::rotations); //port 4 w/ rotations + * + * } + * \endcode + * */ Motor(const std::int8_t port, const pros::v5::MotorGears gearset = pros::v5::MotorGears::invalid, - const pros::v5::MotorUnits encoder_units = pros::v5::MotorUnits::invalid); + const pros::v5::MotorUnits encoder_units = pros::v5::MotorUnits::invalid); - Motor(const Device& device) - : Motor(device.get_port()) {}; + Motor(const Device& device) : Motor(device.get_port()){}; + + /// \name Motor movement functions + /// These functions allow programmers to make motors move + ///@{ /** * Sets the voltage for the motor from -127 to 127. @@ -117,7 +120,7 @@ class Motor : public AbstractMotor, public Device { * * \note This function simply sets the target for the motor, it does not block * program execution until the movement finishes. - * + * * * This function uses the following values of errno when an error state is * reached: @@ -160,7 +163,7 @@ class Motor : public AbstractMotor, public Device { * This movement is relative to the current position of the motor as given in * pros::Motor::motor_get_position(). Providing 10.0 as the position parameter * would result in the motor moving clockwise 10 units (counter clockwise if reversed), - * no matter what the current position is. + * no matter what the current position is. * * \note This function simply sets the target for the motor, it does not block * program execution until the movement finishes. @@ -252,10 +255,10 @@ class Motor : public AbstractMotor, public Device { * \endcode */ std::int32_t move_voltage(const std::int32_t voltage) const; - + /** * Stops the motor using the currently configured brake mode. - * + * * This function sets motor velocity to zero, which will cause it to act * according to the set brake mode. If brake mode is set to MOTOR_BRAKE_HOLD, * this function may behave differently than calling move_absolute(0) @@ -264,20 +267,20 @@ class Motor : public AbstractMotor, public Device { * This function uses the following values of errno when an error state is * reached: * ENODEV - The port cannot be configured as a motor - * - * \return 1 if the operation was successful or PROS_ERR if the operation - * failed, setting errno. - * - * \b Example - * \code - * void autonomous() { - * Motor motor(1); - * motor.move_voltage(12000); - * pros::delay(1000); // Move at max voltage for 1 second - * motor.brake(); - * } - * \endcode - */ + * + * \return 1 if the operation was successful or PROS_ERR if the operation + * failed, setting errno. + * + * \b Example + * \code + * void autonomous() { + * Motor motor(1); + * motor.move_voltage(12000); + * pros::delay(1000); // Move at max voltage for 1 second + * motor.brake(); + * } + * \endcode + */ std::int32_t brake(void) const; /** @@ -295,7 +298,7 @@ class Motor : public AbstractMotor, public Device { * * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. - * + * * \b Example * \code * void autonomous() { @@ -308,19 +311,25 @@ class Motor : public AbstractMotor, public Device { */ std::int32_t modify_profiled_velocity(const std::int32_t velocity) const; + ///@} + + /// \name Motor telemetry functions + /// These functions allow programmers to collect telemetry from motors + ///@{ + /** * Gets the target position set for the motor by the user * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: * ENODEV - The port cannot be configured as a motor * EOVERFLOW - The index is non 0 - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index * @@ -342,19 +351,19 @@ class Motor : public AbstractMotor, public Device { /** * Gets the velocity commanded to the motor by the user at the index specified. * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: * ENODEV - The port cannot be configured as a motor * EOVERFLOW - The index is non 0 - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index - * + * * \return The commanded motor velocity from +-100, +-200, or +-600, or * PROS_ERR if the operation failed, setting errno. * @@ -374,28 +383,22 @@ class Motor : public AbstractMotor, public Device { */ std::int32_t get_target_velocity(const std::uint8_t index = 0) const; - ///@} - - /// \name Motor telemetry functions - /// These functions allow programmers to collect telemetry from motors - ///@{ - /** * Gets the actual velocity of the motor. * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: * ENODEV - The port cannot be configured as a motor * EOVERFLOW - The index is non 0 - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. - * By default index is 0, and will return an error for a non-zero index - * + * By default index is 0, and will return an error for a non-zero index + * * \return The motor's actual velocity in RPM or PROS_ERR_F if the operation * failed, setting errno. * @@ -415,22 +418,22 @@ class Motor : public AbstractMotor, public Device { /** * Gets the current drawn by the motor in mA. - * - * \note This is one of many Motor functions that takes in an optional index parameter. + * + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index - * + * * \return The motor's current in mA or PROS_ERR if the operation failed, * setting errno. * @@ -452,21 +455,21 @@ class Motor : public AbstractMotor, public Device { /** * Gets the direction of movement for the motor. * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index - * + * * \return 1 for moving in the positive direction, -1 for moving in the * negative direction, and PROS_ERR if the operation failed, setting errno. * @@ -492,19 +495,19 @@ class Motor : public AbstractMotor, public Device { * drawing no electrical power, and an efficiency of 0% means that the motor * is drawing power but not moving. * - * - * \note This is one of many Motor functions that takes in an optional index parameter. + * + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index * @@ -531,18 +534,18 @@ class Motor : public AbstractMotor, public Device { * * Compare this bitfield to the bitmasks in pros::motor_fault_e_t. * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index * @@ -562,24 +565,24 @@ class Motor : public AbstractMotor, public Device { * \endcode */ std::uint32_t get_faults(const std::uint8_t index = 0) const; - + /** * Gets the flags set by the motor's operation. * * Compare this bitfield to the bitmasks in pros::motor_flag_e_t. * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index * @@ -603,18 +606,18 @@ class Motor : public AbstractMotor, public Device { /** * Gets the absolute position of the motor in its encoder units. * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index * @@ -639,21 +642,21 @@ class Motor : public AbstractMotor, public Device { /** * Gets the power drawn by the motor in Watts. * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index - * + * * \return The motor's power draw in Watts or PROS_ERR_F if the operation * failed, setting errno. * @@ -675,28 +678,28 @@ class Motor : public AbstractMotor, public Device { /** * Gets the raw encoder count of the motor at a given timestamp. * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * - * + * + * * \param timestamp * A pointer to a time in milliseconds for which the encoder count * will be returned. If NULL, the timestamp at which the encoder * count was read will not be supplied - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index * - * + * * * \return The raw encoder count at the given timestamp or PROS_ERR if the * operation failed. @@ -719,19 +722,19 @@ class Motor : public AbstractMotor, public Device { /** * Gets the temperature of the motor in degrees Celsius. - - * \note This is one of many Motor functions that takes in an optional index parameter. + + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index * @@ -756,18 +759,18 @@ class Motor : public AbstractMotor, public Device { /** * Gets the torque generated by the motor in Newton Meters (Nm). * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index * @@ -792,21 +795,21 @@ class Motor : public AbstractMotor, public Device { /** * Gets the voltage delivered to the motor in millivolts. * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index - * + * * \return The motor's voltage in mV or PROS_ERR_F if the operation failed, * setting errno. * @@ -828,18 +831,18 @@ class Motor : public AbstractMotor, public Device { /** * Checks if the motor is drawing over its current limit. * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index * @@ -865,21 +868,21 @@ class Motor : public AbstractMotor, public Device { /** * Gets the temperature limit flag for the motor. * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index - * + * * \return 1 if the temperature limit is exceeded and 0 if the temperature is * below the limit, or PROS_ERR if the operation failed, setting errno. * @@ -907,18 +910,18 @@ class Motor : public AbstractMotor, public Device { /** * Gets the brake mode that was set for the motor. * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index * @@ -939,21 +942,21 @@ class Motor : public AbstractMotor, public Device { /** * Gets the current limit for the motor in mA. * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index - * + * * \return The motor's current limit in mA or PROS_ERR if the operation failed, * setting errno. * @@ -969,22 +972,22 @@ class Motor : public AbstractMotor, public Device { * \endcode */ std::int32_t get_current_limit(const std::uint8_t index = 0) const; - + /** * Gets the encoder units that were set for the motor. * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index * @@ -1004,18 +1007,18 @@ class Motor : public AbstractMotor, public Device { /** * Gets the gearset that was set for the motor. * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index * @@ -1038,18 +1041,18 @@ class Motor : public AbstractMotor, public Device { * Default value is 0V, which means that there is no software limitation * imposed on the voltage. * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index * @@ -1066,16 +1069,16 @@ class Motor : public AbstractMotor, public Device { /** * Gets whether the motor is reversed or not * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * EOVERFLOW - The index is non 0 - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index * @@ -1093,24 +1096,55 @@ class Motor : public AbstractMotor, public Device { */ std::int32_t is_reversed(const std::uint8_t index = 0) const; + /** + * Gets the type of the motor + * + * \note This is one of many Motor functions that takes in an optional index parameter. + * This parameter can be ignored by most users but exists to give a shared base class + * for motors and motor groups + * + * This function uses the following values of errno when an error state is + * reached: + * + * ENODEV - The port cannot be configured as a motor + * + * EOVERFLOW - The index is non 0 + * + * \param index Optional parameter. + * The zero-indexed index of the motor to get the type of. + * By default index is 0, and will return an error for a non-zero index + * + * \return One of MotorType according to the type of the motor, + * or pros::MotorType::invalid if the operation failed. + * + * \b Example + * \code + * void initialize() { + * pros::Motor motor (1, E_MOTOR_GEARSET_06, E_MOTOR_ENCODER_COUNTS); + * std::cout << "Motor Type: " << motor.get_type(); + * } + * \endcode + */ + MotorType get_type(const std::uint8_t index = 0) const; + /** * Sets one of Motor_Brake to the motor. - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * - * + * + * * \param mode * The MotorBrake to set for the motor - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index * @@ -1129,22 +1163,22 @@ class Motor : public AbstractMotor, public Device { std::int32_t set_brake_mode(const MotorBrake mode, const std::uint8_t index = 0) const; /** * Sets one of MotorBrake to the motor. - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * - * + * + * * \param mode * The MotorBrake to set for the motor - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index * @@ -1164,21 +1198,21 @@ class Motor : public AbstractMotor, public Device { /** * Sets the current limit for the motor in mA. * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * + * * \param limit * The new current limit in mA - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index * @@ -1230,21 +1264,21 @@ class Motor : public AbstractMotor, public Device { * Sets one of MotorUnits for the motor encoder. Works with the C * enum and the C++ enum class. * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index - * + * * * \param units * The new motor encoder units * @@ -1265,18 +1299,18 @@ class Motor : public AbstractMotor, public Device { * Sets one of the gear cartridge (red, green, blue) for the motor. Usable with * the C++ enum class and the C enum. * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index * \param gearset @@ -1295,26 +1329,26 @@ class Motor : public AbstractMotor, public Device { * \endcode */ std::int32_t set_gearing(const MotorGears gearset, const std::uint8_t index = 0) const; - + /** * Sets one of the gear cartridge (red, green, blue) for the motor. Usable with * the C++ enum class and the C enum. - - * \note This is one of many Motor functions that takes in an optional index parameter. + + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * + * * \param gearset * The new motor gearset - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index * @@ -1338,19 +1372,19 @@ class Motor : public AbstractMotor, public Device { * * This will invert its movements and the values returned for its position. * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * EOVERFLOW - The index is non 0 - * + * * \param reverse * True reverses the motor, false is default direction - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index * @@ -1404,25 +1438,25 @@ class Motor : public AbstractMotor, public Device { * This will be the future reference point for the motor's "absolute" * position. * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * + * * \param position * The new reference position in its encoder units - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index - * - * + * + * * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. * @@ -1434,7 +1468,7 @@ class Motor : public AbstractMotor, public Device { * motor.move_absolute(100, 100); // This does not cause a movement * * motor.set_zero_position(80); - * motor.move_absolute(100, 100); // Moves 80 units forward + * motor.move_absolute(100, 100); // Moves 20 units forward * } * \endcode * @@ -1444,21 +1478,21 @@ class Motor : public AbstractMotor, public Device { /** * Sets the "absolute" zero position of the motor to its current position. * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index - * + * * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. * @@ -1478,20 +1512,32 @@ class Motor : public AbstractMotor, public Device { /** * Gets the number of motors. - * + * * \return Always returns 1 - * + * */ std::int8_t size(void) const; + /** + * Gets all motors. + * + * \return A vector of Motor objects. + * + * \b Example + * \code + * void opcontrol() { + * std::vector motor_all = pros::Motor::get_all_devices(); // All motors that are connected + * } + * \endcode + */ static std::vector get_all_devices(); /** * gets the port number of the motor * - * \return The signed port of the motor. (negative if the motor is reversed) - * - */ + * \return The signed port of the motor. (negative if the motor is reversed) + * + */ std::int8_t get_port(const std::uint8_t index = 0) const; ///@} @@ -1529,7 +1575,7 @@ class Motor : public AbstractMotor, public Device { * reached: * ENODEV - The port cannot be configured as a motor * - * \return A vector containing the commanded motor velocity from +-100, + * \return A vector containing the commanded motor velocity from +-100, * +-200, or +-600, or PROS_ERR if the operation failed, setting errno. * * \b Example @@ -1571,18 +1617,18 @@ class Motor : public AbstractMotor, public Device { * } * } * \endcode - */ + */ std::vector get_actual_velocity_all(void) const; /** * Gets a vector containing the current drawn by the motor in mA. - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * - * + * + * * \return A vector containing the motor's current in mA or PROS_ERR if the operation failed, * setting errno. * @@ -1604,13 +1650,13 @@ class Motor : public AbstractMotor, public Device { /** * Gets a vector containing the direction of movement for the motor. * - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * - * + * + * * \return A vector containing 1 for moving in the positive direction, -1 for moving in the * negative direction, and PROS_ERR if the operation failed, setting errno. * @@ -1635,12 +1681,12 @@ class Motor : public AbstractMotor, public Device { * An efficiency of 100% means that the motor is moving electrically while * drawing no electrical power, and an efficiency of 0% means that the motor * is drawing power but not moving. - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * * \return A vector containing The motor's efficiency in percent or PROS_ERR_F if the operation * failed, setting errno. @@ -1659,15 +1705,15 @@ class Motor : public AbstractMotor, public Device { * \endcode */ std::vector get_efficiency_all(void) const; - + /** * Gets a vector of the faults experienced by the motor. * * Compare this bitfield to the bitmasks in pros::motor_fault_e_t. - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor * * \return A bitfield containing the motor's faults. @@ -1680,7 +1726,7 @@ class Motor : public AbstractMotor, public Device { * while (true) { * motor = master.get_analog(E_CONTROLLER_ANALOG_LEFT_Y); * std::cout << "Motor Faults: " << motor.get_faults_all()[0]; - * pros::delay(2); + * pros::delay(2); * } * } * \endcode @@ -1691,10 +1737,10 @@ class Motor : public AbstractMotor, public Device { * Gets a vector of the flags set by the motor's operation. * * Compare this bitfield to the bitmasks in pros::motor_flag_e_t. - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor * * @@ -1714,15 +1760,15 @@ class Motor : public AbstractMotor, public Device { * \endcode */ std::vector get_flags_all(void) const; - + /** * Gets a vector containing the absolute position of the motor in its encoder units. - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - + * * \return A vector containing the motor's absolute position in its encoder units or PROS_ERR_F * if the operation failed, setting errno. @@ -1744,12 +1790,12 @@ class Motor : public AbstractMotor, public Device { /** * Gets a vector containing the power drawn by the motor in Watts. - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * \return A vector containing the motor's power draw in Watts or PROS_ERR_F if the operation * failed, setting errno. * @@ -1767,21 +1813,21 @@ class Motor : public AbstractMotor, public Device { * \endcode */ std::vector get_power_all(void) const; - + /** * Gets a vector of the raw encoder count of the motor at a given timestamp. * - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * \param timestamp * A pointer to a time in milliseconds for which the encoder count * will be returned. If NULL, the timestamp at which the encoder * count was read will not be supplied - * + * * \return A vector containing the raw encoder count at the given timestamp or PROS_ERR if the * operation failed. * @@ -1800,7 +1846,7 @@ class Motor : public AbstractMotor, public Device { * \endcode */ std::vector get_raw_position_all(std::uint32_t* const timestamp) const; - + /** * Gets a vector of the temperature of the motor in degrees Celsius. * @@ -1808,7 +1854,7 @@ class Motor : public AbstractMotor, public Device { * reached: * ENODEV - The port cannot be configured as a motor * - * \return A vector contaioning the motor's temperature in degrees Celsius + * \return A vector contaioning the motor's temperature in degrees Celsius * or PROS_ERR_F if the operation failed, setting errno. * * \b Example @@ -1832,7 +1878,7 @@ class Motor : public AbstractMotor, public Device { * This function uses the following values of errno when an error state is * reached: * ENODEV - The port cannot be configured as a motor - * + * * \return A vector containing the motor's torque in Nm or PROS_ERR_F if the operation failed, * setting errno. * @@ -1850,14 +1896,14 @@ class Motor : public AbstractMotor, public Device { * \endcode */ std::vector get_torque_all(void) const; - + /** * Gets a vector of the voltage delivered to the motor in millivolts. * * This function uses the following values of errno when an error state is * reached: * ENODEV - The port cannot be configured as a motor - * + * * * \return A vector of the motor's voltage in mV or PROS_ERR_F if the operation failed, * setting errno. @@ -1902,14 +1948,14 @@ class Motor : public AbstractMotor, public Device { * \endcode */ std::vector is_over_current_all(void) const; - + /** * Gets the temperature limit flag for the motor. * * This function uses the following values of errno when an error state is * reached: - * ENODEV - The port cannot be configured as a motor - * + * ENODEV - The port cannot be configured as a motor + * * \return A vector containing 1 if the temperature limit is exceeded and 0 if the temperature is * below the limit, or PROS_ERR if the operation failed, setting errno. * @@ -1927,7 +1973,7 @@ class Motor : public AbstractMotor, public Device { * \endcode */ std::vector is_over_temp_all(void) const; - + /** * Gets a vector containing the brake mode that was set for the motor. * @@ -1948,7 +1994,7 @@ class Motor : public AbstractMotor, public Device { * \endcode */ std::vector get_brake_mode_all(void) const; - + /** * Gets a vector containing the current limit for the motor in mA. * @@ -1993,7 +2039,7 @@ class Motor : public AbstractMotor, public Device { * \endcode */ std::vector get_encoder_units_all(void) const; - + /** * Gets a vector containing the gearset that was set for the motor. * @@ -2017,10 +2063,10 @@ class Motor : public AbstractMotor, public Device { /** * Gets returns a vector with all the port numbers in the motor group. * - * \return A vector containing the signed port of the motor. (negative if the motor is reversed) + * \return A vector containing the signed port of the motor. (negative if the motor is reversed) */ std::vector get_port_all(void) const; - + /** * Gets a vector of the voltage limit set by the user. * @@ -2060,9 +2106,29 @@ class Motor : public AbstractMotor, public Device { * \endcode */ std::vector is_reversed_all(void) const; - + /** - * Sets one of Motor_Brake to the motor. + * Gets a vector containing the type of the motor. + * + * This function uses the following values of errno when an error state is + * reached: + * ENODEV - The port cannot be configured as a motor + * + * \return A vector containing one of MotorType according to the type of the motor, + * or pros::MotorType::invalid if the operation failed. + * + * \b Example + * \code + * void initialize() { + * pros::Motor motor (1, E_MOTOR_GEARSET_06, E_MOTOR_ENCODER_COUNTS); + * std::cout << "Motor Type: " << motor.get_type_all()[0]; + * } + * \endcode + */ + std::vector get_type_all(void) const; + + /** + * Sets one of Motor_Brake to the motor. * * This function uses the following values of errno when an error state is * reached: @@ -2084,9 +2150,9 @@ class Motor : public AbstractMotor, public Device { * \endcode */ std::int32_t set_brake_mode_all(const MotorBrake mode) const; - + /** - * Sets one of Motor_Brake to the motor. + * Sets one of Motor_Brake to the motor. * * This function uses the following values of errno when an error state is * reached: @@ -2143,21 +2209,21 @@ class Motor : public AbstractMotor, public Device { * Sets one of Motor_Units for the motor encoder. Works with the C * enum and the C++ enum class. * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * + * * * \param units * The new motor encoder units - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index * @@ -2174,7 +2240,7 @@ class Motor : public AbstractMotor, public Device { * \endcode */ std::int32_t set_encoder_units_all(const MotorUnits units) const; - + /** * Sets one of Motor_Units for the motor encoder. Works with the C * enum and the C++ enum class. @@ -2199,8 +2265,7 @@ class Motor : public AbstractMotor, public Device { * \endcode */ std::int32_t set_encoder_units_all(const pros::motor_encoder_units_e_t units) const; - - + /** * Sets one of the gear cartridge (red, green, blue) for the motor. Usable with * the C++ enum class and the C enum. @@ -2225,7 +2290,7 @@ class Motor : public AbstractMotor, public Device { * \endcode */ std::int32_t set_gearing_all(const MotorGears gearset) const; - + /** * Sets one of the gear cartridge (red, green, blue) for the motor. Usable with * the C++ enum class and the C enum. @@ -2250,7 +2315,7 @@ class Motor : public AbstractMotor, public Device { * \endcode */ std::int32_t set_gearing_all(const pros::motor_gearset_e_t gearset) const; - + /** * Sets the reverse flag for the motor. * @@ -2260,7 +2325,7 @@ class Motor : public AbstractMotor, public Device { * \param reverse * True reverses the motor, false is default direction * - * \return 1 + * \return 1 * * \b Example * \code @@ -2272,25 +2337,25 @@ class Motor : public AbstractMotor, public Device { * \endcode */ std::int32_t set_reversed_all(const bool reverse); - + /** * Sets the voltage limit for the motor in Volts. * - * \note This is one of many Motor functions that takes in an optional index parameter. + * \note This is one of many Motor functions that takes in an optional index parameter. * This parameter can be ignored by most users but exists to give a shared base class * for motors and motor groups - * + * * This function uses the following values of errno when an error state is * reached: - * + * * ENODEV - The port cannot be configured as a motor - * + * * EOVERFLOW - The index is non 0 - * + * * \param limit * The new voltage limit in Volts - * - * \param index Optional parameter. + * + * \param index Optional parameter. * The zero-indexed index of the motor to get the target position of. * By default index is 0, and will return an error for a non-zero index * @@ -2313,7 +2378,7 @@ class Motor : public AbstractMotor, public Device { * \endcode */ std::int32_t set_voltage_limit_all(const std::int32_t limit) const; - + /** * Sets the position for the motor in its encoder units. * @@ -2338,7 +2403,7 @@ class Motor : public AbstractMotor, public Device { * motor.move_absolute(100, 100); // This does not cause a movement * * motor.set_zero_position_all(80); - * motor.move_absolute(100, 100); // Moves 80 units forward + * motor.move_absolute(100, 100); // Moves 20 units forward * } * \endcode * @@ -2372,10 +2437,39 @@ class Motor : public AbstractMotor, public Device { ///@} private: + /** + * The port of the motor. Negative ports indicate that the motor is reversed + */ std::int8_t _port; }; namespace literals { +/** + * Constructs a Motor from a literal ending in _mtr + * + * \return a pros::Motor for the corresponding port + * + * \b Example + * \code + * using namespace pros::literals; + * void opcontrol() { + * pros::Motor motor = 2_mtr; //Makes an Motor object on port 2 + * } + * \endcode + */ const pros::Motor operator"" _mtr(const unsigned long long int m); +/** + * Constructs a reversed Motor from a literal ending in _rmtr + * + * \return a pros::Motor for the corresponding port that is reversed + * + * \b Example + * \code + * using namespace pros::literals; + * void opcontrol() { + * pros::motor motor = 2_rmtr; //Makes an reversed Motor object on port 2 + * } + * \endcode + */ const pros::Motor operator"" _rmtr(const unsigned long long int m); } // namespace literals } // namespace v5 diff --git a/include/pros/optical.h b/include/pros/optical.h index 049c395b..bc124919 100644 --- a/include/pros/optical.h +++ b/include/pros/optical.h @@ -7,7 +7,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this @@ -460,6 +460,39 @@ int32_t optical_enable_gesture(uint8_t port); */ int32_t optical_disable_gesture(uint8_t port); +/** + * Get integration time (update rate) of the optical sensor in milliseconds, with + * minimum time being + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as an Optical Sensor + * + * \param port + * The V5 Optical Sensor port number from 1-21 + * \return Integration time in milliseconds if the operation is successful + * or PROS_ERR_F if the operation failed, setting errno. + */ +double optical_get_integration_time(uint8_t port); + +/** + * Set integration time (update rate) of the optical sensor in milliseconds. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as an Optical Sensor + * + * \param port + * The V5 Optical Sensor port number from 1-21 + * \param time + * The desired integration time in milliseconds + * \return 1 if the operation is successful or PROS_ERR if the operation failed, + * setting errno. + */ +int32_t optical_set_integration_time(uint8_t port, double time); + ///@} ///@} diff --git a/include/pros/optical.hpp b/include/pros/optical.hpp index 787bcd2b..3cb55d91 100644 --- a/include/pros/optical.hpp +++ b/include/pros/optical.hpp @@ -7,12 +7,12 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * + * * \defgroup cpp-optical VEX Optical Sensor C++ API */ @@ -24,8 +24,8 @@ #include #include -#include "pros/optical.h" #include "pros/device.hpp" +#include "pros/optical.h" namespace pros { inline namespace v5 { @@ -48,17 +48,29 @@ class Optical : public Device { * * \param port * The V5 port number from 1-21 - * - * \b Example: + * + * \b Example: * \code{.cpp} * pros::Optical optical(1); * \endcode */ Optical(const std::uint8_t port); - Optical(const Device& device) - : Optical(device.get_port()) {}; + Optical(const Device& device) : Optical(device.get_port()){}; + + /** + * Gets all optical sensors. + * + * \return A vector of Optical sensor objects. + * + * \b Example + * \code + * void opcontrol() { + * std::vector optical_all = pros::Optical::get_all_devices(); // All optical sensors that are connected + * } + * \endcode + */ static std::vector get_all_devices(); /** @@ -98,7 +110,7 @@ class Optical : public Device { * * \return saturation value if the operation was successful or PROS_ERR_F if * the operation failed, setting errno. - * + * * \b Example: * \code{.cpp} * void opcontrol() { @@ -167,8 +179,8 @@ class Optical : public Device { * ENXIO - The given value is not within the range of V5 ports (1-21). * ENODEV - The port cannot be configured as an Optical Sensor * - * \return The Error code encountered or PROS_SUCCESS. - * + * \return The Error code encountered or PROS_SUCCESS. + * * \b Example: * \code{.cpp} * void initialize() { @@ -211,9 +223,9 @@ class Optical : public Device { * ENXIO - The given value is not within the range of V5 ports (1-21). * ENODEV - The port cannot be configured as an Optical Sensor * - * \return rgb value if the operation was successful or an optical_rgb_s_t + * \return rgb value if the operation was successful or an optical_rgb_s_t * with all fields set to PROS_ERR if the operation failed, setting errno. - * + * * \b Example: * \code{.cpp} * void opcontrol() { @@ -239,9 +251,9 @@ class Optical : public Device { * ENXIO - The given value is not within the range of V5 ports (1-21). * ENODEV - The port cannot be configured as an Optical Sensor * - * \return raw rgb value if the operation was successful or an optical_raw_s_t + * \return raw rgb value if the operation was successful or an optical_raw_s_t * with all fields set to PROS_ERR if the operation failed, setting errno. - * + * * \b Example: * \code{.cpp} * void opcontrol() { @@ -263,6 +275,7 @@ class Optical : public Device { * Get the most recent gesture data from the sensor * * Gestures will be cleared after 500mS + * * * 0 = no gesture, * 1 = up (towards cable), @@ -299,7 +312,7 @@ class Optical : public Device { * ENXIO - The given value is not within the range of V5 ports (1-21). * ENODEV - The port cannot be configured as an Optical Sensor * - * \return gesture value if the operation was successful or an optical_gesture_s_t + * \return gesture value if the operation was successful or an optical_gesture_s_t * with all fields set to PROS_ERR if the operation failed, setting errno. * * \b Example: @@ -333,8 +346,8 @@ class Optical : public Device { * ENODEV - The port cannot be configured as an Optical Sensor * * \return 1 if the operation is successful or PROS_ERR if the operation failed, - * setting errno. - * + * setting errno. + * * \b Example: * \code{.cpp} * void opcontrol() { @@ -366,7 +379,7 @@ class Optical : public Device { * ENODEV - The port cannot be configured as an Optical Sensor * * \return 1 if the operation is successful or PROS_ERR if the operation failed, - * setting errno. + * setting errno. * * \b Example: * \code{.cpp} @@ -385,12 +398,40 @@ class Optical : public Device { */ virtual std::int32_t disable_gesture(); + /** + * Get integration time (update rate) of the optical sensor in milliseconds. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as an Optical Sensor + * + * \return Integration time in milliseconds if the operation is successful + * or PROS_ERR_F if the operation failed, setting errno. + */ + double get_integration_time(); /** - * This is the overload for the << operator for printing to streams - * - * Prints in format(this below is all in one line with no new line): - * Optical [port: (port number), hue: (hue), saturation: (saturation), + * Set integration time (update rate) of the optical sensor in milliseconds, with + * minimum time being 3 ms and maximum time being 712 ms. Default is 100 ms, with the + * optical sensor communciating with the V5 brain every 20 ms. + * + * This function uses the following values of errno when an error state is + * reached: + * ENXIO - The given value is not within the range of V5 ports (1-21). + * ENODEV - The port cannot be configured as an Optical Sensor + * + * \param time The desired integration time in milliseconds + * \return 1 if the operation is successful or PROS_ERR if the operation failed, + * setting errno. + */ + std::int32_t set_integration_time(double time); + + /** + * This is the overload for the << operator for printing to streams + * + * Prints in format(this below is all in one line with no new line): + * Optical [port: (port number), hue: (hue), saturation: (saturation), * brightness: (brightness), proximity: (proximity), rgb: {red, green, blue}] * * \b Example: @@ -400,15 +441,28 @@ class Optical : public Device { * \endcode */ friend std::ostream& operator<<(std::ostream& os, pros::Optical& optical); - + private: ///@} }; namespace literals { +/** + * Constructs a Optical sensor from a literal ending in _opt + * + * \return a pros::Optical for the corresponding port + * + * \b Example + * \code + * using namespace pros::literals; + * void opcontrol() { + * pros::Optical opt = 2_opt; //Makes an Optical object on port 2 + * } + * \endcode + */ const pros::Optical operator"" _opt(const unsigned long long int o); } // namespace literals -} +} // namespace v5 } // namespace pros #endif diff --git a/include/pros/rotation.h b/include/pros/rotation.h index 1c1c7317..5a904414 100644 --- a/include/pros/rotation.h +++ b/include/pros/rotation.h @@ -7,7 +7,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this @@ -132,7 +132,7 @@ int32_t rotation_set_data_rate(uint8_t port, uint32_t rate); * } * \endcode */ -int32_t rotation_set_position(uint8_t port, uint32_t position); +int32_t rotation_set_position(uint8_t port, int32_t position); /** * Reset the Rotation Sensor position to 0 diff --git a/include/pros/rotation.hpp b/include/pros/rotation.hpp index 2947300c..1e6dda9d 100644 --- a/include/pros/rotation.hpp +++ b/include/pros/rotation.hpp @@ -7,12 +7,12 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * + * * \defgroup cpp-rotation VEX Rotation Sensor C++ API */ #ifndef _PROS_ROTATION_HPP_ @@ -21,8 +21,8 @@ #include #include -#include "pros/rotation.h" #include "pros/device.hpp" +#include "pros/rotation.h" namespace pros { inline namespace v5 { @@ -38,25 +38,25 @@ class Rotation : public Device { public: /** * Constructs a new Rotation Sensor object - * + * * ENXIO - The given value is not within the range of V5 ports |1-21|. - * ENODEV - The port cannot be configured as a Rotation Sensor - * + * ENODEV - The port cannot be configured as a Rotation Sensor + * * \param port - * The V5 port number from 1 to 21, or from -21 to -1 for reversed Rotation Sensors. - * + * The V5 port number from 1 to 21, or from -21 to -1 for reversed Rotation Sensors. + * * \b Example - * \code - * void opcontrol() { + * \code + * void opcontrol() { * pros::Rotation rotation_sensor(1); //Creates a Rotation Sensor on port 1 * pros::Rotation reversed_rotation_sensor(-2); //Creates a reversed Rotation Sensor on port 2 - * } - * \endcode - */ + * } + * \endcode + */ Rotation(const std::int8_t port); - Rotation(const Device& device) - : Rotation(device.get_port()) {}; + Rotation(const Device& device) : Rotation(device.get_port()){}; + /** * Reset the Rotation Sensor @@ -71,7 +71,7 @@ class Rotation : public Device { * * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. - * + * * \b Example * \code * void opcontrol() { @@ -108,14 +108,14 @@ class Rotation : public Device { * \param rate The data refresh interval in milliseconds * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. - * + * * \b Example - * \code - * void initialize() { - * pros::Rotation rotation_sensor(1); - * rotation_sensor.set_data_rate(5); - * } - * \endcode + * \code + * void initialize() { + * pros::Rotation rotation_sensor(1); + * rotation_sensor.set_data_rate(5); + * } + * \endcode */ virtual std::int32_t set_data_rate(std::uint32_t rate) const; @@ -131,7 +131,7 @@ class Rotation : public Device { * The position in terms of ticks * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. - * + * * \b Example * \code * void opcontrol() { @@ -146,7 +146,7 @@ class Rotation : public Device { * } * \endcode */ - virtual std::int32_t set_position(std::uint32_t position) const; + virtual std::int32_t set_position(std::int32_t position) const; /** * Reset the Rotation Sensor position to 0 @@ -160,7 +160,7 @@ class Rotation : public Device { * The position in terms of ticks * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. - * + * * \b Example * \code * void opcontrol() { @@ -177,6 +177,20 @@ class Rotation : public Device { */ virtual std::int32_t reset_position(void) const; + /** + * Gets all rotation sensors. + * + * \return A vector of Rotation sensor objects. + * + * \b Example + * \code + * void opcontrol() { + * std::vector rotation_all = pros::Rotation::get_all_devices(); // All rotation sensors that are connected + * } + * \endcode + */ + static std::vector get_all_devices(); + /** * Get the Rotation Sensor's current position in centidegrees * @@ -187,17 +201,17 @@ class Rotation : public Device { * * \return The position value or PROS_ERR if the operation failed, setting * errno. - * + * * \b Example - * \code - * void opcontrol() { + * \code + * void opcontrol() { * pros::Rotation rotation_sensor(1); - * while (true) { - * printf("Position: %d Ticks \n", rotation_sensor.get_position()); - * delay(20); - * } - * } - * \endcode + * while (true) { + * printf("Position: %d Ticks \n", rotation_sensor.get_position()); + * delay(20); + * } + * } + * \endcode */ virtual std::int32_t get_position() const; @@ -213,17 +227,17 @@ class Rotation : public Device { * The V5 Rotation Sensor port number from 1-21 * \return The velocity value or PROS_ERR if the operation failed, setting * errno. - * + * * \b Example - * \code - * void opcontrol() { + * \code + * void opcontrol() { * pros::Rotation rotation_sensor(1); - * while (true) { - * printf("Velocity: %d centidegrees per second \n", rotation_sensor.get_velocity)); - * delay(20); - * } - * } - * \endcode + * while (true) { + * printf("Velocity: %d centidegrees per second \n", rotation_sensor.get_velocity)); + * delay(20); + * } + * } + * \endcode */ virtual std::int32_t get_velocity() const; @@ -237,17 +251,17 @@ class Rotation : public Device { * * \return The angle value or PROS_ERR if the operation failed, setting * errno. - * + * * \b Example - * \code - * void opcontrol() { + * \code + * void opcontrol() { * pros::Rotation rotation_sensor(1); - * while (true) { - * printf("Angle: %d centidegrees \n", rotation_sensor.get_angle()); - * delay(20); - * } - * } - * \endcode + * while (true) { + * printf("Angle: %d centidegrees \n", rotation_sensor.get_angle()); + * delay(20); + * } + * } + * \endcode */ virtual std::int32_t get_angle() const; @@ -265,7 +279,7 @@ class Rotation : public Device { * * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. - * + * * \b Example * \code * void opcontrol() { @@ -292,7 +306,7 @@ class Rotation : public Device { * * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. - * + * * \b Example * \code * void opcontrol() { @@ -319,27 +333,27 @@ class Rotation : public Device { * * \return Reversed value or PROS_ERR if the operation failed, setting * errno. - * + * * \b Example - * \code - * void opcontrol() { + * \code + * void opcontrol() { * pros::Rotation rotation_sensor(1); - * while (true) { - * printf("Reversed: %d \n", rotation_sensor.get_reversed()); - * delay(20); - * } - * } - * \endcode + * while (true) { + * printf("Reversed: %d \n", rotation_sensor.get_reversed()); + * delay(20); + * } + * } + * \endcode */ virtual std::int32_t get_reversed() const; /** * This is the overload for the << operator for printing to streams - * + * * Prints in format(this below is all in one line with no new line): - * Rotation [port: rotation._port, position: (rotation position), velocity: (rotation velocity), + * Rotation [port: rotation._port, position: (rotation position), velocity: (rotation velocity), * angle: (rotation angle), reversed: (reversed boolean)] - * + * * \b Example * \code * #define ROTATION_PORT 1 @@ -355,13 +369,26 @@ class Rotation : public Device { */ friend std::ostream& operator<<(std::ostream& os, const pros::Rotation& rotation); -///@} + ///@} }; namespace literals { +/** + * Constructs a Rotation sensor from a literal ending in _rot + * + * \return a pros::Rotation for the corresponding port + * + * \b Example + * \code + * using namespace pros::literals; + * void opcontrol() { + * pros::Rotation rotation = 2_rot; //Makes an Motor object on port 2 + * } + * \endcode + */ const pros::Rotation operator"" _rot(const unsigned long long int r); } // namespace literals -} +} // namespace v5 } // namespace pros #endif diff --git a/include/pros/rtos.h b/include/pros/rtos.h index d6b9be48..b5ae33bf 100644 --- a/include/pros/rtos.h +++ b/include/pros/rtos.h @@ -8,7 +8,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * All rights reserved. * * This Source Code Form is subject to the terms of the Mozilla Public @@ -74,7 +74,7 @@ namespace pros { /** * The minimal stack size for a task. * - * This equates to 512 words, or 2,048 bytes. + * This equates to 512 words, or 2,048 bytes. */ #define TASK_STACK_DEPTH_MIN 0x200 @@ -94,7 +94,7 @@ namespace pros { /// @{ /** - * An opaque type that pontis to a task handle. This is used for referencing a + * An opaque type that points to a task handle. This is used for referencing a * task. */ typedef void* task_t; @@ -1076,7 +1076,87 @@ bool mutex_take(mutex_t mutex, uint32_t timeout); bool mutex_give(mutex_t mutex); /** - * Deletes a mutex + * Creates a recursive mutex which can be locked recursively by the owner. + * + * \return A newly created recursive mutex. + * + * \b Example: + * \code + * mutex_t mutex = mutex_recursive_create(); + * + * void task_fn(void* param) { + * while(1) { + * mutex_recursive_take(mutex, 1000); + * // critical section + * mutex_recursive_give(mutex); + * task_delay(1000); + * } + * } + * task_create(task_fn, (void*)"PROS", TASK_PRIORITY_DEFAULT, + * TASK_STACK_DEPTH_DEFAULT, "task_fn"); + * + * \endcode + */ +mutex_t mutex_recursive_create(void); + +/** + * Takes a recursive mutex. + * + * \param mutex + * A mutex handle created by mutex_recursive_create + * \param wait_time + * Amount of time to wait before timing out + * + * \return 1 if the mutex was obtained, 0 otherwise + * + * \b Example: + * \code + * mutex_t mutex = mutex_recursive_create(); + * + * void task_fn(void* param) { + * while(1) { + * mutex_recursive_take(mutex, 1000); + * // critical section + * mutex_recursive_give(mutex); + * task_delay(1000); + * } + * } + * task_create(task_fn, (void*)"PROS", TASK_PRIORITY_DEFAULT, + * TASK_STACK_DEPTH_DEFAULT, "task_fn"); + * + * \endcode + */ +bool mutex_recursive_take(mutex_t mutex, uint32_t timeout); + +/** + * Gives a recursive mutex. + * + * \param mutex + * A mutex handle created by mutex_recursive_create + * + * \return 1 if the mutex was obtained, 0 otherwise + * + * \b Example: + * \code + * mutex_t mutex = mutex_recursive_create(); + * + * void task_fn(void* param) { + * while(1) { + * mutex_recursive_take(mutex, 1000); + * // critical section + * mutex_recursive_give(mutex); + * task_delay(1000); + * } + * } + * task_create(task_fn, (void*)"PROS", TASK_PRIORITY_DEFAULT, + * TASK_STACK_DEPTH_DEFAULT, "task_fn"); + * + * \endcode + */ +bool mutex_recursive_give(mutex_t mutex); + +/** + * Deletes a mutex or recursive mutex * * \param mutex * Mutex to unlock. diff --git a/include/pros/rtos.hpp b/include/pros/rtos.hpp index f02495c2..d6c2d890 100644 --- a/include/pros/rtos.hpp +++ b/include/pros/rtos.hpp @@ -8,7 +8,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * All rights reserved. * * This Source Code Form is subject to the terms of the Mozilla Public @@ -24,6 +24,7 @@ #include "pros/rtos.h" #undef delay +#include #include #include #include @@ -821,10 +822,14 @@ struct Clock { }; class Mutex { - std::shared_ptr> mutex; - + std::atomic mutex{nullptr}; + mutex_t lazy_init(); public: - Mutex(); + constexpr Mutex() { + if (!std::is_constant_evaluated()) { + lazy_init(); + } + } // disable copy and move construction and assignment per Mutex requirements // (see https://en.cppreference.com/w/cpp/named_req/Mutex) @@ -1278,7 +1283,7 @@ class Mutex { * } * } */ - bool try_lock(); + [[nodiscard]] bool try_lock(); /** * Takes and locks a mutex, waiting for a specified duration. @@ -1308,7 +1313,7 @@ class Mutex { * \endcode */ template - bool try_lock_for(const std::chrono::duration& rel_time) { + [[nodiscard]] bool try_lock_for(const std::chrono::duration& rel_time) { return take(std::chrono::duration_cast(rel_time).count()); } @@ -1346,6 +1351,542 @@ class Mutex { bool try_lock_until(const std::chrono::time_point& abs_time) { return take(std::max(static_cast(0), (abs_time - Clock::now()).count())); } + ~Mutex(); + ///@} +}; + +class RecursiveMutex { + std::atomic mutex{nullptr}; + mutex_t lazy_init(); + public: + constexpr RecursiveMutex() { + if (!std::is_constant_evaluated()) { + lazy_init(); + } + } + + // disable copy and move construction and assignment per Mutex requirements + // (see https://en.cppreference.com/w/cpp/named_req/Mutex) + RecursiveMutex(const RecursiveMutex&) = delete; + RecursiveMutex(RecursiveMutex&&) = delete; + + RecursiveMutex& operator=(const RecursiveMutex&) = delete; + RecursiveMutex& operator=(RecursiveMutex&&) = delete; + + /** + * Takes and locks a mutex indefinetly. + * + * See + * https://pros.cs.purdue.edu/v5/tutorials/topical/multitasking.html#mutexes + * for details. + * + * \return True if the mutex was successfully taken, false otherwise. If false + * is returned, then errno is set with a hint about why the the mutex + * couldn't be taken + * + * \b Example + * \code + * // Global variables for the robot's odometry, which the rest of the robot's + * // subsystems will utilize + * double odom_x = 0.0; + * double odom_y = 0.0; + * double odom_heading = 0.0; + * + * // This mutex protects the odometry data. Whenever we read or write to the + * // odometry data, we should make copies into the local variables, and read + * // all 3 values at once to avoid errors. + * pros::RecursiveMutex odom_mutex; + * + * void odom_task(void* param) { + * while(true) { + * // First we fetch the odom coordinates from the previous iteration of the + * // odometry task. These are put into local variables so that we can + * // keep the size of the critical section as small as possible. This lets + * // other tasks that need to use the odometry data run until we need to + * // update it again. + * odom_mutex.take(); + * double x_old = odom_x; + * double y_old = odom_y; + * double heading_old = odom_heading; + * odom_mutex.give(); + * + * double x_new = 0.0; + * double y_new = 0.0; + * double heading_new = 0.0; + * + * // --- Calculate new pose for the robot here --- + * + * // Now that we have the new pose, we can update the global variables + * odom_mutex.take(); + * odom_x = x_new; + * odom_y = y_new; + * odom_heading = heading_new; + * odom_mutex.give(); + * + * delay(10); + * } + * } + * + * void chassis_task(void* param) { + * while(true) { + * // Here we copy the current odom values into local variables so that + * // we can use them without worrying about the odometry task changing say, + * // the y value right after we've read the x. This ensures our values are + * // sound. + * odom_mutex.take(); + * double current_x = odom_x; + * double current_y = odom_y; + * double current_heading = odom_heading; + * odom_mutex.give(); + * + * // ---- Move the robot using the current locations goes here ---- + * + * delay(10); + * } + * } + * + * void initialize() { + * odom_mutex = pros::RecursiveMutex(); + * + * pros::Task odom_task(odom_task, "Odometry Task"); + * pros::Task chassis_task(odom_task, "Chassis Control Task"); + * } + * \endcode. + */ + bool take(); + + /** + * Takes and locks a mutex, waiting for up to a certain number of milliseconds + * before timing out. + * + * See + * https://pros.cs.purdue.edu/v5/tutorials/topical/multitasking.html#mutexes + * for details. + * + * \param timeout + * Time to wait before the mutex becomes available. A timeout of 0 can + * be used to poll the mutex. TIMEOUT_MAX can be used to block + * indefinitely. + * + * \return True if the mutex was successfully taken, false otherwise. If false + * is returned, then errno is set with a hint about why the the mutex + * couldn't be taken. + * + * \b Example + * \code + * // Global variables for the robot's odometry, which the rest of the robot's + * // subsystems will utilize + * double odom_x = 0.0; + * double odom_y = 0.0; + * double odom_heading = 0.0; + * + * // This mutex protects the odometry data. Whenever we read or write to the + * // odometry data, we should make copies into the local variables, and read + * // all 3 values at once to avoid errors. + * pros::RecursiveMutex odom_mutex; + * + * void odom_task(void* param) { + * while(true) { + * // First we fetch the odom coordinates from the previous iteration of the + * // odometry task. These are put into local variables so that we can + * // keep the size of the critical section as small as possible. This lets + * // other tasks that need to use the odometry data run until we need to + * // update it again. + * odom_mutex.take(); + * double x_old = odom_x; + * double y_old = odom_y; + * double heading_old = odom_heading; + * odom_mutex.give(); + * + * double x_new = 0.0; + * double y_new = 0.0; + * double heading_new = 0.0; + * + * // --- Calculate new pose for the robot here --- + * + * // Now that we have the new pose, we can update the global variables + * odom_mutex.take(); + * odom_x = x_new; + * odom_y = y_new; + * odom_heading = heading_new; + * odom_mutex.give(); + * + * delay(10); + * } + * } + * + * void chassis_task(void* param) { + * while(true) { + * // Here we copy the current odom values into local variables so that + * // we can use them without worrying about the odometry task changing say, + * // the y value right after we've read the x. This ensures our values are + * // sound. + * odom_mutex.take(); + * double current_x = odom_x; + * double current_y = odom_y; + * double current_heading = odom_heading; + * odom_mutex.give(); + * + * // ---- Move the robot using the current locations goes here ---- + * + * delay(10); + * } + * } + * + * void initialize() { + * odom_mutex = pros::RecursiveMutex(); + * + * pros::Task odom_task(odom_task, "Odometry Task"); + * pros::Task chassis_task(odom_task, "Chassis Control Task"); + * } + * \endcode. + */ + bool take(std::uint32_t timeout); + + /** + * Unlocks a mutex. + * + * See + * https://pros.cs.purdue.edu/v5/tutorials/topical/multitasking.html#mutexes + * for details. + * + * \return True if the mutex was successfully returned, false otherwise. If + * false is returned, then errno is set with a hint about why the mutex + * couldn't be returned. + * + * \b Example + * \code + * // Global variables for the robot's odometry, which the rest of the robot's + * // subsystems will utilize + * double odom_x = 0.0; + * double odom_y = 0.0; + * double odom_heading = 0.0; + * + * // This mutex protects the odometry data. Whenever we read or write to the + * // odometry data, we should make copies into the local variables, and read + * // all 3 values at once to avoid errors. + * pros::RecursiveMutex odom_mutex; + * + * void odom_task(void* param) { + * while(true) { + * // First we fetch the odom coordinates from the previous iteration of the + * // odometry task. These are put into local variables so that we can + * // keep the size of the critical section as small as possible. This lets + * // other tasks that need to use the odometry data run until we need to + * // update it again. + * odom_mutex.take(); + * double x_old = odom_x; + * double y_old = odom_y; + * double heading_old = odom_heading; + * odom_mutex.give(); + * + * double x_new = 0.0; + * double y_new = 0.0; + * double heading_new = 0.0; + * + * // --- Calculate new pose for the robot here --- + * + * // Now that we have the new pose, we can update the global variables + * odom_mutex.take(); + * odom_x = x_new; + * odom_y = y_new; + * odom_heading = heading_new; + * odom_mutex.give(); + * + * delay(10); + * } + * } + * + * void chassis_task(void* param) { + * while(true) { + * // Here we copy the current odom values into local variables so that + * // we can use them without worrying about the odometry task changing say, + * // the y value right after we've read the x. This ensures our values are + * // sound. + * odom_mutex.take(); + * double current_x = odom_x; + * double current_y = odom_y; + * double current_heading = odom_heading; + * odom_mutex.give(); + * + * // ---- Move the robot using the current locations goes here ---- + * + * delay(10); + * } + * } + * + * void initialize() { + * odom_mutex = pros::RecursiveMutex(); + * + * pros::Task odom_task(odom_task, "Odometry Task"); + * pros::Task chassis_task(odom_task, "Chassis Control Task"); + * } + * \endcode. + */ + bool give(); + + /** + * Takes and locks a mutex, waiting for up to TIMEOUT_MAX milliseconds. + * + * Effectively equivalent to calling pros::RecursiveMutex::take with TIMEOUT_MAX as + * the parameter. + * + * Conforms to named requirment BasicLockable + * \see https://en.cppreference.com/w/cpp/named_req/BasicLockable + * + * \note Consider using a std::unique_lock, std::lock_guard, or + * std::scoped_lock instead of interacting with the Mutex directly. + * + * \exception std::system_error Mutex could not be locked within TIMEOUT_MAX + * milliseconds. see errno for details. + * + * \b Example + * \code + * // Global variables for the robot's odometry, which the rest of the robot's + * // subsystems will utilize + * double odom_x = 0.0; + * double odom_y = 0.0; + * double odom_heading = 0.0; + * + * // This mutex protects the odometry data. Whenever we read or write to the + * // odometry data, we should make copies into the local variables, and read + * // all 3 values at once to avoid errors. + * pros::RecursiveMutex odom_mutex; + * + * void odom_task(void* param) { + * while(true) { + * // First we fetch the odom coordinates from the previous iteration of the + * // odometry task. These are put into local variables so that we can + * // keep the size of the critical section as small as possible. This lets + * // other tasks that need to use the odometry data run until we need to + * // update it again. + * odom_mutex.lock(); + * double x_old = odom_x; + * double y_old = odom_y; + * double heading_old = odom_heading; + * odom_mutex.unlock(); + * + * double x_new = 0.0; + * double y_new = 0.0; + * double heading_new = 0.0; + * + * // --- Calculate new pose for the robot here --- + * + * // Now that we have the new pose, we can update the global variables + * odom_mutex.lock(); + * odom_x = x_new; + * odom_y = y_new; + * odom_heading = heading_new; + * odom_mutex.unlock(); + * + * delay(10); + * } + * } + * + * void chassis_task(void* param) { + * while(true) { + * // Here we copy the current odom values into local variables so that + * // we can use them without worrying about the odometry task changing say, + * // the y value right after we've read the x. This ensures our values are + * // sound. + * odom_mutex.lock(); + * double current_x = odom_x; + * double current_y = odom_y; + * double current_heading = odom_heading; + * odom_mutex.unlock(); + * + * // ---- Move the robot using the current locations goes here ---- + * + * delay(10); + * } + * } + * + * void initialize() { + * odom_mutex = pros::RecursiveMutex(); + * + * pros::Task odom_task(odom_task, "Odometry Task"); + * pros::Task chassis_task(odom_task, "Chassis Control Task"); + * } + * \endcode. + */ + void lock(); + + /** + * Unlocks a mutex. + * + * Equivalent to calling pros::RecursiveMutex::give. + * + * Conforms to named requirement BasicLockable + * \see https://en.cppreference.com/w/cpp/named_req/BasicLockable + * + * \note Consider using a std::unique_lock, std::lock_guard, or + * std::scoped_lock instead of interacting with the Mutex direcly. + * + * \b Example + * \code + * // Global variables for the robot's odometry, which the rest of the robot's + * // subsystems will utilize + * double odom_x = 0.0; + * double odom_y = 0.0; + * double odom_heading = 0.0; + * + * // This mutex protects the odometry data. Whenever we read or write to the + * // odometry data, we should make copies into the local variables, and read + * // all 3 values at once to avoid errors. + * pros::RecursiveMutex odom_mutex; + * + * void odom_task(void* param) { + * while(true) { + * // First we fetch the odom coordinates from the previous iteration of the + * // odometry task. These are put into local variables so that we can + * // keep the size of the critical section as small as possible. This lets + * // other tasks that need to use the odometry data run until we need to + * // update it again. + * odom_mutex.lock(); + * double x_old = odom_x; + * double y_old = odom_y; + * double heading_old = odom_heading; + * odom_mutex.unlock(); + * + * double x_new = 0.0; + * double y_new = 0.0; + * double heading_new = 0.0; + * + * // --- Calculate new pose for the robot here --- + * + * // Now that we have the new pose, we can update the global variables + * odom_mutex.lock(); + * odom_x = x_new; + * odom_y = y_new; + * odom_heading = heading_new; + * odom_mutex.unlock(); + * + * delay(10); + * } + * } + * + * void chassis_task(void* param) { + * while(true) { + * // Here we copy the current odom values into local variables so that + * // we can use them without worrying about the odometry task changing say, + * // the y value right after we've read the x. This ensures our values are + * // sound. + * odom_mutex.lock(); + * double current_x = odom_x; + * double current_y = odom_y; + * double current_heading = odom_heading; + * odom_mutex.unlock(); + * + * // ---- Move the robot using the current locations goes here ---- + * + * delay(10); + * } + * } + * + * void initialize() { + * odom_mutex = pros::RecursiveMutex(); + * + * pros::Task odom_task(odom_task, "Odometry Task"); + * pros::Task chassis_task(odom_task, "Chassis Control Task"); + * } + * \endcode. + */ + void unlock(); + + /** + * Try to lock a mutex. + * + * Returns immediately if unsucessful. + * + * Conforms to named requirement Lockable + * \see https://en.cppreference.com/w/cpp/named_req/Lockable + * + * \return True when lock was acquired succesfully, or false otherwise. + * + * pros::RecursiveMutex mutex; + * + * void my_task_fn(void* param) { + * while (true) { + * if(mutex.try_lock()) { + * printf("Mutex aquired successfully!\n"); + * // Do stuff that requires the protected resource here + * } + * else { + * printf("Mutex not aquired!\n"); + * } + * } + * } + */ + [[nodiscard]] bool try_lock(); + + /** + * Takes and locks a mutex, waiting for a specified duration. + * + * Equivalent to calling pros::RecursiveMutex::take with a duration specified in + * milliseconds. + * + * Conforms to named requirement TimedLockable + * \see https://en.cppreference.com/w/cpp/named_req/TimedLockable + * + * \param rel_time Time to wait before the mutex becomes available. + * \return True if the lock was acquired succesfully, otherwise false. + * + * \b Example + * \code + * void my_task_fn(void* param) { + * while (true) { + * if(mutex.try_lock_for(std::chrono::milliseconds(100))) { + * printf("Mutex aquired successfully!\n"); + * // Do stuff that requires the protected resource here + * } + * else { + * printf("Mutex not aquired after 100 milliseconds!\n"); + * } + * } + * } + * \endcode + */ + template + [[nodiscard]] bool try_lock_for(const std::chrono::duration& rel_time) { + return take(std::chrono::duration_cast(rel_time).count()); + } + + /** + * Takes and locks a mutex, waiting until a specified time. + * + * Conforms to named requirement TimedLockable + * \see https://en.cppreference.com/w/cpp/named_req/TimedLockable + * + * \param abs_time Time point until which to wait for the mutex. + * \return True if the lock was acquired succesfully, otherwise false. + * + * \b Example + * \code + * void my_task_fn(void* param) { + * while (true) { + * // Get the current time point + * auto now = std::chrono::system_clock::now(); + * + * // Calculate the time point 100 milliseconds from now + * auto abs_time = now + std::chrono::milliseconds(100); + * + * if(mutex.try_lock_until(abs_time)) { + * printf("Mutex aquired successfully!\n"); + * // Do stuff that requires the protected resource here + * } + * else { + * printf("Mutex not aquired after 100 milliseconds!\n"); + * } + * } + * } + * \endcode + */ + template + bool try_lock_until(const std::chrono::time_point& abs_time) { + return take(std::max(static_cast(0), (abs_time - Clock::now()).count())); + } + + ~RecursiveMutex(); ///@} }; diff --git a/include/pros/screen.h b/include/pros/screen.h index 71c28570..c1e567bc 100644 --- a/include/pros/screen.h +++ b/include/pros/screen.h @@ -6,7 +6,7 @@ * * Contains user calls to the v5 screen for touching and displaying graphics. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this diff --git a/include/pros/screen.hpp b/include/pros/screen.hpp index c3980e1f..cbfe2fcf 100644 --- a/include/pros/screen.hpp +++ b/include/pros/screen.hpp @@ -6,7 +6,7 @@ * * Contains user calls to the v5 screen for touching and displaying graphics. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this diff --git a/include/pros/serial.h b/include/pros/serial.h index dc9ead56..4bda6069 100644 --- a/include/pros/serial.h +++ b/include/pros/serial.h @@ -7,7 +7,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this diff --git a/include/pros/serial.hpp b/include/pros/serial.hpp index a2490e23..1996aef1 100644 --- a/include/pros/serial.hpp +++ b/include/pros/serial.hpp @@ -7,12 +7,12 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * + * * \defgroup cpp-serial Generic Serial C++ API */ @@ -20,8 +20,9 @@ #define _PROS_SERIAL_HPP_ #include -#include "pros/serial.h" + #include "pros/device.hpp" +#include "pros/serial.h" namespace pros { /** @@ -46,8 +47,8 @@ class Serial : public Device { * The V5 port number from 1-21 * \param baudrate * The baudrate to run the port at - * - * \b Example: + * + * \b Example: * \code * pros::Serial serial(1, 9600); * \endcode @@ -91,7 +92,7 @@ class Serial : public Device { * * \return 1 if the operation was successful or PROS_ERR if the operation * failed, setting errno. - * + * * \b Example: * \code * pros::Serial serial(1); @@ -142,7 +143,7 @@ class Serial : public Device { * * \return The number of bytes avaliable to be read or PROS_ERR if the operation * failed, setting errno. - * + * * \b Example: * \code * void opcontrol() { @@ -192,7 +193,7 @@ class Serial : public Device { * * \return The next byte avaliable to be read, -1 if none are available, or * PROS_ERR if the operation failed, setting errno. - * + * * \b Example: * \code * void opcontrol() { @@ -318,12 +319,25 @@ class Serial : public Device { * \endcode */ virtual std::int32_t write(std::uint8_t* buffer, std::int32_t length) const; - + private: ///@} }; namespace literals { +/** + * Constructs a Serial device from a litteral ending in _ser + * + * \return a pros::Serial for the corresponding port + * + * \b Example + * \code + * using namespace pros::literals; + * void opcontrol() { + * pros::Serial serial = 2_ser; //Makes an Serial device object on port 2 + * } + * \endcode + */ const pros::Serial operator"" _ser(const unsigned long long int m); } // namespace literals } // namespace pros diff --git a/include/pros/version.h b/include/pros/version.h new file mode 100644 index 00000000..8804838a --- /dev/null +++ b/include/pros/version.h @@ -0,0 +1,23 @@ +/** +* \file version.h +* +* PROS Version Information +* +* Contains PROS kernel version information +* +* +* \copyright Copyright (c) 2017-2024, Purdue University ACM SIGBots. +* All rights reserved. +* +* This Source Code Form is subject to the terms of the Mozilla Public +* License, v. 2.0. If a copy of the MPL was not distributed with this +* file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ + +#pragma once + +#define PROS_VERSION_MAJOR 4 +#define PROS_VERSION_MINOR 2 + +#define PROS_VERSION_PATCH 1 +#define PROS_VERSION_STRING "4.2.1" diff --git a/include/pros/vision.h b/include/pros/vision.h index dbda3fc7..6b3f104c 100644 --- a/include/pros/vision.h +++ b/include/pros/vision.h @@ -7,7 +7,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * All rights reserved. * * This Source Code Form is subject to the terms of the Mozilla Public diff --git a/include/pros/vision.hpp b/include/pros/vision.hpp index e24eec10..0a845f6b 100644 --- a/include/pros/vision.hpp +++ b/include/pros/vision.hpp @@ -7,7 +7,7 @@ * This file should not be modified by users, since it gets replaced whenever * a kernel upgrade occurs. * - * \copyright (c) 2017-2023, Purdue University ACM SIGBots. + * \copyright (c) 2017-2024, Purdue University ACM SIGBots. * All rights reserved. * * This Source Code Form is subject to the terms of the Mozilla Public @@ -59,8 +59,7 @@ class Vision : public Device { */ Vision(std::uint8_t port, vision_zero_e_t zero_point = E_VISION_ZERO_TOPLEFT); - Vision(const Device& device) - : Vision(device.get_port()) {}; + Vision(const Device& device) : Vision(device.get_port()){}; /** * Clears the vision sensor LED color, reseting it back to its default @@ -172,6 +171,18 @@ class Vision : public Device { const std::uint32_t sig_id3 = 0, const std::uint32_t sig_id4 = 0, const std::uint32_t sig_id5 = 0) const; + /** + * Gets all vision sensors. + * + * \return A vector of Vision sensor objects. + * + * \b Example + * \code + * void opcontrol() { + * std::vector vision_all = pros::Vision::get_all_devices(); // All vision sensors that are connected + * } + * \endcode + */ static std::vector get_all_devices(); /** @@ -756,5 +767,21 @@ class Vision : public Device { ///@} }; } // namespace v5 +namespace literals { +/** + * Constructs a Vision sensor from a litteral ending in _vis + * + * \return a pros::Vision for the corresponding port + * + * \b Example + * \code + * using namespace pros::literals; + * void opcontrol() { + * pros::Vision vision = 2_vis; //Makes an Vision sensor object on port 2 + * } + * \endcode + */ +const pros::Vision operator"" _vis(const unsigned long long int m); +} // namespace literals } // namespace pros #endif // _PROS_VISION_HPP_ diff --git a/project.pros b/project.pros index 17c08ae0..a8ebf39a 100644 --- a/project.pros +++ b/project.pros @@ -5,257 +5,595 @@ "target": "v5", "templates": { "kernel": { - "location": "/Users/rockychen/Library/Application Support/PROS/templates/kernel@4.0.7", + "location": "/home/mayank/.config/pros/templates/kernel@4.2.1", "metadata": { "cold_addr": "58720256", "cold_output": "bin/cold.package.bin", "hot_addr": "125829120", "hot_output": "bin/hot.package.bin", - "origin": "kernel-beta-mainline", + "origin": "pros-mainline", "output": "bin/monolith.bin" }, "name": "kernel", "py/object": "pros.conductor.templates.local_template.LocalTemplate", "supported_kernels": null, "system_files": [ - "include\\pros\\error.h", - "include\\pros\\device.h", - "firmware\\libpros.a", - "include\\pros\\ext_adi.h", - "firmware\\v5.ld", - "include\\pros\\rotation.h", - "include\\pros\\adi.h", - "include\\pros\\motor_group.hpp", - "include\\pros\\distance.h", - "include\\pros\\gps.h", - "include\\pros\\colors.h", - "include\\pros\\abstract_motor.hpp", - "firmware\\libm.a", - "include\\pros\\optical.h", - "include\\pros\\rotation.hpp", - "include\\pros\\rtos.h", - "include\\pros\\distance.hpp", - "include\\pros\\vision.hpp", - "include\\pros\\motors.hpp", - "include\\pros\\vision.h", - "firmware\\v5-hot.ld", - "firmware\\libc.a", - "include\\pros\\imu.h", - "include\\pros\\llemu.h", - "firmware\\v5-common.ld", - "include\\pros\\link.h", - "include\\pros\\adi.hpp", - "include\\pros\\misc.hpp", - "include\\pros\\llemu.hpp", - "include\\pros\\motors.h", - "include\\pros\\screen.h", - "include\\pros\\serial.h", - "include\\pros\\colors.hpp", + "include/pros/rotation.hpp", + "include/pros/rotation.h", + "include/pros/colors.hpp", + "include/pros/llemu.h", + "firmware/v5-common.ld", + "include/pros/imu.h", + "include/pros/vision.hpp", + "include/pros/abstract_motor.hpp", + "include/pros/misc.hpp", + "include/pros/adi.hpp", + "firmware/v5-hot.ld", + "include/pros/screen.h", + "include/pros/adi.h", + "include/pros/ai_vision.hpp", + "include/pros/gps.hpp", + "include/pros/rtos.hpp", + "include/pros/misc.h", + "include/pros/colors.h", + "include/pros/ai_vision.h", + "include/pros/imu.hpp", + "firmware/libpros.a", + "include/pros/motors.hpp", + "include/api.h", "common.mk", - "include\\pros\\misc.h", - "include\\pros\\screen.hpp", - "include\\pros\\apix.h", - "include\\pros\\rtos.hpp", - "include\\pros\\optical.hpp", - "include\\pros\\device.hpp", - "include\\pros\\imu.hpp", - "include\\pros\\gps.hpp", - "include\\api.h", - "include\\pros\\link.hpp", - "include\\pros\\serial.hpp" + "include/pros/link.h", + "include/pros/device.h", + "include/pros/link.hpp", + "include/pros/gps.h", + "include/pros/motor_group.hpp", + "include/pros/motors.h", + "include/pros/ext_adi.h", + "include/pros/optical.h", + "include/pros/serial.h", + "include/pros/distance.h", + "include/pros/llemu.hpp", + "include/pros/device.hpp", + "include/pros/rtos.h", + "firmware/libc.a", + "include/pros/vision.h", + "include/pros/screen.hpp", + "include/pros/error.h", + "include/pros/serial.hpp", + "firmware/v5.ld", + "include/pros/distance.hpp", + "include/pros/apix.h", + "firmware/libm.a", + "include/pros/optical.hpp", + "include/pros/version.h" ], "target": "v5", "user_files": [ - "include\\main.hpp", - "include\\main.h", ".gitignore", - "include\\main.hh", - "src\\main.cpp", - "src\\main.c", - "src\\main.cc", - "Makefile" + "src/main.cc", + "src/main.c", + "src/main.cpp", + "Makefile", + "include/main.hh", + "include/main.hpp", + "include/main.h" ], - "version": "4.0.7" + "version": "4.2.1" }, "liblvgl": { - "location": "/Users/rockychen/Library/Application Support/PROS/templates/liblvgl@8.3.8", + "location": "/home/mayank/.config/pros/templates/liblvgl@9.2.0", "metadata": { - "origin": "kernel-beta-mainline" + "origin": "pros-mainline" }, "name": "liblvgl", "py/object": "pros.conductor.templates.local_template.LocalTemplate", - "supported_kernels": ">=4.0.0", + "supported_kernels": ">=4.2.0", "system_files": [ - "include\\liblvgl\\extra\\others\\gridnav\\lv_gridnav.h", - "include\\liblvgl\\font\\lv_font_loader.h", - "include\\liblvgl\\misc\\lv_txt_ap.h", - "include\\liblvgl\\core\\lv_indev_scroll.h", - "include\\liblvgl\\misc\\lv_types.h", - "include\\liblvgl\\draw\\lv_img_decoder.h", - "include\\liblvgl\\extra\\libs\\freetype\\lv_freetype.h", - "include\\liblvgl\\extra\\widgets\\led\\lv_led.h", - "include\\liblvgl\\widgets\\lv_bar.h", - "include\\liblvgl\\draw\\sdl\\lv_draw_sdl_rect.h", - "include\\liblvgl\\draw\\nxp\\vglite\\lv_draw_vglite_blend.h", - "include\\liblvgl\\extra\\libs\\png\\lv_png.h", - "include\\liblvgl\\misc\\lv_printf.h", - "include\\liblvgl\\extra\\themes\\lv_themes.h", - "include\\liblvgl\\draw\\lv_draw_transform.h", - "include\\liblvgl\\lv_conf_checker.h", - "include\\liblvgl\\draw\\sdl\\lv_draw_sdl_priv.h", - "include\\liblvgl\\extra\\themes\\basic\\lv_theme_basic.h", - "include\\liblvgl\\extra\\widgets\\msgbox\\lv_msgbox.h", - "include\\liblvgl\\extra\\libs\\bmp\\lv_bmp.h", - "include\\liblvgl\\extra\\widgets\\span\\lv_span.h", - "include\\liblvgl\\misc\\lv_fs.h", - "include\\liblvgl\\misc\\lv_lru.h", - "include\\liblvgl\\core\\lv_obj_style.h", - "include\\liblvgl\\extra\\libs\\qrcode\\lv_qrcode.h", - "include\\liblvgl\\core\\lv_obj_class.h", - "include\\liblvgl\\lv_conf_internal.h", - "include\\liblvgl\\lv_conf.h", - "include\\liblvgl\\extra\\widgets\\lv_widgets.h", - "include\\liblvgl\\core\\lv_refr.h", - "include\\liblvgl\\extra\\widgets\\imgbtn\\lv_imgbtn.h", - "include\\liblvgl\\draw\\sdl\\lv_draw_sdl_stack_blur.h", - "include\\liblvgl\\lvgl.h", - "include\\liblvgl\\misc\\lv_anim_timeline.h", - "include\\liblvgl\\extra\\widgets\\spinner\\lv_spinner.h", - "include\\liblvgl\\extra\\widgets\\animimg\\lv_animimg.h", - "include\\liblvgl\\draw\\sdl\\lv_draw_sdl_texture_cache.h", - "include\\liblvgl\\extra\\libs\\fsdrv\\lv_fsdrv.h", - "include\\liblvgl\\draw\\nxp\\pxp\\lv_gpu_nxp_pxp.h", - "include\\liblvgl\\extra\\others\\fragment\\lv_fragment.h", - "include\\liblvgl\\extra\\widgets\\calendar\\lv_calendar_header_dropdown.h", - "include\\liblvgl\\draw\\lv_draw_triangle.h", - "include\\liblvgl\\lv_api_map.h", - "include\\liblvgl\\extra\\widgets\\chart\\lv_chart.h", - "include\\liblvgl\\extra\\widgets\\colorwheel\\lv_colorwheel.h", - "include\\liblvgl\\hal\\lv_hal_disp.h", - "include\\liblvgl\\draw\\sdl\\lv_draw_sdl_img.h", - "include\\liblvgl\\extra\\widgets\\tileview\\lv_tileview.h", - "include\\liblvgl\\widgets\\lv_btnmatrix.h", - "include\\liblvgl\\core\\lv_obj_scroll.h", - "include\\liblvgl\\extra\\layouts\\grid\\lv_grid.h", - "include\\liblvgl\\misc\\lv_area.h", - "include\\liblvgl\\widgets\\lv_dropdown.h", - "include\\liblvgl\\hal\\lv_hal.h", - "include\\liblvgl\\draw\\nxp\\lv_gpu_nxp.h", - "include\\liblvgl\\extra\\others\\imgfont\\lv_imgfont.h", - "include\\liblvgl\\misc\\lv_txt.h", - "include\\liblvgl\\core\\lv_obj.h", - "include\\liblvgl\\widgets\\lv_objx_templ.h", - "include\\liblvgl\\core\\lv_group.h", - "firmware\\liblvgl.a", - "include\\liblvgl\\font\\lv_font.h", - "include\\liblvgl\\draw\\sw\\lv_draw_sw_blend.h", - "include\\liblvgl\\draw\\lv_draw_label.h", - "include\\liblvgl\\extra\\libs\\gif\\gifdec.h", - "include\\liblvgl\\lv_conf.old.h", - "include\\liblvgl\\hal\\lv_hal_tick.h", - "include\\liblvgl\\draw\\sw\\lv_draw_sw_dither.h", - "include\\liblvgl\\widgets\\lv_textarea.h", - "include\\liblvgl\\extra\\libs\\gif\\lv_gif.h", - "include\\liblvgl\\llemu.h", - "include\\liblvgl\\misc\\lv_async.h", - "include\\liblvgl\\draw\\sw\\lv_draw_sw.h", - "include\\liblvgl\\draw\\lv_draw_mask.h", - "include\\liblvgl\\widgets\\lv_img.h", - "include\\liblvgl\\extra\\widgets\\tabview\\lv_tabview.h", - "include\\liblvgl\\core\\lv_obj_style_gen.h", - "include\\liblvgl\\extra\\others\\ime\\lv_ime_pinyin.h", - "include\\liblvgl\\misc\\lv_log.h", - "include\\liblvgl\\font\\lv_symbol_def.h", - "include\\liblvgl\\draw\\sdl\\lv_draw_sdl_utils.h", - "include\\liblvgl\\extra\\layouts\\flex\\lv_flex.h", - "include\\liblvgl\\misc\\lv_style_gen.h", - "include\\liblvgl\\extra\\libs\\sjpg\\tjpgd.h", - "include\\liblvgl\\draw\\lv_draw_rect.h", - "include\\liblvgl\\extra\\libs\\ffmpeg\\lv_ffmpeg.h", - "include\\liblvgl\\draw\\lv_draw_arc.h", - "include\\liblvgl\\misc\\lv_bidi.h", - "include\\liblvgl\\extra\\widgets\\win\\lv_win.h", - "include\\liblvgl\\draw\\lv_img_cache.h", - "include\\liblvgl\\core\\lv_obj_draw.h", - "include\\liblvgl\\extra\\others\\msg\\lv_msg.h", - "include\\liblvgl\\misc\\lv_ll.h", - "include\\liblvgl\\widgets\\lv_table.h", - "include\\liblvgl\\lv_conf_kconfig.h", - "include\\liblvgl\\draw\\lv_draw_layer.h", - "include\\liblvgl\\misc\\lv_assert.h", - "include\\liblvgl\\extra\\libs\\sjpg\\tjpgdcnf.h", - "include\\liblvgl\\widgets\\lv_checkbox.h", - "include\\liblvgl\\core\\lv_obj_tree.h", - "include\\liblvgl\\hal\\lv_hal_indev.h", - "include\\liblvgl\\widgets\\lv_label.h", - "include\\liblvgl\\misc\\lv_mem.h", - "include\\liblvgl\\widgets\\lv_btn.h", - "include\\liblvgl\\core\\lv_theme.h", - "include\\liblvgl\\font\\lv_font_fmt_txt.h", - "include\\liblvgl\\extra\\others\\snapshot\\lv_snapshot.h", - "include\\liblvgl\\draw\\sdl\\lv_draw_sdl_layer.h", - "include\\liblvgl\\extra\\libs\\qrcode\\qrcodegen.h", - "include\\liblvgl\\draw\\sw\\lv_draw_sw_gradient.h", - "include\\liblvgl\\misc\\lv_utils.h", - "include\\liblvgl\\draw\\nxp\\pxp\\lv_draw_pxp_blend.h", - "include\\liblvgl\\misc\\lv_style.h", - "include\\liblvgl\\widgets\\lv_line.h", - "include\\liblvgl\\widgets\\lv_slider.h", - "include\\liblvgl\\extra\\widgets\\meter\\lv_meter.h", - "include\\liblvgl\\draw\\arm2d\\lv_gpu_arm2d.h", - "include\\liblvgl\\extra\\libs\\rlottie\\lv_rlottie.h", - "include\\liblvgl\\extra\\others\\lv_others.h", - "include\\liblvgl\\extra\\layouts\\lv_layouts.h", - "include\\liblvgl\\extra\\widgets\\calendar\\lv_calendar_header_arrow.h", - "include\\liblvgl\\draw\\swm341_dma2d\\lv_gpu_swm341_dma2d.h", - "include\\liblvgl\\draw\\stm32_dma2d\\lv_gpu_stm32_dma2d.h", - "include\\liblvgl\\misc\\lv_anim.h", - "include\\liblvgl\\extra\\widgets\\calendar\\lv_calendar.h", - "include\\liblvgl\\misc\\lv_templ.h", - "include\\liblvgl\\extra\\widgets\\spinbox\\lv_spinbox.h", - "include\\liblvgl\\extra\\lv_extra.h", - "include\\liblvgl\\draw\\lv_draw_img.h", - "include\\liblvgl\\draw\\lv_img_buf.h", - "include\\liblvgl\\extra\\libs\\sjpg\\lv_sjpg.h", - "include\\liblvgl\\extra\\libs\\lv_libs.h", - "include\\liblvgl\\draw\\nxp\\vglite\\lv_draw_vglite_rect.h", - "include\\liblvgl\\llemu.hpp", - "include\\liblvgl\\draw\\lv_draw.h", - "include\\liblvgl\\extra\\themes\\mono\\lv_theme_mono.h", - "include\\liblvgl\\core\\lv_event.h", - "include\\liblvgl\\widgets\\lv_canvas.h", - "include\\liblvgl\\extra\\others\\monkey\\lv_monkey.h", - "include\\liblvgl\\draw\\sdl\\lv_draw_sdl_mask.h", - "include\\liblvgl\\draw\\nxp\\vglite\\lv_draw_vglite_arc.h", - "include\\liblvgl\\draw\\nxp\\pxp\\lv_gpu_nxp_pxp_osa.h", - "include\\liblvgl\\core\\lv_disp.h", - "include\\liblvgl\\draw\\sdl\\lv_draw_sdl_composite.h", - "include\\liblvgl\\widgets\\lv_switch.h", - "include\\liblvgl\\draw\\sdl\\lv_draw_sdl.h", - "include\\liblvgl\\core\\lv_indev.h", - "include\\liblvgl\\misc\\lv_timer.h", - "include\\liblvgl\\draw\\nxp\\vglite\\lv_gpu_nxp_vglite.h", - "include\\liblvgl\\extra\\widgets\\menu\\lv_menu.h", - "include\\liblvgl\\misc\\lv_math.h", - "include\\liblvgl\\misc\\lv_color.h", - "include\\liblvgl\\draw\\lv_draw_line.h", - "include\\liblvgl\\misc\\lv_gc.h", - "include\\liblvgl\\core\\lv_obj_pos.h", - "include\\liblvgl\\widgets\\lv_arc.h", - "include\\liblvgl\\extra\\themes\\default\\lv_theme_default.h", - "include\\liblvgl\\extra\\widgets\\list\\lv_list.h", - "include\\liblvgl\\widgets\\lv_roller.h", - "include\\liblvgl\\extra\\libs\\png\\lodepng.h", - "include\\liblvgl\\misc\\lv_tlsf.h", - "include\\liblvgl\\extra\\widgets\\keyboard\\lv_keyboard.h" + "include/liblvgl/libs/thorvg/tvgSvgLoaderCommon.h", + "include/liblvgl/libs/thorvg/tvgText.cpp", + "include/liblvgl/libs/thorvg/rapidjson/internal/biginteger.h", + "include/liblvgl/libs/thorvg/tvgSwCommon.h", + "include/liblvgl/draw/sw/lv_draw_sw_gradient.h", + "include/liblvgl/libs/svg/lv_svg_parser.h", + "include/liblvgl/draw/lv_image_decoder.h", + "include/liblvgl/libs/thorvg/tvgGlCanvas.cpp", + "include/liblvgl/libs/libpng/lv_libpng.h", + "include/liblvgl/libs/freetype/lv_freetype.h", + "include/liblvgl/libs/thorvg/rapidjson/fwd.h", + "include/liblvgl/tick/lv_tick.h", + "include/liblvgl/drivers/windows/lv_windows_context.h", + "include/liblvgl/misc/cache/lv_cache.h", + "include/liblvgl/libs/thorvg/thorvg_lottie.h", + "include/liblvgl/others/font_manager/lv_font_manager_utils.h", + "include/liblvgl/misc/lv_lru.h", + "include/liblvgl/lv_init.h", + "include/liblvgl/libs/thorvg/tvgLottieParser.h", + "include/liblvgl/libs/thorvg/tvgIteratorAccessor.h", + "include/liblvgl/widgets/arc/lv_arc.h", + "include/liblvgl/libs/thorvg/tvgCapi.cpp", + "include/liblvgl/misc/lv_assert.h", + "include/liblvgl/libs/thorvg/tvgRender.h", + "include/liblvgl/libs/thorvg/tvgLines.cpp", + "include/liblvgl/widgets/led/lv_led_private.h", + "include/liblvgl/indev/lv_indev.h", + "include/liblvgl/widgets/image/lv_image.h", + "include/liblvgl/core/lv_group_private.h", + "include/liblvgl/drivers/qnx/lv_qnx.h", + "include/liblvgl/libs/thorvg/rapidjson/internal/ieee754.h", + "include/liblvgl/libs/thorvg/rapidjson/filereadstream.h", + "include/liblvgl/libs/tjpgd/lv_tjpgd.h", + "include/liblvgl/draw/lv_draw_triangle.h", + "include/liblvgl/widgets/tileview/lv_tileview_private.h", + "include/liblvgl/widgets/calendar/lv_calendar_chinese.h", + "include/liblvgl/misc/lv_iter.h", + "include/liblvgl/drivers/windows/lv_windows_display.h", + "include/liblvgl/draw/nxp/pxp/lv_pxp_osa.h", + "include/liblvgl/drivers/x11/lv_x11.h", + "include/liblvgl/osal/lv_os_private.h", + "include/liblvgl/draw/nxp/vglite/lv_vglite_buf.h", + "include/liblvgl/core/lv_obj_draw.h", + "firmware/liblvgl.a", + "include/liblvgl/libs/thorvg/tvgSvgCssStyle.h", + "include/liblvgl/draw/nxp/pxp/lv_draw_pxp.h", + "include/liblvgl/libs/qrcode/qrcodegen.h", + "include/liblvgl/misc/lv_utils.h", + "include/liblvgl/misc/lv_bidi_private.h", + "include/liblvgl/draw/nxp/vglite/lv_vglite_utils.h", + "include/liblvgl/libs/thorvg/tvgCanvas.cpp", + "include/liblvgl/draw/sdl/lv_draw_sdl.h", + "include/liblvgl/widgets/table/lv_table_private.h", + "include/liblvgl/libs/rlottie/lv_rlottie.h", + "include/liblvgl/draw/nema_gfx/lv_draw_nema_gfx.h", + "include/liblvgl/libs/thorvg/tvgStr.h", + "include/liblvgl/widgets/calendar/lv_calendar_header_arrow.h", + "include/liblvgl/drivers/display/st7735/lv_st7735.h", + "include/liblvgl/widgets/textarea/lv_textarea.h", + "include/liblvgl/libs/thorvg/rapidjson/msinttypes/inttypes.h", + "include/liblvgl/libs/thorvg/tvgPicture.cpp", + "include/liblvgl/widgets/menu/lv_menu.h", + "include/liblvgl/osal/lv_os.h", + "include/liblvgl/widgets/slider/lv_slider.h", + "include/liblvgl/draw/renesas/dave2d/lv_draw_dave2d_utils.h", + "include/liblvgl/libs/rle/lv_rle.h", + "include/liblvgl/widgets/button/lv_button.h", + "include/liblvgl/libs/thorvg/tvgSwRasterNeon.h", + "include/liblvgl/libs/thorvg/tvgSwMemPool.cpp", + "include/liblvgl/widgets/calendar/lv_calendar.h", + "include/liblvgl/draw/sw/lv_draw_sw_mask.h", + "include/liblvgl/layouts/lv_layout.h", + "include/liblvgl/others/file_explorer/lv_file_explorer_private.h", + "include/liblvgl/stdlib/lv_mem_private.h", + "include/liblvgl/libs/thorvg/tvgSvgLoader.cpp", + "include/liblvgl/misc/lv_event.h", + "include/liblvgl/widgets/slider/lv_slider_private.h", + "include/liblvgl/libs/rlottie/lv_rlottie_private.h", + "include/liblvgl/widgets/line/lv_line.h", + "include/liblvgl/indev/lv_indev_scroll.h", + "include/liblvgl/libs/thorvg/tvgLottieExpressions.h", + "include/liblvgl/font/lv_font.h", + "include/liblvgl/widgets/imagebutton/lv_imagebutton.h", + "include/liblvgl/libs/thorvg/tvgSwMath.cpp", + "include/liblvgl/draw/lv_draw_label.h", + "include/liblvgl/draw/lv_draw_label_private.h", + "include/liblvgl/libs/svg/lv_svg_token.h", + "include/liblvgl/others/monkey/lv_monkey_private.h", + "include/liblvgl/core/lv_obj_event_private.h", + "include/liblvgl/misc/lv_event_private.h", + "include/liblvgl/widgets/calendar/lv_calendar_private.h", + "include/liblvgl/widgets/roller/lv_roller.h", + "include/liblvgl/draw/sw/arm2d/lv_draw_sw_arm2d.h", + "include/liblvgl/widgets/tileview/lv_tileview.h", + "include/liblvgl/libs/fsdrv/lv_fsdrv.h", + "include/liblvgl/drivers/sdl/lv_sdl_keyboard.h", + "include/liblvgl/indev/lv_indev_private.h", + "include/liblvgl/others/imgfont/lv_imgfont.h", + "include/liblvgl/llemu.h", + "include/liblvgl/font/lv_font_fmt_txt_private.h", + "include/liblvgl/others/snapshot/lv_snapshot.h", + "include/liblvgl/libs/thorvg/tvgSvgUtil.cpp", + "include/liblvgl/others/gridnav/lv_gridnav.h", + "include/liblvgl/libs/gif/gifdec.h", + "include/liblvgl/widgets/property/lv_style_properties.h", + "include/liblvgl/widgets/property/lv_obj_property_names.h", + "include/liblvgl/misc/lv_array.h", + "include/liblvgl/core/lv_refr.h", + "include/liblvgl/misc/lv_timer_private.h", + "include/liblvgl/libs/thorvg/tvgCompressor.cpp", + "include/liblvgl/draw/sw/blend/lv_draw_sw_blend_private.h", + "include/liblvgl/display/lv_display_private.h", + "include/liblvgl/libs/thorvg/tvgArray.h", + "include/liblvgl/libs/thorvg/tvgLottieProperty.h", + "include/liblvgl/draw/nema_gfx/lv_draw_nema_gfx_utils.h", + "include/liblvgl/widgets/keyboard/lv_keyboard_private.h", + "include/liblvgl/libs/tjpgd/tjpgd.h", + "include/liblvgl/widgets/canvas/lv_canvas.h", + "include/liblvgl/libs/thorvg/rapidjson/msinttypes/stdint.h", + "include/liblvgl/draw/vg_lite/lv_vg_lite_utils.h", + "include/liblvgl/llemu.hpp", + "include/liblvgl/widgets/label/lv_label.h", + "include/liblvgl/draw/nxp/vglite/lv_draw_vglite.h", + "include/liblvgl/themes/default/lv_theme_default.h", + "include/liblvgl/tick/lv_tick_private.h", + "include/liblvgl/drivers/sdl/lv_sdl_mouse.h", + "include/liblvgl/libs/thorvg/tvgXmlParser.h", + "include/liblvgl/core/lv_global.h", + "include/liblvgl/libs/thorvg/tvgLottieInterpolator.cpp", + "include/liblvgl/draw/lv_draw_private.h", + "include/liblvgl/libs/thorvg/tvgFill.h", + "include/liblvgl/widgets/spinbox/lv_spinbox.h", + "include/liblvgl/lv_conf_kconfig.h", + "include/liblvgl/draw/vg_lite/lv_vg_lite_decoder.h", + "include/liblvgl/drivers/sdl/lv_sdl_mousewheel.h", + "include/liblvgl/draw/sw/lv_draw_sw_mask_private.h", + "include/liblvgl/draw/renesas/dave2d/lv_draw_dave2d.h", + "include/liblvgl/libs/thorvg/tvgSwRle.cpp", + "include/liblvgl/libs/thorvg/rapidjson/internal/meta.h", + "include/liblvgl/libs/thorvg/tvgLottieLoader.h", + "include/liblvgl/stdlib/lv_string.h", + "include/liblvgl/draw/sw/blend/neon/lv_blend_neon.h", + "include/liblvgl/misc/lv_profiler.h", + "include/liblvgl/libs/barcode/lv_barcode_private.h", + "include/liblvgl/libs/barcode/code128.h", + "include/liblvgl/misc/lv_bidi.h", + "include/liblvgl/libs/thorvg/add_lvgl_if.sh", + "include/liblvgl/draw/vg_lite/lv_draw_vg_lite.h", + "include/liblvgl/libs/thorvg/rapidjson/cursorstreamwrapper.h", + "include/liblvgl/draw/dma2d/lv_draw_dma2d.h", + "include/liblvgl/libs/thorvg/tvgInlist.h", + "include/liblvgl/drivers/sdl/lv_sdl_window.h", + "include/liblvgl/libs/thorvg/tvgLottieLoader.cpp", + "include/liblvgl/others/sysmon/lv_sysmon.h", + "include/liblvgl/drivers/nuttx/lv_nuttx_cache.h", + "include/liblvgl/widgets/buttonmatrix/lv_buttonmatrix.h", + "include/liblvgl/libs/thorvg/tvgLottieModel.h", + "include/liblvgl/misc/cache/lv_cache_lru_rb.h", + "include/liblvgl/drivers/glfw/lv_glfw_window_private.h", + "include/liblvgl/draw/lv_draw_triangle_private.h", + "include/liblvgl/draw/lv_draw_image_private.h", + "include/liblvgl/draw/vg_lite/lv_vg_lite_grad.h", + "include/liblvgl/font/lv_symbol_def.h", + "include/liblvgl/libs/thorvg/tvgLottieParserHandler.cpp", + "include/liblvgl/draw/vg_lite/lv_vg_lite_math.h", + "include/liblvgl/draw/lv_draw_buf_private.h", + "include/liblvgl/libs/gif/lv_gif.h", + "include/liblvgl/libs/thorvg/rapidjson/error/en.h", + "include/liblvgl/themes/lv_theme.h", + "include/liblvgl/libs/thorvg/rapidjson/writer.h", + "include/liblvgl/drivers/nuttx/lv_nuttx_touchscreen.h", + "include/liblvgl/core/lv_obj_property.h", + "include/liblvgl/libs/thorvg/tvgSaver.cpp", + "include/liblvgl/widgets/calendar/lv_calendar_header_dropdown.h", + "include/liblvgl/core/lv_obj_private.h", + "include/liblvgl/libs/thorvg/tvgLottieAnimation.cpp", + "include/liblvgl/misc/lv_tree.h", + "include/liblvgl/widgets/scale/lv_scale_private.h", + "include/liblvgl/draw/lv_draw_rect.h", + "include/liblvgl/libs/tjpgd/tjpgdcnf.h", + "include/liblvgl/misc/lv_rb.h", + "include/liblvgl/libs/thorvg/tvgAnimation.h", + "include/liblvgl/drivers/libinput/lv_xkb_private.h", + "include/liblvgl/libs/thorvg/tvgLottieParserHandler.h", + "include/liblvgl/libs/fsdrv/lv_fs_arduino_sd.cpp", + "include/liblvgl/core/lv_obj_style.h", + "include/liblvgl/libs/gif/lv_gif_private.h", + "include/liblvgl/draw/sw/lv_draw_sw_gradient_private.h", + "include/liblvgl/libs/thorvg/tvgStr.cpp", + "include/liblvgl/drivers/libinput/lv_libinput.h", + "include/liblvgl/libs/thorvg/rapidjson/ostreamwrapper.h", + "include/liblvgl/misc/cache/lv_cache_entry_private.h", + "include/liblvgl/lvgl.h", + "include/liblvgl/libs/thorvg/tvgFill.cpp", + "include/liblvgl/libs/freetype/arial.ttf", + "include/liblvgl/others/vg_lite_tvg/vg_lite.h", + "include/liblvgl/widgets/spinbox/lv_spinbox_private.h", + "include/liblvgl/misc/lv_math.h", + "include/liblvgl/libs/thorvg/tvgMath.cpp", + "include/liblvgl/libs/thorvg/tvgRender.cpp", + "include/liblvgl/draw/vg_lite/lv_vg_lite_stroke.h", + "include/liblvgl/widgets/dropdown/lv_dropdown_private.h", + "include/liblvgl/core/lv_obj_scroll.h", + "include/liblvgl/libs/libjpeg_turbo/lv_libjpeg_turbo.h", + "include/liblvgl/libs/fsdrv/lv_fs_arduino_esp_littlefs.cpp", + "include/liblvgl/themes/mono/lv_theme_mono.h", + "include/liblvgl/misc/lv_circle_buf.h", + "include/liblvgl/drivers/nuttx/lv_nuttx_fbdev.h", + "include/liblvgl/libs/thorvg/tvgCanvas.h", + "include/liblvgl/widgets/imagebutton/lv_imagebutton_private.h", + "include/liblvgl/libs/thorvg/rapidjson/istreamwrapper.h", + "include/liblvgl/libs/thorvg/tvgAccessor.cpp", + "include/liblvgl/libs/thorvg/tvgMath.h", + "include/liblvgl/misc/lv_async.h", + "include/liblvgl/themes/lv_theme_private.h", + "include/liblvgl/libs/thorvg/tvgSvgSceneBuilder.h", + "include/liblvgl/libs/thorvg/tvgSwRaster.cpp", + "include/liblvgl/draw/lv_draw_arc.h", + "include/liblvgl/libs/thorvg/tvgInitializer.cpp", + "include/liblvgl/widgets/textarea/lv_textarea_private.h", + "include/liblvgl/widgets/label/lv_label_private.h", + "include/liblvgl/drivers/windows/lv_windows_input.h", + "include/liblvgl/libs/thorvg/tvgLottieInterpolator.h", + "include/liblvgl/drivers/glfw/lv_opengles_driver.h", + "include/liblvgl/lv_version.h", + "include/liblvgl/libs/ffmpeg/lv_ffmpeg_private.h", + "include/liblvgl/libs/thorvg/rapidjson/stream.h", + "include/liblvgl/drivers/nuttx/lv_nuttx_image_cache.h", + "include/liblvgl/draw/sw/blend/lv_draw_sw_blend_to_argb8888.h", + "include/liblvgl/libs/thorvg/tvgLoadModule.h", + "include/liblvgl/widgets/line/lv_line_private.h", + "include/liblvgl/misc/cache/lv_cache_private.h", + "include/liblvgl/libs/thorvg/tvgFrameModule.h", + "include/liblvgl/osal/lv_freertos.h", + "include/liblvgl/libs/thorvg/tvgSvgSceneBuilder.cpp", + "include/liblvgl/osal/lv_rtthread.h", + "include/liblvgl/draw/vg_lite/lv_draw_vg_lite_type.h", + "include/liblvgl/widgets/animimage/lv_animimage_private.h", + "include/liblvgl/libs/barcode/lv_barcode.h", + "include/liblvgl/stdlib/builtin/lv_tlsf.h", + "include/liblvgl/widgets/objx_templ/lv_objx_templ.h", + "include/liblvgl/others/fragment/README.md", + "include/liblvgl/libs/svg/lv_svg.h", + "include/liblvgl/widgets/image/lv_image_private.h", + "include/liblvgl/libs/qrcode/lv_qrcode.h", + "include/liblvgl/libs/thorvg/rapidjson/uri.h", + "include/liblvgl/osal/lv_os_none.h", + "include/liblvgl/misc/cache/lv_image_header_cache.h", + "include/liblvgl/widgets/span/lv_span_private.h", + "include/liblvgl/widgets/switch/lv_switch_private.h", + "include/liblvgl/libs/thorvg/tvgTaskScheduler.h", + "include/liblvgl/drivers/nuttx/lv_nuttx_profiler.h", + "include/liblvgl/draw/sw/arm2d/lv_draw_sw_helium.h", + "include/liblvgl/drivers/nuttx/lv_nuttx_libuv.h", + "include/liblvgl/libs/thorvg/tvgShape.h", + "include/liblvgl/drivers/nuttx/lv_nuttx_lcd.h", + "include/liblvgl/drivers/nuttx/lv_nuttx_entry.h", + "include/liblvgl/draw/sw/blend/lv_draw_sw_blend.h", + "include/liblvgl/widgets/canvas/lv_canvas_private.h", + "include/liblvgl/libs/thorvg/rapidjson/memorystream.h", + "include/liblvgl/others/fragment/lv_fragment_private.h", + "include/liblvgl/widgets/table/lv_table.h", + "include/liblvgl/drivers/windows/lv_windows_input_private.h", + "include/liblvgl/misc/lv_matrix.h", + "include/liblvgl/libs/thorvg/rapidjson/rapidjson.h", + "include/liblvgl/others/ime/lv_ime_pinyin_private.h", + "include/liblvgl/drivers/display/tft_espi/lv_tft_espi.cpp", + "include/liblvgl/libs/thorvg/tvgSwRasterTexmap.h", + "include/liblvgl/libs/thorvg/config.h", + "include/liblvgl/stdlib/builtin/lv_tlsf_private.h", + "include/liblvgl/libs/thorvg/rapidjson/internal/pow10.h", + "include/liblvgl/libs/thorvg/rapidjson/memorybuffer.h", + "include/liblvgl/lv_api_map_v9_1.h", + "include/liblvgl/libs/thorvg/rapidjson/internal/strtod.h", + "include/liblvgl/widgets/win/lv_win.h", + "include/liblvgl/misc/cache/lv_cache_entry.h", + "include/liblvgl/libs/thorvg/tvgCompressor.h", + "include/liblvgl/libs/thorvg/tvgPaint.cpp", + "include/liblvgl/misc/lv_style_private.h", + "include/liblvgl/draw/lv_draw_rect_private.h", + "include/liblvgl/libs/thorvg/tvgSvgLoader.h", + "include/liblvgl/widgets/span/lv_span.h", + "include/liblvgl/misc/lv_anim_private.h", + "include/liblvgl/draw/sw/lv_draw_sw_private.h", + "include/liblvgl/libs/bmp/lv_bmp.h", + "include/liblvgl/drivers/display/st7789/lv_st7789.h", + "include/liblvgl/libs/thorvg/tvgLoader.cpp", + "include/liblvgl/lvgl_private.h", + "include/liblvgl/libs/thorvg/rapidjson/internal/swap.h", + "include/liblvgl/draw/sw/blend/lv_draw_sw_blend_to_i1.h", + "include/liblvgl/misc/lv_rb_private.h", + "include/liblvgl/others/vg_lite_tvg/vg_lite_tvg.cpp", + "include/liblvgl/widgets/msgbox/lv_msgbox.h", + "include/liblvgl/drivers/glfw/lv_glfw_window.h", + "include/liblvgl/libs/thorvg/tvgRawLoader.h", + "include/liblvgl/themes/simple/lv_theme_simple.h", + "include/liblvgl/draw/sw/blend/lv_draw_sw_blend_to_al88.h", + "include/liblvgl/draw/lv_draw_buf.h", + "include/liblvgl/others/fragment/lv_fragment.h", + "include/liblvgl/libs/thorvg/tvgXmlParser.cpp", + "include/liblvgl/draw/dma2d/lv_draw_dma2d_private.h", + "include/liblvgl/misc/lv_color.h", + "include/liblvgl/libs/thorvg/rapidjson/pointer.h", + "include/liblvgl/libs/thorvg/rapidjson/encodedstream.h", + "include/liblvgl/others/observer/lv_observer.h", + "include/liblvgl/misc/lv_palette.h", + "include/liblvgl/libs/thorvg/tvgScene.h", + "include/liblvgl/draw/sw/blend/helium/lv_blend_helium.h", + "include/liblvgl/misc/lv_ll.h", + "include/liblvgl/libs/thorvg/tvgSwShape.cpp", + "include/liblvgl/drivers/display/st_ltdc/lv_st_ltdc.h", + "include/liblvgl/libs/bin_decoder/lv_bin_decoder.h", + "include/liblvgl/libs/lodepng/lv_lodepng.h", + "include/liblvgl/others/font_manager/lv_font_manager_recycle.h", + "include/liblvgl/libs/thorvg/rapidjson/stringbuffer.h", + "include/liblvgl/libs/thorvg/tvgSvgPath.h", + "include/liblvgl/stdlib/lv_sprintf.h", + "include/liblvgl/widgets/roller/lv_roller_private.h", + "include/liblvgl/font/lv_binfont_loader.h", + "include/liblvgl/libs/thorvg/tvgSwRasterAvx.h", + "include/liblvgl/libs/thorvg/rapidjson/internal/dtoa.h", + "include/liblvgl/drivers/libinput/lv_libinput_private.h", + "include/liblvgl/draw/lv_draw_mask.h", + "include/liblvgl/drivers/lv_drivers.h", + "include/liblvgl/libs/freetype/ftmodule.h", + "include/liblvgl/widgets/tabview/lv_tabview_private.h", + "include/liblvgl/draw/sw/lv_draw_sw.h", + "include/liblvgl/misc/lv_color_op_private.h", + "include/liblvgl/libs/freetype/ftoption.h", + "include/liblvgl/widgets/lottie/lv_lottie.h", + "include/liblvgl/layouts/flex/lv_flex.h", + "include/liblvgl/draw/sw/blend/lv_draw_sw_blend_to_l8.h", + "include/liblvgl/libs/thorvg/tvgRawLoader.cpp", + "include/liblvgl/widgets/calendar/lv_calendar_chinese_private.h", + "include/liblvgl/display/lv_display.h", + "include/liblvgl/drivers/display/st7796/lv_st7796.h", + "include/liblvgl/osal/lv_windows.h", + "include/liblvgl/misc/lv_text.h", + "include/liblvgl/misc/lv_style_gen.h", + "include/liblvgl/widgets/tabview/lv_tabview.h", + "include/liblvgl/draw/nxp/vglite/lv_vglite_path.h", + "include/liblvgl/libs/thorvg/tvgSwImage.cpp", + "include/liblvgl/core/lv_obj_pos.h", + "include/liblvgl/others/ime/lv_ime_pinyin.h", + "include/liblvgl/drivers/display/lcd/lv_lcd_generic_mipi.h", + "include/liblvgl/widgets/list/lv_list.h", + "include/liblvgl/layouts/lv_layout_private.h", + "include/liblvgl/libs/thorvg/tvgLottieBuilder.h", + "include/liblvgl/others/font_manager/lv_font_manager.h", + "include/liblvgl/stdlib/lv_mem.h", + "include/liblvgl/libs/thorvg/tvgSvgUtil.h", + "include/liblvgl/libs/thorvg/tvgLottieExpressions.cpp", + "include/liblvgl/libs/thorvg/tvgSwRasterC.h", + "include/liblvgl/core/lv_obj_scroll_private.h", + "include/liblvgl/misc/lv_fs_private.h", + "include/liblvgl/libs/thorvg/tvgSwFill.cpp", + "include/liblvgl/libs/thorvg/tvgSwRenderer.h", + "include/liblvgl/drivers/display/tft_espi/lv_tft_espi.h", + "include/liblvgl/widgets/dropdown/lv_dropdown.h", + "include/liblvgl/libs/thorvg/rapidjson/document.h", + "include/liblvgl/libs/thorvg/rapidjson/prettywriter.h", + "include/liblvgl/libs/thorvg/rapidjson/internal/strfunc.h", + "include/liblvgl/drivers/glfw/lv_opengles_debug.h", + "include/liblvgl/draw/lv_draw_vector_private.h", + "include/liblvgl/libs/thorvg/rapidjson/reader.h", + "include/liblvgl/libs/thorvg/tvgSaveModule.h", + "include/liblvgl/libs/thorvg/rapidjson/error/error.h", + "include/liblvgl/libs/thorvg/tvgSvgCssStyle.cpp", + "include/liblvgl/draw/lv_draw_mask_private.h", + "include/liblvgl/libs/thorvg/tvgTaskScheduler.cpp", + "include/liblvgl/libs/thorvg/tvgSvgPath.cpp", + "include/liblvgl/layouts/grid/lv_grid.h", + "include/liblvgl/drivers/glfw/lv_opengles_texture.h", + "include/liblvgl/widgets/checkbox/lv_checkbox_private.h", + "include/liblvgl/libs/tiny_ttf/lv_tiny_ttf.h", + "include/liblvgl/draw/sw/blend/arm2d/lv_blend_arm2d.h", + "include/liblvgl/misc/lv_timer.h", + "include/liblvgl/draw/nxp/pxp/lv_pxp_utils.h", + "include/liblvgl/widgets/switch/lv_switch.h", + "include/liblvgl/widgets/led/lv_led.h", + "include/liblvgl/libs/lz4/LICENSE", + "include/liblvgl/libs/thorvg/rapidjson/internal/regex.h", + "include/liblvgl/libs/thorvg/rapidjson/internal/diyfp.h", + "include/liblvgl/draw/nxp/pxp/lv_pxp_cfg.h", + "include/liblvgl/libs/gif/gifdec_mve.h", + "include/liblvgl/misc/lv_profiler_builtin_private.h", + "include/liblvgl/libs/thorvg/tvgBinaryDesc.h", + "include/liblvgl/core/lv_obj_tree.h", + "include/liblvgl/misc/lv_fs.h", + "include/liblvgl/libs/thorvg/tvgPaint.h", + "include/liblvgl/osal/lv_pthread.h", + "include/liblvgl/libs/thorvg/tvgText.h", + "include/liblvgl/draw/lv_draw_line.h", + "include/liblvgl/libs/ffmpeg/lv_ffmpeg.h", + "include/liblvgl/misc/lv_anim_timeline.h", + "include/liblvgl/draw/lv_image_dsc.h", + "include/liblvgl/widgets/chart/lv_chart.h", + "include/liblvgl/libs/thorvg/thorvg_capi.h", + "include/liblvgl/widgets/win/lv_win_private.h", + "include/liblvgl/core/lv_group.h", + "include/liblvgl/draw/nxp/vglite/lv_vglite_matrix.h", + "include/liblvgl/libs/thorvg/tvgAnimation.cpp", + "include/liblvgl/misc/lv_text_ap.h", + "include/liblvgl/widgets/menu/lv_menu_private.h", + "include/liblvgl/draw/lv_image_decoder_private.h", + "include/liblvgl/draw/lv_draw_vector.h", + "include/liblvgl/lv_conf.old.h", + "include/liblvgl/widgets/spinner/lv_spinner.h", + "include/liblvgl/libs/lz4/lz4.h", + "include/liblvgl/libs/thorvg/tvgCommon.h", + "include/liblvgl/libs/thorvg/tvgLottieBuilder.cpp", + "include/liblvgl/core/lv_obj.h", + "include/liblvgl/drivers/display/fb/lv_linux_fbdev.h", + "include/liblvgl/drivers/wayland/lv_wayland_smm.h", + "include/liblvgl/drivers/display/ili9341/lv_ili9341.h", + "include/liblvgl/libs/thorvg/rapidjson/schema.h", + "include/liblvgl/core/lv_obj_class_private.h", + "include/liblvgl/misc/lv_profiler_builtin.h", + "include/liblvgl/libs/thorvg/rapidjson/internal/stack.h", + "include/liblvgl/libs/qrcode/lv_qrcode_private.h", + "include/liblvgl/libs/thorvg/rapidjson/filewritestream.h", + "include/liblvgl/others/sysmon/lv_sysmon_private.h", + "include/liblvgl/libs/thorvg/tvgSwCanvas.cpp", + "include/liblvgl/libs/thorvg/tvgPicture.h", + "include/liblvgl/widgets/lottie/lv_lottie_private.h", + "include/liblvgl/widgets/checkbox/lv_checkbox.h", + "include/liblvgl/others/observer/lv_observer_private.h", + "include/liblvgl/widgets/arc/lv_arc_private.h", + "include/liblvgl/widgets/bar/lv_bar.h", + "include/liblvgl/libs/thorvg/tvgLoader.h", + "include/liblvgl/lv_conf_internal.h", + "include/liblvgl/widgets/msgbox/lv_msgbox_private.h", + "include/liblvgl/misc/lv_log.h", + "include/liblvgl/libs/freetype/lv_freetype_private.h", + "include/liblvgl/drivers/display/renesas_glcdc/lv_renesas_glcdc.h", + "include/liblvgl/widgets/keyboard/lv_keyboard.h", + "include/liblvgl/libs/thorvg/tvgLottieModel.cpp", + "include/liblvgl/others/monkey/lv_monkey.h", + "include/liblvgl/draw/sw/blend/lv_draw_sw_blend_to_rgb565.h", + "include/liblvgl/widgets/bar/lv_bar_private.h", + "include/liblvgl/core/lv_obj_style_gen.h", + "include/liblvgl/libs/thorvg/tvgLottieParser.cpp", + "include/liblvgl/drivers/libinput/lv_xkb.h", + "include/liblvgl/misc/lv_area.h", + "include/liblvgl/misc/lv_area_private.h", + "include/liblvgl/misc/lv_templ.h", + "include/liblvgl/misc/lv_style.h", + "include/liblvgl/drivers/display/drm/lv_linux_drm.h", + "include/liblvgl/core/lv_obj_class.h", + "include/liblvgl/libs/thorvg/tvgLock.h", + "include/liblvgl/libs/tiny_ttf/stb_rect_pack.h", + "include/liblvgl/core/lv_refr_private.h", + "include/liblvgl/font/lv_font_fmt_txt.h", + "include/liblvgl/osal/lv_mqx.h", + "include/liblvgl/lv_api_map_v8.h", + "include/liblvgl/widgets/chart/lv_chart_private.h", + "include/liblvgl/draw/vg_lite/lv_vg_lite_pending.h", + "include/liblvgl/libs/thorvg/rapidjson/allocators.h", + "include/liblvgl/misc/lv_types.h", + "include/liblvgl/widgets/animimage/lv_animimage.h", + "include/liblvgl/libs/thorvg/tvgSwStroke.cpp", + "include/liblvgl/misc/lv_anim.h", + "include/liblvgl/widgets/scale/lv_scale.h", + "include/liblvgl/drivers/README.md", + "include/liblvgl/libs/thorvg/rapidjson/encodings.h", + "include/liblvgl/draw/lv_draw_image.h", + "include/liblvgl/libs/thorvg/rapidjson/internal/clzll.h", + "include/liblvgl/libs/thorvg/tvgSwRenderer.cpp", + "include/liblvgl/draw/vg_lite/lv_vg_lite_path.h", + "include/liblvgl/lv_conf.h", + "include/liblvgl/draw/opengles/lv_draw_opengles.h", + "include/liblvgl/libs/thorvg/thorvg.h", + "include/liblvgl/libs/lodepng/lodepng.h", + "include/liblvgl/osal/lv_cmsis_rtos2.h", + "include/liblvgl/libs/thorvg/tvgWgCanvas.cpp", + "include/liblvgl/core/lv_obj_draw_private.h", + "include/liblvgl/libs/thorvg/rapidjson/internal/itoa.h", + "include/liblvgl/misc/lv_color_op.h", + "include/liblvgl/widgets/buttonmatrix/lv_buttonmatrix_private.h", + "include/liblvgl/core/lv_obj_style_private.h", + "include/liblvgl/others/file_explorer/lv_file_explorer.h", + "include/liblvgl/drivers/evdev/lv_evdev.h", + "include/liblvgl/draw/lv_draw.h", + "include/liblvgl/draw/sw/blend/lv_draw_sw_blend_to_rgb888.h", + "include/liblvgl/libs/tiny_ttf/stb_truetype_htcw.h", + "include/liblvgl/drivers/wayland/lv_wayland.h", + "include/liblvgl/misc/cache/lv_image_cache.h", + "include/liblvgl/misc/lv_text_private.h", + "include/liblvgl/widgets/button/lv_button_private.h", + "include/liblvgl/drivers/sdl/lv_sdl_private.h", + "include/liblvgl/libs/svg/lv_svg_render.h", + "include/liblvgl/libs/thorvg/tvgScene.cpp", + "include/liblvgl/core/lv_obj_event.h", + "include/liblvgl/lv_api_map_v9_0.h", + "include/liblvgl/libs/thorvg/tvgShape.cpp", + "include/liblvgl/libs/thorvg/tvgLines.h" ], "target": "v5", "user_files": [], - "version": "8.3.8" + "version": "9.2.0" } }, "upload_options": { "slot": 8 - } + }, + "use_early_access": false } } \ No newline at end of file diff --git a/src/VOSS/selector/Selector.cpp b/src/VOSS/selector/Selector.cpp index 63cd7fb6..e4f35ae4 100644 --- a/src/VOSS/selector/Selector.cpp +++ b/src/VOSS/selector/Selector.cpp @@ -1,6 +1,7 @@ #include "VOSS/selector/Selector.hpp" #include "liblvgl/lvgl.h" #include "pros/rtos.hpp" +#include namespace voss::selector { @@ -18,17 +19,17 @@ const char* skillsMap[] = {"Skills", ""}; // Rendering the buttons for the autonomous routines // Add logic that if the button is pressed, that autonomous routine is selected void render() { - lv_btnmatrix_clear_btn_ctrl_all(redBtnm, LV_BTNMATRIX_CTRL_CHECKED); - lv_btnmatrix_clear_btn_ctrl_all(blueBtnm, LV_BTNMATRIX_CTRL_CHECKED); - lv_btnmatrix_clear_btn_ctrl_all(skillsBtnm, LV_BTNMATRIX_CTRL_CHECKED); + lv_buttonmatrix_clear_button_ctrl_all(redBtnm, LV_BUTTONMATRIX_CTRL_CHECKED); + lv_buttonmatrix_clear_button_ctrl_all(blueBtnm, LV_BUTTONMATRIX_CTRL_CHECKED); + lv_buttonmatrix_clear_button_ctrl_all(skillsBtnm, LV_BUTTONMATRIX_CTRL_CHECKED); if (auton == 0) { // skills - lv_btnmatrix_set_btn_ctrl(skillsBtnm, 0, LV_BTNMATRIX_CTRL_CHECKED); + lv_buttonmatrix_set_button_ctrl(skillsBtnm, 0, LV_BUTTONMATRIX_CTRL_CHECKED); } else if (auton > 0) { // red - lv_btnmatrix_set_btn_ctrl(redBtnm, auton - 1, - LV_BTNMATRIX_CTRL_CHECKED); + lv_buttonmatrix_set_button_ctrl(redBtnm, auton - 1, + LV_BUTTONMATRIX_CTRL_CHECKED); } else { // blue - lv_btnmatrix_set_btn_ctrl(blueBtnm, -auton - 1, - LV_BTNMATRIX_CTRL_CHECKED); + lv_buttonmatrix_set_button_ctrl(blueBtnm, -auton - 1, + LV_BUTTONMATRIX_CTRL_CHECKED); } } @@ -48,18 +49,18 @@ void init(int default_auton, const char** autons) { auton = default_auton; lv_theme_t* th = - lv_theme_default_init(lv_disp_get_default(), lv_color_hex(0xCFB53B), + lv_theme_default_init(lv_display_get_default(), lv_color_hex(0xCFB53B), lv_color_hex(0xCFB53B), true, LV_FONT_DEFAULT); - redBtnm = lv_btnmatrix_create(lv_scr_act()); - lv_btnmatrix_set_map(redBtnm, btnmMap); + redBtnm = lv_buttonmatrix_create(lv_screen_active()); + lv_buttonmatrix_set_map(redBtnm, btnmMap); lv_obj_set_size(redBtnm, 480, 70); lv_obj_align(redBtnm, LV_ALIGN_CENTER, 0, -70); lv_obj_add_event_cb( redBtnm, [](lv_event_t* e) { auton_mtx.take(); - auton = lv_btnmatrix_get_selected_btn(redBtnm) + 1; + auton = lv_buttonmatrix_get_selected_button(redBtnm) + 1; render(); auton_mtx.give(); }, @@ -74,15 +75,15 @@ void init(int default_auton, const char** autons) { lv_obj_set_style_bg_color(redBtnm, lv_color_hex(0xB71C1C), LV_PART_ITEMS); /*Add content to the tabs*/ - blueBtnm = lv_btnmatrix_create(lv_scr_act()); - lv_btnmatrix_set_map(blueBtnm, btnmMap); + blueBtnm = lv_buttonmatrix_create(lv_screen_active()); + lv_buttonmatrix_set_map(blueBtnm, btnmMap); lv_obj_set_size(blueBtnm, 480, 70); lv_obj_align(blueBtnm, LV_ALIGN_CENTER, 0, 0); lv_obj_add_event_cb( blueBtnm, [](lv_event_t* e) { auton_mtx.take(); - auton = -lv_btnmatrix_get_selected_btn(blueBtnm) - 1; + auton = -lv_buttonmatrix_get_selected_button(blueBtnm) - 1; render(); auton_mtx.give(); }, @@ -97,8 +98,8 @@ void init(int default_auton, const char** autons) { LV_PART_ITEMS | LV_STATE_CHECKED); lv_obj_set_style_bg_color(blueBtnm, lv_color_hex(0x0D47A1), LV_PART_ITEMS); - skillsBtnm = lv_btnmatrix_create(lv_scr_act()); - lv_btnmatrix_set_map(skillsBtnm, skillsMap); + skillsBtnm = lv_buttonmatrix_create(lv_screen_active()); + lv_buttonmatrix_set_map(skillsBtnm, skillsMap); lv_obj_set_size(skillsBtnm, 480, 70); lv_obj_align(skillsBtnm, LV_ALIGN_CENTER, 0, 70); lv_obj_add_event_cb( @@ -135,4 +136,4 @@ int get_auton() { return ret; } -} // namespace voss::selector \ No newline at end of file +} // namespace voss::selector