src/drv_try.c

/* [<][>]
[^][v][top][bottom][index][help] */

FUNCTIONS

This source file includes following functions.
  1. VF_Init_Driver_Try
  2. try_create
  3. try_close
  4. release_mem
  5. try_get_metric1
  6. try_get_fontbbx1
  7. try_get_bitmap1
  8. try_get_outline
  9. try_get_metric2
  10. try_get_fontbbx2
  11. try_get_bitmap2
  12. try_get_font_prop
  13. try_query_font_type
  14. debug_on

   1 /*
   2  * drv_try.c - A font driver that try to open a font among listed fonts.
   3  *
   4  * by Hirotsugu Kakugawa
   5  *
   6  * 16 Jul 1998  First implementation
   7  * 24 Nov 1998  Added get_fontbbx1() and get_fontbbx2().
   8  *
   9  */
  10 /*
  11  * Copyright (C) 1998  Hirotsugu Kakugawa. 
  12  * All rights reserved.
  13  *
  14  * This file is part of the VFlib Library.  This library is free
  15  * software; you can redistribute it and/or modify it under the terms of
  16  * the GNU Library General Public License as published by the Free
  17  * Software Foundation; either version 2 of the License, or (at your
  18  * option) any later version.  This library is distributed in the hope
  19  * that it will be useful, but WITHOUT ANY WARRANTY; without even the
  20  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  21  * PURPOSE.  See the GNU Library General Public License for more details.
  22  * You should have received a copy of the GNU Library General Public
  23  * License along with this library; if not, write to the Free Software
  24  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  25  */
  26 
  27 #include  "config.h"
  28 #include  <stdio.h>
  29 #include  <stdlib.h>
  30 #ifdef HAVE_UNISTD_H
  31 #  include <unistd.h>
  32 #endif
  33 #include  <ctype.h>
  34 #include  "VFlib-3_6.h"
  35 #include  "VFsys.h"
  36 #include  "vflibcap.h"
  37 #include  "cache.h"
  38 #include  "sexp.h"
  39 #include  "str.h"
  40 #include  "path.h"
  41 #include  "try.h"
  42 
  43 struct s_font_try {
  44   char     *font_name;
  45   double   point_size;
  46   double   pixel_size;
  47   double   mag;
  48   double   dpi_x, dpi_y;
  49   int      fid;
  50   SEXP     props;
  51 };
  52 typedef struct s_font_try  *FONT_TRY;
  53 
  54 
  55 Private SEXP_ALIST   default_properties;
  56 Private SEXP_ALIST   default_variables;
  57 Private SEXP_STRING  default_debug_mode;
  58 
  59 
  60 Private int         try_create(VF_FONT,char*,char*,int,SEXP);
  61 Private int         try_close(VF_FONT);
  62 Private int         try_get_metric1(VF_FONT,long,VF_METRIC1,double,double);
  63 Private int         try_get_metric2(VF_FONT,long,VF_METRIC2,double,double);
  64 Private int         try_get_fontbbx1(VF_FONT,double,double,
  65                                      double*,double*,double*,double*);
  66 Private int         try_get_fontbbx2(VF_FONT,double,double, 
  67                                      int*,int*,int*,int*);
  68 Private VF_BITMAP   try_get_bitmap1(VF_FONT,long,double,double);
  69 Private VF_BITMAP   try_get_bitmap2(VF_FONT,long,double,double);
  70 Private VF_OUTLINE  try_get_outline(VF_FONT,long,double,double);
  71 Private char*       try_get_font_prop(VF_FONT,char*);
  72 Private int         try_query_font_type(VF_FONT,long);
  73 Private void        release_mem(FONT_TRY);
  74 Private int         debug_on(char type);
  75 
  76 
  77 Public int
  78 VF_Init_Driver_Try(void)
     /* [<][>][^][v][top][bottom][index][help] */
  79 {
  80   struct s_capability_table  ct[10];
  81   int  z;
  82 
  83   z = 0;
  84   /* VF_CAPE_PROPERTIES */
  85   ct[z].cap = VF_CAPE_PROPERTIES;      ct[z].type = CAPABILITY_ALIST;
  86   ct[z].ess = CAPABILITY_OPTIONAL;     ct[z++].val = &default_properties;
  87   /* VF_CAPE_VARIABLE_VALUES */
  88   ct[z].cap = VF_CAPE_VARIABLE_VALUES; ct[z].type = CAPABILITY_ALIST;
  89   ct[z].ess = CAPABILITY_OPTIONAL;     ct[z++].val = &default_variables;
  90   /* VF_CAPE_DEBUG */
  91   ct[z].cap = VF_CAPE_DEBUG;           ct[z].type = CAPABILITY_STRING;
  92   ct[z].ess = CAPABILITY_OPTIONAL;     ct[z++].val = &default_debug_mode;
  93   /* end */
  94   ct[z].cap = NULL; ct[z].type = 0; ct[z].ess = 0; ct[z++].val = NULL;
  95 
  96 
  97   if (vf_cap_GetParsedClassDefault(FONTCLASS_NAME_TRY, ct, NULL, NULL) 
  98       == VFLIBCAP_PARSED_ERROR)
  99     return -1;
 100 
 101   VF_InstallFontDriver(FONTCLASS_NAME_TRY, 
 102                        (DRIVER_FUNC_TYPE)try_create);
 103 
 104   return 0;
 105 }
 106 
 107 
 108 Private int
 109 try_create(VF_FONT font, char *font_class,
     /* [<][>][^][v][top][bottom][index][help] */
 110            char *font_name, int implicit, SEXP entry)
 111 {
 112   FONT_TRY    font_try;
 113   SEXP        cap_fontlist, cap_point, cap_pixel;
 114   SEXP        cap_mag, cap_dpi, cap_props;
 115   SEXP        s;
 116   char        *fname = NULL;
 117   struct s_capability_table  ct[10];
 118   int  z;
 119 
 120   z = 0;
 121   /* VF_CAPE_FONT_CLASS */
 122   ct[z].cap = VF_CAPE_FONT_CLASS;      ct[z].type = CAPABILITY_STRING;
 123   ct[z].ess = CAPABILITY_ESSENTIAL;    ct[z++].val = NULL;
 124   /* VF_CAPE_TRY_FONT_LIST */
 125   ct[z].cap = VF_CAPE_TRY_FONT_LIST;   ct[z].type = CAPABILITY_STRING_LIST1;
 126   ct[z].ess = CAPABILITY_OPTIONAL;     ct[z++].val = &cap_fontlist;
 127   /* VF_CAPE_POINT_SIZE */
 128   ct[z].cap = VF_CAPE_POINT_SIZE;      ct[z].type = CAPABILITY_STRING;
 129   ct[z].ess = CAPABILITY_OPTIONAL;     ct[z++].val = &cap_point;
 130   /* VF_CAPE_PIXEL_SIZE */
 131   ct[z].cap = VF_CAPE_PIXEL_SIZE;      ct[z].type = CAPABILITY_STRING;
 132   ct[z].ess = CAPABILITY_OPTIONAL;     ct[z++].val = &cap_pixel;
 133   /* VF_CAPE_DPI */
 134   ct[z].cap = VF_CAPE_DPI;             ct[z].type = CAPABILITY_STRING;
 135   ct[z].ess = CAPABILITY_OPTIONAL;     ct[z++].val = &cap_dpi;
 136   /* VF_CAPE_MAG */
 137   ct[z].cap = VF_CAPE_MAG;             ct[z].type = CAPABILITY_STRING;
 138   ct[z].ess = CAPABILITY_OPTIONAL;     ct[z++].val = &cap_mag;
 139   /* VF_CAPE_PROPERTIES */
 140   ct[z].cap = VF_CAPE_PROPERTIES;      ct[z].type = CAPABILITY_ALIST;
 141   ct[z].ess = CAPABILITY_OPTIONAL;     ct[z++].val = &cap_props;
 142   /* end */
 143   ct[z].cap = NULL; ct[z].type = 0; ct[z].ess = 0; ct[z++].val = NULL;
 144 
 145 
 146   /* No support for implicit fonts */
 147   if (implicit == 1)  
 148     return -1;
 149 
 150   /* Only supports explicit fonts */
 151   if (vf_cap_GetParsedFontEntry(entry, font_name, ct, default_variables, NULL) 
 152       == VFLIBCAP_PARSED_ERROR)
 153     return -1;
 154 
 155   font->font_type       = -1;  /* Use try_query_font_type() */
 156   font->get_metric1     = try_get_metric1;
 157   font->get_metric2     = try_get_metric2;
 158   font->get_fontbbx1    = try_get_fontbbx1;
 159   font->get_fontbbx2    = try_get_fontbbx2;
 160   font->get_bitmap1     = try_get_bitmap1;
 161   font->get_bitmap2     = try_get_bitmap2;
 162   font->get_outline     = try_get_outline;
 163   font->get_font_prop   = try_get_font_prop;
 164   font->query_font_type = try_query_font_type;
 165   font->close           = try_close;
 166 
 167   ALLOC_IF_ERR(font_try, struct s_font_try){
 168     vf_error = VF_ERR_NO_MEMORY;
 169     return -1;
 170   }
 171   if ((font_try->font_name = vf_strdup(font_name)) == NULL){
 172     vf_error = VF_ERR_NO_MEMORY;
 173     vf_free(font_try);
 174     return -1;
 175   }
 176 
 177   font_try->point_size = -1;
 178   font_try->pixel_size = -1;
 179   font_try->dpi_x      = -1;
 180   font_try->dpi_y      = -1;
 181   font_try->mag        = 1.0;
 182   font_try->fid        = -1;
 183   font_try->props      = NULL;
 184 
 185   if (cap_point != NULL)
 186     font_try->point_size = atof(vf_sexp_get_cstring(cap_point));
 187   if (cap_pixel != NULL)
 188     font_try->pixel_size = atof(vf_sexp_get_cstring(cap_pixel));
 189   if (cap_dpi != NULL){
 190     font_try->dpi_x =atof(vf_sexp_get_cstring(cap_dpi));
 191     font_try->dpi_y = atof(vf_sexp_get_cstring(cap_dpi));
 192   }
 193   if (cap_props != NULL)
 194     font_try->props = cap_props;
 195 
 196   if (font->point_size >= 0)
 197     font_try->point_size = font->point_size;
 198   if ((font->dpi_x >= 0) && (font->dpi_y >= 0)){
 199     font_try->dpi_x = font->dpi_x;
 200     font_try->dpi_y = font->dpi_y;
 201   }
 202   if (font->pixel_size >= 0)
 203     font_try->pixel_size = font->pixel_size;
 204 
 205   if ((font->mode != 1) && (font->mode != 2)){
 206     fprintf(stderr, "VFlib: internal error in try_create()\n");
 207     abort();
 208   }
 209 
 210   for (s = cap_fontlist; vf_sexp_consp(s); s = vf_sexp_cdr(s)){
 211     fname = vf_sexp_get_cstring(vf_sexp_car(s));
 212     if (fname == NULL) 
 213       continue;
 214     if (debug_on('f'))
 215       printf("VFlib Try:  trying %s\n", fname);
 216     if (font->mode == 1){
 217       font_try->fid = VF_OpenFont1(fname, 
 218                                    font_try->dpi_x, font_try->dpi_y,
 219                                    font_try->point_size, 
 220                                    font->mag_x * font_try->mag, 
 221                                    font->mag_y * font_try->mag);
 222     } else {
 223       font_try->fid = VF_OpenFont2(fname, font_try->pixel_size, 
 224                                    font->mag_x * font_try->mag, 
 225                                    font->mag_y * font_try->mag);
 226     }
 227     if (font_try->fid >= 0)
 228       break;
 229   }
 230 
 231   if (debug_on('f')){
 232     if (font_try->fid >= 0)
 233       printf("VFlib Try:  opened: %s\n", fname);
 234     else
 235       printf("VFlib Try:  failed\n");
 236   }
 237 
 238   if (font_try->fid < 0)
 239     goto CANT_OPEN;
 240 
 241   font->private = font_try;
 242   vf_sexp_free1(&cap_fontlist);
 243   vf_sexp_free4(&cap_point, &cap_pixel, &cap_mag, &cap_dpi);
 244   return 0;
 245 
 246 CANT_OPEN:
 247   vf_sexp_free1(&cap_fontlist);
 248   vf_sexp_free4(&cap_point, &cap_pixel, &cap_mag, &cap_dpi);
 249   vf_error = VF_ERR_CANT_OPEN;
 250   release_mem(font_try);
 251   return -1;
 252 }
 253 
 254 
 255 Private int
 256 try_close(VF_FONT font)
     /* [<][>][^][v][top][bottom][index][help] */
 257 {
 258   release_mem((FONT_TRY)(font->private));
 259 
 260   return 0; 
 261 }
 262 
 263 
 264 Private void
 265 release_mem(FONT_TRY font_try)
     /* [<][>][^][v][top][bottom][index][help] */
 266 {
 267   if (font_try != NULL){
 268     vf_free(font_try->font_name);
 269     if (font_try->fid >= 0)
 270       VF_CloseFont(font_try->fid);
 271     vf_sexp_free1(&font_try->props);
 272     vf_free(font_try);
 273   }
 274 }
 275 
 276 
 277 Private int
 278 try_get_metric1(VF_FONT font, long code_point, VF_METRIC1 metric,
     /* [<][>][^][v][top][bottom][index][help] */
 279                 double mag_x, double mag_y)
 280 {
 281   FONT_TRY  font_try;
 282   
 283   if (metric == NULL){
 284     fprintf(stderr, "VFlib internal error: in try_get_metric1()\n");
 285     abort();
 286   }
 287 
 288   if ((font_try = (FONT_TRY)font->private) == NULL){
 289     fprintf(stderr, "VFlib internal error in try class.\n");
 290     abort();
 291   }
 292 
 293   if (font_try->fid < 0){
 294     vf_error = VF_ERR_NO_GLYPH;
 295     return -1;
 296   }
 297 
 298   VF_GetMetric1(font_try->fid, code_point, metric, mag_x, mag_y);
 299 
 300   return 0;
 301 }
 302 
 303 Private int
 304 try_get_fontbbx1(VF_FONT font, double mag_x, double mag_y,
     /* [<][>][^][v][top][bottom][index][help] */
 305                  double *w_p, double *h_p, double *xoff_p, double *yoff_p)
 306 {
 307   FONT_TRY  font_try;
 308   
 309   if ((font_try = (FONT_TRY)font->private) == NULL){
 310     fprintf(stderr, "VFlib internal error in try class.\n");
 311     abort();
 312   }
 313 
 314   if (font_try->fid < 0){
 315     vf_error = VF_ERR_NO_GLYPH;
 316     return -1;
 317   }
 318 
 319   return VF_GetFontBoundingBox1(font_try->fid,
 320                                 mag_x, mag_y, w_p, h_p, xoff_p, yoff_p);
 321 }
 322 
 323 Private VF_BITMAP
 324 try_get_bitmap1(VF_FONT font, long code_point,
     /* [<][>][^][v][top][bottom][index][help] */
 325                 double mag_x, double mag_y)
 326 {
 327   FONT_TRY  font_try;
 328 
 329   if ((font_try = (FONT_TRY)font->private) == NULL){
 330     fprintf(stderr, "VFlib internal error in try class.\n");
 331     abort();
 332   }
 333 
 334   if (font_try->fid < 0){
 335     vf_error = VF_ERR_NO_GLYPH;
 336     return NULL;
 337   }
 338 
 339   return VF_GetBitmap1(font_try->fid, code_point, mag_x, mag_y);
 340 }
 341 
 342 
 343 Private VF_OUTLINE
 344 try_get_outline(VF_FONT font, long code_point,
     /* [<][>][^][v][top][bottom][index][help] */
 345                 double mag_x, double mag_y)
 346 {
 347   FONT_TRY  font_try;
 348 
 349   if ((font_try = (FONT_TRY)font->private) == NULL){
 350     fprintf(stderr, "VFlib internal error in try class.\n");
 351     abort();
 352   }
 353 
 354   if (font_try->fid < 0){
 355     vf_error = VF_ERR_NO_GLYPH;
 356     return NULL;
 357   }
 358 
 359   return VF_GetOutline(font_try->fid, code_point, mag_x, mag_y);
 360 }
 361 
 362 
 363 Private int
 364 try_get_metric2(VF_FONT font, long code_point, VF_METRIC2 metric, 
     /* [<][>][^][v][top][bottom][index][help] */
 365                   double mag_x, double mag_y)
 366 {
 367   FONT_TRY  font_try;
 368 
 369   if ((font_try = (FONT_TRY)font->private) == NULL){
 370     fprintf(stderr, "VFlib internal error in try class.\n");
 371     abort();
 372   }
 373 
 374   if (font_try->fid < 0){
 375     vf_error = VF_ERR_NO_GLYPH;
 376     return -1;
 377   }
 378 
 379   VF_GetMetric2(font_try->fid, code_point, metric, mag_x, mag_y);
 380 
 381   return 0;
 382 }
 383 
 384 Private int
 385 try_get_fontbbx2(VF_FONT font, double mag_x, double mag_y,
     /* [<][>][^][v][top][bottom][index][help] */
 386                  int *w_p, int *h_p, int *xoff_p, int *yoff_p)
 387 {
 388   FONT_TRY  font_try;
 389   
 390   if ((font_try = (FONT_TRY)font->private) == NULL){
 391     fprintf(stderr, "VFlib internal error in try class.\n");
 392     abort();
 393   }
 394 
 395   if (font_try->fid < 0){
 396     vf_error = VF_ERR_NO_GLYPH;
 397     return -1;
 398   }
 399 
 400   return VF_GetFontBoundingBox2(font_try->fid,
 401                                 mag_x, mag_y, w_p, h_p, xoff_p, yoff_p);
 402 }
 403 
 404 
 405 Private VF_BITMAP
 406 try_get_bitmap2(VF_FONT font, long code_point, 
     /* [<][>][^][v][top][bottom][index][help] */
 407                 double mag_x, double mag_y)
 408 {
 409   FONT_TRY  font_try;
 410 
 411   if ((font_try = (FONT_TRY)font->private) == NULL){
 412     fprintf(stderr, "VFlib internal error in try class.\n");
 413     abort();
 414   }
 415 
 416   if (font_try->fid < 0){
 417     vf_error = VF_ERR_NO_GLYPH;
 418     return NULL;
 419   }
 420 
 421   return VF_GetBitmap2(font_try->fid, code_point, mag_x, mag_y);
 422 }
 423 
 424 
 425 Private char*
 426 try_get_font_prop(VF_FONT font, char *prop_name)
     /* [<][>][^][v][top][bottom][index][help] */
 427 {
 428   FONT_TRY  font_try;
 429 
 430   if ((font_try = (FONT_TRY)font->private) == NULL){
 431     fprintf(stderr, "VFlib internal error in try class.\n");
 432     abort();
 433   }
 434 
 435   if (font_try->fid < 0){
 436     vf_error = VF_ERR_NO_GLYPH;
 437     return NULL;
 438   }
 439 
 440   return VF_GetFontProp(font_try->fid, prop_name);
 441 }
 442 
 443 
 444 Private int
 445 try_query_font_type(VF_FONT font, long code_point)
     /* [<][>][^][v][top][bottom][index][help] */
 446 {
 447   FONT_TRY  font_try;
 448 
 449   if ((font_try = (FONT_TRY)font->private) == NULL){
 450     fprintf(stderr, "VFlib internal error in try class.\n");
 451     abort();
 452   }
 453 
 454   if (font_try->fid < 0){
 455     vf_error = VF_ERR_NO_GLYPH;
 456     return -1;
 457   }
 458 
 459   return VF_QueryFontType(font_try->fid, code_point);
 460 }
 461 
 462 
 463 
 464 Private int
 465 debug_on(char type)
     /* [<][>][^][v][top][bottom][index][help] */
 466 {
 467   char  *p;
 468 
 469   if (default_debug_mode == NULL)
 470     return FALSE;
 471   if ((p = vf_sexp_get_cstring(default_debug_mode)) == NULL)
 472     return FALSE;
 473   
 474   while (*p != '\0'){
 475     if (*p == type)
 476       return TRUE;
 477     p++;
 478   }
 479 
 480   while (*p != '\0'){
 481     if (*p == '*')
 482       return TRUE;
 483     p++;
 484   }
 485 
 486   return TRUE;
 487 }
 488 
 489 
 490 /*EOF*/

/* [<][>][^][v][top][bottom][index][help] */