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>
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.
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.
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
21 #include "kdenlivetitle_wrapper.h"
28 #include <QGraphicsScene>
29 #include <QGraphicsTextItem>
30 #include <QGraphicsSvgItem>
31 #include <QSvgRenderer>
32 #include <QTextCursor>
33 #include <QTextDocument>
34 #include <QStyleOptionGraphicsItem>
38 #include <QDomElement>
42 #include <framework/mlt_log.h>
44 #if QT_VERSION >= 0x040600
45 #include <QGraphicsEffect>
46 #include <QGraphicsBlurEffect>
47 #include <QGraphicsDropShadowEffect>
50 Q_DECLARE_METATYPE(QTextCursor);
52 class ImageItem: public QGraphicsItem
64 virtual QRectF boundingRect() const
66 return QRectF(0, 0, m_img.width(), m_img.height());
69 virtual void paint( QPainter *painter,
70 const QStyleOptionGraphicsItem * /*option*/,
73 painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
74 painter->drawImage(QPoint(), m_img);
79 QRectF stringToRect( const QString & s )
82 QStringList l = s.split( ',' );
85 return QRectF( l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(), l.at( 3 ).toDouble() ).normalized();
88 QColor stringToColor( const QString & s )
90 QStringList l = s.split( ',' );
93 return QColor( l.at( 0 ).toInt(), l.at( 1 ).toInt(), l.at( 2 ).toInt(), l.at( 3 ).toInt() );
96 QTransform stringToTransform( const QString& s )
98 QStringList l = s.split( ',' );
102 l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(),
103 l.at( 3 ).toDouble(), l.at( 4 ).toDouble(), l.at( 5 ).toDouble(),
104 l.at( 6 ).toDouble(), l.at( 7 ).toDouble(), l.at( 8 ).toDouble()
108 static void qscene_delete( void *data )
110 QGraphicsScene *scene = ( QGraphicsScene * )data;
111 if (scene) delete scene;
116 void loadFromXml( mlt_producer producer, QGraphicsScene *scene, const char *templateXml, const char *templateText )
119 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
121 QString data = QString::fromUtf8(templateXml);
122 QString replacementText = QString::fromUtf8(templateText);
123 doc.setContent(data);
124 QDomElement title = doc.documentElement();
126 // Check for invalid title
127 if ( title.isNull() || title.tagName() != "kdenlivetitle" ) return;
129 // Check title locale
130 if ( title.hasAttribute( "LC_NUMERIC" ) ) {
131 QString locale = title.attribute( "LC_NUMERIC" );
132 QLocale::setDefault( locale );
137 if ( title.hasAttribute("width") ) {
138 originalWidth = title.attribute("width").toInt();
139 originalHeight = title.attribute("height").toInt();
140 scene->setSceneRect(0, 0, originalWidth, originalHeight);
143 originalWidth = scene->sceneRect().width();
144 originalHeight = scene->sceneRect().height();
146 if ( title.hasAttribute( "out" ) ) {
147 mlt_properties_set_position( producer_props, "_animation_out", title.attribute( "out" ).toDouble() );
150 mlt_properties_set_position( producer_props, "_animation_out", mlt_producer_get_out( producer ) );
153 mlt_properties_set_int( producer_props, "_original_width", originalWidth );
154 mlt_properties_set_int( producer_props, "_original_height", originalHeight );
157 QDomNodeList items = title.elementsByTagName("item");
158 for ( int i = 0; i < items.count(); i++ )
160 QGraphicsItem *gitem = NULL;
161 node = items.item( i );
162 QDomNamedNodeMap nodeAttributes = node.attributes();
163 int zValue = nodeAttributes.namedItem( "z-index" ).nodeValue().toInt();
164 if ( zValue > -1000 )
166 if ( nodeAttributes.namedItem( "type" ).nodeValue() == "QGraphicsTextItem" )
168 QDomNamedNodeMap txtProperties = node.namedItem( "content" ).attributes();
169 QFont font( txtProperties.namedItem( "font" ).nodeValue() );
170 QDomNode propsNode = txtProperties.namedItem( "font-bold" );
171 if ( !propsNode.isNull() )
173 // Old: Bold/Not bold.
174 font.setBold( propsNode.nodeValue().toInt() );
178 // New: Font weight (QFont::)
179 font.setWeight( txtProperties.namedItem( "font-weight" ).nodeValue().toInt() );
181 font.setItalic( txtProperties.namedItem( "font-italic" ).nodeValue().toInt() );
182 font.setUnderline( txtProperties.namedItem( "font-underline" ).nodeValue().toInt() );
183 // Older Kdenlive version did not store pixel size but point size
184 if ( txtProperties.namedItem( "font-pixel-size" ).isNull() )
187 f2.setPointSize( txtProperties.namedItem( "font-size" ).nodeValue().toInt() );
188 font.setPixelSize( QFontInfo( f2 ).pixelSize() );
191 font.setPixelSize( txtProperties.namedItem( "font-pixel-size" ).nodeValue().toInt() );
192 QColor col( stringToColor( txtProperties.namedItem( "font-color" ).nodeValue() ) );
193 QString text = node.namedItem( "content" ).firstChild().nodeValue();
194 if ( !replacementText.isEmpty() )
196 text = text.replace( "%s", replacementText );
198 QGraphicsTextItem *txt = scene->addText(text, font);
199 if (txtProperties.namedItem("font-outline").nodeValue().toDouble()>0.0){
200 QTextDocument *doc = txt->document();
201 // Make sure some that the text item does not request refresh by itself
202 doc->blockSignals(true);
203 QTextCursor cursor(doc);
204 cursor.select(QTextCursor::Document);
205 QTextCharFormat format;
206 format.setTextOutline(
207 QPen(QColor( stringToColor( txtProperties.namedItem( "font-outline-color" ).nodeValue() ) ),
208 txtProperties.namedItem("font-outline").nodeValue().toDouble(),
209 Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin)
211 format.setForeground(QBrush(col));
213 cursor.mergeCharFormat(format);
215 txt->setDefaultTextColor( col );
219 if (!txtProperties.namedItem( "typewriter" ).isNull()) {
221 mlt_properties_set_int( producer_props, "_animated", 1 );
222 QStringList effetData = QStringList() << "typewriter" << text << txtProperties.namedItem( "typewriter" ).nodeValue();
223 txt->setData(0, effetData);
224 if ( !txtProperties.namedItem( "textwidth" ).isNull() )
225 txt->setData( 1, txtProperties.namedItem( "textwidth" ).nodeValue() );
228 if ( txtProperties.namedItem( "alignment" ).isNull() == false )
230 txt->setTextWidth( txt->boundingRect().width() );
231 QTextOption opt = txt->document()->defaultTextOption ();
232 opt.setAlignment(( Qt::Alignment ) txtProperties.namedItem( "alignment" ).nodeValue().toInt() );
233 txt->document()->setDefaultTextOption (opt);
235 if ( !txtProperties.namedItem( "kdenlive-axis-x-inverted" ).isNull() )
237 //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
239 if ( !txtProperties.namedItem( "kdenlive-axis-y-inverted" ).isNull() )
241 //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
245 else if ( nodeAttributes.namedItem( "type" ).nodeValue() == "QGraphicsRectItem" )
247 QString rect = node.namedItem( "content" ).attributes().namedItem( "rect" ).nodeValue();
248 QString br_str = node.namedItem( "content" ).attributes().namedItem( "brushcolor" ).nodeValue();
249 QString pen_str = node.namedItem( "content" ).attributes().namedItem( "pencolor" ).nodeValue();
250 double penwidth = node.namedItem( "content" ).attributes().namedItem( "penwidth") .nodeValue().toDouble();
251 QGraphicsRectItem *rec = scene->addRect( stringToRect( rect ), QPen( QBrush( stringToColor( pen_str ) ), penwidth, Qt::SolidLine, Qt::SquareCap, Qt::RoundJoin ), QBrush( stringToColor( br_str ) ) );
254 else if ( nodeAttributes.namedItem( "type" ).nodeValue() == "QGraphicsPixmapItem" )
256 const QString url = node.namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
257 const QString base64 = items.item(i).namedItem("content").attributes().namedItem("base64").nodeValue();
259 if (base64.isEmpty()){
262 img.loadFromData(QByteArray::fromBase64(base64.toLatin1()));
264 ImageItem *rec = new ImageItem(img);
265 scene->addItem( rec );
268 else if ( nodeAttributes.namedItem( "type" ).nodeValue() == "QGraphicsSvgItem" )
270 QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
271 QString base64 = items.item(i).namedItem("content").attributes().namedItem("base64").nodeValue();
272 QGraphicsSvgItem *rec = NULL;
273 if (base64.isEmpty()){
274 rec = new QGraphicsSvgItem(url);
276 rec = new QGraphicsSvgItem();
277 QSvgRenderer *renderer= new QSvgRenderer(QByteArray::fromBase64(base64.toLatin1()), rec );
278 rec->setSharedRenderer(renderer);
289 QPointF p( node.namedItem( "position" ).attributes().namedItem( "x" ).nodeValue().toDouble(),
290 node.namedItem( "position" ).attributes().namedItem( "y" ).nodeValue().toDouble() );
292 gitem->setTransform( stringToTransform( node.namedItem( "position" ).firstChild().firstChild().nodeValue() ) );
293 int zValue = nodeAttributes.namedItem( "z-index" ).nodeValue().toInt();
294 gitem->setZValue( zValue );
296 #if QT_VERSION >= 0x040600
298 QDomNode eff = items.item(i).namedItem("effect");
300 QDomElement e = eff.toElement();
301 if (e.attribute("type") == "blur") {
302 QGraphicsBlurEffect *blur = new QGraphicsBlurEffect();
303 blur->setBlurRadius(e.attribute("blurradius").toInt());
304 gitem->setGraphicsEffect(blur);
306 else if (e.attribute("type") == "shadow") {
307 QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect();
308 shadow->setBlurRadius(e.attribute("blurradius").toInt());
309 shadow->setOffset(e.attribute("xoffset").toInt(), e.attribute("yoffset").toInt());
310 gitem->setGraphicsEffect(shadow);
317 QDomNode n = title.firstChildElement("background");
319 QColor color = QColor( stringToColor( n.attributes().namedItem( "color" ).nodeValue() ) );
320 if (color.alpha() > 0) {
321 QGraphicsRectItem *rec = scene->addRect(0, 0, scene->width(), scene->height() , QPen( Qt::NoPen ), QBrush( color ) );
322 rec->setZValue(-1100);
328 n = title.firstChildElement( "startviewport" );
329 // Check if node exists, if it has an x attribute, it is an old version title, don't use viewport
330 if (!n.isNull() && !n.toElement().hasAttribute("x"))
332 startRect = n.attributes().namedItem( "rect" ).nodeValue();
334 n = title.firstChildElement( "endviewport" );
335 // Check if node exists, if it has an x attribute, it is an old version title, don't use viewport
336 if (!n.isNull() && !n.toElement().hasAttribute("x"))
338 QString rect = n.attributes().namedItem( "rect" ).nodeValue();
339 if (startRect != rect)
340 mlt_properties_set( producer_props, "_endrect", rect.toUtf8().data() );
342 if (!startRect.isEmpty()) {
343 mlt_properties_set( producer_props, "_startrect", startRect.toUtf8().data() );
349 void drawKdenliveTitle( producer_ktitle self, mlt_frame frame, int width, int height, double position, int force_refresh )
351 // Obtain the producer
352 mlt_producer producer = &self->parent;
353 mlt_profile profile = mlt_service_profile ( MLT_PRODUCER_SERVICE( producer ) ) ;
354 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
356 // Obtain properties of frame
357 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
359 pthread_mutex_lock( &self->mutex );
361 // Check if user wants us to reload the image
362 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 )
364 //mlt_cache_item_close( self->image_cache );
365 self->current_image = NULL;
366 mlt_properties_set_data( producer_props, "cached_image", NULL, 0, NULL, NULL );
367 mlt_properties_set_int( producer_props, "force_reload", 0 );
370 if (self->current_image == NULL) {
371 // restore QGraphicsScene
372 QGraphicsScene *scene = static_cast<QGraphicsScene *> (mlt_properties_get_data( producer_props, "qscene", NULL ));
374 if ( force_refresh == 1 && scene )
377 mlt_properties_set_data( producer_props, "qscene", NULL, 0, NULL, NULL );
382 if ( !createQApplicationIfNeeded( MLT_PRODUCER_SERVICE(producer) ) )
384 if ( QMetaType::UnknownType == QMetaType::type("QTextCursor") )
385 qRegisterMetaType<QTextCursor>( "QTextCursor" );
386 scene = new QGraphicsScene();
387 scene->setItemIndexMethod( QGraphicsScene::NoIndex );
388 scene->setSceneRect(0, 0, mlt_properties_get_int( properties, "width" ), mlt_properties_get_int( properties, "height" ));
389 if ( mlt_properties_get( producer_props, "resource" ) && mlt_properties_get( producer_props, "resource" )[0] != '\0' )
391 // The title has a resource property, so we read all properties from the resource.
392 // Do not serialize the xmldata
393 loadFromXml( producer, scene, mlt_properties_get( producer_props, "_xmldata" ), mlt_properties_get( producer_props, "templatetext" ) );
397 // The title has no resource, all data should be serialized
398 loadFromXml( producer, scene, mlt_properties_get( producer_props, "xmldata" ), mlt_properties_get( producer_props, "templatetext" ) );
401 mlt_properties_set_data( producer_props, "qscene", scene, 0, ( mlt_destructor )qscene_delete, NULL );
404 QRectF start = stringToRect( QString( mlt_properties_get( producer_props, "_startrect" ) ) );
405 QRectF end = stringToRect( QString( mlt_properties_get( producer_props, "_endrect" ) ) );
407 int originalWidth = mlt_properties_get_int( producer_props, "_original_width" );
408 int originalHeight= mlt_properties_get_int( producer_props, "_original_height" );
409 const QRectF source( 0, 0, width, height );
410 if (start.isNull()) {
411 start = QRectF( 0, 0, originalWidth, originalHeight );
415 QList <QGraphicsItem *> items = scene->items();
416 QGraphicsTextItem *titem = NULL;
417 for (int i = 0; i < items.count(); i++) {
418 titem = static_cast <QGraphicsTextItem*> ( items.at( i ) );
419 if (titem && !titem->data( 0 ).isNull()) {
420 QStringList params = titem->data( 0 ).toStringList();
421 if (params.at( 0 ) == "typewriter" ) {
422 // typewriter effect has 2 param values:
423 // the keystroke delay and a start offset, both in frames
424 QStringList values = params.at( 2 ).split( ";" );
425 int interval = qMax( 0, ( ( int ) position - values.at( 1 ).toInt()) / values.at( 0 ).toInt() );
426 QTextCursor cursor = titem->textCursor();
427 cursor.movePosition(QTextCursor::EndOfBlock);
428 // get the font format
429 QTextCharFormat format = cursor.charFormat();
430 cursor.select(QTextCursor::Document);
431 QString txt = params.at( 1 ).left( interval );
432 // If the string to insert is empty, insert a space / linebreak so that we don't loose
433 // formatting infos for the next iterations
434 int lines = params.at( 1 ).count( '\n' );
436 for (int i = 0; i < lines; i++)
437 empty.append( "\n " );
438 cursor.insertText( txt.isEmpty() ? empty : txt, format );
439 if ( !titem->data( 1 ).isNull() )
440 titem->setTextWidth( titem->data( 1 ).toDouble() );
445 //must be extracted from kdenlive title
446 QImage img( width, height, QImage::Format_ARGB32 );
450 p1.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );
451 //| QPainter::SmoothPixmapTransform );
452 mlt_position anim_out = mlt_properties_get_position( producer_props, "_animation_out" );
456 scene->render( &p1, source, start, Qt::IgnoreAspectRatio );
458 else if ( position > anim_out ) {
459 scene->render( &p1, source, end, Qt::IgnoreAspectRatio );
462 double percentage = 0;
463 if ( position && anim_out )
464 percentage = position / anim_out;
465 QPointF topleft = start.topLeft() + ( end.topLeft() - start.topLeft() ) * percentage;
466 QPointF bottomRight = start.bottomRight() + ( end.bottomRight() - start.bottomRight() ) * percentage;
467 const QRectF r1( topleft, bottomRight );
468 scene->render( &p1, source, r1, Qt::IgnoreAspectRatio );
469 if ( profile && !profile->progressive ){
471 double percentage_next_filed = ( position + 0.5 ) / anim_out;
472 QPointF topleft_next_field = start.topLeft() + ( end.topLeft() - start.topLeft() ) * percentage_next_filed;
473 QPointF bottomRight_next_field = start.bottomRight() + ( end.bottomRight() - start.bottomRight() ) * percentage_next_filed;
474 const QRectF r2( topleft_next_field, bottomRight_next_field );
475 QImage img1( width, height, QImage::Format_ARGB32 );
479 p2.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );
480 scene->render(&p2,source,r2, Qt::IgnoreAspectRatio );
482 int next_field_line = ( mlt_properties_get_int( producer_props, "top_field_first" ) ? 1 : 0 );
483 for (line = next_field_line ;line<height;line+=2){
484 memcpy(img.scanLine(line),img1.scanLine(line),img.bytesPerLine());
491 int size = width * height * 4;
492 uint8_t *pointer=img.bits();
493 QRgb* src = ( QRgb* ) pointer;
494 self->current_image = ( uint8_t * )mlt_pool_alloc( size );
495 uint8_t *dst = self->current_image;
497 for ( int i = 0; i < width * height * 4; i += 4 )
500 *dst++=qGreen( *src );
501 *dst++=qBlue( *src );
502 *dst++=qAlpha( *src );
506 mlt_properties_set_data( producer_props, "cached_image", self->current_image, size, mlt_pool_release, NULL );
507 self->current_width = width;
508 self->current_height = height;
511 pthread_mutex_unlock( &self->mutex );
512 mlt_properties_set_int( properties, "width", self->current_width );
513 mlt_properties_set_int( properties, "height", self->current_height );