Imported contents: kraptor_final_apr_03_2004.tar.gz
[kraptor.git] / src / wordwrap.c
blobed56c642f47c33fd4abf632c439275b698ccf8ae
1 /*--------------------------------------------------------
2 wordwrap.c
3 --------------------------------------------------------
4 Copyright (c) Kronoman
5 En memoria de mi querido padre
6 --------------------------------------------------------
7 Funciones para escribir texto con 'word-wrap'
8 --------------------------------------------------------*/
9 #ifndef KRONO_WORDWRAP_C
10 #define KRONO_WORDWRAP_C
13 #include <string.h>
14 #include "allegro.h"
16 #include "wordwrap.h"
19 Esta funcion imprime texto con word-wrap a 'w' caracteres...
20 NOTA: no puede usar palabras mayores a 1 Kb (1024 bytes!)
21 NOTA: no funciona muy bien con palabras muy largas (es decir, si no entran en el wrap, se salen del borde...)
22 NOTA: NO uso strtok porque se 'come' el separador de palabras!
23 NOTA: \n puede ser usado para saltar la linea
24 Parametros:
25 bmp = bitmap donde escribir
26 f = font a usar
27 x, y = coordenadas en pixeles
28 color = color a usar
29 w = ancho del borde, donde el texto 'cae' (wrap-edge)
30 text = texto a escribir
32 Devuelve:
33 0 si falla
34 si funciona, devuelve la coordenada 'y' inferior hasta donde imprimio
36 int imprimir_wordwrap(BITMAP *bmp,
37 const FONT *f,
38 int x, int y, int color, int w,
39 const char *text)
41 char tok[1024]; /* token actual */
42 char st[2]; /* caracter de busqueda (lo trato como string) */
43 char *limites = " ,;.<>()[]{}/*-+=\\|"; /* limitadores de token */
44 int i1, i2; /* auxiliares */
45 int xp = 0; /* posicion actual en pantalla */
47 xp = x;
48 i1 = 0;
50 if (w < text_length(f, "X")) return 0; /* evita bucle infinito, creo... :^P */
52 while (i1 < strlen(text))
54 /* buscar un token, e imprimirlo */
55 i2 = i1;
56 tok[0] = st[0] = st[1] = '\0'; /* blanquear strings */
58 do {
59 tok[i2-i1] = text[i2];
61 st[0] = text[i2]; /* caracter de comprobacion */
63 if (text[i2] == '\n') /* salto de linea */
65 xp = x;
66 y += text_height(f); // + 1;
67 break;
70 i2++;
71 } while (i2 < strlen(text) && !strstr(limites, st) && i2 - i1 < 1023 && x+text_length(f, tok) < x + w );
73 tok[i2-i1] = '\0';
75 /* avanzar: el +1 evita bucles infinitos, creo... */
76 i1 += (strlen(tok) > 0) ? strlen(tok) : 1;
78 /* Si la palabra llega al borde, salto */
79 if (xp+text_length(f, tok) > x + w)
81 xp = x;
82 y += text_height(f); // + 1;
85 textprintf(bmp, f, xp, y, color, "%s", tok);
87 xp += text_length(f, tok);
90 return y += text_height(f); // + 1;
92 #endif