Merge illumos-gate
[unleashed.git] / kernel / font / font.c
blob3b2525ca3b8b657fdb314b83e0213b2558997774
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 2017 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 uint8_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 cp = f->char_ptr[c];
142 bytes_wide = (f->width + 7) / 8;
144 for (row = 0; row < f->height; row++) {
145 for (byte = 0; byte < bytes_wide; byte++) {
146 data = *cp++;
147 for (i = 0; i < 4; i++) {
148 nibblett = (data >> ((3-i) * 2)) & 0x3;
149 switch (nibblett) {
150 case 0x0:
151 *dest++ = bg_color << 4 | bg_color;
152 break;
153 case 0x1:
154 *dest++ = bg_color << 4 | fg_color;
155 break;
156 case 0x2:
157 *dest++ = fg_color << 4 | bg_color;
158 break;
159 case 0x3:
160 *dest++ = fg_color << 4 | fg_color;
161 break;
169 * bit_to_pix8 is for 8-bit frame buffers. It will write one output byte
170 * for each bit of input bitmap. It inverts the input bits before
171 * doing the output translation, for reverse video.
173 * Assuming foreground is 00000001 and background is 00000000...
174 * An input data byte of 0x53 will output the bit pattern
175 * 0000000 000000001 00000000 00000001 00000000 00000000 00000001 00000001.
178 void
179 font_bit_to_pix8(
180 struct font *f,
181 uint8_t *dest,
182 uint8_t c,
183 uint8_t fg_color,
184 uint8_t bg_color)
186 int row;
187 int byte;
188 int i;
189 uint8_t *cp;
190 uint8_t data;
191 int bytes_wide;
192 uint8_t mask;
193 int bitsleft, nbits;
195 cp = f->char_ptr[c];
196 bytes_wide = (f->width + 7) / 8;
198 for (row = 0; row < f->height; row++) {
199 bitsleft = f->width;
200 for (byte = 0; byte < bytes_wide; byte++) {
201 data = *cp++;
202 mask = 0x80;
203 nbits = MIN(8, bitsleft);
204 bitsleft -= nbits;
205 for (i = 0; i < nbits; i++) {
206 *dest++ = (data & mask ? fg_color: bg_color);
207 mask = mask >> 1;
214 * bit_to_pix16 is for 16-bit frame buffers. It will write two output bytes
215 * for each bit of input bitmap. It inverts the input bits before
216 * doing the output translation, for reverse video.
218 * Assuming foreground is 11111111 11111111
219 * and background is 00000000 00000000
220 * An input data byte of 0x53 will output the bit pattern
222 * 00000000 00000000
223 * 11111111 11111111
224 * 00000000 00000000
225 * 11111111 11111111
226 * 00000000 00000000
227 * 00000000 00000000
228 * 11111111 11111111
229 * 11111111 11111111
233 void
234 font_bit_to_pix16(
235 struct font *f,
236 uint16_t *dest,
237 uint8_t c,
238 uint16_t fg_color16,
239 uint16_t bg_color16)
241 int row;
242 int byte;
243 int i;
244 uint8_t *cp;
245 uint16_t data, d;
246 int bytes_wide;
247 int bitsleft, nbits;
249 cp = f->char_ptr[c];
250 bytes_wide = (f->width + 7) / 8;
252 for (row = 0; row < f->height; row++) {
253 bitsleft = f->width;
254 for (byte = 0; byte < bytes_wide; byte++) {
255 data = *cp++;
256 nbits = MIN(8, bitsleft);
257 bitsleft -= nbits;
258 for (i = 0; i < nbits; i++) {
259 d = ((data << i) & 0x80 ?
260 fg_color16 : bg_color16);
261 *dest++ = d;
268 * bit_to_pix24 is for 24-bit frame buffers. It will write three output bytes
269 * for each bit of input bitmap. It inverts the input bits before
270 * doing the output translation, for reverse video.
272 * Assuming foreground is 11111111 11111111 11111111
273 * and background is 00000000 00000000 00000000
274 * An input data byte of 0x53 will output the bit pattern
276 * 00000000 00000000 00000000
277 * 11111111 11111111 11111111
278 * 00000000 00000000 00000000
279 * 11111111 11111111 11111111
280 * 00000000 00000000 00000000
281 * 00000000 00000000 00000000
282 * 11111111 11111111 11111111
283 * 11111111 11111111 11111111
287 void
288 font_bit_to_pix24(
289 struct font *f,
290 uint8_t *dest,
291 uint8_t c,
292 uint32_t fg_color32,
293 uint32_t bg_color32)
295 int row;
296 int byte;
297 int i;
298 uint8_t *cp;
299 uint32_t data, d;
300 int bytes_wide;
301 int bitsleft, nbits;
303 cp = f->char_ptr[c];
304 bytes_wide = (f->width + 7) / 8;
306 for (row = 0; row < f->height; row++) {
307 bitsleft = f->width;
308 for (byte = 0; byte < bytes_wide; byte++) {
309 data = *cp++;
310 nbits = MIN(8, bitsleft);
311 bitsleft -= nbits;
312 for (i = 0; i < nbits; i++) {
313 d = ((data << i) & 0x80 ?
314 fg_color32 : bg_color32);
315 *dest++ = d & 0xff;
316 *dest++ = (d >> 8) & 0xff;
317 *dest++ = (d >> 16) & 0xff;
324 * bit_to_pix32 is for 32-bit frame buffers. It will write four output bytes
325 * for each bit of input bitmap. It inverts the input bits before
326 * doing the output translation, for reverse video. Note that each
327 * 24-bit RGB value is finally stored in a 32-bit unsigned int, with the
328 * high-order byte set to zero.
330 * Assuming foreground is 00000000 11111111 11111111 11111111
331 * and background is 00000000 00000000 00000000 00000000
332 * An input data byte of 0x53 will output the bit pattern
334 * 00000000 00000000 00000000 00000000
335 * 00000000 11111111 11111111 11111111
336 * 00000000 00000000 00000000 00000000
337 * 00000000 11111111 11111111 11111111
338 * 00000000 00000000 00000000 00000000
339 * 00000000 00000000 00000000 00000000
340 * 00000000 11111111 11111111 11111111
341 * 00000000 11111111 11111111 11111111
345 void
346 font_bit_to_pix32(
347 struct font *f,
348 uint32_t *dest,
349 uint8_t c,
350 uint32_t fg_color32,
351 uint32_t bg_color32)
353 int row;
354 int byte;
355 int i;
356 uint8_t *cp;
357 uint32_t data;
358 int bytes_wide;
359 int bitsleft, nbits;
361 cp = f->char_ptr[c];
362 bytes_wide = (f->width + 7) / 8;
364 for (row = 0; row < f->height; row++) {
365 bitsleft = f->width;
366 for (byte = 0; byte < bytes_wide; byte++) {
367 data = *cp++;
368 nbits = MIN(8, bitsleft);
369 bitsleft -= nbits;
370 for (i = 0; i < nbits; i++) {
371 *dest++ = ((data << i) & 0x80 ?
372 fg_color32 : bg_color32);