File: font.c

package info (click to toggle)
oleo 1.6-16
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,640 kB
  • ctags: 3,139
  • sloc: ansic: 39,221; yacc: 1,737; sh: 362; makefile: 88
file content (265 lines) | stat: -rw-r--r-- 6,165 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*	Copyright (C) 1992, 1993 Free Software Foundation, Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this software; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <ctype.h>
#include <stdio.h>
#include <errno.h>
#include "font.h"
#include "window.h"
#include "io-abstract.h"
#include "cmd.h"
#include "io-x11.h"
#include "io-term.h"
#include "io-utils.h"




static char * default_oleo_name = "default";
static char * default_x_name = "*times-medium-r-*";
static char * default_ps_name = "Times-Roman";

static struct font_names * font_names = 0;
struct font_memo * font_list = 0;
struct font_memo * the_default_font = 0;


void
define_font (char * oleo_name, char * x_name, char * ps_name)
{
  struct font_names * fn;

  for (fn = font_names; fn; fn = fn->next)
    if (!stricmp(fn->oleo_name, oleo_name))
      {
	if (fn->x_name)
	  free (fn->x_name);
	if (fn->ps_name)
	  free (fn->ps_name);
	break;
      }
  if (!fn)
    {
      fn = (struct font_names *)ck_malloc (sizeof (*fn));
      fn->oleo_name =  strdup (oleo_name);
      fn->next = font_names;
      font_names = fn;
    }
  fn->x_name = strdup (x_name);
  fn->ps_name = strdup (ps_name);
}


static struct font_names * 
find_font_name (char * name)
{
  struct font_names * fn = font_names;
  if (!name || says_default (name))
    name = "default";

  while (fn)
    {
      if (!stricmp (name, fn->oleo_name))
	return fn;
      fn = fn->next;
    }
  io_error_msg ("Unknown font family %s\n", name);
  return 0;			/* never reached, actually */
}


/* For backwards compatability, there are created-on-demand fonts
 * who's oleo name is a concatenation of their x and postscript names.
 */

static struct font_names *
matching_font_names (char * x_name, char * ps_name)
{
  struct font_names * fn;
  for (fn = font_names; fn; fn = fn->next)
    {
      if (   stricmp (fn->oleo_name, "default")
	  && !stricmp (ps_name, fn->ps_name)
	  && !stricmp (x_name, fn->x_name))
	return fn;
    }
  {
    char * oleo_name = mk_sprintf ("%s,%s", x_name, ps_name);
    define_font (oleo_name, x_name, ps_name);
    fn = find_font_name (oleo_name);
    free (oleo_name);
    return fn;
  }
}



struct font_memo *
intern_font (char * oleo_name, double scale)
{
  struct font_names *names = find_font_name (oleo_name);
  struct font_memo *it = font_list;
  while (it)
    {
      if ((scale == font_list->scale) && (names == font_list->names))
	return it;
      it = it->next;
    }
  it = (struct font_memo *)ck_malloc (sizeof (*it));
  it->scale = scale;
  it->names = names;
  it->next = font_list;
  font_list = it;
  return it;
}

struct font_memo * 
default_font (void)
{
  return the_default_font;
}

struct font_memo *
matching_font (char * x_name, char * ps_name, double scale)
{
  struct font_names * fn = matching_font_names (x_name, ps_name);
  return intern_font (fn->oleo_name, scale);
}

/* This is for the benefit of oleofile.c only */

struct font_memo *
parsed_matching_font (char * fullname)
{
  char x_name[1000];
  char ps_name[1000];
  char *p;
  char *p2;
  double scale;

  for (p = fullname; isspace (*p); ++p);
  for (p2 = x_name; *p && (*p != ',') && !isspace (*p); *p2++ = *p++);
  *p2 = '\0';
  if (x_name[0] == '\0' || says_default(x_name))
    strcpy (x_name, default_x_name);
/* Got rid of the weird defined font-name expansion here. 
 * These auto-magical fonts are only for the sake of reading old files
 * which don't use that (cough) feature.
 */
  while (isspace (*p))   ++p;
  if (*p == ',')    ++p;
  while (isspace (*p))  ++p;
  for (p2 = ps_name; *p && (*p != ',') && !isspace (*p); *p2++ = *p++) ;
  *p2 = '\0';

  if (ps_name[0] == '\0' || says_default(x_name))
    strcpy (ps_name, default_ps_name);
  while (isspace (*p))     ++p;
  if (*p == ',')     ++p;
  while (isspace (*p))     ++p;

  if (isdigit (*p))
    {
      errno = 0;
      scale = atof (p);
      if (errno)
	scale = 1.;
    }
  else
    scale = 1.;

  return matching_font (x_name, ps_name, scale);
}



void 
set_region_font (struct rng *rng, char * oleo_name, double scale)
{
  struct font_memo *font = ((oleo_name || scale != 1.0)
			    ? intern_font (oleo_name, scale)
			    : 0);
  CELL * cp;
  
  make_cells_in_range (rng);
  cp = next_cell_in_range ();
  while (cp)
    {
      cp->cell_font = font;
      cp = next_cell_in_range ();
    }
  io_redo_region (rng);
}

void 
flush_fonts (void)
{
  CELL * cp;
  struct rng rng;
  rng.lr = MIN_ROW;
  rng.hr = MAX_ROW;
  rng.lc = MIN_COL;
  rng.hc = MAX_COL;
  find_cells_in_range (&rng);
  cp = next_cell_in_range ();
  while (cp)
    {
      cp->cell_font = 0;
      cp = next_cell_in_range ();
    }
}

void
set_x_default_font (char * str)
{
  define_font (default_oleo_name, str, the_default_font->names->ps_name);
  io_repaint ();
}

void
set_ps_font_cmd (char * ps_name)
{
  define_font (default_oleo_name, the_default_font->names->x_name, ps_name);
}

void
set_default_font (char * name, double scale)
{
  struct font_names * fn = find_font_name (name);
  if (fn != find_font_name (default_oleo_name))
    define_font (default_oleo_name, fn->x_name, fn->ps_name);
  the_default_font = intern_font (default_oleo_name, scale);
#ifndef X_DISPLAY_MISSING
  if (using_x)
    set_x_default_point_size (cell_font_point_size);
  else
#endif
    io_repaint ();
}


void 
init_fonts (void)
{
  define_font (default_oleo_name, default_x_name, default_ps_name);
  define_font ("times", default_x_name, default_ps_name);
  define_font ("f9x15", "9x15", "sdlkfj");
  the_default_font = intern_font (default_oleo_name, 1.0);
}