2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Niklas P Andersson, niklas.p.andersson@svt.se
22 #include "../../StdAfx.h"
24 #include "text_producer.h"
26 #include <core/producer/frame_producer.h>
27 #include <core/producer/color/color_producer.h>
28 #include <core/producer/variable.h>
29 #include <core/frame/geometry.h>
30 #include <core/frame/frame.h>
31 #include <core/frame/draw_frame.h>
32 #include <core/frame/frame_factory.h>
33 #include <core/frame/pixel_format.h>
34 #include <core/monitor/monitor.h>
35 #include <core/consumer/frame_consumer.h>
36 #include <core/module_dependencies.h>
38 #include <modules/image/consumer/image_consumer.h>
40 #include <common/except.h>
41 #include <common/array.h>
42 #include <common/env.h>
43 #include <common/future.h>
44 #include <common/param.h>
47 #include <boost/algorithm/string.hpp>
48 #include <boost/property_tree/ptree.hpp>
49 #include <boost/filesystem.hpp>
52 #include FT_FREETYPE_H
55 #include "utils/texture_atlas.h"
56 #include "utils/texture_font.h"
59 const std::wstring& lhs;
61 explicit font_comparer(const std::wstring& p) : lhs(p) {}
62 bool operator()(const std::pair<std::wstring, std::wstring>&rhs) { return boost::iequals(lhs, rhs.first); }
66 namespace caspar { namespace core {
69 using namespace boost::filesystem;
71 std::map<std::wstring, std::wstring> fonts;
73 std::map<std::wstring, std::wstring> enumerate_fonts()
75 std::map<std::wstring, std::wstring> result;
78 FT_Error err = FT_Init_FreeType(&lib);
82 auto fonts = directory_iterator(env::font_folder());
83 auto end = directory_iterator();
84 for(; fonts != end; ++fonts)
87 if(is_regular_file(file.path()))
90 err = FT_New_Face(lib, u8(file.path().native()).c_str(), 0, &face);
94 const char* fontname = FT_Get_Postscript_Name(face); //this doesn't work for .fon fonts. Ignoring those for now
95 if(fontname != nullptr)
97 std::string fontname_str(fontname);
98 result.insert(std::make_pair(u16(fontname_str), u16(file.path().native())));
105 FT_Done_FreeType(lib);
110 void init(module_dependencies dependencies)
112 fonts = enumerate_fonts();
113 dependencies.producer_registry->register_producer_factory(create_text_producer);
116 text_info& find_font_file(text_info& info)
118 auto& font_name = info.font;
119 auto it = std::find_if(fonts.begin(), fonts.end(), font_comparer(font_name));
120 info.font_file = (it != fonts.end()) ? (*it).second : L"";
126 struct text_producer::impl
128 monitor::subject monitor_subject_;
129 spl::shared_ptr<core::frame_factory> frame_factory_;
130 constraints constraints_;
131 int x_, y_, parent_width_, parent_height_;
133 variable_impl<std::wstring> text_;
134 std::shared_ptr<void> text_subscription_;
135 variable_impl<double> tracking_;
136 std::shared_ptr<void> tracking_subscription_;
137 variable_impl<double> current_bearing_y_;
138 variable_impl<double> current_protrude_under_y_;
140 text::texture_atlas atlas_ { 512, 512, 4 };
141 text::texture_font font_;
145 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)
146 : frame_factory_(frame_factory)
147 , constraints_(parent_width, parent_height)
148 , x_(x), y_(y), parent_width_(parent_width), parent_height_(parent_height)
149 , standalone_(standalone)
150 , font_(atlas_, text::find_font_file(text_info), !standalone)
152 //TODO: examine str to determine which unicode_blocks to load
153 font_.load_glyphs(text::unicode_block::Basic_Latin, text_info.color);
154 font_.load_glyphs(text::unicode_block::Latin_1_Supplement, text_info.color);
155 font_.load_glyphs(text::unicode_block::Latin_Extended_A, text_info.color);
157 tracking_.value().set(text_info.tracking);
158 text_subscription_ = text_.value().on_change([this]()
162 tracking_subscription_ = tracking_.value().on_change([this]()
167 constraints_.height.depend_on(text());
168 constraints_.width.depend_on(text());
169 current_bearing_y_.as<double>().depend_on(text());
170 current_protrude_under_y_.as<double>().depend_on(text());
173 text_.value().set(str);
175 CASPAR_LOG(info) << print() << L" Initialized";
178 void generate_frame()
180 core::pixel_format_desc pfd(core::pixel_format::bgra);
181 pfd.planes.push_back(core::pixel_format_desc::plane(static_cast<int>(atlas_.width()), static_cast<int>(atlas_.height()), static_cast<int>(atlas_.depth())));
183 text::string_metrics metrics;
184 font_.set_tracking(static_cast<int>(tracking_.value().get()));
185 auto vertex_stream = font_.create_vertex_stream(text_.value().get(), x_, y_, parent_width_, parent_height_, &metrics);
186 auto frame = frame_factory_->create_frame(vertex_stream.data(), pfd);
187 memcpy(frame.image_data().data(), atlas_.data(), frame.image_data().size());
188 frame.set_geometry(frame_geometry(frame_geometry::geometry_type::quad_list, std::move(vertex_stream)));
190 this->constraints_.width.set(metrics.width);
191 this->constraints_.height.set(metrics.height);
192 current_bearing_y_.value().set(metrics.bearingY);
193 current_protrude_under_y_.value().set(metrics.protrudeUnderY);
194 frame_ = core::draw_frame(std::move(frame));
197 text::string_metrics measure_string(const std::wstring& str)
199 return font_.measure_string(str);
204 draw_frame receive_impl()
215 std::future<std::wstring> call(const std::vector<std::wstring>& param)
218 text_.value().set(param.empty() ? L"" : param[0]);
220 return make_ready_future(std::move(result));
223 variable& get_variable(const std::wstring& name)
227 else if (name == L"current_bearing_y")
228 return current_bearing_y_;
229 else if (name == L"current_protrude_under_y")
230 return current_protrude_under_y_;
231 else if (name == L"tracking")
234 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(L"text_producer does not have a variable called " + name));
237 const std::vector<std::wstring>& get_variables() const
239 static const std::vector<std::wstring> vars = {
242 L"current_bearing_y",
243 L"current_protrude_under_y"
249 constraints& pixel_constraints()
254 binding<std::wstring>& text()
256 return text_.value();
259 binding<double>& tracking()
261 return tracking_.value();
264 const binding<double>& current_bearing_y() const
266 return current_bearing_y_.value();
269 const binding<double>& current_protrude_under_y() const
271 return current_protrude_under_y_.value();
274 std::wstring print() const
276 return L"text[" + text_.value().get() + L"]";
279 std::wstring name() const
284 boost::property_tree::wptree info() const
286 boost::property_tree::wptree info;
287 info.add(L"type", L"text");
288 info.add(L"text", text_.value().get());
293 text_producer::text_producer(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)
294 : impl_(new impl(frame_factory, x, y, str, text_info, parent_width, parent_height, standalone))
297 draw_frame text_producer::receive_impl() { return impl_->receive_impl(); }
298 std::future<std::wstring> text_producer::call(const std::vector<std::wstring>& param) { return impl_->call(param); }
299 variable& text_producer::get_variable(const std::wstring& name) { return impl_->get_variable(name); }
300 const std::vector<std::wstring>& text_producer::get_variables() const { return impl_->get_variables(); }
301 text::string_metrics text_producer::measure_string(const std::wstring& str) { return impl_->measure_string(str); }
303 constraints& text_producer::pixel_constraints() { return impl_->pixel_constraints(); }
304 std::wstring text_producer::print() const { return impl_->print(); }
305 std::wstring text_producer::name() const { return impl_->name(); }
306 boost::property_tree::wptree text_producer::info() const { return impl_->info(); }
307 monitor::subject& text_producer::monitor_output() { return impl_->monitor_subject_; }
308 binding<std::wstring>& text_producer::text() { return impl_->text(); }
309 binding<double>& text_producer::tracking() { return impl_->tracking(); }
310 const binding<double>& text_producer::current_bearing_y() const { return impl_->current_bearing_y(); }
311 const binding<double>& text_producer::current_protrude_under_y() const { return impl_->current_protrude_under_y(); }
313 spl::shared_ptr<text_producer> text_producer::create(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)
315 return spl::make_shared<text_producer>(frame_factory, x, y, str, text_info, parent_width, parent_height, standalone);
318 spl::shared_ptr<frame_producer> create_text_producer(const frame_producer_dependencies& dependencies, const std::vector<std::wstring>& params)
320 if(params.size() < 2 || !boost::iequals(params.at(0), L"[text]"))
321 return core::frame_producer::empty();
324 if(params.size() >= 4)
326 x = boost::lexical_cast<int>(params.at(2));
327 y = boost::lexical_cast<int>(params.at(3));
330 text::text_info text_info;
331 text_info.font = get_param(L"FONT", params, L"verdana");
332 text_info.size = get_param(L"SIZE", params, 30.0); // 30.0f does not seem to work to get as float directly
334 std::wstring col_str = get_param(L"color", params, L"#ffffffff");
335 uint32_t col_val = 0xffffffff;
336 try_get_color(col_str, col_val);
337 text_info.color = core::text::color<double>(col_val);
339 bool standalone = get_param(L"STANDALONE", params, false);
341 return text_producer::create(
342 dependencies.frame_factory,
346 dependencies.format_desc.width, dependencies.format_desc.height,