From de57609a2ec9f1fbf3400d4a34eca77a9cb42a19 Mon Sep 17 00:00:00 2001 From: boehme Date: Fri, 2 Nov 2001 11:38:23 +0000 Subject: [PATCH] new examples: dumpfont and text --- lib/example/Makefile | 12 +++-- lib/example/demofont.c | 122 ++++++++++++++++++++++++++++++++++++++++++ lib/example/dumpfont.c | 138 +++++++++++++++++++++++++++++++++++++++++++++++ lib/example/text.c | 139 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 408 insertions(+), 3 deletions(-) create mode 100644 lib/example/demofont.c create mode 100644 lib/example/dumpfont.c create mode 100644 lib/example/text.c diff --git a/lib/example/Makefile b/lib/example/Makefile index b2e9525..6b1787c 100644 --- a/lib/example/Makefile +++ b/lib/example/Makefile @@ -9,7 +9,7 @@ DBFLAGS = -g2 .c.o: $(CC) -c $(CFLAGS) $(DBFLAGS) -o $@ $< -all: jpegtest box shape1 transtest zlibtest sprites buttontest +all: jpegtest box shape1 transtest zlibtest sprites buttontest dumpfont text box: $(RFXSWF) box.o $(CC) -o box box.o $(RFXSWF) $(LDLIBS) $(DBFLAGS) @@ -17,6 +17,9 @@ box: $(RFXSWF) box.o buttontest: $(RFXSWF) buttontest.o $(CC) -o buttontest buttontest.o $(RFXSWF) $(LDLIBS) $(DBFLAGS) +dumpfont: $(RFXSWF) dumpfont.o + $(CC) -o dumpfont dumpfont.o $(RFXSWF) $(LDLIBS) $(DBFLAGS) + jpegtest: $(RFXSWF) jpegtest.o $(CC) -o jpegtest jpegtest.o $(RFXSWF) $(LDLIBS) $(DBFLAGS) @@ -29,13 +32,16 @@ sprites: $(RFXSWF) sprites.o transtest: $(RFXSWF) transtest.o $(CC) -o transtest transtest.o $(RFXSWF) $(LDLIBS) $(DBFLAGS) +text: $(RFXSWF) text.o + $(CC) -o text text.o $(RFXSWF) $(LDLIBS) $(DBFLAGS) + zlibtest: $(RFXSWF) zlibtest.o $(CC) -o zlibtest zlibtest.o $(RFXSWF) $(LDLIBS) $(DBFLAGS) clean: rm -f jpegtest.o box.o shape1.o trastest.o zlibtest.o sprites.o\ - buttontest.o \ + buttontest.o dumpfont.o text.o\ jpegtest.swf box.swf shape1.swf trastest.swf zlibtest.swf \ - sprites.swf buttontest.swf + sprites.swf buttontest.swf text.swf diff --git a/lib/example/demofont.c b/lib/example/demofont.c new file mode 100644 index 0000000..e8b6672 --- /dev/null +++ b/lib/example/demofont.c @@ -0,0 +1,122 @@ +/* demofont.c + + Character data for some glyphs needed by text.c + + Dumped with dumpfont.c + + Part of the swftools package. + + This file is distributed under the GPL, see file COPYING for details + +*/ + +#define addGlyph fn3711 + +void fn3711(SWFFONT * f,int i,U16 code,U16 advance,U16 gid,U8 * data,U32 bitlen) +{ SHAPE * s; + U32 l = (bitlen+7)/8; + + if (FAILED(swf_ShapeNew(&s))) return; + s->data = malloc(l); + if (!s->data) { swf_ShapeFree(s); return; } + + f->codes[i] = code; + f->glyph[i].advance = advance; + f->glyph[i].gid = gid; + f->glyph[i].shape = s; + s->bitlen = bitlen; + s->bits.fill = 1; + memcpy(s->data,data,l); +} + +U8 Glyphs_Demo_Font[] = { + 0x35, 0x66, 0xc2, 0xd5, 0xe3, 0x9b, 0x62, 0x08, + 0xea, 0x09, 0xe2, 0x4c, 0xf8, 0xba, 0x59, 0x2d, + 0x93, 0x1d, 0xe4, 0xc3, 0x6b, 0x40, 0x92, 0x90, + 0x30, 0x36, 0x4c, 0x0c, 0xe5, 0xa9, 0xbe, 0x46, + 0x40, 0x1a, 0xf5, 0x50, 0xec, 0x3b, 0x6a, 0xa4, + 0xd8, 0x8a, 0xe1, 0x4b, 0x66, 0x00, 0x40, 0x0e, + 0x11, 0x91, 0x35, 0x9f, 0x03, 0x51, 0x4e, 0x8a, + 0x5c, 0x17, 0x7e, 0x8d, 0xda, 0x0a, 0x48, 0xa0, + 0x08, 0xed, 0x22, 0x1b, 0x0f, 0x7c, 0xa1, 0xb7, + 0x80, 0x9f, 0xc3, 0x69, 0xec, 0x53, 0xea, 0xdc, + 0xd8, 0x59, 0xd1, 0x8a, 0x60, 0x0b, 0xc1, 0x6d, + 0x09, 0x4b, 0x51, 0x5b, 0x8e, 0x55, 0xb8, 0x9c, + 0x80, 0x95, 0xa0, 0x05, 0x88, 0xa4, 0x75, 0x70, + 0x1c, 0x8c, 0x0f, 0x9b, 0x40, 0x35, 0x62, 0xfa, + 0x9c, 0xf9, 0x53, 0x04, 0xa0, 0x15, 0x86, 0x1d, + 0x19, 0x9c, 0xd3, 0xca, 0x34, 0x3a, 0x40, 0x1a, + 0x29, 0x6e, 0x47, 0xf4, 0x6e, 0xd6, 0xaf, 0xd4, + 0x6c, 0xc9, 0xa3, 0x28, 0x7e, 0x20, 0x2b, 0x70, + 0xcc, 0x13, 0x00, 0x59, 0x7e, 0x77, 0xe9, 0xd6, + 0x7d, 0x36, 0x75, 0xae, 0xf8, 0x8d, 0x3c, 0x5d, + 0xbd, 0x11, 0xc8, 0xd0, 0x5c, 0x00, 0x35, 0x47, + 0x8c, 0x93, 0x92, 0x50, 0x26, 0x42, 0x5c, 0xa4, + 0x37, 0xa7, 0x97, 0x82, 0x80, 0x0f, 0xa6, 0x00, + 0x64, 0x28, 0x48, 0x98, 0x9d, 0x25, 0x04, 0x02, + 0x56, 0x20, 0x0f, 0xd8, 0x98, 0x83, 0x60, 0x02, + 0x7a, 0x60, 0x08, 0x6c, 0xbb, 0xa9, 0x6e, 0xd0, + 0x92, 0x00, 0x15, 0x12, 0x92, 0x3c, 0xac, 0xf1, + 0x39, 0x01, 0x33, 0x40, 0x01, 0xfa, 0x7c, 0xc6, + 0xc8, 0x60, 0x0b, 0x12, 0x80, 0x7b, 0x89, 0xfc, + 0xb8, 0xdf, 0xcc, 0x41, 0x2c, 0xc4, 0x10, 0x40, + 0x4d, 0x30, 0x01, 0x87, 0x5d, 0x36, 0x6d, 0xd0, + 0x05, 0x54, 0xa0, 0x20, 0x9f, 0x85, 0x29, 0xf8, + 0x2c, 0xf1, 0x80, 0x35, 0x4a, 0x44, 0x53, 0xdd, + 0x4e, 0x68, 0x61, 0x37, 0x6d, 0xb7, 0x17, 0x34, + 0xb8, 0xb2, 0xd2, 0x01, 0x2d, 0xd0, 0x1a, 0xc9, + 0xc9, 0x52, 0x40, 0x2f, 0x20, 0x0e, 0x20, 0xa4, + 0xa4, 0x0a, 0x2f, 0x2f, 0xb5, 0x24, 0x92, 0x66, + 0x63, 0x10, 0x05, 0x19, 0x40, 0x3f, 0xa2, 0x9a, + 0x5a, 0x09, 0xa3, 0x00, 0xdb, 0x41, 0xe3, 0xa6, + 0x5e, 0xe3, 0xa0, 0x34, 0xdd, 0x0b, 0xf7, 0x6b, + 0xba, 0x08, 0xcc, 0x1c, 0x8c, 0x4e, 0x47, 0x2a, + 0x84, 0x8c, 0x40, 0x4a, 0x88, 0x01, 0x6d, 0x92, + 0x2d, 0x58, 0x12, 0x4a, 0x02, 0xee, 0x8d, 0x33, + 0x73, 0xf4, 0x5c, 0x6e, 0x4d, 0x6f, 0xb9, 0xd5, + 0xc7, 0x2e, 0x9c, 0x80, 0x2a, 0x4a, 0x02, 0x42, + 0x7b, 0x52, 0xa7, 0xb4, 0xf4, 0x06, 0xd4, 0x20, + 0xbd, 0x68, 0x84, 0x67, 0x67, 0x33, 0xae, 0x3a, + 0x08, 0x00, 0x35, 0x62, 0x96, 0xd0, 0x77, 0x52, + 0xdb, 0x0d, 0x7a, 0xa8, 0xf6, 0x25, 0x78, 0x52, + 0x99, 0x40, 0x2d, 0x1a, 0x3e, 0x43, 0x50, 0x50, + 0x09, 0x10, 0x01, 0x0d, 0xa4, 0x3f, 0x52, 0x6c, + 0xd0, 0x4e, 0x5d, 0xec, 0x70, 0x19, 0x97, 0x01, + 0x9b, 0xc0, 0x24, 0xa4, 0x0a, 0xb3, 0x92, 0xac, + 0xf5, 0xa6, 0x5e, 0xda, 0x00, 0x4e, 0xe1, 0xb3, + 0x36, 0x2d, 0xf4, 0x70, 0x91, 0xcd, 0x5d, 0x9a, + 0x53, 0x99, 0x4b, 0xdc, 0xda, 0x41, 0x77, 0x98, + 0xf0, 0x00, 0x35, 0x48, 0x34, 0x6d, 0xdc, 0x6d, + 0x68, 0xa7, 0x2d, 0xc0, 0x3c, 0x46, 0x49, 0xb1, + 0x00, 0x23, 0xa4, 0x45, 0x78, 0x8d, 0x08, 0x3e, + 0xf3, 0x0c, 0x0b, 0x58, 0xaa, 0xba, 0x76, 0xbf, + 0x5d, 0xaf, 0x1a, 0x36, 0xee, 0x24, 0x34, 0x53, + 0x96, 0xa8, 0x6d, 0x6d, 0x78, 0xec, 0xec, 0x56, + 0x85, 0x9c, 0x78, 0xdb, 0x73, 0x8e, 0xf6, 0x92, + 0x68, 0x5b, 0xc7, 0x6d, 0x9f, 0xe5, 0xe5, 0x73, + 0x97, 0x39, 0x32, 0xce, 0x2e, 0xb4, 0x6d, 0xdc, + 0x60, 0x68, 0xa7, 0xae, 0xe8, 0x98, 0xf0, 0x80, + 0x3a, 0x40, 0x0f, 0x21, 0x6e, 0x23, 0x12, 0x11, + 0xbc, 0xc7, 0x29, 0xdb, 0xcd, 0x7b, 0x26, 0x91, + 0x59, 0xa6, 0xba, 0xfe, 0x34, 0x6d, 0x00, 0x00 }; + +SWFFONT * Font_Demo_Font(U16 id) +{ SWFFONT * f; + int i; + + if (!(f=malloc(sizeof(SWFFONT)))) return NULL; + memset(f,0x00,sizeof(SWFFONT)); + f->id = id; + f->name = strdup("Demo Font"); + f->flags = 0x10; + + addGlyph(f,102, 0x00, 64, 0, &Glyphs_Demo_Font[0x0000], 872); // f + addGlyph(f,108, 0x00, 53, 1, &Glyphs_Demo_Font[0x006d], 452); // l + addGlyph(f,111, 0x00, 96, 2, &Glyphs_Demo_Font[0x00a6], 743); // o + addGlyph(f,115, 0x00, 74, 3, &Glyphs_Demo_Font[0x0103], 951); // s + addGlyph(f,116, 0x00, 53, 4, &Glyphs_Demo_Font[0x017a], 570); // t + addGlyph(f,119, 0x00, 138, 5, &Glyphs_Demo_Font[0x01c2], 806); // w + return f; +} + +#undef addGlyph diff --git a/lib/example/dumpfont.c b/lib/example/dumpfont.c new file mode 100644 index 0000000..01ffa9b --- /dev/null +++ b/lib/example/dumpfont.c @@ -0,0 +1,138 @@ +/* dumpfont.c + + Example for font handling + + This tool dumps font information into C files + which can be included to rfxswf applications. + This is an easy way for developers to use + fonts in applications without dealing with + external files. + + Usage: ./dumpfont filename.swf > myfont.c + + Limits: does not parse ST_DEFINEFONT2 + + Part of the swftools package. + + Copyright (c) 2000, 2001 Rainer Böhme + + This file is distributed under the GPL, see file COPYING for details + +*/ + +#include +#include +#include +#include "../rfxswf.h" + +#define PRINTABLE(a) ((a>='A')&&(a<='Z'))||((a>='a')&&(a<='z'))||((a>='0')&&(a<='9')) + +SWF swf; + +void DumpFont(SWFFONT * f,char * name) +{ int gpos[MAX_CHAR_PER_FONT]; + int n,i; + + // Glyph Shapes Data + + n = 0; + printf("U8 Glyphs_%s[] = {\n\t ",name); + memset(&gpos,0x00,sizeof(gpos)); + + for (i=0;iglyph[i].shape) + { SHAPE * s = f->glyph[i].shape; + int j,l = (s->bitlen+7)/8; + gpos[i] = n; + + for (j=0;jdata[j]); + if ((n&7)==7) printf("\n\t "); + n++; + } + } + printf("0x00 };\n"); + + // SWFFONT function + + printf("\nSWFFONT * Font_%s(U16 id)\n",name); + printf("{ SWFFONT * f;\n int i;\n\n"); + printf(" if (!(f=malloc(sizeof(SWFFONT)))) return NULL;\n"); + printf(" memset(f,0x00,sizeof(SWFFONT));\n"); + printf(" f->id = id;\n"); + printf(" f->name = strdup(\"%s\");\n",f->name); + printf(" f->flags = 0x%02x;\n\n",f->flags); + + for (i=0;iglyph[i].shape) + { printf(" addGlyph(f,%3i, 0x%02x,%4i,%3i, &Glyphs_%s[0x%04x],%4i); // %c\n", + i, f->codes[i], f->glyph[i].advance, f->glyph[i].gid, name, gpos[i], + f->glyph[i].shape->bitlen,(i!='\\')?i:0x20); + } + + printf(" return f;\n}\n\n"); +} + +void DumpGlobal(char * funcname) +{ printf("\nvoid %s(SWFFONT * f,int i,U16 code,U16 advance,U16 gid,U8 * data,U32 bitlen)\n",funcname); + printf("{ SHAPE * s;\n U32 l = (bitlen+7)/8;\n\n"); + printf(" if (FAILED(swf_ShapeNew(&s))) return;\n"); + printf(" s->data = malloc(l);\n"); + printf(" if (!s->data) { swf_ShapeFree(s); return; }\n\n"); + printf(" f->codes[i] = code;\n"); + printf(" f->glyph[i].advance = advance;\n"); + printf(" f->glyph[i].gid = gid;\n"); + printf(" f->glyph[i].shape = s;\n"); + printf(" s->bitlen = bitlen;\n"); + printf(" s->bits.fill = 1;\n"); + printf(" memcpy(s->data,data,l);\n}\n\n"); +} + + +void fontcallback(U16 id,U8 * name) +{ SWFFONT * font; + + int f; + char s[500],*ss; + + swf_FontExtract(&swf,id,&font); + sprintf(s,"%s%s%s",name,swf_FontIsBold(font)?"_bold":"",swf_FontIsItalic(font)?"_italic":""); + + ss = s; + while(ss[0]) + { if ((*ss)==0x20) (*ss)='_'; + ss++; + } + + DumpFont(font,s); + + swf_FontFree(font); +} + +int main(int argc,char ** argv) +{ int f; + + if (argc>1) + { f = open(argv[1],O_RDONLY); + if (f>=0) + { if FAILED(swf_ReadSWF(f,&swf)) + { fprintf(stderr,"%s is not a valid SWF file or contains errors.\n",argv[1]); + close(f); + } + else + { char fn[200]; + close(f); + sprintf(fn,"fn%04x",getpid()); // avoid name conflicts @ using multiple fonts + printf("#define addGlyph %s\n",fn); + DumpGlobal(fn); + swf_FontEnumerate(&swf,&fontcallback); + swf_FreeTags(&swf); + printf("#undef addGlyph\n"); + } + } else fprintf(stderr,"File not found: %s\n",argv[1]); + } + else fprintf(stderr,"\nreflex SWF Font Dump Utility\n(w) 2000 by Rainer Boehme \n\nUsage: dumpfont filename.swf\n"); + + return 0; +} + diff --git a/lib/example/text.c b/lib/example/text.c new file mode 100644 index 0000000..fb19f7e --- /dev/null +++ b/lib/example/text.c @@ -0,0 +1,139 @@ +/* text.c + + Example for including and using fonts + + Part of the swftools package. + + Copyright (c) 2001 Rainer Böhme + + This file is distributed under the GPL, see file COPYING for details + +*/ + +#include +#include +#include +#include "../rfxswf.h" + +#include "demofont.c" // five glyphs only: f, l, o, s, w + +/* + Due to copyright reasons we don't include full typesets into + the swftools package. But you can easily create fontdumps + of your desired typo: + + * Create a swf file with all characters of your desired fonts + (with any tool that can output swf files) + + * use the dumpfont example in this directory to dump font code + + * include dump code and adjust Font_() calls. + + Note: pdf2swf (Version 0.1.0) doesn't write ST_DEFINEFONTINFO tags, + so you can't extract fonts out of documents made with pdf2swf. + +*/ + +#define BANNER_TEXT "swftools" +#define ID_FONT 2000 +#define ID_BANNER 2001 + +int main(int argc, char ** argv) +{ SWF swf; + TAG * t; + SRECT r; + RGBA rgb; + U8 abits, gbits; + + int f; + int width = 170; + int height = 60; + + int points = 50; // <- change global text size here + + int textsize = points*20; // adjust height + int textscale = points*10; // adjust spacing + + FONTUSAGE use; + SWFFONT * font = Font_Demo_Font(ID_FONT); // change font name here + + swf_FontInitUsage(&use); + swf_FontUse(&use,BANNER_TEXT); // SWF reduces font information to the used glyphs + swf_FontReduce(font,&use); + + memset(&swf,0x00,sizeof(SWF)); + + swf.fileVersion = 4; + swf.frameRate = 0x4000; + swf.movieSize.xmax = 20*width; + swf.movieSize.ymax = 20*height; + + swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR); + t = swf.firstTag; + + rgb.r = 0xff; + rgb.g = 0xff; + rgb.b = 0xff; + swf_SetRGB(t,&rgb); + + t = swf_InsertTag(t,ST_DEFINEFONT); + + swf_FontSetDefine(t,font); + + t = swf_InsertTag(t,ST_DEFINEFONTINFO); + + swf_FontSetInfo(t,font); + + t = swf_InsertTag(t,ST_DEFINETEXT); + + swf_SetU16(t,ID_BANNER); // ID + + r.xmin = 0; + r.ymin = 0; + r.xmax = swf_TextGetWidth(font,BANNER_TEXT,textscale); + r.ymax = textsize; + + swf_SetRect(t,&r); + + swf_SetMatrix(t,NULL); + + swf_TextCountBits(font,BANNER_TEXT,textscale,&gbits,&abits); + + swf_SetU8(t,gbits); + swf_SetU8(t,abits); + + rgb.r = 0x00; + rgb.g = 0x00; + rgb.b = 0x00; + + swf_TextSetInfoRecord(t,font,textsize,&rgb,0,textsize); + swf_TextSetCharRecord(t,font,BANNER_TEXT,textscale,gbits,abits); + + swf_SetU8(t,0); + + + t = swf_InsertTag(t,ST_PLACEOBJECT2); + + swf_ObjectPlace(t,ID_BANNER,1,NULL,NULL,NULL); + + t = swf_InsertTag(t,ST_SHOWFRAME); + + t = swf_InsertTag(t,ST_END); + +// swf_WriteCGI(&swf); + + + f = open("text.swf",O_RDWR|O_CREAT|O_TRUNC,0644); + if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n"); + close(f); + + swf_FreeTags(&swf); + +#ifdef __NT__ + system("start ..\\text.swf"); +#endif + + return 0; +} + + -- 1.7.10.4