home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 360b.lha / SetFont_v2.7 / SetFont.cp < prev    next >
Text File  |  1990-04-06  |  10KB  |  310 lines

  1. // =======================================================================
  2.  
  3. /* SetFont 2.7 - by Dave Haynie
  4.  
  5.             BIX:    hazy
  6.             Usenet:    {uunet|rutgers}!cbmvax!daveh
  7.             PLINK:    D-Dave H
  8.              Drink:    Guinness
  9.  
  10.    BUSINESS:
  11.  
  12.     This program is complete, real, and true public domain software.
  13.    Do with it what you will as long as you don't remove my name from it.
  14.    Feel free to enhance this as you see fit.  I may eventually make one
  15.    that's better....
  16.    
  17.    ABOUT IT:
  18.  
  19.         SetFont V2.7 cleans up all known bugs in previous SetFont versions.
  20.    The code is GREATLY cleaned up and simplified from SetFont V2.0.  I no 
  21.    longer free any fonts that were previously set.  V2.0 did too much 
  22.    freeing, but even with that, it is possible that another program could get 
  23.    a pointer to the font or font descriptor in a window or screen and choke 
  24.    if that's freed.  That may not be good behavior, but this way's safer.  A 
  25.    future version will probably track fonts opened and closed by SetFont 
  26.    itsself so that it can reclaim some of it's resources and still be 
  27.    reasonably safe.  Right now I have no way of knowing if the font or 
  28.    descriptor I see is even really owned by SetFont. SetFont 2.7 also cleans
  29.    up the font open ordering, once and for all.  Apparently, the font system
  30.    is broken or confused.  In SetFont 2.5, I opened a disk font first, and
  31.    ended up with multiple copies of a single font in RAM.  In SetFont 2.6,
  32.    I reversed the order and ended up missing disk fonts, as the software
  33.    took the closest front to what I wanted from RAM.  Now I open both damn
  34.    fonts, take the closest to what I'm looking for from the two, and close
  35.    the other.  Ties go to the RAM font.
  36.  
  37.  
  38.    CONFLICTS:
  39.  
  40.     There are a few potential problems the general notion of SetFont in 
  41.    the Amiga system.  First of all, many programs are written to support only
  42.    the topaz 8 (80 column) font (sloppy, I know, but that's life).  If you're
  43.    a 60 column user, you've probably experienced this before.  It's not a 
  44.    problem with the Amiga as a whole, since most of the system will adjust 
  45.    itself.  But it may be a problem with programs that have a fixed idea of 
  46.    what a font should look like.  Most 80 column fonts work with most 
  47.    applications, and an 80 column 8x8 font will work just about everywhere.  
  48.    Some programs, like CLI for instance, have trouble with proportionally-
  49.    spaced fonts.  The best thing to do is try out the font you like.  One 
  50.    final problem is that some applications ask the WorkBench screen to close 
  51.    when they start up.  It'll close if there's nothing else open on it, but 
  52.    when it re-opens, it'll restart with the Preferences-selected font, not 
  53.    the SetFont selected font.  Of course, preferences doesn't support 
  54.    arbitrary fonts (which is why this program is even necessary).  Oh well, 
  55.    maybe day.  
  56. */
  57.  
  58. #include <exec/types.h>
  59. #include <exec/io.h>
  60. #include <exec/ports.h>
  61. #include <exec/memory.h>
  62. #include <graphics/gfxbase.h>
  63. #include <graphics/text.h>
  64. #include <graphics/rastport.h>
  65. #include <libraries/dos.h>
  66. #include <libraries/dosextens.h>
  67. #include <intuition/intuition.h>
  68. #include <string.h>
  69. #include <ctype.h>
  70. #include <stdio.h>
  71. #include <stdlib.h>
  72.  
  73. // =========================================================================
  74.  
  75. // Miscellaneous stuff.
  76.  
  77. extern TextFont *OpenDiskFont(const TextAttr *);
  78.  
  79. inline BPTR CADDR(APTR cptr) { return BPTR(ULONG(cptr) >> 2); }
  80.  
  81. inline void fail(char *a, char *b = NULL) { printf(a,b); exit(10); }
  82.  
  83. inline UWORD abs(UWORD x) { return (x > 0)?x:(-x); }
  84.  
  85. inline MsgPort *contask(MsgPort *port) {
  86.    return (MsgPort *) ((port->mp_SigTask->ln_Type == NT_PROCESS)
  87.           ? ((Process *)port->mp_SigTask)->pr_ConsoleTask 
  88.           : NULL);
  89. }
  90.  
  91. // =========================================================================
  92.  
  93. // This is the "smart" font class.  SmartFont items stay around after the 
  94. // program exits if they appear to have been used.  As it turns out, the
  95. // Amiga font system looks somewhat broken.  If I OpenDiskFont(), I load
  96. // the font, even if it's in memory.  If I OpenFonr(), I get the closest
  97. // match to the font I want from the list of fonts in memory, not the
  98. // big picture.  So SmartFont now gets a bit smarter, and knows how to
  99. // find the best match from both possible sources.  The font opening order
  100. // is important, since if I fetch a disk font, I'm sure to find it in RAM
  101. // afterward.
  102.  
  103. class SmartFont {
  104.    private:
  105.       static BOOL aok;            // Font checks out    
  106.       TextAttr      sfattr;        // Font descriptor
  107.       
  108.    public:
  109.       SmartFont(char *n, UWORD s);
  110.  
  111.       TextFont *font();
  112.  
  113.       TextAttr *attr() {
  114.          TextAttr *a = (TextAttr *)AllocMem(sizeof(TextAttr),MEMF_PUBLIC|MEMF_CLEAR);
  115.          a->ta_Name = (char *)AllocMem(strlen(sfattr.ta_Name)+1,MEMF_PUBLIC|MEMF_CLEAR);
  116.          strcpy(a->ta_Name,sfattr.ta_Name);
  117.          a->ta_YSize = sfattr.ta_YSize;
  118.          return a;
  119.       }
  120.       char *name() { return sfattr.ta_Name; }
  121.       UWORD size() { return sfattr.ta_YSize; }
  122. };
  123.  
  124. SmartFont::SmartFont(char *n, UWORD s = 8) {
  125.    strcat(strcpy(sfattr.ta_Name = new char[strlen(n)+6],n),".font");
  126.    sfattr.ta_YSize = (s>2)?s:8;
  127.    sfattr.ta_Style = sfattr.ta_Flags = 0;
  128.  
  129.    if (!aok) {
  130.       TextFont *f = font();
  131.       CloseFont(f);
  132.       aok = TRUE;
  133.    }
  134. }
  135.  
  136. TextFont *SmartFont::font() {
  137.    TextFont *memf, *dskf;
  138.  
  139.    if ((memf = OpenFont(&sfattr)) && sfattr.ta_YSize == memf->tf_YSize)
  140.       return memf;
  141.  
  142.    dskf = OpenDiskFont(&sfattr);
  143.  
  144.    if (!memf && !dskf) fail("Font \"%s\" not found\n",sfattr.ta_Name);
  145.    if (!memf && dskf)
  146.       return dskf;
  147.    else if (!dskf && memf)
  148.       return memf;
  149.    else if (abs(sfattr.ta_YSize - memf->tf_YSize) <=
  150.             abs(sfattr.ta_YSize - dskf->tf_YSize)) {
  151.       CloseFont(dskf);
  152.       return memf;
  153.    }
  154.    CloseFont(memf);
  155.    return dskf;
  156. }
  157.  
  158. // =========================================================================
  159.  
  160. // These classes manage the places that fonts are hidden.
  161.  
  162. // This is the basic place node
  163.  
  164. class PlaceNode : public Node {
  165.    private:
  166.       static Window *pwin;
  167.       static Screen *pscr;
  168.  
  169.    public:
  170.       PlaceNode();      
  171.       PlaceNode *next() { return (PlaceNode *)Node::next(); }
  172.  
  173.       void testwindow() { if (!pwin) fail("Window not found\n"); }
  174.       void testscreen() { if (!pscr) fail("Screen not found\n"); }
  175.  
  176.       Window *window()  { return pwin; }
  177.       Screen *screen()  { return pscr; }
  178.  
  179.       virtual void set(SmartFont *f) {
  180.          testwindow();
  181.          SetFont(window()->graphic(),f->font());
  182.          printf("\033c");              // Re-init window's conunit
  183.          (void)flushall();
  184.       }
  185. };
  186.  
  187. // Initialize the static stuff, once.
  188.  
  189. PlaceNode::PlaceNode() {
  190.    if (pwin) return;
  191.  
  192.    StandardPacket *packet = new StandardPacket;
  193.    InfoData *info = new InfoData;   
  194.    MsgPort *port = new StdPort;
  195.  
  196.   // Find the window
  197.    if (contask(port)) {
  198.       packet->sendio(contask(port),port,ACTION_DISK_INFO,CADDR(info));
  199.       (void)port->wait();
  200.       pwin = (Window *)info->id_VolumeNode;
  201.    } else 
  202.       pwin = NULL;
  203.    delete port;
  204.    delete info;
  205.    delete packet;
  206.  
  207.   // Find the screen
  208.    pscr = (pwin) ? pwin->screen() : NULL;
  209. }
  210.  
  211. // These are the derived special nodes, one for each "place".
  212.  
  213. #define WindowNode    PlaceNode
  214.  
  215. class ScreenNode : public PlaceNode {
  216.    public:
  217.       void set(SmartFont *f) {
  218.          testscreen();
  219.          if (strcmp(screen()->Font->ta_Name,f->name()) == 0)
  220.             screen()->Font->ta_YSize = f->size();
  221.          else
  222.             screen()->Font = f->attr();
  223.       }
  224. };
  225.  
  226. class TitleNode : public PlaceNode {
  227.    public:
  228.       void set(SmartFont *f) {
  229.          testscreen();
  230.          SetFont(screen()->graphic(),f->font());
  231.       }
  232. };
  233.  
  234. class BarNode : public PlaceNode {
  235.    public:
  236.       void set(SmartFont *f) {
  237.          testscreen();
  238.          SetFont(screen()->BarLayer->rp,f->font());
  239.       }
  240. };
  241.  
  242. // This is the place list, which links a number of place nodes, and also
  243. // manages to go work like finding windows, etc.
  244.  
  245. class PlaceList : public List {
  246.    public:
  247.       PlaceList(int argc, char **argv);   
  248.       PlaceNode *first() { return (PlaceNode *)List::first(); }
  249. };
  250.  
  251. // The PlaceList constructor does a great deal of the work.  It looks up
  252. // the window data, then parses the command line to build the place
  253. // list.
  254.  
  255. PlaceList::PlaceList(int argc, char **argv) {
  256.   // Parse our input arguments
  257.    for (short i = 0; i < argc; ++i)
  258.       switch (toupper(argv[i][0])) {
  259.          case 'B': add(new BarNode);        break;
  260.          case 'S': add(new ScreenNode);        break;
  261.          case 'T': add(new TitleNode);        break;
  262.          case 'W': add(new WindowNode);        break;
  263.          default :                break;
  264.       }
  265.    if (is_empty()) {
  266.       add(new BarNode);
  267.       add(new ScreenNode);
  268.       add(new TitleNode);
  269.       add(new WindowNode);
  270.    }
  271. }
  272.  
  273. // =========================================================================
  274.  
  275. // Prints help notice
  276.  
  277. void PrintNotice() {
  278.    printf("SetFont V2.7 by Dave Haynie\n\n");
  279.    printf("Usage: SetFont [fontname [point [place]]]\n");
  280.    printf("  where:\n");
  281.    printf("  \2331mfontname\2330m  is the font's name (e.g. \"topaz\")\n");
  282.    printf("  \2331mpoint\2330m     is the point size (default is 8)\n");
  283.    printf("  \2331mplace\2330m     pick the place, one or more of:\n");
  284.    printf("    \2331mBAR\2330m       set the barlayer font only\n");
  285.    printf("    \2331mSCREEN\2330m    set the screen font only\n");
  286.    printf("    \2331mTITLES\2330m    set the screen titles only\n");
  287.    printf("    \2331mWINDOW\2330m    set the window's font only\n\n");
  288.    printf("If no \2331mplace\2330m switch is given, everything is set.\n\n");
  289.    exit(0);
  290. }
  291.  
  292. // The main function
  293.  
  294. void main(int argc, char *argv[]) {
  295.   // Automatic help command
  296.    if (argc < 2 || argv[1][0] == '?') PrintNotice();
  297.  
  298.   // Process the command-line arguments, AmigaDOS style.
  299.    PlaceList *plist = new PlaceList(argc-2,&argv[3]);
  300.  
  301.   // Get the font if it's there.
  302.    SmartFont *font = new SmartFont(argv[1],atoi(argv[2]));
  303.    
  304.   // Here we apply all the requested changes.
  305.    for (PlaceNode *n = plist->first(); n->next(); n = n->next()) n->set(font);
  306.  
  307.   // And we're done!
  308.    exit(0);
  309. }
  310.