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.
Dependents: TouchScreenGUIDemo
Core/Font.h@3:cb004f59b715, 2016-03-27 (annotated)
- Committer:
- duncanFrance
- Date:
- Sun Mar 27 14:41:31 2016 +0000
- Revision:
- 3:cb004f59b715
Added FontRenderer
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| duncanFrance | 3:cb004f59b715 | 1 | #ifndef UNIGRAPHICFONTH |
| duncanFrance | 3:cb004f59b715 | 2 | #define UNIGRAPHICFONTH |
| duncanFrance | 3:cb004f59b715 | 3 | /** |
| duncanFrance | 3:cb004f59b715 | 4 | * A simple class to abstract out dealing with font metrics. |
| duncanFrance | 3:cb004f59b715 | 5 | * Allows other applications to consume fonts and manage layout in the same way as the UniGraphic library |
| duncanFrance | 3:cb004f59b715 | 6 | **/ |
| duncanFrance | 3:cb004f59b715 | 7 | |
| duncanFrance | 3:cb004f59b715 | 8 | class Font |
| duncanFrance | 3:cb004f59b715 | 9 | { |
| duncanFrance | 3:cb004f59b715 | 10 | |
| duncanFrance | 3:cb004f59b715 | 11 | public: |
| duncanFrance | 3:cb004f59b715 | 12 | uint8_t width, height, bytesPerLine, offset; |
| duncanFrance | 3:cb004f59b715 | 13 | uint8_t firstAscii, lastAscii, proportional; |
| duncanFrance | 3:cb004f59b715 | 14 | uint8_t zoomX, zoomY; |
| duncanFrance | 3:cb004f59b715 | 15 | |
| duncanFrance | 3:cb004f59b715 | 16 | /** |
| duncanFrance | 3:cb004f59b715 | 17 | * The actual zoomed height of a character |
| duncanFrance | 3:cb004f59b715 | 18 | **/ |
| duncanFrance | 3:cb004f59b715 | 19 | uint16_t zoomH; |
| duncanFrance | 3:cb004f59b715 | 20 | /** |
| duncanFrance | 3:cb004f59b715 | 21 | * The nominal zoomed width of a character |
| duncanFrance | 3:cb004f59b715 | 22 | **/ |
| duncanFrance | 3:cb004f59b715 | 23 | uint16_t zoomW; |
| duncanFrance | 3:cb004f59b715 | 24 | |
| duncanFrance | 3:cb004f59b715 | 25 | Font(unsigned char* font, unsigned char firstAsciiCode=32, unsigned char lastAsciiCode=127, bool proportional = true) |
| duncanFrance | 3:cb004f59b715 | 26 | : _font(font), firstAscii(firstAsciiCode), lastAscii(lastAsciiCode), proportional(proportional) { |
| duncanFrance | 3:cb004f59b715 | 27 | // read font parameter from start of array in format understood by UniGraphics for now |
| duncanFrance | 3:cb004f59b715 | 28 | //fontoffset = font[0]; // bytes / char |
| duncanFrance | 3:cb004f59b715 | 29 | width = font[1]; |
| duncanFrance | 3:cb004f59b715 | 30 | height = font[2]; |
| duncanFrance | 3:cb004f59b715 | 31 | //fontbpl = font[3]; // bytes per line |
| duncanFrance | 3:cb004f59b715 | 32 | bytesPerLine = (height+7) >> 3; //bytes per line, rounded up to multiple of 8 |
| duncanFrance | 3:cb004f59b715 | 33 | offset = (width * bytesPerLine) + 1; |
| duncanFrance | 3:cb004f59b715 | 34 | setZoom(1,1); |
| duncanFrance | 3:cb004f59b715 | 35 | } |
| duncanFrance | 3:cb004f59b715 | 36 | |
| duncanFrance | 3:cb004f59b715 | 37 | void setZoom(unsigned char xmul, unsigned char ymul) { |
| duncanFrance | 3:cb004f59b715 | 38 | zoomX=((xmul==0) ? 1:xmul); |
| duncanFrance | 3:cb004f59b715 | 39 | zoomY=((ymul==0) ? 1:ymul); |
| duncanFrance | 3:cb004f59b715 | 40 | zoomH = zoomY * height; |
| duncanFrance | 3:cb004f59b715 | 41 | zoomW = zoomX * width; |
| duncanFrance | 3:cb004f59b715 | 42 | } |
| duncanFrance | 3:cb004f59b715 | 43 | |
| duncanFrance | 3:cb004f59b715 | 44 | uint8_t widthOf(char c) { |
| duncanFrance | 3:cb004f59b715 | 45 | |
| duncanFrance | 3:cb004f59b715 | 46 | uint8_t cx, charWidth; |
| duncanFrance | 3:cb004f59b715 | 47 | unsigned char* glyph; |
| duncanFrance | 3:cb004f59b715 | 48 | |
| duncanFrance | 3:cb004f59b715 | 49 | glyph = getGlyph(c); |
| duncanFrance | 3:cb004f59b715 | 50 | if(glyph == NULL) { |
| duncanFrance | 3:cb004f59b715 | 51 | return 0; |
| duncanFrance | 3:cb004f59b715 | 52 | } |
| duncanFrance | 3:cb004f59b715 | 53 | |
| duncanFrance | 3:cb004f59b715 | 54 | charWidth = glyph[0]; // width of actual char |
| duncanFrance | 3:cb004f59b715 | 55 | |
| duncanFrance | 3:cb004f59b715 | 56 | if(proportional && (charWidth+1) < width) { |
| duncanFrance | 3:cb004f59b715 | 57 | cx = (charWidth * zoomX) + 1; // put at least 1 blank after variable-width characters, except characters that occupy whole fonthor space like "" |
| duncanFrance | 3:cb004f59b715 | 58 | } else { |
| duncanFrance | 3:cb004f59b715 | 59 | cx = width * zoomX; |
| duncanFrance | 3:cb004f59b715 | 60 | } |
| duncanFrance | 3:cb004f59b715 | 61 | return cx; |
| duncanFrance | 3:cb004f59b715 | 62 | } |
| duncanFrance | 3:cb004f59b715 | 63 | |
| duncanFrance | 3:cb004f59b715 | 64 | uint8_t* getGlyph(char c) { |
| duncanFrance | 3:cb004f59b715 | 65 | if(c<firstAscii || c>lastAscii) { |
| duncanFrance | 3:cb004f59b715 | 66 | return NULL; |
| duncanFrance | 3:cb004f59b715 | 67 | } |
| duncanFrance | 3:cb004f59b715 | 68 | return &_font[((c - firstAscii) * offset) + 4]; |
| duncanFrance | 3:cb004f59b715 | 69 | } |
| duncanFrance | 3:cb004f59b715 | 70 | |
| duncanFrance | 3:cb004f59b715 | 71 | private: |
| duncanFrance | 3:cb004f59b715 | 72 | |
| duncanFrance | 3:cb004f59b715 | 73 | unsigned char *_font; |
| duncanFrance | 3:cb004f59b715 | 74 | |
| duncanFrance | 3:cb004f59b715 | 75 | }; |
| duncanFrance | 3:cb004f59b715 | 76 | #endif |