From acf83d208426ee60b8f818e1c3b6527476761982 Mon Sep 17 00:00:00 2001 From: Hazel P Date: Fri, 8 Jan 2021 00:02:05 -0800 Subject: [PATCH 1/2] Add XFT support to textwidth --- gadgets/config.mk | 17 +++++- gadgets/textwidth.c | 33 +++++++++++- gadgets/textwidth.old | 117 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+), 4 deletions(-) create mode 100644 gadgets/textwidth.old diff --git a/gadgets/config.mk b/gadgets/config.mk index b6fc39f..097b238 100644 --- a/gadgets/config.mk +++ b/gadgets/config.mk @@ -8,9 +8,22 @@ X11INC = /usr/X11R6/include INCS = -I. -I/usr/include -I${X11INC} X11LIB = /usr/X11R6/lib -LIBS = -L/usr/lib -CFLAGS = -Os ${INCS} +# Configure the features you want to be supported +# Only one of the following options has to be uncommented, +# all others must be commented! +# +# Uncomment: Remove # from the beginning of respective lines +# Comment : Add # to the beginning of the respective lines + +## Option 1: No XFT +#LIBS = -L/usr/lib +#CFLAGS = -Os ${INCS} + +## Option 2: With XFT +LIBS = -L/usr/lib `pkg-config --libs xft` +CFLAGS = -Os ${INCS} -DDZEN_XFT `pkg-config --cflags xft` + LDFLAGS = ${LIBS} # compiler and linker diff --git a/gadgets/textwidth.c b/gadgets/textwidth.c index 6f95f35..521bc0c 100644 --- a/gadgets/textwidth.c +++ b/gadgets/textwidth.c @@ -27,6 +27,9 @@ THE SOFTWARE. #include #include #include +#ifdef DZEN_XFT +#include +#endif typedef struct _Fnt { XFontStruct *xfont; @@ -34,6 +37,11 @@ typedef struct _Fnt { int ascent; int descent; int height; +#ifdef DZEN_XFT + XftFont *xftfont; + XGlyphInfo *extents; + int width; +#endif } Fnt; Fnt font; @@ -42,19 +50,25 @@ Display *dpy; unsigned int textw(const char *text, unsigned int len) { XRectangle r; - +#ifndef DZEN_XFT if(font.set) { XmbTextExtents(font.set, text, len, NULL, &r); return r.width; } return XTextWidth(font.xfont, text, len); +#else + XftTextExtentsUtf8(dpy, font.xftfont, (unsigned const char *) text, strlen(text), font.extents); + if(font.extents->height > font.height) + font.height = font.extents->height; + return font.extents->xOff; +#endif } void setfont(const char *fontstr) { +#ifndef DZEN_XFT char *def, **missing; int i, n; - missing = NULL; if(font.set) XFreeFontSet(dpy, font.set); @@ -88,6 +102,21 @@ setfont(const char *fontstr) { font.descent = font.xfont->descent; } font.height = font.ascent + font.descent; +#else + if(font.xftfont) + XftFontClose(dpy, font.xftfont); + font.xftfont = XftFontOpenXlfd(dpy, DefaultScreen(dpy), fontstr); + if(!font.xftfont) + font.xftfont = XftFontOpenName(dpy, DefaultScreen(dpy), fontstr); + if(!font.xftfont) { + fprintf(stderr, "error, cannot load font: '%s'\n", fontstr); + exit(EXIT_FAILURE); + } + font.extents = malloc(sizeof(XGlyphInfo)); + XftTextExtentsUtf8(dpy, font.xftfont, (unsigned const char *) fontstr, strlen(fontstr), font.extents); + font.height = font.xftfont->ascent + font.xftfont->descent; + font.width = (font.extents->width)/strlen(fontstr); +#endif } int diff --git a/gadgets/textwidth.old b/gadgets/textwidth.old new file mode 100644 index 0000000..6f95f35 --- /dev/null +++ b/gadgets/textwidth.old @@ -0,0 +1,117 @@ +/* + textwidth - calculate width in pixels of text with a given font + + Copyright (C) 2007 by Robert Manea + +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 +#include +#include +#include + +typedef struct _Fnt { + XFontStruct *xfont; + XFontSet set; + int ascent; + int descent; + int height; +} Fnt; + +Fnt font; +Display *dpy; + +unsigned int +textw(const char *text, unsigned int len) { + XRectangle r; + + if(font.set) { + XmbTextExtents(font.set, text, len, NULL, &r); + return r.width; + } + return XTextWidth(font.xfont, text, len); +} + +void +setfont(const char *fontstr) { + char *def, **missing; + int i, n; + + missing = NULL; + if(font.set) + XFreeFontSet(dpy, font.set); + font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def); + if(missing) + XFreeStringList(missing); + if(font.set) { + XFontSetExtents *font_extents; + XFontStruct **xfonts; + char **font_names; + font.ascent = font.descent = 0; + font_extents = XExtentsOfFontSet(font.set); + n = XFontsOfFontSet(font.set, &xfonts, &font_names); + for(i = 0, font.ascent = 0, font.descent = 0; i < n; i++) { + if(font.ascent < (*xfonts)->ascent) + font.ascent = (*xfonts)->ascent; + if(font.descent < (*xfonts)->descent) + font.descent = (*xfonts)->descent; + xfonts++; + } + } + else { + if(font.xfont) + XFreeFont(dpy, font.xfont); + font.xfont = NULL; + if(!(font.xfont = XLoadQueryFont(dpy, fontstr))) { + fprintf(stderr, "error, cannot load font: '%s'\n", fontstr); + exit(EXIT_FAILURE); + } + font.ascent = font.xfont->ascent; + font.descent = font.xfont->descent; + } + font.height = font.ascent + font.descent; +} + +int +main(int argc, char *argv[]) +{ + char *myfont, *text; + + if(argc < 3) { + fprintf(stderr, "usage: %s \n", argv[0]); + return EXIT_FAILURE; + } + + myfont = argv[1]; + text = argv[2]; + + dpy = XOpenDisplay(0); + if(!dpy) { + fprintf(stderr, "cannot open display\n"); + return EXIT_FAILURE; + } + + setfont(myfont); + printf("%u\n", textw(text, strlen(text))); + + return EXIT_SUCCESS; +} + From df86b6f3620cd7a4439c5304a4c6a78011e686f5 Mon Sep 17 00:00:00 2001 From: Hazel P Date: Fri, 8 Jan 2021 00:04:38 -0800 Subject: [PATCH 2/2] Removed accidentally uploaded file --- gadgets/textwidth.old | 117 ------------------------------------------ 1 file changed, 117 deletions(-) delete mode 100644 gadgets/textwidth.old diff --git a/gadgets/textwidth.old b/gadgets/textwidth.old deleted file mode 100644 index 6f95f35..0000000 --- a/gadgets/textwidth.old +++ /dev/null @@ -1,117 +0,0 @@ -/* - textwidth - calculate width in pixels of text with a given font - - Copyright (C) 2007 by Robert Manea - -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 -#include -#include -#include - -typedef struct _Fnt { - XFontStruct *xfont; - XFontSet set; - int ascent; - int descent; - int height; -} Fnt; - -Fnt font; -Display *dpy; - -unsigned int -textw(const char *text, unsigned int len) { - XRectangle r; - - if(font.set) { - XmbTextExtents(font.set, text, len, NULL, &r); - return r.width; - } - return XTextWidth(font.xfont, text, len); -} - -void -setfont(const char *fontstr) { - char *def, **missing; - int i, n; - - missing = NULL; - if(font.set) - XFreeFontSet(dpy, font.set); - font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def); - if(missing) - XFreeStringList(missing); - if(font.set) { - XFontSetExtents *font_extents; - XFontStruct **xfonts; - char **font_names; - font.ascent = font.descent = 0; - font_extents = XExtentsOfFontSet(font.set); - n = XFontsOfFontSet(font.set, &xfonts, &font_names); - for(i = 0, font.ascent = 0, font.descent = 0; i < n; i++) { - if(font.ascent < (*xfonts)->ascent) - font.ascent = (*xfonts)->ascent; - if(font.descent < (*xfonts)->descent) - font.descent = (*xfonts)->descent; - xfonts++; - } - } - else { - if(font.xfont) - XFreeFont(dpy, font.xfont); - font.xfont = NULL; - if(!(font.xfont = XLoadQueryFont(dpy, fontstr))) { - fprintf(stderr, "error, cannot load font: '%s'\n", fontstr); - exit(EXIT_FAILURE); - } - font.ascent = font.xfont->ascent; - font.descent = font.xfont->descent; - } - font.height = font.ascent + font.descent; -} - -int -main(int argc, char *argv[]) -{ - char *myfont, *text; - - if(argc < 3) { - fprintf(stderr, "usage: %s \n", argv[0]); - return EXIT_FAILURE; - } - - myfont = argv[1]; - text = argv[2]; - - dpy = XOpenDisplay(0); - if(!dpy) { - fprintf(stderr, "cannot open display\n"); - return EXIT_FAILURE; - } - - setfont(myfont); - printf("%u\n", textw(text, strlen(text))); - - return EXIT_SUCCESS; -} -