pixelmatix / SmartMatrix

SmartMatrix Library for Teensy 3, Teensy 4, and ESP32
http://docs.pixelmatix.com/SmartMatrix
611 stars 161 forks source link

draw text method return the lenth of drawn string #36

Closed embedded-creations closed 3 years ago

embedded-creations commented 8 years ago

http://community.pixelmatix.com/t/horizontal-update-tearing/94/17?u=louis

embedded-creations commented 8 years ago

#define TEXT_ALIGN_CENTER 0
#define TEXT_ALIGN_RIGHT 1
#define TEXT_ALIGN_LEFT 2

const uint8_t fontDepartureBoard[96][10] = {{3,0,0,0,0,0,0,0,0,0},{1,1,1,1,1,1,0,1,0,0},{3,5,5,5,0,0,0,0,0,0},{5,10,10,31,10,31,10,10,0,0},{5,0,0,0,0,0,0,0,0,0},{5,0,0,0,0,0,0,0,0,0},{5,6,9,6,21,9,9,22,0,0},{1,1,1,0,0,0,0,0,0,0},{3,4,2,1,1,1,2,4,0,0},{3,1,2,4,4,4,2,1,0,0},{5,0,0,0,0,0,0,0,0,0},{5,0,0,4,4,31,4,4,0,0},{2,0,0,0,0,0,0,2,2,1},{4,0,0,0,15,0,0,0,0,0},{1,0,0,0,0,0,0,1,0,0},{4,8,12,4,6,2,3,1,0,0},{4,6,9,9,9,9,9,6,0,0},{4,2,3,2,2,2,2,7,0,0},{4,6,9,8,4,2,1,15,0,0},{4,6,9,8,4,8,9,6,0,0},{4,8,12,10,9,15,8,8,0,0},{4,15,1,7,8,8,9,6,0,0},{4,6,9,1,7,9,9,6,0,0},{4,15,8,4,2,2,2,2,0,0},{4,6,9,9,6,9,9,6,0,0},{4,6,9,9,14,8,9,6,0,0},{1,0,0,1,0,0,1,0,0,0},{1,0,0,1,0,0,1,1,0,0},{4,8,4,2,1,2,4,8,0,0},{4,0,0,15,0,15,0,0,0,0},{4,1,2,4,8,4,2,1,0,0},{5,14,17,16,8,4,0,4,0,0},{5,0,0,0,0,0,0,0,0,0},{4,6,9,9,15,9,9,9,0,0},{4,7,9,9,7,9,9,7,0,0},{4,6,9,1,1,1,9,6,0,0},{4,7,9,9,9,9,9,7,0,0},{4,15,1,1,7,1,1,15,0,0},{4,15,1,1,7,1,1,1,0,0},{4,6,9,1,13,9,9,6,0,0},{4,9,9,9,15,9,9,9,0,0},{3,7,2,2,2,2,2,7,0,0},{4,15,4,4,4,4,5,2,0,0},{4,9,9,5,3,5,9,9,0,0},{4,1,1,1,1,1,1,15,0,0},{5,17,27,21,17,17,17,17,0,0},{4,9,11,13,9,9,9,9,0,0},{4,6,9,9,9,9,9,6,0,0},{4,7,9,9,7,1,1,1,0,0},{4,6,9,9,9,9,6,12,0,0},{4,7,9,9,7,5,9,9,0,0},{4,6,9,1,6,8,9,6,0,0},{5,31,4,4,4,4,4,4,0,0},{4,9,9,9,9,9,9,6,0,0},{5,17,17,17,17,17,10,4,0,0},{5,17,17,17,21,21,27,17,0,0},{5,17,10,4,4,4,10,17,0,0},{5,17,17,10,4,4,4,4,0,0},{5,31,16,8,4,2,1,31,0,0},{3,7,1,1,1,1,1,7,0,0},{4,1,3,2,6,4,12,8,0,0},{3,7,4,4,4,4,4,7,0,0},{5,4,10,17,0,0,0,0,0,0},{5,0,0,0,0,0,0,31,0,0},{5,0,0,0,0,0,0,0,0,0},{4,0,0,6,8,14,9,14,0,0},{4,1,1,7,9,9,9,7,0,0},{4,0,0,14,1,1,1,14,0,0},{4,8,8,14,9,9,9,14,0,0},{4,0,0,6,9,7,1,14,0,0},{4,4,10,2,7,2,2,2,0,0},{4,0,0,6,9,9,9,14,8,7},{4,1,1,5,11,9,9,9,0,0},{1,1,0,1,1,1,1,1,0,0},{3,4,0,4,4,4,4,4,5,2},{4,1,1,9,5,3,5,9,0,0},{2,2,2,2,2,2,2,2,0,0},{5,0,0,11,21,21,17,17,0,0},{4,0,0,5,11,9,9,9,0,0},{4,0,0,6,9,9,9,6,0,0},{4,0,0,7,9,9,9,7,1,1},{4,0,0,14,9,9,9,14,8,8},{4,0,0,5,11,1,1,1,0,0},{4,0,0,14,1,6,8,7,0,0},{4,2,2,7,2,2,2,12,0,0},{4,0,0,9,9,9,9,6,0,0},{5,0,0,17,17,17,10,4,0,0},{5,0,0,17,17,21,21,10,0,0},{5,0,0,17,10,4,10,17,0,0},{4,0,0,9,9,9,9,14,8,7},{5,0,0,31,8,4,2,31,0,0},{5,0,0,0,0,0,0,0,0,0},{1,1,1,1,1,1,1,1,0,0},{5,0,0,0,0,0,0,0,0,0},{5,0,0,2,21,8,0,0,0,0},{5,0,0,0,0,0,0,0,0,0}};

void drawStringAligned(uint8_t x, uint8_t y, uint8_t width, uint8_t charWidth, uint8_t alignment, rgb24 colour, const char* str){
  uint8_t xPos     = 0;
  uint8_t strLen   = strlen(str);
  uint8_t strWidth = (strLen * charWidth);

   // set position of text within container
  switch(alignment){
    case TEXT_ALIGN_LEFT:
      xPos=0;
      break;
    case TEXT_ALIGN_RIGHT:
      xPos = (width-strWidth);
      break;
    case TEXT_ALIGN_CENTER:
      xPos = (width-strWidth)/2;
      break;
  }

  backgroundLayer.drawString(xPos, y, colour, str);
}

/*******************************************************
 * drawString
 *  IN
 *  int x       x position of text
 *  int y       y position of text
 *  uint8_t     width of text container (only needed for text align right/center)
 *  uint16_t    vertical mask (useful when scrolling text up/down without over writing graphics above/below)
 *  uint8_t     text alignment within its container (TEXT_ALIGN_CENTER,TEXT_ALIGN_RIGHT,TEXT_ALIGN_LEFT)
 *  rgb24       text colour
 *  String      text to be displayed
 *
 *  int         length in pixels of drawn text 
 ********************************************************/
int drawString(int x, int y, uint8_t width, uint16_t mask, uint8_t alignment, rgb24 colour, String str){
  uint8_t xPos = 0;
  uint8_t len = 0;
  uint8_t w;

  // pre calculate text width
  for(uint8_t i=0; str[i]!='\0'; ++i)
    len += fontDepartureBoard[ str[i]-32 ][0]+1;
  xPos = len-1;

  // check text is actually within the display canvas, if not DONT draw it! (but still return the length of the string in pixels)
  if((x>=kMatrixWidth) || (x+len<=0))
    return len;

   // set position of text within container
  switch(alignment){
    case TEXT_ALIGN_LEFT:
      xPos=0;
      break;
    case TEXT_ALIGN_RIGHT:
      xPos = (width-xPos);
      break;
    case TEXT_ALIGN_CENTER:
      xPos = (width-xPos)/2;
      break;
  }

  for(uint8_t i=0; str[i]!='\0'; ++i){
    w = drawChar(x+xPos,y,mask,colour,str[i]);
    xPos+=(1+w);
    if(xPos>width && width!=0)
      break;
  }

  return len;
}
uint8_t drawChar(int xPos, int yPos, uint16_t mask, rgb24 colour, char character){
  uint8_t width = fontDepartureBoard[character-32][0];
  // each font row
  for(uint8_t y=0; y<9; y++){
    if(mask & 0x1){
      uint8_t fontRowData = fontDepartureBoard[character-32][y+1];
      // for each font row pixel
      for(uint8_t x=0; x<width; x++){
        if(fontRowData & 0x1)
          backgroundLayer.drawPixel(x+xPos,y+yPos,colour);
        fontRowData>>=1;
      }
    }
    mask>>=1;
  }

  return width;
}