Unleashed v1.4
[unleashed.git] / kernel / font / font.c
blobbc5be7eb2186ac766f7a36093f86a7f8fcb95e59
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Copyright 2019 Toomas Soome <tsoome@me.com>
32 * Generic font related data and functions shared by early boot console
33 * in dboot, kernel startup and full kernel.
35 #include <sys/types.h>
36 #include <sys/systm.h>
37 #include <sys/font.h>
38 #include <sys/sysmacros.h>
41 * Fonts are statically linked with this module. At some point an
42 * RFE might be desireable to allow dynamic font loading. The
43 * original intention to facilitate dynamic fonts can be seen
44 * by examining the data structures and set_font(). As much of
45 * the original code is retained but modified to be suited for
46 * traversing a list of static fonts.
50 * Must be sorted by font size in descending order
52 struct fontlist fonts[] = {
53 { &font_data_12x22, NULL },
54 { &font_data_8x16, NULL },
55 { &font_data_7x14, NULL },
56 { &font_data_6x10, NULL },
57 { NULL, NULL }
60 void
61 set_font(struct font *f, short *rows, short *cols, short height, short width)
63 bitmap_data_t *default_font = NULL, *font_selected = NULL;
64 struct fontlist *fl;
65 int i;
68 * Find best font for these dimensions, or use default
70 * A 1 pixel border is the absolute minimum we could have
71 * as a border around the text window (BORDER_PIXELS = 2),
72 * however a slightly larger border not only looks better
73 * but for the fonts currently statically built into the
74 * emulator causes much better font selection for the
75 * normal range of screen resolutions.
77 for (fl = fonts; fl->data; fl++) {
78 if ((((*rows * fl->data->height) + BORDER_PIXELS) <= height) &&
79 (((*cols * fl->data->width) + BORDER_PIXELS) <= width)) {
80 font_selected = fl->data;
81 *rows = (height - BORDER_PIXELS) /
82 font_selected->height;
83 *cols = (width - BORDER_PIXELS) /
84 font_selected->width;
85 break;
87 default_font = fl->data;
90 * The minus 2 is to make sure we have at least a 1 pixel
91 * border around the entire screen.
93 if (font_selected == NULL) {
94 if (default_font == NULL)
95 default_font = &DEFAULT_FONT_DATA;
97 if (((*rows * default_font->height) > height) ||
98 ((*cols * default_font->width) > width)) {
99 *rows = (height - 2) / default_font->height;
100 *cols = (width - 2) / default_font->width;
102 font_selected = default_font;
105 f->width = font_selected->width;
106 f->height = font_selected->height;
108 for (i = 0; i < ENCODED_CHARS; i++)
109 f->char_ptr[i] = font_selected->encoding[i];
111 f->image_data = font_selected->image;
116 * bit_to_pix4 is for 4-bit frame buffers. It will write one output byte
117 * for each 2 bits of input bitmap. It inverts the input bits before
118 * doing the output translation, for reverse video.
120 * Assuming foreground is 0001 and background is 0000...
121 * An input data byte of 0x53 will output the bit pattern
122 * 00000001 00000001 00000000 00010001.
125 void
126 font_bit_to_pix4(
127 struct font *f,
128 uint8_t *dest,
129 uint32_t c,
130 uint8_t fg_color,
131 uint8_t bg_color)
133 int row;
134 int byte;
135 int i;
136 uint8_t *cp;
137 uint8_t data;
138 uint8_t nibblett;
139 int bytes_wide;
141 if (c >= ENCODED_CHARS)
142 c = '?';
144 cp = f->char_ptr[c];
145 bytes_wide = (f->width + 7) / 8;
147 for (row = 0; row < f->height; row++) {
148 for (byte = 0; byte < bytes_wide; byte++) {
149 data = *cp++;
150 for (i = 0; i < 4; i++) {
151 nibblett = (data >> ((3-i) * 2)) & 0x3;
152 switch (nibblett) {
153 case 0x0:
154 *dest++ = bg_color << 4 | bg_color;
155 break;
156 case 0x1:
157 *dest++ = bg_color << 4 | fg_color;
158 break;
159 case 0x2:
160 *dest++ = fg_color << 4 | bg_color;
161 break;
162 case 0x3:
163 *dest++ = fg_color << 4 | fg_color;
164 break;
172 * bit_to_pix8 is for 8-bit frame buffers. It will write one output byte
173 * for each bit of input bitmap. It inverts the input bits before
174 * doing the output translation, for reverse video.
176 * Assuming foreground is 00000001 and background is 00000000...
177 * An input data byte of 0x53 will output the bit pattern
178 * 0000000 000000001 00000000 00000001 00000000 00000000 00000001 00000001.
181 void
182 font_bit_to_pix8(
183 struct font *f,
184 uint8_t *dest,
185 uint32_t c,
186 uint8_t fg_color,
187 uint8_t bg_color)
189 int row;
190 int byte;
191 int i;
192 uint8_t *cp;
193 uint8_t data;
194 int bytes_wide;
195 uint8_t mask;
196 int bitsleft, nbits;
198 if (c >= ENCODED_CHARS)
199 c = '?';
201 cp = f->char_ptr[c];
202 bytes_wide = (f->width + 7) / 8;
204 for (row = 0; row < f->height; row++) {
205 bitsleft = f->width;
206 for (byte = 0; byte < bytes_wide; byte++) {
207 data = *cp++;
208 mask = 0x80;
209 nbits = MIN(8, bitsleft);
210 bitsleft -= nbits;
211 for (i = 0; i < nbits; i++) {
212 *dest++ = (data & mask ? fg_color: bg_color);
213 mask = mask >> 1;
220 * bit_to_pix16 is for 16-bit frame buffers. It will write two output bytes
221 * for each bit of input bitmap. It inverts the input bits before
222 * doing the output translation, for reverse video.
224 * Assuming foreground is 11111111 11111111
225 * and background is 00000000 00000000
226 * An input data byte of 0x53 will output the bit pattern
228 * 00000000 00000000
229 * 11111111 11111111
230 * 00000000 00000000
231 * 11111111 11111111
232 * 00000000 00000000
233 * 00000000 00000000
234 * 11111111 11111111
235 * 11111111 11111111
239 void
240 font_bit_to_pix16(
241 struct font *f,
242 uint16_t *dest,
243 uint32_t c,
244 uint16_t fg_color16,
245 uint16_t bg_color16)
247 int row;
248 int byte;
249 int i;
250 uint8_t *cp;
251 uint16_t data, d;
252 int bytes_wide;
253 int bitsleft, nbits;
255 if (c >= ENCODED_CHARS)
256 c = '?';
258 cp = f->char_ptr[c];
259 bytes_wide = (f->width + 7) / 8;
261 for (row = 0; row < f->height; row++) {
262 bitsleft = f->width;
263 for (byte = 0; byte < bytes_wide; byte++) {
264 data = *cp++;
265 nbits = MIN(8, bitsleft);
266 bitsleft -= nbits;
267 for (i = 0; i < nbits; i++) {
268 d = ((data << i) & 0x80 ?
269 fg_color16 : bg_color16);
270 *dest++ = d;
277 * bit_to_pix24 is for 24-bit frame buffers. It will write three output bytes
278 * for each bit of input bitmap. It inverts the input bits before
279 * doing the output translation, for reverse video.
281 * Assuming foreground is 11111111 11111111 11111111
282 * and background is 00000000 00000000 00000000
283 * An input data byte of 0x53 will output the bit pattern
285 * 00000000 00000000 00000000
286 * 11111111 11111111 11111111
287 * 00000000 00000000 00000000
288 * 11111111 11111111 11111111
289 * 00000000 00000000 00000000
290 * 00000000 00000000 00000000
291 * 11111111 11111111 11111111
292 * 11111111 11111111 11111111
296 void
297 font_bit_to_pix24(
298 struct font *f,
299 uint8_t *dest,
300 uint32_t c,
301 uint32_t fg_color32,
302 uint32_t bg_color32)
304 int row;
305 int byte;
306 int i;
307 uint8_t *cp;
308 uint32_t data, d;
309 int bytes_wide;
310 int bitsleft, nbits;
312 if (c >= ENCODED_CHARS)
313 c = '?';
315 cp = f->char_ptr[c];
316 bytes_wide = (f->width + 7) / 8;
318 for (row = 0; row < f->height; row++) {
319 bitsleft = f->width;
320 for (byte = 0; byte < bytes_wide; byte++) {
321 data = *cp++;
322 nbits = MIN(8, bitsleft);
323 bitsleft -= nbits;
324 for (i = 0; i < nbits; i++) {
325 d = ((data << i) & 0x80 ?
326 fg_color32 : bg_color32);
327 *dest++ = d & 0xff;
328 *dest++ = (d >> 8) & 0xff;
329 *dest++ = (d >> 16) & 0xff;
336 * bit_to_pix32 is for 32-bit frame buffers. It will write four output bytes
337 * for each bit of input bitmap. It inverts the input bits before
338 * doing the output translation, for reverse video. Note that each
339 * 24-bit RGB value is finally stored in a 32-bit unsigned int, with the
340 * high-order byte set to zero.
342 * Assuming foreground is 00000000 11111111 11111111 11111111
343 * and background is 00000000 00000000 00000000 00000000
344 * An input data byte of 0x53 will output the bit pattern
346 * 00000000 00000000 00000000 00000000
347 * 00000000 11111111 11111111 11111111
348 * 00000000 00000000 00000000 00000000
349 * 00000000 11111111 11111111 11111111
350 * 00000000 00000000 00000000 00000000
351 * 00000000 00000000 00000000 00000000
352 * 00000000 11111111 11111111 11111111
353 * 00000000 11111111 11111111 11111111
357 void
358 font_bit_to_pix32(
359 struct font *f,
360 uint32_t *dest,
361 uint32_t c,
362 uint32_t fg_color32,
363 uint32_t bg_color32)
365 int row;
366 int byte;
367 int i;
368 uint8_t *cp;
369 uint32_t data;
370 int bytes_wide;
371 int bitsleft, nbits;
373 if (c >= ENCODED_CHARS)
374 c = '?';
376 cp = f->char_ptr[c];
377 bytes_wide = (f->width + 7) / 8;
379 for (row = 0; row < f->height; row++) {
380 bitsleft = f->width;
381 for (byte = 0; byte < bytes_wide; byte++) {
382 data = *cp++;
383 nbits = MIN(8, bitsleft);
384 bitsleft -= nbits;
385 for (i = 0; i < nbits; i++) {
386 *dest++ = ((data << i) & 0x80 ?
387 fg_color32 : bg_color32);