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"
26 #include <QApplication>
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 static QApplication *app = NULL;
51 Q_DECLARE_METATYPE(QTextCursor);
53 class ImageItem: public QGraphicsItem
65 virtual QRectF boundingRect() const
67 return QRectF(0, 0, m_img.width(), m_img.height());
70 virtual void paint( QPainter *painter,
71 const QStyleOptionGraphicsItem * /*option*/,
74 painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
75 painter->drawImage(QPoint(), m_img);
80 QRectF stringToRect( const QString & s )
83 QStringList l = s.split( ',' );
86 return QRectF( l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(), l.at( 3 ).toDouble() ).normalized();
89 QColor stringToColor( const QString & s )
91 QStringList l = s.split( ',' );
94 return QColor( l.at( 0 ).toInt(), l.at( 1 ).toInt(), l.at( 2 ).toInt(), l.at( 3 ).toInt() );
97 QTransform stringToTransform( const QString& s )
99 QStringList l = s.split( ',' );
103 l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(),
104 l.at( 3 ).toDouble(), l.at( 4 ).toDouble(), l.at( 5 ).toDouble(),
105 l.at( 6 ).toDouble(), l.at( 7 ).toDouble(), l.at( 8 ).toDouble()
109 static void qscene_delete( void *data )
111 QGraphicsScene *scene = ( QGraphicsScene * )data;
112 if (scene) delete scene;
117 void loadFromXml( mlt_producer producer, QGraphicsScene *scene, const char *templateXml, const char *templateText )
120 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
122 QString data = QString::fromUtf8(templateXml);
123 QString replacementText = QString::fromUtf8(templateText);
124 doc.setContent(data);
125 QDomElement title = doc.documentElement();
127 // Check for invalid title
128 if ( title.isNull() || title.tagName() != "kdenlivetitle" ) return;
130 // Check title locale
131 if ( title.hasAttribute( "LC_NUMERIC" ) ) {
132 QString locale = title.attribute( "LC_NUMERIC" );
133 QLocale::setDefault( locale );
138 if ( title.hasAttribute("width") ) {
139 originalWidth = title.attribute("width").toInt();
140 originalHeight = title.attribute("height").toInt();
141 scene->setSceneRect(0, 0, originalWidth, originalHeight);
144 originalWidth = scene->sceneRect().width();
145 originalHeight = scene->sceneRect().height();
147 if ( title.hasAttribute( "out" ) ) {
148 mlt_properties_set_position( producer_props, "_animation_out", title.attribute( "out" ).toDouble() );
151 mlt_properties_set_position( producer_props, "_animation_out", mlt_producer_get_out( producer ) );
154 mlt_properties_set_int( producer_props, "_original_width", originalWidth );
155 mlt_properties_set_int( producer_props, "_original_height", originalHeight );
158 QDomNodeList items = title.elementsByTagName("item");
159 for ( int i = 0; i < items.count(); i++ )
161 QGraphicsItem *gitem = NULL;
162 node = items.item( i );
163 QDomNamedNodeMap nodeAttributes = node.attributes();
164 int zValue = nodeAttributes.namedItem( "z-index" ).nodeValue().toInt();
165 if ( zValue > -1000 )
167 if ( nodeAttributes.namedItem( "type" ).nodeValue() == "QGraphicsTextItem" )
169 QDomNamedNodeMap txtProperties = node.namedItem( "content" ).attributes();
170 QFont font( txtProperties.namedItem( "font" ).nodeValue() );
171 QDomNode propsNode = txtProperties.namedItem( "font-bold" );
172 if ( !propsNode.isNull() )
174 // Old: Bold/Not bold.
175 font.setBold( propsNode.nodeValue().toInt() );
179 // New: Font weight (QFont::)
180 font.setWeight( txtProperties.namedItem( "font-weight" ).nodeValue().toInt() );
182 font.setItalic( txtProperties.namedItem( "font-italic" ).nodeValue().toInt() );
183 font.setUnderline( txtProperties.namedItem( "font-underline" ).nodeValue().toInt() );
184 // Older Kdenlive version did not store pixel size but point size
185 if ( txtProperties.namedItem( "font-pixel-size" ).isNull() )
188 f2.setPointSize( txtProperties.namedItem( "font-size" ).nodeValue().toInt() );
189 font.setPixelSize( QFontInfo( f2 ).pixelSize() );
192 font.setPixelSize( txtProperties.namedItem( "font-pixel-size" ).nodeValue().toInt() );
193 QColor col( stringToColor( txtProperties.namedItem( "font-color" ).nodeValue() ) );
194 QString text = node.namedItem( "content" ).firstChild().nodeValue();
195 if ( !replacementText.isEmpty() )
197 text = text.replace( "%s", replacementText );
199 QGraphicsTextItem *txt = scene->addText(text, font);
200 if (txtProperties.namedItem("font-outline").nodeValue().toDouble()>0.0){
201 QTextDocument *doc = txt->document();
202 // Make sure some that the text item does not request refresh by itself
203 doc->blockSignals(true);
204 QTextCursor cursor(doc);
205 cursor.select(QTextCursor::Document);
206 QTextCharFormat format;
207 format.setTextOutline(
208 QPen(QColor( stringToColor( txtProperties.namedItem( "font-outline-color" ).nodeValue() ) ),
209 txtProperties.namedItem("font-outline").nodeValue().toDouble(),
210 Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin)
212 format.setForeground(QBrush(col));
214 cursor.mergeCharFormat(format);
216 txt->setDefaultTextColor( col );
220 if (!txtProperties.namedItem( "typewriter" ).isNull()) {
222 mlt_properties_set_int( producer_props, "_animated", 1 );
223 QStringList effetData = QStringList() << "typewriter" << text << txtProperties.namedItem( "typewriter" ).nodeValue();
224 txt->setData(0, effetData);
225 if ( !txtProperties.namedItem( "textwidth" ).isNull() )
226 txt->setData( 1, txtProperties.namedItem( "textwidth" ).nodeValue() );
229 if ( txtProperties.namedItem( "alignment" ).isNull() == false )
231 txt->setTextWidth( txt->boundingRect().width() );
232 QTextOption opt = txt->document()->defaultTextOption ();
233 opt.setAlignment(( Qt::Alignment ) txtProperties.namedItem( "alignment" ).nodeValue().toInt() );
234 txt->document()->setDefaultTextOption (opt);
236 if ( !txtProperties.namedItem( "kdenlive-axis-x-inverted" ).isNull() )
238 //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
240 if ( !txtProperties.namedItem( "kdenlive-axis-y-inverted" ).isNull() )
242 //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
246 else if ( nodeAttributes.namedItem( "type" ).nodeValue() == "QGraphicsRectItem" )
248 QString rect = node.namedItem( "content" ).attributes().namedItem( "rect" ).nodeValue();
249 QString br_str = node.namedItem( "content" ).attributes().namedItem( "brushcolor" ).nodeValue();
250 QString pen_str = node.namedItem( "content" ).attributes().namedItem( "pencolor" ).nodeValue();
251 double penwidth = node.namedItem( "content" ).attributes().namedItem( "penwidth") .nodeValue().toDouble();
252 QGraphicsRectItem *rec = scene->addRect( stringToRect( rect ), QPen( QBrush( stringToColor( pen_str ) ), penwidth, Qt::SolidLine, Qt::SquareCap, Qt::RoundJoin ), QBrush( stringToColor( br_str ) ) );
255 else if ( nodeAttributes.namedItem( "type" ).nodeValue() == "QGraphicsPixmapItem" )
257 const QString url = node.namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
258 const QString base64 = items.item(i).namedItem("content").attributes().namedItem("base64").nodeValue();
260 if (base64.isEmpty()){
263 img.loadFromData(QByteArray::fromBase64(base64.toLatin1()));
265 ImageItem *rec = new ImageItem(img);
266 scene->addItem( rec );
269 else if ( nodeAttributes.namedItem( "type" ).nodeValue() == "QGraphicsSvgItem" )
271 QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
272 QString base64 = items.item(i).namedItem("content").attributes().namedItem("base64").nodeValue();
273 QGraphicsSvgItem *rec = NULL;
274 if (base64.isEmpty()){
275 rec = new QGraphicsSvgItem(url);
277 rec = new QGraphicsSvgItem();
278 QSvgRenderer *renderer= new QSvgRenderer(QByteArray::fromBase64(base64.toLatin1()), rec );
279 rec->setSharedRenderer(renderer);
290 QPointF p( node.namedItem( "position" ).attributes().namedItem( "x" ).nodeValue().toDouble(),
291 node.namedItem( "position" ).attributes().namedItem( "y" ).nodeValue().toDouble() );
293 gitem->setTransform( stringToTransform( node.namedItem( "position" ).firstChild().firstChild().nodeValue() ) );
294 int zValue = nodeAttributes.namedItem( "z-index" ).nodeValue().toInt();
295 gitem->setZValue( zValue );
297 #if QT_VERSION >= 0x040600
299 QDomNode eff = items.item(i).namedItem("effect");
301 QDomElement e = eff.toElement();
302 if (e.attribute("type") == "blur") {
303 QGraphicsBlurEffect *blur = new QGraphicsBlurEffect();
304 blur->setBlurRadius(e.attribute("blurradius").toInt());
305 gitem->setGraphicsEffect(blur);
307 else if (e.attribute("type") == "shadow") {
308 QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect();
309 shadow->setBlurRadius(e.attribute("blurradius").toInt());
310 shadow->setOffset(e.attribute("xoffset").toInt(), e.attribute("yoffset").toInt());
311 gitem->setGraphicsEffect(shadow);
318 QDomNode n = title.firstChildElement("background");
320 QColor color = QColor( stringToColor( n.attributes().namedItem( "color" ).nodeValue() ) );
321 if (color.alpha() > 0) {
322 QGraphicsRectItem *rec = scene->addRect(0, 0, scene->width(), scene->height() , QPen( Qt::NoPen ), QBrush( color ) );
323 rec->setZValue(-1100);
329 n = title.firstChildElement( "startviewport" );
330 // Check if node exists, if it has an x attribute, it is an old version title, don't use viewport
331 if (!n.isNull() && !n.toElement().hasAttribute("x"))
333 startRect = n.attributes().namedItem( "rect" ).nodeValue();
335 n = title.firstChildElement( "endviewport" );
336 // Check if node exists, if it has an x attribute, it is an old version title, don't use viewport
337 if (!n.isNull() && !n.toElement().hasAttribute("x"))
339 QString rect = n.attributes().namedItem( "rect" ).nodeValue();
340 if (startRect != rect)
341 mlt_properties_set( producer_props, "_endrect", rect.toUtf8().data() );
343 if (!startRect.isEmpty()) {
344 mlt_properties_set( producer_props, "_startrect", startRect.toUtf8().data() );
350 void drawKdenliveTitle( producer_ktitle self, mlt_frame frame, int width, int height, double position, int force_refresh )
352 // Obtain the producer
353 mlt_producer producer = &self->parent;
354 mlt_profile profile = mlt_service_profile ( MLT_PRODUCER_SERVICE( producer ) ) ;
355 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
357 // Obtain properties of frame
358 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
360 pthread_mutex_lock( &self->mutex );
362 // Check if user wants us to reload the image
363 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 )
365 //mlt_cache_item_close( self->image_cache );
366 self->current_image = NULL;
367 mlt_properties_set_data( producer_props, "cached_image", NULL, 0, NULL, NULL );
368 mlt_properties_set_int( producer_props, "force_reload", 0 );
371 if (self->current_image == NULL) {
372 // restore QGraphicsScene
373 QGraphicsScene *scene = static_cast<QGraphicsScene *> (mlt_properties_get_data( producer_props, "qscene", NULL ));
375 if ( force_refresh == 1 && scene )
378 mlt_properties_set_data( producer_props, "qscene", NULL, 0, NULL, NULL );
385 argv[0] = (char*) "xxx";
387 // Warning: all Qt graphic objects (QRect, ...) must be initialized AFTER
388 // the QApplication is created, otherwise their will be NULL
396 if ( getenv("DISPLAY") == 0 )
398 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" );
399 pthread_mutex_unlock( &self->mutex );
403 app = new QApplication( argc, argv );
404 const char *localename = mlt_properties_get_lcnumeric( MLT_SERVICE_PROPERTIES( MLT_PRODUCER_SERVICE( producer ) ) );
405 QLocale::setDefault( QLocale( localename ) );
407 qRegisterMetaType<QTextCursor>( "QTextCursor" );
409 scene = new QGraphicsScene();
410 scene->setItemIndexMethod( QGraphicsScene::NoIndex );
411 scene->setSceneRect(0, 0, mlt_properties_get_int( properties, "width" ), mlt_properties_get_int( properties, "height" ));
412 if ( mlt_properties_get( producer_props, "resource" ) && mlt_properties_get( producer_props, "resource" )[0] != '\0' )
414 // The title has a resource property, so we read all properties from the resource.
415 // Do not serialize the xmldata
416 loadFromXml( producer, scene, mlt_properties_get( producer_props, "_xmldata" ), mlt_properties_get( producer_props, "templatetext" ) );
420 // The title has no resource, all data should be serialized
421 loadFromXml( producer, scene, mlt_properties_get( producer_props, "xmldata" ), mlt_properties_get( producer_props, "templatetext" ) );
424 mlt_properties_set_data( producer_props, "qscene", scene, 0, ( mlt_destructor )qscene_delete, NULL );
427 QRectF start = stringToRect( QString( mlt_properties_get( producer_props, "_startrect" ) ) );
428 QRectF end = stringToRect( QString( mlt_properties_get( producer_props, "_endrect" ) ) );
430 int originalWidth = mlt_properties_get_int( producer_props, "_original_width" );
431 int originalHeight= mlt_properties_get_int( producer_props, "_original_height" );
432 const QRectF source( 0, 0, width, height );
433 if (start.isNull()) {
434 start = QRectF( 0, 0, originalWidth, originalHeight );
438 QList <QGraphicsItem *> items = scene->items();
439 QGraphicsTextItem *titem = NULL;
440 for (int i = 0; i < items.count(); i++) {
441 titem = static_cast <QGraphicsTextItem*> ( items.at( i ) );
442 if (titem && !titem->data( 0 ).isNull()) {
443 QStringList params = titem->data( 0 ).toStringList();
444 if (params.at( 0 ) == "typewriter" ) {
445 // typewriter effect has 2 param values:
446 // the keystroke delay and a start offset, both in frames
447 QStringList values = params.at( 2 ).split( ";" );
448 int interval = qMax( 0, ( ( int ) position - values.at( 1 ).toInt()) / values.at( 0 ).toInt() );
449 QTextCursor cursor = titem->textCursor();
450 cursor.movePosition(QTextCursor::EndOfBlock);
451 // get the font format
452 QTextCharFormat format = cursor.charFormat();
453 cursor.select(QTextCursor::Document);
454 QString txt = params.at( 1 ).left( interval );
455 // If the string to insert is empty, insert a space / linebreak so that we don't loose
456 // formatting infos for the next iterations
457 int lines = params.at( 1 ).count( '\n' );
459 for (int i = 0; i < lines; i++)
460 empty.append( "\n " );
461 cursor.insertText( txt.isEmpty() ? empty : txt, format );
462 if ( !titem->data( 1 ).isNull() )
463 titem->setTextWidth( titem->data( 1 ).toDouble() );
468 //must be extracted from kdenlive title
469 QImage img( width, height, QImage::Format_ARGB32 );
473 p1.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );
474 //| QPainter::SmoothPixmapTransform );
475 mlt_position anim_out = mlt_properties_get_position( producer_props, "_animation_out" );
479 scene->render( &p1, source, start, Qt::IgnoreAspectRatio );
481 else if ( position > anim_out ) {
482 scene->render( &p1, source, end, Qt::IgnoreAspectRatio );
485 double percentage = 0;
486 if ( position && anim_out )
487 percentage = position / anim_out;
488 QPointF topleft = start.topLeft() + ( end.topLeft() - start.topLeft() ) * percentage;
489 QPointF bottomRight = start.bottomRight() + ( end.bottomRight() - start.bottomRight() ) * percentage;
490 const QRectF r1( topleft, bottomRight );
491 scene->render( &p1, source, r1, Qt::IgnoreAspectRatio );
492 if ( profile && !profile->progressive ){
494 double percentage_next_filed = ( position + 0.5 ) / anim_out;
495 QPointF topleft_next_field = start.topLeft() + ( end.topLeft() - start.topLeft() ) * percentage_next_filed;
496 QPointF bottomRight_next_field = start.bottomRight() + ( end.bottomRight() - start.bottomRight() ) * percentage_next_filed;
497 const QRectF r2( topleft_next_field, bottomRight_next_field );
498 QImage img1( width, height, QImage::Format_ARGB32 );
502 p2.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );
503 scene->render(&p2,source,r2, Qt::IgnoreAspectRatio );
505 int next_field_line = ( mlt_properties_get_int( producer_props, "top_field_first" ) ? 1 : 0 );
506 for (line = next_field_line ;line<height;line+=2){
507 memcpy(img.scanLine(line),img1.scanLine(line),img.bytesPerLine());
514 int size = width * height * 4;
515 uint8_t *pointer=img.bits();
516 QRgb* src = ( QRgb* ) pointer;
517 self->current_image = ( uint8_t * )mlt_pool_alloc( size );
518 uint8_t *dst = self->current_image;
520 for ( int i = 0; i < width * height * 4; i += 4 )
523 *dst++=qGreen( *src );
524 *dst++=qBlue( *src );
525 *dst++=qAlpha( *src );
529 mlt_properties_set_data( producer_props, "cached_image", self->current_image, size, mlt_pool_release, NULL );
530 self->current_width = width;
531 self->current_height = height;
534 pthread_mutex_unlock( &self->mutex );
535 mlt_properties_set_int( properties, "width", self->current_width );
536 mlt_properties_set_int( properties, "height", self->current_height );