summaryrefslogtreecommitdiffstats
path: root/digikam/imageplugins/inserttext/inserttextwidget.cpp
blob: 63f2eb82aa12700b2edb906561d02e3772d51c8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2005-02-14
 * Description : a widget to insert a text over an image.
 *
 * Copyright (C) 2005-2007 by Gilles Caulier <caulier dot gilles at gmail dot com>
 * Copyright (C) 2006-2007 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation;
 * either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * ============================================================ */

// C++ includes.

#include <cstdio>
#include <cmath>

// Qt includes.

#include <qpainter.h>
#include <qfont.h>
#include <qfontmetrics.h>

// KDE includes.

#include <kstandarddirs.h>
#include <kcursor.h>
#include <kglobal.h>

// Digikam includes.

#include "imageiface.h"

// Local includes.

#include "inserttextwidget.h"
#include "inserttextwidget.moc"

namespace DigikamInsertTextImagesPlugin
{

InsertTextWidget::InsertTextWidget(int w, int h, QWidget *parent)
                : QWidget(parent, 0, Qt::WDestructiveClose)
{
    m_currentMoving = false;

    m_iface  = new Digikam::ImageIface(w, h);
    m_data   = m_iface->getPreviewImage();
    m_w      = m_iface->previewWidth();
    m_h      = m_iface->previewHeight();
    m_pixmap = new QPixmap(w, h);
    m_pixmap->fill(colorGroup().background());

    setBackgroundMode(Qt::NoBackground);
    setMinimumSize(w, h);
    setMouseTracking(true);

    m_rect = QRect(width()/2-m_w/2, height()/2-m_h/2, m_w, m_h);
    m_textRect = QRect();

    m_backgroundColor = QColor(0xCC, 0xCC, 0xCC);
    m_transparency    = 210;
}

InsertTextWidget::~InsertTextWidget()
{
    delete [] m_data;
    delete m_iface;
    delete m_pixmap;
}

Digikam::ImageIface* InsertTextWidget::imageIface()
{
    return m_iface;
}

void InsertTextWidget::resetEdit()
{
    // signal this needs to be filled by makePixmap
    m_textRect = QRect();
    makePixmap();
    repaint(false);
}

void InsertTextWidget::setText(QString text, QFont font, QColor color, int alignMode,
                               bool border, bool transparent, int rotation)
{
    m_textString      = text;
    m_textColor       = color;
    m_textBorder      = border;
    m_textTransparent = transparent;
    m_textRotation    = rotation;

    switch (alignMode)
    {
        case ALIGN_LEFT:
            m_alignMode = Qt::AlignLeft;
            break;

        case ALIGN_RIGHT:
            m_alignMode = Qt::AlignRight;
            break;

        case ALIGN_CENTER:
            m_alignMode = Qt::AlignHCenter;
            break;

        case ALIGN_BLOCK:
            m_alignMode = Qt::AlignJustify;
            break;
    }

    // Center text if top left corner text area is not visible.

    /*
    if ( m_textFont.pointSize() != font.pointSize() &&
         !rect().contains( m_textRect.x(), m_textRect.y() ) )
    {
        m_textFont = font;
        resetEdit();
        return;
    }
    */

    m_textFont = font;

    makePixmap();
    repaint(false);
}

void InsertTextWidget::setPositionHint(QRect hint)
{
    // interpreted by composeImage
    m_positionHint = hint;
    if (m_textRect.isValid())
    {
        // invalidate current position so that hint is certainly interpreted
        m_textRect = QRect();
        makePixmap();
        repaint();
    }
}

QRect InsertTextWidget::getPositionHint()
{
    QRect hint;
    if (m_textRect.isValid())
    {
        // We normalize on the size of the image, but we store as int. Precision loss is no problem.
        hint.setX(      (int) ((float)(m_textRect.x() - m_rect.x())     / (float)m_rect.width()  * 10000.0) );
        hint.setY(      (int) ((float)(m_textRect.y() - m_rect.y())     / (float)m_rect.height() * 10000.0) );
        hint.setWidth(  (int) ((float)m_textRect.width()  / (float)m_rect.width()  * 10000.0) );
        hint.setHeight( (int) ((float)m_textRect.height() / (float)m_rect.height() * 10000.0) );
    }
    return hint;
}

Digikam::DImg InsertTextWidget::makeInsertText(void)
{
    int orgW = m_iface->originalWidth();
    int orgH = m_iface->originalHeight();
    float ratioW = (float)orgW/(float)m_w;
    float ratioH = (float)orgH/(float)m_h;

    int x, y;
    if (m_textRect.isValid())
    {
        // convert from widget to image coordinates, then to original size
        x = lroundf( (m_textRect.x() - m_rect.x()) * ratioW);
        y = lroundf( (m_textRect.y() - m_rect.y()) * ratioH);
    }
    else
    {
        x = -1;
        y = -1;
    }

    // Get original image
    Digikam::DImg image = m_iface->getOriginalImg()->copy();

    int borderWidth = QMAX(1, lroundf(ratioW));
    // compose and draw result on image
    composeImage(&image, 0, x, y,
                  m_textFont, m_textFont.pointSizeFloat(),
                  m_textRotation, m_textColor, m_alignMode, m_textString,
                  m_textTransparent, m_backgroundColor,
                  m_textBorder ? BORDER_NORMAL : BORDER_NONE, borderWidth, borderWidth);

    return image;
}

void InsertTextWidget::makePixmap(void)
{
    int orgW = m_iface->originalWidth();
    int orgH = m_iface->originalHeight();
    float ratioW = (float)m_w / (float)orgW;
    float ratioH = (float)m_h / (float)orgH;

    int x, y;
    if (m_textRect.isValid())
    {
        // convert from widget to image coordinates
        x = m_textRect.x() - m_rect.x();
        y = m_textRect.y() - m_rect.y();
    }
    else
    {
        x = -1;
        y = -1;
    }

    // get preview image data
    uchar *data = m_iface->getPreviewImage();
    Digikam::DImg image(m_iface->previewWidth(), m_iface->previewHeight(), m_iface->previewSixteenBit(),
                        m_iface->previewHasAlpha(), data);
    delete [] data;

    // paint pixmap for drawing this widget
    // First, fill with background color
    m_pixmap->fill(colorGroup().background());
    QPainter p(m_pixmap);
    // Convert image to pixmap and draw it
    QPixmap imagePixmap = image.convertToPixmap();
    p.drawPixmap(m_rect.x(), m_rect.y(),
                 imagePixmap, 0, 0, imagePixmap.width(), imagePixmap.height());

    // prepare painter for use by compose image
    p.setClipRect(m_rect);
    p.translate(m_rect.x(), m_rect.y());

    // compose image and draw result directly on pixmap, with correct offset
    QRect textRect = composeImage(&image, &p, x, y,
                                   m_textFont, m_textFont.pointSizeFloat() * ((ratioW > ratioH) ? ratioW : ratioH),
                                   m_textRotation, m_textColor, m_alignMode, m_textString,
                                   m_textTransparent, m_backgroundColor,
                                   m_textBorder ? BORDER_NORMAL : BORDER_SUPPORT, 1, 1);

    p.end();

    // store new text rectangle
    // convert from image to widget coordinates
    m_textRect.setX(textRect.x() + m_rect.x());
    m_textRect.setY(textRect.y() + m_rect.y());
    m_textRect.setSize(textRect.size());
}

/*
   Take data from image, draw text at x|y with specified parameters.
   If destPainter is null, draw to image,
   if destPainter is not null, draw directly using the painter.
   Returns modified area of image.
*/
QRect InsertTextWidget::composeImage(Digikam::DImg *image, QPainter *destPainter,
                                     int x, int y,
                                     QFont font, float pointSize, int textRotation, QColor textColor,
                                     int alignMode, const QString &textString,
                                     bool transparentBackground, QColor backgroundColor,
                                     BorderMode borderMode, int borderWidth, int spacing)
{
    /*
        The problem we have to solve is that we have no pixel access to font rendering,
        we have to let Qt do the drawing. On the other hand we need to support 16 bit, which
        cannot be done with QPixmap.
        The current solution cuts out the text area, lets Qt do its drawing, converts back and blits to original.
    */
    Digikam::DColorComposer *composer = Digikam::DColorComposer::getComposer(Digikam::DColorComposer::PorterDuffNone);

    int maxWidth, maxHeight;
    if (x == -1 && y == -1)
    {
        maxWidth = image->width();
        maxHeight = image->height();
    }
    else
    {
        maxWidth = image->width() - x;
        maxHeight = image->height() - y;
    }

    // find out size of the area that we are drawing to
    font.setPointSizeFloat(pointSize);
    QFontMetrics fontMt( font );
    QRect fontRect = fontMt.boundingRect(0, 0, maxWidth, maxHeight, 0, textString);

    int fontWidth, fontHeight;

    switch(textRotation)
    {
        case ROTATION_NONE:
        case ROTATION_180:
        default:
            fontWidth = fontRect.width();
            fontHeight = fontRect.height();
            break;

        case ROTATION_90:
        case ROTATION_270:
            fontWidth = fontRect.height();
            fontHeight = fontRect.width();
            break;
    }

    // x, y == -1 means that we have to find a good initial position for the text here
    if (x == -1 && y == -1)
    {
        // was a valid position hint stored from last use?
        if (m_positionHint.isValid())
        {
            // We assume that people tend to orient text along the edges,
            // so we do some guessing so that positions such as "in the lower right corner"
            // will be remembered across different image sizes.

            // get relative positions
            float fromTop =          (float)m_positionHint.top()    / 10000.0;
            float fromBottom = 1.0 - (float)m_positionHint.bottom() / 10000.0;
            float fromLeft =         (float)m_positionHint.left()   / 10000.0;
            float fromRight =  1.0 - (float)m_positionHint.right()  / 10000.0;

            // calculate horizontal position
            if (fromLeft < fromRight)
            {
                x = (int)(fromLeft * maxWidth);

                // we are placing from the smaller distance,
                // so if now the larger distance is actually too small,
                // fall back to standard placement, nothing to lose.
                if (x + fontWidth > maxWidth)
                    x = QMAX( (maxWidth - fontWidth) / 2, 0);
            }
            else
            {
                x = maxWidth - (int)(fromRight * maxWidth) - fontWidth;
                if ( x < 0 )
                    x = QMAX( (maxWidth - fontWidth) / 2, 0);
            }

            // calculate vertical position
            if (fromTop < fromBottom)
            {
                y = (int)(fromTop * maxHeight);
                if (y + fontHeight > maxHeight)
                    y = QMAX( (maxHeight - fontHeight) / 2, 0);
            }
            else
            {
                y = maxHeight - (int)(fromBottom * maxHeight) - fontHeight;
                if ( y < 0 )
                    y = QMAX( (maxHeight - fontHeight) / 2, 0);
            }

            if (! QRect(x, y, fontWidth, fontHeight).
                   intersects(QRect(0, 0, maxWidth, maxHeight)) )
            {
                // emergency fallback - nothing is visible
                x = QMAX( (maxWidth - fontWidth) / 2, 0);
                y = QMAX( (maxHeight - fontHeight) / 2, 0);
            }

            // invalidate position hint, use only once
            m_positionHint = QRect();
        }
        else
        {
            // use standard position
            x = QMAX( (maxWidth - fontWidth) / 2, 0);
            y = QMAX( (maxHeight - fontHeight) / 2, 0);
        }
    }

    // create a rectangle relative to image
    QRect drawRect( x, y, fontWidth + 2 * borderWidth + 2 * spacing, fontHeight + 2 * borderWidth  + 2 * spacing);

    // create a rectangle relative to textArea, excluding the border
    QRect textAreaBackgroundRect( borderWidth, borderWidth, fontWidth + 2 * spacing, fontHeight + 2 * spacing);

    // create a rectangle relative to textArea, excluding the border and spacing
    QRect textAreaTextRect( borderWidth + spacing, borderWidth + spacing, fontWidth, fontHeight );

    // create a rectangle relative to textArea, including the border,
    // for drawing the rectangle, taking into account that the width of the QPen goes in and out in equal parts
    QRect textAreaDrawRect( borderWidth / 2, borderWidth / 2, fontWidth + borderWidth + 2 * spacing,
                            fontHeight + borderWidth + 2 * spacing );

    // cut out the text area
    Digikam::DImg textArea = image->copy(drawRect);

    if (textArea.isNull())
        return QRect();

    // compose semi-transparent background over textArea
    if (transparentBackground)
    {
        Digikam::DImg transparentLayer(textAreaBackgroundRect.width(), textAreaBackgroundRect.height(), textArea.sixteenBit(), true);
        Digikam::DColor transparent(backgroundColor);
        transparent.setAlpha(m_transparency);
        if (image->sixteenBit())
            transparent.convertToSixteenBit();
        transparentLayer.fill(transparent);
        textArea.bitBlendImage(composer, &transparentLayer, 0, 0, transparentLayer.width(), transparentLayer.height(),
                               textAreaBackgroundRect.x(), textAreaBackgroundRect.y());
    }

    Digikam::DImg textNotDrawn;
    if (textArea.sixteenBit())
    {
        textNotDrawn = textArea.copy();
        textNotDrawn.convertToEightBit();
    }
    else
        textNotDrawn = textArea;

    // We have no direct pixel access to font rendering, so now we need to use Qt/X11 for the drawing

    // convert text area to pixmap
    QPixmap pixmap = textNotDrawn.convertToPixmap();
    // paint on pixmap
    QPainter p(&pixmap);
    p.setPen( QPen(textColor, 1) ) ;
    p.setFont( font );
    p.save();

    // translate to origin of text, leaving space for the border
    p.translate(textAreaTextRect.x(), textAreaTextRect.y());

    switch(textRotation)
    {
        case ROTATION_NONE:
            p.drawText( 0, 0, textAreaTextRect.width(),
                        textAreaTextRect.height(), alignMode, textString );
            break;
        case ROTATION_90:
            p.translate(textAreaTextRect.width(), 0);
            p.rotate(90.0);
            p.drawText( 0, 0, textAreaTextRect.height(), textAreaTextRect.width(),
                        alignMode, textString );
            break;
        case ROTATION_180:
            p.translate(textAreaTextRect.width(), textAreaTextRect.height());
            p.rotate(180.0);
            p.drawText( 0, 0, textAreaTextRect.width(), textAreaTextRect.height(),
                        alignMode, textString );
            break;
        case ROTATION_270:
            p.translate(0, textAreaTextRect.height());
            p.rotate(270.0);
            p.drawText( 0, 0, textAreaTextRect.height(), textAreaTextRect.width(),
                        alignMode, textString );
            break;
    }

    p.restore();

    // Drawing rectangle around text.

    if (borderMode == BORDER_NORMAL)      // Decorative border using text color.
    {
        p.setPen( QPen(textColor, borderWidth, Qt::SolidLine,
                  Qt::SquareCap, Qt::RoundJoin) ) ;
        p.drawRect(textAreaDrawRect);
    }
    else if (borderMode == BORDER_SUPPORT)  // Make simple dot line border to help user.
    {
        p.setPen(QPen(Qt::white, 1, Qt::SolidLine));
        p.drawRect(textAreaDrawRect);
        p.setPen(QPen(Qt::red, 1, Qt::DotLine));
        p.drawRect(textAreaDrawRect);
    }
    p.end();

    if (!destPainter)
    {
        // convert to QImage, then to DImg
        QImage pixmapImage = pixmap.convertToImage();
        Digikam::DImg textDrawn(pixmapImage.width(), pixmapImage.height(), false, true, pixmapImage.bits());

        // This does not work: during the conversion, colors are altered significantly (diffs of 1 to 10 in each component),
        // so we cannot find out which pixels have actually been touched.
        /*
        // Compare the result of drawing with the previous version.
        // Set all unchanged pixels to transparent
        Digikam::DColor color, ncolor;
        uchar *ptr, *nptr;
        ptr = textDrawn.bits();
        nptr = textNotDrawn.bits();
        int bytesDepth = textDrawn.bytesDepth();
        int numPixels = textDrawn.width() * textDrawn.height();
        for (int i = 0; i < numPixels; i++, ptr+= bytesDepth, nptr += bytesDepth)
        {
            color.setColor(ptr, false);
            ncolor.setColor(nptr, false);
            if ( color.red()   == ncolor.red() &&
                color.green() == ncolor.green() &&
                color.blue()  == ncolor.blue())
            {
                color.setAlpha(0);
                color.setPixel(ptr);
            }
        }
        // convert to 16 bit if needed
        */
        textDrawn.convertToDepthOfImage(&textArea);

        // now compose to original: only pixels affected by drawing text and border are changed, not whole area
        textArea.bitBlendImage(composer, &textDrawn, 0, 0, textDrawn.width(), textDrawn.height(), 0, 0);

        // copy result to original image
        image->bitBltImage(&textArea, drawRect.x(), drawRect.y());
    }
    else
    {
        destPainter->drawPixmap(drawRect.x(), drawRect.y(), pixmap, 0, 0, pixmap.width(), pixmap.height());
    }

    delete composer;

    return drawRect;
}

void InsertTextWidget::paintEvent( QPaintEvent * )
{
    bitBlt(this, 0, 0, m_pixmap);
}

void InsertTextWidget::resizeEvent(QResizeEvent * e)
{
    blockSignals(true);
    delete m_pixmap;

    int w = e->size().width();
    int h = e->size().height();

    int textX = m_textRect.x() - m_rect.x();
    int textY = m_textRect.y() - m_rect.y();
    int old_w = m_w;
    int old_h = m_h;
    m_data    = m_iface->setPreviewImageSize(w, h);
    m_w       = m_iface->previewWidth();
    m_h       = m_iface->previewHeight();

    m_pixmap = new QPixmap(w, h);
    m_rect = QRect(w/2-m_w/2, h/2-m_h/2, m_w, m_h);

    if (m_textRect.isValid())
    {
        int textWidth  = m_textRect.width();
        int textHeight = m_textRect.height();

        textX = lroundf( textX * (float)m_w / (float)old_w );
        textY = lroundf( textY * (float)m_h / (float)old_h );
        textWidth  = lroundf(textWidth  * (float)m_w / (float)old_w );
        textHeight = lroundf(textHeight * (float)m_h / (float)old_h );

        m_textRect.setX(textX + m_rect.x());
        m_textRect.setY(textY + m_rect.y());
        m_textRect.setWidth(textWidth);
        m_textRect.setHeight(textHeight);
        makePixmap();
    }

    blockSignals(false);
}

void InsertTextWidget::mousePressEvent ( QMouseEvent * e )
{
    if ( e->button() == Qt::LeftButton &&
         m_textRect.contains( e->x(), e->y() ) )
    {
        m_xpos = e->x();
        m_ypos = e->y();
        setCursor ( KCursor::sizeAllCursor() );
        m_currentMoving = true;
    }
}

void InsertTextWidget::mouseReleaseEvent ( QMouseEvent * )
{
    setCursor ( KCursor::arrowCursor() );
    m_currentMoving = false;
}

void InsertTextWidget::mouseMoveEvent ( QMouseEvent * e )
{
    if ( rect().contains( e->x(), e->y() ) )
    {
        if ( e->state() == Qt::LeftButton && m_currentMoving )
        {
            uint newxpos = e->x();
            uint newypos = e->y();

            m_textRect.moveBy(newxpos - m_xpos, newypos - m_ypos);

            makePixmap();
            repaint(false);

            m_xpos = newxpos;
            m_ypos = newypos;
            setCursor( KCursor::handCursor() );
        }
        else if ( m_textRect.contains( e->x(), e->y() ) )
        {
            setCursor ( KCursor::sizeAllCursor() );
        }
        else
        {
            setCursor ( KCursor::arrowCursor() );
        }
    }
}

}  // NameSpace DigikamInsertTextImagesPlugin