File: docIncludePictureField.c

package info (click to toggle)
ted 2.16-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,944 kB
  • ctags: 20,273
  • sloc: ansic: 167,980; makefile: 12,518; sh: 263
file content (285 lines) | stat: -rw-r--r-- 6,952 bytes parent folder | download
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
/************************************************************************/
/*									*/
/*  Evaluate an 'IncludePicture' field.					*/
/*									*/
/************************************************************************/

#   include	"tedConfig.h"

#   include	<stdlib.h>
#   include	<string.h>
#   include	<stdio.h>

#   include	<sioStdio.h>
#   include	<sioMemory.h>
#   include	<sioHex.h>

#   include	<appImage.h>

#   include	<appUnit.h>
#   include	<appSystem.h>
#   include	"docBuf.h"
#   include	"docEvalField.h"

#   include	<appDebugon.h>

/************************************************************************/
/*									*/
/*  Save an included file to a memory buffer and determine its type and	*/
/*  bounding box on the fly.						*/
/*									*/
/*  (Only EPS is supported now)						*/
/*									*/
/************************************************************************/

static int docCollectIncludedEpsFile(	const char *		fullName,
					InsertedObject *	io )
    {
    int				gotBBox= 0;
    int				gotEpsHeader= 0;

    char			line[512+ 2];

    SimpleInputStream *		sisIn= (SimpleInputStream *)0;
    SimpleOutputStream *	sosBuffer= (SimpleOutputStream *)0;
    SimpleOutputStream *	sosHex= (SimpleOutputStream *)0;

    int				n;

    int				llx= 0, lly= 0, urx= 0, ury= 0;
    int				i1, i2, i3, i4;

    sisIn= sioInStdioOpen( fullName );
    if  ( ! sisIn )
	{ SXDEB(fullName,sisIn); return -1;	}

    if  ( ! sioInGetString( line, 512+ 1, sisIn ) )
	{ LDEB(512); sioInClose( sisIn ); return -1; }
    line[512+ 1]= '\0';

    n= sscanf( line, "%%!PS-Adobe-%d.%d EPSF-%d.%d", &i1, &i2, &i3, &i4 );
    if  ( n == 4 )
	{ gotEpsHeader= 1;	}
    else{ SDEB(line);		}

    sosBuffer= sioOutMemoryOpen( &(io->ioResultData) );
    if  ( ! sosBuffer )
	{ XDEB(sosBuffer); sioInClose( sisIn ); return -1;	}
    sosHex= sioOutHexOpen( sosBuffer );
    if  ( ! sosHex )
	{ XDEB(sosHex); sioInClose( sisIn ); return -1;		}

    sioOutPutString( line, sosHex );

    while( ! strchr( line, '\n' ) )
	{
	if  ( ! sioInGetString( line, 512+ 1, sisIn ) )
	    { break;	}
	line[512+ 1]= '\0';

	sioOutPutString( line, sosHex );
	}

    while( sioInGetString( line, 512+ 1, sisIn ) )
	{
	line[512+ 1]= '\0';
	sioOutPutString( line, sosHex );

	if  ( ! gotBBox )
	    {
	    n= sscanf( line, "%%%%BoundingBox: %d %d %d %d",
							&i1, &i2, &i3, &i4 );
	    if  ( n == 4 )
		{ llx= i1; lly= i2; urx= i3; ury= i4; gotBBox= 1; }
	    }

	while( ! strchr( line, '\n' ) )
	    {
	    if  ( ! sioInGetString( line, 512+1, sisIn ) )
		{ break;	}
	    line[512+ 1]= '\0';

	    sioOutPutString( line, sosHex );
	    }
	}

    sioOutClose( sosHex );
    sioOutClose( sosBuffer );
    sioInClose( sisIn );

    if  ( gotEpsHeader && gotBBox )
	{
	io->ioResultKind= DOCokEPS_FILE;

	if  ( llx < 0 )
	    { io->ioTwipsWide= 20* ( urx- llx );	}
	else{ io->ioTwipsWide= 20* ( urx );		}

	if  ( lly < 0 )
	    { io->ioTwipsHigh= 20* ( ury- lly );	}
	else{ io->ioTwipsHigh= 20* ( ury );		}
	}
    else{ LLDEB(gotEpsHeader,gotBBox); return -1;	}

    return 0;
    }


/************************************************************************/
/*									*/
/*  Read a bitmap/pixmap image file.					*/
/*									*/
/************************************************************************/

static int docReadIncludedBitmapFile(	const char *		fullName,
					InsertedObject *	io )
    {
    int			res;
    AppBitmapImage *	abi;

    int			fileFormat;
    double		compressionFactor;


    abi= (AppBitmapImage *)malloc( sizeof(AppBitmapImage) );
    if  ( ! abi )
	{ XDEB(abi); return -1;	}
    appInitBitmapImage( abi );

    res= bmRead( fullName, &(abi->abiBuffer), &(abi->abiBitmap),
					    &fileFormat, &compressionFactor );
    if  ( res )
	{ SLDEB(fullName,res); return -1;	}

    io->ioPrivate= (void *)abi;
    io->ioResultKind= DOCokBITMAP_FILE;

    bmImageSizeTwips( &(io->ioTwipsWide), &(io->ioTwipsHigh),
							&(abi->abiBitmap) );

    return 0;
    }

/************************************************************************/
/*									*/
/*  Evaluate an 'INCLUDEPICTURE' field.					*/
/*									*/
/************************************************************************/

int docRecalculateIncludePictureField(
				int *				pCalculated,
				BufferDocument *		bd,
				int *				pPartShift,
				int *				pStroffShift,
				BufferItem *			paraBi,
				int				part,
				int				partCount,
				DocumentField *			df,
				void *				voidadd,
				DOC_CLOSE_OBJECT		closeObject )
    {
    const DocumentProperties *		dp= &(bd->bdProperties);
    int					i;

    TextParticule *			tp;

    InsertedObject *			io;
    int					objectNumber;

    int					oldPartCount= paraBi->biParaParticuleCount;
    int					oldStrlen= paraBi->biParaStrlen;
    int					stroff;

    int					stroffShift= 0;

    const char *			fileName;
    int					fileSize;

    char				fullName[1000+1];
    int					textAttributeNumber;
    const char *			extension;

    tp= paraBi->biParaParticules+ part+ 1;

    if  ( partCount == 1		&&
	  tp->tpKind == DOCkindOBJECT	)
	{
	io= paraBi->biParaObjects+ tp->tpObjectNumber;

	if  ( io->ioKind == DOCokINCLUDEPICTURE )
	    { *pCalculated= 0; return 0; }
	}

    if  ( docFieldGetIncludePicture( df, &fileName, &fileSize ) )
	{ LDEB(1); return -1;	}

    if  ( fileSize < 1 )
	{ LDEB(fileSize); *pCalculated= 0; return 0; }

    {
    char *	scratch= malloc( fileSize+ 1 );

    if  ( ! scratch )
	{ XDEB(scratch); i= -1;	}
    else{
	const int	relativeIsFile= 1;

	strncpy( scratch, fileName, fileSize )[fileSize]= '\0';

	i= appAbsoluteName( fullName, 1000, scratch,
				relativeIsFile, (const char *)dp->dpFilename );
	free( scratch );
	}

    if  ( i < 0 )
	{ LDEB(i); *pCalculated= 0; return 0; }
    }

    io= docClaimObject( &objectNumber, paraBi );
    if  ( ! io )
	{ XDEB(io); return -1;	}
    
    if  ( docObjectSetData( io, (unsigned char *)fullName, i+ 1 ) )
	{ return -1;	}
    io->ioKind= DOCokINCLUDEPICTURE;


    if  ( docFieldReplaceContents( &stroff,
			    &stroffShift, &textAttributeNumber,
			    bd, paraBi, part, partCount,
			    *pStroffShift, (unsigned char *)" ", 1,
			    voidadd, closeObject ) )
	{ LDEB(1); return -1;	}

    tp= docInsertTextParticule( paraBi, part+ 1, stroff, 1,
				DOCkindOBJECT, textAttributeNumber );
    if  ( ! tp )
	{ XDEB(tp); return -1;	}

    tp->tpObjectNumber= paraBi->biParaObjectCount++;

    extension= appFileExtensionOfName( fullName );

    if  ( extension					&&
	  ( ! strcmp( extension, "ps" )		||
	    ! strcmp( extension, "eps" )	||
	    ! strcmp( extension, "PS" )		||
	    ! strcmp( extension, "EPS" )	)	)
	{
	if  ( docCollectIncludedEpsFile( fullName, io ) )
	    { SDEB(fullName);	}

	goto ready;
	}

    if  ( docReadIncludedBitmapFile( fullName, io ) )
	{ SDEB(fullName);	}

  ready:

    *pCalculated= 1;
    *pPartShift= paraBi->biParaParticuleCount- oldPartCount;
    *pStroffShift= paraBi->biParaStrlen- oldStrlen;
    return 0;
    }