summaryrefslogtreecommitdiff
path: root/console/dynafont.c
blob: cfae051d98aa3365d6c26192d3a49a4a7b9525f6 (plain)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* dynafont.c - Dynamic font handling.
   Copyright (C) 2002 Free Software Foundation, Inc.
   Written by Marcus Brinkmann.

   This file is part of the GNU Hurd.

   The GNU Hurd 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.

   The GNU Hurd 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 program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA. */

#include <assert.h>
#include <malloc.h>
#include <wchar.h>
#include <hurd/ihash.h>
#include <string.h>

#include "vga-hw.h"
#include "vga.h"
#include "bdf.h"
#include "dynafont.h"


/* The currently active (visible) dynafont.  */
static dynafont_t active_dynafont;

/* One glyph in a VGA font is 8 pixels wide and 32 pixels high.  Only
   the first N lines are visible, and N depends on the VGA register
   settings.  */
typedef char vga_font_glyph[VGA_FONT_HEIGHT];

/* For each glyph in the VGA font, one instance of this structure is
   held in the dynafont.  */
struct mapped_character
{
  /* Reference count of the mapped character.  If this drops zero, we
     can remove the mapping at any time.  */
  int refs;

  /* Remember the character this glyph belongs to, so the glyph can be
     reloaded when the font is changed.  */
  wchar_t character;

  /* Used by libihash for fast removal of elements.  */
  void **locp;
};


struct dynafont
{
  /* The sorted font to take the glyphs from.  */
  bdf_font_t font;

  /* The size of the VGA font (256 or 512).  Must be a power of 2!  */
  int size;

  /* A hash containing a pointer to a struct mapped_character for each
     UCS-4 character we map to a VGA font index.  */
  ihash_t charmap;

  /* The struct mapped_characters are preallocated for all vga font
     index values.  This points to an array of SIZE such elements.  */
  struct mapped_character *charmap_data;

  /* The last vga font index that had been free (or could be reused).  */
  int vga_font_last_free_index;

  /* The number of free slots in the VGA font.  */
  int vga_font_free_indices;

  /* The font memory as stored on the card.  */
  vga_font_glyph *vga_font;
};


/* Create a new dynafont object, which uses glyphs from the font FONT
   (which must be 8 pixels wide and up to 32 pixels heigh).  SIZE is
   either 256 or 512, and specifies the number of available glyphs in
   the font cache.  The object is returned in DYNAFONT.  The caller
   must ensure the integrity of FONT until the object is destroyed or
   the font is changed with dynafont_change_font.  */
error_t
dynafont_new (bdf_font_t font, int size, dynafont_t *dynafont)
{
  error_t err = 0;
  dynafont_t df;
  struct bdf_glyph *glyph = NULL;

  df = malloc (sizeof *df);
  if (!df)
    return ENOMEM;

  df->font = font;
  df->size = size;
  err = ihash_create (&df->charmap);
  if (err)
    {
      free (df);
      return err;
    }

  df->charmap_data = calloc (size, sizeof (struct mapped_character));
  if (!df->charmap_data)
    {
      ihash_free (df->charmap);
      free (df);
      return ENOMEM;
    }

  df->vga_font = malloc (sizeof (vga_font_glyph) * size);
  if (!df->vga_font)
    {
      ihash_free (df->charmap);
      free (df->charmap_data);
      free (df);
      return ENOMEM;
    }
  df->vga_font_free_indices = df->size;
  df->vga_font_last_free_index = 0;

  /* The encoding of the unknown glyph.  Some fonts provide an empty
     box for this encoding.  Undefine this if you always want to use
     the built-in font.  XXX Make this a command line option.  */
#define ENCODING_UNKNOWN 0
  /* The encoding of the space.  All fonts should provide it.  */
#define ENCODING_SPACE 32

  {
    struct mapped_character *chr = &df->charmap_data[FONT_INDEX_UNKNOWN];
    df->vga_font_free_indices--;
    chr->refs = 1;

#if defined(ENCODING_UNKNOWN)
    glyph = bdf_find_glyph (df->font, ENCODING_UNKNOWN, 0);
    if (!glyph)
      glyph = bdf_find_glyph (df->font, -1, ENCODING_UNKNOWN);
#endif
    if (glyph)
      {
	/* XXX Take glyph size into account.  */
	memcpy (df->vga_font[FONT_INDEX_UNKNOWN], glyph->bitmap, 32);
	/* Update the hash table.  */
	ihash_add (df->charmap, ENCODING_UNKNOWN, chr, &chr->locp);
      }
    else
      {
	int i;
	char *gl = df->vga_font[FONT_INDEX_UNKNOWN];
	/* XXX Take font height into account.  */
	gl[0] = 0xFF;	/* ******** */
	gl[1] = 0xC3;	/* **    ** */
	gl[2] = 0x99;	/* *  **  * */
	gl[3] = 0x99;	/* *  **  * */
	gl[4] = 0xF9;	/* *****  * */
	gl[5] = 0xF3;	/* ****  ** */
	gl[6] = 0xF3;	/* ****  ** */
	gl[7] = 0xE7;	/* ***  *** */
	gl[8] = 0xE7;	/* ***  *** */
	gl[9] = 0xFF;	/* ******** */
	gl[10] = 0xE7;	/* ***  *** */
	gl[11] = 0xFF;	/* ******** */
	for (i = 12; i < 32; i++)
	  gl[i] = 0;
      }

#if defined(ENCODING_SPACE)
    glyph = bdf_find_glyph (df->font, ENCODING_SPACE, 0);
    if (!glyph)
      glyph = bdf_find_glyph (df->font, -1, ENCODING_SPACE);
#endif
    if (glyph)
      {
	/* XXX Take glyph size into account.  */
	memcpy (df->vga_font[FONT_INDEX_SPACE], glyph->bitmap, 32);
	/* Update the hash table.  */
	ihash_add (df->charmap, ENCODING_SPACE, chr, &chr->locp);
      }
    else
      {
	int i;
	char *gl = df->vga_font[FONT_INDEX_SPACE];
	for (i = 0; i < 32; i++)
	  gl[i] = 0;
      }
  }
  *dynafont = df;
  return err;
}


/* Release a dynafont object and its associated resources.  */
void
dynafont_free (dynafont_t df)
{
  ihash_free (df->charmap);
  free (df->charmap_data);
  free (df->vga_font);
  free (df);
}


/* Look up the vga font index for an UCS-4 character.  If not already
   mapped, try to find space for a new entry and add the mapping.
   Acquires an additional reference to the character.  Might return
   the glyph for the unrepresentable character if the glyph is ot
   available for this character or no free slot is available right
   now.  In the former case, some information gets lost (to do
   otherwise, one would have to either assign one of the scarce font
   indices to each undisplayable character value on screen, or to
   store the whole scrollback buffer as wide chars as well to recover
   the lost info from that copy of the original text.  */
int
dynafont_lookup (dynafont_t df, wchar_t wide_chr)
{
  struct mapped_character *chr = ihash_find (df->charmap, (int) wide_chr);

  if (chr)
    {
      if (!chr->refs++)
	df->vga_font_free_indices--;
      return (chr - df->charmap_data) / sizeof (struct mapped_character);
    }

  /* The character is not currently mapped.  Look for an empty
     slot and add it.  */
  if (df->vga_font_free_indices)
    {
      int pos = (df->vga_font_last_free_index + 1) % df->size;
      struct bdf_glyph *glyph;

      glyph = bdf_find_glyph (df->font, (int) wide_chr, 0);
      if (!glyph)
	glyph = bdf_find_glyph (df->font, -1, (int) wide_chr);
      if (glyph)
	{
	  while (pos != df->vga_font_last_free_index)
	    {
	      if (df->charmap_data[pos].refs == 0)
		{
		  /* Ok, we found a new entry, use it.  */
		  df->vga_font_last_free_index = pos;
		  chr = &df->charmap_data[pos];
		  chr->refs = 1;
		  memcpy (df->vga_font[pos], glyph->bitmap, VGA_FONT_HEIGHT);
		  /* XXX This will have to be updated when fonts are
		     cached.  */
		  if (active_dynafont == df)
		    vga_write_font_buffer (0, pos, (char *) glyph->bitmap,
					   VGA_FONT_HEIGHT);
		  /* Update the hash table.  */
		  if (chr->locp)
		    ihash_locp_remove (df->charmap, chr->locp);
		  ihash_add (df->charmap, (int) wide_chr, chr, &chr->locp);
		  return pos;
		}
	      pos = (pos + 1) % df->size;
	    }
	  /* Should never be reached.  */
	  assert (!"No free vga font index.");
	}
    }

  df->charmap_data[FONT_INDEX_UNKNOWN].refs++;
  return FONT_INDEX_UNKNOWN;
}


/* Release a reference to the glyph VGA_FONT_INDEX in dynafont DF.  */
void
dynafont_release (dynafont_t df, int vga_font_index)
{
  if (! --df->charmap_data[vga_font_index].refs)
    df->vga_font_free_indices++;
}


/* Load the VGA font to the card and make it active.  */
void
dynafont_activate (dynafont_t df)
{
  /* XXX Replace this with some caching method.  We have eight font
     slots available.  */
  vga_write_font_buffer (0, 0, (char *) df->vga_font,
			 df->size * VGA_FONT_HEIGHT);
  vga_select_font_buffer (0, (df->size == 512) ? 1 : 0);
  active_dynafont = df;
}


/* Change the font used by dynafont DF to FONT.  This transformation
   is initially loss-less, if the new font can't display some
   characters on the screen, you can always go back to a font that
   does and get the glyphs for those characters back.  (However, the
   comments in dynafont_lookup hold for glyphs looked up after the font
   change.)  */
void
dynafont_change_font (dynafont_t df, bdf_font_t font)
{
  int i;

  df->font = font;
  for (i = 0; i < df->size; i++)
    {
      /* If we don't derive the unknown or space glyph from the font,
	 we don't update it.  */
#ifndef ENCODING_UNKNOWN
      if (i == FONT_INDEX_UNKNOWN)
	continue;
#endif
#ifndef ENCODING_SPACE
      if (i == FONT_INDEX_SPACE)
	continue;
#endif
      if (! df->charmap_data[i].refs)
	{
	  /* The glyph is not used.  If it is mapped, we need to
	     remove the mapping to invalidate the glyph.  */
	  if (df->charmap_data[i].locp)
	    ihash_locp_remove (df->charmap, df->charmap_data[i].locp);
	}

      else
	{
	  /* The glyph is mapped and in use.  We will not destroy the
	     mapping, but just override the glyph in the VGA font.
	     This way the user can recover from loading a bad font by
	     going back to a better one.  */
	  struct bdf_glyph *glyph;
	  glyph = bdf_find_glyph (df->font,
				  (int) df->charmap_data[i].character, 0);
	  if (!glyph)
	    glyph = bdf_find_glyph (df->font, -1,
				    (int) df->charmap_data[i].character);
	  if (!glyph)
	    {
#ifdef ENCODING_UNKNOWN
	      /* The new font doesn't have a representation for the
		 unknown glyph at the desired place, so keep the old
		 one.  XXX Should load the built-in one here.  */
	      if (i == FONT_INDEX_UNKNOWN)
		continue;
#endif
#ifdef ENCODING_SPACE
	      /* The new font doesn't have a representation for the
		 blank glyph at the desired place, so keep the old
		 one.  XXX Should load the built-in one here.  */
	      if (i == FONT_INDEX_SPACE)
		continue;
#endif
	      memcpy (df->vga_font[i], df->vga_font[FONT_INDEX_UNKNOWN],
		    32);
	    }
	  else
	    /* XXX Take font size and glyph size into account.  */
	    memcpy (df->vga_font[i], glyph->bitmap, 32);
	}
    }

  /* XXX This will have to be changed when we use font caching.  */
  if (active_dynafont == df)
    vga_write_font_buffer (0, 0, (char *) df->vga_font,
			   df->size * VGA_FONT_HEIGHT);
}