bahamut/tools/script-encoder.hpp
//enhanced version of TextEncoder::lineWidth() supporting additional commands.
//returns the number of pixels required to render a line of text.
//returns nothing if the line width cannot be determined or is ambiguous.
enhanced version of TextEncoder::lineWidth() supporting additional commands.
returns the number of pixels required to render a line of text.
returns nothing if the line width cannot be determined or is ambiguous.
enhanced version of TextEncoder :: lineWidth () supporting additional commands . returns the number of pixels required to render a line of text . returns nothing if the line width cannot be determined or is ambiguous .
auto ScriptEncoder::lineWidth(const string& text, u32 index) -> maybe<u32> {
  u32 width = 0;
  TextEncoder::push();
  while(true) {
    auto read = TextEncoder::read(text, index);
    if(read.isTerminal()) break;
    if(read.isLineFeed()) break;
    if(read.isCommand()) {
      if(read.command == "skip") {
        width += read.argument.natural();
        continue;
      }
      if(read.command == "option") {
        width += 12;
        continue;
      }
      if(read.command == "style") {
        if(read.argument == "normal") { TextEncoder::setStyle(Style::Normal); continue; }
        if(read.argument == "italic") { TextEncoder::setStyle(Style::Italic); continue; }
      }
      if(read.command == "color") continue;
      if(read.command == "pause") continue;
      if(read.command == "wait" ) continue;
      if(auto index = TextEncoder::name(read.command)) {
        if(*index <= 9) {
          width += 66;
        } else if(auto nameWidth = TextEncoder::lineWidth(read.command)) {
          width += *nameWidth;
        }
        continue;
      }
      return TextEncoder::pop(), nothing;  
    }
    if(read.isCharacter()) {
      width -= read.kerning;
      width += read.width;
      continue;
    }
    return TextEncoder::pop(), nothing;
  }
  return TextEncoder::pop(), width;
}
auto ScriptEncoder :: lineWidth ( const string & text , u32 index ) -> maybe < u32 > { u32 width = 0 ; TextEncoder :: push ( ) ; while ( true ) { auto read = TextEncoder :: read ( text , index ) ; if ( read . isTerminal ( ) ) break ; if ( read . isLineFeed ( ) ) break ; if ( read . isCommand ( ) ) { if ( read . command == " " ) { width += read . argument . natural ( ) ; continue ; } if ( read . command == " " ) { width += 12 ; continue ; } if ( read . command == " " ) { if ( read . argument == " " ) { TextEncoder :: setStyle ( Style :: Normal ) ; continue ; } if ( read . argument == " " ) { TextEncoder :: setStyle ( Style :: Italic ) ; continue ; } } if ( read . command == " " ) continue ; if ( read . command == " " ) continue ; if ( read . command == " " ) continue ; if ( auto index = TextEncoder :: name ( read . command ) ) { if ( * index <= 9 ) { width += 66 ; } else if ( auto nameWidth = TextEncoder :: lineWidth ( read . command ) ) { width += * nameWidth ; } continue ; } return TextEncoder :: pop ( ) , nothing ; } if ( read . isCharacter ( ) ) { width -= read . kerning ; width += read . width ; continue ; } return TextEncoder :: pop ( ) , nothing ; } return TextEncoder :: pop ( ) , width ; }
enhanced version of TextEncoder::lineWidth() supporting additional commands.
enhanced version of TextEncoder :: lineWidth () supporting additional commands .
//dynamic name; assume the longest possible width //static name; fixed width //other tags cannot be used with {center}