Merge commit '5e2cca1843c61ee0ef1bb95c5dddc9b450b790c6'
[unleashed.git] / kernel / font / font.c
blobb386bf91bc903ca0b9f94fddd52745d168a21486
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 *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;
89 * The minus 2 is to make sure we have at least a 1 pixel
90 * border around the entire screen.
92 if (font_selected == NULL) {
93 if (((*rows * DEFAULT_FONT_DATA.height) > height) ||
94 ((*cols * DEFAULT_FONT_DATA.width) > width)) {
95 *rows = (height - 2) / DEFAULT_FONT_DATA.height;
96 *cols = (width - 2) / DEFAULT_FONT_DATA.width;
98 font_selected = &DEFAULT_FONT_DATA;
101 f->width = font_selected->width;
102 f->height = font_selected->height;
104 for (i = 0; i < ENCODED_CHARS; i++)
105 f->char_ptr[i] = font_selected->encoding[i];
107 f->image_data = font_selected->image;
112 * bit_to_pix4 is for 4-bit frame buffers. It will write one output byte
113 * for each 2 bits of input bitmap. It inverts the input bits before
114 * doing the output translation, for reverse video.
116 * Assuming foreground is 0001 and background is 0000...
117 * An input data byte of 0x53 will output the bit pattern
118 * 00000001 00000001 00000000 00010001.
121 void
122 font_bit_to_pix4(
123 struct font *f,
124 uint8_t *dest,
125 uint8_t c,
126 uint8_t fg_color,
127 uint8_t bg_color)
129 int row;
130 int byte;
131 int i;
132 uint8_t *cp;
133 uint8_t data;
134 uint8_t nibblett;
135 int bytes_wide;
137 cp = f->char_ptr[c];
138 bytes_wide = (f->width + 7) / 8;
140 for (row = 0; row < f->height; row++) {
141 for (byte = 0; byte < bytes_wide; byte++) {
142 data = *cp++;
143 for (i = 0; i < 4; i++) {
144 nibblett = (data >> ((3-i) * 2)) & 0x3;
145 switch (nibblett) {
146 case 0x0:
147 *dest++ = bg_color << 4 | bg_color;
148 break;
149 case 0x1:
150 *dest++ = bg_color << 4 | fg_color;
151 break;
152 case 0x2:
153 *dest++ = fg_color << 4 | bg_color;
154 break;
155 case 0x3:
156 *dest++ = fg_color << 4 | fg_color;
157 break;
165 * bit_to_pix8 is for 8-bit frame buffers. It will write one output byte
166 * for each bit of input bitmap. It inverts the input bits before
167 * doing the output translation, for reverse video.
169 * Assuming foreground is 00000001 and background is 00000000...
170 * An input data byte of 0x53 will output the bit pattern
171 * 0000000 000000001 00000000 00000001 00000000 00000000 00000001 00000001.
174 void
175 font_bit_to_pix8(
176 struct font *f,
177 uint8_t *dest,
178 uint8_t c,
179 uint8_t fg_color,
180 uint8_t bg_color)
182 int row;
183 int byte;
184 int i;
185 uint8_t *cp;
186 uint8_t data;
187 int bytes_wide;
188 uint8_t mask;
189 int bitsleft, nbits;
191 cp = f->char_ptr[c];
192 bytes_wide = (f->width + 7) / 8;
194 for (row = 0; row < f->height; row++) {
195 bitsleft = f->width;
196 for (byte = 0; byte < bytes_wide; byte++) {
197 data = *cp++;
198 mask = 0x80;
199 nbits = MIN(8, bitsleft);
200 bitsleft -= nbits;
201 for (i = 0; i < nbits; i++) {
202 *dest++ = (data & mask ? fg_color: bg_color);
203 mask = mask >> 1;
210 * bit_to_pix24 is for 24-bit frame buffers. It will write four output bytes
211 * for each bit of input bitmap. It inverts the input bits before
212 * doing the output translation, for reverse video. Note that each
213 * 24-bit RGB value is finally stored in a 32-bit unsigned int, with the
214 * high-order byte set to zero.
216 * Assuming foreground is 00000000 11111111 11111111 11111111
217 * and background is 00000000 00000000 00000000 00000000
218 * An input data byte of 0x53 will output the bit pattern
220 * 00000000 00000000 00000000 00000000
221 * 00000000 11111111 11111111 11111111
222 * 00000000 00000000 00000000 00000000
223 * 00000000 11111111 11111111 11111111
224 * 00000000 00000000 00000000 00000000
225 * 00000000 00000000 00000000 00000000
226 * 00000000 11111111 11111111 11111111
227 * 00000000 11111111 11111111 11111111
231 void
232 font_bit_to_pix24(
233 struct font *f,
234 uint32_t *dest,
235 uint8_t c,
236 uint32_t fg_color32,
237 uint32_t bg_color32)
239 int row;
240 int byte;
241 int i;
242 uint8_t *cp;
243 uint32_t data;
244 int bytes_wide;
245 int bitsleft, nbits;
247 cp = f->char_ptr[c];
248 bytes_wide = (f->width + 7) / 8;
250 for (row = 0; row < f->height; row++) {
251 bitsleft = f->width;
252 for (byte = 0; byte < bytes_wide; byte++) {
253 data = *cp++;
254 nbits = MIN(8, bitsleft);
255 bitsleft -= nbits;
256 for (i = 0; i < nbits; i++) {
257 *dest++ = ((data << i) & 0x80 ?
258 fg_color32 : bg_color32);