w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

graphics.c
Go to the documentation of this file.
1 /* graphics.c - routines that handle LaTeX graphics commands
2 
3 Copyright (C) 2001-2002 The Free Software Foundation
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 This file is available from http://sourceforge.net/projects/latex2rtf/
20 
21 Authors:
22  2001-2002 Scott Prahl
23 */
24 #ifdef WIN32
25 #include <kpathsea/config.h>
26 #endif
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <limits.h>
32 #ifdef UNIX
33 #include <unistd.h>
34 #endif
35 #include "cfg.h"
36 #include "main.h"
37 #include "graphics.h"
38 #include "parser.h"
39 #include "util.h"
40 #include "commands.h"
41 #include "convert.h"
42 #include "equation.h"
43 #include "funct1.h"
44 #define POINTS_PER_M 2834.65
45 /* Little endian macros to convert to and from host format to network byte ordering */
46 #define LETONS(A) ((((A) & 0xFF00) >> 8) | (((A) & 0x00FF) << 8))
47 #define LETONL(A) ((((A) & 0xFF000000) >> 24) | (((A) & 0x00FF0000) >> 8) | \
48  (((A) & 0x0000FF00) << 8) | (((A) & 0x000000FF) << 24) )
49 /*
50 Version 1.6 RTF files can include pictures as follows
51 <pict> '{' \pict (<brdr>? & <shading>? & <picttype> & <pictsize> & <metafileinfo>?) <data> '}'
52 <picttype> \emfblip | \pngblip | \jpegblip | \macpict | \pmmetafile | \wmetafile
53  | \dibitmap <bitmapinfo> | \wbitmap <bitmapinfo>
54 <bitmapinfo> \wbmbitspixel & \wbmplanes & \wbmwidthbytes
55 <pictsize> (\picw & \pich) \picwgoal? & \pichgoal? \picscalex? & \picscaley? & \picscaled? & \piccropt? & \piccropb? & \piccropr? & \piccropl?
56 <metafileinfo> \picbmp & \picbpp
57 <data> (\bin #BDATA) | #SDATA
58 
59 \emfblip Source of the picture is an EMF (enhanced metafile).
60 \pngblip Source of the picture is a PNG.
61 \jpegblip Source of the picture is a JPEG.
62 \shppict Specifies a Word 97-2000 picture. This is a destination control word.
63 \nonshppict Specifies that Word 97-2000 has written a {\pict destination that it
64  will not read on input. This keyword is for compatibility with other readers.
65 \macpict Source of the picture is PICT file (Quickdraw)
66 \pmmetafileN Source of the picture is an OS/2 metafile
67 \wmetafileN Source of the picture is a Windows metafile
68 \dibitmapN Source of the picture is a Windows device-independent bitmap
69 \wbitmapN Source of the picture is a Windows device-dependent bitmap
70 */
71 
72 typedef struct _WindowsMetaHeader
73 {
74  unsigned short FileType; /* Type of metafile (0=memory, 1=disk) */
75  unsigned short HeaderSize; /* Size of header in WORDS (always 9) */
76  unsigned short Version; /* Version of Microsoft Windows used */
77  unsigned long FileSize; /* Total size of the metafile in WORDs */
78  unsigned short NumOfObjects; /* Number of objects in the file */
79  unsigned long MaxRecordSize; /* The size of largest record in WORDs */
80  unsigned short NumOfParams; /* Not Used (always 0) */
82 
83 typedef struct _PlaceableMetaHeader
84 {
85  unsigned long Key; /* Magic number (always 0x9AC6CDD7) */
86  unsigned short Handle; /* Metafile HANDLE number (always 0) */
87  short Left; /* Left coordinate in twips */
88  short Top; /* Top coordinate in twips */
89  short Right; /* Right coordinate in twips */
90  short Bottom; /* Bottom coordinate in twips */
91  unsigned short Inch; /* Scaling factor, 1440 => 1:1, 360 => 4:1, 2880 => 1:2 (half size) */
92  unsigned long Reserved; /* Reserved (always 0) */
93  unsigned short Checksum; /* Checksum value for previous 10 WORDs */
95 
96 typedef struct _EnhancedMetaHeader
97 {
98  unsigned long RecordType; /* Record type (always 0x00000001)*/
99  unsigned long RecordSize; /* Size of the record in bytes */
100  long BoundsLeft; /* Left inclusive bounds */
101  long BoundsTop; /* Top inclusive bounds */
102  long BoundsRight; /* Right inclusive bounds */
103  long BoundsBottom; /* Bottom inclusive bounds */
104  long FrameLeft; /* Left side of inclusive picture frame */
105  long FrameTop; /* Top side of inclusive picture frame */
106  long FrameRight; /* Right side of inclusive picture frame */
107  long FrameBottom; /* Bottom side of inclusive picture frame */
108  unsigned long Signature; /* Signature ID (always 0x464D4520) */
109  unsigned long Version; /* Version of the metafile */
110  unsigned long Size; /* Size of the metafile in bytes */
111  unsigned long NumOfRecords; /* Number of records in the metafile */
112  unsigned short NumOfHandles; /* Number of handles in the handle table */
113  unsigned short Reserved; /* Not used (always 0) */
114  unsigned long SizeOfDescrip; /* Size of description string in WORDs */
115  unsigned long OffsOfDescrip; /* Offset of description string in metafile */
116  unsigned long NumPalEntries; /* Number of color palette entries */
117  long WidthDevPixels; /* Width of reference device in pixels */
118  long HeightDevPixels;/* Height of reference device in pixels */
119  long WidthDevMM; /* Width of reference device in millimeters */
120  long HeightDevMM; /* Height of reference device in millimeters */
122 
124 {
125  unsigned long Signature; /* 0x46535045 for EPS, 0x464D4520 for EMF */
126  unsigned long Version; /* EPS version number or 0x00000001 for EMF */
127  unsigned long Data; /* Size of data in bytes */
128  unsigned long OffsetToData; /* Offset to data */
129 };
130 
131 typedef struct _EmrFormat EMR_FORMAT;
132 
134 {
135  unsigned long Identifier; /* Comment ID (0x43494447) */
136  unsigned long Comment; /* Multiformats ID (0x40000004) */
137  long BoundsLeft; /* Left side of bounding rectangle */
138  long BoundsRight; /* Right side of bounding rectangle */
139  long BoundsTop; /* Top side of bounding rectangle */
140  long BoundsBottom; /* Bottom side of bounding rectangle */
141  unsigned long NumFormats; /* Number of formats in comment */
142  EMR_FORMAT *Data; /* Array of comment data */
144 
145 static void
147 /******************************************************************************
148  purpose : portable routine to delete filename
149  ******************************************************************************/
150 {
151 #ifdef UNIX
152  unlink(filename);
153 #endif
154 }
155 
156 static
157 void PicComment(short label, short size, FILE *fp)
158 {
159  short long_comment = 0x00A1;
160  short short_comment = 0x00A0;
161  short tag;
162 
163  tag = (size) ? long_comment : short_comment;
164 
165  if (g_little_endian) {
166  tag = LETONS(tag);
167  label = LETONS(label);
168  size = LETONS(size);
169  }
170 
171  if (fwrite(&tag, 2,1,fp) != 2) return;
172  if (fwrite(&label,2,1,fp) !=2) return;
173  if (size) {
174  if (fwrite(&size, 2,1,fp) !=2) return;
175  }
176 }
177 
178 static char *
179 strdup_new_extension(char *s, char *old_ext, char *new_ext)
180 {
181  char *new_name, *p;
182 
183  p=strstr(s,old_ext);
184  if (p==NULL) return NULL;
185 
186  new_name = strdup_together(s,new_ext);
187  p=strstr(new_name,old_ext);
188  strcpy(p,new_ext);
189  return new_name;
190 }
191 
192 static char *
194 /******************************************************************************
195  purpose : return a string containing an absolute path
196  ******************************************************************************/
197 {
198  char c = PATHSEP;
199  char *abs_path=NULL;
200 
201  if (s) {
202  if (*s==c || g_home_dir==NULL)
203  abs_path = strdup(s);
204  else
205  abs_path = strdup_together(g_home_dir, s);
206  }
207 
208  return abs_path;
209 }
210 
211 
212 static char *
214 /******************************************************************************
215  purpose : create a tmp file name using only the end of the filename
216  ******************************************************************************/
217 {
218  char *tmp, *p, *fullname, c;
219 
220  if (s==NULL) return NULL;
221 
222  tmp = getTmpPath();
223 
224  c = PATHSEP;
225  p=strrchr(s,c);
226 
227  if (!p)
228  fullname = strdup_together(tmp,s);
229  else
230  fullname = strdup_together(tmp,p+1);
231 
232  free(tmp);
233  return fullname;
234 }
235 
236 
237 static char *
239 /******************************************************************************
240  purpose : create a pict file from an EPS file and return file name for
241  the pict file. Ideally this file will contain both the bitmap
242  and the original EPS embedded in the PICT file as comments. If a
243  bitmap cannot be created, then the EPS is still embedded in the PICT
244  file so that at least the printed version will be good.
245  ******************************************************************************/
246 {
247  char *cmd, *p, buffer[560];
248  size_t cmd_len;
249  long ii, pict_bitmap_size, eps_size;
250  short err,handle_size;
251  unsigned char byte;
252  short PostScriptBegin = 190;
253  short PostScriptEnd = 191;
254  short PostScriptHandle = 192;
255  char *pict_bitmap =NULL;
256  char *pict_eps =NULL;
257  char *eps =NULL;
258  char *return_value =NULL;
259  FILE *fp_eps =NULL;
260  FILE *fp_pict_bitmap =NULL;
261  FILE *fp_pict_eps =NULL;
262 
263  diagnostics(2, "eps_to_pict filename = <%s>", s);
264 
265  /* Create filename for bitmap */
266  p = strdup_new_extension(s, ".eps", "a.pict");
267  if (p == NULL) {
268  p = strdup_new_extension(s, ".EPS", "a.pict");
269  if (p == NULL) goto Exit;
270  }
271  pict_bitmap = strdup_tmp_path(p);
272  free(p);
273 
274  /* Create filename for eps file */
275  p = strdup_new_extension(s, ".eps", ".pict");
276  if (p == NULL) {
277  p = strdup_new_extension(s, ".EPS", ".pict");
278  if (p == NULL) goto Exit;
279  }
280  pict_eps = strdup_tmp_path(p);
281  free(p);
282 
284 
285  /* create a bitmap version of the eps file */
286  cmd_len = strlen(eps)+strlen(pict_bitmap)+strlen("convert -crop 0x0 -density ")+40;
287  cmd = (char *) malloc(cmd_len);
288  snprintf(cmd, cmd_len, "convert -crop 0x0 -density %d %s %s", g_dots_per_inch, eps, pict_bitmap);
289  diagnostics(2, "system graphics command = [%s]", cmd);
290  err = system(cmd);
291  free(cmd);
292 
293  if (err!=0)
294  diagnostics(WARNING__, "problem creating bitmap from %s", eps);
295  else
296  return_value = pict_bitmap;
297 
298  /* open the eps file and make sure that it is less than 32k */
299  fp_eps = fopen (eps, "rb");
300  if (fp_eps==NULL) goto Exit;
301  fseek(fp_eps, 0, SEEK_END);
302  eps_size = ftell (fp_eps);
303  if (eps_size > 32000) {
304  diagnostics(WARNING__, "EPS file >32K ... using bitmap only");
305  goto Exit;
306  }
307  rewind (fp_eps);
308  diagnostics(WARNING__, "eps size is 0x%X bytes", eps_size);
309 
310  /*open bitmap pict file and get file size */
311  fp_pict_bitmap = fopen(pict_bitmap, "rb");
312  if (fp_pict_bitmap == NULL) goto Exit;
313  fseek(fp_pict_bitmap, 0, SEEK_END);
314  pict_bitmap_size = ftell(fp_pict_bitmap);
315  rewind(fp_pict_bitmap);
316 
317  /*open new pict file */
318  fp_pict_eps = fopen(pict_eps, "wb");
319  if (fp_pict_eps == NULL) goto Exit;
320 
321  /*copy header 512 buffer + 40 byte header*/
322  if (fread( &buffer,1,512+40,fp_pict_bitmap)!=512+40) goto Exit;
323  if (fwrite(&buffer,1,512+40,fp_pict_eps)!=512+40) goto Exit;
324 
325  /* insert comment that allows embedding postscript */
326  PicComment(PostScriptBegin,0,fp_pict_eps);
327 
328  /*copy bitmap 512+40 bytes of header + 2 bytes at end */
329  for (ii=512+40+2; ii<pict_bitmap_size; ii++) {
330  if(fread (&byte,1,1,fp_pict_bitmap)!=1) goto Exit;
331  if(fwrite(&byte,1,1,fp_pict_eps )!=1) goto Exit;
332  }
333 
334  /*copy eps graphic (write an even number of bytes) */
335  handle_size = eps_size;
336  if (odd(eps_size)) handle_size ++;
337 
338  PicComment(PostScriptHandle,handle_size,fp_pict_eps);
339  for (ii=0; ii<eps_size; ii++) {
340  if(fread(&byte,1,1,fp_eps)!=1) goto Exit;
341  if(fwrite(&byte,1,1,fp_pict_eps)!=1) goto Exit;
342  }
343  if (odd(eps_size)) {
344  byte = ' ';
345  if(fwrite(&byte,1,1,fp_pict_eps) !=1) goto Exit;
346  }
347 
348  /*close file*/
349  PicComment(PostScriptEnd,0,fp_pict_eps);
350  byte = 0x00;
351  if (fwrite(&byte,1,1,fp_pict_eps)!=1) goto Exit;
352  byte = 0xFF;
353  if (fwrite(&byte,1,1,fp_pict_eps)!=1) goto Exit;
354 
355  return_value= pict_eps;
356 
357 Exit:
358  if (eps) free(eps);
359  if (pict_eps) free(pict_eps);
360  if (pict_bitmap) free(pict_bitmap);
361 
362  if (fp_eps) fclose(fp_eps);
363  if (fp_pict_eps) fclose(fp_pict_eps);
364  if (fp_pict_bitmap) fclose(fp_pict_bitmap);
365  return return_value;
366 }
367 
368 static char *
370 /******************************************************************************
371  purpose : create a png file from an EPS or PS file and return file name
372  ******************************************************************************/
373 {
374  char *cmd, *s1, *p, *png;
375  size_t cmd_len;
376  diagnostics(1, "filename = <%s>", eps);
377 
378  s1 = strdup(eps);
379  if ((p=strstr(s1,".eps")) == NULL && (p=strstr(s1,".EPS")) == NULL &&
380  (p=strstr(s1,".ps" )) == NULL && (p=strstr(s1,".PS" )) == NULL) {
381  diagnostics(1, "<%s> is not an EPS or PS file", eps);
382  free(s1);
383  return NULL;
384  }
385 
386  strcpy(p,".png");
388  cmd_len=strlen(eps)+strlen(png)+40;
389  cmd = (char *) malloc(cmd_len);
390  snprintf(cmd, cmd_len, "convert -density %d %s %s", g_dots_per_inch, eps, png);
391  diagnostics(2, "system graphics command = [%s]", cmd);
392  system(cmd);
393 
394  free(cmd);
395  free(s1);
396  return png;
397 }
398 
399 static char *
401 /******************************************************************************
402  purpose : create a png file from an PDF file and return file name
403  ******************************************************************************/
404 {
405  char *cmd, *s1, *p, *png;
406  size_t cmd_len;
407  diagnostics(1, "filename = <%s>", pdf);
408 
409  s1 = strdup(pdf);
410  if ((p=strstr(s1,".pdf")) == NULL && (p=strstr(s1,".PDF")) == NULL) {
411  diagnostics(1, "<%s> is not a PDF file", pdf);
412  free(s1);
413  return NULL;
414  }
415 
416  strcpy(p,".png");
418  cmd_len=strlen(pdf)+strlen(png)+40;
419  cmd = (char *) malloc(cmd_len);
420  snprintf(cmd, cmd_len, "convert -density %d %s %s", g_dots_per_inch, pdf, png);
421  diagnostics(2, "system graphics command = [%s]", cmd);
422  system(cmd);
423 
424  free(cmd);
425  free(s1);
426  return png;
427 }
428 static char *
430 /******************************************************************************
431  purpose : create a wmf file from an EPS file and return file name
432  ******************************************************************************/
433 {
434  FILE *fp;
435  char *cmd, *s1, *p, *emf;
436  size_t cmd_len;
437 
438  char ans[50];
439  long width, height;
440  diagnostics(1, "filename = <%s>", eps);
441 
442  s1 = strdup(eps);
443  if ((p=strstr(s1,".eps")) == NULL && (p=strstr(s1,".EPS")) == NULL) {
444  diagnostics(1, "<%s> is not an EPS file", eps);
445  free(s1);
446  return NULL;
447  }
448 
449  strcpy(p,".wmf");
450  emf = strdup_tmp_path(s1);
451 
452  /* Determine bounding box for EPS file */
453  cmd_len = strlen(eps)+strlen("identify -format \"%w %h\" ")+1;
454  cmd = (char *) malloc(cmd_len);
455  snprintf(cmd, cmd_len, "identify -format \"%%w %%h\" %s", eps);
456  fp=(FILE *)popen(cmd,"r");
457  if (fgets(ans, 50, fp)!=NULL)
458  sscanf(ans,"%ld %ld",&width,&height);
459  pclose(fp);
460  free(cmd);
461 
462  fp = fopen(emf, "wb");
463 
464  /* write ENHANCEDMETAHEADER */
465 
466  /* write GDICOMMENTMULTIFORMATS */
467 
468  /* write EMR_FORMAT containing EPS */
469 
470  free(s1);
471  fclose(fp);
472  return emf;
473 }
474 
475 
476 static void
478 /******************************************************************************
479  purpose : write entire file to RTF as hex
480  ******************************************************************************/
481 {
482 int i, c;
483 
484  i = 0;
485  while ((c = fgetc(fp)) != EOF) {
486  fprintRTF("%.2x", c);
487  if (++i > 126) { /* keep lines 254 chars long */
488  i = 0;
489  fprintRTF("\n");
490  }
491  }
492 }
493 
494 static void
495 PutPictFile(char * s, double scale, double baseline, int full_path)
496 /******************************************************************************
497  purpose : Include .pict file in RTF
498  ******************************************************************************/
499 {
500 FILE *fp;
501 char *pict;
502 short buffer[5];
503 short top, left, bottom, right;
504 int width, height;
505 
506  if (full_path)
507  pict = strdup(s);
508  else
509  pict = strdup_together(g_home_dir, s);
510  diagnostics(1, "PutPictFile <%s>", pict);
511 
512  fp = fopen(pict, "rb");
513  free(pict);
514  if (fp == NULL) return;
515 
516  if (fseek(fp, 514L, SEEK_SET) || fread(buffer, 2, 4, fp) != 4) {
517  diagnostics (WARNING__, "Cannot read graphics file <%s>", s);
518  fclose(fp);
519  return;
520  }
521 
522  top = buffer[0];
523  left = buffer[1];
524  bottom = buffer[2];
525  right = buffer[3];
526 
527  width = right - left;
528  height = bottom - top;
529 
530  if (g_little_endian) {
531  top = LETONS(top);
532  bottom = LETONS(bottom);
533  left = LETONS(left);
534  right = LETONS(right);
535  }
536 
537  diagnostics(4,"top = %d, bottom = %d", top, bottom);
538  diagnostics(4,"left = %d, right = %d", left, right);
539  diagnostics(4,"width = %d, height = %d", width, height);
540  fprintRTF("\n{\\pict\\macpict\\picw%d\\pich%d\n", width, height);
541  if (scale != 1.0) {
542  int iscale = (int) (scale * 100);
543  fprintRTF("\\picscalex%d\\picscaley%d", iscale, iscale);
544  }
545 
546  fseek(fp, -10L, SEEK_CUR);
547  PutHexFile(fp);
548  fprintRTF("}\n");
549  fclose(fp);
550 }
551 
552 static void
553 GetPngSize(char *s, unsigned long *w, unsigned long *h)
554 /******************************************************************************
555  purpose : determine height and width of file
556  ******************************************************************************/
557 {
558 FILE *fp;
559 unsigned char buffer[16];
560 unsigned long width, height;
561 char reftag[9] = "\211PNG\r\n\032\n";
562 char refchunk[5] = "IHDR";
563 
564  *w = 0;
565  *h = 0;
566  fp = fopen(s, "rb");
567  if (fp == NULL) return;
568 
569  if (fread(buffer,1,16,fp)<16) {
570  diagnostics (WARNING__, "Cannot read graphics file <%s>", s);
571  fclose(fp);
572  return;
573  }
574 
575  if (memcmp(buffer,reftag,8)!=0 || memcmp(buffer+12,refchunk,4)!=0) {
576  diagnostics (WARNING__, "Graphics file <%s> is not a PNG file!", s);
577  fclose(fp);
578  return;
579  }
580 
581  if (fread(&width,4,1,fp)!=1 || fread(&height,4,1,fp)!=1) {
582  diagnostics (WARNING__, "Cannot read graphics file <%s>", s);
583  fclose(fp);
584  return;
585  }
586 
587  if (g_little_endian) {
588  width = LETONL(width);
589  height = LETONL(height);
590  }
591 
592  *w = width;
593  *h = height;
594  fclose(fp);
595 }
596 
597 void
598 PutPngFile(char * s, double scale, double baseline, int full_path)
599 /******************************************************************************
600  purpose : Include .png file in RTF
601  ******************************************************************************/
602 {
603  FILE *fp;
604  char *png;
605  unsigned long width, height, w, h, b;
606  int iscale;
607 
608  if (full_path)
609  png = strdup(s);
610  else
612  diagnostics(2, "PutPngFile <%s>",png);
613 
614  GetPngSize(png, &width, &height);
615 
616  diagnostics(4,"width = %ld, height = %ld, baseline = %g", width, height, baseline);
617 
618  if (width==0 || height==0) return;
619 
620  fp = fopen(png, "rb");
621  free(png);
622  if (fp == NULL) return;
623 
624  w = (unsigned long)( 100000.0*width )/ ( 20* POINTS_PER_M );
625  h = (unsigned long)( 100000.0*height )/ ( 20* POINTS_PER_M );
626  b = (unsigned long)( 100000.0*baseline*scale)/ ( 20* POINTS_PER_M );
627 
628  diagnostics(4,"width = %ld, height = %ld, baseline = %ld", w, h, b);
629 
630  fprintRTF("\n{");
631  if (b) fprintRTF("\\dn%ld",b);
632  fprintRTF("\\pict\\pngblip\\picw%ld\\pich%ld", w, h);
633  fprintRTF("\\picwgoal%ld\\pichgoal%ld", width*20, height*20);
634  if (scale != 1.0) {
635  iscale = (int) (scale * 100);
636  fprintRTF("\\picscalex%d\\picscaley%d", iscale, iscale);
637  }
638  fprintRTF("\n");
639  rewind(fp);
640  PutHexFile(fp);
641  fprintRTF("}\n");
642  fclose(fp);
643 }
644 
645 static void
646 PutJpegFile(char * s, double scale, double baseline, int full_path)
647 /******************************************************************************
648  purpose : Include .jpeg file in RTF
649  ******************************************************************************/
650 {
651  FILE *fp;
652  char *jpg;
653  unsigned short buffer[2];
654  int m,c;
655  unsigned short width, height;
656  unsigned long w, h;
657 
659  fp = fopen(jpg, "rb");
660  free(jpg);
661  if (fp == NULL) return;
662 
663  if ((c=fgetc(fp)) != 0xFF && (c=fgetc(fp)) != 0xD8) {
664  fclose(fp);
665  diagnostics(WARNING__, "<%s> is not really a JPEG file --- skipping");
666  return;
667  }
668 
669  do { /* Look for SOFn tag */
670 
671  while (!feof(fp) && fgetc(fp) != 0xFF){} /* Find 0xFF byte */
672 
673  while (!feof(fp) && (m=fgetc(fp)) == 0xFF){} /* Skip multiple 0xFFs */
674 
675  } while (!feof(fp) && m!=0xC0 && m!=0xC1 && m!=0xC2 && m!=0xC3 && m!=0xC5 && m!=0xC6 && m!=0xC7 &&
676  m!=0xC9 && m!=0xCA && m!=0xCB && m!=0xCD && m!=0xCE && m!=0xCF );
677 
678  if (fseek(fp, 3, SEEK_CUR) || fread(buffer,2,2,fp) != 2) {
679  diagnostics (WARNING__, "Cannot read graphics file <%s>", s);
680  fclose(fp);
681  return;
682  }
683 
684  width = buffer[1];
685  height = buffer[0];
686 
687  if (g_little_endian) {
688  width = (unsigned short) LETONS(width);
689  height = (unsigned short) LETONS(height);
690  }
691 
692  diagnostics(4,"width = %d, height = %d", width, height);
693 
694  w = (unsigned long)( 100000.0*width )/ ( 20* POINTS_PER_M );
695  h = (unsigned long)( 100000.0*height )/ ( 20* POINTS_PER_M );
696  fprintRTF("\n{\\pict\\jpegblip\\picw%ld\\pich%ld", w, h);
697  fprintRTF("\\picwgoal%ld\\pichgoal%ld\n", width*20, height*20);
698  if (scale != 1.0) {
699  int iscale = (int) (scale * 100);
700  fprintRTF("\\picscalex%d\\picscaley%d", iscale, iscale);
701  }
702 
703  rewind(fp);
704  PutHexFile(fp);
705  fprintRTF("}\n");
706  fclose(fp);
707 }
708 
709 static void
710 PutEmfFile(char *s, double scale, double baseline, int full_path)
711 {
712  FILE *fp;
713  char *emf;
714  unsigned long RecordType; /* Record type (always 0x00000001)*/
715  unsigned long RecordSize; /* Size of the record in bytes */
716  long BoundsLeft; /* Left inclusive bounds */
717  long BoundsRight; /* Right inclusive bounds */
718  long BoundsTop; /* Top inclusive bounds */
719  long BoundsBottom; /* Bottom inclusive bounds */
720  long FrameLeft; /* Left side of inclusive picture frame */
721  long FrameRight; /* Right side of inclusive picture frame */
722  long FrameTop; /* Top side of inclusive picture frame */
723  long FrameBottom; /* Bottom side of inclusive picture frame */
724  unsigned long Signature; /* Signature ID (always 0x464D4520) */
725  unsigned long w,h,width,height;
726 
727  if (full_path)
728  emf = strdup(s);
729  else
730  emf = strdup_together(g_home_dir, s);
731  diagnostics(1, "PutEmfFile <%s>",emf);
732  fp = fopen(emf,"rb");
733  free(emf);
734  if (fp == NULL) return;
735 
736 /* extract size information*/
737  if (fread(&RecordType,4,1,fp) != 1) goto out;
738  if (fread(&RecordSize,4,1,fp) != 1) goto out;
739  if (fread(&BoundsLeft,4,1,fp) != 1) goto out;
740  if (fread(&BoundsTop,4,1,fp) != 1) goto out;
741  if (fread(&BoundsRight,4,1,fp) != 1) goto out;
742  if (fread(&BoundsBottom,4,1,fp)!= 1) goto out;
743  if (fread(&FrameLeft,4,1,fp) != 1) goto out;
744  if (fread(&FrameRight,4,1,fp) != 1) goto out;
745  if (fread(&FrameTop,4,1,fp) != 1) goto out;
746  if (fread(&FrameBottom,4,1,fp) != 1) goto out;
747  if (fread(&Signature,4,1,fp) != 1) goto out;
748 
749  if (!g_little_endian) {
750  RecordType = LETONL(RecordType);
751  RecordSize = LETONL(RecordSize);
752  BoundsLeft = LETONL(BoundsLeft);
753  BoundsTop = LETONL(BoundsTop);
754  BoundsRight = LETONL(BoundsRight);
755  BoundsBottom = LETONL(BoundsBottom);
756  FrameLeft = LETONL(FrameLeft);
757  FrameRight = LETONL(FrameRight);
758  FrameTop = LETONL(FrameTop);
759  FrameBottom = LETONL(FrameBottom);
761  }
762 
763  if (RecordType != 1 || Signature != 0x464D4520) goto out;
764  height = (unsigned long) (BoundsBottom-BoundsTop);
765  width = (unsigned long) (BoundsRight-BoundsLeft);
766 
767  w = (unsigned long)(( 100000.0*width )/ ( 20* POINTS_PER_M ));
768  h = (unsigned long)(( 100000.0*height )/ ( 20* POINTS_PER_M ));
769  diagnostics(4,"width = %ld, height = %ld", width, height);
770  fprintRTF("\n{\\pict\\emfblip\\picw%ld\\pich%ld", w, h);
771  fprintRTF("\\picwgoal%ld\\pichgoal%ld\n", width*20, height*20);
772  if (scale != 1.0) {
773  int iscale = (int) (scale * 100);
774  fprintRTF("\\picscalex%d\\picscaley%d", iscale, iscale);
775  }
776 
777 /* write file */
778  rewind(fp);
779  PutHexFile(fp);
780  fprintRTF("}\n");
781  fclose(fp);
782  return;
783 
784 out:
785  diagnostics(WARNING__,"Problem with file %s --- not included",s);
786  fclose(fp);
787 }
788 
789 static void
790 PutWmfFile(char *s, double scale, double baseline, int full_path)
791 /******************************************************************************
792  purpose : Insert WMF file (from g_home_dir) into RTF file
793  ******************************************************************************/
794 {
795  FILE *fp;
796  char *wmf;
797  unsigned long Key; /* Magic number (always 0x9AC6CDD7) */
798  unsigned short FileType; /* Type of metafile (0=memory, 1=disk) */
799  unsigned short HeaderSize; /* Size of header in WORDS (always 9) */
800  unsigned short Handle; /* Metafile HANDLE number (always 0) */
801  short Left; /* Left coordinate in twips */
802  short Top; /* Top coordinate in twips */
803  short Right; /* Right coordinate in twips */
804  short Bottom; /* Bottom coordinate in twips */
805  int width, height;
806  unsigned long int magic_number = (unsigned long int) 0x9AC6CDD7;
807 
808  /* open the proper file */
810  diagnostics(1, "PutWmfFile <%s>", wmf);
811  fp = fopen(wmf, "rb");
812  free(wmf);
813  if (fp == NULL) return;
814 
815  /* verify file is actually WMF and get size */
816  if (fread(&Key,4,1,fp) != 1) goto out;
817  if (!g_little_endian) Key = LETONL(Key);
818 
819  if (Key == magic_number) { /* file is placeable metafile */
820  if (fread(&Handle,2,1,fp) != 1) goto out;
821  if (fread(&Left,2,1,fp) != 1) goto out;
822  if (fread(&Top,2,1,fp) != 1) goto out;
823  if (fread(&Right,2,1,fp) != 1) goto out;
824  if (fread(&Bottom,2,1,fp) != 1) goto out;
825 
826  if (!g_little_endian) {
827  Left = LETONS(Left);
828  Top = LETONS(Top);
829  Right = LETONS(Right);
830  Bottom = LETONS(Bottom);
831  }
832 
833  width = abs(Right - Left);
834  height = abs(Top-Bottom);
835 
836  } else { /* file may be old wmf file with no size */
837 
838  rewind(fp);
839  if (fread(&FileType,2,1,fp) != 1) goto out;
840  if (fread(&HeaderSize,2,1,fp) != 1) goto out;
841 
842  if (!g_little_endian) {
843  FileType = (unsigned short) LETONS(FileType);
844  HeaderSize = (unsigned short) LETONS(HeaderSize);
845  }
846 
847  if (FileType != 0 && FileType != 1) goto out;
848  if (HeaderSize != 9) goto out;
849 
850  /* real wmf file ... just assume size */
851  width = 200;
852  height = 200;
853  }
854 
855  diagnostics(4,"width = %d, height = %d", width, height);
856  fprintRTF("\n{\\pict\\wmetafile1\\picw%d\\pich%d\n", width, height);
857  if (scale != 1.0) {
858  int iscale = (int) (scale * 100);
859  fprintRTF("\\picscalex%d\\picscaley%d", iscale, iscale);
860  }
861 
862  rewind(fp);
863  PutHexFile(fp);
864  fprintRTF("}\n");
865  fclose(fp);
866  return;
867 
868 out:
869  diagnostics(WARNING__,"Problem with file %s --- not included",s);
870  fclose(fp);
871 }
872 
873 static void
874 PutPdfFile(char *s, double scale, double baseline, int full_path)
875 {
876  char *png;
877  diagnostics(2, "PutPdfFile filename = <%s>", s);
878 
879  png = pdf_to_png(s);
880  scale *= 72.0 / g_dots_per_inch;
881  if (png) {
882  PutPngFile(png, scale, baseline, TRUE);
883  my_unlink(png);
884  free(png);
885  }
886 }
887 
888 static void
889 PutEpsFile(char *s, double scale, double baseline, int full_path)
890 {
891  char *png, *emf, *pict;
892  diagnostics(2, "PutEpsFile filename = <%s>", s);
893 
894  if (1) {
895  png = eps_to_png(s);
896  scale *= 72.0 / g_dots_per_inch;
897  if (png) {
898  PutPngFile(png, scale, baseline, TRUE);
899  my_unlink(png);
900  free(png);
901  }
902  }
903 
904  if (0) {
905  pict = eps_to_pict(s);
906  if (pict) {
907  PutPictFile(pict, scale, baseline, TRUE);
908  my_unlink(pict);
909  free(pict);
910  }
911  }
912 
913  if (0) {
914  emf = eps_to_emf(s);
915  if (emf) {
916  PutEmfFile(emf, scale, baseline, TRUE);
917  my_unlink(emf);
918  free(emf);
919  }
920  }
921 }
922 
923 static void
924 PutTiffFile(char *s, double scale, double baseline, int full_path)
925 /******************************************************************************
926  purpose : Insert TIFF file (from g_home_dir) into RTF file as a PNG image
927  ******************************************************************************/
928 {
929  char *cmd, *tiff, *png, *tmp_png;
930  size_t cmd_len;
931 
932  diagnostics(1, "filename = <%s>", s);
933  png = strdup_new_extension(s, ".tiff", ".png");
934  if (png == NULL) {
935  png = strdup_new_extension(s, ".TIFF", ".png");
936  if (png == NULL) return;
937  }
938 
939  tmp_png = strdup_tmp_path(png);
941 
942  cmd_len = strlen(tiff)+strlen(tmp_png)+10;
943  cmd = (char *) malloc(cmd_len);
944  snprintf(cmd, cmd_len, "convert %s %s", tiff, tmp_png);
945  diagnostics(2, "system graphics command = [%s]", cmd);
946  system(cmd);
947 
948  PutPngFile(tmp_png, scale, baseline, TRUE);
949  my_unlink(tmp_png);
950 
951  free(tmp_png);
952  free(cmd);
953  free(tiff);
954  free(png);
955 }
956 
957 static void
958 PutGifFile(char *s, double scale, double baseline, int full_path)
959 /******************************************************************************
960  purpose : Insert GIF file (from g_home_dir) into RTF file as a PNG image
961  ******************************************************************************/
962 {
963  char *cmd, *gif, *png, *tmp_png;
964  size_t cmd_len;
965 
966  diagnostics(1, "filename = <%s>", s);
967  png = strdup_new_extension(s, ".gif", ".png");
968  if (png == NULL) {
969  png = strdup_new_extension(s, ".GIF", ".png");
970  if (png == NULL) return;
971  }
972 
973  tmp_png = strdup_tmp_path(png);
975 
976  cmd_len = strlen(gif)+strlen(tmp_png)+10;
977  cmd = (char *) malloc(cmd_len);
978  snprintf(cmd, cmd_len, "convert %s %s", gif, tmp_png);
979  diagnostics(2, "system graphics command = [%s]", cmd);
980  system(cmd);
981 
982  PutPngFile(tmp_png, scale, baseline, TRUE);
983  my_unlink(tmp_png);
984 
985  free(tmp_png);
986  free(cmd);
987  free(gif);
988  free(png);
989 }
990 
991 static int
993 /****************************************************************************
994 purpose: reads up to and and including a line ending (CR, CRLF, or LF)
995  ****************************************************************************/
996 {
997  int thechar;
998  while (1) {
999  thechar = getc(fp);
1000  if (thechar == EOF ) {fclose(fp); return 0;}
1001  if (thechar == 0x0a) return 1; /* LF */
1002  if (thechar == 0x0d) {
1003  thechar = getc(fp);
1004  if (thechar == EOF ) {fclose(fp); return 0;}
1005  if (thechar == 0x0d) return 1; /* CR LF */
1006  ungetc(thechar,fp); /* CR */
1007  return 1;
1008  }
1009  }
1010 }
1011 
1012 long
1013 GetBaseline(char *s, char *pre)
1014 /****************************************************************************
1015 purpose: reads a .pbm file to determine the baseline for an equation
1016  the .pbm file should have dimensions of 1xheight
1017  ****************************************************************************/
1018 {
1019  FILE *fp;
1020  int thechar;
1021  char *pbm;
1022  char magic[250];
1023  long baseline, width, height, items, top, bottom;
1024 
1025  /* baseline=0 if not an inline image */
1026  if ((strcmp(pre,"$")!=0) && (strcmp(pre,"\\begin{math}")!=0) && (strcmp(pre,"\\(")!=0))
1027  return 0;
1028 
1029  pbm = strdup_together(s,".pbm");
1030  baseline = 4;
1031 
1032  diagnostics(4, "GetBaseline opening=<%s>",pbm);
1033 
1034  fp = fopen(pbm, "rb");
1035  if (fp == NULL) {free(pbm); return baseline;}
1036 
1037  items = fscanf(fp,"%2s", magic); /* ensure that file begins with "P4" */
1038  if ((items!=1) || (strcmp(magic,"P4")!=0)) goto Exit;
1039 
1040  items = fscanf(fp," %s", magic);
1041  while ((items==1) && (magic[0]=='#')) { /* skip any comment lines in pbm file */
1042  if (!ReadLine(fp)) goto Exit;
1043  items = fscanf(fp,"%s", magic);
1044  }
1045 
1046  items = sscanf(magic, "%ld", &width); /* make sure image width is 1 */
1047  if ((items != 1) || (width != 1)) goto Exit;
1048 
1049  items=fscanf(fp," %ld", &height); /* read height */
1050  if (items != 1) goto Exit;
1051 
1052  diagnostics(4, "width=%ld height=%ld", width, height);
1053 
1054  if (!ReadLine(fp)) goto Exit; /* pixel map should start on next line */
1055 
1056  for (top=height; top>0; top--) { /* seek first black pixel (0x00) */
1057  thechar = getc(fp);
1058  if (thechar == EOF) goto Exit;
1059  if (thechar != 0 ) break;
1060  }
1061 
1062  for (bottom=top-1; bottom>0; bottom--) { /* seek first black pixel (0x00) */
1063  thechar = getc(fp);
1064  if (thechar == EOF) goto Exit;
1065  if (thechar == 0 ) break;
1066  }
1067 
1068  baseline = (bottom+top)/2;
1069 
1070  diagnostics(4, "top=%ld bottom=%ld baseline=%ld", top, bottom, baseline);
1071 Exit:
1072  free(pbm);
1073  fclose(fp);
1074  return baseline;
1075 }
1076 static char *
1078 {
1079 /*
1080 #ifdef MSDOS
1081  return strdup("command.com /e:2048 /c latex2pn");
1082 #else
1083 */
1084  return strdup_together(g_script_path, "latex2png");
1085 /*
1086 #endif
1087 */
1088 }
1089 void
1090 PutLatexFile(char *s, double scale, char *pre)
1091 /******************************************************************************
1092  purpose : Convert LaTeX to Bitmap and insert in RTF file
1093  ******************************************************************************/
1094 {
1095  char *png, *cmd, *l2p;
1096  int err, baseline,second_pass;
1097  size_t cmd_len;
1098  unsigned long width, height, rw, rh;
1099  unsigned long maxsize=(unsigned long) (32767.0/20.0);
1100  int resolution = g_dots_per_inch; /*points per inch */
1101 
1102  diagnostics(4, "Entering PutLatexFile");
1103 
1104  png = strdup_together(s,".png");
1105  l2p = get_latex2png_name();
1106 
1107  cmd_len = strlen(l2p)+strlen(s)+25;
1108  if (g_home_dir)
1109  cmd_len += strlen(g_home_dir);
1110 
1111  cmd = (char *) malloc(cmd_len);
1112 
1113  do {
1114  second_pass = FALSE; /* only needed if png is too large for Word */
1115  if (g_home_dir==NULL)
1116  snprintf(cmd, cmd_len, "%s -d %d %s", l2p, resolution, s);
1117  else
1118  snprintf(cmd, cmd_len, "%s -d %d -H \"%s\" %s", l2p, resolution, g_home_dir, s);
1119 
1120  diagnostics(2, "system graphics command = [%s]", cmd);
1121  err=system(cmd);
1122  if (err) break;
1123 
1124  GetPngSize(png, &width, &height);
1125  baseline=GetBaseline(s, pre);
1126  diagnostics(4, "png size height=%d baseline=%d width=%d", height, baseline, width);
1127 
1128  if( (width>maxsize && height!=0) || (height>maxsize && width!=0) ){
1129  second_pass = TRUE;
1130  rw=(unsigned long) ((resolution*maxsize)/width);
1131  rh=(unsigned long) ((resolution*maxsize)/height);
1132  resolution=rw<rh?(int)rw:(int)rh;
1133  }
1134  } while (resolution>10 && ( (width>maxsize) || (height>maxsize)) );
1135 
1136  if (err==0)
1137  PutPngFile(png, scale*72.0/resolution, (double) baseline, TRUE);
1138 
1139  free(l2p);
1140  free(png);
1141  free(cmd);
1142 }
1143 
1144 char * upper_case_string(char *s)
1145 {
1146  char *t, *x;
1147 
1148  if (!s) return NULL;
1149 
1150  t=strdup(s);
1151  x=t;
1152 
1153  while (*x) {
1154  if (islower(*x)) *x = toupper(*x);
1155  x++;
1156  }
1157 
1158  return t;
1159 }
1160 
1161 char * exists_with_extension(char *s, char *ext)
1162 /******************************************************************************
1163  purpose : return s.ext or s.EXT if it exists otherwise return NULL
1164  ******************************************************************************/
1165 {
1166  char *t,*x;
1167  FILE *fp;
1168 
1170  fp=fopen(t,"r");
1171  diagnostics(4,"trying to open %s, result = %0x",t,fp);
1172  if (fp) {
1173  fclose(fp);
1174  return t;
1175  }
1176  free(t);
1177 
1178 /* now try upper case version of ext */
1180  t=strdup_together(s,x);
1181  free(x);
1182 
1183  fp=fopen(t,"r");
1184  diagnostics(4,"trying to open %s, result = %0x",t,fp);
1185  if (fp) {
1186  fclose(fp);
1187  return t;
1188  }
1189  free(t);
1190  return NULL;
1191 }
1192 
1193 int has_extension(char *s, char *ext)
1194 /******************************************************************************
1195  purpose : return true if ext is at end of s (case insensitively)
1196  ******************************************************************************/
1197 {
1198  char *t;
1199 
1200  t = s + strlen(s) - strlen(ext);
1201 #ifdef WIN32
1202  if (stricmp(t,ext)==0) return TRUE;
1203 #else
1204  if (strcasecmp(t,ext)==0) return TRUE;
1205 #endif
1206 
1207  return FALSE;
1208 }
1209 
1211 {
1212  char *t;
1213 
1214  if (has_extension(s, ".pict") ||
1215  has_extension(s, ".png") ||
1216  has_extension(s, ".gif") ||
1217  has_extension(s, ".emf") ||
1218  has_extension(s, ".wmf") ||
1219  has_extension(s, ".eps") ||
1220  has_extension(s, ".pdf") ||
1221  has_extension(s, ".ps") ||
1222  has_extension(s, ".tiff") ||
1223  has_extension(s, ".tif") ||
1224  has_extension(s, ".jpg") ||
1225  has_extension(s, ".jpeg"))
1226  return strdup(s);
1227 
1228  t = exists_with_extension(s,".png");
1229  if (t) return t;
1230 
1231  t = exists_with_extension(s,".jpg");
1232  if (t) return t;
1233 
1234  t = exists_with_extension(s,".jpeg");
1235  if (t) return t;
1236 
1237  t = exists_with_extension(s,".tif");
1238  if (t) return t;
1239 
1240  t = exists_with_extension(s,".tiff");
1241  if (t) return t;
1242 
1243  t = exists_with_extension(s,".gif");
1244  if (t) return t;
1245 
1246  t = exists_with_extension(s,".eps");
1247  if (t) return t;
1248 
1249  t = exists_with_extension(s,".pdf");
1250  if (t) return t;
1251 
1252  t = exists_with_extension(s,".ps");
1253  if (t) return t;
1254 
1255  t = exists_with_extension(s,".pict");
1256  if (t) return t;
1257 
1258  t = exists_with_extension(s,".emf");
1259  if (t) return t;
1260 
1261  t = exists_with_extension(s,".wmf");
1262  if (t) return t;
1263 
1264  /* failed to find any file */
1265  return strdup(s);
1266 
1267 }
1268 
1269 void
1271 /*
1272 \includegraphics[parameters]{filename}
1273 
1274 where parameters is a comma-separated list of any of the following:
1275 bb=llx lly urx ury (bounding box),
1276 width=h_length,
1277 height=v_length,
1278 angle=angle,
1279 scale=factor,
1280 clip=true/false,
1281 draft=true/false.
1282 
1283 code=0 => includegraphics
1284 code=1 => epsffile
1285 code=2 => epsfbox
1286 code=3 => \BoxedSPSF
1287 code=4 => psfig
1288 */
1289 {
1290  char *options,*options2;
1291  char *filename,*fullpathname, *fullname;
1292  double scale=1.0;
1293  double baseline=0.0;
1294  double x;
1295  char *p;
1296 
1297  if (code==0) { /* could be \includegraphics*[0,0][5,5]{file.pict} */
1299  options2 = getBracketParam();
1300  if (options2) free(options2);
1301 
1302  if (options) { /* \includegraphics[scale=0.5]{file.png} */
1303  p = strstr(options,"scale");
1304  if (p) {
1305  p = strchr(p,'=');
1306  if (p && (sscanf(p+1,"%lf", &x) == 1)) scale = x;
1307  }
1308  free(options);
1309  }
1310  filename = getBraceParam();
1311  diagnostics(1, "image scale = %g", scale);
1312  }
1313 
1314  if (code==1) { /* \epsffile{filename.eps} */
1315  filename = getBraceParam();
1316  }
1317 
1318  if (code==2) { /* \epsfbox[0 0 30 50]{filename.ps} */
1320  if (options) free(options);
1321  filename = getBraceParam();
1322  }
1323 
1324  if (code==3) { /* \BoxedEPSF{filename [scaled nnn]} */
1325  char *s;
1326  filename = getBraceParam();
1327  s= strchr(filename,' ');
1328  if (s) *s='\0';
1329  }
1330 
1331  if (code==4) { /* \psfig{figure=filename,height=hhh,width=www} */
1332  char *s, *t;
1333  filename = getBraceParam();
1334  s = strstr(filename,"figure=");
1335  if (!s) return;
1336  s += strlen("figure=");
1337  t = strchr(s,',');
1338  if (t) *t='\0';
1339  t = strdup(s);
1340  free(filename);
1341  filename = t;
1342  }
1343 
1345 
1346  fullname=strdup_absolute_path(filename);
1347  fullpathname=append_graphic_extension(fullname);
1348  free(fullname);
1349 
1350  if (has_extension(fullpathname, ".pict"))
1351  PutPictFile(fullpathname, scale, baseline, TRUE);
1352 
1353  else if (has_extension(fullpathname, ".png"))
1354  PutPngFile(fullpathname, scale, baseline, TRUE);
1355 
1356  else if (has_extension(fullpathname, ".gif"))
1357  PutGifFile(fullpathname, scale, baseline, TRUE);
1358 
1359  else if (has_extension(fullpathname, ".emf"))
1360  PutEmfFile(fullpathname, scale, baseline, TRUE);
1361 
1362  else if (has_extension(fullpathname, ".wmf"))
1363  PutWmfFile(fullpathname, scale, baseline, TRUE);
1364 
1365  else if (has_extension(fullpathname, ".eps"))
1366  PutEpsFile(fullpathname, scale, baseline, TRUE);
1367 
1368  else if (has_extension(fullpathname, ".pdf"))
1369  PutPdfFile(fullpathname, scale, baseline, TRUE);
1370 
1371  else if (has_extension(fullpathname, ".ps"))
1372  PutEpsFile(fullpathname, scale, baseline, TRUE);
1373 
1374  else if (has_extension(fullpathname, ".tiff"))
1375  PutTiffFile(fullpathname, scale, baseline, TRUE);
1376 
1377  else if (has_extension(fullpathname, ".tif"))
1378  PutTiffFile(fullpathname, scale, baseline, TRUE);
1379 
1380  else if (has_extension(fullpathname, ".jpg"))
1381  PutJpegFile(fullpathname, scale, baseline, TRUE);
1382 
1383  else if (has_extension(fullpathname, ".jpeg"))
1384  PutJpegFile(fullpathname, scale, baseline, TRUE);
1385 
1386  else
1387  diagnostics(WARNING__, "Conversion of '%s' not supported", filename);
1388 
1389  free(filename);
1390  free(fullpathname);
1391 }
1392 
1393 void
1395 /******************************************************************************
1396  purpose: handle \begin{picture} ... \end{picture}
1397  by converting to png image and inserting
1398  ******************************************************************************/
1399 {
1400  char *pre, *post, *picture;
1401 
1402  if (code & ON) {
1403  pre = strdup("\\begin{picture}");
1404  post = strdup("\\end{picture}");
1407  ConvertString(post); /* to balance the \begin{picture} */
1408  free(pre);
1409  free(post);
1410  free(picture);
1411  }
1412 }
1413 
1414 void
1416 /******************************************************************************
1417  purpose: Process \begin{music} ... \end{music} environment
1418  ******************************************************************************/
1419 {
1420  char *contents;
1421  char endmusic[] = "\\end{music}";
1422 
1423  if (!(code & ON)) {
1424  diagnostics(4, "exiting CmdMusic");
1425  return;
1426  }
1427 
1428  diagnostics(4, "entering CmdMusic");
1429  contents = getTexUntil(endmusic, TRUE);
1430  CmdEndParagraph(0);
1434  WriteLatexAsBitmap("\\begin{music}",contents,endmusic);
1435  ConvertString(endmusic);
1436  CmdEndParagraph(0);
1439  free(contents);
1440 }
#define strdup
Definition: Utility.h:167
#define width(a)
Definition: aptex-macros.h:198
#define height(a)
Definition: aptex-macros.h:200
static boolean second_pass
Definition: aptex.h:593
static char * options
Definition: bmeps.c:236
#define b
Definition: jpegint.h:372
#define MODE_HORIZONTAL
Definition: convert.h:2
@ pre
Definition: ctie-k.c:71
@ FALSE
Definition: dd.h:101
@ TRUE
Definition: dd.h:102
#define free(a)
Definition: decNumber.cpp:310
#define strrchr
Definition: detex.c:67
int w
Definition: dviconv.c:26
int h
Definition: dviconv.c:9
#define fopen
Definition: xxstdio.h:21
#define fread
Definition: xxstdio.h:25
#define fseek
Definition: xxstdio.h:30
#define fgets
Definition: xxstdio.h:29
#define fgetc
Definition: xxstdio.h:26
#define ftell
Definition: xxstdio.h:31
int strcmp()
Definition: coll.cpp:143
int sscanf()
char * strcpy()
int Signature
Definition: dvibook.c:88
#define PATHSEP
Definition: dvidvi.c:105
void Exit(int code)
Definition: dvispc.c:374
void WriteLatexAsBitmap(char *pre, char *eq, char *post)
Definition: equation.c:277
void CmdIndent(int code)
Definition: funct1.c:339
void CmdVspace(int code)
Definition: funct1.c:169
void CmdEndParagraph(int code)
Definition: funct1.c:146
void CmdStartParagraph(int code)
Definition: funct1.c:65
#define INDENT_INHIBIT
Definition: funct1.h:45
#define FIRST_PAR
Definition: funct1.h:40
#define VSPACE_SMALL_SKIP
Definition: funct1.h:59
#define INDENT_NONE
Definition: funct1.h:44
#define s
Definition: afcover.h:80
#define t
Definition: afcover.h:96
#define c(n)
Definition: gpos-common.c:150
#define strchr
Definition: gsftopk.c:59
#define memcmp(s1, s2, n)
Definition: gsftopk.c:66
FILE * out
Definition: hbf2gf.c:286
#define byte
Definition: in_pcx.cpp:28
#define SEEK_SET
Definition: jmemansi.c:26
#define NULL
Definition: ftobjs.h:61
small capitals from c petite p
Definition: afcover.h:72
small capitals from c petite p scientific i
Definition: afcover.h:80
#define SEEK_CUR
Definition: ftzconf.h:250
#define SEEK_END
Definition: ftzconf.h:251
#define EOF
Definition: afmparse.c:59
@ right
Definition: annotate.c:15
int int double double double char double char * top
Definition: gdfx.h:19
int int double double double char double char char * bottom
Definition: gdfx.h:20
#define unlink(file)
Definition: config.h:413
#define strcasecmp
Definition: c-auto.h:150
char * strstr()
#define fclose
Definition: debug.h:100
#define getc
Definition: line.c:39
#define popen(cmd, mode)
Definition: mingw32.h:91
#define pclose(file)
Definition: win32lib.h:271
#define stricmp
Definition: win32lib.h:93
#define system(p)
Definition: win32lib.h:269
void ConvertString(char *string)
Definition: convert.c:103
void SetTexMode(int mode)
Definition: convert.c:78
void diagnostics(int level, char *format,...)
Definition: main.c:469
char * g_home_dir
Definition: main.c:59
void fprintRTF(char *format,...)
Definition: main.c:722
bool g_little_endian
Definition: main.c:75
int g_dots_per_inch
Definition: main.c:76
char * getTmpPath(void)
Definition: main.c:762
char * g_script_path
Definition: main.c:102
#define WARNING__
Definition: main.h:32
static int Top
Definition: type1.c:714
Code related to b fwrite(a, sizeof(char), b, stdout) @d C_printf(c
#define rh
Definition: texmfmem.h:173
#define malloc
Definition: alloca.c:91
#define post
Definition: tie.c:144
int ii
Definition: main.c:50
#define snprintf
Definition: snprintf.c:41
char * getTexUntil(char *target, int raw)
char * getBraceParam(void)
char * getBracketParam(void)
#define Handle
Definition: aliases.h:45
static int return_value
Definition: messages.c:35
#define cmd(arg)
Definition: mitsu.h:44
@ err
Definition: mtxline.h:24
float x
Definition: cordic.py:15
Definition: pdf.py:1
#define abs(a)
Definition: pbmplus.h:225
#define odd(n)
Definition: pbmplus.h:227
integer resolution
Definition: pbmtopk.c:45
char * filename[256]
Definition: pbmtopk.c:46
static bool tiff
Definition: pdftocairo.cc:97
static bool png
Definition: pdftocairo.cc:89
static bool eps
Definition: pdftocairo.cc:92
static int items
Definition: pgmtofs.c:86
#define fp
double scale
Definition: pnmhistmap.c:38
static int size
Definition: ppmlabel.c:24
type_C toupper(type_C)
#define islower(ch)
Definition: utype.h:79
lft_cell * left
Definition: routines.h:73
#define HeaderSize
Definition: sgi.h:17
unsigned long Version
Definition: graphics.c:126
unsigned long Signature
Definition: graphics.c:125
unsigned long OffsetToData
Definition: graphics.c:128
unsigned long Data
Definition: graphics.c:127
unsigned long RecordType
Definition: graphics.c:98
unsigned long RecordSize
Definition: graphics.c:99
unsigned long NumPalEntries
Definition: graphics.c:116
unsigned long Version
Definition: graphics.c:109
unsigned short Reserved
Definition: graphics.c:113
unsigned long SizeOfDescrip
Definition: graphics.c:114
unsigned long Size
Definition: graphics.c:110
unsigned long OffsOfDescrip
Definition: graphics.c:115
unsigned long Signature
Definition: graphics.c:108
unsigned short NumOfHandles
Definition: graphics.c:112
unsigned long NumOfRecords
Definition: graphics.c:111
unsigned long NumFormats
Definition: graphics.c:141
unsigned long Identifier
Definition: graphics.c:135
unsigned long Comment
Definition: graphics.c:136
EMR_FORMAT * Data
Definition: graphics.c:142
unsigned short Handle
Definition: graphics.c:86
unsigned long Reserved
Definition: graphics.c:92
unsigned short Checksum
Definition: graphics.c:93
unsigned short Inch
Definition: graphics.c:91
unsigned long Key
Definition: graphics.c:85
unsigned long FileSize
Definition: graphics.c:77
unsigned short HeaderSize
Definition: graphics.c:75
unsigned short NumOfObjects
Definition: graphics.c:78
unsigned short Version
Definition: graphics.c:76
unsigned short NumOfParams
Definition: graphics.c:80
unsigned short FileType
Definition: graphics.c:74
unsigned long MaxRecordSize
Definition: graphics.c:79
Definition: utils.c:300
Definition: inftrees.h:24
Definition: ttf.h:449
Definition: dvips.h:235
Definition: xmlparse.c:179
Definition: tiffiop.h:70
struct def_label label[1024]
Definition: t1part.c:286
type_key Key[]
Definition: t1part.c:271
#define FILE
Definition: t1stdio.h:34
#define ungetc(c, f)
Definition: t1stdio.h:106
#define feof(f)
Definition: t1stdio.h:109
s1
Definition: t4ht.c:1059
char * ext
Definition: t4ht.c:938
*job_name strlen((char *) job_name) - 4)
tag int tag
Definition: tex4ht.c:2554
m
Definition: tex4ht.c:3990
static char * gif
Definition: tex4ht.c:1229
return() int(((double) *(font_tbl[cur_fnt].wtbl+(int)(*(font_tbl[cur_fnt].char_wi+(int)(ch - font_tbl[cur_fnt].char_f)% 256)))/(double)(1L<< 20)) *(double) font_tbl[cur_fnt].scale)
static void PutEpsFile(char *s, double scale, double baseline, int full_path)
Definition: graphics.c:889
int has_extension(char *s, char *ext)
Definition: graphics.c:1193
static void PutGifFile(char *s, double scale, double baseline, int full_path)
Definition: graphics.c:958
static char * eps_to_emf(char *eps)
Definition: graphics.c:429
void CmdGraphics(int code)
Definition: graphics.c:1270
static char * pdf_to_png(char *pdf)
Definition: graphics.c:400
char * exists_with_extension(char *s, char *ext)
Definition: graphics.c:1161
static void PutJpegFile(char *s, double scale, double baseline, int full_path)
Definition: graphics.c:646
void PutLatexFile(char *s, double scale, char *pre)
Definition: graphics.c:1090
static void PutPictFile(char *s, double scale, double baseline, int full_path)
Definition: graphics.c:495
struct _EnhancedMetaHeader ENHANCEDMETAHEADER
static char * strdup_tmp_path(char *s)
Definition: graphics.c:213
static void GetPngSize(char *s, unsigned long *w, unsigned long *h)
Definition: graphics.c:553
static void PicComment(short label, short size, FILE *fp)
Definition: graphics.c:157
static void PutEmfFile(char *s, double scale, double baseline, int full_path)
Definition: graphics.c:710
char * append_graphic_extension(char *s)
Definition: graphics.c:1210
void PutPngFile(char *s, double scale, double baseline, int full_path)
Definition: graphics.c:598
static char * get_latex2png_name()
Definition: graphics.c:1077
struct _PlaceableMetaHeader PLACEABLEMETAHEADER
static void PutPdfFile(char *s, double scale, double baseline, int full_path)
Definition: graphics.c:874
struct _GdiCommentMultiFormats GDICOMMENTMULTIFORMATS
static int ReadLine(FILE *fp)
Definition: graphics.c:992
static char * strdup_new_extension(char *s, char *old_ext, char *new_ext)
Definition: graphics.c:179
static char * strdup_absolute_path(char *s)
Definition: graphics.c:193
static char * eps_to_png(char *eps)
Definition: graphics.c:369
static void PutHexFile(FILE *fp)
Definition: graphics.c:477
long GetBaseline(char *s, char *pre)
Definition: graphics.c:1013
#define POINTS_PER_M
Definition: graphics.c:44
char * upper_case_string(char *s)
Definition: graphics.c:1144
static void PutWmfFile(char *s, double scale, double baseline, int full_path)
Definition: graphics.c:790
void CmdMusic(int code)
Definition: graphics.c:1415
static void PutTiffFile(char *s, double scale, double baseline, int full_path)
Definition: graphics.c:924
static void my_unlink(char *filename)
Definition: graphics.c:146
void CmdPicture(int code)
Definition: graphics.c:1394
#define LETONS(A)
Definition: graphics.c:46
struct _WindowsMetaHeader WMFHEAD
static char * eps_to_pict(char *s)
Definition: graphics.c:238
#define LETONL(A)
Definition: graphics.c:47
char * strdup_together(char *s, char *t)
Definition: util.c:95
int iscale(int val)
Definition: ttf2pt1.c:1203
@ ON
Definition: ubidiimp.h:55