View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.metadata.mpeg7;
23  
24  import org.w3c.dom.Document;
25  import org.w3c.dom.Element;
26  import org.w3c.dom.Node;
27  
28  import java.awt.Rectangle;
29  import java.text.DecimalFormat;
30  import java.text.DecimalFormatSymbols;
31  import java.util.Locale;
32  
33  /**
34   * Default implementation of the video text element.
35   */
36  public class VideoTextImpl implements VideoText {
37  
38    /**
39     * Number formatter, used to deal with relevance values in a locale
40     * independent way
41     */
42    private static DecimalFormatSymbols standardSymbols = new DecimalFormatSymbols(Locale.US);
43  
44    /** The video text type */
45    protected Type type = Type.superimposed;
46  
47    /** The segment identifier */
48    protected String id = null;
49  
50    /** The text */
51    protected Textual text = null;
52  
53    /** The text's bounding box */
54    protected Rectangle boundary = null;
55  
56    /** The text' time and duration */
57    protected SpatioTemporalLocator locator = null;
58  
59    /** The font's name */
60    protected String fontType = null;
61  
62    /** The font size */
63    protected int fontSize = -1;
64  
65    /**
66     * Creates a new <code>VideoText</code> element with the given id, text and text boundary.
67     *
68     * @param id
69     *          the text identifier
70     */
71    public VideoTextImpl(String id) {
72      this(id, null, null, null);
73    }
74  
75    /**
76     * Creates a new <code>VideoText</code> element with the given id, text and text boundary.
77     *
78     * @param id
79     *          the text identifier
80     * @param text
81     *          the text
82     * @param boundary
83     *          the text's bounding box
84     * @param time
85     *          the media time
86     */
87    public VideoTextImpl(String id, Textual text, Rectangle boundary, MediaTime time) {
88      this.id = id;
89      this.text = text;
90      this.boundary = boundary;
91      if (time != null) {
92        this.locator = new SpatioTemporalLocatorImpl(time);
93      }
94    }
95  
96    /**
97     * {@inheritDoc}
98     *
99     * @see org.opencastproject.metadata.mpeg7.VideoText#setIdentifier(java.lang.String)
100    */
101   @Override
102   public void setIdentifier(String id) {
103     this.id = id;
104   }
105 
106   /**
107    * {@inheritDoc}
108    *
109    * @see org.opencastproject.metadata.mpeg7.Segment#getIdentifier()
110    */
111   @Override
112   public String getIdentifier() {
113     return id;
114   }
115 
116   /**
117    * {@inheritDoc}
118    *
119    * @see org.opencastproject.metadata.mpeg7.VideoText#setType(org.opencastproject.metadata.mpeg7.VideoText.Type)
120    */
121   @Override
122   public void setType(Type type) {
123     this.type = type;
124   }
125 
126   /**
127    * {@inheritDoc}
128    *
129    * @see org.opencastproject.metadata.mpeg7.VideoText#getType()
130    */
131   @Override
132   public Type getType() {
133     return type;
134   }
135 
136   /**
137    * {@inheritDoc}
138    *
139    * @see org.opencastproject.metadata.mpeg7.VideoText#setText(org.opencastproject.metadata.mpeg7.Textual)
140    */
141   @Override
142   public void setText(Textual text) {
143     this.text = text;
144   }
145 
146   /**
147    * {@inheritDoc}
148    *
149    * @see org.opencastproject.metadata.mpeg7.VideoText#getText()
150    */
151   @Override
152   public Textual getText() {
153     return text;
154   }
155 
156   /**
157    * {@inheritDoc}
158    *
159    * @see org.opencastproject.metadata.mpeg7.VideoText#setBoundary(java.awt.Rectangle)
160    */
161   @Override
162   public void setBoundary(Rectangle rectangle) {
163     this.boundary = rectangle;
164   }
165 
166   /**
167    * {@inheritDoc}
168    *
169    * @see org.opencastproject.metadata.mpeg7.VideoText#getBoundary()
170    */
171   @Override
172   public Rectangle getBoundary() {
173     return boundary;
174   }
175 
176   /**
177    * {@inheritDoc}
178    *
179    * @see org.opencastproject.metadata.mpeg7.VideoText#setFontSize(int)
180    */
181   @Override
182   public void setFontSize(int size) {
183     this.fontSize = size;
184   }
185 
186   /**
187    * {@inheritDoc}
188    *
189    * @see org.opencastproject.metadata.mpeg7.VideoText#getFontSize()
190    */
191   @Override
192   public int getFontSize() {
193     return fontSize;
194   }
195 
196   /**
197    * {@inheritDoc}
198    *
199    * @see org.opencastproject.metadata.mpeg7.VideoText#setFontType(java.lang.String)
200    */
201   @Override
202   public void setFontType(String fontType) {
203     this.fontType = fontType;
204   }
205 
206   /**
207    * {@inheritDoc}
208    *
209    * @see org.opencastproject.metadata.mpeg7.VideoText#getFontType()
210    */
211   @Override
212   public String getFontType() {
213     return fontType;
214   }
215 
216   /**
217    * {@inheritDoc}
218    *
219    * @see org.opencastproject.metadata.mpeg7.MovingRegion#setSpatioTemporalLocator(
220    * org.opencastproject.metadata.mpeg7.SpatioTemporalLocator)
221    */
222   @Override
223   public void setSpatioTemporalLocator(SpatioTemporalLocator locator) {
224     this.locator = locator;
225   }
226 
227   /**
228    * {@inheritDoc}
229    *
230    * @see org.opencastproject.metadata.mpeg7.MovingRegion#getSpatioTemporalLocator()
231    */
232   @Override
233   public SpatioTemporalLocator getSpatioTemporalLocator() {
234     return locator;
235   }
236 
237   /**
238    * {@inheritDoc}
239    *
240    * @see org.opencastproject.mediapackage.XmlElement#toXml(org.w3c.dom.Document)
241    */
242   @Override
243   public Node toXml(Document document) {
244     DecimalFormat format = new DecimalFormat();
245     format.setDecimalFormatSymbols(standardSymbols);
246 
247     Element videoText = document.createElement("VideoText");
248     videoText.setAttribute("id", id);
249     videoText.setAttribute("textType", type.toString());
250     if (fontSize > 0) {
251       videoText.setAttribute("fontSize", Integer.toString(fontSize));
252     }
253     if (fontType != null) {
254       videoText.setAttribute("fontType", fontType);
255     }
256 
257     // Media locator
258     if (locator != null) {
259       videoText.appendChild(locator.toXml(document));
260     }
261 
262     // Temporal Mask (Boundary)
263     if (boundary != null) {
264       Element temporalMask = document.createElement("SpatioTemporalMask");
265       videoText.appendChild(temporalMask);
266 
267       Element subRegion = document.createElement("SubRegion");
268       temporalMask.appendChild(subRegion);
269       Element parameterTrajectory = document.createElement("ParameterTrajectory");
270       subRegion.appendChild(parameterTrajectory);
271 
272       parameterTrajectory.appendChild(new MediaTimeImpl(new MediaTimePointImpl(), null).toXml(document));
273       Element initialRegion = document.createElement("InitialRegion");
274       parameterTrajectory.appendChild(initialRegion);
275 
276       StringBuffer coordinates = new StringBuffer();
277       coordinates.append(format.format(boundary.getX())).append(" ");
278       coordinates.append(format.format(boundary.getY())).append(" ");
279       coordinates.append(format.format(boundary.getX() + boundary.getWidth())).append(" ");
280       coordinates.append(format.format(boundary.getY() + boundary.getHeight()));
281 
282       Element box = document.createElement("Box");
283       box.setAttribute("dim", "2 2");
284       box.appendChild(document.createTextNode(coordinates.toString()));
285       initialRegion.appendChild(box);
286     }
287 
288     // Text
289     videoText.appendChild(text.toXml(document));
290     return videoText;
291   }
292 
293 }