bahamut/tools/text-encoder.hpp
//returns the number of pixels required to render a line of text.
//returns nothing if the line width cannot be determined or is ambiguous.
returns the number of pixels required to render a line of text.
returns nothing if the line width cannot be determined or is ambiguous.
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 TextEncoder::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 == "color") continue;
      if(read.command == "skip") {
        width += read.argument.natural();
        continue;
      }
      if(read.command == "tile") {
        u8 tile = read.argument.hex();
        width += FontEncoder::width(tile);
        continue;
      }
      return TextEncoder::pop(), nothing;
    }
    if(read.isCharacter()) {
      width -= read.kerning;
      width += read.width;
      continue;
    }
    return TextEncoder::pop(), nothing;
  }
  return TextEncoder::pop(), width;
}
auto TextEncoder :: 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 == " " ) continue ; if ( read . command == " " ) { width += read . argument . natural ( ) ; continue ; } if ( read . command == " " ) { u8 tile = read . argument . hex ( ) ; width += FontEncoder :: width ( tile ) ; continue ; } return TextEncoder :: pop ( ) , nothing ; } if ( read . isCharacter ( ) ) { width -= read . kerning ; width += read . width ; continue ; } return TextEncoder :: pop ( ) , nothing ; } return TextEncoder :: pop ( ) , width ; }
returns the number of pixels required to render a line of text.
returns the number of pixels required to render a line of text .