]> git.sesse.net Git - mlt/blob - src/modules/qimage/kdenlivetitle_wrapper.cpp
locale fix for decial numbers
[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/QStyleOptionGraphicsItem>
33
34 #include <QtCore/QString>
35
36 #include <QtXml/QDomElement>
37 #include <QtCore/QRectF>
38 #include <QtGui/QColor>
39 #include <QtGui/QWidget>
40 #include <framework/mlt_log.h>
41
42 static QApplication *app = NULL;
43
44 class ImageItem: public QGraphicsRectItem
45 {
46 public:
47     ImageItem(QImage img)
48     {
49         m_img = img;
50     }
51     QImage m_img;
52
53
54 protected:
55 virtual void paint( QPainter *painter,
56                        const QStyleOptionGraphicsItem * /*option*/,
57                        QWidget* )
58
59     painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
60     painter->drawImage(QPoint(), m_img);
61 }
62 };
63
64
65 QString rectTransform( QString s, QTransform t )
66 {
67         QStringList l = s.split( ',' );
68         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());
69 }
70
71 QString colorToString( const QColor& c )
72 {
73         QString ret = "%1,%2,%3,%4";
74         ret = ret.arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() );
75         return ret;
76 }
77
78 QString rectFToString( const QRectF& c )
79 {
80         QString ret = "%1,%2,%3,%4";
81         ret = ret.arg( c.top() ).arg( c.left() ).arg( c.width() ).arg( c.height() );
82         return ret;
83 }
84
85 QRectF stringToRect( const QString & s )
86 {
87
88         QStringList l = s.split( ',' );
89         if ( l.size() < 4 )
90                 return QRectF();
91         return QRectF( l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(), l.at( 3 ).toDouble() ).normalized();
92 }
93
94 QColor stringToColor( const QString & s )
95 {
96         QStringList l = s.split( ',' );
97         if ( l.size() < 4 )
98                 return QColor();
99         return QColor( l.at( 0 ).toInt(), l.at( 1 ).toInt(), l.at( 2 ).toInt(), l.at( 3 ).toInt() );
100         ;
101 }
102 QTransform stringToTransform( const QString& s )
103 {
104         QStringList l = s.split( ',' );
105         if ( l.size() < 9 )
106                 return QTransform();
107         return QTransform(
108                    l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(),
109                    l.at( 3 ).toDouble(), l.at( 4 ).toDouble(), l.at( 5 ).toDouble(),
110                    l.at( 6 ).toDouble(), l.at( 7 ).toDouble(), l.at( 8 ).toDouble()
111                );
112 }
113
114 static void qscene_delete( void *data )
115 {
116         QGraphicsScene *scene = ( QGraphicsScene * )data;
117         if (scene) delete scene;
118         scene = NULL;
119 }
120
121
122 void loadFromXml( mlt_producer producer, QGraphicsScene *scene, const char *templateXml, const char *templateText )
123 {
124         scene->clear();
125         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
126         QDomDocument doc;
127         QString data = QString::fromUtf8(templateXml);
128         QString replacementText = QString::fromUtf8(templateText);
129         doc.setContent(data);
130         QDomElement title = doc.documentElement();
131         
132         // Check for invalid title
133         if ( title.isNull() || title.tagName() != "kdenlivetitle" ) return;
134         
135         int originalWidth;
136         int originalHeight;
137         if ( title.hasAttribute("width") ) {
138             originalWidth = title.attribute("width").toInt();
139             originalHeight = title.attribute("height").toInt();
140             scene->setSceneRect(0, 0, originalWidth, originalHeight);
141         }
142         else {
143             originalWidth = scene->sceneRect().width();
144             originalHeight = scene->sceneRect().height();
145         }
146         mlt_properties_set_int( producer_props, "_original_width", originalWidth );
147         mlt_properties_set_int( producer_props, "_original_height", originalHeight );
148
149         QDomNodeList items = title.elementsByTagName("item");
150         for ( int i = 0; i < items.count(); i++ )
151         {
152                 QGraphicsItem *gitem = NULL;
153                 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
154                 if ( zValue > -1000 )
155                 {
156                         if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsTextItem" )
157                         {
158                                 QDomNamedNodeMap txtProperties = items.item( i ).namedItem( "content" ).attributes();
159                                 QFont font( txtProperties.namedItem( "font" ).nodeValue() );
160                                         QDomNode node = txtProperties.namedItem( "font-bold" );
161                                 if ( !node.isNull() )
162                                 {
163                                 // Old: Bold/Not bold.
164                                         font.setBold( node.nodeValue().toInt() );
165                                 }
166                                 else
167                                 {
168                                         // New: Font weight (QFont::)
169                                         font.setWeight( txtProperties.namedItem( "font-weight" ).nodeValue().toInt() );
170                                 }
171                                         font.setItalic( txtProperties.namedItem( "font-italic" ).nodeValue().toInt() );
172                                 font.setUnderline( txtProperties.namedItem( "font-underline" ).nodeValue().toInt() );
173                                 // Older Kdenlive version did not store pixel size but point size
174                                 if ( txtProperties.namedItem( "font-pixel-size" ).isNull() )
175                                 {
176                                         QFont f2;
177                                         f2.setPointSize( txtProperties.namedItem( "font-size" ).nodeValue().toInt() );
178                                         font.setPixelSize( QFontInfo( f2 ).pixelSize() );
179                                 }
180                                 else
181                                         font.setPixelSize( txtProperties.namedItem( "font-pixel-size" ).nodeValue().toInt() );
182                                 QColor col( stringToColor( txtProperties.namedItem( "font-color" ).nodeValue() ) );
183                                 QString text = items.item( i ).namedItem( "content" ).firstChild().nodeValue();
184                                 if ( !replacementText.isEmpty() )
185                                 {
186                                         text = text.replace( "%s", replacementText );
187                                 }
188                                 QGraphicsTextItem *txt = scene->addText(text, font);
189                                 txt->setDefaultTextColor( col );
190                                 if ( txtProperties.namedItem( "alignment" ).isNull() == false )
191                                 {
192                                         txt->setTextWidth( txt->boundingRect().width() );
193                                         QTextCursor cur = txt->textCursor();
194                                         QTextBlockFormat format = cur.blockFormat();
195                                         format.setAlignment(( Qt::Alignment ) txtProperties.namedItem( "alignment" ).nodeValue().toInt() );
196                                         cur.select( QTextCursor::Document );
197                                         cur.setBlockFormat( format );
198                                         txt->setTextCursor( cur );
199                                         cur.clearSelection();
200                                         txt->setTextCursor( cur );
201                                 }
202                                         if ( !txtProperties.namedItem( "kdenlive-axis-x-inverted" ).isNull() )
203                                 {
204                                         //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
205                                 }
206                                 if ( !txtProperties.namedItem( "kdenlive-axis-y-inverted" ).isNull() )
207                                 {
208                                         //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
209                                 }
210                                         gitem = txt;
211                         }
212                         else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsRectItem" )
213                         {
214                                 QString rect = items.item( i ).namedItem( "content" ).attributes().namedItem( "rect" ).nodeValue();
215                                 QString br_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "brushcolor" ).nodeValue();
216                                 QString pen_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "pencolor" ).nodeValue();
217                                 double penwidth = items.item( i ).namedItem( "content" ).attributes().namedItem( "penwidth") .nodeValue().toDouble();
218                                 QGraphicsRectItem *rec = scene->addRect( stringToRect( rect ), QPen( QBrush( stringToColor( pen_str ) ), penwidth ), QBrush( stringToColor( br_str ) ) );
219                                 gitem = rec;
220                         }
221                         else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsPixmapItem" )
222                         {
223                                 const QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
224                                 QImage img( url );
225                                 ImageItem *rec = new ImageItem(img);
226                                 scene->addItem( rec );
227                                 gitem = rec;
228                         }
229                         else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsSvgItem" )
230                         {
231                                 const QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
232                                 QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
233                                 scene->addItem(rec);
234                                 gitem = rec;
235                         }
236                 }
237                 //pos and transform
238                 if ( gitem )
239                 {
240                         QPointF p( items.item( i ).namedItem( "position" ).attributes().namedItem( "x" ).nodeValue().toDouble(),
241                                    items.item( i ).namedItem( "position" ).attributes().namedItem( "y" ).nodeValue().toDouble() );
242                         gitem->setPos( p );
243                         gitem->setTransform( stringToTransform( items.item( i ).namedItem( "position" ).firstChild().firstChild().nodeValue() ) );
244                         int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
245                         gitem->setZValue( zValue );
246                 }
247         }
248
249         QDomNode n = title.firstChildElement("background");
250         if (!n.isNull()) {
251                 QColor color = QColor( stringToColor( n.attributes().namedItem( "color" ).nodeValue() ) );
252                 if (color.alpha() > 0) {
253                         QGraphicsRectItem *rec = scene->addRect(0, 0, scene->width(), scene->height() , QPen( Qt::NoPen ), QBrush( color ) );
254                         rec->setZValue(-1100);
255                 }
256           
257         }
258
259         QString startRect;
260         n = title.firstChildElement( "startviewport" );
261         // Check if node exists, if it has an x attribute, it is an old version title, don't use viewport
262         if (!n.isNull() && !n.toElement().hasAttribute("x"))
263         {
264                 startRect = n.attributes().namedItem( "rect" ).nodeValue();
265         }
266         n = title.firstChildElement( "endviewport" );
267         // Check if node exists, if it has an x attribute, it is an old version title, don't use viewport
268         if (!n.isNull() && !n.toElement().hasAttribute("x"))
269         {
270                 QString rect = n.attributes().namedItem( "rect" ).nodeValue();
271                 if (startRect != rect)
272                         mlt_properties_set( producer_props, "_endrect", rect.toUtf8().data() );
273         }
274         if (!startRect.isEmpty()) {
275                 mlt_properties_set( producer_props, "_startrect", startRect.toUtf8().data() );
276         }
277         return;
278 }
279
280
281 void drawKdenliveTitle( producer_ktitle self, mlt_frame frame, int width, int height, double position, int force_refresh )
282 {
283         // Obtain the producer 
284         mlt_producer producer = &self->parent;
285         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
286
287         // Obtain properties of frame
288         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
289         
290         pthread_mutex_lock( &self->mutex );
291         
292         // Check if user wants us to reload the image
293         if ( force_refresh == 1 || width != self->current_width || height != self->current_height || mlt_properties_get( producer_props, "_endrect" ) != NULL )
294         {
295                 //mlt_cache_item_close( self->image_cache );
296                 self->current_image = NULL;
297                 mlt_properties_set_data( producer_props, "cached_image", NULL, 0, NULL, NULL );
298                 mlt_properties_set_int( producer_props, "force_reload", 0 );
299         }
300         
301         if (self->current_image == NULL) {
302                 // restore QGraphicsScene
303                 QGraphicsScene *scene = static_cast<QGraphicsScene *> (mlt_properties_get_data( producer_props, "qscene", NULL ));
304
305                 if ( force_refresh == 1 && scene )
306                 {
307                         scene = NULL;
308                         mlt_properties_set_data( producer_props, "qscene", NULL, 0, NULL, NULL );
309                 }
310
311                 if ( scene == NULL )
312                 {
313                         int argc = 1;
314                         char* argv[1];
315                         argv[0] = (char*) "xxx";
316                         
317                         // Warning: all Qt graphic objects (QRect, ...) must be initialized AFTER 
318                         // the QApplication is created, otherwise their will be NULL
319                         
320                         if (qApp) {
321                                 app = qApp;
322                         }
323                         else {
324 #ifdef linux
325                                 if ( getenv("DISPLAY") == 0 )
326                                 {
327                                         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" );
328                                         pthread_mutex_unlock( &self->mutex );
329                                         exit(1);
330                                         return;
331                                 }
332 #endif
333                                 app = new QApplication( argc, argv );
334                 //fix to let the decimal point for every locale be: "."
335                 setlocale(LC_NUMERIC,"POSIX");
336                         }
337                         scene = new QGraphicsScene();
338                         scene->setSceneRect(0, 0, mlt_properties_get_int( properties, "width" ), mlt_properties_get_int( properties, "height" ));
339                         loadFromXml( producer, scene, mlt_properties_get( producer_props, "xmldata" ), mlt_properties_get( producer_props, "templatetext" ) );
340                         mlt_properties_set_data( producer_props, "qscene", scene, 0, ( mlt_destructor )qscene_delete, NULL );
341                 }
342                 
343                 QRectF start = stringToRect( QString( mlt_properties_get( producer_props, "_startrect" ) ) );
344                 QRectF end = stringToRect( QString( mlt_properties_get( producer_props, "_endrect" ) ) );
345         
346                 int originalWidth = mlt_properties_get_int( producer_props, "_original_width" );
347                 int originalHeight= mlt_properties_get_int( producer_props, "_original_height" );
348                 const QRectF source( 0, 0, width, height );
349                 if (start.isNull()) {
350                     start = QRectF( 0, 0, originalWidth, originalHeight );
351                 }
352         
353                 //must be extracted from kdenlive title
354                 QImage img( width, height, QImage::Format_ARGB32 );
355                 img.fill( 0 );
356                 QPainter p1;
357                 p1.begin( &img );
358                 p1.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );
359                 //| QPainter::SmoothPixmapTransform );
360                 if (end.isNull())
361                 {
362                         scene->render( &p1, source, start, Qt::IgnoreAspectRatio );
363                 }
364                 else
365                 {
366                         QPointF topleft = start.topLeft() + ( end.topLeft() - start.topLeft() ) * position;
367                         QPointF bottomRight = start.bottomRight() + ( end.bottomRight() - start.bottomRight() ) * position;
368                         const QRectF r1( topleft, bottomRight );
369                         scene->render( &p1, source, r1, Qt::IgnoreAspectRatio );
370                 }
371                 p1.end();
372
373                 int size = width * height * 4;
374                 uint8_t *pointer=img.bits();
375                 QRgb* src = ( QRgb* ) pointer;
376                 self->current_image = ( uint8_t * )mlt_pool_alloc( size );
377                 uint8_t *dst = self->current_image;
378         
379                 for ( int i = 0; i < width * height * 4; i += 4 )
380                 {
381                         *dst++=qRed( *src );
382                         *dst++=qGreen( *src );
383                         *dst++=qBlue( *src );
384                         *dst++=qAlpha( *src );
385                         src++;
386                 }
387
388                 mlt_properties_set_data( producer_props, "cached_image", self->current_image, size, mlt_pool_release, NULL );
389                 self->current_width = width;
390                 self->current_height = height;
391         }
392
393         pthread_mutex_unlock( &self->mutex );
394         mlt_properties_set_int( properties, "width", self->current_width );
395         mlt_properties_set_int( properties, "height", self->current_height );
396 }
397
398