Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef _SWF_WRITER_HXX_
21 : #define _SWF_WRITER_HXX_
22 :
23 : #include <com/sun/star/uno/Sequence.hxx>
24 : #include <com/sun/star/io/XOutputStream.hpp>
25 : #include <com/sun/star/i18n/XBreakIterator.hpp>
26 : #include <vcl/font.hxx>
27 : #include <vcl/gradient.hxx>
28 : #include <unotools/tempfile.hxx>
29 : #include <tools/color.hxx>
30 : #include <tools/poly.hxx>
31 : #include <tools/gen.hxx>
32 : #include <tools/stream.hxx>
33 :
34 : // #i73264#
35 : #include <basegfx/matrix/b2dhommatrix.hxx>
36 : #include <osl/file.hxx>
37 :
38 : #include <vector>
39 : #include <stack>
40 : #include <map>
41 :
42 : #ifdef AUGUSTUS
43 : #include "lame.h"
44 : #include "sndfile.h"
45 : #endif
46 :
47 : #include <stdio.h>
48 :
49 : class GDIMetaFile;
50 : class BitmapEx;
51 : class PolyPolygon;
52 : class Gradient;
53 : class SvtGraphicFill;
54 : class SvtGraphicStroke;
55 : class LineInfo;
56 : namespace basegfx { class B2DPolygon; }
57 :
58 0 : inline sal_uInt16 _uInt16( sal_Int32 nValue )
59 : {
60 : OSL_ENSURE( (nValue >= 0) && ((sal_uInt32)nValue <= 0xffff), "overflow while converting sal_Int32 to sal_uInt16" );
61 0 : return (sal_uInt16)nValue;
62 : }
63 :
64 0 : inline sal_Int16 _Int16( sal_Int32 nValue )
65 : {
66 : OSL_ENSURE( (nValue >= -32768) && (nValue <= 32767), "overflow while converting sal_Int32 to sal_Int16" );
67 0 : return (sal_Int16)nValue;
68 : }
69 :
70 : class VirtualDevice;
71 :
72 : namespace swf {
73 :
74 : const sal_uInt8 TAG_END = 0;
75 : const sal_uInt8 TAG_SHOWFRAME = 1;
76 :
77 : const sal_uInt8 TAG_DEFINEBUTTON = 7;
78 :
79 : const sal_uInt8 TAG_BACKGROUNDCOLOR = 9;
80 :
81 : const sal_uInt8 TAG_DOACTION = 12;
82 : const sal_uInt8 TAG_STARTSOUND = 15;
83 :
84 : const sal_uInt8 TAG_SOUNDSTREAMBLOCK = 19;
85 : const sal_uInt8 TAG_SOUNDSTREAMHEAD = 18;
86 : const sal_uInt8 TAG_SOUNDSTREAMHEAD2 = 45;
87 :
88 : const sal_uInt8 TAG_JPEGTABLES = 8;
89 : const sal_uInt8 TAG_DEFINEBITS = 6;
90 : const sal_uInt8 TAG_DEFINEBITSLOSSLESS = 20;
91 : const sal_uInt8 TAG_DEFINEBITSJPEG2 = 21;
92 : const sal_uInt8 TAG_DEFINEBITSJPEG3 = 35;
93 : const sal_uInt8 TAG_DEFINEBITSLOSSLESS2 = 36;
94 : const sal_uInt8 TAG_DEFINEEDITTEXT= 37;
95 : const sal_uInt8 TAG_PLACEOBJECT = 4;
96 : const sal_uInt8 TAG_PLACEOBJECT2 = 26;
97 : const sal_uInt8 TAG_REMOVEOBJECT2 = 28;
98 :
99 : const sal_uInt8 TAG_DEFINEFONT = 10;
100 : const sal_uInt8 TAG_DEFINETEXT = 11;
101 : const sal_uInt8 TAG_DEFINESHAPE3 = 32;
102 : const sal_uInt8 TAG_DEFINESPRITE = 39;
103 :
104 : const sal_uInt8 TAG_FRAMELABEL = 43;
105 :
106 : const sal_uInt8 TAG_HEADER = 0xff;
107 :
108 : ///////////////////////////////////////////////////////////////////////
109 :
110 : /** converts a double to a 16.16 flash fixed value */
111 : sal_uInt32 getFixed( double fValue );
112 :
113 : ///////////////////////////////////////////////////////////////////////
114 :
115 : typedef ::std::map<sal_uInt32, sal_uInt16> ChecksumCache;
116 :
117 : /** unsigned int 16 compare operation for stl */
118 : struct ltuint16
119 : {
120 0 : bool operator()(sal_uInt16 s1, sal_uInt16 s2) const
121 : {
122 0 : return s1 < s2;
123 : }
124 : };
125 :
126 : ///////////////////////////////////////////////////////////////////////
127 :
128 : /** container class to create bit structures */
129 0 : class BitStream
130 : {
131 : public:
132 : BitStream();
133 :
134 : void writeUB( sal_uInt32 nValue, sal_uInt16 nBits );
135 : void writeSB( sal_Int32 nValue, sal_uInt16 nBits );
136 : void writeFB( sal_uInt32 nValue, sal_uInt16 nBits );
137 :
138 : void pad();
139 : void writeTo( SvStream& out );
140 :
141 : sal_uInt32 getOffset() const;
142 : private:
143 :
144 : std::vector< sal_uInt8 > maData;
145 : sal_uInt8 mnBitPos;
146 : sal_uInt8 mnCurrentByte;
147 : };
148 :
149 : ///////////////////////////////////////////////////////////////////////
150 :
151 : /** this class collects all used glyphs for a given fonts and maps
152 : characters to glyph ids.
153 : */
154 : class FlashFont
155 : {
156 : public:
157 : FlashFont( const Font& rFont, sal_uInt16 nId );
158 : ~FlashFont();
159 :
160 : sal_uInt16 getGlyph( sal_uInt16 nChar, VirtualDevice* pVDev );
161 :
162 : void write( SvStream& out );
163 :
164 0 : sal_uInt16 getID() const { return mnId; }
165 0 : const Font& getFont() { return maFont; }
166 :
167 : private:
168 : const Font maFont;
169 : std::map<sal_uInt16, sal_uInt16, ltuint16> maGlyphIndex;
170 : sal_uInt16 mnNextIndex;
171 : sal_uInt16 mnId;
172 : BitStream maGlyphData;
173 : std::vector< sal_uInt16 > maGlyphOffsets;
174 : };
175 :
176 : typedef std::vector<FlashFont*> FontMap;
177 :
178 : ///////////////////////////////////////////////////////////////////////
179 :
180 : /** this class helps creating flash tags */
181 0 : class Tag : public SvMemoryStream
182 : {
183 : public:
184 : Tag( sal_uInt8 nTagId );
185 :
186 0 : sal_uInt8 getTagId() const { return mnTagId; }
187 :
188 : void write( SvStream& out );
189 :
190 : void addUI32( sal_uInt32 nValue );
191 : void addUI16( sal_uInt16 nValue );
192 : void addUI8( sal_uInt8 nValue );
193 : void addBits( BitStream& rIn );
194 :
195 : void addRGBA( const Color& rColor );
196 : void addRGB( const Color& rColor );
197 : void addRect( const Rectangle& rRect );
198 : void addMatrix( const ::basegfx::B2DHomMatrix& rMatrix ); // #i73264#
199 : void addString( const char* pString );
200 : void addStream( SvStream& rIn );
201 :
202 : static void writeMatrix( SvStream& rOut, const ::basegfx::B2DHomMatrix& rMatrix ); // #i73264#
203 : static void writeRect( SvStream& rOut, const Rectangle& rRect );
204 :
205 : private:
206 : sal_uInt8 mnTagId;
207 : };
208 :
209 : ///////////////////////////////////////////////////////////////////////
210 :
211 : /** this class helps to define flash sprites */
212 : class Sprite
213 : {
214 : public:
215 : Sprite( sal_uInt16 nId );
216 : ~Sprite();
217 :
218 : void write( SvStream& out );
219 :
220 : sal_uInt16 getId() const { return mnId; }
221 :
222 : void addTag( Tag* pNewTag );
223 :
224 : private:
225 : std::vector< Tag* > maTags;
226 : sal_uInt16 mnId;
227 : sal_uInt32 mnFrames;
228 : };
229 :
230 : ///////////////////////////////////////////////////////////////////////
231 :
232 : /** this class stores a flash fill style for shapes */
233 0 : class FillStyle
234 : {
235 : public:
236 : enum FillStyleType { solid = 0x00, linear_gradient = 0x10, radial_gradient = 0x12, tiled_bitmap = 0x40, clipped_bitmap = 0x41 };
237 :
238 : /** this c'tor creates a solid fill style */
239 : FillStyle( const Color& rSolidColor );
240 :
241 : /** this c'tor creates a linear or radial gradient fill style */
242 : FillStyle( const Rectangle& rBoundRect, const Gradient& rGradient );
243 :
244 : /** this c'tor creates a tiled or clipped bitmap fill style */
245 : FillStyle( sal_uInt16 nBitmapId, bool bClipped, const ::basegfx::B2DHomMatrix& rMatrix ); // #i73264#
246 :
247 : void addTo( Tag* pTag ) const;
248 :
249 : private:
250 : void Impl_addGradient( Tag* pTag ) const;
251 :
252 : FillStyleType meType;
253 : ::basegfx::B2DHomMatrix maMatrix; // #i73264#
254 : sal_uInt16 mnBitmapId;
255 : Color maColor;
256 : Gradient maGradient;
257 : Rectangle maBoundRect;
258 : };
259 :
260 : ///////////////////////////////////////////////////////////////////////
261 :
262 : /** this class creates a flash movie from vcl geometry */
263 : class Writer
264 : {
265 : friend class FlashFont;
266 :
267 : public:
268 : /** creates a writer for a new flash movie.
269 : nDocWidth and nDocHeight are the dimensions of the movie.
270 : They must be in 100th/mm.
271 :
272 : An invisible shape with the size of the document is placed at depth 1
273 : and it clips all shapes on depth 2 and 3.
274 : */
275 : Writer( sal_Int32 nDocWidthInput, sal_Int32 nDocHeightInput, sal_Int32 nDocWidth, sal_Int32 nDocHeight, sal_Int32 nJPEGcompressMode = -1 );
276 : ~Writer();
277 :
278 : void storeTo( com::sun::star::uno::Reference< com::sun::star::io::XOutputStream > &xOutStream );
279 :
280 : // geometry
281 : void setClipping( const PolyPolygon* pClipPolyPolygon );
282 :
283 : /** defines a flash shape from a filled polygon.
284 : The coordinates must be in twips */
285 : sal_uInt16 defineShape( const Polygon& rPoly, const FillStyle& rFillStyle );
286 :
287 : /** defines a flash shape from a filled polypolygon.
288 : The coordinates must be in twips */
289 : sal_uInt16 defineShape( const PolyPolygon& rPolyPoly, const FillStyle& rFillStyle );
290 :
291 : /** defines a flash shape from a outlined polypolygon.
292 : The coordinates must be in twips */
293 : sal_uInt16 defineShape( const PolyPolygon& rPolyPoly, sal_uInt16 nLineWidth, const Color& rLineColor );
294 :
295 : /** defines a flash shape from a vcl metafile.
296 : The mapmode of the metafile is used to map all coordinates to twips.
297 : A character id of a flash sprite is returned that contains all geometry
298 : from the metafile.
299 : */
300 : sal_uInt16 defineShape( const GDIMetaFile& rMtf, sal_Int16 x = 0, sal_Int16 y = 0 );
301 :
302 : /** defines a bitmap and returns its flash id.
303 : */
304 : sal_uInt16 defineBitmap( const BitmapEx& bmpSource, sal_Int32 nJPEGQualityLevel );
305 :
306 : // control tags
307 :
308 : /** inserts a place shape tag into the movie stream or the current sprite */
309 : void placeShape( sal_uInt16 nID, sal_uInt16 nDepth, sal_Int32 x, sal_Int32 y, sal_uInt16 nClipDepth = 0, const char* pName = NULL );
310 :
311 : #ifdef THEFUTURE
312 : /** inserts a move shape tag into the movie stream or the current sprite */
313 : void moveShape( sal_uInt16 nDepth, sal_Int32 x, sal_Int32 y );
314 : #endif
315 :
316 : /** inserts a remove shape tag into the movie stream or the current sprite */
317 : void removeShape( sal_uInt16 nDepth );
318 :
319 : /** inserts a show frame tag into the movie stream or the current sprite */
320 : void showFrame();
321 :
322 : /** creates a new sprite and sets it as the current sprite for editing.
323 : Only one sprite can be edited at one time */
324 : sal_uInt16 startSprite();
325 :
326 : /** ends editing of the curent sprites and adds it to the movie stream */
327 : void endSprite();
328 :
329 : /** inserts a doaction tag with an ActionStop */
330 : void stop();
331 :
332 : /** inserts a doaction tag with an ActionStop, place a button on depth nDepth that
333 : continues playback on click */
334 : void waitOnClick( sal_uInt16 nDepth );
335 :
336 : /** inserts a doaction tag with an ActionGotoFrame */
337 : void gotoFrame( sal_uInt16 nFrame );
338 :
339 : #ifdef AUGUSTUS
340 : /** stream out a sound. Should make it more intelligent so it interleaves with other items.*/
341 : sal_Bool streamSound( const char * filename );
342 : #endif
343 :
344 : private:
345 : Point map( const Point& rPoint ) const;
346 : Size map( const Size& rSize ) const;
347 : void map( PolyPolygon& rPolyPolygon ) const;
348 : sal_Int32 mapRelative( sal_Int32 n100thMM ) const;
349 :
350 : void startTag( sal_uInt8 nTagId );
351 : void endTag();
352 : sal_uInt16 createID();
353 :
354 : void Impl_writeBmp( sal_uInt16 nBitmapId, sal_uInt32 width, sal_uInt32 height, sal_uInt8 *pCompressed, sal_uInt32 compressed_size );
355 : void Impl_writeImage( const BitmapEx& rBmpEx, const Point& rPt, const Size& rSz, const Point& rSrcPt, const Size& rSrcSz, const Rectangle& rClipRect, bool bMap );
356 : void Impl_writeJPEG(sal_uInt16 nBitmapId, const sal_uInt8* pJpgData, sal_uInt32 nJpgDataLength, sal_uInt8 *pCompressed, sal_uInt32 compressed_size );
357 : void Impl_handleLineInfoPolyPolygons(const LineInfo& rInfo, const basegfx::B2DPolygon& rLinePolygon);
358 : void Impl_writeActions( const GDIMetaFile& rMtf );
359 : void Impl_writePolygon( const Polygon& rPoly, sal_Bool bFilled );
360 : void Impl_writePolygon( const Polygon& rPoly, sal_Bool bFilled, const Color& rFillColor, const Color& rLineColor );
361 : void Impl_writePolyPolygon( const PolyPolygon& rPolyPoly, sal_Bool bFilled, sal_uInt8 nTransparence = 0);
362 : void Impl_writePolyPolygon( const PolyPolygon& rPolyPoly, sal_Bool bFilled, const Color& rFillColor, const Color& rLineColor );
363 : void Impl_writeText( const Point& rPos, const String& rText, const sal_Int32* pDXArray, long nWidth );
364 : void Impl_writeText( const Point& rPos, const String& rText, const sal_Int32* pDXArray, long nWidth, Color aTextColor );
365 : void Impl_writeGradientEx( const PolyPolygon& rPolyPoly, const Gradient& rGradient );
366 : void Impl_writeLine( const Point& rPt1, const Point& rPt2, const Color* pLineColor = NULL );
367 : void Impl_writeRect( const Rectangle& rRect, long nRadX, long nRadY );
368 : void Impl_writeEllipse( const Point& rCenter, long nRadX, long nRadY );
369 : bool Impl_writeFilling( SvtGraphicFill& rFilling );
370 : bool Impl_writeStroke( SvtGraphicStroke& rStroke );
371 :
372 : FlashFont& Impl_getFont( const Font& rFont );
373 :
374 : static void Impl_addPolygon( BitStream& rBits, const Polygon& rPoly, sal_Bool bFilled );
375 :
376 : static void Impl_addShapeRecordChange( BitStream& rBits, sal_Int16 dx, sal_Int16 dy, sal_Bool bFilled );
377 : static void Impl_addStraightEdgeRecord( BitStream& rBits, sal_Int16 dx, sal_Int16 dy );
378 : static void Impl_addCurvedEdgeRecord( BitStream& rBits, sal_Int16 control_dx, sal_Int16 control_dy, sal_Int16 anchor_dx, sal_Int16 anchor_dy );
379 : static void Impl_addEndShapeRecord( BitStream& rBits );
380 :
381 : static void Impl_addStraightLine( BitStream& rBits,
382 : Point& rLastPoint,
383 : const double P2x, const double P2y );
384 : static void Impl_addQuadBezier( BitStream& rBits,
385 : Point& rLastPoint,
386 : const double P2x, const double P2y,
387 : const double P3x, const double P3y );
388 : static void Impl_quadBezierApprox( BitStream& rBits,
389 : Point& rLastPoint,
390 : const double d2,
391 : const double P1x, const double P1y,
392 : const double P2x, const double P2y,
393 : const double P3x, const double P3y,
394 : const double P4x, const double P4y );
395 :
396 : com::sun::star::uno::Reference < com::sun::star::i18n::XBreakIterator > Impl_GetBreakIterator();
397 :
398 : private:
399 : com::sun::star::uno::Reference< com::sun::star::i18n::XBreakIterator > mxBreakIterator;
400 :
401 : FontMap maFonts;
402 :
403 : sal_Int32 mnDocWidth;
404 : sal_Int32 mnDocHeight;
405 :
406 : // AS: Scaling factor for output.
407 : double mnDocXScale;
408 : double mnDocYScale;
409 :
410 : sal_uInt16 mnWhiteBackgroundShapeId;
411 : sal_uInt16 mnPageButtonId;
412 :
413 : VirtualDevice* mpVDev;
414 :
415 : const PolyPolygon* mpClipPolyPolygon;
416 :
417 : /** holds the informations of the objects defined in the movie stream
418 : while executing defineShape
419 : */
420 : typedef std::vector<sal_uInt16> CharacterIdVector;
421 : CharacterIdVector maShapeIds;
422 :
423 : Tag* mpTag;
424 : Sprite* mpSprite;
425 : std::stack<Sprite*> mvSpriteStack;
426 : ChecksumCache mBitmapCache;
427 :
428 : sal_uInt16 mnNextId;
429 : sal_uInt32 mnFrames;
430 :
431 : // com::sun::star::uno::Reference< com::sun::star::io::XOutputStream > mxOutStream;
432 : oslFileHandle mxOutStream;
433 :
434 : utl::TempFile maMovieTempFile;
435 : utl::TempFile maFontsTempFile;
436 :
437 : SvStream* mpMovieStream;
438 : SvStream* mpFontsStream;
439 :
440 : #ifdef AUGUSTUS
441 : lame_global_flags *m_lame_flags;
442 : #endif
443 :
444 : sal_uInt8 mnGlobalTransparency;
445 : sal_Int32 mnJPEGCompressMode;
446 : };
447 :
448 : ///////////////////////////////////////////////////////////////////////
449 :
450 : }
451 :
452 : #endif
453 :
454 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|