+
+ msg("<debug> FontConfig: Create \"%s\" Family Pattern", name);
+ pattern = FcPatternBuild(NULL, FC_FAMILY, FcTypeString, name, NULL);
+ if (gfxFont->isItalic()) // check for italic
+ msg("<debug> FontConfig: Adding Italic Slant");
+ FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
+ if (gfxFont->isBold()) // check for bold
+ msg("<debug> FontConfig: Adding Bold Weight");
+ FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
+
+ msg("<debug> FontConfig: Try to match...");
+ // configure and match using the original font name
+ FcConfigSubstitute(0, pattern, FcMatchPattern);
+ FcDefaultSubstitute(pattern);
+ match = FcFontMatch(0, pattern, &result);
+
+ if (FcPatternGetString(match, "family", 0, &v) == FcResultMatch) {
+ msg("<debug> FontConfig: family=%s", (char*)v);
+ // if we get an exact match
+ if (strcmp((char *)v, name) == 0) {
+ if (FcPatternGetString(match, "file", 0, &v) == FcResultMatch) {
+ filename = strdup((char*)v); // mem leak
+ char *nfn = strrchr(filename, '/');
+ if(nfn) fontname = strdup(nfn+1);
+ else fontname = filename;
+ }
+ msg("<debug> FontConfig: Returning \"%s\"", fontname);
+ } else {
+ // initialize patterns
+ FcPatternDestroy(pattern);
+ FcPatternDestroy(match);
+
+ // now match against serif etc.
+ if (gfxFont->isSerif()) {
+ msg("<debug> FontConfig: Create Serif Family Pattern");
+ pattern = FcPatternBuild (NULL, FC_FAMILY, FcTypeString, "serif", NULL);
+ } else if (gfxFont->isFixedWidth()) {
+ msg("<debug> FontConfig: Create Monospace Family Pattern");
+ pattern = FcPatternBuild (NULL, FC_FAMILY, FcTypeString, "monospace", NULL);
+ } else {
+ msg("<debug> FontConfig: Create Sans Family Pattern");
+ pattern = FcPatternBuild (NULL, FC_FAMILY, FcTypeString, "sans", NULL);
+ }
+
+ // check for italic
+ if (gfxFont->isItalic()) {
+ msg("<debug> FontConfig: Adding Italic Slant");
+ int bb = FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
+ }
+ // check for bold
+ if (gfxFont->isBold()) {
+ msg("<debug> FontConfig: Adding Bold Weight");
+ int bb = FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
+ }
+
+ msg("<debug> FontConfig: Try to match... (2)");
+ // configure and match using serif etc
+ FcConfigSubstitute (0, pattern, FcMatchPattern);
+ FcDefaultSubstitute (pattern);
+ match = FcFontMatch (0, pattern, &result);
+
+ if (FcPatternGetString(match, "file", 0, &v) == FcResultMatch) {
+ filename = strdup((char*)v); // mem leak
+ char *nfn = strrchr(filename, '/');
+ if(nfn) fontname = strdup(nfn+1);
+ else fontname = filename;
+ }
+ msg("<debug> FontConfig: Returning \"%s\"", fontname);
+ }
+ }
+
+ //printf("FONTCONFIG: pattern");
+ //FcPatternPrint(pattern);
+ //printf("FONTCONFIG: match");
+ //FcPatternPrint(match);
+
+ FcPatternDestroy(pattern);
+ FcPatternDestroy(match);
+
+ pdfswf_addfont(filename);
+ return fontname;
+#else
+ return 0;
+#endif
+}
+
+char* SWFOutputDev::substituteFont(GfxFont*gfxFont, char* oldname)
+{
+ char*fontname = 0, *filename = 0;
+ msg("<notice> substituteFont(%s)", oldname);
+
+ if(!(fontname = searchForSuitableFont(gfxFont))) {
+ fontname = "Times-Roman";
+ }
+ filename = searchFont(fontname);
+ if(!filename) {
+ msg("<error> Couldn't find font %s- did you install the default fonts?", fontname);
+ return 0;
+ }
+
+ if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
+ msg("<fatal> Too many fonts in file.");
+ exit(1);
+ }
+ if(oldname) {
+ substitutesource[substitutepos] = strdup(oldname); //mem leak
+ substitutetarget[substitutepos] = fontname;
+ msg("<notice> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
+ substitutepos ++;
+ }
+ return strdup(filename); //mem leak
+}
+
+void unlinkfont(char* filename)
+{
+ int l;
+ if(!filename)
+ return;
+ l=strlen(filename);
+ unlink(filename);
+ if(!strncmp(&filename[l-4],".afm",4)) {
+ memcpy(&filename[l-4],".pfb",4);
+ unlink(filename);
+ memcpy(&filename[l-4],".pfa",4);
+ unlink(filename);
+ memcpy(&filename[l-4],".afm",4);
+ return;
+ } else
+ if(!strncmp(&filename[l-4],".pfa",4)) {
+ memcpy(&filename[l-4],".afm",4);
+ unlink(filename);
+ memcpy(&filename[l-4],".pfa",4);
+ return;
+ } else
+ if(!strncmp(&filename[l-4],".pfb",4)) {
+ memcpy(&filename[l-4],".afm",4);
+ unlink(filename);
+ memcpy(&filename[l-4],".pfb",4);
+ return;
+ }
+}
+
+void SWFOutputDev::setXRef(PDFDoc*doc, XRef *xref)
+{
+ this->doc = doc;
+ this->xref = xref;
+}
+
+int SWFOutputDev::setGfxFont(char*id, char*name, char*filename, double maxSize)
+{
+ gfxfont_t*font = 0;
+ fontlist_t*last=0,*l = this->fontlist;
+
+ /* TODO: should this be part of the state? */
+ while(l) {
+ last = l;
+ if(!strcmp(l->id, id)) {
+ current_font_id = l->id;
+ current_gfxfont = l->font;
+ font = l->font;
+ output->addfont(output, id, current_gfxfont);
+ return 1;
+ }
+ l = l->next;
+ }
+ if(!filename) return 0;
+
+ /* A font size of e.g. 9 means the font will be scaled down by
+ 1024 and scaled up by 9. So to have a maximum error of 1/20px,
+ we have to divide 0.05 by (fontsize/1024)
+ */
+ double quality = (1024 * 0.05) / maxSize;
+
+ msg("<verbose> Loading %s...", filename);
+ font = gfxfont_load(filename, quality);
+ msg("<verbose> Font %s loaded successfully", filename);
+
+ l = new fontlist_t;
+ l->font = font;
+ l->filename = strdup(filename);
+ l->id = strdup(id);
+ l->next = 0;
+ current_font_id = l->id;
+ current_gfxfont = l->font;
+ if(last) {
+ last->next = l;
+ } else {
+ this->fontlist = l;
+ }
+ output->addfont(output, id, current_gfxfont);
+ return 1;
+}
+
+void SWFOutputDev::updateFont(GfxState *state)
+{
+ GfxFont*gfxFont = state->getFont();
+
+ if (!gfxFont) {
+ return;
+ }
+
+ char * fontid = getFontID(gfxFont);
+ char * fontname = getFontName(gfxFont);
+
+ double maxSize = 1.0;
+
+ if(this->info) {
+ maxSize = this->info->getMaximumFontSize(fontid);
+ }
+
+ int t;
+ /* first, look if we substituted this font before-
+ this way, we don't initialize the T1 Fonts
+ too often */
+ for(t=0;t<substitutepos;t++) {
+ if(!strcmp(fontid, substitutesource[t])) {
+ free(fontid);fontid=0;
+ fontid = strdup(substitutetarget[t]);
+ break;
+ }
+ }
+
+ /* second, see if this is a font which was used before-
+ if so, we are done */
+ if(setGfxFont(fontid, fontname, 0, 0)) {
+ free(fontid);
+ free(fontname);
+ return;
+ }
+/* if(swfoutput_queryfont(&output, fontid))
+ swfoutput_setfont(&output, fontid, 0);
+
+ msg("<debug> updateFont(%s) [cached]", fontid);
+ return;
+ }*/
+
+ // look for Type 3 font
+ if (gfxFont->getType() == fontType3) {
+ if(!type3Warning) {
+ type3Warning = gTrue;
+ showFontError(gfxFont, 2);