Hi,

The attached patch against Agar's current SVN allows to define per-label fonts and, I believe, fix a few font-related bugs.

I tested it and it seems to work correctly, at least on my example.

It was not very clear for me whether agText* variables (ex: agTextFontHeight) were relative to the default font or to the current text state (maybe some changes are needed at this level).

Hope this helps,
Best regards,

Olivier Boudeville.


Index: gui/text.c
===================================================================
--- gui/text.c	(révision 8929)
+++ gui/text.c	(copie de travail)
@@ -357,7 +357,13 @@
 AG_SetDefaultFont(AG_Font *font)
 {
 	AG_MutexLock(&agTextLock);
+	
 	agDefaultFont = font;
+	agTextFontHeight = font->height;
+	agTextFontAscent = font->ascent;
+	agTextFontDescent = font->descent;
+	agTextFontLineSkip = font->lineskip;
+	
 	AG_MutexUnlock(&agTextLock);
 }
 
@@ -450,6 +456,7 @@
 		AG_SetError("Failed to load default font: %s", AG_GetError());
 		goto fail;
 	}
+	
 	agDefaultFont = font;
 	agTextFontHeight = font->height;
 	agTextFontAscent = font->ascent;
Index: gui/label.c
===================================================================
--- gui/label.c	(révision 8929)
+++ gui/label.c	(copie de travail)
@@ -187,13 +187,19 @@
 {
 	AG_Label *lbl = obj;
 	
+	if ( lbl->font != NULL ) { 
+	  AG_PushTextState();
+	  AG_TextFont(lbl->font) ;
+	}
+
 	if (lbl->flags & AG_LABEL_NOMINSIZE) {
 		r->w = lbl->lPad + lbl->rPad;
-		r->h = agTextFontHeight + lbl->tPad + lbl->bPad;
+		r->h = lbl->font->height + lbl->tPad + lbl->bPad;
 		return;
 	}
-	switch (lbl->type) {
-	case AG_LABEL_STATIC:
+	else {
+	  switch (lbl->type) {
+	  case AG_LABEL_STATIC:
 		AG_TextSize(lbl->text, &r->w, &r->h);
 		r->w += lbl->lPad + lbl->rPad;
 		r->h += lbl->tPad + lbl->bPad;
@@ -203,7 +209,11 @@
 		r->w = lbl->wPre + lbl->lPad + lbl->rPad;
 		r->h = lbl->hPre + lbl->tPad + lbl->bPad;
 		break;
+	  }
 	}
+
+	if ( lbl->font != NULL )  
+	  AG_PopTextState();
 }
 
 static int
@@ -223,6 +233,11 @@
 	if (lbl->text == NULL)
 		return (0);
 
+	if ( lbl->font != NULL ) { 
+	  AG_PushTextState();
+	  AG_TextFont(lbl->font);
+	}
+
 	/*
 	 * If the widget area is too small to display the complete
 	 * string, draw a "..." at the end.
@@ -242,6 +257,10 @@
 	} else {
 		lbl->flags &= ~AG_LABEL_PARTIAL;
 	}
+
+	if ( lbl->font != NULL )  
+	  AG_PopTextState();
+
 	return (0);
 }
 
@@ -253,6 +272,7 @@
 	lbl->type = AG_LABEL_STATIC;
 	lbl->flags = 0;
 	lbl->text = NULL;
+	lbl->font = NULL;
 	lbl->surface = -1;
 	lbl->surfaceCont = -1;
 	lbl->lPad = 2;
@@ -273,6 +293,7 @@
 #ifdef AG_DEBUG
 	AG_BindUint(lbl, "flags", &lbl->flags);
 	AG_BindPointer(lbl, "text", (void *)&lbl->text);
+	AG_BindPointer(lbl, "font", (void *)&lbl->font);
 	AG_BindInt(lbl, "surface", &lbl->surface);
 	AG_BindInt(lbl, "surfaceCont", &lbl->surfaceCont);
 	AG_BindInt(lbl, "wPre", &lbl->wPre);
@@ -293,15 +314,37 @@
 	int hLbl;
 
 	AG_ObjectLock(lbl);
+
+	if ( lbl->font != NULL ) { 
+	  AG_PushTextState();
+	  AG_TextFont(lbl->font) ;
+	}
+
 	if (nlines > 0) {
 		AG_TextSize(text, &lbl->wPre, &hLbl);
-		lbl->hPre = nlines*hLbl + (nlines-1)*agTextFontLineSkip;
+		lbl->hPre = nlines*hLbl + (nlines-1)*agTextState->font->lineskip;
 	} else {
 		AG_TextSize(text, &lbl->wPre, &lbl->hPre);
 	}
 	AG_ObjectUnlock(lbl);
+
+	if ( lbl->font != NULL )  
+	  AG_PopTextState();
+
+	AG_ObjectUnlock(lbl);
 }
 
+/* Set the font associated to this label */
+void
+AG_LabelSetFont(AG_Label *lbl, AG_Font *font)
+{
+	AG_ObjectLock(lbl);
+    lbl->font = font ;
+	lbl->hPre = font->height;
+	AG_ObjectUnlock(lbl);
+	AG_Redraw(lbl);
+}
+
 /* Set the padding around the label in pixels. */
 void
 AG_LabelSetPadding(AG_Label *lbl, int lPad, int rPad, int tPad, int bPad)
@@ -752,6 +795,9 @@
 	}
 	
 	AG_PushTextState();
+	if (lbl->font != 0)
+	  AG_TextFont(lbl->font);
+
 	AG_TextJustify(lbl->justify);
 	AG_TextValign(lbl->valign);
 	AG_TextColor(agColors[TEXT_COLOR]);
Index: gui/label.h
===================================================================
--- gui/label.h	(révision 8929)
+++ gui/label.h	(copie de travail)
@@ -50,6 +50,7 @@
 #define AG_LABEL_FRAME		0x80	/* Draw visible frame */
 #define AG_LABEL_EXPAND		(AG_LABEL_HFILL|AG_LABEL_VFILL)
 	char *text;			/* Text buffer */
+	AG_Font *font ;
 	int surface;			/* Label surface */
 	int surfaceCont;		/* [...] surface */
 	int wPre, hPre;			/* SizeHint dimensions */
@@ -81,6 +82,7 @@
                        NONNULL_ATTRIBUTE(2);
 void      AG_LabelTextS(AG_Label *, const char *);
 
+void	 AG_LabelSetFont(AG_Label *, AG_Font *);
 void	 AG_LabelSetPadding(AG_Label *, int, int, int, int);
 void	 AG_LabelJustify(AG_Label *, enum ag_text_justify);
 void	 AG_LabelValign(AG_Label *, enum ag_text_valign);
_______________________________________________
Agar mailing list
[email protected]
http://libagar.org/lists.html

Reply via email to