* Author: Niklas P Andersson, niklas.p.andersson@svt.se
*/
-#include "../../stdafx.h"
+#include "../../StdAfx.h"
#include "text_producer.h"
#include <core/frame/geometry.h>
#include <core/frame/frame.h>
#include <core/frame/draw_frame.h>
-
#include <core/frame/frame_factory.h>
#include <core/frame/pixel_format.h>
+#include <core/frame/audio_channel_layout.h>
#include <core/monitor/monitor.h>
-
#include <core/consumer/frame_consumer.h>
+#include <core/module_dependencies.h>
+#include <core/help/help_repository.h>
+#include <core/help/help_sink.h>
+
#include <modules/image/consumer/image_consumer.h>
#include <common/except.h>
#include <common/env.h>
#include <common/future.h>
#include <common/param.h>
-#include <common/os/windows/system_info.h>
#include <memory>
-#include <asmlib.h>
-#include <FreeImage.h>
-
#include <boost/algorithm/string.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/filesystem.hpp>
#include FT_FREETYPE_H
#include FT_GLYPH_H
-#include "utils\texture_atlas.h"
-#include "utils\texture_font.h"
+#include "utils/texture_atlas.h"
+#include "utils/texture_font.h"
+#include "utils/freetype_library.h"
class font_comparer {
const std::wstring& lhs;
};
-namespace caspar { namespace core {
- namespace text {
+namespace caspar { namespace core { namespace text {
- using namespace boost::filesystem3;
+using namespace boost::filesystem;
- std::map<std::wstring, std::wstring> fonts;
+std::map<std::wstring, std::wstring> enumerate_fonts()
+{
+ std::map<std::wstring, std::wstring> result;
- std::map<std::wstring, std::wstring> enumerate_fonts()
+ for(auto iter = directory_iterator(env::font_folder()), end = directory_iterator(); iter != end; ++iter)
+ {
+ try
{
- std::map<std::wstring, std::wstring> result;
-
- FT_Library lib;
- FT_Error err = FT_Init_FreeType(&lib);
- if(err)
- return result;
-
- auto fonts = directory_iterator(env::system_font_folder());
- auto end = directory_iterator();
- for(; fonts != end; ++fonts)
+ auto file = (*iter);
+ if (is_regular_file(file.path()))
{
- auto file = (*fonts);
- if(is_regular_file(file.path()))
+ auto face = get_new_face(u8(file.path().native()));
+ const char* fontname = FT_Get_Postscript_Name(face.get()); //this doesn't work for .fon fonts. Ignoring those for now
+ if (fontname != nullptr)
{
- FT_Face face;
- err = FT_New_Face(lib, u8(file.path().native()).c_str(), 0, &face);
- if(err)
- continue;
-
- const char* fontname = FT_Get_Postscript_Name(face); //this doesn't work for .fon fonts. Ignoring those for now
- if(fontname != nullptr)
- {
- std::string fontname_str(fontname);
- result.insert(std::pair<std::wstring, std::wstring>(std::wstring(fontname_str.begin(), fontname_str.end()), file.path().native()));
- }
-
- FT_Done_Face(face);
+ std::string fontname_str(fontname);
+ result.insert(std::make_pair(u16(fontname_str), u16(file.path().native())));
}
}
+ }
+ catch(...) { }
+ }
- FT_Done_FreeType(lib);
+ return result;
+}
- return result;
- }
+std::vector<std::pair<std::wstring, std::wstring>> list_fonts()
+{
+ auto fonts = enumerate_fonts();
+ return std::vector<std::pair<std::wstring, std::wstring>>(fonts.begin(), fonts.end());
+}
- void init()
- {
- fonts = enumerate_fonts();
- if(!fonts.empty())
- register_producer_factory(&create_text_producer);
- }
+void describe_text_producer(help_sink&, const help_repository&);
+spl::shared_ptr<frame_producer> create_text_producer(const frame_producer_dependencies&, const std::vector<std::wstring>&);
+
+void init(module_dependencies dependencies)
+{
+ dependencies.producer_registry->register_producer_factory(L"Text Producer", create_text_producer, describe_text_producer);
+}
+
+text_info& find_font_file(text_info& info)
+{
+ auto& font_name = info.font;
+ auto fonts = enumerate_fonts();
+ auto it = std::find_if(fonts.begin(), fonts.end(), font_comparer(font_name));
+ info.font_file = (it != fonts.end()) ? (*it).second : L"";
+ return info;
+}
+
+} // namespace text
- text_info& find_font_file(text_info& info)
- {
- auto& font_name = info.font;
- auto it = std::find_if(fonts.begin(), fonts.end(), font_comparer(font_name));
- info.font_file = (it != fonts.end()) ? (*it).second : L"";
- return info;
- }
- }
-
struct text_producer::impl
{
- spl::shared_ptr<core::frame_factory> frame_factory_;
- constraints constraints_;
- int x_, y_, parent_width_, parent_height_;
- bool standalone_;
- variable_impl<std::wstring> text_;
- std::shared_ptr<void> text_subscription_;
- variable_impl<double> tracking_;
- std::shared_ptr<void> tracking_subscription_;
- variable_impl<double> current_bearing_y_;
- variable_impl<double> current_protrude_under_y_;
- draw_frame frame_;
- text::texture_atlas atlas_;
- text::texture_font font_;
- bool dirty_;
+ monitor::subject monitor_subject_;
+ spl::shared_ptr<core::frame_factory> frame_factory_;
+ int x_;
+ int y_;
+ int parent_width_;
+ int parent_height_;
+ bool standalone_;
+ constraints constraints_ { parent_width_, parent_height_ };
+ variable_impl<std::wstring> text_;
+ std::shared_ptr<void> text_subscription_;
+ variable_impl<double> tracking_;
+ variable_impl<double> scale_x_;
+ variable_impl<double> scale_y_;
+ variable_impl<double> shear_;
+ std::shared_ptr<void> tracking_subscription_;
+ variable_impl<double> current_bearing_y_;
+ variable_impl<double> current_protrude_under_y_;
+ draw_frame frame_;
+ text::texture_atlas atlas_ { 1024, 512, 4 };
+ text::texture_font font_;
+ const_frame atlas_frame_;
public:
- explicit impl(const spl::shared_ptr<frame_factory>& frame_factory, int x, int y, const std::wstring& str, text::text_info& text_info, long parent_width, long parent_height, bool standalone)
+ explicit impl(const spl::shared_ptr<frame_factory>& frame_factory, int x, int y, const std::wstring& str, text::text_info& text_info, long parent_width, long parent_height, bool standalone)
: frame_factory_(frame_factory)
- , constraints_(parent_width, parent_height)
- , x_(x), y_(y), parent_width_(parent_width), parent_height_(parent_height)
+ , x_(x), y_(y)
+ , parent_width_(parent_width), parent_height_(parent_height)
, standalone_(standalone)
- , atlas_(512,512,4)
, font_(atlas_, text::find_font_file(text_info), !standalone)
- , dirty_(false)
{
//TODO: examine str to determine which unicode_blocks to load
- font_.load_glyphs(text::Basic_Latin, text_info.color);
- font_.load_glyphs(text::Latin_1_Supplement, text_info.color);
- font_.load_glyphs(text::Latin_Extended_A, text_info.color);
+ font_.load_glyphs(text::unicode_block::Basic_Latin, text_info.color);
+ font_.load_glyphs(text::unicode_block::Latin_1_Supplement, text_info.color);
+ font_.load_glyphs(text::unicode_block::Latin_Extended_A, text_info.color);
+
+ atlas_frame_ = create_atlas_frame();
tracking_.value().set(text_info.tracking);
+ scale_x_.value().set(text_info.scale_x);
+ scale_y_.value().set(text_info.scale_y);
+ shear_.value().set(text_info.shear);
text_subscription_ = text_.value().on_change([this]()
{
- dirty_ = true;
+ generate_frame();
});
tracking_subscription_ = tracking_.value().on_change([this]()
{
- dirty_ = true;
+ generate_frame();
});
constraints_.height.depend_on(text());
CASPAR_LOG(info) << print() << L" Initialized";
}
- void generate_frame()
+ core::const_frame create_atlas_frame() const
{
core::pixel_format_desc pfd(core::pixel_format::bgra);
pfd.planes.push_back(core::pixel_format_desc::plane(static_cast<int>(atlas_.width()), static_cast<int>(atlas_.height()), static_cast<int>(atlas_.depth())));
+ auto frame = frame_factory_->create_frame(this, pfd, core::audio_channel_layout::invalid());
+ memcpy(frame.image_data().data(), atlas_.data(), frame.image_data().size());
+ return std::move(frame);
+ }
+ void generate_frame()
+ {
text::string_metrics metrics;
- font_.set_tracking(static_cast<int>(tracking_.value().get()));
- auto vertex_stream = font_.create_vertex_stream(text_.value().get(), x_, y_, parent_width_, parent_height_, &metrics);
- auto frame = frame_factory_->create_frame(vertex_stream.data(), pfd);
- memcpy(frame.image_data().data(), atlas_.data(), frame.image_data().size());
- frame.set_geometry(frame_geometry(frame_geometry::quad_list, std::move(vertex_stream)));
+ font_.set_tracking(tracking_.value().get());
+
+ auto vertex_stream = font_.create_vertex_stream(text_.value().get(), x_, y_, parent_width_, parent_height_, &metrics, shear_.value().get());
+ auto frame = atlas_frame_.with_geometry(frame_geometry(frame_geometry::geometry_type::quad_list, std::move(vertex_stream)));
- this->constraints_.width.set(metrics.width);
- this->constraints_.height.set(metrics.height);
+ this->constraints_.width.set(metrics.width * this->scale_x_.value().get());
+ this->constraints_.height.set(metrics.height * this->scale_y_.value().get());
current_bearing_y_.value().set(metrics.bearingY);
current_protrude_under_y_.value().set(metrics.protrudeUnderY);
frame_ = core::draw_frame(std::move(frame));
- frame_.transform().image_transform.fill_translation[1] = static_cast<double>(metrics.bearingY) / static_cast<double>(metrics.height);
- }
-
- text::string_metrics measure_string(const std::wstring& str)
- {
- return font_.measure_string(str);
}
// frame_producer
-
+
draw_frame receive_impl()
{
- if (dirty_)
- generate_frame();
-
return frame_;
}
- boost::unique_future<std::wstring> call(const std::vector<std::wstring>& param)
+ std::future<std::wstring> call(const std::vector<std::wstring>& param)
{
std::wstring result;
text_.value().set(param.empty() ? L"" : param[0]);
- return async(launch::deferred, [=]{return result;});
+ return make_ready_future(std::move(result));
}
variable& get_variable(const std::wstring& name)
else if (name == L"tracking")
return tracking_;
- CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(L"text_producer does not have a variable called " + name));
+ CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"text_producer does not have a variable called " + name));
}
const std::vector<std::wstring>& get_variables() const
{
- static std::vector<std::wstring> vars =
- boost::assign::list_of<std::wstring>
- (L"text")
- (L"tracking")
- (L"current_bearing_y")
- (L"current_protrude_under_y");
+ static const std::vector<std::wstring> vars = {
+ L"text",
+ L"tracking",
+ L"current_bearing_y",
+ L"current_protrude_under_y"
+ };
return vars;
}
{
return current_protrude_under_y_.value();
}
-
+
std::wstring print() const
{
return L"text[" + text_.value().get() + L"]";
{
return L"text";
}
-
+
boost::property_tree::wptree info() const
{
boost::property_tree::wptree info;
info.add(L"type", L"text");
info.add(L"text", text_.value().get());
+ info.add(L"font", font_.get_name());
+ info.add(L"size", font_.get_size());
return info;
}
};
{}
draw_frame text_producer::receive_impl() { return impl_->receive_impl(); }
-boost::unique_future<std::wstring> text_producer::call(const std::vector<std::wstring>& param) { return impl_->call(param); }
+std::future<std::wstring> text_producer::call(const std::vector<std::wstring>& param) { return impl_->call(param); }
variable& text_producer::get_variable(const std::wstring& name) { return impl_->get_variable(name); }
const std::vector<std::wstring>& text_producer::get_variables() const { return impl_->get_variables(); }
-text::string_metrics text_producer::measure_string(const std::wstring& str) { return impl_->measure_string(str); }
constraints& text_producer::pixel_constraints() { return impl_->pixel_constraints(); }
std::wstring text_producer::print() const { return impl_->print(); }
std::wstring text_producer::name() const { return impl_->name(); }
boost::property_tree::wptree text_producer::info() const { return impl_->info(); }
-monitor::source& text_producer::monitor_output() { static monitor::subject monitor_subject(""); return monitor_subject; }
+monitor::subject& text_producer::monitor_output() { return impl_->monitor_subject_; }
binding<std::wstring>& text_producer::text() { return impl_->text(); }
binding<double>& text_producer::tracking() { return impl_->tracking(); }
const binding<double>& text_producer::current_bearing_y() const { return impl_->current_bearing_y(); }
{
return spl::make_shared<text_producer>(frame_factory, x, y, str, text_info, parent_width, parent_height, standalone);
}
+namespace text {
+
+void describe_text_producer(help_sink& sink, const help_repository& repo)
+{
+ sink.short_description(L"A producer for rendering dynamic text.");
+ sink.syntax(L"[TEXT] [text:string] {[x:int] [y:int]} {FONT [font:string]|verdana} {SIZE [size:float]|30.0} {COLOR [color:string]|#ffffffff} {STANDALONE [standalone:0,1]|0}");
+ sink.para()
+ ->text(L"Renders dynamic text using fonts found under the ")->code(L"fonts")->text(L" folder. ")
+ ->text(L"Parameters:");
+ sink.definitions()
+ ->item(L"text", L"The text to display. Can be changed later via CALL as well.")
+ ->item(L"x", L"The x position of the text.")
+ ->item(L"y", L"The y position of the text.")
+ ->item(L"font", L"The name of the font (not the actual filename, but the font name).")
+ ->item(L"size", L"The point size.")
+ ->item(L"color", L"The color as an ARGB hex value.")
+ ->item(L"standalone", L"Whether to normalize coordinates or not.");
+ sink.para()->text(L"Examples:");
+ sink.example(L">> PLAY 1-10 [TEXT] \"John Doe\" 0 0 FONT ArialMT SIZE 30 COLOR #1b698d STANDALONE 1");
+ sink.example(L">> CALL 1-10 \"Jane Doe\"", L"for modifying the text while playing.");
+ sink.para()->text(L"See ")->see(L"FLS")->text(L" for listing the available fonts.");
+}
-spl::shared_ptr<frame_producer> create_text_producer(const spl::shared_ptr<frame_factory>& frame_factory, const video_format_desc& format_desc, const std::vector<std::wstring>& params)
+spl::shared_ptr<frame_producer> create_text_producer(const frame_producer_dependencies& dependencies, const std::vector<std::wstring>& params)
{
if(params.size() < 2 || !boost::iequals(params.at(0), L"[text]"))
return core::frame_producer::empty();
text::text_info text_info;
text_info.font = get_param(L"FONT", params, L"verdana");
- text_info.size = static_cast<float>(get_param(L"SIZE", params, 30.0)); // 30.0f does not seem to work to get as float directly
-
+ text_info.size = get_param(L"SIZE", params, 30.0); // 30.0f does not seem to work to get as float directly
+
std::wstring col_str = get_param(L"color", params, L"#ffffffff");
uint32_t col_val = 0xffffffff;
try_get_color(col_str, col_val);
- text_info.color = core::text::color<float>(col_val);
+ text_info.color = core::text::color<double>(col_val);
bool standalone = get_param(L"STANDALONE", params, false);
- return text_producer::create(frame_factory, x, y, params.at(1), text_info, format_desc.width, format_desc.height, standalone);
+ return text_producer::create(
+ dependencies.frame_factory,
+ x, y,
+ params.at(1),
+ text_info,
+ dependencies.format_desc.width, dependencies.format_desc.height,
+ standalone);
}
-}}
+}}}