Permalink
Browse files

Conformed to the caption spec, spliting and displaying 32 chars per l…

  • Loading branch information...
1 parent 4b05782 commit bc3f714f10510c6ccb6222b431d0c432808e4545 @arthur-debert arthur-debert committed Jun 7, 2012
Showing with 53 additions and 6 deletions.
  1. +49 −2 media/js/player/captionview.js
  2. +4 −4 media/js/widget/subtitle/transcribeentry.js
@@ -94,6 +94,53 @@ unisubs.player.CaptionView.prototype.setUpPositioning =
};
+/**
+ * Split the given text into the given characters per line and maximumlines.
+ *
+ * @text The text to split in 32x4
+ * @ opt_charsPerLine The max number of characters to allow per line
+ * @ opt_maxLines The max number of lines to allow
+ * @ opt_linebreakStr Which string to use when joining lines, i.e. \n or <bt/>
+ * @return An array with lines of a maximum 32 chars
+**/
+unisubs.player.CaptionView.breakLines = function (text, opt_charsPerLine, opt_maxLines, opt_linebreakStr){
+ var charsPerLine = opt_charsPerLine || 32;
+ var maxLines = opt_maxLines || 4;
+ var linebreakStr = opt_linebreakStr || "<br/>";
+ // short circuit most common case
+ if (!text){
+ return "";
+ }else if(text.length<=charsPerLine){
+ return text;
+ }
+ var lines = [];
+ var currentLine = [],
+ charsOnCurrentLine = 0,
+ nextWord;
+ var words = text.split(" ");
+ while (words.length){
+ nextWord = words.shift();
+ // keep one char for the space between currentLine and nextWord
+ charsOnCurrentLine = currentLine.join(" ").length;
+ if (charsOnCurrentLine + nextWord.length < charsPerLine){
+ currentLine.push(nextWord);
+ }else{
+ lines.push(currentLine);
+ if (lines.length >= maxLines){
+ currentLine = [];
+ break;
+ }
+ currentLine = [nextWord];
+ }
+ }
+ if (currentLine.length){
+ lines.push(currentLine);
+ }
+ var lines = goog.array.map(lines, function(line){
+ return line.join(" ");
+ });
+ return lines.join(linebreakStr);
+}
/*
* @param The html text to show, or null for blank caption
*/
@@ -102,8 +149,8 @@ unisubs.player.CaptionView.prototype.setCaptionText = function(text) {
this.setVisibility(false);
}
else{
- this.getElement().innerHTML =
- goog.string.newLineToBr(goog.string.htmlEscape(text));
+ var text = unisubs.player.CaptionView.breakLines(text);
+ this.getElement().innerHTML = text;
this.redrawInternal();
this.setVisibility(true);
}
@@ -41,7 +41,8 @@ unisubs.subtitle.TranscribeEntry.R = 3;
unisubs.subtitle.TranscribeEntry.S = 1;
// in pixels, js resizes the widget dinamically
unisubs.subtitle.TranscribeEntry.LINE_HEIGHT = 31;
-
+// caption standard says 4 lines with max 32 chars in each
+unisubs.subtitle.TranscribeEntry.CHAR_LIMIT = 128;
unisubs.subtitle.TranscribeEntry.prototype.createDom = function() {
unisubs.subtitle.TranscribeEntry.superClass_.createDom.call(this);
this.getElement().setAttribute('class', 'unisubs-transcribeControls');
@@ -164,14 +165,13 @@ unisubs.subtitle.TranscribeEntry.prototype.addNewTitle_ = function() {
unisubs.subtitle.TranscribeEntry.prototype.issueLengthWarning_ =
function(breakable)
{
- var MAX_CHARS = 100;
var length = this.labelInput_.getValue().length;
- if (breakable && length > MAX_CHARS)
+ if (breakable && length > unisubs.subtitle.TranscribeEntry.CHAR_LIMIT)
this.addNewTitle_();
else
unisubs.style.setProperty(
this.getElement(), 'background',
- this.warningColor_(length, 50, MAX_CHARS));
+ this.warningColor_(length, 90, unisubs.subtitle.TranscribeEntry.CHAR_LIMIT));
};
unisubs.subtitle.TranscribeEntry.prototype.warningColor_ =
function(length, firstChars, maxChars) {

0 comments on commit bc3f714

Please sign in to comment.