libtthimage/Lib/text0.c

288 lines
6.7 KiB
C
Raw Normal View History

2022-06-27 07:55:56 +11:00
/*
text0.c
-------
ce module est en phase de 'reflexion'. je n'ai pas la moindre
idee sur l'organisation de la chose. je pense utiliser les
fontes bitmap de la console PC.
28 Fev 2001: now the standard font is loaded from the file
/usr/local/share/libimage.fonte
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include "../tthimage.h"
#define T_FONTE 2048
static uint8_t priv_fonte[T_FONTE];
2022-09-21 09:06:11 +11:00
static int font_is_loaded;
2022-06-27 07:55:56 +11:00
/*::------------------------------------------------------------------::*/
/*
* for now, parameter *ou must be NULL
*/
2022-09-17 20:23:21 +11:00
int Image_load_fnt8x8(char *nomfnt, uint8_t *ou, int flags)
2022-06-27 07:55:56 +11:00
{
int fd, foo;
2023-09-29 08:50:23 +11:00
#if DEBUG_LEVEL > 1
2022-09-17 20:23:21 +11:00
fprintf(stderr, ">>> %s ( '%s' %p %d )\n", __func__, nomfnt, ou, flags);
2022-06-27 07:55:56 +11:00
#endif
if (NULL != ou)
fprintf(stderr, "in %s, 'ou' (%p) must be NULL\n", __func__, ou);
if (0 != flags)
fprintf(stderr, "in %s, 'flags' (%d) must be 0\n", __func__, flags);
/* patch du 24 septembre 2015 */
if (NULL == nomfnt) {
nomfnt = "libimage.fonte";
2023-09-29 08:50:23 +11:00
#if DEBUG_LEVEL
fprintf(stderr, " using default font '%s'\n", nomfnt);
#endif
2022-06-27 07:55:56 +11:00
}
/* on pourrait aussi mettre la fonte par ddefaut dans l'environ */
2022-09-17 20:23:21 +11:00
if ( (fd=Image_must_open(nomfnt, O_RDONLY, 0)) < 0 ) {
2022-06-27 07:55:56 +11:00
perror (nomfnt);
exit(5);
}
2023-09-29 08:50:23 +11:00
/*
* Maybe add a check on the size of the file ?
*/
2022-06-27 07:55:56 +11:00
foo = read(fd, priv_fonte, T_FONTE);
2022-09-17 20:23:21 +11:00
if ( foo != T_FONTE ) {
2022-09-21 09:06:11 +11:00
fprintf(stderr, "%s: read only %d bytes from font file\n", __func__, foo);
2022-06-27 07:55:56 +11:00
exit(5);
}
close(fd);
#if DEBUG_LEVEL > 1
fprintf(stderr, "%s : font '%s' loaded\n", __func__, nomfnt);
#endif
2023-09-29 08:50:23 +11:00
font_is_loaded = 1;
2022-06-27 07:55:56 +11:00
return OLL_KORRECT;
}
/*::------------------------------------------------------------------::*/
/*
* XXX nasty bug in the alpha channel gestion XXX
*/
2022-09-17 20:23:21 +11:00
int Image_trace_caractere_2(Image_Desc *im, uint8_t *fnt, int x, int y, int code,
2022-06-27 07:55:56 +11:00
RGBA *paper, RGBA *ink)
{
int foo, bar, xi, yi;
int rp, gp, bp, rs, gs, bs;
uint8_t octet;
#if DEBUG_LEVEL > 2
fprintf(stderr, "trace car 2: xy= %4d %4d c= %c\n", x, y,
isprint(code) ? code : ' ');
#endif
2022-09-17 20:23:21 +11:00
if (NULL==fnt) {
2022-06-27 07:55:56 +11:00
fnt = priv_fonte; /* use local static storage */
}
2022-09-17 20:23:21 +11:00
for (foo=0; foo<8; foo++) {
2022-06-27 07:55:56 +11:00
yi = y+foo;
octet = fnt[(code*8)+foo];
2022-09-17 20:23:21 +11:00
for (bar=0; bar<8; bar++) {
2022-06-27 07:55:56 +11:00
xi = x+7-bar;
/* lecture contenu image */
rs = (im->Rpix[yi])[xi];
gs = (im->Gpix[yi])[xi];
bs = (im->Bpix[yi])[xi];
2022-09-17 20:23:21 +11:00
if (octet & 1) { /* ENCRE */
2022-06-27 07:55:56 +11:00
rp = ((rs*(255-ink->a))+(ink->r*ink->a)) / 255;
gp = ((gs*(255-ink->a))+(ink->g*ink->a)) / 255;
bp = ((bs*(255-ink->a))+(ink->b*ink->a)) / 255;
}
2022-09-17 20:23:21 +11:00
else { /* PAPIER */
2022-06-27 07:55:56 +11:00
rp = ((rs*(255-paper->a))+(paper->r*paper->a)) / 255;
gp = ((gs*(255-paper->a))+(paper->g*paper->a)) / 255;
bp = ((bs*(255-paper->a))+(paper->b*paper->a)) / 255;
}
(im->Rpix[yi])[xi] = rp;
(im->Gpix[yi])[xi] = gp;
(im->Bpix[yi])[xi] = bp;
octet >>= 1;
}
}
return 42;
}
/*::------------------------------------------------------------------::*/
/*
* fonction tr<EFBFBD>s primitive...
*/
2022-09-17 20:23:21 +11:00
int Image_trace_caractere(Image_Desc *im, int x, int y, int code)
2022-06-27 07:55:56 +11:00
{
int foo, bar;
uint8_t octet;
2022-09-17 20:23:21 +11:00
for (foo=0; foo<8; foo++) {
2022-06-27 07:55:56 +11:00
octet = priv_fonte[(code*8)+foo];
2022-09-17 20:23:21 +11:00
for (bar=0; bar<8; bar++) {
2022-06-27 07:55:56 +11:00
if (octet & 1)
Image_plotRGB(im, x+8-bar, y+foo, 255, 255, 255);
else
Image_plotRGB(im, x+8-bar, y+foo, 0, 0, 5);
octet >>= 1;
}
}
return 42;
}
/*::------------------------------------------------------------------::*/
/*
* trace d'une chaine. (param 'flag' not used)
* OBSOLETE FUNCTION !
*/
int Image_trace_chaine_0(Image_Desc *im, char *txt, int x, int y,
RGBA *paper, RGBA *ink, int flags)
{
int foo;
fprintf(stderr, "THE FUNCTION '%s' IS OBSOLETE\n", __func__);
if (0 != flags)
fprintf(stderr, "in %s, 'flags' (%d) must be 0\n", __func__, flags);
2023-09-29 08:50:23 +11:00
2022-06-27 07:55:56 +11:00
foo = Image_trace_chaine_1(im, txt, x, y, "libimage.fonte", paper, ink);
return foo;
}
/*::------------------------------------------------------------------::*/
/*
* trace d'une chaine avec choix eventuel de la fonte par le
* nom du fichier. Si celui-ci est NULL, c'est la fonte actuelle
* qui est utilise.
*/
int Image_trace_chaine_1(Image_Desc *im, char *txt, int x, int y,
char *nomfonte, RGBA *paper, RGBA *ink)
{
int posx, posy, t_texte, foo;
int octet;
uint8_t *ptrtxt;
RGBA blanc = { 255, 255, 255, 255, 0, 0 };
2023-09-29 08:50:23 +11:00
RGBA noir = { 0, 0, 0, 64, 0, 0 };
2022-06-27 07:55:56 +11:00
2023-09-29 08:50:23 +11:00
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p '%s' %d %d '%s' %p %p\n", __func__,
im, txt, x, y, nomfonte, paper, ink);
#endif
if (NULL == nomfonte) {
2022-06-27 07:55:56 +11:00
#if DEBUG_LEVEL > 1
fprintf(stderr, "trace chaine 1: using loaded font\n");
#endif
}
else {
#if DEBUG_LEVEL > 1
fprintf(stderr, "trace chaine 1: loading font '%s'\n", nomfonte);
#endif
foo = Image_load_fnt8x8(nomfonte, NULL, 0);
}
if (NULL==paper) paper = &blanc;
if (NULL==ink) ink = &noir;
2023-09-29 08:50:23 +11:00
#if DEBUG_LEVEL
2022-06-27 07:55:56 +11:00
Image_print_rgba("encre", ink, 0);
Image_print_rgba("papier", paper, 0);
#endif
t_texte = strlen(txt);
ptrtxt = (uint8_t *)txt;
#if DEBUG_LEVEL > 1
fprintf(stderr, "texte %3d %s\n", t_texte, txt);
fprintf(stderr, "papier %3d %3d %3d %3d\n", paper->r, paper->g, paper->b, paper->a);
fprintf(stderr, "encre %3d %3d %3d %3d\n", ink->r, ink->g, ink->b, ink->a);
#endif
posx = x; posy = y;
2022-09-17 20:23:21 +11:00
for (foo=0; foo<t_texte; foo++) {
2022-06-27 07:55:56 +11:00
octet = ptrtxt[foo];
2022-09-17 20:23:21 +11:00
if (posx > (im->width-8)) {
2024-08-11 20:21:23 +11:00
fprintf(stderr, "%s: can't plot char '%c' at x=%d\n",
__func__, octet, posx);
2022-06-27 07:55:56 +11:00
break;
}
Image_trace_caractere_2(im, priv_fonte, posx, posy, octet, paper, ink);
posx += 8;
}
return 0;
}
/*::------------------------------------------------------------------::*/
/* new --> Sun Feb 2 20:17:34 CET 2014 */
/*
* Don't forget to load a font, eg :
* Image_load_fnt8x8("8x8thin", NULL, 0);
*/
2022-09-21 09:06:11 +11:00
int Image_trace_big_char_0(Image_Desc *im, int x, int y, int code, int kx, int ky)
2022-06-27 07:55:56 +11:00
{
int foo, bar;
uint8_t octet;
Image_Rect rect;
RGBA paper, ink;
#if DEBUG_LEVEL
char buff[20];
if (isprint(code)) sprintf(buff, "'%c'", code);
else sprintf(buff, "%02xh", code);
fprintf(stderr, "%s ( %p %4d %4d %s %d %d )\n", __func__, im,
x, y, buff, kx, ky);
#endif
2022-09-21 09:06:11 +11:00
if ( (code < 0) || (code > 255)) {
2022-06-27 07:55:56 +11:00
fprintf(stderr, "in %s, code (%d) is invalid\n", __func__, code);
return BAD_PARAMETER;
}
2022-09-21 09:06:11 +11:00
/* Image_load_fnt8x8("8x8thin", NULL, 0); */
2022-06-27 07:55:56 +11:00
paper.r = paper.g = paper.b = 0;
2022-09-21 09:06:11 +11:00
ink.r = ink.g = ink.b = 255;
2022-06-27 07:55:56 +11:00
rect.w = kx, rect.h = ky;
2022-09-21 09:06:11 +11:00
/*
* NEED BOUNDARY CHECK !
*/
2024-08-11 20:21:23 +11:00
for (foo=0; foo<8; foo++) { /* 8 scan lines */
2022-06-27 07:55:56 +11:00
octet = priv_fonte[(code*8)+foo];
rect.y = (foo * ky) + y;
2024-08-11 20:21:23 +11:00
for (bar=0; bar<8; bar++) {
2022-06-27 07:55:56 +11:00
rect.x = (bar * kx) + x;
rect.x = ((8-bar)*kx) + x;
2024-08-11 20:21:23 +11:00
if (octet & 1) {
2022-06-27 07:55:56 +11:00
Image_paint_rect(im, &rect, 255, 198, 0);
Image_draw_rect(im, &rect, 0, 0, 80);
}
octet >>= 1;
}
}
im->modified++;
return FUNC_IS_BETA;
}
/*::------------------------------------------------------------------::*/