reworked external font matching logic
[swftools.git] / lib / pdf / GFXOutputDev.cc
index 3e956ec..635d10d 100644 (file)
@@ -371,6 +371,11 @@ static char fc_ismatch(FcPattern*match, char*family, char*style)
 }
 #endif
 
+static inline char islowercase(char c) 
+{
+    return (c>='a' && c<='z');
+}
+
 char* fontconfig_searchForFont(char*name)
 {
 #ifdef HAVE_FONTCONFIG
@@ -448,16 +453,18 @@ char* fontconfig_searchForFont(char*name)
            int t;
            int p;
            for(p=0;p<2;p++) {
-               for(t=0;t<set->nfont;t++) {
-                   char*fcfamily=0,*fcstyle=0,*filename=0;
-                   FcBool scalable=FcFalse, outline=FcFalse;
-                   FcPatternGetString(set->fonts[t], "family", 0, (FcChar8**)&fcfamily);
-                   FcPatternGetString(set->fonts[t], "style", 0, (FcChar8**)&fcstyle);
-                   FcPatternGetString(set->fonts[t], "file", 0, (FcChar8**)&filename);
-                   FcPatternGetBool(set->fonts[t], "outline", 0, &outline);
-                   FcPatternGetBool(set->fonts[t], "scalable", 0, &scalable);
-                   if(scalable && outline) {
-                       msg("<trace> %s (%s) -> %s", fcfamily, fcstyle, filename);
+               if(set) {
+                   for(t=0;t<set->nfont;t++) {
+                       char*fcfamily=0,*fcstyle=0,*filename=0;
+                       FcBool scalable=FcFalse, outline=FcFalse;
+                       FcPatternGetString(set->fonts[t], "family", 0, (FcChar8**)&fcfamily);
+                       FcPatternGetString(set->fonts[t], "style", 0, (FcChar8**)&fcstyle);
+                       FcPatternGetString(set->fonts[t], "file", 0, (FcChar8**)&filename);
+                       FcPatternGetBool(set->fonts[t], "outline", 0, &outline);
+                       FcPatternGetBool(set->fonts[t], "scalable", 0, &scalable);
+                       if(scalable && outline) {
+                           msg("<trace> %s (%s) -> %s", fcfamily, fcstyle, filename);
+                       }
                    }
                }
                set =  FcConfigGetFonts(config, FcSetApplication);
@@ -466,20 +473,36 @@ char* fontconfig_searchForFont(char*name)
     }
 
     char*family = strdup(name);
-    char*style = 0;
-    char*dash = strchr(family, '-');
-    if(!dash) dash = strchr(family, ',');
+    int len = strlen(family);
 
+    char*styles[] = {"Medium", "Regular", "Bold", "Italic", "Black", "Narrow"};
+    char*style = 0;
+    int t;
+    for(t=0;t<sizeof(styles)/sizeof(styles[0]);t++) {
+       int l = strlen(styles[t]);
+       if(len>l+1 && !strcmp(family+len-l, styles[t]) && islowercase(family[len-l-1])) {
+           style = styles[t];
+           family[len-l]=0;
+           break;
+       }
+    }
+    if(!style) {
+       char*dash = strchr(family, '-');
+       if(!dash) dash = strchr(family, ',');
+       if(dash) {
+           *dash = 0;
+           style = dash+1;
+       }
+    }
     FcPattern*pattern = 0;
-    if(dash) {
-       *dash = 0;
-       style = dash+1;
+    if(style) {
        msg("<debug> FontConfig: Looking for font %s (family=%s style=%s)", name, family, style);
        pattern = FcPatternBuild(NULL, FC_OUTLINE, FcTypeBool, FcTrue, FC_SCALABLE, FcTypeBool, FcTrue, FC_FAMILY, FcTypeString, family, FC_STYLE, FcTypeString, style, NULL);
     } else {
        msg("<debug> FontConfig: Looking for font %s (family=%s)", name, family);
        pattern = FcPatternBuild(NULL, FC_OUTLINE, FcTypeBool, FcTrue, FC_SCALABLE, FcTypeBool, FcTrue, FC_FAMILY, FcTypeString, family, NULL);
     }
+    pattern = FcPatternBuild(NULL, FC_OUTLINE, FcTypeBool, FcTrue, FC_SCALABLE, FcTypeBool, FcTrue, FC_FAMILY, FcTypeString, family, NULL);
 
     FcResult result;
     FcConfigSubstitute(0, pattern, FcMatchPattern); 
@@ -625,6 +648,7 @@ GFXOutputDev::GFXOutputDev(InfoOutputDev*info, PDFDoc*doc)
     this->config_drawonlyshapes = 0;
     this->config_disable_polygon_conversion = 0;
     this->config_multiply = 1;
+    this->config_detectspaces = 1;
     this->config_linkdatafile = 0;
     this->page2page = 0;
     this->num_pages = 0;
@@ -642,6 +666,8 @@ void GFXOutputDev::setParameter(const char*key, const char*value)
         this->config_transparent = atoi(value);
     } else if(!strcmp(key,"drawonlyshapes")) {
         this->config_drawonlyshapes = atoi(value);
+    } else if(!strcmp(key,"detectspaces")) {
+        this->config_detectspaces = atoi(value);
     } else if(!strcmp(key,"extrafontdata")) {
         this->config_extrafontdata = atoi(value);
     } else if(!strcmp(key,"linkdatafile")) {
@@ -994,7 +1020,8 @@ GBool GFXOutputDev::radialShadedFill(GfxState *state, GfxRadialShading *shading)
            colToByte(color2.c[0]), colToByte(color2.c[1]), colToByte(color2.c[2]));
     infofeature("radial shaded fills");
 
-    gfxgradient_t*g = (gfxgradient_t*)malloc(sizeof(gfxgradient_t)*3);
+    gfxgradient_t gr[3];
+    gfxgradient_t*g = &gr[0];
     g[0].next = &g[1];
     g[1].next = &g[2];
     g[2].next = 0;
@@ -1450,7 +1477,7 @@ void GFXOutputDev::drawChar(GfxState *state, double x, double y,
        (render == RENDER_INVISIBLE)) {
 
        int space = this->current_fontinfo->space_char;
-       if(config_extrafontdata && space>=0 && m.m00 && !m.m01) {
+       if(config_extrafontdata && config_detectspaces && space>=0 && m.m00 && !m.m01) {
            /* space char detection */
            if(last_char_gfxfont == current_gfxfont && 
               last_char_y == m.ty &&
@@ -2415,8 +2442,8 @@ void GFXOutputDev::drawGeneralImage(GfxState *state, Object *ref, Stream *str,
          } else {
              msg("<verbose> resampling %dx%d to mask size (%dx%d)", width, height, maskWidth, maskHeight);
              gfxcolor_t*newpic=new gfxcolor_t[maskWidth*maskHeight];
-             double dx = width / maskWidth;
-             double dy = height / maskHeight;
+             double dx = width / (double)maskWidth;
+             double dy = height / (double)maskHeight;
              double yy = 0;
              for(y = 0; y < maskHeight; y++) {
                  double xx = 0;
@@ -2661,7 +2688,7 @@ void GFXOutputDev::beginTransparencyGroup(GfxState *state, double *bbox,
     this->device = (gfxdevice_t*)rfx_calloc(sizeof(gfxdevice_t));
     dbg("this->device now %p (old: %p)", this->device, states[statepos].olddevice);
 
-    gfxdevice_record_init(this->device);
+    gfxdevice_record_init(this->device, 0);
     
     /*if(!forSoftMask) { ////???
        state->setFillOpacity(0.0);
@@ -2758,7 +2785,7 @@ void GFXOutputDev::setSoftMask(GfxState *state, double *bbox, GBool alpha, Funct
     }
     states[statepos].olddevice = this->device;
     this->device = (gfxdevice_t*)rfx_calloc(sizeof(gfxdevice_t));
-    gfxdevice_record_init(this->device);
+    gfxdevice_record_init(this->device, 0);
 
     dbg("softmaskrecording is %p (dev=%p) at statepos %d\n", states[statepos].softmaskrecording, this->device, statepos);