~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina4/src/X11/fonts.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /** @(#)Font creation and usage.
  2  **/
  3 
  4 /* Fonts are created for each display as they are needed.  They are
  5  * specified at the tv level as a string.  To change the font used in a
  6  * tv, call tv_named_font_set with a string (length < 128).  This font
  7  * is then used wherever the tv is installed (or a default if the font
  8  * name can't be found). */
  9 #include <stdio.h>
 10 #include <tina/sys.h>
 11 #include <tina/sysfuncs.h>
 12 #include <tina/math.h>
 13 #include <tina/tv.h>
 14 #include <tina/tv_screen.h>
 15 
 16 static char default_font_name[256] = "6x*";
 17 
 18 void    default_font_set(char *font_name)
 19 {
 20    (void) strcpy(default_font_name, font_name);
 21 }
 22 
 23 char   *default_font_get(void)
 24 {
 25     return default_font_name;
 26 }
 27 
 28 static void *font_match(Font_data * key, Font_data * data)
 29 {
 30     return (key->display == data->display &&
 31      !strcmp(key->font_name, data->font_name) ? (void *) data : NULL);
 32 }
 33 
 34 
 35 static Bool font_exists(Display * display, char *font_name)
 36 {
 37     char  **list;
 38     int     count = 0;
 39     static int max_names = 1;
 40 
 41     list = XListFonts(display, font_name, max_names, &count);
 42     if (count && strcmp(font_name,list[0]))
 43     {
 44        format("nearest font %s \n",list[0]);
 45        strcpy(font_name,list[0]);
 46     }
 47     XFreeFontNames(list);
 48 
 49     return (Bool) count;
 50 }
 51 
 52 
 53 static XFontStruct *font_find(Display * display, char *font_name)
 54 {
 55     Font_data *font_data = NULL;
 56     Font_data font_data_key;
 57     static List *font_database = NULL;
 58 
 59     font_data_key.display = display;
 60     font_data_key.font_name = font_name;
 61 
 62     font_data = (Font_data *) list_query(font_database, font_match,
 63                                          (void *) &font_data_key);
 64 
 65     if (font_data)
 66     {
 67 
 68         /****
 69           format("Font: %x %s got from database.\n", font_data->font, font_name);
 70           ****/
 71     } else if (font_exists(display, font_name))
 72     {
 73         font_data = ts_ralloc(Font_data);
 74 
 75         font_data->display = display;
 76         font_data->font_name = string_copy(font_name);
 77         font_data->font = XLoadQueryFont(display, font_name);
 78 
 79         font_database = ref_addtostart(font_database, (void *) font_data, 0);
 80 
 81         /****
 82           format("Font: %x %s added to database.\n", font_data->font, font_name);
 83           ****/
 84     }
 85     return (font_data ? font_data->font : None);
 86 }
 87 
 88 
 89 /* Set a named font in a graphics context on a display.  Returns the
 90  * font (or None (0) if font_name not found).  NB THIS FONT IS ONLY
 91  * VALID ON THE GIVEN DISPLAY. */
 92 static XFontStruct *named_font_set(Display * display, GC gc, char *font_name)
 93 {
 94     XFontStruct *font = NULL;
 95 
 96     if (!*font_name)
 97     {
 98         font_name = default_font_name;
 99     }
100     font = font_find(display, font_name);
101 
102     if (font)
103     {
104         XSetFont(display, gc, font->fid);
105     } else
106     {
107         format("named_font_set: Can't find font: %s.\n", font_name);
108     }
109     return font;
110 }
111 
112 
113 /* Set a named font (char*) in a tv_screen.  Sets tv_screen->font to
114  * the corresponding font (XFontStruct*) */
115 void    tv_screen_named_font_set(Tv_screen * tv_screen, char *font_name)
116 {
117     if (tv_screen)
118     {
119         Display *display = tv_screen->display;
120         GC      gc = tv_screen->gc;
121 
122         (void) named_font_set(display, gc, font_name);
123     }
124 }
125 
126 /* Get info on a font (XFontStruct*).  NB DOESN'T SET FONT IN TV_SCREEN */
127 void    tv_screen_get_text_extent(Tv_screen * tv_screen, char *font_name, char *string, int *xmin, int *xmax, int *ymin, int *ymax)
128 {
129     if (!font_name || !*font_name)
130     {
131         font_name = default_font_name;
132     }
133     if (tv_screen)
134     {
135         XFontStruct *font = font_find(tv_screen->display, font_name);
136         XCharStruct info;
137         int     len, dir, asc, desc;
138 
139         if (font)
140         {
141            len = strlen(string);
142            XTextExtents(font, string, len, &dir, &asc, &desc, &info);
143 
144            *xmin = info.lbearing;
145            *xmax = info.rbearing;
146            *ymin = -info.ascent;
147            *ymax = info.descent;
148         }
149         else
150             *xmin = *xmax = *ymin = *ymax = 0;
151     } else
152     {
153         *xmin = *xmax = *ymin = *ymax = 0;
154     }
155 }
156 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.