Per dubbi, consigli o richieste, potete mandare un'e-mail ad Andrea Carolfi.
Ringraziamo Amiga Transactor Mailing List per questo tangibile contributo!
Questa lezione sarà rivolta interamente alla gestione dei font tramite l'uso della graphics.library. Cominciamo quindi a vedere le strutture dati che saranno coinvolte in questa puntata.
struct TextAttr { STRPTR ta_Name; /* name of the font*/ UWORD ta_YSize; /* height of the font*/ UBYTE ta_Style; /* intrinsic font style*/ UBYTE ta_Flags; /* font preferences and flags*/ }; viene utilizzata per l'apertura di un font particolare richiesto dal programmatore. struct TextFont { struct Message tf_Message;/* reply message for font removal */ /* font name in LN \ used in this */ UWORD tf_YSize; /* font height | order to best */ UBYTE tf_Style; /* font style | match a font */ UBYTE tf_Flags; /* preferences and flags / request. */ UWORD tf_XSize; /* nominal font width */ UWORD tf_Baseline; /* distance from the top of char to baseline */ UWORD tf_BoldSmear; /* smear to affect a bold enhancement */ UWORD tf_Accessors; /* access count */ UBYTE tf_LoChar; /* the first character described here */ UBYTE tf_HiChar; /* the last character described here */ APTR tf_CharData; /* the bit character data */ UWORD tf_Modulo; /* the row modulo for the strike font data */ APTR tf_CharLoc; /* ptr to location data for the strike font */ /* 2 words: bit offset then size */ APTR tf_CharSpace; /* ptr to words of proportional spacing data */ APTR tf_CharKern; /* ptr to words of kerning data */ }; descrive un font. struct Rectangle { WORD MinX,MinY; WORD MaxX,MaxY; }; struct TextExtent { UWORD te_Width; /* same as TextLength */ UWORD te_Height; /* same as tf_YSize */ struct Rectangle te_Extent; /* relative to CP */ }; viene utilizzata da alcune procedure per memorizzare l'occupazione in pixel di un font o di una stringa di caratteri che utilizzano un font determinato.
Prima di poter utilizzare un font è necessario aprirlo tramite la funzione: struct TextFont *OpenFont( struct TextAttr *textAttr ); valorizzando la struttura TextAttr con i parametri del font richiesto. Per esempio: struct TextAttr tr = {"helvetica.font",15,FS_NORMAL,FPF_DISKFONT}; che aprirà il font helvetica alto 15 pixel. Una volta aperto il font, se vogliamo usarlo, dobbiamo informare la nostra rastport di questo con la procedura: void SetFont( struct RastPort *rp, struct TextFont *textFont ); Infine, una volta finito di utilizzare il font aperto (dopo aver reimpostato il precedente font usato dalla rastport), dovremo chiuderlo con la procedura: void CloseFont( struct TextFont *textFont );
void Text( struct RastPort *rp, STRPTR string, unsigned long count ); che riproduce sulla RastPort rp, alla posizione corrente (o modificata tramite una Move) count caratteri della stringa string. Un'operazione molto utile è sapere quanti pixel occupi un font, o una determinata stringa che utilizza un determinato font. Per saperlo, oltre ad utilizzare la IntuiTextLength della intuition.library, abbiamo a disposizione: WORD TextLength( struct RastPort *rp, STRPTR string, unsigned long count ); void TextExtent( struct RastPort *rp, STRPTR string, long count, struct TextExtent *textExtent ); e void FontExtent( struct TextFont *font, struct TextExtent *fontExtent );
La prima funzione, identica alla Text (come parametri) restituisce il
numero di pixel che la nostra stringa andrà ad occupare sulla RastPort.
Questa informazione è molto utile per gli scopi più disparati:
giustificazione del testo, allocazione di una bitmap per contenere il testo
e via di seguito. te_Width = 7 te_Height = 13 te_Extent.MinX = 0 te_Extent.MinY = -9 te_Extent.MaxX = 7 te_Extent.MaxY = 3 MaxX corrisponde con la larghezza del font.I valori
apparentemente strani sono MinY e MaxY. Il fatto è
che la base del font (baseline) è in basso, non in alto. Quindi quel
-9 indica l'elevazione massima rispetto alla base e quel 3 la
'sforatura' in basso rispetto alla base (tipicamente per i caratteri p,q,j e
simili). te_Width = 28 te_Height = 13 te_Extent.MinX = 0 te_Extent.MinY = -9 te_Extent.MaxX = 27 te_Extent.MaxY = 3 Oltre operazioni che possiamo fare sono: void ClearEOL( struct RastPort *rp ); per cancellare una riga di testo dalla posizione attuale del cursore grafico fino al margine destro della Rastport, void ClearScreen( struct RastPort *rp ); per cancellare dalla posizione attuale del cursore grafico fino in fondo alla RastPort. Infine abbiamo due funzioni per richiedere ed impostare lo stile di una font: ULONG AskSoftStyle( struct RastPort *rp ); ULONG SetSoftStyle( struct RastPort *rp, unsigned long style, unsigned long enable ); con queste due possiamo, dopo aver aperto ed impostato un font normale, richiedere la versione in grassetto oppure in corsivo o tutte le combinazioni possibili. In pratica: ULONG enable; struct RastPort *rp; [...] enable = AskSoftStyle(rp); SetSoftStyle(rp,FSF_BOLD,enable); [...] Infine, per sapere che font sta utilizzando una determinata RastPort, possiamo usare la: void AskFont( struct RastPort *rp, struct TextAttr *textAttr ); che valorizzera la struttura TextAttr passata, con i valori del font usato dalla RastPort rp.
|
![]() |
![]() |