Using another text renderer

textAlignY support:


public void text2(String txt, float x, float y) {

  float sx = x;
  float size = g.textSize;


  // backup tint
  boolean tint = g.tint;
  //float tintR = g.tintR;
  //float tintG = g.tintG;
  //float tintB = g.tintB;
  //float tintA = g.tintA;
  //float tintRi = g.tintRi;
  //float tintGi = g.tintGi;
  //float tintBi = g.tintBi;
  //float tintAi = g.tintAi;
  int tintColor = g.tintColor;
  //boolean tintAlpha = g.tintAlpha;

  tint(g.fillColor);


  // deal with textAlignY
  // ---
  {
    float height = 0;
    for (int i = 0; i < txt.length(); i++) {
      if (txt.charAt(i) == '\n') {
        height += g.textLeading;
      }
    }

    if (g.textAlignY == CENTER) {
      y += (textAscent() - height)/2;
    } else if (g.textAlignY == TOP) {
      y += textAscent();
    } else if (g.textAlignY == BOTTOM) {
      y -= textDescent() + height;
    }
    
  }
  // ---


  while (txt.length() > 0) {

    String line;

    int index_of_new_line = txt.indexOf("\n", 0);
    if (index_of_new_line != -1) {
      line = txt.substring(0, index_of_new_line);
      txt = txt.substring(index_of_new_line+1, txt.length());
    } else {
      line = txt;
      txt = "";
    }


    if (g.textAlign == RIGHT) {
      x -= textWidth(line);
    } else if (g.textAlign == CENTER) {
      x -= textWidth(line) / 2;
    }


    for (int i = 0; i < line.length(); i++) {
      char c = line.charAt(i);
      if (c == '\r') continue;

      Glyph glyph = font.glyphs[(int)c];

      float x_pos = x + -font.offset_x * size / font.creation_size;
      float y_pos = y - (font.text_ascent * size);
      float width = glyph.g.width * size / font.creation_size;
      float height = glyph.g.height * size / font.creation_size;

      image(glyph.g, x_pos, y_pos, width, height);
      x += glyph.w * size;
    }


    y += g.textLeading;
    x = sx;
  }


  // restore tint
  g.tint = tint;
  //g.tintR = tintR;
  //g.tintG = tintG;
  //g.tintB = tintB;
  //g.tintA = tintA;
  //g.tintRi = tintRi;
  //g.tintGi = tintGi;
  //g.tintBi = tintBi;
  //g.tintAi = tintAi;
  g.tintColor = tintColor;
  //g.tintAlpha = tintAlpha;
}

1 Like