kwo pushed a commit to branch master. http://git.enlightenment.org/legacy/imlib2.git/commit/?id=5261da4a6e0be39315e0751d07fb095535faddb4
commit 5261da4a6e0be39315e0751d07fb095535faddb4 Author: Kim Woelders <[email protected]> Date: Sat Jan 22 19:45:21 2022 +0100 Move some code as suggested in source --- src/lib/Makefile.am | 1 + src/lib/font.h | 32 +----- src/lib/font_main.c | 235 -------------------------------------- src/lib/{font_main.c => object.c} | 210 +++++----------------------------- src/lib/object.h | 27 +++++ 5 files changed, 56 insertions(+), 449 deletions(-) diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 7201f45..8fb468c 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -34,6 +34,7 @@ image_tags.c \ line.c \ loaders.c loaders.h \ modules.c \ +object.c object.h \ polygon.c \ rectangle.c \ rgbadraw.c rgbadraw.h \ diff --git a/src/lib/font.h b/src/lib/font.h index 520e299..cf8630e 100644 --- a/src/lib/font.h +++ b/src/lib/font.h @@ -3,23 +3,7 @@ #include FT_GLYPH_H #include "common.h" - -/* TODO separate fonts and data stuff */ - -typedef struct _Imlib_Object_List { - struct _Imlib_Object_List *next, *prev; -} Imlib_Object_List; - -typedef struct { - int population; - Imlib_Object_List *buckets[256]; -} Imlib_Hash; - -typedef struct { - Imlib_Object_List _list_data; - char *key; - void *data; -} Imlib_Hash_El; +#include "object.h" typedef struct _Imlib_Font { Imlib_Object_List _list_data; @@ -104,17 +88,3 @@ void __imlib_font_draw(ImlibImage * dst, DATA32 col, ImlibFont * fn, int x, int y, const char *text, int *nextx, int *nexty, int clx, int cly, int clw, int clh); - -/* data manipulation */ - -void *__imlib_object_list_prepend(void *in_list, void *in_item); -void *__imlib_object_list_remove(void *in_list, void *in_item); -Imlib_Hash *__imlib_hash_add(Imlib_Hash * hash, const char *key, - const void *data); -void *__imlib_hash_find(Imlib_Hash * hash, const char *key); -void __imlib_hash_free(Imlib_Hash * hash); -void __imlib_hash_foreach(Imlib_Hash * hash, - int (*func)(Imlib_Hash * hash, - const char *key, - void *data, void *fdata), - const void *fdata); diff --git a/src/lib/font_main.c b/src/lib/font_main.c index 2e5e47c..e5ae648 100644 --- a/src/lib/font_main.c +++ b/src/lib/font_main.c @@ -17,12 +17,6 @@ FT_Library ft_lib; -static int __imlib_hash_gen(const char *key); -static int imlib_list_alloc_error(void); - -static int _imlib_hash_alloc_error = 0; -static int _imlib_list_alloc_error = 0; - void __imlib_font_init(void) { @@ -174,232 +168,3 @@ __imlib_font_utf8_get_next(unsigned char *buf, int *iindex) *iindex = index; return r; } - -/* TODO put this somewhere else */ - -void * -__imlib_object_list_prepend(void *in_list, void *in_item) -{ - Imlib_Object_List *new_l; - Imlib_Object_List *list, *item; - - list = in_list; - item = in_item; - new_l = item; - new_l->prev = NULL; - if (!list) - { - new_l->next = NULL; - return new_l; - } - new_l->next = list; - list->prev = new_l; - return new_l; -} - -void * -__imlib_object_list_remove(void *in_list, void *in_item) -{ - Imlib_Object_List *return_l; - Imlib_Object_List *list = in_list; - Imlib_Object_List *item = in_item; - - /* checkme */ - if (!list) - return list; - if (!item) - return list; - - if (item->next) - item->next->prev = item->prev; - - if (item->prev) - { - item->prev->next = item->next; - return_l = list; - } - else - { - return_l = item->next; - } - - item->next = NULL; - item->prev = NULL; - - return return_l; -} - -static int -__imlib_hash_gen(const char *key) -{ - unsigned int hash_num = 0; - const unsigned char *ptr; - - if (!key) - return 0; - - for (ptr = (unsigned char *)key; *ptr; ptr++) - hash_num ^= (int)(*ptr); - - hash_num &= 0xff; - return (int)hash_num; -} - -Imlib_Hash * -__imlib_hash_add(Imlib_Hash * hash, const char *key, const void *data) -{ - int hash_num; - Imlib_Hash_El *el; - - _imlib_hash_alloc_error = 0; - if (!hash) - { - hash = calloc(1, sizeof(Imlib_Hash)); - if (!hash) - { - _imlib_hash_alloc_error = 1; - return NULL; - } - } - if (!(el = malloc(sizeof(Imlib_Hash_El)))) - { - if (hash->population <= 0) - { - free(hash); - hash = NULL; - } - _imlib_hash_alloc_error = 1; - return hash; - }; - if (key) - { - el->key = strdup(key); - if (!el->key) - { - free(el); - _imlib_hash_alloc_error = 1; - return hash; - } - hash_num = __imlib_hash_gen(key); - } - else - { - el->key = NULL; - hash_num = 0; - } - el->data = (void *)data; - - hash->buckets[hash_num] = - __imlib_object_list_prepend(hash->buckets[hash_num], el); - - if (imlib_list_alloc_error()) - { - _imlib_hash_alloc_error = 1; - free(el->key); - free(el); - return hash; - } - hash->population++; - return hash; -} - -void * -__imlib_hash_find(Imlib_Hash * hash, const char *key) -{ - int hash_num; - Imlib_Hash_El *el; - Imlib_Object_List *l; - - _imlib_hash_alloc_error = 0; - if (!hash) - return NULL; - hash_num = __imlib_hash_gen(key); - for (l = hash->buckets[hash_num]; l; l = l->next) - { - el = (Imlib_Hash_El *) l; - if (((el->key) && (key) && (!strcmp(el->key, key))) - || ((!el->key) && (!key))) - { - if (l != hash->buckets[hash_num]) - { - /* FIXME: move to front of list without alloc */ - hash->buckets[hash_num] = - __imlib_object_list_remove(hash->buckets[hash_num], el); - hash->buckets[hash_num] = - __imlib_object_list_prepend(hash->buckets[hash_num], el); - if (imlib_list_alloc_error()) - { - _imlib_hash_alloc_error = 1; - return el->data; - } - } - return el->data; - } - } - return NULL; -} - -static int -__imlib_hash_size(Imlib_Hash * hash) -{ - if (!hash) - return 0; - return 256; -} - -void -__imlib_hash_free(Imlib_Hash * hash) -{ - int i, size; - - if (!hash) - return; - - size = __imlib_hash_size(hash); - for (i = 0; i < size; i++) - { - Imlib_Hash_El *el, *el_next; - - for (el = (Imlib_Hash_El *) hash->buckets[i]; el; el = el_next) - { - el_next = (Imlib_Hash_El *) el->_list_data.next; - free(el->key); - free(el); - } - } - free(hash); -} - -void -__imlib_hash_foreach(Imlib_Hash * hash, int (*func)(Imlib_Hash * hash, - const char *key, - void *data, void *fdata), - const void *fdata) -{ - int i, size; - - if (!hash) - return; - size = __imlib_hash_size(hash); - for (i = 0; i < size; i++) - { - Imlib_Object_List *l, *next_l; - - for (l = hash->buckets[i]; l;) - { - Imlib_Hash_El *el; - - next_l = l->next; - el = (Imlib_Hash_El *) l; - if (!func(hash, el->key, el->data, (void *)fdata)) - return; - l = next_l; - } - } -} - -int -imlib_list_alloc_error(void) -{ - return _imlib_list_alloc_error; -} diff --git a/src/lib/font_main.c b/src/lib/object.c similarity index 50% copy from src/lib/font_main.c copy to src/lib/object.c index 2e5e47c..ec3cf52 100644 --- a/src/lib/font_main.c +++ b/src/lib/object.c @@ -1,182 +1,23 @@ -#include "config.h" - -#include <ft2build.h> -#include FT_FREETYPE_H -#include FT_GLYPH_H -#include <math.h> +#include <stdlib.h> #include <string.h> -#include <sys/types.h> - -#include "blend.h" -#include "colormod.h" -#include "common.h" -#include "font.h" -#include "image.h" -#include "rgbadraw.h" -#include "rotate.h" -FT_Library ft_lib; +#include "object.h" -static int __imlib_hash_gen(const char *key); -static int imlib_list_alloc_error(void); +typedef struct { + Imlib_Object_List _list_data; + char *key; + void *data; +} Imlib_Hash_El; -static int _imlib_hash_alloc_error = 0; static int _imlib_list_alloc_error = 0; +static int _imlib_hash_alloc_error = 0; -void -__imlib_font_init(void) -{ - static int initialised = 0; - int error; - - if (initialised) - return; - error = FT_Init_FreeType(&ft_lib); - if (error) - return; - initialised = 1; -} - -int -__imlib_font_ascent_get(ImlibFont * fn) -{ - int val; - int ret; - - val = (int)fn->ft.face->ascender; - fn->ft.face->units_per_EM = 2048; /* nasy hack - need to have correct - * val */ - ret = - (val * fn->ft.face->size->metrics.y_scale) / - (fn->ft.face->units_per_EM * fn->ft.face->units_per_EM); - return ret; -} - -int -__imlib_font_descent_get(ImlibFont * fn) -{ - int val; - int ret; - - val = -(int)fn->ft.face->descender; - fn->ft.face->units_per_EM = 2048; /* nasy hack - need to have correct - * val */ - ret = - (val * fn->ft.face->size->metrics.y_scale) / - (fn->ft.face->units_per_EM * fn->ft.face->units_per_EM); - return ret; -} - -int -__imlib_font_max_ascent_get(ImlibFont * fn) -{ - int val; - int ret; - - val = (int)fn->ft.face->bbox.yMax; - fn->ft.face->units_per_EM = 2048; /* nasy hack - need to have correct - * val */ - ret = - (val * fn->ft.face->size->metrics.y_scale) / - (fn->ft.face->units_per_EM * fn->ft.face->units_per_EM); - return ret; -} - -int -__imlib_font_max_descent_get(ImlibFont * fn) -{ - int val; - int ret; - - val = (int)fn->ft.face->bbox.yMin; - fn->ft.face->units_per_EM = 2048; /* nasy hack - need to have correct - * val */ - ret = - (val * fn->ft.face->size->metrics.y_scale) / - (fn->ft.face->units_per_EM * fn->ft.face->units_per_EM); - return ret; -} - -int -__imlib_font_get_line_advance(ImlibFont * fn) -{ - int val; - int ret; - - val = (int)fn->ft.face->height; - fn->ft.face->units_per_EM = 2048; /* nasy hack - need to have correct - * val */ - ret = - (val * fn->ft.face->size->metrics.y_scale) / - (fn->ft.face->units_per_EM * fn->ft.face->units_per_EM); - return ret; -} - -int -__imlib_font_utf8_get_next(unsigned char *buf, int *iindex) +static int +__imlib_list_alloc_error(void) { - /* Reads UTF8 bytes from @buf, starting at *@index and returns the code - * point of the next valid code point. @index is updated ready for the - * next call. - * - * * Returns 0 to indicate an error (e.g. invalid UTF8) */ - - int index = *iindex, r; - unsigned char d = buf[index++], d2, d3, d4; - - if (!d) - return 0; - if (d < 0x80) - { - *iindex = index; - return d; - } - if ((d & 0xe0) == 0xc0) - { - /* 2 byte */ - d2 = buf[index++]; - if ((d2 & 0xc0) != 0x80) - return 0; - r = d & 0x1f; /* copy lower 5 */ - r <<= 6; - r |= (d2 & 0x3f); /* copy lower 6 */ - } - else if ((d & 0xf0) == 0xe0) - { - /* 3 byte */ - d2 = buf[index++]; - d3 = buf[index++]; - if ((d2 & 0xc0) != 0x80 || (d3 & 0xc0) != 0x80) - return 0; - r = d & 0x0f; /* copy lower 4 */ - r <<= 6; - r |= (d2 & 0x3f); - r <<= 6; - r |= (d3 & 0x3f); - } - else - { - /* 4 byte */ - d2 = buf[index++]; - d3 = buf[index++]; - d4 = buf[index++]; - if ((d2 & 0xc0) != 0x80 || (d3 & 0xc0) != 0x80 || (d4 & 0xc0) != 0x80) - return 0; - r = d & 0x0f; /* copy lower 4 */ - r <<= 6; - r |= (d2 & 0x3f); - r <<= 6; - r |= (d3 & 0x3f); - r <<= 6; - r |= (d4 & 0x3f); - - } - *iindex = index; - return r; + return _imlib_list_alloc_error; } -/* TODO put this somewhere else */ - void * __imlib_object_list_prepend(void *in_list, void *in_item) { @@ -194,6 +35,7 @@ __imlib_object_list_prepend(void *in_list, void *in_item) } new_l->next = list; list->prev = new_l; + return new_l; } @@ -252,6 +94,7 @@ __imlib_hash_add(Imlib_Hash * hash, const char *key, const void *data) Imlib_Hash_El *el; _imlib_hash_alloc_error = 0; + if (!hash) { hash = calloc(1, sizeof(Imlib_Hash)); @@ -261,6 +104,7 @@ __imlib_hash_add(Imlib_Hash * hash, const char *key, const void *data) return NULL; } } + if (!(el = malloc(sizeof(Imlib_Hash_El)))) { if (hash->population <= 0) @@ -270,7 +114,8 @@ __imlib_hash_add(Imlib_Hash * hash, const char *key, const void *data) } _imlib_hash_alloc_error = 1; return hash; - }; + } + if (key) { el->key = strdup(key); @@ -287,19 +132,22 @@ __imlib_hash_add(Imlib_Hash * hash, const char *key, const void *data) el->key = NULL; hash_num = 0; } + el->data = (void *)data; hash->buckets[hash_num] = __imlib_object_list_prepend(hash->buckets[hash_num], el); - if (imlib_list_alloc_error()) + if (__imlib_list_alloc_error()) { _imlib_hash_alloc_error = 1; free(el->key); free(el); return hash; } + hash->population++; + return hash; } @@ -311,8 +159,10 @@ __imlib_hash_find(Imlib_Hash * hash, const char *key) Imlib_Object_List *l; _imlib_hash_alloc_error = 0; + if (!hash) return NULL; + hash_num = __imlib_hash_gen(key); for (l = hash->buckets[hash_num]; l; l = l->next) { @@ -327,7 +177,7 @@ __imlib_hash_find(Imlib_Hash * hash, const char *key) __imlib_object_list_remove(hash->buckets[hash_num], el); hash->buckets[hash_num] = __imlib_object_list_prepend(hash->buckets[hash_num], el); - if (imlib_list_alloc_error()) + if (__imlib_list_alloc_error()) { _imlib_hash_alloc_error = 1; return el->data; @@ -336,6 +186,7 @@ __imlib_hash_find(Imlib_Hash * hash, const char *key) return el->data; } } + return NULL; } @@ -371,15 +222,14 @@ __imlib_hash_free(Imlib_Hash * hash) } void -__imlib_hash_foreach(Imlib_Hash * hash, int (*func)(Imlib_Hash * hash, - const char *key, - void *data, void *fdata), - const void *fdata) +__imlib_hash_foreach(Imlib_Hash * hash, + Imlib_Hash_Func * func, const void *fdata) { int i, size; if (!hash) return; + size = __imlib_hash_size(hash); for (i = 0; i < size; i++) { @@ -397,9 +247,3 @@ __imlib_hash_foreach(Imlib_Hash * hash, int (*func)(Imlib_Hash * hash, } } } - -int -imlib_list_alloc_error(void) -{ - return _imlib_list_alloc_error; -} diff --git a/src/lib/object.h b/src/lib/object.h new file mode 100644 index 0000000..11b1e8e --- /dev/null +++ b/src/lib/object.h @@ -0,0 +1,27 @@ +#ifndef OBJECT_H +#define OBJECT_H + +typedef struct _Imlib_Object_List { + struct _Imlib_Object_List *next, *prev; +} Imlib_Object_List; + +typedef struct { + int population; + Imlib_Object_List *buckets[256]; +} Imlib_Hash; + +typedef int (Imlib_Hash_Func) (Imlib_Hash * hash, const char *key, + void *data, void *fdata); + +void *__imlib_object_list_prepend(void *in_list, void *in_item); +void *__imlib_object_list_remove(void *in_list, void *in_item); + +Imlib_Hash *__imlib_hash_add(Imlib_Hash * hash, const char *key, + const void *data); +void *__imlib_hash_find(Imlib_Hash * hash, const char *key); +void __imlib_hash_free(Imlib_Hash * hash); +void __imlib_hash_foreach(Imlib_Hash * hash, + Imlib_Hash_Func * func, + const void *fdata); + +#endif /* OBJECT_H */ --
