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)

1  
please include function renderText.
2  
3  
static BufferedImage renderText_multiLine(S fontName, float fontSize, S text) {
4  
  ret renderText_multiLine(loadFont_cached(fontName), fontSize, text);
5  
}
6  
7  
static BufferedImage renderText_multiLine(Font font, float fontSize, S text) {
8  
  Color background = optPar(renderText_bg, Color.white);
9  
  Color foreground = optPar(renderText_fg, Color.black);
10  
11  
  font = font.deriveFont(fontSize);
12  
  L<S> lines = toLines(text);
13  
  
14  
  // for font metrics
15  
  BufferedImage dummyImg = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
16  
  Graphics g = antiAliasGraphics(dummyImg);
17  
  g.setFont(font);
18  
  FontMetrics fm = g.getFontMetrics();
19  
  int width = 0;
20  
  for (S line : lines)
21  
    width = max(width, fm.stringWidth(line));
22  
  if (width <= 0) null;
23  
  int lineHeight = fm.getHeight(); 
24  
  int height = lineHeight*l(lines);
25  
  int y = fm.getAscent();
26  
  
27  
  BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
28  
    
29  
  g = antiAliasGraphics(img);
30  
  g.setColor(background);
31  
  g.fillRect(0, 0, width, height);
32  
  g.setColor(foreground);
33  
  g.setFont(font);
34  
  for i over lines:
35  
    g.drawString(lines.get(i), 0, lineHeight*i+y);
36  
  g.dispose();
37  
  
38  
  return img;
39  
}

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: 528 / 553
Version history: 6 change(s)
Referenced in: [show references]