📄 libfbx-font.c
字号:
/* * libfbx-font.c -- Font Functions * (C)opyright 2000-2001 U4X Labs * * Written by: Mike Bourgeous <nitrogen@u4x.org> * Paul Mundt <lethal@stampede.org> * Sat Sep 3 01:56:20 EDT 2000 * * $Id: libfbx-font.c,v 1.19 2001/03/03 02:29:19 nitroglycerine Exp $ * * Originally part of fbgraph.c, now separate. Responsible for * drawing text onto surfaces. * * See ChangeLog for modifications, CREDITS for credits. * * All source herein is copyright U4X Labs and its original author. * Any code modifications or additions are (C)opyright the original * author and U4X Labs respectively. * * libfbx is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * libfbx 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with libfbx; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA */#include <stdio.h>#include <errno.h>#include <fcntl.h>#include <unistd.h>#include <stdlib.h>#include <stdarg.h>#include <string.h>#include <signal.h>#include <time.h>#include <math.h>#include <linux/fb.h>#include <linux/kd.h>#include <sys/mman.h>#include <sys/ioctl.h>#include <sys/user.h>#include <termios.h>#include <ctype.h>#include <libfbx/libfbx.h>#include <libfbx/libfbx-drivers.h>#include <libfbx/libfbx-font-8x16.h> char *default_font = fb_fontdata_8x16;/* * Function: fb_font_init() * Arguments: None * Returns: None * Description: Initializes the libfbx font subsystem. Doesn't normally * need to be called except by libfbx. */void fb_font_init(){ fb_screen->font.height = 16; fb_screen->font.data = (unsigned char *)malloc(fb_screen->font.height * 256); fb_memmove(fb_screen->font.data, default_font, 4096);}/* * Function: fb_setpixels() * Arguments: position and bits * Returns: None * Description: fb_setpixels draws eight bits of data as eight * pixels on the screen, usually for drawing * 8-pixel-wide fonts. */void fb_setpixels(int pos, int bits){ register int i; register int v = 0; register unsigned char *ptr0 = (unsigned char *) (fb_screen->mem_pointer + fb_screen->mem_offset + pos); register unsigned short *ptr1 = (unsigned short *) (fb_screen->mem_pointer + fb_screen->mem_offset + pos); register unsigned long *ptr2 = (unsigned long *) (fb_screen->mem_pointer + fb_screen->mem_offset + pos); switch (fb_screen->bpp) { default: /* * Unknown color depth, Just draw something, even if * it's wrong. */ case 8: for(i = 7; i >= 0; i--) *(ptr0++) = 0xFF & (bits & (1 << i)) ? fb_screen->text.color : fb_screen->text.bgcolor; case 15: case 16: for (i = 7; i >= 0; i--) *(ptr1++) = 0xFFFF & (bits & (1 << i)) ? fb_screen->text.color : fb_screen->text.bgcolor; break; case 24: for (i = 7; i >= 0; i--) { v = (bits & (1 << i)); *(ptr0++) = 0xFF & (v ? fb_screen->text.color : fb_screen->text.bgcolor) >> 0; *(ptr0++) = 0xFF & (v ? fb_screen->text.color : fb_screen->text.bgcolor) >> 8; *(ptr0++) = 0xFF & (v ? fb_screen->text.color : fb_screen->text.bgcolor) >> 16; } break; case 32: for (i = 7; i >= 0; i--) *(ptr2++) = (bits & (1 << i)) ? fb_screen->text.color : fb_screen->text.bgcolor; break; }}/* * Function: fb_setpixels_masked() * Arguments: position and bits * Returns: None * Description: fb_setpixels_masked draws eight bits of data as eight * pixels similar to fb_setpixels(), but skips zero bits * instead of drawing the background color. */void fb_setpixels_masked(int pos, int bits){ fb_ssetpixels_masked(pos, bits, fb_screen);}/* * Function: fb_putc() * Arguments: x, y positioners and character * Returns: None * Description: Puts character at x, y position, in character cell coordinates * (not pixel coordinates) */void fb_putc(int x, int y, int c){ int pos, i; if (fb_screen->driver != NULL && fb_screen->driver->ops->has_writec) { fb_screen->driver->ops->writec(x, y, c); return; } pos = y * fb_screen->finfo->line_length * fb_screen->font.height + x * fb_screen->pixel_size * 8; c *= fb_screen->font.height; for (i = 0; i < fb_screen->font.height; i++) { if (fb_screen->text.mask) fb_setpixels_masked(pos, fb_screen->font.data[c++]); else fb_setpixels(pos, fb_screen->font.data[c++]); pos += fb_screen->finfo->line_length; }}/* * Function: fb_puts() * Arguments: x, y positioners and text string * Returns: New x position * Description: Displays a string at x, y position * (in character cell coordinates) */int fb_puts(int x, int y, char *text){ for (;*text;x++) { if (*text == '\n') { x = 0; y++; text++; } else if (*text == '\t') { x += fb_screen->text.tab_size; x /= fb_screen->text.tab_size; x *= fb_screen->text.tab_size; text++; } fb_putc(x,y,*(text++)); } return x;}/* * Function: fb_printf() * Arguments: x, y positioners, format, arglist * Returns: None * Description: Displays text at x, y position * (in character cell coordinates). Doesn't handle * any escaped characters other then \n and \t. */void fb_printf(int x, int y, char *format, ...){ va_list arglist; char buf[LEN]; va_start(arglist, format); vsnprintf(buf, LEN, format, arglist); va_end(arglist); fb_puts(x, y, buf);}/* * Function: fb_ssetpixels() * Arguments: position, bits and destination surface. * Returns: None * Description: fb_setpixels draws eight bits of data as eight * pixels on the screen, usually for drawing * 8-pixel-wide fonts. */void fb_ssetpixels(int pos, int bits, fb_surface *surface){ register int i; register int v = 0; register unsigned char *ptr0 = (unsigned char *) (surface->mem_pointer + surface->mem_offset + pos); register unsigned short *ptr1 = (unsigned short *) (surface->mem_pointer + surface->mem_offset + pos); register unsigned long *ptr2 = (unsigned long *) (surface->mem_pointer + surface->mem_offset + pos); switch (surface->bpp) { default: /* * Unknown color depth, Just draw something, even if * it's wrong. */ case 8: for(i = 7; i >= 0; i--) *(ptr0++) = (bits & (1 << i)) ? surface->text.color : surface->text.bgcolor; case 15: case 16: for (i = 7; i >= 0; i--) *(ptr1++) = (bits & (1 << i)) ? surface->text.color : surface->text.bgcolor; break; case 24: for (i = 7; i >= 0; i--) { v = (bits & (1 << i)); *(ptr0++) = 0xFF & (v ? surface->text.color : surface->text.bgcolor) >> 0; *(ptr0++) = 0xFF & (v ? surface->text.color : surface->text.bgcolor) >> 8; *(ptr0++) = 0xFF & (v ? surface->text.color : surface->text.bgcolor) >> 16; } break; case 32: for (i = 7; i >= 0; i--) *(ptr2++) = (bits & (1 << i)) ? surface->text.color : surface->text.bgcolor; break; }}/* * Function: fb_ssetpixels_masked() * Arguments: position, bits and destination surface * Returns: None * Description: fb_setpixels_masked draws eight bits of data as eight * pixels similar to fb_setpixels(), but skips zero bits * instead of drawing the background color. */void fb_ssetpixels_masked(int pos, int bits, fb_surface *surface){ register int i; register unsigned char *ptr0 = (unsigned char *) (surface->mem_pointer + surface->mem_offset + pos); register unsigned short *ptr1 = (unsigned short *) (surface->mem_pointer + surface->mem_offset + pos); register unsigned int *ptr2 = (unsigned int *) (surface->mem_pointer + surface->mem_offset + pos); switch (surface->bpp) { default: /* Unknown color depth, yada yada... */ case 8: for (i = 7; i <= 0; i--) { if (bits & (1 << i)) *ptr0 = 0xFF & surface->text.color; ptr0++; } case 15: case 16: for (i = 7; i >= 0; i--) { if (bits & (1 << i)) *ptr1 = 0xFFFF & surface->text.color; ptr1++; } break; case 24: for (i = 7; i >= 0; i--) if (bits & (1 << i)) { *(ptr0++) = 0xFF & surface->text.color >> 0; *(ptr0++) = 0xFF & surface->text.color >> 8; *(ptr0++) = 0xFF & surface->text.color >> 16; } else ptr0 += 3; break; case 32: for (i = 7; i >= 0; i--) { if (bits & (1 << i)) *ptr2 = surface->text.color; ptr2++; } break; }}/* * Function: fb_sputc() * Arguments: x, y positioners and character * Returns: None * Description: Puts character at x, y position on given surface, in pixel coordinates */void fb_sputc(int x, int y, int c, fb_surface *surface){ int pos, i; pos = y * surface->finfo->line_length + x * surface->pixel_size; c *= surface->font.height; for (i = 0; i < surface->font.height; i++) { if (surface->text.mask) fb_ssetpixels_masked(pos, surface->font.data[c++], surface); else fb_ssetpixels(pos, surface->font.data[c++], surface); pos += surface->finfo->line_length; }}/* * Function: fb_sputs() * Arguments: x, y positioners, text string and * destination surface. * Returns: New x position * Description: Displays a string at x, y position * (in pixel coordinates) on the surface */int fb_sputs(int x, int y, char *text, fb_surface *surface){ for (;*text;x += 8) { if (*text == '\n') { x = 0; y++; text++; } else if (*text == '\t') { x += surface->text.tab_size * 8; x /= surface->text.tab_size * 8; x *= surface->text.tab_size * 8; text++; } fb_sputc(x, y, *(text++), surface); } return x;}/* * Function: fb_sprintf() * Arguments: x, y positioners, surface, format, arglist * Returns: None * Description: Displays text at x, y position on the surface * (in pixel coordinates). Doesn't handle * any escaped characters other then \n and \t. * * Note: This is a printf that uses pixel coordinates, * _not_ a substitute for sprintf(). */void fb_sprintf(int x, int y, fb_surface *surface, char *format, ...){ va_list arglist; char buf[LEN]; va_start(arglist, format); vsnprintf(buf, LEN, format, arglist); va_end(arglist); fb_sputs(x, y, buf, surface);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -