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

Linux Cross Reference
Tina5/tina-tools/tinatool/gphx/x11/gphxX11_fonts.c

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

  1 /**********
  2  * 
  3  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  4  * University of Manchester, UK.  All rights reserved.
  5  * 
  6  * Redistribution and use in source and binary forms, with or without modification, 
  7  * are permitted provided that the following conditions are met:
  8  * 
  9  *   . Redistributions of source code must retain the above copyright notice, 
 10  *     this list of conditions and the following disclaimer.
 11  *    
 12  *   . Redistributions in binary form must reproduce the above copyright notice,
 13  *     this list of conditions and the following disclaimer in the documentation 
 14  *     and/or other materials provided with the distribution.
 15  * 
 16  *   . Neither the name of the University of Manchester nor the names of its
 17  *     contributors may be used to endorse or promote products derived from this 
 18  *     software without specific prior written permission.
 19  * 
 20  * 
 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  * POSSIBILITY OF SUCH DAMAGE.
 32  *
 33  **********
 34  * 
 35  * Program :    TINA
 36  * File    :  $Source: /home/tina/cvs/tina-tools/tinatool/gphx/x11/gphxX11_fonts.c,v $
 37  * Date    :  $Date: 2003/10/01 16:02:47 $
 38  * Version :  $Revision: 1.2 $
 39  * CVS Id  :  $Id: gphxX11_fonts.c,v 1.2 2003/10/01 16:02:47 tony Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes   :
 44  * 
 45  *Font creation and usage.
 46  *
 47  * Fonts are created for each display as they are needed.  They are
 48  * specified at the tv level as a string.  To change the font used in a
 49  * tv, call tv_named_font_set with a string (length < 128).  This font
 50  * is then used wherever the tv is installed (or a default if the font
 51  * name can't be found). 
 52  *
 53  *********
 54 */
 55 
 56 #include "gphxX11_fonts.h"
 57 
 58 #if HAVE_CONFIG_H
 59   #include <config.h>
 60 #endif
 61 
 62 
 63 static char default_font_name[256] = "6x*";
 64 
 65 void    default_font_set(char *font_name)
 66 {
 67    (void) strcpy(default_font_name, font_name);
 68 }
 69 
 70 char   *default_font_get(void)
 71 {
 72     return default_font_name;
 73 }
 74 
 75 static void *font_match(Font_data * key, Font_data * data)
 76 {
 77     return (key->display == data->display &&
 78      !strcmp(key->font_name, data->font_name) ? (void *) data : NULL);
 79 }
 80 
 81 
 82 static Bool font_exists(Display * display, char *font_name)
 83 {
 84     char  **list;
 85     int     count = 0;
 86     static int max_names = 1;
 87 
 88     list = XListFonts(display, font_name, max_names, &count);
 89     if (count && strcmp(font_name,list[0]))
 90     {
 91        format("nearest font %s \n",list[0]);
 92        strcpy(font_name,list[0]);
 93     }
 94     XFreeFontNames(list);
 95 
 96     return (Bool) count;
 97 }
 98 
 99 
100 static XFontStruct *font_find(Display * display, char *font_name)
101 {
102     Font_data *font_data = NULL;
103     Font_data font_data_key;
104     static List *font_database = NULL;
105 
106     font_data_key.display = display;
107     font_data_key.font_name = font_name;
108 
109     font_data = (Font_data *) list_query(font_database, font_match,
110                                          (void *) &font_data_key);
111 
112     if (font_data)
113     {
114 
115         /****
116           format("Font: %x %s got from database.\n", font_data->font, font_name);
117           ****/
118     } else if (font_exists(display, font_name))
119     {
120         font_data = ts_ralloc(Font_data);
121 
122         font_data->display = display;
123         font_data->font_name = string_copy(font_name);
124         font_data->font = XLoadQueryFont(display, font_name);
125 
126         font_database = ref_addtostart(font_database, (void *) font_data, 0);
127 
128         /****
129           format("Font: %x %s added to database.\n", font_data->font, font_name);
130           ****/
131     }
132     return (font_data ? font_data->font : None);
133 }
134 
135 
136 /* Set a named font in a graphics context on a display.  Returns the
137  * font (or None (0) if font_name not found).  NB THIS FONT IS ONLY
138  * VALID ON THE GIVEN DISPLAY. */
139 static XFontStruct *named_font_set(Display * display, GC gc, char *font_name)
140 {
141     XFontStruct *font = NULL;
142 
143     if (!*font_name)
144     {
145         font_name = default_font_name;
146     }
147     font = font_find(display, font_name);
148 
149     if (font)
150     {
151         XSetFont(display, gc, font->fid);
152     } else
153     {
154         format("named_font_set: Can't find font: %s.\n", font_name);
155     }
156     return font;
157 }
158 
159 
160 /* Set a named font (char*) in a tv_screen.  Sets tv_screen->font to
161  * the corresponding font (XFontStruct*) */
162 void    tv_screen_named_font_set(Tv_screen * tv_screen, char *font_name)
163 {
164     if (tv_screen)
165     {
166         Display *display = tv_screen->display;
167         GC      gc = tv_screen->gc;
168 
169         (void) named_font_set(display, gc, font_name);
170     }
171 }
172 
173 /* Get info on a font (XFontStruct*).  NB DOESN'T SET FONT IN TV_SCREEN */
174 void    tv_screen_get_text_extent(Tv_screen * tv_screen, char *font_name, char *string, int *xmin, int *xmax, int *ymin, int *ymax)
175 {
176     if (!font_name || !*font_name)
177     {
178         font_name = default_font_name;
179     }
180     if (tv_screen)
181     {
182         XFontStruct *font = font_find(tv_screen->display, font_name);
183         XCharStruct info;
184         int     len, dir, asc, desc;
185 
186         if (font)
187         {
188            len = strlen(string);
189            XTextExtents(font, string, len, &dir, &asc, &desc, &info);
190 
191            *xmin = info.lbearing;
192            *xmax = info.rbearing;
193            *ymin = -info.ascent;
194            *ymax = info.descent;
195         }
196         else
197             *xmin = *xmax = *ymin = *ymax = 0;
198     } else
199     {
200         *xmin = *xmax = *ymin = *ymax = 0;
201     }
202 }
203 

~ [ 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.