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

39
LINES

< > BotCompany Repo | #1006838 // renderText_multiLine - renders text lines from loaded font (e.g. TTF) & anti-alias

JavaX fragment (include)

please include function renderText.

static BufferedImage renderText_multiLine(S fontName, float fontSize, S text) {
  ret renderText_multiLine(loadFont_cached(fontName), fontSize, text);
}

static BufferedImage renderText_multiLine(Font font, float fontSize, S text) {
  Color background = optPar(renderText_bg, Color.white);
  Color foreground = optPar(renderText_fg, Color.black);

  font = font.deriveFont(fontSize);
  L<S> lines = toLines(text);
  
  // for font metrics
  BufferedImage dummyImg = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
  Graphics g = antiAliasGraphics(dummyImg);
  g.setFont(font);
  FontMetrics fm = g.getFontMetrics();
  int width = 0;
  for (S line : lines)
    width = max(width, fm.stringWidth(line));
  if (width <= 0) null;
  int lineHeight = fm.getHeight(); 
  int height = lineHeight*l(lines);
  int y = fm.getAscent();
  
  BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    
  g = antiAliasGraphics(img);
  g.setColor(background);
  g.fillRect(0, 0, width, height);
  g.setColor(foreground);
  g.setFont(font);
  for i over lines:
    g.drawString(lines.get(i), 0, lineHeight*i+y);
  g.dispose();
  
  return img;
}

Author comment

Began life as a copy of #1004566

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1006838
Snippet name: renderText_multiLine - renders text lines from loaded font (e.g. TTF) & anti-alias
Eternal ID of this version: #1006838/7
Text MD5: f8cfa4334352d968ae67f6fca7ab445e
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-02-07 02:04:04
Source code size: 1238 bytes / 39 lines
Pitched / IR pitched: No / No
Views / Downloads: 527 / 551
Version history: 6 change(s)
Referenced in: [show references]