Update ooo320-m1
[ooovba.git] / filter / source / flash / swfwriter.hxx
blobca4df09a518e14519995cd30aff3e67a4e001814
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: swfwriter.hxx,v $
10 * $Revision: 1.14 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef _SWF_WRITER_HXX_
32 #define _SWF_WRITER_HXX_
34 #include <com/sun/star/uno/Sequence.hxx>
35 #include <com/sun/star/io/XOutputStream.hpp>
36 #include <com/sun/star/i18n/XBreakIterator.hpp>
37 #include <vcl/font.hxx>
38 #include <vcl/gradient.hxx>
39 #include <unotools/tempfile.hxx>
40 #include <tools/color.hxx>
41 #include <tools/poly.hxx>
42 #include <tools/gen.hxx>
43 #include <tools/stream.hxx>
45 // #i73264#
46 #include <basegfx/matrix/b2dhommatrix.hxx>
47 #include <osl/file.hxx>
49 #include <vector>
50 #include <stack>
51 #include <map>
53 #ifdef AUGUSTUS
54 #include "lame.h"
55 #include "sndfile.h"
56 #endif
58 #include <stdio.h>
60 class GDIMetaFile;
61 class BitmapEx;
62 class PolyPolygon;
63 class Gradient;
64 class SvtGraphicFill;
65 class SvtGraphicStroke;
67 inline sal_uInt16 _uInt16( sal_Int32 nValue )
69 OSL_ENSURE( (nValue >= 0) && ((sal_uInt32)nValue <= 0xffff), "overflow while converting sal_Int32 to sal_uInt16" );
70 return (sal_uInt16)nValue;
73 inline sal_Int16 _Int16( sal_Int32 nValue )
75 OSL_ENSURE( (nValue >= -32768) && (nValue <= 32767), "overflow while converting sal_Int32 to sal_Int16" );
76 return (sal_Int16)nValue;
79 class VirtualDevice;
81 namespace swf {
83 const sal_uInt8 TAG_END = 0;
84 const sal_uInt8 TAG_SHOWFRAME = 1;
86 const sal_uInt8 TAG_DEFINEBUTTON = 7;
88 const sal_uInt8 TAG_BACKGROUNDCOLOR = 9;
90 const sal_uInt8 TAG_DOACTION = 12;
91 const sal_uInt8 TAG_STARTSOUND = 15;
93 const sal_uInt8 TAG_SOUNDSTREAMBLOCK = 19;
94 const sal_uInt8 TAG_SOUNDSTREAMHEAD = 18;
95 const sal_uInt8 TAG_SOUNDSTREAMHEAD2 = 45;
97 const sal_uInt8 TAG_JPEGTABLES = 8;
98 const sal_uInt8 TAG_DEFINEBITS = 6;
99 const sal_uInt8 TAG_DEFINEBITSLOSSLESS = 20;
100 const sal_uInt8 TAG_DEFINEBITSJPEG2 = 21;
101 const sal_uInt8 TAG_DEFINEBITSJPEG3 = 35;
102 const sal_uInt8 TAG_DEFINEBITSLOSSLESS2 = 36;
103 const sal_uInt8 TAG_DEFINEEDITTEXT= 37;
104 const sal_uInt8 TAG_PLACEOBJECT = 4;
105 const sal_uInt8 TAG_PLACEOBJECT2 = 26;
106 const sal_uInt8 TAG_REMOVEOBJECT2 = 28;
108 const sal_uInt8 TAG_DEFINEFONT = 10;
109 const sal_uInt8 TAG_DEFINETEXT = 11;
110 const sal_uInt8 TAG_DEFINESHAPE3 = 32;
111 const sal_uInt8 TAG_DEFINESPRITE = 39;
113 const sal_uInt8 TAG_FRAMELABEL = 43;
115 const sal_uInt8 TAG_HEADER = 0xff;
117 ///////////////////////////////////////////////////////////////////////
119 /** converts a double to a 16.16 flash fixed value */
120 sal_uInt32 getFixed( double fValue );
122 ///////////////////////////////////////////////////////////////////////
124 typedef ::std::map<sal_uInt32, sal_uInt16> ChecksumCache;
126 /** unsigned int 16 compare operation for stl */
127 struct ltuint16
129 bool operator()(sal_uInt16 s1, sal_uInt16 s2) const
131 return s1 < s2;
135 ///////////////////////////////////////////////////////////////////////
137 /** container class to create bit structures */
138 class BitStream
140 public:
141 BitStream();
143 void writeUB( sal_uInt32 nValue, sal_uInt16 nBits );
144 void writeSB( sal_Int32 nValue, sal_uInt16 nBits );
145 void writeFB( sal_uInt32 nValue, sal_uInt16 nBits );
147 void pad();
148 void writeTo( SvStream& out );
150 sal_uInt32 getOffset() const;
151 private:
153 std::vector< sal_uInt8 > maData;
154 sal_uInt8 mnBitPos;
155 sal_uInt8 mnCurrentByte;
158 ///////////////////////////////////////////////////////////////////////
160 /** this class collects all used glyphs for a given fonts and maps
161 characters to glyph ids.
163 class FlashFont
165 public:
166 FlashFont( const Font& rFont, sal_uInt16 nId );
167 ~FlashFont();
169 sal_uInt16 getGlyph( sal_uInt16 nChar, VirtualDevice* pVDev );
171 void write( SvStream& out );
173 sal_uInt16 getID() const { return mnId; }
174 const Font& getFont() { return maFont; }
176 private:
177 const Font maFont;
178 std::map<sal_uInt16, sal_uInt16, ltuint16> maGlyphIndex;
179 sal_uInt16 mnNextIndex;
180 sal_uInt16 mnId;
181 BitStream maGlyphData;
182 std::vector< sal_uInt16 > maGlyphOffsets;
185 typedef std::vector<FlashFont*> FontMap;
187 ///////////////////////////////////////////////////////////////////////
189 /** this class helps creating flash tags */
190 class Tag : public SvMemoryStream
192 public:
193 Tag( sal_uInt8 nTagId );
195 sal_uInt8 getTagId() const { return mnTagId; }
197 void write( SvStream& out );
199 void addUI32( sal_uInt32 nValue );
200 //unused as of yet void addI32( sal_Int32 nValue );
201 void addUI16( sal_uInt16 nValue );
202 //unused as of yet void addI16( sal_Int16 nValue );
203 void addUI8( sal_uInt8 nValue );
204 void addBits( BitStream& rIn );
206 void addRGBA( const Color& rColor );
207 void addRGB( const Color& rColor );
208 void addRect( const Rectangle& rRect );
209 void addMatrix( const ::basegfx::B2DHomMatrix& rMatrix ); // #i73264#
210 void addString( const char* pString );
211 void addStream( SvStream& rIn );
213 static void writeMatrix( SvStream& rOut, const ::basegfx::B2DHomMatrix& rMatrix ); // #i73264#
214 static void writeRect( SvStream& rOut, const Rectangle& rRect );
216 private:
217 sal_uInt8 mnTagId;
220 ///////////////////////////////////////////////////////////////////////
222 /** this class helps to define flash sprites */
223 class Sprite
225 public:
226 Sprite( sal_uInt16 nId );
227 ~Sprite();
229 void write( SvStream& out );
231 sal_uInt16 getId() const { return mnId; }
233 void addTag( Tag* pNewTag );
235 private:
236 std::vector< Tag* > maTags;
237 sal_uInt16 mnId;
238 sal_uInt32 mnFrames;
241 ///////////////////////////////////////////////////////////////////////
243 /** this class stores a flash fill style for shapes */
244 class FillStyle
246 public:
247 enum FillStyleType { solid = 0x00, linear_gradient = 0x10, radial_gradient = 0x12, tiled_bitmap = 0x40, clipped_bitmap = 0x41 };
249 /** this c'tor creates a solid fill style */
250 FillStyle( const Color& rSolidColor );
252 /** this c'tor creates a linear or radial gradient fill style */
253 FillStyle( const Rectangle& rBoundRect, const Gradient& rGradient );
255 /** this c'tor creates a tiled or clipped bitmap fill style */
256 FillStyle( sal_uInt16 nBitmapId, bool bClipped, const ::basegfx::B2DHomMatrix& rMatrix ); // #i73264#
258 void addTo( Tag* pTag ) const;
260 private:
261 void Impl_addGradient( Tag* pTag ) const;
263 FillStyleType meType;
264 ::basegfx::B2DHomMatrix maMatrix; // #i73264#
265 sal_uInt16 mnBitmapId;
266 Color maColor;
267 Gradient maGradient;
268 Rectangle maBoundRect;
271 ///////////////////////////////////////////////////////////////////////
273 /** this class creates a flash movie from vcl geometry */
274 class Writer
276 friend class FlashFont;
278 public:
279 /** creates a writer for a new flash movie.
280 nDocWidth and nDocHeight are the dimensions of the movie.
281 They must be in 100th/mm.
283 An invisible shape with the size of the document is placed at depth 1
284 and it clips all shapes on depth 2 and 3.
286 Writer( sal_Int32 nDocWidthInput, sal_Int32 nDocHeightInput, sal_Int32 nDocWidth, sal_Int32 nDocHeight, sal_Int32 nJPEGcompressMode = -1 );
287 ~Writer();
289 void storeTo( com::sun::star::uno::Reference< com::sun::star::io::XOutputStream > &xOutStream );
291 // geometry
292 void setClipping( const PolyPolygon* pClipPolyPolygon );
294 /** defines a flash shape from a filled polygon.
295 The coordinates must be in twips */
296 sal_uInt16 defineShape( const Polygon& rPoly, const FillStyle& rFillStyle );
298 /** defines a flash shape from a filled polypolygon.
299 The coordinates must be in twips */
300 sal_uInt16 defineShape( const PolyPolygon& rPolyPoly, const FillStyle& rFillStyle );
302 /** defines a flash shape from a outlined polypolygon.
303 The coordinates must be in twips */
304 sal_uInt16 defineShape( const PolyPolygon& rPolyPoly, sal_uInt16 nLineWidth, const Color& rLineColor );
306 /** defines a flash shape from a vcl metafile.
307 The mapmode of the metafile is used to map all coordinates to twips.
308 A character id of a flash sprite is returned that contains all geometry
309 from the metafile.
311 sal_uInt16 defineShape( const GDIMetaFile& rMtf, sal_Int16 x = 0, sal_Int16 y = 0 );
313 /** defines a bitmap and returns its flash id.
315 sal_uInt16 defineBitmap( const BitmapEx& bmpSource, sal_Int32 nJPEGQualityLevel );
317 // control tags
319 /** inserts a place shape tag into the movie stream or the current sprite */
320 void placeShape( sal_uInt16 nID, sal_uInt16 nDepth, sal_Int32 x, sal_Int32 y, sal_uInt16 nClipDepth = 0, const char* pName = NULL );
322 /** inserts a move shape tag into the movie stream or the current sprite */
323 void moveShape( sal_uInt16 nDepth, sal_Int32 x, sal_Int32 y );
325 /** inserts a remove shape tag into the movie stream or the current sprite */
326 void removeShape( sal_uInt16 nDepth );
328 /** inserts a show frame tag into the movie stream or the current sprite */
329 void showFrame();
331 /** creates a new sprite and sets it as the current sprite for editing.
332 Only one sprite can be edited at one time */
333 sal_uInt16 startSprite();
335 /** ends editing of the curent sprites and adds it to the movie stream */
336 void endSprite();
338 /** inserts a doaction tag with an ActionStop */
339 void stop();
341 /** inserts a doaction tag with an ActionStop, place a button on depth nDepth that
342 continues playback on click */
343 void waitOnClick( sal_uInt16 nDepth );
345 /** inserts a doaction tag with an ActionGotoFrame */
346 void gotoFrame( sal_uInt16 nFrame );
348 #ifdef AUGUSTUS
349 /** stream out a sound. Should make it more intelligent so it interleaves with other items.*/
350 sal_Bool streamSound( const char * filename );
351 #endif
353 private:
354 Point map( const Point& rPoint ) const;
355 Size map( const Size& rSize ) const;
356 void map( PolyPolygon& rPolyPolygon ) const;
357 sal_Int32 mapRelative( sal_Int32 n100thMM ) const;
359 void startTag( sal_uInt8 nTagId );
360 void endTag();
361 sal_uInt16 createID();
363 void Impl_writeBmp( sal_uInt16 nBitmapId, sal_uInt32 width, sal_uInt32 height, sal_uInt8 *pCompressed, sal_uInt32 compressed_size );
364 void Impl_writeImage( const BitmapEx& rBmpEx, const Point& rPt, const Size& rSz, const Point& rSrcPt, const Size& rSrcSz, const Rectangle& rClipRect, bool bMap );
365 void Impl_writeJPEG(sal_uInt16 nBitmapId, const sal_uInt8* pJpgData, sal_uInt32 nJpgDataLength, sal_uInt8 *pCompressed, sal_uInt32 compressed_size );
366 void Impl_writeActions( const GDIMetaFile& rMtf );
367 void Impl_writePolygon( const Polygon& rPoly, sal_Bool bFilled );
368 void Impl_writePolygon( const Polygon& rPoly, sal_Bool bFilled, const Color& rFillColor, const Color& rLineColor );
369 void Impl_writePolyPolygon( const PolyPolygon& rPolyPoly, sal_Bool bFilled, sal_uInt8 nTransparence = 0);
370 void Impl_writePolyPolygon( const PolyPolygon& rPolyPoly, sal_Bool bFilled, const Color& rFillColor, const Color& rLineColor );
371 void Impl_writeText( const Point& rPos, const String& rText, const sal_Int32* pDXArray, long nWidth );
372 void Impl_writeText( const Point& rPos, const String& rText, const sal_Int32* pDXArray, long nWidth, Color aTextColor );
373 void Impl_writeGradientEx( const PolyPolygon& rPolyPoly, const Gradient& rGradient );
374 void Impl_writeLine( const Point& rPt1, const Point& rPt2, const Color* pLineColor = NULL );
375 void Impl_writeRect( const Rectangle& rRect, long nRadX, long nRadY );
376 void Impl_writeEllipse( const Point& rCenter, long nRadX, long nRadY );
377 bool Impl_writeFilling( SvtGraphicFill& rFilling );
378 bool Impl_writeStroke( SvtGraphicStroke& rStroke );
380 FlashFont& Impl_getFont( const Font& rFont );
382 static void Impl_addPolygon( BitStream& rBits, const Polygon& rPoly, sal_Bool bFilled );
384 static void Impl_addShapeRecordChange( BitStream& rBits, sal_Int16 dx, sal_Int16 dy, sal_Bool bFilled );
385 static void Impl_addStraightEdgeRecord( BitStream& rBits, sal_Int16 dx, sal_Int16 dy );
386 static void Impl_addCurvedEdgeRecord( BitStream& rBits, sal_Int16 control_dx, sal_Int16 control_dy, sal_Int16 anchor_dx, sal_Int16 anchor_dy );
387 static void Impl_addEndShapeRecord( BitStream& rBits );
389 static void Impl_addStraightLine( BitStream& rBits,
390 Point& rLastPoint,
391 const double P2x, const double P2y );
392 static void Impl_addQuadBezier( BitStream& rBits,
393 Point& rLastPoint,
394 const double P2x, const double P2y,
395 const double P3x, const double P3y );
396 static void Impl_quadBezierApprox( BitStream& rBits,
397 Point& rLastPoint,
398 const double d2,
399 const double P1x, const double P1y,
400 const double P2x, const double P2y,
401 const double P3x, const double P3y,
402 const double P4x, const double P4y );
404 com::sun::star::uno::Reference < com::sun::star::i18n::XBreakIterator > Impl_GetBreakIterator();
406 private:
407 com::sun::star::uno::Reference< com::sun::star::i18n::XBreakIterator > mxBreakIterator;
409 FontMap maFonts;
411 sal_Int32 mnDocWidth;
412 sal_Int32 mnDocHeight;
414 // AS: Scaling factor for output.
415 double mnDocXScale;
416 double mnDocYScale;
418 sal_uInt16 mnWhiteBackgroundShapeId;
419 sal_uInt16 mnPageButtonId;
421 VirtualDevice* mpVDev;
423 const PolyPolygon* mpClipPolyPolygon;
425 /** holds the informations of the objects defined in the movie stream
426 while executing defineShape
428 typedef std::vector<sal_uInt16> CharacterIdVector;
429 CharacterIdVector maShapeIds;
431 Tag* mpTag;
432 Sprite* mpSprite;
433 std::stack<Sprite*> mvSpriteStack;
434 ChecksumCache mBitmapCache;
436 sal_uInt16 mnNextId;
437 sal_uInt32 mnFrames;
439 // com::sun::star::uno::Reference< com::sun::star::io::XOutputStream > mxOutStream;
440 oslFileHandle mxOutStream;
442 utl::TempFile maMovieTempFile;
443 utl::TempFile maFontsTempFile;
445 SvStream* mpMovieStream;
446 SvStream* mpFontsStream;
448 #ifdef AUGUSTUS
449 lame_global_flags *m_lame_flags;
450 #endif
452 sal_uInt8 mnGlobalTransparency;
453 sal_Int32 mnJPEGCompressMode;
456 ///////////////////////////////////////////////////////////////////////
460 #endif