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

71
LINES

< > BotCompany Repo | #1034602 // RenderText - renders text line from loaded font (e.g. TTF)

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (12539L) is out of date.

1  
sclass RenderText is MakesBufferedImage {
2  
  settable Color foregroundColor = Color.black;
3  
  settable Color backgroundColor = Color.white;
4  
  settable bool withLeading = true;
5  
  settable Font font = sansSerifFont(20);
6  
  settable Float fontSize; // null to keep size of font
7  
  settable double spacing; // extra spacing per character
8  
  settable S text = "DEMO";
9  
  settable S alignment = "left";
10  
  
11  
  Font scaledFont;
12  
  FontMetrics fm;
13  
  LS lines;
14  
  
15  
  *() {}
16  
  *(Font *font) {}
17  
  
18  
  selfType setFont(S fontSnippetID) {
19  
    ret setFont(fontForName(fontSnippetID));
20  
  }
21  
  
22  
  Font scaledFont() {
23  
    Font font = this.font;
24  
    if (spacing != 0) font = fontWithSpacing(spacing, font);
25  
    ret scaledFont = fontSize == null ? font : font.deriveFont(fontSize);
26  
  }
27  
  
28  
  BufferedImage get(S text) {
29  
    text(text);
30  
    ret this!;
31  
  }
32  
33  
  public BufferedImage get aka image aka getBufferedImage() {
34  
    fm = fontMetrics(scaledFont());
35  
    
36  
    lines = lines(text);
37  
    var lineWidths = map(lines, line -> fm.stringWidth(line));
38  
    int width = intMax(lineWidths);
39  
    if (width <= 0) null;
40  
    
41  
    int lineHeight = fm.getHeight(); 
42  
    int height = lineHeight*l(lines);
43  
    int y = withLeading
44  
      ? fm.getLeading() + fm.getMaxAscent()
45  
      : fm.getAscent();
46  
  
47  
    BufferedImage img = bufferedImageWithoutAlpha(width, height, backgroundColor);
48  
    
49  
    Graphics g = imageGraphics(img);
50  
    g.setColor(foregroundColor);
51  
    g.setFont(scaledFont);
52  
    for i over lines: {
53  
      int x = 0;
54  
      if (eq(alignment, "center"))
55  
        x = (width-lineWidths.get(i))/2;
56  
      g.drawString(lines.get(i), x, lineHeight*i+y);
57  
    }
58  
    g.dispose();
59  
  
60  
    ret img;
61  
  }
62  
63  
  // can the font display all the required characters?
64  
  bool canDisplay() {
65  
    ret canFontDisplay(font, text);
66  
  }
67  
  
68  
  selfType fontSize(float f) { ret fontSize((Float) f); }
69  
  
70  
  toString { ret "RenderText " + quote(shorten(text)); }
71  
}

Author comment

Began life as a copy of #1004566

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034602
Snippet name: RenderText - renders text line from loaded font (e.g. TTF)
Eternal ID of this version: #1034602/19
Text MD5: 017cf9997e413a0444ca08866f034bd6
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-02-06 14:40:27
Source code size: 1963 bytes / 71 lines
Pitched / IR pitched: No / No
Views / Downloads: 255 / 429
Version history: 18 change(s)
Referenced in: [show references]