File: fixfont.c

package info (click to toggle)
camserv 0.42-9
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,884 kB
  • ctags: 1,123
  • sloc: ansic: 19,694; sh: 7,454; makefile: 206
file content (163 lines) | stat: -rw-r--r-- 4,831 bytes parent folder | download
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
/*  camserv - An internet streaming picture application
 *
 *  Copyright (C) 1999  Jon Travis (jon@dsndata.com)
 *
 *  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 of the License, 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 program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <stdlib.h>

#include "fixfont.h"

/*
 * fixed_font_char_pack:  Pack a char into a FixedFontChar structure
 *                        from the header-file-thingy.  
 *
 * Arguments:             fontoffset = Pointer to character (offset in char[])
 *                        width      = Width of character
 *                        height     = Height of character
 *                        buffer     = Location to place packed character
 */

void fixed_font_char_pack( const unsigned char *fontoffset, 
			   int width, int height,
			   FixedFontChar *buffer )
{
  unsigned char *line_data_cp;
  const unsigned char *fontcp;
  int x, y, runtype;

  /* Create packed representation of font information. 
     Since the fonts in this format are at most 16 pixels wide, we have
     a lot of power to make this 'line_data' be anything we want.  The format
     of line_data is as follows:

     Each line represents information 'runs'.  Each run is given by 1
     char in the line_data.  If 0 <= char < 16, then the next
     char + 1 pixels are turned 'on'.  If 16 <= char < 32, then the
     next char - 15 pixels are turned 'off'.  If char == 255, then
     the runs for the line are completed, and scanning should cease. */

  for( y=0, fontcp = fontoffset; y< height; y++, fontcp++ ){
    line_data_cp = buffer->line_data[ y ];
    runtype = FFONT_RUN_NONE;
    for( x=7; x >= 0; x --){
      if( *fontcp & (1 << x )) { /* Pixel on */
	if( runtype == FFONT_RUN_NONE ) {
	  runtype = FFONT_RUN_ON;
	  *line_data_cp = 0;
	} else if( runtype == FFONT_RUN_ON ) { /* Continuation */
	  (*line_data_cp)++;
	} else {
	  /* Went from off to on */
	  line_data_cp++;
	  runtype = FFONT_RUN_ON;
	  *line_data_cp = 0;
	}
      } else {
	if( runtype == FFONT_RUN_NONE ) {
	  runtype = FFONT_RUN_OFF;
	  *line_data_cp = 16;
	} else if( runtype == FFONT_RUN_OFF ) {
	  (*line_data_cp)++;
	} else {
	  line_data_cp++;
	  runtype = FFONT_RUN_OFF;
	  *line_data_cp = 16;
	}
      }
    }

    /* Finish off the run with the terminator */
    line_data_cp++;
    *line_data_cp = FFONT_RUN_TERM_CHAR;
  }
}

/*
 * fixed_font_destroy:  Destroy a fixed font structure
 *
 * Arguments:           ffont = font to destroy.
 */

void fixed_font_destroy( FixedFont *ffont ){
  free( ffont );
}

/*
 * fixed_font_create:  Create a packed fixed font structure given
 *                     an array containing the font info.
 *
 * Arguments:          fontset = Fixed font info from the header.
 *                     width   = Width of each char in fontset
 *                     height  = Height of each char in fontset
 *
 * Return values:      Returns NULL on failure, else a pointer to a new
 *                     FixedFont struct on success.
 */

FixedFont *fixed_font_create( const unsigned char fontset[], 
			      int width, int height )
{
  FixedFont *res;
  int i;

  if( width < 0 || width > FFONT_MAX_WIDTH ) return NULL;
  if( height < 0 || height > FFONT_MAX_HEIGHT) return NULL;

  if( (res = malloc( sizeof( *res ))) == NULL )
    return NULL;

  res->width = width;
  res->height = height;

  for( i=0; i< 256; i++ ){
    fixed_font_char_pack( &fontset[ i * height ], width, height, 
			  &res->chars[ i ]);
  }
  return res;
}

/*
 * fixed_font_print_char:  Print a character from the fixed font on stdout.
 *                         Primarily used for debugging.
 *
 * Arguments:              ffont = Fixed font to use
 *                         charval = Character value between 0 and 255 inclusiv
 */
 
void fixed_font_print_char( const FixedFont *ffont, int charval ){
  int y, i;
  const unsigned char *cp;

  for( y=0; y< ffont->height; y++ ){
    cp = ffont->chars[ charval ].line_data[ y ];
    while( *cp != FFONT_RUN_TERM_CHAR ) {
      if( *cp < 16 ) {
	for( i=0; i<= *cp; i++ )
	  printf(" " );
      } else if( *cp < 32 ) {
	for( i=16; i<= *cp; i++ )
	  printf( "O" );
      } else {
	printf("!\n");
      }
      cp++;
    }
    printf("\n");
  }
}