Draw the character of the ASCII code.

Dependents:   mbed-os_Watson-IoT_ZXing_sample mbed-os_Watson-IoT_ZXing_sample MovPlayer GR-PEACH_HVC-P2_sample_client ... more

Fork of AsciiFont by Daiki Kato

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AsciiFont.cpp Source File

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     if ((x + width) > max_width) {
00024         width = max_width - x;
00025     }
00026     if ((y + height) > max_height) {
00027         height = max_height - y;
00028     }
00029     idx_base = (x * pixel_num) + (buf_stride * y);
00030     for (i = 0; i < height; i++) {
00031         wk_idx = idx_base + (buf_stride * i);
00032         for (j = 0; j < width; j++) {
00033             for (k = (pixel_num - 1); k >= 0; k--) {
00034                 p_text_field[wk_idx++] = (uint8_t)(background_colour >> (8 * k));
00035             }
00036         }
00037     }
00038 }
00039 
00040 int AsciiFont::DrawStr(const char * str, int x, int y, uint32_t const colour, int font_size, uint16_t const max_char_num) {
00041     int char_num = 0;
00042 
00043     if ((str == NULL) || (font_size <= 0)) {
00044         return 0;
00045     }
00046     while ((*str != '\0') && (char_num < max_char_num)) {
00047         if (DrawChar(*str, x, y, colour, font_size) == false) {
00048             break;
00049         }
00050         str++;
00051         x += CHAR_PIX_WIDTH * font_size;
00052         char_num++;
00053     }
00054     return char_num;
00055 }
00056 
00057 bool AsciiFont::DrawChar(char c, int x, int y, uint32_t const colour, int font_size) {
00058     int idx_base;
00059     int idx_y = 0;
00060     int wk_idx, i, j ,k, fw, fh;
00061     char * p_pattern;
00062     uint8_t mask = 0x80;
00063     uint32_t wk_colour;
00064 
00065     if (font_size <= 0) {
00066         return false;
00067     }
00068     if ((x + (CHAR_PIX_WIDTH * font_size)) > max_width) {
00069         return false;
00070     }
00071     if ((y + (CHAR_PIX_HEIGHT * font_size)) > max_height) {
00072         return false;
00073     }
00074 
00075     if ((c >= 0x20) && (c <= 0x7e)) {
00076         p_pattern = (char *)&g_ascii_table[c - 0x20][0];
00077     } else {
00078         p_pattern = (char *)&g_ascii_table[10][0]; /* '*' */
00079     }
00080     idx_base = (x * pixel_num) + (buf_stride * y);
00081 
00082     /* Drawing */
00083     for (i = 0; i < CHAR_PIX_HEIGHT; i++) {
00084         for (fh = 0; fh < font_size; fh++) {
00085             wk_idx = idx_base + (buf_stride * idx_y);
00086             for (j = 0; j < CHAR_PIX_WIDTH; j++) {
00087                 if (p_pattern[j] & mask) {
00088                     wk_colour = colour;
00089                 } else {
00090                     wk_colour = background_colour;
00091                 }
00092                 for (fw = 0; fw < font_size; fw++) {
00093                     for (k = (pixel_num - 1); k >= 0; k--) {
00094                         p_text_field[wk_idx++] = (uint8_t)(wk_colour >> (8 * k));
00095                     }
00096                 }
00097             }
00098             idx_y++;
00099         }
00100         mask = (uint8_t)(mask >> 1);
00101     }
00102     return true;
00103 }
00104