]> git.sesse.net Git - casparcg/blob - core/producer/text/text_producer.cpp
4bcf08519a69a4419c26c8ef73f95a14184ac971
[casparcg] / core / producer / text / text_producer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Niklas P Andersson, niklas.p.andersson@svt.se
20 */
21
22 #include "../../StdAfx.h"
23
24 #include "text_producer.h"
25
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/frame/audio_channel_layout.h>
35 #include <core/monitor/monitor.h>
36 #include <core/consumer/frame_consumer.h>
37 #include <core/module_dependencies.h>
38 #include <core/help/help_repository.h>
39 #include <core/help/help_sink.h>
40
41 #include <modules/image/consumer/image_consumer.h>
42
43 #include <common/except.h>
44 #include <common/array.h>
45 #include <common/env.h>
46 #include <common/future.h>
47 #include <common/param.h>
48 #include <memory>
49
50 #include <boost/algorithm/string.hpp>
51 #include <boost/property_tree/ptree.hpp>
52 #include <boost/filesystem.hpp>
53
54 #include <ft2build.h>
55 #include FT_FREETYPE_H
56 #include FT_GLYPH_H
57
58 #include "utils/texture_atlas.h"
59 #include "utils/texture_font.h"
60 #include "utils/freetype_library.h"
61
62 class font_comparer {
63         const std::wstring& lhs;
64 public:
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); }
67 };
68
69
70 namespace caspar { namespace core { namespace text {
71
72 using namespace boost::filesystem;
73
74 std::map<std::wstring, std::wstring> enumerate_fonts()
75 {
76         std::map<std::wstring, std::wstring> result;
77
78         for(auto iter = directory_iterator(env::font_folder()), end = directory_iterator(); iter != end; ++iter)
79         {
80                 try
81                 {
82                         auto file = (*iter);
83                         if (is_regular_file(file.path()))
84                         {
85                                 auto face = get_new_face(u8(file.path().native()));
86                                 const char* fontname = FT_Get_Postscript_Name(face.get());      //this doesn't work for .fon fonts. Ignoring those for now
87                                 if (fontname != nullptr)
88                                 {
89                                         std::string fontname_str(fontname);
90                                         result.insert(std::make_pair(u16(fontname_str), u16(file.path().native())));
91                                 }
92                         }
93                 }
94                 catch(...) { }
95         }
96
97         return result;
98 }
99
100 std::vector<std::pair<std::wstring, std::wstring>> list_fonts()
101 {
102         auto fonts = enumerate_fonts();
103         return std::vector<std::pair<std::wstring, std::wstring>>(fonts.begin(), fonts.end());
104 }
105
106 void describe_text_producer(help_sink&, const help_repository&);
107 spl::shared_ptr<frame_producer> create_text_producer(const frame_producer_dependencies&, const std::vector<std::wstring>&);
108
109 void init(module_dependencies dependencies)
110 {
111         dependencies.producer_registry->register_producer_factory(L"Text Producer", create_text_producer, describe_text_producer);
112 }
113
114 text_info& find_font_file(text_info& info)
115 {
116         auto& font_name = info.font;
117         auto fonts = enumerate_fonts();
118         auto it = std::find_if(fonts.begin(), fonts.end(), font_comparer(font_name));
119         info.font_file = (it != fonts.end()) ? (*it).second : L"";
120         return info;
121 }
122
123 } // namespace text
124
125
126 struct text_producer::impl
127 {
128         monitor::subject                                                monitor_subject_;
129         spl::shared_ptr<core::frame_factory>    frame_factory_;
130         int                                                                             x_;
131         int                                                                             y_;
132         int                                                                             parent_width_;
133         int                                                                             parent_height_;
134         bool                                                                    standalone_;
135         constraints                                                             constraints_                            { parent_width_, parent_height_ };
136         variable_impl<std::wstring>                             text_;
137         std::shared_ptr<void>                                   text_subscription_;
138         variable_impl<double>                                   tracking_;
139         variable_impl<double>                                   scale_x_;
140         variable_impl<double>                                   scale_y_;
141         variable_impl<double>                                   shear_;
142         std::shared_ptr<void>                                   tracking_subscription_;
143         variable_impl<double>                                   current_bearing_y_;
144         variable_impl<double>                                   current_protrude_under_y_;
145         draw_frame                                                              frame_;
146         text::texture_atlas                                             atlas_                                          { 1024, 512, 4 };
147         text::texture_font                                              font_;
148
149 public:
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                 , x_(x), y_(y)
153                 , parent_width_(parent_width), parent_height_(parent_height)
154                 , standalone_(standalone)
155                 , font_(atlas_, text::find_font_file(text_info), !standalone)
156         {
157                 //TODO: examine str to determine which unicode_blocks to load
158                 font_.load_glyphs(text::unicode_block::Basic_Latin, text_info.color);
159                 font_.load_glyphs(text::unicode_block::Latin_1_Supplement, text_info.color);
160                 font_.load_glyphs(text::unicode_block::Latin_Extended_A, text_info.color);
161
162                 tracking_.value().set(text_info.tracking);
163                 scale_x_.value().set(text_info.scale_x);
164                 scale_y_.value().set(text_info.scale_y);
165                 shear_.value().set(text_info.shear);
166                 text_subscription_ = text_.value().on_change([this]()
167                 {
168                         generate_frame();
169                 });
170                 tracking_subscription_ = tracking_.value().on_change([this]()
171                 {
172                         generate_frame();
173                 });
174
175                 constraints_.height.depend_on(text());
176                 constraints_.width.depend_on(text());
177                 current_bearing_y_.as<double>().depend_on(text());
178                 current_protrude_under_y_.as<double>().depend_on(text());
179
180                 //generate frame
181                 text_.value().set(str);
182
183                 CASPAR_LOG(info) << print() << L" Initialized";
184         }
185
186         void generate_frame()
187         {
188                 core::pixel_format_desc pfd(core::pixel_format::bgra);
189                 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
191                 text::string_metrics metrics;
192                 font_.set_tracking(static_cast<int>(tracking_.value().get()));
193
194                 auto vertex_stream = font_.create_vertex_stream(text_.value().get(), x_, y_, parent_width_, parent_height_, &metrics, shear_.value().get());
195                 auto frame = frame_factory_->create_frame(vertex_stream.data(), pfd, core::audio_channel_layout::invalid());
196                 memcpy(frame.image_data().data(), atlas_.data(), frame.image_data().size());
197                 frame.set_geometry(frame_geometry(frame_geometry::geometry_type::quad_list, std::move(vertex_stream)));
198
199                 this->constraints_.width.set(metrics.width * this->scale_x_.value().get());
200                 this->constraints_.height.set(metrics.height * this->scale_y_.value().get());
201                 current_bearing_y_.value().set(metrics.bearingY);
202                 current_protrude_under_y_.value().set(metrics.protrudeUnderY);
203                 frame_ = core::draw_frame(std::move(frame));
204         }
205
206         text::string_metrics measure_string(const std::wstring& str)
207         {
208                 return font_.measure_string(str);
209         }
210
211         // frame_producer
212
213         draw_frame receive_impl()
214         {
215                 return frame_;
216         }
217
218         std::future<std::wstring> call(const std::vector<std::wstring>& param)
219         {
220                 std::wstring result;
221                 text_.value().set(param.empty() ? L"" : param[0]);
222
223                 return make_ready_future(std::move(result));
224         }
225
226         variable& get_variable(const std::wstring& name)
227         {
228                 if (name == L"text")
229                         return text_;
230                 else if (name == L"current_bearing_y")
231                         return current_bearing_y_;
232                 else if (name == L"current_protrude_under_y")
233                         return current_protrude_under_y_;
234                 else if (name == L"tracking")
235                         return tracking_;
236
237                 CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"text_producer does not have a variable called " + name));
238         }
239
240         const std::vector<std::wstring>& get_variables() const
241         {
242                 static const std::vector<std::wstring> vars = {
243                         L"text",
244                         L"tracking",
245                         L"current_bearing_y",
246                         L"current_protrude_under_y"
247                 };
248
249                 return vars;
250         }
251
252         constraints& pixel_constraints()
253         {
254                 return constraints_;
255         }
256
257         binding<std::wstring>& text()
258         {
259                 return text_.value();
260         }
261
262         binding<double>& tracking()
263         {
264                 return tracking_.value();
265         }
266
267         const binding<double>& current_bearing_y() const
268         {
269                 return current_bearing_y_.value();
270         }
271
272         const binding<double>& current_protrude_under_y() const
273         {
274                 return current_protrude_under_y_.value();
275         }
276
277         std::wstring print() const
278         {
279                 return L"text[" + text_.value().get() + L"]";
280         }
281
282         std::wstring name() const
283         {
284                 return L"text";
285         }
286
287         boost::property_tree::wptree info() const
288         {
289                 boost::property_tree::wptree info;
290                 info.add(L"type", L"text");
291                 info.add(L"text", text_.value().get());
292                 info.add(L"font", font_.get_name());
293                 info.add(L"size", font_.get_size());
294                 return info;
295         }
296 };
297
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))
300 {}
301
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); }
307
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(); }
317
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)
319 {
320         return spl::make_shared<text_producer>(frame_factory, x, y, str, text_info, parent_width, parent_height, standalone);
321 }
322 namespace text {
323
324 void describe_text_producer(help_sink& sink, const help_repository& repo)
325 {
326         sink.short_description(L"A producer for rendering dynamic text.");
327         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}");
328         sink.para()
329                 ->text(L"Renders dynamic text using fonts found under the ")->code(L"fonts")->text(L" folder. ")
330                 ->text(L"Parameters:");
331         sink.definitions()
332                 ->item(L"text", L"The text to display. Can be changed later via CALL as well.")
333                 ->item(L"x", L"The x position of the text.")
334                 ->item(L"y", L"The y position of the text.")
335                 ->item(L"font", L"The name of the font (not the actual filename, but the font name).")
336                 ->item(L"size", L"The point size.")
337                 ->item(L"color", L"The color as an ARGB hex value.")
338                 ->item(L"standalone", L"Whether to normalize coordinates or not.");
339         sink.para()->text(L"Examples:");
340         sink.example(L">> PLAY 1-10 [TEXT] \"John Doe\" 0 0 FONT ArialMT SIZE 30 COLOR #1b698d STANDALONE 1");
341         sink.example(L">> CALL 1-10 \"Jane Doe\"", L"for modifying the text while playing.");
342         sink.para()->text(L"See ")->see(L"FLS")->text(L" for listing the available fonts.");
343 }
344
345 spl::shared_ptr<frame_producer> create_text_producer(const frame_producer_dependencies& dependencies, const std::vector<std::wstring>& params)
346 {
347         if(params.size() < 2 || !boost::iequals(params.at(0), L"[text]"))
348                 return core::frame_producer::empty();
349
350         int x = 0, y = 0;
351         if(params.size() >= 4)
352         {
353                 x = boost::lexical_cast<int>(params.at(2));
354                 y = boost::lexical_cast<int>(params.at(3));
355         }
356
357         text::text_info text_info;
358         text_info.font = get_param(L"FONT", params, L"verdana");
359         text_info.size = get_param(L"SIZE", params, 30.0); // 30.0f does not seem to work to get as float directly
360
361         std::wstring col_str = get_param(L"color", params, L"#ffffffff");
362         uint32_t col_val = 0xffffffff;
363         try_get_color(col_str, col_val);
364         text_info.color = core::text::color<double>(col_val);
365
366         bool standalone = get_param(L"STANDALONE", params, false);
367
368         return text_producer::create(
369                         dependencies.frame_factory,
370                         x, y,
371                         params.at(1),
372                         text_info,
373                         dependencies.format_desc.width, dependencies.format_desc.height,
374                         standalone);
375 }
376
377 }}}