home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_11 / splash / chgfnt.cpp < prev    next >
Text File  |  1993-01-15  |  4KB  |  134 lines

  1. #ifdef    TESTCHGFNT
  2. // Read an AmiPro style sheet and change any unknown font
  3. #include    <fstream.h>
  4. #include    <stdlib.h>
  5.  
  6. #ifdef    __TURBOC__
  7. #pragma hdrstop
  8. #endif
  9.  
  10. #include    "splash.h"
  11. #include    "assoc.h"
  12.  
  13. void main(int argc, char **argv)
  14. {
  15. char c, *infn;
  16. ifstream ini("\\win3\\win.ini");
  17. ofstream fout("t.sty");
  18. int ln= 0;
  19. SPString w;
  20. SPStringList l;
  21. SPStringList ttfonts;
  22. Assoc<SPString> repfnts("", "");  // saves font replacement names
  23.  
  24.     if(argc < 2) infn= "test.sty";
  25.     else infn= argv[1];
  26.  
  27.     ifstream fin(infn);
  28.  
  29.     if(!ini){
  30.         cerr << "Can't open \\win3\\win.ini" << endl;
  31.         exit(1);
  32.     }
  33.  
  34.     if(!fin){
  35.         cerr << "Can't open " << infn << endl;
  36.         exit(1);
  37.     }
  38.  
  39.     if(!fout){
  40.         cerr << "Can't open t.sty for write" << endl;
  41.         exit(1);
  42.     }
  43.  
  44.     cout << "Reading in truetype fonts" << endl;
  45.  
  46.     while(ini >> w){ // find the [fonts] section
  47.     if(w.m("\\[fonts\\]")) break;
  48.     }
  49.  
  50. //    cout << buf << endl;
  51.  
  52.     if(!ini.good()){ // checks all file state
  53.         cerr << "Couldn't find [fonts] section in win.ini" << endl;
  54.         exit(1);
  55.     }
  56.  
  57.     // make a list of truetype fonts
  58.     Regexp r1("^([a-zA-Z ]+) \\(([a-zA-Z ]+)\\)=");
  59.     Regexp r2("^TrueType$");
  60.     Regexp r3("\\[.*\\]");
  61.  
  62.     while(ini >> w){
  63.     if(w.m(r3)) break; // found the start of another section
  64.         if(w.m(r1, l) != 3) continue; // ignore this line
  65. //        cout << "Font match:" << l[1] << ", " << l[2] << endl;
  66.         if(l[2].m(r2)){
  67.             ttfonts.push(l[1]);
  68.         } 
  69.     }
  70.  
  71.     cout << "ttfonts: " << endl << ttfonts << endl;
  72.     ini.close();
  73.  
  74.     cout << "Looking for non-truetype fonts" << endl;
  75.  
  76.     SPString s, fnt, newfnt;
  77.     SPStringList sl;
  78.     while(fin >> s){
  79.     ln++;
  80. //        cout << "line " << ln << ": <" << s << ">" << endl;
  81.         if(s.m("\\[fnt\\]")){
  82.             fout << s << endl; // write out [fnt] line
  83.                    // read next line which should have font in it
  84.         if(!(fin >> s)){
  85.         cerr << "Error reading font line " << ln << endl;
  86.         exit(1);
  87.         }
  88.         ln++;
  89.         fnt= s.split("' '").join(" "); // This trims whitespace
  90. //        cout << "font name: <" << fnt << ">" << endl;
  91.             if(!ttfonts.grep("^" + fnt + "$", "i")){ // not a truetype font
  92.                 int pos= s.index(fnt); // get position in string of font
  93.                 if(pos < 0){
  94.                     cerr << "Couldn't find <" << fnt << "> in string <" << s << "> line " << ln << endl;
  95.                     exit(1);
  96.                 }
  97.  
  98.                 // See if we already know what to exchange it with
  99.         if(repfnts.isin(fnt)) // just replace it
  100.             s.substr(pos, strlen(fnt)) = repfnts(fnt);
  101.                 else{ // need to ask what the new font name will be
  102.             do{
  103.             cout << "Replace font <" << fnt << "> with:"; cout.flush();
  104.             cin >> newfnt;
  105.             if(!(sl=ttfonts.grep("^" + newfnt + "$", "i"))){
  106.                 cerr << "<" << newfnt << "> is not a valid font" << endl;
  107.                 continue;
  108.             }
  109.             break;
  110.             }while(1);
  111.             s.substr(pos, strlen(fnt)) = sl[0]; // replace it
  112.                     repfnts(fnt) = sl[0];  // remember for next time
  113.                 }
  114.         fout << s << endl;
  115.             }else{
  116. //                cout << fnt << " is a truetype font" << endl;
  117.                 fout << s << endl; // write out original font line
  118.             }
  119.  
  120.        }else{
  121.             fout << s << endl; // echo line out
  122.         }
  123.     }while(!fin.eof());
  124.  
  125.     cout << "replacement fonts were:" << endl << repfnts << endl;
  126.  
  127.     if(fout.fail())
  128.         cerr << "Something bad happened to the output file" << endl;
  129.     fout.close();
  130.  
  131. }
  132.  
  133. #endif
  134.