Import Upstream version 1.3.0~svn316
This commit is contained in:
1 parent
0f94383c26
commit
f836fd86a6
53 files changed
+1199
-4882
No files matched your search
+4
-1
@@ -4,8 +4,11 @@ AM_CFLAGS=@CFLAGS@
|
||||
DISTCLEANFILES = Makefile.in
|
||||
|
||||
lib_LTLIBRARIES = libg15render.la
|
||||
libg15render_la_SOURCES = libg15render.h text.c pixel.c screen.c font_6x4.c font_7x5.c font_8x8.c
|
||||
libg15render_la_SOURCES = libg15render.h text.c pixel.c screen.c
|
||||
libg15render_la_LIBADD = @FTLIB@
|
||||
libg15render_la_LDFLAGS = -version-info 2:0:1
|
||||
include_HEADERS= libg15render.h
|
||||
|
||||
bin_PROGRAMS = g15fontconvert
|
||||
g15fontconvert_SOURCES = g15fontconvert.c
|
||||
g15fontconvert_LDADD = libg15render.la @FTLIB@
|
||||
@@ -1,20 +0,0 @@
|
||||
/*
|
||||
This file is part of g15lcd.
|
||||
|
||||
g15lcd is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
g15lcd is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with g15lcd; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
/* This font was provided by Sebastian XXX (sorry bastl, i still cant spell your last name ) */
|
||||
|
||||
unsigned char fontdata_6x4[] = {0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,Line truncated
|
||||
-2066
File diff suppressed because it is too large.
Load diff
-2591
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,227 @@
|
||||
/*
|
||||
This file is part of g15tools.
|
||||
|
||||
g15tools is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
g15tools is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with libg15render; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
#include "libg15render.h"
|
||||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#ifdef TTF_SUPPORT
|
||||
#include <ft2build.h>
|
||||
#include FT_FREETYPE_H
|
||||
#include FT_BITMAP_H
|
||||
#endif
|
||||
|
||||
#ifndef TTF_SUPPORT /* No truetype support */
|
||||
int main(char *argv) {
|
||||
printf("g15fontconvert - libg15render has no FreeType support compiled in. This is required for the conversion program. Leaving now.\n");
|
||||
return -1;
|
||||
}
|
||||
#else /* TrueType Support */
|
||||
|
||||
void packpixel(unsigned char *buffer, int width, int x, int y,int colour) {
|
||||
|
||||
if(x<0) x=0;
|
||||
if(y<0) y=0;
|
||||
unsigned char c = 0x80 >> (x % 8);
|
||||
if(colour)
|
||||
buffer[width * y + (x / 8)] |= c;
|
||||
}
|
||||
|
||||
FT_Library lib;
|
||||
int convertG15Font(char *inFilename, char *oFilename, int size, int gap){
|
||||
|
||||
FT_Face face;
|
||||
FT_GlyphSlot glyphslot;
|
||||
g15font *font = NULL;
|
||||
int i;
|
||||
int retval;
|
||||
|
||||
retval = FT_New_Face(lib, inFilename, 0, &face);
|
||||
if(retval== FT_Err_Unknown_File_Format){
|
||||
return -1;
|
||||
}
|
||||
else if(retval) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
glyphslot = face->glyph;
|
||||
if(FT_IS_SCALABLE(face)) {
|
||||
FT_Set_Char_Size(face, 0, size << 6, 80, 0);
|
||||
}else {
|
||||
printf("Bitmap Font has fixed height, ignoring requested size\n");
|
||||
}
|
||||
font = malloc(sizeof(g15font));
|
||||
font->font_height = (face->size->metrics.ascender >> 6) - (face->size->metrics.descender >> 6);
|
||||
font->ascender_height = face->size->metrics.ascender >> 6;
|
||||
font->lineheight = face->size->metrics.height >> 6;
|
||||
|
||||
if(gap>-1)
|
||||
font->default_gap = gap;
|
||||
else
|
||||
font->default_gap = 1;
|
||||
|
||||
if(font->font_height==0) {
|
||||
font->font_height = 2+(face->available_sizes->y_ppem>>6); /* */
|
||||
printf("Font height unknown, guessing %i.\n",font->font_height - 1);
|
||||
}
|
||||
if(font->ascender_height==0) {
|
||||
font->ascender_height = font->font_height;
|
||||
face->size->metrics.ascender = font->font_height<<6;
|
||||
printf("modded ascender to %i\n", (int)face->size->metrics.ascender>>6);
|
||||
}
|
||||
if(face->size->metrics.descender==0) {
|
||||
face->size->metrics.descender = (font->font_height<<6) - (face->available_sizes->size) ;
|
||||
printf("modded descender to %i\n",(int)face->size->metrics.descender>>6);
|
||||
font->font_height = (face->size->metrics.ascender >> 6) + (face->size->metrics.descender >> 6);
|
||||
}
|
||||
if(font->lineheight==0) {
|
||||
printf("modded lineheight\n");
|
||||
font->lineheight=font->font_height;
|
||||
}
|
||||
|
||||
printf("Converting\n");
|
||||
|
||||
for(i=0;i<256;i++) {
|
||||
unsigned int glyph_index = FT_Get_Char_Index(face,i);
|
||||
|
||||
retval = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT|FT_LOAD_TARGET_MONO|FT_LOAD_NO_BITMAP);
|
||||
|
||||
glyphslot = face->glyph;
|
||||
|
||||
retval = FT_Render_Glyph(glyphslot, FT_RENDER_MODE_MONO);
|
||||
|
||||
if(retval==0) {
|
||||
|
||||
if(glyph_index>0)
|
||||
font->active[i]=1;
|
||||
else
|
||||
font->active[i]=0;
|
||||
|
||||
int char_x, char_y;
|
||||
|
||||
if(glyphslot->bitmap.width==0)
|
||||
glyphslot->bitmap.width = glyphslot->advance.x >> 6;
|
||||
|
||||
font->glyph[i].width = glyphslot->bitmap.width;
|
||||
|
||||
font->glyph[i].buffer = malloc(glyphslot->bitmap.width*font->font_height);
|
||||
memset(font->glyph[i].buffer,0,glyphslot->bitmap.width*font->font_height);
|
||||
|
||||
|
||||
unsigned char * bufPtr = glyphslot->bitmap.buffer;
|
||||
|
||||
for ( char_y = 0; char_y < glyphslot->bitmap.rows; char_y++)
|
||||
{
|
||||
char y=(face->size->metrics.ascender >> 6) - (face->glyph->metrics.horiBearingY >> 6);
|
||||
|
||||
for ( char_x = 0; char_x < glyphslot->bitmap.width; char_x++)
|
||||
{
|
||||
if((bufPtr[char_x / 8] >> (7 - char_x % 8)) & 1)
|
||||
packpixel(font->glyph[i].buffer,(glyphslot->bitmap.width+7)/8, char_x,char_y+y,1);
|
||||
|
||||
}
|
||||
bufPtr += glyphslot->bitmap.pitch;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FT_Done_Face(face);
|
||||
if(g15r_saveG15Font(oFilename,font)<0) {
|
||||
printf("Problem saving font\n");
|
||||
return -1;
|
||||
}
|
||||
printf("Font saved to %s\n",oFilename);
|
||||
g15r_deleteG15Font(font);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void helptext() {
|
||||
printf("g15fontconvert - (c) 2008 The G15Tools project\n");
|
||||
printf(" -h\t--help\t\t\tThis helptext\n");
|
||||
printf(" -s\t--size [size]\t\tSpecify size in pixels of desired font (default 10pixels high)\n");
|
||||
printf(" -g\t--gap [gap]\t\tSpecify gap in pixels between characters (default 1pixel)\n");
|
||||
printf(" -i\t--infile [filename]\tFilename of font to convert\n");
|
||||
printf(" -o\t--outfile [filename]\tname of file to write (default to [infile].fnt\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int i;
|
||||
int size = 10;
|
||||
int gap = -1;
|
||||
int have_infile=0;
|
||||
int have_outfile=0;
|
||||
char infile[128];
|
||||
char outfile[128];
|
||||
FT_Init_FreeType(&lib);
|
||||
|
||||
if(argc<2)
|
||||
helptext();
|
||||
|
||||
for (i=0;i<argc;i++) {
|
||||
if(0==strncasecmp((char*)argv[i],"-h",2) || 0==strncasecmp((char*)argv[i],"--help",6)){
|
||||
helptext();
|
||||
}
|
||||
if(0==strncasecmp((char*)argv[i],"-s",2) || 0==strncasecmp((char*)argv[i],"--size",6)){
|
||||
if(argv[i+1]!=NULL) {
|
||||
sscanf(argv[++i],"%i",&size);
|
||||
}
|
||||
}
|
||||
if(0==strncasecmp((char*)argv[i],"-g",2) || 0==strncasecmp((char*)argv[i],"--gap",5)){
|
||||
if(argv[i+1]!=NULL) {
|
||||
sscanf(argv[++i],"%i",&gap);
|
||||
}
|
||||
}
|
||||
if(0==strncasecmp((char*)argv[i],"-i",2) || 0==strncasecmp((char*)argv[i],"--infile",8)){
|
||||
if(argv[i+1]!=NULL) {
|
||||
strncpy(infile,argv[++i],127);
|
||||
have_infile=1;
|
||||
}
|
||||
}
|
||||
if(0==strncasecmp((char*)argv[i],"-o",2) || 0==strncasecmp((char*)argv[i],"--outfile",9)){
|
||||
if(argv[i+1]!=NULL) {
|
||||
strncpy(outfile,argv[++i],127);
|
||||
have_outfile=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!have_infile)
|
||||
helptext();
|
||||
|
||||
if(!have_outfile)
|
||||
snprintf(outfile,128,"%s.fnt",infile);
|
||||
if(0!=strncasecmp(".fnt",outfile+(strlen(outfile)-4),4))
|
||||
strncat(outfile,".fnt",128);
|
||||
|
||||
if(access(infile,O_RDONLY)!=0) {
|
||||
printf("g15fontconvert encountered an error:\n");
|
||||
printf("Unable to open %s. Please check the filename and try again.\n",infile);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
printf("converting %s\n",infile);
|
||||
if(convertG15Font(infile, outfile, size,gap)<0)
|
||||
printf("problem saving font %s..\n",outfile);
|
||||
else
|
||||
printf("Done.\n");
|
||||
FT_Done_FreeType(lib);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* Truetype support */
|
||||
+68
-8
@@ -18,6 +18,9 @@ extern "C"
|
||||
#include FT_BITMAP_H
|
||||
#endif
|
||||
|
||||
#define G15R_FONT_SUPPORT 1
|
||||
#define G15R_FONT_API_V2 1
|
||||
|
||||
#define BYTE_SIZE 8
|
||||
#define G15_BUFFER_LEN 1048
|
||||
#define G15_LCD_OFFSET 32
|
||||
@@ -28,10 +31,17 @@ extern "C"
|
||||
#define G15_TEXT_SMALL 0
|
||||
#define G15_TEXT_MED 1
|
||||
#define G15_TEXT_LARGE 2
|
||||
#define G15_TEXT_HUGE 3
|
||||
#define G15_PIXEL_NOFILL 0
|
||||
#define G15_PIXEL_FILL 1
|
||||
#define G15_MAX_FACE 5
|
||||
#define G15_FONT_HEADER_SIZE 15
|
||||
#define G15_CHAR_HEADER_SIZE 4
|
||||
#define G15_MAX_GLYPH 256
|
||||
|
||||
#define G15_JUSTIFY_LEFT 0
|
||||
#define G15_JUSTIFY_CENTER 1
|
||||
#define G15_JUSTIFY_RIGHT 2
|
||||
/** \brief This structure holds the data need to render objects to the LCD screen.*/
|
||||
typedef struct g15canvas
|
||||
{
|
||||
@@ -50,6 +60,36 @@ extern "C"
|
||||
#endif
|
||||
} g15canvas;
|
||||
|
||||
/** \brief Structure holding glyph data for g15render font types */
|
||||
typedef struct g15glyph {
|
||||
/** g15glyph::buffer holds glyph data */
|
||||
unsigned char *buffer;
|
||||
/** g15glyph::width - width of the glyph, without padding */
|
||||
unsigned char width;
|
||||
/** g15glyph::gap - recommended gap between this character and the next */
|
||||
unsigned char gap;
|
||||
}g15glyph;
|
||||
|
||||
/** \brief Structure holding a single font. One g15font struct is needed per size. */
|
||||
typedef struct g15font {
|
||||
/** g15font::font_height - total max height of font in pixels */
|
||||
unsigned int font_height;
|
||||
/** g15font::ascender_height - height in pixels from baseline to the top pixel of an ascender character */
|
||||
unsigned int ascender_height;
|
||||
/** g15font::lineheight - height in pixels from decender to ascender */
|
||||
unsigned int lineheight;
|
||||
/** g15font::numchars - number of glyphs available in this font */
|
||||
unsigned int numchars;
|
||||
/** g15font::glyph - contains all glyphs available in this font */
|
||||
g15glyph glyph[G15_MAX_GLYPH]; // allow 256 chars.. ought to be enough for our purposes
|
||||
/** g15font::default_gap - default gap between glyphs (in pixels). */
|
||||
unsigned int default_gap;
|
||||
/** g15font::active - each active glyph is set to 1 else 0 */
|
||||
unsigned char active[G15_MAX_GLYPH];
|
||||
/** g15font::glyph_buffer memory pool for glyphs */
|
||||
char *glyph_buffer;
|
||||
}g15font;
|
||||
|
||||
/** \brief Fills an area bounded by (x1, y1) and (x2, y2)*/
|
||||
void g15r_pixelReverseFill (g15canvas * canvas, int x1, int y1, int x2,
|
||||
int y2, int fill, int color);
|
||||
@@ -81,6 +121,9 @@ void g15r_drawSprite(g15canvas *canvas, char *buf, int my_x, int my_y, int width
|
||||
char *g15r_loadWbmpToBuf(char *filename, int *img_width, int *img_height);
|
||||
/** \brief Draw a large number*/
|
||||
void g15r_drawBigNum (g15canvas * canvas, unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, int color, int num);
|
||||
/** \brief Draw an XBM image*/
|
||||
void
|
||||
g15r_drawXBM (g15canvas *canvas, unsigned char* data, int width, int height, int pos_x, int pos_y);
|
||||
|
||||
/** \brief Gets the value of the pixel at (x, y)*/
|
||||
int g15r_getPixel (g15canvas * canvas, unsigned int x, unsigned int y);
|
||||
@@ -92,13 +135,6 @@ void g15r_drawBigNum (g15canvas * canvas, unsigned int x1, unsigned int y1, unsi
|
||||
/** \brief Clears the canvas and resets the mode switches*/
|
||||
void g15r_initCanvas (g15canvas * canvas);
|
||||
|
||||
/** \brief Font data for the large (8x8) font*/
|
||||
extern unsigned char fontdata_8x8[];
|
||||
/** \brief Font data for the medium (7x5) font*/
|
||||
extern unsigned char fontdata_7x5[];
|
||||
/** \brief Font data for the small (6x4) font*/
|
||||
extern unsigned char fontdata_6x4[];
|
||||
|
||||
/** \brief Renders a character in the large font at (x, y)*/
|
||||
void g15r_renderCharacterLarge (g15canvas * canvas, int x, int y,
|
||||
unsigned char character, unsigned int sx,
|
||||
@@ -116,9 +152,33 @@ void g15r_drawBigNum (g15canvas * canvas, unsigned int x1, unsigned int y1, unsi
|
||||
int row, int size, unsigned int sx,
|
||||
unsigned int sy);
|
||||
|
||||
/** \brief Load G15 font and return it in g15font struct */
|
||||
g15font * g15r_loadG15Font(char *filename);
|
||||
/** \brief Save font in font struct to given file, return 0 on success */
|
||||
int g15r_saveG15Font(char *oFilename, g15font *font);
|
||||
/** \brief De-allocate memory associated with font */
|
||||
void g15r_deleteG15Font(g15font*font);
|
||||
/** \brief Returns length (in pixels) of string if rendered in font 'font' */
|
||||
int g15r_testG15FontWidth(g15font *font,char *string);
|
||||
/** \brief Returns g15font structure containing the default font at requested size if available */
|
||||
g15font * g15r_requestG15DefaultFont (int size);
|
||||
/** \brief render glyph 'character' from loaded font struct 'font'. Returns width (in pixels) of rendered glyph */
|
||||
int g15r_renderG15Glyph(g15canvas *canvas, g15font *font,
|
||||
unsigned char character,
|
||||
int top_left_pixel_x, int top_left_pixel_y,
|
||||
int colour, int paint_bg);
|
||||
/** \brief Render a string in font 'font' to canvas */
|
||||
void g15r_G15FontRenderString (g15canvas * canvas, g15font *font,
|
||||
char *string,
|
||||
int row, unsigned int sx, unsigned int sy,
|
||||
int colour, int paint_bg);
|
||||
/** \brief Print a string using the G15 default font at size 'size' */
|
||||
void g15r_G15FPrint (g15canvas *canvas, char *string, int x, int y,
|
||||
int size, int center, int colour, int row);
|
||||
|
||||
#ifdef TTF_SUPPORT
|
||||
/** \brief Loads a font through the FreeType2 library*/
|
||||
void g15r_ttfLoad (g15canvas * canvas, char *fontname, int fontsize,
|
||||
int g15r_ttfLoad (g15canvas * canvas, char *fontname, int fontsize,
|
||||
int face_num);
|
||||
/** \brief Prints a string in a given font*/
|
||||
void g15r_ttfPrint (g15canvas * canvas, int x, int y, int fontsize,
|
||||
|
||||
+83
-36
@@ -32,11 +32,11 @@ swap (int *x, int *y)
|
||||
*y = tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* The area with an upper left corner at (x1, y1) and lower right corner at (x2, y2) will be
|
||||
* filled with color if fill>0 or the current contents of the area will be reversed if fill==0.
|
||||
*
|
||||
* \param canvas A pointer to a g15canvas struct in which the buffer to be operated on is found.
|
||||
*
|
||||
* \param canvas A pointer to a g15canvas struct in which the buffer to be operated on is found.
|
||||
* \param x1 Defines leftmost bound of area to be filled.
|
||||
* \param y1 Defines uppermost bound of area to be filled.
|
||||
* \param x2 Defines rightmost bound of area to be filled.
|
||||
@@ -65,7 +65,7 @@ g15r_pixelReverseFill (g15canvas * canvas, int x1, int y1, int x2, int y2,
|
||||
/**
|
||||
* A 1-bit bitmap defined in colormap[] is drawn to the canvas with an upper left corner at (x1, y1)
|
||||
* and a lower right corner at (x1+width, y1+height).
|
||||
*
|
||||
*
|
||||
* \param canvas A pointer to a g15canvas struct in which the buffer to be operated on is found.
|
||||
* \param x1 Defines the leftmost bound of the area to be drawn.
|
||||
* \param y1 Defines the uppermost bound of the area to be drawn.
|
||||
@@ -90,7 +90,7 @@ g15r_pixelOverlay (g15canvas * canvas, int x1, int y1, int width, int height,
|
||||
|
||||
/**
|
||||
* A line of color is drawn from (px1, py1) to (px2, py2).
|
||||
*
|
||||
*
|
||||
* \param canvas A pointer to a g15canvas struct in which the buffer to be operated on is found.
|
||||
* \param px1 X component of point 1.
|
||||
* \param py1 Y component of point 1.
|
||||
@@ -102,7 +102,7 @@ void
|
||||
g15r_drawLine (g15canvas * canvas, int px1, int py1, int px2, int py2,
|
||||
const int color)
|
||||
{
|
||||
/*
|
||||
/*
|
||||
* Bresenham's Line Algorithm
|
||||
* http://en.wikipedia.org/wiki/Bresenham's_algorithm
|
||||
*/
|
||||
@@ -150,10 +150,10 @@ g15r_drawLine (g15canvas * canvas, int px1, int py1, int px2, int py2,
|
||||
|
||||
/**
|
||||
* Draws a box around the area bounded by (x1, y1) and (x2, y2).
|
||||
*
|
||||
*
|
||||
* The box will be filled if fill != 0 and the sides will be thick pixels wide.
|
||||
*
|
||||
* \param canvas A pointer to a g15canvas struct in which the buffer to be operated on is found.
|
||||
*
|
||||
* \param canvas A pointer to a g15canvas struct in which the buffer to be operated on is found.
|
||||
* \param x1 Defines leftmost bound of the box.
|
||||
* \param y1 Defines uppermost bound of the box.
|
||||
* \param x2 Defines rightmost bound of the box.
|
||||
@@ -192,10 +192,10 @@ g15r_pixelBox (g15canvas * canvas, int x1, int y1, int x2, int y2, int color,
|
||||
|
||||
/**
|
||||
* Draws a circle centered at (x, y) with a radius of r.
|
||||
*
|
||||
*
|
||||
* The circle will be filled if fill != 0.
|
||||
*
|
||||
* \param canvas A pointer to a g15canvas struct in which the buffer to be operated on is found.
|
||||
*
|
||||
* \param canvas A pointer to a g15canvas struct in which the buffer to be operated on is found.
|
||||
* \param x Defines horizontal center of the circle.
|
||||
* \param y Defines vertical center of circle.
|
||||
* \param r Defines radius of circle.
|
||||
@@ -240,10 +240,10 @@ g15r_drawCircle (g15canvas * canvas, int x, int y, int r, int fill, int color)
|
||||
|
||||
/**
|
||||
* Draws a rounded box around the area bounded by (x1, y1) and (x2, y2).
|
||||
*
|
||||
*
|
||||
* The box will be filled if fill != 0.
|
||||
*
|
||||
* \param canvas A pointer to a g15canvas struct in which the buffer to be operated on is found.
|
||||
*
|
||||
* \param canvas A pointer to a g15canvas struct in which the buffer to be operated on is found.
|
||||
* \param x1 Defines leftmost bound of the box.
|
||||
* \param y1 Defines uppermost bound of the box.
|
||||
* \param x2 Defines rightmost bound of the box.
|
||||
@@ -325,7 +325,7 @@ g15r_drawRoundBox (g15canvas * canvas, int x1, int y1, int x2, int y2,
|
||||
|
||||
/**
|
||||
* Given a maximum value, and a value between 0 and that maximum value, calculate and draw a bar showing that percentage.
|
||||
*
|
||||
*
|
||||
* \param canvas A pointer to a g15canvas struct in which the buffer to be operated on is found.
|
||||
* \param x1 Defines leftmost bound of the bar.
|
||||
* \param y1 Defines uppermost bound of the bar.
|
||||
@@ -341,8 +341,7 @@ g15r_drawBar (g15canvas * canvas, int x1, int y1, int x2, int y2, int color,
|
||||
int num, int max, int type)
|
||||
{
|
||||
float len, length;
|
||||
int x;
|
||||
if (max <= 0 || num <= 0)
|
||||
if (max <= 0 || num < 0)
|
||||
return;
|
||||
if (num > max)
|
||||
num = max;
|
||||
@@ -382,16 +381,16 @@ g15r_drawBar (g15canvas * canvas, int x1, int y1, int x2, int y2, int color,
|
||||
|
||||
/**
|
||||
* wbmp splash screen loader - assumes image is 160x43
|
||||
*
|
||||
*
|
||||
* \param canvas A pointer to a g15canvas struct in which the buffer to be operated on is found.
|
||||
* \param filename A string holding the path to the wbmp to be displayed.
|
||||
*/
|
||||
int
|
||||
int
|
||||
g15r_loadWbmpSplash(g15canvas *canvas, char *filename)
|
||||
{
|
||||
int width=0, height=0;
|
||||
char *buf;
|
||||
|
||||
|
||||
buf = g15r_loadWbmpToBuf(filename,
|
||||
&width,
|
||||
&height);
|
||||
@@ -401,7 +400,7 @@ g15r_loadWbmpSplash(g15canvas *canvas, char *filename)
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw an icon to a canvas
|
||||
* Draw an icon to a canvas
|
||||
*
|
||||
* \param canvas A pointer to a g15canvas struct in which the buffer to be operated in is found.
|
||||
* \param buf A pointer to the buffer holding the icon to be displayed.
|
||||
@@ -410,15 +409,18 @@ g15r_loadWbmpSplash(g15canvas *canvas, char *filename)
|
||||
* \param width Width of the image in buf.
|
||||
* \param height Height of the image in buf.
|
||||
*/
|
||||
void
|
||||
void
|
||||
g15r_drawIcon(g15canvas *canvas, char *buf, int my_x, int my_y, int width, int height)
|
||||
{
|
||||
int y,x,val;
|
||||
unsigned int pixel_offset = 0;
|
||||
unsigned int byte_offset, bit_offset;
|
||||
|
||||
for (y=0; y < height - 1; y++)
|
||||
for (x=0; x < width - 1; x++)
|
||||
//TODO:http://www.g15tools.com/forum/viewtopic.php?p=314#p314
|
||||
// Does his new piece of code work? Is it better than below? If so, replace.
|
||||
for (y=0; y < height; y++)
|
||||
{
|
||||
for (x=0; x < width; x++)
|
||||
{
|
||||
pixel_offset = y * width + x;
|
||||
byte_offset = pixel_offset / BYTE_SIZE;
|
||||
@@ -427,6 +429,7 @@ g15r_drawIcon(g15canvas *canvas, char *buf, int my_x, int my_y, int width, int h
|
||||
val = (buf[byte_offset] & (1 << bit_offset)) >> bit_offset;
|
||||
g15r_setPixel (canvas, x + my_x, y + my_y, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -442,7 +445,7 @@ g15r_drawIcon(g15canvas *canvas, char *buf, int my_x, int my_y, int width, int h
|
||||
* \param start_y Y offset for reading sprite from buf.
|
||||
* \param total_width Width of the set of sprites held in buf.
|
||||
*/
|
||||
void
|
||||
void
|
||||
g15r_drawSprite(g15canvas *canvas, char *buf, int my_x, int my_y, int width, int height, int start_x, int start_y, int total_width)
|
||||
{
|
||||
int y,x,val;
|
||||
@@ -468,7 +471,7 @@ g15r_drawSprite(g15canvas *canvas, char *buf, int my_x, int my_y, int width, int
|
||||
* \param img_width A pointer to an int that will hold the image width on return.
|
||||
* \param img_height A pointer to an int that will hold the image height on return.
|
||||
*/
|
||||
char *
|
||||
char *
|
||||
g15r_loadWbmpToBuf(char *filename, int *img_width, int *img_height)
|
||||
{
|
||||
int wbmp_fd;
|
||||
@@ -479,14 +482,14 @@ g15r_loadWbmpToBuf(char *filename, int *img_width, int *img_height)
|
||||
unsigned char headerbytes[5];
|
||||
unsigned int pixel_offset = 0;
|
||||
unsigned int byte_offset, bit_offset;
|
||||
|
||||
|
||||
wbmp_fd=open(filename,O_RDONLY);
|
||||
if(!wbmp_fd){
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
retval=read(wbmp_fd,headerbytes,5);
|
||||
|
||||
|
||||
if(retval){
|
||||
if (headerbytes[2] & 1) {
|
||||
*img_width = ((unsigned char)headerbytes[2] ^ 1) | (unsigned char)headerbytes[3];
|
||||
@@ -542,20 +545,21 @@ g15r_loadWbmpToBuf(char *filename, int *img_width, int *img_height)
|
||||
* \param y1 Defines uppermost bound of the number.
|
||||
* \param x2 Defines rightmost bound of the number.
|
||||
* \param y2 Defines bottommost bound of the number.
|
||||
* \param color The number will be drawn this color.
|
||||
* \param num The number to be drawn.
|
||||
*/
|
||||
void
|
||||
*/
|
||||
void
|
||||
g15r_drawBigNum (g15canvas * canvas, unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, int color, int num)
|
||||
{
|
||||
x1 += 2;
|
||||
x2 -= 2;
|
||||
|
||||
|
||||
switch(num){
|
||||
case 0:
|
||||
g15r_pixelBox (canvas, x1, y1, x2, y2 , color, 1, 1);
|
||||
g15r_pixelBox (canvas, x1 +5, y1 +5, x2 -5, y2 - 6, 1 - color, 1, 1);
|
||||
break;
|
||||
case 1:
|
||||
case 1:
|
||||
g15r_pixelBox (canvas, x2-5, y1, x2, y2 , color, 1, 1);
|
||||
g15r_pixelBox (canvas, x1, y1, x2 -5, y2, 1 - color, 1, 1);
|
||||
break;
|
||||
@@ -598,11 +602,11 @@ g15r_drawBigNum (g15canvas * canvas, unsigned int x1, unsigned int y1, unsigned
|
||||
g15r_pixelBox (canvas, x1+5, y1+5, x2-5 , y1+((y2/2)-3), 1 - color, 1, 1);
|
||||
g15r_pixelBox (canvas, x1, y1+((y2/2)+3), x2-5 , y2, 1 - color, 1, 1);
|
||||
break;
|
||||
case 10:
|
||||
case 10:
|
||||
g15r_pixelBox (canvas, x2-5, y1+5, x2, y1+10 , color, 1, 1);
|
||||
g15r_pixelBox (canvas, x2-5, y2-10, x2, y2-5 , color, 1, 1);
|
||||
break;
|
||||
case 11:
|
||||
case 11:
|
||||
g15r_pixelBox (canvas, x1, y1+((y2/2)-2), x2, y1+((y2/2)+2), color, 1, 1);
|
||||
break;
|
||||
case 12:
|
||||
@@ -611,3 +615,46 @@ g15r_drawBigNum (g15canvas * canvas, unsigned int x1, unsigned int y1, unsigned
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw an XBM Image to the canvas
|
||||
*
|
||||
* \param canvas A pointer to a g15canvas struct in which the buffer to be operated in is found.
|
||||
* \param data A pointer to the buffer holding the icon to be displayed.
|
||||
* \param width Width of the image in data.
|
||||
* \param height Height of the image in data.
|
||||
* \param pos_x Leftmost boundary of image.
|
||||
* \param pos_y Topmost boundary of image.
|
||||
*/
|
||||
void
|
||||
g15r_drawXBM (g15canvas *canvas, unsigned char* data, int width, int height, int pos_x, int pos_y)
|
||||
{
|
||||
int y = 0;
|
||||
int z = 0;
|
||||
unsigned char byte;
|
||||
int bytes_per_row = ceil ((double) width / 8);
|
||||
|
||||
int bits_left = width;
|
||||
int current_bit = 0;
|
||||
|
||||
for (y = 0; y < height; y++)
|
||||
{
|
||||
bits_left = width;
|
||||
for (z=0; z < bytes_per_row; z++)
|
||||
{
|
||||
byte = data[(y * bytes_per_row) + z];
|
||||
current_bit = 0;
|
||||
while (current_bit < 8)
|
||||
{
|
||||
if (bits_left > 0)
|
||||
{
|
||||
if ((byte >> current_bit) & 1)
|
||||
{
|
||||
g15r_setPixel(canvas, (current_bit + (z*8) + pos_x),y + pos_y,G15_COLOR_BLACK);
|
||||
}
|
||||
bits_left--;
|
||||
}
|
||||
current_bit++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+382
-89
@@ -17,131 +17,98 @@
|
||||
*/
|
||||
|
||||
#include "libg15render.h"
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
static g15font *defaultfont[40];
|
||||
|
||||
/** Render a character in std large font
|
||||
* \param canvas A pointer to a g15canvas struct in which the buffer to be operated on is found.
|
||||
* \param col size-dependent column to start rendering.
|
||||
* \param row size-dependent row to start rendering.
|
||||
* \param character ascii character to render.
|
||||
* \param sx horizontal top-left pixel location.
|
||||
* \param sy vertical top-left pixel location.
|
||||
*/
|
||||
void
|
||||
g15r_renderCharacterLarge (g15canvas * canvas, int col, int row,
|
||||
unsigned char character, unsigned int sx,
|
||||
unsigned int sy)
|
||||
{
|
||||
int helper = character * 8; /* for our font which is 8x8 */
|
||||
|
||||
int top_left_pixel_x = sx + col * (8); /* 1 pixel spacing */
|
||||
int top_left_pixel_y = sy + row * (8); /* once again 1 pixel spacing */
|
||||
|
||||
int x, y;
|
||||
for (y = 0; y < 8; ++y)
|
||||
{
|
||||
for (x = 0; x < 8; ++x)
|
||||
{
|
||||
char font_entry = fontdata_8x8[helper + y];
|
||||
|
||||
if (font_entry & 1 << (7 - x))
|
||||
g15r_setPixel (canvas, top_left_pixel_x + x, top_left_pixel_y + y,
|
||||
G15_COLOR_BLACK);
|
||||
else
|
||||
g15r_setPixel (canvas, top_left_pixel_x + x, top_left_pixel_y + y,
|
||||
G15_COLOR_WHITE);
|
||||
|
||||
}
|
||||
}
|
||||
unsigned char buf[2];
|
||||
buf[0]=character;
|
||||
buf[1]=0;
|
||||
g15r_G15FPrint (canvas, (char*)buf, ((col * 8) + sx), sy, G15_TEXT_LARGE, 0, G15_COLOR_BLACK, row);
|
||||
}
|
||||
|
||||
/** Render a character in std medium font.
|
||||
* \param canvas A pointer to a g15canvas struct in which the buffer to be operated on is found.
|
||||
* \param col size-dependent column to start rendering.
|
||||
* \param row size-dependent row to start rendering.
|
||||
* \param character ascii character to render.
|
||||
* \param sx horizontal top-left pixel location.
|
||||
* \param sy vertical top-left pixel location.
|
||||
*/
|
||||
void
|
||||
g15r_renderCharacterMedium (g15canvas * canvas, int col, int row,
|
||||
unsigned char character, unsigned int sx,
|
||||
unsigned int sy)
|
||||
{
|
||||
int helper = character * 7 * 5; /* for our font which is 6x4 */
|
||||
unsigned char buf[2];
|
||||
buf[0]=character;
|
||||
buf[1]=0;
|
||||
g15r_G15FPrint (canvas, (char*)buf, ((col * 5) + sx), sy, G15_TEXT_MED, 0, G15_COLOR_BLACK, row);
|
||||
|
||||
int top_left_pixel_x = sx + col * (5); /* 1 pixel spacing */
|
||||
int top_left_pixel_y = sy + row * (7); /* once again 1 pixel spacing */
|
||||
|
||||
int x, y;
|
||||
for (y = 0; y < 7; ++y)
|
||||
{
|
||||
for (x = 0; x < 5; ++x)
|
||||
{
|
||||
char font_entry = fontdata_7x5[helper + y * 5 + x];
|
||||
if (font_entry)
|
||||
g15r_setPixel (canvas, top_left_pixel_x + x, top_left_pixel_y + y,
|
||||
G15_COLOR_BLACK);
|
||||
else
|
||||
g15r_setPixel (canvas, top_left_pixel_x + x, top_left_pixel_y + y,
|
||||
G15_COLOR_WHITE);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Render a character in std small font.
|
||||
* \param canvas A pointer to a g15canvas struct in which the buffer to be operated on is found.
|
||||
* \param col size-dependent column to start rendering.
|
||||
* \param row size-dependent row to start rendering.
|
||||
* \param character ascii character to render.
|
||||
* \param sx horizontal top-left pixel location.
|
||||
* \param sy vertical top-left pixel location.
|
||||
*/
|
||||
void
|
||||
g15r_renderCharacterSmall (g15canvas * canvas, int col, int row,
|
||||
unsigned char character, unsigned int sx,
|
||||
unsigned int sy)
|
||||
{
|
||||
int helper = character * 6 * 4; /* for our font which is 6x4 */
|
||||
unsigned char buf[2];
|
||||
buf[0]=character;
|
||||
buf[1]=0;
|
||||
g15r_G15FPrint (canvas, (char*)buf, ((col * 4) + sx), sy, G15_TEXT_SMALL, 0, G15_COLOR_BLACK, row);
|
||||
|
||||
int top_left_pixel_x = sx + col * (4); /* 1 pixel spacing */
|
||||
int top_left_pixel_y = sy + row * (6); /* once again 1 pixel spacing */
|
||||
|
||||
int x, y;
|
||||
for (y = 0; y < 6; ++y)
|
||||
{
|
||||
for (x = 0; x < 4; ++x)
|
||||
{
|
||||
char font_entry = fontdata_6x4[helper + y * 4 + x];
|
||||
if (font_entry)
|
||||
g15r_setPixel (canvas, top_left_pixel_x + x, top_left_pixel_y + y,
|
||||
G15_COLOR_BLACK);
|
||||
else
|
||||
g15r_setPixel (canvas, top_left_pixel_x + x, top_left_pixel_y + y,
|
||||
G15_COLOR_WHITE);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a string in the designated size
|
||||
* \param canvas A pointer to a g15canvas struct in which the buffer to be operated on is found.
|
||||
* \param stringOut An unsigned char pointer to the string which is to be printed.
|
||||
* \param row size-dependent row to start rendering.
|
||||
* \param size size of printed string. May be 0-3 for standard sizes, or 5-39 in pixel height.
|
||||
* \param sx horizontal top-left pixel location.
|
||||
* \param sy vertical top-left pixel location.
|
||||
*/
|
||||
void
|
||||
g15r_renderString (g15canvas * canvas, unsigned char stringOut[], int row,
|
||||
int size, unsigned int sx, unsigned int sy)
|
||||
{
|
||||
|
||||
int i = 0;
|
||||
for (i; stringOut[i] != 0; ++i)
|
||||
{
|
||||
switch (size)
|
||||
{
|
||||
case G15_TEXT_SMALL:
|
||||
{
|
||||
g15r_renderCharacterSmall (canvas, i, row, stringOut[i], sx, sy);
|
||||
break;
|
||||
}
|
||||
case G15_TEXT_MED:
|
||||
{
|
||||
g15r_renderCharacterMedium (canvas, i, row, stringOut[i], sx, sy);
|
||||
break;
|
||||
}
|
||||
case G15_TEXT_LARGE:
|
||||
{
|
||||
g15r_renderCharacterLarge (canvas, i, row, stringOut[i], sx, sy);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
g15r_G15FPrint (canvas, (char*)stringOut, sx, sy, size, 0, G15_COLOR_BLACK, row);
|
||||
}
|
||||
|
||||
#ifdef TTF_SUPPORT
|
||||
/**
|
||||
* Load a font for use with FreeType2 font support
|
||||
*
|
||||
*
|
||||
* \param canvas A pointer to a g15canvas struct in which the buffer to be operated on is found.
|
||||
* \param fontname Absolute pathname to font file to be loaded.
|
||||
* \param fontsize Size in points for font to be loaded.
|
||||
* \param face_num Slot into which font face will be loaded.
|
||||
* \return 0 for success or FreeType2 errorcode.
|
||||
*/
|
||||
void
|
||||
int
|
||||
g15r_ttfLoad (g15canvas * canvas, char *fontname, int fontsize, int face_num)
|
||||
{
|
||||
int errcode = 0;
|
||||
@@ -173,6 +140,7 @@ g15r_ttfLoad (g15canvas * canvas, char *fontname, int fontsize, int face_num)
|
||||
FT_Set_Char_Size (canvas->ttf_face[face_num][0], 0,
|
||||
canvas->ttf_fontsize[face_num] * 64, 90, 0);
|
||||
}
|
||||
return errcode;
|
||||
}
|
||||
|
||||
int
|
||||
@@ -269,7 +237,7 @@ draw_ttf_str (g15canvas * canvas, char *str, int x, int y, int color,
|
||||
|
||||
/**
|
||||
* Render a string with a FreeType2 font
|
||||
*
|
||||
*
|
||||
* \param canvas A pointer to a g15canvas struct in which the buffer to be operated on is found.
|
||||
* \param x initial x position for string.
|
||||
* \param y initial y position for string.
|
||||
@@ -283,7 +251,6 @@ void
|
||||
g15r_ttfPrint (g15canvas * canvas, int x, int y, int fontsize, int face_num,
|
||||
int color, int center, char *print_string)
|
||||
{
|
||||
int errcode = 0;
|
||||
|
||||
if (canvas->ttf_fontsize[face_num])
|
||||
{
|
||||
@@ -306,5 +273,331 @@ g15r_ttfPrint (g15canvas * canvas, int x, int y, int fontsize, int face_num,
|
||||
draw_ttf_str (canvas, print_string, x, y, color,
|
||||
canvas->ttf_face[face_num][0]);
|
||||
}
|
||||
else { /* fall back to our default bitmap font */
|
||||
g15r_G15FPrint (canvas, print_string, x, y, fontsize, center, color, 0);
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* TTF_SUPPORT */
|
||||
|
||||
/* G15Font Support */
|
||||
|
||||
/**
|
||||
* Load a g15 font from file.
|
||||
* \param filename string containing full name and location of font to load.
|
||||
* \return pointer to completed g15font structure.
|
||||
*/
|
||||
g15font * g15r_loadG15Font(char *filename) {
|
||||
FILE *file;
|
||||
g15font *font = calloc(1,sizeof(g15font));
|
||||
unsigned char buffer[128];
|
||||
int i;
|
||||
if(access(filename,F_OK)!=0) {
|
||||
fprintf(stderr,"loadG15Font: %s doesn't exist or has permissions problem.\n",filename);
|
||||
return NULL;
|
||||
}
|
||||
if(access(filename,R_OK)!=0) {
|
||||
fprintf(stderr,"loadG15Font: %s exists but cannot be read. Permissions problem?\n",filename);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(!(file=fopen(filename,"rb")))
|
||||
return NULL;
|
||||
|
||||
if(fread(buffer,G15_FONT_HEADER_SIZE,1,file)) {
|
||||
if(buffer[0] != 'G' ||
|
||||
buffer[1] != 'F' ||
|
||||
buffer[2] != 'N' ||
|
||||
buffer[3] != 'T' )
|
||||
{
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
} else
|
||||
return NULL;
|
||||
font->font_height = buffer[4] | (buffer[5] << 8);
|
||||
font->ascender_height = buffer[6] | (buffer[7] << 8);
|
||||
font->lineheight = buffer[8] | (buffer[9] << 8);
|
||||
|
||||
/* any future expansion that require more than one bit to be set should be recorded at the end of the file, */
|
||||
/* with the extended-feature bit (not defined as yet, probably 1) set here */
|
||||
/* The first byte of the extended packet should indicate the extension data type (none defined yet) */
|
||||
/* The second byte should indicate length in bytes of the packet to read, not inclusive of these two bytes */
|
||||
/* followed by the extended data. This should allow for a degree of backward compatibility between future versions */
|
||||
/* should they arise. */
|
||||
|
||||
/* features = buffer[10] | (buffer[11] << 8) */
|
||||
|
||||
font->numchars = buffer[12] | (buffer[13] << 8);
|
||||
font->default_gap = buffer[14];
|
||||
unsigned int current_alloc = 2048;
|
||||
char *glyphBuf = malloc(current_alloc);
|
||||
char *glyphPtr = glyphBuf;
|
||||
unsigned int memsize=0;
|
||||
for (i=0;i <font->numchars; i++) {
|
||||
unsigned char charheader[G15_CHAR_HEADER_SIZE];
|
||||
unsigned int character;
|
||||
fread(charheader, G15_CHAR_HEADER_SIZE, 1, file);
|
||||
character = charheader[0] | (charheader[1] << 8);
|
||||
font->glyph[character].width = charheader[2] | (charheader[3] << 8);
|
||||
memsize+=(font->font_height * ((font->glyph[character].width + 7) / 8));
|
||||
if(memsize>current_alloc) { /* attempt to semi-coherently allocate additional memory */
|
||||
current_alloc+=((font->font_height * ((font->glyph[character].width + 7) / 8)*(font->numchars - i)));
|
||||
glyphBuf = realloc(glyphBuf,(size_t)current_alloc);
|
||||
}
|
||||
font->glyph[character].buffer = (unsigned char*)glyphPtr;
|
||||
font->glyph[character].gap = 0;
|
||||
fread(font->glyph[character].buffer, font->font_height * ((font->glyph[character].width + 7) / 8), 1, file);
|
||||
font->active[character] = 1;
|
||||
glyphPtr+=(font->font_height * ((font->glyph[character].width + 7) / 8));
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
return (font);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save g15font struct to given file.
|
||||
* \param oFilename string containing full name and location of font to save.
|
||||
* \param font g15font structure containing glyphs. Glyphs to be saved should have the corresponding active[glyph] set.
|
||||
* \return 0 on success, -1 on failure.
|
||||
*/
|
||||
|
||||
int g15r_saveG15Font(char *oFilename, g15font *font) {
|
||||
FILE *f;
|
||||
unsigned int i;
|
||||
unsigned char fntheader[G15_FONT_HEADER_SIZE];
|
||||
|
||||
if(font==NULL)
|
||||
return -1;
|
||||
|
||||
f = fopen(oFilename, "w+b");
|
||||
if(f==NULL)
|
||||
return -1;
|
||||
|
||||
font->numchars=0;
|
||||
for(i=0;i<G15_MAX_GLYPH;i++) {
|
||||
if(font->active[i]) {
|
||||
font->numchars++;
|
||||
}
|
||||
}
|
||||
|
||||
fntheader[0] = 'G';
|
||||
fntheader[1] = 'F';
|
||||
fntheader[2] = 'N';
|
||||
fntheader[3] = 'T';
|
||||
fntheader[4] = (unsigned char)font->font_height;
|
||||
fntheader[5] = (unsigned char)(font->font_height >> 8);
|
||||
fntheader[6] = (unsigned char)font->ascender_height;
|
||||
fntheader[7] = (unsigned char)(font->ascender_height >> 8);
|
||||
fntheader[8] = (unsigned char)font->lineheight;
|
||||
fntheader[9] = (unsigned char)(font->lineheight >> 8);
|
||||
/* any future expansion that require more than one bit to be set should be recorded at the end of the file, */
|
||||
/* with the extended-feature bit (not defined as yet, probably 1) set here */
|
||||
/* The first byte of the extended packet should indicate the extension data type (none defined yet) */
|
||||
/* The second byte should indicate length in bytes of the packet to read, not inclusive of these two bytes */
|
||||
/* followed by the extended data. This should allow for a degree of backward compatibility between future versions */
|
||||
/* should they arise. */
|
||||
fntheader[10] = 0; /* features */
|
||||
fntheader[11] = 0; /* features >> 8 */
|
||||
fntheader[12] = (unsigned char)font->numchars;
|
||||
fntheader[13] = (unsigned char)(font->numchars >> 8);
|
||||
fntheader[14] = (unsigned char)font->default_gap;
|
||||
|
||||
fwrite (fntheader, G15_FONT_HEADER_SIZE, 1, f);
|
||||
|
||||
for(i=0;i<G15_MAX_GLYPH;i++) {
|
||||
if(font->active[i]) {
|
||||
unsigned char charheader[G15_CHAR_HEADER_SIZE];
|
||||
|
||||
charheader[0] = (unsigned char)i;
|
||||
charheader[1] = (unsigned char)(i >> 8);
|
||||
charheader[2] = (unsigned char)font->glyph[i].width;
|
||||
charheader[3] = (unsigned char)(font->glyph[i].width >> 8);
|
||||
fwrite(charheader,G15_CHAR_HEADER_SIZE,1,f);
|
||||
fwrite(font->glyph[i].buffer,font->font_height * ((font->glyph[i].width + 7) / 8),1,f);
|
||||
}
|
||||
}
|
||||
fclose(f);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* De-allocate memory associated with g15font struct, including glyph buffers
|
||||
* \param font g15font structure containing glyphs.
|
||||
*/
|
||||
void g15r_deleteG15Font(g15font*font){
|
||||
|
||||
if(font) {
|
||||
if(font->glyph_buffer!=NULL)
|
||||
free(font->glyph_buffer);
|
||||
free(font);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate width (in pixels) of given string if rendered in font 'font'.
|
||||
* \param font Loaded g15font structure as returned by g15r_loadG15Font()
|
||||
* \param string Pointer to string for width calculations.
|
||||
* \return total width in pixels of given string.
|
||||
*/
|
||||
int g15r_testG15FontWidth(g15font *font,char *string){
|
||||
int i;
|
||||
int totalwidth=0;
|
||||
if(font==NULL) return 0;
|
||||
|
||||
font->glyph[32].gap = 0;
|
||||
|
||||
for(i=0;i<strlen(string);i++)
|
||||
totalwidth += font->glyph[(int)string[i]].width + font->glyph[(int)string[i]].gap + font->default_gap;
|
||||
|
||||
return totalwidth;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return g15font structure containing the default font at requested size
|
||||
* loading the font if needed.
|
||||
* \param integer pointsize argument in the range of 0-39
|
||||
* \return pointer to g15font struct containing font at requested size or NULL if not valid.
|
||||
*/
|
||||
g15font * g15r_requestG15DefaultFont (int size)
|
||||
{
|
||||
char filename[128];
|
||||
if (size<0) size=0;
|
||||
if (size>39) size=39;
|
||||
|
||||
/* check if previously loaded, otherwise load it now */
|
||||
if(!defaultfont[size]) {
|
||||
snprintf(filename,128,"%s/G15/default-%.2i.fnt",G15FONT_DIR,size);
|
||||
defaultfont[size] = g15r_loadG15Font(filename);
|
||||
if((defaultfont[size])==NULL) {
|
||||
fprintf(stderr,"libg15render: Unable to load font \"%s\"\n",filename);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
return defaultfont[size];
|
||||
}
|
||||
|
||||
/** Render a character in given font.
|
||||
* \param canvas A pointer to a g15canvas struct in which the buffer to be operated on is found.
|
||||
* \param font Loaded g15font structure as returned by g15r_loadG15Font()
|
||||
* \param character ascii character to render.
|
||||
* \param top_left_pixel_x horizontal top-left pixel location.
|
||||
* \param top_left_pixel_y vertical top-left pixel location.
|
||||
* \param colour desired colour of character when rendered.
|
||||
* \param paint_bg should the background of the character cell be painted?
|
||||
*/
|
||||
int g15r_renderG15Glyph(g15canvas *canvas, g15font *font,unsigned char character,int top_left_pixel_x, int top_left_pixel_y, int colour, int paint_bg)
|
||||
{
|
||||
int x,y,w,bp;
|
||||
|
||||
if(font->glyph[character].buffer==NULL || font->active[character] == 0)
|
||||
return 0;
|
||||
|
||||
unsigned char *buffer = font->glyph[character].buffer;
|
||||
unsigned int height = font->font_height;
|
||||
int i = 0;
|
||||
|
||||
int bufferlen = height * ((font->glyph[character].width + 7) / 8);
|
||||
top_left_pixel_y-=font->font_height - font->ascender_height - 1 ;
|
||||
|
||||
w = font->glyph[character].width + (7-(font->glyph[character].width % 8 ));
|
||||
if(font->glyph[character].width%8==0)
|
||||
w = font->glyph[character].width-1;
|
||||
|
||||
x=0;y=0;
|
||||
|
||||
if(paint_bg)
|
||||
g15r_pixelBox (canvas, top_left_pixel_x, top_left_pixel_y-1,
|
||||
top_left_pixel_x + font->glyph[character].width+font->default_gap,
|
||||
top_left_pixel_y + font->lineheight, colour^1, 1, 1);
|
||||
|
||||
for (bp=0;bp<bufferlen ;bp++){
|
||||
for (i=0;i<8;i++) {
|
||||
if( x > w ) {
|
||||
x=0;y++;
|
||||
}
|
||||
x++;
|
||||
if(x<=font->glyph[character].width) {
|
||||
if( buffer[bp] & (0x80 >> i))
|
||||
g15r_setPixel (canvas, top_left_pixel_x + x, top_left_pixel_y + y,colour);
|
||||
else
|
||||
if(paint_bg)
|
||||
g15r_setPixel (canvas, top_left_pixel_x + x, top_left_pixel_y + y,colour^1);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(character!=32)
|
||||
return font->glyph[character].width + font->default_gap;
|
||||
else
|
||||
return font->glyph[character].width;
|
||||
}
|
||||
|
||||
/** Render a string in given font.
|
||||
* \param canvas A pointer to a g15canvas struct in which the buffer to be operated on is found.
|
||||
* \param font Loaded g15font structure as returned by g15r_loadG15Font()
|
||||
* \param string Pointer to string to operate on.
|
||||
* \param row vertical font-dependent row to start printing on. can usually be left at 0
|
||||
* \param sx horizontal top-left pixel location.
|
||||
* \param sy vertical top-left pixel location.
|
||||
* \param colour desired colour of character when rendered.
|
||||
* \param paint_bg if !0, pixels in the glyph background will also be painted, obstructing any image behind the text.
|
||||
*/
|
||||
void g15r_G15FontRenderString (g15canvas * canvas, g15font *font, char *string, int row, unsigned int sx, unsigned int sy, int colour, int paint_bg)
|
||||
{
|
||||
int i=0;
|
||||
int prevwidth=0;
|
||||
if(font==NULL)
|
||||
return;
|
||||
|
||||
sy += ( font->lineheight * row );
|
||||
|
||||
for(i=0;i<strlen(string);i++){
|
||||
prevwidth = g15r_renderG15Glyph (canvas, font,string[i], sx += prevwidth, sy, colour, paint_bg);
|
||||
}
|
||||
}
|
||||
|
||||
/** Render a string in the default font.
|
||||
* \param canvas A pointer to a g15canvas struct in which the buffer to be operated on is found.
|
||||
* \param string Pointer to string to operate on.
|
||||
* \param x horizontal top-left pixel location.
|
||||
* \param y vertical top-left pixel location.
|
||||
* \param size if size>= 4, denotes height in pixels. if size<4, standard font sizes are used.
|
||||
* \param center Desired text justification. 0==left, 1==centered, 2==right justified.
|
||||
* \param colour desired colour of character when rendered.
|
||||
* \param row vertical font-dependent row to start printing on. can usually be left at 0
|
||||
*/
|
||||
/* print string with the default G15Font, with on-demand loading of required sized bitmaps */
|
||||
void g15r_G15FPrint (g15canvas *canvas, char *string, int x, int y, int size, int center, int colour, int row) {
|
||||
int xc, paint_bg;
|
||||
|
||||
/* check if previously loaded, otherwise load it now */
|
||||
if(g15r_requestG15DefaultFont(size)==NULL)
|
||||
return;
|
||||
|
||||
if(size<3)
|
||||
{
|
||||
paint_bg=1;
|
||||
x-=1;
|
||||
y-=1;
|
||||
}
|
||||
else
|
||||
{
|
||||
paint_bg=0;
|
||||
}
|
||||
|
||||
switch(center) {
|
||||
case 0:
|
||||
g15r_G15FontRenderString (canvas, defaultfont[size], string, row, x, y, colour, paint_bg);
|
||||
break;
|
||||
case 1:
|
||||
xc = g15r_testG15FontWidth(defaultfont[size],string);
|
||||
g15r_G15FontRenderString (canvas, defaultfont[size], string, row, 80-(xc/2),y, colour, paint_bg);
|
||||
break;
|
||||
case 2:
|
||||
xc = g15r_testG15FontWidth(defaultfont[size],string);
|
||||
g15r_G15FontRenderString (canvas, defaultfont[size], string, row, 160-xc,y, colour, paint_bg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user