Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of gr-peach-opencv-project-sd-card by
AsciiFont.cpp
00001 #include "mbed.h" 00002 #include "AsciiFont.h" 00003 #include "ascii.h" 00004 00005 AsciiFont::AsciiFont(uint8_t * p_buf, int width, int height, int stride, int byte_per_pixel, uint32_t const colour) : 00006 p_text_field(p_buf), max_width(width), max_height(height), buf_stride(stride), pixel_num(byte_per_pixel), background_colour(colour) { 00007 00008 } 00009 00010 void AsciiFont::Erase() { 00011 Erase(background_colour, 0, 0, max_width, max_height); 00012 } 00013 00014 void AsciiFont::Erase(uint32_t const colour) { 00015 Erase(colour, 0, 0, max_width, max_height); 00016 } 00017 00018 void AsciiFont::Erase(uint32_t const colour, int x, int y, int width, int height) { 00019 int idx_base; 00020 int wk_idx, i, j ,k; 00021 00022 background_colour = colour; 00023 idx_base = (x * pixel_num) + (buf_stride * y); 00024 for (i = 0; i < height; i++) { 00025 wk_idx = idx_base + (buf_stride * i); 00026 for (j = 0; j < width; j++) { 00027 for (k = (pixel_num - 1); k >= 0; k--) { 00028 p_text_field[wk_idx++] = (uint8_t)(background_colour >> (8 * k)); 00029 } 00030 } 00031 } 00032 } 00033 00034 int AsciiFont::DrawStr(char * str, int x, int y, uint32_t const colour, int font_size, uint16_t const max_char_num) { 00035 int char_num = 0; 00036 00037 if ((str == NULL) || (font_size <= 0)) { 00038 return 0; 00039 } 00040 while ((*str != '\0') && (char_num < max_char_num)) { 00041 if (DrawChar(*str, x, y, colour, font_size) == false) { 00042 break; 00043 } 00044 str++; 00045 x += CHAR_PIX_WIDTH * font_size; 00046 char_num++; 00047 } 00048 return char_num; 00049 } 00050 00051 bool AsciiFont::DrawChar(char c, int x, int y, uint32_t const colour, int font_size) { 00052 int idx_base; 00053 int idx_y = 0; 00054 int wk_idx, i, j ,k, fw, fh; 00055 char * p_pattern; 00056 uint8_t mask = 0x80; 00057 uint32_t wk_colour; 00058 00059 if (font_size <= 0) { 00060 return false; 00061 } 00062 if ((x + (CHAR_PIX_WIDTH * font_size)) > max_width) { 00063 return false; 00064 } 00065 if ((y + (CHAR_PIX_HEIGHT * font_size)) > max_height) { 00066 return false; 00067 } 00068 00069 if ((c >= 0x20) && (c <= 0x7e)) { 00070 p_pattern = (char *)&g_ascii_table[c - 0x20][0]; 00071 } else { 00072 p_pattern = (char *)&g_ascii_table[10][0]; /* '*' */ 00073 } 00074 idx_base = (x * pixel_num) + (buf_stride * y); 00075 00076 /* Drawing */ 00077 for (i = 0; i < CHAR_PIX_HEIGHT; i++) { 00078 for (fh = 0; fh < font_size; fh++) { 00079 wk_idx = idx_base + (buf_stride * idx_y); 00080 for (j = 0; j < CHAR_PIX_WIDTH; j++) { 00081 if (p_pattern[j] & mask) { 00082 wk_colour = colour; 00083 } else { 00084 wk_colour = background_colour; 00085 } 00086 for (fw = 0; fw < font_size; fw++) { 00087 for (k = (pixel_num - 1); k >= 0; k--) { 00088 p_text_field[wk_idx++] = (uint8_t)(wk_colour >> (8 * k)); 00089 } 00090 } 00091 } 00092 idx_y++; 00093 } 00094 mask = (uint8_t)(mask >> 1); 00095 } 00096 return true; 00097 }
Generated on Tue Jul 12 2022 14:45:59 by
1.7.2
