Advertisement
Guest User

alaric-oxp-utilities.js

a guest
Oct 28th, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. this.name        = "alaric-oxp-utilities";
  4. this.author      = "Alaric";
  5. this.copyright   = "2016 Alaric";
  6. this.description = "General helper functions for OXPs";
  7. this.licence     = "CC BY-NC-SA 3.0 AU";
  8.  
  9.  
  10.  
  11.  
  12. /**
  13.  * Trims a length of text to fit in the avilable width. If the text is truncated,
  14.  * an ellipses (U+2026) will be appended unless <ellipses> is false. If <ellipses>
  15.  * is omitted from the call, it defaults to true.
  16.  *
  17.  * text             - The text to trim
  18.  * emDisplayWidth   - The available width, in Em, to display the text
  19.  * ellipses         - optional (default: true)
  20.  *
  21.  * Returns the new, trimmed, string.
  22.  */
  23. this._trimTextToFitWidth = function(text, emDisplayWidth, ellipses)
  24. {
  25.     var font = defaultFont;
  26.     var chPadding = String.fromCharCode(31);
  27.     var ellipsesText = "\u2026";
  28.     var emEllipsesText = font.measureString(ellipsesText);
  29.     var emPaddingText = font.measureString(chPadding);
  30.        
  31.     var chWidth = [0, text.length];
  32.     var emWidth = [font.measureString(text), 0];
  33.  
  34.     // use default for ellipses if not supplied
  35.     if (ellipses === null || ellipses === undefined) ellipses = true;
  36.    
  37.     // if the text already fits, just return it.
  38.     if (emWidth[0] <= emDisplayWidth) return text;
  39.    
  40.     // if the display width is too short for ellipses, disable ellipses
  41.     if (emEllipsesText >= emDisplayWidth) ellipses = false;
  42.  
  43.     // subtract ellipses with from display width if ellipses is true
  44.     emDisplayWidth -= (ellipses) ? emEllipsesText : 0;
  45.        
  46.     while (chWidth[0] != chWidth[1])
  47.     {
  48.         // get Em width of text at length midway between chWidth[0] and chWidth[1]
  49.         var chPivot = Math.ceil((chWidth[0] + chWidth[1]) / 2);
  50.         var emPivot = font.measureString(text.substring(0, chPivot));
  51.  
  52.         // update for next split point based on the text being too long or too short
  53.         var flagDirection = (emPivot <= emDisplayWidth) ? 0 : 1;
  54.  
  55.         chWidth[flagDirection] = chPivot - flagDirection;
  56.         emWidth[flagDirection] = emPivot;
  57.     }
  58.  
  59.     // At this point, chWidth[0] and emWidth[0] contain the trimmed width in
  60.     // characters and Em respectively. Return the text, appending ellipses if
  61.     // <ellipses> is true. The space for ellipses has already been accounted
  62.     // for.
  63.  
  64.     return (ellipses)
  65.         ? text.substring(0, chWidth[0]) + ellipsesText
  66.         : text.substring(0, chWidth[0])
  67.         ;
  68. }
  69.  
  70.  
  71.  
  72.  
  73.  
  74. /*
  75.  * Builds tabular (columns aligned) text for use in mission screens/MFDs.
  76.  *
  77.  * Input to the function is provided by an array of 'rows'. Each row is, itself, an array of
  78.  * objects with the following properties:
  79.  *
  80.  * Required properties:
  81.  *  text:   The text to display in this column
  82.  *  width:  The width of the column in em. Text will be truncated if too long.
  83.  *
  84.  * Optional properties:
  85.  *  alignment:  LEFT, RIGHT or CENTER. Default: LEFT
  86.  *  elipses:    Display elipses for truncated text? Default: true.
  87.  *
  88.  * Multiple rows are deliniated by '\n'. No '\n' is appended to the last row.
  89.  *
  90.  */
  91. this._buildTabularText = function(rows)
  92. {
  93.     var padCharacter = String.fromCharCode(31);
  94.     var padWidth = defaultFont.measureString(padCharacter);
  95.     var tabularText = "";
  96.     var row;
  97.    
  98.     for (row = 0; row < rows.length; ++row)
  99.     {
  100.         if (row > 0) tabularText += "\n";
  101.    
  102.         var i;
  103.        
  104.         var currentEm0 = 0;
  105.         var currentEm1 = 0;
  106.         var columns = rows[row];
  107.         var rowText = "";
  108.  
  109.         for (i = 0; i < columns.length; ++i)
  110.         {
  111.        
  112.             currentEm0 = defaultFont.measureString(rowText);
  113.             var leading = (currentEm1 - currentEm0);
  114.             currentEm1 = currentEm1 + columns[i].width;
  115.                        
  116.             var text = this._trimTextToFitWidth(columns[i].text, currentEm1 - currentEm0, columns[i].ellipses);
  117.             var width = defaultFont.measureString(text);
  118.            
  119.        
  120.             var padding = (currentEm1 - currentEm0) - width;
  121.  
  122.             switch ((columns[i].alignment !== undefined) ? columns[i].alignment : "LEFT")
  123.             {
  124.                
  125.                 case "LEFT" : padding = 0; break;
  126.                 case "RIGHT" : leading = 0; break;
  127.                 case "CENTER" : padding = padding / 2; break;                  
  128.  
  129.                 default:
  130.                     log(this.name, "invalid alignment '" + columns[i].alignment + "'");
  131.                     padding = 0; break;
  132.  
  133.             }
  134.  
  135.             padding = Math.floor((leading + padding) / padWidth);
  136.            
  137.             rowText += (padding >= 1)
  138.                 ? new Array(padding).join(padCharacter) + text
  139.                 : text
  140.                 ;
  141.         }
  142.    
  143.         tabularText += rowText;
  144.     }
  145.  
  146.     return tabularText;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement