blob: 0ae7305de7556f8b3ca11342c12f0cd1cc90e1ce [file] [log] [blame]
DRC2ff39b82011-07-28 08:38:59 +00001//
2// "$Id: fl_set_font.cxx 7903 2010-11-28 21:06:39Z matt $"
3//
4// Font utilities for the Fast Light Tool Kit (FLTK).
5//
6// Copyright 1998-2010 by Bill Spitzak and others.
7//
8// This library is free software; you can redistribute it and/or
9// modify it under the terms of the GNU Library General Public
10// License as published by the Free Software Foundation; either
11// version 2 of the License, or (at your option) any later version.
12//
13// This library is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16// Library General Public License for more details.
17//
18// You should have received a copy of the GNU Library General Public
19// License along with this library; if not, write to the Free Software
20// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21// USA.
22//
23// Please report all bugs and problems on the following page:
24//
25// http://www.fltk.org/str.php
26//
27
28// Add a font to the internal table.
29// Also see fl_set_fonts.cxx which adds all possible fonts.
30
31#include <FL/Fl.H>
32#include <FL/x.H>
33#include <FL/fl_draw.H>
34#include "flstring.h"
35#include "Fl_Font.H"
36#include <stdlib.h>
37
38static int table_size;
39/**
40 Changes a face. The string pointer is simply stored,
41 the string is not copied, so the string must be in static memory.
42*/
43void Fl::set_font(Fl_Font fnum, const char* name) {
44 while (fnum >= table_size) {
45 int i = table_size;
46 if (!i) { // don't realloc the built-in table
47 table_size = 2*FL_FREE_FONT;
48 i = FL_FREE_FONT;
49 Fl_Fontdesc* t = (Fl_Fontdesc*)malloc(table_size*sizeof(Fl_Fontdesc));
50 memcpy(t, fl_fonts, FL_FREE_FONT*sizeof(Fl_Fontdesc));
51 fl_fonts = t;
52 } else {
53 table_size = 2*table_size;
54 fl_fonts=(Fl_Fontdesc*)realloc(fl_fonts, table_size*sizeof(Fl_Fontdesc));
55 }
56 for (; i < table_size; i++) {
57 fl_fonts[i].fontname[0] = 0;
58 fl_fonts[i].name = 0;
59#if !defined(WIN32) && !defined(__APPLE__)
60 fl_fonts[i].xlist = 0;
61 fl_fonts[i].n = 0;
62#endif // !WIN32 && !__APPLE__
63 }
64 }
65 Fl_Fontdesc* s = fl_fonts+fnum;
66 if (s->name) {
67 if (!strcmp(s->name, name)) {s->name = name; return;}
68#if !defined(WIN32) && !defined(__APPLE__)
69 if (s->xlist && s->n >= 0) XFreeFontNames(s->xlist);
70#endif
71 for (Fl_Font_Descriptor* f = s->first; f;) {
72 Fl_Font_Descriptor* n = f->next; delete f; f = n;
73 }
74 s->first = 0;
75 }
76 s->name = name;
77 s->fontname[0] = 0;
78#if !defined(WIN32) && !defined(__APPLE__)
79 s->xlist = 0;
80#endif
81 s->first = 0;
82 fl_font(-1, 0);
83}
84/** Copies one face to another. */
85void Fl::set_font(Fl_Font fnum, Fl_Font from) {
86 Fl::set_font(fnum, get_font(from));
87}
88/**
89 Gets the string for this face. This string is different for each
90 face. Under X this value is passed to XListFonts to get all the sizes
91 of this face.
92*/
93const char* Fl::get_font(Fl_Font fnum) {return fl_fonts[fnum].name;}
94
95//
96// End of "$Id: fl_set_font.cxx 7903 2010-11-28 21:06:39Z matt $".
97//