]> git.sesse.net Git - casparcg/blob - core/producer/text/text_producer.cpp
Merged most recent OSC changes
[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
33 #include <core/frame/frame_factory.h>
34 #include <core/frame/pixel_format.h>
35 #include <core/monitor/monitor.h>
36
37 #include <core/consumer/frame_consumer.h>
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 <common/os/windows/system_info.h>
46 #include <memory>
47
48 #include <asmlib.h>
49 #include <FreeImage.h>
50
51 #include <boost/algorithm/string.hpp>
52 #include <boost/property_tree/ptree.hpp>
53 #include <boost/filesystem.hpp>
54
55 #include <ft2build.h>
56 #include FT_FREETYPE_H
57 #include FT_GLYPH_H
58
59 #include "utils\texture_atlas.h"
60 #include "utils\texture_font.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 {
71         namespace text {
72
73                 using namespace boost::filesystem3;
74
75                 std::map<std::wstring, std::wstring> fonts;
76
77                 std::map<std::wstring, std::wstring> enumerate_fonts()
78                 {
79                         std::map<std::wstring, std::wstring> result;
80
81                         FT_Library lib;
82                         FT_Error err = FT_Init_FreeType(&lib);
83                         if(err) 
84                                 return result;
85
86                         auto fonts = directory_iterator(env::system_font_folder());
87                         auto end = directory_iterator();
88                         for(; fonts != end; ++fonts)
89                         {
90                                 auto file = (*fonts);
91                                 if(is_regular_file(file.path()))
92                                 {
93                                         FT_Face face;
94                                         err = FT_New_Face(lib, u8(file.path().native()).c_str(), 0, &face);
95                                         if(err) 
96                                                 continue;
97
98                                         const char* fontname = FT_Get_Postscript_Name(face);    //this doesn't work for .fon fonts. Ignoring those for now
99                                         if(fontname != nullptr)
100                                         {
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()));
103                                         }
104
105                                         FT_Done_Face(face);
106                                 }
107                         }
108
109                         FT_Done_FreeType(lib);
110
111                         return result;
112                 }
113
114                 void init()
115                 {
116                         fonts = enumerate_fonts();
117                         if(!fonts.empty())
118                                 register_producer_factory(&create_text_producer);
119                 }
120
121                 text_info& find_font_file(text_info& info)
122                 {
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"";
126                         return info;
127                 }
128         }
129         
130
131 struct text_producer::impl
132 {
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_;
137         bool standalone_;
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_;
144         draw_frame frame_;
145         text::texture_atlas atlas_;
146         text::texture_font font_;
147         bool dirty_;
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                 , constraints_(parent_width, parent_height)
153                 , x_(x), y_(y), parent_width_(parent_width), parent_height_(parent_height)
154                 , standalone_(standalone)
155                 , atlas_(512,512,4)
156                 , font_(atlas_, text::find_font_file(text_info), !standalone)
157                 , dirty_(false)
158         {
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);
163
164                 tracking_.value().set(text_info.tracking);
165                 text_subscription_ = text_.value().on_change([this]()
166                 {
167                         dirty_ = true;
168                 });
169                 tracking_subscription_ = tracking_.value().on_change([this]()
170                 {
171                         dirty_ = true;
172                 });
173
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());
178
179                 //generate frame
180                 text_.value().set(str);
181
182                 CASPAR_LOG(info) << print() << L" Initialized";
183         }
184
185         void generate_frame()
186         {
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())));
189
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)));
196
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);
203         }
204
205         text::string_metrics measure_string(const std::wstring& str)
206         {
207                 return font_.measure_string(str);
208         }
209
210         // frame_producer
211                         
212         draw_frame receive_impl()
213         {
214                 if (dirty_)
215                         generate_frame();
216
217                 return frame_;
218         }
219
220         boost::unique_future<std::wstring> call(const std::vector<std::wstring>& param)
221         {
222                 std::wstring result;
223                 text_.value().set(param.empty() ? L"" : param[0]);
224
225                 return async(launch::deferred, [=]{return result;});
226         }
227
228         variable& get_variable(const std::wstring& name)
229         {
230                 if (name == L"text")
231                         return text_;
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")
237                         return tracking_;
238
239                 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(L"text_producer does not have a variable called " + name));
240         }
241
242         const std::vector<std::wstring>& get_variables() const
243         {
244                 static std::vector<std::wstring> vars =
245                                 boost::assign::list_of<std::wstring>
246                                                 (L"text")
247                                                 (L"tracking")
248                                                 (L"current_bearing_y")
249                                                 (L"current_protrude_under_y");
250
251                 return vars;
252         }
253
254         constraints& pixel_constraints()
255         {
256                 return constraints_;
257         }
258
259         binding<std::wstring>& text()
260         {
261                 return text_.value();
262         }
263
264         binding<double>& tracking()
265         {
266                 return tracking_.value();
267         }
268
269         const binding<double>& current_bearing_y() const
270         {
271                 return current_bearing_y_.value();
272         }
273
274         const binding<double>& current_protrude_under_y() const
275         {
276                 return current_protrude_under_y_.value();
277         }
278         
279         std::wstring print() const
280         {
281                 return L"text[" + text_.value().get() + L"]";
282         }
283
284         std::wstring name() const
285         {
286                 return L"text";
287         }
288         
289         boost::property_tree::wptree info() const
290         {
291                 boost::property_tree::wptree info;
292                 info.add(L"type", L"text");
293                 info.add(L"text", text_.value().get());
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 boost::unique_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
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)
324 {
325         if(params.size() < 2 || !boost::iequals(params.at(0), L"[text]"))
326                 return core::frame_producer::empty();
327
328         int x = 0, y = 0;
329         if(params.size() >= 4)
330         {
331                 x = boost::lexical_cast<int>(params.at(2));
332                 y = boost::lexical_cast<int>(params.at(3));
333         }
334
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
338         
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);
343
344         bool standalone = get_param(L"STANDALONE", params, false);
345
346         return text_producer::create(frame_factory, x, y, params.at(1), text_info, format_desc.width, format_desc.height, standalone);
347 }
348
349 }}