1 /************************************************************************/
2 /*									*/
3 /*  Print images to SVG.						*/
4 /*									*/
5 /************************************************************************/
6 
7 #   include	"docHtmlConfig.h"
8 
9 #   include	<stddef.h>
10 #   include	<stdio.h>
11 
12 #   include	<sioMemory.h>
13 #   include	<sioHex.h>
14 
15 #   include	<docDraw.h>
16 #   include	"docSvgDrawImpl.h"
17 #   include	<drawMetafileSvg.h>
18 #   include	<drawImageSvg.h>
19 #   include	<docObjectRect.h>
20 #   include	<docObjectProperties.h>
21 #   include	<docMetafileObject.h>
22 #   include	<docLayoutObject.h>
23 #   include	<docShape.h>
24 
25 #   include	<appDebugon.h>
26 
27 /************************************************************************/
28 /*									*/
29 /*  Emit a metafile.							*/
30 /*									*/
31 /************************************************************************/
32 
docSvgDrawMetafile(SvgWriter * sw,const PictureProperties * pip,const MemoryBuffer * mb,int objectKind,const LayoutContext * lc,const DocumentRectangle * drDest)33 static int docSvgDrawMetafile(	SvgWriter *			sw,
34 				const PictureProperties *	pip,
35 				const MemoryBuffer *		mb,
36 				int				objectKind,
37 				const LayoutContext *		lc,
38 				const DocumentRectangle *	drDest )
39     {
40     XmlWriter *			xw= &(sw->swXmlWriter);
41     int				rval= 0;
42 
43     SimpleInputStream *		sisMem= (SimpleInputStream *)0;
44     SimpleInputStream *		sisMeta= (SimpleInputStream *)0;
45 
46     MetafilePlayer		mp;
47     MetafileWriteSvg		playMetafile;
48 
49     DocumentRectangle		drSrc;
50 
51     switch( objectKind )
52 	{
53 	case DOCokPICTWMETAFILE:
54 	    playMetafile= appMetaPlayWmfSvg;
55 	    break;
56 
57 	case DOCokPICTEMFBLIP:
58 	    playMetafile= appMetaPlayEmfSvg;
59 	    break;
60 
61 	case DOCokMACPICT:
62 	    playMetafile= appMacPictPlayFileSvg;
63 	    break;
64 
65 	case DOCokPICTJPEGBLIP:
66 	case DOCokPICTPNGBLIP:
67 	default:
68 	    LDEB(pip->pipType); goto ready;
69 	}
70 
71     sisMem= sioInMemoryOpen( mb );
72     if  ( ! sisMem )
73 	{ XDEB(sisMem); rval= -1; goto ready;	}
74 
75     sisMeta= sioInHexOpen( sisMem );
76     if  ( ! sisMeta )
77 	{ XDEB(sisMeta); rval= -1; goto ready;	}
78 
79     docObjectGetSourceRect( &drSrc, pip );
80 
81     docSetMetafilePlayer( &mp, sisMeta, lc, pip, 0, 0 );
82 
83     sioOutPutString( "<svg ", xw->xwSos );
84     svgWriteRectangleAttributes( sw, drDest );
85     svgWriteViewBoxAttribute( sw, "viewBox", &drSrc );
86     sioOutPutString( ">", xw->xwSos );
87     xmlNewLine( xw );
88 
89     if  ( (*playMetafile)( sw, &mp ) )
90 	{ LDEB(1);	}
91 
92     sioOutPutString( "</svg>", xw->xwSos );
93     xmlNewLine( xw );
94 
95   ready:
96     if  ( sisMeta )
97 	{ sioInClose( sisMeta );	}
98     if  ( sisMem )
99 	{ sioInClose( sisMem );	}
100 
101     return rval;
102     }
103 
104 /************************************************************************/
105 /*									*/
106 /*  Emit a bitmap image included in the document.			*/
107 /*									*/
108 /************************************************************************/
109 
docSvgDrawRasterImage(SvgWriter * sw,DrawingContext * dc,InsertedObject * io,int objectKind,const MemoryBuffer * mb,const DocumentRectangle * drDest)110 static int docSvgDrawRasterImage( SvgWriter *			sw,
111 				DrawingContext *		dc,
112 				InsertedObject *		io,
113 				int				objectKind,
114 				const MemoryBuffer *		mb,
115 				const DocumentRectangle *	drDest )
116     {
117     const PictureProperties *	pip= &(io->ioPictureProperties);
118     int				cropped= docPictureIsCropped( pip );
119 
120     if  ( ! cropped && objectKind == DOCokPICTJPEGBLIP )
121 	{
122 	if  ( ! drawRasterImageSvgFromData( sw, "image/jpeg", mb, drDest ) )
123 	    { return 1;	}
124 	}
125 
126     if  ( ! cropped && objectKind == DOCokPICTPNGBLIP )
127 	{
128 	if  ( ! drawRasterImageSvgFromData( sw, "image/png", mb, drDest ) )
129 	    { return 1;	}
130 	}
131 
132     if  ( ! io->ioRasterImage.riBytes )
133 	{
134 	if  ( docGetBitmapForObject( io ) )
135 	    { XDEB(io->ioRasterImage.riBytes);	}
136 	}
137 
138     if  ( io->ioRasterImage.riBytes )
139 	{
140 	DocumentRectangle	drSel;
141 
142 	docObjectGetCropRect( &drSel, pip, &(io->ioRasterImage.riDescription) );
143 
144 	if  ( ! drawRasterImageSvg( sw, &(io->ioRasterImage), &drSel, drDest ) )
145 	    { return 1;	}
146 	}
147 
148     return 0;
149     }
150 
151 /************************************************************************/
152 /*									*/
153 /*  Emit a bitmap image included in the document.			*/
154 /*									*/
155 /************************************************************************/
156 
docSvgDrawShapeRaster(SvgWriter * sw,DrawingContext * dc,DrawingShape * ds,const DocumentRectangle * drDest)157 static int docSvgDrawShapeRaster( SvgWriter *			sw,
158 				DrawingContext *		dc,
159 				DrawingShape *			ds,
160 				const DocumentRectangle *	drDest )
161     {
162     const PictureProperties *	pip= &(ds->dsPictureProperties);
163     int				cropped= docPictureIsCropped( pip );
164     const MemoryBuffer *	mb= &(ds->dsPictureData);
165 
166     if  ( ! cropped && pip->pipType == DOCokPICTJPEGBLIP )
167 	{
168 	if  ( ! drawRasterImageSvgFromData( sw, "image/jpeg", mb, drDest ) )
169 	    { return 1;	}
170 	}
171 
172     if  ( ! cropped && pip->pipType == DOCokPICTPNGBLIP )
173 	{
174 	if  ( ! drawRasterImageSvgFromData( sw, "image/png", mb, drDest ) )
175 	    { return 1;	}
176 	}
177 
178     if  ( ! ds->dsRasterImage.riBytes )
179 	{
180 	if  ( docGetBitmapForObjectData( pip->pipType,
181 				&(ds->dsRasterImage), &(ds->dsPictureData) ) )
182 	    { XDEB(ds->dsRasterImage.riBytes);	}
183 	}
184 
185     if  ( ds->dsRasterImage.riBytes )
186 	{
187 	DocumentRectangle	drSel;
188 
189 	docObjectGetCropRect( &drSel, pip,
190 				    &(ds->dsRasterImage.riDescription) );
191 
192 	if  ( ! drawRasterImageSvg( sw, &(ds->dsRasterImage), &drSel, drDest ) )
193 	    { return 1;	}
194 	}
195 
196     return 0;
197     }
198 
docSvgDrawShapeImage(SvgWriter * sw,DrawingContext * dc,DrawingShape * ds,const DocumentRectangle * drTwips,const AffineTransform2D * at)199 int docSvgDrawShapeImage(	SvgWriter *			sw,
200 				DrawingContext *		dc,
201 				DrawingShape *			ds,
202 				const DocumentRectangle *	drTwips,
203 				const AffineTransform2D *	at )
204     {
205     const PictureProperties *	pip= &(ds->dsPictureProperties);
206     const LayoutContext *	lc= &(dc->dcLayoutContext);
207 
208     switch( pip->pipType )
209 	{
210 	case DOCokPICTWMETAFILE:
211 	case DOCokMACPICT:
212 	case DOCokPICTEMFBLIP:
213 	    if  ( docSvgDrawMetafile( sw, pip, &(ds->dsPictureData),
214 						pip->pipType, lc, drTwips ) )
215 		{ LDEB(1); break;	}
216 
217 	    dc->dcCurrentTextAttributeSet= 0;
218 	    dc->dcCurrentColorSet= 0;
219 	    return 0;
220 
221 	case DOCokPICTPNGBLIP:
222 	case DOCokPICTJPEGBLIP:
223 	    if  ( docSvgDrawShapeRaster( sw, dc, ds, drTwips ) < 0 )
224 		{ LDEB(1); return -1;	}
225 	    break;
226 
227 	default:
228 	    LDEB(pip->pipType); return 0;
229 	}
230 
231     return 0;
232     }
233 
234 /************************************************************************/
235 /*									*/
236 /*  Save images and embedded objects to SVG.				*/
237 /*									*/
238 /************************************************************************/
239 
docSvgDrawObject(const DrawTextLine * dtl,int part,InsertedObject * io,const int x0Twips,const int x1Twips,const LayoutPosition * baseLine)240 int docSvgDrawObject(		const DrawTextLine *		dtl,
241 				int				part,
242 				InsertedObject *		io,
243 				const int			x0Twips,
244 				const int			x1Twips,
245 				const LayoutPosition *		baseLine )
246     {
247     SvgWriter *			sw= (SvgWriter *)dtl->dtlThrough;
248 
249     DrawingContext *		dc= dtl->dtlDrawingContext;
250     const LayoutContext *	lc= &(dc->dcLayoutContext);
251 
252     DocumentRectangle		drDest;
253 
254     docObjectGetPageRect( &drDest, io, x0Twips, baseLine->lpPageYTwips );
255 
256     switch( io->ioKind )
257 	{
258 	int		done;
259 
260 	case DOCokPICTWMETAFILE:
261 	case DOCokPICTEMFBLIP:
262 	case DOCokMACPICT:
263 
264 	    if  ( docSvgDrawMetafile( sw, &(io->ioPictureProperties),
265 						    &(io->ioObjectData),
266 						    io->ioKind, lc, &drDest ) )
267 		{ LDEB(1); break;	}
268 
269 	    dc->dcCurrentTextAttributeSet= 0;
270 	    dc->dcCurrentColorSet= 0;
271 	    return 1;
272 
273 	case DOCokPICTJPEGBLIP:
274 	case DOCokPICTPNGBLIP:
275 
276 	    done= docSvgDrawRasterImage( sw, dc, io,
277 				    io->ioKind, &(io->ioObjectData), &drDest );
278 	    return done;
279 
280 	case DOCokOLEOBJECT:
281 	    if  ( io->ioResultKind == DOCokPICTWMETAFILE	||
282 		  io->ioResultKind == DOCokPICTEMFBLIP		||
283 		  io->ioResultKind == DOCokMACPICT		)
284 		{
285 		if  ( docSvgDrawMetafile( sw, &(io->ioPictureProperties),
286 					    &(io->ioResultData),
287 					    io->ioResultKind, lc, &drDest ) )
288 		    { LDEB(1); break;	}
289 
290 		dc->dcCurrentTextAttributeSet= 0;
291 		dc->dcCurrentColorSet= 0;
292 		return 1;
293 		}
294 
295 	    if  ( io->ioResultKind == DOCokPICTJPEGBLIP	||
296 	    	  io->ioResultKind == DOCokPICTPNGBLIP	)
297 		{
298 		done= docSvgDrawRasterImage( sw, dc, io,
299 			    io->ioResultKind, &(io->ioResultData), &drDest );
300 		return done;
301 		}
302 
303 	    break;
304 
305 	case DOCokDRAWING_SHAPE:
306 	    /*  Done in a separate loop from generic drawing code */
307 	    return 1;
308 
309 	case DOCokEPS_FILE:
310 	default:
311 	    LDEB(io->ioKind); return 0;
312 	}
313 
314     LDEB(1); return 1;
315     }
316 
317 
318