cap fixed

This commit is contained in:
lolcat 2024-12-31 01:01:53 -05:00
parent 469320c31a
commit 1431cd3d1b
1 changed files with 101 additions and 27 deletions

128
client.js
View File

@ -1721,45 +1721,119 @@ deek.on("message", async function(msg, user){
var fontsize = Math.floor(image.width * 0.08);
ctx.font = fontsize + "px Impact";
ctx.fillStyle = "white";
ctx.lineJoin = "round";
ctx.lineCap = "round";
ctx.strokeStyle = "black";
ctx.lineWidth = image.width * 0.01;
ctx.textAlign = "center";
ctx.textBaseline = "top";
ctx.textBaseline = "bottom";
var parts = value.replace(/ +/g, " ").split("");
var tmp_text = "";
var tmp_text_old = "";
var text_to_add = "";
var line = 0;
for(var i=0; i<parts.length; i++){
value = value.split("|");
wrap(value[0], false);
if(typeof value[1] != "undefined"){
tmp_text_old = tmp_text;
tmp_text += parts[i];
wrap(value[1], true);
}
function wrap(text, is_bottom){
var just_pushed_line = false;
var words = text.split(" ");
var lines = [];
var line = "";
var line_tmp = "";
var metrics = ctx.measureText(tmp_text.trim());
if(
metrics.width >= image.width ||
parts.length - 1 === i
){
// draw
if(metrics.width >= image.width){
for(var i=0; i<words.length; i++){
var word_width = ctx.measureText(words[i]).width;
if(word_width >= image.width){
// put text on next line since it overflows
tmp_text_old = tmp_text_old.trim();
tmp_text = parts[i];
if(line != ""){
lines.push(line.trim());
line = "";
}
line_tmp = "";
// increment letters till we go over the limit
var letters = words[i].split("");
for(var k=0; k<letters.length; k++){
line_tmp += letters[k];
if(ctx.measureText(line_tmp).width >= image.width){
lines.push(line.trim());
line = letters[k];
line_tmp = letters[k];
}else{
line += letters[k];
}
}
// output remainder
lines.push(line);
line = "";
line_tmp = "";
text_to_add = tmp_text_old;
}else{
text_to_add = tmp_text;
// wrap word normally
line_tmp += words[i] + " ";
if(ctx.measureText(line_tmp.trim()).width >= image.width){
// push line
lines.push(line.trim());
line = "";
line_tmp = words[i] + " ";
}
line += words[i] + " ";
}
}
// output remainder
if(line != ""){
lines.push(line);
line = "";
line_tmp = "";
}
var tmp_metrics = ctx.measureText(lines[i]);
var space = (tmp_metrics.actualBoundingBoxAscent + tmp_metrics.actualBoundingBoxDescent) / 3;
if(is_bottom){
height = image.height - space;
lines = lines.reverse();
}else{
height = space;
}
for(var i=0; i<lines.length; i++){
var metrics = ctx.measureText(lines[i]);
if(is_bottom){
if(i !== 0){
height -= space + metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent;
}
}else{
height += space + metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent;
}
var textheight = metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent;
ctx.strokeText(text_to_add, image.width / 2, textheight * line);
ctx.fillText(text_to_add, image.width / 2, textheight * line);
line++;
ctx.strokeText(lines[i], image.width / 2, height);
ctx.fillText(lines[i], image.width / 2, height);
}
}