test public

Dependencies:   HttpServer_snapshot_mbed-os

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AsciiFont.cpp Source File

AsciiFont.cpp

00001 /* Copyright (c) 2016 dkato
00002  * SPDX-License-Identifier: Apache-2.0
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "mbed.h"
00017 #include "AsciiFont.h"
00018 #include "ascii.h"
00019 
00020 AsciiFont::AsciiFont(uint8_t * p_buf, int width, int height, int stride, int byte_per_pixel, uint32_t const colour) : 
00021     p_text_field(p_buf), max_width(width), max_height(height), buf_stride(stride), pixel_num(byte_per_pixel), background_colour(colour) {
00022     
00023 }
00024 
00025 void AsciiFont::Erase() {
00026     Erase(background_colour, 0, 0, max_width, max_height);
00027 }
00028 
00029 void AsciiFont::Erase(uint32_t const colour) {
00030     Erase(colour, 0, 0, max_width, max_height);
00031 }
00032 
00033 void AsciiFont::Erase(uint32_t const colour, int x, int y, int width, int height) {
00034     int idx_base;
00035     int wk_idx, i, j ,k;
00036 
00037     background_colour = colour;
00038     if ((x + width) > max_width) {
00039         width = max_width - x;
00040     }
00041     if ((y + height) > max_height) {
00042         height = max_height - y;
00043     }
00044     idx_base = (x * pixel_num) + (buf_stride * y);
00045     for (i = 0; i < height; i++) {
00046         wk_idx = idx_base + (buf_stride * i);
00047         for (j = 0; j < width; j++) {
00048             for (k = (pixel_num - 1); k >= 0; k--) {
00049                 p_text_field[wk_idx++] = (uint8_t)(background_colour >> (8 * k));
00050             }
00051         }
00052     }
00053 }
00054 
00055 int AsciiFont::DrawStr(const char * str, int x, int y, uint32_t const colour, int font_size, uint16_t const max_char_num) {
00056     int char_num = 0;
00057 
00058     if ((str == NULL) || (font_size <= 0)) {
00059         return 0;
00060     }
00061     while ((*str != '\0') && (char_num < max_char_num)) {
00062         if (DrawChar(*str, x, y, colour, font_size) == false) {
00063             break;
00064         }
00065         str++;
00066         x += CHAR_PIX_WIDTH * font_size;
00067         char_num++;
00068     }
00069     return char_num;
00070 }
00071 
00072 bool AsciiFont::DrawChar(char c, int x, int y, uint32_t const colour, int font_size) {
00073     int idx_base;
00074     int idx_y = 0;
00075     int wk_idx, i, j ,k, fw, fh;
00076     char * p_pattern;
00077     uint8_t mask = 0x80;
00078     uint32_t wk_colour;
00079 
00080     if (font_size <= 0) {
00081         return false;
00082     }
00083     if ((x + (CHAR_PIX_WIDTH * font_size)) > max_width) {
00084         return false;
00085     }
00086     if ((y + (CHAR_PIX_HEIGHT * font_size)) > max_height) {
00087         return false;
00088     }
00089 
00090     if ((c >= 0x20) && (c <= 0x7e)) {
00091         p_pattern = (char *)&g_ascii_table[c - 0x20][0];
00092     } else {
00093         p_pattern = (char *)&g_ascii_table[10][0]; /* '*' */
00094     }
00095     idx_base = (x * pixel_num) + (buf_stride * y);
00096 
00097     /* Drawing */
00098     for (i = 0; i < CHAR_PIX_HEIGHT; i++) {
00099         for (fh = 0; fh < font_size; fh++) {
00100             wk_idx = idx_base + (buf_stride * idx_y);
00101             for (j = 0; j < CHAR_PIX_WIDTH; j++) {
00102                 if (p_pattern[j] & mask) {
00103                     wk_colour = colour;
00104                 } else {
00105                     wk_colour = background_colour;
00106                 }
00107                 for (fw = 0; fw < font_size; fw++) {
00108                     for (k = (pixel_num - 1); k >= 0; k--) {
00109                         p_text_field[wk_idx++] = (uint8_t)(wk_colour >> (8 * k));
00110                     }
00111                 }
00112             }
00113             idx_y++;
00114         }
00115         mask = (uint8_t)(mask >> 1);
00116     }
00117     return true;
00118 }
00119