Allow fonts from dataflash. use "setExtFont(filename)". for flash fonts use "setIntFont(&font)".

This commit is contained in:
Stefan `Sec` Zehl 2011-07-23 14:30:20 +02:00
parent 157b9ed0f0
commit 30d3089cc8
5 changed files with 313 additions and 77 deletions

View File

@ -7,6 +7,7 @@
#include "lcd/allfonts.h"
#include "filesystem/ff.h"
#include "filesystem/select.h"
#include "funk/nrf24l01p.h"
#include "usb/usbmsc.h"
@ -75,35 +76,22 @@ void f_init(void){
lcdPrintln("Done.");
};
static FONT fonts[] = {
&Font_7x8,
&Font_Ubuntu18pt,
&Font_Ubuntu29pt,
&Font_Ubuntu36pt,
&Font_Orbitron14pt,
&Font_3x6,
&Font_5x8,
&Font_8x8,
&Font_8x8Thin,
&Font_Invaders
};
char fontname[15];
void f_nick(void){
static char ctr=0;
char key;
signed char x=10;
signed char y=10;
static signed char x=10;
static signed char y=10;
while (1) {
lcdClear();
lcdFill(255);
font=fonts[ctr%10];
DoString(x,y,nickname);
// lcdSafeSetPixel(x,y,1);
setExtFont(fontname);
font=&Font_7x8;
DoString(x,y,nickname);
setIntFont(&Font_7x8);
lcdSetCrsr(50,50);
lcdPrintInt(x);
lcdPrint("x");
@ -135,6 +123,23 @@ void f_nick(void){
};
};
void f_font(void){
if( selectFile(fontname,"F0N") != 0){
lcdPrintln("No file selected.");
return;
};
lcdClear();
lcdPrintln(fontname);
setExtFont(fontname);
lcdPrintln("PUabc€");
setIntFont(&Font_7x8);
lcdPrintln("done.");
lcdDisplay();
while(!getInputRaw())delayms(10);
};
/***********************************************************************/
void gotoISP(void) {
@ -178,7 +183,7 @@ void msc_menu(void){
const struct MENU_DEF menu_ISP = {"Invoke ISP", &gotoISP};
const struct MENU_DEF menu_init = {"F Init", &f_init};
const struct MENU_DEF menu_nick = {"F Nick", &f_nick};
//const struct MENU_DEF menu_snd = {"F Send", &f_send};
const struct MENU_DEF menu_font = {"F sel", &f_font};
const struct MENU_DEF menu_mirror = {"Mirror", &lcd_mirror};
const struct MENU_DEF menu_invert = {"Invert", &lcd_invert};
const struct MENU_DEF menu_volt = {"Akku", &adc_check};
@ -188,7 +193,7 @@ const struct MENU_DEF menu_nop = {"---", NULL};
static menuentry menu[] = {
&menu_init,
&menu_nick,
// &menu_snd,
&menu_font,
&menu_nop,
&menu_mirror,
&menu_invert,

View File

@ -1,9 +1,6 @@
#include <fonts.h>
#include <render.h>
#define MAXCHR (30*20)
static uint8_t buf[MAXCHR];
// Local function: Get next nibble.
int ctr=0; // offset for next nibble
int hilo=0; // 0= high nibble next, 1=low nibble next
@ -15,7 +12,11 @@ static uint8_t buf[MAXCHR];
ctr++;
hilo=1-hilo;
if(hilo==1){
byte=data[ctr];
if(efont.type==FONT_EXTERNAL){
byte=_getFontData(GET_DATA,0);
}else{
byte=data[ctr];
};
val=byte>>4;
}else{
val=byte&0x0f;
@ -44,7 +45,7 @@ uint8_t * pk_decode(const uint8_t * ldata,int * len){
int length=*len; // Length of character bytestream
int height; // Height of character in bytes
int hoff; // bit position for non-integer heights
uint8_t * bufptr=buf; // Output buffer for decoded character
uint8_t * bufptr=charBuf; // Output buffer for decoded character
height=(font->u8Height-1)/8+1;
hoff=font->u8Height%8;
@ -55,10 +56,17 @@ uint8_t * pk_decode(const uint8_t * ldata,int * len){
int pos=0; // Decoder internal: current bit position (0..7)
int nyb; // Decoder internal: current nibble / value
if(data[ctr]>>4 == 14){ // Char starts with 1-bits.
gnn();
curbit=1;
};
if(efont.type==FONT_EXTERNAL){
if(_getFontData(PEEK_DATA,0)>>4 == 14){ // Char starts with 1-bits.
gnn();
curbit=1;
};
}else{
if(data[ctr]>>4 == 14){ // Char starts with 1-bits.
gnn();
curbit=1;
};
};
while(ctr<length){ /* Iterate the whole input stream */
@ -89,14 +97,14 @@ uint8_t * pk_decode(const uint8_t * ldata,int * len){
*bufptr|=1<<(7-pos);
};
pos++;
if(((bufptr-buf)%height)==(height-1) && (pos==hoff)){
if(((bufptr-charBuf)%height)==(height-1) && (pos==hoff)){
// Finish incomplete last byte per column
pos=8;
};
if(pos==8){
bufptr++;
if((bufptr-buf)%height==0){ // End of column?
if((bufptr-charBuf)%height==0){ // End of column?
while(repeat>0){
for(int y=0;y<height;y++){
bufptr[0]=bufptr[-height];
@ -111,6 +119,6 @@ uint8_t * pk_decode(const uint8_t * ldata,int * len){
curbit=1-curbit;
};
*len=(bufptr-buf)/height; // return size of output buffer.
return buf;
*len=(bufptr-charBuf)/height; // return size of output buffer.
return charBuf;
};

View File

@ -20,17 +20,19 @@ struct FONT_DEF {
const uint16_t *charExtra; /* Pointer to array of extra char info */
};
struct EXTFONT {
char type; // 0: none, 1: static, 2: loaded
char name[13];
struct FONT_DEF def;
};
typedef const struct FONT_DEF * FONT;
/* interesting / exported stuff */
#define FONT_DIR_LTR 0
#define FONT_DIR_RTL 1
// Not implemented
// #define FONT_DIR_UP 2
// #define FONT_DIR_DOWN 3
#define FONT_DEFAULT 0
#define FONT_INTERNAL 1
#define FONT_EXTERNAL 2
extern const struct FONT_DEF * font;
extern char font_direction;
extern struct EXTFONT efont;
#endif

View File

@ -1,16 +1,193 @@
#include <string.h>
#include <sysdefs.h>
#include <render.h>
#include <decoder.h>
#include <fonts.h>
#include "basic/basic.h"
#include "fonts/smallfonts.h"
#include "filesystem/ff.h"
/* Global Variables */
const struct FONT_DEF * font = NULL;
char font_direction = FONT_DIR_LTR;
struct EXTFONT efont;
FIL file; /* current font file */
/* Exported Functions */
void setIntFont(const struct FONT_DEF * font){
memcpy(&efont.def,font,sizeof(struct FONT_DEF));
efont.type=FONT_INTERNAL;
font=NULL;
};
void setExtFont(const char *fname){
if(strlen(fname)>8+4)
return;
strcpy(efont.name,fname);
// memcpy(efont.name+strlen(fname),".f0n",5);
efont.type=FONT_EXTERNAL;
font=NULL;
};
int _getFontData(int type, int offset){
UINT readbytes;
UINT res;
static uint16_t extras;
static uint16_t character;
static const void * ptr;
if(efont.type == FONT_EXTERNAL){
if (type == START_FONT){
res = f_read(&file, &efont.def.u8Width, sizeof(uint8_t), &readbytes);
res = f_read(&file, &efont.def.u8Height, sizeof(uint8_t), &readbytes);
res = f_read(&file, &efont.def.u8FirstChar, sizeof(uint8_t), &readbytes);
res = f_read(&file, &efont.def.u8LastChar, sizeof(uint8_t), &readbytes);
res = f_read(&file, &extras, sizeof(uint16_t), &readbytes);
return 0;
};
if (type == SEEK_EXTRAS){
f_lseek(&file,6);
return 0;
};
if(type == GET_EXTRAS){
uint16_t word;
res = f_read(&file, &word, sizeof(uint16_t), &readbytes);
return word;
};
if (type == SEEK_WIDTH){
f_lseek(&file,6+(extras*sizeof(uint16_t)));
return 0;
};
if(type == GET_WIDTH || type == GET_DATA){
uint8_t width;
res = f_read(&file, &width, sizeof(uint8_t), &readbytes);
return width;
};
if(type == SEEK_DATA){
character=offset;
f_lseek(&file,6+
(extras*sizeof(uint16_t))+
((extras+font->u8LastChar-font->u8FirstChar)*sizeof(uint8_t))+
(offset*sizeof(uint8_t))
);
return 0;
};
if(type == PEEK_DATA){
uint8_t width;
res = f_read(&file, &width, sizeof(uint8_t), &readbytes);
f_lseek(&file,6+
(extras*sizeof(uint16_t))+
((extras+font->u8LastChar-font->u8FirstChar)*sizeof(uint8_t))+
(character*sizeof(uint8_t))
);
return width;
};
#ifdef NOTYET
}else{ // efont.type==FONT_INTERNAL
if (type == START_FONT){
memcpy(&efont.def,font,sizeof(struct FONT_DEF));
return 0;
};
if (type == SEEK_EXTRAS){
ptr=efont.def.charExtra;
return 0;
};
if(type == GET_EXTRAS){
uint16_t word;
word=*(uint16_t*)ptr;
ptr=((uint16_t*)ptr)+1;
return word;
};
if (type == SEEK_WIDTH){
ptr=efont.def.charInfo;
return 0;
};
if(type == GET_WIDTH || type == GET_DATA){
uint8_t width;
width=*(uint8_t*)ptr;
ptr=((uint8_t*)ptr)+1;
return width;
};
if(type == SEEK_DATA){
if(offset==REPEAT_LAST_CHARACTER)
offset=character;
else
character=offset;
ptr=efont.def.au8FontTable;
return 0;
};
#endif
};
/* NOTREACHED */
return 0;
};
int _getIndex(int c){
#define ERRCHR (font->u8FirstChar+1)
/* Does this font provide this character? */
if(c<font->u8FirstChar)
c=ERRCHR;
if(c>font->u8LastChar && efont.type!=FONT_EXTERNAL && font->charExtra == NULL)
c=ERRCHR;
if(c>font->u8LastChar && (efont.type==FONT_EXTERNAL || font->charExtra != NULL)){
if(efont.type==FONT_EXTERNAL){
_getFontData(SEEK_EXTRAS,0);
int cc=0;
int cache;
while( (cache=_getFontData(GET_EXTRAS,0)) < c)
cc++;
if( cache > c)
c=ERRCHR;
else
c=font->u8LastChar+cc+1;
}else{
int cc=0;
while( font->charExtra[cc] < c)
cc++;
if(font->charExtra[cc] > c)
c=ERRCHR;
else
c=font->u8LastChar+cc+1;
};
};
c-=font->u8FirstChar;
return c;
};
uint8_t charBuf[MAXCHR];
int DoChar(int sx, int sy, int c){
// font=NULL;
if(font==NULL){
if(efont.type==FONT_INTERNAL){
font=&efont.def;
}else if (efont.type==FONT_EXTERNAL){
UINT res;
res=f_open(&file, efont.name, FA_OPEN_EXISTING|FA_READ);
if(res){
efont.type=0;
font=&Font_7x8;
}else{
_getFontData(START_FONT,0);
font=&efont.def;
};
}else{
font=&Font_7x8;
};
};
/* how many bytes is it high? */
char height=(font->u8Height-1)/8+1;
char hoff=(8-(font->u8Height%8))%8;
@ -18,49 +195,76 @@ int DoChar(int sx, int sy, int c){
const uint8_t * data;
int width,preblank=0,postblank=0;
do { /* Get Character data */
/* Does this font provide this character? */
if(c<font->u8FirstChar)
c=font->u8FirstChar+1; // error
if(c>font->u8LastChar && font->charExtra == NULL)
c=font->u8FirstChar+1; // error
if(c>font->u8LastChar && font->charExtra != NULL){
int cc=0;
while( font->charExtra[cc] < c)
cc++;
if(font->charExtra[cc] > c)
c=font->u8FirstChar+1; // error
else
c=font->u8LastChar+cc+1;
};
/* Get intex into character list */
c=_getIndex(c);
/* starting offset into character source data */
int toff=0;
if(font->u8Width==0){
for(int y=0;y<c-font->u8FirstChar;y++)
toff+=font->charInfo[y].widthBits;
width=font->charInfo[c-font->u8FirstChar].widthBits;
if(efont.type == FONT_EXTERNAL){
_getFontData(SEEK_WIDTH,0);
for(int y=0;y<c;y++)
toff+=_getFontData(GET_WIDTH,0);
width=_getFontData(GET_WIDTH,0);
toff*=height;
data=&font->au8FontTable[toff];
_getFontData(SEEK_DATA,toff);
UINT res;
UINT readbytes;
res = f_read(&file, charBuf, width*height, &readbytes);
if(res != FR_OK || readbytes<width*height)
return sx;
data=charBuf;
}else{
for(int y=0;y<c;y++)
toff+=font->charInfo[y].widthBits;
width=font->charInfo[c].widthBits;
toff*=height;
data=&font->au8FontTable[toff];
};
postblank=1;
}else if(font->u8Width==1){ // NEW CODE
// Find offset and length for our character
for(int y=0;y<c-font->u8FirstChar;y++)
toff+=font->charInfo[y].widthBits;
width=font->charInfo[c-font->u8FirstChar].widthBits;
if(font->au8FontTable[toff]>>4 == 15){ // It's a raw character!
preblank = font->au8FontTable[toff+1];
postblank= font->au8FontTable[toff+2];
data=&font->au8FontTable[toff+3];
width=(width-3/height);
if(efont.type == FONT_EXTERNAL){
_getFontData(SEEK_WIDTH,0);
for(int y=0;y<c;y++)
toff+=_getFontData(GET_WIDTH,0);
width=_getFontData(GET_WIDTH,0);
_getFontData(SEEK_DATA,toff);
UINT res;
UINT readbytes;
uint8_t testbyte;
res = f_read(&file, &testbyte, sizeof(uint8_t), &readbytes);
if(testbyte>>4 ==15){
res = f_read(&file, &preblank, sizeof(uint8_t), &readbytes);
res = f_read(&file, &postblank, sizeof(uint8_t), &readbytes);
width-=3;
width/=height;
res = f_read(&file, charBuf, width*height, &readbytes);
if(res != FR_OK || readbytes<width*height)
return sx;
data=charBuf;
}else{
_getFontData(SEEK_DATA,toff);
data=pk_decode(NULL,&width); // Hackety-hack
};
}else{
data=pk_decode(&font->au8FontTable[toff],&width);
}
// Find offset and length for our character
for(int y=0;y<c;y++)
toff+=font->charInfo[y].widthBits;
width=font->charInfo[c].widthBits;
if(font->au8FontTable[toff]>>4 == 15){ // It's a raw character!
preblank = font->au8FontTable[toff+1];
postblank= font->au8FontTable[toff+2];
data=&font->au8FontTable[toff+3];
width=(width-3/height);
}else{
data=pk_decode(&font->au8FontTable[toff],&width);
}
};
}else{
toff=(c-font->u8FirstChar)*font->u8Width*height;
toff=(c)*font->u8Width*height;
width=font->u8Width;
data=&font->au8FontTable[toff];
};

View File

@ -2,6 +2,7 @@
#define __RENDER_H_
#include "display.h"
#include "fonts.h"
/*
#badge <s> 96x68
@ -33,5 +34,21 @@ int DoIntXn(int sx, int sy, unsigned int num, unsigned int maxlen);
int DoIntX(int sx, int sy, unsigned int num);
int DoCharX(int sx, int sy, unsigned char num);
int DoShortX(int sx, int sy, uint16_t num);
void setIntFont(const struct FONT_DEF * font);
void setExtFont(const char *file);
#define START_FONT 0
#define SEEK_EXTRAS 1
#define GET_EXTRAS 2
#define SEEK_WIDTH 3
#define GET_WIDTH 4
#define SEEK_DATA 5
#define GET_DATA 6
#define PEEK_DATA 7
int _getFontData(int type, int offset);
#define MAXCHR (30*20)
extern uint8_t charBuf[MAXCHR];
#endif