/* test ttrender */

#define __NOLIBBASE__

#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <proto/ttengine.h>
#include <proto/asl.h>

#include <libraries/ttengine.h>

extern struct Library *SysBase, *DOSBase;

/*----------------------------------------------------------------------------------------------------*/

static STRPTR get_font_name(struct Library *AslBase)
  {
    struct FileRequester *freq;
    STRPTR name = NULL;

    if (freq = AllocAslRequestTags(ASL_FileRequest, TAG_END))
      {
        if (AslRequestTags(freq,
          ASLFR_TitleText, (ULONG)"Select TrueType font",
          ASLFR_InitialDrawer, (ULONG)"FONTS:",
          ASLFR_DoPatterns, TRUE,
          ASLFR_InitialPattern, (ULONG)"#?.ttf",
          ASLFR_RejectIcons, TRUE,
          TAG_END))
          {
            ULONG namelen = strlen(freq->fr_File) + strlen(freq->fr_Drawer) + 4;

            if (name = AllocVec(namelen + 1, MEMF_ANY | MEMF_CLEAR))
              {
                strncpy(name, freq->fr_Drawer, namelen);
                AddPart(name, freq->fr_File, namelen);
              }
          }
        FreeAslRequest(freq);
      }
    return name;
  }

/*----------------------------------------------------------------------------------------------------*/

static VOID free_font_name(STRPTR name)
  {
    if (name) FreeVec(name);
  }

/*----------------------------------------------------------------------------------------------------*/

UWORD tx[] = {'T', 'e', 's', 't', ' ', '1', '6', '-', 'b', 'i', 't', ' ', 'U', 'n', 'i',
 'c', 'o', 'd', 'e', ' ', 's', 't', 'r', 'i', 'n', 'g', '.', 0x0000};

int Main (void)
  {
    struct Library *TTEngineBase, *IntuitionBase, *GfxBase, *AslBase;
    struct Window *win;
    STRPTR fontname;
    APTR font;

    if (GfxBase = OpenLibrary("graphics.library", 39))
      {
        if (IntuitionBase = OpenLibrary("intuition.library", 39))
          {
            if (AslBase = OpenLibrary("asl.library", 38))
              {
                if (fontname = get_font_name(AslBase))
                  {
                    if (TTEngineBase = OpenLibrary("ttengine.library", 4))
                      {
                        if (win = OpenWindowTags(NULL,
                          WA_Top, 25,
                          WA_Left, 0,
                          WA_Width, 640,
                          WA_Height, 350,
                          WA_CloseGadget, TRUE,
                          WA_DragBar, TRUE,
                          WA_DepthGadget, TRUE,
                          WA_IDCMP, IDCMP_CLOSEWINDOW,
                          WA_Title, (ULONG)"ttengine.library test",
                          TAG_END))
                          {
                            ULONG sigmask, signals;
                            BOOL running = TRUE;
                            struct RastPort *rp = win->RPort;

                            if (font = TT_OpenFont(
                              TT_FontFile, (ULONG)fontname,
                              TT_FontSize, 24,
                            TAG_END))
                              {
                                TT_SetFont(rp, font);
                                TT_SetAttrs(rp, TT_Window, (ULONG)win, TAG_END);

                                SetAPen(rp, 2);
                                SetDrMd(rp, JAM1);
                                Move(rp, 10, 50);
                                TT_Text(rp, "This is a text printed with TT_Text().", 38);

                                TT_SetAttrs(rp,
                                  TT_Antialias, TT_Antialias_Off,
                                  TT_Encoding, TT_Encoding_Unicode,
                                TAG_END);

                                SetDrMd(rp, JAM2);
                                SetBPen(rp, 3);
                                Move(rp, 10, 74);
                                TT_Text(rp, tx, 27);

                                TT_CloseFont(font);
                              }
                            else PutStr("Font open failed.\n");

                            if (font = TT_OpenFont(
                              TT_FontFile, (ULONG)fontname,
                              TT_FontSize, 36,
                            TAG_END))
                              {
                                TT_SetFont(rp, font);
                                TT_SetAttrs(rp,
                                  TT_Encoding, TT_Encoding_Default,
                                  TT_Antialias, TT_Antialias_On,
                                  TT_Window, (ULONG)win,
                                TAG_END);

                                SetAPen(rp, 1);
                                SetBPen(rp, 2);
                                SetDrMd(rp, JAM2);
                                Move(rp, 10, 120);
                                TT_Text(rp, "Antialias with 36 points size.", 30);

                                TT_CloseFont(font);
                              }
                            else PutStr("Font open failed.\n");

                            if (font = TT_OpenFont(
                              TT_FontFile, (ULONG)fontname,
                              TT_FontSize, 16,
                            TAG_END))
                              {
                                TT_SetFont(rp, font);
                                TT_SetAttrs(rp,
                                  TT_Encoding, TT_Encoding_Default,
                                  TT_Antialias, TT_Antialias_On,
                                  TT_Window, (ULONG)win,
                                TAG_END);

                                SetAPen(rp, 1);
                                SetDrMd(rp, JAM1);
                                Move(rp, 10, 140);
                                TT_Text(rp, "Antialias with 16 points size.", 30);
                                SetAPen(rp, 2);
                                Move(rp, 10, 156);
                                TT_Text(rp, "Antialias with 16 points size.", 30);
                                SetAPen(rp, 3);
                                Move(rp, 10, 172);
                                TT_Text(rp, "Antialias with 16 points size.", 30);

                                TT_CloseFont(font);
                              }
                            else PutStr("Font open failed.\n");

                            /* antialias on different backgrounds */

                            if (font = TT_OpenFont(
                              TT_FontFile, (ULONG)fontname,
                              TT_FontSize, 24,
                            TAG_END))
                              {
                                ULONG red, grn, blu;

                                red = ObtainBestPen(win->WScreen->ViewPort.ColorMap, 0xFFFFFFFF, 0, 0, TAG_END);
                                grn = ObtainBestPen(win->WScreen->ViewPort.ColorMap, 0, 0xFFFFFFFF, 0, TAG_END);
                                blu = ObtainBestPen(win->WScreen->ViewPort.ColorMap, 0, 0, 0xFFFFFFFF, TAG_END);

                                TT_SetFont(rp, font);
                                TT_SetAttrs(rp,
                                  TT_Encoding, TT_Encoding_Default,
                                  TT_Antialias, TT_Antialias_On,
                                  TT_Window, (ULONG)win,
                                TAG_END);

                                SetDrMd(rp, JAM2);

                                SetBPen(rp, grn);
                                Move(rp, 10, 200);
                                SetAPen(rp, red);
                                TT_Text(rp, "Smooth47", 8);
                                SetAPen(rp, blu);
                                TT_Text(rp, "Smooth47", 8);
                                SetAPen(rp, 1);
                                TT_Text(rp, "Smooth47", 8);
                                SetAPen(rp, 2);
                                TT_Text(rp, "Smooth47", 8);

                                SetBPen(rp, red);
                                Move(rp, 10, 224);
                                SetAPen(rp, grn);
                                TT_Text(rp, "Smooth47", 8);
                                SetAPen(rp, blu);
                                TT_Text(rp, "Smooth47", 8);
                                SetAPen(rp, 1);
                                TT_Text(rp, "Smooth47", 8);
                                SetAPen(rp, 2);
                                TT_Text(rp, "Smooth47", 8);

                                SetBPen(rp, blu);
                                Move(rp, 10, 248);
                                SetAPen(rp, red);
                                TT_Text(rp, "Smooth47", 8);
                                SetAPen(rp, grn);
                                TT_Text(rp, "Smooth47", 8);
                                SetAPen(rp, 1);
                                TT_Text(rp, "Smooth47", 8);
                                SetAPen(rp, 2);
                                TT_Text(rp, "Smooth47", 8);

                                SetBPen(rp, 1);
                                Move(rp, 10, 272);
                                SetAPen(rp, red);
                                TT_Text(rp, "Smooth47", 8);
                                SetAPen(rp, grn);
                                TT_Text(rp, "Smooth47", 8);
                                SetAPen(rp, blu);
                                TT_Text(rp, "Smooth47", 8);
                                SetAPen(rp, 2);
                                TT_Text(rp, "Smooth47", 8);

                                SetBPen(rp, 2);
                                Move(rp, 10, 296);
                                SetAPen(rp, red);
                                TT_Text(rp, "Smooth47", 8);
                                SetAPen(rp, grn);
                                TT_Text(rp, "Smooth47", 8);
                                SetAPen(rp, 1);
                                TT_Text(rp, "Smooth47", 8);
                                SetAPen(rp, blu);
                                TT_Text(rp, "Smooth47", 8);

                                ReleasePen(win->WScreen->ViewPort.ColorMap, red);
                                ReleasePen(win->WScreen->ViewPort.ColorMap, grn);
                                ReleasePen(win->WScreen->ViewPort.ColorMap, blu);

                                TT_CloseFont(font);
                              }
                            else PutStr("Font open failed.\n");

                            sigmask = SIGBREAKF_CTRL_C | (1 << win->UserPort->mp_SigBit);
                            while (running)
                              {
                                signals = Wait(sigmask);
                                if (signals & SIGBREAKF_CTRL_C) running = FALSE;
                                if (signals & (1 << win->UserPort->mp_SigBit))
                                  {
                                    struct IntuiMessage *imsg;

                                    while (imsg = (struct IntuiMessage*)GetMsg(win->UserPort))
                                      {
                                        if (imsg->Class == IDCMP_CLOSEWINDOW) running = FALSE;
                                        ReplyMsg((struct Message*)imsg);
                                      }
                                  }
                              }
                            CloseWindow(win);
                          }
                        CloseLibrary(TTEngineBase);
                      }
                    free_font_name(fontname);
                  }
                CloseLibrary(AslBase);
              }
            CloseLibrary(IntuitionBase);
          }
        CloseLibrary(GfxBase);
      }
    return 0;
  }
