font.c

Go to the documentation of this file.
00001 
00002 /*  $Id: font.c 1254 2006-04-12 20:27:14Z sethdill $    */
00003 
00004 /******************************************************************************
00005 
00006     UserLand Frontier(tm) -- High performance Web content management,
00007     object database, system-level and Internet scripting environment,
00008     including source code editing and debugging.
00009 
00010     Copyright (C) 1992-2004 UserLand Software, Inc.
00011 
00012     This program is free software; you can redistribute it and/or modify
00013     it under the terms of the GNU General Public License as published by
00014     the Free Software Foundation; either version 2 of the License, or
00015     (at your option) any later version.
00016 
00017     This program is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020     GNU General Public License for more details.
00021 
00022     You should have received a copy of the GNU General Public License
00023     along with this program; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 
00026 ******************************************************************************/
00027 
00028 #include "frontier.h"
00029 #include "standard.h"
00030 
00031 #include "PAIGE.H"
00032 #include "PGDEFTBL.H"
00033 
00034 #include "strings.h"
00035 #include "font.h"
00036 #include "quickdraw.h"
00037 
00038 
00039 FontInfo globalfontinfo;
00040 
00041 static bigstring cachedfontname;
00042 static short cachedfontnum;
00043 
00044 #ifdef WIN95VERSION
00045 typedef struct tywindowsfontinformation {
00046     
00047     FontInfo    fi;
00048 
00049     LOGFONT     lfi;
00050     } tywindowsfontinformation;
00051 
00052 tywindowsfontinformation ** globalWindowsFontInfo;
00053 
00054 short globalFontCount;
00055 
00056 static short currentFontNum;
00057 static short currentFontSize;
00058 static short currentFontStyle;
00059 
00060 static HFONT globalOldFont = 0;
00061 static HFONT globalCurrentFont = 0;
00062 
00063 static BOOL FAR PASCAL EnumFamCallBack1(ENUMLOGFONT * lplf, LPNEWTEXTMETRIC lpntm, DWORD FontType, LPVOID counter) {
00064     
00065     short * ctr = (short *) counter;
00066     *ctr = *ctr + 1;
00067     return TRUE; 
00068     } /*EnumFamCallBack*/
00069 
00070 
00071 static BOOL FAR PASCAL EnumFamCallBack2(ENUMLOGFONT * lplf, LPNEWTEXTMETRIC lpntm, DWORD FontType, LPVOID counter) {
00072     
00073     tywindowsfontinformation * wfi;
00074     short * ctr = (short *) counter;
00075     *ctr = *ctr + 1;
00076     wfi = *globalWindowsFontInfo + *ctr;
00077     wfi->lfi = lplf->elfLogFont;
00078     wfi->fi.ascent = (short)lpntm->tmAscent;
00079     wfi->fi.descent = (short)lpntm->tmDescent;
00080     wfi->fi.leading = (short)lpntm->tmInternalLeading;
00081     return TRUE; 
00082     } /*EnumFamCallBack2*/
00083 
00084 
00085 short geneva = 1;
00086 
00087 static void initgenevafont (void) {
00088 
00089     fontgetnumber ("\x0d" "MS Sans Serif", &geneva);
00090     
00091     if (geneva == 0)
00092         fontgetnumber ("\x05" "Arial", &geneva);
00093     } /*initgenevafont*/
00094 
00095 
00096 void getWindowsLogFont (LOGFONT *lf) {
00097     
00098     short ctr;
00099     tywindowsfontinformation * wfi;
00100 
00101     if (globalCurrentFont != 0)
00102         return;
00103 
00104     ctr = currentFontNum;
00105 
00106     if (ctr == 0)
00107         ctr = ctr + 1;
00108 
00109     wfi = *globalWindowsFontInfo + ctr;
00110 
00111     *lf = wfi->lfi;
00112     lf->lfHeight = -MulDiv(currentFontSize, GetDeviceCaps(getcurrentDC(), LOGPIXELSY), 72);
00113     lf->lfWidth = 0;
00114     } /*getWindowsLogFont*/
00115 
00116 
00117 void setWindowsFont () {
00118     
00119     short ctr;
00120     tywindowsfontinformation * wfi;
00121 
00122     if (globalCurrentFont != 0)
00123         return;
00124 
00125     ctr = currentFontNum;
00126 
00127     if (ctr == 0)
00128         ctr = ctr + 1;
00129 
00130     wfi = *globalWindowsFontInfo + ctr;
00131 
00132     wfi->lfi.lfHeight = -MulDiv(currentFontSize, GetDeviceCaps(getcurrentDC(), LOGPIXELSY), 72);
00133     
00134     wfi->lfi.lfWidth = 0;
00135     
00136     wfi->lfi.lfWeight = (currentFontStyle & bold)? FW_BOLD : FW_NORMAL;
00137     
00138     wfi->lfi.lfItalic = (currentFontStyle & italic)? true : false;
00139     
00140     wfi->lfi.lfUnderline = (currentFontStyle & underline)? true : false;
00141      
00142     globalCurrentFont = CreateFontIndirect (&(wfi->lfi));
00143 
00144     globalOldFont = SelectObject (getcurrentDC(), globalCurrentFont);
00145     } /*setWindowsFont*/
00146 
00147 
00148 void clearWindowsFont () {
00149     
00150     SelectObject (getcurrentDC(), globalOldFont);
00151     DeleteObject (globalCurrentFont);
00152 
00153     globalCurrentFont = 0;
00154     globalOldFont = 0;
00155     } /*clearWindowsFont*/
00156 
00157     
00158 boolean findWindowsFont (ptrstring fontname, short *fontnumber) {
00159 
00160     short i;
00161     char fn[256];
00162     tywindowsfontinformation * wfi;
00163 
00164     memmove (fn, stringbaseaddress(fontname), stringlength(fontname));
00165     fn[stringlength(fontname)] = 0;
00166 
00167     for (i = 1; i <= globalFontCount; i++) {
00168         
00169         wfi = *globalWindowsFontInfo + i;
00170         
00171         if (stricmp (wfi->lfi.lfFaceName, fn)==0) {
00172 
00173             *fontnumber = i;
00174             
00175             return (true);
00176             }
00177         }
00178 
00179     return (false);
00180     } /*findWindowsFont*/
00181 
00182 #endif
00183 
00184 static boolean mapxfont (ptrstring bsfont) {
00185     
00186     /*
00187     2002-10-13 AR: Slight change to eliminate compiler warning about possible unwanted assignment
00188     */
00189 
00190     register pg_char_ptr        table_ptr;
00191     short                       alternate_index, out_index;
00192 
00193     if ((table_ptr = cross_font_table) == (pg_char_ptr)NULL)
00194         return (false);
00195     
00196     while (*table_ptr <= bsfont[1]) {
00197         
00198         alternate_index = pgCompareFontTable(&bsfont[1], table_ptr);
00199         
00200         if (alternate_index != 0) {
00201             
00202             table_ptr += alternate_index;
00203             pgFillBlock(bsfont, FONT_SIZE * sizeof(pg_char), 0);
00204             
00205             for (out_index = 1; out_index < FONT_SIZE; ++out_index) {
00206                 
00207                 bsfont[out_index] = *table_ptr++;
00208                 bsfont[0] += 1;
00209                 
00210                 if (*table_ptr == ']' || *table_ptr == 0)
00211                     break;
00212                 }
00213             
00214             break;
00215             }
00216         
00217         for (;;) {
00218             
00219             if (*table_ptr++ == 0)
00220                 break;
00221             }
00222         }
00223     
00224     return (true);
00225     } /*mapxfont*/
00226 
00227 
00228 void fontgetnumber (bigstring fontname, short *fontnumber) {
00229     
00230     /*
00231     5.0a13 dmb: operate on a copy; mapping modifies string (64 bytes!)
00232     5.0a13 dmb: map on Mac platform too
00233     
00234     5.0.2b19 dmb: added cachedfont logic
00235     */
00236     
00237     bigstring bsfont;
00238     
00239     copystring (fontname, bsfont);
00240     
00241     nullterminate (bsfont);
00242     
00243     if (equalstrings (cachedfontname, bsfont))
00244         goto exit;
00245     
00246     copystring (bsfont, cachedfontname);
00247     
00248 #ifdef MACVERSION   
00249     GetFNum (bsfont, &cachedfontnum);
00250 
00251     if ((cachedfontnum == 0) && !equalstrings (bsfont, BIGSTRING ("\x07" "Chicago")))
00252         if (mapxfont (bsfont))
00253             GetFNum (bsfont, &cachedfontnum);
00254 #endif
00255 #ifdef WIN95VERSION
00256     if (findWindowsFont (bsfont, &cachedfontnum))
00257         goto exit;
00258 
00259     if (mapxfont (bsfont))
00260         if (findWindowsFont (bsfont, &cachedfontnum))
00261             goto exit;
00262 
00263     cachedfontnum = 0;
00264 #endif
00265     
00266     exit:
00267     
00268     *fontnumber = cachedfontnum;
00269     } /*fontgetnumber*/
00270 
00271 
00272 void fontgetname (short fontnumber, bigstring fontname) {
00273 #ifdef MACVERSION       
00274     GetFontName (fontnumber, fontname);
00275 #endif
00276 #ifdef WIN95VERSION
00277     tywindowsfontinformation * wfi;
00278 
00279     wfi = *globalWindowsFontInfo + fontnumber;
00280 
00281     strcpy (stringbaseaddress(fontname), wfi->lfi.lfFaceName);
00282     setstringlength (fontname, strlen(wfi->lfi.lfFaceName));
00283 #endif
00284     } /*fontgetname*/
00285 
00286 
00287 void setfontsizestyle (short fontnum, short fontsize, short fontstyle) {
00288 #ifdef MACVERSION
00289     TextFont (fontnum);
00290     
00291     TextSize (fontsize);
00292     
00293     TextFace (fontstyle);
00294 #endif
00295 #ifdef WIN95VERSION
00296     if (fontnum > 0)
00297         currentFontNum = fontnum;
00298 
00299     if (fontsize != 0)
00300         currentFontSize = fontsize;
00301 
00302     currentFontStyle = fontstyle;
00303 #endif
00304     } /*setfontsizestyle*/
00305     
00306     
00307 void setglobalfontsizestyle (short fontnum, short fontsize, short fontstyle) {
00308 #ifdef MACVERSION   
00309     setfontsizestyle (fontnum, fontsize, fontstyle);
00310     
00311     GetFontInfo (&globalfontinfo);
00312 #endif
00313 
00314 #ifdef WIN95VERSION
00315     HFONT fnt, oldfnt;
00316     TEXTMETRIC tm;
00317     tywindowsfontinformation * wfi;
00318     short ctr;
00319     HDC hdc;
00320     long logpix;
00321 
00322     hdc = GetDC (GetDesktopWindow());
00323 
00324     if (fontnum > 0)
00325         currentFontNum = fontnum;
00326 
00327     if (fontsize != 0)
00328         currentFontSize = fontsize;
00329 
00330     currentFontStyle = fontstyle;
00331 
00332     ctr = currentFontNum;
00333 
00334 //  ctr = ctr + 1;
00335     wfi = *globalWindowsFontInfo + ctr;
00336 
00337     wfi->lfi.lfHeight = -MulDiv(currentFontSize, GetDeviceCaps(getcurrentDC(), LOGPIXELSY), 72);
00338     
00339     wfi->lfi.lfWidth = 0;
00340     
00341     wfi->lfi.lfWeight = (currentFontStyle & bold)? FW_BOLD : FW_NORMAL;
00342     
00343     wfi->lfi.lfItalic = (currentFontStyle & italic)? true : false;
00344     
00345     wfi->lfi.lfUnderline = (currentFontStyle & underline)? true : false;
00346 
00347     fnt = CreateFontIndirect (&(wfi->lfi));
00348 
00349     oldfnt = SelectObject (hdc, fnt);
00350 
00351     GetTextMetrics (hdc, &tm);
00352 
00353     logpix = GetDeviceCaps(getcurrentDC(), LOGPIXELSY);
00354 
00355     globalfontinfo.ascent = (short) tm.tmAscent;
00356     globalfontinfo.descent = (short) tm.tmDescent;
00357     globalfontinfo.leading = (short) tm.tmExternalLeading;
00358 //  globalfontinfo.leading = (short)tm.tmInternalLeading;
00359 
00360     SelectObject (hdc, oldfnt);
00361     DeleteObject (fnt);
00362 
00363     ReleaseDC (GetDesktopWindow(), hdc);
00364 #endif
00365     } /*setglobalfontsizestyle*/
00366 
00367     
00368 #if !flruntime
00369 
00370 boolean realfont (short fontnum, short fontsize) {
00371 #ifdef MACVERSION   
00372     return (RealFont (fontnum, fontsize));
00373 #endif
00374 #ifdef WIN95VERSION
00375     if ((fontnum > 0) && (fontnum <= globalFontCount))
00376         return (true);
00377 
00378     return (false);
00379 #endif
00380     } /*realfont*/
00381 
00382 
00383 short setnamedfont (bigstring bs, short fsize, short fstyle, short defaultfont) {
00384 
00385     /*
00386     give me the name of a font you like.  I'll try to set to that font,
00387     but if its not available, you get the default font.  
00388     */  
00389     short fontnum;
00390     
00391     fontgetnumber (bs, &fontnum);
00392     
00393     if (fontnum == 0) 
00394         fontnum = defaultfont; /*use caller's second choice*/
00395     
00396     setglobalfontsizestyle (fontnum, fsize, fstyle);
00397     
00398     return (fontnum); /*return the font that we are actually using*/
00399     } /*setnamedfont*/
00400 
00401 #endif
00402 
00403 
00404 void getfontsizestyle (short *fontnum, short *fontsize, short *fontstyle) {
00405 #ifdef MACVERSION   
00406     //Code change by Timothy Paustian Monday, May 1, 2000 8:36:17 PM
00407     //Changed to Opaque call for Carbon
00408     #if ACCESSOR_CALLS_ARE_FUNCTIONS == 1
00409     CGrafPtr    thePort = GetQDGlobalsThePort();
00410     *fontnum = GetPortTextFont(thePort);
00411     *fontsize = GetPortTextSize(thePort);
00412     *fontstyle = GetPortTextFace(thePort);
00413     
00414     #else
00415     //old code
00416     *fontnum = (*qd.thePort).txFont;
00417     *fontsize = (*qd.thePort).txSize;
00418     *fontstyle = (*qd.thePort).txFace;
00419     #endif
00420 #endif
00421 #ifdef WIN95VERSION
00422     *fontnum = currentFontNum;
00423     *fontsize = currentFontSize;
00424     *fontstyle = currentFontStyle;
00425 #endif
00426     } /*getfontsizestyle*/
00427 
00428 
00429 #if 0
00430 
00431 void fontstring (short fontnum, short fontsize, boolean flhavefont, boolean flhavesize, bigstring bs) {
00432     
00433     bigstring bsint;
00434     
00435     setstringlength(bs,0); /*set it to the empty string*/
00436     
00437     if (flhavefont)
00438         fontgetname (fontnum, bs);
00439         
00440     if (flhavesize) {   
00441     
00442         NumToString (fontsize, bsint);
00443         
00444         if (flhavefont) {
00445             
00446             pushspace (bs);
00447             
00448             pushstring (bsint, bs); 
00449             }       
00450         else
00451             copystring ((ptrstring) "\pno consistent font", bs);
00452         }
00453     } /*fontstring*/
00454 
00455 
00456 
00457 void getstyle (style, flplain, flbold, flitalic, flunderline, floutline, flshadow) 
00458 
00459     /*
00460     give me a style bit array, and I'll extract the booleans which are 
00461     slightly easier to deal with.
00462     
00463     if none of the others are true, we set flplain to true.  otherwise 
00464     flplain is false.
00465     */
00466 
00467     short style; 
00468     boolean *flplain, *flbold, *flitalic, *flunderline, *floutline, *flshadow; 
00469     
00470     {
00471     *flplain = true;  /*default values*/
00472     
00473     *flbold = false;
00474     
00475     *flitalic = false;
00476     
00477     *flunderline = false;
00478     
00479     *floutline = false;
00480     
00481     *flshadow = false;
00482     
00483     if (style >= shadow) {
00484         
00485         style -= shadow;
00486         
00487         *flshadow = true;
00488         
00489         *flplain = false;
00490         }
00491         
00492     if (style >= outline) {
00493         
00494         style -= outline;
00495         
00496         *floutline = true;
00497         
00498         *flplain = false;
00499         }
00500         
00501     if (style >= underline) {
00502         
00503         style -= underline;
00504         
00505         *flunderline = true;
00506         
00507         *flplain = false;
00508         }
00509         
00510     if (style >= italic) {
00511         
00512         style -= italic;
00513         
00514         *flitalic = true;
00515         
00516         *flplain = false;
00517         }
00518         
00519     if (style >= bold) {
00520         
00521         style -= bold;
00522         
00523         *flbold = true;
00524         
00525         *flplain = false;
00526         }
00527     } /*getstyle*/
00528 
00529 #endif
00530 
00531 
00532 void diskgetfontname (short fontnum, diskfontstring fontname) {
00533     
00534     /*
00535     Apple recommends that fonts be stored on disk as strings.  we return the
00536     fontname, limited in length to 32, based on the indicated font number.
00537     */
00538     
00539     bigstring bs;
00540     
00541     fontgetname (fontnum, bs);
00542     
00543     if (stringlength (bs) > diskfontnamelength)
00544         setstringlength (bs, diskfontnamelength);
00545     
00546     copystring (bs, (ptrstring) fontname);
00547     } /*disksavefontname*/
00548 
00549 
00550 void diskgetfontnum (diskfontstring fontname, short *fontnum) {
00551     
00552     if (stringlength (fontname) == 0)
00553         *fontnum = geneva;
00554     else
00555         fontgetnumber ((ptrstring) fontname, fontnum);
00556     } /*diskloadfontname*/
00557     
00558         
00559 boolean initfonts (void) {
00560     
00561     #ifdef WIN95VERSION
00562         HDC hdc;
00563         short fontnum;
00564 
00565         hdc = GetDC (GetDesktopWindow());
00566 
00567         globalFontCount = 0;
00568 
00569         EnumFontFamilies(hdc, (LPCTSTR) NULL, 
00570             (FONTENUMPROC) EnumFamCallBack1, (LPARAM) &globalFontCount); 
00571 
00572         globalWindowsFontInfo = (tywindowsfontinformation **) NewHandle ((globalFontCount + 1) * sizeof (tywindowsfontinformation));
00573 
00574         if (globalWindowsFontInfo != NULL) {
00575             globalFontCount = 0;
00576 
00577             EnumFontFamilies(hdc, (LPCTSTR) NULL, 
00578                 (FONTENUMPROC) EnumFamCallBack2, (LPARAM) &globalFontCount); 
00579             }
00580 
00581 
00582         fontgetnumber ("\005Arial", &fontnum);
00583         setglobalfontsizestyle (fontnum, 12, 0);        /* set defaults*/
00584         ReleaseDC (GetDesktopWindow(), hdc);
00585 
00586         initgenevafont ();
00587     #endif
00588     
00589     setemptystring (cachedfontname);
00590     
00591     return (true);
00592     } /*initfonts*/
00593 
00594 

Generated on Wed May 31 18:19:47 2006 for frontierkernel 10.1.10a by  doxygen 1.4.6