Loading...
Searching...
No Matches
QtTextReader.cpp
Go to the documentation of this file.
// Copyright (c) 2008-2019 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "QtTextReader.h"
#include "CacheBase.h"
#include "Exceptions.h"
#include "Frame.h"
#include <QImage>
#include <QPainter>
using namespace openshot;
QtTextReader::QtTextReader() : width(1024), height(768), x_offset(0), y_offset(0), text(""), font(QFont("Arial", 10)), text_color("#ffffff"), background_color("#000000"), is_open(false), gravity(GRAVITY_CENTER)
{
// Open and Close the reader, to populate it's attributes (such as height, width, etc...)
Open();
Close();
}
QtTextReader::QtTextReader(int width, int height, int x_offset, int y_offset, GravityType gravity, std::string text, QFont font, std::string text_color, std::string background_color)
: width(width), height(height), x_offset(x_offset), y_offset(y_offset), text(text), font(font), text_color(text_color), background_color(background_color), is_open(false), gravity(gravity)
{
// Open and Close the reader, to populate it's attributes (such as height, width, etc...)
Open();
Close();
}
void QtTextReader::SetTextBackgroundColor(std::string color) {
text_background_color = color;
// Open and Close the reader, to populate it's attributes (such as height, width, etc...) plus the text background color
Open();
Close();
}
// Open reader
void QtTextReader::Open()
{
// Open reader if not already open
if (!is_open)
{
// create image
image = std::make_shared<QImage>(width, height, QImage::Format_RGBA8888_Premultiplied);
image->fill(QColor(background_color.c_str()));
QPainter painter;
if (!painter.begin(image.get())) {
return;
}
// set background
if (!text_background_color.empty()) {
painter.setBackgroundMode(Qt::OpaqueMode);
painter.setBackground(QBrush(text_background_color.c_str()));
}
// set font color
painter.setPen(QPen(text_color.c_str()));
// set font
painter.setFont(font);
// Set gravity (map between OpenShot and Qt)
int align_flag = 0;
switch (gravity)
{
case GRAVITY_TOP_LEFT:
align_flag = Qt::AlignLeft | Qt::AlignTop;
break;
case GRAVITY_TOP:
align_flag = Qt::AlignHCenter | Qt::AlignTop;
break;
case GRAVITY_TOP_RIGHT:
align_flag = Qt::AlignRight | Qt::AlignTop;
break;
case GRAVITY_LEFT:
align_flag = Qt::AlignVCenter | Qt::AlignLeft;
break;
case GRAVITY_CENTER:
align_flag = Qt::AlignCenter;
break;
case GRAVITY_RIGHT:
align_flag = Qt::AlignVCenter | Qt::AlignRight;
break;
case GRAVITY_BOTTOM_LEFT:
align_flag = Qt::AlignLeft | Qt::AlignBottom;
break;
case GRAVITY_BOTTOM:
align_flag = Qt::AlignHCenter | Qt::AlignBottom;
break;
case GRAVITY_BOTTOM_RIGHT:
align_flag = Qt::AlignRight | Qt::AlignBottom;
break;
}
// Draw image
painter.drawText(x_offset, y_offset, width, height, align_flag, text.c_str());
painter.end();
// Update image properties
info.has_audio = false;
info.has_video = true;
info.has_single_image = true;
info.file_size = 0;
info.vcodec = "QImage";
info.width = width;
info.height = height;
info.pixel_ratio.num = 1;
info.pixel_ratio.den = 1;
info.duration = 60 * 60 * 1; // 1 hour duration
info.fps.num = 30;
info.fps.den = 1;
info.video_timebase.num = 1;
info.video_timebase.den = 30;
info.video_length = round(info.duration * info.fps.ToDouble());
// Calculate the DAR (display aspect ratio)
Fraction font_size(info.width * info.pixel_ratio.num, info.height * info.pixel_ratio.den);
// Reduce size fraction
font_size.Reduce();
// Set the ratio based on the reduced fraction
info.display_ratio.num = font_size.num;
info.display_ratio.den = font_size.den;
// Mark as "open"
is_open = true;
}
}
// Close reader
void QtTextReader::Close()
{
// Close all objects, if reader is 'open'
if (is_open)
{
// Mark as "closed"
is_open = false;
// Delete the image
image.reset();
info.vcodec = "";
info.acodec = "";
}
}
// Get an openshot::Frame object for a specific frame number of this reader.
std::shared_ptr<Frame> QtTextReader::GetFrame(int64_t requested_frame)
{
// Create a scoped lock, allowing only a single thread to run the following code at one time
const std::lock_guard<std::recursive_mutex> lock(getFrameMutex);
auto sample_count = Frame::GetSamplesPerFrame(requested_frame, info.fps, info.sample_rate, info.channels);
if (image)
{
// Create or get frame object
auto image_frame = std::make_shared<Frame>(
requested_frame, image->size().width(), image->size().height(),
background_color, sample_count, info.channels);
// Add Image data to frame
image_frame->AddImage(image);
// return frame object
return image_frame;
} else {
// return empty frame
auto image_frame = std::make_shared<Frame>(1, 640, 480, background_color, sample_count, info.channels);
// return frame object
return image_frame;
}
}
// Generate JSON string of this object
std::string QtTextReader::Json() const {
// Return formatted string
return JsonValue().toStyledString();
}
// Generate Json::Value for this object
Json::Value QtTextReader::JsonValue() const {
// Create root json object
Json::Value root = ReaderBase::JsonValue(); // get parent properties
root["type"] = "QtTextReader";
root["width"] = width;
root["height"] = height;
root["x_offset"] = x_offset;
root["y_offset"] = y_offset;
root["text"] = text;
root["font"] = font.toString().toStdString();
root["text_color"] = text_color;
root["background_color"] = background_color;
root["text_background_color"] = text_background_color;
root["gravity"] = gravity;
// return JsonValue
return root;
}
// Load JSON string into this object
void QtTextReader::SetJson(const std::string value) {
// Parse JSON string into JSON objects
try
{
const Json::Value root = openshot::stringToJson(value);
// Set all values that match
SetJsonValue(root);
}
catch (const std::exception& e)
{
// Error parsing JSON (or missing keys)
throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
}
}
// Load Json::Value into this object
void QtTextReader::SetJsonValue(const Json::Value root) {
// Set parent data
ReaderBase::SetJsonValue(root);
// Set data from Json (if key is found)
if (!root["width"].isNull())
width = root["width"].asInt();
if (!root["height"].isNull())
height = root["height"].asInt();
if (!root["x_offset"].isNull())
x_offset = root["x_offset"].asInt();
if (!root["y_offset"].isNull())
y_offset = root["y_offset"].asInt();
if (!root["text"].isNull())
text = root["text"].asString();
if (!root["font"].isNull())
font.fromString(QString::fromStdString(root["font"].asString()));
if (!root["text_color"].isNull())
text_color = root["text_color"].asString();
if (!root["background_color"].isNull())
background_color = root["background_color"].asString();
if (!root["text_background_color"].isNull())
text_background_color = root["text_background_color"].asString();
if (!root["gravity"].isNull())
gravity = (GravityType) root["gravity"].asInt();
// Re-Open path, and re-init everything (if needed)
if (is_open)
{
Close();
Open();
}
}
CacheBase.h
Header file for CacheBase class.
Exceptions.h
Header file for all Exception classes.
Frame.h
Header file for Frame class.
QtTextReader.h
Header file for QtTextReader class.
openshot::Fraction
This class represents a fraction.
Definition Fraction.h:30
openshot::Fraction::num
int num
Numerator for the fraction.
Definition Fraction.h:32
openshot::Fraction::ToDouble
double ToDouble() const
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition Fraction.cpp:40
openshot::Fraction::Reduce
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3)
Definition Fraction.cpp:65
openshot::Fraction::den
int den
Denominator for the fraction.
Definition Fraction.h:33
openshot::Frame::GetSamplesPerFrame
int GetSamplesPerFrame(openshot::Fraction fps, int sample_rate, int channels)
Calculate the # of samples per video frame (for the current frame number)
Definition Frame.cpp:484
openshot::InvalidJSON
Exception for invalid JSON.
Definition Exceptions.h:218
openshot::QtTextReader::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition QtTextReader.cpp:221
openshot::QtTextReader::SetTextBackgroundColor
void SetTextBackgroundColor(std::string color)
Definition QtTextReader.cpp:41
openshot::QtTextReader::Close
void Close() override
Close Reader.
Definition QtTextReader.cpp:147
openshot::QtTextReader::Json
std::string Json() const override
Generate JSON string of this object.
Definition QtTextReader.cpp:193
openshot::QtTextReader::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition QtTextReader.cpp:238
openshot::QtTextReader::QtTextReader
QtTextReader()
Default constructor (blank text)
Definition QtTextReader.cpp:26
openshot::QtTextReader::GetFrame
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame) override
Definition QtTextReader.cpp:164
openshot::QtTextReader::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition QtTextReader.cpp:200
openshot::QtTextReader::Open
void Open() override
Open Reader - which is called by the constructor automatically.
Definition QtTextReader.cpp:50
openshot::ReaderBase::info
openshot::ReaderInfo info
Information about the current media file.
Definition ReaderBase.h:88
openshot::ReaderBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition ReaderBase.cpp:162
openshot::ReaderBase::JsonValue
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition ReaderBase.cpp:107
openshot::ReaderBase::getFrameMutex
std::recursive_mutex getFrameMutex
Mutex for multiple threads.
Definition ReaderBase.h:79
openshot
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
openshot::GravityType
GravityType
This enumeration determines how clips are aligned to their parent container.
Definition Enums.h:22
openshot::GRAVITY_TOP_LEFT
@ GRAVITY_TOP_LEFT
Align clip to the top left of its parent.
Definition Enums.h:23
openshot::GRAVITY_LEFT
@ GRAVITY_LEFT
Align clip to the left of its parent (middle aligned)
Definition Enums.h:26
openshot::GRAVITY_TOP_RIGHT
@ GRAVITY_TOP_RIGHT
Align clip to the top right of its parent.
Definition Enums.h:25
openshot::GRAVITY_RIGHT
@ GRAVITY_RIGHT
Align clip to the right of its parent (middle aligned)
Definition Enums.h:28
openshot::GRAVITY_BOTTOM_LEFT
@ GRAVITY_BOTTOM_LEFT
Align clip to the bottom left of its parent.
Definition Enums.h:29
openshot::GRAVITY_BOTTOM
@ GRAVITY_BOTTOM
Align clip to the bottom center of its parent.
Definition Enums.h:30
openshot::GRAVITY_TOP
@ GRAVITY_TOP
Align clip to the top center of its parent.
Definition Enums.h:24
openshot::GRAVITY_BOTTOM_RIGHT
@ GRAVITY_BOTTOM_RIGHT
Align clip to the bottom right of its parent.
Definition Enums.h:31
openshot::GRAVITY_CENTER
@ GRAVITY_CENTER
Align clip to the center of its parent (middle aligned)
Definition Enums.h:27
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition Json.cpp:16
openshot::ReaderInfo::has_single_image
bool has_single_image
Determines if this file only contains a single image.
Definition ReaderBase.h:42
openshot::ReaderInfo::duration
float duration
Length of time (in seconds)
Definition ReaderBase.h:43
openshot::ReaderInfo::width
int width
The width of the video (in pixesl)
Definition ReaderBase.h:46
openshot::ReaderInfo::channels
int channels
The number of audio channels used in the audio stream.
Definition ReaderBase.h:61
openshot::ReaderInfo::fps
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition ReaderBase.h:48
openshot::ReaderInfo::display_ratio
openshot::Fraction display_ratio
The ratio of width to height of the video stream (i.e. 640x480 has a ratio of 4/3)
Definition ReaderBase.h:51
openshot::ReaderInfo::height
int height
The height of the video (in pixels)
Definition ReaderBase.h:45
openshot::ReaderInfo::video_length
int64_t video_length
The number of frames in the video stream.
Definition ReaderBase.h:53
openshot::ReaderInfo::acodec
std::string acodec
The name of the audio codec used to encode / decode the video stream.
Definition ReaderBase.h:58
openshot::ReaderInfo::vcodec
std::string vcodec
The name of the video codec used to encode / decode the video stream.
Definition ReaderBase.h:52
openshot::ReaderInfo::pixel_ratio
openshot::Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square)
Definition ReaderBase.h:50
openshot::ReaderInfo::has_video
bool has_video
Determines if this file has a video stream.
Definition ReaderBase.h:40
openshot::ReaderInfo::has_audio
bool has_audio
Determines if this file has an audio stream.
Definition ReaderBase.h:41
openshot::ReaderInfo::video_timebase
openshot::Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition ReaderBase.h:55
openshot::ReaderInfo::sample_rate
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
Definition ReaderBase.h:60
openshot::ReaderInfo::file_size
int64_t file_size
Size of file (in bytes)
Definition ReaderBase.h:44