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