public static Movie addTextTrack(Movie mov, String text, String fontName,
String fontOptions, int fontSize, int[] fgColor, int[] bgColor, int x,
int y, float start, float duration)
{
// make a copy of the original movie
Movie movie = MovieUtils.cloneMovie(mov);
try
{
movie = new Movie();
mov.insertSegment(movie, 0, mov.getDuration(), 0);
movie.setDefaultDataRef(new DataRef(new QTHandle()));
} // try
catch (QTException qte)
{
qte.printStackTrace();
} // catch (QTException)
// determine opaque / transparent background color; transparency is
// indicated by a -1 in the first cell of bgColor
int dfFlag = 0;
if (bgColor[0] == -1)
{
dfFlag = StdQTConstants.dfAntiAlias | StdQTConstants.dfKeyedText;
} // if (bgColor[0] == -1)
else
{
dfFlag = StdQTConstants.dfAntiAlias;
} // else
// set up the Java font to estimate dimensions of the actual text
// track using QDFont
Font font = null;
int style = 0;
if (fontOptions.equalsIgnoreCase("plain"))
{
style = QDConstants.normal;
font = new Font(fontName, Font.PLAIN, fontSize);
} // if (fontOptions.equalsIgnoreCase("plain"))
else if (fontOptions.equalsIgnoreCase("bold"))
{
style = QDConstants.bold;
font = new Font(fontName, Font.BOLD, fontSize);
} // else if (fontOptions.equalsIgnoreCase("bold"))
else if (fontOptions.equalsIgnoreCase("italic"))
{
style = QDConstants.italic;
font = new Font(fontName, Font.ITALIC, fontSize + 2);
} // else if (fontOptions.equalsIgnoreCase("italic"))
else if (fontOptions.equalsIgnoreCase("bolditalic"))
{
style = QDConstants.bold | QDConstants.italic;
font = new Font(fontName, Font.PLAIN + Font.ITALIC, fontSize + 5);
} // else if (fontOptions.equalsIgnoreCase("bolditalic"))
else if (fontOptions.equalsIgnoreCase("underline"))
{
style = QDConstants.underlined;
font = new Font(fontName, Font.ITALIC + Font.BOLD, fontSize);
} // else if (fontOptions.equalsIgnoreCase("underline"))
else if (fontOptions.equalsIgnoreCase("boldunderline"))
{
style = QDConstants.underlined | QDConstants.bold;
font = new Font(fontName, Font.ITALIC + Font.BOLD, fontSize + 1);
} // else if (fontOptions.equalsIgnoreCase("boldunderline"))
else if (fontOptions.equalsIgnoreCase("italicunderline"))
{
style = QDConstants.underlined | QDConstants.italic;
font = new Font(fontName, Font.ITALIC + Font.BOLD, fontSize + 1);
} // else if (fontOptions.equalsIgnoreCase("italicunderline"))
else if (fontOptions.equalsIgnoreCase("bolditalicunderline"))
{
style = QDConstants.underlined| QDConstants.italic | QDConstants.bold;
font = new Font(fontName, Font.ITALIC + Font.BOLD, fontSize + 1);
} // else if (fontOptions.equalsIgnoreCase("bolditalicunderline"))
JDialog tempF = new JDialog();
JPanel temp = new JPanel();
temp.setFont(font);
tempF.add(temp);
// must be visible, otherwise g2 is null
tempF.setVisible(true);
temp.setVisible(true);
Graphics2D g2 = (Graphics2D)temp.getGraphics();
g2.setFont(font);
g2.drawString(text, 0, 0);
tempF.setVisible(false);
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D rect = font.getStringBounds(text, context);
int TEXT_TRACK_WIDTH = (int)rect.getWidth();
int TEXT_TRACK_HEIGHT = (int)rect.getHeight();
temp = null;
tempF = null;
QDRect textBox = new QDRect(0, 0, TEXT_TRACK_WIDTH, TEXT_TRACK_HEIGHT);
// add text track
Track textTrack;
try
{
textTrack = movie.addTrack(TEXT_TRACK_WIDTH, TEXT_TRACK_HEIGHT, 0);
Media textMedia = new TextMedia(textTrack, movie.getTimeScale());
TextMediaHandler handler = (TextMediaHandler)textMedia.getHandler();
textMedia.beginEdits();
byte[] msgBytes = text.getBytes();
QTPointer msgPoint = new QTPointer(msgBytes);
handler.addTextSample(msgPoint, QDFont.getFNum(fontName), fontSize, style,
new QDColor(fgColor[0] / 255f, fgColor[1] / 255f, fgColor[2] / 255f),
new QDColor(bgColor[0] / 255f, bgColor[1] / 255f, bgColor[2] / 255f),
QDConstants.teJustLeft, textBox, dfFlag, QDFont.getFNum(fontName), 0, 0,
QDColor.white, Math.round(duration * movie.getTimeScale()));
textMedia.endEdits();
textTrack.insertMedia(Math.round(start * movie.getTimeScale()), 0,
textMedia.getDuration(), 1);
// position the text track to (x, y)
Matrix mat = new Matrix();
mat.rect(new QDRect(0, 0, TEXT_TRACK_WIDTH, TEXT_TRACK_HEIGHT),