]> git.sesse.net Git - casparcg/blob - modules/psd/descriptor.cpp
Merge pull request #427 from krzyc/patch-1
[casparcg] / modules / psd / descriptor.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 "descriptor.h"
23 #include "misc.h"
24 #include "util/pdf_reader.h"
25
26 #include <common/log.h>
27
28 #include <boost/property_tree/ptree.hpp>
29 #include <boost/property_tree/xml_parser.hpp>
30 #include <boost/locale.hpp>
31
32 #include <memory>
33
34 namespace caspar { namespace psd {
35
36         class descriptor::context::scoped_holder
37         {
38                 descriptor::context::ptr_type ctx_;
39         public:
40                 explicit scoped_holder(const std::wstring& key, descriptor::context::ptr_type ctx) : ctx_(ctx) 
41                 {
42                         Ptree *parent = ctx_->stack.back();
43                         Ptree *child = &parent->push_back(std::make_pair(key, Ptree()))->second;
44                         ctx_->stack.push_back(child);
45                 }
46                 ~scoped_holder()
47                 {
48                         ctx_->stack.pop_back();
49                 }
50         };
51
52         descriptor::descriptor(const std::wstring& debug_name) : context_(std::make_shared<context>(debug_name))
53         {
54                 context_->stack.push_back(&context_->root);
55         }
56         descriptor::descriptor(const std::wstring& key, context::ptr_type context) : context_(context)
57         {
58                 Ptree *parent = context_->stack.back();
59                 Ptree *child = &parent->push_back(std::make_pair(key, Ptree()))->second;
60                 context->stack.push_back(child);
61         }
62         descriptor::~descriptor()
63         {
64                 context_->stack.pop_back();
65         }
66
67 void descriptor::populate(bigendian_file_input_stream& stream)
68 {
69         stream.read_unicode_string();
70         stream.read_id_string();
71         int element_count = stream.read_long();
72         for (int element_index = 0; element_index < element_count; ++element_index)
73         {
74                 std::wstring key = stream.read_id_string();
75                 read_value(key, stream);
76         }
77
78         if (context_->stack.size() == 1)
79                 log::print_child(boost::log::trivial::trace, context_->debug_name + L": ", L"", context_->root);
80 }
81
82 void descriptor::read_value(const std::wstring& key, bigendian_file_input_stream& stream)
83 {
84         auto value_type = stream.read_long();
85
86         switch(value_type)
87         {
88         case 'Objc': 
89                 {
90                         descriptor desc(key, context_);
91                         desc.populate(stream);
92                 }
93                 break;
94
95         case 'doub': 
96                 {
97                         context_->stack.back()->put(key, stream.read_double());
98                 }
99                 break;
100
101         case 'TEXT':
102                 {
103                         context_->stack.back()->put(key, stream.read_unicode_string());
104                 }
105                 break;
106
107         case 'enum':
108                 {
109                         auto k = stream.read_id_string();
110                         auto v = stream.read_id_string();
111                         context_->stack.back()->put(k, v);
112                 }
113                 break;
114
115         case 'long':
116                 {
117                         std::int32_t value = stream.read_long();
118                         context_->stack.back()->put(key, value);
119                 }
120                 break;
121
122         case 'bool': 
123                 {
124                         context_->stack.back()->put(key, stream.read_byte());
125                 }
126                 break;
127
128         case 'VlLs': 
129                 {
130                         context::scoped_holder list(key, context_);
131                         int count = stream.read_long();
132
133                         for (int i = 0; i < count; ++i)
134                         {
135                                 read_value(L"li", stream);
136                         }
137                 }
138                 break;
139
140         case 'tdta':
141                 {
142                         auto rawdata_length = stream.read_long();
143                         std::vector<char> rawdata(rawdata_length);
144                         stream.read(rawdata.data(), rawdata_length);
145                         std::wstring data_str(rawdata.begin(), rawdata.end());
146                         context_->stack.back()->put(key, data_str);
147                 }
148                 break;
149
150         case 'UntF': 
151                 {
152                         context::scoped_holder list(key, context_);
153                         auto unit = stream.read_long();
154                         std::string type(reinterpret_cast<char*>(&unit), 4);
155                         std::wstring wtype(type.rbegin(), type.rend());
156                         context_->stack.back()->put(wtype, stream.read_double());
157                 }
158                 break;
159
160         case 'obj ': 
161         case 'GlbO': 
162         case 'type':
163         case 'GlbC':
164         case 'alis':
165         default:
166                 //descriptor type not supported yet
167                 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("descriptor type not supported yet"));
168         }
169 }
170
171 }       //namespace psd
172 }       //namespace caspar