hadif azli / Mbed 2 deprecated TEST123

Dependencies:   mbed Blynk

Committer:
lixianyu
Date:
Mon Jun 13 02:21:11 2016 +0000
Revision:
1:0e75de2a5d21
Parent:
0:d8f4c441e032
u8glib???????????????????????????Adafruit_GFX????OLED????????bitmap??????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lixianyu 0:d8f4c441e032 1 /*
lixianyu 0:d8f4c441e032 2
lixianyu 0:d8f4c441e032 3 u8g_font.c
lixianyu 0:d8f4c441e032 4
lixianyu 0:d8f4c441e032 5 U8G Font High Level Interface
lixianyu 0:d8f4c441e032 6
lixianyu 0:d8f4c441e032 7 Universal 8bit Graphics Library
lixianyu 0:d8f4c441e032 8
lixianyu 0:d8f4c441e032 9 Copyright (c) 2011, olikraus@gmail.com
lixianyu 0:d8f4c441e032 10 All rights reserved.
lixianyu 0:d8f4c441e032 11
lixianyu 0:d8f4c441e032 12 Redistribution and use in source and binary forms, with or without modification,
lixianyu 0:d8f4c441e032 13 are permitted provided that the following conditions are met:
lixianyu 0:d8f4c441e032 14
lixianyu 0:d8f4c441e032 15 * Redistributions of source code must retain the above copyright notice, this list
lixianyu 0:d8f4c441e032 16 of conditions and the following disclaimer.
lixianyu 0:d8f4c441e032 17
lixianyu 0:d8f4c441e032 18 * Redistributions in binary form must reproduce the above copyright notice, this
lixianyu 0:d8f4c441e032 19 list of conditions and the following disclaimer in the documentation and/or other
lixianyu 0:d8f4c441e032 20 materials provided with the distribution.
lixianyu 0:d8f4c441e032 21
lixianyu 0:d8f4c441e032 22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
lixianyu 0:d8f4c441e032 23 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
lixianyu 0:d8f4c441e032 24 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
lixianyu 0:d8f4c441e032 25 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
lixianyu 0:d8f4c441e032 26 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
lixianyu 0:d8f4c441e032 27 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
lixianyu 0:d8f4c441e032 28 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
lixianyu 0:d8f4c441e032 29 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
lixianyu 0:d8f4c441e032 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
lixianyu 0:d8f4c441e032 31 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
lixianyu 0:d8f4c441e032 32 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
lixianyu 0:d8f4c441e032 33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
lixianyu 0:d8f4c441e032 34 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
lixianyu 0:d8f4c441e032 35
lixianyu 0:d8f4c441e032 36 */
lixianyu 0:d8f4c441e032 37
lixianyu 0:d8f4c441e032 38 #include "u8g.h"
lixianyu 0:d8f4c441e032 39
lixianyu 0:d8f4c441e032 40 /* font api */
lixianyu 0:d8f4c441e032 41
lixianyu 0:d8f4c441e032 42 /* pointer to the start adress of the glyph, points to progmem area */
lixianyu 0:d8f4c441e032 43 typedef void * u8g_glyph_t;
lixianyu 0:d8f4c441e032 44
lixianyu 0:d8f4c441e032 45 /* size of the font data structure, there is no struct or class... */
lixianyu 0:d8f4c441e032 46 #define U8G_FONT_DATA_STRUCT_SIZE 17
lixianyu 0:d8f4c441e032 47
lixianyu 0:d8f4c441e032 48 /*
lixianyu 0:d8f4c441e032 49 ... instead the fields of the font data structure are accessed directly by offset
lixianyu 0:d8f4c441e032 50 font information
lixianyu 0:d8f4c441e032 51 offset
lixianyu 0:d8f4c441e032 52 0 font format
lixianyu 0:d8f4c441e032 53 1 FONTBOUNDINGBOX width unsigned
lixianyu 0:d8f4c441e032 54 2 FONTBOUNDINGBOX height unsigned
lixianyu 0:d8f4c441e032 55 3 FONTBOUNDINGBOX x-offset signed
lixianyu 0:d8f4c441e032 56 4 FONTBOUNDINGBOX y-offset signed
lixianyu 0:d8f4c441e032 57 5 capital A height unsigned
lixianyu 0:d8f4c441e032 58 6 start 'A'
lixianyu 0:d8f4c441e032 59 8 start 'a'
lixianyu 0:d8f4c441e032 60 10 encoding start
lixianyu 0:d8f4c441e032 61 11 encoding end
lixianyu 0:d8f4c441e032 62 12 descent 'g' negative: below baseline
lixianyu 0:d8f4c441e032 63 13 font max ascent
lixianyu 0:d8f4c441e032 64 14 font min decent negative: below baseline
lixianyu 0:d8f4c441e032 65 15 font xascent
lixianyu 0:d8f4c441e032 66 16 font xdecent negative: below baseline
lixianyu 0:d8f4c441e032 67
lixianyu 0:d8f4c441e032 68 */
lixianyu 0:d8f4c441e032 69
lixianyu 0:d8f4c441e032 70 /* use case: What is the width and the height of the minimal box into which string s fints? */
lixianyu 0:d8f4c441e032 71 void u8g_font_GetStrSize(const void *font, const char *s, u8g_uint_t *width, u8g_uint_t *height);
lixianyu 0:d8f4c441e032 72 void u8g_font_GetStrSizeP(const void *font, const char *s, u8g_uint_t *width, u8g_uint_t *height);
lixianyu 0:d8f4c441e032 73
lixianyu 0:d8f4c441e032 74 /* use case: lower left edge of a minimal box is known, what is the correct x, y position for the string draw procedure */
lixianyu 0:d8f4c441e032 75 void u8g_font_AdjustXYToDraw(const void *font, const char *s, u8g_uint_t *x, u8g_uint_t *y);
lixianyu 0:d8f4c441e032 76 void u8g_font_AdjustXYToDrawP(const void *font, const char *s, u8g_uint_t *x, u8g_uint_t *y);
lixianyu 0:d8f4c441e032 77
lixianyu 0:d8f4c441e032 78 /* use case: Baseline origin known, return minimal box */
lixianyu 0:d8f4c441e032 79 void u8g_font_GetStrMinBox(u8g_t *u8g, const void *font, const char *s, u8g_uint_t *x, u8g_uint_t *y, u8g_uint_t *width, u8g_uint_t *height);
lixianyu 0:d8f4c441e032 80
lixianyu 0:d8f4c441e032 81 /* procedures */
lixianyu 0:d8f4c441e032 82
lixianyu 0:d8f4c441e032 83 /*========================================================================*/
lixianyu 0:d8f4c441e032 84 /* low level byte and word access */
lixianyu 0:d8f4c441e032 85
lixianyu 0:d8f4c441e032 86 /* removed NOINLINE, because it leads to smaller code, might also be faster */
lixianyu 0:d8f4c441e032 87 //static uint8_t u8g_font_get_byte(const u8g_fntpgm_uint8_t *font, uint8_t offset) U8G_NOINLINE;
lixianyu 0:d8f4c441e032 88 static uint8_t u8g_font_get_byte(const u8g_fntpgm_uint8_t *font, uint8_t offset)
lixianyu 0:d8f4c441e032 89 {
lixianyu 0:d8f4c441e032 90 font += offset;
lixianyu 0:d8f4c441e032 91 return u8g_pgm_read( (u8g_pgm_uint8_t *)font );
lixianyu 0:d8f4c441e032 92 }
lixianyu 0:d8f4c441e032 93
lixianyu 0:d8f4c441e032 94 static uint16_t u8g_font_get_word(const u8g_fntpgm_uint8_t *font, uint8_t offset) U8G_NOINLINE;
lixianyu 0:d8f4c441e032 95 static uint16_t u8g_font_get_word(const u8g_fntpgm_uint8_t *font, uint8_t offset)
lixianyu 0:d8f4c441e032 96 {
lixianyu 0:d8f4c441e032 97 uint16_t pos;
lixianyu 0:d8f4c441e032 98 font += offset;
lixianyu 0:d8f4c441e032 99 pos = u8g_pgm_read( (u8g_pgm_uint8_t *)font );
lixianyu 0:d8f4c441e032 100 font++;
lixianyu 0:d8f4c441e032 101 pos <<= 8;
lixianyu 0:d8f4c441e032 102 pos += u8g_pgm_read( (u8g_pgm_uint8_t *)font);
lixianyu 0:d8f4c441e032 103 return pos;
lixianyu 0:d8f4c441e032 104 }
lixianyu 0:d8f4c441e032 105
lixianyu 0:d8f4c441e032 106 /*========================================================================*/
lixianyu 0:d8f4c441e032 107 /* direct access on the font */
lixianyu 0:d8f4c441e032 108
lixianyu 0:d8f4c441e032 109 static uint8_t u8g_font_GetFormat(const u8g_fntpgm_uint8_t *font) U8G_NOINLINE;
lixianyu 0:d8f4c441e032 110 static uint8_t u8g_font_GetFormat(const u8g_fntpgm_uint8_t *font)
lixianyu 0:d8f4c441e032 111 {
lixianyu 0:d8f4c441e032 112 return u8g_font_get_byte(font, 0);
lixianyu 0:d8f4c441e032 113 }
lixianyu 0:d8f4c441e032 114
lixianyu 0:d8f4c441e032 115 static uint8_t u8g_font_GetFontGlyphStructureSize(const u8g_fntpgm_uint8_t *font) U8G_NOINLINE;
lixianyu 0:d8f4c441e032 116 static uint8_t u8g_font_GetFontGlyphStructureSize(const u8g_fntpgm_uint8_t *font)
lixianyu 0:d8f4c441e032 117 {
lixianyu 0:d8f4c441e032 118 switch(u8g_font_GetFormat(font))
lixianyu 0:d8f4c441e032 119 {
lixianyu 0:d8f4c441e032 120 case 0: return 6;
lixianyu 0:d8f4c441e032 121 case 1: return 3;
lixianyu 0:d8f4c441e032 122 case 2: return 6;
lixianyu 0:d8f4c441e032 123 }
lixianyu 0:d8f4c441e032 124 return 3;
lixianyu 0:d8f4c441e032 125 }
lixianyu 0:d8f4c441e032 126
lixianyu 0:d8f4c441e032 127 static uint8_t u8g_font_GetBBXWidth(const void *font)
lixianyu 0:d8f4c441e032 128 {
lixianyu 0:d8f4c441e032 129 return u8g_font_get_byte(font, 1);
lixianyu 0:d8f4c441e032 130 }
lixianyu 0:d8f4c441e032 131
lixianyu 0:d8f4c441e032 132 static uint8_t u8g_font_GetBBXHeight(const void *font)
lixianyu 0:d8f4c441e032 133 {
lixianyu 0:d8f4c441e032 134 return u8g_font_get_byte(font, 2);
lixianyu 0:d8f4c441e032 135 }
lixianyu 0:d8f4c441e032 136
lixianyu 0:d8f4c441e032 137 static int8_t u8g_font_GetBBXOffX(const void *font)
lixianyu 0:d8f4c441e032 138 {
lixianyu 0:d8f4c441e032 139 return u8g_font_get_byte(font, 3);
lixianyu 0:d8f4c441e032 140 }
lixianyu 0:d8f4c441e032 141
lixianyu 0:d8f4c441e032 142 static int8_t u8g_font_GetBBXOffY(const void *font)
lixianyu 0:d8f4c441e032 143 {
lixianyu 0:d8f4c441e032 144 return u8g_font_get_byte(font, 4);
lixianyu 0:d8f4c441e032 145 }
lixianyu 0:d8f4c441e032 146
lixianyu 0:d8f4c441e032 147 uint8_t u8g_font_GetCapitalAHeight(const void *font)
lixianyu 0:d8f4c441e032 148 {
lixianyu 0:d8f4c441e032 149 return u8g_font_get_byte(font, 5);
lixianyu 0:d8f4c441e032 150 }
lixianyu 0:d8f4c441e032 151
lixianyu 0:d8f4c441e032 152 uint16_t u8g_font_GetEncoding65Pos(const void *font) U8G_NOINLINE;
lixianyu 0:d8f4c441e032 153 uint16_t u8g_font_GetEncoding65Pos(const void *font)
lixianyu 0:d8f4c441e032 154 {
lixianyu 0:d8f4c441e032 155 return u8g_font_get_word(font, 6);
lixianyu 0:d8f4c441e032 156 }
lixianyu 0:d8f4c441e032 157
lixianyu 0:d8f4c441e032 158 uint16_t u8g_font_GetEncoding97Pos(const void *font) U8G_NOINLINE;
lixianyu 0:d8f4c441e032 159 uint16_t u8g_font_GetEncoding97Pos(const void *font)
lixianyu 0:d8f4c441e032 160 {
lixianyu 0:d8f4c441e032 161 return u8g_font_get_word(font, 8);
lixianyu 0:d8f4c441e032 162 }
lixianyu 0:d8f4c441e032 163
lixianyu 0:d8f4c441e032 164 uint8_t u8g_font_GetFontStartEncoding(const void *font)
lixianyu 0:d8f4c441e032 165 {
lixianyu 0:d8f4c441e032 166 return u8g_font_get_byte(font, 10);
lixianyu 0:d8f4c441e032 167 }
lixianyu 0:d8f4c441e032 168
lixianyu 0:d8f4c441e032 169 uint8_t u8g_font_GetFontEndEncoding(const void *font)
lixianyu 0:d8f4c441e032 170 {
lixianyu 0:d8f4c441e032 171 return u8g_font_get_byte(font, 11);
lixianyu 0:d8f4c441e032 172 }
lixianyu 0:d8f4c441e032 173
lixianyu 0:d8f4c441e032 174 int8_t u8g_font_GetLowerGDescent(const void *font)
lixianyu 0:d8f4c441e032 175 {
lixianyu 0:d8f4c441e032 176 return u8g_font_get_byte(font, 12);
lixianyu 0:d8f4c441e032 177 }
lixianyu 0:d8f4c441e032 178
lixianyu 0:d8f4c441e032 179 int8_t u8g_font_GetFontAscent(const void *font)
lixianyu 0:d8f4c441e032 180 {
lixianyu 0:d8f4c441e032 181 return u8g_font_get_byte(font, 13);
lixianyu 0:d8f4c441e032 182 }
lixianyu 0:d8f4c441e032 183
lixianyu 0:d8f4c441e032 184 int8_t u8g_font_GetFontDescent(const void *font)
lixianyu 0:d8f4c441e032 185 {
lixianyu 0:d8f4c441e032 186 return u8g_font_get_byte(font, 14);
lixianyu 0:d8f4c441e032 187 }
lixianyu 0:d8f4c441e032 188
lixianyu 0:d8f4c441e032 189 int8_t u8g_font_GetFontXAscent(const void *font)
lixianyu 0:d8f4c441e032 190 {
lixianyu 0:d8f4c441e032 191 return u8g_font_get_byte(font, 15);
lixianyu 0:d8f4c441e032 192 }
lixianyu 0:d8f4c441e032 193
lixianyu 0:d8f4c441e032 194 int8_t u8g_font_GetFontXDescent(const void *font)
lixianyu 0:d8f4c441e032 195 {
lixianyu 0:d8f4c441e032 196 return u8g_font_get_byte(font, 16);
lixianyu 0:d8f4c441e032 197 }
lixianyu 0:d8f4c441e032 198
lixianyu 0:d8f4c441e032 199
lixianyu 0:d8f4c441e032 200 /* return the data start for a font and the glyph pointer */
lixianyu 0:d8f4c441e032 201 static uint8_t *u8g_font_GetGlyphDataStart(const void *font, u8g_glyph_t g)
lixianyu 0:d8f4c441e032 202 {
lixianyu 0:d8f4c441e032 203 return ((u8g_fntpgm_uint8_t *)g) + u8g_font_GetFontGlyphStructureSize(font);
lixianyu 0:d8f4c441e032 204 }
lixianyu 0:d8f4c441e032 205
lixianyu 0:d8f4c441e032 206 /* calculate the overall length of the font, only used to create the picture for the google wiki */
lixianyu 0:d8f4c441e032 207 size_t u8g_font_GetSize(const void *font)
lixianyu 0:d8f4c441e032 208 {
lixianyu 0:d8f4c441e032 209 uint8_t *p = (uint8_t *)(font);
lixianyu 0:d8f4c441e032 210 uint8_t font_format = u8g_font_GetFormat(font);
lixianyu 0:d8f4c441e032 211 uint8_t data_structure_size = u8g_font_GetFontGlyphStructureSize(font);
lixianyu 0:d8f4c441e032 212 uint8_t start, end;
lixianyu 0:d8f4c441e032 213 uint8_t i;
lixianyu 0:d8f4c441e032 214 uint8_t mask = 255;
lixianyu 0:d8f4c441e032 215
lixianyu 0:d8f4c441e032 216 start = u8g_font_GetFontStartEncoding(font);
lixianyu 0:d8f4c441e032 217 end = u8g_font_GetFontEndEncoding(font);
lixianyu 0:d8f4c441e032 218
lixianyu 0:d8f4c441e032 219 if ( font_format == 1 )
lixianyu 0:d8f4c441e032 220 mask = 15;
lixianyu 0:d8f4c441e032 221
lixianyu 0:d8f4c441e032 222 p += U8G_FONT_DATA_STRUCT_SIZE; /* skip font general information */
lixianyu 0:d8f4c441e032 223
lixianyu 0:d8f4c441e032 224 i = start;
lixianyu 0:d8f4c441e032 225 for(;;)
lixianyu 0:d8f4c441e032 226 {
lixianyu 0:d8f4c441e032 227 if ( u8g_pgm_read((u8g_pgm_uint8_t *)(p)) == 255 )
lixianyu 0:d8f4c441e032 228 {
lixianyu 0:d8f4c441e032 229 p += 1;
lixianyu 0:d8f4c441e032 230 }
lixianyu 0:d8f4c441e032 231 else
lixianyu 0:d8f4c441e032 232 {
lixianyu 0:d8f4c441e032 233 p += u8g_pgm_read( ((u8g_pgm_uint8_t *)(p)) + 2 ) & mask;
lixianyu 0:d8f4c441e032 234 p += data_structure_size;
lixianyu 0:d8f4c441e032 235 }
lixianyu 0:d8f4c441e032 236 if ( i == end )
lixianyu 0:d8f4c441e032 237 break;
lixianyu 0:d8f4c441e032 238 i++;
lixianyu 0:d8f4c441e032 239 }
lixianyu 0:d8f4c441e032 240
lixianyu 0:d8f4c441e032 241 return p - (uint8_t *)font;
lixianyu 0:d8f4c441e032 242 }
lixianyu 0:d8f4c441e032 243
lixianyu 0:d8f4c441e032 244 /*========================================================================*/
lixianyu 0:d8f4c441e032 245 /* u8g interface, font access */
lixianyu 0:d8f4c441e032 246
lixianyu 0:d8f4c441e032 247 uint8_t u8g_GetFontBBXWidth(u8g_t *u8g)
lixianyu 0:d8f4c441e032 248 {
lixianyu 0:d8f4c441e032 249 return u8g_font_GetBBXWidth(u8g->font);
lixianyu 0:d8f4c441e032 250 }
lixianyu 0:d8f4c441e032 251
lixianyu 0:d8f4c441e032 252 uint8_t u8g_GetFontBBXHeight(u8g_t *u8g)
lixianyu 0:d8f4c441e032 253 {
lixianyu 0:d8f4c441e032 254 return u8g_font_GetBBXHeight(u8g->font);
lixianyu 0:d8f4c441e032 255 }
lixianyu 0:d8f4c441e032 256
lixianyu 0:d8f4c441e032 257 int8_t u8g_GetFontBBXOffX(u8g_t *u8g) U8G_NOINLINE;
lixianyu 0:d8f4c441e032 258 int8_t u8g_GetFontBBXOffX(u8g_t *u8g)
lixianyu 0:d8f4c441e032 259 {
lixianyu 0:d8f4c441e032 260 return u8g_font_GetBBXOffX(u8g->font);
lixianyu 0:d8f4c441e032 261 }
lixianyu 0:d8f4c441e032 262
lixianyu 0:d8f4c441e032 263 int8_t u8g_GetFontBBXOffY(u8g_t *u8g) U8G_NOINLINE;
lixianyu 0:d8f4c441e032 264 int8_t u8g_GetFontBBXOffY(u8g_t *u8g)
lixianyu 0:d8f4c441e032 265 {
lixianyu 0:d8f4c441e032 266 return u8g_font_GetBBXOffY(u8g->font);
lixianyu 0:d8f4c441e032 267 }
lixianyu 0:d8f4c441e032 268
lixianyu 0:d8f4c441e032 269 uint8_t u8g_GetFontCapitalAHeight(u8g_t *u8g) U8G_NOINLINE;
lixianyu 0:d8f4c441e032 270 uint8_t u8g_GetFontCapitalAHeight(u8g_t *u8g)
lixianyu 0:d8f4c441e032 271 {
lixianyu 0:d8f4c441e032 272 return u8g_font_GetCapitalAHeight(u8g->font);
lixianyu 0:d8f4c441e032 273 }
lixianyu 0:d8f4c441e032 274
lixianyu 0:d8f4c441e032 275 /*========================================================================*/
lixianyu 0:d8f4c441e032 276 /* glyph handling */
lixianyu 0:d8f4c441e032 277
lixianyu 0:d8f4c441e032 278 static void u8g_CopyGlyphDataToCache(u8g_t *u8g, u8g_glyph_t g)
lixianyu 0:d8f4c441e032 279 {
lixianyu 0:d8f4c441e032 280 uint8_t tmp;
lixianyu 0:d8f4c441e032 281 switch( u8g_font_GetFormat(u8g->font) )
lixianyu 0:d8f4c441e032 282 {
lixianyu 0:d8f4c441e032 283 case 0:
lixianyu 0:d8f4c441e032 284 case 2:
lixianyu 0:d8f4c441e032 285 /*
lixianyu 0:d8f4c441e032 286 format 0
lixianyu 0:d8f4c441e032 287 glyph information
lixianyu 0:d8f4c441e032 288 offset
lixianyu 0:d8f4c441e032 289 0 BBX width unsigned
lixianyu 0:d8f4c441e032 290 1 BBX height unsigned
lixianyu 0:d8f4c441e032 291 2 data size unsigned (BBX width + 7)/8 * BBX height
lixianyu 0:d8f4c441e032 292 3 DWIDTH signed
lixianyu 0:d8f4c441e032 293 4 BBX xoffset signed
lixianyu 0:d8f4c441e032 294 5 BBX yoffset signed
lixianyu 0:d8f4c441e032 295 byte 0 == 255 indicates empty glyph
lixianyu 0:d8f4c441e032 296 */
lixianyu 0:d8f4c441e032 297 u8g->glyph_width = u8g_pgm_read( ((u8g_pgm_uint8_t *)g) + 0 );
lixianyu 0:d8f4c441e032 298 u8g->glyph_height = u8g_pgm_read( ((u8g_pgm_uint8_t *)g) + 1 );
lixianyu 0:d8f4c441e032 299 u8g->glyph_dx = u8g_pgm_read( ((u8g_pgm_uint8_t *)g) + 3 );
lixianyu 0:d8f4c441e032 300 u8g->glyph_x = u8g_pgm_read( ((u8g_pgm_uint8_t *)g) + 4 );
lixianyu 0:d8f4c441e032 301 u8g->glyph_y = u8g_pgm_read( ((u8g_pgm_uint8_t *)g) + 5 );
lixianyu 0:d8f4c441e032 302 break;
lixianyu 0:d8f4c441e032 303 case 1:
lixianyu 0:d8f4c441e032 304 default:
lixianyu 0:d8f4c441e032 305 /*
lixianyu 0:d8f4c441e032 306 format 1
lixianyu 0:d8f4c441e032 307 0 BBX xoffset signed --> upper 4 Bit
lixianyu 0:d8f4c441e032 308 0 BBX yoffset signed --> lower 4 Bit
lixianyu 0:d8f4c441e032 309 1 BBX width unsigned --> upper 4 Bit
lixianyu 0:d8f4c441e032 310 1 BBX height unsigned --> lower 4 Bit
lixianyu 0:d8f4c441e032 311 2 data size unsigned -(BBX width + 7)/8 * BBX height --> lower 4 Bit
lixianyu 0:d8f4c441e032 312 2 DWIDTH signed --> upper 4 Bit
lixianyu 0:d8f4c441e032 313 byte 0 == 255 indicates empty glyph
lixianyu 0:d8f4c441e032 314 */
lixianyu 0:d8f4c441e032 315
lixianyu 0:d8f4c441e032 316 tmp = u8g_pgm_read( ((u8g_pgm_uint8_t *)g) + 0 );
lixianyu 0:d8f4c441e032 317 u8g->glyph_y = tmp & 15;
lixianyu 0:d8f4c441e032 318 u8g->glyph_y-=2;
lixianyu 0:d8f4c441e032 319 tmp >>= 4;
lixianyu 0:d8f4c441e032 320 u8g->glyph_x = tmp;
lixianyu 0:d8f4c441e032 321
lixianyu 0:d8f4c441e032 322 tmp = u8g_pgm_read( ((u8g_pgm_uint8_t *)g) + 1 );
lixianyu 0:d8f4c441e032 323 u8g->glyph_height = tmp & 15;
lixianyu 0:d8f4c441e032 324 tmp >>= 4;
lixianyu 0:d8f4c441e032 325 u8g->glyph_width = tmp;
lixianyu 0:d8f4c441e032 326
lixianyu 0:d8f4c441e032 327 tmp = u8g_pgm_read( ((u8g_pgm_uint8_t *)g) + 2 );
lixianyu 0:d8f4c441e032 328 tmp >>= 4;
lixianyu 0:d8f4c441e032 329 u8g->glyph_dx = tmp;
lixianyu 0:d8f4c441e032 330
lixianyu 0:d8f4c441e032 331
lixianyu 0:d8f4c441e032 332 break;
lixianyu 0:d8f4c441e032 333 }
lixianyu 0:d8f4c441e032 334 }
lixianyu 0:d8f4c441e032 335
lixianyu 0:d8f4c441e032 336 //void u8g_FillEmptyGlyphCache(u8g_t *u8g) U8G_NOINLINE;
lixianyu 0:d8f4c441e032 337 static void u8g_FillEmptyGlyphCache(u8g_t *u8g)
lixianyu 0:d8f4c441e032 338 {
lixianyu 0:d8f4c441e032 339 u8g->glyph_dx = 0;
lixianyu 0:d8f4c441e032 340 u8g->glyph_width = 0;
lixianyu 0:d8f4c441e032 341 u8g->glyph_height = 0;
lixianyu 0:d8f4c441e032 342 u8g->glyph_x = 0;
lixianyu 0:d8f4c441e032 343 u8g->glyph_y = 0;
lixianyu 0:d8f4c441e032 344 }
lixianyu 0:d8f4c441e032 345
lixianyu 0:d8f4c441e032 346 /*
lixianyu 0:d8f4c441e032 347 Find (with some speed optimization) and return a pointer to the glyph data structure
lixianyu 0:d8f4c441e032 348 Also uncompress (format 1) and copy the content of the data structure to the u8g structure
lixianyu 0:d8f4c441e032 349 */
lixianyu 0:d8f4c441e032 350 u8g_glyph_t u8g_GetGlyph(u8g_t *u8g, uint8_t requested_encoding)
lixianyu 0:d8f4c441e032 351 {
lixianyu 0:d8f4c441e032 352 uint8_t *p = (uint8_t *)(u8g->font);
lixianyu 0:d8f4c441e032 353 uint8_t font_format = u8g_font_GetFormat(u8g->font);
lixianyu 0:d8f4c441e032 354 uint8_t data_structure_size = u8g_font_GetFontGlyphStructureSize(u8g->font);
lixianyu 0:d8f4c441e032 355 uint8_t start, end;
lixianyu 0:d8f4c441e032 356 uint16_t pos;
lixianyu 0:d8f4c441e032 357 uint8_t i;
lixianyu 0:d8f4c441e032 358 uint8_t mask = 255;
lixianyu 0:d8f4c441e032 359
lixianyu 0:d8f4c441e032 360 if ( font_format == 1 )
lixianyu 0:d8f4c441e032 361 mask = 15;
lixianyu 0:d8f4c441e032 362
lixianyu 0:d8f4c441e032 363 start = u8g_font_GetFontStartEncoding(u8g->font);
lixianyu 0:d8f4c441e032 364 end = u8g_font_GetFontEndEncoding(u8g->font);
lixianyu 0:d8f4c441e032 365
lixianyu 0:d8f4c441e032 366 pos = u8g_font_GetEncoding97Pos(u8g->font);
lixianyu 0:d8f4c441e032 367 if ( requested_encoding >= 97 && pos > 0 )
lixianyu 0:d8f4c441e032 368 {
lixianyu 0:d8f4c441e032 369 p+= pos;
lixianyu 0:d8f4c441e032 370 start = 97;
lixianyu 0:d8f4c441e032 371 }
lixianyu 0:d8f4c441e032 372 else
lixianyu 0:d8f4c441e032 373 {
lixianyu 0:d8f4c441e032 374 pos = u8g_font_GetEncoding65Pos(u8g->font);
lixianyu 0:d8f4c441e032 375 if ( requested_encoding >= 65 && pos > 0 )
lixianyu 0:d8f4c441e032 376 {
lixianyu 0:d8f4c441e032 377 p+= pos;
lixianyu 0:d8f4c441e032 378 start = 65;
lixianyu 0:d8f4c441e032 379 }
lixianyu 0:d8f4c441e032 380 else
lixianyu 0:d8f4c441e032 381 p += U8G_FONT_DATA_STRUCT_SIZE; /* skip font general information */
lixianyu 0:d8f4c441e032 382 }
lixianyu 0:d8f4c441e032 383
lixianyu 0:d8f4c441e032 384 if ( requested_encoding > end )
lixianyu 0:d8f4c441e032 385 {
lixianyu 0:d8f4c441e032 386 u8g_FillEmptyGlyphCache(u8g);
lixianyu 0:d8f4c441e032 387 return NULL; /* not found */
lixianyu 0:d8f4c441e032 388 }
lixianyu 0:d8f4c441e032 389
lixianyu 0:d8f4c441e032 390 i = start;
lixianyu 0:d8f4c441e032 391 if ( i <= end )
lixianyu 0:d8f4c441e032 392 {
lixianyu 0:d8f4c441e032 393 for(;;)
lixianyu 0:d8f4c441e032 394 {
lixianyu 0:d8f4c441e032 395 if ( u8g_pgm_read((u8g_pgm_uint8_t *)(p)) == 255 )
lixianyu 0:d8f4c441e032 396 {
lixianyu 0:d8f4c441e032 397 p += 1;
lixianyu 0:d8f4c441e032 398 }
lixianyu 0:d8f4c441e032 399 else
lixianyu 0:d8f4c441e032 400 {
lixianyu 0:d8f4c441e032 401 if ( i == requested_encoding )
lixianyu 0:d8f4c441e032 402 {
lixianyu 0:d8f4c441e032 403 u8g_CopyGlyphDataToCache(u8g, p);
lixianyu 0:d8f4c441e032 404 return p;
lixianyu 0:d8f4c441e032 405 }
lixianyu 0:d8f4c441e032 406 p += u8g_pgm_read( ((u8g_pgm_uint8_t *)(p)) + 2 ) & mask;
lixianyu 0:d8f4c441e032 407 p += data_structure_size;
lixianyu 0:d8f4c441e032 408 }
lixianyu 0:d8f4c441e032 409 if ( i == end )
lixianyu 0:d8f4c441e032 410 break;
lixianyu 0:d8f4c441e032 411 i++;
lixianyu 0:d8f4c441e032 412 }
lixianyu 0:d8f4c441e032 413 }
lixianyu 0:d8f4c441e032 414
lixianyu 0:d8f4c441e032 415 u8g_FillEmptyGlyphCache(u8g);
lixianyu 0:d8f4c441e032 416
lixianyu 0:d8f4c441e032 417 return NULL;
lixianyu 0:d8f4c441e032 418 }
lixianyu 0:d8f4c441e032 419
lixianyu 0:d8f4c441e032 420 uint8_t u8g_IsGlyph(u8g_t *u8g, uint8_t requested_encoding)
lixianyu 0:d8f4c441e032 421 {
lixianyu 0:d8f4c441e032 422 if ( u8g_GetGlyph(u8g, requested_encoding) != NULL )
lixianyu 0:d8f4c441e032 423 return 1;
lixianyu 0:d8f4c441e032 424 return 0;
lixianyu 0:d8f4c441e032 425 }
lixianyu 0:d8f4c441e032 426
lixianyu 0:d8f4c441e032 427 int8_t u8g_GetGlyphDeltaX(u8g_t *u8g, uint8_t requested_encoding)
lixianyu 0:d8f4c441e032 428 {
lixianyu 0:d8f4c441e032 429 if ( u8g_GetGlyph(u8g, requested_encoding) == NULL )
lixianyu 0:d8f4c441e032 430 return 0; /* should never happen, so return something */
lixianyu 0:d8f4c441e032 431 return u8g->glyph_dx;
lixianyu 0:d8f4c441e032 432 }
lixianyu 0:d8f4c441e032 433
lixianyu 0:d8f4c441e032 434
lixianyu 0:d8f4c441e032 435 /*========================================================================*/
lixianyu 0:d8f4c441e032 436 /* glyph drawing procedures */
lixianyu 0:d8f4c441e032 437
lixianyu 0:d8f4c441e032 438 #ifdef OBSOLETE
lixianyu 0:d8f4c441e032 439 /*
lixianyu 0:d8f4c441e032 440 Draw a glyph
lixianyu 0:d8f4c441e032 441 x,y: left baseline position of the glyph
lixianyu 0:d8f4c441e032 442 */
lixianyu 0:d8f4c441e032 443 int8_t u8g_DrawGlyphDir(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, uint8_t dir, uint8_t encoding)
lixianyu 0:d8f4c441e032 444 {
lixianyu 0:d8f4c441e032 445 u8g_glyph_t g;
lixianyu 0:d8f4c441e032 446 uint8_t w, h, i, j;
lixianyu 0:d8f4c441e032 447 const u8g_pgm_uint8_t *data;
lixianyu 0:d8f4c441e032 448 uint8_t bytes_per_line;
lixianyu 0:d8f4c441e032 449 u8g_uint_t ix, iy;
lixianyu 0:d8f4c441e032 450
lixianyu 0:d8f4c441e032 451 g = u8g_GetGlyph(u8g, encoding);
lixianyu 0:d8f4c441e032 452 if ( g == NULL )
lixianyu 0:d8f4c441e032 453 return 0;
lixianyu 0:d8f4c441e032 454 w = u8g->glyph_width;
lixianyu 0:d8f4c441e032 455 h = u8g->glyph_height;
lixianyu 0:d8f4c441e032 456
lixianyu 0:d8f4c441e032 457 bytes_per_line = w;
lixianyu 0:d8f4c441e032 458 bytes_per_line += 7;
lixianyu 0:d8f4c441e032 459 bytes_per_line /= 8;
lixianyu 0:d8f4c441e032 460
lixianyu 0:d8f4c441e032 461 data = u8g_font_GetGlyphDataStart(u8g->font, g);
lixianyu 0:d8f4c441e032 462
lixianyu 0:d8f4c441e032 463 switch(dir)
lixianyu 0:d8f4c441e032 464 {
lixianyu 0:d8f4c441e032 465 case 0:
lixianyu 0:d8f4c441e032 466 x += u8g->glyph_x;
lixianyu 0:d8f4c441e032 467 y -= u8g->glyph_y;
lixianyu 0:d8f4c441e032 468 y--;
lixianyu 0:d8f4c441e032 469 //u8g_DrawFrame(u8g, x, y-h+1, w, h);
lixianyu 0:d8f4c441e032 470 if ( u8g_IsBBXIntersection(u8g, x, y-h+1, w, h) == 0 )
lixianyu 0:d8f4c441e032 471 return u8g->glyph_dx;
lixianyu 0:d8f4c441e032 472
lixianyu 0:d8f4c441e032 473 iy = y;
lixianyu 0:d8f4c441e032 474 iy -= h;
lixianyu 0:d8f4c441e032 475 iy++;
lixianyu 0:d8f4c441e032 476
lixianyu 0:d8f4c441e032 477 for( j = 0; j < h; j++ )
lixianyu 0:d8f4c441e032 478 {
lixianyu 0:d8f4c441e032 479 ix = x;
lixianyu 0:d8f4c441e032 480 for( i = 0; i < bytes_per_line; i++ )
lixianyu 0:d8f4c441e032 481 {
lixianyu 0:d8f4c441e032 482 u8g_Draw8Pixel(u8g, ix, iy, dir, u8g_pgm_read(data));
lixianyu 0:d8f4c441e032 483 data++;
lixianyu 0:d8f4c441e032 484 ix+=8;
lixianyu 0:d8f4c441e032 485 }
lixianyu 0:d8f4c441e032 486 iy++;
lixianyu 0:d8f4c441e032 487 }
lixianyu 0:d8f4c441e032 488 break;
lixianyu 0:d8f4c441e032 489 case 1:
lixianyu 0:d8f4c441e032 490 x += u8g->glyph_y;
lixianyu 0:d8f4c441e032 491 x++;
lixianyu 0:d8f4c441e032 492 y += u8g->glyph_x;
lixianyu 0:d8f4c441e032 493 //printf("enc %d, dir %d, x %d, y %d, w %d, h %d\n", encoding, dir, x, y, w, h);
lixianyu 0:d8f4c441e032 494 //u8g_DrawFrame(u8g, x, y, h, w);
lixianyu 0:d8f4c441e032 495 if ( u8g_IsBBXIntersection(u8g, x, y, h, w) == 0 )
lixianyu 0:d8f4c441e032 496 return u8g->glyph_dx;
lixianyu 0:d8f4c441e032 497
lixianyu 0:d8f4c441e032 498 ix = x;
lixianyu 0:d8f4c441e032 499 ix += h;
lixianyu 0:d8f4c441e032 500 ix--;
lixianyu 0:d8f4c441e032 501 for( j = 0; j < h; j++ )
lixianyu 0:d8f4c441e032 502 {
lixianyu 0:d8f4c441e032 503 iy = y;
lixianyu 0:d8f4c441e032 504 for( i = 0; i < bytes_per_line; i++ )
lixianyu 0:d8f4c441e032 505 {
lixianyu 0:d8f4c441e032 506 u8g_Draw8Pixel(u8g, ix, iy, dir, u8g_pgm_read(data));
lixianyu 0:d8f4c441e032 507 data++;
lixianyu 0:d8f4c441e032 508 iy+=8;
lixianyu 0:d8f4c441e032 509 }
lixianyu 0:d8f4c441e032 510 ix--;
lixianyu 0:d8f4c441e032 511 }
lixianyu 0:d8f4c441e032 512 break;
lixianyu 0:d8f4c441e032 513 case 2:
lixianyu 0:d8f4c441e032 514 x -= u8g->glyph_x;
lixianyu 0:d8f4c441e032 515 y += u8g->glyph_y;
lixianyu 0:d8f4c441e032 516 y++;
lixianyu 0:d8f4c441e032 517 if ( u8g_IsBBXIntersection(u8g, x-w-1, y, w, h) == 0 )
lixianyu 0:d8f4c441e032 518 return u8g->glyph_dx;
lixianyu 0:d8f4c441e032 519
lixianyu 0:d8f4c441e032 520 iy = y;
lixianyu 0:d8f4c441e032 521 iy += h;
lixianyu 0:d8f4c441e032 522 iy--;
lixianyu 0:d8f4c441e032 523 for( j = 0; j < h; j++ )
lixianyu 0:d8f4c441e032 524 {
lixianyu 0:d8f4c441e032 525 ix = x;
lixianyu 0:d8f4c441e032 526 for( i = 0; i < bytes_per_line; i++ )
lixianyu 0:d8f4c441e032 527 {
lixianyu 0:d8f4c441e032 528 u8g_Draw8Pixel(u8g, ix, iy, dir, u8g_pgm_read(data));
lixianyu 0:d8f4c441e032 529 data++;
lixianyu 0:d8f4c441e032 530 ix-=8;
lixianyu 0:d8f4c441e032 531 }
lixianyu 0:d8f4c441e032 532 iy--;
lixianyu 0:d8f4c441e032 533 }
lixianyu 0:d8f4c441e032 534 break;
lixianyu 0:d8f4c441e032 535 case 3:
lixianyu 0:d8f4c441e032 536 x -= u8g->glyph_y;
lixianyu 0:d8f4c441e032 537 x--;
lixianyu 0:d8f4c441e032 538 y -= u8g->glyph_x;
lixianyu 0:d8f4c441e032 539
lixianyu 0:d8f4c441e032 540 if ( u8g_IsBBXIntersection(u8g, x-h-1, y-w-1, h, w) == 0 )
lixianyu 0:d8f4c441e032 541 return u8g->glyph_dx;
lixianyu 0:d8f4c441e032 542
lixianyu 0:d8f4c441e032 543 ix = x;
lixianyu 0:d8f4c441e032 544 ix -= h;
lixianyu 0:d8f4c441e032 545 ix++;
lixianyu 0:d8f4c441e032 546
lixianyu 0:d8f4c441e032 547 for( j = 0; j < h; j++ )
lixianyu 0:d8f4c441e032 548 {
lixianyu 0:d8f4c441e032 549 iy = y;
lixianyu 0:d8f4c441e032 550 for( i = 0; i < bytes_per_line; i++ )
lixianyu 0:d8f4c441e032 551 {
lixianyu 0:d8f4c441e032 552 u8g_Draw8Pixel(u8g, ix, iy, dir, u8g_pgm_read(data));
lixianyu 0:d8f4c441e032 553 data++;
lixianyu 0:d8f4c441e032 554 iy-=8;
lixianyu 0:d8f4c441e032 555 }
lixianyu 0:d8f4c441e032 556 ix++;
lixianyu 0:d8f4c441e032 557 }
lixianyu 0:d8f4c441e032 558 break;
lixianyu 0:d8f4c441e032 559 }
lixianyu 0:d8f4c441e032 560 return u8g->glyph_dx;
lixianyu 0:d8f4c441e032 561 }
lixianyu 0:d8f4c441e032 562 #endif
lixianyu 0:d8f4c441e032 563
lixianyu 0:d8f4c441e032 564 int8_t u8g_draw_glyph(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, uint8_t encoding)
lixianyu 0:d8f4c441e032 565 {
lixianyu 0:d8f4c441e032 566 const u8g_pgm_uint8_t *data;
lixianyu 0:d8f4c441e032 567 uint8_t w, h;
lixianyu 0:d8f4c441e032 568 uint8_t i, j;
lixianyu 0:d8f4c441e032 569 u8g_uint_t ix, iy;
lixianyu 0:d8f4c441e032 570
lixianyu 0:d8f4c441e032 571 {
lixianyu 0:d8f4c441e032 572 u8g_glyph_t g = u8g_GetGlyph(u8g, encoding);
lixianyu 0:d8f4c441e032 573 if ( g == NULL )
lixianyu 0:d8f4c441e032 574 return 0;
lixianyu 0:d8f4c441e032 575 data = u8g_font_GetGlyphDataStart(u8g->font, g);
lixianyu 0:d8f4c441e032 576 }
lixianyu 0:d8f4c441e032 577
lixianyu 0:d8f4c441e032 578 w = u8g->glyph_width;
lixianyu 0:d8f4c441e032 579 h = u8g->glyph_height;
lixianyu 0:d8f4c441e032 580
lixianyu 0:d8f4c441e032 581 x += u8g->glyph_x;
lixianyu 0:d8f4c441e032 582 y -= u8g->glyph_y;
lixianyu 0:d8f4c441e032 583 y--;
lixianyu 0:d8f4c441e032 584
lixianyu 0:d8f4c441e032 585 if ( u8g_IsBBXIntersection(u8g, x, y-h+1, w, h) == 0 )
lixianyu 0:d8f4c441e032 586 return u8g->glyph_dx;
lixianyu 0:d8f4c441e032 587
lixianyu 0:d8f4c441e032 588 /* now, w is reused as bytes per line */
lixianyu 0:d8f4c441e032 589 w += 7;
lixianyu 0:d8f4c441e032 590 w /= 8;
lixianyu 0:d8f4c441e032 591
lixianyu 0:d8f4c441e032 592 iy = y;
lixianyu 0:d8f4c441e032 593 iy -= h;
lixianyu 0:d8f4c441e032 594 iy++;
lixianyu 0:d8f4c441e032 595
lixianyu 0:d8f4c441e032 596 for( j = 0; j < h; j++ )
lixianyu 0:d8f4c441e032 597 {
lixianyu 0:d8f4c441e032 598 ix = x;
lixianyu 0:d8f4c441e032 599 for( i = 0; i < w; i++ )
lixianyu 0:d8f4c441e032 600 {
lixianyu 0:d8f4c441e032 601 u8g_Draw8Pixel(u8g, ix, iy, 0, u8g_pgm_read(data));
lixianyu 0:d8f4c441e032 602 data++;
lixianyu 0:d8f4c441e032 603 ix+=8;
lixianyu 0:d8f4c441e032 604 }
lixianyu 0:d8f4c441e032 605 iy++;
lixianyu 0:d8f4c441e032 606 }
lixianyu 0:d8f4c441e032 607 return u8g->glyph_dx;
lixianyu 0:d8f4c441e032 608 }
lixianyu 0:d8f4c441e032 609
lixianyu 0:d8f4c441e032 610 int8_t u8g_DrawGlyph(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, uint8_t encoding)
lixianyu 0:d8f4c441e032 611 {
lixianyu 0:d8f4c441e032 612 y += u8g->font_calc_vref(u8g);
lixianyu 0:d8f4c441e032 613 return u8g_draw_glyph(u8g, x, y, encoding);
lixianyu 0:d8f4c441e032 614 }
lixianyu 0:d8f4c441e032 615
lixianyu 0:d8f4c441e032 616 int8_t u8g_draw_glyph90(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, uint8_t encoding)
lixianyu 0:d8f4c441e032 617 {
lixianyu 0:d8f4c441e032 618 const u8g_pgm_uint8_t *data;
lixianyu 0:d8f4c441e032 619 uint8_t w, h;
lixianyu 0:d8f4c441e032 620 uint8_t i, j;
lixianyu 0:d8f4c441e032 621 u8g_uint_t ix, iy;
lixianyu 0:d8f4c441e032 622
lixianyu 0:d8f4c441e032 623 {
lixianyu 0:d8f4c441e032 624 u8g_glyph_t g = u8g_GetGlyph(u8g, encoding);
lixianyu 0:d8f4c441e032 625 if ( g == NULL )
lixianyu 0:d8f4c441e032 626 return 0;
lixianyu 0:d8f4c441e032 627 data = u8g_font_GetGlyphDataStart(u8g->font, g);
lixianyu 0:d8f4c441e032 628 }
lixianyu 0:d8f4c441e032 629
lixianyu 0:d8f4c441e032 630 w = u8g->glyph_width;
lixianyu 0:d8f4c441e032 631 h = u8g->glyph_height;
lixianyu 0:d8f4c441e032 632
lixianyu 0:d8f4c441e032 633 x += u8g->glyph_y;
lixianyu 0:d8f4c441e032 634 x++;
lixianyu 0:d8f4c441e032 635 y += u8g->glyph_x;
lixianyu 0:d8f4c441e032 636
lixianyu 0:d8f4c441e032 637 if ( u8g_IsBBXIntersection(u8g, x, y, h, w) == 0 )
lixianyu 0:d8f4c441e032 638 return u8g->glyph_dx;
lixianyu 0:d8f4c441e032 639
lixianyu 0:d8f4c441e032 640 /* now, w is reused as bytes per line */
lixianyu 0:d8f4c441e032 641 w += 7;
lixianyu 0:d8f4c441e032 642 w /= 8;
lixianyu 0:d8f4c441e032 643
lixianyu 0:d8f4c441e032 644 ix = x;
lixianyu 0:d8f4c441e032 645 ix += h;
lixianyu 0:d8f4c441e032 646 ix--;
lixianyu 0:d8f4c441e032 647 for( j = 0; j < h; j++ )
lixianyu 0:d8f4c441e032 648 {
lixianyu 0:d8f4c441e032 649 iy = y;
lixianyu 0:d8f4c441e032 650 for( i = 0; i < w; i++ )
lixianyu 0:d8f4c441e032 651 {
lixianyu 0:d8f4c441e032 652 u8g_Draw8Pixel(u8g, ix, iy, 1, u8g_pgm_read(data));
lixianyu 0:d8f4c441e032 653 data++;
lixianyu 0:d8f4c441e032 654 iy+=8;
lixianyu 0:d8f4c441e032 655 }
lixianyu 0:d8f4c441e032 656 ix--;
lixianyu 0:d8f4c441e032 657 }
lixianyu 0:d8f4c441e032 658 return u8g->glyph_dx;
lixianyu 0:d8f4c441e032 659 }
lixianyu 0:d8f4c441e032 660
lixianyu 0:d8f4c441e032 661 int8_t u8g_DrawGlyph90(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, uint8_t encoding)
lixianyu 0:d8f4c441e032 662 {
lixianyu 0:d8f4c441e032 663 x -= u8g->font_calc_vref(u8g);
lixianyu 0:d8f4c441e032 664 return u8g_draw_glyph90(u8g, x, y, encoding);
lixianyu 0:d8f4c441e032 665 }
lixianyu 0:d8f4c441e032 666
lixianyu 0:d8f4c441e032 667
lixianyu 0:d8f4c441e032 668 int8_t u8g_draw_glyph180(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, uint8_t encoding)
lixianyu 0:d8f4c441e032 669 {
lixianyu 0:d8f4c441e032 670 const u8g_pgm_uint8_t *data;
lixianyu 0:d8f4c441e032 671 uint8_t w, h;
lixianyu 0:d8f4c441e032 672 uint8_t i, j;
lixianyu 0:d8f4c441e032 673 u8g_uint_t ix, iy;
lixianyu 0:d8f4c441e032 674
lixianyu 0:d8f4c441e032 675 {
lixianyu 0:d8f4c441e032 676 u8g_glyph_t g = u8g_GetGlyph(u8g, encoding);
lixianyu 0:d8f4c441e032 677 if ( g == NULL )
lixianyu 0:d8f4c441e032 678 return 0;
lixianyu 0:d8f4c441e032 679 data = u8g_font_GetGlyphDataStart(u8g->font, g);
lixianyu 0:d8f4c441e032 680 }
lixianyu 0:d8f4c441e032 681
lixianyu 0:d8f4c441e032 682 w = u8g->glyph_width;
lixianyu 0:d8f4c441e032 683 h = u8g->glyph_height;
lixianyu 0:d8f4c441e032 684
lixianyu 0:d8f4c441e032 685 x -= u8g->glyph_x;
lixianyu 0:d8f4c441e032 686 y += u8g->glyph_y;
lixianyu 0:d8f4c441e032 687 y++;
lixianyu 0:d8f4c441e032 688
lixianyu 0:d8f4c441e032 689 if ( u8g_IsBBXIntersection(u8g, x-(w-1), y, w, h) == 0 )
lixianyu 0:d8f4c441e032 690 return u8g->glyph_dx;
lixianyu 0:d8f4c441e032 691
lixianyu 0:d8f4c441e032 692 /* now, w is reused as bytes per line */
lixianyu 0:d8f4c441e032 693 w += 7;
lixianyu 0:d8f4c441e032 694 w /= 8;
lixianyu 0:d8f4c441e032 695
lixianyu 0:d8f4c441e032 696 iy = y;
lixianyu 0:d8f4c441e032 697 iy += h;
lixianyu 0:d8f4c441e032 698 iy--;
lixianyu 0:d8f4c441e032 699 for( j = 0; j < h; j++ )
lixianyu 0:d8f4c441e032 700 {
lixianyu 0:d8f4c441e032 701 ix = x;
lixianyu 0:d8f4c441e032 702 for( i = 0; i < w; i++ )
lixianyu 0:d8f4c441e032 703 {
lixianyu 0:d8f4c441e032 704 u8g_Draw8Pixel(u8g, ix, iy, 2, u8g_pgm_read(data));
lixianyu 0:d8f4c441e032 705 data++;
lixianyu 0:d8f4c441e032 706 ix-=8;
lixianyu 0:d8f4c441e032 707 }
lixianyu 0:d8f4c441e032 708 iy--;
lixianyu 0:d8f4c441e032 709 }
lixianyu 0:d8f4c441e032 710 return u8g->glyph_dx;
lixianyu 0:d8f4c441e032 711 }
lixianyu 0:d8f4c441e032 712
lixianyu 0:d8f4c441e032 713 int8_t u8g_DrawGlyph180(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, uint8_t encoding)
lixianyu 0:d8f4c441e032 714 {
lixianyu 0:d8f4c441e032 715 y -= u8g->font_calc_vref(u8g);
lixianyu 0:d8f4c441e032 716 return u8g_draw_glyph180(u8g, x, y, encoding);
lixianyu 0:d8f4c441e032 717 }
lixianyu 0:d8f4c441e032 718
lixianyu 0:d8f4c441e032 719
lixianyu 0:d8f4c441e032 720 int8_t u8g_draw_glyph270(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, uint8_t encoding)
lixianyu 0:d8f4c441e032 721 {
lixianyu 0:d8f4c441e032 722 const u8g_pgm_uint8_t *data;
lixianyu 0:d8f4c441e032 723 uint8_t w, h;
lixianyu 0:d8f4c441e032 724 uint8_t i, j;
lixianyu 0:d8f4c441e032 725 u8g_uint_t ix, iy;
lixianyu 0:d8f4c441e032 726
lixianyu 0:d8f4c441e032 727 {
lixianyu 0:d8f4c441e032 728 u8g_glyph_t g = u8g_GetGlyph(u8g, encoding);
lixianyu 0:d8f4c441e032 729 if ( g == NULL )
lixianyu 0:d8f4c441e032 730 return 0;
lixianyu 0:d8f4c441e032 731 data = u8g_font_GetGlyphDataStart(u8g->font, g);
lixianyu 0:d8f4c441e032 732 }
lixianyu 0:d8f4c441e032 733
lixianyu 0:d8f4c441e032 734 w = u8g->glyph_width;
lixianyu 0:d8f4c441e032 735 h = u8g->glyph_height;
lixianyu 0:d8f4c441e032 736
lixianyu 0:d8f4c441e032 737 x -= u8g->glyph_y;
lixianyu 0:d8f4c441e032 738 x--;
lixianyu 0:d8f4c441e032 739 y -= u8g->glyph_x;
lixianyu 0:d8f4c441e032 740
lixianyu 0:d8f4c441e032 741 if ( u8g_IsBBXIntersection(u8g, x-(h-1), y-(w-1), h, w) == 0 )
lixianyu 0:d8f4c441e032 742 return u8g->glyph_dx;
lixianyu 0:d8f4c441e032 743
lixianyu 0:d8f4c441e032 744
lixianyu 0:d8f4c441e032 745 /* now, w is reused as bytes per line */
lixianyu 0:d8f4c441e032 746 w += 7;
lixianyu 0:d8f4c441e032 747 w /= 8;
lixianyu 0:d8f4c441e032 748
lixianyu 0:d8f4c441e032 749 ix = x;
lixianyu 0:d8f4c441e032 750 ix -= h;
lixianyu 0:d8f4c441e032 751 ix++;
lixianyu 0:d8f4c441e032 752
lixianyu 0:d8f4c441e032 753 for( j = 0; j < h; j++ )
lixianyu 0:d8f4c441e032 754 {
lixianyu 0:d8f4c441e032 755 iy = y;
lixianyu 0:d8f4c441e032 756 for( i = 0; i < w; i++ )
lixianyu 0:d8f4c441e032 757 {
lixianyu 0:d8f4c441e032 758 u8g_Draw8Pixel(u8g, ix, iy, 3, u8g_pgm_read(data));
lixianyu 0:d8f4c441e032 759 data++;
lixianyu 0:d8f4c441e032 760 iy-=8;
lixianyu 0:d8f4c441e032 761 }
lixianyu 0:d8f4c441e032 762 ix++;
lixianyu 0:d8f4c441e032 763 }
lixianyu 0:d8f4c441e032 764 return u8g->glyph_dx;
lixianyu 0:d8f4c441e032 765 }
lixianyu 0:d8f4c441e032 766
lixianyu 0:d8f4c441e032 767 int8_t u8g_DrawGlyph270(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, uint8_t encoding)
lixianyu 0:d8f4c441e032 768 {
lixianyu 0:d8f4c441e032 769 x += u8g->font_calc_vref(u8g);
lixianyu 0:d8f4c441e032 770 return u8g_draw_glyph270(u8g, x, y, encoding);
lixianyu 0:d8f4c441e032 771 }
lixianyu 0:d8f4c441e032 772
lixianyu 0:d8f4c441e032 773
lixianyu 0:d8f4c441e032 774
lixianyu 0:d8f4c441e032 775 #ifdef OBSOLETE
lixianyu 0:d8f4c441e032 776 /*
lixianyu 0:d8f4c441e032 777 Draw a glyph
lixianyu 0:d8f4c441e032 778 x,y: lower left corner of the font bounding box
lixianyu 0:d8f4c441e032 779 */
lixianyu 0:d8f4c441e032 780 int8_t u8g_DrawGlyphFontBBX(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, uint8_t dir, uint8_t encoding)
lixianyu 0:d8f4c441e032 781 {
lixianyu 0:d8f4c441e032 782 /* TODO: apply "dir" */
lixianyu 0:d8f4c441e032 783 x -= u8g_GetFontBBXOffX(u8g);
lixianyu 0:d8f4c441e032 784 y += u8g_GetFontBBXOffY(u8g);
lixianyu 0:d8f4c441e032 785 return u8g_DrawGlyphDir(u8g, x, y, dir, encoding);
lixianyu 0:d8f4c441e032 786 }
lixianyu 0:d8f4c441e032 787 #endif
lixianyu 0:d8f4c441e032 788
lixianyu 0:d8f4c441e032 789 /*========================================================================*/
lixianyu 0:d8f4c441e032 790 /* string drawing procedures */
lixianyu 0:d8f4c441e032 791
lixianyu 0:d8f4c441e032 792
lixianyu 0:d8f4c441e032 793 u8g_uint_t u8g_DrawStr(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, const char *s)
lixianyu 0:d8f4c441e032 794 {
lixianyu 0:d8f4c441e032 795 u8g_uint_t t = 0;
lixianyu 0:d8f4c441e032 796 int8_t d;
lixianyu 0:d8f4c441e032 797
lixianyu 0:d8f4c441e032 798 //u8g_uint_t u8g_GetStrWidth(u8g, s);
lixianyu 0:d8f4c441e032 799 //u8g_font_GetFontAscent(u8g->font)-u8g_font_GetFontDescent(u8g->font);
lixianyu 0:d8f4c441e032 800
lixianyu 0:d8f4c441e032 801 y += u8g->font_calc_vref(u8g);
lixianyu 0:d8f4c441e032 802
lixianyu 0:d8f4c441e032 803 while( *s != '\0' )
lixianyu 0:d8f4c441e032 804 {
lixianyu 0:d8f4c441e032 805 d = u8g_draw_glyph(u8g, x, y, *s);
lixianyu 0:d8f4c441e032 806 x += d;
lixianyu 0:d8f4c441e032 807 t += d;
lixianyu 0:d8f4c441e032 808 s++;
lixianyu 0:d8f4c441e032 809 }
lixianyu 0:d8f4c441e032 810 return t;
lixianyu 0:d8f4c441e032 811 }
lixianyu 0:d8f4c441e032 812
lixianyu 0:d8f4c441e032 813 u8g_uint_t u8g_DrawStr90(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, const char *s)
lixianyu 0:d8f4c441e032 814 {
lixianyu 0:d8f4c441e032 815 u8g_uint_t t = 0;
lixianyu 0:d8f4c441e032 816 int8_t d;
lixianyu 0:d8f4c441e032 817
lixianyu 0:d8f4c441e032 818 x -= u8g->font_calc_vref(u8g);
lixianyu 0:d8f4c441e032 819
lixianyu 0:d8f4c441e032 820 while( *s != '\0' )
lixianyu 0:d8f4c441e032 821 {
lixianyu 0:d8f4c441e032 822 d = u8g_draw_glyph90(u8g, x, y, *s);
lixianyu 0:d8f4c441e032 823 y += d;
lixianyu 0:d8f4c441e032 824 t += d;
lixianyu 0:d8f4c441e032 825 s++;
lixianyu 0:d8f4c441e032 826 }
lixianyu 0:d8f4c441e032 827 return t;
lixianyu 0:d8f4c441e032 828 }
lixianyu 0:d8f4c441e032 829
lixianyu 0:d8f4c441e032 830 u8g_uint_t u8g_DrawStr180(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, const char *s)
lixianyu 0:d8f4c441e032 831 {
lixianyu 0:d8f4c441e032 832 u8g_uint_t t = 0;
lixianyu 0:d8f4c441e032 833 int8_t d;
lixianyu 0:d8f4c441e032 834
lixianyu 0:d8f4c441e032 835 y -= u8g->font_calc_vref(u8g);
lixianyu 0:d8f4c441e032 836
lixianyu 0:d8f4c441e032 837 while( *s != '\0' )
lixianyu 0:d8f4c441e032 838 {
lixianyu 0:d8f4c441e032 839 d = u8g_draw_glyph180(u8g, x, y, *s);
lixianyu 0:d8f4c441e032 840 x -= d;
lixianyu 0:d8f4c441e032 841 t += d;
lixianyu 0:d8f4c441e032 842 s++;
lixianyu 0:d8f4c441e032 843 }
lixianyu 0:d8f4c441e032 844 return t;
lixianyu 0:d8f4c441e032 845 }
lixianyu 0:d8f4c441e032 846
lixianyu 0:d8f4c441e032 847 u8g_uint_t u8g_DrawStr270(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, const char *s)
lixianyu 0:d8f4c441e032 848 {
lixianyu 0:d8f4c441e032 849 u8g_uint_t t = 0;
lixianyu 0:d8f4c441e032 850 int8_t d;
lixianyu 0:d8f4c441e032 851
lixianyu 0:d8f4c441e032 852 x += u8g->font_calc_vref(u8g);
lixianyu 0:d8f4c441e032 853
lixianyu 0:d8f4c441e032 854 while( *s != '\0' )
lixianyu 0:d8f4c441e032 855 {
lixianyu 0:d8f4c441e032 856 d = u8g_draw_glyph270(u8g, x, y, *s);
lixianyu 0:d8f4c441e032 857 y -= d;
lixianyu 0:d8f4c441e032 858 t += d;
lixianyu 0:d8f4c441e032 859 s++;
lixianyu 0:d8f4c441e032 860 }
lixianyu 0:d8f4c441e032 861 return t;
lixianyu 0:d8f4c441e032 862 }
lixianyu 0:d8f4c441e032 863
lixianyu 0:d8f4c441e032 864 u8g_uint_t u8g_DrawStrDir(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, uint8_t dir, const char *s)
lixianyu 0:d8f4c441e032 865 {
lixianyu 0:d8f4c441e032 866 switch(dir)
lixianyu 0:d8f4c441e032 867 {
lixianyu 0:d8f4c441e032 868 case 0:
lixianyu 0:d8f4c441e032 869 return u8g_DrawStr(u8g, x, y, s);
lixianyu 0:d8f4c441e032 870 case 1:
lixianyu 0:d8f4c441e032 871 return u8g_DrawStr90(u8g, x, y, s);
lixianyu 0:d8f4c441e032 872 case 2:
lixianyu 0:d8f4c441e032 873 return u8g_DrawStr180(u8g, x, y, s);
lixianyu 0:d8f4c441e032 874 case 3:
lixianyu 0:d8f4c441e032 875 return u8g_DrawStr270(u8g, x, y, s);
lixianyu 0:d8f4c441e032 876 }
lixianyu 0:d8f4c441e032 877 return 0;
lixianyu 0:d8f4c441e032 878 }
lixianyu 0:d8f4c441e032 879
lixianyu 0:d8f4c441e032 880 u8g_uint_t u8g_DrawStrP(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, const u8g_pgm_uint8_t *s)
lixianyu 0:d8f4c441e032 881 {
lixianyu 0:d8f4c441e032 882 u8g_uint_t t = 0;
lixianyu 0:d8f4c441e032 883 int8_t d;
lixianyu 0:d8f4c441e032 884 uint8_t c;
lixianyu 0:d8f4c441e032 885
lixianyu 0:d8f4c441e032 886 y += u8g->font_calc_vref(u8g);
lixianyu 0:d8f4c441e032 887
lixianyu 0:d8f4c441e032 888 for(;;)
lixianyu 0:d8f4c441e032 889 {
lixianyu 0:d8f4c441e032 890 c = u8g_pgm_read(s);
lixianyu 0:d8f4c441e032 891 if ( c == '\0' )
lixianyu 0:d8f4c441e032 892 break;
lixianyu 0:d8f4c441e032 893 d = u8g_draw_glyph(u8g, x, y, c);
lixianyu 0:d8f4c441e032 894 x += d;
lixianyu 0:d8f4c441e032 895 t += d;
lixianyu 0:d8f4c441e032 896 s++;
lixianyu 0:d8f4c441e032 897 }
lixianyu 0:d8f4c441e032 898 return t;
lixianyu 0:d8f4c441e032 899 }
lixianyu 0:d8f4c441e032 900
lixianyu 0:d8f4c441e032 901 u8g_uint_t u8g_DrawStr90P(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, const u8g_pgm_uint8_t *s)
lixianyu 0:d8f4c441e032 902 {
lixianyu 0:d8f4c441e032 903 u8g_uint_t t = 0;
lixianyu 0:d8f4c441e032 904 int8_t d;
lixianyu 0:d8f4c441e032 905 uint8_t c;
lixianyu 0:d8f4c441e032 906
lixianyu 0:d8f4c441e032 907 x -= u8g->font_calc_vref(u8g);
lixianyu 0:d8f4c441e032 908
lixianyu 0:d8f4c441e032 909 for(;;)
lixianyu 0:d8f4c441e032 910 {
lixianyu 0:d8f4c441e032 911 c = u8g_pgm_read(s);
lixianyu 0:d8f4c441e032 912 if ( c == '\0' )
lixianyu 0:d8f4c441e032 913 break;
lixianyu 0:d8f4c441e032 914 d = u8g_DrawGlyph90(u8g, x, y, c);
lixianyu 0:d8f4c441e032 915 y += d;
lixianyu 0:d8f4c441e032 916 t += d;
lixianyu 0:d8f4c441e032 917 s++;
lixianyu 0:d8f4c441e032 918 }
lixianyu 0:d8f4c441e032 919 return t;
lixianyu 0:d8f4c441e032 920 }
lixianyu 0:d8f4c441e032 921
lixianyu 0:d8f4c441e032 922 u8g_uint_t u8g_DrawStr180P(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, const u8g_pgm_uint8_t *s)
lixianyu 0:d8f4c441e032 923 {
lixianyu 0:d8f4c441e032 924 u8g_uint_t t = 0;
lixianyu 0:d8f4c441e032 925 int8_t d;
lixianyu 0:d8f4c441e032 926 uint8_t c;
lixianyu 0:d8f4c441e032 927
lixianyu 0:d8f4c441e032 928 y -= u8g->font_calc_vref(u8g);
lixianyu 0:d8f4c441e032 929
lixianyu 0:d8f4c441e032 930 for(;;)
lixianyu 0:d8f4c441e032 931 {
lixianyu 0:d8f4c441e032 932 c = u8g_pgm_read(s);
lixianyu 0:d8f4c441e032 933 if ( c == '\0' )
lixianyu 0:d8f4c441e032 934 break;
lixianyu 0:d8f4c441e032 935 d = u8g_DrawGlyph180(u8g, x, y, c);
lixianyu 0:d8f4c441e032 936 x -= d;
lixianyu 0:d8f4c441e032 937 t += d;
lixianyu 0:d8f4c441e032 938 s++;
lixianyu 0:d8f4c441e032 939 }
lixianyu 0:d8f4c441e032 940 return t;
lixianyu 0:d8f4c441e032 941 }
lixianyu 0:d8f4c441e032 942
lixianyu 0:d8f4c441e032 943 u8g_uint_t u8g_DrawStr270P(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, const u8g_pgm_uint8_t *s)
lixianyu 0:d8f4c441e032 944 {
lixianyu 0:d8f4c441e032 945 u8g_uint_t t = 0;
lixianyu 0:d8f4c441e032 946 int8_t d;
lixianyu 0:d8f4c441e032 947 uint8_t c;
lixianyu 0:d8f4c441e032 948
lixianyu 0:d8f4c441e032 949 x += u8g->font_calc_vref(u8g);
lixianyu 0:d8f4c441e032 950
lixianyu 0:d8f4c441e032 951 for(;;)
lixianyu 0:d8f4c441e032 952 {
lixianyu 0:d8f4c441e032 953 c = u8g_pgm_read(s);
lixianyu 0:d8f4c441e032 954 if ( c == '\0' )
lixianyu 0:d8f4c441e032 955 break;
lixianyu 0:d8f4c441e032 956 d = u8g_DrawGlyph270(u8g, x, y, c);
lixianyu 0:d8f4c441e032 957 y -= d;
lixianyu 0:d8f4c441e032 958 t += d;
lixianyu 0:d8f4c441e032 959 s++;
lixianyu 0:d8f4c441e032 960 }
lixianyu 0:d8f4c441e032 961 return t;
lixianyu 0:d8f4c441e032 962 }
lixianyu 0:d8f4c441e032 963
lixianyu 0:d8f4c441e032 964 u8g_uint_t u8g_DrawStrFontBBX(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, uint8_t dir, const char *s)
lixianyu 0:d8f4c441e032 965 {
lixianyu 0:d8f4c441e032 966 x -= u8g_GetFontBBXOffX(u8g);
lixianyu 0:d8f4c441e032 967 y += u8g_GetFontBBXOffY(u8g);
lixianyu 0:d8f4c441e032 968 return u8g_DrawStrDir(u8g, x, y, dir, s);
lixianyu 0:d8f4c441e032 969 }
lixianyu 0:d8f4c441e032 970
lixianyu 0:d8f4c441e032 971 /* still used by picgen.c, dir argument is ignored */
lixianyu 0:d8f4c441e032 972 int8_t u8g_DrawGlyphFontBBX(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, uint8_t dir, uint8_t encoding)
lixianyu 0:d8f4c441e032 973 {
lixianyu 0:d8f4c441e032 974 x -= u8g_GetFontBBXOffX(u8g);
lixianyu 0:d8f4c441e032 975 y += u8g_GetFontBBXOffY(u8g);
lixianyu 0:d8f4c441e032 976 u8g_draw_glyph(u8g, x, y, encoding);
lixianyu 0:d8f4c441e032 977 return 0;
lixianyu 0:d8f4c441e032 978 }
lixianyu 0:d8f4c441e032 979
lixianyu 0:d8f4c441e032 980
lixianyu 0:d8f4c441e032 981 /*========================================================================*/
lixianyu 0:d8f4c441e032 982 /* set ascent/descent for reference point calculation */
lixianyu 0:d8f4c441e032 983
lixianyu 0:d8f4c441e032 984 void u8g_UpdateRefHeight(u8g_t *u8g)
lixianyu 0:d8f4c441e032 985 {
lixianyu 0:d8f4c441e032 986 uint16_t ls;
lixianyu 0:d8f4c441e032 987 if ( u8g->font == NULL )
lixianyu 0:d8f4c441e032 988 return;
lixianyu 0:d8f4c441e032 989 if ( u8g->font_height_mode == U8G_FONT_HEIGHT_MODE_TEXT )
lixianyu 0:d8f4c441e032 990 {
lixianyu 0:d8f4c441e032 991 u8g->font_ref_ascent = u8g_font_GetCapitalAHeight(u8g->font);
lixianyu 0:d8f4c441e032 992 u8g->font_ref_descent = u8g_font_GetLowerGDescent(u8g->font);
lixianyu 0:d8f4c441e032 993 }
lixianyu 0:d8f4c441e032 994 else if ( u8g->font_height_mode == U8G_FONT_HEIGHT_MODE_XTEXT )
lixianyu 0:d8f4c441e032 995 {
lixianyu 0:d8f4c441e032 996 u8g->font_ref_ascent = u8g_font_GetFontXAscent(u8g->font);
lixianyu 0:d8f4c441e032 997 u8g->font_ref_descent = u8g_font_GetFontXDescent(u8g->font);
lixianyu 0:d8f4c441e032 998 }
lixianyu 0:d8f4c441e032 999 else
lixianyu 0:d8f4c441e032 1000 {
lixianyu 0:d8f4c441e032 1001 u8g->font_ref_ascent = u8g_font_GetFontAscent(u8g->font);
lixianyu 0:d8f4c441e032 1002 u8g->font_ref_descent = u8g_font_GetFontDescent(u8g->font);
lixianyu 0:d8f4c441e032 1003 }
lixianyu 0:d8f4c441e032 1004
lixianyu 0:d8f4c441e032 1005 ls = u8g->font_ref_ascent - u8g->font_ref_descent;
lixianyu 0:d8f4c441e032 1006 if ( u8g->font_line_spacing_factor != 64 )
lixianyu 0:d8f4c441e032 1007 {
lixianyu 0:d8f4c441e032 1008 ls &= 255;
lixianyu 0:d8f4c441e032 1009 ls *= u8g->font_line_spacing_factor;
lixianyu 0:d8f4c441e032 1010 ls >>= 6;
lixianyu 0:d8f4c441e032 1011 }
lixianyu 0:d8f4c441e032 1012 u8g->line_spacing = ls;
lixianyu 0:d8f4c441e032 1013 }
lixianyu 0:d8f4c441e032 1014
lixianyu 0:d8f4c441e032 1015 void u8g_SetFontRefHeightText(u8g_t *u8g)
lixianyu 0:d8f4c441e032 1016 {
lixianyu 0:d8f4c441e032 1017 u8g->font_height_mode = U8G_FONT_HEIGHT_MODE_TEXT;
lixianyu 0:d8f4c441e032 1018 u8g_UpdateRefHeight(u8g);
lixianyu 0:d8f4c441e032 1019 }
lixianyu 0:d8f4c441e032 1020
lixianyu 0:d8f4c441e032 1021 void u8g_SetFontRefHeightExtendedText(u8g_t *u8g)
lixianyu 0:d8f4c441e032 1022 {
lixianyu 0:d8f4c441e032 1023 u8g->font_height_mode = U8G_FONT_HEIGHT_MODE_XTEXT;
lixianyu 0:d8f4c441e032 1024 u8g_UpdateRefHeight(u8g);
lixianyu 0:d8f4c441e032 1025 }
lixianyu 0:d8f4c441e032 1026
lixianyu 0:d8f4c441e032 1027
lixianyu 0:d8f4c441e032 1028 void u8g_SetFontRefHeightAll(u8g_t *u8g)
lixianyu 0:d8f4c441e032 1029 {
lixianyu 0:d8f4c441e032 1030 u8g->font_height_mode = U8G_FONT_HEIGHT_MODE_ALL;
lixianyu 0:d8f4c441e032 1031 u8g_UpdateRefHeight(u8g);
lixianyu 0:d8f4c441e032 1032 }
lixianyu 0:d8f4c441e032 1033
lixianyu 0:d8f4c441e032 1034 /* factor = 64: linespaceing == ascent and descent */
lixianyu 0:d8f4c441e032 1035 void u8g_SetFontLineSpacingFactor(u8g_t *u8g, uint8_t factor)
lixianyu 0:d8f4c441e032 1036 {
lixianyu 0:d8f4c441e032 1037 u8g->font_line_spacing_factor = factor;
lixianyu 0:d8f4c441e032 1038 u8g_UpdateRefHeight(u8g);
lixianyu 0:d8f4c441e032 1039 }
lixianyu 0:d8f4c441e032 1040
lixianyu 0:d8f4c441e032 1041
lixianyu 0:d8f4c441e032 1042
lixianyu 0:d8f4c441e032 1043 /*========================================================================*/
lixianyu 0:d8f4c441e032 1044 /* callback procedures to correct the y position */
lixianyu 0:d8f4c441e032 1045
lixianyu 0:d8f4c441e032 1046 u8g_uint_t u8g_font_calc_vref_font(u8g_t *u8g)
lixianyu 0:d8f4c441e032 1047 {
lixianyu 0:d8f4c441e032 1048 return 0;
lixianyu 0:d8f4c441e032 1049 }
lixianyu 0:d8f4c441e032 1050
lixianyu 0:d8f4c441e032 1051 void u8g_SetFontPosBaseline(u8g_t *u8g)
lixianyu 0:d8f4c441e032 1052 {
lixianyu 0:d8f4c441e032 1053 u8g->font_calc_vref = u8g_font_calc_vref_font;
lixianyu 0:d8f4c441e032 1054 }
lixianyu 0:d8f4c441e032 1055
lixianyu 0:d8f4c441e032 1056
lixianyu 0:d8f4c441e032 1057 u8g_uint_t u8g_font_calc_vref_bottom(u8g_t *u8g)
lixianyu 0:d8f4c441e032 1058 {
lixianyu 0:d8f4c441e032 1059 /* y += (u8g_uint_t)(u8g_int_t)(u8g->font_ref_descent); */
lixianyu 0:d8f4c441e032 1060 return (u8g_uint_t)(u8g_int_t)(u8g->font_ref_descent);
lixianyu 0:d8f4c441e032 1061 }
lixianyu 0:d8f4c441e032 1062
lixianyu 0:d8f4c441e032 1063 void u8g_SetFontPosBottom(u8g_t *u8g)
lixianyu 0:d8f4c441e032 1064 {
lixianyu 0:d8f4c441e032 1065 u8g->font_calc_vref = u8g_font_calc_vref_bottom;
lixianyu 0:d8f4c441e032 1066 }
lixianyu 0:d8f4c441e032 1067
lixianyu 0:d8f4c441e032 1068 u8g_uint_t u8g_font_calc_vref_top(u8g_t *u8g)
lixianyu 0:d8f4c441e032 1069 {
lixianyu 0:d8f4c441e032 1070 u8g_uint_t tmp;
lixianyu 0:d8f4c441e032 1071 /* reference pos is one pixel above the upper edge of the reference glyph */
lixianyu 0:d8f4c441e032 1072
lixianyu 0:d8f4c441e032 1073 /*
lixianyu 0:d8f4c441e032 1074 y += (u8g_uint_t)(u8g_int_t)(u8g->font_ref_ascent);
lixianyu 0:d8f4c441e032 1075 y++;
lixianyu 0:d8f4c441e032 1076 */
lixianyu 0:d8f4c441e032 1077 tmp = (u8g_uint_t)(u8g_int_t)(u8g->font_ref_ascent);
lixianyu 0:d8f4c441e032 1078 tmp++;
lixianyu 0:d8f4c441e032 1079 return tmp;
lixianyu 0:d8f4c441e032 1080 }
lixianyu 0:d8f4c441e032 1081
lixianyu 0:d8f4c441e032 1082 void u8g_SetFontPosTop(u8g_t *u8g)
lixianyu 0:d8f4c441e032 1083 {
lixianyu 0:d8f4c441e032 1084 u8g->font_calc_vref = u8g_font_calc_vref_top;
lixianyu 0:d8f4c441e032 1085 }
lixianyu 0:d8f4c441e032 1086
lixianyu 0:d8f4c441e032 1087 u8g_uint_t u8g_font_calc_vref_center(u8g_t *u8g)
lixianyu 0:d8f4c441e032 1088 {
lixianyu 0:d8f4c441e032 1089 int8_t tmp;
lixianyu 0:d8f4c441e032 1090 tmp = u8g->font_ref_ascent;
lixianyu 0:d8f4c441e032 1091 tmp -= u8g->font_ref_descent;
lixianyu 0:d8f4c441e032 1092 tmp /= 2;
lixianyu 0:d8f4c441e032 1093 tmp += u8g->font_ref_descent;
lixianyu 0:d8f4c441e032 1094 /* y += (u8g_uint_t)(u8g_int_t)(tmp); */
lixianyu 0:d8f4c441e032 1095 return tmp;
lixianyu 0:d8f4c441e032 1096 }
lixianyu 0:d8f4c441e032 1097
lixianyu 0:d8f4c441e032 1098 void u8g_SetFontPosCenter(u8g_t *u8g)
lixianyu 0:d8f4c441e032 1099 {
lixianyu 0:d8f4c441e032 1100 u8g->font_calc_vref = u8g_font_calc_vref_center;
lixianyu 0:d8f4c441e032 1101 }
lixianyu 0:d8f4c441e032 1102
lixianyu 0:d8f4c441e032 1103 /*========================================================================*/
lixianyu 0:d8f4c441e032 1104 /* string pixel width calculation */
lixianyu 0:d8f4c441e032 1105
lixianyu 0:d8f4c441e032 1106 char u8g_font_get_char(const void *s)
lixianyu 0:d8f4c441e032 1107 {
lixianyu 0:d8f4c441e032 1108 return *(const char *)(s);
lixianyu 0:d8f4c441e032 1109 }
lixianyu 0:d8f4c441e032 1110
lixianyu 0:d8f4c441e032 1111 char u8g_font_get_charP(const void *s)
lixianyu 0:d8f4c441e032 1112 {
lixianyu 0:d8f4c441e032 1113 return u8g_pgm_read(s);
lixianyu 0:d8f4c441e032 1114 }
lixianyu 0:d8f4c441e032 1115
lixianyu 0:d8f4c441e032 1116 typedef char (*u8g_font_get_char_fn)(const void *s);
lixianyu 0:d8f4c441e032 1117
lixianyu 0:d8f4c441e032 1118
lixianyu 0:d8f4c441e032 1119 u8g_uint_t u8g_font_calc_str_pixel_width(u8g_t *u8g, const char *s, u8g_font_get_char_fn get_char )
lixianyu 0:d8f4c441e032 1120 {
lixianyu 0:d8f4c441e032 1121 u8g_uint_t w;
lixianyu 0:d8f4c441e032 1122 uint8_t enc;
lixianyu 0:d8f4c441e032 1123
lixianyu 0:d8f4c441e032 1124 /* reset the total minimal width to zero, this will be expanded during calculation */
lixianyu 0:d8f4c441e032 1125 w = 0;
lixianyu 0:d8f4c441e032 1126
lixianyu 0:d8f4c441e032 1127 enc = get_char(s);
lixianyu 0:d8f4c441e032 1128
lixianyu 0:d8f4c441e032 1129 /* check for empty string, width is already 0 */
lixianyu 0:d8f4c441e032 1130 if ( enc == '\0' )
lixianyu 0:d8f4c441e032 1131 {
lixianyu 0:d8f4c441e032 1132 return w;
lixianyu 0:d8f4c441e032 1133 }
lixianyu 0:d8f4c441e032 1134
lixianyu 0:d8f4c441e032 1135 /* get the glyph information of the first char. This must be valid, because we already checked for the empty string */
lixianyu 0:d8f4c441e032 1136 /* if *s is not inside the font, then the cached parameters of the glyph are all zero */
lixianyu 0:d8f4c441e032 1137 u8g_GetGlyph(u8g, enc);
lixianyu 0:d8f4c441e032 1138
lixianyu 0:d8f4c441e032 1139 /* strlen(s) == 1: width = width(s[0]) */
lixianyu 0:d8f4c441e032 1140 /* strlen(s) == 2: width = - offx(s[0]) + deltax(s[0]) + offx(s[1]) + width(s[1]) */
lixianyu 0:d8f4c441e032 1141 /* strlen(s) == 3: width = - offx(s[0]) + deltax(s[0]) + deltax(s[1]) + offx(s[2]) + width(s[2]) */
lixianyu 0:d8f4c441e032 1142
lixianyu 0:d8f4c441e032 1143 /* assume that the string has size 2 or more, than start with negative offset-x */
lixianyu 0:d8f4c441e032 1144 /* for string with size 1, this will be nullified after the loop */
lixianyu 0:d8f4c441e032 1145 w = -u8g->glyph_x;
lixianyu 0:d8f4c441e032 1146 for(;;)
lixianyu 0:d8f4c441e032 1147 {
lixianyu 0:d8f4c441e032 1148
lixianyu 0:d8f4c441e032 1149 /* check and stop if the end of the string is reached */
lixianyu 0:d8f4c441e032 1150 s++;
lixianyu 0:d8f4c441e032 1151 if ( get_char(s) == '\0' )
lixianyu 0:d8f4c441e032 1152 break;
lixianyu 0:d8f4c441e032 1153
lixianyu 0:d8f4c441e032 1154 /* if there are still more characters, add the delta to the next glyph */
lixianyu 0:d8f4c441e032 1155 w += u8g->glyph_dx;
lixianyu 0:d8f4c441e032 1156
lixianyu 0:d8f4c441e032 1157 /* store the encoding in a local variable, used also after the for(;;) loop */
lixianyu 0:d8f4c441e032 1158 enc = get_char(s);
lixianyu 0:d8f4c441e032 1159
lixianyu 0:d8f4c441e032 1160 /* load the next glyph information */
lixianyu 0:d8f4c441e032 1161 u8g_GetGlyph(u8g, enc);
lixianyu 0:d8f4c441e032 1162 }
lixianyu 0:d8f4c441e032 1163
lixianyu 0:d8f4c441e032 1164 /* finally calculate the width of the last char */
lixianyu 0:d8f4c441e032 1165 /* here is another exception, if the last char is a black, use the dx value instead */
lixianyu 0:d8f4c441e032 1166 if ( enc != ' ' )
lixianyu 0:d8f4c441e032 1167 {
lixianyu 0:d8f4c441e032 1168 /* if g was not updated in the for loop (strlen() == 1), then the initial offset x gets removed */
lixianyu 0:d8f4c441e032 1169 w += u8g->glyph_width;
lixianyu 0:d8f4c441e032 1170 w += u8g->glyph_x;
lixianyu 0:d8f4c441e032 1171 }
lixianyu 0:d8f4c441e032 1172 else
lixianyu 0:d8f4c441e032 1173 {
lixianyu 0:d8f4c441e032 1174 w += u8g->glyph_dx;
lixianyu 0:d8f4c441e032 1175 }
lixianyu 0:d8f4c441e032 1176
lixianyu 0:d8f4c441e032 1177
lixianyu 0:d8f4c441e032 1178 return w;
lixianyu 0:d8f4c441e032 1179 }
lixianyu 0:d8f4c441e032 1180
lixianyu 0:d8f4c441e032 1181 u8g_uint_t u8g_GetStrPixelWidth(u8g_t *u8g, const char *s)
lixianyu 0:d8f4c441e032 1182 {
lixianyu 0:d8f4c441e032 1183 return u8g_font_calc_str_pixel_width(u8g, s, u8g_font_get_char);
lixianyu 0:d8f4c441e032 1184 }
lixianyu 0:d8f4c441e032 1185
lixianyu 0:d8f4c441e032 1186 u8g_uint_t u8g_GetStrPixelWidthP(u8g_t *u8g, const u8g_pgm_uint8_t *s)
lixianyu 0:d8f4c441e032 1187 {
lixianyu 0:d8f4c441e032 1188 return u8g_font_calc_str_pixel_width(u8g, (const char *)s, u8g_font_get_charP);
lixianyu 0:d8f4c441e032 1189 }
lixianyu 0:d8f4c441e032 1190
lixianyu 0:d8f4c441e032 1191 int8_t u8g_GetStrX(u8g_t *u8g, const char *s)
lixianyu 0:d8f4c441e032 1192 {
lixianyu 0:d8f4c441e032 1193 u8g_GetGlyph(u8g, *s);
lixianyu 0:d8f4c441e032 1194 return u8g->glyph_x;
lixianyu 0:d8f4c441e032 1195 }
lixianyu 0:d8f4c441e032 1196
lixianyu 0:d8f4c441e032 1197 int8_t u8g_GetStrXP(u8g_t *u8g, const u8g_pgm_uint8_t *s)
lixianyu 0:d8f4c441e032 1198 {
lixianyu 0:d8f4c441e032 1199 u8g_GetGlyph(u8g, u8g_pgm_read(s));
lixianyu 0:d8f4c441e032 1200 return u8g->glyph_x;
lixianyu 0:d8f4c441e032 1201 }
lixianyu 0:d8f4c441e032 1202
lixianyu 0:d8f4c441e032 1203 /*========================================================================*/
lixianyu 0:d8f4c441e032 1204 /* string width calculation */
lixianyu 0:d8f4c441e032 1205
lixianyu 0:d8f4c441e032 1206 u8g_uint_t u8g_GetStrWidth(u8g_t *u8g, const char *s)
lixianyu 0:d8f4c441e032 1207 {
lixianyu 0:d8f4c441e032 1208 u8g_uint_t w;
lixianyu 0:d8f4c441e032 1209 uint8_t encoding;
lixianyu 0:d8f4c441e032 1210
lixianyu 0:d8f4c441e032 1211 /* reset the total width to zero, this will be expanded during calculation */
lixianyu 0:d8f4c441e032 1212 w = 0;
lixianyu 0:d8f4c441e032 1213
lixianyu 0:d8f4c441e032 1214 for(;;)
lixianyu 0:d8f4c441e032 1215 {
lixianyu 0:d8f4c441e032 1216 encoding = *s;
lixianyu 0:d8f4c441e032 1217 if ( encoding == 0 )
lixianyu 0:d8f4c441e032 1218 break;
lixianyu 0:d8f4c441e032 1219
lixianyu 0:d8f4c441e032 1220 /* load glyph information */
lixianyu 0:d8f4c441e032 1221 u8g_GetGlyph(u8g, encoding);
lixianyu 0:d8f4c441e032 1222 w += u8g->glyph_dx;
lixianyu 0:d8f4c441e032 1223
lixianyu 0:d8f4c441e032 1224 /* goto next char */
lixianyu 0:d8f4c441e032 1225 s++;
lixianyu 0:d8f4c441e032 1226 }
lixianyu 0:d8f4c441e032 1227
lixianyu 0:d8f4c441e032 1228 return w;
lixianyu 0:d8f4c441e032 1229 }
lixianyu 0:d8f4c441e032 1230
lixianyu 0:d8f4c441e032 1231
lixianyu 0:d8f4c441e032 1232 u8g_uint_t u8g_GetStrWidthP(u8g_t *u8g, const u8g_pgm_uint8_t *s)
lixianyu 0:d8f4c441e032 1233 {
lixianyu 0:d8f4c441e032 1234 u8g_uint_t w;
lixianyu 0:d8f4c441e032 1235 uint8_t encoding;
lixianyu 0:d8f4c441e032 1236
lixianyu 0:d8f4c441e032 1237 /* reset the total width to zero, this will be expanded during calculation */
lixianyu 0:d8f4c441e032 1238 w = 0;
lixianyu 0:d8f4c441e032 1239
lixianyu 0:d8f4c441e032 1240 for(;;)
lixianyu 0:d8f4c441e032 1241 {
lixianyu 0:d8f4c441e032 1242 encoding = u8g_pgm_read(s);
lixianyu 0:d8f4c441e032 1243 if ( encoding == 0 )
lixianyu 0:d8f4c441e032 1244 break;
lixianyu 0:d8f4c441e032 1245
lixianyu 0:d8f4c441e032 1246 /* load glyph information */
lixianyu 0:d8f4c441e032 1247 u8g_GetGlyph(u8g, encoding);
lixianyu 0:d8f4c441e032 1248 w += u8g->glyph_dx;
lixianyu 0:d8f4c441e032 1249
lixianyu 0:d8f4c441e032 1250 /* goto next char */
lixianyu 0:d8f4c441e032 1251 s++;
lixianyu 0:d8f4c441e032 1252 }
lixianyu 0:d8f4c441e032 1253
lixianyu 0:d8f4c441e032 1254 return w;
lixianyu 0:d8f4c441e032 1255 }
lixianyu 0:d8f4c441e032 1256
lixianyu 0:d8f4c441e032 1257
lixianyu 0:d8f4c441e032 1258 /*========================================================================*/
lixianyu 0:d8f4c441e032 1259 /* calculation of font/glyph/string characteristics */
lixianyu 0:d8f4c441e032 1260
lixianyu 0:d8f4c441e032 1261
lixianyu 0:d8f4c441e032 1262 /*
lixianyu 0:d8f4c441e032 1263 Description:
lixianyu 0:d8f4c441e032 1264 Calculate parameter for the minimal bounding box on a given string
lixianyu 0:d8f4c441e032 1265 Output
lixianyu 0:d8f4c441e032 1266 buf->y_min extend of the lower left edge if the string below (y_min<0) or above (y_min>0) baseline (descent)
lixianyu 0:d8f4c441e032 1267 buf->y_max extend of the upper left edge if the string below (y_min<0) or above (y_min>0) baseline (ascent)
lixianyu 0:d8f4c441e032 1268 buf->w the width of the string
lixianyu 0:d8f4c441e032 1269 */
lixianyu 0:d8f4c441e032 1270 struct u8g_str_size_struct
lixianyu 0:d8f4c441e032 1271 {
lixianyu 0:d8f4c441e032 1272 int8_t y_min; /* descent */
lixianyu 0:d8f4c441e032 1273 int8_t y_max; /* ascent */
lixianyu 0:d8f4c441e032 1274 int8_t x, y; /* the reference point of the font (negated!) */
lixianyu 0:d8f4c441e032 1275 u8g_uint_t w; /* width of the overall string */
lixianyu 0:d8f4c441e032 1276 };
lixianyu 0:d8f4c441e032 1277 typedef struct u8g_str_size_struct u8g_str_size_t;
lixianyu 0:d8f4c441e032 1278
lixianyu 0:d8f4c441e032 1279 static void u8g_font_calc_str_min_box(u8g_t *u8g, const char *s, u8g_str_size_t *buf)
lixianyu 0:d8f4c441e032 1280 {
lixianyu 0:d8f4c441e032 1281 /* u8g_glyph_t g; */
lixianyu 0:d8f4c441e032 1282 int8_t tmp;
lixianyu 0:d8f4c441e032 1283
lixianyu 0:d8f4c441e032 1284 /* reset the total minimal width to zero, this will be expanded during calculation */
lixianyu 0:d8f4c441e032 1285 buf->w = 0;
lixianyu 0:d8f4c441e032 1286
lixianyu 0:d8f4c441e032 1287 /* check for empty string, width is already 0, but also reset y_min and y_max to 0 */
lixianyu 0:d8f4c441e032 1288 if ( *s == '\0' )
lixianyu 0:d8f4c441e032 1289 {
lixianyu 0:d8f4c441e032 1290 buf->y_min = 0;
lixianyu 0:d8f4c441e032 1291 buf->y_max = 0;
lixianyu 0:d8f4c441e032 1292 buf->x = 0;
lixianyu 0:d8f4c441e032 1293 buf->y = 0;
lixianyu 0:d8f4c441e032 1294 return;
lixianyu 0:d8f4c441e032 1295 }
lixianyu 0:d8f4c441e032 1296
lixianyu 0:d8f4c441e032 1297 /* reset y_min to the largest possible value. Later we search for the smallest value */
lixianyu 0:d8f4c441e032 1298 /* y_min contains the position [pixel] of the lower left edge of the glyph above (y_min>0) or below (y_min<0) baseline */
lixianyu 0:d8f4c441e032 1299 buf->y_min = 127;
lixianyu 0:d8f4c441e032 1300 /* reset y_max to the smallest possible value. Later we search for the highest value */
lixianyu 0:d8f4c441e032 1301 /* y_max contains the position [pixel] of the upper left edge of the glyph above (y_max>0) or below (y_max<0) baseline */
lixianyu 0:d8f4c441e032 1302 buf->y_max = -128;
lixianyu 0:d8f4c441e032 1303
lixianyu 0:d8f4c441e032 1304 /* get the glyph information of the first char. This must be valid, because we already checked for the empty string */
lixianyu 0:d8f4c441e032 1305 u8g_GetGlyph(u8g, *s);
lixianyu 0:d8f4c441e032 1306
lixianyu 0:d8f4c441e032 1307 /* strlen(s) == 1: width = width(s[0]) */
lixianyu 0:d8f4c441e032 1308 /* strlen(s) == 2: width = - offx(s[0]) + deltax(s[0]) + offx(s[1]) + width(s[1]) */
lixianyu 0:d8f4c441e032 1309 /* strlen(s) == 3: width = - offx(s[0]) + deltax(s[0]) + deltax(s[1]) + offx(s[2]) + width(s[2]) */
lixianyu 0:d8f4c441e032 1310
lixianyu 0:d8f4c441e032 1311 /* assume that the string has size 2 or more, than start with negative offset-x */
lixianyu 0:d8f4c441e032 1312 /* for string with size 1, this will be nullified after the loop */
lixianyu 0:d8f4c441e032 1313 // buf->w = - u8g_font_GetGlyphBBXOffX(u8g->font, g);
lixianyu 0:d8f4c441e032 1314 buf->w = - u8g->glyph_x;
lixianyu 0:d8f4c441e032 1315
lixianyu 0:d8f4c441e032 1316 /* Also copy the position of the first glyph. This is the reference point of the string (negated) */
lixianyu 0:d8f4c441e032 1317 buf->x = u8g->glyph_x;
lixianyu 0:d8f4c441e032 1318 buf->y = u8g->glyph_y;
lixianyu 0:d8f4c441e032 1319
lixianyu 0:d8f4c441e032 1320 for(;;)
lixianyu 0:d8f4c441e032 1321 {
lixianyu 0:d8f4c441e032 1322
lixianyu 0:d8f4c441e032 1323 /* calculated y position of the upper left corner (y_max) and lower left corner (y_min) of the string */
lixianyu 0:d8f4c441e032 1324 /* relative to the base line */
lixianyu 0:d8f4c441e032 1325
lixianyu 0:d8f4c441e032 1326 tmp = u8g->glyph_y;
lixianyu 0:d8f4c441e032 1327 if ( buf->y_min > tmp )
lixianyu 0:d8f4c441e032 1328 buf->y_min = tmp;
lixianyu 0:d8f4c441e032 1329
lixianyu 0:d8f4c441e032 1330 tmp +=u8g->glyph_height;
lixianyu 0:d8f4c441e032 1331 if ( buf->y_max < tmp )
lixianyu 0:d8f4c441e032 1332 buf->y_max = tmp;
lixianyu 0:d8f4c441e032 1333
lixianyu 0:d8f4c441e032 1334 /* check and stop if the end of the string is reached */
lixianyu 0:d8f4c441e032 1335 s++;
lixianyu 0:d8f4c441e032 1336 if ( *s == '\0' )
lixianyu 0:d8f4c441e032 1337 break;
lixianyu 0:d8f4c441e032 1338
lixianyu 0:d8f4c441e032 1339 /* if there are still more characters, add the delta to the next glyph */
lixianyu 0:d8f4c441e032 1340 buf->w += u8g->glyph_dx;
lixianyu 0:d8f4c441e032 1341
lixianyu 0:d8f4c441e032 1342 /* load the next glyph information */
lixianyu 0:d8f4c441e032 1343 u8g_GetGlyph(u8g, *s);
lixianyu 0:d8f4c441e032 1344 }
lixianyu 0:d8f4c441e032 1345
lixianyu 0:d8f4c441e032 1346 /* finally calculate the width of the last char */
lixianyu 0:d8f4c441e032 1347 /* if g was not updated in the for loop (strlen() == 1), then the initial offset x gets removed */
lixianyu 0:d8f4c441e032 1348 buf->w += u8g->glyph_width;
lixianyu 0:d8f4c441e032 1349 // buf->w += u8g_font_GetGlyphBBXOffX(u8g->font, g);
lixianyu 0:d8f4c441e032 1350
lixianyu 0:d8f4c441e032 1351 buf->w += u8g->glyph_x;
lixianyu 0:d8f4c441e032 1352 }
lixianyu 0:d8f4c441e032 1353
lixianyu 0:d8f4c441e032 1354 /* calculate minimal box */
lixianyu 0:d8f4c441e032 1355 void u8g_font_box_min(u8g_t *u8g, const char *s, u8g_str_size_t *buf)
lixianyu 0:d8f4c441e032 1356 {
lixianyu 0:d8f4c441e032 1357 u8g_font_calc_str_min_box(u8g, s, buf);
lixianyu 0:d8f4c441e032 1358 }
lixianyu 0:d8f4c441e032 1359
lixianyu 0:d8f4c441e032 1360 /* calculate gA box, but do not calculate the overall width */
lixianyu 0:d8f4c441e032 1361 void u8g_font_box_left_gA(u8g_t *u8g, const char *s, u8g_str_size_t *buf)
lixianyu 0:d8f4c441e032 1362 {
lixianyu 0:d8f4c441e032 1363
lixianyu 0:d8f4c441e032 1364 }
lixianyu 0:d8f4c441e032 1365
lixianyu 0:d8f4c441e032 1366 /* calculate gA box, including overall width */
lixianyu 0:d8f4c441e032 1367 void u8g_font_box_all_gA(u8g_t *u8g, const char *s, u8g_str_size_t *buf)
lixianyu 0:d8f4c441e032 1368 {
lixianyu 0:d8f4c441e032 1369
lixianyu 0:d8f4c441e032 1370 }
lixianyu 0:d8f4c441e032 1371
lixianyu 0:d8f4c441e032 1372
lixianyu 0:d8f4c441e032 1373 static void u8g_font_get_str_box_fill_args(u8g_t *u8g, const char *s, u8g_str_size_t *buf, u8g_uint_t *x, u8g_uint_t *y, u8g_uint_t *width, u8g_uint_t *height)
lixianyu 0:d8f4c441e032 1374 {
lixianyu 0:d8f4c441e032 1375 /*
lixianyu 0:d8f4c441e032 1376 u8g_glyph_t g;
lixianyu 0:d8f4c441e032 1377 g =
lixianyu 0:d8f4c441e032 1378 */
lixianyu 0:d8f4c441e032 1379 u8g_GetGlyph(u8g, *s);
lixianyu 0:d8f4c441e032 1380 *x += u8g->glyph_x;
lixianyu 0:d8f4c441e032 1381 *width = buf->w;
lixianyu 0:d8f4c441e032 1382 *y -= buf->y_max;
lixianyu 0:d8f4c441e032 1383 /* +1 because y_max is a height, this compensates the next step */
lixianyu 0:d8f4c441e032 1384 //*y += 1;
lixianyu 0:d8f4c441e032 1385 /* because the reference point is one below the string, this compensates the previous step */
lixianyu 0:d8f4c441e032 1386 //*y -= 1;
lixianyu 0:d8f4c441e032 1387 *height = buf->y_max;
lixianyu 0:d8f4c441e032 1388 *height -= buf->y_min;
lixianyu 0:d8f4c441e032 1389 }
lixianyu 0:d8f4c441e032 1390
lixianyu 0:d8f4c441e032 1391
lixianyu 0:d8f4c441e032 1392 void u8g_GetStrMinBox(u8g_t *u8g, const char *s, u8g_uint_t *x, u8g_uint_t *y, u8g_uint_t *width, u8g_uint_t *height)
lixianyu 0:d8f4c441e032 1393 {
lixianyu 0:d8f4c441e032 1394 u8g_str_size_t buf;
lixianyu 0:d8f4c441e032 1395
lixianyu 0:d8f4c441e032 1396 if ( *s == '\0' )
lixianyu 0:d8f4c441e032 1397 {
lixianyu 0:d8f4c441e032 1398 *width= 0;
lixianyu 0:d8f4c441e032 1399 *height = 0;
lixianyu 0:d8f4c441e032 1400 return;
lixianyu 0:d8f4c441e032 1401 }
lixianyu 0:d8f4c441e032 1402
lixianyu 0:d8f4c441e032 1403 u8g_font_calc_str_min_box(u8g, s, &buf);
lixianyu 0:d8f4c441e032 1404 u8g_font_get_str_box_fill_args(u8g, s, &buf, x, y, width, height);
lixianyu 0:d8f4c441e032 1405 }
lixianyu 0:d8f4c441e032 1406
lixianyu 0:d8f4c441e032 1407
lixianyu 0:d8f4c441e032 1408 void u8g_GetStrAMinBox(u8g_t *u8g, const char *s, u8g_uint_t *x, u8g_uint_t *y, u8g_uint_t *width, u8g_uint_t *height)
lixianyu 0:d8f4c441e032 1409 {
lixianyu 0:d8f4c441e032 1410 u8g_str_size_t buf;
lixianyu 0:d8f4c441e032 1411 uint8_t cap_a;
lixianyu 0:d8f4c441e032 1412
lixianyu 0:d8f4c441e032 1413 if ( *s == '\0' )
lixianyu 0:d8f4c441e032 1414 {
lixianyu 0:d8f4c441e032 1415 *width= 0;
lixianyu 0:d8f4c441e032 1416 *height = 0;
lixianyu 0:d8f4c441e032 1417 return;
lixianyu 0:d8f4c441e032 1418 }
lixianyu 0:d8f4c441e032 1419
lixianyu 0:d8f4c441e032 1420 cap_a = u8g_font_GetCapitalAHeight(u8g->font);
lixianyu 0:d8f4c441e032 1421 u8g_font_calc_str_min_box(u8g, s, &buf);
lixianyu 0:d8f4c441e032 1422 if ( buf.y_max < cap_a )
lixianyu 0:d8f4c441e032 1423 buf.y_max = cap_a;
lixianyu 0:d8f4c441e032 1424 u8g_font_get_str_box_fill_args(u8g, s, &buf, x, y, width, height);
lixianyu 0:d8f4c441e032 1425 }
lixianyu 0:d8f4c441e032 1426
lixianyu 0:d8f4c441e032 1427 void u8g_SetFont(u8g_t *u8g, const u8g_fntpgm_uint8_t *font)
lixianyu 0:d8f4c441e032 1428 {
lixianyu 0:d8f4c441e032 1429 if ( u8g->font != font )
lixianyu 0:d8f4c441e032 1430 {
lixianyu 0:d8f4c441e032 1431 u8g->font = font;
lixianyu 0:d8f4c441e032 1432 u8g_UpdateRefHeight(u8g);
lixianyu 0:d8f4c441e032 1433 u8g_SetFontPosBaseline(u8g);
lixianyu 0:d8f4c441e032 1434 }
lixianyu 0:d8f4c441e032 1435 }
lixianyu 0:d8f4c441e032 1436
lixianyu 0:d8f4c441e032 1437 /*========================================================================*/
lixianyu 0:d8f4c441e032 1438 /* anti aliasing fonts */
lixianyu 0:d8f4c441e032 1439
lixianyu 0:d8f4c441e032 1440 int8_t u8g_draw_aa_glyph(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, uint8_t encoding)
lixianyu 0:d8f4c441e032 1441 {
lixianyu 0:d8f4c441e032 1442 const u8g_pgm_uint8_t *data;
lixianyu 0:d8f4c441e032 1443 uint8_t w, h;
lixianyu 0:d8f4c441e032 1444 uint8_t i, j;
lixianyu 0:d8f4c441e032 1445 u8g_uint_t ix, iy;
lixianyu 0:d8f4c441e032 1446
lixianyu 0:d8f4c441e032 1447 {
lixianyu 0:d8f4c441e032 1448 u8g_glyph_t g = u8g_GetGlyph(u8g, encoding);
lixianyu 0:d8f4c441e032 1449 if ( g == NULL )
lixianyu 0:d8f4c441e032 1450 return 0;
lixianyu 0:d8f4c441e032 1451 data = u8g_font_GetGlyphDataStart(u8g->font, g);
lixianyu 0:d8f4c441e032 1452 }
lixianyu 0:d8f4c441e032 1453
lixianyu 0:d8f4c441e032 1454 w = u8g->glyph_width;
lixianyu 0:d8f4c441e032 1455 h = u8g->glyph_height;
lixianyu 0:d8f4c441e032 1456
lixianyu 0:d8f4c441e032 1457 x += u8g->glyph_x;
lixianyu 0:d8f4c441e032 1458 y -= u8g->glyph_y;
lixianyu 0:d8f4c441e032 1459 y--;
lixianyu 0:d8f4c441e032 1460
lixianyu 0:d8f4c441e032 1461 if ( u8g_IsBBXIntersection(u8g, x, y-h+1, w, h) == 0 )
lixianyu 0:d8f4c441e032 1462 return u8g->glyph_dx;
lixianyu 0:d8f4c441e032 1463
lixianyu 0:d8f4c441e032 1464 /* now, w is reused as bytes per line */
lixianyu 0:d8f4c441e032 1465 w += 3;
lixianyu 0:d8f4c441e032 1466 w /= 4;
lixianyu 0:d8f4c441e032 1467
lixianyu 0:d8f4c441e032 1468 iy = y;
lixianyu 0:d8f4c441e032 1469 iy -= h;
lixianyu 0:d8f4c441e032 1470 iy++;
lixianyu 0:d8f4c441e032 1471
lixianyu 0:d8f4c441e032 1472 for( j = 0; j < h; j++ )
lixianyu 0:d8f4c441e032 1473 {
lixianyu 0:d8f4c441e032 1474 ix = x;
lixianyu 0:d8f4c441e032 1475 for( i = 0; i < w; i++ )
lixianyu 0:d8f4c441e032 1476 {
lixianyu 0:d8f4c441e032 1477 u8g_Draw4TPixel(u8g, ix, iy, 0, u8g_pgm_read(data));
lixianyu 0:d8f4c441e032 1478 data++;
lixianyu 0:d8f4c441e032 1479 ix+=4;
lixianyu 0:d8f4c441e032 1480 }
lixianyu 0:d8f4c441e032 1481 iy++;
lixianyu 0:d8f4c441e032 1482 }
lixianyu 0:d8f4c441e032 1483 return u8g->glyph_dx;
lixianyu 0:d8f4c441e032 1484 }
lixianyu 0:d8f4c441e032 1485
lixianyu 0:d8f4c441e032 1486 int8_t u8g_DrawAAGlyph(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, uint8_t encoding)
lixianyu 0:d8f4c441e032 1487 {
lixianyu 0:d8f4c441e032 1488 y += u8g->font_calc_vref(u8g);
lixianyu 0:d8f4c441e032 1489 return u8g_draw_aa_glyph(u8g, x, y, encoding);
lixianyu 0:d8f4c441e032 1490 }
lixianyu 0:d8f4c441e032 1491
lixianyu 0:d8f4c441e032 1492 u8g_uint_t u8g_DrawAAStr(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, const char *s)
lixianyu 0:d8f4c441e032 1493 {
lixianyu 0:d8f4c441e032 1494 u8g_uint_t t = 0;
lixianyu 0:d8f4c441e032 1495 int8_t d;
lixianyu 0:d8f4c441e032 1496
lixianyu 0:d8f4c441e032 1497 if ( u8g_font_GetFormat(u8g->font) != 2 )
lixianyu 0:d8f4c441e032 1498 return 0;
lixianyu 0:d8f4c441e032 1499 //u8g_uint_t u8g_GetStrWidth(u8g, s);
lixianyu 0:d8f4c441e032 1500 //u8g_font_GetFontAscent(u8g->font)-u8g_font_GetFontDescent(u8g->font);
lixianyu 0:d8f4c441e032 1501
lixianyu 0:d8f4c441e032 1502 y += u8g->font_calc_vref(u8g);
lixianyu 0:d8f4c441e032 1503
lixianyu 0:d8f4c441e032 1504 while( *s != '\0' )
lixianyu 0:d8f4c441e032 1505 {
lixianyu 0:d8f4c441e032 1506 d = u8g_draw_aa_glyph(u8g, x, y, *s);
lixianyu 0:d8f4c441e032 1507 x += d;
lixianyu 0:d8f4c441e032 1508 t += d;
lixianyu 0:d8f4c441e032 1509 s++;
lixianyu 0:d8f4c441e032 1510 }
lixianyu 0:d8f4c441e032 1511 return t;
lixianyu 0:d8f4c441e032 1512 }
lixianyu 0:d8f4c441e032 1513