forked from magsilva/latex2rtf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.c
2486 lines (2036 loc) · 79.4 KB
/
graphics.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* graphics.c - routines that handle LaTeX graphics commands
Copyright (C) 2001-2010 The Free Software Foundation
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
This file is available from http://sourceforge.net/projects/latex2rtf/
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdint.h>
#include "main.h"
#ifdef UNIX
#include <unistd.h>
#endif
#include "cfg.h"
#include "graphics.h"
#include "parser.h"
#include "utils.h"
#include "commands.h"
#include "convert.h"
#include "funct1.h"
#include "preamble.h"
#include "counters.h"
#include "vertical.h"
#include "fields.h"
/* number of points (72/inch) in a meter */
#define POINTS_PER_METER 2834.65
/* Little endian macros to convert to and from host format to network byte ordering */
#define LETONS(A) ((((A) & 0xFF00) >> 8) | (((A) & 0x00FF) << 8))
#define LETONL(A) ((((A) & 0xFF000000) >> 24) | (((A) & 0x00FF0000) >> 8) | \
(((A) & 0x0000FF00) << 8) | (((A) & 0x000000FF) << 24) )
/*
Version 1.6 RTF files can include pictures as follows
<pict> '{' \pict (<brdr>? & <shading>? & <picttype> & <pictsize> & <metafileinfo>?) <data> '}'
<picttype> \emfblip | \pngblip | \jpegblip | \macpict | \pmmetafile | \wmetafile
| \dibitmap <bitmapinfo> | \wbitmap <bitmapinfo>
<bitmapinfo> \wbmbitspixel & \wbmplanes & \wbmwidthbytes
<pictsize> (\picw & \pich) \picwgoal? & \pichgoal? \picscalex? & \picscaley? & \picscaled? & \piccropt? & \piccropb? & \piccropr? & \piccropl?
<metafileinfo> \picbmp & \picbpp
<data> (\bin #BDATA) | #SDATA
\emfblip Source of the picture is an EMF (enhanced metafile).
\pngblip Source of the picture is a PNG.
\jpegblip Source of the picture is a JPEG.
\shppict Specifies a Word 97-2000 picture. This is a destination control word.
\nonshppict Specifies that Word 97-2000 has written a {\pict destination that it
will not read on input. This keyword is for compatibility with other readers.
\macpict Source of the picture is PICT file (Quickdraw)
\pmmetafileN Source of the picture is an OS/2 metafile
\wmetafileN Source of the picture is a Windows metafile
\dibitmapN Source of the picture is a Windows device-independent bitmap
\wbitmapN Source of the picture is a Windows device-dependent bitmap
*/
typedef struct _WindowsMetaHeader {
uint16_t FileType; /* Type of metafile (0=memory, 1=disk) */
uint16_t HeaderSize; /* Size of header in WORDS (always 9) */
uint16_t Version; /* Version of Microsoft Windows used */
uint32_t FileSize; /* Total size of the metafile in WORDs */
uint16_t NumOfObjects; /* Number of objects in the file */
uint32_t MaxRecordSize; /* The size of largest record in WORDs */
uint16_t NumOfParams; /* Not Used (always 0) */
} WMFHEAD;
typedef struct _PlaceableMetaHeader {
uint32_t Key; /* Magic number (always 0x9AC6CDD7) */
uint16_t Handle; /* Metafile HANDLE number (always 0) */
int16_t Left; /* Left coordinate in twips */
int16_t Top; /* Top coordinate in twips */
int16_t Right; /* Right coordinate in twips */
int16_t Bottom; /* Bottom coordinate in twips */
uint16_t Inch; /* Scaling factor, 1440 => 1:1, 360 => 4:1, 2880 => 1:2 (half size) */
uint32_t Reserved; /* Reserved (always 0) */
uint16_t Checksum; /* Checksum value for previous 10 WORDs */
} PLACEABLEMETAHEADER;
typedef struct _EnhancedMetaHeader {
uint32_t RecordType; /* Record type (always 0x00000001) */
uint32_t RecordSize; /* Size of the record in bytes */
int32_t BoundsLeft; /* Left inclusive bounds */
int32_t BoundsTop; /* Top inclusive bounds */
int32_t BoundsRight; /* Right inclusive bounds */
int32_t BoundsBottom; /* Bottom inclusive bounds */
int32_t FrameLeft; /* Left side of inclusive picture frame */
int32_t FrameTop; /* Top side of inclusive picture frame */
int32_t FrameRight; /* Right side of inclusive picture frame */
int32_t FrameBottom; /* Bottom side of inclusive picture frame */
uint32_t Signature; /* Signature ID (always 0x464D4520) */
uint32_t Version; /* Version of the metafile */
uint32_t Size; /* Size of the metafile in bytes */
uint32_t NumOfRecords; /* Number of records in the metafile */
uint16_t NumOfHandles; /* Number of handles in the handle table */
uint16_t Reserved; /* Not used (always 0) */
uint32_t SizeOfDescrip; /* Size of description string in WORDs */
uint32_t OffsOfDescrip; /* Offset of description string in metafile */
uint32_t NumPalEntries; /* Number of color palette entries */
int32_t WidthDevPixels; /* Width of reference device in pixels */
int32_t HeightDevPixels; /* Height of reference device in pixels */
int32_t WidthDevMM; /* Width of reference device in millimeters */
int32_t HeightDevMM; /* Height of reference device in millimeters */
} ENHANCEDMETAHEADER;
typedef struct _EmrFormat {
uint32_t Signature; /* 0x46535045 for EPS, 0x464D4520 for EMF */
uint32_t Version; /* EPS version number or 0x00000001 for EMF */
uint32_t Data; /* Size of data in bytes */
uint32_t OffsetToData; /* Offset to data */
} EMRFORMAT;
typedef struct _GdiCommentMultiFormats {
uint32_t Identifier; /* Comment ID (0x43494447) */
uint32_t Comment; /* Multiformats ID (0x40000004) */
int32_t BoundsLeft; /* Left side of bounding rectangle */
int32_t BoundsRight; /* Right side of bounding rectangle */
int32_t BoundsTop; /* Top side of bounding rectangle */
int32_t BoundsBottom; /* Bottom side of bounding rectangle */
uint32_t NumFormats; /* Number of formats in comment */
EMRFORMAT *Data; /* Array of comment data */
} GDICOMMENTMULTIFORMATS;
/**********************************
table for graphics format handling
**********************************/
typedef void PutFileFnc(char *, double, double, double, double);
typedef struct {
char *extension;
PutFileFnc *encoder;
} GraphConvertElement;
static void PutPictFile(char *, double, double, double, double);
static void PutPngFile (char *, double, double, double, double);
static void PutJpegFile(char *, double, double, double, double);
static void PutEmfFile (char *, double, double, double, double);
static void PutWmfFile (char *, double, double, double, double);
static void PutPdfFile (char *, double, double, double, double);
static void PutEpsFile (char *, double, double, double, double);
static void PutPsFile (char *, double, double, double, double);
static void PutTiffFile(char *, double, double, double, double);
static void PutGifFile (char *, double, double, double, double);
GraphConvertElement GraphConvertTable[] = {
{ ".png", PutPngFile },
{ ".pdf", PutPdfFile },
{ ".jpg", PutJpegFile },
{ ".jpeg", PutJpegFile },
{ ".gif", PutGifFile },
{ ".eps", PutEpsFile },
{ ".tiff", PutTiffFile },
{ ".tif", PutTiffFile },
{ ".ps", PutPsFile },
{ ".pict", PutPictFile },
{ ".emf", PutEmfFile },
{ ".wmf", PutWmfFile },
{ NULL, NULL }
};
/********************************************************************************
purpose: implement \graphicspath{{dir1}{dir2}{...}}
********************************************************************************/
static char **graphicsPath = NULL;
static int nGraphicsPathElems = 0;
static void appendGraphicsPath (char *newPath)
{
int i;
void *ptr;
char *add;
for (i = 0; i < nGraphicsPathElems; i++)
{
if (streq (graphicsPath[i], newPath)) {
diagnostics(WARNING,"Repeated graphics path element {%s}",newPath);
return;
}
}
ptr = (void *) graphicsPath;
ptr = realloc (ptr, sizeof (char *) * (nGraphicsPathElems + 1));
graphicsPath = (char **) ptr;
/* path must end with a '/' */
if (*(newPath+strlen(newPath)-1) == '/')
add = strdup(newPath);
else
add = strdup_together(newPath,"/");
graphicsPath[nGraphicsPathElems++] = add;
diagnostics (WARNING, "Included %s in graphics search path", add);
}
void CmdGraphicsPath(int code)
{
char *directories = getBraceParam();
if (directories != NULL) {
char *candidate=strtok(directories,"{}");
while (NULL != candidate) {
appendGraphicsPath(candidate);
candidate = strtok(NULL,"{}");
}
}
safe_free(directories);
}
#define CONVERT_SIMPLE 1
#define CONVERT_CROP 2
#define CONVERT_LATEX_TO_PNG 3
#define CONVERT_LATEX_TO_EPS 4
#define CONVERT_PDF 5
#define CONVERT_PS_TO_EPS 6
static char *g_psset_info = NULL;
static char *g_psstyle_info = NULL;
static char *tikzlibs[32];
static int tikzlibsnum = 0;
/******************************************************************************
purpose : portable routine to delete filename
******************************************************************************/
static void my_unlink(char *filename)
{
#ifdef UNIX
unlink(filename);
#endif
}
/******************************************************************************
purpose : create a tmp file name using only the end of the filename
******************************************************************************/
static char *strdup_tmp_path(const char *s)
{
char *tmp, *p, *fullname;
if (s == NULL)
return NULL;
tmp = getTmpPath();
/* Even in Windows, LaTeX uses the forward slash as path separator */
p = strrchr(s, '/');
if (!p)
fullname = strdup_together(tmp, s);
else
fullname = strdup_together(tmp, p + 1);
safe_free(tmp);
return fullname;
}
/******************************************************************************
purpose : System specific shell graphics commands
Need to worry about the global flags used on the command-line
-D dpi g_dots_per_inch
-P path g_script_dir
-T /path/to/tmp g_tmp_path
We need e.g. to create a system command to convert a PDF to a PNG file.
In principle, this could be as simple as
convert file.pdf file.png
Unfortunately, we need to specify the pixel density, and more importantly,
crop whitespace out of the images appropriately. The command then becomes
convert -crop 0x0 -units PixelsPerInch -density 300 file.pdf file.png
Now the problem arises that apparently ImageMagick reads the wrong /MediaBox
for PDF files and this gives a full-page image. Since GhostScript is
required for ImageMagick, the solution is to use GhostScript directly
gs -dNOPAUSE -dSAFER -dBATCH -sDEVICE=pngalpha -sOutputFile=file.png -r300 file.pdf
Unfortunately, this fails to work under Windows XP because gs (gswin32c)
fails to execute with message "Program too large for working storage"
(in German: "Programm zu gross fuer den Arbeitsspeicher"). Wilfried wrote
the script pdf2pnga to solve this problem.
So here we are, creating different commands for Windows XP and Unix!
Parameters:
opt type of conversion
offset vertical offset
in input filename
out output filename
******************************************************************************/
static char *SysGraphicsConvert(int opt, int offset, uint16_t dpi, const char *in, const char *out)
{
char cmd[512], *out_tmp;
int err;
int N = 511;
diagnostics(3, "SysGraphicsConvert '%s' to '%s'", in, out);
out_tmp = strdup_tmp_path(out);
if (in == NULL || out == NULL || out_tmp == NULL)
return NULL;
#ifdef UNIX
if (strchr(in, (int) '\'')) {
diagnostics(WARNING, "single quote found in filename '%s'. skipping conversion", in);
free(out_tmp);
return NULL;
}
if (out_tmp && strchr(out_tmp, (int) '\'')) {
diagnostics(WARNING, "single quote found in filename '%s'. skipping conversion", out_tmp);
free(out_tmp);
return NULL;
}
if (opt == CONVERT_SIMPLE) {
const char format_simple[] = "convert '%s' '%s'";
snprintf(cmd, N, format_simple, in, out_tmp);
}
if (opt == CONVERT_CROP) {
const char format_crop[] = "convert -trim +repage -units PixelsPerInch -density %d '%s' '%s'";
snprintf(cmd, N, format_crop, dpi, in, out_tmp);
}
if (opt == CONVERT_LATEX_TO_PNG) {
if (g_home_dir == NULL) {
const char format_unix[] = "%slatex2png -d %d -o %d '%s'";
if (g_script_dir)
snprintf(cmd, N, format_unix, g_script_dir, dpi, offset, in);
else
snprintf(cmd, N, format_unix, "", dpi, offset, in);
} else {
const char format_unix[] = "%slatex2png -k -d %d -o %d -H '%s' '%s'";
if (g_script_dir)
snprintf(cmd, N, format_unix, g_script_dir, dpi, offset, g_home_dir, in);
else
snprintf(cmd, N, format_unix, "", dpi, offset, g_home_dir, in);
}
}
if (opt == CONVERT_LATEX_TO_EPS) {
if (g_home_dir == NULL) {
const char format_unix[] = "%slatex2png -e '%s'";
if (g_script_dir)
snprintf(cmd, N, format_unix, g_script_dir, in);
else
snprintf(cmd, N, format_unix, "", in);
} else {
const char format_unix[] = "%slatex2png -e -H '%s' '%s'";
if (g_script_dir)
snprintf(cmd, N, format_unix, g_script_dir, g_home_dir, in);
else
snprintf(cmd, N, format_unix, "", g_home_dir, in);
}
}
if (opt == CONVERT_PDF) {
const char format_unix[] = "gs -q -dNOPAUSE -dSAFER -dBATCH -sDEVICE=pngalpha -r%d -sOutputFile='%s' '%s'";
snprintf(cmd, N, format_unix, dpi, out_tmp, in);
}
if (opt == CONVERT_PS_TO_EPS) {
const char format_unix[] = "eps2eps '%s' '%s'";
snprintf(cmd, N, format_unix, in, out_tmp);
}
#else
if (opt == CONVERT_SIMPLE) {
char format_simple[] = "convert \"%s\" \"%s\"";
snprintf(cmd, N, format_simple, in, out_tmp);
}
if (opt == CONVERT_CROP) {
char format_crop[] = "convert -crop 0x0 -units PixelsPerInch -density %d \"%s\" \"%s\"";
snprintf(cmd, N, format_crop, dpi, in, out_tmp);
}
if (opt == CONVERT_LATEX_TO_PNG) {
if (g_home_dir == NULL){
char format_xp[] = "bash latex2png -d %d -o %d \"%s\"";
snprintf(cmd, N, format_xp, dpi, offset, in);
} else {
char format_xp[] = "bash latex2png -d %d -o %d -H \"%s\" \"%s\"";
snprintf(cmd, N, format_xp, dpi, offset, g_home_dir, in);
}
}
if (opt == CONVERT_LATEX_TO_EPS) {
if (g_home_dir == NULL){
char format_xp[] = "bash latex2png -e \"%s\"";
snprintf(cmd, N, format_xp, in);
} else {
char format_xp[] = "bash latex2png -e -H \"%s\" \"%s\"";
snprintf(cmd, N, format_xp, g_home_dir, in);
}
}
if (opt == CONVERT_PDF) {
char format_xp[] = "bash pdf2pnga \"%s\" \"%s\" %d";
snprintf(cmd, N, format_xp, in, out_tmp, dpi);
}
if (opt == CONVERT_PS_TO_EPS) {
char format_xp[] = "eps2eps \"%s\" \"%s\"";
snprintf(cmd, N, format_xp, in, out_tmp);
}
#endif
diagnostics(3, "`%s`", cmd);
err = system(cmd);
if (err) {
diagnostics(WARNING, "\nerror=%d when converting %s", err, in);
safe_free(out_tmp);
return NULL;
}
return out_tmp;
}
static void PicComment(int16_t label, int16_t size, FILE * fp)
{
int16_t long_comment = 0x00A1;
int16_t short_comment = 0x00A0;
int16_t tag;
tag = (size) ? long_comment : short_comment;
if (g_little_endian) {
tag = LETONS(tag);
label = LETONS(label);
size = LETONS(size);
}
if (fwrite(&tag, 2, 1, fp) != 2)
return;
if (fwrite(&label, 2, 1, fp) != 2)
return;
if (size) {
if (fwrite(&size, 2, 1, fp) != 2)
return;
}
}
static char *strdup_new_extension(const char *s, const char *old_ext, const char *new_ext)
{
char *new_name, *p;
int s_len, o_len, n_len, siz;
if (s==NULL || old_ext == NULL || new_ext == NULL) return NULL;
s_len = (int) strlen(s);
o_len = (int) strlen(old_ext);
n_len = (int) strlen(new_ext);
/* make sure that old_ext exists */
p= (char *) s;
do {
p = strstr(p, old_ext);
} while (p && p-s > s_len-o_len) ;
if (p == NULL) return NULL;
siz = s_len - o_len + n_len + 1;
new_name = (char *) malloc(siz);
my_strlcpy(new_name, s, s_len - o_len + 1);
my_strlcat(new_name, new_ext, siz);
return new_name;
}
/******************************************************************************
purpose : return true if ext is at end of s (case insensitively)
******************************************************************************/
static int has_extension(const char *s, const char *ext)
{
char *t;
t = (char *)s + strlen(s) - strlen(ext);
if (strcasecmp(t, ext) == 0)
return TRUE;
return FALSE;
}
/******************************************************************************
purpose : if the file has a graphic extension, return it otherwise return NULL
******************************************************************************/
static char *has_graphic_extension(const char *s)
{
GraphConvertElement *thisFormat;
diagnostics(4,"testing for graphics extension '%s'",s);
for (thisFormat = GraphConvertTable; thisFormat->extension != NULL; thisFormat++) {
if (has_extension(s,thisFormat->extension))
return strdup(thisFormat->extension);
}
return NULL;
}
/******************************************************************************
purpose : split filename into directory, name, and extension
******************************************************************************/
static void split_filename(const char *f, char **dir, char **name, char **ext)
{
char *s, *t, *x;
*dir = NULL;
*name = NULL;
*ext = NULL;
if (f == NULL) return;
/* first figure out the directory */
s = strrchr(f,'/');
if (s) {
t = strdup(f);
x = strrchr(t,'/')+1;
*x = '\0';
if (*f == '/') { /* absolute names start with '/' */
*dir=t;
} else if (g_home_dir) { /* names are relative to home_dir */
*dir = strdup_together(g_home_dir, t);
safe_free(t);
} else {
*dir=t;
}
s++;
} else {
if (g_home_dir)
*dir = strdup(g_home_dir);
s = (char *) f;
}
*name = strdup(s);
*ext = has_graphic_extension(s);
if (*ext) {
t = *name + strlen(*name) - strlen(*ext);
*t = '\0';
}
diagnostics(5,"filename='%s', dir='%s', name='%s', ext='%s'", f, *dir, *name, *ext);
}
/******************************************************************************
purpose : create a pict file from an EPS file and return file name for
the pict file. Ideally this file will contain both the bitmap
and the original EPS embedded in the PICT file as comments. If a
bitmap cannot be created, then the EPS is still embedded in the PICT
file so that at least the printed version will be good.
******************************************************************************/
static char *eps_to_pict(char *s)
{
char *p, *pict, buffer[560];
long ii, pict_bitmap_size, eps_size;
int16_t handle_size;
unsigned char byte;
int16_t PostScriptBegin = 190;
int16_t PostScriptEnd = 191;
int16_t PostScriptHandle = 192;
char *pict_eps = NULL;
char *eps = NULL;
char *return_value = NULL;
FILE *fp_eps = NULL;
FILE *fp_pict_bitmap = NULL;
FILE *fp_pict_eps = NULL;
int offset = 0;
diagnostics(3, "eps_to_pict '%s'", s);
/* Create filename for bitmap */
pict = strdup_new_extension(s, ".eps", "a.pict");
if (pict == NULL) {
pict = strdup_new_extension(s, ".EPS", "a.pict");
if (pict == NULL)
goto Exit;
}
/* Create filename for eps file */
p = strdup_new_extension(s, ".eps", ".pict");
if (p == NULL) {
p = strdup_new_extension(s, ".EPS", ".pict");
if (p == NULL)
goto Exit;
}
pict_eps = strdup(p);
free(p);
eps = strdup_together(g_home_dir, s);
/* create a bitmap version of the eps file */
return_value = SysGraphicsConvert(CONVERT_CROP, offset, g_dots_per_inch, eps, pict);
free(pict);
if (return_value == NULL) goto Exit;
/* open the eps file and make sure that it is less than 32k */
fp_eps = fopen(eps, "rb");
if (fp_eps == NULL)
goto Exit;
fseek(fp_eps, 0, SEEK_END);
eps_size = ftell(fp_eps);
if (eps_size > 32000) {
diagnostics(WARNING, "EPS file >32K ... using bitmap only");
goto Exit;
}
rewind(fp_eps);
diagnostics(WARNING, "eps size is 0x%X bytes", eps_size);
/* open bitmap pict file and get file size */
fp_pict_bitmap = fopen(pict, "rb");
if (fp_pict_bitmap == NULL)
goto Exit;
fseek(fp_pict_bitmap, 0, SEEK_END);
pict_bitmap_size = ftell(fp_pict_bitmap);
rewind(fp_pict_bitmap);
/* open new pict file */
fp_pict_eps = fopen(pict_eps, "w");
if (fp_pict_eps == NULL)
goto Exit;
/* copy header 512 buffer + 40 byte header */
if (fread(&buffer, 1, 512 + 40, fp_pict_bitmap) != 512 + 40)
goto Exit;
if (fwrite(&buffer, 1, 512 + 40, fp_pict_eps) != 512 + 40)
goto Exit;
/* insert comment that allows embedding postscript */
PicComment(PostScriptBegin, 0, fp_pict_eps);
/* copy bitmap 512+40 bytes of header + 2 bytes at end */
for (ii = 512 + 40 + 2; ii < pict_bitmap_size; ii++) {
if (fread(&byte, 1, 1, fp_pict_bitmap) != 1)
goto Exit;
if (fwrite(&byte, 1, 1, fp_pict_eps) != 1)
goto Exit;
}
/* copy eps graphic (write an even number of bytes) */
handle_size = eps_size;
if (odd(eps_size))
handle_size++;
PicComment(PostScriptHandle, handle_size, fp_pict_eps);
for (ii = 0; ii < eps_size; ii++) {
if (fread(&byte, 1, 1, fp_eps) != 1)
goto Exit;
if (fwrite(&byte, 1, 1, fp_pict_eps) != 1)
goto Exit;
}
if (odd(eps_size)) {
byte = ' ';
if (fwrite(&byte, 1, 1, fp_pict_eps) != 1)
goto Exit;
}
/* close file */
PicComment(PostScriptEnd, 0, fp_pict_eps);
byte = 0x00;
if (fwrite(&byte, 1, 1, fp_pict_eps) != 1)
goto Exit;
byte = 0xFF;
if (fwrite(&byte, 1, 1, fp_pict_eps) != 1)
goto Exit;
return_value = pict_eps;
Exit:
if (eps)
free(eps);
if (pict_eps)
free(pict_eps);
if (pict)
free(pict);
if (fp_eps)
fclose(fp_eps);
if (fp_pict_eps)
fclose(fp_pict_eps);
if (fp_pict_bitmap)
fclose(fp_pict_bitmap);
return return_value;
}
/******************************************************************************
purpose : create a png file from an EPS or PS file and return file name
******************************************************************************/
static char *eps_to_png(char *name)
{
char *png, *out;
if (name == NULL) return NULL;
diagnostics(3, " eps_to_png '%s'", name);
if (strstr(name, ".eps") != NULL)
png = strdup_new_extension(name, ".eps", ".png");
else if (strstr(name, ".EPS") != NULL)
png = strdup_new_extension(name, ".EPS", ".png");
else if (strstr(name, ".ps") != NULL)
png = strdup_new_extension(name, ".ps", ".png");
else if (strstr(name, ".PS") != NULL)
png = strdup_new_extension(name, ".PS", ".png");
else
return NULL;
out = SysGraphicsConvert(CONVERT_CROP, 0, g_dots_per_inch, name, png);
free(png);
return out;
}
/******************************************************************************
purpose : create a png file from a PDF file and return file name
******************************************************************************/
static char *pdf_to_png(char *pdf)
{
char *png, *out;
if (pdf == NULL) return NULL;
if (strstr(pdf, ".pdf") != NULL)
png = strdup_new_extension(pdf, ".pdf", ".png");
else if (strstr(pdf, ".PDF") != NULL)
png = strdup_new_extension(pdf, ".PDF", ".png");
else
return NULL;
out = SysGraphicsConvert(CONVERT_PDF, 0, g_dots_per_inch, pdf, png);
safe_free(png);
return out;
}
/******************************************************************************
purpose : create a wmf file from an EPS file and return file name
******************************************************************************/
static char *eps_to_emf(char *name)
{
FILE *fp;
char *cmd, *emf, *outfile;
size_t cmd_len;
char ans[50];
int32_t width, height;
outfile = NULL;
diagnostics(3, "filename = '%s'", name);
if (strstr(name, ".eps") != NULL)
outfile = strdup_new_extension(name, ".eps", ".wmf");
else if (strstr(name, ".EPS") != NULL)
outfile = strdup_new_extension(name, ".EPS", ".wmf");
if (outfile == NULL) return NULL;
emf = strdup_tmp_path(outfile);
/* Determine bounding box for EPS file */
cmd_len = strlen(name) + strlen("identify -format \"%w %h\" ") + 3;
cmd = (char *) malloc(cmd_len);
snprintf(cmd, cmd_len, "identify -format \"%%w %%h\" \"%s\"", name);
fp = popen(cmd, "r");
if (fgets(ans, 50, fp) != NULL)
sscanf(ans, "%ld %ld", (long *)&width, (long *)&height);
pclose(fp);
free(cmd);
fp = fopen(emf, "wb");
/* write ENHANCEDMETAHEADER */
/* write GDICOMMENTMULTIFORMATS */
/* write EMRFORMAT containing EPS */
free(outfile);
fclose(fp);
return emf;
}
/******************************************************************************
purpose : figure out appropriate x and y scaling
h and w are the actual height and width of the image in twips
target_h and target_w are the desired height and width of the image in twips
s is the scaling desired as a fraction
******************************************************************************/
static void AdjustScaling(double h, double w, double target_h, double target_w, double s, uint16_t *sx, uint16_t *sy)
{
diagnostics(5,"AdjustScaling h =%f w =%f s=%f", h, w, s);
diagnostics(5,"AdjustScaling target_h=%f target_w=%f", target_h, target_w);
if (target_h != 0 && h != 0)
*sy = (uint16_t) my_rint(100.0 * target_h / h);
else
*sy = (uint16_t) my_rint(s * 100);
if (target_w == 0 || w == 0)
*sx = *sy;
else
*sx = (uint16_t) my_rint(100.0 * target_w / w);
/* catch the case when width is specified, but not height */
if (target_h == 0 && target_w != 0)
*sy = *sx;
/* special case if both are zero then just set scaling to one */
if (target_h == 0 && target_w == 0) {
*sx = 100. * s;
*sy = 100. * s;
}
diagnostics(5,"AdjustScaling xscale=%d yscale=%d", *sx, *sy);
}
/******************************************************************************
purpose : write entire file to RTF as hex
******************************************************************************/
static void PutHexFile(FILE * fp)
{
int i, c;
i = 0;
while ((c = fgetc(fp)) != EOF) {
fprintRTF("%.2x", c);
if (++i > 126) { /* keep lines 254 chars long */
i = 0;
fprintRTF("\n");
}
}
}
/******************************************************************************
purpose : Include .pict file in RTF
******************************************************************************/
static void PutPictFile(char *s, double height0, double width0, double scale, double baseline)
{
FILE *fp;
char *pict;
int16_t buffer[5];
int16_t top, left, bottom, right;
int16_t width, height;
uint16_t sx,sy;
pict = strdup(s);
diagnostics(2, "PutPictFile '%s'", pict);
fp = fopen(pict, "rb");
free(pict);
if (fp == NULL)
return;
if (fseek(fp, 514L, SEEK_SET) || fread(buffer, 2, 4, fp) != 4) {
diagnostics(WARNING, "Cannot read graphics file '%s'", s);
fclose(fp);
return;
}
top = buffer[0];
left = buffer[1];
bottom = buffer[2];
right = buffer[3];
width = right - left;
height = bottom - top;
if (g_little_endian) {
top = LETONS(top);
bottom = LETONS(bottom);
left = LETONS(left);
right = LETONS(right);
}
diagnostics(4, "top = %d, bottom = %d", top, bottom);
diagnostics(4, "left = %d, right = %d", left, right);
diagnostics(4, "width = %d, height = %d", width, height);
fprintRTF("\n{\\pict\\macpict\\picw%d\\pich%d\n", width, height);
AdjustScaling(height*20,width*20,height0,width0,scale,&sx,&sy);
if (sx != 100 && sy != 100)
fprintRTF("\\picscalex%d\\picscaley%d", sx,sy);
fseek(fp, -10L, SEEK_CUR);
PutHexFile(fp);
fprintRTF("}\n");
fclose(fp);
}
/*
typedef struct _PngChunk
{
uint32_t DataLength; Size of Data field in bytes
uint32_t Type; Code identifying the type of chunk
BYTE Data[]; The actual data stored by the chunk
uint32_t Crc; CRC-32 value of the Type and Data fields
} PNGCHUNK;
typedef struct _pHYsChunkEntry
{
uint32_t PixelsPerUnitX; Pixels per unit, X axis
uint32_t PixelsPerUnitY; Pixels per unit, Y axis
BYTE UnitSpecifier; 0 = unknown, 1 = meter
} PHYSCHUNKENTRY;
*/
static unsigned char * getPngChunk(FILE *fp, char *s)
{
uint32_t crc, DataLength;
char Type[5];
unsigned char *data;
Type[4]='\0';
diagnostics(6, "getPngChunk ... seeking '%s'",s);
data = NULL;
do {
if (data) free(data);
/* read chuck size */
fread(&DataLength, 4, 1, fp);
if (g_little_endian) DataLength = LETONL(DataLength);
/* read chunk type */
fread(Type, 1, 4, fp);
if (strcmp(Type,"IEND") == 0) return NULL;
diagnostics(6,"found chunk '%s' size %u bytes",Type, DataLength);
data = (unsigned char *) malloc(DataLength);
if (data == NULL) return NULL;
fread(data, DataLength, 1, fp);
fread(&crc, 4, 1, fp);
crc++; /* ignored, but touch to eliminate warning */
} while (strcmp(s,Type) != 0);
return data;
}
static void InsertFigureAsComment(const char *s, int hinline)
{
if (!s) return;
if (*s=='\0') return;
if (hinline == 1) fprintRTF("{\\dn10 ");
putRtfStrEscaped("[###");
putRtfStrEscaped(s);
putRtfStrEscaped("###]");
if (hinline == 1) fprintRTF("}");
}
/******************************************************************************
purpose : determine height and width of file
w & h are the size in pixels
xres is the number of pixels per meter
******************************************************************************/
static void GetPngSize(char *s, uint32_t *w_pixels, uint32_t *h_pixels, double *xres, double *yres, int *bad_res)
{
FILE *fp;
uint32_t *p;
unsigned char buffer[16];
char reftag[9] = "\211PNG\r\n\032\n";
unsigned char *data = NULL;
diagnostics(4, "GetPngSize of '%s'", s);
*xres = POINTS_PER_METER;
*yres = POINTS_PER_METER;
*bad_res = 1;