A simple yet powerful library for controlling graphical displays. Multiple display controllers are supported using inheritance.
Dependents: mbed_rifletool Hexi_Bubble_Game Hexi_Catch-the-dot_Game Hexi_Acceleromagnetic_Synth
NOTE: This library is in beta right now. As far as I know, everything here works, but there are many features that are lacking so far. Most notably containers, button handling, and display drivers other than the SSD1306.
Decoders/Font.cpp@0:b876cf091464, 2013-08-30 (annotated)
- Committer:
- neilt6
- Date:
- Fri Aug 30 17:09:18 2013 +0000
- Revision:
- 0:b876cf091464
- Child:
- 1:f7003ec66a51
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
neilt6 | 0:b876cf091464 | 1 | /* NeatGUI Library |
neilt6 | 0:b876cf091464 | 2 | * Copyright (c) 2013 Neil Thiessen |
neilt6 | 0:b876cf091464 | 3 | * |
neilt6 | 0:b876cf091464 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
neilt6 | 0:b876cf091464 | 5 | * you may not use this file except in compliance with the License. |
neilt6 | 0:b876cf091464 | 6 | * You may obtain a copy of the License at |
neilt6 | 0:b876cf091464 | 7 | * |
neilt6 | 0:b876cf091464 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
neilt6 | 0:b876cf091464 | 9 | * |
neilt6 | 0:b876cf091464 | 10 | * Unless required by applicable law or agreed to in writing, software |
neilt6 | 0:b876cf091464 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
neilt6 | 0:b876cf091464 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
neilt6 | 0:b876cf091464 | 13 | * See the License for the specific language governing permissions and |
neilt6 | 0:b876cf091464 | 14 | * limitations under the License. |
neilt6 | 0:b876cf091464 | 15 | */ |
neilt6 | 0:b876cf091464 | 16 | |
neilt6 | 0:b876cf091464 | 17 | #include "Font.h" |
neilt6 | 0:b876cf091464 | 18 | |
neilt6 | 0:b876cf091464 | 19 | Font::Font(const char *table) |
neilt6 | 0:b876cf091464 | 20 | { |
neilt6 | 0:b876cf091464 | 21 | m_FontTable = table; |
neilt6 | 0:b876cf091464 | 22 | m_Color = 0xFFFFFFFF; |
neilt6 | 0:b876cf091464 | 23 | } |
neilt6 | 0:b876cf091464 | 24 | |
neilt6 | 0:b876cf091464 | 25 | Font::Font(const char *table, unsigned int color) |
neilt6 | 0:b876cf091464 | 26 | { |
neilt6 | 0:b876cf091464 | 27 | m_FontTable = table; |
neilt6 | 0:b876cf091464 | 28 | m_Color = color; |
neilt6 | 0:b876cf091464 | 29 | } |
neilt6 | 0:b876cf091464 | 30 | |
neilt6 | 0:b876cf091464 | 31 | BitmapImage Font::glyph(char c) |
neilt6 | 0:b876cf091464 | 32 | { |
neilt6 | 0:b876cf091464 | 33 | while(1) { |
neilt6 | 0:b876cf091464 | 34 | //Skip the height and numChars bytes |
neilt6 | 0:b876cf091464 | 35 | const char *ptr = m_FontTable + 2; |
neilt6 | 0:b876cf091464 | 36 | |
neilt6 | 0:b876cf091464 | 37 | //Search for the character in the font table |
neilt6 | 0:b876cf091464 | 38 | for(int i = 0; i < m_FontTable[0]; i++) { |
neilt6 | 0:b876cf091464 | 39 | if (*ptr == c) { |
neilt6 | 0:b876cf091464 | 40 | //Return the image |
neilt6 | 0:b876cf091464 | 41 | return BitmapImage(m_FontTable + ptr[1] * 0xFF + ptr[2], m_Color); |
neilt6 | 0:b876cf091464 | 42 | } |
neilt6 | 0:b876cf091464 | 43 | ptr += 3; |
neilt6 | 0:b876cf091464 | 44 | } |
neilt6 | 0:b876cf091464 | 45 | |
neilt6 | 0:b876cf091464 | 46 | //The character wasn't found, replace it with a space |
neilt6 | 0:b876cf091464 | 47 | c = ' '; |
neilt6 | 0:b876cf091464 | 48 | } |
neilt6 | 0:b876cf091464 | 49 | } |
neilt6 | 0:b876cf091464 | 50 | |
neilt6 | 0:b876cf091464 | 51 | unsigned int Font::color(void) |
neilt6 | 0:b876cf091464 | 52 | { |
neilt6 | 0:b876cf091464 | 53 | return m_Color; |
neilt6 | 0:b876cf091464 | 54 | } |
neilt6 | 0:b876cf091464 | 55 | |
neilt6 | 0:b876cf091464 | 56 | void Font::color(unsigned int c) |
neilt6 | 0:b876cf091464 | 57 | { |
neilt6 | 0:b876cf091464 | 58 | m_Color = c; |
neilt6 | 0:b876cf091464 | 59 | } |
neilt6 | 0:b876cf091464 | 60 | |
neilt6 | 0:b876cf091464 | 61 | int Font::height(void) |
neilt6 | 0:b876cf091464 | 62 | { |
neilt6 | 0:b876cf091464 | 63 | return m_FontTable[1]; |
neilt6 | 0:b876cf091464 | 64 | } |
neilt6 | 0:b876cf091464 | 65 | |
neilt6 | 0:b876cf091464 | 66 | int Font::measureString(const char *str) |
neilt6 | 0:b876cf091464 | 67 | { |
neilt6 | 0:b876cf091464 | 68 | int i = 0; |
neilt6 | 0:b876cf091464 | 69 | int slen = 0; |
neilt6 | 0:b876cf091464 | 70 | |
neilt6 | 0:b876cf091464 | 71 | //Find the length in pixels of the whole string |
neilt6 | 0:b876cf091464 | 72 | while (str[i] != NULL) { |
neilt6 | 0:b876cf091464 | 73 | slen += glyph(*str).width(); |
neilt6 | 0:b876cf091464 | 74 | i++; |
neilt6 | 0:b876cf091464 | 75 | } |
neilt6 | 0:b876cf091464 | 76 | |
neilt6 | 0:b876cf091464 | 77 | return slen; |
neilt6 | 0:b876cf091464 | 78 | } |
neilt6 | 0:b876cf091464 | 79 | |
neilt6 | 0:b876cf091464 | 80 | int Font::measureWord(const char *str) |
neilt6 | 0:b876cf091464 | 81 | { |
neilt6 | 0:b876cf091464 | 82 | int i = 0; |
neilt6 | 0:b876cf091464 | 83 | int wlen = 0; |
neilt6 | 0:b876cf091464 | 84 | |
neilt6 | 0:b876cf091464 | 85 | //Find the length in pixels of the next word (separated by whitespace) |
neilt6 | 0:b876cf091464 | 86 | while ((str[i] > ' ') && (str[i] <= 0x7E)) { |
neilt6 | 0:b876cf091464 | 87 | wlen += glyph(*str).width(); |
neilt6 | 0:b876cf091464 | 88 | i++; |
neilt6 | 0:b876cf091464 | 89 | } |
neilt6 | 0:b876cf091464 | 90 | |
neilt6 | 0:b876cf091464 | 91 | return wlen; |
neilt6 | 0:b876cf091464 | 92 | } |