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     * {@inheritDoc}
97     *
98     * @see org.opencastproject.metadata.mpeg7.VideoText#setIdentifier(java.lang.String)
99     */
100   @Override
101   public void setIdentifier(String id) {
102     this.id = id;
103   }
104 
105   /**
106    * {@inheritDoc}
107    *
108    * @see org.opencastproject.metadata.mpeg7.Segment#getIdentifier()
109    */
110   @Override
111   public String getIdentifier() {
112     return id;
113   }
114 
115   /**
116    * {@inheritDoc}
117    *
118    * @see org.opencastproject.metadata.mpeg7.VideoText#setType(org.opencastproject.metadata.mpeg7.VideoText.Type)
119    */
120   @Override
121   public void setType(Type type) {
122     this.type = type;
123   }
124 
125   /**
126    * {@inheritDoc}
127    *
128    * @see org.opencastproject.metadata.mpeg7.VideoText#getType()
129    */
130   @Override
131   public Type getType() {
132     return type;
133   }
134 
135   /**
136    * {@inheritDoc}
137    *
138    * @see org.opencastproject.metadata.mpeg7.VideoText#setText(org.opencastproject.metadata.mpeg7.Textual)
139    */
140   @Override
141   public void setText(Textual text) {
142     this.text = text;
143   }
144 
145   /**
146    * {@inheritDoc}
147    *
148    * @see org.opencastproject.metadata.mpeg7.VideoText#getText()
149    */
150   @Override
151   public Textual getText() {
152     return text;
153   }
154 
155   /**
156    * {@inheritDoc}
157    *
158    * @see org.opencastproject.metadata.mpeg7.VideoText#setBoundary(java.awt.Rectangle)
159    */
160   @Override
161   public void setBoundary(Rectangle rectangle) {
162     this.boundary = rectangle;
163   }
164 
165   /**
166    * {@inheritDoc}
167    *
168    * @see org.opencastproject.metadata.mpeg7.VideoText#getBoundary()
169    */
170   @Override
171   public Rectangle getBoundary() {
172     return boundary;
173   }
174 
175   /**
176    * {@inheritDoc}
177    *
178    * @see org.opencastproject.metadata.mpeg7.VideoText#setFontSize(int)
179    */
180   @Override
181   public void setFontSize(int size) {
182     this.fontSize = size;
183   }
184 
185   /**
186    * {@inheritDoc}
187    *
188    * @see org.opencastproject.metadata.mpeg7.VideoText#getFontSize()
189    */
190   @Override
191   public int getFontSize() {
192     return fontSize;
193   }
194 
195   /**
196    * {@inheritDoc}
197    *
198    * @see org.opencastproject.metadata.mpeg7.VideoText#setFontType(java.lang.String)
199    */
200   @Override
201   public void setFontType(String fontType) {
202     this.fontType = fontType;
203   }
204 
205   /**
206    * {@inheritDoc}
207    *
208    * @see org.opencastproject.metadata.mpeg7.VideoText#getFontType()
209    */
210   @Override
211   public String getFontType() {
212     return fontType;
213   }
214 
215   /**
216    * {@inheritDoc}
217    *
218    * @see org.opencastproject.metadata.mpeg7.MovingRegion#setSpatioTemporalLocator(org.opencastproject.metadata.mpeg7.SpatioTemporalLocator)
219    */
220   @Override
221   public void setSpatioTemporalLocator(SpatioTemporalLocator locator) {
222     this.locator = locator;
223   }
224 
225   /**
226    * {@inheritDoc}
227    *
228    * @see org.opencastproject.metadata.mpeg7.MovingRegion#getSpatioTemporalLocator()
229    */
230   @Override
231   public SpatioTemporalLocator getSpatioTemporalLocator() {
232     return locator;
233   }
234 
235   /**
236    * {@inheritDoc}
237    *
238    * @see org.opencastproject.mediapackage.XmlElement#toXml(org.w3c.dom.Document)
239    */
240   @Override
241   public Node toXml(Document document) {
242     DecimalFormat format = new DecimalFormat();
243     format.setDecimalFormatSymbols(standardSymbols);
244 
245     Element videoText = document.createElement("VideoText");
246     videoText.setAttribute("id", id);
247     videoText.setAttribute("textType", type.toString());
248     if (fontSize > 0)
249       videoText.setAttribute("fontSize", Integer.toString(fontSize));
250     if (fontType != null)
251       videoText.setAttribute("fontType", fontType);
252 
253     // Media locator
254     if (locator != null)
255       videoText.appendChild(locator.toXml(document));
256 
257     // Temporal Mask (Boundary)
258     if (boundary != null) {
259       Element temporalMask = document.createElement("SpatioTemporalMask");
260       videoText.appendChild(temporalMask);
261 
262       Element subRegion = document.createElement("SubRegion");
263       temporalMask.appendChild(subRegion);
264       Element parameterTrajectory = document.createElement("ParameterTrajectory");
265       subRegion.appendChild(parameterTrajectory);
266 
267       parameterTrajectory.appendChild(new MediaTimeImpl(new MediaTimePointImpl(), null).toXml(document));
268       Element initialRegion = document.createElement("InitialRegion");
269       parameterTrajectory.appendChild(initialRegion);
270 
271       StringBuffer coordinates = new StringBuffer();
272       coordinates.append(format.format(boundary.getX())).append(" ");
273       coordinates.append(format.format(boundary.getY())).append(" ");
274       coordinates.append(format.format(boundary.getX() + boundary.getWidth())).append(" ");
275       coordinates.append(format.format(boundary.getY() + boundary.getHeight()));
276 
277       Element box = document.createElement("Box");
278       box.setAttribute("dim", "2 2");
279       box.appendChild(document.createTextNode(coordinates.toString()));
280       initialRegion.appendChild(box);
281     }
282 
283     // Text
284     videoText.appendChild(text.toXml(document));
285     return videoText;
286   }
287 
288 }