kerning support

This commit is contained in:
olikraus 2016-08-14 13:03:25 +02:00
parent 26d60388ea
commit 0f05a5ade0
5 changed files with 204 additions and 9 deletions

View File

@ -185,7 +185,7 @@ typedef void (*u8g2_update_dimension_cb)(u8g2_t *u8g2);
typedef void (*u8g2_draw_l90_cb)(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir);
typedef void (*u8g2_draw_ll_hvline_cb)(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir);
typedef void (*u8g2_get_kerning_cb)(u8g2_t *u8g2, uint16_t e1, uint16_t e2);
typedef uint8_t (*u8g2_get_kerning_cb)(u8g2_t *u8g2, uint16_t e1, uint16_t e2);
/* from ucglib... */

View File

@ -714,8 +714,9 @@ u8g2_uint_t u8g2_DrawGlyph(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, uint16_t
static u8g2_uint_t u8g2_draw_string(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, const char *str) U8G2_NOINLINE;
static u8g2_uint_t u8g2_draw_string(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, const char *str)
{
uint16_t e_prev = 0x0ffff;
uint16_t e;
u8g2_uint_t delta, sum;
u8g2_uint_t delta, sum, k;
u8x8_utf8_init(u8g2_GetU8x8(u8g2));
sum = 0;
for(;;)
@ -726,6 +727,29 @@ static u8g2_uint_t u8g2_draw_string(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y,
str++;
if ( e != 0x0fffe )
{
k = u8g2->get_kerning_cb(u8g2, e_prev, e);
e_prev = e;
#ifdef U8G2_WITH_FONT_ROTATION
switch(u8g2->font_decode.dir)
{
case 0:
x -= k;
break;
case 1:
y -= k;
break;
case 2:
x += k;
break;
case 3:
y += k;
break;
}
#else
x -= k;
#endif
delta = u8g2_DrawGlyph(u8g2, x, y, e);
#ifdef U8G2_WITH_FONT_ROTATION
@ -746,7 +770,8 @@ static u8g2_uint_t u8g2_draw_string(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y,
}
#else
x += delta;
#endif
#endif
sum += delta;
}
}

View File

@ -49,27 +49,27 @@ uint8_t u8g2_GetKerning(u8g2_t *u8g2, uint16_t e1, uint16_t e2)
return 0;
/* search for the encoding in the first table */
cnt = u8g2->kerning.first_table_cnt;
cnt = u8g2->kerning->first_table_cnt;
cnt--; /* ignore the last element of the table, which is 0x0ffff */
for( i1 = 0; i1 < cnt; i1++ )
{
if ( u8g2->first_encoding_table[i1] == e1 )
if ( u8g2->kerning->first_encoding_table[i1] == e1 )
break;
}
if ( i1 >= cnt )
return 0; /* e1 not part of the kerning table, return 0 */
/* get the upper index for i2 */
end = u8g2->index_to_second_table[i1+1];
for( i2 = u8g2->index_to_second_table[i1]; i2 < end; i2++ )
end = u8g2->kerning->index_to_second_table[i1+1];
for( i2 = u8g2->kerning->index_to_second_table[i1]; i2 < end; i2++ )
{
if ( u8g2->second_encoding_table[i2] == e2 )
if ( u8g2->kerning->second_encoding_table[i2] == e2 )
break;
}
if ( i2 >= end )
return 0; /* e2 not part of any pair with e1, return 0 */
return u8g2->kerning_values[i2];
return u8g2->kerning->kerning_values[i2];
}

View File

@ -0,0 +1,12 @@
CFLAGS = -g -Wall -I../../../csrc/. `sdl-config --cflags`
SRC = $(shell ls ../../../csrc/*.c) $(shell ls ../common/*.c ) main.c
OBJ = $(SRC:.c=.o)
helloworld: $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) `sdl-config --libs` -o u8g2_sdl
clean:
-rm $(OBJ) u8g2_sdl

158
sys/sdl/text_kerning/main.c Normal file
View File

@ -0,0 +1,158 @@
#include "u8g2.h"
#include <stdio.h>
u8g2_t u8g2;
static const uint16_t helv_first_encoding_table[50] = {
65, 67, 70, 75, 76, 80, 84, 86, 87, 88, 89, 97, 98, 99, 101, 103, 104,
107, 109, 110, 111, 112, 113, 114, 115, 117, 118, 119, 120, 121, 122, 192, 193,
194, 195, 196, 197, 199, 215, 221, 222, 224, 230, 231, 232, 242, 248, 249, 254,
65535};
static const uint16_t helv_index_to_second_table[50] = {
0, 7, 9, 19, 39, 47, 56, 92, 101, 109, 112, 135, 138, 141, 142, 145, 146,
150, 151, 154, 158, 161, 164, 165, 169, 172, 173, 176, 177, 178, 181, 182, 189,
196, 203, 210, 217, 224, 226, 229, 252, 255, 258, 261, 262, 265, 268, 269, 270,
273};
static const uint16_t helv_second_encoding_table[273] = {
84, 86, 87, 89, 121, 221, 253, 121, 253, 65, 74, 122, 192, 193, 194, 195, 196,
197, 198, 67, 71, 79, 81, 116, 117, 118, 119, 121, 199, 210, 211, 212, 213,
214, 249, 250, 251, 252, 253, 84, 86, 87, 89, 118, 121, 221, 253, 65, 74,
192, 193, 194, 195, 196, 197, 198, 65, 74, 97, 99, 100, 101, 103, 109, 110,
111, 112, 113, 114, 115, 117, 118, 119, 120, 121, 122, 192, 193, 194, 195, 196,
197, 198, 215, 225, 230, 231, 233, 243, 248, 250, 253, 65, 74, 192, 193, 194,
195, 196, 197, 198, 65, 192, 193, 194, 195, 196, 197, 198, 118, 121, 253, 65,
74, 97, 99, 100, 101, 103, 111, 113, 192, 193, 194, 195, 196, 197, 198, 225,
229, 230, 231, 233, 243, 248, 84, 89, 221, 84, 89, 221, 84, 84, 89, 221,
84, 84, 86, 89, 221, 84, 84, 89, 221, 84, 86, 89, 221, 84, 89, 221,
84, 89, 221, 84, 74, 84, 88, 90, 84, 89, 221, 84, 84, 88, 90, 84,
84, 84, 88, 90, 84, 84, 86, 87, 89, 121, 221, 253, 84, 86, 87, 89,
121, 221, 253, 84, 86, 87, 89, 121, 221, 253, 84, 86, 87, 89, 121, 221,
253, 84, 86, 87, 89, 121, 221, 253, 84, 86, 87, 89, 121, 221, 253, 121,
253, 84, 89, 221, 65, 74, 97, 99, 100, 101, 103, 111, 113, 192, 193, 194,
195, 196, 197, 198, 225, 229, 230, 231, 233, 243, 248, 84, 88, 90, 84, 89,
221, 84, 89, 221, 84, 84, 89, 221, 84, 89, 221, 84, 84, 84, 89, 221};
static const uint8_t helv_kerning_values[273] = {
2, 2, 1, 3, 1, 3, 1, 1, 1, 1, 5, 3, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 3, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 3, 2, 3, 1, 3, 1, 3, 3, 3, 1, 5,
1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3,
2, 3, 2, 3, 2, 3, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 2, 2, 2,
2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3,
3, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 1,
1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 2, 2, 1, 1,
3, 3, 1, 2, 2, 2, 3, 1, 1, 3, 1, 2, 2, 2, 1, 1,
2, 1, 1, 3, 1, 2, 1, 1, 2, 1, 1, 3, 2, 1, 1, 1,
2, 2, 1, 1, 2, 2, 2, 1, 3, 1, 3, 1, 2, 2, 1, 3,
1, 3, 1, 2, 2, 1, 3, 1, 3, 1, 2, 2, 1, 3, 1, 3,
1, 2, 2, 1, 3, 1, 3, 1, 2, 2, 1, 3, 1, 3, 1, 1,
1, 3, 1, 1, 3, 3, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3,
3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1,
1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1};
u8g2_kerning_t helv = {
50, 273,
helv_first_encoding_table,
helv_index_to_second_table,
helv_second_encoding_table,
helv_kerning_values};
int main(void)
{
int x, y;
int k;
int i;
u8g2_SetupBuffer_SDL_128x64_4(&u8g2, &u8g2_cb_r0);
u8x8_InitDisplay(u8g2_GetU8x8(&u8g2));
u8x8_SetPowerSave(u8g2_GetU8x8(&u8g2), 0);
//u8g2_SetFont(&u8g2, u8g2_font_helvB18_tr);
u8g2_SetFont(&u8g2, u8g2_font_helvB18_tf);
x = 50;
y = 30;
for(;;)
{
#ifdef U8G2_WITH_HVLINE_COUNT
u8g2.hv_cnt = 0UL;
#endif /* U8G2_WITH_HVLINE_COUNT */
/*
u8g2_ClearBuffer(&u8g2);
u8g2_SetFontDirection(&u8g2, 0);
u8g2_DrawStr(&u8g2, x, y, "ABC");
u8g2_SetFontDirection(&u8g2, 1);
u8g2_DrawStr(&u8g2, x, y, "abc");
u8g2_SetFontDirection(&u8g2, 2);
u8g2_DrawStr(&u8g2, x, y, "abc");
u8g2_SetFontDirection(&u8g2, 3);
u8g2_DrawStr(&u8g2, x, y, "abc");
u8g2_SendBuffer(&u8g2);
*/
u8g2.kerning = &helv;
u8g2.get_kerning_cb = u8g2_GetKerning;
u8g2_FirstPage(&u8g2);
i = 0;
do
{
u8g2_SetFontDirection(&u8g2, 0);
u8g2_DrawUTF8(&u8g2, x, y, "WAV");
u8g2_SetFontDirection(&u8g2, 1);
u8g2_DrawUTF8(&u8g2, x, y, "WAV");
u8g2_SetFontDirection(&u8g2, 2);
u8g2_DrawUTF8(&u8g2, x, y, "WAV");
u8g2_SetFontDirection(&u8g2, 3);
u8g2_DrawUTF8(&u8g2, x, y, "WAV");
if ( i == 1 )
{
u8g2_DrawHVLine(&u8g2, u8g2.user_x0, u8g2.user_y0, 1, 0);
u8g2_DrawHVLine(&u8g2, u8g2.user_x0, u8g2.user_y1-1, 1, 0);
u8g2_DrawHVLine(&u8g2, u8g2.user_x1-1, u8g2.user_y1-1, 1, 0);
u8g2_DrawHVLine(&u8g2, u8g2.user_x1-1, u8g2.user_y0, 1, 0);
}
i++;
} while( u8g2_NextPage(&u8g2) );
#ifdef U8G2_WITH_HVLINE_COUNT
printf("hv cnt: %ld\n", u8g2.hv_cnt);
#endif /* U8G2_WITH_HVLINE_COUNT */
do
{
k = u8g_sdl_get_key();
} while( k < 0 );
if ( k == 273 ) y -= 7;
if ( k == 274 ) y += 7;
if ( k == 276 ) x -= 7;
if ( k == 275 ) x += 7;
if ( k == 'e' ) y -= 1;
if ( k == 'x' ) y += 1;
if ( k == 's' ) x -= 1;
if ( k == 'd' ) x += 1;
if ( k == 'q' ) break;
}
//u8x8_Set8x8Font(u8g2_GetU8x8(&u8g2), bdf_font);
//u8x8_Draw8x8String(u8g2_GetU8x8(&u8g2), 0, 0, "Hello World!");
return 0;
}