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>
33 #include <core/frame/frame_factory.h>
34 #include <core/frame/pixel_format.h>
35 #include <core/monitor/monitor.h>
37 #include <core/consumer/frame_consumer.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>
45 #include <common/os/windows/system_info.h>
49 #include <FreeImage.h>
51 #include <boost/algorithm/string.hpp>
52 #include <boost/property_tree/ptree.hpp>
53 #include <boost/filesystem.hpp>
56 #include FT_FREETYPE_H
59 #include "utils\texture_atlas.h"
60 #include "utils\texture_font.h"
63 const std::wstring& lhs;
65 explicit font_comparer(const std::wstring& p) : lhs(p) {}
66 bool operator()(const std::pair<std::wstring, std::wstring>&rhs) { return boost::iequals(lhs, rhs.first); }
70 namespace caspar { namespace core {
73 using namespace boost::filesystem;
75 std::map<std::wstring, std::wstring> fonts;
77 std::map<std::wstring, std::wstring> enumerate_fonts()
79 std::map<std::wstring, std::wstring> result;
82 FT_Error err = FT_Init_FreeType(&lib);
86 auto fonts = directory_iterator(env::system_font_folder());
87 auto end = directory_iterator();
88 for(; fonts != end; ++fonts)
91 if(is_regular_file(file.path()))
94 err = FT_New_Face(lib, u8(file.path().native()).c_str(), 0, &face);
98 const char* fontname = FT_Get_Postscript_Name(face); //this doesn't work for .fon fonts. Ignoring those for now
99 if(fontname != nullptr)
101 std::string fontname_str(fontname);
102 result.insert(std::pair<std::wstring, std::wstring>(std::wstring(fontname_str.begin(), fontname_str.end()), file.path().native()));
109 FT_Done_FreeType(lib);
116 fonts = enumerate_fonts();
118 register_producer_factory(&create_text_producer);
121 text_info& find_font_file(text_info& info)
123 auto& font_name = info.font;
124 auto it = std::find_if(fonts.begin(), fonts.end(), font_comparer(font_name));
125 info.font_file = (it != fonts.end()) ? (*it).second : L"";
131 struct text_producer::impl
133 monitor::subject monitor_subject_;
134 spl::shared_ptr<core::frame_factory> frame_factory_;
135 constraints constraints_;
136 int x_, y_, parent_width_, parent_height_;
138 variable_impl<std::wstring> text_;
139 std::shared_ptr<void> text_subscription_;
140 variable_impl<double> tracking_;
141 std::shared_ptr<void> tracking_subscription_;
142 variable_impl<double> current_bearing_y_;
143 variable_impl<double> current_protrude_under_y_;
145 text::texture_atlas atlas_;
146 text::texture_font font_;
150 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)
151 : frame_factory_(frame_factory)
152 , constraints_(parent_width, parent_height)
153 , x_(x), y_(y), parent_width_(parent_width), parent_height_(parent_height)
154 , standalone_(standalone)
156 , font_(atlas_, text::find_font_file(text_info), !standalone)
159 //TODO: examine str to determine which unicode_blocks to load
160 font_.load_glyphs(text::Basic_Latin, text_info.color);
161 font_.load_glyphs(text::Latin_1_Supplement, text_info.color);
162 font_.load_glyphs(text::Latin_Extended_A, text_info.color);
164 tracking_.value().set(text_info.tracking);
165 text_subscription_ = text_.value().on_change([this]()
169 tracking_subscription_ = tracking_.value().on_change([this]()
174 constraints_.height.depend_on(text());
175 constraints_.width.depend_on(text());
176 current_bearing_y_.as<double>().depend_on(text());
177 current_protrude_under_y_.as<double>().depend_on(text());
180 text_.value().set(str);
182 CASPAR_LOG(info) << print() << L" Initialized";
185 void generate_frame()
187 core::pixel_format_desc pfd(core::pixel_format::bgra);
188 pfd.planes.push_back(core::pixel_format_desc::plane(static_cast<int>(atlas_.width()), static_cast<int>(atlas_.height()), static_cast<int>(atlas_.depth())));
190 text::string_metrics metrics;
191 font_.set_tracking(static_cast<int>(tracking_.value().get()));
192 auto vertex_stream = font_.create_vertex_stream(text_.value().get(), x_, y_, parent_width_, parent_height_, &metrics);
193 auto frame = frame_factory_->create_frame(vertex_stream.data(), pfd);
194 memcpy(frame.image_data().data(), atlas_.data(), frame.image_data().size());
195 frame.set_geometry(frame_geometry(frame_geometry::quad_list, std::move(vertex_stream)));
197 this->constraints_.width.set(metrics.width);
198 this->constraints_.height.set(metrics.height);
199 current_bearing_y_.value().set(metrics.bearingY);
200 current_protrude_under_y_.value().set(metrics.protrudeUnderY);
201 frame_ = core::draw_frame(std::move(frame));
202 frame_.transform().image_transform.fill_translation[1] = static_cast<double>(metrics.bearingY) / static_cast<double>(metrics.height);
205 text::string_metrics measure_string(const std::wstring& str)
207 return font_.measure_string(str);
212 draw_frame receive_impl()
220 std::future<std::wstring> call(const std::vector<std::wstring>& param)
223 text_.value().set(param.empty() ? L"" : param[0]);
225 return make_ready_future(std::move(result));
228 variable& get_variable(const std::wstring& name)
232 else if (name == L"current_bearing_y")
233 return current_bearing_y_;
234 else if (name == L"current_protrude_under_y")
235 return current_protrude_under_y_;
236 else if (name == L"tracking")
239 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(L"text_producer does not have a variable called " + name));
242 const std::vector<std::wstring>& get_variables() const
244 static const std::vector<std::wstring> vars = {
247 L"current_bearing_y",
248 L"current_protrude_under_y"
254 constraints& pixel_constraints()
259 binding<std::wstring>& text()
261 return text_.value();
264 binding<double>& tracking()
266 return tracking_.value();
269 const binding<double>& current_bearing_y() const
271 return current_bearing_y_.value();
274 const binding<double>& current_protrude_under_y() const
276 return current_protrude_under_y_.value();
279 std::wstring print() const
281 return L"text[" + text_.value().get() + L"]";
284 std::wstring name() const
289 boost::property_tree::wptree info() const
291 boost::property_tree::wptree info;
292 info.add(L"type", L"text");
293 info.add(L"text", text_.value().get());
298 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)
299 : impl_(new impl(frame_factory, x, y, str, text_info, parent_width, parent_height, standalone))
302 draw_frame text_producer::receive_impl() { return impl_->receive_impl(); }
303 std::future<std::wstring> text_producer::call(const std::vector<std::wstring>& param) { return impl_->call(param); }
304 variable& text_producer::get_variable(const std::wstring& name) { return impl_->get_variable(name); }
305 const std::vector<std::wstring>& text_producer::get_variables() const { return impl_->get_variables(); }
306 text::string_metrics text_producer::measure_string(const std::wstring& str) { return impl_->measure_string(str); }
308 constraints& text_producer::pixel_constraints() { return impl_->pixel_constraints(); }
309 std::wstring text_producer::print() const { return impl_->print(); }
310 std::wstring text_producer::name() const { return impl_->name(); }
311 boost::property_tree::wptree text_producer::info() const { return impl_->info(); }
312 monitor::subject& text_producer::monitor_output() { return impl_->monitor_subject_; }
313 binding<std::wstring>& text_producer::text() { return impl_->text(); }
314 binding<double>& text_producer::tracking() { return impl_->tracking(); }
315 const binding<double>& text_producer::current_bearing_y() const { return impl_->current_bearing_y(); }
316 const binding<double>& text_producer::current_protrude_under_y() const { return impl_->current_protrude_under_y(); }
318 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)
320 return spl::make_shared<text_producer>(frame_factory, x, y, str, text_info, parent_width, parent_height, standalone);
323 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)
325 if(params.size() < 2 || !boost::iequals(params.at(0), L"[text]"))
326 return core::frame_producer::empty();
329 if(params.size() >= 4)
331 x = boost::lexical_cast<int>(params.at(2));
332 y = boost::lexical_cast<int>(params.at(3));
335 text::text_info text_info;
336 text_info.font = get_param(L"FONT", params, L"verdana");
337 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
339 std::wstring col_str = get_param(L"color", params, L"#ffffffff");
340 uint32_t col_val = 0xffffffff;
341 try_get_color(col_str, col_val);
342 text_info.color = core::text::color<float>(col_val);
344 bool standalone = get_param(L"STANDALONE", params, false);
346 return text_producer::create(frame_factory, x, y, params.at(1), text_info, format_desc.width, format_desc.height, standalone);