summaryrefslogtreecommitdiff
path: root/poppler/Rendition.cc
blob: 4e7727e8cac1cc8fdb3fcf84c8cb4c707da7cf94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//*********************************************************************************
//                               Rendition.cc
//---------------------------------------------------------------------------------
// 
//---------------------------------------------------------------------------------
// Hugo Mercier <hmercier31[at]gmail.com> (c) 2008
// Pino Toscano <pino@kde.org> (c) 2008
// Carlos Garcia Campos <carlosgc@gnome.org> (c) 2010
// Tobias Koenig <tobias.koenig@kdab.com> (c) 2012
// Albert Astals Cid <aacid@kde.org> (C) 2017
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//*********************************************************************************

#include <math.h>
#include "Rendition.h"
#include "FileSpec.h"

MediaWindowParameters::MediaWindowParameters() {
  // default values
  type = windowEmbedded;
  width = -1;
  height = -1;
  relativeTo = windowRelativeToDocument;
  XPosition = 0.5;
  YPosition = 0.5;
  hasTitleBar = gTrue;
  hasCloseButton = gTrue;
  isResizeable = gTrue;
}

MediaWindowParameters::~MediaWindowParameters() {
}

void MediaWindowParameters::parseFWParams(Object* obj) {
  Object tmp = obj->dictLookup("D");
  if (tmp.isArray()) {
    Array * dim = tmp.getArray();
    
    if (dim->getLength() >= 2) {
      Object dd = dim->get(0);
      if (dd.isInt()) {
	width = dd.getInt();
      }

      dd = dim->get(1);
      if (dd.isInt()) {
	height = dd.getInt();
      }

    }
  }

  tmp = obj->dictLookup("RT");
  if (tmp.isInt()) {
    int t = tmp.getInt();
    switch(t) {
    case 0: relativeTo = windowRelativeToDocument; break;
    case 1: relativeTo = windowRelativeToApplication; break;
    case 2: relativeTo = windowRelativeToDesktop; break;
    }
  }

  tmp = obj->dictLookup("P");
  if (tmp.isInt()) {
    int t = tmp.getInt();

    switch(t) {
    case 0: // Upper left
      XPosition = 0.0;
      YPosition = 0.0;
      break;
    case 1: // Upper Center
      XPosition = 0.5;
      YPosition = 0.0;
      break;
    case 2: // Upper Right
      XPosition = 1.0;
      YPosition = 0.0;
      break;
    case 3: // Center Left
      XPosition = 0.0;
      YPosition = 0.5;
      break;
    case 4: // Center
      XPosition = 0.5;
      YPosition = 0.5;
      break;
    case 5: // Center Right
      XPosition = 1.0;
      YPosition = 0.5;
      break;
    case 6: // Lower Left
      XPosition = 0.0;
      YPosition = 1.0;
      break;
    case 7: // Lower Center
      XPosition = 0.5;
      YPosition = 1.0;
      break;
    case 8: // Lower Right
      XPosition = 1.0;
      YPosition = 1.0;
      break;
    }
  }

  tmp = obj->dictLookup("T");
  if (tmp.isBool()) {
    hasTitleBar = tmp.getBool();
  }
  tmp = obj->dictLookup("UC");
  if (tmp.isBool()) {
    hasCloseButton = tmp.getBool();
  }
  tmp = obj->dictLookup("R");
  if (tmp.isInt()) {
    isResizeable = (tmp.getInt() != 0);
  }
}

MediaParameters::MediaParameters() {
  // instanciate to default values

  volume = 100;
  fittingPolicy = fittingUndefined;
  autoPlay = gTrue;
  repeatCount = 1.0;
  opacity = 1.0;
  showControls = gFalse;
  duration = 0;
}

MediaParameters::~MediaParameters() {
}

void MediaParameters::parseMediaPlayParameters(Object* obj) {
  Object tmp = obj->dictLookup("V");
  if (tmp.isInt()) {
    volume = tmp.getInt();
  }

  tmp = obj->dictLookup("C");
  if (tmp.isBool()) {
    showControls = tmp.getBool();
  }

  tmp = obj->dictLookup("F");
  if (tmp.isInt()) {
    int t = tmp.getInt();
    
    switch(t) {
    case 0: fittingPolicy = fittingMeet; break;
    case 1: fittingPolicy = fittingSlice; break;
    case 2: fittingPolicy = fittingFill; break;
    case 3: fittingPolicy = fittingScroll; break;
    case 4: fittingPolicy = fittingHidden; break;
    case 5: fittingPolicy = fittingUndefined; break;
    }
  }

  // duration parsing
  // duration's default value is set to 0, which means : intrinsinc media duration
  tmp = obj->dictLookup("D");
  if (tmp.isDict()) {
    Object oname = tmp.dictLookup("S");
    if (oname.isName()) {
      char* name = oname.getName();
      if (!strcmp(name, "F"))
	duration = -1; // infinity
      else if (!strcmp(name, "T")) {
	Object ddict = tmp.dictLookup("T");
	if (ddict.isDict()) {
	  Object tmp2 = ddict.dictLookup("V");
	  if (tmp2.isNum()) {
	    duration = Gulong(tmp2.getNum());
	  }
	}
      }
    }
  }

  tmp = obj->dictLookup("A");
  if (tmp.isBool()) {
    autoPlay = tmp.getBool();
  }

  tmp = obj->dictLookup("RC");
  if (tmp.isNum()) {
    repeatCount = tmp.getNum();
  }
}

void MediaParameters::parseMediaScreenParameters(Object* obj) {
  Object tmp = obj->dictLookup("W");
  if (tmp.isInt()) {
    int t = tmp.getInt();
    
    switch(t) {
    case 0: windowParams.type = MediaWindowParameters::windowFloating; break;
    case 1: windowParams.type = MediaWindowParameters::windowFullscreen; break;
    case 2: windowParams.type = MediaWindowParameters::windowHidden; break;
    case 3: windowParams.type = MediaWindowParameters::windowEmbedded; break;
    }
  }

  // background color
  tmp = obj->dictLookup("B");
  if (tmp.isArray()) {
    Array* color = tmp.getArray();

    Object component = color->get(0);
    bgColor.r = component.getNum();

    component = color->get(1);
    bgColor.g = component.getNum();

    component = color->get(2);
    bgColor.b = component.getNum();
  }

  // opacity
  tmp = obj->dictLookup("O");
  if (tmp.isNum()) {
    opacity = tmp.getNum();
  }

  if (windowParams.type == MediaWindowParameters::windowFloating) {
    Object winDict = obj->dictLookup("F");
    if (winDict.isDict()) {
      windowParams.parseFWParams(&winDict);
    }
  }
}

MediaRendition::~MediaRendition() {
  if (fileName)
    delete fileName;
  if (contentType)
    delete contentType;

  if (embeddedStream && (!embeddedStream->decRef())) {
    delete embeddedStream;
  }
}

MediaRendition::MediaRendition(Object* obj) {
  GBool hasClip = gFalse;

  ok = gTrue;
  fileName = NULL;
  contentType = NULL;
  isEmbedded = gFalse;
  embeddedStream = NULL;

  //
  // Parse media clip data
  //
  Object tmp2 = obj->dictLookup("C");
  if (tmp2.isDict()) { // media clip
    hasClip = gTrue;
    Object tmp = tmp2.dictLookup("S");
    if (tmp.isName()) {
      if (!strcmp(tmp.getName(), "MCD")) { // media clip data
        Object obj1 = tmp2.dictLookup("D");
	if (obj1.isDict()) {
	  Object obj2 = obj1.dictLookup("F");
	  if (obj2.isString()) {
	    fileName = obj2.getString()->copy();
	  }
	  obj2 = obj1.dictLookup("EF");
	  if (obj2.isDict()) {
	    Object embedded = obj2.dictLookup("F");
	    if (embedded.isStream()) {
	      isEmbedded = gTrue;
	      embeddedStream = embedded.getStream();
	      // "copy" stream
	      embeddedStream->incRef();
	    }
	  }

	  // TODO: D might be a form XObject too
	} else {
	  error (errSyntaxError, -1, "Invalid Media Clip Data");
	  ok = gFalse;
	}

	// FIXME: ignore CT if D is a form XObject
	obj1 = tmp2.dictLookup("CT");
	if (obj1.isString()) {
	  contentType = obj1.getString()->copy();
	}
      } else if (!strcmp(tmp.getName(), "MCS")) { // media clip data
        // TODO
      }
    } else {
      error (errSyntaxError, -1, "Invalid Media Clip");
      ok = gFalse;
    }
  }

  if (!ok)
    return;

  //
  // parse Media Play Parameters
  tmp2 = obj->dictLookup("P");
  if (tmp2.isDict()) { // media play parameters
    Object params = tmp2.dictLookup("MH");
    if (params.isDict()) {
      MH.parseMediaPlayParameters(&params);
    }
    params = tmp2.dictLookup("BE");
    if (params.isDict()) {
      BE.parseMediaPlayParameters(&params);
    }
  } else if (!hasClip) {
    error (errSyntaxError, -1, "Invalid Media Rendition");
    ok = gFalse;
  }

  //
  // parse Media Screen Parameters
  tmp2 = obj->dictLookup("SP");
  if (tmp2.isDict()) { // media screen parameters
    Object params = tmp2.dictLookup("MH");
    if (params.isDict()) {
      MH.parseMediaScreenParameters(&params);
    }
    params = tmp2.dictLookup("BE");
    if (params.isDict()) {
      BE.parseMediaScreenParameters(&params);
    }
  }
}

void MediaRendition::outputToFile(FILE* fp) {
  if (!isEmbedded)
    return;

  embeddedStream->reset();

  while (1) {
    int c = embeddedStream->getChar();
    if (c == EOF)
      break;
    
    fwrite(&c, 1, 1, fp);
  }
  
}

MediaRendition *MediaRendition::copy() {
  // call default copy constructor
  MediaRendition* new_media = new MediaRendition(*this);

  if (contentType)
    new_media->contentType = contentType->copy();
  if (fileName)
    new_media->fileName = fileName->copy();

  if (new_media->embeddedStream)
    new_media->embeddedStream->incRef();

  return new_media;
}

// TODO: SelectorRendition