Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- include_library( "string" );
- function get_width( a )
- {
- if( a == "e" ) return 9;
- if( a == "s" ) return 7;
- if( a == "t" ) return 5;
- if( a == " " ) return 5;
- return 8;
- }
- function get_kerning( a, b )
- {
- var c = a $ b;
- if( c == "et" ) return -2;
- if( c == "tt" ) return -1;
- if( c == "st" ) return -1;
- return 0;
- }
- function draw_text( text, width, height )
- {
- println( "\n\n\t" $ text $ "\n\n" );
- /* assumptions */
- lineheight = 12;
- lines = [];
- if( height < lineheight )
- return;
- from = 0;
- eol = 0;
- curline = 0;
- curword = 0;
- prevchar = null;
- for( i = 0; i < text.length; ++i )
- {
- c = text[ i ];
- cw = get_width( c ) + get_kerning( prevchar, c );
- if( c == "\n" )
- {
- curline += curword;
- lines.push({ from = from, to = i, width = curline });
- curline = 0;
- curword = 0;
- if( ( lines.size + 1 ) * lineheight > height )
- break;
- prevchar = null;
- i++;
- while( i < text.length && text[ i ] == " " )
- i++;
- from = i--;
- continue;
- }
- if( c == " " )
- {
- curline += curword;
- eol = i;
- curword = 0;
- }
- if( curline + curword + cw < width )
- {
- // still within line
- curword += cw;
- prevchar = c;
- continue;
- }
- else
- {
- // over the limit
- if( curline )
- {
- // if not first word, commit line and restart the word
- curword = 0;
- i = eol;
- }
- curline += curword;
- lines.push({ from = from, to = i, width = curline });
- curline = 0;
- curword = 0;
- if( ( lines.size + 1 ) * lineheight > height )
- break;
- prevchar = null;
- while( i < text.length && text[ i ] == " " )
- i++;
- from = i--;
- continue;
- }
- }
- curline += curword;
- if( curline )
- {
- lines.push({ from = from, to = text.length, width = curline });
- }
- printvar( lines );
- println();
- println( string_repeat( "-", width ) );
- foreach( line : lines )
- {
- str = "";
- prevchar = null;
- for( i = line.from; i < line.to; ++i )
- {
- c = text[ i ];
- cw = get_width( c ) + get_kerning( prevchar, c );
- while( cw-- )
- str $= c;
- prevchar = c;
- }
- println( str$"\n"$str$"\n" );
- }
- println( string_repeat( "-", width ) );
- println();
- }
- draw_text( "test set tess stest", 50, 30 );
- draw_text( "test set tesstess stest", 50, 50 );
- draw_text( "est est est", 60, 50 );
- draw_text( "test\nset tess stest", 50, 40 );
- draw_text( "est \n est est", 60, 60 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement