[gl2ps] Impossible to set bold or italic fonts in SVG.

Jean-Baptiste Silvy jean-baptiste.silvy at scilab.org
Tue Feb 24 18:23:45 CET 2009


Jean-Baptiste Silvy a écrit :
> Hello,
>
> I found a bug in SVG export related to fonts. GL2PS is supposed to
> support the 14 Type 1 fonts, such  as "Times-BoldItalic".
> The text export works well for Postscript and PDF outputs with these
> fonts. However, for SVG output the Bold and Italic informations are not
> used.
>
> The problem is that the font name specified in gl2psText is directly
> copied as the "font-family" in the SVG text markup.
> This gives a text markup like this one:
> <text fill="#000000" x="85" y="395" font-size="74"
> font-family="Times-BoldItalic">foo</text>
>
> However, it should be:
> <text fill="#000000" x="85" y="395" font-size="74" font-family="Times"
> font-style="italic" font-weight="bold">foo</text>
>
> A possible solution to fix the bug could be to parse the font name to
> retrieve the 3 SVG properties.
> An other one would be to add more options on gl2psText.
>
> Jean-Baptiste Silvy
>
>
> _______________________________________________
> gl2ps mailing list
> gl2ps at geuz.org
> http://www.geuz.org/mailman/listinfo/gl2ps
>   
Here is a patch which implements the first solution I described above.
It just adds the "font-style" and "font-weight" fields to the text tag.

The function gl2psPrintSVGPrimitive should be replaced by this:

----------------------------------------------------------

static BOOL gl2psIsPSFontContainingOption(const char * fontName, const
char * requestedOption)
{
    /* Return true if the font described by its */
    /* Postscript name has the specified option. */
    /* A Postscript font is made as following */
    /* <fontFamily>-<option1><option2> */
    /* <fontFamily> name of the font family */
    /* <optionI> Bold, Italic or Oblique */
    /* So the requested option must be either "Bold", "Italic" or
"Oblique" */

    /* Search for the beginning of options */
    char * optionString = strchr(fontName, '-');
    if (optionString != NULL)
    {
        /* Some options are defined. */
        /* Check if the one we are looking for is there .*/
        return (strstr(optionString, requestedOption) != NULL);
    }
    return FALSE;
}

static BOOL gl2psIsBoldPSFont(const char * fontName)
{
    return gl2psIsPSFontContainingOption(fontName, "Bold");
}

static BOOL gl2psIsItalicPSFont(const char * fontName)
{
    return gl2psIsPSFontContainingOption(fontName, "Italic");
}

static BOOL gl2psIsObliquePSFont(const char * fontName)
{
    return gl2psIsPSFontContainingOption(fontName, "Oblique");
}


static int gl2psGetPSFontFamilyNameLength(const char * fontName)
{
    return (int) strcspn(fontName, "-");
}

static int gl2psGetPSFontFamilyName(const char * fontName, char *
familyName)
{
    /* Extract the family name of a font from its PS name */
    int familyNameLength = gl2psGetPSFontFamilyNameLength(fontName);
    strncpy(familyName, fontName, familyNameLength);
    /* strncpy doesn't add the null terminating character */
    familyName[familyNameLength] = 0;
}


static void gl2psPrintSVGPrimitive(void *data)
{
   <<<<< SAME AS BEFORE >>>>>>
  case GL2PS_TEXT :
        {
            char * fontName = prim->data.text->fontname; /* Postscript
font name */
            char * fontFamily = NULL; /* Family of the font */
           
            /* Get font options */
            BOOL isItalicFont = gl2psIsItalicPSFont(fontName);
            BOOL isObliqueFont = gl2psIsObliquePSFont(fontName);
            BOOL isBoldFont = gl2psIsBoldPSFont(fontName);

            /* Get font family */
            fontFamily =
gl2psMalloc((gl2psGetPSFontFamilyNameLength(fontName) + 1) * sizeof(char));
            gl2psGetPSFontFamilyName(fontName, fontFamily);
           
            gl2psSVGGetColorString(prim->verts[0].rgba, col);
            gl2psPrintf("<text fill=\"%s\" x=\"%g\" y=\"%g\" "
                            "font-size=\"%d\" font-family=\"%s\" "
                          "font-style=\"%s\"
font-weight=\"%s\">%s</text>\n",
                              col, xyz[0][0], xyz[0][1],
                                prim->data.text->fontsize,
                                  fontName,
                                    (isItalicFont ? "italic" :
(isObliqueFont ? "oblique" : "normal")),
                                    (isBoldFont ? "bold" : "normal"),
                                    prim->data.text->str);
            gl2psFree(fontFamily);
        }
    break;
  case GL2PS_SPECIAL :
<<<<< SAME AS BEFORE >>>>>>
}


----------------------------------------------------------

Jean-Baptiste Silvy