Advertisement
snake5

SGScript - multiline text rendering prototype

Apr 30th, 2013
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. include_library( "string" );
  2.  
  3.  
  4. function get_width( a )
  5. {
  6.     if( a == "e" ) return 9;
  7.     if( a == "s" ) return 7;
  8.     if( a == "t" ) return 5;
  9.     if( a == " " ) return 5;
  10.     return 8;
  11. }
  12.  
  13. function get_kerning( a, b )
  14. {
  15.     var c = a $ b;
  16.     if( c == "et" ) return -2;
  17.     if( c == "tt" ) return -1;
  18.     if( c == "st" ) return -1;
  19.     return 0;
  20. }
  21.  
  22.  
  23. function draw_text( text, width, height )
  24. {
  25.     println( "\n\n\t" $ text $ "\n\n" );
  26.  
  27.     /* assumptions */
  28.     lineheight = 12;
  29.  
  30.     lines = [];
  31.  
  32.     if( height < lineheight )
  33.         return;
  34.  
  35.     from = 0;
  36.     eol = 0;
  37.     curline = 0;
  38.     curword = 0;
  39.     prevchar = null;
  40.     for( i = 0; i < text.length; ++i )
  41.     {
  42.         c = text[ i ];
  43.         cw = get_width( c ) + get_kerning( prevchar, c );
  44.         if( c == "\n" )
  45.         {
  46.             curline += curword;
  47.             lines.push({ from = from, to = i, width = curline });
  48.             curline = 0;
  49.             curword = 0;
  50.             if( ( lines.size + 1 ) * lineheight > height )
  51.                 break;
  52.             prevchar = null;
  53.             i++;
  54.             while( i < text.length && text[ i ] == " " )
  55.                 i++;
  56.             from = i--;
  57.             continue;
  58.         }
  59.         if( c == " " )
  60.         {
  61.             curline += curword;
  62.             eol = i;
  63.             curword = 0;
  64.         }
  65.         if( curline + curword + cw < width )
  66.         {
  67.             // still within line
  68.             curword += cw;
  69.             prevchar = c;
  70.             continue;
  71.         }
  72.         else
  73.         {
  74.             // over the limit
  75.             if( curline )
  76.             {
  77.                 // if not first word, commit line and restart the word
  78.                 curword = 0;
  79.                 i = eol;
  80.             }
  81.             curline += curword;
  82.             lines.push({ from = from, to = i, width = curline });
  83.             curline = 0;
  84.             curword = 0;
  85.             if( ( lines.size + 1 ) * lineheight > height )
  86.                 break;
  87.             prevchar = null;
  88.             while( i < text.length && text[ i ] == " " )
  89.                 i++;
  90.             from = i--;
  91.             continue;
  92.         }
  93.     }
  94.  
  95.     curline += curword;
  96.     if( curline )
  97.     {
  98.         lines.push({ from = from, to = text.length, width = curline });
  99.     }
  100.  
  101.     printvar( lines );
  102.     println();
  103.  
  104.     println( string_repeat( "-", width ) );
  105.     foreach( line : lines )
  106.     {
  107.         str = "";
  108.         prevchar = null;
  109.         for( i = line.from; i < line.to; ++i )
  110.         {
  111.             c = text[ i ];
  112.             cw = get_width( c ) + get_kerning( prevchar, c );
  113.             while( cw-- )
  114.                 str $= c;
  115.             prevchar = c;
  116.         }
  117.         println( str$"\n"$str$"\n" );
  118.     }
  119.     println( string_repeat( "-", width ) );
  120.     println();
  121. }
  122.  
  123.  
  124. draw_text( "test set tess stest", 50, 30 );
  125. draw_text( "test set tesstess stest", 50, 50 );
  126. draw_text( "est    est  est", 60, 50 );
  127. draw_text( "test\nset tess stest", 50, 40 );
  128. draw_text( "est  \n  est  est", 60, 60 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement