Open reader
| 42 | |
| 43 | // Open reader |
| 44 | void QtHtmlReader::Open() |
| 45 | { |
| 46 | // Open reader if not already open |
| 47 | if (!is_open) |
| 48 | { |
| 49 | // create image |
| 50 | image = std::make_shared<QImage>(width, height, QImage::Format_RGBA8888_Premultiplied); |
| 51 | image->fill(QColor(background_color.c_str())); |
| 52 | |
| 53 | //start painting |
| 54 | QPainter painter; |
| 55 | if (!painter.begin(image.get())) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | //set background |
| 60 | painter.setBackground(QBrush(background_color.c_str())); |
| 61 | |
| 62 | //draw text |
| 63 | QTextDocument text_document; |
| 64 | |
| 65 | //disable redo/undo stack as not needed |
| 66 | text_document.setUndoRedoEnabled(false); |
| 67 | |
| 68 | //create the HTML/CSS document |
| 69 | text_document.setTextWidth(width); |
| 70 | text_document.setDefaultStyleSheet(css.c_str()); |
| 71 | text_document.setHtml(html.c_str()); |
| 72 | |
| 73 | int td_height = text_document.documentLayout()->documentSize().height(); |
| 74 | |
| 75 | if (gravity == GRAVITY_TOP_LEFT || gravity == GRAVITY_TOP || gravity == GRAVITY_TOP_RIGHT) { |
| 76 | painter.translate(x_offset, y_offset); |
| 77 | } else if (gravity == GRAVITY_LEFT || gravity == GRAVITY_CENTER || gravity == GRAVITY_RIGHT) { |
| 78 | painter.translate(x_offset, (height - td_height) / 2 + y_offset); |
| 79 | } else if (gravity == GRAVITY_BOTTOM_LEFT || gravity == GRAVITY_BOTTOM_RIGHT || gravity == GRAVITY_BOTTOM) { |
| 80 | painter.translate(x_offset, height - td_height + y_offset); |
| 81 | } |
| 82 | |
| 83 | if (gravity == GRAVITY_TOP_LEFT || gravity == GRAVITY_LEFT || gravity == GRAVITY_BOTTOM_LEFT) { |
| 84 | text_document.setDefaultTextOption(QTextOption(Qt::AlignLeft)); |
| 85 | } else if (gravity == GRAVITY_CENTER || gravity == GRAVITY_TOP || gravity == GRAVITY_BOTTOM) { |
| 86 | text_document.setDefaultTextOption(QTextOption(Qt::AlignHCenter)); |
| 87 | } else if (gravity == GRAVITY_TOP_RIGHT || gravity == GRAVITY_RIGHT|| gravity == GRAVITY_BOTTOM_RIGHT) { |
| 88 | text_document.setDefaultTextOption(QTextOption(Qt::AlignRight)); |
| 89 | } |
| 90 | |
| 91 | // Draw image |
| 92 | text_document.drawContents(&painter); |
| 93 | |
| 94 | painter.end(); |
| 95 | |
| 96 | // Update image properties |
| 97 | info.has_audio = false; |
| 98 | info.has_video = true; |
| 99 | info.has_single_image = true; |
| 100 | info.file_size = 0; |
| 101 | info.vcodec = "QImage"; |