palib/source/arm9/PA_TextBits.c
2024-12-02 18:20:16 +00:00

163 lines
5.3 KiB
C
Raw Blame History

#include "PA_TextBits.h"
#include "PA_BmpFont0_Tiles_bin.h"
#include "PA_BmpFont1_Tiles_bin.h"
#include "PA_BmpFont2_Tiles_bin.h"
#include "PA_BmpFont3_Tiles_bin.h"
#include "PA_BmpFont4_Tiles_bin.h"
#include "PA_BmpFont0_Map_bin.h"
#include "PA_BmpFont1_Map_bin.h"
#include "PA_BmpFont2_Map_bin.h"
#include "PA_BmpFont3_Map_bin.h"
#include "PA_BmpFont4_Map_bin.h"
#include "PA_BmpFont0_Sizes_bin.h"
#include "PA_BmpFont1_Sizes_bin.h"
#include "PA_BmpFont2_Sizes_bin.h"
#include "PA_BmpFont3_Sizes_bin.h"
#include "PA_BmpFont4_Sizes_bin.h"
u16 *bittext_maps[10] = {
(u16 *)PA_BmpFont0_Map_bin,
(u16 *)PA_BmpFont1_Map_bin,
(u16 *)PA_BmpFont2_Map_bin,
(u16 *)PA_BmpFont3_Map_bin,
(u16 *)PA_BmpFont4_Map_bin
};
u8 *bittext_tiles_blank[10] = {
PA_BmpFont0_Tiles_bin,
PA_BmpFont1_Tiles_bin,
PA_BmpFont2_Tiles_bin,
PA_BmpFont3_Tiles_bin,
PA_BmpFont4_Tiles_bin
};
u8 *pa_bittextdefaultsize[10] = {
PA_BmpFont0_Sizes_bin,
PA_BmpFont1_Sizes_bin,
PA_BmpFont2_Sizes_bin,
PA_BmpFont3_Sizes_bin,
PA_BmpFont4_Sizes_bin
};
// Will be filled later
u16 pa_bittextsizes[5];
u8 pa_bittextpoliceheight[10];
LetterPos PA_LetterPos;
textinfo_type textinfo = {1, 0, ALIGN_LEFT, 0};
extern inline void PA_AddLetterPos(s16 Letter, s16 x, s16 y, u8 size, u16 color) {
PA_LetterPos.Letter[PA_LetterPos.NLetters].Letter = Letter;
PA_LetterPos.Letter[PA_LetterPos.NLetters].X = x;
PA_LetterPos.Letter[PA_LetterPos.NLetters].Y = y;
PA_LetterPos.Letter[PA_LetterPos.NLetters].Size = size;
PA_LetterPos.Letter[PA_LetterPos.NLetters].Color = color;
PA_LetterPos.NLetters++;
}
void PA_DoAlign(u16 start, s16 x, s16 maxx, u8 justify) {
s16 i;
s16 width = (maxx + 1) - x;
if (textinfo.align == ALIGN_RIGHT) { // Cas simple, on rajoute la diff<66>rence de largeur <20> toutes les lettres...
width++;
for (i = start; i < PA_LetterPos.NLetters; i++) PA_LetterPos.Letter[i].X += width;
} else if (textinfo.align == ALIGN_CENTER) { // Cas simple, on rajoute la moiti<74> de la largeur <20> toutes les lettres...
width = (width + 1) >> 1;
for (i = start; i < PA_LetterPos.NLetters; i++) PA_LetterPos.Letter[i].X += width;
} else if (justify && (textinfo.align == ALIGN_JUSTIFY)) { // Cas relou ^^
//u8 nletters = PA_LetterPos.NLetters-start; // Nombre de lettres
u8 justify = 0; // D<>callage <20> faire au d<>but...
u8 spaces = 0;
u8 change = 0;
u8 quickadd = 0;
u8 nletters = PA_LetterPos.NLetters - 1 - start;
if (nletters < width) { // Less letters than the width to add, add 1 pixel/letter...
quickadd = 1;
width -= nletters;
}
// Compter le nombre d'espaces
for (i = start; i < PA_LetterPos.NLetters - 1; i++) if (PA_LetterPos.Letter[i].Letter == ' ') spaces++;
for (i = start; i < PA_LetterPos.NLetters; i++) {
justify += quickadd;
PA_LetterPos.Letter[i].X += justify;
if ((PA_LetterPos.Letter[i].Letter == ' ') && width) {
change = (width + (spaces >> 1)) / spaces;
justify += change;
width -= change; // Moins <20> corriger par la suite...
spaces--;
}
}
}
}
void PA_DoLetterPos(s16 basex, s16 basey, s16 maxx, s16 maxy, const char* text, u16 color, u8 size, s32 limit) {
s16 x = basex; s16 y = basey;
s16 letterstart;
u8 lx, letter = ' ';
s16 i;
PA_LetterPos.NLetters = 0;
letterstart = 0;
if ((textinfo.align == ALIGN_JUSTIFY) || (textinfo.align == ALIGN_LEFT)) limit += 100; // Pour bien caler la ligne
for (i = 0; (i < limit + 100) && (text[i]); i++) {
letter = text[i];
lx = pa_bittextdefaultsize[size][letter] + textinfo.letterspacing;
if ((letter == '\n') || ((x + lx > maxx) && (letter != ' '))) { // retour ligne, ou on d<>passe...
if (letter == '\n')PA_AddLetterPos(letter, x, y, size, color); // Retour ligne <20> mettre dans le tableau
else if (letter == ' ') PA_AddLetterPos(letter, x, y, size, color); // Si espace, on met <20> la suite
else {
// Sinon, on revient en arri<72>re pour pas couper un mot !
while (text[i-1] != ' ') {
i--; x -= pa_bittextdefaultsize[size][(u8)text[i]] + textinfo.letterspacing; PA_LetterPos.NLetters--;
} // Retirer les lettres et la largeur
letter = text[i];
lx = pa_bittextdefaultsize[size][letter] + textinfo.letterspacing;
x--; // Moins de largeur
}
if (y + pa_bittextpoliceheight[size] * 2 > maxy) {
break; // EXIT !
}
// Calculer diff<66>rence de largeur...
if (text[i-1] == ' ') x -= pa_bittextdefaultsize[size][(u8)(' ')]; // Retirer la largeur de l'espace
// Retour ligne ! Voir si besoin d'aligne le texte d'un c<>t<EFBFBD> ou de l'autre, centrer, etc...
PA_DoAlign(letterstart, x, maxx, (letter != '\n')); // Autorise le justify que si pas '\'n
x = basex;
y += pa_bittextpoliceheight[size] + textinfo.linespacing;
letterstart = PA_LetterPos.NLetters; // Premi<6D>re lettre de cette ligne !
if (letter > 32) {
PA_AddLetterPos(letter, x, y, size, color);
x += lx;
}
} else {
PA_AddLetterPos(letter, x, y, size, color);
x += lx;
}
}
if (text[i-1] == ' ') x -= pa_bittextdefaultsize[size][(u8)(' ')]; // Retirer la largeur de l'espace
PA_DoAlign(letterstart, x, maxx, (letter != '\n') && (text[i] != '\0'));
// End Position
PA_LetterPos.Letter[PA_LetterPos.NLetters].X = x;
PA_LetterPos.Letter[PA_LetterPos.NLetters].Y = y;
}