+/* SWFFont: copy all t1 font outlines to a local
+ array. */
+SWFFont::SWFFont(char*name, int id, char*filename)
+{
+ if(!T1_GetFontName(id))
+ T1_LoadFont(id);
+
+ this->name = strdup(T1_GetFontFileName(id));
+ this->fontid = strdup(name);
+ this->t1id = id;
+
+ char**a= T1_GetAllCharNames(id);
+ int t, outlinepos=0;
+ char*map[256];
+
+ t=0;
+ while(a[t])
+ t++;
+ this->charnum = t;
+
+ if(!charnum)
+ return;
+ logf("<verbose> Font %s(%d): Storing %d outlines.\n", name, id, charnum);
+
+ this->standardtablesize = 256;
+ if(this->charnum < this->standardtablesize)
+ this->standardtablesize = this->charnum;
+ this->standardtable = (char**)malloc(standardtablesize*sizeof(char*));
+
+ for(t = 0; t < this->standardtablesize; t++) {
+ char*name = T1_GetCharName(id,t);
+ if(!name)
+ name = "";
+ standardtable[t] = strdup(name);
+ }
+
+ outline = (T1_OUTLINE**)malloc(charnum*sizeof(T1_OUTLINE*));
+ charname = (char**)malloc(charnum*sizeof(char*));
+ width = (int*)malloc(charnum*sizeof(int));
+ memset(width, 0, charnum*sizeof(int));
+ memset(charname, 0, charnum*sizeof(char*));
+ used = (char*)malloc(charnum*sizeof(char));
+ char2swfcharid = (U16*)malloc(charnum*2);
+ swfcharid2char = (U16*)malloc(charnum*2);
+ swfcharpos = 0;
+
+ memset(used,0,charnum*sizeof(char));
+
+ this->swfid = ++currentswfid;
+
+ t=0;
+ while(*a)
+ {
+ map[t] = *a;
+ a++;
+ t++;
+ if(t==256 || !*a) {
+ int s;
+ for(s=t;s<256;s++)
+ map[s] = ".notdef";
+
+ int ret = T1_ReencodeFont(id, map);
+ if(ret) {
+ T1_DeleteFont(id);
+ T1_LoadFont(id);
+ int ret = T1_ReencodeFont(id, map);
+ if(ret)
+ fprintf(stderr,"Can't reencode font: (%s) ret:%d\n",filename, ret);
+ }
+
+ // parsecharacters
+ for(s=0;s<t;s++)
+ {
+ char* name = T1_GetCharName(id, s);
+ if(!name) name = "";
+ this->outline[outlinepos] = T1_CopyOutline(T1_GetCharOutline(id, s, 100.0, 0));
+ this->width[outlinepos] = T1_GetCharWidth(id, s);
+ this->charname[outlinepos] = strdup(name);
+ outlinepos++;
+ }
+ t=0;
+ }
+ }
+}
+
+/* free all tables, write out definefont tags */
+SWFFont::~SWFFont()
+{
+ int t,usednum=0;
+ int*ptr;
+
+ if(storeallcharacters)
+ {
+ int t;
+ for(t=0;t<this->charnum;t++)
+ {
+ if(this->charname[t])
+ getSWFCharID(this->charname[t], -1);
+ }
+ }
+
+ ptr = (int*)malloc(swfcharpos*sizeof(int));
+
+ for(t=0;t<charnum;t++)
+ if(used[t]) usednum++;
+
+ if(usednum && !drawonlyshapes)
+ {
+ logf("<verbose> Font %s has %d used characters",fontid, usednum);
+ TAG*ftag = swf_InsertTag(swf.firstTag,ST_DEFINEFONT);
+ swf_SetU16(ftag, this->swfid);
+ int initpos = swf_GetTagLen(ftag);
+ swfmatrix m;
+ m.m11 = m.m22 = 1;
+ m.m21 = m.m12 = 0;
+ m.m13 = CHARMIDX;
+ m.m23 = CHARMIDY;
+
+ for(t=0;t<swfcharpos;t++)
+ {
+ ptr[t] = swf_GetTagLen(ftag);
+ swf_SetU16(ftag, 0x1234);
+ }
+ for(t=0;t<swfcharpos;t++)
+ {
+ *(U16*)&ftag->data[ptr[t]] =
+ SWAP16(swf_GetTagLen(ftag)-initpos);
+
+ swflastx=0;
+ swflasty=0;
+ swf_SetU8(ftag,0x10); //0 fill bits, 0 linestyle bits
+ SHAPE s;
+ s.bits.fill = 1;
+ s.bits.line = 0;
+ swf_ShapeSetStyle(ftag,&s,0,1,0);
+ int lastfill = fill;
+ fill = 1;
+ storefont = 1;
+ drawpath(ftag, outline[swfcharid2char[t]],&m, 0);
+ storefont = 0;
+ fill = lastfill;
+ swf_ShapeSetEnd(ftag);
+ }
+ ftag = swf_InsertTag(ftag,ST_DEFINEFONTINFO);
+ swf_SetU16(ftag, this->swfid);
+ if(this->fontid) {
+ swf_SetU8(ftag, strlen(this->fontid));
+ swf_SetBlock(ftag, (U8*)this->fontid, strlen(this->fontid));
+ } else {
+ swf_SetU8(ftag, 0);
+ }
+ swf_SetU8(ftag, 0); //flags
+ for(t=0;t<swfcharpos;t++)
+ {
+ int s;
+ char * name = this->charname[this->swfcharid2char[t]];
+ for(s=0;s<256;s++) {
+ if(standardEncodingNames[s] &&
+ !strcmp(name,standardEncodingNames[s]))
+ break;
+ }
+ swf_SetU8(ftag, (U8)s);
+ }
+ }
+
+ free(ptr);
+ free(outline);
+ for(t=0;t<charnum;t++)
+ free(charname[t]);
+ for(t=0;t<standardtablesize;t++)
+ if(standardtable[t]) {
+ free(standardtable[t]);
+ }
+ free(standardtable);
+ free(charname);
+ free(width);
+ free(used);
+ free(swfcharid2char);
+ free(char2swfcharid);
+}
+
+T1_OUTLINE*SWFFont::getOutline(char*name)
+{
+ int t;
+ for(t=0;t<this->charnum;t++) {
+ if(!strcmp(this->charname[t],name)) {
+ if(!used[t])
+ {
+ swfcharid2char[swfcharpos] = t;
+ char2swfcharid[t] = swfcharpos;
+ swfcharpos++;
+ used[t] = 1;
+ }
+ return outline[t];
+ }
+ }
+ return 0;
+}
+
+int SWFFont::getWidth(char*name)
+{
+ int t;
+ for(t=0;t<this->charnum;t++) {
+ if(!strcmp(this->charname[t],name)) {
+ return this->width[t];
+ }
+ }
+ return 0;
+}
+
+int SWFFont::getSWFCharID(char*name, int charnr)
+{
+ int t;
+ for(t=0;t<this->charnum;t++) {
+ if(!strcmp(this->charname[t],name)) {
+ if(!used[t])
+ {
+ swfcharid2char[swfcharpos] = t;
+ char2swfcharid[t] = swfcharpos++;
+ used[t] = 1;
+ }
+ return char2swfcharid[t];
+ }
+ }
+ if(this->standardtable && charnr>=0 && charnr < this->standardtablesize) {
+ return getSWFCharID(this->standardtable[charnr], -1);
+ }
+ logf("<warning> Didn't find character '%s' in font '%s'", name, this->name);
+ return 0;
+}
+
+char*SWFFont::getName()
+{
+ return this->name;
+}
+
+struct fontlist_t
+{
+ SWFFont * font;
+ fontlist_t*next;
+} *fontlist = 0;