home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 19 / Amiga Plus Leser CD 19.iso / Tools / Freeware / ttengine-5.0 / Examples / Scaling / scaling.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-11-05  |  9.9 KB  |  330 lines

  1. /* test ttrender */
  2.  
  3. #define __NOLIBBASE__
  4.  
  5. #include <proto/dos.h>
  6. #include <proto/exec.h>
  7. #include <proto/intuition.h>
  8. #include <proto/graphics.h>
  9. #include <proto/ttengine.h>
  10. #include <proto/asl.h>
  11. #include <proto/datatypes.h>
  12. #include <proto/mathieeesingbas.h>
  13. #include <datatypes/pictureclass.h>
  14. #include <utility/hooks.h>
  15.  
  16. #include <libraries/ttengine.h>
  17.  
  18. #define reg(a) __asm(#a)
  19.  
  20. extern struct Library *SysBase, *DOSBase;
  21.  
  22. struct ResizeInfo
  23.   {
  24.     WORD oldw, oldh, bline;
  25.   };
  26.  
  27. struct Library *TTEngineBase, *IntuitionBase, *GfxBase, *AslBase, *DataTypesBase,
  28.   *MathIeeeSingBasBase;
  29. struct BitMap *PicBitMap;
  30. Object *Picture;
  31. struct TextExtent te;
  32.  
  33. /*----------------------------------------------------------------------------------------------------*/
  34.  
  35. static STRPTR get_font_name(struct Library *AslBase)
  36.   {
  37.     struct FileRequester *freq;
  38.     STRPTR name = NULL;
  39.  
  40.     if (freq = AllocAslRequestTags(ASL_FileRequest, TAG_END))
  41.       {
  42.         if (AslRequestTags(freq,
  43.           ASLFR_TitleText, (ULONG)"Select TrueType font",
  44.           ASLFR_InitialDrawer, (ULONG)"FONTS:",
  45.           ASLFR_DoPatterns, TRUE,
  46.           ASLFR_InitialPattern, (ULONG)"#?.ttf",
  47.           ASLFR_RejectIcons, TRUE,
  48.           TAG_END))
  49.           {
  50.             ULONG namelen = strlen(freq->fr_File) + strlen(freq->fr_Drawer) + 4;
  51.  
  52.             if (name = AllocVec(namelen + 1, MEMF_ANY | MEMF_CLEAR))
  53.               {
  54.                 strncpy(name, freq->fr_Drawer, namelen);
  55.                 AddPart(name, freq->fr_File, namelen);
  56.               }
  57.           }
  58.         FreeAslRequest(freq);
  59.       }
  60.     return name;
  61.   }
  62.  
  63. /*----------------------------------------------------------------------------------------------------*/
  64.  
  65. static VOID free_font_name(STRPTR name)
  66.   {
  67.     if (name) FreeVec(name);
  68.   }
  69.  
  70. /*----------------------------------------------------------------------------------------------------*/
  71.  
  72. BOOL init(VOID)
  73.   {
  74.     if (!(GfxBase = OpenLibrary("graphics.library", 39))) return FALSE;
  75.     if (!(IntuitionBase = OpenLibrary("intuition.library", 39))) return FALSE;
  76.     if (!(AslBase = OpenLibrary("asl.library", 38))) return FALSE;
  77.     if (!(TTEngineBase = OpenLibrary("ttengine.library", 4))) return FALSE;
  78.     if (!(DataTypesBase = OpenLibrary("datatypes.library", 39))) return FALSE;
  79.     if (!(MathIeeeSingBasBase = OpenLibrary("mathieeesingbas.library", 38))) return FALSE;
  80.     return TRUE;
  81.   }
  82.  
  83. /*----------------------------------------------------------------------------------------------------*/
  84.  
  85. VOID cleanup(VOID)
  86.   {
  87.     if (MathIeeeSingBasBase) CloseLibrary(MathIeeeSingBasBase);
  88.     if (DataTypesBase) CloseLibrary(DataTypesBase);
  89.     if (TTEngineBase) CloseLibrary(TTEngineBase);
  90.     if (AslBase) CloseLibrary(AslBase);
  91.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  92.     if (GfxBase) CloseLibrary(GfxBase);
  93.     return;
  94.   }
  95.  
  96. /*----------------------------------------------------------------------------------------------------*/
  97.  
  98. VOID load_picture(struct Screen *s)
  99.   {
  100.     if (Picture = NewDTObject("PROGDIR:background",
  101.       DTA_GroupID,    GID_PICTURE,
  102.       PDTA_Remap,     TRUE,
  103.       PDTA_DestMode,  PMODE_V43,
  104.       PDTA_Screen,    (ULONG)s,
  105.       TAG_END))
  106.       {
  107.         DoDTMethod(Picture, NULL, NULL, DTM_PROCLAYOUT, NULL, DTSIF_NEWSIZE);
  108.         GetDTAttrs(Picture, PDTA_DestBitMap, (ULONG)&PicBitMap, TAG_END);
  109.       }
  110.     return;
  111.   }
  112.  
  113. /*----------------------------------------------------------------------------------------------------*/
  114.  
  115. struct BfMsg
  116.   {
  117.     struct Layer        *layer;
  118.     struct Rectangle     bounds;
  119.     LONG                 xoffs;
  120.     LONG                 yoffs;
  121.   };
  122.  
  123. __saveds LONG bf(struct RastPort *rp reg(a2), struct BfMsg *msg reg(a1))
  124.   {
  125.     WORD lenx, srcx, destx, ix;
  126.     WORD leny, srcy, desty, iy;
  127.  
  128.     for (ix = (msg->bounds.MinX & 0xFFC0); ix <= msg->bounds.MaxX; ix += 64)
  129.       {
  130.         destx = ix;
  131.         srcx = 0;
  132.         lenx = 64;
  133.  
  134.         if (destx < msg->bounds.MinX)
  135.           {
  136.             destx = msg->bounds.MinX;
  137.             srcx = destx & 0x3F;
  138.             lenx = 64 - srcx;
  139.           }
  140.  
  141.         if (destx + 64 > msg->bounds.MaxX)
  142.           {
  143.             lenx = msg->bounds.MaxX - destx + 1;
  144.           }
  145.  
  146.         for (iy = (msg->bounds.MinY & 0xFFC0); iy <= msg->bounds.MaxY; iy += 64)
  147.           {
  148.             desty = iy;
  149.             srcy = 0;
  150.             leny = 64;
  151.  
  152.             if (desty < msg->bounds.MinY)
  153.               {
  154.                 desty = msg->bounds.MinY;
  155.                 srcy = desty & 0x3F;
  156.                 leny = 64 - srcy;
  157.               }
  158.  
  159.             if (desty + 64 > msg->bounds.MaxY)
  160.               {
  161.                 leny = msg->bounds.MaxY - desty + 1;
  162.               }
  163.  
  164.             BltBitMap(PicBitMap, srcx, srcy, rp->BitMap, destx, desty, lenx, leny,
  165.               0xC0, 0xFF, NULL);
  166.           }
  167.  
  168.       }
  169.     return 0;
  170.   }
  171.  
  172. struct Hook bf_hook = {NULL, NULL, (HOOKFUNC)bf, NULL, NULL};
  173.  
  174. /*----------------------------------------------------------------------------------------------------*/
  175.  
  176. ULONG initialize_window(struct Window *win, APTR font, struct TextExtent *te)
  177.   {
  178.     WORD x, y;
  179.     ULONG pen;
  180.  
  181.     x = win->BorderLeft - te->te_Extent.MinX + 5;
  182.     y = win->BorderTop - te->te_Extent.MinY + 5;
  183.  
  184.     pen = ObtainBestPen(win->WScreen->ViewPort.ColorMap, 0xEEEEEEEE, 0xBBBBBBBB, 0,
  185.       OBP_Precision, PRECISION_IMAGE,
  186.     TAG_END);
  187.     TT_SetFont(win->RPort, font);
  188.     TT_SetAttrs(win->RPort,
  189.       TT_Window, (ULONG)win,
  190.     TAG_END);
  191.     Move(win->RPort, x, y);
  192.     SetAPen(win->RPort, pen);
  193.     SetDrMd(win->RPort, JAM1);
  194.     TT_Text(win->RPort, "Font scaling", 12);
  195.     return pen;
  196.   }
  197.  
  198. /*----------------------------------------------------------------------------------------------------*/
  199.  
  200. VOID resize_text(struct Window *win)
  201.   {
  202.     WORD x, y;
  203.     WORD neww, newh, newbline, newxdelta;
  204.     FLOAT sclx, scly;
  205.  
  206.     neww = win->Width - win->BorderLeft - win->BorderRight - 10;
  207.     newh = win->Height - win->BorderTop - win->BorderBottom - 10;
  208.  
  209.     sclx = IEEESPDiv(IEEESPFlt(neww),IEEESPFlt(te.te_Extent.MaxX - te.te_Extent.MinX + 1));
  210.     scly = IEEESPDiv(IEEESPFlt(newh),IEEESPFlt(te.te_Extent.MaxY - te.te_Extent.MinY + 1));
  211.     newbline = IEEESPFix(IEEESPMul(IEEESPFlt(-te.te_Extent.MinY), scly));
  212.     newxdelta = IEEESPFix(IEEESPMul(IEEESPFlt(te.te_Extent.MinX), sclx));
  213.  
  214.     TT_SetAttrs(win->RPort,
  215.       TT_ScaleX, *((ULONG*)&sclx),
  216.       TT_ScaleY, *((ULONG*)&scly),
  217.     TAG_END);
  218.  
  219.     EraseRect(win->RPort, win->BorderLeft, win->BorderTop, win->Width - win->BorderRight,
  220.       win->Height - win->BorderBottom);
  221.     Move(win->RPort, win->BorderLeft - newxdelta + 5, win->BorderTop + newbline + 5);
  222.     TT_Text(win->RPort, "Font scaling", 12);
  223.   }
  224.  
  225. /*----------------------------------------------------------------------------------------------------*/
  226.  
  227. void main_loop(struct Window *win)
  228.   {
  229.     BOOL running = TRUE;
  230.     ULONG signals, portmask, idcmp_class;
  231.     struct IntuiMessage *msg;
  232.  
  233.     portmask = 1 << win->UserPort->mp_SigBit;
  234.  
  235.     while (running)
  236.       {
  237.         signals = Wait(portmask | SIGBREAKF_CTRL_C);
  238.  
  239.         if (signals & SIGBREAKF_CTRL_C) running = FALSE;
  240.  
  241.         if (signals & portmask)
  242.           {
  243.             while (msg = (struct IntuiMessage*)GetMsg(win->UserPort))
  244.               {
  245.                 idcmp_class = msg->Class;
  246.                 ReplyMsg((struct Message*)msg);
  247.  
  248.                 switch (idcmp_class)
  249.                   {
  250.                     case IDCMP_CLOSEWINDOW:
  251.                       running = FALSE;
  252.                     break;
  253.  
  254.                     case IDCMP_NEWSIZE:
  255.                       resize_text(win);
  256.                     break;
  257.                   }
  258.               }
  259.           }
  260.       }
  261.     return;
  262.   }
  263.  
  264. /*----------------------------------------------------------------------------------------------------*/
  265.  
  266. int Main (void)
  267.   {
  268.     struct Window *win;
  269.     struct Screen *wb;
  270.     STRPTR fontname, text = "Font scaling";
  271.     APTR font;
  272.     WORD ww, wh;
  273.     ULONG pen;
  274.  
  275.     if (init())
  276.       {
  277.         if (fontname = get_font_name(AslBase))
  278.           {
  279.             if (font = TT_OpenFont(
  280.               TT_FontFile, (ULONG)fontname,
  281.               TT_FontSize, 50,
  282.             TAG_END))
  283.               {
  284.                 if (wb = LockPubScreen(NULL))
  285.                   {
  286.                     if (TT_SetFont(&wb->RastPort, font))
  287.                       {
  288.                       TT_SetAttrs(&wb->RastPort,
  289.                         TT_Screen, (ULONG)wb,
  290.                         TT_Antialias, TT_Antialias_On,
  291.                       TAG_END);
  292.                       TT_TextExtent(&wb->RastPort, text, 12, &te);
  293.                       ww = te.te_Extent.MaxX - te.te_Extent.MinX + 10;
  294.                       wh = te.te_Extent.MaxY - te.te_Extent.MinY + 10;
  295.                       load_picture(wb);
  296.                       if (win = OpenWindowTags(NULL,
  297.                         WA_Top, wb->BarHeight + 1,
  298.                         WA_InnerWidth, ww + 1,
  299.                         WA_InnerHeight, wh + 1,
  300.                         WA_Title, (ULONG)"TTEngine font scaling",
  301.                         WA_CloseGadget, TRUE,
  302.                         WA_DepthGadget, TRUE,
  303.                         WA_DragBar, TRUE,
  304.                         WA_SizeGadget, TRUE,
  305.                         WA_MinHeight, 50,
  306.                         WA_MinWidth, 100,
  307.                         WA_MaxHeight, 20000,
  308.                         WA_MaxWidth, 20000,
  309.                         WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_NEWSIZE,
  310.                         WA_BackFill, (ULONG)&bf_hook,
  311.                       TAG_END))
  312.                         {
  313.                           pen = initialize_window(win, font, &te);
  314.                           main_loop(win);
  315.                           TT_DoneRastPort(win->RPort);
  316.                           CloseWindow(win);
  317.                           ReleasePen(win->WScreen->ViewPort.ColorMap, pen);
  318.                         }
  319.                       }
  320.                     UnlockPubScreen(NULL, wb);
  321.                   }
  322.                 TT_CloseFont(font);
  323.               }
  324.             free_font_name(fontname);
  325.           }
  326.       }
  327.     cleanup();
  328.     return 0;
  329.   }
  330.