]> git.sesse.net Git - casparcg/blob - modules/psd/psd_scene_producer.cpp
Allow for a scene_producer to expose parameters updateable via CALL
[casparcg] / modules / psd / psd_scene_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 "psd_scene_producer.h"
23 #include "layer.h"
24 #include "doc.h"
25
26 #include <core/frame/pixel_format.h>
27 #include <core/frame/frame_factory.h>
28 #include <core/producer/frame_producer.h>
29 #include <core/producer/text/text_producer.h>
30 #include <core/producer/scene/scene_producer.h>
31 #include <core/producer/scene/const_producer.h>
32 #include <core/frame/draw_frame.h>
33
34 #include <common/env.h>
35 #include <common/memory.h>
36
37 #include <boost/filesystem.hpp>
38 #include <boost/foreach.hpp>
39 #include <boost/thread/future.hpp>
40
41 namespace caspar { namespace psd {
42
43 void init()
44 {
45         core::register_producer_factory(create_psd_scene_producer);
46 }
47
48 core::text::text_info get_text_info(const boost::property_tree::wptree& ptree)
49 {
50         core::text::text_info result;
51         int font_index = ptree.get(L"EngineDict.StyleRun.RunArray..StyleSheet.StyleSheetData.Font", 0);
52         result.size = ptree.get(L"EngineDict.StyleRun.RunArray..StyleSheet.StyleSheetData.FontSize", 30.0f);
53
54         int child_index = 0;
55         auto color_node = ptree.get_child(L"EngineDict.StyleRun.RunArray..StyleSheet.StyleSheetData.FillColor.Values");
56         for(auto it = color_node.begin(); it != color_node.end(); ++it, ++child_index)
57         {
58                 auto& value_node = (*it).second;
59                 float value = value_node.get_value(0.0f);
60                 switch(child_index)
61                 {
62                 case 0: result.color.a = value; break;
63                 case 1: result.color.r = value; break;
64                 case 2: result.color.g = value; break;
65                 case 3: result.color.b = value; break;
66                 }
67         }
68
69         //find fontname
70         child_index = 0;
71         auto fontset_node = ptree.get_child(L"ResourceDict.FontSet");
72         for(auto it = fontset_node.begin(); it != fontset_node.end(); ++it, ++child_index)
73         {
74                 auto& font_node = (*it).second;
75                 if(child_index == font_index)
76                 {
77                         result.font = font_node.get(L"Name", L"");
78                         break;
79                 }
80         }
81
82         return result;
83 }
84
85         class layer_link_constructor
86         {
87                 struct linked_layer_record
88                 {
89                         caspar::core::scene::layer* layer;
90                         int link_id;
91                         bool is_master;
92                 };              
93
94                 std::vector<linked_layer_record> layers;
95                 std::vector<linked_layer_record> masters;
96
97         public:
98                 void add(caspar::core::scene::layer* layer, int link_group, bool master)
99                 {
100                         linked_layer_record rec;
101                         rec.layer = layer;
102                         rec.link_id = link_group;
103                         rec.is_master = master;
104                         layers.push_back(rec);
105
106                         if(rec.is_master)
107                         {
108                                 auto it = std::find_if(masters.begin(), masters.end(), [=](linked_layer_record const &r){ return r.link_id == link_group; });
109                                 if(it == masters.end())
110                                         masters.push_back(rec);
111                                 else
112                                         masters.erase(it);      //ambigous, two linked layers with locked position
113                         }
114                 }
115
116                 void calculate()
117                 {
118                         for(auto it = masters.begin(); it != masters.end(); ++it)
119                         {
120                                 auto& master = (*it);
121                                 //std::for_each(layers.begin(), layers.end(), [&master](linked_layer_record &r) mutable { 
122                                 BOOST_FOREACH(auto &r, layers) {
123                                         if(r.link_id == master.link_id && r.layer != master.layer)
124                                         {
125                                                 {       //x-coords
126                                                         double slave_left = r.layer->position.x.get();
127                                                         double slave_right = slave_left + r.layer->producer.get()->pixel_constraints().width.get();
128
129                                                         double master_left = master.layer->position.x.get();
130                                                         double master_right = master_left + master.layer->producer.get()->pixel_constraints().width.get();
131
132                                                         if((slave_left >= master_left && slave_right <= master_right) || (slave_left <= master_left && slave_right >= master_right))
133                                                         {
134                                                                 //change width of slave
135                                                                 r.layer->position.x = master.layer->position.x + (slave_left - master_left);
136                                                                 r.layer->producer.get()->pixel_constraints().width = master.layer->producer.get()->pixel_constraints().width + (slave_right - slave_left - master_right + master_left); 
137                                                         }
138                                                         else if(slave_left >= master_right)
139                                                         {
140                                                                 //push slave ahead of master
141                                                                 r.layer->position.x = master.layer->position.x + master.layer->producer.get()->pixel_constraints().width + (slave_left - master_right);
142                                                         }
143                                                         else if(slave_right <= master_left)
144                                                         {
145                                                                 r.layer->position.x = master.layer->position.x - (master_left - slave_left);
146                                                         }
147                                                 }
148
149                                                 {       //y-coords
150                                                         double slave_top = r.layer->position.y.get();
151                                                         double slave_bottom = slave_top + r.layer->producer.get()->pixel_constraints().height.get();
152
153                                                         double master_top = master.layer->position.y.get();
154                                                         double master_bottom = master_top + master.layer->producer.get()->pixel_constraints().height.get();
155
156                                                         if((slave_top >= master_top && slave_bottom <= master_bottom) || (slave_top <= master_top && slave_bottom >= master_bottom))
157                                                         {
158                                                                 //change width of slave
159                                                                 r.layer->position.y = master.layer->position.y + (slave_top - master_top);
160                                                                 r.layer->producer.get()->pixel_constraints().height = master.layer->producer.get()->pixel_constraints().height + (slave_bottom - slave_top - master_bottom + master_top); 
161                                                         }
162                                                         else if(slave_top >= master_bottom)
163                                                         {
164                                                                 //push slave ahead of master
165                                                                 r.layer->position.y = master.layer->position.y + master.layer->producer.get()->pixel_constraints().height + (slave_top - master_bottom);
166                                                         }
167                                                         else if(slave_bottom <= master_top)
168                                                         {
169                                                                 r.layer->position.y = master.layer->position.y - (master_top - slave_top);
170                                                         }
171                                                 }
172
173                                         }
174                                 }
175                         }
176                 }
177         };
178
179 spl::shared_ptr<core::frame_producer> create_psd_scene_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const core::video_format_desc& format_desc, const std::vector<std::wstring>& params)
180 {
181         std::wstring filename = env::media_folder() + L"\\" + params[0] + L".psd";
182         if(!boost::filesystem::is_regular_file(boost::filesystem::path(filename)))
183                 return core::frame_producer::empty();
184
185         psd_document doc;
186         if(!doc.parse(filename))
187                 return core::frame_producer::empty();
188
189         spl::shared_ptr<core::scene::scene_producer> root(spl::make_shared<core::scene::scene_producer>(doc.width(), doc.height()));
190
191         layer_link_constructor link_constructor;
192
193         std::vector<std::pair<std::wstring, spl::shared_ptr<core::text_producer>>> text_producers_by_layer_name;
194
195         auto layers_end = doc.layers().end();
196         for(auto it = doc.layers().begin(); it != layers_end; ++it)
197         {
198                 if((*it)->is_text() && (*it)->visible())
199                 {
200                         std::wstring str = (*it)->text_data().get(L"EngineDict.Editor.Text", L"");
201                         
202                         core::text::text_info text_info(std::move(get_text_info((*it)->text_data())));
203                         auto text_producer = core::text_producer::create(frame_factory, 0, 0, str, text_info, doc.width(), doc.height());
204                         
205                         core::text::string_metrics metrics = text_producer->measure_string(str);
206                         
207                         auto& new_layer = root->create_layer(text_producer, (*it)->rect().left - 2, (*it)->rect().top + metrics.bearingY, (*it)->name());       //the 2 offset is just a hack for now. don't know why our text is rendered 2 px to the right of that in photoshop
208                         new_layer.adjustments.opacity.set((*it)->opacity() / 255.0);
209                         new_layer.hidden.set(!(*it)->visible());
210
211                         if((*it)->link_group_id() != 0)
212                                 link_constructor.add(&new_layer, (*it)->link_group_id(), true);
213
214                         text_producers_by_layer_name.push_back(std::make_pair((*it)->name(), text_producer));
215                 }
216                 else if((*it)->image() && (*it)->visible())
217                 {
218                         core::pixel_format_desc pfd(core::pixel_format::bgra);
219                         pfd.planes.push_back(core::pixel_format_desc::plane((*it)->rect().width(), (*it)->rect().height(), 4));
220
221                         auto frame = frame_factory->create_frame(it->get(), pfd);
222                         memcpy(frame.image_data().data(), (*it)->image()->data(), frame.image_data().size());
223
224                         auto layer_producer = core::create_const_producer(core::draw_frame(std::move(frame)), (*it)->rect().width(), (*it)->rect().height());
225
226                         auto& new_layer = root->create_layer(layer_producer, (*it)->rect().left, (*it)->rect().top);
227                         new_layer.adjustments.opacity.set((*it)->opacity() / 255.0);
228                         new_layer.hidden.set(!(*it)->visible());
229
230                         if((*it)->link_group_id() != 0)
231                                 link_constructor.add(&new_layer, (*it)->link_group_id(), false);
232                 }
233         }
234
235         link_constructor.calculate();
236
237         // Reset all dynamic text fields to empty strings and expose them as a scene parameter.
238         BOOST_FOREACH(auto& text_layer, text_producers_by_layer_name)
239                 text_layer.second->text().bind(root->create_parameter<std::wstring>(text_layer.first, L""));
240
241         auto params2 = params;
242         params2.erase(params2.cbegin());
243
244         root->call(params2);
245
246         return root;
247 }
248
249 }}