[xiph-commits] r10128 - trunk/planarity

xiphmont at svn.xiph.org xiphmont at svn.xiph.org
Wed Oct 5 03:25:46 PDT 2005


Author: xiphmont
Date: 2005-10-05 03:25:42 -0700 (Wed, 05 Oct 2005)
New Revision: 10128

Modified:
   trunk/planarity/Makefile
   trunk/planarity/dialog_finish.c
   trunk/planarity/dialog_level.c
   trunk/planarity/dialog_level_icons.c
   trunk/planarity/dialog_pause.c
   trunk/planarity/gameboard_draw_intersection.c
   trunk/planarity/gameboard_draw_score.c
   trunk/planarity/gameboard_draw_text.c
   trunk/planarity/gameboard_logic_button.c
   trunk/planarity/graph_generate.c
   trunk/planarity/main.c
   trunk/planarity/main.h
   trunk/planarity/version.h
Log:
Font fixes:

Account somewhat for widely varying font aspect rations; go for a
tall/thin ratio based on Arial

Scale the Level description on the game board if it gets too long

Correct a level name

Update Makefile to enforce gtk/cairo versions rather than a cryptic
failure to build



Modified: trunk/planarity/Makefile
===================================================================
--- trunk/planarity/Makefile	2005-10-05 08:12:23 UTC (rev 10127)
+++ trunk/planarity/Makefile	2005-10-05 10:25:42 UTC (rev 10128)
@@ -36,17 +36,16 @@
 	timer.o gameboard_draw_vertex.o levelstate.o dialog_level.o\
 	dialog_level_icons.o gameboard_draw_text.o random.o graph_generate_data.o\
 	gameboard_logic_fade.o graph_generate_mesh2.o
-GCF  = `pkg-config --static --cflags "gtk+-2.0 >= 2.7.2"`
+CAIROVER =  >= 1.0.0
+GTKVER   =  >= 2.7.2
+GCF  = `pkg-config --static --cflags "gtk+-2.0 $(GTKVER) cairo $(CAIROVER)"`
+LDF  = `pkg-config --static --libs "gtk+-2.0 $(GTKVER) cairo $(CAIROVER)"`
 
-# uncomment below for a normal dynamic build
+# a build with static Gtk 2.7+ generally requires that all of the Gtk
+# dependencies were built with static dependencies also pointing to
+# the local install
 
-LDF  = `pkg-config --static --libs "gtk+-2.0 >= 2.7.2"`
-
-# uncomment below for a build with static Gtk 2.7+; this generally
-# requires that all of the Gtk dependencies were built with static
-# dependencies also pointing to the local install
-
-SLDF  = `pkg-config --static --libs-only-L "gtk+-2.0 >= 2.7.2"`
+SLDF  = `pkg-config --static --libs-only-L "gtk+-2.0 $(GTKVER) cairo $(CAIROVER)"`
 GTK   = /usr/local/lib
 EXPAT= /usr/lib/libexpat.a
 XINERAMA = /usr/X11R6/lib/libXinerama.a
@@ -64,6 +63,7 @@
 	-lXft $(PNG)
 
 all:    
+	pkg-config --cflags "gtk+-2.0 $(GTKVER) cairo $(CAIROVER)" 1>/dev/null
 	$(MAKE) target CFLAGS="-O2 -ffast-math $(GCF) $(ADD_DEF)"
 	$(STRIP) $(TARGET)
 

Modified: trunk/planarity/dialog_finish.c
===================================================================
--- trunk/planarity/dialog_finish.c	2005-10-05 08:12:23 UTC (rev 10127)
+++ trunk/planarity/dialog_finish.c	2005-10-05 10:25:42 UTC (rev 10128)
@@ -36,6 +36,7 @@
 #include "levelstate.h"
 #include "dialog_finish.h"
 #include "dialog_level.h"
+#include "main.h"
 
 static void finish_post (Gameboard *g){
   // back to buttonbar activity!
@@ -146,29 +147,19 @@
 	    SCOREHEIGHT);
 
   {
-    cairo_matrix_t ma;
     char *time = get_timer_string();
     char buffer[160];
     int time_bonus=graphscore_get_bonus(&g->g);
 
     int y;
 
-    cairo_select_font_face (c, "Arial",
-			    CAIRO_FONT_SLANT_NORMAL,
-			    CAIRO_FONT_WEIGHT_BOLD);
-
-    cairo_matrix_init_scale (&ma, 18.,18.);
-    cairo_set_font_matrix (c,&ma);
+    set_font(c,18,18,0,1);
     cairo_set_source_rgba (c, TEXT_COLOR);
 
     y=h/2-FINISHBOX_HEIGHT/2+SCOREHEIGHT/2;
     render_text_centered(c,"Level Complete!", w/2,y);y+=45;
 
-    cairo_select_font_face (c, "Arial",
-			    CAIRO_FONT_SLANT_NORMAL,
-			    CAIRO_FONT_WEIGHT_NORMAL);
-    cairo_matrix_init_scale (&ma, 16.,16.);
-    cairo_set_font_matrix (c,&ma);
+    set_font(c,16,16,0,0);
 
     snprintf(buffer,160,"Elapsed: %s",time);
     render_bordertext_centered(c,buffer, w/2,y);y+=24;
@@ -179,9 +170,7 @@
 
     if(graphscore_get_multiplier(&g->g)>1){
       cairo_save(c);
-      cairo_select_font_face (c, "Arial",
-			      CAIRO_FONT_SLANT_NORMAL,
-			      CAIRO_FONT_WEIGHT_BOLD);
+      set_font(c,16,16,0,1);
       cairo_set_source_rgba (c, HIGH_COLOR);
       
       snprintf(buffer,160,"Objective Exceeded! x%d",graphscore_get_multiplier(&g->g));
@@ -192,9 +181,7 @@
     snprintf(buffer,160,"Time bonus: %d points",time_bonus);
     render_bordertext_centered(c,buffer, w/2,y);y+=35;
 
-    cairo_select_font_face (c, "Arial",
-			    CAIRO_FONT_SLANT_NORMAL,
-			    CAIRO_FONT_WEIGHT_BOLD);
+    set_font(c,16,16,0,1);
 
     snprintf(buffer,160,"Final score: %d points",graphscore_get_score(&g->g)+time_bonus);
     render_bordertext_centered(c,buffer, w/2,y);y+=24;

Modified: trunk/planarity/dialog_level.c
===================================================================
--- trunk/planarity/dialog_level.c	2005-10-05 08:12:23 UTC (rev 10127)
+++ trunk/planarity/dialog_level.c	2005-10-05 10:25:42 UTC (rev 10128)
@@ -132,21 +132,11 @@
 	    LEVELBOX_WIDTH,
 	    SCOREHEIGHT);
 
-  {
-    cairo_matrix_t ma;
+  set_font(c,18,18,0,1);
+  cairo_set_source_rgba (c, TEXT_COLOR);
+  
+  render_text_centered(c,"Available Levels", w/2,h/2-LEVELBOX_HEIGHT/2+SCOREHEIGHT/2);
 
-    cairo_select_font_face (c, "Arial",
-			    CAIRO_FONT_SLANT_NORMAL,
-			    CAIRO_FONT_WEIGHT_BOLD);
-    
-    cairo_matrix_init_scale (&ma, 18.,18.);
-    cairo_set_font_matrix (c,&ma);
-    cairo_set_source_rgba (c, TEXT_COLOR);
-
-    render_text_centered(c,"Available Levels", w/2,h/2-LEVELBOX_HEIGHT/2+SCOREHEIGHT/2);
-
-  }
-
   cairo_destroy(c);
 }
 

Modified: trunk/planarity/dialog_level_icons.c
===================================================================
--- trunk/planarity/dialog_level_icons.c	2005-10-05 08:12:23 UTC (rev 10127)
+++ trunk/planarity/dialog_level_icons.c	2005-10-05 10:25:42 UTC (rev 10128)
@@ -235,29 +235,20 @@
     // render level related text
     if(g->d.center_x == 0 && c){
       char buffer[160];
-      cairo_matrix_t ma;
 
       // above text
       if(g->d.text1.width==0 || 
 	 (ey<g->d.text1.y+g->d.text1.height && ey2>g->d.text1.y)){
 	
 	snprintf(buffer,160,"Level %d:",get_level_num()+1);
-	cairo_select_font_face (c, "Arial",
-				CAIRO_FONT_SLANT_NORMAL,
-				CAIRO_FONT_WEIGHT_BOLD);
-	cairo_matrix_init_scale (&ma, 20.,20.);
-	cairo_set_font_matrix (c,&ma);
+	set_font(c,20,20,0,1);
 	cairo_set_source_rgba (c, TEXT_COLOR);
 	g->d.text1=render_bordertext_centered(c, buffer,w/2,y+45);
       }
       
       if(g->d.text2.width==0 || 
 	 (ey<g->d.text2.y+g->d.text2.height && ey2>g->d.text2.y)){
-	cairo_select_font_face (c, "Arial",
-				CAIRO_FONT_SLANT_NORMAL,
-				CAIRO_FONT_WEIGHT_NORMAL);
-	cairo_matrix_init_scale (&ma, 18.,18.);
-	cairo_set_font_matrix (c,&ma);
+	set_font(c,18,18,0,0);
 	cairo_set_source_rgba (c, TEXT_COLOR);
 	g->d.text2=render_bordertext_centered(c, get_level_desc(),w/2,y+70);
       }
@@ -266,19 +257,13 @@
 	 (ey<g->d.text3.y+g->d.text3.height && ey2>g->d.text3.y)){
 	
 	if(levelstate_get_hiscore()==0){
-	  cairo_select_font_face (c, "Arial",
-				  CAIRO_FONT_SLANT_ITALIC,
-				  CAIRO_FONT_WEIGHT_NORMAL);
+	  set_font(c,18,18,1,0);
 	  snprintf(buffer,160,"[not yet completed]");
 	}else{
-	  cairo_select_font_face (c, "Arial",
-				  CAIRO_FONT_SLANT_NORMAL,
-				  CAIRO_FONT_WEIGHT_NORMAL);
+	  set_font(c,18,18,0,0);
 	  snprintf(buffer,160,"level high score: %ld",levelstate_get_hiscore());
 	}
 	
-	cairo_matrix_init_scale (&ma, 18.,18.);
-	cairo_set_font_matrix (c,&ma);
 	cairo_set_source_rgba (c, TEXT_COLOR);
 	g->d.text3=render_bordertext_centered(c, buffer,w/2,y+245);
       }
@@ -288,11 +273,7 @@
 
 	snprintf(buffer,160,"total score all levels: %ld",levelstate_total_hiscore());
 	
-	cairo_select_font_face (c, "Arial",
-				CAIRO_FONT_SLANT_NORMAL,
-				CAIRO_FONT_WEIGHT_NORMAL);
-	cairo_matrix_init_scale (&ma, 18.,18.);
-	cairo_set_font_matrix (c,&ma);
+	set_font(c,18,18,0,0);
 	cairo_set_source_rgba (c, TEXT_COLOR);
 	g->d.text4=render_bordertext_centered(c, buffer,w/2,y+265);
       }

Modified: trunk/planarity/dialog_pause.c
===================================================================
--- trunk/planarity/dialog_pause.c	2005-10-05 08:12:23 UTC (rev 10127)
+++ trunk/planarity/dialog_pause.c	2005-10-05 10:25:42 UTC (rev 10128)
@@ -34,9 +34,8 @@
 #include "gameboard.h"
 #include "gameboard_draw_button.h"
 #include "dialog_pause.h"
+#include "main.h"
 
-extern char *version;
-
 static void unpause_post (Gameboard *g){
   // back to buttonbar activity!
   pop_curtain(g);
@@ -124,21 +123,14 @@
 	    SCOREHEIGHT);
 
   {
-    cairo_matrix_t ma;
     char *time = get_timer_string();
 
-    cairo_select_font_face (c, "Arial",
-			    CAIRO_FONT_SLANT_NORMAL,
-			    CAIRO_FONT_WEIGHT_BOLD);
-    
-    cairo_matrix_init_scale (&ma, 18.,18.);
-    cairo_set_font_matrix (c,&ma);
+    set_font(c,18,18,0,1);
     cairo_set_source_rgba (c, TEXT_COLOR);
 
     render_text_centered(c,"Game Paused", w/2,h/2-PAUSEBOX_HEIGHT/2+SCOREHEIGHT/2);
-    cairo_select_font_face (c, "Arial",
-			    CAIRO_FONT_SLANT_NORMAL,
-			    CAIRO_FONT_WEIGHT_NORMAL);
+
+    set_font(c,18,18,0,0);
     render_bordertext_centered(c,"Time Elapsed:", w/2,h/2-30);
     render_bordertext_centered(c,time, w/2,h/2);
   }
@@ -187,26 +179,18 @@
 	    SCOREHEIGHT);
 
   {
-    cairo_matrix_t ma;
     int y = h/2-ABOUTBOX_HEIGHT/2+SCOREHEIGHT/2;
-    cairo_select_font_face (c, "Sans",
-			    CAIRO_FONT_SLANT_NORMAL,
-			    CAIRO_FONT_WEIGHT_BOLD);
-
-    cairo_matrix_init_scale (&ma, 18.,18.);
-    cairo_set_font_matrix (c,&ma);
+    set_font(c,18,18,0,1);
     cairo_set_source_rgba (c, TEXT_COLOR);
 
     render_text_centered(c,"gPlanarity", w/2,y);
-    cairo_select_font_face (c, "Sans",
-			    CAIRO_FONT_SLANT_NORMAL,
-			    CAIRO_FONT_WEIGHT_NORMAL);
+
+    set_font(c,18,18,0,0);
     y+=45;
     render_bordertext_centered(c,"Untangle the mess!", w/2,y);
     y+=30;
 
-    cairo_matrix_init_scale (&ma, 13.,13.);
-    cairo_set_font_matrix (c,&ma);
+    set_font(c,13,13,0,0);
     render_bordertext_centered(c,"Drag verticies to eliminate crossed lines.", w/2,y); y+=16;
     render_bordertext_centered(c,"The objective may be a complete solution or", w/2,y); y+=16;
     render_bordertext_centered(c,"getting as close as possible to solving an", w/2,y); y+=16;
@@ -219,8 +203,7 @@
     cairo_stroke(c);
     y+=32;
 
-    cairo_matrix_init_scale (&ma, 12.,13.);
-    cairo_set_font_matrix (c,&ma);
+    set_font(c,12,13,0,0);
     render_bordertext_centered(c,"gPlanarity written by Monty <monty at xiph.org>",w/2,y);y+=17;
     render_bordertext_centered(c,"as a demonstration of Gtk+/Cairo",w/2,y);y+=32;
 
@@ -231,12 +214,8 @@
 
 
     y = h/2+ABOUTBOX_HEIGHT/2-SCOREHEIGHT/2;
-    cairo_select_font_face (c, "Sans",
-			    CAIRO_FONT_SLANT_NORMAL,
-			    CAIRO_FONT_WEIGHT_BOLD);
 
-    cairo_matrix_init_scale (&ma, 10.,11.);
-    cairo_set_font_matrix (c,&ma);
+    set_font(c,10,11,0,1);
     render_text_centered(c,version, w/2,y);
 
   }

Modified: trunk/planarity/gameboard_draw_intersection.c
===================================================================
--- trunk/planarity/gameboard_draw_intersection.c	2005-10-05 08:12:23 UTC (rev 10127)
+++ trunk/planarity/gameboard_draw_intersection.c	2005-10-05 10:25:42 UTC (rev 10128)
@@ -35,6 +35,7 @@
 
 #include "graph.h"
 #include "gameboard.h"
+#include "main.h"
 
 static void draw_intersection(cairo_t *c,double x, double y){
   cairo_move_to(c,x-INTERSECTION_RADIUS,y);
@@ -45,7 +46,6 @@
 }
 
 static void draw_many_intersection(Gameboard *g,cairo_t *c){
-  cairo_matrix_t ma;
   int x2 = g->g.width/2;
   int y2 = g->g.height/2;
   int r = INTERSECTION_RADIUS*10;
@@ -61,12 +61,7 @@
 
   cairo_stroke(c);
 
-  cairo_select_font_face (c, "Sans",
-			  CAIRO_FONT_SLANT_NORMAL,
-			  CAIRO_FONT_WEIGHT_BOLD);
-  
-  cairo_matrix_init_scale (&ma, 30.,34.);
-  cairo_set_font_matrix (c,&ma);
+  set_font(c, 30., 34., 0,1);
   render_bordertext_centered(c,"rather many, really",x2,y2+r/4);
 
 }

Modified: trunk/planarity/gameboard_draw_score.c
===================================================================
--- trunk/planarity/gameboard_draw_score.c	2005-10-05 08:12:23 UTC (rev 10127)
+++ trunk/planarity/gameboard_draw_score.c	2005-10-05 10:25:42 UTC (rev 10128)
@@ -36,6 +36,7 @@
 #include "graph.h"
 #include "gameboard.h"
 #include "levelstate.h"
+#include "main.h"
 
 void draw_score(Gameboard *g){
   char level_string[160];
@@ -48,9 +49,11 @@
   cairo_text_extents_t extentsM;
   cairo_text_extents_t extentsO;
   cairo_text_extents_t extentsI;
-  cairo_matrix_t m;
 
   cairo_t *c = cairo_create(g->forescore);
+  int xpx = 12;
+  int ty1 = 23;
+  int ty2 = 38;
 
   // clear the pane
   cairo_save(c);
@@ -61,36 +64,21 @@
 
   topbox(g,c,g->g.width,SCOREHEIGHT);
 
-  cairo_select_font_face (c, "Arial",
-			  CAIRO_FONT_SLANT_NORMAL,
-			  CAIRO_FONT_WEIGHT_BOLD);
-
-  cairo_matrix_init_scale (&m, 12.,15.);
-  cairo_set_font_matrix (c,&m);
-  cairo_set_source_rgba (c, TEXT_COLOR);
-
   snprintf(level_string,160,"Level %d: %s",get_level_num()+1,get_level_desc());
   snprintf(score_string,160,"Score: %d",graphscore_get_raw_score(&g->g));
   snprintf(mult_string,160,"x%d",graphscore_get_multiplier(&g->g));
   snprintf(int_string,160,"Intersections: %ld",g->g.active_intersections);
   snprintf(obj_string,160,"Objective: %s",graphscore_objective_string(&g->g));
 
-  cairo_text_extents (c, level_string, &extentsL);
+  set_font(c,xpx,15,0,1);
+  cairo_set_source_rgba (c, TEXT_COLOR);
+
   cairo_text_extents (c, obj_string, &extentsO);
   cairo_text_extents (c, int_string, &extentsI);
   cairo_text_extents (c, score_string, &extentsS);
   cairo_text_extents (c, mult_string, &extentsM);
 
-  /*
-  text_h = extentsL.height;
-  text_h = max(text_h,extentsO.height);
-  text_h = max(text_h,extentsI.height);
-  text_h = max(text_h,extentsS.height);
-  */
 
-  int ty1 = 23;
-  int ty2 = 38;
-
   cairo_move_to (c, 15, ty1);
   cairo_show_text (c, int_string);  
   cairo_move_to (c, 15, ty2);
@@ -103,11 +91,23 @@
     cairo_restore(c);
   }
 
-  cairo_move_to (c, g->g.width-extentsL.width-15, ty1);
-  cairo_show_text (c, level_string);  
   cairo_move_to (c, g->g.width-extentsO.width-15, ty2);
   cairo_show_text (c, obj_string);  
 
+
+  while(xpx){
+    cairo_text_extents (c, level_string, &extentsL);
+
+    if(extentsL.width > 300){
+      xpx--;
+      set_font(c,xpx,15,0,1);
+    }else
+      break;
+  }
+
+  cairo_move_to (c, g->g.width-extentsL.width-15, ty1);
+  cairo_show_text (c, level_string);  
+
   cairo_destroy(c);
 }
 

Modified: trunk/planarity/gameboard_draw_text.c
===================================================================
--- trunk/planarity/gameboard_draw_text.c	2005-10-05 08:12:23 UTC (rev 10127)
+++ trunk/planarity/gameboard_draw_text.c	2005-10-05 10:25:42 UTC (rev 10128)
@@ -39,7 +39,7 @@
   cairo_text_extents (c, s, &ex);
 
   r.x=x-(ex.width/2);
-  r.y=y-(ex.height/2);
+  r.y=y+(ex.y_bearing/2);
   r.width=ex.width;
   r.height=ex.height;
 
@@ -56,7 +56,7 @@
   cairo_text_extents (c, s, &ex);
 
   r.x=x-(ex.width/2)-2;
-  r.y=y-(ex.height/2)-2;
+  r.y=y+(ex.y_bearing/2)-2;
   r.width=ex.width+5;
   r.height=ex.height+5;
 
@@ -76,3 +76,4 @@
   render_text_centered(c,s,x,y);
   return r;
 }
+

Modified: trunk/planarity/gameboard_logic_button.c
===================================================================
--- trunk/planarity/gameboard_logic_button.c	2005-10-05 08:12:23 UTC (rev 10127)
+++ trunk/planarity/gameboard_logic_button.c	2005-10-05 10:25:42 UTC (rev 10128)
@@ -33,6 +33,7 @@
 #include "graph.h"
 #include "gameboard.h"
 #include "gameboard_draw_button.h"
+#include "main.h"
 
 
 /************************ manage the buttons for buttonbar and dialogs *********************/
@@ -63,14 +64,7 @@
 
 /* draw the actual rollover */
 static void stroke_rollover(Gameboard *g, buttonstate *b, cairo_t *c){
-  cairo_matrix_t m;
-
-  cairo_select_font_face (c, "Arial",
-			  CAIRO_FONT_SLANT_NORMAL,
-			  CAIRO_FONT_WEIGHT_BOLD);
-
-  cairo_matrix_init_scale (&m, BUTTON_TEXT_SIZE);
-  cairo_set_font_matrix (c,&m);
+  set_font(c, BUTTON_TEXT_SIZE, 0, 1);
   
   {
     GdkRectangle r=rollover_box(g,b);
@@ -443,16 +437,9 @@
 /* cache the text extents of a rollover */
 void rollover_extents(Gameboard *g, buttonstate *b){
   
-  cairo_matrix_t m;
-
   cairo_t *c = cairo_create(g->foreground);
-  cairo_select_font_face (c, "Arial",
-			  CAIRO_FONT_SLANT_NORMAL,
-			  CAIRO_FONT_WEIGHT_BOLD);
-  
-  cairo_matrix_init_scale (&m, BUTTON_TEXT_SIZE);
-  cairo_set_font_matrix (c,&m);
-  
+
+  set_font(c, BUTTON_TEXT_SIZE, 0, 1);
   cairo_text_extents (c, b->rollovertext, &b->ex);
 
   cairo_destroy(c);

Modified: trunk/planarity/graph_generate.c
===================================================================
--- trunk/planarity/graph_generate.c	2005-10-05 08:12:23 UTC (rev 10127)
+++ trunk/planarity/graph_generate.c	2005-10-05 10:25:42 UTC (rev 10128)
@@ -66,7 +66,7 @@
 
   {"rogue",    5, "A New Objective",                                generate_rogue,    1.6,1., 3 }, // 17
   {"rogue",    6, "How Low Can You Go?",                            generate_rogue,    1.6,1., 3 }, // 18
-  {"rogue",    7, "Industrial Military Complex",                    generate_rogue,    1.6,1., 4 }, // 19
+  {"rogue",    7, "Military Industrial Complex",                    generate_rogue,    1.6,1., 4 }, // 19
 
   {"embed",    4, "The Hexagon is a Subtle and Wily Beast",         generate_embed,     2.,1.,  4 }, // 20
   {"embed",    5, "No, Really, The Hexagon Puzzles Are Harder",     generate_embed,     3.,1.,  5 }, // 21

Modified: trunk/planarity/main.c
===================================================================
--- trunk/planarity/main.c	2005-10-05 08:12:23 UTC (rev 10127)
+++ trunk/planarity/main.c	2005-10-05 10:25:42 UTC (rev 10128)
@@ -29,10 +29,13 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <errno.h>
+#include <math.h>
 #include <signal.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <gtk/gtk.h>
+#include <cairo/cairo-ft.h>
+#include <fontconfig/fontconfig.h>
 #include "version.h"
 #include "graph.h"
 #include "gameboard.h"
@@ -49,7 +52,15 @@
 graph maingraph;
 
 char *version = "";
+static cairo_font_face_t *font_face=0;
+static cairo_font_face_t *bold_face=0;
+static cairo_font_face_t *ital_face=0;
 
+static float adjust_x_normal;
+static float adjust_y_normal;
+static float adjust_x_bold;
+static float adjust_y_bold;
+
 static int dir_create(char *name){
   if(mkdir(name,0700)){
     switch(errno){
@@ -104,6 +115,147 @@
   return 1;  // we're handling it, don't continue the delete chain
 }
 
+/* font handling... still a pain in the ass!  ...but mostly due to
+   lack of documentation.  
+
+   Try first to find one of the default fonts we prefer (FontConfig
+   can search for a list, Cairo cannot).
+
+   Regardless of what's found, try to determine the approximate best
+   aspect ratio of the text; gPlanarity looks best with a relatively
+   tall/narrow font that renders with horizontal/vertical strokes of
+   approximately identical width */
+
+static cairo_font_face_t *init_font(char *list, int slant,int bold){
+  cairo_font_face_t *ret;
+  char *fontface;
+  FcPattern *fc_pattern;
+  FcBool scalable;
+  
+  fc_pattern = FcNameParse(list);
+
+  // load the system config
+  FcConfigSubstitute(0, fc_pattern, FcMatchPattern);
+
+  FcPatternDel(fc_pattern,FC_SLANT);
+  FcPatternDel(fc_pattern,FC_WEIGHT);
+  if(slant)
+    FcPatternAddInteger(fc_pattern,FC_SLANT,FC_SLANT_ITALIC);
+  if(bold)
+    FcPatternAddInteger(fc_pattern,FC_WEIGHT,FC_WEIGHT_BOLD);
+
+  // fill in missing defaults
+  FcDefaultSubstitute(fc_pattern);
+  // find a font face on our list if possible
+  fc_pattern = FcFontMatch(0, fc_pattern, 0);
+
+  if(!fc_pattern){
+    fprintf(stderr,"\nUnable to find any suitable %s fonts!\n"
+	    "Continuing, but the the results are likely to be poor.\n\n",
+
+	    (slant?(bold?"bold italic":"italic"):(bold?"bold":"medium")) );
+  }
+
+  FcPatternGetString (fc_pattern, FC_FAMILY, 0, (FcChar8 **)&fontface);
+
+  /*
+  if(!strstr(list,fontface)){
+    fprintf(stderr,"\nUnable to find any of gPlanarity's preferred %s fonts (%s);\n"
+	    "Using system default font \"%s\".\n\n",
+	    (slant?(bold?"bold italic":"italic"):(bold?"bold":"medium")),
+	    list,fontface);
+	    }*/
+
+  FcPatternGetBool(fc_pattern, FC_SCALABLE, 0, &scalable);
+  if (scalable != FcTrue) {
+    fprintf(stderr,"\nSelected %s font \"%s\" is not scalable!  This is almost as bad\n"
+	    "as not finding any font at all.  Continuing, but this may look\n"
+	    "very poor indeed.\n\n",
+	    (slant?(bold?"bold italic":"italic"):(bold?"bold":"medium")), fontface);
+  }
+  
+  /* Set the hinting behavior we want; only the autohinter is giving
+     decent results across a wide range of sizes and face options.
+     Don't obey the system config with respect to hinting; every one
+     of my systems installed a default config that resulted in the
+     available fonts looking like ass.  Bitstream Vera Sans especially
+     looks bad without the autohinter, and that's the font most
+     systems will have as the only sans default. */
+  
+  // Must remove the preexisting settings!  The settings are a list,
+  // and we're tacking ours on the end; Cairo only inspects first hit
+  FcPatternDel(fc_pattern,FC_HINT_STYLE);
+  FcPatternDel(fc_pattern,FC_HINTING);
+  FcPatternDel(fc_pattern,FC_AUTOHINT);
+
+  // Add ours requesting autohint
+  FcPatternAddBool(fc_pattern,FC_HINTING,FcTrue);
+  FcPatternAddBool(fc_pattern,FC_AUTOHINT,FcTrue);
+
+  // Make the font face
+  ret = cairo_ft_font_face_create_for_pattern(fc_pattern);
+  FcPatternDestroy(fc_pattern);
+  return ret;
+}
+
+static void init_fonts(char *list){
+
+  font_face = init_font(list,0,0);
+  bold_face = init_font(list,0,1);
+  ital_face = init_font(list,1,0);
+
+  // We've done the best we can, font-choice-wise.  Determine an
+  // aspect ratio correction for whatever font we've got.  Arial is
+  // declared to be the ideal aspect, correct other fonts to the same
+  // rough proportions (it looks better).  This is a rough hack, but
+  // yields an unmistakable improvement.
+
+  {
+    cairo_text_extents_t ex;
+    char *test_string = "Practice Before the Handbasket: Three of Three";
+    cairo_surface_t *s = 
+      cairo_image_surface_create (CAIRO_FORMAT_ARGB32,16,16);
+    cairo_t *c = cairo_create(s);
+    cairo_matrix_t m;
+    
+    cairo_set_font_face (c, font_face);
+    cairo_matrix_init_scale (&m, 50.,50.);
+    cairo_set_font_matrix (c,&m);
+    cairo_text_extents (c, test_string, &ex);
+    adjust_x_normal = 1058 / ex.width;
+    adjust_y_normal = -37 / ex.y_bearing;
+    fprintf(stderr,"%f,%f\n",ex.width,ex.y_bearing);
+
+
+    cairo_set_font_face (c, bold_face);
+    cairo_text_extents (c, test_string, &ex);
+    adjust_x_bold = 1130 / ex.width;
+    adjust_y_bold = -37 / ex.y_bearing;
+    fprintf(stderr,"%f,%f\n",ex.width,ex.y_bearing);
+
+
+    cairo_destroy(c);
+    cairo_surface_destroy(s);
+  }
+}
+
+void set_font (cairo_t *c, float w, float h, int slant, int bold){
+  cairo_matrix_t m;
+
+  if(slant){
+    cairo_set_font_face(c,ital_face);
+    cairo_matrix_init_scale (&m, rint(w*adjust_x_normal),rint(h*adjust_y_normal));
+  }else if (bold){
+    cairo_set_font_face(c,bold_face);
+    cairo_matrix_init_scale (&m, rint(w*adjust_x_bold),rint(h*adjust_y_bold));
+  }else{
+    cairo_set_font_face(c,font_face);
+    cairo_matrix_init_scale (&m, rint(w*adjust_x_normal),rint(h*adjust_y_normal));
+  }
+  cairo_set_font_matrix (c,&m);
+
+}
+
 int main(int argc, char *argv[]){
   char *homedir = getenv("home");
   if(!homedir)
@@ -145,6 +297,8 @@
     version="";
   }
 
+  init_fonts("");
+
   gtk_init (&argc, &argv);
 
   toplevel_window   = gtk_window_new (GTK_WINDOW_TOPLEVEL);
@@ -173,5 +327,10 @@
 
   if(gameboard !=0 )
     levelstate_write();
+
+  cairo_font_face_destroy(font_face);
+  cairo_font_face_destroy(bold_face);
+  cairo_font_face_destroy(ital_face);
+
   return 0;
 }

Modified: trunk/planarity/main.h
===================================================================
--- trunk/planarity/main.h	2005-10-05 08:12:23 UTC (rev 10127)
+++ trunk/planarity/main.h	2005-10-05 10:25:42 UTC (rev 10128)
@@ -24,8 +24,14 @@
  * 
  */
 
+#include <gtk/gtk.h>
+
 extern char *boarddir;
 extern char *statedir;
+extern char *fontface;
+extern char *version;
+
 extern Gameboard *gameboard;
 
 extern void request_resize(int width, int height);
+extern void set_font(cairo_t *c, float w, float h, int slant, int bold);

Modified: trunk/planarity/version.h
===================================================================
--- trunk/planarity/version.h	2005-10-05 08:12:23 UTC (rev 10127)
+++ trunk/planarity/version.h	2005-10-05 10:25:42 UTC (rev 10128)
@@ -1,2 +1,2 @@
 #define VERSION "$Id$ "
-/* DO NOT EDIT: Automated versioning hack [Mon Oct  3 02:49:41 EDT 2005] */
+/* DO NOT EDIT: Automated versioning hack [Wed Oct  5 06:16:08 EDT 2005] */



More information about the commits mailing list