home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / printer / gnuplot.lha / amiga.trm next >
Text File  |  1991-07-01  |  8KB  |  256 lines

  1. /* GNUPLOT - amiga.trm */
  2. /*
  3.  * Copyright (C) 1991
  4.  *
  5.  * Permission to use, copy, and distribute this software and its
  6.  * documentation for any purpose with or without fee is hereby granted, 
  7.  * provided that the above copyright notice appear in all copies and 
  8.  * that both that copyright notice and this permission notice appear 
  9.  * in supporting documentation.
  10.  *
  11.  * Permission to modify the software is granted, but not the right to
  12.  * distribute the modified code.  Modifications are to be distributed 
  13.  * as patches to released version.
  14.  *  
  15.  * This software  is provided "as is" without express or implied warranty.
  16.  * 
  17.  * This file is included by ../term.c.
  18.  *
  19.  * This terminal driver supports:
  20.  *   Amiga Custom Screen
  21.  *
  22.  * AUTHORS
  23.  *   Carsten Steger
  24.  * 
  25.  * send your comments or suggestions to (pixar!info-gnuplot@sun.com).
  26.  * 
  27.  */
  28.  
  29. #include <exec/types.h>
  30. #include <intuition/intuitionbase.h>
  31. #include <proto/intuition.h>
  32. #include <proto/graphics.h>
  33. #include <proto/exec.h>
  34. #include <proto/diskfont.h>
  35.  
  36. extern char *getenv(),*strchr();
  37.  
  38. #define AMIGA_XMAX 640
  39. #define AMIGA_YMAX 512
  40.  
  41. #define AMIGA_VCHAR (12)
  42. #define AMIGA_HCHAR (8)
  43. #define AMIGA_VTIC (AMIGA_YMAX/80)
  44. #define AMIGA_HTIC (AMIGA_XMAX/80)
  45. /* The origin is in the upper left hand corner, so we have to translate */
  46. /* and flip the coordinates: */
  47. #define AMIGA_VTF(y) (AMIGA_ymax-1-(y))
  48.  
  49.  
  50. struct IntuitionBase *IntuitionBase;
  51. struct GfxBase *GfxBase;
  52. struct Library *DiskfontBase;
  53. static struct TextAttr AMIGA_Font = {
  54.   "topaz.font",TOPAZ_EIGHTY,FS_NORMAL,FPF_ROMFONT
  55. };
  56. static struct TextFont *AMIGA_TextFont;
  57. static struct NewScreen AMIGA_NewScreen = {
  58.   0,0,AMIGA_XMAX,AMIGA_YMAX,4,15,0,HIRES|LACE,
  59.   CUSTOMSCREEN|SCREENBEHIND|SCREENQUIET,NULL,NULL,NULL,NULL
  60. };
  61. static struct Screen *AMIGA_Screen;
  62. static UWORD AMIGA_Colors [] = {
  63.   0x000,0xfff,0xbbb,0x0f0,0xf00,0x00f,0x3ca,0xf0f,
  64.   0x94d,0x0ff,0x82f,0xff0,0x0af,0xc5e,0xfa2,0xf44
  65. };
  66. static int AMIGA_slinetype;
  67. static enum JUSTIFY AMIGA_justify = LEFT;
  68. static unsigned int AMIGA_ymax,AMIGA_xmax; 
  69. static WORD AMIGA_cwd,AMIGA_cht,AMIGA_bsl,AMIGA_vadj;
  70.  
  71. AMIGA_reset()
  72. {
  73.   if (AMIGA_TextFont != NULL) CloseFont(AMIGA_TextFont);
  74.   if (DiskfontBase != NULL) CloseLibrary(DiskfontBase);
  75.   if (AMIGA_Screen != NULL) CloseScreen(AMIGA_Screen);
  76.   if (IntuitionBase != NULL) CloseLibrary(IntuitionBase);
  77.   if (GfxBase != NULL) CloseLibrary(GfxBase);
  78.   AMIGA_TextFont = NULL;
  79.   DiskfontBase = NULL;
  80.   AMIGA_Screen = NULL;
  81.   IntuitionBase = NULL;
  82.   GfxBase = NULL;
  83. }
  84.  
  85.  
  86. AMIGA_init()
  87. {
  88.   static char fontname[80],*gnufont,*search;
  89.   static int fsize;
  90.   static char *test_str =
  91.     " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
  92.   static WORD test_len,test_pxl;
  93.  
  94.   GfxBase = OpenLibrary("graphics.library",0);
  95.   if (GfxBase == NULL) {
  96.     fprintf(stderr,"No Graphics-Library\n");
  97.     AMIGA_reset();
  98.     exit(20);
  99.   }
  100.   IntuitionBase = OpenLibrary("intuition.library",0);
  101.   if (IntuitionBase == NULL) {
  102.     fprintf(stderr,"No Intuition-Library\n");
  103.     AMIGA_reset();
  104.     exit(20);
  105.   }
  106.   /* We compute the vertical resolution for those poor NTSC-souls   :-)   */
  107.   AMIGA_Screen = IntuitionBase->FirstScreen; /* Search for Workbench-Screen */
  108.   while ((AMIGA_Screen != NULL) && ((AMIGA_Screen->Flags & 0xf) != WBENCHSCREEN))
  109.     AMIGA_Screen = AMIGA_Screen->NextScreen;
  110.   if (AMIGA_Screen->ViewPort.Modes & LACE) AMIGA_ymax = AMIGA_Screen->Height;
  111.   else AMIGA_ymax = AMIGA_Screen->Height * 2;
  112.   AMIGA_xmax = 640;
  113.   term_tbl[term].xmax = AMIGA_xmax;
  114.   term_tbl[term].ymax = AMIGA_ymax;
  115.   AMIGA_NewScreen.Width = AMIGA_xmax;
  116.   AMIGA_NewScreen.Height = AMIGA_ymax;
  117.   AMIGA_Screen = OpenScreen(&AMIGA_NewScreen);
  118.   if (AMIGA_Screen == NULL) {
  119.     fprintf(stderr,"No Screen\n");
  120.     AMIGA_reset();
  121.     exit(20);
  122.   }
  123.   gnufont = getenv("GNUFONT");
  124.   if (gnufont != NULL ) {
  125.     search = strchr(gnufont,'/');
  126.     if (search != NULL) {
  127.       *search++ = '\0';
  128.       strncpy(fontname,gnufont,74);
  129.       strcat(fontname,".font");
  130.       sscanf(search,"%d",&fsize);
  131.       /* Avoid opening "diskfont.library" if a built-in font is desired */
  132.       if ((strcmp("topaz.font",fontname) == 0) &&
  133.         ((fsize == TOPAZ_EIGHTY) || (fsize == TOPAZ_SIXTY))) {
  134.         AMIGA_Font.ta_Name = fontname;
  135.         AMIGA_Font.ta_YSize = fsize;
  136.         AMIGA_Font.ta_Style = FS_NORMAL;
  137.         AMIGA_Font.ta_Flags = FPF_ROMFONT;
  138.         AMIGA_TextFont = OpenFont(&AMIGA_Font);
  139.         if (AMIGA_TextFont != NULL) 
  140.           SetFont(&AMIGA_Screen->RastPort,AMIGA_TextFont);
  141.       } else {
  142.         DiskfontBase = OpenLibrary("diskfont.library",0);
  143.         if (DiskfontBase != NULL) {
  144.           AMIGA_Font.ta_Name = fontname;
  145.           AMIGA_Font.ta_YSize = fsize;
  146.           AMIGA_Font.ta_Style = FS_NORMAL;
  147.           AMIGA_Font.ta_Flags = FPF_ROMFONT|FPF_DISKFONT;
  148.           AMIGA_TextFont = OpenDiskFont(&AMIGA_Font);
  149.           if (AMIGA_TextFont != NULL)
  150.             SetFont(&AMIGA_Screen->RastPort,AMIGA_TextFont);
  151.         }
  152.       }
  153.     }
  154.   }
  155.   /* Width of characters: This works better for proportional fonts than */
  156.   /* AMIGA_Screen->RastPort.TxWidth + AMIGA_Screen->RastPort.TxSpacing */
  157.   test_len = strlen(test_str);
  158.   test_pxl = TextLength(&AMIGA_Screen->RastPort,test_str,test_len);
  159.   AMIGA_cwd = test_pxl / test_len;
  160.   AMIGA_cht = AMIGA_Screen->RastPort.TxHeight; /* Height of characters */
  161.   AMIGA_bsl = AMIGA_Screen->RastPort.TxBaseline; /* Reference line */
  162.   /* Amount by which characters have to be shifted upwards to be */
  163.   /* vertically justified: */
  164.   AMIGA_vadj = AMIGA_bsl / 2;
  165.   term_tbl[term].v_char = AMIGA_cht + 4; /* So lines won't be too close */
  166.   term_tbl[term].h_char = AMIGA_cwd;
  167.   LoadRGB4(&AMIGA_Screen->ViewPort,AMIGA_Colors,16);
  168.   RemakeDisplay();
  169.   AMIGA_slinetype = 1;
  170.   SetAPen(&AMIGA_Screen->RastPort,AMIGA_slinetype);
  171.   SetDrMd(&AMIGA_Screen->RastPort,JAM1);
  172. }
  173.  
  174.  
  175. AMIGA_text()
  176. {
  177.   char c;
  178.  
  179.   c = getc(stdin); /* This is extremely ugly... Yuk !!!!   >:-(   */
  180.   ungetc(c,stdin); /* Maybe someone else will find a better solution */
  181.   ScreenToBack(AMIGA_Screen);
  182. }
  183.  
  184.  
  185. AMIGA_graphics()
  186. {
  187.   SetRast(&AMIGA_Screen->RastPort,0);
  188.   SetAPen(&AMIGA_Screen->RastPort,AMIGA_slinetype);
  189.   ScreenToFront(AMIGA_Screen);
  190. }
  191.  
  192.  
  193. AMIGA_move(x,y)
  194. unsigned int x,y;
  195. {
  196.   if ((x>=AMIGA_xmax) || (y>=AMIGA_ymax)) return;
  197.   Move(&AMIGA_Screen->RastPort,x,AMIGA_VTF(y));
  198. }
  199.  
  200.  
  201. AMIGA_vector(x,y)
  202. unsigned int x,y;
  203. {
  204.   if ((x>=AMIGA_xmax) || (y>=AMIGA_ymax)) return;
  205.   Draw(&AMIGA_Screen->RastPort,x,AMIGA_VTF(y));
  206. }
  207.  
  208.  
  209. AMIGA_linetype(linetype)
  210. int linetype;
  211. {
  212.   if (linetype >= 13) linetype %= 13;
  213.   AMIGA_slinetype = linetype+3;
  214.   SetAPen(&AMIGA_Screen->RastPort,AMIGA_slinetype);
  215. }
  216.  
  217.  
  218. AMIGA_put_text(x,y,str)
  219. unsigned int x,y;
  220. char *str;
  221. {
  222.   LONG len,tx_len;
  223.   WORD xmin,xmax,ymin,ymax;
  224.  
  225.   len = strlen(str);
  226.   tx_len = TextLength(&AMIGA_Screen->RastPort,str,len);
  227.   switch (AMIGA_justify) {
  228.     case LEFT:
  229.       xmin = x;
  230.       xmax = x + tx_len;
  231.       break;
  232.     case CENTRE:
  233.       xmin = x - tx_len / 2;
  234.       xmax = x + tx_len - tx_len / 2; /* aviod roundoff errors ! */
  235.       break;
  236.     case RIGHT:
  237.       xmin = x - tx_len;
  238.       xmax = x;
  239.       break;
  240.   }
  241.   ymin = AMIGA_VTF(y) - AMIGA_vadj;
  242.   ymax = ymin + AMIGA_cht;
  243.   /* Check if character-string lies completely within the screen: */
  244.   if ((xmax >= AMIGA_xmax) || (ymin < 0) || (ymax >= AMIGA_ymax)) return;
  245.   Move(&AMIGA_Screen->RastPort,xmin,ymin+AMIGA_bsl);
  246.   Text(&AMIGA_Screen->RastPort,str,len);
  247. }
  248.  
  249.  
  250. int AMIGA_justify_text(mode)
  251. enum JUSTIFY mode;
  252. {
  253.   AMIGA_justify = mode;
  254.   return TRUE;
  255. }
  256.