]> git.sesse.net Git - casparcg/blob - core/producer/text/text_producer.cpp
Merge branch '2.1.0' of https://github.com/CasparCG/Server into 2.1.0
[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/frame/geometry.h>
29 #include <core/frame/frame.h>
30 #include <core/frame/draw_frame.h>
31
32 #include <core/frame/frame_factory.h>
33 #include <core/frame/pixel_format.h>
34 #include <core/monitor/monitor.h>
35
36 #include <core/consumer/frame_consumer.h>
37 #include <modules/image/consumer/image_consumer.h>
38
39 #include <common/except.h>
40 #include <common/array.h>
41 #include <common/env.h>
42 #include <common/future.h>
43 #include <common/param.h>
44 #include <common/os/windows/system_info.h>
45 #include <memory>
46
47 #include <asmlib.h>
48 #include <FreeImage.h>
49
50 #include <boost/algorithm/string.hpp>
51 #include <boost/property_tree/ptree.hpp>
52 #include <boost/filesystem.hpp>
53
54 #include "utils\texture_atlas.h"
55 #include "utils\texture_font.h"
56
57 class font_comparer {
58         const std::wstring& lhs;
59 public:
60         explicit font_comparer(const std::wstring& p) : lhs(p) {}
61         bool operator()(const std::pair<std::wstring, std::wstring>&rhs) { return boost::iequals(lhs, rhs.first); }
62 };
63
64
65 namespace caspar { namespace core {
66         namespace text {
67                 
68                 std::map<std::wstring, std::wstring> fonts;
69                 
70                 void init()
71                 {
72                         fonts.swap(std::move(caspar::enumerate_fonts()));
73                 }
74
75                 std::wstring find_font_file(const std::wstring& font_name)
76                 {
77                         auto it = std::find_if(fonts.begin(), fonts.end(), font_comparer(font_name));
78                         if(it != fonts.end())
79                         {
80                                 std::wstring filename = L"c:\\windows\\fonts\\" + (*it).second; //TODO: move font-folder setting to settings
81                                 return filename;
82                         }
83         
84                         //try the default font
85                         it = std::find_if(fonts.begin(), fonts.end(), font_comparer(L"verdana"));       //TODO: move default-font to settings
86                         if(it != fonts.end())
87                         {
88                                 std::wstring filename = L"c:\\windows\\fonts\\" + (*it).second; //TODO: move font-folder setting to settings
89                                 return filename;
90                         }
91
92                         //fail
93                         return L"";
94                 }
95         }
96         
97
98 struct text_producer::impl
99 {
100         spl::shared_ptr<core::frame_factory>    frame_factory_;
101         constraints constraints_;
102         int x_, y_, parent_width_, parent_height_;
103         bool standalone_;
104         binding<std::wstring> text_;
105         std::shared_ptr<void> text_subscription_;
106         draw_frame frame_;
107         text::texture_atlas atlas_;
108         text::texture_font font_;
109
110 public:
111         explicit impl(const spl::shared_ptr<frame_factory>& frame_factory, int x, int y, const std::wstring& str, const text::text_info& text_info, long parent_width, long parent_height, bool standalone) 
112                 : frame_factory_(frame_factory)
113                 , constraints_(parent_width, parent_height)
114                 , x_(x), y_(y), parent_width_(parent_width), parent_height_(parent_height)
115                 , standalone_(standalone)
116                 , atlas_(512,512,4)
117                 , font_(atlas_, text::find_font_file(text_info.font), text_info.size, !standalone)
118         {
119                 font_.load_glyphs(text::Basic_Latin, text_info.color);
120                 font_.load_glyphs(text::Latin_1_Supplement, text_info.color);
121                 font_.load_glyphs(text::Latin_Extended_A, text_info.color);
122
123                 text_subscription_ = text_.on_change([this]()
124                 {
125                         generate_frame(text_.get());
126                 });
127
128                 //generate frame
129                 text_.set(str);
130
131                 CASPAR_LOG(info) << print() << L" Initialized";
132         }
133
134         void generate_frame(const std::wstring& str)
135         {
136                 core::pixel_format_desc pfd(core::pixel_format::bgra);
137                 pfd.planes.push_back(core::pixel_format_desc::plane(static_cast<int>(atlas_.width()), static_cast<int>(atlas_.height()), static_cast<int>(atlas_.depth())));
138
139                 text::string_metrics metrics;
140                 std::vector<float> vertex_stream(std::move(font_.create_vertex_stream(str, x_, y_, parent_width_, parent_height_, &metrics)));
141                 auto frame = frame_factory_->create_frame(vertex_stream.data(), pfd);
142                 memcpy(frame.image_data().data(), atlas_.data(), frame.image_data().size());
143                 frame.set_geometry(frame_geometry(frame_geometry::quad_list, std::move(vertex_stream)));
144
145                 this->constraints_.width.set(metrics.width);
146                 this->constraints_.height.set(metrics.height);
147                 frame_ = core::draw_frame(std::move(frame));
148         }
149
150         text::string_metrics measure_string(const std::wstring& str)
151         {
152                 return font_.measure_string(str);
153         }
154
155         // frame_producer
156                         
157         draw_frame receive_impl()
158         {
159                 return frame_;
160         }
161
162         boost::unique_future<std::wstring> call(const std::vector<std::wstring>& param)
163         {
164                 std::wstring result;
165                 text_.set(param.empty() ? L"" : param[0]);
166
167                 return async(launch::deferred, [=]{return result;});
168         }
169
170         constraints& pixel_constraints()
171         {
172                 return constraints_;
173         }
174
175         binding<std::wstring>& text()
176         {
177                 return text_;
178         }
179         
180         std::wstring print() const
181         {
182                 return L"text[" + text_.get() + L"]";
183         }
184
185         std::wstring name() const
186         {
187                 return L"text";
188         }
189         
190         boost::property_tree::wptree info() const
191         {
192                 boost::property_tree::wptree info;
193                 info.add(L"type", L"text");
194                 info.add(L"text", text_.get());
195                 return info;
196         }
197 };
198
199 text_producer::text_producer(const spl::shared_ptr<frame_factory>& frame_factory, int x, int y, const std::wstring& str, const text::text_info& text_info, long parent_width, long parent_height, bool standalone)
200         : impl_(new impl(frame_factory, x, y, str, text_info, parent_width, parent_height, standalone))
201 {}
202
203 draw_frame text_producer::receive_impl() { return impl_->receive_impl(); }
204 boost::unique_future<std::wstring> text_producer::call(const std::vector<std::wstring>& param) { return impl_->call(param); }
205 text::string_metrics text_producer::measure_string(const std::wstring& str) { return impl_->measure_string(str); }
206
207 constraints& text_producer::pixel_constraints() { return impl_->pixel_constraints(); }
208 std::wstring text_producer::print() const { return impl_->print(); }
209 std::wstring text_producer::name() const { return impl_->name(); }
210 boost::property_tree::wptree text_producer::info() const { return impl_->info(); }
211 void text_producer::subscribe(const monitor::observable::observer_ptr& o) {}
212 void text_producer::unsubscribe(const monitor::observable::observer_ptr& o) {}
213 binding<std::wstring>& text_producer::text() { return impl_->text(); }
214
215 spl::shared_ptr<text_producer> text_producer::create(const spl::shared_ptr<frame_factory>& frame_factory, int x, int y, const std::wstring& str, const text::text_info& text_info, long parent_width, long parent_height, bool standalone)
216 {
217         return spl::make_shared<text_producer>(frame_factory, x, y, str, text_info, parent_width, parent_height, standalone);
218 }
219
220 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)
221 {
222         if(params.size() < 2 || !boost::iequals(params.at(0), L"[text]"))
223                 return core::frame_producer::empty();
224
225         int x = 0, y = 0;
226         if(params.size() >= 4)
227         {
228                 x = boost::lexical_cast<int>(params.at(2));
229                 y = boost::lexical_cast<int>(params.at(3));
230         }
231
232         text::text_info text_info;
233         text_info.font = get_param(L"FONT", params, L"verdana");
234         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
235         
236         std::wstring col_str = get_param(L"color", params, L"#ffffffff");
237         uint32_t col_val = 0xffffffff;
238         try_get_color(col_str, col_val);
239         text_info.color = core::text::color<float>(col_val);
240
241         return text_producer::create(frame_factory, x, y, params.at(1), text_info, format_desc.width, format_desc.height, true);
242 }
243
244 }}