Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

45
LINES

< > BotCompany Repo | #1034607 // RenderText - backup before multiline

JavaX fragment (include)

sclass RenderText {
  settable Color foregroundColor = Color.black;
  settable Color backgroundColor = Color.white;
  settable bool withLeading = true;
  settable Font font = sansSerifFont(20);
  settable float fontSize = 20;
  settable S text = "DEMO";
  
  Font scaledFont;
  FontMetrics fm;
  
  *() {}
  *(Font *font) {}
  
  selfType setFont(S fontSnippetID) {
    ret setFont(loadFont_cached(fontSnippetID));
  }
  
  Font scaledFont() { ret scaledFont = font.deriveFont(fontSize); }
  
  BufferedImage get(S text) {
    text(text);
    ret this!;
  }

  BufferedImage get() {
    fm = fontMetrics(scaledFont());
    int width = fm.stringWidth(text);
    if (width <= 0) null;
    int height = fm.getHeight();
    int y = withLeading
      ? fm.getLeading() + fm.getMaxAscent()
      : fm.getAscent();
  
    BufferedImage img = bufferedImageWithoutAlpha(width, height, backgroundColor);
    
    Graphics g = imageGraphics(img);
    g.setColor(foregroundColor);
    g.setFont(scaledFont);
    g.drawString(text, 0, y);
    g.dispose();
  
    ret img;
  }
}

Author comment

Began life as a copy of #1034602

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034607
Snippet name: RenderText - backup before multiline
Eternal ID of this version: #1034607/1
Text MD5: 94ad28c295e60e6f5ba6564c533d94e0
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-02-20 20:42:55
Source code size: 1110 bytes / 45 lines
Pitched / IR pitched: No / No
Views / Downloads: 129 / 151
Referenced in: [show references]