1 | sclass RenderText { |
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 = 20; |
7 | settable S text = "DEMO"; |
8 | |
9 | Font scaledFont; |
10 | FontMetrics fm; |
11 | |
12 | *() {} |
13 | *(Font *font) {} |
14 | |
15 | selfType setFont(S fontSnippetID) { |
16 | ret setFont(loadFont_cached(fontSnippetID)); |
17 | } |
18 | |
19 | Font scaledFont() { ret scaledFont = font.deriveFont(fontSize); } |
20 | |
21 | BufferedImage get(S text) { |
22 | text(text); |
23 | ret this!; |
24 | } |
25 | |
26 | BufferedImage get() { |
27 | fm = fontMetrics(scaledFont()); |
28 | int width = fm.stringWidth(text); |
29 | if (width <= 0) null; |
30 | int height = fm.getHeight(); |
31 | int y = withLeading |
32 | ? fm.getLeading() + fm.getMaxAscent() |
33 | : fm.getAscent(); |
34 | |
35 | BufferedImage img = bufferedImageWithoutAlpha(width, height, backgroundColor); |
36 | |
37 | Graphics g = imageGraphics(img); |
38 | g.setColor(foregroundColor); |
39 | g.setFont(scaledFont); |
40 | g.drawString(text, 0, y); |
41 | g.dispose(); |
42 | |
43 | ret img; |
44 | } |
45 | } |
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: | 131 / 154 |
Referenced in: | [show references] |