From 06881fd1b3cab84831b4d0bf531ef6a2b45c5343 Mon Sep 17 00:00:00 2001 From: TangoCash Date: Mon, 2 May 2016 22:19:33 +0200 Subject: [PATCH] reformat new source code --- widget_ttf.c | 351 +++++++++++++++++++++++++++------------------------ widget_ttf.h | 3 +- 2 files changed, 190 insertions(+), 164 deletions(-) diff --git a/widget_ttf.c b/widget_ttf.c index 2229414..df08126 100644 --- a/widget_ttf.c +++ b/widget_ttf.c @@ -85,104 +85,118 @@ static void widget_ttf_render(const char *Name, WIDGET_TTF * Image) /* clear bitmap */ - if (Image->bitmap) { - int i; - for (i = 0; i < Image->height * Image->width; i++) { - RGBA empty = {.R = 0x00,.G = 0x00,.B = 0x00,.A = 0x00 }; - Image->bitmap[i] = empty; - } + if (Image->bitmap) + { + int i; + for (i = 0; i < Image->height * Image->width; i++) + { + RGBA empty = {.R = 0x00,.G = 0x00,.B = 0x00,.A = 0x00 }; + Image->bitmap[i] = empty; + } } /* reload image only on first call or on explicit reload request */ - if (Image->gdImage == NULL || P2N(&Image->reload)) { - - /* free previous image */ - if (Image->gdImage) { - gdImageDestroy(Image->gdImage); - Image->gdImage = NULL; - } - - //replace with expression and use as text - text = P2S(&Image->value); - font = P2S(&Image->font); - size = P2N(&Image->size); - - err = gdImageStringFT(NULL,&brect[0],0,font,size,0.,0,0,text); - - x = brect[2]-brect[6] + 6; - y = brect[3]-brect[7] + 6; - Image->gdImage = gdImageCreateTrueColor(x,y); - gdImageSaveAlpha(Image->gdImage, 1); - - if (Image->gdImage == NULL) { - error("Warning: Image %s: Create failed!", Name); - return; - } - trans = gdImageColorAllocateAlpha(Image->gdImage, 0, 0, 0, 127); - gdImageFill(Image->gdImage, 0, 0, trans); - - fcolor = P2S(&Image->fcolor); - - l = strtoul(fcolor, &e, 16); - r = (l >> 16) & 0xff; - g = (l >> 8) & 0xff; - b = l & 0xff; - - color = gdImageColorAllocate(Image->gdImage, r, g, b); - - x = 3 - brect[6]; - y = 3 - brect[7]; - err = gdImageStringFT(Image->gdImage,&brect[0],color,font,size,0.0,x,y,text); + if (Image->gdImage == NULL || P2N(&Image->reload)) + { + + /* free previous image */ + if (Image->gdImage) + { + gdImageDestroy(Image->gdImage); + Image->gdImage = NULL; + } + + //replace with expression and use as text + text = P2S(&Image->value); + font = P2S(&Image->font); + size = P2N(&Image->size); + + err = gdImageStringFT(NULL,&brect[0],0,font,size,0.,0,0,text); + + x = brect[2]-brect[6] + 6; + y = brect[3]-brect[7] + 6; + Image->gdImage = gdImageCreateTrueColor(x,y); + gdImageSaveAlpha(Image->gdImage, 1); + + if (Image->gdImage == NULL) + { + error("Warning: Image %s: Create failed!", Name); + return; + } + trans = gdImageColorAllocateAlpha(Image->gdImage, 0, 0, 0, 127); + gdImageFill(Image->gdImage, 0, 0, trans); + + fcolor = P2S(&Image->fcolor); + + l = strtoul(fcolor, &e, 16); + r = (l >> 16) & 0xff; + g = (l >> 8) & 0xff; + b = l & 0xff; + + color = gdImageColorAllocate(Image->gdImage, r, g, b); + + x = 3 - brect[6]; + y = 3 - brect[7]; + err = gdImageStringFT(Image->gdImage,&brect[0],color,font,size,0.0,x,y,text); } /* maybe resize bitmap */ gdImage = Image->gdImage; - if (gdImage->sx > Image->width) { - Image->width = gdImage->sx; - free(Image->bitmap); - Image->bitmap = NULL; + if (gdImage->sx > Image->width) + { + Image->width = gdImage->sx; + free(Image->bitmap); + Image->bitmap = NULL; } - if (gdImage->sy > Image->height) { - Image->height = gdImage->sy; - free(Image->bitmap); - Image->bitmap = NULL; + if (gdImage->sy > Image->height) + { + Image->height = gdImage->sy; + free(Image->bitmap); + Image->bitmap = NULL; } - if (Image->bitmap == NULL && Image->width > 0 && Image->height > 0) { - int i = Image->width * Image->height * sizeof(Image->bitmap[0]); - Image->bitmap = malloc(i); - if (Image->bitmap == NULL) { - error("Warning: Image %s: malloc(%d) failed: %s", Name, i, strerror(errno)); - return; - } - for (i = 0; i < Image->height * Image->width; i++) { - RGBA empty = {.R = 0x00,.G = 0x00,.B = 0x00,.A = 0x00 }; - Image->bitmap[i] = empty; - } + if (Image->bitmap == NULL && Image->width > 0 && Image->height > 0) + { + int i = Image->width * Image->height * sizeof(Image->bitmap[0]); + Image->bitmap = malloc(i); + if (Image->bitmap == NULL) + { + error("Warning: Image %s: malloc(%d) failed: %s", Name, i, strerror(errno)); + return; + } + for (i = 0; i < Image->height * Image->width; i++) + { + RGBA empty = {.R = 0x00,.G = 0x00,.B = 0x00,.A = 0x00 }; + Image->bitmap[i] = empty; + } } /* finally really render it */ inverted = P2N(&Image->inverted); - if (P2N(&Image->visible)) { - for (x = 0; x < gdImage->sx; x++) { - for (y = 0; y < gdImage->sy; y++) { - int p = gdImageGetTrueColorPixel(gdImage, x, y); - int a = gdTrueColorGetAlpha(p); - int i = y * Image->width + x; - Image->bitmap[i].R = gdTrueColorGetRed(p); - Image->bitmap[i].G = gdTrueColorGetGreen(p); - Image->bitmap[i].B = gdTrueColorGetBlue(p); - /* GD's alpha is 0 (opaque) to 127 (tranparanet) */ - /* our alpha is 0 (transparent) to 255 (opaque) */ - Image->bitmap[i].A = (a == 127) ? 0 : 255 - 2 * a; - if (inverted) { - Image->bitmap[i].R = 255 - Image->bitmap[i].R; - Image->bitmap[i].G = 255 - Image->bitmap[i].G; - Image->bitmap[i].B = 255 - Image->bitmap[i].B; - } - } - } + if (P2N(&Image->visible)) + { + for (x = 0; x < gdImage->sx; x++) + { + for (y = 0; y < gdImage->sy; y++) + { + int p = gdImageGetTrueColorPixel(gdImage, x, y); + int a = gdTrueColorGetAlpha(p); + int i = y * Image->width + x; + Image->bitmap[i].R = gdTrueColorGetRed(p); + Image->bitmap[i].G = gdTrueColorGetGreen(p); + Image->bitmap[i].B = gdTrueColorGetBlue(p); + /* GD's alpha is 0 (opaque) to 127 (tranparanet) */ + /* our alpha is 0 (transparent) to 255 (opaque) */ + Image->bitmap[i].A = (a == 127) ? 0 : 255 - 2 * a; + if (inverted) + { + Image->bitmap[i].R = 255 - Image->bitmap[i].R; + Image->bitmap[i].G = 255 - Image->bitmap[i].G; + Image->bitmap[i].B = 255 - Image->bitmap[i].B; + } + } + } } } @@ -193,30 +207,32 @@ static void widget_ttf_update(void *Self) WIDGET_TTF *Image = W->data; /* process the parent only */ - if (W->parent == NULL) { - - /* evaluate properties */ - property_eval(&Image->value); - property_eval(&Image->size); - property_eval(&Image->font); - property_eval(&Image->fcolor); - property_eval(&Image->update); - property_eval(&Image->reload); - property_eval(&Image->visible); - property_eval(&Image->inverted); - - /* render image into bitmap */ - widget_ttf_render(W->name, Image); + if (W->parent == NULL) + { + + /* evaluate properties */ + property_eval(&Image->value); + property_eval(&Image->size); + property_eval(&Image->font); + property_eval(&Image->fcolor); + property_eval(&Image->update); + property_eval(&Image->reload); + property_eval(&Image->visible); + property_eval(&Image->inverted); + + /* render image into bitmap */ + widget_ttf_render(W->name, Image); } /* finally, draw it! */ if (W->class->draw) - W->class->draw(W); + W->class->draw(W); /* add a new one-shot timer */ - if (P2N(&Image->update) > 0) { - timer_add_widget(widget_ttf_update, Self, P2N(&Image->update), 1); + if (P2N(&Image->update) > 0) + { + timer_add_widget(widget_ttf_update, Self, P2N(&Image->update), 1); } } @@ -228,46 +244,50 @@ int widget_ttf_init(WIDGET * Self) WIDGET_TTF *Image; /* re-use the parent if one exists */ - if (Self->parent == NULL) { - - /* prepare config section */ - /* strlen("Widget:")=7 */ - section = malloc(strlen(Self->name) + 8); - strcpy(section, "Widget:"); - strcat(section, Self->name); - - Image = malloc(sizeof(WIDGET_TTF)); - memset(Image, 0, sizeof(WIDGET_TTF)); - - /* initial size */ - Image->width = 0; - Image->height = 0; - Image->bitmap = NULL; - - /* load properties */ - property_load(section, "expression", "Samsung", &Image->value); - property_load(section, "size", "40", &Image->size); - property_load(section, "font", NULL, &Image->font); - property_load(section, "fcolor", "ff0000", &Image->fcolor); - property_load(section, "update", "100", &Image->update); - property_load(section, "reload", "0", &Image->reload); - property_load(section, "visible", "1", &Image->visible); - property_load(section, "inverted", "0", &Image->inverted); - - /* sanity checks */ - if (!property_valid(&Image->font)) { - error("Warning: widget %s has no font", section); - } - - free(section); - Self->data = Image; - Self->x2 = Self->col + Image->width; - Self->y2 = Self->row + Image->height; - - } else { - - /* re-use the parent */ - Self->data = Self->parent->data; + if (Self->parent == NULL) + { + + /* prepare config section */ + /* strlen("Widget:")=7 */ + section = malloc(strlen(Self->name) + 8); + strcpy(section, "Widget:"); + strcat(section, Self->name); + + Image = malloc(sizeof(WIDGET_TTF)); + memset(Image, 0, sizeof(WIDGET_TTF)); + + /* initial size */ + Image->width = 0; + Image->height = 0; + Image->bitmap = NULL; + + /* load properties */ + property_load(section, "expression", "Samsung", &Image->value); + property_load(section, "size", "40", &Image->size); + property_load(section, "font", NULL, &Image->font); + property_load(section, "fcolor", "ff0000", &Image->fcolor); + property_load(section, "update", "100", &Image->update); + property_load(section, "reload", "0", &Image->reload); + property_load(section, "visible", "1", &Image->visible); + property_load(section, "inverted", "0", &Image->inverted); + + /* sanity checks */ + if (!property_valid(&Image->font)) + { + error("Warning: widget %s has no font", section); + } + + free(section); + Self->data = Image; + Self->x2 = Self->col + Image->width; + Self->y2 = Self->row + Image->height; + + } + else + { + + /* re-use the parent */ + Self->data = Self->parent->data; } @@ -280,28 +300,32 @@ int widget_ttf_init(WIDGET * Self) int widget_ttf_quit(WIDGET * Self) { - if (Self) { - /* do not deallocate child widget! */ - if (Self->parent == NULL) { - if (Self->data) { - WIDGET_TTF *Image = Self->data; - if (Image->gdImage) { - gdImageDestroy(Image->gdImage); - Image->gdImage = NULL; - } - free(Image->bitmap); - property_free(&Image->value); - property_free(&Image->size); - property_free(&Image->font); - property_free(&Image->fcolor); - property_free(&Image->update); - property_free(&Image->reload); - property_free(&Image->visible); - property_free(&Image->inverted); - free(Self->data); - Self->data = NULL; - } - } + if (Self) + { + /* do not deallocate child widget! */ + if (Self->parent == NULL) + { + if (Self->data) + { + WIDGET_TTF *Image = Self->data; + if (Image->gdImage) + { + gdImageDestroy(Image->gdImage); + Image->gdImage = NULL; + } + free(Image->bitmap); + property_free(&Image->value); + property_free(&Image->size); + property_free(&Image->font); + property_free(&Image->fcolor); + property_free(&Image->update); + property_free(&Image->reload); + property_free(&Image->visible); + property_free(&Image->inverted); + free(Self->data); + Self->data = NULL; + } + } } return 0; @@ -310,7 +334,8 @@ int widget_ttf_quit(WIDGET * Self) -WIDGET_CLASS Widget_Truetype = { +WIDGET_CLASS Widget_Truetype = +{ .name = "Truetype", .type = WIDGET_TYPE_XY, .init = widget_ttf_init, diff --git a/widget_ttf.h b/widget_ttf.h index c385c45..f53dfe9 100644 --- a/widget_ttf.h +++ b/widget_ttf.h @@ -28,7 +28,8 @@ #include "widget.h" #include "rgb.h" -typedef struct WIDGET_TTF { +typedef struct WIDGET_TTF +{ void *gdImage; /* raw gd image */ RGBA *bitmap; /* image bitmap */ int width, height; /* size of the image */ -- 2.39.5