/* test ttrender */

#define __NOLIBBASE__

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

#include <libraries/ttrender.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:_TrueType_Outlines/",
          ASLFR_DoPatterns, TRUE,
          ASLFR_InitialPattern, (ULONG)"#?.ttf",
          ASLFR_RejectIcons, TRUE,
          TAG_END))
          {
            ULONG namelen = strlen(freq->fr_File) - 4;

            if (name = AllocVec(namelen + 1, MEMF_ANY | MEMF_CLEAR))
              {
                strncpy(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 *TTRenderBase, *IntuitionBase, *GfxBase, *AslBase;
    struct Window *win;
    STRPTR fontname;

    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 (TTRenderBase = OpenLibrary("ttrender.library", 0))
                      {
                        if (win = OpenWindowTags(NULL,
                          WA_Top, 25,
                          WA_Left, 0,
                          WA_Width, 640,
                          WA_Height, 250,
                          WA_CloseGadget, TRUE,
                          WA_DragBar, TRUE,
                          WA_DepthGadget, TRUE,
                          WA_IDCMP, IDCMP_CLOSEWINDOW,
                          WA_Title, (ULONG)"ttrender.library test",
                          TAG_END))
                          {
                            ULONG sigmask, signals;
                            BOOL running = TRUE;
                            struct RastPort *rp = win->RPort;

                            TT_SetModesTags(TTA_Window, (ULONG)win, TAG_END);
                            TT_SetFont(fontname, 36);
                            SetAPen(rp, 1);
                            SetDrMd(rp, JAM1);
                            Move(rp, 10, 50);
                            TT_PutStr("This is a text printed with TT_PutStr().");
                            SetAPen(rp, 2);
                            Move(rp, 10, 86);
                            TT_PutUStr(tx);
                            Move(rp, 10, 122);
                            SetAPen(rp, 3);
                            TT_PutStrTags("Local antialias on.", TTA_Antialias, TRUE, TAG_END);
                            Move(rp, 10, 134);
                            TT_SetFont(fontname, 14);
                            Move(rp, 10, 154);
                            SetAPen(rp, 1);
                            TT_PutStr("Font size change. 14 points for now. This text can be a bit longer. Kerning sample: AVAVAVATA.");
                            SetAPen(rp, 2);
                            SetBPen(rp, 1);
                            SetDrMd(rp, JAM2);
                            Move(rp, 10, 180);
                            TT_SetFont(fontname, 21);
                            TT_PutStrTags("Test of JAM2 RastPort mode, 21 pixels size, antialias on.",
                              TTA_Antialias, TRUE,
                              TAG_END);
                            Move(rp, 10, 201);
                            TT_PutStrTags("JAM2 background filling - there should be no background between the lines.",
                              TTA_Antialias, TRUE,
                              TAG_END);
                            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(TTRenderBase);
                      }
                    free_font_name(fontname);
                  }
                CloseLibrary(AslBase);
              }
            CloseLibrary(IntuitionBase);
          }
        CloseLibrary(GfxBase);
      }
    return 0;
  }
