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.
Font.cpp
00001 /* NeatGUI Library 00002 * Copyright (c) 2013 Neil Thiessen 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 00017 #include "Font.h" 00018 00019 Font::Font(const char* table) 00020 { 00021 m_FontTable = table; 00022 m_Color = 0xFFFFFFFF; 00023 } 00024 00025 Font::Font(const char* table, unsigned int color) 00026 { 00027 m_FontTable = table; 00028 m_Color = color; 00029 } 00030 00031 BitmapImage Font::glyph(char c) 00032 { 00033 while(1) { 00034 //Skip the height and numChars bytes 00035 const char *ptr = m_FontTable + 2; 00036 00037 //Search for the character in the font table 00038 for(int i = 0; i < m_FontTable[0]; i++) { 00039 if (*ptr == c) { 00040 //Return the image 00041 return BitmapImage(m_FontTable + ptr[1] * 0xFF + ptr[2], m_Color); 00042 } 00043 ptr += 3; 00044 } 00045 00046 //The character wasn't found, replace it with a space 00047 c = ' '; 00048 } 00049 } 00050 00051 unsigned int Font::color() 00052 { 00053 return m_Color; 00054 } 00055 00056 void Font::color(unsigned int c) 00057 { 00058 m_Color = c; 00059 } 00060 00061 int Font::height() 00062 { 00063 return m_FontTable[1]; 00064 } 00065 00066 int Font::measureString(const char* str) 00067 { 00068 int i = 0; 00069 int slen = 0; 00070 00071 //Find the length in pixels of the whole string 00072 while (str[i] != 0) { 00073 slen += glyph(*str).width(); 00074 i++; 00075 } 00076 00077 return slen; 00078 } 00079 00080 int Font::measureWord(const char* str) 00081 { 00082 int i = 0; 00083 int wlen = 0; 00084 00085 //Find the length in pixels of the next word (separated by whitespace) 00086 while ((str[i] > ' ') && (str[i] <= 0x7E)) { 00087 wlen += glyph(*str).width(); 00088 i++; 00089 } 00090 00091 return wlen; 00092 }
Generated on Tue Jul 12 2022 21:38:03 by
1.7.2