"Fossies" - the Fresh Open Source Software Archive

Member "PDFlib-Lite-7.0.5p3/libs/font/ft_corefont.c" (6 Jun 2012, 11034 Bytes) of package /linux/misc/old/PDFlib-Lite-7.0.5p3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ft_corefont.c" see the Fossies "Dox" file reference documentation.

    1 /*---------------------------------------------------------------------------*
    2  |              PDFlib - A library for generating PDF on the fly             |
    3  +---------------------------------------------------------------------------+
    4  | Copyright (c) 1997-2006 Thomas Merz and PDFlib GmbH. All rights reserved. |
    5  +---------------------------------------------------------------------------+
    6  |                                                                           |
    7  |    This software is subject to the PDFlib license. It is NOT in the       |
    8  |    public domain. Extended versions and commercial licenses are           |
    9  |    available, please check http://www.pdflib.com.                         |
   10  |                                                                           |
   11  *---------------------------------------------------------------------------*/
   12 
   13 /* $Id: ft_corefont.c,v 1.22.2.3 2008/01/08 18:39:09 kurt Exp $
   14  *
   15  * FONT in-core and basic font metric functions
   16  *
   17  */
   18 
   19 #define FT_COREFONT_C
   20 
   21 #include "ft_font.h"
   22 #include "ft_corefont.h"
   23 
   24 
   25 /* ------------------------------ core fonts ------------------------------ */
   26 
   27 /* PDF basic font names */
   28 static const char *fnt_base14_names[] =
   29 {
   30   "Courier",
   31   "Courier-Bold",
   32   "Courier-Oblique",
   33   "Courier-BoldOblique",
   34   "Helvetica",
   35   "Helvetica-Bold",
   36   "Helvetica-Oblique",
   37   "Helvetica-BoldOblique",
   38   "Symbol",
   39   "Times-Roman",
   40   "Times-Bold",
   41   "Times-Italic",
   42   "Times-BoldItalic",
   43   "ZapfDingbats"
   44 };
   45 
   46 pdc_bool
   47 fnt_is_standard_font(const char *fontname)
   48 {
   49     int slot;
   50 
   51     for (slot = 0;
   52          slot < (int)(sizeof(fnt_base14_names) / sizeof(fnt_base14_names[0]));
   53          slot++)
   54     {
   55         if (!strcmp(fnt_base14_names[slot], fontname))
   56             return pdc_true;
   57     }
   58 
   59     return pdc_false;
   60 }
   61 
   62 /* abbreviations of PDF basic font names for form fields */
   63 static const char *fnt_abb_base14_names[] =
   64 {
   65   "Cour",
   66   "CoBo",
   67   "CoOb",
   68   "CoBO",
   69   "Helv",
   70   "HeBo",
   71   "HeOb",
   72   "HeBO",
   73   "Symb",
   74   "TiRo",
   75   "TiBo",
   76   "TiIt",
   77   "TiBI",
   78   "ZaDb"
   79 };
   80 
   81 const char *
   82 fnt_get_abb_std_fontname(const char *fontname)
   83 {
   84     int slot;
   85 
   86     for (slot = 0;
   87          slot < (int)(sizeof(fnt_base14_names) / sizeof(fnt_base14_names[0]));
   88          slot++)
   89     {
   90         if (!strcmp(fnt_base14_names[slot], fontname))
   91             return fnt_abb_base14_names[slot];
   92     }
   93 
   94     return NULL;
   95 }
   96 
   97 
   98 /* Basic fonts core metrics */
   99 static const fnt_font_metric *fnt_base_font_metrics[] =
  100 {
  101     &fnt_font_metric_01,
  102     &fnt_font_metric_02,
  103     &fnt_font_metric_03,
  104     &fnt_font_metric_04,
  105     &fnt_font_metric_05,
  106     &fnt_font_metric_06,
  107     &fnt_font_metric_07,
  108     &fnt_font_metric_08,
  109     &fnt_font_metric_09,
  110     &fnt_font_metric_10,
  111     &fnt_font_metric_11,
  112     &fnt_font_metric_12,
  113     &fnt_font_metric_13,
  114     &fnt_font_metric_14
  115 };
  116 
  117 const fnt_font_metric *
  118 fnt_get_core_metric(const char *fontname)
  119 {
  120 #ifdef PDF_BUILTINMETRIC_SUPPORTED
  121     const fnt_font_metric *metric = NULL;
  122     int slot;
  123 
  124     for (slot = 0;
  125          slot < (int)(sizeof(fnt_base_font_metrics) /
  126                       sizeof(fnt_base_font_metrics[0]));
  127          slot++)
  128     {
  129         metric = fnt_base_font_metrics[slot];
  130         if (!strcmp(metric->name, fontname))
  131             return metric;
  132     }
  133 #endif /* PDF_BUILTINMETRIC_SUPPORTED */
  134     return(NULL);
  135 }
  136 
  137 
  138 /* --------------------- Pre-installed CID fonts ---------------------- */
  139 
  140 int
  141 fnt_get_preinstalled_cidfont(const char *fontname,
  142                              const fnt_font_metric **fontmetric)
  143 {
  144     int slot;
  145 
  146     for (slot = 0; slot < FNT_NUM_OF_CIDFONTS; slot++)
  147     {
  148         if (!strcmp(fnt_cid_metrics[slot].name, fontname))
  149         {
  150             if (fontmetric)
  151                 *fontmetric = &fnt_cid_metrics[slot];
  152             return fnt_cid_metrics[slot].charcoll;
  153         }
  154     }
  155 
  156     if (fontmetric)
  157         *fontmetric = NULL;
  158 
  159     return (int) cc_none;
  160 }
  161 
  162 /* abbreviations of PDF basic CJK font names for form fields */
  163 static const char *fnt_abb_cjk_names[] =
  164 {
  165   "KaGo",
  166   "KaMi",
  167   "HyGo",
  168   "HySm",
  169   "MHei",
  170   "MSun",
  171   "STSo",
  172 };
  173 
  174 const char *
  175 fnt_get_abb_cjk_fontname(const char *fontname)
  176 {
  177     int slot;
  178 
  179     for (slot = 0;
  180          slot < (int)(sizeof(fnt_abb_cjk_names) / sizeof(fnt_abb_cjk_names[0]));
  181          slot++)
  182     {
  183         if (!strcmp(fnt_cid_metrics[slot].name, fontname))
  184             return fnt_abb_cjk_names[slot];
  185     }
  186 
  187     return NULL;
  188 }
  189 
  190 #ifdef PDF_CJKFONTWIDTHS_SUPPORTED
  191 
  192 const char **
  193 fnt_get_cid_widths_array(pdc_core *pdc, fnt_font *font)
  194 {
  195     int slot, slotm;
  196 
  197     (void) pdc;
  198 
  199     /* search for font name */
  200     slotm = 100;
  201     for (slot = 0; slot < slotm; slot += FNT_CIDMETRIC_INCR)
  202     {
  203         if (!strcmp(fnt_cid_width_arrays[slot], font->name))
  204             break;
  205     }
  206 
  207     return &fnt_cid_width_arrays[slot + 1];  /* skip font name */
  208 }
  209 
  210 static void
  211 fnt_parse_cid_widths(pdc_core *pdc, fnt_font *font)
  212 {
  213     static const char fn[] = "fnt_parse_cid_widths";
  214     int slot, slota, slotm;
  215     const char *chunk;
  216     char **strlist = NULL, **sstrlist = NULL, *str;
  217     int cid = 0, cidfirst, cidlast, width;
  218     int il, is, ns, nss = 0;
  219     int wformat = 2;
  220 
  221     /* search for font name */
  222     slotm = 100;
  223     for (slot = 0; slot < slotm; slot += FNT_CIDMETRIC_INCR)
  224     {
  225         if (!strcmp(fnt_cid_width_arrays[slot], font->name))
  226             break;
  227     }
  228     if (slot == slotm)
  229         return;
  230 
  231     /* we take the maximum */
  232     font->m.numwidths = fnt_get_maxcid(font->m.charcoll, -1) + 1;
  233     font->m.widths = (int *) pdc_malloc(pdc,
  234                                  font->m.numwidths * sizeof(int), fn);
  235 
  236     slota = slot + 1;                       /* skip font name  */
  237     slotm = slot + FNT_CIDMETRIC_INCR;
  238     for (slot = slota; slot < slotm; slot++)
  239     {
  240         chunk = fnt_cid_width_arrays[slot];
  241 
  242         ns = pdc_split_stringlist(pdc, chunk, " \n", 0, &strlist);
  243         for (is = 0; is < ns; is++)
  244         {
  245             str = strlist[is];
  246 
  247             /* check for next format 1 chunk */
  248             if (wformat == 2 && strchr(str, '['))
  249             {
  250                 nss = pdc_split_stringlist(pdc, str, " [", 0, &sstrlist);
  251                 str = sstrlist[0];
  252                 pdc_str2integer(str, 0, &cidfirst);
  253                 for (; cid < cidfirst; cid++)
  254                     font->m.widths[cid] = FNT_DEFAULT_CIDWIDTH;
  255                 str = sstrlist[1];
  256                 wformat = 1;
  257             }
  258 
  259             /* format 1:  cid [width_1 width_2 ... width_n] */
  260             if (wformat == 1)
  261             {
  262                 il = (int) strlen(str) - 1;
  263                 if (str[il] == ']')
  264                 {
  265                     str[il] = 0;
  266                     wformat = 2;
  267                 }
  268 
  269                 pdc_str2integer(str, 0, &font->m.widths[cid]);
  270                 cid++;
  271 
  272                 if (nss)
  273                 {
  274                     pdc_cleanup_stringlist(pdc, sstrlist);
  275                     nss = 0;
  276                 }
  277             }
  278             else
  279             {
  280                 /* format 2:  cid_first cid_last width */
  281                 pdc_str2integer(str, 0, &cidfirst);
  282                 is++;
  283                 str = strlist[is];
  284                 pdc_str2integer(str, 0, &cidlast);
  285                 is++;
  286                 str = strlist[is];
  287                 pdc_str2integer(str, 0, &width);
  288 
  289                 for (; cid < cidfirst; cid++)
  290                     font->m.widths[cid] = FNT_DEFAULT_CIDWIDTH;
  291                 for (; cid <= cidlast; cid++)
  292                     font->m.widths[cid] = width;
  293             }
  294         }
  295 
  296         pdc_cleanup_stringlist(pdc, strlist);
  297     }
  298 
  299     for (; cid < font->m.numwidths; cid++)
  300         font->m.widths[cid] = FNT_DEFAULT_CIDWIDTH;
  301 
  302     if (pdc_logg_is_enabled(pdc, 5, trc_font))
  303     {
  304         for (cid = 0; cid < font->m.numwidths; cid++)
  305             pdc_logg(pdc, "\t\t\tCID width[%d]: %d\n",
  306                      cid, font->m.widths[cid]);
  307     }
  308 }
  309 
  310 #endif /* PDF_CJKFONTWIDTHS_SUPPORTED */
  311 
  312 
  313 /* ------------------------- general functions -------------------------- */
  314 
  315 /*
  316  * Fill font metric struct from core metric struct
  317  */
  318 void
  319 fnt_fill_font_metric(pdc_core *pdc, fnt_font *font, pdc_bool kerning,
  320                      const fnt_font_metric *metric)
  321 {
  322     static const char fn[] = "fnt_fill_font_metric";
  323 
  324     (void) kerning;
  325 
  326     /* Fill font metric struct. Font struct must be initialized */
  327     font->m = *metric;
  328     font->m.charcoll = abs(font->m.charcoll);
  329     font->m.name = pdc_strdup(pdc, metric->name);
  330     font->name = pdc_strdup(pdc, metric->name);
  331 
  332     /* Fill glyph widths array (double mapping Unicode <-> code <-> width) */
  333     if (font->m.numglwidths)
  334     {
  335         font->m.glw = (fnt_glyphwidth *) pdc_calloc(pdc,
  336                           metric->numglwidths * sizeof(fnt_glyphwidth), fn);
  337         memcpy(font->m.glw, metric->glw,
  338                metric->numglwidths * sizeof(fnt_glyphwidth));
  339     }
  340 
  341     /* Fill glyph width array (mapping Unicode interval <-> width) */
  342     if (metric->numinters)
  343     {
  344         /* We must convert */
  345         if (font->m.type == fnt_Type1)
  346         {
  347             int i, j, iw, iwe;
  348             pdc_ushort uv;
  349 
  350             for (i = 0; i < metric->numinters; i++)
  351             {
  352                 if (i && metric->ciw[i-1].width != 0)
  353                     font->m.numglwidths += metric->ciw[i].startcode -
  354                                            metric->ciw[i-1].startcode;
  355             }
  356             font->m.glw = (fnt_glyphwidth *) pdc_calloc(pdc,
  357                               font->m.numglwidths * sizeof(fnt_glyphwidth), fn);
  358 
  359             j = 0;
  360             iw = 0;
  361             for (i = 0; i < metric->numinters; i++)
  362             {
  363                 if (i && metric->ciw[j].width != 0)
  364                 {
  365                     uv = metric->ciw[j].startcode;
  366                     iwe = iw + metric->ciw[i].startcode - uv;
  367                     for (; iw < iwe; iw++)
  368                     {
  369                         font->m.glw[iw].unicode = uv;
  370                         font->m.glw[iw].width = metric->ciw[j].width;
  371                         uv++;
  372                     }
  373                 }
  374                 j = i;
  375             }
  376             font->m.numinters = 0;
  377             font->m.ciw = NULL;
  378         }
  379         else
  380         {
  381             font->m.ciw = (fnt_interwidth *) pdc_calloc(pdc,
  382                                 font->m.numinters * sizeof(fnt_interwidth), fn);
  383             memcpy(font->m.ciw, metric->ciw,
  384                    metric->numinters * sizeof(fnt_interwidth));
  385         }
  386     }
  387 
  388 #ifdef PDF_CJKFONTWIDTHS_SUPPORTED
  389     /* Fill glyph width array (mapping CID -> width) */
  390     if (font->m.type == fnt_CIDFontType0)
  391         fnt_parse_cid_widths(pdc, font);
  392 #endif /* PDF_CJKFONTWIDTHS_SUPPORTED */
  393 
  394     /* Number of glyphs */
  395     if (font->m.type == fnt_Type1)
  396         font->numglyphs = font->m.numglwidths;
  397 
  398 
  399     /* font weight */
  400     font->weight = fnt_stemv2weight(font->m.StdVW);
  401 
  402     /* standard Adobe font */
  403     font->isstdfont = pdc_true;
  404 
  405     /* symbol font */
  406     if (!(font->m.flags & FNT_SYMBOL))
  407         font->issymbfont = pdc_false;
  408 }
  409 
  410 
  411