]> git.sesse.net Git - mlt/blob - src/modules/qimage/kdenlivetitle_wrapper.cpp
Get ready for typewriter effect in titles
[mlt] / src / modules / qimage / kdenlivetitle_wrapper.cpp
1 /*
2  * kdenlivetitle_wrapper.cpp -- kdenlivetitle wrapper
3  * Copyright (c) 2009 Marco Gittler <g.marco@freenet.de>
4  * Copyright (c) 2009 Jean-Baptiste Mardelle <jb@kdenlive.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "kdenlivetitle_wrapper.h"
22
23 #include <QtGui/QImage>
24 #include <QtGui/QPainter>
25 #include <QtCore/QDebug>
26 #include <QtGui/QApplication>
27 #include <QtCore/QMutex>
28 #include <QtGui/QGraphicsScene>
29 #include <QtGui/QGraphicsTextItem>
30 #include <QtSvg/QGraphicsSvgItem>
31 #include <QtGui/QTextCursor>
32 #include <QtGui/QTextDocument>
33 #include <QtGui/QStyleOptionGraphicsItem>
34
35 #include <QtCore/QString>
36
37 #include <QtXml/QDomElement>
38 #include <QtCore/QRectF>
39 #include <QtGui/QColor>
40 #include <QtGui/QWidget>
41 #include <framework/mlt_log.h>
42
43 #if QT_VERSION >= 0x040600
44 #include <QtGui/QGraphicsEffect>
45 #include <QtGui/QGraphicsBlurEffect>
46 #include <QtGui/QGraphicsDropShadowEffect>
47 #endif
48
49 static QApplication *app = NULL;
50
51 class ImageItem: public QGraphicsItem
52 {
53 public:
54     ImageItem(QImage img)
55     {
56         m_img = img;
57     }
58     QImage m_img;
59
60
61 protected:
62
63 virtual QRectF boundingRect() const
64 {
65     return QRectF(0, 0, m_img.width(), m_img.height());
66 }
67
68 virtual void paint( QPainter *painter,
69                        const QStyleOptionGraphicsItem * /*option*/,
70                        QWidget* )
71
72     painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
73     painter->drawImage(QPoint(), m_img);
74 }
75 };
76
77
78 QString rectTransform( QString s, QTransform t )
79 {
80         QStringList l = s.split( ',' );
81         return QString::number(l.at(0).toDouble() * t.m11()) + ',' + QString::number(l.at(1).toDouble() * t.m22()) + ',' + QString::number(l.at(2).toDouble() * t.m11()) + ',' + QString::number(l.at(3).toDouble() * t.m22());
82 }
83
84 QString colorToString( const QColor& c )
85 {
86         QString ret = "%1,%2,%3,%4";
87         ret = ret.arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() );
88         return ret;
89 }
90
91 QString rectFToString( const QRectF& c )
92 {
93         QString ret = "%1,%2,%3,%4";
94         ret = ret.arg( c.top() ).arg( c.left() ).arg( c.width() ).arg( c.height() );
95         return ret;
96 }
97
98 QRectF stringToRect( const QString & s )
99 {
100
101         QStringList l = s.split( ',' );
102         if ( l.size() < 4 )
103                 return QRectF();
104         return QRectF( l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(), l.at( 3 ).toDouble() ).normalized();
105 }
106
107 QColor stringToColor( const QString & s )
108 {
109         QStringList l = s.split( ',' );
110         if ( l.size() < 4 )
111                 return QColor();
112         return QColor( l.at( 0 ).toInt(), l.at( 1 ).toInt(), l.at( 2 ).toInt(), l.at( 3 ).toInt() );
113         ;
114 }
115 QTransform stringToTransform( const QString& s )
116 {
117         QStringList l = s.split( ',' );
118         if ( l.size() < 9 )
119                 return QTransform();
120         return QTransform(
121                    l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(),
122                    l.at( 3 ).toDouble(), l.at( 4 ).toDouble(), l.at( 5 ).toDouble(),
123                    l.at( 6 ).toDouble(), l.at( 7 ).toDouble(), l.at( 8 ).toDouble()
124                );
125 }
126
127 static void qscene_delete( void *data )
128 {
129         QGraphicsScene *scene = ( QGraphicsScene * )data;
130         if (scene) delete scene;
131         scene = NULL;
132 }
133
134
135 void loadFromXml( mlt_producer producer, QGraphicsScene *scene, const char *templateXml, const char *templateText )
136 {
137         scene->clear();
138         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
139         QDomDocument doc;
140         QString data = QString::fromUtf8(templateXml);
141         QString replacementText = QString::fromUtf8(templateText);
142         doc.setContent(data);
143         QDomElement title = doc.documentElement();
144         
145         // Check for invalid title
146         if ( title.isNull() || title.tagName() != "kdenlivetitle" ) return;
147         
148         int originalWidth;
149         int originalHeight;
150         if ( title.hasAttribute("width") ) {
151             originalWidth = title.attribute("width").toInt();
152             originalHeight = title.attribute("height").toInt();
153             scene->setSceneRect(0, 0, originalWidth, originalHeight);
154         }
155         else {
156             originalWidth = scene->sceneRect().width();
157             originalHeight = scene->sceneRect().height();
158         }
159         if ( title.hasAttribute( "out" ) ) {
160             mlt_properties_set_position( producer_props, "_animation_out", title.attribute( "out" ).toDouble() );
161         }
162         else {
163             mlt_properties_set_position( producer_props, "_animation_out", mlt_producer_get_out( producer ) );
164         }
165         
166         mlt_properties_set_int( producer_props, "_original_width", originalWidth );
167         mlt_properties_set_int( producer_props, "_original_height", originalHeight );
168
169         QDomNodeList items = title.elementsByTagName("item");
170         for ( int i = 0; i < items.count(); i++ )
171         {
172                 QGraphicsItem *gitem = NULL;
173                 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
174                 if ( zValue > -1000 )
175                 {
176                         if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsTextItem" )
177                         {
178                                 QDomNamedNodeMap txtProperties = items.item( i ).namedItem( "content" ).attributes();
179                                 QFont font( txtProperties.namedItem( "font" ).nodeValue() );
180                                         QDomNode node = txtProperties.namedItem( "font-bold" );
181                                 if ( !node.isNull() )
182                                 {
183                                 // Old: Bold/Not bold.
184                                         font.setBold( node.nodeValue().toInt() );
185                                 }
186                                 else
187                                 {
188                                         // New: Font weight (QFont::)
189                                         font.setWeight( txtProperties.namedItem( "font-weight" ).nodeValue().toInt() );
190                                 }
191                                         font.setItalic( txtProperties.namedItem( "font-italic" ).nodeValue().toInt() );
192                                 font.setUnderline( txtProperties.namedItem( "font-underline" ).nodeValue().toInt() );
193                                 // Older Kdenlive version did not store pixel size but point size
194                                 if ( txtProperties.namedItem( "font-pixel-size" ).isNull() )
195                                 {
196                                         QFont f2;
197                                         f2.setPointSize( txtProperties.namedItem( "font-size" ).nodeValue().toInt() );
198                                         font.setPixelSize( QFontInfo( f2 ).pixelSize() );
199                                 }
200                                 else
201                                         font.setPixelSize( txtProperties.namedItem( "font-pixel-size" ).nodeValue().toInt() );
202                                 QColor col( stringToColor( txtProperties.namedItem( "font-color" ).nodeValue() ) );
203                                 QString text = items.item( i ).namedItem( "content" ).firstChild().nodeValue();
204                                 if ( !replacementText.isEmpty() )
205                                 {
206                                         text = text.replace( "%s", replacementText );
207                                 }
208                                 QGraphicsTextItem *txt = scene->addText(text, font);
209                                 txt->setDefaultTextColor( col );
210                                 
211                                 // Effects
212                                 if (!txtProperties.namedItem( "typewriter" ).isNull()) {
213                                         // typewriter effect
214                                         mlt_properties_set_int( producer_props, "_animated", 1 );
215                                         QStringList effetData = QStringList() << "typewriter" << text << txtProperties.namedItem( "typewriter" ).nodeValue();
216                                         txt->setData(0, effetData);
217                                 }
218                                 
219                                 if ( txtProperties.namedItem( "alignment" ).isNull() == false )
220                                 {
221                                         txt->setTextWidth( txt->boundingRect().width() );
222                                         QTextOption opt = txt->document()->defaultTextOption ();
223                                         opt.setAlignment(( Qt::Alignment ) txtProperties.namedItem( "alignment" ).nodeValue().toInt() );
224                                         txt->document()->setDefaultTextOption (opt);
225                                 }
226                                         if ( !txtProperties.namedItem( "kdenlive-axis-x-inverted" ).isNull() )
227                                 {
228                                         //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
229                                 }
230                                 if ( !txtProperties.namedItem( "kdenlive-axis-y-inverted" ).isNull() )
231                                 {
232                                         //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
233                                 }
234                                         gitem = txt;
235                         }
236                         else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsRectItem" )
237                         {
238                                 QString rect = items.item( i ).namedItem( "content" ).attributes().namedItem( "rect" ).nodeValue();
239                                 QString br_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "brushcolor" ).nodeValue();
240                                 QString pen_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "pencolor" ).nodeValue();
241                                 double penwidth = items.item( i ).namedItem( "content" ).attributes().namedItem( "penwidth") .nodeValue().toDouble();
242                                 QGraphicsRectItem *rec = scene->addRect( stringToRect( rect ), QPen( QBrush( stringToColor( pen_str ) ), penwidth ), QBrush( stringToColor( br_str ) ) );
243                                 gitem = rec;
244                         }
245                         else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsPixmapItem" )
246                         {
247                                 const QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
248                                 QImage img( url );
249                                 ImageItem *rec = new ImageItem(img);
250                                 scene->addItem( rec );
251                                 gitem = rec;
252                         }
253                         else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsSvgItem" )
254                         {
255                                 const QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
256                                 QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
257                                 scene->addItem(rec);
258                                 gitem = rec;
259                         }
260                 }
261                 //pos and transform
262                 if ( gitem )
263                 {
264                         QPointF p( items.item( i ).namedItem( "position" ).attributes().namedItem( "x" ).nodeValue().toDouble(),
265                                    items.item( i ).namedItem( "position" ).attributes().namedItem( "y" ).nodeValue().toDouble() );
266                         gitem->setPos( p );
267                         gitem->setTransform( stringToTransform( items.item( i ).namedItem( "position" ).firstChild().firstChild().nodeValue() ) );
268                         int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
269                         gitem->setZValue( zValue );
270
271 #if QT_VERSION >= 0x040600
272                         // effects
273                         QDomNode eff = items.item(i).namedItem("effect");
274                         if (!eff.isNull()) {
275                                 QDomElement e = eff.toElement();
276                                 if (e.attribute("type") == "blur") {
277                                         QGraphicsBlurEffect *blur = new QGraphicsBlurEffect();
278                                         blur->setBlurRadius(e.attribute("blurradius").toInt());
279                                         gitem->setGraphicsEffect(blur);
280                                 }
281                                 else if (e.attribute("type") == "shadow") {
282                                         QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect();
283                                         shadow->setBlurRadius(e.attribute("blurradius").toInt());
284                                         shadow->setOffset(e.attribute("xoffset").toInt(), e.attribute("yoffset").toInt());
285                                         gitem->setGraphicsEffect(shadow);
286                                 }
287                         }
288 #endif
289                 }
290         }
291
292         QDomNode n = title.firstChildElement("background");
293         if (!n.isNull()) {
294                 QColor color = QColor( stringToColor( n.attributes().namedItem( "color" ).nodeValue() ) );
295                 if (color.alpha() > 0) {
296                         QGraphicsRectItem *rec = scene->addRect(0, 0, scene->width(), scene->height() , QPen( Qt::NoPen ), QBrush( color ) );
297                         rec->setZValue(-1100);
298                 }
299           
300         }
301
302         QString startRect;
303         n = title.firstChildElement( "startviewport" );
304         // Check if node exists, if it has an x attribute, it is an old version title, don't use viewport
305         if (!n.isNull() && !n.toElement().hasAttribute("x"))
306         {
307                 startRect = n.attributes().namedItem( "rect" ).nodeValue();
308         }
309         n = title.firstChildElement( "endviewport" );
310         // Check if node exists, if it has an x attribute, it is an old version title, don't use viewport
311         if (!n.isNull() && !n.toElement().hasAttribute("x"))
312         {
313                 QString rect = n.attributes().namedItem( "rect" ).nodeValue();
314                 if (startRect != rect)
315                         mlt_properties_set( producer_props, "_endrect", rect.toUtf8().data() );
316         }
317         if (!startRect.isEmpty()) {
318                 mlt_properties_set( producer_props, "_startrect", startRect.toUtf8().data() );
319         }
320         return;
321 }
322
323
324 void drawKdenliveTitle( producer_ktitle self, mlt_frame frame, int width, int height, double position, int force_refresh )
325 {
326         // Obtain the producer 
327         mlt_producer producer = &self->parent;
328         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
329
330         // Obtain properties of frame
331         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
332         
333         pthread_mutex_lock( &self->mutex );
334         
335         // Check if user wants us to reload the image
336         if ( mlt_properties_get( producer_props, "_animated" ) != NULL || force_refresh == 1 || width != self->current_width || height != self->current_height || mlt_properties_get( producer_props, "_endrect" ) != NULL )
337         {
338                 //mlt_cache_item_close( self->image_cache );
339                 self->current_image = NULL;
340                 mlt_properties_set_data( producer_props, "cached_image", NULL, 0, NULL, NULL );
341                 mlt_properties_set_int( producer_props, "force_reload", 0 );
342         }
343         
344         if (self->current_image == NULL) {
345                 // restore QGraphicsScene
346                 QGraphicsScene *scene = static_cast<QGraphicsScene *> (mlt_properties_get_data( producer_props, "qscene", NULL ));
347
348                 if ( force_refresh == 1 && scene )
349                 {
350                         scene = NULL;
351                         mlt_properties_set_data( producer_props, "qscene", NULL, 0, NULL, NULL );
352                 }
353
354                 if ( scene == NULL )
355                 {
356                         int argc = 1;
357                         char* argv[1];
358                         argv[0] = (char*) "xxx";
359                         
360                         // Warning: all Qt graphic objects (QRect, ...) must be initialized AFTER 
361                         // the QApplication is created, otherwise their will be NULL
362                         
363                         if ( app == NULL ) {
364                                 if ( qApp ) {
365                                         app = qApp;
366                                 }
367                                 else {
368 #ifdef linux
369                                         if ( getenv("DISPLAY") == 0 )
370                                         {
371                                                 mlt_log_panic( MLT_PRODUCER_SERVICE( producer ), "Error, cannot render titles without an X11 environment.\nPlease either run melt from an X session or use a fake X server like xvfb:\nxvfb-run -a melt (...)\n" );
372                                                 pthread_mutex_unlock( &self->mutex );
373                                                 exit(1);
374                                                 return;
375                                         }
376 #endif
377                                         app = new QApplication( argc, argv );
378                                         //fix to let the decimal point for every locale be: "."
379                                         setlocale(LC_NUMERIC,"POSIX");
380                                 }
381                         }
382                         scene = new QGraphicsScene();
383                         scene->setSceneRect(0, 0, mlt_properties_get_int( properties, "width" ), mlt_properties_get_int( properties, "height" ));
384                         loadFromXml( producer, scene, mlt_properties_get( producer_props, "xmldata" ), mlt_properties_get( producer_props, "templatetext" ) );
385                         mlt_properties_set_data( producer_props, "qscene", scene, 0, ( mlt_destructor )qscene_delete, NULL );
386                 }
387                 
388                 QRectF start = stringToRect( QString( mlt_properties_get( producer_props, "_startrect" ) ) );
389                 QRectF end = stringToRect( QString( mlt_properties_get( producer_props, "_endrect" ) ) );
390         
391                 int originalWidth = mlt_properties_get_int( producer_props, "_original_width" );
392                 int originalHeight= mlt_properties_get_int( producer_props, "_original_height" );
393                 const QRectF source( 0, 0, width, height );
394                 if (start.isNull()) {
395                     start = QRectF( 0, 0, originalWidth, originalHeight );
396                 }
397
398                 // Effects
399                 QList <QGraphicsItem *> items = scene->items();
400                 QGraphicsTextItem *titem = NULL;
401                 for (int i = 0; i < items.count(); i++) {
402                     titem = static_cast <QGraphicsTextItem*> ( items.at( i ) );
403                     if (titem && !titem->data( 0 ).isNull()) {
404                             QStringList params = titem->data( 0 ).toStringList();
405                             if (params.at( 0 ) == "typewriter") {
406                                     int interval = ( ( int ) position) / params.at( 2 ).toInt();
407                                     QTextDocument *td = new QTextDocument( params.at( 1 ).left( interval ) );
408                                     td->setDefaultFont( titem->font() );
409                                     td->setDefaultTextOption( titem->document()->defaultTextOption() );
410                                     td->setTextWidth( titem->document()->textWidth() );
411                                     titem->setDocument( td );
412                             }
413                     }
414                 }
415
416                 //must be extracted from kdenlive title
417                 QImage img( width, height, QImage::Format_ARGB32 );
418                 img.fill( 0 );
419                 QPainter p1;
420                 p1.begin( &img );
421                 p1.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );
422                 //| QPainter::SmoothPixmapTransform );
423                 mlt_position anim_out = mlt_properties_get_position( producer_props, "_animation_out" );
424
425                 if (end.isNull())
426                 {
427                         scene->render( &p1, source, start, Qt::IgnoreAspectRatio );
428                 }
429                 else if ( position > anim_out ) {
430                         scene->render( &p1, source, end, Qt::IgnoreAspectRatio );
431                 }
432                 else {
433                         double percentage = position / anim_out;
434                         QPointF topleft = start.topLeft() + ( end.topLeft() - start.topLeft() ) * percentage;
435                         QPointF bottomRight = start.bottomRight() + ( end.bottomRight() - start.bottomRight() ) * percentage;
436                         const QRectF r1( topleft, bottomRight );
437                         scene->render( &p1, source, r1, Qt::IgnoreAspectRatio );
438                 }
439                 p1.end();
440
441                 int size = width * height * 4;
442                 uint8_t *pointer=img.bits();
443                 QRgb* src = ( QRgb* ) pointer;
444                 self->current_image = ( uint8_t * )mlt_pool_alloc( size );
445                 uint8_t *dst = self->current_image;
446         
447                 for ( int i = 0; i < width * height * 4; i += 4 )
448                 {
449                         *dst++=qRed( *src );
450                         *dst++=qGreen( *src );
451                         *dst++=qBlue( *src );
452                         *dst++=qAlpha( *src );
453                         src++;
454                 }
455
456                 mlt_properties_set_data( producer_props, "cached_image", self->current_image, size, mlt_pool_release, NULL );
457                 self->current_width = width;
458                 self->current_height = height;
459         }
460
461         pthread_mutex_unlock( &self->mutex );
462         mlt_properties_set_int( properties, "width", self->current_width );
463         mlt_properties_set_int( properties, "height", self->current_height );
464 }
465
466