]> git.sesse.net Git - casparcg/blob - core/producer/text/text_producer.cpp
* Refactored so that frame_producers are stored in a frame_producer_registry instance...
[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/monitor/monitor.h>
35 #include <core/consumer/frame_consumer.h>
36 #include <core/module_dependencies.h>
37
38 #include <modules/image/consumer/image_consumer.h>
39
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 <memory>
46
47 #include <boost/algorithm/string.hpp>
48 #include <boost/property_tree/ptree.hpp>
49 #include <boost/filesystem.hpp>
50
51 #include <ft2build.h>
52 #include FT_FREETYPE_H
53 #include FT_GLYPH_H
54
55 #include "utils/texture_atlas.h"
56 #include "utils/texture_font.h"
57
58 class font_comparer {
59         const std::wstring& lhs;
60 public:
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); }
63 };
64
65
66 namespace caspar { namespace core {
67         namespace text {
68
69                 using namespace boost::filesystem;
70
71                 std::map<std::wstring, std::wstring> fonts;
72
73                 std::map<std::wstring, std::wstring> enumerate_fonts()
74                 {
75                         std::map<std::wstring, std::wstring> result;
76
77                         FT_Library lib;
78                         FT_Error err = FT_Init_FreeType(&lib);
79                         if(err) 
80                                 return result;
81
82                         auto fonts = directory_iterator(env::font_folder());
83                         auto end = directory_iterator();
84                         for(; fonts != end; ++fonts)
85                         {
86                                 auto file = (*fonts);
87                                 if(is_regular_file(file.path()))
88                                 {
89                                         FT_Face face;
90                                         err = FT_New_Face(lib, u8(file.path().native()).c_str(), 0, &face);
91                                         if(err) 
92                                                 continue;
93
94                                         const char* fontname = FT_Get_Postscript_Name(face);    //this doesn't work for .fon fonts. Ignoring those for now
95                                         if(fontname != nullptr)
96                                         {
97                                                 std::string fontname_str(fontname);
98                                                 result.insert(std::make_pair(u16(fontname_str), u16(file.path().native())));
99                                         }
100
101                                         FT_Done_Face(face);
102                                 }
103                         }
104
105                         FT_Done_FreeType(lib);
106
107                         return result;
108                 }
109
110                 void init(module_dependencies dependencies)
111                 {
112                         fonts = enumerate_fonts();
113                         dependencies.producer_registry->register_producer_factory(create_text_producer);
114                 }
115
116                 text_info& find_font_file(text_info& info)
117                 {
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"";
121                         return info;
122                 }
123         }
124         
125
126 struct text_producer::impl
127 {
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_;
132         bool standalone_;
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_;
139         draw_frame frame_;
140         text::texture_atlas atlas_                                                              { 512, 512, 4 };
141         text::texture_font font_;
142         bool dirty_                                                                                             = false;
143
144 public:
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)
151         {
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);
156
157                 tracking_.value().set(text_info.tracking);
158                 text_subscription_ = text_.value().on_change([this]()
159                 {
160                         dirty_ = true;
161                 });
162                 tracking_subscription_ = tracking_.value().on_change([this]()
163                 {
164                         dirty_ = true;
165                 });
166
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());
171
172                 //generate frame
173                 text_.value().set(str);
174
175                 CASPAR_LOG(info) << print() << L" Initialized";
176         }
177
178         void generate_frame()
179         {
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())));
182
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)));
189
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));
195         }
196
197         text::string_metrics measure_string(const std::wstring& str)
198         {
199                 return font_.measure_string(str);
200         }
201
202         // frame_producer
203                         
204         draw_frame receive_impl()
205         {
206                 if (dirty_)
207                 {
208                         generate_frame();
209                         dirty_ = false;
210                 }
211
212                 return frame_;
213         }
214
215         std::future<std::wstring> call(const std::vector<std::wstring>& param)
216         {
217                 std::wstring result;
218                 text_.value().set(param.empty() ? L"" : param[0]);
219
220                 return make_ready_future(std::move(result));
221         }
222
223         variable& get_variable(const std::wstring& name)
224         {
225                 if (name == L"text")
226                         return text_;
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")
232                         return tracking_;
233
234                 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(L"text_producer does not have a variable called " + name));
235         }
236
237         const std::vector<std::wstring>& get_variables() const
238         {
239                 static const std::vector<std::wstring> vars = {
240                         L"text",
241                         L"tracking",
242                         L"current_bearing_y",
243                         L"current_protrude_under_y"
244                 };
245
246                 return vars;
247         }
248
249         constraints& pixel_constraints()
250         {
251                 return constraints_;
252         }
253
254         binding<std::wstring>& text()
255         {
256                 return text_.value();
257         }
258
259         binding<double>& tracking()
260         {
261                 return tracking_.value();
262         }
263
264         const binding<double>& current_bearing_y() const
265         {
266                 return current_bearing_y_.value();
267         }
268
269         const binding<double>& current_protrude_under_y() const
270         {
271                 return current_protrude_under_y_.value();
272         }
273         
274         std::wstring print() const
275         {
276                 return L"text[" + text_.value().get() + L"]";
277         }
278
279         std::wstring name() const
280         {
281                 return L"text";
282         }
283         
284         boost::property_tree::wptree info() const
285         {
286                 boost::property_tree::wptree info;
287                 info.add(L"type", L"text");
288                 info.add(L"text", text_.value().get());
289                 return info;
290         }
291 };
292
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))
295 {}
296
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); }
302
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(); }
312
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)
314 {
315         return spl::make_shared<text_producer>(frame_factory, x, y, str, text_info, parent_width, parent_height, standalone);
316 }
317
318 spl::shared_ptr<frame_producer> create_text_producer(const frame_producer_dependencies& dependencies, const std::vector<std::wstring>& params)
319 {
320         if(params.size() < 2 || !boost::iequals(params.at(0), L"[text]"))
321                 return core::frame_producer::empty();
322
323         int x = 0, y = 0;
324         if(params.size() >= 4)
325         {
326                 x = boost::lexical_cast<int>(params.at(2));
327                 y = boost::lexical_cast<int>(params.at(3));
328         }
329
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
333         
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);
338
339         bool standalone = get_param(L"STANDALONE", params, false);
340
341         return text_producer::create(
342                         dependencies.frame_factory,
343                         x, y,
344                         params.at(1),
345                         text_info,
346                         dependencies.format_desc.width, dependencies.format_desc.height,
347                         standalone);
348 }
349
350 }}