some fixes for font in proba.pdf.
[swftools.git] / pdf2swf / ttf2pt1 / ft.c
index 9b65785..12f44c6 100644 (file)
@@ -5,6 +5,7 @@
  *
  */
 
+#include "../../config.h"
 #ifdef USE_FREETYPE
 
 #include <stdio.h>
@@ -14,7 +15,7 @@
 #include <sys/types.h>
 #include <freetype/freetype.h>
 #include <freetype/ftglyph.h>
-#include <freetype/ftnames.h>
+#include <freetype/ftsnames.h>
 #include <freetype/ttnameid.h>
 #include <freetype/ftoutln.h>
 #include "pt1.h"
@@ -51,7 +52,7 @@ struct frontsw freetype_sw = {
 
 /* statics */
 
-static char * dupcnstring( char *s, int len);
+static char * dupcnstring( unsigned char *s, int len);
 
 static FT_Library library;
 static FT_Face face;
@@ -153,10 +154,9 @@ glnames(
 
        for(i=0; i < face->num_glyphs; i++) {
                if( FT_Get_Glyph_Name(face, i, bf, MAX_NAMELEN) || bf[0]==0 ) {
-                       sprintf(bf, "_%d", i);
+                       sprintf(bf, "_g_%d", i);
                        WARNING_2 fprintf(stderr,
-                               "**** Glyph No. %d has no postscript name, becomes %s ****\n",
-                               i, bf);
+                               "Glyph No. %d has no postscript name, becomes %s\n", i, bf);
                }
                glyph_list[i].name = strdup(bf);
                if(ISDBG(FT)) fprintf(stderr, "%d has name %s\n", i, bf);
@@ -308,9 +308,8 @@ glenc(
                e = 0;
        }
        
-       if( FT_Set_Charmap(face, face->charmaps[e]) ) {
+       if( !face->charmaps || FT_Set_Charmap(face, face->charmaps[e]) ) {
                fprintf(stderr, "**** Cannot set charmap in FreeType ****\n");
-               exit(1);
        }
 
 populate_map:
@@ -338,19 +337,29 @@ populate_map:
 /* duplicate a string with counter to a 0-terminated string */
 static char *
 dupcnstring(
-       char *s,
+       unsigned char *s,
        int len
 )
 {
-       char *res;
+       char *res, *out;
+       int i, c;
+       static int warned=0;
 
        if(( res = malloc(len+1) )==NULL) {
                fprintf (stderr, "****malloc failed %s line %d\n", __FILE__, __LINE__);
                exit(255);
        }
 
-       memcpy(res, s, len);
-       res[len] = 0;
+       out = res;
+       for(i=0; i<len; i++) {
+               if(( c=s[i] )>=' ' && c!=127)
+                       *out++ = c;
+               else if(!warned) {
+                       warned=1;
+                       WARNING_1 fprintf(stderr, "Some font name strings are in Unicode, may not show properly\n");
+               }
+       }
+       *out = 0;
        return res;
 }
 
@@ -405,6 +414,10 @@ fnmetrics(
        {
                int len;
 
+               if(!fm->name_family)
+                   fm->name_family = "";
+               if(!fm->name_style)
+                   fm->name_style= "";
                len = strlen(fm->name_family) + strlen(fm->name_style) + 2;
                if(( fm->name_full = malloc(len) )==NULL) {
                        fprintf (stderr, "****malloc failed %s line %d\n", __FILE__, __LINE__);
@@ -441,6 +454,9 @@ fnmetrics(
        } else
                fm->name_ps = dupcnstring(sn.string, sn.string_len);
 #endif /* ENABLE_SFNT */
+       for(i=0; fm->name_ps[i]!=0; i++)
+               if(fm->name_ps[i] == ' ')
+                       fm->name_ps[i] = '_'; /* no spaces in the Postscript name */
 
        /* guess the boldness from the font names */
        fm->force_bold=0;
@@ -450,14 +466,15 @@ fnmetrics(
        fieldstocheck[2] = fm->name_ps;
 
        for(i=0; !fm->force_bold && i<sizeof fieldstocheck /sizeof(fieldstocheck[0]); i++) {
+               int j;
                str=fieldstocheck[i];
-               for(i=0; str[i]!=0; i++) {
-                       if( (str[i]=='B'
-                               || str[i]=='b' 
-                                       && ( i==0 || !isalpha(str[i-1]) )
+               for(j=0; str[j]!=0; j++) {
+                       if( (str[j]=='B'
+                               || str[j]=='b' 
+                                       && ( j==0 || !isalpha(str[j-1]) )
                                )
-                       && !strncmp("old",&str[i+1],3)
-                       && !islower(str[i+4])
+                       && !strncmp("old",&str[j+1],3)
+                       && !islower(str[j+4])
                        ) {
                                fm->force_bold=1;
                                break;