; Description:  Example of using the palette information in Colorfonts.
;               You will need (version II) amigalibs.res resident.
;               You also need NeilsReqToolsLib
; Type;         Intuition

WBStartup
DEFTYPE.w

LoadFont 0,"topaz.font",8
MaxLen fi$ = 256

RTEZSetPattern 0,"#?.font"
fontname$=RTEZLoadFile("Select a font file",fi$)
If LCase$(Right$(fontname$,5))=".font"
    RTEZSetPattern 0,"#?"
    RTEZSetDefaultDirectory 0,UnLeft$(fontname$,5)
    fontsize = RTEZGetLongRange("Request","Enter font size in points",10,127,50)
Else
    fontsize = RTEZRequest("Error","You did not select a font file","OK")
    Free IntuiFont 0
    End
End If

; Have to use OS functions for colorfonts, as Blitz sucks
ta0.TextAttr\ta_Name = &fontname$, fontsize, #FSF_COLORFONT, #FPF_DISKFONT
*cfont.ColorTextFont = OpenDiskFont_(&ta0)

; Try to get palette information from font. First of all, check the style
; of the font, making sure that it is a colorfont. If it is then go on to
; do palette stuff.
If (*cfont = 0) Then End

If (*cfont\ctf_TF\tf_Style & #FSF_COLORFONT) = #FSF_COLORFONT
    fontdepth.w = *cfont\ctf_Depth                   ; Number of bitplanes in font
    remapcolor.w = *cfont\ctf_FgColor & $FF                   ; This color will be drawn as current foreground color
    locolorindex.w = *cfont\ctf_Low & $FF                     ; These two supposedly important for greyscale
    hicolorindex.w = *cfont\ctf_High & $FF
    *cfpalette.ColorFontColors = *cfont\ctf_ColorFontColors

    ; Get colorfont palette data. Just get all colors from palette. You could
    ; also use locolor To hicolor to get only the colors that the font uses
    For i.w = 0 To *cfpalette\cfc_Count
        colorinfo.w = Peek.w(*cfpalette\cfc_ColorTable + i * SizeOf.w)
        fontred.w   = (colorinfo & $0F00) LSR 8
        fontgreen.w = (colorinfo & $00F0) LSR 4
        fontblue.w  = (colorinfo & $000F)
        PalRGB 1,i,fontred,fontgreen,fontblue
    Next
End If

; Open our display
Screen 0,0,0,640,200,fontdepth,$8000,"Coloured screen",-1,-1
Window 0,0,11,640,180,$100f,"Font display",-1,-1

; Set the font to topaz (so that the font information can be read properly)
WindowFont 0
NPrint "Blitz sucks. So does topaz."

NPrint "Depth = ", fontdepth
NPrint "Color to remap = ", remapcolor
NPrint "Color limits = ", locolorindex, ", ", hicolorindex

; Check palette type flags
If ((*cfont\ctf_Flags & #CT_COLORFONT) = #CT_COLORFONT) Then NPrint "Palette is colours"
If ((*cfont\ctf_Flags & #CT_GREYFONT) = #CT_GREYFONT) Then NPrint "Palette is even stepped greyscale from "

; Check antialias flag
If ((*cfont\ctf_Flags & #CT_ANTIALIAS) = #CT_ANTIALIAS) Then NPrint "Font graphics are anti aliased"

; There is also a flag for remapping the ctf_FgColor, this is the same as
; #CT_COLORFONT but is only used if ctf_FgColor is within the color limits
; of the font
If (*cfont\ctf_Flags & #CTF_MAPCOLOR) = #CTF_MAPCOLOR
    If (remapcolor >= locolor AND remapcolor <= hicolor) Then NPrint "Color ",remapcolor," is remapped to current foreground"
End If

; Let user see the font information before changing palette
NPrint "Click mouse button to use colorfont's palette"
While ev.l<>#IDCMP_MOUSEBUTTONS
    ev=WaitEvent
Wend

; Print some text using colorfont and its colours
ShowPalette 1
SetFont_ RastPort(0),*cfont
sample$ = "Hello, world!"
Move_ RastPort(0), 4, 64 + fontsize + WTopOff
Text_ RastPort(0), &sample$, Len(sample$)

While ev.l<>#IDCMP_CLOSEWINDOW
    ev=WaitEvent
Wend

CloseWindow 0
CloseScreen 0

If *cfont
    CloseFont_ *cfont
    *cfont = 0
End If
Free IntuiFont 0

End


