[teklib-general] changeset in /hg/teklib/current: added

Franciska Schulze fschulze at neoscientists.org
Mon Nov 26 16:22:31 CET 2007


changeset 736cdd95eb49 in /hg/teklib/current
details: http://teklib.org:8001/hg/teklib/current?cmd=changeset;node=736cdd95eb49
description:
	added

diffs (truncated from 673 to 300 lines):

diff -r a8f2c81c5191 -r 736cdd95eb49 src/visual/posix/visual_font.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/visual/posix/visual_font.c	Tue Sep 11 19:05:20 2007 +0200
@@ -0,0 +1,610 @@
+/*
+**	$Id: visual_host.c,v 1.3 2006/09/10 14:38:04 tmueller Exp $
+**	teklib/src/visual/posix/visual_font.c - xlib based font management
+**
+*/
+
+#include <tek/debug.h>
+#include <tek/teklib.h>
+#include <tek/proto/hal.h>
+#include "visual_mod.h"
+
+#include <stdio.h>
+#include <string.h>
+#include "visual_host.h"
+#include "visual_font.h"
+
+/*****************************************************************************/
+
+LOCAL TAPTR
+vis_hostopenfont(TMOD_VIS *mod, TTAGITEM *tags)
+{
+	GLOBAL *g = mod->vis_HostGlobal;
+	TINT fpxsize;
+	TBOOL fitalic, fbold;
+	TAPTR font = TNULL;
+	TSTRPTR fname = TNULL;
+	TSTRPTR fquery = TNULL;
+	struct FontNode *fn;
+	TAPTR exec = TGetExecBase(mod);
+
+	/* fetch user specified attributes */
+	fname = (TSTRPTR) TGetTag(tags, TVisual_FontName, (TTAG) FNT_DEFNAME);
+	fpxsize = (TINT) TGetTag(tags, TVisual_FontPxSize, (TTAG) FNT_DEFPXSIZE);
+	fitalic = (TBOOL) TGetTag(tags, TVisual_FontItalic, (TTAG) TFALSE);
+	fbold = (TBOOL) TGetTag(tags, TVisual_FontBold, (TTAG) TFALSE);
+
+	if (fname)
+		fquery = TExecAlloc0(exec, mod->vis_MMU, FNT_LENGTH + strlen(fname));
+	else
+		TDBPRINTF(20, ("invalid fontname '%s' specified\n", fname));
+
+	if (fquery)
+	{
+		TExecLock(exec, g->lock);
+
+		/* build fontquery name */
+		sprintf(fquery, "-*-%s-%s-%s-*-*-%d-*-*-*-*-*-%s",
+			fname,
+			fbold ? FNT_WGHT_BOLD : FNT_WGHT_MEDIUM,
+			fitalic ? FNT_SLANT_I : FNT_SLANT_R,
+			fpxsize,
+			FNT_DEFREGENC
+		);
+
+		TDBPRINTF(10, ("fquery = %s\n", fquery));
+
+		/* allocate new font node */
+		fn = TExecAlloc0(exec, mod->vis_MMU, sizeof(struct FontNode));
+		if (fn)
+		{
+			fn->font = XLoadQueryFont(g->display, fquery);
+			if (fn->font)
+			{
+				/* save font attributes */
+				fn->pxsize = fpxsize;
+				if (fitalic)
+					fn->attr = FNT_ITALIC;
+				if (fbold)
+					fn->attr |= FNT_BOLD;
+
+				/* append to the list of open fonts */
+				TDBPRINTF(10, ("'%s' successfully loaded\n", fname));
+				TAddTail(&g->fm.openfonts, &fn->node);
+				font = (TAPTR)fn;
+			}
+			else
+			{
+				/* query failed, return pointer to default font */
+				TDBPRINTF(10, ("unable to load '%s'\n", fname));
+				TDBPRINTF(10, ("defaulting to '%s'\n", DEFFONTNAME));
+				TExecFree(exec, fn);
+				font = g->fm.deffont;
+				g->fm.defref++;
+			}
+		}
+
+		TExecUnlock(exec, g->lock);
+		TExecFree(exec, fquery);
+	}
+
+	return font;
+}
+
+/*****************************************************************************/
+
+LOCAL TSTRPTR
+fnt_getsubstring(TMOD_VIS *mod, TSTRPTR fstring, TINT m)
+{
+	TINT i, p = 0;
+	TINT mcount = 0;
+	TSTRPTR substr = TNULL;
+	TAPTR exec = TGetExecBase(mod);
+
+	/* match '-' at pos m and m+1 */
+	for (i = 0; i < strlen(fstring); i++)
+	{
+		if (fstring[i] == '-')
+		{
+			mcount++;
+			if (mcount == m)
+				p = i;
+			if (mcount == m+1)
+				break;
+		}
+	}
+
+	/* extract substring */
+	substr = TExecAlloc0(exec, mod->vis_MMU, i-p);
+	if (substr)
+	{
+		TExecCopyMem(exec, fstring+p+1, substr, i-p-1);
+		TDBPRINTF(2, ("extracted = '%s'\n", substr));
+	}
+	else
+		TDBPRINTF(20, ("out of memory :(\n"));
+
+	return substr;
+}
+
+LOCAL TVOID
+fnt_getattr(TMOD_VIS *mod, TTAGITEM *tag, TSTRPTR fstring)
+{
+	TAPTR exec = TGetExecBase(mod);
+
+	/* "-*-%s-%s-%s-*-*-%d-*-*-*-*-*-%s" */
+	switch (tag->tti_Tag)
+	{
+		case TVisual_FontName:
+		{
+			/* extract name -> 2th and 3th '-' */
+			tag->tti_Value = (TTAG)fnt_getsubstring(mod, fstring, 2);
+			break;
+		}
+		case TVisual_FontPxSize:
+		{
+			/* extract pixel size -> 7th and 8th '-' */
+			TSTRPTR pxsize = fnt_getsubstring(mod, fstring, 7);
+			if (pxsize)
+			{
+				tag->tti_Value = (TTAG)atoi(pxsize);
+				TExecFree(exec, pxsize);
+			}
+			break;
+		}
+		case TVisual_FontItalic:
+		{
+			/* extract slant -> 4th and 5th '-' */
+			TSTRPTR slant = fnt_getsubstring(mod, fstring, 4);
+			if (slant)
+			{
+				/* italic attribute set? */
+				if (strncmp(FNT_SLANT_I, slant, strlen(FNT_SLANT_I)) == 0)
+					tag->tti_Value = (TTAG)TTRUE;
+				TExecFree(exec, slant);
+			}
+			break;
+		}
+		case TVisual_FontBold:
+		{
+			/* extract weight -> 3th and 4th '-' */
+			TSTRPTR weight = fnt_getsubstring(mod, fstring, 3);
+			if (weight)
+			{
+				/* bold attribute set? */
+				if (strncmp(FNT_WGHT_BOLD, weight, strlen(FNT_WGHT_BOLD)) == 0)
+					tag->tti_Value = (TTAG)TTRUE;
+				TExecFree(exec, weight);
+			}
+			break;
+		}
+		default:
+			break;
+	}
+}
+
+/*****************************************************************************/
+
+THOOKENTRY TTAG
+fqhdestroy(struct THook *hook, TAPTR obj, TTAG msg)
+{
+	if (msg == TMSG_DESTROY)
+	{
+		struct FontQueryHandle *fqh = obj;
+		TMOD_VIS *mod = fqh->handle.thn_Owner;
+		TAPTR exec = TGetExecBase(mod);
+		struct TNode *node, *next;
+
+		node = fqh->reslist.tlh_Head;
+		for (; (next = node->tln_Succ); node = next)
+		{
+			struct FontQueryNode *fqn = (struct FontQueryNode *)node;
+
+			/* remove from resultlist */
+			TRemove(&fqn->node);
+
+			/* destroy fontname */
+			if (fqn->tags[0].tti_Value)
+				TExecFree(exec, (TAPTR)fqn->tags[0].tti_Value);
+
+			/* destroy node */
+			TExecFree(exec, fqn);
+		}
+
+		/* destroy queryhandle */
+		TExecFree(exec, fqh);
+	}
+
+	return 0;
+}
+
+/*****************************************************************************/
+
+LOCAL TBOOL
+fnt_checkfqnode(struct TList *rlist, struct FontQueryNode *fqnode)
+{
+	TUINT8 flags;
+	TBOOL match = TFALSE;
+	struct TNode *node, *next;
+
+	TSTRPTR newfname = (TSTRPTR)fqnode->tags[0].tti_Value;
+	TINT newpxsize = (TINT)fqnode->tags[1].tti_Value;
+	TBOOL newslant = (TBOOL)fqnode->tags[2].tti_Value;
+	TBOOL newweight = (TBOOL)fqnode->tags[3].tti_Value;
+	TINT flen = strlen(newfname);
+
+	/* filter out fonts with pxsize = 0 to keep our api consistent */
+	if (newpxsize == 0)
+		return TTRUE;
+
+	node = rlist->tlh_Head;
+	for (; (next = node->tln_Succ); node = next)
+	{
+		struct FontQueryNode *fqn = (struct FontQueryNode *)node;
+		flags = 0;
+
+		if (strlen((TSTRPTR)fqn->tags[0].tti_Value) == flen)
+		{
+			if (strncmp((TSTRPTR)fqn->tags[0].tti_Value, newfname, flen) == 0)
+				flags = FNT_MATCH_NAME;
+		}
+
+		if ((TINT)fqn->tags[1].tti_Value == newpxsize)
+			flags |= FNT_MATCH_SIZE;
+
+		if ((TBOOL)fqn->tags[2].tti_Value == newslant)
+			flags |= FNT_MATCH_SLANT;
+
+		if ((TBOOL)fqn->tags[3].tti_Value == newweight)
+			flags |= FNT_MATCH_WEIGHT;
+
+		if (flags == FNT_MATCH_ALL)
+		{
+			/* fqnode is not unique */
+			match = TTRUE;
+			break;
+		}
+	}
+
+	return match;
+}
+
+LOCAL TVOID
+fnt_dumpnode(struct FontQueryNode *fqn)
+{
+	TDBPRINTF(10, ("-----------------------------------------------\n"));
+	TDBPRINTF(10, ("dumping fontquerynode @ %p\n", fqn));
+	TDBPRINTF(10, (" * FontName: %s\n", (TSTRPTR)fqn->tags[0].tti_Value));
+	TDBPRINTF(10, (" * PxSize:   %d\n", (TINT)fqn->tags[1].tti_Value));
+	TDBPRINTF(10, (" * Italic:   %s\n", (TBOOL)fqn->tags[2].tti_Value ? "on" : "off"));
+	TDBPRINTF(10, (" * Bold:     %s\n", (TBOOL)fqn->tags[3].tti_Value ? "on" : "off"));
+	TDBPRINTF(10, ("-----------------------------------------------\n"));
+}
+
+LOCAL TVOID
+fnt_dumplist(struct TList *rlist)
+{
+	struct TNode *node, *next;
+	node = rlist->tlh_Head;
+	for (; (next = node->tln_Succ); node = next)
+	{
+		struct FontQueryNode *fqn = (struct FontQueryNode *)node;
+		fnt_dumpnode(fqn);
+	}
+}
+
+/*****************************************************************************/


More information about the teklib-general mailing list