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