antiright-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Antiright-devel] antiright/rootcat Makefile rootcat.c


From: Jeffrey Bedard
Subject: [Antiright-devel] antiright/rootcat Makefile rootcat.c
Date: Tue, 15 Jan 2008 18:20:56 +0000

CVSROOT:        /sources/antiright
Module name:    antiright
Changes by:     Jeffrey Bedard <jefbed> 08/01/15 18:20:56

Modified files:
        rootcat        : Makefile rootcat.c 

Log message:
        Use Xft for text rendering.  

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/antiright/rootcat/Makefile?cvsroot=antiright&r1=1.1&r2=1.2
http://cvs.savannah.gnu.org/viewcvs/antiright/rootcat/rootcat.c?cvsroot=antiright&r1=1.1&r2=1.2

Patches:
Index: Makefile
===================================================================
RCS file: /sources/antiright/antiright/rootcat/Makefile,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- Makefile    13 Jan 2008 21:27:04 -0000      1.1
+++ Makefile    15 Jan 2008 18:20:56 -0000      1.2
@@ -26,8 +26,9 @@
 include ../config.mk
 
 PROG=rootcat
-INCLUDES=-I/usr/X11R6/include -I/usr/pkg/include
-LDFLAGS=-L/usr/X11R6/lib -L/usr/pkg/lib -lX11
+INCLUDES=-I/usr/pkg/include/freetype2 -I/usr/X11R6/include/freetype2 
+INCLUDES+=-I/usr/pkg/include -I/usr/X11R6/include
+LDFLAGS=-L/usr/pkg/lib -L/usr/X11R6/lib -lX11 -lXft -lXext
 CFLAGS+=$(INCLUDES)
 CFLAGS+=-g3
 objs=rootcat.o
@@ -37,7 +38,7 @@
 $(PROG): $(objs) 
        $(CC) -o $(PROG) $(objs) $(LDFLAGS)
 clean:
-       rm -f $(objs) $(PROG)
+       rm -f $(objs) $(PROG) *.core
 install:
        $(INSTALL) -c $(PROG) $(PREFIX)/bin
 

Index: rootcat.c
===================================================================
RCS file: /sources/antiright/antiright/rootcat/rootcat.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- rootcat.c   13 Jan 2008 21:27:04 -0000      1.1
+++ rootcat.c   15 Jan 2008 18:20:56 -0000      1.2
@@ -1,9 +1,3 @@
-#include <X11/X.h>
-#include <X11/Xutil.h>
-#include <X11/Xlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
 /*
   AntiRight
   (c) 2008 Jeffrey Bedard
@@ -26,32 +20,154 @@
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  
USA
 */
 
+#include <X11/X.h>
+#include <X11/Xutil.h>
+#include <X11/Xlib.h>
+#include <X11/Xft/Xft.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <assert.h>
+
+#define SR_DEFAULT_FONT "Mono 12"
+#define SR_DEFAULT_COLOR "white"
+
+typedef struct _StringRenderer
+{
+       Display * dpy;
+       Window window;
+       XftDraw * __draw;
+       XftColor color;
+       XftFont * font;
+       void (*delete)(struct _StringRenderer *);
+       void (*draw)(struct _StringRenderer *, 
+               const int, const int, const char *);
+       void (*clear)(struct _StringRenderer *);
+       unsigned int (*get_line_height)(struct _StringRenderer *,       
+               const char *);
+} StringRenderer;
+
+void
+StringRenderer_clear(StringRenderer * rc)
+{
+       XClearWindow(rc->dpy, rc->window);
+}
+
+unsigned int
+StringRenderer_get_line_height(StringRenderer * rc, const char * string)
+{
+       XGlyphInfo extents;
+
+       assert(string);
+       assert(rc);
+       assert(rc->dpy);
+       assert(rc->font);
+
+       XftTextExtentsUtf8(rc->dpy, rc->font, 
+               (FcChar8 *)string, strlen(string),
+               &extents);
+
+       return extents.height;
+}
+
+static void
+StringRenderer_draw(StringRenderer * rc, 
+       const int x, const int y, const char * string)
+{
+       XftDrawStringUtf8(rc->__draw, &(rc->color), rc->font,
+               x, y, (FcChar8 *)string, strlen(string));
+}
+
+static void
+delete_StringRenderer(StringRenderer * rc)
+{
+       Display * dpy = rc->dpy;
+       int screen = DefaultScreen(dpy);
+
+       XftColorFree(dpy, DefaultVisual(dpy, screen), 
+               DefaultColormap(dpy, screen), &(rc->color));
+       XftFontClose(dpy, rc->font);
+       XftDrawDestroy(rc->__draw);
+       XCloseDisplay(dpy);
+       free(rc);
+}
+
+static void
+error_opening_display(void)
+{
+       fprintf(stderr, "Error:  cannot open DISPLAY\n");
+       exit(1);
+}
+
+static StringRenderer *
+new_StringRenderer(Display * dpy, Window w, 
+       const char * font, const char * color)
+{
+       int screen;
+       Visual * visual;
+       Colormap cmap;
+
+       /* Validate inputs.  */
+       if(!dpy)
+               error_opening_display();
+       if(!font)
+               font=SR_DEFAULT_FONT;
+       if(!color)
+               color=SR_DEFAULT_COLOR;
+
+       /* Allocate class.  */
+       StringRenderer * rc = malloc(sizeof(StringRenderer));
+       assert(rc);
+
+       rc->delete=&delete_StringRenderer;
+       rc->draw=&StringRenderer_draw;
+       rc->get_line_height=&StringRenderer_get_line_height;
+       rc->clear = &StringRenderer_clear;
+
+       rc->dpy=dpy;
+       rc->window=w;
+       screen=DefaultScreen(dpy);
+       visual=DefaultVisual(dpy, screen);
+       cmap=DefaultColormap(dpy, screen);
+       rc->__draw=XftDrawCreate(dpy, w, visual, cmap);
+       XftColorAllocName(dpy, visual, cmap, color, &(rc->color));
+       rc->font=XftFontOpenName(dpy, screen, font);
+
+       return rc;
+}
+
+static StringRenderer *
+new_StringRenderer_root(const char * font)
+{
+       Display * dpy = XOpenDisplay(getenv("DISPLAY"));
+       if(!dpy)
+               error_opening_display();
+       return new_StringRenderer(dpy, DefaultRootWindow(dpy), font, NULL);
+}
+
+#ifndef $
+#define $(class, method, ...) class->method(class, ##__VA_ARGS__)
+#endif /* ! $ */
 
 int
 main(int argc, char ** argv)
 {
-       char * display_name = getenv("DISPLAY");
-       Display * dpy = XOpenDisplay(display_name);
-       Window root = DefaultRootWindow(dpy);
-       XGCValues values;
-       GC gc;
-       const int scr = DefaultScreen(dpy);
-       const int line_height = 16;
-
-       values.foreground=WhitePixel(dpy, scr);
-       values.font=XLoadFont(dpy, "fixed");
-       gc=XCreateGC(dpy, root, GCForeground | GCFont, &values);
+       StringRenderer * rc;
+
+       rc=new_StringRenderer_root(NULL);
+       assert(rc);
 
-       XClearWindow(dpy, root);
+       $(rc, clear);
        for(;argc>1; argc--)
        {
-               XDrawString(dpy, root, gc, line_height/2, (argc-1)*line_height, 
-                       argv[argc-1], strlen(argv[argc-1]));
+               const int index=argc-1;
+               const char * string = argv[index];
+               unsigned int height=$(rc, get_line_height, string);
+
+               $(rc, draw, (height/2), ((index)*1.5*height), string);
        }
 
-       XFreeGC(dpy, gc);
-       XUnloadFont(dpy, values.font);
-       XCloseDisplay(dpy);
+       $(rc, delete);
 
        return 0;
 }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]