feat: Implemented text

This commit is contained in:
Jakub Žitník 2025-03-15 11:31:08 +01:00
parent a8c309d199
commit c558b756de
Signed by: jzitnik
GPG Key ID: C577A802A6AF4EF3
4 changed files with 103 additions and 19 deletions

View File

@ -3,10 +3,53 @@ package cz.jzitnik.game.sprites.ui;
import cz.jzitnik.tui.ResourceLoader;
public class Font {
private String fullSprite = ResourceLoader.loadResource("ui/font.ans");
private final String[] lines = ResourceLoader.loadResource("ui/font.ans").split("\n");
private final int HEIGHT = 7;
public String getChar(char character) {
// This does not work idk what is wrong with meee
return null;
public String getChar(char character, String color) {
if (character == ' ') {
return ("\033[49m ".repeat(12) + "\n").repeat(HEIGHT);
}
boolean upper = Character.isUpperCase(character);
int width = upper ? 14 : 12;
int startY = (upper ? 0 : 1) * HEIGHT;
int startX = (upper ? character - 'A' : character - 'a') * width;
StringBuilder stringBuilder = new StringBuilder();
for (int y = startY; y < startY + HEIGHT; y++) {
String[] line = lines[y].split("\033");
for (int x = startX; x < startX + width - 1; x++) {
stringBuilder.append("\033").append(line[x].replaceAll("\\[(?!49m)[0-9;]+m", color));
}
stringBuilder.append("\033[0m\n");
}
return stringBuilder.toString();
}
public String getLine(String text) {
char[] chars = text.toCharArray();
String[][] letters = new String[chars.length][];
for (int i = 0; i < chars.length; i++) {
letters[i] = getChar(chars[i], "[47m").split("\n");
}
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < letters.length; j++) {
stringBuilder.append(letters[j][i]);
}
stringBuilder.append("\n");
}
return stringBuilder.toString();
}
}

View File

@ -7,7 +7,7 @@ import cz.jzitnik.game.sprites.ui.Font;
public class Escape {
public static void render(StringBuilder buffer, Terminal terminal) {
Font font = new Font();
String character = font.getChar('W');
String character = font.getLine("Hello world");
buffer.append(character);
}

View File

@ -1,14 +1,14 @@
                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                                                                                                       

View File

@ -0,0 +1,41 @@
const fs = require("fs");
const file = fs.readFileSync("./art.ans", "utf8");
const lines = file.split("\n");
const final = [];
for (const line of lines) {
let result = "";
let currentBg = "";
const regex = /(\x1b\[[0-9;]*m|.)/g;
const parts = line.match(regex) || [];
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
if (part.startsWith("\x1b[")) {
// Background color tracking
const params = part.slice(2, -1).split(";");
const isBg = params.some(p => p === "49" || p.startsWith("4"));
if (isBg) currentBg = part;
// Don't immediately output it — we'll output it *before* the next space
continue;
}
if (part === " ") {
// Always output color + space
if (currentBg) {
result += currentBg + " ";
} else {
result += " ";
}
} else {
result += part;
}
}
final.push(result);
}
fs.writeFileSync("./newart.ans", final.join("\n"), "utf8");