Back to index

tetex-bin  3.0
dvipng.h
Go to the documentation of this file.
00001 /* dvipng.h */
00002 
00003 /************************************************************************
00004 
00005   Part of the dvipng distribution
00006 
00007   This program is free software; you can redistribute it and/or modify
00008   it under the terms of the GNU General Public License as published by
00009   the Free Software Foundation; either version 2 of the License, or
00010   (at your option) any later version.
00011 
00012   This program is distributed in the hope that it will be useful, but
00013   WITHOUT ANY WARRANTY; without even the implied warranty of
00014   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015   General Public License for more details.
00016 
00017   You should have received a copy of the GNU General Public License
00018   along with this program; if not, write to the Free Software
00019   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00020   02111-1307, USA.
00021 
00022   Copyright (C) 2002-2005 Jan-Åke Larsson
00023 
00024 ************************************************************************/
00025 
00026 #ifndef DVIPNG_H
00027 #define DVIPNG_H
00028 #include "config.h"
00029 
00030 #define  STRSIZE         255     /* stringsize for file specifications  */
00031 
00032 #define  FIRSTFNTCHAR  0
00033 #define  LASTFNTCHAR   255
00034 #define  NFNTCHARS     LASTFNTCHAR+1
00035 
00036 #define  STACK_SIZE      100     /* DVI-stack size                     */
00037 
00038 #define DEFAULT_GAMMA 1.0
00039 
00040 /* Name of the program which is called to generate missing pk files */
00041 #define MAKETEXPK "mktexpk"
00042 
00043 #ifdef HAVE_INTTYPES_H
00044 #  include <inttypes.h>
00045 #else /* HAVE_INTTYPES_H */
00046 typedef signed char               int8_t;
00047 typedef unsigned char            uint8_t;
00048 typedef short                    int16_t;
00049 typedef unsigned short          uint16_t;
00050 typedef int                      int32_t;
00051 typedef unsigned int            uint32_t;
00052 typedef long long                int64_t;
00053 typedef unsigned long long      uint64_t;
00054 #endif /* HAVE_INTTYPES_H */
00055 
00056 #ifndef INT32_MIN
00057 #define INT32_MIN   (-2147483647-1)
00058 #endif
00059 #ifndef INT32_MAX
00060 #define INT32_MAX   (2147483647)
00061 #endif
00062 
00063 #ifdef HAVE_STDBOOL_H
00064 #include <stdbool.h>
00065 #endif
00066 
00067 #include <gd.h>
00068 
00069 #ifdef HAVE_LIBKPATHSEA
00070 # include <kpathsea/kpathsea.h>
00071 /* boolean is an enum type from kpathsea/types.h loaded in
00072    kpathsea/kpathsea.h, use it as fallback */
00073 # ifndef __bool_true_false_are_defined
00074 #  define bool boolean
00075 # endif
00076 #else
00077 /* This is really insufficient, currently no path searches will work
00078    without kpathsea anyway */
00079 typedef int bool;
00080 # define true (bool) 1
00081 # define false (bool) 0
00082 #endif
00083 
00084 #ifdef HAVE_FT2
00085 #define HAVE_FT2_OR_LIBT1
00086 #include <ft2build.h>  
00087 #include FT_FREETYPE_H
00088 #endif
00089 
00090 #ifdef HAVE_LIBT1
00091 #define HAVE_FT2_OR_LIBT1
00092 #include <t1lib.h>
00093 #endif
00094 
00095 #ifndef HAVE_VPRINTF
00096 # ifdef HAVE_DOPRNT
00097 #  define   vfprintf(stream, message, args)  _doprnt(message, args, stream)
00098 # else
00099 #  error: vfprintf AND _doprnt are missing!!!
00100    /* If we have neither, should fall back to fprintf with fixed args.  */
00101 # endif
00102 #endif
00103 
00104 /* Autoconf may define malloc to rpl_malloc, if the system does not
00105  * have a GNU Libc-compatible malloc (for which malloc(0) gives a
00106  * valid pointer). We don't need that (yet) */
00107 #ifdef malloc
00108 # undef malloc
00109 #endif
00110 
00111 
00112 /*************************************************************/
00113 /*************************  protos.h  ************************/
00114 /*************************************************************/
00115 
00116 typedef int pixels;
00117 typedef int32_t subpixels;
00118 typedef int32_t dviunits;
00119 
00120 #define  MM_TO_PXL(x)   (int)(((x)*resolution*10)/254)
00121 #define  PT_TO_PXL(x)   (int)((int32_t)((x)*resolution*100l)/7224)
00122 #define  PT_TO_DVI(x)   (int32_t)((x)*65536l)
00123 
00124 /*#define PIXROUND(x,c) ((((double)x+(double)(c>>1))/(double)c)+0.5)*/
00125 /*#define PIXROUND(x,c) (((x)+c)/(c))*/
00126 /*#define PIXROUND(x,c) ((x+c-1)/(c))*/
00127 /*#define PIXROUND(x,c) ((x)/(c))*/
00128 /* integer round to the nearest number, not towards zero */
00129 #define PIXROUND(num,den) ((num)>0 ? ((num)+(den)/2)/(den) : -(((den)/2-(num))/(den)))
00130 
00131 #if HAVE_ALLOCA_H
00132 # include <alloca.h>
00133 #endif
00134 #define TEMPSTR(s,a) { char* tmp=a; \
00135                if (tmp!=NULL) {\
00136                  s=alloca(strlen(tmp)+1);strcpy(s,tmp);free(tmp);\
00137                } else \
00138                  s=NULL;}
00139 
00140 /*************************************************************************/
00141 
00142 
00143 /********************************************************/
00144 /***********************  dvi.h  ************************/
00145 /********************************************************/
00146 
00147 #define DVI_TYPE            0
00148 struct dvi_data {    /* dvi entry */
00149   int          type;            /* This is a DVI                    */
00150   struct dvi_data *next;
00151   uint32_t     num, den, mag;   /* PRE command parameters            */
00152   int32_t      conv;            /* computed from num and den         */
00153   /* divide dvi units (sp) with conv to get mf device resolution */
00154   /* divide further with shrinkfactor to get true resolution */
00155   char *       name;            /* full name of DVI file             */
00156   char *       outname;         /* output filename (basename)        */
00157   FILE *       filep;           /* file pointer                      */
00158   time_t       mtime;           /* modification time                 */
00159   struct font_num  *fontnump;   /* DVI font numbering                */
00160   struct page_list *pagelistp;  /* DVI page list                     */
00161 };
00162 
00163 #define PAGE_POST      INT32_MAX
00164 #define PAGE_LASTPAGE  INT32_MAX-1
00165 #define PAGE_MAXPAGE   INT32_MAX-2    /* assume no pages out of this range */
00166 #define PAGE_FIRSTPAGE INT32_MIN  
00167 #define PAGE_MINPAGE   INT32_MIN+1    /* assume no pages out of this range */
00168 
00169 struct dvi_color {
00170   int red,green,blue;
00171 };
00172 
00173 struct page_list {
00174   struct page_list* next;
00175   int               offset;           /* file offset to BOP */
00176   int32_t           count[11];        /* 10 dvi counters + absolute pagenum in file */
00177   int               csp;              /* color stack pointer at BOP */
00178   struct dvi_color  cstack[2];        /* color stack at BOP, may be longer */
00179 };
00180 
00181 
00182 
00183 
00184 struct dvi_data* DVIOpen(char*,char*);
00185 void             DVIClose(struct dvi_data*);
00186 bool             DVIReOpen(struct dvi_data*);
00187 struct page_list*FindPage(struct dvi_data*, int32_t, bool);
00188 struct page_list*NextPage(struct dvi_data*, struct page_list*);
00189 struct page_list*PrevPage(struct dvi_data*, struct page_list*);
00190 int              SeekPage(struct dvi_data*, struct page_list*);
00191 bool             DVIFollowToggle(void);
00192 unsigned char*   DVIGetCommand(struct dvi_data*);
00193 uint32_t         CommandLength(unsigned char*); 
00194 
00195 /********************************************************/
00196 /**********************  misc.h  ************************/
00197 /********************************************************/
00198 
00199 struct filemmap {
00200 #ifdef MIKTEX
00201   HANDLE hFile;
00202   HANDLE hMap;
00203 #else  /* MIKTEX */
00204   int fd;
00205 #endif /* MIKTEX */
00206   char* mmap;
00207   size_t size;
00208 };
00209 
00210 bool    DecodeArgs(int, char *[]);
00211 void    DecodeString(char *);
00212 bool    MmapFile (char *filename,struct filemmap *fmmap);
00213 void    UnMmapFile(struct filemmap* fmmap);
00214 
00215 void    Message(int, char *fmt, ...);
00216 void    Warning(char *fmt, ...);
00217 void    Fatal(char *fmt, ...);
00218 
00219 int32_t   SNumRead(unsigned char*, register int);
00220 uint32_t   UNumRead(unsigned char*, register int);
00221 
00222 bool MmapFile (char *filename,struct filemmap *fmmap);
00223 void UnMmapFile(struct filemmap* fmmap);
00224 
00225 
00226 /********************************************************/
00227 /***********************  font.h  ***********************/
00228 /********************************************************/
00229 struct encoding {
00230   struct encoding* next;
00231   char*            name;
00232   char*            charname[257];
00233 };
00234 
00235 #ifdef HAVE_FT2_OR_LIBT1
00236 struct psfontmap {
00237   struct psfontmap *next;
00238   char *line,*psfile,*tfmname,*encname,*end;
00239   struct encoding* encoding;
00240 #ifdef HAVE_FT2
00241   FT_Matrix* ft_transformp;
00242   FT_Matrix ft_transform;
00243 #endif
00244 #ifdef HAVE_LIBT1
00245   T1_TMATRIX* t1_transformp;
00246   T1_TMATRIX t1_transform;
00247 #endif
00248 };
00249 #endif
00250 
00251 #define FONT_TYPE_PK            1
00252 #define FONT_TYPE_VF            2
00253 #define FONT_TYPE_FT            3
00254 #define FONT_TYPE_T1            4
00255 struct char_entry {                /* PK/FT/T1 Glyph/VF Macro             */
00256   dviunits       tfmw;             /* TFM width                           */
00257   unsigned char *data;             /* glyph data, either pixel data
00258                                 * (0=transp, 255=max ink) or VF macro */
00259   uint32_t       length;           /* Length of PK data or VF macro       */
00260   /* Only used in pixel fonts */
00261   pixels         w,h;              /* width and height in pixels          */
00262   subpixels      xOffset, yOffset; /* x offset and y offset in subpixels  */
00263   /* Only used in PK fonts */
00264   unsigned char *pkdata;           /* Points to beginning of PK data      */
00265   unsigned char  flag_byte;        /* PK flagbyte                         */
00266 };
00267 
00268 struct font_entry {    /* font entry */
00269   int          type;            /* PK/VF/Type1 ...                   */
00270   struct font_entry *next;
00271   uint32_t     c, s, d;                                                
00272   uint8_t      a, l;                                                   
00273   char         n[STRSIZE];      /* FNT_DEF command parameters        */
00274   int          dpi;             /* computed from s and d             */
00275   char         name[STRSIZE];   /* full name of PK/VF file           */
00276   struct filemmap fmmap;        /* file memory map                   */
00277   uint32_t     magnification;   /* magnification read from font file */
00278   uint32_t     designsize;      /* design size read from font file   */
00279   void *       chr[NFNTCHARS];  /* character information             */ 
00280 #ifdef HAVE_FT2
00281   FT_Face      face;            /* Freetype2 face                    */
00282 #endif
00283 #ifdef HAVE_LIBT1
00284   int          T1id;            /* T1lib font id                     */
00285 #endif
00286 #ifdef HAVE_FT2_OR_LIBT1
00287   struct psfontmap* psfontmap;  /* Font transformation               */
00288 #endif
00289   struct font_num *vffontnump;  /* VF local font numbering           */
00290   int32_t      defaultfont;     /* VF default font number            */
00291 };
00292 
00293 struct font_num {    /* Font number. Different for VF/DVI, and several
00294                      font_num can point to one font_entry */
00295   struct font_num   *next;
00296   int32_t            k;
00297   struct font_entry *fontp;
00298 };
00299 
00300 void    CheckChecksum(uint32_t, uint32_t, const char*);
00301 void    InitPK (struct font_entry *newfontp);
00302 void    DonePK(struct font_entry *oldfontp);
00303 void    InitVF (struct font_entry *newfontp);
00304 void    DoneVF(struct font_entry *oldfontp);
00305 
00306 void    FontDef(unsigned char*, void* /* dvi/vf */);
00307 void    ClearFonts(void);
00308 void    SetFntNum(int32_t, void* /* dvi/vf */);      
00309 void    FreeFontNumP(struct font_num *hfontnump);
00310 
00311 #ifdef HAVE_FT2_OR_LIBT1
00312 void    InitPSFontMap(void);
00313 void    ClearPSFontMap(void);
00314 struct psfontmap* FindPSFontMap(char*);
00315 struct encoding* FindEncoding(char*);
00316 void    ClearEncoding(void);
00317 bool    ReadTFM(struct font_entry *, char*);
00318 #endif
00319 
00320 #ifdef HAVE_FT2
00321 bool    InitFT(struct font_entry *);
00322 void    DoneFT(struct font_entry *tfontp);
00323 void    LoadFT(int32_t, struct char_entry *);
00324 #endif
00325 
00326 #ifdef HAVE_LIBT1
00327 bool    InitT1(struct font_entry *);
00328 void    DoneT1(struct font_entry *tfontp);
00329 void    LoadT1(int32_t, struct char_entry *);
00330 #endif
00331 
00332 /********************************************************/
00333 /*********************  pplist.h  ***********************/
00334 /********************************************************/
00335 
00336 bool    ParsePages(char*);
00337 void    FirstPage(int32_t,bool);
00338 void    LastPage(int32_t,bool);
00339 void    ClearPpList(void);
00340 void    Reverse(bool);
00341 struct page_list*   NextPPage(void* /* dvi */, struct page_list*);
00342 
00343 
00344 
00345 
00346 #ifdef MAIN
00347 #define EXTERN
00348 #define INIT(x) =x
00349 #else
00350 #define EXTERN extern
00351 #define INIT(x)
00352 #endif
00353 
00354 /********************************************************/
00355 /**********************  draw.h  ************************/
00356 /********************************************************/
00357 #include "commands.h"
00358 
00359 void      CreateImage(pixels width, pixels height);
00360 void      DestroyImage(void);
00361 void      DrawCommand(unsigned char*, void* /* dvi/vf */); 
00362 void      DrawPages(void);
00363 void      WriteImage(char*, int);
00364 void      LoadPK(int32_t, register struct char_entry *);
00365 int32_t   SetChar(int32_t);
00366 dviunits  SetGlyph(int32_t c, int32_t hh,int32_t vv);
00367 void      Gamma(double gamma);
00368 int32_t   SetVF(int32_t);
00369 int32_t   SetRule(int32_t, int32_t, int32_t, int32_t);
00370 void      SetSpecial(char *, int32_t, int32_t, int32_t);
00371 void      BeginVFMacro(struct font_entry*);
00372 void      EndVFMacro(void);
00373 
00374 /**************************************************/
00375 void handlepapersize(char*,int32_t*,int32_t*);
00376 
00377 void stringrgb(char* colorstring,int *r,int *g,int *b);
00378 void background(char *);
00379 void initcolor(void);
00380 void popcolor(void);
00381 void pushcolor(char *);
00382 void resetcolorstack(char *);
00383 void StoreColorStack(struct page_list *tpagep);
00384 void ReadColorStack(struct page_list *tpagep);
00385 void StoreBackgroundColor(struct page_list *tpagep);
00386 void ClearDvipsNam(void);
00387 
00388 
00389 /**********************************************************************/
00390 /*************************  Global Variables  *************************/
00391 /**********************************************************************/
00392 
00393 #ifdef MAKETEXPK
00394 #ifdef HAVE_LIBKPATHSEA
00395 EXTERN bool    makeTexPK INIT(MAKE_TEX_PK_BY_DEFAULT);
00396 #else
00397 EXTERN bool    makeTexPK INIT(_TRUE);
00398 #endif
00399 #endif
00400 
00401 EXTERN uint32_t    usermag INIT(0);     /* user specified magstep          */
00402 EXTERN struct font_entry *hfontptr INIT(NULL); /* font list pointer        */
00403 
00404 EXTERN struct internal_state {
00405   struct font_entry* currentfont;
00406 } current_state;
00407 
00408 #define BE_NONQUIET                  1
00409 #define BE_VERBOSE                   (1<<1)
00410 #define PARSE_STDIN                  (1<<2)
00411 #define EXPAND_BBOX                  (1<<3)
00412 #define TIGHT_BBOX                   (1<<4)
00413 #define CACHE_IMAGES                 (1<<5)
00414 #define RENDER_TRUECOLOR             (1<<6)
00415 #define USE_FREETYPE                 (1<<7)
00416 #define USE_LIBT1                    (1<<8)
00417 #define REPORT_HEIGHT                (1<<9)
00418 #define REPORT_DEPTH                 (1<<10)
00419 #define DVI_PAGENUM                  (1<<11)
00420 #define MODE_PICKY                   (1<<12)
00421 #define PAGE_GAVE_WARN               (1<<13)
00422 #define PREVIEW_LATEX_TIGHTPAGE      (1<<14)
00423 #define GIF_OUTPUT                   (1<<15)
00424 #define MODE_STRICT                  (1<<16)
00425 #define NO_GHOSTSCRIPT               (1<<17)
00426 #define NO_GSSAFER                   (1<<18)
00427 #define BG_TRANSPARENT               (1<<19)
00428 EXTERN uint32_t flags INIT(BE_NONQUIET | USE_FREETYPE | USE_LIBT1);
00429 
00430 #ifdef DEBUG
00431 EXTERN unsigned int debug INIT(0);
00432 #define DEBUG_PRINT(a,b) if (debug & a) { printf b; fflush(stdout); }
00433 #define DEBUG_DVI                    1
00434 #define DEBUG_VF                     (1<<1)
00435 #define DEBUG_PK                     (1<<2)
00436 #define DEBUG_TFM                    (1<<3)
00437 #define DEBUG_GLYPH                  (1<<4)
00438 #define DEBUG_FT                     (1<<5)
00439 #define DEBUG_ENC                    (1<<6)
00440 #define DEBUG_COLOR                  (1<<7)
00441 #define DEBUG_T1                     (1<<8)
00442 #define DEBUG_GS                     (1<<9)
00443 #define LASTDEBUG                    DEBUG_GS
00444 #define DEBUG_DEFAULT                DEBUG_DVI
00445 #else
00446 #define DEBUG_PRINT(a,b)
00447 #endif
00448 
00449 /************************timing stuff*********************/
00450 #ifdef TIMING
00451 #if TIME_WITH_SYS_TIME
00452 # include <sys/time.h>
00453 # include <time.h>
00454 #else
00455 # if HAVE_SYS_TIME_H
00456 #  include <sys/time.h>
00457 # else
00458 #  include <time.h>
00459 # endif
00460 #endif
00461 EXTERN double timer INIT(0);
00462 EXTERN double my_tic,my_toc INIT(0);
00463 EXTERN int      ndone INIT(0);          /* number of pages converted       */
00464 # ifdef HAVE_GETTIMEOFDAY
00465 EXTERN struct timeval Tp;
00466 #  define TIC { gettimeofday(&Tp, NULL); \
00467     my_tic= (float)Tp.tv_sec + ((float)(Tp.tv_usec))/ 1000000.0;}
00468 #  define TOC { gettimeofday(&Tp, NULL); \
00469     my_toc += ((float)Tp.tv_sec + ((float)(Tp.tv_usec))/ 1000000.0) - my_tic;}
00470 # else
00471 #  ifdef HAVE_FTIME
00472 EXTERN struct timeb timebuffer;
00473 #   define TIC() { ftime(&timebuffer); \
00474  my_tic= timebuffer.time + (float)(timebuffer.millitm) / 1000.0;
00475 #   define TOC() { gettimeofday(&Tp, NULL); \
00476  my_toc += (timebuffer.time + (float)(timebuffer.millitm) / 1000.0) - my_tic;}
00477 #  else
00478 #   define TIC()
00479 #   define TOC()
00480 #  endif
00481 # endif
00482 #endif /* TIMING */
00483 
00484 EXTERN char*  user_mfmode          INIT(NULL);
00485 EXTERN int    user_bdpi            INIT(0);
00486 EXTERN int    dpi                  INIT(100);
00487 
00488 #ifdef HAVE_GDIMAGEPNGEX
00489 EXTERN int   compression INIT(1);
00490 #endif
00491 #ifdef MIKTEX
00492 #undef min
00493 #undef max
00494 #endif        /* MIKTEX */
00495 # define  max(x,y)       if ((y)>(x)) x = y
00496 # define  min(x,y)       if ((y)<(x)) x = y
00497 
00498 /* These are in pixels*/
00499 EXTERN  int x_min INIT(0); 
00500 EXTERN  int y_min INIT(0);
00501 EXTERN  int x_max INIT(0);
00502 EXTERN  int y_max INIT(0);
00503 
00504 /* Page size: default set by -T */
00505 EXTERN  int x_width_def INIT(0); 
00506 EXTERN  int y_width_def INIT(0);
00507 
00508 /* Offset: default set by -O and -T bbox */
00509 EXTERN  int x_offset_def INIT(0);
00510 EXTERN  int y_offset_def INIT(0);
00511 
00512 /* Preview-latex's tightpage */
00513 EXTERN  int x_width_tightpage INIT(0); 
00514 EXTERN  int y_width_tightpage INIT(0);
00515 EXTERN  int x_offset_tightpage INIT(0);
00516 EXTERN  int y_offset_tightpage INIT(0);
00517 
00518 /* Paper size: set by -t, for cropmark purposes only */
00519 /* This has yet to be written */
00520 EXTERN  int x_pwidth INIT(0); 
00521 EXTERN  int y_pwidth INIT(0);
00522 
00523 /* The transparent border preview-latex desires */
00524 EXTERN  int borderwidth INIT(0);
00525 
00526 /* fallback color for transparent background */
00527 EXTERN bool userbordercolor INIT(FALSE); /* if true, use user-supplied color */
00528 EXTERN struct dvi_color bordercolor;
00529 
00530 
00531 EXTERN gdImagePtr page_imagep INIT(NULL);
00532 EXTERN int32_t shrinkfactor INIT(4);
00533 
00534 EXTERN struct dvi_color cstack[STACK_SIZE];
00535 EXTERN int csp INIT(1);
00536 
00537 EXTERN struct font_entry* currentfont;
00538 EXTERN struct dvi_data* dvi INIT(NULL);
00539 
00540 #ifdef HAVE_FT2
00541 EXTERN FT_Library libfreetype INIT(NULL);
00542 #endif
00543 
00544 #ifdef HAVE_LIBT1
00545 EXTERN void* libt1 INIT(NULL);
00546 #endif
00547 
00548 #define  EXIT_FATAL    EXIT_FAILURE+1
00549 EXTERN int exitcode INIT(EXIT_SUCCESS);
00550 
00551 #endif /* DVIPNG_H */