GNU Unifont: src/unifont-support.c File Reference

GNU Unifont 15.1.05 Pan-Unicode font with complete Unicode Plane 0 coverage and partial coverage of higher planes

Functions
unifont-support.c File Reference

: Support functions for Unifont .hex files. More...

`#include <stdio.h>`
`#include <stdlib.h>`
`#include <string.h>`
Include dependency graph for unifont-support.c:

Go to the source code of this file.

## Functions
void | parse_hex (char *hexstring, int *width, unsigned *codept, unsigned char glyph[16][2])
| Decode a Unifont .hex file into Uniocde code point and glyph.
void | glyph2bits (int width, unsigned char glyph[16][2], unsigned char glyphbits[16][16])
| Convert a Unifont binary glyph into a binary glyph array of bits.
void | hexpose (int width, unsigned char glyphbits[16][16], unsigned char transpose[2][16])
| Transpose a Unifont .hex format glyph into 2 column-major sub-arrays.
void | glyph2string (int width, unsigned codept, unsigned char glyph[16][2], char *outstring)
| Convert a glyph code point and byte array into a Unifont .hex string.
void | xglyph2string (int width, unsigned codept, unsigned char transpose[2][16], char *outstring)
| Convert a code point and transposed glyph into a Unifont .hex string.

## Detailed Description

: Support functions for Unifont .hex files.

Author
  Paul Hardy

Copyright
  Copyright © 2023 Paul Hardy

Definition in file unifont-support.c.

## Function Documentation

## ◆ glyph2bits()

void glyph2bits | ( | int | width,
|  | unsigned char | glyph[16][2],
|  | unsigned char | glyphbits[16][16]
| ) |  |

Convert a Unifont binary glyph into a binary glyph array of bits.

This function takes a Unifont 16-row by 1- or 2-byte wide binary glyph and returns an array of 16 rows by 16 columns. For each output array element, a 1 indicates the corresponding bit was set in the binary glyph, and a 0 indicates the corresponding bit was not set.

Parameters

  [in] | width | The number of columns in the glyph.
  [in] | glyph | The binary glyph, as a 16-row by 2-byte array.
  [out] | glyphbits | The converted glyph, as a 16-row, 16-column array.

Definition at line 91 of file unifont-support.c.

93 {
94
95 unsigned char tmp_byte;
96 unsigned char mask;
97 int row, column;
98
99 for (row = 0; row < 16; row++) {
100 tmp_byte = glyph [row][0];
101 mask = 0x80;
102 for (column = 0; column < 8; column++) {
103 glyphbits [row][column] = tmp_byte & mask ? 1 : 0;
104 mask >>= 1;
105 }
106
107 if (width > 8)
108 tmp_byte = glyph [row][1];
109 else
110 tmp_byte = 0x00;
111
112 mask = 0x80;
113 for (column = 8; column < 16; column++) {
114 glyphbits [row][column] = tmp_byte & mask ? 1 : 0;
115 mask >>= 1;
116 }
117 }
118
119
120 return;
121}

## ◆ glyph2string()

void glyph2string | ( | int | width,
|  | unsigned | codept,
|  | unsigned char | glyph[16][2],
|  | char * | outstring
| ) |  |

Convert a glyph code point and byte array into a Unifont .hex string.

This function takes a code point and a 16-row by 1- or 2-byte binary glyph, and converts it into a Unifont .hex format character array.

Parameters

  [in] | width | The number of columns in the glyph.
  [in] | codept | The code point to appear in the output .hex string.
  [in] | glyph | The glyph, with each of 16 rows 1 or 2 bytes wide.
  [out] | outstring | The output string, in Unifont .hex format.

Definition at line 221 of file unifont-support.c.

223 {
224
225 int i; /* index into outstring array */
226 int row;
227
228 if (codept <= 0xFFFF) {
229 sprintf (outstring, "%04X:", codept);
230 i = 5;
231 }
232 else {
233 sprintf (outstring, "%06X:", codept);
234 i = 7;
235 }
236
237 for (row = 0; row < 16; row++) {
238 sprintf (&outstring[i], "%02X", glyph [row][0]);
239 i += 2;
240
241 if (width > 8) {
242 sprintf (&outstring[i], "%02X", glyph [row][1]);
243 i += 2;
244 }
245 }
246
247 outstring[i] = '\0'; /* terminate output string */
248
249
250 return;
251}

## ◆ hexpose()

void hexpose | ( | int | width,
|  | unsigned char | glyphbits[16][16],
|  | unsigned char | transpose[2][16]
| ) |  |

Transpose a Unifont .hex format glyph into 2 column-major sub-arrays.

This function takes a 16-by-16 cell bit array made from a Unifont glyph (as created by the glyph2bits function) and outputs a transposed array of 2 sets of 8 or 16 columns, depending on the glyph width. This format simplifies outputting these bit patterns on a graphics display with a controller chip designed to output a column of 8 pixels at a time.

For a line of text with Unifont output, first all glyphs can have their first 8 rows of pixels displayed on a line. Then the second 8 rows of all glyphs on the line can be displayed. This simplifies code for such controller chips that are designed to automatically increment input bytes of column data by one column at a time for each successive byte.

The glyphbits array contains a '1' in each cell where the corresponding non-transposed glyph has a pixel set, and 0 in each cell where a pixel is not set.

Parameters

  [in] | width | The number of columns in the glyph.
  [in] | glyphbits | The 16-by-16 pixel glyph bits.
  [out] | transpose | The array of 2 sets of 8 ot 16 columns of 8 pixels.

Definition at line 150 of file unifont-support.c.

152 {
153
154 int column;
155
156
157 for (column = 0; column < 8; column++) {
158 transpose [0][column] =
159 (glyphbits [ 0][column] << 7) |
160 (glyphbits [ 1][column] << 6) |
161 (glyphbits [ 2][column] << 5) |
162 (glyphbits [ 3][column] << 4) |
163 (glyphbits [ 4][column] << 3) |
164 (glyphbits [ 5][column] << 2) |
165 (glyphbits [ 6][column] << 1) |
166 (glyphbits [ 7][column] );
167 transpose [1][column] =
168 (glyphbits [ 8][column] << 7) |
169 (glyphbits [ 9][column] << 6) |
170 (glyphbits [10][column] << 5) |
171 (glyphbits [11][column] << 4) |
172 (glyphbits [12][column] << 3) |
173 (glyphbits [13][column] << 2) |
174 (glyphbits [14][column] << 1) |
175 (glyphbits [15][column] );
176 }
177 if (width > 8) {
178 for (column = 8; column < width; column++) {
179 transpose [0][column] =
180 (glyphbits [0][column] << 7) |
181 (glyphbits [1][column] << 6) |
182 (glyphbits [2][column] << 5) |
183 (glyphbits [3][column] << 4) |
184 (glyphbits [4][column] << 3) |
185 (glyphbits [5][column] << 2) |
186 (glyphbits [6][column] << 1) |
187 (glyphbits [7][column] );
188 transpose [1][column] =
189 (glyphbits [ 8][column] << 7) |
190 (glyphbits [ 9][column] << 6) |
191 (glyphbits [10][column] << 5) |
192 (glyphbits [11][column] << 4) |
193 (glyphbits [12][column] << 3) |
194 (glyphbits [13][column] << 2) |
195 (glyphbits [14][column] << 1) |
196 (glyphbits [15][column] );
197 }
198 }
199 else {
200 for (column = 8; column < width; column++)
201 transpose [0][column] = transpose [1][column] = 0x00;
202 }
203
204
205 return;
206}

## ◆ parse_hex()

void parse_hex | ( | char * | hexstring,
|  | int * | width,
|  | unsigned * | codept,
|  | unsigned char | glyph[16][2]
| ) |  |

Decode a Unifont .hex file into Uniocde code point and glyph.

This function takes one line from a Unifont .hex file and decodes it into a code point followed by a 16-row glyph array. The glyph array can be one byte (8 columns) or two bytes (16 columns).

Parameters

  [in] | hexstring | The Unicode .hex string for one code point.
  [out] | width | The number of columns in a glyph with 16 rows.
  [out] | codept | The code point, contained in the first .hex file field.
  [out] | glyph | The Unifont glyph, as 16 rows by 1 or 2 bytes wide.

Definition at line 44 of file unifont-support.c.

47 {
48
49 int i;
50 int row;
51 int length;
52
53 sscanf (hexstring, "%X", codept);
54 length = strlen (hexstring);
55 for (i = length - 1; i > 0 && hexstring[i] != '\n'; i--);
56 hexstring[i] = '\0';
57 for (i = 0; i < 9 && hexstring[i] != ':'; i++);
58 i++; /* Skip over ':' */
59 *width = (length - i) * 4 / 16; /* 16 rows per glyphbits */
60
61 for (row = 0; row < 16; row++) {
62 sscanf (&hexstring[i], "%2hhX", &glyph [row][0]);
63 i += 2;
64 if (*width > 8) {
65 sscanf (&hexstring[i], "%2hhX", &glyph [row][1]);
66 i += 2;
67 }
68 else {
69 glyph [row][1] = 0x00;
70 }
71 }
72
73
74 return;
75}

## ◆ xglyph2string()

void xglyph2string | ( | int | width,
|  | unsigned | codept,
|  | unsigned char | transpose[2][16],
|  | char * | outstring
| ) |  |

Convert a code point and transposed glyph into a Unifont .hex string.

This function takes a code point and a transposed Unifont glyph of 2 rows of 8 pixels in a column, and converts it into a Unifont .hex format character array.

Parameters

  [in] | width | The number of columns in the glyph.
  [in] | codept | The code point to appear in the output .hex string.
  [in] | transpose | The transposed glyph, with 2 sets of 8-row data.
  [out] | outstring | The output string, in Unifont .hex format.

Definition at line 267 of file unifont-support.c.

269 {
270
271 int i; /* index into outstring array */
272 int column;
273
274 if (codept <= 0xFFFF) {
275 sprintf (outstring, "%04X:", codept);
276 i = 5;
277 }
278 else {
279 sprintf (outstring, "%06X:", codept);
280 i = 7;
281 }
282
283 for (column = 0; column < 8; column++) {
284 sprintf (&outstring[i], "%02X", transpose [0][column]);
285 i += 2;
286 }
287 if (width > 8) {
288 for (column = 8; column < 16; column++) {
289 sprintf (&outstring[i], "%02X", transpose [0][column]);
290 i += 2;
291 }
292 }
293 for (column = 0; column < 8; column++) {
294 sprintf (&outstring[i], "%02X", transpose [1][column]);
295 i += 2;
296 }
297 if (width > 8) {
298 for (column = 8; column < 16; column++) {
299 sprintf (&outstring[i], "%02X", transpose [1][column]);
300 i += 2;
301 }
302 }
303
304 outstring[i] = '\0'; /* terminate output string */
305
306
307 return;
308}