el h / SimpleGUI

Fork of SimpleGUI by Duncan McIntyre

Revision:
3:cb004f59b715
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Core/Font.h	Sun Mar 27 14:41:31 2016 +0000
@@ -0,0 +1,76 @@
+#ifndef UNIGRAPHICFONTH
+#define UNIGRAPHICFONTH
+/**
+* A simple class to abstract out dealing with font metrics.
+* Allows other applications to consume fonts and manage layout in the same way as the UniGraphic library
+**/
+
+class Font
+{
+
+public:
+    uint8_t width, height, bytesPerLine, offset;
+    uint8_t firstAscii, lastAscii, proportional;
+    uint8_t zoomX, zoomY;
+    
+    /**
+    * The actual zoomed height of a character
+    **/
+    uint16_t zoomH;
+    /**
+    * The nominal zoomed width of a character
+    **/
+    uint16_t zoomW;
+
+    Font(unsigned char* font, unsigned char firstAsciiCode=32, unsigned char lastAsciiCode=127, bool proportional = true)
+        : _font(font), firstAscii(firstAsciiCode), lastAscii(lastAsciiCode), proportional(proportional) {
+        // read font parameter from start of array in format understood by UniGraphics for now
+        //fontoffset = font[0];                    // bytes / char
+        width = font[1];
+        height = font[2];
+        //fontbpl = font[3];                       // bytes per line
+        bytesPerLine = (height+7) >> 3; //bytes per line, rounded up to multiple of 8
+        offset = (width * bytesPerLine) + 1;
+        setZoom(1,1);
+    }
+
+    void setZoom(unsigned char xmul, unsigned char ymul) {
+        zoomX=((xmul==0) ? 1:xmul);
+        zoomY=((ymul==0) ? 1:ymul);
+        zoomH = zoomY * height;
+        zoomW = zoomX * width;
+    }
+
+    uint8_t widthOf(char c) {
+
+        uint8_t cx, charWidth;
+        unsigned char* glyph;
+
+        glyph = getGlyph(c);
+        if(glyph == NULL) {
+            return 0;
+        }
+        
+        charWidth = glyph[0];                          // width of actual char
+
+        if(proportional && (charWidth+1) < width) {
+            cx = (charWidth * zoomX) + 1; // put at least 1 blank after variable-width characters, except characters that occupy whole fonthor space like ""
+        } else {
+            cx = width * zoomX;
+        }
+        return cx;
+    }
+    
+    uint8_t* getGlyph(char c) {
+        if(c<firstAscii || c>lastAscii) {
+            return NULL;
+        }
+        return &_font[((c - firstAscii) * offset) + 4];
+    }
+
+private:
+
+    unsigned char *_font;
+
+};
+#endif
\ No newline at end of file